Skip to main content

fuser/ll/
mod.rs

1//! Low-level kernel communication.
2
3mod argument;
4pub mod fuse_abi;
5#[cfg(feature = "abi-7-11")]
6pub(crate) mod notify;
7pub(crate) mod reply;
8mod request;
9
10use std::{convert::TryInto, num::NonZeroI32, time::SystemTime};
11
12pub use reply::Response;
13pub use request::{
14    AnyRequest, FileHandle, INodeNo, Lock, Operation, Request, RequestError, RequestId, Version,
15};
16
17#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
18/// Possible input arguments for atime & mtime, which can either be set to a specified time,
19/// or to the current time
20pub enum TimeOrNow {
21    /// Specific time provided
22    SpecificTime(SystemTime),
23    /// Current time
24    Now,
25}
26
27macro_rules! errno {
28    ($x: expr) => {
29        Errno(unsafe {
30            // This is a static assertion that the constant $x is > 0
31            const _X: [(); 0 - !{
32                const ASSERT: bool = ($x > 0);
33                ASSERT
34            } as usize] = [];
35            // Which makes this safe
36            NonZeroI32::new_unchecked($x)
37        })
38    };
39}
40
41/// Represents an error code to be returned to the caller
42#[derive(Debug)]
43pub struct Errno(pub NonZeroI32);
44impl Errno {
45    /// Operation not permitted
46    pub const EPERM: Errno = errno!(libc::EPERM);
47    /// No such file or directory
48    pub const ENOENT: Errno = errno!(libc::ENOENT);
49    /// No such process
50    pub const ESRCH: Errno = errno!(libc::ESRCH);
51    /// Interrupted system call
52    pub const EINTR: Errno = errno!(libc::EINTR);
53    /// Input/output error
54    pub const EIO: Errno = errno!(libc::EIO);
55    /// No such device or address
56    pub const ENXIO: Errno = errno!(libc::ENXIO);
57    /// Argument list too long
58    pub const E2BIG: Errno = errno!(libc::E2BIG);
59    /// Exec format error
60    pub const ENOEXEC: Errno = errno!(libc::ENOEXEC);
61    /// Bad file descriptor
62    pub const EBADF: Errno = errno!(libc::EBADF);
63    /// No child processes
64    pub const ECHILD: Errno = errno!(libc::ECHILD);
65    /// Resource temporarily unavailable
66    pub const EAGAIN: Errno = errno!(libc::EAGAIN);
67    /// Cannot allocate memory
68    pub const ENOMEM: Errno = errno!(libc::ENOMEM);
69    /// Permission denied
70    pub const EACCES: Errno = errno!(libc::EACCES);
71    /// Bad address
72    pub const EFAULT: Errno = errno!(libc::EFAULT);
73    /// Block device required
74    pub const ENOTBLK: Errno = errno!(libc::ENOTBLK);
75    /// Device or resource busy
76    pub const EBUSY: Errno = errno!(libc::EBUSY);
77    /// File exists
78    pub const EEXIST: Errno = errno!(libc::EEXIST);
79    /// Invalid cross-device link
80    pub const EXDEV: Errno = errno!(libc::EXDEV);
81    /// No such device
82    pub const ENODEV: Errno = errno!(libc::ENODEV);
83    /// Not a directory
84    pub const ENOTDIR: Errno = errno!(libc::ENOTDIR);
85    /// Is a directory
86    pub const EISDIR: Errno = errno!(libc::EISDIR);
87    /// Invalid argument
88    pub const EINVAL: Errno = errno!(libc::EINVAL);
89    /// Too many open files in system
90    pub const ENFILE: Errno = errno!(libc::ENFILE);
91    /// Too many open files
92    pub const EMFILE: Errno = errno!(libc::EMFILE);
93    /// Inappropriate ioctl for device
94    pub const ENOTTY: Errno = errno!(libc::ENOTTY);
95    /// Text file busy
96    pub const ETXTBSY: Errno = errno!(libc::ETXTBSY);
97    /// File too large
98    pub const EFBIG: Errno = errno!(libc::EFBIG);
99    /// No space left on device
100    pub const ENOSPC: Errno = errno!(libc::ENOSPC);
101    /// Illegal seek
102    pub const ESPIPE: Errno = errno!(libc::ESPIPE);
103    /// Read-only file system
104    pub const EROFS: Errno = errno!(libc::EROFS);
105    /// Too many links
106    pub const EMLINK: Errno = errno!(libc::EMLINK);
107    /// Broken pipe
108    pub const EPIPE: Errno = errno!(libc::EPIPE);
109    /// Numerical argument out of domain
110    pub const EDOM: Errno = errno!(libc::EDOM);
111    /// Numerical result out of range
112    pub const ERANGE: Errno = errno!(libc::ERANGE);
113    /// Resource deadlock avoided
114    pub const EDEADLK: Errno = errno!(libc::EDEADLK);
115    /// File name too long
116    pub const ENAMETOOLONG: Errno = errno!(libc::ENAMETOOLONG);
117    /// No locks available
118    pub const ENOLCK: Errno = errno!(libc::ENOLCK);
119    /// Function not implemented
120    pub const ENOSYS: Errno = errno!(libc::ENOSYS);
121    /// Directory not empty
122    pub const ENOTEMPTY: Errno = errno!(libc::ENOTEMPTY);
123    /// Too many levels of symbolic links
124    pub const ELOOP: Errno = errno!(libc::ELOOP);
125    /// Resource temporarily unavailable
126    pub const EWOULDBLOCK: Errno = errno!(libc::EWOULDBLOCK);
127    /// No message of desired type
128    pub const ENOMSG: Errno = errno!(libc::ENOMSG);
129    /// Identifier removed
130    pub const EIDRM: Errno = errno!(libc::EIDRM);
131    /// Object is remote
132    pub const EREMOTE: Errno = errno!(libc::EREMOTE);
133    /// Link has been severed
134    pub const ENOLINK: Errno = errno!(libc::ENOLINK);
135    /// Protocol error
136    pub const EPROTO: Errno = errno!(libc::EPROTO);
137    /// Multihop attempted
138    pub const EMULTIHOP: Errno = errno!(libc::EMULTIHOP);
139    /// Bad message
140    pub const EBADMSG: Errno = errno!(libc::EBADMSG);
141    /// Value too large for defined data type
142    pub const EOVERFLOW: Errno = errno!(libc::EOVERFLOW);
143    /// Invalid or incomplete multibyte or wide character
144    pub const EILSEQ: Errno = errno!(libc::EILSEQ);
145    /// Too many users
146    pub const EUSERS: Errno = errno!(libc::EUSERS);
147    /// Socket operation on non-socket
148    pub const ENOTSOCK: Errno = errno!(libc::ENOTSOCK);
149    /// Destination address required
150    pub const EDESTADDRREQ: Errno = errno!(libc::EDESTADDRREQ);
151    /// Message too long
152    pub const EMSGSIZE: Errno = errno!(libc::EMSGSIZE);
153    /// Protocol wrong type for socket
154    pub const EPROTOTYPE: Errno = errno!(libc::EPROTOTYPE);
155    /// Protocol not available
156    pub const ENOPROTOOPT: Errno = errno!(libc::ENOPROTOOPT);
157    /// Protocol not supported
158    pub const EPROTONOSUPPORT: Errno = errno!(libc::EPROTONOSUPPORT);
159    /// Socket type not supported
160    pub const ESOCKTNOSUPPORT: Errno = errno!(libc::ESOCKTNOSUPPORT);
161    /// Operation not supported
162    pub const EOPNOTSUPP: Errno = errno!(libc::EOPNOTSUPP);
163    /// Protocol family not supported
164    pub const EPFNOSUPPORT: Errno = errno!(libc::EPFNOSUPPORT);
165    /// Address family not supported by protocol
166    pub const EAFNOSUPPORT: Errno = errno!(libc::EAFNOSUPPORT);
167    /// Address already in use
168    pub const EADDRINUSE: Errno = errno!(libc::EADDRINUSE);
169    /// Cannot assign requested address
170    pub const EADDRNOTAVAIL: Errno = errno!(libc::EADDRNOTAVAIL);
171    /// Network is down
172    pub const ENETDOWN: Errno = errno!(libc::ENETDOWN);
173    /// Network is unreachable
174    pub const ENETUNREACH: Errno = errno!(libc::ENETUNREACH);
175    /// Network dropped connection on reset
176    pub const ENETRESET: Errno = errno!(libc::ENETRESET);
177    /// Software caused connection abort
178    pub const ECONNABORTED: Errno = errno!(libc::ECONNABORTED);
179    /// Connection reset by peer
180    pub const ECONNRESET: Errno = errno!(libc::ECONNRESET);
181    /// No buffer space available
182    pub const ENOBUFS: Errno = errno!(libc::ENOBUFS);
183    /// Transport endpoint is already connected
184    pub const EISCONN: Errno = errno!(libc::EISCONN);
185    /// Transport endpoint is not connected
186    pub const ENOTCONN: Errno = errno!(libc::ENOTCONN);
187    /// Cannot send after transport endpoint shutdown
188    pub const ESHUTDOWN: Errno = errno!(libc::ESHUTDOWN);
189    /// Too many references: cannot splice
190    pub const ETOOMANYREFS: Errno = errno!(libc::ETOOMANYREFS);
191    /// Connection timed out
192    pub const ETIMEDOUT: Errno = errno!(libc::ETIMEDOUT);
193    /// Connection refused
194    pub const ECONNREFUSED: Errno = errno!(libc::ECONNREFUSED);
195    /// Host is down
196    pub const EHOSTDOWN: Errno = errno!(libc::EHOSTDOWN);
197    /// No route to host
198    pub const EHOSTUNREACH: Errno = errno!(libc::EHOSTUNREACH);
199    /// Operation already in progress
200    pub const EALREADY: Errno = errno!(libc::EALREADY);
201    /// Operation now in progress
202    pub const EINPROGRESS: Errno = errno!(libc::EINPROGRESS);
203    /// Stale file handle
204    pub const ESTALE: Errno = errno!(libc::ESTALE);
205    /// Disk quota exceeded
206    pub const EDQUOT: Errno = errno!(libc::EDQUOT);
207    /// Operation cancelled
208    pub const ECANCELED: Errno = errno!(libc::ECANCELED);
209    /// Owner died
210    pub const EOWNERDEAD: Errno = errno!(libc::EOWNERDEAD);
211    /// State not recoverable
212    pub const ENOTRECOVERABLE: Errno = errno!(libc::ENOTRECOVERABLE);
213    /// Operation not supported
214    pub const ENOTSUP: Errno = errno!(libc::ENOTSUP);
215
216    /// No data available
217    #[cfg(target_os = "linux")]
218    pub const ENODATA: Errno = errno!(libc::ENODATA);
219    /// Attribute not found
220    #[cfg(not(target_os = "linux"))]
221    pub const ENOATTR: Errno = errno!(libc::ENOATTR);
222
223    /// Use this as an error return from getxattr/removexattr to indicate that the xattr doesn't
224    /// exist.  This resolves to the appropriate platform specific error code.
225    #[cfg(target_os = "linux")]
226    pub const NO_XATTR: Errno = Self::ENODATA;
227    #[cfg(not(target_os = "linux"))]
228    pub const NO_XATTR: Errno = Self::ENOATTR;
229
230    pub fn from_i32(err: i32) -> Errno {
231        err.try_into().ok().map(Errno).unwrap_or(Errno::EIO)
232    }
233}
234impl From<std::io::Error> for Errno {
235    fn from(err: std::io::Error) -> Self {
236        let errno = err.raw_os_error().unwrap_or(0);
237        match errno.try_into() {
238            Ok(i) => Errno(i),
239            Err(_) => Errno::EIO,
240        }
241    }
242}
243impl From<std::io::ErrorKind> for Errno {
244    fn from(x: std::io::ErrorKind) -> Self {
245        let err: std::io::Error = x.into();
246        err.into()
247    }
248}
249impl From<Errno> for i32 {
250    fn from(x: Errno) -> Self {
251        x.0.into()
252    }
253}
254
255/// A newtype for generation numbers
256///
257/// If the file system will be exported over NFS, the (ino, generation) pairs
258/// need to be unique over the file system's lifetime (rather than just the
259/// mount time). So if the file system reuses an inode after it has been
260/// deleted, it must assign a new, previously unused generation number to the
261/// inode at the same time.
262#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
263pub struct Generation(pub u64);
264impl From<Generation> for u64 {
265    fn from(fh: Generation) -> Self {
266        fh.0
267    }
268}
269
270#[cfg(test)]
271mod test {
272    use std::io::IoSlice;
273    use std::ops::{Deref, DerefMut};
274    /// If we want to be able to cast bytes to our fuse C struct types we need it
275    /// to be aligned.  This struct helps getting &[u8]s which are 8 byte aligned.
276    #[cfg(test)]
277    #[repr(align(8))]
278    pub(crate) struct AlignedData<T>(pub T);
279    impl<T> Deref for AlignedData<T> {
280        type Target = T;
281
282        fn deref(&self) -> &Self::Target {
283            &self.0
284        }
285    }
286    impl<T> DerefMut for AlignedData<T> {
287        fn deref_mut(&mut self) -> &mut Self::Target {
288            &mut self.0
289        }
290    }
291
292    pub fn ioslice_to_vec(s: &[IoSlice<'_>]) -> Vec<u8> {
293        let mut v = Vec::with_capacity(s.iter().map(|x| x.len()).sum());
294        for x in s {
295            v.extend_from_slice(x);
296        }
297        v
298    }
299}