2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
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/posix_acl.h>
19 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
21 struct fuse_conn *fc = get_fuse_conn(dir);
22 struct fuse_inode *fi = get_fuse_inode(dir);
24 if (!fc->do_readdirplus)
26 if (!fc->readdirplus_auto)
28 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
35 static void fuse_advise_use_readdirplus(struct inode *dir)
37 struct fuse_inode *fi = get_fuse_inode(dir);
39 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
47 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
49 ((union fuse_dentry *) entry->d_fsdata)->time = time;
52 static inline u64 fuse_dentry_time(struct dentry *entry)
54 return ((union fuse_dentry *) entry->d_fsdata)->time;
58 * FUSE caches dentries and attributes with separate timeout. The
59 * time in jiffies until the dentry/attributes are valid is stored in
60 * dentry->d_fsdata and fuse_inode->i_time respectively.
64 * Calculate the time in jiffies until a dentry/attributes are valid
66 static u64 time_to_jiffies(u64 sec, u32 nsec)
69 struct timespec64 ts = {
71 min_t(u32, nsec, NSEC_PER_SEC - 1)
74 return get_jiffies_64() + timespec64_to_jiffies(&ts);
80 * Set dentry and possibly attribute timeouts from the lookup/mk*
83 static void fuse_change_entry_timeout(struct dentry *entry,
84 struct fuse_entry_out *o)
86 fuse_dentry_settime(entry,
87 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
90 static u64 attr_timeout(struct fuse_attr_out *o)
92 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
95 static u64 entry_attr_timeout(struct fuse_entry_out *o)
97 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
101 * Mark the attributes as stale, so that at the next call to
102 * ->getattr() they will be fetched from userspace
104 void fuse_invalidate_attr(struct inode *inode)
106 get_fuse_inode(inode)->i_time = 0;
110 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
113 void fuse_invalidate_atime(struct inode *inode)
115 if (!IS_RDONLY(inode))
116 fuse_invalidate_attr(inode);
120 * Just mark the entry as stale, so that a next attempt to look it up
121 * will result in a new lookup call to userspace
123 * This is called when a dentry is about to become negative and the
124 * timeout is unknown (unlink, rmdir, rename and in some cases
127 void fuse_invalidate_entry_cache(struct dentry *entry)
129 fuse_dentry_settime(entry, 0);
133 * Same as fuse_invalidate_entry_cache(), but also try to remove the
134 * dentry from the hash
136 static void fuse_invalidate_entry(struct dentry *entry)
139 fuse_invalidate_entry_cache(entry);
142 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
143 u64 nodeid, const struct qstr *name,
144 struct fuse_entry_out *outarg)
146 memset(outarg, 0, sizeof(struct fuse_entry_out));
147 args->in.h.opcode = FUSE_LOOKUP;
148 args->in.h.nodeid = nodeid;
149 args->in.numargs = 1;
150 args->in.args[0].size = name->len + 1;
151 args->in.args[0].value = name->name;
152 args->out.numargs = 1;
153 args->out.args[0].size = sizeof(struct fuse_entry_out);
154 args->out.args[0].value = outarg;
157 u64 fuse_get_attr_version(struct fuse_conn *fc)
162 * The spin lock isn't actually needed on 64bit archs, but we
163 * don't yet care too much about such optimizations.
165 spin_lock(&fc->lock);
166 curr_version = fc->attr_version;
167 spin_unlock(&fc->lock);
173 * Check whether the dentry is still valid
175 * If the entry validity timeout has expired and the dentry is
176 * positive, try to redo the lookup. If the lookup results in a
177 * different inode, then let the VFS invalidate the dentry and redo
178 * the lookup once more. If the lookup results in the same inode,
179 * then refresh the attributes, timeouts and mark the dentry valid.
181 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
184 struct dentry *parent;
185 struct fuse_conn *fc;
186 struct fuse_inode *fi;
189 inode = d_inode_rcu(entry);
190 if (inode && is_bad_inode(inode))
192 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
193 (flags & LOOKUP_REVAL)) {
194 struct fuse_entry_out outarg;
196 struct fuse_forget_link *forget;
199 /* For negative dentries, always do a fresh lookup */
204 if (flags & LOOKUP_RCU)
207 fc = get_fuse_conn(inode);
209 forget = fuse_alloc_forget();
214 attr_version = fuse_get_attr_version(fc);
216 parent = dget_parent(entry);
217 fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
218 &entry->d_name, &outarg);
219 ret = fuse_simple_request(fc, &args);
221 /* Zero nodeid is same as -ENOENT */
222 if (!ret && !outarg.nodeid)
225 fi = get_fuse_inode(inode);
226 if (outarg.nodeid != get_node_id(inode)) {
227 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
230 spin_lock(&fc->lock);
232 spin_unlock(&fc->lock);
237 if (ret || fuse_invalid_attr(&outarg.attr) ||
238 (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
241 forget_all_cached_acls(inode);
242 fuse_change_attributes(inode, &outarg.attr,
243 entry_attr_timeout(&outarg),
245 fuse_change_entry_timeout(entry, &outarg);
247 fi = get_fuse_inode(inode);
248 if (flags & LOOKUP_RCU) {
249 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
251 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
252 parent = dget_parent(entry);
253 fuse_advise_use_readdirplus(d_inode(parent));
266 static int invalid_nodeid(u64 nodeid)
268 return !nodeid || nodeid == FUSE_ROOT_ID;
271 static int fuse_dentry_init(struct dentry *dentry)
273 dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry), GFP_KERNEL);
275 return dentry->d_fsdata ? 0 : -ENOMEM;
277 static void fuse_dentry_release(struct dentry *dentry)
279 union fuse_dentry *fd = dentry->d_fsdata;
284 const struct dentry_operations fuse_dentry_operations = {
285 .d_revalidate = fuse_dentry_revalidate,
286 .d_init = fuse_dentry_init,
287 .d_release = fuse_dentry_release,
290 const struct dentry_operations fuse_root_dentry_operations = {
291 .d_init = fuse_dentry_init,
292 .d_release = fuse_dentry_release,
295 int fuse_valid_type(int m)
297 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
298 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
301 bool fuse_invalid_attr(struct fuse_attr *attr)
303 return !fuse_valid_type(attr->mode) ||
304 attr->size > LLONG_MAX;
307 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
308 struct fuse_entry_out *outarg, struct inode **inode)
310 struct fuse_conn *fc = get_fuse_conn_super(sb);
312 struct fuse_forget_link *forget;
318 if (name->len > FUSE_NAME_MAX)
322 forget = fuse_alloc_forget();
327 attr_version = fuse_get_attr_version(fc);
329 fuse_lookup_init(fc, &args, nodeid, name, outarg);
330 err = fuse_simple_request(fc, &args);
331 /* Zero nodeid is same as -ENOENT, but with valid timeout */
332 if (err || !outarg->nodeid)
338 if (fuse_invalid_attr(&outarg->attr))
341 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
342 &outarg->attr, entry_attr_timeout(outarg),
346 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
357 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
361 struct fuse_entry_out outarg;
363 struct dentry *newent;
364 bool outarg_valid = true;
367 locked = fuse_lock_inode(dir);
368 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
370 fuse_unlock_inode(dir, locked);
371 if (err == -ENOENT) {
372 outarg_valid = false;
379 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
382 newent = d_splice_alias(inode, entry);
383 err = PTR_ERR(newent);
387 entry = newent ? newent : entry;
389 fuse_change_entry_timeout(entry, &outarg);
391 fuse_invalidate_entry_cache(entry);
393 fuse_advise_use_readdirplus(dir);
403 * Atomic create+open operation
405 * If the filesystem doesn't support this, then fall back to separate
406 * 'mknod' + 'open' requests.
408 static int fuse_create_open(struct inode *dir, struct dentry *entry,
409 struct file *file, unsigned flags,
414 struct fuse_conn *fc = get_fuse_conn(dir);
416 struct fuse_forget_link *forget;
417 struct fuse_create_in inarg;
418 struct fuse_open_out outopen;
419 struct fuse_entry_out outentry;
420 struct fuse_file *ff;
422 /* Userspace expects S_IFREG in create mode */
423 BUG_ON((mode & S_IFMT) != S_IFREG);
425 forget = fuse_alloc_forget();
431 ff = fuse_file_alloc(fc);
433 goto out_put_forget_req;
436 mode &= ~current_umask();
439 memset(&inarg, 0, sizeof(inarg));
440 memset(&outentry, 0, sizeof(outentry));
443 inarg.umask = current_umask();
444 args.in.h.opcode = FUSE_CREATE;
445 args.in.h.nodeid = get_node_id(dir);
447 args.in.args[0].size = sizeof(inarg);
448 args.in.args[0].value = &inarg;
449 args.in.args[1].size = entry->d_name.len + 1;
450 args.in.args[1].value = entry->d_name.name;
451 args.out.numargs = 2;
452 args.out.args[0].size = sizeof(outentry);
453 args.out.args[0].value = &outentry;
454 args.out.args[1].size = sizeof(outopen);
455 args.out.args[1].value = &outopen;
456 err = fuse_simple_request(fc, &args);
461 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
462 fuse_invalid_attr(&outentry.attr))
466 ff->nodeid = outentry.nodeid;
467 ff->open_flags = outopen.open_flags;
468 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
469 &outentry.attr, entry_attr_timeout(&outentry), 0);
471 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
472 fuse_sync_release(ff, flags);
473 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
478 d_instantiate(entry, inode);
479 fuse_change_entry_timeout(entry, &outentry);
480 fuse_invalidate_attr(dir);
481 err = finish_open(file, entry, generic_file_open);
483 fuse_sync_release(ff, flags);
485 file->private_data = ff;
486 fuse_finish_open(inode, file);
498 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
499 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
500 struct file *file, unsigned flags,
504 struct fuse_conn *fc = get_fuse_conn(dir);
505 struct dentry *res = NULL;
507 if (d_in_lookup(entry)) {
508 res = fuse_lookup(dir, entry, 0);
516 if (!(flags & O_CREAT) || d_really_is_positive(entry))
520 file->f_mode |= FMODE_CREATED;
525 err = fuse_create_open(dir, entry, file, flags, mode);
526 if (err == -ENOSYS) {
535 err = fuse_mknod(dir, entry, mode, 0);
539 return finish_no_open(file, res);
543 * Code shared between mknod, mkdir, symlink and link
545 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
546 struct inode *dir, struct dentry *entry,
549 struct fuse_entry_out outarg;
553 struct fuse_forget_link *forget;
555 forget = fuse_alloc_forget();
559 memset(&outarg, 0, sizeof(outarg));
560 args->in.h.nodeid = get_node_id(dir);
561 args->out.numargs = 1;
562 args->out.args[0].size = sizeof(outarg);
563 args->out.args[0].value = &outarg;
564 err = fuse_simple_request(fc, args);
566 goto out_put_forget_req;
569 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
570 goto out_put_forget_req;
572 if ((outarg.attr.mode ^ mode) & S_IFMT)
573 goto out_put_forget_req;
575 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
576 &outarg.attr, entry_attr_timeout(&outarg), 0);
578 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
584 d = d_splice_alias(inode, entry);
589 fuse_change_entry_timeout(d, &outarg);
592 fuse_change_entry_timeout(entry, &outarg);
594 fuse_invalidate_attr(dir);
602 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
605 struct fuse_mknod_in inarg;
606 struct fuse_conn *fc = get_fuse_conn(dir);
610 mode &= ~current_umask();
612 memset(&inarg, 0, sizeof(inarg));
614 inarg.rdev = new_encode_dev(rdev);
615 inarg.umask = current_umask();
616 args.in.h.opcode = FUSE_MKNOD;
618 args.in.args[0].size = sizeof(inarg);
619 args.in.args[0].value = &inarg;
620 args.in.args[1].size = entry->d_name.len + 1;
621 args.in.args[1].value = entry->d_name.name;
622 return create_new_entry(fc, &args, dir, entry, mode);
625 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
628 return fuse_mknod(dir, entry, mode, 0);
631 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
633 struct fuse_mkdir_in inarg;
634 struct fuse_conn *fc = get_fuse_conn(dir);
638 mode &= ~current_umask();
640 memset(&inarg, 0, sizeof(inarg));
642 inarg.umask = current_umask();
643 args.in.h.opcode = FUSE_MKDIR;
645 args.in.args[0].size = sizeof(inarg);
646 args.in.args[0].value = &inarg;
647 args.in.args[1].size = entry->d_name.len + 1;
648 args.in.args[1].value = entry->d_name.name;
649 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
652 static int fuse_symlink(struct inode *dir, struct dentry *entry,
655 struct fuse_conn *fc = get_fuse_conn(dir);
656 unsigned len = strlen(link) + 1;
659 args.in.h.opcode = FUSE_SYMLINK;
661 args.in.args[0].size = entry->d_name.len + 1;
662 args.in.args[0].value = entry->d_name.name;
663 args.in.args[1].size = len;
664 args.in.args[1].value = link;
665 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
668 void fuse_update_ctime(struct inode *inode)
670 if (!IS_NOCMTIME(inode)) {
671 inode->i_ctime = current_time(inode);
672 mark_inode_dirty_sync(inode);
676 static int fuse_unlink(struct inode *dir, struct dentry *entry)
679 struct fuse_conn *fc = get_fuse_conn(dir);
682 args.in.h.opcode = FUSE_UNLINK;
683 args.in.h.nodeid = get_node_id(dir);
685 args.in.args[0].size = entry->d_name.len + 1;
686 args.in.args[0].value = entry->d_name.name;
687 err = fuse_simple_request(fc, &args);
689 struct inode *inode = d_inode(entry);
690 struct fuse_inode *fi = get_fuse_inode(inode);
692 spin_lock(&fc->lock);
693 fi->attr_version = ++fc->attr_version;
695 * If i_nlink == 0 then unlink doesn't make sense, yet this can
696 * happen if userspace filesystem is careless. It would be
697 * difficult to enforce correct nlink usage so just ignore this
700 if (inode->i_nlink > 0)
702 spin_unlock(&fc->lock);
703 fuse_invalidate_attr(inode);
704 fuse_invalidate_attr(dir);
705 fuse_invalidate_entry_cache(entry);
706 fuse_update_ctime(inode);
707 } else if (err == -EINTR)
708 fuse_invalidate_entry(entry);
712 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
715 struct fuse_conn *fc = get_fuse_conn(dir);
718 args.in.h.opcode = FUSE_RMDIR;
719 args.in.h.nodeid = get_node_id(dir);
721 args.in.args[0].size = entry->d_name.len + 1;
722 args.in.args[0].value = entry->d_name.name;
723 err = fuse_simple_request(fc, &args);
725 clear_nlink(d_inode(entry));
726 fuse_invalidate_attr(dir);
727 fuse_invalidate_entry_cache(entry);
728 } else if (err == -EINTR)
729 fuse_invalidate_entry(entry);
733 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
734 struct inode *newdir, struct dentry *newent,
735 unsigned int flags, int opcode, size_t argsize)
738 struct fuse_rename2_in inarg;
739 struct fuse_conn *fc = get_fuse_conn(olddir);
742 memset(&inarg, 0, argsize);
743 inarg.newdir = get_node_id(newdir);
745 args.in.h.opcode = opcode;
746 args.in.h.nodeid = get_node_id(olddir);
748 args.in.args[0].size = argsize;
749 args.in.args[0].value = &inarg;
750 args.in.args[1].size = oldent->d_name.len + 1;
751 args.in.args[1].value = oldent->d_name.name;
752 args.in.args[2].size = newent->d_name.len + 1;
753 args.in.args[2].value = newent->d_name.name;
754 err = fuse_simple_request(fc, &args);
757 fuse_invalidate_attr(d_inode(oldent));
758 fuse_update_ctime(d_inode(oldent));
760 if (flags & RENAME_EXCHANGE) {
761 fuse_invalidate_attr(d_inode(newent));
762 fuse_update_ctime(d_inode(newent));
765 fuse_invalidate_attr(olddir);
766 if (olddir != newdir)
767 fuse_invalidate_attr(newdir);
769 /* newent will end up negative */
770 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
771 fuse_invalidate_attr(d_inode(newent));
772 fuse_invalidate_entry_cache(newent);
773 fuse_update_ctime(d_inode(newent));
775 } else if (err == -EINTR) {
776 /* If request was interrupted, DEITY only knows if the
777 rename actually took place. If the invalidation
778 fails (e.g. some process has CWD under the renamed
779 directory), then there can be inconsistency between
780 the dcache and the real filesystem. Tough luck. */
781 fuse_invalidate_entry(oldent);
782 if (d_really_is_positive(newent))
783 fuse_invalidate_entry(newent);
789 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
790 struct inode *newdir, struct dentry *newent,
793 struct fuse_conn *fc = get_fuse_conn(olddir);
796 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
800 if (fc->no_rename2 || fc->minor < 23)
803 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
805 sizeof(struct fuse_rename2_in));
806 if (err == -ENOSYS) {
811 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
813 sizeof(struct fuse_rename_in));
819 static int fuse_link(struct dentry *entry, struct inode *newdir,
820 struct dentry *newent)
823 struct fuse_link_in inarg;
824 struct inode *inode = d_inode(entry);
825 struct fuse_conn *fc = get_fuse_conn(inode);
828 memset(&inarg, 0, sizeof(inarg));
829 inarg.oldnodeid = get_node_id(inode);
830 args.in.h.opcode = FUSE_LINK;
832 args.in.args[0].size = sizeof(inarg);
833 args.in.args[0].value = &inarg;
834 args.in.args[1].size = newent->d_name.len + 1;
835 args.in.args[1].value = newent->d_name.name;
836 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
837 /* Contrary to "normal" filesystems it can happen that link
838 makes two "logical" inodes point to the same "physical"
839 inode. We invalidate the attributes of the old one, so it
840 will reflect changes in the backing inode (link count,
844 struct fuse_inode *fi = get_fuse_inode(inode);
846 spin_lock(&fc->lock);
847 fi->attr_version = ++fc->attr_version;
848 if (likely(inode->i_nlink < UINT_MAX))
850 spin_unlock(&fc->lock);
851 fuse_invalidate_attr(inode);
852 fuse_update_ctime(inode);
853 } else if (err == -EINTR) {
854 fuse_invalidate_attr(inode);
859 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
862 unsigned int blkbits;
863 struct fuse_conn *fc = get_fuse_conn(inode);
865 /* see the comment in fuse_change_attributes() */
866 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
867 attr->size = i_size_read(inode);
868 attr->mtime = inode->i_mtime.tv_sec;
869 attr->mtimensec = inode->i_mtime.tv_nsec;
870 attr->ctime = inode->i_ctime.tv_sec;
871 attr->ctimensec = inode->i_ctime.tv_nsec;
874 stat->dev = inode->i_sb->s_dev;
875 stat->ino = attr->ino;
876 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
877 stat->nlink = attr->nlink;
878 stat->uid = make_kuid(fc->user_ns, attr->uid);
879 stat->gid = make_kgid(fc->user_ns, attr->gid);
880 stat->rdev = inode->i_rdev;
881 stat->atime.tv_sec = attr->atime;
882 stat->atime.tv_nsec = attr->atimensec;
883 stat->mtime.tv_sec = attr->mtime;
884 stat->mtime.tv_nsec = attr->mtimensec;
885 stat->ctime.tv_sec = attr->ctime;
886 stat->ctime.tv_nsec = attr->ctimensec;
887 stat->size = attr->size;
888 stat->blocks = attr->blocks;
890 if (attr->blksize != 0)
891 blkbits = ilog2(attr->blksize);
893 blkbits = inode->i_sb->s_blocksize_bits;
895 stat->blksize = 1 << blkbits;
898 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
902 struct fuse_getattr_in inarg;
903 struct fuse_attr_out outarg;
904 struct fuse_conn *fc = get_fuse_conn(inode);
908 attr_version = fuse_get_attr_version(fc);
910 memset(&inarg, 0, sizeof(inarg));
911 memset(&outarg, 0, sizeof(outarg));
912 /* Directories have separate file-handle space */
913 if (file && S_ISREG(inode->i_mode)) {
914 struct fuse_file *ff = file->private_data;
916 inarg.getattr_flags |= FUSE_GETATTR_FH;
919 args.in.h.opcode = FUSE_GETATTR;
920 args.in.h.nodeid = get_node_id(inode);
922 args.in.args[0].size = sizeof(inarg);
923 args.in.args[0].value = &inarg;
924 args.out.numargs = 1;
925 args.out.args[0].size = sizeof(outarg);
926 args.out.args[0].value = &outarg;
927 err = fuse_simple_request(fc, &args);
929 if (fuse_invalid_attr(&outarg.attr) ||
930 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
931 make_bad_inode(inode);
934 fuse_change_attributes(inode, &outarg.attr,
935 attr_timeout(&outarg),
938 fuse_fillattr(inode, &outarg.attr, stat);
944 static int fuse_update_get_attr(struct inode *inode, struct file *file,
945 struct kstat *stat, unsigned int flags)
947 struct fuse_inode *fi = get_fuse_inode(inode);
951 if (flags & AT_STATX_FORCE_SYNC)
953 else if (flags & AT_STATX_DONT_SYNC)
956 sync = time_before64(fi->i_time, get_jiffies_64());
959 forget_all_cached_acls(inode);
960 err = fuse_do_getattr(inode, stat, file);
962 generic_fillattr(inode, stat);
963 stat->mode = fi->orig_i_mode;
964 stat->ino = fi->orig_ino;
970 int fuse_update_attributes(struct inode *inode, struct file *file)
972 return fuse_update_get_attr(inode, file, NULL, 0);
975 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
976 u64 child_nodeid, struct qstr *name)
979 struct inode *parent;
981 struct dentry *entry;
983 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
988 if (!S_ISDIR(parent->i_mode))
992 dir = d_find_alias(parent);
996 name->hash = full_name_hash(dir, name->name, name->len);
997 entry = d_lookup(dir, name);
1002 fuse_invalidate_attr(parent);
1003 fuse_invalidate_entry(entry);
1005 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1006 inode_lock(d_inode(entry));
1007 if (get_node_id(d_inode(entry)) != child_nodeid) {
1011 if (d_mountpoint(entry)) {
1015 if (d_is_dir(entry)) {
1016 shrink_dcache_parent(entry);
1017 if (!simple_empty(entry)) {
1021 d_inode(entry)->i_flags |= S_DEAD;
1024 clear_nlink(d_inode(entry));
1027 inode_unlock(d_inode(entry));
1036 inode_unlock(parent);
1042 * Calling into a user-controlled filesystem gives the filesystem
1043 * daemon ptrace-like capabilities over the current process. This
1044 * means, that the filesystem daemon is able to record the exact
1045 * filesystem operations performed, and can also control the behavior
1046 * of the requester process in otherwise impossible ways. For example
1047 * it can delay the operation for arbitrary length of time allowing
1048 * DoS against the requester.
1050 * For this reason only those processes can call into the filesystem,
1051 * for which the owner of the mount has ptrace privilege. This
1052 * excludes processes started by other users, suid or sgid processes.
1054 int fuse_allow_current_process(struct fuse_conn *fc)
1056 const struct cred *cred;
1058 if (fc->allow_other)
1059 return current_in_userns(fc->user_ns);
1061 cred = current_cred();
1062 if (uid_eq(cred->euid, fc->user_id) &&
1063 uid_eq(cred->suid, fc->user_id) &&
1064 uid_eq(cred->uid, fc->user_id) &&
1065 gid_eq(cred->egid, fc->group_id) &&
1066 gid_eq(cred->sgid, fc->group_id) &&
1067 gid_eq(cred->gid, fc->group_id))
1073 static int fuse_access(struct inode *inode, int mask)
1075 struct fuse_conn *fc = get_fuse_conn(inode);
1077 struct fuse_access_in inarg;
1080 BUG_ON(mask & MAY_NOT_BLOCK);
1085 memset(&inarg, 0, sizeof(inarg));
1086 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1087 args.in.h.opcode = FUSE_ACCESS;
1088 args.in.h.nodeid = get_node_id(inode);
1089 args.in.numargs = 1;
1090 args.in.args[0].size = sizeof(inarg);
1091 args.in.args[0].value = &inarg;
1092 err = fuse_simple_request(fc, &args);
1093 if (err == -ENOSYS) {
1100 static int fuse_perm_getattr(struct inode *inode, int mask)
1102 if (mask & MAY_NOT_BLOCK)
1105 forget_all_cached_acls(inode);
1106 return fuse_do_getattr(inode, NULL, NULL);
1110 * Check permission. The two basic access models of FUSE are:
1112 * 1) Local access checking ('default_permissions' mount option) based
1113 * on file mode. This is the plain old disk filesystem permission
1116 * 2) "Remote" access checking, where server is responsible for
1117 * checking permission in each inode operation. An exception to this
1118 * is if ->permission() was invoked from sys_access() in which case an
1119 * access request is sent. Execute permission is still checked
1120 * locally based on file mode.
1122 static int fuse_permission(struct inode *inode, int mask)
1124 struct fuse_conn *fc = get_fuse_conn(inode);
1125 bool refreshed = false;
1128 if (!fuse_allow_current_process(fc))
1132 * If attributes are needed, refresh them before proceeding
1134 if (fc->default_permissions ||
1135 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1136 struct fuse_inode *fi = get_fuse_inode(inode);
1138 if (time_before64(fi->i_time, get_jiffies_64())) {
1141 err = fuse_perm_getattr(inode, mask);
1147 if (fc->default_permissions) {
1148 err = generic_permission(inode, mask);
1150 /* If permission is denied, try to refresh file
1151 attributes. This is also needed, because the root
1152 node will at first have no permissions */
1153 if (err == -EACCES && !refreshed) {
1154 err = fuse_perm_getattr(inode, mask);
1156 err = generic_permission(inode, mask);
1159 /* Note: the opposite of the above test does not
1160 exist. So if permissions are revoked this won't be
1161 noticed immediately, only after the attribute
1162 timeout has expired */
1163 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1164 err = fuse_access(inode, mask);
1165 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1166 if (!(inode->i_mode & S_IXUGO)) {
1170 err = fuse_perm_getattr(inode, mask);
1171 if (!err && !(inode->i_mode & S_IXUGO))
1178 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1179 struct dir_context *ctx)
1181 while (nbytes >= FUSE_NAME_OFFSET) {
1182 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1183 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1184 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1186 if (reclen > nbytes)
1188 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1191 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1192 dirent->ino, dirent->type))
1197 ctx->pos = dirent->off;
1203 static int fuse_direntplus_link(struct file *file,
1204 struct fuse_direntplus *direntplus,
1207 struct fuse_entry_out *o = &direntplus->entry_out;
1208 struct fuse_dirent *dirent = &direntplus->dirent;
1209 struct dentry *parent = file->f_path.dentry;
1210 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1211 struct dentry *dentry;
1212 struct dentry *alias;
1213 struct inode *dir = d_inode(parent);
1214 struct fuse_conn *fc;
1215 struct inode *inode;
1216 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1220 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1221 * ENOENT. Instead, it only means the userspace filesystem did
1222 * not want to return attributes/handle for this entry.
1229 if (name.name[0] == '.') {
1231 * We could potentially refresh the attributes of the directory
1236 if (name.name[1] == '.' && name.len == 2)
1240 if (invalid_nodeid(o->nodeid))
1242 if (fuse_invalid_attr(&o->attr))
1245 fc = get_fuse_conn(dir);
1247 name.hash = full_name_hash(parent, name.name, name.len);
1248 dentry = d_lookup(parent, &name);
1251 dentry = d_alloc_parallel(parent, &name, &wq);
1253 return PTR_ERR(dentry);
1255 if (!d_in_lookup(dentry)) {
1256 struct fuse_inode *fi;
1257 inode = d_inode(dentry);
1259 get_node_id(inode) != o->nodeid ||
1260 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1261 d_invalidate(dentry);
1265 if (is_bad_inode(inode)) {
1270 fi = get_fuse_inode(inode);
1271 spin_lock(&fc->lock);
1273 spin_unlock(&fc->lock);
1275 forget_all_cached_acls(inode);
1276 fuse_change_attributes(inode, &o->attr,
1277 entry_attr_timeout(o),
1280 * The other branch comes via fuse_iget()
1281 * which bumps nlookup inside
1284 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1285 &o->attr, entry_attr_timeout(o),
1288 inode = ERR_PTR(-ENOMEM);
1290 alias = d_splice_alias(inode, dentry);
1291 d_lookup_done(dentry);
1297 return PTR_ERR(dentry);
1299 if (fc->readdirplus_auto)
1300 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1301 fuse_change_entry_timeout(dentry, o);
1307 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1308 struct dir_context *ctx, u64 attr_version)
1310 struct fuse_direntplus *direntplus;
1311 struct fuse_dirent *dirent;
1316 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1317 direntplus = (struct fuse_direntplus *) buf;
1318 dirent = &direntplus->dirent;
1319 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1321 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1323 if (reclen > nbytes)
1325 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1329 /* We fill entries into dstbuf only as much as
1330 it can hold. But we still continue iterating
1331 over remaining entries to link them. If not,
1332 we need to send a FORGET for each of those
1333 which we did not link.
1335 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1336 dirent->ino, dirent->type);
1338 ctx->pos = dirent->off;
1344 ret = fuse_direntplus_link(file, direntplus, attr_version);
1346 fuse_force_forget(file, direntplus->entry_out.nodeid);
1352 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1357 struct inode *inode = file_inode(file);
1358 struct fuse_conn *fc = get_fuse_conn(inode);
1359 struct fuse_req *req;
1360 u64 attr_version = 0;
1363 if (is_bad_inode(inode))
1366 req = fuse_get_req(fc, 1);
1368 return PTR_ERR(req);
1370 page = alloc_page(GFP_KERNEL);
1372 fuse_put_request(fc, req);
1376 plus = fuse_use_readdirplus(inode, ctx);
1377 req->out.argpages = 1;
1379 req->pages[0] = page;
1380 req->page_descs[0].length = PAGE_SIZE;
1382 attr_version = fuse_get_attr_version(fc);
1383 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1386 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1389 locked = fuse_lock_inode(inode);
1390 fuse_request_send(fc, req);
1391 fuse_unlock_inode(inode, locked);
1392 nbytes = req->out.args[0].size;
1393 err = req->out.h.error;
1394 fuse_put_request(fc, req);
1397 err = parse_dirplusfile(page_address(page), nbytes,
1401 err = parse_dirfile(page_address(page), nbytes, file,
1407 fuse_invalidate_atime(inode);
1411 static const char *fuse_get_link(struct dentry *dentry,
1412 struct inode *inode,
1413 struct delayed_call *done)
1415 struct fuse_conn *fc = get_fuse_conn(inode);
1421 return ERR_PTR(-ECHILD);
1423 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
1425 return ERR_PTR(-ENOMEM);
1427 args.in.h.opcode = FUSE_READLINK;
1428 args.in.h.nodeid = get_node_id(inode);
1429 args.out.argvar = 1;
1430 args.out.numargs = 1;
1431 args.out.args[0].size = PAGE_SIZE - 1;
1432 args.out.args[0].value = link;
1433 ret = fuse_simple_request(fc, &args);
1436 link = ERR_PTR(ret);
1439 set_delayed_call(done, kfree_link, link);
1441 fuse_invalidate_atime(inode);
1445 static int fuse_dir_open(struct inode *inode, struct file *file)
1447 return fuse_open_common(inode, file, true);
1450 static int fuse_dir_release(struct inode *inode, struct file *file)
1452 fuse_release_common(file, true);
1457 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1460 return fuse_fsync_common(file, start, end, datasync, 1);
1463 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1466 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1468 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1472 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1475 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1478 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1483 return fuse_ioctl_common(file, cmd, arg,
1484 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1487 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1489 /* Always update if mtime is explicitly set */
1490 if (ivalid & ATTR_MTIME_SET)
1493 /* Or if kernel i_mtime is the official one */
1494 if (trust_local_mtime)
1497 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1498 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1501 /* In all other cases update */
1505 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1506 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1508 unsigned ivalid = iattr->ia_valid;
1510 if (ivalid & ATTR_MODE)
1511 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1512 if (ivalid & ATTR_UID)
1513 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1514 if (ivalid & ATTR_GID)
1515 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1516 if (ivalid & ATTR_SIZE)
1517 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1518 if (ivalid & ATTR_ATIME) {
1519 arg->valid |= FATTR_ATIME;
1520 arg->atime = iattr->ia_atime.tv_sec;
1521 arg->atimensec = iattr->ia_atime.tv_nsec;
1522 if (!(ivalid & ATTR_ATIME_SET))
1523 arg->valid |= FATTR_ATIME_NOW;
1525 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1526 arg->valid |= FATTR_MTIME;
1527 arg->mtime = iattr->ia_mtime.tv_sec;
1528 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1529 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1530 arg->valid |= FATTR_MTIME_NOW;
1532 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1533 arg->valid |= FATTR_CTIME;
1534 arg->ctime = iattr->ia_ctime.tv_sec;
1535 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1540 * Prevent concurrent writepages on inode
1542 * This is done by adding a negative bias to the inode write counter
1543 * and waiting for all pending writes to finish.
1545 void fuse_set_nowrite(struct inode *inode)
1547 struct fuse_conn *fc = get_fuse_conn(inode);
1548 struct fuse_inode *fi = get_fuse_inode(inode);
1550 BUG_ON(!inode_is_locked(inode));
1552 spin_lock(&fc->lock);
1553 BUG_ON(fi->writectr < 0);
1554 fi->writectr += FUSE_NOWRITE;
1555 spin_unlock(&fc->lock);
1556 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1560 * Allow writepages on inode
1562 * Remove the bias from the writecounter and send any queued
1565 static void __fuse_release_nowrite(struct inode *inode)
1567 struct fuse_inode *fi = get_fuse_inode(inode);
1569 BUG_ON(fi->writectr != FUSE_NOWRITE);
1571 fuse_flush_writepages(inode);
1574 void fuse_release_nowrite(struct inode *inode)
1576 struct fuse_conn *fc = get_fuse_conn(inode);
1578 spin_lock(&fc->lock);
1579 __fuse_release_nowrite(inode);
1580 spin_unlock(&fc->lock);
1583 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1584 struct inode *inode,
1585 struct fuse_setattr_in *inarg_p,
1586 struct fuse_attr_out *outarg_p)
1588 args->in.h.opcode = FUSE_SETATTR;
1589 args->in.h.nodeid = get_node_id(inode);
1590 args->in.numargs = 1;
1591 args->in.args[0].size = sizeof(*inarg_p);
1592 args->in.args[0].value = inarg_p;
1593 args->out.numargs = 1;
1594 args->out.args[0].size = sizeof(*outarg_p);
1595 args->out.args[0].value = outarg_p;
1599 * Flush inode->i_mtime to the server
1601 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1603 struct fuse_conn *fc = get_fuse_conn(inode);
1605 struct fuse_setattr_in inarg;
1606 struct fuse_attr_out outarg;
1608 memset(&inarg, 0, sizeof(inarg));
1609 memset(&outarg, 0, sizeof(outarg));
1611 inarg.valid = FATTR_MTIME;
1612 inarg.mtime = inode->i_mtime.tv_sec;
1613 inarg.mtimensec = inode->i_mtime.tv_nsec;
1614 if (fc->minor >= 23) {
1615 inarg.valid |= FATTR_CTIME;
1616 inarg.ctime = inode->i_ctime.tv_sec;
1617 inarg.ctimensec = inode->i_ctime.tv_nsec;
1620 inarg.valid |= FATTR_FH;
1623 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1625 return fuse_simple_request(fc, &args);
1629 * Set attributes, and at the same time refresh them.
1631 * Truncation is slightly complicated, because the 'truncate' request
1632 * may fail, in which case we don't want to touch the mapping.
1633 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1634 * and the actual truncation by hand.
1636 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1639 struct inode *inode = d_inode(dentry);
1640 struct fuse_conn *fc = get_fuse_conn(inode);
1641 struct fuse_inode *fi = get_fuse_inode(inode);
1643 struct fuse_setattr_in inarg;
1644 struct fuse_attr_out outarg;
1645 bool is_truncate = false;
1646 bool is_wb = fc->writeback_cache;
1649 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1651 if (!fc->default_permissions)
1652 attr->ia_valid |= ATTR_FORCE;
1654 err = setattr_prepare(dentry, attr);
1658 if (attr->ia_valid & ATTR_OPEN) {
1659 /* This is coming from open(..., ... | O_TRUNC); */
1660 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1661 WARN_ON(attr->ia_size != 0);
1662 if (fc->atomic_o_trunc) {
1664 * No need to send request to userspace, since actual
1665 * truncation has already been done by OPEN. But still
1666 * need to truncate page cache.
1668 i_size_write(inode, 0);
1669 truncate_pagecache(inode, 0);
1675 if (attr->ia_valid & ATTR_SIZE)
1678 /* Flush dirty data/metadata before non-truncate SETATTR */
1679 if (is_wb && S_ISREG(inode->i_mode) &&
1681 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1683 err = write_inode_now(inode, true);
1687 fuse_set_nowrite(inode);
1688 fuse_release_nowrite(inode);
1692 fuse_set_nowrite(inode);
1693 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1694 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1695 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1698 memset(&inarg, 0, sizeof(inarg));
1699 memset(&outarg, 0, sizeof(outarg));
1700 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1702 struct fuse_file *ff = file->private_data;
1703 inarg.valid |= FATTR_FH;
1706 if (attr->ia_valid & ATTR_SIZE) {
1707 /* For mandatory locking in truncate */
1708 inarg.valid |= FATTR_LOCKOWNER;
1709 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1711 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1712 err = fuse_simple_request(fc, &args);
1715 fuse_invalidate_attr(inode);
1719 if (fuse_invalid_attr(&outarg.attr) ||
1720 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1721 make_bad_inode(inode);
1726 spin_lock(&fc->lock);
1727 /* the kernel maintains i_mtime locally */
1728 if (trust_local_cmtime) {
1729 if (attr->ia_valid & ATTR_MTIME)
1730 inode->i_mtime = attr->ia_mtime;
1731 if (attr->ia_valid & ATTR_CTIME)
1732 inode->i_ctime = attr->ia_ctime;
1733 /* FIXME: clear I_DIRTY_SYNC? */
1736 fuse_change_attributes_common(inode, &outarg.attr,
1737 attr_timeout(&outarg));
1738 oldsize = inode->i_size;
1739 /* see the comment in fuse_change_attributes() */
1740 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1741 i_size_write(inode, outarg.attr.size);
1744 /* NOTE: this may release/reacquire fc->lock */
1745 __fuse_release_nowrite(inode);
1747 spin_unlock(&fc->lock);
1750 * Only call invalidate_inode_pages2() after removing
1751 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1753 if ((is_truncate || !is_wb) &&
1754 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1755 truncate_pagecache(inode, outarg.attr.size);
1756 invalidate_inode_pages2(inode->i_mapping);
1759 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1764 fuse_release_nowrite(inode);
1766 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1770 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1772 struct inode *inode = d_inode(entry);
1773 struct fuse_conn *fc = get_fuse_conn(inode);
1774 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1777 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1780 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1781 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1785 * The only sane way to reliably kill suid/sgid is to do it in
1786 * the userspace filesystem
1788 * This should be done on write(), truncate() and chown().
1790 if (!fc->handle_killpriv) {
1792 * ia_mode calculation may have used stale i_mode.
1793 * Refresh and recalculate.
1795 ret = fuse_do_getattr(inode, NULL, file);
1799 attr->ia_mode = inode->i_mode;
1800 if (inode->i_mode & S_ISUID) {
1801 attr->ia_valid |= ATTR_MODE;
1802 attr->ia_mode &= ~S_ISUID;
1804 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1805 attr->ia_valid |= ATTR_MODE;
1806 attr->ia_mode &= ~S_ISGID;
1810 if (!attr->ia_valid)
1813 ret = fuse_do_setattr(entry, attr, file);
1816 * If filesystem supports acls it may have updated acl xattrs in
1817 * the filesystem, so forget cached acls for the inode.
1820 forget_all_cached_acls(inode);
1822 /* Directory mode changed, may need to revalidate access */
1823 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1824 fuse_invalidate_entry_cache(entry);
1829 static int fuse_getattr(const struct path *path, struct kstat *stat,
1830 u32 request_mask, unsigned int flags)
1832 struct inode *inode = d_inode(path->dentry);
1833 struct fuse_conn *fc = get_fuse_conn(inode);
1835 if (!fuse_allow_current_process(fc))
1838 return fuse_update_get_attr(inode, NULL, stat, flags);
1841 static const struct inode_operations fuse_dir_inode_operations = {
1842 .lookup = fuse_lookup,
1843 .mkdir = fuse_mkdir,
1844 .symlink = fuse_symlink,
1845 .unlink = fuse_unlink,
1846 .rmdir = fuse_rmdir,
1847 .rename = fuse_rename2,
1849 .setattr = fuse_setattr,
1850 .create = fuse_create,
1851 .atomic_open = fuse_atomic_open,
1852 .mknod = fuse_mknod,
1853 .permission = fuse_permission,
1854 .getattr = fuse_getattr,
1855 .listxattr = fuse_listxattr,
1856 .get_acl = fuse_get_acl,
1857 .set_acl = fuse_set_acl,
1860 static const struct file_operations fuse_dir_operations = {
1861 .llseek = generic_file_llseek,
1862 .read = generic_read_dir,
1863 .iterate_shared = fuse_readdir,
1864 .open = fuse_dir_open,
1865 .release = fuse_dir_release,
1866 .fsync = fuse_dir_fsync,
1867 .unlocked_ioctl = fuse_dir_ioctl,
1868 .compat_ioctl = fuse_dir_compat_ioctl,
1871 static const struct inode_operations fuse_common_inode_operations = {
1872 .setattr = fuse_setattr,
1873 .permission = fuse_permission,
1874 .getattr = fuse_getattr,
1875 .listxattr = fuse_listxattr,
1876 .get_acl = fuse_get_acl,
1877 .set_acl = fuse_set_acl,
1880 static const struct inode_operations fuse_symlink_inode_operations = {
1881 .setattr = fuse_setattr,
1882 .get_link = fuse_get_link,
1883 .getattr = fuse_getattr,
1884 .listxattr = fuse_listxattr,
1887 void fuse_init_common(struct inode *inode)
1889 inode->i_op = &fuse_common_inode_operations;
1892 void fuse_init_dir(struct inode *inode)
1894 inode->i_op = &fuse_dir_inode_operations;
1895 inode->i_fop = &fuse_dir_operations;
1898 void fuse_init_symlink(struct inode *inode)
1900 inode->i_op = &fuse_symlink_inode_operations;