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