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