Drop support for kernels earlier than 2.6.9
[fuse.git] / kernel / dir.c
blobc238674e1185ca8549d6649b392e297b4ef487aa
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)
20 entry->d_time = time;
23 static inline u64 fuse_dentry_time(struct dentry *entry)
25 return entry->d_time;
27 #else
29 * On 32 bit archs store the high 32 bits of time in d_fsdata
31 static void fuse_dentry_settime(struct dentry *entry, u64 time)
33 entry->d_time = time;
34 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
37 static u64 fuse_dentry_time(struct dentry *entry)
39 return (u64) entry->d_time +
40 ((u64) (unsigned long) entry->d_fsdata << 32);
42 #endif
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.
51 * Calculate the time in jiffies until a dentry/attributes are valid
53 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
55 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
57 return get_jiffies_64() + timespec_to_jiffies(&ts);
58 } else
59 return 0;
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
66 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
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);
76 * Mark the attributes as stale, so that at the next call to
77 * ->getattr() they will be fetched from userspace
79 void fuse_invalidate_attr(struct inode *inode)
81 get_fuse_inode(inode)->i_time = 0;
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
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)
92 static void fuse_invalidate_entry_cache(struct dentry *entry)
94 fuse_dentry_settime(entry, 0);
98 * Same as fuse_invalidate_entry_cache(), but also try to remove the
99 * dentry from the hash
101 static void fuse_invalidate_entry(struct dentry *entry)
103 d_invalidate(entry);
104 fuse_invalidate_entry_cache(entry);
107 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
108 struct dentry *entry,
109 struct fuse_entry_out *outarg)
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;
122 * Check whether the dentry is still valid
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.
130 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
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;
142 /* Doesn't hurt to "reset" the validity timeout */
143 fuse_invalidate_entry_cache(entry);
145 /* For negative dentries, always do a fresh lookup */
146 if (!inode)
147 return 0;
149 fc = get_fuse_conn(inode);
150 req = fuse_get_req(fc);
151 if (IS_ERR(req))
152 return 0;
154 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
155 request_send(fc, req);
156 err = req->out.h.error;
157 /* Zero nodeid is same as -ENOENT */
158 if (!err && !outarg.nodeid)
159 err = -ENOENT;
160 if (!err) {
161 struct fuse_inode *fi = get_fuse_inode(inode);
162 if (outarg.nodeid != get_node_id(inode)) {
163 fuse_send_forget(fc, req, outarg.nodeid, 1);
164 return 0;
166 fi->nlookup ++;
168 fuse_put_request(fc, req);
169 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
170 return 0;
172 fuse_change_attributes(inode, &outarg.attr);
173 fuse_change_timeout(entry, &outarg);
175 return 1;
179 * Check if there's already a hashed alias of this directory inode.
180 * If yes, then lookup and mkdir must not create a new alias.
182 static int dir_alias(struct inode *inode)
184 if (S_ISDIR(inode->i_mode)) {
185 struct dentry *alias = d_find_alias(inode);
186 #if defined(FUSE_MAINLINE)
187 if (alias) {
188 dput(alias);
189 return 1;
191 #else
192 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
193 dput(alias);
194 return 1;
196 dput(alias);
197 #endif
199 return 0;
202 static int invalid_nodeid(u64 nodeid)
204 return !nodeid || nodeid == FUSE_ROOT_ID;
207 static struct dentry_operations fuse_dentry_operations = {
208 .d_revalidate = fuse_dentry_revalidate,
211 static int valid_mode(int m)
213 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
214 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
217 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
218 struct nameidata *nd)
220 int err;
221 struct fuse_entry_out outarg;
222 struct inode *inode = NULL;
223 #if !defined(FUSE_MAINLINE)
224 struct dentry *newent;
225 #endif
226 struct fuse_conn *fc = get_fuse_conn(dir);
227 struct fuse_req *req;
229 if (entry->d_name.len > FUSE_NAME_MAX)
230 return ERR_PTR(-ENAMETOOLONG);
232 req = fuse_get_req(fc);
233 if (IS_ERR(req))
234 return ERR_PTR(PTR_ERR(req));
236 fuse_lookup_init(req, dir, entry, &outarg);
237 request_send(fc, req);
238 err = req->out.h.error;
239 /* Zero nodeid is same as -ENOENT, but with valid timeout */
240 if (!err && outarg.nodeid &&
241 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
242 err = -EIO;
243 if (!err && outarg.nodeid) {
244 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
245 &outarg.attr);
246 if (!inode) {
247 fuse_send_forget(fc, req, outarg.nodeid, 1);
248 return ERR_PTR(-ENOMEM);
251 fuse_put_request(fc, req);
252 if (err && err != -ENOENT)
253 return ERR_PTR(err);
255 if (inode && dir_alias(inode)) {
256 iput(inode);
257 return ERR_PTR(-EIO);
259 #if defined(FUSE_MAINLINE)
260 d_add(entry, inode);
261 #else
262 newent = d_splice_alias(inode, entry);
263 entry = newent ? newent : entry;
264 #endif
265 entry->d_op = &fuse_dentry_operations;
266 if (!err)
267 fuse_change_timeout(entry, &outarg);
268 else
269 fuse_invalidate_entry_cache(entry);
270 #if defined(FUSE_MAINLINE)
271 return NULL;
272 #else
273 return newent;
274 #endif
277 #ifdef HAVE_LOOKUP_INSTANTIATE_FILP
279 * Synchronous release for the case when something goes wrong in CREATE_OPEN
281 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
282 u64 nodeid, int flags)
284 struct fuse_req *req;
286 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
287 req->force = 1;
288 request_send(fc, req);
289 fuse_put_request(fc, req);
293 * Atomic create+open operation
295 * If the filesystem doesn't support this, then fall back to separate
296 * 'mknod' + 'open' requests.
298 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
299 struct nameidata *nd)
301 int err;
302 struct inode *inode;
303 struct fuse_conn *fc = get_fuse_conn(dir);
304 struct fuse_req *req;
305 struct fuse_req *forget_req;
306 struct fuse_open_in inarg;
307 struct fuse_open_out outopen;
308 struct fuse_entry_out outentry;
309 struct fuse_file *ff;
310 struct file *file;
311 int flags = nd->intent.open.flags - 1;
313 if (fc->no_create)
314 return -ENOSYS;
316 forget_req = fuse_get_req(fc);
317 if (IS_ERR(forget_req))
318 return PTR_ERR(forget_req);
320 req = fuse_get_req(fc);
321 err = PTR_ERR(req);
322 if (IS_ERR(req))
323 goto out_put_forget_req;
325 err = -ENOMEM;
326 ff = fuse_file_alloc();
327 if (!ff)
328 goto out_put_request;
330 flags &= ~O_NOCTTY;
331 memset(&inarg, 0, sizeof(inarg));
332 inarg.flags = flags;
333 inarg.mode = mode;
334 req->in.h.opcode = FUSE_CREATE;
335 req->in.h.nodeid = get_node_id(dir);
336 req->in.numargs = 2;
337 req->in.args[0].size = sizeof(inarg);
338 req->in.args[0].value = &inarg;
339 req->in.args[1].size = entry->d_name.len + 1;
340 req->in.args[1].value = entry->d_name.name;
341 req->out.numargs = 2;
342 req->out.args[0].size = sizeof(outentry);
343 req->out.args[0].value = &outentry;
344 req->out.args[1].size = sizeof(outopen);
345 req->out.args[1].value = &outopen;
346 request_send(fc, req);
347 err = req->out.h.error;
348 if (err) {
349 if (err == -ENOSYS)
350 fc->no_create = 1;
351 goto out_free_ff;
354 err = -EIO;
355 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
356 goto out_free_ff;
358 fuse_put_request(fc, req);
359 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
360 &outentry.attr);
361 if (!inode) {
362 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
363 ff->fh = outopen.fh;
364 fuse_sync_release(fc, ff, outentry.nodeid, flags);
365 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
366 return -ENOMEM;
368 fuse_put_request(fc, forget_req);
369 d_instantiate(entry, inode);
370 fuse_change_timeout(entry, &outentry);
371 file = lookup_instantiate_filp(nd, entry, generic_file_open);
372 if (IS_ERR(file)) {
373 ff->fh = outopen.fh;
374 fuse_sync_release(fc, ff, outentry.nodeid, flags);
375 return PTR_ERR(file);
377 fuse_finish_open(inode, file, ff, &outopen);
378 return 0;
380 out_free_ff:
381 fuse_file_free(ff);
382 out_put_request:
383 fuse_put_request(fc, req);
384 out_put_forget_req:
385 fuse_put_request(fc, forget_req);
386 return err;
388 #endif
391 * Code shared between mknod, mkdir, symlink and link
393 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
394 struct inode *dir, struct dentry *entry,
395 int mode)
397 struct fuse_entry_out outarg;
398 struct inode *inode;
399 int err;
401 req->in.h.nodeid = get_node_id(dir);
402 req->out.numargs = 1;
403 req->out.args[0].size = sizeof(outarg);
404 req->out.args[0].value = &outarg;
405 request_send(fc, req);
406 err = req->out.h.error;
407 if (err) {
408 fuse_put_request(fc, req);
409 return err;
411 err = -EIO;
412 if (invalid_nodeid(outarg.nodeid))
413 goto out_put_request;
415 if ((outarg.attr.mode ^ mode) & S_IFMT)
416 goto out_put_request;
418 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
419 &outarg.attr);
420 if (!inode) {
421 fuse_send_forget(fc, req, outarg.nodeid, 1);
422 return -ENOMEM;
424 fuse_put_request(fc, req);
426 if (dir_alias(inode)) {
427 iput(inode);
428 return -EIO;
431 d_instantiate(entry, inode);
432 fuse_change_timeout(entry, &outarg);
433 fuse_invalidate_attr(dir);
434 return 0;
436 out_put_request:
437 fuse_put_request(fc, req);
438 return err;
441 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
442 dev_t rdev)
444 struct fuse_mknod_in inarg;
445 struct fuse_conn *fc = get_fuse_conn(dir);
446 struct fuse_req *req = fuse_get_req(fc);
447 if (IS_ERR(req))
448 return PTR_ERR(req);
450 memset(&inarg, 0, sizeof(inarg));
451 inarg.mode = mode;
452 inarg.rdev = new_encode_dev(rdev);
453 req->in.h.opcode = FUSE_MKNOD;
454 req->in.numargs = 2;
455 req->in.args[0].size = sizeof(inarg);
456 req->in.args[0].value = &inarg;
457 req->in.args[1].size = entry->d_name.len + 1;
458 req->in.args[1].value = entry->d_name.name;
459 return create_new_entry(fc, req, dir, entry, mode);
462 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
463 struct nameidata *nd)
465 #ifdef HAVE_LOOKUP_INSTANTIATE_FILP
466 if (nd && (nd->flags & LOOKUP_CREATE)) {
467 int err = fuse_create_open(dir, entry, mode, nd);
468 if (err != -ENOSYS)
469 return err;
470 /* Fall back on mknod */
472 #endif
473 return fuse_mknod(dir, entry, mode, 0);
476 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
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);
495 static int fuse_symlink(struct inode *dir, struct dentry *entry,
496 const char *link)
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);
513 static int fuse_unlink(struct inode *dir, struct dentry *entry)
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;
544 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
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;
569 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
570 struct inode *newdir, struct dentry *newent)
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);
612 return err;
615 static int fuse_link(struct dentry *entry, struct inode *newdir,
616 struct dentry *newent)
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.)
641 if (!err || err == -EINTR)
642 fuse_invalidate_attr(inode);
643 return err;
646 int fuse_do_getattr(struct inode *inode)
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 #ifndef KERNEL_2_6_12_PLUS
666 if (get_node_id(inode) != FUSE_ROOT_ID)
667 make_bad_inode(inode);
668 #else
669 make_bad_inode(inode);
670 #endif
671 err = -EIO;
672 } else {
673 struct fuse_inode *fi = get_fuse_inode(inode);
674 fuse_change_attributes(inode, &arg.attr);
675 fi->i_time = time_to_jiffies(arg.attr_valid,
676 arg.attr_valid_nsec);
679 return err;
683 * Calling into a user-controlled filesystem gives the filesystem
684 * daemon ptrace-like capabilities over the requester process. This
685 * means, that the filesystem daemon is able to record the exact
686 * filesystem operations performed, and can also control the behavior
687 * of the requester process in otherwise impossible ways. For example
688 * it can delay the operation for arbitrary length of time allowing
689 * DoS against the requester.
691 * For this reason only those processes can call into the filesystem,
692 * for which the owner of the mount has ptrace privilege. This
693 * excludes processes started by other users, suid or sgid processes.
695 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
697 if (fc->flags & FUSE_ALLOW_OTHER)
698 return 1;
700 if (task->euid == fc->user_id &&
701 task->suid == fc->user_id &&
702 task->uid == fc->user_id &&
703 task->egid == fc->group_id &&
704 task->sgid == fc->group_id &&
705 task->gid == fc->group_id)
706 return 1;
708 return 0;
712 * Check whether the inode attributes are still valid
714 * If the attribute validity timeout has expired, then fetch the fresh
715 * attributes with a 'getattr' request
717 * I'm not sure why cached attributes are never returned for the root
718 * inode, this is probably being too cautious.
720 static int fuse_revalidate(struct dentry *entry)
722 struct inode *inode = entry->d_inode;
723 struct fuse_inode *fi = get_fuse_inode(inode);
724 struct fuse_conn *fc = get_fuse_conn(inode);
726 if (!fuse_allow_task(fc, current))
727 return -EACCES;
728 if (get_node_id(inode) != FUSE_ROOT_ID &&
729 fi->i_time >= get_jiffies_64())
730 return 0;
732 return fuse_do_getattr(inode);
735 static int fuse_access(struct inode *inode, int mask)
737 struct fuse_conn *fc = get_fuse_conn(inode);
738 struct fuse_req *req;
739 struct fuse_access_in inarg;
740 int err;
742 if (fc->no_access)
743 return 0;
745 req = fuse_get_req(fc);
746 if (IS_ERR(req))
747 return PTR_ERR(req);
749 memset(&inarg, 0, sizeof(inarg));
750 inarg.mask = mask;
751 req->in.h.opcode = FUSE_ACCESS;
752 req->in.h.nodeid = get_node_id(inode);
753 req->in.numargs = 1;
754 req->in.args[0].size = sizeof(inarg);
755 req->in.args[0].value = &inarg;
756 request_send(fc, req);
757 err = req->out.h.error;
758 fuse_put_request(fc, req);
759 if (err == -ENOSYS) {
760 fc->no_access = 1;
761 err = 0;
763 return err;
767 * Check permission. The two basic access models of FUSE are:
769 * 1) Local access checking ('default_permissions' mount option) based
770 * on file mode. This is the plain old disk filesystem permission
771 * modell.
773 * 2) "Remote" access checking, where server is responsible for
774 * checking permission in each inode operation. An exception to this
775 * is if ->permission() was invoked from sys_access() in which case an
776 * access request is sent. Execute permission is still checked
777 * locally based on file mode.
779 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
781 struct fuse_conn *fc = get_fuse_conn(inode);
783 if (!fuse_allow_task(fc, current))
784 return -EACCES;
785 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
786 #ifdef KERNEL_2_6_10_PLUS
787 int err = generic_permission(inode, mask, NULL);
788 #else
789 int err = vfs_permission(inode, mask);
790 #endif
792 /* If permission is denied, try to refresh file
793 attributes. This is also needed, because the root
794 node will at first have no permissions */
795 if (err == -EACCES) {
796 err = fuse_do_getattr(inode);
797 if (!err)
798 #ifdef KERNEL_2_6_10_PLUS
799 err = generic_permission(inode, mask, NULL);
800 #else
801 err = vfs_permission(inode, mask);
802 #endif
805 /* Note: the opposite of the above test does not
806 exist. So if permissions are revoked this won't be
807 noticed immediately, only after the attribute
808 timeout has expired */
810 return err;
811 } else {
812 int mode = inode->i_mode;
813 #ifndef KERNEL_2_6_11_PLUS
814 if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&
815 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
816 return -EROFS;
817 #endif
818 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
819 return -EACCES;
821 if (nd && (nd->flags & LOOKUP_ACCESS))
822 return fuse_access(inode, mask);
823 return 0;
827 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
828 void *dstbuf, filldir_t filldir)
830 while (nbytes >= FUSE_NAME_OFFSET) {
831 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
832 size_t reclen = FUSE_DIRENT_SIZE(dirent);
833 int over;
834 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
835 return -EIO;
836 if (reclen > nbytes)
837 break;
839 over = filldir(dstbuf, dirent->name, dirent->namelen,
840 file->f_pos, dirent->ino, dirent->type);
841 if (over)
842 break;
844 buf += reclen;
845 nbytes -= reclen;
846 file->f_pos = dirent->off;
849 return 0;
852 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
854 int err;
855 size_t nbytes;
856 struct page *page;
857 struct inode *inode = file->f_dentry->d_inode;
858 struct fuse_conn *fc = get_fuse_conn(inode);
859 struct fuse_req *req;
861 if (is_bad_inode(inode))
862 return -EIO;
864 req = fuse_get_req(fc);
865 if (IS_ERR(req))
866 return PTR_ERR(req);
868 page = alloc_page(GFP_KERNEL);
869 if (!page) {
870 fuse_put_request(fc, req);
871 return -ENOMEM;
873 req->num_pages = 1;
874 req->pages[0] = page;
875 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
876 request_send(fc, req);
877 nbytes = req->out.args[0].size;
878 err = req->out.h.error;
879 fuse_put_request(fc, req);
880 if (!err)
881 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
882 filldir);
884 __free_page(page);
885 fuse_invalidate_attr(inode); /* atime changed */
886 return err;
889 static char *read_link(struct dentry *dentry)
891 struct inode *inode = dentry->d_inode;
892 struct fuse_conn *fc = get_fuse_conn(inode);
893 struct fuse_req *req = fuse_get_req(fc);
894 char *link;
896 if (IS_ERR(req))
897 return ERR_PTR(PTR_ERR(req));
899 link = (char *) __get_free_page(GFP_KERNEL);
900 if (!link) {
901 link = ERR_PTR(-ENOMEM);
902 goto out;
904 req->in.h.opcode = FUSE_READLINK;
905 req->in.h.nodeid = get_node_id(inode);
906 req->out.argvar = 1;
907 req->out.numargs = 1;
908 req->out.args[0].size = PAGE_SIZE - 1;
909 req->out.args[0].value = link;
910 request_send(fc, req);
911 if (req->out.h.error) {
912 free_page((unsigned long) link);
913 link = ERR_PTR(req->out.h.error);
914 } else
915 link[req->out.args[0].size] = '\0';
916 out:
917 fuse_put_request(fc, req);
918 fuse_invalidate_attr(inode); /* atime changed */
919 return link;
922 static void free_link(char *link)
924 if (!IS_ERR(link))
925 free_page((unsigned long) link);
928 #ifdef KERNEL_2_6_13_PLUS
929 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
931 nd_set_link(nd, read_link(dentry));
932 return NULL;
935 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
937 free_link(nd_get_link(nd));
939 #else
940 static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
942 nd_set_link(nd, read_link(dentry));
943 return 0;
946 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd)
948 free_link(nd_get_link(nd));
950 #endif
952 static int fuse_dir_open(struct inode *inode, struct file *file)
954 return fuse_open_common(inode, file, 1);
957 static int fuse_dir_release(struct inode *inode, struct file *file)
959 return fuse_release_common(inode, file, 1);
962 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
964 /* nfsd can call this with no file */
965 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
968 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
970 unsigned ivalid = iattr->ia_valid;
972 if (ivalid & ATTR_MODE)
973 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
974 if (ivalid & ATTR_UID)
975 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
976 if (ivalid & ATTR_GID)
977 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
978 if (ivalid & ATTR_SIZE)
979 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
980 /* You can only _set_ these together (they may change by themselves) */
981 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
982 arg->valid |= FATTR_ATIME | FATTR_MTIME;
983 arg->atime = iattr->ia_atime.tv_sec;
984 arg->mtime = iattr->ia_mtime.tv_sec;
986 #ifdef ATTR_FILE
987 if (ivalid & ATTR_FILE) {
988 struct fuse_file *ff = iattr->ia_file->private_data;
989 arg->valid |= FATTR_FH;
990 arg->fh = ff->fh;
992 #endif
996 * Set attributes, and at the same time refresh them.
998 * Truncation is slightly complicated, because the 'truncate' request
999 * may fail, in which case we don't want to touch the mapping.
1000 * vmtruncate() doesn't allow for this case. So do the rlimit
1001 * checking by hand and call vmtruncate() only after the file has
1002 * actually been truncated.
1004 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1006 struct inode *inode = entry->d_inode;
1007 struct fuse_conn *fc = get_fuse_conn(inode);
1008 struct fuse_inode *fi = get_fuse_inode(inode);
1009 struct fuse_req *req;
1010 struct fuse_setattr_in inarg;
1011 struct fuse_attr_out outarg;
1012 int err;
1013 int is_truncate = 0;
1015 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1016 err = inode_change_ok(inode, attr);
1017 if (err)
1018 return err;
1021 if (attr->ia_valid & ATTR_SIZE) {
1022 unsigned long limit;
1023 is_truncate = 1;
1024 #ifdef KERNEL_2_6_10_PLUS
1025 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1026 #else
1027 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1028 #endif
1029 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1030 send_sig(SIGXFSZ, current, 0);
1031 return -EFBIG;
1035 req = fuse_get_req(fc);
1036 if (IS_ERR(req))
1037 return PTR_ERR(req);
1039 memset(&inarg, 0, sizeof(inarg));
1040 iattr_to_fattr(attr, &inarg);
1041 req->in.h.opcode = FUSE_SETATTR;
1042 req->in.h.nodeid = get_node_id(inode);
1043 req->in.numargs = 1;
1044 req->in.args[0].size = sizeof(inarg);
1045 req->in.args[0].value = &inarg;
1046 req->out.numargs = 1;
1047 req->out.args[0].size = sizeof(outarg);
1048 req->out.args[0].value = &outarg;
1049 request_send(fc, req);
1050 err = req->out.h.error;
1051 fuse_put_request(fc, req);
1052 if (!err) {
1053 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1054 #ifndef KERNEL_2_6_12_PLUS
1055 if (get_node_id(inode) != FUSE_ROOT_ID)
1056 make_bad_inode(inode);
1057 #else
1058 make_bad_inode(inode);
1059 #endif
1060 err = -EIO;
1061 } else {
1062 if (is_truncate) {
1063 loff_t origsize = i_size_read(inode);
1064 i_size_write(inode, outarg.attr.size);
1065 if (origsize > outarg.attr.size)
1066 vmtruncate(inode, outarg.attr.size);
1068 fuse_change_attributes(inode, &outarg.attr);
1069 fi->i_time = time_to_jiffies(outarg.attr_valid,
1070 outarg.attr_valid_nsec);
1072 } else if (err == -EINTR)
1073 fuse_invalidate_attr(inode);
1075 return err;
1078 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1079 struct kstat *stat)
1081 struct inode *inode = entry->d_inode;
1082 int err = fuse_revalidate(entry);
1083 if (!err)
1084 generic_fillattr(inode, stat);
1086 return err;
1089 static int fuse_setxattr(struct dentry *entry, const char *name,
1090 const void *value, size_t size, int flags)
1092 struct inode *inode = entry->d_inode;
1093 struct fuse_conn *fc = get_fuse_conn(inode);
1094 struct fuse_req *req;
1095 struct fuse_setxattr_in inarg;
1096 int err;
1098 if (fc->no_setxattr)
1099 return -EOPNOTSUPP;
1101 req = fuse_get_req(fc);
1102 if (IS_ERR(req))
1103 return PTR_ERR(req);
1105 memset(&inarg, 0, sizeof(inarg));
1106 inarg.size = size;
1107 inarg.flags = flags;
1108 req->in.h.opcode = FUSE_SETXATTR;
1109 req->in.h.nodeid = get_node_id(inode);
1110 req->in.numargs = 3;
1111 req->in.args[0].size = sizeof(inarg);
1112 req->in.args[0].value = &inarg;
1113 req->in.args[1].size = strlen(name) + 1;
1114 req->in.args[1].value = name;
1115 req->in.args[2].size = size;
1116 req->in.args[2].value = value;
1117 request_send(fc, req);
1118 err = req->out.h.error;
1119 fuse_put_request(fc, req);
1120 if (err == -ENOSYS) {
1121 fc->no_setxattr = 1;
1122 err = -EOPNOTSUPP;
1124 return err;
1127 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1128 void *value, size_t size)
1130 struct inode *inode = entry->d_inode;
1131 struct fuse_conn *fc = get_fuse_conn(inode);
1132 struct fuse_req *req;
1133 struct fuse_getxattr_in inarg;
1134 struct fuse_getxattr_out outarg;
1135 ssize_t ret;
1137 if (fc->no_getxattr)
1138 return -EOPNOTSUPP;
1140 req = fuse_get_req(fc);
1141 if (IS_ERR(req))
1142 return PTR_ERR(req);
1144 memset(&inarg, 0, sizeof(inarg));
1145 inarg.size = size;
1146 req->in.h.opcode = FUSE_GETXATTR;
1147 req->in.h.nodeid = get_node_id(inode);
1148 req->in.numargs = 2;
1149 req->in.args[0].size = sizeof(inarg);
1150 req->in.args[0].value = &inarg;
1151 req->in.args[1].size = strlen(name) + 1;
1152 req->in.args[1].value = name;
1153 /* This is really two different operations rolled into one */
1154 req->out.numargs = 1;
1155 if (size) {
1156 req->out.argvar = 1;
1157 req->out.args[0].size = size;
1158 req->out.args[0].value = value;
1159 } else {
1160 req->out.args[0].size = sizeof(outarg);
1161 req->out.args[0].value = &outarg;
1163 request_send(fc, req);
1164 ret = req->out.h.error;
1165 if (!ret)
1166 ret = size ? req->out.args[0].size : outarg.size;
1167 else {
1168 if (ret == -ENOSYS) {
1169 fc->no_getxattr = 1;
1170 ret = -EOPNOTSUPP;
1173 fuse_put_request(fc, req);
1174 return ret;
1177 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1179 struct inode *inode = entry->d_inode;
1180 struct fuse_conn *fc = get_fuse_conn(inode);
1181 struct fuse_req *req;
1182 struct fuse_getxattr_in inarg;
1183 struct fuse_getxattr_out outarg;
1184 ssize_t ret;
1186 if (fc->no_listxattr)
1187 return -EOPNOTSUPP;
1189 req = fuse_get_req(fc);
1190 if (IS_ERR(req))
1191 return PTR_ERR(req);
1193 memset(&inarg, 0, sizeof(inarg));
1194 inarg.size = size;
1195 req->in.h.opcode = FUSE_LISTXATTR;
1196 req->in.h.nodeid = get_node_id(inode);
1197 req->in.numargs = 1;
1198 req->in.args[0].size = sizeof(inarg);
1199 req->in.args[0].value = &inarg;
1200 /* This is really two different operations rolled into one */
1201 req->out.numargs = 1;
1202 if (size) {
1203 req->out.argvar = 1;
1204 req->out.args[0].size = size;
1205 req->out.args[0].value = list;
1206 } else {
1207 req->out.args[0].size = sizeof(outarg);
1208 req->out.args[0].value = &outarg;
1210 request_send(fc, req);
1211 ret = req->out.h.error;
1212 if (!ret)
1213 ret = size ? req->out.args[0].size : outarg.size;
1214 else {
1215 if (ret == -ENOSYS) {
1216 fc->no_listxattr = 1;
1217 ret = -EOPNOTSUPP;
1220 fuse_put_request(fc, req);
1221 return ret;
1224 static int fuse_removexattr(struct dentry *entry, const char *name)
1226 struct inode *inode = entry->d_inode;
1227 struct fuse_conn *fc = get_fuse_conn(inode);
1228 struct fuse_req *req;
1229 int err;
1231 if (fc->no_removexattr)
1232 return -EOPNOTSUPP;
1234 req = fuse_get_req(fc);
1235 if (IS_ERR(req))
1236 return PTR_ERR(req);
1238 req->in.h.opcode = FUSE_REMOVEXATTR;
1239 req->in.h.nodeid = get_node_id(inode);
1240 req->in.numargs = 1;
1241 req->in.args[0].size = strlen(name) + 1;
1242 req->in.args[0].value = name;
1243 request_send(fc, req);
1244 err = req->out.h.error;
1245 fuse_put_request(fc, req);
1246 if (err == -ENOSYS) {
1247 fc->no_removexattr = 1;
1248 err = -EOPNOTSUPP;
1250 return err;
1253 static struct inode_operations fuse_dir_inode_operations = {
1254 .lookup = fuse_lookup,
1255 .mkdir = fuse_mkdir,
1256 .symlink = fuse_symlink,
1257 .unlink = fuse_unlink,
1258 .rmdir = fuse_rmdir,
1259 .rename = fuse_rename,
1260 .link = fuse_link,
1261 .setattr = fuse_setattr,
1262 .create = fuse_create,
1263 .mknod = fuse_mknod,
1264 .permission = fuse_permission,
1265 .getattr = fuse_getattr,
1266 .setxattr = fuse_setxattr,
1267 .getxattr = fuse_getxattr,
1268 .listxattr = fuse_listxattr,
1269 .removexattr = fuse_removexattr,
1272 static struct file_operations fuse_dir_operations = {
1273 .llseek = generic_file_llseek,
1274 .read = generic_read_dir,
1275 .readdir = fuse_readdir,
1276 .open = fuse_dir_open,
1277 .release = fuse_dir_release,
1278 .fsync = fuse_dir_fsync,
1281 static struct inode_operations fuse_common_inode_operations = {
1282 .setattr = fuse_setattr,
1283 .permission = fuse_permission,
1284 .getattr = fuse_getattr,
1285 .setxattr = fuse_setxattr,
1286 .getxattr = fuse_getxattr,
1287 .listxattr = fuse_listxattr,
1288 .removexattr = fuse_removexattr,
1291 static struct inode_operations fuse_symlink_inode_operations = {
1292 .setattr = fuse_setattr,
1293 .follow_link = fuse_follow_link,
1294 .put_link = fuse_put_link,
1295 .readlink = generic_readlink,
1296 .getattr = fuse_getattr,
1297 .setxattr = fuse_setxattr,
1298 .getxattr = fuse_getxattr,
1299 .listxattr = fuse_listxattr,
1300 .removexattr = fuse_removexattr,
1303 void fuse_init_common(struct inode *inode)
1305 inode->i_op = &fuse_common_inode_operations;
1308 void fuse_init_dir(struct inode *inode)
1310 inode->i_op = &fuse_dir_inode_operations;
1311 inode->i_fop = &fuse_dir_operations;
1314 void fuse_init_symlink(struct inode *inode)
1316 inode->i_op = &fuse_symlink_inode_operations;