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
);
115 /* For negative dentries, always do a fresh lookup */
119 fc
= get_fuse_conn(inode
);
120 req
= fuse_get_request(fc
);
124 fuse_lookup_init(req
, entry
->d_parent
->d_inode
, entry
, &outarg
);
125 request_send(fc
, req
);
126 err
= req
->out
.h
.error
;
127 /* Zero nodeid is same as -ENOENT */
128 if (!err
&& !outarg
.nodeid
)
131 struct fuse_inode
*fi
= get_fuse_inode(inode
);
132 if (outarg
.nodeid
!= get_node_id(inode
)) {
133 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
138 fuse_put_request(fc
, req
);
139 if (err
|| (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
142 fuse_change_attributes(inode
, &outarg
.attr
);
143 fuse_change_timeout(entry
, &outarg
);
149 * Check if there's already a hashed alias of this directory inode.
150 * If yes, then lookup and mkdir must not create a new alias.
152 static int dir_alias(struct inode
*inode
)
154 if (S_ISDIR(inode
->i_mode
)) {
155 struct dentry
*alias
= d_find_alias(inode
);
164 static int invalid_nodeid(u64 nodeid
)
166 return !nodeid
|| nodeid
== FUSE_ROOT_ID
;
169 static struct dentry_operations fuse_dentry_operations
= {
170 .d_revalidate
= fuse_dentry_revalidate
,
173 static int valid_mode(int m
)
175 return S_ISREG(m
) || S_ISDIR(m
) || S_ISLNK(m
) || S_ISCHR(m
) ||
176 S_ISBLK(m
) || S_ISFIFO(m
) || S_ISSOCK(m
);
179 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
180 struct nameidata
*nd
)
183 struct fuse_entry_out outarg
;
184 struct inode
*inode
= NULL
;
185 struct fuse_conn
*fc
= get_fuse_conn(dir
);
186 struct fuse_req
*req
;
188 if (entry
->d_name
.len
> FUSE_NAME_MAX
)
189 return ERR_PTR(-ENAMETOOLONG
);
191 req
= fuse_get_request(fc
);
193 return ERR_PTR(-EINTR
);
195 fuse_lookup_init(req
, dir
, entry
, &outarg
);
196 request_send(fc
, req
);
197 err
= req
->out
.h
.error
;
198 /* Zero nodeid is same as -ENOENT, but with valid timeout */
199 if (!err
&& outarg
.nodeid
&&
200 (invalid_nodeid(outarg
.nodeid
) || !valid_mode(outarg
.attr
.mode
)))
202 if (!err
&& outarg
.nodeid
) {
203 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
206 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
207 return ERR_PTR(-ENOMEM
);
210 fuse_put_request(fc
, req
);
211 if (err
&& err
!= -ENOENT
)
214 if (inode
&& dir_alias(inode
)) {
216 return ERR_PTR(-EIO
);
219 entry
->d_op
= &fuse_dentry_operations
;
221 fuse_change_timeout(entry
, &outarg
);
223 fuse_invalidate_entry_cache(entry
);
228 * Atomic create+open operation
230 * If the filesystem doesn't support this, then fall back to separate
231 * 'mknod' + 'open' requests.
233 static int fuse_create_open(struct inode
*dir
, struct dentry
*entry
, int mode
,
234 struct nameidata
*nd
)
238 struct fuse_conn
*fc
= get_fuse_conn(dir
);
239 struct fuse_req
*req
;
240 struct fuse_open_in inarg
;
241 struct fuse_open_out outopen
;
242 struct fuse_entry_out outentry
;
243 struct fuse_file
*ff
;
245 int flags
= nd
->intent
.open
.flags
- 1;
252 req
= fuse_get_request(fc
);
256 ff
= fuse_file_alloc();
258 goto out_put_request
;
261 memset(&inarg
, 0, sizeof(inarg
));
264 req
->in
.h
.opcode
= FUSE_CREATE
;
265 req
->in
.h
.nodeid
= get_node_id(dir
);
268 req
->in
.args
[0].size
= sizeof(inarg
);
269 req
->in
.args
[0].value
= &inarg
;
270 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
271 req
->in
.args
[1].value
= entry
->d_name
.name
;
272 req
->out
.numargs
= 2;
273 req
->out
.args
[0].size
= sizeof(outentry
);
274 req
->out
.args
[0].value
= &outentry
;
275 req
->out
.args
[1].size
= sizeof(outopen
);
276 req
->out
.args
[1].value
= &outopen
;
277 request_send(fc
, req
);
278 err
= req
->out
.h
.error
;
286 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
289 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
293 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
295 /* Special release, with inode = NULL, this will
296 trigger a 'forget' request when the release is
298 fuse_send_release(fc
, ff
, outentry
.nodeid
, NULL
, flags
, 0);
299 goto out_put_request
;
301 fuse_put_request(fc
, req
);
302 d_instantiate(entry
, inode
);
303 fuse_change_timeout(entry
, &outentry
);
304 file
= lookup_instantiate_filp(nd
, entry
, generic_file_open
);
307 fuse_send_release(fc
, ff
, outentry
.nodeid
, inode
, flags
, 0);
308 return PTR_ERR(file
);
310 fuse_finish_open(inode
, file
, ff
, &outopen
);
316 fuse_put_request(fc
, req
);
322 * Code shared between mknod, mkdir, symlink and link
324 static int create_new_entry(struct fuse_conn
*fc
, struct fuse_req
*req
,
325 struct inode
*dir
, struct dentry
*entry
,
328 struct fuse_entry_out outarg
;
332 req
->in
.h
.nodeid
= get_node_id(dir
);
334 req
->out
.numargs
= 1;
335 req
->out
.args
[0].size
= sizeof(outarg
);
336 req
->out
.args
[0].value
= &outarg
;
337 request_send(fc
, req
);
338 err
= req
->out
.h
.error
;
340 fuse_put_request(fc
, req
);
344 if (invalid_nodeid(outarg
.nodeid
))
345 goto out_put_request
;
347 if ((outarg
.attr
.mode
^ mode
) & S_IFMT
)
348 goto out_put_request
;
350 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
353 fuse_send_forget(fc
, req
, outarg
.nodeid
, 1);
356 fuse_put_request(fc
, req
);
358 if (dir_alias(inode
)) {
363 d_instantiate(entry
, inode
);
364 fuse_change_timeout(entry
, &outarg
);
365 fuse_invalidate_attr(dir
);
369 fuse_put_request(fc
, req
);
373 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, int mode
,
376 struct fuse_mknod_in inarg
;
377 struct fuse_conn
*fc
= get_fuse_conn(dir
);
378 struct fuse_req
*req
= fuse_get_request(fc
);
382 memset(&inarg
, 0, sizeof(inarg
));
384 inarg
.rdev
= new_encode_dev(rdev
);
385 req
->in
.h
.opcode
= FUSE_MKNOD
;
387 req
->in
.args
[0].size
= sizeof(inarg
);
388 req
->in
.args
[0].value
= &inarg
;
389 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
390 req
->in
.args
[1].value
= entry
->d_name
.name
;
391 return create_new_entry(fc
, req
, dir
, entry
, mode
);
394 static int fuse_create(struct inode
*dir
, struct dentry
*entry
, int mode
,
395 struct nameidata
*nd
)
397 if (nd
&& (nd
->flags
& LOOKUP_CREATE
)) {
398 int err
= fuse_create_open(dir
, entry
, mode
, nd
);
401 /* Fall back on mknod */
403 return fuse_mknod(dir
, entry
, mode
, 0);
406 static int fuse_mkdir(struct inode
*dir
, struct dentry
*entry
, int mode
)
408 struct fuse_mkdir_in inarg
;
409 struct fuse_conn
*fc
= get_fuse_conn(dir
);
410 struct fuse_req
*req
= fuse_get_request(fc
);
414 memset(&inarg
, 0, sizeof(inarg
));
416 req
->in
.h
.opcode
= FUSE_MKDIR
;
418 req
->in
.args
[0].size
= sizeof(inarg
);
419 req
->in
.args
[0].value
= &inarg
;
420 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
421 req
->in
.args
[1].value
= entry
->d_name
.name
;
422 return create_new_entry(fc
, req
, dir
, entry
, S_IFDIR
);
425 static int fuse_symlink(struct inode
*dir
, struct dentry
*entry
,
428 struct fuse_conn
*fc
= get_fuse_conn(dir
);
429 unsigned len
= strlen(link
) + 1;
430 struct fuse_req
*req
= fuse_get_request(fc
);
434 req
->in
.h
.opcode
= FUSE_SYMLINK
;
436 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
437 req
->in
.args
[0].value
= entry
->d_name
.name
;
438 req
->in
.args
[1].size
= len
;
439 req
->in
.args
[1].value
= link
;
440 return create_new_entry(fc
, req
, dir
, entry
, S_IFLNK
);
443 static int fuse_unlink(struct inode
*dir
, struct dentry
*entry
)
446 struct fuse_conn
*fc
= get_fuse_conn(dir
);
447 struct fuse_req
*req
= fuse_get_request(fc
);
451 req
->in
.h
.opcode
= FUSE_UNLINK
;
452 req
->in
.h
.nodeid
= get_node_id(dir
);
455 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
456 req
->in
.args
[0].value
= entry
->d_name
.name
;
457 request_send(fc
, req
);
458 err
= req
->out
.h
.error
;
459 fuse_put_request(fc
, req
);
461 struct inode
*inode
= entry
->d_inode
;
463 /* Set nlink to zero so the inode can be cleared, if
464 the inode does have more links this will be
465 discovered at the next lookup/getattr */
467 fuse_invalidate_attr(inode
);
468 fuse_invalidate_attr(dir
);
469 fuse_invalidate_entry_cache(entry
);
470 } else if (err
== -EINTR
)
471 fuse_invalidate_entry(entry
);
475 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
478 struct fuse_conn
*fc
= get_fuse_conn(dir
);
479 struct fuse_req
*req
= fuse_get_request(fc
);
483 req
->in
.h
.opcode
= FUSE_RMDIR
;
484 req
->in
.h
.nodeid
= get_node_id(dir
);
487 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
488 req
->in
.args
[0].value
= entry
->d_name
.name
;
489 request_send(fc
, req
);
490 err
= req
->out
.h
.error
;
491 fuse_put_request(fc
, req
);
493 entry
->d_inode
->i_nlink
= 0;
494 fuse_invalidate_attr(dir
);
495 fuse_invalidate_entry_cache(entry
);
496 } else if (err
== -EINTR
)
497 fuse_invalidate_entry(entry
);
501 static int fuse_rename(struct inode
*olddir
, struct dentry
*oldent
,
502 struct inode
*newdir
, struct dentry
*newent
)
505 struct fuse_rename_in inarg
;
506 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
507 struct fuse_req
*req
= fuse_get_request(fc
);
511 memset(&inarg
, 0, sizeof(inarg
));
512 inarg
.newdir
= get_node_id(newdir
);
513 req
->in
.h
.opcode
= FUSE_RENAME
;
514 req
->in
.h
.nodeid
= get_node_id(olddir
);
516 req
->inode2
= newdir
;
518 req
->in
.args
[0].size
= sizeof(inarg
);
519 req
->in
.args
[0].value
= &inarg
;
520 req
->in
.args
[1].size
= oldent
->d_name
.len
+ 1;
521 req
->in
.args
[1].value
= oldent
->d_name
.name
;
522 req
->in
.args
[2].size
= newent
->d_name
.len
+ 1;
523 req
->in
.args
[2].value
= newent
->d_name
.name
;
524 request_send(fc
, req
);
525 err
= req
->out
.h
.error
;
526 fuse_put_request(fc
, req
);
528 fuse_invalidate_attr(olddir
);
529 if (olddir
!= newdir
)
530 fuse_invalidate_attr(newdir
);
532 /* newent will end up negative */
534 fuse_invalidate_entry_cache(newent
);
535 } else if (err
== -EINTR
) {
536 /* If request was interrupted, DEITY only knows if the
537 rename actually took place. If the invalidation
538 fails (e.g. some process has CWD under the renamed
539 directory), then there can be inconsistency between
540 the dcache and the real filesystem. Tough luck. */
541 fuse_invalidate_entry(oldent
);
543 fuse_invalidate_entry(newent
);
549 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
550 struct dentry
*newent
)
553 struct fuse_link_in inarg
;
554 struct inode
*inode
= entry
->d_inode
;
555 struct fuse_conn
*fc
= get_fuse_conn(inode
);
556 struct fuse_req
*req
= fuse_get_request(fc
);
560 memset(&inarg
, 0, sizeof(inarg
));
561 inarg
.oldnodeid
= get_node_id(inode
);
562 req
->in
.h
.opcode
= FUSE_LINK
;
565 req
->in
.args
[0].size
= sizeof(inarg
);
566 req
->in
.args
[0].value
= &inarg
;
567 req
->in
.args
[1].size
= newent
->d_name
.len
+ 1;
568 req
->in
.args
[1].value
= newent
->d_name
.name
;
569 err
= create_new_entry(fc
, req
, newdir
, newent
, inode
->i_mode
);
570 /* Contrary to "normal" filesystems it can happen that link
571 makes two "logical" inodes point to the same "physical"
572 inode. We invalidate the attributes of the old one, so it
573 will reflect changes in the backing inode (link count,
576 if (!err
|| err
== -EINTR
)
577 fuse_invalidate_attr(inode
);
581 int fuse_do_getattr(struct inode
*inode
)
584 struct fuse_attr_out arg
;
585 struct fuse_conn
*fc
= get_fuse_conn(inode
);
586 struct fuse_req
*req
= fuse_get_request(fc
);
590 req
->in
.h
.opcode
= FUSE_GETATTR
;
591 req
->in
.h
.nodeid
= get_node_id(inode
);
593 req
->out
.numargs
= 1;
594 req
->out
.args
[0].size
= sizeof(arg
);
595 req
->out
.args
[0].value
= &arg
;
596 request_send(fc
, req
);
597 err
= req
->out
.h
.error
;
598 fuse_put_request(fc
, req
);
600 if ((inode
->i_mode
^ arg
.attr
.mode
) & S_IFMT
) {
601 make_bad_inode(inode
);
604 struct fuse_inode
*fi
= get_fuse_inode(inode
);
605 fuse_change_attributes(inode
, &arg
.attr
);
606 fi
->i_time
= time_to_jiffies(arg
.attr_valid
,
607 arg
.attr_valid_nsec
);
614 * Calling into a user-controlled filesystem gives the filesystem
615 * daemon ptrace-like capabilities over the requester process. This
616 * means, that the filesystem daemon is able to record the exact
617 * filesystem operations performed, and can also control the behavior
618 * of the requester process in otherwise impossible ways. For example
619 * it can delay the operation for arbitrary length of time allowing
620 * DoS against the requester.
622 * For this reason only those processes can call into the filesystem,
623 * for which the owner of the mount has ptrace privilege. This
624 * excludes processes started by other users, suid or sgid processes.
626 static int fuse_allow_task(struct fuse_conn
*fc
, struct task_struct
*task
)
628 if (fc
->flags
& FUSE_ALLOW_OTHER
)
631 if (task
->euid
== fc
->user_id
&&
632 task
->suid
== fc
->user_id
&&
633 task
->uid
== fc
->user_id
&&
634 task
->egid
== fc
->group_id
&&
635 task
->sgid
== fc
->group_id
&&
636 task
->gid
== fc
->group_id
)
643 * Check whether the inode attributes are still valid
645 * If the attribute validity timeout has expired, then fetch the fresh
646 * attributes with a 'getattr' request
648 * I'm not sure why cached attributes are never returned for the root
649 * inode, this is probably being too cautious.
651 static int fuse_revalidate(struct dentry
*entry
)
653 struct inode
*inode
= entry
->d_inode
;
654 struct fuse_inode
*fi
= get_fuse_inode(inode
);
655 struct fuse_conn
*fc
= get_fuse_conn(inode
);
657 if (!fuse_allow_task(fc
, current
))
659 if (get_node_id(inode
) != FUSE_ROOT_ID
&&
660 time_before_eq(jiffies
, fi
->i_time
))
663 return fuse_do_getattr(inode
);
666 static int fuse_access(struct inode
*inode
, int mask
)
668 struct fuse_conn
*fc
= get_fuse_conn(inode
);
669 struct fuse_req
*req
;
670 struct fuse_access_in inarg
;
676 req
= fuse_get_request(fc
);
680 memset(&inarg
, 0, sizeof(inarg
));
682 req
->in
.h
.opcode
= FUSE_ACCESS
;
683 req
->in
.h
.nodeid
= get_node_id(inode
);
686 req
->in
.args
[0].size
= sizeof(inarg
);
687 req
->in
.args
[0].value
= &inarg
;
688 request_send(fc
, req
);
689 err
= req
->out
.h
.error
;
690 fuse_put_request(fc
, req
);
691 if (err
== -ENOSYS
) {
699 * Check permission. The two basic access models of FUSE are:
701 * 1) Local access checking ('default_permissions' mount option) based
702 * on file mode. This is the plain old disk filesystem permission
705 * 2) "Remote" access checking, where server is responsible for
706 * checking permission in each inode operation. An exception to this
707 * is if ->permission() was invoked from sys_access() in which case an
708 * access request is sent. Execute permission is still checked
709 * locally based on file mode.
711 static int fuse_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
713 struct fuse_conn
*fc
= get_fuse_conn(inode
);
715 if (!fuse_allow_task(fc
, current
))
717 else if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
718 int err
= generic_permission(inode
, mask
, NULL
);
720 /* If permission is denied, try to refresh file
721 attributes. This is also needed, because the root
722 node will at first have no permissions */
723 if (err
== -EACCES
) {
724 err
= fuse_do_getattr(inode
);
726 err
= generic_permission(inode
, mask
, NULL
);
729 /* Note: the opposite of the above test does not
730 exist. So if permissions are revoked this won't be
731 noticed immediately, only after the attribute
732 timeout has expired */
736 int mode
= inode
->i_mode
;
737 if ((mask
& MAY_EXEC
) && !S_ISDIR(mode
) && !(mode
& S_IXUGO
))
740 if (nd
&& (nd
->flags
& LOOKUP_ACCESS
))
741 return fuse_access(inode
, mask
);
746 static int parse_dirfile(char *buf
, size_t nbytes
, struct file
*file
,
747 void *dstbuf
, filldir_t filldir
)
749 while (nbytes
>= FUSE_NAME_OFFSET
) {
750 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
751 size_t reclen
= FUSE_DIRENT_SIZE(dirent
);
753 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
758 over
= filldir(dstbuf
, dirent
->name
, dirent
->namelen
,
759 file
->f_pos
, dirent
->ino
, dirent
->type
);
765 file
->f_pos
= dirent
->off
;
771 static int fuse_readdir(struct file
*file
, void *dstbuf
, filldir_t filldir
)
776 struct inode
*inode
= file
->f_dentry
->d_inode
;
777 struct fuse_conn
*fc
= get_fuse_conn(inode
);
778 struct fuse_req
*req
;
780 if (is_bad_inode(inode
))
783 req
= fuse_get_request(fc
);
787 page
= alloc_page(GFP_KERNEL
);
789 fuse_put_request(fc
, req
);
793 req
->pages
[0] = page
;
794 fuse_read_fill(req
, file
, inode
, file
->f_pos
, PAGE_SIZE
, FUSE_READDIR
);
795 request_send(fc
, req
);
796 nbytes
= req
->out
.args
[0].size
;
797 err
= req
->out
.h
.error
;
798 fuse_put_request(fc
, req
);
800 err
= parse_dirfile(page_address(page
), nbytes
, file
, dstbuf
,
804 fuse_invalidate_attr(inode
); /* atime changed */
808 static char *read_link(struct dentry
*dentry
)
810 struct inode
*inode
= dentry
->d_inode
;
811 struct fuse_conn
*fc
= get_fuse_conn(inode
);
812 struct fuse_req
*req
= fuse_get_request(fc
);
816 return ERR_PTR(-EINTR
);
818 link
= (char *) __get_free_page(GFP_KERNEL
);
820 link
= ERR_PTR(-ENOMEM
);
823 req
->in
.h
.opcode
= FUSE_READLINK
;
824 req
->in
.h
.nodeid
= get_node_id(inode
);
827 req
->out
.numargs
= 1;
828 req
->out
.args
[0].size
= PAGE_SIZE
- 1;
829 req
->out
.args
[0].value
= link
;
830 request_send(fc
, req
);
831 if (req
->out
.h
.error
) {
832 free_page((unsigned long) link
);
833 link
= ERR_PTR(req
->out
.h
.error
);
835 link
[req
->out
.args
[0].size
] = '\0';
837 fuse_put_request(fc
, req
);
838 fuse_invalidate_attr(inode
); /* atime changed */
842 static void free_link(char *link
)
845 free_page((unsigned long) link
);
848 static void *fuse_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
850 nd_set_link(nd
, read_link(dentry
));
854 static void fuse_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *c
)
856 free_link(nd_get_link(nd
));
859 static int fuse_dir_open(struct inode
*inode
, struct file
*file
)
861 return fuse_open_common(inode
, file
, 1);
864 static int fuse_dir_release(struct inode
*inode
, struct file
*file
)
866 return fuse_release_common(inode
, file
, 1);
869 static int fuse_dir_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
871 /* nfsd can call this with no file */
872 return file
? fuse_fsync_common(file
, de
, datasync
, 1) : 0;
875 static void iattr_to_fattr(struct iattr
*iattr
, struct fuse_setattr_in
*arg
)
877 unsigned ivalid
= iattr
->ia_valid
;
879 if (ivalid
& ATTR_MODE
)
880 arg
->valid
|= FATTR_MODE
, arg
->mode
= iattr
->ia_mode
;
881 if (ivalid
& ATTR_UID
)
882 arg
->valid
|= FATTR_UID
, arg
->uid
= iattr
->ia_uid
;
883 if (ivalid
& ATTR_GID
)
884 arg
->valid
|= FATTR_GID
, arg
->gid
= iattr
->ia_gid
;
885 if (ivalid
& ATTR_SIZE
)
886 arg
->valid
|= FATTR_SIZE
, arg
->size
= iattr
->ia_size
;
887 /* You can only _set_ these together (they may change by themselves) */
888 if ((ivalid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
)) {
889 arg
->valid
|= FATTR_ATIME
| FATTR_MTIME
;
890 arg
->atime
= iattr
->ia_atime
.tv_sec
;
891 arg
->mtime
= iattr
->ia_mtime
.tv_sec
;
893 if (ivalid
& ATTR_FILE
) {
894 struct fuse_file
*ff
= iattr
->ia_file
->private_data
;
895 arg
->valid
|= FATTR_FH
;
900 static void fuse_vmtruncate(struct inode
*inode
, loff_t offset
)
904 spin_lock(&fuse_lock
);
905 need_trunc
= inode
->i_size
> offset
;
906 i_size_write(inode
, offset
);
907 spin_unlock(&fuse_lock
);
910 struct address_space
*mapping
= inode
->i_mapping
;
911 unmap_mapping_range(mapping
, offset
+ PAGE_SIZE
- 1, 0, 1);
912 truncate_inode_pages(mapping
, offset
);
917 * Set attributes, and at the same time refresh them.
919 * Truncation is slightly complicated, because the 'truncate' request
920 * may fail, in which case we don't want to touch the mapping.
921 * vmtruncate() doesn't allow for this case, so do the rlimit checking
922 * and the actual truncation by hand.
924 static int fuse_setattr(struct dentry
*entry
, struct iattr
*attr
)
926 struct inode
*inode
= entry
->d_inode
;
927 struct fuse_conn
*fc
= get_fuse_conn(inode
);
928 struct fuse_inode
*fi
= get_fuse_inode(inode
);
929 struct fuse_req
*req
;
930 struct fuse_setattr_in inarg
;
931 struct fuse_attr_out outarg
;
935 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
936 err
= inode_change_ok(inode
, attr
);
941 if (attr
->ia_valid
& ATTR_SIZE
) {
944 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
945 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> (loff_t
) limit
) {
946 send_sig(SIGXFSZ
, current
, 0);
951 req
= fuse_get_request(fc
);
955 memset(&inarg
, 0, sizeof(inarg
));
956 iattr_to_fattr(attr
, &inarg
);
957 req
->in
.h
.opcode
= FUSE_SETATTR
;
958 req
->in
.h
.nodeid
= get_node_id(inode
);
961 req
->in
.args
[0].size
= sizeof(inarg
);
962 req
->in
.args
[0].value
= &inarg
;
963 req
->out
.numargs
= 1;
964 req
->out
.args
[0].size
= sizeof(outarg
);
965 req
->out
.args
[0].value
= &outarg
;
966 request_send(fc
, req
);
967 err
= req
->out
.h
.error
;
968 fuse_put_request(fc
, req
);
970 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
971 make_bad_inode(inode
);
975 fuse_vmtruncate(inode
, outarg
.attr
.size
);
976 fuse_change_attributes(inode
, &outarg
.attr
);
977 fi
->i_time
= time_to_jiffies(outarg
.attr_valid
,
978 outarg
.attr_valid_nsec
);
980 } else if (err
== -EINTR
)
981 fuse_invalidate_attr(inode
);
986 static int fuse_getattr(struct vfsmount
*mnt
, struct dentry
*entry
,
989 struct inode
*inode
= entry
->d_inode
;
990 int err
= fuse_revalidate(entry
);
992 generic_fillattr(inode
, stat
);
997 static int fuse_setxattr(struct dentry
*entry
, const char *name
,
998 const void *value
, size_t size
, int flags
)
1000 struct inode
*inode
= entry
->d_inode
;
1001 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1002 struct fuse_req
*req
;
1003 struct fuse_setxattr_in inarg
;
1006 if (fc
->no_setxattr
)
1009 req
= fuse_get_request(fc
);
1013 memset(&inarg
, 0, sizeof(inarg
));
1015 inarg
.flags
= flags
;
1016 req
->in
.h
.opcode
= FUSE_SETXATTR
;
1017 req
->in
.h
.nodeid
= get_node_id(inode
);
1019 req
->in
.numargs
= 3;
1020 req
->in
.args
[0].size
= sizeof(inarg
);
1021 req
->in
.args
[0].value
= &inarg
;
1022 req
->in
.args
[1].size
= strlen(name
) + 1;
1023 req
->in
.args
[1].value
= name
;
1024 req
->in
.args
[2].size
= size
;
1025 req
->in
.args
[2].value
= value
;
1026 request_send(fc
, req
);
1027 err
= req
->out
.h
.error
;
1028 fuse_put_request(fc
, req
);
1029 if (err
== -ENOSYS
) {
1030 fc
->no_setxattr
= 1;
1036 static ssize_t
fuse_getxattr(struct dentry
*entry
, const char *name
,
1037 void *value
, size_t size
)
1039 struct inode
*inode
= entry
->d_inode
;
1040 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1041 struct fuse_req
*req
;
1042 struct fuse_getxattr_in inarg
;
1043 struct fuse_getxattr_out outarg
;
1046 if (fc
->no_getxattr
)
1049 req
= fuse_get_request(fc
);
1053 memset(&inarg
, 0, sizeof(inarg
));
1055 req
->in
.h
.opcode
= FUSE_GETXATTR
;
1056 req
->in
.h
.nodeid
= get_node_id(inode
);
1058 req
->in
.numargs
= 2;
1059 req
->in
.args
[0].size
= sizeof(inarg
);
1060 req
->in
.args
[0].value
= &inarg
;
1061 req
->in
.args
[1].size
= strlen(name
) + 1;
1062 req
->in
.args
[1].value
= name
;
1063 /* This is really two different operations rolled into one */
1064 req
->out
.numargs
= 1;
1066 req
->out
.argvar
= 1;
1067 req
->out
.args
[0].size
= size
;
1068 req
->out
.args
[0].value
= value
;
1070 req
->out
.args
[0].size
= sizeof(outarg
);
1071 req
->out
.args
[0].value
= &outarg
;
1073 request_send(fc
, req
);
1074 ret
= req
->out
.h
.error
;
1076 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1078 if (ret
== -ENOSYS
) {
1079 fc
->no_getxattr
= 1;
1083 fuse_put_request(fc
, req
);
1087 static ssize_t
fuse_listxattr(struct dentry
*entry
, char *list
, size_t size
)
1089 struct inode
*inode
= entry
->d_inode
;
1090 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1091 struct fuse_req
*req
;
1092 struct fuse_getxattr_in inarg
;
1093 struct fuse_getxattr_out outarg
;
1096 if (fc
->no_listxattr
)
1099 req
= fuse_get_request(fc
);
1103 memset(&inarg
, 0, sizeof(inarg
));
1105 req
->in
.h
.opcode
= FUSE_LISTXATTR
;
1106 req
->in
.h
.nodeid
= get_node_id(inode
);
1108 req
->in
.numargs
= 1;
1109 req
->in
.args
[0].size
= sizeof(inarg
);
1110 req
->in
.args
[0].value
= &inarg
;
1111 /* This is really two different operations rolled into one */
1112 req
->out
.numargs
= 1;
1114 req
->out
.argvar
= 1;
1115 req
->out
.args
[0].size
= size
;
1116 req
->out
.args
[0].value
= list
;
1118 req
->out
.args
[0].size
= sizeof(outarg
);
1119 req
->out
.args
[0].value
= &outarg
;
1121 request_send(fc
, req
);
1122 ret
= req
->out
.h
.error
;
1124 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1126 if (ret
== -ENOSYS
) {
1127 fc
->no_listxattr
= 1;
1131 fuse_put_request(fc
, req
);
1135 static int fuse_removexattr(struct dentry
*entry
, const char *name
)
1137 struct inode
*inode
= entry
->d_inode
;
1138 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1139 struct fuse_req
*req
;
1142 if (fc
->no_removexattr
)
1145 req
= fuse_get_request(fc
);
1149 req
->in
.h
.opcode
= FUSE_REMOVEXATTR
;
1150 req
->in
.h
.nodeid
= get_node_id(inode
);
1152 req
->in
.numargs
= 1;
1153 req
->in
.args
[0].size
= strlen(name
) + 1;
1154 req
->in
.args
[0].value
= name
;
1155 request_send(fc
, req
);
1156 err
= req
->out
.h
.error
;
1157 fuse_put_request(fc
, req
);
1158 if (err
== -ENOSYS
) {
1159 fc
->no_removexattr
= 1;
1165 static struct inode_operations fuse_dir_inode_operations
= {
1166 .lookup
= fuse_lookup
,
1167 .mkdir
= fuse_mkdir
,
1168 .symlink
= fuse_symlink
,
1169 .unlink
= fuse_unlink
,
1170 .rmdir
= fuse_rmdir
,
1171 .rename
= fuse_rename
,
1173 .setattr
= fuse_setattr
,
1174 .create
= fuse_create
,
1175 .mknod
= fuse_mknod
,
1176 .permission
= fuse_permission
,
1177 .getattr
= fuse_getattr
,
1178 .setxattr
= fuse_setxattr
,
1179 .getxattr
= fuse_getxattr
,
1180 .listxattr
= fuse_listxattr
,
1181 .removexattr
= fuse_removexattr
,
1184 static struct file_operations fuse_dir_operations
= {
1185 .llseek
= generic_file_llseek
,
1186 .read
= generic_read_dir
,
1187 .readdir
= fuse_readdir
,
1188 .open
= fuse_dir_open
,
1189 .release
= fuse_dir_release
,
1190 .fsync
= fuse_dir_fsync
,
1193 static struct inode_operations fuse_common_inode_operations
= {
1194 .setattr
= fuse_setattr
,
1195 .permission
= fuse_permission
,
1196 .getattr
= fuse_getattr
,
1197 .setxattr
= fuse_setxattr
,
1198 .getxattr
= fuse_getxattr
,
1199 .listxattr
= fuse_listxattr
,
1200 .removexattr
= fuse_removexattr
,
1203 static struct inode_operations fuse_symlink_inode_operations
= {
1204 .setattr
= fuse_setattr
,
1205 .follow_link
= fuse_follow_link
,
1206 .put_link
= fuse_put_link
,
1207 .readlink
= generic_readlink
,
1208 .getattr
= fuse_getattr
,
1209 .setxattr
= fuse_setxattr
,
1210 .getxattr
= fuse_getxattr
,
1211 .listxattr
= fuse_listxattr
,
1212 .removexattr
= fuse_removexattr
,
1215 void fuse_init_common(struct inode
*inode
)
1217 inode
->i_op
= &fuse_common_inode_operations
;
1220 void fuse_init_dir(struct inode
*inode
)
1222 inode
->i_op
= &fuse_dir_inode_operations
;
1223 inode
->i_fop
= &fuse_dir_operations
;
1226 void fuse_init_symlink(struct inode
*inode
)
1228 inode
->i_op
= &fuse_symlink_inode_operations
;