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 && fuse_is_bad(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 if (fuse_is_bad(dir))
368 return ERR_PTR(-EIO);
370 locked = fuse_lock_inode(dir);
371 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
373 fuse_unlock_inode(dir, locked);
374 if (err == -ENOENT) {
375 outarg_valid = false;
382 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
385 newent = d_splice_alias(inode, entry);
386 err = PTR_ERR(newent);
390 entry = newent ? newent : entry;
392 fuse_change_entry_timeout(entry, &outarg);
394 fuse_invalidate_entry_cache(entry);
396 fuse_advise_use_readdirplus(dir);
406 * Atomic create+open operation
408 * If the filesystem doesn't support this, then fall back to separate
409 * 'mknod' + 'open' requests.
411 static int fuse_create_open(struct inode *dir, struct dentry *entry,
412 struct file *file, unsigned flags,
417 struct fuse_conn *fc = get_fuse_conn(dir);
419 struct fuse_forget_link *forget;
420 struct fuse_create_in inarg;
421 struct fuse_open_out outopen;
422 struct fuse_entry_out outentry;
423 struct fuse_file *ff;
425 /* Userspace expects S_IFREG in create mode */
426 BUG_ON((mode & S_IFMT) != S_IFREG);
428 forget = fuse_alloc_forget();
434 ff = fuse_file_alloc(fc);
436 goto out_put_forget_req;
439 mode &= ~current_umask();
442 memset(&inarg, 0, sizeof(inarg));
443 memset(&outentry, 0, sizeof(outentry));
446 inarg.umask = current_umask();
447 args.in.h.opcode = FUSE_CREATE;
448 args.in.h.nodeid = get_node_id(dir);
450 args.in.args[0].size = sizeof(inarg);
451 args.in.args[0].value = &inarg;
452 args.in.args[1].size = entry->d_name.len + 1;
453 args.in.args[1].value = entry->d_name.name;
454 args.out.numargs = 2;
455 args.out.args[0].size = sizeof(outentry);
456 args.out.args[0].value = &outentry;
457 args.out.args[1].size = sizeof(outopen);
458 args.out.args[1].value = &outopen;
459 err = fuse_simple_request(fc, &args);
464 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
465 fuse_invalid_attr(&outentry.attr))
469 ff->nodeid = outentry.nodeid;
470 ff->open_flags = outopen.open_flags;
471 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
472 &outentry.attr, entry_attr_timeout(&outentry), 0);
474 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
475 fuse_sync_release(ff, flags);
476 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
481 d_instantiate(entry, inode);
482 fuse_change_entry_timeout(entry, &outentry);
483 fuse_invalidate_attr(dir);
484 err = finish_open(file, entry, generic_file_open);
486 fuse_sync_release(ff, flags);
488 file->private_data = ff;
489 fuse_finish_open(inode, file);
501 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
502 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
503 struct file *file, unsigned flags,
507 struct fuse_conn *fc = get_fuse_conn(dir);
508 struct dentry *res = NULL;
510 if (fuse_is_bad(dir))
513 if (d_in_lookup(entry)) {
514 res = fuse_lookup(dir, entry, 0);
522 if (!(flags & O_CREAT) || d_really_is_positive(entry))
526 file->f_mode |= FMODE_CREATED;
531 err = fuse_create_open(dir, entry, file, flags, mode);
532 if (err == -ENOSYS) {
541 err = fuse_mknod(dir, entry, mode, 0);
545 return finish_no_open(file, res);
549 * Code shared between mknod, mkdir, symlink and link
551 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
552 struct inode *dir, struct dentry *entry,
555 struct fuse_entry_out outarg;
559 struct fuse_forget_link *forget;
561 if (fuse_is_bad(dir))
564 forget = fuse_alloc_forget();
568 memset(&outarg, 0, sizeof(outarg));
569 args->in.h.nodeid = get_node_id(dir);
570 args->out.numargs = 1;
571 args->out.args[0].size = sizeof(outarg);
572 args->out.args[0].value = &outarg;
573 err = fuse_simple_request(fc, args);
575 goto out_put_forget_req;
578 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
579 goto out_put_forget_req;
581 if ((outarg.attr.mode ^ mode) & S_IFMT)
582 goto out_put_forget_req;
584 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
585 &outarg.attr, entry_attr_timeout(&outarg), 0);
587 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
593 d = d_splice_alias(inode, entry);
598 fuse_change_entry_timeout(d, &outarg);
601 fuse_change_entry_timeout(entry, &outarg);
603 fuse_invalidate_attr(dir);
611 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
614 struct fuse_mknod_in inarg;
615 struct fuse_conn *fc = get_fuse_conn(dir);
619 mode &= ~current_umask();
621 memset(&inarg, 0, sizeof(inarg));
623 inarg.rdev = new_encode_dev(rdev);
624 inarg.umask = current_umask();
625 args.in.h.opcode = FUSE_MKNOD;
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, mode);
634 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
637 return fuse_mknod(dir, entry, mode, 0);
640 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
642 struct fuse_mkdir_in inarg;
643 struct fuse_conn *fc = get_fuse_conn(dir);
647 mode &= ~current_umask();
649 memset(&inarg, 0, sizeof(inarg));
651 inarg.umask = current_umask();
652 args.in.h.opcode = FUSE_MKDIR;
654 args.in.args[0].size = sizeof(inarg);
655 args.in.args[0].value = &inarg;
656 args.in.args[1].size = entry->d_name.len + 1;
657 args.in.args[1].value = entry->d_name.name;
658 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
661 static int fuse_symlink(struct inode *dir, struct dentry *entry,
664 struct fuse_conn *fc = get_fuse_conn(dir);
665 unsigned len = strlen(link) + 1;
668 args.in.h.opcode = FUSE_SYMLINK;
670 args.in.args[0].size = entry->d_name.len + 1;
671 args.in.args[0].value = entry->d_name.name;
672 args.in.args[1].size = len;
673 args.in.args[1].value = link;
674 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
677 void fuse_update_ctime(struct inode *inode)
679 if (!IS_NOCMTIME(inode)) {
680 inode->i_ctime = current_time(inode);
681 mark_inode_dirty_sync(inode);
685 static int fuse_unlink(struct inode *dir, struct dentry *entry)
688 struct fuse_conn *fc = get_fuse_conn(dir);
691 if (fuse_is_bad(dir))
694 args.in.h.opcode = FUSE_UNLINK;
695 args.in.h.nodeid = get_node_id(dir);
697 args.in.args[0].size = entry->d_name.len + 1;
698 args.in.args[0].value = entry->d_name.name;
699 err = fuse_simple_request(fc, &args);
701 struct inode *inode = d_inode(entry);
702 struct fuse_inode *fi = get_fuse_inode(inode);
704 spin_lock(&fc->lock);
705 fi->attr_version = ++fc->attr_version;
707 * If i_nlink == 0 then unlink doesn't make sense, yet this can
708 * happen if userspace filesystem is careless. It would be
709 * difficult to enforce correct nlink usage so just ignore this
712 if (inode->i_nlink > 0)
714 spin_unlock(&fc->lock);
715 fuse_invalidate_attr(inode);
716 fuse_invalidate_attr(dir);
717 fuse_invalidate_entry_cache(entry);
718 fuse_update_ctime(inode);
719 } else if (err == -EINTR)
720 fuse_invalidate_entry(entry);
724 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
727 struct fuse_conn *fc = get_fuse_conn(dir);
730 if (fuse_is_bad(dir))
733 args.in.h.opcode = FUSE_RMDIR;
734 args.in.h.nodeid = get_node_id(dir);
736 args.in.args[0].size = entry->d_name.len + 1;
737 args.in.args[0].value = entry->d_name.name;
738 err = fuse_simple_request(fc, &args);
740 clear_nlink(d_inode(entry));
741 fuse_invalidate_attr(dir);
742 fuse_invalidate_entry_cache(entry);
743 } else if (err == -EINTR)
744 fuse_invalidate_entry(entry);
748 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
749 struct inode *newdir, struct dentry *newent,
750 unsigned int flags, int opcode, size_t argsize)
753 struct fuse_rename2_in inarg;
754 struct fuse_conn *fc = get_fuse_conn(olddir);
757 memset(&inarg, 0, argsize);
758 inarg.newdir = get_node_id(newdir);
760 args.in.h.opcode = opcode;
761 args.in.h.nodeid = get_node_id(olddir);
763 args.in.args[0].size = argsize;
764 args.in.args[0].value = &inarg;
765 args.in.args[1].size = oldent->d_name.len + 1;
766 args.in.args[1].value = oldent->d_name.name;
767 args.in.args[2].size = newent->d_name.len + 1;
768 args.in.args[2].value = newent->d_name.name;
769 err = fuse_simple_request(fc, &args);
772 fuse_invalidate_attr(d_inode(oldent));
773 fuse_update_ctime(d_inode(oldent));
775 if (flags & RENAME_EXCHANGE) {
776 fuse_invalidate_attr(d_inode(newent));
777 fuse_update_ctime(d_inode(newent));
780 fuse_invalidate_attr(olddir);
781 if (olddir != newdir)
782 fuse_invalidate_attr(newdir);
784 /* newent will end up negative */
785 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
786 fuse_invalidate_attr(d_inode(newent));
787 fuse_invalidate_entry_cache(newent);
788 fuse_update_ctime(d_inode(newent));
790 } else if (err == -EINTR) {
791 /* If request was interrupted, DEITY only knows if the
792 rename actually took place. If the invalidation
793 fails (e.g. some process has CWD under the renamed
794 directory), then there can be inconsistency between
795 the dcache and the real filesystem. Tough luck. */
796 fuse_invalidate_entry(oldent);
797 if (d_really_is_positive(newent))
798 fuse_invalidate_entry(newent);
804 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
805 struct inode *newdir, struct dentry *newent,
808 struct fuse_conn *fc = get_fuse_conn(olddir);
811 if (fuse_is_bad(olddir))
814 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
818 if (fc->no_rename2 || fc->minor < 23)
821 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
823 sizeof(struct fuse_rename2_in));
824 if (err == -ENOSYS) {
829 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
831 sizeof(struct fuse_rename_in));
837 static int fuse_link(struct dentry *entry, struct inode *newdir,
838 struct dentry *newent)
841 struct fuse_link_in inarg;
842 struct inode *inode = d_inode(entry);
843 struct fuse_conn *fc = get_fuse_conn(inode);
846 memset(&inarg, 0, sizeof(inarg));
847 inarg.oldnodeid = get_node_id(inode);
848 args.in.h.opcode = FUSE_LINK;
850 args.in.args[0].size = sizeof(inarg);
851 args.in.args[0].value = &inarg;
852 args.in.args[1].size = newent->d_name.len + 1;
853 args.in.args[1].value = newent->d_name.name;
854 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
855 /* Contrary to "normal" filesystems it can happen that link
856 makes two "logical" inodes point to the same "physical"
857 inode. We invalidate the attributes of the old one, so it
858 will reflect changes in the backing inode (link count,
862 struct fuse_inode *fi = get_fuse_inode(inode);
864 spin_lock(&fc->lock);
865 fi->attr_version = ++fc->attr_version;
866 if (likely(inode->i_nlink < UINT_MAX))
868 spin_unlock(&fc->lock);
869 fuse_invalidate_attr(inode);
870 fuse_update_ctime(inode);
871 } else if (err == -EINTR) {
872 fuse_invalidate_attr(inode);
877 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
880 unsigned int blkbits;
881 struct fuse_conn *fc = get_fuse_conn(inode);
883 /* see the comment in fuse_change_attributes() */
884 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
885 attr->size = i_size_read(inode);
886 attr->mtime = inode->i_mtime.tv_sec;
887 attr->mtimensec = inode->i_mtime.tv_nsec;
888 attr->ctime = inode->i_ctime.tv_sec;
889 attr->ctimensec = inode->i_ctime.tv_nsec;
892 stat->dev = inode->i_sb->s_dev;
893 stat->ino = attr->ino;
894 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
895 stat->nlink = attr->nlink;
896 stat->uid = make_kuid(fc->user_ns, attr->uid);
897 stat->gid = make_kgid(fc->user_ns, attr->gid);
898 stat->rdev = inode->i_rdev;
899 stat->atime.tv_sec = attr->atime;
900 stat->atime.tv_nsec = attr->atimensec;
901 stat->mtime.tv_sec = attr->mtime;
902 stat->mtime.tv_nsec = attr->mtimensec;
903 stat->ctime.tv_sec = attr->ctime;
904 stat->ctime.tv_nsec = attr->ctimensec;
905 stat->size = attr->size;
906 stat->blocks = attr->blocks;
908 if (attr->blksize != 0)
909 blkbits = ilog2(attr->blksize);
911 blkbits = inode->i_sb->s_blocksize_bits;
913 stat->blksize = 1 << blkbits;
916 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
920 struct fuse_getattr_in inarg;
921 struct fuse_attr_out outarg;
922 struct fuse_conn *fc = get_fuse_conn(inode);
926 attr_version = fuse_get_attr_version(fc);
928 memset(&inarg, 0, sizeof(inarg));
929 memset(&outarg, 0, sizeof(outarg));
930 /* Directories have separate file-handle space */
931 if (file && S_ISREG(inode->i_mode)) {
932 struct fuse_file *ff = file->private_data;
934 inarg.getattr_flags |= FUSE_GETATTR_FH;
937 args.in.h.opcode = FUSE_GETATTR;
938 args.in.h.nodeid = get_node_id(inode);
940 args.in.args[0].size = sizeof(inarg);
941 args.in.args[0].value = &inarg;
942 args.out.numargs = 1;
943 args.out.args[0].size = sizeof(outarg);
944 args.out.args[0].value = &outarg;
945 err = fuse_simple_request(fc, &args);
947 if (fuse_invalid_attr(&outarg.attr) ||
948 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
949 fuse_make_bad(inode);
952 fuse_change_attributes(inode, &outarg.attr,
953 attr_timeout(&outarg),
956 fuse_fillattr(inode, &outarg.attr, stat);
962 static int fuse_update_get_attr(struct inode *inode, struct file *file,
963 struct kstat *stat, unsigned int flags)
965 struct fuse_inode *fi = get_fuse_inode(inode);
969 if (flags & AT_STATX_FORCE_SYNC)
971 else if (flags & AT_STATX_DONT_SYNC)
974 sync = time_before64(fi->i_time, get_jiffies_64());
977 forget_all_cached_acls(inode);
978 err = fuse_do_getattr(inode, stat, file);
980 generic_fillattr(inode, stat);
981 stat->mode = fi->orig_i_mode;
982 stat->ino = fi->orig_ino;
988 int fuse_update_attributes(struct inode *inode, struct file *file)
990 return fuse_update_get_attr(inode, file, NULL, 0);
993 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
994 u64 child_nodeid, struct qstr *name)
997 struct inode *parent;
999 struct dentry *entry;
1001 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1005 inode_lock_nested(parent, I_MUTEX_PARENT);
1006 if (!S_ISDIR(parent->i_mode))
1010 dir = d_find_alias(parent);
1014 name->hash = full_name_hash(dir, name->name, name->len);
1015 entry = d_lookup(dir, name);
1020 fuse_invalidate_attr(parent);
1021 fuse_invalidate_entry(entry);
1023 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1024 inode_lock(d_inode(entry));
1025 if (get_node_id(d_inode(entry)) != child_nodeid) {
1029 if (d_mountpoint(entry)) {
1033 if (d_is_dir(entry)) {
1034 shrink_dcache_parent(entry);
1035 if (!simple_empty(entry)) {
1039 d_inode(entry)->i_flags |= S_DEAD;
1042 clear_nlink(d_inode(entry));
1045 inode_unlock(d_inode(entry));
1054 inode_unlock(parent);
1060 * Calling into a user-controlled filesystem gives the filesystem
1061 * daemon ptrace-like capabilities over the current process. This
1062 * means, that the filesystem daemon is able to record the exact
1063 * filesystem operations performed, and can also control the behavior
1064 * of the requester process in otherwise impossible ways. For example
1065 * it can delay the operation for arbitrary length of time allowing
1066 * DoS against the requester.
1068 * For this reason only those processes can call into the filesystem,
1069 * for which the owner of the mount has ptrace privilege. This
1070 * excludes processes started by other users, suid or sgid processes.
1072 int fuse_allow_current_process(struct fuse_conn *fc)
1074 const struct cred *cred;
1076 if (fc->allow_other)
1077 return current_in_userns(fc->user_ns);
1079 cred = current_cred();
1080 if (uid_eq(cred->euid, fc->user_id) &&
1081 uid_eq(cred->suid, fc->user_id) &&
1082 uid_eq(cred->uid, fc->user_id) &&
1083 gid_eq(cred->egid, fc->group_id) &&
1084 gid_eq(cred->sgid, fc->group_id) &&
1085 gid_eq(cred->gid, fc->group_id))
1091 static int fuse_access(struct inode *inode, int mask)
1093 struct fuse_conn *fc = get_fuse_conn(inode);
1095 struct fuse_access_in inarg;
1098 BUG_ON(mask & MAY_NOT_BLOCK);
1103 memset(&inarg, 0, sizeof(inarg));
1104 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1105 args.in.h.opcode = FUSE_ACCESS;
1106 args.in.h.nodeid = get_node_id(inode);
1107 args.in.numargs = 1;
1108 args.in.args[0].size = sizeof(inarg);
1109 args.in.args[0].value = &inarg;
1110 err = fuse_simple_request(fc, &args);
1111 if (err == -ENOSYS) {
1118 static int fuse_perm_getattr(struct inode *inode, int mask)
1120 if (mask & MAY_NOT_BLOCK)
1123 forget_all_cached_acls(inode);
1124 return fuse_do_getattr(inode, NULL, NULL);
1128 * Check permission. The two basic access models of FUSE are:
1130 * 1) Local access checking ('default_permissions' mount option) based
1131 * on file mode. This is the plain old disk filesystem permission
1134 * 2) "Remote" access checking, where server is responsible for
1135 * checking permission in each inode operation. An exception to this
1136 * is if ->permission() was invoked from sys_access() in which case an
1137 * access request is sent. Execute permission is still checked
1138 * locally based on file mode.
1140 static int fuse_permission(struct inode *inode, int mask)
1142 struct fuse_conn *fc = get_fuse_conn(inode);
1143 bool refreshed = false;
1146 if (fuse_is_bad(inode))
1149 if (!fuse_allow_current_process(fc))
1153 * If attributes are needed, refresh them before proceeding
1155 if (fc->default_permissions ||
1156 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1157 struct fuse_inode *fi = get_fuse_inode(inode);
1159 if (time_before64(fi->i_time, get_jiffies_64())) {
1162 err = fuse_perm_getattr(inode, mask);
1168 if (fc->default_permissions) {
1169 err = generic_permission(inode, mask);
1171 /* If permission is denied, try to refresh file
1172 attributes. This is also needed, because the root
1173 node will at first have no permissions */
1174 if (err == -EACCES && !refreshed) {
1175 err = fuse_perm_getattr(inode, mask);
1177 err = generic_permission(inode, mask);
1180 /* Note: the opposite of the above test does not
1181 exist. So if permissions are revoked this won't be
1182 noticed immediately, only after the attribute
1183 timeout has expired */
1184 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1185 err = fuse_access(inode, mask);
1186 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1187 if (!(inode->i_mode & S_IXUGO)) {
1191 err = fuse_perm_getattr(inode, mask);
1192 if (!err && !(inode->i_mode & S_IXUGO))
1199 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1200 struct dir_context *ctx)
1202 while (nbytes >= FUSE_NAME_OFFSET) {
1203 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1204 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1205 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1207 if (reclen > nbytes)
1209 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1212 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1213 dirent->ino, dirent->type))
1218 ctx->pos = dirent->off;
1224 static int fuse_direntplus_link(struct file *file,
1225 struct fuse_direntplus *direntplus,
1228 struct fuse_entry_out *o = &direntplus->entry_out;
1229 struct fuse_dirent *dirent = &direntplus->dirent;
1230 struct dentry *parent = file->f_path.dentry;
1231 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1232 struct dentry *dentry;
1233 struct dentry *alias;
1234 struct inode *dir = d_inode(parent);
1235 struct fuse_conn *fc;
1236 struct inode *inode;
1237 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1241 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1242 * ENOENT. Instead, it only means the userspace filesystem did
1243 * not want to return attributes/handle for this entry.
1250 if (name.name[0] == '.') {
1252 * We could potentially refresh the attributes of the directory
1257 if (name.name[1] == '.' && name.len == 2)
1261 if (invalid_nodeid(o->nodeid))
1263 if (fuse_invalid_attr(&o->attr))
1266 fc = get_fuse_conn(dir);
1268 name.hash = full_name_hash(parent, name.name, name.len);
1269 dentry = d_lookup(parent, &name);
1272 dentry = d_alloc_parallel(parent, &name, &wq);
1274 return PTR_ERR(dentry);
1276 if (!d_in_lookup(dentry)) {
1277 struct fuse_inode *fi;
1278 inode = d_inode(dentry);
1280 get_node_id(inode) != o->nodeid ||
1281 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1282 d_invalidate(dentry);
1286 if (fuse_is_bad(inode)) {
1291 fi = get_fuse_inode(inode);
1292 spin_lock(&fc->lock);
1294 spin_unlock(&fc->lock);
1296 forget_all_cached_acls(inode);
1297 fuse_change_attributes(inode, &o->attr,
1298 entry_attr_timeout(o),
1301 * The other branch comes via fuse_iget()
1302 * which bumps nlookup inside
1305 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1306 &o->attr, entry_attr_timeout(o),
1309 inode = ERR_PTR(-ENOMEM);
1311 alias = d_splice_alias(inode, dentry);
1312 d_lookup_done(dentry);
1318 return PTR_ERR(dentry);
1320 if (fc->readdirplus_auto)
1321 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1322 fuse_change_entry_timeout(dentry, o);
1328 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1329 struct dir_context *ctx, u64 attr_version)
1331 struct fuse_direntplus *direntplus;
1332 struct fuse_dirent *dirent;
1337 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1338 direntplus = (struct fuse_direntplus *) buf;
1339 dirent = &direntplus->dirent;
1340 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1342 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1344 if (reclen > nbytes)
1346 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1350 /* We fill entries into dstbuf only as much as
1351 it can hold. But we still continue iterating
1352 over remaining entries to link them. If not,
1353 we need to send a FORGET for each of those
1354 which we did not link.
1356 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1357 dirent->ino, dirent->type);
1359 ctx->pos = dirent->off;
1365 ret = fuse_direntplus_link(file, direntplus, attr_version);
1367 fuse_force_forget(file, direntplus->entry_out.nodeid);
1373 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1378 struct inode *inode = file_inode(file);
1379 struct fuse_conn *fc = get_fuse_conn(inode);
1380 struct fuse_req *req;
1381 u64 attr_version = 0;
1384 if (fuse_is_bad(inode))
1387 req = fuse_get_req(fc, 1);
1389 return PTR_ERR(req);
1391 page = alloc_page(GFP_KERNEL);
1393 fuse_put_request(fc, req);
1397 plus = fuse_use_readdirplus(inode, ctx);
1398 req->out.argpages = 1;
1400 req->pages[0] = page;
1401 req->page_descs[0].length = PAGE_SIZE;
1403 attr_version = fuse_get_attr_version(fc);
1404 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1407 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1410 locked = fuse_lock_inode(inode);
1411 fuse_request_send(fc, req);
1412 fuse_unlock_inode(inode, locked);
1413 nbytes = req->out.args[0].size;
1414 err = req->out.h.error;
1415 fuse_put_request(fc, req);
1418 err = parse_dirplusfile(page_address(page), nbytes,
1422 err = parse_dirfile(page_address(page), nbytes, file,
1428 fuse_invalidate_atime(inode);
1432 static const char *fuse_get_link(struct dentry *dentry,
1433 struct inode *inode,
1434 struct delayed_call *done)
1436 struct fuse_conn *fc = get_fuse_conn(inode);
1442 return ERR_PTR(-ECHILD);
1444 if (fuse_is_bad(inode))
1445 return ERR_PTR(-EIO);
1447 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
1449 return ERR_PTR(-ENOMEM);
1451 args.in.h.opcode = FUSE_READLINK;
1452 args.in.h.nodeid = get_node_id(inode);
1453 args.out.argvar = 1;
1454 args.out.numargs = 1;
1455 args.out.args[0].size = PAGE_SIZE - 1;
1456 args.out.args[0].value = link;
1457 ret = fuse_simple_request(fc, &args);
1460 link = ERR_PTR(ret);
1463 set_delayed_call(done, kfree_link, link);
1465 fuse_invalidate_atime(inode);
1469 static int fuse_dir_open(struct inode *inode, struct file *file)
1471 return fuse_open_common(inode, file, true);
1474 static int fuse_dir_release(struct inode *inode, struct file *file)
1476 fuse_release_common(file, true);
1481 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1484 return fuse_fsync_common(file, start, end, datasync, 1);
1487 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1490 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1492 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1496 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1499 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1502 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1507 return fuse_ioctl_common(file, cmd, arg,
1508 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1511 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1513 /* Always update if mtime is explicitly set */
1514 if (ivalid & ATTR_MTIME_SET)
1517 /* Or if kernel i_mtime is the official one */
1518 if (trust_local_mtime)
1521 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1522 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1525 /* In all other cases update */
1529 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1530 struct fuse_setattr_in *arg, bool trust_local_cmtime)
1532 unsigned ivalid = iattr->ia_valid;
1534 if (ivalid & ATTR_MODE)
1535 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1536 if (ivalid & ATTR_UID)
1537 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1538 if (ivalid & ATTR_GID)
1539 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1540 if (ivalid & ATTR_SIZE)
1541 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1542 if (ivalid & ATTR_ATIME) {
1543 arg->valid |= FATTR_ATIME;
1544 arg->atime = iattr->ia_atime.tv_sec;
1545 arg->atimensec = iattr->ia_atime.tv_nsec;
1546 if (!(ivalid & ATTR_ATIME_SET))
1547 arg->valid |= FATTR_ATIME_NOW;
1549 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1550 arg->valid |= FATTR_MTIME;
1551 arg->mtime = iattr->ia_mtime.tv_sec;
1552 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1553 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1554 arg->valid |= FATTR_MTIME_NOW;
1556 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1557 arg->valid |= FATTR_CTIME;
1558 arg->ctime = iattr->ia_ctime.tv_sec;
1559 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1564 * Prevent concurrent writepages on inode
1566 * This is done by adding a negative bias to the inode write counter
1567 * and waiting for all pending writes to finish.
1569 void fuse_set_nowrite(struct inode *inode)
1571 struct fuse_conn *fc = get_fuse_conn(inode);
1572 struct fuse_inode *fi = get_fuse_inode(inode);
1574 BUG_ON(!inode_is_locked(inode));
1576 spin_lock(&fc->lock);
1577 BUG_ON(fi->writectr < 0);
1578 fi->writectr += FUSE_NOWRITE;
1579 spin_unlock(&fc->lock);
1580 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1584 * Allow writepages on inode
1586 * Remove the bias from the writecounter and send any queued
1589 static void __fuse_release_nowrite(struct inode *inode)
1591 struct fuse_inode *fi = get_fuse_inode(inode);
1593 BUG_ON(fi->writectr != FUSE_NOWRITE);
1595 fuse_flush_writepages(inode);
1598 void fuse_release_nowrite(struct inode *inode)
1600 struct fuse_conn *fc = get_fuse_conn(inode);
1602 spin_lock(&fc->lock);
1603 __fuse_release_nowrite(inode);
1604 spin_unlock(&fc->lock);
1607 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1608 struct inode *inode,
1609 struct fuse_setattr_in *inarg_p,
1610 struct fuse_attr_out *outarg_p)
1612 args->in.h.opcode = FUSE_SETATTR;
1613 args->in.h.nodeid = get_node_id(inode);
1614 args->in.numargs = 1;
1615 args->in.args[0].size = sizeof(*inarg_p);
1616 args->in.args[0].value = inarg_p;
1617 args->out.numargs = 1;
1618 args->out.args[0].size = sizeof(*outarg_p);
1619 args->out.args[0].value = outarg_p;
1623 * Flush inode->i_mtime to the server
1625 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1627 struct fuse_conn *fc = get_fuse_conn(inode);
1629 struct fuse_setattr_in inarg;
1630 struct fuse_attr_out outarg;
1632 memset(&inarg, 0, sizeof(inarg));
1633 memset(&outarg, 0, sizeof(outarg));
1635 inarg.valid = FATTR_MTIME;
1636 inarg.mtime = inode->i_mtime.tv_sec;
1637 inarg.mtimensec = inode->i_mtime.tv_nsec;
1638 if (fc->minor >= 23) {
1639 inarg.valid |= FATTR_CTIME;
1640 inarg.ctime = inode->i_ctime.tv_sec;
1641 inarg.ctimensec = inode->i_ctime.tv_nsec;
1644 inarg.valid |= FATTR_FH;
1647 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1649 return fuse_simple_request(fc, &args);
1653 * Set attributes, and at the same time refresh them.
1655 * Truncation is slightly complicated, because the 'truncate' request
1656 * may fail, in which case we don't want to touch the mapping.
1657 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1658 * and the actual truncation by hand.
1660 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1663 struct inode *inode = d_inode(dentry);
1664 struct fuse_conn *fc = get_fuse_conn(inode);
1665 struct fuse_inode *fi = get_fuse_inode(inode);
1667 struct fuse_setattr_in inarg;
1668 struct fuse_attr_out outarg;
1669 bool is_truncate = false;
1670 bool is_wb = fc->writeback_cache;
1673 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1675 if (!fc->default_permissions)
1676 attr->ia_valid |= ATTR_FORCE;
1678 err = setattr_prepare(dentry, attr);
1682 if (attr->ia_valid & ATTR_OPEN) {
1683 /* This is coming from open(..., ... | O_TRUNC); */
1684 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1685 WARN_ON(attr->ia_size != 0);
1686 if (fc->atomic_o_trunc) {
1688 * No need to send request to userspace, since actual
1689 * truncation has already been done by OPEN. But still
1690 * need to truncate page cache.
1692 i_size_write(inode, 0);
1693 truncate_pagecache(inode, 0);
1699 if (attr->ia_valid & ATTR_SIZE)
1702 /* Flush dirty data/metadata before non-truncate SETATTR */
1703 if (is_wb && S_ISREG(inode->i_mode) &&
1705 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1707 err = write_inode_now(inode, true);
1711 fuse_set_nowrite(inode);
1712 fuse_release_nowrite(inode);
1716 fuse_set_nowrite(inode);
1717 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1718 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1719 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1722 memset(&inarg, 0, sizeof(inarg));
1723 memset(&outarg, 0, sizeof(outarg));
1724 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1726 struct fuse_file *ff = file->private_data;
1727 inarg.valid |= FATTR_FH;
1730 if (attr->ia_valid & ATTR_SIZE) {
1731 /* For mandatory locking in truncate */
1732 inarg.valid |= FATTR_LOCKOWNER;
1733 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1735 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1736 err = fuse_simple_request(fc, &args);
1739 fuse_invalidate_attr(inode);
1743 if (fuse_invalid_attr(&outarg.attr) ||
1744 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1745 fuse_make_bad(inode);
1750 spin_lock(&fc->lock);
1751 /* the kernel maintains i_mtime locally */
1752 if (trust_local_cmtime) {
1753 if (attr->ia_valid & ATTR_MTIME)
1754 inode->i_mtime = attr->ia_mtime;
1755 if (attr->ia_valid & ATTR_CTIME)
1756 inode->i_ctime = attr->ia_ctime;
1757 /* FIXME: clear I_DIRTY_SYNC? */
1760 fuse_change_attributes_common(inode, &outarg.attr,
1761 attr_timeout(&outarg));
1762 oldsize = inode->i_size;
1763 /* see the comment in fuse_change_attributes() */
1764 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1765 i_size_write(inode, outarg.attr.size);
1768 /* NOTE: this may release/reacquire fc->lock */
1769 __fuse_release_nowrite(inode);
1771 spin_unlock(&fc->lock);
1774 * Only call invalidate_inode_pages2() after removing
1775 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1777 if ((is_truncate || !is_wb) &&
1778 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1779 truncate_pagecache(inode, outarg.attr.size);
1780 invalidate_inode_pages2(inode->i_mapping);
1783 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1788 fuse_release_nowrite(inode);
1790 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1794 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1796 struct inode *inode = d_inode(entry);
1797 struct fuse_conn *fc = get_fuse_conn(inode);
1798 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1801 if (fuse_is_bad(inode))
1804 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1807 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1808 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1812 * The only sane way to reliably kill suid/sgid is to do it in
1813 * the userspace filesystem
1815 * This should be done on write(), truncate() and chown().
1817 if (!fc->handle_killpriv) {
1819 * ia_mode calculation may have used stale i_mode.
1820 * Refresh and recalculate.
1822 ret = fuse_do_getattr(inode, NULL, file);
1826 attr->ia_mode = inode->i_mode;
1827 if (inode->i_mode & S_ISUID) {
1828 attr->ia_valid |= ATTR_MODE;
1829 attr->ia_mode &= ~S_ISUID;
1831 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1832 attr->ia_valid |= ATTR_MODE;
1833 attr->ia_mode &= ~S_ISGID;
1837 if (!attr->ia_valid)
1840 ret = fuse_do_setattr(entry, attr, file);
1843 * If filesystem supports acls it may have updated acl xattrs in
1844 * the filesystem, so forget cached acls for the inode.
1847 forget_all_cached_acls(inode);
1849 /* Directory mode changed, may need to revalidate access */
1850 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1851 fuse_invalidate_entry_cache(entry);
1856 static int fuse_getattr(const struct path *path, struct kstat *stat,
1857 u32 request_mask, unsigned int flags)
1859 struct inode *inode = d_inode(path->dentry);
1860 struct fuse_conn *fc = get_fuse_conn(inode);
1862 if (fuse_is_bad(inode))
1865 if (!fuse_allow_current_process(fc))
1868 return fuse_update_get_attr(inode, NULL, stat, flags);
1871 static const struct inode_operations fuse_dir_inode_operations = {
1872 .lookup = fuse_lookup,
1873 .mkdir = fuse_mkdir,
1874 .symlink = fuse_symlink,
1875 .unlink = fuse_unlink,
1876 .rmdir = fuse_rmdir,
1877 .rename = fuse_rename2,
1879 .setattr = fuse_setattr,
1880 .create = fuse_create,
1881 .atomic_open = fuse_atomic_open,
1882 .mknod = fuse_mknod,
1883 .permission = fuse_permission,
1884 .getattr = fuse_getattr,
1885 .listxattr = fuse_listxattr,
1886 .get_acl = fuse_get_acl,
1887 .set_acl = fuse_set_acl,
1890 static const struct file_operations fuse_dir_operations = {
1891 .llseek = generic_file_llseek,
1892 .read = generic_read_dir,
1893 .iterate_shared = fuse_readdir,
1894 .open = fuse_dir_open,
1895 .release = fuse_dir_release,
1896 .fsync = fuse_dir_fsync,
1897 .unlocked_ioctl = fuse_dir_ioctl,
1898 .compat_ioctl = fuse_dir_compat_ioctl,
1901 static const struct inode_operations fuse_common_inode_operations = {
1902 .setattr = fuse_setattr,
1903 .permission = fuse_permission,
1904 .getattr = fuse_getattr,
1905 .listxattr = fuse_listxattr,
1906 .get_acl = fuse_get_acl,
1907 .set_acl = fuse_set_acl,
1910 static const struct inode_operations fuse_symlink_inode_operations = {
1911 .setattr = fuse_setattr,
1912 .get_link = fuse_get_link,
1913 .getattr = fuse_getattr,
1914 .listxattr = fuse_listxattr,
1917 void fuse_init_common(struct inode *inode)
1919 inode->i_op = &fuse_common_inode_operations;
1922 void fuse_init_dir(struct inode *inode)
1924 inode->i_op = &fuse_dir_inode_operations;
1925 inode->i_fop = &fuse_dir_operations;
1928 void fuse_init_symlink(struct inode *inode)
1930 inode->i_op = &fuse_symlink_inode_operations;