qemu/tools/virtiofsd/fuse_common.h
<<
>>
Prefs
   1/*
   2 * FUSE: Filesystem in Userspace
   3 * Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
   4 *
   5 * This program can be distributed under the terms of the GNU LGPLv2.
   6 * See the file COPYING.LIB.
   7 */
   8
   9/** @file */
  10
  11#if !defined(FUSE_H_) && !defined(FUSE_LOWLEVEL_H_)
  12#error \
  13    "Never include <fuse_common.h> directly; use <fuse.h> or <fuse_lowlevel.h> instead."
  14#endif
  15
  16#ifndef FUSE_COMMON_H_
  17#define FUSE_COMMON_H_
  18
  19#include "fuse_log.h"
  20#include "fuse_opt.h"
  21
  22/** Major version of FUSE library interface */
  23#define FUSE_MAJOR_VERSION 3
  24
  25/** Minor version of FUSE library interface */
  26#define FUSE_MINOR_VERSION 2
  27
  28#define FUSE_MAKE_VERSION(maj, min) ((maj) * 10 + (min))
  29#define FUSE_VERSION FUSE_MAKE_VERSION(FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION)
  30
  31/**
  32 * Information about an open file.
  33 *
  34 * File Handles are created by the open, opendir, and create methods and closed
  35 * by the release and releasedir methods.  Multiple file handles may be
  36 * concurrently open for the same file.  Generally, a client will create one
  37 * file handle per file descriptor, though in some cases multiple file
  38 * descriptors can share a single file handle.
  39 */
  40struct fuse_file_info {
  41    /** Open flags. Available in open() and release() */
  42    int flags;
  43
  44    /*
  45     * In case of a write operation indicates if this was caused
  46     * by a delayed write from the page cache. If so, then the
  47     * context's pid, uid, and gid fields will not be valid, and
  48     * the *fh* value may not match the *fh* value that would
  49     * have been sent with the corresponding individual write
  50     * requests if write caching had been disabled.
  51     */
  52    unsigned int writepage:1;
  53
  54    /** Can be filled in by open, to use direct I/O on this file. */
  55    unsigned int direct_io:1;
  56
  57    /*
  58     *  Can be filled in by open. It signals the kernel that any
  59     *  currently cached file data (ie., data that the filesystem
  60     *  provided the last time the file was open) need not be
  61     *  invalidated. Has no effect when set in other contexts (in
  62     *  particular it does nothing when set by opendir()).
  63     */
  64    unsigned int keep_cache:1;
  65
  66    /*
  67     *  Indicates a flush operation.  Set in flush operation, also
  68     *  maybe set in highlevel lock operation and lowlevel release
  69     *  operation.
  70     */
  71    unsigned int flush:1;
  72
  73    /*
  74     *  Can be filled in by open, to indicate that the file is not
  75     *  seekable.
  76     */
  77    unsigned int nonseekable:1;
  78
  79    /*
  80     * Indicates that flock locks for this file should be
  81     * released.  If set, lock_owner shall contain a valid value.
  82     * May only be set in ->release().
  83     */
  84    unsigned int flock_release:1;
  85
  86    /*
  87     *  Can be filled in by opendir. It signals the kernel to
  88     *  enable caching of entries returned by readdir().  Has no
  89     *  effect when set in other contexts (in particular it does
  90     *  nothing when set by open()).
  91     */
  92    unsigned int cache_readdir:1;
  93
  94    /* Indicates that suid/sgid bits should be removed upon write */
  95    unsigned int kill_priv:1;
  96
  97
  98    /** Padding.  Reserved for future use*/
  99    unsigned int padding:24;
 100    unsigned int padding2:32;
 101
 102    /*
 103     *  File handle id.  May be filled in by filesystem in create,
 104     * open, and opendir().  Available in most other file operations on the
 105     * same file handle.
 106     */
 107    uint64_t fh;
 108
 109    /** Lock owner id.  Available in locking operations and flush */
 110    uint64_t lock_owner;
 111
 112    /*
 113     * Requested poll events.  Available in ->poll.  Only set on kernels
 114     * which support it.  If unsupported, this field is set to zero.
 115     */
 116    uint32_t poll_events;
 117};
 118
 119/*
 120 * Capability bits for 'fuse_conn_info.capable' and 'fuse_conn_info.want'
 121 */
 122
 123/**
 124 * Indicates that the filesystem supports asynchronous read requests.
 125 *
 126 * If this capability is not requested/available, the kernel will
 127 * ensure that there is at most one pending read request per
 128 * file-handle at any time, and will attempt to order read requests by
 129 * increasing offset.
 130 *
 131 * This feature is enabled by default when supported by the kernel.
 132 */
 133#define FUSE_CAP_ASYNC_READ (1 << 0)
 134
 135/**
 136 * Indicates that the filesystem supports "remote" locking.
 137 *
 138 * This feature is enabled by default when supported by the kernel,
 139 * and if getlk() and setlk() handlers are implemented.
 140 */
 141#define FUSE_CAP_POSIX_LOCKS (1 << 1)
 142
 143/**
 144 * Indicates that the filesystem supports the O_TRUNC open flag.  If
 145 * disabled, and an application specifies O_TRUNC, fuse first calls
 146 * truncate() and then open() with O_TRUNC filtered out.
 147 *
 148 * This feature is enabled by default when supported by the kernel.
 149 */
 150#define FUSE_CAP_ATOMIC_O_TRUNC (1 << 3)
 151
 152/**
 153 * Indicates that the filesystem supports lookups of "." and "..".
 154 *
 155 * This feature is disabled by default.
 156 */
 157#define FUSE_CAP_EXPORT_SUPPORT (1 << 4)
 158
 159/**
 160 * Indicates that the kernel should not apply the umask to the
 161 * file mode on create operations.
 162 *
 163 * This feature is disabled by default.
 164 */
 165#define FUSE_CAP_DONT_MASK (1 << 6)
 166
 167/**
 168 * Indicates that libfuse should try to use splice() when writing to
 169 * the fuse device. This may improve performance.
 170 *
 171 * This feature is disabled by default.
 172 */
 173#define FUSE_CAP_SPLICE_WRITE (1 << 7)
 174
 175/**
 176 * Indicates that libfuse should try to move pages instead of copying when
 177 * writing to / reading from the fuse device. This may improve performance.
 178 *
 179 * This feature is disabled by default.
 180 */
 181#define FUSE_CAP_SPLICE_MOVE (1 << 8)
 182
 183/**
 184 * Indicates that libfuse should try to use splice() when reading from
 185 * the fuse device. This may improve performance.
 186 *
 187 * This feature is enabled by default when supported by the kernel and
 188 * if the filesystem implements a write_buf() handler.
 189 */
 190#define FUSE_CAP_SPLICE_READ (1 << 9)
 191
 192/**
 193 * If set, the calls to flock(2) will be emulated using POSIX locks and must
 194 * then be handled by the filesystem's setlock() handler.
 195 *
 196 * If not set, flock(2) calls will be handled by the FUSE kernel module
 197 * internally (so any access that does not go through the kernel cannot be taken
 198 * into account).
 199 *
 200 * This feature is enabled by default when supported by the kernel and
 201 * if the filesystem implements a flock() handler.
 202 */
 203#define FUSE_CAP_FLOCK_LOCKS (1 << 10)
 204
 205/**
 206 * Indicates that the filesystem supports ioctl's on directories.
 207 *
 208 * This feature is enabled by default when supported by the kernel.
 209 */
 210#define FUSE_CAP_IOCTL_DIR (1 << 11)
 211
 212/**
 213 * Traditionally, while a file is open the FUSE kernel module only
 214 * asks the filesystem for an update of the file's attributes when a
 215 * client attempts to read beyond EOF. This is unsuitable for
 216 * e.g. network filesystems, where the file contents may change
 217 * without the kernel knowing about it.
 218 *
 219 * If this flag is set, FUSE will check the validity of the attributes
 220 * on every read. If the attributes are no longer valid (i.e., if the
 221 * *attr_timeout* passed to fuse_reply_attr() or set in `struct
 222 * fuse_entry_param` has passed), it will first issue a `getattr`
 223 * request. If the new mtime differs from the previous value, any
 224 * cached file *contents* will be invalidated as well.
 225 *
 226 * This flag should always be set when available. If all file changes
 227 * go through the kernel, *attr_timeout* should be set to a very large
 228 * number to avoid unnecessary getattr() calls.
 229 *
 230 * This feature is enabled by default when supported by the kernel.
 231 */
 232#define FUSE_CAP_AUTO_INVAL_DATA (1 << 12)
 233
 234/**
 235 * Indicates that the filesystem supports readdirplus.
 236 *
 237 * This feature is enabled by default when supported by the kernel and if the
 238 * filesystem implements a readdirplus() handler.
 239 */
 240#define FUSE_CAP_READDIRPLUS (1 << 13)
 241
 242/**
 243 * Indicates that the filesystem supports adaptive readdirplus.
 244 *
 245 * If FUSE_CAP_READDIRPLUS is not set, this flag has no effect.
 246 *
 247 * If FUSE_CAP_READDIRPLUS is set and this flag is not set, the kernel
 248 * will always issue readdirplus() requests to retrieve directory
 249 * contents.
 250 *
 251 * If FUSE_CAP_READDIRPLUS is set and this flag is set, the kernel
 252 * will issue both readdir() and readdirplus() requests, depending on
 253 * how much information is expected to be required.
 254 *
 255 * As of Linux 4.20, the algorithm is as follows: when userspace
 256 * starts to read directory entries, issue a READDIRPLUS request to
 257 * the filesystem. If any entry attributes have been looked up by the
 258 * time userspace requests the next batch of entries continue with
 259 * READDIRPLUS, otherwise switch to plain READDIR.  This will reasult
 260 * in eg plain "ls" triggering READDIRPLUS first then READDIR after
 261 * that because it doesn't do lookups.  "ls -l" should result in all
 262 * READDIRPLUS, except if dentries are already cached.
 263 *
 264 * This feature is enabled by default when supported by the kernel and
 265 * if the filesystem implements both a readdirplus() and a readdir()
 266 * handler.
 267 */
 268#define FUSE_CAP_READDIRPLUS_AUTO (1 << 14)
 269
 270/**
 271 * Indicates that the filesystem supports asynchronous direct I/O submission.
 272 *
 273 * If this capability is not requested/available, the kernel will ensure that
 274 * there is at most one pending read and one pending write request per direct
 275 * I/O file-handle at any time.
 276 *
 277 * This feature is enabled by default when supported by the kernel.
 278 */
 279#define FUSE_CAP_ASYNC_DIO (1 << 15)
 280
 281/**
 282 * Indicates that writeback caching should be enabled. This means that
 283 * individual write request may be buffered and merged in the kernel
 284 * before they are send to the filesystem.
 285 *
 286 * This feature is disabled by default.
 287 */
 288#define FUSE_CAP_WRITEBACK_CACHE (1 << 16)
 289
 290/**
 291 * Indicates support for zero-message opens. If this flag is set in
 292 * the `capable` field of the `fuse_conn_info` structure, then the
 293 * filesystem may return `ENOSYS` from the open() handler to indicate
 294 * success. Further attempts to open files will be handled in the
 295 * kernel. (If this flag is not set, returning ENOSYS will be treated
 296 * as an error and signaled to the caller).
 297 *
 298 * Setting (or unsetting) this flag in the `want` field has *no
 299 * effect*.
 300 */
 301#define FUSE_CAP_NO_OPEN_SUPPORT (1 << 17)
 302
 303/**
 304 * Indicates support for parallel directory operations. If this flag
 305 * is unset, the FUSE kernel module will ensure that lookup() and
 306 * readdir() requests are never issued concurrently for the same
 307 * directory.
 308 *
 309 * This feature is enabled by default when supported by the kernel.
 310 */
 311#define FUSE_CAP_PARALLEL_DIROPS (1 << 18)
 312
 313/**
 314 * Indicates support for POSIX ACLs.
 315 *
 316 * If this feature is enabled, the kernel will cache and have
 317 * responsibility for enforcing ACLs. ACL will be stored as xattrs and
 318 * passed to userspace, which is responsible for updating the ACLs in
 319 * the filesystem, keeping the file mode in sync with the ACL, and
 320 * ensuring inheritance of default ACLs when new filesystem nodes are
 321 * created. Note that this requires that the file system is able to
 322 * parse and interpret the xattr representation of ACLs.
 323 *
 324 * Enabling this feature implicitly turns on the
 325 * ``default_permissions`` mount option (even if it was not passed to
 326 * mount(2)).
 327 *
 328 * This feature is disabled by default.
 329 */
 330#define FUSE_CAP_POSIX_ACL (1 << 19)
 331
 332/**
 333 * Indicates that the filesystem is responsible for unsetting
 334 * setuid and setgid bits when a file is written, truncated, or
 335 * its owner is changed.
 336 *
 337 * This feature is enabled by default when supported by the kernel.
 338 */
 339#define FUSE_CAP_HANDLE_KILLPRIV (1 << 20)
 340
 341/**
 342 * Indicates support for zero-message opendirs. If this flag is set in
 343 * the `capable` field of the `fuse_conn_info` structure, then the filesystem
 344 * may return `ENOSYS` from the opendir() handler to indicate success. Further
 345 * opendir and releasedir messages will be handled in the kernel. (If this
 346 * flag is not set, returning ENOSYS will be treated as an error and signalled
 347 * to the caller.)
 348 *
 349 * Setting (or unsetting) this flag in the `want` field has *no effect*.
 350 */
 351#define FUSE_CAP_NO_OPENDIR_SUPPORT (1 << 24)
 352
 353/**
 354 * Indicates that the kernel supports the FUSE_ATTR_SUBMOUNT flag.
 355 *
 356 * Setting (or unsetting) this flag in the `want` field has *no effect*.
 357 */
 358#define FUSE_CAP_SUBMOUNTS (1 << 27)
 359
 360/**
 361 * Indicates that the filesystem is responsible for clearing
 362 * security.capability xattr and clearing setuid and setgid bits. Following
 363 * are the rules.
 364 * - clear "security.capability" on write, truncate and chown unconditionally
 365 * - clear suid/sgid if following is true. Note, sgid is cleared only if
 366 *   group executable bit is set.
 367 *    o setattr has FATTR_SIZE and FATTR_KILL_SUIDGID set.
 368 *    o setattr has FATTR_UID or FATTR_GID
 369 *    o open has O_TRUNC and FUSE_OPEN_KILL_SUIDGID
 370 *    o create has O_TRUNC and FUSE_OPEN_KILL_SUIDGID flag set.
 371 *    o write has FUSE_WRITE_KILL_SUIDGID
 372 */
 373#define FUSE_CAP_HANDLE_KILLPRIV_V2 (1 << 28)
 374
 375/**
 376 * Indicates that file server supports extended struct fuse_setxattr_in
 377 */
 378#define FUSE_CAP_SETXATTR_EXT (1 << 29)
 379
 380/**
 381 * Indicates that file server supports creating file security context
 382 */
 383#define FUSE_CAP_SECURITY_CTX (1ULL << 32)
 384
 385/**
 386 * Ioctl flags
 387 *
 388 * FUSE_IOCTL_COMPAT: 32bit compat ioctl on 64bit machine
 389 * FUSE_IOCTL_UNRESTRICTED: not restricted to well-formed ioctls, retry allowed
 390 * FUSE_IOCTL_RETRY: retry with new iovecs
 391 * FUSE_IOCTL_DIR: is a directory
 392 *
 393 * FUSE_IOCTL_MAX_IOV: maximum of in_iovecs + out_iovecs
 394 */
 395#define FUSE_IOCTL_COMPAT (1 << 0)
 396#define FUSE_IOCTL_UNRESTRICTED (1 << 1)
 397#define FUSE_IOCTL_RETRY (1 << 2)
 398#define FUSE_IOCTL_DIR (1 << 4)
 399
 400#define FUSE_IOCTL_MAX_IOV 256
 401
 402/**
 403 * Connection information, passed to the ->init() method
 404 *
 405 * Some of the elements are read-write, these can be changed to
 406 * indicate the value requested by the filesystem.  The requested
 407 * value must usually be smaller than the indicated value.
 408 */
 409struct fuse_conn_info {
 410    /**
 411     * Major version of the protocol (read-only)
 412     */
 413    unsigned proto_major;
 414
 415    /**
 416     * Minor version of the protocol (read-only)
 417     */
 418    unsigned proto_minor;
 419
 420    /**
 421     * Maximum size of the write buffer
 422     */
 423    unsigned max_write;
 424
 425    /**
 426     * Maximum size of read requests. A value of zero indicates no
 427     * limit. However, even if the filesystem does not specify a
 428     * limit, the maximum size of read requests will still be
 429     * limited by the kernel.
 430     *
 431     * NOTE: For the time being, the maximum size of read requests
 432     * must be set both here *and* passed to fuse_session_new()
 433     * using the ``-o max_read=<n>`` mount option. At some point
 434     * in the future, specifying the mount option will no longer
 435     * be necessary.
 436     */
 437    unsigned max_read;
 438
 439    /**
 440     * Maximum readahead
 441     */
 442    unsigned max_readahead;
 443
 444    /**
 445     * Capability flags that the kernel supports (read-only)
 446     */
 447    uint64_t capable;
 448
 449    /**
 450     * Capability flags that the filesystem wants to enable.
 451     *
 452     * libfuse attempts to initialize this field with
 453     * reasonable default values before calling the init() handler.
 454     */
 455    uint64_t want;
 456
 457    /**
 458     * Maximum number of pending "background" requests. A
 459     * background request is any type of request for which the
 460     * total number is not limited by other means. As of kernel
 461     * 4.8, only two types of requests fall into this category:
 462     *
 463     *   1. Read-ahead requests
 464     *   2. Asynchronous direct I/O requests
 465     *
 466     * Read-ahead requests are generated (if max_readahead is
 467     * non-zero) by the kernel to preemptively fill its caches
 468     * when it anticipates that userspace will soon read more
 469     * data.
 470     *
 471     * Asynchronous direct I/O requests are generated if
 472     * FUSE_CAP_ASYNC_DIO is enabled and userspace submits a large
 473     * direct I/O request. In this case the kernel will internally
 474     * split it up into multiple smaller requests and submit them
 475     * to the filesystem concurrently.
 476     *
 477     * Note that the following requests are *not* background
 478     * requests: writeback requests (limited by the kernel's
 479     * flusher algorithm), regular (i.e., synchronous and
 480     * buffered) userspace read/write requests (limited to one per
 481     * thread), asynchronous read requests (Linux's io_submit(2)
 482     * call actually blocks, so these are also limited to one per
 483     * thread).
 484     */
 485    unsigned max_background;
 486
 487    /**
 488     * Kernel congestion threshold parameter. If the number of pending
 489     * background requests exceeds this number, the FUSE kernel module will
 490     * mark the filesystem as "congested". This instructs the kernel to
 491     * expect that queued requests will take some time to complete, and to
 492     * adjust its algorithms accordingly (e.g. by putting a waiting thread
 493     * to sleep instead of using a busy-loop).
 494     */
 495    unsigned congestion_threshold;
 496
 497    /**
 498     * When FUSE_CAP_WRITEBACK_CACHE is enabled, the kernel is responsible
 499     * for updating mtime and ctime when write requests are received. The
 500     * updated values are passed to the filesystem with setattr() requests.
 501     * However, if the filesystem does not support the full resolution of
 502     * the kernel timestamps (nanoseconds), the mtime and ctime values used
 503     * by kernel and filesystem will differ (and result in an apparent
 504     * change of times after a cache flush).
 505     *
 506     * To prevent this problem, this variable can be used to inform the
 507     * kernel about the timestamp granularity supported by the file-system.
 508     * The value should be power of 10.  The default is 1, i.e. full
 509     * nano-second resolution. Filesystems supporting only second resolution
 510     * should set this to 1000000000.
 511     */
 512    unsigned time_gran;
 513
 514    /**
 515     * For future use.
 516     */
 517    unsigned reserved[22];
 518};
 519
 520struct fuse_session;
 521struct fuse_pollhandle;
 522struct fuse_conn_info_opts;
 523
 524/**
 525 * This function parses several command-line options that can be used
 526 * to override elements of struct fuse_conn_info. The pointer returned
 527 * by this function should be passed to the
 528 * fuse_apply_conn_info_opts() method by the file system's init()
 529 * handler.
 530 *
 531 * Before using this function, think twice if you really want these
 532 * parameters to be adjustable from the command line. In most cases,
 533 * they should be determined by the file system internally.
 534 *
 535 * The following options are recognized:
 536 *
 537 *   -o max_write=N         sets conn->max_write
 538 *   -o max_readahead=N     sets conn->max_readahead
 539 *   -o max_background=N    sets conn->max_background
 540 *   -o congestion_threshold=N  sets conn->congestion_threshold
 541 *   -o async_read          sets FUSE_CAP_ASYNC_READ in conn->want
 542 *   -o sync_read           unsets FUSE_CAP_ASYNC_READ in conn->want
 543 *   -o atomic_o_trunc      sets FUSE_CAP_ATOMIC_O_TRUNC in conn->want
 544 *   -o no_remote_lock      Equivalent to -o
 545 *no_remote_flock,no_remote_posix_lock -o no_remote_flock     Unsets
 546 *FUSE_CAP_FLOCK_LOCKS in conn->want -o no_remote_posix_lock  Unsets
 547 *FUSE_CAP_POSIX_LOCKS in conn->want -o [no_]splice_write     (un-)sets
 548 *FUSE_CAP_SPLICE_WRITE in conn->want -o [no_]splice_move      (un-)sets
 549 *FUSE_CAP_SPLICE_MOVE in conn->want -o [no_]splice_read      (un-)sets
 550 *FUSE_CAP_SPLICE_READ in conn->want -o [no_]auto_inval_data  (un-)sets
 551 *FUSE_CAP_AUTO_INVAL_DATA in conn->want -o readdirplus=no        unsets
 552 *FUSE_CAP_READDIRPLUS in conn->want -o readdirplus=yes       sets
 553 *FUSE_CAP_READDIRPLUS and unsets FUSE_CAP_READDIRPLUS_AUTO in conn->want -o
 554 *readdirplus=auto      sets FUSE_CAP_READDIRPLUS and FUSE_CAP_READDIRPLUS_AUTO
 555 *in conn->want -o [no_]async_dio        (un-)sets FUSE_CAP_ASYNC_DIO in
 556 *conn->want -o [no_]writeback_cache  (un-)sets FUSE_CAP_WRITEBACK_CACHE in
 557 *conn->want -o time_gran=N           sets conn->time_gran
 558 *
 559 * Known options will be removed from *args*, unknown options will be
 560 * passed through unchanged.
 561 *
 562 * @param args argument vector (input+output)
 563 * @return parsed options
 564 **/
 565struct fuse_conn_info_opts *fuse_parse_conn_info_opts(struct fuse_args *args);
 566
 567/**
 568 * This function applies the (parsed) parameters in *opts* to the
 569 * *conn* pointer. It may modify the following fields: wants,
 570 * max_write, max_readahead, congestion_threshold, max_background,
 571 * time_gran. A field is only set (or unset) if the corresponding
 572 * option has been explicitly set.
 573 */
 574void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
 575                               struct fuse_conn_info *conn);
 576
 577/**
 578 * Go into the background
 579 *
 580 * @param foreground if true, stay in the foreground
 581 * @return 0 on success, -1 on failure
 582 */
 583int fuse_daemonize(int foreground);
 584
 585/**
 586 * Get the version of the library
 587 *
 588 * @return the version
 589 */
 590int fuse_version(void);
 591
 592/**
 593 * Get the full package version string of the library
 594 *
 595 * @return the package version
 596 */
 597const char *fuse_pkgversion(void);
 598
 599/**
 600 * Destroy poll handle
 601 *
 602 * @param ph the poll handle
 603 */
 604void fuse_pollhandle_destroy(struct fuse_pollhandle *ph);
 605
 606/*
 607 * Data buffer
 608 */
 609
 610/**
 611 * Buffer flags
 612 */
 613enum fuse_buf_flags {
 614    /**
 615     * Buffer contains a file descriptor
 616     *
 617     * If this flag is set, the .fd field is valid, otherwise the
 618     * .mem fields is valid.
 619     */
 620    FUSE_BUF_IS_FD = (1 << 1),
 621
 622    /**
 623     * Seek on the file descriptor
 624     *
 625     * If this flag is set then the .pos field is valid and is
 626     * used to seek to the given offset before performing
 627     * operation on file descriptor.
 628     */
 629    FUSE_BUF_FD_SEEK = (1 << 2),
 630
 631    /**
 632     * Retry operation on file descriptor
 633     *
 634     * If this flag is set then retry operation on file descriptor
 635     * until .size bytes have been copied or an error or EOF is
 636     * detected.
 637     */
 638    FUSE_BUF_FD_RETRY = (1 << 3),
 639};
 640
 641/**
 642 * Single data buffer
 643 *
 644 * Generic data buffer for I/O, extended attributes, etc...  Data may
 645 * be supplied as a memory pointer or as a file descriptor
 646 */
 647struct fuse_buf {
 648    /**
 649     * Size of data in bytes
 650     */
 651    size_t size;
 652
 653    /**
 654     * Buffer flags
 655     */
 656    enum fuse_buf_flags flags;
 657
 658    /**
 659     * Memory pointer
 660     *
 661     * Used unless FUSE_BUF_IS_FD flag is set.
 662     */
 663    void *mem;
 664
 665    /**
 666     * File descriptor
 667     *
 668     * Used if FUSE_BUF_IS_FD flag is set.
 669     */
 670    int fd;
 671
 672    /**
 673     * File position
 674     *
 675     * Used if FUSE_BUF_FD_SEEK flag is set.
 676     */
 677    off_t pos;
 678};
 679
 680/**
 681 * Data buffer vector
 682 *
 683 * An array of data buffers, each containing a memory pointer or a
 684 * file descriptor.
 685 *
 686 * Allocate dynamically to add more than one buffer.
 687 */
 688struct fuse_bufvec {
 689    /**
 690     * Number of buffers in the array
 691     */
 692    size_t count;
 693
 694    /**
 695     * Index of current buffer within the array
 696     */
 697    size_t idx;
 698
 699    /**
 700     * Current offset within the current buffer
 701     */
 702    size_t off;
 703
 704    /**
 705     * Array of buffers
 706     */
 707    struct fuse_buf buf[1];
 708};
 709
 710/* Initialize bufvec with a single buffer of given size */
 711#define FUSE_BUFVEC_INIT(size__)                                      \
 712    ((struct fuse_bufvec){ /* .count= */ 1,                           \
 713                           /* .idx =  */ 0,                           \
 714                           /* .off =  */ 0, /* .buf =  */             \
 715                           { /* [0] = */ {                            \
 716                               /* .size =  */ (size__),               \
 717                               /* .flags = */ (enum fuse_buf_flags)0, \
 718                               /* .mem =   */ NULL,                   \
 719                               /* .fd =    */ -1,                     \
 720                               /* .pos =   */ 0,                      \
 721                           } } })
 722
 723/**
 724 * Get total size of data in a fuse buffer vector
 725 *
 726 * @param bufv buffer vector
 727 * @return size of data
 728 */
 729size_t fuse_buf_size(const struct fuse_bufvec *bufv);
 730
 731/**
 732 * Copy data from one buffer vector to another
 733 *
 734 * @param dst destination buffer vector
 735 * @param src source buffer vector
 736 * @return actual number of bytes copied or -errno on error
 737 */
 738ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src);
 739
 740/**
 741 * Memory buffer iterator
 742 *
 743 */
 744struct fuse_mbuf_iter {
 745    /**
 746     * Data pointer
 747     */
 748    void *mem;
 749
 750    /**
 751     * Total length, in bytes
 752     */
 753    size_t size;
 754
 755    /**
 756     * Offset from start of buffer
 757     */
 758    size_t pos;
 759};
 760
 761/* Initialize memory buffer iterator from a fuse_buf */
 762#define FUSE_MBUF_ITER_INIT(fbuf) \
 763    ((struct fuse_mbuf_iter){     \
 764        .mem = fbuf->mem,         \
 765        .size = fbuf->size,       \
 766        .pos = 0,                 \
 767    })
 768
 769/**
 770 * Consume bytes from a memory buffer iterator
 771 *
 772 * @param iter memory buffer iterator
 773 * @param len number of bytes to consume
 774 * @return pointer to start of consumed bytes or
 775 *         NULL if advancing beyond end of buffer
 776 */
 777void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len);
 778
 779/**
 780 * Consume a NUL-terminated string from a memory buffer iterator
 781 *
 782 * @param iter memory buffer iterator
 783 * @return pointer to the string or
 784 *         NULL if advancing beyond end of buffer or there is no NUL-terminator
 785 */
 786const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter);
 787
 788/*
 789 * Signal handling
 790 */
 791/**
 792 * Exit session on HUP, TERM and INT signals and ignore PIPE signal
 793 *
 794 * Stores session in a global variable. May only be called once per
 795 * process until fuse_remove_signal_handlers() is called.
 796 *
 797 * Once either of the POSIX signals arrives, the signal handler calls
 798 * fuse_session_exit().
 799 *
 800 * @param se the session to exit
 801 * @return 0 on success, -1 on failure
 802 *
 803 * See also:
 804 * fuse_remove_signal_handlers()
 805 */
 806int fuse_set_signal_handlers(struct fuse_session *se);
 807
 808/**
 809 * Restore default signal handlers
 810 *
 811 * Resets global session.  After this fuse_set_signal_handlers() may
 812 * be called again.
 813 *
 814 * @param se the same session as given in fuse_set_signal_handlers()
 815 *
 816 * See also:
 817 * fuse_set_signal_handlers()
 818 */
 819void fuse_remove_signal_handlers(struct fuse_session *se);
 820
 821/*
 822 * Compatibility stuff
 823 */
 824
 825#if !defined(FUSE_USE_VERSION) || FUSE_USE_VERSION < 30
 826#error only API version 30 or greater is supported
 827#endif
 828
 829
 830/*
 831 * This interface uses 64 bit off_t.
 832 *
 833 * On 32bit systems please add -D_FILE_OFFSET_BITS=64 to your compile flags!
 834 */
 835QEMU_BUILD_BUG_ON(sizeof(off_t) != 8);
 836
 837#endif /* FUSE_COMMON_H_ */
 838