Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | use anyhow::Result; |
| 18 | use std::collections::BTreeMap; |
| 19 | use std::convert::TryFrom; |
| 20 | use std::ffi::CStr; |
| 21 | use std::fs::OpenOptions; |
| 22 | use std::io; |
| 23 | use std::mem::MaybeUninit; |
| 24 | use std::option::Option; |
| 25 | use std::os::unix::io::AsRawFd; |
| 26 | use std::path::Path; |
| 27 | use std::time::Duration; |
| 28 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 29 | use fuse::filesystem::{ |
| 30 | Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, ZeroCopyReader, |
| 31 | ZeroCopyWriter, |
| 32 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 33 | use fuse::mount::MountOption; |
| 34 | |
Victor Hsieh | ac4f3f4 | 2021-02-26 12:35:58 -0800 | [diff] [blame] | 35 | use crate::common::{divide_roundup, ChunkedSizeIter, CHUNK_SIZE}; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 36 | use crate::file::{ |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 37 | LocalFileReader, RandomWrite, ReadByChunk, RemoteFileEditor, RemoteFileReader, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 38 | RemoteMerkleTreeReader, |
| 39 | }; |
| 40 | use crate::fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 41 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 42 | const DEFAULT_METADATA_TIMEOUT: std::time::Duration = Duration::from_secs(5); |
| 43 | |
| 44 | pub type Inode = u64; |
| 45 | type Handle = u64; |
| 46 | |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 47 | /// `FileConfig` defines the file type supported by AuthFS. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 48 | pub enum FileConfig { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 49 | /// A file type that is verified against fs-verity signature (thus read-only). The file is |
| 50 | /// backed by a local file. Debug only. |
| 51 | LocalVerifiedReadonlyFile { |
| 52 | reader: VerifiedFileReader<LocalFileReader, LocalFileReader>, |
| 53 | file_size: u64, |
| 54 | }, |
| 55 | /// A file type that is a read-only passthrough from a local file. Debug only. |
| 56 | LocalUnverifiedReadonlyFile { reader: LocalFileReader, file_size: u64 }, |
| 57 | /// A file type that is verified against fs-verity signature (thus read-only). The file is |
| 58 | /// served from a remote server. |
| 59 | RemoteVerifiedReadonlyFile { |
| 60 | reader: VerifiedFileReader<RemoteFileReader, RemoteMerkleTreeReader>, |
| 61 | file_size: u64, |
| 62 | }, |
| 63 | /// A file type that is a read-only passthrough from a file on a remote serrver. |
| 64 | RemoteUnverifiedReadonlyFile { reader: RemoteFileReader, file_size: u64 }, |
| 65 | /// A file type that is initially empty, and the content is stored on a remote server. File |
| 66 | /// integrity is guaranteed with private Merkle tree. |
| 67 | RemoteVerifiedNewFile { editor: VerifiedFileEditor<RemoteFileEditor> }, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | struct AuthFs { |
| 71 | /// Store `FileConfig`s using the `Inode` number as the search index. |
| 72 | /// |
| 73 | /// For further optimization to minimize the search cost, since Inode is integer, we may |
| 74 | /// consider storing them in a Vec if we can guarantee that the numbers are small and |
| 75 | /// consecutive. |
| 76 | file_pool: BTreeMap<Inode, FileConfig>, |
| 77 | |
| 78 | /// Maximum bytes in the write transaction to the FUSE device. This limits the maximum size to |
| 79 | /// a read request (including FUSE protocol overhead). |
| 80 | max_write: u32, |
| 81 | } |
| 82 | |
| 83 | impl AuthFs { |
| 84 | pub fn new(file_pool: BTreeMap<Inode, FileConfig>, max_write: u32) -> AuthFs { |
| 85 | AuthFs { file_pool, max_write } |
| 86 | } |
| 87 | |
| 88 | fn get_file_config(&self, inode: &Inode) -> io::Result<&FileConfig> { |
| 89 | self.file_pool.get(&inode).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> { |
| 94 | if (flags & libc::O_ACCMODE as u32) == mode as u32 { |
| 95 | Ok(()) |
| 96 | } else { |
| 97 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | cfg_if::cfg_if! { |
| 102 | if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 103 | fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 104 | } else { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 105 | fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 109 | enum FileMode { |
| 110 | ReadOnly, |
| 111 | ReadWrite, |
| 112 | } |
| 113 | |
| 114 | fn create_stat(ino: libc::ino_t, file_size: u64, file_mode: FileMode) -> io::Result<libc::stat64> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 115 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 116 | |
| 117 | st.st_ino = ino; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 118 | st.st_mode = match file_mode { |
| 119 | // Until needed, let's just grant the owner access. |
| 120 | FileMode::ReadOnly => libc::S_IFREG | libc::S_IRUSR, |
| 121 | FileMode::ReadWrite => libc::S_IFREG | libc::S_IRUSR | libc::S_IWUSR, |
| 122 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 123 | st.st_dev = 0; |
| 124 | st.st_nlink = 1; |
| 125 | st.st_uid = 0; |
| 126 | st.st_gid = 0; |
| 127 | st.st_rdev = 0; |
| 128 | st.st_size = libc::off64_t::try_from(file_size) |
| 129 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 130 | st.st_blksize = blk_size(); |
| 131 | // Per man stat(2), st_blocks is "Number of 512B blocks allocated". |
| 132 | st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512)) |
| 133 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 134 | Ok(st) |
| 135 | } |
| 136 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 137 | fn offset_to_chunk_index(offset: u64) -> u64 { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 138 | offset / CHUNK_SIZE |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 141 | fn read_chunks<W: io::Write, T: ReadByChunk>( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 142 | mut w: W, |
| 143 | file: &T, |
| 144 | file_size: u64, |
| 145 | offset: u64, |
| 146 | size: u32, |
| 147 | ) -> io::Result<usize> { |
| 148 | let remaining = file_size.saturating_sub(offset); |
| 149 | let size_to_read = std::cmp::min(size as usize, remaining as usize); |
Victor Hsieh | ac4f3f4 | 2021-02-26 12:35:58 -0800 | [diff] [blame] | 150 | let total = ChunkedSizeIter::new(size_to_read, offset, CHUNK_SIZE as usize).try_fold( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 151 | 0, |
| 152 | |total, (current_offset, planned_data_size)| { |
| 153 | // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example, |
| 154 | // instead of accepting a buffer, the writer could expose the final destination buffer |
| 155 | // for the reader to write to. It might not be generally applicable though, e.g. with |
| 156 | // virtio transport, the buffer may not be continuous. |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 157 | let mut buf = [0u8; CHUNK_SIZE as usize]; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 158 | let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?; |
| 159 | if read_size < planned_data_size { |
| 160 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 161 | } |
| 162 | |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 163 | let begin = (current_offset % CHUNK_SIZE) as usize; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 164 | let end = begin + planned_data_size; |
| 165 | let s = w.write(&buf[begin..end])?; |
| 166 | if s != planned_data_size { |
| 167 | return Err(io::Error::from_raw_os_error(libc::EIO)); |
| 168 | } |
| 169 | Ok(total + s) |
| 170 | }, |
| 171 | )?; |
| 172 | |
| 173 | Ok(total) |
| 174 | } |
| 175 | |
| 176 | // No need to support enumerating directory entries. |
| 177 | struct EmptyDirectoryIterator {} |
| 178 | |
| 179 | impl DirectoryIterator for EmptyDirectoryIterator { |
| 180 | fn next(&mut self) -> Option<DirEntry> { |
| 181 | None |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | impl FileSystem for AuthFs { |
| 186 | type Inode = Inode; |
| 187 | type Handle = Handle; |
| 188 | type DirIter = EmptyDirectoryIterator; |
| 189 | |
| 190 | fn max_buffer_size(&self) -> u32 { |
| 191 | self.max_write |
| 192 | } |
| 193 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 194 | fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> { |
| 195 | // Enable writeback cache for better performance especially since our bandwidth to the |
| 196 | // backend service is limited. |
| 197 | Ok(FsOptions::WRITEBACK_CACHE) |
| 198 | } |
| 199 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 200 | fn lookup(&self, _ctx: Context, _parent: Inode, name: &CStr) -> io::Result<Entry> { |
| 201 | // Only accept file name that looks like an integrer. Files in the pool are simply exposed |
| 202 | // by their inode number. Also, there is currently no directory structure. |
| 203 | let num = name.to_str().map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))?; |
| 204 | // Normally, `lookup` is required to increase a reference count for the inode (while |
| 205 | // `forget` will decrease it). It is not necessary here since the files are configured to |
| 206 | // be static. |
| 207 | let inode = num.parse::<Inode>().map_err(|_| io::Error::from_raw_os_error(libc::ENOENT))?; |
| 208 | let st = match self.get_file_config(&inode)? { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 209 | FileConfig::LocalVerifiedReadonlyFile { file_size, .. } |
| 210 | | FileConfig::LocalUnverifiedReadonlyFile { file_size, .. } |
| 211 | | FileConfig::RemoteUnverifiedReadonlyFile { file_size, .. } |
| 212 | | FileConfig::RemoteVerifiedReadonlyFile { file_size, .. } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 213 | create_stat(inode, *file_size, FileMode::ReadOnly)? |
| 214 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 215 | FileConfig::RemoteVerifiedNewFile { editor } => { |
| 216 | create_stat(inode, editor.size(), FileMode::ReadWrite)? |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 217 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 218 | }; |
| 219 | Ok(Entry { |
| 220 | inode, |
| 221 | generation: 0, |
| 222 | attr: st, |
| 223 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 224 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 225 | }) |
| 226 | } |
| 227 | |
| 228 | fn getattr( |
| 229 | &self, |
| 230 | _ctx: Context, |
| 231 | inode: Inode, |
| 232 | _handle: Option<Handle>, |
| 233 | ) -> io::Result<(libc::stat64, Duration)> { |
| 234 | Ok(( |
| 235 | match self.get_file_config(&inode)? { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 236 | FileConfig::LocalVerifiedReadonlyFile { file_size, .. } |
| 237 | | FileConfig::LocalUnverifiedReadonlyFile { file_size, .. } |
| 238 | | FileConfig::RemoteUnverifiedReadonlyFile { file_size, .. } |
| 239 | | FileConfig::RemoteVerifiedReadonlyFile { file_size, .. } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 240 | create_stat(inode, *file_size, FileMode::ReadOnly)? |
| 241 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 242 | FileConfig::RemoteVerifiedNewFile { editor } => { |
| 243 | create_stat(inode, editor.size(), FileMode::ReadWrite)? |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 244 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 245 | }, |
| 246 | DEFAULT_METADATA_TIMEOUT, |
| 247 | )) |
| 248 | } |
| 249 | |
| 250 | fn open( |
| 251 | &self, |
| 252 | _ctx: Context, |
| 253 | inode: Self::Inode, |
| 254 | flags: u32, |
| 255 | ) -> io::Result<(Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 256 | // Since file handle is not really used in later operations (which use Inode directly), |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 257 | // return None as the handle. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 258 | match self.get_file_config(&inode)? { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 259 | FileConfig::LocalVerifiedReadonlyFile { .. } |
Victor Hsieh | 8e161b5 | 2021-04-26 17:47:19 -0700 | [diff] [blame^] | 260 | | FileConfig::LocalUnverifiedReadonlyFile { .. } |
| 261 | | FileConfig::RemoteVerifiedReadonlyFile { .. } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 262 | | FileConfig::RemoteUnverifiedReadonlyFile { .. } => { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 263 | check_access_mode(flags, libc::O_RDONLY)?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 264 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 265 | FileConfig::RemoteVerifiedNewFile { .. } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 266 | // No need to check access modes since all the modes are allowed to the |
| 267 | // read-writable file. |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 268 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 269 | } |
Victor Hsieh | 8e161b5 | 2021-04-26 17:47:19 -0700 | [diff] [blame^] | 270 | // Always cache the file content. There is currently no need to support direct I/O or avoid |
| 271 | // the cache buffer. Memory mapping is only possible with cache enabled. |
| 272 | Ok((None, fuse::sys::OpenOptions::KEEP_CACHE)) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | fn read<W: io::Write + ZeroCopyWriter>( |
| 276 | &self, |
| 277 | _ctx: Context, |
| 278 | inode: Inode, |
| 279 | _handle: Handle, |
| 280 | w: W, |
| 281 | size: u32, |
| 282 | offset: u64, |
| 283 | _lock_owner: Option<u64>, |
| 284 | _flags: u32, |
| 285 | ) -> io::Result<usize> { |
| 286 | match self.get_file_config(&inode)? { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 287 | FileConfig::LocalVerifiedReadonlyFile { reader, file_size } => { |
| 288 | read_chunks(w, reader, *file_size, offset, size) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 289 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 290 | FileConfig::LocalUnverifiedReadonlyFile { reader, file_size } => { |
| 291 | read_chunks(w, reader, *file_size, offset, size) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 292 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 293 | FileConfig::RemoteVerifiedReadonlyFile { reader, file_size } => { |
| 294 | read_chunks(w, reader, *file_size, offset, size) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 295 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 296 | FileConfig::RemoteUnverifiedReadonlyFile { reader, file_size } => { |
| 297 | read_chunks(w, reader, *file_size, offset, size) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 298 | } |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 299 | FileConfig::RemoteVerifiedNewFile { editor } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 300 | // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to |
| 301 | // request a read even if the file is open with O_WRONLY. |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 302 | read_chunks(w, editor, editor.size(), offset, size) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | fn write<R: io::Read + ZeroCopyReader>( |
| 308 | &self, |
| 309 | _ctx: Context, |
| 310 | inode: Self::Inode, |
| 311 | _handle: Self::Handle, |
| 312 | mut r: R, |
| 313 | size: u32, |
| 314 | offset: u64, |
| 315 | _lock_owner: Option<u64>, |
| 316 | _delayed_write: bool, |
| 317 | _flags: u32, |
| 318 | ) -> io::Result<usize> { |
| 319 | match self.get_file_config(&inode)? { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 320 | FileConfig::RemoteVerifiedNewFile { editor } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 321 | let mut buf = vec![0; size as usize]; |
| 322 | r.read_exact(&mut buf)?; |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 323 | editor.write_at(&buf, offset) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 324 | } |
| 325 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /// Mount and start the FUSE instance. This requires CAP_SYS_ADMIN. |
| 331 | pub fn loop_forever( |
| 332 | file_pool: BTreeMap<Inode, FileConfig>, |
| 333 | mountpoint: &Path, |
| 334 | ) -> Result<(), fuse::Error> { |
| 335 | let max_read: u32 = 65536; |
| 336 | let max_write: u32 = 65536; |
| 337 | let dev_fuse = OpenOptions::new() |
| 338 | .read(true) |
| 339 | .write(true) |
| 340 | .open("/dev/fuse") |
| 341 | .expect("Failed to open /dev/fuse"); |
| 342 | |
| 343 | fuse::mount( |
| 344 | mountpoint, |
| 345 | "authfs", |
| 346 | libc::MS_NOSUID | libc::MS_NODEV, |
| 347 | &[ |
| 348 | MountOption::FD(dev_fuse.as_raw_fd()), |
| 349 | MountOption::RootMode(libc::S_IFDIR | libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH), |
| 350 | MountOption::AllowOther, |
| 351 | MountOption::UserId(0), |
| 352 | MountOption::GroupId(0), |
| 353 | MountOption::MaxRead(max_read), |
| 354 | ], |
| 355 | ) |
| 356 | .expect("Failed to mount fuse"); |
| 357 | |
| 358 | fuse::worker::start_message_loop( |
| 359 | dev_fuse, |
| 360 | max_write, |
| 361 | max_read, |
| 362 | AuthFs::new(file_pool, max_write), |
| 363 | ) |
| 364 | } |