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
);
194 static int invalid_nodeid(u64 nodeid
)
196 return !nodeid
|| nodeid
== FUSE_ROOT_ID
;
199 static struct dentry_operations fuse_dentry_operations
= {
200 .d_revalidate
= fuse_dentry_revalidate
,
203 static int valid_mode(int m
)
205 return S_ISREG(m
) || S_ISDIR(m
) || S_ISLNK(m
) || S_ISCHR(m
) ||
206 S_ISBLK(m
) || S_ISFIFO(m
) || S_ISSOCK(m
);
209 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
210 struct nameidata
*nd
)
213 struct fuse_entry_out outarg
;
214 struct inode
*inode
= NULL
;
215 struct fuse_conn
*fc
= get_fuse_conn(dir
);
216 struct fuse_req
*req
;
218 if (entry
->d_name
.len
> FUSE_NAME_MAX
)
219 return ERR_PTR(-ENAMETOOLONG
);
221 req
= fuse_get_req(fc
);
223 return ERR_PTR(PTR_ERR(req
));
225 fuse_lookup_init(req
, dir
, entry
, &outarg
);
226 request_send(fc
, req
);
227 err
= req
->out
.h
.error
;
228 /* Zero nodeid is same as -ENOENT, but with valid timeout */
229 if (!err
&& outarg
.nodeid
&&
230 (invalid_nodeid(outarg
.nodeid
) || !valid_mode(outarg
.attr
.mode
)))
232 if (!err
&& outarg
.nodeid
) {
233 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
236 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
237 return ERR_PTR(-ENOMEM
);
240 fuse_put_request(fc
, req
);
241 if (err
&& err
!= -ENOENT
)
244 if (inode
&& dir_alias(inode
)) {
246 return ERR_PTR(-EIO
);
249 entry
->d_op
= &fuse_dentry_operations
;
251 fuse_change_timeout(entry
, &outarg
);
253 fuse_invalidate_entry_cache(entry
);
258 * Synchronous release for the case when something goes wrong in CREATE_OPEN
260 static void fuse_sync_release(struct fuse_conn
*fc
, struct fuse_file
*ff
,
261 u64 nodeid
, int flags
)
263 struct fuse_req
*req
;
265 req
= fuse_release_fill(ff
, nodeid
, flags
, FUSE_RELEASE
);
267 request_send(fc
, req
);
268 fuse_put_request(fc
, req
);
272 * Atomic create+open operation
274 * If the filesystem doesn't support this, then fall back to separate
275 * 'mknod' + 'open' requests.
277 static int fuse_create_open(struct inode
*dir
, struct dentry
*entry
, int mode
,
278 struct nameidata
*nd
)
282 struct fuse_conn
*fc
= get_fuse_conn(dir
);
283 struct fuse_req
*req
;
284 struct fuse_req
*forget_req
;
285 struct fuse_open_in inarg
;
286 struct fuse_open_out outopen
;
287 struct fuse_entry_out outentry
;
288 struct fuse_file
*ff
;
290 int flags
= nd
->intent
.open
.flags
- 1;
295 forget_req
= fuse_get_req(fc
);
296 if (IS_ERR(forget_req
))
297 return PTR_ERR(forget_req
);
299 req
= fuse_get_req(fc
);
302 goto out_put_forget_req
;
305 ff
= fuse_file_alloc();
307 goto out_put_request
;
310 memset(&inarg
, 0, sizeof(inarg
));
313 req
->in
.h
.opcode
= FUSE_CREATE
;
314 req
->in
.h
.nodeid
= get_node_id(dir
);
316 req
->in
.args
[0].size
= sizeof(inarg
);
317 req
->in
.args
[0].value
= &inarg
;
318 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
319 req
->in
.args
[1].value
= entry
->d_name
.name
;
320 req
->out
.numargs
= 2;
321 req
->out
.args
[0].size
= sizeof(outentry
);
322 req
->out
.args
[0].value
= &outentry
;
323 req
->out
.args
[1].size
= sizeof(outopen
);
324 req
->out
.args
[1].value
= &outopen
;
325 request_send(fc
, req
);
326 err
= req
->out
.h
.error
;
334 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
337 fuse_put_request(fc
, req
);
338 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
341 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
343 fuse_sync_release(fc
, ff
, outentry
.nodeid
, flags
);
344 fuse_send_forget(fc
, forget_req
, outentry
.nodeid
, 1);
347 fuse_put_request(fc
, forget_req
);
348 d_instantiate(entry
, inode
);
349 fuse_change_timeout(entry
, &outentry
);
350 file
= lookup_instantiate_filp(nd
, entry
, generic_file_open
);
353 fuse_sync_release(fc
, ff
, outentry
.nodeid
, flags
);
354 return PTR_ERR(file
);
356 fuse_finish_open(inode
, file
, ff
, &outopen
);
362 fuse_put_request(fc
, req
);
364 fuse_put_request(fc
, forget_req
);
369 * Code shared between mknod, mkdir, symlink and link
371 static int create_new_entry(struct fuse_conn
*fc
, struct fuse_req
*req
,
372 struct inode
*dir
, struct dentry
*entry
,
375 struct fuse_entry_out outarg
;
379 req
->in
.h
.nodeid
= get_node_id(dir
);
380 req
->out
.numargs
= 1;
381 req
->out
.args
[0].size
= sizeof(outarg
);
382 req
->out
.args
[0].value
= &outarg
;
383 request_send(fc
, req
);
384 err
= req
->out
.h
.error
;
386 fuse_put_request(fc
, req
);
390 if (invalid_nodeid(outarg
.nodeid
))
391 goto out_put_request
;
393 if ((outarg
.attr
.mode
^ mode
) & S_IFMT
)
394 goto out_put_request
;
396 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
399 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
402 fuse_put_request(fc
, req
);
404 if (dir_alias(inode
)) {
409 d_instantiate(entry
, inode
);
410 fuse_change_timeout(entry
, &outarg
);
411 fuse_invalidate_attr(dir
);
415 fuse_put_request(fc
, req
);
419 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, int mode
,
422 struct fuse_mknod_in inarg
;
423 struct fuse_conn
*fc
= get_fuse_conn(dir
);
424 struct fuse_req
*req
= fuse_get_req(fc
);
428 memset(&inarg
, 0, sizeof(inarg
));
430 inarg
.rdev
= new_encode_dev(rdev
);
431 req
->in
.h
.opcode
= FUSE_MKNOD
;
433 req
->in
.args
[0].size
= sizeof(inarg
);
434 req
->in
.args
[0].value
= &inarg
;
435 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
436 req
->in
.args
[1].value
= entry
->d_name
.name
;
437 return create_new_entry(fc
, req
, dir
, entry
, mode
);
440 static int fuse_create(struct inode
*dir
, struct dentry
*entry
, int mode
,
441 struct nameidata
*nd
)
443 if (nd
&& (nd
->flags
& LOOKUP_CREATE
)) {
444 int err
= fuse_create_open(dir
, entry
, mode
, nd
);
447 /* Fall back on mknod */
449 return fuse_mknod(dir
, entry
, mode
, 0);
452 static int fuse_mkdir(struct inode
*dir
, struct dentry
*entry
, int mode
)
454 struct fuse_mkdir_in inarg
;
455 struct fuse_conn
*fc
= get_fuse_conn(dir
);
456 struct fuse_req
*req
= fuse_get_req(fc
);
460 memset(&inarg
, 0, sizeof(inarg
));
462 req
->in
.h
.opcode
= FUSE_MKDIR
;
464 req
->in
.args
[0].size
= sizeof(inarg
);
465 req
->in
.args
[0].value
= &inarg
;
466 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
467 req
->in
.args
[1].value
= entry
->d_name
.name
;
468 return create_new_entry(fc
, req
, dir
, entry
, S_IFDIR
);
471 static int fuse_symlink(struct inode
*dir
, struct dentry
*entry
,
474 struct fuse_conn
*fc
= get_fuse_conn(dir
);
475 unsigned len
= strlen(link
) + 1;
476 struct fuse_req
*req
= fuse_get_req(fc
);
480 req
->in
.h
.opcode
= FUSE_SYMLINK
;
482 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
483 req
->in
.args
[0].value
= entry
->d_name
.name
;
484 req
->in
.args
[1].size
= len
;
485 req
->in
.args
[1].value
= link
;
486 return create_new_entry(fc
, req
, dir
, entry
, S_IFLNK
);
489 static int fuse_unlink(struct inode
*dir
, struct dentry
*entry
)
492 struct fuse_conn
*fc
= get_fuse_conn(dir
);
493 struct fuse_req
*req
= fuse_get_req(fc
);
497 req
->in
.h
.opcode
= FUSE_UNLINK
;
498 req
->in
.h
.nodeid
= get_node_id(dir
);
500 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
501 req
->in
.args
[0].value
= entry
->d_name
.name
;
502 request_send(fc
, req
);
503 err
= req
->out
.h
.error
;
504 fuse_put_request(fc
, req
);
506 struct inode
*inode
= entry
->d_inode
;
508 /* Set nlink to zero so the inode can be cleared, if
509 the inode does have more links this will be
510 discovered at the next lookup/getattr */
512 fuse_invalidate_attr(inode
);
513 fuse_invalidate_attr(dir
);
514 fuse_invalidate_entry_cache(entry
);
515 } else if (err
== -EINTR
)
516 fuse_invalidate_entry(entry
);
520 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
523 struct fuse_conn
*fc
= get_fuse_conn(dir
);
524 struct fuse_req
*req
= fuse_get_req(fc
);
528 req
->in
.h
.opcode
= FUSE_RMDIR
;
529 req
->in
.h
.nodeid
= get_node_id(dir
);
531 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
532 req
->in
.args
[0].value
= entry
->d_name
.name
;
533 request_send(fc
, req
);
534 err
= req
->out
.h
.error
;
535 fuse_put_request(fc
, req
);
537 entry
->d_inode
->i_nlink
= 0;
538 fuse_invalidate_attr(dir
);
539 fuse_invalidate_entry_cache(entry
);
540 } else if (err
== -EINTR
)
541 fuse_invalidate_entry(entry
);
545 static int fuse_rename(struct inode
*olddir
, struct dentry
*oldent
,
546 struct inode
*newdir
, struct dentry
*newent
)
549 struct fuse_rename_in inarg
;
550 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
551 struct fuse_req
*req
= fuse_get_req(fc
);
555 memset(&inarg
, 0, sizeof(inarg
));
556 inarg
.newdir
= get_node_id(newdir
);
557 req
->in
.h
.opcode
= FUSE_RENAME
;
558 req
->in
.h
.nodeid
= get_node_id(olddir
);
560 req
->in
.args
[0].size
= sizeof(inarg
);
561 req
->in
.args
[0].value
= &inarg
;
562 req
->in
.args
[1].size
= oldent
->d_name
.len
+ 1;
563 req
->in
.args
[1].value
= oldent
->d_name
.name
;
564 req
->in
.args
[2].size
= newent
->d_name
.len
+ 1;
565 req
->in
.args
[2].value
= newent
->d_name
.name
;
566 request_send(fc
, req
);
567 err
= req
->out
.h
.error
;
568 fuse_put_request(fc
, req
);
570 fuse_invalidate_attr(olddir
);
571 if (olddir
!= newdir
)
572 fuse_invalidate_attr(newdir
);
574 /* newent will end up negative */
576 fuse_invalidate_entry_cache(newent
);
577 } else if (err
== -EINTR
) {
578 /* If request was interrupted, DEITY only knows if the
579 rename actually took place. If the invalidation
580 fails (e.g. some process has CWD under the renamed
581 directory), then there can be inconsistency between
582 the dcache and the real filesystem. Tough luck. */
583 fuse_invalidate_entry(oldent
);
585 fuse_invalidate_entry(newent
);
591 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
592 struct dentry
*newent
)
595 struct fuse_link_in inarg
;
596 struct inode
*inode
= entry
->d_inode
;
597 struct fuse_conn
*fc
= get_fuse_conn(inode
);
598 struct fuse_req
*req
= fuse_get_req(fc
);
602 memset(&inarg
, 0, sizeof(inarg
));
603 inarg
.oldnodeid
= get_node_id(inode
);
604 req
->in
.h
.opcode
= FUSE_LINK
;
606 req
->in
.args
[0].size
= sizeof(inarg
);
607 req
->in
.args
[0].value
= &inarg
;
608 req
->in
.args
[1].size
= newent
->d_name
.len
+ 1;
609 req
->in
.args
[1].value
= newent
->d_name
.name
;
610 err
= create_new_entry(fc
, req
, newdir
, newent
, inode
->i_mode
);
611 /* Contrary to "normal" filesystems it can happen that link
612 makes two "logical" inodes point to the same "physical"
613 inode. We invalidate the attributes of the old one, so it
614 will reflect changes in the backing inode (link count,
617 if (!err
|| err
== -EINTR
)
618 fuse_invalidate_attr(inode
);
622 int fuse_do_getattr(struct inode
*inode
)
625 struct fuse_attr_out arg
;
626 struct fuse_conn
*fc
= get_fuse_conn(inode
);
627 struct fuse_req
*req
= fuse_get_req(fc
);
631 req
->in
.h
.opcode
= FUSE_GETATTR
;
632 req
->in
.h
.nodeid
= get_node_id(inode
);
633 req
->out
.numargs
= 1;
634 req
->out
.args
[0].size
= sizeof(arg
);
635 req
->out
.args
[0].value
= &arg
;
636 request_send(fc
, req
);
637 err
= req
->out
.h
.error
;
638 fuse_put_request(fc
, req
);
640 if ((inode
->i_mode
^ arg
.attr
.mode
) & S_IFMT
) {
641 make_bad_inode(inode
);
644 struct fuse_inode
*fi
= get_fuse_inode(inode
);
645 fuse_change_attributes(inode
, &arg
.attr
);
646 fi
->i_time
= time_to_jiffies(arg
.attr_valid
,
647 arg
.attr_valid_nsec
);
654 * Calling into a user-controlled filesystem gives the filesystem
655 * daemon ptrace-like capabilities over the requester process. This
656 * means, that the filesystem daemon is able to record the exact
657 * filesystem operations performed, and can also control the behavior
658 * of the requester process in otherwise impossible ways. For example
659 * it can delay the operation for arbitrary length of time allowing
660 * DoS against the requester.
662 * For this reason only those processes can call into the filesystem,
663 * for which the owner of the mount has ptrace privilege. This
664 * excludes processes started by other users, suid or sgid processes.
666 static int fuse_allow_task(struct fuse_conn
*fc
, struct task_struct
*task
)
668 if (fc
->flags
& FUSE_ALLOW_OTHER
)
671 if (task
->euid
== fc
->user_id
&&
672 task
->suid
== fc
->user_id
&&
673 task
->uid
== fc
->user_id
&&
674 task
->egid
== fc
->group_id
&&
675 task
->sgid
== fc
->group_id
&&
676 task
->gid
== fc
->group_id
)
683 * Check whether the inode attributes are still valid
685 * If the attribute validity timeout has expired, then fetch the fresh
686 * attributes with a 'getattr' request
688 * I'm not sure why cached attributes are never returned for the root
689 * inode, this is probably being too cautious.
691 static int fuse_revalidate(struct dentry
*entry
)
693 struct inode
*inode
= entry
->d_inode
;
694 struct fuse_inode
*fi
= get_fuse_inode(inode
);
695 struct fuse_conn
*fc
= get_fuse_conn(inode
);
697 if (!fuse_allow_task(fc
, current
))
699 if (get_node_id(inode
) != FUSE_ROOT_ID
&&
700 fi
->i_time
>= get_jiffies_64())
703 return fuse_do_getattr(inode
);
706 static int fuse_access(struct inode
*inode
, int mask
)
708 struct fuse_conn
*fc
= get_fuse_conn(inode
);
709 struct fuse_req
*req
;
710 struct fuse_access_in inarg
;
716 req
= fuse_get_req(fc
);
720 memset(&inarg
, 0, sizeof(inarg
));
722 req
->in
.h
.opcode
= FUSE_ACCESS
;
723 req
->in
.h
.nodeid
= get_node_id(inode
);
725 req
->in
.args
[0].size
= sizeof(inarg
);
726 req
->in
.args
[0].value
= &inarg
;
727 request_send(fc
, req
);
728 err
= req
->out
.h
.error
;
729 fuse_put_request(fc
, req
);
730 if (err
== -ENOSYS
) {
738 * Check permission. The two basic access models of FUSE are:
740 * 1) Local access checking ('default_permissions' mount option) based
741 * on file mode. This is the plain old disk filesystem permission
744 * 2) "Remote" access checking, where server is responsible for
745 * checking permission in each inode operation. An exception to this
746 * is if ->permission() was invoked from sys_access() in which case an
747 * access request is sent. Execute permission is still checked
748 * locally based on file mode.
750 static int fuse_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
752 struct fuse_conn
*fc
= get_fuse_conn(inode
);
754 if (!fuse_allow_task(fc
, current
))
756 else if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
757 int err
= generic_permission(inode
, mask
, NULL
);
759 /* If permission is denied, try to refresh file
760 attributes. This is also needed, because the root
761 node will at first have no permissions */
762 if (err
== -EACCES
) {
763 err
= fuse_do_getattr(inode
);
765 err
= generic_permission(inode
, mask
, NULL
);
768 /* Note: the opposite of the above test does not
769 exist. So if permissions are revoked this won't be
770 noticed immediately, only after the attribute
771 timeout has expired */
775 int mode
= inode
->i_mode
;
776 if ((mask
& MAY_EXEC
) && !S_ISDIR(mode
) && !(mode
& S_IXUGO
))
779 if (nd
&& (nd
->flags
& LOOKUP_ACCESS
))
780 return fuse_access(inode
, mask
);
785 static int parse_dirfile(char *buf
, size_t nbytes
, struct file
*file
,
786 void *dstbuf
, filldir_t filldir
)
788 while (nbytes
>= FUSE_NAME_OFFSET
) {
789 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
790 size_t reclen
= FUSE_DIRENT_SIZE(dirent
);
792 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
797 over
= filldir(dstbuf
, dirent
->name
, dirent
->namelen
,
798 file
->f_pos
, dirent
->ino
, dirent
->type
);
804 file
->f_pos
= dirent
->off
;
810 static int fuse_readdir(struct file
*file
, void *dstbuf
, filldir_t filldir
)
815 struct inode
*inode
= file
->f_dentry
->d_inode
;
816 struct fuse_conn
*fc
= get_fuse_conn(inode
);
817 struct fuse_req
*req
;
819 if (is_bad_inode(inode
))
822 req
= fuse_get_req(fc
);
826 page
= alloc_page(GFP_KERNEL
);
828 fuse_put_request(fc
, req
);
832 req
->pages
[0] = page
;
833 fuse_read_fill(req
, file
, inode
, file
->f_pos
, PAGE_SIZE
, FUSE_READDIR
);
834 request_send(fc
, req
);
835 nbytes
= req
->out
.args
[0].size
;
836 err
= req
->out
.h
.error
;
837 fuse_put_request(fc
, req
);
839 err
= parse_dirfile(page_address(page
), nbytes
, file
, dstbuf
,
843 fuse_invalidate_attr(inode
); /* atime changed */
847 static char *read_link(struct dentry
*dentry
)
849 struct inode
*inode
= dentry
->d_inode
;
850 struct fuse_conn
*fc
= get_fuse_conn(inode
);
851 struct fuse_req
*req
= fuse_get_req(fc
);
855 return ERR_PTR(PTR_ERR(req
));
857 link
= (char *) __get_free_page(GFP_KERNEL
);
859 link
= ERR_PTR(-ENOMEM
);
862 req
->in
.h
.opcode
= FUSE_READLINK
;
863 req
->in
.h
.nodeid
= get_node_id(inode
);
865 req
->out
.numargs
= 1;
866 req
->out
.args
[0].size
= PAGE_SIZE
- 1;
867 req
->out
.args
[0].value
= link
;
868 request_send(fc
, req
);
869 if (req
->out
.h
.error
) {
870 free_page((unsigned long) link
);
871 link
= ERR_PTR(req
->out
.h
.error
);
873 link
[req
->out
.args
[0].size
] = '\0';
875 fuse_put_request(fc
, req
);
876 fuse_invalidate_attr(inode
); /* atime changed */
880 static void free_link(char *link
)
883 free_page((unsigned long) link
);
886 static void *fuse_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
888 nd_set_link(nd
, read_link(dentry
));
892 static void fuse_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *c
)
894 free_link(nd_get_link(nd
));
897 static int fuse_dir_open(struct inode
*inode
, struct file
*file
)
899 return fuse_open_common(inode
, file
, 1);
902 static int fuse_dir_release(struct inode
*inode
, struct file
*file
)
904 return fuse_release_common(inode
, file
, 1);
907 static int fuse_dir_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
909 /* nfsd can call this with no file */
910 return file
? fuse_fsync_common(file
, de
, datasync
, 1) : 0;
913 static void iattr_to_fattr(struct iattr
*iattr
, struct fuse_setattr_in
*arg
)
915 unsigned ivalid
= iattr
->ia_valid
;
917 if (ivalid
& ATTR_MODE
)
918 arg
->valid
|= FATTR_MODE
, arg
->mode
= iattr
->ia_mode
;
919 if (ivalid
& ATTR_UID
)
920 arg
->valid
|= FATTR_UID
, arg
->uid
= iattr
->ia_uid
;
921 if (ivalid
& ATTR_GID
)
922 arg
->valid
|= FATTR_GID
, arg
->gid
= iattr
->ia_gid
;
923 if (ivalid
& ATTR_SIZE
)
924 arg
->valid
|= FATTR_SIZE
, arg
->size
= iattr
->ia_size
;
925 /* You can only _set_ these together (they may change by themselves) */
926 if ((ivalid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
)) {
927 arg
->valid
|= FATTR_ATIME
| FATTR_MTIME
;
928 arg
->atime
= iattr
->ia_atime
.tv_sec
;
929 arg
->mtime
= iattr
->ia_mtime
.tv_sec
;
931 if (ivalid
& ATTR_FILE
) {
932 struct fuse_file
*ff
= iattr
->ia_file
->private_data
;
933 arg
->valid
|= FATTR_FH
;
939 * Set attributes, and at the same time refresh them.
941 * Truncation is slightly complicated, because the 'truncate' request
942 * may fail, in which case we don't want to touch the mapping.
943 * vmtruncate() doesn't allow for this case. So do the rlimit
944 * checking by hand and call vmtruncate() only after the file has
945 * actually been truncated.
947 static int fuse_setattr(struct dentry
*entry
, struct iattr
*attr
)
949 struct inode
*inode
= entry
->d_inode
;
950 struct fuse_conn
*fc
= get_fuse_conn(inode
);
951 struct fuse_inode
*fi
= get_fuse_inode(inode
);
952 struct fuse_req
*req
;
953 struct fuse_setattr_in inarg
;
954 struct fuse_attr_out outarg
;
958 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
959 err
= inode_change_ok(inode
, attr
);
964 if (attr
->ia_valid
& ATTR_SIZE
) {
967 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
968 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> (loff_t
) limit
) {
969 send_sig(SIGXFSZ
, current
, 0);
974 req
= fuse_get_req(fc
);
978 memset(&inarg
, 0, sizeof(inarg
));
979 iattr_to_fattr(attr
, &inarg
);
980 req
->in
.h
.opcode
= FUSE_SETATTR
;
981 req
->in
.h
.nodeid
= get_node_id(inode
);
983 req
->in
.args
[0].size
= sizeof(inarg
);
984 req
->in
.args
[0].value
= &inarg
;
985 req
->out
.numargs
= 1;
986 req
->out
.args
[0].size
= sizeof(outarg
);
987 req
->out
.args
[0].value
= &outarg
;
988 request_send(fc
, req
);
989 err
= req
->out
.h
.error
;
990 fuse_put_request(fc
, req
);
992 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
993 make_bad_inode(inode
);
997 loff_t origsize
= i_size_read(inode
);
998 i_size_write(inode
, outarg
.attr
.size
);
999 if (origsize
> outarg
.attr
.size
)
1000 vmtruncate(inode
, outarg
.attr
.size
);
1002 fuse_change_attributes(inode
, &outarg
.attr
);
1003 fi
->i_time
= time_to_jiffies(outarg
.attr_valid
,
1004 outarg
.attr_valid_nsec
);
1006 } else if (err
== -EINTR
)
1007 fuse_invalidate_attr(inode
);
1012 static int fuse_getattr(struct vfsmount
*mnt
, struct dentry
*entry
,
1015 struct inode
*inode
= entry
->d_inode
;
1016 int err
= fuse_revalidate(entry
);
1018 generic_fillattr(inode
, stat
);
1023 static int fuse_setxattr(struct dentry
*entry
, const char *name
,
1024 const void *value
, size_t size
, int flags
)
1026 struct inode
*inode
= entry
->d_inode
;
1027 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1028 struct fuse_req
*req
;
1029 struct fuse_setxattr_in inarg
;
1032 if (fc
->no_setxattr
)
1035 req
= fuse_get_req(fc
);
1037 return PTR_ERR(req
);
1039 memset(&inarg
, 0, sizeof(inarg
));
1041 inarg
.flags
= flags
;
1042 req
->in
.h
.opcode
= FUSE_SETXATTR
;
1043 req
->in
.h
.nodeid
= get_node_id(inode
);
1044 req
->in
.numargs
= 3;
1045 req
->in
.args
[0].size
= sizeof(inarg
);
1046 req
->in
.args
[0].value
= &inarg
;
1047 req
->in
.args
[1].size
= strlen(name
) + 1;
1048 req
->in
.args
[1].value
= name
;
1049 req
->in
.args
[2].size
= size
;
1050 req
->in
.args
[2].value
= value
;
1051 request_send(fc
, req
);
1052 err
= req
->out
.h
.error
;
1053 fuse_put_request(fc
, req
);
1054 if (err
== -ENOSYS
) {
1055 fc
->no_setxattr
= 1;
1061 static ssize_t
fuse_getxattr(struct dentry
*entry
, const char *name
,
1062 void *value
, size_t size
)
1064 struct inode
*inode
= entry
->d_inode
;
1065 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1066 struct fuse_req
*req
;
1067 struct fuse_getxattr_in inarg
;
1068 struct fuse_getxattr_out outarg
;
1071 if (fc
->no_getxattr
)
1074 req
= fuse_get_req(fc
);
1076 return PTR_ERR(req
);
1078 memset(&inarg
, 0, sizeof(inarg
));
1080 req
->in
.h
.opcode
= FUSE_GETXATTR
;
1081 req
->in
.h
.nodeid
= get_node_id(inode
);
1082 req
->in
.numargs
= 2;
1083 req
->in
.args
[0].size
= sizeof(inarg
);
1084 req
->in
.args
[0].value
= &inarg
;
1085 req
->in
.args
[1].size
= strlen(name
) + 1;
1086 req
->in
.args
[1].value
= name
;
1087 /* This is really two different operations rolled into one */
1088 req
->out
.numargs
= 1;
1090 req
->out
.argvar
= 1;
1091 req
->out
.args
[0].size
= size
;
1092 req
->out
.args
[0].value
= value
;
1094 req
->out
.args
[0].size
= sizeof(outarg
);
1095 req
->out
.args
[0].value
= &outarg
;
1097 request_send(fc
, req
);
1098 ret
= req
->out
.h
.error
;
1100 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1102 if (ret
== -ENOSYS
) {
1103 fc
->no_getxattr
= 1;
1107 fuse_put_request(fc
, req
);
1111 static ssize_t
fuse_listxattr(struct dentry
*entry
, char *list
, size_t size
)
1113 struct inode
*inode
= entry
->d_inode
;
1114 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1115 struct fuse_req
*req
;
1116 struct fuse_getxattr_in inarg
;
1117 struct fuse_getxattr_out outarg
;
1120 if (fc
->no_listxattr
)
1123 req
= fuse_get_req(fc
);
1125 return PTR_ERR(req
);
1127 memset(&inarg
, 0, sizeof(inarg
));
1129 req
->in
.h
.opcode
= FUSE_LISTXATTR
;
1130 req
->in
.h
.nodeid
= get_node_id(inode
);
1131 req
->in
.numargs
= 1;
1132 req
->in
.args
[0].size
= sizeof(inarg
);
1133 req
->in
.args
[0].value
= &inarg
;
1134 /* This is really two different operations rolled into one */
1135 req
->out
.numargs
= 1;
1137 req
->out
.argvar
= 1;
1138 req
->out
.args
[0].size
= size
;
1139 req
->out
.args
[0].value
= list
;
1141 req
->out
.args
[0].size
= sizeof(outarg
);
1142 req
->out
.args
[0].value
= &outarg
;
1144 request_send(fc
, req
);
1145 ret
= req
->out
.h
.error
;
1147 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1149 if (ret
== -ENOSYS
) {
1150 fc
->no_listxattr
= 1;
1154 fuse_put_request(fc
, req
);
1158 static int fuse_removexattr(struct dentry
*entry
, const char *name
)
1160 struct inode
*inode
= entry
->d_inode
;
1161 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1162 struct fuse_req
*req
;
1165 if (fc
->no_removexattr
)
1168 req
= fuse_get_req(fc
);
1170 return PTR_ERR(req
);
1172 req
->in
.h
.opcode
= FUSE_REMOVEXATTR
;
1173 req
->in
.h
.nodeid
= get_node_id(inode
);
1174 req
->in
.numargs
= 1;
1175 req
->in
.args
[0].size
= strlen(name
) + 1;
1176 req
->in
.args
[0].value
= name
;
1177 request_send(fc
, req
);
1178 err
= req
->out
.h
.error
;
1179 fuse_put_request(fc
, req
);
1180 if (err
== -ENOSYS
) {
1181 fc
->no_removexattr
= 1;
1187 static struct inode_operations fuse_dir_inode_operations
= {
1188 .lookup
= fuse_lookup
,
1189 .mkdir
= fuse_mkdir
,
1190 .symlink
= fuse_symlink
,
1191 .unlink
= fuse_unlink
,
1192 .rmdir
= fuse_rmdir
,
1193 .rename
= fuse_rename
,
1195 .setattr
= fuse_setattr
,
1196 .create
= fuse_create
,
1197 .mknod
= fuse_mknod
,
1198 .permission
= fuse_permission
,
1199 .getattr
= fuse_getattr
,
1200 .setxattr
= fuse_setxattr
,
1201 .getxattr
= fuse_getxattr
,
1202 .listxattr
= fuse_listxattr
,
1203 .removexattr
= fuse_removexattr
,
1206 static const struct file_operations fuse_dir_operations
= {
1207 .llseek
= generic_file_llseek
,
1208 .read
= generic_read_dir
,
1209 .readdir
= fuse_readdir
,
1210 .open
= fuse_dir_open
,
1211 .release
= fuse_dir_release
,
1212 .fsync
= fuse_dir_fsync
,
1215 static struct inode_operations fuse_common_inode_operations
= {
1216 .setattr
= fuse_setattr
,
1217 .permission
= fuse_permission
,
1218 .getattr
= fuse_getattr
,
1219 .setxattr
= fuse_setxattr
,
1220 .getxattr
= fuse_getxattr
,
1221 .listxattr
= fuse_listxattr
,
1222 .removexattr
= fuse_removexattr
,
1225 static struct inode_operations fuse_symlink_inode_operations
= {
1226 .setattr
= fuse_setattr
,
1227 .follow_link
= fuse_follow_link
,
1228 .put_link
= fuse_put_link
,
1229 .readlink
= generic_readlink
,
1230 .getattr
= fuse_getattr
,
1231 .setxattr
= fuse_setxattr
,
1232 .getxattr
= fuse_getxattr
,
1233 .listxattr
= fuse_listxattr
,
1234 .removexattr
= fuse_removexattr
,
1237 void fuse_init_common(struct inode
*inode
)
1239 inode
->i_op
= &fuse_common_inode_operations
;
1242 void fuse_init_dir(struct inode
*inode
)
1244 inode
->i_op
= &fuse_dir_inode_operations
;
1245 inode
->i_fop
= &fuse_dir_operations
;
1248 void fuse_init_symlink(struct inode
*inode
)
1250 inode
->i_op
= &fuse_symlink_inode_operations
;