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/iversion.h>
18 #include <linux/posix_acl.h>
20 static void fuse_advise_use_readdirplus(struct inode
*dir
)
22 struct fuse_inode
*fi
= get_fuse_inode(dir
);
24 set_bit(FUSE_I_ADVISE_RDPLUS
, &fi
->state
);
27 #if BITS_PER_LONG >= 64
28 static inline void __fuse_dentry_settime(struct dentry
*entry
, u64 time
)
30 entry
->d_fsdata
= (void *) time
;
33 static inline u64
fuse_dentry_time(const struct dentry
*entry
)
35 return (u64
)entry
->d_fsdata
;
44 static inline void __fuse_dentry_settime(struct dentry
*dentry
, u64 time
)
46 ((union fuse_dentry
*) dentry
->d_fsdata
)->time
= time
;
49 static inline u64
fuse_dentry_time(const struct dentry
*entry
)
51 return ((union fuse_dentry
*) entry
->d_fsdata
)->time
;
55 static void fuse_dentry_settime(struct dentry
*dentry
, u64 time
)
57 struct fuse_conn
*fc
= get_fuse_conn_super(dentry
->d_sb
);
58 bool delete = !time
&& fc
->delete_stale
;
60 * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
61 * Don't care about races, either way it's just an optimization
63 if ((!delete && (dentry
->d_flags
& DCACHE_OP_DELETE
)) ||
64 (delete && !(dentry
->d_flags
& DCACHE_OP_DELETE
))) {
65 spin_lock(&dentry
->d_lock
);
67 dentry
->d_flags
&= ~DCACHE_OP_DELETE
;
69 dentry
->d_flags
|= DCACHE_OP_DELETE
;
70 spin_unlock(&dentry
->d_lock
);
73 __fuse_dentry_settime(dentry
, time
);
77 * FUSE caches dentries and attributes with separate timeout. The
78 * time in jiffies until the dentry/attributes are valid is stored in
79 * dentry->d_fsdata and fuse_inode->i_time respectively.
83 * Calculate the time in jiffies until a dentry/attributes are valid
85 static u64
time_to_jiffies(u64 sec
, u32 nsec
)
88 struct timespec64 ts
= {
90 min_t(u32
, nsec
, NSEC_PER_SEC
- 1)
93 return get_jiffies_64() + timespec64_to_jiffies(&ts
);
99 * Set dentry and possibly attribute timeouts from the lookup/mk*
102 void fuse_change_entry_timeout(struct dentry
*entry
, struct fuse_entry_out
*o
)
104 fuse_dentry_settime(entry
,
105 time_to_jiffies(o
->entry_valid
, o
->entry_valid_nsec
));
108 static u64
attr_timeout(struct fuse_attr_out
*o
)
110 return time_to_jiffies(o
->attr_valid
, o
->attr_valid_nsec
);
113 u64
entry_attr_timeout(struct fuse_entry_out
*o
)
115 return time_to_jiffies(o
->attr_valid
, o
->attr_valid_nsec
);
118 static void fuse_invalidate_attr_mask(struct inode
*inode
, u32 mask
)
120 set_mask_bits(&get_fuse_inode(inode
)->inval_mask
, 0, mask
);
124 * Mark the attributes as stale, so that at the next call to
125 * ->getattr() they will be fetched from userspace
127 void fuse_invalidate_attr(struct inode
*inode
)
129 fuse_invalidate_attr_mask(inode
, STATX_BASIC_STATS
);
132 static void fuse_dir_changed(struct inode
*dir
)
134 fuse_invalidate_attr(dir
);
135 inode_maybe_inc_iversion(dir
, false);
139 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
142 void fuse_invalidate_atime(struct inode
*inode
)
144 if (!IS_RDONLY(inode
))
145 fuse_invalidate_attr_mask(inode
, STATX_ATIME
);
149 * Just mark the entry as stale, so that a next attempt to look it up
150 * will result in a new lookup call to userspace
152 * This is called when a dentry is about to become negative and the
153 * timeout is unknown (unlink, rmdir, rename and in some cases
156 void fuse_invalidate_entry_cache(struct dentry
*entry
)
158 fuse_dentry_settime(entry
, 0);
162 * Same as fuse_invalidate_entry_cache(), but also try to remove the
163 * dentry from the hash
165 static void fuse_invalidate_entry(struct dentry
*entry
)
168 fuse_invalidate_entry_cache(entry
);
171 static void fuse_lookup_init(struct fuse_conn
*fc
, struct fuse_args
*args
,
172 u64 nodeid
, const struct qstr
*name
,
173 struct fuse_entry_out
*outarg
)
175 memset(outarg
, 0, sizeof(struct fuse_entry_out
));
176 args
->opcode
= FUSE_LOOKUP
;
177 args
->nodeid
= nodeid
;
178 args
->in_numargs
= 1;
179 args
->in_args
[0].size
= name
->len
+ 1;
180 args
->in_args
[0].value
= name
->name
;
181 args
->out_numargs
= 1;
182 args
->out_args
[0].size
= sizeof(struct fuse_entry_out
);
183 args
->out_args
[0].value
= outarg
;
187 * Check whether the dentry is still valid
189 * If the entry validity timeout has expired and the dentry is
190 * positive, try to redo the lookup. If the lookup results in a
191 * different inode, then let the VFS invalidate the dentry and redo
192 * the lookup once more. If the lookup results in the same inode,
193 * then refresh the attributes, timeouts and mark the dentry valid.
195 static int fuse_dentry_revalidate(struct dentry
*entry
, unsigned int flags
)
198 struct dentry
*parent
;
199 struct fuse_conn
*fc
;
200 struct fuse_inode
*fi
;
203 inode
= d_inode_rcu(entry
);
204 if (inode
&& fuse_is_bad(inode
))
206 else if (time_before64(fuse_dentry_time(entry
), get_jiffies_64()) ||
207 (flags
& LOOKUP_REVAL
)) {
208 struct fuse_entry_out outarg
;
210 struct fuse_forget_link
*forget
;
213 /* For negative dentries, always do a fresh lookup */
218 if (flags
& LOOKUP_RCU
)
221 fc
= get_fuse_conn(inode
);
223 forget
= fuse_alloc_forget();
228 attr_version
= fuse_get_attr_version(fc
);
230 parent
= dget_parent(entry
);
231 fuse_lookup_init(fc
, &args
, get_node_id(d_inode(parent
)),
232 &entry
->d_name
, &outarg
);
233 ret
= fuse_simple_request(fc
, &args
);
235 /* Zero nodeid is same as -ENOENT */
236 if (!ret
&& !outarg
.nodeid
)
239 fi
= get_fuse_inode(inode
);
240 if (outarg
.nodeid
!= get_node_id(inode
)) {
241 fuse_queue_forget(fc
, forget
, outarg
.nodeid
, 1);
244 spin_lock(&fi
->lock
);
246 spin_unlock(&fi
->lock
);
251 if (ret
|| fuse_invalid_attr(&outarg
.attr
) ||
252 (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
255 forget_all_cached_acls(inode
);
256 fuse_change_attributes(inode
, &outarg
.attr
,
257 entry_attr_timeout(&outarg
),
259 fuse_change_entry_timeout(entry
, &outarg
);
261 fi
= get_fuse_inode(inode
);
262 if (flags
& LOOKUP_RCU
) {
263 if (test_bit(FUSE_I_INIT_RDPLUS
, &fi
->state
))
265 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS
, &fi
->state
)) {
266 parent
= dget_parent(entry
);
267 fuse_advise_use_readdirplus(d_inode(parent
));
280 #if BITS_PER_LONG < 64
281 static int fuse_dentry_init(struct dentry
*dentry
)
283 dentry
->d_fsdata
= kzalloc(sizeof(union fuse_dentry
),
284 GFP_KERNEL_ACCOUNT
| __GFP_RECLAIMABLE
);
286 return dentry
->d_fsdata
? 0 : -ENOMEM
;
288 static void fuse_dentry_release(struct dentry
*dentry
)
290 union fuse_dentry
*fd
= dentry
->d_fsdata
;
296 static int fuse_dentry_delete(const struct dentry
*dentry
)
298 return time_before64(fuse_dentry_time(dentry
), get_jiffies_64());
301 const struct dentry_operations fuse_dentry_operations
= {
302 .d_revalidate
= fuse_dentry_revalidate
,
303 .d_delete
= fuse_dentry_delete
,
304 #if BITS_PER_LONG < 64
305 .d_init
= fuse_dentry_init
,
306 .d_release
= fuse_dentry_release
,
310 const struct dentry_operations fuse_root_dentry_operations
= {
311 #if BITS_PER_LONG < 64
312 .d_init
= fuse_dentry_init
,
313 .d_release
= fuse_dentry_release
,
317 int fuse_valid_type(int m
)
319 return S_ISREG(m
) || S_ISDIR(m
) || S_ISLNK(m
) || S_ISCHR(m
) ||
320 S_ISBLK(m
) || S_ISFIFO(m
) || S_ISSOCK(m
);
323 bool fuse_invalid_attr(struct fuse_attr
*attr
)
325 return !fuse_valid_type(attr
->mode
) ||
326 attr
->size
> LLONG_MAX
;
329 int fuse_lookup_name(struct super_block
*sb
, u64 nodeid
, const struct qstr
*name
,
330 struct fuse_entry_out
*outarg
, struct inode
**inode
)
332 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
334 struct fuse_forget_link
*forget
;
340 if (name
->len
> FUSE_NAME_MAX
)
344 forget
= fuse_alloc_forget();
349 attr_version
= fuse_get_attr_version(fc
);
351 fuse_lookup_init(fc
, &args
, nodeid
, name
, outarg
);
352 err
= fuse_simple_request(fc
, &args
);
353 /* Zero nodeid is same as -ENOENT, but with valid timeout */
354 if (err
|| !outarg
->nodeid
)
360 if (fuse_invalid_attr(&outarg
->attr
))
363 *inode
= fuse_iget(sb
, outarg
->nodeid
, outarg
->generation
,
364 &outarg
->attr
, entry_attr_timeout(outarg
),
368 fuse_queue_forget(fc
, forget
, outarg
->nodeid
, 1);
379 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
383 struct fuse_entry_out outarg
;
385 struct dentry
*newent
;
386 bool outarg_valid
= true;
389 if (fuse_is_bad(dir
))
390 return ERR_PTR(-EIO
);
392 locked
= fuse_lock_inode(dir
);
393 err
= fuse_lookup_name(dir
->i_sb
, get_node_id(dir
), &entry
->d_name
,
395 fuse_unlock_inode(dir
, locked
);
396 if (err
== -ENOENT
) {
397 outarg_valid
= false;
404 if (inode
&& get_node_id(inode
) == FUSE_ROOT_ID
)
407 newent
= d_splice_alias(inode
, entry
);
408 err
= PTR_ERR(newent
);
412 entry
= newent
? newent
: entry
;
414 fuse_change_entry_timeout(entry
, &outarg
);
416 fuse_invalidate_entry_cache(entry
);
419 fuse_advise_use_readdirplus(dir
);
429 * Atomic create+open operation
431 * If the filesystem doesn't support this, then fall back to separate
432 * 'mknod' + 'open' requests.
434 static int fuse_create_open(struct inode
*dir
, struct dentry
*entry
,
435 struct file
*file
, unsigned flags
,
440 struct fuse_conn
*fc
= get_fuse_conn(dir
);
442 struct fuse_forget_link
*forget
;
443 struct fuse_create_in inarg
;
444 struct fuse_open_out outopen
;
445 struct fuse_entry_out outentry
;
446 struct fuse_inode
*fi
;
447 struct fuse_file
*ff
;
449 /* Userspace expects S_IFREG in create mode */
450 BUG_ON((mode
& S_IFMT
) != S_IFREG
);
452 forget
= fuse_alloc_forget();
458 ff
= fuse_file_alloc(fc
);
460 goto out_put_forget_req
;
463 mode
&= ~current_umask();
466 memset(&inarg
, 0, sizeof(inarg
));
467 memset(&outentry
, 0, sizeof(outentry
));
470 inarg
.umask
= current_umask();
471 args
.opcode
= FUSE_CREATE
;
472 args
.nodeid
= get_node_id(dir
);
474 args
.in_args
[0].size
= sizeof(inarg
);
475 args
.in_args
[0].value
= &inarg
;
476 args
.in_args
[1].size
= entry
->d_name
.len
+ 1;
477 args
.in_args
[1].value
= entry
->d_name
.name
;
478 args
.out_numargs
= 2;
479 args
.out_args
[0].size
= sizeof(outentry
);
480 args
.out_args
[0].value
= &outentry
;
481 args
.out_args
[1].size
= sizeof(outopen
);
482 args
.out_args
[1].value
= &outopen
;
483 err
= fuse_simple_request(fc
, &args
);
488 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
) ||
489 fuse_invalid_attr(&outentry
.attr
))
493 ff
->nodeid
= outentry
.nodeid
;
494 ff
->open_flags
= outopen
.open_flags
;
495 inode
= fuse_iget(dir
->i_sb
, outentry
.nodeid
, outentry
.generation
,
496 &outentry
.attr
, entry_attr_timeout(&outentry
), 0);
498 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
499 fuse_sync_release(NULL
, ff
, flags
);
500 fuse_queue_forget(fc
, forget
, outentry
.nodeid
, 1);
505 d_instantiate(entry
, inode
);
506 fuse_change_entry_timeout(entry
, &outentry
);
507 fuse_dir_changed(dir
);
508 err
= finish_open(file
, entry
, generic_file_open
);
510 fi
= get_fuse_inode(inode
);
511 fuse_sync_release(fi
, ff
, flags
);
513 file
->private_data
= ff
;
514 fuse_finish_open(inode
, file
);
526 static int fuse_mknod(struct inode
*, struct dentry
*, umode_t
, dev_t
);
527 static int fuse_atomic_open(struct inode
*dir
, struct dentry
*entry
,
528 struct file
*file
, unsigned flags
,
532 struct fuse_conn
*fc
= get_fuse_conn(dir
);
533 struct dentry
*res
= NULL
;
535 if (fuse_is_bad(dir
))
538 if (d_in_lookup(entry
)) {
539 res
= fuse_lookup(dir
, entry
, 0);
547 if (!(flags
& O_CREAT
) || d_really_is_positive(entry
))
551 file
->f_mode
|= FMODE_CREATED
;
556 err
= fuse_create_open(dir
, entry
, file
, flags
, mode
);
557 if (err
== -ENOSYS
) {
566 err
= fuse_mknod(dir
, entry
, mode
, 0);
570 return finish_no_open(file
, res
);
574 * Code shared between mknod, mkdir, symlink and link
576 static int create_new_entry(struct fuse_conn
*fc
, struct fuse_args
*args
,
577 struct inode
*dir
, struct dentry
*entry
,
580 struct fuse_entry_out outarg
;
584 struct fuse_forget_link
*forget
;
586 if (fuse_is_bad(dir
))
589 forget
= fuse_alloc_forget();
593 memset(&outarg
, 0, sizeof(outarg
));
594 args
->nodeid
= get_node_id(dir
);
595 args
->out_numargs
= 1;
596 args
->out_args
[0].size
= sizeof(outarg
);
597 args
->out_args
[0].value
= &outarg
;
598 err
= fuse_simple_request(fc
, args
);
600 goto out_put_forget_req
;
603 if (invalid_nodeid(outarg
.nodeid
) || fuse_invalid_attr(&outarg
.attr
))
604 goto out_put_forget_req
;
606 if ((outarg
.attr
.mode
^ mode
) & S_IFMT
)
607 goto out_put_forget_req
;
609 inode
= fuse_iget(dir
->i_sb
, outarg
.nodeid
, outarg
.generation
,
610 &outarg
.attr
, entry_attr_timeout(&outarg
), 0);
612 fuse_queue_forget(fc
, forget
, outarg
.nodeid
, 1);
618 d
= d_splice_alias(inode
, entry
);
623 fuse_change_entry_timeout(d
, &outarg
);
626 fuse_change_entry_timeout(entry
, &outarg
);
628 fuse_dir_changed(dir
);
636 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, umode_t mode
,
639 struct fuse_mknod_in inarg
;
640 struct fuse_conn
*fc
= get_fuse_conn(dir
);
644 mode
&= ~current_umask();
646 memset(&inarg
, 0, sizeof(inarg
));
648 inarg
.rdev
= new_encode_dev(rdev
);
649 inarg
.umask
= current_umask();
650 args
.opcode
= FUSE_MKNOD
;
652 args
.in_args
[0].size
= sizeof(inarg
);
653 args
.in_args
[0].value
= &inarg
;
654 args
.in_args
[1].size
= entry
->d_name
.len
+ 1;
655 args
.in_args
[1].value
= entry
->d_name
.name
;
656 return create_new_entry(fc
, &args
, dir
, entry
, mode
);
659 static int fuse_create(struct inode
*dir
, struct dentry
*entry
, umode_t mode
,
662 return fuse_mknod(dir
, entry
, mode
, 0);
665 static int fuse_mkdir(struct inode
*dir
, struct dentry
*entry
, umode_t mode
)
667 struct fuse_mkdir_in inarg
;
668 struct fuse_conn
*fc
= get_fuse_conn(dir
);
672 mode
&= ~current_umask();
674 memset(&inarg
, 0, sizeof(inarg
));
676 inarg
.umask
= current_umask();
677 args
.opcode
= FUSE_MKDIR
;
679 args
.in_args
[0].size
= sizeof(inarg
);
680 args
.in_args
[0].value
= &inarg
;
681 args
.in_args
[1].size
= entry
->d_name
.len
+ 1;
682 args
.in_args
[1].value
= entry
->d_name
.name
;
683 return create_new_entry(fc
, &args
, dir
, entry
, S_IFDIR
);
686 static int fuse_symlink(struct inode
*dir
, struct dentry
*entry
,
689 struct fuse_conn
*fc
= get_fuse_conn(dir
);
690 unsigned len
= strlen(link
) + 1;
693 args
.opcode
= FUSE_SYMLINK
;
695 args
.in_args
[0].size
= entry
->d_name
.len
+ 1;
696 args
.in_args
[0].value
= entry
->d_name
.name
;
697 args
.in_args
[1].size
= len
;
698 args
.in_args
[1].value
= link
;
699 return create_new_entry(fc
, &args
, dir
, entry
, S_IFLNK
);
702 void fuse_update_ctime(struct inode
*inode
)
704 if (!IS_NOCMTIME(inode
)) {
705 inode
->i_ctime
= current_time(inode
);
706 mark_inode_dirty_sync(inode
);
710 static int fuse_unlink(struct inode
*dir
, struct dentry
*entry
)
713 struct fuse_conn
*fc
= get_fuse_conn(dir
);
716 if (fuse_is_bad(dir
))
719 args
.opcode
= FUSE_UNLINK
;
720 args
.nodeid
= get_node_id(dir
);
722 args
.in_args
[0].size
= entry
->d_name
.len
+ 1;
723 args
.in_args
[0].value
= entry
->d_name
.name
;
724 err
= fuse_simple_request(fc
, &args
);
726 struct inode
*inode
= d_inode(entry
);
727 struct fuse_inode
*fi
= get_fuse_inode(inode
);
729 spin_lock(&fi
->lock
);
730 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
732 * If i_nlink == 0 then unlink doesn't make sense, yet this can
733 * happen if userspace filesystem is careless. It would be
734 * difficult to enforce correct nlink usage so just ignore this
737 if (inode
->i_nlink
> 0)
739 spin_unlock(&fi
->lock
);
740 fuse_invalidate_attr(inode
);
741 fuse_dir_changed(dir
);
742 fuse_invalidate_entry_cache(entry
);
743 fuse_update_ctime(inode
);
744 } else if (err
== -EINTR
)
745 fuse_invalidate_entry(entry
);
749 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
752 struct fuse_conn
*fc
= get_fuse_conn(dir
);
755 if (fuse_is_bad(dir
))
758 args
.opcode
= FUSE_RMDIR
;
759 args
.nodeid
= get_node_id(dir
);
761 args
.in_args
[0].size
= entry
->d_name
.len
+ 1;
762 args
.in_args
[0].value
= entry
->d_name
.name
;
763 err
= fuse_simple_request(fc
, &args
);
765 clear_nlink(d_inode(entry
));
766 fuse_dir_changed(dir
);
767 fuse_invalidate_entry_cache(entry
);
768 } else if (err
== -EINTR
)
769 fuse_invalidate_entry(entry
);
773 static int fuse_rename_common(struct inode
*olddir
, struct dentry
*oldent
,
774 struct inode
*newdir
, struct dentry
*newent
,
775 unsigned int flags
, int opcode
, size_t argsize
)
778 struct fuse_rename2_in inarg
;
779 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
782 memset(&inarg
, 0, argsize
);
783 inarg
.newdir
= get_node_id(newdir
);
785 args
.opcode
= opcode
;
786 args
.nodeid
= get_node_id(olddir
);
788 args
.in_args
[0].size
= argsize
;
789 args
.in_args
[0].value
= &inarg
;
790 args
.in_args
[1].size
= oldent
->d_name
.len
+ 1;
791 args
.in_args
[1].value
= oldent
->d_name
.name
;
792 args
.in_args
[2].size
= newent
->d_name
.len
+ 1;
793 args
.in_args
[2].value
= newent
->d_name
.name
;
794 err
= fuse_simple_request(fc
, &args
);
797 fuse_invalidate_attr(d_inode(oldent
));
798 fuse_update_ctime(d_inode(oldent
));
800 if (flags
& RENAME_EXCHANGE
) {
801 fuse_invalidate_attr(d_inode(newent
));
802 fuse_update_ctime(d_inode(newent
));
805 fuse_dir_changed(olddir
);
806 if (olddir
!= newdir
)
807 fuse_dir_changed(newdir
);
809 /* newent will end up negative */
810 if (!(flags
& RENAME_EXCHANGE
) && d_really_is_positive(newent
)) {
811 fuse_invalidate_attr(d_inode(newent
));
812 fuse_invalidate_entry_cache(newent
);
813 fuse_update_ctime(d_inode(newent
));
815 } else if (err
== -EINTR
) {
816 /* If request was interrupted, DEITY only knows if the
817 rename actually took place. If the invalidation
818 fails (e.g. some process has CWD under the renamed
819 directory), then there can be inconsistency between
820 the dcache and the real filesystem. Tough luck. */
821 fuse_invalidate_entry(oldent
);
822 if (d_really_is_positive(newent
))
823 fuse_invalidate_entry(newent
);
829 static int fuse_rename2(struct inode
*olddir
, struct dentry
*oldent
,
830 struct inode
*newdir
, struct dentry
*newent
,
833 struct fuse_conn
*fc
= get_fuse_conn(olddir
);
836 if (fuse_is_bad(olddir
))
839 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
))
843 if (fc
->no_rename2
|| fc
->minor
< 23)
846 err
= fuse_rename_common(olddir
, oldent
, newdir
, newent
, flags
,
848 sizeof(struct fuse_rename2_in
));
849 if (err
== -ENOSYS
) {
854 err
= fuse_rename_common(olddir
, oldent
, newdir
, newent
, 0,
856 sizeof(struct fuse_rename_in
));
862 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
863 struct dentry
*newent
)
866 struct fuse_link_in inarg
;
867 struct inode
*inode
= d_inode(entry
);
868 struct fuse_conn
*fc
= get_fuse_conn(inode
);
871 memset(&inarg
, 0, sizeof(inarg
));
872 inarg
.oldnodeid
= get_node_id(inode
);
873 args
.opcode
= FUSE_LINK
;
875 args
.in_args
[0].size
= sizeof(inarg
);
876 args
.in_args
[0].value
= &inarg
;
877 args
.in_args
[1].size
= newent
->d_name
.len
+ 1;
878 args
.in_args
[1].value
= newent
->d_name
.name
;
879 err
= create_new_entry(fc
, &args
, newdir
, newent
, inode
->i_mode
);
880 /* Contrary to "normal" filesystems it can happen that link
881 makes two "logical" inodes point to the same "physical"
882 inode. We invalidate the attributes of the old one, so it
883 will reflect changes in the backing inode (link count,
887 struct fuse_inode
*fi
= get_fuse_inode(inode
);
889 spin_lock(&fi
->lock
);
890 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
891 if (likely(inode
->i_nlink
< UINT_MAX
))
893 spin_unlock(&fi
->lock
);
894 fuse_invalidate_attr(inode
);
895 fuse_update_ctime(inode
);
896 } else if (err
== -EINTR
) {
897 fuse_invalidate_attr(inode
);
902 static void fuse_fillattr(struct inode
*inode
, struct fuse_attr
*attr
,
905 unsigned int blkbits
;
906 struct fuse_conn
*fc
= get_fuse_conn(inode
);
908 /* see the comment in fuse_change_attributes() */
909 if (fc
->writeback_cache
&& S_ISREG(inode
->i_mode
)) {
910 attr
->size
= i_size_read(inode
);
911 attr
->mtime
= inode
->i_mtime
.tv_sec
;
912 attr
->mtimensec
= inode
->i_mtime
.tv_nsec
;
913 attr
->ctime
= inode
->i_ctime
.tv_sec
;
914 attr
->ctimensec
= inode
->i_ctime
.tv_nsec
;
917 stat
->dev
= inode
->i_sb
->s_dev
;
918 stat
->ino
= attr
->ino
;
919 stat
->mode
= (inode
->i_mode
& S_IFMT
) | (attr
->mode
& 07777);
920 stat
->nlink
= attr
->nlink
;
921 stat
->uid
= make_kuid(fc
->user_ns
, attr
->uid
);
922 stat
->gid
= make_kgid(fc
->user_ns
, attr
->gid
);
923 stat
->rdev
= inode
->i_rdev
;
924 stat
->atime
.tv_sec
= attr
->atime
;
925 stat
->atime
.tv_nsec
= attr
->atimensec
;
926 stat
->mtime
.tv_sec
= attr
->mtime
;
927 stat
->mtime
.tv_nsec
= attr
->mtimensec
;
928 stat
->ctime
.tv_sec
= attr
->ctime
;
929 stat
->ctime
.tv_nsec
= attr
->ctimensec
;
930 stat
->size
= attr
->size
;
931 stat
->blocks
= attr
->blocks
;
933 if (attr
->blksize
!= 0)
934 blkbits
= ilog2(attr
->blksize
);
936 blkbits
= inode
->i_sb
->s_blocksize_bits
;
938 stat
->blksize
= 1 << blkbits
;
941 static int fuse_do_getattr(struct inode
*inode
, struct kstat
*stat
,
945 struct fuse_getattr_in inarg
;
946 struct fuse_attr_out outarg
;
947 struct fuse_conn
*fc
= get_fuse_conn(inode
);
951 attr_version
= fuse_get_attr_version(fc
);
953 memset(&inarg
, 0, sizeof(inarg
));
954 memset(&outarg
, 0, sizeof(outarg
));
955 /* Directories have separate file-handle space */
956 if (file
&& S_ISREG(inode
->i_mode
)) {
957 struct fuse_file
*ff
= file
->private_data
;
959 inarg
.getattr_flags
|= FUSE_GETATTR_FH
;
962 args
.opcode
= FUSE_GETATTR
;
963 args
.nodeid
= get_node_id(inode
);
965 args
.in_args
[0].size
= sizeof(inarg
);
966 args
.in_args
[0].value
= &inarg
;
967 args
.out_numargs
= 1;
968 args
.out_args
[0].size
= sizeof(outarg
);
969 args
.out_args
[0].value
= &outarg
;
970 err
= fuse_simple_request(fc
, &args
);
972 if (fuse_invalid_attr(&outarg
.attr
) ||
973 (inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
974 fuse_make_bad(inode
);
977 fuse_change_attributes(inode
, &outarg
.attr
,
978 attr_timeout(&outarg
),
981 fuse_fillattr(inode
, &outarg
.attr
, stat
);
987 static int fuse_update_get_attr(struct inode
*inode
, struct file
*file
,
988 struct kstat
*stat
, u32 request_mask
,
991 struct fuse_inode
*fi
= get_fuse_inode(inode
);
995 if (flags
& AT_STATX_FORCE_SYNC
)
997 else if (flags
& AT_STATX_DONT_SYNC
)
999 else if (request_mask
& READ_ONCE(fi
->inval_mask
))
1002 sync
= time_before64(fi
->i_time
, get_jiffies_64());
1005 forget_all_cached_acls(inode
);
1006 err
= fuse_do_getattr(inode
, stat
, file
);
1008 generic_fillattr(inode
, stat
);
1009 stat
->mode
= fi
->orig_i_mode
;
1010 stat
->ino
= fi
->orig_ino
;
1016 int fuse_update_attributes(struct inode
*inode
, struct file
*file
)
1018 /* Do *not* need to get atime for internal purposes */
1019 return fuse_update_get_attr(inode
, file
, NULL
,
1020 STATX_BASIC_STATS
& ~STATX_ATIME
, 0);
1023 int fuse_reverse_inval_entry(struct super_block
*sb
, u64 parent_nodeid
,
1024 u64 child_nodeid
, struct qstr
*name
)
1027 struct inode
*parent
;
1029 struct dentry
*entry
;
1031 parent
= ilookup5(sb
, parent_nodeid
, fuse_inode_eq
, &parent_nodeid
);
1035 inode_lock_nested(parent
, I_MUTEX_PARENT
);
1036 if (!S_ISDIR(parent
->i_mode
))
1040 dir
= d_find_alias(parent
);
1044 name
->hash
= full_name_hash(dir
, name
->name
, name
->len
);
1045 entry
= d_lookup(dir
, name
);
1050 fuse_dir_changed(parent
);
1051 fuse_invalidate_entry(entry
);
1053 if (child_nodeid
!= 0 && d_really_is_positive(entry
)) {
1054 inode_lock(d_inode(entry
));
1055 if (get_node_id(d_inode(entry
)) != child_nodeid
) {
1059 if (d_mountpoint(entry
)) {
1063 if (d_is_dir(entry
)) {
1064 shrink_dcache_parent(entry
);
1065 if (!simple_empty(entry
)) {
1069 d_inode(entry
)->i_flags
|= S_DEAD
;
1072 clear_nlink(d_inode(entry
));
1075 inode_unlock(d_inode(entry
));
1084 inode_unlock(parent
);
1090 * Calling into a user-controlled filesystem gives the filesystem
1091 * daemon ptrace-like capabilities over the current process. This
1092 * means, that the filesystem daemon is able to record the exact
1093 * filesystem operations performed, and can also control the behavior
1094 * of the requester process in otherwise impossible ways. For example
1095 * it can delay the operation for arbitrary length of time allowing
1096 * DoS against the requester.
1098 * For this reason only those processes can call into the filesystem,
1099 * for which the owner of the mount has ptrace privilege. This
1100 * excludes processes started by other users, suid or sgid processes.
1102 int fuse_allow_current_process(struct fuse_conn
*fc
)
1104 const struct cred
*cred
;
1106 if (fc
->allow_other
)
1107 return current_in_userns(fc
->user_ns
);
1109 cred
= current_cred();
1110 if (uid_eq(cred
->euid
, fc
->user_id
) &&
1111 uid_eq(cred
->suid
, fc
->user_id
) &&
1112 uid_eq(cred
->uid
, fc
->user_id
) &&
1113 gid_eq(cred
->egid
, fc
->group_id
) &&
1114 gid_eq(cred
->sgid
, fc
->group_id
) &&
1115 gid_eq(cred
->gid
, fc
->group_id
))
1121 static int fuse_access(struct inode
*inode
, int mask
)
1123 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1125 struct fuse_access_in inarg
;
1128 BUG_ON(mask
& MAY_NOT_BLOCK
);
1133 memset(&inarg
, 0, sizeof(inarg
));
1134 inarg
.mask
= mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
);
1135 args
.opcode
= FUSE_ACCESS
;
1136 args
.nodeid
= get_node_id(inode
);
1137 args
.in_numargs
= 1;
1138 args
.in_args
[0].size
= sizeof(inarg
);
1139 args
.in_args
[0].value
= &inarg
;
1140 err
= fuse_simple_request(fc
, &args
);
1141 if (err
== -ENOSYS
) {
1148 static int fuse_perm_getattr(struct inode
*inode
, int mask
)
1150 if (mask
& MAY_NOT_BLOCK
)
1153 forget_all_cached_acls(inode
);
1154 return fuse_do_getattr(inode
, NULL
, NULL
);
1158 * Check permission. The two basic access models of FUSE are:
1160 * 1) Local access checking ('default_permissions' mount option) based
1161 * on file mode. This is the plain old disk filesystem permission
1164 * 2) "Remote" access checking, where server is responsible for
1165 * checking permission in each inode operation. An exception to this
1166 * is if ->permission() was invoked from sys_access() in which case an
1167 * access request is sent. Execute permission is still checked
1168 * locally based on file mode.
1170 static int fuse_permission(struct inode
*inode
, int mask
)
1172 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1173 bool refreshed
= false;
1176 if (fuse_is_bad(inode
))
1179 if (!fuse_allow_current_process(fc
))
1183 * If attributes are needed, refresh them before proceeding
1185 if (fc
->default_permissions
||
1186 ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))) {
1187 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1188 u32 perm_mask
= STATX_MODE
| STATX_UID
| STATX_GID
;
1190 if (perm_mask
& READ_ONCE(fi
->inval_mask
) ||
1191 time_before64(fi
->i_time
, get_jiffies_64())) {
1194 err
= fuse_perm_getattr(inode
, mask
);
1200 if (fc
->default_permissions
) {
1201 err
= generic_permission(inode
, mask
);
1203 /* If permission is denied, try to refresh file
1204 attributes. This is also needed, because the root
1205 node will at first have no permissions */
1206 if (err
== -EACCES
&& !refreshed
) {
1207 err
= fuse_perm_getattr(inode
, mask
);
1209 err
= generic_permission(inode
, mask
);
1212 /* Note: the opposite of the above test does not
1213 exist. So if permissions are revoked this won't be
1214 noticed immediately, only after the attribute
1215 timeout has expired */
1216 } else if (mask
& (MAY_ACCESS
| MAY_CHDIR
)) {
1217 err
= fuse_access(inode
, mask
);
1218 } else if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
)) {
1219 if (!(inode
->i_mode
& S_IXUGO
)) {
1223 err
= fuse_perm_getattr(inode
, mask
);
1224 if (!err
&& !(inode
->i_mode
& S_IXUGO
))
1231 static int fuse_readlink_page(struct inode
*inode
, struct page
*page
)
1233 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1234 struct fuse_page_desc desc
= { .length
= PAGE_SIZE
- 1 };
1235 struct fuse_args_pages ap
= {
1243 ap
.args
.opcode
= FUSE_READLINK
;
1244 ap
.args
.nodeid
= get_node_id(inode
);
1245 ap
.args
.out_pages
= true;
1246 ap
.args
.out_argvar
= true;
1247 ap
.args
.page_zeroing
= true;
1248 ap
.args
.out_numargs
= 1;
1249 ap
.args
.out_args
[0].size
= desc
.length
;
1250 res
= fuse_simple_request(fc
, &ap
.args
);
1252 fuse_invalidate_atime(inode
);
1257 if (WARN_ON(res
>= PAGE_SIZE
))
1260 link
= page_address(page
);
1266 static const char *fuse_get_link(struct dentry
*dentry
, struct inode
*inode
,
1267 struct delayed_call
*callback
)
1269 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1274 if (fuse_is_bad(inode
))
1277 if (fc
->cache_symlinks
)
1278 return page_get_link(dentry
, inode
, callback
);
1284 page
= alloc_page(GFP_KERNEL
);
1289 err
= fuse_readlink_page(inode
, page
);
1295 set_delayed_call(callback
, page_put_link
, page
);
1297 return page_address(page
);
1300 return ERR_PTR(err
);
1303 static int fuse_dir_open(struct inode
*inode
, struct file
*file
)
1305 return fuse_open_common(inode
, file
, true);
1308 static int fuse_dir_release(struct inode
*inode
, struct file
*file
)
1310 fuse_release_common(file
, true);
1315 static int fuse_dir_fsync(struct file
*file
, loff_t start
, loff_t end
,
1318 struct inode
*inode
= file
->f_mapping
->host
;
1319 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1322 if (fuse_is_bad(inode
))
1325 if (fc
->no_fsyncdir
)
1329 err
= fuse_fsync_common(file
, start
, end
, datasync
, FUSE_FSYNCDIR
);
1330 if (err
== -ENOSYS
) {
1331 fc
->no_fsyncdir
= 1;
1334 inode_unlock(inode
);
1339 static long fuse_dir_ioctl(struct file
*file
, unsigned int cmd
,
1342 struct fuse_conn
*fc
= get_fuse_conn(file
->f_mapping
->host
);
1344 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1348 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_DIR
);
1351 static long fuse_dir_compat_ioctl(struct file
*file
, unsigned int cmd
,
1354 struct fuse_conn
*fc
= get_fuse_conn(file
->f_mapping
->host
);
1359 return fuse_ioctl_common(file
, cmd
, arg
,
1360 FUSE_IOCTL_COMPAT
| FUSE_IOCTL_DIR
);
1363 static bool update_mtime(unsigned ivalid
, bool trust_local_mtime
)
1365 /* Always update if mtime is explicitly set */
1366 if (ivalid
& ATTR_MTIME_SET
)
1369 /* Or if kernel i_mtime is the official one */
1370 if (trust_local_mtime
)
1373 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1374 if ((ivalid
& ATTR_SIZE
) && (ivalid
& (ATTR_OPEN
| ATTR_FILE
)))
1377 /* In all other cases update */
1381 static void iattr_to_fattr(struct fuse_conn
*fc
, struct iattr
*iattr
,
1382 struct fuse_setattr_in
*arg
, bool trust_local_cmtime
)
1384 unsigned ivalid
= iattr
->ia_valid
;
1386 if (ivalid
& ATTR_MODE
)
1387 arg
->valid
|= FATTR_MODE
, arg
->mode
= iattr
->ia_mode
;
1388 if (ivalid
& ATTR_UID
)
1389 arg
->valid
|= FATTR_UID
, arg
->uid
= from_kuid(fc
->user_ns
, iattr
->ia_uid
);
1390 if (ivalid
& ATTR_GID
)
1391 arg
->valid
|= FATTR_GID
, arg
->gid
= from_kgid(fc
->user_ns
, iattr
->ia_gid
);
1392 if (ivalid
& ATTR_SIZE
)
1393 arg
->valid
|= FATTR_SIZE
, arg
->size
= iattr
->ia_size
;
1394 if (ivalid
& ATTR_ATIME
) {
1395 arg
->valid
|= FATTR_ATIME
;
1396 arg
->atime
= iattr
->ia_atime
.tv_sec
;
1397 arg
->atimensec
= iattr
->ia_atime
.tv_nsec
;
1398 if (!(ivalid
& ATTR_ATIME_SET
))
1399 arg
->valid
|= FATTR_ATIME_NOW
;
1401 if ((ivalid
& ATTR_MTIME
) && update_mtime(ivalid
, trust_local_cmtime
)) {
1402 arg
->valid
|= FATTR_MTIME
;
1403 arg
->mtime
= iattr
->ia_mtime
.tv_sec
;
1404 arg
->mtimensec
= iattr
->ia_mtime
.tv_nsec
;
1405 if (!(ivalid
& ATTR_MTIME_SET
) && !trust_local_cmtime
)
1406 arg
->valid
|= FATTR_MTIME_NOW
;
1408 if ((ivalid
& ATTR_CTIME
) && trust_local_cmtime
) {
1409 arg
->valid
|= FATTR_CTIME
;
1410 arg
->ctime
= iattr
->ia_ctime
.tv_sec
;
1411 arg
->ctimensec
= iattr
->ia_ctime
.tv_nsec
;
1416 * Prevent concurrent writepages on inode
1418 * This is done by adding a negative bias to the inode write counter
1419 * and waiting for all pending writes to finish.
1421 void fuse_set_nowrite(struct inode
*inode
)
1423 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1425 BUG_ON(!inode_is_locked(inode
));
1427 spin_lock(&fi
->lock
);
1428 BUG_ON(fi
->writectr
< 0);
1429 fi
->writectr
+= FUSE_NOWRITE
;
1430 spin_unlock(&fi
->lock
);
1431 wait_event(fi
->page_waitq
, fi
->writectr
== FUSE_NOWRITE
);
1435 * Allow writepages on inode
1437 * Remove the bias from the writecounter and send any queued
1440 static void __fuse_release_nowrite(struct inode
*inode
)
1442 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1444 BUG_ON(fi
->writectr
!= FUSE_NOWRITE
);
1446 fuse_flush_writepages(inode
);
1449 void fuse_release_nowrite(struct inode
*inode
)
1451 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1453 spin_lock(&fi
->lock
);
1454 __fuse_release_nowrite(inode
);
1455 spin_unlock(&fi
->lock
);
1458 static void fuse_setattr_fill(struct fuse_conn
*fc
, struct fuse_args
*args
,
1459 struct inode
*inode
,
1460 struct fuse_setattr_in
*inarg_p
,
1461 struct fuse_attr_out
*outarg_p
)
1463 args
->opcode
= FUSE_SETATTR
;
1464 args
->nodeid
= get_node_id(inode
);
1465 args
->in_numargs
= 1;
1466 args
->in_args
[0].size
= sizeof(*inarg_p
);
1467 args
->in_args
[0].value
= inarg_p
;
1468 args
->out_numargs
= 1;
1469 args
->out_args
[0].size
= sizeof(*outarg_p
);
1470 args
->out_args
[0].value
= outarg_p
;
1474 * Flush inode->i_mtime to the server
1476 int fuse_flush_times(struct inode
*inode
, struct fuse_file
*ff
)
1478 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1480 struct fuse_setattr_in inarg
;
1481 struct fuse_attr_out outarg
;
1483 memset(&inarg
, 0, sizeof(inarg
));
1484 memset(&outarg
, 0, sizeof(outarg
));
1486 inarg
.valid
= FATTR_MTIME
;
1487 inarg
.mtime
= inode
->i_mtime
.tv_sec
;
1488 inarg
.mtimensec
= inode
->i_mtime
.tv_nsec
;
1489 if (fc
->minor
>= 23) {
1490 inarg
.valid
|= FATTR_CTIME
;
1491 inarg
.ctime
= inode
->i_ctime
.tv_sec
;
1492 inarg
.ctimensec
= inode
->i_ctime
.tv_nsec
;
1495 inarg
.valid
|= FATTR_FH
;
1498 fuse_setattr_fill(fc
, &args
, inode
, &inarg
, &outarg
);
1500 return fuse_simple_request(fc
, &args
);
1504 * Set attributes, and at the same time refresh them.
1506 * Truncation is slightly complicated, because the 'truncate' request
1507 * may fail, in which case we don't want to touch the mapping.
1508 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1509 * and the actual truncation by hand.
1511 int fuse_do_setattr(struct dentry
*dentry
, struct iattr
*attr
,
1514 struct inode
*inode
= d_inode(dentry
);
1515 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1516 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1518 struct fuse_setattr_in inarg
;
1519 struct fuse_attr_out outarg
;
1520 bool is_truncate
= false;
1521 bool is_wb
= fc
->writeback_cache
;
1524 bool trust_local_cmtime
= is_wb
&& S_ISREG(inode
->i_mode
);
1526 if (!fc
->default_permissions
)
1527 attr
->ia_valid
|= ATTR_FORCE
;
1529 err
= setattr_prepare(dentry
, attr
);
1533 if (attr
->ia_valid
& ATTR_OPEN
) {
1534 /* This is coming from open(..., ... | O_TRUNC); */
1535 WARN_ON(!(attr
->ia_valid
& ATTR_SIZE
));
1536 WARN_ON(attr
->ia_size
!= 0);
1537 if (fc
->atomic_o_trunc
) {
1539 * No need to send request to userspace, since actual
1540 * truncation has already been done by OPEN. But still
1541 * need to truncate page cache.
1543 i_size_write(inode
, 0);
1544 truncate_pagecache(inode
, 0);
1550 if (attr
->ia_valid
& ATTR_SIZE
) {
1551 if (WARN_ON(!S_ISREG(inode
->i_mode
)))
1556 /* Flush dirty data/metadata before non-truncate SETATTR */
1557 if (is_wb
&& S_ISREG(inode
->i_mode
) &&
1559 (ATTR_MODE
| ATTR_UID
| ATTR_GID
| ATTR_MTIME_SET
|
1561 err
= write_inode_now(inode
, true);
1565 fuse_set_nowrite(inode
);
1566 fuse_release_nowrite(inode
);
1570 fuse_set_nowrite(inode
);
1571 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1572 if (trust_local_cmtime
&& attr
->ia_size
!= inode
->i_size
)
1573 attr
->ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
1576 memset(&inarg
, 0, sizeof(inarg
));
1577 memset(&outarg
, 0, sizeof(outarg
));
1578 iattr_to_fattr(fc
, attr
, &inarg
, trust_local_cmtime
);
1580 struct fuse_file
*ff
= file
->private_data
;
1581 inarg
.valid
|= FATTR_FH
;
1584 if (attr
->ia_valid
& ATTR_SIZE
) {
1585 /* For mandatory locking in truncate */
1586 inarg
.valid
|= FATTR_LOCKOWNER
;
1587 inarg
.lock_owner
= fuse_lock_owner_id(fc
, current
->files
);
1589 fuse_setattr_fill(fc
, &args
, inode
, &inarg
, &outarg
);
1590 err
= fuse_simple_request(fc
, &args
);
1593 fuse_invalidate_attr(inode
);
1597 if (fuse_invalid_attr(&outarg
.attr
) ||
1598 (inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
1599 fuse_make_bad(inode
);
1604 spin_lock(&fi
->lock
);
1605 /* the kernel maintains i_mtime locally */
1606 if (trust_local_cmtime
) {
1607 if (attr
->ia_valid
& ATTR_MTIME
)
1608 inode
->i_mtime
= attr
->ia_mtime
;
1609 if (attr
->ia_valid
& ATTR_CTIME
)
1610 inode
->i_ctime
= attr
->ia_ctime
;
1611 /* FIXME: clear I_DIRTY_SYNC? */
1614 fuse_change_attributes_common(inode
, &outarg
.attr
,
1615 attr_timeout(&outarg
));
1616 oldsize
= inode
->i_size
;
1617 /* see the comment in fuse_change_attributes() */
1618 if (!is_wb
|| is_truncate
|| !S_ISREG(inode
->i_mode
))
1619 i_size_write(inode
, outarg
.attr
.size
);
1622 /* NOTE: this may release/reacquire fi->lock */
1623 __fuse_release_nowrite(inode
);
1625 spin_unlock(&fi
->lock
);
1628 * Only call invalidate_inode_pages2() after removing
1629 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1631 if ((is_truncate
|| !is_wb
) &&
1632 S_ISREG(inode
->i_mode
) && oldsize
!= outarg
.attr
.size
) {
1633 truncate_pagecache(inode
, outarg
.attr
.size
);
1634 invalidate_inode_pages2(inode
->i_mapping
);
1637 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1642 fuse_release_nowrite(inode
);
1644 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1648 static int fuse_setattr(struct dentry
*entry
, struct iattr
*attr
)
1650 struct inode
*inode
= d_inode(entry
);
1651 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1652 struct file
*file
= (attr
->ia_valid
& ATTR_FILE
) ? attr
->ia_file
: NULL
;
1655 if (fuse_is_bad(inode
))
1658 if (!fuse_allow_current_process(get_fuse_conn(inode
)))
1661 if (attr
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
)) {
1662 attr
->ia_valid
&= ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
|
1666 * The only sane way to reliably kill suid/sgid is to do it in
1667 * the userspace filesystem
1669 * This should be done on write(), truncate() and chown().
1671 if (!fc
->handle_killpriv
) {
1673 * ia_mode calculation may have used stale i_mode.
1674 * Refresh and recalculate.
1676 ret
= fuse_do_getattr(inode
, NULL
, file
);
1680 attr
->ia_mode
= inode
->i_mode
;
1681 if (inode
->i_mode
& S_ISUID
) {
1682 attr
->ia_valid
|= ATTR_MODE
;
1683 attr
->ia_mode
&= ~S_ISUID
;
1685 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
1686 attr
->ia_valid
|= ATTR_MODE
;
1687 attr
->ia_mode
&= ~S_ISGID
;
1691 if (!attr
->ia_valid
)
1694 ret
= fuse_do_setattr(entry
, attr
, file
);
1697 * If filesystem supports acls it may have updated acl xattrs in
1698 * the filesystem, so forget cached acls for the inode.
1701 forget_all_cached_acls(inode
);
1703 /* Directory mode changed, may need to revalidate access */
1704 if (d_is_dir(entry
) && (attr
->ia_valid
& ATTR_MODE
))
1705 fuse_invalidate_entry_cache(entry
);
1710 static int fuse_getattr(const struct path
*path
, struct kstat
*stat
,
1711 u32 request_mask
, unsigned int flags
)
1713 struct inode
*inode
= d_inode(path
->dentry
);
1714 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1716 if (fuse_is_bad(inode
))
1719 if (!fuse_allow_current_process(fc
))
1722 return fuse_update_get_attr(inode
, NULL
, stat
, request_mask
, flags
);
1725 static const struct inode_operations fuse_dir_inode_operations
= {
1726 .lookup
= fuse_lookup
,
1727 .mkdir
= fuse_mkdir
,
1728 .symlink
= fuse_symlink
,
1729 .unlink
= fuse_unlink
,
1730 .rmdir
= fuse_rmdir
,
1731 .rename
= fuse_rename2
,
1733 .setattr
= fuse_setattr
,
1734 .create
= fuse_create
,
1735 .atomic_open
= fuse_atomic_open
,
1736 .mknod
= fuse_mknod
,
1737 .permission
= fuse_permission
,
1738 .getattr
= fuse_getattr
,
1739 .listxattr
= fuse_listxattr
,
1740 .get_acl
= fuse_get_acl
,
1741 .set_acl
= fuse_set_acl
,
1744 static const struct file_operations fuse_dir_operations
= {
1745 .llseek
= generic_file_llseek
,
1746 .read
= generic_read_dir
,
1747 .iterate_shared
= fuse_readdir
,
1748 .open
= fuse_dir_open
,
1749 .release
= fuse_dir_release
,
1750 .fsync
= fuse_dir_fsync
,
1751 .unlocked_ioctl
= fuse_dir_ioctl
,
1752 .compat_ioctl
= fuse_dir_compat_ioctl
,
1755 static const struct inode_operations fuse_common_inode_operations
= {
1756 .setattr
= fuse_setattr
,
1757 .permission
= fuse_permission
,
1758 .getattr
= fuse_getattr
,
1759 .listxattr
= fuse_listxattr
,
1760 .get_acl
= fuse_get_acl
,
1761 .set_acl
= fuse_set_acl
,
1764 static const struct inode_operations fuse_symlink_inode_operations
= {
1765 .setattr
= fuse_setattr
,
1766 .get_link
= fuse_get_link
,
1767 .getattr
= fuse_getattr
,
1768 .listxattr
= fuse_listxattr
,
1771 void fuse_init_common(struct inode
*inode
)
1773 inode
->i_op
= &fuse_common_inode_operations
;
1776 void fuse_init_dir(struct inode
*inode
)
1778 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1780 inode
->i_op
= &fuse_dir_inode_operations
;
1781 inode
->i_fop
= &fuse_dir_operations
;
1783 spin_lock_init(&fi
->rdc
.lock
);
1784 fi
->rdc
.cached
= false;
1787 fi
->rdc
.version
= 0;
1790 static int fuse_symlink_readpage(struct file
*null
, struct page
*page
)
1792 int err
= fuse_readlink_page(page
->mapping
->host
, page
);
1795 SetPageUptodate(page
);
1802 static const struct address_space_operations fuse_symlink_aops
= {
1803 .readpage
= fuse_symlink_readpage
,
1806 void fuse_init_symlink(struct inode
*inode
)
1808 inode
->i_op
= &fuse_symlink_inode_operations
;
1809 inode
->i_data
.a_ops
= &fuse_symlink_aops
;
1810 inode_nohighmem(inode
);