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.
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
)
23 static inline u64
fuse_dentry_time(struct dentry
*entry
)
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
)
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);
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
)
56 struct timespec ts
= {sec
, nsec
};
57 return get_jiffies_64() + timespec_to_jiffies(&ts
);
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
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
));
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
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
)
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
);
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
))
136 else if (fuse_dentry_time(entry
) < get_jiffies_64()) {
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 */
149 fc
= get_fuse_conn(inode
);
150 req
= fuse_get_req(fc
);
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
)
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);
168 fuse_put_request(fc
, req
);
169 if (err
|| (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
172 fuse_change_attributes(inode
, &outarg
.attr
);
173 fuse_change_timeout(entry
, &outarg
);
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)
192 if (alias
&& !(alias
->d_flags
& DCACHE_DISCONNECTED
)) {
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
)
221 struct fuse_entry_out outarg
;
222 struct inode
*inode
= NULL
;
223 #if !defined(FUSE_MAINLINE)
224 struct dentry
*newent
;
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
);
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
)))
243 if (!err
&& outarg
.nodeid
) {
244 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
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
)
255 if (inode
&& dir_alias(inode
)) {
257 return ERR_PTR(-EIO
);
259 #if defined(FUSE_MAINLINE)
262 newent
= d_splice_alias(inode
, entry
);
263 entry
= newent
? newent
: entry
;
265 entry
->d_op
= &fuse_dentry_operations
;
267 fuse_change_timeout(entry
, &outarg
);
269 fuse_invalidate_entry_cache(entry
);
270 #if defined(FUSE_MAINLINE)
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
);
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
)
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
;
311 int flags
= nd
->intent
.open
.flags
- 1;
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
);
323 goto out_put_forget_req
;
326 ff
= fuse_file_alloc();
328 goto out_put_request
;
331 memset(&inarg
, 0, sizeof(inarg
));
334 req
->in
.h
.opcode
= FUSE_CREATE
;
335 req
->in
.h
.nodeid
= get_node_id(dir
);
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
;
355 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
358 fuse_put_request(fc
, req
);
359 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
362 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
364 fuse_sync_release(fc
, ff
, outentry
.nodeid
, flags
);
365 fuse_send_forget(fc
, forget_req
, outentry
.nodeid
, 1);
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
);
374 fuse_sync_release(fc
, ff
, outentry
.nodeid
, flags
);
375 return PTR_ERR(file
);
377 fuse_finish_open(inode
, file
, ff
, &outopen
);
383 fuse_put_request(fc
, req
);
385 fuse_put_request(fc
, forget_req
);
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
,
397 struct fuse_entry_out outarg
;
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
;
408 fuse_put_request(fc
, req
);
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
,
421 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
424 fuse_put_request(fc
, req
);
426 if (dir_alias(inode
)) {
431 d_instantiate(entry
, inode
);
432 fuse_change_timeout(entry
, &outarg
);
433 fuse_invalidate_attr(dir
);
437 fuse_put_request(fc
, req
);
441 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, int mode
,
444 struct fuse_mknod_in inarg
;
445 struct fuse_conn
*fc
= get_fuse_conn(dir
);
446 struct fuse_req
*req
= fuse_get_req(fc
);
450 memset(&inarg
, 0, sizeof(inarg
));
452 inarg
.rdev
= new_encode_dev(rdev
);
453 req
->in
.h
.opcode
= FUSE_MKNOD
;
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
);
470 /* Fall back on mknod */
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
);
484 memset(&inarg
, 0, sizeof(inarg
));
486 req
->in
.h
.opcode
= FUSE_MKDIR
;
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
,
498 struct fuse_conn
*fc
= get_fuse_conn(dir
);
499 unsigned len
= strlen(link
) + 1;
500 struct fuse_req
*req
= fuse_get_req(fc
);
504 req
->in
.h
.opcode
= FUSE_SYMLINK
;
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
)
516 struct fuse_conn
*fc
= get_fuse_conn(dir
);
517 struct fuse_req
*req
= fuse_get_req(fc
);
521 req
->in
.h
.opcode
= FUSE_UNLINK
;
522 req
->in
.h
.nodeid
= get_node_id(dir
);
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
);
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 */
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
);
544 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
547 struct fuse_conn
*fc
= get_fuse_conn(dir
);
548 struct fuse_req
*req
= fuse_get_req(fc
);
552 req
->in
.h
.opcode
= FUSE_RMDIR
;
553 req
->in
.h
.nodeid
= get_node_id(dir
);
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
);
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
);
569 static int fuse_rename(struct inode
*olddir
, struct dentry
*oldent
,
570 struct inode
*newdir
, struct dentry
*newent
)
573 struct fuse_rename_in inarg
;
574 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
575 struct fuse_req
*req
= fuse_get_req(fc
);
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
);
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
);
594 fuse_invalidate_attr(olddir
);
595 if (olddir
!= newdir
)
596 fuse_invalidate_attr(newdir
);
598 /* newent will end up negative */
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
);
609 fuse_invalidate_entry(newent
);
615 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
616 struct dentry
*newent
)
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
);
626 memset(&inarg
, 0, sizeof(inarg
));
627 inarg
.oldnodeid
= get_node_id(inode
);
628 req
->in
.h
.opcode
= FUSE_LINK
;
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,
641 if (!err
|| err
== -EINTR
)
642 fuse_invalidate_attr(inode
);
646 int fuse_do_getattr(struct inode
*inode
)
649 struct fuse_attr_out arg
;
650 struct fuse_conn
*fc
= get_fuse_conn(inode
);
651 struct fuse_req
*req
= fuse_get_req(fc
);
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
);
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
);
669 make_bad_inode(inode
);
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
);
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
)
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
)
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
))
728 if (get_node_id(inode
) != FUSE_ROOT_ID
&&
729 fi
->i_time
>= get_jiffies_64())
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
;
745 req
= fuse_get_req(fc
);
749 memset(&inarg
, 0, sizeof(inarg
));
751 req
->in
.h
.opcode
= FUSE_ACCESS
;
752 req
->in
.h
.nodeid
= get_node_id(inode
);
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
) {
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
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
))
785 else if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
786 #ifdef KERNEL_2_6_10_PLUS
787 int err
= generic_permission(inode
, mask
, NULL
);
789 int err
= vfs_permission(inode
, mask
);
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
);
798 #ifdef KERNEL_2_6_10_PLUS
799 err
= generic_permission(inode
, mask
, NULL
);
801 err
= vfs_permission(inode
, mask
);
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 */
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
)))
818 if ((mask
& MAY_EXEC
) && !S_ISDIR(mode
) && !(mode
& S_IXUGO
))
821 if (nd
&& (nd
->flags
& LOOKUP_ACCESS
))
822 return fuse_access(inode
, mask
);
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
);
834 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
839 over
= filldir(dstbuf
, dirent
->name
, dirent
->namelen
,
840 file
->f_pos
, dirent
->ino
, dirent
->type
);
846 file
->f_pos
= dirent
->off
;
852 static int fuse_readdir(struct file
*file
, void *dstbuf
, filldir_t filldir
)
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
))
864 req
= fuse_get_req(fc
);
868 page
= alloc_page(GFP_KERNEL
);
870 fuse_put_request(fc
, req
);
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
);
881 err
= parse_dirfile(page_address(page
), nbytes
, file
, dstbuf
,
885 fuse_invalidate_attr(inode
); /* atime changed */
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
);
897 return ERR_PTR(PTR_ERR(req
));
899 link
= (char *) __get_free_page(GFP_KERNEL
);
901 link
= ERR_PTR(-ENOMEM
);
904 req
->in
.h
.opcode
= FUSE_READLINK
;
905 req
->in
.h
.nodeid
= get_node_id(inode
);
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
);
915 link
[req
->out
.args
[0].size
] = '\0';
917 fuse_put_request(fc
, req
);
918 fuse_invalidate_attr(inode
); /* atime changed */
922 static void free_link(char *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
));
935 static void fuse_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *c
)
937 free_link(nd_get_link(nd
));
940 static int fuse_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
942 nd_set_link(nd
, read_link(dentry
));
946 static void fuse_put_link(struct dentry
*dentry
, struct nameidata
*nd
)
948 free_link(nd_get_link(nd
));
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
;
987 if (ivalid
& ATTR_FILE
) {
988 struct fuse_file
*ff
= iattr
->ia_file
->private_data
;
989 arg
->valid
|= FATTR_FH
;
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
;
1013 int is_truncate
= 0;
1015 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
1016 err
= inode_change_ok(inode
, attr
);
1021 if (attr
->ia_valid
& ATTR_SIZE
) {
1022 unsigned long limit
;
1024 #ifdef KERNEL_2_6_10_PLUS
1025 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1027 limit
= current
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1029 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> (loff_t
) limit
) {
1030 send_sig(SIGXFSZ
, current
, 0);
1035 req
= fuse_get_req(fc
);
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
);
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
);
1058 make_bad_inode(inode
);
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
);
1078 static int fuse_getattr(struct vfsmount
*mnt
, struct dentry
*entry
,
1081 struct inode
*inode
= entry
->d_inode
;
1082 int err
= fuse_revalidate(entry
);
1084 generic_fillattr(inode
, stat
);
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
;
1098 if (fc
->no_setxattr
)
1101 req
= fuse_get_req(fc
);
1103 return PTR_ERR(req
);
1105 memset(&inarg
, 0, sizeof(inarg
));
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;
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
;
1137 if (fc
->no_getxattr
)
1140 req
= fuse_get_req(fc
);
1142 return PTR_ERR(req
);
1144 memset(&inarg
, 0, sizeof(inarg
));
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;
1156 req
->out
.argvar
= 1;
1157 req
->out
.args
[0].size
= size
;
1158 req
->out
.args
[0].value
= value
;
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
;
1166 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1168 if (ret
== -ENOSYS
) {
1169 fc
->no_getxattr
= 1;
1173 fuse_put_request(fc
, req
);
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
;
1186 if (fc
->no_listxattr
)
1189 req
= fuse_get_req(fc
);
1191 return PTR_ERR(req
);
1193 memset(&inarg
, 0, sizeof(inarg
));
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;
1203 req
->out
.argvar
= 1;
1204 req
->out
.args
[0].size
= size
;
1205 req
->out
.args
[0].value
= list
;
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
;
1213 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1215 if (ret
== -ENOSYS
) {
1216 fc
->no_listxattr
= 1;
1220 fuse_put_request(fc
, req
);
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
;
1231 if (fc
->no_removexattr
)
1234 req
= fuse_get_req(fc
);
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;
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
,
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
;