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