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 fuse_stale_inode(inode, outarg.generation, &outarg.attr))
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 vfsmount *mnt;
313 struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
315 fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
317 return ERR_CAST(fsc);
319 /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */
320 fsc->fs_private = mp_fi;
322 /* Create the submount */
331 const struct dentry_operations fuse_dentry_operations = {
332 .d_revalidate = fuse_dentry_revalidate,
333 .d_delete = fuse_dentry_delete,
334 #if BITS_PER_LONG < 64
335 .d_init = fuse_dentry_init,
336 .d_release = fuse_dentry_release,
338 .d_automount = fuse_dentry_automount,
341 const struct dentry_operations fuse_root_dentry_operations = {
342 #if BITS_PER_LONG < 64
343 .d_init = fuse_dentry_init,
344 .d_release = fuse_dentry_release,
348 int fuse_valid_type(int m)
350 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
351 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
354 bool fuse_invalid_attr(struct fuse_attr *attr)
356 return !fuse_valid_type(attr->mode) ||
357 attr->size > LLONG_MAX;
360 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
361 struct fuse_entry_out *outarg, struct inode **inode)
363 struct fuse_mount *fm = get_fuse_mount_super(sb);
365 struct fuse_forget_link *forget;
371 if (name->len > FUSE_NAME_MAX)
375 forget = fuse_alloc_forget();
380 attr_version = fuse_get_attr_version(fm->fc);
382 fuse_lookup_init(fm->fc, &args, nodeid, name, outarg);
383 err = fuse_simple_request(fm, &args);
384 /* Zero nodeid is same as -ENOENT, but with valid timeout */
385 if (err || !outarg->nodeid)
391 if (fuse_invalid_attr(&outarg->attr))
394 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
395 &outarg->attr, entry_attr_timeout(outarg),
399 fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1);
410 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
414 struct fuse_entry_out outarg;
416 struct dentry *newent;
417 bool outarg_valid = true;
420 if (fuse_is_bad(dir))
421 return ERR_PTR(-EIO);
423 locked = fuse_lock_inode(dir);
424 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
426 fuse_unlock_inode(dir, locked);
427 if (err == -ENOENT) {
428 outarg_valid = false;
435 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
438 newent = d_splice_alias(inode, entry);
439 err = PTR_ERR(newent);
443 entry = newent ? newent : entry;
445 fuse_change_entry_timeout(entry, &outarg);
447 fuse_invalidate_entry_cache(entry);
450 fuse_advise_use_readdirplus(dir);
460 * Atomic create+open operation
462 * If the filesystem doesn't support this, then fall back to separate
463 * 'mknod' + 'open' requests.
465 static int fuse_create_open(struct inode *dir, struct dentry *entry,
466 struct file *file, unsigned int flags,
471 struct fuse_mount *fm = get_fuse_mount(dir);
473 struct fuse_forget_link *forget;
474 struct fuse_create_in inarg;
475 struct fuse_open_out outopen;
476 struct fuse_entry_out outentry;
477 struct fuse_inode *fi;
478 struct fuse_file *ff;
480 /* Userspace expects S_IFREG in create mode */
481 BUG_ON((mode & S_IFMT) != S_IFREG);
483 forget = fuse_alloc_forget();
489 ff = fuse_file_alloc(fm);
491 goto out_put_forget_req;
493 if (!fm->fc->dont_mask)
494 mode &= ~current_umask();
497 memset(&inarg, 0, sizeof(inarg));
498 memset(&outentry, 0, sizeof(outentry));
501 inarg.umask = current_umask();
503 if (fm->fc->handle_killpriv_v2 && (flags & O_TRUNC) &&
504 !(flags & O_EXCL) && !capable(CAP_FSETID)) {
505 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
508 args.opcode = FUSE_CREATE;
509 args.nodeid = get_node_id(dir);
511 args.in_args[0].size = sizeof(inarg);
512 args.in_args[0].value = &inarg;
513 args.in_args[1].size = entry->d_name.len + 1;
514 args.in_args[1].value = entry->d_name.name;
515 args.out_numargs = 2;
516 args.out_args[0].size = sizeof(outentry);
517 args.out_args[0].value = &outentry;
518 args.out_args[1].size = sizeof(outopen);
519 args.out_args[1].value = &outopen;
520 err = fuse_simple_request(fm, &args);
525 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
526 fuse_invalid_attr(&outentry.attr))
530 ff->nodeid = outentry.nodeid;
531 ff->open_flags = outopen.open_flags;
532 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
533 &outentry.attr, entry_attr_timeout(&outentry), 0);
535 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
536 fuse_sync_release(NULL, ff, flags);
537 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
542 d_instantiate(entry, inode);
543 fuse_change_entry_timeout(entry, &outentry);
544 fuse_dir_changed(dir);
545 err = finish_open(file, entry, generic_file_open);
547 fi = get_fuse_inode(inode);
548 fuse_sync_release(fi, ff, flags);
550 file->private_data = ff;
551 fuse_finish_open(inode, file);
563 static int fuse_mknod(struct user_namespace *, struct inode *, struct dentry *,
565 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
566 struct file *file, unsigned flags,
570 struct fuse_conn *fc = get_fuse_conn(dir);
571 struct dentry *res = NULL;
573 if (fuse_is_bad(dir))
576 if (d_in_lookup(entry)) {
577 res = fuse_lookup(dir, entry, 0);
585 if (!(flags & O_CREAT) || d_really_is_positive(entry))
589 file->f_mode |= FMODE_CREATED;
594 err = fuse_create_open(dir, entry, file, flags, mode);
595 if (err == -ENOSYS) {
604 err = fuse_mknod(&init_user_ns, dir, entry, mode, 0);
608 return finish_no_open(file, res);
612 * Code shared between mknod, mkdir, symlink and link
614 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
615 struct inode *dir, struct dentry *entry,
618 struct fuse_entry_out outarg;
622 struct fuse_forget_link *forget;
624 if (fuse_is_bad(dir))
627 forget = fuse_alloc_forget();
631 memset(&outarg, 0, sizeof(outarg));
632 args->nodeid = get_node_id(dir);
633 args->out_numargs = 1;
634 args->out_args[0].size = sizeof(outarg);
635 args->out_args[0].value = &outarg;
636 err = fuse_simple_request(fm, args);
638 goto out_put_forget_req;
641 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
642 goto out_put_forget_req;
644 if ((outarg.attr.mode ^ mode) & S_IFMT)
645 goto out_put_forget_req;
647 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
648 &outarg.attr, entry_attr_timeout(&outarg), 0);
650 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
656 d = d_splice_alias(inode, entry);
661 fuse_change_entry_timeout(d, &outarg);
664 fuse_change_entry_timeout(entry, &outarg);
666 fuse_dir_changed(dir);
674 static int fuse_mknod(struct user_namespace *mnt_userns, struct inode *dir,
675 struct dentry *entry, umode_t mode, dev_t rdev)
677 struct fuse_mknod_in inarg;
678 struct fuse_mount *fm = get_fuse_mount(dir);
681 if (!fm->fc->dont_mask)
682 mode &= ~current_umask();
684 memset(&inarg, 0, sizeof(inarg));
686 inarg.rdev = new_encode_dev(rdev);
687 inarg.umask = current_umask();
688 args.opcode = FUSE_MKNOD;
690 args.in_args[0].size = sizeof(inarg);
691 args.in_args[0].value = &inarg;
692 args.in_args[1].size = entry->d_name.len + 1;
693 args.in_args[1].value = entry->d_name.name;
694 return create_new_entry(fm, &args, dir, entry, mode);
697 static int fuse_create(struct user_namespace *mnt_userns, struct inode *dir,
698 struct dentry *entry, umode_t mode, bool excl)
700 return fuse_mknod(&init_user_ns, dir, entry, mode, 0);
703 static int fuse_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
704 struct dentry *entry, umode_t mode)
706 struct fuse_mkdir_in inarg;
707 struct fuse_mount *fm = get_fuse_mount(dir);
710 if (!fm->fc->dont_mask)
711 mode &= ~current_umask();
713 memset(&inarg, 0, sizeof(inarg));
715 inarg.umask = current_umask();
716 args.opcode = FUSE_MKDIR;
718 args.in_args[0].size = sizeof(inarg);
719 args.in_args[0].value = &inarg;
720 args.in_args[1].size = entry->d_name.len + 1;
721 args.in_args[1].value = entry->d_name.name;
722 return create_new_entry(fm, &args, dir, entry, S_IFDIR);
725 static int fuse_symlink(struct user_namespace *mnt_userns, struct inode *dir,
726 struct dentry *entry, const char *link)
728 struct fuse_mount *fm = get_fuse_mount(dir);
729 unsigned len = strlen(link) + 1;
732 args.opcode = FUSE_SYMLINK;
734 args.in_args[0].size = entry->d_name.len + 1;
735 args.in_args[0].value = entry->d_name.name;
736 args.in_args[1].size = len;
737 args.in_args[1].value = link;
738 return create_new_entry(fm, &args, dir, entry, S_IFLNK);
741 void fuse_update_ctime(struct inode *inode)
743 if (!IS_NOCMTIME(inode)) {
744 inode->i_ctime = current_time(inode);
745 mark_inode_dirty_sync(inode);
749 static int fuse_unlink(struct inode *dir, struct dentry *entry)
752 struct fuse_mount *fm = get_fuse_mount(dir);
755 if (fuse_is_bad(dir))
758 args.opcode = FUSE_UNLINK;
759 args.nodeid = get_node_id(dir);
761 args.in_args[0].size = entry->d_name.len + 1;
762 args.in_args[0].value = entry->d_name.name;
763 err = fuse_simple_request(fm, &args);
765 struct inode *inode = d_inode(entry);
766 struct fuse_inode *fi = get_fuse_inode(inode);
768 spin_lock(&fi->lock);
769 fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
771 * If i_nlink == 0 then unlink doesn't make sense, yet this can
772 * happen if userspace filesystem is careless. It would be
773 * difficult to enforce correct nlink usage so just ignore this
776 if (inode->i_nlink > 0)
778 spin_unlock(&fi->lock);
779 fuse_invalidate_attr(inode);
780 fuse_dir_changed(dir);
781 fuse_invalidate_entry_cache(entry);
782 fuse_update_ctime(inode);
783 } else if (err == -EINTR)
784 fuse_invalidate_entry(entry);
788 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
791 struct fuse_mount *fm = get_fuse_mount(dir);
794 if (fuse_is_bad(dir))
797 args.opcode = FUSE_RMDIR;
798 args.nodeid = get_node_id(dir);
800 args.in_args[0].size = entry->d_name.len + 1;
801 args.in_args[0].value = entry->d_name.name;
802 err = fuse_simple_request(fm, &args);
804 clear_nlink(d_inode(entry));
805 fuse_dir_changed(dir);
806 fuse_invalidate_entry_cache(entry);
807 } else if (err == -EINTR)
808 fuse_invalidate_entry(entry);
812 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
813 struct inode *newdir, struct dentry *newent,
814 unsigned int flags, int opcode, size_t argsize)
817 struct fuse_rename2_in inarg;
818 struct fuse_mount *fm = get_fuse_mount(olddir);
821 memset(&inarg, 0, argsize);
822 inarg.newdir = get_node_id(newdir);
824 args.opcode = opcode;
825 args.nodeid = get_node_id(olddir);
827 args.in_args[0].size = argsize;
828 args.in_args[0].value = &inarg;
829 args.in_args[1].size = oldent->d_name.len + 1;
830 args.in_args[1].value = oldent->d_name.name;
831 args.in_args[2].size = newent->d_name.len + 1;
832 args.in_args[2].value = newent->d_name.name;
833 err = fuse_simple_request(fm, &args);
836 fuse_invalidate_attr(d_inode(oldent));
837 fuse_update_ctime(d_inode(oldent));
839 if (flags & RENAME_EXCHANGE) {
840 fuse_invalidate_attr(d_inode(newent));
841 fuse_update_ctime(d_inode(newent));
844 fuse_dir_changed(olddir);
845 if (olddir != newdir)
846 fuse_dir_changed(newdir);
848 /* newent will end up negative */
849 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
850 fuse_invalidate_attr(d_inode(newent));
851 fuse_invalidate_entry_cache(newent);
852 fuse_update_ctime(d_inode(newent));
854 } else if (err == -EINTR) {
855 /* If request was interrupted, DEITY only knows if the
856 rename actually took place. If the invalidation
857 fails (e.g. some process has CWD under the renamed
858 directory), then there can be inconsistency between
859 the dcache and the real filesystem. Tough luck. */
860 fuse_invalidate_entry(oldent);
861 if (d_really_is_positive(newent))
862 fuse_invalidate_entry(newent);
868 static int fuse_rename2(struct user_namespace *mnt_userns, struct inode *olddir,
869 struct dentry *oldent, struct inode *newdir,
870 struct dentry *newent, unsigned int flags)
872 struct fuse_conn *fc = get_fuse_conn(olddir);
875 if (fuse_is_bad(olddir))
878 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
882 if (fc->no_rename2 || fc->minor < 23)
885 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
887 sizeof(struct fuse_rename2_in));
888 if (err == -ENOSYS) {
893 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
895 sizeof(struct fuse_rename_in));
901 static int fuse_link(struct dentry *entry, struct inode *newdir,
902 struct dentry *newent)
905 struct fuse_link_in inarg;
906 struct inode *inode = d_inode(entry);
907 struct fuse_mount *fm = get_fuse_mount(inode);
910 memset(&inarg, 0, sizeof(inarg));
911 inarg.oldnodeid = get_node_id(inode);
912 args.opcode = FUSE_LINK;
914 args.in_args[0].size = sizeof(inarg);
915 args.in_args[0].value = &inarg;
916 args.in_args[1].size = newent->d_name.len + 1;
917 args.in_args[1].value = newent->d_name.name;
918 err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
919 /* Contrary to "normal" filesystems it can happen that link
920 makes two "logical" inodes point to the same "physical"
921 inode. We invalidate the attributes of the old one, so it
922 will reflect changes in the backing inode (link count,
926 struct fuse_inode *fi = get_fuse_inode(inode);
928 spin_lock(&fi->lock);
929 fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
930 if (likely(inode->i_nlink < UINT_MAX))
932 spin_unlock(&fi->lock);
933 fuse_invalidate_attr(inode);
934 fuse_update_ctime(inode);
935 } else if (err == -EINTR) {
936 fuse_invalidate_attr(inode);
941 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
944 unsigned int blkbits;
945 struct fuse_conn *fc = get_fuse_conn(inode);
947 /* see the comment in fuse_change_attributes() */
948 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
949 attr->size = i_size_read(inode);
950 attr->mtime = inode->i_mtime.tv_sec;
951 attr->mtimensec = inode->i_mtime.tv_nsec;
952 attr->ctime = inode->i_ctime.tv_sec;
953 attr->ctimensec = inode->i_ctime.tv_nsec;
956 stat->dev = inode->i_sb->s_dev;
957 stat->ino = attr->ino;
958 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
959 stat->nlink = attr->nlink;
960 stat->uid = make_kuid(fc->user_ns, attr->uid);
961 stat->gid = make_kgid(fc->user_ns, attr->gid);
962 stat->rdev = inode->i_rdev;
963 stat->atime.tv_sec = attr->atime;
964 stat->atime.tv_nsec = attr->atimensec;
965 stat->mtime.tv_sec = attr->mtime;
966 stat->mtime.tv_nsec = attr->mtimensec;
967 stat->ctime.tv_sec = attr->ctime;
968 stat->ctime.tv_nsec = attr->ctimensec;
969 stat->size = attr->size;
970 stat->blocks = attr->blocks;
972 if (attr->blksize != 0)
973 blkbits = ilog2(attr->blksize);
975 blkbits = inode->i_sb->s_blocksize_bits;
977 stat->blksize = 1 << blkbits;
980 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
984 struct fuse_getattr_in inarg;
985 struct fuse_attr_out outarg;
986 struct fuse_mount *fm = get_fuse_mount(inode);
990 attr_version = fuse_get_attr_version(fm->fc);
992 memset(&inarg, 0, sizeof(inarg));
993 memset(&outarg, 0, sizeof(outarg));
994 /* Directories have separate file-handle space */
995 if (file && S_ISREG(inode->i_mode)) {
996 struct fuse_file *ff = file->private_data;
998 inarg.getattr_flags |= FUSE_GETATTR_FH;
1001 args.opcode = FUSE_GETATTR;
1002 args.nodeid = get_node_id(inode);
1003 args.in_numargs = 1;
1004 args.in_args[0].size = sizeof(inarg);
1005 args.in_args[0].value = &inarg;
1006 args.out_numargs = 1;
1007 args.out_args[0].size = sizeof(outarg);
1008 args.out_args[0].value = &outarg;
1009 err = fuse_simple_request(fm, &args);
1011 if (fuse_invalid_attr(&outarg.attr) ||
1012 inode_wrong_type(inode, outarg.attr.mode)) {
1013 fuse_make_bad(inode);
1016 fuse_change_attributes(inode, &outarg.attr,
1017 attr_timeout(&outarg),
1020 fuse_fillattr(inode, &outarg.attr, stat);
1026 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1027 struct kstat *stat, u32 request_mask,
1030 struct fuse_inode *fi = get_fuse_inode(inode);
1034 if (flags & AT_STATX_FORCE_SYNC)
1036 else if (flags & AT_STATX_DONT_SYNC)
1038 else if (request_mask & READ_ONCE(fi->inval_mask))
1041 sync = time_before64(fi->i_time, get_jiffies_64());
1044 forget_all_cached_acls(inode);
1045 err = fuse_do_getattr(inode, stat, file);
1047 generic_fillattr(&init_user_ns, inode, stat);
1048 stat->mode = fi->orig_i_mode;
1049 stat->ino = fi->orig_ino;
1055 int fuse_update_attributes(struct inode *inode, struct file *file)
1057 /* Do *not* need to get atime for internal purposes */
1058 return fuse_update_get_attr(inode, file, NULL,
1059 STATX_BASIC_STATS & ~STATX_ATIME, 0);
1062 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1063 u64 child_nodeid, struct qstr *name)
1066 struct inode *parent;
1068 struct dentry *entry;
1070 parent = fuse_ilookup(fc, parent_nodeid, NULL);
1075 if (!S_ISDIR(parent->i_mode))
1079 dir = d_find_alias(parent);
1083 name->hash = full_name_hash(dir, name->name, name->len);
1084 entry = d_lookup(dir, name);
1089 fuse_dir_changed(parent);
1090 fuse_invalidate_entry(entry);
1092 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1093 inode_lock(d_inode(entry));
1094 if (get_node_id(d_inode(entry)) != child_nodeid) {
1098 if (d_mountpoint(entry)) {
1102 if (d_is_dir(entry)) {
1103 shrink_dcache_parent(entry);
1104 if (!simple_empty(entry)) {
1108 d_inode(entry)->i_flags |= S_DEAD;
1111 clear_nlink(d_inode(entry));
1114 inode_unlock(d_inode(entry));
1123 inode_unlock(parent);
1129 * Calling into a user-controlled filesystem gives the filesystem
1130 * daemon ptrace-like capabilities over the current process. This
1131 * means, that the filesystem daemon is able to record the exact
1132 * filesystem operations performed, and can also control the behavior
1133 * of the requester process in otherwise impossible ways. For example
1134 * it can delay the operation for arbitrary length of time allowing
1135 * DoS against the requester.
1137 * For this reason only those processes can call into the filesystem,
1138 * for which the owner of the mount has ptrace privilege. This
1139 * excludes processes started by other users, suid or sgid processes.
1141 int fuse_allow_current_process(struct fuse_conn *fc)
1143 const struct cred *cred;
1145 if (fc->allow_other)
1146 return current_in_userns(fc->user_ns);
1148 cred = current_cred();
1149 if (uid_eq(cred->euid, fc->user_id) &&
1150 uid_eq(cred->suid, fc->user_id) &&
1151 uid_eq(cred->uid, fc->user_id) &&
1152 gid_eq(cred->egid, fc->group_id) &&
1153 gid_eq(cred->sgid, fc->group_id) &&
1154 gid_eq(cred->gid, fc->group_id))
1160 static int fuse_access(struct inode *inode, int mask)
1162 struct fuse_mount *fm = get_fuse_mount(inode);
1164 struct fuse_access_in inarg;
1167 BUG_ON(mask & MAY_NOT_BLOCK);
1169 if (fm->fc->no_access)
1172 memset(&inarg, 0, sizeof(inarg));
1173 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1174 args.opcode = FUSE_ACCESS;
1175 args.nodeid = get_node_id(inode);
1176 args.in_numargs = 1;
1177 args.in_args[0].size = sizeof(inarg);
1178 args.in_args[0].value = &inarg;
1179 err = fuse_simple_request(fm, &args);
1180 if (err == -ENOSYS) {
1181 fm->fc->no_access = 1;
1187 static int fuse_perm_getattr(struct inode *inode, int mask)
1189 if (mask & MAY_NOT_BLOCK)
1192 forget_all_cached_acls(inode);
1193 return fuse_do_getattr(inode, NULL, NULL);
1197 * Check permission. The two basic access models of FUSE are:
1199 * 1) Local access checking ('default_permissions' mount option) based
1200 * on file mode. This is the plain old disk filesystem permission
1203 * 2) "Remote" access checking, where server is responsible for
1204 * checking permission in each inode operation. An exception to this
1205 * is if ->permission() was invoked from sys_access() in which case an
1206 * access request is sent. Execute permission is still checked
1207 * locally based on file mode.
1209 static int fuse_permission(struct user_namespace *mnt_userns,
1210 struct inode *inode, int mask)
1212 struct fuse_conn *fc = get_fuse_conn(inode);
1213 bool refreshed = false;
1216 if (fuse_is_bad(inode))
1219 if (!fuse_allow_current_process(fc))
1223 * If attributes are needed, refresh them before proceeding
1225 if (fc->default_permissions ||
1226 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1227 struct fuse_inode *fi = get_fuse_inode(inode);
1228 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1230 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1231 time_before64(fi->i_time, get_jiffies_64())) {
1234 err = fuse_perm_getattr(inode, mask);
1240 if (fc->default_permissions) {
1241 err = generic_permission(&init_user_ns, inode, mask);
1243 /* If permission is denied, try to refresh file
1244 attributes. This is also needed, because the root
1245 node will at first have no permissions */
1246 if (err == -EACCES && !refreshed) {
1247 err = fuse_perm_getattr(inode, mask);
1249 err = generic_permission(&init_user_ns,
1253 /* Note: the opposite of the above test does not
1254 exist. So if permissions are revoked this won't be
1255 noticed immediately, only after the attribute
1256 timeout has expired */
1257 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1258 err = fuse_access(inode, mask);
1259 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1260 if (!(inode->i_mode & S_IXUGO)) {
1264 err = fuse_perm_getattr(inode, mask);
1265 if (!err && !(inode->i_mode & S_IXUGO))
1272 static int fuse_readlink_page(struct inode *inode, struct page *page)
1274 struct fuse_mount *fm = get_fuse_mount(inode);
1275 struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1276 struct fuse_args_pages ap = {
1284 ap.args.opcode = FUSE_READLINK;
1285 ap.args.nodeid = get_node_id(inode);
1286 ap.args.out_pages = true;
1287 ap.args.out_argvar = true;
1288 ap.args.page_zeroing = true;
1289 ap.args.out_numargs = 1;
1290 ap.args.out_args[0].size = desc.length;
1291 res = fuse_simple_request(fm, &ap.args);
1293 fuse_invalidate_atime(inode);
1298 if (WARN_ON(res >= PAGE_SIZE))
1301 link = page_address(page);
1307 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1308 struct delayed_call *callback)
1310 struct fuse_conn *fc = get_fuse_conn(inode);
1315 if (fuse_is_bad(inode))
1318 if (fc->cache_symlinks)
1319 return page_get_link(dentry, inode, callback);
1325 page = alloc_page(GFP_KERNEL);
1330 err = fuse_readlink_page(inode, page);
1336 set_delayed_call(callback, page_put_link, page);
1338 return page_address(page);
1341 return ERR_PTR(err);
1344 static int fuse_dir_open(struct inode *inode, struct file *file)
1346 return fuse_open_common(inode, file, true);
1349 static int fuse_dir_release(struct inode *inode, struct file *file)
1351 fuse_release_common(file, true);
1356 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1359 struct inode *inode = file->f_mapping->host;
1360 struct fuse_conn *fc = get_fuse_conn(inode);
1363 if (fuse_is_bad(inode))
1366 if (fc->no_fsyncdir)
1370 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1371 if (err == -ENOSYS) {
1372 fc->no_fsyncdir = 1;
1375 inode_unlock(inode);
1380 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1383 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1385 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1389 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1392 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1395 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1400 return fuse_ioctl_common(file, cmd, arg,
1401 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1404 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1406 /* Always update if mtime is explicitly set */
1407 if (ivalid & ATTR_MTIME_SET)
1410 /* Or if kernel i_mtime is the official one */
1411 if (trust_local_mtime)
1414 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1415 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1418 /* In all other cases update */
1422 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1423 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1425 unsigned ivalid = iattr->ia_valid;
1427 if (ivalid & ATTR_MODE)
1428 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1429 if (ivalid & ATTR_UID)
1430 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1431 if (ivalid & ATTR_GID)
1432 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1433 if (ivalid & ATTR_SIZE)
1434 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1435 if (ivalid & ATTR_ATIME) {
1436 arg->valid |= FATTR_ATIME;
1437 arg->atime = iattr->ia_atime.tv_sec;
1438 arg->atimensec = iattr->ia_atime.tv_nsec;
1439 if (!(ivalid & ATTR_ATIME_SET))
1440 arg->valid |= FATTR_ATIME_NOW;
1442 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1443 arg->valid |= FATTR_MTIME;
1444 arg->mtime = iattr->ia_mtime.tv_sec;
1445 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1446 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1447 arg->valid |= FATTR_MTIME_NOW;
1449 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1450 arg->valid |= FATTR_CTIME;
1451 arg->ctime = iattr->ia_ctime.tv_sec;
1452 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1457 * Prevent concurrent writepages on inode
1459 * This is done by adding a negative bias to the inode write counter
1460 * and waiting for all pending writes to finish.
1462 void fuse_set_nowrite(struct inode *inode)
1464 struct fuse_inode *fi = get_fuse_inode(inode);
1466 BUG_ON(!inode_is_locked(inode));
1468 spin_lock(&fi->lock);
1469 BUG_ON(fi->writectr < 0);
1470 fi->writectr += FUSE_NOWRITE;
1471 spin_unlock(&fi->lock);
1472 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1476 * Allow writepages on inode
1478 * Remove the bias from the writecounter and send any queued
1481 static void __fuse_release_nowrite(struct inode *inode)
1483 struct fuse_inode *fi = get_fuse_inode(inode);
1485 BUG_ON(fi->writectr != FUSE_NOWRITE);
1487 fuse_flush_writepages(inode);
1490 void fuse_release_nowrite(struct inode *inode)
1492 struct fuse_inode *fi = get_fuse_inode(inode);
1494 spin_lock(&fi->lock);
1495 __fuse_release_nowrite(inode);
1496 spin_unlock(&fi->lock);
1499 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1500 struct inode *inode,
1501 struct fuse_setattr_in *inarg_p,
1502 struct fuse_attr_out *outarg_p)
1504 args->opcode = FUSE_SETATTR;
1505 args->nodeid = get_node_id(inode);
1506 args->in_numargs = 1;
1507 args->in_args[0].size = sizeof(*inarg_p);
1508 args->in_args[0].value = inarg_p;
1509 args->out_numargs = 1;
1510 args->out_args[0].size = sizeof(*outarg_p);
1511 args->out_args[0].value = outarg_p;
1515 * Flush inode->i_mtime to the server
1517 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1519 struct fuse_mount *fm = get_fuse_mount(inode);
1521 struct fuse_setattr_in inarg;
1522 struct fuse_attr_out outarg;
1524 memset(&inarg, 0, sizeof(inarg));
1525 memset(&outarg, 0, sizeof(outarg));
1527 inarg.valid = FATTR_MTIME;
1528 inarg.mtime = inode->i_mtime.tv_sec;
1529 inarg.mtimensec = inode->i_mtime.tv_nsec;
1530 if (fm->fc->minor >= 23) {
1531 inarg.valid |= FATTR_CTIME;
1532 inarg.ctime = inode->i_ctime.tv_sec;
1533 inarg.ctimensec = inode->i_ctime.tv_nsec;
1536 inarg.valid |= FATTR_FH;
1539 fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1541 return fuse_simple_request(fm, &args);
1545 * Set attributes, and at the same time refresh them.
1547 * Truncation is slightly complicated, because the 'truncate' request
1548 * may fail, in which case we don't want to touch the mapping.
1549 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1550 * and the actual truncation by hand.
1552 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1555 struct inode *inode = d_inode(dentry);
1556 struct fuse_mount *fm = get_fuse_mount(inode);
1557 struct fuse_conn *fc = fm->fc;
1558 struct fuse_inode *fi = get_fuse_inode(inode);
1559 struct address_space *mapping = inode->i_mapping;
1561 struct fuse_setattr_in inarg;
1562 struct fuse_attr_out outarg;
1563 bool is_truncate = false;
1564 bool is_wb = fc->writeback_cache;
1567 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1568 bool fault_blocked = false;
1570 if (!fc->default_permissions)
1571 attr->ia_valid |= ATTR_FORCE;
1573 err = setattr_prepare(&init_user_ns, dentry, attr);
1577 if (attr->ia_valid & ATTR_SIZE) {
1578 if (WARN_ON(!S_ISREG(inode->i_mode)))
1583 if (FUSE_IS_DAX(inode) && is_truncate) {
1584 filemap_invalidate_lock(mapping);
1585 fault_blocked = true;
1586 err = fuse_dax_break_layouts(inode, 0, 0);
1588 filemap_invalidate_unlock(mapping);
1593 if (attr->ia_valid & ATTR_OPEN) {
1594 /* This is coming from open(..., ... | O_TRUNC); */
1595 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1596 WARN_ON(attr->ia_size != 0);
1597 if (fc->atomic_o_trunc) {
1599 * No need to send request to userspace, since actual
1600 * truncation has already been done by OPEN. But still
1601 * need to truncate page cache.
1603 i_size_write(inode, 0);
1604 truncate_pagecache(inode, 0);
1610 /* Flush dirty data/metadata before non-truncate SETATTR */
1611 if (is_wb && S_ISREG(inode->i_mode) &&
1613 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1615 err = write_inode_now(inode, true);
1619 fuse_set_nowrite(inode);
1620 fuse_release_nowrite(inode);
1624 fuse_set_nowrite(inode);
1625 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1626 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1627 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1630 memset(&inarg, 0, sizeof(inarg));
1631 memset(&outarg, 0, sizeof(outarg));
1632 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1634 struct fuse_file *ff = file->private_data;
1635 inarg.valid |= FATTR_FH;
1639 /* Kill suid/sgid for non-directory chown unconditionally */
1640 if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
1641 attr->ia_valid & (ATTR_UID | ATTR_GID))
1642 inarg.valid |= FATTR_KILL_SUIDGID;
1644 if (attr->ia_valid & ATTR_SIZE) {
1645 /* For mandatory locking in truncate */
1646 inarg.valid |= FATTR_LOCKOWNER;
1647 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1649 /* Kill suid/sgid for truncate only if no CAP_FSETID */
1650 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1651 inarg.valid |= FATTR_KILL_SUIDGID;
1653 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1654 err = fuse_simple_request(fm, &args);
1657 fuse_invalidate_attr(inode);
1661 if (fuse_invalid_attr(&outarg.attr) ||
1662 inode_wrong_type(inode, outarg.attr.mode)) {
1663 fuse_make_bad(inode);
1668 spin_lock(&fi->lock);
1669 /* the kernel maintains i_mtime locally */
1670 if (trust_local_cmtime) {
1671 if (attr->ia_valid & ATTR_MTIME)
1672 inode->i_mtime = attr->ia_mtime;
1673 if (attr->ia_valid & ATTR_CTIME)
1674 inode->i_ctime = attr->ia_ctime;
1675 /* FIXME: clear I_DIRTY_SYNC? */
1678 fuse_change_attributes_common(inode, &outarg.attr,
1679 attr_timeout(&outarg));
1680 oldsize = inode->i_size;
1681 /* see the comment in fuse_change_attributes() */
1682 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1683 i_size_write(inode, outarg.attr.size);
1686 /* NOTE: this may release/reacquire fi->lock */
1687 __fuse_release_nowrite(inode);
1689 spin_unlock(&fi->lock);
1692 * Only call invalidate_inode_pages2() after removing
1693 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1695 if ((is_truncate || !is_wb) &&
1696 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1697 truncate_pagecache(inode, outarg.attr.size);
1698 invalidate_inode_pages2(mapping);
1701 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1704 filemap_invalidate_unlock(mapping);
1710 fuse_release_nowrite(inode);
1712 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1715 filemap_invalidate_unlock(mapping);
1719 static int fuse_setattr(struct user_namespace *mnt_userns, struct dentry *entry,
1722 struct inode *inode = d_inode(entry);
1723 struct fuse_conn *fc = get_fuse_conn(inode);
1724 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1727 if (fuse_is_bad(inode))
1730 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1733 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1734 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1738 * The only sane way to reliably kill suid/sgid is to do it in
1739 * the userspace filesystem
1741 * This should be done on write(), truncate() and chown().
1743 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
1745 * ia_mode calculation may have used stale i_mode.
1746 * Refresh and recalculate.
1748 ret = fuse_do_getattr(inode, NULL, file);
1752 attr->ia_mode = inode->i_mode;
1753 if (inode->i_mode & S_ISUID) {
1754 attr->ia_valid |= ATTR_MODE;
1755 attr->ia_mode &= ~S_ISUID;
1757 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1758 attr->ia_valid |= ATTR_MODE;
1759 attr->ia_mode &= ~S_ISGID;
1763 if (!attr->ia_valid)
1766 ret = fuse_do_setattr(entry, attr, file);
1769 * If filesystem supports acls it may have updated acl xattrs in
1770 * the filesystem, so forget cached acls for the inode.
1773 forget_all_cached_acls(inode);
1775 /* Directory mode changed, may need to revalidate access */
1776 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1777 fuse_invalidate_entry_cache(entry);
1782 static int fuse_getattr(struct user_namespace *mnt_userns,
1783 const struct path *path, struct kstat *stat,
1784 u32 request_mask, unsigned int flags)
1786 struct inode *inode = d_inode(path->dentry);
1787 struct fuse_conn *fc = get_fuse_conn(inode);
1789 if (fuse_is_bad(inode))
1792 if (!fuse_allow_current_process(fc)) {
1793 if (!request_mask) {
1795 * If user explicitly requested *nothing* then don't
1796 * error out, but return st_dev only.
1798 stat->result_mask = 0;
1799 stat->dev = inode->i_sb->s_dev;
1805 return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1808 static const struct inode_operations fuse_dir_inode_operations = {
1809 .lookup = fuse_lookup,
1810 .mkdir = fuse_mkdir,
1811 .symlink = fuse_symlink,
1812 .unlink = fuse_unlink,
1813 .rmdir = fuse_rmdir,
1814 .rename = fuse_rename2,
1816 .setattr = fuse_setattr,
1817 .create = fuse_create,
1818 .atomic_open = fuse_atomic_open,
1819 .mknod = fuse_mknod,
1820 .permission = fuse_permission,
1821 .getattr = fuse_getattr,
1822 .listxattr = fuse_listxattr,
1823 .get_acl = fuse_get_acl,
1824 .set_acl = fuse_set_acl,
1825 .fileattr_get = fuse_fileattr_get,
1826 .fileattr_set = fuse_fileattr_set,
1829 static const struct file_operations fuse_dir_operations = {
1830 .llseek = generic_file_llseek,
1831 .read = generic_read_dir,
1832 .iterate_shared = fuse_readdir,
1833 .open = fuse_dir_open,
1834 .release = fuse_dir_release,
1835 .fsync = fuse_dir_fsync,
1836 .unlocked_ioctl = fuse_dir_ioctl,
1837 .compat_ioctl = fuse_dir_compat_ioctl,
1840 static const struct inode_operations fuse_common_inode_operations = {
1841 .setattr = fuse_setattr,
1842 .permission = fuse_permission,
1843 .getattr = fuse_getattr,
1844 .listxattr = fuse_listxattr,
1845 .get_acl = fuse_get_acl,
1846 .set_acl = fuse_set_acl,
1847 .fileattr_get = fuse_fileattr_get,
1848 .fileattr_set = fuse_fileattr_set,
1851 static const struct inode_operations fuse_symlink_inode_operations = {
1852 .setattr = fuse_setattr,
1853 .get_link = fuse_get_link,
1854 .getattr = fuse_getattr,
1855 .listxattr = fuse_listxattr,
1858 void fuse_init_common(struct inode *inode)
1860 inode->i_op = &fuse_common_inode_operations;
1863 void fuse_init_dir(struct inode *inode)
1865 struct fuse_inode *fi = get_fuse_inode(inode);
1867 inode->i_op = &fuse_dir_inode_operations;
1868 inode->i_fop = &fuse_dir_operations;
1870 spin_lock_init(&fi->rdc.lock);
1871 fi->rdc.cached = false;
1874 fi->rdc.version = 0;
1877 static int fuse_symlink_readpage(struct file *null, struct page *page)
1879 int err = fuse_readlink_page(page->mapping->host, page);
1882 SetPageUptodate(page);
1889 static const struct address_space_operations fuse_symlink_aops = {
1890 .readpage = fuse_symlink_readpage,
1893 void fuse_init_symlink(struct inode *inode)
1895 inode->i_op = &fuse_symlink_inode_operations;
1896 inode->i_data.a_ops = &fuse_symlink_aops;
1897 inode_nohighmem(inode);