1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
7 */
8
9 #ifndef FUSE_LOWLEVEL_H_
10 #define FUSE_LOWLEVEL_H_
11
12 /** @file
13 *
14 * Low level API
15 *
16 * IMPORTANT: you should define FUSE_USE_VERSION before including this
17 * header. To use the newest API define it to 35 (recommended for any
18 * new application).
19 */
20
21 #ifndef FUSE_USE_VERSION
22 #error FUSE_USE_VERSION not defined
23 #endif
24
25 #include "fuse_common.h"
26
27 #include <utime.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/statvfs.h>
32 #include <sys/uio.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /* ----------------------------------------------------------- *
39 * Miscellaneous definitions *
40 * ----------------------------------------------------------- */
41
42 /** The node ID of the root inode */
43 #define FUSE_ROOT_ID 1
44
45 /** Inode number type */
46 typedef uint64_t fuse_ino_t;
47
48 /** Request pointer type */
49 typedef struct fuse_req *fuse_req_t;
50
51 /**
52 * Session
53 *
54 * This provides hooks for processing requests, and exiting
55 */
56 struct fuse_session;
57
58 /** Directory entry parameters supplied to fuse_reply_entry() */
59 struct fuse_entry_param {
60 /** Unique inode number
61 *
62 * In lookup, zero means negative entry (from version 2.5)
63 * Returning ENOENT also means negative entry, but by setting zero
64 * ino the kernel may cache negative entries for entry_timeout
65 * seconds.
66 */
67 fuse_ino_t ino;
68
69 /** Generation number for this entry.
70 *
71 * If the file system will be exported over NFS, the
72 * ino/generation pairs need to be unique over the file
73 * system's lifetime (rather than just the mount time). So if
74 * the file system reuses an inode after it has been deleted,
75 * it must assign a new, previously unused generation number
76 * to the inode at the same time.
77 *
78 */
79 uint64_t generation;
80
81 /** Inode attributes.
82 *
83 * Even if attr_timeout == 0, attr must be correct. For example,
84 * for open(), FUSE uses attr.st_size from lookup() to determine
85 * how many bytes to request. If this value is not correct,
86 * incorrect data will be returned.
87 */
88 struct stat attr;
89
90 /** Validity timeout (in seconds) for inode attributes. If
91 attributes only change as a result of requests that come
92 through the kernel, this should be set to a very large
93 value. */
94 double attr_timeout;
95
96 /** Validity timeout (in seconds) for the name. If directory
97 entries are changed/deleted only as a result of requests
98 that come through the kernel, this should be set to a very
99 large value. */
100 double entry_timeout;
101 uint64_t backing_action;
102 uint64_t backing_fd;
103 uint64_t bpf_action;
104 uint64_t bpf_fd;
105 };
106
107 /**
108 * Additional context associated with requests.
109 *
110 * Note that the reported client uid, gid and pid may be zero in some
111 * situations. For example, if the FUSE file system is running in a
112 * PID or user namespace but then accessed from outside the namespace,
113 * there is no valid uid/pid/gid that could be reported.
114 */
115 struct fuse_ctx {
116 /** User ID of the calling process */
117 uid_t uid;
118
119 /** Group ID of the calling process */
120 gid_t gid;
121
122 /** Thread ID of the calling process */
123 pid_t pid;
124
125 /** Umask of the calling process */
126 mode_t umask;
127 };
128
129 struct fuse_forget_data {
130 fuse_ino_t ino;
131 uint64_t nlookup;
132 };
133
134 struct fuse_custom_io {
135 ssize_t (*writev)(int fd, struct iovec *iov, int count, void *userdata);
136 ssize_t (*read)(int fd, void *buf, size_t buf_len, void *userdata);
137 ssize_t (*splice_receive)(int fdin, off_t *offin, int fdout,
138 off_t *offout, size_t len,
139 unsigned int flags, void *userdata);
140 ssize_t (*splice_send)(int fdin, off_t *offin, int fdout,
141 off_t *offout, size_t len,
142 unsigned int flags, void *userdata);
143 int (*clone_fd)(int master_fd);
144 };
145
146 /**
147 * Flags for fuse_lowlevel_notify_entry()
148 * 0 = invalidate entry
149 * FUSE_LL_EXPIRE_ONLY = expire entry
150 */
151 enum fuse_notify_entry_flags {
152 FUSE_LL_INVALIDATE = 0,
153 FUSE_LL_EXPIRE_ONLY = (1 << 0),
154 };
155
156 /* 'to_set' flags in setattr */
157 #define FUSE_SET_ATTR_MODE (1 << 0)
158 #define FUSE_SET_ATTR_UID (1 << 1)
159 #define FUSE_SET_ATTR_GID (1 << 2)
160 #define FUSE_SET_ATTR_SIZE (1 << 3)
161 #define FUSE_SET_ATTR_ATIME (1 << 4)
162 #define FUSE_SET_ATTR_MTIME (1 << 5)
163 #define FUSE_SET_ATTR_ATIME_NOW (1 << 7)
164 #define FUSE_SET_ATTR_MTIME_NOW (1 << 8)
165 #define FUSE_SET_ATTR_FORCE (1 << 9)
166 #define FUSE_SET_ATTR_CTIME (1 << 10)
167 #define FUSE_SET_ATTR_KILL_SUID (1 << 11)
168 #define FUSE_SET_ATTR_KILL_SGID (1 << 12)
169 #define FUSE_SET_ATTR_FILE (1 << 13)
170 #define FUSE_SET_ATTR_KILL_PRIV (1 << 14)
171 #define FUSE_SET_ATTR_OPEN (1 << 15)
172 #define FUSE_SET_ATTR_TIMES_SET (1 << 16)
173 #define FUSE_SET_ATTR_TOUCH (1 << 17)
174
175 /* ----------------------------------------------------------- *
176 * structs from fuse_kernel.h *
177 * ----------------------------------------------------------- */
178 struct fuse_entry_out;
179 struct fuse_entry_bpf_out;
180
181 /* ----------------------------------------------------------- *
182 * Request methods and replies *
183 * ----------------------------------------------------------- */
184
185 /**
186 * Low level filesystem operations
187 *
188 * Most of the methods (with the exception of init and destroy)
189 * receive a request handle (fuse_req_t) as their first argument.
190 * This handle must be passed to one of the specified reply functions.
191 *
192 * This may be done inside the method invocation, or after the call
193 * has returned. The request handle is valid until one of the reply
194 * functions is called.
195 *
196 * Other pointer arguments (name, fuse_file_info, etc) are not valid
197 * after the call has returned, so if they are needed later, their
198 * contents have to be copied.
199 *
200 * In general, all methods are expected to perform any necessary
201 * permission checking. However, a filesystem may delegate this task
202 * to the kernel by passing the `default_permissions` mount option to
203 * `fuse_session_new()`. In this case, methods will only be called if
204 * the kernel's permission check has succeeded.
205 *
206 * The filesystem sometimes needs to handle a return value of -ENOENT
207 * from the reply function, which means, that the request was
208 * interrupted, and the reply discarded. For example if
209 * fuse_reply_open() return -ENOENT means, that the release method for
210 * this file will not be called.
211 */
212 struct fuse_lowlevel_ops {
213 /**
214 * Initialize filesystem
215 *
216 * This function is called when libfuse establishes
217 * communication with the FUSE kernel module. The file system
218 * should use this module to inspect and/or modify the
219 * connection parameters provided in the `conn` structure.
220 *
221 * Note that some parameters may be overwritten by options
222 * passed to fuse_session_new() which take precedence over the
223 * values set in this handler.
224 *
225 * There's no reply to this function
226 *
227 * @param userdata the user data passed to fuse_session_new()
228 */
229 void (*init) (void *userdata, struct fuse_conn_info *conn);
230
231 /**
232 * Clean up filesystem.
233 *
234 * Called on filesystem exit. When this method is called, the
235 * connection to the kernel may be gone already, so that eg. calls
236 * to fuse_lowlevel_notify_* will fail.
237 *
238 * There's no reply to this function
239 *
240 * @param userdata the user data passed to fuse_session_new()
241 */
242 void (*destroy) (void *userdata);
243
244 /**
245 * Look up a directory entry by name and get its attributes.
246 *
247 * Valid replies:
248 * fuse_reply_entry
249 * fuse_reply_err
250 *
251 * @param req request handle
252 * @param parent inode number of the parent directory
253 * @param name the name to look up
254 */
255 void (*lookup) (fuse_req_t req, fuse_ino_t parent, const char *name);
256
257 /**
258 * post filter a lookup
259 *
260 * Valid replies:
261 * fuse_reply_entry
262 * fuse_reply_err
263 *
264 * @param req request handle
265 * @param parent inode number of the parent directory
266 * @param error_in the error, or 0, of the lookup
267 * @param name the name that was looked up
268 * @param feo the fuse entry out struct from the lookup
269 * @param febo the fuse entry bpf out struct from the lookup
270 */
271 void (*lookup_postfilter)(fuse_req_t req, fuse_ino_t parent,
272 uint32_t error_in, const char *name,
273 struct fuse_entry_out *feo,
274 struct fuse_entry_bpf_out *febo);
275
276 /**
277 * Forget about an inode
278 *
279 * This function is called when the kernel removes an inode
280 * from its internal caches.
281 *
282 * The inode's lookup count increases by one for every call to
283 * fuse_reply_entry and fuse_reply_create. The nlookup parameter
284 * indicates by how much the lookup count should be decreased.
285 *
286 * Inodes with a non-zero lookup count may receive request from
287 * the kernel even after calls to unlink, rmdir or (when
288 * overwriting an existing file) rename. Filesystems must handle
289 * such requests properly and it is recommended to defer removal
290 * of the inode until the lookup count reaches zero. Calls to
291 * unlink, rmdir or rename will be followed closely by forget
292 * unless the file or directory is open, in which case the
293 * kernel issues forget only after the release or releasedir
294 * calls.
295 *
296 * Note that if a file system will be exported over NFS the
297 * inodes lifetime must extend even beyond forget. See the
298 * generation field in struct fuse_entry_param above.
299 *
300 * On unmount the lookup count for all inodes implicitly drops
301 * to zero. It is not guaranteed that the file system will
302 * receive corresponding forget messages for the affected
303 * inodes.
304 *
305 * Valid replies:
306 * fuse_reply_none
307 *
308 * @param req request handle
309 * @param ino the inode number
310 * @param nlookup the number of lookups to forget
311 */
312 void (*forget) (fuse_req_t req, fuse_ino_t ino, uint64_t nlookup);
313
314 /**
315 * Get file attributes.
316 *
317 * If writeback caching is enabled, the kernel may have a
318 * better idea of a file's length than the FUSE file system
319 * (eg if there has been a write that extended the file size,
320 * but that has not yet been passed to the filesystem.n
321 *
322 * In this case, the st_size value provided by the file system
323 * will be ignored.
324 *
325 * Valid replies:
326 * fuse_reply_attr
327 * fuse_reply_err
328 *
329 * @param req request handle
330 * @param ino the inode number
331 * @param fi for future use, currently always NULL
332 */
333 void (*getattr) (fuse_req_t req, fuse_ino_t ino,
334 struct fuse_file_info *fi);
335
336 /**
337 * Set file attributes
338 *
339 * In the 'attr' argument only members indicated by the 'to_set'
340 * bitmask contain valid values. Other members contain undefined
341 * values.
342 *
343 * Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
344 * expected to reset the setuid and setgid bits if the file
345 * size or owner is being changed.
346 *
347 * This method will not be called to update st_atime or st_ctime implicitly
348 * (eg. after a read() request), and only be called to implicitly update st_mtime
349 * if writeback caching is active. It is the filesystem's responsibility to update
350 * these timestamps when needed, and (if desired) to implement mount options like
351 * `noatime` or `relatime`.
352 *
353 * If the setattr was invoked from the ftruncate() system call
354 * under Linux kernel versions 2.6.15 or later, the fi->fh will
355 * contain the value set by the open method or will be undefined
356 * if the open method didn't set any value. Otherwise (not
357 * ftruncate call, or kernel version earlier than 2.6.15) the fi
358 * parameter will be NULL.
359 *
360 * Valid replies:
361 * fuse_reply_attr
362 * fuse_reply_err
363 *
364 * @param req request handle
365 * @param ino the inode number
366 * @param attr the attributes
367 * @param to_set bit mask of attributes which should be set
368 * @param fi file information, or NULL
369 */
370 void (*setattr) (fuse_req_t req, fuse_ino_t ino, struct stat *attr,
371 int to_set, struct fuse_file_info *fi);
372
373 /**
374 * Read symbolic link
375 *
376 * Valid replies:
377 * fuse_reply_readlink
378 * fuse_reply_err
379 *
380 * @param req request handle
381 * @param ino the inode number
382 */
383 void (*readlink) (fuse_req_t req, fuse_ino_t ino);
384
385 /**
386 * Return canonical path for inotify
387 *
388 * Valid replies:
389 * fuse_reply_canonical_path
390 * fuse_reply_err
391 *
392 * @param req request handle
393 * @param ino the inode number
394 */
395 void (*canonical_path) (fuse_req_t req, fuse_ino_t ino);
396
397 /**
398 * Create file node
399 *
400 * Create a regular file, character device, block device, fifo or
401 * socket node.
402 *
403 * Valid replies:
404 * fuse_reply_entry
405 * fuse_reply_err
406 *
407 * @param req request handle
408 * @param parent inode number of the parent directory
409 * @param name to create
410 * @param mode file type and mode with which to create the new file
411 * @param rdev the device number (only valid if created file is a device)
412 */
413 void (*mknod) (fuse_req_t req, fuse_ino_t parent, const char *name,
414 mode_t mode, dev_t rdev);
415
416 /**
417 * Create a directory
418 *
419 * Valid replies:
420 * fuse_reply_entry
421 * fuse_reply_err
422 *
423 * @param req request handle
424 * @param parent inode number of the parent directory
425 * @param name to create
426 * @param mode with which to create the new file
427 */
428 void (*mkdir) (fuse_req_t req, fuse_ino_t parent, const char *name,
429 mode_t mode);
430
431 /**
432 * Remove a file
433 *
434 * If the file's inode's lookup count is non-zero, the file
435 * system is expected to postpone any removal of the inode
436 * until the lookup count reaches zero (see description of the
437 * forget function).
438 *
439 * Valid replies:
440 * fuse_reply_err
441 *
442 * @param req request handle
443 * @param parent inode number of the parent directory
444 * @param name to remove
445 */
446 void (*unlink) (fuse_req_t req, fuse_ino_t parent, const char *name);
447
448 /**
449 * Remove a directory
450 *
451 * If the directory's inode's lookup count is non-zero, the
452 * file system is expected to postpone any removal of the
453 * inode until the lookup count reaches zero (see description
454 * of the forget function).
455 *
456 * Valid replies:
457 * fuse_reply_err
458 *
459 * @param req request handle
460 * @param parent inode number of the parent directory
461 * @param name to remove
462 */
463 void (*rmdir) (fuse_req_t req, fuse_ino_t parent, const char *name);
464
465 /**
466 * Create a symbolic link
467 *
468 * Valid replies:
469 * fuse_reply_entry
470 * fuse_reply_err
471 *
472 * @param req request handle
473 * @param link the contents of the symbolic link
474 * @param parent inode number of the parent directory
475 * @param name to create
476 */
477 void (*symlink) (fuse_req_t req, const char *link, fuse_ino_t parent,
478 const char *name);
479
480 /** Rename a file
481 *
482 * If the target exists it should be atomically replaced. If
483 * the target's inode's lookup count is non-zero, the file
484 * system is expected to postpone any removal of the inode
485 * until the lookup count reaches zero (see description of the
486 * forget function).
487 *
488 * If this request is answered with an error code of ENOSYS, this is
489 * treated as a permanent failure with error code EINVAL, i.e. all
490 * future bmap requests will fail with EINVAL without being
491 * send to the filesystem process.
492 *
493 * *flags* may be `RENAME_EXCHANGE` or `RENAME_NOREPLACE`. If
494 * RENAME_NOREPLACE is specified, the filesystem must not
495 * overwrite *newname* if it exists and return an error
496 * instead. If `RENAME_EXCHANGE` is specified, the filesystem
497 * must atomically exchange the two files, i.e. both must
498 * exist and neither may be deleted.
499 *
500 * Valid replies:
501 * fuse_reply_err
502 *
503 * @param req request handle
504 * @param parent inode number of the old parent directory
505 * @param name old name
506 * @param newparent inode number of the new parent directory
507 * @param newname new name
508 */
509 void (*rename) (fuse_req_t req, fuse_ino_t parent, const char *name,
510 fuse_ino_t newparent, const char *newname,
511 unsigned int flags);
512
513 /**
514 * Create a hard link
515 *
516 * Valid replies:
517 * fuse_reply_entry
518 * fuse_reply_err
519 *
520 * @param req request handle
521 * @param ino the old inode number
522 * @param newparent inode number of the new parent directory
523 * @param newname new name to create
524 */
525 void (*link) (fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent,
526 const char *newname);
527
528 /**
529 * Open a file
530 *
531 * Open flags are available in fi->flags. The following rules
532 * apply.
533 *
534 * - Creation (O_CREAT, O_EXCL, O_NOCTTY) flags will be
535 * filtered out / handled by the kernel.
536 *
537 * - Access modes (O_RDONLY, O_WRONLY, O_RDWR) should be used
538 * by the filesystem to check if the operation is
539 * permitted. If the ``-o default_permissions`` mount
540 * option is given, this check is already done by the
541 * kernel before calling open() and may thus be omitted by
542 * the filesystem.
543 *
544 * - When writeback caching is enabled, the kernel may send
545 * read requests even for files opened with O_WRONLY. The
546 * filesystem should be prepared to handle this.
547 *
548 * - When writeback caching is disabled, the filesystem is
549 * expected to properly handle the O_APPEND flag and ensure
550 * that each write is appending to the end of the file.
551 *
552 * - When writeback caching is enabled, the kernel will
553 * handle O_APPEND. However, unless all changes to the file
554 * come through the kernel this will not work reliably. The
555 * filesystem should thus either ignore the O_APPEND flag
556 * (and let the kernel handle it), or return an error
557 * (indicating that reliably O_APPEND is not available).
558 *
559 * Filesystem may store an arbitrary file handle (pointer,
560 * index, etc) in fi->fh, and use this in other all other file
561 * operations (read, write, flush, release, fsync).
562 *
563 * Filesystem may also implement stateless file I/O and not store
564 * anything in fi->fh.
565 *
566 * There are also some flags (direct_io, keep_cache) which the
567 * filesystem may set in fi, to change the way the file is opened.
568 * See fuse_file_info structure in <fuse_common.h> for more details.
569 *
570 * If this request is answered with an error code of ENOSYS
571 * and FUSE_CAP_NO_OPEN_SUPPORT is set in
572 * `fuse_conn_info.capable`, this is treated as success and
573 * future calls to open and release will also succeed without being
574 * sent to the filesystem process.
575 *
576 * To get this behavior without providing an opendir handler, you may
577 * set FUSE_CAP_NO_OPEN_SUPPORT in `fuse_conn_info.want` on supported
578 * kernels to automatically get the zero message open().
579 *
580 * If this callback is not provided and FUSE_CAP_NO_OPEN_SUPPORT is not
581 * set in `fuse_conn_info.want` then an empty reply will be sent.
582 *
583 * Valid replies:
584 * fuse_reply_open
585 * fuse_reply_err
586 *
587 * @param req request handle
588 * @param ino the inode number
589 * @param fi file information
590 */
591 void (*open) (fuse_req_t req, fuse_ino_t ino,
592 struct fuse_file_info *fi);
593
594 /**
595 * Read data
596 *
597 * Read should send exactly the number of bytes requested except
598 * on EOF or error, otherwise the rest of the data will be
599 * substituted with zeroes. An exception to this is when the file
600 * has been opened in 'direct_io' mode, in which case the return
601 * value of the read system call will reflect the return value of
602 * this operation.
603 *
604 * fi->fh will contain the value set by the open method, or will
605 * be undefined if the open method didn't set any value.
606 *
607 * Valid replies:
608 * fuse_reply_buf
609 * fuse_reply_iov
610 * fuse_reply_data
611 * fuse_reply_err
612 *
613 * @param req request handle
614 * @param ino the inode number
615 * @param size number of bytes to read
616 * @param off offset to read from
617 * @param fi file information
618 */
619 void (*read) (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
620 struct fuse_file_info *fi);
621
622 /**
623 * Write data
624 *
625 * Write should return exactly the number of bytes requested
626 * except on error. An exception to this is when the file has
627 * been opened in 'direct_io' mode, in which case the return value
628 * of the write system call will reflect the return value of this
629 * operation.
630 *
631 * Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
632 * expected to reset the setuid and setgid bits.
633 *
634 * fi->fh will contain the value set by the open method, or will
635 * be undefined if the open method didn't set any value.
636 *
637 * Valid replies:
638 * fuse_reply_write
639 * fuse_reply_err
640 *
641 * @param req request handle
642 * @param ino the inode number
643 * @param buf data to write
644 * @param size number of bytes to write
645 * @param off offset to write to
646 * @param fi file information
647 */
648 void (*write) (fuse_req_t req, fuse_ino_t ino, const char *buf,
649 size_t size, off_t off, struct fuse_file_info *fi);
650
651 /**
652 * Flush method
653 *
654 * This is called on each close() of the opened file.
655 *
656 * Since file descriptors can be duplicated (dup, dup2, fork), for
657 * one open call there may be many flush calls.
658 *
659 * Filesystems shouldn't assume that flush will always be called
660 * after some writes, or that if will be called at all.
661 *
662 * fi->fh will contain the value set by the open method, or will
663 * be undefined if the open method didn't set any value.
664 *
665 * NOTE: the name of the method is misleading, since (unlike
666 * fsync) the filesystem is not forced to flush pending writes.
667 * One reason to flush data is if the filesystem wants to return
668 * write errors during close. However, such use is non-portable
669 * because POSIX does not require [close] to wait for delayed I/O to
670 * complete.
671 *
672 * If the filesystem supports file locking operations (setlk,
673 * getlk) it should remove all locks belonging to 'fi->owner'.
674 *
675 * If this request is answered with an error code of ENOSYS,
676 * this is treated as success and future calls to flush() will
677 * succeed automatically without being send to the filesystem
678 * process.
679 *
680 * Valid replies:
681 * fuse_reply_err
682 *
683 * @param req request handle
684 * @param ino the inode number
685 * @param fi file information
686 *
687 * [close]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
688 */
689 void (*flush) (fuse_req_t req, fuse_ino_t ino,
690 struct fuse_file_info *fi);
691
692 /**
693 * Release an open file
694 *
695 * Release is called when there are no more references to an open
696 * file: all file descriptors are closed and all memory mappings
697 * are unmapped.
698 *
699 * For every open call there will be exactly one release call (unless
700 * the filesystem is force-unmounted).
701 *
702 * The filesystem may reply with an error, but error values are
703 * not returned to close() or munmap() which triggered the
704 * release.
705 *
706 * fi->fh will contain the value set by the open method, or will
707 * be undefined if the open method didn't set any value.
708 * fi->flags will contain the same flags as for open.
709 *
710 * Valid replies:
711 * fuse_reply_err
712 *
713 * @param req request handle
714 * @param ino the inode number
715 * @param fi file information
716 */
717 void (*release) (fuse_req_t req, fuse_ino_t ino,
718 struct fuse_file_info *fi);
719
720 /**
721 * Synchronize file contents
722 *
723 * If the datasync parameter is non-zero, then only the user data
724 * should be flushed, not the meta data.
725 *
726 * If this request is answered with an error code of ENOSYS,
727 * this is treated as success and future calls to fsync() will
728 * succeed automatically without being send to the filesystem
729 * process.
730 *
731 * Valid replies:
732 * fuse_reply_err
733 *
734 * @param req request handle
735 * @param ino the inode number
736 * @param datasync flag indicating if only data should be flushed
737 * @param fi file information
738 */
739 void (*fsync) (fuse_req_t req, fuse_ino_t ino, int datasync,
740 struct fuse_file_info *fi);
741
742 /**
743 * Open a directory
744 *
745 * Filesystem may store an arbitrary file handle (pointer, index,
746 * etc) in fi->fh, and use this in other all other directory
747 * stream operations (readdir, releasedir, fsyncdir).
748 *
749 * If this request is answered with an error code of ENOSYS and
750 * FUSE_CAP_NO_OPENDIR_SUPPORT is set in `fuse_conn_info.capable`,
751 * this is treated as success and future calls to opendir and
752 * releasedir will also succeed without being sent to the filesystem
753 * process. In addition, the kernel will cache readdir results
754 * as if opendir returned FOPEN_KEEP_CACHE | FOPEN_CACHE_DIR.
755 *
756 * To get this behavior without providing an opendir handler, you may
757 * set FUSE_CAP_NO_OPENDIR_SUPPORT in `fuse_conn_info.want` on supported
758 * kernels to automatically get the zero message opendir().
759 *
760 * If this callback is not provided and FUSE_CAP_NO_OPENDIR_SUPPORT is
761 * not set in `fuse_conn_info.want` then an empty reply will be sent.
762 *
763 * Valid replies:
764 * fuse_reply_open
765 * fuse_reply_err
766 *
767 * @param req request handle
768 * @param ino the inode number
769 * @param fi file information
770 */
771 void (*opendir) (fuse_req_t req, fuse_ino_t ino,
772 struct fuse_file_info *fi);
773
774 /**
775 * Read directory
776 *
777 * Send a buffer filled using fuse_add_direntry(), with size not
778 * exceeding the requested size. Send an empty buffer on end of
779 * stream.
780 *
781 * fi->fh will contain the value set by the opendir method, or
782 * will be undefined if the opendir method didn't set any value.
783 *
784 * Returning a directory entry from readdir() does not affect
785 * its lookup count.
786 *
787 * If off_t is non-zero, then it will correspond to one of the off_t
788 * values that was previously returned by readdir() for the same
789 * directory handle. In this case, readdir() should skip over entries
790 * coming before the position defined by the off_t value. If entries
791 * are added or removed while the directory handle is open, the filesystem
792 * may still include the entries that have been removed, and may not
793 * report the entries that have been created. However, addition or
794 * removal of entries must never cause readdir() to skip over unrelated
795 * entries or to report them more than once. This means
796 * that off_t can not be a simple index that enumerates the entries
797 * that have been returned but must contain sufficient information to
798 * uniquely determine the next directory entry to return even when the
799 * set of entries is changing.
800 *
801 * The function does not have to report the '.' and '..'
802 * entries, but is allowed to do so. Note that, if readdir does
803 * not return '.' or '..', they will not be implicitly returned,
804 * and this behavior is observable by the caller.
805 *
806 * Valid replies:
807 * fuse_reply_buf
808 * fuse_reply_data
809 * fuse_reply_err
810 *
811 * @param req request handle
812 * @param ino the inode number
813 * @param size maximum number of bytes to send
814 * @param off offset to continue reading the directory stream
815 * @param fi file information
816 */
817 void (*readdir) (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
818 struct fuse_file_info *fi);
819
820 /**
821 * Read directory postfilter
822 *
823 * Valid replies:
824 * fuse_reply_buf
825 * fuse_reply_data
826 * fuse_reply_err
827 *
828 * @param req request handle
829 * @param ino the inode number
830 * @param error_in the error from the readdir
831 * @param off_in offset to continue reading the directory stream before backing
832 * @param off_out offset to continue reading the directory stream after backing
833 * @param size_out length in bytes of dirents
834 * @param dirents array of dirents read by backing
835 * @param fi file information
836 */
837 void (*readdirpostfilter)(fuse_req_t req, fuse_ino_t ino, uint32_t error_in,
838 off_t off_in, off_t off_out, size_t size_out,
839 const void *dirents, struct fuse_file_info *fi);
840
841 /**
842 * Release an open directory
843 *
844 * For every opendir call there will be exactly one releasedir
845 * call (unless the filesystem is force-unmounted).
846 *
847 * fi->fh will contain the value set by the opendir method, or
848 * will be undefined if the opendir method didn't set any value.
849 *
850 * Valid replies:
851 * fuse_reply_err
852 *
853 * @param req request handle
854 * @param ino the inode number
855 * @param fi file information
856 */
857 void (*releasedir) (fuse_req_t req, fuse_ino_t ino,
858 struct fuse_file_info *fi);
859
860 /**
861 * Synchronize directory contents
862 *
863 * If the datasync parameter is non-zero, then only the directory
864 * contents should be flushed, not the meta data.
865 *
866 * fi->fh will contain the value set by the opendir method, or
867 * will be undefined if the opendir method didn't set any value.
868 *
869 * If this request is answered with an error code of ENOSYS,
870 * this is treated as success and future calls to fsyncdir() will
871 * succeed automatically without being send to the filesystem
872 * process.
873 *
874 * Valid replies:
875 * fuse_reply_err
876 *
877 * @param req request handle
878 * @param ino the inode number
879 * @param datasync flag indicating if only data should be flushed
880 * @param fi file information
881 */
882 void (*fsyncdir) (fuse_req_t req, fuse_ino_t ino, int datasync,
883 struct fuse_file_info *fi);
884
885 /**
886 * Get file system statistics
887 *
888 * Valid replies:
889 * fuse_reply_statfs
890 * fuse_reply_err
891 *
892 * @param req request handle
893 * @param ino the inode number, zero means "undefined"
894 */
895 void (*statfs) (fuse_req_t req, fuse_ino_t ino);
896
897 /**
898 * Set an extended attribute
899 *
900 * If this request is answered with an error code of ENOSYS, this is
901 * treated as a permanent failure with error code EOPNOTSUPP, i.e. all
902 * future setxattr() requests will fail with EOPNOTSUPP without being
903 * send to the filesystem process.
904 *
905 * Valid replies:
906 * fuse_reply_err
907 */
908 void (*setxattr) (fuse_req_t req, fuse_ino_t ino, const char *name,
909 const char *value, size_t size, int flags);
910
911 /**
912 * Get an extended attribute
913 *
914 * If size is zero, the size of the value should be sent with
915 * fuse_reply_xattr.
916 *
917 * If the size is non-zero, and the value fits in the buffer, the
918 * value should be sent with fuse_reply_buf.
919 *
920 * If the size is too small for the value, the ERANGE error should
921 * be sent.
922 *
923 * If this request is answered with an error code of ENOSYS, this is
924 * treated as a permanent failure with error code EOPNOTSUPP, i.e. all
925 * future getxattr() requests will fail with EOPNOTSUPP without being
926 * send to the filesystem process.
927 *
928 * Valid replies:
929 * fuse_reply_buf
930 * fuse_reply_data
931 * fuse_reply_xattr
932 * fuse_reply_err
933 *
934 * @param req request handle
935 * @param ino the inode number
936 * @param name of the extended attribute
937 * @param size maximum size of the value to send
938 */
939 void (*getxattr) (fuse_req_t req, fuse_ino_t ino, const char *name,
940 size_t size);
941
942 /**
943 * List extended attribute names
944 *
945 * If size is zero, the total size of the attribute list should be
946 * sent with fuse_reply_xattr.
947 *
948 * If the size is non-zero, and the null character separated
949 * attribute list fits in the buffer, the list should be sent with
950 * fuse_reply_buf.
951 *
952 * If the size is too small for the list, the ERANGE error should
953 * be sent.
954 *
955 * If this request is answered with an error code of ENOSYS, this is
956 * treated as a permanent failure with error code EOPNOTSUPP, i.e. all
957 * future listxattr() requests will fail with EOPNOTSUPP without being
958 * send to the filesystem process.
959 *
960 * Valid replies:
961 * fuse_reply_buf
962 * fuse_reply_data
963 * fuse_reply_xattr
964 * fuse_reply_err
965 *
966 * @param req request handle
967 * @param ino the inode number
968 * @param size maximum size of the list to send
969 */
970 void (*listxattr) (fuse_req_t req, fuse_ino_t ino, size_t size);
971
972 /**
973 * Remove an extended attribute
974 *
975 * If this request is answered with an error code of ENOSYS, this is
976 * treated as a permanent failure with error code EOPNOTSUPP, i.e. all
977 * future removexattr() requests will fail with EOPNOTSUPP without being
978 * send to the filesystem process.
979 *
980 * Valid replies:
981 * fuse_reply_err
982 *
983 * @param req request handle
984 * @param ino the inode number
985 * @param name of the extended attribute
986 */
987 void (*removexattr) (fuse_req_t req, fuse_ino_t ino, const char *name);
988
989 /**
990 * Check file access permissions
991 *
992 * This will be called for the access() and chdir() system
993 * calls. If the 'default_permissions' mount option is given,
994 * this method is not called.
995 *
996 * This method is not called under Linux kernel versions 2.4.x
997 *
998 * If this request is answered with an error code of ENOSYS, this is
999 * treated as a permanent success, i.e. this and all future access()
1000 * requests will succeed without being send to the filesystem process.
1001 *
1002 * Valid replies:
1003 * fuse_reply_err
1004 *
1005 * @param req request handle
1006 * @param ino the inode number
1007 * @param mask requested access mode
1008 */
1009 void (*access) (fuse_req_t req, fuse_ino_t ino, int mask);
1010
1011 /**
1012 * Create and open a file
1013 *
1014 * If the file does not exist, first create it with the specified
1015 * mode, and then open it.
1016 *
1017 * See the description of the open handler for more
1018 * information.
1019 *
1020 * If this method is not implemented or under Linux kernel
1021 * versions earlier than 2.6.15, the mknod() and open() methods
1022 * will be called instead.
1023 *
1024 * If this request is answered with an error code of ENOSYS, the handler
1025 * is treated as not implemented (i.e., for this and future requests the
1026 * mknod() and open() handlers will be called instead).
1027 *
1028 * Valid replies:
1029 * fuse_reply_create
1030 * fuse_reply_err
1031 *
1032 * @param req request handle
1033 * @param parent inode number of the parent directory
1034 * @param name to create
1035 * @param mode file type and mode with which to create the new file
1036 * @param fi file information
1037 */
1038 void (*create) (fuse_req_t req, fuse_ino_t parent, const char *name,
1039 mode_t mode, struct fuse_file_info *fi);
1040
1041 /**
1042 * Test for a POSIX file lock
1043 *
1044 * Valid replies:
1045 * fuse_reply_lock
1046 * fuse_reply_err
1047 *
1048 * @param req request handle
1049 * @param ino the inode number
1050 * @param fi file information
1051 * @param lock the region/type to test
1052 */
1053 void (*getlk) (fuse_req_t req, fuse_ino_t ino,
1054 struct fuse_file_info *fi, struct flock *lock);
1055
1056 /**
1057 * Acquire, modify or release a POSIX file lock
1058 *
1059 * For POSIX threads (NPTL) there's a 1-1 relation between pid and
1060 * owner, but otherwise this is not always the case. For checking
1061 * lock ownership, 'fi->owner' must be used. The l_pid field in
1062 * 'struct flock' should only be used to fill in this field in
1063 * getlk().
1064 *
1065 * Note: if the locking methods are not implemented, the kernel
1066 * will still allow file locking to work locally. Hence these are
1067 * only interesting for network filesystems and similar.
1068 *
1069 * Valid replies:
1070 * fuse_reply_err
1071 *
1072 * @param req request handle
1073 * @param ino the inode number
1074 * @param fi file information
1075 * @param lock the region/type to set
1076 * @param sleep locking operation may sleep
1077 */
1078 void (*setlk) (fuse_req_t req, fuse_ino_t ino,
1079 struct fuse_file_info *fi,
1080 struct flock *lock, int sleep);
1081
1082 /**
1083 * Map block index within file to block index within device
1084 *
1085 * Note: This makes sense only for block device backed filesystems
1086 * mounted with the 'blkdev' option
1087 *
1088 * If this request is answered with an error code of ENOSYS, this is
1089 * treated as a permanent failure, i.e. all future bmap() requests will
1090 * fail with the same error code without being send to the filesystem
1091 * process.
1092 *
1093 * Valid replies:
1094 * fuse_reply_bmap
1095 * fuse_reply_err
1096 *
1097 * @param req request handle
1098 * @param ino the inode number
1099 * @param blocksize unit of block index
1100 * @param idx block index within file
1101 */
1102 void (*bmap) (fuse_req_t req, fuse_ino_t ino, size_t blocksize,
1103 uint64_t idx);
1104
1105 #if FUSE_USE_VERSION < 35
1106 void (*ioctl) (fuse_req_t req, fuse_ino_t ino, int cmd,
1107 void *arg, struct fuse_file_info *fi, unsigned flags,
1108 const void *in_buf, size_t in_bufsz, size_t out_bufsz);
1109 #else
1110 /**
1111 * Ioctl
1112 *
1113 * Note: For unrestricted ioctls (not allowed for FUSE
1114 * servers), data in and out areas can be discovered by giving
1115 * iovs and setting FUSE_IOCTL_RETRY in *flags*. For
1116 * restricted ioctls, kernel prepares in/out data area
1117 * according to the information encoded in cmd.
1118 *
1119 * Valid replies:
1120 * fuse_reply_ioctl_retry
1121 * fuse_reply_ioctl
1122 * fuse_reply_ioctl_iov
1123 * fuse_reply_err
1124 *
1125 * @param req request handle
1126 * @param ino the inode number
1127 * @param cmd ioctl command
1128 * @param arg ioctl argument
1129 * @param fi file information
1130 * @param flags for FUSE_IOCTL_* flags
1131 * @param in_buf data fetched from the caller
1132 * @param in_bufsz number of fetched bytes
1133 * @param out_bufsz maximum size of output data
1134 *
1135 * Note : the unsigned long request submitted by the application
1136 * is truncated to 32 bits.
1137 */
1138 void (*ioctl) (fuse_req_t req, fuse_ino_t ino, unsigned int cmd,
1139 void *arg, struct fuse_file_info *fi, unsigned flags,
1140 const void *in_buf, size_t in_bufsz, size_t out_bufsz);
1141 #endif
1142
1143 /**
1144 * Poll for IO readiness
1145 *
1146 * The client should immediately respond with fuse_reply_poll(),
1147 * setting revents appropriately according to which events are ready.
1148 *
1149 * Additionally, if ph is non-NULL, the client must retain it and
1150 * notify when all future IO readiness events occur by calling
1151 * fuse_lowlevel_notify_poll() with the specified ph.
1152 *
1153 * Regardless of the number of times poll with a non-NULL ph is
1154 * received, a single notify_poll is enough to service all. (Notifying
1155 * more times incurs overhead but doesn't harm correctness.) Any
1156 * additional received handles can be immediately destroyed.
1157 *
1158 * The callee is responsible for destroying ph with
1159 * fuse_pollhandle_destroy() when no longer in use.
1160 *
1161 * If this request is answered with an error code of ENOSYS, this is
1162 * treated as success (with a kernel-defined default poll-mask) and
1163 * future calls to poll() will succeed the same way without being send
1164 * to the filesystem process.
1165 *
1166 * Valid replies:
1167 * fuse_reply_poll
1168 * fuse_reply_err
1169 *
1170 * @param req request handle
1171 * @param ino the inode number
1172 * @param fi file information
1173 * @param ph poll handle to be used for notification
1174 */
1175 void (*poll) (fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1176 struct fuse_pollhandle *ph);
1177
1178 /**
1179 * Write data made available in a buffer
1180 *
1181 * This is a more generic version of the ->write() method. If
1182 * FUSE_CAP_SPLICE_READ is set in fuse_conn_info.want and the
1183 * kernel supports splicing from the fuse device, then the
1184 * data will be made available in pipe for supporting zero
1185 * copy data transfer.
1186 *
1187 * buf->count is guaranteed to be one (and thus buf->idx is
1188 * always zero). The write_buf handler must ensure that
1189 * bufv->off is correctly updated (reflecting the number of
1190 * bytes read from bufv->buf[0]).
1191 *
1192 * Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
1193 * expected to reset the setuid and setgid bits.
1194 *
1195 * Valid replies:
1196 * fuse_reply_write
1197 * fuse_reply_err
1198 *
1199 * @param req request handle
1200 * @param ino the inode number
1201 * @param bufv buffer containing the data
1202 * @param off offset to write to
1203 * @param fi file information
1204 */
1205 void (*write_buf) (fuse_req_t req, fuse_ino_t ino,
1206 struct fuse_bufvec *bufv, off_t off,
1207 struct fuse_file_info *fi);
1208
1209 /**
1210 * Callback function for the retrieve request
1211 *
1212 * Valid replies:
1213 * fuse_reply_none
1214 *
1215 * @param req request handle
1216 * @param cookie user data supplied to fuse_lowlevel_notify_retrieve()
1217 * @param ino the inode number supplied to fuse_lowlevel_notify_retrieve()
1218 * @param offset the offset supplied to fuse_lowlevel_notify_retrieve()
1219 * @param bufv the buffer containing the returned data
1220 */
1221 void (*retrieve_reply) (fuse_req_t req, void *cookie, fuse_ino_t ino,
1222 off_t offset, struct fuse_bufvec *bufv);
1223
1224 /**
1225 * Forget about multiple inodes
1226 *
1227 * See description of the forget function for more
1228 * information.
1229 *
1230 * Valid replies:
1231 * fuse_reply_none
1232 *
1233 * @param req request handle
1234 */
1235 void (*forget_multi) (fuse_req_t req, size_t count,
1236 struct fuse_forget_data *forgets);
1237
1238 /**
1239 * Acquire, modify or release a BSD file lock
1240 *
1241 * Note: if the locking methods are not implemented, the kernel
1242 * will still allow file locking to work locally. Hence these are
1243 * only interesting for network filesystems and similar.
1244 *
1245 * Valid replies:
1246 * fuse_reply_err
1247 *
1248 * @param req request handle
1249 * @param ino the inode number
1250 * @param fi file information
1251 * @param op the locking operation, see flock(2)
1252 */
1253 void (*flock) (fuse_req_t req, fuse_ino_t ino,
1254 struct fuse_file_info *fi, int op);
1255
1256 /**
1257 * Allocate requested space. If this function returns success then
1258 * subsequent writes to the specified range shall not fail due to the lack
1259 * of free space on the file system storage media.
1260 *
1261 * If this request is answered with an error code of ENOSYS, this is
1262 * treated as a permanent failure with error code EOPNOTSUPP, i.e. all
1263 * future fallocate() requests will fail with EOPNOTSUPP without being
1264 * send to the filesystem process.
1265 *
1266 * Valid replies:
1267 * fuse_reply_err
1268 *
1269 * @param req request handle
1270 * @param ino the inode number
1271 * @param offset starting point for allocated region
1272 * @param length size of allocated region
1273 * @param mode determines the operation to be performed on the given range,
1274 * see fallocate(2)
1275 */
1276 void (*fallocate) (fuse_req_t req, fuse_ino_t ino, int mode,
1277 off_t offset, off_t length, struct fuse_file_info *fi);
1278
1279 /**
1280 * Read directory with attributes
1281 *
1282 * Send a buffer filled using fuse_add_direntry_plus(), with size not
1283 * exceeding the requested size. Send an empty buffer on end of
1284 * stream.
1285 *
1286 * fi->fh will contain the value set by the opendir method, or
1287 * will be undefined if the opendir method didn't set any value.
1288 *
1289 * In contrast to readdir() (which does not affect the lookup counts),
1290 * the lookup count of every entry returned by readdirplus(), except "."
1291 * and "..", is incremented by one.
1292 *
1293 * Valid replies:
1294 * fuse_reply_buf
1295 * fuse_reply_data
1296 * fuse_reply_err
1297 *
1298 * @param req request handle
1299 * @param ino the inode number
1300 * @param size maximum number of bytes to send
1301 * @param off offset to continue reading the directory stream
1302 * @param fi file information
1303 */
1304 void (*readdirplus) (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
1305 struct fuse_file_info *fi);
1306
1307 /**
1308 * Copy a range of data from one file to another
1309 *
1310 * Performs an optimized copy between two file descriptors without the
1311 * additional cost of transferring data through the FUSE kernel module
1312 * to user space (glibc) and then back into the FUSE filesystem again.
1313 *
1314 * In case this method is not implemented, glibc falls back to reading
1315 * data from the source and writing to the destination. Effectively
1316 * doing an inefficient copy of the data.
1317 *
1318 * If this request is answered with an error code of ENOSYS, this is
1319 * treated as a permanent failure with error code EOPNOTSUPP, i.e. all
1320 * future copy_file_range() requests will fail with EOPNOTSUPP without
1321 * being send to the filesystem process.
1322 *
1323 * Valid replies:
1324 * fuse_reply_write
1325 * fuse_reply_err
1326 *
1327 * @param req request handle
1328 * @param ino_in the inode number or the source file
1329 * @param off_in starting point from were the data should be read
1330 * @param fi_in file information of the source file
1331 * @param ino_out the inode number or the destination file
1332 * @param off_out starting point where the data should be written
1333 * @param fi_out file information of the destination file
1334 * @param len maximum size of the data to copy
1335 * @param flags passed along with the copy_file_range() syscall
1336 */
1337 void (*copy_file_range) (fuse_req_t req, fuse_ino_t ino_in,
1338 off_t off_in, struct fuse_file_info *fi_in,
1339 fuse_ino_t ino_out, off_t off_out,
1340 struct fuse_file_info *fi_out, size_t len,
1341 int flags);
1342
1343 /**
1344 * Find next data or hole after the specified offset
1345 *
1346 * If this request is answered with an error code of ENOSYS, this is
1347 * treated as a permanent failure, i.e. all future lseek() requests will
1348 * fail with the same error code without being send to the filesystem
1349 * process.
1350 *
1351 * Valid replies:
1352 * fuse_reply_lseek
1353 * fuse_reply_err
1354 *
1355 * @param req request handle
1356 * @param ino the inode number
1357 * @param off offset to start search from
1358 * @param whence either SEEK_DATA or SEEK_HOLE
1359 * @param fi file information
1360 */
1361 void (*lseek) (fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
1362 struct fuse_file_info *fi);
1363 };
1364
1365 /**
1366 * Reply with an error code or success.
1367 *
1368 * Possible requests:
1369 * all except forget, forget_multi, retrieve_reply
1370 *
1371 * Wherever possible, error codes should be chosen from the list of
1372 * documented error conditions in the corresponding system calls
1373 * manpage.
1374 *
1375 * An error code of ENOSYS is sometimes treated specially. This is
1376 * indicated in the documentation of the affected handler functions.
1377 *
1378 * The following requests may be answered with a zero error code:
1379 * unlink, rmdir, rename, flush, release, fsync, fsyncdir, setxattr,
1380 * removexattr, setlk.
1381 *
1382 * @param req request handle
1383 * @param err the positive error value, or zero for success
1384 * @return zero for success, -errno for failure to send reply
1385 */
1386 int fuse_reply_err(fuse_req_t req, int err);
1387
1388 /**
1389 * Don't send reply
1390 *
1391 * Possible requests:
1392 * forget
1393 * forget_multi
1394 * retrieve_reply
1395 *
1396 * @param req request handle
1397 */
1398 void fuse_reply_none(fuse_req_t req);
1399
1400 /**
1401 * Reply with a directory entry
1402 *
1403 * Possible requests:
1404 * lookup, mknod, mkdir, symlink, link
1405 *
1406 * Side effects:
1407 * increments the lookup count on success
1408 *
1409 * @param req request handle
1410 * @param e the entry parameters
1411 * @return zero for success, -errno for failure to send reply
1412 */
1413 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e);
1414
1415 /**
1416 * Reply with a directory entry and open parameters
1417 *
1418 * currently the following members of 'fi' are used:
1419 * fh, direct_io, keep_cache
1420 *
1421 * Possible requests:
1422 * create
1423 *
1424 * Side effects:
1425 * increments the lookup count on success
1426 *
1427 * @param req request handle
1428 * @param e the entry parameters
1429 * @param fi file information
1430 * @return zero for success, -errno for failure to send reply
1431 */
1432 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
1433 const struct fuse_file_info *fi);
1434
1435 /**
1436 * Reply with attributes
1437 *
1438 * Possible requests:
1439 * getattr, setattr
1440 *
1441 * @param req request handle
1442 * @param attr the attributes
1443 * @param attr_timeout validity timeout (in seconds) for the attributes
1444 * @return zero for success, -errno for failure to send reply
1445 */
1446 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
1447 double attr_timeout);
1448
1449 /**
1450 * Reply with the contents of a symbolic link
1451 *
1452 * Possible requests:
1453 * readlink
1454 *
1455 * @param req request handle
1456 * @param link symbolic link contents
1457 * @return zero for success, -errno for failure to send reply
1458 */
1459 int fuse_reply_readlink(fuse_req_t req, const char *link);
1460
1461 int fuse_passthrough_enable(fuse_req_t req, unsigned int fd);
1462
1463 /**
1464 * Reply with the canonical path for inotify
1465 *
1466 * Possible requests:
1467 * canonical_path
1468 *
1469 * @param req request handle
1470 * @param path to canonicalize
1471 * @return zero for success, -errno for failure to send reply
1472 */
1473 int fuse_reply_canonical_path(fuse_req_t req, const char *path);
1474
1475 /**
1476 * Setup passthrough backing file for open reply
1477 *
1478 * Possible requests:
1479 * open, opendir, create
1480 *
1481 * @param req request handle
1482 * @param fd backing file descriptor
1483 * @return positive backing id for success, 0 for failure
1484 */
1485 int fuse_passthrough_open(fuse_req_t req, int fd);
1486 int fuse_passthrough_close(fuse_req_t req, int backing_id);
1487
1488 /**
1489 * Reply with open parameters
1490 *
1491 * currently the following members of 'fi' are used:
1492 * fh, direct_io, keep_cache
1493 *
1494 * Possible requests:
1495 * open, opendir
1496 *
1497 * @param req request handle
1498 * @param fi file information
1499 * @return zero for success, -errno for failure to send reply
1500 */
1501 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi);
1502
1503 /**
1504 * Reply with number of bytes written
1505 *
1506 * Possible requests:
1507 * write
1508 *
1509 * @param req request handle
1510 * @param count the number of bytes written
1511 * @return zero for success, -errno for failure to send reply
1512 */
1513 int fuse_reply_write(fuse_req_t req, size_t count);
1514
1515 /**
1516 * Reply with data
1517 *
1518 * Possible requests:
1519 * read, readdir, getxattr, listxattr
1520 *
1521 * @param req request handle
1522 * @param buf buffer containing data
1523 * @param size the size of data in bytes
1524 * @return zero for success, -errno for failure to send reply
1525 */
1526 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size);
1527
1528 /**
1529 * Reply with data copied/moved from buffer(s)
1530 *
1531 * Zero copy data transfer ("splicing") will be used under
1532 * the following circumstances:
1533 *
1534 * 1. FUSE_CAP_SPLICE_WRITE is set in fuse_conn_info.want, and
1535 * 2. the kernel supports splicing from the fuse device
1536 * (FUSE_CAP_SPLICE_WRITE is set in fuse_conn_info.capable), and
1537 * 3. *flags* does not contain FUSE_BUF_NO_SPLICE
1538 * 4. The amount of data that is provided in file-descriptor backed
1539 * buffers (i.e., buffers for which bufv[n].flags == FUSE_BUF_FD)
1540 * is at least twice the page size.
1541 *
1542 * In order for SPLICE_F_MOVE to be used, the following additional
1543 * conditions have to be fulfilled:
1544 *
1545 * 1. FUSE_CAP_SPLICE_MOVE is set in fuse_conn_info.want, and
1546 * 2. the kernel supports it (i.e, FUSE_CAP_SPLICE_MOVE is set in
1547 fuse_conn_info.capable), and
1548 * 3. *flags* contains FUSE_BUF_SPLICE_MOVE
1549 *
1550 * Note that, if splice is used, the data is actually spliced twice:
1551 * once into a temporary pipe (to prepend header data), and then again
1552 * into the kernel. If some of the provided buffers are memory-backed,
1553 * the data in them is copied in step one and spliced in step two.
1554 *
1555 * The FUSE_BUF_SPLICE_FORCE_SPLICE and FUSE_BUF_SPLICE_NONBLOCK flags
1556 * are silently ignored.
1557 *
1558 * Possible requests:
1559 * read, readdir, getxattr, listxattr
1560 *
1561 * Side effects:
1562 * when used to return data from a readdirplus() (but not readdir())
1563 * call, increments the lookup count of each returned entry by one
1564 * on success.
1565 *
1566 * @param req request handle
1567 * @param bufv buffer vector
1568 * @param flags flags controlling the copy
1569 * @return zero for success, -errno for failure to send reply
1570 */
1571 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
1572 enum fuse_buf_copy_flags flags);
1573
1574 /**
1575 * Reply with data vector
1576 *
1577 * Possible requests:
1578 * read, readdir, getxattr, listxattr
1579 *
1580 * @param req request handle
1581 * @param iov the vector containing the data
1582 * @param count the size of vector
1583 * @return zero for success, -errno for failure to send reply
1584 */
1585 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count);
1586
1587 /**
1588 * Reply with filesystem statistics
1589 *
1590 * Possible requests:
1591 * statfs
1592 *
1593 * @param req request handle
1594 * @param stbuf filesystem statistics
1595 * @return zero for success, -errno for failure to send reply
1596 */
1597 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf);
1598
1599 /**
1600 * Reply with needed buffer size
1601 *
1602 * Possible requests:
1603 * getxattr, listxattr
1604 *
1605 * @param req request handle
1606 * @param count the buffer size needed in bytes
1607 * @return zero for success, -errno for failure to send reply
1608 */
1609 int fuse_reply_xattr(fuse_req_t req, size_t count);
1610
1611 /**
1612 * Reply with file lock information
1613 *
1614 * Possible requests:
1615 * getlk
1616 *
1617 * @param req request handle
1618 * @param lock the lock information
1619 * @return zero for success, -errno for failure to send reply
1620 */
1621 int fuse_reply_lock(fuse_req_t req, const struct flock *lock);
1622
1623 /**
1624 * Reply with block index
1625 *
1626 * Possible requests:
1627 * bmap
1628 *
1629 * @param req request handle
1630 * @param idx block index within device
1631 * @return zero for success, -errno for failure to send reply
1632 */
1633 int fuse_reply_bmap(fuse_req_t req, uint64_t idx);
1634
1635 /* ----------------------------------------------------------- *
1636 * Filling a buffer in readdir *
1637 * ----------------------------------------------------------- */
1638
1639 /**
1640 * Add a directory entry to the buffer
1641 *
1642 * Buffer needs to be large enough to hold the entry. If it's not,
1643 * then the entry is not filled in but the size of the entry is still
1644 * returned. The caller can check this by comparing the bufsize
1645 * parameter with the returned entry size. If the entry size is
1646 * larger than the buffer size, the operation failed.
1647 *
1648 * From the 'stbuf' argument the st_ino field and bits 12-15 of the
1649 * st_mode field are used. The other fields are ignored.
1650 *
1651 * *off* should be any non-zero value that the filesystem can use to
1652 * identify the current point in the directory stream. It does not
1653 * need to be the actual physical position. A value of zero is
1654 * reserved to mean "from the beginning", and should therefore never
1655 * be used (the first call to fuse_add_direntry should be passed the
1656 * offset of the second directory entry).
1657 *
1658 * @param req request handle
1659 * @param buf the point where the new entry will be added to the buffer
1660 * @param bufsize remaining size of the buffer
1661 * @param name the name of the entry
1662 * @param stbuf the file attributes
1663 * @param off the offset of the next entry
1664 * @return the space needed for the entry
1665 */
1666 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
1667 const char *name, const struct stat *stbuf,
1668 off_t off);
1669
1670 /**
1671 * Add a directory entry to the buffer with the attributes
1672 *
1673 * See documentation of `fuse_add_direntry()` for more details.
1674 *
1675 * @param req request handle
1676 * @param buf the point where the new entry will be added to the buffer
1677 * @param bufsize remaining size of the buffer
1678 * @param name the name of the entry
1679 * @param e the directory entry
1680 * @param off the offset of the next entry
1681 * @return the space needed for the entry
1682 */
1683 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
1684 const char *name,
1685 const struct fuse_entry_param *e, off_t off);
1686
1687 /**
1688 * Reply to ask for data fetch and output buffer preparation. ioctl
1689 * will be retried with the specified input data fetched and output
1690 * buffer prepared.
1691 *
1692 * Possible requests:
1693 * ioctl
1694 *
1695 * @param req request handle
1696 * @param in_iov iovec specifying data to fetch from the caller
1697 * @param in_count number of entries in in_iov
1698 * @param out_iov iovec specifying addresses to write output to
1699 * @param out_count number of entries in out_iov
1700 * @return zero for success, -errno for failure to send reply
1701 */
1702 int fuse_reply_ioctl_retry(fuse_req_t req,
1703 const struct iovec *in_iov, size_t in_count,
1704 const struct iovec *out_iov, size_t out_count);
1705
1706 /**
1707 * Reply to finish ioctl
1708 *
1709 * Possible requests:
1710 * ioctl
1711 *
1712 * @param req request handle
1713 * @param result result to be passed to the caller
1714 * @param buf buffer containing output data
1715 * @param size length of output data
1716 */
1717 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size);
1718
1719 /**
1720 * Reply to finish ioctl with iov buffer
1721 *
1722 * Possible requests:
1723 * ioctl
1724 *
1725 * @param req request handle
1726 * @param result result to be passed to the caller
1727 * @param iov the vector containing the data
1728 * @param count the size of vector
1729 */
1730 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1731 int count);
1732
1733 /**
1734 * Reply with poll result event mask
1735 *
1736 * @param req request handle
1737 * @param revents poll result event mask
1738 */
1739 int fuse_reply_poll(fuse_req_t req, unsigned revents);
1740
1741 /**
1742 * Reply with offset
1743 *
1744 * Possible requests:
1745 * lseek
1746 *
1747 * @param req request handle
1748 * @param off offset of next data or hole
1749 * @return zero for success, -errno for failure to send reply
1750 */
1751 int fuse_reply_lseek(fuse_req_t req, off_t off);
1752
1753 /* ----------------------------------------------------------- *
1754 * Notification *
1755 * ----------------------------------------------------------- */
1756
1757 /**
1758 * Notify IO readiness event
1759 *
1760 * For more information, please read comment for poll operation.
1761 *
1762 * @param ph poll handle to notify IO readiness event for
1763 */
1764 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph);
1765
1766 /**
1767 * Notify to invalidate cache for an inode.
1768 *
1769 * Added in FUSE protocol version 7.12. If the kernel does not support
1770 * this (or a newer) version, the function will return -ENOSYS and do
1771 * nothing.
1772 *
1773 * If the filesystem has writeback caching enabled, invalidating an
1774 * inode will first trigger a writeback of all dirty pages. The call
1775 * will block until all writeback requests have completed and the
1776 * inode has been invalidated. It will, however, not wait for
1777 * completion of pending writeback requests that have been issued
1778 * before.
1779 *
1780 * If there are no dirty pages, this function will never block.
1781 *
1782 * @param se the session object
1783 * @param ino the inode number
1784 * @param off the offset in the inode where to start invalidating
1785 * or negative to invalidate attributes only
1786 * @param len the amount of cache to invalidate or 0 for all
1787 * @return zero for success, -errno for failure
1788 */
1789 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
1790 off_t off, off_t len);
1791
1792 /**
1793 * Notify to invalidate parent attributes and the dentry matching parent/name
1794 *
1795 * To avoid a deadlock this function must not be called in the
1796 * execution path of a related filesystem operation or within any code
1797 * that could hold a lock that could be needed to execute such an
1798 * operation. As of kernel 4.18, a "related operation" is a lookup(),
1799 * symlink(), mknod(), mkdir(), unlink(), rename(), link() or create()
1800 * request for the parent, and a setattr(), unlink(), rmdir(),
1801 * rename(), setxattr(), removexattr(), readdir() or readdirplus()
1802 * request for the inode itself.
1803 *
1804 * When called correctly, this function will never block.
1805 *
1806 * Added in FUSE protocol version 7.12. If the kernel does not support
1807 * this (or a newer) version, the function will return -ENOSYS and do
1808 * nothing.
1809 *
1810 * @param se the session object
1811 * @param parent inode number
1812 * @param name file name
1813 * @param namelen strlen() of file name
1814 * @return zero for success, -errno for failure
1815 */
1816 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
1817 const char *name, size_t namelen);
1818
1819 /**
1820 * Notify to expire parent attributes and the dentry matching parent/name
1821 *
1822 * Same restrictions apply as for fuse_lowlevel_notify_inval_entry()
1823 *
1824 * Compared to invalidating an entry, expiring the entry results not in a
1825 * forceful removal of that entry from kernel cache but instead the next access
1826 * to it forces a lookup from the filesystem.
1827 *
1828 * This makes a difference for overmounted dentries, where plain invalidation
1829 * would detach all submounts before dropping the dentry from the cache.
1830 * If only expiry is set on the dentry, then any overmounts are left alone and
1831 * until ->d_revalidate() is called.
1832 *
1833 * Note: ->d_revalidate() is not called for the case of following a submount,
1834 * so invalidation will only be triggered for the non-overmounted case.
1835 * The dentry could also be mounted in a different mount instance, in which case
1836 * any submounts will still be detached.
1837 *
1838 * Added in FUSE protocol version 7.38. If the kernel does not support
1839 * this (or a newer) version, the function will return -ENOSYS and do nothing.
1840 *
1841 * @param se the session object
1842 * @param parent inode number
1843 * @param name file name
1844 * @param namelen strlen() of file name
1845 * @return zero for success, -errno for failure, -enosys if no kernel support
1846 */
1847 int fuse_lowlevel_notify_expire_entry(struct fuse_session *se, fuse_ino_t parent,
1848 const char *name, size_t namelen);
1849
1850 /**
1851 * This function behaves like fuse_lowlevel_notify_inval_entry() with
1852 * the following additional effect (at least as of Linux kernel 4.8):
1853 *
1854 * If the provided *child* inode matches the inode that is currently
1855 * associated with the cached dentry, and if there are any inotify
1856 * watches registered for the dentry, then the watchers are informed
1857 * that the dentry has been deleted.
1858 *
1859 * To avoid a deadlock this function must not be called while
1860 * executing a related filesystem operation or while holding a lock
1861 * that could be needed to execute such an operation (see the
1862 * description of fuse_lowlevel_notify_inval_entry() for more
1863 * details).
1864 *
1865 * When called correctly, this function will never block.
1866 *
1867 * Added in FUSE protocol version 7.18. If the kernel does not support
1868 * this (or a newer) version, the function will return -ENOSYS and do
1869 * nothing.
1870 *
1871 * @param se the session object
1872 * @param parent inode number
1873 * @param child inode number
1874 * @param name file name
1875 * @param namelen strlen() of file name
1876 * @return zero for success, -errno for failure
1877 */
1878 int fuse_lowlevel_notify_delete(struct fuse_session *se,
1879 fuse_ino_t parent, fuse_ino_t child,
1880 const char *name, size_t namelen);
1881
1882 /**
1883 * Store data to the kernel buffers
1884 *
1885 * Synchronously store data in the kernel buffers belonging to the
1886 * given inode. The stored data is marked up-to-date (no read will be
1887 * performed against it, unless it's invalidated or evicted from the
1888 * cache).
1889 *
1890 * If the stored data overflows the current file size, then the size
1891 * is extended, similarly to a write(2) on the filesystem.
1892 *
1893 * If this function returns an error, then the store wasn't fully
1894 * completed, but it may have been partially completed.
1895 *
1896 * Added in FUSE protocol version 7.15. If the kernel does not support
1897 * this (or a newer) version, the function will return -ENOSYS and do
1898 * nothing.
1899 *
1900 * @param se the session object
1901 * @param ino the inode number
1902 * @param offset the starting offset into the file to store to
1903 * @param bufv buffer vector
1904 * @param flags flags controlling the copy
1905 * @return zero for success, -errno for failure
1906 */
1907 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
1908 off_t offset, struct fuse_bufvec *bufv,
1909 enum fuse_buf_copy_flags flags);
1910 /**
1911 * Retrieve data from the kernel buffers
1912 *
1913 * Retrieve data in the kernel buffers belonging to the given inode.
1914 * If successful then the retrieve_reply() method will be called with
1915 * the returned data.
1916 *
1917 * Only present pages are returned in the retrieve reply. Retrieving
1918 * stops when it finds a non-present page and only data prior to that
1919 * is returned.
1920 *
1921 * If this function returns an error, then the retrieve will not be
1922 * completed and no reply will be sent.
1923 *
1924 * This function doesn't change the dirty state of pages in the kernel
1925 * buffer. For dirty pages the write() method will be called
1926 * regardless of having been retrieved previously.
1927 *
1928 * Added in FUSE protocol version 7.15. If the kernel does not support
1929 * this (or a newer) version, the function will return -ENOSYS and do
1930 * nothing.
1931 *
1932 * @param se the session object
1933 * @param ino the inode number
1934 * @param size the number of bytes to retrieve
1935 * @param offset the starting offset into the file to retrieve from
1936 * @param cookie user data to supply to the reply callback
1937 * @return zero for success, -errno for failure
1938 */
1939 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
1940 size_t size, off_t offset, void *cookie);
1941
1942
1943 /* ----------------------------------------------------------- *
1944 * Utility functions *
1945 * ----------------------------------------------------------- */
1946
1947 /**
1948 * Get the userdata from the request
1949 *
1950 * @param req request handle
1951 * @return the user data passed to fuse_session_new()
1952 */
1953 void *fuse_req_userdata(fuse_req_t req);
1954
1955 /**
1956 * Get the context from the request
1957 *
1958 * The pointer returned by this function will only be valid for the
1959 * request's lifetime
1960 *
1961 * @param req request handle
1962 * @return the context structure
1963 */
1964 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req);
1965
1966 /**
1967 * Get the current supplementary group IDs for the specified request
1968 *
1969 * Similar to the getgroups(2) system call, except the return value is
1970 * always the total number of group IDs, even if it is larger than the
1971 * specified size.
1972 *
1973 * The current fuse kernel module in linux (as of 2.6.30) doesn't pass
1974 * the group list to userspace, hence this function needs to parse
1975 * "/proc/$TID/task/$TID/status" to get the group IDs.
1976 *
1977 * This feature may not be supported on all operating systems. In
1978 * such a case this function will return -ENOSYS.
1979 *
1980 * @param req request handle
1981 * @param size size of given array
1982 * @param list array of group IDs to be filled in
1983 * @return the total number of supplementary group IDs or -errno on failure
1984 */
1985 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]);
1986
1987 /**
1988 * Callback function for an interrupt
1989 *
1990 * @param req interrupted request
1991 * @param data user data
1992 */
1993 typedef void (*fuse_interrupt_func_t)(fuse_req_t req, void *data);
1994
1995 /**
1996 * Register/unregister callback for an interrupt
1997 *
1998 * If an interrupt has already happened, then the callback function is
1999 * called from within this function, hence it's not possible for
2000 * interrupts to be lost.
2001 *
2002 * @param req request handle
2003 * @param func the callback function or NULL for unregister
2004 * @param data user data passed to the callback function
2005 */
2006 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2007 void *data);
2008
2009 /**
2010 * Check if a request has already been interrupted
2011 *
2012 * @param req request handle
2013 * @return 1 if the request has been interrupted, 0 otherwise
2014 */
2015 int fuse_req_interrupted(fuse_req_t req);
2016
2017
2018 /* ----------------------------------------------------------- *
2019 * Inquiry functions *
2020 * ----------------------------------------------------------- */
2021
2022 /**
2023 * Print low-level version information to stdout.
2024 */
2025 void fuse_lowlevel_version(void);
2026
2027 /**
2028 * Print available low-level options to stdout. This is not an
2029 * exhaustive list, but includes only those options that may be of
2030 * interest to an end-user of a file system.
2031 */
2032 void fuse_lowlevel_help(void);
2033
2034 /**
2035 * Print available options for `fuse_parse_cmdline()`.
2036 */
2037 void fuse_cmdline_help(void);
2038
2039 /* ----------------------------------------------------------- *
2040 * Filesystem setup & teardown *
2041 * ----------------------------------------------------------- */
2042
2043 /**
2044 * Note: Any addition to this struct needs to create a compatibility symbol
2045 * for fuse_parse_cmdline(). For ABI compatibility reasons it is also
2046 * not possible to remove struct members.
2047 */
2048 struct fuse_cmdline_opts {
2049 int singlethread;
2050 int foreground;
2051 int debug;
2052 int nodefault_subtype;
2053 char *mountpoint;
2054 int show_version;
2055 int show_help;
2056 int clone_fd;
2057 unsigned int max_idle_threads; /* discouraged, due to thread
2058 * destruct overhead */
2059
2060 /* Added in libfuse-3.12 */
2061 unsigned int max_threads;
2062 };
2063
2064 /**
2065 * Utility function to parse common options for simple file systems
2066 * using the low-level API. A help text that describes the available
2067 * options can be printed with `fuse_cmdline_help`. A single
2068 * non-option argument is treated as the mountpoint. Multiple
2069 * non-option arguments will result in an error.
2070 *
2071 * If neither -o subtype= or -o fsname= options are given, a new
2072 * subtype option will be added and set to the basename of the program
2073 * (the fsname will remain unset, and then defaults to "fuse").
2074 *
2075 * Known options will be removed from *args*, unknown options will
2076 * remain.
2077 *
2078 * @param args argument vector (input+output)
2079 * @param opts output argument for parsed options
2080 * @return 0 on success, -1 on failure
2081 */
2082 #if (defined(LIBFUSE_BUILT_WITH_VERSIONED_SYMBOLS))
2083 int fuse_parse_cmdline(struct fuse_args *args,
2084 struct fuse_cmdline_opts *opts);
2085 #else
2086 #if FUSE_USE_VERSION < FUSE_MAKE_VERSION(3, 12)
2087 int fuse_parse_cmdline_30(struct fuse_args *args,
2088 struct fuse_cmdline_opts *opts);
2089 #define fuse_parse_cmdline(args, opts) fuse_parse_cmdline_30(args, opts)
2090 #else
2091 int fuse_parse_cmdline_312(struct fuse_args *args,
2092 struct fuse_cmdline_opts *opts);
2093 #define fuse_parse_cmdline(args, opts) fuse_parse_cmdline_312(args, opts)
2094 #endif
2095 #endif
2096
2097 /*
2098 * This should mostly not be called directly, but instead the fuse_session_new()
2099 * macro should be used, which fills in the libfuse version compilation
2100 * is done against automatically.
2101 */
2102 struct fuse_session *_fuse_session_new_317(struct fuse_args *args,
2103 const struct fuse_lowlevel_ops *op,
2104 size_t op_size,
2105 struct libfuse_version *version,
2106 void *userdata);
2107
2108 /* Do not call this directly, but only through fuse_session_new() */
2109 #if (defined(LIBFUSE_BUILT_WITH_VERSIONED_SYMBOLS))
2110 struct fuse_session *
2111 _fuse_session_new(struct fuse_args *args,
2112 const struct fuse_lowlevel_ops *op,
2113 size_t op_size,
2114 struct libfuse_version *version,
2115 void *userdata);
2116 #else
2117 struct fuse_session *
2118 _fuse_session_new_317(struct fuse_args *args,
2119 const struct fuse_lowlevel_ops *op,
2120 size_t op_size,
2121 struct libfuse_version *version,
2122 void *userdata);
2123 #define _fuse_session_new(args, op, op_size, version, userdata) \
2124 _fuse_session_new_317(args, op, op_size, version, userdata)
2125 #endif
2126
2127 /**
2128 * Create a low level session.
2129 *
2130 * Returns a session structure suitable for passing to
2131 * fuse_session_mount() and fuse_session_loop().
2132 *
2133 * This function accepts most file-system independent mount options
2134 * (like context, nodev, ro - see mount(8)), as well as the general
2135 * fuse mount options listed in mount.fuse(8) (e.g. -o allow_root and
2136 * -o default_permissions, but not ``-o use_ino``). Instead of `-o
2137 * debug`, debugging may also enabled with `-d` or `--debug`.
2138 *
2139 * If not all options are known, an error message is written to stderr
2140 * and the function returns NULL.
2141 *
2142 * Option parsing skips argv[0], which is assumed to contain the
2143 * program name. To prevent accidentally passing an option in
2144 * argv[0], this element must always be present (even if no options
2145 * are specified). It may be set to the empty string ('\0') if no
2146 * reasonable value can be provided.
2147 *
2148 * @param args argument vector
2149 * @param op the (low-level) filesystem operations
2150 * @param op_size sizeof(struct fuse_lowlevel_ops)
2151 * @param version the libfuse version a file system server was compiled against
2152 * @param userdata user data
2153 * @return the fuse session on success, NULL on failure
2154 **/
2155 static inline struct fuse_session *
fuse_session_new(struct fuse_args * args,const struct fuse_lowlevel_ops * op,size_t op_size,void * userdata)2156 fuse_session_new(struct fuse_args *args,
2157 const struct fuse_lowlevel_ops *op,
2158 size_t op_size,
2159 void *userdata)
2160 {
2161 struct libfuse_version version = {
2162 .major = FUSE_MAJOR_VERSION,
2163 .minor = FUSE_MINOR_VERSION,
2164 .hotfix = FUSE_HOTFIX_VERSION,
2165 .padding = 0
2166 };
2167
2168 return _fuse_session_new(args, op, op_size, &version, userdata);
2169 }
2170
2171 /**
2172 * Set a file descriptor for the session.
2173 *
2174 * This function can be used if you want to have a custom communication
2175 * interface instead of using a mountpoint. In practice, this means that instead
2176 * of calling fuse_session_mount() and fuse_session_unmount(), one could call
2177 * fuse_session_custom_io() where fuse_session_mount() would have otherwise been
2178 * called.
2179 *
2180 * In `io`, implementations for read and writev MUST be provided. Otherwise -1
2181 * will be returned and `fd` will not be used. Implementations for `splice_send`
2182 * and `splice_receive` are optional. If they are not provided splice will not
2183 * be used for send or receive respectively.
2184 *
2185 * The provided file descriptor `fd` will be closed when fuse_session_destroy()
2186 * is called.
2187 *
2188 * @param se session object
2189 * @param io Custom io to use when retrieving/sending requests/responses
2190 * @param fd file descriptor for the session
2191 *
2192 * @return 0 on success
2193 * @return -EINVAL if `io`, `io->read` or `ìo->writev` are NULL
2194 * @return -EBADF if `fd` was smaller than 0
2195 * @return -errno if failed to allocate memory to store `io`
2196 *
2197 **/
2198 int fuse_session_custom_io(struct fuse_session *se,
2199 const struct fuse_custom_io *io, int fd);
2200
2201 /**
2202 * Mount a FUSE file system.
2203 *
2204 * @param mountpoint the mount point path
2205 * @param se session object
2206 *
2207 * @return 0 on success, -1 on failure.
2208 **/
2209 int fuse_session_mount(struct fuse_session *se, const char *mountpoint);
2210
2211 /**
2212 * Enter a single threaded, blocking event loop.
2213 *
2214 * When the event loop terminates because the connection to the FUSE
2215 * kernel module has been closed, this function returns zero. This
2216 * happens when the filesystem is unmounted regularly (by the
2217 * filesystem owner or root running the umount(8) or fusermount(1)
2218 * command), or if connection is explicitly severed by writing ``1``
2219 * to the``abort`` file in ``/sys/fs/fuse/connections/NNN``. The only
2220 * way to distinguish between these two conditions is to check if the
2221 * filesystem is still mounted after the session loop returns.
2222 *
2223 * When some error occurs during request processing, the function
2224 * returns a negated errno(3) value.
2225 *
2226 * If the loop has been terminated because of a signal handler
2227 * installed by fuse_set_signal_handlers(), this function returns the
2228 * (positive) signal value that triggered the exit.
2229 *
2230 * @param se the session
2231 * @return 0, -errno, or a signal value
2232 */
2233 int fuse_session_loop(struct fuse_session *se);
2234
2235 #if FUSE_USE_VERSION < 32
2236 int fuse_session_loop_mt_31(struct fuse_session *se, int clone_fd);
2237 #define fuse_session_loop_mt(se, clone_fd) fuse_session_loop_mt_31(se, clone_fd)
2238 #elif FUSE_USE_VERSION < FUSE_MAKE_VERSION(3, 12)
2239 int fuse_session_loop_mt_32(struct fuse_session *se, struct fuse_loop_config *config);
2240 #define fuse_session_loop_mt(se, config) fuse_session_loop_mt_32(se, config)
2241 #else
2242 #if (defined(LIBFUSE_BUILT_WITH_VERSIONED_SYMBOLS))
2243 /**
2244 * Enter a multi-threaded event loop.
2245 *
2246 * For a description of the return value and the conditions when the
2247 * event loop exits, refer to the documentation of
2248 * fuse_session_loop().
2249 *
2250 * @param se the session
2251 * @param config session loop configuration
2252 * @return see fuse_session_loop()
2253 */
2254 int fuse_session_loop_mt(struct fuse_session *se, struct fuse_loop_config *config);
2255 #else
2256 int fuse_session_loop_mt_312(struct fuse_session *se, struct fuse_loop_config *config);
2257 #define fuse_session_loop_mt(se, config) fuse_session_loop_mt_312(se, config)
2258 #endif
2259 #endif
2260
2261 /**
2262 * Flag a session as terminated.
2263 *
2264 * This will cause any running event loops to terminate on the next opportunity. If this function is
2265 * called by a thread that is not a FUSE worker thread, the next
2266 * opportunity will be when FUSE a request is received (which may be far in the future if the
2267 * filesystem is not currently being used by any clients). One way to avoid this delay is to
2268 * afterwards sent a signal to the main thread (if fuse_set_signal_handlers() is used, SIGPIPE
2269 * will cause the main thread to wake-up but otherwise be ignored).
2270 *
2271 * @param se the session
2272 */
2273 void fuse_session_exit(struct fuse_session *se);
2274
2275 /**
2276 * Reset the terminated flag of a session
2277 *
2278 * @param se the session
2279 */
2280 void fuse_session_reset(struct fuse_session *se);
2281
2282 /**
2283 * Query the terminated flag of a session
2284 *
2285 * @param se the session
2286 * @return 1 if exited, 0 if not exited
2287 */
2288 int fuse_session_exited(struct fuse_session *se);
2289
2290 /**
2291 * Ensure that file system is unmounted.
2292 *
2293 * In regular operation, the file system is typically unmounted by the
2294 * user calling umount(8) or fusermount(1), which then terminates the
2295 * FUSE session loop. However, the session loop may also terminate as
2296 * a result of an explicit call to fuse_session_exit() (e.g. by a
2297 * signal handler installed by fuse_set_signal_handler()). In this
2298 * case the filesystem remains mounted, but any attempt to access it
2299 * will block (while the filesystem process is still running) or give
2300 * an ESHUTDOWN error (after the filesystem process has terminated).
2301 *
2302 * If the communication channel with the FUSE kernel module is still
2303 * open (i.e., if the session loop was terminated by an explicit call
2304 * to fuse_session_exit()), this function will close it and unmount
2305 * the filesystem. If the communication channel has been closed by the
2306 * kernel, this method will do (almost) nothing.
2307 *
2308 * NOTE: The above semantics mean that if the connection to the kernel
2309 * is terminated via the ``/sys/fs/fuse/connections/NNN/abort`` file,
2310 * this method will *not* unmount the filesystem.
2311 *
2312 * @param se the session
2313 */
2314 void fuse_session_unmount(struct fuse_session *se);
2315
2316 /**
2317 * Destroy a session
2318 *
2319 * @param se the session
2320 */
2321 void fuse_session_destroy(struct fuse_session *se);
2322
2323 /* ----------------------------------------------------------- *
2324 * Custom event loop support *
2325 * ----------------------------------------------------------- */
2326
2327 /**
2328 * Return file descriptor for communication with kernel.
2329 *
2330 * The file selector can be used to integrate FUSE with a custom event
2331 * loop. Whenever data is available for reading on the provided fd,
2332 * the event loop should call `fuse_session_receive_buf` followed by
2333 * `fuse_session_process_buf` to process the request.
2334 *
2335 * The returned file descriptor is valid until `fuse_session_unmount`
2336 * is called.
2337 *
2338 * @param se the session
2339 * @return a file descriptor
2340 */
2341 int fuse_session_fd(struct fuse_session *se);
2342
2343 /**
2344 * Process a raw request supplied in a generic buffer
2345 *
2346 * The fuse_buf may contain a memory buffer or a pipe file descriptor.
2347 *
2348 * @param se the session
2349 * @param buf the fuse_buf containing the request
2350 */
2351 void fuse_session_process_buf(struct fuse_session *se,
2352 const struct fuse_buf *buf);
2353
2354 /**
2355 * Read a raw request from the kernel into the supplied buffer.
2356 *
2357 * Depending on file system options, system capabilities, and request
2358 * size the request is either read into a memory buffer or spliced
2359 * into a temporary pipe.
2360 *
2361 * @param se the session
2362 * @param buf the fuse_buf to store the request in
2363 * @return the actual size of the raw request, or -errno on error
2364 */
2365 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf);
2366
2367 #ifdef __cplusplus
2368 }
2369 #endif
2370
2371 #endif /* FUSE_LOWLEVEL_H_ */
2372