~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/fuse/dir.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/fuse/dir.c (Version linux-6.11.5) and /fs/fuse/dir.c (Version linux-6.4.16)


  1 /*                                                  1 /*
  2   FUSE: Filesystem in Userspace                     2   FUSE: Filesystem in Userspace
  3   Copyright (C) 2001-2008  Miklos Szeredi <mik      3   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
  4                                                     4 
  5   This program can be distributed under the te      5   This program can be distributed under the terms of the GNU GPL.
  6   See the file COPYING.                             6   See the file COPYING.
  7 */                                                  7 */
  8                                                     8 
  9 #include "fuse_i.h"                                 9 #include "fuse_i.h"
 10                                                    10 
 11 #include <linux/pagemap.h>                         11 #include <linux/pagemap.h>
 12 #include <linux/file.h>                            12 #include <linux/file.h>
 13 #include <linux/fs_context.h>                      13 #include <linux/fs_context.h>
 14 #include <linux/moduleparam.h>                     14 #include <linux/moduleparam.h>
 15 #include <linux/sched.h>                           15 #include <linux/sched.h>
 16 #include <linux/namei.h>                           16 #include <linux/namei.h>
 17 #include <linux/slab.h>                            17 #include <linux/slab.h>
 18 #include <linux/xattr.h>                           18 #include <linux/xattr.h>
 19 #include <linux/iversion.h>                        19 #include <linux/iversion.h>
 20 #include <linux/posix_acl.h>                       20 #include <linux/posix_acl.h>
 21 #include <linux/security.h>                        21 #include <linux/security.h>
 22 #include <linux/types.h>                           22 #include <linux/types.h>
 23 #include <linux/kernel.h>                          23 #include <linux/kernel.h>
 24                                                    24 
 25 static bool __read_mostly allow_sys_admin_acce     25 static bool __read_mostly allow_sys_admin_access;
 26 module_param(allow_sys_admin_access, bool, 064     26 module_param(allow_sys_admin_access, bool, 0644);
 27 MODULE_PARM_DESC(allow_sys_admin_access,           27 MODULE_PARM_DESC(allow_sys_admin_access,
 28                  "Allow users with CAP_SYS_ADM     28                  "Allow users with CAP_SYS_ADMIN in initial userns to bypass allow_other access check");
 29                                                    29 
 30 static void fuse_advise_use_readdirplus(struct     30 static void fuse_advise_use_readdirplus(struct inode *dir)
 31 {                                                  31 {
 32         struct fuse_inode *fi = get_fuse_inode     32         struct fuse_inode *fi = get_fuse_inode(dir);
 33                                                    33 
 34         set_bit(FUSE_I_ADVISE_RDPLUS, &fi->sta     34         set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
 35 }                                                  35 }
 36                                                    36 
 37 #if BITS_PER_LONG >= 64                            37 #if BITS_PER_LONG >= 64
 38 static inline void __fuse_dentry_settime(struc     38 static inline void __fuse_dentry_settime(struct dentry *entry, u64 time)
 39 {                                                  39 {
 40         entry->d_fsdata = (void *) time;           40         entry->d_fsdata = (void *) time;
 41 }                                                  41 }
 42                                                    42 
 43 static inline u64 fuse_dentry_time(const struc     43 static inline u64 fuse_dentry_time(const struct dentry *entry)
 44 {                                                  44 {
 45         return (u64)entry->d_fsdata;               45         return (u64)entry->d_fsdata;
 46 }                                                  46 }
 47                                                    47 
 48 #else                                              48 #else
 49 union fuse_dentry {                                49 union fuse_dentry {
 50         u64 time;                                  50         u64 time;
 51         struct rcu_head rcu;                       51         struct rcu_head rcu;
 52 };                                                 52 };
 53                                                    53 
 54 static inline void __fuse_dentry_settime(struc     54 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
 55 {                                                  55 {
 56         ((union fuse_dentry *) dentry->d_fsdat     56         ((union fuse_dentry *) dentry->d_fsdata)->time = time;
 57 }                                                  57 }
 58                                                    58 
 59 static inline u64 fuse_dentry_time(const struc     59 static inline u64 fuse_dentry_time(const struct dentry *entry)
 60 {                                                  60 {
 61         return ((union fuse_dentry *) entry->d     61         return ((union fuse_dentry *) entry->d_fsdata)->time;
 62 }                                                  62 }
 63 #endif                                             63 #endif
 64                                                    64 
 65 static void fuse_dentry_settime(struct dentry      65 static void fuse_dentry_settime(struct dentry *dentry, u64 time)
 66 {                                                  66 {
 67         struct fuse_conn *fc = get_fuse_conn_s     67         struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
 68         bool delete = !time && fc->delete_stal     68         bool delete = !time && fc->delete_stale;
 69         /*                                         69         /*
 70          * Mess with DCACHE_OP_DELETE because      70          * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
 71          * Don't care about races, either way      71          * Don't care about races, either way it's just an optimization
 72          */                                        72          */
 73         if ((!delete && (dentry->d_flags & DCA     73         if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
 74             (delete && !(dentry->d_flags & DCA     74             (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
 75                 spin_lock(&dentry->d_lock);        75                 spin_lock(&dentry->d_lock);
 76                 if (!delete)                       76                 if (!delete)
 77                         dentry->d_flags &= ~DC     77                         dentry->d_flags &= ~DCACHE_OP_DELETE;
 78                 else                               78                 else
 79                         dentry->d_flags |= DCA     79                         dentry->d_flags |= DCACHE_OP_DELETE;
 80                 spin_unlock(&dentry->d_lock);      80                 spin_unlock(&dentry->d_lock);
 81         }                                          81         }
 82                                                    82 
 83         __fuse_dentry_settime(dentry, time);       83         __fuse_dentry_settime(dentry, time);
 84 }                                                  84 }
 85                                                    85 
 86 /*                                                 86 /*
 87  * FUSE caches dentries and attributes with se     87  * FUSE caches dentries and attributes with separate timeout.  The
 88  * time in jiffies until the dentry/attributes     88  * time in jiffies until the dentry/attributes are valid is stored in
 89  * dentry->d_fsdata and fuse_inode->i_time res     89  * dentry->d_fsdata and fuse_inode->i_time respectively.
 90  */                                                90  */
 91                                                    91 
 92 /*                                                 92 /*
 93  * Calculate the time in jiffies until a dentr     93  * Calculate the time in jiffies until a dentry/attributes are valid
 94  */                                                94  */
 95 u64 fuse_time_to_jiffies(u64 sec, u32 nsec)    !!  95 static u64 time_to_jiffies(u64 sec, u32 nsec)
 96 {                                                  96 {
 97         if (sec || nsec) {                         97         if (sec || nsec) {
 98                 struct timespec64 ts = {           98                 struct timespec64 ts = {
 99                         sec,                       99                         sec,
100                         min_t(u32, nsec, NSEC_    100                         min_t(u32, nsec, NSEC_PER_SEC - 1)
101                 };                                101                 };
102                                                   102 
103                 return get_jiffies_64() + time    103                 return get_jiffies_64() + timespec64_to_jiffies(&ts);
104         } else                                    104         } else
105                 return 0;                         105                 return 0;
106 }                                                 106 }
107                                                   107 
108 /*                                                108 /*
109  * Set dentry and possibly attribute timeouts     109  * Set dentry and possibly attribute timeouts from the lookup/mk*
110  * replies                                        110  * replies
111  */                                               111  */
112 void fuse_change_entry_timeout(struct dentry *    112 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
113 {                                                 113 {
114         fuse_dentry_settime(entry,                114         fuse_dentry_settime(entry,
115                 fuse_time_to_jiffies(o->entry_ !! 115                 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
                                                   >> 116 }
                                                   >> 117 
                                                   >> 118 static u64 attr_timeout(struct fuse_attr_out *o)
                                                   >> 119 {
                                                   >> 120         return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
                                                   >> 121 }
                                                   >> 122 
                                                   >> 123 u64 entry_attr_timeout(struct fuse_entry_out *o)
                                                   >> 124 {
                                                   >> 125         return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
116 }                                                 126 }
117                                                   127 
118 void fuse_invalidate_attr_mask(struct inode *i    128 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
119 {                                                 129 {
120         set_mask_bits(&get_fuse_inode(inode)->    130         set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
121 }                                                 131 }
122                                                   132 
123 /*                                                133 /*
124  * Mark the attributes as stale, so that at th    134  * Mark the attributes as stale, so that at the next call to
125  * ->getattr() they will be fetched from users    135  * ->getattr() they will be fetched from userspace
126  */                                               136  */
127 void fuse_invalidate_attr(struct inode *inode)    137 void fuse_invalidate_attr(struct inode *inode)
128 {                                                 138 {
129         fuse_invalidate_attr_mask(inode, STATX    139         fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
130 }                                                 140 }
131                                                   141 
132 static void fuse_dir_changed(struct inode *dir    142 static void fuse_dir_changed(struct inode *dir)
133 {                                                 143 {
134         fuse_invalidate_attr(dir);                144         fuse_invalidate_attr(dir);
135         inode_maybe_inc_iversion(dir, false);     145         inode_maybe_inc_iversion(dir, false);
136 }                                                 146 }
137                                                   147 
138 /*                                                148 /*
139  * Mark the attributes as stale due to an atim    149  * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
140  * atime is not used.                             150  * atime is not used.
141  */                                               151  */
142 void fuse_invalidate_atime(struct inode *inode    152 void fuse_invalidate_atime(struct inode *inode)
143 {                                                 153 {
144         if (!IS_RDONLY(inode))                    154         if (!IS_RDONLY(inode))
145                 fuse_invalidate_attr_mask(inod    155                 fuse_invalidate_attr_mask(inode, STATX_ATIME);
146 }                                                 156 }
147                                                   157 
148 /*                                                158 /*
149  * Just mark the entry as stale, so that a nex    159  * Just mark the entry as stale, so that a next attempt to look it up
150  * will result in a new lookup call to userspa    160  * will result in a new lookup call to userspace
151  *                                                161  *
152  * This is called when a dentry is about to be    162  * This is called when a dentry is about to become negative and the
153  * timeout is unknown (unlink, rmdir, rename a    163  * timeout is unknown (unlink, rmdir, rename and in some cases
154  * lookup)                                        164  * lookup)
155  */                                               165  */
156 void fuse_invalidate_entry_cache(struct dentry    166 void fuse_invalidate_entry_cache(struct dentry *entry)
157 {                                                 167 {
158         fuse_dentry_settime(entry, 0);            168         fuse_dentry_settime(entry, 0);
159 }                                                 169 }
160                                                   170 
161 /*                                                171 /*
162  * Same as fuse_invalidate_entry_cache(), but     172  * Same as fuse_invalidate_entry_cache(), but also try to remove the
163  * dentry from the hash                           173  * dentry from the hash
164  */                                               174  */
165 static void fuse_invalidate_entry(struct dentr    175 static void fuse_invalidate_entry(struct dentry *entry)
166 {                                                 176 {
167         d_invalidate(entry);                      177         d_invalidate(entry);
168         fuse_invalidate_entry_cache(entry);       178         fuse_invalidate_entry_cache(entry);
169 }                                                 179 }
170                                                   180 
171 static void fuse_lookup_init(struct fuse_conn     181 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
172                              u64 nodeid, const    182                              u64 nodeid, const struct qstr *name,
173                              struct fuse_entry    183                              struct fuse_entry_out *outarg)
174 {                                                 184 {
175         memset(outarg, 0, sizeof(struct fuse_e    185         memset(outarg, 0, sizeof(struct fuse_entry_out));
176         args->opcode = FUSE_LOOKUP;               186         args->opcode = FUSE_LOOKUP;
177         args->nodeid = nodeid;                    187         args->nodeid = nodeid;
178         args->in_numargs = 1;                     188         args->in_numargs = 1;
179         args->in_args[0].size = name->len + 1;    189         args->in_args[0].size = name->len + 1;
180         args->in_args[0].value = name->name;      190         args->in_args[0].value = name->name;
181         args->out_numargs = 1;                    191         args->out_numargs = 1;
182         args->out_args[0].size = sizeof(struct    192         args->out_args[0].size = sizeof(struct fuse_entry_out);
183         args->out_args[0].value = outarg;         193         args->out_args[0].value = outarg;
184 }                                                 194 }
185                                                   195 
186 /*                                                196 /*
187  * Check whether the dentry is still valid        197  * Check whether the dentry is still valid
188  *                                                198  *
189  * If the entry validity timeout has expired a    199  * If the entry validity timeout has expired and the dentry is
190  * positive, try to redo the lookup.  If the l    200  * positive, try to redo the lookup.  If the lookup results in a
191  * different inode, then let the VFS invalidat    201  * different inode, then let the VFS invalidate the dentry and redo
192  * the lookup once more.  If the lookup result    202  * the lookup once more.  If the lookup results in the same inode,
193  * then refresh the attributes, timeouts and m    203  * then refresh the attributes, timeouts and mark the dentry valid.
194  */                                               204  */
195 static int fuse_dentry_revalidate(struct dentr    205 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
196 {                                                 206 {
197         struct inode *inode;                      207         struct inode *inode;
198         struct dentry *parent;                    208         struct dentry *parent;
199         struct fuse_mount *fm;                    209         struct fuse_mount *fm;
200         struct fuse_inode *fi;                    210         struct fuse_inode *fi;
201         int ret;                                  211         int ret;
202                                                   212 
203         inode = d_inode_rcu(entry);               213         inode = d_inode_rcu(entry);
204         if (inode && fuse_is_bad(inode))          214         if (inode && fuse_is_bad(inode))
205                 goto invalid;                     215                 goto invalid;
206         else if (time_before64(fuse_dentry_tim    216         else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
207                  (flags & (LOOKUP_EXCL | LOOKU    217                  (flags & (LOOKUP_EXCL | LOOKUP_REVAL | LOOKUP_RENAME_TARGET))) {
208                 struct fuse_entry_out outarg;     218                 struct fuse_entry_out outarg;
209                 FUSE_ARGS(args);                  219                 FUSE_ARGS(args);
210                 struct fuse_forget_link *forge    220                 struct fuse_forget_link *forget;
211                 u64 attr_version;                 221                 u64 attr_version;
212                                                   222 
213                 /* For negative dentries, alwa    223                 /* For negative dentries, always do a fresh lookup */
214                 if (!inode)                       224                 if (!inode)
215                         goto invalid;             225                         goto invalid;
216                                                   226 
217                 ret = -ECHILD;                    227                 ret = -ECHILD;
218                 if (flags & LOOKUP_RCU)           228                 if (flags & LOOKUP_RCU)
219                         goto out;                 229                         goto out;
220                                                   230 
221                 fm = get_fuse_mount(inode);       231                 fm = get_fuse_mount(inode);
222                                                   232 
223                 forget = fuse_alloc_forget();     233                 forget = fuse_alloc_forget();
224                 ret = -ENOMEM;                    234                 ret = -ENOMEM;
225                 if (!forget)                      235                 if (!forget)
226                         goto out;                 236                         goto out;
227                                                   237 
228                 attr_version = fuse_get_attr_v    238                 attr_version = fuse_get_attr_version(fm->fc);
229                                                   239 
230                 parent = dget_parent(entry);      240                 parent = dget_parent(entry);
231                 fuse_lookup_init(fm->fc, &args    241                 fuse_lookup_init(fm->fc, &args, get_node_id(d_inode(parent)),
232                                  &entry->d_nam    242                                  &entry->d_name, &outarg);
233                 ret = fuse_simple_request(fm,     243                 ret = fuse_simple_request(fm, &args);
234                 dput(parent);                     244                 dput(parent);
235                 /* Zero nodeid is same as -ENO    245                 /* Zero nodeid is same as -ENOENT */
236                 if (!ret && !outarg.nodeid)       246                 if (!ret && !outarg.nodeid)
237                         ret = -ENOENT;            247                         ret = -ENOENT;
238                 if (!ret) {                       248                 if (!ret) {
239                         fi = get_fuse_inode(in    249                         fi = get_fuse_inode(inode);
240                         if (outarg.nodeid != g    250                         if (outarg.nodeid != get_node_id(inode) ||
241                             (bool) IS_AUTOMOUN    251                             (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) {
242                                 fuse_queue_for    252                                 fuse_queue_forget(fm->fc, forget,
243                                                   253                                                   outarg.nodeid, 1);
244                                 goto invalid;     254                                 goto invalid;
245                         }                         255                         }
246                         spin_lock(&fi->lock);     256                         spin_lock(&fi->lock);
247                         fi->nlookup++;            257                         fi->nlookup++;
248                         spin_unlock(&fi->lock)    258                         spin_unlock(&fi->lock);
249                 }                                 259                 }
250                 kfree(forget);                    260                 kfree(forget);
251                 if (ret == -ENOMEM || ret == -    261                 if (ret == -ENOMEM || ret == -EINTR)
252                         goto out;                 262                         goto out;
253                 if (ret || fuse_invalid_attr(&    263                 if (ret || fuse_invalid_attr(&outarg.attr) ||
254                     fuse_stale_inode(inode, ou    264                     fuse_stale_inode(inode, outarg.generation, &outarg.attr))
255                         goto invalid;             265                         goto invalid;
256                                                   266 
257                 forget_all_cached_acls(inode);    267                 forget_all_cached_acls(inode);
258                 fuse_change_attributes(inode,  !! 268                 fuse_change_attributes(inode, &outarg.attr,
259                                        ATTR_TI !! 269                                        entry_attr_timeout(&outarg),
260                                        attr_ve    270                                        attr_version);
261                 fuse_change_entry_timeout(entr    271                 fuse_change_entry_timeout(entry, &outarg);
262         } else if (inode) {                       272         } else if (inode) {
263                 fi = get_fuse_inode(inode);       273                 fi = get_fuse_inode(inode);
264                 if (flags & LOOKUP_RCU) {         274                 if (flags & LOOKUP_RCU) {
265                         if (test_bit(FUSE_I_IN    275                         if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
266                                 return -ECHILD    276                                 return -ECHILD;
267                 } else if (test_and_clear_bit(    277                 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
268                         parent = dget_parent(e    278                         parent = dget_parent(entry);
269                         fuse_advise_use_readdi    279                         fuse_advise_use_readdirplus(d_inode(parent));
270                         dput(parent);             280                         dput(parent);
271                 }                                 281                 }
272         }                                         282         }
273         ret = 1;                                  283         ret = 1;
274 out:                                              284 out:
275         return ret;                               285         return ret;
276                                                   286 
277 invalid:                                          287 invalid:
278         ret = 0;                                  288         ret = 0;
279         goto out;                                 289         goto out;
280 }                                                 290 }
281                                                   291 
282 #if BITS_PER_LONG < 64                            292 #if BITS_PER_LONG < 64
283 static int fuse_dentry_init(struct dentry *den    293 static int fuse_dentry_init(struct dentry *dentry)
284 {                                                 294 {
285         dentry->d_fsdata = kzalloc(sizeof(unio    295         dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry),
286                                    GFP_KERNEL_    296                                    GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
287                                                   297 
288         return dentry->d_fsdata ? 0 : -ENOMEM;    298         return dentry->d_fsdata ? 0 : -ENOMEM;
289 }                                                 299 }
290 static void fuse_dentry_release(struct dentry     300 static void fuse_dentry_release(struct dentry *dentry)
291 {                                                 301 {
292         union fuse_dentry *fd = dentry->d_fsda    302         union fuse_dentry *fd = dentry->d_fsdata;
293                                                   303 
294         kfree_rcu(fd, rcu);                       304         kfree_rcu(fd, rcu);
295 }                                                 305 }
296 #endif                                            306 #endif
297                                                   307 
298 static int fuse_dentry_delete(const struct den    308 static int fuse_dentry_delete(const struct dentry *dentry)
299 {                                                 309 {
300         return time_before64(fuse_dentry_time(    310         return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
301 }                                                 311 }
302                                                   312 
303 /*                                                313 /*
304  * Create a fuse_mount object with a new super    314  * Create a fuse_mount object with a new superblock (with path->dentry
305  * as the root), and return that mount so it c    315  * as the root), and return that mount so it can be auto-mounted on
306  * @path.                                         316  * @path.
307  */                                               317  */
308 static struct vfsmount *fuse_dentry_automount(    318 static struct vfsmount *fuse_dentry_automount(struct path *path)
309 {                                                 319 {
310         struct fs_context *fsc;                   320         struct fs_context *fsc;
311         struct vfsmount *mnt;                     321         struct vfsmount *mnt;
312         struct fuse_inode *mp_fi = get_fuse_in    322         struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
313                                                   323 
314         fsc = fs_context_for_submount(path->mn    324         fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
315         if (IS_ERR(fsc))                          325         if (IS_ERR(fsc))
316                 return ERR_CAST(fsc);             326                 return ERR_CAST(fsc);
317                                                   327 
318         /* Pass the FUSE inode of the mount fo    328         /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */
319         fsc->fs_private = mp_fi;                  329         fsc->fs_private = mp_fi;
320                                                   330 
321         /* Create the submount */                 331         /* Create the submount */
322         mnt = fc_mount(fsc);                      332         mnt = fc_mount(fsc);
323         if (!IS_ERR(mnt))                         333         if (!IS_ERR(mnt))
324                 mntget(mnt);                      334                 mntget(mnt);
325                                                   335 
326         put_fs_context(fsc);                      336         put_fs_context(fsc);
327         return mnt;                               337         return mnt;
328 }                                                 338 }
329                                                   339 
330 const struct dentry_operations fuse_dentry_ope    340 const struct dentry_operations fuse_dentry_operations = {
331         .d_revalidate   = fuse_dentry_revalida    341         .d_revalidate   = fuse_dentry_revalidate,
332         .d_delete       = fuse_dentry_delete,     342         .d_delete       = fuse_dentry_delete,
333 #if BITS_PER_LONG < 64                            343 #if BITS_PER_LONG < 64
334         .d_init         = fuse_dentry_init,       344         .d_init         = fuse_dentry_init,
335         .d_release      = fuse_dentry_release,    345         .d_release      = fuse_dentry_release,
336 #endif                                            346 #endif
337         .d_automount    = fuse_dentry_automoun    347         .d_automount    = fuse_dentry_automount,
338 };                                                348 };
339                                                   349 
340 const struct dentry_operations fuse_root_dentr    350 const struct dentry_operations fuse_root_dentry_operations = {
341 #if BITS_PER_LONG < 64                            351 #if BITS_PER_LONG < 64
342         .d_init         = fuse_dentry_init,       352         .d_init         = fuse_dentry_init,
343         .d_release      = fuse_dentry_release,    353         .d_release      = fuse_dentry_release,
344 #endif                                            354 #endif
345 };                                                355 };
346                                                   356 
347 int fuse_valid_type(int m)                        357 int fuse_valid_type(int m)
348 {                                                 358 {
349         return S_ISREG(m) || S_ISDIR(m) || S_I    359         return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
350                 S_ISBLK(m) || S_ISFIFO(m) || S    360                 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
351 }                                                 361 }
352                                                   362 
353 static bool fuse_valid_size(u64 size)          << 
354 {                                              << 
355         return size <= LLONG_MAX;              << 
356 }                                              << 
357                                                << 
358 bool fuse_invalid_attr(struct fuse_attr *attr)    363 bool fuse_invalid_attr(struct fuse_attr *attr)
359 {                                                 364 {
360         return !fuse_valid_type(attr->mode) || !! 365         return !fuse_valid_type(attr->mode) ||
                                                   >> 366                 attr->size > LLONG_MAX;
361 }                                                 367 }
362                                                   368 
363 int fuse_lookup_name(struct super_block *sb, u    369 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
364                      struct fuse_entry_out *ou    370                      struct fuse_entry_out *outarg, struct inode **inode)
365 {                                                 371 {
366         struct fuse_mount *fm = get_fuse_mount    372         struct fuse_mount *fm = get_fuse_mount_super(sb);
367         FUSE_ARGS(args);                          373         FUSE_ARGS(args);
368         struct fuse_forget_link *forget;          374         struct fuse_forget_link *forget;
369         u64 attr_version;                         375         u64 attr_version;
370         int err;                                  376         int err;
371                                                   377 
372         *inode = NULL;                            378         *inode = NULL;
373         err = -ENAMETOOLONG;                      379         err = -ENAMETOOLONG;
374         if (name->len > FUSE_NAME_MAX)            380         if (name->len > FUSE_NAME_MAX)
375                 goto out;                         381                 goto out;
376                                                   382 
377                                                   383 
378         forget = fuse_alloc_forget();             384         forget = fuse_alloc_forget();
379         err = -ENOMEM;                            385         err = -ENOMEM;
380         if (!forget)                              386         if (!forget)
381                 goto out;                         387                 goto out;
382                                                   388 
383         attr_version = fuse_get_attr_version(f    389         attr_version = fuse_get_attr_version(fm->fc);
384                                                   390 
385         fuse_lookup_init(fm->fc, &args, nodeid    391         fuse_lookup_init(fm->fc, &args, nodeid, name, outarg);
386         err = fuse_simple_request(fm, &args);     392         err = fuse_simple_request(fm, &args);
387         /* Zero nodeid is same as -ENOENT, but    393         /* Zero nodeid is same as -ENOENT, but with valid timeout */
388         if (err || !outarg->nodeid)               394         if (err || !outarg->nodeid)
389                 goto out_put_forget;              395                 goto out_put_forget;
390                                                   396 
391         err = -EIO;                               397         err = -EIO;
                                                   >> 398         if (!outarg->nodeid)
                                                   >> 399                 goto out_put_forget;
392         if (fuse_invalid_attr(&outarg->attr))     400         if (fuse_invalid_attr(&outarg->attr))
393                 goto out_put_forget;              401                 goto out_put_forget;
394         if (outarg->nodeid == FUSE_ROOT_ID &&  << 
395                 pr_warn_once("root generation  << 
396                 outarg->generation = 0;        << 
397         }                                      << 
398                                                   402 
399         *inode = fuse_iget(sb, outarg->nodeid,    403         *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
400                            &outarg->attr, ATTR !! 404                            &outarg->attr, entry_attr_timeout(outarg),
401                            attr_version);         405                            attr_version);
402         err = -ENOMEM;                            406         err = -ENOMEM;
403         if (!*inode) {                            407         if (!*inode) {
404                 fuse_queue_forget(fm->fc, forg    408                 fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1);
405                 goto out;                         409                 goto out;
406         }                                         410         }
407         err = 0;                                  411         err = 0;
408                                                   412 
409  out_put_forget:                                  413  out_put_forget:
410         kfree(forget);                            414         kfree(forget);
411  out:                                             415  out:
412         return err;                               416         return err;
413 }                                                 417 }
414                                                   418 
415 static struct dentry *fuse_lookup(struct inode    419 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
416                                   unsigned int    420                                   unsigned int flags)
417 {                                                 421 {
418         int err;                                  422         int err;
419         struct fuse_entry_out outarg;             423         struct fuse_entry_out outarg;
420         struct inode *inode;                      424         struct inode *inode;
421         struct dentry *newent;                    425         struct dentry *newent;
422         bool outarg_valid = true;                 426         bool outarg_valid = true;
423         bool locked;                              427         bool locked;
424                                                   428 
425         if (fuse_is_bad(dir))                     429         if (fuse_is_bad(dir))
426                 return ERR_PTR(-EIO);             430                 return ERR_PTR(-EIO);
427                                                   431 
428         locked = fuse_lock_inode(dir);            432         locked = fuse_lock_inode(dir);
429         err = fuse_lookup_name(dir->i_sb, get_    433         err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
430                                &outarg, &inode    434                                &outarg, &inode);
431         fuse_unlock_inode(dir, locked);           435         fuse_unlock_inode(dir, locked);
432         if (err == -ENOENT) {                     436         if (err == -ENOENT) {
433                 outarg_valid = false;             437                 outarg_valid = false;
434                 err = 0;                          438                 err = 0;
435         }                                         439         }
436         if (err)                                  440         if (err)
437                 goto out_err;                     441                 goto out_err;
438                                                   442 
439         err = -EIO;                               443         err = -EIO;
440         if (inode && get_node_id(inode) == FUS    444         if (inode && get_node_id(inode) == FUSE_ROOT_ID)
441                 goto out_iput;                    445                 goto out_iput;
442                                                   446 
443         newent = d_splice_alias(inode, entry);    447         newent = d_splice_alias(inode, entry);
444         err = PTR_ERR(newent);                    448         err = PTR_ERR(newent);
445         if (IS_ERR(newent))                       449         if (IS_ERR(newent))
446                 goto out_err;                     450                 goto out_err;
447                                                   451 
448         entry = newent ? newent : entry;          452         entry = newent ? newent : entry;
449         if (outarg_valid)                         453         if (outarg_valid)
450                 fuse_change_entry_timeout(entr    454                 fuse_change_entry_timeout(entry, &outarg);
451         else                                      455         else
452                 fuse_invalidate_entry_cache(en    456                 fuse_invalidate_entry_cache(entry);
453                                                   457 
454         if (inode)                                458         if (inode)
455                 fuse_advise_use_readdirplus(di    459                 fuse_advise_use_readdirplus(dir);
456         return newent;                            460         return newent;
457                                                   461 
458  out_iput:                                        462  out_iput:
459         iput(inode);                              463         iput(inode);
460  out_err:                                         464  out_err:
461         return ERR_PTR(err);                      465         return ERR_PTR(err);
462 }                                                 466 }
463                                                   467 
464 static int get_security_context(struct dentry     468 static int get_security_context(struct dentry *entry, umode_t mode,
465                                 struct fuse_in    469                                 struct fuse_in_arg *ext)
466 {                                                 470 {
467         struct fuse_secctx *fctx;                 471         struct fuse_secctx *fctx;
468         struct fuse_secctx_header *header;        472         struct fuse_secctx_header *header;
469         void *ctx = NULL, *ptr;                   473         void *ctx = NULL, *ptr;
470         u32 ctxlen, total_len = sizeof(*header    474         u32 ctxlen, total_len = sizeof(*header);
471         int err, nr_ctx = 0;                      475         int err, nr_ctx = 0;
472         const char *name;                         476         const char *name;
473         size_t namelen;                           477         size_t namelen;
474                                                   478 
475         err = security_dentry_init_security(en    479         err = security_dentry_init_security(entry, mode, &entry->d_name,
476                                             &n    480                                             &name, &ctx, &ctxlen);
477         if (err) {                                481         if (err) {
478                 if (err != -EOPNOTSUPP)           482                 if (err != -EOPNOTSUPP)
479                         goto out_err;             483                         goto out_err;
480                 /* No LSM is supporting this s    484                 /* No LSM is supporting this security hook. Ignore error */
481                 ctxlen = 0;                       485                 ctxlen = 0;
482                 ctx = NULL;                       486                 ctx = NULL;
483         }                                         487         }
484                                                   488 
485         if (ctxlen) {                             489         if (ctxlen) {
486                 nr_ctx = 1;                       490                 nr_ctx = 1;
487                 namelen = strlen(name) + 1;       491                 namelen = strlen(name) + 1;
488                 err = -EIO;                       492                 err = -EIO;
489                 if (WARN_ON(namelen > XATTR_NA    493                 if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || ctxlen > S32_MAX))
490                         goto out_err;             494                         goto out_err;
491                 total_len += FUSE_REC_ALIGN(si    495                 total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + ctxlen);
492         }                                         496         }
493                                                   497 
494         err = -ENOMEM;                            498         err = -ENOMEM;
495         header = ptr = kzalloc(total_len, GFP_    499         header = ptr = kzalloc(total_len, GFP_KERNEL);
496         if (!ptr)                                 500         if (!ptr)
497                 goto out_err;                     501                 goto out_err;
498                                                   502 
499         header->nr_secctx = nr_ctx;               503         header->nr_secctx = nr_ctx;
500         header->size = total_len;                 504         header->size = total_len;
501         ptr += sizeof(*header);                   505         ptr += sizeof(*header);
502         if (nr_ctx) {                             506         if (nr_ctx) {
503                 fctx = ptr;                       507                 fctx = ptr;
504                 fctx->size = ctxlen;              508                 fctx->size = ctxlen;
505                 ptr += sizeof(*fctx);             509                 ptr += sizeof(*fctx);
506                                                   510 
507                 strcpy(ptr, name);                511                 strcpy(ptr, name);
508                 ptr += namelen;                   512                 ptr += namelen;
509                                                   513 
510                 memcpy(ptr, ctx, ctxlen);         514                 memcpy(ptr, ctx, ctxlen);
511         }                                         515         }
512         ext->size = total_len;                    516         ext->size = total_len;
513         ext->value = header;                      517         ext->value = header;
514         err = 0;                                  518         err = 0;
515 out_err:                                          519 out_err:
516         kfree(ctx);                               520         kfree(ctx);
517         return err;                               521         return err;
518 }                                                 522 }
519                                                   523 
520 static void *extend_arg(struct fuse_in_arg *bu    524 static void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
521 {                                                 525 {
522         void *p;                                  526         void *p;
523         u32 newlen = buf->size + bytes;           527         u32 newlen = buf->size + bytes;
524                                                   528 
525         p = krealloc(buf->value, newlen, GFP_K    529         p = krealloc(buf->value, newlen, GFP_KERNEL);
526         if (!p) {                                 530         if (!p) {
527                 kfree(buf->value);                531                 kfree(buf->value);
528                 buf->size = 0;                    532                 buf->size = 0;
529                 buf->value = NULL;                533                 buf->value = NULL;
530                 return NULL;                      534                 return NULL;
531         }                                         535         }
532                                                   536 
533         memset(p + buf->size, 0, bytes);          537         memset(p + buf->size, 0, bytes);
534         buf->value = p;                           538         buf->value = p;
535         buf->size = newlen;                       539         buf->size = newlen;
536                                                   540 
537         return p + newlen - bytes;                541         return p + newlen - bytes;
538 }                                                 542 }
539                                                   543 
540 static u32 fuse_ext_size(size_t size)             544 static u32 fuse_ext_size(size_t size)
541 {                                                 545 {
542         return FUSE_REC_ALIGN(sizeof(struct fu    546         return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size);
543 }                                                 547 }
544                                                   548 
545 /*                                                549 /*
546  * This adds just a single supplementary group    550  * This adds just a single supplementary group that matches the parent's group.
547  */                                               551  */
548 static int get_create_supp_group(struct inode     552 static int get_create_supp_group(struct inode *dir, struct fuse_in_arg *ext)
549 {                                                 553 {
550         struct fuse_conn *fc = get_fuse_conn(d    554         struct fuse_conn *fc = get_fuse_conn(dir);
551         struct fuse_ext_header *xh;               555         struct fuse_ext_header *xh;
552         struct fuse_supp_groups *sg;              556         struct fuse_supp_groups *sg;
553         kgid_t kgid = dir->i_gid;                 557         kgid_t kgid = dir->i_gid;
554         gid_t parent_gid = from_kgid(fc->user_    558         gid_t parent_gid = from_kgid(fc->user_ns, kgid);
555         u32 sg_len = fuse_ext_size(sizeof(*sg)    559         u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0]));
556                                                   560 
557         if (parent_gid == (gid_t) -1 || gid_eq    561         if (parent_gid == (gid_t) -1 || gid_eq(kgid, current_fsgid()) ||
558             !in_group_p(kgid))                    562             !in_group_p(kgid))
559                 return 0;                         563                 return 0;
560                                                   564 
561         xh = extend_arg(ext, sg_len);             565         xh = extend_arg(ext, sg_len);
562         if (!xh)                                  566         if (!xh)
563                 return -ENOMEM;                   567                 return -ENOMEM;
564                                                   568 
565         xh->size = sg_len;                        569         xh->size = sg_len;
566         xh->type = FUSE_EXT_GROUPS;               570         xh->type = FUSE_EXT_GROUPS;
567                                                   571 
568         sg = (struct fuse_supp_groups *) &xh[1    572         sg = (struct fuse_supp_groups *) &xh[1];
569         sg->nr_groups = 1;                        573         sg->nr_groups = 1;
570         sg->groups[0] = parent_gid;               574         sg->groups[0] = parent_gid;
571                                                   575 
572         return 0;                                 576         return 0;
573 }                                                 577 }
574                                                   578 
575 static int get_create_ext(struct fuse_args *ar    579 static int get_create_ext(struct fuse_args *args,
576                           struct inode *dir, s    580                           struct inode *dir, struct dentry *dentry,
577                           umode_t mode)           581                           umode_t mode)
578 {                                                 582 {
579         struct fuse_conn *fc = get_fuse_conn_s    583         struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
580         struct fuse_in_arg ext = { .size = 0,     584         struct fuse_in_arg ext = { .size = 0, .value = NULL };
581         int err = 0;                              585         int err = 0;
582                                                   586 
583         if (fc->init_security)                    587         if (fc->init_security)
584                 err = get_security_context(den    588                 err = get_security_context(dentry, mode, &ext);
585         if (!err && fc->create_supp_group)        589         if (!err && fc->create_supp_group)
586                 err = get_create_supp_group(di    590                 err = get_create_supp_group(dir, &ext);
587                                                   591 
588         if (!err && ext.size) {                   592         if (!err && ext.size) {
589                 WARN_ON(args->in_numargs >= AR    593                 WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args));
590                 args->is_ext = true;              594                 args->is_ext = true;
591                 args->ext_idx = args->in_numar    595                 args->ext_idx = args->in_numargs++;
592                 args->in_args[args->ext_idx] =    596                 args->in_args[args->ext_idx] = ext;
593         } else {                                  597         } else {
594                 kfree(ext.value);                 598                 kfree(ext.value);
595         }                                         599         }
596                                                   600 
597         return err;                               601         return err;
598 }                                                 602 }
599                                                   603 
600 static void free_ext_value(struct fuse_args *a    604 static void free_ext_value(struct fuse_args *args)
601 {                                                 605 {
602         if (args->is_ext)                         606         if (args->is_ext)
603                 kfree(args->in_args[args->ext_    607                 kfree(args->in_args[args->ext_idx].value);
604 }                                                 608 }
605                                                   609 
606 /*                                                610 /*
607  * Atomic create+open operation                   611  * Atomic create+open operation
608  *                                                612  *
609  * If the filesystem doesn't support this, the    613  * If the filesystem doesn't support this, then fall back to separate
610  * 'mknod' + 'open' requests.                     614  * 'mknod' + 'open' requests.
611  */                                               615  */
612 static int fuse_create_open(struct inode *dir,    616 static int fuse_create_open(struct inode *dir, struct dentry *entry,
613                             struct file *file,    617                             struct file *file, unsigned int flags,
614                             umode_t mode, u32     618                             umode_t mode, u32 opcode)
615 {                                                 619 {
616         int err;                                  620         int err;
617         struct inode *inode;                      621         struct inode *inode;
618         struct fuse_mount *fm = get_fuse_mount    622         struct fuse_mount *fm = get_fuse_mount(dir);
619         FUSE_ARGS(args);                          623         FUSE_ARGS(args);
620         struct fuse_forget_link *forget;          624         struct fuse_forget_link *forget;
621         struct fuse_create_in inarg;              625         struct fuse_create_in inarg;
622         struct fuse_open_out *outopenp;        !! 626         struct fuse_open_out outopen;
623         struct fuse_entry_out outentry;           627         struct fuse_entry_out outentry;
624         struct fuse_inode *fi;                    628         struct fuse_inode *fi;
625         struct fuse_file *ff;                     629         struct fuse_file *ff;
626         bool trunc = flags & O_TRUNC;             630         bool trunc = flags & O_TRUNC;
627                                                   631 
628         /* Userspace expects S_IFREG in create    632         /* Userspace expects S_IFREG in create mode */
629         BUG_ON((mode & S_IFMT) != S_IFREG);       633         BUG_ON((mode & S_IFMT) != S_IFREG);
630                                                   634 
631         forget = fuse_alloc_forget();             635         forget = fuse_alloc_forget();
632         err = -ENOMEM;                            636         err = -ENOMEM;
633         if (!forget)                              637         if (!forget)
634                 goto out_err;                     638                 goto out_err;
635                                                   639 
636         err = -ENOMEM;                            640         err = -ENOMEM;
637         ff = fuse_file_alloc(fm, true);        !! 641         ff = fuse_file_alloc(fm);
638         if (!ff)                                  642         if (!ff)
639                 goto out_put_forget_req;          643                 goto out_put_forget_req;
640                                                   644 
641         if (!fm->fc->dont_mask)                   645         if (!fm->fc->dont_mask)
642                 mode &= ~current_umask();         646                 mode &= ~current_umask();
643                                                   647 
644         flags &= ~O_NOCTTY;                       648         flags &= ~O_NOCTTY;
645         memset(&inarg, 0, sizeof(inarg));         649         memset(&inarg, 0, sizeof(inarg));
646         memset(&outentry, 0, sizeof(outentry))    650         memset(&outentry, 0, sizeof(outentry));
647         inarg.flags = flags;                      651         inarg.flags = flags;
648         inarg.mode = mode;                        652         inarg.mode = mode;
649         inarg.umask = current_umask();            653         inarg.umask = current_umask();
650                                                   654 
651         if (fm->fc->handle_killpriv_v2 && trun    655         if (fm->fc->handle_killpriv_v2 && trunc &&
652             !(flags & O_EXCL) && !capable(CAP_    656             !(flags & O_EXCL) && !capable(CAP_FSETID)) {
653                 inarg.open_flags |= FUSE_OPEN_    657                 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
654         }                                         658         }
655                                                   659 
656         args.opcode = opcode;                     660         args.opcode = opcode;
657         args.nodeid = get_node_id(dir);           661         args.nodeid = get_node_id(dir);
658         args.in_numargs = 2;                      662         args.in_numargs = 2;
659         args.in_args[0].size = sizeof(inarg);     663         args.in_args[0].size = sizeof(inarg);
660         args.in_args[0].value = &inarg;           664         args.in_args[0].value = &inarg;
661         args.in_args[1].size = entry->d_name.l    665         args.in_args[1].size = entry->d_name.len + 1;
662         args.in_args[1].value = entry->d_name.    666         args.in_args[1].value = entry->d_name.name;
663         args.out_numargs = 2;                     667         args.out_numargs = 2;
664         args.out_args[0].size = sizeof(outentr    668         args.out_args[0].size = sizeof(outentry);
665         args.out_args[0].value = &outentry;       669         args.out_args[0].value = &outentry;
666         /* Store outarg for fuse_finish_open() !! 670         args.out_args[1].size = sizeof(outopen);
667         outopenp = &ff->args->open_outarg;     !! 671         args.out_args[1].value = &outopen;
668         args.out_args[1].size = sizeof(*outope << 
669         args.out_args[1].value = outopenp;     << 
670                                                   672 
671         err = get_create_ext(&args, dir, entry    673         err = get_create_ext(&args, dir, entry, mode);
672         if (err)                                  674         if (err)
673                 goto out_free_ff;              !! 675                 goto out_put_forget_req;
674                                                   676 
675         err = fuse_simple_request(fm, &args);     677         err = fuse_simple_request(fm, &args);
676         free_ext_value(&args);                    678         free_ext_value(&args);
677         if (err)                                  679         if (err)
678                 goto out_free_ff;                 680                 goto out_free_ff;
679                                                   681 
680         err = -EIO;                               682         err = -EIO;
681         if (!S_ISREG(outentry.attr.mode) || in    683         if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
682             fuse_invalid_attr(&outentry.attr))    684             fuse_invalid_attr(&outentry.attr))
683                 goto out_free_ff;                 685                 goto out_free_ff;
684                                                   686 
685         ff->fh = outopenp->fh;                 !! 687         ff->fh = outopen.fh;
686         ff->nodeid = outentry.nodeid;             688         ff->nodeid = outentry.nodeid;
687         ff->open_flags = outopenp->open_flags; !! 689         ff->open_flags = outopen.open_flags;
688         inode = fuse_iget(dir->i_sb, outentry.    690         inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
689                           &outentry.attr, ATTR !! 691                           &outentry.attr, entry_attr_timeout(&outentry), 0);
690         if (!inode) {                             692         if (!inode) {
691                 flags &= ~(O_CREAT | O_EXCL |     693                 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
692                 fuse_sync_release(NULL, ff, fl    694                 fuse_sync_release(NULL, ff, flags);
693                 fuse_queue_forget(fm->fc, forg    695                 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
694                 err = -ENOMEM;                    696                 err = -ENOMEM;
695                 goto out_err;                     697                 goto out_err;
696         }                                         698         }
697         kfree(forget);                            699         kfree(forget);
698         d_instantiate(entry, inode);              700         d_instantiate(entry, inode);
699         fuse_change_entry_timeout(entry, &oute    701         fuse_change_entry_timeout(entry, &outentry);
700         fuse_dir_changed(dir);                    702         fuse_dir_changed(dir);
701         err = generic_file_open(inode, file);  !! 703         err = finish_open(file, entry, generic_file_open);
702         if (!err) {                            << 
703                 file->private_data = ff;       << 
704                 err = finish_open(file, entry, << 
705         }                                      << 
706         if (err) {                                704         if (err) {
707                 fi = get_fuse_inode(inode);       705                 fi = get_fuse_inode(inode);
708                 fuse_sync_release(fi, ff, flag    706                 fuse_sync_release(fi, ff, flags);
709         } else {                                  707         } else {
                                                   >> 708                 file->private_data = ff;
                                                   >> 709                 fuse_finish_open(inode, file);
710                 if (fm->fc->atomic_o_trunc &&     710                 if (fm->fc->atomic_o_trunc && trunc)
711                         truncate_pagecache(ino    711                         truncate_pagecache(inode, 0);
712                 else if (!(ff->open_flags & FO    712                 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
713                         invalidate_inode_pages    713                         invalidate_inode_pages2(inode->i_mapping);
714         }                                         714         }
715         return err;                               715         return err;
716                                                   716 
717 out_free_ff:                                      717 out_free_ff:
718         fuse_file_free(ff);                       718         fuse_file_free(ff);
719 out_put_forget_req:                               719 out_put_forget_req:
720         kfree(forget);                            720         kfree(forget);
721 out_err:                                          721 out_err:
722         return err;                               722         return err;
723 }                                                 723 }
724                                                   724 
725 static int fuse_mknod(struct mnt_idmap *, stru    725 static int fuse_mknod(struct mnt_idmap *, struct inode *, struct dentry *,
726                       umode_t, dev_t);            726                       umode_t, dev_t);
727 static int fuse_atomic_open(struct inode *dir,    727 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
728                             struct file *file,    728                             struct file *file, unsigned flags,
729                             umode_t mode)         729                             umode_t mode)
730 {                                                 730 {
731         int err;                                  731         int err;
732         struct fuse_conn *fc = get_fuse_conn(d    732         struct fuse_conn *fc = get_fuse_conn(dir);
733         struct dentry *res = NULL;                733         struct dentry *res = NULL;
734                                                   734 
735         if (fuse_is_bad(dir))                     735         if (fuse_is_bad(dir))
736                 return -EIO;                      736                 return -EIO;
737                                                   737 
738         if (d_in_lookup(entry)) {                 738         if (d_in_lookup(entry)) {
739                 res = fuse_lookup(dir, entry,     739                 res = fuse_lookup(dir, entry, 0);
740                 if (IS_ERR(res))                  740                 if (IS_ERR(res))
741                         return PTR_ERR(res);      741                         return PTR_ERR(res);
742                                                   742 
743                 if (res)                          743                 if (res)
744                         entry = res;              744                         entry = res;
745         }                                         745         }
746                                                   746 
747         if (!(flags & O_CREAT) || d_really_is_    747         if (!(flags & O_CREAT) || d_really_is_positive(entry))
748                 goto no_open;                     748                 goto no_open;
749                                                   749 
750         /* Only creates */                        750         /* Only creates */
751         file->f_mode |= FMODE_CREATED;            751         file->f_mode |= FMODE_CREATED;
752                                                   752 
753         if (fc->no_create)                        753         if (fc->no_create)
754                 goto mknod;                       754                 goto mknod;
755                                                   755 
756         err = fuse_create_open(dir, entry, fil    756         err = fuse_create_open(dir, entry, file, flags, mode, FUSE_CREATE);
757         if (err == -ENOSYS) {                     757         if (err == -ENOSYS) {
758                 fc->no_create = 1;                758                 fc->no_create = 1;
759                 goto mknod;                       759                 goto mknod;
760         } else if (err == -EEXIST)             !! 760         }
761                 fuse_invalidate_entry(entry);  << 
762 out_dput:                                         761 out_dput:
763         dput(res);                                762         dput(res);
764         return err;                               763         return err;
765                                                   764 
766 mknod:                                            765 mknod:
767         err = fuse_mknod(&nop_mnt_idmap, dir,     766         err = fuse_mknod(&nop_mnt_idmap, dir, entry, mode, 0);
768         if (err)                                  767         if (err)
769                 goto out_dput;                    768                 goto out_dput;
770 no_open:                                          769 no_open:
771         return finish_no_open(file, res);         770         return finish_no_open(file, res);
772 }                                                 771 }
773                                                   772 
774 /*                                                773 /*
775  * Code shared between mknod, mkdir, symlink a    774  * Code shared between mknod, mkdir, symlink and link
776  */                                               775  */
777 static int create_new_entry(struct fuse_mount     776 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
778                             struct inode *dir,    777                             struct inode *dir, struct dentry *entry,
779                             umode_t mode)         778                             umode_t mode)
780 {                                                 779 {
781         struct fuse_entry_out outarg;             780         struct fuse_entry_out outarg;
782         struct inode *inode;                      781         struct inode *inode;
783         struct dentry *d;                         782         struct dentry *d;
784         int err;                                  783         int err;
785         struct fuse_forget_link *forget;          784         struct fuse_forget_link *forget;
786                                                   785 
787         if (fuse_is_bad(dir))                     786         if (fuse_is_bad(dir))
788                 return -EIO;                      787                 return -EIO;
789                                                   788 
790         forget = fuse_alloc_forget();             789         forget = fuse_alloc_forget();
791         if (!forget)                              790         if (!forget)
792                 return -ENOMEM;                   791                 return -ENOMEM;
793                                                   792 
794         memset(&outarg, 0, sizeof(outarg));       793         memset(&outarg, 0, sizeof(outarg));
795         args->nodeid = get_node_id(dir);          794         args->nodeid = get_node_id(dir);
796         args->out_numargs = 1;                    795         args->out_numargs = 1;
797         args->out_args[0].size = sizeof(outarg    796         args->out_args[0].size = sizeof(outarg);
798         args->out_args[0].value = &outarg;        797         args->out_args[0].value = &outarg;
799                                                   798 
800         if (args->opcode != FUSE_LINK) {          799         if (args->opcode != FUSE_LINK) {
801                 err = get_create_ext(args, dir    800                 err = get_create_ext(args, dir, entry, mode);
802                 if (err)                          801                 if (err)
803                         goto out_put_forget_re    802                         goto out_put_forget_req;
804         }                                         803         }
805                                                   804 
806         err = fuse_simple_request(fm, args);      805         err = fuse_simple_request(fm, args);
807         free_ext_value(args);                     806         free_ext_value(args);
808         if (err)                                  807         if (err)
809                 goto out_put_forget_req;          808                 goto out_put_forget_req;
810                                                   809 
811         err = -EIO;                               810         err = -EIO;
812         if (invalid_nodeid(outarg.nodeid) || f    811         if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
813                 goto out_put_forget_req;          812                 goto out_put_forget_req;
814                                                   813 
815         if ((outarg.attr.mode ^ mode) & S_IFMT    814         if ((outarg.attr.mode ^ mode) & S_IFMT)
816                 goto out_put_forget_req;          815                 goto out_put_forget_req;
817                                                   816 
818         inode = fuse_iget(dir->i_sb, outarg.no    817         inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
819                           &outarg.attr, ATTR_T !! 818                           &outarg.attr, entry_attr_timeout(&outarg), 0);
820         if (!inode) {                             819         if (!inode) {
821                 fuse_queue_forget(fm->fc, forg    820                 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
822                 return -ENOMEM;                   821                 return -ENOMEM;
823         }                                         822         }
824         kfree(forget);                            823         kfree(forget);
825                                                   824 
826         d_drop(entry);                            825         d_drop(entry);
827         d = d_splice_alias(inode, entry);         826         d = d_splice_alias(inode, entry);
828         if (IS_ERR(d))                            827         if (IS_ERR(d))
829                 return PTR_ERR(d);                828                 return PTR_ERR(d);
830                                                   829 
831         if (d) {                                  830         if (d) {
832                 fuse_change_entry_timeout(d, &    831                 fuse_change_entry_timeout(d, &outarg);
833                 dput(d);                          832                 dput(d);
834         } else {                                  833         } else {
835                 fuse_change_entry_timeout(entr    834                 fuse_change_entry_timeout(entry, &outarg);
836         }                                         835         }
837         fuse_dir_changed(dir);                    836         fuse_dir_changed(dir);
838         return 0;                                 837         return 0;
839                                                   838 
840  out_put_forget_req:                              839  out_put_forget_req:
841         if (err == -EEXIST)                    << 
842                 fuse_invalidate_entry(entry);  << 
843         kfree(forget);                            840         kfree(forget);
844         return err;                               841         return err;
845 }                                                 842 }
846                                                   843 
847 static int fuse_mknod(struct mnt_idmap *idmap,    844 static int fuse_mknod(struct mnt_idmap *idmap, struct inode *dir,
848                       struct dentry *entry, um    845                       struct dentry *entry, umode_t mode, dev_t rdev)
849 {                                                 846 {
850         struct fuse_mknod_in inarg;               847         struct fuse_mknod_in inarg;
851         struct fuse_mount *fm = get_fuse_mount    848         struct fuse_mount *fm = get_fuse_mount(dir);
852         FUSE_ARGS(args);                          849         FUSE_ARGS(args);
853                                                   850 
854         if (!fm->fc->dont_mask)                   851         if (!fm->fc->dont_mask)
855                 mode &= ~current_umask();         852                 mode &= ~current_umask();
856                                                   853 
857         memset(&inarg, 0, sizeof(inarg));         854         memset(&inarg, 0, sizeof(inarg));
858         inarg.mode = mode;                        855         inarg.mode = mode;
859         inarg.rdev = new_encode_dev(rdev);        856         inarg.rdev = new_encode_dev(rdev);
860         inarg.umask = current_umask();            857         inarg.umask = current_umask();
861         args.opcode = FUSE_MKNOD;                 858         args.opcode = FUSE_MKNOD;
862         args.in_numargs = 2;                      859         args.in_numargs = 2;
863         args.in_args[0].size = sizeof(inarg);     860         args.in_args[0].size = sizeof(inarg);
864         args.in_args[0].value = &inarg;           861         args.in_args[0].value = &inarg;
865         args.in_args[1].size = entry->d_name.l    862         args.in_args[1].size = entry->d_name.len + 1;
866         args.in_args[1].value = entry->d_name.    863         args.in_args[1].value = entry->d_name.name;
867         return create_new_entry(fm, &args, dir    864         return create_new_entry(fm, &args, dir, entry, mode);
868 }                                                 865 }
869                                                   866 
870 static int fuse_create(struct mnt_idmap *idmap    867 static int fuse_create(struct mnt_idmap *idmap, struct inode *dir,
871                        struct dentry *entry, u    868                        struct dentry *entry, umode_t mode, bool excl)
872 {                                                 869 {
873         return fuse_mknod(&nop_mnt_idmap, dir,    870         return fuse_mknod(&nop_mnt_idmap, dir, entry, mode, 0);
874 }                                                 871 }
875                                                   872 
876 static int fuse_tmpfile(struct mnt_idmap *idma    873 static int fuse_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
877                         struct file *file, umo    874                         struct file *file, umode_t mode)
878 {                                                 875 {
879         struct fuse_conn *fc = get_fuse_conn(d    876         struct fuse_conn *fc = get_fuse_conn(dir);
880         int err;                                  877         int err;
881                                                   878 
882         if (fc->no_tmpfile)                       879         if (fc->no_tmpfile)
883                 return -EOPNOTSUPP;               880                 return -EOPNOTSUPP;
884                                                   881 
885         err = fuse_create_open(dir, file->f_pa    882         err = fuse_create_open(dir, file->f_path.dentry, file, file->f_flags, mode, FUSE_TMPFILE);
886         if (err == -ENOSYS) {                     883         if (err == -ENOSYS) {
887                 fc->no_tmpfile = 1;               884                 fc->no_tmpfile = 1;
888                 err = -EOPNOTSUPP;                885                 err = -EOPNOTSUPP;
889         }                                         886         }
890         return err;                               887         return err;
891 }                                                 888 }
892                                                   889 
893 static int fuse_mkdir(struct mnt_idmap *idmap,    890 static int fuse_mkdir(struct mnt_idmap *idmap, struct inode *dir,
894                       struct dentry *entry, um    891                       struct dentry *entry, umode_t mode)
895 {                                                 892 {
896         struct fuse_mkdir_in inarg;               893         struct fuse_mkdir_in inarg;
897         struct fuse_mount *fm = get_fuse_mount    894         struct fuse_mount *fm = get_fuse_mount(dir);
898         FUSE_ARGS(args);                          895         FUSE_ARGS(args);
899                                                   896 
900         if (!fm->fc->dont_mask)                   897         if (!fm->fc->dont_mask)
901                 mode &= ~current_umask();         898                 mode &= ~current_umask();
902                                                   899 
903         memset(&inarg, 0, sizeof(inarg));         900         memset(&inarg, 0, sizeof(inarg));
904         inarg.mode = mode;                        901         inarg.mode = mode;
905         inarg.umask = current_umask();            902         inarg.umask = current_umask();
906         args.opcode = FUSE_MKDIR;                 903         args.opcode = FUSE_MKDIR;
907         args.in_numargs = 2;                      904         args.in_numargs = 2;
908         args.in_args[0].size = sizeof(inarg);     905         args.in_args[0].size = sizeof(inarg);
909         args.in_args[0].value = &inarg;           906         args.in_args[0].value = &inarg;
910         args.in_args[1].size = entry->d_name.l    907         args.in_args[1].size = entry->d_name.len + 1;
911         args.in_args[1].value = entry->d_name.    908         args.in_args[1].value = entry->d_name.name;
912         return create_new_entry(fm, &args, dir    909         return create_new_entry(fm, &args, dir, entry, S_IFDIR);
913 }                                                 910 }
914                                                   911 
915 static int fuse_symlink(struct mnt_idmap *idma    912 static int fuse_symlink(struct mnt_idmap *idmap, struct inode *dir,
916                         struct dentry *entry,     913                         struct dentry *entry, const char *link)
917 {                                                 914 {
918         struct fuse_mount *fm = get_fuse_mount    915         struct fuse_mount *fm = get_fuse_mount(dir);
919         unsigned len = strlen(link) + 1;          916         unsigned len = strlen(link) + 1;
920         FUSE_ARGS(args);                          917         FUSE_ARGS(args);
921                                                   918 
922         args.opcode = FUSE_SYMLINK;               919         args.opcode = FUSE_SYMLINK;
923         args.in_numargs = 2;                      920         args.in_numargs = 2;
924         args.in_args[0].size = entry->d_name.l    921         args.in_args[0].size = entry->d_name.len + 1;
925         args.in_args[0].value = entry->d_name.    922         args.in_args[0].value = entry->d_name.name;
926         args.in_args[1].size = len;               923         args.in_args[1].size = len;
927         args.in_args[1].value = link;             924         args.in_args[1].value = link;
928         return create_new_entry(fm, &args, dir    925         return create_new_entry(fm, &args, dir, entry, S_IFLNK);
929 }                                                 926 }
930                                                   927 
931 void fuse_flush_time_update(struct inode *inod    928 void fuse_flush_time_update(struct inode *inode)
932 {                                                 929 {
933         int err = sync_inode_metadata(inode, 1    930         int err = sync_inode_metadata(inode, 1);
934                                                   931 
935         mapping_set_error(inode->i_mapping, er    932         mapping_set_error(inode->i_mapping, err);
936 }                                                 933 }
937                                                   934 
938 static void fuse_update_ctime_in_cache(struct     935 static void fuse_update_ctime_in_cache(struct inode *inode)
939 {                                                 936 {
940         if (!IS_NOCMTIME(inode)) {                937         if (!IS_NOCMTIME(inode)) {
941                 inode_set_ctime_current(inode) !! 938                 inode->i_ctime = current_time(inode);
942                 mark_inode_dirty_sync(inode);     939                 mark_inode_dirty_sync(inode);
943                 fuse_flush_time_update(inode);    940                 fuse_flush_time_update(inode);
944         }                                         941         }
945 }                                                 942 }
946                                                   943 
947 void fuse_update_ctime(struct inode *inode)       944 void fuse_update_ctime(struct inode *inode)
948 {                                                 945 {
949         fuse_invalidate_attr_mask(inode, STATX    946         fuse_invalidate_attr_mask(inode, STATX_CTIME);
950         fuse_update_ctime_in_cache(inode);        947         fuse_update_ctime_in_cache(inode);
951 }                                                 948 }
952                                                   949 
953 static void fuse_entry_unlinked(struct dentry     950 static void fuse_entry_unlinked(struct dentry *entry)
954 {                                                 951 {
955         struct inode *inode = d_inode(entry);     952         struct inode *inode = d_inode(entry);
956         struct fuse_conn *fc = get_fuse_conn(i    953         struct fuse_conn *fc = get_fuse_conn(inode);
957         struct fuse_inode *fi = get_fuse_inode    954         struct fuse_inode *fi = get_fuse_inode(inode);
958                                                   955 
959         spin_lock(&fi->lock);                     956         spin_lock(&fi->lock);
960         fi->attr_version = atomic64_inc_return    957         fi->attr_version = atomic64_inc_return(&fc->attr_version);
961         /*                                        958         /*
962          * If i_nlink == 0 then unlink doesn't    959          * If i_nlink == 0 then unlink doesn't make sense, yet this can
963          * happen if userspace filesystem is c    960          * happen if userspace filesystem is careless.  It would be
964          * difficult to enforce correct nlink     961          * difficult to enforce correct nlink usage so just ignore this
965          * condition here                         962          * condition here
966          */                                       963          */
967         if (S_ISDIR(inode->i_mode))               964         if (S_ISDIR(inode->i_mode))
968                 clear_nlink(inode);               965                 clear_nlink(inode);
969         else if (inode->i_nlink > 0)              966         else if (inode->i_nlink > 0)
970                 drop_nlink(inode);                967                 drop_nlink(inode);
971         spin_unlock(&fi->lock);                   968         spin_unlock(&fi->lock);
972         fuse_invalidate_entry_cache(entry);       969         fuse_invalidate_entry_cache(entry);
973         fuse_update_ctime(inode);                 970         fuse_update_ctime(inode);
974 }                                                 971 }
975                                                   972 
976 static int fuse_unlink(struct inode *dir, stru    973 static int fuse_unlink(struct inode *dir, struct dentry *entry)
977 {                                                 974 {
978         int err;                                  975         int err;
979         struct fuse_mount *fm = get_fuse_mount    976         struct fuse_mount *fm = get_fuse_mount(dir);
980         FUSE_ARGS(args);                          977         FUSE_ARGS(args);
981                                                   978 
982         if (fuse_is_bad(dir))                     979         if (fuse_is_bad(dir))
983                 return -EIO;                      980                 return -EIO;
984                                                   981 
985         args.opcode = FUSE_UNLINK;                982         args.opcode = FUSE_UNLINK;
986         args.nodeid = get_node_id(dir);           983         args.nodeid = get_node_id(dir);
987         args.in_numargs = 1;                      984         args.in_numargs = 1;
988         args.in_args[0].size = entry->d_name.l    985         args.in_args[0].size = entry->d_name.len + 1;
989         args.in_args[0].value = entry->d_name.    986         args.in_args[0].value = entry->d_name.name;
990         err = fuse_simple_request(fm, &args);     987         err = fuse_simple_request(fm, &args);
991         if (!err) {                               988         if (!err) {
992                 fuse_dir_changed(dir);            989                 fuse_dir_changed(dir);
993                 fuse_entry_unlinked(entry);       990                 fuse_entry_unlinked(entry);
994         } else if (err == -EINTR || err == -EN !! 991         } else if (err == -EINTR)
995                 fuse_invalidate_entry(entry);     992                 fuse_invalidate_entry(entry);
996         return err;                               993         return err;
997 }                                                 994 }
998                                                   995 
999 static int fuse_rmdir(struct inode *dir, struc    996 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
1000 {                                                997 {
1001         int err;                                 998         int err;
1002         struct fuse_mount *fm = get_fuse_moun    999         struct fuse_mount *fm = get_fuse_mount(dir);
1003         FUSE_ARGS(args);                         1000         FUSE_ARGS(args);
1004                                                  1001 
1005         if (fuse_is_bad(dir))                    1002         if (fuse_is_bad(dir))
1006                 return -EIO;                     1003                 return -EIO;
1007                                                  1004 
1008         args.opcode = FUSE_RMDIR;                1005         args.opcode = FUSE_RMDIR;
1009         args.nodeid = get_node_id(dir);          1006         args.nodeid = get_node_id(dir);
1010         args.in_numargs = 1;                     1007         args.in_numargs = 1;
1011         args.in_args[0].size = entry->d_name.    1008         args.in_args[0].size = entry->d_name.len + 1;
1012         args.in_args[0].value = entry->d_name    1009         args.in_args[0].value = entry->d_name.name;
1013         err = fuse_simple_request(fm, &args);    1010         err = fuse_simple_request(fm, &args);
1014         if (!err) {                              1011         if (!err) {
1015                 fuse_dir_changed(dir);           1012                 fuse_dir_changed(dir);
1016                 fuse_entry_unlinked(entry);      1013                 fuse_entry_unlinked(entry);
1017         } else if (err == -EINTR || err == -E !! 1014         } else if (err == -EINTR)
1018                 fuse_invalidate_entry(entry);    1015                 fuse_invalidate_entry(entry);
1019         return err;                              1016         return err;
1020 }                                                1017 }
1021                                                  1018 
1022 static int fuse_rename_common(struct inode *o    1019 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
1023                               struct inode *n    1020                               struct inode *newdir, struct dentry *newent,
1024                               unsigned int fl    1021                               unsigned int flags, int opcode, size_t argsize)
1025 {                                                1022 {
1026         int err;                                 1023         int err;
1027         struct fuse_rename2_in inarg;            1024         struct fuse_rename2_in inarg;
1028         struct fuse_mount *fm = get_fuse_moun    1025         struct fuse_mount *fm = get_fuse_mount(olddir);
1029         FUSE_ARGS(args);                         1026         FUSE_ARGS(args);
1030                                                  1027 
1031         memset(&inarg, 0, argsize);              1028         memset(&inarg, 0, argsize);
1032         inarg.newdir = get_node_id(newdir);      1029         inarg.newdir = get_node_id(newdir);
1033         inarg.flags = flags;                     1030         inarg.flags = flags;
1034         args.opcode = opcode;                    1031         args.opcode = opcode;
1035         args.nodeid = get_node_id(olddir);       1032         args.nodeid = get_node_id(olddir);
1036         args.in_numargs = 3;                     1033         args.in_numargs = 3;
1037         args.in_args[0].size = argsize;          1034         args.in_args[0].size = argsize;
1038         args.in_args[0].value = &inarg;          1035         args.in_args[0].value = &inarg;
1039         args.in_args[1].size = oldent->d_name    1036         args.in_args[1].size = oldent->d_name.len + 1;
1040         args.in_args[1].value = oldent->d_nam    1037         args.in_args[1].value = oldent->d_name.name;
1041         args.in_args[2].size = newent->d_name    1038         args.in_args[2].size = newent->d_name.len + 1;
1042         args.in_args[2].value = newent->d_nam    1039         args.in_args[2].value = newent->d_name.name;
1043         err = fuse_simple_request(fm, &args);    1040         err = fuse_simple_request(fm, &args);
1044         if (!err) {                              1041         if (!err) {
1045                 /* ctime changes */              1042                 /* ctime changes */
1046                 fuse_update_ctime(d_inode(old    1043                 fuse_update_ctime(d_inode(oldent));
1047                                                  1044 
1048                 if (flags & RENAME_EXCHANGE)     1045                 if (flags & RENAME_EXCHANGE)
1049                         fuse_update_ctime(d_i    1046                         fuse_update_ctime(d_inode(newent));
1050                                                  1047 
1051                 fuse_dir_changed(olddir);        1048                 fuse_dir_changed(olddir);
1052                 if (olddir != newdir)            1049                 if (olddir != newdir)
1053                         fuse_dir_changed(newd    1050                         fuse_dir_changed(newdir);
1054                                                  1051 
1055                 /* newent will end up negativ    1052                 /* newent will end up negative */
1056                 if (!(flags & RENAME_EXCHANGE    1053                 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent))
1057                         fuse_entry_unlinked(n    1054                         fuse_entry_unlinked(newent);
1058         } else if (err == -EINTR || err == -E !! 1055         } else if (err == -EINTR) {
1059                 /* If request was interrupted    1056                 /* If request was interrupted, DEITY only knows if the
1060                    rename actually took place    1057                    rename actually took place.  If the invalidation
1061                    fails (e.g. some process h    1058                    fails (e.g. some process has CWD under the renamed
1062                    directory), then there can    1059                    directory), then there can be inconsistency between
1063                    the dcache and the real fi    1060                    the dcache and the real filesystem.  Tough luck. */
1064                 fuse_invalidate_entry(oldent)    1061                 fuse_invalidate_entry(oldent);
1065                 if (d_really_is_positive(newe    1062                 if (d_really_is_positive(newent))
1066                         fuse_invalidate_entry    1063                         fuse_invalidate_entry(newent);
1067         }                                        1064         }
1068                                                  1065 
1069         return err;                              1066         return err;
1070 }                                                1067 }
1071                                                  1068 
1072 static int fuse_rename2(struct mnt_idmap *idm    1069 static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir,
1073                         struct dentry *oldent    1070                         struct dentry *oldent, struct inode *newdir,
1074                         struct dentry *newent    1071                         struct dentry *newent, unsigned int flags)
1075 {                                                1072 {
1076         struct fuse_conn *fc = get_fuse_conn(    1073         struct fuse_conn *fc = get_fuse_conn(olddir);
1077         int err;                                 1074         int err;
1078                                                  1075 
1079         if (fuse_is_bad(olddir))                 1076         if (fuse_is_bad(olddir))
1080                 return -EIO;                     1077                 return -EIO;
1081                                                  1078 
1082         if (flags & ~(RENAME_NOREPLACE | RENA    1079         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1083                 return -EINVAL;                  1080                 return -EINVAL;
1084                                                  1081 
1085         if (flags) {                             1082         if (flags) {
1086                 if (fc->no_rename2 || fc->min    1083                 if (fc->no_rename2 || fc->minor < 23)
1087                         return -EINVAL;          1084                         return -EINVAL;
1088                                                  1085 
1089                 err = fuse_rename_common(oldd    1086                 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
1090                                          FUSE    1087                                          FUSE_RENAME2,
1091                                          size    1088                                          sizeof(struct fuse_rename2_in));
1092                 if (err == -ENOSYS) {            1089                 if (err == -ENOSYS) {
1093                         fc->no_rename2 = 1;      1090                         fc->no_rename2 = 1;
1094                         err = -EINVAL;           1091                         err = -EINVAL;
1095                 }                                1092                 }
1096         } else {                                 1093         } else {
1097                 err = fuse_rename_common(oldd    1094                 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
1098                                          FUSE    1095                                          FUSE_RENAME,
1099                                          size    1096                                          sizeof(struct fuse_rename_in));
1100         }                                        1097         }
1101                                                  1098 
1102         return err;                              1099         return err;
1103 }                                                1100 }
1104                                                  1101 
1105 static int fuse_link(struct dentry *entry, st    1102 static int fuse_link(struct dentry *entry, struct inode *newdir,
1106                      struct dentry *newent)      1103                      struct dentry *newent)
1107 {                                                1104 {
1108         int err;                                 1105         int err;
1109         struct fuse_link_in inarg;               1106         struct fuse_link_in inarg;
1110         struct inode *inode = d_inode(entry);    1107         struct inode *inode = d_inode(entry);
1111         struct fuse_mount *fm = get_fuse_moun    1108         struct fuse_mount *fm = get_fuse_mount(inode);
1112         FUSE_ARGS(args);                         1109         FUSE_ARGS(args);
1113                                                  1110 
1114         memset(&inarg, 0, sizeof(inarg));        1111         memset(&inarg, 0, sizeof(inarg));
1115         inarg.oldnodeid = get_node_id(inode);    1112         inarg.oldnodeid = get_node_id(inode);
1116         args.opcode = FUSE_LINK;                 1113         args.opcode = FUSE_LINK;
1117         args.in_numargs = 2;                     1114         args.in_numargs = 2;
1118         args.in_args[0].size = sizeof(inarg);    1115         args.in_args[0].size = sizeof(inarg);
1119         args.in_args[0].value = &inarg;          1116         args.in_args[0].value = &inarg;
1120         args.in_args[1].size = newent->d_name    1117         args.in_args[1].size = newent->d_name.len + 1;
1121         args.in_args[1].value = newent->d_nam    1118         args.in_args[1].value = newent->d_name.name;
1122         err = create_new_entry(fm, &args, new    1119         err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
1123         if (!err)                                1120         if (!err)
1124                 fuse_update_ctime_in_cache(in    1121                 fuse_update_ctime_in_cache(inode);
1125         else if (err == -EINTR)                  1122         else if (err == -EINTR)
1126                 fuse_invalidate_attr(inode);     1123                 fuse_invalidate_attr(inode);
1127                                                  1124 
1128         return err;                              1125         return err;
1129 }                                                1126 }
1130                                                  1127 
1131 static void fuse_fillattr(struct inode *inode    1128 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1132                           struct kstat *stat)    1129                           struct kstat *stat)
1133 {                                                1130 {
1134         unsigned int blkbits;                    1131         unsigned int blkbits;
1135         struct fuse_conn *fc = get_fuse_conn(    1132         struct fuse_conn *fc = get_fuse_conn(inode);
1136                                                  1133 
1137         stat->dev = inode->i_sb->s_dev;          1134         stat->dev = inode->i_sb->s_dev;
1138         stat->ino = attr->ino;                   1135         stat->ino = attr->ino;
1139         stat->mode = (inode->i_mode & S_IFMT)    1136         stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1140         stat->nlink = attr->nlink;               1137         stat->nlink = attr->nlink;
1141         stat->uid = make_kuid(fc->user_ns, at    1138         stat->uid = make_kuid(fc->user_ns, attr->uid);
1142         stat->gid = make_kgid(fc->user_ns, at    1139         stat->gid = make_kgid(fc->user_ns, attr->gid);
1143         stat->rdev = inode->i_rdev;              1140         stat->rdev = inode->i_rdev;
1144         stat->atime.tv_sec = attr->atime;        1141         stat->atime.tv_sec = attr->atime;
1145         stat->atime.tv_nsec = attr->atimensec    1142         stat->atime.tv_nsec = attr->atimensec;
1146         stat->mtime.tv_sec = attr->mtime;        1143         stat->mtime.tv_sec = attr->mtime;
1147         stat->mtime.tv_nsec = attr->mtimensec    1144         stat->mtime.tv_nsec = attr->mtimensec;
1148         stat->ctime.tv_sec = attr->ctime;        1145         stat->ctime.tv_sec = attr->ctime;
1149         stat->ctime.tv_nsec = attr->ctimensec    1146         stat->ctime.tv_nsec = attr->ctimensec;
1150         stat->size = attr->size;                 1147         stat->size = attr->size;
1151         stat->blocks = attr->blocks;             1148         stat->blocks = attr->blocks;
1152                                                  1149 
1153         if (attr->blksize != 0)                  1150         if (attr->blksize != 0)
1154                 blkbits = ilog2(attr->blksize    1151                 blkbits = ilog2(attr->blksize);
1155         else                                     1152         else
1156                 blkbits = inode->i_sb->s_bloc    1153                 blkbits = inode->i_sb->s_blocksize_bits;
1157                                                  1154 
1158         stat->blksize = 1 << blkbits;            1155         stat->blksize = 1 << blkbits;
1159 }                                                1156 }
1160                                                  1157 
1161 static void fuse_statx_to_attr(struct fuse_st << 
1162 {                                             << 
1163         memset(attr, 0, sizeof(*attr));       << 
1164         attr->ino = sx->ino;                  << 
1165         attr->size = sx->size;                << 
1166         attr->blocks = sx->blocks;            << 
1167         attr->atime = sx->atime.tv_sec;       << 
1168         attr->mtime = sx->mtime.tv_sec;       << 
1169         attr->ctime = sx->ctime.tv_sec;       << 
1170         attr->atimensec = sx->atime.tv_nsec;  << 
1171         attr->mtimensec = sx->mtime.tv_nsec;  << 
1172         attr->ctimensec = sx->ctime.tv_nsec;  << 
1173         attr->mode = sx->mode;                << 
1174         attr->nlink = sx->nlink;              << 
1175         attr->uid = sx->uid;                  << 
1176         attr->gid = sx->gid;                  << 
1177         attr->rdev = new_encode_dev(MKDEV(sx- << 
1178         attr->blksize = sx->blksize;          << 
1179 }                                             << 
1180                                               << 
1181 static int fuse_do_statx(struct inode *inode, << 
1182                          struct kstat *stat)  << 
1183 {                                             << 
1184         int err;                              << 
1185         struct fuse_attr attr;                << 
1186         struct fuse_statx *sx;                << 
1187         struct fuse_statx_in inarg;           << 
1188         struct fuse_statx_out outarg;         << 
1189         struct fuse_mount *fm = get_fuse_moun << 
1190         u64 attr_version = fuse_get_attr_vers << 
1191         FUSE_ARGS(args);                      << 
1192                                               << 
1193         memset(&inarg, 0, sizeof(inarg));     << 
1194         memset(&outarg, 0, sizeof(outarg));   << 
1195         /* Directories have separate file-han << 
1196         if (file && S_ISREG(inode->i_mode)) { << 
1197                 struct fuse_file *ff = file-> << 
1198                                               << 
1199                 inarg.getattr_flags |= FUSE_G << 
1200                 inarg.fh = ff->fh;            << 
1201         }                                     << 
1202         /* For now leave sync hints as the de << 
1203         inarg.sx_flags = 0;                   << 
1204         inarg.sx_mask = STATX_BASIC_STATS | S << 
1205         args.opcode = FUSE_STATX;             << 
1206         args.nodeid = get_node_id(inode);     << 
1207         args.in_numargs = 1;                  << 
1208         args.in_args[0].size = sizeof(inarg); << 
1209         args.in_args[0].value = &inarg;       << 
1210         args.out_numargs = 1;                 << 
1211         args.out_args[0].size = sizeof(outarg << 
1212         args.out_args[0].value = &outarg;     << 
1213         err = fuse_simple_request(fm, &args); << 
1214         if (err)                              << 
1215                 return err;                   << 
1216                                               << 
1217         sx = &outarg.stat;                    << 
1218         if (((sx->mask & STATX_SIZE) && !fuse << 
1219             ((sx->mask & STATX_TYPE) && (!fus << 
1220                                          inod << 
1221                 fuse_make_bad(inode);         << 
1222                 return -EIO;                  << 
1223         }                                     << 
1224                                               << 
1225         fuse_statx_to_attr(&outarg.stat, &att << 
1226         if ((sx->mask & STATX_BASIC_STATS) == << 
1227                 fuse_change_attributes(inode, << 
1228                                        ATTR_T << 
1229         }                                     << 
1230                                               << 
1231         if (stat) {                           << 
1232                 stat->result_mask = sx->mask  << 
1233                 stat->btime.tv_sec = sx->btim << 
1234                 stat->btime.tv_nsec = min_t(u << 
1235                 fuse_fillattr(inode, &attr, s << 
1236                 stat->result_mask |= STATX_TY << 
1237         }                                     << 
1238                                               << 
1239         return 0;                             << 
1240 }                                             << 
1241                                               << 
1242 static int fuse_do_getattr(struct inode *inod    1158 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
1243                            struct file *file)    1159                            struct file *file)
1244 {                                                1160 {
1245         int err;                                 1161         int err;
1246         struct fuse_getattr_in inarg;            1162         struct fuse_getattr_in inarg;
1247         struct fuse_attr_out outarg;             1163         struct fuse_attr_out outarg;
1248         struct fuse_mount *fm = get_fuse_moun    1164         struct fuse_mount *fm = get_fuse_mount(inode);
1249         FUSE_ARGS(args);                         1165         FUSE_ARGS(args);
1250         u64 attr_version;                        1166         u64 attr_version;
1251                                                  1167 
1252         attr_version = fuse_get_attr_version(    1168         attr_version = fuse_get_attr_version(fm->fc);
1253                                                  1169 
1254         memset(&inarg, 0, sizeof(inarg));        1170         memset(&inarg, 0, sizeof(inarg));
1255         memset(&outarg, 0, sizeof(outarg));      1171         memset(&outarg, 0, sizeof(outarg));
1256         /* Directories have separate file-han    1172         /* Directories have separate file-handle space */
1257         if (file && S_ISREG(inode->i_mode)) {    1173         if (file && S_ISREG(inode->i_mode)) {
1258                 struct fuse_file *ff = file->    1174                 struct fuse_file *ff = file->private_data;
1259                                                  1175 
1260                 inarg.getattr_flags |= FUSE_G    1176                 inarg.getattr_flags |= FUSE_GETATTR_FH;
1261                 inarg.fh = ff->fh;               1177                 inarg.fh = ff->fh;
1262         }                                        1178         }
1263         args.opcode = FUSE_GETATTR;              1179         args.opcode = FUSE_GETATTR;
1264         args.nodeid = get_node_id(inode);        1180         args.nodeid = get_node_id(inode);
1265         args.in_numargs = 1;                     1181         args.in_numargs = 1;
1266         args.in_args[0].size = sizeof(inarg);    1182         args.in_args[0].size = sizeof(inarg);
1267         args.in_args[0].value = &inarg;          1183         args.in_args[0].value = &inarg;
1268         args.out_numargs = 1;                    1184         args.out_numargs = 1;
1269         args.out_args[0].size = sizeof(outarg    1185         args.out_args[0].size = sizeof(outarg);
1270         args.out_args[0].value = &outarg;        1186         args.out_args[0].value = &outarg;
1271         err = fuse_simple_request(fm, &args);    1187         err = fuse_simple_request(fm, &args);
1272         if (!err) {                              1188         if (!err) {
1273                 if (fuse_invalid_attr(&outarg    1189                 if (fuse_invalid_attr(&outarg.attr) ||
1274                     inode_wrong_type(inode, o    1190                     inode_wrong_type(inode, outarg.attr.mode)) {
1275                         fuse_make_bad(inode);    1191                         fuse_make_bad(inode);
1276                         err = -EIO;              1192                         err = -EIO;
1277                 } else {                         1193                 } else {
1278                         fuse_change_attribute !! 1194                         fuse_change_attributes(inode, &outarg.attr,
1279                                               !! 1195                                                attr_timeout(&outarg),
1280                                                  1196                                                attr_version);
1281                         if (stat)                1197                         if (stat)
1282                                 fuse_fillattr    1198                                 fuse_fillattr(inode, &outarg.attr, stat);
1283                 }                                1199                 }
1284         }                                        1200         }
1285         return err;                              1201         return err;
1286 }                                                1202 }
1287                                                  1203 
1288 static int fuse_update_get_attr(struct inode     1204 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1289                                 struct kstat     1205                                 struct kstat *stat, u32 request_mask,
1290                                 unsigned int     1206                                 unsigned int flags)
1291 {                                                1207 {
1292         struct fuse_inode *fi = get_fuse_inod    1208         struct fuse_inode *fi = get_fuse_inode(inode);
1293         struct fuse_conn *fc = get_fuse_conn( << 
1294         int err = 0;                             1209         int err = 0;
1295         bool sync;                               1210         bool sync;
1296         u32 inval_mask = READ_ONCE(fi->inval_    1211         u32 inval_mask = READ_ONCE(fi->inval_mask);
1297         u32 cache_mask = fuse_get_cache_mask(    1212         u32 cache_mask = fuse_get_cache_mask(inode);
1298                                                  1213 
1299                                               !! 1214         if (flags & AT_STATX_FORCE_SYNC)
1300         /* FUSE only supports basic stats and << 
1301         request_mask &= STATX_BASIC_STATS | S << 
1302 retry:                                        << 
1303         if (fc->no_statx)                     << 
1304                 request_mask &= STATX_BASIC_S << 
1305                                               << 
1306         if (!request_mask)                    << 
1307                 sync = false;                 << 
1308         else if (flags & AT_STATX_FORCE_SYNC) << 
1309                 sync = true;                     1215                 sync = true;
1310         else if (flags & AT_STATX_DONT_SYNC)     1216         else if (flags & AT_STATX_DONT_SYNC)
1311                 sync = false;                    1217                 sync = false;
1312         else if (request_mask & inval_mask &     1218         else if (request_mask & inval_mask & ~cache_mask)
1313                 sync = true;                     1219                 sync = true;
1314         else                                     1220         else
1315                 sync = time_before64(fi->i_ti    1221                 sync = time_before64(fi->i_time, get_jiffies_64());
1316                                                  1222 
1317         if (sync) {                              1223         if (sync) {
1318                 forget_all_cached_acls(inode)    1224                 forget_all_cached_acls(inode);
1319                 /* Try statx if BTIME is requ !! 1225                 err = fuse_do_getattr(inode, stat, file);
1320                 if (!fc->no_statx && (request << 
1321                         err = fuse_do_statx(i << 
1322                         if (err == -ENOSYS) { << 
1323                                 fc->no_statx  << 
1324                                 err = 0;      << 
1325                                 goto retry;   << 
1326                         }                     << 
1327                 } else {                      << 
1328                         err = fuse_do_getattr << 
1329                 }                             << 
1330         } else if (stat) {                       1226         } else if (stat) {
1331                 generic_fillattr(&nop_mnt_idm !! 1227                 generic_fillattr(&nop_mnt_idmap, inode, stat);
1332                 stat->mode = fi->orig_i_mode;    1228                 stat->mode = fi->orig_i_mode;
1333                 stat->ino = fi->orig_ino;        1229                 stat->ino = fi->orig_ino;
1334                 if (test_bit(FUSE_I_BTIME, &f << 
1335                         stat->btime = fi->i_b << 
1336                         stat->result_mask |=  << 
1337                 }                             << 
1338         }                                        1230         }
1339                                                  1231 
1340         return err;                              1232         return err;
1341 }                                                1233 }
1342                                                  1234 
1343 int fuse_update_attributes(struct inode *inod    1235 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask)
1344 {                                                1236 {
1345         return fuse_update_get_attr(inode, fi    1237         return fuse_update_get_attr(inode, file, NULL, mask, 0);
1346 }                                                1238 }
1347                                                  1239 
1348 int fuse_reverse_inval_entry(struct fuse_conn    1240 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1349                              u64 child_nodeid    1241                              u64 child_nodeid, struct qstr *name, u32 flags)
1350 {                                                1242 {
1351         int err = -ENOTDIR;                      1243         int err = -ENOTDIR;
1352         struct inode *parent;                    1244         struct inode *parent;
1353         struct dentry *dir;                      1245         struct dentry *dir;
1354         struct dentry *entry;                    1246         struct dentry *entry;
1355                                                  1247 
1356         parent = fuse_ilookup(fc, parent_node    1248         parent = fuse_ilookup(fc, parent_nodeid, NULL);
1357         if (!parent)                             1249         if (!parent)
1358                 return -ENOENT;                  1250                 return -ENOENT;
1359                                                  1251 
1360         inode_lock_nested(parent, I_MUTEX_PAR    1252         inode_lock_nested(parent, I_MUTEX_PARENT);
1361         if (!S_ISDIR(parent->i_mode))            1253         if (!S_ISDIR(parent->i_mode))
1362                 goto unlock;                     1254                 goto unlock;
1363                                                  1255 
1364         err = -ENOENT;                           1256         err = -ENOENT;
1365         dir = d_find_alias(parent);              1257         dir = d_find_alias(parent);
1366         if (!dir)                                1258         if (!dir)
1367                 goto unlock;                     1259                 goto unlock;
1368                                                  1260 
1369         name->hash = full_name_hash(dir, name    1261         name->hash = full_name_hash(dir, name->name, name->len);
1370         entry = d_lookup(dir, name);             1262         entry = d_lookup(dir, name);
1371         dput(dir);                               1263         dput(dir);
1372         if (!entry)                              1264         if (!entry)
1373                 goto unlock;                     1265                 goto unlock;
1374                                                  1266 
1375         fuse_dir_changed(parent);                1267         fuse_dir_changed(parent);
1376         if (!(flags & FUSE_EXPIRE_ONLY))         1268         if (!(flags & FUSE_EXPIRE_ONLY))
1377                 d_invalidate(entry);             1269                 d_invalidate(entry);
1378         fuse_invalidate_entry_cache(entry);      1270         fuse_invalidate_entry_cache(entry);
1379                                                  1271 
1380         if (child_nodeid != 0 && d_really_is_    1272         if (child_nodeid != 0 && d_really_is_positive(entry)) {
1381                 inode_lock(d_inode(entry));      1273                 inode_lock(d_inode(entry));
1382                 if (get_node_id(d_inode(entry    1274                 if (get_node_id(d_inode(entry)) != child_nodeid) {
1383                         err = -ENOENT;           1275                         err = -ENOENT;
1384                         goto badentry;           1276                         goto badentry;
1385                 }                                1277                 }
1386                 if (d_mountpoint(entry)) {       1278                 if (d_mountpoint(entry)) {
1387                         err = -EBUSY;            1279                         err = -EBUSY;
1388                         goto badentry;           1280                         goto badentry;
1389                 }                                1281                 }
1390                 if (d_is_dir(entry)) {           1282                 if (d_is_dir(entry)) {
1391                         shrink_dcache_parent(    1283                         shrink_dcache_parent(entry);
1392                         if (!simple_empty(ent    1284                         if (!simple_empty(entry)) {
1393                                 err = -ENOTEM    1285                                 err = -ENOTEMPTY;
1394                                 goto badentry    1286                                 goto badentry;
1395                         }                        1287                         }
1396                         d_inode(entry)->i_fla    1288                         d_inode(entry)->i_flags |= S_DEAD;
1397                 }                                1289                 }
1398                 dont_mount(entry);               1290                 dont_mount(entry);
1399                 clear_nlink(d_inode(entry));     1291                 clear_nlink(d_inode(entry));
1400                 err = 0;                         1292                 err = 0;
1401  badentry:                                       1293  badentry:
1402                 inode_unlock(d_inode(entry));    1294                 inode_unlock(d_inode(entry));
1403                 if (!err)                        1295                 if (!err)
1404                         d_delete(entry);         1296                         d_delete(entry);
1405         } else {                                 1297         } else {
1406                 err = 0;                         1298                 err = 0;
1407         }                                        1299         }
1408         dput(entry);                             1300         dput(entry);
1409                                                  1301 
1410  unlock:                                         1302  unlock:
1411         inode_unlock(parent);                    1303         inode_unlock(parent);
1412         iput(parent);                            1304         iput(parent);
1413         return err;                              1305         return err;
1414 }                                                1306 }
1415                                                  1307 
1416 static inline bool fuse_permissible_uidgid(st    1308 static inline bool fuse_permissible_uidgid(struct fuse_conn *fc)
1417 {                                                1309 {
1418         const struct cred *cred = current_cre    1310         const struct cred *cred = current_cred();
1419                                                  1311 
1420         return (uid_eq(cred->euid, fc->user_i    1312         return (uid_eq(cred->euid, fc->user_id) &&
1421                 uid_eq(cred->suid, fc->user_i    1313                 uid_eq(cred->suid, fc->user_id) &&
1422                 uid_eq(cred->uid,  fc->user_i    1314                 uid_eq(cred->uid,  fc->user_id) &&
1423                 gid_eq(cred->egid, fc->group_    1315                 gid_eq(cred->egid, fc->group_id) &&
1424                 gid_eq(cred->sgid, fc->group_    1316                 gid_eq(cred->sgid, fc->group_id) &&
1425                 gid_eq(cred->gid,  fc->group_    1317                 gid_eq(cred->gid,  fc->group_id));
1426 }                                                1318 }
1427                                                  1319 
1428 /*                                               1320 /*
1429  * Calling into a user-controlled filesystem     1321  * Calling into a user-controlled filesystem gives the filesystem
1430  * daemon ptrace-like capabilities over the c    1322  * daemon ptrace-like capabilities over the current process.  This
1431  * means, that the filesystem daemon is able     1323  * means, that the filesystem daemon is able to record the exact
1432  * filesystem operations performed, and can a    1324  * filesystem operations performed, and can also control the behavior
1433  * of the requester process in otherwise impo    1325  * of the requester process in otherwise impossible ways.  For example
1434  * it can delay the operation for arbitrary l    1326  * it can delay the operation for arbitrary length of time allowing
1435  * DoS against the requester.                    1327  * DoS against the requester.
1436  *                                               1328  *
1437  * For this reason only those processes can c    1329  * For this reason only those processes can call into the filesystem,
1438  * for which the owner of the mount has ptrac    1330  * for which the owner of the mount has ptrace privilege.  This
1439  * excludes processes started by other users,    1331  * excludes processes started by other users, suid or sgid processes.
1440  */                                              1332  */
1441 bool fuse_allow_current_process(struct fuse_c    1333 bool fuse_allow_current_process(struct fuse_conn *fc)
1442 {                                                1334 {
1443         bool allow;                              1335         bool allow;
1444                                                  1336 
1445         if (fc->allow_other)                     1337         if (fc->allow_other)
1446                 allow = current_in_userns(fc-    1338                 allow = current_in_userns(fc->user_ns);
1447         else                                     1339         else
1448                 allow = fuse_permissible_uidg    1340                 allow = fuse_permissible_uidgid(fc);
1449                                                  1341 
1450         if (!allow && allow_sys_admin_access     1342         if (!allow && allow_sys_admin_access && capable(CAP_SYS_ADMIN))
1451                 allow = true;                    1343                 allow = true;
1452                                                  1344 
1453         return allow;                            1345         return allow;
1454 }                                                1346 }
1455                                                  1347 
1456 static int fuse_access(struct inode *inode, i    1348 static int fuse_access(struct inode *inode, int mask)
1457 {                                                1349 {
1458         struct fuse_mount *fm = get_fuse_moun    1350         struct fuse_mount *fm = get_fuse_mount(inode);
1459         FUSE_ARGS(args);                         1351         FUSE_ARGS(args);
1460         struct fuse_access_in inarg;             1352         struct fuse_access_in inarg;
1461         int err;                                 1353         int err;
1462                                                  1354 
1463         BUG_ON(mask & MAY_NOT_BLOCK);            1355         BUG_ON(mask & MAY_NOT_BLOCK);
1464                                                  1356 
1465         if (fm->fc->no_access)                   1357         if (fm->fc->no_access)
1466                 return 0;                        1358                 return 0;
1467                                                  1359 
1468         memset(&inarg, 0, sizeof(inarg));        1360         memset(&inarg, 0, sizeof(inarg));
1469         inarg.mask = mask & (MAY_READ | MAY_W    1361         inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1470         args.opcode = FUSE_ACCESS;               1362         args.opcode = FUSE_ACCESS;
1471         args.nodeid = get_node_id(inode);        1363         args.nodeid = get_node_id(inode);
1472         args.in_numargs = 1;                     1364         args.in_numargs = 1;
1473         args.in_args[0].size = sizeof(inarg);    1365         args.in_args[0].size = sizeof(inarg);
1474         args.in_args[0].value = &inarg;          1366         args.in_args[0].value = &inarg;
1475         err = fuse_simple_request(fm, &args);    1367         err = fuse_simple_request(fm, &args);
1476         if (err == -ENOSYS) {                    1368         if (err == -ENOSYS) {
1477                 fm->fc->no_access = 1;           1369                 fm->fc->no_access = 1;
1478                 err = 0;                         1370                 err = 0;
1479         }                                        1371         }
1480         return err;                              1372         return err;
1481 }                                                1373 }
1482                                                  1374 
1483 static int fuse_perm_getattr(struct inode *in    1375 static int fuse_perm_getattr(struct inode *inode, int mask)
1484 {                                                1376 {
1485         if (mask & MAY_NOT_BLOCK)                1377         if (mask & MAY_NOT_BLOCK)
1486                 return -ECHILD;                  1378                 return -ECHILD;
1487                                                  1379 
1488         forget_all_cached_acls(inode);           1380         forget_all_cached_acls(inode);
1489         return fuse_do_getattr(inode, NULL, N    1381         return fuse_do_getattr(inode, NULL, NULL);
1490 }                                                1382 }
1491                                                  1383 
1492 /*                                               1384 /*
1493  * Check permission.  The two basic access mo    1385  * Check permission.  The two basic access models of FUSE are:
1494  *                                               1386  *
1495  * 1) Local access checking ('default_permiss    1387  * 1) Local access checking ('default_permissions' mount option) based
1496  * on file mode.  This is the plain old disk     1388  * on file mode.  This is the plain old disk filesystem permission
1497  * model.                                     !! 1389  * modell.
1498  *                                               1390  *
1499  * 2) "Remote" access checking, where server     1391  * 2) "Remote" access checking, where server is responsible for
1500  * checking permission in each inode operatio    1392  * checking permission in each inode operation.  An exception to this
1501  * is if ->permission() was invoked from sys_    1393  * is if ->permission() was invoked from sys_access() in which case an
1502  * access request is sent.  Execute permissio    1394  * access request is sent.  Execute permission is still checked
1503  * locally based on file mode.                   1395  * locally based on file mode.
1504  */                                              1396  */
1505 static int fuse_permission(struct mnt_idmap *    1397 static int fuse_permission(struct mnt_idmap *idmap,
1506                            struct inode *inod    1398                            struct inode *inode, int mask)
1507 {                                                1399 {
1508         struct fuse_conn *fc = get_fuse_conn(    1400         struct fuse_conn *fc = get_fuse_conn(inode);
1509         bool refreshed = false;                  1401         bool refreshed = false;
1510         int err = 0;                             1402         int err = 0;
1511                                                  1403 
1512         if (fuse_is_bad(inode))                  1404         if (fuse_is_bad(inode))
1513                 return -EIO;                     1405                 return -EIO;
1514                                                  1406 
1515         if (!fuse_allow_current_process(fc))     1407         if (!fuse_allow_current_process(fc))
1516                 return -EACCES;                  1408                 return -EACCES;
1517                                                  1409 
1518         /*                                       1410         /*
1519          * If attributes are needed, refresh     1411          * If attributes are needed, refresh them before proceeding
1520          */                                      1412          */
1521         if (fc->default_permissions ||           1413         if (fc->default_permissions ||
1522             ((mask & MAY_EXEC) && S_ISREG(ino    1414             ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1523                 struct fuse_inode *fi = get_f    1415                 struct fuse_inode *fi = get_fuse_inode(inode);
1524                 u32 perm_mask = STATX_MODE |     1416                 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1525                                                  1417 
1526                 if (perm_mask & READ_ONCE(fi-    1418                 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1527                     time_before64(fi->i_time,    1419                     time_before64(fi->i_time, get_jiffies_64())) {
1528                         refreshed = true;        1420                         refreshed = true;
1529                                                  1421 
1530                         err = fuse_perm_getat    1422                         err = fuse_perm_getattr(inode, mask);
1531                         if (err)                 1423                         if (err)
1532                                 return err;      1424                                 return err;
1533                 }                                1425                 }
1534         }                                        1426         }
1535                                                  1427 
1536         if (fc->default_permissions) {           1428         if (fc->default_permissions) {
1537                 err = generic_permission(&nop    1429                 err = generic_permission(&nop_mnt_idmap, inode, mask);
1538                                                  1430 
1539                 /* If permission is denied, t    1431                 /* If permission is denied, try to refresh file
1540                    attributes.  This is also     1432                    attributes.  This is also needed, because the root
1541                    node will at first have no    1433                    node will at first have no permissions */
1542                 if (err == -EACCES && !refres    1434                 if (err == -EACCES && !refreshed) {
1543                         err = fuse_perm_getat    1435                         err = fuse_perm_getattr(inode, mask);
1544                         if (!err)                1436                         if (!err)
1545                                 err = generic    1437                                 err = generic_permission(&nop_mnt_idmap,
1546                                                  1438                                                          inode, mask);
1547                 }                                1439                 }
1548                                                  1440 
1549                 /* Note: the opposite of the     1441                 /* Note: the opposite of the above test does not
1550                    exist.  So if permissions     1442                    exist.  So if permissions are revoked this won't be
1551                    noticed immediately, only     1443                    noticed immediately, only after the attribute
1552                    timeout has expired */        1444                    timeout has expired */
1553         } else if (mask & (MAY_ACCESS | MAY_C    1445         } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1554                 err = fuse_access(inode, mask    1446                 err = fuse_access(inode, mask);
1555         } else if ((mask & MAY_EXEC) && S_ISR    1447         } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1556                 if (!(inode->i_mode & S_IXUGO    1448                 if (!(inode->i_mode & S_IXUGO)) {
1557                         if (refreshed)           1449                         if (refreshed)
1558                                 return -EACCE    1450                                 return -EACCES;
1559                                                  1451 
1560                         err = fuse_perm_getat    1452                         err = fuse_perm_getattr(inode, mask);
1561                         if (!err && !(inode->    1453                         if (!err && !(inode->i_mode & S_IXUGO))
1562                                 return -EACCE    1454                                 return -EACCES;
1563                 }                                1455                 }
1564         }                                        1456         }
1565         return err;                              1457         return err;
1566 }                                                1458 }
1567                                                  1459 
1568 static int fuse_readlink_page(struct inode *i    1460 static int fuse_readlink_page(struct inode *inode, struct page *page)
1569 {                                                1461 {
1570         struct fuse_mount *fm = get_fuse_moun    1462         struct fuse_mount *fm = get_fuse_mount(inode);
1571         struct fuse_page_desc desc = { .lengt    1463         struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1572         struct fuse_args_pages ap = {            1464         struct fuse_args_pages ap = {
1573                 .num_pages = 1,                  1465                 .num_pages = 1,
1574                 .pages = &page,                  1466                 .pages = &page,
1575                 .descs = &desc,                  1467                 .descs = &desc,
1576         };                                       1468         };
1577         char *link;                              1469         char *link;
1578         ssize_t res;                             1470         ssize_t res;
1579                                                  1471 
1580         ap.args.opcode = FUSE_READLINK;          1472         ap.args.opcode = FUSE_READLINK;
1581         ap.args.nodeid = get_node_id(inode);     1473         ap.args.nodeid = get_node_id(inode);
1582         ap.args.out_pages = true;                1474         ap.args.out_pages = true;
1583         ap.args.out_argvar = true;               1475         ap.args.out_argvar = true;
1584         ap.args.page_zeroing = true;             1476         ap.args.page_zeroing = true;
1585         ap.args.out_numargs = 1;                 1477         ap.args.out_numargs = 1;
1586         ap.args.out_args[0].size = desc.lengt    1478         ap.args.out_args[0].size = desc.length;
1587         res = fuse_simple_request(fm, &ap.arg    1479         res = fuse_simple_request(fm, &ap.args);
1588                                                  1480 
1589         fuse_invalidate_atime(inode);            1481         fuse_invalidate_atime(inode);
1590                                                  1482 
1591         if (res < 0)                             1483         if (res < 0)
1592                 return res;                      1484                 return res;
1593                                                  1485 
1594         if (WARN_ON(res >= PAGE_SIZE))           1486         if (WARN_ON(res >= PAGE_SIZE))
1595                 return -EIO;                     1487                 return -EIO;
1596                                                  1488 
1597         link = page_address(page);               1489         link = page_address(page);
1598         link[res] = '\0';                        1490         link[res] = '\0';
1599                                                  1491 
1600         return 0;                                1492         return 0;
1601 }                                                1493 }
1602                                                  1494 
1603 static const char *fuse_get_link(struct dentr    1495 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1604                                  struct delay    1496                                  struct delayed_call *callback)
1605 {                                                1497 {
1606         struct fuse_conn *fc = get_fuse_conn(    1498         struct fuse_conn *fc = get_fuse_conn(inode);
1607         struct page *page;                       1499         struct page *page;
1608         int err;                                 1500         int err;
1609                                                  1501 
1610         err = -EIO;                              1502         err = -EIO;
1611         if (fuse_is_bad(inode))                  1503         if (fuse_is_bad(inode))
1612                 goto out_err;                    1504                 goto out_err;
1613                                                  1505 
1614         if (fc->cache_symlinks)                  1506         if (fc->cache_symlinks)
1615                 return page_get_link(dentry,     1507                 return page_get_link(dentry, inode, callback);
1616                                                  1508 
1617         err = -ECHILD;                           1509         err = -ECHILD;
1618         if (!dentry)                             1510         if (!dentry)
1619                 goto out_err;                    1511                 goto out_err;
1620                                                  1512 
1621         page = alloc_page(GFP_KERNEL);           1513         page = alloc_page(GFP_KERNEL);
1622         err = -ENOMEM;                           1514         err = -ENOMEM;
1623         if (!page)                               1515         if (!page)
1624                 goto out_err;                    1516                 goto out_err;
1625                                                  1517 
1626         err = fuse_readlink_page(inode, page)    1518         err = fuse_readlink_page(inode, page);
1627         if (err) {                               1519         if (err) {
1628                 __free_page(page);               1520                 __free_page(page);
1629                 goto out_err;                    1521                 goto out_err;
1630         }                                        1522         }
1631                                                  1523 
1632         set_delayed_call(callback, page_put_l    1524         set_delayed_call(callback, page_put_link, page);
1633                                                  1525 
1634         return page_address(page);               1526         return page_address(page);
1635                                                  1527 
1636 out_err:                                         1528 out_err:
1637         return ERR_PTR(err);                     1529         return ERR_PTR(err);
1638 }                                                1530 }
1639                                                  1531 
1640 static int fuse_dir_open(struct inode *inode,    1532 static int fuse_dir_open(struct inode *inode, struct file *file)
1641 {                                                1533 {
1642         struct fuse_mount *fm = get_fuse_moun !! 1534         return fuse_open_common(inode, file, true);
1643         int err;                              << 
1644                                               << 
1645         if (fuse_is_bad(inode))               << 
1646                 return -EIO;                  << 
1647                                               << 
1648         err = generic_file_open(inode, file); << 
1649         if (err)                              << 
1650                 return err;                   << 
1651                                               << 
1652         err = fuse_do_open(fm, get_node_id(in << 
1653         if (!err) {                           << 
1654                 struct fuse_file *ff = file-> << 
1655                                               << 
1656                 /*                            << 
1657                  * Keep handling FOPEN_STREAM << 
1658                  * directories for backward c << 
1659                  * to be useful.              << 
1660                  */                           << 
1661                 if (ff->open_flags & (FOPEN_S << 
1662                         nonseekable_open(inod << 
1663         }                                     << 
1664                                               << 
1665         return err;                           << 
1666 }                                                1535 }
1667                                                  1536 
1668 static int fuse_dir_release(struct inode *ino    1537 static int fuse_dir_release(struct inode *inode, struct file *file)
1669 {                                                1538 {
1670         fuse_release_common(file, true);         1539         fuse_release_common(file, true);
1671                                                  1540 
1672         return 0;                                1541         return 0;
1673 }                                                1542 }
1674                                                  1543 
1675 static int fuse_dir_fsync(struct file *file,     1544 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1676                           int datasync)          1545                           int datasync)
1677 {                                                1546 {
1678         struct inode *inode = file->f_mapping    1547         struct inode *inode = file->f_mapping->host;
1679         struct fuse_conn *fc = get_fuse_conn(    1548         struct fuse_conn *fc = get_fuse_conn(inode);
1680         int err;                                 1549         int err;
1681                                                  1550 
1682         if (fuse_is_bad(inode))                  1551         if (fuse_is_bad(inode))
1683                 return -EIO;                     1552                 return -EIO;
1684                                                  1553 
1685         if (fc->no_fsyncdir)                     1554         if (fc->no_fsyncdir)
1686                 return 0;                        1555                 return 0;
1687                                                  1556 
1688         inode_lock(inode);                       1557         inode_lock(inode);
1689         err = fuse_fsync_common(file, start,     1558         err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1690         if (err == -ENOSYS) {                    1559         if (err == -ENOSYS) {
1691                 fc->no_fsyncdir = 1;             1560                 fc->no_fsyncdir = 1;
1692                 err = 0;                         1561                 err = 0;
1693         }                                        1562         }
1694         inode_unlock(inode);                     1563         inode_unlock(inode);
1695                                                  1564 
1696         return err;                              1565         return err;
1697 }                                                1566 }
1698                                                  1567 
1699 static long fuse_dir_ioctl(struct file *file,    1568 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1700                             unsigned long arg    1569                             unsigned long arg)
1701 {                                                1570 {
1702         struct fuse_conn *fc = get_fuse_conn(    1571         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1703                                                  1572 
1704         /* FUSE_IOCTL_DIR only supported for     1573         /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1705         if (fc->minor < 18)                      1574         if (fc->minor < 18)
1706                 return -ENOTTY;                  1575                 return -ENOTTY;
1707                                                  1576 
1708         return fuse_ioctl_common(file, cmd, a    1577         return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1709 }                                                1578 }
1710                                                  1579 
1711 static long fuse_dir_compat_ioctl(struct file    1580 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1712                                    unsigned l    1581                                    unsigned long arg)
1713 {                                                1582 {
1714         struct fuse_conn *fc = get_fuse_conn(    1583         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1715                                                  1584 
1716         if (fc->minor < 18)                      1585         if (fc->minor < 18)
1717                 return -ENOTTY;                  1586                 return -ENOTTY;
1718                                                  1587 
1719         return fuse_ioctl_common(file, cmd, a    1588         return fuse_ioctl_common(file, cmd, arg,
1720                                  FUSE_IOCTL_C    1589                                  FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1721 }                                                1590 }
1722                                                  1591 
1723 static bool update_mtime(unsigned ivalid, boo    1592 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1724 {                                                1593 {
1725         /* Always update if mtime is explicit    1594         /* Always update if mtime is explicitly set  */
1726         if (ivalid & ATTR_MTIME_SET)             1595         if (ivalid & ATTR_MTIME_SET)
1727                 return true;                     1596                 return true;
1728                                                  1597 
1729         /* Or if kernel i_mtime is the offici    1598         /* Or if kernel i_mtime is the official one */
1730         if (trust_local_mtime)                   1599         if (trust_local_mtime)
1731                 return true;                     1600                 return true;
1732                                                  1601 
1733         /* If it's an open(O_TRUNC) or an ftr    1602         /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1734         if ((ivalid & ATTR_SIZE) && (ivalid &    1603         if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1735                 return false;                    1604                 return false;
1736                                                  1605 
1737         /* In all other cases update */          1606         /* In all other cases update */
1738         return true;                             1607         return true;
1739 }                                                1608 }
1740                                                  1609 
1741 static void iattr_to_fattr(struct fuse_conn *    1610 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1742                            struct fuse_setatt    1611                            struct fuse_setattr_in *arg, bool trust_local_cmtime)
1743 {                                                1612 {
1744         unsigned ivalid = iattr->ia_valid;       1613         unsigned ivalid = iattr->ia_valid;
1745                                                  1614 
1746         if (ivalid & ATTR_MODE)                  1615         if (ivalid & ATTR_MODE)
1747                 arg->valid |= FATTR_MODE,   a    1616                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1748         if (ivalid & ATTR_UID)                   1617         if (ivalid & ATTR_UID)
1749                 arg->valid |= FATTR_UID,    a    1618                 arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1750         if (ivalid & ATTR_GID)                   1619         if (ivalid & ATTR_GID)
1751                 arg->valid |= FATTR_GID,    a    1620                 arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1752         if (ivalid & ATTR_SIZE)                  1621         if (ivalid & ATTR_SIZE)
1753                 arg->valid |= FATTR_SIZE,   a    1622                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1754         if (ivalid & ATTR_ATIME) {               1623         if (ivalid & ATTR_ATIME) {
1755                 arg->valid |= FATTR_ATIME;       1624                 arg->valid |= FATTR_ATIME;
1756                 arg->atime = iattr->ia_atime.    1625                 arg->atime = iattr->ia_atime.tv_sec;
1757                 arg->atimensec = iattr->ia_at    1626                 arg->atimensec = iattr->ia_atime.tv_nsec;
1758                 if (!(ivalid & ATTR_ATIME_SET    1627                 if (!(ivalid & ATTR_ATIME_SET))
1759                         arg->valid |= FATTR_A    1628                         arg->valid |= FATTR_ATIME_NOW;
1760         }                                        1629         }
1761         if ((ivalid & ATTR_MTIME) && update_m    1630         if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1762                 arg->valid |= FATTR_MTIME;       1631                 arg->valid |= FATTR_MTIME;
1763                 arg->mtime = iattr->ia_mtime.    1632                 arg->mtime = iattr->ia_mtime.tv_sec;
1764                 arg->mtimensec = iattr->ia_mt    1633                 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1765                 if (!(ivalid & ATTR_MTIME_SET    1634                 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1766                         arg->valid |= FATTR_M    1635                         arg->valid |= FATTR_MTIME_NOW;
1767         }                                        1636         }
1768         if ((ivalid & ATTR_CTIME) && trust_lo    1637         if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1769                 arg->valid |= FATTR_CTIME;       1638                 arg->valid |= FATTR_CTIME;
1770                 arg->ctime = iattr->ia_ctime.    1639                 arg->ctime = iattr->ia_ctime.tv_sec;
1771                 arg->ctimensec = iattr->ia_ct    1640                 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1772         }                                        1641         }
1773 }                                                1642 }
1774                                                  1643 
1775 /*                                               1644 /*
1776  * Prevent concurrent writepages on inode        1645  * Prevent concurrent writepages on inode
1777  *                                               1646  *
1778  * This is done by adding a negative bias to     1647  * This is done by adding a negative bias to the inode write counter
1779  * and waiting for all pending writes to fini    1648  * and waiting for all pending writes to finish.
1780  */                                              1649  */
1781 void fuse_set_nowrite(struct inode *inode)       1650 void fuse_set_nowrite(struct inode *inode)
1782 {                                                1651 {
1783         struct fuse_inode *fi = get_fuse_inod    1652         struct fuse_inode *fi = get_fuse_inode(inode);
1784                                                  1653 
1785         BUG_ON(!inode_is_locked(inode));         1654         BUG_ON(!inode_is_locked(inode));
1786                                                  1655 
1787         spin_lock(&fi->lock);                    1656         spin_lock(&fi->lock);
1788         BUG_ON(fi->writectr < 0);                1657         BUG_ON(fi->writectr < 0);
1789         fi->writectr += FUSE_NOWRITE;            1658         fi->writectr += FUSE_NOWRITE;
1790         spin_unlock(&fi->lock);                  1659         spin_unlock(&fi->lock);
1791         wait_event(fi->page_waitq, fi->writec    1660         wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1792 }                                                1661 }
1793                                                  1662 
1794 /*                                               1663 /*
1795  * Allow writepages on inode                     1664  * Allow writepages on inode
1796  *                                               1665  *
1797  * Remove the bias from the writecounter and     1666  * Remove the bias from the writecounter and send any queued
1798  * writepages.                                   1667  * writepages.
1799  */                                              1668  */
1800 static void __fuse_release_nowrite(struct ino    1669 static void __fuse_release_nowrite(struct inode *inode)
1801 {                                                1670 {
1802         struct fuse_inode *fi = get_fuse_inod    1671         struct fuse_inode *fi = get_fuse_inode(inode);
1803                                                  1672 
1804         BUG_ON(fi->writectr != FUSE_NOWRITE);    1673         BUG_ON(fi->writectr != FUSE_NOWRITE);
1805         fi->writectr = 0;                        1674         fi->writectr = 0;
1806         fuse_flush_writepages(inode);            1675         fuse_flush_writepages(inode);
1807 }                                                1676 }
1808                                                  1677 
1809 void fuse_release_nowrite(struct inode *inode    1678 void fuse_release_nowrite(struct inode *inode)
1810 {                                                1679 {
1811         struct fuse_inode *fi = get_fuse_inod    1680         struct fuse_inode *fi = get_fuse_inode(inode);
1812                                                  1681 
1813         spin_lock(&fi->lock);                    1682         spin_lock(&fi->lock);
1814         __fuse_release_nowrite(inode);           1683         __fuse_release_nowrite(inode);
1815         spin_unlock(&fi->lock);                  1684         spin_unlock(&fi->lock);
1816 }                                                1685 }
1817                                                  1686 
1818 static void fuse_setattr_fill(struct fuse_con    1687 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1819                               struct inode *i    1688                               struct inode *inode,
1820                               struct fuse_set    1689                               struct fuse_setattr_in *inarg_p,
1821                               struct fuse_att    1690                               struct fuse_attr_out *outarg_p)
1822 {                                                1691 {
1823         args->opcode = FUSE_SETATTR;             1692         args->opcode = FUSE_SETATTR;
1824         args->nodeid = get_node_id(inode);       1693         args->nodeid = get_node_id(inode);
1825         args->in_numargs = 1;                    1694         args->in_numargs = 1;
1826         args->in_args[0].size = sizeof(*inarg    1695         args->in_args[0].size = sizeof(*inarg_p);
1827         args->in_args[0].value = inarg_p;        1696         args->in_args[0].value = inarg_p;
1828         args->out_numargs = 1;                   1697         args->out_numargs = 1;
1829         args->out_args[0].size = sizeof(*outa    1698         args->out_args[0].size = sizeof(*outarg_p);
1830         args->out_args[0].value = outarg_p;      1699         args->out_args[0].value = outarg_p;
1831 }                                                1700 }
1832                                                  1701 
1833 /*                                               1702 /*
1834  * Flush inode->i_mtime to the server            1703  * Flush inode->i_mtime to the server
1835  */                                              1704  */
1836 int fuse_flush_times(struct inode *inode, str    1705 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1837 {                                                1706 {
1838         struct fuse_mount *fm = get_fuse_moun    1707         struct fuse_mount *fm = get_fuse_mount(inode);
1839         FUSE_ARGS(args);                         1708         FUSE_ARGS(args);
1840         struct fuse_setattr_in inarg;            1709         struct fuse_setattr_in inarg;
1841         struct fuse_attr_out outarg;             1710         struct fuse_attr_out outarg;
1842                                                  1711 
1843         memset(&inarg, 0, sizeof(inarg));        1712         memset(&inarg, 0, sizeof(inarg));
1844         memset(&outarg, 0, sizeof(outarg));      1713         memset(&outarg, 0, sizeof(outarg));
1845                                                  1714 
1846         inarg.valid = FATTR_MTIME;               1715         inarg.valid = FATTR_MTIME;
1847         inarg.mtime = inode_get_mtime_sec(ino !! 1716         inarg.mtime = inode->i_mtime.tv_sec;
1848         inarg.mtimensec = inode_get_mtime_nse !! 1717         inarg.mtimensec = inode->i_mtime.tv_nsec;
1849         if (fm->fc->minor >= 23) {               1718         if (fm->fc->minor >= 23) {
1850                 inarg.valid |= FATTR_CTIME;      1719                 inarg.valid |= FATTR_CTIME;
1851                 inarg.ctime = inode_get_ctime !! 1720                 inarg.ctime = inode->i_ctime.tv_sec;
1852                 inarg.ctimensec = inode_get_c !! 1721                 inarg.ctimensec = inode->i_ctime.tv_nsec;
1853         }                                        1722         }
1854         if (ff) {                                1723         if (ff) {
1855                 inarg.valid |= FATTR_FH;         1724                 inarg.valid |= FATTR_FH;
1856                 inarg.fh = ff->fh;               1725                 inarg.fh = ff->fh;
1857         }                                        1726         }
1858         fuse_setattr_fill(fm->fc, &args, inod    1727         fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1859                                                  1728 
1860         return fuse_simple_request(fm, &args)    1729         return fuse_simple_request(fm, &args);
1861 }                                                1730 }
1862                                                  1731 
1863 /*                                               1732 /*
1864  * Set attributes, and at the same time refre    1733  * Set attributes, and at the same time refresh them.
1865  *                                               1734  *
1866  * Truncation is slightly complicated, becaus    1735  * Truncation is slightly complicated, because the 'truncate' request
1867  * may fail, in which case we don't want to t    1736  * may fail, in which case we don't want to touch the mapping.
1868  * vmtruncate() doesn't allow for this case,     1737  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1869  * and the actual truncation by hand.            1738  * and the actual truncation by hand.
1870  */                                              1739  */
1871 int fuse_do_setattr(struct dentry *dentry, st    1740 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1872                     struct file *file)           1741                     struct file *file)
1873 {                                                1742 {
1874         struct inode *inode = d_inode(dentry)    1743         struct inode *inode = d_inode(dentry);
1875         struct fuse_mount *fm = get_fuse_moun    1744         struct fuse_mount *fm = get_fuse_mount(inode);
1876         struct fuse_conn *fc = fm->fc;           1745         struct fuse_conn *fc = fm->fc;
1877         struct fuse_inode *fi = get_fuse_inod    1746         struct fuse_inode *fi = get_fuse_inode(inode);
1878         struct address_space *mapping = inode    1747         struct address_space *mapping = inode->i_mapping;
1879         FUSE_ARGS(args);                         1748         FUSE_ARGS(args);
1880         struct fuse_setattr_in inarg;            1749         struct fuse_setattr_in inarg;
1881         struct fuse_attr_out outarg;             1750         struct fuse_attr_out outarg;
1882         bool is_truncate = false;                1751         bool is_truncate = false;
1883         bool is_wb = fc->writeback_cache && S    1752         bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode);
1884         loff_t oldsize;                          1753         loff_t oldsize;
1885         int err;                                 1754         int err;
1886         bool trust_local_cmtime = is_wb;         1755         bool trust_local_cmtime = is_wb;
1887         bool fault_blocked = false;              1756         bool fault_blocked = false;
1888                                                  1757 
1889         if (!fc->default_permissions)            1758         if (!fc->default_permissions)
1890                 attr->ia_valid |= ATTR_FORCE;    1759                 attr->ia_valid |= ATTR_FORCE;
1891                                                  1760 
1892         err = setattr_prepare(&nop_mnt_idmap,    1761         err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
1893         if (err)                                 1762         if (err)
1894                 return err;                      1763                 return err;
1895                                                  1764 
1896         if (attr->ia_valid & ATTR_SIZE) {        1765         if (attr->ia_valid & ATTR_SIZE) {
1897                 if (WARN_ON(!S_ISREG(inode->i    1766                 if (WARN_ON(!S_ISREG(inode->i_mode)))
1898                         return -EIO;             1767                         return -EIO;
1899                 is_truncate = true;              1768                 is_truncate = true;
1900         }                                        1769         }
1901                                                  1770 
1902         if (FUSE_IS_DAX(inode) && is_truncate    1771         if (FUSE_IS_DAX(inode) && is_truncate) {
1903                 filemap_invalidate_lock(mappi    1772                 filemap_invalidate_lock(mapping);
1904                 fault_blocked = true;            1773                 fault_blocked = true;
1905                 err = fuse_dax_break_layouts(    1774                 err = fuse_dax_break_layouts(inode, 0, 0);
1906                 if (err) {                       1775                 if (err) {
1907                         filemap_invalidate_un    1776                         filemap_invalidate_unlock(mapping);
1908                         return err;              1777                         return err;
1909                 }                                1778                 }
1910         }                                        1779         }
1911                                                  1780 
1912         if (attr->ia_valid & ATTR_OPEN) {        1781         if (attr->ia_valid & ATTR_OPEN) {
1913                 /* This is coming from open(.    1782                 /* This is coming from open(..., ... | O_TRUNC); */
1914                 WARN_ON(!(attr->ia_valid & AT    1783                 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1915                 WARN_ON(attr->ia_size != 0);     1784                 WARN_ON(attr->ia_size != 0);
1916                 if (fc->atomic_o_trunc) {        1785                 if (fc->atomic_o_trunc) {
1917                         /*                       1786                         /*
1918                          * No need to send re    1787                          * No need to send request to userspace, since actual
1919                          * truncation has alr    1788                          * truncation has already been done by OPEN.  But still
1920                          * need to truncate p    1789                          * need to truncate page cache.
1921                          */                      1790                          */
1922                         i_size_write(inode, 0    1791                         i_size_write(inode, 0);
1923                         truncate_pagecache(in    1792                         truncate_pagecache(inode, 0);
1924                         goto out;                1793                         goto out;
1925                 }                                1794                 }
1926                 file = NULL;                     1795                 file = NULL;
1927         }                                        1796         }
1928                                                  1797 
1929         /* Flush dirty data/metadata before n    1798         /* Flush dirty data/metadata before non-truncate SETATTR */
1930         if (is_wb &&                             1799         if (is_wb &&
1931             attr->ia_valid &                     1800             attr->ia_valid &
1932                         (ATTR_MODE | ATTR_UID    1801                         (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1933                          ATTR_TIMES_SET)) {      1802                          ATTR_TIMES_SET)) {
1934                 err = write_inode_now(inode,     1803                 err = write_inode_now(inode, true);
1935                 if (err)                         1804                 if (err)
1936                         return err;              1805                         return err;
1937                                                  1806 
1938                 fuse_set_nowrite(inode);         1807                 fuse_set_nowrite(inode);
1939                 fuse_release_nowrite(inode);     1808                 fuse_release_nowrite(inode);
1940         }                                        1809         }
1941                                                  1810 
1942         if (is_truncate) {                       1811         if (is_truncate) {
1943                 fuse_set_nowrite(inode);         1812                 fuse_set_nowrite(inode);
1944                 set_bit(FUSE_I_SIZE_UNSTABLE,    1813                 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1945                 if (trust_local_cmtime && att    1814                 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1946                         attr->ia_valid |= ATT    1815                         attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1947         }                                        1816         }
1948                                                  1817 
1949         memset(&inarg, 0, sizeof(inarg));        1818         memset(&inarg, 0, sizeof(inarg));
1950         memset(&outarg, 0, sizeof(outarg));      1819         memset(&outarg, 0, sizeof(outarg));
1951         iattr_to_fattr(fc, attr, &inarg, trus    1820         iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1952         if (file) {                              1821         if (file) {
1953                 struct fuse_file *ff = file->    1822                 struct fuse_file *ff = file->private_data;
1954                 inarg.valid |= FATTR_FH;         1823                 inarg.valid |= FATTR_FH;
1955                 inarg.fh = ff->fh;               1824                 inarg.fh = ff->fh;
1956         }                                        1825         }
1957                                                  1826 
1958         /* Kill suid/sgid for non-directory c    1827         /* Kill suid/sgid for non-directory chown unconditionally */
1959         if (fc->handle_killpriv_v2 && !S_ISDI    1828         if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
1960             attr->ia_valid & (ATTR_UID | ATTR    1829             attr->ia_valid & (ATTR_UID | ATTR_GID))
1961                 inarg.valid |= FATTR_KILL_SUI    1830                 inarg.valid |= FATTR_KILL_SUIDGID;
1962                                                  1831 
1963         if (attr->ia_valid & ATTR_SIZE) {        1832         if (attr->ia_valid & ATTR_SIZE) {
1964                 /* For mandatory locking in t    1833                 /* For mandatory locking in truncate */
1965                 inarg.valid |= FATTR_LOCKOWNE    1834                 inarg.valid |= FATTR_LOCKOWNER;
1966                 inarg.lock_owner = fuse_lock_    1835                 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1967                                                  1836 
1968                 /* Kill suid/sgid for truncat    1837                 /* Kill suid/sgid for truncate only if no CAP_FSETID */
1969                 if (fc->handle_killpriv_v2 &&    1838                 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1970                         inarg.valid |= FATTR_    1839                         inarg.valid |= FATTR_KILL_SUIDGID;
1971         }                                        1840         }
1972         fuse_setattr_fill(fc, &args, inode, &    1841         fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1973         err = fuse_simple_request(fm, &args);    1842         err = fuse_simple_request(fm, &args);
1974         if (err) {                               1843         if (err) {
1975                 if (err == -EINTR)               1844                 if (err == -EINTR)
1976                         fuse_invalidate_attr(    1845                         fuse_invalidate_attr(inode);
1977                 goto error;                      1846                 goto error;
1978         }                                        1847         }
1979                                                  1848 
1980         if (fuse_invalid_attr(&outarg.attr) |    1849         if (fuse_invalid_attr(&outarg.attr) ||
1981             inode_wrong_type(inode, outarg.at    1850             inode_wrong_type(inode, outarg.attr.mode)) {
1982                 fuse_make_bad(inode);            1851                 fuse_make_bad(inode);
1983                 err = -EIO;                      1852                 err = -EIO;
1984                 goto error;                      1853                 goto error;
1985         }                                        1854         }
1986                                                  1855 
1987         spin_lock(&fi->lock);                    1856         spin_lock(&fi->lock);
1988         /* the kernel maintains i_mtime local    1857         /* the kernel maintains i_mtime locally */
1989         if (trust_local_cmtime) {                1858         if (trust_local_cmtime) {
1990                 if (attr->ia_valid & ATTR_MTI    1859                 if (attr->ia_valid & ATTR_MTIME)
1991                         inode_set_mtime_to_ts !! 1860                         inode->i_mtime = attr->ia_mtime;
1992                 if (attr->ia_valid & ATTR_CTI    1861                 if (attr->ia_valid & ATTR_CTIME)
1993                         inode_set_ctime_to_ts !! 1862                         inode->i_ctime = attr->ia_ctime;
1994                 /* FIXME: clear I_DIRTY_SYNC?    1863                 /* FIXME: clear I_DIRTY_SYNC? */
1995         }                                        1864         }
1996                                                  1865 
1997         fuse_change_attributes_common(inode,  !! 1866         fuse_change_attributes_common(inode, &outarg.attr,
1998                                       ATTR_TI !! 1867                                       attr_timeout(&outarg),
1999                                       fuse_ge    1868                                       fuse_get_cache_mask(inode));
2000         oldsize = inode->i_size;                 1869         oldsize = inode->i_size;
2001         /* see the comment in fuse_change_att    1870         /* see the comment in fuse_change_attributes() */
2002         if (!is_wb || is_truncate)               1871         if (!is_wb || is_truncate)
2003                 i_size_write(inode, outarg.at    1872                 i_size_write(inode, outarg.attr.size);
2004                                                  1873 
2005         if (is_truncate) {                       1874         if (is_truncate) {
2006                 /* NOTE: this may release/rea    1875                 /* NOTE: this may release/reacquire fi->lock */
2007                 __fuse_release_nowrite(inode)    1876                 __fuse_release_nowrite(inode);
2008         }                                        1877         }
2009         spin_unlock(&fi->lock);                  1878         spin_unlock(&fi->lock);
2010                                                  1879 
2011         /*                                       1880         /*
2012          * Only call invalidate_inode_pages2(    1881          * Only call invalidate_inode_pages2() after removing
2013          * FUSE_NOWRITE, otherwise fuse_laund    1882          * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock.
2014          */                                      1883          */
2015         if ((is_truncate || !is_wb) &&           1884         if ((is_truncate || !is_wb) &&
2016             S_ISREG(inode->i_mode) && oldsize    1885             S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
2017                 truncate_pagecache(inode, out    1886                 truncate_pagecache(inode, outarg.attr.size);
2018                 invalidate_inode_pages2(mappi    1887                 invalidate_inode_pages2(mapping);
2019         }                                        1888         }
2020                                                  1889 
2021         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->    1890         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2022 out:                                             1891 out:
2023         if (fault_blocked)                       1892         if (fault_blocked)
2024                 filemap_invalidate_unlock(map    1893                 filemap_invalidate_unlock(mapping);
2025                                                  1894 
2026         return 0;                                1895         return 0;
2027                                                  1896 
2028 error:                                           1897 error:
2029         if (is_truncate)                         1898         if (is_truncate)
2030                 fuse_release_nowrite(inode);     1899                 fuse_release_nowrite(inode);
2031                                                  1900 
2032         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->    1901         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2033                                                  1902 
2034         if (fault_blocked)                       1903         if (fault_blocked)
2035                 filemap_invalidate_unlock(map    1904                 filemap_invalidate_unlock(mapping);
2036         return err;                              1905         return err;
2037 }                                                1906 }
2038                                                  1907 
2039 static int fuse_setattr(struct mnt_idmap *idm    1908 static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,
2040                         struct iattr *attr)      1909                         struct iattr *attr)
2041 {                                                1910 {
2042         struct inode *inode = d_inode(entry);    1911         struct inode *inode = d_inode(entry);
2043         struct fuse_conn *fc = get_fuse_conn(    1912         struct fuse_conn *fc = get_fuse_conn(inode);
2044         struct file *file = (attr->ia_valid &    1913         struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
2045         int ret;                                 1914         int ret;
2046                                                  1915 
2047         if (fuse_is_bad(inode))                  1916         if (fuse_is_bad(inode))
2048                 return -EIO;                     1917                 return -EIO;
2049                                                  1918 
2050         if (!fuse_allow_current_process(get_f    1919         if (!fuse_allow_current_process(get_fuse_conn(inode)))
2051                 return -EACCES;                  1920                 return -EACCES;
2052                                                  1921 
2053         if (attr->ia_valid & (ATTR_KILL_SUID     1922         if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
2054                 attr->ia_valid &= ~(ATTR_KILL    1923                 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
2055                                     ATTR_MODE    1924                                     ATTR_MODE);
2056                                                  1925 
2057                 /*                               1926                 /*
2058                  * The only sane way to relia    1927                  * The only sane way to reliably kill suid/sgid is to do it in
2059                  * the userspace filesystem      1928                  * the userspace filesystem
2060                  *                               1929                  *
2061                  * This should be done on wri    1930                  * This should be done on write(), truncate() and chown().
2062                  */                              1931                  */
2063                 if (!fc->handle_killpriv && !    1932                 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
2064                         /*                       1933                         /*
2065                          * ia_mode calculatio    1934                          * ia_mode calculation may have used stale i_mode.
2066                          * Refresh and recalc    1935                          * Refresh and recalculate.
2067                          */                      1936                          */
2068                         ret = fuse_do_getattr    1937                         ret = fuse_do_getattr(inode, NULL, file);
2069                         if (ret)                 1938                         if (ret)
2070                                 return ret;      1939                                 return ret;
2071                                                  1940 
2072                         attr->ia_mode = inode    1941                         attr->ia_mode = inode->i_mode;
2073                         if (inode->i_mode & S    1942                         if (inode->i_mode & S_ISUID) {
2074                                 attr->ia_vali    1943                                 attr->ia_valid |= ATTR_MODE;
2075                                 attr->ia_mode    1944                                 attr->ia_mode &= ~S_ISUID;
2076                         }                        1945                         }
2077                         if ((inode->i_mode &     1946                         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2078                                 attr->ia_vali    1947                                 attr->ia_valid |= ATTR_MODE;
2079                                 attr->ia_mode    1948                                 attr->ia_mode &= ~S_ISGID;
2080                         }                        1949                         }
2081                 }                                1950                 }
2082         }                                        1951         }
2083         if (!attr->ia_valid)                     1952         if (!attr->ia_valid)
2084                 return 0;                        1953                 return 0;
2085                                                  1954 
2086         ret = fuse_do_setattr(entry, attr, fi    1955         ret = fuse_do_setattr(entry, attr, file);
2087         if (!ret) {                              1956         if (!ret) {
2088                 /*                               1957                 /*
2089                  * If filesystem supports acl    1958                  * If filesystem supports acls it may have updated acl xattrs in
2090                  * the filesystem, so forget     1959                  * the filesystem, so forget cached acls for the inode.
2091                  */                              1960                  */
2092                 if (fc->posix_acl)               1961                 if (fc->posix_acl)
2093                         forget_all_cached_acl    1962                         forget_all_cached_acls(inode);
2094                                                  1963 
2095                 /* Directory mode changed, ma    1964                 /* Directory mode changed, may need to revalidate access */
2096                 if (d_is_dir(entry) && (attr-    1965                 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
2097                         fuse_invalidate_entry    1966                         fuse_invalidate_entry_cache(entry);
2098         }                                        1967         }
2099         return ret;                              1968         return ret;
2100 }                                                1969 }
2101                                                  1970 
2102 static int fuse_getattr(struct mnt_idmap *idm    1971 static int fuse_getattr(struct mnt_idmap *idmap,
2103                         const struct path *pa    1972                         const struct path *path, struct kstat *stat,
2104                         u32 request_mask, uns    1973                         u32 request_mask, unsigned int flags)
2105 {                                                1974 {
2106         struct inode *inode = d_inode(path->d    1975         struct inode *inode = d_inode(path->dentry);
2107         struct fuse_conn *fc = get_fuse_conn(    1976         struct fuse_conn *fc = get_fuse_conn(inode);
2108                                                  1977 
2109         if (fuse_is_bad(inode))                  1978         if (fuse_is_bad(inode))
2110                 return -EIO;                     1979                 return -EIO;
2111                                                  1980 
2112         if (!fuse_allow_current_process(fc))     1981         if (!fuse_allow_current_process(fc)) {
2113                 if (!request_mask) {             1982                 if (!request_mask) {
2114                         /*                       1983                         /*
2115                          * If user explicitly    1984                          * If user explicitly requested *nothing* then don't
2116                          * error out, but ret    1985                          * error out, but return st_dev only.
2117                          */                      1986                          */
2118                         stat->result_mask = 0    1987                         stat->result_mask = 0;
2119                         stat->dev = inode->i_    1988                         stat->dev = inode->i_sb->s_dev;
2120                         return 0;                1989                         return 0;
2121                 }                                1990                 }
2122                 return -EACCES;                  1991                 return -EACCES;
2123         }                                        1992         }
2124                                                  1993 
2125         return fuse_update_get_attr(inode, NU    1994         return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
2126 }                                                1995 }
2127                                                  1996 
2128 static const struct inode_operations fuse_dir    1997 static const struct inode_operations fuse_dir_inode_operations = {
2129         .lookup         = fuse_lookup,           1998         .lookup         = fuse_lookup,
2130         .mkdir          = fuse_mkdir,            1999         .mkdir          = fuse_mkdir,
2131         .symlink        = fuse_symlink,          2000         .symlink        = fuse_symlink,
2132         .unlink         = fuse_unlink,           2001         .unlink         = fuse_unlink,
2133         .rmdir          = fuse_rmdir,            2002         .rmdir          = fuse_rmdir,
2134         .rename         = fuse_rename2,          2003         .rename         = fuse_rename2,
2135         .link           = fuse_link,             2004         .link           = fuse_link,
2136         .setattr        = fuse_setattr,          2005         .setattr        = fuse_setattr,
2137         .create         = fuse_create,           2006         .create         = fuse_create,
2138         .atomic_open    = fuse_atomic_open,      2007         .atomic_open    = fuse_atomic_open,
2139         .tmpfile        = fuse_tmpfile,          2008         .tmpfile        = fuse_tmpfile,
2140         .mknod          = fuse_mknod,            2009         .mknod          = fuse_mknod,
2141         .permission     = fuse_permission,       2010         .permission     = fuse_permission,
2142         .getattr        = fuse_getattr,          2011         .getattr        = fuse_getattr,
2143         .listxattr      = fuse_listxattr,        2012         .listxattr      = fuse_listxattr,
2144         .get_inode_acl  = fuse_get_inode_acl,    2013         .get_inode_acl  = fuse_get_inode_acl,
2145         .get_acl        = fuse_get_acl,          2014         .get_acl        = fuse_get_acl,
2146         .set_acl        = fuse_set_acl,          2015         .set_acl        = fuse_set_acl,
2147         .fileattr_get   = fuse_fileattr_get,     2016         .fileattr_get   = fuse_fileattr_get,
2148         .fileattr_set   = fuse_fileattr_set,     2017         .fileattr_set   = fuse_fileattr_set,
2149 };                                               2018 };
2150                                                  2019 
2151 static const struct file_operations fuse_dir_    2020 static const struct file_operations fuse_dir_operations = {
2152         .llseek         = generic_file_llseek    2021         .llseek         = generic_file_llseek,
2153         .read           = generic_read_dir,      2022         .read           = generic_read_dir,
2154         .iterate_shared = fuse_readdir,          2023         .iterate_shared = fuse_readdir,
2155         .open           = fuse_dir_open,         2024         .open           = fuse_dir_open,
2156         .release        = fuse_dir_release,      2025         .release        = fuse_dir_release,
2157         .fsync          = fuse_dir_fsync,        2026         .fsync          = fuse_dir_fsync,
2158         .unlocked_ioctl = fuse_dir_ioctl,        2027         .unlocked_ioctl = fuse_dir_ioctl,
2159         .compat_ioctl   = fuse_dir_compat_ioc    2028         .compat_ioctl   = fuse_dir_compat_ioctl,
2160 };                                               2029 };
2161                                                  2030 
2162 static const struct inode_operations fuse_com    2031 static const struct inode_operations fuse_common_inode_operations = {
2163         .setattr        = fuse_setattr,          2032         .setattr        = fuse_setattr,
2164         .permission     = fuse_permission,       2033         .permission     = fuse_permission,
2165         .getattr        = fuse_getattr,          2034         .getattr        = fuse_getattr,
2166         .listxattr      = fuse_listxattr,        2035         .listxattr      = fuse_listxattr,
2167         .get_inode_acl  = fuse_get_inode_acl,    2036         .get_inode_acl  = fuse_get_inode_acl,
2168         .get_acl        = fuse_get_acl,          2037         .get_acl        = fuse_get_acl,
2169         .set_acl        = fuse_set_acl,          2038         .set_acl        = fuse_set_acl,
2170         .fileattr_get   = fuse_fileattr_get,     2039         .fileattr_get   = fuse_fileattr_get,
2171         .fileattr_set   = fuse_fileattr_set,     2040         .fileattr_set   = fuse_fileattr_set,
2172 };                                               2041 };
2173                                                  2042 
2174 static const struct inode_operations fuse_sym    2043 static const struct inode_operations fuse_symlink_inode_operations = {
2175         .setattr        = fuse_setattr,          2044         .setattr        = fuse_setattr,
2176         .get_link       = fuse_get_link,         2045         .get_link       = fuse_get_link,
2177         .getattr        = fuse_getattr,          2046         .getattr        = fuse_getattr,
2178         .listxattr      = fuse_listxattr,        2047         .listxattr      = fuse_listxattr,
2179 };                                               2048 };
2180                                                  2049 
2181 void fuse_init_common(struct inode *inode)       2050 void fuse_init_common(struct inode *inode)
2182 {                                                2051 {
2183         inode->i_op = &fuse_common_inode_oper    2052         inode->i_op = &fuse_common_inode_operations;
2184 }                                                2053 }
2185                                                  2054 
2186 void fuse_init_dir(struct inode *inode)          2055 void fuse_init_dir(struct inode *inode)
2187 {                                                2056 {
2188         struct fuse_inode *fi = get_fuse_inod    2057         struct fuse_inode *fi = get_fuse_inode(inode);
2189                                                  2058 
2190         inode->i_op = &fuse_dir_inode_operati    2059         inode->i_op = &fuse_dir_inode_operations;
2191         inode->i_fop = &fuse_dir_operations;     2060         inode->i_fop = &fuse_dir_operations;
2192                                                  2061 
2193         spin_lock_init(&fi->rdc.lock);           2062         spin_lock_init(&fi->rdc.lock);
2194         fi->rdc.cached = false;                  2063         fi->rdc.cached = false;
2195         fi->rdc.size = 0;                        2064         fi->rdc.size = 0;
2196         fi->rdc.pos = 0;                         2065         fi->rdc.pos = 0;
2197         fi->rdc.version = 0;                     2066         fi->rdc.version = 0;
2198 }                                                2067 }
2199                                                  2068 
2200 static int fuse_symlink_read_folio(struct fil    2069 static int fuse_symlink_read_folio(struct file *null, struct folio *folio)
2201 {                                                2070 {
2202         int err = fuse_readlink_page(folio->m    2071         int err = fuse_readlink_page(folio->mapping->host, &folio->page);
2203                                                  2072 
2204         if (!err)                                2073         if (!err)
2205                 folio_mark_uptodate(folio);      2074                 folio_mark_uptodate(folio);
2206                                                  2075 
2207         folio_unlock(folio);                     2076         folio_unlock(folio);
2208                                                  2077 
2209         return err;                              2078         return err;
2210 }                                                2079 }
2211                                                  2080 
2212 static const struct address_space_operations     2081 static const struct address_space_operations fuse_symlink_aops = {
2213         .read_folio     = fuse_symlink_read_f    2082         .read_folio     = fuse_symlink_read_folio,
2214 };                                               2083 };
2215                                                  2084 
2216 void fuse_init_symlink(struct inode *inode)      2085 void fuse_init_symlink(struct inode *inode)
2217 {                                                2086 {
2218         inode->i_op = &fuse_symlink_inode_ope    2087         inode->i_op = &fuse_symlink_inode_operations;
2219         inode->i_data.a_ops = &fuse_symlink_a    2088         inode->i_data.a_ops = &fuse_symlink_aops;
2220         inode_nohighmem(inode);                  2089         inode_nohighmem(inode);
2221 }                                                2090 }
2222                                                  2091 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php