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