2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 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/sched.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 #include <linux/xattr.h>
17 #include <linux/posix_acl.h>
19 static bool fuse_use_readdirplus(struct inode
*dir
, struct dir_context
*ctx
)
21 struct fuse_conn
*fc
= get_fuse_conn(dir
);
22 struct fuse_inode
*fi
= get_fuse_inode(dir
);
24 if (!fc
->do_readdirplus
)
26 if (!fc
->readdirplus_auto
)
28 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS
, &fi
->state
))
35 static void fuse_advise_use_readdirplus(struct inode
*dir
)
37 struct fuse_inode
*fi
= get_fuse_inode(dir
);
39 set_bit(FUSE_I_ADVISE_RDPLUS
, &fi
->state
);
47 static inline void fuse_dentry_settime(struct dentry
*entry
, u64 time
)
49 ((union fuse_dentry
*) entry
->d_fsdata
)->time
= time
;
52 static inline u64
fuse_dentry_time(struct dentry
*entry
)
54 return ((union fuse_dentry
*) entry
->d_fsdata
)->time
;
58 * FUSE caches dentries and attributes with separate timeout. The
59 * time in jiffies until the dentry/attributes are valid is stored in
60 * dentry->d_fsdata and fuse_inode->i_time respectively.
64 * Calculate the time in jiffies until a dentry/attributes are valid
66 static u64
time_to_jiffies(u64 sec
, u32 nsec
)
69 struct timespec64 ts
= {
71 min_t(u32
, nsec
, NSEC_PER_SEC
- 1)
74 return get_jiffies_64() + timespec64_to_jiffies(&ts
);
80 * Set dentry and possibly attribute timeouts from the lookup/mk*
83 static void fuse_change_entry_timeout(struct dentry
*entry
,
84 struct fuse_entry_out
*o
)
86 fuse_dentry_settime(entry
,
87 time_to_jiffies(o
->entry_valid
, o
->entry_valid_nsec
));
90 static u64
attr_timeout(struct fuse_attr_out
*o
)
92 return time_to_jiffies(o
->attr_valid
, o
->attr_valid_nsec
);
95 static u64
entry_attr_timeout(struct fuse_entry_out
*o
)
97 return time_to_jiffies(o
->attr_valid
, o
->attr_valid_nsec
);
101 * Mark the attributes as stale, so that at the next call to
102 * ->getattr() they will be fetched from userspace
104 void fuse_invalidate_attr(struct inode
*inode
)
106 get_fuse_inode(inode
)->i_time
= 0;
110 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
113 void fuse_invalidate_atime(struct inode
*inode
)
115 if (!IS_RDONLY(inode
))
116 fuse_invalidate_attr(inode
);
120 * Just mark the entry as stale, so that a next attempt to look it up
121 * will result in a new lookup call to userspace
123 * This is called when a dentry is about to become negative and the
124 * timeout is unknown (unlink, rmdir, rename and in some cases
127 void fuse_invalidate_entry_cache(struct dentry
*entry
)
129 fuse_dentry_settime(entry
, 0);
133 * Same as fuse_invalidate_entry_cache(), but also try to remove the
134 * dentry from the hash
136 static void fuse_invalidate_entry(struct dentry
*entry
)
139 fuse_invalidate_entry_cache(entry
);
142 static void fuse_lookup_init(struct fuse_conn
*fc
, struct fuse_args
*args
,
143 u64 nodeid
, const struct qstr
*name
,
144 struct fuse_entry_out
*outarg
)
146 memset(outarg
, 0, sizeof(struct fuse_entry_out
));
147 args
->in
.h
.opcode
= FUSE_LOOKUP
;
148 args
->in
.h
.nodeid
= nodeid
;
149 args
->in
.numargs
= 1;
150 args
->in
.args
[0].size
= name
->len
+ 1;
151 args
->in
.args
[0].value
= name
->name
;
152 args
->out
.numargs
= 1;
153 args
->out
.args
[0].size
= sizeof(struct fuse_entry_out
);
154 args
->out
.args
[0].value
= outarg
;
157 u64
fuse_get_attr_version(struct fuse_conn
*fc
)
162 * The spin lock isn't actually needed on 64bit archs, but we
163 * don't yet care too much about such optimizations.
165 spin_lock(&fc
->lock
);
166 curr_version
= fc
->attr_version
;
167 spin_unlock(&fc
->lock
);
173 * Check whether the dentry is still valid
175 * If the entry validity timeout has expired and the dentry is
176 * positive, try to redo the lookup. If the lookup results in a
177 * different inode, then let the VFS invalidate the dentry and redo
178 * the lookup once more. If the lookup results in the same inode,
179 * then refresh the attributes, timeouts and mark the dentry valid.
181 static int fuse_dentry_revalidate(struct dentry
*entry
, unsigned int flags
)
184 struct dentry
*parent
;
185 struct fuse_conn
*fc
;
186 struct fuse_inode
*fi
;
189 inode
= d_inode_rcu(entry
);
190 if (inode
&& is_bad_inode(inode
))
192 else if (time_before64(fuse_dentry_time(entry
), get_jiffies_64()) ||
193 (flags
& LOOKUP_REVAL
)) {
194 struct fuse_entry_out outarg
;
196 struct fuse_forget_link
*forget
;
199 /* For negative dentries, always do a fresh lookup */
204 if (flags
& LOOKUP_RCU
)
207 fc
= get_fuse_conn(inode
);
209 forget
= fuse_alloc_forget();
214 attr_version
= fuse_get_attr_version(fc
);
216 parent
= dget_parent(entry
);
217 fuse_lookup_init(fc
, &args
, get_node_id(d_inode(parent
)),
218 &entry
->d_name
, &outarg
);
219 ret
= fuse_simple_request(fc
, &args
);
221 /* Zero nodeid is same as -ENOENT */
222 if (!ret
&& !outarg
.nodeid
)
225 fi
= get_fuse_inode(inode
);
226 if (outarg
.nodeid
!= get_node_id(inode
)) {
227 fuse_queue_forget(fc
, forget
, outarg
.nodeid
, 1);
230 spin_lock(&fc
->lock
);
232 spin_unlock(&fc
->lock
);
237 if (ret
|| (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
240 forget_all_cached_acls(inode
);
241 fuse_change_attributes(inode
, &outarg
.attr
,
242 entry_attr_timeout(&outarg
),
244 fuse_change_entry_timeout(entry
, &outarg
);
246 fi
= get_fuse_inode(inode
);
247 if (flags
& LOOKUP_RCU
) {
248 if (test_bit(FUSE_I_INIT_RDPLUS
, &fi
->state
))
250 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS
, &fi
->state
)) {
251 parent
= dget_parent(entry
);
252 fuse_advise_use_readdirplus(d_inode(parent
));
266 * Get the canonical path. Since we must translate to a path, this must be done
267 * in the context of the userspace daemon, however, the userspace daemon cannot
268 * look up paths on its own. Instead, we handle the lookup as a special case
269 * inside of the write request.
271 static void fuse_dentry_canonical_path(const struct path
*path
, struct path
*canonical_path
) {
272 struct inode
*inode
= path
->dentry
->d_inode
;
273 struct fuse_conn
*fc
= get_fuse_conn(inode
);
274 struct fuse_req
*req
;
278 req
= fuse_get_req(fc
, 1);
283 path_name
= (char*)__get_free_page(GFP_KERNEL
);
285 fuse_put_request(fc
, req
);
289 req
->in
.h
.opcode
= FUSE_CANONICAL_PATH
;
290 req
->in
.h
.nodeid
= get_node_id(inode
);
292 req
->out
.numargs
= 1;
293 req
->out
.args
[0].size
= PATH_MAX
;
294 req
->out
.args
[0].value
= path_name
;
295 req
->canonical_path
= canonical_path
;
297 fuse_request_send(fc
, req
);
298 err
= req
->out
.h
.error
;
299 fuse_put_request(fc
, req
);
300 free_page((unsigned long)path_name
);
304 canonical_path
->dentry
= path
->dentry
;
305 canonical_path
->mnt
= path
->mnt
;
306 path_get(canonical_path
);
309 static int invalid_nodeid(u64 nodeid
)
311 return !nodeid
|| nodeid
== FUSE_ROOT_ID
;
314 static int fuse_dentry_init(struct dentry
*dentry
)
316 dentry
->d_fsdata
= kzalloc(sizeof(union fuse_dentry
), GFP_KERNEL
);
318 return dentry
->d_fsdata
? 0 : -ENOMEM
;
320 static void fuse_dentry_release(struct dentry
*dentry
)
322 union fuse_dentry
*fd
= dentry
->d_fsdata
;
327 const struct dentry_operations fuse_dentry_operations
= {
328 .d_revalidate
= fuse_dentry_revalidate
,
329 .d_init
= fuse_dentry_init
,
330 .d_release
= fuse_dentry_release
,
331 .d_canonical_path
= fuse_dentry_canonical_path
,
334 const struct dentry_operations fuse_root_dentry_operations
= {
335 .d_init
= fuse_dentry_init
,
336 .d_release
= fuse_dentry_release
,
337 .d_canonical_path
= fuse_dentry_canonical_path
,
340 int fuse_valid_type(int m
)
342 return S_ISREG(m
) || S_ISDIR(m
) || S_ISLNK(m
) || S_ISCHR(m
) ||
343 S_ISBLK(m
) || S_ISFIFO(m
) || S_ISSOCK(m
);
346 int fuse_lookup_name(struct super_block
*sb
, u64 nodeid
, const struct qstr
*name
,
347 struct fuse_entry_out
*outarg
, struct inode
**inode
)
349 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
351 struct fuse_forget_link
*forget
;
357 if (name
->len
> FUSE_NAME_MAX
)
361 forget
= fuse_alloc_forget();
366 attr_version
= fuse_get_attr_version(fc
);
368 fuse_lookup_init(fc
, &args
, nodeid
, name
, outarg
);
369 err
= fuse_simple_request(fc
, &args
);
370 /* Zero nodeid is same as -ENOENT, but with valid timeout */
371 if (err
|| !outarg
->nodeid
)
377 if (!fuse_valid_type(outarg
->attr
.mode
))
380 *inode
= fuse_iget(sb
, outarg
->nodeid
, outarg
->generation
,
381 &outarg
->attr
, entry_attr_timeout(outarg
),
385 fuse_queue_forget(fc
, forget
, outarg
->nodeid
, 1);
396 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
400 struct fuse_entry_out outarg
;
402 struct dentry
*newent
;
403 bool outarg_valid
= true;
406 locked
= fuse_lock_inode(dir
);
407 err
= fuse_lookup_name(dir
->i_sb
, get_node_id(dir
), &entry
->d_name
,
409 fuse_unlock_inode(dir
, locked
);
410 if (err
== -ENOENT
) {
411 outarg_valid
= false;
418 if (inode
&& get_node_id(inode
) == FUSE_ROOT_ID
)
421 newent
= d_splice_alias(inode
, entry
);
422 err
= PTR_ERR(newent
);
426 entry
= newent
? newent
: entry
;
428 fuse_change_entry_timeout(entry
, &outarg
);
430 fuse_invalidate_entry_cache(entry
);
432 fuse_advise_use_readdirplus(dir
);
442 * Atomic create+open operation
444 * If the filesystem doesn't support this, then fall back to separate
445 * 'mknod' + 'open' requests.
447 static int fuse_create_open(struct inode
*dir
, struct dentry
*entry
,
448 struct file
*file
, unsigned flags
,
449 umode_t mode
, int *opened
)
453 struct fuse_conn
*fc
= get_fuse_conn(dir
);
455 struct fuse_forget_link
*forget
;
456 struct fuse_create_in inarg
;
457 struct fuse_open_out outopen
;
458 struct fuse_entry_out outentry
;
459 struct fuse_file
*ff
;
461 /* Userspace expects S_IFREG in create mode */
462 BUG_ON((mode
& S_IFMT
) != S_IFREG
);
464 forget
= fuse_alloc_forget();
470 ff
= fuse_file_alloc(fc
);
472 goto out_put_forget_req
;
475 mode
&= ~current_umask();
478 memset(&inarg
, 0, sizeof(inarg
));
479 memset(&outentry
, 0, sizeof(outentry
));
482 inarg
.umask
= current_umask();
483 args
.in
.h
.opcode
= FUSE_CREATE
;
484 args
.in
.h
.nodeid
= get_node_id(dir
);
486 args
.in
.args
[0].size
= sizeof(inarg
);
487 args
.in
.args
[0].value
= &inarg
;
488 args
.in
.args
[1].size
= entry
->d_name
.len
+ 1;
489 args
.in
.args
[1].value
= entry
->d_name
.name
;
490 args
.out
.numargs
= 2;
491 args
.out
.args
[0].size
= sizeof(outentry
);
492 args
.out
.args
[0].value
= &outentry
;
493 args
.out
.args
[1].size
= sizeof(outopen
);
494 args
.out
.args
[1].value
= &outopen
;
495 err
= fuse_simple_request(fc
, &args
);
500 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
504 ff
->nodeid
= outentry
.nodeid
;
505 ff
->open_flags
= outopen
.open_flags
;
506 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
507 &outentry
.attr
, entry_attr_timeout(&outentry
), 0);
509 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
510 fuse_sync_release(ff
, flags
);
511 fuse_queue_forget(fc
, forget
, outentry
.nodeid
, 1);
516 d_instantiate(entry
, inode
);
517 fuse_change_entry_timeout(entry
, &outentry
);
518 fuse_invalidate_attr(dir
);
519 err
= finish_open(file
, entry
, generic_file_open
, opened
);
521 fuse_sync_release(ff
, flags
);
523 file
->private_data
= ff
;
524 fuse_finish_open(inode
, file
);
536 static int fuse_mknod(struct inode
*, struct dentry
*, umode_t
, dev_t
);
537 static int fuse_atomic_open(struct inode
*dir
, struct dentry
*entry
,
538 struct file
*file
, unsigned flags
,
539 umode_t mode
, int *opened
)
542 struct fuse_conn
*fc
= get_fuse_conn(dir
);
543 struct dentry
*res
= NULL
;
545 if (d_in_lookup(entry
)) {
546 res
= fuse_lookup(dir
, entry
, 0);
554 if (!(flags
& O_CREAT
) || d_really_is_positive(entry
))
558 *opened
|= FILE_CREATED
;
563 err
= fuse_create_open(dir
, entry
, file
, flags
, mode
, opened
);
564 if (err
== -ENOSYS
) {
573 err
= fuse_mknod(dir
, entry
, mode
, 0);
577 return finish_no_open(file
, res
);
581 * Code shared between mknod, mkdir, symlink and link
583 static int create_new_entry(struct fuse_conn
*fc
, struct fuse_args
*args
,
584 struct inode
*dir
, struct dentry
*entry
,
587 struct fuse_entry_out outarg
;
590 struct fuse_forget_link
*forget
;
592 forget
= fuse_alloc_forget();
596 memset(&outarg
, 0, sizeof(outarg
));
597 args
->in
.h
.nodeid
= get_node_id(dir
);
598 args
->out
.numargs
= 1;
599 args
->out
.args
[0].size
= sizeof(outarg
);
600 args
->out
.args
[0].value
= &outarg
;
601 err
= fuse_simple_request(fc
, args
);
603 goto out_put_forget_req
;
606 if (invalid_nodeid(outarg
.nodeid
))
607 goto out_put_forget_req
;
609 if ((outarg
.attr
.mode
^ mode
) & S_IFMT
)
610 goto out_put_forget_req
;
612 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
613 &outarg
.attr
, entry_attr_timeout(&outarg
), 0);
615 fuse_queue_forget(fc
, forget
, outarg
.nodeid
, 1);
620 err
= d_instantiate_no_diralias(entry
, inode
);
624 fuse_change_entry_timeout(entry
, &outarg
);
625 fuse_invalidate_attr(dir
);
633 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, umode_t mode
,
636 struct fuse_mknod_in inarg
;
637 struct fuse_conn
*fc
= get_fuse_conn(dir
);
641 mode
&= ~current_umask();
643 memset(&inarg
, 0, sizeof(inarg
));
645 inarg
.rdev
= new_encode_dev(rdev
);
646 inarg
.umask
= current_umask();
647 args
.in
.h
.opcode
= FUSE_MKNOD
;
649 args
.in
.args
[0].size
= sizeof(inarg
);
650 args
.in
.args
[0].value
= &inarg
;
651 args
.in
.args
[1].size
= entry
->d_name
.len
+ 1;
652 args
.in
.args
[1].value
= entry
->d_name
.name
;
653 return create_new_entry(fc
, &args
, dir
, entry
, mode
);
656 static int fuse_create(struct inode
*dir
, struct dentry
*entry
, umode_t mode
,
659 return fuse_mknod(dir
, entry
, mode
, 0);
662 static int fuse_mkdir(struct inode
*dir
, struct dentry
*entry
, umode_t mode
)
664 struct fuse_mkdir_in inarg
;
665 struct fuse_conn
*fc
= get_fuse_conn(dir
);
669 mode
&= ~current_umask();
671 memset(&inarg
, 0, sizeof(inarg
));
673 inarg
.umask
= current_umask();
674 args
.in
.h
.opcode
= FUSE_MKDIR
;
676 args
.in
.args
[0].size
= sizeof(inarg
);
677 args
.in
.args
[0].value
= &inarg
;
678 args
.in
.args
[1].size
= entry
->d_name
.len
+ 1;
679 args
.in
.args
[1].value
= entry
->d_name
.name
;
680 return create_new_entry(fc
, &args
, dir
, entry
, S_IFDIR
);
683 static int fuse_symlink(struct inode
*dir
, struct dentry
*entry
,
686 struct fuse_conn
*fc
= get_fuse_conn(dir
);
687 unsigned len
= strlen(link
) + 1;
690 args
.in
.h
.opcode
= FUSE_SYMLINK
;
692 args
.in
.args
[0].size
= entry
->d_name
.len
+ 1;
693 args
.in
.args
[0].value
= entry
->d_name
.name
;
694 args
.in
.args
[1].size
= len
;
695 args
.in
.args
[1].value
= link
;
696 return create_new_entry(fc
, &args
, dir
, entry
, S_IFLNK
);
699 void fuse_update_ctime(struct inode
*inode
)
701 if (!IS_NOCMTIME(inode
)) {
702 inode
->i_ctime
= current_time(inode
);
703 mark_inode_dirty_sync(inode
);
707 static int fuse_unlink(struct inode
*dir
, struct dentry
*entry
)
710 struct fuse_conn
*fc
= get_fuse_conn(dir
);
713 args
.in
.h
.opcode
= FUSE_UNLINK
;
714 args
.in
.h
.nodeid
= get_node_id(dir
);
716 args
.in
.args
[0].size
= entry
->d_name
.len
+ 1;
717 args
.in
.args
[0].value
= entry
->d_name
.name
;
718 err
= fuse_simple_request(fc
, &args
);
720 struct inode
*inode
= d_inode(entry
);
721 struct fuse_inode
*fi
= get_fuse_inode(inode
);
723 spin_lock(&fc
->lock
);
724 fi
->attr_version
= ++fc
->attr_version
;
726 * If i_nlink == 0 then unlink doesn't make sense, yet this can
727 * happen if userspace filesystem is careless. It would be
728 * difficult to enforce correct nlink usage so just ignore this
731 if (inode
->i_nlink
> 0)
733 spin_unlock(&fc
->lock
);
734 fuse_invalidate_attr(inode
);
735 fuse_invalidate_attr(dir
);
736 fuse_invalidate_entry_cache(entry
);
737 fuse_update_ctime(inode
);
738 } else if (err
== -EINTR
)
739 fuse_invalidate_entry(entry
);
743 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
746 struct fuse_conn
*fc
= get_fuse_conn(dir
);
749 args
.in
.h
.opcode
= FUSE_RMDIR
;
750 args
.in
.h
.nodeid
= get_node_id(dir
);
752 args
.in
.args
[0].size
= entry
->d_name
.len
+ 1;
753 args
.in
.args
[0].value
= entry
->d_name
.name
;
754 err
= fuse_simple_request(fc
, &args
);
756 clear_nlink(d_inode(entry
));
757 fuse_invalidate_attr(dir
);
758 fuse_invalidate_entry_cache(entry
);
759 } else if (err
== -EINTR
)
760 fuse_invalidate_entry(entry
);
764 static int fuse_rename_common(struct inode
*olddir
, struct dentry
*oldent
,
765 struct inode
*newdir
, struct dentry
*newent
,
766 unsigned int flags
, int opcode
, size_t argsize
)
769 struct fuse_rename2_in inarg
;
770 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
773 memset(&inarg
, 0, argsize
);
774 inarg
.newdir
= get_node_id(newdir
);
776 args
.in
.h
.opcode
= opcode
;
777 args
.in
.h
.nodeid
= get_node_id(olddir
);
779 args
.in
.args
[0].size
= argsize
;
780 args
.in
.args
[0].value
= &inarg
;
781 args
.in
.args
[1].size
= oldent
->d_name
.len
+ 1;
782 args
.in
.args
[1].value
= oldent
->d_name
.name
;
783 args
.in
.args
[2].size
= newent
->d_name
.len
+ 1;
784 args
.in
.args
[2].value
= newent
->d_name
.name
;
785 err
= fuse_simple_request(fc
, &args
);
788 fuse_invalidate_attr(d_inode(oldent
));
789 fuse_update_ctime(d_inode(oldent
));
791 if (flags
& RENAME_EXCHANGE
) {
792 fuse_invalidate_attr(d_inode(newent
));
793 fuse_update_ctime(d_inode(newent
));
796 fuse_invalidate_attr(olddir
);
797 if (olddir
!= newdir
)
798 fuse_invalidate_attr(newdir
);
800 /* newent will end up negative */
801 if (!(flags
& RENAME_EXCHANGE
) && d_really_is_positive(newent
)) {
802 fuse_invalidate_attr(d_inode(newent
));
803 fuse_invalidate_entry_cache(newent
);
804 fuse_update_ctime(d_inode(newent
));
806 } else if (err
== -EINTR
) {
807 /* If request was interrupted, DEITY only knows if the
808 rename actually took place. If the invalidation
809 fails (e.g. some process has CWD under the renamed
810 directory), then there can be inconsistency between
811 the dcache and the real filesystem. Tough luck. */
812 fuse_invalidate_entry(oldent
);
813 if (d_really_is_positive(newent
))
814 fuse_invalidate_entry(newent
);
820 static int fuse_rename2(struct inode
*olddir
, struct dentry
*oldent
,
821 struct inode
*newdir
, struct dentry
*newent
,
824 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
827 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
))
831 if (fc
->no_rename2
|| fc
->minor
< 23)
834 err
= fuse_rename_common(olddir
, oldent
, newdir
, newent
, flags
,
836 sizeof(struct fuse_rename2_in
));
837 if (err
== -ENOSYS
) {
842 err
= fuse_rename_common(olddir
, oldent
, newdir
, newent
, 0,
844 sizeof(struct fuse_rename_in
));
850 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
851 struct dentry
*newent
)
854 struct fuse_link_in inarg
;
855 struct inode
*inode
= d_inode(entry
);
856 struct fuse_conn
*fc
= get_fuse_conn(inode
);
859 memset(&inarg
, 0, sizeof(inarg
));
860 inarg
.oldnodeid
= get_node_id(inode
);
861 args
.in
.h
.opcode
= FUSE_LINK
;
863 args
.in
.args
[0].size
= sizeof(inarg
);
864 args
.in
.args
[0].value
= &inarg
;
865 args
.in
.args
[1].size
= newent
->d_name
.len
+ 1;
866 args
.in
.args
[1].value
= newent
->d_name
.name
;
867 err
= create_new_entry(fc
, &args
, newdir
, newent
, inode
->i_mode
);
868 /* Contrary to "normal" filesystems it can happen that link
869 makes two "logical" inodes point to the same "physical"
870 inode. We invalidate the attributes of the old one, so it
871 will reflect changes in the backing inode (link count,
875 struct fuse_inode
*fi
= get_fuse_inode(inode
);
877 spin_lock(&fc
->lock
);
878 fi
->attr_version
= ++fc
->attr_version
;
880 spin_unlock(&fc
->lock
);
881 fuse_invalidate_attr(inode
);
882 fuse_update_ctime(inode
);
883 } else if (err
== -EINTR
) {
884 fuse_invalidate_attr(inode
);
889 static void fuse_fillattr(struct inode
*inode
, struct fuse_attr
*attr
,
892 unsigned int blkbits
;
893 struct fuse_conn
*fc
= get_fuse_conn(inode
);
895 /* see the comment in fuse_change_attributes() */
896 if (fc
->writeback_cache
&& S_ISREG(inode
->i_mode
)) {
897 attr
->size
= i_size_read(inode
);
898 attr
->mtime
= inode
->i_mtime
.tv_sec
;
899 attr
->mtimensec
= inode
->i_mtime
.tv_nsec
;
900 attr
->ctime
= inode
->i_ctime
.tv_sec
;
901 attr
->ctimensec
= inode
->i_ctime
.tv_nsec
;
904 stat
->dev
= inode
->i_sb
->s_dev
;
905 stat
->ino
= attr
->ino
;
906 stat
->mode
= (inode
->i_mode
& S_IFMT
) | (attr
->mode
& 07777);
907 stat
->nlink
= attr
->nlink
;
908 stat
->uid
= make_kuid(&init_user_ns
, attr
->uid
);
909 stat
->gid
= make_kgid(&init_user_ns
, attr
->gid
);
910 stat
->rdev
= inode
->i_rdev
;
911 stat
->atime
.tv_sec
= attr
->atime
;
912 stat
->atime
.tv_nsec
= attr
->atimensec
;
913 stat
->mtime
.tv_sec
= attr
->mtime
;
914 stat
->mtime
.tv_nsec
= attr
->mtimensec
;
915 stat
->ctime
.tv_sec
= attr
->ctime
;
916 stat
->ctime
.tv_nsec
= attr
->ctimensec
;
917 stat
->size
= attr
->size
;
918 stat
->blocks
= attr
->blocks
;
920 if (attr
->blksize
!= 0)
921 blkbits
= ilog2(attr
->blksize
);
923 blkbits
= inode
->i_sb
->s_blocksize_bits
;
925 stat
->blksize
= 1 << blkbits
;
928 static int fuse_do_getattr(struct inode
*inode
, struct kstat
*stat
,
932 struct fuse_getattr_in inarg
;
933 struct fuse_attr_out outarg
;
934 struct fuse_conn
*fc
= get_fuse_conn(inode
);
938 attr_version
= fuse_get_attr_version(fc
);
940 memset(&inarg
, 0, sizeof(inarg
));
941 memset(&outarg
, 0, sizeof(outarg
));
942 /* Directories have separate file-handle space */
943 if (file
&& S_ISREG(inode
->i_mode
)) {
944 struct fuse_file
*ff
= file
->private_data
;
946 inarg
.getattr_flags
|= FUSE_GETATTR_FH
;
949 args
.in
.h
.opcode
= FUSE_GETATTR
;
950 args
.in
.h
.nodeid
= get_node_id(inode
);
952 args
.in
.args
[0].size
= sizeof(inarg
);
953 args
.in
.args
[0].value
= &inarg
;
954 args
.out
.numargs
= 1;
955 args
.out
.args
[0].size
= sizeof(outarg
);
956 args
.out
.args
[0].value
= &outarg
;
957 err
= fuse_simple_request(fc
, &args
);
959 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
960 make_bad_inode(inode
);
963 fuse_change_attributes(inode
, &outarg
.attr
,
964 attr_timeout(&outarg
),
967 fuse_fillattr(inode
, &outarg
.attr
, stat
);
973 static int fuse_update_get_attr(struct inode
*inode
, struct file
*file
,
976 struct fuse_inode
*fi
= get_fuse_inode(inode
);
979 if (time_before64(fi
->i_time
, get_jiffies_64())) {
980 forget_all_cached_acls(inode
);
981 err
= fuse_do_getattr(inode
, stat
, file
);
983 generic_fillattr(inode
, stat
);
984 stat
->mode
= fi
->orig_i_mode
;
985 stat
->ino
= fi
->orig_ino
;
991 int fuse_update_attributes(struct inode
*inode
, struct file
*file
)
993 return fuse_update_get_attr(inode
, file
, NULL
);
996 int fuse_reverse_inval_entry(struct super_block
*sb
, u64 parent_nodeid
,
997 u64 child_nodeid
, struct qstr
*name
)
1000 struct inode
*parent
;
1002 struct dentry
*entry
;
1004 parent
= ilookup5(sb
, parent_nodeid
, fuse_inode_eq
, &parent_nodeid
);
1009 if (!S_ISDIR(parent
->i_mode
))
1013 dir
= d_find_alias(parent
);
1017 name
->hash
= full_name_hash(dir
, name
->name
, name
->len
);
1018 entry
= d_lookup(dir
, name
);
1023 fuse_invalidate_attr(parent
);
1024 fuse_invalidate_entry(entry
);
1026 if (child_nodeid
!= 0 && d_really_is_positive(entry
)) {
1027 inode_lock(d_inode(entry
));
1028 if (get_node_id(d_inode(entry
)) != child_nodeid
) {
1032 if (d_mountpoint(entry
)) {
1036 if (d_is_dir(entry
)) {
1037 shrink_dcache_parent(entry
);
1038 if (!simple_empty(entry
)) {
1042 d_inode(entry
)->i_flags
|= S_DEAD
;
1045 clear_nlink(d_inode(entry
));
1048 inode_unlock(d_inode(entry
));
1057 inode_unlock(parent
);
1063 * Calling into a user-controlled filesystem gives the filesystem
1064 * daemon ptrace-like capabilities over the current process. This
1065 * means, that the filesystem daemon is able to record the exact
1066 * filesystem operations performed, and can also control the behavior
1067 * of the requester process in otherwise impossible ways. For example
1068 * it can delay the operation for arbitrary length of time allowing
1069 * DoS against the requester.
1071 * For this reason only those processes can call into the filesystem,
1072 * for which the owner of the mount has ptrace privilege. This
1073 * excludes processes started by other users, suid or sgid processes.
1075 int fuse_allow_current_process(struct fuse_conn
*fc
)
1077 const struct cred
*cred
;
1079 if (fc
->allow_other
)
1082 cred
= current_cred();
1083 if (uid_eq(cred
->euid
, fc
->user_id
) &&
1084 uid_eq(cred
->suid
, fc
->user_id
) &&
1085 uid_eq(cred
->uid
, fc
->user_id
) &&
1086 gid_eq(cred
->egid
, fc
->group_id
) &&
1087 gid_eq(cred
->sgid
, fc
->group_id
) &&
1088 gid_eq(cred
->gid
, fc
->group_id
))
1094 static int fuse_access(struct inode
*inode
, int mask
)
1096 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1098 struct fuse_access_in inarg
;
1101 BUG_ON(mask
& MAY_NOT_BLOCK
);
1106 memset(&inarg
, 0, sizeof(inarg
));
1107 inarg
.mask
= mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
);
1108 args
.in
.h
.opcode
= FUSE_ACCESS
;
1109 args
.in
.h
.nodeid
= get_node_id(inode
);
1110 args
.in
.numargs
= 1;
1111 args
.in
.args
[0].size
= sizeof(inarg
);
1112 args
.in
.args
[0].value
= &inarg
;
1113 err
= fuse_simple_request(fc
, &args
);
1114 if (err
== -ENOSYS
) {
1121 static int fuse_perm_getattr(struct inode
*inode
, int mask
)
1123 if (mask
& MAY_NOT_BLOCK
)
1126 forget_all_cached_acls(inode
);
1127 return fuse_do_getattr(inode
, NULL
, NULL
);
1131 * Check permission. The two basic access models of FUSE are:
1133 * 1) Local access checking ('default_permissions' mount option) based
1134 * on file mode. This is the plain old disk filesystem permission
1137 * 2) "Remote" access checking, where server is responsible for
1138 * checking permission in each inode operation. An exception to this
1139 * is if ->permission() was invoked from sys_access() in which case an
1140 * access request is sent. Execute permission is still checked
1141 * locally based on file mode.
1143 static int fuse_permission(struct inode
*inode
, int mask
)
1145 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1146 bool refreshed
= false;
1149 if (!fuse_allow_current_process(fc
))
1153 * If attributes are needed, refresh them before proceeding
1155 if (fc
->default_permissions
||
1156 ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))) {
1157 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1159 if (time_before64(fi
->i_time
, get_jiffies_64())) {
1162 err
= fuse_perm_getattr(inode
, mask
);
1168 if (fc
->default_permissions
) {
1169 err
= generic_permission(inode
, mask
);
1171 /* If permission is denied, try to refresh file
1172 attributes. This is also needed, because the root
1173 node will at first have no permissions */
1174 if (err
== -EACCES
&& !refreshed
) {
1175 err
= fuse_perm_getattr(inode
, mask
);
1177 err
= generic_permission(inode
, mask
);
1180 /* Note: the opposite of the above test does not
1181 exist. So if permissions are revoked this won't be
1182 noticed immediately, only after the attribute
1183 timeout has expired */
1184 } else if (mask
& (MAY_ACCESS
| MAY_CHDIR
)) {
1185 err
= fuse_access(inode
, mask
);
1186 } else if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
)) {
1187 if (!(inode
->i_mode
& S_IXUGO
)) {
1191 err
= fuse_perm_getattr(inode
, mask
);
1192 if (!err
&& !(inode
->i_mode
& S_IXUGO
))
1199 static int parse_dirfile(char *buf
, size_t nbytes
, struct file
*file
,
1200 struct dir_context
*ctx
)
1202 while (nbytes
>= FUSE_NAME_OFFSET
) {
1203 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
1204 size_t reclen
= FUSE_DIRENT_SIZE(dirent
);
1205 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
1207 if (reclen
> nbytes
)
1209 if (memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
)
1212 if (!dir_emit(ctx
, dirent
->name
, dirent
->namelen
,
1213 dirent
->ino
, dirent
->type
))
1218 ctx
->pos
= dirent
->off
;
1224 static int fuse_direntplus_link(struct file
*file
,
1225 struct fuse_direntplus
*direntplus
,
1228 struct fuse_entry_out
*o
= &direntplus
->entry_out
;
1229 struct fuse_dirent
*dirent
= &direntplus
->dirent
;
1230 struct dentry
*parent
= file
->f_path
.dentry
;
1231 struct qstr name
= QSTR_INIT(dirent
->name
, dirent
->namelen
);
1232 struct dentry
*dentry
;
1233 struct dentry
*alias
;
1234 struct inode
*dir
= d_inode(parent
);
1235 struct fuse_conn
*fc
;
1236 struct inode
*inode
;
1237 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1241 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1242 * ENOENT. Instead, it only means the userspace filesystem did
1243 * not want to return attributes/handle for this entry.
1250 if (name
.name
[0] == '.') {
1252 * We could potentially refresh the attributes of the directory
1257 if (name
.name
[1] == '.' && name
.len
== 2)
1261 if (invalid_nodeid(o
->nodeid
))
1263 if (!fuse_valid_type(o
->attr
.mode
))
1266 fc
= get_fuse_conn(dir
);
1268 name
.hash
= full_name_hash(parent
, name
.name
, name
.len
);
1269 dentry
= d_lookup(parent
, &name
);
1272 dentry
= d_alloc_parallel(parent
, &name
, &wq
);
1274 return PTR_ERR(dentry
);
1276 if (!d_in_lookup(dentry
)) {
1277 struct fuse_inode
*fi
;
1278 inode
= d_inode(dentry
);
1280 get_node_id(inode
) != o
->nodeid
||
1281 ((o
->attr
.mode
^ inode
->i_mode
) & S_IFMT
)) {
1282 d_invalidate(dentry
);
1286 if (is_bad_inode(inode
)) {
1291 fi
= get_fuse_inode(inode
);
1292 spin_lock(&fc
->lock
);
1294 spin_unlock(&fc
->lock
);
1296 forget_all_cached_acls(inode
);
1297 fuse_change_attributes(inode
, &o
->attr
,
1298 entry_attr_timeout(o
),
1301 * The other branch comes via fuse_iget()
1302 * which bumps nlookup inside
1305 inode
= fuse_iget(dir
->i_sb
, o
->nodeid
, o
->generation
,
1306 &o
->attr
, entry_attr_timeout(o
),
1309 inode
= ERR_PTR(-ENOMEM
);
1311 alias
= d_splice_alias(inode
, dentry
);
1312 d_lookup_done(dentry
);
1318 return PTR_ERR(dentry
);
1320 if (fc
->readdirplus_auto
)
1321 set_bit(FUSE_I_INIT_RDPLUS
, &get_fuse_inode(inode
)->state
);
1322 fuse_change_entry_timeout(dentry
, o
);
1328 static int parse_dirplusfile(char *buf
, size_t nbytes
, struct file
*file
,
1329 struct dir_context
*ctx
, u64 attr_version
)
1331 struct fuse_direntplus
*direntplus
;
1332 struct fuse_dirent
*dirent
;
1337 while (nbytes
>= FUSE_NAME_OFFSET_DIRENTPLUS
) {
1338 direntplus
= (struct fuse_direntplus
*) buf
;
1339 dirent
= &direntplus
->dirent
;
1340 reclen
= FUSE_DIRENTPLUS_SIZE(direntplus
);
1342 if (!dirent
->namelen
|| dirent
->namelen
> FUSE_NAME_MAX
)
1344 if (reclen
> nbytes
)
1346 if (memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
)
1350 /* We fill entries into dstbuf only as much as
1351 it can hold. But we still continue iterating
1352 over remaining entries to link them. If not,
1353 we need to send a FORGET for each of those
1354 which we did not link.
1356 over
= !dir_emit(ctx
, dirent
->name
, dirent
->namelen
,
1357 dirent
->ino
, dirent
->type
);
1359 ctx
->pos
= dirent
->off
;
1365 ret
= fuse_direntplus_link(file
, direntplus
, attr_version
);
1367 fuse_force_forget(file
, direntplus
->entry_out
.nodeid
);
1373 static int fuse_readdir(struct file
*file
, struct dir_context
*ctx
)
1378 struct inode
*inode
= file_inode(file
);
1379 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1380 struct fuse_req
*req
;
1381 u64 attr_version
= 0;
1384 if (is_bad_inode(inode
))
1387 req
= fuse_get_req(fc
, 1);
1389 return PTR_ERR(req
);
1391 page
= alloc_page(GFP_KERNEL
);
1393 fuse_put_request(fc
, req
);
1397 plus
= fuse_use_readdirplus(inode
, ctx
);
1398 req
->out
.argpages
= 1;
1400 req
->pages
[0] = page
;
1401 req
->page_descs
[0].length
= PAGE_SIZE
;
1403 attr_version
= fuse_get_attr_version(fc
);
1404 fuse_read_fill(req
, file
, ctx
->pos
, PAGE_SIZE
,
1407 fuse_read_fill(req
, file
, ctx
->pos
, PAGE_SIZE
,
1410 locked
= fuse_lock_inode(inode
);
1411 fuse_request_send(fc
, req
);
1412 fuse_unlock_inode(inode
, locked
);
1413 nbytes
= req
->out
.args
[0].size
;
1414 err
= req
->out
.h
.error
;
1415 fuse_put_request(fc
, req
);
1418 err
= parse_dirplusfile(page_address(page
), nbytes
,
1422 err
= parse_dirfile(page_address(page
), nbytes
, file
,
1428 fuse_invalidate_atime(inode
);
1432 static const char *fuse_get_link(struct dentry
*dentry
,
1433 struct inode
*inode
,
1434 struct delayed_call
*done
)
1436 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1442 return ERR_PTR(-ECHILD
);
1444 link
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1446 return ERR_PTR(-ENOMEM
);
1448 args
.in
.h
.opcode
= FUSE_READLINK
;
1449 args
.in
.h
.nodeid
= get_node_id(inode
);
1450 args
.out
.argvar
= 1;
1451 args
.out
.numargs
= 1;
1452 args
.out
.args
[0].size
= PAGE_SIZE
- 1;
1453 args
.out
.args
[0].value
= link
;
1454 ret
= fuse_simple_request(fc
, &args
);
1457 link
= ERR_PTR(ret
);
1460 set_delayed_call(done
, kfree_link
, link
);
1462 fuse_invalidate_atime(inode
);
1466 static int fuse_dir_open(struct inode
*inode
, struct file
*file
)
1468 return fuse_open_common(inode
, file
, true);
1471 static int fuse_dir_release(struct inode
*inode
, struct file
*file
)
1473 fuse_release_common(file
, FUSE_RELEASEDIR
);
1478 static int fuse_dir_fsync(struct file
*file
, loff_t start
, loff_t end
,
1481 return fuse_fsync_common(file
, start
, end
, datasync
, 1);
1484 static long fuse_dir_ioctl(struct file
*file
, unsigned int cmd
,
1487 struct fuse_conn
*fc
= get_fuse_conn(file
->f_mapping
->host
);
1489 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1493 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_DIR
);
1496 static long fuse_dir_compat_ioctl(struct file
*file
, unsigned int cmd
,
1499 struct fuse_conn
*fc
= get_fuse_conn(file
->f_mapping
->host
);
1504 return fuse_ioctl_common(file
, cmd
, arg
,
1505 FUSE_IOCTL_COMPAT
| FUSE_IOCTL_DIR
);
1508 static bool update_mtime(unsigned ivalid
, bool trust_local_mtime
)
1510 /* Always update if mtime is explicitly set */
1511 if (ivalid
& ATTR_MTIME_SET
)
1514 /* Or if kernel i_mtime is the official one */
1515 if (trust_local_mtime
)
1518 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1519 if ((ivalid
& ATTR_SIZE
) && (ivalid
& (ATTR_OPEN
| ATTR_FILE
)))
1522 /* In all other cases update */
1526 static void iattr_to_fattr(struct iattr
*iattr
, struct fuse_setattr_in
*arg
,
1527 bool trust_local_cmtime
)
1529 unsigned ivalid
= iattr
->ia_valid
;
1531 if (ivalid
& ATTR_MODE
)
1532 arg
->valid
|= FATTR_MODE
, arg
->mode
= iattr
->ia_mode
;
1533 if (ivalid
& ATTR_UID
)
1534 arg
->valid
|= FATTR_UID
, arg
->uid
= from_kuid(&init_user_ns
, iattr
->ia_uid
);
1535 if (ivalid
& ATTR_GID
)
1536 arg
->valid
|= FATTR_GID
, arg
->gid
= from_kgid(&init_user_ns
, iattr
->ia_gid
);
1537 if (ivalid
& ATTR_SIZE
)
1538 arg
->valid
|= FATTR_SIZE
, arg
->size
= iattr
->ia_size
;
1539 if (ivalid
& ATTR_ATIME
) {
1540 arg
->valid
|= FATTR_ATIME
;
1541 arg
->atime
= iattr
->ia_atime
.tv_sec
;
1542 arg
->atimensec
= iattr
->ia_atime
.tv_nsec
;
1543 if (!(ivalid
& ATTR_ATIME_SET
))
1544 arg
->valid
|= FATTR_ATIME_NOW
;
1546 if ((ivalid
& ATTR_MTIME
) && update_mtime(ivalid
, trust_local_cmtime
)) {
1547 arg
->valid
|= FATTR_MTIME
;
1548 arg
->mtime
= iattr
->ia_mtime
.tv_sec
;
1549 arg
->mtimensec
= iattr
->ia_mtime
.tv_nsec
;
1550 if (!(ivalid
& ATTR_MTIME_SET
) && !trust_local_cmtime
)
1551 arg
->valid
|= FATTR_MTIME_NOW
;
1553 if ((ivalid
& ATTR_CTIME
) && trust_local_cmtime
) {
1554 arg
->valid
|= FATTR_CTIME
;
1555 arg
->ctime
= iattr
->ia_ctime
.tv_sec
;
1556 arg
->ctimensec
= iattr
->ia_ctime
.tv_nsec
;
1561 * Prevent concurrent writepages on inode
1563 * This is done by adding a negative bias to the inode write counter
1564 * and waiting for all pending writes to finish.
1566 void fuse_set_nowrite(struct inode
*inode
)
1568 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1569 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1571 BUG_ON(!inode_is_locked(inode
));
1573 spin_lock(&fc
->lock
);
1574 BUG_ON(fi
->writectr
< 0);
1575 fi
->writectr
+= FUSE_NOWRITE
;
1576 spin_unlock(&fc
->lock
);
1577 wait_event(fi
->page_waitq
, fi
->writectr
== FUSE_NOWRITE
);
1581 * Allow writepages on inode
1583 * Remove the bias from the writecounter and send any queued
1586 static void __fuse_release_nowrite(struct inode
*inode
)
1588 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1590 BUG_ON(fi
->writectr
!= FUSE_NOWRITE
);
1592 fuse_flush_writepages(inode
);
1595 void fuse_release_nowrite(struct inode
*inode
)
1597 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1599 spin_lock(&fc
->lock
);
1600 __fuse_release_nowrite(inode
);
1601 spin_unlock(&fc
->lock
);
1604 static void fuse_setattr_fill(struct fuse_conn
*fc
, struct fuse_args
*args
,
1605 struct inode
*inode
,
1606 struct fuse_setattr_in
*inarg_p
,
1607 struct fuse_attr_out
*outarg_p
)
1609 args
->in
.h
.opcode
= FUSE_SETATTR
;
1610 args
->in
.h
.nodeid
= get_node_id(inode
);
1611 args
->in
.numargs
= 1;
1612 args
->in
.args
[0].size
= sizeof(*inarg_p
);
1613 args
->in
.args
[0].value
= inarg_p
;
1614 args
->out
.numargs
= 1;
1615 args
->out
.args
[0].size
= sizeof(*outarg_p
);
1616 args
->out
.args
[0].value
= outarg_p
;
1620 * Flush inode->i_mtime to the server
1622 int fuse_flush_times(struct inode
*inode
, struct fuse_file
*ff
)
1624 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1626 struct fuse_setattr_in inarg
;
1627 struct fuse_attr_out outarg
;
1629 memset(&inarg
, 0, sizeof(inarg
));
1630 memset(&outarg
, 0, sizeof(outarg
));
1632 inarg
.valid
= FATTR_MTIME
;
1633 inarg
.mtime
= inode
->i_mtime
.tv_sec
;
1634 inarg
.mtimensec
= inode
->i_mtime
.tv_nsec
;
1635 if (fc
->minor
>= 23) {
1636 inarg
.valid
|= FATTR_CTIME
;
1637 inarg
.ctime
= inode
->i_ctime
.tv_sec
;
1638 inarg
.ctimensec
= inode
->i_ctime
.tv_nsec
;
1641 inarg
.valid
|= FATTR_FH
;
1644 fuse_setattr_fill(fc
, &args
, inode
, &inarg
, &outarg
);
1646 return fuse_simple_request(fc
, &args
);
1650 * Set attributes, and at the same time refresh them.
1652 * Truncation is slightly complicated, because the 'truncate' request
1653 * may fail, in which case we don't want to touch the mapping.
1654 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1655 * and the actual truncation by hand.
1657 int fuse_do_setattr(struct dentry
*dentry
, struct iattr
*attr
,
1660 struct inode
*inode
= d_inode(dentry
);
1661 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1662 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1664 struct fuse_setattr_in inarg
;
1665 struct fuse_attr_out outarg
;
1666 bool is_truncate
= false;
1667 bool is_wb
= fc
->writeback_cache
;
1670 bool trust_local_cmtime
= is_wb
&& S_ISREG(inode
->i_mode
);
1672 if (!fc
->default_permissions
)
1673 attr
->ia_valid
|= ATTR_FORCE
;
1675 err
= setattr_prepare(dentry
, attr
);
1679 if (attr
->ia_valid
& ATTR_OPEN
) {
1680 /* This is coming from open(..., ... | O_TRUNC); */
1681 WARN_ON(!(attr
->ia_valid
& ATTR_SIZE
));
1682 WARN_ON(attr
->ia_size
!= 0);
1683 if (fc
->atomic_o_trunc
) {
1685 * No need to send request to userspace, since actual
1686 * truncation has already been done by OPEN. But still
1687 * need to truncate page cache.
1689 i_size_write(inode
, 0);
1690 truncate_pagecache(inode
, 0);
1696 if (attr
->ia_valid
& ATTR_SIZE
)
1700 fuse_set_nowrite(inode
);
1701 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1702 if (trust_local_cmtime
&& attr
->ia_size
!= inode
->i_size
)
1703 attr
->ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
1706 memset(&inarg
, 0, sizeof(inarg
));
1707 memset(&outarg
, 0, sizeof(outarg
));
1708 iattr_to_fattr(attr
, &inarg
, trust_local_cmtime
);
1710 struct fuse_file
*ff
= file
->private_data
;
1711 inarg
.valid
|= FATTR_FH
;
1714 if (attr
->ia_valid
& ATTR_SIZE
) {
1715 /* For mandatory locking in truncate */
1716 inarg
.valid
|= FATTR_LOCKOWNER
;
1717 inarg
.lock_owner
= fuse_lock_owner_id(fc
, current
->files
);
1719 fuse_setattr_fill(fc
, &args
, inode
, &inarg
, &outarg
);
1720 err
= fuse_simple_request(fc
, &args
);
1723 fuse_invalidate_attr(inode
);
1727 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
1728 make_bad_inode(inode
);
1733 spin_lock(&fc
->lock
);
1734 /* the kernel maintains i_mtime locally */
1735 if (trust_local_cmtime
) {
1736 if (attr
->ia_valid
& ATTR_MTIME
)
1737 inode
->i_mtime
= attr
->ia_mtime
;
1738 if (attr
->ia_valid
& ATTR_CTIME
)
1739 inode
->i_ctime
= attr
->ia_ctime
;
1740 /* FIXME: clear I_DIRTY_SYNC? */
1743 fuse_change_attributes_common(inode
, &outarg
.attr
,
1744 attr_timeout(&outarg
));
1745 oldsize
= inode
->i_size
;
1746 /* see the comment in fuse_change_attributes() */
1747 if (!is_wb
|| is_truncate
|| !S_ISREG(inode
->i_mode
))
1748 i_size_write(inode
, outarg
.attr
.size
);
1751 /* NOTE: this may release/reacquire fc->lock */
1752 __fuse_release_nowrite(inode
);
1754 spin_unlock(&fc
->lock
);
1757 * Only call invalidate_inode_pages2() after removing
1758 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1760 if ((is_truncate
|| !is_wb
) &&
1761 S_ISREG(inode
->i_mode
) && oldsize
!= outarg
.attr
.size
) {
1762 truncate_pagecache(inode
, outarg
.attr
.size
);
1763 invalidate_inode_pages2(inode
->i_mapping
);
1766 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1771 fuse_release_nowrite(inode
);
1773 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1777 static int fuse_setattr(struct dentry
*entry
, struct iattr
*attr
)
1779 struct inode
*inode
= d_inode(entry
);
1780 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1781 struct file
*file
= (attr
->ia_valid
& ATTR_FILE
) ? attr
->ia_file
: NULL
;
1784 if (!fuse_allow_current_process(get_fuse_conn(inode
)))
1787 if (attr
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
)) {
1788 attr
->ia_valid
&= ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
|
1792 * The only sane way to reliably kill suid/sgid is to do it in
1793 * the userspace filesystem
1795 * This should be done on write(), truncate() and chown().
1797 if (!fc
->handle_killpriv
) {
1799 * ia_mode calculation may have used stale i_mode.
1800 * Refresh and recalculate.
1802 ret
= fuse_do_getattr(inode
, NULL
, file
);
1806 attr
->ia_mode
= inode
->i_mode
;
1807 if (inode
->i_mode
& S_ISUID
) {
1808 attr
->ia_valid
|= ATTR_MODE
;
1809 attr
->ia_mode
&= ~S_ISUID
;
1811 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
1812 attr
->ia_valid
|= ATTR_MODE
;
1813 attr
->ia_mode
&= ~S_ISGID
;
1817 if (!attr
->ia_valid
)
1820 ret
= fuse_do_setattr(entry
, attr
, file
);
1823 * If filesystem supports acls it may have updated acl xattrs in
1824 * the filesystem, so forget cached acls for the inode.
1827 forget_all_cached_acls(inode
);
1829 /* Directory mode changed, may need to revalidate access */
1830 if (d_is_dir(entry
) && (attr
->ia_valid
& ATTR_MODE
))
1831 fuse_invalidate_entry_cache(entry
);
1836 static int fuse_getattr(const struct path
*path
, struct kstat
*stat
,
1837 u32 request_mask
, unsigned int flags
)
1839 struct inode
*inode
= d_inode(path
->dentry
);
1840 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1842 if (!fuse_allow_current_process(fc
))
1845 return fuse_update_get_attr(inode
, NULL
, stat
);
1848 static const struct inode_operations fuse_dir_inode_operations
= {
1849 .lookup
= fuse_lookup
,
1850 .mkdir
= fuse_mkdir
,
1851 .symlink
= fuse_symlink
,
1852 .unlink
= fuse_unlink
,
1853 .rmdir
= fuse_rmdir
,
1854 .rename
= fuse_rename2
,
1856 .setattr
= fuse_setattr
,
1857 .create
= fuse_create
,
1858 .atomic_open
= fuse_atomic_open
,
1859 .mknod
= fuse_mknod
,
1860 .permission
= fuse_permission
,
1861 .getattr
= fuse_getattr
,
1862 .listxattr
= fuse_listxattr
,
1863 .get_acl
= fuse_get_acl
,
1864 .set_acl
= fuse_set_acl
,
1867 static const struct file_operations fuse_dir_operations
= {
1868 .llseek
= generic_file_llseek
,
1869 .read
= generic_read_dir
,
1870 .iterate_shared
= fuse_readdir
,
1871 .open
= fuse_dir_open
,
1872 .release
= fuse_dir_release
,
1873 .fsync
= fuse_dir_fsync
,
1874 .unlocked_ioctl
= fuse_dir_ioctl
,
1875 .compat_ioctl
= fuse_dir_compat_ioctl
,
1878 static const struct inode_operations fuse_common_inode_operations
= {
1879 .setattr
= fuse_setattr
,
1880 .permission
= fuse_permission
,
1881 .getattr
= fuse_getattr
,
1882 .listxattr
= fuse_listxattr
,
1883 .get_acl
= fuse_get_acl
,
1884 .set_acl
= fuse_set_acl
,
1887 static const struct inode_operations fuse_symlink_inode_operations
= {
1888 .setattr
= fuse_setattr
,
1889 .get_link
= fuse_get_link
,
1890 .getattr
= fuse_getattr
,
1891 .listxattr
= fuse_listxattr
,
1894 void fuse_init_common(struct inode
*inode
)
1896 inode
->i_op
= &fuse_common_inode_operations
;
1899 void fuse_init_dir(struct inode
*inode
)
1901 inode
->i_op
= &fuse_dir_inode_operations
;
1902 inode
->i_fop
= &fuse_dir_operations
;
1905 void fuse_init_symlink(struct inode
*inode
)
1907 inode
->i_op
= &fuse_symlink_inode_operations
;