ia64/linux-2.6.18-xen.hg

view fs/fuse/dir.c @ 524:7f8b544237bf

netfront: Allow netfront in domain 0.

This is useful if your physical network device is in a utility domain.

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