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,
413 umode_t mode, int *opened)
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, opened);
486 fuse_sync_release(ff, flags);
488 file->private_data = fuse_file_get(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,
504 umode_t mode, int *opened)
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 *opened |= FILE_CREATED;
531 err = fuse_create_open(dir, entry, file, flags, mode, opened);
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;
558 struct fuse_forget_link *forget;
560 if (fuse_is_bad(dir))
563 forget = fuse_alloc_forget();
567 memset(&outarg, 0, sizeof(outarg));
568 args->in.h.nodeid = get_node_id(dir);
569 args->out.numargs = 1;
570 args->out.args[0].size = sizeof(outarg);
571 args->out.args[0].value = &outarg;
572 err = fuse_simple_request(fc, args);
574 goto out_put_forget_req;
577 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
578 goto out_put_forget_req;
580 if ((outarg.attr.mode ^ mode) & S_IFMT)
581 goto out_put_forget_req;
583 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
584 &outarg.attr, entry_attr_timeout(&outarg), 0);
586 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
591 err = d_instantiate_no_diralias(entry, inode);
595 fuse_change_entry_timeout(entry, &outarg);
596 fuse_invalidate_attr(dir);
604 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
607 struct fuse_mknod_in inarg;
608 struct fuse_conn *fc = get_fuse_conn(dir);
612 mode &= ~current_umask();
614 memset(&inarg, 0, sizeof(inarg));
616 inarg.rdev = new_encode_dev(rdev);
617 inarg.umask = current_umask();
618 args.in.h.opcode = FUSE_MKNOD;
620 args.in.args[0].size = sizeof(inarg);
621 args.in.args[0].value = &inarg;
622 args.in.args[1].size = entry->d_name.len + 1;
623 args.in.args[1].value = entry->d_name.name;
624 return create_new_entry(fc, &args, dir, entry, mode);
627 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
630 return fuse_mknod(dir, entry, mode, 0);
633 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
635 struct fuse_mkdir_in inarg;
636 struct fuse_conn *fc = get_fuse_conn(dir);
640 mode &= ~current_umask();
642 memset(&inarg, 0, sizeof(inarg));
644 inarg.umask = current_umask();
645 args.in.h.opcode = FUSE_MKDIR;
647 args.in.args[0].size = sizeof(inarg);
648 args.in.args[0].value = &inarg;
649 args.in.args[1].size = entry->d_name.len + 1;
650 args.in.args[1].value = entry->d_name.name;
651 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
654 static int fuse_symlink(struct inode *dir, struct dentry *entry,
657 struct fuse_conn *fc = get_fuse_conn(dir);
658 unsigned len = strlen(link) + 1;
661 args.in.h.opcode = FUSE_SYMLINK;
663 args.in.args[0].size = entry->d_name.len + 1;
664 args.in.args[0].value = entry->d_name.name;
665 args.in.args[1].size = len;
666 args.in.args[1].value = link;
667 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
670 void fuse_update_ctime(struct inode *inode)
672 if (!IS_NOCMTIME(inode)) {
673 inode->i_ctime = current_time(inode);
674 mark_inode_dirty_sync(inode);
678 static int fuse_unlink(struct inode *dir, struct dentry *entry)
681 struct fuse_conn *fc = get_fuse_conn(dir);
684 if (fuse_is_bad(dir))
687 args.in.h.opcode = FUSE_UNLINK;
688 args.in.h.nodeid = get_node_id(dir);
690 args.in.args[0].size = entry->d_name.len + 1;
691 args.in.args[0].value = entry->d_name.name;
692 err = fuse_simple_request(fc, &args);
694 struct inode *inode = d_inode(entry);
695 struct fuse_inode *fi = get_fuse_inode(inode);
697 spin_lock(&fc->lock);
698 fi->attr_version = ++fc->attr_version;
700 * If i_nlink == 0 then unlink doesn't make sense, yet this can
701 * happen if userspace filesystem is careless. It would be
702 * difficult to enforce correct nlink usage so just ignore this
705 if (inode->i_nlink > 0)
707 spin_unlock(&fc->lock);
708 fuse_invalidate_attr(inode);
709 fuse_invalidate_attr(dir);
710 fuse_invalidate_entry_cache(entry);
711 fuse_update_ctime(inode);
712 } else if (err == -EINTR)
713 fuse_invalidate_entry(entry);
717 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
720 struct fuse_conn *fc = get_fuse_conn(dir);
723 if (fuse_is_bad(dir))
726 args.in.h.opcode = FUSE_RMDIR;
727 args.in.h.nodeid = get_node_id(dir);
729 args.in.args[0].size = entry->d_name.len + 1;
730 args.in.args[0].value = entry->d_name.name;
731 err = fuse_simple_request(fc, &args);
733 clear_nlink(d_inode(entry));
734 fuse_invalidate_attr(dir);
735 fuse_invalidate_entry_cache(entry);
736 } else if (err == -EINTR)
737 fuse_invalidate_entry(entry);
741 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
742 struct inode *newdir, struct dentry *newent,
743 unsigned int flags, int opcode, size_t argsize)
746 struct fuse_rename2_in inarg;
747 struct fuse_conn *fc = get_fuse_conn(olddir);
750 memset(&inarg, 0, argsize);
751 inarg.newdir = get_node_id(newdir);
753 args.in.h.opcode = opcode;
754 args.in.h.nodeid = get_node_id(olddir);
756 args.in.args[0].size = argsize;
757 args.in.args[0].value = &inarg;
758 args.in.args[1].size = oldent->d_name.len + 1;
759 args.in.args[1].value = oldent->d_name.name;
760 args.in.args[2].size = newent->d_name.len + 1;
761 args.in.args[2].value = newent->d_name.name;
762 err = fuse_simple_request(fc, &args);
765 fuse_invalidate_attr(d_inode(oldent));
766 fuse_update_ctime(d_inode(oldent));
768 if (flags & RENAME_EXCHANGE) {
769 fuse_invalidate_attr(d_inode(newent));
770 fuse_update_ctime(d_inode(newent));
773 fuse_invalidate_attr(olddir);
774 if (olddir != newdir)
775 fuse_invalidate_attr(newdir);
777 /* newent will end up negative */
778 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
779 fuse_invalidate_attr(d_inode(newent));
780 fuse_invalidate_entry_cache(newent);
781 fuse_update_ctime(d_inode(newent));
783 } else if (err == -EINTR) {
784 /* If request was interrupted, DEITY only knows if the
785 rename actually took place. If the invalidation
786 fails (e.g. some process has CWD under the renamed
787 directory), then there can be inconsistency between
788 the dcache and the real filesystem. Tough luck. */
789 fuse_invalidate_entry(oldent);
790 if (d_really_is_positive(newent))
791 fuse_invalidate_entry(newent);
797 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
798 struct inode *newdir, struct dentry *newent,
801 struct fuse_conn *fc = get_fuse_conn(olddir);
804 if (fuse_is_bad(olddir))
807 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
811 if (fc->no_rename2 || fc->minor < 23)
814 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
816 sizeof(struct fuse_rename2_in));
817 if (err == -ENOSYS) {
822 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
824 sizeof(struct fuse_rename_in));
830 static int fuse_link(struct dentry *entry, struct inode *newdir,
831 struct dentry *newent)
834 struct fuse_link_in inarg;
835 struct inode *inode = d_inode(entry);
836 struct fuse_conn *fc = get_fuse_conn(inode);
839 memset(&inarg, 0, sizeof(inarg));
840 inarg.oldnodeid = get_node_id(inode);
841 args.in.h.opcode = FUSE_LINK;
843 args.in.args[0].size = sizeof(inarg);
844 args.in.args[0].value = &inarg;
845 args.in.args[1].size = newent->d_name.len + 1;
846 args.in.args[1].value = newent->d_name.name;
847 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
848 /* Contrary to "normal" filesystems it can happen that link
849 makes two "logical" inodes point to the same "physical"
850 inode. We invalidate the attributes of the old one, so it
851 will reflect changes in the backing inode (link count,
855 struct fuse_inode *fi = get_fuse_inode(inode);
857 spin_lock(&fc->lock);
858 fi->attr_version = ++fc->attr_version;
859 if (likely(inode->i_nlink < UINT_MAX))
861 spin_unlock(&fc->lock);
862 fuse_invalidate_attr(inode);
863 fuse_update_ctime(inode);
864 } else if (err == -EINTR) {
865 fuse_invalidate_attr(inode);
870 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
873 unsigned int blkbits;
874 struct fuse_conn *fc = get_fuse_conn(inode);
876 /* see the comment in fuse_change_attributes() */
877 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
878 attr->size = i_size_read(inode);
879 attr->mtime = inode->i_mtime.tv_sec;
880 attr->mtimensec = inode->i_mtime.tv_nsec;
881 attr->ctime = inode->i_ctime.tv_sec;
882 attr->ctimensec = inode->i_ctime.tv_nsec;
885 stat->dev = inode->i_sb->s_dev;
886 stat->ino = attr->ino;
887 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
888 stat->nlink = attr->nlink;
889 stat->uid = make_kuid(&init_user_ns, attr->uid);
890 stat->gid = make_kgid(&init_user_ns, attr->gid);
891 stat->rdev = inode->i_rdev;
892 stat->atime.tv_sec = attr->atime;
893 stat->atime.tv_nsec = attr->atimensec;
894 stat->mtime.tv_sec = attr->mtime;
895 stat->mtime.tv_nsec = attr->mtimensec;
896 stat->ctime.tv_sec = attr->ctime;
897 stat->ctime.tv_nsec = attr->ctimensec;
898 stat->size = attr->size;
899 stat->blocks = attr->blocks;
901 if (attr->blksize != 0)
902 blkbits = ilog2(attr->blksize);
904 blkbits = inode->i_sb->s_blocksize_bits;
906 stat->blksize = 1 << blkbits;
909 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
913 struct fuse_getattr_in inarg;
914 struct fuse_attr_out outarg;
915 struct fuse_conn *fc = get_fuse_conn(inode);
919 attr_version = fuse_get_attr_version(fc);
921 memset(&inarg, 0, sizeof(inarg));
922 memset(&outarg, 0, sizeof(outarg));
923 /* Directories have separate file-handle space */
924 if (file && S_ISREG(inode->i_mode)) {
925 struct fuse_file *ff = file->private_data;
927 inarg.getattr_flags |= FUSE_GETATTR_FH;
930 args.in.h.opcode = FUSE_GETATTR;
931 args.in.h.nodeid = get_node_id(inode);
933 args.in.args[0].size = sizeof(inarg);
934 args.in.args[0].value = &inarg;
935 args.out.numargs = 1;
936 args.out.args[0].size = sizeof(outarg);
937 args.out.args[0].value = &outarg;
938 err = fuse_simple_request(fc, &args);
940 if (fuse_invalid_attr(&outarg.attr) ||
941 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
942 fuse_make_bad(inode);
945 fuse_change_attributes(inode, &outarg.attr,
946 attr_timeout(&outarg),
949 fuse_fillattr(inode, &outarg.attr, stat);
955 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
956 struct file *file, bool *refreshed)
958 struct fuse_inode *fi = get_fuse_inode(inode);
962 if (time_before64(fi->i_time, get_jiffies_64())) {
964 forget_all_cached_acls(inode);
965 err = fuse_do_getattr(inode, stat, file);
970 generic_fillattr(inode, stat);
971 stat->mode = fi->orig_i_mode;
972 stat->ino = fi->orig_ino;
976 if (refreshed != NULL)
982 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
983 u64 child_nodeid, struct qstr *name)
986 struct inode *parent;
988 struct dentry *entry;
990 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
994 inode_lock_nested(parent, I_MUTEX_PARENT);
995 if (!S_ISDIR(parent->i_mode))
999 dir = d_find_alias(parent);
1003 name->hash = full_name_hash(dir, name->name, name->len);
1004 entry = d_lookup(dir, name);
1009 fuse_invalidate_attr(parent);
1010 fuse_invalidate_entry(entry);
1012 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1013 inode_lock(d_inode(entry));
1014 if (get_node_id(d_inode(entry)) != child_nodeid) {
1018 if (d_mountpoint(entry)) {
1022 if (d_is_dir(entry)) {
1023 shrink_dcache_parent(entry);
1024 if (!simple_empty(entry)) {
1028 d_inode(entry)->i_flags |= S_DEAD;
1031 clear_nlink(d_inode(entry));
1034 inode_unlock(d_inode(entry));
1043 inode_unlock(parent);
1049 * Calling into a user-controlled filesystem gives the filesystem
1050 * daemon ptrace-like capabilities over the current process. This
1051 * means, that the filesystem daemon is able to record the exact
1052 * filesystem operations performed, and can also control the behavior
1053 * of the requester process in otherwise impossible ways. For example
1054 * it can delay the operation for arbitrary length of time allowing
1055 * DoS against the requester.
1057 * For this reason only those processes can call into the filesystem,
1058 * for which the owner of the mount has ptrace privilege. This
1059 * excludes processes started by other users, suid or sgid processes.
1061 int fuse_allow_current_process(struct fuse_conn *fc)
1063 const struct cred *cred;
1065 if (fc->allow_other)
1068 cred = current_cred();
1069 if (uid_eq(cred->euid, fc->user_id) &&
1070 uid_eq(cred->suid, fc->user_id) &&
1071 uid_eq(cred->uid, fc->user_id) &&
1072 gid_eq(cred->egid, fc->group_id) &&
1073 gid_eq(cred->sgid, fc->group_id) &&
1074 gid_eq(cred->gid, fc->group_id))
1080 static int fuse_access(struct inode *inode, int mask)
1082 struct fuse_conn *fc = get_fuse_conn(inode);
1084 struct fuse_access_in inarg;
1087 BUG_ON(mask & MAY_NOT_BLOCK);
1092 memset(&inarg, 0, sizeof(inarg));
1093 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1094 args.in.h.opcode = FUSE_ACCESS;
1095 args.in.h.nodeid = get_node_id(inode);
1096 args.in.numargs = 1;
1097 args.in.args[0].size = sizeof(inarg);
1098 args.in.args[0].value = &inarg;
1099 err = fuse_simple_request(fc, &args);
1100 if (err == -ENOSYS) {
1107 static int fuse_perm_getattr(struct inode *inode, int mask)
1109 if (mask & MAY_NOT_BLOCK)
1112 forget_all_cached_acls(inode);
1113 return fuse_do_getattr(inode, NULL, NULL);
1117 * Check permission. The two basic access models of FUSE are:
1119 * 1) Local access checking ('default_permissions' mount option) based
1120 * on file mode. This is the plain old disk filesystem permission
1123 * 2) "Remote" access checking, where server is responsible for
1124 * checking permission in each inode operation. An exception to this
1125 * is if ->permission() was invoked from sys_access() in which case an
1126 * access request is sent. Execute permission is still checked
1127 * locally based on file mode.
1129 static int fuse_permission(struct inode *inode, int mask)
1131 struct fuse_conn *fc = get_fuse_conn(inode);
1132 bool refreshed = false;
1135 if (fuse_is_bad(inode))
1138 if (!fuse_allow_current_process(fc))
1142 * If attributes are needed, refresh them before proceeding
1144 if (fc->default_permissions ||
1145 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1146 struct fuse_inode *fi = get_fuse_inode(inode);
1148 if (time_before64(fi->i_time, get_jiffies_64())) {
1151 err = fuse_perm_getattr(inode, mask);
1157 if (fc->default_permissions) {
1158 err = generic_permission(inode, mask);
1160 /* If permission is denied, try to refresh file
1161 attributes. This is also needed, because the root
1162 node will at first have no permissions */
1163 if (err == -EACCES && !refreshed) {
1164 err = fuse_perm_getattr(inode, mask);
1166 err = generic_permission(inode, mask);
1169 /* Note: the opposite of the above test does not
1170 exist. So if permissions are revoked this won't be
1171 noticed immediately, only after the attribute
1172 timeout has expired */
1173 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1174 err = fuse_access(inode, mask);
1175 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1176 if (!(inode->i_mode & S_IXUGO)) {
1180 err = fuse_perm_getattr(inode, mask);
1181 if (!err && !(inode->i_mode & S_IXUGO))
1188 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1189 struct dir_context *ctx)
1191 while (nbytes >= FUSE_NAME_OFFSET) {
1192 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1193 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1194 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1196 if (reclen > nbytes)
1198 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1201 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1202 dirent->ino, dirent->type))
1207 ctx->pos = dirent->off;
1213 static int fuse_direntplus_link(struct file *file,
1214 struct fuse_direntplus *direntplus,
1217 struct fuse_entry_out *o = &direntplus->entry_out;
1218 struct fuse_dirent *dirent = &direntplus->dirent;
1219 struct dentry *parent = file->f_path.dentry;
1220 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1221 struct dentry *dentry;
1222 struct dentry *alias;
1223 struct inode *dir = d_inode(parent);
1224 struct fuse_conn *fc;
1225 struct inode *inode;
1226 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1230 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1231 * ENOENT. Instead, it only means the userspace filesystem did
1232 * not want to return attributes/handle for this entry.
1239 if (name.name[0] == '.') {
1241 * We could potentially refresh the attributes of the directory
1246 if (name.name[1] == '.' && name.len == 2)
1250 if (invalid_nodeid(o->nodeid))
1252 if (fuse_invalid_attr(&o->attr))
1255 fc = get_fuse_conn(dir);
1257 name.hash = full_name_hash(parent, name.name, name.len);
1258 dentry = d_lookup(parent, &name);
1261 dentry = d_alloc_parallel(parent, &name, &wq);
1263 return PTR_ERR(dentry);
1265 if (!d_in_lookup(dentry)) {
1266 struct fuse_inode *fi;
1267 inode = d_inode(dentry);
1269 get_node_id(inode) != o->nodeid ||
1270 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1271 d_invalidate(dentry);
1275 if (fuse_is_bad(inode)) {
1280 fi = get_fuse_inode(inode);
1281 spin_lock(&fc->lock);
1283 spin_unlock(&fc->lock);
1285 forget_all_cached_acls(inode);
1286 fuse_change_attributes(inode, &o->attr,
1287 entry_attr_timeout(o),
1290 * The other branch comes via fuse_iget()
1291 * which bumps nlookup inside
1294 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1295 &o->attr, entry_attr_timeout(o),
1298 inode = ERR_PTR(-ENOMEM);
1300 alias = d_splice_alias(inode, dentry);
1301 d_lookup_done(dentry);
1307 return PTR_ERR(dentry);
1309 if (fc->readdirplus_auto)
1310 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1311 fuse_change_entry_timeout(dentry, o);
1317 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1318 struct dir_context *ctx, u64 attr_version)
1320 struct fuse_direntplus *direntplus;
1321 struct fuse_dirent *dirent;
1326 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1327 direntplus = (struct fuse_direntplus *) buf;
1328 dirent = &direntplus->dirent;
1329 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1331 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1333 if (reclen > nbytes)
1335 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1339 /* We fill entries into dstbuf only as much as
1340 it can hold. But we still continue iterating
1341 over remaining entries to link them. If not,
1342 we need to send a FORGET for each of those
1343 which we did not link.
1345 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1346 dirent->ino, dirent->type);
1348 ctx->pos = dirent->off;
1354 ret = fuse_direntplus_link(file, direntplus, attr_version);
1356 fuse_force_forget(file, direntplus->entry_out.nodeid);
1362 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1367 struct inode *inode = file_inode(file);
1368 struct fuse_conn *fc = get_fuse_conn(inode);
1369 struct fuse_req *req;
1370 u64 attr_version = 0;
1373 if (fuse_is_bad(inode))
1376 req = fuse_get_req(fc, 1);
1378 return PTR_ERR(req);
1380 page = alloc_page(GFP_KERNEL);
1382 fuse_put_request(fc, req);
1386 plus = fuse_use_readdirplus(inode, ctx);
1387 req->out.argpages = 1;
1389 req->pages[0] = page;
1390 req->page_descs[0].length = PAGE_SIZE;
1392 attr_version = fuse_get_attr_version(fc);
1393 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1396 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1399 locked = fuse_lock_inode(inode);
1400 fuse_request_send(fc, req);
1401 fuse_unlock_inode(inode, locked);
1402 nbytes = req->out.args[0].size;
1403 err = req->out.h.error;
1404 fuse_put_request(fc, req);
1407 err = parse_dirplusfile(page_address(page), nbytes,
1411 err = parse_dirfile(page_address(page), nbytes, file,
1417 fuse_invalidate_atime(inode);
1421 static const char *fuse_get_link(struct dentry *dentry,
1422 struct inode *inode,
1423 struct delayed_call *done)
1425 struct fuse_conn *fc = get_fuse_conn(inode);
1431 return ERR_PTR(-ECHILD);
1433 if (fuse_is_bad(inode))
1434 return ERR_PTR(-EIO);
1436 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
1438 return ERR_PTR(-ENOMEM);
1440 args.in.h.opcode = FUSE_READLINK;
1441 args.in.h.nodeid = get_node_id(inode);
1442 args.out.argvar = 1;
1443 args.out.numargs = 1;
1444 args.out.args[0].size = PAGE_SIZE - 1;
1445 args.out.args[0].value = link;
1446 ret = fuse_simple_request(fc, &args);
1449 link = ERR_PTR(ret);
1452 set_delayed_call(done, kfree_link, link);
1454 fuse_invalidate_atime(inode);
1458 static int fuse_dir_open(struct inode *inode, struct file *file)
1460 return fuse_open_common(inode, file, true);
1463 static int fuse_dir_release(struct inode *inode, struct file *file)
1465 fuse_release_common(file, FUSE_RELEASEDIR);
1470 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1473 return fuse_fsync_common(file, start, end, datasync, 1);
1476 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1479 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1481 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1485 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1488 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1491 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1496 return fuse_ioctl_common(file, cmd, arg,
1497 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1500 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1502 /* Always update if mtime is explicitly set */
1503 if (ivalid & ATTR_MTIME_SET)
1506 /* Or if kernel i_mtime is the official one */
1507 if (trust_local_mtime)
1510 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1511 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1514 /* In all other cases update */
1518 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1519 bool trust_local_cmtime)
1521 unsigned ivalid = iattr->ia_valid;
1523 if (ivalid & ATTR_MODE)
1524 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1525 if (ivalid & ATTR_UID)
1526 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1527 if (ivalid & ATTR_GID)
1528 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1529 if (ivalid & ATTR_SIZE)
1530 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1531 if (ivalid & ATTR_ATIME) {
1532 arg->valid |= FATTR_ATIME;
1533 arg->atime = iattr->ia_atime.tv_sec;
1534 arg->atimensec = iattr->ia_atime.tv_nsec;
1535 if (!(ivalid & ATTR_ATIME_SET))
1536 arg->valid |= FATTR_ATIME_NOW;
1538 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1539 arg->valid |= FATTR_MTIME;
1540 arg->mtime = iattr->ia_mtime.tv_sec;
1541 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1542 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1543 arg->valid |= FATTR_MTIME_NOW;
1545 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1546 arg->valid |= FATTR_CTIME;
1547 arg->ctime = iattr->ia_ctime.tv_sec;
1548 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1553 * Prevent concurrent writepages on inode
1555 * This is done by adding a negative bias to the inode write counter
1556 * and waiting for all pending writes to finish.
1558 void fuse_set_nowrite(struct inode *inode)
1560 struct fuse_conn *fc = get_fuse_conn(inode);
1561 struct fuse_inode *fi = get_fuse_inode(inode);
1563 BUG_ON(!inode_is_locked(inode));
1565 spin_lock(&fc->lock);
1566 BUG_ON(fi->writectr < 0);
1567 fi->writectr += FUSE_NOWRITE;
1568 spin_unlock(&fc->lock);
1569 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1573 * Allow writepages on inode
1575 * Remove the bias from the writecounter and send any queued
1578 static void __fuse_release_nowrite(struct inode *inode)
1580 struct fuse_inode *fi = get_fuse_inode(inode);
1582 BUG_ON(fi->writectr != FUSE_NOWRITE);
1584 fuse_flush_writepages(inode);
1587 void fuse_release_nowrite(struct inode *inode)
1589 struct fuse_conn *fc = get_fuse_conn(inode);
1591 spin_lock(&fc->lock);
1592 __fuse_release_nowrite(inode);
1593 spin_unlock(&fc->lock);
1596 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1597 struct inode *inode,
1598 struct fuse_setattr_in *inarg_p,
1599 struct fuse_attr_out *outarg_p)
1601 args->in.h.opcode = FUSE_SETATTR;
1602 args->in.h.nodeid = get_node_id(inode);
1603 args->in.numargs = 1;
1604 args->in.args[0].size = sizeof(*inarg_p);
1605 args->in.args[0].value = inarg_p;
1606 args->out.numargs = 1;
1607 args->out.args[0].size = sizeof(*outarg_p);
1608 args->out.args[0].value = outarg_p;
1612 * Flush inode->i_mtime to the server
1614 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1616 struct fuse_conn *fc = get_fuse_conn(inode);
1618 struct fuse_setattr_in inarg;
1619 struct fuse_attr_out outarg;
1621 memset(&inarg, 0, sizeof(inarg));
1622 memset(&outarg, 0, sizeof(outarg));
1624 inarg.valid = FATTR_MTIME;
1625 inarg.mtime = inode->i_mtime.tv_sec;
1626 inarg.mtimensec = inode->i_mtime.tv_nsec;
1627 if (fc->minor >= 23) {
1628 inarg.valid |= FATTR_CTIME;
1629 inarg.ctime = inode->i_ctime.tv_sec;
1630 inarg.ctimensec = inode->i_ctime.tv_nsec;
1633 inarg.valid |= FATTR_FH;
1636 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1638 return fuse_simple_request(fc, &args);
1642 * Set attributes, and at the same time refresh them.
1644 * Truncation is slightly complicated, because the 'truncate' request
1645 * may fail, in which case we don't want to touch the mapping.
1646 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1647 * and the actual truncation by hand.
1649 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1652 struct inode *inode = d_inode(dentry);
1653 struct fuse_conn *fc = get_fuse_conn(inode);
1654 struct fuse_inode *fi = get_fuse_inode(inode);
1656 struct fuse_setattr_in inarg;
1657 struct fuse_attr_out outarg;
1658 bool is_truncate = false;
1659 bool is_wb = fc->writeback_cache;
1662 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1664 if (!fc->default_permissions)
1665 attr->ia_valid |= ATTR_FORCE;
1667 err = setattr_prepare(dentry, attr);
1671 if (attr->ia_valid & ATTR_OPEN) {
1672 /* This is coming from open(..., ... | O_TRUNC); */
1673 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1674 WARN_ON(attr->ia_size != 0);
1675 if (fc->atomic_o_trunc) {
1677 * No need to send request to userspace, since actual
1678 * truncation has already been done by OPEN. But still
1679 * need to truncate page cache.
1681 i_size_write(inode, 0);
1682 truncate_pagecache(inode, 0);
1688 if (attr->ia_valid & ATTR_SIZE)
1691 /* Flush dirty data/metadata before non-truncate SETATTR */
1692 if (is_wb && S_ISREG(inode->i_mode) &&
1694 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1696 err = write_inode_now(inode, true);
1700 fuse_set_nowrite(inode);
1701 fuse_release_nowrite(inode);
1705 fuse_set_nowrite(inode);
1706 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1707 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1708 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1711 memset(&inarg, 0, sizeof(inarg));
1712 memset(&outarg, 0, sizeof(outarg));
1713 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1715 struct fuse_file *ff = file->private_data;
1716 inarg.valid |= FATTR_FH;
1719 if (attr->ia_valid & ATTR_SIZE) {
1720 /* For mandatory locking in truncate */
1721 inarg.valid |= FATTR_LOCKOWNER;
1722 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1724 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1725 err = fuse_simple_request(fc, &args);
1728 fuse_invalidate_attr(inode);
1732 if (fuse_invalid_attr(&outarg.attr) ||
1733 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1734 fuse_make_bad(inode);
1739 spin_lock(&fc->lock);
1740 /* the kernel maintains i_mtime locally */
1741 if (trust_local_cmtime) {
1742 if (attr->ia_valid & ATTR_MTIME)
1743 inode->i_mtime = attr->ia_mtime;
1744 if (attr->ia_valid & ATTR_CTIME)
1745 inode->i_ctime = attr->ia_ctime;
1746 /* FIXME: clear I_DIRTY_SYNC? */
1749 fuse_change_attributes_common(inode, &outarg.attr,
1750 attr_timeout(&outarg));
1751 oldsize = inode->i_size;
1752 /* see the comment in fuse_change_attributes() */
1753 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1754 i_size_write(inode, outarg.attr.size);
1757 /* NOTE: this may release/reacquire fc->lock */
1758 __fuse_release_nowrite(inode);
1760 spin_unlock(&fc->lock);
1763 * Only call invalidate_inode_pages2() after removing
1764 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1766 if ((is_truncate || !is_wb) &&
1767 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1768 truncate_pagecache(inode, outarg.attr.size);
1769 invalidate_inode_pages2(inode->i_mapping);
1772 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1777 fuse_release_nowrite(inode);
1779 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1783 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1785 struct inode *inode = d_inode(entry);
1786 struct fuse_conn *fc = get_fuse_conn(inode);
1787 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1790 if (fuse_is_bad(inode))
1793 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1796 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1797 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1801 * The only sane way to reliably kill suid/sgid is to do it in
1802 * the userspace filesystem
1804 * This should be done on write(), truncate() and chown().
1806 if (!fc->handle_killpriv) {
1808 * ia_mode calculation may have used stale i_mode.
1809 * Refresh and recalculate.
1811 ret = fuse_do_getattr(inode, NULL, file);
1815 attr->ia_mode = inode->i_mode;
1816 if (inode->i_mode & S_ISUID) {
1817 attr->ia_valid |= ATTR_MODE;
1818 attr->ia_mode &= ~S_ISUID;
1820 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1821 attr->ia_valid |= ATTR_MODE;
1822 attr->ia_mode &= ~S_ISGID;
1826 if (!attr->ia_valid)
1829 ret = fuse_do_setattr(entry, attr, file);
1832 * If filesystem supports acls it may have updated acl xattrs in
1833 * the filesystem, so forget cached acls for the inode.
1836 forget_all_cached_acls(inode);
1838 /* Directory mode changed, may need to revalidate access */
1839 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1840 fuse_invalidate_entry_cache(entry);
1845 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1848 struct inode *inode = d_inode(entry);
1849 struct fuse_conn *fc = get_fuse_conn(inode);
1851 if (fuse_is_bad(inode))
1854 if (!fuse_allow_current_process(fc))
1857 return fuse_update_attributes(inode, stat, NULL, NULL);
1860 static const struct inode_operations fuse_dir_inode_operations = {
1861 .lookup = fuse_lookup,
1862 .mkdir = fuse_mkdir,
1863 .symlink = fuse_symlink,
1864 .unlink = fuse_unlink,
1865 .rmdir = fuse_rmdir,
1866 .rename = fuse_rename2,
1868 .setattr = fuse_setattr,
1869 .create = fuse_create,
1870 .atomic_open = fuse_atomic_open,
1871 .mknod = fuse_mknod,
1872 .permission = fuse_permission,
1873 .getattr = fuse_getattr,
1874 .listxattr = fuse_listxattr,
1875 .get_acl = fuse_get_acl,
1876 .set_acl = fuse_set_acl,
1879 static const struct file_operations fuse_dir_operations = {
1880 .llseek = generic_file_llseek,
1881 .read = generic_read_dir,
1882 .iterate_shared = fuse_readdir,
1883 .open = fuse_dir_open,
1884 .release = fuse_dir_release,
1885 .fsync = fuse_dir_fsync,
1886 .unlocked_ioctl = fuse_dir_ioctl,
1887 .compat_ioctl = fuse_dir_compat_ioctl,
1890 static const struct inode_operations fuse_common_inode_operations = {
1891 .setattr = fuse_setattr,
1892 .permission = fuse_permission,
1893 .getattr = fuse_getattr,
1894 .listxattr = fuse_listxattr,
1895 .get_acl = fuse_get_acl,
1896 .set_acl = fuse_set_acl,
1899 static const struct inode_operations fuse_symlink_inode_operations = {
1900 .setattr = fuse_setattr,
1901 .get_link = fuse_get_link,
1902 .readlink = generic_readlink,
1903 .getattr = fuse_getattr,
1904 .listxattr = fuse_listxattr,
1907 void fuse_init_common(struct inode *inode)
1909 inode->i_op = &fuse_common_inode_operations;
1912 void fuse_init_dir(struct inode *inode)
1914 inode->i_op = &fuse_dir_inode_operations;
1915 inode->i_fop = &fuse_dir_operations;
1918 void fuse_init_symlink(struct inode *inode)
1920 inode->i_op = &fuse_symlink_inode_operations;