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);
235 if (ret == -ENOMEM || ret == -EINTR)
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 = 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 static int fuse_update_get_attr(struct inode *inode, struct file *file,
958 struct fuse_inode *fi = get_fuse_inode(inode);
961 if (time_before64(fi->i_time, get_jiffies_64())) {
962 forget_all_cached_acls(inode);
963 err = fuse_do_getattr(inode, stat, file);
965 generic_fillattr(inode, stat);
966 stat->mode = fi->orig_i_mode;
967 stat->ino = fi->orig_ino;
973 int fuse_update_attributes(struct inode *inode, struct file *file)
975 return fuse_update_get_attr(inode, file, NULL);
978 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
979 u64 child_nodeid, struct qstr *name)
982 struct inode *parent;
984 struct dentry *entry;
986 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
990 inode_lock_nested(parent, I_MUTEX_PARENT);
991 if (!S_ISDIR(parent->i_mode))
995 dir = d_find_alias(parent);
999 name->hash = full_name_hash(dir, name->name, name->len);
1000 entry = d_lookup(dir, name);
1005 fuse_invalidate_attr(parent);
1006 fuse_invalidate_entry(entry);
1008 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1009 inode_lock(d_inode(entry));
1010 if (get_node_id(d_inode(entry)) != child_nodeid) {
1014 if (d_mountpoint(entry)) {
1018 if (d_is_dir(entry)) {
1019 shrink_dcache_parent(entry);
1020 if (!simple_empty(entry)) {
1024 d_inode(entry)->i_flags |= S_DEAD;
1027 clear_nlink(d_inode(entry));
1030 inode_unlock(d_inode(entry));
1039 inode_unlock(parent);
1045 * Calling into a user-controlled filesystem gives the filesystem
1046 * daemon ptrace-like capabilities over the current process. This
1047 * means, that the filesystem daemon is able to record the exact
1048 * filesystem operations performed, and can also control the behavior
1049 * of the requester process in otherwise impossible ways. For example
1050 * it can delay the operation for arbitrary length of time allowing
1051 * DoS against the requester.
1053 * For this reason only those processes can call into the filesystem,
1054 * for which the owner of the mount has ptrace privilege. This
1055 * excludes processes started by other users, suid or sgid processes.
1057 int fuse_allow_current_process(struct fuse_conn *fc)
1059 const struct cred *cred;
1061 if (fc->allow_other)
1064 cred = current_cred();
1065 if (uid_eq(cred->euid, fc->user_id) &&
1066 uid_eq(cred->suid, fc->user_id) &&
1067 uid_eq(cred->uid, fc->user_id) &&
1068 gid_eq(cred->egid, fc->group_id) &&
1069 gid_eq(cred->sgid, fc->group_id) &&
1070 gid_eq(cred->gid, fc->group_id))
1076 static int fuse_access(struct inode *inode, int mask)
1078 struct fuse_conn *fc = get_fuse_conn(inode);
1080 struct fuse_access_in inarg;
1083 BUG_ON(mask & MAY_NOT_BLOCK);
1088 memset(&inarg, 0, sizeof(inarg));
1089 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1090 args.in.h.opcode = FUSE_ACCESS;
1091 args.in.h.nodeid = get_node_id(inode);
1092 args.in.numargs = 1;
1093 args.in.args[0].size = sizeof(inarg);
1094 args.in.args[0].value = &inarg;
1095 err = fuse_simple_request(fc, &args);
1096 if (err == -ENOSYS) {
1103 static int fuse_perm_getattr(struct inode *inode, int mask)
1105 if (mask & MAY_NOT_BLOCK)
1108 forget_all_cached_acls(inode);
1109 return fuse_do_getattr(inode, NULL, NULL);
1113 * Check permission. The two basic access models of FUSE are:
1115 * 1) Local access checking ('default_permissions' mount option) based
1116 * on file mode. This is the plain old disk filesystem permission
1119 * 2) "Remote" access checking, where server is responsible for
1120 * checking permission in each inode operation. An exception to this
1121 * is if ->permission() was invoked from sys_access() in which case an
1122 * access request is sent. Execute permission is still checked
1123 * locally based on file mode.
1125 static int fuse_permission(struct inode *inode, int mask)
1127 struct fuse_conn *fc = get_fuse_conn(inode);
1128 bool refreshed = false;
1131 if (fuse_is_bad(inode))
1134 if (!fuse_allow_current_process(fc))
1138 * If attributes are needed, refresh them before proceeding
1140 if (fc->default_permissions ||
1141 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1142 struct fuse_inode *fi = get_fuse_inode(inode);
1144 if (time_before64(fi->i_time, get_jiffies_64())) {
1147 err = fuse_perm_getattr(inode, mask);
1153 if (fc->default_permissions) {
1154 err = generic_permission(inode, mask);
1156 /* If permission is denied, try to refresh file
1157 attributes. This is also needed, because the root
1158 node will at first have no permissions */
1159 if (err == -EACCES && !refreshed) {
1160 err = fuse_perm_getattr(inode, mask);
1162 err = generic_permission(inode, mask);
1165 /* Note: the opposite of the above test does not
1166 exist. So if permissions are revoked this won't be
1167 noticed immediately, only after the attribute
1168 timeout has expired */
1169 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1170 err = fuse_access(inode, mask);
1171 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1172 if (!(inode->i_mode & S_IXUGO)) {
1176 err = fuse_perm_getattr(inode, mask);
1177 if (!err && !(inode->i_mode & S_IXUGO))
1184 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1185 struct dir_context *ctx)
1187 while (nbytes >= FUSE_NAME_OFFSET) {
1188 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1189 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1190 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1192 if (reclen > nbytes)
1194 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1197 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1198 dirent->ino, dirent->type))
1203 ctx->pos = dirent->off;
1209 static int fuse_direntplus_link(struct file *file,
1210 struct fuse_direntplus *direntplus,
1213 struct fuse_entry_out *o = &direntplus->entry_out;
1214 struct fuse_dirent *dirent = &direntplus->dirent;
1215 struct dentry *parent = file->f_path.dentry;
1216 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1217 struct dentry *dentry;
1218 struct dentry *alias;
1219 struct inode *dir = d_inode(parent);
1220 struct fuse_conn *fc;
1221 struct inode *inode;
1222 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1226 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1227 * ENOENT. Instead, it only means the userspace filesystem did
1228 * not want to return attributes/handle for this entry.
1235 if (name.name[0] == '.') {
1237 * We could potentially refresh the attributes of the directory
1242 if (name.name[1] == '.' && name.len == 2)
1246 if (invalid_nodeid(o->nodeid))
1248 if (fuse_invalid_attr(&o->attr))
1251 fc = get_fuse_conn(dir);
1253 name.hash = full_name_hash(parent, name.name, name.len);
1254 dentry = d_lookup(parent, &name);
1257 dentry = d_alloc_parallel(parent, &name, &wq);
1259 return PTR_ERR(dentry);
1261 if (!d_in_lookup(dentry)) {
1262 struct fuse_inode *fi;
1263 inode = d_inode(dentry);
1265 get_node_id(inode) != o->nodeid ||
1266 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1267 d_invalidate(dentry);
1271 if (fuse_is_bad(inode)) {
1276 fi = get_fuse_inode(inode);
1277 spin_lock(&fc->lock);
1279 spin_unlock(&fc->lock);
1281 forget_all_cached_acls(inode);
1282 fuse_change_attributes(inode, &o->attr,
1283 entry_attr_timeout(o),
1286 * The other branch comes via fuse_iget()
1287 * which bumps nlookup inside
1290 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1291 &o->attr, entry_attr_timeout(o),
1294 inode = ERR_PTR(-ENOMEM);
1296 alias = d_splice_alias(inode, dentry);
1297 d_lookup_done(dentry);
1303 return PTR_ERR(dentry);
1305 if (fc->readdirplus_auto)
1306 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1307 fuse_change_entry_timeout(dentry, o);
1313 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1314 struct dir_context *ctx, u64 attr_version)
1316 struct fuse_direntplus *direntplus;
1317 struct fuse_dirent *dirent;
1322 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1323 direntplus = (struct fuse_direntplus *) buf;
1324 dirent = &direntplus->dirent;
1325 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1327 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1329 if (reclen > nbytes)
1331 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1335 /* We fill entries into dstbuf only as much as
1336 it can hold. But we still continue iterating
1337 over remaining entries to link them. If not,
1338 we need to send a FORGET for each of those
1339 which we did not link.
1341 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1342 dirent->ino, dirent->type);
1344 ctx->pos = dirent->off;
1350 ret = fuse_direntplus_link(file, direntplus, attr_version);
1352 fuse_force_forget(file, direntplus->entry_out.nodeid);
1358 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1363 struct inode *inode = file_inode(file);
1364 struct fuse_conn *fc = get_fuse_conn(inode);
1365 struct fuse_req *req;
1366 u64 attr_version = 0;
1369 if (fuse_is_bad(inode))
1372 req = fuse_get_req(fc, 1);
1374 return PTR_ERR(req);
1376 page = alloc_page(GFP_KERNEL);
1378 fuse_put_request(fc, req);
1382 plus = fuse_use_readdirplus(inode, ctx);
1383 req->out.argpages = 1;
1385 req->pages[0] = page;
1386 req->page_descs[0].length = PAGE_SIZE;
1388 attr_version = fuse_get_attr_version(fc);
1389 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1392 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1395 locked = fuse_lock_inode(inode);
1396 fuse_request_send(fc, req);
1397 fuse_unlock_inode(inode, locked);
1398 nbytes = req->out.args[0].size;
1399 err = req->out.h.error;
1400 fuse_put_request(fc, req);
1403 err = parse_dirplusfile(page_address(page), nbytes,
1407 err = parse_dirfile(page_address(page), nbytes, file,
1413 fuse_invalidate_atime(inode);
1417 static const char *fuse_get_link(struct dentry *dentry,
1418 struct inode *inode,
1419 struct delayed_call *done)
1421 struct fuse_conn *fc = get_fuse_conn(inode);
1427 return ERR_PTR(-ECHILD);
1429 if (fuse_is_bad(inode))
1430 return ERR_PTR(-EIO);
1432 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
1434 return ERR_PTR(-ENOMEM);
1436 args.in.h.opcode = FUSE_READLINK;
1437 args.in.h.nodeid = get_node_id(inode);
1438 args.out.argvar = 1;
1439 args.out.numargs = 1;
1440 args.out.args[0].size = PAGE_SIZE - 1;
1441 args.out.args[0].value = link;
1442 ret = fuse_simple_request(fc, &args);
1445 link = ERR_PTR(ret);
1448 set_delayed_call(done, kfree_link, link);
1450 fuse_invalidate_atime(inode);
1454 static int fuse_dir_open(struct inode *inode, struct file *file)
1456 return fuse_open_common(inode, file, true);
1459 static int fuse_dir_release(struct inode *inode, struct file *file)
1461 fuse_release_common(file, true);
1466 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1469 return fuse_fsync_common(file, start, end, datasync, 1);
1472 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1475 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1477 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1481 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1484 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1487 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1492 return fuse_ioctl_common(file, cmd, arg,
1493 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1496 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1498 /* Always update if mtime is explicitly set */
1499 if (ivalid & ATTR_MTIME_SET)
1502 /* Or if kernel i_mtime is the official one */
1503 if (trust_local_mtime)
1506 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1507 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1510 /* In all other cases update */
1514 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1515 bool trust_local_cmtime)
1517 unsigned ivalid = iattr->ia_valid;
1519 if (ivalid & ATTR_MODE)
1520 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1521 if (ivalid & ATTR_UID)
1522 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1523 if (ivalid & ATTR_GID)
1524 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1525 if (ivalid & ATTR_SIZE)
1526 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1527 if (ivalid & ATTR_ATIME) {
1528 arg->valid |= FATTR_ATIME;
1529 arg->atime = iattr->ia_atime.tv_sec;
1530 arg->atimensec = iattr->ia_atime.tv_nsec;
1531 if (!(ivalid & ATTR_ATIME_SET))
1532 arg->valid |= FATTR_ATIME_NOW;
1534 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1535 arg->valid |= FATTR_MTIME;
1536 arg->mtime = iattr->ia_mtime.tv_sec;
1537 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1538 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1539 arg->valid |= FATTR_MTIME_NOW;
1541 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1542 arg->valid |= FATTR_CTIME;
1543 arg->ctime = iattr->ia_ctime.tv_sec;
1544 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1549 * Prevent concurrent writepages on inode
1551 * This is done by adding a negative bias to the inode write counter
1552 * and waiting for all pending writes to finish.
1554 void fuse_set_nowrite(struct inode *inode)
1556 struct fuse_conn *fc = get_fuse_conn(inode);
1557 struct fuse_inode *fi = get_fuse_inode(inode);
1559 BUG_ON(!inode_is_locked(inode));
1561 spin_lock(&fc->lock);
1562 BUG_ON(fi->writectr < 0);
1563 fi->writectr += FUSE_NOWRITE;
1564 spin_unlock(&fc->lock);
1565 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1569 * Allow writepages on inode
1571 * Remove the bias from the writecounter and send any queued
1574 static void __fuse_release_nowrite(struct inode *inode)
1576 struct fuse_inode *fi = get_fuse_inode(inode);
1578 BUG_ON(fi->writectr != FUSE_NOWRITE);
1580 fuse_flush_writepages(inode);
1583 void fuse_release_nowrite(struct inode *inode)
1585 struct fuse_conn *fc = get_fuse_conn(inode);
1587 spin_lock(&fc->lock);
1588 __fuse_release_nowrite(inode);
1589 spin_unlock(&fc->lock);
1592 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1593 struct inode *inode,
1594 struct fuse_setattr_in *inarg_p,
1595 struct fuse_attr_out *outarg_p)
1597 args->in.h.opcode = FUSE_SETATTR;
1598 args->in.h.nodeid = get_node_id(inode);
1599 args->in.numargs = 1;
1600 args->in.args[0].size = sizeof(*inarg_p);
1601 args->in.args[0].value = inarg_p;
1602 args->out.numargs = 1;
1603 args->out.args[0].size = sizeof(*outarg_p);
1604 args->out.args[0].value = outarg_p;
1608 * Flush inode->i_mtime to the server
1610 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1612 struct fuse_conn *fc = get_fuse_conn(inode);
1614 struct fuse_setattr_in inarg;
1615 struct fuse_attr_out outarg;
1617 memset(&inarg, 0, sizeof(inarg));
1618 memset(&outarg, 0, sizeof(outarg));
1620 inarg.valid = FATTR_MTIME;
1621 inarg.mtime = inode->i_mtime.tv_sec;
1622 inarg.mtimensec = inode->i_mtime.tv_nsec;
1623 if (fc->minor >= 23) {
1624 inarg.valid |= FATTR_CTIME;
1625 inarg.ctime = inode->i_ctime.tv_sec;
1626 inarg.ctimensec = inode->i_ctime.tv_nsec;
1629 inarg.valid |= FATTR_FH;
1632 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1634 return fuse_simple_request(fc, &args);
1638 * Set attributes, and at the same time refresh them.
1640 * Truncation is slightly complicated, because the 'truncate' request
1641 * may fail, in which case we don't want to touch the mapping.
1642 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1643 * and the actual truncation by hand.
1645 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1648 struct inode *inode = d_inode(dentry);
1649 struct fuse_conn *fc = get_fuse_conn(inode);
1650 struct fuse_inode *fi = get_fuse_inode(inode);
1652 struct fuse_setattr_in inarg;
1653 struct fuse_attr_out outarg;
1654 bool is_truncate = false;
1655 bool is_wb = fc->writeback_cache;
1658 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1660 if (!fc->default_permissions)
1661 attr->ia_valid |= ATTR_FORCE;
1663 err = setattr_prepare(dentry, attr);
1667 if (attr->ia_valid & ATTR_OPEN) {
1668 /* This is coming from open(..., ... | O_TRUNC); */
1669 WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1670 WARN_ON(attr->ia_size != 0);
1671 if (fc->atomic_o_trunc) {
1673 * No need to send request to userspace, since actual
1674 * truncation has already been done by OPEN. But still
1675 * need to truncate page cache.
1677 i_size_write(inode, 0);
1678 truncate_pagecache(inode, 0);
1684 if (attr->ia_valid & ATTR_SIZE)
1687 /* Flush dirty data/metadata before non-truncate SETATTR */
1688 if (is_wb && S_ISREG(inode->i_mode) &&
1690 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1692 err = write_inode_now(inode, true);
1696 fuse_set_nowrite(inode);
1697 fuse_release_nowrite(inode);
1701 fuse_set_nowrite(inode);
1702 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1703 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1704 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1707 memset(&inarg, 0, sizeof(inarg));
1708 memset(&outarg, 0, sizeof(outarg));
1709 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1711 struct fuse_file *ff = file->private_data;
1712 inarg.valid |= FATTR_FH;
1715 if (attr->ia_valid & ATTR_SIZE) {
1716 /* For mandatory locking in truncate */
1717 inarg.valid |= FATTR_LOCKOWNER;
1718 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1720 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1721 err = fuse_simple_request(fc, &args);
1724 fuse_invalidate_attr(inode);
1728 if (fuse_invalid_attr(&outarg.attr) ||
1729 (inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1730 fuse_make_bad(inode);
1735 spin_lock(&fc->lock);
1736 /* the kernel maintains i_mtime locally */
1737 if (trust_local_cmtime) {
1738 if (attr->ia_valid & ATTR_MTIME)
1739 inode->i_mtime = attr->ia_mtime;
1740 if (attr->ia_valid & ATTR_CTIME)
1741 inode->i_ctime = attr->ia_ctime;
1742 /* FIXME: clear I_DIRTY_SYNC? */
1745 fuse_change_attributes_common(inode, &outarg.attr,
1746 attr_timeout(&outarg));
1747 oldsize = inode->i_size;
1748 /* see the comment in fuse_change_attributes() */
1749 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1750 i_size_write(inode, outarg.attr.size);
1753 /* NOTE: this may release/reacquire fc->lock */
1754 __fuse_release_nowrite(inode);
1756 spin_unlock(&fc->lock);
1759 * Only call invalidate_inode_pages2() after removing
1760 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1762 if ((is_truncate || !is_wb) &&
1763 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1764 truncate_pagecache(inode, outarg.attr.size);
1765 invalidate_inode_pages2(inode->i_mapping);
1768 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1773 fuse_release_nowrite(inode);
1775 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1779 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1781 struct inode *inode = d_inode(entry);
1782 struct fuse_conn *fc = get_fuse_conn(inode);
1783 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1786 if (fuse_is_bad(inode))
1789 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1792 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1793 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1797 * The only sane way to reliably kill suid/sgid is to do it in
1798 * the userspace filesystem
1800 * This should be done on write(), truncate() and chown().
1802 if (!fc->handle_killpriv) {
1804 * ia_mode calculation may have used stale i_mode.
1805 * Refresh and recalculate.
1807 ret = fuse_do_getattr(inode, NULL, file);
1811 attr->ia_mode = inode->i_mode;
1812 if (inode->i_mode & S_ISUID) {
1813 attr->ia_valid |= ATTR_MODE;
1814 attr->ia_mode &= ~S_ISUID;
1816 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1817 attr->ia_valid |= ATTR_MODE;
1818 attr->ia_mode &= ~S_ISGID;
1822 if (!attr->ia_valid)
1825 ret = fuse_do_setattr(entry, attr, file);
1828 * If filesystem supports acls it may have updated acl xattrs in
1829 * the filesystem, so forget cached acls for the inode.
1832 forget_all_cached_acls(inode);
1834 /* Directory mode changed, may need to revalidate access */
1835 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1836 fuse_invalidate_entry_cache(entry);
1841 static int fuse_getattr(const struct path *path, struct kstat *stat,
1842 u32 request_mask, unsigned int flags)
1844 struct inode *inode = d_inode(path->dentry);
1845 struct fuse_conn *fc = get_fuse_conn(inode);
1847 if (fuse_is_bad(inode))
1850 if (!fuse_allow_current_process(fc))
1853 return fuse_update_get_attr(inode, NULL, stat);
1856 static const struct inode_operations fuse_dir_inode_operations = {
1857 .lookup = fuse_lookup,
1858 .mkdir = fuse_mkdir,
1859 .symlink = fuse_symlink,
1860 .unlink = fuse_unlink,
1861 .rmdir = fuse_rmdir,
1862 .rename = fuse_rename2,
1864 .setattr = fuse_setattr,
1865 .create = fuse_create,
1866 .atomic_open = fuse_atomic_open,
1867 .mknod = fuse_mknod,
1868 .permission = fuse_permission,
1869 .getattr = fuse_getattr,
1870 .listxattr = fuse_listxattr,
1871 .get_acl = fuse_get_acl,
1872 .set_acl = fuse_set_acl,
1875 static const struct file_operations fuse_dir_operations = {
1876 .llseek = generic_file_llseek,
1877 .read = generic_read_dir,
1878 .iterate_shared = fuse_readdir,
1879 .open = fuse_dir_open,
1880 .release = fuse_dir_release,
1881 .fsync = fuse_dir_fsync,
1882 .unlocked_ioctl = fuse_dir_ioctl,
1883 .compat_ioctl = fuse_dir_compat_ioctl,
1886 static const struct inode_operations fuse_common_inode_operations = {
1887 .setattr = fuse_setattr,
1888 .permission = fuse_permission,
1889 .getattr = fuse_getattr,
1890 .listxattr = fuse_listxattr,
1891 .get_acl = fuse_get_acl,
1892 .set_acl = fuse_set_acl,
1895 static const struct inode_operations fuse_symlink_inode_operations = {
1896 .setattr = fuse_setattr,
1897 .get_link = fuse_get_link,
1898 .getattr = fuse_getattr,
1899 .listxattr = fuse_listxattr,
1902 void fuse_init_common(struct inode *inode)
1904 inode->i_op = &fuse_common_inode_operations;
1907 void fuse_init_dir(struct inode *inode)
1909 inode->i_op = &fuse_dir_inode_operations;
1910 inode->i_fop = &fuse_dir_operations;
1913 void fuse_init_symlink(struct inode *inode)
1915 inode->i_op = &fuse_symlink_inode_operations;