blob: 84129b64865ccf3e750067725a0822e1a32f43d5 [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 Hsieh79f296b2021-12-02 15:38:08 -080017mod mount;
18
Victor Hsiehd18b9752021-11-09 16:03:34 -080019use anyhow::{anyhow, bail, Result};
Victor Hsieh43a751e2021-12-09 17:10:58 -080020use fuse::filesystem::{
21 Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply,
22 SetattrValid, ZeroCopyReader, ZeroCopyWriter,
23};
24use fuse::sys::OpenOptions as FuseOpenOptions;
Victor Hsieh3dccf702021-12-02 15:45:14 -080025use log::{debug, error, warn};
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080026use std::collections::{btree_map, BTreeMap};
Victor Hsieh43a751e2021-12-09 17:10:58 -080027use std::convert::{TryFrom, TryInto};
28use std::ffi::{CStr, CString, OsStr};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080029use std::io;
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080030use std::mem::{zeroed, MaybeUninit};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080031use std::option::Option;
Victor Hsieh79f296b2021-12-02 15:38:08 -080032use std::os::unix::ffi::OsStrExt;
Victor Hsiehd18b9752021-11-09 16:03:34 -080033use std::path::{Component, Path, PathBuf};
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -080034use std::sync::atomic::{AtomicU64, Ordering};
Victor Hsieh43a751e2021-12-09 17:10:58 -080035use std::sync::{Arc, Mutex};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080036use std::time::Duration;
37
Victor Hsiehac4f3f42021-02-26 12:35:58 -080038use crate::common::{divide_roundup, ChunkedSizeIter, CHUNK_SIZE};
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080039use crate::file::{
Victor Hsieh35dfa1e2022-01-12 17:03:35 -080040 validate_basename, Attr, EagerChunkReader, InMemoryDir, RandomWrite, ReadByChunk,
41 RemoteDirEditor, RemoteFileEditor, RemoteFileReader,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080042};
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080043use crate::fsstat::RemoteFsStatsReader;
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080044use crate::fsverity::{VerifiedFileEditor, VerifiedFileReader};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080045
Victor Hsieh79f296b2021-12-02 15:38:08 -080046pub use self::mount::mount_and_enter_message_loop;
47use self::mount::MAX_WRITE_BYTES;
48
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080049pub type Inode = u64;
50type Handle = u64;
51
Victor Hsieh259dd9c2022-02-09 20:31:57 +000052/// Maximum time for a file's metadata to be cached by the kernel. Since any file and directory
53/// changes (if not read-only) has to go through AuthFS to be trusted, the timeout can be maximum.
54const DEFAULT_METADATA_TIMEOUT: Duration = Duration::MAX;
55
Victor Hsieh26cea2f2021-11-03 10:28:33 -070056const ROOT_INODE: Inode = 1;
57
58/// `AuthFsEntry` defines the filesystem entry type supported by AuthFS.
59pub enum AuthFsEntry {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080060 /// A read-only directory (writable during initialization). Root directory is an example.
61 ReadonlyDirectory { dir: InMemoryDir },
Victor Hsieh1bcf4112021-03-19 14:26:57 -070062 /// A file type that is verified against fs-verity signature (thus read-only). The file is
Victor Hsieh1bcf4112021-03-19 14:26:57 -070063 /// served from a remote server.
Victor Hsieh88e50172021-10-15 13:27:13 -070064 VerifiedReadonly {
Victor Hsieh35dfa1e2022-01-12 17:03:35 -080065 reader: VerifiedFileReader<RemoteFileReader, EagerChunkReader>,
Victor Hsieh1bcf4112021-03-19 14:26:57 -070066 file_size: u64,
67 },
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080068 /// A file type that is a read-only passthrough from a file on a remote server.
Victor Hsieh88e50172021-10-15 13:27:13 -070069 UnverifiedReadonly { reader: RemoteFileReader, file_size: u64 },
Victor Hsieh1bcf4112021-03-19 14:26:57 -070070 /// A file type that is initially empty, and the content is stored on a remote server. File
71 /// integrity is guaranteed with private Merkle tree.
Victor Hsiehf393a722021-12-08 13:04:27 -080072 VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor>, attr: Attr },
Victor Hsieh45636232021-10-15 17:52:51 -070073 /// A directory type that is initially empty. One can create new file (`VerifiedNew`) and new
74 /// directory (`VerifiedNewDirectory` itself) with integrity guaranteed within the VM.
Victor Hsiehf393a722021-12-08 13:04:27 -080075 VerifiedNewDirectory { dir: RemoteDirEditor, attr: Attr },
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080076}
77
Victor Hsiehdd99b462021-12-02 17:36:15 -080078impl AuthFsEntry {
Victor Hsiehf393a722021-12-08 13:04:27 -080079 fn expect_empty_deletable_directory(&self) -> io::Result<()> {
Victor Hsiehdd99b462021-12-02 17:36:15 -080080 match self {
Victor Hsiehf393a722021-12-08 13:04:27 -080081 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -080082 if dir.number_of_entries() == 0 {
83 Ok(())
84 } else {
85 Err(io::Error::from_raw_os_error(libc::ENOTEMPTY))
86 }
87 }
88 AuthFsEntry::ReadonlyDirectory { .. } => {
89 Err(io::Error::from_raw_os_error(libc::EACCES))
90 }
91 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
92 }
93 }
94}
95
Victor Hsieh3dccf702021-12-02 15:45:14 -080096struct InodeState {
97 /// Actual inode entry.
98 entry: AuthFsEntry,
99
100 /// Number of `Handle`s (i.e. file descriptors) that are currently referring to the this inode.
101 ///
102 /// Technically, this does not matter to readonly entries, since they live forever. The
103 /// reference count is only needed for manageing lifetime of writable entries like `VerifiedNew`
104 /// and `VerifiedNewDirectory`. That is, when an entry is deleted, the actual entry needs to
105 /// stay alive until the reference count reaches zero.
106 ///
107 /// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement.
108 handle_ref_count: u64,
Victor Hsiehdd99b462021-12-02 17:36:15 -0800109
110 /// Whether the inode is already unlinked, i.e. should be removed, once `handle_ref_count` is
111 /// down to zero.
112 unlinked: bool,
Victor Hsieh3dccf702021-12-02 15:45:14 -0800113}
114
115impl InodeState {
116 fn new(entry: AuthFsEntry) -> Self {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800117 InodeState { entry, handle_ref_count: 0, unlinked: false }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800118 }
119
120 fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800121 InodeState { entry, handle_ref_count, unlinked: false }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800122 }
123}
124
Victor Hsieh43a751e2021-12-09 17:10:58 -0800125/// Data type that a directory implementation should be able to present its entry to `AuthFs`.
126#[derive(Clone)]
127pub struct AuthFsDirEntry {
128 pub inode: Inode,
129 pub name: CString,
130 pub is_dir: bool,
131}
132
133/// A snapshot of a directory entries for supporting `readdir` operation.
134///
135/// The `readdir` implementation is required by FUSE to not return any entries that have been
136/// returned previously (while it's fine to not return new entries). Snapshot is the easiest way to
137/// be compliant. See `fuse::filesystem::readdir` for more details.
138///
139/// A `DirEntriesSnapshot` is created on `opendir`, and is associated with the returned
140/// `Handle`/FD. The snapshot is deleted when the handle is released in `releasedir`.
141type DirEntriesSnapshot = Vec<AuthFsDirEntry>;
142
143/// An iterator for reading from `DirEntriesSnapshot`.
144pub struct DirEntriesSnapshotIterator {
145 /// A reference to the `DirEntriesSnapshot` in `AuthFs`.
146 snapshot: Arc<DirEntriesSnapshot>,
147
148 /// A value determined by `Self` to identify the last entry. 0 is a reserved value by FUSE to
149 /// mean reading from the beginning.
150 prev_offset: usize,
151}
152
153impl<'a> DirectoryIterator for DirEntriesSnapshotIterator {
154 fn next(&mut self) -> Option<DirEntry> {
155 // This iterator should not be the only reference to the snapshot. The snapshot should
156 // still be hold in `dir_handle_table`, i.e. when the FD is not yet closed.
157 //
158 // This code is unreachable when `readdir` is called with a closed FD. Only when the FD is
159 // not yet closed, `DirEntriesSnapshotIterator` can be created (but still short-lived
160 // during `readdir`).
161 debug_assert!(Arc::strong_count(&self.snapshot) >= 2);
162
163 // Since 0 is reserved, let's use 1-based index for the offset. This allows us to
164 // resume from the previous read in the snapshot easily.
165 let current_offset = if self.prev_offset == 0 {
166 1 // first element in the vector
167 } else {
168 self.prev_offset + 1 // next element in the vector
169 };
170 if current_offset > self.snapshot.len() {
171 None
172 } else {
173 let AuthFsDirEntry { inode, name, is_dir } = &self.snapshot[current_offset - 1];
174 let entry = DirEntry {
175 offset: current_offset as u64,
176 ino: *inode,
177 name,
178 type_: if *is_dir { libc::DT_DIR.into() } else { libc::DT_REG.into() },
179 };
180 self.prev_offset = current_offset;
181 Some(entry)
182 }
183 }
184}
185
186type DirHandleTable = BTreeMap<Handle, Arc<DirEntriesSnapshot>>;
187
Victor Hsieh60c2f412021-11-03 13:02:19 -0700188// AuthFS needs to be `Sync` to be accepted by fuse::worker::start_message_loop as a `FileSystem`.
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800189pub struct AuthFs {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800190 /// Table for `Inode` to `InodeState` lookup. This needs to be `Sync` to be used in
Victor Hsieh60c2f412021-11-03 13:02:19 -0700191 /// `fuse::worker::start_message_loop`.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800192 inode_table: Mutex<BTreeMap<Inode, InodeState>>,
Victor Hsieh60c2f412021-11-03 13:02:19 -0700193
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800194 /// The next available inode number.
195 next_inode: AtomicU64,
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800196
Victor Hsieh43a751e2021-12-09 17:10:58 -0800197 /// Table for `Handle` to `Arc<DirEntriesSnapshot>` lookup. On `opendir`, a new directory handle
198 /// is created and the snapshot of the current directory is created. This is not super
199 /// efficient, but is the simplest way to be compliant to the FUSE contract (see
200 /// `fuse::filesystem::readdir`).
201 ///
202 /// Currently, no code locks `dir_handle_table` and `inode_table` at the same time to avoid
203 /// deadlock.
204 dir_handle_table: Mutex<DirHandleTable>,
205
206 /// The next available handle number.
207 next_handle: AtomicU64,
208
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800209 /// A reader to access the remote filesystem stats, which is supposed to be of "the" output
210 /// directory. We assume all output are stored in the same partition.
211 remote_fs_stats_reader: RemoteFsStatsReader,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800212}
213
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800214// Implementation for preparing an `AuthFs` instance, before starting to serve.
215// TODO(victorhsieh): Consider implement a builder to separate the mutable initialization from the
216// immutable / interiorly mutable serving phase.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800217impl AuthFs {
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800218 pub fn new(remote_fs_stats_reader: RemoteFsStatsReader) -> AuthFs {
Victor Hsieh60c2f412021-11-03 13:02:19 -0700219 let mut inode_table = BTreeMap::new();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800220 inode_table.insert(
221 ROOT_INODE,
222 InodeState::new(AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }),
223 );
Victor Hsieh60c2f412021-11-03 13:02:19 -0700224
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800225 AuthFs {
226 inode_table: Mutex::new(inode_table),
227 next_inode: AtomicU64::new(ROOT_INODE + 1),
Victor Hsieh43a751e2021-12-09 17:10:58 -0800228 dir_handle_table: Mutex::new(BTreeMap::new()),
229 next_handle: AtomicU64::new(1),
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800230 remote_fs_stats_reader,
231 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800232 }
233
Victor Hsiehd18b9752021-11-09 16:03:34 -0800234 /// Add an `AuthFsEntry` as `basename` to the filesystem root.
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800235 pub fn add_entry_at_root_dir(
236 &mut self,
237 basename: PathBuf,
238 entry: AuthFsEntry,
239 ) -> Result<Inode> {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800240 validate_basename(&basename)?;
241 self.add_entry_at_ro_dir_by_path(ROOT_INODE, &basename, entry)
242 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800243
Victor Hsiehd18b9752021-11-09 16:03:34 -0800244 /// Add an `AuthFsEntry` by path from the `ReadonlyDirectory` represented by `dir_inode`. The
245 /// path must be a related path. If some ancestor directories do not exist, they will be
246 /// created (also as `ReadonlyDirectory`) automatically.
247 pub fn add_entry_at_ro_dir_by_path(
248 &mut self,
249 dir_inode: Inode,
250 path: &Path,
251 entry: AuthFsEntry,
252 ) -> Result<Inode> {
253 // 1. Make sure the parent directories all exist. Derive the entry's parent inode.
254 let parent_path =
255 path.parent().ok_or_else(|| anyhow!("No parent directory: {:?}", path))?;
256 let parent_inode =
257 parent_path.components().try_fold(dir_inode, |current_dir_inode, path_component| {
258 match path_component {
259 Component::RootDir => bail!("Absolute path is not supported"),
260 Component::Normal(name) => {
261 let inode_table = self.inode_table.get_mut().unwrap();
262 // Locate the internal directory structure.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800263 let current_dir_entry = &mut inode_table
264 .get_mut(&current_dir_inode)
265 .ok_or_else(|| {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800266 anyhow!("Unknown directory inode {}", current_dir_inode)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800267 })?
268 .entry;
Victor Hsiehd18b9752021-11-09 16:03:34 -0800269 let dir = match current_dir_entry {
270 AuthFsEntry::ReadonlyDirectory { dir } => dir,
271 _ => unreachable!("Not a ReadonlyDirectory"),
272 };
273 // Return directory inode. Create first if not exists.
274 if let Some(existing_inode) = dir.lookup_inode(name.as_ref()) {
275 Ok(existing_inode)
276 } else {
277 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
278 let new_dir_entry =
279 AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() };
280
281 // Actually update the tables.
Victor Hsieh43a751e2021-12-09 17:10:58 -0800282 dir.add_dir(name.as_ref(), new_inode)?;
Victor Hsieh3dccf702021-12-02 15:45:14 -0800283 if inode_table
284 .insert(new_inode, InodeState::new(new_dir_entry))
285 .is_some()
286 {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800287 bail!("Unexpected to find a duplicated inode");
288 }
289 Ok(new_inode)
290 }
291 }
292 _ => Err(anyhow!("Path is not canonical: {:?}", path)),
293 }
294 })?;
295
296 // 2. Insert the entry to the parent directory, as well as the inode table.
297 let inode_table = self.inode_table.get_mut().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800298 let inode_state = inode_table.get_mut(&parent_inode).expect("previously returned inode");
299 match &mut inode_state.entry {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800300 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800301 let basename =
302 path.file_name().ok_or_else(|| anyhow!("Bad file name: {:?}", path))?;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800303 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
304
Victor Hsiehd18b9752021-11-09 16:03:34 -0800305 // Actually update the tables.
Victor Hsieh43a751e2021-12-09 17:10:58 -0800306 dir.add_file(basename.as_ref(), new_inode)?;
Victor Hsieh3dccf702021-12-02 15:45:14 -0800307 if inode_table.insert(new_inode, InodeState::new(entry)).is_some() {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800308 bail!("Unexpected to find a duplicated inode");
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800309 }
310 Ok(new_inode)
311 }
Victor Hsiehd18b9752021-11-09 16:03:34 -0800312 _ => unreachable!("Not a ReadonlyDirectory"),
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800313 }
314 }
315}
316
317// Implementation for serving requests.
318impl AuthFs {
Victor Hsieh45636232021-10-15 17:52:51 -0700319 /// Handles the file associated with `inode` if found. This function returns whatever
320 /// `handle_fn` returns.
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700321 fn handle_inode<F, R>(&self, inode: &Inode, handle_fn: F) -> io::Result<R>
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700322 where
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700323 F: FnOnce(&AuthFsEntry) -> io::Result<R>,
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700324 {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700325 let inode_table = self.inode_table.lock().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800326 handle_inode_locked(&inode_table, inode, |inode_state| handle_fn(&inode_state.entry))
Victor Hsieh45636232021-10-15 17:52:51 -0700327 }
328
Victor Hsieh3dccf702021-12-02 15:45:14 -0800329 /// Adds a new entry `name` created by `create_fn` at `parent_inode`, with an initial ref count
330 /// of one.
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800331 ///
332 /// The operation involves two updates: adding the name with a new allocated inode to the
333 /// parent directory, and insert the new inode and the actual `AuthFsEntry` to the global inode
334 /// table.
335 ///
336 /// `create_fn` receives the parent directory, through which it can create the new entry at and
337 /// register the new inode to. Its returned entry is then added to the inode table.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800338 fn create_new_entry_with_ref_count<F>(
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800339 &self,
340 parent_inode: Inode,
341 name: &CStr,
342 create_fn: F,
343 ) -> io::Result<Inode>
Victor Hsieh45636232021-10-15 17:52:51 -0700344 where
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800345 F: FnOnce(&mut AuthFsEntry, &Path, Inode) -> io::Result<AuthFsEntry>,
Victor Hsieh45636232021-10-15 17:52:51 -0700346 {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700347 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800348 let (new_inode, new_file_entry) = handle_inode_mut_locked(
349 &mut inode_table,
350 &parent_inode,
351 |InodeState { entry, .. }| {
352 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
353 let basename: &Path = cstr_to_path(name);
354 let new_file_entry = create_fn(entry, basename, new_inode)?;
355 Ok((new_inode, new_file_entry))
356 },
357 )?;
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800358
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700359 if let btree_map::Entry::Vacant(entry) = inode_table.entry(new_inode) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800360 entry.insert(InodeState::new_with_ref_count(new_file_entry, 1));
Victor Hsieh45636232021-10-15 17:52:51 -0700361 Ok(new_inode)
362 } else {
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800363 unreachable!("Unexpected duplication of inode {}", new_inode);
Victor Hsieh45636232021-10-15 17:52:51 -0700364 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800365 }
Victor Hsieh43a751e2021-12-09 17:10:58 -0800366
367 fn open_dir_store_snapshot(
368 &self,
369 dir_entries: Vec<AuthFsDirEntry>,
370 ) -> io::Result<(Option<Handle>, FuseOpenOptions)> {
371 let handle = self.next_handle.fetch_add(1, Ordering::Relaxed);
372 let mut dir_handle_table = self.dir_handle_table.lock().unwrap();
373 if let btree_map::Entry::Vacant(value) = dir_handle_table.entry(handle) {
374 value.insert(Arc::new(dir_entries));
375 Ok((Some(handle), FuseOpenOptions::empty()))
376 } else {
377 unreachable!("Unexpected to see new handle {} to existing in the table", handle);
378 }
379 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800380}
381
382fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> {
383 if (flags & libc::O_ACCMODE as u32) == mode as u32 {
384 Ok(())
385 } else {
386 Err(io::Error::from_raw_os_error(libc::EACCES))
387 }
388}
389
390cfg_if::cfg_if! {
391 if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800392 fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800393 } else {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800394 fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800395 }
396}
397
Victor Hsieh45636232021-10-15 17:52:51 -0700398#[allow(clippy::enum_variant_names)]
399enum AccessMode {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800400 ReadOnly,
Victor Hsiehf393a722021-12-08 13:04:27 -0800401 Variable(u32),
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800402}
403
Victor Hsieh45636232021-10-15 17:52:51 -0700404fn create_stat(
405 ino: libc::ino_t,
406 file_size: u64,
407 access_mode: AccessMode,
408) -> io::Result<libc::stat64> {
409 // SAFETY: stat64 is a plan C struct without pointer.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800410 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
411
412 st.st_ino = ino;
Victor Hsieh45636232021-10-15 17:52:51 -0700413 st.st_mode = match access_mode {
Victor Hsiehf393a722021-12-08 13:04:27 -0800414 AccessMode::ReadOnly => {
415 // Until needed, let's just grant the owner access.
416 libc::S_IFREG | libc::S_IRUSR
417 }
418 AccessMode::Variable(mode) => libc::S_IFREG | mode,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800419 };
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800420 st.st_nlink = 1;
421 st.st_uid = 0;
422 st.st_gid = 0;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800423 st.st_size = libc::off64_t::try_from(file_size)
424 .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?;
425 st.st_blksize = blk_size();
426 // Per man stat(2), st_blocks is "Number of 512B blocks allocated".
427 st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512))
428 .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?;
429 Ok(st)
430}
431
Victor Hsiehf393a722021-12-08 13:04:27 -0800432fn create_dir_stat(
433 ino: libc::ino_t,
434 file_number: u16,
435 access_mode: AccessMode,
436) -> io::Result<libc::stat64> {
Victor Hsieh45636232021-10-15 17:52:51 -0700437 // SAFETY: stat64 is a plan C struct without pointer.
438 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
439
440 st.st_ino = ino;
Victor Hsiehf393a722021-12-08 13:04:27 -0800441 st.st_mode = match access_mode {
442 AccessMode::ReadOnly => {
443 // Until needed, let's just grant the owner access and search to group and others.
444 libc::S_IFDIR | libc::S_IXUSR | libc::S_IRUSR | libc::S_IXGRP | libc::S_IXOTH
445 }
446 AccessMode::Variable(mode) => libc::S_IFDIR | mode,
447 };
Victor Hsieh45636232021-10-15 17:52:51 -0700448
449 // 2 extra for . and ..
450 st.st_nlink = file_number
451 .checked_add(2)
452 .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))?
453 .into();
454
455 st.st_uid = 0;
456 st.st_gid = 0;
457 Ok(st)
458}
459
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800460fn offset_to_chunk_index(offset: u64) -> u64 {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800461 offset / CHUNK_SIZE
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800462}
463
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700464fn read_chunks<W: io::Write, T: ReadByChunk>(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800465 mut w: W,
466 file: &T,
467 file_size: u64,
468 offset: u64,
469 size: u32,
470) -> io::Result<usize> {
471 let remaining = file_size.saturating_sub(offset);
472 let size_to_read = std::cmp::min(size as usize, remaining as usize);
Victor Hsiehac4f3f42021-02-26 12:35:58 -0800473 let total = ChunkedSizeIter::new(size_to_read, offset, CHUNK_SIZE as usize).try_fold(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800474 0,
475 |total, (current_offset, planned_data_size)| {
476 // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example,
477 // instead of accepting a buffer, the writer could expose the final destination buffer
478 // for the reader to write to. It might not be generally applicable though, e.g. with
479 // virtio transport, the buffer may not be continuous.
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800480 let mut buf = [0u8; CHUNK_SIZE as usize];
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800481 let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?;
482 if read_size < planned_data_size {
483 return Err(io::Error::from_raw_os_error(libc::ENODATA));
484 }
485
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800486 let begin = (current_offset % CHUNK_SIZE) as usize;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800487 let end = begin + planned_data_size;
488 let s = w.write(&buf[begin..end])?;
489 if s != planned_data_size {
490 return Err(io::Error::from_raw_os_error(libc::EIO));
491 }
492 Ok(total + s)
493 },
494 )?;
495
496 Ok(total)
497}
498
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800499impl FileSystem for AuthFs {
500 type Inode = Inode;
501 type Handle = Handle;
Victor Hsieh43a751e2021-12-09 17:10:58 -0800502 type DirIter = DirEntriesSnapshotIterator;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800503
504 fn max_buffer_size(&self) -> u32 {
Victor Hsieh766e5332021-11-09 09:41:25 -0800505 MAX_WRITE_BYTES
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800506 }
507
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800508 fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> {
509 // Enable writeback cache for better performance especially since our bandwidth to the
510 // backend service is limited.
511 Ok(FsOptions::WRITEBACK_CACHE)
512 }
513
Victor Hsieh45636232021-10-15 17:52:51 -0700514 fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800515 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800516
Victor Hsieh3dccf702021-12-02 15:45:14 -0800517 // Look up the entry's inode number in parent directory.
518 let inode =
519 handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry {
520 AuthFsEntry::ReadonlyDirectory { dir } => {
521 let path = cstr_to_path(name);
522 dir.lookup_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))
523 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800524 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800525 let path = cstr_to_path(name);
Victor Hsiehdd99b462021-12-02 17:36:15 -0800526 dir.find_inode(path)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800527 }
528 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
529 })?;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800530
531 // Create the entry's stat if found.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800532 let st = handle_inode_mut_locked(
533 &mut inode_table,
534 &inode,
535 |InodeState { entry, handle_ref_count, .. }| {
536 let st = match entry {
537 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehf393a722021-12-08 13:04:27 -0800538 create_dir_stat(inode, dir.number_of_entries(), AccessMode::ReadOnly)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800539 }
540 AuthFsEntry::UnverifiedReadonly { file_size, .. }
541 | AuthFsEntry::VerifiedReadonly { file_size, .. } => {
542 create_stat(inode, *file_size, AccessMode::ReadOnly)
543 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800544 AuthFsEntry::VerifiedNew { editor, attr, .. } => {
545 create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))
Victor Hsieh3dccf702021-12-02 15:45:14 -0800546 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800547 AuthFsEntry::VerifiedNewDirectory { dir, attr } => create_dir_stat(
548 inode,
549 dir.number_of_entries(),
550 AccessMode::Variable(attr.mode()),
551 ),
Victor Hsieh3dccf702021-12-02 15:45:14 -0800552 }?;
553 *handle_ref_count += 1;
554 Ok(st)
555 },
556 )?;
557
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800558 Ok(Entry {
559 inode,
560 generation: 0,
561 attr: st,
562 entry_timeout: DEFAULT_METADATA_TIMEOUT,
563 attr_timeout: DEFAULT_METADATA_TIMEOUT,
564 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800565 }
566
Victor Hsieh3dccf702021-12-02 15:45:14 -0800567 fn forget(&self, _ctx: Context, inode: Self::Inode, count: u64) {
568 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsiehdd99b462021-12-02 17:36:15 -0800569 let delete_now = handle_inode_mut_locked(
Victor Hsieh3dccf702021-12-02 15:45:14 -0800570 &mut inode_table,
571 &inode,
Victor Hsiehdd99b462021-12-02 17:36:15 -0800572 |InodeState { handle_ref_count, unlinked, .. }| {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800573 if count > *handle_ref_count {
574 error!(
575 "Trying to decrease refcount of inode {} by {} (> current {})",
576 inode, count, *handle_ref_count
577 );
578 panic!(); // log to logcat with error!
579 }
580 *handle_ref_count = handle_ref_count.saturating_sub(count);
Victor Hsiehdd99b462021-12-02 17:36:15 -0800581 Ok(*unlinked && *handle_ref_count == 0)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800582 },
583 );
Victor Hsiehdd99b462021-12-02 17:36:15 -0800584
585 match delete_now {
586 Ok(true) => {
587 let _ = inode_table.remove(&inode).expect("Removed an existing entry");
588 }
589 Ok(false) => { /* Let the inode stay */ }
590 Err(e) => {
591 warn!(
592 "Unexpected failure when tries to forget an inode {} by refcount {}: {:?}",
593 inode, count, e
594 );
595 }
596 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800597 }
598
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800599 fn getattr(
600 &self,
601 _ctx: Context,
602 inode: Inode,
603 _handle: Option<Handle>,
604 ) -> io::Result<(libc::stat64, Duration)> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700605 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700606 Ok((
607 match config {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800608 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehf393a722021-12-08 13:04:27 -0800609 create_dir_stat(inode, dir.number_of_entries(), AccessMode::ReadOnly)
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800610 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700611 AuthFsEntry::UnverifiedReadonly { file_size, .. }
612 | AuthFsEntry::VerifiedReadonly { 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 Hsiehf393a722021-12-08 13:04:27 -0800615 AuthFsEntry::VerifiedNew { editor, attr, .. } => {
616 create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))
Victor Hsieh45636232021-10-15 17:52:51 -0700617 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800618 AuthFsEntry::VerifiedNewDirectory { dir, attr } => create_dir_stat(
619 inode,
620 dir.number_of_entries(),
621 AccessMode::Variable(attr.mode()),
622 ),
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800623 }?,
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700624 DEFAULT_METADATA_TIMEOUT,
625 ))
626 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800627 }
628
629 fn open(
630 &self,
631 _ctx: Context,
632 inode: Self::Inode,
633 flags: u32,
Victor Hsieh43a751e2021-12-09 17:10:58 -0800634 ) -> io::Result<(Option<Self::Handle>, FuseOpenOptions)> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800635 // Since file handle is not really used in later operations (which use Inode directly),
Victor Hsieh09e26262021-03-03 16:00:55 -0800636 // return None as the handle.
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700637 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700638 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700639 AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700640 check_access_mode(flags, libc::O_RDONLY)?;
641 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700642 AuthFsEntry::VerifiedNew { .. } => {
Victor Hsiehf393a722021-12-08 13:04:27 -0800643 // TODO(victorhsieh): Imeplement ACL check using the attr and ctx. Always allow
644 // for now.
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700645 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800646 AuthFsEntry::ReadonlyDirectory { .. }
647 | AuthFsEntry::VerifiedNewDirectory { .. } => {
Victor Hsieh45636232021-10-15 17:52:51 -0700648 // TODO(victorhsieh): implement when needed.
649 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
650 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800651 }
Victor Hsieh45636232021-10-15 17:52:51 -0700652 // Always cache the file content. There is currently no need to support direct I/O or
653 // avoid the cache buffer. Memory mapping is only possible with cache enabled.
Victor Hsieh43a751e2021-12-09 17:10:58 -0800654 Ok((None, FuseOpenOptions::KEEP_CACHE))
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700655 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800656 }
657
Victor Hsieh45636232021-10-15 17:52:51 -0700658 fn create(
659 &self,
660 _ctx: Context,
661 parent: Self::Inode,
662 name: &CStr,
Victor Hsiehf393a722021-12-08 13:04:27 -0800663 mode: u32,
Victor Hsieh45636232021-10-15 17:52:51 -0700664 _flags: u32,
Victor Hsiehf393a722021-12-08 13:04:27 -0800665 umask: u32,
Victor Hsieh43a751e2021-12-09 17:10:58 -0800666 ) -> io::Result<(Entry, Option<Self::Handle>, FuseOpenOptions)> {
Victor Hsieh45636232021-10-15 17:52:51 -0700667 // TODO(205172873): handle O_TRUNC and O_EXCL properly.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800668 let new_inode = self.create_new_entry_with_ref_count(
669 parent,
670 name,
671 |parent_entry, basename, new_inode| match parent_entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800672 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800673 if dir.has_entry(basename) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800674 return Err(io::Error::from_raw_os_error(libc::EEXIST));
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800675 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800676 let mode = mode & !umask;
677 let (new_file, new_attr) = dir.create_file(basename, new_inode, mode)?;
678 Ok(AuthFsEntry::VerifiedNew { editor: new_file, attr: new_attr })
Victor Hsieh45636232021-10-15 17:52:51 -0700679 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800680 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
681 },
682 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700683
684 Ok((
685 Entry {
686 inode: new_inode,
687 generation: 0,
Victor Hsiehf393a722021-12-08 13:04:27 -0800688 attr: create_stat(new_inode, /* file_size */ 0, AccessMode::Variable(mode))?,
Victor Hsieh45636232021-10-15 17:52:51 -0700689 entry_timeout: DEFAULT_METADATA_TIMEOUT,
690 attr_timeout: DEFAULT_METADATA_TIMEOUT,
691 },
692 // See also `open`.
693 /* handle */ None,
Victor Hsieh43a751e2021-12-09 17:10:58 -0800694 FuseOpenOptions::KEEP_CACHE,
Victor Hsieh45636232021-10-15 17:52:51 -0700695 ))
696 }
697
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800698 fn read<W: io::Write + ZeroCopyWriter>(
699 &self,
700 _ctx: Context,
701 inode: Inode,
702 _handle: Handle,
703 w: W,
704 size: u32,
705 offset: u64,
706 _lock_owner: Option<u64>,
707 _flags: u32,
708 ) -> io::Result<usize> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700709 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700710 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700711 AuthFsEntry::VerifiedReadonly { reader, file_size } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700712 read_chunks(w, reader, *file_size, offset, size)
713 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700714 AuthFsEntry::UnverifiedReadonly { reader, file_size } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700715 read_chunks(w, reader, *file_size, offset, size)
716 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800717 AuthFsEntry::VerifiedNew { editor, .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700718 // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to
719 // request a read even if the file is open with O_WRONLY.
720 read_chunks(w, editor, editor.size(), offset, size)
721 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800722 AuthFsEntry::ReadonlyDirectory { .. }
723 | AuthFsEntry::VerifiedNewDirectory { .. } => {
724 Err(io::Error::from_raw_os_error(libc::EISDIR))
725 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800726 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700727 })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800728 }
729
730 fn write<R: io::Read + ZeroCopyReader>(
731 &self,
732 _ctx: Context,
733 inode: Self::Inode,
734 _handle: Self::Handle,
735 mut r: R,
736 size: u32,
737 offset: u64,
738 _lock_owner: Option<u64>,
739 _delayed_write: bool,
740 _flags: u32,
741 ) -> io::Result<usize> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700742 self.handle_inode(&inode, |config| match config {
Victor Hsiehf393a722021-12-08 13:04:27 -0800743 AuthFsEntry::VerifiedNew { editor, .. } => {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800744 let mut buf = vec![0; size as usize];
745 r.read_exact(&mut buf)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700746 editor.write_at(&buf, offset)
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800747 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800748 AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => {
749 Err(io::Error::from_raw_os_error(libc::EPERM))
750 }
751 AuthFsEntry::ReadonlyDirectory { .. } | AuthFsEntry::VerifiedNewDirectory { .. } => {
752 Err(io::Error::from_raw_os_error(libc::EISDIR))
753 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700754 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800755 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700756
757 fn setattr(
758 &self,
759 _ctx: Context,
760 inode: Inode,
Victor Hsiehf393a722021-12-08 13:04:27 -0800761 in_attr: libc::stat64,
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700762 _handle: Option<Handle>,
763 valid: SetattrValid,
764 ) -> io::Result<(libc::stat64, Duration)> {
Victor Hsiehf393a722021-12-08 13:04:27 -0800765 let mut inode_table = self.inode_table.lock().unwrap();
766 handle_inode_mut_locked(&mut inode_table, &inode, |InodeState { entry, .. }| match entry {
767 AuthFsEntry::VerifiedNew { editor, attr } => {
768 check_unsupported_setattr_request(valid)?;
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700769
Victor Hsiehf393a722021-12-08 13:04:27 -0800770 // Initialize the default stat.
771 let mut new_attr =
772 create_stat(inode, editor.size(), AccessMode::Variable(attr.mode()))?;
773 // `valid` indicates what fields in `attr` are valid. Update to return correctly.
774 if valid.contains(SetattrValid::SIZE) {
775 // st_size is i64, but the cast should be safe since kernel should not give a
776 // negative size.
777 debug_assert!(in_attr.st_size >= 0);
778 new_attr.st_size = in_attr.st_size;
779 editor.resize(in_attr.st_size as u64)?;
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700780 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800781 if valid.contains(SetattrValid::MODE) {
782 attr.set_mode(in_attr.st_mode)?;
783 new_attr.st_mode = in_attr.st_mode;
784 }
785 Ok((new_attr, DEFAULT_METADATA_TIMEOUT))
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700786 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800787 AuthFsEntry::VerifiedNewDirectory { dir, attr } => {
788 check_unsupported_setattr_request(valid)?;
789 if valid.contains(SetattrValid::SIZE) {
790 return Err(io::Error::from_raw_os_error(libc::EISDIR));
791 }
792
793 // Initialize the default stat.
794 let mut new_attr = create_dir_stat(
795 inode,
796 dir.number_of_entries(),
797 AccessMode::Variable(attr.mode()),
798 )?;
799 if valid.contains(SetattrValid::MODE) {
800 attr.set_mode(in_attr.st_mode)?;
801 new_attr.st_mode = in_attr.st_mode;
802 }
803 Ok((new_attr, DEFAULT_METADATA_TIMEOUT))
804 }
805 _ => Err(io::Error::from_raw_os_error(libc::EPERM)),
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700806 })
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700807 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700808
809 fn getxattr(
810 &self,
811 _ctx: Context,
812 inode: Self::Inode,
813 name: &CStr,
814 size: u32,
815 ) -> io::Result<GetxattrReply> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700816 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700817 match config {
Victor Hsiehf393a722021-12-08 13:04:27 -0800818 AuthFsEntry::VerifiedNew { editor, .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700819 // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel
820 // change (see b/196635431). Until it's possible, use xattr to expose what we need
821 // as an authfs specific API.
822 if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() {
823 return Err(io::Error::from_raw_os_error(libc::ENODATA));
824 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700825
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700826 if size == 0 {
827 // Per protocol, when size is 0, return the value size.
828 Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32))
Victor Hsieh71f10032021-08-13 11:24:02 -0700829 } else {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700830 let digest = editor.calculate_fsverity_digest()?;
831 if digest.len() > size as usize {
832 Err(io::Error::from_raw_os_error(libc::ERANGE))
833 } else {
834 Ok(GetxattrReply::Value(digest.to_vec()))
835 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700836 }
837 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700838 _ => Err(io::Error::from_raw_os_error(libc::ENODATA)),
Victor Hsieh71f10032021-08-13 11:24:02 -0700839 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700840 })
Victor Hsieh71f10032021-08-13 11:24:02 -0700841 }
Victor Hsieh45636232021-10-15 17:52:51 -0700842
843 fn mkdir(
844 &self,
845 _ctx: Context,
846 parent: Self::Inode,
847 name: &CStr,
Victor Hsiehf393a722021-12-08 13:04:27 -0800848 mode: u32,
849 umask: u32,
Victor Hsieh45636232021-10-15 17:52:51 -0700850 ) -> io::Result<Entry> {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800851 let new_inode = self.create_new_entry_with_ref_count(
852 parent,
853 name,
854 |parent_entry, basename, new_inode| match parent_entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800855 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800856 if dir.has_entry(basename) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800857 return Err(io::Error::from_raw_os_error(libc::EEXIST));
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800858 }
Victor Hsiehf393a722021-12-08 13:04:27 -0800859 let mode = mode & !umask;
860 let (new_dir, new_attr) = dir.mkdir(basename, new_inode, mode)?;
861 Ok(AuthFsEntry::VerifiedNewDirectory { dir: new_dir, attr: new_attr })
Victor Hsieh45636232021-10-15 17:52:51 -0700862 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800863 AuthFsEntry::ReadonlyDirectory { .. } => {
864 Err(io::Error::from_raw_os_error(libc::EACCES))
865 }
866 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
867 },
868 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700869
870 Ok(Entry {
871 inode: new_inode,
872 generation: 0,
Victor Hsiehf393a722021-12-08 13:04:27 -0800873 attr: create_dir_stat(new_inode, /* file_number */ 0, AccessMode::Variable(mode))?,
Victor Hsieh45636232021-10-15 17:52:51 -0700874 entry_timeout: DEFAULT_METADATA_TIMEOUT,
875 attr_timeout: DEFAULT_METADATA_TIMEOUT,
876 })
877 }
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800878
Victor Hsiehdd99b462021-12-02 17:36:15 -0800879 fn unlink(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> {
880 let mut inode_table = self.inode_table.lock().unwrap();
881 handle_inode_mut_locked(
882 &mut inode_table,
883 &parent,
884 |InodeState { entry, unlinked, .. }| match entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800885 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800886 let basename: &Path = cstr_to_path(name);
887 // Delete the file from in both the local and remote directories.
888 let _inode = dir.delete_file(basename)?;
889 *unlinked = true;
890 Ok(())
891 }
892 AuthFsEntry::ReadonlyDirectory { .. } => {
893 Err(io::Error::from_raw_os_error(libc::EACCES))
894 }
895 AuthFsEntry::VerifiedNew { .. } => {
896 // Deleting a entry in filesystem root is not currently supported.
897 Err(io::Error::from_raw_os_error(libc::ENOSYS))
898 }
899 AuthFsEntry::UnverifiedReadonly { .. } | AuthFsEntry::VerifiedReadonly { .. } => {
900 Err(io::Error::from_raw_os_error(libc::ENOTDIR))
901 }
902 },
903 )
904 }
905
906 fn rmdir(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<()> {
907 let mut inode_table = self.inode_table.lock().unwrap();
908
909 // Check before actual removal, with readonly borrow.
910 handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800911 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800912 let basename: &Path = cstr_to_path(name);
913 let existing_inode = dir.find_inode(basename)?;
914 handle_inode_locked(&inode_table, &existing_inode, |inode_state| {
Victor Hsiehf393a722021-12-08 13:04:27 -0800915 inode_state.entry.expect_empty_deletable_directory()
Victor Hsiehdd99b462021-12-02 17:36:15 -0800916 })
917 }
918 AuthFsEntry::ReadonlyDirectory { .. } => {
919 Err(io::Error::from_raw_os_error(libc::EACCES))
920 }
921 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
922 })?;
923
924 // Look up again, this time with mutable borrow. This needs to be done separately because
925 // the previous lookup needs to borrow multiple entry references in the table.
926 handle_inode_mut_locked(
927 &mut inode_table,
928 &parent,
929 |InodeState { entry, unlinked, .. }| match entry {
Victor Hsiehf393a722021-12-08 13:04:27 -0800930 AuthFsEntry::VerifiedNewDirectory { dir, .. } => {
Victor Hsiehdd99b462021-12-02 17:36:15 -0800931 let basename: &Path = cstr_to_path(name);
932 let _inode = dir.force_delete_directory(basename)?;
933 *unlinked = true;
934 Ok(())
935 }
936 _ => unreachable!("Mismatched entry type that is just checked"),
937 },
938 )
939 }
940
Victor Hsieh43a751e2021-12-09 17:10:58 -0800941 fn opendir(
942 &self,
943 _ctx: Context,
944 inode: Self::Inode,
945 _flags: u32,
946 ) -> io::Result<(Option<Self::Handle>, FuseOpenOptions)> {
947 let entries = self.handle_inode(&inode, |config| match config {
948 AuthFsEntry::VerifiedNewDirectory { dir, .. } => dir.retrieve_entries(),
949 AuthFsEntry::ReadonlyDirectory { dir } => dir.retrieve_entries(),
950 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
951 })?;
952 self.open_dir_store_snapshot(entries)
953 }
954
955 fn readdir(
956 &self,
957 _ctx: Context,
958 _inode: Self::Inode,
959 handle: Self::Handle,
960 _size: u32,
961 offset: u64,
962 ) -> io::Result<Self::DirIter> {
963 let dir_handle_table = self.dir_handle_table.lock().unwrap();
964 if let Some(entry) = dir_handle_table.get(&handle) {
965 Ok(DirEntriesSnapshotIterator {
966 snapshot: entry.clone(),
967 prev_offset: offset.try_into().unwrap(),
968 })
969 } else {
970 Err(io::Error::from_raw_os_error(libc::EBADF))
971 }
972 }
973
974 fn releasedir(
975 &self,
976 _ctx: Context,
977 inode: Self::Inode,
978 _flags: u32,
979 handle: Self::Handle,
980 ) -> io::Result<()> {
981 let mut dir_handle_table = self.dir_handle_table.lock().unwrap();
982 if dir_handle_table.remove(&handle).is_none() {
983 unreachable!("Unknown directory handle {}, inode {}", handle, inode);
984 }
985 Ok(())
986 }
987
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800988 fn statfs(&self, _ctx: Context, _inode: Self::Inode) -> io::Result<libc::statvfs64> {
989 let remote_stat = self.remote_fs_stats_reader.statfs()?;
990
991 // Safe because we are zero-initializing a struct with only POD fields. Not all fields
992 // matter to FUSE. See also:
993 // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/fuse/inode.c?h=v5.15#n460
994 let mut st: libc::statvfs64 = unsafe { zeroed() };
995
996 // Use the remote stat as a template, since it'd matter the most to consider the writable
997 // files/directories that are written to the remote.
998 st.f_bsize = remote_stat.block_size;
999 st.f_frsize = remote_stat.fragment_size;
1000 st.f_blocks = remote_stat.block_numbers;
1001 st.f_bavail = remote_stat.block_available;
1002 st.f_favail = remote_stat.inodes_available;
1003 st.f_namemax = remote_stat.max_filename;
1004 // Assuming we are not privileged to use all free spaces on the remote server, set the free
1005 // blocks/fragment to the same available amount.
1006 st.f_bfree = st.f_bavail;
1007 st.f_ffree = st.f_favail;
1008 // Number of inodes on the filesystem
1009 st.f_files = self.inode_table.lock().unwrap().len() as u64;
1010
1011 Ok(st)
1012 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -08001013}
1014
Victor Hsieh3dccf702021-12-02 15:45:14 -08001015fn handle_inode_locked<F, R>(
1016 inode_table: &BTreeMap<Inode, InodeState>,
1017 inode: &Inode,
1018 handle_fn: F,
1019) -> io::Result<R>
1020where
1021 F: FnOnce(&InodeState) -> io::Result<R>,
1022{
1023 if let Some(inode_state) = inode_table.get(inode) {
1024 handle_fn(inode_state)
1025 } else {
1026 Err(io::Error::from_raw_os_error(libc::ENOENT))
1027 }
1028}
1029
1030fn handle_inode_mut_locked<F, R>(
1031 inode_table: &mut BTreeMap<Inode, InodeState>,
1032 inode: &Inode,
1033 handle_fn: F,
1034) -> io::Result<R>
1035where
1036 F: FnOnce(&mut InodeState) -> io::Result<R>,
1037{
1038 if let Some(inode_state) = inode_table.get_mut(inode) {
1039 handle_fn(inode_state)
1040 } else {
1041 Err(io::Error::from_raw_os_error(libc::ENOENT))
1042 }
1043}
1044
Victor Hsiehf393a722021-12-08 13:04:27 -08001045fn check_unsupported_setattr_request(valid: SetattrValid) -> io::Result<()> {
1046 if valid.contains(SetattrValid::UID) {
1047 warn!("Changing st_uid is not currently supported");
1048 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1049 }
1050 if valid.contains(SetattrValid::GID) {
1051 warn!("Changing st_gid is not currently supported");
1052 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1053 }
1054 if valid.intersects(
1055 SetattrValid::CTIME
1056 | SetattrValid::ATIME
1057 | SetattrValid::ATIME_NOW
1058 | SetattrValid::MTIME
1059 | SetattrValid::MTIME_NOW,
1060 ) {
1061 debug!("Ignoring ctime/atime/mtime change as authfs does not maintain timestamp currently");
1062 }
1063 Ok(())
1064}
1065
Victor Hsieh45636232021-10-15 17:52:51 -07001066fn cstr_to_path(cstr: &CStr) -> &Path {
1067 OsStr::from_bytes(cstr.to_bytes()).as_ref()
1068}