Back to home page

OSCL-LXR

 
 

    


0001 /*
0002   FUSE: Filesystem in Userspace
0003   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
0004 
0005   This program can be distributed under the terms of the GNU GPL.
0006   See the file COPYING.
0007 */
0008 
0009 #ifndef _FS_FUSE_I_H
0010 #define _FS_FUSE_I_H
0011 
0012 #ifndef pr_fmt
0013 # define pr_fmt(fmt) "fuse: " fmt
0014 #endif
0015 
0016 #include <linux/fuse.h>
0017 #include <linux/fs.h>
0018 #include <linux/mount.h>
0019 #include <linux/wait.h>
0020 #include <linux/list.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/mm.h>
0023 #include <linux/backing-dev.h>
0024 #include <linux/mutex.h>
0025 #include <linux/rwsem.h>
0026 #include <linux/rbtree.h>
0027 #include <linux/poll.h>
0028 #include <linux/workqueue.h>
0029 #include <linux/kref.h>
0030 #include <linux/xattr.h>
0031 #include <linux/pid_namespace.h>
0032 #include <linux/refcount.h>
0033 #include <linux/user_namespace.h>
0034 
0035 /** Default max number of pages that can be used in a single read request */
0036 #define FUSE_DEFAULT_MAX_PAGES_PER_REQ 32
0037 
0038 /** Maximum of max_pages received in init_out */
0039 #define FUSE_MAX_MAX_PAGES 256
0040 
0041 /** Bias for fi->writectr, meaning new writepages must not be sent */
0042 #define FUSE_NOWRITE INT_MIN
0043 
0044 /** It could be as large as PATH_MAX, but would that have any uses? */
0045 #define FUSE_NAME_MAX 1024
0046 
0047 /** Number of dentries for each connection in the control filesystem */
0048 #define FUSE_CTL_NUM_DENTRIES 5
0049 
0050 /** List of active connections */
0051 extern struct list_head fuse_conn_list;
0052 
0053 /** Global mutex protecting fuse_conn_list and the control filesystem */
0054 extern struct mutex fuse_mutex;
0055 
0056 /** Module parameters */
0057 extern unsigned max_user_bgreq;
0058 extern unsigned max_user_congthresh;
0059 
0060 /* One forget request */
0061 struct fuse_forget_link {
0062     struct fuse_forget_one forget_one;
0063     struct fuse_forget_link *next;
0064 };
0065 
0066 /** FUSE inode */
0067 struct fuse_inode {
0068     /** Inode data */
0069     struct inode inode;
0070 
0071     /** Unique ID, which identifies the inode between userspace
0072      * and kernel */
0073     u64 nodeid;
0074 
0075     /** Number of lookups on this inode */
0076     u64 nlookup;
0077 
0078     /** The request used for sending the FORGET message */
0079     struct fuse_forget_link *forget;
0080 
0081     /** Time in jiffies until the file attributes are valid */
0082     u64 i_time;
0083 
0084     /* Which attributes are invalid */
0085     u32 inval_mask;
0086 
0087     /** The sticky bit in inode->i_mode may have been removed, so
0088         preserve the original mode */
0089     umode_t orig_i_mode;
0090 
0091     /** 64 bit inode number */
0092     u64 orig_ino;
0093 
0094     /** Version of last attribute change */
0095     u64 attr_version;
0096 
0097     union {
0098         /* Write related fields (regular file only) */
0099         struct {
0100             /* Files usable in writepage.  Protected by fi->lock */
0101             struct list_head write_files;
0102 
0103             /* Writepages pending on truncate or fsync */
0104             struct list_head queued_writes;
0105 
0106             /* Number of sent writes, a negative bias
0107              * (FUSE_NOWRITE) means more writes are blocked */
0108             int writectr;
0109 
0110             /* Waitq for writepage completion */
0111             wait_queue_head_t page_waitq;
0112 
0113             /* List of writepage requestst (pending or sent) */
0114             struct rb_root writepages;
0115         };
0116 
0117         /* readdir cache (directory only) */
0118         struct {
0119             /* true if fully cached */
0120             bool cached;
0121 
0122             /* size of cache */
0123             loff_t size;
0124 
0125             /* position at end of cache (position of next entry) */
0126             loff_t pos;
0127 
0128             /* version of the cache */
0129             u64 version;
0130 
0131             /* modification time of directory when cache was
0132              * started */
0133             struct timespec64 mtime;
0134 
0135             /* iversion of directory when cache was started */
0136             u64 iversion;
0137 
0138             /* protects above fields */
0139             spinlock_t lock;
0140         } rdc;
0141     };
0142 
0143     /** Miscellaneous bits describing inode state */
0144     unsigned long state;
0145 
0146     /** Lock for serializing lookup and readdir for back compatibility*/
0147     struct mutex mutex;
0148 
0149     /** Lock to protect write related fields */
0150     spinlock_t lock;
0151 
0152 #ifdef CONFIG_FUSE_DAX
0153     /*
0154      * Dax specific inode data
0155      */
0156     struct fuse_inode_dax *dax;
0157 #endif
0158 };
0159 
0160 /** FUSE inode state bits */
0161 enum {
0162     /** Advise readdirplus  */
0163     FUSE_I_ADVISE_RDPLUS,
0164     /** Initialized with readdirplus */
0165     FUSE_I_INIT_RDPLUS,
0166     /** An operation changing file size is in progress  */
0167     FUSE_I_SIZE_UNSTABLE,
0168     /* Bad inode */
0169     FUSE_I_BAD,
0170 };
0171 
0172 struct fuse_conn;
0173 struct fuse_mount;
0174 struct fuse_release_args;
0175 
0176 /** FUSE specific file data */
0177 struct fuse_file {
0178     /** Fuse connection for this file */
0179     struct fuse_mount *fm;
0180 
0181     /* Argument space reserved for release */
0182     struct fuse_release_args *release_args;
0183 
0184     /** Kernel file handle guaranteed to be unique */
0185     u64 kh;
0186 
0187     /** File handle used by userspace */
0188     u64 fh;
0189 
0190     /** Node id of this file */
0191     u64 nodeid;
0192 
0193     /** Refcount */
0194     refcount_t count;
0195 
0196     /** FOPEN_* flags returned by open */
0197     u32 open_flags;
0198 
0199     /** Entry on inode's write_files list */
0200     struct list_head write_entry;
0201 
0202     /* Readdir related */
0203     struct {
0204         /*
0205          * Protects below fields against (crazy) parallel readdir on
0206          * same open file.  Uncontended in the normal case.
0207          */
0208         struct mutex lock;
0209 
0210         /* Dir stream position */
0211         loff_t pos;
0212 
0213         /* Offset in cache */
0214         loff_t cache_off;
0215 
0216         /* Version of cache we are reading */
0217         u64 version;
0218 
0219     } readdir;
0220 
0221     /** RB node to be linked on fuse_conn->polled_files */
0222     struct rb_node polled_node;
0223 
0224     /** Wait queue head for poll */
0225     wait_queue_head_t poll_wait;
0226 
0227     /** Has flock been performed on this file? */
0228     bool flock:1;
0229 };
0230 
0231 /** One input argument of a request */
0232 struct fuse_in_arg {
0233     unsigned size;
0234     const void *value;
0235 };
0236 
0237 /** One output argument of a request */
0238 struct fuse_arg {
0239     unsigned size;
0240     void *value;
0241 };
0242 
0243 /** FUSE page descriptor */
0244 struct fuse_page_desc {
0245     unsigned int length;
0246     unsigned int offset;
0247 };
0248 
0249 struct fuse_args {
0250     uint64_t nodeid;
0251     uint32_t opcode;
0252     unsigned short in_numargs;
0253     unsigned short out_numargs;
0254     bool force:1;
0255     bool noreply:1;
0256     bool nocreds:1;
0257     bool in_pages:1;
0258     bool out_pages:1;
0259     bool user_pages:1;
0260     bool out_argvar:1;
0261     bool page_zeroing:1;
0262     bool page_replace:1;
0263     bool may_block:1;
0264     struct fuse_in_arg in_args[3];
0265     struct fuse_arg out_args[2];
0266     void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error);
0267 };
0268 
0269 struct fuse_args_pages {
0270     struct fuse_args args;
0271     struct page **pages;
0272     struct fuse_page_desc *descs;
0273     unsigned int num_pages;
0274 };
0275 
0276 #define FUSE_ARGS(args) struct fuse_args args = {}
0277 
0278 /** The request IO state (for asynchronous processing) */
0279 struct fuse_io_priv {
0280     struct kref refcnt;
0281     int async;
0282     spinlock_t lock;
0283     unsigned reqs;
0284     ssize_t bytes;
0285     size_t size;
0286     __u64 offset;
0287     bool write;
0288     bool should_dirty;
0289     int err;
0290     struct kiocb *iocb;
0291     struct completion *done;
0292     bool blocking;
0293 };
0294 
0295 #define FUSE_IO_PRIV_SYNC(i) \
0296 {                   \
0297     .refcnt = KREF_INIT(1),     \
0298     .async = 0,         \
0299     .iocb = i,          \
0300 }
0301 
0302 /**
0303  * Request flags
0304  *
0305  * FR_ISREPLY:      set if the request has reply
0306  * FR_FORCE:        force sending of the request even if interrupted
0307  * FR_BACKGROUND:   request is sent in the background
0308  * FR_WAITING:      request is counted as "waiting"
0309  * FR_ABORTED:      the request was aborted
0310  * FR_INTERRUPTED:  the request has been interrupted
0311  * FR_LOCKED:       data is being copied to/from the request
0312  * FR_PENDING:      request is not yet in userspace
0313  * FR_SENT:     request is in userspace, waiting for an answer
0314  * FR_FINISHED:     request is finished
0315  * FR_PRIVATE:      request is on private list
0316  * FR_ASYNC:        request is asynchronous
0317  */
0318 enum fuse_req_flag {
0319     FR_ISREPLY,
0320     FR_FORCE,
0321     FR_BACKGROUND,
0322     FR_WAITING,
0323     FR_ABORTED,
0324     FR_INTERRUPTED,
0325     FR_LOCKED,
0326     FR_PENDING,
0327     FR_SENT,
0328     FR_FINISHED,
0329     FR_PRIVATE,
0330     FR_ASYNC,
0331 };
0332 
0333 /**
0334  * A request to the client
0335  *
0336  * .waitq.lock protects the following fields:
0337  *   - FR_ABORTED
0338  *   - FR_LOCKED (may also be modified under fc->lock, tested under both)
0339  */
0340 struct fuse_req {
0341     /** This can be on either pending processing or io lists in
0342         fuse_conn */
0343     struct list_head list;
0344 
0345     /** Entry on the interrupts list  */
0346     struct list_head intr_entry;
0347 
0348     /* Input/output arguments */
0349     struct fuse_args *args;
0350 
0351     /** refcount */
0352     refcount_t count;
0353 
0354     /* Request flags, updated with test/set/clear_bit() */
0355     unsigned long flags;
0356 
0357     /* The request input header */
0358     struct {
0359         struct fuse_in_header h;
0360     } in;
0361 
0362     /* The request output header */
0363     struct {
0364         struct fuse_out_header h;
0365     } out;
0366 
0367     /** Used to wake up the task waiting for completion of request*/
0368     wait_queue_head_t waitq;
0369 
0370 #if IS_ENABLED(CONFIG_VIRTIO_FS)
0371     /** virtio-fs's physically contiguous buffer for in and out args */
0372     void *argbuf;
0373 #endif
0374 
0375     /** fuse_mount this request belongs to */
0376     struct fuse_mount *fm;
0377 };
0378 
0379 struct fuse_iqueue;
0380 
0381 /**
0382  * Input queue callbacks
0383  *
0384  * Input queue signalling is device-specific.  For example, the /dev/fuse file
0385  * uses fiq->waitq and fasync to wake processes that are waiting on queue
0386  * readiness.  These callbacks allow other device types to respond to input
0387  * queue activity.
0388  */
0389 struct fuse_iqueue_ops {
0390     /**
0391      * Signal that a forget has been queued
0392      */
0393     void (*wake_forget_and_unlock)(struct fuse_iqueue *fiq)
0394         __releases(fiq->lock);
0395 
0396     /**
0397      * Signal that an INTERRUPT request has been queued
0398      */
0399     void (*wake_interrupt_and_unlock)(struct fuse_iqueue *fiq)
0400         __releases(fiq->lock);
0401 
0402     /**
0403      * Signal that a request has been queued
0404      */
0405     void (*wake_pending_and_unlock)(struct fuse_iqueue *fiq)
0406         __releases(fiq->lock);
0407 
0408     /**
0409      * Clean up when fuse_iqueue is destroyed
0410      */
0411     void (*release)(struct fuse_iqueue *fiq);
0412 };
0413 
0414 /** /dev/fuse input queue operations */
0415 extern const struct fuse_iqueue_ops fuse_dev_fiq_ops;
0416 
0417 struct fuse_iqueue {
0418     /** Connection established */
0419     unsigned connected;
0420 
0421     /** Lock protecting accesses to members of this structure */
0422     spinlock_t lock;
0423 
0424     /** Readers of the connection are waiting on this */
0425     wait_queue_head_t waitq;
0426 
0427     /** The next unique request id */
0428     u64 reqctr;
0429 
0430     /** The list of pending requests */
0431     struct list_head pending;
0432 
0433     /** Pending interrupts */
0434     struct list_head interrupts;
0435 
0436     /** Queue of pending forgets */
0437     struct fuse_forget_link forget_list_head;
0438     struct fuse_forget_link *forget_list_tail;
0439 
0440     /** Batching of FORGET requests (positive indicates FORGET batch) */
0441     int forget_batch;
0442 
0443     /** O_ASYNC requests */
0444     struct fasync_struct *fasync;
0445 
0446     /** Device-specific callbacks */
0447     const struct fuse_iqueue_ops *ops;
0448 
0449     /** Device-specific state */
0450     void *priv;
0451 };
0452 
0453 #define FUSE_PQ_HASH_BITS 8
0454 #define FUSE_PQ_HASH_SIZE (1 << FUSE_PQ_HASH_BITS)
0455 
0456 struct fuse_pqueue {
0457     /** Connection established */
0458     unsigned connected;
0459 
0460     /** Lock protecting accessess to  members of this structure */
0461     spinlock_t lock;
0462 
0463     /** Hash table of requests being processed */
0464     struct list_head *processing;
0465 
0466     /** The list of requests under I/O */
0467     struct list_head io;
0468 };
0469 
0470 /**
0471  * Fuse device instance
0472  */
0473 struct fuse_dev {
0474     /** Fuse connection for this device */
0475     struct fuse_conn *fc;
0476 
0477     /** Processing queue */
0478     struct fuse_pqueue pq;
0479 
0480     /** list entry on fc->devices */
0481     struct list_head entry;
0482 };
0483 
0484 enum fuse_dax_mode {
0485     FUSE_DAX_INODE_DEFAULT, /* default */
0486     FUSE_DAX_ALWAYS,    /* "-o dax=always" */
0487     FUSE_DAX_NEVER,     /* "-o dax=never" */
0488     FUSE_DAX_INODE_USER,    /* "-o dax=inode" */
0489 };
0490 
0491 static inline bool fuse_is_inode_dax_mode(enum fuse_dax_mode mode)
0492 {
0493     return mode == FUSE_DAX_INODE_DEFAULT || mode == FUSE_DAX_INODE_USER;
0494 }
0495 
0496 struct fuse_fs_context {
0497     int fd;
0498     struct file *file;
0499     unsigned int rootmode;
0500     kuid_t user_id;
0501     kgid_t group_id;
0502     bool is_bdev:1;
0503     bool fd_present:1;
0504     bool rootmode_present:1;
0505     bool user_id_present:1;
0506     bool group_id_present:1;
0507     bool default_permissions:1;
0508     bool allow_other:1;
0509     bool destroy:1;
0510     bool no_control:1;
0511     bool no_force_umount:1;
0512     bool legacy_opts_show:1;
0513     enum fuse_dax_mode dax_mode;
0514     unsigned int max_read;
0515     unsigned int blksize;
0516     const char *subtype;
0517 
0518     /* DAX device, may be NULL */
0519     struct dax_device *dax_dev;
0520 
0521     /* fuse_dev pointer to fill in, should contain NULL on entry */
0522     void **fudptr;
0523 };
0524 
0525 struct fuse_sync_bucket {
0526     /* count is a possible scalability bottleneck */
0527     atomic_t count;
0528     wait_queue_head_t waitq;
0529     struct rcu_head rcu;
0530 };
0531 
0532 /**
0533  * A Fuse connection.
0534  *
0535  * This structure is created, when the root filesystem is mounted, and
0536  * is destroyed, when the client device is closed and the last
0537  * fuse_mount is destroyed.
0538  */
0539 struct fuse_conn {
0540     /** Lock protecting accessess to  members of this structure */
0541     spinlock_t lock;
0542 
0543     /** Refcount */
0544     refcount_t count;
0545 
0546     /** Number of fuse_dev's */
0547     atomic_t dev_count;
0548 
0549     struct rcu_head rcu;
0550 
0551     /** The user id for this mount */
0552     kuid_t user_id;
0553 
0554     /** The group id for this mount */
0555     kgid_t group_id;
0556 
0557     /** The pid namespace for this mount */
0558     struct pid_namespace *pid_ns;
0559 
0560     /** The user namespace for this mount */
0561     struct user_namespace *user_ns;
0562 
0563     /** Maximum read size */
0564     unsigned max_read;
0565 
0566     /** Maximum write size */
0567     unsigned max_write;
0568 
0569     /** Maximum number of pages that can be used in a single request */
0570     unsigned int max_pages;
0571 
0572     /** Constrain ->max_pages to this value during feature negotiation */
0573     unsigned int max_pages_limit;
0574 
0575     /** Input queue */
0576     struct fuse_iqueue iq;
0577 
0578     /** The next unique kernel file handle */
0579     atomic64_t khctr;
0580 
0581     /** rbtree of fuse_files waiting for poll events indexed by ph */
0582     struct rb_root polled_files;
0583 
0584     /** Maximum number of outstanding background requests */
0585     unsigned max_background;
0586 
0587     /** Number of background requests at which congestion starts */
0588     unsigned congestion_threshold;
0589 
0590     /** Number of requests currently in the background */
0591     unsigned num_background;
0592 
0593     /** Number of background requests currently queued for userspace */
0594     unsigned active_background;
0595 
0596     /** The list of background requests set aside for later queuing */
0597     struct list_head bg_queue;
0598 
0599     /** Protects: max_background, congestion_threshold, num_background,
0600      * active_background, bg_queue, blocked */
0601     spinlock_t bg_lock;
0602 
0603     /** Flag indicating that INIT reply has been received. Allocating
0604      * any fuse request will be suspended until the flag is set */
0605     int initialized;
0606 
0607     /** Flag indicating if connection is blocked.  This will be
0608         the case before the INIT reply is received, and if there
0609         are too many outstading backgrounds requests */
0610     int blocked;
0611 
0612     /** waitq for blocked connection */
0613     wait_queue_head_t blocked_waitq;
0614 
0615     /** Connection established, cleared on umount, connection
0616         abort and device release */
0617     unsigned connected;
0618 
0619     /** Connection aborted via sysfs */
0620     bool aborted;
0621 
0622     /** Connection failed (version mismatch).  Cannot race with
0623         setting other bitfields since it is only set once in INIT
0624         reply, before any other request, and never cleared */
0625     unsigned conn_error:1;
0626 
0627     /** Connection successful.  Only set in INIT */
0628     unsigned conn_init:1;
0629 
0630     /** Do readahead asynchronously?  Only set in INIT */
0631     unsigned async_read:1;
0632 
0633     /** Return an unique read error after abort.  Only set in INIT */
0634     unsigned abort_err:1;
0635 
0636     /** Do not send separate SETATTR request before open(O_TRUNC)  */
0637     unsigned atomic_o_trunc:1;
0638 
0639     /** Filesystem supports NFS exporting.  Only set in INIT */
0640     unsigned export_support:1;
0641 
0642     /** write-back cache policy (default is write-through) */
0643     unsigned writeback_cache:1;
0644 
0645     /** allow parallel lookups and readdir (default is serialized) */
0646     unsigned parallel_dirops:1;
0647 
0648     /** handle fs handles killing suid/sgid/cap on write/chown/trunc */
0649     unsigned handle_killpriv:1;
0650 
0651     /** cache READLINK responses in page cache */
0652     unsigned cache_symlinks:1;
0653 
0654     /* show legacy mount options */
0655     unsigned int legacy_opts_show:1;
0656 
0657     /*
0658      * fs kills suid/sgid/cap on write/chown/trunc. suid is killed on
0659      * write/trunc only if caller did not have CAP_FSETID.  sgid is killed
0660      * on write/truncate only if caller did not have CAP_FSETID as well as
0661      * file has group execute permission.
0662      */
0663     unsigned handle_killpriv_v2:1;
0664 
0665     /*
0666      * The following bitfields are only for optimization purposes
0667      * and hence races in setting them will not cause malfunction
0668      */
0669 
0670     /** Is open/release not implemented by fs? */
0671     unsigned no_open:1;
0672 
0673     /** Is opendir/releasedir not implemented by fs? */
0674     unsigned no_opendir:1;
0675 
0676     /** Is fsync not implemented by fs? */
0677     unsigned no_fsync:1;
0678 
0679     /** Is fsyncdir not implemented by fs? */
0680     unsigned no_fsyncdir:1;
0681 
0682     /** Is flush not implemented by fs? */
0683     unsigned no_flush:1;
0684 
0685     /** Is setxattr not implemented by fs? */
0686     unsigned no_setxattr:1;
0687 
0688     /** Does file server support extended setxattr */
0689     unsigned setxattr_ext:1;
0690 
0691     /** Is getxattr not implemented by fs? */
0692     unsigned no_getxattr:1;
0693 
0694     /** Is listxattr not implemented by fs? */
0695     unsigned no_listxattr:1;
0696 
0697     /** Is removexattr not implemented by fs? */
0698     unsigned no_removexattr:1;
0699 
0700     /** Are posix file locking primitives not implemented by fs? */
0701     unsigned no_lock:1;
0702 
0703     /** Is access not implemented by fs? */
0704     unsigned no_access:1;
0705 
0706     /** Is create not implemented by fs? */
0707     unsigned no_create:1;
0708 
0709     /** Is interrupt not implemented by fs? */
0710     unsigned no_interrupt:1;
0711 
0712     /** Is bmap not implemented by fs? */
0713     unsigned no_bmap:1;
0714 
0715     /** Is poll not implemented by fs? */
0716     unsigned no_poll:1;
0717 
0718     /** Do multi-page cached writes */
0719     unsigned big_writes:1;
0720 
0721     /** Don't apply umask to creation modes */
0722     unsigned dont_mask:1;
0723 
0724     /** Are BSD file locking primitives not implemented by fs? */
0725     unsigned no_flock:1;
0726 
0727     /** Is fallocate not implemented by fs? */
0728     unsigned no_fallocate:1;
0729 
0730     /** Is rename with flags implemented by fs? */
0731     unsigned no_rename2:1;
0732 
0733     /** Use enhanced/automatic page cache invalidation. */
0734     unsigned auto_inval_data:1;
0735 
0736     /** Filesystem is fully responsible for page cache invalidation. */
0737     unsigned explicit_inval_data:1;
0738 
0739     /** Does the filesystem support readdirplus? */
0740     unsigned do_readdirplus:1;
0741 
0742     /** Does the filesystem want adaptive readdirplus? */
0743     unsigned readdirplus_auto:1;
0744 
0745     /** Does the filesystem support asynchronous direct-IO submission? */
0746     unsigned async_dio:1;
0747 
0748     /** Is lseek not implemented by fs? */
0749     unsigned no_lseek:1;
0750 
0751     /** Does the filesystem support posix acls? */
0752     unsigned posix_acl:1;
0753 
0754     /** Check permissions based on the file mode or not? */
0755     unsigned default_permissions:1;
0756 
0757     /** Allow other than the mounter user to access the filesystem ? */
0758     unsigned allow_other:1;
0759 
0760     /** Does the filesystem support copy_file_range? */
0761     unsigned no_copy_file_range:1;
0762 
0763     /* Send DESTROY request */
0764     unsigned int destroy:1;
0765 
0766     /* Delete dentries that have gone stale */
0767     unsigned int delete_stale:1;
0768 
0769     /** Do not create entry in fusectl fs */
0770     unsigned int no_control:1;
0771 
0772     /** Do not allow MNT_FORCE umount */
0773     unsigned int no_force_umount:1;
0774 
0775     /* Auto-mount submounts announced by the server */
0776     unsigned int auto_submounts:1;
0777 
0778     /* Propagate syncfs() to server */
0779     unsigned int sync_fs:1;
0780 
0781     /* Initialize security xattrs when creating a new inode */
0782     unsigned int init_security:1;
0783 
0784     /* Does the filesystem support per inode DAX? */
0785     unsigned int inode_dax:1;
0786 
0787     /** The number of requests waiting for completion */
0788     atomic_t num_waiting;
0789 
0790     /** Negotiated minor version */
0791     unsigned minor;
0792 
0793     /** Entry on the fuse_mount_list */
0794     struct list_head entry;
0795 
0796     /** Device ID from the root super block */
0797     dev_t dev;
0798 
0799     /** Dentries in the control filesystem */
0800     struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
0801 
0802     /** number of dentries used in the above array */
0803     int ctl_ndents;
0804 
0805     /** Key for lock owner ID scrambling */
0806     u32 scramble_key[4];
0807 
0808     /** Version counter for attribute changes */
0809     atomic64_t attr_version;
0810 
0811     /** Called on final put */
0812     void (*release)(struct fuse_conn *);
0813 
0814     /**
0815      * Read/write semaphore to hold when accessing the sb of any
0816      * fuse_mount belonging to this connection
0817      */
0818     struct rw_semaphore killsb;
0819 
0820     /** List of device instances belonging to this connection */
0821     struct list_head devices;
0822 
0823 #ifdef CONFIG_FUSE_DAX
0824     /* Dax mode */
0825     enum fuse_dax_mode dax_mode;
0826 
0827     /* Dax specific conn data, non-NULL if DAX is enabled */
0828     struct fuse_conn_dax *dax;
0829 #endif
0830 
0831     /** List of filesystems using this connection */
0832     struct list_head mounts;
0833 
0834     /* New writepages go into this bucket */
0835     struct fuse_sync_bucket __rcu *curr_bucket;
0836 };
0837 
0838 /*
0839  * Represents a mounted filesystem, potentially a submount.
0840  *
0841  * This object allows sharing a fuse_conn between separate mounts to
0842  * allow submounts with dedicated superblocks and thus separate device
0843  * IDs.
0844  */
0845 struct fuse_mount {
0846     /* Underlying (potentially shared) connection to the FUSE server */
0847     struct fuse_conn *fc;
0848 
0849     /*
0850      * Super block for this connection (fc->killsb must be held when
0851      * accessing this).
0852      */
0853     struct super_block *sb;
0854 
0855     /* Entry on fc->mounts */
0856     struct list_head fc_entry;
0857 };
0858 
0859 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb)
0860 {
0861     return sb->s_fs_info;
0862 }
0863 
0864 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
0865 {
0866     return get_fuse_mount_super(sb)->fc;
0867 }
0868 
0869 static inline struct fuse_mount *get_fuse_mount(struct inode *inode)
0870 {
0871     return get_fuse_mount_super(inode->i_sb);
0872 }
0873 
0874 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
0875 {
0876     return get_fuse_mount_super(inode->i_sb)->fc;
0877 }
0878 
0879 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
0880 {
0881     return container_of(inode, struct fuse_inode, inode);
0882 }
0883 
0884 static inline u64 get_node_id(struct inode *inode)
0885 {
0886     return get_fuse_inode(inode)->nodeid;
0887 }
0888 
0889 static inline int invalid_nodeid(u64 nodeid)
0890 {
0891     return !nodeid || nodeid == FUSE_ROOT_ID;
0892 }
0893 
0894 static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
0895 {
0896     return atomic64_read(&fc->attr_version);
0897 }
0898 
0899 static inline bool fuse_stale_inode(const struct inode *inode, int generation,
0900                     struct fuse_attr *attr)
0901 {
0902     return inode->i_generation != generation ||
0903         inode_wrong_type(inode, attr->mode);
0904 }
0905 
0906 static inline void fuse_make_bad(struct inode *inode)
0907 {
0908     remove_inode_hash(inode);
0909     set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state);
0910 }
0911 
0912 static inline bool fuse_is_bad(struct inode *inode)
0913 {
0914     return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state));
0915 }
0916 
0917 static inline struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
0918                          struct fuse_page_desc **desc)
0919 {
0920     struct page **pages;
0921 
0922     pages = kzalloc(npages * (sizeof(struct page *) +
0923                   sizeof(struct fuse_page_desc)), flags);
0924     *desc = (void *) (pages + npages);
0925 
0926     return pages;
0927 }
0928 
0929 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
0930                            unsigned int index,
0931                            unsigned int nr_pages)
0932 {
0933     int i;
0934 
0935     for (i = index; i < index + nr_pages; i++)
0936         descs[i].length = PAGE_SIZE - descs[i].offset;
0937 }
0938 
0939 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
0940 {
0941     /* Need RCU protection to prevent use after free after the decrement */
0942     rcu_read_lock();
0943     if (atomic_dec_and_test(&bucket->count))
0944         wake_up(&bucket->waitq);
0945     rcu_read_unlock();
0946 }
0947 
0948 /** Device operations */
0949 extern const struct file_operations fuse_dev_operations;
0950 
0951 extern const struct dentry_operations fuse_dentry_operations;
0952 extern const struct dentry_operations fuse_root_dentry_operations;
0953 
0954 /**
0955  * Get a filled in inode
0956  */
0957 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
0958             int generation, struct fuse_attr *attr,
0959             u64 attr_valid, u64 attr_version);
0960 
0961 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
0962              struct fuse_entry_out *outarg, struct inode **inode);
0963 
0964 /**
0965  * Send FORGET command
0966  */
0967 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
0968                u64 nodeid, u64 nlookup);
0969 
0970 struct fuse_forget_link *fuse_alloc_forget(void);
0971 
0972 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
0973                          unsigned int max,
0974                          unsigned int *countp);
0975 
0976 /*
0977  * Initialize READ or READDIR request
0978  */
0979 struct fuse_io_args {
0980     union {
0981         struct {
0982             struct fuse_read_in in;
0983             u64 attr_ver;
0984         } read;
0985         struct {
0986             struct fuse_write_in in;
0987             struct fuse_write_out out;
0988             bool page_locked;
0989         } write;
0990     };
0991     struct fuse_args_pages ap;
0992     struct fuse_io_priv *io;
0993     struct fuse_file *ff;
0994 };
0995 
0996 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
0997              size_t count, int opcode);
0998 
0999 
1000 /**
1001  * Send OPEN or OPENDIR request
1002  */
1003 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
1004 
1005 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm);
1006 void fuse_file_free(struct fuse_file *ff);
1007 void fuse_finish_open(struct inode *inode, struct file *file);
1008 
1009 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
1010                unsigned int flags);
1011 
1012 /**
1013  * Send RELEASE or RELEASEDIR request
1014  */
1015 void fuse_release_common(struct file *file, bool isdir);
1016 
1017 /**
1018  * Send FSYNC or FSYNCDIR request
1019  */
1020 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
1021               int datasync, int opcode);
1022 
1023 /**
1024  * Notify poll wakeup
1025  */
1026 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1027                 struct fuse_notify_poll_wakeup_out *outarg);
1028 
1029 /**
1030  * Initialize file operations on a regular file
1031  */
1032 void fuse_init_file_inode(struct inode *inode, unsigned int flags);
1033 
1034 /**
1035  * Initialize inode operations on regular files and special files
1036  */
1037 void fuse_init_common(struct inode *inode);
1038 
1039 /**
1040  * Initialize inode and file operations on a directory
1041  */
1042 void fuse_init_dir(struct inode *inode);
1043 
1044 /**
1045  * Initialize inode operations on a symlink
1046  */
1047 void fuse_init_symlink(struct inode *inode);
1048 
1049 /**
1050  * Change attributes of an inode
1051  */
1052 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
1053                 u64 attr_valid, u64 attr_version);
1054 
1055 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
1056                    u64 attr_valid, u32 cache_mask);
1057 
1058 u32 fuse_get_cache_mask(struct inode *inode);
1059 
1060 /**
1061  * Initialize the client device
1062  */
1063 int fuse_dev_init(void);
1064 
1065 /**
1066  * Cleanup the client device
1067  */
1068 void fuse_dev_cleanup(void);
1069 
1070 int fuse_ctl_init(void);
1071 void __exit fuse_ctl_cleanup(void);
1072 
1073 /**
1074  * Simple request sending that does request allocation and freeing
1075  */
1076 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args);
1077 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
1078                gfp_t gfp_flags);
1079 
1080 /**
1081  * End a finished request
1082  */
1083 void fuse_request_end(struct fuse_req *req);
1084 
1085 /* Abort all requests */
1086 void fuse_abort_conn(struct fuse_conn *fc);
1087 void fuse_wait_aborted(struct fuse_conn *fc);
1088 
1089 /**
1090  * Invalidate inode attributes
1091  */
1092 
1093 /* Attributes possibly changed on data modification */
1094 #define FUSE_STATX_MODIFY   (STATX_MTIME | STATX_CTIME | STATX_BLOCKS)
1095 
1096 /* Attributes possibly changed on data and/or size modification */
1097 #define FUSE_STATX_MODSIZE  (FUSE_STATX_MODIFY | STATX_SIZE)
1098 
1099 void fuse_invalidate_attr(struct inode *inode);
1100 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask);
1101 
1102 void fuse_invalidate_entry_cache(struct dentry *entry);
1103 
1104 void fuse_invalidate_atime(struct inode *inode);
1105 
1106 u64 entry_attr_timeout(struct fuse_entry_out *o);
1107 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o);
1108 
1109 /**
1110  * Acquire reference to fuse_conn
1111  */
1112 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
1113 
1114 /**
1115  * Initialize fuse_conn
1116  */
1117 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
1118             struct user_namespace *user_ns,
1119             const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv);
1120 
1121 /**
1122  * Release reference to fuse_conn
1123  */
1124 void fuse_conn_put(struct fuse_conn *fc);
1125 
1126 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc);
1127 struct fuse_dev *fuse_dev_alloc(void);
1128 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc);
1129 void fuse_dev_free(struct fuse_dev *fud);
1130 void fuse_send_init(struct fuse_mount *fm);
1131 
1132 /**
1133  * Fill in superblock and initialize fuse connection
1134  * @sb: partially-initialized superblock to fill in
1135  * @ctx: mount context
1136  */
1137 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx);
1138 
1139 /*
1140  * Remove the mount from the connection
1141  *
1142  * Returns whether this was the last mount
1143  */
1144 bool fuse_mount_remove(struct fuse_mount *fm);
1145 
1146 /*
1147  * Setup context ops for submounts
1148  */
1149 int fuse_init_fs_context_submount(struct fs_context *fsc);
1150 
1151 /*
1152  * Shut down the connection (possibly sending DESTROY request).
1153  */
1154 void fuse_conn_destroy(struct fuse_mount *fm);
1155 
1156 /* Drop the connection and free the fuse mount */
1157 void fuse_mount_destroy(struct fuse_mount *fm);
1158 
1159 /**
1160  * Add connection to control filesystem
1161  */
1162 int fuse_ctl_add_conn(struct fuse_conn *fc);
1163 
1164 /**
1165  * Remove connection from control filesystem
1166  */
1167 void fuse_ctl_remove_conn(struct fuse_conn *fc);
1168 
1169 /**
1170  * Is file type valid?
1171  */
1172 int fuse_valid_type(int m);
1173 
1174 bool fuse_invalid_attr(struct fuse_attr *attr);
1175 
1176 /**
1177  * Is current process allowed to perform filesystem operation?
1178  */
1179 int fuse_allow_current_process(struct fuse_conn *fc);
1180 
1181 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
1182 
1183 void fuse_flush_time_update(struct inode *inode);
1184 void fuse_update_ctime(struct inode *inode);
1185 
1186 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask);
1187 
1188 void fuse_flush_writepages(struct inode *inode);
1189 
1190 void fuse_set_nowrite(struct inode *inode);
1191 void fuse_release_nowrite(struct inode *inode);
1192 
1193 /**
1194  * Scan all fuse_mounts belonging to fc to find the first where
1195  * ilookup5() returns a result.  Return that result and the
1196  * respective fuse_mount in *fm (unless fm is NULL).
1197  *
1198  * The caller must hold fc->killsb.
1199  */
1200 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
1201                struct fuse_mount **fm);
1202 
1203 /**
1204  * File-system tells the kernel to invalidate cache for the given node id.
1205  */
1206 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
1207                  loff_t offset, loff_t len);
1208 
1209 /**
1210  * File-system tells the kernel to invalidate parent attributes and
1211  * the dentry matching parent/name.
1212  *
1213  * If the child_nodeid is non-zero and:
1214  *    - matches the inode number for the dentry matching parent/name,
1215  *    - is not a mount point
1216  *    - is a file or oan empty directory
1217  * then the dentry is unhashed (d_delete()).
1218  */
1219 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1220                  u64 child_nodeid, struct qstr *name);
1221 
1222 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
1223          bool isdir);
1224 
1225 /**
1226  * fuse_direct_io() flags
1227  */
1228 
1229 /** If set, it is WRITE; otherwise - READ */
1230 #define FUSE_DIO_WRITE (1 << 0)
1231 
1232 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */
1233 #define FUSE_DIO_CUSE  (1 << 1)
1234 
1235 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1236                loff_t *ppos, int flags);
1237 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1238            unsigned int flags);
1239 long fuse_ioctl_common(struct file *file, unsigned int cmd,
1240                unsigned long arg, unsigned int flags);
1241 __poll_t fuse_file_poll(struct file *file, poll_table *wait);
1242 int fuse_dev_release(struct inode *inode, struct file *file);
1243 
1244 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written);
1245 
1246 int fuse_flush_times(struct inode *inode, struct fuse_file *ff);
1247 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc);
1248 
1249 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1250             struct file *file);
1251 
1252 void fuse_set_initialized(struct fuse_conn *fc);
1253 
1254 void fuse_unlock_inode(struct inode *inode, bool locked);
1255 bool fuse_lock_inode(struct inode *inode);
1256 
1257 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
1258           size_t size, int flags, unsigned int extra_flags);
1259 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
1260               size_t size);
1261 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size);
1262 int fuse_removexattr(struct inode *inode, const char *name);
1263 extern const struct xattr_handler *fuse_xattr_handlers[];
1264 extern const struct xattr_handler *fuse_acl_xattr_handlers[];
1265 extern const struct xattr_handler *fuse_no_acl_xattr_handlers[];
1266 
1267 struct posix_acl;
1268 struct posix_acl *fuse_get_acl(struct inode *inode, int type, bool rcu);
1269 int fuse_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
1270          struct posix_acl *acl, int type);
1271 
1272 /* readdir.c */
1273 int fuse_readdir(struct file *file, struct dir_context *ctx);
1274 
1275 /**
1276  * Return the number of bytes in an arguments list
1277  */
1278 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
1279 
1280 /**
1281  * Get the next unique ID for a request
1282  */
1283 u64 fuse_get_unique(struct fuse_iqueue *fiq);
1284 void fuse_free_conn(struct fuse_conn *fc);
1285 
1286 /* dax.c */
1287 
1288 #define FUSE_IS_DAX(inode) (IS_ENABLED(CONFIG_FUSE_DAX) && IS_DAX(inode))
1289 
1290 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to);
1291 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from);
1292 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma);
1293 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end);
1294 int fuse_dax_conn_alloc(struct fuse_conn *fc, enum fuse_dax_mode mode,
1295             struct dax_device *dax_dev);
1296 void fuse_dax_conn_free(struct fuse_conn *fc);
1297 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi);
1298 void fuse_dax_inode_init(struct inode *inode, unsigned int flags);
1299 void fuse_dax_inode_cleanup(struct inode *inode);
1300 void fuse_dax_dontcache(struct inode *inode, unsigned int flags);
1301 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment);
1302 void fuse_dax_cancel_work(struct fuse_conn *fc);
1303 
1304 /* ioctl.c */
1305 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1306 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1307                 unsigned long arg);
1308 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1309 int fuse_fileattr_set(struct user_namespace *mnt_userns,
1310               struct dentry *dentry, struct fileattr *fa);
1311 
1312 /* file.c */
1313 
1314 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1315                  unsigned int open_flags, bool isdir);
1316 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1317                unsigned int open_flags, fl_owner_t id, bool isdir);
1318 
1319 #endif /* _FS_FUSE_I_H */