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