fuse: allow umask processing in userspace
[safe/jmp/linux-2.6] / 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/gfp.h>
14 #include <linux/sched.h>
15 #include <linux/namei.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 = entry->d_inode;
160
161         if (inode && is_bad_inode(inode))
162                 return 0;
163         else if (fuse_dentry_time(entry) < get_jiffies_64()) {
164                 int err;
165                 struct fuse_entry_out outarg;
166                 struct fuse_conn *fc;
167                 struct fuse_req *req;
168                 struct fuse_req *forget_req;
169                 struct dentry *parent;
170                 u64 attr_version;
171
172                 /* For negative dentries, always do a fresh lookup */
173                 if (!inode)
174                         return 0;
175
176                 fc = get_fuse_conn(inode);
177                 req = fuse_get_req(fc);
178                 if (IS_ERR(req))
179                         return 0;
180
181                 forget_req = fuse_get_req(fc);
182                 if (IS_ERR(forget_req)) {
183                         fuse_put_request(fc, req);
184                         return 0;
185                 }
186
187                 attr_version = fuse_get_attr_version(fc);
188
189                 parent = dget_parent(entry);
190                 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
191                                  &entry->d_name, &outarg);
192                 fuse_request_send(fc, req);
193                 dput(parent);
194                 err = req->out.h.error;
195                 fuse_put_request(fc, req);
196                 /* Zero nodeid is same as -ENOENT */
197                 if (!err && !outarg.nodeid)
198                         err = -ENOENT;
199                 if (!err) {
200                         struct fuse_inode *fi = get_fuse_inode(inode);
201                         if (outarg.nodeid != get_node_id(inode)) {
202                                 fuse_send_forget(fc, forget_req,
203                                                  outarg.nodeid, 1);
204                                 return 0;
205                         }
206                         spin_lock(&fc->lock);
207                         fi->nlookup++;
208                         spin_unlock(&fc->lock);
209                 }
210                 fuse_put_request(fc, forget_req);
211                 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
212                         return 0;
213
214                 fuse_change_attributes(inode, &outarg.attr,
215                                        entry_attr_timeout(&outarg),
216                                        attr_version);
217                 fuse_change_entry_timeout(entry, &outarg);
218         }
219         return 1;
220 }
221
222 static int invalid_nodeid(u64 nodeid)
223 {
224         return !nodeid || nodeid == FUSE_ROOT_ID;
225 }
226
227 const struct dentry_operations fuse_dentry_operations = {
228         .d_revalidate   = fuse_dentry_revalidate,
229 };
230
231 int fuse_valid_type(int m)
232 {
233         return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
234                 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
235 }
236
237 /*
238  * Add a directory inode to a dentry, ensuring that no other dentry
239  * refers to this inode.  Called with fc->inst_mutex.
240  */
241 static struct dentry *fuse_d_add_directory(struct dentry *entry,
242                                            struct inode *inode)
243 {
244         struct dentry *alias = d_find_alias(inode);
245         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
246                 /* This tries to shrink the subtree below alias */
247                 fuse_invalidate_entry(alias);
248                 dput(alias);
249                 if (!list_empty(&inode->i_dentry))
250                         return ERR_PTR(-EBUSY);
251         } else {
252                 dput(alias);
253         }
254         return d_splice_alias(inode, entry);
255 }
256
257 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
258                      struct fuse_entry_out *outarg, struct inode **inode)
259 {
260         struct fuse_conn *fc = get_fuse_conn_super(sb);
261         struct fuse_req *req;
262         struct fuse_req *forget_req;
263         u64 attr_version;
264         int err;
265
266         *inode = NULL;
267         err = -ENAMETOOLONG;
268         if (name->len > FUSE_NAME_MAX)
269                 goto out;
270
271         req = fuse_get_req(fc);
272         err = PTR_ERR(req);
273         if (IS_ERR(req))
274                 goto out;
275
276         forget_req = fuse_get_req(fc);
277         err = PTR_ERR(forget_req);
278         if (IS_ERR(forget_req)) {
279                 fuse_put_request(fc, req);
280                 goto out;
281         }
282
283         attr_version = fuse_get_attr_version(fc);
284
285         fuse_lookup_init(fc, req, nodeid, name, outarg);
286         fuse_request_send(fc, req);
287         err = req->out.h.error;
288         fuse_put_request(fc, req);
289         /* Zero nodeid is same as -ENOENT, but with valid timeout */
290         if (err || !outarg->nodeid)
291                 goto out_put_forget;
292
293         err = -EIO;
294         if (!outarg->nodeid)
295                 goto out_put_forget;
296         if (!fuse_valid_type(outarg->attr.mode))
297                 goto out_put_forget;
298
299         *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
300                            &outarg->attr, entry_attr_timeout(outarg),
301                            attr_version);
302         err = -ENOMEM;
303         if (!*inode) {
304                 fuse_send_forget(fc, forget_req, outarg->nodeid, 1);
305                 goto out;
306         }
307         err = 0;
308
309  out_put_forget:
310         fuse_put_request(fc, forget_req);
311  out:
312         return err;
313 }
314
315 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
316                                   struct nameidata *nd)
317 {
318         int err;
319         struct fuse_entry_out outarg;
320         struct inode *inode;
321         struct dentry *newent;
322         struct fuse_conn *fc = get_fuse_conn(dir);
323         bool outarg_valid = true;
324
325         err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
326                                &outarg, &inode);
327         if (err == -ENOENT) {
328                 outarg_valid = false;
329                 err = 0;
330         }
331         if (err)
332                 goto out_err;
333
334         err = -EIO;
335         if (inode && get_node_id(inode) == FUSE_ROOT_ID)
336                 goto out_iput;
337
338         if (inode && S_ISDIR(inode->i_mode)) {
339                 mutex_lock(&fc->inst_mutex);
340                 newent = fuse_d_add_directory(entry, inode);
341                 mutex_unlock(&fc->inst_mutex);
342                 err = PTR_ERR(newent);
343                 if (IS_ERR(newent))
344                         goto out_iput;
345         } else {
346                 newent = d_splice_alias(inode, entry);
347         }
348
349         entry = newent ? newent : entry;
350         entry->d_op = &fuse_dentry_operations;
351         if (outarg_valid)
352                 fuse_change_entry_timeout(entry, &outarg);
353         else
354                 fuse_invalidate_entry_cache(entry);
355
356         return newent;
357
358  out_iput:
359         iput(inode);
360  out_err:
361         return ERR_PTR(err);
362 }
363
364 /*
365  * Atomic create+open operation
366  *
367  * If the filesystem doesn't support this, then fall back to separate
368  * 'mknod' + 'open' requests.
369  */
370 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
371                             struct nameidata *nd)
372 {
373         int err;
374         struct inode *inode;
375         struct fuse_conn *fc = get_fuse_conn(dir);
376         struct fuse_req *req;
377         struct fuse_req *forget_req;
378         struct fuse_create_in inarg;
379         struct fuse_open_out outopen;
380         struct fuse_entry_out outentry;
381         struct fuse_file *ff;
382         struct file *file;
383         int flags = nd->intent.open.flags - 1;
384
385         if (fc->no_create)
386                 return -ENOSYS;
387
388         forget_req = fuse_get_req(fc);
389         if (IS_ERR(forget_req))
390                 return PTR_ERR(forget_req);
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                 if (err == -ENOSYS)
431                         fc->no_create = 1;
432                 goto out_free_ff;
433         }
434
435         err = -EIO;
436         if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
437                 goto out_free_ff;
438
439         fuse_put_request(fc, req);
440         ff->fh = outopen.fh;
441         ff->nodeid = outentry.nodeid;
442         ff->open_flags = outopen.open_flags;
443         inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
444                           &outentry.attr, entry_attr_timeout(&outentry), 0);
445         if (!inode) {
446                 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
447                 fuse_sync_release(ff, flags);
448                 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
449                 return -ENOMEM;
450         }
451         fuse_put_request(fc, forget_req);
452         d_instantiate(entry, inode);
453         fuse_change_entry_timeout(entry, &outentry);
454         fuse_invalidate_attr(dir);
455         file = lookup_instantiate_filp(nd, entry, generic_file_open);
456         if (IS_ERR(file)) {
457                 fuse_sync_release(ff, flags);
458                 return PTR_ERR(file);
459         }
460         file->private_data = fuse_file_get(ff);
461         fuse_finish_open(inode, file);
462         return 0;
463
464  out_free_ff:
465         fuse_file_free(ff);
466  out_put_request:
467         fuse_put_request(fc, req);
468  out_put_forget_req:
469         fuse_put_request(fc, forget_req);
470         return err;
471 }
472
473 /*
474  * Code shared between mknod, mkdir, symlink and link
475  */
476 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
477                             struct inode *dir, struct dentry *entry,
478                             int mode)
479 {
480         struct fuse_entry_out outarg;
481         struct inode *inode;
482         int err;
483         struct fuse_req *forget_req;
484
485         forget_req = fuse_get_req(fc);
486         if (IS_ERR(forget_req)) {
487                 fuse_put_request(fc, req);
488                 return PTR_ERR(forget_req);
489         }
490
491         memset(&outarg, 0, sizeof(outarg));
492         req->in.h.nodeid = get_node_id(dir);
493         req->out.numargs = 1;
494         if (fc->minor < 9)
495                 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
496         else
497                 req->out.args[0].size = sizeof(outarg);
498         req->out.args[0].value = &outarg;
499         fuse_request_send(fc, req);
500         err = req->out.h.error;
501         fuse_put_request(fc, req);
502         if (err)
503                 goto out_put_forget_req;
504
505         err = -EIO;
506         if (invalid_nodeid(outarg.nodeid))
507                 goto out_put_forget_req;
508
509         if ((outarg.attr.mode ^ mode) & S_IFMT)
510                 goto out_put_forget_req;
511
512         inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
513                           &outarg.attr, entry_attr_timeout(&outarg), 0);
514         if (!inode) {
515                 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
516                 return -ENOMEM;
517         }
518         fuse_put_request(fc, forget_req);
519
520         if (S_ISDIR(inode->i_mode)) {
521                 struct dentry *alias;
522                 mutex_lock(&fc->inst_mutex);
523                 alias = d_find_alias(inode);
524                 if (alias) {
525                         /* New directory must have moved since mkdir */
526                         mutex_unlock(&fc->inst_mutex);
527                         dput(alias);
528                         iput(inode);
529                         return -EBUSY;
530                 }
531                 d_instantiate(entry, inode);
532                 mutex_unlock(&fc->inst_mutex);
533         } else
534                 d_instantiate(entry, inode);
535
536         fuse_change_entry_timeout(entry, &outarg);
537         fuse_invalidate_attr(dir);
538         return 0;
539
540  out_put_forget_req:
541         fuse_put_request(fc, forget_req);
542         return err;
543 }
544
545 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
546                       dev_t rdev)
547 {
548         struct fuse_mknod_in inarg;
549         struct fuse_conn *fc = get_fuse_conn(dir);
550         struct fuse_req *req = fuse_get_req(fc);
551         if (IS_ERR(req))
552                 return PTR_ERR(req);
553
554         if (!fc->dont_mask)
555                 mode &= ~current_umask();
556
557         memset(&inarg, 0, sizeof(inarg));
558         inarg.mode = mode;
559         inarg.rdev = new_encode_dev(rdev);
560         inarg.umask = current_umask();
561         req->in.h.opcode = FUSE_MKNOD;
562         req->in.numargs = 2;
563         req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
564                                                 sizeof(inarg);
565         req->in.args[0].value = &inarg;
566         req->in.args[1].size = entry->d_name.len + 1;
567         req->in.args[1].value = entry->d_name.name;
568         return create_new_entry(fc, req, dir, entry, mode);
569 }
570
571 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
572                        struct nameidata *nd)
573 {
574         if (nd && (nd->flags & LOOKUP_OPEN)) {
575                 int err = fuse_create_open(dir, entry, mode, nd);
576                 if (err != -ENOSYS)
577                         return err;
578                 /* Fall back on mknod */
579         }
580         return fuse_mknod(dir, entry, mode, 0);
581 }
582
583 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
584 {
585         struct fuse_mkdir_in inarg;
586         struct fuse_conn *fc = get_fuse_conn(dir);
587         struct fuse_req *req = fuse_get_req(fc);
588         if (IS_ERR(req))
589                 return PTR_ERR(req);
590
591         if (!fc->dont_mask)
592                 mode &= ~current_umask();
593
594         memset(&inarg, 0, sizeof(inarg));
595         inarg.mode = mode;
596         inarg.umask = current_umask();
597         req->in.h.opcode = FUSE_MKDIR;
598         req->in.numargs = 2;
599         req->in.args[0].size = sizeof(inarg);
600         req->in.args[0].value = &inarg;
601         req->in.args[1].size = entry->d_name.len + 1;
602         req->in.args[1].value = entry->d_name.name;
603         return create_new_entry(fc, req, dir, entry, S_IFDIR);
604 }
605
606 static int fuse_symlink(struct inode *dir, struct dentry *entry,
607                         const char *link)
608 {
609         struct fuse_conn *fc = get_fuse_conn(dir);
610         unsigned len = strlen(link) + 1;
611         struct fuse_req *req = fuse_get_req(fc);
612         if (IS_ERR(req))
613                 return PTR_ERR(req);
614
615         req->in.h.opcode = FUSE_SYMLINK;
616         req->in.numargs = 2;
617         req->in.args[0].size = entry->d_name.len + 1;
618         req->in.args[0].value = entry->d_name.name;
619         req->in.args[1].size = len;
620         req->in.args[1].value = link;
621         return create_new_entry(fc, req, dir, entry, S_IFLNK);
622 }
623
624 static int fuse_unlink(struct inode *dir, struct dentry *entry)
625 {
626         int err;
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         req->in.h.opcode = FUSE_UNLINK;
633         req->in.h.nodeid = get_node_id(dir);
634         req->in.numargs = 1;
635         req->in.args[0].size = entry->d_name.len + 1;
636         req->in.args[0].value = entry->d_name.name;
637         fuse_request_send(fc, req);
638         err = req->out.h.error;
639         fuse_put_request(fc, req);
640         if (!err) {
641                 struct inode *inode = entry->d_inode;
642
643                 /*
644                  * Set nlink to zero so the inode can be cleared, if the inode
645                  * does have more links this will be discovered at the next
646                  * lookup/getattr.
647                  */
648                 clear_nlink(inode);
649                 fuse_invalidate_attr(inode);
650                 fuse_invalidate_attr(dir);
651                 fuse_invalidate_entry_cache(entry);
652         } else if (err == -EINTR)
653                 fuse_invalidate_entry(entry);
654         return err;
655 }
656
657 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
658 {
659         int err;
660         struct fuse_conn *fc = get_fuse_conn(dir);
661         struct fuse_req *req = fuse_get_req(fc);
662         if (IS_ERR(req))
663                 return PTR_ERR(req);
664
665         req->in.h.opcode = FUSE_RMDIR;
666         req->in.h.nodeid = get_node_id(dir);
667         req->in.numargs = 1;
668         req->in.args[0].size = entry->d_name.len + 1;
669         req->in.args[0].value = entry->d_name.name;
670         fuse_request_send(fc, req);
671         err = req->out.h.error;
672         fuse_put_request(fc, req);
673         if (!err) {
674                 clear_nlink(entry->d_inode);
675                 fuse_invalidate_attr(dir);
676                 fuse_invalidate_entry_cache(entry);
677         } else if (err == -EINTR)
678                 fuse_invalidate_entry(entry);
679         return err;
680 }
681
682 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
683                        struct inode *newdir, struct dentry *newent)
684 {
685         int err;
686         struct fuse_rename_in inarg;
687         struct fuse_conn *fc = get_fuse_conn(olddir);
688         struct fuse_req *req = fuse_get_req(fc);
689         if (IS_ERR(req))
690                 return PTR_ERR(req);
691
692         memset(&inarg, 0, sizeof(inarg));
693         inarg.newdir = get_node_id(newdir);
694         req->in.h.opcode = FUSE_RENAME;
695         req->in.h.nodeid = get_node_id(olddir);
696         req->in.numargs = 3;
697         req->in.args[0].size = sizeof(inarg);
698         req->in.args[0].value = &inarg;
699         req->in.args[1].size = oldent->d_name.len + 1;
700         req->in.args[1].value = oldent->d_name.name;
701         req->in.args[2].size = newent->d_name.len + 1;
702         req->in.args[2].value = newent->d_name.name;
703         fuse_request_send(fc, req);
704         err = req->out.h.error;
705         fuse_put_request(fc, req);
706         if (!err) {
707                 /* ctime changes */
708                 fuse_invalidate_attr(oldent->d_inode);
709
710                 fuse_invalidate_attr(olddir);
711                 if (olddir != newdir)
712                         fuse_invalidate_attr(newdir);
713
714                 /* newent will end up negative */
715                 if (newent->d_inode)
716                         fuse_invalidate_entry_cache(newent);
717         } else if (err == -EINTR) {
718                 /* If request was interrupted, DEITY only knows if the
719                    rename actually took place.  If the invalidation
720                    fails (e.g. some process has CWD under the renamed
721                    directory), then there can be inconsistency between
722                    the dcache and the real filesystem.  Tough luck. */
723                 fuse_invalidate_entry(oldent);
724                 if (newent->d_inode)
725                         fuse_invalidate_entry(newent);
726         }
727
728         return err;
729 }
730
731 static int fuse_link(struct dentry *entry, struct inode *newdir,
732                      struct dentry *newent)
733 {
734         int err;
735         struct fuse_link_in inarg;
736         struct inode *inode = entry->d_inode;
737         struct fuse_conn *fc = get_fuse_conn(inode);
738         struct fuse_req *req = fuse_get_req(fc);
739         if (IS_ERR(req))
740                 return PTR_ERR(req);
741
742         memset(&inarg, 0, sizeof(inarg));
743         inarg.oldnodeid = get_node_id(inode);
744         req->in.h.opcode = FUSE_LINK;
745         req->in.numargs = 2;
746         req->in.args[0].size = sizeof(inarg);
747         req->in.args[0].value = &inarg;
748         req->in.args[1].size = newent->d_name.len + 1;
749         req->in.args[1].value = newent->d_name.name;
750         err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
751         /* Contrary to "normal" filesystems it can happen that link
752            makes two "logical" inodes point to the same "physical"
753            inode.  We invalidate the attributes of the old one, so it
754            will reflect changes in the backing inode (link count,
755            etc.)
756         */
757         if (!err || err == -EINTR)
758                 fuse_invalidate_attr(inode);
759         return err;
760 }
761
762 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
763                           struct kstat *stat)
764 {
765         stat->dev = inode->i_sb->s_dev;
766         stat->ino = attr->ino;
767         stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
768         stat->nlink = attr->nlink;
769         stat->uid = attr->uid;
770         stat->gid = attr->gid;
771         stat->rdev = inode->i_rdev;
772         stat->atime.tv_sec = attr->atime;
773         stat->atime.tv_nsec = attr->atimensec;
774         stat->mtime.tv_sec = attr->mtime;
775         stat->mtime.tv_nsec = attr->mtimensec;
776         stat->ctime.tv_sec = attr->ctime;
777         stat->ctime.tv_nsec = attr->ctimensec;
778         stat->size = attr->size;
779         stat->blocks = attr->blocks;
780         stat->blksize = (1 << inode->i_blkbits);
781 }
782
783 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
784                            struct file *file)
785 {
786         int err;
787         struct fuse_getattr_in inarg;
788         struct fuse_attr_out outarg;
789         struct fuse_conn *fc = get_fuse_conn(inode);
790         struct fuse_req *req;
791         u64 attr_version;
792
793         req = fuse_get_req(fc);
794         if (IS_ERR(req))
795                 return PTR_ERR(req);
796
797         attr_version = fuse_get_attr_version(fc);
798
799         memset(&inarg, 0, sizeof(inarg));
800         memset(&outarg, 0, sizeof(outarg));
801         /* Directories have separate file-handle space */
802         if (file && S_ISREG(inode->i_mode)) {
803                 struct fuse_file *ff = file->private_data;
804
805                 inarg.getattr_flags |= FUSE_GETATTR_FH;
806                 inarg.fh = ff->fh;
807         }
808         req->in.h.opcode = FUSE_GETATTR;
809         req->in.h.nodeid = get_node_id(inode);
810         req->in.numargs = 1;
811         req->in.args[0].size = sizeof(inarg);
812         req->in.args[0].value = &inarg;
813         req->out.numargs = 1;
814         if (fc->minor < 9)
815                 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
816         else
817                 req->out.args[0].size = sizeof(outarg);
818         req->out.args[0].value = &outarg;
819         fuse_request_send(fc, req);
820         err = req->out.h.error;
821         fuse_put_request(fc, req);
822         if (!err) {
823                 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
824                         make_bad_inode(inode);
825                         err = -EIO;
826                 } else {
827                         fuse_change_attributes(inode, &outarg.attr,
828                                                attr_timeout(&outarg),
829                                                attr_version);
830                         if (stat)
831                                 fuse_fillattr(inode, &outarg.attr, stat);
832                 }
833         }
834         return err;
835 }
836
837 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
838                            struct file *file, bool *refreshed)
839 {
840         struct fuse_inode *fi = get_fuse_inode(inode);
841         int err;
842         bool r;
843
844         if (fi->i_time < get_jiffies_64()) {
845                 r = true;
846                 err = fuse_do_getattr(inode, stat, file);
847         } else {
848                 r = false;
849                 err = 0;
850                 if (stat) {
851                         generic_fillattr(inode, stat);
852                         stat->mode = fi->orig_i_mode;
853                 }
854         }
855
856         if (refreshed != NULL)
857                 *refreshed = r;
858
859         return err;
860 }
861
862 /*
863  * Calling into a user-controlled filesystem gives the filesystem
864  * daemon ptrace-like capabilities over the requester process.  This
865  * means, that the filesystem daemon is able to record the exact
866  * filesystem operations performed, and can also control the behavior
867  * of the requester process in otherwise impossible ways.  For example
868  * it can delay the operation for arbitrary length of time allowing
869  * DoS against the requester.
870  *
871  * For this reason only those processes can call into the filesystem,
872  * for which the owner of the mount has ptrace privilege.  This
873  * excludes processes started by other users, suid or sgid processes.
874  */
875 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
876 {
877         const struct cred *cred;
878         int ret;
879
880         if (fc->flags & FUSE_ALLOW_OTHER)
881                 return 1;
882
883         rcu_read_lock();
884         ret = 0;
885         cred = __task_cred(task);
886         if (cred->euid == fc->user_id &&
887             cred->suid == fc->user_id &&
888             cred->uid  == fc->user_id &&
889             cred->egid == fc->group_id &&
890             cred->sgid == fc->group_id &&
891             cred->gid  == fc->group_id)
892                 ret = 1;
893         rcu_read_unlock();
894
895         return ret;
896 }
897
898 static int fuse_access(struct inode *inode, int mask)
899 {
900         struct fuse_conn *fc = get_fuse_conn(inode);
901         struct fuse_req *req;
902         struct fuse_access_in inarg;
903         int err;
904
905         if (fc->no_access)
906                 return 0;
907
908         req = fuse_get_req(fc);
909         if (IS_ERR(req))
910                 return PTR_ERR(req);
911
912         memset(&inarg, 0, sizeof(inarg));
913         inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
914         req->in.h.opcode = FUSE_ACCESS;
915         req->in.h.nodeid = get_node_id(inode);
916         req->in.numargs = 1;
917         req->in.args[0].size = sizeof(inarg);
918         req->in.args[0].value = &inarg;
919         fuse_request_send(fc, req);
920         err = req->out.h.error;
921         fuse_put_request(fc, req);
922         if (err == -ENOSYS) {
923                 fc->no_access = 1;
924                 err = 0;
925         }
926         return err;
927 }
928
929 /*
930  * Check permission.  The two basic access models of FUSE are:
931  *
932  * 1) Local access checking ('default_permissions' mount option) based
933  * on file mode.  This is the plain old disk filesystem permission
934  * modell.
935  *
936  * 2) "Remote" access checking, where server is responsible for
937  * checking permission in each inode operation.  An exception to this
938  * is if ->permission() was invoked from sys_access() in which case an
939  * access request is sent.  Execute permission is still checked
940  * locally based on file mode.
941  */
942 static int fuse_permission(struct inode *inode, int mask)
943 {
944         struct fuse_conn *fc = get_fuse_conn(inode);
945         bool refreshed = false;
946         int err = 0;
947
948         if (!fuse_allow_task(fc, current))
949                 return -EACCES;
950
951         /*
952          * If attributes are needed, refresh them before proceeding
953          */
954         if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
955             ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
956                 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
957                 if (err)
958                         return err;
959         }
960
961         if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
962                 err = generic_permission(inode, mask, NULL);
963
964                 /* If permission is denied, try to refresh file
965                    attributes.  This is also needed, because the root
966                    node will at first have no permissions */
967                 if (err == -EACCES && !refreshed) {
968                         err = fuse_do_getattr(inode, NULL, NULL);
969                         if (!err)
970                                 err = generic_permission(inode, mask, NULL);
971                 }
972
973                 /* Note: the opposite of the above test does not
974                    exist.  So if permissions are revoked this won't be
975                    noticed immediately, only after the attribute
976                    timeout has expired */
977         } else if (mask & MAY_ACCESS) {
978                 err = fuse_access(inode, mask);
979         } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
980                 if (!(inode->i_mode & S_IXUGO)) {
981                         if (refreshed)
982                                 return -EACCES;
983
984                         err = fuse_do_getattr(inode, NULL, NULL);
985                         if (!err && !(inode->i_mode & S_IXUGO))
986                                 return -EACCES;
987                 }
988         }
989         return err;
990 }
991
992 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
993                          void *dstbuf, filldir_t filldir)
994 {
995         while (nbytes >= FUSE_NAME_OFFSET) {
996                 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
997                 size_t reclen = FUSE_DIRENT_SIZE(dirent);
998                 int over;
999                 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1000                         return -EIO;
1001                 if (reclen > nbytes)
1002                         break;
1003
1004                 over = filldir(dstbuf, dirent->name, dirent->namelen,
1005                                file->f_pos, dirent->ino, dirent->type);
1006                 if (over)
1007                         break;
1008
1009                 buf += reclen;
1010                 nbytes -= reclen;
1011                 file->f_pos = dirent->off;
1012         }
1013
1014         return 0;
1015 }
1016
1017 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1018 {
1019         int err;
1020         size_t nbytes;
1021         struct page *page;
1022         struct inode *inode = file->f_path.dentry->d_inode;
1023         struct fuse_conn *fc = get_fuse_conn(inode);
1024         struct fuse_req *req;
1025
1026         if (is_bad_inode(inode))
1027                 return -EIO;
1028
1029         req = fuse_get_req(fc);
1030         if (IS_ERR(req))
1031                 return PTR_ERR(req);
1032
1033         page = alloc_page(GFP_KERNEL);
1034         if (!page) {
1035                 fuse_put_request(fc, req);
1036                 return -ENOMEM;
1037         }
1038         req->out.argpages = 1;
1039         req->num_pages = 1;
1040         req->pages[0] = page;
1041         fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1042         fuse_request_send(fc, req);
1043         nbytes = req->out.args[0].size;
1044         err = req->out.h.error;
1045         fuse_put_request(fc, req);
1046         if (!err)
1047                 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1048                                     filldir);
1049
1050         __free_page(page);
1051         fuse_invalidate_attr(inode); /* atime changed */
1052         return err;
1053 }
1054
1055 static char *read_link(struct dentry *dentry)
1056 {
1057         struct inode *inode = dentry->d_inode;
1058         struct fuse_conn *fc = get_fuse_conn(inode);
1059         struct fuse_req *req = fuse_get_req(fc);
1060         char *link;
1061
1062         if (IS_ERR(req))
1063                 return ERR_CAST(req);
1064
1065         link = (char *) __get_free_page(GFP_KERNEL);
1066         if (!link) {
1067                 link = ERR_PTR(-ENOMEM);
1068                 goto out;
1069         }
1070         req->in.h.opcode = FUSE_READLINK;
1071         req->in.h.nodeid = get_node_id(inode);
1072         req->out.argvar = 1;
1073         req->out.numargs = 1;
1074         req->out.args[0].size = PAGE_SIZE - 1;
1075         req->out.args[0].value = link;
1076         fuse_request_send(fc, req);
1077         if (req->out.h.error) {
1078                 free_page((unsigned long) link);
1079                 link = ERR_PTR(req->out.h.error);
1080         } else
1081                 link[req->out.args[0].size] = '\0';
1082  out:
1083         fuse_put_request(fc, req);
1084         fuse_invalidate_attr(inode); /* atime changed */
1085         return link;
1086 }
1087
1088 static void free_link(char *link)
1089 {
1090         if (!IS_ERR(link))
1091                 free_page((unsigned long) link);
1092 }
1093
1094 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1095 {
1096         nd_set_link(nd, read_link(dentry));
1097         return NULL;
1098 }
1099
1100 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1101 {
1102         free_link(nd_get_link(nd));
1103 }
1104
1105 static int fuse_dir_open(struct inode *inode, struct file *file)
1106 {
1107         return fuse_open_common(inode, file, true);
1108 }
1109
1110 static int fuse_dir_release(struct inode *inode, struct file *file)
1111 {
1112         fuse_release_common(file, FUSE_RELEASEDIR);
1113
1114         return 0;
1115 }
1116
1117 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1118 {
1119         /* nfsd can call this with no file */
1120         return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1121 }
1122
1123 static bool update_mtime(unsigned ivalid)
1124 {
1125         /* Always update if mtime is explicitly set  */
1126         if (ivalid & ATTR_MTIME_SET)
1127                 return true;
1128
1129         /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1130         if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1131                 return false;
1132
1133         /* In all other cases update */
1134         return true;
1135 }
1136
1137 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1138 {
1139         unsigned ivalid = iattr->ia_valid;
1140
1141         if (ivalid & ATTR_MODE)
1142                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1143         if (ivalid & ATTR_UID)
1144                 arg->valid |= FATTR_UID,    arg->uid = iattr->ia_uid;
1145         if (ivalid & ATTR_GID)
1146                 arg->valid |= FATTR_GID,    arg->gid = iattr->ia_gid;
1147         if (ivalid & ATTR_SIZE)
1148                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1149         if (ivalid & ATTR_ATIME) {
1150                 arg->valid |= FATTR_ATIME;
1151                 arg->atime = iattr->ia_atime.tv_sec;
1152                 arg->atimensec = iattr->ia_atime.tv_nsec;
1153                 if (!(ivalid & ATTR_ATIME_SET))
1154                         arg->valid |= FATTR_ATIME_NOW;
1155         }
1156         if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1157                 arg->valid |= FATTR_MTIME;
1158                 arg->mtime = iattr->ia_mtime.tv_sec;
1159                 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1160                 if (!(ivalid & ATTR_MTIME_SET))
1161                         arg->valid |= FATTR_MTIME_NOW;
1162         }
1163 }
1164
1165 /*
1166  * Prevent concurrent writepages on inode
1167  *
1168  * This is done by adding a negative bias to the inode write counter
1169  * and waiting for all pending writes to finish.
1170  */
1171 void fuse_set_nowrite(struct inode *inode)
1172 {
1173         struct fuse_conn *fc = get_fuse_conn(inode);
1174         struct fuse_inode *fi = get_fuse_inode(inode);
1175
1176         BUG_ON(!mutex_is_locked(&inode->i_mutex));
1177
1178         spin_lock(&fc->lock);
1179         BUG_ON(fi->writectr < 0);
1180         fi->writectr += FUSE_NOWRITE;
1181         spin_unlock(&fc->lock);
1182         wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1183 }
1184
1185 /*
1186  * Allow writepages on inode
1187  *
1188  * Remove the bias from the writecounter and send any queued
1189  * writepages.
1190  */
1191 static void __fuse_release_nowrite(struct inode *inode)
1192 {
1193         struct fuse_inode *fi = get_fuse_inode(inode);
1194
1195         BUG_ON(fi->writectr != FUSE_NOWRITE);
1196         fi->writectr = 0;
1197         fuse_flush_writepages(inode);
1198 }
1199
1200 void fuse_release_nowrite(struct inode *inode)
1201 {
1202         struct fuse_conn *fc = get_fuse_conn(inode);
1203
1204         spin_lock(&fc->lock);
1205         __fuse_release_nowrite(inode);
1206         spin_unlock(&fc->lock);
1207 }
1208
1209 /*
1210  * Set attributes, and at the same time refresh them.
1211  *
1212  * Truncation is slightly complicated, because the 'truncate' request
1213  * may fail, in which case we don't want to touch the mapping.
1214  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1215  * and the actual truncation by hand.
1216  */
1217 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1218                            struct file *file)
1219 {
1220         struct inode *inode = entry->d_inode;
1221         struct fuse_conn *fc = get_fuse_conn(inode);
1222         struct fuse_req *req;
1223         struct fuse_setattr_in inarg;
1224         struct fuse_attr_out outarg;
1225         bool is_truncate = false;
1226         loff_t oldsize;
1227         int err;
1228
1229         if (!fuse_allow_task(fc, current))
1230                 return -EACCES;
1231
1232         if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1233                 err = inode_change_ok(inode, attr);
1234                 if (err)
1235                         return err;
1236         }
1237
1238         if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1239                 return 0;
1240
1241         if (attr->ia_valid & ATTR_SIZE) {
1242                 unsigned long limit;
1243                 if (IS_SWAPFILE(inode))
1244                         return -ETXTBSY;
1245                 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1246                 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1247                         send_sig(SIGXFSZ, current, 0);
1248                         return -EFBIG;
1249                 }
1250                 is_truncate = true;
1251         }
1252
1253         req = fuse_get_req(fc);
1254         if (IS_ERR(req))
1255                 return PTR_ERR(req);
1256
1257         if (is_truncate)
1258                 fuse_set_nowrite(inode);
1259
1260         memset(&inarg, 0, sizeof(inarg));
1261         memset(&outarg, 0, sizeof(outarg));
1262         iattr_to_fattr(attr, &inarg);
1263         if (file) {
1264                 struct fuse_file *ff = file->private_data;
1265                 inarg.valid |= FATTR_FH;
1266                 inarg.fh = ff->fh;
1267         }
1268         if (attr->ia_valid & ATTR_SIZE) {
1269                 /* For mandatory locking in truncate */
1270                 inarg.valid |= FATTR_LOCKOWNER;
1271                 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1272         }
1273         req->in.h.opcode = FUSE_SETATTR;
1274         req->in.h.nodeid = get_node_id(inode);
1275         req->in.numargs = 1;
1276         req->in.args[0].size = sizeof(inarg);
1277         req->in.args[0].value = &inarg;
1278         req->out.numargs = 1;
1279         if (fc->minor < 9)
1280                 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1281         else
1282                 req->out.args[0].size = sizeof(outarg);
1283         req->out.args[0].value = &outarg;
1284         fuse_request_send(fc, req);
1285         err = req->out.h.error;
1286         fuse_put_request(fc, req);
1287         if (err) {
1288                 if (err == -EINTR)
1289                         fuse_invalidate_attr(inode);
1290                 goto error;
1291         }
1292
1293         if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1294                 make_bad_inode(inode);
1295                 err = -EIO;
1296                 goto error;
1297         }
1298
1299         spin_lock(&fc->lock);
1300         fuse_change_attributes_common(inode, &outarg.attr,
1301                                       attr_timeout(&outarg));
1302         oldsize = inode->i_size;
1303         i_size_write(inode, outarg.attr.size);
1304
1305         if (is_truncate) {
1306                 /* NOTE: this may release/reacquire fc->lock */
1307                 __fuse_release_nowrite(inode);
1308         }
1309         spin_unlock(&fc->lock);
1310
1311         /*
1312          * Only call invalidate_inode_pages2() after removing
1313          * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1314          */
1315         if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1316                 if (outarg.attr.size < oldsize)
1317                         fuse_truncate(inode->i_mapping, outarg.attr.size);
1318                 invalidate_inode_pages2(inode->i_mapping);
1319         }
1320
1321         return 0;
1322
1323 error:
1324         if (is_truncate)
1325                 fuse_release_nowrite(inode);
1326
1327         return err;
1328 }
1329
1330 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1331 {
1332         if (attr->ia_valid & ATTR_FILE)
1333                 return fuse_do_setattr(entry, attr, attr->ia_file);
1334         else
1335                 return fuse_do_setattr(entry, attr, NULL);
1336 }
1337
1338 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1339                         struct kstat *stat)
1340 {
1341         struct inode *inode = entry->d_inode;
1342         struct fuse_conn *fc = get_fuse_conn(inode);
1343
1344         if (!fuse_allow_task(fc, current))
1345                 return -EACCES;
1346
1347         return fuse_update_attributes(inode, stat, NULL, NULL);
1348 }
1349
1350 static int fuse_setxattr(struct dentry *entry, const char *name,
1351                          const void *value, size_t size, int flags)
1352 {
1353         struct inode *inode = entry->d_inode;
1354         struct fuse_conn *fc = get_fuse_conn(inode);
1355         struct fuse_req *req;
1356         struct fuse_setxattr_in inarg;
1357         int err;
1358
1359         if (fc->no_setxattr)
1360                 return -EOPNOTSUPP;
1361
1362         req = fuse_get_req(fc);
1363         if (IS_ERR(req))
1364                 return PTR_ERR(req);
1365
1366         memset(&inarg, 0, sizeof(inarg));
1367         inarg.size = size;
1368         inarg.flags = flags;
1369         req->in.h.opcode = FUSE_SETXATTR;
1370         req->in.h.nodeid = get_node_id(inode);
1371         req->in.numargs = 3;
1372         req->in.args[0].size = sizeof(inarg);
1373         req->in.args[0].value = &inarg;
1374         req->in.args[1].size = strlen(name) + 1;
1375         req->in.args[1].value = name;
1376         req->in.args[2].size = size;
1377         req->in.args[2].value = value;
1378         fuse_request_send(fc, req);
1379         err = req->out.h.error;
1380         fuse_put_request(fc, req);
1381         if (err == -ENOSYS) {
1382                 fc->no_setxattr = 1;
1383                 err = -EOPNOTSUPP;
1384         }
1385         return err;
1386 }
1387
1388 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1389                              void *value, size_t size)
1390 {
1391         struct inode *inode = entry->d_inode;
1392         struct fuse_conn *fc = get_fuse_conn(inode);
1393         struct fuse_req *req;
1394         struct fuse_getxattr_in inarg;
1395         struct fuse_getxattr_out outarg;
1396         ssize_t ret;
1397
1398         if (fc->no_getxattr)
1399                 return -EOPNOTSUPP;
1400
1401         req = fuse_get_req(fc);
1402         if (IS_ERR(req))
1403                 return PTR_ERR(req);
1404
1405         memset(&inarg, 0, sizeof(inarg));
1406         inarg.size = size;
1407         req->in.h.opcode = FUSE_GETXATTR;
1408         req->in.h.nodeid = get_node_id(inode);
1409         req->in.numargs = 2;
1410         req->in.args[0].size = sizeof(inarg);
1411         req->in.args[0].value = &inarg;
1412         req->in.args[1].size = strlen(name) + 1;
1413         req->in.args[1].value = name;
1414         /* This is really two different operations rolled into one */
1415         req->out.numargs = 1;
1416         if (size) {
1417                 req->out.argvar = 1;
1418                 req->out.args[0].size = size;
1419                 req->out.args[0].value = value;
1420         } else {
1421                 req->out.args[0].size = sizeof(outarg);
1422                 req->out.args[0].value = &outarg;
1423         }
1424         fuse_request_send(fc, req);
1425         ret = req->out.h.error;
1426         if (!ret)
1427                 ret = size ? req->out.args[0].size : outarg.size;
1428         else {
1429                 if (ret == -ENOSYS) {
1430                         fc->no_getxattr = 1;
1431                         ret = -EOPNOTSUPP;
1432                 }
1433         }
1434         fuse_put_request(fc, req);
1435         return ret;
1436 }
1437
1438 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1439 {
1440         struct inode *inode = entry->d_inode;
1441         struct fuse_conn *fc = get_fuse_conn(inode);
1442         struct fuse_req *req;
1443         struct fuse_getxattr_in inarg;
1444         struct fuse_getxattr_out outarg;
1445         ssize_t ret;
1446
1447         if (!fuse_allow_task(fc, current))
1448                 return -EACCES;
1449
1450         if (fc->no_listxattr)
1451                 return -EOPNOTSUPP;
1452
1453         req = fuse_get_req(fc);
1454         if (IS_ERR(req))
1455                 return PTR_ERR(req);
1456
1457         memset(&inarg, 0, sizeof(inarg));
1458         inarg.size = size;
1459         req->in.h.opcode = FUSE_LISTXATTR;
1460         req->in.h.nodeid = get_node_id(inode);
1461         req->in.numargs = 1;
1462         req->in.args[0].size = sizeof(inarg);
1463         req->in.args[0].value = &inarg;
1464         /* This is really two different operations rolled into one */
1465         req->out.numargs = 1;
1466         if (size) {
1467                 req->out.argvar = 1;
1468                 req->out.args[0].size = size;
1469                 req->out.args[0].value = list;
1470         } else {
1471                 req->out.args[0].size = sizeof(outarg);
1472                 req->out.args[0].value = &outarg;
1473         }
1474         fuse_request_send(fc, req);
1475         ret = req->out.h.error;
1476         if (!ret)
1477                 ret = size ? req->out.args[0].size : outarg.size;
1478         else {
1479                 if (ret == -ENOSYS) {
1480                         fc->no_listxattr = 1;
1481                         ret = -EOPNOTSUPP;
1482                 }
1483         }
1484         fuse_put_request(fc, req);
1485         return ret;
1486 }
1487
1488 static int fuse_removexattr(struct dentry *entry, const char *name)
1489 {
1490         struct inode *inode = entry->d_inode;
1491         struct fuse_conn *fc = get_fuse_conn(inode);
1492         struct fuse_req *req;
1493         int err;
1494
1495         if (fc->no_removexattr)
1496                 return -EOPNOTSUPP;
1497
1498         req = fuse_get_req(fc);
1499         if (IS_ERR(req))
1500                 return PTR_ERR(req);
1501
1502         req->in.h.opcode = FUSE_REMOVEXATTR;
1503         req->in.h.nodeid = get_node_id(inode);
1504         req->in.numargs = 1;
1505         req->in.args[0].size = strlen(name) + 1;
1506         req->in.args[0].value = name;
1507         fuse_request_send(fc, req);
1508         err = req->out.h.error;
1509         fuse_put_request(fc, req);
1510         if (err == -ENOSYS) {
1511                 fc->no_removexattr = 1;
1512                 err = -EOPNOTSUPP;
1513         }
1514         return err;
1515 }
1516
1517 static const struct inode_operations fuse_dir_inode_operations = {
1518         .lookup         = fuse_lookup,
1519         .mkdir          = fuse_mkdir,
1520         .symlink        = fuse_symlink,
1521         .unlink         = fuse_unlink,
1522         .rmdir          = fuse_rmdir,
1523         .rename         = fuse_rename,
1524         .link           = fuse_link,
1525         .setattr        = fuse_setattr,
1526         .create         = fuse_create,
1527         .mknod          = fuse_mknod,
1528         .permission     = fuse_permission,
1529         .getattr        = fuse_getattr,
1530         .setxattr       = fuse_setxattr,
1531         .getxattr       = fuse_getxattr,
1532         .listxattr      = fuse_listxattr,
1533         .removexattr    = fuse_removexattr,
1534 };
1535
1536 static const struct file_operations fuse_dir_operations = {
1537         .llseek         = generic_file_llseek,
1538         .read           = generic_read_dir,
1539         .readdir        = fuse_readdir,
1540         .open           = fuse_dir_open,
1541         .release        = fuse_dir_release,
1542         .fsync          = fuse_dir_fsync,
1543 };
1544
1545 static const struct inode_operations fuse_common_inode_operations = {
1546         .setattr        = fuse_setattr,
1547         .permission     = fuse_permission,
1548         .getattr        = fuse_getattr,
1549         .setxattr       = fuse_setxattr,
1550         .getxattr       = fuse_getxattr,
1551         .listxattr      = fuse_listxattr,
1552         .removexattr    = fuse_removexattr,
1553 };
1554
1555 static const struct inode_operations fuse_symlink_inode_operations = {
1556         .setattr        = fuse_setattr,
1557         .follow_link    = fuse_follow_link,
1558         .put_link       = fuse_put_link,
1559         .readlink       = generic_readlink,
1560         .getattr        = fuse_getattr,
1561         .setxattr       = fuse_setxattr,
1562         .getxattr       = fuse_getxattr,
1563         .listxattr      = fuse_listxattr,
1564         .removexattr    = fuse_removexattr,
1565 };
1566
1567 void fuse_init_common(struct inode *inode)
1568 {
1569         inode->i_op = &fuse_common_inode_operations;
1570 }
1571
1572 void fuse_init_dir(struct inode *inode)
1573 {
1574         inode->i_op = &fuse_dir_inode_operations;
1575         inode->i_fop = &fuse_dir_operations;
1576 }
1577
1578 void fuse_init_symlink(struct inode *inode)
1579 {
1580         inode->i_op = &fuse_symlink_inode_operations;
1581 }