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