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