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

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

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ 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.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/fuse/dir.c (Version linux-6.12-rc7) and /fs/fuse/dir.c (Version linux-6.10.14)


  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 mnt_id !! 548 static int get_create_supp_group(struct inode *dir, struct fuse_in_arg *ext)
549                                  struct inode  << 
550                                  struct fuse_i << 
551 {                                                 549 {
552         struct fuse_conn *fc = get_fuse_conn(d    550         struct fuse_conn *fc = get_fuse_conn(dir);
553         struct fuse_ext_header *xh;               551         struct fuse_ext_header *xh;
554         struct fuse_supp_groups *sg;              552         struct fuse_supp_groups *sg;
555         kgid_t kgid = dir->i_gid;                 553         kgid_t kgid = dir->i_gid;
556         vfsgid_t vfsgid = make_vfsgid(idmap, f << 
557         gid_t parent_gid = from_kgid(fc->user_    554         gid_t parent_gid = from_kgid(fc->user_ns, kgid);
558                                                << 
559         u32 sg_len = fuse_ext_size(sizeof(*sg)    555         u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0]));
560                                                   556 
561         if (parent_gid == (gid_t) -1 || vfsgid !! 557         if (parent_gid == (gid_t) -1 || gid_eq(kgid, current_fsgid()) ||
562             !vfsgid_in_group_p(vfsgid))        !! 558             !in_group_p(kgid))
563                 return 0;                         559                 return 0;
564                                                   560 
565         xh = extend_arg(ext, sg_len);             561         xh = extend_arg(ext, sg_len);
566         if (!xh)                                  562         if (!xh)
567                 return -ENOMEM;                   563                 return -ENOMEM;
568                                                   564 
569         xh->size = sg_len;                        565         xh->size = sg_len;
570         xh->type = FUSE_EXT_GROUPS;               566         xh->type = FUSE_EXT_GROUPS;
571                                                   567 
572         sg = (struct fuse_supp_groups *) &xh[1    568         sg = (struct fuse_supp_groups *) &xh[1];
573         sg->nr_groups = 1;                        569         sg->nr_groups = 1;
574         sg->groups[0] = parent_gid;               570         sg->groups[0] = parent_gid;
575                                                   571 
576         return 0;                                 572         return 0;
577 }                                                 573 }
578                                                   574 
579 static int get_create_ext(struct mnt_idmap *id !! 575 static int get_create_ext(struct fuse_args *args,
580                           struct fuse_args *ar << 
581                           struct inode *dir, s    576                           struct inode *dir, struct dentry *dentry,
582                           umode_t mode)           577                           umode_t mode)
583 {                                                 578 {
584         struct fuse_conn *fc = get_fuse_conn_s    579         struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
585         struct fuse_in_arg ext = { .size = 0,     580         struct fuse_in_arg ext = { .size = 0, .value = NULL };
586         int err = 0;                              581         int err = 0;
587                                                   582 
588         if (fc->init_security)                    583         if (fc->init_security)
589                 err = get_security_context(den    584                 err = get_security_context(dentry, mode, &ext);
590         if (!err && fc->create_supp_group)        585         if (!err && fc->create_supp_group)
591                 err = get_create_supp_group(id !! 586                 err = get_create_supp_group(dir, &ext);
592                                                   587 
593         if (!err && ext.size) {                   588         if (!err && ext.size) {
594                 WARN_ON(args->in_numargs >= AR    589                 WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args));
595                 args->is_ext = true;              590                 args->is_ext = true;
596                 args->ext_idx = args->in_numar    591                 args->ext_idx = args->in_numargs++;
597                 args->in_args[args->ext_idx] =    592                 args->in_args[args->ext_idx] = ext;
598         } else {                                  593         } else {
599                 kfree(ext.value);                 594                 kfree(ext.value);
600         }                                         595         }
601                                                   596 
602         return err;                               597         return err;
603 }                                                 598 }
604                                                   599 
605 static void free_ext_value(struct fuse_args *a    600 static void free_ext_value(struct fuse_args *args)
606 {                                                 601 {
607         if (args->is_ext)                         602         if (args->is_ext)
608                 kfree(args->in_args[args->ext_    603                 kfree(args->in_args[args->ext_idx].value);
609 }                                                 604 }
610                                                   605 
611 /*                                                606 /*
612  * Atomic create+open operation                   607  * Atomic create+open operation
613  *                                                608  *
614  * If the filesystem doesn't support this, the    609  * If the filesystem doesn't support this, then fall back to separate
615  * 'mknod' + 'open' requests.                     610  * 'mknod' + 'open' requests.
616  */                                               611  */
617 static int fuse_create_open(struct mnt_idmap * !! 612 static int fuse_create_open(struct inode *dir, struct dentry *entry,
618                             struct dentry *ent !! 613                             struct file *file, unsigned int flags,
619                             unsigned int flags !! 614                             umode_t mode, u32 opcode)
620 {                                                 615 {
621         int err;                                  616         int err;
622         struct inode *inode;                      617         struct inode *inode;
623         struct fuse_mount *fm = get_fuse_mount    618         struct fuse_mount *fm = get_fuse_mount(dir);
624         FUSE_ARGS(args);                          619         FUSE_ARGS(args);
625         struct fuse_forget_link *forget;          620         struct fuse_forget_link *forget;
626         struct fuse_create_in inarg;              621         struct fuse_create_in inarg;
627         struct fuse_open_out *outopenp;           622         struct fuse_open_out *outopenp;
628         struct fuse_entry_out outentry;           623         struct fuse_entry_out outentry;
629         struct fuse_inode *fi;                    624         struct fuse_inode *fi;
630         struct fuse_file *ff;                     625         struct fuse_file *ff;
631         bool trunc = flags & O_TRUNC;             626         bool trunc = flags & O_TRUNC;
632                                                   627 
633         /* Userspace expects S_IFREG in create    628         /* Userspace expects S_IFREG in create mode */
634         BUG_ON((mode & S_IFMT) != S_IFREG);       629         BUG_ON((mode & S_IFMT) != S_IFREG);
635                                                   630 
636         forget = fuse_alloc_forget();             631         forget = fuse_alloc_forget();
637         err = -ENOMEM;                            632         err = -ENOMEM;
638         if (!forget)                              633         if (!forget)
639                 goto out_err;                     634                 goto out_err;
640                                                   635 
641         err = -ENOMEM;                            636         err = -ENOMEM;
642         ff = fuse_file_alloc(fm, true);           637         ff = fuse_file_alloc(fm, true);
643         if (!ff)                                  638         if (!ff)
644                 goto out_put_forget_req;          639                 goto out_put_forget_req;
645                                                   640 
646         if (!fm->fc->dont_mask)                   641         if (!fm->fc->dont_mask)
647                 mode &= ~current_umask();         642                 mode &= ~current_umask();
648                                                   643 
649         flags &= ~O_NOCTTY;                       644         flags &= ~O_NOCTTY;
650         memset(&inarg, 0, sizeof(inarg));         645         memset(&inarg, 0, sizeof(inarg));
651         memset(&outentry, 0, sizeof(outentry))    646         memset(&outentry, 0, sizeof(outentry));
652         inarg.flags = flags;                      647         inarg.flags = flags;
653         inarg.mode = mode;                        648         inarg.mode = mode;
654         inarg.umask = current_umask();            649         inarg.umask = current_umask();
655                                                   650 
656         if (fm->fc->handle_killpriv_v2 && trun    651         if (fm->fc->handle_killpriv_v2 && trunc &&
657             !(flags & O_EXCL) && !capable(CAP_    652             !(flags & O_EXCL) && !capable(CAP_FSETID)) {
658                 inarg.open_flags |= FUSE_OPEN_    653                 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
659         }                                         654         }
660                                                   655 
661         args.opcode = opcode;                     656         args.opcode = opcode;
662         args.nodeid = get_node_id(dir);           657         args.nodeid = get_node_id(dir);
663         args.in_numargs = 2;                      658         args.in_numargs = 2;
664         args.in_args[0].size = sizeof(inarg);     659         args.in_args[0].size = sizeof(inarg);
665         args.in_args[0].value = &inarg;           660         args.in_args[0].value = &inarg;
666         args.in_args[1].size = entry->d_name.l    661         args.in_args[1].size = entry->d_name.len + 1;
667         args.in_args[1].value = entry->d_name.    662         args.in_args[1].value = entry->d_name.name;
668         args.out_numargs = 2;                     663         args.out_numargs = 2;
669         args.out_args[0].size = sizeof(outentr    664         args.out_args[0].size = sizeof(outentry);
670         args.out_args[0].value = &outentry;       665         args.out_args[0].value = &outentry;
671         /* Store outarg for fuse_finish_open()    666         /* Store outarg for fuse_finish_open() */
672         outopenp = &ff->args->open_outarg;        667         outopenp = &ff->args->open_outarg;
673         args.out_args[1].size = sizeof(*outope    668         args.out_args[1].size = sizeof(*outopenp);
674         args.out_args[1].value = outopenp;        669         args.out_args[1].value = outopenp;
675                                                   670 
676         err = get_create_ext(idmap, &args, dir !! 671         err = get_create_ext(&args, dir, entry, mode);
677         if (err)                                  672         if (err)
678                 goto out_free_ff;                 673                 goto out_free_ff;
679                                                   674 
680         err = fuse_simple_idmap_request(idmap, !! 675         err = fuse_simple_request(fm, &args);
681         free_ext_value(&args);                    676         free_ext_value(&args);
682         if (err)                                  677         if (err)
683                 goto out_free_ff;                 678                 goto out_free_ff;
684                                                   679 
685         err = -EIO;                               680         err = -EIO;
686         if (!S_ISREG(outentry.attr.mode) || in    681         if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
687             fuse_invalid_attr(&outentry.attr))    682             fuse_invalid_attr(&outentry.attr))
688                 goto out_free_ff;                 683                 goto out_free_ff;
689                                                   684 
690         ff->fh = outopenp->fh;                    685         ff->fh = outopenp->fh;
691         ff->nodeid = outentry.nodeid;             686         ff->nodeid = outentry.nodeid;
692         ff->open_flags = outopenp->open_flags;    687         ff->open_flags = outopenp->open_flags;
693         inode = fuse_iget(dir->i_sb, outentry.    688         inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
694                           &outentry.attr, ATTR    689                           &outentry.attr, ATTR_TIMEOUT(&outentry), 0);
695         if (!inode) {                             690         if (!inode) {
696                 flags &= ~(O_CREAT | O_EXCL |     691                 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
697                 fuse_sync_release(NULL, ff, fl    692                 fuse_sync_release(NULL, ff, flags);
698                 fuse_queue_forget(fm->fc, forg    693                 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
699                 err = -ENOMEM;                    694                 err = -ENOMEM;
700                 goto out_err;                     695                 goto out_err;
701         }                                         696         }
702         kfree(forget);                            697         kfree(forget);
703         d_instantiate(entry, inode);              698         d_instantiate(entry, inode);
704         fuse_change_entry_timeout(entry, &oute    699         fuse_change_entry_timeout(entry, &outentry);
705         fuse_dir_changed(dir);                    700         fuse_dir_changed(dir);
706         err = generic_file_open(inode, file);     701         err = generic_file_open(inode, file);
707         if (!err) {                               702         if (!err) {
708                 file->private_data = ff;          703                 file->private_data = ff;
709                 err = finish_open(file, entry,    704                 err = finish_open(file, entry, fuse_finish_open);
710         }                                         705         }
711         if (err) {                                706         if (err) {
712                 fi = get_fuse_inode(inode);       707                 fi = get_fuse_inode(inode);
713                 fuse_sync_release(fi, ff, flag    708                 fuse_sync_release(fi, ff, flags);
714         } else {                                  709         } else {
715                 if (fm->fc->atomic_o_trunc &&     710                 if (fm->fc->atomic_o_trunc && trunc)
716                         truncate_pagecache(ino    711                         truncate_pagecache(inode, 0);
717                 else if (!(ff->open_flags & FO    712                 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
718                         invalidate_inode_pages    713                         invalidate_inode_pages2(inode->i_mapping);
719         }                                         714         }
720         return err;                               715         return err;
721                                                   716 
722 out_free_ff:                                      717 out_free_ff:
723         fuse_file_free(ff);                       718         fuse_file_free(ff);
724 out_put_forget_req:                               719 out_put_forget_req:
725         kfree(forget);                            720         kfree(forget);
726 out_err:                                          721 out_err:
727         return err;                               722         return err;
728 }                                                 723 }
729                                                   724 
730 static int fuse_mknod(struct mnt_idmap *, stru    725 static int fuse_mknod(struct mnt_idmap *, struct inode *, struct dentry *,
731                       umode_t, dev_t);            726                       umode_t, dev_t);
732 static int fuse_atomic_open(struct inode *dir,    727 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
733                             struct file *file,    728                             struct file *file, unsigned flags,
734                             umode_t mode)         729                             umode_t mode)
735 {                                                 730 {
736         int err;                                  731         int err;
737         struct mnt_idmap *idmap = file_mnt_idm << 
738         struct fuse_conn *fc = get_fuse_conn(d    732         struct fuse_conn *fc = get_fuse_conn(dir);
739         struct dentry *res = NULL;                733         struct dentry *res = NULL;
740                                                   734 
741         if (fuse_is_bad(dir))                     735         if (fuse_is_bad(dir))
742                 return -EIO;                      736                 return -EIO;
743                                                   737 
744         if (d_in_lookup(entry)) {                 738         if (d_in_lookup(entry)) {
745                 res = fuse_lookup(dir, entry,     739                 res = fuse_lookup(dir, entry, 0);
746                 if (IS_ERR(res))                  740                 if (IS_ERR(res))
747                         return PTR_ERR(res);      741                         return PTR_ERR(res);
748                                                   742 
749                 if (res)                          743                 if (res)
750                         entry = res;              744                         entry = res;
751         }                                         745         }
752                                                   746 
753         if (!(flags & O_CREAT) || d_really_is_    747         if (!(flags & O_CREAT) || d_really_is_positive(entry))
754                 goto no_open;                     748                 goto no_open;
755                                                   749 
756         /* Only creates */                        750         /* Only creates */
757         file->f_mode |= FMODE_CREATED;            751         file->f_mode |= FMODE_CREATED;
758                                                   752 
759         if (fc->no_create)                        753         if (fc->no_create)
760                 goto mknod;                       754                 goto mknod;
761                                                   755 
762         err = fuse_create_open(idmap, dir, ent !! 756         err = fuse_create_open(dir, entry, file, flags, mode, FUSE_CREATE);
763         if (err == -ENOSYS) {                     757         if (err == -ENOSYS) {
764                 fc->no_create = 1;                758                 fc->no_create = 1;
765                 goto mknod;                       759                 goto mknod;
766         } else if (err == -EEXIST)                760         } else if (err == -EEXIST)
767                 fuse_invalidate_entry(entry);     761                 fuse_invalidate_entry(entry);
768 out_dput:                                         762 out_dput:
769         dput(res);                                763         dput(res);
770         return err;                               764         return err;
771                                                   765 
772 mknod:                                            766 mknod:
773         err = fuse_mknod(idmap, dir, entry, mo !! 767         err = fuse_mknod(&nop_mnt_idmap, dir, entry, mode, 0);
774         if (err)                                  768         if (err)
775                 goto out_dput;                    769                 goto out_dput;
776 no_open:                                          770 no_open:
777         return finish_no_open(file, res);         771         return finish_no_open(file, res);
778 }                                                 772 }
779                                                   773 
780 /*                                                774 /*
781  * Code shared between mknod, mkdir, symlink a    775  * Code shared between mknod, mkdir, symlink and link
782  */                                               776  */
783 static int create_new_entry(struct mnt_idmap * !! 777 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
784                             struct fuse_args * !! 778                             struct inode *dir, struct dentry *entry,
785                             struct dentry *ent !! 779                             umode_t mode)
786 {                                                 780 {
787         struct fuse_entry_out outarg;             781         struct fuse_entry_out outarg;
788         struct inode *inode;                      782         struct inode *inode;
789         struct dentry *d;                         783         struct dentry *d;
790         int err;                                  784         int err;
791         struct fuse_forget_link *forget;          785         struct fuse_forget_link *forget;
792                                                   786 
793         if (fuse_is_bad(dir))                     787         if (fuse_is_bad(dir))
794                 return -EIO;                      788                 return -EIO;
795                                                   789 
796         forget = fuse_alloc_forget();             790         forget = fuse_alloc_forget();
797         if (!forget)                              791         if (!forget)
798                 return -ENOMEM;                   792                 return -ENOMEM;
799                                                   793 
800         memset(&outarg, 0, sizeof(outarg));       794         memset(&outarg, 0, sizeof(outarg));
801         args->nodeid = get_node_id(dir);          795         args->nodeid = get_node_id(dir);
802         args->out_numargs = 1;                    796         args->out_numargs = 1;
803         args->out_args[0].size = sizeof(outarg    797         args->out_args[0].size = sizeof(outarg);
804         args->out_args[0].value = &outarg;        798         args->out_args[0].value = &outarg;
805                                                   799 
806         if (args->opcode != FUSE_LINK) {          800         if (args->opcode != FUSE_LINK) {
807                 err = get_create_ext(idmap, ar !! 801                 err = get_create_ext(args, dir, entry, mode);
808                 if (err)                          802                 if (err)
809                         goto out_put_forget_re    803                         goto out_put_forget_req;
810         }                                         804         }
811                                                   805 
812         err = fuse_simple_idmap_request(idmap, !! 806         err = fuse_simple_request(fm, args);
813         free_ext_value(args);                     807         free_ext_value(args);
814         if (err)                                  808         if (err)
815                 goto out_put_forget_req;          809                 goto out_put_forget_req;
816                                                   810 
817         err = -EIO;                               811         err = -EIO;
818         if (invalid_nodeid(outarg.nodeid) || f    812         if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
819                 goto out_put_forget_req;          813                 goto out_put_forget_req;
820                                                   814 
821         if ((outarg.attr.mode ^ mode) & S_IFMT    815         if ((outarg.attr.mode ^ mode) & S_IFMT)
822                 goto out_put_forget_req;          816                 goto out_put_forget_req;
823                                                   817 
824         inode = fuse_iget(dir->i_sb, outarg.no    818         inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
825                           &outarg.attr, ATTR_T    819                           &outarg.attr, ATTR_TIMEOUT(&outarg), 0);
826         if (!inode) {                             820         if (!inode) {
827                 fuse_queue_forget(fm->fc, forg    821                 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
828                 return -ENOMEM;                   822                 return -ENOMEM;
829         }                                         823         }
830         kfree(forget);                            824         kfree(forget);
831                                                   825 
832         d_drop(entry);                            826         d_drop(entry);
833         d = d_splice_alias(inode, entry);         827         d = d_splice_alias(inode, entry);
834         if (IS_ERR(d))                            828         if (IS_ERR(d))
835                 return PTR_ERR(d);                829                 return PTR_ERR(d);
836                                                   830 
837         if (d) {                                  831         if (d) {
838                 fuse_change_entry_timeout(d, &    832                 fuse_change_entry_timeout(d, &outarg);
839                 dput(d);                          833                 dput(d);
840         } else {                                  834         } else {
841                 fuse_change_entry_timeout(entr    835                 fuse_change_entry_timeout(entry, &outarg);
842         }                                         836         }
843         fuse_dir_changed(dir);                    837         fuse_dir_changed(dir);
844         return 0;                                 838         return 0;
845                                                   839 
846  out_put_forget_req:                              840  out_put_forget_req:
847         if (err == -EEXIST)                       841         if (err == -EEXIST)
848                 fuse_invalidate_entry(entry);     842                 fuse_invalidate_entry(entry);
849         kfree(forget);                            843         kfree(forget);
850         return err;                               844         return err;
851 }                                                 845 }
852                                                   846 
853 static int fuse_mknod(struct mnt_idmap *idmap,    847 static int fuse_mknod(struct mnt_idmap *idmap, struct inode *dir,
854                       struct dentry *entry, um    848                       struct dentry *entry, umode_t mode, dev_t rdev)
855 {                                                 849 {
856         struct fuse_mknod_in inarg;               850         struct fuse_mknod_in inarg;
857         struct fuse_mount *fm = get_fuse_mount    851         struct fuse_mount *fm = get_fuse_mount(dir);
858         FUSE_ARGS(args);                          852         FUSE_ARGS(args);
859                                                   853 
860         if (!fm->fc->dont_mask)                   854         if (!fm->fc->dont_mask)
861                 mode &= ~current_umask();         855                 mode &= ~current_umask();
862                                                   856 
863         memset(&inarg, 0, sizeof(inarg));         857         memset(&inarg, 0, sizeof(inarg));
864         inarg.mode = mode;                        858         inarg.mode = mode;
865         inarg.rdev = new_encode_dev(rdev);        859         inarg.rdev = new_encode_dev(rdev);
866         inarg.umask = current_umask();            860         inarg.umask = current_umask();
867         args.opcode = FUSE_MKNOD;                 861         args.opcode = FUSE_MKNOD;
868         args.in_numargs = 2;                      862         args.in_numargs = 2;
869         args.in_args[0].size = sizeof(inarg);     863         args.in_args[0].size = sizeof(inarg);
870         args.in_args[0].value = &inarg;           864         args.in_args[0].value = &inarg;
871         args.in_args[1].size = entry->d_name.l    865         args.in_args[1].size = entry->d_name.len + 1;
872         args.in_args[1].value = entry->d_name.    866         args.in_args[1].value = entry->d_name.name;
873         return create_new_entry(idmap, fm, &ar !! 867         return create_new_entry(fm, &args, dir, entry, mode);
874 }                                                 868 }
875                                                   869 
876 static int fuse_create(struct mnt_idmap *idmap    870 static int fuse_create(struct mnt_idmap *idmap, struct inode *dir,
877                        struct dentry *entry, u    871                        struct dentry *entry, umode_t mode, bool excl)
878 {                                                 872 {
879         return fuse_mknod(idmap, dir, entry, m !! 873         return fuse_mknod(&nop_mnt_idmap, dir, entry, mode, 0);
880 }                                                 874 }
881                                                   875 
882 static int fuse_tmpfile(struct mnt_idmap *idma    876 static int fuse_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
883                         struct file *file, umo    877                         struct file *file, umode_t mode)
884 {                                                 878 {
885         struct fuse_conn *fc = get_fuse_conn(d    879         struct fuse_conn *fc = get_fuse_conn(dir);
886         int err;                                  880         int err;
887                                                   881 
888         if (fc->no_tmpfile)                       882         if (fc->no_tmpfile)
889                 return -EOPNOTSUPP;               883                 return -EOPNOTSUPP;
890                                                   884 
891         err = fuse_create_open(idmap, dir, fil !! 885         err = fuse_create_open(dir, file->f_path.dentry, file, file->f_flags, mode, FUSE_TMPFILE);
892                                file->f_flags,  << 
893         if (err == -ENOSYS) {                     886         if (err == -ENOSYS) {
894                 fc->no_tmpfile = 1;               887                 fc->no_tmpfile = 1;
895                 err = -EOPNOTSUPP;                888                 err = -EOPNOTSUPP;
896         }                                         889         }
897         return err;                               890         return err;
898 }                                                 891 }
899                                                   892 
900 static int fuse_mkdir(struct mnt_idmap *idmap,    893 static int fuse_mkdir(struct mnt_idmap *idmap, struct inode *dir,
901                       struct dentry *entry, um    894                       struct dentry *entry, umode_t mode)
902 {                                                 895 {
903         struct fuse_mkdir_in inarg;               896         struct fuse_mkdir_in inarg;
904         struct fuse_mount *fm = get_fuse_mount    897         struct fuse_mount *fm = get_fuse_mount(dir);
905         FUSE_ARGS(args);                          898         FUSE_ARGS(args);
906                                                   899 
907         if (!fm->fc->dont_mask)                   900         if (!fm->fc->dont_mask)
908                 mode &= ~current_umask();         901                 mode &= ~current_umask();
909                                                   902 
910         memset(&inarg, 0, sizeof(inarg));         903         memset(&inarg, 0, sizeof(inarg));
911         inarg.mode = mode;                        904         inarg.mode = mode;
912         inarg.umask = current_umask();            905         inarg.umask = current_umask();
913         args.opcode = FUSE_MKDIR;                 906         args.opcode = FUSE_MKDIR;
914         args.in_numargs = 2;                      907         args.in_numargs = 2;
915         args.in_args[0].size = sizeof(inarg);     908         args.in_args[0].size = sizeof(inarg);
916         args.in_args[0].value = &inarg;           909         args.in_args[0].value = &inarg;
917         args.in_args[1].size = entry->d_name.l    910         args.in_args[1].size = entry->d_name.len + 1;
918         args.in_args[1].value = entry->d_name.    911         args.in_args[1].value = entry->d_name.name;
919         return create_new_entry(idmap, fm, &ar !! 912         return create_new_entry(fm, &args, dir, entry, S_IFDIR);
920 }                                                 913 }
921                                                   914 
922 static int fuse_symlink(struct mnt_idmap *idma    915 static int fuse_symlink(struct mnt_idmap *idmap, struct inode *dir,
923                         struct dentry *entry,     916                         struct dentry *entry, const char *link)
924 {                                                 917 {
925         struct fuse_mount *fm = get_fuse_mount    918         struct fuse_mount *fm = get_fuse_mount(dir);
926         unsigned len = strlen(link) + 1;          919         unsigned len = strlen(link) + 1;
927         FUSE_ARGS(args);                          920         FUSE_ARGS(args);
928                                                   921 
929         args.opcode = FUSE_SYMLINK;               922         args.opcode = FUSE_SYMLINK;
930         args.in_numargs = 2;                      923         args.in_numargs = 2;
931         args.in_args[0].size = entry->d_name.l    924         args.in_args[0].size = entry->d_name.len + 1;
932         args.in_args[0].value = entry->d_name.    925         args.in_args[0].value = entry->d_name.name;
933         args.in_args[1].size = len;               926         args.in_args[1].size = len;
934         args.in_args[1].value = link;             927         args.in_args[1].value = link;
935         return create_new_entry(idmap, fm, &ar !! 928         return create_new_entry(fm, &args, dir, entry, S_IFLNK);
936 }                                                 929 }
937                                                   930 
938 void fuse_flush_time_update(struct inode *inod    931 void fuse_flush_time_update(struct inode *inode)
939 {                                                 932 {
940         int err = sync_inode_metadata(inode, 1    933         int err = sync_inode_metadata(inode, 1);
941                                                   934 
942         mapping_set_error(inode->i_mapping, er    935         mapping_set_error(inode->i_mapping, err);
943 }                                                 936 }
944                                                   937 
945 static void fuse_update_ctime_in_cache(struct     938 static void fuse_update_ctime_in_cache(struct inode *inode)
946 {                                                 939 {
947         if (!IS_NOCMTIME(inode)) {                940         if (!IS_NOCMTIME(inode)) {
948                 inode_set_ctime_current(inode)    941                 inode_set_ctime_current(inode);
949                 mark_inode_dirty_sync(inode);     942                 mark_inode_dirty_sync(inode);
950                 fuse_flush_time_update(inode);    943                 fuse_flush_time_update(inode);
951         }                                         944         }
952 }                                                 945 }
953                                                   946 
954 void fuse_update_ctime(struct inode *inode)       947 void fuse_update_ctime(struct inode *inode)
955 {                                                 948 {
956         fuse_invalidate_attr_mask(inode, STATX    949         fuse_invalidate_attr_mask(inode, STATX_CTIME);
957         fuse_update_ctime_in_cache(inode);        950         fuse_update_ctime_in_cache(inode);
958 }                                                 951 }
959                                                   952 
960 static void fuse_entry_unlinked(struct dentry     953 static void fuse_entry_unlinked(struct dentry *entry)
961 {                                                 954 {
962         struct inode *inode = d_inode(entry);     955         struct inode *inode = d_inode(entry);
963         struct fuse_conn *fc = get_fuse_conn(i    956         struct fuse_conn *fc = get_fuse_conn(inode);
964         struct fuse_inode *fi = get_fuse_inode    957         struct fuse_inode *fi = get_fuse_inode(inode);
965                                                   958 
966         spin_lock(&fi->lock);                     959         spin_lock(&fi->lock);
967         fi->attr_version = atomic64_inc_return    960         fi->attr_version = atomic64_inc_return(&fc->attr_version);
968         /*                                        961         /*
969          * If i_nlink == 0 then unlink doesn't    962          * If i_nlink == 0 then unlink doesn't make sense, yet this can
970          * happen if userspace filesystem is c    963          * happen if userspace filesystem is careless.  It would be
971          * difficult to enforce correct nlink     964          * difficult to enforce correct nlink usage so just ignore this
972          * condition here                         965          * condition here
973          */                                       966          */
974         if (S_ISDIR(inode->i_mode))               967         if (S_ISDIR(inode->i_mode))
975                 clear_nlink(inode);               968                 clear_nlink(inode);
976         else if (inode->i_nlink > 0)              969         else if (inode->i_nlink > 0)
977                 drop_nlink(inode);                970                 drop_nlink(inode);
978         spin_unlock(&fi->lock);                   971         spin_unlock(&fi->lock);
979         fuse_invalidate_entry_cache(entry);       972         fuse_invalidate_entry_cache(entry);
980         fuse_update_ctime(inode);                 973         fuse_update_ctime(inode);
981 }                                                 974 }
982                                                   975 
983 static int fuse_unlink(struct inode *dir, stru    976 static int fuse_unlink(struct inode *dir, struct dentry *entry)
984 {                                                 977 {
985         int err;                                  978         int err;
986         struct fuse_mount *fm = get_fuse_mount    979         struct fuse_mount *fm = get_fuse_mount(dir);
987         FUSE_ARGS(args);                          980         FUSE_ARGS(args);
988                                                   981 
989         if (fuse_is_bad(dir))                     982         if (fuse_is_bad(dir))
990                 return -EIO;                      983                 return -EIO;
991                                                   984 
992         args.opcode = FUSE_UNLINK;                985         args.opcode = FUSE_UNLINK;
993         args.nodeid = get_node_id(dir);           986         args.nodeid = get_node_id(dir);
994         args.in_numargs = 1;                      987         args.in_numargs = 1;
995         args.in_args[0].size = entry->d_name.l    988         args.in_args[0].size = entry->d_name.len + 1;
996         args.in_args[0].value = entry->d_name.    989         args.in_args[0].value = entry->d_name.name;
997         err = fuse_simple_request(fm, &args);     990         err = fuse_simple_request(fm, &args);
998         if (!err) {                               991         if (!err) {
999                 fuse_dir_changed(dir);            992                 fuse_dir_changed(dir);
1000                 fuse_entry_unlinked(entry);      993                 fuse_entry_unlinked(entry);
1001         } else if (err == -EINTR || err == -E    994         } else if (err == -EINTR || err == -ENOENT)
1002                 fuse_invalidate_entry(entry);    995                 fuse_invalidate_entry(entry);
1003         return err;                              996         return err;
1004 }                                                997 }
1005                                                  998 
1006 static int fuse_rmdir(struct inode *dir, stru    999 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
1007 {                                                1000 {
1008         int err;                                 1001         int err;
1009         struct fuse_mount *fm = get_fuse_moun    1002         struct fuse_mount *fm = get_fuse_mount(dir);
1010         FUSE_ARGS(args);                         1003         FUSE_ARGS(args);
1011                                                  1004 
1012         if (fuse_is_bad(dir))                    1005         if (fuse_is_bad(dir))
1013                 return -EIO;                     1006                 return -EIO;
1014                                                  1007 
1015         args.opcode = FUSE_RMDIR;                1008         args.opcode = FUSE_RMDIR;
1016         args.nodeid = get_node_id(dir);          1009         args.nodeid = get_node_id(dir);
1017         args.in_numargs = 1;                     1010         args.in_numargs = 1;
1018         args.in_args[0].size = entry->d_name.    1011         args.in_args[0].size = entry->d_name.len + 1;
1019         args.in_args[0].value = entry->d_name    1012         args.in_args[0].value = entry->d_name.name;
1020         err = fuse_simple_request(fm, &args);    1013         err = fuse_simple_request(fm, &args);
1021         if (!err) {                              1014         if (!err) {
1022                 fuse_dir_changed(dir);           1015                 fuse_dir_changed(dir);
1023                 fuse_entry_unlinked(entry);      1016                 fuse_entry_unlinked(entry);
1024         } else if (err == -EINTR || err == -E    1017         } else if (err == -EINTR || err == -ENOENT)
1025                 fuse_invalidate_entry(entry);    1018                 fuse_invalidate_entry(entry);
1026         return err;                              1019         return err;
1027 }                                                1020 }
1028                                                  1021 
1029 static int fuse_rename_common(struct mnt_idma !! 1022 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
1030                               struct inode *n    1023                               struct inode *newdir, struct dentry *newent,
1031                               unsigned int fl    1024                               unsigned int flags, int opcode, size_t argsize)
1032 {                                                1025 {
1033         int err;                                 1026         int err;
1034         struct fuse_rename2_in inarg;            1027         struct fuse_rename2_in inarg;
1035         struct fuse_mount *fm = get_fuse_moun    1028         struct fuse_mount *fm = get_fuse_mount(olddir);
1036         FUSE_ARGS(args);                         1029         FUSE_ARGS(args);
1037                                                  1030 
1038         memset(&inarg, 0, argsize);              1031         memset(&inarg, 0, argsize);
1039         inarg.newdir = get_node_id(newdir);      1032         inarg.newdir = get_node_id(newdir);
1040         inarg.flags = flags;                     1033         inarg.flags = flags;
1041         args.opcode = opcode;                    1034         args.opcode = opcode;
1042         args.nodeid = get_node_id(olddir);       1035         args.nodeid = get_node_id(olddir);
1043         args.in_numargs = 3;                     1036         args.in_numargs = 3;
1044         args.in_args[0].size = argsize;          1037         args.in_args[0].size = argsize;
1045         args.in_args[0].value = &inarg;          1038         args.in_args[0].value = &inarg;
1046         args.in_args[1].size = oldent->d_name    1039         args.in_args[1].size = oldent->d_name.len + 1;
1047         args.in_args[1].value = oldent->d_nam    1040         args.in_args[1].value = oldent->d_name.name;
1048         args.in_args[2].size = newent->d_name    1041         args.in_args[2].size = newent->d_name.len + 1;
1049         args.in_args[2].value = newent->d_nam    1042         args.in_args[2].value = newent->d_name.name;
1050         err = fuse_simple_idmap_request(idmap !! 1043         err = fuse_simple_request(fm, &args);
1051         if (!err) {                              1044         if (!err) {
1052                 /* ctime changes */              1045                 /* ctime changes */
1053                 fuse_update_ctime(d_inode(old    1046                 fuse_update_ctime(d_inode(oldent));
1054                                                  1047 
1055                 if (flags & RENAME_EXCHANGE)     1048                 if (flags & RENAME_EXCHANGE)
1056                         fuse_update_ctime(d_i    1049                         fuse_update_ctime(d_inode(newent));
1057                                                  1050 
1058                 fuse_dir_changed(olddir);        1051                 fuse_dir_changed(olddir);
1059                 if (olddir != newdir)            1052                 if (olddir != newdir)
1060                         fuse_dir_changed(newd    1053                         fuse_dir_changed(newdir);
1061                                                  1054 
1062                 /* newent will end up negativ    1055                 /* newent will end up negative */
1063                 if (!(flags & RENAME_EXCHANGE    1056                 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent))
1064                         fuse_entry_unlinked(n    1057                         fuse_entry_unlinked(newent);
1065         } else if (err == -EINTR || err == -E    1058         } else if (err == -EINTR || err == -ENOENT) {
1066                 /* If request was interrupted    1059                 /* If request was interrupted, DEITY only knows if the
1067                    rename actually took place    1060                    rename actually took place.  If the invalidation
1068                    fails (e.g. some process h    1061                    fails (e.g. some process has CWD under the renamed
1069                    directory), then there can    1062                    directory), then there can be inconsistency between
1070                    the dcache and the real fi    1063                    the dcache and the real filesystem.  Tough luck. */
1071                 fuse_invalidate_entry(oldent)    1064                 fuse_invalidate_entry(oldent);
1072                 if (d_really_is_positive(newe    1065                 if (d_really_is_positive(newent))
1073                         fuse_invalidate_entry    1066                         fuse_invalidate_entry(newent);
1074         }                                        1067         }
1075                                                  1068 
1076         return err;                              1069         return err;
1077 }                                                1070 }
1078                                                  1071 
1079 static int fuse_rename2(struct mnt_idmap *idm    1072 static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir,
1080                         struct dentry *oldent    1073                         struct dentry *oldent, struct inode *newdir,
1081                         struct dentry *newent    1074                         struct dentry *newent, unsigned int flags)
1082 {                                                1075 {
1083         struct fuse_conn *fc = get_fuse_conn(    1076         struct fuse_conn *fc = get_fuse_conn(olddir);
1084         int err;                                 1077         int err;
1085                                                  1078 
1086         if (fuse_is_bad(olddir))                 1079         if (fuse_is_bad(olddir))
1087                 return -EIO;                     1080                 return -EIO;
1088                                                  1081 
1089         if (flags & ~(RENAME_NOREPLACE | RENA    1082         if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1090                 return -EINVAL;                  1083                 return -EINVAL;
1091                                                  1084 
1092         if (flags) {                             1085         if (flags) {
1093                 if (fc->no_rename2 || fc->min    1086                 if (fc->no_rename2 || fc->minor < 23)
1094                         return -EINVAL;          1087                         return -EINVAL;
1095                                                  1088 
1096                 err = fuse_rename_common((fla !! 1089                 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
1097                                          oldd << 
1098                                          FUSE    1090                                          FUSE_RENAME2,
1099                                          size    1091                                          sizeof(struct fuse_rename2_in));
1100                 if (err == -ENOSYS) {            1092                 if (err == -ENOSYS) {
1101                         fc->no_rename2 = 1;      1093                         fc->no_rename2 = 1;
1102                         err = -EINVAL;           1094                         err = -EINVAL;
1103                 }                                1095                 }
1104         } else {                                 1096         } else {
1105                 err = fuse_rename_common(&inv !! 1097                 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
1106                                          FUSE    1098                                          FUSE_RENAME,
1107                                          size    1099                                          sizeof(struct fuse_rename_in));
1108         }                                        1100         }
1109                                                  1101 
1110         return err;                              1102         return err;
1111 }                                                1103 }
1112                                                  1104 
1113 static int fuse_link(struct dentry *entry, st    1105 static int fuse_link(struct dentry *entry, struct inode *newdir,
1114                      struct dentry *newent)      1106                      struct dentry *newent)
1115 {                                                1107 {
1116         int err;                                 1108         int err;
1117         struct fuse_link_in inarg;               1109         struct fuse_link_in inarg;
1118         struct inode *inode = d_inode(entry);    1110         struct inode *inode = d_inode(entry);
1119         struct fuse_mount *fm = get_fuse_moun    1111         struct fuse_mount *fm = get_fuse_mount(inode);
1120         FUSE_ARGS(args);                         1112         FUSE_ARGS(args);
1121                                                  1113 
1122         memset(&inarg, 0, sizeof(inarg));        1114         memset(&inarg, 0, sizeof(inarg));
1123         inarg.oldnodeid = get_node_id(inode);    1115         inarg.oldnodeid = get_node_id(inode);
1124         args.opcode = FUSE_LINK;                 1116         args.opcode = FUSE_LINK;
1125         args.in_numargs = 2;                     1117         args.in_numargs = 2;
1126         args.in_args[0].size = sizeof(inarg);    1118         args.in_args[0].size = sizeof(inarg);
1127         args.in_args[0].value = &inarg;          1119         args.in_args[0].value = &inarg;
1128         args.in_args[1].size = newent->d_name    1120         args.in_args[1].size = newent->d_name.len + 1;
1129         args.in_args[1].value = newent->d_nam    1121         args.in_args[1].value = newent->d_name.name;
1130         err = create_new_entry(&invalid_mnt_i !! 1122         err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
1131         if (!err)                                1123         if (!err)
1132                 fuse_update_ctime_in_cache(in    1124                 fuse_update_ctime_in_cache(inode);
1133         else if (err == -EINTR)                  1125         else if (err == -EINTR)
1134                 fuse_invalidate_attr(inode);     1126                 fuse_invalidate_attr(inode);
1135                                                  1127 
1136         return err;                              1128         return err;
1137 }                                                1129 }
1138                                                  1130 
1139 static void fuse_fillattr(struct mnt_idmap *i !! 1131 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1140                           struct fuse_attr *a !! 1132                           struct kstat *stat)
1141 {                                                1133 {
1142         unsigned int blkbits;                    1134         unsigned int blkbits;
1143         struct fuse_conn *fc = get_fuse_conn(    1135         struct fuse_conn *fc = get_fuse_conn(inode);
1144         vfsuid_t vfsuid = make_vfsuid(idmap,  << 
1145                                       make_ku << 
1146         vfsgid_t vfsgid = make_vfsgid(idmap,  << 
1147                                       make_kg << 
1148                                                  1136 
1149         stat->dev = inode->i_sb->s_dev;          1137         stat->dev = inode->i_sb->s_dev;
1150         stat->ino = attr->ino;                   1138         stat->ino = attr->ino;
1151         stat->mode = (inode->i_mode & S_IFMT)    1139         stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1152         stat->nlink = attr->nlink;               1140         stat->nlink = attr->nlink;
1153         stat->uid = vfsuid_into_kuid(vfsuid); !! 1141         stat->uid = make_kuid(fc->user_ns, attr->uid);
1154         stat->gid = vfsgid_into_kgid(vfsgid); !! 1142         stat->gid = make_kgid(fc->user_ns, attr->gid);
1155         stat->rdev = inode->i_rdev;              1143         stat->rdev = inode->i_rdev;
1156         stat->atime.tv_sec = attr->atime;        1144         stat->atime.tv_sec = attr->atime;
1157         stat->atime.tv_nsec = attr->atimensec    1145         stat->atime.tv_nsec = attr->atimensec;
1158         stat->mtime.tv_sec = attr->mtime;        1146         stat->mtime.tv_sec = attr->mtime;
1159         stat->mtime.tv_nsec = attr->mtimensec    1147         stat->mtime.tv_nsec = attr->mtimensec;
1160         stat->ctime.tv_sec = attr->ctime;        1148         stat->ctime.tv_sec = attr->ctime;
1161         stat->ctime.tv_nsec = attr->ctimensec    1149         stat->ctime.tv_nsec = attr->ctimensec;
1162         stat->size = attr->size;                 1150         stat->size = attr->size;
1163         stat->blocks = attr->blocks;             1151         stat->blocks = attr->blocks;
1164                                                  1152 
1165         if (attr->blksize != 0)                  1153         if (attr->blksize != 0)
1166                 blkbits = ilog2(attr->blksize    1154                 blkbits = ilog2(attr->blksize);
1167         else                                     1155         else
1168                 blkbits = inode->i_sb->s_bloc    1156                 blkbits = inode->i_sb->s_blocksize_bits;
1169                                                  1157 
1170         stat->blksize = 1 << blkbits;            1158         stat->blksize = 1 << blkbits;
1171 }                                                1159 }
1172                                                  1160 
1173 static void fuse_statx_to_attr(struct fuse_st    1161 static void fuse_statx_to_attr(struct fuse_statx *sx, struct fuse_attr *attr)
1174 {                                                1162 {
1175         memset(attr, 0, sizeof(*attr));          1163         memset(attr, 0, sizeof(*attr));
1176         attr->ino = sx->ino;                     1164         attr->ino = sx->ino;
1177         attr->size = sx->size;                   1165         attr->size = sx->size;
1178         attr->blocks = sx->blocks;               1166         attr->blocks = sx->blocks;
1179         attr->atime = sx->atime.tv_sec;          1167         attr->atime = sx->atime.tv_sec;
1180         attr->mtime = sx->mtime.tv_sec;          1168         attr->mtime = sx->mtime.tv_sec;
1181         attr->ctime = sx->ctime.tv_sec;          1169         attr->ctime = sx->ctime.tv_sec;
1182         attr->atimensec = sx->atime.tv_nsec;     1170         attr->atimensec = sx->atime.tv_nsec;
1183         attr->mtimensec = sx->mtime.tv_nsec;     1171         attr->mtimensec = sx->mtime.tv_nsec;
1184         attr->ctimensec = sx->ctime.tv_nsec;     1172         attr->ctimensec = sx->ctime.tv_nsec;
1185         attr->mode = sx->mode;                   1173         attr->mode = sx->mode;
1186         attr->nlink = sx->nlink;                 1174         attr->nlink = sx->nlink;
1187         attr->uid = sx->uid;                     1175         attr->uid = sx->uid;
1188         attr->gid = sx->gid;                     1176         attr->gid = sx->gid;
1189         attr->rdev = new_encode_dev(MKDEV(sx-    1177         attr->rdev = new_encode_dev(MKDEV(sx->rdev_major, sx->rdev_minor));
1190         attr->blksize = sx->blksize;             1178         attr->blksize = sx->blksize;
1191 }                                                1179 }
1192                                                  1180 
1193 static int fuse_do_statx(struct mnt_idmap *id !! 1181 static int fuse_do_statx(struct inode *inode, struct file *file,
1194                          struct file *file, s !! 1182                          struct kstat *stat)
1195 {                                                1183 {
1196         int err;                                 1184         int err;
1197         struct fuse_attr attr;                   1185         struct fuse_attr attr;
1198         struct fuse_statx *sx;                   1186         struct fuse_statx *sx;
1199         struct fuse_statx_in inarg;              1187         struct fuse_statx_in inarg;
1200         struct fuse_statx_out outarg;            1188         struct fuse_statx_out outarg;
1201         struct fuse_mount *fm = get_fuse_moun    1189         struct fuse_mount *fm = get_fuse_mount(inode);
1202         u64 attr_version = fuse_get_attr_vers    1190         u64 attr_version = fuse_get_attr_version(fm->fc);
1203         FUSE_ARGS(args);                         1191         FUSE_ARGS(args);
1204                                                  1192 
1205         memset(&inarg, 0, sizeof(inarg));        1193         memset(&inarg, 0, sizeof(inarg));
1206         memset(&outarg, 0, sizeof(outarg));      1194         memset(&outarg, 0, sizeof(outarg));
1207         /* Directories have separate file-han    1195         /* Directories have separate file-handle space */
1208         if (file && S_ISREG(inode->i_mode)) {    1196         if (file && S_ISREG(inode->i_mode)) {
1209                 struct fuse_file *ff = file->    1197                 struct fuse_file *ff = file->private_data;
1210                                                  1198 
1211                 inarg.getattr_flags |= FUSE_G    1199                 inarg.getattr_flags |= FUSE_GETATTR_FH;
1212                 inarg.fh = ff->fh;               1200                 inarg.fh = ff->fh;
1213         }                                        1201         }
1214         /* For now leave sync hints as the de    1202         /* For now leave sync hints as the default, request all stats. */
1215         inarg.sx_flags = 0;                      1203         inarg.sx_flags = 0;
1216         inarg.sx_mask = STATX_BASIC_STATS | S    1204         inarg.sx_mask = STATX_BASIC_STATS | STATX_BTIME;
1217         args.opcode = FUSE_STATX;                1205         args.opcode = FUSE_STATX;
1218         args.nodeid = get_node_id(inode);        1206         args.nodeid = get_node_id(inode);
1219         args.in_numargs = 1;                     1207         args.in_numargs = 1;
1220         args.in_args[0].size = sizeof(inarg);    1208         args.in_args[0].size = sizeof(inarg);
1221         args.in_args[0].value = &inarg;          1209         args.in_args[0].value = &inarg;
1222         args.out_numargs = 1;                    1210         args.out_numargs = 1;
1223         args.out_args[0].size = sizeof(outarg    1211         args.out_args[0].size = sizeof(outarg);
1224         args.out_args[0].value = &outarg;        1212         args.out_args[0].value = &outarg;
1225         err = fuse_simple_request(fm, &args);    1213         err = fuse_simple_request(fm, &args);
1226         if (err)                                 1214         if (err)
1227                 return err;                      1215                 return err;
1228                                                  1216 
1229         sx = &outarg.stat;                       1217         sx = &outarg.stat;
1230         if (((sx->mask & STATX_SIZE) && !fuse    1218         if (((sx->mask & STATX_SIZE) && !fuse_valid_size(sx->size)) ||
1231             ((sx->mask & STATX_TYPE) && (!fus    1219             ((sx->mask & STATX_TYPE) && (!fuse_valid_type(sx->mode) ||
1232                                          inod    1220                                          inode_wrong_type(inode, sx->mode)))) {
1233                 fuse_make_bad(inode);            1221                 fuse_make_bad(inode);
1234                 return -EIO;                     1222                 return -EIO;
1235         }                                        1223         }
1236                                                  1224 
1237         fuse_statx_to_attr(&outarg.stat, &att    1225         fuse_statx_to_attr(&outarg.stat, &attr);
1238         if ((sx->mask & STATX_BASIC_STATS) ==    1226         if ((sx->mask & STATX_BASIC_STATS) == STATX_BASIC_STATS) {
1239                 fuse_change_attributes(inode,    1227                 fuse_change_attributes(inode, &attr, &outarg.stat,
1240                                        ATTR_T    1228                                        ATTR_TIMEOUT(&outarg), attr_version);
1241         }                                        1229         }
1242                                                  1230 
1243         if (stat) {                              1231         if (stat) {
1244                 stat->result_mask = sx->mask     1232                 stat->result_mask = sx->mask & (STATX_BASIC_STATS | STATX_BTIME);
1245                 stat->btime.tv_sec = sx->btim    1233                 stat->btime.tv_sec = sx->btime.tv_sec;
1246                 stat->btime.tv_nsec = min_t(u    1234                 stat->btime.tv_nsec = min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1);
1247                 fuse_fillattr(idmap, inode, & !! 1235                 fuse_fillattr(inode, &attr, stat);
1248                 stat->result_mask |= STATX_TY    1236                 stat->result_mask |= STATX_TYPE;
1249         }                                        1237         }
1250                                                  1238 
1251         return 0;                                1239         return 0;
1252 }                                                1240 }
1253                                                  1241 
1254 static int fuse_do_getattr(struct mnt_idmap * !! 1242 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
1255                            struct kstat *stat !! 1243                            struct file *file)
1256 {                                                1244 {
1257         int err;                                 1245         int err;
1258         struct fuse_getattr_in inarg;            1246         struct fuse_getattr_in inarg;
1259         struct fuse_attr_out outarg;             1247         struct fuse_attr_out outarg;
1260         struct fuse_mount *fm = get_fuse_moun    1248         struct fuse_mount *fm = get_fuse_mount(inode);
1261         FUSE_ARGS(args);                         1249         FUSE_ARGS(args);
1262         u64 attr_version;                        1250         u64 attr_version;
1263                                                  1251 
1264         attr_version = fuse_get_attr_version(    1252         attr_version = fuse_get_attr_version(fm->fc);
1265                                                  1253 
1266         memset(&inarg, 0, sizeof(inarg));        1254         memset(&inarg, 0, sizeof(inarg));
1267         memset(&outarg, 0, sizeof(outarg));      1255         memset(&outarg, 0, sizeof(outarg));
1268         /* Directories have separate file-han    1256         /* Directories have separate file-handle space */
1269         if (file && S_ISREG(inode->i_mode)) {    1257         if (file && S_ISREG(inode->i_mode)) {
1270                 struct fuse_file *ff = file->    1258                 struct fuse_file *ff = file->private_data;
1271                                                  1259 
1272                 inarg.getattr_flags |= FUSE_G    1260                 inarg.getattr_flags |= FUSE_GETATTR_FH;
1273                 inarg.fh = ff->fh;               1261                 inarg.fh = ff->fh;
1274         }                                        1262         }
1275         args.opcode = FUSE_GETATTR;              1263         args.opcode = FUSE_GETATTR;
1276         args.nodeid = get_node_id(inode);        1264         args.nodeid = get_node_id(inode);
1277         args.in_numargs = 1;                     1265         args.in_numargs = 1;
1278         args.in_args[0].size = sizeof(inarg);    1266         args.in_args[0].size = sizeof(inarg);
1279         args.in_args[0].value = &inarg;          1267         args.in_args[0].value = &inarg;
1280         args.out_numargs = 1;                    1268         args.out_numargs = 1;
1281         args.out_args[0].size = sizeof(outarg    1269         args.out_args[0].size = sizeof(outarg);
1282         args.out_args[0].value = &outarg;        1270         args.out_args[0].value = &outarg;
1283         err = fuse_simple_request(fm, &args);    1271         err = fuse_simple_request(fm, &args);
1284         if (!err) {                              1272         if (!err) {
1285                 if (fuse_invalid_attr(&outarg    1273                 if (fuse_invalid_attr(&outarg.attr) ||
1286                     inode_wrong_type(inode, o    1274                     inode_wrong_type(inode, outarg.attr.mode)) {
1287                         fuse_make_bad(inode);    1275                         fuse_make_bad(inode);
1288                         err = -EIO;              1276                         err = -EIO;
1289                 } else {                         1277                 } else {
1290                         fuse_change_attribute    1278                         fuse_change_attributes(inode, &outarg.attr, NULL,
1291                                                  1279                                                ATTR_TIMEOUT(&outarg),
1292                                                  1280                                                attr_version);
1293                         if (stat)                1281                         if (stat)
1294                                 fuse_fillattr !! 1282                                 fuse_fillattr(inode, &outarg.attr, stat);
1295                 }                                1283                 }
1296         }                                        1284         }
1297         return err;                              1285         return err;
1298 }                                                1286 }
1299                                                  1287 
1300 static int fuse_update_get_attr(struct mnt_id !! 1288 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1301                                 struct file * !! 1289                                 struct kstat *stat, u32 request_mask,
1302                                 u32 request_m !! 1290                                 unsigned int flags)
1303 {                                                1291 {
1304         struct fuse_inode *fi = get_fuse_inod    1292         struct fuse_inode *fi = get_fuse_inode(inode);
1305         struct fuse_conn *fc = get_fuse_conn(    1293         struct fuse_conn *fc = get_fuse_conn(inode);
1306         int err = 0;                             1294         int err = 0;
1307         bool sync;                               1295         bool sync;
1308         u32 inval_mask = READ_ONCE(fi->inval_    1296         u32 inval_mask = READ_ONCE(fi->inval_mask);
1309         u32 cache_mask = fuse_get_cache_mask(    1297         u32 cache_mask = fuse_get_cache_mask(inode);
1310                                                  1298 
1311                                                  1299 
1312         /* FUSE only supports basic stats and    1300         /* FUSE only supports basic stats and possibly btime */
1313         request_mask &= STATX_BASIC_STATS | S    1301         request_mask &= STATX_BASIC_STATS | STATX_BTIME;
1314 retry:                                           1302 retry:
1315         if (fc->no_statx)                        1303         if (fc->no_statx)
1316                 request_mask &= STATX_BASIC_S    1304                 request_mask &= STATX_BASIC_STATS;
1317                                                  1305 
1318         if (!request_mask)                       1306         if (!request_mask)
1319                 sync = false;                    1307                 sync = false;
1320         else if (flags & AT_STATX_FORCE_SYNC)    1308         else if (flags & AT_STATX_FORCE_SYNC)
1321                 sync = true;                     1309                 sync = true;
1322         else if (flags & AT_STATX_DONT_SYNC)     1310         else if (flags & AT_STATX_DONT_SYNC)
1323                 sync = false;                    1311                 sync = false;
1324         else if (request_mask & inval_mask &     1312         else if (request_mask & inval_mask & ~cache_mask)
1325                 sync = true;                     1313                 sync = true;
1326         else                                     1314         else
1327                 sync = time_before64(fi->i_ti    1315                 sync = time_before64(fi->i_time, get_jiffies_64());
1328                                                  1316 
1329         if (sync) {                              1317         if (sync) {
1330                 forget_all_cached_acls(inode)    1318                 forget_all_cached_acls(inode);
1331                 /* Try statx if BTIME is requ    1319                 /* Try statx if BTIME is requested */
1332                 if (!fc->no_statx && (request    1320                 if (!fc->no_statx && (request_mask & ~STATX_BASIC_STATS)) {
1333                         err = fuse_do_statx(i !! 1321                         err = fuse_do_statx(inode, file, stat);
1334                         if (err == -ENOSYS) {    1322                         if (err == -ENOSYS) {
1335                                 fc->no_statx     1323                                 fc->no_statx = 1;
1336                                 err = 0;         1324                                 err = 0;
1337                                 goto retry;      1325                                 goto retry;
1338                         }                        1326                         }
1339                 } else {                         1327                 } else {
1340                         err = fuse_do_getattr !! 1328                         err = fuse_do_getattr(inode, stat, file);
1341                 }                                1329                 }
1342         } else if (stat) {                       1330         } else if (stat) {
1343                 generic_fillattr(idmap, reque !! 1331                 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1344                 stat->mode = fi->orig_i_mode;    1332                 stat->mode = fi->orig_i_mode;
1345                 stat->ino = fi->orig_ino;        1333                 stat->ino = fi->orig_ino;
1346                 if (test_bit(FUSE_I_BTIME, &f    1334                 if (test_bit(FUSE_I_BTIME, &fi->state)) {
1347                         stat->btime = fi->i_b    1335                         stat->btime = fi->i_btime;
1348                         stat->result_mask |=     1336                         stat->result_mask |= STATX_BTIME;
1349                 }                                1337                 }
1350         }                                        1338         }
1351                                                  1339 
1352         return err;                              1340         return err;
1353 }                                                1341 }
1354                                                  1342 
1355 int fuse_update_attributes(struct inode *inod    1343 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask)
1356 {                                                1344 {
1357         return fuse_update_get_attr(&nop_mnt_ !! 1345         return fuse_update_get_attr(inode, file, NULL, mask, 0);
1358 }                                                1346 }
1359                                                  1347 
1360 int fuse_reverse_inval_entry(struct fuse_conn    1348 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1361                              u64 child_nodeid    1349                              u64 child_nodeid, struct qstr *name, u32 flags)
1362 {                                                1350 {
1363         int err = -ENOTDIR;                      1351         int err = -ENOTDIR;
1364         struct inode *parent;                    1352         struct inode *parent;
1365         struct dentry *dir;                      1353         struct dentry *dir;
1366         struct dentry *entry;                    1354         struct dentry *entry;
1367                                                  1355 
1368         parent = fuse_ilookup(fc, parent_node    1356         parent = fuse_ilookup(fc, parent_nodeid, NULL);
1369         if (!parent)                             1357         if (!parent)
1370                 return -ENOENT;                  1358                 return -ENOENT;
1371                                                  1359 
1372         inode_lock_nested(parent, I_MUTEX_PAR    1360         inode_lock_nested(parent, I_MUTEX_PARENT);
1373         if (!S_ISDIR(parent->i_mode))            1361         if (!S_ISDIR(parent->i_mode))
1374                 goto unlock;                     1362                 goto unlock;
1375                                                  1363 
1376         err = -ENOENT;                           1364         err = -ENOENT;
1377         dir = d_find_alias(parent);              1365         dir = d_find_alias(parent);
1378         if (!dir)                                1366         if (!dir)
1379                 goto unlock;                     1367                 goto unlock;
1380                                                  1368 
1381         name->hash = full_name_hash(dir, name    1369         name->hash = full_name_hash(dir, name->name, name->len);
1382         entry = d_lookup(dir, name);             1370         entry = d_lookup(dir, name);
1383         dput(dir);                               1371         dput(dir);
1384         if (!entry)                              1372         if (!entry)
1385                 goto unlock;                     1373                 goto unlock;
1386                                                  1374 
1387         fuse_dir_changed(parent);                1375         fuse_dir_changed(parent);
1388         if (!(flags & FUSE_EXPIRE_ONLY))         1376         if (!(flags & FUSE_EXPIRE_ONLY))
1389                 d_invalidate(entry);             1377                 d_invalidate(entry);
1390         fuse_invalidate_entry_cache(entry);      1378         fuse_invalidate_entry_cache(entry);
1391                                                  1379 
1392         if (child_nodeid != 0 && d_really_is_    1380         if (child_nodeid != 0 && d_really_is_positive(entry)) {
1393                 inode_lock(d_inode(entry));      1381                 inode_lock(d_inode(entry));
1394                 if (get_node_id(d_inode(entry    1382                 if (get_node_id(d_inode(entry)) != child_nodeid) {
1395                         err = -ENOENT;           1383                         err = -ENOENT;
1396                         goto badentry;           1384                         goto badentry;
1397                 }                                1385                 }
1398                 if (d_mountpoint(entry)) {       1386                 if (d_mountpoint(entry)) {
1399                         err = -EBUSY;            1387                         err = -EBUSY;
1400                         goto badentry;           1388                         goto badentry;
1401                 }                                1389                 }
1402                 if (d_is_dir(entry)) {           1390                 if (d_is_dir(entry)) {
1403                         shrink_dcache_parent(    1391                         shrink_dcache_parent(entry);
1404                         if (!simple_empty(ent    1392                         if (!simple_empty(entry)) {
1405                                 err = -ENOTEM    1393                                 err = -ENOTEMPTY;
1406                                 goto badentry    1394                                 goto badentry;
1407                         }                        1395                         }
1408                         d_inode(entry)->i_fla    1396                         d_inode(entry)->i_flags |= S_DEAD;
1409                 }                                1397                 }
1410                 dont_mount(entry);               1398                 dont_mount(entry);
1411                 clear_nlink(d_inode(entry));     1399                 clear_nlink(d_inode(entry));
1412                 err = 0;                         1400                 err = 0;
1413  badentry:                                       1401  badentry:
1414                 inode_unlock(d_inode(entry));    1402                 inode_unlock(d_inode(entry));
1415                 if (!err)                        1403                 if (!err)
1416                         d_delete(entry);         1404                         d_delete(entry);
1417         } else {                                 1405         } else {
1418                 err = 0;                         1406                 err = 0;
1419         }                                        1407         }
1420         dput(entry);                             1408         dput(entry);
1421                                                  1409 
1422  unlock:                                         1410  unlock:
1423         inode_unlock(parent);                    1411         inode_unlock(parent);
1424         iput(parent);                            1412         iput(parent);
1425         return err;                              1413         return err;
1426 }                                                1414 }
1427                                                  1415 
1428 static inline bool fuse_permissible_uidgid(st    1416 static inline bool fuse_permissible_uidgid(struct fuse_conn *fc)
1429 {                                                1417 {
1430         const struct cred *cred = current_cre    1418         const struct cred *cred = current_cred();
1431                                                  1419 
1432         return (uid_eq(cred->euid, fc->user_i    1420         return (uid_eq(cred->euid, fc->user_id) &&
1433                 uid_eq(cred->suid, fc->user_i    1421                 uid_eq(cred->suid, fc->user_id) &&
1434                 uid_eq(cred->uid,  fc->user_i    1422                 uid_eq(cred->uid,  fc->user_id) &&
1435                 gid_eq(cred->egid, fc->group_    1423                 gid_eq(cred->egid, fc->group_id) &&
1436                 gid_eq(cred->sgid, fc->group_    1424                 gid_eq(cred->sgid, fc->group_id) &&
1437                 gid_eq(cred->gid,  fc->group_    1425                 gid_eq(cred->gid,  fc->group_id));
1438 }                                                1426 }
1439                                                  1427 
1440 /*                                               1428 /*
1441  * Calling into a user-controlled filesystem     1429  * Calling into a user-controlled filesystem gives the filesystem
1442  * daemon ptrace-like capabilities over the c    1430  * daemon ptrace-like capabilities over the current process.  This
1443  * means, that the filesystem daemon is able     1431  * means, that the filesystem daemon is able to record the exact
1444  * filesystem operations performed, and can a    1432  * filesystem operations performed, and can also control the behavior
1445  * of the requester process in otherwise impo    1433  * of the requester process in otherwise impossible ways.  For example
1446  * it can delay the operation for arbitrary l    1434  * it can delay the operation for arbitrary length of time allowing
1447  * DoS against the requester.                    1435  * DoS against the requester.
1448  *                                               1436  *
1449  * For this reason only those processes can c    1437  * For this reason only those processes can call into the filesystem,
1450  * for which the owner of the mount has ptrac    1438  * for which the owner of the mount has ptrace privilege.  This
1451  * excludes processes started by other users,    1439  * excludes processes started by other users, suid or sgid processes.
1452  */                                              1440  */
1453 bool fuse_allow_current_process(struct fuse_c    1441 bool fuse_allow_current_process(struct fuse_conn *fc)
1454 {                                                1442 {
1455         bool allow;                              1443         bool allow;
1456                                                  1444 
1457         if (fc->allow_other)                     1445         if (fc->allow_other)
1458                 allow = current_in_userns(fc-    1446                 allow = current_in_userns(fc->user_ns);
1459         else                                     1447         else
1460                 allow = fuse_permissible_uidg    1448                 allow = fuse_permissible_uidgid(fc);
1461                                                  1449 
1462         if (!allow && allow_sys_admin_access     1450         if (!allow && allow_sys_admin_access && capable(CAP_SYS_ADMIN))
1463                 allow = true;                    1451                 allow = true;
1464                                                  1452 
1465         return allow;                            1453         return allow;
1466 }                                                1454 }
1467                                                  1455 
1468 static int fuse_access(struct inode *inode, i    1456 static int fuse_access(struct inode *inode, int mask)
1469 {                                                1457 {
1470         struct fuse_mount *fm = get_fuse_moun    1458         struct fuse_mount *fm = get_fuse_mount(inode);
1471         FUSE_ARGS(args);                         1459         FUSE_ARGS(args);
1472         struct fuse_access_in inarg;             1460         struct fuse_access_in inarg;
1473         int err;                                 1461         int err;
1474                                                  1462 
1475         BUG_ON(mask & MAY_NOT_BLOCK);            1463         BUG_ON(mask & MAY_NOT_BLOCK);
1476                                                  1464 
1477         /*                                    << 
1478          * We should not send FUSE_ACCESS to  << 
1479          * when idmapped mounts are enabled a << 
1480          * we have fc->default_permissions =  << 
1481          * permission checks are done on the  << 
1482          */                                   << 
1483         WARN_ON_ONCE(!(fm->sb->s_iflags & SB_ << 
1484                                               << 
1485         if (fm->fc->no_access)                   1465         if (fm->fc->no_access)
1486                 return 0;                        1466                 return 0;
1487                                                  1467 
1488         memset(&inarg, 0, sizeof(inarg));        1468         memset(&inarg, 0, sizeof(inarg));
1489         inarg.mask = mask & (MAY_READ | MAY_W    1469         inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1490         args.opcode = FUSE_ACCESS;               1470         args.opcode = FUSE_ACCESS;
1491         args.nodeid = get_node_id(inode);        1471         args.nodeid = get_node_id(inode);
1492         args.in_numargs = 1;                     1472         args.in_numargs = 1;
1493         args.in_args[0].size = sizeof(inarg);    1473         args.in_args[0].size = sizeof(inarg);
1494         args.in_args[0].value = &inarg;          1474         args.in_args[0].value = &inarg;
1495         err = fuse_simple_request(fm, &args);    1475         err = fuse_simple_request(fm, &args);
1496         if (err == -ENOSYS) {                    1476         if (err == -ENOSYS) {
1497                 fm->fc->no_access = 1;           1477                 fm->fc->no_access = 1;
1498                 err = 0;                         1478                 err = 0;
1499         }                                        1479         }
1500         return err;                              1480         return err;
1501 }                                                1481 }
1502                                                  1482 
1503 static int fuse_perm_getattr(struct inode *in    1483 static int fuse_perm_getattr(struct inode *inode, int mask)
1504 {                                                1484 {
1505         if (mask & MAY_NOT_BLOCK)                1485         if (mask & MAY_NOT_BLOCK)
1506                 return -ECHILD;                  1486                 return -ECHILD;
1507                                                  1487 
1508         forget_all_cached_acls(inode);           1488         forget_all_cached_acls(inode);
1509         return fuse_do_getattr(&nop_mnt_idmap !! 1489         return fuse_do_getattr(inode, NULL, NULL);
1510 }                                                1490 }
1511                                                  1491 
1512 /*                                               1492 /*
1513  * Check permission.  The two basic access mo    1493  * Check permission.  The two basic access models of FUSE are:
1514  *                                               1494  *
1515  * 1) Local access checking ('default_permiss    1495  * 1) Local access checking ('default_permissions' mount option) based
1516  * on file mode.  This is the plain old disk     1496  * on file mode.  This is the plain old disk filesystem permission
1517  * model.                                        1497  * model.
1518  *                                               1498  *
1519  * 2) "Remote" access checking, where server     1499  * 2) "Remote" access checking, where server is responsible for
1520  * checking permission in each inode operatio    1500  * checking permission in each inode operation.  An exception to this
1521  * is if ->permission() was invoked from sys_    1501  * is if ->permission() was invoked from sys_access() in which case an
1522  * access request is sent.  Execute permissio    1502  * access request is sent.  Execute permission is still checked
1523  * locally based on file mode.                   1503  * locally based on file mode.
1524  */                                              1504  */
1525 static int fuse_permission(struct mnt_idmap *    1505 static int fuse_permission(struct mnt_idmap *idmap,
1526                            struct inode *inod    1506                            struct inode *inode, int mask)
1527 {                                                1507 {
1528         struct fuse_conn *fc = get_fuse_conn(    1508         struct fuse_conn *fc = get_fuse_conn(inode);
1529         bool refreshed = false;                  1509         bool refreshed = false;
1530         int err = 0;                             1510         int err = 0;
1531                                                  1511 
1532         if (fuse_is_bad(inode))                  1512         if (fuse_is_bad(inode))
1533                 return -EIO;                     1513                 return -EIO;
1534                                                  1514 
1535         if (!fuse_allow_current_process(fc))     1515         if (!fuse_allow_current_process(fc))
1536                 return -EACCES;                  1516                 return -EACCES;
1537                                                  1517 
1538         /*                                       1518         /*
1539          * If attributes are needed, refresh     1519          * If attributes are needed, refresh them before proceeding
1540          */                                      1520          */
1541         if (fc->default_permissions ||           1521         if (fc->default_permissions ||
1542             ((mask & MAY_EXEC) && S_ISREG(ino    1522             ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1543                 struct fuse_inode *fi = get_f    1523                 struct fuse_inode *fi = get_fuse_inode(inode);
1544                 u32 perm_mask = STATX_MODE |     1524                 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1545                                                  1525 
1546                 if (perm_mask & READ_ONCE(fi-    1526                 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1547                     time_before64(fi->i_time,    1527                     time_before64(fi->i_time, get_jiffies_64())) {
1548                         refreshed = true;        1528                         refreshed = true;
1549                                                  1529 
1550                         err = fuse_perm_getat    1530                         err = fuse_perm_getattr(inode, mask);
1551                         if (err)                 1531                         if (err)
1552                                 return err;      1532                                 return err;
1553                 }                                1533                 }
1554         }                                        1534         }
1555                                                  1535 
1556         if (fc->default_permissions) {           1536         if (fc->default_permissions) {
1557                 err = generic_permission(idma !! 1537                 err = generic_permission(&nop_mnt_idmap, inode, mask);
1558                                                  1538 
1559                 /* If permission is denied, t    1539                 /* If permission is denied, try to refresh file
1560                    attributes.  This is also     1540                    attributes.  This is also needed, because the root
1561                    node will at first have no    1541                    node will at first have no permissions */
1562                 if (err == -EACCES && !refres    1542                 if (err == -EACCES && !refreshed) {
1563                         err = fuse_perm_getat    1543                         err = fuse_perm_getattr(inode, mask);
1564                         if (!err)                1544                         if (!err)
1565                                 err = generic !! 1545                                 err = generic_permission(&nop_mnt_idmap,
1566                                                  1546                                                          inode, mask);
1567                 }                                1547                 }
1568                                                  1548 
1569                 /* Note: the opposite of the     1549                 /* Note: the opposite of the above test does not
1570                    exist.  So if permissions     1550                    exist.  So if permissions are revoked this won't be
1571                    noticed immediately, only     1551                    noticed immediately, only after the attribute
1572                    timeout has expired */        1552                    timeout has expired */
1573         } else if (mask & (MAY_ACCESS | MAY_C    1553         } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1574                 err = fuse_access(inode, mask    1554                 err = fuse_access(inode, mask);
1575         } else if ((mask & MAY_EXEC) && S_ISR    1555         } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1576                 if (!(inode->i_mode & S_IXUGO    1556                 if (!(inode->i_mode & S_IXUGO)) {
1577                         if (refreshed)           1557                         if (refreshed)
1578                                 return -EACCE    1558                                 return -EACCES;
1579                                                  1559 
1580                         err = fuse_perm_getat    1560                         err = fuse_perm_getattr(inode, mask);
1581                         if (!err && !(inode->    1561                         if (!err && !(inode->i_mode & S_IXUGO))
1582                                 return -EACCE    1562                                 return -EACCES;
1583                 }                                1563                 }
1584         }                                        1564         }
1585         return err;                              1565         return err;
1586 }                                                1566 }
1587                                                  1567 
1588 static int fuse_readlink_page(struct inode *i    1568 static int fuse_readlink_page(struct inode *inode, struct page *page)
1589 {                                                1569 {
1590         struct fuse_mount *fm = get_fuse_moun    1570         struct fuse_mount *fm = get_fuse_mount(inode);
1591         struct fuse_page_desc desc = { .lengt    1571         struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1592         struct fuse_args_pages ap = {            1572         struct fuse_args_pages ap = {
1593                 .num_pages = 1,                  1573                 .num_pages = 1,
1594                 .pages = &page,                  1574                 .pages = &page,
1595                 .descs = &desc,                  1575                 .descs = &desc,
1596         };                                       1576         };
1597         char *link;                              1577         char *link;
1598         ssize_t res;                             1578         ssize_t res;
1599                                                  1579 
1600         ap.args.opcode = FUSE_READLINK;          1580         ap.args.opcode = FUSE_READLINK;
1601         ap.args.nodeid = get_node_id(inode);     1581         ap.args.nodeid = get_node_id(inode);
1602         ap.args.out_pages = true;                1582         ap.args.out_pages = true;
1603         ap.args.out_argvar = true;               1583         ap.args.out_argvar = true;
1604         ap.args.page_zeroing = true;             1584         ap.args.page_zeroing = true;
1605         ap.args.out_numargs = 1;                 1585         ap.args.out_numargs = 1;
1606         ap.args.out_args[0].size = desc.lengt    1586         ap.args.out_args[0].size = desc.length;
1607         res = fuse_simple_request(fm, &ap.arg    1587         res = fuse_simple_request(fm, &ap.args);
1608                                                  1588 
1609         fuse_invalidate_atime(inode);            1589         fuse_invalidate_atime(inode);
1610                                                  1590 
1611         if (res < 0)                             1591         if (res < 0)
1612                 return res;                      1592                 return res;
1613                                                  1593 
1614         if (WARN_ON(res >= PAGE_SIZE))           1594         if (WARN_ON(res >= PAGE_SIZE))
1615                 return -EIO;                     1595                 return -EIO;
1616                                                  1596 
1617         link = page_address(page);               1597         link = page_address(page);
1618         link[res] = '\0';                        1598         link[res] = '\0';
1619                                                  1599 
1620         return 0;                                1600         return 0;
1621 }                                                1601 }
1622                                                  1602 
1623 static const char *fuse_get_link(struct dentr    1603 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1624                                  struct delay    1604                                  struct delayed_call *callback)
1625 {                                                1605 {
1626         struct fuse_conn *fc = get_fuse_conn(    1606         struct fuse_conn *fc = get_fuse_conn(inode);
1627         struct page *page;                       1607         struct page *page;
1628         int err;                                 1608         int err;
1629                                                  1609 
1630         err = -EIO;                              1610         err = -EIO;
1631         if (fuse_is_bad(inode))                  1611         if (fuse_is_bad(inode))
1632                 goto out_err;                    1612                 goto out_err;
1633                                                  1613 
1634         if (fc->cache_symlinks)                  1614         if (fc->cache_symlinks)
1635                 return page_get_link(dentry,     1615                 return page_get_link(dentry, inode, callback);
1636                                                  1616 
1637         err = -ECHILD;                           1617         err = -ECHILD;
1638         if (!dentry)                             1618         if (!dentry)
1639                 goto out_err;                    1619                 goto out_err;
1640                                                  1620 
1641         page = alloc_page(GFP_KERNEL);           1621         page = alloc_page(GFP_KERNEL);
1642         err = -ENOMEM;                           1622         err = -ENOMEM;
1643         if (!page)                               1623         if (!page)
1644                 goto out_err;                    1624                 goto out_err;
1645                                                  1625 
1646         err = fuse_readlink_page(inode, page)    1626         err = fuse_readlink_page(inode, page);
1647         if (err) {                               1627         if (err) {
1648                 __free_page(page);               1628                 __free_page(page);
1649                 goto out_err;                    1629                 goto out_err;
1650         }                                        1630         }
1651                                                  1631 
1652         set_delayed_call(callback, page_put_l    1632         set_delayed_call(callback, page_put_link, page);
1653                                                  1633 
1654         return page_address(page);               1634         return page_address(page);
1655                                                  1635 
1656 out_err:                                         1636 out_err:
1657         return ERR_PTR(err);                     1637         return ERR_PTR(err);
1658 }                                                1638 }
1659                                                  1639 
1660 static int fuse_dir_open(struct inode *inode,    1640 static int fuse_dir_open(struct inode *inode, struct file *file)
1661 {                                                1641 {
1662         struct fuse_mount *fm = get_fuse_moun    1642         struct fuse_mount *fm = get_fuse_mount(inode);
1663         int err;                                 1643         int err;
1664                                                  1644 
1665         if (fuse_is_bad(inode))                  1645         if (fuse_is_bad(inode))
1666                 return -EIO;                     1646                 return -EIO;
1667                                                  1647 
1668         err = generic_file_open(inode, file);    1648         err = generic_file_open(inode, file);
1669         if (err)                                 1649         if (err)
1670                 return err;                      1650                 return err;
1671                                                  1651 
1672         err = fuse_do_open(fm, get_node_id(in    1652         err = fuse_do_open(fm, get_node_id(inode), file, true);
1673         if (!err) {                              1653         if (!err) {
1674                 struct fuse_file *ff = file->    1654                 struct fuse_file *ff = file->private_data;
1675                                                  1655 
1676                 /*                               1656                 /*
1677                  * Keep handling FOPEN_STREAM    1657                  * Keep handling FOPEN_STREAM and FOPEN_NONSEEKABLE for
1678                  * directories for backward c    1658                  * directories for backward compatibility, though it's unlikely
1679                  * to be useful.                 1659                  * to be useful.
1680                  */                              1660                  */
1681                 if (ff->open_flags & (FOPEN_S    1661                 if (ff->open_flags & (FOPEN_STREAM | FOPEN_NONSEEKABLE))
1682                         nonseekable_open(inod    1662                         nonseekable_open(inode, file);
1683         }                                        1663         }
1684                                                  1664 
1685         return err;                              1665         return err;
1686 }                                                1666 }
1687                                                  1667 
1688 static int fuse_dir_release(struct inode *ino    1668 static int fuse_dir_release(struct inode *inode, struct file *file)
1689 {                                                1669 {
1690         fuse_release_common(file, true);         1670         fuse_release_common(file, true);
1691                                                  1671 
1692         return 0;                                1672         return 0;
1693 }                                                1673 }
1694                                                  1674 
1695 static int fuse_dir_fsync(struct file *file,     1675 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1696                           int datasync)          1676                           int datasync)
1697 {                                                1677 {
1698         struct inode *inode = file->f_mapping    1678         struct inode *inode = file->f_mapping->host;
1699         struct fuse_conn *fc = get_fuse_conn(    1679         struct fuse_conn *fc = get_fuse_conn(inode);
1700         int err;                                 1680         int err;
1701                                                  1681 
1702         if (fuse_is_bad(inode))                  1682         if (fuse_is_bad(inode))
1703                 return -EIO;                     1683                 return -EIO;
1704                                                  1684 
1705         if (fc->no_fsyncdir)                     1685         if (fc->no_fsyncdir)
1706                 return 0;                        1686                 return 0;
1707                                                  1687 
1708         inode_lock(inode);                       1688         inode_lock(inode);
1709         err = fuse_fsync_common(file, start,     1689         err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1710         if (err == -ENOSYS) {                    1690         if (err == -ENOSYS) {
1711                 fc->no_fsyncdir = 1;             1691                 fc->no_fsyncdir = 1;
1712                 err = 0;                         1692                 err = 0;
1713         }                                        1693         }
1714         inode_unlock(inode);                     1694         inode_unlock(inode);
1715                                                  1695 
1716         return err;                              1696         return err;
1717 }                                                1697 }
1718                                                  1698 
1719 static long fuse_dir_ioctl(struct file *file,    1699 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1720                             unsigned long arg    1700                             unsigned long arg)
1721 {                                                1701 {
1722         struct fuse_conn *fc = get_fuse_conn(    1702         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1723                                                  1703 
1724         /* FUSE_IOCTL_DIR only supported for     1704         /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1725         if (fc->minor < 18)                      1705         if (fc->minor < 18)
1726                 return -ENOTTY;                  1706                 return -ENOTTY;
1727                                                  1707 
1728         return fuse_ioctl_common(file, cmd, a    1708         return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1729 }                                                1709 }
1730                                                  1710 
1731 static long fuse_dir_compat_ioctl(struct file    1711 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1732                                    unsigned l    1712                                    unsigned long arg)
1733 {                                                1713 {
1734         struct fuse_conn *fc = get_fuse_conn(    1714         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1735                                                  1715 
1736         if (fc->minor < 18)                      1716         if (fc->minor < 18)
1737                 return -ENOTTY;                  1717                 return -ENOTTY;
1738                                                  1718 
1739         return fuse_ioctl_common(file, cmd, a    1719         return fuse_ioctl_common(file, cmd, arg,
1740                                  FUSE_IOCTL_C    1720                                  FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1741 }                                                1721 }
1742                                                  1722 
1743 static bool update_mtime(unsigned ivalid, boo    1723 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1744 {                                                1724 {
1745         /* Always update if mtime is explicit    1725         /* Always update if mtime is explicitly set  */
1746         if (ivalid & ATTR_MTIME_SET)             1726         if (ivalid & ATTR_MTIME_SET)
1747                 return true;                     1727                 return true;
1748                                                  1728 
1749         /* Or if kernel i_mtime is the offici    1729         /* Or if kernel i_mtime is the official one */
1750         if (trust_local_mtime)                   1730         if (trust_local_mtime)
1751                 return true;                     1731                 return true;
1752                                                  1732 
1753         /* If it's an open(O_TRUNC) or an ftr    1733         /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1754         if ((ivalid & ATTR_SIZE) && (ivalid &    1734         if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1755                 return false;                    1735                 return false;
1756                                                  1736 
1757         /* In all other cases update */          1737         /* In all other cases update */
1758         return true;                             1738         return true;
1759 }                                                1739 }
1760                                                  1740 
1761 static void iattr_to_fattr(struct mnt_idmap * !! 1741 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1762                            struct iattr *iatt !! 1742                            struct fuse_setattr_in *arg, bool trust_local_cmtime)
1763                            bool trust_local_c << 
1764 {                                                1743 {
1765         unsigned ivalid = iattr->ia_valid;       1744         unsigned ivalid = iattr->ia_valid;
1766                                                  1745 
1767         if (ivalid & ATTR_MODE)                  1746         if (ivalid & ATTR_MODE)
1768                 arg->valid |= FATTR_MODE,   a    1747                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1769                                               !! 1748         if (ivalid & ATTR_UID)
1770         if (ivalid & ATTR_UID) {              !! 1749                 arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1771                 kuid_t fsuid = from_vfsuid(id !! 1750         if (ivalid & ATTR_GID)
1772                                               !! 1751                 arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1773                 arg->valid |= FATTR_UID;      << 
1774                 arg->uid = from_kuid(fc->user << 
1775         }                                     << 
1776                                               << 
1777         if (ivalid & ATTR_GID) {              << 
1778                 kgid_t fsgid = from_vfsgid(id << 
1779                                               << 
1780                 arg->valid |= FATTR_GID;      << 
1781                 arg->gid = from_kgid(fc->user << 
1782         }                                     << 
1783                                               << 
1784         if (ivalid & ATTR_SIZE)                  1752         if (ivalid & ATTR_SIZE)
1785                 arg->valid |= FATTR_SIZE,   a    1753                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1786         if (ivalid & ATTR_ATIME) {               1754         if (ivalid & ATTR_ATIME) {
1787                 arg->valid |= FATTR_ATIME;       1755                 arg->valid |= FATTR_ATIME;
1788                 arg->atime = iattr->ia_atime.    1756                 arg->atime = iattr->ia_atime.tv_sec;
1789                 arg->atimensec = iattr->ia_at    1757                 arg->atimensec = iattr->ia_atime.tv_nsec;
1790                 if (!(ivalid & ATTR_ATIME_SET    1758                 if (!(ivalid & ATTR_ATIME_SET))
1791                         arg->valid |= FATTR_A    1759                         arg->valid |= FATTR_ATIME_NOW;
1792         }                                        1760         }
1793         if ((ivalid & ATTR_MTIME) && update_m    1761         if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1794                 arg->valid |= FATTR_MTIME;       1762                 arg->valid |= FATTR_MTIME;
1795                 arg->mtime = iattr->ia_mtime.    1763                 arg->mtime = iattr->ia_mtime.tv_sec;
1796                 arg->mtimensec = iattr->ia_mt    1764                 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1797                 if (!(ivalid & ATTR_MTIME_SET    1765                 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1798                         arg->valid |= FATTR_M    1766                         arg->valid |= FATTR_MTIME_NOW;
1799         }                                        1767         }
1800         if ((ivalid & ATTR_CTIME) && trust_lo    1768         if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1801                 arg->valid |= FATTR_CTIME;       1769                 arg->valid |= FATTR_CTIME;
1802                 arg->ctime = iattr->ia_ctime.    1770                 arg->ctime = iattr->ia_ctime.tv_sec;
1803                 arg->ctimensec = iattr->ia_ct    1771                 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1804         }                                        1772         }
1805 }                                                1773 }
1806                                                  1774 
1807 /*                                               1775 /*
1808  * Prevent concurrent writepages on inode        1776  * Prevent concurrent writepages on inode
1809  *                                               1777  *
1810  * This is done by adding a negative bias to     1778  * This is done by adding a negative bias to the inode write counter
1811  * and waiting for all pending writes to fini    1779  * and waiting for all pending writes to finish.
1812  */                                              1780  */
1813 void fuse_set_nowrite(struct inode *inode)       1781 void fuse_set_nowrite(struct inode *inode)
1814 {                                                1782 {
1815         struct fuse_inode *fi = get_fuse_inod    1783         struct fuse_inode *fi = get_fuse_inode(inode);
1816                                                  1784 
1817         BUG_ON(!inode_is_locked(inode));         1785         BUG_ON(!inode_is_locked(inode));
1818                                                  1786 
1819         spin_lock(&fi->lock);                    1787         spin_lock(&fi->lock);
1820         BUG_ON(fi->writectr < 0);                1788         BUG_ON(fi->writectr < 0);
1821         fi->writectr += FUSE_NOWRITE;            1789         fi->writectr += FUSE_NOWRITE;
1822         spin_unlock(&fi->lock);                  1790         spin_unlock(&fi->lock);
1823         wait_event(fi->page_waitq, fi->writec    1791         wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1824 }                                                1792 }
1825                                                  1793 
1826 /*                                               1794 /*
1827  * Allow writepages on inode                     1795  * Allow writepages on inode
1828  *                                               1796  *
1829  * Remove the bias from the writecounter and     1797  * Remove the bias from the writecounter and send any queued
1830  * writepages.                                   1798  * writepages.
1831  */                                              1799  */
1832 static void __fuse_release_nowrite(struct ino    1800 static void __fuse_release_nowrite(struct inode *inode)
1833 {                                                1801 {
1834         struct fuse_inode *fi = get_fuse_inod    1802         struct fuse_inode *fi = get_fuse_inode(inode);
1835                                                  1803 
1836         BUG_ON(fi->writectr != FUSE_NOWRITE);    1804         BUG_ON(fi->writectr != FUSE_NOWRITE);
1837         fi->writectr = 0;                        1805         fi->writectr = 0;
1838         fuse_flush_writepages(inode);            1806         fuse_flush_writepages(inode);
1839 }                                                1807 }
1840                                                  1808 
1841 void fuse_release_nowrite(struct inode *inode    1809 void fuse_release_nowrite(struct inode *inode)
1842 {                                                1810 {
1843         struct fuse_inode *fi = get_fuse_inod    1811         struct fuse_inode *fi = get_fuse_inode(inode);
1844                                                  1812 
1845         spin_lock(&fi->lock);                    1813         spin_lock(&fi->lock);
1846         __fuse_release_nowrite(inode);           1814         __fuse_release_nowrite(inode);
1847         spin_unlock(&fi->lock);                  1815         spin_unlock(&fi->lock);
1848 }                                                1816 }
1849                                                  1817 
1850 static void fuse_setattr_fill(struct fuse_con    1818 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1851                               struct inode *i    1819                               struct inode *inode,
1852                               struct fuse_set    1820                               struct fuse_setattr_in *inarg_p,
1853                               struct fuse_att    1821                               struct fuse_attr_out *outarg_p)
1854 {                                                1822 {
1855         args->opcode = FUSE_SETATTR;             1823         args->opcode = FUSE_SETATTR;
1856         args->nodeid = get_node_id(inode);       1824         args->nodeid = get_node_id(inode);
1857         args->in_numargs = 1;                    1825         args->in_numargs = 1;
1858         args->in_args[0].size = sizeof(*inarg    1826         args->in_args[0].size = sizeof(*inarg_p);
1859         args->in_args[0].value = inarg_p;        1827         args->in_args[0].value = inarg_p;
1860         args->out_numargs = 1;                   1828         args->out_numargs = 1;
1861         args->out_args[0].size = sizeof(*outa    1829         args->out_args[0].size = sizeof(*outarg_p);
1862         args->out_args[0].value = outarg_p;      1830         args->out_args[0].value = outarg_p;
1863 }                                                1831 }
1864                                                  1832 
1865 /*                                               1833 /*
1866  * Flush inode->i_mtime to the server            1834  * Flush inode->i_mtime to the server
1867  */                                              1835  */
1868 int fuse_flush_times(struct inode *inode, str    1836 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1869 {                                                1837 {
1870         struct fuse_mount *fm = get_fuse_moun    1838         struct fuse_mount *fm = get_fuse_mount(inode);
1871         FUSE_ARGS(args);                         1839         FUSE_ARGS(args);
1872         struct fuse_setattr_in inarg;            1840         struct fuse_setattr_in inarg;
1873         struct fuse_attr_out outarg;             1841         struct fuse_attr_out outarg;
1874                                                  1842 
1875         memset(&inarg, 0, sizeof(inarg));        1843         memset(&inarg, 0, sizeof(inarg));
1876         memset(&outarg, 0, sizeof(outarg));      1844         memset(&outarg, 0, sizeof(outarg));
1877                                                  1845 
1878         inarg.valid = FATTR_MTIME;               1846         inarg.valid = FATTR_MTIME;
1879         inarg.mtime = inode_get_mtime_sec(ino    1847         inarg.mtime = inode_get_mtime_sec(inode);
1880         inarg.mtimensec = inode_get_mtime_nse    1848         inarg.mtimensec = inode_get_mtime_nsec(inode);
1881         if (fm->fc->minor >= 23) {               1849         if (fm->fc->minor >= 23) {
1882                 inarg.valid |= FATTR_CTIME;      1850                 inarg.valid |= FATTR_CTIME;
1883                 inarg.ctime = inode_get_ctime    1851                 inarg.ctime = inode_get_ctime_sec(inode);
1884                 inarg.ctimensec = inode_get_c    1852                 inarg.ctimensec = inode_get_ctime_nsec(inode);
1885         }                                        1853         }
1886         if (ff) {                                1854         if (ff) {
1887                 inarg.valid |= FATTR_FH;         1855                 inarg.valid |= FATTR_FH;
1888                 inarg.fh = ff->fh;               1856                 inarg.fh = ff->fh;
1889         }                                        1857         }
1890         fuse_setattr_fill(fm->fc, &args, inod    1858         fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1891                                                  1859 
1892         return fuse_simple_request(fm, &args)    1860         return fuse_simple_request(fm, &args);
1893 }                                                1861 }
1894                                                  1862 
1895 /*                                               1863 /*
1896  * Set attributes, and at the same time refre    1864  * Set attributes, and at the same time refresh them.
1897  *                                               1865  *
1898  * Truncation is slightly complicated, becaus    1866  * Truncation is slightly complicated, because the 'truncate' request
1899  * may fail, in which case we don't want to t    1867  * may fail, in which case we don't want to touch the mapping.
1900  * vmtruncate() doesn't allow for this case,     1868  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1901  * and the actual truncation by hand.            1869  * and the actual truncation by hand.
1902  */                                              1870  */
1903 int fuse_do_setattr(struct mnt_idmap *idmap,  !! 1871 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1904                     struct iattr *attr, struc !! 1872                     struct file *file)
1905 {                                                1873 {
1906         struct inode *inode = d_inode(dentry)    1874         struct inode *inode = d_inode(dentry);
1907         struct fuse_mount *fm = get_fuse_moun    1875         struct fuse_mount *fm = get_fuse_mount(inode);
1908         struct fuse_conn *fc = fm->fc;           1876         struct fuse_conn *fc = fm->fc;
1909         struct fuse_inode *fi = get_fuse_inod    1877         struct fuse_inode *fi = get_fuse_inode(inode);
1910         struct address_space *mapping = inode    1878         struct address_space *mapping = inode->i_mapping;
1911         FUSE_ARGS(args);                         1879         FUSE_ARGS(args);
1912         struct fuse_setattr_in inarg;            1880         struct fuse_setattr_in inarg;
1913         struct fuse_attr_out outarg;             1881         struct fuse_attr_out outarg;
1914         bool is_truncate = false;                1882         bool is_truncate = false;
1915         bool is_wb = fc->writeback_cache && S    1883         bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode);
1916         loff_t oldsize;                          1884         loff_t oldsize;
1917         int err;                                 1885         int err;
1918         bool trust_local_cmtime = is_wb;         1886         bool trust_local_cmtime = is_wb;
1919         bool fault_blocked = false;              1887         bool fault_blocked = false;
1920                                                  1888 
1921         if (!fc->default_permissions)            1889         if (!fc->default_permissions)
1922                 attr->ia_valid |= ATTR_FORCE;    1890                 attr->ia_valid |= ATTR_FORCE;
1923                                                  1891 
1924         err = setattr_prepare(idmap, dentry,  !! 1892         err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
1925         if (err)                                 1893         if (err)
1926                 return err;                      1894                 return err;
1927                                                  1895 
1928         if (attr->ia_valid & ATTR_SIZE) {        1896         if (attr->ia_valid & ATTR_SIZE) {
1929                 if (WARN_ON(!S_ISREG(inode->i    1897                 if (WARN_ON(!S_ISREG(inode->i_mode)))
1930                         return -EIO;             1898                         return -EIO;
1931                 is_truncate = true;              1899                 is_truncate = true;
1932         }                                        1900         }
1933                                                  1901 
1934         if (FUSE_IS_DAX(inode) && is_truncate    1902         if (FUSE_IS_DAX(inode) && is_truncate) {
1935                 filemap_invalidate_lock(mappi    1903                 filemap_invalidate_lock(mapping);
1936                 fault_blocked = true;            1904                 fault_blocked = true;
1937                 err = fuse_dax_break_layouts(    1905                 err = fuse_dax_break_layouts(inode, 0, 0);
1938                 if (err) {                       1906                 if (err) {
1939                         filemap_invalidate_un    1907                         filemap_invalidate_unlock(mapping);
1940                         return err;              1908                         return err;
1941                 }                                1909                 }
1942         }                                        1910         }
1943                                                  1911 
1944         if (attr->ia_valid & ATTR_OPEN) {        1912         if (attr->ia_valid & ATTR_OPEN) {
1945                 /* This is coming from open(.    1913                 /* This is coming from open(..., ... | O_TRUNC); */
1946                 WARN_ON(!(attr->ia_valid & AT    1914                 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1947                 WARN_ON(attr->ia_size != 0);     1915                 WARN_ON(attr->ia_size != 0);
1948                 if (fc->atomic_o_trunc) {        1916                 if (fc->atomic_o_trunc) {
1949                         /*                       1917                         /*
1950                          * No need to send re    1918                          * No need to send request to userspace, since actual
1951                          * truncation has alr    1919                          * truncation has already been done by OPEN.  But still
1952                          * need to truncate p    1920                          * need to truncate page cache.
1953                          */                      1921                          */
1954                         i_size_write(inode, 0    1922                         i_size_write(inode, 0);
1955                         truncate_pagecache(in    1923                         truncate_pagecache(inode, 0);
1956                         goto out;                1924                         goto out;
1957                 }                                1925                 }
1958                 file = NULL;                     1926                 file = NULL;
1959         }                                        1927         }
1960                                                  1928 
1961         /* Flush dirty data/metadata before n    1929         /* Flush dirty data/metadata before non-truncate SETATTR */
1962         if (is_wb &&                             1930         if (is_wb &&
1963             attr->ia_valid &                     1931             attr->ia_valid &
1964                         (ATTR_MODE | ATTR_UID    1932                         (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1965                          ATTR_TIMES_SET)) {      1933                          ATTR_TIMES_SET)) {
1966                 err = write_inode_now(inode,     1934                 err = write_inode_now(inode, true);
1967                 if (err)                         1935                 if (err)
1968                         return err;              1936                         return err;
1969                                                  1937 
1970                 fuse_set_nowrite(inode);         1938                 fuse_set_nowrite(inode);
1971                 fuse_release_nowrite(inode);     1939                 fuse_release_nowrite(inode);
1972         }                                        1940         }
1973                                                  1941 
1974         if (is_truncate) {                       1942         if (is_truncate) {
1975                 fuse_set_nowrite(inode);         1943                 fuse_set_nowrite(inode);
1976                 set_bit(FUSE_I_SIZE_UNSTABLE,    1944                 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1977                 if (trust_local_cmtime && att    1945                 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1978                         attr->ia_valid |= ATT    1946                         attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1979         }                                        1947         }
1980                                                  1948 
1981         memset(&inarg, 0, sizeof(inarg));        1949         memset(&inarg, 0, sizeof(inarg));
1982         memset(&outarg, 0, sizeof(outarg));      1950         memset(&outarg, 0, sizeof(outarg));
1983         iattr_to_fattr(idmap, fc, attr, &inar !! 1951         iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1984         if (file) {                              1952         if (file) {
1985                 struct fuse_file *ff = file->    1953                 struct fuse_file *ff = file->private_data;
1986                 inarg.valid |= FATTR_FH;         1954                 inarg.valid |= FATTR_FH;
1987                 inarg.fh = ff->fh;               1955                 inarg.fh = ff->fh;
1988         }                                        1956         }
1989                                                  1957 
1990         /* Kill suid/sgid for non-directory c    1958         /* Kill suid/sgid for non-directory chown unconditionally */
1991         if (fc->handle_killpriv_v2 && !S_ISDI    1959         if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
1992             attr->ia_valid & (ATTR_UID | ATTR    1960             attr->ia_valid & (ATTR_UID | ATTR_GID))
1993                 inarg.valid |= FATTR_KILL_SUI    1961                 inarg.valid |= FATTR_KILL_SUIDGID;
1994                                                  1962 
1995         if (attr->ia_valid & ATTR_SIZE) {        1963         if (attr->ia_valid & ATTR_SIZE) {
1996                 /* For mandatory locking in t    1964                 /* For mandatory locking in truncate */
1997                 inarg.valid |= FATTR_LOCKOWNE    1965                 inarg.valid |= FATTR_LOCKOWNER;
1998                 inarg.lock_owner = fuse_lock_    1966                 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1999                                                  1967 
2000                 /* Kill suid/sgid for truncat    1968                 /* Kill suid/sgid for truncate only if no CAP_FSETID */
2001                 if (fc->handle_killpriv_v2 &&    1969                 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
2002                         inarg.valid |= FATTR_    1970                         inarg.valid |= FATTR_KILL_SUIDGID;
2003         }                                        1971         }
2004         fuse_setattr_fill(fc, &args, inode, &    1972         fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
2005         err = fuse_simple_request(fm, &args);    1973         err = fuse_simple_request(fm, &args);
2006         if (err) {                               1974         if (err) {
2007                 if (err == -EINTR)               1975                 if (err == -EINTR)
2008                         fuse_invalidate_attr(    1976                         fuse_invalidate_attr(inode);
2009                 goto error;                      1977                 goto error;
2010         }                                        1978         }
2011                                                  1979 
2012         if (fuse_invalid_attr(&outarg.attr) |    1980         if (fuse_invalid_attr(&outarg.attr) ||
2013             inode_wrong_type(inode, outarg.at    1981             inode_wrong_type(inode, outarg.attr.mode)) {
2014                 fuse_make_bad(inode);            1982                 fuse_make_bad(inode);
2015                 err = -EIO;                      1983                 err = -EIO;
2016                 goto error;                      1984                 goto error;
2017         }                                        1985         }
2018                                                  1986 
2019         spin_lock(&fi->lock);                    1987         spin_lock(&fi->lock);
2020         /* the kernel maintains i_mtime local    1988         /* the kernel maintains i_mtime locally */
2021         if (trust_local_cmtime) {                1989         if (trust_local_cmtime) {
2022                 if (attr->ia_valid & ATTR_MTI    1990                 if (attr->ia_valid & ATTR_MTIME)
2023                         inode_set_mtime_to_ts    1991                         inode_set_mtime_to_ts(inode, attr->ia_mtime);
2024                 if (attr->ia_valid & ATTR_CTI    1992                 if (attr->ia_valid & ATTR_CTIME)
2025                         inode_set_ctime_to_ts    1993                         inode_set_ctime_to_ts(inode, attr->ia_ctime);
2026                 /* FIXME: clear I_DIRTY_SYNC?    1994                 /* FIXME: clear I_DIRTY_SYNC? */
2027         }                                        1995         }
2028                                                  1996 
2029         fuse_change_attributes_common(inode,     1997         fuse_change_attributes_common(inode, &outarg.attr, NULL,
2030                                       ATTR_TI    1998                                       ATTR_TIMEOUT(&outarg),
2031                                       fuse_ge    1999                                       fuse_get_cache_mask(inode));
2032         oldsize = inode->i_size;                 2000         oldsize = inode->i_size;
2033         /* see the comment in fuse_change_att    2001         /* see the comment in fuse_change_attributes() */
2034         if (!is_wb || is_truncate)               2002         if (!is_wb || is_truncate)
2035                 i_size_write(inode, outarg.at    2003                 i_size_write(inode, outarg.attr.size);
2036                                                  2004 
2037         if (is_truncate) {                       2005         if (is_truncate) {
2038                 /* NOTE: this may release/rea    2006                 /* NOTE: this may release/reacquire fi->lock */
2039                 __fuse_release_nowrite(inode)    2007                 __fuse_release_nowrite(inode);
2040         }                                        2008         }
2041         spin_unlock(&fi->lock);                  2009         spin_unlock(&fi->lock);
2042                                                  2010 
2043         /*                                       2011         /*
2044          * Only call invalidate_inode_pages2(    2012          * Only call invalidate_inode_pages2() after removing
2045          * FUSE_NOWRITE, otherwise fuse_laund    2013          * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock.
2046          */                                      2014          */
2047         if ((is_truncate || !is_wb) &&           2015         if ((is_truncate || !is_wb) &&
2048             S_ISREG(inode->i_mode) && oldsize    2016             S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
2049                 truncate_pagecache(inode, out    2017                 truncate_pagecache(inode, outarg.attr.size);
2050                 invalidate_inode_pages2(mappi    2018                 invalidate_inode_pages2(mapping);
2051         }                                        2019         }
2052                                                  2020 
2053         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->    2021         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2054 out:                                             2022 out:
2055         if (fault_blocked)                       2023         if (fault_blocked)
2056                 filemap_invalidate_unlock(map    2024                 filemap_invalidate_unlock(mapping);
2057                                                  2025 
2058         return 0;                                2026         return 0;
2059                                                  2027 
2060 error:                                           2028 error:
2061         if (is_truncate)                         2029         if (is_truncate)
2062                 fuse_release_nowrite(inode);     2030                 fuse_release_nowrite(inode);
2063                                                  2031 
2064         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->    2032         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2065                                                  2033 
2066         if (fault_blocked)                       2034         if (fault_blocked)
2067                 filemap_invalidate_unlock(map    2035                 filemap_invalidate_unlock(mapping);
2068         return err;                              2036         return err;
2069 }                                                2037 }
2070                                                  2038 
2071 static int fuse_setattr(struct mnt_idmap *idm    2039 static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,
2072                         struct iattr *attr)      2040                         struct iattr *attr)
2073 {                                                2041 {
2074         struct inode *inode = d_inode(entry);    2042         struct inode *inode = d_inode(entry);
2075         struct fuse_conn *fc = get_fuse_conn(    2043         struct fuse_conn *fc = get_fuse_conn(inode);
2076         struct file *file = (attr->ia_valid &    2044         struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
2077         int ret;                                 2045         int ret;
2078                                                  2046 
2079         if (fuse_is_bad(inode))                  2047         if (fuse_is_bad(inode))
2080                 return -EIO;                     2048                 return -EIO;
2081                                                  2049 
2082         if (!fuse_allow_current_process(get_f    2050         if (!fuse_allow_current_process(get_fuse_conn(inode)))
2083                 return -EACCES;                  2051                 return -EACCES;
2084                                                  2052 
2085         if (attr->ia_valid & (ATTR_KILL_SUID     2053         if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
2086                 attr->ia_valid &= ~(ATTR_KILL    2054                 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
2087                                     ATTR_MODE    2055                                     ATTR_MODE);
2088                                                  2056 
2089                 /*                               2057                 /*
2090                  * The only sane way to relia    2058                  * The only sane way to reliably kill suid/sgid is to do it in
2091                  * the userspace filesystem      2059                  * the userspace filesystem
2092                  *                               2060                  *
2093                  * This should be done on wri    2061                  * This should be done on write(), truncate() and chown().
2094                  */                              2062                  */
2095                 if (!fc->handle_killpriv && !    2063                 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
2096                         /*                       2064                         /*
2097                          * ia_mode calculatio    2065                          * ia_mode calculation may have used stale i_mode.
2098                          * Refresh and recalc    2066                          * Refresh and recalculate.
2099                          */                      2067                          */
2100                         ret = fuse_do_getattr !! 2068                         ret = fuse_do_getattr(inode, NULL, file);
2101                         if (ret)                 2069                         if (ret)
2102                                 return ret;      2070                                 return ret;
2103                                                  2071 
2104                         attr->ia_mode = inode    2072                         attr->ia_mode = inode->i_mode;
2105                         if (inode->i_mode & S    2073                         if (inode->i_mode & S_ISUID) {
2106                                 attr->ia_vali    2074                                 attr->ia_valid |= ATTR_MODE;
2107                                 attr->ia_mode    2075                                 attr->ia_mode &= ~S_ISUID;
2108                         }                        2076                         }
2109                         if ((inode->i_mode &     2077                         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2110                                 attr->ia_vali    2078                                 attr->ia_valid |= ATTR_MODE;
2111                                 attr->ia_mode    2079                                 attr->ia_mode &= ~S_ISGID;
2112                         }                        2080                         }
2113                 }                                2081                 }
2114         }                                        2082         }
2115         if (!attr->ia_valid)                     2083         if (!attr->ia_valid)
2116                 return 0;                        2084                 return 0;
2117                                                  2085 
2118         ret = fuse_do_setattr(idmap, entry, a !! 2086         ret = fuse_do_setattr(entry, attr, file);
2119         if (!ret) {                              2087         if (!ret) {
2120                 /*                               2088                 /*
2121                  * If filesystem supports acl    2089                  * If filesystem supports acls it may have updated acl xattrs in
2122                  * the filesystem, so forget     2090                  * the filesystem, so forget cached acls for the inode.
2123                  */                              2091                  */
2124                 if (fc->posix_acl)               2092                 if (fc->posix_acl)
2125                         forget_all_cached_acl    2093                         forget_all_cached_acls(inode);
2126                                                  2094 
2127                 /* Directory mode changed, ma    2095                 /* Directory mode changed, may need to revalidate access */
2128                 if (d_is_dir(entry) && (attr-    2096                 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
2129                         fuse_invalidate_entry    2097                         fuse_invalidate_entry_cache(entry);
2130         }                                        2098         }
2131         return ret;                              2099         return ret;
2132 }                                                2100 }
2133                                                  2101 
2134 static int fuse_getattr(struct mnt_idmap *idm    2102 static int fuse_getattr(struct mnt_idmap *idmap,
2135                         const struct path *pa    2103                         const struct path *path, struct kstat *stat,
2136                         u32 request_mask, uns    2104                         u32 request_mask, unsigned int flags)
2137 {                                                2105 {
2138         struct inode *inode = d_inode(path->d    2106         struct inode *inode = d_inode(path->dentry);
2139         struct fuse_conn *fc = get_fuse_conn(    2107         struct fuse_conn *fc = get_fuse_conn(inode);
2140                                                  2108 
2141         if (fuse_is_bad(inode))                  2109         if (fuse_is_bad(inode))
2142                 return -EIO;                     2110                 return -EIO;
2143                                                  2111 
2144         if (!fuse_allow_current_process(fc))     2112         if (!fuse_allow_current_process(fc)) {
2145                 if (!request_mask) {             2113                 if (!request_mask) {
2146                         /*                       2114                         /*
2147                          * If user explicitly    2115                          * If user explicitly requested *nothing* then don't
2148                          * error out, but ret    2116                          * error out, but return st_dev only.
2149                          */                      2117                          */
2150                         stat->result_mask = 0    2118                         stat->result_mask = 0;
2151                         stat->dev = inode->i_    2119                         stat->dev = inode->i_sb->s_dev;
2152                         return 0;                2120                         return 0;
2153                 }                                2121                 }
2154                 return -EACCES;                  2122                 return -EACCES;
2155         }                                        2123         }
2156                                                  2124 
2157         return fuse_update_get_attr(idmap, in !! 2125         return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
2158 }                                                2126 }
2159                                                  2127 
2160 static const struct inode_operations fuse_dir    2128 static const struct inode_operations fuse_dir_inode_operations = {
2161         .lookup         = fuse_lookup,           2129         .lookup         = fuse_lookup,
2162         .mkdir          = fuse_mkdir,            2130         .mkdir          = fuse_mkdir,
2163         .symlink        = fuse_symlink,          2131         .symlink        = fuse_symlink,
2164         .unlink         = fuse_unlink,           2132         .unlink         = fuse_unlink,
2165         .rmdir          = fuse_rmdir,            2133         .rmdir          = fuse_rmdir,
2166         .rename         = fuse_rename2,          2134         .rename         = fuse_rename2,
2167         .link           = fuse_link,             2135         .link           = fuse_link,
2168         .setattr        = fuse_setattr,          2136         .setattr        = fuse_setattr,
2169         .create         = fuse_create,           2137         .create         = fuse_create,
2170         .atomic_open    = fuse_atomic_open,      2138         .atomic_open    = fuse_atomic_open,
2171         .tmpfile        = fuse_tmpfile,          2139         .tmpfile        = fuse_tmpfile,
2172         .mknod          = fuse_mknod,            2140         .mknod          = fuse_mknod,
2173         .permission     = fuse_permission,       2141         .permission     = fuse_permission,
2174         .getattr        = fuse_getattr,          2142         .getattr        = fuse_getattr,
2175         .listxattr      = fuse_listxattr,        2143         .listxattr      = fuse_listxattr,
2176         .get_inode_acl  = fuse_get_inode_acl,    2144         .get_inode_acl  = fuse_get_inode_acl,
2177         .get_acl        = fuse_get_acl,          2145         .get_acl        = fuse_get_acl,
2178         .set_acl        = fuse_set_acl,          2146         .set_acl        = fuse_set_acl,
2179         .fileattr_get   = fuse_fileattr_get,     2147         .fileattr_get   = fuse_fileattr_get,
2180         .fileattr_set   = fuse_fileattr_set,     2148         .fileattr_set   = fuse_fileattr_set,
2181 };                                               2149 };
2182                                                  2150 
2183 static const struct file_operations fuse_dir_    2151 static const struct file_operations fuse_dir_operations = {
2184         .llseek         = generic_file_llseek    2152         .llseek         = generic_file_llseek,
2185         .read           = generic_read_dir,      2153         .read           = generic_read_dir,
2186         .iterate_shared = fuse_readdir,          2154         .iterate_shared = fuse_readdir,
2187         .open           = fuse_dir_open,         2155         .open           = fuse_dir_open,
2188         .release        = fuse_dir_release,      2156         .release        = fuse_dir_release,
2189         .fsync          = fuse_dir_fsync,        2157         .fsync          = fuse_dir_fsync,
2190         .unlocked_ioctl = fuse_dir_ioctl,        2158         .unlocked_ioctl = fuse_dir_ioctl,
2191         .compat_ioctl   = fuse_dir_compat_ioc    2159         .compat_ioctl   = fuse_dir_compat_ioctl,
2192 };                                               2160 };
2193                                                  2161 
2194 static const struct inode_operations fuse_com    2162 static const struct inode_operations fuse_common_inode_operations = {
2195         .setattr        = fuse_setattr,          2163         .setattr        = fuse_setattr,
2196         .permission     = fuse_permission,       2164         .permission     = fuse_permission,
2197         .getattr        = fuse_getattr,          2165         .getattr        = fuse_getattr,
2198         .listxattr      = fuse_listxattr,        2166         .listxattr      = fuse_listxattr,
2199         .get_inode_acl  = fuse_get_inode_acl,    2167         .get_inode_acl  = fuse_get_inode_acl,
2200         .get_acl        = fuse_get_acl,          2168         .get_acl        = fuse_get_acl,
2201         .set_acl        = fuse_set_acl,          2169         .set_acl        = fuse_set_acl,
2202         .fileattr_get   = fuse_fileattr_get,     2170         .fileattr_get   = fuse_fileattr_get,
2203         .fileattr_set   = fuse_fileattr_set,     2171         .fileattr_set   = fuse_fileattr_set,
2204 };                                               2172 };
2205                                                  2173 
2206 static const struct inode_operations fuse_sym    2174 static const struct inode_operations fuse_symlink_inode_operations = {
2207         .setattr        = fuse_setattr,          2175         .setattr        = fuse_setattr,
2208         .get_link       = fuse_get_link,         2176         .get_link       = fuse_get_link,
2209         .getattr        = fuse_getattr,          2177         .getattr        = fuse_getattr,
2210         .listxattr      = fuse_listxattr,        2178         .listxattr      = fuse_listxattr,
2211 };                                               2179 };
2212                                                  2180 
2213 void fuse_init_common(struct inode *inode)       2181 void fuse_init_common(struct inode *inode)
2214 {                                                2182 {
2215         inode->i_op = &fuse_common_inode_oper    2183         inode->i_op = &fuse_common_inode_operations;
2216 }                                                2184 }
2217                                                  2185 
2218 void fuse_init_dir(struct inode *inode)          2186 void fuse_init_dir(struct inode *inode)
2219 {                                                2187 {
2220         struct fuse_inode *fi = get_fuse_inod    2188         struct fuse_inode *fi = get_fuse_inode(inode);
2221                                                  2189 
2222         inode->i_op = &fuse_dir_inode_operati    2190         inode->i_op = &fuse_dir_inode_operations;
2223         inode->i_fop = &fuse_dir_operations;     2191         inode->i_fop = &fuse_dir_operations;
2224                                                  2192 
2225         spin_lock_init(&fi->rdc.lock);           2193         spin_lock_init(&fi->rdc.lock);
2226         fi->rdc.cached = false;                  2194         fi->rdc.cached = false;
2227         fi->rdc.size = 0;                        2195         fi->rdc.size = 0;
2228         fi->rdc.pos = 0;                         2196         fi->rdc.pos = 0;
2229         fi->rdc.version = 0;                     2197         fi->rdc.version = 0;
2230 }                                                2198 }
2231                                                  2199 
2232 static int fuse_symlink_read_folio(struct fil    2200 static int fuse_symlink_read_folio(struct file *null, struct folio *folio)
2233 {                                                2201 {
2234         int err = fuse_readlink_page(folio->m    2202         int err = fuse_readlink_page(folio->mapping->host, &folio->page);
2235                                                  2203 
2236         if (!err)                                2204         if (!err)
2237                 folio_mark_uptodate(folio);      2205                 folio_mark_uptodate(folio);
2238                                                  2206 
2239         folio_unlock(folio);                     2207         folio_unlock(folio);
2240                                                  2208 
2241         return err;                              2209         return err;
2242 }                                                2210 }
2243                                                  2211 
2244 static const struct address_space_operations     2212 static const struct address_space_operations fuse_symlink_aops = {
2245         .read_folio     = fuse_symlink_read_f    2213         .read_folio     = fuse_symlink_read_folio,
2246 };                                               2214 };
2247                                                  2215 
2248 void fuse_init_symlink(struct inode *inode)      2216 void fuse_init_symlink(struct inode *inode)
2249 {                                                2217 {
2250         inode->i_op = &fuse_symlink_inode_ope    2218         inode->i_op = &fuse_symlink_inode_operations;
2251         inode->i_data.a_ops = &fuse_symlink_a    2219         inode->i_data.a_ops = &fuse_symlink_aops;
2252         inode_nohighmem(inode);                  2220         inode_nohighmem(inode);
2253 }                                                2221 }
2254                                                  2222 

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