blob: 6fdcd624092608cc6ac89811180636707127dae8 [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 Hsieh3dccf702021-12-02 15:45:14 -080020use log::{debug, error, warn};
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080021use std::collections::{btree_map, BTreeMap};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080022use std::convert::TryFrom;
Victor Hsieh45636232021-10-15 17:52:51 -070023use std::ffi::{CStr, OsStr};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080024use std::io;
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080025use std::mem::{zeroed, MaybeUninit};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080026use std::option::Option;
Victor Hsieh79f296b2021-12-02 15:38:08 -080027use std::os::unix::ffi::OsStrExt;
Victor Hsiehd18b9752021-11-09 16:03:34 -080028use std::path::{Component, Path, PathBuf};
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -080029use std::sync::atomic::{AtomicU64, Ordering};
Victor Hsieh60c2f412021-11-03 13:02:19 -070030use std::sync::Mutex;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080031use std::time::Duration;
32
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080033use fuse::filesystem::{
Victor Hsieh71f10032021-08-13 11:24:02 -070034 Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply,
35 SetattrValid, ZeroCopyReader, ZeroCopyWriter,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080036};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080037
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 Hsiehd18b9752021-11-09 16:03:34 -080040 validate_basename, InMemoryDir, RandomWrite, ReadByChunk, RemoteDirEditor, RemoteFileEditor,
41 RemoteFileReader, RemoteMerkleTreeReader,
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 Hsieh26cea2f2021-11-03 10:28:33 -070052const DEFAULT_METADATA_TIMEOUT: Duration = Duration::from_secs(5);
53const ROOT_INODE: Inode = 1;
54
55/// `AuthFsEntry` defines the filesystem entry type supported by AuthFS.
56pub enum AuthFsEntry {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080057 /// A read-only directory (writable during initialization). Root directory is an example.
58 ReadonlyDirectory { dir: InMemoryDir },
Victor Hsieh1bcf4112021-03-19 14:26:57 -070059 /// A file type that is verified against fs-verity signature (thus read-only). The file is
Victor Hsieh1bcf4112021-03-19 14:26:57 -070060 /// served from a remote server.
Victor Hsieh88e50172021-10-15 13:27:13 -070061 VerifiedReadonly {
Victor Hsieh1bcf4112021-03-19 14:26:57 -070062 reader: VerifiedFileReader<RemoteFileReader, RemoteMerkleTreeReader>,
63 file_size: u64,
64 },
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080065 /// A file type that is a read-only passthrough from a file on a remote server.
Victor Hsieh88e50172021-10-15 13:27:13 -070066 UnverifiedReadonly { reader: RemoteFileReader, file_size: u64 },
Victor Hsieh1bcf4112021-03-19 14:26:57 -070067 /// A file type that is initially empty, and the content is stored on a remote server. File
68 /// integrity is guaranteed with private Merkle tree.
Victor Hsieh88e50172021-10-15 13:27:13 -070069 VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor> },
Victor Hsieh45636232021-10-15 17:52:51 -070070 /// A directory type that is initially empty. One can create new file (`VerifiedNew`) and new
71 /// directory (`VerifiedNewDirectory` itself) with integrity guaranteed within the VM.
72 VerifiedNewDirectory { dir: RemoteDirEditor },
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080073}
74
Victor Hsieh3dccf702021-12-02 15:45:14 -080075struct InodeState {
76 /// Actual inode entry.
77 entry: AuthFsEntry,
78
79 /// Number of `Handle`s (i.e. file descriptors) that are currently referring to the this inode.
80 ///
81 /// Technically, this does not matter to readonly entries, since they live forever. The
82 /// reference count is only needed for manageing lifetime of writable entries like `VerifiedNew`
83 /// and `VerifiedNewDirectory`. That is, when an entry is deleted, the actual entry needs to
84 /// stay alive until the reference count reaches zero.
85 ///
86 /// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement.
87 handle_ref_count: u64,
88}
89
90impl InodeState {
91 fn new(entry: AuthFsEntry) -> Self {
92 InodeState { entry, handle_ref_count: 0 }
93 }
94
95 fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self {
96 InodeState { entry, handle_ref_count }
97 }
98}
99
Victor Hsieh60c2f412021-11-03 13:02:19 -0700100// AuthFS needs to be `Sync` to be accepted by fuse::worker::start_message_loop as a `FileSystem`.
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800101pub struct AuthFs {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800102 /// Table for `Inode` to `InodeState` lookup. This needs to be `Sync` to be used in
Victor Hsieh60c2f412021-11-03 13:02:19 -0700103 /// `fuse::worker::start_message_loop`.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800104 inode_table: Mutex<BTreeMap<Inode, InodeState>>,
Victor Hsieh60c2f412021-11-03 13:02:19 -0700105
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800106 /// The next available inode number.
107 next_inode: AtomicU64,
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800108
109 /// A reader to access the remote filesystem stats, which is supposed to be of "the" output
110 /// directory. We assume all output are stored in the same partition.
111 remote_fs_stats_reader: RemoteFsStatsReader,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800112}
113
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800114// Implementation for preparing an `AuthFs` instance, before starting to serve.
115// TODO(victorhsieh): Consider implement a builder to separate the mutable initialization from the
116// immutable / interiorly mutable serving phase.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800117impl AuthFs {
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800118 pub fn new(remote_fs_stats_reader: RemoteFsStatsReader) -> AuthFs {
Victor Hsieh60c2f412021-11-03 13:02:19 -0700119 let mut inode_table = BTreeMap::new();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800120 inode_table.insert(
121 ROOT_INODE,
122 InodeState::new(AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }),
123 );
Victor Hsieh60c2f412021-11-03 13:02:19 -0700124
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800125 AuthFs {
126 inode_table: Mutex::new(inode_table),
127 next_inode: AtomicU64::new(ROOT_INODE + 1),
128 remote_fs_stats_reader,
129 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800130 }
131
Victor Hsiehd18b9752021-11-09 16:03:34 -0800132 /// Add an `AuthFsEntry` as `basename` to the filesystem root.
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800133 pub fn add_entry_at_root_dir(
134 &mut self,
135 basename: PathBuf,
136 entry: AuthFsEntry,
137 ) -> Result<Inode> {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800138 validate_basename(&basename)?;
139 self.add_entry_at_ro_dir_by_path(ROOT_INODE, &basename, entry)
140 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800141
Victor Hsiehd18b9752021-11-09 16:03:34 -0800142 /// Add an `AuthFsEntry` by path from the `ReadonlyDirectory` represented by `dir_inode`. The
143 /// path must be a related path. If some ancestor directories do not exist, they will be
144 /// created (also as `ReadonlyDirectory`) automatically.
145 pub fn add_entry_at_ro_dir_by_path(
146 &mut self,
147 dir_inode: Inode,
148 path: &Path,
149 entry: AuthFsEntry,
150 ) -> Result<Inode> {
151 // 1. Make sure the parent directories all exist. Derive the entry's parent inode.
152 let parent_path =
153 path.parent().ok_or_else(|| anyhow!("No parent directory: {:?}", path))?;
154 let parent_inode =
155 parent_path.components().try_fold(dir_inode, |current_dir_inode, path_component| {
156 match path_component {
157 Component::RootDir => bail!("Absolute path is not supported"),
158 Component::Normal(name) => {
159 let inode_table = self.inode_table.get_mut().unwrap();
160 // Locate the internal directory structure.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800161 let current_dir_entry = &mut inode_table
162 .get_mut(&current_dir_inode)
163 .ok_or_else(|| {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800164 anyhow!("Unknown directory inode {}", current_dir_inode)
Victor Hsieh3dccf702021-12-02 15:45:14 -0800165 })?
166 .entry;
Victor Hsiehd18b9752021-11-09 16:03:34 -0800167 let dir = match current_dir_entry {
168 AuthFsEntry::ReadonlyDirectory { dir } => dir,
169 _ => unreachable!("Not a ReadonlyDirectory"),
170 };
171 // Return directory inode. Create first if not exists.
172 if let Some(existing_inode) = dir.lookup_inode(name.as_ref()) {
173 Ok(existing_inode)
174 } else {
175 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
176 let new_dir_entry =
177 AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() };
178
179 // Actually update the tables.
180 dir.add_entry(name.as_ref(), new_inode)?;
Victor Hsieh3dccf702021-12-02 15:45:14 -0800181 if inode_table
182 .insert(new_inode, InodeState::new(new_dir_entry))
183 .is_some()
184 {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800185 bail!("Unexpected to find a duplicated inode");
186 }
187 Ok(new_inode)
188 }
189 }
190 _ => Err(anyhow!("Path is not canonical: {:?}", path)),
191 }
192 })?;
193
194 // 2. Insert the entry to the parent directory, as well as the inode table.
195 let inode_table = self.inode_table.get_mut().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800196 let inode_state = inode_table.get_mut(&parent_inode).expect("previously returned inode");
197 match &mut inode_state.entry {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800198 AuthFsEntry::ReadonlyDirectory { dir } => {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800199 let basename =
200 path.file_name().ok_or_else(|| anyhow!("Bad file name: {:?}", path))?;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800201 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
202
Victor Hsiehd18b9752021-11-09 16:03:34 -0800203 // Actually update the tables.
204 dir.add_entry(basename.as_ref(), new_inode)?;
Victor Hsieh3dccf702021-12-02 15:45:14 -0800205 if inode_table.insert(new_inode, InodeState::new(entry)).is_some() {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800206 bail!("Unexpected to find a duplicated inode");
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800207 }
208 Ok(new_inode)
209 }
Victor Hsiehd18b9752021-11-09 16:03:34 -0800210 _ => unreachable!("Not a ReadonlyDirectory"),
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800211 }
212 }
213}
214
215// Implementation for serving requests.
216impl AuthFs {
Victor Hsieh45636232021-10-15 17:52:51 -0700217 /// Handles the file associated with `inode` if found. This function returns whatever
218 /// `handle_fn` returns.
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700219 fn handle_inode<F, R>(&self, inode: &Inode, handle_fn: F) -> io::Result<R>
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700220 where
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700221 F: FnOnce(&AuthFsEntry) -> io::Result<R>,
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700222 {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700223 let inode_table = self.inode_table.lock().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800224 handle_inode_locked(&inode_table, inode, |inode_state| handle_fn(&inode_state.entry))
Victor Hsieh45636232021-10-15 17:52:51 -0700225 }
226
Victor Hsieh3dccf702021-12-02 15:45:14 -0800227 /// Adds a new entry `name` created by `create_fn` at `parent_inode`, with an initial ref count
228 /// of one.
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800229 ///
230 /// The operation involves two updates: adding the name with a new allocated inode to the
231 /// parent directory, and insert the new inode and the actual `AuthFsEntry` to the global inode
232 /// table.
233 ///
234 /// `create_fn` receives the parent directory, through which it can create the new entry at and
235 /// register the new inode to. Its returned entry is then added to the inode table.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800236 fn create_new_entry_with_ref_count<F>(
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800237 &self,
238 parent_inode: Inode,
239 name: &CStr,
240 create_fn: F,
241 ) -> io::Result<Inode>
Victor Hsieh45636232021-10-15 17:52:51 -0700242 where
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800243 F: FnOnce(&mut AuthFsEntry, &Path, Inode) -> io::Result<AuthFsEntry>,
Victor Hsieh45636232021-10-15 17:52:51 -0700244 {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700245 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsieh3dccf702021-12-02 15:45:14 -0800246 let (new_inode, new_file_entry) = handle_inode_mut_locked(
247 &mut inode_table,
248 &parent_inode,
249 |InodeState { entry, .. }| {
250 let new_inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
251 let basename: &Path = cstr_to_path(name);
252 let new_file_entry = create_fn(entry, basename, new_inode)?;
253 Ok((new_inode, new_file_entry))
254 },
255 )?;
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800256
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700257 if let btree_map::Entry::Vacant(entry) = inode_table.entry(new_inode) {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800258 entry.insert(InodeState::new_with_ref_count(new_file_entry, 1));
Victor Hsieh45636232021-10-15 17:52:51 -0700259 Ok(new_inode)
260 } else {
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800261 unreachable!("Unexpected duplication of inode {}", new_inode);
Victor Hsieh45636232021-10-15 17:52:51 -0700262 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800263 }
264}
265
266fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> {
267 if (flags & libc::O_ACCMODE as u32) == mode as u32 {
268 Ok(())
269 } else {
270 Err(io::Error::from_raw_os_error(libc::EACCES))
271 }
272}
273
274cfg_if::cfg_if! {
275 if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800276 fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800277 } else {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800278 fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800279 }
280}
281
Victor Hsieh45636232021-10-15 17:52:51 -0700282#[allow(clippy::enum_variant_names)]
283enum AccessMode {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800284 ReadOnly,
285 ReadWrite,
286}
287
Victor Hsieh45636232021-10-15 17:52:51 -0700288fn create_stat(
289 ino: libc::ino_t,
290 file_size: u64,
291 access_mode: AccessMode,
292) -> io::Result<libc::stat64> {
293 // SAFETY: stat64 is a plan C struct without pointer.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800294 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
295
296 st.st_ino = ino;
Victor Hsieh45636232021-10-15 17:52:51 -0700297 st.st_mode = match access_mode {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800298 // Until needed, let's just grant the owner access.
Victor Hsieh45636232021-10-15 17:52:51 -0700299 // TODO(205169366): Implement mode properly.
300 AccessMode::ReadOnly => libc::S_IFREG | libc::S_IRUSR,
301 AccessMode::ReadWrite => libc::S_IFREG | libc::S_IRUSR | libc::S_IWUSR,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800302 };
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800303 st.st_nlink = 1;
304 st.st_uid = 0;
305 st.st_gid = 0;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800306 st.st_size = libc::off64_t::try_from(file_size)
307 .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?;
308 st.st_blksize = blk_size();
309 // Per man stat(2), st_blocks is "Number of 512B blocks allocated".
310 st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512))
311 .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?;
312 Ok(st)
313}
314
Victor Hsieh45636232021-10-15 17:52:51 -0700315fn create_dir_stat(ino: libc::ino_t, file_number: u16) -> io::Result<libc::stat64> {
316 // SAFETY: stat64 is a plan C struct without pointer.
317 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
318
319 st.st_ino = ino;
320 // TODO(205169366): Implement mode properly.
321 st.st_mode = libc::S_IFDIR
322 | libc::S_IXUSR
323 | libc::S_IWUSR
324 | libc::S_IRUSR
325 | libc::S_IXGRP
326 | libc::S_IXOTH;
327
328 // 2 extra for . and ..
329 st.st_nlink = file_number
330 .checked_add(2)
331 .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))?
332 .into();
333
334 st.st_uid = 0;
335 st.st_gid = 0;
336 Ok(st)
337}
338
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800339fn offset_to_chunk_index(offset: u64) -> u64 {
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800340 offset / CHUNK_SIZE
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800341}
342
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700343fn read_chunks<W: io::Write, T: ReadByChunk>(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800344 mut w: W,
345 file: &T,
346 file_size: u64,
347 offset: u64,
348 size: u32,
349) -> io::Result<usize> {
350 let remaining = file_size.saturating_sub(offset);
351 let size_to_read = std::cmp::min(size as usize, remaining as usize);
Victor Hsiehac4f3f42021-02-26 12:35:58 -0800352 let total = ChunkedSizeIter::new(size_to_read, offset, CHUNK_SIZE as usize).try_fold(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800353 0,
354 |total, (current_offset, planned_data_size)| {
355 // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example,
356 // instead of accepting a buffer, the writer could expose the final destination buffer
357 // for the reader to write to. It might not be generally applicable though, e.g. with
358 // virtio transport, the buffer may not be continuous.
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800359 let mut buf = [0u8; CHUNK_SIZE as usize];
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800360 let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?;
361 if read_size < planned_data_size {
362 return Err(io::Error::from_raw_os_error(libc::ENODATA));
363 }
364
Victor Hsiehda3fbc42021-02-23 16:12:49 -0800365 let begin = (current_offset % CHUNK_SIZE) as usize;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800366 let end = begin + planned_data_size;
367 let s = w.write(&buf[begin..end])?;
368 if s != planned_data_size {
369 return Err(io::Error::from_raw_os_error(libc::EIO));
370 }
371 Ok(total + s)
372 },
373 )?;
374
375 Ok(total)
376}
377
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800378// TODO(205715172): Support enumerating directory entries.
379pub struct EmptyDirectoryIterator {}
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800380
381impl DirectoryIterator for EmptyDirectoryIterator {
382 fn next(&mut self) -> Option<DirEntry> {
383 None
384 }
385}
386
387impl FileSystem for AuthFs {
388 type Inode = Inode;
389 type Handle = Handle;
390 type DirIter = EmptyDirectoryIterator;
391
392 fn max_buffer_size(&self) -> u32 {
Victor Hsieh766e5332021-11-09 09:41:25 -0800393 MAX_WRITE_BYTES
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800394 }
395
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800396 fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> {
397 // Enable writeback cache for better performance especially since our bandwidth to the
398 // backend service is limited.
399 Ok(FsOptions::WRITEBACK_CACHE)
400 }
401
Victor Hsieh45636232021-10-15 17:52:51 -0700402 fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> {
Victor Hsieh3dccf702021-12-02 15:45:14 -0800403 let mut inode_table = self.inode_table.lock().unwrap();
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800404
Victor Hsieh3dccf702021-12-02 15:45:14 -0800405 // Look up the entry's inode number in parent directory.
406 let inode =
407 handle_inode_locked(&inode_table, &parent, |inode_state| match &inode_state.entry {
408 AuthFsEntry::ReadonlyDirectory { dir } => {
409 let path = cstr_to_path(name);
410 dir.lookup_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))
411 }
412 AuthFsEntry::VerifiedNewDirectory { dir } => {
413 let path = cstr_to_path(name);
414 dir.find_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))
415 }
416 _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
417 })?;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800418
419 // Create the entry's stat if found.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800420 let st = handle_inode_mut_locked(
421 &mut inode_table,
422 &inode,
423 |InodeState { entry, handle_ref_count, .. }| {
424 let st = match entry {
425 AuthFsEntry::ReadonlyDirectory { dir } => {
426 create_dir_stat(inode, dir.number_of_entries())
427 }
428 AuthFsEntry::UnverifiedReadonly { file_size, .. }
429 | AuthFsEntry::VerifiedReadonly { file_size, .. } => {
430 create_stat(inode, *file_size, AccessMode::ReadOnly)
431 }
432 AuthFsEntry::VerifiedNew { editor } => {
433 create_stat(inode, editor.size(), AccessMode::ReadWrite)
434 }
435 AuthFsEntry::VerifiedNewDirectory { dir } => {
436 create_dir_stat(inode, dir.number_of_entries())
437 }
438 }?;
439 *handle_ref_count += 1;
440 Ok(st)
441 },
442 )?;
443
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800444 Ok(Entry {
445 inode,
446 generation: 0,
447 attr: st,
448 entry_timeout: DEFAULT_METADATA_TIMEOUT,
449 attr_timeout: DEFAULT_METADATA_TIMEOUT,
450 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800451 }
452
Victor Hsieh3dccf702021-12-02 15:45:14 -0800453 fn forget(&self, _ctx: Context, inode: Self::Inode, count: u64) {
454 let mut inode_table = self.inode_table.lock().unwrap();
455 let _ = handle_inode_mut_locked(
456 &mut inode_table,
457 &inode,
458 |InodeState { handle_ref_count, .. }| {
459 if count > *handle_ref_count {
460 error!(
461 "Trying to decrease refcount of inode {} by {} (> current {})",
462 inode, count, *handle_ref_count
463 );
464 panic!(); // log to logcat with error!
465 }
466 *handle_ref_count = handle_ref_count.saturating_sub(count);
467 // TODO(208892249): Remove the inode with zero ref count from inode_table, if the
468 // inode is marked for deletion.
469 Ok(())
470 },
471 );
472 }
473
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800474 fn getattr(
475 &self,
476 _ctx: Context,
477 inode: Inode,
478 _handle: Option<Handle>,
479 ) -> io::Result<(libc::stat64, Duration)> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700480 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700481 Ok((
482 match config {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800483 AuthFsEntry::ReadonlyDirectory { dir } => {
484 create_dir_stat(inode, dir.number_of_entries())
485 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700486 AuthFsEntry::UnverifiedReadonly { file_size, .. }
487 | AuthFsEntry::VerifiedReadonly { file_size, .. } => {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800488 create_stat(inode, *file_size, AccessMode::ReadOnly)
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700489 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700490 AuthFsEntry::VerifiedNew { editor } => {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800491 create_stat(inode, editor.size(), AccessMode::ReadWrite)
Victor Hsieh45636232021-10-15 17:52:51 -0700492 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700493 AuthFsEntry::VerifiedNewDirectory { dir } => {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800494 create_dir_stat(inode, dir.number_of_entries())
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700495 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800496 }?,
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700497 DEFAULT_METADATA_TIMEOUT,
498 ))
499 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800500 }
501
502 fn open(
503 &self,
504 _ctx: Context,
505 inode: Self::Inode,
506 flags: u32,
507 ) -> io::Result<(Option<Self::Handle>, fuse::sys::OpenOptions)> {
508 // Since file handle is not really used in later operations (which use Inode directly),
Victor Hsieh09e26262021-03-03 16:00:55 -0800509 // return None as the handle.
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700510 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700511 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700512 AuthFsEntry::VerifiedReadonly { .. } | AuthFsEntry::UnverifiedReadonly { .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700513 check_access_mode(flags, libc::O_RDONLY)?;
514 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700515 AuthFsEntry::VerifiedNew { .. } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700516 // No need to check access modes since all the modes are allowed to the
517 // read-writable file.
518 }
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800519 AuthFsEntry::ReadonlyDirectory { .. }
520 | AuthFsEntry::VerifiedNewDirectory { .. } => {
Victor Hsieh45636232021-10-15 17:52:51 -0700521 // TODO(victorhsieh): implement when needed.
522 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
523 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800524 }
Victor Hsieh45636232021-10-15 17:52:51 -0700525 // Always cache the file content. There is currently no need to support direct I/O or
526 // avoid the cache buffer. Memory mapping is only possible with cache enabled.
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700527 Ok((None, fuse::sys::OpenOptions::KEEP_CACHE))
528 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800529 }
530
Victor Hsieh45636232021-10-15 17:52:51 -0700531 fn create(
532 &self,
533 _ctx: Context,
534 parent: Self::Inode,
535 name: &CStr,
536 _mode: u32,
537 _flags: u32,
538 _umask: u32,
539 ) -> io::Result<(Entry, Option<Self::Handle>, fuse::sys::OpenOptions)> {
540 // TODO(205169366): Implement mode properly.
541 // TODO(205172873): handle O_TRUNC and O_EXCL properly.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800542 let new_inode = self.create_new_entry_with_ref_count(
543 parent,
544 name,
545 |parent_entry, basename, new_inode| match parent_entry {
546 AuthFsEntry::VerifiedNewDirectory { dir } => {
547 if dir.find_inode(basename).is_some() {
548 return Err(io::Error::from_raw_os_error(libc::EEXIST));
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800549 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800550 let new_file = dir.create_file(basename, new_inode)?;
551 Ok(AuthFsEntry::VerifiedNew { editor: new_file })
Victor Hsieh45636232021-10-15 17:52:51 -0700552 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800553 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
554 },
555 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700556
557 Ok((
558 Entry {
559 inode: new_inode,
560 generation: 0,
561 attr: create_stat(new_inode, /* file_size */ 0, AccessMode::ReadWrite)?,
562 entry_timeout: DEFAULT_METADATA_TIMEOUT,
563 attr_timeout: DEFAULT_METADATA_TIMEOUT,
564 },
565 // See also `open`.
566 /* handle */ None,
567 fuse::sys::OpenOptions::KEEP_CACHE,
568 ))
569 }
570
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800571 fn read<W: io::Write + ZeroCopyWriter>(
572 &self,
573 _ctx: Context,
574 inode: Inode,
575 _handle: Handle,
576 w: W,
577 size: u32,
578 offset: u64,
579 _lock_owner: Option<u64>,
580 _flags: u32,
581 ) -> io::Result<usize> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700582 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700583 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700584 AuthFsEntry::VerifiedReadonly { reader, file_size } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700585 read_chunks(w, reader, *file_size, offset, size)
586 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700587 AuthFsEntry::UnverifiedReadonly { reader, file_size } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700588 read_chunks(w, reader, *file_size, offset, size)
589 }
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700590 AuthFsEntry::VerifiedNew { editor } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700591 // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to
592 // request a read even if the file is open with O_WRONLY.
593 read_chunks(w, editor, editor.size(), offset, size)
594 }
Victor Hsieh45636232021-10-15 17:52:51 -0700595 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800596 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700597 })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800598 }
599
600 fn write<R: io::Read + ZeroCopyReader>(
601 &self,
602 _ctx: Context,
603 inode: Self::Inode,
604 _handle: Self::Handle,
605 mut r: R,
606 size: u32,
607 offset: u64,
608 _lock_owner: Option<u64>,
609 _delayed_write: bool,
610 _flags: u32,
611 ) -> io::Result<usize> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700612 self.handle_inode(&inode, |config| match config {
613 AuthFsEntry::VerifiedNew { editor } => {
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800614 let mut buf = vec![0; size as usize];
615 r.read_exact(&mut buf)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700616 editor.write_at(&buf, offset)
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800617 }
618 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700619 })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800620 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700621
622 fn setattr(
623 &self,
624 _ctx: Context,
625 inode: Inode,
626 attr: libc::stat64,
627 _handle: Option<Handle>,
628 valid: SetattrValid,
629 ) -> io::Result<(libc::stat64, Duration)> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700630 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700631 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700632 AuthFsEntry::VerifiedNew { editor } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700633 // Initialize the default stat.
Victor Hsieh45636232021-10-15 17:52:51 -0700634 let mut new_attr = create_stat(inode, editor.size(), AccessMode::ReadWrite)?;
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700635 // `valid` indicates what fields in `attr` are valid. Update to return correctly.
636 if valid.contains(SetattrValid::SIZE) {
637 // st_size is i64, but the cast should be safe since kernel should not give a
638 // negative size.
639 debug_assert!(attr.st_size >= 0);
640 new_attr.st_size = attr.st_size;
641 editor.resize(attr.st_size as u64)?;
642 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700643
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700644 if valid.contains(SetattrValid::MODE) {
645 warn!("Changing st_mode is not currently supported");
646 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
647 }
648 if valid.contains(SetattrValid::UID) {
649 warn!("Changing st_uid is not currently supported");
650 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
651 }
652 if valid.contains(SetattrValid::GID) {
653 warn!("Changing st_gid is not currently supported");
654 return Err(io::Error::from_raw_os_error(libc::ENOSYS));
655 }
656 if valid.contains(SetattrValid::CTIME) {
657 debug!(
658 "Ignoring ctime change as authfs does not maintain timestamp currently"
659 );
660 }
661 if valid.intersects(SetattrValid::ATIME | SetattrValid::ATIME_NOW) {
662 debug!(
663 "Ignoring atime change as authfs does not maintain timestamp currently"
664 );
665 }
666 if valid.intersects(SetattrValid::MTIME | SetattrValid::MTIME_NOW) {
667 debug!(
668 "Ignoring mtime change as authfs does not maintain timestamp currently"
669 );
670 }
671 Ok((new_attr, DEFAULT_METADATA_TIMEOUT))
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700672 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700673 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700674 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700675 })
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700676 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700677
678 fn getxattr(
679 &self,
680 _ctx: Context,
681 inode: Self::Inode,
682 name: &CStr,
683 size: u32,
684 ) -> io::Result<GetxattrReply> {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700685 self.handle_inode(&inode, |config| {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700686 match config {
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700687 AuthFsEntry::VerifiedNew { editor } => {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700688 // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel
689 // change (see b/196635431). Until it's possible, use xattr to expose what we need
690 // as an authfs specific API.
691 if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() {
692 return Err(io::Error::from_raw_os_error(libc::ENODATA));
693 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700694
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700695 if size == 0 {
696 // Per protocol, when size is 0, return the value size.
697 Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32))
Victor Hsieh71f10032021-08-13 11:24:02 -0700698 } else {
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700699 let digest = editor.calculate_fsverity_digest()?;
700 if digest.len() > size as usize {
701 Err(io::Error::from_raw_os_error(libc::ERANGE))
702 } else {
703 Ok(GetxattrReply::Value(digest.to_vec()))
704 }
Victor Hsieh71f10032021-08-13 11:24:02 -0700705 }
706 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700707 _ => Err(io::Error::from_raw_os_error(libc::ENODATA)),
Victor Hsieh71f10032021-08-13 11:24:02 -0700708 }
Victor Hsiehc85e4ef2021-10-18 15:28:53 -0700709 })
Victor Hsieh71f10032021-08-13 11:24:02 -0700710 }
Victor Hsieh45636232021-10-15 17:52:51 -0700711
712 fn mkdir(
713 &self,
714 _ctx: Context,
715 parent: Self::Inode,
716 name: &CStr,
717 _mode: u32,
718 _umask: u32,
719 ) -> io::Result<Entry> {
720 // TODO(205169366): Implement mode properly.
Victor Hsieh3dccf702021-12-02 15:45:14 -0800721 let new_inode = self.create_new_entry_with_ref_count(
722 parent,
723 name,
724 |parent_entry, basename, new_inode| match parent_entry {
725 AuthFsEntry::VerifiedNewDirectory { dir } => {
726 if dir.find_inode(basename).is_some() {
727 return Err(io::Error::from_raw_os_error(libc::EEXIST));
Victor Hsiehd5a5b1e2021-11-09 11:42:34 -0800728 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800729 let new_dir = dir.mkdir(basename, new_inode)?;
730 Ok(AuthFsEntry::VerifiedNewDirectory { dir: new_dir })
Victor Hsieh45636232021-10-15 17:52:51 -0700731 }
Victor Hsieh3dccf702021-12-02 15:45:14 -0800732 AuthFsEntry::ReadonlyDirectory { .. } => {
733 Err(io::Error::from_raw_os_error(libc::EACCES))
734 }
735 _ => Err(io::Error::from_raw_os_error(libc::EBADF)),
736 },
737 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700738
739 Ok(Entry {
740 inode: new_inode,
741 generation: 0,
742 attr: create_dir_stat(new_inode, /* file_number */ 0)?,
743 entry_timeout: DEFAULT_METADATA_TIMEOUT,
744 attr_timeout: DEFAULT_METADATA_TIMEOUT,
745 })
746 }
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800747
748 fn statfs(&self, _ctx: Context, _inode: Self::Inode) -> io::Result<libc::statvfs64> {
749 let remote_stat = self.remote_fs_stats_reader.statfs()?;
750
751 // Safe because we are zero-initializing a struct with only POD fields. Not all fields
752 // matter to FUSE. See also:
753 // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/fuse/inode.c?h=v5.15#n460
754 let mut st: libc::statvfs64 = unsafe { zeroed() };
755
756 // Use the remote stat as a template, since it'd matter the most to consider the writable
757 // files/directories that are written to the remote.
758 st.f_bsize = remote_stat.block_size;
759 st.f_frsize = remote_stat.fragment_size;
760 st.f_blocks = remote_stat.block_numbers;
761 st.f_bavail = remote_stat.block_available;
762 st.f_favail = remote_stat.inodes_available;
763 st.f_namemax = remote_stat.max_filename;
764 // Assuming we are not privileged to use all free spaces on the remote server, set the free
765 // blocks/fragment to the same available amount.
766 st.f_bfree = st.f_bavail;
767 st.f_ffree = st.f_favail;
768 // Number of inodes on the filesystem
769 st.f_files = self.inode_table.lock().unwrap().len() as u64;
770
771 Ok(st)
772 }
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800773}
774
Victor Hsieh3dccf702021-12-02 15:45:14 -0800775fn handle_inode_locked<F, R>(
776 inode_table: &BTreeMap<Inode, InodeState>,
777 inode: &Inode,
778 handle_fn: F,
779) -> io::Result<R>
780where
781 F: FnOnce(&InodeState) -> io::Result<R>,
782{
783 if let Some(inode_state) = inode_table.get(inode) {
784 handle_fn(inode_state)
785 } else {
786 Err(io::Error::from_raw_os_error(libc::ENOENT))
787 }
788}
789
790fn handle_inode_mut_locked<F, R>(
791 inode_table: &mut BTreeMap<Inode, InodeState>,
792 inode: &Inode,
793 handle_fn: F,
794) -> io::Result<R>
795where
796 F: FnOnce(&mut InodeState) -> io::Result<R>,
797{
798 if let Some(inode_state) = inode_table.get_mut(inode) {
799 handle_fn(inode_state)
800 } else {
801 Err(io::Error::from_raw_os_error(libc::ENOENT))
802 }
803}
804
Victor Hsieh45636232021-10-15 17:52:51 -0700805fn cstr_to_path(cstr: &CStr) -> &Path {
806 OsStr::from_bytes(cstr.to_bytes()).as_ref()
807}