LCOV - code coverage report
Current view: top level - fs/fuse - dir.c (source / functions) Hit Total Coverage
Test: Real Lines: 98 736 13.3 %
Date: 2020-10-17 15:46:16 Functions: 0 59 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/file.h>
      13                 :            : #include <linux/sched.h>
      14                 :            : #include <linux/namei.h>
      15                 :            : #include <linux/slab.h>
      16                 :            : #include <linux/xattr.h>
      17                 :            : #include <linux/iversion.h>
      18                 :            : #include <linux/posix_acl.h>
      19                 :            : 
      20                 :            : static void fuse_advise_use_readdirplus(struct inode *dir)
      21                 :            : {
      22                 :            :         struct fuse_inode *fi = get_fuse_inode(dir);
      23                 :            : 
      24                 :          0 :         set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
      25                 :            : }
      26                 :            : 
      27                 :            : #if BITS_PER_LONG >= 64
      28                 :            : static inline void __fuse_dentry_settime(struct dentry *entry, u64 time)
      29                 :            : {
      30                 :            :         entry->d_fsdata = (void *) time;
      31                 :            : }
      32                 :            : 
      33                 :            : static inline u64 fuse_dentry_time(const struct dentry *entry)
      34                 :            : {
      35                 :            :         return (u64)entry->d_fsdata;
      36                 :            : }
      37                 :            : 
      38                 :            : #else
      39                 :            : union fuse_dentry {
      40                 :            :         u64 time;
      41                 :            :         struct rcu_head rcu;
      42                 :            : };
      43                 :            : 
      44                 :            : static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
      45                 :            : {
      46                 :          3 :         ((union fuse_dentry *) dentry->d_fsdata)->time = time;
      47                 :            : }
      48                 :            : 
      49                 :            : static inline u64 fuse_dentry_time(const struct dentry *entry)
      50                 :            : {
      51                 :          3 :         return ((union fuse_dentry *) entry->d_fsdata)->time;
      52                 :            : }
      53                 :            : #endif
      54                 :            : 
      55                 :          3 : static void fuse_dentry_settime(struct dentry *dentry, u64 time)
      56                 :            : {
      57                 :          3 :         struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
      58                 :          3 :         bool delete = !time && fc->delete_stale;
      59                 :            :         /*
      60                 :            :          * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
      61                 :            :          * Don't care about races, either way it's just an optimization
      62                 :            :          */
      63                 :          3 :         if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
      64                 :          0 :             (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
      65                 :            :                 spin_lock(&dentry->d_lock);
      66                 :          3 :                 if (!delete)
      67                 :          3 :                         dentry->d_flags &= ~DCACHE_OP_DELETE;
      68                 :            :                 else
      69                 :          0 :                         dentry->d_flags |= DCACHE_OP_DELETE;
      70                 :            :                 spin_unlock(&dentry->d_lock);
      71                 :            :         }
      72                 :            : 
      73                 :            :         __fuse_dentry_settime(dentry, time);
      74                 :          3 : }
      75                 :            : 
      76                 :            : /*
      77                 :            :  * FUSE caches dentries and attributes with separate timeout.  The
      78                 :            :  * time in jiffies until the dentry/attributes are valid is stored in
      79                 :            :  * dentry->d_fsdata and fuse_inode->i_time respectively.
      80                 :            :  */
      81                 :            : 
      82                 :            : /*
      83                 :            :  * Calculate the time in jiffies until a dentry/attributes are valid
      84                 :            :  */
      85                 :          0 : static u64 time_to_jiffies(u64 sec, u32 nsec)
      86                 :            : {
      87                 :          0 :         if (sec || nsec) {
      88                 :          0 :                 struct timespec64 ts = {
      89                 :            :                         sec,
      90                 :          0 :                         min_t(u32, nsec, NSEC_PER_SEC - 1)
      91                 :            :                 };
      92                 :            : 
      93                 :          0 :                 return get_jiffies_64() + timespec64_to_jiffies(&ts);
      94                 :            :         } else
      95                 :            :                 return 0;
      96                 :            : }
      97                 :            : 
      98                 :            : /*
      99                 :            :  * Set dentry and possibly attribute timeouts from the lookup/mk*
     100                 :            :  * replies
     101                 :            :  */
     102                 :          0 : void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
     103                 :            : {
     104                 :          0 :         fuse_dentry_settime(entry,
     105                 :            :                 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
     106                 :          0 : }
     107                 :            : 
     108                 :            : static u64 attr_timeout(struct fuse_attr_out *o)
     109                 :            : {
     110                 :          0 :         return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
     111                 :            : }
     112                 :            : 
     113                 :          0 : u64 entry_attr_timeout(struct fuse_entry_out *o)
     114                 :            : {
     115                 :          0 :         return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
     116                 :            : }
     117                 :            : 
     118                 :          0 : static void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
     119                 :            : {
     120                 :          0 :         set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
     121                 :          0 : }
     122                 :            : 
     123                 :            : /*
     124                 :            :  * Mark the attributes as stale, so that at the next call to
     125                 :            :  * ->getattr() they will be fetched from userspace
     126                 :            :  */
     127                 :          0 : void fuse_invalidate_attr(struct inode *inode)
     128                 :            : {
     129                 :          0 :         fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
     130                 :          0 : }
     131                 :            : 
     132                 :          0 : static void fuse_dir_changed(struct inode *dir)
     133                 :            : {
     134                 :            :         fuse_invalidate_attr(dir);
     135                 :          0 :         inode_maybe_inc_iversion(dir, false);
     136                 :          0 : }
     137                 :            : 
     138                 :            : /**
     139                 :            :  * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
     140                 :            :  * atime is not used.
     141                 :            :  */
     142                 :          0 : void fuse_invalidate_atime(struct inode *inode)
     143                 :            : {
     144                 :          0 :         if (!IS_RDONLY(inode))
     145                 :          0 :                 fuse_invalidate_attr_mask(inode, STATX_ATIME);
     146                 :          0 : }
     147                 :            : 
     148                 :            : /*
     149                 :            :  * Just mark the entry as stale, so that a next attempt to look it up
     150                 :            :  * will result in a new lookup call to userspace
     151                 :            :  *
     152                 :            :  * This is called when a dentry is about to become negative and the
     153                 :            :  * timeout is unknown (unlink, rmdir, rename and in some cases
     154                 :            :  * lookup)
     155                 :            :  */
     156                 :          0 : void fuse_invalidate_entry_cache(struct dentry *entry)
     157                 :            : {
     158                 :          3 :         fuse_dentry_settime(entry, 0);
     159                 :          0 : }
     160                 :            : 
     161                 :            : /*
     162                 :            :  * Same as fuse_invalidate_entry_cache(), but also try to remove the
     163                 :            :  * dentry from the hash
     164                 :            :  */
     165                 :          0 : static void fuse_invalidate_entry(struct dentry *entry)
     166                 :            : {
     167                 :          0 :         d_invalidate(entry);
     168                 :            :         fuse_invalidate_entry_cache(entry);
     169                 :          0 : }
     170                 :            : 
     171                 :            : static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
     172                 :            :                              u64 nodeid, const struct qstr *name,
     173                 :            :                              struct fuse_entry_out *outarg)
     174                 :            : {
     175                 :          3 :         memset(outarg, 0, sizeof(struct fuse_entry_out));
     176                 :          3 :         args->opcode = FUSE_LOOKUP;
     177                 :          3 :         args->nodeid = nodeid;
     178                 :          3 :         args->in_numargs = 1;
     179                 :          3 :         args->in_args[0].size = name->len + 1;
     180                 :          3 :         args->in_args[0].value = name->name;
     181                 :          3 :         args->out_numargs = 1;
     182                 :          3 :         args->out_args[0].size = sizeof(struct fuse_entry_out);
     183                 :          3 :         args->out_args[0].value = outarg;
     184                 :            : }
     185                 :            : 
     186                 :            : /*
     187                 :            :  * Check whether the dentry is still valid
     188                 :            :  *
     189                 :            :  * If the entry validity timeout has expired and the dentry is
     190                 :            :  * positive, try to redo the lookup.  If the lookup results in a
     191                 :            :  * different inode, then let the VFS invalidate the dentry and redo
     192                 :            :  * the lookup once more.  If the lookup results in the same inode,
     193                 :            :  * then refresh the attributes, timeouts and mark the dentry valid.
     194                 :            :  */
     195                 :          3 : static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
     196                 :            : {
     197                 :            :         struct inode *inode;
     198                 :            :         struct dentry *parent;
     199                 :            :         struct fuse_conn *fc;
     200                 :            :         struct fuse_inode *fi;
     201                 :            :         int ret;
     202                 :            : 
     203                 :            :         inode = d_inode_rcu(entry);
     204                 :          3 :         if (inode && is_bad_inode(inode))
     205                 :            :                 goto invalid;
     206                 :          3 :         else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
     207                 :          0 :                  (flags & LOOKUP_REVAL)) {
     208                 :            :                 struct fuse_entry_out outarg;
     209                 :          3 :                 FUSE_ARGS(args);
     210                 :            :                 struct fuse_forget_link *forget;
     211                 :            :                 u64 attr_version;
     212                 :            : 
     213                 :            :                 /* For negative dentries, always do a fresh lookup */
     214                 :          3 :                 if (!inode)
     215                 :            :                         goto invalid;
     216                 :            : 
     217                 :            :                 ret = -ECHILD;
     218                 :          0 :                 if (flags & LOOKUP_RCU)
     219                 :            :                         goto out;
     220                 :            : 
     221                 :            :                 fc = get_fuse_conn(inode);
     222                 :            : 
     223                 :          0 :                 forget = fuse_alloc_forget();
     224                 :            :                 ret = -ENOMEM;
     225                 :          0 :                 if (!forget)
     226                 :            :                         goto out;
     227                 :            : 
     228                 :            :                 attr_version = fuse_get_attr_version(fc);
     229                 :            : 
     230                 :          0 :                 parent = dget_parent(entry);
     231                 :            :                 fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
     232                 :            :                                  &entry->d_name, &outarg);
     233                 :          0 :                 ret = fuse_simple_request(fc, &args);
     234                 :          0 :                 dput(parent);
     235                 :            :                 /* Zero nodeid is same as -ENOENT */
     236                 :          0 :                 if (!ret && !outarg.nodeid)
     237                 :            :                         ret = -ENOENT;
     238                 :          0 :                 if (!ret) {
     239                 :            :                         fi = get_fuse_inode(inode);
     240                 :          0 :                         if (outarg.nodeid != get_node_id(inode)) {
     241                 :          0 :                                 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
     242                 :          0 :                                 goto invalid;
     243                 :            :                         }
     244                 :            :                         spin_lock(&fi->lock);
     245                 :          0 :                         fi->nlookup++;
     246                 :            :                         spin_unlock(&fi->lock);
     247                 :            :                 }
     248                 :          0 :                 kfree(forget);
     249                 :          0 :                 if (ret == -ENOMEM)
     250                 :            :                         goto out;
     251                 :          0 :                 if (ret || fuse_invalid_attr(&outarg.attr) ||
     252                 :          0 :                     (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
     253                 :            :                         goto invalid;
     254                 :            : 
     255                 :          0 :                 forget_all_cached_acls(inode);
     256                 :          0 :                 fuse_change_attributes(inode, &outarg.attr,
     257                 :            :                                        entry_attr_timeout(&outarg),
     258                 :            :                                        attr_version);
     259                 :          0 :                 fuse_change_entry_timeout(entry, &outarg);
     260                 :          0 :         } else if (inode) {
     261                 :            :                 fi = get_fuse_inode(inode);
     262                 :          0 :                 if (flags & LOOKUP_RCU) {
     263                 :          0 :                         if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
     264                 :            :                                 return -ECHILD;
     265                 :          0 :                 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
     266                 :          0 :                         parent = dget_parent(entry);
     267                 :            :                         fuse_advise_use_readdirplus(d_inode(parent));
     268                 :          0 :                         dput(parent);
     269                 :            :                 }
     270                 :            :         }
     271                 :            :         ret = 1;
     272                 :            : out:
     273                 :          3 :         return ret;
     274                 :            : 
     275                 :            : invalid:
     276                 :            :         ret = 0;
     277                 :            :         goto out;
     278                 :            : }
     279                 :            : 
     280                 :            : #if BITS_PER_LONG < 64
     281                 :          3 : static int fuse_dentry_init(struct dentry *dentry)
     282                 :            : {
     283                 :          3 :         dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry),
     284                 :            :                                    GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
     285                 :            : 
     286                 :          3 :         return dentry->d_fsdata ? 0 : -ENOMEM;
     287                 :            : }
     288                 :          3 : static void fuse_dentry_release(struct dentry *dentry)
     289                 :            : {
     290                 :          3 :         union fuse_dentry *fd = dentry->d_fsdata;
     291                 :            : 
     292                 :          3 :         kfree_rcu(fd, rcu);
     293                 :          3 : }
     294                 :            : #endif
     295                 :            : 
     296                 :          0 : static int fuse_dentry_delete(const struct dentry *dentry)
     297                 :            : {
     298                 :          0 :         return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
     299                 :            : }
     300                 :            : 
     301                 :            : const struct dentry_operations fuse_dentry_operations = {
     302                 :            :         .d_revalidate   = fuse_dentry_revalidate,
     303                 :            :         .d_delete       = fuse_dentry_delete,
     304                 :            : #if BITS_PER_LONG < 64
     305                 :            :         .d_init         = fuse_dentry_init,
     306                 :            :         .d_release      = fuse_dentry_release,
     307                 :            : #endif
     308                 :            : };
     309                 :            : 
     310                 :            : const struct dentry_operations fuse_root_dentry_operations = {
     311                 :            : #if BITS_PER_LONG < 64
     312                 :            :         .d_init         = fuse_dentry_init,
     313                 :            :         .d_release      = fuse_dentry_release,
     314                 :            : #endif
     315                 :            : };
     316                 :            : 
     317                 :          3 : int fuse_valid_type(int m)
     318                 :            : {
     319                 :          3 :         return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
     320                 :          3 :                 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
     321                 :            : }
     322                 :            : 
     323                 :          0 : bool fuse_invalid_attr(struct fuse_attr *attr)
     324                 :            : {
     325                 :          0 :         return !fuse_valid_type(attr->mode) ||
     326                 :          0 :                 attr->size > LLONG_MAX;
     327                 :            : }
     328                 :            : 
     329                 :          3 : int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
     330                 :            :                      struct fuse_entry_out *outarg, struct inode **inode)
     331                 :            : {
     332                 :            :         struct fuse_conn *fc = get_fuse_conn_super(sb);
     333                 :          3 :         FUSE_ARGS(args);
     334                 :            :         struct fuse_forget_link *forget;
     335                 :            :         u64 attr_version;
     336                 :            :         int err;
     337                 :            : 
     338                 :          3 :         *inode = NULL;
     339                 :            :         err = -ENAMETOOLONG;
     340                 :          3 :         if (name->len > FUSE_NAME_MAX)
     341                 :            :                 goto out;
     342                 :            : 
     343                 :            : 
     344                 :          3 :         forget = fuse_alloc_forget();
     345                 :            :         err = -ENOMEM;
     346                 :          3 :         if (!forget)
     347                 :            :                 goto out;
     348                 :            : 
     349                 :            :         attr_version = fuse_get_attr_version(fc);
     350                 :            : 
     351                 :            :         fuse_lookup_init(fc, &args, nodeid, name, outarg);
     352                 :          3 :         err = fuse_simple_request(fc, &args);
     353                 :            :         /* Zero nodeid is same as -ENOENT, but with valid timeout */
     354                 :          3 :         if (err || !outarg->nodeid)
     355                 :            :                 goto out_put_forget;
     356                 :            : 
     357                 :            :         err = -EIO;
     358                 :          0 :         if (!outarg->nodeid)
     359                 :            :                 goto out_put_forget;
     360                 :          0 :         if (fuse_invalid_attr(&outarg->attr))
     361                 :            :                 goto out_put_forget;
     362                 :            : 
     363                 :          0 :         *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
     364                 :            :                            &outarg->attr, entry_attr_timeout(outarg),
     365                 :            :                            attr_version);
     366                 :            :         err = -ENOMEM;
     367                 :          0 :         if (!*inode) {
     368                 :          0 :                 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
     369                 :          0 :                 goto out;
     370                 :            :         }
     371                 :            :         err = 0;
     372                 :            : 
     373                 :            :  out_put_forget:
     374                 :          3 :         kfree(forget);
     375                 :            :  out:
     376                 :          3 :         return err;
     377                 :            : }
     378                 :            : 
     379                 :          3 : static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
     380                 :            :                                   unsigned int flags)
     381                 :            : {
     382                 :            :         int err;
     383                 :            :         struct fuse_entry_out outarg;
     384                 :            :         struct inode *inode;
     385                 :            :         struct dentry *newent;
     386                 :            :         bool outarg_valid = true;
     387                 :            :         bool locked;
     388                 :            : 
     389                 :          3 :         locked = fuse_lock_inode(dir);
     390                 :          3 :         err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
     391                 :            :                                &outarg, &inode);
     392                 :          3 :         fuse_unlock_inode(dir, locked);
     393                 :          3 :         if (err == -ENOENT) {
     394                 :            :                 outarg_valid = false;
     395                 :            :                 err = 0;
     396                 :            :         }
     397                 :          3 :         if (err)
     398                 :            :                 goto out_err;
     399                 :            : 
     400                 :            :         err = -EIO;
     401                 :          3 :         if (inode && get_node_id(inode) == FUSE_ROOT_ID)
     402                 :            :                 goto out_iput;
     403                 :            : 
     404                 :          3 :         newent = d_splice_alias(inode, entry);
     405                 :            :         err = PTR_ERR(newent);
     406                 :          3 :         if (IS_ERR(newent))
     407                 :            :                 goto out_err;
     408                 :            : 
     409                 :          3 :         entry = newent ? newent : entry;
     410                 :          3 :         if (outarg_valid)
     411                 :          0 :                 fuse_change_entry_timeout(entry, &outarg);
     412                 :            :         else
     413                 :            :                 fuse_invalidate_entry_cache(entry);
     414                 :            : 
     415                 :          3 :         if (inode)
     416                 :            :                 fuse_advise_use_readdirplus(dir);
     417                 :          3 :         return newent;
     418                 :            : 
     419                 :            :  out_iput:
     420                 :          0 :         iput(inode);
     421                 :            :  out_err:
     422                 :          0 :         return ERR_PTR(err);
     423                 :            : }
     424                 :            : 
     425                 :            : /*
     426                 :            :  * Atomic create+open operation
     427                 :            :  *
     428                 :            :  * If the filesystem doesn't support this, then fall back to separate
     429                 :            :  * 'mknod' + 'open' requests.
     430                 :            :  */
     431                 :          0 : static int fuse_create_open(struct inode *dir, struct dentry *entry,
     432                 :            :                             struct file *file, unsigned flags,
     433                 :            :                             umode_t mode)
     434                 :            : {
     435                 :            :         int err;
     436                 :            :         struct inode *inode;
     437                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     438                 :          0 :         FUSE_ARGS(args);
     439                 :            :         struct fuse_forget_link *forget;
     440                 :            :         struct fuse_create_in inarg;
     441                 :            :         struct fuse_open_out outopen;
     442                 :            :         struct fuse_entry_out outentry;
     443                 :            :         struct fuse_inode *fi;
     444                 :            :         struct fuse_file *ff;
     445                 :            : 
     446                 :            :         /* Userspace expects S_IFREG in create mode */
     447                 :          0 :         BUG_ON((mode & S_IFMT) != S_IFREG);
     448                 :            : 
     449                 :          0 :         forget = fuse_alloc_forget();
     450                 :            :         err = -ENOMEM;
     451                 :          0 :         if (!forget)
     452                 :            :                 goto out_err;
     453                 :            : 
     454                 :            :         err = -ENOMEM;
     455                 :          0 :         ff = fuse_file_alloc(fc);
     456                 :          0 :         if (!ff)
     457                 :            :                 goto out_put_forget_req;
     458                 :            : 
     459                 :          0 :         if (!fc->dont_mask)
     460                 :          0 :                 mode &= ~current_umask();
     461                 :            : 
     462                 :          0 :         flags &= ~O_NOCTTY;
     463                 :          0 :         memset(&inarg, 0, sizeof(inarg));
     464                 :          0 :         memset(&outentry, 0, sizeof(outentry));
     465                 :          0 :         inarg.flags = flags;
     466                 :          0 :         inarg.mode = mode;
     467                 :          0 :         inarg.umask = current_umask();
     468                 :          0 :         args.opcode = FUSE_CREATE;
     469                 :          0 :         args.nodeid = get_node_id(dir);
     470                 :          0 :         args.in_numargs = 2;
     471                 :          0 :         args.in_args[0].size = sizeof(inarg);
     472                 :          0 :         args.in_args[0].value = &inarg;
     473                 :          0 :         args.in_args[1].size = entry->d_name.len + 1;
     474                 :          0 :         args.in_args[1].value = entry->d_name.name;
     475                 :          0 :         args.out_numargs = 2;
     476                 :          0 :         args.out_args[0].size = sizeof(outentry);
     477                 :          0 :         args.out_args[0].value = &outentry;
     478                 :          0 :         args.out_args[1].size = sizeof(outopen);
     479                 :          0 :         args.out_args[1].value = &outopen;
     480                 :          0 :         err = fuse_simple_request(fc, &args);
     481                 :          0 :         if (err)
     482                 :            :                 goto out_free_ff;
     483                 :            : 
     484                 :            :         err = -EIO;
     485                 :          0 :         if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
     486                 :          0 :             fuse_invalid_attr(&outentry.attr))
     487                 :            :                 goto out_free_ff;
     488                 :            : 
     489                 :          0 :         ff->fh = outopen.fh;
     490                 :          0 :         ff->nodeid = outentry.nodeid;
     491                 :          0 :         ff->open_flags = outopen.open_flags;
     492                 :          0 :         inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
     493                 :            :                           &outentry.attr, entry_attr_timeout(&outentry), 0);
     494                 :          0 :         if (!inode) {
     495                 :          0 :                 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
     496                 :          0 :                 fuse_sync_release(NULL, ff, flags);
     497                 :          0 :                 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
     498                 :            :                 err = -ENOMEM;
     499                 :          0 :                 goto out_err;
     500                 :            :         }
     501                 :          0 :         kfree(forget);
     502                 :          0 :         d_instantiate(entry, inode);
     503                 :          0 :         fuse_change_entry_timeout(entry, &outentry);
     504                 :          0 :         fuse_dir_changed(dir);
     505                 :          0 :         err = finish_open(file, entry, generic_file_open);
     506                 :          0 :         if (err) {
     507                 :            :                 fi = get_fuse_inode(inode);
     508                 :          0 :                 fuse_sync_release(fi, ff, flags);
     509                 :            :         } else {
     510                 :          0 :                 file->private_data = ff;
     511                 :          0 :                 fuse_finish_open(inode, file);
     512                 :            :         }
     513                 :          0 :         return err;
     514                 :            : 
     515                 :            : out_free_ff:
     516                 :          0 :         fuse_file_free(ff);
     517                 :            : out_put_forget_req:
     518                 :          0 :         kfree(forget);
     519                 :            : out_err:
     520                 :          0 :         return err;
     521                 :            : }
     522                 :            : 
     523                 :            : static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
     524                 :          0 : static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
     525                 :            :                             struct file *file, unsigned flags,
     526                 :            :                             umode_t mode)
     527                 :            : {
     528                 :            :         int err;
     529                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     530                 :            :         struct dentry *res = NULL;
     531                 :            : 
     532                 :          0 :         if (d_in_lookup(entry)) {
     533                 :          0 :                 res = fuse_lookup(dir, entry, 0);
     534                 :          0 :                 if (IS_ERR(res))
     535                 :          0 :                         return PTR_ERR(res);
     536                 :            : 
     537                 :          0 :                 if (res)
     538                 :            :                         entry = res;
     539                 :            :         }
     540                 :            : 
     541                 :          0 :         if (!(flags & O_CREAT) || d_really_is_positive(entry))
     542                 :            :                 goto no_open;
     543                 :            : 
     544                 :            :         /* Only creates */
     545                 :          0 :         file->f_mode |= FMODE_CREATED;
     546                 :            : 
     547                 :          0 :         if (fc->no_create)
     548                 :            :                 goto mknod;
     549                 :            : 
     550                 :          0 :         err = fuse_create_open(dir, entry, file, flags, mode);
     551                 :          0 :         if (err == -ENOSYS) {
     552                 :          0 :                 fc->no_create = 1;
     553                 :          0 :                 goto mknod;
     554                 :            :         }
     555                 :            : out_dput:
     556                 :          0 :         dput(res);
     557                 :          0 :         return err;
     558                 :            : 
     559                 :            : mknod:
     560                 :          0 :         err = fuse_mknod(dir, entry, mode, 0);
     561                 :          0 :         if (err)
     562                 :            :                 goto out_dput;
     563                 :            : no_open:
     564                 :          0 :         return finish_no_open(file, res);
     565                 :            : }
     566                 :            : 
     567                 :            : /*
     568                 :            :  * Code shared between mknod, mkdir, symlink and link
     569                 :            :  */
     570                 :          0 : static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
     571                 :            :                             struct inode *dir, struct dentry *entry,
     572                 :            :                             umode_t mode)
     573                 :            : {
     574                 :            :         struct fuse_entry_out outarg;
     575                 :            :         struct inode *inode;
     576                 :            :         struct dentry *d;
     577                 :            :         int err;
     578                 :            :         struct fuse_forget_link *forget;
     579                 :            : 
     580                 :          0 :         forget = fuse_alloc_forget();
     581                 :          0 :         if (!forget)
     582                 :            :                 return -ENOMEM;
     583                 :            : 
     584                 :          0 :         memset(&outarg, 0, sizeof(outarg));
     585                 :          0 :         args->nodeid = get_node_id(dir);
     586                 :          0 :         args->out_numargs = 1;
     587                 :          0 :         args->out_args[0].size = sizeof(outarg);
     588                 :          0 :         args->out_args[0].value = &outarg;
     589                 :          0 :         err = fuse_simple_request(fc, args);
     590                 :          0 :         if (err)
     591                 :            :                 goto out_put_forget_req;
     592                 :            : 
     593                 :            :         err = -EIO;
     594                 :          0 :         if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
     595                 :            :                 goto out_put_forget_req;
     596                 :            : 
     597                 :          0 :         if ((outarg.attr.mode ^ mode) & S_IFMT)
     598                 :            :                 goto out_put_forget_req;
     599                 :            : 
     600                 :          0 :         inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
     601                 :            :                           &outarg.attr, entry_attr_timeout(&outarg), 0);
     602                 :          0 :         if (!inode) {
     603                 :          0 :                 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
     604                 :          0 :                 return -ENOMEM;
     605                 :            :         }
     606                 :          0 :         kfree(forget);
     607                 :            : 
     608                 :          0 :         d_drop(entry);
     609                 :          0 :         d = d_splice_alias(inode, entry);
     610                 :          0 :         if (IS_ERR(d))
     611                 :          0 :                 return PTR_ERR(d);
     612                 :            : 
     613                 :          0 :         if (d) {
     614                 :          0 :                 fuse_change_entry_timeout(d, &outarg);
     615                 :          0 :                 dput(d);
     616                 :            :         } else {
     617                 :          0 :                 fuse_change_entry_timeout(entry, &outarg);
     618                 :            :         }
     619                 :          0 :         fuse_dir_changed(dir);
     620                 :          0 :         return 0;
     621                 :            : 
     622                 :            :  out_put_forget_req:
     623                 :          0 :         kfree(forget);
     624                 :          0 :         return err;
     625                 :            : }
     626                 :            : 
     627                 :          0 : static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
     628                 :            :                       dev_t rdev)
     629                 :            : {
     630                 :            :         struct fuse_mknod_in inarg;
     631                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     632                 :          0 :         FUSE_ARGS(args);
     633                 :            : 
     634                 :          0 :         if (!fc->dont_mask)
     635                 :          0 :                 mode &= ~current_umask();
     636                 :            : 
     637                 :          0 :         memset(&inarg, 0, sizeof(inarg));
     638                 :          0 :         inarg.mode = mode;
     639                 :          0 :         inarg.rdev = new_encode_dev(rdev);
     640                 :          0 :         inarg.umask = current_umask();
     641                 :          0 :         args.opcode = FUSE_MKNOD;
     642                 :          0 :         args.in_numargs = 2;
     643                 :          0 :         args.in_args[0].size = sizeof(inarg);
     644                 :          0 :         args.in_args[0].value = &inarg;
     645                 :          0 :         args.in_args[1].size = entry->d_name.len + 1;
     646                 :          0 :         args.in_args[1].value = entry->d_name.name;
     647                 :          0 :         return create_new_entry(fc, &args, dir, entry, mode);
     648                 :            : }
     649                 :            : 
     650                 :          0 : static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
     651                 :            :                        bool excl)
     652                 :            : {
     653                 :          0 :         return fuse_mknod(dir, entry, mode, 0);
     654                 :            : }
     655                 :            : 
     656                 :          0 : static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
     657                 :            : {
     658                 :            :         struct fuse_mkdir_in inarg;
     659                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     660                 :          0 :         FUSE_ARGS(args);
     661                 :            : 
     662                 :          0 :         if (!fc->dont_mask)
     663                 :          0 :                 mode &= ~current_umask();
     664                 :            : 
     665                 :          0 :         memset(&inarg, 0, sizeof(inarg));
     666                 :          0 :         inarg.mode = mode;
     667                 :          0 :         inarg.umask = current_umask();
     668                 :          0 :         args.opcode = FUSE_MKDIR;
     669                 :          0 :         args.in_numargs = 2;
     670                 :          0 :         args.in_args[0].size = sizeof(inarg);
     671                 :          0 :         args.in_args[0].value = &inarg;
     672                 :          0 :         args.in_args[1].size = entry->d_name.len + 1;
     673                 :          0 :         args.in_args[1].value = entry->d_name.name;
     674                 :          0 :         return create_new_entry(fc, &args, dir, entry, S_IFDIR);
     675                 :            : }
     676                 :            : 
     677                 :          0 : static int fuse_symlink(struct inode *dir, struct dentry *entry,
     678                 :            :                         const char *link)
     679                 :            : {
     680                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     681                 :          0 :         unsigned len = strlen(link) + 1;
     682                 :          0 :         FUSE_ARGS(args);
     683                 :            : 
     684                 :          0 :         args.opcode = FUSE_SYMLINK;
     685                 :          0 :         args.in_numargs = 2;
     686                 :          0 :         args.in_args[0].size = entry->d_name.len + 1;
     687                 :          0 :         args.in_args[0].value = entry->d_name.name;
     688                 :          0 :         args.in_args[1].size = len;
     689                 :          0 :         args.in_args[1].value = link;
     690                 :          0 :         return create_new_entry(fc, &args, dir, entry, S_IFLNK);
     691                 :            : }
     692                 :            : 
     693                 :          0 : void fuse_update_ctime(struct inode *inode)
     694                 :            : {
     695                 :          0 :         if (!IS_NOCMTIME(inode)) {
     696                 :          0 :                 inode->i_ctime = current_time(inode);
     697                 :            :                 mark_inode_dirty_sync(inode);
     698                 :            :         }
     699                 :          0 : }
     700                 :            : 
     701                 :          0 : static int fuse_unlink(struct inode *dir, struct dentry *entry)
     702                 :            : {
     703                 :            :         int err;
     704                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     705                 :          0 :         FUSE_ARGS(args);
     706                 :            : 
     707                 :          0 :         args.opcode = FUSE_UNLINK;
     708                 :          0 :         args.nodeid = get_node_id(dir);
     709                 :          0 :         args.in_numargs = 1;
     710                 :          0 :         args.in_args[0].size = entry->d_name.len + 1;
     711                 :          0 :         args.in_args[0].value = entry->d_name.name;
     712                 :          0 :         err = fuse_simple_request(fc, &args);
     713                 :          0 :         if (!err) {
     714                 :            :                 struct inode *inode = d_inode(entry);
     715                 :            :                 struct fuse_inode *fi = get_fuse_inode(inode);
     716                 :            : 
     717                 :            :                 spin_lock(&fi->lock);
     718                 :          0 :                 fi->attr_version = atomic64_inc_return(&fc->attr_version);
     719                 :            :                 /*
     720                 :            :                  * If i_nlink == 0 then unlink doesn't make sense, yet this can
     721                 :            :                  * happen if userspace filesystem is careless.  It would be
     722                 :            :                  * difficult to enforce correct nlink usage so just ignore this
     723                 :            :                  * condition here
     724                 :            :                  */
     725                 :          0 :                 if (inode->i_nlink > 0)
     726                 :          0 :                         drop_nlink(inode);
     727                 :            :                 spin_unlock(&fi->lock);
     728                 :            :                 fuse_invalidate_attr(inode);
     729                 :          0 :                 fuse_dir_changed(dir);
     730                 :            :                 fuse_invalidate_entry_cache(entry);
     731                 :          0 :                 fuse_update_ctime(inode);
     732                 :          0 :         } else if (err == -EINTR)
     733                 :          0 :                 fuse_invalidate_entry(entry);
     734                 :          0 :         return err;
     735                 :            : }
     736                 :            : 
     737                 :          0 : static int fuse_rmdir(struct inode *dir, struct dentry *entry)
     738                 :            : {
     739                 :            :         int err;
     740                 :            :         struct fuse_conn *fc = get_fuse_conn(dir);
     741                 :          0 :         FUSE_ARGS(args);
     742                 :            : 
     743                 :          0 :         args.opcode = FUSE_RMDIR;
     744                 :          0 :         args.nodeid = get_node_id(dir);
     745                 :          0 :         args.in_numargs = 1;
     746                 :          0 :         args.in_args[0].size = entry->d_name.len + 1;
     747                 :          0 :         args.in_args[0].value = entry->d_name.name;
     748                 :          0 :         err = fuse_simple_request(fc, &args);
     749                 :          0 :         if (!err) {
     750                 :          0 :                 clear_nlink(d_inode(entry));
     751                 :          0 :                 fuse_dir_changed(dir);
     752                 :            :                 fuse_invalidate_entry_cache(entry);
     753                 :          0 :         } else if (err == -EINTR)
     754                 :          0 :                 fuse_invalidate_entry(entry);
     755                 :          0 :         return err;
     756                 :            : }
     757                 :            : 
     758                 :          0 : static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
     759                 :            :                               struct inode *newdir, struct dentry *newent,
     760                 :            :                               unsigned int flags, int opcode, size_t argsize)
     761                 :            : {
     762                 :            :         int err;
     763                 :            :         struct fuse_rename2_in inarg;
     764                 :            :         struct fuse_conn *fc = get_fuse_conn(olddir);
     765                 :          0 :         FUSE_ARGS(args);
     766                 :            : 
     767                 :          0 :         memset(&inarg, 0, argsize);
     768                 :          0 :         inarg.newdir = get_node_id(newdir);
     769                 :          0 :         inarg.flags = flags;
     770                 :          0 :         args.opcode = opcode;
     771                 :          0 :         args.nodeid = get_node_id(olddir);
     772                 :          0 :         args.in_numargs = 3;
     773                 :          0 :         args.in_args[0].size = argsize;
     774                 :          0 :         args.in_args[0].value = &inarg;
     775                 :          0 :         args.in_args[1].size = oldent->d_name.len + 1;
     776                 :          0 :         args.in_args[1].value = oldent->d_name.name;
     777                 :          0 :         args.in_args[2].size = newent->d_name.len + 1;
     778                 :          0 :         args.in_args[2].value = newent->d_name.name;
     779                 :          0 :         err = fuse_simple_request(fc, &args);
     780                 :          0 :         if (!err) {
     781                 :            :                 /* ctime changes */
     782                 :            :                 fuse_invalidate_attr(d_inode(oldent));
     783                 :          0 :                 fuse_update_ctime(d_inode(oldent));
     784                 :            : 
     785                 :          0 :                 if (flags & RENAME_EXCHANGE) {
     786                 :            :                         fuse_invalidate_attr(d_inode(newent));
     787                 :          0 :                         fuse_update_ctime(d_inode(newent));
     788                 :            :                 }
     789                 :            : 
     790                 :          0 :                 fuse_dir_changed(olddir);
     791                 :          0 :                 if (olddir != newdir)
     792                 :          0 :                         fuse_dir_changed(newdir);
     793                 :            : 
     794                 :            :                 /* newent will end up negative */
     795                 :          0 :                 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
     796                 :            :                         fuse_invalidate_attr(d_inode(newent));
     797                 :            :                         fuse_invalidate_entry_cache(newent);
     798                 :          0 :                         fuse_update_ctime(d_inode(newent));
     799                 :            :                 }
     800                 :          0 :         } else if (err == -EINTR) {
     801                 :            :                 /* If request was interrupted, DEITY only knows if the
     802                 :            :                    rename actually took place.  If the invalidation
     803                 :            :                    fails (e.g. some process has CWD under the renamed
     804                 :            :                    directory), then there can be inconsistency between
     805                 :            :                    the dcache and the real filesystem.  Tough luck. */
     806                 :          0 :                 fuse_invalidate_entry(oldent);
     807                 :          0 :                 if (d_really_is_positive(newent))
     808                 :          0 :                         fuse_invalidate_entry(newent);
     809                 :            :         }
     810                 :            : 
     811                 :          0 :         return err;
     812                 :            : }
     813                 :            : 
     814                 :          0 : static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
     815                 :            :                         struct inode *newdir, struct dentry *newent,
     816                 :            :                         unsigned int flags)
     817                 :            : {
     818                 :            :         struct fuse_conn *fc = get_fuse_conn(olddir);
     819                 :            :         int err;
     820                 :            : 
     821                 :          0 :         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
     822                 :            :                 return -EINVAL;
     823                 :            : 
     824                 :          0 :         if (flags) {
     825                 :          0 :                 if (fc->no_rename2 || fc->minor < 23)
     826                 :            :                         return -EINVAL;
     827                 :            : 
     828                 :          0 :                 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
     829                 :            :                                          FUSE_RENAME2,
     830                 :            :                                          sizeof(struct fuse_rename2_in));
     831                 :          0 :                 if (err == -ENOSYS) {
     832                 :          0 :                         fc->no_rename2 = 1;
     833                 :            :                         err = -EINVAL;
     834                 :            :                 }
     835                 :            :         } else {
     836                 :          0 :                 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
     837                 :            :                                          FUSE_RENAME,
     838                 :            :                                          sizeof(struct fuse_rename_in));
     839                 :            :         }
     840                 :            : 
     841                 :          0 :         return err;
     842                 :            : }
     843                 :            : 
     844                 :          0 : static int fuse_link(struct dentry *entry, struct inode *newdir,
     845                 :            :                      struct dentry *newent)
     846                 :            : {
     847                 :            :         int err;
     848                 :            :         struct fuse_link_in inarg;
     849                 :            :         struct inode *inode = d_inode(entry);
     850                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
     851                 :          0 :         FUSE_ARGS(args);
     852                 :            : 
     853                 :          0 :         memset(&inarg, 0, sizeof(inarg));
     854                 :          0 :         inarg.oldnodeid = get_node_id(inode);
     855                 :          0 :         args.opcode = FUSE_LINK;
     856                 :          0 :         args.in_numargs = 2;
     857                 :          0 :         args.in_args[0].size = sizeof(inarg);
     858                 :          0 :         args.in_args[0].value = &inarg;
     859                 :          0 :         args.in_args[1].size = newent->d_name.len + 1;
     860                 :          0 :         args.in_args[1].value = newent->d_name.name;
     861                 :          0 :         err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
     862                 :            :         /* Contrary to "normal" filesystems it can happen that link
     863                 :            :            makes two "logical" inodes point to the same "physical"
     864                 :            :            inode.  We invalidate the attributes of the old one, so it
     865                 :            :            will reflect changes in the backing inode (link count,
     866                 :            :            etc.)
     867                 :            :         */
     868                 :          0 :         if (!err) {
     869                 :            :                 struct fuse_inode *fi = get_fuse_inode(inode);
     870                 :            : 
     871                 :            :                 spin_lock(&fi->lock);
     872                 :          0 :                 fi->attr_version = atomic64_inc_return(&fc->attr_version);
     873                 :          0 :                 if (likely(inode->i_nlink < UINT_MAX))
     874                 :          0 :                         inc_nlink(inode);
     875                 :            :                 spin_unlock(&fi->lock);
     876                 :            :                 fuse_invalidate_attr(inode);
     877                 :          0 :                 fuse_update_ctime(inode);
     878                 :          0 :         } else if (err == -EINTR) {
     879                 :            :                 fuse_invalidate_attr(inode);
     880                 :            :         }
     881                 :          0 :         return err;
     882                 :            : }
     883                 :            : 
     884                 :          0 : static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
     885                 :            :                           struct kstat *stat)
     886                 :            : {
     887                 :            :         unsigned int blkbits;
     888                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
     889                 :            : 
     890                 :            :         /* see the comment in fuse_change_attributes() */
     891                 :          0 :         if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
     892                 :          0 :                 attr->size = i_size_read(inode);
     893                 :          0 :                 attr->mtime = inode->i_mtime.tv_sec;
     894                 :          0 :                 attr->mtimensec = inode->i_mtime.tv_nsec;
     895                 :          0 :                 attr->ctime = inode->i_ctime.tv_sec;
     896                 :          0 :                 attr->ctimensec = inode->i_ctime.tv_nsec;
     897                 :            :         }
     898                 :            : 
     899                 :          0 :         stat->dev = inode->i_sb->s_dev;
     900                 :          0 :         stat->ino = attr->ino;
     901                 :          0 :         stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
     902                 :          0 :         stat->nlink = attr->nlink;
     903                 :          0 :         stat->uid = make_kuid(fc->user_ns, attr->uid);
     904                 :          0 :         stat->gid = make_kgid(fc->user_ns, attr->gid);
     905                 :          0 :         stat->rdev = inode->i_rdev;
     906                 :          0 :         stat->atime.tv_sec = attr->atime;
     907                 :          0 :         stat->atime.tv_nsec = attr->atimensec;
     908                 :          0 :         stat->mtime.tv_sec = attr->mtime;
     909                 :          0 :         stat->mtime.tv_nsec = attr->mtimensec;
     910                 :          0 :         stat->ctime.tv_sec = attr->ctime;
     911                 :          0 :         stat->ctime.tv_nsec = attr->ctimensec;
     912                 :          0 :         stat->size = attr->size;
     913                 :          0 :         stat->blocks = attr->blocks;
     914                 :            : 
     915                 :          0 :         if (attr->blksize != 0)
     916                 :          0 :                 blkbits = ilog2(attr->blksize);
     917                 :            :         else
     918                 :          0 :                 blkbits = inode->i_sb->s_blocksize_bits;
     919                 :            : 
     920                 :          0 :         stat->blksize = 1 << blkbits;
     921                 :          0 : }
     922                 :            : 
     923                 :          0 : static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
     924                 :            :                            struct file *file)
     925                 :            : {
     926                 :            :         int err;
     927                 :            :         struct fuse_getattr_in inarg;
     928                 :            :         struct fuse_attr_out outarg;
     929                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
     930                 :          0 :         FUSE_ARGS(args);
     931                 :            :         u64 attr_version;
     932                 :            : 
     933                 :            :         attr_version = fuse_get_attr_version(fc);
     934                 :            : 
     935                 :          0 :         memset(&inarg, 0, sizeof(inarg));
     936                 :          0 :         memset(&outarg, 0, sizeof(outarg));
     937                 :            :         /* Directories have separate file-handle space */
     938                 :          0 :         if (file && S_ISREG(inode->i_mode)) {
     939                 :          0 :                 struct fuse_file *ff = file->private_data;
     940                 :            : 
     941                 :          0 :                 inarg.getattr_flags |= FUSE_GETATTR_FH;
     942                 :          0 :                 inarg.fh = ff->fh;
     943                 :            :         }
     944                 :          0 :         args.opcode = FUSE_GETATTR;
     945                 :          0 :         args.nodeid = get_node_id(inode);
     946                 :          0 :         args.in_numargs = 1;
     947                 :          0 :         args.in_args[0].size = sizeof(inarg);
     948                 :          0 :         args.in_args[0].value = &inarg;
     949                 :          0 :         args.out_numargs = 1;
     950                 :          0 :         args.out_args[0].size = sizeof(outarg);
     951                 :          0 :         args.out_args[0].value = &outarg;
     952                 :          0 :         err = fuse_simple_request(fc, &args);
     953                 :          0 :         if (!err) {
     954                 :          0 :                 if (fuse_invalid_attr(&outarg.attr) ||
     955                 :          0 :                     (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
     956                 :          0 :                         make_bad_inode(inode);
     957                 :          0 :                         err = -EIO;
     958                 :            :                 } else {
     959                 :          0 :                         fuse_change_attributes(inode, &outarg.attr,
     960                 :            :                                                attr_timeout(&outarg),
     961                 :            :                                                attr_version);
     962                 :          0 :                         if (stat)
     963                 :          0 :                                 fuse_fillattr(inode, &outarg.attr, stat);
     964                 :            :                 }
     965                 :            :         }
     966                 :          0 :         return err;
     967                 :            : }
     968                 :            : 
     969                 :          0 : static int fuse_update_get_attr(struct inode *inode, struct file *file,
     970                 :            :                                 struct kstat *stat, u32 request_mask,
     971                 :            :                                 unsigned int flags)
     972                 :            : {
     973                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
     974                 :            :         int err = 0;
     975                 :            :         bool sync;
     976                 :            : 
     977                 :          0 :         if (flags & AT_STATX_FORCE_SYNC)
     978                 :            :                 sync = true;
     979                 :          0 :         else if (flags & AT_STATX_DONT_SYNC)
     980                 :            :                 sync = false;
     981                 :          0 :         else if (request_mask & READ_ONCE(fi->inval_mask))
     982                 :            :                 sync = true;
     983                 :            :         else
     984                 :          0 :                 sync = time_before64(fi->i_time, get_jiffies_64());
     985                 :            : 
     986                 :          0 :         if (sync) {
     987                 :          0 :                 forget_all_cached_acls(inode);
     988                 :          0 :                 err = fuse_do_getattr(inode, stat, file);
     989                 :          0 :         } else if (stat) {
     990                 :          0 :                 generic_fillattr(inode, stat);
     991                 :          0 :                 stat->mode = fi->orig_i_mode;
     992                 :          0 :                 stat->ino = fi->orig_ino;
     993                 :            :         }
     994                 :            : 
     995                 :          0 :         return err;
     996                 :            : }
     997                 :            : 
     998                 :          0 : int fuse_update_attributes(struct inode *inode, struct file *file)
     999                 :            : {
    1000                 :            :         /* Do *not* need to get atime for internal purposes */
    1001                 :          0 :         return fuse_update_get_attr(inode, file, NULL,
    1002                 :            :                                     STATX_BASIC_STATS & ~STATX_ATIME, 0);
    1003                 :            : }
    1004                 :            : 
    1005                 :          0 : int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
    1006                 :            :                              u64 child_nodeid, struct qstr *name)
    1007                 :            : {
    1008                 :            :         int err = -ENOTDIR;
    1009                 :            :         struct inode *parent;
    1010                 :            :         struct dentry *dir;
    1011                 :            :         struct dentry *entry;
    1012                 :            : 
    1013                 :          0 :         parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
    1014                 :          0 :         if (!parent)
    1015                 :            :                 return -ENOENT;
    1016                 :            : 
    1017                 :            :         inode_lock(parent);
    1018                 :          0 :         if (!S_ISDIR(parent->i_mode))
    1019                 :            :                 goto unlock;
    1020                 :            : 
    1021                 :            :         err = -ENOENT;
    1022                 :          0 :         dir = d_find_alias(parent);
    1023                 :          0 :         if (!dir)
    1024                 :            :                 goto unlock;
    1025                 :            : 
    1026                 :          0 :         name->hash = full_name_hash(dir, name->name, name->len);
    1027                 :          0 :         entry = d_lookup(dir, name);
    1028                 :          0 :         dput(dir);
    1029                 :          0 :         if (!entry)
    1030                 :            :                 goto unlock;
    1031                 :            : 
    1032                 :          0 :         fuse_dir_changed(parent);
    1033                 :          0 :         fuse_invalidate_entry(entry);
    1034                 :            : 
    1035                 :          0 :         if (child_nodeid != 0 && d_really_is_positive(entry)) {
    1036                 :            :                 inode_lock(d_inode(entry));
    1037                 :          0 :                 if (get_node_id(d_inode(entry)) != child_nodeid) {
    1038                 :            :                         err = -ENOENT;
    1039                 :            :                         goto badentry;
    1040                 :            :                 }
    1041                 :          0 :                 if (d_mountpoint(entry)) {
    1042                 :            :                         err = -EBUSY;
    1043                 :            :                         goto badentry;
    1044                 :            :                 }
    1045                 :          0 :                 if (d_is_dir(entry)) {
    1046                 :          0 :                         shrink_dcache_parent(entry);
    1047                 :          0 :                         if (!simple_empty(entry)) {
    1048                 :            :                                 err = -ENOTEMPTY;
    1049                 :            :                                 goto badentry;
    1050                 :            :                         }
    1051                 :          0 :                         d_inode(entry)->i_flags |= S_DEAD;
    1052                 :            :                 }
    1053                 :          0 :                 dont_mount(entry);
    1054                 :          0 :                 clear_nlink(d_inode(entry));
    1055                 :            :                 err = 0;
    1056                 :            :  badentry:
    1057                 :            :                 inode_unlock(d_inode(entry));
    1058                 :          0 :                 if (!err)
    1059                 :          0 :                         d_delete(entry);
    1060                 :            :         } else {
    1061                 :            :                 err = 0;
    1062                 :            :         }
    1063                 :          0 :         dput(entry);
    1064                 :            : 
    1065                 :            :  unlock:
    1066                 :            :         inode_unlock(parent);
    1067                 :          0 :         iput(parent);
    1068                 :          0 :         return err;
    1069                 :            : }
    1070                 :            : 
    1071                 :            : /*
    1072                 :            :  * Calling into a user-controlled filesystem gives the filesystem
    1073                 :            :  * daemon ptrace-like capabilities over the current process.  This
    1074                 :            :  * means, that the filesystem daemon is able to record the exact
    1075                 :            :  * filesystem operations performed, and can also control the behavior
    1076                 :            :  * of the requester process in otherwise impossible ways.  For example
    1077                 :            :  * it can delay the operation for arbitrary length of time allowing
    1078                 :            :  * DoS against the requester.
    1079                 :            :  *
    1080                 :            :  * For this reason only those processes can call into the filesystem,
    1081                 :            :  * for which the owner of the mount has ptrace privilege.  This
    1082                 :            :  * excludes processes started by other users, suid or sgid processes.
    1083                 :            :  */
    1084                 :          3 : int fuse_allow_current_process(struct fuse_conn *fc)
    1085                 :            : {
    1086                 :            :         const struct cred *cred;
    1087                 :            : 
    1088                 :          3 :         if (fc->allow_other)
    1089                 :          0 :                 return current_in_userns(fc->user_ns);
    1090                 :            : 
    1091                 :          3 :         cred = current_cred();
    1092                 :          3 :         if (uid_eq(cred->euid, fc->user_id) &&
    1093                 :          3 :             uid_eq(cred->suid, fc->user_id) &&
    1094                 :          3 :             uid_eq(cred->uid,  fc->user_id) &&
    1095                 :          3 :             gid_eq(cred->egid, fc->group_id) &&
    1096                 :          3 :             gid_eq(cred->sgid, fc->group_id) &&
    1097                 :            :             gid_eq(cred->gid,  fc->group_id))
    1098                 :            :                 return 1;
    1099                 :            : 
    1100                 :            :         return 0;
    1101                 :            : }
    1102                 :            : 
    1103                 :          3 : static int fuse_access(struct inode *inode, int mask)
    1104                 :            : {
    1105                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1106                 :          3 :         FUSE_ARGS(args);
    1107                 :            :         struct fuse_access_in inarg;
    1108                 :            :         int err;
    1109                 :            : 
    1110                 :          3 :         BUG_ON(mask & MAY_NOT_BLOCK);
    1111                 :            : 
    1112                 :          3 :         if (fc->no_access)
    1113                 :            :                 return 0;
    1114                 :            : 
    1115                 :          3 :         memset(&inarg, 0, sizeof(inarg));
    1116                 :          3 :         inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
    1117                 :          3 :         args.opcode = FUSE_ACCESS;
    1118                 :          3 :         args.nodeid = get_node_id(inode);
    1119                 :          3 :         args.in_numargs = 1;
    1120                 :          3 :         args.in_args[0].size = sizeof(inarg);
    1121                 :          3 :         args.in_args[0].value = &inarg;
    1122                 :          3 :         err = fuse_simple_request(fc, &args);
    1123                 :          3 :         if (err == -ENOSYS) {
    1124                 :          0 :                 fc->no_access = 1;
    1125                 :            :                 err = 0;
    1126                 :            :         }
    1127                 :          3 :         return err;
    1128                 :            : }
    1129                 :            : 
    1130                 :          0 : static int fuse_perm_getattr(struct inode *inode, int mask)
    1131                 :            : {
    1132                 :          0 :         if (mask & MAY_NOT_BLOCK)
    1133                 :            :                 return -ECHILD;
    1134                 :            : 
    1135                 :          0 :         forget_all_cached_acls(inode);
    1136                 :          0 :         return fuse_do_getattr(inode, NULL, NULL);
    1137                 :            : }
    1138                 :            : 
    1139                 :            : /*
    1140                 :            :  * Check permission.  The two basic access models of FUSE are:
    1141                 :            :  *
    1142                 :            :  * 1) Local access checking ('default_permissions' mount option) based
    1143                 :            :  * on file mode.  This is the plain old disk filesystem permission
    1144                 :            :  * modell.
    1145                 :            :  *
    1146                 :            :  * 2) "Remote" access checking, where server is responsible for
    1147                 :            :  * checking permission in each inode operation.  An exception to this
    1148                 :            :  * is if ->permission() was invoked from sys_access() in which case an
    1149                 :            :  * access request is sent.  Execute permission is still checked
    1150                 :            :  * locally based on file mode.
    1151                 :            :  */
    1152                 :          3 : static int fuse_permission(struct inode *inode, int mask)
    1153                 :            : {
    1154                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1155                 :            :         bool refreshed = false;
    1156                 :            :         int err = 0;
    1157                 :            : 
    1158                 :          3 :         if (!fuse_allow_current_process(fc))
    1159                 :            :                 return -EACCES;
    1160                 :            : 
    1161                 :            :         /*
    1162                 :            :          * If attributes are needed, refresh them before proceeding
    1163                 :            :          */
    1164                 :          3 :         if (fc->default_permissions ||
    1165                 :          3 :             ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
    1166                 :            :                 struct fuse_inode *fi = get_fuse_inode(inode);
    1167                 :            :                 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
    1168                 :            : 
    1169                 :          0 :                 if (perm_mask & READ_ONCE(fi->inval_mask) ||
    1170                 :          0 :                     time_before64(fi->i_time, get_jiffies_64())) {
    1171                 :            :                         refreshed = true;
    1172                 :            : 
    1173                 :          0 :                         err = fuse_perm_getattr(inode, mask);
    1174                 :          0 :                         if (err)
    1175                 :            :                                 return err;
    1176                 :            :                 }
    1177                 :            :         }
    1178                 :            : 
    1179                 :          3 :         if (fc->default_permissions) {
    1180                 :          0 :                 err = generic_permission(inode, mask);
    1181                 :            : 
    1182                 :            :                 /* If permission is denied, try to refresh file
    1183                 :            :                    attributes.  This is also needed, because the root
    1184                 :            :                    node will at first have no permissions */
    1185                 :          0 :                 if (err == -EACCES && !refreshed) {
    1186                 :          0 :                         err = fuse_perm_getattr(inode, mask);
    1187                 :          0 :                         if (!err)
    1188                 :          0 :                                 err = generic_permission(inode, mask);
    1189                 :            :                 }
    1190                 :            : 
    1191                 :            :                 /* Note: the opposite of the above test does not
    1192                 :            :                    exist.  So if permissions are revoked this won't be
    1193                 :            :                    noticed immediately, only after the attribute
    1194                 :            :                    timeout has expired */
    1195                 :          3 :         } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
    1196                 :          3 :                 err = fuse_access(inode, mask);
    1197                 :          3 :         } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
    1198                 :          0 :                 if (!(inode->i_mode & S_IXUGO)) {
    1199                 :          0 :                         if (refreshed)
    1200                 :            :                                 return -EACCES;
    1201                 :            : 
    1202                 :          0 :                         err = fuse_perm_getattr(inode, mask);
    1203                 :          0 :                         if (!err && !(inode->i_mode & S_IXUGO))
    1204                 :            :                                 return -EACCES;
    1205                 :            :                 }
    1206                 :            :         }
    1207                 :          3 :         return err;
    1208                 :            : }
    1209                 :            : 
    1210                 :          0 : static int fuse_readlink_page(struct inode *inode, struct page *page)
    1211                 :            : {
    1212                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1213                 :          0 :         struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
    1214                 :          0 :         struct fuse_args_pages ap = {
    1215                 :            :                 .num_pages = 1,
    1216                 :            :                 .pages = &page,
    1217                 :            :                 .descs = &desc,
    1218                 :            :         };
    1219                 :            :         char *link;
    1220                 :            :         ssize_t res;
    1221                 :            : 
    1222                 :          0 :         ap.args.opcode = FUSE_READLINK;
    1223                 :          0 :         ap.args.nodeid = get_node_id(inode);
    1224                 :          0 :         ap.args.out_pages = true;
    1225                 :          0 :         ap.args.out_argvar = true;
    1226                 :          0 :         ap.args.page_zeroing = true;
    1227                 :          0 :         ap.args.out_numargs = 1;
    1228                 :          0 :         ap.args.out_args[0].size = desc.length;
    1229                 :          0 :         res = fuse_simple_request(fc, &ap.args);
    1230                 :            : 
    1231                 :          0 :         fuse_invalidate_atime(inode);
    1232                 :            : 
    1233                 :          0 :         if (res < 0)
    1234                 :            :                 return res;
    1235                 :            : 
    1236                 :          0 :         if (WARN_ON(res >= PAGE_SIZE))
    1237                 :            :                 return -EIO;
    1238                 :            : 
    1239                 :          0 :         link = page_address(page);
    1240                 :          0 :         link[res] = '\0';
    1241                 :            : 
    1242                 :          0 :         return 0;
    1243                 :            : }
    1244                 :            : 
    1245                 :          0 : static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
    1246                 :            :                                  struct delayed_call *callback)
    1247                 :            : {
    1248                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1249                 :            :         struct page *page;
    1250                 :            :         int err;
    1251                 :            : 
    1252                 :            :         err = -EIO;
    1253                 :          0 :         if (is_bad_inode(inode))
    1254                 :            :                 goto out_err;
    1255                 :            : 
    1256                 :          0 :         if (fc->cache_symlinks)
    1257                 :          0 :                 return page_get_link(dentry, inode, callback);
    1258                 :            : 
    1259                 :            :         err = -ECHILD;
    1260                 :          0 :         if (!dentry)
    1261                 :            :                 goto out_err;
    1262                 :            : 
    1263                 :            :         page = alloc_page(GFP_KERNEL);
    1264                 :            :         err = -ENOMEM;
    1265                 :          0 :         if (!page)
    1266                 :            :                 goto out_err;
    1267                 :            : 
    1268                 :          0 :         err = fuse_readlink_page(inode, page);
    1269                 :          0 :         if (err) {
    1270                 :          0 :                 __free_page(page);
    1271                 :          0 :                 goto out_err;
    1272                 :            :         }
    1273                 :            : 
    1274                 :            :         set_delayed_call(callback, page_put_link, page);
    1275                 :            : 
    1276                 :          0 :         return page_address(page);
    1277                 :            : 
    1278                 :            : out_err:
    1279                 :          0 :         return ERR_PTR(err);
    1280                 :            : }
    1281                 :            : 
    1282                 :          0 : static int fuse_dir_open(struct inode *inode, struct file *file)
    1283                 :            : {
    1284                 :          0 :         return fuse_open_common(inode, file, true);
    1285                 :            : }
    1286                 :            : 
    1287                 :          0 : static int fuse_dir_release(struct inode *inode, struct file *file)
    1288                 :            : {
    1289                 :          0 :         fuse_release_common(file, true);
    1290                 :            : 
    1291                 :          0 :         return 0;
    1292                 :            : }
    1293                 :            : 
    1294                 :          0 : static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
    1295                 :            :                           int datasync)
    1296                 :            : {
    1297                 :          0 :         struct inode *inode = file->f_mapping->host;
    1298                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1299                 :            :         int err;
    1300                 :            : 
    1301                 :          0 :         if (is_bad_inode(inode))
    1302                 :            :                 return -EIO;
    1303                 :            : 
    1304                 :          0 :         if (fc->no_fsyncdir)
    1305                 :            :                 return 0;
    1306                 :            : 
    1307                 :            :         inode_lock(inode);
    1308                 :          0 :         err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
    1309                 :          0 :         if (err == -ENOSYS) {
    1310                 :          0 :                 fc->no_fsyncdir = 1;
    1311                 :            :                 err = 0;
    1312                 :            :         }
    1313                 :            :         inode_unlock(inode);
    1314                 :            : 
    1315                 :          0 :         return err;
    1316                 :            : }
    1317                 :            : 
    1318                 :          0 : static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
    1319                 :            :                             unsigned long arg)
    1320                 :            : {
    1321                 :          0 :         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
    1322                 :            : 
    1323                 :            :         /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
    1324                 :          0 :         if (fc->minor < 18)
    1325                 :            :                 return -ENOTTY;
    1326                 :            : 
    1327                 :          0 :         return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
    1328                 :            : }
    1329                 :            : 
    1330                 :          0 : static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
    1331                 :            :                                    unsigned long arg)
    1332                 :            : {
    1333                 :          0 :         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
    1334                 :            : 
    1335                 :          0 :         if (fc->minor < 18)
    1336                 :            :                 return -ENOTTY;
    1337                 :            : 
    1338                 :          0 :         return fuse_ioctl_common(file, cmd, arg,
    1339                 :            :                                  FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
    1340                 :            : }
    1341                 :            : 
    1342                 :            : static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
    1343                 :            : {
    1344                 :            :         /* Always update if mtime is explicitly set  */
    1345                 :          0 :         if (ivalid & ATTR_MTIME_SET)
    1346                 :            :                 return true;
    1347                 :            : 
    1348                 :            :         /* Or if kernel i_mtime is the official one */
    1349                 :          0 :         if (trust_local_mtime)
    1350                 :            :                 return true;
    1351                 :            : 
    1352                 :            :         /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
    1353                 :          0 :         if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
    1354                 :            :                 return false;
    1355                 :            : 
    1356                 :            :         /* In all other cases update */
    1357                 :            :         return true;
    1358                 :            : }
    1359                 :            : 
    1360                 :          0 : static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
    1361                 :            :                            struct fuse_setattr_in *arg, bool trust_local_cmtime)
    1362                 :            : {
    1363                 :          0 :         unsigned ivalid = iattr->ia_valid;
    1364                 :            : 
    1365                 :          0 :         if (ivalid & ATTR_MODE)
    1366                 :          0 :                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
    1367                 :          0 :         if (ivalid & ATTR_UID)
    1368                 :          0 :                 arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
    1369                 :          0 :         if (ivalid & ATTR_GID)
    1370                 :          0 :                 arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
    1371                 :          0 :         if (ivalid & ATTR_SIZE)
    1372                 :          0 :                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
    1373                 :          0 :         if (ivalid & ATTR_ATIME) {
    1374                 :          0 :                 arg->valid |= FATTR_ATIME;
    1375                 :          0 :                 arg->atime = iattr->ia_atime.tv_sec;
    1376                 :          0 :                 arg->atimensec = iattr->ia_atime.tv_nsec;
    1377                 :          0 :                 if (!(ivalid & ATTR_ATIME_SET))
    1378                 :          0 :                         arg->valid |= FATTR_ATIME_NOW;
    1379                 :            :         }
    1380                 :          0 :         if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
    1381                 :          0 :                 arg->valid |= FATTR_MTIME;
    1382                 :          0 :                 arg->mtime = iattr->ia_mtime.tv_sec;
    1383                 :          0 :                 arg->mtimensec = iattr->ia_mtime.tv_nsec;
    1384                 :          0 :                 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
    1385                 :          0 :                         arg->valid |= FATTR_MTIME_NOW;
    1386                 :            :         }
    1387                 :          0 :         if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
    1388                 :          0 :                 arg->valid |= FATTR_CTIME;
    1389                 :          0 :                 arg->ctime = iattr->ia_ctime.tv_sec;
    1390                 :          0 :                 arg->ctimensec = iattr->ia_ctime.tv_nsec;
    1391                 :            :         }
    1392                 :          0 : }
    1393                 :            : 
    1394                 :            : /*
    1395                 :            :  * Prevent concurrent writepages on inode
    1396                 :            :  *
    1397                 :            :  * This is done by adding a negative bias to the inode write counter
    1398                 :            :  * and waiting for all pending writes to finish.
    1399                 :            :  */
    1400                 :          0 : void fuse_set_nowrite(struct inode *inode)
    1401                 :            : {
    1402                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
    1403                 :            : 
    1404                 :          0 :         BUG_ON(!inode_is_locked(inode));
    1405                 :            : 
    1406                 :            :         spin_lock(&fi->lock);
    1407                 :          0 :         BUG_ON(fi->writectr < 0);
    1408                 :          0 :         fi->writectr += FUSE_NOWRITE;
    1409                 :            :         spin_unlock(&fi->lock);
    1410                 :          0 :         wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
    1411                 :          0 : }
    1412                 :            : 
    1413                 :            : /*
    1414                 :            :  * Allow writepages on inode
    1415                 :            :  *
    1416                 :            :  * Remove the bias from the writecounter and send any queued
    1417                 :            :  * writepages.
    1418                 :            :  */
    1419                 :          0 : static void __fuse_release_nowrite(struct inode *inode)
    1420                 :            : {
    1421                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
    1422                 :            : 
    1423                 :          0 :         BUG_ON(fi->writectr != FUSE_NOWRITE);
    1424                 :          0 :         fi->writectr = 0;
    1425                 :          0 :         fuse_flush_writepages(inode);
    1426                 :          0 : }
    1427                 :            : 
    1428                 :          0 : void fuse_release_nowrite(struct inode *inode)
    1429                 :            : {
    1430                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
    1431                 :            : 
    1432                 :            :         spin_lock(&fi->lock);
    1433                 :          0 :         __fuse_release_nowrite(inode);
    1434                 :            :         spin_unlock(&fi->lock);
    1435                 :          0 : }
    1436                 :            : 
    1437                 :            : static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
    1438                 :            :                               struct inode *inode,
    1439                 :            :                               struct fuse_setattr_in *inarg_p,
    1440                 :            :                               struct fuse_attr_out *outarg_p)
    1441                 :            : {
    1442                 :          0 :         args->opcode = FUSE_SETATTR;
    1443                 :          0 :         args->nodeid = get_node_id(inode);
    1444                 :          0 :         args->in_numargs = 1;
    1445                 :          0 :         args->in_args[0].size = sizeof(*inarg_p);
    1446                 :          0 :         args->in_args[0].value = inarg_p;
    1447                 :          0 :         args->out_numargs = 1;
    1448                 :          0 :         args->out_args[0].size = sizeof(*outarg_p);
    1449                 :          0 :         args->out_args[0].value = outarg_p;
    1450                 :            : }
    1451                 :            : 
    1452                 :            : /*
    1453                 :            :  * Flush inode->i_mtime to the server
    1454                 :            :  */
    1455                 :          0 : int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
    1456                 :            : {
    1457                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1458                 :          0 :         FUSE_ARGS(args);
    1459                 :            :         struct fuse_setattr_in inarg;
    1460                 :            :         struct fuse_attr_out outarg;
    1461                 :            : 
    1462                 :          0 :         memset(&inarg, 0, sizeof(inarg));
    1463                 :          0 :         memset(&outarg, 0, sizeof(outarg));
    1464                 :            : 
    1465                 :          0 :         inarg.valid = FATTR_MTIME;
    1466                 :          0 :         inarg.mtime = inode->i_mtime.tv_sec;
    1467                 :          0 :         inarg.mtimensec = inode->i_mtime.tv_nsec;
    1468                 :          0 :         if (fc->minor >= 23) {
    1469                 :          0 :                 inarg.valid |= FATTR_CTIME;
    1470                 :          0 :                 inarg.ctime = inode->i_ctime.tv_sec;
    1471                 :          0 :                 inarg.ctimensec = inode->i_ctime.tv_nsec;
    1472                 :            :         }
    1473                 :          0 :         if (ff) {
    1474                 :          0 :                 inarg.valid |= FATTR_FH;
    1475                 :          0 :                 inarg.fh = ff->fh;
    1476                 :            :         }
    1477                 :            :         fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
    1478                 :            : 
    1479                 :          0 :         return fuse_simple_request(fc, &args);
    1480                 :            : }
    1481                 :            : 
    1482                 :            : /*
    1483                 :            :  * Set attributes, and at the same time refresh them.
    1484                 :            :  *
    1485                 :            :  * Truncation is slightly complicated, because the 'truncate' request
    1486                 :            :  * may fail, in which case we don't want to touch the mapping.
    1487                 :            :  * vmtruncate() doesn't allow for this case, so do the rlimit checking
    1488                 :            :  * and the actual truncation by hand.
    1489                 :            :  */
    1490                 :          0 : int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
    1491                 :            :                     struct file *file)
    1492                 :            : {
    1493                 :            :         struct inode *inode = d_inode(dentry);
    1494                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1495                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
    1496                 :          0 :         FUSE_ARGS(args);
    1497                 :            :         struct fuse_setattr_in inarg;
    1498                 :            :         struct fuse_attr_out outarg;
    1499                 :            :         bool is_truncate = false;
    1500                 :          0 :         bool is_wb = fc->writeback_cache;
    1501                 :            :         loff_t oldsize;
    1502                 :            :         int err;
    1503                 :          0 :         bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
    1504                 :            : 
    1505                 :          0 :         if (!fc->default_permissions)
    1506                 :          0 :                 attr->ia_valid |= ATTR_FORCE;
    1507                 :            : 
    1508                 :          0 :         err = setattr_prepare(dentry, attr);
    1509                 :          0 :         if (err)
    1510                 :            :                 return err;
    1511                 :            : 
    1512                 :          0 :         if (attr->ia_valid & ATTR_OPEN) {
    1513                 :            :                 /* This is coming from open(..., ... | O_TRUNC); */
    1514                 :          0 :                 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
    1515                 :          0 :                 WARN_ON(attr->ia_size != 0);
    1516                 :          0 :                 if (fc->atomic_o_trunc) {
    1517                 :            :                         /*
    1518                 :            :                          * No need to send request to userspace, since actual
    1519                 :            :                          * truncation has already been done by OPEN.  But still
    1520                 :            :                          * need to truncate page cache.
    1521                 :            :                          */
    1522                 :            :                         i_size_write(inode, 0);
    1523                 :          0 :                         truncate_pagecache(inode, 0);
    1524                 :          0 :                         return 0;
    1525                 :            :                 }
    1526                 :            :                 file = NULL;
    1527                 :            :         }
    1528                 :            : 
    1529                 :          0 :         if (attr->ia_valid & ATTR_SIZE) {
    1530                 :          0 :                 if (WARN_ON(!S_ISREG(inode->i_mode)))
    1531                 :            :                         return -EIO;
    1532                 :            :                 is_truncate = true;
    1533                 :            :         }
    1534                 :            : 
    1535                 :            :         /* Flush dirty data/metadata before non-truncate SETATTR */
    1536                 :          0 :         if (is_wb && S_ISREG(inode->i_mode) &&
    1537                 :          0 :             attr->ia_valid &
    1538                 :            :                         (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
    1539                 :            :                          ATTR_TIMES_SET)) {
    1540                 :          0 :                 err = write_inode_now(inode, true);
    1541                 :          0 :                 if (err)
    1542                 :            :                         return err;
    1543                 :            : 
    1544                 :          0 :                 fuse_set_nowrite(inode);
    1545                 :          0 :                 fuse_release_nowrite(inode);
    1546                 :            :         }
    1547                 :            : 
    1548                 :          0 :         if (is_truncate) {
    1549                 :          0 :                 fuse_set_nowrite(inode);
    1550                 :          0 :                 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
    1551                 :          0 :                 if (trust_local_cmtime && attr->ia_size != inode->i_size)
    1552                 :          0 :                         attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
    1553                 :            :         }
    1554                 :            : 
    1555                 :          0 :         memset(&inarg, 0, sizeof(inarg));
    1556                 :          0 :         memset(&outarg, 0, sizeof(outarg));
    1557                 :          0 :         iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
    1558                 :          0 :         if (file) {
    1559                 :          0 :                 struct fuse_file *ff = file->private_data;
    1560                 :          0 :                 inarg.valid |= FATTR_FH;
    1561                 :          0 :                 inarg.fh = ff->fh;
    1562                 :            :         }
    1563                 :          0 :         if (attr->ia_valid & ATTR_SIZE) {
    1564                 :            :                 /* For mandatory locking in truncate */
    1565                 :          0 :                 inarg.valid |= FATTR_LOCKOWNER;
    1566                 :          0 :                 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
    1567                 :            :         }
    1568                 :            :         fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
    1569                 :          0 :         err = fuse_simple_request(fc, &args);
    1570                 :          0 :         if (err) {
    1571                 :          0 :                 if (err == -EINTR)
    1572                 :            :                         fuse_invalidate_attr(inode);
    1573                 :            :                 goto error;
    1574                 :            :         }
    1575                 :            : 
    1576                 :          0 :         if (fuse_invalid_attr(&outarg.attr) ||
    1577                 :          0 :             (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
    1578                 :          0 :                 make_bad_inode(inode);
    1579                 :            :                 err = -EIO;
    1580                 :          0 :                 goto error;
    1581                 :            :         }
    1582                 :            : 
    1583                 :            :         spin_lock(&fi->lock);
    1584                 :            :         /* the kernel maintains i_mtime locally */
    1585                 :          0 :         if (trust_local_cmtime) {
    1586                 :          0 :                 if (attr->ia_valid & ATTR_MTIME)
    1587                 :          0 :                         inode->i_mtime = attr->ia_mtime;
    1588                 :          0 :                 if (attr->ia_valid & ATTR_CTIME)
    1589                 :          0 :                         inode->i_ctime = attr->ia_ctime;
    1590                 :            :                 /* FIXME: clear I_DIRTY_SYNC? */
    1591                 :            :         }
    1592                 :            : 
    1593                 :          0 :         fuse_change_attributes_common(inode, &outarg.attr,
    1594                 :            :                                       attr_timeout(&outarg));
    1595                 :          0 :         oldsize = inode->i_size;
    1596                 :            :         /* see the comment in fuse_change_attributes() */
    1597                 :          0 :         if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
    1598                 :          0 :                 i_size_write(inode, outarg.attr.size);
    1599                 :            : 
    1600                 :          0 :         if (is_truncate) {
    1601                 :            :                 /* NOTE: this may release/reacquire fi->lock */
    1602                 :          0 :                 __fuse_release_nowrite(inode);
    1603                 :            :         }
    1604                 :            :         spin_unlock(&fi->lock);
    1605                 :            : 
    1606                 :            :         /*
    1607                 :            :          * Only call invalidate_inode_pages2() after removing
    1608                 :            :          * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
    1609                 :            :          */
    1610                 :          0 :         if ((is_truncate || !is_wb) &&
    1611                 :          0 :             S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
    1612                 :          0 :                 truncate_pagecache(inode, outarg.attr.size);
    1613                 :          0 :                 invalidate_inode_pages2(inode->i_mapping);
    1614                 :            :         }
    1615                 :            : 
    1616                 :          0 :         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
    1617                 :          0 :         return 0;
    1618                 :            : 
    1619                 :            : error:
    1620                 :          0 :         if (is_truncate)
    1621                 :          0 :                 fuse_release_nowrite(inode);
    1622                 :            : 
    1623                 :          0 :         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
    1624                 :          0 :         return err;
    1625                 :            : }
    1626                 :            : 
    1627                 :          0 : static int fuse_setattr(struct dentry *entry, struct iattr *attr)
    1628                 :            : {
    1629                 :            :         struct inode *inode = d_inode(entry);
    1630                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1631                 :          0 :         struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
    1632                 :            :         int ret;
    1633                 :            : 
    1634                 :          0 :         if (!fuse_allow_current_process(get_fuse_conn(inode)))
    1635                 :            :                 return -EACCES;
    1636                 :            : 
    1637                 :          0 :         if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
    1638                 :          0 :                 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
    1639                 :            :                                     ATTR_MODE);
    1640                 :            : 
    1641                 :            :                 /*
    1642                 :            :                  * The only sane way to reliably kill suid/sgid is to do it in
    1643                 :            :                  * the userspace filesystem
    1644                 :            :                  *
    1645                 :            :                  * This should be done on write(), truncate() and chown().
    1646                 :            :                  */
    1647                 :          0 :                 if (!fc->handle_killpriv) {
    1648                 :            :                         /*
    1649                 :            :                          * ia_mode calculation may have used stale i_mode.
    1650                 :            :                          * Refresh and recalculate.
    1651                 :            :                          */
    1652                 :          0 :                         ret = fuse_do_getattr(inode, NULL, file);
    1653                 :          0 :                         if (ret)
    1654                 :            :                                 return ret;
    1655                 :            : 
    1656                 :          0 :                         attr->ia_mode = inode->i_mode;
    1657                 :          0 :                         if (inode->i_mode & S_ISUID) {
    1658                 :          0 :                                 attr->ia_valid |= ATTR_MODE;
    1659                 :          0 :                                 attr->ia_mode &= ~S_ISUID;
    1660                 :            :                         }
    1661                 :          0 :                         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
    1662                 :          0 :                                 attr->ia_valid |= ATTR_MODE;
    1663                 :          0 :                                 attr->ia_mode &= ~S_ISGID;
    1664                 :            :                         }
    1665                 :            :                 }
    1666                 :            :         }
    1667                 :          0 :         if (!attr->ia_valid)
    1668                 :            :                 return 0;
    1669                 :            : 
    1670                 :          0 :         ret = fuse_do_setattr(entry, attr, file);
    1671                 :          0 :         if (!ret) {
    1672                 :            :                 /*
    1673                 :            :                  * If filesystem supports acls it may have updated acl xattrs in
    1674                 :            :                  * the filesystem, so forget cached acls for the inode.
    1675                 :            :                  */
    1676                 :          0 :                 if (fc->posix_acl)
    1677                 :          0 :                         forget_all_cached_acls(inode);
    1678                 :            : 
    1679                 :            :                 /* Directory mode changed, may need to revalidate access */
    1680                 :          0 :                 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
    1681                 :            :                         fuse_invalidate_entry_cache(entry);
    1682                 :            :         }
    1683                 :          0 :         return ret;
    1684                 :            : }
    1685                 :            : 
    1686                 :          0 : static int fuse_getattr(const struct path *path, struct kstat *stat,
    1687                 :            :                         u32 request_mask, unsigned int flags)
    1688                 :            : {
    1689                 :          0 :         struct inode *inode = d_inode(path->dentry);
    1690                 :            :         struct fuse_conn *fc = get_fuse_conn(inode);
    1691                 :            : 
    1692                 :          0 :         if (!fuse_allow_current_process(fc))
    1693                 :            :                 return -EACCES;
    1694                 :            : 
    1695                 :          0 :         return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
    1696                 :            : }
    1697                 :            : 
    1698                 :            : static const struct inode_operations fuse_dir_inode_operations = {
    1699                 :            :         .lookup         = fuse_lookup,
    1700                 :            :         .mkdir          = fuse_mkdir,
    1701                 :            :         .symlink        = fuse_symlink,
    1702                 :            :         .unlink         = fuse_unlink,
    1703                 :            :         .rmdir          = fuse_rmdir,
    1704                 :            :         .rename         = fuse_rename2,
    1705                 :            :         .link           = fuse_link,
    1706                 :            :         .setattr        = fuse_setattr,
    1707                 :            :         .create         = fuse_create,
    1708                 :            :         .atomic_open    = fuse_atomic_open,
    1709                 :            :         .mknod          = fuse_mknod,
    1710                 :            :         .permission     = fuse_permission,
    1711                 :            :         .getattr        = fuse_getattr,
    1712                 :            :         .listxattr      = fuse_listxattr,
    1713                 :            :         .get_acl        = fuse_get_acl,
    1714                 :            :         .set_acl        = fuse_set_acl,
    1715                 :            : };
    1716                 :            : 
    1717                 :            : static const struct file_operations fuse_dir_operations = {
    1718                 :            :         .llseek         = generic_file_llseek,
    1719                 :            :         .read           = generic_read_dir,
    1720                 :            :         .iterate_shared = fuse_readdir,
    1721                 :            :         .open           = fuse_dir_open,
    1722                 :            :         .release        = fuse_dir_release,
    1723                 :            :         .fsync          = fuse_dir_fsync,
    1724                 :            :         .unlocked_ioctl = fuse_dir_ioctl,
    1725                 :            :         .compat_ioctl   = fuse_dir_compat_ioctl,
    1726                 :            : };
    1727                 :            : 
    1728                 :            : static const struct inode_operations fuse_common_inode_operations = {
    1729                 :            :         .setattr        = fuse_setattr,
    1730                 :            :         .permission     = fuse_permission,
    1731                 :            :         .getattr        = fuse_getattr,
    1732                 :            :         .listxattr      = fuse_listxattr,
    1733                 :            :         .get_acl        = fuse_get_acl,
    1734                 :            :         .set_acl        = fuse_set_acl,
    1735                 :            : };
    1736                 :            : 
    1737                 :            : static const struct inode_operations fuse_symlink_inode_operations = {
    1738                 :            :         .setattr        = fuse_setattr,
    1739                 :            :         .get_link       = fuse_get_link,
    1740                 :            :         .getattr        = fuse_getattr,
    1741                 :            :         .listxattr      = fuse_listxattr,
    1742                 :            : };
    1743                 :            : 
    1744                 :          0 : void fuse_init_common(struct inode *inode)
    1745                 :            : {
    1746                 :          0 :         inode->i_op = &fuse_common_inode_operations;
    1747                 :          0 : }
    1748                 :            : 
    1749                 :          3 : void fuse_init_dir(struct inode *inode)
    1750                 :            : {
    1751                 :            :         struct fuse_inode *fi = get_fuse_inode(inode);
    1752                 :            : 
    1753                 :          3 :         inode->i_op = &fuse_dir_inode_operations;
    1754                 :          3 :         inode->i_fop = &fuse_dir_operations;
    1755                 :            : 
    1756                 :          3 :         spin_lock_init(&fi->rdc.lock);
    1757                 :          3 :         fi->rdc.cached = false;
    1758                 :          3 :         fi->rdc.size = 0;
    1759                 :          3 :         fi->rdc.pos = 0;
    1760                 :          3 :         fi->rdc.version = 0;
    1761                 :          3 : }
    1762                 :            : 
    1763                 :          0 : static int fuse_symlink_readpage(struct file *null, struct page *page)
    1764                 :            : {
    1765                 :          0 :         int err = fuse_readlink_page(page->mapping->host, page);
    1766                 :            : 
    1767                 :          0 :         if (!err)
    1768                 :            :                 SetPageUptodate(page);
    1769                 :            : 
    1770                 :          0 :         unlock_page(page);
    1771                 :            : 
    1772                 :          0 :         return err;
    1773                 :            : }
    1774                 :            : 
    1775                 :            : static const struct address_space_operations fuse_symlink_aops = {
    1776                 :            :         .readpage       = fuse_symlink_readpage,
    1777                 :            : };
    1778                 :            : 
    1779                 :          0 : void fuse_init_symlink(struct inode *inode)
    1780                 :            : {
    1781                 :          0 :         inode->i_op = &fuse_symlink_inode_operations;
    1782                 :          0 :         inode->i_data.a_ops = &fuse_symlink_aops;
    1783                 :          0 :         inode_nohighmem(inode);
    1784                 :          0 : }
    

Generated by: LCOV version 1.14