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