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