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