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