easy_fuser 0.7.1

A flexible and idiomatic Fuse implementation for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Read, write, and close files through file descriptors.
//!
//! These helpers provide selected FUSE methods for filesystems whose file
//! handles contain open file descriptors. They are method helpers, not complete
//! `FuseHandler` implementations. Add them as fields in your handler and
//! delegate only the methods they provide.
//!
//! # Methods provided
//!
//! `FileDescriptorHandlerReadOnly<TId>` provides `flush`, `fsync`, `getlk`,
//! `ioctl`, `lseek`, `read`, `release`, and `setlk`.
//!
//! `FileDescriptorHandler<TId>` provides those same methods, plus `copy_file_range`,
//! `fallocate`, and `write`.
//!
//! With the `async` feature, `FileDescriptorHandlerAsync` and
//! `FileDescriptorHandlerReadOnlyAsync` expose the same methods as async
//! functions for use with `delegate_fs_async!`. On Linux, enabling `io_uring`
//! makes their `read`, `write`, `flush`, `fsync`, and `fallocate` methods use
//! io_uring. Other methods, and all methods on BSD/macOS, retain the synchronous
//! `unix_fs` implementation.
//!
//! The current io_uring implementation creates and drops a ring for each
//! operation. In a warm local-file benchmark it delivered roughly 3–10x lower
//! throughput than async syscalls. This describes the current implementation,
//! not io_uring's general performance; treat the feature as experimental and
//! opt-in until a persistent-ring implementation is measured.
//!
//! # Methods your filesystem still provides
//!
//! Your `open` and `create` methods must return valid open file descriptors as
//! file handles. The helpers use those descriptors for later reads or writes,
//! and `release` closes them. Other methods, such as `lookup`, `getattr`, and
//! `readdir`, belong to your filesystem or another preset.
//!
//! `FileDescriptorHandlerReadOnly` only omits write methods; use a read-only mount
//! option if the whole mounted filesystem must reject changes.

use crate::types::*;
use crate::unix_fs;
use std::marker::PhantomData;
use std::os::fd::{AsFd, OwnedFd};

#[doc(hidden)]
#[macro_export]
macro_rules! file_descriptor_io_call {
    ($this:expr, flush, $fd:expr, async) => {{
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        {
            $this.io_uring.flush($fd).await
        }
        #[cfg(not(all(target_os = "linux", feature = "io_uring")))]
        {
            $crate::unix_fs::flush($fd)
        }
    }};
    ($this:expr, flush, $fd:expr) => {
        $crate::unix_fs::flush($fd)
    };
    ($this:expr, fsync, $fd:expr, $datasync:expr, async) => {{
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        {
            $this.io_uring.fsync($fd, $datasync).await
        }
        #[cfg(not(all(target_os = "linux", feature = "io_uring")))]
        {
            $crate::unix_fs::fsync($fd, $datasync)
        }
    }};
    ($this:expr, fsync, $fd:expr, $datasync:expr) => {
        $crate::unix_fs::fsync($fd, $datasync)
    };
    ($this:expr, release, $fd:expr, async) => {{
        let fd = $fd;
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        {
            let raw_fd = std::os::fd::AsRawFd::as_raw_fd(&fd);
            $this.io_uring.wait_for_fd(raw_fd).await;
            $crate::unix_fs::release(fd)
        }
        #[cfg(not(all(target_os = "linux", feature = "io_uring")))]
        {
            $crate::unix_fs::release(fd)
        }
    }};
    ($this:expr, release, $fd:expr) => {
        $crate::unix_fs::release($fd)
    };
    ($this:expr, read, $fd:expr, $seek:expr, $size:expr, async) => {{
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        {
            $this.io_uring.read($fd, $seek, $size).await
        }
        #[cfg(not(all(target_os = "linux", feature = "io_uring")))]
        {
            $crate::unix_fs::read($fd, $seek, $size)
        }
    }};
    ($this:expr, read, $fd:expr, $seek:expr, $size:expr) => {
        $crate::unix_fs::read($fd, $seek, $size)
    };
    ($this:expr, write, $fd:expr, $seek:expr, $data:expr, async) => {{
        let data = $data;
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        {
            $this.io_uring.write($fd, $seek, data).await
        }
        #[cfg(not(all(target_os = "linux", feature = "io_uring")))]
        {
            $crate::unix_fs::write($fd, $seek, &data)
        }
    }};
    ($this:expr, write, $fd:expr, $seek:expr, $data:expr) => {
        $crate::unix_fs::write($fd, $seek, &$data)
    };
    ($this:expr, fallocate, $fd:expr, $offset:expr, $length:expr, $mode:expr, async) => {{
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        {
            $this.io_uring.fallocate($fd, $offset, $length, $mode).await
        }
        #[cfg(not(all(target_os = "linux", feature = "io_uring")))]
        {
            $crate::unix_fs::fallocate($fd, $offset, $length, $mode)
        }
    }};
    ($this:expr, fallocate, $fd:expr, $offset:expr, $length:expr, $mode:expr) => {
        $crate::unix_fs::fallocate($fd, $offset, $length, $mode)
    };
}

macro_rules! file_descriptor_handler_readonly_methods {
    ($file_id:path $(, $asyncness:ident)?) => {
        pub $( $asyncness )? fn flush<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            _lock_owner: u64,
        ) -> FuseResult<()> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            $crate::file_descriptor_io_call!(self, flush, fd.as_fd() $(, $asyncness)?)
        }

        pub $( $asyncness )? fn fsync<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            datasync: bool,
        ) -> FuseResult<()> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            $crate::file_descriptor_io_call!(self, fsync, fd.as_fd(), datasync $(, $asyncness)?)
        }

        pub $( $asyncness )? fn lseek<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            seek: SeekFrom,
        ) -> FuseResult<i64> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            unix_fs::lseek(fd.as_fd(), seek)
        }

        pub $( $asyncness )? fn read<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            seek: SeekFrom,
            size: u32,
            _flags: OpenFlags,
            _lock_owner: Option<u64>,
        ) -> FuseResult<Vec<u8>> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            $crate::file_descriptor_io_call!(self, read, fd.as_fd(), seek, size as usize $(, $asyncness)?)
        }

        pub $( $asyncness )? fn release(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<OwnedFd>,
            _flags: OpenFlags,
            _lock_owner: Option<u64>,
            _flush: bool,
        ) -> FuseResult<()> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            $crate::file_descriptor_io_call!(self, release, fd $(, $asyncness)?)
        }

        pub $( $asyncness )? fn getlk<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            lock_owner: u64,
            lock_info: LockInfo,
        ) -> FuseResult<LockInfo> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            unix_fs::getlk(fd.as_fd(), lock_owner, lock_info)
        }

        pub $( $asyncness )? fn ioctl<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            _flags: IoctlFlags,
            cmd: u32,
            in_data: Vec<u8>,
            out_size: u32,
        ) -> FuseResult<(i32, Vec<u8>)> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            unix_fs::ioctl(fd.as_fd(), cmd, in_data, out_size)
        }

        pub $( $asyncness )? fn setlk<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            lock_owner: u64,
            lock_info: LockInfo,
            sleep: bool,
        ) -> FuseResult<()> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            unix_fs::setlk(fd.as_fd(), lock_owner, lock_info, sleep)
        }
    };
}

macro_rules! file_descriptor_handler_readwrite_methods {
    ($file_id:path $(, $asyncness:ident)?) => {
        pub $( $asyncness )? fn copy_file_range<'a, 'b>(
            &self,
            _req: &RequestInfo,
            _file_in: $file_id,
            file_handle_in: Option<&OwnedFd>,
            offset_in: u64,
            _file_out: $file_id,
            file_handle_out: Option<&OwnedFd>,
            offset_out: u64,
            len: u64,
            _flags: CopyFileRangeFlags,
        ) -> FuseResult<u32> {
            unix_fs::copy_file_range(
                file_handle_in
                    .ok_or_else(|| {
                        ErrorKind::BadFileDescriptor.to_error("missing source file handle")
                    })?
                    .as_fd(),
                offset_in as i64,
                file_handle_out
                    .ok_or_else(|| {
                        ErrorKind::BadFileDescriptor.to_error("missing destination file handle")
                    })?
                    .as_fd(),
                offset_out as i64,
                len,
            )
        }

        pub $( $asyncness )? fn fallocate<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            offset: i64,
            length: i64,
            mode: FallocateFlags,
        ) -> FuseResult<()> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            $crate::file_descriptor_io_call!(self, fallocate, fd.as_fd(), offset, length, mode $(, $asyncness)?)
        }

        pub $( $asyncness )? fn write<'a>(
            &self,
            _req: &RequestInfo,
            _file_id: $file_id,
            file_handle: Option<&mut OwnedFd>,
            seek: SeekFrom,
            data: Vec<u8>,
            _write_flags: WriteFlags,
            _flags: OpenFlags,
            _lock_owner: Option<u64>,
        ) -> FuseResult<u32> {
            let fd = file_handle
                .ok_or_else(|| ErrorKind::BadFileDescriptor.to_error("missing file handle"))?;
            $crate::file_descriptor_io_call!(self, write, fd.as_fd(), seek, data $(, $asyncness)?)
                .map(|res| res as u32)
        }
    };
}

/// File-descriptor backed helpers for read and write operations.
///
/// Provides descriptor-backed `flush`, `fsync`, `getlk`, `ioctl`, `lseek`,
/// `read`, `release`, and `setlk`, plus `copy_file_range`, `fallocate`, and
/// `write`. Use `OwnedFd` as your handler's `FileHandle` and return it from
/// `open` and `create`.
pub struct FileDescriptorHandler<TId: FileIdType> {
    phantom: PhantomData<TId>,
}

impl<TId: FileIdType> Default for FileDescriptorHandler<TId> {
    fn default() -> Self {
        Self::new()
    }
}

impl<TId: FileIdType> FileDescriptorHandler<TId> {
    pub fn new() -> Self {
        Self {
            phantom: PhantomData,
        }
    }
}

impl<TId: FileIdType> FileDescriptorHandler<TId> {
    file_descriptor_handler_readonly_methods!(TId);
    file_descriptor_handler_readwrite_methods!(TId);
}

/// Read-only file-descriptor backed helpers.
///
/// Provides descriptor-backed `flush`, `fsync`, `getlk`, `ioctl`, `lseek`,
/// `read`, `release`, and `setlk`. Your `open` method must return an open file
/// descriptor as its `FileHandle`.
pub struct FileDescriptorHandlerReadOnly<TId: FileIdType> {
    phantom: PhantomData<TId>,
}

impl<TId: FileIdType> Default for FileDescriptorHandlerReadOnly<TId> {
    fn default() -> Self {
        Self::new()
    }
}

impl<TId: FileIdType> FileDescriptorHandlerReadOnly<TId> {
    pub fn new() -> Self {
        Self {
            phantom: PhantomData,
        }
    }
}

impl<TId: FileIdType> FileDescriptorHandlerReadOnly<TId> {
    file_descriptor_handler_readonly_methods!(TId);
}

/// Async file-descriptor helpers.
///
/// These methods satisfy the async `FuseHandler` interface. On Linux, enabling
/// the `io_uring` feature uses io_uring for `read`, `write`, `flush`, `fsync`,
/// and `fallocate`; remaining methods use synchronous `unix_fs` calls. On
/// BSD/macOS, methods use the synchronous implementation. The ring-backed
/// methods own their buffers while requests are in flight, and `release` waits
/// for requests submitted through this helper before closing the descriptor.
#[cfg(feature = "async")]
pub struct FileDescriptorHandlerAsync<TId: FileIdType> {
    phantom: PhantomData<TId>,
    #[cfg(all(target_os = "linux", feature = "io_uring"))]
    io_uring: unix_fs::io_uring::IoUringExecutor,
}

#[cfg(feature = "async")]
impl<TId: FileIdType> Default for FileDescriptorHandlerAsync<TId> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "async")]
impl<TId: FileIdType> FileDescriptorHandlerAsync<TId> {
    pub fn new() -> Self {
        Self {
            phantom: PhantomData,
            #[cfg(all(target_os = "linux", feature = "io_uring"))]
            io_uring: unix_fs::io_uring::IoUringExecutor::new(),
        }
    }
}

#[cfg(feature = "async")]
impl<TId: FileIdType> FileDescriptorHandlerAsync<TId> {
    file_descriptor_handler_readonly_methods!(TId, async);
    file_descriptor_handler_readwrite_methods!(TId, async);
}

/// Async read-only file-descriptor helpers.
///
/// With Linux feature `io_uring`, `read`, `flush`, and `fsync` use io_uring;
/// other methods use synchronous `unix_fs` calls. On BSD/macOS they all remain
/// synchronous.
#[cfg(feature = "async")]
pub struct FileDescriptorHandlerReadOnlyAsync<TId: FileIdType> {
    phantom: PhantomData<TId>,
    #[cfg(all(target_os = "linux", feature = "io_uring"))]
    io_uring: unix_fs::io_uring::IoUringExecutor,
}

#[cfg(feature = "async")]
impl<TId: FileIdType> Default for FileDescriptorHandlerReadOnlyAsync<TId> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "async")]
impl<TId: FileIdType> FileDescriptorHandlerReadOnlyAsync<TId> {
    pub fn new() -> Self {
        Self {
            phantom: PhantomData,
            #[cfg(all(target_os = "linux", feature = "io_uring"))]
            io_uring: unix_fs::io_uring::IoUringExecutor::new(),
        }
    }
}

#[cfg(feature = "async")]
impl<TId: FileIdType> FileDescriptorHandlerReadOnlyAsync<TId> {
    file_descriptor_handler_readonly_methods!(TId, async);
}

/// Deprecated alias for [`FileDescriptorHandler`].
#[deprecated(since = "0.8.0", note = "use `FileDescriptorHandler` instead")]
pub type FdHandlerHelper<TId> = FileDescriptorHandler<TId>;

/// Deprecated alias for [`FileDescriptorHandlerReadOnly`].
#[deprecated(since = "0.8.0", note = "use `FileDescriptorHandlerReadOnly` instead")]
pub type FdHandlerHelperReadOnly<TId> = FileDescriptorHandlerReadOnly<TId>;

pub(super) use file_descriptor_handler_readonly_methods;
pub(super) use file_descriptor_handler_readwrite_methods;

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn missing_resource_returns_bad_file_descriptor() {
        let request = RequestInfo {
            id: RequestId(0),
            uid: 0,
            gid: 0,
            pid: 0,
        };
        let handler = FileDescriptorHandler::<PathBuf>::new();

        let read_error = handler
            .read(
                &request,
                PathBuf::new(),
                None,
                SeekFrom::Start(0),
                1,
                OpenFlags(0),
                None,
            )
            .unwrap_err();
        assert_eq!(read_error.kind(), ErrorKind::BadFileDescriptor);

        let release_error = handler
            .release(&request, PathBuf::new(), None, OpenFlags(0), None, false)
            .unwrap_err();
        assert_eq!(release_error.kind(), ErrorKind::BadFileDescriptor);
    }
}