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 | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 20 | use log::{debug, error, warn}; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 21 | use std::collections::{btree_map, BTreeMap}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 22 | use std::convert::TryFrom; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 23 | use std::ffi::{CStr, OsStr}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 24 | use std::io; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 25 | use std::mem::{zeroed, MaybeUninit}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 26 | use std::option::Option; |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 27 | use std::os::unix::ffi::OsStrExt; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 28 | use std::path::{Component, Path, PathBuf}; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 29 | use std::sync::atomic::{AtomicU64, Ordering}; |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 30 | use std::sync::Mutex; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 31 | use std::time::Duration; |
| 32 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 33 | use fuse::filesystem::{ |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 34 | Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply, |
| 35 | SetattrValid, ZeroCopyReader, ZeroCopyWriter, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 36 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 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 | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 40 | validate_basename, InMemoryDir, RandomWrite, ReadByChunk, RemoteDirEditor, RemoteFileEditor, |
| 41 | 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 | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 69 | VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor> }, |
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. |
| 72 | VerifiedNewDirectory { dir: RemoteDirEditor }, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 75 | struct InodeState { |
| 76 | /// Actual inode entry. |
| 77 | entry: AuthFsEntry, |
| 78 | |
| 79 | /// Number of `Handle`s (i.e. file descriptors) that are currently referring to the this inode. |
| 80 | /// |
| 81 | /// Technically, this does not matter to readonly entries, since they live forever. The |
| 82 | /// reference count is only needed for manageing lifetime of writable entries like `VerifiedNew` |
| 83 | /// and `VerifiedNewDirectory`. That is, when an entry is deleted, the actual entry needs to |
| 84 | /// stay alive until the reference count reaches zero. |
| 85 | /// |
| 86 | /// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement. |
| 87 | handle_ref_count: u64, |
| 88 | } |
| 89 | |
| 90 | impl InodeState { |
| 91 | fn new(entry: AuthFsEntry) -> Self { |
| 92 | InodeState { entry, handle_ref_count: 0 } |
| 93 | } |
| 94 | |
| 95 | fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self { |
| 96 | InodeState { entry, handle_ref_count } |
| 97 | } |
| 98 | } |
| 99 | |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 100 | // 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] | 101 | pub struct AuthFs { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 102 | /// 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] | 103 | /// `fuse::worker::start_message_loop`. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 104 | inode_table: Mutex<BTreeMap<Inode, InodeState>>, |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 105 | |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 106 | /// The next available inode number. |
| 107 | next_inode: AtomicU64, |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 108 | |
| 109 | /// A reader to access the remote filesystem stats, which is supposed to be of "the" output |
| 110 | /// directory. We assume all output are stored in the same partition. |
| 111 | remote_fs_stats_reader: RemoteFsStatsReader, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 114 | // Implementation for preparing an `AuthFs` instance, before starting to serve. |
| 115 | // TODO(victorhsieh): Consider implement a builder to separate the mutable initialization from the |
| 116 | // immutable / interiorly mutable serving phase. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 117 | impl AuthFs { |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 118 | pub fn new(remote_fs_stats_reader: RemoteFsStatsReader) -> AuthFs { |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 119 | let mut inode_table = BTreeMap::new(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 120 | inode_table.insert( |
| 121 | ROOT_INODE, |
| 122 | InodeState::new(AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }), |
| 123 | ); |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 124 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 125 | AuthFs { |
| 126 | inode_table: Mutex::new(inode_table), |
| 127 | next_inode: AtomicU64::new(ROOT_INODE + 1), |
| 128 | remote_fs_stats_reader, |
| 129 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 132 | /// Add an `AuthFsEntry` as `basename` to the filesystem root. |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 133 | pub fn add_entry_at_root_dir( |
| 134 | &mut self, |
| 135 | basename: PathBuf, |
| 136 | entry: AuthFsEntry, |
| 137 | ) -> Result<Inode> { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 138 | validate_basename(&basename)?; |
| 139 | self.add_entry_at_ro_dir_by_path(ROOT_INODE, &basename, entry) |
| 140 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 141 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 142 | /// Add an `AuthFsEntry` by path from the `ReadonlyDirectory` represented by `dir_inode`. The |
| 143 | /// path must be a related path. If some ancestor directories do not exist, they will be |
| 144 | /// created (also as `ReadonlyDirectory`) automatically. |
| 145 | pub fn add_entry_at_ro_dir_by_path( |
| 146 | &mut self, |
| 147 | dir_inode: Inode, |
| 148 | path: &Path, |
| 149 | entry: AuthFsEntry, |
| 150 | ) -> Result<Inode> { |
| 151 | // 1. Make sure the parent directories all exist. Derive the entry's parent inode. |
| 152 | let parent_path = |
| 153 | path.parent().ok_or_else(|| anyhow!("No parent directory: {:?}", path))?; |
| 154 | let parent_inode = |
| 155 | parent_path.components().try_fold(dir_inode, |current_dir_inode, path_component| { |
| 156 | match path_component { |
| 157 | Component::RootDir => bail!("Absolute path is not supported"), |
| 158 | Component::Normal(name) => { |
| 159 | let inode_table = self.inode_table.get_mut().unwrap(); |
| 160 | // Locate the internal directory structure. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 161 | let current_dir_entry = &mut inode_table |
| 162 | .get_mut(¤t_dir_inode) |
| 163 | .ok_or_else(|| { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 164 | anyhow!("Unknown directory inode {}", current_dir_inode) |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 165 | })? |
| 166 | .entry; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 167 | let dir = match current_dir_entry { |
| 168 | AuthFsEntry::ReadonlyDirectory { dir } => dir, |
| 169 | _ => unreachable!("Not a ReadonlyDirectory"), |
| 170 | }; |
| 171 | // Return directory inode. Create first if not exists. |
| 172 | if let Some(existing_inode) = dir.lookup_inode(name.as_ref()) { |
| 173 | Ok(existing_inode) |
| 174 | } else { |
| 175 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 176 | let new_dir_entry = |
| 177 | AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }; |
| 178 | |
| 179 | // Actually update the tables. |
| 180 | dir.add_entry(name.as_ref(), new_inode)?; |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 181 | if inode_table |
| 182 | .insert(new_inode, InodeState::new(new_dir_entry)) |
| 183 | .is_some() |
| 184 | { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 185 | bail!("Unexpected to find a duplicated inode"); |
| 186 | } |
| 187 | Ok(new_inode) |
| 188 | } |
| 189 | } |
| 190 | _ => Err(anyhow!("Path is not canonical: {:?}", path)), |
| 191 | } |
| 192 | })?; |
| 193 | |
| 194 | // 2. Insert the entry to the parent directory, as well as the inode table. |
| 195 | let inode_table = self.inode_table.get_mut().unwrap(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 196 | let inode_state = inode_table.get_mut(&parent_inode).expect("previously returned inode"); |
| 197 | match &mut inode_state.entry { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 198 | AuthFsEntry::ReadonlyDirectory { dir } => { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 199 | let basename = |
| 200 | path.file_name().ok_or_else(|| anyhow!("Bad file name: {:?}", path))?; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 201 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 202 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 203 | // Actually update the tables. |
| 204 | dir.add_entry(basename.as_ref(), new_inode)?; |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 205 | if inode_table.insert(new_inode, InodeState::new(entry)).is_some() { |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 206 | bail!("Unexpected to find a duplicated inode"); |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 207 | } |
| 208 | Ok(new_inode) |
| 209 | } |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 210 | _ => unreachable!("Not a ReadonlyDirectory"), |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Implementation for serving requests. |
| 216 | impl AuthFs { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 217 | /// Handles the file associated with `inode` if found. This function returns whatever |
| 218 | /// `handle_fn` returns. |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 219 | 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] | 220 | where |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 221 | F: FnOnce(&AuthFsEntry) -> io::Result<R>, |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 222 | { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 223 | let inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 224 | 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] | 225 | } |
| 226 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 227 | /// Adds a new entry `name` created by `create_fn` at `parent_inode`, with an initial ref count |
| 228 | /// of one. |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 229 | /// |
| 230 | /// The operation involves two updates: adding the name with a new allocated inode to the |
| 231 | /// parent directory, and insert the new inode and the actual `AuthFsEntry` to the global inode |
| 232 | /// table. |
| 233 | /// |
| 234 | /// `create_fn` receives the parent directory, through which it can create the new entry at and |
| 235 | /// 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^] | 236 | fn create_new_entry_with_ref_count<F>( |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 237 | &self, |
| 238 | parent_inode: Inode, |
| 239 | name: &CStr, |
| 240 | create_fn: F, |
| 241 | ) -> io::Result<Inode> |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 242 | where |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 243 | F: FnOnce(&mut AuthFsEntry, &Path, Inode) -> io::Result<AuthFsEntry>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 244 | { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 245 | let mut inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 246 | let (new_inode, new_file_entry) = handle_inode_mut_locked( |
| 247 | &mut inode_table, |
| 248 | &parent_inode, |
| 249 | |InodeState { entry, .. }| { |
| 250 | let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed); |
| 251 | let basename: &Path = cstr_to_path(name); |
| 252 | let new_file_entry = create_fn(entry, basename, new_inode)?; |
| 253 | Ok((new_inode, new_file_entry)) |
| 254 | }, |
| 255 | )?; |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 256 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 257 | if let btree_map::Entry::Vacant(entry) = inode_table.entry(new_inode) { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 258 | entry.insert(InodeState::new_with_ref_count(new_file_entry, 1)); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 259 | Ok(new_inode) |
| 260 | } else { |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 261 | unreachable!("Unexpected duplication of inode {}", new_inode); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 262 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> { |
| 267 | if (flags & libc::O_ACCMODE as u32) == mode as u32 { |
| 268 | Ok(()) |
| 269 | } else { |
| 270 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | cfg_if::cfg_if! { |
| 275 | if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 276 | fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 277 | } else { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 278 | fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 282 | #[allow(clippy::enum_variant_names)] |
| 283 | enum AccessMode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 284 | ReadOnly, |
| 285 | ReadWrite, |
| 286 | } |
| 287 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 288 | fn create_stat( |
| 289 | ino: libc::ino_t, |
| 290 | file_size: u64, |
| 291 | access_mode: AccessMode, |
| 292 | ) -> io::Result<libc::stat64> { |
| 293 | // SAFETY: stat64 is a plan C struct without pointer. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 294 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 295 | |
| 296 | st.st_ino = ino; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 297 | st.st_mode = match access_mode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 298 | // Until needed, let's just grant the owner access. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 299 | // TODO(205169366): Implement mode properly. |
| 300 | AccessMode::ReadOnly => libc::S_IFREG | libc::S_IRUSR, |
| 301 | AccessMode::ReadWrite => libc::S_IFREG | libc::S_IRUSR | libc::S_IWUSR, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 302 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 303 | st.st_nlink = 1; |
| 304 | st.st_uid = 0; |
| 305 | st.st_gid = 0; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 306 | st.st_size = libc::off64_t::try_from(file_size) |
| 307 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 308 | st.st_blksize = blk_size(); |
| 309 | // Per man stat(2), st_blocks is "Number of 512B blocks allocated". |
| 310 | st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512)) |
| 311 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 312 | Ok(st) |
| 313 | } |
| 314 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 315 | fn create_dir_stat(ino: libc::ino_t, file_number: u16) -> io::Result<libc::stat64> { |
| 316 | // SAFETY: stat64 is a plan C struct without pointer. |
| 317 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 318 | |
| 319 | st.st_ino = ino; |
| 320 | // TODO(205169366): Implement mode properly. |
| 321 | st.st_mode = libc::S_IFDIR |
| 322 | | libc::S_IXUSR |
| 323 | | libc::S_IWUSR |
| 324 | | libc::S_IRUSR |
| 325 | | libc::S_IXGRP |
| 326 | | libc::S_IXOTH; |
| 327 | |
| 328 | // 2 extra for . and .. |
| 329 | st.st_nlink = file_number |
| 330 | .checked_add(2) |
| 331 | .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))? |
| 332 | .into(); |
| 333 | |
| 334 | st.st_uid = 0; |
| 335 | st.st_gid = 0; |
| 336 | Ok(st) |
| 337 | } |
| 338 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 339 | fn offset_to_chunk_index(offset: u64) -> u64 { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 340 | offset / CHUNK_SIZE |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 343 | fn read_chunks<W: io::Write, T: ReadByChunk>( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 344 | mut w: W, |
| 345 | file: &T, |
| 346 | file_size: u64, |
| 347 | offset: u64, |
| 348 | size: u32, |
| 349 | ) -> io::Result<usize> { |
| 350 | let remaining = file_size.saturating_sub(offset); |
| 351 | 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] | 352 | 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] | 353 | 0, |
| 354 | |total, (current_offset, planned_data_size)| { |
| 355 | // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example, |
| 356 | // instead of accepting a buffer, the writer could expose the final destination buffer |
| 357 | // for the reader to write to. It might not be generally applicable though, e.g. with |
| 358 | // virtio transport, the buffer may not be continuous. |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 359 | let mut buf = [0u8; CHUNK_SIZE as usize]; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 360 | let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?; |
| 361 | if read_size < planned_data_size { |
| 362 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 363 | } |
| 364 | |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 365 | let begin = (current_offset % CHUNK_SIZE) as usize; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 366 | let end = begin + planned_data_size; |
| 367 | let s = w.write(&buf[begin..end])?; |
| 368 | if s != planned_data_size { |
| 369 | return Err(io::Error::from_raw_os_error(libc::EIO)); |
| 370 | } |
| 371 | Ok(total + s) |
| 372 | }, |
| 373 | )?; |
| 374 | |
| 375 | Ok(total) |
| 376 | } |
| 377 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 378 | // TODO(205715172): Support enumerating directory entries. |
| 379 | pub struct EmptyDirectoryIterator {} |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 380 | |
| 381 | impl DirectoryIterator for EmptyDirectoryIterator { |
| 382 | fn next(&mut self) -> Option<DirEntry> { |
| 383 | None |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | impl FileSystem for AuthFs { |
| 388 | type Inode = Inode; |
| 389 | type Handle = Handle; |
| 390 | type DirIter = EmptyDirectoryIterator; |
| 391 | |
| 392 | fn max_buffer_size(&self) -> u32 { |
Victor Hsieh | 766e533 | 2021-11-09 09:41:25 -0800 | [diff] [blame] | 393 | MAX_WRITE_BYTES |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 396 | fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> { |
| 397 | // Enable writeback cache for better performance especially since our bandwidth to the |
| 398 | // backend service is limited. |
| 399 | Ok(FsOptions::WRITEBACK_CACHE) |
| 400 | } |
| 401 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 402 | fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> { |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 403 | let mut inode_table = self.inode_table.lock().unwrap(); |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 404 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 405 | // Look up the entry's inode number in parent directory. |
| 406 | let inode = |
| 407 | handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry { |
| 408 | AuthFsEntry::ReadonlyDirectory { dir } => { |
| 409 | let path = cstr_to_path(name); |
| 410 | dir.lookup_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT)) |
| 411 | } |
| 412 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
| 413 | let path = cstr_to_path(name); |
| 414 | dir.find_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT)) |
| 415 | } |
| 416 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 417 | })?; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 418 | |
| 419 | // Create the entry's stat if found. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 420 | let st = handle_inode_mut_locked( |
| 421 | &mut inode_table, |
| 422 | &inode, |
| 423 | |InodeState { entry, handle_ref_count, .. }| { |
| 424 | let st = match entry { |
| 425 | AuthFsEntry::ReadonlyDirectory { dir } => { |
| 426 | create_dir_stat(inode, dir.number_of_entries()) |
| 427 | } |
| 428 | AuthFsEntry::UnverifiedReadonly { file_size, .. } |
| 429 | | AuthFsEntry::VerifiedReadonly { file_size, .. } => { |
| 430 | create_stat(inode, *file_size, AccessMode::ReadOnly) |
| 431 | } |
| 432 | AuthFsEntry::VerifiedNew { editor } => { |
| 433 | create_stat(inode, editor.size(), AccessMode::ReadWrite) |
| 434 | } |
| 435 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
| 436 | create_dir_stat(inode, dir.number_of_entries()) |
| 437 | } |
| 438 | }?; |
| 439 | *handle_ref_count += 1; |
| 440 | Ok(st) |
| 441 | }, |
| 442 | )?; |
| 443 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 444 | Ok(Entry { |
| 445 | inode, |
| 446 | generation: 0, |
| 447 | attr: st, |
| 448 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 449 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 450 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 453 | fn forget(&self, _ctx: Context, inode: Self::Inode, count: u64) { |
| 454 | let mut inode_table = self.inode_table.lock().unwrap(); |
| 455 | let _ = handle_inode_mut_locked( |
| 456 | &mut inode_table, |
| 457 | &inode, |
| 458 | |InodeState { handle_ref_count, .. }| { |
| 459 | if count > *handle_ref_count { |
| 460 | error!( |
| 461 | "Trying to decrease refcount of inode {} by {} (> current {})", |
| 462 | inode, count, *handle_ref_count |
| 463 | ); |
| 464 | panic!(); // log to logcat with error! |
| 465 | } |
| 466 | *handle_ref_count = handle_ref_count.saturating_sub(count); |
| 467 | // TODO(208892249): Remove the inode with zero ref count from inode_table, if the |
| 468 | // inode is marked for deletion. |
| 469 | Ok(()) |
| 470 | }, |
| 471 | ); |
| 472 | } |
| 473 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 474 | fn getattr( |
| 475 | &self, |
| 476 | _ctx: Context, |
| 477 | inode: Inode, |
| 478 | _handle: Option<Handle>, |
| 479 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 480 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 481 | Ok(( |
| 482 | match config { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 483 | AuthFsEntry::ReadonlyDirectory { dir } => { |
| 484 | create_dir_stat(inode, dir.number_of_entries()) |
| 485 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 486 | AuthFsEntry::UnverifiedReadonly { file_size, .. } |
| 487 | | AuthFsEntry::VerifiedReadonly { file_size, .. } => { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 488 | create_stat(inode, *file_size, AccessMode::ReadOnly) |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 489 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 490 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 491 | create_stat(inode, editor.size(), AccessMode::ReadWrite) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 492 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 493 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 494 | create_dir_stat(inode, dir.number_of_entries()) |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 495 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 496 | }?, |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 497 | DEFAULT_METADATA_TIMEOUT, |
| 498 | )) |
| 499 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | fn open( |
| 503 | &self, |
| 504 | _ctx: Context, |
| 505 | inode: Self::Inode, |
| 506 | flags: u32, |
| 507 | ) -> io::Result<(Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 508 | // 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] | 509 | // return None as the handle. |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 510 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 511 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 512 | AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 513 | check_access_mode(flags, libc::O_RDONLY)?; |
| 514 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 515 | AuthFsEntry::VerifiedNew { .. } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 516 | // No need to check access modes since all the modes are allowed to the |
| 517 | // read-writable file. |
| 518 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 519 | AuthFsEntry::ReadonlyDirectory { .. } |
| 520 | | AuthFsEntry::VerifiedNewDirectory { .. } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 521 | // TODO(victorhsieh): implement when needed. |
| 522 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 523 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 524 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 525 | // Always cache the file content. There is currently no need to support direct I/O or |
| 526 | // avoid the cache buffer. Memory mapping is only possible with cache enabled. |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 527 | Ok((None, fuse::sys::OpenOptions::KEEP_CACHE)) |
| 528 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 531 | fn create( |
| 532 | &self, |
| 533 | _ctx: Context, |
| 534 | parent: Self::Inode, |
| 535 | name: &CStr, |
| 536 | _mode: u32, |
| 537 | _flags: u32, |
| 538 | _umask: u32, |
| 539 | ) -> io::Result<(Entry, Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 540 | // TODO(205169366): Implement mode properly. |
| 541 | // TODO(205172873): handle O_TRUNC and O_EXCL properly. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 542 | let new_inode = self.create_new_entry_with_ref_count( |
| 543 | parent, |
| 544 | name, |
| 545 | |parent_entry, basename, new_inode| match parent_entry { |
| 546 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
| 547 | if dir.find_inode(basename).is_some() { |
| 548 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 549 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 550 | let new_file = dir.create_file(basename, new_inode)?; |
| 551 | Ok(AuthFsEntry::VerifiedNew { editor: new_file }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 552 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 553 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 554 | }, |
| 555 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 556 | |
| 557 | Ok(( |
| 558 | Entry { |
| 559 | inode: new_inode, |
| 560 | generation: 0, |
| 561 | attr: create_stat(new_inode, /* file_size */ 0, AccessMode::ReadWrite)?, |
| 562 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 563 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 564 | }, |
| 565 | // See also `open`. |
| 566 | /* handle */ None, |
| 567 | fuse::sys::OpenOptions::KEEP_CACHE, |
| 568 | )) |
| 569 | } |
| 570 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 571 | fn read<W: io::Write + ZeroCopyWriter>( |
| 572 | &self, |
| 573 | _ctx: Context, |
| 574 | inode: Inode, |
| 575 | _handle: Handle, |
| 576 | w: W, |
| 577 | size: u32, |
| 578 | offset: u64, |
| 579 | _lock_owner: Option<u64>, |
| 580 | _flags: u32, |
| 581 | ) -> io::Result<usize> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 582 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 583 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 584 | AuthFsEntry::VerifiedReadonly { reader, file_size } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 585 | read_chunks(w, reader, *file_size, offset, size) |
| 586 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 587 | AuthFsEntry::UnverifiedReadonly { reader, file_size } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 588 | read_chunks(w, reader, *file_size, offset, size) |
| 589 | } |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 590 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 591 | // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to |
| 592 | // request a read even if the file is open with O_WRONLY. |
| 593 | read_chunks(w, editor, editor.size(), offset, size) |
| 594 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 595 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 596 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 597 | }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | fn write<R: io::Read + ZeroCopyReader>( |
| 601 | &self, |
| 602 | _ctx: Context, |
| 603 | inode: Self::Inode, |
| 604 | _handle: Self::Handle, |
| 605 | mut r: R, |
| 606 | size: u32, |
| 607 | offset: u64, |
| 608 | _lock_owner: Option<u64>, |
| 609 | _delayed_write: bool, |
| 610 | _flags: u32, |
| 611 | ) -> io::Result<usize> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 612 | self.handle_inode(&inode, |config| match config { |
| 613 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 614 | let mut buf = vec![0; size as usize]; |
| 615 | r.read_exact(&mut buf)?; |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 616 | editor.write_at(&buf, offset) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 617 | } |
| 618 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 619 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 620 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 621 | |
| 622 | fn setattr( |
| 623 | &self, |
| 624 | _ctx: Context, |
| 625 | inode: Inode, |
| 626 | attr: libc::stat64, |
| 627 | _handle: Option<Handle>, |
| 628 | valid: SetattrValid, |
| 629 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 630 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 631 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 632 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 633 | // Initialize the default stat. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 634 | let mut new_attr = create_stat(inode, editor.size(), AccessMode::ReadWrite)?; |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 635 | // `valid` indicates what fields in `attr` are valid. Update to return correctly. |
| 636 | if valid.contains(SetattrValid::SIZE) { |
| 637 | // st_size is i64, but the cast should be safe since kernel should not give a |
| 638 | // negative size. |
| 639 | debug_assert!(attr.st_size >= 0); |
| 640 | new_attr.st_size = attr.st_size; |
| 641 | editor.resize(attr.st_size as u64)?; |
| 642 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 643 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 644 | if valid.contains(SetattrValid::MODE) { |
| 645 | warn!("Changing st_mode is not currently supported"); |
| 646 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 647 | } |
| 648 | if valid.contains(SetattrValid::UID) { |
| 649 | warn!("Changing st_uid is not currently supported"); |
| 650 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 651 | } |
| 652 | if valid.contains(SetattrValid::GID) { |
| 653 | warn!("Changing st_gid is not currently supported"); |
| 654 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 655 | } |
| 656 | if valid.contains(SetattrValid::CTIME) { |
| 657 | debug!( |
| 658 | "Ignoring ctime change as authfs does not maintain timestamp currently" |
| 659 | ); |
| 660 | } |
| 661 | if valid.intersects(SetattrValid::ATIME | SetattrValid::ATIME_NOW) { |
| 662 | debug!( |
| 663 | "Ignoring atime change as authfs does not maintain timestamp currently" |
| 664 | ); |
| 665 | } |
| 666 | if valid.intersects(SetattrValid::MTIME | SetattrValid::MTIME_NOW) { |
| 667 | debug!( |
| 668 | "Ignoring mtime change as authfs does not maintain timestamp currently" |
| 669 | ); |
| 670 | } |
| 671 | Ok((new_attr, DEFAULT_METADATA_TIMEOUT)) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 672 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 673 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 674 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 675 | }) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 676 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 677 | |
| 678 | fn getxattr( |
| 679 | &self, |
| 680 | _ctx: Context, |
| 681 | inode: Self::Inode, |
| 682 | name: &CStr, |
| 683 | size: u32, |
| 684 | ) -> io::Result<GetxattrReply> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 685 | self.handle_inode(&inode, |config| { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 686 | match config { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 687 | AuthFsEntry::VerifiedNew { editor } => { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 688 | // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel |
| 689 | // change (see b/196635431). Until it's possible, use xattr to expose what we need |
| 690 | // as an authfs specific API. |
| 691 | if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() { |
| 692 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 693 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 694 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 695 | if size == 0 { |
| 696 | // Per protocol, when size is 0, return the value size. |
| 697 | Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32)) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 698 | } else { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 699 | let digest = editor.calculate_fsverity_digest()?; |
| 700 | if digest.len() > size as usize { |
| 701 | Err(io::Error::from_raw_os_error(libc::ERANGE)) |
| 702 | } else { |
| 703 | Ok(GetxattrReply::Value(digest.to_vec())) |
| 704 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 705 | } |
| 706 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 707 | _ => Err(io::Error::from_raw_os_error(libc::ENODATA)), |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 708 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 709 | }) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 710 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 711 | |
| 712 | fn mkdir( |
| 713 | &self, |
| 714 | _ctx: Context, |
| 715 | parent: Self::Inode, |
| 716 | name: &CStr, |
| 717 | _mode: u32, |
| 718 | _umask: u32, |
| 719 | ) -> io::Result<Entry> { |
| 720 | // TODO(205169366): Implement mode properly. |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 721 | let new_inode = self.create_new_entry_with_ref_count( |
| 722 | parent, |
| 723 | name, |
| 724 | |parent_entry, basename, new_inode| match parent_entry { |
| 725 | AuthFsEntry::VerifiedNewDirectory { dir } => { |
| 726 | if dir.find_inode(basename).is_some() { |
| 727 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame] | 728 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 729 | let new_dir = dir.mkdir(basename, new_inode)?; |
| 730 | Ok(AuthFsEntry::VerifiedNewDirectory { dir: new_dir }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 731 | } |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 732 | AuthFsEntry::ReadonlyDirectory { .. } => { |
| 733 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 734 | } |
| 735 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 736 | }, |
| 737 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 738 | |
| 739 | Ok(Entry { |
| 740 | inode: new_inode, |
| 741 | generation: 0, |
| 742 | attr: create_dir_stat(new_inode, /* file_number */ 0)?, |
| 743 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 744 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 745 | }) |
| 746 | } |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 747 | |
| 748 | fn statfs(&self, _ctx: Context, _inode: Self::Inode) -> io::Result<libc::statvfs64> { |
| 749 | let remote_stat = self.remote_fs_stats_reader.statfs()?; |
| 750 | |
| 751 | // Safe because we are zero-initializing a struct with only POD fields. Not all fields |
| 752 | // matter to FUSE. See also: |
| 753 | // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/fuse/inode.c?h=v5.15#n460 |
| 754 | let mut st: libc::statvfs64 = unsafe { zeroed() }; |
| 755 | |
| 756 | // Use the remote stat as a template, since it'd matter the most to consider the writable |
| 757 | // files/directories that are written to the remote. |
| 758 | st.f_bsize = remote_stat.block_size; |
| 759 | st.f_frsize = remote_stat.fragment_size; |
| 760 | st.f_blocks = remote_stat.block_numbers; |
| 761 | st.f_bavail = remote_stat.block_available; |
| 762 | st.f_favail = remote_stat.inodes_available; |
| 763 | st.f_namemax = remote_stat.max_filename; |
| 764 | // Assuming we are not privileged to use all free spaces on the remote server, set the free |
| 765 | // blocks/fragment to the same available amount. |
| 766 | st.f_bfree = st.f_bavail; |
| 767 | st.f_ffree = st.f_favail; |
| 768 | // Number of inodes on the filesystem |
| 769 | st.f_files = self.inode_table.lock().unwrap().len() as u64; |
| 770 | |
| 771 | Ok(st) |
| 772 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Victor Hsieh | 3dccf70 | 2021-12-02 15:45:14 -0800 | [diff] [blame^] | 775 | fn handle_inode_locked<F, R>( |
| 776 | inode_table: &BTreeMap<Inode, InodeState>, |
| 777 | inode: &Inode, |
| 778 | handle_fn: F, |
| 779 | ) -> io::Result<R> |
| 780 | where |
| 781 | F: FnOnce(&InodeState) -> io::Result<R>, |
| 782 | { |
| 783 | if let Some(inode_state) = inode_table.get(inode) { |
| 784 | handle_fn(inode_state) |
| 785 | } else { |
| 786 | Err(io::Error::from_raw_os_error(libc::ENOENT)) |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | fn handle_inode_mut_locked<F, R>( |
| 791 | inode_table: &mut BTreeMap<Inode, InodeState>, |
| 792 | inode: &Inode, |
| 793 | handle_fn: F, |
| 794 | ) -> io::Result<R> |
| 795 | where |
| 796 | F: FnOnce(&mut InodeState) -> io::Result<R>, |
| 797 | { |
| 798 | if let Some(inode_state) = inode_table.get_mut(inode) { |
| 799 | handle_fn(inode_state) |
| 800 | } else { |
| 801 | Err(io::Error::from_raw_os_error(libc::ENOENT)) |
| 802 | } |
| 803 | } |
| 804 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 805 | fn cstr_to_path(cstr: &CStr) -> &Path { |
| 806 | OsStr::from_bytes(cstr.to_bytes()).as_ref() |
| 807 | } |