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