2 FUSE: Filesystem in Userspace
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>
17 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
19 struct fuse_conn *fc = get_fuse_conn(dir);
20 struct fuse_inode *fi = get_fuse_inode(dir);
22 if (!fc->do_readdirplus)
24 if (!fc->readdirplus_auto)
26 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
33 static void fuse_advise_use_readdirplus(struct inode *dir)
35 struct fuse_inode *fi = get_fuse_inode(dir);
37 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
40 #if BITS_PER_LONG >= 64
41 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
46 static inline u64 fuse_dentry_time(struct dentry *entry)
52 * On 32 bit archs store the high 32 bits of time in d_fsdata
54 static void fuse_dentry_settime(struct dentry *entry, u64 time)
57 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
60 static u64 fuse_dentry_time(struct dentry *entry)
62 return (u64) entry->d_time +
63 ((u64) (unsigned long) entry->d_fsdata << 32);
68 * FUSE caches dentries and attributes with separate timeout. The
69 * time in jiffies until the dentry/attributes are valid is stored in
70 * dentry->d_time and fuse_inode->i_time respectively.
74 * Calculate the time in jiffies until a dentry/attributes are valid
76 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
79 struct timespec ts = {sec, nsec};
80 return get_jiffies_64() + timespec_to_jiffies(&ts);
86 * Set dentry and possibly attribute timeouts from the lookup/mk*
89 static void fuse_change_entry_timeout(struct dentry *entry,
90 struct fuse_entry_out *o)
92 fuse_dentry_settime(entry,
93 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
96 static u64 attr_timeout(struct fuse_attr_out *o)
98 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
101 static u64 entry_attr_timeout(struct fuse_entry_out *o)
103 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
107 * Mark the attributes as stale, so that at the next call to
108 * ->getattr() they will be fetched from userspace
110 void fuse_invalidate_attr(struct inode *inode)
112 get_fuse_inode(inode)->i_time = 0;
116 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
119 void fuse_invalidate_atime(struct inode *inode)
121 if (!IS_RDONLY(inode))
122 fuse_invalidate_attr(inode);
126 * Just mark the entry as stale, so that a next attempt to look it up
127 * will result in a new lookup call to userspace
129 * This is called when a dentry is about to become negative and the
130 * timeout is unknown (unlink, rmdir, rename and in some cases
133 void fuse_invalidate_entry_cache(struct dentry *entry)
135 fuse_dentry_settime(entry, 0);
139 * Same as fuse_invalidate_entry_cache(), but also try to remove the
140 * dentry from the hash
142 static void fuse_invalidate_entry(struct dentry *entry)
145 fuse_invalidate_entry_cache(entry);
148 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
149 u64 nodeid, struct qstr *name,
150 struct fuse_entry_out *outarg)
152 memset(outarg, 0, sizeof(struct fuse_entry_out));
153 args->in.h.opcode = FUSE_LOOKUP;
154 args->in.h.nodeid = nodeid;
155 args->in.numargs = 1;
156 args->in.args[0].size = name->len + 1;
157 args->in.args[0].value = name->name;
158 args->out.numargs = 1;
160 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
162 args->out.args[0].size = sizeof(struct fuse_entry_out);
163 args->out.args[0].value = outarg;
166 u64 fuse_get_attr_version(struct fuse_conn *fc)
171 * The spin lock isn't actually needed on 64bit archs, but we
172 * don't yet care too much about such optimizations.
174 spin_lock(&fc->lock);
175 curr_version = fc->attr_version;
176 spin_unlock(&fc->lock);
182 * Check whether the dentry is still valid
184 * If the entry validity timeout has expired and the dentry is
185 * positive, try to redo the lookup. If the lookup results in a
186 * different inode, then let the VFS invalidate the dentry and redo
187 * the lookup once more. If the lookup results in the same inode,
188 * then refresh the attributes, timeouts and mark the dentry valid.
190 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
193 struct dentry *parent;
194 struct fuse_conn *fc;
195 struct fuse_inode *fi;
198 inode = ACCESS_ONCE(entry->d_inode);
199 if (inode && is_bad_inode(inode))
201 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
202 (flags & LOOKUP_REVAL)) {
203 struct fuse_entry_out outarg;
205 struct fuse_forget_link *forget;
208 /* For negative dentries, always do a fresh lookup */
213 if (flags & LOOKUP_RCU)
216 fc = get_fuse_conn(inode);
218 forget = fuse_alloc_forget();
223 attr_version = fuse_get_attr_version(fc);
225 parent = dget_parent(entry);
226 fuse_lookup_init(fc, &args, get_node_id(parent->d_inode),
227 &entry->d_name, &outarg);
228 ret = fuse_simple_request(fc, &args);
230 /* Zero nodeid is same as -ENOENT */
231 if (!ret && !outarg.nodeid)
234 fi = get_fuse_inode(inode);
235 if (outarg.nodeid != get_node_id(inode)) {
236 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
239 spin_lock(&fc->lock);
241 spin_unlock(&fc->lock);
246 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
249 fuse_change_attributes(inode, &outarg.attr,
250 entry_attr_timeout(&outarg),
252 fuse_change_entry_timeout(entry, &outarg);
254 fi = get_fuse_inode(inode);
255 if (flags & LOOKUP_RCU) {
256 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
258 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
259 parent = dget_parent(entry);
260 fuse_advise_use_readdirplus(parent->d_inode);
273 static int invalid_nodeid(u64 nodeid)
275 return !nodeid || nodeid == FUSE_ROOT_ID;
278 const struct dentry_operations fuse_dentry_operations = {
279 .d_revalidate = fuse_dentry_revalidate,
282 int fuse_valid_type(int m)
284 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
285 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
288 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
289 struct fuse_entry_out *outarg, struct inode **inode)
291 struct fuse_conn *fc = get_fuse_conn_super(sb);
293 struct fuse_forget_link *forget;
299 if (name->len > FUSE_NAME_MAX)
303 forget = fuse_alloc_forget();
308 attr_version = fuse_get_attr_version(fc);
310 fuse_lookup_init(fc, &args, nodeid, name, outarg);
311 err = fuse_simple_request(fc, &args);
312 /* Zero nodeid is same as -ENOENT, but with valid timeout */
313 if (err || !outarg->nodeid)
319 if (!fuse_valid_type(outarg->attr.mode))
322 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
323 &outarg->attr, entry_attr_timeout(outarg),
327 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
338 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
342 struct fuse_entry_out outarg;
344 struct dentry *newent;
345 bool outarg_valid = true;
347 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
349 if (err == -ENOENT) {
350 outarg_valid = false;
357 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
360 newent = d_splice_alias(inode, entry);
361 err = PTR_ERR(newent);
365 entry = newent ? newent : entry;
367 fuse_change_entry_timeout(entry, &outarg);
369 fuse_invalidate_entry_cache(entry);
371 fuse_advise_use_readdirplus(dir);
381 * Atomic create+open operation
383 * If the filesystem doesn't support this, then fall back to separate
384 * 'mknod' + 'open' requests.
386 static int fuse_create_open(struct inode *dir, struct dentry *entry,
387 struct file *file, unsigned flags,
388 umode_t mode, int *opened)
392 struct fuse_conn *fc = get_fuse_conn(dir);
394 struct fuse_forget_link *forget;
395 struct fuse_create_in inarg;
396 struct fuse_open_out outopen;
397 struct fuse_entry_out outentry;
398 struct fuse_file *ff;
400 /* Userspace expects S_IFREG in create mode */
401 BUG_ON((mode & S_IFMT) != S_IFREG);
403 forget = fuse_alloc_forget();
409 ff = fuse_file_alloc(fc);
411 goto out_put_forget_req;
414 mode &= ~current_umask();
417 memset(&inarg, 0, sizeof(inarg));
418 memset(&outentry, 0, sizeof(outentry));
421 inarg.umask = current_umask();
422 args.in.h.opcode = FUSE_CREATE;
423 args.in.h.nodeid = get_node_id(dir);
425 args.in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
427 args.in.args[0].value = &inarg;
428 args.in.args[1].size = entry->d_name.len + 1;
429 args.in.args[1].value = entry->d_name.name;
430 args.out.numargs = 2;
432 args.out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
434 args.out.args[0].size = sizeof(outentry);
435 args.out.args[0].value = &outentry;
436 args.out.args[1].size = sizeof(outopen);
437 args.out.args[1].value = &outopen;
438 err = fuse_simple_request(fc, &args);
443 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
447 ff->nodeid = outentry.nodeid;
448 ff->open_flags = outopen.open_flags;
449 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
450 &outentry.attr, entry_attr_timeout(&outentry), 0);
452 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
453 fuse_sync_release(ff, flags);
454 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
459 d_instantiate(entry, inode);
460 fuse_change_entry_timeout(entry, &outentry);
461 fuse_invalidate_attr(dir);
462 err = finish_open(file, entry, generic_file_open, opened);
464 fuse_sync_release(ff, flags);
466 file->private_data = fuse_file_get(ff);
467 fuse_finish_open(inode, file);
479 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
480 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
481 struct file *file, unsigned flags,
482 umode_t mode, int *opened)
485 struct fuse_conn *fc = get_fuse_conn(dir);
486 struct dentry *res = NULL;
488 if (d_unhashed(entry)) {
489 res = fuse_lookup(dir, entry, 0);
497 if (!(flags & O_CREAT) || entry->d_inode)
501 *opened |= FILE_CREATED;
506 err = fuse_create_open(dir, entry, file, flags, mode, opened);
507 if (err == -ENOSYS) {
516 err = fuse_mknod(dir, entry, mode, 0);
520 return finish_no_open(file, res);
524 * Code shared between mknod, mkdir, symlink and link
526 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
527 struct inode *dir, struct dentry *entry,
530 struct fuse_entry_out outarg;
533 struct fuse_forget_link *forget;
535 forget = fuse_alloc_forget();
539 memset(&outarg, 0, sizeof(outarg));
540 args->in.h.nodeid = get_node_id(dir);
541 args->out.numargs = 1;
543 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
545 args->out.args[0].size = sizeof(outarg);
546 args->out.args[0].value = &outarg;
547 err = fuse_simple_request(fc, args);
549 goto out_put_forget_req;
552 if (invalid_nodeid(outarg.nodeid))
553 goto out_put_forget_req;
555 if ((outarg.attr.mode ^ mode) & S_IFMT)
556 goto out_put_forget_req;
558 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
559 &outarg.attr, entry_attr_timeout(&outarg), 0);
561 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
566 err = d_instantiate_no_diralias(entry, inode);
570 fuse_change_entry_timeout(entry, &outarg);
571 fuse_invalidate_attr(dir);
579 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
582 struct fuse_mknod_in inarg;
583 struct fuse_conn *fc = get_fuse_conn(dir);
587 mode &= ~current_umask();
589 memset(&inarg, 0, sizeof(inarg));
591 inarg.rdev = new_encode_dev(rdev);
592 inarg.umask = current_umask();
593 args.in.h.opcode = FUSE_MKNOD;
595 args.in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
597 args.in.args[0].value = &inarg;
598 args.in.args[1].size = entry->d_name.len + 1;
599 args.in.args[1].value = entry->d_name.name;
600 return create_new_entry(fc, &args, dir, entry, mode);
603 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
606 return fuse_mknod(dir, entry, mode, 0);
609 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
611 struct fuse_mkdir_in inarg;
612 struct fuse_conn *fc = get_fuse_conn(dir);
616 mode &= ~current_umask();
618 memset(&inarg, 0, sizeof(inarg));
620 inarg.umask = current_umask();
621 args.in.h.opcode = FUSE_MKDIR;
623 args.in.args[0].size = sizeof(inarg);
624 args.in.args[0].value = &inarg;
625 args.in.args[1].size = entry->d_name.len + 1;
626 args.in.args[1].value = entry->d_name.name;
627 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
630 static int fuse_symlink(struct inode *dir, struct dentry *entry,
633 struct fuse_conn *fc = get_fuse_conn(dir);
634 unsigned len = strlen(link) + 1;
637 args.in.h.opcode = FUSE_SYMLINK;
639 args.in.args[0].size = entry->d_name.len + 1;
640 args.in.args[0].value = entry->d_name.name;
641 args.in.args[1].size = len;
642 args.in.args[1].value = link;
643 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
646 static inline void fuse_update_ctime(struct inode *inode)
648 if (!IS_NOCMTIME(inode)) {
649 inode->i_ctime = current_fs_time(inode->i_sb);
650 mark_inode_dirty_sync(inode);
654 static int fuse_unlink(struct inode *dir, struct dentry *entry)
657 struct fuse_conn *fc = get_fuse_conn(dir);
660 args.in.h.opcode = FUSE_UNLINK;
661 args.in.h.nodeid = get_node_id(dir);
663 args.in.args[0].size = entry->d_name.len + 1;
664 args.in.args[0].value = entry->d_name.name;
665 err = fuse_simple_request(fc, &args);
667 struct inode *inode = entry->d_inode;
668 struct fuse_inode *fi = get_fuse_inode(inode);
670 spin_lock(&fc->lock);
671 fi->attr_version = ++fc->attr_version;
673 * If i_nlink == 0 then unlink doesn't make sense, yet this can
674 * happen if userspace filesystem is careless. It would be
675 * difficult to enforce correct nlink usage so just ignore this
678 if (inode->i_nlink > 0)
680 spin_unlock(&fc->lock);
681 fuse_invalidate_attr(inode);
682 fuse_invalidate_attr(dir);
683 fuse_invalidate_entry_cache(entry);
684 fuse_update_ctime(inode);
685 } else if (err == -EINTR)
686 fuse_invalidate_entry(entry);
690 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
693 struct fuse_conn *fc = get_fuse_conn(dir);
696 args.in.h.opcode = FUSE_RMDIR;
697 args.in.h.nodeid = get_node_id(dir);
699 args.in.args[0].size = entry->d_name.len + 1;
700 args.in.args[0].value = entry->d_name.name;
701 err = fuse_simple_request(fc, &args);
703 clear_nlink(entry->d_inode);
704 fuse_invalidate_attr(dir);
705 fuse_invalidate_entry_cache(entry);
706 } else if (err == -EINTR)
707 fuse_invalidate_entry(entry);
711 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
712 struct inode *newdir, struct dentry *newent,
713 unsigned int flags, int opcode, size_t argsize)
716 struct fuse_rename2_in inarg;
717 struct fuse_conn *fc = get_fuse_conn(olddir);
720 memset(&inarg, 0, argsize);
721 inarg.newdir = get_node_id(newdir);
723 args.in.h.opcode = opcode;
724 args.in.h.nodeid = get_node_id(olddir);
726 args.in.args[0].size = argsize;
727 args.in.args[0].value = &inarg;
728 args.in.args[1].size = oldent->d_name.len + 1;
729 args.in.args[1].value = oldent->d_name.name;
730 args.in.args[2].size = newent->d_name.len + 1;
731 args.in.args[2].value = newent->d_name.name;
732 err = fuse_simple_request(fc, &args);
735 fuse_invalidate_attr(oldent->d_inode);
736 fuse_update_ctime(oldent->d_inode);
738 if (flags & RENAME_EXCHANGE) {
739 fuse_invalidate_attr(newent->d_inode);
740 fuse_update_ctime(newent->d_inode);
743 fuse_invalidate_attr(olddir);
744 if (olddir != newdir)
745 fuse_invalidate_attr(newdir);
747 /* newent will end up negative */
748 if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
749 fuse_invalidate_attr(newent->d_inode);
750 fuse_invalidate_entry_cache(newent);
751 fuse_update_ctime(newent->d_inode);
753 } else if (err == -EINTR) {
754 /* If request was interrupted, DEITY only knows if the
755 rename actually took place. If the invalidation
756 fails (e.g. some process has CWD under the renamed
757 directory), then there can be inconsistency between
758 the dcache and the real filesystem. Tough luck. */
759 fuse_invalidate_entry(oldent);
761 fuse_invalidate_entry(newent);
767 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
768 struct inode *newdir, struct dentry *newent,
771 struct fuse_conn *fc = get_fuse_conn(olddir);
774 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
778 if (fc->no_rename2 || fc->minor < 23)
781 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
783 sizeof(struct fuse_rename2_in));
784 if (err == -ENOSYS) {
789 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
791 sizeof(struct fuse_rename_in));
797 static int fuse_link(struct dentry *entry, struct inode *newdir,
798 struct dentry *newent)
801 struct fuse_link_in inarg;
802 struct inode *inode = entry->d_inode;
803 struct fuse_conn *fc = get_fuse_conn(inode);
806 memset(&inarg, 0, sizeof(inarg));
807 inarg.oldnodeid = get_node_id(inode);
808 args.in.h.opcode = FUSE_LINK;
810 args.in.args[0].size = sizeof(inarg);
811 args.in.args[0].value = &inarg;
812 args.in.args[1].size = newent->d_name.len + 1;
813 args.in.args[1].value = newent->d_name.name;
814 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
815 /* Contrary to "normal" filesystems it can happen that link
816 makes two "logical" inodes point to the same "physical"
817 inode. We invalidate the attributes of the old one, so it
818 will reflect changes in the backing inode (link count,
822 struct fuse_inode *fi = get_fuse_inode(inode);
824 spin_lock(&fc->lock);
825 fi->attr_version = ++fc->attr_version;
827 spin_unlock(&fc->lock);
828 fuse_invalidate_attr(inode);
829 fuse_update_ctime(inode);
830 } else if (err == -EINTR) {
831 fuse_invalidate_attr(inode);
836 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
839 unsigned int blkbits;
840 struct fuse_conn *fc = get_fuse_conn(inode);
842 /* see the comment in fuse_change_attributes() */
843 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
844 attr->size = i_size_read(inode);
845 attr->mtime = inode->i_mtime.tv_sec;
846 attr->mtimensec = inode->i_mtime.tv_nsec;
847 attr->ctime = inode->i_ctime.tv_sec;
848 attr->ctimensec = inode->i_ctime.tv_nsec;
851 stat->dev = inode->i_sb->s_dev;
852 stat->ino = attr->ino;
853 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
854 stat->nlink = attr->nlink;
855 stat->uid = make_kuid(&init_user_ns, attr->uid);
856 stat->gid = make_kgid(&init_user_ns, attr->gid);
857 stat->rdev = inode->i_rdev;
858 stat->atime.tv_sec = attr->atime;
859 stat->atime.tv_nsec = attr->atimensec;
860 stat->mtime.tv_sec = attr->mtime;
861 stat->mtime.tv_nsec = attr->mtimensec;
862 stat->ctime.tv_sec = attr->ctime;
863 stat->ctime.tv_nsec = attr->ctimensec;
864 stat->size = attr->size;
865 stat->blocks = attr->blocks;
867 if (attr->blksize != 0)
868 blkbits = ilog2(attr->blksize);
870 blkbits = inode->i_sb->s_blocksize_bits;
872 stat->blksize = 1 << blkbits;
875 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
879 struct fuse_getattr_in inarg;
880 struct fuse_attr_out outarg;
881 struct fuse_conn *fc = get_fuse_conn(inode);
885 attr_version = fuse_get_attr_version(fc);
887 memset(&inarg, 0, sizeof(inarg));
888 memset(&outarg, 0, sizeof(outarg));
889 /* Directories have separate file-handle space */
890 if (file && S_ISREG(inode->i_mode)) {
891 struct fuse_file *ff = file->private_data;
893 inarg.getattr_flags |= FUSE_GETATTR_FH;
896 args.in.h.opcode = FUSE_GETATTR;
897 args.in.h.nodeid = get_node_id(inode);
899 args.in.args[0].size = sizeof(inarg);
900 args.in.args[0].value = &inarg;
901 args.out.numargs = 1;
903 args.out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
905 args.out.args[0].size = sizeof(outarg);
906 args.out.args[0].value = &outarg;
907 err = fuse_simple_request(fc, &args);
909 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
910 make_bad_inode(inode);
913 fuse_change_attributes(inode, &outarg.attr,
914 attr_timeout(&outarg),
917 fuse_fillattr(inode, &outarg.attr, stat);
923 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
924 struct file *file, bool *refreshed)
926 struct fuse_inode *fi = get_fuse_inode(inode);
930 if (time_before64(fi->i_time, get_jiffies_64())) {
932 err = fuse_do_getattr(inode, stat, file);
937 generic_fillattr(inode, stat);
938 stat->mode = fi->orig_i_mode;
939 stat->ino = fi->orig_ino;
943 if (refreshed != NULL)
949 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
950 u64 child_nodeid, struct qstr *name)
953 struct inode *parent;
955 struct dentry *entry;
957 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
961 mutex_lock(&parent->i_mutex);
962 if (!S_ISDIR(parent->i_mode))
966 dir = d_find_alias(parent);
970 entry = d_lookup(dir, name);
975 fuse_invalidate_attr(parent);
976 fuse_invalidate_entry(entry);
978 if (child_nodeid != 0 && entry->d_inode) {
979 mutex_lock(&entry->d_inode->i_mutex);
980 if (get_node_id(entry->d_inode) != child_nodeid) {
984 if (d_mountpoint(entry)) {
988 if (S_ISDIR(entry->d_inode->i_mode)) {
989 shrink_dcache_parent(entry);
990 if (!simple_empty(entry)) {
994 entry->d_inode->i_flags |= S_DEAD;
997 clear_nlink(entry->d_inode);
1000 mutex_unlock(&entry->d_inode->i_mutex);
1009 mutex_unlock(&parent->i_mutex);
1015 * Calling into a user-controlled filesystem gives the filesystem
1016 * daemon ptrace-like capabilities over the current process. This
1017 * means, that the filesystem daemon is able to record the exact
1018 * filesystem operations performed, and can also control the behavior
1019 * of the requester process in otherwise impossible ways. For example
1020 * it can delay the operation for arbitrary length of time allowing
1021 * DoS against the requester.
1023 * For this reason only those processes can call into the filesystem,
1024 * for which the owner of the mount has ptrace privilege. This
1025 * excludes processes started by other users, suid or sgid processes.
1027 int fuse_allow_current_process(struct fuse_conn *fc)
1029 const struct cred *cred;
1031 if (fc->flags & FUSE_ALLOW_OTHER)
1034 cred = current_cred();
1035 if (uid_eq(cred->euid, fc->user_id) &&
1036 uid_eq(cred->suid, fc->user_id) &&
1037 uid_eq(cred->uid, fc->user_id) &&
1038 gid_eq(cred->egid, fc->group_id) &&
1039 gid_eq(cred->sgid, fc->group_id) &&
1040 gid_eq(cred->gid, fc->group_id))
1046 static int fuse_access(struct inode *inode, int mask)
1048 struct fuse_conn *fc = get_fuse_conn(inode);
1050 struct fuse_access_in inarg;
1053 BUG_ON(mask & MAY_NOT_BLOCK);
1058 memset(&inarg, 0, sizeof(inarg));
1059 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1060 args.in.h.opcode = FUSE_ACCESS;
1061 args.in.h.nodeid = get_node_id(inode);
1062 args.in.numargs = 1;
1063 args.in.args[0].size = sizeof(inarg);
1064 args.in.args[0].value = &inarg;
1065 err = fuse_simple_request(fc, &args);
1066 if (err == -ENOSYS) {
1073 static int fuse_perm_getattr(struct inode *inode, int mask)
1075 if (mask & MAY_NOT_BLOCK)
1078 return fuse_do_getattr(inode, NULL, NULL);
1082 * Check permission. The two basic access models of FUSE are:
1084 * 1) Local access checking ('default_permissions' mount option) based
1085 * on file mode. This is the plain old disk filesystem permission
1088 * 2) "Remote" access checking, where server is responsible for
1089 * checking permission in each inode operation. An exception to this
1090 * is if ->permission() was invoked from sys_access() in which case an
1091 * access request is sent. Execute permission is still checked
1092 * locally based on file mode.
1094 static int fuse_permission(struct inode *inode, int mask)
1096 struct fuse_conn *fc = get_fuse_conn(inode);
1097 bool refreshed = false;
1100 if (!fuse_allow_current_process(fc))
1104 * If attributes are needed, refresh them before proceeding
1106 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1107 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1108 struct fuse_inode *fi = get_fuse_inode(inode);
1110 if (time_before64(fi->i_time, get_jiffies_64())) {
1113 err = fuse_perm_getattr(inode, mask);
1119 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1120 err = generic_permission(inode, mask);
1122 /* If permission is denied, try to refresh file
1123 attributes. This is also needed, because the root
1124 node will at first have no permissions */
1125 if (err == -EACCES && !refreshed) {
1126 err = fuse_perm_getattr(inode, mask);
1128 err = generic_permission(inode, mask);
1131 /* Note: the opposite of the above test does not
1132 exist. So if permissions are revoked this won't be
1133 noticed immediately, only after the attribute
1134 timeout has expired */
1135 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1136 err = fuse_access(inode, mask);
1137 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1138 if (!(inode->i_mode & S_IXUGO)) {
1142 err = fuse_perm_getattr(inode, mask);
1143 if (!err && !(inode->i_mode & S_IXUGO))
1150 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1151 struct dir_context *ctx)
1153 while (nbytes >= FUSE_NAME_OFFSET) {
1154 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1155 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1156 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1158 if (reclen > nbytes)
1160 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1163 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1164 dirent->ino, dirent->type))
1169 ctx->pos = dirent->off;
1175 static int fuse_direntplus_link(struct file *file,
1176 struct fuse_direntplus *direntplus,
1180 struct fuse_entry_out *o = &direntplus->entry_out;
1181 struct fuse_dirent *dirent = &direntplus->dirent;
1182 struct dentry *parent = file->f_path.dentry;
1183 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1184 struct dentry *dentry;
1185 struct dentry *alias;
1186 struct inode *dir = parent->d_inode;
1187 struct fuse_conn *fc;
1188 struct inode *inode;
1192 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1193 * ENOENT. Instead, it only means the userspace filesystem did
1194 * not want to return attributes/handle for this entry.
1201 if (name.name[0] == '.') {
1203 * We could potentially refresh the attributes of the directory
1208 if (name.name[1] == '.' && name.len == 2)
1212 if (invalid_nodeid(o->nodeid))
1214 if (!fuse_valid_type(o->attr.mode))
1217 fc = get_fuse_conn(dir);
1219 name.hash = full_name_hash(name.name, name.len);
1220 dentry = d_lookup(parent, &name);
1222 inode = dentry->d_inode;
1225 } else if (get_node_id(inode) != o->nodeid ||
1226 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1227 d_invalidate(dentry);
1228 } else if (is_bad_inode(inode)) {
1232 struct fuse_inode *fi;
1233 fi = get_fuse_inode(inode);
1234 spin_lock(&fc->lock);
1236 spin_unlock(&fc->lock);
1238 fuse_change_attributes(inode, &o->attr,
1239 entry_attr_timeout(o),
1243 * The other branch to 'found' comes via fuse_iget()
1244 * which bumps nlookup inside
1251 dentry = d_alloc(parent, &name);
1256 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1257 &o->attr, entry_attr_timeout(o), attr_version);
1261 alias = d_splice_alias(inode, dentry);
1262 err = PTR_ERR(alias);
1272 if (fc->readdirplus_auto)
1273 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1274 fuse_change_entry_timeout(dentry, o);
1282 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1283 struct dir_context *ctx, u64 attr_version)
1285 struct fuse_direntplus *direntplus;
1286 struct fuse_dirent *dirent;
1291 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1292 direntplus = (struct fuse_direntplus *) buf;
1293 dirent = &direntplus->dirent;
1294 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1296 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1298 if (reclen > nbytes)
1300 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1304 /* We fill entries into dstbuf only as much as
1305 it can hold. But we still continue iterating
1306 over remaining entries to link them. If not,
1307 we need to send a FORGET for each of those
1308 which we did not link.
1310 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1311 dirent->ino, dirent->type);
1312 ctx->pos = dirent->off;
1318 ret = fuse_direntplus_link(file, direntplus, attr_version);
1320 fuse_force_forget(file, direntplus->entry_out.nodeid);
1326 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1331 struct inode *inode = file_inode(file);
1332 struct fuse_conn *fc = get_fuse_conn(inode);
1333 struct fuse_req *req;
1334 u64 attr_version = 0;
1336 if (is_bad_inode(inode))
1339 req = fuse_get_req(fc, 1);
1341 return PTR_ERR(req);
1343 page = alloc_page(GFP_KERNEL);
1345 fuse_put_request(fc, req);
1349 plus = fuse_use_readdirplus(inode, ctx);
1350 req->out.argpages = 1;
1352 req->pages[0] = page;
1353 req->page_descs[0].length = PAGE_SIZE;
1355 attr_version = fuse_get_attr_version(fc);
1356 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1359 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1362 fuse_request_send(fc, req);
1363 nbytes = req->out.args[0].size;
1364 err = req->out.h.error;
1365 fuse_put_request(fc, req);
1368 err = parse_dirplusfile(page_address(page), nbytes,
1372 err = parse_dirfile(page_address(page), nbytes, file,
1378 fuse_invalidate_atime(inode);
1382 static char *read_link(struct dentry *dentry)
1384 struct inode *inode = dentry->d_inode;
1385 struct fuse_conn *fc = get_fuse_conn(inode);
1390 link = (char *) __get_free_page(GFP_KERNEL);
1392 return ERR_PTR(-ENOMEM);
1394 args.in.h.opcode = FUSE_READLINK;
1395 args.in.h.nodeid = get_node_id(inode);
1396 args.out.argvar = 1;
1397 args.out.numargs = 1;
1398 args.out.args[0].size = PAGE_SIZE - 1;
1399 args.out.args[0].value = link;
1400 ret = fuse_simple_request(fc, &args);
1402 free_page((unsigned long) link);
1403 link = ERR_PTR(ret);
1407 fuse_invalidate_atime(inode);
1411 static void free_link(char *link)
1414 free_page((unsigned long) link);
1417 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1419 nd_set_link(nd, read_link(dentry));
1423 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1425 free_link(nd_get_link(nd));
1428 static int fuse_dir_open(struct inode *inode, struct file *file)
1430 return fuse_open_common(inode, file, true);
1433 static int fuse_dir_release(struct inode *inode, struct file *file)
1435 fuse_release_common(file, FUSE_RELEASEDIR);
1440 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1443 return fuse_fsync_common(file, start, end, datasync, 1);
1446 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1449 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1451 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1455 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1458 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1461 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1466 return fuse_ioctl_common(file, cmd, arg,
1467 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1470 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1472 /* Always update if mtime is explicitly set */
1473 if (ivalid & ATTR_MTIME_SET)
1476 /* Or if kernel i_mtime is the official one */
1477 if (trust_local_mtime)
1480 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1481 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1484 /* In all other cases update */
1488 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1489 bool trust_local_cmtime)
1491 unsigned ivalid = iattr->ia_valid;
1493 if (ivalid & ATTR_MODE)
1494 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1495 if (ivalid & ATTR_UID)
1496 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1497 if (ivalid & ATTR_GID)
1498 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1499 if (ivalid & ATTR_SIZE)
1500 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1501 if (ivalid & ATTR_ATIME) {
1502 arg->valid |= FATTR_ATIME;
1503 arg->atime = iattr->ia_atime.tv_sec;
1504 arg->atimensec = iattr->ia_atime.tv_nsec;
1505 if (!(ivalid & ATTR_ATIME_SET))
1506 arg->valid |= FATTR_ATIME_NOW;
1508 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1509 arg->valid |= FATTR_MTIME;
1510 arg->mtime = iattr->ia_mtime.tv_sec;
1511 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1512 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1513 arg->valid |= FATTR_MTIME_NOW;
1515 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1516 arg->valid |= FATTR_CTIME;
1517 arg->ctime = iattr->ia_ctime.tv_sec;
1518 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1523 * Prevent concurrent writepages on inode
1525 * This is done by adding a negative bias to the inode write counter
1526 * and waiting for all pending writes to finish.
1528 void fuse_set_nowrite(struct inode *inode)
1530 struct fuse_conn *fc = get_fuse_conn(inode);
1531 struct fuse_inode *fi = get_fuse_inode(inode);
1533 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1535 spin_lock(&fc->lock);
1536 BUG_ON(fi->writectr < 0);
1537 fi->writectr += FUSE_NOWRITE;
1538 spin_unlock(&fc->lock);
1539 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1543 * Allow writepages on inode
1545 * Remove the bias from the writecounter and send any queued
1548 static void __fuse_release_nowrite(struct inode *inode)
1550 struct fuse_inode *fi = get_fuse_inode(inode);
1552 BUG_ON(fi->writectr != FUSE_NOWRITE);
1554 fuse_flush_writepages(inode);
1557 void fuse_release_nowrite(struct inode *inode)
1559 struct fuse_conn *fc = get_fuse_conn(inode);
1561 spin_lock(&fc->lock);
1562 __fuse_release_nowrite(inode);
1563 spin_unlock(&fc->lock);
1566 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1567 struct inode *inode,
1568 struct fuse_setattr_in *inarg_p,
1569 struct fuse_attr_out *outarg_p)
1571 args->in.h.opcode = FUSE_SETATTR;
1572 args->in.h.nodeid = get_node_id(inode);
1573 args->in.numargs = 1;
1574 args->in.args[0].size = sizeof(*inarg_p);
1575 args->in.args[0].value = inarg_p;
1576 args->out.numargs = 1;
1578 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1580 args->out.args[0].size = sizeof(*outarg_p);
1581 args->out.args[0].value = outarg_p;
1585 * Flush inode->i_mtime to the server
1587 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1589 struct fuse_conn *fc = get_fuse_conn(inode);
1591 struct fuse_setattr_in inarg;
1592 struct fuse_attr_out outarg;
1594 memset(&inarg, 0, sizeof(inarg));
1595 memset(&outarg, 0, sizeof(outarg));
1597 inarg.valid = FATTR_MTIME;
1598 inarg.mtime = inode->i_mtime.tv_sec;
1599 inarg.mtimensec = inode->i_mtime.tv_nsec;
1600 if (fc->minor >= 23) {
1601 inarg.valid |= FATTR_CTIME;
1602 inarg.ctime = inode->i_ctime.tv_sec;
1603 inarg.ctimensec = inode->i_ctime.tv_nsec;
1606 inarg.valid |= FATTR_FH;
1609 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1611 return fuse_simple_request(fc, &args);
1615 * Set attributes, and at the same time refresh them.
1617 * Truncation is slightly complicated, because the 'truncate' request
1618 * may fail, in which case we don't want to touch the mapping.
1619 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1620 * and the actual truncation by hand.
1622 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1625 struct fuse_conn *fc = get_fuse_conn(inode);
1626 struct fuse_inode *fi = get_fuse_inode(inode);
1628 struct fuse_setattr_in inarg;
1629 struct fuse_attr_out outarg;
1630 bool is_truncate = false;
1631 bool is_wb = fc->writeback_cache;
1634 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1636 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1637 attr->ia_valid |= ATTR_FORCE;
1639 err = inode_change_ok(inode, attr);
1643 if (attr->ia_valid & ATTR_OPEN) {
1644 if (fc->atomic_o_trunc)
1649 if (attr->ia_valid & ATTR_SIZE)
1653 fuse_set_nowrite(inode);
1654 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1655 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1656 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1659 memset(&inarg, 0, sizeof(inarg));
1660 memset(&outarg, 0, sizeof(outarg));
1661 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1663 struct fuse_file *ff = file->private_data;
1664 inarg.valid |= FATTR_FH;
1667 if (attr->ia_valid & ATTR_SIZE) {
1668 /* For mandatory locking in truncate */
1669 inarg.valid |= FATTR_LOCKOWNER;
1670 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1672 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1673 err = fuse_simple_request(fc, &args);
1676 fuse_invalidate_attr(inode);
1680 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1681 make_bad_inode(inode);
1686 spin_lock(&fc->lock);
1687 /* the kernel maintains i_mtime locally */
1688 if (trust_local_cmtime) {
1689 if (attr->ia_valid & ATTR_MTIME)
1690 inode->i_mtime = attr->ia_mtime;
1691 if (attr->ia_valid & ATTR_CTIME)
1692 inode->i_ctime = attr->ia_ctime;
1693 /* FIXME: clear I_DIRTY_SYNC? */
1696 fuse_change_attributes_common(inode, &outarg.attr,
1697 attr_timeout(&outarg));
1698 oldsize = inode->i_size;
1699 /* see the comment in fuse_change_attributes() */
1700 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1701 i_size_write(inode, outarg.attr.size);
1704 /* NOTE: this may release/reacquire fc->lock */
1705 __fuse_release_nowrite(inode);
1707 spin_unlock(&fc->lock);
1710 * Only call invalidate_inode_pages2() after removing
1711 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1713 if ((is_truncate || !is_wb) &&
1714 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1715 truncate_pagecache(inode, outarg.attr.size);
1716 invalidate_inode_pages2(inode->i_mapping);
1719 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1724 fuse_release_nowrite(inode);
1726 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1730 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1732 struct inode *inode = entry->d_inode;
1734 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1737 if (attr->ia_valid & ATTR_FILE)
1738 return fuse_do_setattr(inode, attr, attr->ia_file);
1740 return fuse_do_setattr(inode, attr, NULL);
1743 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1746 struct inode *inode = entry->d_inode;
1747 struct fuse_conn *fc = get_fuse_conn(inode);
1749 if (!fuse_allow_current_process(fc))
1752 return fuse_update_attributes(inode, stat, NULL, NULL);
1755 static int fuse_setxattr(struct dentry *entry, const char *name,
1756 const void *value, size_t size, int flags)
1758 struct inode *inode = entry->d_inode;
1759 struct fuse_conn *fc = get_fuse_conn(inode);
1761 struct fuse_setxattr_in inarg;
1764 if (fc->no_setxattr)
1767 memset(&inarg, 0, sizeof(inarg));
1769 inarg.flags = flags;
1770 args.in.h.opcode = FUSE_SETXATTR;
1771 args.in.h.nodeid = get_node_id(inode);
1772 args.in.numargs = 3;
1773 args.in.args[0].size = sizeof(inarg);
1774 args.in.args[0].value = &inarg;
1775 args.in.args[1].size = strlen(name) + 1;
1776 args.in.args[1].value = name;
1777 args.in.args[2].size = size;
1778 args.in.args[2].value = value;
1779 err = fuse_simple_request(fc, &args);
1780 if (err == -ENOSYS) {
1781 fc->no_setxattr = 1;
1785 fuse_invalidate_attr(inode);
1786 fuse_update_ctime(inode);
1791 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1792 void *value, size_t size)
1794 struct inode *inode = entry->d_inode;
1795 struct fuse_conn *fc = get_fuse_conn(inode);
1797 struct fuse_getxattr_in inarg;
1798 struct fuse_getxattr_out outarg;
1801 if (fc->no_getxattr)
1804 memset(&inarg, 0, sizeof(inarg));
1806 args.in.h.opcode = FUSE_GETXATTR;
1807 args.in.h.nodeid = get_node_id(inode);
1808 args.in.numargs = 2;
1809 args.in.args[0].size = sizeof(inarg);
1810 args.in.args[0].value = &inarg;
1811 args.in.args[1].size = strlen(name) + 1;
1812 args.in.args[1].value = name;
1813 /* This is really two different operations rolled into one */
1814 args.out.numargs = 1;
1816 args.out.argvar = 1;
1817 args.out.args[0].size = size;
1818 args.out.args[0].value = value;
1820 args.out.args[0].size = sizeof(outarg);
1821 args.out.args[0].value = &outarg;
1823 ret = fuse_simple_request(fc, &args);
1826 if (ret == -ENOSYS) {
1827 fc->no_getxattr = 1;
1833 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1835 struct inode *inode = entry->d_inode;
1836 struct fuse_conn *fc = get_fuse_conn(inode);
1838 struct fuse_getxattr_in inarg;
1839 struct fuse_getxattr_out outarg;
1842 if (!fuse_allow_current_process(fc))
1845 if (fc->no_listxattr)
1848 memset(&inarg, 0, sizeof(inarg));
1850 args.in.h.opcode = FUSE_LISTXATTR;
1851 args.in.h.nodeid = get_node_id(inode);
1852 args.in.numargs = 1;
1853 args.in.args[0].size = sizeof(inarg);
1854 args.in.args[0].value = &inarg;
1855 /* This is really two different operations rolled into one */
1856 args.out.numargs = 1;
1858 args.out.argvar = 1;
1859 args.out.args[0].size = size;
1860 args.out.args[0].value = list;
1862 args.out.args[0].size = sizeof(outarg);
1863 args.out.args[0].value = &outarg;
1865 ret = fuse_simple_request(fc, &args);
1868 if (ret == -ENOSYS) {
1869 fc->no_listxattr = 1;
1875 static int fuse_removexattr(struct dentry *entry, const char *name)
1877 struct inode *inode = entry->d_inode;
1878 struct fuse_conn *fc = get_fuse_conn(inode);
1882 if (fc->no_removexattr)
1885 args.in.h.opcode = FUSE_REMOVEXATTR;
1886 args.in.h.nodeid = get_node_id(inode);
1887 args.in.numargs = 1;
1888 args.in.args[0].size = strlen(name) + 1;
1889 args.in.args[0].value = name;
1890 err = fuse_simple_request(fc, &args);
1891 if (err == -ENOSYS) {
1892 fc->no_removexattr = 1;
1896 fuse_invalidate_attr(inode);
1897 fuse_update_ctime(inode);
1902 static const struct inode_operations fuse_dir_inode_operations = {
1903 .lookup = fuse_lookup,
1904 .mkdir = fuse_mkdir,
1905 .symlink = fuse_symlink,
1906 .unlink = fuse_unlink,
1907 .rmdir = fuse_rmdir,
1908 .rename2 = fuse_rename2,
1910 .setattr = fuse_setattr,
1911 .create = fuse_create,
1912 .atomic_open = fuse_atomic_open,
1913 .mknod = fuse_mknod,
1914 .permission = fuse_permission,
1915 .getattr = fuse_getattr,
1916 .setxattr = fuse_setxattr,
1917 .getxattr = fuse_getxattr,
1918 .listxattr = fuse_listxattr,
1919 .removexattr = fuse_removexattr,
1922 static const struct file_operations fuse_dir_operations = {
1923 .llseek = generic_file_llseek,
1924 .read = generic_read_dir,
1925 .iterate = fuse_readdir,
1926 .open = fuse_dir_open,
1927 .release = fuse_dir_release,
1928 .fsync = fuse_dir_fsync,
1929 .unlocked_ioctl = fuse_dir_ioctl,
1930 .compat_ioctl = fuse_dir_compat_ioctl,
1933 static const struct inode_operations fuse_common_inode_operations = {
1934 .setattr = fuse_setattr,
1935 .permission = fuse_permission,
1936 .getattr = fuse_getattr,
1937 .setxattr = fuse_setxattr,
1938 .getxattr = fuse_getxattr,
1939 .listxattr = fuse_listxattr,
1940 .removexattr = fuse_removexattr,
1943 static const struct inode_operations fuse_symlink_inode_operations = {
1944 .setattr = fuse_setattr,
1945 .follow_link = fuse_follow_link,
1946 .put_link = fuse_put_link,
1947 .readlink = generic_readlink,
1948 .getattr = fuse_getattr,
1949 .setxattr = fuse_setxattr,
1950 .getxattr = fuse_getxattr,
1951 .listxattr = fuse_listxattr,
1952 .removexattr = fuse_removexattr,
1955 void fuse_init_common(struct inode *inode)
1957 inode->i_op = &fuse_common_inode_operations;
1960 void fuse_init_dir(struct inode *inode)
1962 inode->i_op = &fuse_dir_inode_operations;
1963 inode->i_fop = &fuse_dir_operations;
1966 void fuse_init_symlink(struct inode *inode)
1968 inode->i_op = &fuse_symlink_inode_operations;