Skip to main content

starnix_modules_fuse/
lib.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![recursion_limit = "512"]
6
7use fuchsia_rcu::RcuReadScope;
8use linux_uapi::FUSE_DEV_IOC_PASSTHROUGH_OPEN_V2;
9use starnix_core::mm::{MemoryAccessorExt, PAGE_SIZE};
10use starnix_core::mutable_state::Guard;
11use starnix_core::security;
12use starnix_core::task::waiter::WaiterOptions;
13use starnix_core::task::{CurrentTask, EventHandler, Kernel, WaitCanceler, WaitQueue, Waiter};
14use starnix_core::vfs::buffers::{
15    Buffer, InputBuffer, InputBufferExt as _, OutputBuffer, OutputBufferCallback,
16};
17use starnix_core::vfs::pseudo::dynamic_file::{DynamicFile, DynamicFileBuf, DynamicFileSource};
18use starnix_core::vfs::pseudo::simple_directory::SimpleDirectory;
19use starnix_core::vfs::pseudo::simple_file::SimpleFileNode;
20use starnix_core::vfs::pseudo::vec_directory::{VecDirectory, VecDirectoryEntry};
21use starnix_core::vfs::{
22    AppendLockWriteGuard, CacheMode, CheckAccessReason, DirEntry, DirEntryOps, DirectoryEntryType,
23    DirentSink, FallocMode, FdNumber, FileObject, FileObjectState, FileOps, FileSystem,
24    FileSystemHandle, FileSystemOps, FileSystemOptions, FsLockDepType, FsNode, FsNodeFlags,
25    FsNodeHandle, FsNodeInfo, FsNodeOps, FsStr, FsString, NamespaceNode,
26    PeekBufferSegmentsCallback, RenameContext, SeekTarget, SymlinkTarget, ValueOrSize,
27    WeakFileHandle, XattrOp, default_eof_offset, default_fcntl, default_ioctl, default_seek,
28    fileops_impl_nonseekable, fileops_impl_noop_sync, fs_args, fs_node_impl_dir_readonly,
29};
30use starnix_lifecycle::AtomicCounter;
31use starnix_logging::{log_error, log_trace, log_warn, track_stub};
32use starnix_sync::{
33    AtomicMonotonicInstant, DynamicLockDepRwLock, FileOpsCore, LockDepReadGuard, LockDepWriteGuard,
34    LockEqualOrBefore, Locked, Mutex, MutexGuard, Unlocked,
35};
36use starnix_syscalls::{SyscallArg, SyscallResult};
37use starnix_types::time::{NANOS_PER_SECOND, duration_from_timespec, time_from_timespec};
38use starnix_types::vfs::default_statfs;
39use starnix_uapi::auth::FsCred;
40use starnix_uapi::device_id::DeviceId;
41use starnix_uapi::errors::{EINTR, EINVAL, ENOENT, ENOSYS, Errno};
42use starnix_uapi::file_mode::{Access, FileMode};
43use starnix_uapi::math::round_up_to_increment;
44use starnix_uapi::open_flags::OpenFlags;
45use starnix_uapi::vfs::FdEvents;
46use starnix_uapi::{
47    FUSE_SUPER_MAGIC, errno, errno_from_code, error, ino_t, mode, off_t, statfs, uapi,
48};
49use std::collections::hash_map::Entry;
50use std::collections::{HashMap, VecDeque};
51use std::ops::{Deref, DerefMut};
52use std::sync::atomic::{AtomicBool, Ordering};
53use std::sync::{Arc, Weak};
54use syncio::zxio_node_attr_has_t;
55use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
56
57const FUSE_ROOT_ID_U64: u64 = uapi::FUSE_ROOT_ID as u64;
58const CONFIGURATION_AVAILABLE_EVENT: u64 = std::u64::MAX;
59
60uapi::check_arch_independent_layout! {
61    fuse_access_in {}
62    fuse_attr {}
63    fuse_attr_out {}
64    fuse_create_in {}
65    fuse_dirent {}
66    fuse_entry_bpf_out {}
67    fuse_entry_out {}
68    fuse_flush_in {}
69    fuse_forget_in {}
70    fuse_getxattr_in {}
71    fuse_getxattr_out {}
72    fuse_in_header {}
73    fuse_init_in {}
74    fuse_init_out {}
75    fuse_interrupt_in {}
76    fuse_link_in {}
77    fuse_lseek_in {}
78    fuse_lseek_out {}
79    fuse_mkdir_in {}
80    fuse_mknod_in {}
81    fuse_opcode {}
82    fuse_open_in {}
83    fuse_open_out {}
84    fuse_out_header {}
85    fuse_poll_in {}
86    fuse_poll_out {}
87    fuse_read_in {}
88    fuse_release_in {}
89    fuse_rename2_in {}
90    fuse_setattr_in {}
91    fuse_setxattr_in {}
92    fuse_statfs_out {}
93    fuse_write_in {}
94    fuse_write_out {}
95}
96
97#[derive(Debug)]
98struct DevFuse {
99    connection: Arc<FuseConnection>,
100}
101
102pub fn open_fuse_device(
103    _locked: &mut Locked<FileOpsCore>,
104    current_task: &CurrentTask,
105    _id: DeviceId,
106    _node: &NamespaceNode,
107    _flags: OpenFlags,
108) -> Result<Box<dyn FileOps>, Errno> {
109    let connection = fuse_connections(current_task.kernel()).new_connection(current_task);
110    Ok(Box::new(DevFuse { connection }))
111}
112
113fn attr_valid_to_duration(
114    attr_valid: u64,
115    attr_valid_nsec: u32,
116) -> Result<zx::MonotonicDuration, Errno> {
117    duration_from_timespec(uapi::timespec {
118        tv_sec: i64::try_from(attr_valid).unwrap_or(std::i64::MAX),
119        tv_nsec: attr_valid_nsec.into(),
120    })
121}
122
123impl FileOps for DevFuse {
124    fileops_impl_nonseekable!();
125    fileops_impl_noop_sync!();
126
127    fn close(
128        self: Box<Self>,
129        _locked: &mut Locked<FileOpsCore>,
130        _file: &FileObjectState,
131        _current_task: &CurrentTask,
132    ) {
133        self.connection.lock().disconnect();
134    }
135
136    fn read(
137        &self,
138        locked: &mut Locked<FileOpsCore>,
139        file: &FileObject,
140        current_task: &CurrentTask,
141        offset: usize,
142        data: &mut dyn OutputBuffer,
143    ) -> Result<usize, Errno> {
144        debug_assert!(offset == 0);
145        file.blocking_op(locked, current_task, FdEvents::POLLIN, None, |_| {
146            self.connection.lock().read(data)
147        })
148    }
149
150    fn write(
151        &self,
152        _locked: &mut Locked<FileOpsCore>,
153        _file: &FileObject,
154        _current_task: &CurrentTask,
155        offset: usize,
156        data: &mut dyn InputBuffer,
157    ) -> Result<usize, Errno> {
158        debug_assert!(offset == 0);
159        self.connection.lock().write(data)
160    }
161
162    fn wait_async(
163        &self,
164        _locked: &mut Locked<FileOpsCore>,
165        _file: &FileObject,
166        _current_task: &CurrentTask,
167        waiter: &Waiter,
168        events: FdEvents,
169        handler: EventHandler,
170    ) -> Option<WaitCanceler> {
171        self.connection.lock().wait_async(waiter, events, handler)
172    }
173
174    fn query_events(
175        &self,
176        _locked: &mut Locked<FileOpsCore>,
177        _file: &FileObject,
178        _current_task: &CurrentTask,
179    ) -> Result<FdEvents, Errno> {
180        Ok(self.connection.lock().query_events())
181    }
182
183    fn ioctl(
184        &self,
185        locked: &mut Locked<Unlocked>,
186        file: &FileObject,
187        current_task: &CurrentTask,
188        request: u32,
189        arg: SyscallArg,
190    ) -> Result<SyscallResult, Errno> {
191        match request {
192            FUSE_DEV_IOC_PASSTHROUGH_OPEN_V2 => {
193                let fd = current_task.read_object::<FdNumber>(arg.into())?;
194                let fd = current_task.get_file(fd)?;
195                let id = {
196                    let mut connection = self.connection.lock();
197                    let (mut id, _) = connection.last_passthrough_id.overflowing_add(1);
198                    let mut entry = connection.registered_passthrough.entry(id);
199                    while id == 0 || matches!(entry, Entry::Occupied(_)) {
200                        let (new_id, _) = id.overflowing_add(1);
201                        id = new_id;
202                        entry = connection.registered_passthrough.entry(id);
203                    }
204                    entry.or_insert_with(|| Arc::downgrade(&fd));
205                    connection.last_passthrough_id = id;
206                    id
207                };
208                Ok(id.into())
209            }
210            _ => default_ioctl(file, locked, current_task, request, arg),
211        }
212    }
213}
214
215pub fn new_fuse_fs(
216    locked: &mut Locked<Unlocked>,
217    current_task: &CurrentTask,
218    options: FileSystemOptions,
219) -> Result<FileSystemHandle, Errno> {
220    let fd = fs_args::parse::<FdNumber>(
221        options.params.get(b"fd").ok_or_else(|| errno!(EINVAL))?.as_ref(),
222    )?;
223    let default_permissions = options.params.get(b"default_permissions").is_some().into();
224    let connection = current_task
225        .running_state()
226        .files
227        .get(fd)?
228        .downcast_file::<DevFuse>()
229        .ok_or_else(|| errno!(EINVAL))?
230        .connection
231        .clone();
232
233    let fs = FileSystem::new(
234        locked,
235        current_task.kernel(),
236        CacheMode::Cached(current_task.kernel().fs_cache_config()),
237        FuseFs { connection: connection.clone(), default_permissions },
238        options,
239    )?;
240    let fuse_node = FuseNode::new(connection.clone(), FUSE_ROOT_ID_U64, 0);
241    fuse_node.state.lock().nlookup += 1;
242
243    fs.create_root(FUSE_ROOT_ID_U64, fuse_node);
244
245    {
246        let mut state = connection.lock();
247        state.connect();
248        state.execute_operation(
249            locked,
250            current_task,
251            FuseNode::from_node(&fs.root().node),
252            FuseOperation::Init { fs: Arc::downgrade(&fs) },
253        )?;
254    }
255    Ok(fs)
256}
257
258fn fuse_connections(kernel: &Kernel) -> Arc<FuseConnections> {
259    kernel.expando.get::<FuseConnections>()
260}
261
262pub fn new_fusectl_fs(
263    locked: &mut Locked<Unlocked>,
264    current_task: &CurrentTask,
265    options: FileSystemOptions,
266) -> Result<FileSystemHandle, Errno> {
267    let fs =
268        FileSystem::new(locked, current_task.kernel(), CacheMode::Uncached, FuseCtlFs, options)?;
269
270    let root_ino = fs.allocate_ino();
271    fs.create_root_with_info(
272        root_ino,
273        FuseCtlConnectionsDirectory {},
274        FsNodeInfo::new(mode!(IFDIR, 0o755), FsCred::root()),
275    );
276
277    Ok(fs)
278}
279
280#[derive(Debug)]
281struct FuseFs {
282    connection: Arc<FuseConnection>,
283    default_permissions: AtomicBool,
284}
285
286impl FuseFs {
287    /// Downcasts this `fs` to a [`FuseFs`].
288    ///
289    /// # Panics
290    ///
291    /// Panics if the `fs` is not a `FuseFs`.
292    fn from_fs(fs: &FileSystem) -> &FuseFs {
293        fs.downcast_ops::<FuseFs>().expect("FUSE should only handle `FuseFs`s")
294    }
295}
296
297impl FileSystemOps for FuseFs {
298    fn fs_lockdep_type(&self) -> FsLockDepType {
299        FsLockDepType::Fuse
300    }
301
302    fn rename(
303        &self,
304        locked: &mut Locked<FileOpsCore>,
305        _fs: &FileSystem,
306        current_task: &CurrentTask,
307        context: &mut RenameContext<'_>,
308        old_name: &FsStr,
309        new_name: &FsStr,
310    ) -> Result<(), Errno> {
311        let old_parent = &context.old_parent().node;
312        let new_parent = &context.new_parent().node;
313        self.connection.lock().execute_operation(
314            locked,
315            current_task,
316            FuseNode::from_node(&old_parent),
317            FuseOperation::Rename {
318                old_name: old_name.to_owned(),
319                new_dir: new_parent.node_key(),
320                new_name: new_name.to_owned(),
321            },
322        )?;
323        Ok(())
324    }
325
326    fn uses_external_node_ids(&self) -> bool {
327        true
328    }
329
330    fn statfs(
331        &self,
332        locked: &mut Locked<FileOpsCore>,
333        fs: &FileSystem,
334        current_task: &CurrentTask,
335    ) -> Result<statfs, Errno> {
336        let node = FuseNode::from_node(&fs.root().node);
337        let response = self.connection.lock().execute_operation(
338            locked,
339            current_task,
340            &node,
341            FuseOperation::Statfs,
342        )?;
343        let FuseResponse::Statfs(statfs_out) = response else {
344            return error!(EINVAL);
345        };
346        Ok(statfs {
347            f_type: FUSE_SUPER_MAGIC as i64,
348            f_blocks: statfs_out.st.blocks.try_into().map_err(|_| errno!(EINVAL))?,
349            f_bfree: statfs_out.st.bfree.try_into().map_err(|_| errno!(EINVAL))?,
350            f_bavail: statfs_out.st.bavail.try_into().map_err(|_| errno!(EINVAL))?,
351            f_files: statfs_out.st.files.try_into().map_err(|_| errno!(EINVAL))?,
352            f_ffree: statfs_out.st.ffree.try_into().map_err(|_| errno!(EINVAL))?,
353            f_bsize: statfs_out.st.bsize.try_into().map_err(|_| errno!(EINVAL))?,
354            f_namelen: statfs_out.st.namelen.try_into().map_err(|_| errno!(EINVAL))?,
355            f_frsize: statfs_out.st.frsize.try_into().map_err(|_| errno!(EINVAL))?,
356            ..statfs::default()
357        })
358    }
359    fn name(&self) -> &'static FsStr {
360        "fuse".into()
361    }
362    fn unmount(&self) {
363        self.connection.lock().disconnect();
364    }
365}
366
367#[derive(Debug, Default)]
368struct FuseConnections {
369    connections: Mutex<Vec<Weak<FuseConnection>>>,
370    next_identifier: AtomicCounter<u64>,
371}
372
373impl FuseConnections {
374    fn new_connection(&self, current_task: &CurrentTask) -> Arc<FuseConnection> {
375        let connection = Arc::new(FuseConnection {
376            id: self.next_identifier.next(),
377            creds: current_task.current_fscred(),
378            state: Default::default(),
379        });
380        self.connections.lock().push(Arc::downgrade(&connection));
381        connection
382    }
383
384    fn for_each<F>(&self, mut f: F)
385    where
386        F: FnMut(Arc<FuseConnection>),
387    {
388        self.connections.lock().retain(|connection| {
389            if let Some(connection) = connection.upgrade() {
390                f(connection);
391                true
392            } else {
393                false
394            }
395        });
396    }
397}
398
399struct FuseCtlFs;
400
401impl FileSystemOps for FuseCtlFs {
402    fn rename(
403        &self,
404        _locked: &mut Locked<FileOpsCore>,
405        _fs: &FileSystem,
406        _current_task: &CurrentTask,
407        _context: &mut RenameContext<'_>,
408        _old_name: &FsStr,
409        _new_name: &FsStr,
410    ) -> Result<(), Errno> {
411        error!(ENOTSUP)
412    }
413
414    fn statfs(
415        &self,
416        _locked: &mut Locked<FileOpsCore>,
417        _fs: &FileSystem,
418        _current_task: &CurrentTask,
419    ) -> Result<statfs, Errno> {
420        // Magic number has been extracted from the stat utility.
421        const FUSE_CTL_MAGIC: u32 = 0x65735543;
422        Ok(default_statfs(FUSE_CTL_MAGIC))
423    }
424
425    fn name(&self) -> &'static FsStr {
426        "fusectl".into()
427    }
428}
429
430#[derive(Debug)]
431struct FuseCtlConnectionsDirectory;
432
433impl FsNodeOps for FuseCtlConnectionsDirectory {
434    fs_node_impl_dir_readonly!();
435
436    fn create_file_ops(
437        &self,
438        _locked: &mut Locked<FileOpsCore>,
439        _node: &FsNode,
440        current_task: &CurrentTask,
441        _flags: OpenFlags,
442    ) -> Result<Box<dyn FileOps>, Errno> {
443        let connnections = fuse_connections(current_task.kernel());
444        let mut entries = vec![];
445        connnections.for_each(|connection| {
446            entries.push(VecDirectoryEntry {
447                entry_type: DirectoryEntryType::DIR,
448                name: connection.id.to_string().into(),
449                inode: None,
450            });
451        });
452        Ok(VecDirectory::new_file(entries))
453    }
454
455    fn lookup(
456        &self,
457        _locked: &mut Locked<FileOpsCore>,
458        node: &FsNode,
459        current_task: &CurrentTask,
460        name: &FsStr,
461    ) -> Result<FsNodeHandle, Errno> {
462        let name = std::str::from_utf8(name).map_err(|_| errno!(ENOENT))?;
463        let id = name.parse::<u64>().map_err(|_| errno!(ENOENT))?;
464        let connnections = fuse_connections(current_task.kernel());
465        let mut connection = None;
466        connnections.for_each(|c| {
467            if c.id == id {
468                connection = Some(c);
469            }
470        });
471        let Some(connection) = connection else {
472            return error!(ENOENT);
473        };
474        let fs = node.fs();
475        let dir = SimpleDirectory::new();
476        dir.edit(&fs, |dir| {
477            dir.node(
478                "abort".into(),
479                fs.create_node_and_allocate_node_id(
480                    AbortFile::new_node(connection.clone()),
481                    FsNodeInfo::new(mode!(IFREG, 0o200), connection.creds),
482                ),
483            );
484            dir.node(
485                "waiting".into(),
486                fs.create_node_and_allocate_node_id(
487                    WaitingFile::new_node(connection.clone()),
488                    FsNodeInfo::new(mode!(IFREG, 0o400), connection.creds),
489                ),
490            );
491        });
492
493        let info = FsNodeInfo::new(mode!(IFDIR, 0o500), connection.creds);
494        Ok(fs.create_node_and_allocate_node_id(dir, info))
495    }
496}
497
498#[derive(Debug)]
499struct AbortFile {
500    connection: Arc<FuseConnection>,
501}
502
503impl AbortFile {
504    fn new_node(connection: Arc<FuseConnection>) -> impl FsNodeOps {
505        SimpleFileNode::new(move |_, _| Ok(Self { connection: connection.clone() }))
506    }
507}
508
509impl FileOps for AbortFile {
510    fileops_impl_nonseekable!();
511    fileops_impl_noop_sync!();
512
513    fn read(
514        &self,
515        _locked: &mut Locked<FileOpsCore>,
516        _file: &FileObject,
517        _current_task: &CurrentTask,
518        _offset: usize,
519        _data: &mut dyn OutputBuffer,
520    ) -> Result<usize, Errno> {
521        Ok(0)
522    }
523
524    fn write(
525        &self,
526        _locked: &mut Locked<FileOpsCore>,
527        _file: &FileObject,
528        _current_task: &CurrentTask,
529        _offset: usize,
530        data: &mut dyn InputBuffer,
531    ) -> Result<usize, Errno> {
532        let drained = data.drain();
533        if drained > 0 {
534            self.connection.lock().disconnect();
535        }
536        Ok(drained)
537    }
538}
539
540#[derive(Clone, Debug)]
541struct WaitingFile {
542    connection: Arc<FuseConnection>,
543}
544
545impl WaitingFile {
546    fn new_node(connection: Arc<FuseConnection>) -> impl FsNodeOps {
547        DynamicFile::new_node(Self { connection })
548    }
549}
550
551impl DynamicFileSource for WaitingFile {
552    fn generate(
553        &self,
554        _current_task: &CurrentTask,
555        sink: &mut DynamicFileBuf,
556    ) -> Result<(), Errno> {
557        let value = {
558            let state = self.connection.state.lock();
559            state.operations.len() + state.message_queue.len()
560        };
561        let value = format!("{value}\n");
562        sink.write(value.as_bytes());
563        Ok(())
564    }
565}
566
567#[derive(Debug, Default)]
568struct FuseNodeMutableState {
569    nlookup: u64,
570}
571
572#[derive(Debug)]
573struct FuseNode {
574    connection: Arc<FuseConnection>,
575
576    /// A unique identifier for this node.
577    ///
578    /// This value might not be the same as the inode number ([`FsNodeInfo::ino`]).
579    ///
580    /// See <https://libfuse.github.io/doxygen/structfuse__operations.html#ac39a0b7125a0e5001eb5ff42e05faa5d>
581    /// for more information.
582    nodeid: u64,
583
584    generation: u64,
585    attributes_valid_until: AtomicMonotonicInstant,
586    state: Mutex<FuseNodeMutableState>,
587}
588
589impl FuseNode {
590    fn new(connection: Arc<FuseConnection>, nodeid: u64, generation: u64) -> Self {
591        Self {
592            connection,
593            nodeid,
594            generation,
595            attributes_valid_until: zx::MonotonicInstant::INFINITE_PAST.into(),
596            state: Default::default(),
597        }
598    }
599
600    /// Downcasts this `node` to a [`FuseNode`].
601    ///
602    /// # Panics
603    ///
604    /// Panics if the `node` is not a `FuseNode`.
605    fn from_node(node: &FsNode) -> &FuseNode {
606        node.downcast_ops::<FuseNode>().expect("FUSE should only handle `FuseNode`s")
607    }
608
609    fn default_check_access_with_valid_node_attributes(
610        &self,
611        locked: &mut Locked<FileOpsCore>,
612        node: &FsNode,
613        current_task: &CurrentTask,
614        permission_flags: security::PermissionFlags,
615        reason: CheckAccessReason,
616        info: &DynamicLockDepRwLock<FsNodeInfo>,
617        audit_context: security::Auditable<'_>,
618    ) -> Result<(), Errno> {
619        let info = self.refresh_expired_node_attributes(locked, current_task, info)?;
620        node.default_check_access_impl(current_task, permission_flags, reason, info, audit_context)
621    }
622
623    fn refresh_expired_node_attributes<'a>(
624        &self,
625        locked: &mut Locked<FileOpsCore>,
626        current_task: &CurrentTask,
627        info: &'a DynamicLockDepRwLock<FsNodeInfo>,
628    ) -> Result<LockDepReadGuard<'a, FsNodeInfo>, Errno> {
629        // Relaxed because the attributes valid until atomic is not used to synchronize
630        // anything. Its final access is protected by the info lock anyways.
631        const VALID_UNTIL_LOAD_ORDERING: Ordering = Ordering::Relaxed;
632
633        let now = zx::MonotonicInstant::get();
634        if self.attributes_valid_until.load(VALID_UNTIL_LOAD_ORDERING) >= now {
635            let info = info.read();
636
637            // Check the valid_until again after taking the info lock to make sure
638            // that the attributes are still valid. We do this because after we
639            // checked the first time and now, the node's attributes could have been
640            // invalidated or expired.
641            //
642            // But why not only check if the attributes are valid after taking the
643            // lock? Because when we will impact FUSE implementations that don't
644            // support caching, or caching with small valid durations, by always
645            // taking a read lock which we then drop to acquire a write lock. We
646            // slightly pessimize the "happy" path with an extra atomic load so
647            // that we don't overly pessimize the uncached/"slower" path.
648            if self.attributes_valid_until.load(VALID_UNTIL_LOAD_ORDERING) >= now {
649                return Ok(info);
650            }
651        }
652
653        // Force a refresh of our cached attributes.
654        self.fetch_and_refresh_info_impl(locked, current_task, info)
655    }
656
657    fn fetch_and_refresh_info_impl<'a>(
658        &self,
659        locked: &mut Locked<FileOpsCore>,
660        current_task: &CurrentTask,
661        info: &'a DynamicLockDepRwLock<FsNodeInfo>,
662    ) -> Result<LockDepReadGuard<'a, FsNodeInfo>, Errno> {
663        let response = self.connection.lock().execute_operation(
664            locked,
665            current_task,
666            self,
667            FuseOperation::GetAttr,
668        )?;
669        let uapi::fuse_attr_out { attr_valid, attr_valid_nsec, attr, .. } =
670            if let FuseResponse::Attr(attr) = response {
671                attr
672            } else {
673                return error!(EINVAL);
674            };
675        let mut info = info.write();
676        FuseNode::update_node_info_from_attr(
677            &mut info,
678            attr,
679            attr_valid_to_duration(attr_valid, attr_valid_nsec)?,
680            &self.attributes_valid_until,
681        )?;
682        Ok(LockDepWriteGuard::downgrade(info))
683    }
684
685    fn invalidate_attributes(&self) {
686        self.attributes_valid_until.store(zx::MonotonicInstant::INFINITE_PAST, Ordering::Relaxed);
687    }
688
689    fn update_node_info_from_attr(
690        info: &mut FsNodeInfo,
691        attributes: uapi::fuse_attr,
692        attr_valid_duration: zx::MonotonicDuration,
693        node_attributes_valid_until: &AtomicMonotonicInstant,
694    ) -> Result<(), Errno> {
695        info.mode = FileMode::from_bits(attributes.mode);
696        info.size = attributes.size.try_into().map_err(|_| errno!(EINVAL))?;
697        info.blocks = attributes.blocks.try_into().map_err(|_| errno!(EINVAL))?;
698        info.blksize = attributes.blksize.try_into().map_err(|_| errno!(EINVAL))?;
699        info.uid = attributes.uid;
700        info.gid = attributes.gid;
701        info.link_count = attributes.nlink.try_into().map_err(|_| errno!(EINVAL))?;
702        info.time_status_change = time_from_timespec(uapi::timespec {
703            tv_sec: attributes.ctime as i64,
704            tv_nsec: attributes.ctimensec as i64,
705        })?;
706        info.time_access = time_from_timespec(uapi::timespec {
707            tv_sec: attributes.atime as i64,
708            tv_nsec: attributes.atimensec as i64,
709        })?;
710        info.time_modify = time_from_timespec(uapi::timespec {
711            tv_sec: attributes.mtime as i64,
712            tv_nsec: attributes.mtimensec as i64,
713        })?;
714        info.rdev = DeviceId::from_bits(attributes.rdev as u64);
715
716        node_attributes_valid_until
717            .store(zx::MonotonicInstant::after(attr_valid_duration), Ordering::Relaxed);
718        Ok(())
719    }
720
721    /// Build a FsNodeHandle from a FuseResponse that is expected to be a FuseResponse::Entry.
722    fn fs_node_from_entry(
723        &self,
724        node: &FsNode,
725        name: &FsStr,
726        entry: &uapi::fuse_entry_out,
727    ) -> Result<FsNodeHandle, Errno> {
728        if entry.nodeid == 0 {
729            return error!(ENOENT);
730        }
731        let node = node.fs().get_and_validate_or_create_node(
732            entry.nodeid,
733            |node| {
734                let fuse_node = FuseNode::from_node(&node);
735                fuse_node.generation == entry.generation
736            },
737            || {
738                let fuse_node =
739                    FuseNode::new(self.connection.clone(), entry.nodeid, entry.generation);
740                let mut info = FsNodeInfo::default();
741                FuseNode::update_node_info_from_attr(
742                    &mut info,
743                    entry.attr,
744                    attr_valid_to_duration(entry.attr_valid, entry.attr_valid_nsec)?,
745                    &fuse_node.attributes_valid_until,
746                )?;
747                Ok(FsNode::new_uncached(
748                    entry.attr.ino,
749                    fuse_node,
750                    &node.fs(),
751                    info,
752                    FsNodeFlags::empty(),
753                ))
754            },
755        )?;
756        // . and .. do not get their lookup count increased.
757        if !DirEntry::is_reserved_name(name) {
758            let fuse_node = FuseNode::from_node(&node);
759            fuse_node.state.lock().nlookup += 1;
760        }
761        Ok(node)
762    }
763}
764
765struct FuseFileObject {
766    connection: Arc<FuseConnection>,
767    /// An optional file handle to redirect all read/write to.
768    passthrough_file: WeakFileHandle,
769    /// The response to the open calls from the userspace process.
770    open_out: uapi::fuse_open_out,
771}
772
773impl FuseFileObject {
774    /// Returns the `FuseNode` associated with the opened file.
775    fn get_fuse_node(file: &FileObject) -> &FuseNode {
776        FuseNode::from_node(file.node())
777    }
778}
779
780impl FileOps for FuseFileObject {
781    fn close(
782        self: Box<Self>,
783        locked: &mut Locked<FileOpsCore>,
784        file: &FileObjectState,
785        current_task: &CurrentTask,
786    ) {
787        let node = FuseNode::from_node(file.node());
788        let is_dir = file.node().is_dir();
789        {
790            let mut connection = self.connection.lock();
791            if let Err(e) = connection.execute_operation(
792                locked,
793                current_task,
794                node,
795                if is_dir {
796                    FuseOperation::ReleaseDir(self.open_out)
797                } else {
798                    FuseOperation::Release(self.open_out)
799                },
800            ) {
801                if e.code != ENOSYS {
802                    log_error!("Error when releasing fh: {e:?}");
803                }
804            }
805            connection.clear_released_passthrough_fds();
806        }
807    }
808
809    fn flush(
810        &self,
811        locked: &mut Locked<FileOpsCore>,
812        file: &FileObject,
813        current_task: &CurrentTask,
814    ) {
815        let node = Self::get_fuse_node(file);
816        if let Err(e) = self.connection.lock().execute_operation(
817            locked,
818            current_task,
819            node,
820            FuseOperation::Flush(self.open_out),
821        ) {
822            log_error!("Error when flushing fh: {e:?}");
823        }
824    }
825
826    fn is_seekable(&self) -> bool {
827        true
828    }
829
830    fn read(
831        &self,
832        locked: &mut Locked<FileOpsCore>,
833        file: &FileObject,
834        current_task: &CurrentTask,
835        offset: usize,
836        data: &mut dyn OutputBuffer,
837    ) -> Result<usize, Errno> {
838        if file.node().info().mode.is_dir() {
839            return error!(EISDIR);
840        }
841        if let Some(file_object) = self.passthrough_file.upgrade() {
842            return file_object.ops().read(locked, &file_object, current_task, offset, data);
843        }
844
845        // EOF bounds capping to prevent reading past file limits.
846        let file_size = file.node().info().size;
847        if offset >= file_size {
848            return Ok(0);
849        }
850        let target_size = std::cmp::min(data.available(), file_size - offset);
851        if target_size == 0 {
852            return Ok(0);
853        }
854
855        let node = Self::get_fuse_node(file);
856        let response = self.connection.lock().execute_operation(
857            locked,
858            current_task,
859            node,
860            FuseOperation::Read(uapi::fuse_read_in {
861                fh: self.open_out.fh,
862                offset: offset.try_into().map_err(|_| errno!(EINVAL))?,
863                size: target_size.try_into().unwrap_or(u32::MAX),
864                read_flags: 0,
865                lock_owner: 0,
866                flags: 0,
867                padding: 0,
868            }),
869        )?;
870        let FuseResponse::Read(read_out) = response else {
871            return error!(EINVAL);
872        };
873        data.write(&read_out)
874    }
875
876    fn write(
877        &self,
878        locked: &mut Locked<FileOpsCore>,
879        file: &FileObject,
880        current_task: &CurrentTask,
881        offset: usize,
882        data: &mut dyn InputBuffer,
883    ) -> Result<usize, Errno> {
884        if file.node().info().mode.is_dir() {
885            return error!(EISDIR);
886        }
887        if let Some(file_object) = self.passthrough_file.upgrade() {
888            return file_object.ops().write(locked, &file_object, current_task, offset, data);
889        }
890        let node = Self::get_fuse_node(file);
891        let content = data.peek_all()?;
892        let response = self.connection.lock().execute_operation(
893            locked,
894            current_task,
895            node,
896            FuseOperation::Write {
897                write_in: uapi::fuse_write_in {
898                    fh: self.open_out.fh,
899                    offset: offset.try_into().map_err(|_| errno!(EINVAL))?,
900                    size: content.len().try_into().map_err(|_| errno!(EINVAL))?,
901                    write_flags: 0,
902                    lock_owner: 0,
903                    flags: 0,
904                    padding: 0,
905                },
906                content,
907            },
908        )?;
909        let FuseResponse::Write(write_out) = response else {
910            return error!(EINVAL);
911        };
912        node.invalidate_attributes();
913
914        let written = write_out.size as usize;
915
916        data.advance(written)?;
917        Ok(written)
918    }
919
920    fn seek(
921        &self,
922        locked: &mut Locked<FileOpsCore>,
923        file: &FileObject,
924        current_task: &CurrentTask,
925        current_offset: off_t,
926        target: SeekTarget,
927    ) -> Result<off_t, Errno> {
928        // Only delegate SEEK_DATA and SEEK_HOLE to the userspace process.
929        if matches!(target, SeekTarget::Data(_) | SeekTarget::Hole(_)) {
930            let node = Self::get_fuse_node(file);
931            let response = self.connection.lock().execute_operation(
932                locked,
933                current_task,
934                node,
935                FuseOperation::Seek(uapi::fuse_lseek_in {
936                    fh: self.open_out.fh,
937                    offset: target.offset().try_into().map_err(|_| errno!(EINVAL))?,
938                    whence: target.whence(),
939                    padding: 0,
940                }),
941            );
942            match response {
943                Ok(response) => {
944                    let FuseResponse::Seek(seek_out) = response else {
945                        return error!(EINVAL);
946                    };
947                    return seek_out.offset.try_into().map_err(|_| errno!(EINVAL));
948                }
949                // If errno is ENOSYS, the userspace process doesn't support the seek operation and
950                // the default seek must be used.
951                Err(errno) if errno == ENOSYS => {}
952                Err(errno) => return Err(errno),
953            };
954        }
955
956        default_seek(current_offset, target, || default_eof_offset(locked, file, current_task))
957    }
958
959    fn sync(&self, _file: &FileObject, _current_task: &CurrentTask) -> Result<(), Errno> {
960        track_stub!(TODO("https://fxbug.dev/352359968"), "FUSE fsync()");
961        Ok(())
962    }
963
964    fn wait_async(
965        &self,
966        _locked: &mut Locked<FileOpsCore>,
967        _file: &FileObject,
968        _current_task: &CurrentTask,
969        _waiter: &Waiter,
970        _events: FdEvents,
971        _handler: EventHandler,
972    ) -> Option<WaitCanceler> {
973        None
974    }
975
976    fn query_events(
977        &self,
978        locked: &mut Locked<FileOpsCore>,
979        file: &FileObject,
980        current_task: &CurrentTask,
981    ) -> Result<FdEvents, Errno> {
982        let node = Self::get_fuse_node(file);
983        let response = self.connection.lock().execute_operation(
984            locked,
985            current_task,
986            node,
987            FuseOperation::Poll(uapi::fuse_poll_in {
988                fh: self.open_out.fh,
989                kh: 0,
990                flags: 0,
991                events: FdEvents::all().bits(),
992            }),
993        )?;
994        let FuseResponse::Poll(poll_out) = response else {
995            return error!(EINVAL);
996        };
997        FdEvents::from_bits(poll_out.revents).ok_or_else(|| errno!(EINVAL))
998    }
999
1000    fn readdir(
1001        &self,
1002        locked: &mut Locked<FileOpsCore>,
1003        file: &FileObject,
1004        current_task: &CurrentTask,
1005        sink: &mut dyn DirentSink,
1006    ) -> Result<(), Errno> {
1007        let mut state = self.connection.lock();
1008        let configuration = state.get_configuration(locked, current_task)?;
1009        let use_readdirplus = {
1010            if configuration.flags.contains(FuseInitFlags::DO_READDIRPLUS) {
1011                if configuration.flags.contains(FuseInitFlags::READDIRPLUS_AUTO) {
1012                    sink.offset() == 0
1013                } else {
1014                    true
1015                }
1016            } else {
1017                false
1018            }
1019        };
1020        // Request a number of bytes related to the user capacity. If none is given, default to a
1021        // single page of data.
1022        let user_capacity = if let Some(base_user_capacity) = sink.user_capacity() {
1023            if use_readdirplus {
1024                // Add some amount of capacity for the entries.
1025                base_user_capacity * 3 / 2
1026            } else {
1027                base_user_capacity
1028            }
1029        } else {
1030            *PAGE_SIZE as usize
1031        };
1032        let node = Self::get_fuse_node(file);
1033        let response = state.execute_operation(
1034            locked,
1035            current_task,
1036            node,
1037            FuseOperation::Readdir {
1038                read_in: uapi::fuse_read_in {
1039                    fh: self.open_out.fh,
1040                    offset: sink.offset().try_into().map_err(|_| errno!(EINVAL))?,
1041                    size: user_capacity.try_into().map_err(|_| errno!(EINVAL))?,
1042                    read_flags: 0,
1043                    lock_owner: 0,
1044                    flags: 0,
1045                    padding: 0,
1046                },
1047                use_readdirplus,
1048            },
1049        )?;
1050        std::mem::drop(state);
1051        let FuseResponse::Readdir(dirents) = response else {
1052            return error!(EINVAL);
1053        };
1054        let mut sink_result = Ok(());
1055        for (dirent, name, entry) in dirents {
1056            if let Some(entry) = entry {
1057                // nodeid == 0 means the server doesn't want to send entry info.
1058                if entry.nodeid != 0 {
1059                    if let Err(e) = node.fs_node_from_entry(file.node(), name.as_ref(), &entry) {
1060                        log_error!("Unable to prefill entry: {e:?}");
1061                    }
1062                }
1063            }
1064            if sink_result.is_ok() {
1065                sink_result = sink.add(
1066                    dirent.ino,
1067                    dirent.off.try_into().map_err(|_| errno!(EINVAL))?,
1068                    DirectoryEntryType::from_bits(
1069                        dirent.type_.try_into().map_err(|_| errno!(EINVAL))?,
1070                    ),
1071                    name.as_ref(),
1072                );
1073            }
1074        }
1075        sink_result
1076    }
1077
1078    fn ioctl(
1079        &self,
1080        locked: &mut Locked<Unlocked>,
1081        file: &FileObject,
1082        current_task: &CurrentTask,
1083        request: u32,
1084        arg: SyscallArg,
1085    ) -> Result<SyscallResult, Errno> {
1086        track_stub!(TODO("https://fxbug.dev/322875259"), "fuse ioctl");
1087        default_ioctl(file, locked, current_task, request, arg)
1088    }
1089
1090    fn fcntl(
1091        &self,
1092        _file: &FileObject,
1093        _current_task: &CurrentTask,
1094        cmd: u32,
1095        _arg: u64,
1096    ) -> Result<SyscallResult, Errno> {
1097        track_stub!(TODO("https://fxbug.dev/322875764"), "fuse fcntl");
1098        default_fcntl(cmd)
1099    }
1100}
1101
1102struct FuseDirEntry {
1103    valid_until: AtomicMonotonicInstant,
1104}
1105
1106impl Default for FuseDirEntry {
1107    fn default() -> Self {
1108        Self { valid_until: zx::MonotonicInstant::INFINITE_PAST.into() }
1109    }
1110}
1111
1112impl DirEntryOps for FuseDirEntry {
1113    fn revalidate(
1114        &self,
1115        locked: &mut Locked<FileOpsCore>,
1116        current_task: &CurrentTask,
1117        dir_entry: &DirEntry,
1118    ) -> Result<bool, Errno> {
1119        // Relaxed because the attributes valid until atomic is not used to synchronize
1120        // anything.
1121        const VALID_UNTIL_ORDERING: Ordering = Ordering::Relaxed;
1122
1123        let now = zx::MonotonicInstant::get();
1124        if self.valid_until.load(VALID_UNTIL_ORDERING) >= now {
1125            return Ok(true);
1126        }
1127
1128        let node = FuseNode::from_node(&dir_entry.node);
1129        if node.nodeid == FUSE_ROOT_ID_U64 {
1130            // The root node entry is always valid.
1131            return Ok(true);
1132        }
1133
1134        // Perform a lookup on this entry's parent FUSE node to revalidate this
1135        // entry.
1136        let (parent, name) = {
1137            let scope = RcuReadScope::new();
1138            let parent = dir_entry.parent().expect("non-root nodes always has a parent");
1139            let name = dir_entry.local_name(&scope).to_owned();
1140            (parent, name)
1141        };
1142        let parent = FuseNode::from_node(&parent.node);
1143        let FuseEntryOutExtended {
1144            arg:
1145                uapi::fuse_entry_out {
1146                    nodeid,
1147                    generation,
1148                    entry_valid,
1149                    entry_valid_nsec,
1150                    attr,
1151                    attr_valid,
1152                    attr_valid_nsec,
1153                },
1154            ..
1155        } = match parent.connection.lock().execute_operation(
1156            locked,
1157            current_task,
1158            parent,
1159            FuseOperation::Lookup { name },
1160        ) {
1161            Ok(FuseResponse::Entry(entry)) => entry,
1162            Ok(_) => return error!(EINVAL),
1163            Err(errno) => {
1164                if errno == ENOENT {
1165                    // The entry no longer exists.
1166                    return Ok(false);
1167                } else {
1168                    return Err(errno);
1169                };
1170            }
1171        };
1172
1173        if (nodeid != node.nodeid) || (generation != node.generation) {
1174            // A new entry exists with the name. This `DirEntry` is no longer
1175            // valid. The caller should attempt to restart the path walk at this
1176            // node.
1177            return Ok(false);
1178        }
1179
1180        dir_entry.node.update_info(|info| {
1181            FuseNode::update_node_info_from_attr(
1182                info,
1183                attr,
1184                attr_valid_to_duration(attr_valid, attr_valid_nsec)?,
1185                &node.attributes_valid_until,
1186            )?;
1187
1188            self.valid_until.store(
1189                zx::MonotonicInstant::after(attr_valid_to_duration(entry_valid, entry_valid_nsec)?),
1190                VALID_UNTIL_ORDERING,
1191            );
1192
1193            Ok(true)
1194        })
1195    }
1196}
1197
1198// `FuseFs.default_permissions` is not used to synchronize anything.
1199const DEFAULT_PERMISSIONS_ATOMIC_ORDERING: Ordering = Ordering::Relaxed;
1200
1201impl FsNodeOps for FuseNode {
1202    fn check_access(
1203        &self,
1204        locked: &mut Locked<FileOpsCore>,
1205        node: &FsNode,
1206        current_task: &CurrentTask,
1207        permission_flags: security::PermissionFlags,
1208        info: &DynamicLockDepRwLock<FsNodeInfo>,
1209        reason: CheckAccessReason,
1210        audit_context: security::Auditable<'_>,
1211    ) -> Result<(), Errno> {
1212        // Perform access checks regardless of the reason when userspace configured
1213        // the kernel to perform its default access checks on behalf of the FUSE fs.
1214        if FuseFs::from_fs(&node.fs()).default_permissions.load(DEFAULT_PERMISSIONS_ATOMIC_ORDERING)
1215        {
1216            return self.default_check_access_with_valid_node_attributes(
1217                locked,
1218                node,
1219                current_task,
1220                permission_flags,
1221                reason,
1222                info,
1223                audit_context,
1224            );
1225        }
1226
1227        match reason {
1228            CheckAccessReason::Access | CheckAccessReason::Chdir | CheckAccessReason::Chroot => {
1229                // Per `libfuse`'s low-level handler for `FUSE_ACCESS` requests, the kernel
1230                // is only expected to send `FUSE_ACCESS` requests for the `access` and `chdir`
1231                // family of syscalls when the `default_permissions` flag isn't set on the FUSE
1232                // fs. Seems like `chroot` also triggers a `FUSE_ACCESS` request on Linux.
1233                let response = self.connection.lock().execute_operation(
1234                    locked,
1235                    current_task,
1236                    self,
1237                    FuseOperation::Access {
1238                        mask: (permission_flags.as_access() & Access::ACCESS_MASK).bits() as u32,
1239                    },
1240                )?;
1241
1242                if let FuseResponse::Access(result) = response { result } else { error!(EINVAL) }
1243            }
1244            CheckAccessReason::Exec => self.default_check_access_with_valid_node_attributes(
1245                locked,
1246                node,
1247                current_task,
1248                permission_flags,
1249                reason,
1250                info,
1251                audit_context,
1252            ),
1253            CheckAccessReason::ChangeTimestamps { .. }
1254            | CheckAccessReason::InternalPermissionChecks => {
1255                // Per FUSE's mount options, the kernel does not check file access
1256                // permissions unless the default permissions mount option is set.
1257                //
1258                // See https://www.kernel.org/doc/html/v5.6/filesystems/fuse.html#mount-options.
1259                Ok(())
1260            }
1261        }
1262    }
1263
1264    fn create_dir_entry_ops(&self) -> Box<dyn DirEntryOps> {
1265        Box::new(FuseDirEntry::default())
1266    }
1267
1268    fn create_file_ops(
1269        &self,
1270        locked: &mut Locked<FileOpsCore>,
1271        node: &FsNode,
1272        current_task: &CurrentTask,
1273        flags: OpenFlags,
1274    ) -> Result<Box<dyn FileOps>, Errno> {
1275        // The node already exists. The creation has been handled before calling this method.
1276        let flags = flags & !(OpenFlags::CREAT | OpenFlags::EXCL);
1277        let mode = node.info().mode;
1278        let response = self.connection.lock().execute_operation(
1279            locked,
1280            current_task,
1281            self,
1282            FuseOperation::Open { flags, mode },
1283        )?;
1284        let FuseResponse::Open(open_out) = response else {
1285            return error!(EINVAL);
1286        };
1287        // SAFETY: The data has been read with zerocopy which ensures every bits have been
1288        // initialized.
1289        let passthrough_fh = unsafe { open_out.__bindgen_anon_1.passthrough_fh };
1290        let passthrough_file = if passthrough_fh != 0 {
1291            let mut connection = self.connection.lock();
1292            connection.registered_passthrough.remove(&passthrough_fh).unwrap_or_default()
1293        } else {
1294            Weak::new()
1295        };
1296        Ok(Box::new(FuseFileObject {
1297            connection: self.connection.clone(),
1298            passthrough_file,
1299            open_out,
1300        }))
1301    }
1302
1303    fn lookup(
1304        &self,
1305        locked: &mut Locked<FileOpsCore>,
1306        node: &FsNode,
1307        current_task: &CurrentTask,
1308        name: &FsStr,
1309    ) -> Result<FsNodeHandle, Errno> {
1310        let response = self.connection.lock().execute_operation(
1311            locked,
1312            current_task,
1313            self,
1314            FuseOperation::Lookup { name: name.to_owned() },
1315        )?;
1316        self.fs_node_from_entry(node, name, response.entry().ok_or_else(|| errno!(EINVAL))?)
1317    }
1318
1319    fn mknod(
1320        &self,
1321        locked: &mut Locked<FileOpsCore>,
1322        node: &FsNode,
1323        current_task: &CurrentTask,
1324        name: &FsStr,
1325        mode: FileMode,
1326        dev: DeviceId,
1327        _owner: FsCred,
1328    ) -> Result<FsNodeHandle, Errno> {
1329        let get_entry = |locked: &mut Locked<FileOpsCore>| {
1330            let umask = current_task.fs().umask().bits();
1331            let mut connection = self.connection.lock();
1332
1333            if dev == DeviceId::NONE && !connection.no_create {
1334                match connection.execute_operation(
1335                    locked,
1336                    current_task,
1337                    self,
1338                    FuseOperation::Create(
1339                        uapi::fuse_create_in {
1340                            flags: OpenFlags::CREAT.bits(),
1341                            mode: mode.bits(),
1342                            umask,
1343                            open_flags: 0,
1344                        },
1345                        name.to_owned(),
1346                    ),
1347                ) {
1348                    Ok(response) => {
1349                        let FuseResponse::Create(response) = response else {
1350                            return error!(EINVAL);
1351                        };
1352
1353                        let fuse_node = FuseNode::new(
1354                            self.connection.clone(),
1355                            response.entry.nodeid,
1356                            response.entry.generation,
1357                        );
1358
1359                        // It is unfortunate that we have to immediately release the file (only for
1360                        // it to typically be reopened a short time later), but it will be a little
1361                        // tricky to fix this properly, and there are Fuse implementations that rely
1362                        // on us using create rather than mknod to create regular files.  We will
1363                        // have to tackle this if it shows up as a performance issue.
1364                        if let Err(e) = connection.execute_operation(
1365                            locked,
1366                            current_task,
1367                            &fuse_node,
1368                            FuseOperation::Release(response.open),
1369                        ) {
1370                            log_error!("Error when releasing fh: {e:?}");
1371                        }
1372
1373                        return Ok(response.entry);
1374                    }
1375                    Err(e) if e == ENOSYS => {
1376                        connection.no_create = true;
1377                        // Fall through to use mknod below.
1378                    }
1379                    Err(e) => return Err(e),
1380                }
1381            }
1382
1383            connection
1384                .execute_operation(
1385                    locked,
1386                    current_task,
1387                    self,
1388                    FuseOperation::Mknod {
1389                        mknod_in: uapi::fuse_mknod_in {
1390                            mode: mode.bits(),
1391                            rdev: dev.bits() as u32,
1392                            umask,
1393                            padding: 0,
1394                        },
1395                        name: name.to_owned(),
1396                    },
1397                )?
1398                .entry()
1399                .copied()
1400                .ok_or_else(|| errno!(EINVAL))
1401        };
1402
1403        let entry = get_entry(locked)?;
1404        self.fs_node_from_entry(node, name, &entry)
1405    }
1406
1407    fn mkdir(
1408        &self,
1409        locked: &mut Locked<FileOpsCore>,
1410        node: &FsNode,
1411        current_task: &CurrentTask,
1412        name: &FsStr,
1413        mode: FileMode,
1414        _owner: FsCred,
1415    ) -> Result<FsNodeHandle, Errno> {
1416        let response = self.connection.lock().execute_operation(
1417            locked,
1418            current_task,
1419            self,
1420            FuseOperation::Mkdir {
1421                mkdir_in: uapi::fuse_mkdir_in {
1422                    mode: mode.bits(),
1423                    umask: current_task.fs().umask().bits(),
1424                },
1425                name: name.to_owned(),
1426            },
1427        )?;
1428        self.fs_node_from_entry(node, name, response.entry().ok_or_else(|| errno!(EINVAL))?)
1429    }
1430
1431    fn create_symlink(
1432        &self,
1433        locked: &mut Locked<FileOpsCore>,
1434        node: &FsNode,
1435        current_task: &CurrentTask,
1436        name: &FsStr,
1437        target: &FsStr,
1438        _owner: FsCred,
1439    ) -> Result<FsNodeHandle, Errno> {
1440        let response = self.connection.lock().execute_operation(
1441            locked,
1442            current_task,
1443            self,
1444            FuseOperation::Symlink { target: target.to_owned(), name: name.to_owned() },
1445        )?;
1446        self.fs_node_from_entry(node, name, response.entry().ok_or_else(|| errno!(EINVAL))?)
1447    }
1448
1449    fn readlink(
1450        &self,
1451        locked: &mut Locked<FileOpsCore>,
1452        _node: &FsNode,
1453        current_task: &CurrentTask,
1454    ) -> Result<SymlinkTarget, Errno> {
1455        let response = self.connection.lock().execute_operation(
1456            locked,
1457            current_task,
1458            self,
1459            FuseOperation::Readlink,
1460        )?;
1461        let FuseResponse::Read(read_out) = response else {
1462            return error!(EINVAL);
1463        };
1464        Ok(SymlinkTarget::Path(read_out.into()))
1465    }
1466
1467    fn link(
1468        &self,
1469        locked: &mut Locked<FileOpsCore>,
1470        _node: &FsNode,
1471        current_task: &CurrentTask,
1472        name: &FsStr,
1473        child: &FsNodeHandle,
1474    ) -> Result<(), Errno> {
1475        let child_node = FuseNode::from_node(child);
1476        self.connection
1477            .lock()
1478            .execute_operation(
1479                locked,
1480                current_task,
1481                self,
1482                FuseOperation::Link {
1483                    link_in: uapi::fuse_link_in { oldnodeid: child_node.nodeid },
1484                    name: name.to_owned(),
1485                },
1486            )
1487            .map(|_| ())
1488    }
1489
1490    fn unlink(
1491        &self,
1492        locked: &mut Locked<FileOpsCore>,
1493        _node: &FsNode,
1494        current_task: &CurrentTask,
1495        name: &FsStr,
1496        child: &FsNodeHandle,
1497    ) -> Result<(), Errno> {
1498        let is_dir = child.is_dir();
1499        self.connection
1500            .lock()
1501            .execute_operation(
1502                locked,
1503                current_task,
1504                self,
1505                if is_dir {
1506                    FuseOperation::Rmdir { name: name.to_owned() }
1507                } else {
1508                    FuseOperation::Unlink { name: name.to_owned() }
1509                },
1510            )
1511            .map(|_| ())
1512    }
1513
1514    fn truncate(
1515        &self,
1516        locked: &mut Locked<FileOpsCore>,
1517        _guard: &AppendLockWriteGuard<'_>,
1518        node: &FsNode,
1519        current_task: &CurrentTask,
1520        length: u64,
1521    ) -> Result<(), Errno> {
1522        node.update_info(|info| {
1523            // Truncate is implemented by updating the attributes of the file.
1524            let attributes = uapi::fuse_setattr_in {
1525                size: length,
1526                valid: uapi::FATTR_SIZE,
1527                ..Default::default()
1528            };
1529
1530            let response = self.connection.lock().execute_operation(
1531                locked,
1532                current_task,
1533                self,
1534                FuseOperation::SetAttr(attributes),
1535            )?;
1536            let uapi::fuse_attr_out { attr_valid, attr_valid_nsec, attr, .. } =
1537                if let FuseResponse::Attr(attr) = response {
1538                    attr
1539                } else {
1540                    return error!(EINVAL);
1541                };
1542            FuseNode::update_node_info_from_attr(
1543                info,
1544                attr,
1545                attr_valid_to_duration(attr_valid, attr_valid_nsec)?,
1546                &self.attributes_valid_until,
1547            )?;
1548            Ok(())
1549        })
1550    }
1551
1552    fn allocate(
1553        &self,
1554        _locked: &mut Locked<FileOpsCore>,
1555        _guard: &AppendLockWriteGuard<'_>,
1556        _node: &FsNode,
1557        _current_task: &CurrentTask,
1558        _mode: FallocMode,
1559        _offset: u64,
1560        _length: u64,
1561    ) -> Result<(), Errno> {
1562        track_stub!(TODO("https://fxbug.dev/322875414"), "FsNodeOps::allocate");
1563        error!(ENOTSUP)
1564    }
1565
1566    fn fetch_and_refresh_info<'a>(
1567        &self,
1568        locked: &mut Locked<FileOpsCore>,
1569        _node: &FsNode,
1570        current_task: &CurrentTask,
1571        info: &'a DynamicLockDepRwLock<FsNodeInfo>,
1572    ) -> Result<LockDepReadGuard<'a, FsNodeInfo>, Errno> {
1573        // NOTE: Do not be tempted to always refresh information here; sadly, there are CTS tests
1574        // that rely on this only updating attributes if they have expired, and this matches what
1575        // Linux appears to do.
1576        self.refresh_expired_node_attributes(locked, current_task, info)
1577    }
1578
1579    fn update_attributes(
1580        &self,
1581        locked: &mut Locked<FileOpsCore>,
1582        _node: &FsNode,
1583        current_task: &CurrentTask,
1584        info: &FsNodeInfo,
1585        has: zxio_node_attr_has_t,
1586    ) -> Result<(), Errno> {
1587        let mut valid = 0u32;
1588        // Nb: We don't have a mechanism for 'FATTR_*TIME_NOW'.
1589        if has.modification_time {
1590            valid |= uapi::FATTR_MTIME;
1591        }
1592        if has.access_time {
1593            valid |= uapi::FATTR_ATIME;
1594        }
1595        if has.mode {
1596            valid |= uapi::FATTR_MODE;
1597        }
1598        if has.uid {
1599            valid |= uapi::FATTR_UID;
1600        }
1601        if has.gid {
1602            valid |= uapi::FATTR_GID;
1603        }
1604
1605        let attributes = uapi::fuse_setattr_in {
1606            valid,
1607            atime: (info.time_access.into_nanos() / NANOS_PER_SECOND) as u64,
1608            mtime: (info.time_modify.into_nanos() / NANOS_PER_SECOND) as u64,
1609            ctime: (info.time_status_change.into_nanos() / NANOS_PER_SECOND) as u64,
1610            atimensec: (info.time_access.into_nanos() % NANOS_PER_SECOND) as u32,
1611            mtimensec: (info.time_modify.into_nanos() % NANOS_PER_SECOND) as u32,
1612            ctimensec: (info.time_status_change.into_nanos() % NANOS_PER_SECOND) as u32,
1613            mode: info.mode.bits(),
1614            uid: info.uid,
1615            gid: info.gid,
1616            ..Default::default()
1617        };
1618
1619        let response = self.connection.lock().execute_operation(
1620            locked,
1621            current_task,
1622            self,
1623            FuseOperation::SetAttr(attributes),
1624        )?;
1625        if let FuseResponse::Attr(_attr) = response { Ok(()) } else { error!(EINVAL) }
1626    }
1627
1628    fn get_xattr(
1629        &self,
1630        locked: &mut Locked<FileOpsCore>,
1631        _node: &FsNode,
1632        current_task: &CurrentTask,
1633        name: &FsStr,
1634        max_size: usize,
1635    ) -> Result<ValueOrSize<FsString>, Errno> {
1636        let response = self.connection.lock().execute_operation(
1637            locked,
1638            current_task,
1639            self,
1640            FuseOperation::GetXAttr {
1641                getxattr_in: uapi::fuse_getxattr_in {
1642                    size: max_size.try_into().map_err(|_| errno!(EINVAL))?,
1643                    padding: 0,
1644                },
1645                name: name.to_owned(),
1646            },
1647        )?;
1648        if let FuseResponse::GetXAttr(result) = response { Ok(result) } else { error!(EINVAL) }
1649    }
1650
1651    fn set_xattr(
1652        &self,
1653        locked: &mut Locked<FileOpsCore>,
1654        _node: &FsNode,
1655        current_task: &CurrentTask,
1656        name: &FsStr,
1657        value: &FsStr,
1658        op: XattrOp,
1659    ) -> Result<(), Errno> {
1660        let mut state = self.connection.lock();
1661        let configuration = state.get_configuration(locked, current_task)?;
1662        state.execute_operation(
1663            locked,
1664            current_task,
1665            self,
1666            FuseOperation::SetXAttr {
1667                setxattr_in: uapi::fuse_setxattr_in {
1668                    size: value.len().try_into().map_err(|_| errno!(EINVAL))?,
1669                    flags: op.into_flags(),
1670                    setxattr_flags: 0,
1671                    padding: 0,
1672                },
1673                is_ext: configuration.flags.contains(FuseInitFlags::SETXATTR_EXT),
1674                name: name.to_owned(),
1675                value: value.to_owned(),
1676            },
1677        )?;
1678        Ok(())
1679    }
1680
1681    fn remove_xattr(
1682        &self,
1683        locked: &mut Locked<FileOpsCore>,
1684        _node: &FsNode,
1685        current_task: &CurrentTask,
1686        name: &FsStr,
1687    ) -> Result<(), Errno> {
1688        self.connection.lock().execute_operation(
1689            locked,
1690            current_task,
1691            self,
1692            FuseOperation::RemoveXAttr { name: name.to_owned() },
1693        )?;
1694        Ok(())
1695    }
1696
1697    fn list_xattrs(
1698        &self,
1699        locked: &mut Locked<FileOpsCore>,
1700        _node: &FsNode,
1701        current_task: &CurrentTask,
1702        max_size: usize,
1703    ) -> Result<ValueOrSize<Vec<FsString>>, Errno> {
1704        let response = self.connection.lock().execute_operation(
1705            locked,
1706            current_task,
1707            self,
1708            FuseOperation::ListXAttr(uapi::fuse_getxattr_in {
1709                size: max_size.try_into().map_err(|_| errno!(EINVAL))?,
1710                padding: 0,
1711            }),
1712        )?;
1713        if let FuseResponse::GetXAttr(result) = response {
1714            Ok(result.map(|s| {
1715                let mut result = s.split(|c| *c == 0).map(FsString::from).collect::<Vec<_>>();
1716                // The returned string ends with a '\0', so the split ends with an empty value that
1717                // needs to be removed.
1718                result.pop();
1719                result
1720            }))
1721        } else {
1722            error!(EINVAL)
1723        }
1724    }
1725
1726    fn forget(
1727        self: Box<Self>,
1728        locked: &mut Locked<FileOpsCore>,
1729        current_task: &CurrentTask,
1730        _info: FsNodeInfo,
1731    ) -> Result<(), Errno> {
1732        let nlookup = self.state.lock().nlookup;
1733        let mut state = self.connection.lock();
1734        if !state.is_connected() {
1735            return Ok(());
1736        }
1737        if nlookup > 0 {
1738            state.execute_operation(
1739                locked,
1740                current_task,
1741                self.as_ref(),
1742                FuseOperation::Forget(uapi::fuse_forget_in { nlookup }),
1743            )?;
1744        };
1745        Ok(())
1746    }
1747
1748    fn node_key(&self, _node: &FsNode) -> ino_t {
1749        self.nodeid
1750    }
1751}
1752
1753/// The state of the connection to the /dev/fuse file descriptor.
1754#[derive(Debug, Default)]
1755enum FuseConnectionState {
1756    #[default]
1757    /// The /dev/fuse device has been opened, but the filesystem has not been mounted yet.
1758    Waiting,
1759    /// The file system is mounted.
1760    Connected,
1761    /// The file system has been unmounted.
1762    Disconnected,
1763}
1764
1765#[derive(Debug)]
1766struct FuseConnection {
1767    /// Connection identifier for fusectl.
1768    id: u64,
1769
1770    /// Credentials of the task that opened the connection.
1771    creds: FsCred,
1772
1773    /// Mutable state of the connection.
1774    state: Mutex<FuseMutableState>,
1775}
1776
1777struct FuseMutableStateGuard<'a>(Guard<'a, FuseConnection, MutexGuard<'a, FuseMutableState>>);
1778
1779impl<'a> Deref for FuseMutableStateGuard<'a> {
1780    type Target = Guard<'a, FuseConnection, MutexGuard<'a, FuseMutableState>>;
1781    fn deref(&self) -> &Self::Target {
1782        &self.0
1783    }
1784}
1785
1786impl<'a> DerefMut for FuseMutableStateGuard<'a> {
1787    fn deref_mut(&mut self) -> &mut Self::Target {
1788        &mut self.0
1789    }
1790}
1791
1792impl FuseConnection {
1793    fn lock<'a>(&'a self) -> FuseMutableStateGuard<'a> {
1794        FuseMutableStateGuard(Guard::<'a, FuseConnection, MutexGuard<'a, FuseMutableState>>::new(
1795            self,
1796            self.state.lock(),
1797        ))
1798    }
1799}
1800
1801#[derive(Clone, Copy, Debug)]
1802struct FuseConfiguration {
1803    flags: FuseInitFlags,
1804}
1805
1806impl TryFrom<uapi::fuse_init_out> for FuseConfiguration {
1807    type Error = Errno;
1808    fn try_from(init_out: uapi::fuse_init_out) -> Result<Self, Errno> {
1809        let flags = FuseInitFlags::try_from(init_out)?;
1810        Ok(Self { flags })
1811    }
1812}
1813
1814/// A per connection state for operations that can be shortcircuited.
1815///
1816/// For a number of Fuse operation, Fuse protocol specifies that if they fail in a specific way,
1817/// they should not be sent to the server again and must be handled in a predefined way. This
1818/// map keep track of these operations for a given connection. If this map contains a result for a
1819/// given opcode, any further attempt to send this opcode to userspace will be answered with the
1820/// content of the map.
1821type OperationsState = HashMap<uapi::fuse_opcode, Result<FuseResponse, Errno>>;
1822
1823#[derive(Debug, Default)]
1824struct FuseMutableState {
1825    /// The state of the mount.
1826    state: FuseConnectionState,
1827
1828    /// Last unique id used to identify messages between the kernel and user space.
1829    last_unique_id: u64,
1830
1831    /// The configuration, negotiated with the client.
1832    configuration: Option<FuseConfiguration>,
1833
1834    /// In progress operations.
1835    operations: HashMap<u64, RunningOperation>,
1836
1837    /// Enqueued messages. These messages have not yet been sent to userspace. There should be
1838    /// multiple queues, but for now, push every messages to the same queue.
1839    /// New messages are added at the end of the queues. Read consume from the front of the queue.
1840    message_queue: VecDeque<FuseKernelMessage>,
1841
1842    /// Queue to notify of new messages.
1843    waiters: WaitQueue,
1844
1845    /// The state of the different operations, to allow short-circuiting the userspace process.
1846    operations_state: OperationsState,
1847
1848    /// If true, then the create operation is not supported in which case mknod will be used.
1849    no_create: bool,
1850
1851    /// Last used id for registered fd for passthrough.
1852    last_passthrough_id: u32,
1853
1854    /// All currently registered fd for passthrough, associated with their id. The elements are
1855    /// cleared when used and regularly to check for closed files.
1856    registered_passthrough: HashMap<u32, WeakFileHandle>,
1857}
1858
1859impl<'a> FuseMutableStateGuard<'a> {
1860    fn wait_for_configuration<L, T>(
1861        &mut self,
1862        locked: &mut Locked<L>,
1863        current_task: &CurrentTask,
1864        f: impl Fn(&FuseConfiguration) -> T,
1865    ) -> Result<T, Errno>
1866    where
1867        L: LockEqualOrBefore<FileOpsCore>,
1868    {
1869        if let Some(configuration) = self.configuration.as_ref() {
1870            return Ok(f(configuration));
1871        }
1872        loop {
1873            if !self.is_connected() {
1874                return error!(ECONNABORTED);
1875            }
1876            let waiter = Waiter::new();
1877            self.waiters.wait_async_value(&waiter, CONFIGURATION_AVAILABLE_EVENT);
1878            if let Some(configuration) = self.configuration.as_ref() {
1879                return Ok(f(configuration));
1880            }
1881            Guard::<'a, FuseConnection, MutexGuard<'a, FuseMutableState>>::unlocked(self, || {
1882                waiter.wait(locked, current_task)
1883            })?;
1884        }
1885    }
1886
1887    fn get_configuration<L>(
1888        &mut self,
1889        locked: &mut Locked<L>,
1890        current_task: &CurrentTask,
1891    ) -> Result<FuseConfiguration, Errno>
1892    where
1893        L: LockEqualOrBefore<FileOpsCore>,
1894    {
1895        self.wait_for_configuration(locked, current_task, Clone::clone)
1896    }
1897
1898    fn wait_for_configuration_ready<L>(
1899        &mut self,
1900        locked: &mut Locked<L>,
1901        current_task: &CurrentTask,
1902    ) -> Result<(), Errno>
1903    where
1904        L: LockEqualOrBefore<FileOpsCore>,
1905    {
1906        self.wait_for_configuration(locked, current_task, |_| ())
1907    }
1908
1909    /// Execute the given operation on the `node`. If the operation is not asynchronous, this
1910    /// method will wait on the userspace process for a response. If the operation is interrupted,
1911    /// an interrupt will be sent to the userspace process and the operation will then block until
1912    /// the initial operation has a response. This block can only be interrupted by the filesystem
1913    /// being unmounted.
1914    fn execute_operation<L>(
1915        &mut self,
1916        locked: &mut Locked<L>,
1917        current_task: &CurrentTask,
1918        node: &FuseNode,
1919        operation: FuseOperation,
1920    ) -> Result<FuseResponse, Errno>
1921    where
1922        L: LockEqualOrBefore<FileOpsCore>,
1923    {
1924        // Block until we have a valid configuration to make sure that the FUSE
1925        // implementation has initialized, indicated by its response to the
1926        // `FUSE_INIT` request. Obviously, we skip this check for the `FUSE_INIT`
1927        // request itself.
1928        if !matches!(operation, FuseOperation::Init { .. }) {
1929            self.wait_for_configuration_ready(locked, current_task)?;
1930        }
1931
1932        if let Some(result) = self.operations_state.get(&operation.opcode()) {
1933            return result.clone();
1934        }
1935        if !operation.has_response() {
1936            self.queue_operation(current_task, node.nodeid, operation, None)?;
1937            return Ok(FuseResponse::None);
1938        }
1939        let waiter = Waiter::with_options(WaiterOptions::UNSAFE_CALLSTACK);
1940        let is_async = operation.is_async();
1941        let unique_id =
1942            self.queue_operation(current_task, node.nodeid, operation, Some(&waiter))?;
1943        if is_async {
1944            return Ok(FuseResponse::None);
1945        }
1946        let mut first_loop = true;
1947        loop {
1948            if !self.is_connected() {
1949                return error!(ECONNABORTED);
1950            }
1951            if let Some(response) = self.get_response(unique_id) {
1952                return response;
1953            }
1954            match Guard::<'a, FuseConnection, MutexGuard<'a, FuseMutableState>>::unlocked(
1955                self,
1956                || waiter.wait(locked, current_task),
1957            ) {
1958                Ok(()) => {}
1959                Err(e) if e == EINTR => {
1960                    // If interrupted by another process, send an interrupt command to the server
1961                    // the first time, then wait unconditionally.
1962                    if first_loop {
1963                        self.interrupt(current_task, node.nodeid, unique_id)?;
1964                        first_loop = false;
1965                    }
1966                }
1967                Err(e) => {
1968                    log_error!("Unexpected error: {e:?}");
1969                    return Err(e);
1970                }
1971            }
1972        }
1973    }
1974}
1975
1976impl FuseMutableState {
1977    fn wait_async(
1978        &self,
1979        waiter: &Waiter,
1980        events: FdEvents,
1981        handler: EventHandler,
1982    ) -> Option<WaitCanceler> {
1983        Some(self.waiters.wait_async_fd_events(waiter, events, handler))
1984    }
1985
1986    fn is_connected(&self) -> bool {
1987        matches!(self.state, FuseConnectionState::Connected)
1988    }
1989
1990    fn set_configuration(&mut self, configuration: FuseConfiguration) {
1991        debug_assert!(self.configuration.is_none());
1992        log_trace!("Fuse configuration: {configuration:?}");
1993        self.configuration = Some(configuration);
1994        self.waiters.notify_value(CONFIGURATION_AVAILABLE_EVENT);
1995    }
1996
1997    fn connect(&mut self) {
1998        debug_assert!(matches!(self.state, FuseConnectionState::Waiting));
1999        self.state = FuseConnectionState::Connected;
2000    }
2001
2002    /// Disconnect the mount. Happens on unmount. Every filesystem operation will fail with
2003    /// ECONNABORTED, and every read/write on the /dev/fuse fd will fail with ENODEV.
2004    fn disconnect(&mut self) {
2005        if matches!(self.state, FuseConnectionState::Disconnected) {
2006            return;
2007        }
2008        self.state = FuseConnectionState::Disconnected;
2009        self.message_queue.clear();
2010        self.operations.clear();
2011        self.waiters.notify_all();
2012    }
2013
2014    /// Queue the given operation on the internal queue for the userspace daemon to read. If
2015    /// `waiter` is not None, register `waiter` to be notified when userspace responds to the
2016    /// operation. This should only be used if the operation expects a response.
2017    fn queue_operation(
2018        &mut self,
2019        current_task: &CurrentTask,
2020        nodeid: u64,
2021        operation: FuseOperation,
2022        waiter: Option<&Waiter>,
2023    ) -> Result<u64, Errno> {
2024        debug_assert!(waiter.is_some() == operation.has_response(), "{operation:?}");
2025        if !self.is_connected() {
2026            return error!(ECONNABORTED);
2027        }
2028        self.last_unique_id += 1;
2029        let message = FuseKernelMessage::new(self.last_unique_id, current_task, nodeid, operation)?;
2030        if let Some(waiter) = waiter {
2031            self.waiters.wait_async_value(waiter, self.last_unique_id);
2032        }
2033        if message.operation.has_response() {
2034            self.operations.insert(self.last_unique_id, message.operation.as_running().into());
2035        }
2036        self.message_queue.push_back(message);
2037        self.waiters.notify_fd_events(FdEvents::POLLIN);
2038        Ok(self.last_unique_id)
2039    }
2040
2041    /// Interrupt the operation with the given unique_id.
2042    ///
2043    /// If the operation is still enqueued, this will immediately dequeue the operation and return
2044    /// with an EINTR error.
2045    ///
2046    /// If not, it will send an interrupt operation.
2047    fn interrupt(
2048        &mut self,
2049        current_task: &CurrentTask,
2050        nodeid: u64,
2051        unique_id: u64,
2052    ) -> Result<(), Errno> {
2053        debug_assert!(self.operations.contains_key(&unique_id));
2054
2055        let mut in_queue = false;
2056        self.message_queue.retain(|m| {
2057            if m.header.unique == unique_id {
2058                self.operations.remove(&unique_id);
2059                in_queue = true;
2060                false
2061            } else {
2062                true
2063            }
2064        });
2065        if in_queue {
2066            // Nothing to do, the operation has been cancelled before being sent.
2067            return error!(EINTR);
2068        }
2069        self.queue_operation(current_task, nodeid, FuseOperation::Interrupt { unique_id }, None)
2070            .map(|_| ())
2071    }
2072
2073    /// Returns the response for the operation with the given identifier. Returns None if the
2074    /// operation is still in flight.
2075    fn get_response(&mut self, unique_id: u64) -> Option<Result<FuseResponse, Errno>> {
2076        match self.operations.entry(unique_id) {
2077            Entry::Vacant(_) => Some(error!(EINVAL)),
2078            Entry::Occupied(mut entry) => {
2079                let result = entry.get_mut().response.take();
2080                if result.is_some() {
2081                    entry.remove();
2082                }
2083                result
2084            }
2085        }
2086    }
2087
2088    fn query_events(&self) -> FdEvents {
2089        let mut events = FdEvents::POLLOUT;
2090        if !self.is_connected() || !self.message_queue.is_empty() {
2091            events |= FdEvents::POLLIN
2092        };
2093        if !self.is_connected() {
2094            events |= FdEvents::POLLERR;
2095        }
2096        events
2097    }
2098
2099    fn read(&mut self, data: &mut dyn OutputBuffer) -> Result<usize, Errno> {
2100        match self.state {
2101            FuseConnectionState::Waiting => return error!(EPERM),
2102            FuseConnectionState::Disconnected => return error!(ENODEV),
2103            _ => {}
2104        }
2105        if let Some(message) = self.message_queue.pop_front() {
2106            message.serialize(data)
2107        } else {
2108            error!(EAGAIN)
2109        }
2110    }
2111
2112    fn write(&mut self, data: &mut dyn InputBuffer) -> Result<usize, Errno> {
2113        match self.state {
2114            FuseConnectionState::Waiting => return error!(EPERM),
2115            FuseConnectionState::Disconnected => return error!(ENODEV),
2116            _ => {}
2117        }
2118        let header: uapi::fuse_out_header = data.read_to_object()?;
2119        let payload_size = (header.len as usize)
2120            .checked_sub(std::mem::size_of::<uapi::fuse_out_header>())
2121            .ok_or_else(|| errno!(EINVAL))?;
2122        if payload_size > data.available() {
2123            return error!(EINVAL);
2124        }
2125        if header.unique == 0 {
2126            track_stub!(TODO("https://fxbug.dev/322873416"), "Fuse notification from userspace");
2127            return error!(ENOTSUP);
2128        }
2129        self.waiters.notify_value(header.unique);
2130        let mut running_operation = match self.operations.entry(header.unique) {
2131            Entry::Occupied(e) => e,
2132            Entry::Vacant(_) => return error!(EINVAL),
2133        };
2134        let operation = &running_operation.get().operation;
2135        let is_async = operation.is_async();
2136        if header.error < 0 {
2137            log_trace!("Fuse: {operation:?} -> {header:?}");
2138            let code = i16::try_from(-header.error).unwrap_or_else(|_| EINVAL.error_code() as i16);
2139            let errno = errno_from_code!(code);
2140            let response = operation.handle_error(&mut self.operations_state, errno);
2141            if is_async {
2142                running_operation.remove();
2143            } else {
2144                running_operation.get_mut().response = Some(response);
2145            }
2146        } else {
2147            let buffer = data.read_to_vec_limited(payload_size)?;
2148            if buffer.len() != payload_size {
2149                return error!(EINVAL);
2150            }
2151            let response = operation.parse_response(buffer)?;
2152            log_trace!("Fuse: {operation:?} -> {response:?}");
2153            if is_async {
2154                let operation = running_operation.remove();
2155                self.handle_async(operation, response)?;
2156            } else {
2157                running_operation.get_mut().response = Some(Ok(response));
2158            }
2159        }
2160        Ok(data.bytes_read())
2161    }
2162
2163    fn handle_async(
2164        &mut self,
2165        operation: RunningOperation,
2166        response: FuseResponse,
2167    ) -> Result<(), Errno> {
2168        match (operation.operation, response) {
2169            (RunningOperationKind::Init { fs }, FuseResponse::Init(init_out)) => {
2170                let configuration = FuseConfiguration::try_from(init_out)?;
2171                if configuration.flags.contains(FuseInitFlags::POSIX_ACL) {
2172                    // Per libfuse's documentation on `FUSE_CAP_POSIX_ACL`,
2173                    // the POSIX_ACL flag implicitly enables the
2174                    // `default_permissions` mount option.
2175                    if let Some(fs) = fs.upgrade() {
2176                        FuseFs::from_fs(&fs)
2177                            .default_permissions
2178                            .store(true, DEFAULT_PERMISSIONS_ATOMIC_ORDERING)
2179                    } else {
2180                        log_warn!("failed to upgrade FuseFs when handling FUSE_INIT response");
2181                        return error!(ENOTCONN);
2182                    }
2183                }
2184                self.set_configuration(configuration);
2185                Ok(())
2186            }
2187            operation => {
2188                // Init is the only async operation.
2189                panic!("Incompatible operation={operation:?}");
2190            }
2191        }
2192    }
2193
2194    fn clear_released_passthrough_fds(&mut self) {
2195        self.registered_passthrough.retain(|_, s| s.strong_count() > 0);
2196    }
2197}
2198
2199/// An operation that is either queued to be send to userspace, or already sent to userspace and
2200/// waiting for a response.
2201#[derive(Debug)]
2202struct RunningOperation {
2203    operation: RunningOperationKind,
2204    response: Option<Result<FuseResponse, Errno>>,
2205}
2206
2207impl From<RunningOperationKind> for RunningOperation {
2208    fn from(operation: RunningOperationKind) -> Self {
2209        Self { operation, response: None }
2210    }
2211}
2212
2213#[derive(Debug)]
2214struct FuseKernelMessage {
2215    header: uapi::fuse_in_header,
2216    operation: FuseOperation,
2217}
2218
2219impl FuseKernelMessage {
2220    fn new(
2221        unique: u64,
2222        current_task: &CurrentTask,
2223        nodeid: u64,
2224        operation: FuseOperation,
2225    ) -> Result<Self, Errno> {
2226        let current_creds = current_task.current_creds();
2227        Ok(Self {
2228            header: uapi::fuse_in_header {
2229                len: u32::try_from(std::mem::size_of::<uapi::fuse_in_header>() + operation.len())
2230                    .map_err(|_| errno!(EINVAL))?,
2231                opcode: operation.opcode(),
2232                unique,
2233                nodeid,
2234                uid: current_creds.uid,
2235                gid: current_creds.gid,
2236                pid: current_task.get_tid() as u32,
2237                __bindgen_anon_1: Default::default(),
2238            },
2239            operation,
2240        })
2241    }
2242
2243    fn serialize(&self, data: &mut dyn OutputBuffer) -> Result<usize, Errno> {
2244        let size = data.write(self.header.as_bytes())?;
2245        Ok(size + self.operation.serialize(data)?)
2246    }
2247}
2248
2249bitflags::bitflags! {
2250    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2251    pub struct FuseInitFlags : u64 {
2252        const BIG_WRITES = uapi::FUSE_BIG_WRITES as u64;
2253        const DONT_MASK = uapi::FUSE_DONT_MASK as u64;
2254        const SPLICE_WRITE = uapi::FUSE_SPLICE_WRITE as u64;
2255        const SPLICE_MOVE = uapi::FUSE_SPLICE_MOVE as u64;
2256        const SPLICE_READ = uapi::FUSE_SPLICE_READ as u64;
2257        const DO_READDIRPLUS = uapi::FUSE_DO_READDIRPLUS as u64;
2258        const READDIRPLUS_AUTO = uapi::FUSE_READDIRPLUS_AUTO as u64;
2259        const SETXATTR_EXT = uapi::FUSE_SETXATTR_EXT as u64;
2260        const POSIX_ACL = uapi::FUSE_POSIX_ACL as u64;
2261        const PASSTHROUGH = uapi::FUSE_PASSTHROUGH as u64;
2262        const INIT_EXT = uapi::FUSE_INIT_EXT as u64;
2263    }
2264}
2265
2266impl TryFrom<uapi::fuse_init_out> for FuseInitFlags {
2267    type Error = Errno;
2268    fn try_from(init_out: uapi::fuse_init_out) -> Result<Self, Errno> {
2269        let flags = (init_out.flags as u64) | ((init_out.flags2 as u64) << 32);
2270        let unknown_flags = flags & !Self::all().bits();
2271        if unknown_flags != 0 {
2272            track_stub!(
2273                TODO("https://fxbug.dev/322875725"),
2274                "FUSE unknown init flags",
2275                unknown_flags
2276            );
2277            log_warn!("FUSE daemon requested unknown flags in init: {unknown_flags}");
2278        }
2279        Ok(Self::from_bits_truncate(flags))
2280    }
2281}
2282
2283impl FuseInitFlags {
2284    /// Returns the 2 u32 components for these flags. The lowest part is returned first.
2285    fn get_u32_components(&self) -> (u32, u32) {
2286        let flags = (self.bits() & (u32::max_value() as u64)) as u32;
2287        let flags2 = (self.bits() >> 32) as u32;
2288        (flags, flags2)
2289    }
2290}
2291
2292#[derive(Clone, Debug)]
2293enum RunningOperationKind {
2294    Access,
2295    Create,
2296    Flush,
2297    Forget,
2298    GetAttr,
2299    Init {
2300        /// The FUSE fs that triggered this operation.
2301        ///
2302        /// `Weak` because the `FuseFs` holds an `Arc<FuseConnection>` which
2303        /// may hold this operation.
2304        fs: Weak<FileSystem>,
2305    },
2306    Interrupt,
2307    GetXAttr {
2308        size: u32,
2309    },
2310    ListXAttr {
2311        size: u32,
2312    },
2313    Lookup,
2314    Mkdir,
2315    Mknod,
2316    Link,
2317    Open {
2318        dir: bool,
2319    },
2320    Poll,
2321    Read,
2322    Readdir {
2323        use_readdirplus: bool,
2324    },
2325    Readlink,
2326    Release {
2327        dir: bool,
2328    },
2329    RemoveXAttr,
2330    Rename,
2331    Rmdir,
2332    Seek,
2333    SetAttr,
2334    SetXAttr,
2335    Statfs,
2336    Symlink,
2337    Unlink,
2338    Write,
2339}
2340
2341impl RunningOperationKind {
2342    fn is_async(&self) -> bool {
2343        matches!(self, Self::Init { .. })
2344    }
2345
2346    fn opcode(&self) -> u32 {
2347        match self {
2348            Self::Access => uapi::fuse_opcode_FUSE_ACCESS,
2349            Self::Create => uapi::fuse_opcode_FUSE_CREATE,
2350            Self::Flush => uapi::fuse_opcode_FUSE_FLUSH,
2351            Self::Forget => uapi::fuse_opcode_FUSE_FORGET,
2352            Self::GetAttr => uapi::fuse_opcode_FUSE_GETATTR,
2353            Self::GetXAttr { .. } => uapi::fuse_opcode_FUSE_GETXATTR,
2354            Self::Init { .. } => uapi::fuse_opcode_FUSE_INIT,
2355            Self::Interrupt => uapi::fuse_opcode_FUSE_INTERRUPT,
2356            Self::ListXAttr { .. } => uapi::fuse_opcode_FUSE_LISTXATTR,
2357            Self::Lookup => uapi::fuse_opcode_FUSE_LOOKUP,
2358            Self::Mkdir => uapi::fuse_opcode_FUSE_MKDIR,
2359            Self::Mknod => uapi::fuse_opcode_FUSE_MKNOD,
2360            Self::Link => uapi::fuse_opcode_FUSE_LINK,
2361            Self::Open { dir } => {
2362                if *dir {
2363                    uapi::fuse_opcode_FUSE_OPENDIR
2364                } else {
2365                    uapi::fuse_opcode_FUSE_OPEN
2366                }
2367            }
2368            Self::Poll => uapi::fuse_opcode_FUSE_POLL,
2369            Self::Read => uapi::fuse_opcode_FUSE_READ,
2370            Self::Readdir { use_readdirplus } => {
2371                if *use_readdirplus {
2372                    uapi::fuse_opcode_FUSE_READDIRPLUS
2373                } else {
2374                    uapi::fuse_opcode_FUSE_READDIR
2375                }
2376            }
2377            Self::Readlink => uapi::fuse_opcode_FUSE_READLINK,
2378            Self::Release { dir } => {
2379                if *dir {
2380                    uapi::fuse_opcode_FUSE_RELEASEDIR
2381                } else {
2382                    uapi::fuse_opcode_FUSE_RELEASE
2383                }
2384            }
2385            Self::RemoveXAttr => uapi::fuse_opcode_FUSE_REMOVEXATTR,
2386            Self::Rename => uapi::fuse_opcode_FUSE_RENAME2,
2387            Self::Rmdir => uapi::fuse_opcode_FUSE_RMDIR,
2388            Self::Seek => uapi::fuse_opcode_FUSE_LSEEK,
2389            Self::SetAttr => uapi::fuse_opcode_FUSE_SETATTR,
2390            Self::SetXAttr => uapi::fuse_opcode_FUSE_SETXATTR,
2391            Self::Statfs => uapi::fuse_opcode_FUSE_STATFS,
2392            Self::Symlink => uapi::fuse_opcode_FUSE_SYMLINK,
2393            Self::Unlink => uapi::fuse_opcode_FUSE_UNLINK,
2394            Self::Write => uapi::fuse_opcode_FUSE_WRITE,
2395        }
2396    }
2397
2398    fn to_response<T: FromBytes + IntoBytes + Immutable>(buffer: &[u8]) -> T {
2399        let mut result = T::new_zeroed();
2400        let length_to_copy = std::cmp::min(buffer.len(), std::mem::size_of::<T>());
2401        result.as_mut_bytes()[..length_to_copy].copy_from_slice(&buffer[..length_to_copy]);
2402        result
2403    }
2404
2405    fn parse_response(&self, buffer: Vec<u8>) -> Result<FuseResponse, Errno> {
2406        match self {
2407            Self::Access => Ok(FuseResponse::Access(Ok(()))),
2408            Self::Create { .. } => {
2409                Ok(FuseResponse::Create(Self::to_response::<CreateResponse>(&buffer)))
2410            }
2411            Self::GetAttr | Self::SetAttr => {
2412                Ok(FuseResponse::Attr(Self::to_response::<uapi::fuse_attr_out>(&buffer)))
2413            }
2414            Self::GetXAttr { size } | Self::ListXAttr { size } => {
2415                if *size == 0 {
2416                    if buffer.len() < std::mem::size_of::<uapi::fuse_getxattr_out>() {
2417                        return error!(EINVAL);
2418                    }
2419                    let getxattr_out = Self::to_response::<uapi::fuse_getxattr_out>(&buffer);
2420                    Ok(FuseResponse::GetXAttr(ValueOrSize::Size(getxattr_out.size as usize)))
2421                } else {
2422                    Ok(FuseResponse::GetXAttr(FsString::new(buffer).into()))
2423                }
2424            }
2425            Self::Init { .. } => {
2426                Ok(FuseResponse::Init(Self::to_response::<uapi::fuse_init_out>(&buffer)))
2427            }
2428            Self::Lookup | Self::Mkdir | Self::Mknod | Self::Link | Self::Symlink => {
2429                Ok(FuseResponse::Entry(Self::to_response::<FuseEntryOutExtended>(&buffer)))
2430            }
2431            Self::Open { .. } => {
2432                Ok(FuseResponse::Open(Self::to_response::<uapi::fuse_open_out>(&buffer)))
2433            }
2434            Self::Poll => Ok(FuseResponse::Poll(Self::to_response::<uapi::fuse_poll_out>(&buffer))),
2435            Self::Read | Self::Readlink => Ok(FuseResponse::Read(buffer)),
2436            Self::Readdir { use_readdirplus, .. } => {
2437                let mut result = vec![];
2438                let mut slice = &buffer[..];
2439                while !slice.is_empty() {
2440                    // If using READDIRPLUS, the data starts with the entry.
2441                    let entry = if *use_readdirplus {
2442                        if slice.len() < std::mem::size_of::<uapi::fuse_entry_out>() {
2443                            return error!(EINVAL);
2444                        }
2445                        let entry = Self::to_response::<uapi::fuse_entry_out>(slice);
2446                        slice = &slice[std::mem::size_of::<uapi::fuse_entry_out>()..];
2447                        Some(entry)
2448                    } else {
2449                        None
2450                    };
2451                    // The next item is the dirent.
2452                    if slice.len() < std::mem::size_of::<uapi::fuse_dirent>() {
2453                        return error!(EINVAL);
2454                    }
2455                    let dirent = Self::to_response::<uapi::fuse_dirent>(slice);
2456                    // And it ends with the name.
2457                    slice = &slice[std::mem::size_of::<uapi::fuse_dirent>()..];
2458                    let namelen = dirent.namelen as usize;
2459                    if slice.len() < namelen {
2460                        return error!(EINVAL);
2461                    }
2462                    let name = FsString::from(&slice[..namelen]);
2463                    result.push((dirent, name, entry));
2464                    let skipped = round_up_to_increment(namelen, 8)?;
2465                    if slice.len() < skipped {
2466                        return error!(EINVAL);
2467                    }
2468                    slice = &slice[skipped..];
2469                }
2470                Ok(FuseResponse::Readdir(result))
2471            }
2472            Self::Flush
2473            | Self::Release { .. }
2474            | Self::RemoveXAttr
2475            | Self::Rename
2476            | Self::Rmdir
2477            | Self::SetXAttr
2478            | Self::Unlink => Ok(FuseResponse::None),
2479            Self::Statfs => {
2480                Ok(FuseResponse::Statfs(Self::to_response::<uapi::fuse_statfs_out>(&buffer)))
2481            }
2482            Self::Seek => {
2483                Ok(FuseResponse::Seek(Self::to_response::<uapi::fuse_lseek_out>(&buffer)))
2484            }
2485            Self::Write => {
2486                Ok(FuseResponse::Write(Self::to_response::<uapi::fuse_write_out>(&buffer)))
2487            }
2488            Self::Interrupt | Self::Forget => {
2489                panic!("Response for operation without one");
2490            }
2491        }
2492    }
2493
2494    /// Handles an error from the userspace daemon.
2495    ///
2496    /// Given the `errno` returned by the userspace daemon, returns the response the caller should
2497    /// see. This can also update the `OperationState` to allow shortcircuit on future requests.
2498    fn handle_error(
2499        &self,
2500        state: &mut OperationsState,
2501        errno: Errno,
2502    ) -> Result<FuseResponse, Errno> {
2503        match self {
2504            Self::Access if errno == ENOSYS => {
2505                // Per libfuse, ENOSYS is interpreted as a "permanent success"
2506                // so we don't need to do anything further, including performing
2507                // the default/standard file permission checks like we do
2508                // when the `default_permissions` mount option is set.
2509                const UNIMPLEMENTED_ACCESS_RESPONSE: Result<FuseResponse, Errno> =
2510                    Ok(FuseResponse::Access(Ok(())));
2511                state.insert(self.opcode(), UNIMPLEMENTED_ACCESS_RESPONSE);
2512                UNIMPLEMENTED_ACCESS_RESPONSE
2513            }
2514            Self::Flush if errno == ENOSYS => {
2515                state.insert(self.opcode(), Ok(FuseResponse::None));
2516                Ok(FuseResponse::None)
2517            }
2518            Self::Seek if errno == ENOSYS => {
2519                state.insert(self.opcode(), Err(errno.clone()));
2520                Err(errno)
2521            }
2522            Self::Poll if errno == ENOSYS => {
2523                let response = FuseResponse::Poll(uapi::fuse_poll_out {
2524                    revents: (FdEvents::POLLIN | FdEvents::POLLOUT).bits(),
2525                    padding: 0,
2526                });
2527                state.insert(self.opcode(), Ok(response.clone()));
2528                Ok(response)
2529            }
2530            _ => Err(errno),
2531        }
2532    }
2533}
2534
2535#[derive(Debug)]
2536enum FuseOperation {
2537    Access {
2538        mask: u32,
2539    },
2540    Create(uapi::fuse_create_in, FsString),
2541    Flush(uapi::fuse_open_out),
2542    Forget(uapi::fuse_forget_in),
2543    GetAttr,
2544    Init {
2545        /// The FUSE fs that triggered this operation.
2546        ///
2547        /// `Weak` because the `FuseFs` holds an `Arc<FuseConnection>` which
2548        /// may hold this operation.
2549        fs: Weak<FileSystem>,
2550    },
2551    Interrupt {
2552        /// Identifier of the operation to interrupt
2553        unique_id: u64,
2554    },
2555    GetXAttr {
2556        getxattr_in: uapi::fuse_getxattr_in,
2557        /// Name of the attribute
2558        name: FsString,
2559    },
2560    ListXAttr(uapi::fuse_getxattr_in),
2561    Lookup {
2562        /// Name of the entry to lookup
2563        name: FsString,
2564    },
2565    Mkdir {
2566        mkdir_in: uapi::fuse_mkdir_in,
2567        /// Name of the entry to create
2568        name: FsString,
2569    },
2570    Mknod {
2571        mknod_in: uapi::fuse_mknod_in,
2572        /// Name of the node to create
2573        name: FsString,
2574    },
2575    Link {
2576        link_in: uapi::fuse_link_in,
2577        /// Name of the link to create
2578        name: FsString,
2579    },
2580    Open {
2581        flags: OpenFlags,
2582        mode: FileMode,
2583    },
2584    Poll(uapi::fuse_poll_in),
2585    Read(uapi::fuse_read_in),
2586    Readdir {
2587        read_in: uapi::fuse_read_in,
2588        /// Whether to use the READDIRPLUS api
2589        use_readdirplus: bool,
2590    },
2591    Readlink,
2592    Release(uapi::fuse_open_out),
2593    ReleaseDir(uapi::fuse_open_out),
2594    RemoveXAttr {
2595        /// Name of the attribute
2596        name: FsString,
2597    },
2598    Rename {
2599        old_name: FsString,
2600        new_dir: u64,
2601        new_name: FsString,
2602    },
2603    Rmdir {
2604        name: FsString,
2605    },
2606    Seek(uapi::fuse_lseek_in),
2607    SetAttr(uapi::fuse_setattr_in),
2608    SetXAttr {
2609        setxattr_in: uapi::fuse_setxattr_in,
2610        /// Indicates if userspace supports the, most-recent/extended variant of
2611        /// `fuse_setxattr_in`.
2612        is_ext: bool,
2613        /// Name of the attribute
2614        name: FsString,
2615        /// Value of the attribute
2616        value: FsString,
2617    },
2618    Statfs,
2619    Symlink {
2620        /// Target of the link
2621        target: FsString,
2622        /// Name of the link
2623        name: FsString,
2624    },
2625    Unlink {
2626        /// Name of the file to unlink
2627        name: FsString,
2628    },
2629    Write {
2630        write_in: uapi::fuse_write_in,
2631        // Content to write
2632        content: Vec<u8>,
2633    },
2634}
2635
2636#[derive(Clone, Debug)]
2637enum FuseResponse {
2638    Access(Result<(), Errno>),
2639    Attr(uapi::fuse_attr_out),
2640    Create(CreateResponse),
2641    Entry(FuseEntryOutExtended),
2642    GetXAttr(ValueOrSize<FsString>),
2643    Init(uapi::fuse_init_out),
2644    Open(uapi::fuse_open_out),
2645    Poll(uapi::fuse_poll_out),
2646    Read(
2647        // Content read
2648        Vec<u8>,
2649    ),
2650    Seek(uapi::fuse_lseek_out),
2651    Readdir(Vec<(uapi::fuse_dirent, FsString, Option<uapi::fuse_entry_out>)>),
2652    Statfs(uapi::fuse_statfs_out),
2653    Write(uapi::fuse_write_out),
2654    None,
2655}
2656
2657impl FuseResponse {
2658    fn entry(&self) -> Option<&uapi::fuse_entry_out> {
2659        if let Self::Entry(entry) = self { Some(&entry.arg) } else { None }
2660    }
2661}
2662
2663#[repr(C)]
2664#[derive(Clone, Debug, KnownLayout, FromBytes, IntoBytes, Immutable)]
2665struct CreateResponse {
2666    entry: uapi::fuse_entry_out,
2667    open: uapi::fuse_open_out,
2668}
2669
2670static_assertions::const_assert_eq!(
2671    std::mem::offset_of!(CreateResponse, open),
2672    std::mem::size_of::<uapi::fuse_entry_out>()
2673);
2674
2675#[repr(C)]
2676#[derive(Clone, Debug, KnownLayout, FromBytes, IntoBytes, Immutable)]
2677struct FuseEntryOutExtended {
2678    arg: uapi::fuse_entry_out,
2679    bpf_arg: uapi::fuse_entry_bpf_out,
2680}
2681
2682static_assertions::const_assert_eq!(
2683    std::mem::offset_of!(FuseEntryOutExtended, bpf_arg),
2684    std::mem::size_of::<uapi::fuse_entry_out>()
2685);
2686
2687impl FuseOperation {
2688    fn serialize(&self, data: &mut dyn OutputBuffer) -> Result<usize, Errno> {
2689        match self {
2690            Self::Access { mask } => {
2691                let message = uapi::fuse_access_in { mask: *mask, padding: 0 };
2692                data.write_all(message.as_bytes())
2693            }
2694            Self::Create(create_in, name) => {
2695                Ok(data.write_all(create_in.as_bytes())? + Self::write_null_terminated(data, name)?)
2696            }
2697            Self::Flush(open_in) => {
2698                let message =
2699                    uapi::fuse_flush_in { fh: open_in.fh, unused: 0, padding: 0, lock_owner: 0 };
2700                data.write_all(message.as_bytes())
2701            }
2702            Self::Forget(forget_in) => data.write_all(forget_in.as_bytes()),
2703            Self::GetAttr | Self::Readlink | Self::Statfs => Ok(0),
2704            Self::GetXAttr { getxattr_in, name } => {
2705                let mut len = data.write_all(getxattr_in.as_bytes())?;
2706                len += Self::write_null_terminated(data, name)?;
2707                Ok(len)
2708            }
2709            Self::Init { .. } => {
2710                let (flags, flags2) = FuseInitFlags::all().get_u32_components();
2711                let message = uapi::fuse_init_in {
2712                    major: uapi::FUSE_KERNEL_VERSION,
2713                    minor: uapi::FUSE_KERNEL_MINOR_VERSION,
2714                    flags,
2715                    flags2,
2716                    ..Default::default()
2717                };
2718                data.write_all(message.as_bytes())
2719            }
2720            Self::Interrupt { unique_id } => {
2721                let message = uapi::fuse_interrupt_in { unique: *unique_id };
2722                data.write_all(message.as_bytes())
2723            }
2724            Self::ListXAttr(getxattr_in) => data.write_all(getxattr_in.as_bytes()),
2725            Self::Lookup { name } => Self::write_null_terminated(data, name),
2726            Self::Open { flags, .. } => {
2727                let message = uapi::fuse_open_in { flags: flags.bits(), open_flags: 0 };
2728                data.write_all(message.as_bytes())
2729            }
2730            Self::Poll(poll_in) => data.write_all(poll_in.as_bytes()),
2731            Self::Mkdir { mkdir_in, name } => {
2732                let mut len = data.write_all(mkdir_in.as_bytes())?;
2733                len += Self::write_null_terminated(data, name)?;
2734                Ok(len)
2735            }
2736            Self::Mknod { mknod_in, name } => {
2737                let mut len = data.write_all(mknod_in.as_bytes())?;
2738                len += Self::write_null_terminated(data, name)?;
2739                Ok(len)
2740            }
2741            Self::Link { link_in, name } => {
2742                let mut len = data.write_all(link_in.as_bytes())?;
2743                len += Self::write_null_terminated(data, name)?;
2744                Ok(len)
2745            }
2746            Self::Read(read_in) | Self::Readdir { read_in, .. } => {
2747                data.write_all(read_in.as_bytes())
2748            }
2749            Self::Release(open_out) | Self::ReleaseDir(open_out) => {
2750                let message = uapi::fuse_release_in {
2751                    fh: open_out.fh,
2752                    flags: 0,
2753                    release_flags: 0,
2754                    lock_owner: 0,
2755                };
2756                data.write_all(message.as_bytes())
2757            }
2758            Self::RemoveXAttr { name } => Self::write_null_terminated(data, name),
2759            Self::Rename { old_name, new_dir, new_name } => {
2760                Ok(data.write_all(
2761                    uapi::fuse_rename2_in { newdir: *new_dir, flags: 0, padding: 0 }.as_bytes(),
2762                )? + Self::write_null_terminated(data, old_name)?
2763                    + Self::write_null_terminated(data, new_name)?)
2764            }
2765            Self::Seek(seek_in) => data.write_all(seek_in.as_bytes()),
2766            Self::SetAttr(setattr_in) => data.write_all(setattr_in.as_bytes()),
2767            Self::SetXAttr { setxattr_in, is_ext, name, value } => {
2768                let header =
2769                    if *is_ext { setxattr_in.as_bytes() } else { &setxattr_in.as_bytes()[..8] };
2770                let mut len = data.write_all(header)?;
2771                len += Self::write_null_terminated(data, name)?;
2772                len += data.write_all(value.as_bytes())?;
2773                Ok(len)
2774            }
2775            Self::Symlink { target, name } => {
2776                let mut len = Self::write_null_terminated(data, name)?;
2777                len += Self::write_null_terminated(data, target)?;
2778                Ok(len)
2779            }
2780            Self::Rmdir { name } | Self::Unlink { name } => Self::write_null_terminated(data, name),
2781            &Self::Write { mut write_in, ref content } => {
2782                let mut write_in_size = write_in.size as usize;
2783                assert!(write_in_size == content.len());
2784                if write_in_size + write_in.as_bytes().len() > data.available() {
2785                    write_in_size = data.available() - write_in.as_bytes().len();
2786                    write_in.size = write_in_size as u32;
2787                }
2788                let mut len = data.write_all(write_in.as_bytes())?;
2789                len += data.write_all(&content[..write_in_size])?;
2790                Ok(len)
2791            }
2792        }
2793    }
2794
2795    fn write_null_terminated(
2796        data: &mut dyn OutputBuffer,
2797        content: &Vec<u8>,
2798    ) -> Result<usize, Errno> {
2799        let mut len = data.write_all(content.as_bytes())?;
2800        len += data.write_all(&[0])?;
2801        Ok(len)
2802    }
2803
2804    fn opcode(&self) -> u32 {
2805        match self {
2806            Self::Access { .. } => uapi::fuse_opcode_FUSE_ACCESS,
2807            Self::Create { .. } => uapi::fuse_opcode_FUSE_CREATE,
2808            Self::Flush(_) => uapi::fuse_opcode_FUSE_FLUSH,
2809            Self::Forget(_) => uapi::fuse_opcode_FUSE_FORGET,
2810            Self::GetAttr => uapi::fuse_opcode_FUSE_GETATTR,
2811            Self::GetXAttr { .. } => uapi::fuse_opcode_FUSE_GETXATTR,
2812            Self::Init { .. } => uapi::fuse_opcode_FUSE_INIT,
2813            Self::Interrupt { .. } => uapi::fuse_opcode_FUSE_INTERRUPT,
2814            Self::ListXAttr(_) => uapi::fuse_opcode_FUSE_LISTXATTR,
2815            Self::Lookup { .. } => uapi::fuse_opcode_FUSE_LOOKUP,
2816            Self::Mkdir { .. } => uapi::fuse_opcode_FUSE_MKDIR,
2817            Self::Mknod { .. } => uapi::fuse_opcode_FUSE_MKNOD,
2818            Self::Link { .. } => uapi::fuse_opcode_FUSE_LINK,
2819            Self::Open { flags, mode } => {
2820                if mode.is_dir() || flags.contains(OpenFlags::DIRECTORY) {
2821                    uapi::fuse_opcode_FUSE_OPENDIR
2822                } else {
2823                    uapi::fuse_opcode_FUSE_OPEN
2824                }
2825            }
2826            Self::Poll(_) => uapi::fuse_opcode_FUSE_POLL,
2827            Self::Read(_) => uapi::fuse_opcode_FUSE_READ,
2828            Self::Readdir { use_readdirplus, .. } => {
2829                if *use_readdirplus {
2830                    uapi::fuse_opcode_FUSE_READDIRPLUS
2831                } else {
2832                    uapi::fuse_opcode_FUSE_READDIR
2833                }
2834            }
2835            Self::Readlink => uapi::fuse_opcode_FUSE_READLINK,
2836            Self::Release(_) => uapi::fuse_opcode_FUSE_RELEASE,
2837            Self::ReleaseDir(_) => uapi::fuse_opcode_FUSE_RELEASEDIR,
2838            Self::RemoveXAttr { .. } => uapi::fuse_opcode_FUSE_REMOVEXATTR,
2839            Self::Rename { .. } => uapi::fuse_opcode_FUSE_RENAME2,
2840            Self::Rmdir { .. } => uapi::fuse_opcode_FUSE_RMDIR,
2841            Self::Seek(_) => uapi::fuse_opcode_FUSE_LSEEK,
2842            Self::SetAttr(_) => uapi::fuse_opcode_FUSE_SETATTR,
2843            Self::SetXAttr { .. } => uapi::fuse_opcode_FUSE_SETXATTR,
2844            Self::Statfs => uapi::fuse_opcode_FUSE_STATFS,
2845            Self::Symlink { .. } => uapi::fuse_opcode_FUSE_SYMLINK,
2846            Self::Unlink { .. } => uapi::fuse_opcode_FUSE_UNLINK,
2847            Self::Write { .. } => uapi::fuse_opcode_FUSE_WRITE,
2848        }
2849    }
2850
2851    fn as_running(&self) -> RunningOperationKind {
2852        match self {
2853            Self::Access { .. } => RunningOperationKind::Access,
2854            Self::Create { .. } => RunningOperationKind::Create,
2855            Self::Flush(_) => RunningOperationKind::Flush,
2856            Self::Forget(_) => RunningOperationKind::Forget,
2857            Self::GetAttr => RunningOperationKind::GetAttr,
2858            Self::GetXAttr { getxattr_in, .. } => {
2859                RunningOperationKind::GetXAttr { size: getxattr_in.size }
2860            }
2861            Self::Init { fs } => RunningOperationKind::Init { fs: fs.clone() },
2862            Self::Interrupt { .. } => RunningOperationKind::Interrupt,
2863            Self::ListXAttr(getxattr_in) => {
2864                RunningOperationKind::ListXAttr { size: getxattr_in.size }
2865            }
2866            Self::Lookup { .. } => RunningOperationKind::Lookup,
2867            Self::Mkdir { .. } => RunningOperationKind::Mkdir,
2868            Self::Mknod { .. } => RunningOperationKind::Mknod,
2869            Self::Link { .. } => RunningOperationKind::Link,
2870            Self::Open { flags, mode } => RunningOperationKind::Open {
2871                dir: mode.is_dir() || flags.contains(OpenFlags::DIRECTORY),
2872            },
2873            Self::Poll(_) => RunningOperationKind::Poll,
2874            Self::Read(_) => RunningOperationKind::Read,
2875            Self::Readdir { use_readdirplus, .. } => {
2876                RunningOperationKind::Readdir { use_readdirplus: *use_readdirplus }
2877            }
2878            Self::Readlink => RunningOperationKind::Readlink,
2879            Self::Release(_) => RunningOperationKind::Release { dir: false },
2880            Self::ReleaseDir(_) => RunningOperationKind::Release { dir: true },
2881            Self::RemoveXAttr { .. } => RunningOperationKind::RemoveXAttr,
2882            Self::Rename { .. } => RunningOperationKind::Rename,
2883            Self::Rmdir { .. } => RunningOperationKind::Rmdir,
2884            Self::Seek(_) => RunningOperationKind::Seek,
2885            Self::SetAttr(_) => RunningOperationKind::SetAttr,
2886            Self::SetXAttr { .. } => RunningOperationKind::SetXAttr,
2887            Self::Statfs => RunningOperationKind::Statfs,
2888            Self::Symlink { .. } => RunningOperationKind::Symlink,
2889            Self::Unlink { .. } => RunningOperationKind::Unlink,
2890            Self::Write { .. } => RunningOperationKind::Write,
2891        }
2892    }
2893
2894    fn len(&self) -> usize {
2895        #[derive(Debug, Default)]
2896        struct CountingOutputBuffer {
2897            written: usize,
2898        }
2899
2900        impl Buffer for CountingOutputBuffer {
2901            fn segments_count(&self) -> Result<usize, Errno> {
2902                panic!("Should not be called");
2903            }
2904
2905            fn peek_each_segment(
2906                &mut self,
2907                _callback: &mut PeekBufferSegmentsCallback<'_>,
2908            ) -> Result<(), Errno> {
2909                panic!("Should not be called");
2910            }
2911        }
2912
2913        impl OutputBuffer for CountingOutputBuffer {
2914            fn available(&self) -> usize {
2915                usize::MAX
2916            }
2917
2918            fn bytes_written(&self) -> usize {
2919                self.written
2920            }
2921
2922            fn zero(&mut self) -> Result<usize, Errno> {
2923                panic!("Should not be called");
2924            }
2925
2926            fn write_each(
2927                &mut self,
2928                _callback: &mut OutputBufferCallback<'_>,
2929            ) -> Result<usize, Errno> {
2930                panic!("Should not be called.");
2931            }
2932
2933            fn write_all(&mut self, buffer: &[u8]) -> Result<usize, Errno> {
2934                self.written += buffer.len();
2935                Ok(buffer.len())
2936            }
2937
2938            unsafe fn advance(&mut self, _length: usize) -> Result<(), Errno> {
2939                panic!("Should not be called.");
2940            }
2941        }
2942
2943        let mut counting_output_buffer = CountingOutputBuffer::default();
2944        self.serialize(&mut counting_output_buffer).expect("Serialization should not fail");
2945        counting_output_buffer.written
2946    }
2947
2948    fn has_response(&self) -> bool {
2949        !matches!(self, Self::Interrupt { .. } | Self::Forget(_))
2950    }
2951
2952    fn is_async(&self) -> bool {
2953        matches!(self, Self::Init { .. })
2954    }
2955}