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
;
141 struct fuse_req
*forget_req
;
142 struct dentry
*parent
;
144 /* For negative dentries, always do a fresh lookup */
148 fc
= get_fuse_conn(inode
);
149 req
= fuse_get_req(fc
);
153 forget_req
= fuse_get_req(fc
);
154 if (IS_ERR(forget_req
)) {
155 fuse_put_request(fc
, req
);
159 parent
= dget_parent(entry
);
160 fuse_lookup_init(req
, parent
->d_inode
, entry
, &outarg
);
161 request_send(fc
, req
);
163 err
= req
->out
.h
.error
;
164 fuse_put_request(fc
, req
);
165 /* Zero nodeid is same as -ENOENT */
166 if (!err
&& !outarg
.nodeid
)
169 struct fuse_inode
*fi
= get_fuse_inode(inode
);
170 if (outarg
.nodeid
!= get_node_id(inode
)) {
171 fuse_send_forget(fc
, forget_req
,
175 spin_lock(&fc
->lock
);
177 spin_unlock(&fc
->lock
);
179 fuse_put_request(fc
, forget_req
);
180 if (err
|| (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
183 fuse_change_attributes(inode
, &outarg
.attr
);
184 fuse_change_timeout(entry
, &outarg
);
189 static int invalid_nodeid(u64 nodeid
)
191 return !nodeid
|| nodeid
== FUSE_ROOT_ID
;
194 static struct dentry_operations fuse_dentry_operations
= {
195 .d_revalidate
= fuse_dentry_revalidate
,
198 int fuse_valid_type(int m
)
200 return S_ISREG(m
) || S_ISDIR(m
) || S_ISLNK(m
) || S_ISCHR(m
) ||
201 S_ISBLK(m
) || S_ISFIFO(m
) || S_ISSOCK(m
);
205 * Add a directory inode to a dentry, ensuring that no other dentry
206 * refers to this inode. Called with fc->inst_mutex.
208 static int fuse_d_add_directory(struct dentry
*entry
, struct inode
*inode
)
210 struct dentry
*alias
= d_find_alias(inode
);
212 /* This tries to shrink the subtree below alias */
213 fuse_invalidate_entry(alias
);
215 if (!list_empty(&inode
->i_dentry
))
222 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
223 struct nameidata
*nd
)
226 struct fuse_entry_out outarg
;
227 struct inode
*inode
= NULL
;
228 struct fuse_conn
*fc
= get_fuse_conn(dir
);
229 struct fuse_req
*req
;
230 struct fuse_req
*forget_req
;
232 if (entry
->d_name
.len
> FUSE_NAME_MAX
)
233 return ERR_PTR(-ENAMETOOLONG
);
235 req
= fuse_get_req(fc
);
237 return ERR_PTR(PTR_ERR(req
));
239 forget_req
= fuse_get_req(fc
);
240 if (IS_ERR(forget_req
)) {
241 fuse_put_request(fc
, req
);
242 return ERR_PTR(PTR_ERR(forget_req
));
245 fuse_lookup_init(req
, dir
, entry
, &outarg
);
246 request_send(fc
, req
);
247 err
= req
->out
.h
.error
;
248 fuse_put_request(fc
, req
);
249 /* Zero nodeid is same as -ENOENT, but with valid timeout */
250 if (!err
&& outarg
.nodeid
&&
251 (invalid_nodeid(outarg
.nodeid
) ||
252 !fuse_valid_type(outarg
.attr
.mode
)))
254 if (!err
&& outarg
.nodeid
) {
255 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
258 fuse_send_forget(fc
, forget_req
, outarg
.nodeid
, 1);
259 return ERR_PTR(-ENOMEM
);
262 fuse_put_request(fc
, forget_req
);
263 if (err
&& err
!= -ENOENT
)
266 if (inode
&& S_ISDIR(inode
->i_mode
)) {
267 mutex_lock(&fc
->inst_mutex
);
268 err
= fuse_d_add_directory(entry
, inode
);
269 mutex_unlock(&fc
->inst_mutex
);
277 entry
->d_op
= &fuse_dentry_operations
;
279 fuse_change_timeout(entry
, &outarg
);
281 fuse_invalidate_entry_cache(entry
);
286 * Synchronous release for the case when something goes wrong in CREATE_OPEN
288 static void fuse_sync_release(struct fuse_conn
*fc
, struct fuse_file
*ff
,
289 u64 nodeid
, int flags
)
291 fuse_release_fill(ff
, nodeid
, flags
, FUSE_RELEASE
);
292 ff
->reserved_req
->force
= 1;
293 request_send(fc
, ff
->reserved_req
);
294 fuse_put_request(fc
, ff
->reserved_req
);
299 * Atomic create+open operation
301 * If the filesystem doesn't support this, then fall back to separate
302 * 'mknod' + 'open' requests.
304 static int fuse_create_open(struct inode
*dir
, struct dentry
*entry
, int mode
,
305 struct nameidata
*nd
)
309 struct fuse_conn
*fc
= get_fuse_conn(dir
);
310 struct fuse_req
*req
;
311 struct fuse_req
*forget_req
;
312 struct fuse_open_in inarg
;
313 struct fuse_open_out outopen
;
314 struct fuse_entry_out outentry
;
315 struct fuse_file
*ff
;
317 int flags
= nd
->intent
.open
.flags
- 1;
322 forget_req
= fuse_get_req(fc
);
323 if (IS_ERR(forget_req
))
324 return PTR_ERR(forget_req
);
326 req
= fuse_get_req(fc
);
329 goto out_put_forget_req
;
332 ff
= fuse_file_alloc();
334 goto out_put_request
;
337 memset(&inarg
, 0, sizeof(inarg
));
340 req
->in
.h
.opcode
= FUSE_CREATE
;
341 req
->in
.h
.nodeid
= get_node_id(dir
);
343 req
->in
.args
[0].size
= sizeof(inarg
);
344 req
->in
.args
[0].value
= &inarg
;
345 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
346 req
->in
.args
[1].value
= entry
->d_name
.name
;
347 req
->out
.numargs
= 2;
348 req
->out
.args
[0].size
= sizeof(outentry
);
349 req
->out
.args
[0].value
= &outentry
;
350 req
->out
.args
[1].size
= sizeof(outopen
);
351 req
->out
.args
[1].value
= &outopen
;
352 request_send(fc
, req
);
353 err
= req
->out
.h
.error
;
361 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
364 fuse_put_request(fc
, req
);
365 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
368 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
370 fuse_sync_release(fc
, ff
, outentry
.nodeid
, flags
);
371 fuse_send_forget(fc
, forget_req
, outentry
.nodeid
, 1);
374 fuse_put_request(fc
, forget_req
);
375 d_instantiate(entry
, inode
);
376 fuse_change_timeout(entry
, &outentry
);
377 file
= lookup_instantiate_filp(nd
, entry
, generic_file_open
);
380 fuse_sync_release(fc
, ff
, outentry
.nodeid
, flags
);
381 return PTR_ERR(file
);
383 fuse_finish_open(inode
, file
, ff
, &outopen
);
389 fuse_put_request(fc
, req
);
391 fuse_put_request(fc
, forget_req
);
396 * Code shared between mknod, mkdir, symlink and link
398 static int create_new_entry(struct fuse_conn
*fc
, struct fuse_req
*req
,
399 struct inode
*dir
, struct dentry
*entry
,
402 struct fuse_entry_out outarg
;
405 struct fuse_req
*forget_req
;
407 forget_req
= fuse_get_req(fc
);
408 if (IS_ERR(forget_req
)) {
409 fuse_put_request(fc
, req
);
410 return PTR_ERR(forget_req
);
413 req
->in
.h
.nodeid
= get_node_id(dir
);
414 req
->out
.numargs
= 1;
415 req
->out
.args
[0].size
= sizeof(outarg
);
416 req
->out
.args
[0].value
= &outarg
;
417 request_send(fc
, req
);
418 err
= req
->out
.h
.error
;
419 fuse_put_request(fc
, req
);
421 goto out_put_forget_req
;
424 if (invalid_nodeid(outarg
.nodeid
))
425 goto out_put_forget_req
;
427 if ((outarg
.attr
.mode
^ mode
) & S_IFMT
)
428 goto out_put_forget_req
;
430 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
433 fuse_send_forget(fc
, forget_req
, outarg
.nodeid
, 1);
436 fuse_put_request(fc
, forget_req
);
438 if (S_ISDIR(inode
->i_mode
)) {
439 struct dentry
*alias
;
440 mutex_lock(&fc
->inst_mutex
);
441 alias
= d_find_alias(inode
);
443 /* New directory must have moved since mkdir */
444 mutex_unlock(&fc
->inst_mutex
);
449 d_instantiate(entry
, inode
);
450 mutex_unlock(&fc
->inst_mutex
);
452 d_instantiate(entry
, inode
);
454 fuse_change_timeout(entry
, &outarg
);
455 fuse_invalidate_attr(dir
);
459 fuse_put_request(fc
, forget_req
);
463 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, int mode
,
466 struct fuse_mknod_in inarg
;
467 struct fuse_conn
*fc
= get_fuse_conn(dir
);
468 struct fuse_req
*req
= fuse_get_req(fc
);
472 memset(&inarg
, 0, sizeof(inarg
));
474 inarg
.rdev
= new_encode_dev(rdev
);
475 req
->in
.h
.opcode
= FUSE_MKNOD
;
477 req
->in
.args
[0].size
= sizeof(inarg
);
478 req
->in
.args
[0].value
= &inarg
;
479 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
480 req
->in
.args
[1].value
= entry
->d_name
.name
;
481 return create_new_entry(fc
, req
, dir
, entry
, mode
);
484 static int fuse_create(struct inode
*dir
, struct dentry
*entry
, int mode
,
485 struct nameidata
*nd
)
487 if (nd
&& (nd
->flags
& LOOKUP_OPEN
)) {
488 int err
= fuse_create_open(dir
, entry
, mode
, nd
);
491 /* Fall back on mknod */
493 return fuse_mknod(dir
, entry
, mode
, 0);
496 static int fuse_mkdir(struct inode
*dir
, struct dentry
*entry
, int mode
)
498 struct fuse_mkdir_in inarg
;
499 struct fuse_conn
*fc
= get_fuse_conn(dir
);
500 struct fuse_req
*req
= fuse_get_req(fc
);
504 memset(&inarg
, 0, sizeof(inarg
));
506 req
->in
.h
.opcode
= FUSE_MKDIR
;
508 req
->in
.args
[0].size
= sizeof(inarg
);
509 req
->in
.args
[0].value
= &inarg
;
510 req
->in
.args
[1].size
= entry
->d_name
.len
+ 1;
511 req
->in
.args
[1].value
= entry
->d_name
.name
;
512 return create_new_entry(fc
, req
, dir
, entry
, S_IFDIR
);
515 static int fuse_symlink(struct inode
*dir
, struct dentry
*entry
,
518 struct fuse_conn
*fc
= get_fuse_conn(dir
);
519 unsigned len
= strlen(link
) + 1;
520 struct fuse_req
*req
= fuse_get_req(fc
);
524 req
->in
.h
.opcode
= FUSE_SYMLINK
;
526 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
527 req
->in
.args
[0].value
= entry
->d_name
.name
;
528 req
->in
.args
[1].size
= len
;
529 req
->in
.args
[1].value
= link
;
530 return create_new_entry(fc
, req
, dir
, entry
, S_IFLNK
);
533 static int fuse_unlink(struct inode
*dir
, struct dentry
*entry
)
536 struct fuse_conn
*fc
= get_fuse_conn(dir
);
537 struct fuse_req
*req
= fuse_get_req(fc
);
541 req
->in
.h
.opcode
= FUSE_UNLINK
;
542 req
->in
.h
.nodeid
= get_node_id(dir
);
544 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
545 req
->in
.args
[0].value
= entry
->d_name
.name
;
546 request_send(fc
, req
);
547 err
= req
->out
.h
.error
;
548 fuse_put_request(fc
, req
);
550 struct inode
*inode
= entry
->d_inode
;
552 /* Set nlink to zero so the inode can be cleared, if
553 the inode does have more links this will be
554 discovered at the next lookup/getattr */
556 fuse_invalidate_attr(inode
);
557 fuse_invalidate_attr(dir
);
558 fuse_invalidate_entry_cache(entry
);
559 } else if (err
== -EINTR
)
560 fuse_invalidate_entry(entry
);
564 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
567 struct fuse_conn
*fc
= get_fuse_conn(dir
);
568 struct fuse_req
*req
= fuse_get_req(fc
);
572 req
->in
.h
.opcode
= FUSE_RMDIR
;
573 req
->in
.h
.nodeid
= get_node_id(dir
);
575 req
->in
.args
[0].size
= entry
->d_name
.len
+ 1;
576 req
->in
.args
[0].value
= entry
->d_name
.name
;
577 request_send(fc
, req
);
578 err
= req
->out
.h
.error
;
579 fuse_put_request(fc
, req
);
581 clear_nlink(entry
->d_inode
);
582 fuse_invalidate_attr(dir
);
583 fuse_invalidate_entry_cache(entry
);
584 } else if (err
== -EINTR
)
585 fuse_invalidate_entry(entry
);
589 static int fuse_rename(struct inode
*olddir
, struct dentry
*oldent
,
590 struct inode
*newdir
, struct dentry
*newent
)
593 struct fuse_rename_in inarg
;
594 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
595 struct fuse_req
*req
= fuse_get_req(fc
);
599 memset(&inarg
, 0, sizeof(inarg
));
600 inarg
.newdir
= get_node_id(newdir
);
601 req
->in
.h
.opcode
= FUSE_RENAME
;
602 req
->in
.h
.nodeid
= get_node_id(olddir
);
604 req
->in
.args
[0].size
= sizeof(inarg
);
605 req
->in
.args
[0].value
= &inarg
;
606 req
->in
.args
[1].size
= oldent
->d_name
.len
+ 1;
607 req
->in
.args
[1].value
= oldent
->d_name
.name
;
608 req
->in
.args
[2].size
= newent
->d_name
.len
+ 1;
609 req
->in
.args
[2].value
= newent
->d_name
.name
;
610 request_send(fc
, req
);
611 err
= req
->out
.h
.error
;
612 fuse_put_request(fc
, req
);
614 fuse_invalidate_attr(olddir
);
615 if (olddir
!= newdir
)
616 fuse_invalidate_attr(newdir
);
618 /* newent will end up negative */
620 fuse_invalidate_entry_cache(newent
);
621 } else if (err
== -EINTR
) {
622 /* If request was interrupted, DEITY only knows if the
623 rename actually took place. If the invalidation
624 fails (e.g. some process has CWD under the renamed
625 directory), then there can be inconsistency between
626 the dcache and the real filesystem. Tough luck. */
627 fuse_invalidate_entry(oldent
);
629 fuse_invalidate_entry(newent
);
635 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
636 struct dentry
*newent
)
639 struct fuse_link_in inarg
;
640 struct inode
*inode
= entry
->d_inode
;
641 struct fuse_conn
*fc
= get_fuse_conn(inode
);
642 struct fuse_req
*req
= fuse_get_req(fc
);
646 memset(&inarg
, 0, sizeof(inarg
));
647 inarg
.oldnodeid
= get_node_id(inode
);
648 req
->in
.h
.opcode
= FUSE_LINK
;
650 req
->in
.args
[0].size
= sizeof(inarg
);
651 req
->in
.args
[0].value
= &inarg
;
652 req
->in
.args
[1].size
= newent
->d_name
.len
+ 1;
653 req
->in
.args
[1].value
= newent
->d_name
.name
;
654 err
= create_new_entry(fc
, req
, newdir
, newent
, inode
->i_mode
);
655 /* Contrary to "normal" filesystems it can happen that link
656 makes two "logical" inodes point to the same "physical"
657 inode. We invalidate the attributes of the old one, so it
658 will reflect changes in the backing inode (link count,
661 if (!err
|| err
== -EINTR
)
662 fuse_invalidate_attr(inode
);
666 static int fuse_do_getattr(struct inode
*inode
)
669 struct fuse_attr_out arg
;
670 struct fuse_conn
*fc
= get_fuse_conn(inode
);
671 struct fuse_req
*req
= fuse_get_req(fc
);
675 req
->in
.h
.opcode
= FUSE_GETATTR
;
676 req
->in
.h
.nodeid
= get_node_id(inode
);
677 req
->out
.numargs
= 1;
678 req
->out
.args
[0].size
= sizeof(arg
);
679 req
->out
.args
[0].value
= &arg
;
680 request_send(fc
, req
);
681 err
= req
->out
.h
.error
;
682 fuse_put_request(fc
, req
);
684 if ((inode
->i_mode
^ arg
.attr
.mode
) & S_IFMT
) {
685 make_bad_inode(inode
);
688 struct fuse_inode
*fi
= get_fuse_inode(inode
);
689 fuse_change_attributes(inode
, &arg
.attr
);
690 fi
->i_time
= time_to_jiffies(arg
.attr_valid
,
691 arg
.attr_valid_nsec
);
698 * Check if attributes are still valid, and if not send a GETATTR
699 * request to refresh them.
701 static int fuse_refresh_attributes(struct inode
*inode
)
703 struct fuse_inode
*fi
= get_fuse_inode(inode
);
705 if (fi
->i_time
< get_jiffies_64())
706 return fuse_do_getattr(inode
);
712 * Calling into a user-controlled filesystem gives the filesystem
713 * daemon ptrace-like capabilities over the requester process. This
714 * means, that the filesystem daemon is able to record the exact
715 * filesystem operations performed, and can also control the behavior
716 * of the requester process in otherwise impossible ways. For example
717 * it can delay the operation for arbitrary length of time allowing
718 * DoS against the requester.
720 * For this reason only those processes can call into the filesystem,
721 * for which the owner of the mount has ptrace privilege. This
722 * excludes processes started by other users, suid or sgid processes.
724 static int fuse_allow_task(struct fuse_conn
*fc
, struct task_struct
*task
)
726 if (fc
->flags
& FUSE_ALLOW_OTHER
)
729 if (task
->euid
== fc
->user_id
&&
730 task
->suid
== fc
->user_id
&&
731 task
->uid
== fc
->user_id
&&
732 task
->egid
== fc
->group_id
&&
733 task
->sgid
== fc
->group_id
&&
734 task
->gid
== fc
->group_id
)
740 static int fuse_access(struct inode
*inode
, int mask
)
742 struct fuse_conn
*fc
= get_fuse_conn(inode
);
743 struct fuse_req
*req
;
744 struct fuse_access_in inarg
;
750 req
= fuse_get_req(fc
);
754 memset(&inarg
, 0, sizeof(inarg
));
756 req
->in
.h
.opcode
= FUSE_ACCESS
;
757 req
->in
.h
.nodeid
= get_node_id(inode
);
759 req
->in
.args
[0].size
= sizeof(inarg
);
760 req
->in
.args
[0].value
= &inarg
;
761 request_send(fc
, req
);
762 err
= req
->out
.h
.error
;
763 fuse_put_request(fc
, req
);
764 if (err
== -ENOSYS
) {
772 * Check permission. The two basic access models of FUSE are:
774 * 1) Local access checking ('default_permissions' mount option) based
775 * on file mode. This is the plain old disk filesystem permission
778 * 2) "Remote" access checking, where server is responsible for
779 * checking permission in each inode operation. An exception to this
780 * is if ->permission() was invoked from sys_access() in which case an
781 * access request is sent. Execute permission is still checked
782 * locally based on file mode.
784 static int fuse_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
786 struct fuse_conn
*fc
= get_fuse_conn(inode
);
787 bool refreshed
= false;
790 if (!fuse_allow_task(fc
, current
))
794 * If attributes are needed, refresh them before proceeding
796 if ((fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) ||
797 ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))) {
798 err
= fuse_refresh_attributes(inode
);
805 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
806 int err
= generic_permission(inode
, mask
, NULL
);
808 /* If permission is denied, try to refresh file
809 attributes. This is also needed, because the root
810 node will at first have no permissions */
811 if (err
== -EACCES
&& !refreshed
) {
812 err
= fuse_do_getattr(inode
);
814 err
= generic_permission(inode
, mask
, NULL
);
817 /* Note: the opposite of the above test does not
818 exist. So if permissions are revoked this won't be
819 noticed immediately, only after the attribute
820 timeout has expired */
821 } else if (nd
&& (nd
->flags
& (LOOKUP_ACCESS
| LOOKUP_CHDIR
))) {
822 err
= fuse_access(inode
, mask
);
823 } else if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
)) {
824 if (!(inode
->i_mode
& S_IXUGO
)) {
828 err
= fuse_do_getattr(inode
);
829 if (!err
&& !(inode
->i_mode
& S_IXUGO
))
836 static int parse_dirfile(char *buf
, size_t nbytes
, struct file
*file
,
837 void *dstbuf
, filldir_t filldir
)
839 while (nbytes
>= FUSE_NAME_OFFSET
) {
840 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
841 size_t reclen
= FUSE_DIRENT_SIZE(dirent
);
843 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
848 over
= filldir(dstbuf
, dirent
->name
, dirent
->namelen
,
849 file
->f_pos
, dirent
->ino
, dirent
->type
);
855 file
->f_pos
= dirent
->off
;
861 static int fuse_readdir(struct file
*file
, void *dstbuf
, filldir_t filldir
)
866 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
867 struct fuse_conn
*fc
= get_fuse_conn(inode
);
868 struct fuse_file
*ff
= file
->private_data
;
869 struct fuse_req
*req
;
871 if (is_bad_inode(inode
))
874 req
= fuse_get_req(fc
);
878 page
= alloc_page(GFP_KERNEL
);
880 fuse_put_request(fc
, req
);
884 req
->pages
[0] = page
;
885 fuse_read_fill(req
, ff
, inode
, file
->f_pos
, PAGE_SIZE
, FUSE_READDIR
);
886 request_send(fc
, req
);
887 nbytes
= req
->out
.args
[0].size
;
888 err
= req
->out
.h
.error
;
889 fuse_put_request(fc
, req
);
891 err
= parse_dirfile(page_address(page
), nbytes
, file
, dstbuf
,
895 fuse_invalidate_attr(inode
); /* atime changed */
899 static char *read_link(struct dentry
*dentry
)
901 struct inode
*inode
= dentry
->d_inode
;
902 struct fuse_conn
*fc
= get_fuse_conn(inode
);
903 struct fuse_req
*req
= fuse_get_req(fc
);
907 return ERR_PTR(PTR_ERR(req
));
909 link
= (char *) __get_free_page(GFP_KERNEL
);
911 link
= ERR_PTR(-ENOMEM
);
914 req
->in
.h
.opcode
= FUSE_READLINK
;
915 req
->in
.h
.nodeid
= get_node_id(inode
);
917 req
->out
.numargs
= 1;
918 req
->out
.args
[0].size
= PAGE_SIZE
- 1;
919 req
->out
.args
[0].value
= link
;
920 request_send(fc
, req
);
921 if (req
->out
.h
.error
) {
922 free_page((unsigned long) link
);
923 link
= ERR_PTR(req
->out
.h
.error
);
925 link
[req
->out
.args
[0].size
] = '\0';
927 fuse_put_request(fc
, req
);
928 fuse_invalidate_attr(inode
); /* atime changed */
932 static void free_link(char *link
)
935 free_page((unsigned long) link
);
938 static void *fuse_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
940 nd_set_link(nd
, read_link(dentry
));
944 static void fuse_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *c
)
946 free_link(nd_get_link(nd
));
949 static int fuse_dir_open(struct inode
*inode
, struct file
*file
)
951 return fuse_open_common(inode
, file
, 1);
954 static int fuse_dir_release(struct inode
*inode
, struct file
*file
)
956 return fuse_release_common(inode
, file
, 1);
959 static int fuse_dir_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
961 /* nfsd can call this with no file */
962 return file
? fuse_fsync_common(file
, de
, datasync
, 1) : 0;
965 static void iattr_to_fattr(struct iattr
*iattr
, struct fuse_setattr_in
*arg
)
967 unsigned ivalid
= iattr
->ia_valid
;
969 if (ivalid
& ATTR_MODE
)
970 arg
->valid
|= FATTR_MODE
, arg
->mode
= iattr
->ia_mode
;
971 if (ivalid
& ATTR_UID
)
972 arg
->valid
|= FATTR_UID
, arg
->uid
= iattr
->ia_uid
;
973 if (ivalid
& ATTR_GID
)
974 arg
->valid
|= FATTR_GID
, arg
->gid
= iattr
->ia_gid
;
975 if (ivalid
& ATTR_SIZE
)
976 arg
->valid
|= FATTR_SIZE
, arg
->size
= iattr
->ia_size
;
977 /* You can only _set_ these together (they may change by themselves) */
978 if ((ivalid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
)) {
979 arg
->valid
|= FATTR_ATIME
| FATTR_MTIME
;
980 arg
->atime
= iattr
->ia_atime
.tv_sec
;
981 arg
->mtime
= iattr
->ia_mtime
.tv_sec
;
983 if (ivalid
& ATTR_FILE
) {
984 struct fuse_file
*ff
= iattr
->ia_file
->private_data
;
985 arg
->valid
|= FATTR_FH
;
991 * Set attributes, and at the same time refresh them.
993 * Truncation is slightly complicated, because the 'truncate' request
994 * may fail, in which case we don't want to touch the mapping.
995 * vmtruncate() doesn't allow for this case, so do the rlimit checking
996 * and the actual truncation by hand.
998 static int fuse_setattr(struct dentry
*entry
, struct iattr
*attr
)
1000 struct inode
*inode
= entry
->d_inode
;
1001 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1002 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1003 struct fuse_req
*req
;
1004 struct fuse_setattr_in inarg
;
1005 struct fuse_attr_out outarg
;
1008 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
) {
1009 err
= inode_change_ok(inode
, attr
);
1014 if (attr
->ia_valid
& ATTR_SIZE
) {
1015 unsigned long limit
;
1016 if (IS_SWAPFILE(inode
))
1018 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1019 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> (loff_t
) limit
) {
1020 send_sig(SIGXFSZ
, current
, 0);
1025 req
= fuse_get_req(fc
);
1027 return PTR_ERR(req
);
1029 memset(&inarg
, 0, sizeof(inarg
));
1030 iattr_to_fattr(attr
, &inarg
);
1031 req
->in
.h
.opcode
= FUSE_SETATTR
;
1032 req
->in
.h
.nodeid
= get_node_id(inode
);
1033 req
->in
.numargs
= 1;
1034 req
->in
.args
[0].size
= sizeof(inarg
);
1035 req
->in
.args
[0].value
= &inarg
;
1036 req
->out
.numargs
= 1;
1037 req
->out
.args
[0].size
= sizeof(outarg
);
1038 req
->out
.args
[0].value
= &outarg
;
1039 request_send(fc
, req
);
1040 err
= req
->out
.h
.error
;
1041 fuse_put_request(fc
, req
);
1044 fuse_invalidate_attr(inode
);
1048 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
1049 make_bad_inode(inode
);
1053 fuse_change_attributes(inode
, &outarg
.attr
);
1054 fi
->i_time
= time_to_jiffies(outarg
.attr_valid
, outarg
.attr_valid_nsec
);
1058 static int fuse_getattr(struct vfsmount
*mnt
, struct dentry
*entry
,
1061 struct inode
*inode
= entry
->d_inode
;
1062 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1063 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1066 if (!fuse_allow_task(fc
, current
))
1069 err
= fuse_refresh_attributes(inode
);
1071 generic_fillattr(inode
, stat
);
1072 stat
->mode
= fi
->orig_i_mode
;
1078 static int fuse_setxattr(struct dentry
*entry
, const char *name
,
1079 const void *value
, size_t size
, int flags
)
1081 struct inode
*inode
= entry
->d_inode
;
1082 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1083 struct fuse_req
*req
;
1084 struct fuse_setxattr_in inarg
;
1087 if (fc
->no_setxattr
)
1090 req
= fuse_get_req(fc
);
1092 return PTR_ERR(req
);
1094 memset(&inarg
, 0, sizeof(inarg
));
1096 inarg
.flags
= flags
;
1097 req
->in
.h
.opcode
= FUSE_SETXATTR
;
1098 req
->in
.h
.nodeid
= get_node_id(inode
);
1099 req
->in
.numargs
= 3;
1100 req
->in
.args
[0].size
= sizeof(inarg
);
1101 req
->in
.args
[0].value
= &inarg
;
1102 req
->in
.args
[1].size
= strlen(name
) + 1;
1103 req
->in
.args
[1].value
= name
;
1104 req
->in
.args
[2].size
= size
;
1105 req
->in
.args
[2].value
= value
;
1106 request_send(fc
, req
);
1107 err
= req
->out
.h
.error
;
1108 fuse_put_request(fc
, req
);
1109 if (err
== -ENOSYS
) {
1110 fc
->no_setxattr
= 1;
1116 static ssize_t
fuse_getxattr(struct dentry
*entry
, const char *name
,
1117 void *value
, size_t size
)
1119 struct inode
*inode
= entry
->d_inode
;
1120 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1121 struct fuse_req
*req
;
1122 struct fuse_getxattr_in inarg
;
1123 struct fuse_getxattr_out outarg
;
1126 if (fc
->no_getxattr
)
1129 req
= fuse_get_req(fc
);
1131 return PTR_ERR(req
);
1133 memset(&inarg
, 0, sizeof(inarg
));
1135 req
->in
.h
.opcode
= FUSE_GETXATTR
;
1136 req
->in
.h
.nodeid
= get_node_id(inode
);
1137 req
->in
.numargs
= 2;
1138 req
->in
.args
[0].size
= sizeof(inarg
);
1139 req
->in
.args
[0].value
= &inarg
;
1140 req
->in
.args
[1].size
= strlen(name
) + 1;
1141 req
->in
.args
[1].value
= name
;
1142 /* This is really two different operations rolled into one */
1143 req
->out
.numargs
= 1;
1145 req
->out
.argvar
= 1;
1146 req
->out
.args
[0].size
= size
;
1147 req
->out
.args
[0].value
= value
;
1149 req
->out
.args
[0].size
= sizeof(outarg
);
1150 req
->out
.args
[0].value
= &outarg
;
1152 request_send(fc
, req
);
1153 ret
= req
->out
.h
.error
;
1155 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1157 if (ret
== -ENOSYS
) {
1158 fc
->no_getxattr
= 1;
1162 fuse_put_request(fc
, req
);
1166 static ssize_t
fuse_listxattr(struct dentry
*entry
, char *list
, size_t size
)
1168 struct inode
*inode
= entry
->d_inode
;
1169 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1170 struct fuse_req
*req
;
1171 struct fuse_getxattr_in inarg
;
1172 struct fuse_getxattr_out outarg
;
1175 if (fc
->no_listxattr
)
1178 req
= fuse_get_req(fc
);
1180 return PTR_ERR(req
);
1182 memset(&inarg
, 0, sizeof(inarg
));
1184 req
->in
.h
.opcode
= FUSE_LISTXATTR
;
1185 req
->in
.h
.nodeid
= get_node_id(inode
);
1186 req
->in
.numargs
= 1;
1187 req
->in
.args
[0].size
= sizeof(inarg
);
1188 req
->in
.args
[0].value
= &inarg
;
1189 /* This is really two different operations rolled into one */
1190 req
->out
.numargs
= 1;
1192 req
->out
.argvar
= 1;
1193 req
->out
.args
[0].size
= size
;
1194 req
->out
.args
[0].value
= list
;
1196 req
->out
.args
[0].size
= sizeof(outarg
);
1197 req
->out
.args
[0].value
= &outarg
;
1199 request_send(fc
, req
);
1200 ret
= req
->out
.h
.error
;
1202 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1204 if (ret
== -ENOSYS
) {
1205 fc
->no_listxattr
= 1;
1209 fuse_put_request(fc
, req
);
1213 static int fuse_removexattr(struct dentry
*entry
, const char *name
)
1215 struct inode
*inode
= entry
->d_inode
;
1216 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1217 struct fuse_req
*req
;
1220 if (fc
->no_removexattr
)
1223 req
= fuse_get_req(fc
);
1225 return PTR_ERR(req
);
1227 req
->in
.h
.opcode
= FUSE_REMOVEXATTR
;
1228 req
->in
.h
.nodeid
= get_node_id(inode
);
1229 req
->in
.numargs
= 1;
1230 req
->in
.args
[0].size
= strlen(name
) + 1;
1231 req
->in
.args
[0].value
= name
;
1232 request_send(fc
, req
);
1233 err
= req
->out
.h
.error
;
1234 fuse_put_request(fc
, req
);
1235 if (err
== -ENOSYS
) {
1236 fc
->no_removexattr
= 1;
1242 static const struct inode_operations fuse_dir_inode_operations
= {
1243 .lookup
= fuse_lookup
,
1244 .mkdir
= fuse_mkdir
,
1245 .symlink
= fuse_symlink
,
1246 .unlink
= fuse_unlink
,
1247 .rmdir
= fuse_rmdir
,
1248 .rename
= fuse_rename
,
1250 .setattr
= fuse_setattr
,
1251 .create
= fuse_create
,
1252 .mknod
= fuse_mknod
,
1253 .permission
= fuse_permission
,
1254 .getattr
= fuse_getattr
,
1255 .setxattr
= fuse_setxattr
,
1256 .getxattr
= fuse_getxattr
,
1257 .listxattr
= fuse_listxattr
,
1258 .removexattr
= fuse_removexattr
,
1261 static const struct file_operations fuse_dir_operations
= {
1262 .llseek
= generic_file_llseek
,
1263 .read
= generic_read_dir
,
1264 .readdir
= fuse_readdir
,
1265 .open
= fuse_dir_open
,
1266 .release
= fuse_dir_release
,
1267 .fsync
= fuse_dir_fsync
,
1270 static const struct inode_operations fuse_common_inode_operations
= {
1271 .setattr
= fuse_setattr
,
1272 .permission
= fuse_permission
,
1273 .getattr
= fuse_getattr
,
1274 .setxattr
= fuse_setxattr
,
1275 .getxattr
= fuse_getxattr
,
1276 .listxattr
= fuse_listxattr
,
1277 .removexattr
= fuse_removexattr
,
1280 static const struct inode_operations fuse_symlink_inode_operations
= {
1281 .setattr
= fuse_setattr
,
1282 .follow_link
= fuse_follow_link
,
1283 .put_link
= fuse_put_link
,
1284 .readlink
= generic_readlink
,
1285 .getattr
= fuse_getattr
,
1286 .setxattr
= fuse_setxattr
,
1287 .getxattr
= fuse_getxattr
,
1288 .listxattr
= fuse_listxattr
,
1289 .removexattr
= fuse_removexattr
,
1292 void fuse_init_common(struct inode
*inode
)
1294 inode
->i_op
= &fuse_common_inode_operations
;
1297 void fuse_init_dir(struct inode
*inode
)
1299 inode
->i_op
= &fuse_dir_inode_operations
;
1300 inode
->i_fop
= &fuse_dir_operations
;
1303 void fuse_init_symlink(struct inode
*inode
)
1305 inode
->i_op
= &fuse_symlink_inode_operations
;