2 FUSE: Filesystem in Userspace
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/fs_context.h>
14 #include <linux/sched.h>
15 #include <linux/namei.h>
16 #include <linux/slab.h>
17 #include <linux/xattr.h>
18 #include <linux/iversion.h>
19 #include <linux/posix_acl.h>
21 static void fuse_advise_use_readdirplus(struct inode *dir)
23 struct fuse_inode *fi = get_fuse_inode(dir);
25 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
28 #if BITS_PER_LONG >= 64
29 static inline void __fuse_dentry_settime(struct dentry *entry, u64 time)
31 entry->d_fsdata = (void *) time;
34 static inline u64 fuse_dentry_time(const struct dentry *entry)
36 return (u64)entry->d_fsdata;
45 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
47 ((union fuse_dentry *) dentry->d_fsdata)->time = time;
50 static inline u64 fuse_dentry_time(const struct dentry *entry)
52 return ((union fuse_dentry *) entry->d_fsdata)->time;
56 static void fuse_dentry_settime(struct dentry *dentry, u64 time)
58 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
59 bool delete = !time && fc->delete_stale;
61 * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
62 * Don't care about races, either way it's just an optimization
64 if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
65 (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
66 spin_lock(&dentry->d_lock);
68 dentry->d_flags &= ~DCACHE_OP_DELETE;
70 dentry->d_flags |= DCACHE_OP_DELETE;
71 spin_unlock(&dentry->d_lock);
74 __fuse_dentry_settime(dentry, time);
78 * FUSE caches dentries and attributes with separate timeout. The
79 * time in jiffies until the dentry/attributes are valid is stored in
80 * dentry->d_fsdata and fuse_inode->i_time respectively.
84 * Calculate the time in jiffies until a dentry/attributes are valid
86 static u64 time_to_jiffies(u64 sec, u32 nsec)
89 struct timespec64 ts = {
91 min_t(u32, nsec, NSEC_PER_SEC - 1)
94 return get_jiffies_64() + timespec64_to_jiffies(&ts);
100 * Set dentry and possibly attribute timeouts from the lookup/mk*
103 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
105 fuse_dentry_settime(entry,
106 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
109 static u64 attr_timeout(struct fuse_attr_out *o)
111 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
114 u64 entry_attr_timeout(struct fuse_entry_out *o)
116 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
119 static void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
121 set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
125 * Mark the attributes as stale, so that at the next call to
126 * ->getattr() they will be fetched from userspace
128 void fuse_invalidate_attr(struct inode *inode)
130 fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
133 static void fuse_dir_changed(struct inode *dir)
135 fuse_invalidate_attr(dir);
136 inode_maybe_inc_iversion(dir, false);
140 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
143 void fuse_invalidate_atime(struct inode *inode)
145 if (!IS_RDONLY(inode))
146 fuse_invalidate_attr_mask(inode, STATX_ATIME);
150 * Just mark the entry as stale, so that a next attempt to look it up
151 * will result in a new lookup call to userspace
153 * This is called when a dentry is about to become negative and the
154 * timeout is unknown (unlink, rmdir, rename and in some cases
157 void fuse_invalidate_entry_cache(struct dentry *entry)
159 fuse_dentry_settime(entry, 0);
163 * Same as fuse_invalidate_entry_cache(), but also try to remove the
164 * dentry from the hash
166 static void fuse_invalidate_entry(struct dentry *entry)
169 fuse_invalidate_entry_cache(entry);
172 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
173 u64 nodeid, const struct qstr *name,
174 struct fuse_entry_out *outarg)
176 memset(outarg, 0, sizeof(struct fuse_entry_out));
177 args->opcode = FUSE_LOOKUP;
178 args->nodeid = nodeid;
179 args->in_numargs = 1;
180 args->in_args[0].size = name->len + 1;
181 args->in_args[0].value = name->name;
182 args->out_numargs = 1;
183 args->out_args[0].size = sizeof(struct fuse_entry_out);
184 args->out_args[0].value = outarg;
188 * Check whether the dentry is still valid
190 * If the entry validity timeout has expired and the dentry is
191 * positive, try to redo the lookup. If the lookup results in a
192 * different inode, then let the VFS invalidate the dentry and redo
193 * the lookup once more. If the lookup results in the same inode,
194 * then refresh the attributes, timeouts and mark the dentry valid.
196 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
199 struct dentry *parent;
200 struct fuse_mount *fm;
201 struct fuse_inode *fi;
204 inode = d_inode_rcu(entry);
205 if (inode && fuse_is_bad(inode))
207 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
208 (flags & (LOOKUP_EXCL | LOOKUP_REVAL))) {
209 struct fuse_entry_out outarg;
211 struct fuse_forget_link *forget;
214 /* For negative dentries, always do a fresh lookup */
219 if (flags & LOOKUP_RCU)
222 fm = get_fuse_mount(inode);
224 forget = fuse_alloc_forget();
229 attr_version = fuse_get_attr_version(fm->fc);
231 parent = dget_parent(entry);
232 fuse_lookup_init(fm->fc, &args, get_node_id(d_inode(parent)),
233 &entry->d_name, &outarg);
234 ret = fuse_simple_request(fm, &args);
236 /* Zero nodeid is same as -ENOENT */
237 if (!ret && !outarg.nodeid)
240 fi = get_fuse_inode(inode);
241 if (outarg.nodeid != get_node_id(inode) ||
242 (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) {
243 fuse_queue_forget(fm->fc, forget,
247 spin_lock(&fi->lock);
249 spin_unlock(&fi->lock);
254 if (ret || fuse_invalid_attr(&outarg.attr) ||
255 inode_wrong_type(inode, outarg.attr.mode))
258 forget_all_cached_acls(inode);
259 fuse_change_attributes(inode, &outarg.attr,
260 entry_attr_timeout(&outarg),
262 fuse_change_entry_timeout(entry, &outarg);
264 fi = get_fuse_inode(inode);
265 if (flags & LOOKUP_RCU) {
266 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
268 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
269 parent = dget_parent(entry);
270 fuse_advise_use_readdirplus(d_inode(parent));
283 #if BITS_PER_LONG < 64
284 static int fuse_dentry_init(struct dentry *dentry)
286 dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry),
287 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
289 return dentry->d_fsdata ? 0 : -ENOMEM;
291 static void fuse_dentry_release(struct dentry *dentry)
293 union fuse_dentry *fd = dentry->d_fsdata;
299 static int fuse_dentry_delete(const struct dentry *dentry)
301 return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
305 * Create a fuse_mount object with a new superblock (with path->dentry
306 * as the root), and return that mount so it can be auto-mounted on
309 static struct vfsmount *fuse_dentry_automount(struct path *path)
311 struct fs_context *fsc;
312 struct fuse_mount *parent_fm = get_fuse_mount_super(path->mnt->mnt_sb);
313 struct fuse_conn *fc = parent_fm->fc;
314 struct fuse_mount *fm;
315 struct vfsmount *mnt;
316 struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
317 struct super_block *sb;
320 fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
327 fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
332 sb = sget_fc(fsc, NULL, set_anon_super_fc);
338 fm->fc = fuse_conn_get(fc);
340 /* Initialize superblock, making @mp_fi its root */
341 err = fuse_fill_super_submount(sb, mp_fi);
345 sb->s_flags |= SB_ACTIVE;
346 fsc->root = dget(sb->s_root);
347 /* We are done configuring the superblock, so unlock it */
348 up_write(&sb->s_umount);
350 down_write(&fc->killsb);
351 list_add_tail(&fm->fc_entry, &fc->mounts);
352 up_write(&fc->killsb);
354 /* Create the submount */
355 mnt = vfs_create_mount(fsc);
366 * Only jump here when fsc->root is NULL and sb is still locked
367 * (otherwise put_fs_context() will put the superblock)
369 deactivate_locked_super(sb);
376 const struct dentry_operations fuse_dentry_operations = {
377 .d_revalidate = fuse_dentry_revalidate,
378 .d_delete = fuse_dentry_delete,
379 #if BITS_PER_LONG < 64
380 .d_init = fuse_dentry_init,
381 .d_release = fuse_dentry_release,
383 .d_automount = fuse_dentry_automount,
386 const struct dentry_operations fuse_root_dentry_operations = {
387 #if BITS_PER_LONG < 64
388 .d_init = fuse_dentry_init,
389 .d_release = fuse_dentry_release,
393 int fuse_valid_type(int m)
395 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
396 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
399 bool fuse_invalid_attr(struct fuse_attr *attr)
401 return !fuse_valid_type(attr->mode) ||
402 attr->size > LLONG_MAX;
405 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
406 struct fuse_entry_out *outarg, struct inode **inode)
408 struct fuse_mount *fm = get_fuse_mount_super(sb);
410 struct fuse_forget_link *forget;
416 if (name->len > FUSE_NAME_MAX)
420 forget = fuse_alloc_forget();
425 attr_version = fuse_get_attr_version(fm->fc);
427 fuse_lookup_init(fm->fc, &args, nodeid, name, outarg);
428 err = fuse_simple_request(fm, &args);
429 /* Zero nodeid is same as -ENOENT, but with valid timeout */
430 if (err || !outarg->nodeid)
436 if (fuse_invalid_attr(&outarg->attr))
439 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
440 &outarg->attr, entry_attr_timeout(outarg),
444 fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1);
455 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
459 struct fuse_entry_out outarg;
461 struct dentry *newent;
462 bool outarg_valid = true;
465 if (fuse_is_bad(dir))
466 return ERR_PTR(-EIO);
468 locked = fuse_lock_inode(dir);
469 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
471 fuse_unlock_inode(dir, locked);
472 if (err == -ENOENT) {
473 outarg_valid = false;
480 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
483 newent = d_splice_alias(inode, entry);
484 err = PTR_ERR(newent);
488 entry = newent ? newent : entry;
490 fuse_change_entry_timeout(entry, &outarg);
492 fuse_invalidate_entry_cache(entry);
495 fuse_advise_use_readdirplus(dir);
505 * Atomic create+open operation
507 * If the filesystem doesn't support this, then fall back to separate
508 * 'mknod' + 'open' requests.
510 static int fuse_create_open(struct inode *dir, struct dentry *entry,
511 struct file *file, unsigned int flags,
516 struct fuse_mount *fm = get_fuse_mount(dir);
518 struct fuse_forget_link *forget;
519 struct fuse_create_in inarg;
520 struct fuse_open_out outopen;
521 struct fuse_entry_out outentry;
522 struct fuse_inode *fi;
523 struct fuse_file *ff;
525 /* Userspace expects S_IFREG in create mode */
526 BUG_ON((mode & S_IFMT) != S_IFREG);
528 forget = fuse_alloc_forget();
534 ff = fuse_file_alloc(fm);
536 goto out_put_forget_req;
538 if (!fm->fc->dont_mask)
539 mode &= ~current_umask();
542 memset(&inarg, 0, sizeof(inarg));
543 memset(&outentry, 0, sizeof(outentry));
546 inarg.umask = current_umask();
548 if (fm->fc->handle_killpriv_v2 && (flags & O_TRUNC) &&
549 !(flags & O_EXCL) && !capable(CAP_FSETID)) {
550 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
553 args.opcode = FUSE_CREATE;
554 args.nodeid = get_node_id(dir);
556 args.in_args[0].size = sizeof(inarg);
557 args.in_args[0].value = &inarg;
558 args.in_args[1].size = entry->d_name.len + 1;
559 args.in_args[1].value = entry->d_name.name;
560 args.out_numargs = 2;
561 args.out_args[0].size = sizeof(outentry);
562 args.out_args[0].value = &outentry;
563 args.out_args[1].size = sizeof(outopen);
564 args.out_args[1].value = &outopen;
565 err = fuse_simple_request(fm, &args);
570 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
571 fuse_invalid_attr(&outentry.attr))
575 ff->nodeid = outentry.nodeid;
576 ff->open_flags = outopen.open_flags;
577 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
578 &outentry.attr, entry_attr_timeout(&outentry), 0);
580 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
581 fuse_sync_release(NULL, ff, flags);
582 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
587 d_instantiate(entry, inode);
588 fuse_change_entry_timeout(entry, &outentry);
589 fuse_dir_changed(dir);
590 err = finish_open(file, entry, generic_file_open);
592 fi = get_fuse_inode(inode);
593 fuse_sync_release(fi, ff, flags);
595 file->private_data = ff;
596 fuse_finish_open(inode, file);
608 static int fuse_mknod(struct user_namespace *, struct inode *, struct dentry *,
610 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
611 struct file *file, unsigned flags,
615 struct fuse_conn *fc = get_fuse_conn(dir);
616 struct dentry *res = NULL;
618 if (fuse_is_bad(dir))
621 if (d_in_lookup(entry)) {
622 res = fuse_lookup(dir, entry, 0);
630 if (!(flags & O_CREAT) || d_really_is_positive(entry))
634 file->f_mode |= FMODE_CREATED;
639 err = fuse_create_open(dir, entry, file, flags, mode);
640 if (err == -ENOSYS) {
649 err = fuse_mknod(&init_user_ns, dir, entry, mode, 0);
653 return finish_no_open(file, res);
657 * Code shared between mknod, mkdir, symlink and link
659 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
660 struct inode *dir, struct dentry *entry,
663 struct fuse_entry_out outarg;
667 struct fuse_forget_link *forget;
669 if (fuse_is_bad(dir))
672 forget = fuse_alloc_forget();
676 memset(&outarg, 0, sizeof(outarg));
677 args->nodeid = get_node_id(dir);
678 args->out_numargs = 1;
679 args->out_args[0].size = sizeof(outarg);
680 args->out_args[0].value = &outarg;
681 err = fuse_simple_request(fm, args);
683 goto out_put_forget_req;
686 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
687 goto out_put_forget_req;
689 if ((outarg.attr.mode ^ mode) & S_IFMT)
690 goto out_put_forget_req;
692 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
693 &outarg.attr, entry_attr_timeout(&outarg), 0);
695 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
701 d = d_splice_alias(inode, entry);
706 fuse_change_entry_timeout(d, &outarg);
709 fuse_change_entry_timeout(entry, &outarg);
711 fuse_dir_changed(dir);
719 static int fuse_mknod(struct user_namespace *mnt_userns, struct inode *dir,
720 struct dentry *entry, umode_t mode, dev_t rdev)
722 struct fuse_mknod_in inarg;
723 struct fuse_mount *fm = get_fuse_mount(dir);
726 if (!fm->fc->dont_mask)
727 mode &= ~current_umask();
729 memset(&inarg, 0, sizeof(inarg));
731 inarg.rdev = new_encode_dev(rdev);
732 inarg.umask = current_umask();
733 args.opcode = FUSE_MKNOD;
735 args.in_args[0].size = sizeof(inarg);
736 args.in_args[0].value = &inarg;
737 args.in_args[1].size = entry->d_name.len + 1;
738 args.in_args[1].value = entry->d_name.name;
739 return create_new_entry(fm, &args, dir, entry, mode);
742 static int fuse_create(struct user_namespace *mnt_userns, struct inode *dir,
743 struct dentry *entry, umode_t mode, bool excl)
745 return fuse_mknod(&init_user_ns, dir, entry, mode, 0);
748 static int fuse_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
749 struct dentry *entry, umode_t mode)
751 struct fuse_mkdir_in inarg;
752 struct fuse_mount *fm = get_fuse_mount(dir);
755 if (!fm->fc->dont_mask)
756 mode &= ~current_umask();
758 memset(&inarg, 0, sizeof(inarg));
760 inarg.umask = current_umask();
761 args.opcode = FUSE_MKDIR;
763 args.in_args[0].size = sizeof(inarg);
764 args.in_args[0].value = &inarg;
765 args.in_args[1].size = entry->d_name.len + 1;
766 args.in_args[1].value = entry->d_name.name;
767 return create_new_entry(fm, &args, dir, entry, S_IFDIR);
770 static int fuse_symlink(struct user_namespace *mnt_userns, struct inode *dir,
771 struct dentry *entry, const char *link)
773 struct fuse_mount *fm = get_fuse_mount(dir);
774 unsigned len = strlen(link) + 1;
777 args.opcode = FUSE_SYMLINK;
779 args.in_args[0].size = entry->d_name.len + 1;
780 args.in_args[0].value = entry->d_name.name;
781 args.in_args[1].size = len;
782 args.in_args[1].value = link;
783 return create_new_entry(fm, &args, dir, entry, S_IFLNK);
786 void fuse_update_ctime(struct inode *inode)
788 if (!IS_NOCMTIME(inode)) {
789 inode->i_ctime = current_time(inode);
790 mark_inode_dirty_sync(inode);
794 static int fuse_unlink(struct inode *dir, struct dentry *entry)
797 struct fuse_mount *fm = get_fuse_mount(dir);
800 if (fuse_is_bad(dir))
803 args.opcode = FUSE_UNLINK;
804 args.nodeid = get_node_id(dir);
806 args.in_args[0].size = entry->d_name.len + 1;
807 args.in_args[0].value = entry->d_name.name;
808 err = fuse_simple_request(fm, &args);
810 struct inode *inode = d_inode(entry);
811 struct fuse_inode *fi = get_fuse_inode(inode);
813 spin_lock(&fi->lock);
814 fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
816 * If i_nlink == 0 then unlink doesn't make sense, yet this can
817 * happen if userspace filesystem is careless. It would be
818 * difficult to enforce correct nlink usage so just ignore this
821 if (inode->i_nlink > 0)
823 spin_unlock(&fi->lock);
824 fuse_invalidate_attr(inode);
825 fuse_dir_changed(dir);
826 fuse_invalidate_entry_cache(entry);
827 fuse_update_ctime(inode);
828 } else if (err == -EINTR)
829 fuse_invalidate_entry(entry);
833 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
836 struct fuse_mount *fm = get_fuse_mount(dir);
839 if (fuse_is_bad(dir))
842 args.opcode = FUSE_RMDIR;
843 args.nodeid = get_node_id(dir);
845 args.in_args[0].size = entry->d_name.len + 1;
846 args.in_args[0].value = entry->d_name.name;
847 err = fuse_simple_request(fm, &args);
849 clear_nlink(d_inode(entry));
850 fuse_dir_changed(dir);
851 fuse_invalidate_entry_cache(entry);
852 } else if (err == -EINTR)
853 fuse_invalidate_entry(entry);
857 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
858 struct inode *newdir, struct dentry *newent,
859 unsigned int flags, int opcode, size_t argsize)
862 struct fuse_rename2_in inarg;
863 struct fuse_mount *fm = get_fuse_mount(olddir);
866 memset(&inarg, 0, argsize);
867 inarg.newdir = get_node_id(newdir);
869 args.opcode = opcode;
870 args.nodeid = get_node_id(olddir);
872 args.in_args[0].size = argsize;
873 args.in_args[0].value = &inarg;
874 args.in_args[1].size = oldent->d_name.len + 1;
875 args.in_args[1].value = oldent->d_name.name;
876 args.in_args[2].size = newent->d_name.len + 1;
877 args.in_args[2].value = newent->d_name.name;
878 err = fuse_simple_request(fm, &args);
881 fuse_invalidate_attr(d_inode(oldent));
882 fuse_update_ctime(d_inode(oldent));
884 if (flags & RENAME_EXCHANGE) {
885 fuse_invalidate_attr(d_inode(newent));
886 fuse_update_ctime(d_inode(newent));
889 fuse_dir_changed(olddir);
890 if (olddir != newdir)
891 fuse_dir_changed(newdir);
893 /* newent will end up negative */
894 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
895 fuse_invalidate_attr(d_inode(newent));
896 fuse_invalidate_entry_cache(newent);
897 fuse_update_ctime(d_inode(newent));
899 } else if (err == -EINTR) {
900 /* If request was interrupted, DEITY only knows if the
901 rename actually took place. If the invalidation
902 fails (e.g. some process has CWD under the renamed
903 directory), then there can be inconsistency between
904 the dcache and the real filesystem. Tough luck. */
905 fuse_invalidate_entry(oldent);
906 if (d_really_is_positive(newent))
907 fuse_invalidate_entry(newent);
913 static int fuse_rename2(struct user_namespace *mnt_userns, struct inode *olddir,
914 struct dentry *oldent, struct inode *newdir,
915 struct dentry *newent, unsigned int flags)
917 struct fuse_conn *fc = get_fuse_conn(olddir);
920 if (fuse_is_bad(olddir))
923 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
927 if (fc->no_rename2 || fc->minor < 23)
930 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
932 sizeof(struct fuse_rename2_in));
933 if (err == -ENOSYS) {
938 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
940 sizeof(struct fuse_rename_in));
946 static int fuse_link(struct dentry *entry, struct inode *newdir,
947 struct dentry *newent)
950 struct fuse_link_in inarg;
951 struct inode *inode = d_inode(entry);
952 struct fuse_mount *fm = get_fuse_mount(inode);
955 memset(&inarg, 0, sizeof(inarg));
956 inarg.oldnodeid = get_node_id(inode);
957 args.opcode = FUSE_LINK;
959 args.in_args[0].size = sizeof(inarg);
960 args.in_args[0].value = &inarg;
961 args.in_args[1].size = newent->d_name.len + 1;
962 args.in_args[1].value = newent->d_name.name;
963 err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
964 /* Contrary to "normal" filesystems it can happen that link
965 makes two "logical" inodes point to the same "physical"
966 inode. We invalidate the attributes of the old one, so it
967 will reflect changes in the backing inode (link count,
971 struct fuse_inode *fi = get_fuse_inode(inode);
973 spin_lock(&fi->lock);
974 fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
975 if (likely(inode->i_nlink < UINT_MAX))
977 spin_unlock(&fi->lock);
978 fuse_invalidate_attr(inode);
979 fuse_update_ctime(inode);
980 } else if (err == -EINTR) {
981 fuse_invalidate_attr(inode);
986 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
989 unsigned int blkbits;
990 struct fuse_conn *fc = get_fuse_conn(inode);
992 /* see the comment in fuse_change_attributes() */
993 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
994 attr->size = i_size_read(inode);
995 attr->mtime = inode->i_mtime.tv_sec;
996 attr->mtimensec = inode->i_mtime.tv_nsec;
997 attr->ctime = inode->i_ctime.tv_sec;
998 attr->ctimensec = inode->i_ctime.tv_nsec;
1001 stat->dev = inode->i_sb->s_dev;
1002 stat->ino = attr->ino;
1003 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1004 stat->nlink = attr->nlink;
1005 stat->uid = make_kuid(fc->user_ns, attr->uid);
1006 stat->gid = make_kgid(fc->user_ns, attr->gid);
1007 stat->rdev = inode->i_rdev;
1008 stat->atime.tv_sec = attr->atime;
1009 stat->atime.tv_nsec = attr->atimensec;
1010 stat->mtime.tv_sec = attr->mtime;
1011 stat->mtime.tv_nsec = attr->mtimensec;
1012 stat->ctime.tv_sec = attr->ctime;
1013 stat->ctime.tv_nsec = attr->ctimensec;
1014 stat->size = attr->size;
1015 stat->blocks = attr->blocks;
1017 if (attr->blksize != 0)
1018 blkbits = ilog2(attr->blksize);
1020 blkbits = inode->i_sb->s_blocksize_bits;
1022 stat->blksize = 1 << blkbits;
1025 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
1029 struct fuse_getattr_in inarg;
1030 struct fuse_attr_out outarg;
1031 struct fuse_mount *fm = get_fuse_mount(inode);
1035 attr_version = fuse_get_attr_version(fm->fc);
1037 memset(&inarg, 0, sizeof(inarg));
1038 memset(&outarg, 0, sizeof(outarg));
1039 /* Directories have separate file-handle space */
1040 if (file && S_ISREG(inode->i_mode)) {
1041 struct fuse_file *ff = file->private_data;
1043 inarg.getattr_flags |= FUSE_GETATTR_FH;
1046 args.opcode = FUSE_GETATTR;
1047 args.nodeid = get_node_id(inode);
1048 args.in_numargs = 1;
1049 args.in_args[0].size = sizeof(inarg);
1050 args.in_args[0].value = &inarg;
1051 args.out_numargs = 1;
1052 args.out_args[0].size = sizeof(outarg);
1053 args.out_args[0].value = &outarg;
1054 err = fuse_simple_request(fm, &args);
1056 if (fuse_invalid_attr(&outarg.attr) ||
1057 inode_wrong_type(inode, outarg.attr.mode)) {
1058 fuse_make_bad(inode);
1061 fuse_change_attributes(inode, &outarg.attr,
1062 attr_timeout(&outarg),
1065 fuse_fillattr(inode, &outarg.attr, stat);
1071 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1072 struct kstat *stat, u32 request_mask,
1075 struct fuse_inode *fi = get_fuse_inode(inode);
1079 if (flags & AT_STATX_FORCE_SYNC)
1081 else if (flags & AT_STATX_DONT_SYNC)
1083 else if (request_mask & READ_ONCE(fi->inval_mask))
1086 sync = time_before64(fi->i_time, get_jiffies_64());
1089 forget_all_cached_acls(inode);
1090 err = fuse_do_getattr(inode, stat, file);
1092 generic_fillattr(&init_user_ns, inode, stat);
1093 stat->mode = fi->orig_i_mode;
1094 stat->ino = fi->orig_ino;
1100 int fuse_update_attributes(struct inode *inode, struct file *file)
1102 /* Do *not* need to get atime for internal purposes */
1103 return fuse_update_get_attr(inode, file, NULL,
1104 STATX_BASIC_STATS & ~STATX_ATIME, 0);
1107 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1108 u64 child_nodeid, struct qstr *name)
1111 struct inode *parent;
1113 struct dentry *entry;
1115 parent = fuse_ilookup(fc, parent_nodeid, NULL);
1120 if (!S_ISDIR(parent->i_mode))
1124 dir = d_find_alias(parent);
1128 name->hash = full_name_hash(dir, name->name, name->len);
1129 entry = d_lookup(dir, name);
1134 fuse_dir_changed(parent);
1135 fuse_invalidate_entry(entry);
1137 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1138 inode_lock(d_inode(entry));
1139 if (get_node_id(d_inode(entry)) != child_nodeid) {
1143 if (d_mountpoint(entry)) {
1147 if (d_is_dir(entry)) {
1148 shrink_dcache_parent(entry);
1149 if (!simple_empty(entry)) {
1153 d_inode(entry)->i_flags |= S_DEAD;
1156 clear_nlink(d_inode(entry));
1159 inode_unlock(d_inode(entry));
1168 inode_unlock(parent);
1174 * Calling into a user-controlled filesystem gives the filesystem
1175 * daemon ptrace-like capabilities over the current process. This
1176 * means, that the filesystem daemon is able to record the exact
1177 * filesystem operations performed, and can also control the behavior
1178 * of the requester process in otherwise impossible ways. For example
1179 * it can delay the operation for arbitrary length of time allowing
1180 * DoS against the requester.
1182 * For this reason only those processes can call into the filesystem,
1183 * for which the owner of the mount has ptrace privilege. This
1184 * excludes processes started by other users, suid or sgid processes.
1186 int fuse_allow_current_process(struct fuse_conn *fc)
1188 const struct cred *cred;
1190 if (fc->allow_other)
1191 return current_in_userns(fc->user_ns);
1193 cred = current_cred();
1194 if (uid_eq(cred->euid, fc->user_id) &&
1195 uid_eq(cred->suid, fc->user_id) &&
1196 uid_eq(cred->uid, fc->user_id) &&
1197 gid_eq(cred->egid, fc->group_id) &&
1198 gid_eq(cred->sgid, fc->group_id) &&
1199 gid_eq(cred->gid, fc->group_id))
1205 static int fuse_access(struct inode *inode, int mask)
1207 struct fuse_mount *fm = get_fuse_mount(inode);
1209 struct fuse_access_in inarg;
1212 BUG_ON(mask & MAY_NOT_BLOCK);
1214 if (fm->fc->no_access)
1217 memset(&inarg, 0, sizeof(inarg));
1218 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1219 args.opcode = FUSE_ACCESS;
1220 args.nodeid = get_node_id(inode);
1221 args.in_numargs = 1;
1222 args.in_args[0].size = sizeof(inarg);
1223 args.in_args[0].value = &inarg;
1224 err = fuse_simple_request(fm, &args);
1225 if (err == -ENOSYS) {
1226 fm->fc->no_access = 1;
1232 static int fuse_perm_getattr(struct inode *inode, int mask)
1234 if (mask & MAY_NOT_BLOCK)
1237 forget_all_cached_acls(inode);
1238 return fuse_do_getattr(inode, NULL, NULL);
1242 * Check permission. The two basic access models of FUSE are:
1244 * 1) Local access checking ('default_permissions' mount option) based
1245 * on file mode. This is the plain old disk filesystem permission
1248 * 2) "Remote" access checking, where server is responsible for
1249 * checking permission in each inode operation. An exception to this
1250 * is if ->permission() was invoked from sys_access() in which case an
1251 * access request is sent. Execute permission is still checked
1252 * locally based on file mode.
1254 static int fuse_permission(struct user_namespace *mnt_userns,
1255 struct inode *inode, int mask)
1257 struct fuse_conn *fc = get_fuse_conn(inode);
1258 bool refreshed = false;
1261 if (fuse_is_bad(inode))
1264 if (!fuse_allow_current_process(fc))
1268 * If attributes are needed, refresh them before proceeding
1270 if (fc->default_permissions ||
1271 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1272 struct fuse_inode *fi = get_fuse_inode(inode);
1273 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1275 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1276 time_before64(fi->i_time, get_jiffies_64())) {
1279 err = fuse_perm_getattr(inode, mask);
1285 if (fc->default_permissions) {
1286 err = generic_permission(&init_user_ns, inode, mask);
1288 /* If permission is denied, try to refresh file
1289 attributes. This is also needed, because the root
1290 node will at first have no permissions */
1291 if (err == -EACCES && !refreshed) {
1292 err = fuse_perm_getattr(inode, mask);
1294 err = generic_permission(&init_user_ns,
1298 /* Note: the opposite of the above test does not
1299 exist. So if permissions are revoked this won't be
1300 noticed immediately, only after the attribute
1301 timeout has expired */
1302 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1303 err = fuse_access(inode, mask);
1304 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1305 if (!(inode->i_mode & S_IXUGO)) {
1309 err = fuse_perm_getattr(inode, mask);
1310 if (!err && !(inode->i_mode & S_IXUGO))
1317 static int fuse_readlink_page(struct inode *inode, struct page *page)
1319 struct fuse_mount *fm = get_fuse_mount(inode);
1320 struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1321 struct fuse_args_pages ap = {
1329 ap.args.opcode = FUSE_READLINK;
1330 ap.args.nodeid = get_node_id(inode);
1331 ap.args.out_pages = true;
1332 ap.args.out_argvar = true;
1333 ap.args.page_zeroing = true;
1334 ap.args.out_numargs = 1;
1335 ap.args.out_args[0].size = desc.length;
1336 res = fuse_simple_request(fm, &ap.args);
1338 fuse_invalidate_atime(inode);
1343 if (WARN_ON(res >= PAGE_SIZE))
1346 link = page_address(page);
1352 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1353 struct delayed_call *callback)
1355 struct fuse_conn *fc = get_fuse_conn(inode);
1360 if (fuse_is_bad(inode))
1363 if (fc->cache_symlinks)
1364 return page_get_link(dentry, inode, callback);
1370 page = alloc_page(GFP_KERNEL);
1375 err = fuse_readlink_page(inode, page);
1381 set_delayed_call(callback, page_put_link, page);
1383 return page_address(page);
1386 return ERR_PTR(err);
1389 static int fuse_dir_open(struct inode *inode, struct file *file)
1391 return fuse_open_common(inode, file, true);
1394 static int fuse_dir_release(struct inode *inode, struct file *file)
1396 fuse_release_common(file, true);
1401 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1404 struct inode *inode = file->f_mapping->host;
1405 struct fuse_conn *fc = get_fuse_conn(inode);
1408 if (fuse_is_bad(inode))
1411 if (fc->no_fsyncdir)
1415 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1416 if (err == -ENOSYS) {
1417 fc->no_fsyncdir = 1;
1420 inode_unlock(inode);
1425 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1428 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1430 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1434 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1437 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1440 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1445 return fuse_ioctl_common(file, cmd, arg,
1446 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1449 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1451 /* Always update if mtime is explicitly set */
1452 if (ivalid & ATTR_MTIME_SET)
1455 /* Or if kernel i_mtime is the official one */
1456 if (trust_local_mtime)
1459 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1460 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1463 /* In all other cases update */
1467 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1468 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1470 unsigned ivalid = iattr->ia_valid;
1472 if (ivalid & ATTR_MODE)
1473 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1474 if (ivalid & ATTR_UID)
1475 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1476 if (ivalid & ATTR_GID)
1477 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1478 if (ivalid & ATTR_SIZE)
1479 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1480 if (ivalid & ATTR_ATIME) {
1481 arg->valid |= FATTR_ATIME;
1482 arg->atime = iattr->ia_atime.tv_sec;
1483 arg->atimensec = iattr->ia_atime.tv_nsec;
1484 if (!(ivalid & ATTR_ATIME_SET))
1485 arg->valid |= FATTR_ATIME_NOW;
1487 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1488 arg->valid |= FATTR_MTIME;
1489 arg->mtime = iattr->ia_mtime.tv_sec;
1490 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1491 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1492 arg->valid |= FATTR_MTIME_NOW;
1494 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1495 arg->valid |= FATTR_CTIME;
1496 arg->ctime = iattr->ia_ctime.tv_sec;
1497 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1502 * Prevent concurrent writepages on inode
1504 * This is done by adding a negative bias to the inode write counter
1505 * and waiting for all pending writes to finish.
1507 void fuse_set_nowrite(struct inode *inode)
1509 struct fuse_inode *fi = get_fuse_inode(inode);
1511 BUG_ON(!inode_is_locked(inode));
1513 spin_lock(&fi->lock);
1514 BUG_ON(fi->writectr < 0);
1515 fi->writectr += FUSE_NOWRITE;
1516 spin_unlock(&fi->lock);
1517 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1521 * Allow writepages on inode
1523 * Remove the bias from the writecounter and send any queued
1526 static void __fuse_release_nowrite(struct inode *inode)
1528 struct fuse_inode *fi = get_fuse_inode(inode);
1530 BUG_ON(fi->writectr != FUSE_NOWRITE);
1532 fuse_flush_writepages(inode);
1535 void fuse_release_nowrite(struct inode *inode)
1537 struct fuse_inode *fi = get_fuse_inode(inode);
1539 spin_lock(&fi->lock);
1540 __fuse_release_nowrite(inode);
1541 spin_unlock(&fi->lock);
1544 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1545 struct inode *inode,
1546 struct fuse_setattr_in *inarg_p,
1547 struct fuse_attr_out *outarg_p)
1549 args->opcode = FUSE_SETATTR;
1550 args->nodeid = get_node_id(inode);
1551 args->in_numargs = 1;
1552 args->in_args[0].size = sizeof(*inarg_p);
1553 args->in_args[0].value = inarg_p;
1554 args->out_numargs = 1;
1555 args->out_args[0].size = sizeof(*outarg_p);
1556 args->out_args[0].value = outarg_p;
1560 * Flush inode->i_mtime to the server
1562 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1564 struct fuse_mount *fm = get_fuse_mount(inode);
1566 struct fuse_setattr_in inarg;
1567 struct fuse_attr_out outarg;
1569 memset(&inarg, 0, sizeof(inarg));
1570 memset(&outarg, 0, sizeof(outarg));
1572 inarg.valid = FATTR_MTIME;
1573 inarg.mtime = inode->i_mtime.tv_sec;
1574 inarg.mtimensec = inode->i_mtime.tv_nsec;
1575 if (fm->fc->minor >= 23) {
1576 inarg.valid |= FATTR_CTIME;
1577 inarg.ctime = inode->i_ctime.tv_sec;
1578 inarg.ctimensec = inode->i_ctime.tv_nsec;
1581 inarg.valid |= FATTR_FH;
1584 fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1586 return fuse_simple_request(fm, &args);
1590 * Set attributes, and at the same time refresh them.
1592 * Truncation is slightly complicated, because the 'truncate' request
1593 * may fail, in which case we don't want to touch the mapping.
1594 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1595 * and the actual truncation by hand.
1597 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1600 struct inode *inode = d_inode(dentry);
1601 struct fuse_mount *fm = get_fuse_mount(inode);
1602 struct fuse_conn *fc = fm->fc;
1603 struct fuse_inode *fi = get_fuse_inode(inode);
1605 struct fuse_setattr_in inarg;
1606 struct fuse_attr_out outarg;
1607 bool is_truncate = false;
1608 bool is_wb = fc->writeback_cache;
1611 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1612 bool fault_blocked = false;
1614 if (!fc->default_permissions)
1615 attr->ia_valid |= ATTR_FORCE;
1617 err = setattr_prepare(&init_user_ns, dentry, attr);
1621 if (attr->ia_valid & ATTR_SIZE) {
1622 if (WARN_ON(!S_ISREG(inode->i_mode)))
1627 if (FUSE_IS_DAX(inode) && is_truncate) {
1628 down_write(&fi->i_mmap_sem);
1629 fault_blocked = true;
1630 err = fuse_dax_break_layouts(inode, 0, 0);
1632 up_write(&fi->i_mmap_sem);
1637 if (attr->ia_valid & ATTR_OPEN) {
1638 /* This is coming from open(..., ... | O_TRUNC); */
1639 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1640 WARN_ON(attr->ia_size != 0);
1641 if (fc->atomic_o_trunc) {
1643 * No need to send request to userspace, since actual
1644 * truncation has already been done by OPEN. But still
1645 * need to truncate page cache.
1647 i_size_write(inode, 0);
1648 truncate_pagecache(inode, 0);
1654 /* Flush dirty data/metadata before non-truncate SETATTR */
1655 if (is_wb && S_ISREG(inode->i_mode) &&
1657 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1659 err = write_inode_now(inode, true);
1663 fuse_set_nowrite(inode);
1664 fuse_release_nowrite(inode);
1668 fuse_set_nowrite(inode);
1669 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1670 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1671 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1674 memset(&inarg, 0, sizeof(inarg));
1675 memset(&outarg, 0, sizeof(outarg));
1676 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1678 struct fuse_file *ff = file->private_data;
1679 inarg.valid |= FATTR_FH;
1683 /* Kill suid/sgid for non-directory chown unconditionally */
1684 if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
1685 attr->ia_valid & (ATTR_UID | ATTR_GID))
1686 inarg.valid |= FATTR_KILL_SUIDGID;
1688 if (attr->ia_valid & ATTR_SIZE) {
1689 /* For mandatory locking in truncate */
1690 inarg.valid |= FATTR_LOCKOWNER;
1691 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1693 /* Kill suid/sgid for truncate only if no CAP_FSETID */
1694 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1695 inarg.valid |= FATTR_KILL_SUIDGID;
1697 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1698 err = fuse_simple_request(fm, &args);
1701 fuse_invalidate_attr(inode);
1705 if (fuse_invalid_attr(&outarg.attr) ||
1706 inode_wrong_type(inode, outarg.attr.mode)) {
1707 fuse_make_bad(inode);
1712 spin_lock(&fi->lock);
1713 /* the kernel maintains i_mtime locally */
1714 if (trust_local_cmtime) {
1715 if (attr->ia_valid & ATTR_MTIME)
1716 inode->i_mtime = attr->ia_mtime;
1717 if (attr->ia_valid & ATTR_CTIME)
1718 inode->i_ctime = attr->ia_ctime;
1719 /* FIXME: clear I_DIRTY_SYNC? */
1722 fuse_change_attributes_common(inode, &outarg.attr,
1723 attr_timeout(&outarg));
1724 oldsize = inode->i_size;
1725 /* see the comment in fuse_change_attributes() */
1726 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1727 i_size_write(inode, outarg.attr.size);
1730 /* NOTE: this may release/reacquire fi->lock */
1731 __fuse_release_nowrite(inode);
1733 spin_unlock(&fi->lock);
1736 * Only call invalidate_inode_pages2() after removing
1737 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1739 if ((is_truncate || !is_wb) &&
1740 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1741 truncate_pagecache(inode, outarg.attr.size);
1742 invalidate_inode_pages2(inode->i_mapping);
1745 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1748 up_write(&fi->i_mmap_sem);
1754 fuse_release_nowrite(inode);
1756 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1759 up_write(&fi->i_mmap_sem);
1763 static int fuse_setattr(struct user_namespace *mnt_userns, struct dentry *entry,
1766 struct inode *inode = d_inode(entry);
1767 struct fuse_conn *fc = get_fuse_conn(inode);
1768 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1771 if (fuse_is_bad(inode))
1774 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1777 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1778 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1782 * The only sane way to reliably kill suid/sgid is to do it in
1783 * the userspace filesystem
1785 * This should be done on write(), truncate() and chown().
1787 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
1789 * ia_mode calculation may have used stale i_mode.
1790 * Refresh and recalculate.
1792 ret = fuse_do_getattr(inode, NULL, file);
1796 attr->ia_mode = inode->i_mode;
1797 if (inode->i_mode & S_ISUID) {
1798 attr->ia_valid |= ATTR_MODE;
1799 attr->ia_mode &= ~S_ISUID;
1801 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1802 attr->ia_valid |= ATTR_MODE;
1803 attr->ia_mode &= ~S_ISGID;
1807 if (!attr->ia_valid)
1810 ret = fuse_do_setattr(entry, attr, file);
1813 * If filesystem supports acls it may have updated acl xattrs in
1814 * the filesystem, so forget cached acls for the inode.
1817 forget_all_cached_acls(inode);
1819 /* Directory mode changed, may need to revalidate access */
1820 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1821 fuse_invalidate_entry_cache(entry);
1826 static int fuse_getattr(struct user_namespace *mnt_userns,
1827 const struct path *path, struct kstat *stat,
1828 u32 request_mask, unsigned int flags)
1830 struct inode *inode = d_inode(path->dentry);
1831 struct fuse_conn *fc = get_fuse_conn(inode);
1833 if (fuse_is_bad(inode))
1836 if (!fuse_allow_current_process(fc)) {
1837 if (!request_mask) {
1839 * If user explicitly requested *nothing* then don't
1840 * error out, but return st_dev only.
1842 stat->result_mask = 0;
1843 stat->dev = inode->i_sb->s_dev;
1849 return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1852 static const struct inode_operations fuse_dir_inode_operations = {
1853 .lookup = fuse_lookup,
1854 .mkdir = fuse_mkdir,
1855 .symlink = fuse_symlink,
1856 .unlink = fuse_unlink,
1857 .rmdir = fuse_rmdir,
1858 .rename = fuse_rename2,
1860 .setattr = fuse_setattr,
1861 .create = fuse_create,
1862 .atomic_open = fuse_atomic_open,
1863 .mknod = fuse_mknod,
1864 .permission = fuse_permission,
1865 .getattr = fuse_getattr,
1866 .listxattr = fuse_listxattr,
1867 .get_acl = fuse_get_acl,
1868 .set_acl = fuse_set_acl,
1869 .fileattr_get = fuse_fileattr_get,
1870 .fileattr_set = fuse_fileattr_set,
1873 static const struct file_operations fuse_dir_operations = {
1874 .llseek = generic_file_llseek,
1875 .read = generic_read_dir,
1876 .iterate_shared = fuse_readdir,
1877 .open = fuse_dir_open,
1878 .release = fuse_dir_release,
1879 .fsync = fuse_dir_fsync,
1880 .unlocked_ioctl = fuse_dir_ioctl,
1881 .compat_ioctl = fuse_dir_compat_ioctl,
1884 static const struct inode_operations fuse_common_inode_operations = {
1885 .setattr = fuse_setattr,
1886 .permission = fuse_permission,
1887 .getattr = fuse_getattr,
1888 .listxattr = fuse_listxattr,
1889 .get_acl = fuse_get_acl,
1890 .set_acl = fuse_set_acl,
1891 .fileattr_get = fuse_fileattr_get,
1892 .fileattr_set = fuse_fileattr_set,
1895 static const struct inode_operations fuse_symlink_inode_operations = {
1896 .setattr = fuse_setattr,
1897 .get_link = fuse_get_link,
1898 .getattr = fuse_getattr,
1899 .listxattr = fuse_listxattr,
1902 void fuse_init_common(struct inode *inode)
1904 inode->i_op = &fuse_common_inode_operations;
1907 void fuse_init_dir(struct inode *inode)
1909 struct fuse_inode *fi = get_fuse_inode(inode);
1911 inode->i_op = &fuse_dir_inode_operations;
1912 inode->i_fop = &fuse_dir_operations;
1914 spin_lock_init(&fi->rdc.lock);
1915 fi->rdc.cached = false;
1918 fi->rdc.version = 0;
1921 static int fuse_symlink_readpage(struct file *null, struct page *page)
1923 int err = fuse_readlink_page(page->mapping->host, page);
1926 SetPageUptodate(page);
1933 static const struct address_space_operations fuse_symlink_aops = {
1934 .readpage = fuse_symlink_readpage,
1937 void fuse_init_symlink(struct inode *inode)
1939 inode->i_op = &fuse_symlink_inode_operations;
1940 inode->i_data.a_ops = &fuse_symlink_aops;
1941 inode_nohighmem(inode);