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/moduleparam.h>
15 #include <linux/sched.h>
16 #include <linux/namei.h>
17 #include <linux/slab.h>
18 #include <linux/xattr.h>
19 #include <linux/iversion.h>
20 #include <linux/posix_acl.h>
21 #include <linux/security.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
25 static bool __read_mostly allow_sys_admin_access;
26 module_param(allow_sys_admin_access, bool, 0644);
27 MODULE_PARM_DESC(allow_sys_admin_access,
28 "Allow users with CAP_SYS_ADMIN in initial userns to bypass allow_other access check");
30 static void fuse_advise_use_readdirplus(struct inode *dir)
32 struct fuse_inode *fi = get_fuse_inode(dir);
34 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
37 #if BITS_PER_LONG >= 64
38 static inline void __fuse_dentry_settime(struct dentry *entry, u64 time)
40 entry->d_fsdata = (void *) time;
43 static inline u64 fuse_dentry_time(const struct dentry *entry)
45 return (u64)entry->d_fsdata;
54 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
56 ((union fuse_dentry *) dentry->d_fsdata)->time = time;
59 static inline u64 fuse_dentry_time(const struct dentry *entry)
61 return ((union fuse_dentry *) entry->d_fsdata)->time;
65 static void fuse_dentry_settime(struct dentry *dentry, u64 time)
67 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
68 bool delete = !time && fc->delete_stale;
70 * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
71 * Don't care about races, either way it's just an optimization
73 if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
74 (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
75 spin_lock(&dentry->d_lock);
77 dentry->d_flags &= ~DCACHE_OP_DELETE;
79 dentry->d_flags |= DCACHE_OP_DELETE;
80 spin_unlock(&dentry->d_lock);
83 __fuse_dentry_settime(dentry, time);
87 * FUSE caches dentries and attributes with separate timeout. The
88 * time in jiffies until the dentry/attributes are valid is stored in
89 * dentry->d_fsdata and fuse_inode->i_time respectively.
93 * Calculate the time in jiffies until a dentry/attributes are valid
95 static u64 time_to_jiffies(u64 sec, u32 nsec)
98 struct timespec64 ts = {
100 min_t(u32, nsec, NSEC_PER_SEC - 1)
103 return get_jiffies_64() + timespec64_to_jiffies(&ts);
109 * Set dentry and possibly attribute timeouts from the lookup/mk*
112 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
114 fuse_dentry_settime(entry,
115 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
118 static u64 attr_timeout(struct fuse_attr_out *o)
120 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
123 u64 entry_attr_timeout(struct fuse_entry_out *o)
125 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
128 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
130 set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
134 * Mark the attributes as stale, so that at the next call to
135 * ->getattr() they will be fetched from userspace
137 void fuse_invalidate_attr(struct inode *inode)
139 fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
142 static void fuse_dir_changed(struct inode *dir)
144 fuse_invalidate_attr(dir);
145 inode_maybe_inc_iversion(dir, false);
149 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
152 void fuse_invalidate_atime(struct inode *inode)
154 if (!IS_RDONLY(inode))
155 fuse_invalidate_attr_mask(inode, STATX_ATIME);
159 * Just mark the entry as stale, so that a next attempt to look it up
160 * will result in a new lookup call to userspace
162 * This is called when a dentry is about to become negative and the
163 * timeout is unknown (unlink, rmdir, rename and in some cases
166 void fuse_invalidate_entry_cache(struct dentry *entry)
168 fuse_dentry_settime(entry, 0);
172 * Same as fuse_invalidate_entry_cache(), but also try to remove the
173 * dentry from the hash
175 static void fuse_invalidate_entry(struct dentry *entry)
178 fuse_invalidate_entry_cache(entry);
181 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
182 u64 nodeid, const struct qstr *name,
183 struct fuse_entry_out *outarg)
185 memset(outarg, 0, sizeof(struct fuse_entry_out));
186 args->opcode = FUSE_LOOKUP;
187 args->nodeid = nodeid;
188 args->in_numargs = 1;
189 args->in_args[0].size = name->len + 1;
190 args->in_args[0].value = name->name;
191 args->out_numargs = 1;
192 args->out_args[0].size = sizeof(struct fuse_entry_out);
193 args->out_args[0].value = outarg;
197 * Check whether the dentry is still valid
199 * If the entry validity timeout has expired and the dentry is
200 * positive, try to redo the lookup. If the lookup results in a
201 * different inode, then let the VFS invalidate the dentry and redo
202 * the lookup once more. If the lookup results in the same inode,
203 * then refresh the attributes, timeouts and mark the dentry valid.
205 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
208 struct dentry *parent;
209 struct fuse_mount *fm;
210 struct fuse_inode *fi;
213 inode = d_inode_rcu(entry);
214 if (inode && fuse_is_bad(inode))
216 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
217 (flags & (LOOKUP_EXCL | LOOKUP_REVAL | LOOKUP_RENAME_TARGET))) {
218 struct fuse_entry_out outarg;
220 struct fuse_forget_link *forget;
223 /* For negative dentries, always do a fresh lookup */
228 if (flags & LOOKUP_RCU)
231 fm = get_fuse_mount(inode);
233 forget = fuse_alloc_forget();
238 attr_version = fuse_get_attr_version(fm->fc);
240 parent = dget_parent(entry);
241 fuse_lookup_init(fm->fc, &args, get_node_id(d_inode(parent)),
242 &entry->d_name, &outarg);
243 ret = fuse_simple_request(fm, &args);
245 /* Zero nodeid is same as -ENOENT */
246 if (!ret && !outarg.nodeid)
249 fi = get_fuse_inode(inode);
250 if (outarg.nodeid != get_node_id(inode) ||
251 (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) {
252 fuse_queue_forget(fm->fc, forget,
256 spin_lock(&fi->lock);
258 spin_unlock(&fi->lock);
261 if (ret == -ENOMEM || ret == -EINTR)
263 if (ret || fuse_invalid_attr(&outarg.attr) ||
264 fuse_stale_inode(inode, outarg.generation, &outarg.attr))
267 forget_all_cached_acls(inode);
268 fuse_change_attributes(inode, &outarg.attr,
269 entry_attr_timeout(&outarg),
271 fuse_change_entry_timeout(entry, &outarg);
273 fi = get_fuse_inode(inode);
274 if (flags & LOOKUP_RCU) {
275 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
277 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
278 parent = dget_parent(entry);
279 fuse_advise_use_readdirplus(d_inode(parent));
292 #if BITS_PER_LONG < 64
293 static int fuse_dentry_init(struct dentry *dentry)
295 dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry),
296 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
298 return dentry->d_fsdata ? 0 : -ENOMEM;
300 static void fuse_dentry_release(struct dentry *dentry)
302 union fuse_dentry *fd = dentry->d_fsdata;
308 static int fuse_dentry_delete(const struct dentry *dentry)
310 return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
314 * Create a fuse_mount object with a new superblock (with path->dentry
315 * as the root), and return that mount so it can be auto-mounted on
318 static struct vfsmount *fuse_dentry_automount(struct path *path)
320 struct fs_context *fsc;
321 struct vfsmount *mnt;
322 struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
324 fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
326 return ERR_CAST(fsc);
328 /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */
329 fsc->fs_private = mp_fi;
331 /* Create the submount */
340 const struct dentry_operations fuse_dentry_operations = {
341 .d_revalidate = fuse_dentry_revalidate,
342 .d_delete = fuse_dentry_delete,
343 #if BITS_PER_LONG < 64
344 .d_init = fuse_dentry_init,
345 .d_release = fuse_dentry_release,
347 .d_automount = fuse_dentry_automount,
350 const struct dentry_operations fuse_root_dentry_operations = {
351 #if BITS_PER_LONG < 64
352 .d_init = fuse_dentry_init,
353 .d_release = fuse_dentry_release,
357 int fuse_valid_type(int m)
359 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
360 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
363 bool fuse_invalid_attr(struct fuse_attr *attr)
365 return !fuse_valid_type(attr->mode) ||
366 attr->size > LLONG_MAX;
369 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
370 struct fuse_entry_out *outarg, struct inode **inode)
372 struct fuse_mount *fm = get_fuse_mount_super(sb);
374 struct fuse_forget_link *forget;
380 if (name->len > FUSE_NAME_MAX)
384 forget = fuse_alloc_forget();
389 attr_version = fuse_get_attr_version(fm->fc);
391 fuse_lookup_init(fm->fc, &args, nodeid, name, outarg);
392 err = fuse_simple_request(fm, &args);
393 /* Zero nodeid is same as -ENOENT, but with valid timeout */
394 if (err || !outarg->nodeid)
398 if (fuse_invalid_attr(&outarg->attr))
401 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
402 &outarg->attr, entry_attr_timeout(outarg),
406 fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1);
417 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
421 struct fuse_entry_out outarg;
423 struct dentry *newent;
424 bool outarg_valid = true;
427 if (fuse_is_bad(dir))
428 return ERR_PTR(-EIO);
430 locked = fuse_lock_inode(dir);
431 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
433 fuse_unlock_inode(dir, locked);
434 if (err == -ENOENT) {
435 outarg_valid = false;
442 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
445 newent = d_splice_alias(inode, entry);
446 err = PTR_ERR(newent);
450 entry = newent ? newent : entry;
452 fuse_change_entry_timeout(entry, &outarg);
454 fuse_invalidate_entry_cache(entry);
457 fuse_advise_use_readdirplus(dir);
466 static int get_security_context(struct dentry *entry, umode_t mode,
467 struct fuse_in_arg *ext)
469 struct fuse_secctx *fctx;
470 struct fuse_secctx_header *header;
471 void *ctx = NULL, *ptr;
472 u32 ctxlen, total_len = sizeof(*header);
477 err = security_dentry_init_security(entry, mode, &entry->d_name,
478 &name, &ctx, &ctxlen);
480 if (err != -EOPNOTSUPP)
482 /* No LSM is supporting this security hook. Ignore error */
489 namelen = strlen(name) + 1;
491 if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || ctxlen > S32_MAX))
493 total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + ctxlen);
497 header = ptr = kzalloc(total_len, GFP_KERNEL);
501 header->nr_secctx = nr_ctx;
502 header->size = total_len;
503 ptr += sizeof(*header);
507 ptr += sizeof(*fctx);
512 memcpy(ptr, ctx, ctxlen);
514 ext->size = total_len;
522 static void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
525 u32 newlen = buf->size + bytes;
527 p = krealloc(buf->value, newlen, GFP_KERNEL);
535 memset(p + buf->size, 0, bytes);
539 return p + newlen - bytes;
542 static u32 fuse_ext_size(size_t size)
544 return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size);
548 * This adds just a single supplementary group that matches the parent's group.
550 static int get_create_supp_group(struct inode *dir, struct fuse_in_arg *ext)
552 struct fuse_conn *fc = get_fuse_conn(dir);
553 struct fuse_ext_header *xh;
554 struct fuse_supp_groups *sg;
555 kgid_t kgid = dir->i_gid;
556 gid_t parent_gid = from_kgid(fc->user_ns, kgid);
557 u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0]));
559 if (parent_gid == (gid_t) -1 || gid_eq(kgid, current_fsgid()) ||
563 xh = extend_arg(ext, sg_len);
568 xh->type = FUSE_EXT_GROUPS;
570 sg = (struct fuse_supp_groups *) &xh[1];
572 sg->groups[0] = parent_gid;
577 static int get_create_ext(struct fuse_args *args,
578 struct inode *dir, struct dentry *dentry,
581 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
582 struct fuse_in_arg ext = { .size = 0, .value = NULL };
585 if (fc->init_security)
586 err = get_security_context(dentry, mode, &ext);
587 if (!err && fc->create_supp_group)
588 err = get_create_supp_group(dir, &ext);
590 if (!err && ext.size) {
591 WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args));
593 args->ext_idx = args->in_numargs++;
594 args->in_args[args->ext_idx] = ext;
602 static void free_ext_value(struct fuse_args *args)
605 kfree(args->in_args[args->ext_idx].value);
609 * Atomic create+open operation
611 * If the filesystem doesn't support this, then fall back to separate
612 * 'mknod' + 'open' requests.
614 static int fuse_create_open(struct inode *dir, struct dentry *entry,
615 struct file *file, unsigned int flags,
616 umode_t mode, u32 opcode)
620 struct fuse_mount *fm = get_fuse_mount(dir);
622 struct fuse_forget_link *forget;
623 struct fuse_create_in inarg;
624 struct fuse_open_out outopen;
625 struct fuse_entry_out outentry;
626 struct fuse_inode *fi;
627 struct fuse_file *ff;
628 bool trunc = flags & O_TRUNC;
630 /* Userspace expects S_IFREG in create mode */
631 BUG_ON((mode & S_IFMT) != S_IFREG);
633 forget = fuse_alloc_forget();
639 ff = fuse_file_alloc(fm);
641 goto out_put_forget_req;
643 if (!fm->fc->dont_mask)
644 mode &= ~current_umask();
647 memset(&inarg, 0, sizeof(inarg));
648 memset(&outentry, 0, sizeof(outentry));
651 inarg.umask = current_umask();
653 if (fm->fc->handle_killpriv_v2 && trunc &&
654 !(flags & O_EXCL) && !capable(CAP_FSETID)) {
655 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
658 args.opcode = opcode;
659 args.nodeid = get_node_id(dir);
661 args.in_args[0].size = sizeof(inarg);
662 args.in_args[0].value = &inarg;
663 args.in_args[1].size = entry->d_name.len + 1;
664 args.in_args[1].value = entry->d_name.name;
665 args.out_numargs = 2;
666 args.out_args[0].size = sizeof(outentry);
667 args.out_args[0].value = &outentry;
668 args.out_args[1].size = sizeof(outopen);
669 args.out_args[1].value = &outopen;
671 err = get_create_ext(&args, dir, entry, mode);
673 goto out_put_forget_req;
675 err = fuse_simple_request(fm, &args);
676 free_ext_value(&args);
681 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
682 fuse_invalid_attr(&outentry.attr))
686 ff->nodeid = outentry.nodeid;
687 ff->open_flags = outopen.open_flags;
688 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
689 &outentry.attr, entry_attr_timeout(&outentry), 0);
691 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
692 fuse_sync_release(NULL, ff, flags);
693 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
698 d_instantiate(entry, inode);
699 fuse_change_entry_timeout(entry, &outentry);
700 fuse_dir_changed(dir);
701 err = finish_open(file, entry, generic_file_open);
703 fi = get_fuse_inode(inode);
704 fuse_sync_release(fi, ff, flags);
706 file->private_data = ff;
707 fuse_finish_open(inode, file);
708 if (fm->fc->atomic_o_trunc && trunc)
709 truncate_pagecache(inode, 0);
710 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
711 invalidate_inode_pages2(inode->i_mapping);
723 static int fuse_mknod(struct mnt_idmap *, struct inode *, struct dentry *,
725 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
726 struct file *file, unsigned flags,
730 struct fuse_conn *fc = get_fuse_conn(dir);
731 struct dentry *res = NULL;
733 if (fuse_is_bad(dir))
736 if (d_in_lookup(entry)) {
737 res = fuse_lookup(dir, entry, 0);
745 if (!(flags & O_CREAT) || d_really_is_positive(entry))
749 file->f_mode |= FMODE_CREATED;
754 err = fuse_create_open(dir, entry, file, flags, mode, FUSE_CREATE);
755 if (err == -ENOSYS) {
764 err = fuse_mknod(&nop_mnt_idmap, dir, entry, mode, 0);
768 return finish_no_open(file, res);
772 * Code shared between mknod, mkdir, symlink and link
774 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
775 struct inode *dir, struct dentry *entry,
778 struct fuse_entry_out outarg;
782 struct fuse_forget_link *forget;
784 if (fuse_is_bad(dir))
787 forget = fuse_alloc_forget();
791 memset(&outarg, 0, sizeof(outarg));
792 args->nodeid = get_node_id(dir);
793 args->out_numargs = 1;
794 args->out_args[0].size = sizeof(outarg);
795 args->out_args[0].value = &outarg;
797 if (args->opcode != FUSE_LINK) {
798 err = get_create_ext(args, dir, entry, mode);
800 goto out_put_forget_req;
803 err = fuse_simple_request(fm, args);
804 free_ext_value(args);
806 goto out_put_forget_req;
809 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
810 goto out_put_forget_req;
812 if ((outarg.attr.mode ^ mode) & S_IFMT)
813 goto out_put_forget_req;
815 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
816 &outarg.attr, entry_attr_timeout(&outarg), 0);
818 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
824 d = d_splice_alias(inode, entry);
829 fuse_change_entry_timeout(d, &outarg);
832 fuse_change_entry_timeout(entry, &outarg);
834 fuse_dir_changed(dir);
842 static int fuse_mknod(struct mnt_idmap *idmap, struct inode *dir,
843 struct dentry *entry, umode_t mode, dev_t rdev)
845 struct fuse_mknod_in inarg;
846 struct fuse_mount *fm = get_fuse_mount(dir);
849 if (!fm->fc->dont_mask)
850 mode &= ~current_umask();
852 memset(&inarg, 0, sizeof(inarg));
854 inarg.rdev = new_encode_dev(rdev);
855 inarg.umask = current_umask();
856 args.opcode = FUSE_MKNOD;
858 args.in_args[0].size = sizeof(inarg);
859 args.in_args[0].value = &inarg;
860 args.in_args[1].size = entry->d_name.len + 1;
861 args.in_args[1].value = entry->d_name.name;
862 return create_new_entry(fm, &args, dir, entry, mode);
865 static int fuse_create(struct mnt_idmap *idmap, struct inode *dir,
866 struct dentry *entry, umode_t mode, bool excl)
868 return fuse_mknod(&nop_mnt_idmap, dir, entry, mode, 0);
871 static int fuse_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
872 struct file *file, umode_t mode)
874 struct fuse_conn *fc = get_fuse_conn(dir);
880 err = fuse_create_open(dir, file->f_path.dentry, file, file->f_flags, mode, FUSE_TMPFILE);
881 if (err == -ENOSYS) {
888 static int fuse_mkdir(struct mnt_idmap *idmap, struct inode *dir,
889 struct dentry *entry, umode_t mode)
891 struct fuse_mkdir_in inarg;
892 struct fuse_mount *fm = get_fuse_mount(dir);
895 if (!fm->fc->dont_mask)
896 mode &= ~current_umask();
898 memset(&inarg, 0, sizeof(inarg));
900 inarg.umask = current_umask();
901 args.opcode = FUSE_MKDIR;
903 args.in_args[0].size = sizeof(inarg);
904 args.in_args[0].value = &inarg;
905 args.in_args[1].size = entry->d_name.len + 1;
906 args.in_args[1].value = entry->d_name.name;
907 return create_new_entry(fm, &args, dir, entry, S_IFDIR);
910 static int fuse_symlink(struct mnt_idmap *idmap, struct inode *dir,
911 struct dentry *entry, const char *link)
913 struct fuse_mount *fm = get_fuse_mount(dir);
914 unsigned len = strlen(link) + 1;
917 args.opcode = FUSE_SYMLINK;
919 args.in_args[0].size = entry->d_name.len + 1;
920 args.in_args[0].value = entry->d_name.name;
921 args.in_args[1].size = len;
922 args.in_args[1].value = link;
923 return create_new_entry(fm, &args, dir, entry, S_IFLNK);
926 void fuse_flush_time_update(struct inode *inode)
928 int err = sync_inode_metadata(inode, 1);
930 mapping_set_error(inode->i_mapping, err);
933 static void fuse_update_ctime_in_cache(struct inode *inode)
935 if (!IS_NOCMTIME(inode)) {
936 inode->i_ctime = current_time(inode);
937 mark_inode_dirty_sync(inode);
938 fuse_flush_time_update(inode);
942 void fuse_update_ctime(struct inode *inode)
944 fuse_invalidate_attr_mask(inode, STATX_CTIME);
945 fuse_update_ctime_in_cache(inode);
948 static void fuse_entry_unlinked(struct dentry *entry)
950 struct inode *inode = d_inode(entry);
951 struct fuse_conn *fc = get_fuse_conn(inode);
952 struct fuse_inode *fi = get_fuse_inode(inode);
954 spin_lock(&fi->lock);
955 fi->attr_version = atomic64_inc_return(&fc->attr_version);
957 * If i_nlink == 0 then unlink doesn't make sense, yet this can
958 * happen if userspace filesystem is careless. It would be
959 * difficult to enforce correct nlink usage so just ignore this
962 if (S_ISDIR(inode->i_mode))
964 else if (inode->i_nlink > 0)
966 spin_unlock(&fi->lock);
967 fuse_invalidate_entry_cache(entry);
968 fuse_update_ctime(inode);
971 static int fuse_unlink(struct inode *dir, struct dentry *entry)
974 struct fuse_mount *fm = get_fuse_mount(dir);
977 if (fuse_is_bad(dir))
980 args.opcode = FUSE_UNLINK;
981 args.nodeid = get_node_id(dir);
983 args.in_args[0].size = entry->d_name.len + 1;
984 args.in_args[0].value = entry->d_name.name;
985 err = fuse_simple_request(fm, &args);
987 fuse_dir_changed(dir);
988 fuse_entry_unlinked(entry);
989 } else if (err == -EINTR)
990 fuse_invalidate_entry(entry);
994 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
997 struct fuse_mount *fm = get_fuse_mount(dir);
1000 if (fuse_is_bad(dir))
1003 args.opcode = FUSE_RMDIR;
1004 args.nodeid = get_node_id(dir);
1005 args.in_numargs = 1;
1006 args.in_args[0].size = entry->d_name.len + 1;
1007 args.in_args[0].value = entry->d_name.name;
1008 err = fuse_simple_request(fm, &args);
1010 fuse_dir_changed(dir);
1011 fuse_entry_unlinked(entry);
1012 } else if (err == -EINTR)
1013 fuse_invalidate_entry(entry);
1017 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
1018 struct inode *newdir, struct dentry *newent,
1019 unsigned int flags, int opcode, size_t argsize)
1022 struct fuse_rename2_in inarg;
1023 struct fuse_mount *fm = get_fuse_mount(olddir);
1026 memset(&inarg, 0, argsize);
1027 inarg.newdir = get_node_id(newdir);
1028 inarg.flags = flags;
1029 args.opcode = opcode;
1030 args.nodeid = get_node_id(olddir);
1031 args.in_numargs = 3;
1032 args.in_args[0].size = argsize;
1033 args.in_args[0].value = &inarg;
1034 args.in_args[1].size = oldent->d_name.len + 1;
1035 args.in_args[1].value = oldent->d_name.name;
1036 args.in_args[2].size = newent->d_name.len + 1;
1037 args.in_args[2].value = newent->d_name.name;
1038 err = fuse_simple_request(fm, &args);
1041 fuse_update_ctime(d_inode(oldent));
1043 if (flags & RENAME_EXCHANGE)
1044 fuse_update_ctime(d_inode(newent));
1046 fuse_dir_changed(olddir);
1047 if (olddir != newdir)
1048 fuse_dir_changed(newdir);
1050 /* newent will end up negative */
1051 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent))
1052 fuse_entry_unlinked(newent);
1053 } else if (err == -EINTR) {
1054 /* If request was interrupted, DEITY only knows if the
1055 rename actually took place. If the invalidation
1056 fails (e.g. some process has CWD under the renamed
1057 directory), then there can be inconsistency between
1058 the dcache and the real filesystem. Tough luck. */
1059 fuse_invalidate_entry(oldent);
1060 if (d_really_is_positive(newent))
1061 fuse_invalidate_entry(newent);
1067 static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir,
1068 struct dentry *oldent, struct inode *newdir,
1069 struct dentry *newent, unsigned int flags)
1071 struct fuse_conn *fc = get_fuse_conn(olddir);
1074 if (fuse_is_bad(olddir))
1077 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1081 if (fc->no_rename2 || fc->minor < 23)
1084 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
1086 sizeof(struct fuse_rename2_in));
1087 if (err == -ENOSYS) {
1092 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
1094 sizeof(struct fuse_rename_in));
1100 static int fuse_link(struct dentry *entry, struct inode *newdir,
1101 struct dentry *newent)
1104 struct fuse_link_in inarg;
1105 struct inode *inode = d_inode(entry);
1106 struct fuse_mount *fm = get_fuse_mount(inode);
1109 memset(&inarg, 0, sizeof(inarg));
1110 inarg.oldnodeid = get_node_id(inode);
1111 args.opcode = FUSE_LINK;
1112 args.in_numargs = 2;
1113 args.in_args[0].size = sizeof(inarg);
1114 args.in_args[0].value = &inarg;
1115 args.in_args[1].size = newent->d_name.len + 1;
1116 args.in_args[1].value = newent->d_name.name;
1117 err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
1119 fuse_update_ctime_in_cache(inode);
1120 else if (err == -EINTR)
1121 fuse_invalidate_attr(inode);
1126 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1129 unsigned int blkbits;
1130 struct fuse_conn *fc = get_fuse_conn(inode);
1132 stat->dev = inode->i_sb->s_dev;
1133 stat->ino = attr->ino;
1134 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1135 stat->nlink = attr->nlink;
1136 stat->uid = make_kuid(fc->user_ns, attr->uid);
1137 stat->gid = make_kgid(fc->user_ns, attr->gid);
1138 stat->rdev = inode->i_rdev;
1139 stat->atime.tv_sec = attr->atime;
1140 stat->atime.tv_nsec = attr->atimensec;
1141 stat->mtime.tv_sec = attr->mtime;
1142 stat->mtime.tv_nsec = attr->mtimensec;
1143 stat->ctime.tv_sec = attr->ctime;
1144 stat->ctime.tv_nsec = attr->ctimensec;
1145 stat->size = attr->size;
1146 stat->blocks = attr->blocks;
1148 if (attr->blksize != 0)
1149 blkbits = ilog2(attr->blksize);
1151 blkbits = inode->i_sb->s_blocksize_bits;
1153 stat->blksize = 1 << blkbits;
1156 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
1160 struct fuse_getattr_in inarg;
1161 struct fuse_attr_out outarg;
1162 struct fuse_mount *fm = get_fuse_mount(inode);
1166 attr_version = fuse_get_attr_version(fm->fc);
1168 memset(&inarg, 0, sizeof(inarg));
1169 memset(&outarg, 0, sizeof(outarg));
1170 /* Directories have separate file-handle space */
1171 if (file && S_ISREG(inode->i_mode)) {
1172 struct fuse_file *ff = file->private_data;
1174 inarg.getattr_flags |= FUSE_GETATTR_FH;
1177 args.opcode = FUSE_GETATTR;
1178 args.nodeid = get_node_id(inode);
1179 args.in_numargs = 1;
1180 args.in_args[0].size = sizeof(inarg);
1181 args.in_args[0].value = &inarg;
1182 args.out_numargs = 1;
1183 args.out_args[0].size = sizeof(outarg);
1184 args.out_args[0].value = &outarg;
1185 err = fuse_simple_request(fm, &args);
1187 if (fuse_invalid_attr(&outarg.attr) ||
1188 inode_wrong_type(inode, outarg.attr.mode)) {
1189 fuse_make_bad(inode);
1192 fuse_change_attributes(inode, &outarg.attr,
1193 attr_timeout(&outarg),
1196 fuse_fillattr(inode, &outarg.attr, stat);
1202 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1203 struct kstat *stat, u32 request_mask,
1206 struct fuse_inode *fi = get_fuse_inode(inode);
1209 u32 inval_mask = READ_ONCE(fi->inval_mask);
1210 u32 cache_mask = fuse_get_cache_mask(inode);
1212 if (flags & AT_STATX_FORCE_SYNC)
1214 else if (flags & AT_STATX_DONT_SYNC)
1216 else if (request_mask & inval_mask & ~cache_mask)
1219 sync = time_before64(fi->i_time, get_jiffies_64());
1222 forget_all_cached_acls(inode);
1223 err = fuse_do_getattr(inode, stat, file);
1225 generic_fillattr(&nop_mnt_idmap, inode, stat);
1226 stat->mode = fi->orig_i_mode;
1227 stat->ino = fi->orig_ino;
1233 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask)
1235 return fuse_update_get_attr(inode, file, NULL, mask, 0);
1238 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1239 u64 child_nodeid, struct qstr *name, u32 flags)
1242 struct inode *parent;
1244 struct dentry *entry;
1246 parent = fuse_ilookup(fc, parent_nodeid, NULL);
1250 inode_lock_nested(parent, I_MUTEX_PARENT);
1251 if (!S_ISDIR(parent->i_mode))
1255 dir = d_find_alias(parent);
1259 name->hash = full_name_hash(dir, name->name, name->len);
1260 entry = d_lookup(dir, name);
1265 fuse_dir_changed(parent);
1266 if (!(flags & FUSE_EXPIRE_ONLY))
1267 d_invalidate(entry);
1268 fuse_invalidate_entry_cache(entry);
1270 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1271 inode_lock(d_inode(entry));
1272 if (get_node_id(d_inode(entry)) != child_nodeid) {
1276 if (d_mountpoint(entry)) {
1280 if (d_is_dir(entry)) {
1281 shrink_dcache_parent(entry);
1282 if (!simple_empty(entry)) {
1286 d_inode(entry)->i_flags |= S_DEAD;
1289 clear_nlink(d_inode(entry));
1292 inode_unlock(d_inode(entry));
1301 inode_unlock(parent);
1306 static inline bool fuse_permissible_uidgid(struct fuse_conn *fc)
1308 const struct cred *cred = current_cred();
1310 return (uid_eq(cred->euid, fc->user_id) &&
1311 uid_eq(cred->suid, fc->user_id) &&
1312 uid_eq(cred->uid, fc->user_id) &&
1313 gid_eq(cred->egid, fc->group_id) &&
1314 gid_eq(cred->sgid, fc->group_id) &&
1315 gid_eq(cred->gid, fc->group_id));
1319 * Calling into a user-controlled filesystem gives the filesystem
1320 * daemon ptrace-like capabilities over the current process. This
1321 * means, that the filesystem daemon is able to record the exact
1322 * filesystem operations performed, and can also control the behavior
1323 * of the requester process in otherwise impossible ways. For example
1324 * it can delay the operation for arbitrary length of time allowing
1325 * DoS against the requester.
1327 * For this reason only those processes can call into the filesystem,
1328 * for which the owner of the mount has ptrace privilege. This
1329 * excludes processes started by other users, suid or sgid processes.
1331 bool fuse_allow_current_process(struct fuse_conn *fc)
1335 if (fc->allow_other)
1336 allow = current_in_userns(fc->user_ns);
1338 allow = fuse_permissible_uidgid(fc);
1340 if (!allow && allow_sys_admin_access && capable(CAP_SYS_ADMIN))
1346 static int fuse_access(struct inode *inode, int mask)
1348 struct fuse_mount *fm = get_fuse_mount(inode);
1350 struct fuse_access_in inarg;
1353 BUG_ON(mask & MAY_NOT_BLOCK);
1355 if (fm->fc->no_access)
1358 memset(&inarg, 0, sizeof(inarg));
1359 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1360 args.opcode = FUSE_ACCESS;
1361 args.nodeid = get_node_id(inode);
1362 args.in_numargs = 1;
1363 args.in_args[0].size = sizeof(inarg);
1364 args.in_args[0].value = &inarg;
1365 err = fuse_simple_request(fm, &args);
1366 if (err == -ENOSYS) {
1367 fm->fc->no_access = 1;
1373 static int fuse_perm_getattr(struct inode *inode, int mask)
1375 if (mask & MAY_NOT_BLOCK)
1378 forget_all_cached_acls(inode);
1379 return fuse_do_getattr(inode, NULL, NULL);
1383 * Check permission. The two basic access models of FUSE are:
1385 * 1) Local access checking ('default_permissions' mount option) based
1386 * on file mode. This is the plain old disk filesystem permission
1389 * 2) "Remote" access checking, where server is responsible for
1390 * checking permission in each inode operation. An exception to this
1391 * is if ->permission() was invoked from sys_access() in which case an
1392 * access request is sent. Execute permission is still checked
1393 * locally based on file mode.
1395 static int fuse_permission(struct mnt_idmap *idmap,
1396 struct inode *inode, int mask)
1398 struct fuse_conn *fc = get_fuse_conn(inode);
1399 bool refreshed = false;
1402 if (fuse_is_bad(inode))
1405 if (!fuse_allow_current_process(fc))
1409 * If attributes are needed, refresh them before proceeding
1411 if (fc->default_permissions ||
1412 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1413 struct fuse_inode *fi = get_fuse_inode(inode);
1414 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1416 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1417 time_before64(fi->i_time, get_jiffies_64())) {
1420 err = fuse_perm_getattr(inode, mask);
1426 if (fc->default_permissions) {
1427 err = generic_permission(&nop_mnt_idmap, inode, mask);
1429 /* If permission is denied, try to refresh file
1430 attributes. This is also needed, because the root
1431 node will at first have no permissions */
1432 if (err == -EACCES && !refreshed) {
1433 err = fuse_perm_getattr(inode, mask);
1435 err = generic_permission(&nop_mnt_idmap,
1439 /* Note: the opposite of the above test does not
1440 exist. So if permissions are revoked this won't be
1441 noticed immediately, only after the attribute
1442 timeout has expired */
1443 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1444 err = fuse_access(inode, mask);
1445 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1446 if (!(inode->i_mode & S_IXUGO)) {
1450 err = fuse_perm_getattr(inode, mask);
1451 if (!err && !(inode->i_mode & S_IXUGO))
1458 static int fuse_readlink_page(struct inode *inode, struct page *page)
1460 struct fuse_mount *fm = get_fuse_mount(inode);
1461 struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1462 struct fuse_args_pages ap = {
1470 ap.args.opcode = FUSE_READLINK;
1471 ap.args.nodeid = get_node_id(inode);
1472 ap.args.out_pages = true;
1473 ap.args.out_argvar = true;
1474 ap.args.page_zeroing = true;
1475 ap.args.out_numargs = 1;
1476 ap.args.out_args[0].size = desc.length;
1477 res = fuse_simple_request(fm, &ap.args);
1479 fuse_invalidate_atime(inode);
1484 if (WARN_ON(res >= PAGE_SIZE))
1487 link = page_address(page);
1493 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1494 struct delayed_call *callback)
1496 struct fuse_conn *fc = get_fuse_conn(inode);
1501 if (fuse_is_bad(inode))
1504 if (fc->cache_symlinks)
1505 return page_get_link(dentry, inode, callback);
1511 page = alloc_page(GFP_KERNEL);
1516 err = fuse_readlink_page(inode, page);
1522 set_delayed_call(callback, page_put_link, page);
1524 return page_address(page);
1527 return ERR_PTR(err);
1530 static int fuse_dir_open(struct inode *inode, struct file *file)
1532 return fuse_open_common(inode, file, true);
1535 static int fuse_dir_release(struct inode *inode, struct file *file)
1537 fuse_release_common(file, true);
1542 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1545 struct inode *inode = file->f_mapping->host;
1546 struct fuse_conn *fc = get_fuse_conn(inode);
1549 if (fuse_is_bad(inode))
1552 if (fc->no_fsyncdir)
1556 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1557 if (err == -ENOSYS) {
1558 fc->no_fsyncdir = 1;
1561 inode_unlock(inode);
1566 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1569 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1571 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1575 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1578 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1581 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1586 return fuse_ioctl_common(file, cmd, arg,
1587 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1590 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1592 /* Always update if mtime is explicitly set */
1593 if (ivalid & ATTR_MTIME_SET)
1596 /* Or if kernel i_mtime is the official one */
1597 if (trust_local_mtime)
1600 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1601 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1604 /* In all other cases update */
1608 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1609 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1611 unsigned ivalid = iattr->ia_valid;
1613 if (ivalid & ATTR_MODE)
1614 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1615 if (ivalid & ATTR_UID)
1616 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1617 if (ivalid & ATTR_GID)
1618 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1619 if (ivalid & ATTR_SIZE)
1620 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1621 if (ivalid & ATTR_ATIME) {
1622 arg->valid |= FATTR_ATIME;
1623 arg->atime = iattr->ia_atime.tv_sec;
1624 arg->atimensec = iattr->ia_atime.tv_nsec;
1625 if (!(ivalid & ATTR_ATIME_SET))
1626 arg->valid |= FATTR_ATIME_NOW;
1628 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1629 arg->valid |= FATTR_MTIME;
1630 arg->mtime = iattr->ia_mtime.tv_sec;
1631 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1632 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1633 arg->valid |= FATTR_MTIME_NOW;
1635 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1636 arg->valid |= FATTR_CTIME;
1637 arg->ctime = iattr->ia_ctime.tv_sec;
1638 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1643 * Prevent concurrent writepages on inode
1645 * This is done by adding a negative bias to the inode write counter
1646 * and waiting for all pending writes to finish.
1648 void fuse_set_nowrite(struct inode *inode)
1650 struct fuse_inode *fi = get_fuse_inode(inode);
1652 BUG_ON(!inode_is_locked(inode));
1654 spin_lock(&fi->lock);
1655 BUG_ON(fi->writectr < 0);
1656 fi->writectr += FUSE_NOWRITE;
1657 spin_unlock(&fi->lock);
1658 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1662 * Allow writepages on inode
1664 * Remove the bias from the writecounter and send any queued
1667 static void __fuse_release_nowrite(struct inode *inode)
1669 struct fuse_inode *fi = get_fuse_inode(inode);
1671 BUG_ON(fi->writectr != FUSE_NOWRITE);
1673 fuse_flush_writepages(inode);
1676 void fuse_release_nowrite(struct inode *inode)
1678 struct fuse_inode *fi = get_fuse_inode(inode);
1680 spin_lock(&fi->lock);
1681 __fuse_release_nowrite(inode);
1682 spin_unlock(&fi->lock);
1685 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1686 struct inode *inode,
1687 struct fuse_setattr_in *inarg_p,
1688 struct fuse_attr_out *outarg_p)
1690 args->opcode = FUSE_SETATTR;
1691 args->nodeid = get_node_id(inode);
1692 args->in_numargs = 1;
1693 args->in_args[0].size = sizeof(*inarg_p);
1694 args->in_args[0].value = inarg_p;
1695 args->out_numargs = 1;
1696 args->out_args[0].size = sizeof(*outarg_p);
1697 args->out_args[0].value = outarg_p;
1701 * Flush inode->i_mtime to the server
1703 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1705 struct fuse_mount *fm = get_fuse_mount(inode);
1707 struct fuse_setattr_in inarg;
1708 struct fuse_attr_out outarg;
1710 memset(&inarg, 0, sizeof(inarg));
1711 memset(&outarg, 0, sizeof(outarg));
1713 inarg.valid = FATTR_MTIME;
1714 inarg.mtime = inode->i_mtime.tv_sec;
1715 inarg.mtimensec = inode->i_mtime.tv_nsec;
1716 if (fm->fc->minor >= 23) {
1717 inarg.valid |= FATTR_CTIME;
1718 inarg.ctime = inode->i_ctime.tv_sec;
1719 inarg.ctimensec = inode->i_ctime.tv_nsec;
1722 inarg.valid |= FATTR_FH;
1725 fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1727 return fuse_simple_request(fm, &args);
1731 * Set attributes, and at the same time refresh them.
1733 * Truncation is slightly complicated, because the 'truncate' request
1734 * may fail, in which case we don't want to touch the mapping.
1735 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1736 * and the actual truncation by hand.
1738 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1741 struct inode *inode = d_inode(dentry);
1742 struct fuse_mount *fm = get_fuse_mount(inode);
1743 struct fuse_conn *fc = fm->fc;
1744 struct fuse_inode *fi = get_fuse_inode(inode);
1745 struct address_space *mapping = inode->i_mapping;
1747 struct fuse_setattr_in inarg;
1748 struct fuse_attr_out outarg;
1749 bool is_truncate = false;
1750 bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode);
1753 bool trust_local_cmtime = is_wb;
1754 bool fault_blocked = false;
1756 if (!fc->default_permissions)
1757 attr->ia_valid |= ATTR_FORCE;
1759 err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
1763 if (attr->ia_valid & ATTR_SIZE) {
1764 if (WARN_ON(!S_ISREG(inode->i_mode)))
1769 if (FUSE_IS_DAX(inode) && is_truncate) {
1770 filemap_invalidate_lock(mapping);
1771 fault_blocked = true;
1772 err = fuse_dax_break_layouts(inode, 0, 0);
1774 filemap_invalidate_unlock(mapping);
1779 if (attr->ia_valid & ATTR_OPEN) {
1780 /* This is coming from open(..., ... | O_TRUNC); */
1781 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1782 WARN_ON(attr->ia_size != 0);
1783 if (fc->atomic_o_trunc) {
1785 * No need to send request to userspace, since actual
1786 * truncation has already been done by OPEN. But still
1787 * need to truncate page cache.
1789 i_size_write(inode, 0);
1790 truncate_pagecache(inode, 0);
1796 /* Flush dirty data/metadata before non-truncate SETATTR */
1799 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1801 err = write_inode_now(inode, true);
1805 fuse_set_nowrite(inode);
1806 fuse_release_nowrite(inode);
1810 fuse_set_nowrite(inode);
1811 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1812 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1813 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1816 memset(&inarg, 0, sizeof(inarg));
1817 memset(&outarg, 0, sizeof(outarg));
1818 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1820 struct fuse_file *ff = file->private_data;
1821 inarg.valid |= FATTR_FH;
1825 /* Kill suid/sgid for non-directory chown unconditionally */
1826 if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
1827 attr->ia_valid & (ATTR_UID | ATTR_GID))
1828 inarg.valid |= FATTR_KILL_SUIDGID;
1830 if (attr->ia_valid & ATTR_SIZE) {
1831 /* For mandatory locking in truncate */
1832 inarg.valid |= FATTR_LOCKOWNER;
1833 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1835 /* Kill suid/sgid for truncate only if no CAP_FSETID */
1836 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1837 inarg.valid |= FATTR_KILL_SUIDGID;
1839 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1840 err = fuse_simple_request(fm, &args);
1843 fuse_invalidate_attr(inode);
1847 if (fuse_invalid_attr(&outarg.attr) ||
1848 inode_wrong_type(inode, outarg.attr.mode)) {
1849 fuse_make_bad(inode);
1854 spin_lock(&fi->lock);
1855 /* the kernel maintains i_mtime locally */
1856 if (trust_local_cmtime) {
1857 if (attr->ia_valid & ATTR_MTIME)
1858 inode->i_mtime = attr->ia_mtime;
1859 if (attr->ia_valid & ATTR_CTIME)
1860 inode->i_ctime = attr->ia_ctime;
1861 /* FIXME: clear I_DIRTY_SYNC? */
1864 fuse_change_attributes_common(inode, &outarg.attr,
1865 attr_timeout(&outarg),
1866 fuse_get_cache_mask(inode));
1867 oldsize = inode->i_size;
1868 /* see the comment in fuse_change_attributes() */
1869 if (!is_wb || is_truncate)
1870 i_size_write(inode, outarg.attr.size);
1873 /* NOTE: this may release/reacquire fi->lock */
1874 __fuse_release_nowrite(inode);
1876 spin_unlock(&fi->lock);
1879 * Only call invalidate_inode_pages2() after removing
1880 * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock.
1882 if ((is_truncate || !is_wb) &&
1883 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1884 truncate_pagecache(inode, outarg.attr.size);
1885 invalidate_inode_pages2(mapping);
1888 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1891 filemap_invalidate_unlock(mapping);
1897 fuse_release_nowrite(inode);
1899 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1902 filemap_invalidate_unlock(mapping);
1906 static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,
1909 struct inode *inode = d_inode(entry);
1910 struct fuse_conn *fc = get_fuse_conn(inode);
1911 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1914 if (fuse_is_bad(inode))
1917 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1920 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1921 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1925 * The only sane way to reliably kill suid/sgid is to do it in
1926 * the userspace filesystem
1928 * This should be done on write(), truncate() and chown().
1930 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
1932 * ia_mode calculation may have used stale i_mode.
1933 * Refresh and recalculate.
1935 ret = fuse_do_getattr(inode, NULL, file);
1939 attr->ia_mode = inode->i_mode;
1940 if (inode->i_mode & S_ISUID) {
1941 attr->ia_valid |= ATTR_MODE;
1942 attr->ia_mode &= ~S_ISUID;
1944 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1945 attr->ia_valid |= ATTR_MODE;
1946 attr->ia_mode &= ~S_ISGID;
1950 if (!attr->ia_valid)
1953 ret = fuse_do_setattr(entry, attr, file);
1956 * If filesystem supports acls it may have updated acl xattrs in
1957 * the filesystem, so forget cached acls for the inode.
1960 forget_all_cached_acls(inode);
1962 /* Directory mode changed, may need to revalidate access */
1963 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1964 fuse_invalidate_entry_cache(entry);
1969 static int fuse_getattr(struct mnt_idmap *idmap,
1970 const struct path *path, struct kstat *stat,
1971 u32 request_mask, unsigned int flags)
1973 struct inode *inode = d_inode(path->dentry);
1974 struct fuse_conn *fc = get_fuse_conn(inode);
1976 if (fuse_is_bad(inode))
1979 if (!fuse_allow_current_process(fc)) {
1980 if (!request_mask) {
1982 * If user explicitly requested *nothing* then don't
1983 * error out, but return st_dev only.
1985 stat->result_mask = 0;
1986 stat->dev = inode->i_sb->s_dev;
1992 return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1995 static const struct inode_operations fuse_dir_inode_operations = {
1996 .lookup = fuse_lookup,
1997 .mkdir = fuse_mkdir,
1998 .symlink = fuse_symlink,
1999 .unlink = fuse_unlink,
2000 .rmdir = fuse_rmdir,
2001 .rename = fuse_rename2,
2003 .setattr = fuse_setattr,
2004 .create = fuse_create,
2005 .atomic_open = fuse_atomic_open,
2006 .tmpfile = fuse_tmpfile,
2007 .mknod = fuse_mknod,
2008 .permission = fuse_permission,
2009 .getattr = fuse_getattr,
2010 .listxattr = fuse_listxattr,
2011 .get_inode_acl = fuse_get_inode_acl,
2012 .get_acl = fuse_get_acl,
2013 .set_acl = fuse_set_acl,
2014 .fileattr_get = fuse_fileattr_get,
2015 .fileattr_set = fuse_fileattr_set,
2018 static const struct file_operations fuse_dir_operations = {
2019 .llseek = generic_file_llseek,
2020 .read = generic_read_dir,
2021 .iterate_shared = fuse_readdir,
2022 .open = fuse_dir_open,
2023 .release = fuse_dir_release,
2024 .fsync = fuse_dir_fsync,
2025 .unlocked_ioctl = fuse_dir_ioctl,
2026 .compat_ioctl = fuse_dir_compat_ioctl,
2029 static const struct inode_operations fuse_common_inode_operations = {
2030 .setattr = fuse_setattr,
2031 .permission = fuse_permission,
2032 .getattr = fuse_getattr,
2033 .listxattr = fuse_listxattr,
2034 .get_inode_acl = fuse_get_inode_acl,
2035 .get_acl = fuse_get_acl,
2036 .set_acl = fuse_set_acl,
2037 .fileattr_get = fuse_fileattr_get,
2038 .fileattr_set = fuse_fileattr_set,
2041 static const struct inode_operations fuse_symlink_inode_operations = {
2042 .setattr = fuse_setattr,
2043 .get_link = fuse_get_link,
2044 .getattr = fuse_getattr,
2045 .listxattr = fuse_listxattr,
2048 void fuse_init_common(struct inode *inode)
2050 inode->i_op = &fuse_common_inode_operations;
2053 void fuse_init_dir(struct inode *inode)
2055 struct fuse_inode *fi = get_fuse_inode(inode);
2057 inode->i_op = &fuse_dir_inode_operations;
2058 inode->i_fop = &fuse_dir_operations;
2060 spin_lock_init(&fi->rdc.lock);
2061 fi->rdc.cached = false;
2064 fi->rdc.version = 0;
2067 static int fuse_symlink_read_folio(struct file *null, struct folio *folio)
2069 int err = fuse_readlink_page(folio->mapping->host, &folio->page);
2072 folio_mark_uptodate(folio);
2074 folio_unlock(folio);
2079 static const struct address_space_operations fuse_symlink_aops = {
2080 .read_folio = fuse_symlink_read_folio,
2083 void fuse_init_symlink(struct inode *inode)
2085 inode->i_op = &fuse_symlink_inode_operations;
2086 inode->i_data.a_ops = &fuse_symlink_aops;
2087 inode_nohighmem(inode);