libfuse_fs/overlayfs/
async_io.rs

1use super::Inode;
2use super::OverlayFs;
3use super::utils;
4use crate::overlayfs::HandleData;
5use crate::overlayfs::RealHandle;
6use crate::overlayfs::{AtomicU64, CachePolicy};
7use crate::util::open_options::OpenOptions;
8use rfuse3::raw::prelude::*;
9use rfuse3::*;
10use std::ffi::OsStr;
11use std::io::Error;
12use std::io::ErrorKind;
13use std::num::NonZeroU32;
14use std::sync::Arc;
15use std::sync::atomic::Ordering;
16use tokio::sync::Mutex;
17use tracing::info;
18use tracing::trace;
19
20impl Filesystem for OverlayFs {
21    /// initialize filesystem. Called before any other filesystem method.
22    async fn init(&self, _req: Request) -> Result<ReplyInit> {
23        if self.config.do_import {
24            self.import().await?;
25        }
26        if !self.config.do_import || self.config.writeback {
27            self.writeback.store(true, Ordering::Relaxed);
28        }
29        if !self.config.do_import || self.config.no_open {
30            self.no_open.store(true, Ordering::Relaxed);
31        }
32        if !self.config.do_import || self.config.no_opendir {
33            self.no_opendir.store(true, Ordering::Relaxed);
34        }
35        if !self.config.do_import || self.config.killpriv_v2 {
36            self.killpriv_v2.store(true, Ordering::Relaxed);
37        }
38        if self.config.perfile_dax {
39            self.perfile_dax.store(true, Ordering::Relaxed);
40        }
41
42        Ok(ReplyInit {
43            max_write: NonZeroU32::new(128 * 1024).unwrap(),
44        })
45    }
46
47    /// clean up filesystem. Called on filesystem exit which is fuseblk, in normal fuse filesystem,
48    /// kernel may call forget for root. There is some discuss for this
49    /// <https://github.com/bazil/fuse/issues/82#issuecomment-88126886>,
50    /// <https://sourceforge.net/p/fuse/mailman/message/31995737/>
51    async fn destroy(&self, _req: Request) {}
52
53    /// look up a directory entry by name and get its attributes.
54    async fn lookup(&self, req: Request, parent: Inode, name: &OsStr) -> Result<ReplyEntry> {
55        let tmp = name.to_string_lossy().to_string();
56        let result = self.do_lookup(req, parent, tmp.as_str()).await;
57        match result {
58            Ok(e) => Ok(e),
59            Err(err) => Err(err.into()),
60        }
61    }
62
63    /// forget an inode. The nlookup parameter indicates the number of lookups previously
64    /// performed on this inode. If the filesystem implements inode lifetimes, it is recommended
65    /// that inodes acquire a single reference on each lookup, and lose nlookup references on each
66    /// forget. The filesystem may ignore forget calls, if the inodes don't need to have a limited
67    /// lifetime. On unmount it is not guaranteed, that all referenced inodes will receive a forget
68    /// message. When filesystem is normal(not fuseblk) and unmounting, kernel may send forget
69    /// request for root and this library will stop session after call forget. There is some
70    /// discussion for this <https://github.com/bazil/fuse/issues/82#issuecomment-88126886>,
71    /// <https://sourceforge.net/p/fuse/mailman/message/31995737/>
72    async fn forget(&self, _req: Request, inode: Inode, nlookup: u64) {
73        self.forget_one(inode, nlookup).await;
74    }
75
76    /// get file attributes. If `fh` is None, means `fh` is not set.
77    async fn getattr(
78        &self,
79        req: Request,
80        inode: Inode,
81        fh: Option<u64>,
82        flags: u32,
83    ) -> Result<ReplyAttr> {
84        if !self.no_open.load(Ordering::Relaxed)
85            && let Some(h) = fh
86        {
87            let handles = self.handles.lock().await;
88            if let Some(hd) = handles.get(&h)
89                && let Some(ref rh) = hd.real_handle
90            {
91                let mut rep: ReplyAttr = rh
92                    .layer
93                    .getattr(req, rh.inode, Some(rh.handle.load(Ordering::Relaxed)), 0)
94                    .await?;
95                rep.attr.ino = inode;
96                return Ok(rep);
97            }
98        }
99
100        let node: Arc<super::OverlayInode> = self.lookup_node(req, inode, "").await?;
101        let (layer, _, lower_inode) = node.first_layer_inode().await;
102        let mut re = layer.getattr(req, lower_inode, None, flags).await?;
103        re.attr.ino = inode;
104        Ok(re)
105    }
106
107    /// set file attributes. If `fh` is None, means `fh` is not set.
108    async fn setattr(
109        &self,
110        req: Request,
111        inode: Inode,
112        fh: Option<u64>,
113        set_attr: SetAttr,
114    ) -> Result<ReplyAttr> {
115        // Check if upper layer exists.
116        self.upper_layer
117            .as_ref()
118            .cloned()
119            .ok_or_else(|| Error::from_raw_os_error(libc::EROFS))?;
120
121        // deal with handle first
122        if !self.no_open.load(Ordering::Relaxed)
123            && let Some(h) = fh
124        {
125            let handles = self.handles.lock().await;
126            if let Some(hd) = handles.get(&h)
127                && let Some(ref rhd) = hd.real_handle
128            {
129                // handle opened in upper layer
130                if rhd.in_upper_layer {
131                    let mut rep = rhd
132                        .layer
133                        .setattr(
134                            req,
135                            rhd.inode,
136                            Some(rhd.handle.load(Ordering::Relaxed)),
137                            set_attr,
138                        )
139                        .await?;
140                    rep.attr.ino = inode;
141                    return Ok(rep);
142                }
143            }
144        }
145
146        let mut node = self.lookup_node(req, inode, "").await?;
147
148        if !node.in_upper_layer().await {
149            node = self.copy_node_up(req, node.clone()).await?
150        }
151
152        let (layer, _, real_inode) = node.first_layer_inode().await;
153        // layer.setattr(req, real_inode, None, set_attr).await
154        let mut rep = layer.setattr(req, real_inode, None, set_attr).await?;
155        rep.attr.ino = inode;
156        Ok(rep)
157    }
158
159    /// read symbolic link.
160    async fn readlink(&self, req: Request, inode: Inode) -> Result<ReplyData> {
161        trace!("READLINK: inode: {inode}\n");
162
163        let node = self.lookup_node(req, inode, "").await?;
164
165        if node.whiteout.load(Ordering::Relaxed) {
166            return Err(Error::from_raw_os_error(libc::ENOENT).into());
167        }
168
169        let (layer, _, inode) = node.first_layer_inode().await;
170        layer.readlink(req, inode).await
171    }
172
173    /// create a symbolic link.
174    async fn symlink(
175        &self,
176        req: Request,
177        parent: Inode,
178        name: &OsStr,
179        link: &OsStr,
180    ) -> Result<ReplyEntry> {
181        // soft link
182        let sname = name.to_string_lossy().into_owned().to_owned();
183        let slinkname = link.to_string_lossy().into_owned().to_owned();
184
185        let pnode = self.lookup_node(req, parent, "").await?;
186        self.do_symlink(req, slinkname.as_str(), &pnode, sname.as_str())
187            .await?;
188
189        self.do_lookup(req, parent, sname.as_str())
190            .await
191            .map_err(|e| e.into())
192    }
193
194    /// create file node. Create a regular file, character device, block device, fifo or socket
195    /// node. When creating file, most cases user only need to implement
196    /// [`create`][Filesystem::create].
197    async fn mknod(
198        &self,
199        req: Request,
200        parent: Inode,
201        name: &OsStr,
202        mode: u32,
203        rdev: u32,
204    ) -> Result<ReplyEntry> {
205        let sname = name.to_string_lossy().to_string();
206
207        // Check if parent exists.
208        let pnode = self.lookup_node(req, parent, "").await?;
209        if pnode.whiteout.load(Ordering::Relaxed) {
210            return Err(Error::from_raw_os_error(libc::ENOENT).into());
211        }
212
213        self.do_mknod(req, &pnode, sname.as_str(), mode, rdev, 0)
214            .await?;
215        self.do_lookup(req, parent, sname.as_str())
216            .await
217            .map_err(|e| e.into())
218    }
219
220    /// create a directory.
221    async fn mkdir(
222        &self,
223        req: Request,
224        parent: Inode,
225        name: &OsStr,
226        mode: u32,
227        umask: u32,
228    ) -> Result<ReplyEntry> {
229        let sname = name.to_string_lossy().to_string();
230
231        // no entry or whiteout
232        let pnode = self.lookup_node(req, parent, "").await?;
233        if pnode.whiteout.load(Ordering::Relaxed) {
234            return Err(Error::from_raw_os_error(libc::ENOENT).into());
235        }
236
237        self.do_mkdir(req, pnode, sname.as_str(), mode, umask)
238            .await?;
239        self.do_lookup(req, parent, sname.as_str())
240            .await
241            .map_err(|e| e.into())
242    }
243
244    /// remove a file.
245    async fn unlink(&self, req: Request, parent: Inode, name: &OsStr) -> Result<()> {
246        self.do_rm(req, parent, name, false)
247            .await
248            .map_err(|e| e.into())
249    }
250
251    /// remove a directory.
252    async fn rmdir(&self, req: Request, parent: Inode, name: &OsStr) -> Result<()> {
253        self.do_rm(req, parent, name, true)
254            .await
255            .map_err(|e| e.into())
256    }
257
258    /// rename a file or directory.
259    async fn rename(
260        &self,
261        req: Request,
262        parent: Inode,
263        name: &OsStr,
264        new_parent: Inode,
265        new_name: &OsStr,
266    ) -> Result<()> {
267        self.do_rename(req, parent, name, new_parent, new_name)
268            .await
269            .map_err(|e| e.into())
270    }
271
272    /// create a hard link.
273    async fn link(
274        &self,
275        req: Request,
276        inode: Inode,
277        new_parent: Inode,
278        new_name: &OsStr,
279    ) -> Result<ReplyEntry> {
280        let node = self.lookup_node(req, inode, "").await?;
281        if node.whiteout.load(Ordering::Relaxed) {
282            return Err(Error::from_raw_os_error(libc::ENOENT).into());
283        }
284
285        let newpnode = self.lookup_node(req, new_parent, "").await?;
286        if newpnode.whiteout.load(Ordering::Relaxed) {
287            return Err(Error::from_raw_os_error(libc::ENOENT).into());
288        }
289        let new_name = new_name.to_str().unwrap();
290        // trace!(
291        //     "LINK: inode: {}, new_parent: {}, trying to do_link: src_inode: {}, newpnode: {}",
292        //     inode, new_parent, node.inode, newpnode.inode
293        // );
294        self.do_link(req, &node, &newpnode, new_name).await?;
295        // trace!("LINK: done, looking up new entry");
296        self.do_lookup(req, new_parent, new_name)
297            .await
298            .map_err(|e| e.into())
299    }
300
301    /// open a file. Open flags (with the exception of `O_CREAT`, `O_EXCL` and `O_NOCTTY`) are
302    /// available in flags. Filesystem may store an arbitrary file handle (pointer, index, etc) in
303    /// fh, and use this in other all other file operations (read, write, flush, release, fsync).
304    /// Filesystem may also implement stateless file I/O and not store anything in fh. There are
305    /// also some flags (`direct_io`, `keep_cache`) which the filesystem may set, to change the way
306    /// the file is opened. A filesystem need not implement this method if it
307    /// sets [`MountOptions::no_open_support`][crate::MountOptions::no_open_support] and if the
308    /// kernel supports `FUSE_NO_OPEN_SUPPORT`.
309    ///
310    /// # Notes:
311    ///
312    /// See `fuse_file_info` structure in
313    /// [fuse_common.h](https://libfuse.github.io/doxygen/include_2fuse__common_8h_source.html) for
314    /// more details.
315    async fn open(&self, req: Request, inode: Inode, flags: u32) -> Result<ReplyOpen> {
316        if self.no_open.load(Ordering::Relaxed) {
317            info!("fuse: open is not supported.");
318            return Err(Error::from_raw_os_error(libc::ENOSYS).into());
319        }
320
321        let readonly: bool = flags
322            & (libc::O_APPEND | libc::O_CREAT | libc::O_TRUNC | libc::O_RDWR | libc::O_WRONLY)
323                as u32
324            == 0;
325        // toggle flags
326        let mut flags: i32 = flags as i32;
327
328        flags |= libc::O_NOFOLLOW;
329
330        if self.config.writeback {
331            if flags & libc::O_ACCMODE == libc::O_WRONLY {
332                flags &= !libc::O_ACCMODE;
333                flags |= libc::O_RDWR;
334            }
335
336            if flags & libc::O_APPEND != 0 {
337                flags &= !libc::O_APPEND;
338            }
339        }
340        // lookup node
341        let node = self.lookup_node(req, inode, "").await?;
342
343        // whiteout node
344        if node.whiteout.load(Ordering::Relaxed) {
345            return Err(Error::from_raw_os_error(libc::ENOENT).into());
346        }
347
348        if !readonly {
349            // copy up to upper layer
350            self.copy_node_up(req, node.clone()).await?;
351        }
352
353        // assign a handle in overlayfs and open it
354        let (_l, h) = node.open(req, flags as u32, 0).await?;
355
356        let hd = self.next_handle.fetch_add(1, Ordering::Relaxed);
357        let (layer, in_upper_layer, inode) = node.first_layer_inode().await;
358        let handle_data = HandleData {
359            node: node.clone(),
360            real_handle: Some(RealHandle {
361                layer,
362                in_upper_layer,
363                inode,
364                handle: AtomicU64::new(h.fh),
365            }),
366            dir_snapshot: Mutex::new(None),
367        };
368
369        self.handles.lock().await.insert(hd, Arc::new(handle_data));
370
371        let mut opts = OpenOptions::empty();
372        match self.config.cache_policy {
373            CachePolicy::Never => opts |= OpenOptions::DIRECT_IO,
374            CachePolicy::Always => opts |= OpenOptions::KEEP_CACHE,
375            _ => {}
376        }
377
378        // trace!("OPEN: returning handle: {hd}");
379
380        Ok(ReplyOpen {
381            fh: hd,
382            flags: opts.bits(),
383        })
384    }
385
386    /// read data. Read should send exactly the number of bytes requested except on EOF or error,
387    /// otherwise the rest of the data will be substituted with zeroes. An exception to this is
388    /// when the file has been opened in `direct_io` mode, in which case the return value of the
389    /// read system call will reflect the return value of this operation. `fh` will contain the
390    /// value set by the open method, or will be undefined if the open method didn't set any value.
391    async fn read(
392        &self,
393        req: Request,
394        inode: Inode,
395        fh: u64,
396        offset: u64,
397        size: u32,
398    ) -> Result<ReplyData> {
399        let data = self.get_data(req, Some(fh), inode, 0).await?;
400
401        match data.real_handle {
402            None => Err(Error::from_raw_os_error(libc::ENOENT).into()),
403            Some(ref hd) => {
404                hd.layer
405                    .read(
406                        req,
407                        hd.inode,
408                        hd.handle.load(Ordering::Relaxed),
409                        offset,
410                        size,
411                    )
412                    .await
413            }
414        }
415    }
416
417    /// write data. Write should return exactly the number of bytes requested except on error. An
418    /// exception to this is when the file has been opened in `direct_io` mode, in which case the
419    /// return value of the write system call will reflect the return value of this operation. `fh`
420    /// will contain the value set by the open method, or will be undefined if the open method
421    /// didn't set any value. When `write_flags` contains
422    /// [`FUSE_WRITE_CACHE`](crate::raw::flags::FUSE_WRITE_CACHE), means the write operation is a
423    /// delay write.
424    #[allow(clippy::too_many_arguments)]
425    async fn write(
426        &self,
427        req: Request,
428        inode: Inode,
429        fh: u64,
430        offset: u64,
431        data: &[u8],
432        write_flags: u32,
433        flags: u32,
434    ) -> Result<ReplyWrite> {
435        let handle_data: Arc<HandleData> = self.get_data(req, Some(fh), inode, flags).await?;
436
437        match handle_data.real_handle {
438            None => Err(Error::from_raw_os_error(libc::ENOENT).into()),
439            Some(ref hd) => {
440                hd.layer
441                    .write(
442                        req,
443                        hd.inode,
444                        hd.handle.load(Ordering::Relaxed),
445                        offset,
446                        data,
447                        write_flags,
448                        flags,
449                    )
450                    .await
451            }
452        }
453    }
454
455    /// Copy a range of data from one file to another. This can improve performance because it
456    /// reduces data copying: normally, data will be copied from FUSE server to kernel, then to
457    /// user-space, then to kernel, and finally sent back to FUSE server. By implementing this
458    /// method, data will only be copied internally within the FUSE server.
459    #[allow(clippy::too_many_arguments)]
460    async fn copy_file_range(
461        &self,
462        req: Request,
463        inode_in: Inode,
464        fh_in: u64,
465        offset_in: u64,
466        inode_out: Inode,
467        fh_out: u64,
468        offset_out: u64,
469        length: u64,
470        flags: u64,
471    ) -> Result<ReplyCopyFileRange> {
472        // Get handle data for source file
473        let data_in = self.get_data(req, Some(fh_in), inode_in, 0).await?;
474        let handle_in = match data_in.real_handle {
475            None => return Err(Error::from_raw_os_error(libc::ENOENT).into()),
476            Some(ref hd) => hd,
477        };
478
479        // Get handle data for destination file
480        let data_out = self.get_data(req, Some(fh_out), inode_out, 0).await?;
481        let handle_out = match data_out.real_handle {
482            None => return Err(Error::from_raw_os_error(libc::ENOENT).into()),
483            Some(ref hd) => hd,
484        };
485
486        // Both files must be on the same layer for copy_file_range to work
487        if !Arc::ptr_eq(&handle_in.layer, &handle_out.layer) {
488            // Different layers - return EXDEV to trigger fallback to read/write
489            return Err(Error::from_raw_os_error(libc::EXDEV).into());
490        }
491
492        // Delegate to the underlying PassthroughFs layer
493        handle_in
494            .layer
495            .copy_file_range(
496                req,
497                handle_in.inode,
498                handle_in.handle.load(Ordering::Relaxed),
499                offset_in,
500                handle_out.inode,
501                handle_out.handle.load(Ordering::Relaxed),
502                offset_out,
503                length,
504                flags,
505            )
506            .await
507    }
508    /// get filesystem statistics.
509    async fn statfs(&self, req: Request, inode: Inode) -> Result<ReplyStatFs> {
510        self.do_statvfs(req, inode).await.map_err(|e| e.into())
511    }
512
513    /// release an open file. Release is called when there are no more references to an open file:
514    /// all file descriptors are closed and all memory mappings are unmapped. For every open call
515    /// there will be exactly one release call. The filesystem may reply with an error, but error
516    /// values are not returned to `close()` or `munmap()` which triggered the release. `fh` will
517    /// contain the value set by the open method, or will be undefined if the open method didn't
518    /// set any value. `flags` will contain the same flags as for open. `flush` means flush the
519    /// data or not when closing file.
520    async fn release(
521        &self,
522        req: Request,
523        _inode: Inode,
524        fh: u64,
525        flags: u32,
526        lock_owner: u64,
527        flush: bool,
528    ) -> Result<()> {
529        if self.no_open.load(Ordering::Relaxed) {
530            info!("fuse: release is not supported.");
531            return Err(Error::from_raw_os_error(libc::ENOSYS).into());
532        }
533
534        if let Some(hd) = self.handles.lock().await.get(&fh) {
535            let rh = if let Some(ref h) = hd.real_handle {
536                h
537            } else {
538                return Err(
539                    Error::other(format!("no real handle found for file handle {fh}")).into(),
540                );
541            };
542            let real_handle = rh.handle.load(Ordering::Relaxed);
543            let real_inode = rh.inode;
544            rh.layer
545                .release(req, real_inode, real_handle, flags, lock_owner, flush)
546                .await?;
547        }
548
549        self.handles.lock().await.remove(&fh);
550
551        Ok(())
552    }
553
554    /// synchronize file contents. If the `datasync` is true, then only the user data should be
555    /// flushed, not the metadata.
556    async fn fsync(&self, req: Request, inode: Inode, fh: u64, datasync: bool) -> Result<()> {
557        self.do_fsync(req, inode, datasync, fh, false)
558            .await
559            .map_err(|e| e.into())
560    }
561
562    /// set an extended attribute.
563    async fn setxattr(
564        &self,
565        req: Request,
566        inode: Inode,
567        name: &OsStr,
568        value: &[u8],
569        flags: u32,
570        position: u32,
571    ) -> Result<()> {
572        let node = self.lookup_node(req, inode, "").await?;
573
574        if node.whiteout.load(Ordering::Relaxed) {
575            return Err(Error::from_raw_os_error(libc::ENOENT).into());
576        }
577
578        if !node.in_upper_layer().await {
579            // Copy node up.
580            self.copy_node_up(req, node.clone()).await?;
581        }
582
583        let (layer, _, real_inode) = node.first_layer_inode().await;
584
585        layer
586            .setxattr(req, real_inode, name, value, flags, position)
587            .await
588    }
589
590    /// Get an extended attribute. If `size` is too small, return `Err<ERANGE>`.
591    /// Otherwise, use [`ReplyXAttr::Data`] to send the attribute data, or
592    /// return an error.
593    async fn getxattr(
594        &self,
595        req: Request,
596        inode: Inode,
597        name: &OsStr,
598        size: u32,
599    ) -> Result<ReplyXAttr> {
600        let node = self.lookup_node(req, inode, "").await?;
601
602        if node.whiteout.load(Ordering::Relaxed) {
603            return Err(Error::from_raw_os_error(libc::ENOENT).into());
604        }
605
606        let (layer, real_inode) = self.find_real_inode(inode).await?;
607
608        layer.getxattr(req, real_inode, name, size).await
609    }
610
611    /// List extended attribute names.
612    ///
613    /// If `size` is too small, return `Err<ERANGE>`.  Otherwise, use
614    /// [`ReplyXAttr::Data`] to send the attribute list, or return an error.
615    async fn listxattr(&self, req: Request, inode: Inode, size: u32) -> Result<ReplyXAttr> {
616        let node = self.lookup_node(req, inode, "").await?;
617        if node.whiteout.load(Ordering::Relaxed) {
618            return Err(Error::from_raw_os_error(libc::ENOENT).into());
619        }
620        let (layer, real_inode) = self.find_real_inode(inode).await?;
621        layer.listxattr(req, real_inode, size).await
622    }
623
624    /// remove an extended attribute.
625    async fn removexattr(&self, req: Request, inode: Inode, name: &OsStr) -> Result<()> {
626        let node = self.lookup_node(req, inode, "").await?;
627
628        if node.whiteout.load(Ordering::Relaxed) {
629            return Err(Error::from_raw_os_error(libc::ENOENT).into());
630        }
631
632        if !node.in_upper_layer().await {
633            // copy node into upper layer
634            self.copy_node_up(req, node.clone()).await?;
635        }
636
637        let (layer, _, ino) = node.first_layer_inode().await;
638        layer.removexattr(req, ino, name).await
639
640        // TODO: recreate the node since removexattr may remove the opaque xattr.
641    }
642
643    /// flush method. This is called on each `close()` of the opened file. Since file descriptors
644    /// can be duplicated (`dup`, `dup2`, `fork`), for one open call there may be many flush calls.
645    /// Filesystems shouldn't assume that flush will always be called after some writes, or that if
646    /// will be called at all. `fh` will contain the value set by the open method, or will be
647    /// undefined if the open method didn't set any value.
648    ///
649    /// # Notes:
650    ///
651    /// the name of the method is misleading, since (unlike fsync) the filesystem is not forced to
652    /// flush pending writes. One reason to flush data, is if the filesystem wants to return write
653    /// errors. If the filesystem supports file locking operations ([`setlk`][Filesystem::setlk],
654    /// [`getlk`][Filesystem::getlk]) it should remove all locks belonging to `lock_owner`.
655    async fn flush(&self, req: Request, inode: Inode, fh: u64, lock_owner: u64) -> Result<()> {
656        if self.no_open.load(Ordering::Relaxed) {
657            return Err(Error::from_raw_os_error(libc::ENOSYS).into());
658        }
659
660        let node = self.lookup_node(req, inode, "").await;
661        match node {
662            Ok(n) => {
663                if n.whiteout.load(Ordering::Relaxed) {
664                    return Err(Error::from_raw_os_error(libc::ENOENT).into());
665                }
666            }
667            Err(e) => {
668                if e.raw_os_error() == Some(libc::ENOENT) {
669                    trace!("flush: inode {inode} is stale");
670                } else {
671                    return Err(e.into());
672                }
673            }
674        }
675
676        let (layer, real_inode, real_handle) = self.find_real_info_from_handle(fh).await?;
677
678        // FIXME: need to test if inode matches corresponding handle?
679        if inode
680            != self
681                .handles
682                .lock()
683                .await
684                .get(&fh)
685                .map(|h| h.node.inode)
686                .unwrap_or(0)
687        {
688            return Err(Error::other("inode does not match handle").into());
689        }
690
691        trace!("flushing, real_inode: {real_inode}, real_handle: {real_handle}");
692        layer.flush(req, real_inode, real_handle, lock_owner).await
693    }
694
695    /// open a directory. Filesystem may store an arbitrary file handle (pointer, index, etc) in
696    /// `fh`, and use this in other all other directory stream operations
697    /// ([`readdir`][Filesystem::readdir], [`releasedir`][Filesystem::releasedir],
698    /// [`fsyncdir`][Filesystem::fsyncdir]). Filesystem may also implement stateless directory
699    /// I/O and not store anything in `fh`.  A file system need not implement this method if it
700    /// sets [`MountOptions::no_open_dir_support`][crate::MountOptions::no_open_dir_support] and
701    /// if the kernel supports `FUSE_NO_OPENDIR_SUPPORT`.
702    async fn opendir(&self, req: Request, inode: Inode, flags: u32) -> Result<ReplyOpen> {
703        if self.no_opendir.load(Ordering::Relaxed) {
704            info!("fuse: opendir is not supported.");
705            return Err(Error::from_raw_os_error(libc::ENOSYS).into());
706        }
707
708        // lookup node
709        let node = self.lookup_node(req, inode, ".").await?;
710
711        if node.whiteout.load(Ordering::Relaxed) {
712            return Err(Error::from_raw_os_error(libc::ENOENT).into());
713        }
714
715        let st = node.stat64(req).await?;
716        if !utils::is_dir(&st.attr.kind) {
717            return Err(Error::from_raw_os_error(libc::ENOTDIR).into());
718        }
719
720        let handle = self.next_handle.fetch_add(1, Ordering::Relaxed);
721        // Get the layer information and open directory in the underlying layer
722        let (layer, in_upper_layer, real_inode) = node.first_layer_inode().await;
723        let reply = layer.opendir(req, real_inode, flags).await?;
724
725        self.handles.lock().await.insert(
726            handle,
727            Arc::new(HandleData {
728                node: Arc::clone(&node),
729                real_handle: Some(RealHandle {
730                    layer,
731                    in_upper_layer,
732                    inode: real_inode,
733                    handle: AtomicU64::new(reply.fh),
734                }),
735                dir_snapshot: Mutex::new(None),
736            }),
737        );
738
739        Ok(ReplyOpen { fh: handle, flags })
740    }
741
742    /// read directory. `offset` is used to track the offset of the directory entries. `fh` will
743    /// contain the value set by the [`opendir`][Filesystem::opendir] method, or will be
744    /// undefined if the [`opendir`][Filesystem::opendir] method didn't set any value.
745    async fn readdir<'a>(
746        &'a self,
747        req: Request,
748        parent: Inode,
749        fh: u64,
750        offset: i64,
751    ) -> Result<
752        ReplyDirectory<
753            impl futures_util::stream::Stream<Item = Result<DirectoryEntry>> + Send + 'a,
754        >,
755    > {
756        if self.config.no_readdir {
757            info!("fuse: readdir is not supported.");
758            return Err(Error::from_raw_os_error(libc::ENOTDIR).into());
759        }
760        let entries = self
761            .do_readdir(req, parent, fh, offset.try_into().unwrap())
762            .await?;
763        Ok(ReplyDirectory { entries })
764    }
765
766    /// read directory entries, but with their attribute, like [`readdir`][Filesystem::readdir]
767    /// + [`lookup`][Filesystem::lookup] at the same time.
768    async fn readdirplus<'a>(
769        &'a self,
770        req: Request,
771        parent: Inode,
772        fh: u64,
773        offset: u64,
774        _lock_owner: u64,
775    ) -> Result<
776        ReplyDirectoryPlus<
777            impl futures_util::stream::Stream<Item = Result<DirectoryEntryPlus>> + Send + 'a,
778        >,
779    > {
780        if self.config.no_readdir {
781            info!("fuse: readdir is not supported.");
782            return Err(Error::from_raw_os_error(libc::ENOTDIR).into());
783        }
784        trace!("readdirplus: parent: {parent}, fh: {fh}, offset: {offset}");
785        let entries = self.do_readdirplus(req, parent, fh, offset).await?;
786        match self.handles.lock().await.get(&fh) {
787            Some(h) => {
788                trace!(
789                    "after readdirplus: found handle, seeing real_handle: {}",
790                    h.real_handle.is_some()
791                );
792            }
793            None => trace!("after readdirplus: no handle found: {fh}"),
794        }
795        Ok(ReplyDirectoryPlus { entries })
796    }
797    /// release an open directory. For every [`opendir`][Filesystem::opendir] call there will
798    /// be exactly one `releasedir` call. `fh` will contain the value set by the
799    /// [`opendir`][Filesystem::opendir] method, or will be undefined if the
800    /// [`opendir`][Filesystem::opendir] method didn't set any value.
801    async fn releasedir(&self, req: Request, _inode: Inode, fh: u64, flags: u32) -> Result<()> {
802        if self.no_opendir.load(Ordering::Relaxed) {
803            info!("fuse: releasedir is not supported.");
804            return Err(Error::from_raw_os_error(libc::ENOSYS).into());
805        }
806
807        if let Some(hd) = self.handles.lock().await.get(&fh) {
808            let rh = if let Some(ref h) = hd.real_handle {
809                h
810            } else {
811                return Err(
812                    Error::other(format!("no real handle found for file handle {fh}")).into(),
813                );
814            };
815            let real_handle = rh.handle.load(Ordering::Relaxed);
816            let real_inode = rh.inode;
817            rh.layer
818                .releasedir(req, real_inode, real_handle, flags)
819                .await?;
820        }
821
822        self.handles.lock().await.remove(&fh);
823        Ok(())
824    }
825
826    /// synchronize directory contents. If the `datasync` is true, then only the directory contents
827    /// should be flushed, not the metadata. `fh` will contain the value set by the
828    /// [`opendir`][Filesystem::opendir] method, or will be undefined if the
829    /// [`opendir`][Filesystem::opendir] method didn't set any value.
830    async fn fsyncdir(&self, req: Request, inode: Inode, fh: u64, datasync: bool) -> Result<()> {
831        self.do_fsync(req, inode, datasync, fh, true)
832            .await
833            .map_err(|e| e.into())
834    }
835    /// check file access permissions. This will be called for the `access()` system call. If the
836    /// `default_permissions` mount option is given, this method is not be called. This method is
837    /// not called under Linux kernel versions 2.4.x.
838    async fn access(&self, req: Request, inode: Inode, mask: u32) -> Result<()> {
839        let node = self.lookup_node(req, inode, "").await?;
840
841        if node.whiteout.load(Ordering::Relaxed) {
842            return Err(Error::from_raw_os_error(libc::ENOENT).into());
843        }
844
845        let (layer, real_inode) = self.find_real_inode(inode).await?;
846        layer.access(req, real_inode, mask).await
847    }
848
849    /// create and open a file. If the file does not exist, first create it with the specified
850    /// mode, and then open it. Open flags (with the exception of `O_NOCTTY`) are available in
851    /// flags. Filesystem may store an arbitrary file handle (pointer, index, etc) in `fh`, and use
852    /// this in other all other file operations ([`read`][Filesystem::read],
853    /// [`write`][Filesystem::write], [`flush`][Filesystem::flush],
854    /// [`release`][Filesystem::release], [`fsync`][Filesystem::fsync]). There are also some flags
855    /// (`direct_io`, `keep_cache`) which the filesystem may set, to change the way the file is
856    /// opened. If this method is not implemented or under Linux kernel versions earlier than
857    /// 2.6.15, the [`mknod`][Filesystem::mknod] and [`open`][Filesystem::open] methods will be
858    /// called instead.
859    ///
860    /// # Notes:
861    ///
862    /// See `fuse_file_info` structure in
863    /// [fuse_common.h](https://libfuse.github.io/doxygen/include_2fuse__common_8h_source.html) for
864    /// more details.
865    async fn create(
866        &self,
867        req: Request,
868        parent: Inode,
869        name: &OsStr,
870        mode: u32,
871        flags: u32,
872    ) -> Result<ReplyCreated> {
873        // Parent doesn't exist.
874        let pnode = self.lookup_node(req, parent, "").await?;
875        if pnode.whiteout.load(Ordering::Relaxed) {
876            return Err(Error::from_raw_os_error(libc::ENOENT).into());
877        }
878
879        let mut flags: i32 = flags as i32;
880        flags |= libc::O_NOFOLLOW;
881        flags &= !libc::O_DIRECT;
882        if self.config.writeback {
883            if flags & libc::O_ACCMODE == libc::O_WRONLY {
884                flags &= !libc::O_ACCMODE;
885                flags |= libc::O_RDWR;
886            }
887
888            if flags & libc::O_APPEND != 0 {
889                flags &= !libc::O_APPEND;
890            }
891        }
892
893        let final_handle = self
894            .do_create(req, &pnode, name, mode, flags.try_into().unwrap())
895            .await?;
896        let entry = self.do_lookup(req, parent, name.to_str().unwrap()).await?;
897        let fh = final_handle
898            .ok_or_else(|| std::io::Error::new(ErrorKind::NotFound, "Handle not found"))?;
899
900        let mut opts = OpenOptions::empty();
901        match self.config.cache_policy {
902            CachePolicy::Never => opts |= OpenOptions::DIRECT_IO,
903            CachePolicy::Always => opts |= OpenOptions::KEEP_CACHE,
904            _ => {}
905        }
906
907        Ok(ReplyCreated {
908            ttl: entry.ttl,
909            attr: entry.attr,
910            generation: entry.generation,
911            fh,
912            flags: opts.bits(),
913        })
914    }
915
916    /// forget more than one inode. This is a batch version [`forget`][Filesystem::forget]
917    async fn batch_forget(&self, _req: Request, inodes: &[(Inode, u64)]) {
918        for inode in inodes {
919            self.forget_one(inode.0, inode.1).await;
920        }
921    }
922
923    /// allocate space for an open file. This function ensures that required space is allocated for
924    /// specified file.
925    ///
926    /// # Notes:
927    ///
928    /// more information about `fallocate`, please see **`man 2 fallocate`**
929    async fn fallocate(
930        &self,
931        req: Request,
932        inode: Inode,
933        fh: u64,
934        offset: u64,
935        length: u64,
936        mode: u32,
937    ) -> Result<()> {
938        // Use O_RDONLY flags which indicates no copy up.
939        let data = self
940            .get_data(req, Some(fh), inode, libc::O_RDONLY as u32)
941            .await?;
942
943        match data.real_handle {
944            None => Err(Error::from_raw_os_error(libc::ENOENT).into()),
945            Some(ref rhd) => {
946                if !rhd.in_upper_layer {
947                    // TODO: in lower layer, error out or just success?
948                    return Err(Error::from_raw_os_error(libc::EROFS).into());
949                }
950                rhd.layer
951                    .fallocate(
952                        req,
953                        rhd.inode,
954                        rhd.handle.load(Ordering::Relaxed),
955                        offset,
956                        length,
957                        mode,
958                    )
959                    .await
960            }
961        }
962    }
963
964    /// find next data or hole after the specified offset.
965    async fn lseek(
966        &self,
967        req: Request,
968        inode: Inode,
969        fh: u64,
970        offset: u64,
971        whence: u32,
972    ) -> Result<ReplyLSeek> {
973        let node = self.lookup_node(req, inode, "").await?;
974
975        if node.whiteout.load(Ordering::Relaxed) {
976            return Err(Error::from_raw_os_error(libc::ENOENT).into());
977        }
978
979        let st = node.stat64(req).await?;
980        if utils::is_dir(&st.attr.kind) {
981            // Special handling and security restrictions for directory operations.
982            // Use the common API to obtain the underlying layer and handle info.
983            let (layer, real_inode, real_handle) = self.find_real_info_from_handle(fh).await?;
984
985            // Verify that the underlying handle refers to a directory.
986            let handle_stat = match layer.getattr(req, real_inode, Some(real_handle), 0).await {
987                Ok(s) => s,
988                Err(_) => return Err(Error::from_raw_os_error(libc::EBADF).into()),
989            };
990
991            if !utils::is_dir(&handle_stat.attr.kind) {
992                return Err(Error::from_raw_os_error(libc::ENOTDIR).into());
993            }
994
995            // Handle directory lseek operations according to POSIX standard
996            // This enables seekdir/telldir functionality on directories
997            match whence {
998                // SEEK_SET: Set the directory position to an absolute value
999                x if x == libc::SEEK_SET as u32 => {
1000                    // Validate offset bounds to prevent overflow
1001                    // Directory offsets should not exceed i64::MAX
1002                    if offset > i64::MAX as u64 {
1003                        return Err(Error::from_raw_os_error(libc::EINVAL).into());
1004                    }
1005
1006                    // Perform the seek operation on the underlying layer
1007                    // Delegate to the lower layer implementation
1008                    layer
1009                        .lseek(req, real_inode, real_handle, offset, whence)
1010                        .await
1011                }
1012                // SEEK_CUR: Move relative to the current directory position
1013                x if x == libc::SEEK_CUR as u32 => {
1014                    // Get current position from underlying layer
1015                    // This is needed to calculate the new position
1016                    let current = match layer
1017                        .lseek(req, real_inode, real_handle, 0, libc::SEEK_CUR as u32)
1018                        .await
1019                    {
1020                        Ok(r) => r.offset,
1021                        Err(_) => return Err(Error::from_raw_os_error(libc::EINVAL).into()),
1022                    };
1023
1024                    // Check for potential overflow when adding the provided offset
1025                    // This prevents invalid position calculations
1026                    if let Some(new_offset) = current.checked_add(offset) {
1027                        // Ensure the new offset is within valid bounds
1028                        if new_offset > i64::MAX as u64 {
1029                            return Err(Error::from_raw_os_error(libc::EINVAL).into());
1030                        }
1031
1032                        // Actually set the underlying offset to the new value so behavior
1033                        // matches passthrough which uses libc::lseek64 to set the fd offset.
1034                        match layer
1035                            .lseek(
1036                                req,
1037                                real_inode,
1038                                real_handle,
1039                                new_offset,
1040                                libc::SEEK_SET as u32,
1041                            )
1042                            .await
1043                        {
1044                            Ok(_) => Ok(ReplyLSeek { offset: new_offset }),
1045                            Err(_) => Err(Error::from_raw_os_error(libc::EINVAL).into()),
1046                        }
1047                    } else {
1048                        Err(Error::from_raw_os_error(libc::EINVAL).into())
1049                    }
1050                }
1051                // Any other whence value is invalid for directories
1052                _ => Err(Error::from_raw_os_error(libc::EINVAL).into()),
1053            }
1054        } else {
1055            // Keep the original lseek behavior for regular files
1056            // Delegate directly to the underlying layer
1057            let (layer, real_inode, real_handle) = self.find_real_info_from_handle(fh).await?;
1058            layer
1059                .lseek(req, real_inode, real_handle, offset, whence)
1060                .await
1061        }
1062    }
1063}
1064#[cfg(test)]
1065mod tests {
1066    use std::{ffi::OsString, path::PathBuf, sync::Arc};
1067
1068    use rfuse3::{MountOptions, raw::Session};
1069    use tokio::signal;
1070    use tracing_subscriber::EnvFilter;
1071
1072    use crate::{
1073        overlayfs::{OverlayFs, config::Config},
1074        passthrough::{PassthroughArgs, new_passthroughfs_layer, newlogfs::LoggingFileSystem},
1075    };
1076
1077    #[tokio::test]
1078    #[ignore]
1079    async fn test_a_ovlfs() {
1080        let _ = tracing_subscriber::fmt()
1081            .with_env_filter(EnvFilter::from_default_env().add_directive("trace".parse().unwrap()))
1082            .try_init();
1083
1084        // Set up test environment
1085        let mountpoint = PathBuf::from("/home/luxian/megatest/true_temp");
1086        let lowerdir = vec![PathBuf::from("/home/luxian/github/buck2-rust-third-party")];
1087        let upperdir = PathBuf::from("/home/luxian/upper");
1088
1089        // Create lower layers
1090        let mut lower_layers = Vec::new();
1091        for lower in &lowerdir {
1092            let layer = new_passthroughfs_layer(PassthroughArgs {
1093                root_dir: lower.clone(),
1094                mapping: None::<&str>,
1095            })
1096            .await
1097            .unwrap();
1098            lower_layers.push(Arc::new(layer));
1099        }
1100        // Create upper layer
1101        let upper_layer = Arc::new(
1102            new_passthroughfs_layer(PassthroughArgs {
1103                root_dir: upperdir,
1104                mapping: None::<&str>,
1105            })
1106            .await
1107            .unwrap(),
1108        );
1109        // Create overlayfs
1110        let config = Config {
1111            mountpoint: mountpoint.clone(),
1112            do_import: true,
1113            ..Default::default()
1114        };
1115
1116        let overlayfs = OverlayFs::new(Some(upper_layer), lower_layers, config, 1).unwrap();
1117
1118        let logfs = LoggingFileSystem::new(overlayfs);
1119
1120        let mount_path: OsString = OsString::from(mountpoint);
1121
1122        let uid = unsafe { libc::getuid() };
1123        let gid = unsafe { libc::getgid() };
1124
1125        let not_unprivileged = false;
1126
1127        let mut mount_options = MountOptions::default();
1128        // .allow_other(true)
1129        mount_options.force_readdir_plus(true).uid(uid).gid(gid);
1130
1131        let mut mount_handle: rfuse3::raw::MountHandle = if !not_unprivileged {
1132            Session::new(mount_options)
1133                .mount_with_unprivileged(logfs, mount_path)
1134                .await
1135                .unwrap()
1136        } else {
1137            Session::new(mount_options)
1138                .mount(logfs, mount_path)
1139                .await
1140                .unwrap()
1141        };
1142
1143        let handle = &mut mount_handle;
1144
1145        tokio::select! {
1146            res = handle => res.unwrap(),
1147            _ = signal::ctrl_c() => {
1148                mount_handle.unmount().await.unwrap()
1149            }
1150        }
1151    }
1152}