LCOV - code coverage report
Current view: top level - fs/fuse - inode.c (source / functions) Hit Total Coverage
Test: Real Lines: 375 677 55.4 %
Date: 2020-10-17 15:46:16 Functions: 0 55 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :   FUSE: Filesystem in Userspace
       3                 :            :   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
       4                 :            : 
       5                 :            :   This program can be distributed under the terms of the GNU GPL.
       6                 :            :   See the file COPYING.
       7                 :            : */
       8                 :            : 
       9                 :            : #include "fuse_i.h"
      10                 :            : 
      11                 :            : #include <linux/pagemap.h>
      12                 :            : #include <linux/slab.h>
      13                 :            : #include <linux/file.h>
      14                 :            : #include <linux/seq_file.h>
      15                 :            : #include <linux/init.h>
      16                 :            : #include <linux/module.h>
      17                 :            : #include <linux/moduleparam.h>
      18                 :            : #include <linux/fs_context.h>
      19                 :            : #include <linux/fs_parser.h>
      20                 :            : #include <linux/statfs.h>
      21                 :            : #include <linux/random.h>
      22                 :            : #include <linux/sched.h>
      23                 :            : #include <linux/exportfs.h>
      24                 :            : #include <linux/posix_acl.h>
      25                 :            : #include <linux/pid_namespace.h>
      26                 :            : 
      27                 :            : MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
      28                 :            : MODULE_DESCRIPTION("Filesystem in Userspace");
      29                 :            : MODULE_LICENSE("GPL");
      30                 :            : 
      31                 :            : static struct kmem_cache *fuse_inode_cachep;
      32                 :            : struct list_head fuse_conn_list;
      33                 :            : DEFINE_MUTEX(fuse_mutex);
      34                 :            : 
      35                 :            : static int set_global_limit(const char *val, const struct kernel_param *kp);
      36                 :            : 
      37                 :            : unsigned max_user_bgreq;
      38                 :            : module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
      39                 :            :                   &max_user_bgreq, 0644);
      40                 :            : __MODULE_PARM_TYPE(max_user_bgreq, "uint");
      41                 :            : MODULE_PARM_DESC(max_user_bgreq,
      42                 :            :  "Global limit for the maximum number of backgrounded requests an "
      43                 :            :  "unprivileged user can set");
      44                 :            : 
      45                 :            : unsigned max_user_congthresh;
      46                 :            : module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
      47                 :            :                   &max_user_congthresh, 0644);
      48                 :            : __MODULE_PARM_TYPE(max_user_congthresh, "uint");
      49                 :            : MODULE_PARM_DESC(max_user_congthresh,
      50                 :            :  "Global limit for the maximum congestion threshold an "
      51                 :            :  "unprivileged user can set");
      52                 :            : 
      53                 :            : #define FUSE_SUPER_MAGIC 0x65735546
      54                 :            : 
      55                 :            : #define FUSE_DEFAULT_BLKSIZE 512
      56                 :            : 
      57                 :            : /** Maximum number of outstanding background requests */
      58                 :            : #define FUSE_DEFAULT_MAX_BACKGROUND 12
      59                 :            : 
      60                 :            : /** Congestion starts at 75% of maximum */
      61                 :            : #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
      62                 :            : 
      63                 :            : #ifdef CONFIG_BLOCK
      64                 :            : static struct file_system_type fuseblk_fs_type;
      65                 :            : #endif
      66                 :            : 
      67                 :          3 : struct fuse_forget_link *fuse_alloc_forget(void)
      68                 :            : {
      69                 :          3 :         return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
      70                 :            : }
      71                 :            : 
      72                 :          3 : static struct inode *fuse_alloc_inode(struct super_block *sb)
      73                 :            : {
      74                 :            :         struct fuse_inode *fi;
      75                 :            : 
      76                 :          3 :         fi = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
      77                 :          3 :         if (!fi)
      78                 :            :                 return NULL;
      79                 :            : 
      80                 :          3 :         fi->i_time = 0;
      81                 :          3 :         fi->inval_mask = 0;
      82                 :          3 :         fi->nodeid = 0;
      83                 :          3 :         fi->nlookup = 0;
      84                 :          3 :         fi->attr_version = 0;
      85                 :          3 :         fi->orig_ino = 0;
      86                 :          3 :         fi->state = 0;
      87                 :          3 :         mutex_init(&fi->mutex);
      88                 :          3 :         spin_lock_init(&fi->lock);
      89                 :          3 :         fi->forget = fuse_alloc_forget();
      90                 :          3 :         if (!fi->forget) {
      91                 :          0 :                 kmem_cache_free(fuse_inode_cachep, fi);
      92                 :          0 :                 return NULL;
      93                 :            :         }
      94                 :            : 
      95                 :          3 :         return &fi->inode;
      96                 :            : }
      97                 :            : 
      98                 :          0 : static void fuse_free_inode(struct inode *inode)
      99                 :            : {
     100                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
     101                 :            : 
     102                 :            :         mutex_destroy(&fi->mutex);
     103                 :          0 :         kfree(fi->forget);
     104                 :          0 :         kmem_cache_free(fuse_inode_cachep, fi);
     105                 :          0 : }
     106                 :            : 
     107                 :          0 : static void fuse_evict_inode(struct inode *inode)
     108                 :            : {
     109                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
     110                 :            : 
     111                 :          0 :         truncate_inode_pages_final(&inode->i_data);
     112                 :          0 :         clear_inode(inode);
     113                 :          0 :         if (inode->i_sb->s_flags & SB_ACTIVE) {
     114                 :            :                 struct fuse_conn *fc = get_fuse_conn(inode);
     115                 :          0 :                 fuse_queue_forget(fc, fi->forget, fi->nodeid, fi->nlookup);
     116                 :          0 :                 fi->forget = NULL;
     117                 :            :         }
     118                 :          0 :         if (S_ISREG(inode->i_mode) && !is_bad_inode(inode)) {
     119                 :          0 :                 WARN_ON(!list_empty(&fi->write_files));
     120                 :          0 :                 WARN_ON(!list_empty(&fi->queued_writes));
     121                 :            :         }
     122                 :          0 : }
     123                 :            : 
     124                 :          0 : static int fuse_reconfigure(struct fs_context *fc)
     125                 :            : {
     126                 :          0 :         struct super_block *sb = fc->root->d_sb;
     127                 :            : 
     128                 :          0 :         sync_filesystem(sb);
     129                 :          0 :         if (fc->sb_flags & SB_MANDLOCK)
     130                 :            :                 return -EINVAL;
     131                 :            : 
     132                 :          0 :         return 0;
     133                 :            : }
     134                 :            : 
     135                 :            : /*
     136                 :            :  * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
     137                 :            :  * so that it will fit.
     138                 :            :  */
     139                 :            : static ino_t fuse_squash_ino(u64 ino64)
     140                 :            : {
     141                 :          3 :         ino_t ino = (ino_t) ino64;
     142                 :            :         if (sizeof(ino_t) < sizeof(u64))
     143                 :          3 :                 ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
     144                 :            :         return ino;
     145                 :            : }
     146                 :            : 
     147                 :          3 : void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
     148                 :            :                                    u64 attr_valid)
     149                 :            : {
     150                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
     151                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
     152                 :            : 
     153                 :            :         lockdep_assert_held(&fi->lock);
     154                 :            : 
     155                 :          3 :         fi->attr_version = atomic64_inc_return(&fc->attr_version);
     156                 :          3 :         fi->i_time = attr_valid;
     157                 :            :         WRITE_ONCE(fi->inval_mask, 0);
     158                 :            : 
     159                 :          3 :         inode->i_ino     = fuse_squash_ino(attr->ino);
     160                 :          3 :         inode->i_mode    = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
     161                 :          3 :         set_nlink(inode, attr->nlink);
     162                 :          3 :         inode->i_uid     = make_kuid(fc->user_ns, attr->uid);
     163                 :          3 :         inode->i_gid     = make_kgid(fc->user_ns, attr->gid);
     164                 :          3 :         inode->i_blocks  = attr->blocks;
     165                 :          3 :         inode->i_atime.tv_sec   = attr->atime;
     166                 :          3 :         inode->i_atime.tv_nsec  = attr->atimensec;
     167                 :            :         /* mtime from server may be stale due to local buffered write */
     168                 :          3 :         if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) {
     169                 :          3 :                 inode->i_mtime.tv_sec   = attr->mtime;
     170                 :          3 :                 inode->i_mtime.tv_nsec  = attr->mtimensec;
     171                 :          3 :                 inode->i_ctime.tv_sec   = attr->ctime;
     172                 :          3 :                 inode->i_ctime.tv_nsec  = attr->ctimensec;
     173                 :            :         }
     174                 :            : 
     175                 :          3 :         if (attr->blksize != 0)
     176                 :          0 :                 inode->i_blkbits = ilog2(attr->blksize);
     177                 :            :         else
     178                 :          3 :                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
     179                 :            : 
     180                 :            :         /*
     181                 :            :          * Don't set the sticky bit in i_mode, unless we want the VFS
     182                 :            :          * to check permissions.  This prevents failures due to the
     183                 :            :          * check in may_delete().
     184                 :            :          */
     185                 :          3 :         fi->orig_i_mode = inode->i_mode;
     186                 :          3 :         if (!fc->default_permissions)
     187                 :          3 :                 inode->i_mode &= ~S_ISVTX;
     188                 :            : 
     189                 :          3 :         fi->orig_ino = attr->ino;
     190                 :          3 : }
     191                 :            : 
     192                 :          3 : void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
     193                 :            :                             u64 attr_valid, u64 attr_version)
     194                 :            : {
     195                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
     196                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
     197                 :          3 :         bool is_wb = fc->writeback_cache;
     198                 :            :         loff_t oldsize;
     199                 :            :         struct timespec64 old_mtime;
     200                 :            : 
     201                 :            :         spin_lock(&fi->lock);
     202                 :          3 :         if ((attr_version != 0 && fi->attr_version > attr_version) ||
     203                 :            :             test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
     204                 :            :                 spin_unlock(&fi->lock);
     205                 :          3 :                 return;
     206                 :            :         }
     207                 :            : 
     208                 :          3 :         old_mtime = inode->i_mtime;
     209                 :          3 :         fuse_change_attributes_common(inode, attr, attr_valid);
     210                 :            : 
     211                 :          3 :         oldsize = inode->i_size;
     212                 :            :         /*
     213                 :            :          * In case of writeback_cache enabled, the cached writes beyond EOF
     214                 :            :          * extend local i_size without keeping userspace server in sync. So,
     215                 :            :          * attr->size coming from server can be stale. We cannot trust it.
     216                 :            :          */
     217                 :          3 :         if (!is_wb || !S_ISREG(inode->i_mode))
     218                 :          3 :                 i_size_write(inode, attr->size);
     219                 :            :         spin_unlock(&fi->lock);
     220                 :            : 
     221                 :          3 :         if (!is_wb && S_ISREG(inode->i_mode)) {
     222                 :            :                 bool inval = false;
     223                 :            : 
     224                 :          0 :                 if (oldsize != attr->size) {
     225                 :          0 :                         truncate_pagecache(inode, attr->size);
     226                 :          0 :                         if (!fc->explicit_inval_data)
     227                 :            :                                 inval = true;
     228                 :          0 :                 } else if (fc->auto_inval_data) {
     229                 :          0 :                         struct timespec64 new_mtime = {
     230                 :          0 :                                 .tv_sec = attr->mtime,
     231                 :          0 :                                 .tv_nsec = attr->mtimensec,
     232                 :            :                         };
     233                 :            : 
     234                 :            :                         /*
     235                 :            :                          * Auto inval mode also checks and invalidates if mtime
     236                 :            :                          * has changed.
     237                 :            :                          */
     238                 :          0 :                         if (!timespec64_equal(&old_mtime, &new_mtime))
     239                 :            :                                 inval = true;
     240                 :            :                 }
     241                 :            : 
     242                 :          0 :                 if (inval)
     243                 :          0 :                         invalidate_inode_pages2(inode->i_mapping);
     244                 :            :         }
     245                 :            : }
     246                 :            : 
     247                 :          3 : static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
     248                 :            : {
     249                 :          3 :         inode->i_mode = attr->mode & S_IFMT;
     250                 :          3 :         inode->i_size = attr->size;
     251                 :          3 :         inode->i_mtime.tv_sec  = attr->mtime;
     252                 :          3 :         inode->i_mtime.tv_nsec = attr->mtimensec;
     253                 :          3 :         inode->i_ctime.tv_sec  = attr->ctime;
     254                 :          3 :         inode->i_ctime.tv_nsec = attr->ctimensec;
     255                 :          3 :         if (S_ISREG(inode->i_mode)) {
     256                 :          0 :                 fuse_init_common(inode);
     257                 :          0 :                 fuse_init_file_inode(inode);
     258                 :          3 :         } else if (S_ISDIR(inode->i_mode))
     259                 :          3 :                 fuse_init_dir(inode);
     260                 :          0 :         else if (S_ISLNK(inode->i_mode))
     261                 :          0 :                 fuse_init_symlink(inode);
     262                 :          0 :         else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
     263                 :          0 :                  S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
     264                 :          0 :                 fuse_init_common(inode);
     265                 :          0 :                 init_special_inode(inode, inode->i_mode,
     266                 :            :                                    new_decode_dev(attr->rdev));
     267                 :            :         } else
     268                 :          0 :                 BUG();
     269                 :          3 : }
     270                 :            : 
     271                 :          0 : int fuse_inode_eq(struct inode *inode, void *_nodeidp)
     272                 :            : {
     273                 :          0 :         u64 nodeid = *(u64 *) _nodeidp;
     274                 :          0 :         if (get_node_id(inode) == nodeid)
     275                 :            :                 return 1;
     276                 :            :         else
     277                 :          0 :                 return 0;
     278                 :            : }
     279                 :            : 
     280                 :          3 : static int fuse_inode_set(struct inode *inode, void *_nodeidp)
     281                 :            : {
     282                 :          3 :         u64 nodeid = *(u64 *) _nodeidp;
     283                 :          3 :         get_fuse_inode(inode)->nodeid = nodeid;
     284                 :          3 :         return 0;
     285                 :            : }
     286                 :            : 
     287                 :          3 : struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
     288                 :            :                         int generation, struct fuse_attr *attr,
     289                 :            :                         u64 attr_valid, u64 attr_version)
     290                 :            : {
     291                 :            :         struct inode *inode;
     292                 :            :         struct fuse_inode *fi;
     293                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     294                 :            : 
     295                 :            :  retry:
     296                 :          3 :         inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
     297                 :          3 :         if (!inode)
     298                 :            :                 return NULL;
     299                 :            : 
     300                 :          3 :         if ((inode->i_state & I_NEW)) {
     301                 :          3 :                 inode->i_flags |= S_NOATIME;
     302                 :          3 :                 if (!fc->writeback_cache || !S_ISREG(attr->mode))
     303                 :          3 :                         inode->i_flags |= S_NOCMTIME;
     304                 :          3 :                 inode->i_generation = generation;
     305                 :          3 :                 fuse_init_inode(inode, attr);
     306                 :          3 :                 unlock_new_inode(inode);
     307                 :          0 :         } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
     308                 :            :                 /* Inode has changed type, any I/O on the old should fail */
     309                 :          0 :                 make_bad_inode(inode);
     310                 :          0 :                 iput(inode);
     311                 :          0 :                 goto retry;
     312                 :            :         }
     313                 :            : 
     314                 :            :         fi = get_fuse_inode(inode);
     315                 :            :         spin_lock(&fi->lock);
     316                 :          3 :         fi->nlookup++;
     317                 :            :         spin_unlock(&fi->lock);
     318                 :          3 :         fuse_change_attributes(inode, attr, attr_valid, attr_version);
     319                 :            : 
     320                 :          3 :         return inode;
     321                 :            : }
     322                 :            : 
     323                 :          0 : int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
     324                 :            :                              loff_t offset, loff_t len)
     325                 :            : {
     326                 :            :         struct inode *inode;
     327                 :            :         pgoff_t pg_start;
     328                 :            :         pgoff_t pg_end;
     329                 :            : 
     330                 :          0 :         inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
     331                 :          0 :         if (!inode)
     332                 :            :                 return -ENOENT;
     333                 :            : 
     334                 :          0 :         fuse_invalidate_attr(inode);
     335                 :          0 :         forget_all_cached_acls(inode);
     336                 :          0 :         if (offset >= 0) {
     337                 :          0 :                 pg_start = offset >> PAGE_SHIFT;
     338                 :          0 :                 if (len <= 0)
     339                 :            :                         pg_end = -1;
     340                 :            :                 else
     341                 :          0 :                         pg_end = (offset + len - 1) >> PAGE_SHIFT;
     342                 :          0 :                 invalidate_inode_pages2_range(inode->i_mapping,
     343                 :            :                                               pg_start, pg_end);
     344                 :            :         }
     345                 :          0 :         iput(inode);
     346                 :          0 :         return 0;
     347                 :            : }
     348                 :            : 
     349                 :          3 : bool fuse_lock_inode(struct inode *inode)
     350                 :            : {
     351                 :            :         bool locked = false;
     352                 :            : 
     353                 :          3 :         if (!get_fuse_conn(inode)->parallel_dirops) {
     354                 :          3 :                 mutex_lock(&get_fuse_inode(inode)->mutex);
     355                 :            :                 locked = true;
     356                 :            :         }
     357                 :            : 
     358                 :          3 :         return locked;
     359                 :            : }
     360                 :            : 
     361                 :          3 : void fuse_unlock_inode(struct inode *inode, bool locked)
     362                 :            : {
     363                 :          3 :         if (locked)
     364                 :          3 :                 mutex_unlock(&get_fuse_inode(inode)->mutex);
     365                 :          3 : }
     366                 :            : 
     367                 :          0 : static void fuse_umount_begin(struct super_block *sb)
     368                 :            : {
     369                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     370                 :            : 
     371                 :          0 :         if (!fc->no_force_umount)
     372                 :          0 :                 fuse_abort_conn(fc);
     373                 :          0 : }
     374                 :            : 
     375                 :          0 : static void fuse_send_destroy(struct fuse_conn *fc)
     376                 :            : {
     377                 :          0 :         if (fc->conn_init) {
     378                 :          0 :                 FUSE_ARGS(args);
     379                 :            : 
     380                 :          0 :                 args.opcode = FUSE_DESTROY;
     381                 :          0 :                 args.force = true;
     382                 :          0 :                 args.nocreds = true;
     383                 :          0 :                 fuse_simple_request(fc, &args);
     384                 :            :         }
     385                 :          0 : }
     386                 :            : 
     387                 :          0 : static void fuse_put_super(struct super_block *sb)
     388                 :            : {
     389                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     390                 :            : 
     391                 :          0 :         mutex_lock(&fuse_mutex);
     392                 :            :         list_del(&fc->entry);
     393                 :          0 :         fuse_ctl_remove_conn(fc);
     394                 :          0 :         mutex_unlock(&fuse_mutex);
     395                 :            : 
     396                 :          0 :         fuse_conn_put(fc);
     397                 :          0 : }
     398                 :            : 
     399                 :            : static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
     400                 :            : {
     401                 :          0 :         stbuf->f_type    = FUSE_SUPER_MAGIC;
     402                 :          0 :         stbuf->f_bsize   = attr->bsize;
     403                 :          0 :         stbuf->f_frsize  = attr->frsize;
     404                 :          0 :         stbuf->f_blocks  = attr->blocks;
     405                 :          0 :         stbuf->f_bfree   = attr->bfree;
     406                 :          0 :         stbuf->f_bavail  = attr->bavail;
     407                 :          0 :         stbuf->f_files   = attr->files;
     408                 :          0 :         stbuf->f_ffree   = attr->ffree;
     409                 :          0 :         stbuf->f_namelen = attr->namelen;
     410                 :            :         /* fsid is left zero */
     411                 :            : }
     412                 :            : 
     413                 :          0 : static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
     414                 :            : {
     415                 :          0 :         struct super_block *sb = dentry->d_sb;
     416                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     417                 :          0 :         FUSE_ARGS(args);
     418                 :            :         struct fuse_statfs_out outarg;
     419                 :            :         int err;
     420                 :            : 
     421                 :          0 :         if (!fuse_allow_current_process(fc)) {
     422                 :          0 :                 buf->f_type = FUSE_SUPER_MAGIC;
     423                 :          0 :                 return 0;
     424                 :            :         }
     425                 :            : 
     426                 :          0 :         memset(&outarg, 0, sizeof(outarg));
     427                 :          0 :         args.in_numargs = 0;
     428                 :          0 :         args.opcode = FUSE_STATFS;
     429                 :          0 :         args.nodeid = get_node_id(d_inode(dentry));
     430                 :          0 :         args.out_numargs = 1;
     431                 :          0 :         args.out_args[0].size = sizeof(outarg);
     432                 :          0 :         args.out_args[0].value = &outarg;
     433                 :          0 :         err = fuse_simple_request(fc, &args);
     434                 :          0 :         if (!err)
     435                 :            :                 convert_fuse_statfs(buf, &outarg.st);
     436                 :          0 :         return err;
     437                 :            : }
     438                 :            : 
     439                 :            : enum {
     440                 :            :         OPT_SOURCE,
     441                 :            :         OPT_SUBTYPE,
     442                 :            :         OPT_FD,
     443                 :            :         OPT_ROOTMODE,
     444                 :            :         OPT_USER_ID,
     445                 :            :         OPT_GROUP_ID,
     446                 :            :         OPT_DEFAULT_PERMISSIONS,
     447                 :            :         OPT_ALLOW_OTHER,
     448                 :            :         OPT_MAX_READ,
     449                 :            :         OPT_BLKSIZE,
     450                 :            :         OPT_ERR
     451                 :            : };
     452                 :            : 
     453                 :            : static const struct fs_parameter_spec fuse_param_specs[] = {
     454                 :            :         fsparam_string  ("source",            OPT_SOURCE),
     455                 :            :         fsparam_u32     ("fd",                        OPT_FD),
     456                 :            :         fsparam_u32oct  ("rootmode",          OPT_ROOTMODE),
     457                 :            :         fsparam_u32     ("user_id",           OPT_USER_ID),
     458                 :            :         fsparam_u32     ("group_id",          OPT_GROUP_ID),
     459                 :            :         fsparam_flag    ("default_permissions",       OPT_DEFAULT_PERMISSIONS),
     460                 :            :         fsparam_flag    ("allow_other",               OPT_ALLOW_OTHER),
     461                 :            :         fsparam_u32     ("max_read",          OPT_MAX_READ),
     462                 :            :         fsparam_u32     ("blksize",           OPT_BLKSIZE),
     463                 :            :         fsparam_string  ("subtype",           OPT_SUBTYPE),
     464                 :            :         {}
     465                 :            : };
     466                 :            : 
     467                 :            : static const struct fs_parameter_description fuse_fs_parameters = {
     468                 :            :         .name           = "fuse",
     469                 :            :         .specs          = fuse_param_specs,
     470                 :            : };
     471                 :            : 
     472                 :          3 : static int fuse_parse_param(struct fs_context *fc, struct fs_parameter *param)
     473                 :            : {
     474                 :            :         struct fs_parse_result result;
     475                 :          3 :         struct fuse_fs_context *ctx = fc->fs_private;
     476                 :            :         int opt;
     477                 :            : 
     478                 :            :         /*
     479                 :            :          * Ignore options coming from mount(MS_REMOUNT) for backward
     480                 :            :          * compatibility.
     481                 :            :          */
     482                 :          3 :         if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
     483                 :            :                 return 0;
     484                 :            : 
     485                 :          3 :         opt = fs_parse(fc, &fuse_fs_parameters, param, &result);
     486                 :          3 :         if (opt < 0)
     487                 :            :                 return opt;
     488                 :            : 
     489                 :          3 :         switch (opt) {
     490                 :            :         case OPT_SOURCE:
     491                 :          3 :                 if (fc->source)
     492                 :          0 :                         return invalf(fc, "fuse: Multiple sources specified");
     493                 :          3 :                 fc->source = param->string;
     494                 :          3 :                 param->string = NULL;
     495                 :          3 :                 break;
     496                 :            : 
     497                 :            :         case OPT_SUBTYPE:
     498                 :          3 :                 if (ctx->subtype)
     499                 :          0 :                         return invalf(fc, "fuse: Multiple subtypes specified");
     500                 :          3 :                 ctx->subtype = param->string;
     501                 :          3 :                 param->string = NULL;
     502                 :          3 :                 return 0;
     503                 :            : 
     504                 :            :         case OPT_FD:
     505                 :          3 :                 ctx->fd = result.uint_32;
     506                 :          3 :                 ctx->fd_present = 1;
     507                 :          3 :                 break;
     508                 :            : 
     509                 :            :         case OPT_ROOTMODE:
     510                 :          3 :                 if (!fuse_valid_type(result.uint_32))
     511                 :          0 :                         return invalf(fc, "fuse: Invalid rootmode");
     512                 :          3 :                 ctx->rootmode = result.uint_32;
     513                 :          3 :                 ctx->rootmode_present = 1;
     514                 :          3 :                 break;
     515                 :            : 
     516                 :            :         case OPT_USER_ID:
     517                 :          3 :                 ctx->user_id = make_kuid(fc->user_ns, result.uint_32);
     518                 :          3 :                 if (!uid_valid(ctx->user_id))
     519                 :          0 :                         return invalf(fc, "fuse: Invalid user_id");
     520                 :          3 :                 ctx->user_id_present = 1;
     521                 :          3 :                 break;
     522                 :            : 
     523                 :            :         case OPT_GROUP_ID:
     524                 :          3 :                 ctx->group_id = make_kgid(fc->user_ns, result.uint_32);
     525                 :          3 :                 if (!gid_valid(ctx->group_id))
     526                 :          0 :                         return invalf(fc, "fuse: Invalid group_id");
     527                 :          3 :                 ctx->group_id_present = 1;
     528                 :          3 :                 break;
     529                 :            : 
     530                 :            :         case OPT_DEFAULT_PERMISSIONS:
     531                 :          0 :                 ctx->default_permissions = 1;
     532                 :          0 :                 break;
     533                 :            : 
     534                 :            :         case OPT_ALLOW_OTHER:
     535                 :          0 :                 ctx->allow_other = 1;
     536                 :          0 :                 break;
     537                 :            : 
     538                 :            :         case OPT_MAX_READ:
     539                 :          0 :                 ctx->max_read = result.uint_32;
     540                 :          0 :                 break;
     541                 :            : 
     542                 :            :         case OPT_BLKSIZE:
     543                 :          0 :                 if (!ctx->is_bdev)
     544                 :          0 :                         return invalf(fc, "fuse: blksize only supported for fuseblk");
     545                 :          0 :                 ctx->blksize = result.uint_32;
     546                 :          0 :                 break;
     547                 :            : 
     548                 :            :         default:
     549                 :            :                 return -EINVAL;
     550                 :            :         }
     551                 :            : 
     552                 :            :         return 0;
     553                 :            : }
     554                 :            : 
     555                 :          3 : static void fuse_free_fc(struct fs_context *fc)
     556                 :            : {
     557                 :          3 :         struct fuse_fs_context *ctx = fc->fs_private;
     558                 :            : 
     559                 :          3 :         if (ctx) {
     560                 :          3 :                 kfree(ctx->subtype);
     561                 :          3 :                 kfree(ctx);
     562                 :            :         }
     563                 :          3 : }
     564                 :            : 
     565                 :          3 : static int fuse_show_options(struct seq_file *m, struct dentry *root)
     566                 :            : {
     567                 :          3 :         struct super_block *sb = root->d_sb;
     568                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     569                 :            : 
     570                 :          3 :         if (fc->no_mount_options)
     571                 :            :                 return 0;
     572                 :            : 
     573                 :          3 :         seq_printf(m, ",user_id=%u", from_kuid_munged(fc->user_ns, fc->user_id));
     574                 :          3 :         seq_printf(m, ",group_id=%u", from_kgid_munged(fc->user_ns, fc->group_id));
     575                 :          3 :         if (fc->default_permissions)
     576                 :          0 :                 seq_puts(m, ",default_permissions");
     577                 :          3 :         if (fc->allow_other)
     578                 :          0 :                 seq_puts(m, ",allow_other");
     579                 :          3 :         if (fc->max_read != ~0)
     580                 :          0 :                 seq_printf(m, ",max_read=%u", fc->max_read);
     581                 :          3 :         if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
     582                 :          0 :                 seq_printf(m, ",blksize=%lu", sb->s_blocksize);
     583                 :            :         return 0;
     584                 :            : }
     585                 :            : 
     586                 :          3 : static void fuse_iqueue_init(struct fuse_iqueue *fiq,
     587                 :            :                              const struct fuse_iqueue_ops *ops,
     588                 :            :                              void *priv)
     589                 :            : {
     590                 :          3 :         memset(fiq, 0, sizeof(struct fuse_iqueue));
     591                 :          3 :         spin_lock_init(&fiq->lock);
     592                 :          3 :         init_waitqueue_head(&fiq->waitq);
     593                 :          3 :         INIT_LIST_HEAD(&fiq->pending);
     594                 :          3 :         INIT_LIST_HEAD(&fiq->interrupts);
     595                 :          3 :         fiq->forget_list_tail = &fiq->forget_list_head;
     596                 :          3 :         fiq->connected = 1;
     597                 :          3 :         fiq->ops = ops;
     598                 :          3 :         fiq->priv = priv;
     599                 :          3 : }
     600                 :            : 
     601                 :            : static void fuse_pqueue_init(struct fuse_pqueue *fpq)
     602                 :            : {
     603                 :            :         unsigned int i;
     604                 :            : 
     605                 :          3 :         spin_lock_init(&fpq->lock);
     606                 :          3 :         for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
     607                 :          3 :                 INIT_LIST_HEAD(&fpq->processing[i]);
     608                 :          3 :         INIT_LIST_HEAD(&fpq->io);
     609                 :          3 :         fpq->connected = 1;
     610                 :            : }
     611                 :            : 
     612                 :          3 : void fuse_conn_init(struct fuse_conn *fc, struct user_namespace *user_ns,
     613                 :            :                     const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
     614                 :            : {
     615                 :          3 :         memset(fc, 0, sizeof(*fc));
     616                 :          3 :         spin_lock_init(&fc->lock);
     617                 :          3 :         spin_lock_init(&fc->bg_lock);
     618                 :          3 :         init_rwsem(&fc->killsb);
     619                 :            :         refcount_set(&fc->count, 1);
     620                 :            :         atomic_set(&fc->dev_count, 1);
     621                 :          3 :         init_waitqueue_head(&fc->blocked_waitq);
     622                 :          3 :         fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
     623                 :          3 :         INIT_LIST_HEAD(&fc->bg_queue);
     624                 :          3 :         INIT_LIST_HEAD(&fc->entry);
     625                 :          3 :         INIT_LIST_HEAD(&fc->devices);
     626                 :            :         atomic_set(&fc->num_waiting, 0);
     627                 :          3 :         fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
     628                 :          3 :         fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
     629                 :          3 :         atomic64_set(&fc->khctr, 0);
     630                 :          3 :         fc->polled_files = RB_ROOT;
     631                 :          3 :         fc->blocked = 0;
     632                 :          3 :         fc->initialized = 0;
     633                 :          3 :         fc->connected = 1;
     634                 :          3 :         atomic64_set(&fc->attr_version, 1);
     635                 :          3 :         get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
     636                 :          3 :         fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
     637                 :          3 :         fc->user_ns = get_user_ns(user_ns);
     638                 :          3 :         fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
     639                 :          3 : }
     640                 :            : EXPORT_SYMBOL_GPL(fuse_conn_init);
     641                 :            : 
     642                 :          0 : void fuse_conn_put(struct fuse_conn *fc)
     643                 :            : {
     644                 :          0 :         if (refcount_dec_and_test(&fc->count)) {
     645                 :          0 :                 struct fuse_iqueue *fiq = &fc->iq;
     646                 :            : 
     647                 :          0 :                 if (fiq->ops->release)
     648                 :          0 :                         fiq->ops->release(fiq);
     649                 :          0 :                 put_pid_ns(fc->pid_ns);
     650                 :          0 :                 put_user_ns(fc->user_ns);
     651                 :          0 :                 fc->release(fc);
     652                 :            :         }
     653                 :          0 : }
     654                 :            : EXPORT_SYMBOL_GPL(fuse_conn_put);
     655                 :            : 
     656                 :          0 : struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
     657                 :            : {
     658                 :          3 :         refcount_inc(&fc->count);
     659                 :          0 :         return fc;
     660                 :            : }
     661                 :            : EXPORT_SYMBOL_GPL(fuse_conn_get);
     662                 :            : 
     663                 :          3 : static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
     664                 :            : {
     665                 :            :         struct fuse_attr attr;
     666                 :          3 :         memset(&attr, 0, sizeof(attr));
     667                 :            : 
     668                 :          3 :         attr.mode = mode;
     669                 :          3 :         attr.ino = FUSE_ROOT_ID;
     670                 :          3 :         attr.nlink = 1;
     671                 :          3 :         return fuse_iget(sb, 1, 0, &attr, 0, 0);
     672                 :            : }
     673                 :            : 
     674                 :            : struct fuse_inode_handle {
     675                 :            :         u64 nodeid;
     676                 :            :         u32 generation;
     677                 :            : };
     678                 :            : 
     679                 :          0 : static struct dentry *fuse_get_dentry(struct super_block *sb,
     680                 :            :                                       struct fuse_inode_handle *handle)
     681                 :            : {
     682                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     683                 :            :         struct inode *inode;
     684                 :            :         struct dentry *entry;
     685                 :            :         int err = -ESTALE;
     686                 :            : 
     687                 :          0 :         if (handle->nodeid == 0)
     688                 :            :                 goto out_err;
     689                 :            : 
     690                 :          0 :         inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
     691                 :          0 :         if (!inode) {
     692                 :            :                 struct fuse_entry_out outarg;
     693                 :          0 :                 const struct qstr name = QSTR_INIT(".", 1);
     694                 :            : 
     695                 :          0 :                 if (!fc->export_support)
     696                 :            :                         goto out_err;
     697                 :            : 
     698                 :          0 :                 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
     699                 :            :                                        &inode);
     700                 :          0 :                 if (err && err != -ENOENT)
     701                 :            :                         goto out_err;
     702                 :          0 :                 if (err || !inode) {
     703                 :            :                         err = -ESTALE;
     704                 :            :                         goto out_err;
     705                 :            :                 }
     706                 :            :                 err = -EIO;
     707                 :          0 :                 if (get_node_id(inode) != handle->nodeid)
     708                 :            :                         goto out_iput;
     709                 :            :         }
     710                 :            :         err = -ESTALE;
     711                 :          0 :         if (inode->i_generation != handle->generation)
     712                 :            :                 goto out_iput;
     713                 :            : 
     714                 :          0 :         entry = d_obtain_alias(inode);
     715                 :          0 :         if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
     716                 :          0 :                 fuse_invalidate_entry_cache(entry);
     717                 :            : 
     718                 :          0 :         return entry;
     719                 :            : 
     720                 :            :  out_iput:
     721                 :          0 :         iput(inode);
     722                 :            :  out_err:
     723                 :          0 :         return ERR_PTR(err);
     724                 :            : }
     725                 :            : 
     726                 :          0 : static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
     727                 :            :                            struct inode *parent)
     728                 :            : {
     729                 :          0 :         int len = parent ? 6 : 3;
     730                 :            :         u64 nodeid;
     731                 :            :         u32 generation;
     732                 :            : 
     733                 :          0 :         if (*max_len < len) {
     734                 :          0 :                 *max_len = len;
     735                 :          0 :                 return  FILEID_INVALID;
     736                 :            :         }
     737                 :            : 
     738                 :          0 :         nodeid = get_fuse_inode(inode)->nodeid;
     739                 :          0 :         generation = inode->i_generation;
     740                 :            : 
     741                 :          0 :         fh[0] = (u32)(nodeid >> 32);
     742                 :          0 :         fh[1] = (u32)(nodeid & 0xffffffff);
     743                 :          0 :         fh[2] = generation;
     744                 :            : 
     745                 :          0 :         if (parent) {
     746                 :          0 :                 nodeid = get_fuse_inode(parent)->nodeid;
     747                 :          0 :                 generation = parent->i_generation;
     748                 :            : 
     749                 :          0 :                 fh[3] = (u32)(nodeid >> 32);
     750                 :          0 :                 fh[4] = (u32)(nodeid & 0xffffffff);
     751                 :          0 :                 fh[5] = generation;
     752                 :            :         }
     753                 :            : 
     754                 :          0 :         *max_len = len;
     755                 :          0 :         return parent ? 0x82 : 0x81;
     756                 :            : }
     757                 :            : 
     758                 :          0 : static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
     759                 :            :                 struct fid *fid, int fh_len, int fh_type)
     760                 :            : {
     761                 :            :         struct fuse_inode_handle handle;
     762                 :            : 
     763                 :          0 :         if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
     764                 :            :                 return NULL;
     765                 :            : 
     766                 :          0 :         handle.nodeid = (u64) fid->raw[0] << 32;
     767                 :          0 :         handle.nodeid |= (u64) fid->raw[1];
     768                 :          0 :         handle.generation = fid->raw[2];
     769                 :          0 :         return fuse_get_dentry(sb, &handle);
     770                 :            : }
     771                 :            : 
     772                 :          0 : static struct dentry *fuse_fh_to_parent(struct super_block *sb,
     773                 :            :                 struct fid *fid, int fh_len, int fh_type)
     774                 :            : {
     775                 :            :         struct fuse_inode_handle parent;
     776                 :            : 
     777                 :          0 :         if (fh_type != 0x82 || fh_len < 6)
     778                 :            :                 return NULL;
     779                 :            : 
     780                 :          0 :         parent.nodeid = (u64) fid->raw[3] << 32;
     781                 :          0 :         parent.nodeid |= (u64) fid->raw[4];
     782                 :          0 :         parent.generation = fid->raw[5];
     783                 :          0 :         return fuse_get_dentry(sb, &parent);
     784                 :            : }
     785                 :            : 
     786                 :          0 : static struct dentry *fuse_get_parent(struct dentry *child)
     787                 :            : {
     788                 :            :         struct inode *child_inode = d_inode(child);
     789                 :            :         struct fuse_conn *fc = get_fuse_conn(child_inode);
     790                 :            :         struct inode *inode;
     791                 :            :         struct dentry *parent;
     792                 :            :         struct fuse_entry_out outarg;
     793                 :          0 :         const struct qstr name = QSTR_INIT("..", 2);
     794                 :            :         int err;
     795                 :            : 
     796                 :          0 :         if (!fc->export_support)
     797                 :            :                 return ERR_PTR(-ESTALE);
     798                 :            : 
     799                 :          0 :         err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
     800                 :            :                                &name, &outarg, &inode);
     801                 :          0 :         if (err) {
     802                 :          0 :                 if (err == -ENOENT)
     803                 :            :                         return ERR_PTR(-ESTALE);
     804                 :          0 :                 return ERR_PTR(err);
     805                 :            :         }
     806                 :            : 
     807                 :          0 :         parent = d_obtain_alias(inode);
     808                 :          0 :         if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
     809                 :          0 :                 fuse_invalidate_entry_cache(parent);
     810                 :            : 
     811                 :          0 :         return parent;
     812                 :            : }
     813                 :            : 
     814                 :            : static const struct export_operations fuse_export_operations = {
     815                 :            :         .fh_to_dentry   = fuse_fh_to_dentry,
     816                 :            :         .fh_to_parent   = fuse_fh_to_parent,
     817                 :            :         .encode_fh      = fuse_encode_fh,
     818                 :            :         .get_parent     = fuse_get_parent,
     819                 :            : };
     820                 :            : 
     821                 :            : static const struct super_operations fuse_super_operations = {
     822                 :            :         .alloc_inode    = fuse_alloc_inode,
     823                 :            :         .free_inode     = fuse_free_inode,
     824                 :            :         .evict_inode    = fuse_evict_inode,
     825                 :            :         .write_inode    = fuse_write_inode,
     826                 :            :         .drop_inode     = generic_delete_inode,
     827                 :            :         .put_super      = fuse_put_super,
     828                 :            :         .umount_begin   = fuse_umount_begin,
     829                 :            :         .statfs         = fuse_statfs,
     830                 :            :         .show_options   = fuse_show_options,
     831                 :            : };
     832                 :            : 
     833                 :            : static void sanitize_global_limit(unsigned *limit)
     834                 :            : {
     835                 :            :         /*
     836                 :            :          * The default maximum number of async requests is calculated to consume
     837                 :            :          * 1/2^13 of the total memory, assuming 392 bytes per request.
     838                 :            :          */
     839                 :          3 :         if (*limit == 0)
     840                 :          3 :                 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
     841                 :            : 
     842                 :          3 :         if (*limit >= 1 << 16)
     843                 :          0 :                 *limit = (1 << 16) - 1;
     844                 :            : }
     845                 :            : 
     846                 :          0 : static int set_global_limit(const char *val, const struct kernel_param *kp)
     847                 :            : {
     848                 :            :         int rv;
     849                 :            : 
     850                 :          0 :         rv = param_set_uint(val, kp);
     851                 :          0 :         if (rv)
     852                 :            :                 return rv;
     853                 :            : 
     854                 :          0 :         sanitize_global_limit((unsigned *)kp->arg);
     855                 :            : 
     856                 :            :         return 0;
     857                 :            : }
     858                 :            : 
     859                 :          3 : static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
     860                 :            : {
     861                 :          3 :         int cap_sys_admin = capable(CAP_SYS_ADMIN);
     862                 :            : 
     863                 :          3 :         if (arg->minor < 13)
     864                 :          3 :                 return;
     865                 :            : 
     866                 :            :         sanitize_global_limit(&max_user_bgreq);
     867                 :            :         sanitize_global_limit(&max_user_congthresh);
     868                 :            : 
     869                 :            :         spin_lock(&fc->bg_lock);
     870                 :          3 :         if (arg->max_background) {
     871                 :          0 :                 fc->max_background = arg->max_background;
     872                 :            : 
     873                 :          0 :                 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
     874                 :          0 :                         fc->max_background = max_user_bgreq;
     875                 :            :         }
     876                 :          3 :         if (arg->congestion_threshold) {
     877                 :          0 :                 fc->congestion_threshold = arg->congestion_threshold;
     878                 :            : 
     879                 :          0 :                 if (!cap_sys_admin &&
     880                 :          0 :                     fc->congestion_threshold > max_user_congthresh)
     881                 :          0 :                         fc->congestion_threshold = max_user_congthresh;
     882                 :            :         }
     883                 :            :         spin_unlock(&fc->bg_lock);
     884                 :            : }
     885                 :            : 
     886                 :            : struct fuse_init_args {
     887                 :            :         struct fuse_args args;
     888                 :            :         struct fuse_init_in in;
     889                 :            :         struct fuse_init_out out;
     890                 :            : };
     891                 :            : 
     892                 :          3 : static void process_init_reply(struct fuse_conn *fc, struct fuse_args *args,
     893                 :            :                                int error)
     894                 :            : {
     895                 :            :         struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
     896                 :          3 :         struct fuse_init_out *arg = &ia->out;
     897                 :            : 
     898                 :          3 :         if (error || arg->major != FUSE_KERNEL_VERSION)
     899                 :          0 :                 fc->conn_error = 1;
     900                 :            :         else {
     901                 :            :                 unsigned long ra_pages;
     902                 :            : 
     903                 :          3 :                 process_init_limits(fc, arg);
     904                 :            : 
     905                 :          3 :                 if (arg->minor >= 6) {
     906                 :          3 :                         ra_pages = arg->max_readahead / PAGE_SIZE;
     907                 :          3 :                         if (arg->flags & FUSE_ASYNC_READ)
     908                 :          0 :                                 fc->async_read = 1;
     909                 :          3 :                         if (!(arg->flags & FUSE_POSIX_LOCKS))
     910                 :          3 :                                 fc->no_lock = 1;
     911                 :          3 :                         if (arg->minor >= 17) {
     912                 :          3 :                                 if (!(arg->flags & FUSE_FLOCK_LOCKS))
     913                 :          3 :                                         fc->no_flock = 1;
     914                 :            :                         } else {
     915                 :          0 :                                 if (!(arg->flags & FUSE_POSIX_LOCKS))
     916                 :          0 :                                         fc->no_flock = 1;
     917                 :            :                         }
     918                 :          3 :                         if (arg->flags & FUSE_ATOMIC_O_TRUNC)
     919                 :          3 :                                 fc->atomic_o_trunc = 1;
     920                 :          3 :                         if (arg->minor >= 9) {
     921                 :            :                                 /* LOOKUP has dependency on proto version */
     922                 :          3 :                                 if (arg->flags & FUSE_EXPORT_SUPPORT)
     923                 :          3 :                                         fc->export_support = 1;
     924                 :            :                         }
     925                 :          3 :                         if (arg->flags & FUSE_BIG_WRITES)
     926                 :          3 :                                 fc->big_writes = 1;
     927                 :          3 :                         if (arg->flags & FUSE_DONT_MASK)
     928                 :          0 :                                 fc->dont_mask = 1;
     929                 :          3 :                         if (arg->flags & FUSE_AUTO_INVAL_DATA)
     930                 :          0 :                                 fc->auto_inval_data = 1;
     931                 :          3 :                         else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
     932                 :          0 :                                 fc->explicit_inval_data = 1;
     933                 :          3 :                         if (arg->flags & FUSE_DO_READDIRPLUS) {
     934                 :          0 :                                 fc->do_readdirplus = 1;
     935                 :          0 :                                 if (arg->flags & FUSE_READDIRPLUS_AUTO)
     936                 :          0 :                                         fc->readdirplus_auto = 1;
     937                 :            :                         }
     938                 :          3 :                         if (arg->flags & FUSE_ASYNC_DIO)
     939                 :          0 :                                 fc->async_dio = 1;
     940                 :          3 :                         if (arg->flags & FUSE_WRITEBACK_CACHE)
     941                 :          0 :                                 fc->writeback_cache = 1;
     942                 :          3 :                         if (arg->flags & FUSE_PARALLEL_DIROPS)
     943                 :          0 :                                 fc->parallel_dirops = 1;
     944                 :          3 :                         if (arg->flags & FUSE_HANDLE_KILLPRIV)
     945                 :          0 :                                 fc->handle_killpriv = 1;
     946                 :          3 :                         if (arg->time_gran && arg->time_gran <= 1000000000)
     947                 :          0 :                                 fc->sb->s_time_gran = arg->time_gran;
     948                 :          3 :                         if ((arg->flags & FUSE_POSIX_ACL)) {
     949                 :          0 :                                 fc->default_permissions = 1;
     950                 :          0 :                                 fc->posix_acl = 1;
     951                 :          0 :                                 fc->sb->s_xattr = fuse_acl_xattr_handlers;
     952                 :            :                         }
     953                 :          3 :                         if (arg->flags & FUSE_CACHE_SYMLINKS)
     954                 :          0 :                                 fc->cache_symlinks = 1;
     955                 :          3 :                         if (arg->flags & FUSE_ABORT_ERROR)
     956                 :          0 :                                 fc->abort_err = 1;
     957                 :          3 :                         if (arg->flags & FUSE_MAX_PAGES) {
     958                 :          0 :                                 fc->max_pages =
     959                 :          0 :                                         min_t(unsigned int, FUSE_MAX_MAX_PAGES,
     960                 :            :                                         max_t(unsigned int, arg->max_pages, 1));
     961                 :            :                         }
     962                 :            :                 } else {
     963                 :          0 :                         ra_pages = fc->max_read / PAGE_SIZE;
     964                 :          0 :                         fc->no_lock = 1;
     965                 :          0 :                         fc->no_flock = 1;
     966                 :            :                 }
     967                 :            : 
     968                 :          3 :                 fc->sb->s_bdi->ra_pages =
     969                 :          3 :                                 min(fc->sb->s_bdi->ra_pages, ra_pages);
     970                 :          3 :                 fc->minor = arg->minor;
     971                 :          3 :                 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
     972                 :          3 :                 fc->max_write = max_t(unsigned, 4096, fc->max_write);
     973                 :          3 :                 fc->conn_init = 1;
     974                 :            :         }
     975                 :          3 :         kfree(ia);
     976                 :            : 
     977                 :          3 :         fuse_set_initialized(fc);
     978                 :          3 :         wake_up_all(&fc->blocked_waitq);
     979                 :          3 : }
     980                 :            : 
     981                 :          3 : void fuse_send_init(struct fuse_conn *fc)
     982                 :            : {
     983                 :            :         struct fuse_init_args *ia;
     984                 :            : 
     985                 :          3 :         ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
     986                 :            : 
     987                 :          3 :         ia->in.major = FUSE_KERNEL_VERSION;
     988                 :          3 :         ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
     989                 :          3 :         ia->in.max_readahead = fc->sb->s_bdi->ra_pages * PAGE_SIZE;
     990                 :          3 :         ia->in.flags |=
     991                 :            :                 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
     992                 :            :                 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
     993                 :            :                 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
     994                 :            :                 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
     995                 :            :                 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
     996                 :            :                 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
     997                 :            :                 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
     998                 :            :                 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
     999                 :            :                 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA;
    1000                 :          3 :         ia->args.opcode = FUSE_INIT;
    1001                 :          3 :         ia->args.in_numargs = 1;
    1002                 :          3 :         ia->args.in_args[0].size = sizeof(ia->in);
    1003                 :          3 :         ia->args.in_args[0].value = &ia->in;
    1004                 :          3 :         ia->args.out_numargs = 1;
    1005                 :            :         /* Variable length argument used for backward compatibility
    1006                 :            :            with interface version < 7.5.  Rest of init_out is zeroed
    1007                 :            :            by do_get_request(), so a short reply is not a problem */
    1008                 :          3 :         ia->args.out_argvar = 1;
    1009                 :          3 :         ia->args.out_args[0].size = sizeof(ia->out);
    1010                 :          3 :         ia->args.out_args[0].value = &ia->out;
    1011                 :          3 :         ia->args.force = true;
    1012                 :          3 :         ia->args.nocreds = true;
    1013                 :          3 :         ia->args.end = process_init_reply;
    1014                 :            : 
    1015                 :          3 :         if (fuse_simple_background(fc, &ia->args, GFP_KERNEL) != 0)
    1016                 :          0 :                 process_init_reply(fc, &ia->args, -ENOTCONN);
    1017                 :          3 : }
    1018                 :            : EXPORT_SYMBOL_GPL(fuse_send_init);
    1019                 :            : 
    1020                 :          0 : void fuse_free_conn(struct fuse_conn *fc)
    1021                 :            : {
    1022                 :          0 :         WARN_ON(!list_empty(&fc->devices));
    1023                 :          0 :         kfree_rcu(fc, rcu);
    1024                 :          0 : }
    1025                 :            : EXPORT_SYMBOL_GPL(fuse_free_conn);
    1026                 :            : 
    1027                 :          3 : static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
    1028                 :            : {
    1029                 :            :         int err;
    1030                 :            :         char *suffix = "";
    1031                 :            : 
    1032                 :          3 :         if (sb->s_bdev) {
    1033                 :            :                 suffix = "-fuseblk";
    1034                 :            :                 /*
    1035                 :            :                  * sb->s_bdi points to blkdev's bdi however we want to redirect
    1036                 :            :                  * it to our private bdi...
    1037                 :            :                  */
    1038                 :          0 :                 bdi_put(sb->s_bdi);
    1039                 :          0 :                 sb->s_bdi = &noop_backing_dev_info;
    1040                 :            :         }
    1041                 :          3 :         err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
    1042                 :            :                                    MINOR(fc->dev), suffix);
    1043                 :          3 :         if (err)
    1044                 :            :                 return err;
    1045                 :            : 
    1046                 :          3 :         sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
    1047                 :            :         /* fuse does it's own writeback accounting */
    1048                 :          3 :         sb->s_bdi->capabilities = BDI_CAP_NO_ACCT_WB | BDI_CAP_STRICTLIMIT;
    1049                 :            : 
    1050                 :            :         /*
    1051                 :            :          * For a single fuse filesystem use max 1% of dirty +
    1052                 :            :          * writeback threshold.
    1053                 :            :          *
    1054                 :            :          * This gives about 1M of write buffer for memory maps on a
    1055                 :            :          * machine with 1G and 10% dirty_ratio, which should be more
    1056                 :            :          * than enough.
    1057                 :            :          *
    1058                 :            :          * Privileged users can raise it by writing to
    1059                 :            :          *
    1060                 :            :          *    /sys/class/bdi/<bdi>/max_ratio
    1061                 :            :          */
    1062                 :          3 :         bdi_set_max_ratio(sb->s_bdi, 1);
    1063                 :            : 
    1064                 :          3 :         return 0;
    1065                 :            : }
    1066                 :            : 
    1067                 :          3 : struct fuse_dev *fuse_dev_alloc(void)
    1068                 :            : {
    1069                 :            :         struct fuse_dev *fud;
    1070                 :            :         struct list_head *pq;
    1071                 :            : 
    1072                 :          3 :         fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
    1073                 :          3 :         if (!fud)
    1074                 :            :                 return NULL;
    1075                 :            : 
    1076                 :            :         pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
    1077                 :          3 :         if (!pq) {
    1078                 :          0 :                 kfree(fud);
    1079                 :          0 :                 return NULL;
    1080                 :            :         }
    1081                 :            : 
    1082                 :          3 :         fud->pq.processing = pq;
    1083                 :            :         fuse_pqueue_init(&fud->pq);
    1084                 :            : 
    1085                 :          3 :         return fud;
    1086                 :            : }
    1087                 :            : EXPORT_SYMBOL_GPL(fuse_dev_alloc);
    1088                 :            : 
    1089                 :          3 : void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
    1090                 :            : {
    1091                 :          3 :         fud->fc = fuse_conn_get(fc);
    1092                 :            :         spin_lock(&fc->lock);
    1093                 :          3 :         list_add_tail(&fud->entry, &fc->devices);
    1094                 :            :         spin_unlock(&fc->lock);
    1095                 :          3 : }
    1096                 :            : EXPORT_SYMBOL_GPL(fuse_dev_install);
    1097                 :            : 
    1098                 :          3 : struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
    1099                 :            : {
    1100                 :            :         struct fuse_dev *fud;
    1101                 :            : 
    1102                 :          3 :         fud = fuse_dev_alloc();
    1103                 :          3 :         if (!fud)
    1104                 :            :                 return NULL;
    1105                 :            : 
    1106                 :          3 :         fuse_dev_install(fud, fc);
    1107                 :          3 :         return fud;
    1108                 :            : }
    1109                 :            : EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
    1110                 :            : 
    1111                 :          0 : void fuse_dev_free(struct fuse_dev *fud)
    1112                 :            : {
    1113                 :          0 :         struct fuse_conn *fc = fud->fc;
    1114                 :            : 
    1115                 :          0 :         if (fc) {
    1116                 :            :                 spin_lock(&fc->lock);
    1117                 :            :                 list_del(&fud->entry);
    1118                 :            :                 spin_unlock(&fc->lock);
    1119                 :            : 
    1120                 :          0 :                 fuse_conn_put(fc);
    1121                 :            :         }
    1122                 :          0 :         kfree(fud->pq.processing);
    1123                 :          0 :         kfree(fud);
    1124                 :          0 : }
    1125                 :            : EXPORT_SYMBOL_GPL(fuse_dev_free);
    1126                 :            : 
    1127                 :          3 : int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
    1128                 :            : {
    1129                 :            :         struct fuse_dev *fud;
    1130                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
    1131                 :            :         struct inode *root;
    1132                 :            :         struct dentry *root_dentry;
    1133                 :            :         int err;
    1134                 :            : 
    1135                 :            :         err = -EINVAL;
    1136                 :          3 :         if (sb->s_flags & SB_MANDLOCK)
    1137                 :            :                 goto err;
    1138                 :            : 
    1139                 :          3 :         sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
    1140                 :            : 
    1141                 :          3 :         if (ctx->is_bdev) {
    1142                 :            : #ifdef CONFIG_BLOCK
    1143                 :            :                 err = -EINVAL;
    1144                 :          0 :                 if (!sb_set_blocksize(sb, ctx->blksize))
    1145                 :            :                         goto err;
    1146                 :            : #endif
    1147                 :            :         } else {
    1148                 :          3 :                 sb->s_blocksize = PAGE_SIZE;
    1149                 :          3 :                 sb->s_blocksize_bits = PAGE_SHIFT;
    1150                 :            :         }
    1151                 :            : 
    1152                 :          3 :         sb->s_subtype = ctx->subtype;
    1153                 :          3 :         ctx->subtype = NULL;
    1154                 :          3 :         sb->s_magic = FUSE_SUPER_MAGIC;
    1155                 :          3 :         sb->s_op = &fuse_super_operations;
    1156                 :          3 :         sb->s_xattr = fuse_xattr_handlers;
    1157                 :          3 :         sb->s_maxbytes = MAX_LFS_FILESIZE;
    1158                 :          3 :         sb->s_time_gran = 1;
    1159                 :          3 :         sb->s_export_op = &fuse_export_operations;
    1160                 :          3 :         sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
    1161                 :          3 :         if (sb->s_user_ns != &init_user_ns)
    1162                 :          0 :                 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
    1163                 :            : 
    1164                 :            :         /*
    1165                 :            :          * If we are not in the initial user namespace posix
    1166                 :            :          * acls must be translated.
    1167                 :            :          */
    1168                 :          3 :         if (sb->s_user_ns != &init_user_ns)
    1169                 :          0 :                 sb->s_xattr = fuse_no_acl_xattr_handlers;
    1170                 :            : 
    1171                 :          3 :         fud = fuse_dev_alloc_install(fc);
    1172                 :          3 :         if (!fud)
    1173                 :            :                 goto err;
    1174                 :            : 
    1175                 :          3 :         fc->dev = sb->s_dev;
    1176                 :          3 :         fc->sb = sb;
    1177                 :          3 :         err = fuse_bdi_init(fc, sb);
    1178                 :          3 :         if (err)
    1179                 :            :                 goto err_dev_free;
    1180                 :            : 
    1181                 :            :         /* Handle umasking inside the fuse code */
    1182                 :          3 :         if (sb->s_flags & SB_POSIXACL)
    1183                 :          0 :                 fc->dont_mask = 1;
    1184                 :          3 :         sb->s_flags |= SB_POSIXACL;
    1185                 :            : 
    1186                 :          3 :         fc->default_permissions = ctx->default_permissions;
    1187                 :          3 :         fc->allow_other = ctx->allow_other;
    1188                 :          3 :         fc->user_id = ctx->user_id;
    1189                 :          3 :         fc->group_id = ctx->group_id;
    1190                 :          3 :         fc->max_read = max_t(unsigned, 4096, ctx->max_read);
    1191                 :          3 :         fc->destroy = ctx->destroy;
    1192                 :          3 :         fc->no_control = ctx->no_control;
    1193                 :          3 :         fc->no_force_umount = ctx->no_force_umount;
    1194                 :          3 :         fc->no_mount_options = ctx->no_mount_options;
    1195                 :            : 
    1196                 :            :         err = -ENOMEM;
    1197                 :          3 :         root = fuse_get_root_inode(sb, ctx->rootmode);
    1198                 :          3 :         sb->s_d_op = &fuse_root_dentry_operations;
    1199                 :          3 :         root_dentry = d_make_root(root);
    1200                 :          3 :         if (!root_dentry)
    1201                 :            :                 goto err_dev_free;
    1202                 :            :         /* Root dentry doesn't have .d_revalidate */
    1203                 :          3 :         sb->s_d_op = &fuse_dentry_operations;
    1204                 :            : 
    1205                 :          3 :         mutex_lock(&fuse_mutex);
    1206                 :            :         err = -EINVAL;
    1207                 :          3 :         if (*ctx->fudptr)
    1208                 :            :                 goto err_unlock;
    1209                 :            : 
    1210                 :          3 :         err = fuse_ctl_add_conn(fc);
    1211                 :          3 :         if (err)
    1212                 :            :                 goto err_unlock;
    1213                 :            : 
    1214                 :          3 :         list_add_tail(&fc->entry, &fuse_conn_list);
    1215                 :          3 :         sb->s_root = root_dentry;
    1216                 :          3 :         *ctx->fudptr = fud;
    1217                 :          3 :         mutex_unlock(&fuse_mutex);
    1218                 :          3 :         return 0;
    1219                 :            : 
    1220                 :            :  err_unlock:
    1221                 :          0 :         mutex_unlock(&fuse_mutex);
    1222                 :          0 :         dput(root_dentry);
    1223                 :            :  err_dev_free:
    1224                 :          0 :         fuse_dev_free(fud);
    1225                 :            :  err:
    1226                 :          0 :         return err;
    1227                 :            : }
    1228                 :            : EXPORT_SYMBOL_GPL(fuse_fill_super_common);
    1229                 :            : 
    1230                 :          3 : static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
    1231                 :            : {
    1232                 :          3 :         struct fuse_fs_context *ctx = fsc->fs_private;
    1233                 :            :         struct file *file;
    1234                 :            :         int err;
    1235                 :            :         struct fuse_conn *fc;
    1236                 :            : 
    1237                 :            :         err = -EINVAL;
    1238                 :          3 :         file = fget(ctx->fd);
    1239                 :          3 :         if (!file)
    1240                 :            :                 goto err;
    1241                 :            : 
    1242                 :            :         /*
    1243                 :            :          * Require mount to happen from the same user namespace which
    1244                 :            :          * opened /dev/fuse to prevent potential attacks.
    1245                 :            :          */
    1246                 :          3 :         if ((file->f_op != &fuse_dev_operations) ||
    1247                 :          3 :             (file->f_cred->user_ns != sb->s_user_ns))
    1248                 :            :                 goto err_fput;
    1249                 :          3 :         ctx->fudptr = &file->private_data;
    1250                 :            : 
    1251                 :            :         fc = kmalloc(sizeof(*fc), GFP_KERNEL);
    1252                 :            :         err = -ENOMEM;
    1253                 :          3 :         if (!fc)
    1254                 :            :                 goto err_fput;
    1255                 :            : 
    1256                 :          3 :         fuse_conn_init(fc, sb->s_user_ns, &fuse_dev_fiq_ops, NULL);
    1257                 :          3 :         fc->release = fuse_free_conn;
    1258                 :          3 :         sb->s_fs_info = fc;
    1259                 :            : 
    1260                 :          3 :         err = fuse_fill_super_common(sb, ctx);
    1261                 :          3 :         if (err)
    1262                 :            :                 goto err_put_conn;
    1263                 :            :         /*
    1264                 :            :          * atomic_dec_and_test() in fput() provides the necessary
    1265                 :            :          * memory barrier for file->private_data to be visible on all
    1266                 :            :          * CPUs after this
    1267                 :            :          */
    1268                 :          3 :         fput(file);
    1269                 :          3 :         fuse_send_init(get_fuse_conn_super(sb));
    1270                 :          3 :         return 0;
    1271                 :            : 
    1272                 :            :  err_put_conn:
    1273                 :          0 :         fuse_conn_put(fc);
    1274                 :          0 :         sb->s_fs_info = NULL;
    1275                 :            :  err_fput:
    1276                 :          0 :         fput(file);
    1277                 :            :  err:
    1278                 :          0 :         return err;
    1279                 :            : }
    1280                 :            : 
    1281                 :          3 : static int fuse_get_tree(struct fs_context *fc)
    1282                 :            : {
    1283                 :          3 :         struct fuse_fs_context *ctx = fc->fs_private;
    1284                 :            : 
    1285                 :          3 :         if (!ctx->fd_present || !ctx->rootmode_present ||
    1286                 :          3 :             !ctx->user_id_present || !ctx->group_id_present)
    1287                 :            :                 return -EINVAL;
    1288                 :            : 
    1289                 :            : #ifdef CONFIG_BLOCK
    1290                 :          3 :         if (ctx->is_bdev)
    1291                 :          0 :                 return get_tree_bdev(fc, fuse_fill_super);
    1292                 :            : #endif
    1293                 :            : 
    1294                 :          3 :         return get_tree_nodev(fc, fuse_fill_super);
    1295                 :            : }
    1296                 :            : 
    1297                 :            : static const struct fs_context_operations fuse_context_ops = {
    1298                 :            :         .free           = fuse_free_fc,
    1299                 :            :         .parse_param    = fuse_parse_param,
    1300                 :            :         .reconfigure    = fuse_reconfigure,
    1301                 :            :         .get_tree       = fuse_get_tree,
    1302                 :            : };
    1303                 :            : 
    1304                 :            : /*
    1305                 :            :  * Set up the filesystem mount context.
    1306                 :            :  */
    1307                 :          3 : static int fuse_init_fs_context(struct fs_context *fc)
    1308                 :            : {
    1309                 :            :         struct fuse_fs_context *ctx;
    1310                 :            : 
    1311                 :          3 :         ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
    1312                 :          3 :         if (!ctx)
    1313                 :            :                 return -ENOMEM;
    1314                 :            : 
    1315                 :          3 :         ctx->max_read = ~0;
    1316                 :          3 :         ctx->blksize = FUSE_DEFAULT_BLKSIZE;
    1317                 :            : 
    1318                 :            : #ifdef CONFIG_BLOCK
    1319                 :          3 :         if (fc->fs_type == &fuseblk_fs_type) {
    1320                 :          0 :                 ctx->is_bdev = true;
    1321                 :          0 :                 ctx->destroy = true;
    1322                 :            :         }
    1323                 :            : #endif
    1324                 :            : 
    1325                 :          3 :         fc->fs_private = ctx;
    1326                 :          3 :         fc->ops = &fuse_context_ops;
    1327                 :          3 :         return 0;
    1328                 :            : }
    1329                 :            : 
    1330                 :          0 : static void fuse_sb_destroy(struct super_block *sb)
    1331                 :            : {
    1332                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
    1333                 :            : 
    1334                 :          0 :         if (fc) {
    1335                 :          0 :                 if (fc->destroy)
    1336                 :          0 :                         fuse_send_destroy(fc);
    1337                 :            : 
    1338                 :          0 :                 fuse_abort_conn(fc);
    1339                 :          0 :                 fuse_wait_aborted(fc);
    1340                 :            : 
    1341                 :          0 :                 down_write(&fc->killsb);
    1342                 :          0 :                 fc->sb = NULL;
    1343                 :          0 :                 up_write(&fc->killsb);
    1344                 :            :         }
    1345                 :          0 : }
    1346                 :            : 
    1347                 :          0 : void fuse_kill_sb_anon(struct super_block *sb)
    1348                 :            : {
    1349                 :          0 :         fuse_sb_destroy(sb);
    1350                 :          0 :         kill_anon_super(sb);
    1351                 :          0 : }
    1352                 :            : EXPORT_SYMBOL_GPL(fuse_kill_sb_anon);
    1353                 :            : 
    1354                 :            : static struct file_system_type fuse_fs_type = {
    1355                 :            :         .owner          = THIS_MODULE,
    1356                 :            :         .name           = "fuse",
    1357                 :            :         .fs_flags       = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
    1358                 :            :         .init_fs_context = fuse_init_fs_context,
    1359                 :            :         .parameters     = &fuse_fs_parameters,
    1360                 :            :         .kill_sb        = fuse_kill_sb_anon,
    1361                 :            : };
    1362                 :            : MODULE_ALIAS_FS("fuse");
    1363                 :            : 
    1364                 :            : #ifdef CONFIG_BLOCK
    1365                 :          0 : static void fuse_kill_sb_blk(struct super_block *sb)
    1366                 :            : {
    1367                 :          0 :         fuse_sb_destroy(sb);
    1368                 :          0 :         kill_block_super(sb);
    1369                 :          0 : }
    1370                 :            : 
    1371                 :            : static struct file_system_type fuseblk_fs_type = {
    1372                 :            :         .owner          = THIS_MODULE,
    1373                 :            :         .name           = "fuseblk",
    1374                 :            :         .init_fs_context = fuse_init_fs_context,
    1375                 :            :         .parameters     = &fuse_fs_parameters,
    1376                 :            :         .kill_sb        = fuse_kill_sb_blk,
    1377                 :            :         .fs_flags       = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
    1378                 :            : };
    1379                 :            : MODULE_ALIAS_FS("fuseblk");
    1380                 :            : 
    1381                 :            : static inline int register_fuseblk(void)
    1382                 :            : {
    1383                 :          3 :         return register_filesystem(&fuseblk_fs_type);
    1384                 :            : }
    1385                 :            : 
    1386                 :            : static inline void unregister_fuseblk(void)
    1387                 :            : {
    1388                 :          0 :         unregister_filesystem(&fuseblk_fs_type);
    1389                 :            : }
    1390                 :            : #else
    1391                 :            : static inline int register_fuseblk(void)
    1392                 :            : {
    1393                 :            :         return 0;
    1394                 :            : }
    1395                 :            : 
    1396                 :            : static inline void unregister_fuseblk(void)
    1397                 :            : {
    1398                 :            : }
    1399                 :            : #endif
    1400                 :            : 
    1401                 :          3 : static void fuse_inode_init_once(void *foo)
    1402                 :            : {
    1403                 :            :         struct inode *inode = foo;
    1404                 :            : 
    1405                 :          3 :         inode_init_once(inode);
    1406                 :          3 : }
    1407                 :            : 
    1408                 :          3 : static int __init fuse_fs_init(void)
    1409                 :            : {
    1410                 :            :         int err;
    1411                 :            : 
    1412                 :          3 :         fuse_inode_cachep = kmem_cache_create("fuse_inode",
    1413                 :            :                         sizeof(struct fuse_inode), 0,
    1414                 :            :                         SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
    1415                 :            :                         fuse_inode_init_once);
    1416                 :            :         err = -ENOMEM;
    1417                 :          3 :         if (!fuse_inode_cachep)
    1418                 :            :                 goto out;
    1419                 :            : 
    1420                 :            :         err = register_fuseblk();
    1421                 :          3 :         if (err)
    1422                 :            :                 goto out2;
    1423                 :            : 
    1424                 :          3 :         err = register_filesystem(&fuse_fs_type);
    1425                 :          3 :         if (err)
    1426                 :            :                 goto out3;
    1427                 :            : 
    1428                 :            :         return 0;
    1429                 :            : 
    1430                 :            :  out3:
    1431                 :            :         unregister_fuseblk();
    1432                 :            :  out2:
    1433                 :          0 :         kmem_cache_destroy(fuse_inode_cachep);
    1434                 :            :  out:
    1435                 :          0 :         return err;
    1436                 :            : }
    1437                 :            : 
    1438                 :          0 : static void fuse_fs_cleanup(void)
    1439                 :            : {
    1440                 :          0 :         unregister_filesystem(&fuse_fs_type);
    1441                 :            :         unregister_fuseblk();
    1442                 :            : 
    1443                 :            :         /*
    1444                 :            :          * Make sure all delayed rcu free inodes are flushed before we
    1445                 :            :          * destroy cache.
    1446                 :            :          */
    1447                 :          0 :         rcu_barrier();
    1448                 :          0 :         kmem_cache_destroy(fuse_inode_cachep);
    1449                 :          0 : }
    1450                 :            : 
    1451                 :            : static struct kobject *fuse_kobj;
    1452                 :            : 
    1453                 :          3 : static int fuse_sysfs_init(void)
    1454                 :            : {
    1455                 :            :         int err;
    1456                 :            : 
    1457                 :          3 :         fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
    1458                 :          3 :         if (!fuse_kobj) {
    1459                 :            :                 err = -ENOMEM;
    1460                 :            :                 goto out_err;
    1461                 :            :         }
    1462                 :            : 
    1463                 :          3 :         err = sysfs_create_mount_point(fuse_kobj, "connections");
    1464                 :          3 :         if (err)
    1465                 :            :                 goto out_fuse_unregister;
    1466                 :            : 
    1467                 :            :         return 0;
    1468                 :            : 
    1469                 :            :  out_fuse_unregister:
    1470                 :          0 :         kobject_put(fuse_kobj);
    1471                 :            :  out_err:
    1472                 :          0 :         return err;
    1473                 :            : }
    1474                 :            : 
    1475                 :          0 : static void fuse_sysfs_cleanup(void)
    1476                 :            : {
    1477                 :          0 :         sysfs_remove_mount_point(fuse_kobj, "connections");
    1478                 :          0 :         kobject_put(fuse_kobj);
    1479                 :          0 : }
    1480                 :            : 
    1481                 :          3 : static int __init fuse_init(void)
    1482                 :            : {
    1483                 :            :         int res;
    1484                 :            : 
    1485                 :          3 :         pr_info("init (API version %i.%i)\n",
    1486                 :            :                 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
    1487                 :            : 
    1488                 :            :         INIT_LIST_HEAD(&fuse_conn_list);
    1489                 :          3 :         res = fuse_fs_init();
    1490                 :          3 :         if (res)
    1491                 :            :                 goto err;
    1492                 :            : 
    1493                 :          3 :         res = fuse_dev_init();
    1494                 :          3 :         if (res)
    1495                 :            :                 goto err_fs_cleanup;
    1496                 :            : 
    1497                 :          3 :         res = fuse_sysfs_init();
    1498                 :          3 :         if (res)
    1499                 :            :                 goto err_dev_cleanup;
    1500                 :            : 
    1501                 :          3 :         res = fuse_ctl_init();
    1502                 :          3 :         if (res)
    1503                 :            :                 goto err_sysfs_cleanup;
    1504                 :            : 
    1505                 :            :         sanitize_global_limit(&max_user_bgreq);
    1506                 :            :         sanitize_global_limit(&max_user_congthresh);
    1507                 :            : 
    1508                 :            :         return 0;
    1509                 :            : 
    1510                 :            :  err_sysfs_cleanup:
    1511                 :          0 :         fuse_sysfs_cleanup();
    1512                 :            :  err_dev_cleanup:
    1513                 :          0 :         fuse_dev_cleanup();
    1514                 :            :  err_fs_cleanup:
    1515                 :          0 :         fuse_fs_cleanup();
    1516                 :            :  err:
    1517                 :          0 :         return res;
    1518                 :            : }
    1519                 :            : 
    1520                 :          0 : static void __exit fuse_exit(void)
    1521                 :            : {
    1522                 :            :         pr_debug("exit\n");
    1523                 :            : 
    1524                 :          0 :         fuse_ctl_cleanup();
    1525                 :          0 :         fuse_sysfs_cleanup();
    1526                 :          0 :         fuse_fs_cleanup();
    1527                 :          0 :         fuse_dev_cleanup();
    1528                 :          0 : }
    1529                 :            : 
    1530                 :            : module_init(fuse_init);
    1531                 :            : module_exit(fuse_exit);
    

Generated by: LCOV version 1.14