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