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