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>
20 #include <linux/security.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
24 static void fuse_advise_use_readdirplus(struct inode *dir)
26 struct fuse_inode *fi = get_fuse_inode(dir);
28 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
31 #if BITS_PER_LONG >= 64
32 static inline void __fuse_dentry_settime(struct dentry *entry, u64 time)
34 entry->d_fsdata = (void *) time;
37 static inline u64 fuse_dentry_time(const struct dentry *entry)
39 return (u64)entry->d_fsdata;
48 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
50 ((union fuse_dentry *) dentry->d_fsdata)->time = time;
53 static inline u64 fuse_dentry_time(const struct dentry *entry)
55 return ((union fuse_dentry *) entry->d_fsdata)->time;
59 static void fuse_dentry_settime(struct dentry *dentry, u64 time)
61 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
62 bool delete = !time && fc->delete_stale;
64 * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
65 * Don't care about races, either way it's just an optimization
67 if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
68 (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
69 spin_lock(&dentry->d_lock);
71 dentry->d_flags &= ~DCACHE_OP_DELETE;
73 dentry->d_flags |= DCACHE_OP_DELETE;
74 spin_unlock(&dentry->d_lock);
77 __fuse_dentry_settime(dentry, time);
81 * FUSE caches dentries and attributes with separate timeout. The
82 * time in jiffies until the dentry/attributes are valid is stored in
83 * dentry->d_fsdata and fuse_inode->i_time respectively.
87 * Calculate the time in jiffies until a dentry/attributes are valid
89 static u64 time_to_jiffies(u64 sec, u32 nsec)
92 struct timespec64 ts = {
94 min_t(u32, nsec, NSEC_PER_SEC - 1)
97 return get_jiffies_64() + timespec64_to_jiffies(&ts);
103 * Set dentry and possibly attribute timeouts from the lookup/mk*
106 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
108 fuse_dentry_settime(entry,
109 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
112 static u64 attr_timeout(struct fuse_attr_out *o)
114 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
117 u64 entry_attr_timeout(struct fuse_entry_out *o)
119 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
122 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
124 set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
128 * Mark the attributes as stale, so that at the next call to
129 * ->getattr() they will be fetched from userspace
131 void fuse_invalidate_attr(struct inode *inode)
133 fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
136 static void fuse_dir_changed(struct inode *dir)
138 fuse_invalidate_attr(dir);
139 inode_maybe_inc_iversion(dir, false);
143 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
146 void fuse_invalidate_atime(struct inode *inode)
148 if (!IS_RDONLY(inode))
149 fuse_invalidate_attr_mask(inode, STATX_ATIME);
153 * Just mark the entry as stale, so that a next attempt to look it up
154 * will result in a new lookup call to userspace
156 * This is called when a dentry is about to become negative and the
157 * timeout is unknown (unlink, rmdir, rename and in some cases
160 void fuse_invalidate_entry_cache(struct dentry *entry)
162 fuse_dentry_settime(entry, 0);
166 * Same as fuse_invalidate_entry_cache(), but also try to remove the
167 * dentry from the hash
169 static void fuse_invalidate_entry(struct dentry *entry)
172 fuse_invalidate_entry_cache(entry);
175 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
176 u64 nodeid, const struct qstr *name,
177 struct fuse_entry_out *outarg)
179 memset(outarg, 0, sizeof(struct fuse_entry_out));
180 args->opcode = FUSE_LOOKUP;
181 args->nodeid = nodeid;
182 args->in_numargs = 1;
183 args->in_args[0].size = name->len + 1;
184 args->in_args[0].value = name->name;
185 args->out_numargs = 1;
186 args->out_args[0].size = sizeof(struct fuse_entry_out);
187 args->out_args[0].value = outarg;
191 * Check whether the dentry is still valid
193 * If the entry validity timeout has expired and the dentry is
194 * positive, try to redo the lookup. If the lookup results in a
195 * different inode, then let the VFS invalidate the dentry and redo
196 * the lookup once more. If the lookup results in the same inode,
197 * then refresh the attributes, timeouts and mark the dentry valid.
199 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
202 struct dentry *parent;
203 struct fuse_mount *fm;
204 struct fuse_inode *fi;
207 inode = d_inode_rcu(entry);
208 if (inode && fuse_is_bad(inode))
210 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
211 (flags & (LOOKUP_EXCL | LOOKUP_REVAL))) {
212 struct fuse_entry_out outarg;
214 struct fuse_forget_link *forget;
217 /* For negative dentries, always do a fresh lookup */
222 if (flags & LOOKUP_RCU)
225 fm = get_fuse_mount(inode);
227 forget = fuse_alloc_forget();
232 attr_version = fuse_get_attr_version(fm->fc);
234 parent = dget_parent(entry);
235 fuse_lookup_init(fm->fc, &args, get_node_id(d_inode(parent)),
236 &entry->d_name, &outarg);
237 ret = fuse_simple_request(fm, &args);
239 /* Zero nodeid is same as -ENOENT */
240 if (!ret && !outarg.nodeid)
243 fi = get_fuse_inode(inode);
244 if (outarg.nodeid != get_node_id(inode) ||
245 (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) {
246 fuse_queue_forget(fm->fc, forget,
250 spin_lock(&fi->lock);
252 spin_unlock(&fi->lock);
257 if (ret || fuse_invalid_attr(&outarg.attr) ||
258 fuse_stale_inode(inode, outarg.generation, &outarg.attr))
261 forget_all_cached_acls(inode);
262 fuse_change_attributes(inode, &outarg.attr,
263 entry_attr_timeout(&outarg),
265 fuse_change_entry_timeout(entry, &outarg);
267 fi = get_fuse_inode(inode);
268 if (flags & LOOKUP_RCU) {
269 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
271 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
272 parent = dget_parent(entry);
273 fuse_advise_use_readdirplus(d_inode(parent));
286 #if BITS_PER_LONG < 64
287 static int fuse_dentry_init(struct dentry *dentry)
289 dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry),
290 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
292 return dentry->d_fsdata ? 0 : -ENOMEM;
294 static void fuse_dentry_release(struct dentry *dentry)
296 union fuse_dentry *fd = dentry->d_fsdata;
302 static int fuse_dentry_delete(const struct dentry *dentry)
304 return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
308 * Create a fuse_mount object with a new superblock (with path->dentry
309 * as the root), and return that mount so it can be auto-mounted on
312 static struct vfsmount *fuse_dentry_automount(struct path *path)
314 struct fs_context *fsc;
315 struct vfsmount *mnt;
316 struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
318 fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
320 return ERR_CAST(fsc);
322 /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */
323 fsc->fs_private = mp_fi;
325 /* Create the submount */
334 const struct dentry_operations fuse_dentry_operations = {
335 .d_revalidate = fuse_dentry_revalidate,
336 .d_delete = fuse_dentry_delete,
337 #if BITS_PER_LONG < 64
338 .d_init = fuse_dentry_init,
339 .d_release = fuse_dentry_release,
341 .d_automount = fuse_dentry_automount,
344 const struct dentry_operations fuse_root_dentry_operations = {
345 #if BITS_PER_LONG < 64
346 .d_init = fuse_dentry_init,
347 .d_release = fuse_dentry_release,
351 int fuse_valid_type(int m)
353 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
354 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
357 bool fuse_invalid_attr(struct fuse_attr *attr)
359 return !fuse_valid_type(attr->mode) ||
360 attr->size > LLONG_MAX;
363 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
364 struct fuse_entry_out *outarg, struct inode **inode)
366 struct fuse_mount *fm = get_fuse_mount_super(sb);
368 struct fuse_forget_link *forget;
374 if (name->len > FUSE_NAME_MAX)
378 forget = fuse_alloc_forget();
383 attr_version = fuse_get_attr_version(fm->fc);
385 fuse_lookup_init(fm->fc, &args, nodeid, name, outarg);
386 err = fuse_simple_request(fm, &args);
387 /* Zero nodeid is same as -ENOENT, but with valid timeout */
388 if (err || !outarg->nodeid)
394 if (fuse_invalid_attr(&outarg->attr))
397 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
398 &outarg->attr, entry_attr_timeout(outarg),
402 fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1);
413 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
417 struct fuse_entry_out outarg;
419 struct dentry *newent;
420 bool outarg_valid = true;
423 if (fuse_is_bad(dir))
424 return ERR_PTR(-EIO);
426 locked = fuse_lock_inode(dir);
427 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
429 fuse_unlock_inode(dir, locked);
430 if (err == -ENOENT) {
431 outarg_valid = false;
438 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
441 newent = d_splice_alias(inode, entry);
442 err = PTR_ERR(newent);
446 entry = newent ? newent : entry;
448 fuse_change_entry_timeout(entry, &outarg);
450 fuse_invalidate_entry_cache(entry);
453 fuse_advise_use_readdirplus(dir);
462 static int get_security_context(struct dentry *entry, umode_t mode,
463 void **security_ctx, u32 *security_ctxlen)
465 struct fuse_secctx *fctx;
466 struct fuse_secctx_header *header;
467 void *ctx = NULL, *ptr;
468 u32 ctxlen, total_len = sizeof(*header);
473 err = security_dentry_init_security(entry, mode, &entry->d_name,
474 &name, &ctx, &ctxlen);
476 if (err != -EOPNOTSUPP)
478 /* No LSM is supporting this security hook. Ignore error */
485 namelen = strlen(name) + 1;
487 if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || ctxlen > S32_MAX))
489 total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + ctxlen);
493 header = ptr = kzalloc(total_len, GFP_KERNEL);
497 header->nr_secctx = nr_ctx;
498 header->size = total_len;
499 ptr += sizeof(*header);
503 ptr += sizeof(*fctx);
508 memcpy(ptr, ctx, ctxlen);
510 *security_ctxlen = total_len;
511 *security_ctx = header;
519 * Atomic create+open operation
521 * If the filesystem doesn't support this, then fall back to separate
522 * 'mknod' + 'open' requests.
524 static int fuse_create_open(struct inode *dir, struct dentry *entry,
525 struct file *file, unsigned int flags,
530 struct fuse_mount *fm = get_fuse_mount(dir);
532 struct fuse_forget_link *forget;
533 struct fuse_create_in inarg;
534 struct fuse_open_out outopen;
535 struct fuse_entry_out outentry;
536 struct fuse_inode *fi;
537 struct fuse_file *ff;
538 void *security_ctx = NULL;
541 /* Userspace expects S_IFREG in create mode */
542 BUG_ON((mode & S_IFMT) != S_IFREG);
544 forget = fuse_alloc_forget();
550 ff = fuse_file_alloc(fm);
552 goto out_put_forget_req;
554 if (!fm->fc->dont_mask)
555 mode &= ~current_umask();
558 memset(&inarg, 0, sizeof(inarg));
559 memset(&outentry, 0, sizeof(outentry));
562 inarg.umask = current_umask();
564 if (fm->fc->handle_killpriv_v2 && (flags & O_TRUNC) &&
565 !(flags & O_EXCL) && !capable(CAP_FSETID)) {
566 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
569 args.opcode = FUSE_CREATE;
570 args.nodeid = get_node_id(dir);
572 args.in_args[0].size = sizeof(inarg);
573 args.in_args[0].value = &inarg;
574 args.in_args[1].size = entry->d_name.len + 1;
575 args.in_args[1].value = entry->d_name.name;
576 args.out_numargs = 2;
577 args.out_args[0].size = sizeof(outentry);
578 args.out_args[0].value = &outentry;
579 args.out_args[1].size = sizeof(outopen);
580 args.out_args[1].value = &outopen;
582 if (fm->fc->init_security) {
583 err = get_security_context(entry, mode, &security_ctx,
586 goto out_put_forget_req;
589 args.in_args[2].size = security_ctxlen;
590 args.in_args[2].value = security_ctx;
593 err = fuse_simple_request(fm, &args);
599 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
600 fuse_invalid_attr(&outentry.attr))
604 ff->nodeid = outentry.nodeid;
605 ff->open_flags = outopen.open_flags;
606 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
607 &outentry.attr, entry_attr_timeout(&outentry), 0);
609 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
610 fuse_sync_release(NULL, ff, flags);
611 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
616 d_instantiate(entry, inode);
617 fuse_change_entry_timeout(entry, &outentry);
618 fuse_dir_changed(dir);
619 err = finish_open(file, entry, generic_file_open);
621 fi = get_fuse_inode(inode);
622 fuse_sync_release(fi, ff, flags);
624 file->private_data = ff;
625 fuse_finish_open(inode, file);
637 static int fuse_mknod(struct user_namespace *, struct inode *, struct dentry *,
639 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
640 struct file *file, unsigned flags,
644 struct fuse_conn *fc = get_fuse_conn(dir);
645 struct dentry *res = NULL;
647 if (fuse_is_bad(dir))
650 if (d_in_lookup(entry)) {
651 res = fuse_lookup(dir, entry, 0);
659 if (!(flags & O_CREAT) || d_really_is_positive(entry))
663 file->f_mode |= FMODE_CREATED;
668 err = fuse_create_open(dir, entry, file, flags, mode);
669 if (err == -ENOSYS) {
678 err = fuse_mknod(&init_user_ns, dir, entry, mode, 0);
682 return finish_no_open(file, res);
686 * Code shared between mknod, mkdir, symlink and link
688 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
689 struct inode *dir, struct dentry *entry,
692 struct fuse_entry_out outarg;
696 struct fuse_forget_link *forget;
697 void *security_ctx = NULL;
700 if (fuse_is_bad(dir))
703 forget = fuse_alloc_forget();
707 memset(&outarg, 0, sizeof(outarg));
708 args->nodeid = get_node_id(dir);
709 args->out_numargs = 1;
710 args->out_args[0].size = sizeof(outarg);
711 args->out_args[0].value = &outarg;
713 if (fm->fc->init_security && args->opcode != FUSE_LINK) {
714 err = get_security_context(entry, mode, &security_ctx,
717 goto out_put_forget_req;
719 BUG_ON(args->in_numargs != 2);
721 args->in_numargs = 3;
722 args->in_args[2].size = security_ctxlen;
723 args->in_args[2].value = security_ctx;
726 err = fuse_simple_request(fm, args);
729 goto out_put_forget_req;
732 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
733 goto out_put_forget_req;
735 if ((outarg.attr.mode ^ mode) & S_IFMT)
736 goto out_put_forget_req;
738 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
739 &outarg.attr, entry_attr_timeout(&outarg), 0);
741 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
747 d = d_splice_alias(inode, entry);
752 fuse_change_entry_timeout(d, &outarg);
755 fuse_change_entry_timeout(entry, &outarg);
757 fuse_dir_changed(dir);
765 static int fuse_mknod(struct user_namespace *mnt_userns, struct inode *dir,
766 struct dentry *entry, umode_t mode, dev_t rdev)
768 struct fuse_mknod_in inarg;
769 struct fuse_mount *fm = get_fuse_mount(dir);
772 if (!fm->fc->dont_mask)
773 mode &= ~current_umask();
775 memset(&inarg, 0, sizeof(inarg));
777 inarg.rdev = new_encode_dev(rdev);
778 inarg.umask = current_umask();
779 args.opcode = FUSE_MKNOD;
781 args.in_args[0].size = sizeof(inarg);
782 args.in_args[0].value = &inarg;
783 args.in_args[1].size = entry->d_name.len + 1;
784 args.in_args[1].value = entry->d_name.name;
785 return create_new_entry(fm, &args, dir, entry, mode);
788 static int fuse_create(struct user_namespace *mnt_userns, struct inode *dir,
789 struct dentry *entry, umode_t mode, bool excl)
791 return fuse_mknod(&init_user_ns, dir, entry, mode, 0);
794 static int fuse_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
795 struct dentry *entry, umode_t mode)
797 struct fuse_mkdir_in inarg;
798 struct fuse_mount *fm = get_fuse_mount(dir);
801 if (!fm->fc->dont_mask)
802 mode &= ~current_umask();
804 memset(&inarg, 0, sizeof(inarg));
806 inarg.umask = current_umask();
807 args.opcode = FUSE_MKDIR;
809 args.in_args[0].size = sizeof(inarg);
810 args.in_args[0].value = &inarg;
811 args.in_args[1].size = entry->d_name.len + 1;
812 args.in_args[1].value = entry->d_name.name;
813 return create_new_entry(fm, &args, dir, entry, S_IFDIR);
816 static int fuse_symlink(struct user_namespace *mnt_userns, struct inode *dir,
817 struct dentry *entry, const char *link)
819 struct fuse_mount *fm = get_fuse_mount(dir);
820 unsigned len = strlen(link) + 1;
823 args.opcode = FUSE_SYMLINK;
825 args.in_args[0].size = entry->d_name.len + 1;
826 args.in_args[0].value = entry->d_name.name;
827 args.in_args[1].size = len;
828 args.in_args[1].value = link;
829 return create_new_entry(fm, &args, dir, entry, S_IFLNK);
832 void fuse_flush_time_update(struct inode *inode)
834 int err = sync_inode_metadata(inode, 1);
836 mapping_set_error(inode->i_mapping, err);
839 static void fuse_update_ctime_in_cache(struct inode *inode)
841 if (!IS_NOCMTIME(inode)) {
842 inode->i_ctime = current_time(inode);
843 mark_inode_dirty_sync(inode);
844 fuse_flush_time_update(inode);
848 void fuse_update_ctime(struct inode *inode)
850 fuse_invalidate_attr_mask(inode, STATX_CTIME);
851 fuse_update_ctime_in_cache(inode);
854 static void fuse_entry_unlinked(struct dentry *entry)
856 struct inode *inode = d_inode(entry);
857 struct fuse_conn *fc = get_fuse_conn(inode);
858 struct fuse_inode *fi = get_fuse_inode(inode);
860 spin_lock(&fi->lock);
861 fi->attr_version = atomic64_inc_return(&fc->attr_version);
863 * If i_nlink == 0 then unlink doesn't make sense, yet this can
864 * happen if userspace filesystem is careless. It would be
865 * difficult to enforce correct nlink usage so just ignore this
868 if (S_ISDIR(inode->i_mode))
870 else if (inode->i_nlink > 0)
872 spin_unlock(&fi->lock);
873 fuse_invalidate_entry_cache(entry);
874 fuse_update_ctime(inode);
877 static int fuse_unlink(struct inode *dir, struct dentry *entry)
880 struct fuse_mount *fm = get_fuse_mount(dir);
883 if (fuse_is_bad(dir))
886 args.opcode = FUSE_UNLINK;
887 args.nodeid = get_node_id(dir);
889 args.in_args[0].size = entry->d_name.len + 1;
890 args.in_args[0].value = entry->d_name.name;
891 err = fuse_simple_request(fm, &args);
893 fuse_dir_changed(dir);
894 fuse_entry_unlinked(entry);
895 } else if (err == -EINTR)
896 fuse_invalidate_entry(entry);
900 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
903 struct fuse_mount *fm = get_fuse_mount(dir);
906 if (fuse_is_bad(dir))
909 args.opcode = FUSE_RMDIR;
910 args.nodeid = get_node_id(dir);
912 args.in_args[0].size = entry->d_name.len + 1;
913 args.in_args[0].value = entry->d_name.name;
914 err = fuse_simple_request(fm, &args);
916 fuse_dir_changed(dir);
917 fuse_entry_unlinked(entry);
918 } else if (err == -EINTR)
919 fuse_invalidate_entry(entry);
923 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
924 struct inode *newdir, struct dentry *newent,
925 unsigned int flags, int opcode, size_t argsize)
928 struct fuse_rename2_in inarg;
929 struct fuse_mount *fm = get_fuse_mount(olddir);
932 memset(&inarg, 0, argsize);
933 inarg.newdir = get_node_id(newdir);
935 args.opcode = opcode;
936 args.nodeid = get_node_id(olddir);
938 args.in_args[0].size = argsize;
939 args.in_args[0].value = &inarg;
940 args.in_args[1].size = oldent->d_name.len + 1;
941 args.in_args[1].value = oldent->d_name.name;
942 args.in_args[2].size = newent->d_name.len + 1;
943 args.in_args[2].value = newent->d_name.name;
944 err = fuse_simple_request(fm, &args);
947 fuse_update_ctime(d_inode(oldent));
949 if (flags & RENAME_EXCHANGE)
950 fuse_update_ctime(d_inode(newent));
952 fuse_dir_changed(olddir);
953 if (olddir != newdir)
954 fuse_dir_changed(newdir);
956 /* newent will end up negative */
957 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent))
958 fuse_entry_unlinked(newent);
959 } else if (err == -EINTR) {
960 /* If request was interrupted, DEITY only knows if the
961 rename actually took place. If the invalidation
962 fails (e.g. some process has CWD under the renamed
963 directory), then there can be inconsistency between
964 the dcache and the real filesystem. Tough luck. */
965 fuse_invalidate_entry(oldent);
966 if (d_really_is_positive(newent))
967 fuse_invalidate_entry(newent);
973 static int fuse_rename2(struct user_namespace *mnt_userns, struct inode *olddir,
974 struct dentry *oldent, struct inode *newdir,
975 struct dentry *newent, unsigned int flags)
977 struct fuse_conn *fc = get_fuse_conn(olddir);
980 if (fuse_is_bad(olddir))
983 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
987 if (fc->no_rename2 || fc->minor < 23)
990 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
992 sizeof(struct fuse_rename2_in));
993 if (err == -ENOSYS) {
998 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
1000 sizeof(struct fuse_rename_in));
1006 static int fuse_link(struct dentry *entry, struct inode *newdir,
1007 struct dentry *newent)
1010 struct fuse_link_in inarg;
1011 struct inode *inode = d_inode(entry);
1012 struct fuse_mount *fm = get_fuse_mount(inode);
1015 memset(&inarg, 0, sizeof(inarg));
1016 inarg.oldnodeid = get_node_id(inode);
1017 args.opcode = FUSE_LINK;
1018 args.in_numargs = 2;
1019 args.in_args[0].size = sizeof(inarg);
1020 args.in_args[0].value = &inarg;
1021 args.in_args[1].size = newent->d_name.len + 1;
1022 args.in_args[1].value = newent->d_name.name;
1023 err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
1025 fuse_update_ctime_in_cache(inode);
1026 else if (err == -EINTR)
1027 fuse_invalidate_attr(inode);
1032 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1035 unsigned int blkbits;
1036 struct fuse_conn *fc = get_fuse_conn(inode);
1038 stat->dev = inode->i_sb->s_dev;
1039 stat->ino = attr->ino;
1040 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1041 stat->nlink = attr->nlink;
1042 stat->uid = make_kuid(fc->user_ns, attr->uid);
1043 stat->gid = make_kgid(fc->user_ns, attr->gid);
1044 stat->rdev = inode->i_rdev;
1045 stat->atime.tv_sec = attr->atime;
1046 stat->atime.tv_nsec = attr->atimensec;
1047 stat->mtime.tv_sec = attr->mtime;
1048 stat->mtime.tv_nsec = attr->mtimensec;
1049 stat->ctime.tv_sec = attr->ctime;
1050 stat->ctime.tv_nsec = attr->ctimensec;
1051 stat->size = attr->size;
1052 stat->blocks = attr->blocks;
1054 if (attr->blksize != 0)
1055 blkbits = ilog2(attr->blksize);
1057 blkbits = inode->i_sb->s_blocksize_bits;
1059 stat->blksize = 1 << blkbits;
1062 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
1066 struct fuse_getattr_in inarg;
1067 struct fuse_attr_out outarg;
1068 struct fuse_mount *fm = get_fuse_mount(inode);
1072 attr_version = fuse_get_attr_version(fm->fc);
1074 memset(&inarg, 0, sizeof(inarg));
1075 memset(&outarg, 0, sizeof(outarg));
1076 /* Directories have separate file-handle space */
1077 if (file && S_ISREG(inode->i_mode)) {
1078 struct fuse_file *ff = file->private_data;
1080 inarg.getattr_flags |= FUSE_GETATTR_FH;
1083 args.opcode = FUSE_GETATTR;
1084 args.nodeid = get_node_id(inode);
1085 args.in_numargs = 1;
1086 args.in_args[0].size = sizeof(inarg);
1087 args.in_args[0].value = &inarg;
1088 args.out_numargs = 1;
1089 args.out_args[0].size = sizeof(outarg);
1090 args.out_args[0].value = &outarg;
1091 err = fuse_simple_request(fm, &args);
1093 if (fuse_invalid_attr(&outarg.attr) ||
1094 inode_wrong_type(inode, outarg.attr.mode)) {
1095 fuse_make_bad(inode);
1098 fuse_change_attributes(inode, &outarg.attr,
1099 attr_timeout(&outarg),
1102 fuse_fillattr(inode, &outarg.attr, stat);
1108 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1109 struct kstat *stat, u32 request_mask,
1112 struct fuse_inode *fi = get_fuse_inode(inode);
1115 u32 inval_mask = READ_ONCE(fi->inval_mask);
1116 u32 cache_mask = fuse_get_cache_mask(inode);
1118 if (flags & AT_STATX_FORCE_SYNC)
1120 else if (flags & AT_STATX_DONT_SYNC)
1122 else if (request_mask & inval_mask & ~cache_mask)
1125 sync = time_before64(fi->i_time, get_jiffies_64());
1128 forget_all_cached_acls(inode);
1129 err = fuse_do_getattr(inode, stat, file);
1131 generic_fillattr(&init_user_ns, inode, stat);
1132 stat->mode = fi->orig_i_mode;
1133 stat->ino = fi->orig_ino;
1139 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask)
1141 return fuse_update_get_attr(inode, file, NULL, mask, 0);
1144 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1145 u64 child_nodeid, struct qstr *name)
1148 struct inode *parent;
1150 struct dentry *entry;
1152 parent = fuse_ilookup(fc, parent_nodeid, NULL);
1156 inode_lock_nested(parent, I_MUTEX_PARENT);
1157 if (!S_ISDIR(parent->i_mode))
1161 dir = d_find_alias(parent);
1165 name->hash = full_name_hash(dir, name->name, name->len);
1166 entry = d_lookup(dir, name);
1171 fuse_dir_changed(parent);
1172 fuse_invalidate_entry(entry);
1174 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1175 inode_lock(d_inode(entry));
1176 if (get_node_id(d_inode(entry)) != child_nodeid) {
1180 if (d_mountpoint(entry)) {
1184 if (d_is_dir(entry)) {
1185 shrink_dcache_parent(entry);
1186 if (!simple_empty(entry)) {
1190 d_inode(entry)->i_flags |= S_DEAD;
1193 clear_nlink(d_inode(entry));
1196 inode_unlock(d_inode(entry));
1205 inode_unlock(parent);
1211 * Calling into a user-controlled filesystem gives the filesystem
1212 * daemon ptrace-like capabilities over the current process. This
1213 * means, that the filesystem daemon is able to record the exact
1214 * filesystem operations performed, and can also control the behavior
1215 * of the requester process in otherwise impossible ways. For example
1216 * it can delay the operation for arbitrary length of time allowing
1217 * DoS against the requester.
1219 * For this reason only those processes can call into the filesystem,
1220 * for which the owner of the mount has ptrace privilege. This
1221 * excludes processes started by other users, suid or sgid processes.
1223 int fuse_allow_current_process(struct fuse_conn *fc)
1225 const struct cred *cred;
1227 if (fc->allow_other)
1228 return current_in_userns(fc->user_ns);
1230 cred = current_cred();
1231 if (uid_eq(cred->euid, fc->user_id) &&
1232 uid_eq(cred->suid, fc->user_id) &&
1233 uid_eq(cred->uid, fc->user_id) &&
1234 gid_eq(cred->egid, fc->group_id) &&
1235 gid_eq(cred->sgid, fc->group_id) &&
1236 gid_eq(cred->gid, fc->group_id))
1242 static int fuse_access(struct inode *inode, int mask)
1244 struct fuse_mount *fm = get_fuse_mount(inode);
1246 struct fuse_access_in inarg;
1249 BUG_ON(mask & MAY_NOT_BLOCK);
1251 if (fm->fc->no_access)
1254 memset(&inarg, 0, sizeof(inarg));
1255 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1256 args.opcode = FUSE_ACCESS;
1257 args.nodeid = get_node_id(inode);
1258 args.in_numargs = 1;
1259 args.in_args[0].size = sizeof(inarg);
1260 args.in_args[0].value = &inarg;
1261 err = fuse_simple_request(fm, &args);
1262 if (err == -ENOSYS) {
1263 fm->fc->no_access = 1;
1269 static int fuse_perm_getattr(struct inode *inode, int mask)
1271 if (mask & MAY_NOT_BLOCK)
1274 forget_all_cached_acls(inode);
1275 return fuse_do_getattr(inode, NULL, NULL);
1279 * Check permission. The two basic access models of FUSE are:
1281 * 1) Local access checking ('default_permissions' mount option) based
1282 * on file mode. This is the plain old disk filesystem permission
1285 * 2) "Remote" access checking, where server is responsible for
1286 * checking permission in each inode operation. An exception to this
1287 * is if ->permission() was invoked from sys_access() in which case an
1288 * access request is sent. Execute permission is still checked
1289 * locally based on file mode.
1291 static int fuse_permission(struct user_namespace *mnt_userns,
1292 struct inode *inode, int mask)
1294 struct fuse_conn *fc = get_fuse_conn(inode);
1295 bool refreshed = false;
1298 if (fuse_is_bad(inode))
1301 if (!fuse_allow_current_process(fc))
1305 * If attributes are needed, refresh them before proceeding
1307 if (fc->default_permissions ||
1308 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1309 struct fuse_inode *fi = get_fuse_inode(inode);
1310 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1312 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1313 time_before64(fi->i_time, get_jiffies_64())) {
1316 err = fuse_perm_getattr(inode, mask);
1322 if (fc->default_permissions) {
1323 err = generic_permission(&init_user_ns, inode, mask);
1325 /* If permission is denied, try to refresh file
1326 attributes. This is also needed, because the root
1327 node will at first have no permissions */
1328 if (err == -EACCES && !refreshed) {
1329 err = fuse_perm_getattr(inode, mask);
1331 err = generic_permission(&init_user_ns,
1335 /* Note: the opposite of the above test does not
1336 exist. So if permissions are revoked this won't be
1337 noticed immediately, only after the attribute
1338 timeout has expired */
1339 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1340 err = fuse_access(inode, mask);
1341 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1342 if (!(inode->i_mode & S_IXUGO)) {
1346 err = fuse_perm_getattr(inode, mask);
1347 if (!err && !(inode->i_mode & S_IXUGO))
1354 static int fuse_readlink_page(struct inode *inode, struct page *page)
1356 struct fuse_mount *fm = get_fuse_mount(inode);
1357 struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1358 struct fuse_args_pages ap = {
1366 ap.args.opcode = FUSE_READLINK;
1367 ap.args.nodeid = get_node_id(inode);
1368 ap.args.out_pages = true;
1369 ap.args.out_argvar = true;
1370 ap.args.page_zeroing = true;
1371 ap.args.out_numargs = 1;
1372 ap.args.out_args[0].size = desc.length;
1373 res = fuse_simple_request(fm, &ap.args);
1375 fuse_invalidate_atime(inode);
1380 if (WARN_ON(res >= PAGE_SIZE))
1383 link = page_address(page);
1389 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1390 struct delayed_call *callback)
1392 struct fuse_conn *fc = get_fuse_conn(inode);
1397 if (fuse_is_bad(inode))
1400 if (fc->cache_symlinks)
1401 return page_get_link(dentry, inode, callback);
1407 page = alloc_page(GFP_KERNEL);
1412 err = fuse_readlink_page(inode, page);
1418 set_delayed_call(callback, page_put_link, page);
1420 return page_address(page);
1423 return ERR_PTR(err);
1426 static int fuse_dir_open(struct inode *inode, struct file *file)
1428 return fuse_open_common(inode, file, true);
1431 static int fuse_dir_release(struct inode *inode, struct file *file)
1433 fuse_release_common(file, true);
1438 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1441 struct inode *inode = file->f_mapping->host;
1442 struct fuse_conn *fc = get_fuse_conn(inode);
1445 if (fuse_is_bad(inode))
1448 if (fc->no_fsyncdir)
1452 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1453 if (err == -ENOSYS) {
1454 fc->no_fsyncdir = 1;
1457 inode_unlock(inode);
1462 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1465 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1467 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1471 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1474 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1477 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1482 return fuse_ioctl_common(file, cmd, arg,
1483 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1486 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1488 /* Always update if mtime is explicitly set */
1489 if (ivalid & ATTR_MTIME_SET)
1492 /* Or if kernel i_mtime is the official one */
1493 if (trust_local_mtime)
1496 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1497 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1500 /* In all other cases update */
1504 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1505 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1507 unsigned ivalid = iattr->ia_valid;
1509 if (ivalid & ATTR_MODE)
1510 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1511 if (ivalid & ATTR_UID)
1512 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1513 if (ivalid & ATTR_GID)
1514 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1515 if (ivalid & ATTR_SIZE)
1516 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1517 if (ivalid & ATTR_ATIME) {
1518 arg->valid |= FATTR_ATIME;
1519 arg->atime = iattr->ia_atime.tv_sec;
1520 arg->atimensec = iattr->ia_atime.tv_nsec;
1521 if (!(ivalid & ATTR_ATIME_SET))
1522 arg->valid |= FATTR_ATIME_NOW;
1524 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1525 arg->valid |= FATTR_MTIME;
1526 arg->mtime = iattr->ia_mtime.tv_sec;
1527 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1528 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1529 arg->valid |= FATTR_MTIME_NOW;
1531 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1532 arg->valid |= FATTR_CTIME;
1533 arg->ctime = iattr->ia_ctime.tv_sec;
1534 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1539 * Prevent concurrent writepages on inode
1541 * This is done by adding a negative bias to the inode write counter
1542 * and waiting for all pending writes to finish.
1544 void fuse_set_nowrite(struct inode *inode)
1546 struct fuse_inode *fi = get_fuse_inode(inode);
1548 BUG_ON(!inode_is_locked(inode));
1550 spin_lock(&fi->lock);
1551 BUG_ON(fi->writectr < 0);
1552 fi->writectr += FUSE_NOWRITE;
1553 spin_unlock(&fi->lock);
1554 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1558 * Allow writepages on inode
1560 * Remove the bias from the writecounter and send any queued
1563 static void __fuse_release_nowrite(struct inode *inode)
1565 struct fuse_inode *fi = get_fuse_inode(inode);
1567 BUG_ON(fi->writectr != FUSE_NOWRITE);
1569 fuse_flush_writepages(inode);
1572 void fuse_release_nowrite(struct inode *inode)
1574 struct fuse_inode *fi = get_fuse_inode(inode);
1576 spin_lock(&fi->lock);
1577 __fuse_release_nowrite(inode);
1578 spin_unlock(&fi->lock);
1581 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1582 struct inode *inode,
1583 struct fuse_setattr_in *inarg_p,
1584 struct fuse_attr_out *outarg_p)
1586 args->opcode = FUSE_SETATTR;
1587 args->nodeid = get_node_id(inode);
1588 args->in_numargs = 1;
1589 args->in_args[0].size = sizeof(*inarg_p);
1590 args->in_args[0].value = inarg_p;
1591 args->out_numargs = 1;
1592 args->out_args[0].size = sizeof(*outarg_p);
1593 args->out_args[0].value = outarg_p;
1597 * Flush inode->i_mtime to the server
1599 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1601 struct fuse_mount *fm = get_fuse_mount(inode);
1603 struct fuse_setattr_in inarg;
1604 struct fuse_attr_out outarg;
1606 memset(&inarg, 0, sizeof(inarg));
1607 memset(&outarg, 0, sizeof(outarg));
1609 inarg.valid = FATTR_MTIME;
1610 inarg.mtime = inode->i_mtime.tv_sec;
1611 inarg.mtimensec = inode->i_mtime.tv_nsec;
1612 if (fm->fc->minor >= 23) {
1613 inarg.valid |= FATTR_CTIME;
1614 inarg.ctime = inode->i_ctime.tv_sec;
1615 inarg.ctimensec = inode->i_ctime.tv_nsec;
1618 inarg.valid |= FATTR_FH;
1621 fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1623 return fuse_simple_request(fm, &args);
1627 * Set attributes, and at the same time refresh them.
1629 * Truncation is slightly complicated, because the 'truncate' request
1630 * may fail, in which case we don't want to touch the mapping.
1631 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1632 * and the actual truncation by hand.
1634 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1637 struct inode *inode = d_inode(dentry);
1638 struct fuse_mount *fm = get_fuse_mount(inode);
1639 struct fuse_conn *fc = fm->fc;
1640 struct fuse_inode *fi = get_fuse_inode(inode);
1641 struct address_space *mapping = inode->i_mapping;
1643 struct fuse_setattr_in inarg;
1644 struct fuse_attr_out outarg;
1645 bool is_truncate = false;
1646 bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode);
1649 bool trust_local_cmtime = is_wb;
1650 bool fault_blocked = false;
1652 if (!fc->default_permissions)
1653 attr->ia_valid |= ATTR_FORCE;
1655 err = setattr_prepare(&init_user_ns, dentry, attr);
1659 if (attr->ia_valid & ATTR_SIZE) {
1660 if (WARN_ON(!S_ISREG(inode->i_mode)))
1665 if (FUSE_IS_DAX(inode) && is_truncate) {
1666 filemap_invalidate_lock(mapping);
1667 fault_blocked = true;
1668 err = fuse_dax_break_layouts(inode, 0, 0);
1670 filemap_invalidate_unlock(mapping);
1675 if (attr->ia_valid & ATTR_OPEN) {
1676 /* This is coming from open(..., ... | O_TRUNC); */
1677 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1678 WARN_ON(attr->ia_size != 0);
1679 if (fc->atomic_o_trunc) {
1681 * No need to send request to userspace, since actual
1682 * truncation has already been done by OPEN. But still
1683 * need to truncate page cache.
1685 i_size_write(inode, 0);
1686 truncate_pagecache(inode, 0);
1692 /* Flush dirty data/metadata before non-truncate SETATTR */
1695 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1697 err = write_inode_now(inode, true);
1701 fuse_set_nowrite(inode);
1702 fuse_release_nowrite(inode);
1706 fuse_set_nowrite(inode);
1707 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1708 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1709 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1712 memset(&inarg, 0, sizeof(inarg));
1713 memset(&outarg, 0, sizeof(outarg));
1714 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1716 struct fuse_file *ff = file->private_data;
1717 inarg.valid |= FATTR_FH;
1721 /* Kill suid/sgid for non-directory chown unconditionally */
1722 if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
1723 attr->ia_valid & (ATTR_UID | ATTR_GID))
1724 inarg.valid |= FATTR_KILL_SUIDGID;
1726 if (attr->ia_valid & ATTR_SIZE) {
1727 /* For mandatory locking in truncate */
1728 inarg.valid |= FATTR_LOCKOWNER;
1729 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1731 /* Kill suid/sgid for truncate only if no CAP_FSETID */
1732 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1733 inarg.valid |= FATTR_KILL_SUIDGID;
1735 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1736 err = fuse_simple_request(fm, &args);
1739 fuse_invalidate_attr(inode);
1743 if (fuse_invalid_attr(&outarg.attr) ||
1744 inode_wrong_type(inode, outarg.attr.mode)) {
1745 fuse_make_bad(inode);
1750 spin_lock(&fi->lock);
1751 /* the kernel maintains i_mtime locally */
1752 if (trust_local_cmtime) {
1753 if (attr->ia_valid & ATTR_MTIME)
1754 inode->i_mtime = attr->ia_mtime;
1755 if (attr->ia_valid & ATTR_CTIME)
1756 inode->i_ctime = attr->ia_ctime;
1757 /* FIXME: clear I_DIRTY_SYNC? */
1760 fuse_change_attributes_common(inode, &outarg.attr,
1761 attr_timeout(&outarg),
1762 fuse_get_cache_mask(inode));
1763 oldsize = inode->i_size;
1764 /* see the comment in fuse_change_attributes() */
1765 if (!is_wb || is_truncate)
1766 i_size_write(inode, outarg.attr.size);
1769 /* NOTE: this may release/reacquire fi->lock */
1770 __fuse_release_nowrite(inode);
1772 spin_unlock(&fi->lock);
1775 * Only call invalidate_inode_pages2() after removing
1776 * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock.
1778 if ((is_truncate || !is_wb) &&
1779 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1780 truncate_pagecache(inode, outarg.attr.size);
1781 invalidate_inode_pages2(mapping);
1784 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1787 filemap_invalidate_unlock(mapping);
1793 fuse_release_nowrite(inode);
1795 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1798 filemap_invalidate_unlock(mapping);
1802 static int fuse_setattr(struct user_namespace *mnt_userns, struct dentry *entry,
1805 struct inode *inode = d_inode(entry);
1806 struct fuse_conn *fc = get_fuse_conn(inode);
1807 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1810 if (fuse_is_bad(inode))
1813 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1816 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1817 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1821 * The only sane way to reliably kill suid/sgid is to do it in
1822 * the userspace filesystem
1824 * This should be done on write(), truncate() and chown().
1826 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
1828 * ia_mode calculation may have used stale i_mode.
1829 * Refresh and recalculate.
1831 ret = fuse_do_getattr(inode, NULL, file);
1835 attr->ia_mode = inode->i_mode;
1836 if (inode->i_mode & S_ISUID) {
1837 attr->ia_valid |= ATTR_MODE;
1838 attr->ia_mode &= ~S_ISUID;
1840 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1841 attr->ia_valid |= ATTR_MODE;
1842 attr->ia_mode &= ~S_ISGID;
1846 if (!attr->ia_valid)
1849 ret = fuse_do_setattr(entry, attr, file);
1852 * If filesystem supports acls it may have updated acl xattrs in
1853 * the filesystem, so forget cached acls for the inode.
1856 forget_all_cached_acls(inode);
1858 /* Directory mode changed, may need to revalidate access */
1859 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1860 fuse_invalidate_entry_cache(entry);
1865 static int fuse_getattr(struct user_namespace *mnt_userns,
1866 const struct path *path, struct kstat *stat,
1867 u32 request_mask, unsigned int flags)
1869 struct inode *inode = d_inode(path->dentry);
1870 struct fuse_conn *fc = get_fuse_conn(inode);
1872 if (fuse_is_bad(inode))
1875 if (!fuse_allow_current_process(fc)) {
1876 if (!request_mask) {
1878 * If user explicitly requested *nothing* then don't
1879 * error out, but return st_dev only.
1881 stat->result_mask = 0;
1882 stat->dev = inode->i_sb->s_dev;
1888 return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1891 static const struct inode_operations fuse_dir_inode_operations = {
1892 .lookup = fuse_lookup,
1893 .mkdir = fuse_mkdir,
1894 .symlink = fuse_symlink,
1895 .unlink = fuse_unlink,
1896 .rmdir = fuse_rmdir,
1897 .rename = fuse_rename2,
1899 .setattr = fuse_setattr,
1900 .create = fuse_create,
1901 .atomic_open = fuse_atomic_open,
1902 .mknod = fuse_mknod,
1903 .permission = fuse_permission,
1904 .getattr = fuse_getattr,
1905 .listxattr = fuse_listxattr,
1906 .get_acl = fuse_get_acl,
1907 .set_acl = fuse_set_acl,
1908 .fileattr_get = fuse_fileattr_get,
1909 .fileattr_set = fuse_fileattr_set,
1912 static const struct file_operations fuse_dir_operations = {
1913 .llseek = generic_file_llseek,
1914 .read = generic_read_dir,
1915 .iterate_shared = fuse_readdir,
1916 .open = fuse_dir_open,
1917 .release = fuse_dir_release,
1918 .fsync = fuse_dir_fsync,
1919 .unlocked_ioctl = fuse_dir_ioctl,
1920 .compat_ioctl = fuse_dir_compat_ioctl,
1923 static const struct inode_operations fuse_common_inode_operations = {
1924 .setattr = fuse_setattr,
1925 .permission = fuse_permission,
1926 .getattr = fuse_getattr,
1927 .listxattr = fuse_listxattr,
1928 .get_acl = fuse_get_acl,
1929 .set_acl = fuse_set_acl,
1930 .fileattr_get = fuse_fileattr_get,
1931 .fileattr_set = fuse_fileattr_set,
1934 static const struct inode_operations fuse_symlink_inode_operations = {
1935 .setattr = fuse_setattr,
1936 .get_link = fuse_get_link,
1937 .getattr = fuse_getattr,
1938 .listxattr = fuse_listxattr,
1941 void fuse_init_common(struct inode *inode)
1943 inode->i_op = &fuse_common_inode_operations;
1946 void fuse_init_dir(struct inode *inode)
1948 struct fuse_inode *fi = get_fuse_inode(inode);
1950 inode->i_op = &fuse_dir_inode_operations;
1951 inode->i_fop = &fuse_dir_operations;
1953 spin_lock_init(&fi->rdc.lock);
1954 fi->rdc.cached = false;
1957 fi->rdc.version = 0;
1960 static int fuse_symlink_readpage(struct file *null, struct page *page)
1962 int err = fuse_readlink_page(page->mapping->host, page);
1965 SetPageUptodate(page);
1972 static const struct address_space_operations fuse_symlink_aops = {
1973 .readpage = fuse_symlink_readpage,
1976 void fuse_init_symlink(struct inode *inode)
1978 inode->i_op = &fuse_symlink_inode_operations;
1979 inode->i_data.a_ops = &fuse_symlink_aops;
1980 inode_nohighmem(inode);