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