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