Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | use anyhow::Result; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 18 | use log::{debug, warn}; |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 19 | use std::collections::{btree_map, BTreeMap, HashMap}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 20 | use std::convert::TryFrom; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 21 | use std::ffi::{CStr, OsStr}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 22 | use std::fs::OpenOptions; |
| 23 | use std::io; |
| 24 | use std::mem::MaybeUninit; |
| 25 | use std::option::Option; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 26 | use std::os::unix::{ffi::OsStrExt, io::AsRawFd}; |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 27 | use std::path::{Path, PathBuf}; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 28 | use std::sync::atomic::{AtomicU64, Ordering}; |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 29 | use std::sync::Mutex; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 30 | use std::time::Duration; |
| 31 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 32 | use fuse::filesystem::{ |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 33 | Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply, |
| 34 | SetattrValid, ZeroCopyReader, ZeroCopyWriter, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 35 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 36 | use fuse::mount::MountOption; |
| 37 | |
Victor Hsieh | ac4f3f4 | 2021-02-26 12:35:58 -0800 | [diff] [blame] | 38 | use crate::common::{divide_roundup, ChunkedSizeIter, CHUNK_SIZE}; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 39 | use crate::file::{ |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 40 | RandomWrite, ReadByChunk, RemoteDirEditor, RemoteFileEditor, RemoteFileReader, |
| 41 | RemoteMerkleTreeReader, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 42 | }; |
| 43 | use crate::fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 44 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 45 | pub type Inode = u64; |
| 46 | type Handle = u64; |
| 47 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 48 | const DEFAULT_METADATA_TIMEOUT: Duration = Duration::from_secs(5); |
| 49 | const ROOT_INODE: Inode = 1; |
| 50 | |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 51 | /// Maximum bytes in the write transaction to the FUSE device. This limits the maximum buffer |
| 52 | /// size in a read request (including FUSE protocol overhead) that the filesystem writes to. |
| 53 | const MAX_WRITE_BYTES: u32 = 65536; |
| 54 | |
| 55 | /// Maximum bytes in a read operation. |
| 56 | /// TODO(victorhsieh): This option is deprecated by FUSE. Figure out if we can remove this. |
| 57 | const MAX_READ_BYTES: u32 = 65536; |
| 58 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 59 | /// `AuthFsEntry` defines the filesystem entry type supported by AuthFS. |
| 60 | pub enum AuthFsEntry { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 61 | /// A file type that is verified against fs-verity signature (thus read-only). The file is |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 62 | /// served from a remote server. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 63 | VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 64 | reader: VerifiedFileReader<RemoteFileReader, RemoteMerkleTreeReader>, |
| 65 | file_size: u64, |
| 66 | }, |
| 67 | /// A file type that is a read-only passthrough from a file on a remote serrver. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 68 | UnverifiedReadonly { reader: RemoteFileReader, file_size: u64 }, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 69 | /// A file type that is initially empty, and the content is stored on a remote server. File |
| 70 | /// integrity is guaranteed with private Merkle tree. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 71 | VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor> }, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 72 | /// A directory type that is initially empty. One can create new file (`VerifiedNew`) and new |
| 73 | /// directory (`VerifiedNewDirectory` itself) with integrity guaranteed within the VM. |
| 74 | VerifiedNewDirectory { dir: RemoteDirEditor }, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 77 | // AuthFS needs to be `Sync` to be accepted by fuse::worker::start_message_loop as a `FileSystem`. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 78 | struct AuthFs { |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 79 | /// Table for `Inode` to `AuthFsEntry` lookup. This needs to be `Sync` to be used in |
| 80 | /// `fuse::worker::start_message_loop`. |
| 81 | inode_table: Mutex<BTreeMap<Inode, AuthFsEntry>>, |
| 82 | |
| 83 | /// Root directory entry table for path to `Inode` lookup. The root directory content should |
| 84 | /// remain constant throughout the filesystem's lifetime. |
| 85 | root_entries: HashMap<PathBuf, Inode>, |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 86 | |
| 87 | /// The next available inode number. |
| 88 | next_inode: AtomicU64, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | impl AuthFs { |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 92 | pub fn new(root_entries_by_path: HashMap<PathBuf, AuthFsEntry>) -> AuthFs { |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 93 | let mut next_inode = ROOT_INODE + 1; |
| 94 | let mut inode_table = BTreeMap::new(); |
| 95 | let mut root_entries = HashMap::new(); |
| 96 | |
| 97 | root_entries_by_path.into_iter().for_each(|(path_buf, entry)| { |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 98 | root_entries.insert(path_buf, next_inode); |
| 99 | inode_table.insert(next_inode, entry); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 100 | next_inode += 1; |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 101 | }); |
| 102 | |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 103 | AuthFs { |
| 104 | inode_table: Mutex::new(inode_table), |
| 105 | root_entries, |
| 106 | next_inode: AtomicU64::new(next_inode), |
| 107 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 110 | /// Handles the file associated with `inode` if found. This function returns whatever |
| 111 | /// `handle_fn` returns. |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 112 | fn handle_inode<F, R>(&self, inode: &Inode, handle_fn: F) -> io::Result<R> |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 113 | where |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 114 | F: FnOnce(&AuthFsEntry) -> io::Result<R>, |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 115 | { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 116 | let inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 117 | let entry = |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 118 | inode_table.get(inode).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 119 | handle_fn(entry) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 122 | /// Adds a new entry `name` created by `create_fn` at `parent_inode`. |
| 123 | /// |
| 124 | /// The operation involves two updates: adding the name with a new allocated inode to the |
| 125 | /// parent directory, and insert the new inode and the actual `AuthFsEntry` to the global inode |
| 126 | /// table. |
| 127 | /// |
| 128 | /// `create_fn` receives the parent directory, through which it can create the new entry at and |
| 129 | /// register the new inode to. Its returned entry is then added to the inode table. |
| 130 | fn create_new_entry<F>( |
| 131 | &self, |
| 132 | parent_inode: Inode, |
| 133 | name: &CStr, |
| 134 | create_fn: F, |
| 135 | ) -> io::Result<Inode> |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 136 | where |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 137 | F: FnOnce(&mut AuthFsEntry, &Path, Inode) -> io::Result<AuthFsEntry>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 138 | { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 139 | let mut inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 140 | let mut parent_entry = inode_table |
| 141 | .get_mut(&parent_inode) |
| 142 | .ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?; |
| 143 | |
| 144 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 145 | let basename: &Path = cstr_to_path(name); |
| 146 | let new_file_entry = create_fn(&mut parent_entry, basename, new_inode)?; |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 147 | if let btree_map::Entry::Vacant(entry) = inode_table.entry(new_inode) { |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 148 | entry.insert(new_file_entry); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 149 | Ok(new_inode) |
| 150 | } else { |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 151 | unreachable!("Unexpected duplication of inode {}", new_inode); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 152 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> { |
| 157 | if (flags & libc::O_ACCMODE as u32) == mode as u32 { |
| 158 | Ok(()) |
| 159 | } else { |
| 160 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | cfg_if::cfg_if! { |
| 165 | if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 166 | fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 167 | } else { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 168 | fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 172 | #[allow(clippy::enum_variant_names)] |
| 173 | enum AccessMode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 174 | ReadOnly, |
| 175 | ReadWrite, |
| 176 | } |
| 177 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 178 | fn create_stat( |
| 179 | ino: libc::ino_t, |
| 180 | file_size: u64, |
| 181 | access_mode: AccessMode, |
| 182 | ) -> io::Result<libc::stat64> { |
| 183 | // SAFETY: stat64 is a plan C struct without pointer. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 184 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 185 | |
| 186 | st.st_ino = ino; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 187 | st.st_mode = match access_mode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 188 | // Until needed, let's just grant the owner access. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 189 | // TODO(205169366): Implement mode properly. |
| 190 | AccessMode::ReadOnly => libc::S_IFREG | libc::S_IRUSR, |
| 191 | AccessMode::ReadWrite => libc::S_IFREG | libc::S_IRUSR | libc::S_IWUSR, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 192 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 193 | st.st_nlink = 1; |
| 194 | st.st_uid = 0; |
| 195 | st.st_gid = 0; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 196 | st.st_size = libc::off64_t::try_from(file_size) |
| 197 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 198 | st.st_blksize = blk_size(); |
| 199 | // Per man stat(2), st_blocks is "Number of 512B blocks allocated". |
| 200 | st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512)) |
| 201 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 202 | Ok(st) |
| 203 | } |
| 204 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 205 | fn create_dir_stat(ino: libc::ino_t, file_number: u16) -> io::Result<libc::stat64> { |
| 206 | // SAFETY: stat64 is a plan C struct without pointer. |
| 207 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 208 | |
| 209 | st.st_ino = ino; |
| 210 | // TODO(205169366): Implement mode properly. |
| 211 | st.st_mode = libc::S_IFDIR |
| 212 | | libc::S_IXUSR |
| 213 | | libc::S_IWUSR |
| 214 | | libc::S_IRUSR |
| 215 | | libc::S_IXGRP |
| 216 | | libc::S_IXOTH; |
| 217 | |
| 218 | // 2 extra for . and .. |
| 219 | st.st_nlink = file_number |
| 220 | .checked_add(2) |
| 221 | .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))? |
| 222 | .into(); |
| 223 | |
| 224 | st.st_uid = 0; |
| 225 | st.st_gid = 0; |
| 226 | Ok(st) |
| 227 | } |
| 228 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 229 | fn offset_to_chunk_index(offset: u64) -> u64 { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 230 | offset / CHUNK_SIZE |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 233 | fn read_chunks<W: io::Write, T: ReadByChunk>( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 234 | mut w: W, |
| 235 | file: &T, |
| 236 | file_size: u64, |
| 237 | offset: u64, |
| 238 | size: u32, |
| 239 | ) -> io::Result<usize> { |
| 240 | let remaining = file_size.saturating_sub(offset); |
| 241 | let size_to_read = std::cmp::min(size as usize, remaining as usize); |
Victor Hsieh | ac4f3f4 | 2021-02-26 12:35:58 -0800 | [diff] [blame] | 242 | let total = ChunkedSizeIter::new(size_to_read, offset, CHUNK_SIZE as usize).try_fold( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 243 | 0, |
| 244 | |total, (current_offset, planned_data_size)| { |
| 245 | // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example, |
| 246 | // instead of accepting a buffer, the writer could expose the final destination buffer |
| 247 | // for the reader to write to. It might not be generally applicable though, e.g. with |
| 248 | // virtio transport, the buffer may not be continuous. |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 249 | let mut buf = [0u8; CHUNK_SIZE as usize]; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 250 | let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?; |
| 251 | if read_size < planned_data_size { |
| 252 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 253 | } |
| 254 | |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 255 | let begin = (current_offset % CHUNK_SIZE) as usize; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 256 | let end = begin + planned_data_size; |
| 257 | let s = w.write(&buf[begin..end])?; |
| 258 | if s != planned_data_size { |
| 259 | return Err(io::Error::from_raw_os_error(libc::EIO)); |
| 260 | } |
| 261 | Ok(total + s) |
| 262 | }, |
| 263 | )?; |
| 264 | |
| 265 | Ok(total) |
| 266 | } |
| 267 | |
| 268 | // No need to support enumerating directory entries. |
| 269 | struct EmptyDirectoryIterator {} |
| 270 | |
| 271 | impl DirectoryIterator for EmptyDirectoryIterator { |
| 272 | fn next(&mut self) -> Option<DirEntry> { |
| 273 | None |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | impl FileSystem for AuthFs { |
| 278 | type Inode = Inode; |
| 279 | type Handle = Handle; |
| 280 | type DirIter = EmptyDirectoryIterator; |
| 281 | |
| 282 | fn max_buffer_size(&self) -> u32 { |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 283 | MAX_WRITE_BYTES |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 286 | fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> { |
| 287 | // Enable writeback cache for better performance especially since our bandwidth to the |
| 288 | // backend service is limited. |
| 289 | Ok(FsOptions::WRITEBACK_CACHE) |
| 290 | } |
| 291 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 292 | fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 293 | if parent == ROOT_INODE { |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 294 | let inode = *self |
| 295 | .root_entries |
| 296 | .get(cstr_to_path(name)) |
| 297 | .ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 298 | // Normally, `lookup` is required to increase a reference count for the inode (while |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 299 | // `forget` will decrease it). It is not yet necessary until we start to support |
| 300 | // deletion (only for `VerifiedNewDirectory`). |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 301 | let st = self.handle_inode(&inode, |config| match config { |
| 302 | AuthFsEntry::UnverifiedReadonly { file_size, .. } |
| 303 | | AuthFsEntry::VerifiedReadonly { file_size, .. } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 304 | create_stat(inode, *file_size, AccessMode::ReadOnly) |
| 305 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 306 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 307 | create_stat(inode, editor.size(), AccessMode::ReadWrite) |
| 308 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 309 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 310 | create_dir_stat(inode, dir.number_of_entries()) |
| 311 | } |
| 312 | })?; |
| 313 | Ok(Entry { |
| 314 | inode, |
| 315 | generation: 0, |
| 316 | attr: st, |
| 317 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 318 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 319 | }) |
| 320 | } else { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 321 | let inode = self.handle_inode(&parent, |config| match config { |
| 322 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 323 | let path: &Path = cstr_to_path(name); |
| 324 | dir.find_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT)) |
| 325 | } |
| 326 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 327 | })?; |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 328 | let st = self.handle_inode(&inode, |config| match config { |
| 329 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 330 | create_stat(inode, editor.size(), AccessMode::ReadWrite) |
| 331 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 332 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 333 | create_dir_stat(inode, dir.number_of_entries()) |
| 334 | } |
| 335 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 336 | })?; |
| 337 | Ok(Entry { |
| 338 | inode, |
| 339 | generation: 0, |
| 340 | attr: st, |
| 341 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 342 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 343 | }) |
| 344 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | fn getattr( |
| 348 | &self, |
| 349 | _ctx: Context, |
| 350 | inode: Inode, |
| 351 | _handle: Option<Handle>, |
| 352 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 353 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 354 | Ok(( |
| 355 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 356 | AuthFsEntry::UnverifiedReadonly { file_size, .. } |
| 357 | | AuthFsEntry::VerifiedReadonly { file_size, .. } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 358 | create_stat(inode, *file_size, AccessMode::ReadOnly)? |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 359 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 360 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 361 | create_stat(inode, editor.size(), AccessMode::ReadWrite)? |
| 362 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 363 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 364 | create_dir_stat(inode, dir.number_of_entries())? |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 365 | } |
| 366 | }, |
| 367 | DEFAULT_METADATA_TIMEOUT, |
| 368 | )) |
| 369 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | fn open( |
| 373 | &self, |
| 374 | _ctx: Context, |
| 375 | inode: Self::Inode, |
| 376 | flags: u32, |
| 377 | ) -> io::Result<(Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 378 | // Since file handle is not really used in later operations (which use Inode directly), |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 379 | // return None as the handle. |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 380 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 381 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 382 | AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 383 | check_access_mode(flags, libc::O_RDONLY)?; |
| 384 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 385 | AuthFsEntry::VerifiedNew { .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 386 | // No need to check access modes since all the modes are allowed to the |
| 387 | // read-writable file. |
| 388 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 389 | AuthFsEntry::VerifiedNewDirectory { .. } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 390 | // TODO(victorhsieh): implement when needed. |
| 391 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 392 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 393 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 394 | // Always cache the file content. There is currently no need to support direct I/O or |
| 395 | // avoid the cache buffer. Memory mapping is only possible with cache enabled. |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 396 | Ok((None, fuse::sys::OpenOptions::KEEP_CACHE)) |
| 397 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 400 | fn create( |
| 401 | &self, |
| 402 | _ctx: Context, |
| 403 | parent: Self::Inode, |
| 404 | name: &CStr, |
| 405 | _mode: u32, |
| 406 | _flags: u32, |
| 407 | _umask: u32, |
| 408 | ) -> io::Result<(Entry, Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 409 | // TODO(205169366): Implement mode properly. |
| 410 | // TODO(205172873): handle O_TRUNC and O_EXCL properly. |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 411 | let new_inode = |
| 412 | self.create_new_entry(parent, name, |parent_entry, basename, new_inode| { |
| 413 | match parent_entry { |
| 414 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
| 415 | if dir.find_inode(basename).is_some() { |
| 416 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
| 417 | } |
| 418 | let new_file = dir.create_file(basename, new_inode)?; |
| 419 | Ok(AuthFsEntry::VerifiedNew { editor: new_file }) |
| 420 | } |
| 421 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 422 | } |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 423 | })?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 424 | |
| 425 | Ok(( |
| 426 | Entry { |
| 427 | inode: new_inode, |
| 428 | generation: 0, |
| 429 | attr: create_stat(new_inode, /* file_size */ 0, AccessMode::ReadWrite)?, |
| 430 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 431 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 432 | }, |
| 433 | // See also `open`. |
| 434 | /* handle */ None, |
| 435 | fuse::sys::OpenOptions::KEEP_CACHE, |
| 436 | )) |
| 437 | } |
| 438 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 439 | fn read<W: io::Write + ZeroCopyWriter>( |
| 440 | &self, |
| 441 | _ctx: Context, |
| 442 | inode: Inode, |
| 443 | _handle: Handle, |
| 444 | w: W, |
| 445 | size: u32, |
| 446 | offset: u64, |
| 447 | _lock_owner: Option<u64>, |
| 448 | _flags: u32, |
| 449 | ) -> io::Result<usize> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 450 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 451 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 452 | AuthFsEntry::VerifiedReadonly { reader, file_size } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 453 | read_chunks(w, reader, *file_size, offset, size) |
| 454 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 455 | AuthFsEntry::UnverifiedReadonly { reader, file_size } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 456 | read_chunks(w, reader, *file_size, offset, size) |
| 457 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 458 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 459 | // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to |
| 460 | // request a read even if the file is open with O_WRONLY. |
| 461 | read_chunks(w, editor, editor.size(), offset, size) |
| 462 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 463 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 464 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 465 | }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | fn write<R: io::Read + ZeroCopyReader>( |
| 469 | &self, |
| 470 | _ctx: Context, |
| 471 | inode: Self::Inode, |
| 472 | _handle: Self::Handle, |
| 473 | mut r: R, |
| 474 | size: u32, |
| 475 | offset: u64, |
| 476 | _lock_owner: Option<u64>, |
| 477 | _delayed_write: bool, |
| 478 | _flags: u32, |
| 479 | ) -> io::Result<usize> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 480 | self.handle_inode(&inode, |config| match config { |
| 481 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 482 | let mut buf = vec![0; size as usize]; |
| 483 | r.read_exact(&mut buf)?; |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 484 | editor.write_at(&buf, offset) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 485 | } |
| 486 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 487 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 488 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 489 | |
| 490 | fn setattr( |
| 491 | &self, |
| 492 | _ctx: Context, |
| 493 | inode: Inode, |
| 494 | attr: libc::stat64, |
| 495 | _handle: Option<Handle>, |
| 496 | valid: SetattrValid, |
| 497 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 498 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 499 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 500 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 501 | // Initialize the default stat. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 502 | let mut new_attr = create_stat(inode, editor.size(), AccessMode::ReadWrite)?; |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 503 | // `valid` indicates what fields in `attr` are valid. Update to return correctly. |
| 504 | if valid.contains(SetattrValid::SIZE) { |
| 505 | // st_size is i64, but the cast should be safe since kernel should not give a |
| 506 | // negative size. |
| 507 | debug_assert!(attr.st_size >= 0); |
| 508 | new_attr.st_size = attr.st_size; |
| 509 | editor.resize(attr.st_size as u64)?; |
| 510 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 511 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 512 | if valid.contains(SetattrValid::MODE) { |
| 513 | warn!("Changing st_mode is not currently supported"); |
| 514 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 515 | } |
| 516 | if valid.contains(SetattrValid::UID) { |
| 517 | warn!("Changing st_uid is not currently supported"); |
| 518 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 519 | } |
| 520 | if valid.contains(SetattrValid::GID) { |
| 521 | warn!("Changing st_gid is not currently supported"); |
| 522 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 523 | } |
| 524 | if valid.contains(SetattrValid::CTIME) { |
| 525 | debug!( |
| 526 | "Ignoring ctime change as authfs does not maintain timestamp currently" |
| 527 | ); |
| 528 | } |
| 529 | if valid.intersects(SetattrValid::ATIME | SetattrValid::ATIME_NOW) { |
| 530 | debug!( |
| 531 | "Ignoring atime change as authfs does not maintain timestamp currently" |
| 532 | ); |
| 533 | } |
| 534 | if valid.intersects(SetattrValid::MTIME | SetattrValid::MTIME_NOW) { |
| 535 | debug!( |
| 536 | "Ignoring mtime change as authfs does not maintain timestamp currently" |
| 537 | ); |
| 538 | } |
| 539 | Ok((new_attr, DEFAULT_METADATA_TIMEOUT)) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 540 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 541 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 542 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 543 | }) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 544 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 545 | |
| 546 | fn getxattr( |
| 547 | &self, |
| 548 | _ctx: Context, |
| 549 | inode: Self::Inode, |
| 550 | name: &CStr, |
| 551 | size: u32, |
| 552 | ) -> io::Result<GetxattrReply> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 553 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 554 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 555 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 556 | // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel |
| 557 | // change (see b/196635431). Until it's possible, use xattr to expose what we need |
| 558 | // as an authfs specific API. |
| 559 | if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() { |
| 560 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 561 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 562 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 563 | if size == 0 { |
| 564 | // Per protocol, when size is 0, return the value size. |
| 565 | Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32)) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 566 | } else { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 567 | let digest = editor.calculate_fsverity_digest()?; |
| 568 | if digest.len() > size as usize { |
| 569 | Err(io::Error::from_raw_os_error(libc::ERANGE)) |
| 570 | } else { |
| 571 | Ok(GetxattrReply::Value(digest.to_vec())) |
| 572 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 575 | _ => Err(io::Error::from_raw_os_error(libc::ENODATA)), |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 576 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 577 | }) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 578 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 579 | |
| 580 | fn mkdir( |
| 581 | &self, |
| 582 | _ctx: Context, |
| 583 | parent: Self::Inode, |
| 584 | name: &CStr, |
| 585 | _mode: u32, |
| 586 | _umask: u32, |
| 587 | ) -> io::Result<Entry> { |
| 588 | // TODO(205169366): Implement mode properly. |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 589 | let new_inode = |
| 590 | self.create_new_entry(parent, name, |parent_entry, basename, new_inode| { |
| 591 | match parent_entry { |
| 592 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
| 593 | if dir.find_inode(basename).is_some() { |
| 594 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
| 595 | } |
| 596 | let new_dir = dir.mkdir(basename, new_inode)?; |
| 597 | Ok(AuthFsEntry::VerifiedNewDirectory { dir: new_dir }) |
| 598 | } |
| 599 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 600 | } |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 601 | })?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 602 | |
| 603 | Ok(Entry { |
| 604 | inode: new_inode, |
| 605 | generation: 0, |
| 606 | attr: create_dir_stat(new_inode, /* file_number */ 0)?, |
| 607 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 608 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 609 | }) |
| 610 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /// Mount and start the FUSE instance. This requires CAP_SYS_ADMIN. |
| 614 | pub fn loop_forever( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 615 | root_entries: HashMap<PathBuf, AuthFsEntry>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 616 | mountpoint: &Path, |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 617 | extra_options: &Option<String>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 618 | ) -> Result<(), fuse::Error> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 619 | let dev_fuse = OpenOptions::new() |
| 620 | .read(true) |
| 621 | .write(true) |
| 622 | .open("/dev/fuse") |
| 623 | .expect("Failed to open /dev/fuse"); |
| 624 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 625 | let mut mount_options = vec![ |
| 626 | MountOption::FD(dev_fuse.as_raw_fd()), |
| 627 | MountOption::RootMode(libc::S_IFDIR | libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH), |
| 628 | MountOption::AllowOther, |
| 629 | MountOption::UserId(0), |
| 630 | MountOption::GroupId(0), |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 631 | MountOption::MaxRead(MAX_READ_BYTES), |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 632 | ]; |
| 633 | if let Some(value) = extra_options { |
| 634 | mount_options.push(MountOption::Extra(value)); |
| 635 | } |
| 636 | |
| 637 | fuse::mount(mountpoint, "authfs", libc::MS_NOSUID | libc::MS_NODEV, &mount_options) |
| 638 | .expect("Failed to mount fuse"); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 639 | |
| 640 | fuse::worker::start_message_loop( |
| 641 | dev_fuse, |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 642 | MAX_WRITE_BYTES, |
| 643 | MAX_READ_BYTES, |
| 644 | AuthFs::new(root_entries), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 645 | ) |
| 646 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 647 | |
| 648 | fn cstr_to_path(cstr: &CStr) -> &Path { |
| 649 | OsStr::from_bytes(cstr.to_bytes()).as_ref() |
| 650 | } |