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