[PATCH] fuse: fix zero timeout
[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 /*
18  * FUSE caches dentries and attributes with separate timeout.  The
19  * time in jiffies until the dentry/attributes are valid is stored in
20  * dentry->d_time and fuse_inode->i_time respectively.
21  */
22
23 /*
24  * Calculate the time in jiffies until a dentry/attributes are valid
25  */
26 static unsigned long time_to_jiffies(unsigned long sec, unsigned long nsec)
27 {
28         if (sec || nsec) {
29                 struct timespec ts = {sec, nsec};
30                 return jiffies + timespec_to_jiffies(&ts);
31         } else
32                 return jiffies - 1;
33 }
34
35 /*
36  * Set dentry and possibly attribute timeouts from the lookup/mk*
37  * replies
38  */
39 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
40 {
41         entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
42         if (entry->d_inode)
43                 get_fuse_inode(entry->d_inode)->i_time =
44                         time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
45 }
46
47 /*
48  * Mark the attributes as stale, so that at the next call to
49  * ->getattr() they will be fetched from userspace
50  */
51 void fuse_invalidate_attr(struct inode *inode)
52 {
53         get_fuse_inode(inode)->i_time = jiffies - 1;
54 }
55
56 /*
57  * Just mark the entry as stale, so that a next attempt to look it up
58  * will result in a new lookup call to userspace
59  *
60  * This is called when a dentry is about to become negative and the
61  * timeout is unknown (unlink, rmdir, rename and in some cases
62  * lookup)
63  */
64 static void fuse_invalidate_entry_cache(struct dentry *entry)
65 {
66         entry->d_time = jiffies - 1;
67 }
68
69 /*
70  * Same as fuse_invalidate_entry_cache(), but also try to remove the
71  * dentry from the hash
72  */
73 static void fuse_invalidate_entry(struct dentry *entry)
74 {
75         d_invalidate(entry);
76         fuse_invalidate_entry_cache(entry);
77 }
78
79 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
80                              struct dentry *entry,
81                              struct fuse_entry_out *outarg)
82 {
83         req->in.h.opcode = FUSE_LOOKUP;
84         req->in.h.nodeid = get_node_id(dir);
85         req->in.numargs = 1;
86         req->in.args[0].size = entry->d_name.len + 1;
87         req->in.args[0].value = entry->d_name.name;
88         req->out.numargs = 1;
89         req->out.args[0].size = sizeof(struct fuse_entry_out);
90         req->out.args[0].value = outarg;
91 }
92
93 /*
94  * Check whether the dentry is still valid
95  *
96  * If the entry validity timeout has expired and the dentry is
97  * positive, try to redo the lookup.  If the lookup results in a
98  * different inode, then let the VFS invalidate the dentry and redo
99  * the lookup once more.  If the lookup results in the same inode,
100  * then refresh the attributes, timeouts and mark the dentry valid.
101  */
102 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
103 {
104         struct inode *inode = entry->d_inode;
105
106         if (inode && is_bad_inode(inode))
107                 return 0;
108         else if (time_after(jiffies, entry->d_time)) {
109                 int err;
110                 struct fuse_entry_out outarg;
111                 struct fuse_conn *fc;
112                 struct fuse_req *req;
113
114                 /* Doesn't hurt to "reset" the validity timeout */
115                 fuse_invalidate_entry_cache(entry);
116
117                 /* For negative dentries, always do a fresh lookup */
118                 if (!inode)
119                         return 0;
120
121                 fc = get_fuse_conn(inode);
122                 req = fuse_get_req(fc);
123                 if (IS_ERR(req))
124                         return 0;
125
126                 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
127                 request_send(fc, req);
128                 err = req->out.h.error;
129                 /* Zero nodeid is same as -ENOENT */
130                 if (!err && !outarg.nodeid)
131                         err = -ENOENT;
132                 if (!err) {
133                         struct fuse_inode *fi = get_fuse_inode(inode);
134                         if (outarg.nodeid != get_node_id(inode)) {
135                                 fuse_send_forget(fc, req, outarg.nodeid, 1);
136                                 return 0;
137                         }
138                         fi->nlookup ++;
139                 }
140                 fuse_put_request(fc, req);
141                 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
142                         return 0;
143
144                 fuse_change_attributes(inode, &outarg.attr);
145                 fuse_change_timeout(entry, &outarg);
146         }
147         return 1;
148 }
149
150 /*
151  * Check if there's already a hashed alias of this directory inode.
152  * If yes, then lookup and mkdir must not create a new alias.
153  */
154 static int dir_alias(struct inode *inode)
155 {
156         if (S_ISDIR(inode->i_mode)) {
157                 struct dentry *alias = d_find_alias(inode);
158                 if (alias) {
159                         dput(alias);
160                         return 1;
161                 }
162         }
163         return 0;
164 }
165
166 static int invalid_nodeid(u64 nodeid)
167 {
168         return !nodeid || nodeid == FUSE_ROOT_ID;
169 }
170
171 static struct dentry_operations fuse_dentry_operations = {
172         .d_revalidate   = fuse_dentry_revalidate,
173 };
174
175 static int valid_mode(int m)
176 {
177         return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
178                 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
179 }
180
181 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
182                                   struct nameidata *nd)
183 {
184         int err;
185         struct fuse_entry_out outarg;
186         struct inode *inode = NULL;
187         struct fuse_conn *fc = get_fuse_conn(dir);
188         struct fuse_req *req;
189
190         if (entry->d_name.len > FUSE_NAME_MAX)
191                 return ERR_PTR(-ENAMETOOLONG);
192
193         req = fuse_get_req(fc);
194         if (IS_ERR(req))
195                 return ERR_PTR(PTR_ERR(req));
196
197         fuse_lookup_init(req, dir, entry, &outarg);
198         request_send(fc, req);
199         err = req->out.h.error;
200         /* Zero nodeid is same as -ENOENT, but with valid timeout */
201         if (!err && outarg.nodeid &&
202             (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
203                 err = -EIO;
204         if (!err && outarg.nodeid) {
205                 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
206                                   &outarg.attr);
207                 if (!inode) {
208                         fuse_send_forget(fc, req, outarg.nodeid, 1);
209                         return ERR_PTR(-ENOMEM);
210                 }
211         }
212         fuse_put_request(fc, req);
213         if (err && err != -ENOENT)
214                 return ERR_PTR(err);
215
216         if (inode && dir_alias(inode)) {
217                 iput(inode);
218                 return ERR_PTR(-EIO);
219         }
220         d_add(entry, inode);
221         entry->d_op = &fuse_dentry_operations;
222         if (!err)
223                 fuse_change_timeout(entry, &outarg);
224         else
225                 fuse_invalidate_entry_cache(entry);
226         return NULL;
227 }
228
229 /*
230  * Synchronous release for the case when something goes wrong in CREATE_OPEN
231  */
232 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
233                               u64 nodeid, int flags)
234 {
235         struct fuse_req *req;
236
237         req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
238         req->force = 1;
239         request_send(fc, req);
240         fuse_put_request(fc, req);
241 }
242
243 /*
244  * Atomic create+open operation
245  *
246  * If the filesystem doesn't support this, then fall back to separate
247  * 'mknod' + 'open' requests.
248  */
249 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
250                             struct nameidata *nd)
251 {
252         int err;
253         struct inode *inode;
254         struct fuse_conn *fc = get_fuse_conn(dir);
255         struct fuse_req *req;
256         struct fuse_req *forget_req;
257         struct fuse_open_in inarg;
258         struct fuse_open_out outopen;
259         struct fuse_entry_out outentry;
260         struct fuse_file *ff;
261         struct file *file;
262         int flags = nd->intent.open.flags - 1;
263
264         if (fc->no_create)
265                 return -ENOSYS;
266
267         forget_req = fuse_get_req(fc);
268         if (IS_ERR(forget_req))
269                 return PTR_ERR(forget_req);
270
271         req = fuse_get_req(fc);
272         err = PTR_ERR(req);
273         if (IS_ERR(req))
274                 goto out_put_forget_req;
275
276         err = -ENOMEM;
277         ff = fuse_file_alloc();
278         if (!ff)
279                 goto out_put_request;
280
281         flags &= ~O_NOCTTY;
282         memset(&inarg, 0, sizeof(inarg));
283         inarg.flags = flags;
284         inarg.mode = mode;
285         req->in.h.opcode = FUSE_CREATE;
286         req->in.h.nodeid = get_node_id(dir);
287         req->in.numargs = 2;
288         req->in.args[0].size = sizeof(inarg);
289         req->in.args[0].value = &inarg;
290         req->in.args[1].size = entry->d_name.len + 1;
291         req->in.args[1].value = entry->d_name.name;
292         req->out.numargs = 2;
293         req->out.args[0].size = sizeof(outentry);
294         req->out.args[0].value = &outentry;
295         req->out.args[1].size = sizeof(outopen);
296         req->out.args[1].value = &outopen;
297         request_send(fc, req);
298         err = req->out.h.error;
299         if (err) {
300                 if (err == -ENOSYS)
301                         fc->no_create = 1;
302                 goto out_free_ff;
303         }
304
305         err = -EIO;
306         if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
307                 goto out_free_ff;
308
309         fuse_put_request(fc, req);
310         inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
311                           &outentry.attr);
312         if (!inode) {
313                 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
314                 ff->fh = outopen.fh;
315                 fuse_sync_release(fc, ff, outentry.nodeid, flags);
316                 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
317                 return -ENOMEM;
318         }
319         fuse_put_request(fc, forget_req);
320         d_instantiate(entry, inode);
321         fuse_change_timeout(entry, &outentry);
322         file = lookup_instantiate_filp(nd, entry, generic_file_open);
323         if (IS_ERR(file)) {
324                 ff->fh = outopen.fh;
325                 fuse_sync_release(fc, ff, outentry.nodeid, flags);
326                 return PTR_ERR(file);
327         }
328         fuse_finish_open(inode, file, ff, &outopen);
329         return 0;
330
331  out_free_ff:
332         fuse_file_free(ff);
333  out_put_request:
334         fuse_put_request(fc, req);
335  out_put_forget_req:
336         fuse_put_request(fc, forget_req);
337         return err;
338 }
339
340 /*
341  * Code shared between mknod, mkdir, symlink and link
342  */
343 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
344                             struct inode *dir, struct dentry *entry,
345                             int mode)
346 {
347         struct fuse_entry_out outarg;
348         struct inode *inode;
349         int err;
350
351         req->in.h.nodeid = get_node_id(dir);
352         req->out.numargs = 1;
353         req->out.args[0].size = sizeof(outarg);
354         req->out.args[0].value = &outarg;
355         request_send(fc, req);
356         err = req->out.h.error;
357         if (err) {
358                 fuse_put_request(fc, req);
359                 return err;
360         }
361         err = -EIO;
362         if (invalid_nodeid(outarg.nodeid))
363                 goto out_put_request;
364
365         if ((outarg.attr.mode ^ mode) & S_IFMT)
366                 goto out_put_request;
367
368         inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
369                           &outarg.attr);
370         if (!inode) {
371                 fuse_send_forget(fc, req, outarg.nodeid, 1);
372                 return -ENOMEM;
373         }
374         fuse_put_request(fc, req);
375
376         if (dir_alias(inode)) {
377                 iput(inode);
378                 return -EIO;
379         }
380
381         d_instantiate(entry, inode);
382         fuse_change_timeout(entry, &outarg);
383         fuse_invalidate_attr(dir);
384         return 0;
385
386  out_put_request:
387         fuse_put_request(fc, req);
388         return err;
389 }
390
391 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
392                       dev_t rdev)
393 {
394         struct fuse_mknod_in inarg;
395         struct fuse_conn *fc = get_fuse_conn(dir);
396         struct fuse_req *req = fuse_get_req(fc);
397         if (IS_ERR(req))
398                 return PTR_ERR(req);
399
400         memset(&inarg, 0, sizeof(inarg));
401         inarg.mode = mode;
402         inarg.rdev = new_encode_dev(rdev);
403         req->in.h.opcode = FUSE_MKNOD;
404         req->in.numargs = 2;
405         req->in.args[0].size = sizeof(inarg);
406         req->in.args[0].value = &inarg;
407         req->in.args[1].size = entry->d_name.len + 1;
408         req->in.args[1].value = entry->d_name.name;
409         return create_new_entry(fc, req, dir, entry, mode);
410 }
411
412 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
413                        struct nameidata *nd)
414 {
415         if (nd && (nd->flags & LOOKUP_CREATE)) {
416                 int err = fuse_create_open(dir, entry, mode, nd);
417                 if (err != -ENOSYS)
418                         return err;
419                 /* Fall back on mknod */
420         }
421         return fuse_mknod(dir, entry, mode, 0);
422 }
423
424 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
425 {
426         struct fuse_mkdir_in inarg;
427         struct fuse_conn *fc = get_fuse_conn(dir);
428         struct fuse_req *req = fuse_get_req(fc);
429         if (IS_ERR(req))
430                 return PTR_ERR(req);
431
432         memset(&inarg, 0, sizeof(inarg));
433         inarg.mode = mode;
434         req->in.h.opcode = FUSE_MKDIR;
435         req->in.numargs = 2;
436         req->in.args[0].size = sizeof(inarg);
437         req->in.args[0].value = &inarg;
438         req->in.args[1].size = entry->d_name.len + 1;
439         req->in.args[1].value = entry->d_name.name;
440         return create_new_entry(fc, req, dir, entry, S_IFDIR);
441 }
442
443 static int fuse_symlink(struct inode *dir, struct dentry *entry,
444                         const char *link)
445 {
446         struct fuse_conn *fc = get_fuse_conn(dir);
447         unsigned len = strlen(link) + 1;
448         struct fuse_req *req = fuse_get_req(fc);
449         if (IS_ERR(req))
450                 return PTR_ERR(req);
451
452         req->in.h.opcode = FUSE_SYMLINK;
453         req->in.numargs = 2;
454         req->in.args[0].size = entry->d_name.len + 1;
455         req->in.args[0].value = entry->d_name.name;
456         req->in.args[1].size = len;
457         req->in.args[1].value = link;
458         return create_new_entry(fc, req, dir, entry, S_IFLNK);
459 }
460
461 static int fuse_unlink(struct inode *dir, struct dentry *entry)
462 {
463         int err;
464         struct fuse_conn *fc = get_fuse_conn(dir);
465         struct fuse_req *req = fuse_get_req(fc);
466         if (IS_ERR(req))
467                 return PTR_ERR(req);
468
469         req->in.h.opcode = FUSE_UNLINK;
470         req->in.h.nodeid = get_node_id(dir);
471         req->in.numargs = 1;
472         req->in.args[0].size = entry->d_name.len + 1;
473         req->in.args[0].value = entry->d_name.name;
474         request_send(fc, req);
475         err = req->out.h.error;
476         fuse_put_request(fc, req);
477         if (!err) {
478                 struct inode *inode = entry->d_inode;
479
480                 /* Set nlink to zero so the inode can be cleared, if
481                    the inode does have more links this will be
482                    discovered at the next lookup/getattr */
483                 inode->i_nlink = 0;
484                 fuse_invalidate_attr(inode);
485                 fuse_invalidate_attr(dir);
486                 fuse_invalidate_entry_cache(entry);
487         } else if (err == -EINTR)
488                 fuse_invalidate_entry(entry);
489         return err;
490 }
491
492 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
493 {
494         int err;
495         struct fuse_conn *fc = get_fuse_conn(dir);
496         struct fuse_req *req = fuse_get_req(fc);
497         if (IS_ERR(req))
498                 return PTR_ERR(req);
499
500         req->in.h.opcode = FUSE_RMDIR;
501         req->in.h.nodeid = get_node_id(dir);
502         req->in.numargs = 1;
503         req->in.args[0].size = entry->d_name.len + 1;
504         req->in.args[0].value = entry->d_name.name;
505         request_send(fc, req);
506         err = req->out.h.error;
507         fuse_put_request(fc, req);
508         if (!err) {
509                 entry->d_inode->i_nlink = 0;
510                 fuse_invalidate_attr(dir);
511                 fuse_invalidate_entry_cache(entry);
512         } else if (err == -EINTR)
513                 fuse_invalidate_entry(entry);
514         return err;
515 }
516
517 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
518                        struct inode *newdir, struct dentry *newent)
519 {
520         int err;
521         struct fuse_rename_in inarg;
522         struct fuse_conn *fc = get_fuse_conn(olddir);
523         struct fuse_req *req = fuse_get_req(fc);
524         if (IS_ERR(req))
525                 return PTR_ERR(req);
526
527         memset(&inarg, 0, sizeof(inarg));
528         inarg.newdir = get_node_id(newdir);
529         req->in.h.opcode = FUSE_RENAME;
530         req->in.h.nodeid = get_node_id(olddir);
531         req->in.numargs = 3;
532         req->in.args[0].size = sizeof(inarg);
533         req->in.args[0].value = &inarg;
534         req->in.args[1].size = oldent->d_name.len + 1;
535         req->in.args[1].value = oldent->d_name.name;
536         req->in.args[2].size = newent->d_name.len + 1;
537         req->in.args[2].value = newent->d_name.name;
538         request_send(fc, req);
539         err = req->out.h.error;
540         fuse_put_request(fc, req);
541         if (!err) {
542                 fuse_invalidate_attr(olddir);
543                 if (olddir != newdir)
544                         fuse_invalidate_attr(newdir);
545
546                 /* newent will end up negative */
547                 if (newent->d_inode)
548                         fuse_invalidate_entry_cache(newent);
549         } else if (err == -EINTR) {
550                 /* If request was interrupted, DEITY only knows if the
551                    rename actually took place.  If the invalidation
552                    fails (e.g. some process has CWD under the renamed
553                    directory), then there can be inconsistency between
554                    the dcache and the real filesystem.  Tough luck. */
555                 fuse_invalidate_entry(oldent);
556                 if (newent->d_inode)
557                         fuse_invalidate_entry(newent);
558         }
559
560         return err;
561 }
562
563 static int fuse_link(struct dentry *entry, struct inode *newdir,
564                      struct dentry *newent)
565 {
566         int err;
567         struct fuse_link_in inarg;
568         struct inode *inode = entry->d_inode;
569         struct fuse_conn *fc = get_fuse_conn(inode);
570         struct fuse_req *req = fuse_get_req(fc);
571         if (IS_ERR(req))
572                 return PTR_ERR(req);
573
574         memset(&inarg, 0, sizeof(inarg));
575         inarg.oldnodeid = get_node_id(inode);
576         req->in.h.opcode = FUSE_LINK;
577         req->in.numargs = 2;
578         req->in.args[0].size = sizeof(inarg);
579         req->in.args[0].value = &inarg;
580         req->in.args[1].size = newent->d_name.len + 1;
581         req->in.args[1].value = newent->d_name.name;
582         err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
583         /* Contrary to "normal" filesystems it can happen that link
584            makes two "logical" inodes point to the same "physical"
585            inode.  We invalidate the attributes of the old one, so it
586            will reflect changes in the backing inode (link count,
587            etc.)
588         */
589         if (!err || err == -EINTR)
590                 fuse_invalidate_attr(inode);
591         return err;
592 }
593
594 int fuse_do_getattr(struct inode *inode)
595 {
596         int err;
597         struct fuse_attr_out arg;
598         struct fuse_conn *fc = get_fuse_conn(inode);
599         struct fuse_req *req = fuse_get_req(fc);
600         if (IS_ERR(req))
601                 return PTR_ERR(req);
602
603         req->in.h.opcode = FUSE_GETATTR;
604         req->in.h.nodeid = get_node_id(inode);
605         req->out.numargs = 1;
606         req->out.args[0].size = sizeof(arg);
607         req->out.args[0].value = &arg;
608         request_send(fc, req);
609         err = req->out.h.error;
610         fuse_put_request(fc, req);
611         if (!err) {
612                 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
613                         make_bad_inode(inode);
614                         err = -EIO;
615                 } else {
616                         struct fuse_inode *fi = get_fuse_inode(inode);
617                         fuse_change_attributes(inode, &arg.attr);
618                         fi->i_time = time_to_jiffies(arg.attr_valid,
619                                                      arg.attr_valid_nsec);
620                 }
621         }
622         return err;
623 }
624
625 /*
626  * Calling into a user-controlled filesystem gives the filesystem
627  * daemon ptrace-like capabilities over the requester process.  This
628  * means, that the filesystem daemon is able to record the exact
629  * filesystem operations performed, and can also control the behavior
630  * of the requester process in otherwise impossible ways.  For example
631  * it can delay the operation for arbitrary length of time allowing
632  * DoS against the requester.
633  *
634  * For this reason only those processes can call into the filesystem,
635  * for which the owner of the mount has ptrace privilege.  This
636  * excludes processes started by other users, suid or sgid processes.
637  */
638 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
639 {
640         if (fc->flags & FUSE_ALLOW_OTHER)
641                 return 1;
642
643         if (task->euid == fc->user_id &&
644             task->suid == fc->user_id &&
645             task->uid == fc->user_id &&
646             task->egid == fc->group_id &&
647             task->sgid == fc->group_id &&
648             task->gid == fc->group_id)
649                 return 1;
650
651         return 0;
652 }
653
654 /*
655  * Check whether the inode attributes are still valid
656  *
657  * If the attribute validity timeout has expired, then fetch the fresh
658  * attributes with a 'getattr' request
659  *
660  * I'm not sure why cached attributes are never returned for the root
661  * inode, this is probably being too cautious.
662  */
663 static int fuse_revalidate(struct dentry *entry)
664 {
665         struct inode *inode = entry->d_inode;
666         struct fuse_inode *fi = get_fuse_inode(inode);
667         struct fuse_conn *fc = get_fuse_conn(inode);
668
669         if (!fuse_allow_task(fc, current))
670                 return -EACCES;
671         if (get_node_id(inode) != FUSE_ROOT_ID &&
672             time_before_eq(jiffies, fi->i_time))
673                 return 0;
674
675         return fuse_do_getattr(inode);
676 }
677
678 static int fuse_access(struct inode *inode, int mask)
679 {
680         struct fuse_conn *fc = get_fuse_conn(inode);
681         struct fuse_req *req;
682         struct fuse_access_in inarg;
683         int err;
684
685         if (fc->no_access)
686                 return 0;
687
688         req = fuse_get_req(fc);
689         if (IS_ERR(req))
690                 return PTR_ERR(req);
691
692         memset(&inarg, 0, sizeof(inarg));
693         inarg.mask = mask;
694         req->in.h.opcode = FUSE_ACCESS;
695         req->in.h.nodeid = get_node_id(inode);
696         req->in.numargs = 1;
697         req->in.args[0].size = sizeof(inarg);
698         req->in.args[0].value = &inarg;
699         request_send(fc, req);
700         err = req->out.h.error;
701         fuse_put_request(fc, req);
702         if (err == -ENOSYS) {
703                 fc->no_access = 1;
704                 err = 0;
705         }
706         return err;
707 }
708
709 /*
710  * Check permission.  The two basic access models of FUSE are:
711  *
712  * 1) Local access checking ('default_permissions' mount option) based
713  * on file mode.  This is the plain old disk filesystem permission
714  * modell.
715  *
716  * 2) "Remote" access checking, where server is responsible for
717  * checking permission in each inode operation.  An exception to this
718  * is if ->permission() was invoked from sys_access() in which case an
719  * access request is sent.  Execute permission is still checked
720  * locally based on file mode.
721  */
722 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
723 {
724         struct fuse_conn *fc = get_fuse_conn(inode);
725
726         if (!fuse_allow_task(fc, current))
727                 return -EACCES;
728         else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
729                 int err = generic_permission(inode, mask, NULL);
730
731                 /* If permission is denied, try to refresh file
732                    attributes.  This is also needed, because the root
733                    node will at first have no permissions */
734                 if (err == -EACCES) {
735                         err = fuse_do_getattr(inode);
736                         if (!err)
737                                 err = generic_permission(inode, mask, NULL);
738                 }
739
740                 /* Note: the opposite of the above test does not
741                    exist.  So if permissions are revoked this won't be
742                    noticed immediately, only after the attribute
743                    timeout has expired */
744
745                 return err;
746         } else {
747                 int mode = inode->i_mode;
748                 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
749                         return -EACCES;
750
751                 if (nd && (nd->flags & LOOKUP_ACCESS))
752                         return fuse_access(inode, mask);
753                 return 0;
754         }
755 }
756
757 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
758                          void *dstbuf, filldir_t filldir)
759 {
760         while (nbytes >= FUSE_NAME_OFFSET) {
761                 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
762                 size_t reclen = FUSE_DIRENT_SIZE(dirent);
763                 int over;
764                 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
765                         return -EIO;
766                 if (reclen > nbytes)
767                         break;
768
769                 over = filldir(dstbuf, dirent->name, dirent->namelen,
770                                file->f_pos, dirent->ino, dirent->type);
771                 if (over)
772                         break;
773
774                 buf += reclen;
775                 nbytes -= reclen;
776                 file->f_pos = dirent->off;
777         }
778
779         return 0;
780 }
781
782 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
783 {
784         int err;
785         size_t nbytes;
786         struct page *page;
787         struct inode *inode = file->f_dentry->d_inode;
788         struct fuse_conn *fc = get_fuse_conn(inode);
789         struct fuse_req *req;
790
791         if (is_bad_inode(inode))
792                 return -EIO;
793
794         req = fuse_get_req(fc);
795         if (IS_ERR(req))
796                 return PTR_ERR(req);
797
798         page = alloc_page(GFP_KERNEL);
799         if (!page) {
800                 fuse_put_request(fc, req);
801                 return -ENOMEM;
802         }
803         req->num_pages = 1;
804         req->pages[0] = page;
805         fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
806         request_send(fc, req);
807         nbytes = req->out.args[0].size;
808         err = req->out.h.error;
809         fuse_put_request(fc, req);
810         if (!err)
811                 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
812                                     filldir);
813
814         __free_page(page);
815         fuse_invalidate_attr(inode); /* atime changed */
816         return err;
817 }
818
819 static char *read_link(struct dentry *dentry)
820 {
821         struct inode *inode = dentry->d_inode;
822         struct fuse_conn *fc = get_fuse_conn(inode);
823         struct fuse_req *req = fuse_get_req(fc);
824         char *link;
825
826         if (IS_ERR(req))
827                 return ERR_PTR(PTR_ERR(req));
828
829         link = (char *) __get_free_page(GFP_KERNEL);
830         if (!link) {
831                 link = ERR_PTR(-ENOMEM);
832                 goto out;
833         }
834         req->in.h.opcode = FUSE_READLINK;
835         req->in.h.nodeid = get_node_id(inode);
836         req->out.argvar = 1;
837         req->out.numargs = 1;
838         req->out.args[0].size = PAGE_SIZE - 1;
839         req->out.args[0].value = link;
840         request_send(fc, req);
841         if (req->out.h.error) {
842                 free_page((unsigned long) link);
843                 link = ERR_PTR(req->out.h.error);
844         } else
845                 link[req->out.args[0].size] = '\0';
846  out:
847         fuse_put_request(fc, req);
848         fuse_invalidate_attr(inode); /* atime changed */
849         return link;
850 }
851
852 static void free_link(char *link)
853 {
854         if (!IS_ERR(link))
855                 free_page((unsigned long) link);
856 }
857
858 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
859 {
860         nd_set_link(nd, read_link(dentry));
861         return NULL;
862 }
863
864 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
865 {
866         free_link(nd_get_link(nd));
867 }
868
869 static int fuse_dir_open(struct inode *inode, struct file *file)
870 {
871         return fuse_open_common(inode, file, 1);
872 }
873
874 static int fuse_dir_release(struct inode *inode, struct file *file)
875 {
876         return fuse_release_common(inode, file, 1);
877 }
878
879 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
880 {
881         /* nfsd can call this with no file */
882         return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
883 }
884
885 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
886 {
887         unsigned ivalid = iattr->ia_valid;
888
889         if (ivalid & ATTR_MODE)
890                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
891         if (ivalid & ATTR_UID)
892                 arg->valid |= FATTR_UID,    arg->uid = iattr->ia_uid;
893         if (ivalid & ATTR_GID)
894                 arg->valid |= FATTR_GID,    arg->gid = iattr->ia_gid;
895         if (ivalid & ATTR_SIZE)
896                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
897         /* You can only _set_ these together (they may change by themselves) */
898         if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
899                 arg->valid |= FATTR_ATIME | FATTR_MTIME;
900                 arg->atime = iattr->ia_atime.tv_sec;
901                 arg->mtime = iattr->ia_mtime.tv_sec;
902         }
903         if (ivalid & ATTR_FILE) {
904                 struct fuse_file *ff = iattr->ia_file->private_data;
905                 arg->valid |= FATTR_FH;
906                 arg->fh = ff->fh;
907         }
908 }
909
910 /*
911  * Set attributes, and at the same time refresh them.
912  *
913  * Truncation is slightly complicated, because the 'truncate' request
914  * may fail, in which case we don't want to touch the mapping.
915  * vmtruncate() doesn't allow for this case.  So do the rlimit
916  * checking by hand and call vmtruncate() only after the file has
917  * actually been truncated.
918  */
919 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
920 {
921         struct inode *inode = entry->d_inode;
922         struct fuse_conn *fc = get_fuse_conn(inode);
923         struct fuse_inode *fi = get_fuse_inode(inode);
924         struct fuse_req *req;
925         struct fuse_setattr_in inarg;
926         struct fuse_attr_out outarg;
927         int err;
928         int is_truncate = 0;
929
930         if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
931                 err = inode_change_ok(inode, attr);
932                 if (err)
933                         return err;
934         }
935
936         if (attr->ia_valid & ATTR_SIZE) {
937                 unsigned long limit;
938                 is_truncate = 1;
939                 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
940                 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
941                         send_sig(SIGXFSZ, current, 0);
942                         return -EFBIG;
943                 }
944         }
945
946         req = fuse_get_req(fc);
947         if (IS_ERR(req))
948                 return PTR_ERR(req);
949
950         memset(&inarg, 0, sizeof(inarg));
951         iattr_to_fattr(attr, &inarg);
952         req->in.h.opcode = FUSE_SETATTR;
953         req->in.h.nodeid = get_node_id(inode);
954         req->in.numargs = 1;
955         req->in.args[0].size = sizeof(inarg);
956         req->in.args[0].value = &inarg;
957         req->out.numargs = 1;
958         req->out.args[0].size = sizeof(outarg);
959         req->out.args[0].value = &outarg;
960         request_send(fc, req);
961         err = req->out.h.error;
962         fuse_put_request(fc, req);
963         if (!err) {
964                 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
965                         make_bad_inode(inode);
966                         err = -EIO;
967                 } else {
968                         if (is_truncate) {
969                                 loff_t origsize = i_size_read(inode);
970                                 i_size_write(inode, outarg.attr.size);
971                                 if (origsize > outarg.attr.size)
972                                         vmtruncate(inode, outarg.attr.size);
973                         }
974                         fuse_change_attributes(inode, &outarg.attr);
975                         fi->i_time = time_to_jiffies(outarg.attr_valid,
976                                                      outarg.attr_valid_nsec);
977                 }
978         } else if (err == -EINTR)
979                 fuse_invalidate_attr(inode);
980
981         return err;
982 }
983
984 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
985                         struct kstat *stat)
986 {
987         struct inode *inode = entry->d_inode;
988         int err = fuse_revalidate(entry);
989         if (!err)
990                 generic_fillattr(inode, stat);
991
992         return err;
993 }
994
995 static int fuse_setxattr(struct dentry *entry, const char *name,
996                          const void *value, size_t size, int flags)
997 {
998         struct inode *inode = entry->d_inode;
999         struct fuse_conn *fc = get_fuse_conn(inode);
1000         struct fuse_req *req;
1001         struct fuse_setxattr_in inarg;
1002         int err;
1003
1004         if (fc->no_setxattr)
1005                 return -EOPNOTSUPP;
1006
1007         req = fuse_get_req(fc);
1008         if (IS_ERR(req))
1009                 return PTR_ERR(req);
1010
1011         memset(&inarg, 0, sizeof(inarg));
1012         inarg.size = size;
1013         inarg.flags = flags;
1014         req->in.h.opcode = FUSE_SETXATTR;
1015         req->in.h.nodeid = get_node_id(inode);
1016         req->in.numargs = 3;
1017         req->in.args[0].size = sizeof(inarg);
1018         req->in.args[0].value = &inarg;
1019         req->in.args[1].size = strlen(name) + 1;
1020         req->in.args[1].value = name;
1021         req->in.args[2].size = size;
1022         req->in.args[2].value = value;
1023         request_send(fc, req);
1024         err = req->out.h.error;
1025         fuse_put_request(fc, req);
1026         if (err == -ENOSYS) {
1027                 fc->no_setxattr = 1;
1028                 err = -EOPNOTSUPP;
1029         }
1030         return err;
1031 }
1032
1033 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1034                              void *value, size_t size)
1035 {
1036         struct inode *inode = entry->d_inode;
1037         struct fuse_conn *fc = get_fuse_conn(inode);
1038         struct fuse_req *req;
1039         struct fuse_getxattr_in inarg;
1040         struct fuse_getxattr_out outarg;
1041         ssize_t ret;
1042
1043         if (fc->no_getxattr)
1044                 return -EOPNOTSUPP;
1045
1046         req = fuse_get_req(fc);
1047         if (IS_ERR(req))
1048                 return PTR_ERR(req);
1049
1050         memset(&inarg, 0, sizeof(inarg));
1051         inarg.size = size;
1052         req->in.h.opcode = FUSE_GETXATTR;
1053         req->in.h.nodeid = get_node_id(inode);
1054         req->in.numargs = 2;
1055         req->in.args[0].size = sizeof(inarg);
1056         req->in.args[0].value = &inarg;
1057         req->in.args[1].size = strlen(name) + 1;
1058         req->in.args[1].value = name;
1059         /* This is really two different operations rolled into one */
1060         req->out.numargs = 1;
1061         if (size) {
1062                 req->out.argvar = 1;
1063                 req->out.args[0].size = size;
1064                 req->out.args[0].value = value;
1065         } else {
1066                 req->out.args[0].size = sizeof(outarg);
1067                 req->out.args[0].value = &outarg;
1068         }
1069         request_send(fc, req);
1070         ret = req->out.h.error;
1071         if (!ret)
1072                 ret = size ? req->out.args[0].size : outarg.size;
1073         else {
1074                 if (ret == -ENOSYS) {
1075                         fc->no_getxattr = 1;
1076                         ret = -EOPNOTSUPP;
1077                 }
1078         }
1079         fuse_put_request(fc, req);
1080         return ret;
1081 }
1082
1083 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1084 {
1085         struct inode *inode = entry->d_inode;
1086         struct fuse_conn *fc = get_fuse_conn(inode);
1087         struct fuse_req *req;
1088         struct fuse_getxattr_in inarg;
1089         struct fuse_getxattr_out outarg;
1090         ssize_t ret;
1091
1092         if (fc->no_listxattr)
1093                 return -EOPNOTSUPP;
1094
1095         req = fuse_get_req(fc);
1096         if (IS_ERR(req))
1097                 return PTR_ERR(req);
1098
1099         memset(&inarg, 0, sizeof(inarg));
1100         inarg.size = size;
1101         req->in.h.opcode = FUSE_LISTXATTR;
1102         req->in.h.nodeid = get_node_id(inode);
1103         req->in.numargs = 1;
1104         req->in.args[0].size = sizeof(inarg);
1105         req->in.args[0].value = &inarg;
1106         /* This is really two different operations rolled into one */
1107         req->out.numargs = 1;
1108         if (size) {
1109                 req->out.argvar = 1;
1110                 req->out.args[0].size = size;
1111                 req->out.args[0].value = list;
1112         } else {
1113                 req->out.args[0].size = sizeof(outarg);
1114                 req->out.args[0].value = &outarg;
1115         }
1116         request_send(fc, req);
1117         ret = req->out.h.error;
1118         if (!ret)
1119                 ret = size ? req->out.args[0].size : outarg.size;
1120         else {
1121                 if (ret == -ENOSYS) {
1122                         fc->no_listxattr = 1;
1123                         ret = -EOPNOTSUPP;
1124                 }
1125         }
1126         fuse_put_request(fc, req);
1127         return ret;
1128 }
1129
1130 static int fuse_removexattr(struct dentry *entry, const char *name)
1131 {
1132         struct inode *inode = entry->d_inode;
1133         struct fuse_conn *fc = get_fuse_conn(inode);
1134         struct fuse_req *req;
1135         int err;
1136
1137         if (fc->no_removexattr)
1138                 return -EOPNOTSUPP;
1139
1140         req = fuse_get_req(fc);
1141         if (IS_ERR(req))
1142                 return PTR_ERR(req);
1143
1144         req->in.h.opcode = FUSE_REMOVEXATTR;
1145         req->in.h.nodeid = get_node_id(inode);
1146         req->in.numargs = 1;
1147         req->in.args[0].size = strlen(name) + 1;
1148         req->in.args[0].value = name;
1149         request_send(fc, req);
1150         err = req->out.h.error;
1151         fuse_put_request(fc, req);
1152         if (err == -ENOSYS) {
1153                 fc->no_removexattr = 1;
1154                 err = -EOPNOTSUPP;
1155         }
1156         return err;
1157 }
1158
1159 static struct inode_operations fuse_dir_inode_operations = {
1160         .lookup         = fuse_lookup,
1161         .mkdir          = fuse_mkdir,
1162         .symlink        = fuse_symlink,
1163         .unlink         = fuse_unlink,
1164         .rmdir          = fuse_rmdir,
1165         .rename         = fuse_rename,
1166         .link           = fuse_link,
1167         .setattr        = fuse_setattr,
1168         .create         = fuse_create,
1169         .mknod          = fuse_mknod,
1170         .permission     = fuse_permission,
1171         .getattr        = fuse_getattr,
1172         .setxattr       = fuse_setxattr,
1173         .getxattr       = fuse_getxattr,
1174         .listxattr      = fuse_listxattr,
1175         .removexattr    = fuse_removexattr,
1176 };
1177
1178 static const struct file_operations fuse_dir_operations = {
1179         .llseek         = generic_file_llseek,
1180         .read           = generic_read_dir,
1181         .readdir        = fuse_readdir,
1182         .open           = fuse_dir_open,
1183         .release        = fuse_dir_release,
1184         .fsync          = fuse_dir_fsync,
1185 };
1186
1187 static struct inode_operations fuse_common_inode_operations = {
1188         .setattr        = fuse_setattr,
1189         .permission     = fuse_permission,
1190         .getattr        = fuse_getattr,
1191         .setxattr       = fuse_setxattr,
1192         .getxattr       = fuse_getxattr,
1193         .listxattr      = fuse_listxattr,
1194         .removexattr    = fuse_removexattr,
1195 };
1196
1197 static struct inode_operations fuse_symlink_inode_operations = {
1198         .setattr        = fuse_setattr,
1199         .follow_link    = fuse_follow_link,
1200         .put_link       = fuse_put_link,
1201         .readlink       = generic_readlink,
1202         .getattr        = fuse_getattr,
1203         .setxattr       = fuse_setxattr,
1204         .getxattr       = fuse_getxattr,
1205         .listxattr      = fuse_listxattr,
1206         .removexattr    = fuse_removexattr,
1207 };
1208
1209 void fuse_init_common(struct inode *inode)
1210 {
1211         inode->i_op = &fuse_common_inode_operations;
1212 }
1213
1214 void fuse_init_dir(struct inode *inode)
1215 {
1216         inode->i_op = &fuse_dir_inode_operations;
1217         inode->i_fop = &fuse_dir_operations;
1218 }
1219
1220 void fuse_init_symlink(struct inode *inode)
1221 {
1222         inode->i_op = &fuse_symlink_inode_operations;
1223 }