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