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