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