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