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/sched.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 #include <linux/xattr.h>
17 #include <linux/iversion.h>
18 #include <linux/posix_acl.h>
20 static void fuse_advise_use_readdirplus(struct inode *dir)
22 struct fuse_inode *fi = get_fuse_inode(dir);
24 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
32 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
34 ((union fuse_dentry *) entry->d_fsdata)->time = time;
37 static inline u64 fuse_dentry_time(struct dentry *entry)
39 return ((union fuse_dentry *) entry->d_fsdata)->time;
43 * FUSE caches dentries and attributes with separate timeout. The
44 * time in jiffies until the dentry/attributes are valid is stored in
45 * dentry->d_fsdata and fuse_inode->i_time respectively.
49 * Calculate the time in jiffies until a dentry/attributes are valid
51 static u64 time_to_jiffies(u64 sec, u32 nsec)
54 struct timespec64 ts = {
56 min_t(u32, nsec, NSEC_PER_SEC - 1)
59 return get_jiffies_64() + timespec64_to_jiffies(&ts);
65 * Set dentry and possibly attribute timeouts from the lookup/mk*
68 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
70 fuse_dentry_settime(entry,
71 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
74 static u64 attr_timeout(struct fuse_attr_out *o)
76 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
79 u64 entry_attr_timeout(struct fuse_entry_out *o)
81 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
84 static void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
86 set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
90 * Mark the attributes as stale, so that at the next call to
91 * ->getattr() they will be fetched from userspace
93 void fuse_invalidate_attr(struct inode *inode)
95 fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
98 static void fuse_dir_changed(struct inode *dir)
100 fuse_invalidate_attr(dir);
101 inode_maybe_inc_iversion(dir, false);
105 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
108 void fuse_invalidate_atime(struct inode *inode)
110 if (!IS_RDONLY(inode))
111 fuse_invalidate_attr_mask(inode, STATX_ATIME);
115 * Just mark the entry as stale, so that a next attempt to look it up
116 * will result in a new lookup call to userspace
118 * This is called when a dentry is about to become negative and the
119 * timeout is unknown (unlink, rmdir, rename and in some cases
122 void fuse_invalidate_entry_cache(struct dentry *entry)
124 fuse_dentry_settime(entry, 0);
128 * Same as fuse_invalidate_entry_cache(), but also try to remove the
129 * dentry from the hash
131 static void fuse_invalidate_entry(struct dentry *entry)
134 fuse_invalidate_entry_cache(entry);
137 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
138 u64 nodeid, const struct qstr *name,
139 struct fuse_entry_out *outarg)
141 memset(outarg, 0, sizeof(struct fuse_entry_out));
142 args->in.h.opcode = FUSE_LOOKUP;
143 args->in.h.nodeid = nodeid;
144 args->in.numargs = 1;
145 args->in.args[0].size = name->len + 1;
146 args->in.args[0].value = name->name;
147 args->out.numargs = 1;
148 args->out.args[0].size = sizeof(struct fuse_entry_out);
149 args->out.args[0].value = outarg;
152 u64 fuse_get_attr_version(struct fuse_conn *fc)
157 * The spin lock isn't actually needed on 64bit archs, but we
158 * don't yet care too much about such optimizations.
160 spin_lock(&fc->lock);
161 curr_version = fc->attr_version;
162 spin_unlock(&fc->lock);
168 * Check whether the dentry is still valid
170 * If the entry validity timeout has expired and the dentry is
171 * positive, try to redo the lookup. If the lookup results in a
172 * different inode, then let the VFS invalidate the dentry and redo
173 * the lookup once more. If the lookup results in the same inode,
174 * then refresh the attributes, timeouts and mark the dentry valid.
176 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
179 struct dentry *parent;
180 struct fuse_conn *fc;
181 struct fuse_inode *fi;
184 inode = d_inode_rcu(entry);
185 if (inode && is_bad_inode(inode))
187 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
188 (flags & LOOKUP_REVAL)) {
189 struct fuse_entry_out outarg;
191 struct fuse_forget_link *forget;
194 /* For negative dentries, always do a fresh lookup */
199 if (flags & LOOKUP_RCU)
202 fc = get_fuse_conn(inode);
204 forget = fuse_alloc_forget();
209 attr_version = fuse_get_attr_version(fc);
211 parent = dget_parent(entry);
212 fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
213 &entry->d_name, &outarg);
214 ret = fuse_simple_request(fc, &args);
216 /* Zero nodeid is same as -ENOENT */
217 if (!ret && !outarg.nodeid)
220 fi = get_fuse_inode(inode);
221 if (outarg.nodeid != get_node_id(inode)) {
222 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
225 spin_lock(&fc->lock);
227 spin_unlock(&fc->lock);
232 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
235 forget_all_cached_acls(inode);
236 fuse_change_attributes(inode, &outarg.attr,
237 entry_attr_timeout(&outarg),
239 fuse_change_entry_timeout(entry, &outarg);
241 fi = get_fuse_inode(inode);
242 if (flags & LOOKUP_RCU) {
243 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
245 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
246 parent = dget_parent(entry);
247 fuse_advise_use_readdirplus(d_inode(parent));
260 static int fuse_dentry_init(struct dentry *dentry)
262 dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry), GFP_KERNEL);
264 return dentry->d_fsdata ? 0 : -ENOMEM;
266 static void fuse_dentry_release(struct dentry *dentry)
268 union fuse_dentry *fd = dentry->d_fsdata;
273 const struct dentry_operations fuse_dentry_operations = {
274 .d_revalidate = fuse_dentry_revalidate,
275 .d_init = fuse_dentry_init,
276 .d_release = fuse_dentry_release,
279 const struct dentry_operations fuse_root_dentry_operations = {
280 .d_init = fuse_dentry_init,
281 .d_release = fuse_dentry_release,
284 int fuse_valid_type(int m)
286 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
287 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
290 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
291 struct fuse_entry_out *outarg, struct inode **inode)
293 struct fuse_conn *fc = get_fuse_conn_super(sb);
295 struct fuse_forget_link *forget;
301 if (name->len > FUSE_NAME_MAX)
305 forget = fuse_alloc_forget();
310 attr_version = fuse_get_attr_version(fc);
312 fuse_lookup_init(fc, &args, nodeid, name, outarg);
313 err = fuse_simple_request(fc, &args);
314 /* Zero nodeid is same as -ENOENT, but with valid timeout */
315 if (err || !outarg->nodeid)
321 if (!fuse_valid_type(outarg->attr.mode))
324 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
325 &outarg->attr, entry_attr_timeout(outarg),
329 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
340 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
344 struct fuse_entry_out outarg;
346 struct dentry *newent;
347 bool outarg_valid = true;
350 locked = fuse_lock_inode(dir);
351 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
353 fuse_unlock_inode(dir, locked);
354 if (err == -ENOENT) {
355 outarg_valid = false;
362 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
365 newent = d_splice_alias(inode, entry);
366 err = PTR_ERR(newent);
370 entry = newent ? newent : entry;
372 fuse_change_entry_timeout(entry, &outarg);
374 fuse_invalidate_entry_cache(entry);
376 fuse_advise_use_readdirplus(dir);
386 * Atomic create+open operation
388 * If the filesystem doesn't support this, then fall back to separate
389 * 'mknod' + 'open' requests.
391 static int fuse_create_open(struct inode *dir, struct dentry *entry,
392 struct file *file, unsigned flags,
397 struct fuse_conn *fc = get_fuse_conn(dir);
399 struct fuse_forget_link *forget;
400 struct fuse_create_in inarg;
401 struct fuse_open_out outopen;
402 struct fuse_entry_out outentry;
403 struct fuse_file *ff;
405 /* Userspace expects S_IFREG in create mode */
406 BUG_ON((mode & S_IFMT) != S_IFREG);
408 forget = fuse_alloc_forget();
414 ff = fuse_file_alloc(fc);
416 goto out_put_forget_req;
419 mode &= ~current_umask();
422 memset(&inarg, 0, sizeof(inarg));
423 memset(&outentry, 0, sizeof(outentry));
426 inarg.umask = current_umask();
427 args.in.h.opcode = FUSE_CREATE;
428 args.in.h.nodeid = get_node_id(dir);
430 args.in.args[0].size = sizeof(inarg);
431 args.in.args[0].value = &inarg;
432 args.in.args[1].size = entry->d_name.len + 1;
433 args.in.args[1].value = entry->d_name.name;
434 args.out.numargs = 2;
435 args.out.args[0].size = sizeof(outentry);
436 args.out.args[0].value = &outentry;
437 args.out.args[1].size = sizeof(outopen);
438 args.out.args[1].value = &outopen;
439 err = fuse_simple_request(fc, &args);
444 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
448 ff->nodeid = outentry.nodeid;
449 ff->open_flags = outopen.open_flags;
450 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
451 &outentry.attr, entry_attr_timeout(&outentry), 0);
453 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
454 fuse_sync_release(ff, flags);
455 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
460 d_instantiate(entry, inode);
461 fuse_change_entry_timeout(entry, &outentry);
462 fuse_dir_changed(dir);
463 err = finish_open(file, entry, generic_file_open);
465 fuse_sync_release(ff, flags);
467 file->private_data = ff;
468 fuse_finish_open(inode, file);
480 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
481 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
482 struct file *file, unsigned flags,
486 struct fuse_conn *fc = get_fuse_conn(dir);
487 struct dentry *res = NULL;
489 if (d_in_lookup(entry)) {
490 res = fuse_lookup(dir, entry, 0);
498 if (!(flags & O_CREAT) || d_really_is_positive(entry))
502 file->f_mode |= FMODE_CREATED;
507 err = fuse_create_open(dir, entry, file, flags, mode);
508 if (err == -ENOSYS) {
517 err = fuse_mknod(dir, entry, mode, 0);
521 return finish_no_open(file, res);
525 * Code shared between mknod, mkdir, symlink and link
527 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
528 struct inode *dir, struct dentry *entry,
531 struct fuse_entry_out outarg;
535 struct fuse_forget_link *forget;
537 forget = fuse_alloc_forget();
541 memset(&outarg, 0, sizeof(outarg));
542 args->in.h.nodeid = get_node_id(dir);
543 args->out.numargs = 1;
544 args->out.args[0].size = sizeof(outarg);
545 args->out.args[0].value = &outarg;
546 err = fuse_simple_request(fc, args);
548 goto out_put_forget_req;
551 if (invalid_nodeid(outarg.nodeid))
552 goto out_put_forget_req;
554 if ((outarg.attr.mode ^ mode) & S_IFMT)
555 goto out_put_forget_req;
557 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
558 &outarg.attr, entry_attr_timeout(&outarg), 0);
560 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
566 d = d_splice_alias(inode, entry);
571 fuse_change_entry_timeout(d, &outarg);
574 fuse_change_entry_timeout(entry, &outarg);
576 fuse_dir_changed(dir);
584 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
587 struct fuse_mknod_in inarg;
588 struct fuse_conn *fc = get_fuse_conn(dir);
592 mode &= ~current_umask();
594 memset(&inarg, 0, sizeof(inarg));
596 inarg.rdev = new_encode_dev(rdev);
597 inarg.umask = current_umask();
598 args.in.h.opcode = FUSE_MKNOD;
600 args.in.args[0].size = sizeof(inarg);
601 args.in.args[0].value = &inarg;
602 args.in.args[1].size = entry->d_name.len + 1;
603 args.in.args[1].value = entry->d_name.name;
604 return create_new_entry(fc, &args, dir, entry, mode);
607 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
610 return fuse_mknod(dir, entry, mode, 0);
613 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
615 struct fuse_mkdir_in inarg;
616 struct fuse_conn *fc = get_fuse_conn(dir);
620 mode &= ~current_umask();
622 memset(&inarg, 0, sizeof(inarg));
624 inarg.umask = current_umask();
625 args.in.h.opcode = FUSE_MKDIR;
627 args.in.args[0].size = sizeof(inarg);
628 args.in.args[0].value = &inarg;
629 args.in.args[1].size = entry->d_name.len + 1;
630 args.in.args[1].value = entry->d_name.name;
631 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
634 static int fuse_symlink(struct inode *dir, struct dentry *entry,
637 struct fuse_conn *fc = get_fuse_conn(dir);
638 unsigned len = strlen(link) + 1;
641 args.in.h.opcode = FUSE_SYMLINK;
643 args.in.args[0].size = entry->d_name.len + 1;
644 args.in.args[0].value = entry->d_name.name;
645 args.in.args[1].size = len;
646 args.in.args[1].value = link;
647 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
650 void fuse_update_ctime(struct inode *inode)
652 if (!IS_NOCMTIME(inode)) {
653 inode->i_ctime = current_time(inode);
654 mark_inode_dirty_sync(inode);
658 static int fuse_unlink(struct inode *dir, struct dentry *entry)
661 struct fuse_conn *fc = get_fuse_conn(dir);
664 args.in.h.opcode = FUSE_UNLINK;
665 args.in.h.nodeid = get_node_id(dir);
667 args.in.args[0].size = entry->d_name.len + 1;
668 args.in.args[0].value = entry->d_name.name;
669 err = fuse_simple_request(fc, &args);
671 struct inode *inode = d_inode(entry);
672 struct fuse_inode *fi = get_fuse_inode(inode);
674 spin_lock(&fc->lock);
675 fi->attr_version = ++fc->attr_version;
677 * If i_nlink == 0 then unlink doesn't make sense, yet this can
678 * happen if userspace filesystem is careless. It would be
679 * difficult to enforce correct nlink usage so just ignore this
682 if (inode->i_nlink > 0)
684 spin_unlock(&fc->lock);
685 fuse_invalidate_attr(inode);
686 fuse_dir_changed(dir);
687 fuse_invalidate_entry_cache(entry);
688 fuse_update_ctime(inode);
689 } else if (err == -EINTR)
690 fuse_invalidate_entry(entry);
694 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
697 struct fuse_conn *fc = get_fuse_conn(dir);
700 args.in.h.opcode = FUSE_RMDIR;
701 args.in.h.nodeid = get_node_id(dir);
703 args.in.args[0].size = entry->d_name.len + 1;
704 args.in.args[0].value = entry->d_name.name;
705 err = fuse_simple_request(fc, &args);
707 clear_nlink(d_inode(entry));
708 fuse_dir_changed(dir);
709 fuse_invalidate_entry_cache(entry);
710 } else if (err == -EINTR)
711 fuse_invalidate_entry(entry);
715 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
716 struct inode *newdir, struct dentry *newent,
717 unsigned int flags, int opcode, size_t argsize)
720 struct fuse_rename2_in inarg;
721 struct fuse_conn *fc = get_fuse_conn(olddir);
724 memset(&inarg, 0, argsize);
725 inarg.newdir = get_node_id(newdir);
727 args.in.h.opcode = opcode;
728 args.in.h.nodeid = get_node_id(olddir);
730 args.in.args[0].size = argsize;
731 args.in.args[0].value = &inarg;
732 args.in.args[1].size = oldent->d_name.len + 1;
733 args.in.args[1].value = oldent->d_name.name;
734 args.in.args[2].size = newent->d_name.len + 1;
735 args.in.args[2].value = newent->d_name.name;
736 err = fuse_simple_request(fc, &args);
739 fuse_invalidate_attr(d_inode(oldent));
740 fuse_update_ctime(d_inode(oldent));
742 if (flags & RENAME_EXCHANGE) {
743 fuse_invalidate_attr(d_inode(newent));
744 fuse_update_ctime(d_inode(newent));
747 fuse_dir_changed(olddir);
748 if (olddir != newdir)
749 fuse_dir_changed(newdir);
751 /* newent will end up negative */
752 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
753 fuse_invalidate_attr(d_inode(newent));
754 fuse_invalidate_entry_cache(newent);
755 fuse_update_ctime(d_inode(newent));
757 } else if (err == -EINTR) {
758 /* If request was interrupted, DEITY only knows if the
759 rename actually took place. If the invalidation
760 fails (e.g. some process has CWD under the renamed
761 directory), then there can be inconsistency between
762 the dcache and the real filesystem. Tough luck. */
763 fuse_invalidate_entry(oldent);
764 if (d_really_is_positive(newent))
765 fuse_invalidate_entry(newent);
771 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
772 struct inode *newdir, struct dentry *newent,
775 struct fuse_conn *fc = get_fuse_conn(olddir);
778 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
782 if (fc->no_rename2 || fc->minor < 23)
785 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
787 sizeof(struct fuse_rename2_in));
788 if (err == -ENOSYS) {
793 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
795 sizeof(struct fuse_rename_in));
801 static int fuse_link(struct dentry *entry, struct inode *newdir,
802 struct dentry *newent)
805 struct fuse_link_in inarg;
806 struct inode *inode = d_inode(entry);
807 struct fuse_conn *fc = get_fuse_conn(inode);
810 memset(&inarg, 0, sizeof(inarg));
811 inarg.oldnodeid = get_node_id(inode);
812 args.in.h.opcode = FUSE_LINK;
814 args.in.args[0].size = sizeof(inarg);
815 args.in.args[0].value = &inarg;
816 args.in.args[1].size = newent->d_name.len + 1;
817 args.in.args[1].value = newent->d_name.name;
818 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
819 /* Contrary to "normal" filesystems it can happen that link
820 makes two "logical" inodes point to the same "physical"
821 inode. We invalidate the attributes of the old one, so it
822 will reflect changes in the backing inode (link count,
826 struct fuse_inode *fi = get_fuse_inode(inode);
828 spin_lock(&fc->lock);
829 fi->attr_version = ++fc->attr_version;
831 spin_unlock(&fc->lock);
832 fuse_invalidate_attr(inode);
833 fuse_update_ctime(inode);
834 } else if (err == -EINTR) {
835 fuse_invalidate_attr(inode);
840 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
843 unsigned int blkbits;
844 struct fuse_conn *fc = get_fuse_conn(inode);
846 /* see the comment in fuse_change_attributes() */
847 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
848 attr->size = i_size_read(inode);
849 attr->mtime = inode->i_mtime.tv_sec;
850 attr->mtimensec = inode->i_mtime.tv_nsec;
851 attr->ctime = inode->i_ctime.tv_sec;
852 attr->ctimensec = inode->i_ctime.tv_nsec;
855 stat->dev = inode->i_sb->s_dev;
856 stat->ino = attr->ino;
857 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
858 stat->nlink = attr->nlink;
859 stat->uid = make_kuid(fc->user_ns, attr->uid);
860 stat->gid = make_kgid(fc->user_ns, attr->gid);
861 stat->rdev = inode->i_rdev;
862 stat->atime.tv_sec = attr->atime;
863 stat->atime.tv_nsec = attr->atimensec;
864 stat->mtime.tv_sec = attr->mtime;
865 stat->mtime.tv_nsec = attr->mtimensec;
866 stat->ctime.tv_sec = attr->ctime;
867 stat->ctime.tv_nsec = attr->ctimensec;
868 stat->size = attr->size;
869 stat->blocks = attr->blocks;
871 if (attr->blksize != 0)
872 blkbits = ilog2(attr->blksize);
874 blkbits = inode->i_sb->s_blocksize_bits;
876 stat->blksize = 1 << blkbits;
879 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
883 struct fuse_getattr_in inarg;
884 struct fuse_attr_out outarg;
885 struct fuse_conn *fc = get_fuse_conn(inode);
889 attr_version = fuse_get_attr_version(fc);
891 memset(&inarg, 0, sizeof(inarg));
892 memset(&outarg, 0, sizeof(outarg));
893 /* Directories have separate file-handle space */
894 if (file && S_ISREG(inode->i_mode)) {
895 struct fuse_file *ff = file->private_data;
897 inarg.getattr_flags |= FUSE_GETATTR_FH;
900 args.in.h.opcode = FUSE_GETATTR;
901 args.in.h.nodeid = get_node_id(inode);
903 args.in.args[0].size = sizeof(inarg);
904 args.in.args[0].value = &inarg;
905 args.out.numargs = 1;
906 args.out.args[0].size = sizeof(outarg);
907 args.out.args[0].value = &outarg;
908 err = fuse_simple_request(fc, &args);
910 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
911 make_bad_inode(inode);
914 fuse_change_attributes(inode, &outarg.attr,
915 attr_timeout(&outarg),
918 fuse_fillattr(inode, &outarg.attr, stat);
924 static int fuse_update_get_attr(struct inode *inode, struct file *file,
925 struct kstat *stat, u32 request_mask,
928 struct fuse_inode *fi = get_fuse_inode(inode);
932 if (flags & AT_STATX_FORCE_SYNC)
934 else if (flags & AT_STATX_DONT_SYNC)
936 else if (request_mask & READ_ONCE(fi->inval_mask))
939 sync = time_before64(fi->i_time, get_jiffies_64());
942 forget_all_cached_acls(inode);
943 err = fuse_do_getattr(inode, stat, file);
945 generic_fillattr(inode, stat);
946 stat->mode = fi->orig_i_mode;
947 stat->ino = fi->orig_ino;
953 int fuse_update_attributes(struct inode *inode, struct file *file)
955 /* Do *not* need to get atime for internal purposes */
956 return fuse_update_get_attr(inode, file, NULL,
957 STATX_BASIC_STATS & ~STATX_ATIME, 0);
960 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
961 u64 child_nodeid, struct qstr *name)
964 struct inode *parent;
966 struct dentry *entry;
968 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
973 if (!S_ISDIR(parent->i_mode))
977 dir = d_find_alias(parent);
981 name->hash = full_name_hash(dir, name->name, name->len);
982 entry = d_lookup(dir, name);
987 fuse_dir_changed(parent);
988 fuse_invalidate_entry(entry);
990 if (child_nodeid != 0 && d_really_is_positive(entry)) {
991 inode_lock(d_inode(entry));
992 if (get_node_id(d_inode(entry)) != child_nodeid) {
996 if (d_mountpoint(entry)) {
1000 if (d_is_dir(entry)) {
1001 shrink_dcache_parent(entry);
1002 if (!simple_empty(entry)) {
1006 d_inode(entry)->i_flags |= S_DEAD;
1009 clear_nlink(d_inode(entry));
1012 inode_unlock(d_inode(entry));
1021 inode_unlock(parent);
1027 * Calling into a user-controlled filesystem gives the filesystem
1028 * daemon ptrace-like capabilities over the current process. This
1029 * means, that the filesystem daemon is able to record the exact
1030 * filesystem operations performed, and can also control the behavior
1031 * of the requester process in otherwise impossible ways. For example
1032 * it can delay the operation for arbitrary length of time allowing
1033 * DoS against the requester.
1035 * For this reason only those processes can call into the filesystem,
1036 * for which the owner of the mount has ptrace privilege. This
1037 * excludes processes started by other users, suid or sgid processes.
1039 int fuse_allow_current_process(struct fuse_conn *fc)
1041 const struct cred *cred;
1043 if (fc->allow_other)
1044 return current_in_userns(fc->user_ns);
1046 cred = current_cred();
1047 if (uid_eq(cred->euid, fc->user_id) &&
1048 uid_eq(cred->suid, fc->user_id) &&
1049 uid_eq(cred->uid, fc->user_id) &&
1050 gid_eq(cred->egid, fc->group_id) &&
1051 gid_eq(cred->sgid, fc->group_id) &&
1052 gid_eq(cred->gid, fc->group_id))
1058 static int fuse_access(struct inode *inode, int mask)
1060 struct fuse_conn *fc = get_fuse_conn(inode);
1062 struct fuse_access_in inarg;
1065 BUG_ON(mask & MAY_NOT_BLOCK);
1070 memset(&inarg, 0, sizeof(inarg));
1071 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1072 args.in.h.opcode = FUSE_ACCESS;
1073 args.in.h.nodeid = get_node_id(inode);
1074 args.in.numargs = 1;
1075 args.in.args[0].size = sizeof(inarg);
1076 args.in.args[0].value = &inarg;
1077 err = fuse_simple_request(fc, &args);
1078 if (err == -ENOSYS) {
1085 static int fuse_perm_getattr(struct inode *inode, int mask)
1087 if (mask & MAY_NOT_BLOCK)
1090 forget_all_cached_acls(inode);
1091 return fuse_do_getattr(inode, NULL, NULL);
1095 * Check permission. The two basic access models of FUSE are:
1097 * 1) Local access checking ('default_permissions' mount option) based
1098 * on file mode. This is the plain old disk filesystem permission
1101 * 2) "Remote" access checking, where server is responsible for
1102 * checking permission in each inode operation. An exception to this
1103 * is if ->permission() was invoked from sys_access() in which case an
1104 * access request is sent. Execute permission is still checked
1105 * locally based on file mode.
1107 static int fuse_permission(struct inode *inode, int mask)
1109 struct fuse_conn *fc = get_fuse_conn(inode);
1110 bool refreshed = false;
1113 if (!fuse_allow_current_process(fc))
1117 * If attributes are needed, refresh them before proceeding
1119 if (fc->default_permissions ||
1120 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1121 struct fuse_inode *fi = get_fuse_inode(inode);
1122 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1124 if (perm_mask & READ_ONCE(fi->inval_mask) ||
1125 time_before64(fi->i_time, get_jiffies_64())) {
1128 err = fuse_perm_getattr(inode, mask);
1134 if (fc->default_permissions) {
1135 err = generic_permission(inode, mask);
1137 /* If permission is denied, try to refresh file
1138 attributes. This is also needed, because the root
1139 node will at first have no permissions */
1140 if (err == -EACCES && !refreshed) {
1141 err = fuse_perm_getattr(inode, mask);
1143 err = generic_permission(inode, mask);
1146 /* Note: the opposite of the above test does not
1147 exist. So if permissions are revoked this won't be
1148 noticed immediately, only after the attribute
1149 timeout has expired */
1150 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1151 err = fuse_access(inode, mask);
1152 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1153 if (!(inode->i_mode & S_IXUGO)) {
1157 err = fuse_perm_getattr(inode, mask);
1158 if (!err && !(inode->i_mode & S_IXUGO))
1165 static int fuse_readlink_page(struct inode *inode, struct page *page)
1167 struct fuse_conn *fc = get_fuse_conn(inode);
1168 struct fuse_req *req;
1171 req = fuse_get_req(fc, 1);
1173 return PTR_ERR(req);
1175 req->out.page_zeroing = 1;
1176 req->out.argpages = 1;
1178 req->pages[0] = page;
1179 req->page_descs[0].length = PAGE_SIZE - 1;
1180 req->in.h.opcode = FUSE_READLINK;
1181 req->in.h.nodeid = get_node_id(inode);
1182 req->out.argvar = 1;
1183 req->out.numargs = 1;
1184 req->out.args[0].size = PAGE_SIZE - 1;
1185 fuse_request_send(fc, req);
1186 err = req->out.h.error;
1189 char *link = page_address(page);
1190 size_t len = req->out.args[0].size;
1192 BUG_ON(len >= PAGE_SIZE);
1196 fuse_put_request(fc, req);
1197 fuse_invalidate_atime(inode);
1202 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1203 struct delayed_call *callback)
1205 struct fuse_conn *fc = get_fuse_conn(inode);
1210 if (is_bad_inode(inode))
1213 if (fc->cache_symlinks)
1214 return page_get_link(dentry, inode, callback);
1220 page = alloc_page(GFP_KERNEL);
1225 err = fuse_readlink_page(inode, page);
1231 set_delayed_call(callback, page_put_link, page);
1233 return page_address(page);
1236 return ERR_PTR(err);
1239 static int fuse_dir_open(struct inode *inode, struct file *file)
1241 return fuse_open_common(inode, file, true);
1244 static int fuse_dir_release(struct inode *inode, struct file *file)
1246 fuse_release_common(file, true);
1251 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1254 struct inode *inode = file->f_mapping->host;
1255 struct fuse_conn *fc = get_fuse_conn(inode);
1258 if (is_bad_inode(inode))
1261 if (fc->no_fsyncdir)
1265 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1266 if (err == -ENOSYS) {
1267 fc->no_fsyncdir = 1;
1270 inode_unlock(inode);
1275 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1278 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1280 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1284 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1287 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1290 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1295 return fuse_ioctl_common(file, cmd, arg,
1296 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1299 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1301 /* Always update if mtime is explicitly set */
1302 if (ivalid & ATTR_MTIME_SET)
1305 /* Or if kernel i_mtime is the official one */
1306 if (trust_local_mtime)
1309 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1310 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1313 /* In all other cases update */
1317 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1318 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1320 unsigned ivalid = iattr->ia_valid;
1322 if (ivalid & ATTR_MODE)
1323 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1324 if (ivalid & ATTR_UID)
1325 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1326 if (ivalid & ATTR_GID)
1327 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1328 if (ivalid & ATTR_SIZE)
1329 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1330 if (ivalid & ATTR_ATIME) {
1331 arg->valid |= FATTR_ATIME;
1332 arg->atime = iattr->ia_atime.tv_sec;
1333 arg->atimensec = iattr->ia_atime.tv_nsec;
1334 if (!(ivalid & ATTR_ATIME_SET))
1335 arg->valid |= FATTR_ATIME_NOW;
1337 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1338 arg->valid |= FATTR_MTIME;
1339 arg->mtime = iattr->ia_mtime.tv_sec;
1340 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1341 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1342 arg->valid |= FATTR_MTIME_NOW;
1344 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1345 arg->valid |= FATTR_CTIME;
1346 arg->ctime = iattr->ia_ctime.tv_sec;
1347 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1352 * Prevent concurrent writepages on inode
1354 * This is done by adding a negative bias to the inode write counter
1355 * and waiting for all pending writes to finish.
1357 void fuse_set_nowrite(struct inode *inode)
1359 struct fuse_conn *fc = get_fuse_conn(inode);
1360 struct fuse_inode *fi = get_fuse_inode(inode);
1362 BUG_ON(!inode_is_locked(inode));
1364 spin_lock(&fc->lock);
1365 BUG_ON(fi->writectr < 0);
1366 fi->writectr += FUSE_NOWRITE;
1367 spin_unlock(&fc->lock);
1368 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1372 * Allow writepages on inode
1374 * Remove the bias from the writecounter and send any queued
1377 static void __fuse_release_nowrite(struct inode *inode)
1379 struct fuse_inode *fi = get_fuse_inode(inode);
1381 BUG_ON(fi->writectr != FUSE_NOWRITE);
1383 fuse_flush_writepages(inode);
1386 void fuse_release_nowrite(struct inode *inode)
1388 struct fuse_conn *fc = get_fuse_conn(inode);
1390 spin_lock(&fc->lock);
1391 __fuse_release_nowrite(inode);
1392 spin_unlock(&fc->lock);
1395 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1396 struct inode *inode,
1397 struct fuse_setattr_in *inarg_p,
1398 struct fuse_attr_out *outarg_p)
1400 args->in.h.opcode = FUSE_SETATTR;
1401 args->in.h.nodeid = get_node_id(inode);
1402 args->in.numargs = 1;
1403 args->in.args[0].size = sizeof(*inarg_p);
1404 args->in.args[0].value = inarg_p;
1405 args->out.numargs = 1;
1406 args->out.args[0].size = sizeof(*outarg_p);
1407 args->out.args[0].value = outarg_p;
1411 * Flush inode->i_mtime to the server
1413 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1415 struct fuse_conn *fc = get_fuse_conn(inode);
1417 struct fuse_setattr_in inarg;
1418 struct fuse_attr_out outarg;
1420 memset(&inarg, 0, sizeof(inarg));
1421 memset(&outarg, 0, sizeof(outarg));
1423 inarg.valid = FATTR_MTIME;
1424 inarg.mtime = inode->i_mtime.tv_sec;
1425 inarg.mtimensec = inode->i_mtime.tv_nsec;
1426 if (fc->minor >= 23) {
1427 inarg.valid |= FATTR_CTIME;
1428 inarg.ctime = inode->i_ctime.tv_sec;
1429 inarg.ctimensec = inode->i_ctime.tv_nsec;
1432 inarg.valid |= FATTR_FH;
1435 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1437 return fuse_simple_request(fc, &args);
1441 * Set attributes, and at the same time refresh them.
1443 * Truncation is slightly complicated, because the 'truncate' request
1444 * may fail, in which case we don't want to touch the mapping.
1445 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1446 * and the actual truncation by hand.
1448 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1451 struct inode *inode = d_inode(dentry);
1452 struct fuse_conn *fc = get_fuse_conn(inode);
1453 struct fuse_inode *fi = get_fuse_inode(inode);
1455 struct fuse_setattr_in inarg;
1456 struct fuse_attr_out outarg;
1457 bool is_truncate = false;
1458 bool is_wb = fc->writeback_cache;
1461 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1463 if (!fc->default_permissions)
1464 attr->ia_valid |= ATTR_FORCE;
1466 err = setattr_prepare(dentry, attr);
1470 if (attr->ia_valid & ATTR_OPEN) {
1471 /* This is coming from open(..., ... | O_TRUNC); */
1472 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1473 WARN_ON(attr->ia_size != 0);
1474 if (fc->atomic_o_trunc) {
1476 * No need to send request to userspace, since actual
1477 * truncation has already been done by OPEN. But still
1478 * need to truncate page cache.
1480 i_size_write(inode, 0);
1481 truncate_pagecache(inode, 0);
1487 if (attr->ia_valid & ATTR_SIZE) {
1488 if (WARN_ON(!S_ISREG(inode->i_mode)))
1494 fuse_set_nowrite(inode);
1495 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1496 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1497 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1500 memset(&inarg, 0, sizeof(inarg));
1501 memset(&outarg, 0, sizeof(outarg));
1502 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1504 struct fuse_file *ff = file->private_data;
1505 inarg.valid |= FATTR_FH;
1508 if (attr->ia_valid & ATTR_SIZE) {
1509 /* For mandatory locking in truncate */
1510 inarg.valid |= FATTR_LOCKOWNER;
1511 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1513 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1514 err = fuse_simple_request(fc, &args);
1517 fuse_invalidate_attr(inode);
1521 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1522 make_bad_inode(inode);
1527 spin_lock(&fc->lock);
1528 /* the kernel maintains i_mtime locally */
1529 if (trust_local_cmtime) {
1530 if (attr->ia_valid & ATTR_MTIME)
1531 inode->i_mtime = attr->ia_mtime;
1532 if (attr->ia_valid & ATTR_CTIME)
1533 inode->i_ctime = attr->ia_ctime;
1534 /* FIXME: clear I_DIRTY_SYNC? */
1537 fuse_change_attributes_common(inode, &outarg.attr,
1538 attr_timeout(&outarg));
1539 oldsize = inode->i_size;
1540 /* see the comment in fuse_change_attributes() */
1541 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1542 i_size_write(inode, outarg.attr.size);
1545 /* NOTE: this may release/reacquire fc->lock */
1546 __fuse_release_nowrite(inode);
1548 spin_unlock(&fc->lock);
1551 * Only call invalidate_inode_pages2() after removing
1552 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1554 if ((is_truncate || !is_wb) &&
1555 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1556 truncate_pagecache(inode, outarg.attr.size);
1557 invalidate_inode_pages2(inode->i_mapping);
1560 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1565 fuse_release_nowrite(inode);
1567 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1571 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1573 struct inode *inode = d_inode(entry);
1574 struct fuse_conn *fc = get_fuse_conn(inode);
1575 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1578 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1581 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1582 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1586 * The only sane way to reliably kill suid/sgid is to do it in
1587 * the userspace filesystem
1589 * This should be done on write(), truncate() and chown().
1591 if (!fc->handle_killpriv) {
1593 * ia_mode calculation may have used stale i_mode.
1594 * Refresh and recalculate.
1596 ret = fuse_do_getattr(inode, NULL, file);
1600 attr->ia_mode = inode->i_mode;
1601 if (inode->i_mode & S_ISUID) {
1602 attr->ia_valid |= ATTR_MODE;
1603 attr->ia_mode &= ~S_ISUID;
1605 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1606 attr->ia_valid |= ATTR_MODE;
1607 attr->ia_mode &= ~S_ISGID;
1611 if (!attr->ia_valid)
1614 ret = fuse_do_setattr(entry, attr, file);
1617 * If filesystem supports acls it may have updated acl xattrs in
1618 * the filesystem, so forget cached acls for the inode.
1621 forget_all_cached_acls(inode);
1623 /* Directory mode changed, may need to revalidate access */
1624 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1625 fuse_invalidate_entry_cache(entry);
1630 static int fuse_getattr(const struct path *path, struct kstat *stat,
1631 u32 request_mask, unsigned int flags)
1633 struct inode *inode = d_inode(path->dentry);
1634 struct fuse_conn *fc = get_fuse_conn(inode);
1636 if (!fuse_allow_current_process(fc))
1639 return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1642 static const struct inode_operations fuse_dir_inode_operations = {
1643 .lookup = fuse_lookup,
1644 .mkdir = fuse_mkdir,
1645 .symlink = fuse_symlink,
1646 .unlink = fuse_unlink,
1647 .rmdir = fuse_rmdir,
1648 .rename = fuse_rename2,
1650 .setattr = fuse_setattr,
1651 .create = fuse_create,
1652 .atomic_open = fuse_atomic_open,
1653 .mknod = fuse_mknod,
1654 .permission = fuse_permission,
1655 .getattr = fuse_getattr,
1656 .listxattr = fuse_listxattr,
1657 .get_acl = fuse_get_acl,
1658 .set_acl = fuse_set_acl,
1661 static const struct file_operations fuse_dir_operations = {
1662 .llseek = generic_file_llseek,
1663 .read = generic_read_dir,
1664 .iterate_shared = fuse_readdir,
1665 .open = fuse_dir_open,
1666 .release = fuse_dir_release,
1667 .fsync = fuse_dir_fsync,
1668 .unlocked_ioctl = fuse_dir_ioctl,
1669 .compat_ioctl = fuse_dir_compat_ioctl,
1672 static const struct inode_operations fuse_common_inode_operations = {
1673 .setattr = fuse_setattr,
1674 .permission = fuse_permission,
1675 .getattr = fuse_getattr,
1676 .listxattr = fuse_listxattr,
1677 .get_acl = fuse_get_acl,
1678 .set_acl = fuse_set_acl,
1681 static const struct inode_operations fuse_symlink_inode_operations = {
1682 .setattr = fuse_setattr,
1683 .get_link = fuse_get_link,
1684 .getattr = fuse_getattr,
1685 .listxattr = fuse_listxattr,
1688 void fuse_init_common(struct inode *inode)
1690 inode->i_op = &fuse_common_inode_operations;
1693 void fuse_init_dir(struct inode *inode)
1695 struct fuse_inode *fi = get_fuse_inode(inode);
1697 inode->i_op = &fuse_dir_inode_operations;
1698 inode->i_fop = &fuse_dir_operations;
1700 spin_lock_init(&fi->rdc.lock);
1701 fi->rdc.cached = false;
1704 fi->rdc.version = 0;
1707 static int fuse_symlink_readpage(struct file *null, struct page *page)
1709 int err = fuse_readlink_page(page->mapping->host, page);
1712 SetPageUptodate(page);
1719 static const struct address_space_operations fuse_symlink_aops = {
1720 .readpage = fuse_symlink_readpage,
1723 void fuse_init_symlink(struct inode *inode)
1725 inode->i_op = &fuse_symlink_inode_operations;
1726 inode->i_data.a_ops = &fuse_symlink_aops;
1727 inode_nohighmem(inode);