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,
410 umode_t mode, int *opened)
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, opened);
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,
501 umode_t mode, int *opened)
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 *opened |= FILE_CREATED;
525 err = fuse_create_open(dir, entry, file, flags, mode, opened);
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;
552 struct fuse_forget_link *forget;
554 forget = fuse_alloc_forget();
558 memset(&outarg, 0, sizeof(outarg));
559 args->in.h.nodeid = get_node_id(dir);
560 args->out.numargs = 1;
561 args->out.args[0].size = sizeof(outarg);
562 args->out.args[0].value = &outarg;
563 err = fuse_simple_request(fc, args);
565 goto out_put_forget_req;
568 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
569 goto out_put_forget_req;
571 if ((outarg.attr.mode ^ mode) & S_IFMT)
572 goto out_put_forget_req;
574 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
575 &outarg.attr, entry_attr_timeout(&outarg), 0);
577 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
582 err = d_instantiate_no_diralias(entry, inode);
586 fuse_change_entry_timeout(entry, &outarg);
587 fuse_invalidate_attr(dir);
595 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
598 struct fuse_mknod_in inarg;
599 struct fuse_conn *fc = get_fuse_conn(dir);
603 mode &= ~current_umask();
605 memset(&inarg, 0, sizeof(inarg));
607 inarg.rdev = new_encode_dev(rdev);
608 inarg.umask = current_umask();
609 args.in.h.opcode = FUSE_MKNOD;
611 args.in.args[0].size = sizeof(inarg);
612 args.in.args[0].value = &inarg;
613 args.in.args[1].size = entry->d_name.len + 1;
614 args.in.args[1].value = entry->d_name.name;
615 return create_new_entry(fc, &args, dir, entry, mode);
618 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
621 return fuse_mknod(dir, entry, mode, 0);
624 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
626 struct fuse_mkdir_in inarg;
627 struct fuse_conn *fc = get_fuse_conn(dir);
631 mode &= ~current_umask();
633 memset(&inarg, 0, sizeof(inarg));
635 inarg.umask = current_umask();
636 args.in.h.opcode = FUSE_MKDIR;
638 args.in.args[0].size = sizeof(inarg);
639 args.in.args[0].value = &inarg;
640 args.in.args[1].size = entry->d_name.len + 1;
641 args.in.args[1].value = entry->d_name.name;
642 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
645 static int fuse_symlink(struct inode *dir, struct dentry *entry,
648 struct fuse_conn *fc = get_fuse_conn(dir);
649 unsigned len = strlen(link) + 1;
652 args.in.h.opcode = FUSE_SYMLINK;
654 args.in.args[0].size = entry->d_name.len + 1;
655 args.in.args[0].value = entry->d_name.name;
656 args.in.args[1].size = len;
657 args.in.args[1].value = link;
658 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
661 void fuse_update_ctime(struct inode *inode)
663 if (!IS_NOCMTIME(inode)) {
664 inode->i_ctime = current_time(inode);
665 mark_inode_dirty_sync(inode);
669 static int fuse_unlink(struct inode *dir, struct dentry *entry)
672 struct fuse_conn *fc = get_fuse_conn(dir);
675 args.in.h.opcode = FUSE_UNLINK;
676 args.in.h.nodeid = get_node_id(dir);
678 args.in.args[0].size = entry->d_name.len + 1;
679 args.in.args[0].value = entry->d_name.name;
680 err = fuse_simple_request(fc, &args);
682 struct inode *inode = d_inode(entry);
683 struct fuse_inode *fi = get_fuse_inode(inode);
685 spin_lock(&fc->lock);
686 fi->attr_version = ++fc->attr_version;
688 * If i_nlink == 0 then unlink doesn't make sense, yet this can
689 * happen if userspace filesystem is careless. It would be
690 * difficult to enforce correct nlink usage so just ignore this
693 if (inode->i_nlink > 0)
695 spin_unlock(&fc->lock);
696 fuse_invalidate_attr(inode);
697 fuse_invalidate_attr(dir);
698 fuse_invalidate_entry_cache(entry);
699 fuse_update_ctime(inode);
700 } else if (err == -EINTR)
701 fuse_invalidate_entry(entry);
705 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
708 struct fuse_conn *fc = get_fuse_conn(dir);
711 args.in.h.opcode = FUSE_RMDIR;
712 args.in.h.nodeid = get_node_id(dir);
714 args.in.args[0].size = entry->d_name.len + 1;
715 args.in.args[0].value = entry->d_name.name;
716 err = fuse_simple_request(fc, &args);
718 clear_nlink(d_inode(entry));
719 fuse_invalidate_attr(dir);
720 fuse_invalidate_entry_cache(entry);
721 } else if (err == -EINTR)
722 fuse_invalidate_entry(entry);
726 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
727 struct inode *newdir, struct dentry *newent,
728 unsigned int flags, int opcode, size_t argsize)
731 struct fuse_rename2_in inarg;
732 struct fuse_conn *fc = get_fuse_conn(olddir);
735 memset(&inarg, 0, argsize);
736 inarg.newdir = get_node_id(newdir);
738 args.in.h.opcode = opcode;
739 args.in.h.nodeid = get_node_id(olddir);
741 args.in.args[0].size = argsize;
742 args.in.args[0].value = &inarg;
743 args.in.args[1].size = oldent->d_name.len + 1;
744 args.in.args[1].value = oldent->d_name.name;
745 args.in.args[2].size = newent->d_name.len + 1;
746 args.in.args[2].value = newent->d_name.name;
747 err = fuse_simple_request(fc, &args);
750 fuse_invalidate_attr(d_inode(oldent));
751 fuse_update_ctime(d_inode(oldent));
753 if (flags & RENAME_EXCHANGE) {
754 fuse_invalidate_attr(d_inode(newent));
755 fuse_update_ctime(d_inode(newent));
758 fuse_invalidate_attr(olddir);
759 if (olddir != newdir)
760 fuse_invalidate_attr(newdir);
762 /* newent will end up negative */
763 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
764 fuse_invalidate_attr(d_inode(newent));
765 fuse_invalidate_entry_cache(newent);
766 fuse_update_ctime(d_inode(newent));
768 } else if (err == -EINTR) {
769 /* If request was interrupted, DEITY only knows if the
770 rename actually took place. If the invalidation
771 fails (e.g. some process has CWD under the renamed
772 directory), then there can be inconsistency between
773 the dcache and the real filesystem. Tough luck. */
774 fuse_invalidate_entry(oldent);
775 if (d_really_is_positive(newent))
776 fuse_invalidate_entry(newent);
782 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
783 struct inode *newdir, struct dentry *newent,
786 struct fuse_conn *fc = get_fuse_conn(olddir);
789 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
793 if (fc->no_rename2 || fc->minor < 23)
796 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
798 sizeof(struct fuse_rename2_in));
799 if (err == -ENOSYS) {
804 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
806 sizeof(struct fuse_rename_in));
812 static int fuse_link(struct dentry *entry, struct inode *newdir,
813 struct dentry *newent)
816 struct fuse_link_in inarg;
817 struct inode *inode = d_inode(entry);
818 struct fuse_conn *fc = get_fuse_conn(inode);
821 memset(&inarg, 0, sizeof(inarg));
822 inarg.oldnodeid = get_node_id(inode);
823 args.in.h.opcode = FUSE_LINK;
825 args.in.args[0].size = sizeof(inarg);
826 args.in.args[0].value = &inarg;
827 args.in.args[1].size = newent->d_name.len + 1;
828 args.in.args[1].value = newent->d_name.name;
829 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
830 /* Contrary to "normal" filesystems it can happen that link
831 makes two "logical" inodes point to the same "physical"
832 inode. We invalidate the attributes of the old one, so it
833 will reflect changes in the backing inode (link count,
837 struct fuse_inode *fi = get_fuse_inode(inode);
839 spin_lock(&fc->lock);
840 fi->attr_version = ++fc->attr_version;
841 if (likely(inode->i_nlink < UINT_MAX))
843 spin_unlock(&fc->lock);
844 fuse_invalidate_attr(inode);
845 fuse_update_ctime(inode);
846 } else if (err == -EINTR) {
847 fuse_invalidate_attr(inode);
852 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
855 unsigned int blkbits;
856 struct fuse_conn *fc = get_fuse_conn(inode);
858 /* see the comment in fuse_change_attributes() */
859 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
860 attr->size = i_size_read(inode);
861 attr->mtime = inode->i_mtime.tv_sec;
862 attr->mtimensec = inode->i_mtime.tv_nsec;
863 attr->ctime = inode->i_ctime.tv_sec;
864 attr->ctimensec = inode->i_ctime.tv_nsec;
867 stat->dev = inode->i_sb->s_dev;
868 stat->ino = attr->ino;
869 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
870 stat->nlink = attr->nlink;
871 stat->uid = make_kuid(&init_user_ns, attr->uid);
872 stat->gid = make_kgid(&init_user_ns, attr->gid);
873 stat->rdev = inode->i_rdev;
874 stat->atime.tv_sec = attr->atime;
875 stat->atime.tv_nsec = attr->atimensec;
876 stat->mtime.tv_sec = attr->mtime;
877 stat->mtime.tv_nsec = attr->mtimensec;
878 stat->ctime.tv_sec = attr->ctime;
879 stat->ctime.tv_nsec = attr->ctimensec;
880 stat->size = attr->size;
881 stat->blocks = attr->blocks;
883 if (attr->blksize != 0)
884 blkbits = ilog2(attr->blksize);
886 blkbits = inode->i_sb->s_blocksize_bits;
888 stat->blksize = 1 << blkbits;
891 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
895 struct fuse_getattr_in inarg;
896 struct fuse_attr_out outarg;
897 struct fuse_conn *fc = get_fuse_conn(inode);
901 attr_version = fuse_get_attr_version(fc);
903 memset(&inarg, 0, sizeof(inarg));
904 memset(&outarg, 0, sizeof(outarg));
905 /* Directories have separate file-handle space */
906 if (file && S_ISREG(inode->i_mode)) {
907 struct fuse_file *ff = file->private_data;
909 inarg.getattr_flags |= FUSE_GETATTR_FH;
912 args.in.h.opcode = FUSE_GETATTR;
913 args.in.h.nodeid = get_node_id(inode);
915 args.in.args[0].size = sizeof(inarg);
916 args.in.args[0].value = &inarg;
917 args.out.numargs = 1;
918 args.out.args[0].size = sizeof(outarg);
919 args.out.args[0].value = &outarg;
920 err = fuse_simple_request(fc, &args);
922 if (fuse_invalid_attr(&outarg.attr) ||
923 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
924 make_bad_inode(inode);
927 fuse_change_attributes(inode, &outarg.attr,
928 attr_timeout(&outarg),
931 fuse_fillattr(inode, &outarg.attr, stat);
937 static int fuse_update_get_attr(struct inode *inode, struct file *file,
940 struct fuse_inode *fi = get_fuse_inode(inode);
943 if (time_before64(fi->i_time, get_jiffies_64())) {
944 forget_all_cached_acls(inode);
945 err = fuse_do_getattr(inode, stat, file);
947 generic_fillattr(inode, stat);
948 stat->mode = fi->orig_i_mode;
949 stat->ino = fi->orig_ino;
955 int fuse_update_attributes(struct inode *inode, struct file *file)
957 return fuse_update_get_attr(inode, file, NULL);
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);
972 inode_lock_nested(parent, I_MUTEX_PARENT);
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_invalidate_attr(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)
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);
1123 if (time_before64(fi->i_time, get_jiffies_64())) {
1126 err = fuse_perm_getattr(inode, mask);
1132 if (fc->default_permissions) {
1133 err = generic_permission(inode, mask);
1135 /* If permission is denied, try to refresh file
1136 attributes. This is also needed, because the root
1137 node will at first have no permissions */
1138 if (err == -EACCES && !refreshed) {
1139 err = fuse_perm_getattr(inode, mask);
1141 err = generic_permission(inode, mask);
1144 /* Note: the opposite of the above test does not
1145 exist. So if permissions are revoked this won't be
1146 noticed immediately, only after the attribute
1147 timeout has expired */
1148 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1149 err = fuse_access(inode, mask);
1150 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1151 if (!(inode->i_mode & S_IXUGO)) {
1155 err = fuse_perm_getattr(inode, mask);
1156 if (!err && !(inode->i_mode & S_IXUGO))
1163 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1164 struct dir_context *ctx)
1166 while (nbytes >= FUSE_NAME_OFFSET) {
1167 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1168 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1169 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1171 if (reclen > nbytes)
1173 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1176 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1177 dirent->ino, dirent->type))
1182 ctx->pos = dirent->off;
1188 static int fuse_direntplus_link(struct file *file,
1189 struct fuse_direntplus *direntplus,
1192 struct fuse_entry_out *o = &direntplus->entry_out;
1193 struct fuse_dirent *dirent = &direntplus->dirent;
1194 struct dentry *parent = file->f_path.dentry;
1195 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1196 struct dentry *dentry;
1197 struct dentry *alias;
1198 struct inode *dir = d_inode(parent);
1199 struct fuse_conn *fc;
1200 struct inode *inode;
1201 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1205 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1206 * ENOENT. Instead, it only means the userspace filesystem did
1207 * not want to return attributes/handle for this entry.
1214 if (name.name[0] == '.') {
1216 * We could potentially refresh the attributes of the directory
1221 if (name.name[1] == '.' && name.len == 2)
1225 if (invalid_nodeid(o->nodeid))
1227 if (fuse_invalid_attr(&o->attr))
1230 fc = get_fuse_conn(dir);
1232 name.hash = full_name_hash(parent, name.name, name.len);
1233 dentry = d_lookup(parent, &name);
1236 dentry = d_alloc_parallel(parent, &name, &wq);
1238 return PTR_ERR(dentry);
1240 if (!d_in_lookup(dentry)) {
1241 struct fuse_inode *fi;
1242 inode = d_inode(dentry);
1244 get_node_id(inode) != o->nodeid ||
1245 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1246 d_invalidate(dentry);
1250 if (is_bad_inode(inode)) {
1255 fi = get_fuse_inode(inode);
1256 spin_lock(&fc->lock);
1258 spin_unlock(&fc->lock);
1260 forget_all_cached_acls(inode);
1261 fuse_change_attributes(inode, &o->attr,
1262 entry_attr_timeout(o),
1265 * The other branch comes via fuse_iget()
1266 * which bumps nlookup inside
1269 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1270 &o->attr, entry_attr_timeout(o),
1273 inode = ERR_PTR(-ENOMEM);
1275 alias = d_splice_alias(inode, dentry);
1276 d_lookup_done(dentry);
1282 return PTR_ERR(dentry);
1284 if (fc->readdirplus_auto)
1285 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1286 fuse_change_entry_timeout(dentry, o);
1292 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1293 struct dir_context *ctx, u64 attr_version)
1295 struct fuse_direntplus *direntplus;
1296 struct fuse_dirent *dirent;
1301 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1302 direntplus = (struct fuse_direntplus *) buf;
1303 dirent = &direntplus->dirent;
1304 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1306 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1308 if (reclen > nbytes)
1310 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1314 /* We fill entries into dstbuf only as much as
1315 it can hold. But we still continue iterating
1316 over remaining entries to link them. If not,
1317 we need to send a FORGET for each of those
1318 which we did not link.
1320 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1321 dirent->ino, dirent->type);
1323 ctx->pos = dirent->off;
1329 ret = fuse_direntplus_link(file, direntplus, attr_version);
1331 fuse_force_forget(file, direntplus->entry_out.nodeid);
1337 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1342 struct inode *inode = file_inode(file);
1343 struct fuse_conn *fc = get_fuse_conn(inode);
1344 struct fuse_req *req;
1345 u64 attr_version = 0;
1348 if (is_bad_inode(inode))
1351 req = fuse_get_req(fc, 1);
1353 return PTR_ERR(req);
1355 page = alloc_page(GFP_KERNEL);
1357 fuse_put_request(fc, req);
1361 plus = fuse_use_readdirplus(inode, ctx);
1362 req->out.argpages = 1;
1364 req->pages[0] = page;
1365 req->page_descs[0].length = PAGE_SIZE;
1367 attr_version = fuse_get_attr_version(fc);
1368 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1371 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1374 locked = fuse_lock_inode(inode);
1375 fuse_request_send(fc, req);
1376 fuse_unlock_inode(inode, locked);
1377 nbytes = req->out.args[0].size;
1378 err = req->out.h.error;
1379 fuse_put_request(fc, req);
1382 err = parse_dirplusfile(page_address(page), nbytes,
1386 err = parse_dirfile(page_address(page), nbytes, file,
1392 fuse_invalidate_atime(inode);
1396 static const char *fuse_get_link(struct dentry *dentry,
1397 struct inode *inode,
1398 struct delayed_call *done)
1400 struct fuse_conn *fc = get_fuse_conn(inode);
1406 return ERR_PTR(-ECHILD);
1408 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
1410 return ERR_PTR(-ENOMEM);
1412 args.in.h.opcode = FUSE_READLINK;
1413 args.in.h.nodeid = get_node_id(inode);
1414 args.out.argvar = 1;
1415 args.out.numargs = 1;
1416 args.out.args[0].size = PAGE_SIZE - 1;
1417 args.out.args[0].value = link;
1418 ret = fuse_simple_request(fc, &args);
1421 link = ERR_PTR(ret);
1424 set_delayed_call(done, kfree_link, link);
1426 fuse_invalidate_atime(inode);
1430 static int fuse_dir_open(struct inode *inode, struct file *file)
1432 return fuse_open_common(inode, file, true);
1435 static int fuse_dir_release(struct inode *inode, struct file *file)
1437 fuse_release_common(file, true);
1442 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1445 return fuse_fsync_common(file, start, end, datasync, 1);
1448 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1451 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1453 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1457 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1460 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1463 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1468 return fuse_ioctl_common(file, cmd, arg,
1469 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1472 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1474 /* Always update if mtime is explicitly set */
1475 if (ivalid & ATTR_MTIME_SET)
1478 /* Or if kernel i_mtime is the official one */
1479 if (trust_local_mtime)
1482 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1483 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1486 /* In all other cases update */
1490 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1491 bool trust_local_cmtime)
1493 unsigned ivalid = iattr->ia_valid;
1495 if (ivalid & ATTR_MODE)
1496 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1497 if (ivalid & ATTR_UID)
1498 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1499 if (ivalid & ATTR_GID)
1500 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1501 if (ivalid & ATTR_SIZE)
1502 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1503 if (ivalid & ATTR_ATIME) {
1504 arg->valid |= FATTR_ATIME;
1505 arg->atime = iattr->ia_atime.tv_sec;
1506 arg->atimensec = iattr->ia_atime.tv_nsec;
1507 if (!(ivalid & ATTR_ATIME_SET))
1508 arg->valid |= FATTR_ATIME_NOW;
1510 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1511 arg->valid |= FATTR_MTIME;
1512 arg->mtime = iattr->ia_mtime.tv_sec;
1513 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1514 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1515 arg->valid |= FATTR_MTIME_NOW;
1517 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1518 arg->valid |= FATTR_CTIME;
1519 arg->ctime = iattr->ia_ctime.tv_sec;
1520 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1525 * Prevent concurrent writepages on inode
1527 * This is done by adding a negative bias to the inode write counter
1528 * and waiting for all pending writes to finish.
1530 void fuse_set_nowrite(struct inode *inode)
1532 struct fuse_conn *fc = get_fuse_conn(inode);
1533 struct fuse_inode *fi = get_fuse_inode(inode);
1535 BUG_ON(!inode_is_locked(inode));
1537 spin_lock(&fc->lock);
1538 BUG_ON(fi->writectr < 0);
1539 fi->writectr += FUSE_NOWRITE;
1540 spin_unlock(&fc->lock);
1541 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1545 * Allow writepages on inode
1547 * Remove the bias from the writecounter and send any queued
1550 static void __fuse_release_nowrite(struct inode *inode)
1552 struct fuse_inode *fi = get_fuse_inode(inode);
1554 BUG_ON(fi->writectr != FUSE_NOWRITE);
1556 fuse_flush_writepages(inode);
1559 void fuse_release_nowrite(struct inode *inode)
1561 struct fuse_conn *fc = get_fuse_conn(inode);
1563 spin_lock(&fc->lock);
1564 __fuse_release_nowrite(inode);
1565 spin_unlock(&fc->lock);
1568 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1569 struct inode *inode,
1570 struct fuse_setattr_in *inarg_p,
1571 struct fuse_attr_out *outarg_p)
1573 args->in.h.opcode = FUSE_SETATTR;
1574 args->in.h.nodeid = get_node_id(inode);
1575 args->in.numargs = 1;
1576 args->in.args[0].size = sizeof(*inarg_p);
1577 args->in.args[0].value = inarg_p;
1578 args->out.numargs = 1;
1579 args->out.args[0].size = sizeof(*outarg_p);
1580 args->out.args[0].value = outarg_p;
1584 * Flush inode->i_mtime to the server
1586 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1588 struct fuse_conn *fc = get_fuse_conn(inode);
1590 struct fuse_setattr_in inarg;
1591 struct fuse_attr_out outarg;
1593 memset(&inarg, 0, sizeof(inarg));
1594 memset(&outarg, 0, sizeof(outarg));
1596 inarg.valid = FATTR_MTIME;
1597 inarg.mtime = inode->i_mtime.tv_sec;
1598 inarg.mtimensec = inode->i_mtime.tv_nsec;
1599 if (fc->minor >= 23) {
1600 inarg.valid |= FATTR_CTIME;
1601 inarg.ctime = inode->i_ctime.tv_sec;
1602 inarg.ctimensec = inode->i_ctime.tv_nsec;
1605 inarg.valid |= FATTR_FH;
1608 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1610 return fuse_simple_request(fc, &args);
1614 * Set attributes, and at the same time refresh them.
1616 * Truncation is slightly complicated, because the 'truncate' request
1617 * may fail, in which case we don't want to touch the mapping.
1618 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1619 * and the actual truncation by hand.
1621 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1624 struct inode *inode = d_inode(dentry);
1625 struct fuse_conn *fc = get_fuse_conn(inode);
1626 struct fuse_inode *fi = get_fuse_inode(inode);
1628 struct fuse_setattr_in inarg;
1629 struct fuse_attr_out outarg;
1630 bool is_truncate = false;
1631 bool is_wb = fc->writeback_cache;
1634 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1636 if (!fc->default_permissions)
1637 attr->ia_valid |= ATTR_FORCE;
1639 err = setattr_prepare(dentry, attr);
1643 if (attr->ia_valid & ATTR_OPEN) {
1644 /* This is coming from open(..., ... | O_TRUNC); */
1645 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1646 WARN_ON(attr->ia_size != 0);
1647 if (fc->atomic_o_trunc) {
1649 * No need to send request to userspace, since actual
1650 * truncation has already been done by OPEN. But still
1651 * need to truncate page cache.
1653 i_size_write(inode, 0);
1654 truncate_pagecache(inode, 0);
1660 if (attr->ia_valid & ATTR_SIZE)
1663 /* Flush dirty data/metadata before non-truncate SETATTR */
1664 if (is_wb && S_ISREG(inode->i_mode) &&
1666 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1668 err = write_inode_now(inode, true);
1672 fuse_set_nowrite(inode);
1673 fuse_release_nowrite(inode);
1677 fuse_set_nowrite(inode);
1678 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1679 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1680 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1683 memset(&inarg, 0, sizeof(inarg));
1684 memset(&outarg, 0, sizeof(outarg));
1685 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1687 struct fuse_file *ff = file->private_data;
1688 inarg.valid |= FATTR_FH;
1691 if (attr->ia_valid & ATTR_SIZE) {
1692 /* For mandatory locking in truncate */
1693 inarg.valid |= FATTR_LOCKOWNER;
1694 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1696 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1697 err = fuse_simple_request(fc, &args);
1700 fuse_invalidate_attr(inode);
1704 if (fuse_invalid_attr(&outarg.attr) ||
1705 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1706 make_bad_inode(inode);
1711 spin_lock(&fc->lock);
1712 /* the kernel maintains i_mtime locally */
1713 if (trust_local_cmtime) {
1714 if (attr->ia_valid & ATTR_MTIME)
1715 inode->i_mtime = attr->ia_mtime;
1716 if (attr->ia_valid & ATTR_CTIME)
1717 inode->i_ctime = attr->ia_ctime;
1718 /* FIXME: clear I_DIRTY_SYNC? */
1721 fuse_change_attributes_common(inode, &outarg.attr,
1722 attr_timeout(&outarg));
1723 oldsize = inode->i_size;
1724 /* see the comment in fuse_change_attributes() */
1725 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1726 i_size_write(inode, outarg.attr.size);
1729 /* NOTE: this may release/reacquire fc->lock */
1730 __fuse_release_nowrite(inode);
1732 spin_unlock(&fc->lock);
1735 * Only call invalidate_inode_pages2() after removing
1736 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1738 if ((is_truncate || !is_wb) &&
1739 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1740 truncate_pagecache(inode, outarg.attr.size);
1741 invalidate_inode_pages2(inode->i_mapping);
1744 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1749 fuse_release_nowrite(inode);
1751 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1755 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1757 struct inode *inode = d_inode(entry);
1758 struct fuse_conn *fc = get_fuse_conn(inode);
1759 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1762 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1765 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1766 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1770 * The only sane way to reliably kill suid/sgid is to do it in
1771 * the userspace filesystem
1773 * This should be done on write(), truncate() and chown().
1775 if (!fc->handle_killpriv) {
1777 * ia_mode calculation may have used stale i_mode.
1778 * Refresh and recalculate.
1780 ret = fuse_do_getattr(inode, NULL, file);
1784 attr->ia_mode = inode->i_mode;
1785 if (inode->i_mode & S_ISUID) {
1786 attr->ia_valid |= ATTR_MODE;
1787 attr->ia_mode &= ~S_ISUID;
1789 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1790 attr->ia_valid |= ATTR_MODE;
1791 attr->ia_mode &= ~S_ISGID;
1795 if (!attr->ia_valid)
1798 ret = fuse_do_setattr(entry, attr, file);
1801 * If filesystem supports acls it may have updated acl xattrs in
1802 * the filesystem, so forget cached acls for the inode.
1805 forget_all_cached_acls(inode);
1807 /* Directory mode changed, may need to revalidate access */
1808 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1809 fuse_invalidate_entry_cache(entry);
1814 static int fuse_getattr(const struct path *path, struct kstat *stat,
1815 u32 request_mask, unsigned int flags)
1817 struct inode *inode = d_inode(path->dentry);
1818 struct fuse_conn *fc = get_fuse_conn(inode);
1820 if (!fuse_allow_current_process(fc))
1823 return fuse_update_get_attr(inode, NULL, stat);
1826 static const struct inode_operations fuse_dir_inode_operations = {
1827 .lookup = fuse_lookup,
1828 .mkdir = fuse_mkdir,
1829 .symlink = fuse_symlink,
1830 .unlink = fuse_unlink,
1831 .rmdir = fuse_rmdir,
1832 .rename = fuse_rename2,
1834 .setattr = fuse_setattr,
1835 .create = fuse_create,
1836 .atomic_open = fuse_atomic_open,
1837 .mknod = fuse_mknod,
1838 .permission = fuse_permission,
1839 .getattr = fuse_getattr,
1840 .listxattr = fuse_listxattr,
1841 .get_acl = fuse_get_acl,
1842 .set_acl = fuse_set_acl,
1845 static const struct file_operations fuse_dir_operations = {
1846 .llseek = generic_file_llseek,
1847 .read = generic_read_dir,
1848 .iterate_shared = fuse_readdir,
1849 .open = fuse_dir_open,
1850 .release = fuse_dir_release,
1851 .fsync = fuse_dir_fsync,
1852 .unlocked_ioctl = fuse_dir_ioctl,
1853 .compat_ioctl = fuse_dir_compat_ioctl,
1856 static const struct inode_operations fuse_common_inode_operations = {
1857 .setattr = fuse_setattr,
1858 .permission = fuse_permission,
1859 .getattr = fuse_getattr,
1860 .listxattr = fuse_listxattr,
1861 .get_acl = fuse_get_acl,
1862 .set_acl = fuse_set_acl,
1865 static const struct inode_operations fuse_symlink_inode_operations = {
1866 .setattr = fuse_setattr,
1867 .get_link = fuse_get_link,
1868 .getattr = fuse_getattr,
1869 .listxattr = fuse_listxattr,
1872 void fuse_init_common(struct inode *inode)
1874 inode->i_op = &fuse_common_inode_operations;
1877 void fuse_init_dir(struct inode *inode)
1879 inode->i_op = &fuse_dir_inode_operations;
1880 inode->i_fop = &fuse_dir_operations;
1883 void fuse_init_symlink(struct inode *inode)
1885 inode->i_op = &fuse_symlink_inode_operations;