blob: 9b866f5a31ac4f6b5460ea07597d7a942732655c [file] [log] [blame]
Victor Hsieh88ac6ca2020-11-13 15:20:24 -08001/*
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 Hsiehe8137e32022-02-11 22:14:12 +000017mod file;
Victor Hsieh79f296b2021-12-02 15:38:08 -080018mod mount;
19
Victor Hsiehd18b9752021-11-09 16:03:34 -080020use anyhow::{anyhow, bail, Result};
Victor Hsieh43a751e2021-12-09 17:10:58 -080021use fuse::filesystem::{
22 Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply,
23 SetattrValid, ZeroCopyReader, ZeroCopyWriter,
24};
25use fuse::sys::OpenOptions as FuseOpenOptions;
Victor Hsieh3dccf702021-12-02 15:45:14 -080026use log::{debug, error, warn};
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080027use std::collections::{btree_map, BTreeMap};
Victor Hsieh43a751e2021-12-09 17:10:58 -080028use std::convert::{TryFrom, TryInto};
29use std::ffi::{CStr, CString, OsStr};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080030use std::io;
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080031use std::mem::{zeroed, MaybeUninit};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080032use std::option::Option;
Victor Hsieh79f296b2021-12-02 15:38:08 -080033use std::os::unix::ffi::OsStrExt;
Victor Hsiehd18b9752021-11-09 16:03:34 -080034use std::path::{Component, Path, PathBuf};
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -080035use std::sync::atomic::{AtomicU64, Ordering};
Victor Hsieh43a751e2021-12-09 17:10:58 -080036use std::sync::{Arc, Mutex};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080037use std::time::Duration;
38
Victor Hsiehac4f3f42021-02-26 12:35:58 -080039use crate::common::{divide_roundup, ChunkedSizeIter, CHUNK_SIZE};
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080040use crate::file::{
Victor Hsiehe8137e32022-02-11 22:14:12 +000041 validate_basename, Attr, InMemoryDir, RandomWrite, ReadByChunk, RemoteDirEditor,
42 RemoteFileEditor, RemoteFileReader,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080043};
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080044use crate::fsstat::RemoteFsStatsReader;
Victor Hsiehe8137e32022-02-11 22:14:12 +000045use crate::fsverity::VerifiedFileEditor;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080046
Victor Hsiehe8137e32022-02-11 22:14:12 +000047pub use self::file::LazyVerifiedReadonlyFile;
Victor Hsieh79f296b2021-12-02 15:38:08 -080048pub use self::mount::mount_and_enter_message_loop;
49use self::mount::MAX_WRITE_BYTES;
50
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080051pub type Inode = u64;
52type Handle = u64;
53
Victor Hsieh259dd9c2022-02-09 20:31:57 +000054/// Maximum time for a file's metadata to be cached by the kernel. Since any file and directory
55/// changes (if not read-only) has to go through AuthFS to be trusted, the timeout can be maximum.
56const DEFAULT_METADATA_TIMEOUT: Duration = Duration::MAX;
57
Victor Hsieh26cea2f2021-11-03 10:28:33 -070058const ROOT_INODE: Inode = 1;
59
60/// `AuthFsEntry` defines the filesystem entry type supported by AuthFS.
61pub enum AuthFsEntry {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080062 /// A read-only directory (writable during initialization). Root directory is an example.
63 ReadonlyDirectory { dir: InMemoryDir },
Victor Hsieh1bcf4112021-03-19 14:26:57 -070064 /// A file type that is verified against fs-verity signature (thus read-only). The file is
Victor Hsieh1bcf4112021-03-19 14:26:57 -070065 /// served from a remote server.
Victor Hsiehe8137e32022-02-11 22:14:12 +000066 VerifiedReadonly { reader: LazyVerifiedReadonlyFile },
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080067 /// A file type that is a read-only passthrough from a file on a remote server.
Victor Hsieh88e50172021-10-15 13:27:13 -070068 UnverifiedReadonly { reader: RemoteFileReader, file_size: u64 },
Victor Hsieh1bcf4112021-03-19 14:26:57 -070069 /// A file type that is initially empty, and the content is stored on a remote server. File
70 /// integrity is guaranteed with private Merkle tree.
Victor Hsiehf393a722021-12-08 13:04:27 -080071 VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor>, attr: Attr },
Victor Hsieh45636232021-10-15 17:52:51 -070072 /// A directory type that is initially empty. One can create new file (`VerifiedNew`) and new
73 /// directory (`VerifiedNewDirectory` itself) with integrity guaranteed within the VM.
Victor Hsiehf393a722021-12-08 13:04:27 -080074 VerifiedNewDirectory { dir: RemoteDirEditor, attr: Attr },
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080075}
76
Victor Hsiehdd99b462021-12-02 17:36:15 -080077impl AuthFsEntry {
Victor Hsiehf393a722021-12-08 13:04:27 -080078 fn expect_empty_deletable_directory(&self) -> io::Result<()> {
Victor Hsiehdd99b462021-12-02 17:36:15 -080079 match self {
Victor Hsiehf393a722021-12-08 13:04:27 -080080 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -080081 if dir.number_of_entries() == 0 {
82 Ok(())
83 } else {
84 Err(io::Error::from_raw_os_error(libc::ENOTEMPTY))
85 }
86 }
87 AuthFsEntry::ReadonlyDirectory { .. } => {
88 Err(io::Error::from_raw_os_error(libc::EACCES))
89 }
90 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
91 }
92 }
93}
94
Victor Hsieh3dccf702021-12-02 15:45:14 -080095struct InodeState {
96 /// Actual inode entry.
97 entry: AuthFsEntry,
98
99 /// Number of `Handle`s (i.e. file descriptors) that are currently referring to the this inode.
100 ///
101 /// Technically, this does not matter to readonly entries, since they live forever. The
102 /// reference count is only needed for manageing lifetime of writable entries like `VerifiedNew`
103 /// and `VerifiedNewDirectory`. That is, when an entry is deleted, the actual entry needs to
104 /// stay alive until the reference count reaches zero.
105 ///
106 /// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement.
107 handle_ref_count: u64,
Victor Hsiehdd99b462021-12-02 17:36:15 -0800108
109 /// Whether the inode is already unlinked, i.e. should be removed, once `handle_ref_count` is
110 /// down to zero.
111 unlinked: bool,
Victor Hsieh3dccf702021-12-02 15:45:14 -0800112}
113
114impl InodeState {
115 fn new(entry: AuthFsEntry) -> Self {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800116 InodeState { entry, handle_ref_count: 0, unlinked: false }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800117 }
118
119 fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800120 InodeState { entry, handle_ref_count, unlinked: false }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800121 }
122}
123
Victor Hsieh43a751e2021-12-09 17:10:58 -0800124/// Data type that a directory implementation should be able to present its entry to `AuthFs`.
125#[derive(Clone)]
126pub struct AuthFsDirEntry {
127 pub inode: Inode,
128 pub name: CString,
129 pub is_dir: bool,
130}
131
132/// A snapshot of a directory entries for supporting `readdir` operation.
133///
134/// The `readdir` implementation is required by FUSE to not return any entries that have been
135/// returned previously (while it's fine to not return new entries). Snapshot is the easiest way to
136/// be compliant. See `fuse::filesystem::readdir` for more details.
137///
138/// A `DirEntriesSnapshot` is created on `opendir`, and is associated with the returned
139/// `Handle`/FD. The snapshot is deleted when the handle is released in `releasedir`.
140type DirEntriesSnapshot = Vec<AuthFsDirEntry>;
141
142/// An iterator for reading from `DirEntriesSnapshot`.
143pub struct DirEntriesSnapshotIterator {
144 /// A reference to the `DirEntriesSnapshot` in `AuthFs`.
145 snapshot: Arc<DirEntriesSnapshot>,
146
147 /// A value determined by `Self` to identify the last entry. 0 is a reserved value by FUSE to
148 /// mean reading from the beginning.
149 prev_offset: usize,
150}
151
152impl<'a> DirectoryIterator for DirEntriesSnapshotIterator {
153 fn next(&mut self) -> Option<DirEntry> {
154 // This iterator should not be the only reference to the snapshot. The snapshot should
155 // still be hold in `dir_handle_table`, i.e. when the FD is not yet closed.
156 //
157 // This code is unreachable when `readdir` is called with a closed FD. Only when the FD is
158 // not yet closed, `DirEntriesSnapshotIterator` can be created (but still short-lived
159 // during `readdir`).
160 debug_assert!(Arc::strong_count(&self.snapshot) >= 2);
161
162 // Since 0 is reserved, let's use 1-based index for the offset. This allows us to
163 // resume from the previous read in the snapshot easily.
164 let current_offset = if self.prev_offset == 0 {
165 1 // first element in the vector
166 } else {
167 self.prev_offset + 1 // next element in the vector
168 };
169 if current_offset > self.snapshot.len() {
170 None
171 } else {
172 let AuthFsDirEntry { inode, name, is_dir } = &self.snapshot[current_offset - 1];
173 let entry = DirEntry {
174 offset: current_offset as u64,
175 ino: *inode,
176 name,
177 type_: if *is_dir { libc::DT_DIR.into() } else { libc::DT_REG.into() },
178 };
179 self.prev_offset = current_offset;
180 Some(entry)
181 }
182 }
183}
184
185type DirHandleTable = BTreeMap<Handle, Arc<DirEntriesSnapshot>>;
186
Victor Hsieh60c2f412021-11-03 13:02:19 -0700187// AuthFS needs to be `Sync` to be accepted by fuse::worker::start_message_loop as a `FileSystem`.
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800188pub struct AuthFs {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800189 /// Table for `Inode` to `InodeState` lookup. This needs to be `Sync` to be used in
Victor Hsieh60c2f412021-11-03 13:02:19 -0700190 /// `fuse::worker::start_message_loop`.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800191 inode_table: Mutex<BTreeMap<Inode, InodeState>>,
Victor Hsieh60c2f412021-11-03 13:02:19 -0700192
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800193 /// The next available inode number.
194 next_inode: AtomicU64,
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800195
Victor Hsieh43a751e2021-12-09 17:10:58 -0800196 /// Table for `Handle` to `Arc<DirEntriesSnapshot>` lookup. On `opendir`, a new directory handle
197 /// is created and the snapshot of the current directory is created. This is not super
198 /// efficient, but is the simplest way to be compliant to the FUSE contract (see
199 /// `fuse::filesystem::readdir`).
200 ///
201 /// Currently, no code locks `dir_handle_table` and `inode_table` at the same time to avoid
202 /// deadlock.
203 dir_handle_table: Mutex<DirHandleTable>,
204
205 /// The next available handle number.
206 next_handle: AtomicU64,
207
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800208 /// A reader to access the remote filesystem stats, which is supposed to be of "the" output
209 /// directory. We assume all output are stored in the same partition.
210 remote_fs_stats_reader: RemoteFsStatsReader,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800211}
212
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800213// Implementation for preparing an `AuthFs` instance, before starting to serve.
214// TODO(victorhsieh): Consider implement a builder to separate the mutable initialization from the
215// immutable / interiorly mutable serving phase.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800216impl AuthFs {
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800217 pub fn new(remote_fs_stats_reader: RemoteFsStatsReader) -> AuthFs {
Victor Hsieh60c2f412021-11-03 13:02:19 -0700218 let mut inode_table = BTreeMap::new();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800219 inode_table.insert(
220 ROOT_INODE,
221 InodeState::new(AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }),
222 );
Victor Hsieh60c2f412021-11-03 13:02:19 -0700223
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800224 AuthFs {
225 inode_table: Mutex::new(inode_table),
226 next_inode: AtomicU64::new(ROOT_INODE + 1),
Victor Hsieh43a751e2021-12-09 17:10:58 -0800227 dir_handle_table: Mutex::new(BTreeMap::new()),
228 next_handle: AtomicU64::new(1),
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800229 remote_fs_stats_reader,
230 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800231 }
232
Victor Hsiehd18b9752021-11-09 16:03:34 -0800233 /// Add an `AuthFsEntry` as `basename` to the filesystem root.
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800234 pub fn add_entry_at_root_dir(
235 &mut self,
236 basename: PathBuf,
237 entry: AuthFsEntry,
238 ) -> Result<Inode> {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800239 validate_basename(&basename)?;
240 self.add_entry_at_ro_dir_by_path(ROOT_INODE, &basename, entry)
241 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800242
Victor Hsiehd18b9752021-11-09 16:03:34 -0800243 /// Add an `AuthFsEntry` by path from the `ReadonlyDirectory` represented by `dir_inode`. The
244 /// path must be a related path. If some ancestor directories do not exist, they will be
245 /// created (also as `ReadonlyDirectory`) automatically.
246 pub fn add_entry_at_ro_dir_by_path(
247 &mut self,
248 dir_inode: Inode,
249 path: &Path,
250 entry: AuthFsEntry,
251 ) -> Result<Inode> {
252 // 1. Make sure the parent directories all exist. Derive the entry's parent inode.
253 let parent_path =
254 path.parent().ok_or_else(|| anyhow!("No parent directory: {:?}", path))?;
255 let parent_inode =
256 parent_path.components().try_fold(dir_inode, |current_dir_inode, path_component| {
257 match path_component {
258 Component::RootDir => bail!("Absolute path is not supported"),
259 Component::Normal(name) => {
260 let inode_table = self.inode_table.get_mut().unwrap();
261 // Locate the internal directory structure.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800262 let current_dir_entry = &mut inode_table
263 .get_mut(&current_dir_inode)
264 .ok_or_else(|| {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800265 anyhow!("Unknown directory inode {}", current_dir_inode)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800266 })?
267 .entry;
Victor Hsiehd18b9752021-11-09 16:03:34 -0800268 let dir = match current_dir_entry {
269 AuthFsEntry::ReadonlyDirectory { dir } => dir,
270 _ => unreachable!("Not a ReadonlyDirectory"),
271 };
272 // Return directory inode. Create first if not exists.
273 if let Some(existing_inode) = dir.lookup_inode(name.as_ref()) {
274 Ok(existing_inode)
275 } else {
276 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
277 let new_dir_entry =
278 AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() };
279
280 // Actually update the tables.
Victor Hsieh43a751e2021-12-09 17:10:58 -0800281 dir.add_dir(name.as_ref(), new_inode)?;
Victor Hsieh3dccf702021-12-02 15:45:14 -0800282 if inode_table
283 .insert(new_inode, InodeState::new(new_dir_entry))
284 .is_some()
285 {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800286 bail!("Unexpected to find a duplicated inode");
287 }
288 Ok(new_inode)
289 }
290 }
291 _ => Err(anyhow!("Path is not canonical: {:?}", path)),
292 }
293 })?;
294
295 // 2. Insert the entry to the parent directory, as well as the inode table.
296 let inode_table = self.inode_table.get_mut().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800297 let inode_state = inode_table.get_mut(&parent_inode).expect("previously returned inode");
298 match &mut inode_state.entry {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800299 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800300 let basename =
301 path.file_name().ok_or_else(|| anyhow!("Bad file name: {:?}", path))?;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800302 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
303
Victor Hsiehd18b9752021-11-09 16:03:34 -0800304 // Actually update the tables.
Victor Hsieh43a751e2021-12-09 17:10:58 -0800305 dir.add_file(basename.as_ref(), new_inode)?;
Victor Hsieh3dccf702021-12-02 15:45:14 -0800306 if inode_table.insert(new_inode, InodeState::new(entry)).is_some() {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800307 bail!("Unexpected to find a duplicated inode");
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800308 }
309 Ok(new_inode)
310 }
Victor Hsiehd18b9752021-11-09 16:03:34 -0800311 _ => unreachable!("Not a ReadonlyDirectory"),
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800312 }
313 }
314}
315
316// Implementation for serving requests.
317impl AuthFs {
Victor Hsieh45636232021-10-15 17:52:51 -0700318 /// Handles the file associated with `inode` if found. This function returns whatever
319 /// `handle_fn` returns.
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700320 fn handle_inode<F, R>(&self, inode: &Inode, handle_fn: F) -> io::Result<R>
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700321 where
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700322 F: FnOnce(&AuthFsEntry) -> io::Result<R>,
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700323 {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700324 let inode_table = self.inode_table.lock().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800325 handle_inode_locked(&inode_table, inode, |inode_state| handle_fn(&inode_state.entry))
Victor Hsieh45636232021-10-15 17:52:51 -0700326 }
327
Victor Hsieh3dccf702021-12-02 15:45:14 -0800328 /// Adds a new entry `name` created by `create_fn` at `parent_inode`, with an initial ref count
329 /// of one.
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800330 ///
331 /// The operation involves two updates: adding the name with a new allocated inode to the
332 /// parent directory, and insert the new inode and the actual `AuthFsEntry` to the global inode
333 /// table.
334 ///
335 /// `create_fn` receives the parent directory, through which it can create the new entry at and
336 /// register the new inode to. Its returned entry is then added to the inode table.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800337 fn create_new_entry_with_ref_count<F>(
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800338 &self,
339 parent_inode: Inode,
340 name: &CStr,
341 create_fn: F,
342 ) -> io::Result<Inode>
Victor Hsieh45636232021-10-15 17:52:51 -0700343 where
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800344 F: FnOnce(&mut AuthFsEntry, &Path, Inode) -> io::Result<AuthFsEntry>,
Victor Hsieh45636232021-10-15 17:52:51 -0700345 {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700346 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800347 let (new_inode, new_file_entry) = handle_inode_mut_locked(
348 &mut inode_table,
349 &parent_inode,
350 |InodeState { entry, .. }| {
351 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
352 let basename: &Path = cstr_to_path(name);
353 let new_file_entry = create_fn(entry, basename, new_inode)?;
354 Ok((new_inode, new_file_entry))
355 },
356 )?;
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800357
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700358 if let btree_map::Entry::Vacant(entry) = inode_table.entry(new_inode) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800359 entry.insert(InodeState::new_with_ref_count(new_file_entry, 1));
Victor Hsieh45636232021-10-15 17:52:51 -0700360 Ok(new_inode)
361 } else {
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800362 unreachable!("Unexpected duplication of inode {}", new_inode);
Victor Hsieh45636232021-10-15 17:52:51 -0700363 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800364 }
Victor Hsieh43a751e2021-12-09 17:10:58 -0800365
366 fn open_dir_store_snapshot(
367 &self,
368 dir_entries: Vec<AuthFsDirEntry>,
369 ) -> io::Result<(Option<Handle>, FuseOpenOptions)> {
370 let handle = self.next_handle.fetch_add(1, Ordering::Relaxed);
371 let mut dir_handle_table = self.dir_handle_table.lock().unwrap();
372 if let btree_map::Entry::Vacant(value) = dir_handle_table.entry(handle) {
373 value.insert(Arc::new(dir_entries));
374 Ok((Some(handle), FuseOpenOptions::empty()))
375 } else {
376 unreachable!("Unexpected to see new handle {} to existing in the table", handle);
377 }
378 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800379}
380
381fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> {
382 if (flags & libc::O_ACCMODE as u32) == mode as u32 {
383 Ok(())
384 } else {
385 Err(io::Error::from_raw_os_error(libc::EACCES))
386 }
387}
388
389cfg_if::cfg_if! {
390 if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800391 fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800392 } else {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800393 fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800394 }
395}
396
Victor Hsieh45636232021-10-15 17:52:51 -0700397#[allow(clippy::enum_variant_names)]
398enum AccessMode {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800399 ReadOnly,
Victor Hsiehf393a722021-12-08 13:04:27 -0800400 Variable(u32),
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800401}
402
Victor Hsieh45636232021-10-15 17:52:51 -0700403fn create_stat(
404 ino: libc::ino_t,
405 file_size: u64,
406 access_mode: AccessMode,
407) -> io::Result<libc::stat64> {
408 // SAFETY: stat64 is a plan C struct without pointer.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800409 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
410
411 st.st_ino = ino;
Victor Hsieh45636232021-10-15 17:52:51 -0700412 st.st_mode = match access_mode {
Victor Hsiehf393a722021-12-08 13:04:27 -0800413 AccessMode::ReadOnly => {
414 // Until needed, let's just grant the owner access.
415 libc::S_IFREG | libc::S_IRUSR
416 }
417 AccessMode::Variable(mode) => libc::S_IFREG | mode,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800418 };
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800419 st.st_nlink = 1;
420 st.st_uid = 0;
421 st.st_gid = 0;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800422 st.st_size = libc::off64_t::try_from(file_size)
423 .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?;
424 st.st_blksize = blk_size();
425 // Per man stat(2), st_blocks is "Number of 512B blocks allocated".
426 st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512))
427 .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?;
428 Ok(st)
429}
430
Victor Hsiehf393a722021-12-08 13:04:27 -0800431fn create_dir_stat(
432 ino: libc::ino_t,
433 file_number: u16,
434 access_mode: AccessMode,
435) -> io::Result<libc::stat64> {
Victor Hsieh45636232021-10-15 17:52:51 -0700436 // SAFETY: stat64 is a plan C struct without pointer.
437 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
438
439 st.st_ino = ino;
Victor Hsiehf393a722021-12-08 13:04:27 -0800440 st.st_mode = match access_mode {
441 AccessMode::ReadOnly => {
442 // Until needed, let's just grant the owner access and search to group and others.
443 libc::S_IFDIR | libc::S_IXUSR | libc::S_IRUSR | libc::S_IXGRP | libc::S_IXOTH
444 }
445 AccessMode::Variable(mode) => libc::S_IFDIR | mode,
446 };
Victor Hsieh45636232021-10-15 17:52:51 -0700447
448 // 2 extra for . and ..
449 st.st_nlink = file_number
450 .checked_add(2)
451 .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))?
452 .into();
453
454 st.st_uid = 0;
455 st.st_gid = 0;
456 Ok(st)
457}
458
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800459fn offset_to_chunk_index(offset: u64) -> u64 {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800460 offset / CHUNK_SIZE
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800461}
462
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700463fn read_chunks<W: io::Write, T: ReadByChunk>(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800464 mut w: W,
465 file: &T,
466 file_size: u64,
467 offset: u64,
468 size: u32,
469) -> io::Result<usize> {
470 let remaining = file_size.saturating_sub(offset);
471 let size_to_read = std::cmp::min(size as usize, remaining as usize);
Victor Hsiehac4f3f42021-02-26 12:35:58 -0800472 let total = ChunkedSizeIter::new(size_to_read, offset, CHUNK_SIZE as usize).try_fold(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800473 0,
474 |total, (current_offset, planned_data_size)| {
475 // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example,
476 // instead of accepting a buffer, the writer could expose the final destination buffer
477 // for the reader to write to. It might not be generally applicable though, e.g. with
478 // virtio transport, the buffer may not be continuous.
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800479 let mut buf = [0u8; CHUNK_SIZE as usize];
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800480 let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?;
481 if read_size < planned_data_size {
482 return Err(io::Error::from_raw_os_error(libc::ENODATA));
483 }
484
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800485 let begin = (current_offset % CHUNK_SIZE) as usize;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800486 let end = begin + planned_data_size;
487 let s = w.write(&buf[begin..end])?;
488 if s != planned_data_size {
489 return Err(io::Error::from_raw_os_error(libc::EIO));
490 }
491 Ok(total + s)
492 },
493 )?;
494
495 Ok(total)
496}
497
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800498impl FileSystem for AuthFs {
499 type Inode = Inode;
500 type Handle = Handle;
Victor Hsieh43a751e2021-12-09 17:10:58 -0800501 type DirIter = DirEntriesSnapshotIterator;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800502
503 fn max_buffer_size(&self) -> u32 {
Victor Hsieh766e5332021-11-09 09:41:25 -0800504 MAX_WRITE_BYTES
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800505 }
506
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800507 fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> {
508 // Enable writeback cache for better performance especially since our bandwidth to the
509 // backend service is limited.
510 Ok(FsOptions::WRITEBACK_CACHE)
511 }
512
Victor Hsieh45636232021-10-15 17:52:51 -0700513 fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800514 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800515
Victor Hsieh3dccf702021-12-02 15:45:14 -0800516 // Look up the entry's inode number in parent directory.
517 let inode =
518 handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry {
519 AuthFsEntry::ReadonlyDirectory { dir } => {
520 let path = cstr_to_path(name);
521 dir.lookup_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))
522 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800523 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800524 let path = cstr_to_path(name);
Victor Hsiehdd99b462021-12-02 17:36:15 -0800525 dir.find_inode(path)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800526 }
527 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
528 })?;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800529
530 // Create the entry's stat if found.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800531 let st = handle_inode_mut_locked(
532 &mut inode_table,
533 &inode,
534 |InodeState { entry, handle_ref_count, .. }| {
535 let st = match entry {
536 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehf393a722021-12-08 13:04:27 -0800537 create_dir_stat(inode, dir.number_of_entries(), AccessMode::ReadOnly)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800538 }
Victor Hsiehe8137e32022-02-11 22:14:12 +0000539 AuthFsEntry::UnverifiedReadonly { file_size, .. } => {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800540 create_stat(inode, *file_size, AccessMode::ReadOnly)
541 }
Victor Hsiehe8137e32022-02-11 22:14:12 +0000542 AuthFsEntry::VerifiedReadonly { reader } => {
543 create_stat(inode, reader.file_size()?, AccessMode::ReadOnly)
544 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800545 AuthFsEntry::VerifiedNew { editor, attr, .. } => {
546 create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))
Victor Hsieh3dccf702021-12-02 15:45:14 -0800547 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800548 AuthFsEntry::VerifiedNewDirectory { dir, attr } => create_dir_stat(
549 inode,
550 dir.number_of_entries(),
551 AccessMode::Variable(attr.mode()),
552 ),
Victor Hsieh3dccf702021-12-02 15:45:14 -0800553 }?;
554 *handle_ref_count += 1;
555 Ok(st)
556 },
557 )?;
558
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800559 Ok(Entry {
560 inode,
561 generation: 0,
562 attr: st,
563 entry_timeout: DEFAULT_METADATA_TIMEOUT,
564 attr_timeout: DEFAULT_METADATA_TIMEOUT,
565 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800566 }
567
Victor Hsieh3dccf702021-12-02 15:45:14 -0800568 fn forget(&self, _ctx: Context, inode: Self::Inode, count: u64) {
569 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsiehdd99b462021-12-02 17:36:15 -0800570 let delete_now = handle_inode_mut_locked(
Victor Hsieh3dccf702021-12-02 15:45:14 -0800571 &mut inode_table,
572 &inode,
Victor Hsiehdd99b462021-12-02 17:36:15 -0800573 |InodeState { handle_ref_count, unlinked, .. }| {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800574 if count > *handle_ref_count {
575 error!(
576 "Trying to decrease refcount of inode {} by {} (> current {})",
577 inode, count, *handle_ref_count
578 );
579 panic!(); // log to logcat with error!
580 }
581 *handle_ref_count = handle_ref_count.saturating_sub(count);
Victor Hsiehdd99b462021-12-02 17:36:15 -0800582 Ok(*unlinked && *handle_ref_count == 0)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800583 },
584 );
Victor Hsiehdd99b462021-12-02 17:36:15 -0800585
586 match delete_now {
587 Ok(true) => {
588 let _ = inode_table.remove(&inode).expect("Removed an existing entry");
589 }
590 Ok(false) => { /* Let the inode stay */ }
591 Err(e) => {
592 warn!(
593 "Unexpected failure when tries to forget an inode {} by refcount {}: {:?}",
594 inode, count, e
595 );
596 }
597 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800598 }
599
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800600 fn getattr(
601 &self,
602 _ctx: Context,
603 inode: Inode,
604 _handle: Option<Handle>,
605 ) -> io::Result<(libc::stat64, Duration)> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700606 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700607 Ok((
608 match config {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800609 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehf393a722021-12-08 13:04:27 -0800610 create_dir_stat(inode, dir.number_of_entries(), AccessMode::ReadOnly)
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800611 }
Victor Hsiehe8137e32022-02-11 22:14:12 +0000612 AuthFsEntry::UnverifiedReadonly { file_size, .. } => {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800613 create_stat(inode, *file_size, AccessMode::ReadOnly)
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700614 }
Victor Hsiehe8137e32022-02-11 22:14:12 +0000615 AuthFsEntry::VerifiedReadonly { reader } => {
616 create_stat(inode, reader.file_size()?, AccessMode::ReadOnly)
617 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800618 AuthFsEntry::VerifiedNew { editor, attr, .. } => {
619 create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))
Victor Hsieh45636232021-10-15 17:52:51 -0700620 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800621 AuthFsEntry::VerifiedNewDirectory { dir, attr } => create_dir_stat(
622 inode,
623 dir.number_of_entries(),
624 AccessMode::Variable(attr.mode()),
625 ),
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800626 }?,
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700627 DEFAULT_METADATA_TIMEOUT,
628 ))
629 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800630 }
631
632 fn open(
633 &self,
634 _ctx: Context,
635 inode: Self::Inode,
636 flags: u32,
Victor Hsieh43a751e2021-12-09 17:10:58 -0800637 ) -> io::Result<(Option<Self::Handle>, FuseOpenOptions)> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800638 // Since file handle is not really used in later operations (which use Inode directly),
Victor Hsieh09e26262021-03-03 16:00:55 -0800639 // return None as the handle.
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700640 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700641 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700642 AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700643 check_access_mode(flags, libc::O_RDONLY)?;
644 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700645 AuthFsEntry::VerifiedNew { .. } => {
Victor Hsiehf393a722021-12-08 13:04:27 -0800646 // TODO(victorhsieh): Imeplement ACL check using the attr and ctx. Always allow
647 // for now.
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700648 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800649 AuthFsEntry::ReadonlyDirectory { .. }
650 | AuthFsEntry::VerifiedNewDirectory { .. } => {
Victor Hsieh45636232021-10-15 17:52:51 -0700651 // TODO(victorhsieh): implement when needed.
652 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
653 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800654 }
Victor Hsieh45636232021-10-15 17:52:51 -0700655 // Always cache the file content. There is currently no need to support direct I/O or
656 // avoid the cache buffer. Memory mapping is only possible with cache enabled.
Victor Hsieh43a751e2021-12-09 17:10:58 -0800657 Ok((None, FuseOpenOptions::KEEP_CACHE))
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700658 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800659 }
660
Victor Hsieh45636232021-10-15 17:52:51 -0700661 fn create(
662 &self,
663 _ctx: Context,
664 parent: Self::Inode,
665 name: &CStr,
Victor Hsiehf393a722021-12-08 13:04:27 -0800666 mode: u32,
Victor Hsieh45636232021-10-15 17:52:51 -0700667 _flags: u32,
Victor Hsiehf393a722021-12-08 13:04:27 -0800668 umask: u32,
Victor Hsieh43a751e2021-12-09 17:10:58 -0800669 ) -> io::Result<(Entry, Option<Self::Handle>, FuseOpenOptions)> {
Victor Hsieh45636232021-10-15 17:52:51 -0700670 // TODO(205172873): handle O_TRUNC and O_EXCL properly.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800671 let new_inode = self.create_new_entry_with_ref_count(
672 parent,
673 name,
674 |parent_entry, basename, new_inode| match parent_entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800675 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800676 if dir.has_entry(basename) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800677 return Err(io::Error::from_raw_os_error(libc::EEXIST));
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800678 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800679 let mode = mode & !umask;
680 let (new_file, new_attr) = dir.create_file(basename, new_inode, mode)?;
681 Ok(AuthFsEntry::VerifiedNew { editor: new_file, attr: new_attr })
Victor Hsieh45636232021-10-15 17:52:51 -0700682 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800683 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
684 },
685 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700686
687 Ok((
688 Entry {
689 inode: new_inode,
690 generation: 0,
Victor Hsiehf393a722021-12-08 13:04:27 -0800691 attr: create_stat(new_inode, /* file_size */ 0, AccessMode::Variable(mode))?,
Victor Hsieh45636232021-10-15 17:52:51 -0700692 entry_timeout: DEFAULT_METADATA_TIMEOUT,
693 attr_timeout: DEFAULT_METADATA_TIMEOUT,
694 },
695 // See also `open`.
696 /* handle */ None,
Victor Hsieh43a751e2021-12-09 17:10:58 -0800697 FuseOpenOptions::KEEP_CACHE,
Victor Hsieh45636232021-10-15 17:52:51 -0700698 ))
699 }
700
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800701 fn read<W: io::Write + ZeroCopyWriter>(
702 &self,
703 _ctx: Context,
704 inode: Inode,
705 _handle: Handle,
706 w: W,
707 size: u32,
708 offset: u64,
709 _lock_owner: Option<u64>,
710 _flags: u32,
711 ) -> io::Result<usize> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700712 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700713 match config {
Victor Hsiehe8137e32022-02-11 22:14:12 +0000714 AuthFsEntry::VerifiedReadonly { reader } => {
715 read_chunks(w, reader, reader.file_size()?, offset, size)
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700716 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700717 AuthFsEntry::UnverifiedReadonly { reader, file_size } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700718 read_chunks(w, reader, *file_size, offset, size)
719 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800720 AuthFsEntry::VerifiedNew { editor, .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700721 // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to
722 // request a read even if the file is open with O_WRONLY.
723 read_chunks(w, editor, editor.size(), offset, size)
724 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800725 AuthFsEntry::ReadonlyDirectory { .. }
726 | AuthFsEntry::VerifiedNewDirectory { .. } => {
727 Err(io::Error::from_raw_os_error(libc::EISDIR))
728 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800729 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700730 })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800731 }
732
733 fn write<R: io::Read + ZeroCopyReader>(
734 &self,
735 _ctx: Context,
736 inode: Self::Inode,
737 _handle: Self::Handle,
738 mut r: R,
739 size: u32,
740 offset: u64,
741 _lock_owner: Option<u64>,
742 _delayed_write: bool,
743 _flags: u32,
744 ) -> io::Result<usize> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700745 self.handle_inode(&inode, |config| match config {
Victor Hsiehf393a722021-12-08 13:04:27 -0800746 AuthFsEntry::VerifiedNew { editor, .. } => {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800747 let mut buf = vec![0; size as usize];
748 r.read_exact(&mut buf)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700749 editor.write_at(&buf, offset)
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800750 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800751 AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => {
752 Err(io::Error::from_raw_os_error(libc::EPERM))
753 }
754 AuthFsEntry::ReadonlyDirectory { .. } | AuthFsEntry::VerifiedNewDirectory { .. } => {
755 Err(io::Error::from_raw_os_error(libc::EISDIR))
756 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700757 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800758 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700759
760 fn setattr(
761 &self,
762 _ctx: Context,
763 inode: Inode,
Victor Hsiehf393a722021-12-08 13:04:27 -0800764 in_attr: libc::stat64,
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700765 _handle: Option<Handle>,
766 valid: SetattrValid,
767 ) -> io::Result<(libc::stat64, Duration)> {
Victor Hsiehf393a722021-12-08 13:04:27 -0800768 let mut inode_table = self.inode_table.lock().unwrap();
769 handle_inode_mut_locked(&mut inode_table, &inode, |InodeState { entry, .. }| match entry {
770 AuthFsEntry::VerifiedNew { editor, attr } => {
771 check_unsupported_setattr_request(valid)?;
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700772
Victor Hsiehf393a722021-12-08 13:04:27 -0800773 // Initialize the default stat.
774 let mut new_attr =
775 create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))?;
776 // `valid` indicates what fields in `attr` are valid. Update to return correctly.
777 if valid.contains(SetattrValid::SIZE) {
778 // st_size is i64, but the cast should be safe since kernel should not give a
779 // negative size.
780 debug_assert!(in_attr.st_size >= 0);
781 new_attr.st_size = in_attr.st_size;
782 editor.resize(in_attr.st_size as u64)?;
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700783 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800784 if valid.contains(SetattrValid::MODE) {
785 attr.set_mode(in_attr.st_mode)?;
786 new_attr.st_mode = in_attr.st_mode;
787 }
788 Ok((new_attr, DEFAULT_METADATA_TIMEOUT))
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700789 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800790 AuthFsEntry::VerifiedNewDirectory { dir, attr } => {
791 check_unsupported_setattr_request(valid)?;
792 if valid.contains(SetattrValid::SIZE) {
793 return Err(io::Error::from_raw_os_error(libc::EISDIR));
794 }
795
796 // Initialize the default stat.
797 let mut new_attr = create_dir_stat(
798 inode,
799 dir.number_of_entries(),
800 AccessMode::Variable(attr.mode()),
801 )?;
802 if valid.contains(SetattrValid::MODE) {
803 attr.set_mode(in_attr.st_mode)?;
804 new_attr.st_mode = in_attr.st_mode;
805 }
806 Ok((new_attr, DEFAULT_METADATA_TIMEOUT))
807 }
808 _ => Err(io::Error::from_raw_os_error(libc::EPERM)),
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700809 })
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700810 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700811
812 fn getxattr(
813 &self,
814 _ctx: Context,
815 inode: Self::Inode,
816 name: &CStr,
817 size: u32,
818 ) -> io::Result<GetxattrReply> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700819 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700820 match config {
Victor Hsiehf393a722021-12-08 13:04:27 -0800821 AuthFsEntry::VerifiedNew { editor, .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700822 // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel
823 // change (see b/196635431). Until it's possible, use xattr to expose what we need
824 // as an authfs specific API.
825 if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() {
826 return Err(io::Error::from_raw_os_error(libc::ENODATA));
827 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700828
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700829 if size == 0 {
830 // Per protocol, when size is 0, return the value size.
831 Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32))
Victor Hsieh71f10032021-08-13 11:24:02 -0700832 } else {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700833 let digest = editor.calculate_fsverity_digest()?;
834 if digest.len() > size as usize {
835 Err(io::Error::from_raw_os_error(libc::ERANGE))
836 } else {
837 Ok(GetxattrReply::Value(digest.to_vec()))
838 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700839 }
840 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700841 _ => Err(io::Error::from_raw_os_error(libc::ENODATA)),
Victor Hsieh71f10032021-08-13 11:24:02 -0700842 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700843 })
Victor Hsieh71f10032021-08-13 11:24:02 -0700844 }
Victor Hsieh45636232021-10-15 17:52:51 -0700845
846 fn mkdir(
847 &self,
848 _ctx: Context,
849 parent: Self::Inode,
850 name: &CStr,
Victor Hsiehf393a722021-12-08 13:04:27 -0800851 mode: u32,
852 umask: u32,
Victor Hsieh45636232021-10-15 17:52:51 -0700853 ) -> io::Result<Entry> {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800854 let new_inode = self.create_new_entry_with_ref_count(
855 parent,
856 name,
857 |parent_entry, basename, new_inode| match parent_entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800858 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800859 if dir.has_entry(basename) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800860 return Err(io::Error::from_raw_os_error(libc::EEXIST));
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800861 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800862 let mode = mode & !umask;
863 let (new_dir, new_attr) = dir.mkdir(basename, new_inode, mode)?;
864 Ok(AuthFsEntry::VerifiedNewDirectory { dir: new_dir, attr: new_attr })
Victor Hsieh45636232021-10-15 17:52:51 -0700865 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800866 AuthFsEntry::ReadonlyDirectory { .. } => {
867 Err(io::Error::from_raw_os_error(libc::EACCES))
868 }
869 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
870 },
871 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700872
873 Ok(Entry {
874 inode: new_inode,
875 generation: 0,
Victor Hsiehf393a722021-12-08 13:04:27 -0800876 attr: create_dir_stat(new_inode, /* file_number */ 0, AccessMode::Variable(mode))?,
Victor Hsieh45636232021-10-15 17:52:51 -0700877 entry_timeout: DEFAULT_METADATA_TIMEOUT,
878 attr_timeout: DEFAULT_METADATA_TIMEOUT,
879 })
880 }
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800881
Victor Hsiehdd99b462021-12-02 17:36:15 -0800882 fn unlink(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> {
883 let mut inode_table = self.inode_table.lock().unwrap();
884 handle_inode_mut_locked(
885 &mut inode_table,
886 &parent,
887 |InodeState { entry, unlinked, .. }| match entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800888 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800889 let basename: &Path = cstr_to_path(name);
890 // Delete the file from in both the local and remote directories.
891 let _inode = dir.delete_file(basename)?;
892 *unlinked = true;
893 Ok(())
894 }
895 AuthFsEntry::ReadonlyDirectory { .. } => {
896 Err(io::Error::from_raw_os_error(libc::EACCES))
897 }
898 AuthFsEntry::VerifiedNew { .. } => {
899 // Deleting a entry in filesystem root is not currently supported.
900 Err(io::Error::from_raw_os_error(libc::ENOSYS))
901 }
902 AuthFsEntry::UnverifiedReadonly { .. } | AuthFsEntry::VerifiedReadonly { .. } => {
903 Err(io::Error::from_raw_os_error(libc::ENOTDIR))
904 }
905 },
906 )
907 }
908
909 fn rmdir(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> {
910 let mut inode_table = self.inode_table.lock().unwrap();
911
912 // Check before actual removal, with readonly borrow.
913 handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800914 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800915 let basename: &Path = cstr_to_path(name);
916 let existing_inode = dir.find_inode(basename)?;
917 handle_inode_locked(&inode_table, &existing_inode, |inode_state| {
Victor Hsiehf393a722021-12-08 13:04:27 -0800918 inode_state.entry.expect_empty_deletable_directory()
Victor Hsiehdd99b462021-12-02 17:36:15 -0800919 })
920 }
921 AuthFsEntry::ReadonlyDirectory { .. } => {
922 Err(io::Error::from_raw_os_error(libc::EACCES))
923 }
924 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
925 })?;
926
927 // Look up again, this time with mutable borrow. This needs to be done separately because
928 // the previous lookup needs to borrow multiple entry references in the table.
929 handle_inode_mut_locked(
930 &mut inode_table,
931 &parent,
932 |InodeState { entry, unlinked, .. }| match entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800933 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800934 let basename: &Path = cstr_to_path(name);
935 let _inode = dir.force_delete_directory(basename)?;
936 *unlinked = true;
937 Ok(())
938 }
939 _ => unreachable!("Mismatched entry type that is just checked"),
940 },
941 )
942 }
943
Victor Hsieh43a751e2021-12-09 17:10:58 -0800944 fn opendir(
945 &self,
946 _ctx: Context,
947 inode: Self::Inode,
948 _flags: u32,
949 ) -> io::Result<(Option<Self::Handle>, FuseOpenOptions)> {
950 let entries = self.handle_inode(&inode, |config| match config {
951 AuthFsEntry::VerifiedNewDirectory { dir, .. } => dir.retrieve_entries(),
952 AuthFsEntry::ReadonlyDirectory { dir } => dir.retrieve_entries(),
953 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
954 })?;
955 self.open_dir_store_snapshot(entries)
956 }
957
958 fn readdir(
959 &self,
960 _ctx: Context,
961 _inode: Self::Inode,
962 handle: Self::Handle,
963 _size: u32,
964 offset: u64,
965 ) -> io::Result<Self::DirIter> {
966 let dir_handle_table = self.dir_handle_table.lock().unwrap();
967 if let Some(entry) = dir_handle_table.get(&handle) {
968 Ok(DirEntriesSnapshotIterator {
969 snapshot: entry.clone(),
970 prev_offset: offset.try_into().unwrap(),
971 })
972 } else {
973 Err(io::Error::from_raw_os_error(libc::EBADF))
974 }
975 }
976
977 fn releasedir(
978 &self,
979 _ctx: Context,
980 inode: Self::Inode,
981 _flags: u32,
982 handle: Self::Handle,
983 ) -> io::Result<()> {
984 let mut dir_handle_table = self.dir_handle_table.lock().unwrap();
985 if dir_handle_table.remove(&handle).is_none() {
986 unreachable!("Unknown directory handle {}, inode {}", handle, inode);
987 }
988 Ok(())
989 }
990
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800991 fn statfs(&self, _ctx: Context, _inode: Self::Inode) -> io::Result<libc::statvfs64> {
992 let remote_stat = self.remote_fs_stats_reader.statfs()?;
993
994 // Safe because we are zero-initializing a struct with only POD fields. Not all fields
995 // matter to FUSE. See also:
996 // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/fuse/inode.c?h=v5.15#n460
997 let mut st: libc::statvfs64 = unsafe { zeroed() };
998
999 // Use the remote stat as a template, since it'd matter the most to consider the writable
1000 // files/directories that are written to the remote.
1001 st.f_bsize = remote_stat.block_size;
1002 st.f_frsize = remote_stat.fragment_size;
1003 st.f_blocks = remote_stat.block_numbers;
1004 st.f_bavail = remote_stat.block_available;
1005 st.f_favail = remote_stat.inodes_available;
1006 st.f_namemax = remote_stat.max_filename;
1007 // Assuming we are not privileged to use all free spaces on the remote server, set the free
1008 // blocks/fragment to the same available amount.
1009 st.f_bfree = st.f_bavail;
1010 st.f_ffree = st.f_favail;
1011 // Number of inodes on the filesystem
1012 st.f_files = self.inode_table.lock().unwrap().len() as u64;
1013
1014 Ok(st)
1015 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -08001016}
1017
Victor Hsieh3dccf702021-12-02 15:45:14 -08001018fn handle_inode_locked<F, R>(
1019 inode_table: &BTreeMap<Inode, InodeState>,
1020 inode: &Inode,
1021 handle_fn: F,
1022) -> io::Result<R>
1023where
1024 F: FnOnce(&InodeState) -> io::Result<R>,
1025{
1026 if let Some(inode_state) = inode_table.get(inode) {
1027 handle_fn(inode_state)
1028 } else {
1029 Err(io::Error::from_raw_os_error(libc::ENOENT))
1030 }
1031}
1032
1033fn handle_inode_mut_locked<F, R>(
1034 inode_table: &mut BTreeMap<Inode, InodeState>,
1035 inode: &Inode,
1036 handle_fn: F,
1037) -> io::Result<R>
1038where
1039 F: FnOnce(&mut InodeState) -> io::Result<R>,
1040{
1041 if let Some(inode_state) = inode_table.get_mut(inode) {
1042 handle_fn(inode_state)
1043 } else {
1044 Err(io::Error::from_raw_os_error(libc::ENOENT))
1045 }
1046}
1047
Victor Hsiehf393a722021-12-08 13:04:27 -08001048fn check_unsupported_setattr_request(valid: SetattrValid) -> io::Result<()> {
1049 if valid.contains(SetattrValid::UID) {
1050 warn!("Changing st_uid is not currently supported");
1051 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1052 }
1053 if valid.contains(SetattrValid::GID) {
1054 warn!("Changing st_gid is not currently supported");
1055 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1056 }
1057 if valid.intersects(
1058 SetattrValid::CTIME
1059 | SetattrValid::ATIME
1060 | SetattrValid::ATIME_NOW
1061 | SetattrValid::MTIME
1062 | SetattrValid::MTIME_NOW,
1063 ) {
1064 debug!("Ignoring ctime/atime/mtime change as authfs does not maintain timestamp currently");
1065 }
1066 Ok(())
1067}
1068
Victor Hsieh45636232021-10-15 17:52:51 -07001069fn cstr_to_path(cstr: &CStr) -> &Path {
1070 OsStr::from_bytes(cstr.to_bytes()).as_ref()
1071}