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 | |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 17 | mod mount; |
| 18 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 19 | use anyhow::{anyhow, bail, Result}; |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 20 | use fuse::filesystem::{ |
| 21 | Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply, |
| 22 | SetattrValid, ZeroCopyReader, ZeroCopyWriter, |
| 23 | }; |
| 24 | use fuse::sys::OpenOptions as FuseOpenOptions; |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 25 | use log::{debug, error, warn}; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 26 | use std::collections::{btree_map, BTreeMap}; |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 27 | use std::convert::{TryFrom, TryInto}; |
| 28 | use std::ffi::{CStr, CString, OsStr}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 29 | use std::io; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 30 | use std::mem::{zeroed, MaybeUninit}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 31 | use std::option::Option; |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 32 | use std::os::unix::ffi::OsStrExt; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 33 | use std::path::{Component, Path, PathBuf}; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 34 | use std::sync::atomic::{AtomicU64, Ordering}; |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 35 | use std::sync::{Arc, Mutex}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 36 | use std::time::Duration; |
| 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 | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 40 | validate_basename, Attr, EagerChunkReader, InMemoryDir, RandomWrite, ReadByChunk, |
| 41 | RemoteDirEditor, RemoteFileEditor, RemoteFileReader, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 42 | }; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 43 | use crate::fsstat::RemoteFsStatsReader; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 44 | use crate::fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 45 | |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 46 | pub use self::mount::mount_and_enter_message_loop; |
| 47 | use self::mount::MAX_WRITE_BYTES; |
| 48 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 49 | pub type Inode = u64; |
| 50 | type Handle = u64; |
| 51 | |
Victor Hsieh | 259dd9c | 2022-02-09 20:31:57 +0000 | [diff] [blame^] | 52 | /// Maximum time for a file's metadata to be cached by the kernel. Since any file and directory |
| 53 | /// changes (if not read-only) has to go through AuthFS to be trusted, the timeout can be maximum. |
| 54 | const DEFAULT_METADATA_TIMEOUT: Duration = Duration::MAX; |
| 55 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 56 | const ROOT_INODE: Inode = 1; |
| 57 | |
| 58 | /// `AuthFsEntry` defines the filesystem entry type supported by AuthFS. |
| 59 | pub enum AuthFsEntry { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 60 | /// A read-only directory (writable during initialization). Root directory is an example. |
| 61 | ReadonlyDirectory { dir: InMemoryDir }, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 62 | /// 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] | 63 | /// served from a remote server. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 64 | VerifiedReadonly { |
Victor Hsieh | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 65 | reader: VerifiedFileReader<RemoteFileReader, EagerChunkReader>, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 66 | file_size: u64, |
| 67 | }, |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 68 | /// A file type that is a read-only passthrough from a file on a remote server. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 69 | UnverifiedReadonly { reader: RemoteFileReader, file_size: u64 }, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 70 | /// A file type that is initially empty, and the content is stored on a remote server. File |
| 71 | /// integrity is guaranteed with private Merkle tree. |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 72 | VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor>, attr: Attr }, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 73 | /// A directory type that is initially empty. One can create new file (`VerifiedNew`) and new |
| 74 | /// directory (`VerifiedNewDirectory` itself) with integrity guaranteed within the VM. |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 75 | VerifiedNewDirectory { dir: RemoteDirEditor, attr: Attr }, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 78 | impl AuthFsEntry { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 79 | fn expect_empty_deletable_directory(&self) -> io::Result<()> { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 80 | match self { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 81 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 82 | if dir.number_of_entries() == 0 { |
| 83 | Ok(()) |
| 84 | } else { |
| 85 | Err(io::Error::from_raw_os_error(libc::ENOTEMPTY)) |
| 86 | } |
| 87 | } |
| 88 | AuthFsEntry::ReadonlyDirectory { .. } => { |
| 89 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 90 | } |
| 91 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 96 | struct InodeState { |
| 97 | /// Actual inode entry. |
| 98 | entry: AuthFsEntry, |
| 99 | |
| 100 | /// Number of `Handle`s (i.e. file descriptors) that are currently referring to the this inode. |
| 101 | /// |
| 102 | /// Technically, this does not matter to readonly entries, since they live forever. The |
| 103 | /// reference count is only needed for manageing lifetime of writable entries like `VerifiedNew` |
| 104 | /// and `VerifiedNewDirectory`. That is, when an entry is deleted, the actual entry needs to |
| 105 | /// stay alive until the reference count reaches zero. |
| 106 | /// |
| 107 | /// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement. |
| 108 | handle_ref_count: u64, |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 109 | |
| 110 | /// Whether the inode is already unlinked, i.e. should be removed, once `handle_ref_count` is |
| 111 | /// down to zero. |
| 112 | unlinked: bool, |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | impl InodeState { |
| 116 | fn new(entry: AuthFsEntry) -> Self { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 117 | InodeState { entry, handle_ref_count: 0, unlinked: false } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 121 | InodeState { entry, handle_ref_count, unlinked: false } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 125 | /// Data type that a directory implementation should be able to present its entry to `AuthFs`. |
| 126 | #[derive(Clone)] |
| 127 | pub struct AuthFsDirEntry { |
| 128 | pub inode: Inode, |
| 129 | pub name: CString, |
| 130 | pub is_dir: bool, |
| 131 | } |
| 132 | |
| 133 | /// A snapshot of a directory entries for supporting `readdir` operation. |
| 134 | /// |
| 135 | /// The `readdir` implementation is required by FUSE to not return any entries that have been |
| 136 | /// returned previously (while it's fine to not return new entries). Snapshot is the easiest way to |
| 137 | /// be compliant. See `fuse::filesystem::readdir` for more details. |
| 138 | /// |
| 139 | /// A `DirEntriesSnapshot` is created on `opendir`, and is associated with the returned |
| 140 | /// `Handle`/FD. The snapshot is deleted when the handle is released in `releasedir`. |
| 141 | type DirEntriesSnapshot = Vec<AuthFsDirEntry>; |
| 142 | |
| 143 | /// An iterator for reading from `DirEntriesSnapshot`. |
| 144 | pub struct DirEntriesSnapshotIterator { |
| 145 | /// A reference to the `DirEntriesSnapshot` in `AuthFs`. |
| 146 | snapshot: Arc<DirEntriesSnapshot>, |
| 147 | |
| 148 | /// A value determined by `Self` to identify the last entry. 0 is a reserved value by FUSE to |
| 149 | /// mean reading from the beginning. |
| 150 | prev_offset: usize, |
| 151 | } |
| 152 | |
| 153 | impl<'a> DirectoryIterator for DirEntriesSnapshotIterator { |
| 154 | fn next(&mut self) -> Option<DirEntry> { |
| 155 | // This iterator should not be the only reference to the snapshot. The snapshot should |
| 156 | // still be hold in `dir_handle_table`, i.e. when the FD is not yet closed. |
| 157 | // |
| 158 | // This code is unreachable when `readdir` is called with a closed FD. Only when the FD is |
| 159 | // not yet closed, `DirEntriesSnapshotIterator` can be created (but still short-lived |
| 160 | // during `readdir`). |
| 161 | debug_assert!(Arc::strong_count(&self.snapshot) >= 2); |
| 162 | |
| 163 | // Since 0 is reserved, let's use 1-based index for the offset. This allows us to |
| 164 | // resume from the previous read in the snapshot easily. |
| 165 | let current_offset = if self.prev_offset == 0 { |
| 166 | 1 // first element in the vector |
| 167 | } else { |
| 168 | self.prev_offset + 1 // next element in the vector |
| 169 | }; |
| 170 | if current_offset > self.snapshot.len() { |
| 171 | None |
| 172 | } else { |
| 173 | let AuthFsDirEntry { inode, name, is_dir } = &self.snapshot[current_offset - 1]; |
| 174 | let entry = DirEntry { |
| 175 | offset: current_offset as u64, |
| 176 | ino: *inode, |
| 177 | name, |
| 178 | type_: if *is_dir { libc::DT_DIR.into() } else { libc::DT_REG.into() }, |
| 179 | }; |
| 180 | self.prev_offset = current_offset; |
| 181 | Some(entry) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | type DirHandleTable = BTreeMap<Handle, Arc<DirEntriesSnapshot>>; |
| 187 | |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 188 | // AuthFS needs to be `Sync` to be accepted by fuse::worker::start_message_loop as a `FileSystem`. |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 189 | pub struct AuthFs { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 190 | /// Table for `Inode` to `InodeState` lookup. This needs to be `Sync` to be used in |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 191 | /// `fuse::worker::start_message_loop`. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 192 | inode_table: Mutex<BTreeMap<Inode, InodeState>>, |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 193 | |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 194 | /// The next available inode number. |
| 195 | next_inode: AtomicU64, |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 196 | |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 197 | /// Table for `Handle` to `Arc<DirEntriesSnapshot>` lookup. On `opendir`, a new directory handle |
| 198 | /// is created and the snapshot of the current directory is created. This is not super |
| 199 | /// efficient, but is the simplest way to be compliant to the FUSE contract (see |
| 200 | /// `fuse::filesystem::readdir`). |
| 201 | /// |
| 202 | /// Currently, no code locks `dir_handle_table` and `inode_table` at the same time to avoid |
| 203 | /// deadlock. |
| 204 | dir_handle_table: Mutex<DirHandleTable>, |
| 205 | |
| 206 | /// The next available handle number. |
| 207 | next_handle: AtomicU64, |
| 208 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 209 | /// A reader to access the remote filesystem stats, which is supposed to be of "the" output |
| 210 | /// directory. We assume all output are stored in the same partition. |
| 211 | remote_fs_stats_reader: RemoteFsStatsReader, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 214 | // Implementation for preparing an `AuthFs` instance, before starting to serve. |
| 215 | // TODO(victorhsieh): Consider implement a builder to separate the mutable initialization from the |
| 216 | // immutable / interiorly mutable serving phase. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 217 | impl AuthFs { |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 218 | pub fn new(remote_fs_stats_reader: RemoteFsStatsReader) -> AuthFs { |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 219 | let mut inode_table = BTreeMap::new(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 220 | inode_table.insert( |
| 221 | ROOT_INODE, |
| 222 | InodeState::new(AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }), |
| 223 | ); |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 224 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 225 | AuthFs { |
| 226 | inode_table: Mutex::new(inode_table), |
| 227 | next_inode: AtomicU64::new(ROOT_INODE + 1), |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 228 | dir_handle_table: Mutex::new(BTreeMap::new()), |
| 229 | next_handle: AtomicU64::new(1), |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 230 | remote_fs_stats_reader, |
| 231 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 234 | /// Add an `AuthFsEntry` as `basename` to the filesystem root. |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 235 | pub fn add_entry_at_root_dir( |
| 236 | &mut self, |
| 237 | basename: PathBuf, |
| 238 | entry: AuthFsEntry, |
| 239 | ) -> Result<Inode> { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 240 | validate_basename(&basename)?; |
| 241 | self.add_entry_at_ro_dir_by_path(ROOT_INODE, &basename, entry) |
| 242 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 243 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 244 | /// Add an `AuthFsEntry` by path from the `ReadonlyDirectory` represented by `dir_inode`. The |
| 245 | /// path must be a related path. If some ancestor directories do not exist, they will be |
| 246 | /// created (also as `ReadonlyDirectory`) automatically. |
| 247 | pub fn add_entry_at_ro_dir_by_path( |
| 248 | &mut self, |
| 249 | dir_inode: Inode, |
| 250 | path: &Path, |
| 251 | entry: AuthFsEntry, |
| 252 | ) -> Result<Inode> { |
| 253 | // 1. Make sure the parent directories all exist. Derive the entry's parent inode. |
| 254 | let parent_path = |
| 255 | path.parent().ok_or_else(|| anyhow!("No parent directory: {:?}", path))?; |
| 256 | let parent_inode = |
| 257 | parent_path.components().try_fold(dir_inode, |current_dir_inode, path_component| { |
| 258 | match path_component { |
| 259 | Component::RootDir => bail!("Absolute path is not supported"), |
| 260 | Component::Normal(name) => { |
| 261 | let inode_table = self.inode_table.get_mut().unwrap(); |
| 262 | // Locate the internal directory structure. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 263 | let current_dir_entry = &mut inode_table |
| 264 | .get_mut(¤t_dir_inode) |
| 265 | .ok_or_else(|| { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 266 | anyhow!("Unknown directory inode {}", current_dir_inode) |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 267 | })? |
| 268 | .entry; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 269 | let dir = match current_dir_entry { |
| 270 | AuthFsEntry::ReadonlyDirectory { dir } => dir, |
| 271 | _ => unreachable!("Not a ReadonlyDirectory"), |
| 272 | }; |
| 273 | // Return directory inode. Create first if not exists. |
| 274 | if let Some(existing_inode) = dir.lookup_inode(name.as_ref()) { |
| 275 | Ok(existing_inode) |
| 276 | } else { |
| 277 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 278 | let new_dir_entry = |
| 279 | AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }; |
| 280 | |
| 281 | // Actually update the tables. |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 282 | dir.add_dir(name.as_ref(), new_inode)?; |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 283 | if inode_table |
| 284 | .insert(new_inode, InodeState::new(new_dir_entry)) |
| 285 | .is_some() |
| 286 | { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 287 | bail!("Unexpected to find a duplicated inode"); |
| 288 | } |
| 289 | Ok(new_inode) |
| 290 | } |
| 291 | } |
| 292 | _ => Err(anyhow!("Path is not canonical: {:?}", path)), |
| 293 | } |
| 294 | })?; |
| 295 | |
| 296 | // 2. Insert the entry to the parent directory, as well as the inode table. |
| 297 | let inode_table = self.inode_table.get_mut().unwrap(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 298 | let inode_state = inode_table.get_mut(&parent_inode).expect("previously returned inode"); |
| 299 | match &mut inode_state.entry { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 300 | AuthFsEntry::ReadonlyDirectory { dir } => { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 301 | let basename = |
| 302 | path.file_name().ok_or_else(|| anyhow!("Bad file name: {:?}", path))?; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 303 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 304 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 305 | // Actually update the tables. |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 306 | dir.add_file(basename.as_ref(), new_inode)?; |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 307 | if inode_table.insert(new_inode, InodeState::new(entry)).is_some() { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 308 | bail!("Unexpected to find a duplicated inode"); |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 309 | } |
| 310 | Ok(new_inode) |
| 311 | } |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 312 | _ => unreachable!("Not a ReadonlyDirectory"), |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Implementation for serving requests. |
| 318 | impl AuthFs { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 319 | /// Handles the file associated with `inode` if found. This function returns whatever |
| 320 | /// `handle_fn` returns. |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 321 | 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] | 322 | where |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 323 | F: FnOnce(&AuthFsEntry) -> io::Result<R>, |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 324 | { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 325 | let inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 326 | handle_inode_locked(&inode_table, inode, |inode_state| handle_fn(&inode_state.entry)) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 329 | /// Adds a new entry `name` created by `create_fn` at `parent_inode`, with an initial ref count |
| 330 | /// of one. |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 331 | /// |
| 332 | /// The operation involves two updates: adding the name with a new allocated inode to the |
| 333 | /// parent directory, and insert the new inode and the actual `AuthFsEntry` to the global inode |
| 334 | /// table. |
| 335 | /// |
| 336 | /// `create_fn` receives the parent directory, through which it can create the new entry at and |
| 337 | /// register the new inode to. Its returned entry is then added to the inode table. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 338 | fn create_new_entry_with_ref_count<F>( |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 339 | &self, |
| 340 | parent_inode: Inode, |
| 341 | name: &CStr, |
| 342 | create_fn: F, |
| 343 | ) -> io::Result<Inode> |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 344 | where |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 345 | F: FnOnce(&mut AuthFsEntry, &Path, Inode) -> io::Result<AuthFsEntry>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 346 | { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 347 | let mut inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 348 | let (new_inode, new_file_entry) = handle_inode_mut_locked( |
| 349 | &mut inode_table, |
| 350 | &parent_inode, |
| 351 | |InodeState { entry, .. }| { |
| 352 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 353 | let basename: &Path = cstr_to_path(name); |
| 354 | let new_file_entry = create_fn(entry, basename, new_inode)?; |
| 355 | Ok((new_inode, new_file_entry)) |
| 356 | }, |
| 357 | )?; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 358 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 359 | if let btree_map::Entry::Vacant(entry) = inode_table.entry(new_inode) { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 360 | entry.insert(InodeState::new_with_ref_count(new_file_entry, 1)); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 361 | Ok(new_inode) |
| 362 | } else { |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 363 | unreachable!("Unexpected duplication of inode {}", new_inode); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 364 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 365 | } |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 366 | |
| 367 | fn open_dir_store_snapshot( |
| 368 | &self, |
| 369 | dir_entries: Vec<AuthFsDirEntry>, |
| 370 | ) -> io::Result<(Option<Handle>, FuseOpenOptions)> { |
| 371 | let handle = self.next_handle.fetch_add(1, Ordering::Relaxed); |
| 372 | let mut dir_handle_table = self.dir_handle_table.lock().unwrap(); |
| 373 | if let btree_map::Entry::Vacant(value) = dir_handle_table.entry(handle) { |
| 374 | value.insert(Arc::new(dir_entries)); |
| 375 | Ok((Some(handle), FuseOpenOptions::empty())) |
| 376 | } else { |
| 377 | unreachable!("Unexpected to see new handle {} to existing in the table", handle); |
| 378 | } |
| 379 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> { |
| 383 | if (flags & libc::O_ACCMODE as u32) == mode as u32 { |
| 384 | Ok(()) |
| 385 | } else { |
| 386 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | cfg_if::cfg_if! { |
| 391 | if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 392 | fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 393 | } else { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 394 | fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 398 | #[allow(clippy::enum_variant_names)] |
| 399 | enum AccessMode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 400 | ReadOnly, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 401 | Variable(u32), |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 404 | fn create_stat( |
| 405 | ino: libc::ino_t, |
| 406 | file_size: u64, |
| 407 | access_mode: AccessMode, |
| 408 | ) -> io::Result<libc::stat64> { |
| 409 | // SAFETY: stat64 is a plan C struct without pointer. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 410 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 411 | |
| 412 | st.st_ino = ino; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 413 | st.st_mode = match access_mode { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 414 | AccessMode::ReadOnly => { |
| 415 | // Until needed, let's just grant the owner access. |
| 416 | libc::S_IFREG | libc::S_IRUSR |
| 417 | } |
| 418 | AccessMode::Variable(mode) => libc::S_IFREG | mode, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 419 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 420 | st.st_nlink = 1; |
| 421 | st.st_uid = 0; |
| 422 | st.st_gid = 0; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 423 | st.st_size = libc::off64_t::try_from(file_size) |
| 424 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 425 | st.st_blksize = blk_size(); |
| 426 | // Per man stat(2), st_blocks is "Number of 512B blocks allocated". |
| 427 | st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512)) |
| 428 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 429 | Ok(st) |
| 430 | } |
| 431 | |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 432 | fn create_dir_stat( |
| 433 | ino: libc::ino_t, |
| 434 | file_number: u16, |
| 435 | access_mode: AccessMode, |
| 436 | ) -> io::Result<libc::stat64> { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 437 | // SAFETY: stat64 is a plan C struct without pointer. |
| 438 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 439 | |
| 440 | st.st_ino = ino; |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 441 | st.st_mode = match access_mode { |
| 442 | AccessMode::ReadOnly => { |
| 443 | // Until needed, let's just grant the owner access and search to group and others. |
| 444 | libc::S_IFDIR | libc::S_IXUSR | libc::S_IRUSR | libc::S_IXGRP | libc::S_IXOTH |
| 445 | } |
| 446 | AccessMode::Variable(mode) => libc::S_IFDIR | mode, |
| 447 | }; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 448 | |
| 449 | // 2 extra for . and .. |
| 450 | st.st_nlink = file_number |
| 451 | .checked_add(2) |
| 452 | .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))? |
| 453 | .into(); |
| 454 | |
| 455 | st.st_uid = 0; |
| 456 | st.st_gid = 0; |
| 457 | Ok(st) |
| 458 | } |
| 459 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 460 | fn offset_to_chunk_index(offset: u64) -> u64 { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 461 | offset / CHUNK_SIZE |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 464 | fn read_chunks<W: io::Write, T: ReadByChunk>( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 465 | mut w: W, |
| 466 | file: &T, |
| 467 | file_size: u64, |
| 468 | offset: u64, |
| 469 | size: u32, |
| 470 | ) -> io::Result<usize> { |
| 471 | let remaining = file_size.saturating_sub(offset); |
| 472 | 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] | 473 | 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] | 474 | 0, |
| 475 | |total, (current_offset, planned_data_size)| { |
| 476 | // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example, |
| 477 | // instead of accepting a buffer, the writer could expose the final destination buffer |
| 478 | // for the reader to write to. It might not be generally applicable though, e.g. with |
| 479 | // virtio transport, the buffer may not be continuous. |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 480 | let mut buf = [0u8; CHUNK_SIZE as usize]; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 481 | let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?; |
| 482 | if read_size < planned_data_size { |
| 483 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 484 | } |
| 485 | |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 486 | let begin = (current_offset % CHUNK_SIZE) as usize; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 487 | let end = begin + planned_data_size; |
| 488 | let s = w.write(&buf[begin..end])?; |
| 489 | if s != planned_data_size { |
| 490 | return Err(io::Error::from_raw_os_error(libc::EIO)); |
| 491 | } |
| 492 | Ok(total + s) |
| 493 | }, |
| 494 | )?; |
| 495 | |
| 496 | Ok(total) |
| 497 | } |
| 498 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 499 | impl FileSystem for AuthFs { |
| 500 | type Inode = Inode; |
| 501 | type Handle = Handle; |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 502 | type DirIter = DirEntriesSnapshotIterator; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 503 | |
| 504 | fn max_buffer_size(&self) -> u32 { |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 505 | MAX_WRITE_BYTES |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 508 | fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> { |
| 509 | // Enable writeback cache for better performance especially since our bandwidth to the |
| 510 | // backend service is limited. |
| 511 | Ok(FsOptions::WRITEBACK_CACHE) |
| 512 | } |
| 513 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 514 | fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 515 | let mut inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 516 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 517 | // Look up the entry's inode number in parent directory. |
| 518 | let inode = |
| 519 | handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry { |
| 520 | AuthFsEntry::ReadonlyDirectory { dir } => { |
| 521 | let path = cstr_to_path(name); |
| 522 | dir.lookup_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT)) |
| 523 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 524 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 525 | let path = cstr_to_path(name); |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 526 | dir.find_inode(path) |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 527 | } |
| 528 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 529 | })?; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 530 | |
| 531 | // Create the entry's stat if found. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 532 | let st = handle_inode_mut_locked( |
| 533 | &mut inode_table, |
| 534 | &inode, |
| 535 | |InodeState { entry, handle_ref_count, .. }| { |
| 536 | let st = match entry { |
| 537 | AuthFsEntry::ReadonlyDirectory { dir } => { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 538 | create_dir_stat(inode, dir.number_of_entries(), AccessMode::ReadOnly) |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 539 | } |
| 540 | AuthFsEntry::UnverifiedReadonly { file_size, .. } |
| 541 | | AuthFsEntry::VerifiedReadonly { file_size, .. } => { |
| 542 | create_stat(inode, *file_size, AccessMode::ReadOnly) |
| 543 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 544 | AuthFsEntry::VerifiedNew { editor, attr, .. } => { |
| 545 | create_stat(inode, editor.size(), AccessMode::Variable(attr.mode())) |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 546 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 547 | AuthFsEntry::VerifiedNewDirectory { dir, attr } => create_dir_stat( |
| 548 | inode, |
| 549 | dir.number_of_entries(), |
| 550 | AccessMode::Variable(attr.mode()), |
| 551 | ), |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 552 | }?; |
| 553 | *handle_ref_count += 1; |
| 554 | Ok(st) |
| 555 | }, |
| 556 | )?; |
| 557 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 558 | Ok(Entry { |
| 559 | inode, |
| 560 | generation: 0, |
| 561 | attr: st, |
| 562 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 563 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 564 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 567 | fn forget(&self, _ctx: Context, inode: Self::Inode, count: u64) { |
| 568 | let mut inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 569 | let delete_now = handle_inode_mut_locked( |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 570 | &mut inode_table, |
| 571 | &inode, |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 572 | |InodeState { handle_ref_count, unlinked, .. }| { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 573 | if count > *handle_ref_count { |
| 574 | error!( |
| 575 | "Trying to decrease refcount of inode {} by {} (> current {})", |
| 576 | inode, count, *handle_ref_count |
| 577 | ); |
| 578 | panic!(); // log to logcat with error! |
| 579 | } |
| 580 | *handle_ref_count = handle_ref_count.saturating_sub(count); |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 581 | Ok(*unlinked && *handle_ref_count == 0) |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 582 | }, |
| 583 | ); |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 584 | |
| 585 | match delete_now { |
| 586 | Ok(true) => { |
| 587 | let _ = inode_table.remove(&inode).expect("Removed an existing entry"); |
| 588 | } |
| 589 | Ok(false) => { /* Let the inode stay */ } |
| 590 | Err(e) => { |
| 591 | warn!( |
| 592 | "Unexpected failure when tries to forget an inode {} by refcount {}: {:?}", |
| 593 | inode, count, e |
| 594 | ); |
| 595 | } |
| 596 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 599 | fn getattr( |
| 600 | &self, |
| 601 | _ctx: Context, |
| 602 | inode: Inode, |
| 603 | _handle: Option<Handle>, |
| 604 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 605 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 606 | Ok(( |
| 607 | match config { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 608 | AuthFsEntry::ReadonlyDirectory { dir } => { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 609 | create_dir_stat(inode, dir.number_of_entries(), AccessMode::ReadOnly) |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 610 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 611 | AuthFsEntry::UnverifiedReadonly { file_size, .. } |
| 612 | | AuthFsEntry::VerifiedReadonly { file_size, .. } => { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 613 | create_stat(inode, *file_size, AccessMode::ReadOnly) |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 614 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 615 | AuthFsEntry::VerifiedNew { editor, attr, .. } => { |
| 616 | create_stat(inode, editor.size(), AccessMode::Variable(attr.mode())) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 617 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 618 | AuthFsEntry::VerifiedNewDirectory { dir, attr } => create_dir_stat( |
| 619 | inode, |
| 620 | dir.number_of_entries(), |
| 621 | AccessMode::Variable(attr.mode()), |
| 622 | ), |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 623 | }?, |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 624 | DEFAULT_METADATA_TIMEOUT, |
| 625 | )) |
| 626 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | fn open( |
| 630 | &self, |
| 631 | _ctx: Context, |
| 632 | inode: Self::Inode, |
| 633 | flags: u32, |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 634 | ) -> io::Result<(Option<Self::Handle>, FuseOpenOptions)> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 635 | // 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] | 636 | // return None as the handle. |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 637 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 638 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 639 | AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 640 | check_access_mode(flags, libc::O_RDONLY)?; |
| 641 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 642 | AuthFsEntry::VerifiedNew { .. } => { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 643 | // TODO(victorhsieh): Imeplement ACL check using the attr and ctx. Always allow |
| 644 | // for now. |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 645 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 646 | AuthFsEntry::ReadonlyDirectory { .. } |
| 647 | | AuthFsEntry::VerifiedNewDirectory { .. } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 648 | // TODO(victorhsieh): implement when needed. |
| 649 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 650 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 651 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 652 | // Always cache the file content. There is currently no need to support direct I/O or |
| 653 | // avoid the cache buffer. Memory mapping is only possible with cache enabled. |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 654 | Ok((None, FuseOpenOptions::KEEP_CACHE)) |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 655 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 658 | fn create( |
| 659 | &self, |
| 660 | _ctx: Context, |
| 661 | parent: Self::Inode, |
| 662 | name: &CStr, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 663 | mode: u32, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 664 | _flags: u32, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 665 | umask: u32, |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 666 | ) -> io::Result<(Entry, Option<Self::Handle>, FuseOpenOptions)> { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 667 | // TODO(205172873): handle O_TRUNC and O_EXCL properly. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 668 | let new_inode = self.create_new_entry_with_ref_count( |
| 669 | parent, |
| 670 | name, |
| 671 | |parent_entry, basename, new_inode| match parent_entry { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 672 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 673 | if dir.has_entry(basename) { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 674 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 675 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 676 | let mode = mode & !umask; |
| 677 | let (new_file, new_attr) = dir.create_file(basename, new_inode, mode)?; |
| 678 | Ok(AuthFsEntry::VerifiedNew { editor: new_file, attr: new_attr }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 679 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 680 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 681 | }, |
| 682 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 683 | |
| 684 | Ok(( |
| 685 | Entry { |
| 686 | inode: new_inode, |
| 687 | generation: 0, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 688 | attr: create_stat(new_inode, /* file_size */ 0, AccessMode::Variable(mode))?, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 689 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 690 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 691 | }, |
| 692 | // See also `open`. |
| 693 | /* handle */ None, |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 694 | FuseOpenOptions::KEEP_CACHE, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 695 | )) |
| 696 | } |
| 697 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 698 | fn read<W: io::Write + ZeroCopyWriter>( |
| 699 | &self, |
| 700 | _ctx: Context, |
| 701 | inode: Inode, |
| 702 | _handle: Handle, |
| 703 | w: W, |
| 704 | size: u32, |
| 705 | offset: u64, |
| 706 | _lock_owner: Option<u64>, |
| 707 | _flags: u32, |
| 708 | ) -> io::Result<usize> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 709 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 710 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 711 | AuthFsEntry::VerifiedReadonly { reader, file_size } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 712 | read_chunks(w, reader, *file_size, offset, size) |
| 713 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 714 | AuthFsEntry::UnverifiedReadonly { reader, file_size } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 715 | read_chunks(w, reader, *file_size, offset, size) |
| 716 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 717 | AuthFsEntry::VerifiedNew { editor, .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 718 | // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to |
| 719 | // request a read even if the file is open with O_WRONLY. |
| 720 | read_chunks(w, editor, editor.size(), offset, size) |
| 721 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 722 | AuthFsEntry::ReadonlyDirectory { .. } |
| 723 | | AuthFsEntry::VerifiedNewDirectory { .. } => { |
| 724 | Err(io::Error::from_raw_os_error(libc::EISDIR)) |
| 725 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 726 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 727 | }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | fn write<R: io::Read + ZeroCopyReader>( |
| 731 | &self, |
| 732 | _ctx: Context, |
| 733 | inode: Self::Inode, |
| 734 | _handle: Self::Handle, |
| 735 | mut r: R, |
| 736 | size: u32, |
| 737 | offset: u64, |
| 738 | _lock_owner: Option<u64>, |
| 739 | _delayed_write: bool, |
| 740 | _flags: u32, |
| 741 | ) -> io::Result<usize> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 742 | self.handle_inode(&inode, |config| match config { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 743 | AuthFsEntry::VerifiedNew { editor, .. } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 744 | let mut buf = vec![0; size as usize]; |
| 745 | r.read_exact(&mut buf)?; |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 746 | editor.write_at(&buf, offset) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 747 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 748 | AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => { |
| 749 | Err(io::Error::from_raw_os_error(libc::EPERM)) |
| 750 | } |
| 751 | AuthFsEntry::ReadonlyDirectory { .. } | AuthFsEntry::VerifiedNewDirectory { .. } => { |
| 752 | Err(io::Error::from_raw_os_error(libc::EISDIR)) |
| 753 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 754 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 755 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 756 | |
| 757 | fn setattr( |
| 758 | &self, |
| 759 | _ctx: Context, |
| 760 | inode: Inode, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 761 | in_attr: libc::stat64, |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 762 | _handle: Option<Handle>, |
| 763 | valid: SetattrValid, |
| 764 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 765 | let mut inode_table = self.inode_table.lock().unwrap(); |
| 766 | handle_inode_mut_locked(&mut inode_table, &inode, |InodeState { entry, .. }| match entry { |
| 767 | AuthFsEntry::VerifiedNew { editor, attr } => { |
| 768 | check_unsupported_setattr_request(valid)?; |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 769 | |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 770 | // Initialize the default stat. |
| 771 | let mut new_attr = |
| 772 | create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))?; |
| 773 | // `valid` indicates what fields in `attr` are valid. Update to return correctly. |
| 774 | if valid.contains(SetattrValid::SIZE) { |
| 775 | // st_size is i64, but the cast should be safe since kernel should not give a |
| 776 | // negative size. |
| 777 | debug_assert!(in_attr.st_size >= 0); |
| 778 | new_attr.st_size = in_attr.st_size; |
| 779 | editor.resize(in_attr.st_size as u64)?; |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 780 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 781 | if valid.contains(SetattrValid::MODE) { |
| 782 | attr.set_mode(in_attr.st_mode)?; |
| 783 | new_attr.st_mode = in_attr.st_mode; |
| 784 | } |
| 785 | Ok((new_attr, DEFAULT_METADATA_TIMEOUT)) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 786 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 787 | AuthFsEntry::VerifiedNewDirectory { dir, attr } => { |
| 788 | check_unsupported_setattr_request(valid)?; |
| 789 | if valid.contains(SetattrValid::SIZE) { |
| 790 | return Err(io::Error::from_raw_os_error(libc::EISDIR)); |
| 791 | } |
| 792 | |
| 793 | // Initialize the default stat. |
| 794 | let mut new_attr = create_dir_stat( |
| 795 | inode, |
| 796 | dir.number_of_entries(), |
| 797 | AccessMode::Variable(attr.mode()), |
| 798 | )?; |
| 799 | if valid.contains(SetattrValid::MODE) { |
| 800 | attr.set_mode(in_attr.st_mode)?; |
| 801 | new_attr.st_mode = in_attr.st_mode; |
| 802 | } |
| 803 | Ok((new_attr, DEFAULT_METADATA_TIMEOUT)) |
| 804 | } |
| 805 | _ => Err(io::Error::from_raw_os_error(libc::EPERM)), |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 806 | }) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 807 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 808 | |
| 809 | fn getxattr( |
| 810 | &self, |
| 811 | _ctx: Context, |
| 812 | inode: Self::Inode, |
| 813 | name: &CStr, |
| 814 | size: u32, |
| 815 | ) -> io::Result<GetxattrReply> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 816 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 817 | match config { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 818 | AuthFsEntry::VerifiedNew { editor, .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 819 | // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel |
| 820 | // change (see b/196635431). Until it's possible, use xattr to expose what we need |
| 821 | // as an authfs specific API. |
| 822 | if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() { |
| 823 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 824 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 825 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 826 | if size == 0 { |
| 827 | // Per protocol, when size is 0, return the value size. |
| 828 | Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32)) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 829 | } else { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 830 | let digest = editor.calculate_fsverity_digest()?; |
| 831 | if digest.len() > size as usize { |
| 832 | Err(io::Error::from_raw_os_error(libc::ERANGE)) |
| 833 | } else { |
| 834 | Ok(GetxattrReply::Value(digest.to_vec())) |
| 835 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 838 | _ => Err(io::Error::from_raw_os_error(libc::ENODATA)), |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 839 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 840 | }) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 841 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 842 | |
| 843 | fn mkdir( |
| 844 | &self, |
| 845 | _ctx: Context, |
| 846 | parent: Self::Inode, |
| 847 | name: &CStr, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 848 | mode: u32, |
| 849 | umask: u32, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 850 | ) -> io::Result<Entry> { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 851 | let new_inode = self.create_new_entry_with_ref_count( |
| 852 | parent, |
| 853 | name, |
| 854 | |parent_entry, basename, new_inode| match parent_entry { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 855 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 856 | if dir.has_entry(basename) { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 857 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 858 | } |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 859 | let mode = mode & !umask; |
| 860 | let (new_dir, new_attr) = dir.mkdir(basename, new_inode, mode)?; |
| 861 | Ok(AuthFsEntry::VerifiedNewDirectory { dir: new_dir, attr: new_attr }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 862 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 863 | AuthFsEntry::ReadonlyDirectory { .. } => { |
| 864 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 865 | } |
| 866 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 867 | }, |
| 868 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 869 | |
| 870 | Ok(Entry { |
| 871 | inode: new_inode, |
| 872 | generation: 0, |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 873 | attr: create_dir_stat(new_inode, /* file_number */ 0, AccessMode::Variable(mode))?, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 874 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 875 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 876 | }) |
| 877 | } |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 878 | |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 879 | fn unlink(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> { |
| 880 | let mut inode_table = self.inode_table.lock().unwrap(); |
| 881 | handle_inode_mut_locked( |
| 882 | &mut inode_table, |
| 883 | &parent, |
| 884 | |InodeState { entry, unlinked, .. }| match entry { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 885 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 886 | let basename: &Path = cstr_to_path(name); |
| 887 | // Delete the file from in both the local and remote directories. |
| 888 | let _inode = dir.delete_file(basename)?; |
| 889 | *unlinked = true; |
| 890 | Ok(()) |
| 891 | } |
| 892 | AuthFsEntry::ReadonlyDirectory { .. } => { |
| 893 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 894 | } |
| 895 | AuthFsEntry::VerifiedNew { .. } => { |
| 896 | // Deleting a entry in filesystem root is not currently supported. |
| 897 | Err(io::Error::from_raw_os_error(libc::ENOSYS)) |
| 898 | } |
| 899 | AuthFsEntry::UnverifiedReadonly { .. } | AuthFsEntry::VerifiedReadonly { .. } => { |
| 900 | Err(io::Error::from_raw_os_error(libc::ENOTDIR)) |
| 901 | } |
| 902 | }, |
| 903 | ) |
| 904 | } |
| 905 | |
| 906 | fn rmdir(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> { |
| 907 | let mut inode_table = self.inode_table.lock().unwrap(); |
| 908 | |
| 909 | // Check before actual removal, with readonly borrow. |
| 910 | handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 911 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 912 | let basename: &Path = cstr_to_path(name); |
| 913 | let existing_inode = dir.find_inode(basename)?; |
| 914 | handle_inode_locked(&inode_table, &existing_inode, |inode_state| { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 915 | inode_state.entry.expect_empty_deletable_directory() |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 916 | }) |
| 917 | } |
| 918 | AuthFsEntry::ReadonlyDirectory { .. } => { |
| 919 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 920 | } |
| 921 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 922 | })?; |
| 923 | |
| 924 | // Look up again, this time with mutable borrow. This needs to be done separately because |
| 925 | // the previous lookup needs to borrow multiple entry references in the table. |
| 926 | handle_inode_mut_locked( |
| 927 | &mut inode_table, |
| 928 | &parent, |
| 929 | |InodeState { entry, unlinked, .. }| match entry { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 930 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => { |
Victor Hsieh | dd99b46 | 2021-12-02 17:36:15 -0800 | [diff] [blame] | 931 | let basename: &Path = cstr_to_path(name); |
| 932 | let _inode = dir.force_delete_directory(basename)?; |
| 933 | *unlinked = true; |
| 934 | Ok(()) |
| 935 | } |
| 936 | _ => unreachable!("Mismatched entry type that is just checked"), |
| 937 | }, |
| 938 | ) |
| 939 | } |
| 940 | |
Victor Hsieh | 43a751e | 2021-12-09 17:10:58 -0800 | [diff] [blame] | 941 | fn opendir( |
| 942 | &self, |
| 943 | _ctx: Context, |
| 944 | inode: Self::Inode, |
| 945 | _flags: u32, |
| 946 | ) -> io::Result<(Option<Self::Handle>, FuseOpenOptions)> { |
| 947 | let entries = self.handle_inode(&inode, |config| match config { |
| 948 | AuthFsEntry::VerifiedNewDirectory { dir, .. } => dir.retrieve_entries(), |
| 949 | AuthFsEntry::ReadonlyDirectory { dir } => dir.retrieve_entries(), |
| 950 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 951 | })?; |
| 952 | self.open_dir_store_snapshot(entries) |
| 953 | } |
| 954 | |
| 955 | fn readdir( |
| 956 | &self, |
| 957 | _ctx: Context, |
| 958 | _inode: Self::Inode, |
| 959 | handle: Self::Handle, |
| 960 | _size: u32, |
| 961 | offset: u64, |
| 962 | ) -> io::Result<Self::DirIter> { |
| 963 | let dir_handle_table = self.dir_handle_table.lock().unwrap(); |
| 964 | if let Some(entry) = dir_handle_table.get(&handle) { |
| 965 | Ok(DirEntriesSnapshotIterator { |
| 966 | snapshot: entry.clone(), |
| 967 | prev_offset: offset.try_into().unwrap(), |
| 968 | }) |
| 969 | } else { |
| 970 | Err(io::Error::from_raw_os_error(libc::EBADF)) |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | fn releasedir( |
| 975 | &self, |
| 976 | _ctx: Context, |
| 977 | inode: Self::Inode, |
| 978 | _flags: u32, |
| 979 | handle: Self::Handle, |
| 980 | ) -> io::Result<()> { |
| 981 | let mut dir_handle_table = self.dir_handle_table.lock().unwrap(); |
| 982 | if dir_handle_table.remove(&handle).is_none() { |
| 983 | unreachable!("Unknown directory handle {}, inode {}", handle, inode); |
| 984 | } |
| 985 | Ok(()) |
| 986 | } |
| 987 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 988 | fn statfs(&self, _ctx: Context, _inode: Self::Inode) -> io::Result<libc::statvfs64> { |
| 989 | let remote_stat = self.remote_fs_stats_reader.statfs()?; |
| 990 | |
| 991 | // Safe because we are zero-initializing a struct with only POD fields. Not all fields |
| 992 | // matter to FUSE. See also: |
| 993 | // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/fuse/inode.c?h=v5.15#n460 |
| 994 | let mut st: libc::statvfs64 = unsafe { zeroed() }; |
| 995 | |
| 996 | // Use the remote stat as a template, since it'd matter the most to consider the writable |
| 997 | // files/directories that are written to the remote. |
| 998 | st.f_bsize = remote_stat.block_size; |
| 999 | st.f_frsize = remote_stat.fragment_size; |
| 1000 | st.f_blocks = remote_stat.block_numbers; |
| 1001 | st.f_bavail = remote_stat.block_available; |
| 1002 | st.f_favail = remote_stat.inodes_available; |
| 1003 | st.f_namemax = remote_stat.max_filename; |
| 1004 | // Assuming we are not privileged to use all free spaces on the remote server, set the free |
| 1005 | // blocks/fragment to the same available amount. |
| 1006 | st.f_bfree = st.f_bavail; |
| 1007 | st.f_ffree = st.f_favail; |
| 1008 | // Number of inodes on the filesystem |
| 1009 | st.f_files = self.inode_table.lock().unwrap().len() as u64; |
| 1010 | |
| 1011 | Ok(st) |
| 1012 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame] | 1015 | fn handle_inode_locked<F, R>( |
| 1016 | inode_table: &BTreeMap<Inode, InodeState>, |
| 1017 | inode: &Inode, |
| 1018 | handle_fn: F, |
| 1019 | ) -> io::Result<R> |
| 1020 | where |
| 1021 | F: FnOnce(&InodeState) -> io::Result<R>, |
| 1022 | { |
| 1023 | if let Some(inode_state) = inode_table.get(inode) { |
| 1024 | handle_fn(inode_state) |
| 1025 | } else { |
| 1026 | Err(io::Error::from_raw_os_error(libc::ENOENT)) |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | fn handle_inode_mut_locked<F, R>( |
| 1031 | inode_table: &mut BTreeMap<Inode, InodeState>, |
| 1032 | inode: &Inode, |
| 1033 | handle_fn: F, |
| 1034 | ) -> io::Result<R> |
| 1035 | where |
| 1036 | F: FnOnce(&mut InodeState) -> io::Result<R>, |
| 1037 | { |
| 1038 | if let Some(inode_state) = inode_table.get_mut(inode) { |
| 1039 | handle_fn(inode_state) |
| 1040 | } else { |
| 1041 | Err(io::Error::from_raw_os_error(libc::ENOENT)) |
| 1042 | } |
| 1043 | } |
| 1044 | |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 1045 | fn check_unsupported_setattr_request(valid: SetattrValid) -> io::Result<()> { |
| 1046 | if valid.contains(SetattrValid::UID) { |
| 1047 | warn!("Changing st_uid is not currently supported"); |
| 1048 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 1049 | } |
| 1050 | if valid.contains(SetattrValid::GID) { |
| 1051 | warn!("Changing st_gid is not currently supported"); |
| 1052 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 1053 | } |
| 1054 | if valid.intersects( |
| 1055 | SetattrValid::CTIME |
| 1056 | | SetattrValid::ATIME |
| 1057 | | SetattrValid::ATIME_NOW |
| 1058 | | SetattrValid::MTIME |
| 1059 | | SetattrValid::MTIME_NOW, |
| 1060 | ) { |
| 1061 | debug!("Ignoring ctime/atime/mtime change as authfs does not maintain timestamp currently"); |
| 1062 | } |
| 1063 | Ok(()) |
| 1064 | } |
| 1065 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 1066 | fn cstr_to_path(cstr: &CStr) -> &Path { |
| 1067 | OsStr::from_bytes(cstr.to_bytes()).as_ref() |
| 1068 | } |