GNU Linux-libre 4.9.337-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 = fuse_file_get(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 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
956                            struct file *file, bool *refreshed)
957 {
958         struct fuse_inode *fi = get_fuse_inode(inode);
959         int err;
960         bool r;
961
962         if (time_before64(fi->i_time, get_jiffies_64())) {
963                 r = true;
964                 forget_all_cached_acls(inode);
965                 err = fuse_do_getattr(inode, stat, file);
966         } else {
967                 r = false;
968                 err = 0;
969                 if (stat) {
970                         generic_fillattr(inode, stat);
971                         stat->mode = fi->orig_i_mode;
972                         stat->ino = fi->orig_ino;
973                 }
974         }
975
976         if (refreshed != NULL)
977                 *refreshed = r;
978
979         return err;
980 }
981
982 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
983                              u64 child_nodeid, struct qstr *name)
984 {
985         int err = -ENOTDIR;
986         struct inode *parent;
987         struct dentry *dir;
988         struct dentry *entry;
989
990         parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
991         if (!parent)
992                 return -ENOENT;
993
994         inode_lock_nested(parent, I_MUTEX_PARENT);
995         if (!S_ISDIR(parent->i_mode))
996                 goto unlock;
997
998         err = -ENOENT;
999         dir = d_find_alias(parent);
1000         if (!dir)
1001                 goto unlock;
1002
1003         name->hash = full_name_hash(dir, name->name, name->len);
1004         entry = d_lookup(dir, name);
1005         dput(dir);
1006         if (!entry)
1007                 goto unlock;
1008
1009         fuse_invalidate_attr(parent);
1010         fuse_invalidate_entry(entry);
1011
1012         if (child_nodeid != 0 && d_really_is_positive(entry)) {
1013                 inode_lock(d_inode(entry));
1014                 if (get_node_id(d_inode(entry)) != child_nodeid) {
1015                         err = -ENOENT;
1016                         goto badentry;
1017                 }
1018                 if (d_mountpoint(entry)) {
1019                         err = -EBUSY;
1020                         goto badentry;
1021                 }
1022                 if (d_is_dir(entry)) {
1023                         shrink_dcache_parent(entry);
1024                         if (!simple_empty(entry)) {
1025                                 err = -ENOTEMPTY;
1026                                 goto badentry;
1027                         }
1028                         d_inode(entry)->i_flags |= S_DEAD;
1029                 }
1030                 dont_mount(entry);
1031                 clear_nlink(d_inode(entry));
1032                 err = 0;
1033  badentry:
1034                 inode_unlock(d_inode(entry));
1035                 if (!err)
1036                         d_delete(entry);
1037         } else {
1038                 err = 0;
1039         }
1040         dput(entry);
1041
1042  unlock:
1043         inode_unlock(parent);
1044         iput(parent);
1045         return err;
1046 }
1047
1048 /*
1049  * Calling into a user-controlled filesystem gives the filesystem
1050  * daemon ptrace-like capabilities over the current process.  This
1051  * means, that the filesystem daemon is able to record the exact
1052  * filesystem operations performed, and can also control the behavior
1053  * of the requester process in otherwise impossible ways.  For example
1054  * it can delay the operation for arbitrary length of time allowing
1055  * DoS against the requester.
1056  *
1057  * For this reason only those processes can call into the filesystem,
1058  * for which the owner of the mount has ptrace privilege.  This
1059  * excludes processes started by other users, suid or sgid processes.
1060  */
1061 int fuse_allow_current_process(struct fuse_conn *fc)
1062 {
1063         const struct cred *cred;
1064
1065         if (fc->allow_other)
1066                 return 1;
1067
1068         cred = current_cred();
1069         if (uid_eq(cred->euid, fc->user_id) &&
1070             uid_eq(cred->suid, fc->user_id) &&
1071             uid_eq(cred->uid,  fc->user_id) &&
1072             gid_eq(cred->egid, fc->group_id) &&
1073             gid_eq(cred->sgid, fc->group_id) &&
1074             gid_eq(cred->gid,  fc->group_id))
1075                 return 1;
1076
1077         return 0;
1078 }
1079
1080 static int fuse_access(struct inode *inode, int mask)
1081 {
1082         struct fuse_conn *fc = get_fuse_conn(inode);
1083         FUSE_ARGS(args);
1084         struct fuse_access_in inarg;
1085         int err;
1086
1087         BUG_ON(mask & MAY_NOT_BLOCK);
1088
1089         if (fc->no_access)
1090                 return 0;
1091
1092         memset(&inarg, 0, sizeof(inarg));
1093         inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1094         args.in.h.opcode = FUSE_ACCESS;
1095         args.in.h.nodeid = get_node_id(inode);
1096         args.in.numargs = 1;
1097         args.in.args[0].size = sizeof(inarg);
1098         args.in.args[0].value = &inarg;
1099         err = fuse_simple_request(fc, &args);
1100         if (err == -ENOSYS) {
1101                 fc->no_access = 1;
1102                 err = 0;
1103         }
1104         return err;
1105 }
1106
1107 static int fuse_perm_getattr(struct inode *inode, int mask)
1108 {
1109         if (mask & MAY_NOT_BLOCK)
1110                 return -ECHILD;
1111
1112         forget_all_cached_acls(inode);
1113         return fuse_do_getattr(inode, NULL, NULL);
1114 }
1115
1116 /*
1117  * Check permission.  The two basic access models of FUSE are:
1118  *
1119  * 1) Local access checking ('default_permissions' mount option) based
1120  * on file mode.  This is the plain old disk filesystem permission
1121  * modell.
1122  *
1123  * 2) "Remote" access checking, where server is responsible for
1124  * checking permission in each inode operation.  An exception to this
1125  * is if ->permission() was invoked from sys_access() in which case an
1126  * access request is sent.  Execute permission is still checked
1127  * locally based on file mode.
1128  */
1129 static int fuse_permission(struct inode *inode, int mask)
1130 {
1131         struct fuse_conn *fc = get_fuse_conn(inode);
1132         bool refreshed = false;
1133         int err = 0;
1134
1135         if (fuse_is_bad(inode))
1136                 return -EIO;
1137
1138         if (!fuse_allow_current_process(fc))
1139                 return -EACCES;
1140
1141         /*
1142          * If attributes are needed, refresh them before proceeding
1143          */
1144         if (fc->default_permissions ||
1145             ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1146                 struct fuse_inode *fi = get_fuse_inode(inode);
1147
1148                 if (time_before64(fi->i_time, get_jiffies_64())) {
1149                         refreshed = true;
1150
1151                         err = fuse_perm_getattr(inode, mask);
1152                         if (err)
1153                                 return err;
1154                 }
1155         }
1156
1157         if (fc->default_permissions) {
1158                 err = generic_permission(inode, mask);
1159
1160                 /* If permission is denied, try to refresh file
1161                    attributes.  This is also needed, because the root
1162                    node will at first have no permissions */
1163                 if (err == -EACCES && !refreshed) {
1164                         err = fuse_perm_getattr(inode, mask);
1165                         if (!err)
1166                                 err = generic_permission(inode, mask);
1167                 }
1168
1169                 /* Note: the opposite of the above test does not
1170                    exist.  So if permissions are revoked this won't be
1171                    noticed immediately, only after the attribute
1172                    timeout has expired */
1173         } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1174                 err = fuse_access(inode, mask);
1175         } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1176                 if (!(inode->i_mode & S_IXUGO)) {
1177                         if (refreshed)
1178                                 return -EACCES;
1179
1180                         err = fuse_perm_getattr(inode, mask);
1181                         if (!err && !(inode->i_mode & S_IXUGO))
1182                                 return -EACCES;
1183                 }
1184         }
1185         return err;
1186 }
1187
1188 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1189                          struct dir_context *ctx)
1190 {
1191         while (nbytes >= FUSE_NAME_OFFSET) {
1192                 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1193                 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1194                 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1195                         return -EIO;
1196                 if (reclen > nbytes)
1197                         break;
1198                 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1199                         return -EIO;
1200
1201                 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1202                                dirent->ino, dirent->type))
1203                         break;
1204
1205                 buf += reclen;
1206                 nbytes -= reclen;
1207                 ctx->pos = dirent->off;
1208         }
1209
1210         return 0;
1211 }
1212
1213 static int fuse_direntplus_link(struct file *file,
1214                                 struct fuse_direntplus *direntplus,
1215                                 u64 attr_version)
1216 {
1217         struct fuse_entry_out *o = &direntplus->entry_out;
1218         struct fuse_dirent *dirent = &direntplus->dirent;
1219         struct dentry *parent = file->f_path.dentry;
1220         struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1221         struct dentry *dentry;
1222         struct dentry *alias;
1223         struct inode *dir = d_inode(parent);
1224         struct fuse_conn *fc;
1225         struct inode *inode;
1226         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1227
1228         if (!o->nodeid) {
1229                 /*
1230                  * Unlike in the case of fuse_lookup, zero nodeid does not mean
1231                  * ENOENT. Instead, it only means the userspace filesystem did
1232                  * not want to return attributes/handle for this entry.
1233                  *
1234                  * So do nothing.
1235                  */
1236                 return 0;
1237         }
1238
1239         if (name.name[0] == '.') {
1240                 /*
1241                  * We could potentially refresh the attributes of the directory
1242                  * and its parent?
1243                  */
1244                 if (name.len == 1)
1245                         return 0;
1246                 if (name.name[1] == '.' && name.len == 2)
1247                         return 0;
1248         }
1249
1250         if (invalid_nodeid(o->nodeid))
1251                 return -EIO;
1252         if (fuse_invalid_attr(&o->attr))
1253                 return -EIO;
1254
1255         fc = get_fuse_conn(dir);
1256
1257         name.hash = full_name_hash(parent, name.name, name.len);
1258         dentry = d_lookup(parent, &name);
1259         if (!dentry) {
1260 retry:
1261                 dentry = d_alloc_parallel(parent, &name, &wq);
1262                 if (IS_ERR(dentry))
1263                         return PTR_ERR(dentry);
1264         }
1265         if (!d_in_lookup(dentry)) {
1266                 struct fuse_inode *fi;
1267                 inode = d_inode(dentry);
1268                 if (!inode ||
1269                     get_node_id(inode) != o->nodeid ||
1270                     ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1271                         d_invalidate(dentry);
1272                         dput(dentry);
1273                         goto retry;
1274                 }
1275                 if (fuse_is_bad(inode)) {
1276                         dput(dentry);
1277                         return -EIO;
1278                 }
1279
1280                 fi = get_fuse_inode(inode);
1281                 spin_lock(&fc->lock);
1282                 fi->nlookup++;
1283                 spin_unlock(&fc->lock);
1284
1285                 forget_all_cached_acls(inode);
1286                 fuse_change_attributes(inode, &o->attr,
1287                                        entry_attr_timeout(o),
1288                                        attr_version);
1289                 /*
1290                  * The other branch comes via fuse_iget()
1291                  * which bumps nlookup inside
1292                  */
1293         } else {
1294                 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1295                                   &o->attr, entry_attr_timeout(o),
1296                                   attr_version);
1297                 if (!inode)
1298                         inode = ERR_PTR(-ENOMEM);
1299
1300                 alias = d_splice_alias(inode, dentry);
1301                 d_lookup_done(dentry);
1302                 if (alias) {
1303                         dput(dentry);
1304                         dentry = alias;
1305                 }
1306                 if (IS_ERR(dentry))
1307                         return PTR_ERR(dentry);
1308         }
1309         if (fc->readdirplus_auto)
1310                 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1311         fuse_change_entry_timeout(dentry, o);
1312
1313         dput(dentry);
1314         return 0;
1315 }
1316
1317 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1318                              struct dir_context *ctx, u64 attr_version)
1319 {
1320         struct fuse_direntplus *direntplus;
1321         struct fuse_dirent *dirent;
1322         size_t reclen;
1323         int over = 0;
1324         int ret;
1325
1326         while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1327                 direntplus = (struct fuse_direntplus *) buf;
1328                 dirent = &direntplus->dirent;
1329                 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1330
1331                 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1332                         return -EIO;
1333                 if (reclen > nbytes)
1334                         break;
1335                 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1336                         return -EIO;
1337
1338                 if (!over) {
1339                         /* We fill entries into dstbuf only as much as
1340                            it can hold. But we still continue iterating
1341                            over remaining entries to link them. If not,
1342                            we need to send a FORGET for each of those
1343                            which we did not link.
1344                         */
1345                         over = !dir_emit(ctx, dirent->name, dirent->namelen,
1346                                        dirent->ino, dirent->type);
1347                         if (!over)
1348                                 ctx->pos = dirent->off;
1349                 }
1350
1351                 buf += reclen;
1352                 nbytes -= reclen;
1353
1354                 ret = fuse_direntplus_link(file, direntplus, attr_version);
1355                 if (ret)
1356                         fuse_force_forget(file, direntplus->entry_out.nodeid);
1357         }
1358
1359         return 0;
1360 }
1361
1362 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1363 {
1364         int plus, err;
1365         size_t nbytes;
1366         struct page *page;
1367         struct inode *inode = file_inode(file);
1368         struct fuse_conn *fc = get_fuse_conn(inode);
1369         struct fuse_req *req;
1370         u64 attr_version = 0;
1371         bool locked;
1372
1373         if (fuse_is_bad(inode))
1374                 return -EIO;
1375
1376         req = fuse_get_req(fc, 1);
1377         if (IS_ERR(req))
1378                 return PTR_ERR(req);
1379
1380         page = alloc_page(GFP_KERNEL);
1381         if (!page) {
1382                 fuse_put_request(fc, req);
1383                 return -ENOMEM;
1384         }
1385
1386         plus = fuse_use_readdirplus(inode, ctx);
1387         req->out.argpages = 1;
1388         req->num_pages = 1;
1389         req->pages[0] = page;
1390         req->page_descs[0].length = PAGE_SIZE;
1391         if (plus) {
1392                 attr_version = fuse_get_attr_version(fc);
1393                 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1394                                FUSE_READDIRPLUS);
1395         } else {
1396                 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1397                                FUSE_READDIR);
1398         }
1399         locked = fuse_lock_inode(inode);
1400         fuse_request_send(fc, req);
1401         fuse_unlock_inode(inode, locked);
1402         nbytes = req->out.args[0].size;
1403         err = req->out.h.error;
1404         fuse_put_request(fc, req);
1405         if (!err) {
1406                 if (plus) {
1407                         err = parse_dirplusfile(page_address(page), nbytes,
1408                                                 file, ctx,
1409                                                 attr_version);
1410                 } else {
1411                         err = parse_dirfile(page_address(page), nbytes, file,
1412                                             ctx);
1413                 }
1414         }
1415
1416         __free_page(page);
1417         fuse_invalidate_atime(inode);
1418         return err;
1419 }
1420
1421 static const char *fuse_get_link(struct dentry *dentry,
1422                                  struct inode *inode,
1423                                  struct delayed_call *done)
1424 {
1425         struct fuse_conn *fc = get_fuse_conn(inode);
1426         FUSE_ARGS(args);
1427         char *link;
1428         ssize_t ret;
1429
1430         if (!dentry)
1431                 return ERR_PTR(-ECHILD);
1432
1433         if (fuse_is_bad(inode))
1434                 return ERR_PTR(-EIO);
1435
1436         link = kmalloc(PAGE_SIZE, GFP_KERNEL);
1437         if (!link)
1438                 return ERR_PTR(-ENOMEM);
1439
1440         args.in.h.opcode = FUSE_READLINK;
1441         args.in.h.nodeid = get_node_id(inode);
1442         args.out.argvar = 1;
1443         args.out.numargs = 1;
1444         args.out.args[0].size = PAGE_SIZE - 1;
1445         args.out.args[0].value = link;
1446         ret = fuse_simple_request(fc, &args);
1447         if (ret < 0) {
1448                 kfree(link);
1449                 link = ERR_PTR(ret);
1450         } else {
1451                 link[ret] = '\0';
1452                 set_delayed_call(done, kfree_link, link);
1453         }
1454         fuse_invalidate_atime(inode);
1455         return link;
1456 }
1457
1458 static int fuse_dir_open(struct inode *inode, struct file *file)
1459 {
1460         return fuse_open_common(inode, file, true);
1461 }
1462
1463 static int fuse_dir_release(struct inode *inode, struct file *file)
1464 {
1465         fuse_release_common(file, FUSE_RELEASEDIR);
1466
1467         return 0;
1468 }
1469
1470 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1471                           int datasync)
1472 {
1473         return fuse_fsync_common(file, start, end, datasync, 1);
1474 }
1475
1476 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1477                             unsigned long arg)
1478 {
1479         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1480
1481         /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1482         if (fc->minor < 18)
1483                 return -ENOTTY;
1484
1485         return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1486 }
1487
1488 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1489                                    unsigned long arg)
1490 {
1491         struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1492
1493         if (fc->minor < 18)
1494                 return -ENOTTY;
1495
1496         return fuse_ioctl_common(file, cmd, arg,
1497                                  FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1498 }
1499
1500 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1501 {
1502         /* Always update if mtime is explicitly set  */
1503         if (ivalid & ATTR_MTIME_SET)
1504                 return true;
1505
1506         /* Or if kernel i_mtime is the official one */
1507         if (trust_local_mtime)
1508                 return true;
1509
1510         /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1511         if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1512                 return false;
1513
1514         /* In all other cases update */
1515         return true;
1516 }
1517
1518 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1519                            bool trust_local_cmtime)
1520 {
1521         unsigned ivalid = iattr->ia_valid;
1522
1523         if (ivalid & ATTR_MODE)
1524                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1525         if (ivalid & ATTR_UID)
1526                 arg->valid |= FATTR_UID,    arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1527         if (ivalid & ATTR_GID)
1528                 arg->valid |= FATTR_GID,    arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1529         if (ivalid & ATTR_SIZE)
1530                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1531         if (ivalid & ATTR_ATIME) {
1532                 arg->valid |= FATTR_ATIME;
1533                 arg->atime = iattr->ia_atime.tv_sec;
1534                 arg->atimensec = iattr->ia_atime.tv_nsec;
1535                 if (!(ivalid & ATTR_ATIME_SET))
1536                         arg->valid |= FATTR_ATIME_NOW;
1537         }
1538         if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1539                 arg->valid |= FATTR_MTIME;
1540                 arg->mtime = iattr->ia_mtime.tv_sec;
1541                 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1542                 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1543                         arg->valid |= FATTR_MTIME_NOW;
1544         }
1545         if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1546                 arg->valid |= FATTR_CTIME;
1547                 arg->ctime = iattr->ia_ctime.tv_sec;
1548                 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1549         }
1550 }
1551
1552 /*
1553  * Prevent concurrent writepages on inode
1554  *
1555  * This is done by adding a negative bias to the inode write counter
1556  * and waiting for all pending writes to finish.
1557  */
1558 void fuse_set_nowrite(struct inode *inode)
1559 {
1560         struct fuse_conn *fc = get_fuse_conn(inode);
1561         struct fuse_inode *fi = get_fuse_inode(inode);
1562
1563         BUG_ON(!inode_is_locked(inode));
1564
1565         spin_lock(&fc->lock);
1566         BUG_ON(fi->writectr < 0);
1567         fi->writectr += FUSE_NOWRITE;
1568         spin_unlock(&fc->lock);
1569         wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1570 }
1571
1572 /*
1573  * Allow writepages on inode
1574  *
1575  * Remove the bias from the writecounter and send any queued
1576  * writepages.
1577  */
1578 static void __fuse_release_nowrite(struct inode *inode)
1579 {
1580         struct fuse_inode *fi = get_fuse_inode(inode);
1581
1582         BUG_ON(fi->writectr != FUSE_NOWRITE);
1583         fi->writectr = 0;
1584         fuse_flush_writepages(inode);
1585 }
1586
1587 void fuse_release_nowrite(struct inode *inode)
1588 {
1589         struct fuse_conn *fc = get_fuse_conn(inode);
1590
1591         spin_lock(&fc->lock);
1592         __fuse_release_nowrite(inode);
1593         spin_unlock(&fc->lock);
1594 }
1595
1596 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1597                               struct inode *inode,
1598                               struct fuse_setattr_in *inarg_p,
1599                               struct fuse_attr_out *outarg_p)
1600 {
1601         args->in.h.opcode = FUSE_SETATTR;
1602         args->in.h.nodeid = get_node_id(inode);
1603         args->in.numargs = 1;
1604         args->in.args[0].size = sizeof(*inarg_p);
1605         args->in.args[0].value = inarg_p;
1606         args->out.numargs = 1;
1607         args->out.args[0].size = sizeof(*outarg_p);
1608         args->out.args[0].value = outarg_p;
1609 }
1610
1611 /*
1612  * Flush inode->i_mtime to the server
1613  */
1614 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1615 {
1616         struct fuse_conn *fc = get_fuse_conn(inode);
1617         FUSE_ARGS(args);
1618         struct fuse_setattr_in inarg;
1619         struct fuse_attr_out outarg;
1620
1621         memset(&inarg, 0, sizeof(inarg));
1622         memset(&outarg, 0, sizeof(outarg));
1623
1624         inarg.valid = FATTR_MTIME;
1625         inarg.mtime = inode->i_mtime.tv_sec;
1626         inarg.mtimensec = inode->i_mtime.tv_nsec;
1627         if (fc->minor >= 23) {
1628                 inarg.valid |= FATTR_CTIME;
1629                 inarg.ctime = inode->i_ctime.tv_sec;
1630                 inarg.ctimensec = inode->i_ctime.tv_nsec;
1631         }
1632         if (ff) {
1633                 inarg.valid |= FATTR_FH;
1634                 inarg.fh = ff->fh;
1635         }
1636         fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1637
1638         return fuse_simple_request(fc, &args);
1639 }
1640
1641 /*
1642  * Set attributes, and at the same time refresh them.
1643  *
1644  * Truncation is slightly complicated, because the 'truncate' request
1645  * may fail, in which case we don't want to touch the mapping.
1646  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1647  * and the actual truncation by hand.
1648  */
1649 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1650                     struct file *file)
1651 {
1652         struct inode *inode = d_inode(dentry);
1653         struct fuse_conn *fc = get_fuse_conn(inode);
1654         struct fuse_inode *fi = get_fuse_inode(inode);
1655         FUSE_ARGS(args);
1656         struct fuse_setattr_in inarg;
1657         struct fuse_attr_out outarg;
1658         bool is_truncate = false;
1659         bool is_wb = fc->writeback_cache;
1660         loff_t oldsize;
1661         int err;
1662         bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1663
1664         if (!fc->default_permissions)
1665                 attr->ia_valid |= ATTR_FORCE;
1666
1667         err = setattr_prepare(dentry, attr);
1668         if (err)
1669                 return err;
1670
1671         if (attr->ia_valid & ATTR_OPEN) {
1672                 /* This is coming from open(..., ... | O_TRUNC); */
1673                 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1674                 WARN_ON(attr->ia_size != 0);
1675                 if (fc->atomic_o_trunc) {
1676                         /*
1677                          * No need to send request to userspace, since actual
1678                          * truncation has already been done by OPEN.  But still
1679                          * need to truncate page cache.
1680                          */
1681                         i_size_write(inode, 0);
1682                         truncate_pagecache(inode, 0);
1683                         return 0;
1684                 }
1685                 file = NULL;
1686         }
1687
1688         if (attr->ia_valid & ATTR_SIZE)
1689                 is_truncate = true;
1690
1691         /* Flush dirty data/metadata before non-truncate SETATTR */
1692         if (is_wb && S_ISREG(inode->i_mode) &&
1693             attr->ia_valid &
1694                         (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1695                          ATTR_TIMES_SET)) {
1696                 err = write_inode_now(inode, true);
1697                 if (err)
1698                         return err;
1699
1700                 fuse_set_nowrite(inode);
1701                 fuse_release_nowrite(inode);
1702         }
1703
1704         if (is_truncate) {
1705                 fuse_set_nowrite(inode);
1706                 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1707                 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1708                         attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1709         }
1710
1711         memset(&inarg, 0, sizeof(inarg));
1712         memset(&outarg, 0, sizeof(outarg));
1713         iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1714         if (file) {
1715                 struct fuse_file *ff = file->private_data;
1716                 inarg.valid |= FATTR_FH;
1717                 inarg.fh = ff->fh;
1718         }
1719         if (attr->ia_valid & ATTR_SIZE) {
1720                 /* For mandatory locking in truncate */
1721                 inarg.valid |= FATTR_LOCKOWNER;
1722                 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1723         }
1724         fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1725         err = fuse_simple_request(fc, &args);
1726         if (err) {
1727                 if (err == -EINTR)
1728                         fuse_invalidate_attr(inode);
1729                 goto error;
1730         }
1731
1732         if (fuse_invalid_attr(&outarg.attr) ||
1733             (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1734                 fuse_make_bad(inode);
1735                 err = -EIO;
1736                 goto error;
1737         }
1738
1739         spin_lock(&fc->lock);
1740         /* the kernel maintains i_mtime locally */
1741         if (trust_local_cmtime) {
1742                 if (attr->ia_valid & ATTR_MTIME)
1743                         inode->i_mtime = attr->ia_mtime;
1744                 if (attr->ia_valid & ATTR_CTIME)
1745                         inode->i_ctime = attr->ia_ctime;
1746                 /* FIXME: clear I_DIRTY_SYNC? */
1747         }
1748
1749         fuse_change_attributes_common(inode, &outarg.attr,
1750                                       attr_timeout(&outarg));
1751         oldsize = inode->i_size;
1752         /* see the comment in fuse_change_attributes() */
1753         if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1754                 i_size_write(inode, outarg.attr.size);
1755
1756         if (is_truncate) {
1757                 /* NOTE: this may release/reacquire fc->lock */
1758                 __fuse_release_nowrite(inode);
1759         }
1760         spin_unlock(&fc->lock);
1761
1762         /*
1763          * Only call invalidate_inode_pages2() after removing
1764          * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1765          */
1766         if ((is_truncate || !is_wb) &&
1767             S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1768                 truncate_pagecache(inode, outarg.attr.size);
1769                 invalidate_inode_pages2(inode->i_mapping);
1770         }
1771
1772         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1773         return 0;
1774
1775 error:
1776         if (is_truncate)
1777                 fuse_release_nowrite(inode);
1778
1779         clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1780         return err;
1781 }
1782
1783 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1784 {
1785         struct inode *inode = d_inode(entry);
1786         struct fuse_conn *fc = get_fuse_conn(inode);
1787         struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1788         int ret;
1789
1790         if (fuse_is_bad(inode))
1791                 return -EIO;
1792
1793         if (!fuse_allow_current_process(get_fuse_conn(inode)))
1794                 return -EACCES;
1795
1796         if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1797                 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1798                                     ATTR_MODE);
1799
1800                 /*
1801                  * The only sane way to reliably kill suid/sgid is to do it in
1802                  * the userspace filesystem
1803                  *
1804                  * This should be done on write(), truncate() and chown().
1805                  */
1806                 if (!fc->handle_killpriv) {
1807                         /*
1808                          * ia_mode calculation may have used stale i_mode.
1809                          * Refresh and recalculate.
1810                          */
1811                         ret = fuse_do_getattr(inode, NULL, file);
1812                         if (ret)
1813                                 return ret;
1814
1815                         attr->ia_mode = inode->i_mode;
1816                         if (inode->i_mode & S_ISUID) {
1817                                 attr->ia_valid |= ATTR_MODE;
1818                                 attr->ia_mode &= ~S_ISUID;
1819                         }
1820                         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1821                                 attr->ia_valid |= ATTR_MODE;
1822                                 attr->ia_mode &= ~S_ISGID;
1823                         }
1824                 }
1825         }
1826         if (!attr->ia_valid)
1827                 return 0;
1828
1829         ret = fuse_do_setattr(entry, attr, file);
1830         if (!ret) {
1831                 /*
1832                  * If filesystem supports acls it may have updated acl xattrs in
1833                  * the filesystem, so forget cached acls for the inode.
1834                  */
1835                 if (fc->posix_acl)
1836                         forget_all_cached_acls(inode);
1837
1838                 /* Directory mode changed, may need to revalidate access */
1839                 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1840                         fuse_invalidate_entry_cache(entry);
1841         }
1842         return ret;
1843 }
1844
1845 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1846                         struct kstat *stat)
1847 {
1848         struct inode *inode = d_inode(entry);
1849         struct fuse_conn *fc = get_fuse_conn(inode);
1850
1851         if (fuse_is_bad(inode))
1852                 return -EIO;
1853
1854         if (!fuse_allow_current_process(fc))
1855                 return -EACCES;
1856
1857         return fuse_update_attributes(inode, stat, NULL, NULL);
1858 }
1859
1860 static const struct inode_operations fuse_dir_inode_operations = {
1861         .lookup         = fuse_lookup,
1862         .mkdir          = fuse_mkdir,
1863         .symlink        = fuse_symlink,
1864         .unlink         = fuse_unlink,
1865         .rmdir          = fuse_rmdir,
1866         .rename         = fuse_rename2,
1867         .link           = fuse_link,
1868         .setattr        = fuse_setattr,
1869         .create         = fuse_create,
1870         .atomic_open    = fuse_atomic_open,
1871         .mknod          = fuse_mknod,
1872         .permission     = fuse_permission,
1873         .getattr        = fuse_getattr,
1874         .listxattr      = fuse_listxattr,
1875         .get_acl        = fuse_get_acl,
1876         .set_acl        = fuse_set_acl,
1877 };
1878
1879 static const struct file_operations fuse_dir_operations = {
1880         .llseek         = generic_file_llseek,
1881         .read           = generic_read_dir,
1882         .iterate_shared = fuse_readdir,
1883         .open           = fuse_dir_open,
1884         .release        = fuse_dir_release,
1885         .fsync          = fuse_dir_fsync,
1886         .unlocked_ioctl = fuse_dir_ioctl,
1887         .compat_ioctl   = fuse_dir_compat_ioctl,
1888 };
1889
1890 static const struct inode_operations fuse_common_inode_operations = {
1891         .setattr        = fuse_setattr,
1892         .permission     = fuse_permission,
1893         .getattr        = fuse_getattr,
1894         .listxattr      = fuse_listxattr,
1895         .get_acl        = fuse_get_acl,
1896         .set_acl        = fuse_set_acl,
1897 };
1898
1899 static const struct inode_operations fuse_symlink_inode_operations = {
1900         .setattr        = fuse_setattr,
1901         .get_link       = fuse_get_link,
1902         .readlink       = generic_readlink,
1903         .getattr        = fuse_getattr,
1904         .listxattr      = fuse_listxattr,
1905 };
1906
1907 void fuse_init_common(struct inode *inode)
1908 {
1909         inode->i_op = &fuse_common_inode_operations;
1910 }
1911
1912 void fuse_init_dir(struct inode *inode)
1913 {
1914         inode->i_op = &fuse_dir_inode_operations;
1915         inode->i_fop = &fuse_dir_operations;
1916 }
1917
1918 void fuse_init_symlink(struct inode *inode)
1919 {
1920         inode->i_op = &fuse_symlink_inode_operations;
1921 }