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_req *req,
149 u64 nodeid, struct qstr *name,
150 struct fuse_entry_out *outarg)
152 memset(outarg, 0, sizeof(struct fuse_entry_out));
153 req->in.h.opcode = FUSE_LOOKUP;
154 req->in.h.nodeid = nodeid;
156 req->in.args[0].size = name->len + 1;
157 req->in.args[0].value = name->name;
158 req->out.numargs = 1;
160 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
162 req->out.args[0].size = sizeof(struct fuse_entry_out);
163 req->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)) {
204 struct fuse_entry_out outarg;
205 struct fuse_req *req;
206 struct fuse_forget_link *forget;
209 /* For negative dentries, always do a fresh lookup */
214 if (flags & LOOKUP_RCU)
217 fc = get_fuse_conn(inode);
218 req = fuse_get_req_nopages(fc);
223 forget = fuse_alloc_forget();
225 fuse_put_request(fc, req);
230 attr_version = fuse_get_attr_version(fc);
232 parent = dget_parent(entry);
233 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
234 &entry->d_name, &outarg);
235 fuse_request_send(fc, req);
237 err = req->out.h.error;
238 fuse_put_request(fc, req);
239 /* Zero nodeid is same as -ENOENT */
240 if (!err && !outarg.nodeid)
243 fi = get_fuse_inode(inode);
244 if (outarg.nodeid != get_node_id(inode)) {
245 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
248 spin_lock(&fc->lock);
250 spin_unlock(&fc->lock);
253 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
256 fuse_change_attributes(inode, &outarg.attr,
257 entry_attr_timeout(&outarg),
259 fuse_change_entry_timeout(entry, &outarg);
261 fi = get_fuse_inode(inode);
262 if (flags & LOOKUP_RCU) {
263 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
265 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
266 parent = dget_parent(entry);
267 fuse_advise_use_readdirplus(parent->d_inode);
280 static int invalid_nodeid(u64 nodeid)
282 return !nodeid || nodeid == FUSE_ROOT_ID;
285 const struct dentry_operations fuse_dentry_operations = {
286 .d_revalidate = fuse_dentry_revalidate,
289 int fuse_valid_type(int m)
291 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
292 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
295 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
296 struct fuse_entry_out *outarg, struct inode **inode)
298 struct fuse_conn *fc = get_fuse_conn_super(sb);
299 struct fuse_req *req;
300 struct fuse_forget_link *forget;
306 if (name->len > FUSE_NAME_MAX)
309 req = fuse_get_req_nopages(fc);
314 forget = fuse_alloc_forget();
317 fuse_put_request(fc, req);
321 attr_version = fuse_get_attr_version(fc);
323 fuse_lookup_init(fc, req, nodeid, name, outarg);
324 fuse_request_send(fc, req);
325 err = req->out.h.error;
326 fuse_put_request(fc, req);
327 /* Zero nodeid is same as -ENOENT, but with valid timeout */
328 if (err || !outarg->nodeid)
334 if (!fuse_valid_type(outarg->attr.mode))
337 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
338 &outarg->attr, entry_attr_timeout(outarg),
342 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
353 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
357 struct fuse_entry_out outarg;
359 struct dentry *newent;
360 bool outarg_valid = true;
362 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
364 if (err == -ENOENT) {
365 outarg_valid = false;
372 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
375 newent = d_materialise_unique(entry, inode);
376 err = PTR_ERR(newent);
380 entry = newent ? newent : entry;
382 fuse_change_entry_timeout(entry, &outarg);
384 fuse_invalidate_entry_cache(entry);
386 fuse_advise_use_readdirplus(dir);
396 * Atomic create+open operation
398 * If the filesystem doesn't support this, then fall back to separate
399 * 'mknod' + 'open' requests.
401 static int fuse_create_open(struct inode *dir, struct dentry *entry,
402 struct file *file, unsigned flags,
403 umode_t mode, int *opened)
407 struct fuse_conn *fc = get_fuse_conn(dir);
408 struct fuse_req *req;
409 struct fuse_forget_link *forget;
410 struct fuse_create_in inarg;
411 struct fuse_open_out outopen;
412 struct fuse_entry_out outentry;
413 struct fuse_file *ff;
415 /* Userspace expects S_IFREG in create mode */
416 BUG_ON((mode & S_IFMT) != S_IFREG);
418 forget = fuse_alloc_forget();
423 req = fuse_get_req_nopages(fc);
426 goto out_put_forget_req;
429 ff = fuse_file_alloc(fc);
431 goto out_put_request;
434 mode &= ~current_umask();
437 memset(&inarg, 0, sizeof(inarg));
438 memset(&outentry, 0, sizeof(outentry));
441 inarg.umask = current_umask();
442 req->in.h.opcode = FUSE_CREATE;
443 req->in.h.nodeid = get_node_id(dir);
445 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
447 req->in.args[0].value = &inarg;
448 req->in.args[1].size = entry->d_name.len + 1;
449 req->in.args[1].value = entry->d_name.name;
450 req->out.numargs = 2;
452 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
454 req->out.args[0].size = sizeof(outentry);
455 req->out.args[0].value = &outentry;
456 req->out.args[1].size = sizeof(outopen);
457 req->out.args[1].value = &outopen;
458 fuse_request_send(fc, req);
459 err = req->out.h.error;
464 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
467 fuse_put_request(fc, req);
469 ff->nodeid = outentry.nodeid;
470 ff->open_flags = outopen.open_flags;
471 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
472 &outentry.attr, entry_attr_timeout(&outentry), 0);
474 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
475 fuse_sync_release(ff, flags);
476 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
481 d_instantiate(entry, inode);
482 fuse_change_entry_timeout(entry, &outentry);
483 fuse_invalidate_attr(dir);
484 err = finish_open(file, entry, generic_file_open, opened);
486 fuse_sync_release(ff, flags);
488 file->private_data = fuse_file_get(ff);
489 fuse_finish_open(inode, file);
496 fuse_put_request(fc, req);
503 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
504 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
505 struct file *file, unsigned flags,
506 umode_t mode, int *opened)
509 struct fuse_conn *fc = get_fuse_conn(dir);
510 struct dentry *res = NULL;
512 if (d_unhashed(entry)) {
513 res = fuse_lookup(dir, entry, 0);
521 if (!(flags & O_CREAT) || entry->d_inode)
525 *opened |= FILE_CREATED;
530 err = fuse_create_open(dir, entry, file, flags, mode, opened);
531 if (err == -ENOSYS) {
540 err = fuse_mknod(dir, entry, mode, 0);
544 return finish_no_open(file, res);
548 * Code shared between mknod, mkdir, symlink and link
550 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
551 struct inode *dir, struct dentry *entry,
554 struct fuse_entry_out outarg;
557 struct fuse_forget_link *forget;
559 forget = fuse_alloc_forget();
561 fuse_put_request(fc, req);
565 memset(&outarg, 0, sizeof(outarg));
566 req->in.h.nodeid = get_node_id(dir);
567 req->out.numargs = 1;
569 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
571 req->out.args[0].size = sizeof(outarg);
572 req->out.args[0].value = &outarg;
573 fuse_request_send(fc, req);
574 err = req->out.h.error;
575 fuse_put_request(fc, req);
577 goto out_put_forget_req;
580 if (invalid_nodeid(outarg.nodeid))
581 goto out_put_forget_req;
583 if ((outarg.attr.mode ^ mode) & S_IFMT)
584 goto out_put_forget_req;
586 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
587 &outarg.attr, entry_attr_timeout(&outarg), 0);
589 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
594 err = d_instantiate_no_diralias(entry, inode);
598 fuse_change_entry_timeout(entry, &outarg);
599 fuse_invalidate_attr(dir);
607 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
610 struct fuse_mknod_in inarg;
611 struct fuse_conn *fc = get_fuse_conn(dir);
612 struct fuse_req *req = fuse_get_req_nopages(fc);
617 mode &= ~current_umask();
619 memset(&inarg, 0, sizeof(inarg));
621 inarg.rdev = new_encode_dev(rdev);
622 inarg.umask = current_umask();
623 req->in.h.opcode = FUSE_MKNOD;
625 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
627 req->in.args[0].value = &inarg;
628 req->in.args[1].size = entry->d_name.len + 1;
629 req->in.args[1].value = entry->d_name.name;
630 return create_new_entry(fc, req, dir, entry, mode);
633 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
636 return fuse_mknod(dir, entry, mode, 0);
639 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
641 struct fuse_mkdir_in inarg;
642 struct fuse_conn *fc = get_fuse_conn(dir);
643 struct fuse_req *req = fuse_get_req_nopages(fc);
648 mode &= ~current_umask();
650 memset(&inarg, 0, sizeof(inarg));
652 inarg.umask = current_umask();
653 req->in.h.opcode = FUSE_MKDIR;
655 req->in.args[0].size = sizeof(inarg);
656 req->in.args[0].value = &inarg;
657 req->in.args[1].size = entry->d_name.len + 1;
658 req->in.args[1].value = entry->d_name.name;
659 return create_new_entry(fc, req, dir, entry, S_IFDIR);
662 static int fuse_symlink(struct inode *dir, struct dentry *entry,
665 struct fuse_conn *fc = get_fuse_conn(dir);
666 unsigned len = strlen(link) + 1;
667 struct fuse_req *req = fuse_get_req_nopages(fc);
671 req->in.h.opcode = FUSE_SYMLINK;
673 req->in.args[0].size = entry->d_name.len + 1;
674 req->in.args[0].value = entry->d_name.name;
675 req->in.args[1].size = len;
676 req->in.args[1].value = link;
677 return create_new_entry(fc, req, 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);
692 struct fuse_req *req = fuse_get_req_nopages(fc);
696 req->in.h.opcode = FUSE_UNLINK;
697 req->in.h.nodeid = get_node_id(dir);
699 req->in.args[0].size = entry->d_name.len + 1;
700 req->in.args[0].value = entry->d_name.name;
701 fuse_request_send(fc, req);
702 err = req->out.h.error;
703 fuse_put_request(fc, req);
705 struct inode *inode = entry->d_inode;
706 struct fuse_inode *fi = get_fuse_inode(inode);
708 spin_lock(&fc->lock);
709 fi->attr_version = ++fc->attr_version;
711 * If i_nlink == 0 then unlink doesn't make sense, yet this can
712 * happen if userspace filesystem is careless. It would be
713 * difficult to enforce correct nlink usage so just ignore this
716 if (inode->i_nlink > 0)
718 spin_unlock(&fc->lock);
719 fuse_invalidate_attr(inode);
720 fuse_invalidate_attr(dir);
721 fuse_invalidate_entry_cache(entry);
722 fuse_update_ctime(inode);
723 } else if (err == -EINTR)
724 fuse_invalidate_entry(entry);
728 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
731 struct fuse_conn *fc = get_fuse_conn(dir);
732 struct fuse_req *req = fuse_get_req_nopages(fc);
736 req->in.h.opcode = FUSE_RMDIR;
737 req->in.h.nodeid = get_node_id(dir);
739 req->in.args[0].size = entry->d_name.len + 1;
740 req->in.args[0].value = entry->d_name.name;
741 fuse_request_send(fc, req);
742 err = req->out.h.error;
743 fuse_put_request(fc, req);
745 clear_nlink(entry->d_inode);
746 fuse_invalidate_attr(dir);
747 fuse_invalidate_entry_cache(entry);
748 } else if (err == -EINTR)
749 fuse_invalidate_entry(entry);
753 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
754 struct inode *newdir, struct dentry *newent,
755 unsigned int flags, int opcode, size_t argsize)
758 struct fuse_rename2_in inarg;
759 struct fuse_conn *fc = get_fuse_conn(olddir);
760 struct fuse_req *req;
762 req = fuse_get_req_nopages(fc);
766 memset(&inarg, 0, argsize);
767 inarg.newdir = get_node_id(newdir);
769 req->in.h.opcode = opcode;
770 req->in.h.nodeid = get_node_id(olddir);
772 req->in.args[0].size = argsize;
773 req->in.args[0].value = &inarg;
774 req->in.args[1].size = oldent->d_name.len + 1;
775 req->in.args[1].value = oldent->d_name.name;
776 req->in.args[2].size = newent->d_name.len + 1;
777 req->in.args[2].value = newent->d_name.name;
778 fuse_request_send(fc, req);
779 err = req->out.h.error;
780 fuse_put_request(fc, req);
783 fuse_invalidate_attr(oldent->d_inode);
784 fuse_update_ctime(oldent->d_inode);
786 if (flags & RENAME_EXCHANGE) {
787 fuse_invalidate_attr(newent->d_inode);
788 fuse_update_ctime(newent->d_inode);
791 fuse_invalidate_attr(olddir);
792 if (olddir != newdir)
793 fuse_invalidate_attr(newdir);
795 /* newent will end up negative */
796 if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
797 fuse_invalidate_attr(newent->d_inode);
798 fuse_invalidate_entry_cache(newent);
799 fuse_update_ctime(newent->d_inode);
801 } else if (err == -EINTR) {
802 /* If request was interrupted, DEITY only knows if the
803 rename actually took place. If the invalidation
804 fails (e.g. some process has CWD under the renamed
805 directory), then there can be inconsistency between
806 the dcache and the real filesystem. Tough luck. */
807 fuse_invalidate_entry(oldent);
809 fuse_invalidate_entry(newent);
815 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
816 struct inode *newdir, struct dentry *newent,
819 struct fuse_conn *fc = get_fuse_conn(olddir);
822 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
826 if (fc->no_rename2 || fc->minor < 23)
829 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
831 sizeof(struct fuse_rename2_in));
832 if (err == -ENOSYS) {
837 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
839 sizeof(struct fuse_rename_in));
845 static int fuse_link(struct dentry *entry, struct inode *newdir,
846 struct dentry *newent)
849 struct fuse_link_in inarg;
850 struct inode *inode = entry->d_inode;
851 struct fuse_conn *fc = get_fuse_conn(inode);
852 struct fuse_req *req = fuse_get_req_nopages(fc);
856 memset(&inarg, 0, sizeof(inarg));
857 inarg.oldnodeid = get_node_id(inode);
858 req->in.h.opcode = FUSE_LINK;
860 req->in.args[0].size = sizeof(inarg);
861 req->in.args[0].value = &inarg;
862 req->in.args[1].size = newent->d_name.len + 1;
863 req->in.args[1].value = newent->d_name.name;
864 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
865 /* Contrary to "normal" filesystems it can happen that link
866 makes two "logical" inodes point to the same "physical"
867 inode. We invalidate the attributes of the old one, so it
868 will reflect changes in the backing inode (link count,
872 struct fuse_inode *fi = get_fuse_inode(inode);
874 spin_lock(&fc->lock);
875 fi->attr_version = ++fc->attr_version;
877 spin_unlock(&fc->lock);
878 fuse_invalidate_attr(inode);
879 fuse_update_ctime(inode);
880 } else if (err == -EINTR) {
881 fuse_invalidate_attr(inode);
886 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
889 unsigned int blkbits;
890 struct fuse_conn *fc = get_fuse_conn(inode);
892 /* see the comment in fuse_change_attributes() */
893 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
894 attr->size = i_size_read(inode);
895 attr->mtime = inode->i_mtime.tv_sec;
896 attr->mtimensec = inode->i_mtime.tv_nsec;
897 attr->ctime = inode->i_ctime.tv_sec;
898 attr->ctimensec = inode->i_ctime.tv_nsec;
901 stat->dev = inode->i_sb->s_dev;
902 stat->ino = attr->ino;
903 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
904 stat->nlink = attr->nlink;
905 stat->uid = make_kuid(&init_user_ns, attr->uid);
906 stat->gid = make_kgid(&init_user_ns, attr->gid);
907 stat->rdev = inode->i_rdev;
908 stat->atime.tv_sec = attr->atime;
909 stat->atime.tv_nsec = attr->atimensec;
910 stat->mtime.tv_sec = attr->mtime;
911 stat->mtime.tv_nsec = attr->mtimensec;
912 stat->ctime.tv_sec = attr->ctime;
913 stat->ctime.tv_nsec = attr->ctimensec;
914 stat->size = attr->size;
915 stat->blocks = attr->blocks;
917 if (attr->blksize != 0)
918 blkbits = ilog2(attr->blksize);
920 blkbits = inode->i_sb->s_blocksize_bits;
922 stat->blksize = 1 << blkbits;
925 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
929 struct fuse_getattr_in inarg;
930 struct fuse_attr_out outarg;
931 struct fuse_conn *fc = get_fuse_conn(inode);
932 struct fuse_req *req;
935 req = fuse_get_req_nopages(fc);
939 attr_version = fuse_get_attr_version(fc);
941 memset(&inarg, 0, sizeof(inarg));
942 memset(&outarg, 0, sizeof(outarg));
943 /* Directories have separate file-handle space */
944 if (file && S_ISREG(inode->i_mode)) {
945 struct fuse_file *ff = file->private_data;
947 inarg.getattr_flags |= FUSE_GETATTR_FH;
950 req->in.h.opcode = FUSE_GETATTR;
951 req->in.h.nodeid = get_node_id(inode);
953 req->in.args[0].size = sizeof(inarg);
954 req->in.args[0].value = &inarg;
955 req->out.numargs = 1;
957 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
959 req->out.args[0].size = sizeof(outarg);
960 req->out.args[0].value = &outarg;
961 fuse_request_send(fc, req);
962 err = req->out.h.error;
963 fuse_put_request(fc, req);
965 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
966 make_bad_inode(inode);
969 fuse_change_attributes(inode, &outarg.attr,
970 attr_timeout(&outarg),
973 fuse_fillattr(inode, &outarg.attr, stat);
979 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
980 struct file *file, bool *refreshed)
982 struct fuse_inode *fi = get_fuse_inode(inode);
986 if (time_before64(fi->i_time, get_jiffies_64())) {
988 err = fuse_do_getattr(inode, stat, file);
993 generic_fillattr(inode, stat);
994 stat->mode = fi->orig_i_mode;
995 stat->ino = fi->orig_ino;
999 if (refreshed != NULL)
1005 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
1006 u64 child_nodeid, struct qstr *name)
1009 struct inode *parent;
1011 struct dentry *entry;
1013 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1017 mutex_lock(&parent->i_mutex);
1018 if (!S_ISDIR(parent->i_mode))
1022 dir = d_find_alias(parent);
1026 entry = d_lookup(dir, name);
1031 fuse_invalidate_attr(parent);
1032 fuse_invalidate_entry(entry);
1034 if (child_nodeid != 0 && entry->d_inode) {
1035 mutex_lock(&entry->d_inode->i_mutex);
1036 if (get_node_id(entry->d_inode) != child_nodeid) {
1040 if (d_mountpoint(entry)) {
1044 if (S_ISDIR(entry->d_inode->i_mode)) {
1045 shrink_dcache_parent(entry);
1046 if (!simple_empty(entry)) {
1050 entry->d_inode->i_flags |= S_DEAD;
1053 clear_nlink(entry->d_inode);
1056 mutex_unlock(&entry->d_inode->i_mutex);
1065 mutex_unlock(&parent->i_mutex);
1071 * Calling into a user-controlled filesystem gives the filesystem
1072 * daemon ptrace-like capabilities over the current process. This
1073 * means, that the filesystem daemon is able to record the exact
1074 * filesystem operations performed, and can also control the behavior
1075 * of the requester process in otherwise impossible ways. For example
1076 * it can delay the operation for arbitrary length of time allowing
1077 * DoS against the requester.
1079 * For this reason only those processes can call into the filesystem,
1080 * for which the owner of the mount has ptrace privilege. This
1081 * excludes processes started by other users, suid or sgid processes.
1083 int fuse_allow_current_process(struct fuse_conn *fc)
1085 const struct cred *cred;
1087 if (fc->flags & FUSE_ALLOW_OTHER)
1090 cred = current_cred();
1091 if (uid_eq(cred->euid, fc->user_id) &&
1092 uid_eq(cred->suid, fc->user_id) &&
1093 uid_eq(cred->uid, fc->user_id) &&
1094 gid_eq(cred->egid, fc->group_id) &&
1095 gid_eq(cred->sgid, fc->group_id) &&
1096 gid_eq(cred->gid, fc->group_id))
1102 static int fuse_access(struct inode *inode, int mask)
1104 struct fuse_conn *fc = get_fuse_conn(inode);
1105 struct fuse_req *req;
1106 struct fuse_access_in inarg;
1109 BUG_ON(mask & MAY_NOT_BLOCK);
1114 req = fuse_get_req_nopages(fc);
1116 return PTR_ERR(req);
1118 memset(&inarg, 0, sizeof(inarg));
1119 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1120 req->in.h.opcode = FUSE_ACCESS;
1121 req->in.h.nodeid = get_node_id(inode);
1122 req->in.numargs = 1;
1123 req->in.args[0].size = sizeof(inarg);
1124 req->in.args[0].value = &inarg;
1125 fuse_request_send(fc, req);
1126 err = req->out.h.error;
1127 fuse_put_request(fc, req);
1128 if (err == -ENOSYS) {
1135 static int fuse_perm_getattr(struct inode *inode, int mask)
1137 if (mask & MAY_NOT_BLOCK)
1140 return fuse_do_getattr(inode, NULL, NULL);
1144 * Check permission. The two basic access models of FUSE are:
1146 * 1) Local access checking ('default_permissions' mount option) based
1147 * on file mode. This is the plain old disk filesystem permission
1150 * 2) "Remote" access checking, where server is responsible for
1151 * checking permission in each inode operation. An exception to this
1152 * is if ->permission() was invoked from sys_access() in which case an
1153 * access request is sent. Execute permission is still checked
1154 * locally based on file mode.
1156 static int fuse_permission(struct inode *inode, int mask)
1158 struct fuse_conn *fc = get_fuse_conn(inode);
1159 bool refreshed = false;
1162 if (!fuse_allow_current_process(fc))
1166 * If attributes are needed, refresh them before proceeding
1168 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1169 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1170 struct fuse_inode *fi = get_fuse_inode(inode);
1172 if (time_before64(fi->i_time, get_jiffies_64())) {
1175 err = fuse_perm_getattr(inode, mask);
1181 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1182 err = generic_permission(inode, mask);
1184 /* If permission is denied, try to refresh file
1185 attributes. This is also needed, because the root
1186 node will at first have no permissions */
1187 if (err == -EACCES && !refreshed) {
1188 err = fuse_perm_getattr(inode, mask);
1190 err = generic_permission(inode, mask);
1193 /* Note: the opposite of the above test does not
1194 exist. So if permissions are revoked this won't be
1195 noticed immediately, only after the attribute
1196 timeout has expired */
1197 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1198 err = fuse_access(inode, mask);
1199 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1200 if (!(inode->i_mode & S_IXUGO)) {
1204 err = fuse_perm_getattr(inode, mask);
1205 if (!err && !(inode->i_mode & S_IXUGO))
1212 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1213 struct dir_context *ctx)
1215 while (nbytes >= FUSE_NAME_OFFSET) {
1216 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1217 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1218 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1220 if (reclen > nbytes)
1222 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1225 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1226 dirent->ino, dirent->type))
1231 ctx->pos = dirent->off;
1237 static int fuse_direntplus_link(struct file *file,
1238 struct fuse_direntplus *direntplus,
1242 struct fuse_entry_out *o = &direntplus->entry_out;
1243 struct fuse_dirent *dirent = &direntplus->dirent;
1244 struct dentry *parent = file->f_path.dentry;
1245 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1246 struct dentry *dentry;
1247 struct dentry *alias;
1248 struct inode *dir = parent->d_inode;
1249 struct fuse_conn *fc;
1250 struct inode *inode;
1254 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1255 * ENOENT. Instead, it only means the userspace filesystem did
1256 * not want to return attributes/handle for this entry.
1263 if (name.name[0] == '.') {
1265 * We could potentially refresh the attributes of the directory
1270 if (name.name[1] == '.' && name.len == 2)
1274 if (invalid_nodeid(o->nodeid))
1276 if (!fuse_valid_type(o->attr.mode))
1279 fc = get_fuse_conn(dir);
1281 name.hash = full_name_hash(name.name, name.len);
1282 dentry = d_lookup(parent, &name);
1284 inode = dentry->d_inode;
1287 } else if (get_node_id(inode) != o->nodeid ||
1288 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1289 d_invalidate(dentry);
1290 } else if (is_bad_inode(inode)) {
1294 struct fuse_inode *fi;
1295 fi = get_fuse_inode(inode);
1296 spin_lock(&fc->lock);
1298 spin_unlock(&fc->lock);
1300 fuse_change_attributes(inode, &o->attr,
1301 entry_attr_timeout(o),
1305 * The other branch to 'found' comes via fuse_iget()
1306 * which bumps nlookup inside
1313 dentry = d_alloc(parent, &name);
1318 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1319 &o->attr, entry_attr_timeout(o), attr_version);
1323 alias = d_materialise_unique(dentry, inode);
1324 err = PTR_ERR(alias);
1334 if (fc->readdirplus_auto)
1335 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1336 fuse_change_entry_timeout(dentry, o);
1344 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1345 struct dir_context *ctx, u64 attr_version)
1347 struct fuse_direntplus *direntplus;
1348 struct fuse_dirent *dirent;
1353 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1354 direntplus = (struct fuse_direntplus *) buf;
1355 dirent = &direntplus->dirent;
1356 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1358 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1360 if (reclen > nbytes)
1362 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1366 /* We fill entries into dstbuf only as much as
1367 it can hold. But we still continue iterating
1368 over remaining entries to link them. If not,
1369 we need to send a FORGET for each of those
1370 which we did not link.
1372 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1373 dirent->ino, dirent->type);
1374 ctx->pos = dirent->off;
1380 ret = fuse_direntplus_link(file, direntplus, attr_version);
1382 fuse_force_forget(file, direntplus->entry_out.nodeid);
1388 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1393 struct inode *inode = file_inode(file);
1394 struct fuse_conn *fc = get_fuse_conn(inode);
1395 struct fuse_req *req;
1396 u64 attr_version = 0;
1398 if (is_bad_inode(inode))
1401 req = fuse_get_req(fc, 1);
1403 return PTR_ERR(req);
1405 page = alloc_page(GFP_KERNEL);
1407 fuse_put_request(fc, req);
1411 plus = fuse_use_readdirplus(inode, ctx);
1412 req->out.argpages = 1;
1414 req->pages[0] = page;
1415 req->page_descs[0].length = PAGE_SIZE;
1417 attr_version = fuse_get_attr_version(fc);
1418 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1421 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1424 fuse_request_send(fc, req);
1425 nbytes = req->out.args[0].size;
1426 err = req->out.h.error;
1427 fuse_put_request(fc, req);
1430 err = parse_dirplusfile(page_address(page), nbytes,
1434 err = parse_dirfile(page_address(page), nbytes, file,
1440 fuse_invalidate_atime(inode);
1444 static char *read_link(struct dentry *dentry)
1446 struct inode *inode = dentry->d_inode;
1447 struct fuse_conn *fc = get_fuse_conn(inode);
1448 struct fuse_req *req = fuse_get_req_nopages(fc);
1452 return ERR_CAST(req);
1454 link = (char *) __get_free_page(GFP_KERNEL);
1456 link = ERR_PTR(-ENOMEM);
1459 req->in.h.opcode = FUSE_READLINK;
1460 req->in.h.nodeid = get_node_id(inode);
1461 req->out.argvar = 1;
1462 req->out.numargs = 1;
1463 req->out.args[0].size = PAGE_SIZE - 1;
1464 req->out.args[0].value = link;
1465 fuse_request_send(fc, req);
1466 if (req->out.h.error) {
1467 free_page((unsigned long) link);
1468 link = ERR_PTR(req->out.h.error);
1470 link[req->out.args[0].size] = '\0';
1472 fuse_put_request(fc, req);
1473 fuse_invalidate_atime(inode);
1477 static void free_link(char *link)
1480 free_page((unsigned long) link);
1483 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1485 nd_set_link(nd, read_link(dentry));
1489 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1491 free_link(nd_get_link(nd));
1494 static int fuse_dir_open(struct inode *inode, struct file *file)
1496 return fuse_open_common(inode, file, true);
1499 static int fuse_dir_release(struct inode *inode, struct file *file)
1501 fuse_release_common(file, FUSE_RELEASEDIR);
1506 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1509 return fuse_fsync_common(file, start, end, datasync, 1);
1512 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1515 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1517 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1521 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1524 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1527 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1532 return fuse_ioctl_common(file, cmd, arg,
1533 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1536 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1538 /* Always update if mtime is explicitly set */
1539 if (ivalid & ATTR_MTIME_SET)
1542 /* Or if kernel i_mtime is the official one */
1543 if (trust_local_mtime)
1546 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1547 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1550 /* In all other cases update */
1554 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1555 bool trust_local_cmtime)
1557 unsigned ivalid = iattr->ia_valid;
1559 if (ivalid & ATTR_MODE)
1560 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1561 if (ivalid & ATTR_UID)
1562 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1563 if (ivalid & ATTR_GID)
1564 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1565 if (ivalid & ATTR_SIZE)
1566 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1567 if (ivalid & ATTR_ATIME) {
1568 arg->valid |= FATTR_ATIME;
1569 arg->atime = iattr->ia_atime.tv_sec;
1570 arg->atimensec = iattr->ia_atime.tv_nsec;
1571 if (!(ivalid & ATTR_ATIME_SET))
1572 arg->valid |= FATTR_ATIME_NOW;
1574 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1575 arg->valid |= FATTR_MTIME;
1576 arg->mtime = iattr->ia_mtime.tv_sec;
1577 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1578 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1579 arg->valid |= FATTR_MTIME_NOW;
1581 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1582 arg->valid |= FATTR_CTIME;
1583 arg->ctime = iattr->ia_ctime.tv_sec;
1584 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1589 * Prevent concurrent writepages on inode
1591 * This is done by adding a negative bias to the inode write counter
1592 * and waiting for all pending writes to finish.
1594 void fuse_set_nowrite(struct inode *inode)
1596 struct fuse_conn *fc = get_fuse_conn(inode);
1597 struct fuse_inode *fi = get_fuse_inode(inode);
1599 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1601 spin_lock(&fc->lock);
1602 BUG_ON(fi->writectr < 0);
1603 fi->writectr += FUSE_NOWRITE;
1604 spin_unlock(&fc->lock);
1605 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1609 * Allow writepages on inode
1611 * Remove the bias from the writecounter and send any queued
1614 static void __fuse_release_nowrite(struct inode *inode)
1616 struct fuse_inode *fi = get_fuse_inode(inode);
1618 BUG_ON(fi->writectr != FUSE_NOWRITE);
1620 fuse_flush_writepages(inode);
1623 void fuse_release_nowrite(struct inode *inode)
1625 struct fuse_conn *fc = get_fuse_conn(inode);
1627 spin_lock(&fc->lock);
1628 __fuse_release_nowrite(inode);
1629 spin_unlock(&fc->lock);
1632 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1633 struct inode *inode,
1634 struct fuse_setattr_in *inarg_p,
1635 struct fuse_attr_out *outarg_p)
1637 req->in.h.opcode = FUSE_SETATTR;
1638 req->in.h.nodeid = get_node_id(inode);
1639 req->in.numargs = 1;
1640 req->in.args[0].size = sizeof(*inarg_p);
1641 req->in.args[0].value = inarg_p;
1642 req->out.numargs = 1;
1644 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1646 req->out.args[0].size = sizeof(*outarg_p);
1647 req->out.args[0].value = outarg_p;
1651 * Flush inode->i_mtime to the server
1653 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1655 struct fuse_conn *fc = get_fuse_conn(inode);
1656 struct fuse_req *req;
1657 struct fuse_setattr_in inarg;
1658 struct fuse_attr_out outarg;
1661 req = fuse_get_req_nopages(fc);
1663 return PTR_ERR(req);
1665 memset(&inarg, 0, sizeof(inarg));
1666 memset(&outarg, 0, sizeof(outarg));
1668 inarg.valid = FATTR_MTIME;
1669 inarg.mtime = inode->i_mtime.tv_sec;
1670 inarg.mtimensec = inode->i_mtime.tv_nsec;
1671 if (fc->minor >= 23) {
1672 inarg.valid |= FATTR_CTIME;
1673 inarg.ctime = inode->i_ctime.tv_sec;
1674 inarg.ctimensec = inode->i_ctime.tv_nsec;
1677 inarg.valid |= FATTR_FH;
1680 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1681 fuse_request_send(fc, req);
1682 err = req->out.h.error;
1683 fuse_put_request(fc, req);
1689 * Set attributes, and at the same time refresh them.
1691 * Truncation is slightly complicated, because the 'truncate' request
1692 * may fail, in which case we don't want to touch the mapping.
1693 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1694 * and the actual truncation by hand.
1696 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1699 struct fuse_conn *fc = get_fuse_conn(inode);
1700 struct fuse_inode *fi = get_fuse_inode(inode);
1701 struct fuse_req *req;
1702 struct fuse_setattr_in inarg;
1703 struct fuse_attr_out outarg;
1704 bool is_truncate = false;
1705 bool is_wb = fc->writeback_cache;
1708 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1710 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1711 attr->ia_valid |= ATTR_FORCE;
1713 err = inode_change_ok(inode, attr);
1717 if (attr->ia_valid & ATTR_OPEN) {
1718 if (fc->atomic_o_trunc)
1723 if (attr->ia_valid & ATTR_SIZE)
1726 req = fuse_get_req_nopages(fc);
1728 return PTR_ERR(req);
1731 fuse_set_nowrite(inode);
1732 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1733 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1734 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1737 memset(&inarg, 0, sizeof(inarg));
1738 memset(&outarg, 0, sizeof(outarg));
1739 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1741 struct fuse_file *ff = file->private_data;
1742 inarg.valid |= FATTR_FH;
1745 if (attr->ia_valid & ATTR_SIZE) {
1746 /* For mandatory locking in truncate */
1747 inarg.valid |= FATTR_LOCKOWNER;
1748 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1750 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1751 fuse_request_send(fc, req);
1752 err = req->out.h.error;
1753 fuse_put_request(fc, req);
1756 fuse_invalidate_attr(inode);
1760 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1761 make_bad_inode(inode);
1766 spin_lock(&fc->lock);
1767 /* the kernel maintains i_mtime locally */
1768 if (trust_local_cmtime) {
1769 if (attr->ia_valid & ATTR_MTIME)
1770 inode->i_mtime = attr->ia_mtime;
1771 if (attr->ia_valid & ATTR_CTIME)
1772 inode->i_ctime = attr->ia_ctime;
1773 /* FIXME: clear I_DIRTY_SYNC? */
1776 fuse_change_attributes_common(inode, &outarg.attr,
1777 attr_timeout(&outarg));
1778 oldsize = inode->i_size;
1779 /* see the comment in fuse_change_attributes() */
1780 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1781 i_size_write(inode, outarg.attr.size);
1784 /* NOTE: this may release/reacquire fc->lock */
1785 __fuse_release_nowrite(inode);
1787 spin_unlock(&fc->lock);
1790 * Only call invalidate_inode_pages2() after removing
1791 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1793 if ((is_truncate || !is_wb) &&
1794 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1795 truncate_pagecache(inode, outarg.attr.size);
1796 invalidate_inode_pages2(inode->i_mapping);
1799 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1804 fuse_release_nowrite(inode);
1806 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1810 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1812 struct inode *inode = entry->d_inode;
1814 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1817 if (attr->ia_valid & ATTR_FILE)
1818 return fuse_do_setattr(inode, attr, attr->ia_file);
1820 return fuse_do_setattr(inode, attr, NULL);
1823 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1826 struct inode *inode = entry->d_inode;
1827 struct fuse_conn *fc = get_fuse_conn(inode);
1829 if (!fuse_allow_current_process(fc))
1832 return fuse_update_attributes(inode, stat, NULL, NULL);
1835 static int fuse_setxattr(struct dentry *entry, const char *name,
1836 const void *value, size_t size, int flags)
1838 struct inode *inode = entry->d_inode;
1839 struct fuse_conn *fc = get_fuse_conn(inode);
1840 struct fuse_req *req;
1841 struct fuse_setxattr_in inarg;
1844 if (fc->no_setxattr)
1847 req = fuse_get_req_nopages(fc);
1849 return PTR_ERR(req);
1851 memset(&inarg, 0, sizeof(inarg));
1853 inarg.flags = flags;
1854 req->in.h.opcode = FUSE_SETXATTR;
1855 req->in.h.nodeid = get_node_id(inode);
1856 req->in.numargs = 3;
1857 req->in.args[0].size = sizeof(inarg);
1858 req->in.args[0].value = &inarg;
1859 req->in.args[1].size = strlen(name) + 1;
1860 req->in.args[1].value = name;
1861 req->in.args[2].size = size;
1862 req->in.args[2].value = value;
1863 fuse_request_send(fc, req);
1864 err = req->out.h.error;
1865 fuse_put_request(fc, req);
1866 if (err == -ENOSYS) {
1867 fc->no_setxattr = 1;
1871 fuse_invalidate_attr(inode);
1872 fuse_update_ctime(inode);
1877 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1878 void *value, size_t size)
1880 struct inode *inode = entry->d_inode;
1881 struct fuse_conn *fc = get_fuse_conn(inode);
1882 struct fuse_req *req;
1883 struct fuse_getxattr_in inarg;
1884 struct fuse_getxattr_out outarg;
1887 if (fc->no_getxattr)
1890 req = fuse_get_req_nopages(fc);
1892 return PTR_ERR(req);
1894 memset(&inarg, 0, sizeof(inarg));
1896 req->in.h.opcode = FUSE_GETXATTR;
1897 req->in.h.nodeid = get_node_id(inode);
1898 req->in.numargs = 2;
1899 req->in.args[0].size = sizeof(inarg);
1900 req->in.args[0].value = &inarg;
1901 req->in.args[1].size = strlen(name) + 1;
1902 req->in.args[1].value = name;
1903 /* This is really two different operations rolled into one */
1904 req->out.numargs = 1;
1906 req->out.argvar = 1;
1907 req->out.args[0].size = size;
1908 req->out.args[0].value = value;
1910 req->out.args[0].size = sizeof(outarg);
1911 req->out.args[0].value = &outarg;
1913 fuse_request_send(fc, req);
1914 ret = req->out.h.error;
1916 ret = size ? req->out.args[0].size : outarg.size;
1918 if (ret == -ENOSYS) {
1919 fc->no_getxattr = 1;
1923 fuse_put_request(fc, req);
1927 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1929 struct inode *inode = entry->d_inode;
1930 struct fuse_conn *fc = get_fuse_conn(inode);
1931 struct fuse_req *req;
1932 struct fuse_getxattr_in inarg;
1933 struct fuse_getxattr_out outarg;
1936 if (!fuse_allow_current_process(fc))
1939 if (fc->no_listxattr)
1942 req = fuse_get_req_nopages(fc);
1944 return PTR_ERR(req);
1946 memset(&inarg, 0, sizeof(inarg));
1948 req->in.h.opcode = FUSE_LISTXATTR;
1949 req->in.h.nodeid = get_node_id(inode);
1950 req->in.numargs = 1;
1951 req->in.args[0].size = sizeof(inarg);
1952 req->in.args[0].value = &inarg;
1953 /* This is really two different operations rolled into one */
1954 req->out.numargs = 1;
1956 req->out.argvar = 1;
1957 req->out.args[0].size = size;
1958 req->out.args[0].value = list;
1960 req->out.args[0].size = sizeof(outarg);
1961 req->out.args[0].value = &outarg;
1963 fuse_request_send(fc, req);
1964 ret = req->out.h.error;
1966 ret = size ? req->out.args[0].size : outarg.size;
1968 if (ret == -ENOSYS) {
1969 fc->no_listxattr = 1;
1973 fuse_put_request(fc, req);
1977 static int fuse_removexattr(struct dentry *entry, const char *name)
1979 struct inode *inode = entry->d_inode;
1980 struct fuse_conn *fc = get_fuse_conn(inode);
1981 struct fuse_req *req;
1984 if (fc->no_removexattr)
1987 req = fuse_get_req_nopages(fc);
1989 return PTR_ERR(req);
1991 req->in.h.opcode = FUSE_REMOVEXATTR;
1992 req->in.h.nodeid = get_node_id(inode);
1993 req->in.numargs = 1;
1994 req->in.args[0].size = strlen(name) + 1;
1995 req->in.args[0].value = name;
1996 fuse_request_send(fc, req);
1997 err = req->out.h.error;
1998 fuse_put_request(fc, req);
1999 if (err == -ENOSYS) {
2000 fc->no_removexattr = 1;
2004 fuse_invalidate_attr(inode);
2005 fuse_update_ctime(inode);
2010 static const struct inode_operations fuse_dir_inode_operations = {
2011 .lookup = fuse_lookup,
2012 .mkdir = fuse_mkdir,
2013 .symlink = fuse_symlink,
2014 .unlink = fuse_unlink,
2015 .rmdir = fuse_rmdir,
2016 .rename2 = fuse_rename2,
2018 .setattr = fuse_setattr,
2019 .create = fuse_create,
2020 .atomic_open = fuse_atomic_open,
2021 .mknod = fuse_mknod,
2022 .permission = fuse_permission,
2023 .getattr = fuse_getattr,
2024 .setxattr = fuse_setxattr,
2025 .getxattr = fuse_getxattr,
2026 .listxattr = fuse_listxattr,
2027 .removexattr = fuse_removexattr,
2030 static const struct file_operations fuse_dir_operations = {
2031 .llseek = generic_file_llseek,
2032 .read = generic_read_dir,
2033 .iterate = fuse_readdir,
2034 .open = fuse_dir_open,
2035 .release = fuse_dir_release,
2036 .fsync = fuse_dir_fsync,
2037 .unlocked_ioctl = fuse_dir_ioctl,
2038 .compat_ioctl = fuse_dir_compat_ioctl,
2041 static const struct inode_operations fuse_common_inode_operations = {
2042 .setattr = fuse_setattr,
2043 .permission = fuse_permission,
2044 .getattr = fuse_getattr,
2045 .setxattr = fuse_setxattr,
2046 .getxattr = fuse_getxattr,
2047 .listxattr = fuse_listxattr,
2048 .removexattr = fuse_removexattr,
2051 static const struct inode_operations fuse_symlink_inode_operations = {
2052 .setattr = fuse_setattr,
2053 .follow_link = fuse_follow_link,
2054 .put_link = fuse_put_link,
2055 .readlink = generic_readlink,
2056 .getattr = fuse_getattr,
2057 .setxattr = fuse_setxattr,
2058 .getxattr = fuse_getxattr,
2059 .listxattr = fuse_listxattr,
2060 .removexattr = fuse_removexattr,
2063 void fuse_init_common(struct inode *inode)
2065 inode->i_op = &fuse_common_inode_operations;
2068 void fuse_init_dir(struct inode *inode)
2070 inode->i_op = &fuse_dir_inode_operations;
2071 inode->i_fop = &fuse_dir_operations;
2074 void fuse_init_symlink(struct inode *inode)
2076 inode->i_op = &fuse_symlink_inode_operations;