Skip to main content

hyprstream_vfs/
zero_open.rs

1//! `ZeroOpenLayer` — make a *handleless* `FileSystem`/`Layer` usable as an
2//! `OverlayFs` lower (native only).
3//!
4//! ## Why
5//!
6//! `fuse_backend_rs::overlayfs::OverlayFs` is handle-based: when it serves
7//! `open`/`read` it calls the underlying layer's `open` and **requires it to
8//! return `Some(handle)`** — a `None` handle is treated as ENOENT (see
9//! `OverlayFs::open`). Some filesystems are *handleless* ("zero-message-open"):
10//! they return `(None, KEEP_CACHE, _)` from `open` and address reads by inode.
11//! The motivating case is **RAFS** (`nydus_rafs::fs::Rafs`), the FS-B (#363)
12//! rootfs lower: its `open` returns no handle, so used directly as an OverlayFs
13//! lower every read of an image file fails with ENOENT.
14//!
15//! ## What
16//!
17//! [`ZeroOpenLayer`] wraps any [`Layer`] and substitutes a constant handle (`0`)
18//! whenever the inner `open`/`opendir` returns `None`, leaving every other op a
19//! straight delegation. The substituted handle is harmless: a handleless backend
20//! ignores the handle on `read`/`readdir` (it resolves by inode), and on
21//! `release`/`releasedir` the wrapper passes it straight back. The overlay stays
22//! in its normal, production (Kata) handle mode — copy-up still routes writes to
23//! the writable upper layer (which keeps real handles); only the read-only lower
24//! is wrapped.
25//!
26//! This keeps the version-pinned overlay engine and the up-adapter unchanged: it
27//! is purely a lower-layer shim.
28
29use std::ffi::CStr;
30use std::io;
31use std::time::Duration;
32
33use fuse_backend_rs::abi::fuse_abi::{stat64, statvfs64};
34use fuse_backend_rs::api::filesystem::{
35    Context, DirEntry, Entry, FileSystem, FsOptions, GetxattrReply, Layer, ListxattrReply,
36    OpenOptions, SetattrValid, ZeroCopyReader, ZeroCopyWriter,
37};
38
39/// Wrap a handleless [`Layer`] so it presents a non-`None` handle to
40/// `OverlayFs`. See the module docs.
41pub struct ZeroOpenLayer<L>
42where
43    L: Layer<Inode = u64, Handle = u64> + Send + Sync,
44{
45    inner: L,
46}
47
48impl<L> ZeroOpenLayer<L>
49where
50    L: Layer<Inode = u64, Handle = u64> + Send + Sync,
51{
52    /// Wrap `inner`. The inner layer must already be ready to serve (e.g. a
53    /// RAFS whose `import` has run).
54    pub fn new(inner: L) -> Self {
55        Self { inner }
56    }
57}
58
59impl<L> FileSystem for ZeroOpenLayer<L>
60where
61    L: Layer<Inode = u64, Handle = u64> + Send + Sync,
62{
63    type Inode = u64;
64    type Handle = u64;
65
66    fn init(&self, capable: FsOptions) -> io::Result<FsOptions> {
67        self.inner.init(capable)
68    }
69
70    fn destroy(&self) {
71        self.inner.destroy()
72    }
73
74    fn lookup(&self, ctx: &Context, parent: u64, name: &CStr) -> io::Result<Entry> {
75        self.inner.lookup(ctx, parent, name)
76    }
77
78    fn forget(&self, ctx: &Context, inode: u64, count: u64) {
79        self.inner.forget(ctx, inode, count)
80    }
81
82    fn batch_forget(&self, ctx: &Context, requests: Vec<(u64, u64)>) {
83        self.inner.batch_forget(ctx, requests)
84    }
85
86    fn getattr(
87        &self,
88        ctx: &Context,
89        inode: u64,
90        handle: Option<u64>,
91    ) -> io::Result<(stat64, Duration)> {
92        self.inner.getattr(ctx, inode, handle)
93    }
94
95    fn setattr(
96        &self,
97        ctx: &Context,
98        inode: u64,
99        attr: stat64,
100        handle: Option<u64>,
101        valid: SetattrValid,
102    ) -> io::Result<(stat64, Duration)> {
103        self.inner.setattr(ctx, inode, attr, handle, valid)
104    }
105
106    fn readlink(&self, ctx: &Context, inode: u64) -> io::Result<Vec<u8>> {
107        self.inner.readlink(ctx, inode)
108    }
109
110    fn unlink(&self, ctx: &Context, parent: u64, name: &CStr) -> io::Result<()> {
111        self.inner.unlink(ctx, parent, name)
112    }
113
114    fn rmdir(&self, ctx: &Context, parent: u64, name: &CStr) -> io::Result<()> {
115        self.inner.rmdir(ctx, parent, name)
116    }
117
118    /// Substitute a constant handle when the inner layer is handleless.
119    fn open(
120        &self,
121        ctx: &Context,
122        inode: u64,
123        flags: u32,
124        fuse_flags: u32,
125    ) -> io::Result<(Option<u64>, OpenOptions, Option<u32>)> {
126        let (handle, opts, passthrough) = self.inner.open(ctx, inode, flags, fuse_flags)?;
127        Ok((Some(handle.unwrap_or(0)), opts, passthrough))
128    }
129
130    #[allow(clippy::too_many_arguments)]
131    fn read(
132        &self,
133        ctx: &Context,
134        inode: u64,
135        handle: u64,
136        w: &mut dyn ZeroCopyWriter,
137        size: u32,
138        offset: u64,
139        lock_owner: Option<u64>,
140        flags: u32,
141    ) -> io::Result<usize> {
142        self.inner
143            .read(ctx, inode, handle, w, size, offset, lock_owner, flags)
144    }
145
146    #[allow(clippy::too_many_arguments)]
147    fn write(
148        &self,
149        ctx: &Context,
150        inode: u64,
151        handle: u64,
152        r: &mut dyn ZeroCopyReader,
153        size: u32,
154        offset: u64,
155        lock_owner: Option<u64>,
156        delayed_write: bool,
157        flags: u32,
158        fuse_flags: u32,
159    ) -> io::Result<usize> {
160        self.inner.write(
161            ctx,
162            inode,
163            handle,
164            r,
165            size,
166            offset,
167            lock_owner,
168            delayed_write,
169            flags,
170            fuse_flags,
171        )
172    }
173
174    fn release(
175        &self,
176        ctx: &Context,
177        inode: u64,
178        flags: u32,
179        handle: u64,
180        flush: bool,
181        flock_release: bool,
182        lock_owner: Option<u64>,
183    ) -> io::Result<()> {
184        self.inner
185            .release(ctx, inode, flags, handle, flush, flock_release, lock_owner)
186    }
187
188    fn statfs(&self, ctx: &Context, inode: u64) -> io::Result<statvfs64> {
189        self.inner.statfs(ctx, inode)
190    }
191
192    fn getxattr(
193        &self,
194        ctx: &Context,
195        inode: u64,
196        name: &CStr,
197        size: u32,
198    ) -> io::Result<GetxattrReply> {
199        self.inner.getxattr(ctx, inode, name, size)
200    }
201
202    fn listxattr(&self, ctx: &Context, inode: u64, size: u32) -> io::Result<ListxattrReply> {
203        self.inner.listxattr(ctx, inode, size)
204    }
205
206    /// Substitute a constant handle when the inner layer is handleless.
207    fn opendir(
208        &self,
209        ctx: &Context,
210        inode: u64,
211        flags: u32,
212    ) -> io::Result<(Option<u64>, OpenOptions)> {
213        let (handle, opts) = self.inner.opendir(ctx, inode, flags)?;
214        Ok((Some(handle.unwrap_or(0)), opts))
215    }
216
217    fn readdir(
218        &self,
219        ctx: &Context,
220        inode: u64,
221        handle: u64,
222        size: u32,
223        offset: u64,
224        add_entry: &mut dyn FnMut(DirEntry) -> io::Result<usize>,
225    ) -> io::Result<()> {
226        self.inner
227            .readdir(ctx, inode, handle, size, offset, add_entry)
228    }
229
230    fn readdirplus(
231        &self,
232        ctx: &Context,
233        inode: u64,
234        handle: u64,
235        size: u32,
236        offset: u64,
237        add_entry: &mut dyn FnMut(DirEntry, Entry) -> io::Result<usize>,
238    ) -> io::Result<()> {
239        self.inner
240            .readdirplus(ctx, inode, handle, size, offset, add_entry)
241    }
242
243    fn releasedir(&self, ctx: &Context, inode: u64, flags: u32, handle: u64) -> io::Result<()> {
244        self.inner.releasedir(ctx, inode, flags, handle)
245    }
246
247    fn access(&self, ctx: &Context, inode: u64, mask: u32) -> io::Result<()> {
248        self.inner.access(ctx, inode, mask)
249    }
250}
251
252impl<L> Layer for ZeroOpenLayer<L>
253where
254    L: Layer<Inode = u64, Handle = u64> + Send + Sync,
255{
256    fn root_inode(&self) -> u64 {
257        self.inner.root_inode()
258    }
259}