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