2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 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>
18 * FUSE caches dentries and attributes with separate timeout. The
19 * time in jiffies until the dentry/attributes are valid is stored in
20 * dentry->d_time and fuse_inode->i_time respectively.
24 * Calculate the time in jiffies until a dentry/attributes are valid
26 static unsigned long time_to_jiffies(unsigned long sec
, unsigned long nsec
)
28 struct timespec ts
= {sec
, nsec
};
29 return jiffies
+ timespec_to_jiffies(&ts
);
33 * Set dentry and possibly attribute timeouts from the lookup/mk*
36 static void fuse_change_timeout(struct dentry
*entry
, struct fuse_entry_out
*o
)
38 entry
->d_time
= time_to_jiffies(o
->entry_valid
, o
->entry_valid_nsec
);
40 get_fuse_inode(entry
->d_inode
)->i_time
=
41 time_to_jiffies(o
->attr_valid
, o
->attr_valid_nsec
);
45 * Mark the attributes as stale, so that at the next call to
46 * ->getattr() they will be fetched from userspace
48 void fuse_invalidate_attr(struct inode
*inode
)
50 get_fuse_inode(inode
)->i_time
= jiffies
- 1;
54 * Just mark the entry as stale, so that a next attempt to look it up
55 * will result in a new lookup call to userspace
57 * This is called when a dentry is about to become negative and the
58 * timeout is unknown (unlink, rmdir, rename and in some cases
61 static void fuse_invalidate_entry_cache(struct dentry
*entry
)
63 entry
->d_time
= jiffies
- 1;
67 * Same as fuse_invalidate_entry_cache(), but also try to remove the
68 * dentry from the hash
70 static void fuse_invalidate_entry(struct dentry
*entry
)
73 fuse_invalidate_entry_cache(entry
);
76 static void fuse_lookup_init(struct fuse_req
*req
, struct inode
*dir
,
78 struct fuse_entry_out
*outarg
)
80 req
->in
.h
.opcode
= FUSE_LOOKUP
;
81 req
->in
.h
.nodeid
= get_node_id(dir
);
84 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
85 req
->in
.args
[0].value
= entry
->d_name
.name
;
87 req
->out
.args
[0].size
= sizeof(struct fuse_entry_out
);
88 req
->out
.args
[0].value
= outarg
;
92 * Check whether the dentry is still valid
94 * If the entry validity timeout has expired and the dentry is
95 * positive, try to redo the lookup. If the lookup results in a
96 * different inode, then let the VFS invalidate the dentry and redo
97 * the lookup once more. If the lookup results in the same inode,
98 * then refresh the attributes, timeouts and mark the dentry valid.
100 static int fuse_dentry_revalidate(struct dentry
*entry
, struct nameidata
*nd
)
102 struct inode
*inode
= entry
->d_inode
;
104 if (inode
&& is_bad_inode(inode
))
106 else if (time_after(jiffies
, entry
->d_time
)) {
108 struct fuse_entry_out outarg
;
109 struct fuse_conn
*fc
;
110 struct fuse_req
*req
;
112 /* Doesn't hurt to "reset" the validity timeout */
113 fuse_invalidate_entry_cache(entry
);
117 fc
= get_fuse_conn(inode
);
118 req
= fuse_get_request(fc
);
122 fuse_lookup_init(req
, entry
->d_parent
->d_inode
, entry
, &outarg
);
123 request_send(fc
, req
);
124 err
= req
->out
.h
.error
;
126 struct fuse_inode
*fi
= get_fuse_inode(inode
);
127 if (outarg
.nodeid
!= get_node_id(inode
)) {
128 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
133 fuse_put_request(fc
, req
);
134 if (err
|| (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
137 fuse_change_attributes(inode
, &outarg
.attr
);
138 fuse_change_timeout(entry
, &outarg
);
144 * Check if there's already a hashed alias of this directory inode.
145 * If yes, then lookup and mkdir must not create a new alias.
147 static int dir_alias(struct inode
*inode
)
149 if (S_ISDIR(inode
->i_mode
)) {
150 struct dentry
*alias
= d_find_alias(inode
);
159 static int invalid_nodeid(u64 nodeid
)
161 return !nodeid
|| nodeid
== FUSE_ROOT_ID
;
164 static struct dentry_operations fuse_dentry_operations
= {
165 .d_revalidate
= fuse_dentry_revalidate
,
168 static int valid_mode(int m
)
170 return S_ISREG(m
) || S_ISDIR(m
) || S_ISLNK(m
) || S_ISCHR(m
) ||
171 S_ISBLK(m
) || S_ISFIFO(m
) || S_ISSOCK(m
);
174 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
175 struct nameidata
*nd
)
178 struct fuse_entry_out outarg
;
179 struct inode
*inode
= NULL
;
180 struct fuse_conn
*fc
= get_fuse_conn(dir
);
181 struct fuse_req
*req
;
183 if (entry
->d_name
.len
> FUSE_NAME_MAX
)
184 return ERR_PTR(-ENAMETOOLONG
);
186 req
= fuse_get_request(fc
);
188 return ERR_PTR(-EINTR
);
190 fuse_lookup_init(req
, dir
, entry
, &outarg
);
191 request_send(fc
, req
);
192 err
= req
->out
.h
.error
;
193 if (!err
&& ((outarg
.nodeid
&& invalid_nodeid(outarg
.nodeid
)) ||
194 !valid_mode(outarg
.attr
.mode
)))
196 if (!err
&& outarg
.nodeid
) {
197 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
200 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
201 return ERR_PTR(-ENOMEM
);
204 fuse_put_request(fc
, req
);
205 if (err
&& err
!= -ENOENT
)
208 if (inode
&& dir_alias(inode
)) {
210 return ERR_PTR(-EIO
);
213 entry
->d_op
= &fuse_dentry_operations
;
215 fuse_change_timeout(entry
, &outarg
);
217 fuse_invalidate_entry_cache(entry
);
222 * Atomic create+open operation
224 * If the filesystem doesn't support this, then fall back to separate
225 * 'mknod' + 'open' requests.
227 static int fuse_create_open(struct inode
*dir
, struct dentry
*entry
, int mode
,
228 struct nameidata
*nd
)
232 struct fuse_conn
*fc
= get_fuse_conn(dir
);
233 struct fuse_req
*req
;
234 struct fuse_open_in inarg
;
235 struct fuse_open_out outopen
;
236 struct fuse_entry_out outentry
;
237 struct fuse_file
*ff
;
239 int flags
= nd
->intent
.open
.flags
- 1;
246 req
= fuse_get_request(fc
);
250 ff
= fuse_file_alloc();
252 goto out_put_request
;
255 memset(&inarg
, 0, sizeof(inarg
));
258 req
->in
.h
.opcode
= FUSE_CREATE
;
259 req
->in
.h
.nodeid
= get_node_id(dir
);
262 req
->in
.args
[0].size
= sizeof(inarg
);
263 req
->in
.args
[0].value
= &inarg
;
264 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
265 req
->in
.args
[1].value
= entry
->d_name
.name
;
266 req
->out
.numargs
= 2;
267 req
->out
.args
[0].size
= sizeof(outentry
);
268 req
->out
.args
[0].value
= &outentry
;
269 req
->out
.args
[1].size
= sizeof(outopen
);
270 req
->out
.args
[1].value
= &outopen
;
271 request_send(fc
, req
);
272 err
= req
->out
.h
.error
;
280 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
283 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
287 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
289 /* Special release, with inode = NULL, this will
290 trigger a 'forget' request when the release is
292 fuse_send_release(fc
, ff
, outentry
.nodeid
, NULL
, flags
, 0);
293 goto out_put_request
;
295 fuse_put_request(fc
, req
);
296 d_instantiate(entry
, inode
);
297 fuse_change_timeout(entry
, &outentry
);
298 file
= lookup_instantiate_filp(nd
, entry
, generic_file_open
);
301 fuse_send_release(fc
, ff
, outentry
.nodeid
, inode
, flags
, 0);
302 return PTR_ERR(file
);
304 fuse_finish_open(inode
, file
, ff
, &outopen
);
310 fuse_put_request(fc
, req
);
316 * Code shared between mknod, mkdir, symlink and link
318 static int create_new_entry(struct fuse_conn
*fc
, struct fuse_req
*req
,
319 struct inode
*dir
, struct dentry
*entry
,
322 struct fuse_entry_out outarg
;
326 req
->in
.h
.nodeid
= get_node_id(dir
);
328 req
->out
.numargs
= 1;
329 req
->out
.args
[0].size
= sizeof(outarg
);
330 req
->out
.args
[0].value
= &outarg
;
331 request_send(fc
, req
);
332 err
= req
->out
.h
.error
;
334 fuse_put_request(fc
, req
);
338 if (invalid_nodeid(outarg
.nodeid
))
339 goto out_put_request
;
341 if ((outarg
.attr
.mode
^ mode
) & S_IFMT
)
342 goto out_put_request
;
344 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
347 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
350 fuse_put_request(fc
, req
);
352 if (dir_alias(inode
)) {
357 d_instantiate(entry
, inode
);
358 fuse_change_timeout(entry
, &outarg
);
359 fuse_invalidate_attr(dir
);
363 fuse_put_request(fc
, req
);
367 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, int mode
,
370 struct fuse_mknod_in inarg
;
371 struct fuse_conn
*fc
= get_fuse_conn(dir
);
372 struct fuse_req
*req
= fuse_get_request(fc
);
376 memset(&inarg
, 0, sizeof(inarg
));
378 inarg
.rdev
= new_encode_dev(rdev
);
379 req
->in
.h
.opcode
= FUSE_MKNOD
;
381 req
->in
.args
[0].size
= sizeof(inarg
);
382 req
->in
.args
[0].value
= &inarg
;
383 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
384 req
->in
.args
[1].value
= entry
->d_name
.name
;
385 return create_new_entry(fc
, req
, dir
, entry
, mode
);
388 static int fuse_create(struct inode
*dir
, struct dentry
*entry
, int mode
,
389 struct nameidata
*nd
)
391 if (nd
&& (nd
->flags
& LOOKUP_CREATE
)) {
392 int err
= fuse_create_open(dir
, entry
, mode
, nd
);
395 /* Fall back on mknod */
397 return fuse_mknod(dir
, entry
, mode
, 0);
400 static int fuse_mkdir(struct inode
*dir
, struct dentry
*entry
, int mode
)
402 struct fuse_mkdir_in inarg
;
403 struct fuse_conn
*fc
= get_fuse_conn(dir
);
404 struct fuse_req
*req
= fuse_get_request(fc
);
408 memset(&inarg
, 0, sizeof(inarg
));
410 req
->in
.h
.opcode
= FUSE_MKDIR
;
412 req
->in
.args
[0].size
= sizeof(inarg
);
413 req
->in
.args
[0].value
= &inarg
;
414 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
415 req
->in
.args
[1].value
= entry
->d_name
.name
;
416 return create_new_entry(fc
, req
, dir
, entry
, S_IFDIR
);
419 static int fuse_symlink(struct inode
*dir
, struct dentry
*entry
,
422 struct fuse_conn
*fc
= get_fuse_conn(dir
);
423 unsigned len
= strlen(link
) + 1;
424 struct fuse_req
*req
= fuse_get_request(fc
);
428 req
->in
.h
.opcode
= FUSE_SYMLINK
;
430 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
431 req
->in
.args
[0].value
= entry
->d_name
.name
;
432 req
->in
.args
[1].size
= len
;
433 req
->in
.args
[1].value
= link
;
434 return create_new_entry(fc
, req
, dir
, entry
, S_IFLNK
);
437 static int fuse_unlink(struct inode
*dir
, struct dentry
*entry
)
440 struct fuse_conn
*fc
= get_fuse_conn(dir
);
441 struct fuse_req
*req
= fuse_get_request(fc
);
445 req
->in
.h
.opcode
= FUSE_UNLINK
;
446 req
->in
.h
.nodeid
= get_node_id(dir
);
449 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
450 req
->in
.args
[0].value
= entry
->d_name
.name
;
451 request_send(fc
, req
);
452 err
= req
->out
.h
.error
;
453 fuse_put_request(fc
, req
);
455 struct inode
*inode
= entry
->d_inode
;
457 /* Set nlink to zero so the inode can be cleared, if
458 the inode does have more links this will be
459 discovered at the next lookup/getattr */
461 fuse_invalidate_attr(inode
);
462 fuse_invalidate_attr(dir
);
463 fuse_invalidate_entry_cache(entry
);
464 } else if (err
== -EINTR
)
465 fuse_invalidate_entry(entry
);
469 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
472 struct fuse_conn
*fc
= get_fuse_conn(dir
);
473 struct fuse_req
*req
= fuse_get_request(fc
);
477 req
->in
.h
.opcode
= FUSE_RMDIR
;
478 req
->in
.h
.nodeid
= get_node_id(dir
);
481 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
482 req
->in
.args
[0].value
= entry
->d_name
.name
;
483 request_send(fc
, req
);
484 err
= req
->out
.h
.error
;
485 fuse_put_request(fc
, req
);
487 entry
->d_inode
->i_nlink
= 0;
488 fuse_invalidate_attr(dir
);
489 fuse_invalidate_entry_cache(entry
);
490 } else if (err
== -EINTR
)
491 fuse_invalidate_entry(entry
);
495 static int fuse_rename(struct inode
*olddir
, struct dentry
*oldent
,
496 struct inode
*newdir
, struct dentry
*newent
)
499 struct fuse_rename_in inarg
;
500 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
501 struct fuse_req
*req
= fuse_get_request(fc
);
505 memset(&inarg
, 0, sizeof(inarg
));
506 inarg
.newdir
= get_node_id(newdir
);
507 req
->in
.h
.opcode
= FUSE_RENAME
;
508 req
->in
.h
.nodeid
= get_node_id(olddir
);
510 req
->inode2
= newdir
;
512 req
->in
.args
[0].size
= sizeof(inarg
);
513 req
->in
.args
[0].value
= &inarg
;
514 req
->in
.args
[1].size
= oldent
->d_name
.len
+ 1;
515 req
->in
.args
[1].value
= oldent
->d_name
.name
;
516 req
->in
.args
[2].size
= newent
->d_name
.len
+ 1;
517 req
->in
.args
[2].value
= newent
->d_name
.name
;
518 request_send(fc
, req
);
519 err
= req
->out
.h
.error
;
520 fuse_put_request(fc
, req
);
522 fuse_invalidate_attr(olddir
);
523 if (olddir
!= newdir
)
524 fuse_invalidate_attr(newdir
);
526 /* newent will end up negative */
528 fuse_invalidate_entry_cache(newent
);
529 } else if (err
== -EINTR
) {
530 /* If request was interrupted, DEITY only knows if the
531 rename actually took place. If the invalidation
532 fails (e.g. some process has CWD under the renamed
533 directory), then there can be inconsistency between
534 the dcache and the real filesystem. Tough luck. */
535 fuse_invalidate_entry(oldent
);
537 fuse_invalidate_entry(newent
);
543 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
544 struct dentry
*newent
)
547 struct fuse_link_in inarg
;
548 struct inode
*inode
= entry
->d_inode
;
549 struct fuse_conn
*fc
= get_fuse_conn(inode
);
550 struct fuse_req
*req
= fuse_get_request(fc
);
554 memset(&inarg
, 0, sizeof(inarg
));
555 inarg
.oldnodeid
= get_node_id(inode
);
556 req
->in
.h
.opcode
= FUSE_LINK
;
559 req
->in
.args
[0].size
= sizeof(inarg
);
560 req
->in
.args
[0].value
= &inarg
;
561 req
->in
.args
[1].size
= newent
->d_name
.len
+ 1;
562 req
->in
.args
[1].value
= newent
->d_name
.name
;
563 err
= create_new_entry(fc
, req
, newdir
, newent
, inode
->i_mode
);
564 /* Contrary to "normal" filesystems it can happen that link
565 makes two "logical" inodes point to the same "physical"
566 inode. We invalidate the attributes of the old one, so it
567 will reflect changes in the backing inode (link count,
570 if (!err
|| err
== -EINTR
)
571 fuse_invalidate_attr(inode
);
575 int fuse_do_getattr(struct inode
*inode
)
578 struct fuse_attr_out arg
;
579 struct fuse_conn
*fc
= get_fuse_conn(inode
);
580 struct fuse_req
*req
= fuse_get_request(fc
);
584 req
->in
.h
.opcode
= FUSE_GETATTR
;
585 req
->in
.h
.nodeid
= get_node_id(inode
);
587 req
->out
.numargs
= 1;
588 req
->out
.args
[0].size
= sizeof(arg
);
589 req
->out
.args
[0].value
= &arg
;
590 request_send(fc
, req
);
591 err
= req
->out
.h
.error
;
592 fuse_put_request(fc
, req
);
594 if ((inode
->i_mode
^ arg
.attr
.mode
) & S_IFMT
) {
595 make_bad_inode(inode
);
598 struct fuse_inode
*fi
= get_fuse_inode(inode
);
599 fuse_change_attributes(inode
, &arg
.attr
);
600 fi
->i_time
= time_to_jiffies(arg
.attr_valid
,
601 arg
.attr_valid_nsec
);
608 * Calling into a user-controlled filesystem gives the filesystem
609 * daemon ptrace-like capabilities over the requester process. This
610 * means, that the filesystem daemon is able to record the exact
611 * filesystem operations performed, and can also control the behavior
612 * of the requester process in otherwise impossible ways. For example
613 * it can delay the operation for arbitrary length of time allowing
614 * DoS against the requester.
616 * For this reason only those processes can call into the filesystem,
617 * for which the owner of the mount has ptrace privilege. This
618 * excludes processes started by other users, suid or sgid processes.
620 static int fuse_allow_task(struct fuse_conn
*fc
, struct task_struct
*task
)
622 if (fc
->flags
& FUSE_ALLOW_OTHER
)
625 if (task
->euid
== fc
->user_id
&&
626 task
->suid
== fc
->user_id
&&
627 task
->uid
== fc
->user_id
&&
628 task
->egid
== fc
->group_id
&&
629 task
->sgid
== fc
->group_id
&&
630 task
->gid
== fc
->group_id
)
637 * Check whether the inode attributes are still valid
639 * If the attribute validity timeout has expired, then fetch the fresh
640 * attributes with a 'getattr' request
642 * I'm not sure why cached attributes are never returned for the root
643 * inode, this is probably being too cautious.
645 static int fuse_revalidate(struct dentry
*entry
)
647 struct inode
*inode
= entry
->d_inode
;
648 struct fuse_inode
*fi
= get_fuse_inode(inode
);
649 struct fuse_conn
*fc
= get_fuse_conn(inode
);
651 if (!fuse_allow_task(fc
, current
))
653 if (get_node_id(inode
) != FUSE_ROOT_ID
&&
654 time_before_eq(jiffies
, fi
->i_time
))
657 return fuse_do_getattr(inode
);
660 static int fuse_access(struct inode
*inode
, int mask
)
662 struct fuse_conn
*fc
= get_fuse_conn(inode
);
663 struct fuse_req
*req
;
664 struct fuse_access_in inarg
;
670 req
= fuse_get_request(fc
);
674 memset(&inarg
, 0, sizeof(inarg
));
676 req
->in
.h
.opcode
= FUSE_ACCESS
;
677 req
->in
.h
.nodeid
= get_node_id(inode
);
680 req
->in
.args
[0].size
= sizeof(inarg
);
681 req
->in
.args
[0].value
= &inarg
;
682 request_send(fc
, req
);
683 err
= req
->out
.h
.error
;
684 fuse_put_request(fc
, req
);
685 if (err
== -ENOSYS
) {
693 * Check permission. The two basic access models of FUSE are:
695 * 1) Local access checking ('default_permissions' mount option) based
696 * on file mode. This is the plain old disk filesystem permission
699 * 2) "Remote" access checking, where server is responsible for
700 * checking permission in each inode operation. An exception to this
701 * is if ->permission() was invoked from sys_access() in which case an
702 * access request is sent. Execute permission is still checked
703 * locally based on file mode.
705 static int fuse_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
707 struct fuse_conn
*fc
= get_fuse_conn(inode
);
709 if (!fuse_allow_task(fc
, current
))
711 else if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
712 int err
= generic_permission(inode
, mask
, NULL
);
714 /* If permission is denied, try to refresh file
715 attributes. This is also needed, because the root
716 node will at first have no permissions */
717 if (err
== -EACCES
) {
718 err
= fuse_do_getattr(inode
);
720 err
= generic_permission(inode
, mask
, NULL
);
723 /* Note: the opposite of the above test does not
724 exist. So if permissions are revoked this won't be
725 noticed immediately, only after the attribute
726 timeout has expired */
730 int mode
= inode
->i_mode
;
731 if ((mask
& MAY_EXEC
) && !S_ISDIR(mode
) && !(mode
& S_IXUGO
))
734 if (nd
&& (nd
->flags
& LOOKUP_ACCESS
))
735 return fuse_access(inode
, mask
);
740 static int parse_dirfile(char *buf
, size_t nbytes
, struct file
*file
,
741 void *dstbuf
, filldir_t filldir
)
743 while (nbytes
>= FUSE_NAME_OFFSET
) {
744 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
745 size_t reclen
= FUSE_DIRENT_SIZE(dirent
);
747 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
752 over
= filldir(dstbuf
, dirent
->name
, dirent
->namelen
,
753 file
->f_pos
, dirent
->ino
, dirent
->type
);
759 file
->f_pos
= dirent
->off
;
765 static int fuse_readdir(struct file
*file
, void *dstbuf
, filldir_t filldir
)
770 struct inode
*inode
= file
->f_dentry
->d_inode
;
771 struct fuse_conn
*fc
= get_fuse_conn(inode
);
772 struct fuse_req
*req
;
774 if (is_bad_inode(inode
))
777 req
= fuse_get_request(fc
);
781 page
= alloc_page(GFP_KERNEL
);
783 fuse_put_request(fc
, req
);
787 req
->pages
[0] = page
;
788 fuse_read_fill(req
, file
, inode
, file
->f_pos
, PAGE_SIZE
, FUSE_READDIR
);
789 request_send(fc
, req
);
790 nbytes
= req
->out
.args
[0].size
;
791 err
= req
->out
.h
.error
;
792 fuse_put_request(fc
, req
);
794 err
= parse_dirfile(page_address(page
), nbytes
, file
, dstbuf
,
798 fuse_invalidate_attr(inode
); /* atime changed */
802 static char *read_link(struct dentry
*dentry
)
804 struct inode
*inode
= dentry
->d_inode
;
805 struct fuse_conn
*fc
= get_fuse_conn(inode
);
806 struct fuse_req
*req
= fuse_get_request(fc
);
810 return ERR_PTR(-EINTR
);
812 link
= (char *) __get_free_page(GFP_KERNEL
);
814 link
= ERR_PTR(-ENOMEM
);
817 req
->in
.h
.opcode
= FUSE_READLINK
;
818 req
->in
.h
.nodeid
= get_node_id(inode
);
821 req
->out
.numargs
= 1;
822 req
->out
.args
[0].size
= PAGE_SIZE
- 1;
823 req
->out
.args
[0].value
= link
;
824 request_send(fc
, req
);
825 if (req
->out
.h
.error
) {
826 free_page((unsigned long) link
);
827 link
= ERR_PTR(req
->out
.h
.error
);
829 link
[req
->out
.args
[0].size
] = '\0';
831 fuse_put_request(fc
, req
);
832 fuse_invalidate_attr(inode
); /* atime changed */
836 static void free_link(char *link
)
839 free_page((unsigned long) link
);
842 static void *fuse_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
844 nd_set_link(nd
, read_link(dentry
));
848 static void fuse_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *c
)
850 free_link(nd_get_link(nd
));
853 static int fuse_dir_open(struct inode
*inode
, struct file
*file
)
855 return fuse_open_common(inode
, file
, 1);
858 static int fuse_dir_release(struct inode
*inode
, struct file
*file
)
860 return fuse_release_common(inode
, file
, 1);
863 static int fuse_dir_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
865 /* nfsd can call this with no file */
866 return file
? fuse_fsync_common(file
, de
, datasync
, 1) : 0;
869 static void iattr_to_fattr(struct iattr
*iattr
, struct fuse_setattr_in
*arg
)
871 unsigned ivalid
= iattr
->ia_valid
;
873 if (ivalid
& ATTR_MODE
)
874 arg
->valid
|= FATTR_MODE
, arg
->mode
= iattr
->ia_mode
;
875 if (ivalid
& ATTR_UID
)
876 arg
->valid
|= FATTR_UID
, arg
->uid
= iattr
->ia_uid
;
877 if (ivalid
& ATTR_GID
)
878 arg
->valid
|= FATTR_GID
, arg
->gid
= iattr
->ia_gid
;
879 if (ivalid
& ATTR_SIZE
)
880 arg
->valid
|= FATTR_SIZE
, arg
->size
= iattr
->ia_size
;
881 /* You can only _set_ these together (they may change by themselves) */
882 if ((ivalid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
)) {
883 arg
->valid
|= FATTR_ATIME
| FATTR_MTIME
;
884 arg
->atime
= iattr
->ia_atime
.tv_sec
;
885 arg
->mtime
= iattr
->ia_mtime
.tv_sec
;
887 if (ivalid
& ATTR_FILE
) {
888 struct fuse_file
*ff
= iattr
->ia_file
->private_data
;
889 arg
->valid
|= FATTR_FH
;
895 * Set attributes, and at the same time refresh them.
897 * Truncation is slightly complicated, because the 'truncate' request
898 * may fail, in which case we don't want to touch the mapping.
899 * vmtruncate() doesn't allow for this case. So do the rlimit
900 * checking by hand and call vmtruncate() only after the file has
901 * actually been truncated.
903 static int fuse_setattr(struct dentry
*entry
, struct iattr
*attr
)
905 struct inode
*inode
= entry
->d_inode
;
906 struct fuse_conn
*fc
= get_fuse_conn(inode
);
907 struct fuse_inode
*fi
= get_fuse_inode(inode
);
908 struct fuse_req
*req
;
909 struct fuse_setattr_in inarg
;
910 struct fuse_attr_out outarg
;
914 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
915 err
= inode_change_ok(inode
, attr
);
920 if (attr
->ia_valid
& ATTR_SIZE
) {
923 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
924 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> (loff_t
) limit
) {
925 send_sig(SIGXFSZ
, current
, 0);
930 req
= fuse_get_request(fc
);
934 memset(&inarg
, 0, sizeof(inarg
));
935 iattr_to_fattr(attr
, &inarg
);
936 req
->in
.h
.opcode
= FUSE_SETATTR
;
937 req
->in
.h
.nodeid
= get_node_id(inode
);
940 req
->in
.args
[0].size
= sizeof(inarg
);
941 req
->in
.args
[0].value
= &inarg
;
942 req
->out
.numargs
= 1;
943 req
->out
.args
[0].size
= sizeof(outarg
);
944 req
->out
.args
[0].value
= &outarg
;
945 request_send(fc
, req
);
946 err
= req
->out
.h
.error
;
947 fuse_put_request(fc
, req
);
949 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
950 make_bad_inode(inode
);
954 loff_t origsize
= i_size_read(inode
);
955 i_size_write(inode
, outarg
.attr
.size
);
956 if (origsize
> outarg
.attr
.size
)
957 vmtruncate(inode
, outarg
.attr
.size
);
959 fuse_change_attributes(inode
, &outarg
.attr
);
960 fi
->i_time
= time_to_jiffies(outarg
.attr_valid
,
961 outarg
.attr_valid_nsec
);
963 } else if (err
== -EINTR
)
964 fuse_invalidate_attr(inode
);
969 static int fuse_getattr(struct vfsmount
*mnt
, struct dentry
*entry
,
972 struct inode
*inode
= entry
->d_inode
;
973 int err
= fuse_revalidate(entry
);
975 generic_fillattr(inode
, stat
);
980 static int fuse_setxattr(struct dentry
*entry
, const char *name
,
981 const void *value
, size_t size
, int flags
)
983 struct inode
*inode
= entry
->d_inode
;
984 struct fuse_conn
*fc
= get_fuse_conn(inode
);
985 struct fuse_req
*req
;
986 struct fuse_setxattr_in inarg
;
992 req
= fuse_get_request(fc
);
996 memset(&inarg
, 0, sizeof(inarg
));
999 req
->in
.h
.opcode
= FUSE_SETXATTR
;
1000 req
->in
.h
.nodeid
= get_node_id(inode
);
1002 req
->in
.numargs
= 3;
1003 req
->in
.args
[0].size
= sizeof(inarg
);
1004 req
->in
.args
[0].value
= &inarg
;
1005 req
->in
.args
[1].size
= strlen(name
) + 1;
1006 req
->in
.args
[1].value
= name
;
1007 req
->in
.args
[2].size
= size
;
1008 req
->in
.args
[2].value
= value
;
1009 request_send(fc
, req
);
1010 err
= req
->out
.h
.error
;
1011 fuse_put_request(fc
, req
);
1012 if (err
== -ENOSYS
) {
1013 fc
->no_setxattr
= 1;
1019 static ssize_t
fuse_getxattr(struct dentry
*entry
, const char *name
,
1020 void *value
, size_t size
)
1022 struct inode
*inode
= entry
->d_inode
;
1023 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1024 struct fuse_req
*req
;
1025 struct fuse_getxattr_in inarg
;
1026 struct fuse_getxattr_out outarg
;
1029 if (fc
->no_getxattr
)
1032 req
= fuse_get_request(fc
);
1036 memset(&inarg
, 0, sizeof(inarg
));
1038 req
->in
.h
.opcode
= FUSE_GETXATTR
;
1039 req
->in
.h
.nodeid
= get_node_id(inode
);
1041 req
->in
.numargs
= 2;
1042 req
->in
.args
[0].size
= sizeof(inarg
);
1043 req
->in
.args
[0].value
= &inarg
;
1044 req
->in
.args
[1].size
= strlen(name
) + 1;
1045 req
->in
.args
[1].value
= name
;
1046 /* This is really two different operations rolled into one */
1047 req
->out
.numargs
= 1;
1049 req
->out
.argvar
= 1;
1050 req
->out
.args
[0].size
= size
;
1051 req
->out
.args
[0].value
= value
;
1053 req
->out
.args
[0].size
= sizeof(outarg
);
1054 req
->out
.args
[0].value
= &outarg
;
1056 request_send(fc
, req
);
1057 ret
= req
->out
.h
.error
;
1059 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1061 if (ret
== -ENOSYS
) {
1062 fc
->no_getxattr
= 1;
1066 fuse_put_request(fc
, req
);
1070 static ssize_t
fuse_listxattr(struct dentry
*entry
, char *list
, size_t size
)
1072 struct inode
*inode
= entry
->d_inode
;
1073 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1074 struct fuse_req
*req
;
1075 struct fuse_getxattr_in inarg
;
1076 struct fuse_getxattr_out outarg
;
1079 if (fc
->no_listxattr
)
1082 req
= fuse_get_request(fc
);
1086 memset(&inarg
, 0, sizeof(inarg
));
1088 req
->in
.h
.opcode
= FUSE_LISTXATTR
;
1089 req
->in
.h
.nodeid
= get_node_id(inode
);
1091 req
->in
.numargs
= 1;
1092 req
->in
.args
[0].size
= sizeof(inarg
);
1093 req
->in
.args
[0].value
= &inarg
;
1094 /* This is really two different operations rolled into one */
1095 req
->out
.numargs
= 1;
1097 req
->out
.argvar
= 1;
1098 req
->out
.args
[0].size
= size
;
1099 req
->out
.args
[0].value
= list
;
1101 req
->out
.args
[0].size
= sizeof(outarg
);
1102 req
->out
.args
[0].value
= &outarg
;
1104 request_send(fc
, req
);
1105 ret
= req
->out
.h
.error
;
1107 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1109 if (ret
== -ENOSYS
) {
1110 fc
->no_listxattr
= 1;
1114 fuse_put_request(fc
, req
);
1118 static int fuse_removexattr(struct dentry
*entry
, const char *name
)
1120 struct inode
*inode
= entry
->d_inode
;
1121 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1122 struct fuse_req
*req
;
1125 if (fc
->no_removexattr
)
1128 req
= fuse_get_request(fc
);
1132 req
->in
.h
.opcode
= FUSE_REMOVEXATTR
;
1133 req
->in
.h
.nodeid
= get_node_id(inode
);
1135 req
->in
.numargs
= 1;
1136 req
->in
.args
[0].size
= strlen(name
) + 1;
1137 req
->in
.args
[0].value
= name
;
1138 request_send(fc
, req
);
1139 err
= req
->out
.h
.error
;
1140 fuse_put_request(fc
, req
);
1141 if (err
== -ENOSYS
) {
1142 fc
->no_removexattr
= 1;
1148 static struct inode_operations fuse_dir_inode_operations
= {
1149 .lookup
= fuse_lookup
,
1150 .mkdir
= fuse_mkdir
,
1151 .symlink
= fuse_symlink
,
1152 .unlink
= fuse_unlink
,
1153 .rmdir
= fuse_rmdir
,
1154 .rename
= fuse_rename
,
1156 .setattr
= fuse_setattr
,
1157 .create
= fuse_create
,
1158 .mknod
= fuse_mknod
,
1159 .permission
= fuse_permission
,
1160 .getattr
= fuse_getattr
,
1161 .setxattr
= fuse_setxattr
,
1162 .getxattr
= fuse_getxattr
,
1163 .listxattr
= fuse_listxattr
,
1164 .removexattr
= fuse_removexattr
,
1167 static struct file_operations fuse_dir_operations
= {
1168 .llseek
= generic_file_llseek
,
1169 .read
= generic_read_dir
,
1170 .readdir
= fuse_readdir
,
1171 .open
= fuse_dir_open
,
1172 .release
= fuse_dir_release
,
1173 .fsync
= fuse_dir_fsync
,
1176 static struct inode_operations fuse_common_inode_operations
= {
1177 .setattr
= fuse_setattr
,
1178 .permission
= fuse_permission
,
1179 .getattr
= fuse_getattr
,
1180 .setxattr
= fuse_setxattr
,
1181 .getxattr
= fuse_getxattr
,
1182 .listxattr
= fuse_listxattr
,
1183 .removexattr
= fuse_removexattr
,
1186 static struct inode_operations fuse_symlink_inode_operations
= {
1187 .setattr
= fuse_setattr
,
1188 .follow_link
= fuse_follow_link
,
1189 .put_link
= fuse_put_link
,
1190 .readlink
= generic_readlink
,
1191 .getattr
= fuse_getattr
,
1192 .setxattr
= fuse_setxattr
,
1193 .getxattr
= fuse_getxattr
,
1194 .listxattr
= fuse_listxattr
,
1195 .removexattr
= fuse_removexattr
,
1198 void fuse_init_common(struct inode
*inode
)
1200 inode
->i_op
= &fuse_common_inode_operations
;
1203 void fuse_init_dir(struct inode
*inode
)
1205 inode
->i_op
= &fuse_dir_inode_operations
;
1206 inode
->i_fop
= &fuse_dir_operations
;
1209 void fuse_init_symlink(struct inode
*inode
)
1211 inode
->i_op
= &fuse_symlink_inode_operations
;