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

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

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ 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-rc3) and /fs/fuse/dir.c (Version linux-6.7.12)


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

~ [ 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