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>
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;
159 args->out.args[0].size = sizeof(struct fuse_entry_out);
160 args->out.args[0].value = outarg;
163 u64 fuse_get_attr_version(struct fuse_conn *fc)
168 * The spin lock isn't actually needed on 64bit archs, but we
169 * don't yet care too much about such optimizations.
171 spin_lock(&fc->lock);
172 curr_version = fc->attr_version;
173 spin_unlock(&fc->lock);
179 * Check whether the dentry is still valid
181 * If the entry validity timeout has expired and the dentry is
182 * positive, try to redo the lookup. If the lookup results in a
183 * different inode, then let the VFS invalidate the dentry and redo
184 * the lookup once more. If the lookup results in the same inode,
185 * then refresh the attributes, timeouts and mark the dentry valid.
187 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
190 struct dentry *parent;
191 struct fuse_conn *fc;
192 struct fuse_inode *fi;
195 inode = d_inode_rcu(entry);
196 if (inode && is_bad_inode(inode))
198 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
199 (flags & LOOKUP_REVAL)) {
200 struct fuse_entry_out outarg;
202 struct fuse_forget_link *forget;
205 /* For negative dentries, always do a fresh lookup */
210 if (flags & LOOKUP_RCU)
213 fc = get_fuse_conn(inode);
215 forget = fuse_alloc_forget();
220 attr_version = fuse_get_attr_version(fc);
222 parent = dget_parent(entry);
223 fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
224 &entry->d_name, &outarg);
225 ret = fuse_simple_request(fc, &args);
227 /* Zero nodeid is same as -ENOENT */
228 if (!ret && !outarg.nodeid)
231 fi = get_fuse_inode(inode);
232 if (outarg.nodeid != get_node_id(inode)) {
233 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
236 spin_lock(&fc->lock);
238 spin_unlock(&fc->lock);
243 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
246 fuse_change_attributes(inode, &outarg.attr,
247 entry_attr_timeout(&outarg),
249 fuse_change_entry_timeout(entry, &outarg);
251 fi = get_fuse_inode(inode);
252 if (flags & LOOKUP_RCU) {
253 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
255 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
256 parent = dget_parent(entry);
257 fuse_advise_use_readdirplus(d_inode(parent));
271 * Get the canonical path. Since we must translate to a path, this must be done
272 * in the context of the userspace daemon, however, the userspace daemon cannot
273 * look up paths on its own. Instead, we handle the lookup as a special case
274 * inside of the write request.
276 static void fuse_dentry_canonical_path(const struct path *path, struct path *canonical_path) {
277 struct inode *inode = path->dentry->d_inode;
278 struct fuse_conn *fc = get_fuse_conn(inode);
279 struct fuse_req *req;
283 req = fuse_get_req(fc, 1);
288 path_name = (char*)__get_free_page(GFP_KERNEL);
290 fuse_put_request(fc, req);
294 req->in.h.opcode = FUSE_CANONICAL_PATH;
295 req->in.h.nodeid = get_node_id(inode);
297 req->out.numargs = 1;
298 req->out.args[0].size = PATH_MAX;
299 req->out.args[0].value = path_name;
300 req->canonical_path = canonical_path;
302 fuse_request_send(fc, req);
303 err = req->out.h.error;
304 fuse_put_request(fc, req);
305 free_page((unsigned long)path_name);
309 canonical_path->dentry = path->dentry;
310 canonical_path->mnt = path->mnt;
311 path_get(canonical_path);
314 static int invalid_nodeid(u64 nodeid)
316 return !nodeid || nodeid == FUSE_ROOT_ID;
319 const struct dentry_operations fuse_dentry_operations = {
320 .d_revalidate = fuse_dentry_revalidate,
321 .d_canonical_path = fuse_dentry_canonical_path,
324 int fuse_valid_type(int m)
326 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
327 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
330 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
331 struct fuse_entry_out *outarg, struct inode **inode)
333 struct fuse_conn *fc = get_fuse_conn_super(sb);
335 struct fuse_forget_link *forget;
341 if (name->len > FUSE_NAME_MAX)
345 forget = fuse_alloc_forget();
350 attr_version = fuse_get_attr_version(fc);
352 fuse_lookup_init(fc, &args, nodeid, name, outarg);
353 err = fuse_simple_request(fc, &args);
354 /* Zero nodeid is same as -ENOENT, but with valid timeout */
355 if (err || !outarg->nodeid)
361 if (!fuse_valid_type(outarg->attr.mode))
364 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
365 &outarg->attr, entry_attr_timeout(outarg),
369 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
380 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
384 struct fuse_entry_out outarg;
386 struct dentry *newent;
387 bool outarg_valid = true;
389 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
391 if (err == -ENOENT) {
392 outarg_valid = false;
399 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
402 newent = d_splice_alias(inode, entry);
403 err = PTR_ERR(newent);
407 entry = newent ? newent : entry;
409 fuse_change_entry_timeout(entry, &outarg);
411 fuse_invalidate_entry_cache(entry);
413 fuse_advise_use_readdirplus(dir);
423 * Atomic create+open operation
425 * If the filesystem doesn't support this, then fall back to separate
426 * 'mknod' + 'open' requests.
428 static int fuse_create_open(struct inode *dir, struct dentry *entry,
429 struct file *file, unsigned flags,
430 umode_t mode, int *opened)
434 struct fuse_conn *fc = get_fuse_conn(dir);
436 struct fuse_forget_link *forget;
437 struct fuse_create_in inarg;
438 struct fuse_open_out outopen;
439 struct fuse_entry_out outentry;
440 struct fuse_file *ff;
442 /* Userspace expects S_IFREG in create mode */
443 BUG_ON((mode & S_IFMT) != S_IFREG);
445 forget = fuse_alloc_forget();
451 ff = fuse_file_alloc(fc);
453 goto out_put_forget_req;
456 mode &= ~current_umask();
459 memset(&inarg, 0, sizeof(inarg));
460 memset(&outentry, 0, sizeof(outentry));
463 inarg.umask = current_umask();
464 args.in.h.opcode = FUSE_CREATE;
465 args.in.h.nodeid = get_node_id(dir);
467 args.in.args[0].size = sizeof(inarg);
468 args.in.args[0].value = &inarg;
469 args.in.args[1].size = entry->d_name.len + 1;
470 args.in.args[1].value = entry->d_name.name;
471 args.out.numargs = 2;
472 args.out.args[0].size = sizeof(outentry);
473 args.out.args[0].value = &outentry;
474 args.out.args[1].size = sizeof(outopen);
475 args.out.args[1].value = &outopen;
476 err = fuse_simple_request(fc, &args);
481 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
485 ff->nodeid = outentry.nodeid;
486 ff->open_flags = outopen.open_flags;
487 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
488 &outentry.attr, entry_attr_timeout(&outentry), 0);
490 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
491 fuse_sync_release(ff, flags);
492 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
497 d_instantiate(entry, inode);
498 fuse_change_entry_timeout(entry, &outentry);
499 fuse_invalidate_attr(dir);
500 err = finish_open(file, entry, generic_file_open, opened);
502 fuse_sync_release(ff, flags);
504 file->private_data = fuse_file_get(ff);
505 fuse_finish_open(inode, file);
517 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
518 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
519 struct file *file, unsigned flags,
520 umode_t mode, int *opened)
523 struct fuse_conn *fc = get_fuse_conn(dir);
524 struct dentry *res = NULL;
526 if (d_unhashed(entry)) {
527 res = fuse_lookup(dir, entry, 0);
535 if (!(flags & O_CREAT) || d_really_is_positive(entry))
539 *opened |= FILE_CREATED;
544 err = fuse_create_open(dir, entry, file, flags, mode, opened);
545 if (err == -ENOSYS) {
554 err = fuse_mknod(dir, entry, mode, 0);
558 return finish_no_open(file, res);
562 * Code shared between mknod, mkdir, symlink and link
564 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
565 struct inode *dir, struct dentry *entry,
568 struct fuse_entry_out outarg;
571 struct fuse_forget_link *forget;
573 forget = fuse_alloc_forget();
577 memset(&outarg, 0, sizeof(outarg));
578 args->in.h.nodeid = get_node_id(dir);
579 args->out.numargs = 1;
580 args->out.args[0].size = sizeof(outarg);
581 args->out.args[0].value = &outarg;
582 err = fuse_simple_request(fc, args);
584 goto out_put_forget_req;
587 if (invalid_nodeid(outarg.nodeid))
588 goto out_put_forget_req;
590 if ((outarg.attr.mode ^ mode) & S_IFMT)
591 goto out_put_forget_req;
593 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
594 &outarg.attr, entry_attr_timeout(&outarg), 0);
596 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
601 err = d_instantiate_no_diralias(entry, inode);
605 fuse_change_entry_timeout(entry, &outarg);
606 fuse_invalidate_attr(dir);
614 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
617 struct fuse_mknod_in inarg;
618 struct fuse_conn *fc = get_fuse_conn(dir);
622 mode &= ~current_umask();
624 memset(&inarg, 0, sizeof(inarg));
626 inarg.rdev = new_encode_dev(rdev);
627 inarg.umask = current_umask();
628 args.in.h.opcode = FUSE_MKNOD;
630 args.in.args[0].size = sizeof(inarg);
631 args.in.args[0].value = &inarg;
632 args.in.args[1].size = entry->d_name.len + 1;
633 args.in.args[1].value = entry->d_name.name;
634 return create_new_entry(fc, &args, dir, entry, mode);
637 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
640 return fuse_mknod(dir, entry, mode, 0);
643 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
645 struct fuse_mkdir_in inarg;
646 struct fuse_conn *fc = get_fuse_conn(dir);
650 mode &= ~current_umask();
652 memset(&inarg, 0, sizeof(inarg));
654 inarg.umask = current_umask();
655 args.in.h.opcode = FUSE_MKDIR;
657 args.in.args[0].size = sizeof(inarg);
658 args.in.args[0].value = &inarg;
659 args.in.args[1].size = entry->d_name.len + 1;
660 args.in.args[1].value = entry->d_name.name;
661 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
664 static int fuse_symlink(struct inode *dir, struct dentry *entry,
667 struct fuse_conn *fc = get_fuse_conn(dir);
668 unsigned len = strlen(link) + 1;
671 args.in.h.opcode = FUSE_SYMLINK;
673 args.in.args[0].size = entry->d_name.len + 1;
674 args.in.args[0].value = entry->d_name.name;
675 args.in.args[1].size = len;
676 args.in.args[1].value = link;
677 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
680 static inline void fuse_update_ctime(struct inode *inode)
682 if (!IS_NOCMTIME(inode)) {
683 inode->i_ctime = current_fs_time(inode->i_sb);
684 mark_inode_dirty_sync(inode);
688 static int fuse_unlink(struct inode *dir, struct dentry *entry)
691 struct fuse_conn *fc = get_fuse_conn(dir);
694 args.in.h.opcode = FUSE_UNLINK;
695 args.in.h.nodeid = get_node_id(dir);
697 args.in.args[0].size = entry->d_name.len + 1;
698 args.in.args[0].value = entry->d_name.name;
699 err = fuse_simple_request(fc, &args);
701 struct inode *inode = d_inode(entry);
702 struct fuse_inode *fi = get_fuse_inode(inode);
704 spin_lock(&fc->lock);
705 fi->attr_version = ++fc->attr_version;
707 * If i_nlink == 0 then unlink doesn't make sense, yet this can
708 * happen if userspace filesystem is careless. It would be
709 * difficult to enforce correct nlink usage so just ignore this
712 if (inode->i_nlink > 0)
714 spin_unlock(&fc->lock);
715 fuse_invalidate_attr(inode);
716 fuse_invalidate_attr(dir);
717 fuse_invalidate_entry_cache(entry);
718 fuse_update_ctime(inode);
719 } else if (err == -EINTR)
720 fuse_invalidate_entry(entry);
724 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
727 struct fuse_conn *fc = get_fuse_conn(dir);
730 args.in.h.opcode = FUSE_RMDIR;
731 args.in.h.nodeid = get_node_id(dir);
733 args.in.args[0].size = entry->d_name.len + 1;
734 args.in.args[0].value = entry->d_name.name;
735 err = fuse_simple_request(fc, &args);
737 clear_nlink(d_inode(entry));
738 fuse_invalidate_attr(dir);
739 fuse_invalidate_entry_cache(entry);
740 } else if (err == -EINTR)
741 fuse_invalidate_entry(entry);
745 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
746 struct inode *newdir, struct dentry *newent,
747 unsigned int flags, int opcode, size_t argsize)
750 struct fuse_rename2_in inarg;
751 struct fuse_conn *fc = get_fuse_conn(olddir);
754 memset(&inarg, 0, argsize);
755 inarg.newdir = get_node_id(newdir);
757 args.in.h.opcode = opcode;
758 args.in.h.nodeid = get_node_id(olddir);
760 args.in.args[0].size = argsize;
761 args.in.args[0].value = &inarg;
762 args.in.args[1].size = oldent->d_name.len + 1;
763 args.in.args[1].value = oldent->d_name.name;
764 args.in.args[2].size = newent->d_name.len + 1;
765 args.in.args[2].value = newent->d_name.name;
766 err = fuse_simple_request(fc, &args);
769 fuse_invalidate_attr(d_inode(oldent));
770 fuse_update_ctime(d_inode(oldent));
772 if (flags & RENAME_EXCHANGE) {
773 fuse_invalidate_attr(d_inode(newent));
774 fuse_update_ctime(d_inode(newent));
777 fuse_invalidate_attr(olddir);
778 if (olddir != newdir)
779 fuse_invalidate_attr(newdir);
781 /* newent will end up negative */
782 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
783 fuse_invalidate_attr(d_inode(newent));
784 fuse_invalidate_entry_cache(newent);
785 fuse_update_ctime(d_inode(newent));
787 } else if (err == -EINTR) {
788 /* If request was interrupted, DEITY only knows if the
789 rename actually took place. If the invalidation
790 fails (e.g. some process has CWD under the renamed
791 directory), then there can be inconsistency between
792 the dcache and the real filesystem. Tough luck. */
793 fuse_invalidate_entry(oldent);
794 if (d_really_is_positive(newent))
795 fuse_invalidate_entry(newent);
801 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
802 struct inode *newdir, struct dentry *newent,
805 struct fuse_conn *fc = get_fuse_conn(olddir);
808 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
812 if (fc->no_rename2 || fc->minor < 23)
815 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
817 sizeof(struct fuse_rename2_in));
818 if (err == -ENOSYS) {
823 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
825 sizeof(struct fuse_rename_in));
831 static int fuse_link(struct dentry *entry, struct inode *newdir,
832 struct dentry *newent)
835 struct fuse_link_in inarg;
836 struct inode *inode = d_inode(entry);
837 struct fuse_conn *fc = get_fuse_conn(inode);
840 memset(&inarg, 0, sizeof(inarg));
841 inarg.oldnodeid = get_node_id(inode);
842 args.in.h.opcode = FUSE_LINK;
844 args.in.args[0].size = sizeof(inarg);
845 args.in.args[0].value = &inarg;
846 args.in.args[1].size = newent->d_name.len + 1;
847 args.in.args[1].value = newent->d_name.name;
848 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
849 /* Contrary to "normal" filesystems it can happen that link
850 makes two "logical" inodes point to the same "physical"
851 inode. We invalidate the attributes of the old one, so it
852 will reflect changes in the backing inode (link count,
856 struct fuse_inode *fi = get_fuse_inode(inode);
858 spin_lock(&fc->lock);
859 fi->attr_version = ++fc->attr_version;
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 ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
941 make_bad_inode(inode);
944 fuse_change_attributes(inode, &outarg.attr,
945 attr_timeout(&outarg),
948 fuse_fillattr(inode, &outarg.attr, stat);
954 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
955 struct file *file, bool *refreshed)
957 struct fuse_inode *fi = get_fuse_inode(inode);
961 if (time_before64(fi->i_time, get_jiffies_64())) {
963 err = fuse_do_getattr(inode, stat, file);
968 generic_fillattr(inode, stat);
969 stat->mode = fi->orig_i_mode;
970 stat->ino = fi->orig_ino;
974 if (refreshed != NULL)
980 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
981 u64 child_nodeid, struct qstr *name)
984 struct inode *parent;
986 struct dentry *entry;
988 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
992 mutex_lock(&parent->i_mutex);
993 if (!S_ISDIR(parent->i_mode))
997 dir = d_find_alias(parent);
1001 entry = d_lookup(dir, name);
1006 fuse_invalidate_attr(parent);
1007 fuse_invalidate_entry(entry);
1009 if (child_nodeid != 0 && d_really_is_positive(entry)) {
1010 mutex_lock(&d_inode(entry)->i_mutex);
1011 if (get_node_id(d_inode(entry)) != child_nodeid) {
1015 if (d_mountpoint(entry)) {
1019 if (d_is_dir(entry)) {
1020 shrink_dcache_parent(entry);
1021 if (!simple_empty(entry)) {
1025 d_inode(entry)->i_flags |= S_DEAD;
1028 clear_nlink(d_inode(entry));
1031 mutex_unlock(&d_inode(entry)->i_mutex);
1040 mutex_unlock(&parent->i_mutex);
1046 * Calling into a user-controlled filesystem gives the filesystem
1047 * daemon ptrace-like capabilities over the current process. This
1048 * means, that the filesystem daemon is able to record the exact
1049 * filesystem operations performed, and can also control the behavior
1050 * of the requester process in otherwise impossible ways. For example
1051 * it can delay the operation for arbitrary length of time allowing
1052 * DoS against the requester.
1054 * For this reason only those processes can call into the filesystem,
1055 * for which the owner of the mount has ptrace privilege. This
1056 * excludes processes started by other users, suid or sgid processes.
1058 int fuse_allow_current_process(struct fuse_conn *fc)
1060 const struct cred *cred;
1062 if (fc->flags & FUSE_ALLOW_OTHER)
1065 cred = current_cred();
1066 if (uid_eq(cred->euid, fc->user_id) &&
1067 uid_eq(cred->suid, fc->user_id) &&
1068 uid_eq(cred->uid, fc->user_id) &&
1069 gid_eq(cred->egid, fc->group_id) &&
1070 gid_eq(cred->sgid, fc->group_id) &&
1071 gid_eq(cred->gid, fc->group_id))
1077 static int fuse_access(struct inode *inode, int mask)
1079 struct fuse_conn *fc = get_fuse_conn(inode);
1081 struct fuse_access_in inarg;
1084 BUG_ON(mask & MAY_NOT_BLOCK);
1089 memset(&inarg, 0, sizeof(inarg));
1090 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1091 args.in.h.opcode = FUSE_ACCESS;
1092 args.in.h.nodeid = get_node_id(inode);
1093 args.in.numargs = 1;
1094 args.in.args[0].size = sizeof(inarg);
1095 args.in.args[0].value = &inarg;
1096 err = fuse_simple_request(fc, &args);
1097 if (err == -ENOSYS) {
1104 static int fuse_perm_getattr(struct inode *inode, int mask)
1106 if (mask & MAY_NOT_BLOCK)
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_allow_current_process(fc))
1135 * If attributes are needed, refresh them before proceeding
1137 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1138 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1139 struct fuse_inode *fi = get_fuse_inode(inode);
1141 if (time_before64(fi->i_time, get_jiffies_64())) {
1144 err = fuse_perm_getattr(inode, mask);
1150 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1151 err = generic_permission(inode, mask);
1153 /* If permission is denied, try to refresh file
1154 attributes. This is also needed, because the root
1155 node will at first have no permissions */
1156 if (err == -EACCES && !refreshed) {
1157 err = fuse_perm_getattr(inode, mask);
1159 err = generic_permission(inode, mask);
1162 /* Note: the opposite of the above test does not
1163 exist. So if permissions are revoked this won't be
1164 noticed immediately, only after the attribute
1165 timeout has expired */
1166 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1167 err = fuse_access(inode, mask);
1168 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1169 if (!(inode->i_mode & S_IXUGO)) {
1173 err = fuse_perm_getattr(inode, mask);
1174 if (!err && !(inode->i_mode & S_IXUGO))
1181 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1182 struct dir_context *ctx)
1184 while (nbytes >= FUSE_NAME_OFFSET) {
1185 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1186 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1187 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1189 if (reclen > nbytes)
1191 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1194 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1195 dirent->ino, dirent->type))
1200 ctx->pos = dirent->off;
1206 static int fuse_direntplus_link(struct file *file,
1207 struct fuse_direntplus *direntplus,
1211 struct fuse_entry_out *o = &direntplus->entry_out;
1212 struct fuse_dirent *dirent = &direntplus->dirent;
1213 struct dentry *parent = file->f_path.dentry;
1214 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1215 struct dentry *dentry;
1216 struct dentry *alias;
1217 struct inode *dir = d_inode(parent);
1218 struct fuse_conn *fc;
1219 struct inode *inode;
1223 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1224 * ENOENT. Instead, it only means the userspace filesystem did
1225 * not want to return attributes/handle for this entry.
1232 if (name.name[0] == '.') {
1234 * We could potentially refresh the attributes of the directory
1239 if (name.name[1] == '.' && name.len == 2)
1243 if (invalid_nodeid(o->nodeid))
1245 if (!fuse_valid_type(o->attr.mode))
1248 fc = get_fuse_conn(dir);
1250 name.hash = full_name_hash(name.name, name.len);
1251 dentry = d_lookup(parent, &name);
1253 inode = d_inode(dentry);
1256 } else if (get_node_id(inode) != o->nodeid ||
1257 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1258 d_invalidate(dentry);
1259 } else if (is_bad_inode(inode)) {
1263 struct fuse_inode *fi;
1264 fi = get_fuse_inode(inode);
1265 spin_lock(&fc->lock);
1267 spin_unlock(&fc->lock);
1269 fuse_change_attributes(inode, &o->attr,
1270 entry_attr_timeout(o),
1274 * The other branch to 'found' comes via fuse_iget()
1275 * which bumps nlookup inside
1282 dentry = d_alloc(parent, &name);
1287 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1288 &o->attr, entry_attr_timeout(o), attr_version);
1292 alias = d_splice_alias(inode, dentry);
1293 err = PTR_ERR(alias);
1303 if (fc->readdirplus_auto)
1304 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1305 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);
1343 ctx->pos = dirent->off;
1349 ret = fuse_direntplus_link(file, direntplus, attr_version);
1351 fuse_force_forget(file, direntplus->entry_out.nodeid);
1357 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1362 struct inode *inode = file_inode(file);
1363 struct fuse_conn *fc = get_fuse_conn(inode);
1364 struct fuse_req *req;
1365 u64 attr_version = 0;
1367 if (is_bad_inode(inode))
1370 req = fuse_get_req(fc, 1);
1372 return PTR_ERR(req);
1374 page = alloc_page(GFP_KERNEL);
1376 fuse_put_request(fc, req);
1380 plus = fuse_use_readdirplus(inode, ctx);
1381 req->out.argpages = 1;
1383 req->pages[0] = page;
1384 req->page_descs[0].length = PAGE_SIZE;
1386 attr_version = fuse_get_attr_version(fc);
1387 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1390 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1393 fuse_request_send(fc, req);
1394 nbytes = req->out.args[0].size;
1395 err = req->out.h.error;
1396 fuse_put_request(fc, req);
1399 err = parse_dirplusfile(page_address(page), nbytes,
1403 err = parse_dirfile(page_address(page), nbytes, file,
1409 fuse_invalidate_atime(inode);
1413 static const char *fuse_follow_link(struct dentry *dentry, void **cookie)
1415 struct inode *inode = d_inode(dentry);
1416 struct fuse_conn *fc = get_fuse_conn(inode);
1421 link = (char *) __get_free_page(GFP_KERNEL);
1423 return ERR_PTR(-ENOMEM);
1425 args.in.h.opcode = FUSE_READLINK;
1426 args.in.h.nodeid = get_node_id(inode);
1427 args.out.argvar = 1;
1428 args.out.numargs = 1;
1429 args.out.args[0].size = PAGE_SIZE - 1;
1430 args.out.args[0].value = link;
1431 ret = fuse_simple_request(fc, &args);
1433 free_page((unsigned long) link);
1434 link = ERR_PTR(ret);
1439 fuse_invalidate_atime(inode);
1443 static int fuse_dir_open(struct inode *inode, struct file *file)
1445 return fuse_open_common(inode, file, true);
1448 static int fuse_dir_release(struct inode *inode, struct file *file)
1450 fuse_release_common(file, FUSE_RELEASEDIR);
1455 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1458 return fuse_fsync_common(file, start, end, datasync, 1);
1461 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1464 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1466 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1470 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1473 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1476 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1481 return fuse_ioctl_common(file, cmd, arg,
1482 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1485 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1487 /* Always update if mtime is explicitly set */
1488 if (ivalid & ATTR_MTIME_SET)
1491 /* Or if kernel i_mtime is the official one */
1492 if (trust_local_mtime)
1495 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1496 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1499 /* In all other cases update */
1503 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1504 bool trust_local_cmtime)
1506 unsigned ivalid = iattr->ia_valid;
1508 if (ivalid & ATTR_MODE)
1509 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1510 if (ivalid & ATTR_UID)
1511 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1512 if (ivalid & ATTR_GID)
1513 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1514 if (ivalid & ATTR_SIZE)
1515 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1516 if (ivalid & ATTR_ATIME) {
1517 arg->valid |= FATTR_ATIME;
1518 arg->atime = iattr->ia_atime.tv_sec;
1519 arg->atimensec = iattr->ia_atime.tv_nsec;
1520 if (!(ivalid & ATTR_ATIME_SET))
1521 arg->valid |= FATTR_ATIME_NOW;
1523 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1524 arg->valid |= FATTR_MTIME;
1525 arg->mtime = iattr->ia_mtime.tv_sec;
1526 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1527 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1528 arg->valid |= FATTR_MTIME_NOW;
1530 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1531 arg->valid |= FATTR_CTIME;
1532 arg->ctime = iattr->ia_ctime.tv_sec;
1533 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1538 * Prevent concurrent writepages on inode
1540 * This is done by adding a negative bias to the inode write counter
1541 * and waiting for all pending writes to finish.
1543 void fuse_set_nowrite(struct inode *inode)
1545 struct fuse_conn *fc = get_fuse_conn(inode);
1546 struct fuse_inode *fi = get_fuse_inode(inode);
1548 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1550 spin_lock(&fc->lock);
1551 BUG_ON(fi->writectr < 0);
1552 fi->writectr += FUSE_NOWRITE;
1553 spin_unlock(&fc->lock);
1554 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1558 * Allow writepages on inode
1560 * Remove the bias from the writecounter and send any queued
1563 static void __fuse_release_nowrite(struct inode *inode)
1565 struct fuse_inode *fi = get_fuse_inode(inode);
1567 BUG_ON(fi->writectr != FUSE_NOWRITE);
1569 fuse_flush_writepages(inode);
1572 void fuse_release_nowrite(struct inode *inode)
1574 struct fuse_conn *fc = get_fuse_conn(inode);
1576 spin_lock(&fc->lock);
1577 __fuse_release_nowrite(inode);
1578 spin_unlock(&fc->lock);
1581 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1582 struct inode *inode,
1583 struct fuse_setattr_in *inarg_p,
1584 struct fuse_attr_out *outarg_p)
1586 args->in.h.opcode = FUSE_SETATTR;
1587 args->in.h.nodeid = get_node_id(inode);
1588 args->in.numargs = 1;
1589 args->in.args[0].size = sizeof(*inarg_p);
1590 args->in.args[0].value = inarg_p;
1591 args->out.numargs = 1;
1592 args->out.args[0].size = sizeof(*outarg_p);
1593 args->out.args[0].value = outarg_p;
1597 * Flush inode->i_mtime to the server
1599 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1601 struct fuse_conn *fc = get_fuse_conn(inode);
1603 struct fuse_setattr_in inarg;
1604 struct fuse_attr_out outarg;
1606 memset(&inarg, 0, sizeof(inarg));
1607 memset(&outarg, 0, sizeof(outarg));
1609 inarg.valid = FATTR_MTIME;
1610 inarg.mtime = inode->i_mtime.tv_sec;
1611 inarg.mtimensec = inode->i_mtime.tv_nsec;
1612 if (fc->minor >= 23) {
1613 inarg.valid |= FATTR_CTIME;
1614 inarg.ctime = inode->i_ctime.tv_sec;
1615 inarg.ctimensec = inode->i_ctime.tv_nsec;
1618 inarg.valid |= FATTR_FH;
1621 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1623 return fuse_simple_request(fc, &args);
1627 * Set attributes, and at the same time refresh them.
1629 * Truncation is slightly complicated, because the 'truncate' request
1630 * may fail, in which case we don't want to touch the mapping.
1631 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1632 * and the actual truncation by hand.
1634 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1637 struct fuse_conn *fc = get_fuse_conn(inode);
1638 struct fuse_inode *fi = get_fuse_inode(inode);
1640 struct fuse_setattr_in inarg;
1641 struct fuse_attr_out outarg;
1642 bool is_truncate = false;
1643 bool is_wb = fc->writeback_cache;
1646 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1648 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1649 attr->ia_valid |= ATTR_FORCE;
1651 err = inode_change_ok(inode, attr);
1655 if (attr->ia_valid & ATTR_OPEN) {
1656 if (fc->atomic_o_trunc)
1661 if (attr->ia_valid & ATTR_SIZE)
1665 fuse_set_nowrite(inode);
1666 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1667 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1668 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1671 memset(&inarg, 0, sizeof(inarg));
1672 memset(&outarg, 0, sizeof(outarg));
1673 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1675 struct fuse_file *ff = file->private_data;
1676 inarg.valid |= FATTR_FH;
1679 if (attr->ia_valid & ATTR_SIZE) {
1680 /* For mandatory locking in truncate */
1681 inarg.valid |= FATTR_LOCKOWNER;
1682 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1684 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1685 err = fuse_simple_request(fc, &args);
1688 fuse_invalidate_attr(inode);
1692 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1693 make_bad_inode(inode);
1698 spin_lock(&fc->lock);
1699 /* the kernel maintains i_mtime locally */
1700 if (trust_local_cmtime) {
1701 if (attr->ia_valid & ATTR_MTIME)
1702 inode->i_mtime = attr->ia_mtime;
1703 if (attr->ia_valid & ATTR_CTIME)
1704 inode->i_ctime = attr->ia_ctime;
1705 /* FIXME: clear I_DIRTY_SYNC? */
1708 fuse_change_attributes_common(inode, &outarg.attr,
1709 attr_timeout(&outarg));
1710 oldsize = inode->i_size;
1711 /* see the comment in fuse_change_attributes() */
1712 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1713 i_size_write(inode, outarg.attr.size);
1716 /* NOTE: this may release/reacquire fc->lock */
1717 __fuse_release_nowrite(inode);
1719 spin_unlock(&fc->lock);
1722 * Only call invalidate_inode_pages2() after removing
1723 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1725 if ((is_truncate || !is_wb) &&
1726 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1727 truncate_pagecache(inode, outarg.attr.size);
1728 invalidate_inode_pages2(inode->i_mapping);
1731 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1736 fuse_release_nowrite(inode);
1738 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1742 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1744 struct inode *inode = d_inode(entry);
1745 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1748 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1751 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1754 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1757 * ia_mode calculation may have used stale i_mode. Refresh and
1760 ret = fuse_do_getattr(inode, NULL, file);
1764 attr->ia_mode = inode->i_mode;
1765 kill = should_remove_suid(entry);
1766 if (kill & ATTR_KILL_SUID) {
1767 attr->ia_valid |= ATTR_MODE;
1768 attr->ia_mode &= ~S_ISUID;
1770 if (kill & ATTR_KILL_SGID) {
1771 attr->ia_valid |= ATTR_MODE;
1772 attr->ia_mode &= ~S_ISGID;
1775 if (!attr->ia_valid)
1778 ret = fuse_do_setattr(inode, attr, file);
1780 /* Directory mode changed, may need to revalidate access */
1781 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1782 fuse_invalidate_entry_cache(entry);
1787 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1790 struct inode *inode = d_inode(entry);
1791 struct fuse_conn *fc = get_fuse_conn(inode);
1793 if (!fuse_allow_current_process(fc))
1796 return fuse_update_attributes(inode, stat, NULL, NULL);
1799 static int fuse_setxattr(struct dentry *entry, const char *name,
1800 const void *value, size_t size, int flags)
1802 struct inode *inode = d_inode(entry);
1803 struct fuse_conn *fc = get_fuse_conn(inode);
1805 struct fuse_setxattr_in inarg;
1808 if (fc->no_setxattr)
1811 memset(&inarg, 0, sizeof(inarg));
1813 inarg.flags = flags;
1814 args.in.h.opcode = FUSE_SETXATTR;
1815 args.in.h.nodeid = get_node_id(inode);
1816 args.in.numargs = 3;
1817 args.in.args[0].size = sizeof(inarg);
1818 args.in.args[0].value = &inarg;
1819 args.in.args[1].size = strlen(name) + 1;
1820 args.in.args[1].value = name;
1821 args.in.args[2].size = size;
1822 args.in.args[2].value = value;
1823 err = fuse_simple_request(fc, &args);
1824 if (err == -ENOSYS) {
1825 fc->no_setxattr = 1;
1829 fuse_invalidate_attr(inode);
1830 fuse_update_ctime(inode);
1835 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1836 void *value, size_t size)
1838 struct inode *inode = d_inode(entry);
1839 struct fuse_conn *fc = get_fuse_conn(inode);
1841 struct fuse_getxattr_in inarg;
1842 struct fuse_getxattr_out outarg;
1845 if (fc->no_getxattr)
1848 memset(&inarg, 0, sizeof(inarg));
1850 args.in.h.opcode = FUSE_GETXATTR;
1851 args.in.h.nodeid = get_node_id(inode);
1852 args.in.numargs = 2;
1853 args.in.args[0].size = sizeof(inarg);
1854 args.in.args[0].value = &inarg;
1855 args.in.args[1].size = strlen(name) + 1;
1856 args.in.args[1].value = name;
1857 /* This is really two different operations rolled into one */
1858 args.out.numargs = 1;
1860 args.out.argvar = 1;
1861 args.out.args[0].size = size;
1862 args.out.args[0].value = value;
1864 args.out.args[0].size = sizeof(outarg);
1865 args.out.args[0].value = &outarg;
1867 ret = fuse_simple_request(fc, &args);
1870 if (ret == -ENOSYS) {
1871 fc->no_getxattr = 1;
1877 static int fuse_verify_xattr_list(char *list, size_t size)
1879 size_t origsize = size;
1882 size_t thislen = strnlen(list, size);
1884 if (!thislen || thislen == size)
1887 size -= thislen + 1;
1888 list += thislen + 1;
1894 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1896 struct inode *inode = d_inode(entry);
1897 struct fuse_conn *fc = get_fuse_conn(inode);
1899 struct fuse_getxattr_in inarg;
1900 struct fuse_getxattr_out outarg;
1903 if (!fuse_allow_current_process(fc))
1906 if (fc->no_listxattr)
1909 memset(&inarg, 0, sizeof(inarg));
1911 args.in.h.opcode = FUSE_LISTXATTR;
1912 args.in.h.nodeid = get_node_id(inode);
1913 args.in.numargs = 1;
1914 args.in.args[0].size = sizeof(inarg);
1915 args.in.args[0].value = &inarg;
1916 /* This is really two different operations rolled into one */
1917 args.out.numargs = 1;
1919 args.out.argvar = 1;
1920 args.out.args[0].size = size;
1921 args.out.args[0].value = list;
1923 args.out.args[0].size = sizeof(outarg);
1924 args.out.args[0].value = &outarg;
1926 ret = fuse_simple_request(fc, &args);
1929 if (ret > 0 && size)
1930 ret = fuse_verify_xattr_list(list, ret);
1931 if (ret == -ENOSYS) {
1932 fc->no_listxattr = 1;
1938 static int fuse_removexattr(struct dentry *entry, const char *name)
1940 struct inode *inode = d_inode(entry);
1941 struct fuse_conn *fc = get_fuse_conn(inode);
1945 if (fc->no_removexattr)
1948 args.in.h.opcode = FUSE_REMOVEXATTR;
1949 args.in.h.nodeid = get_node_id(inode);
1950 args.in.numargs = 1;
1951 args.in.args[0].size = strlen(name) + 1;
1952 args.in.args[0].value = name;
1953 err = fuse_simple_request(fc, &args);
1954 if (err == -ENOSYS) {
1955 fc->no_removexattr = 1;
1959 fuse_invalidate_attr(inode);
1960 fuse_update_ctime(inode);
1965 static const struct inode_operations fuse_dir_inode_operations = {
1966 .lookup = fuse_lookup,
1967 .mkdir = fuse_mkdir,
1968 .symlink = fuse_symlink,
1969 .unlink = fuse_unlink,
1970 .rmdir = fuse_rmdir,
1971 .rename2 = fuse_rename2,
1973 .setattr = fuse_setattr,
1974 .create = fuse_create,
1975 .atomic_open = fuse_atomic_open,
1976 .mknod = fuse_mknod,
1977 .permission = fuse_permission,
1978 .getattr = fuse_getattr,
1979 .setxattr = fuse_setxattr,
1980 .getxattr = fuse_getxattr,
1981 .listxattr = fuse_listxattr,
1982 .removexattr = fuse_removexattr,
1985 static const struct file_operations fuse_dir_operations = {
1986 .llseek = generic_file_llseek,
1987 .read = generic_read_dir,
1988 .iterate = fuse_readdir,
1989 .open = fuse_dir_open,
1990 .release = fuse_dir_release,
1991 .fsync = fuse_dir_fsync,
1992 .unlocked_ioctl = fuse_dir_ioctl,
1993 .compat_ioctl = fuse_dir_compat_ioctl,
1996 static const struct inode_operations fuse_common_inode_operations = {
1997 .setattr = fuse_setattr,
1998 .permission = fuse_permission,
1999 .getattr = fuse_getattr,
2000 .setxattr = fuse_setxattr,
2001 .getxattr = fuse_getxattr,
2002 .listxattr = fuse_listxattr,
2003 .removexattr = fuse_removexattr,
2006 static const struct inode_operations fuse_symlink_inode_operations = {
2007 .setattr = fuse_setattr,
2008 .follow_link = fuse_follow_link,
2009 .put_link = free_page_put_link,
2010 .readlink = generic_readlink,
2011 .getattr = fuse_getattr,
2012 .setxattr = fuse_setxattr,
2013 .getxattr = fuse_getxattr,
2014 .listxattr = fuse_listxattr,
2015 .removexattr = fuse_removexattr,
2018 void fuse_init_common(struct inode *inode)
2020 inode->i_op = &fuse_common_inode_operations;
2023 void fuse_init_dir(struct inode *inode)
2025 inode->i_op = &fuse_dir_inode_operations;
2026 inode->i_fop = &fuse_dir_operations;
2029 void fuse_init_symlink(struct inode *inode)
2031 inode->i_op = &fuse_symlink_inode_operations;