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_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 (fuse_dentry_time(entry) < get_jiffies_64()) {
203 struct fuse_entry_out outarg;
204 struct fuse_req *req;
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);
217 req = fuse_get_req_nopages(fc);
222 forget = fuse_alloc_forget();
224 fuse_put_request(fc, req);
229 attr_version = fuse_get_attr_version(fc);
231 parent = dget_parent(entry);
232 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
233 &entry->d_name, &outarg);
234 fuse_request_send(fc, req);
236 err = req->out.h.error;
237 fuse_put_request(fc, req);
238 /* Zero nodeid is same as -ENOENT */
239 if (!err && !outarg.nodeid)
242 fi = get_fuse_inode(inode);
243 if (outarg.nodeid != get_node_id(inode)) {
244 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
247 spin_lock(&fc->lock);
249 spin_unlock(&fc->lock);
252 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
255 fuse_change_attributes(inode, &outarg.attr,
256 entry_attr_timeout(&outarg),
258 fuse_change_entry_timeout(entry, &outarg);
260 fi = get_fuse_inode(inode);
261 if (flags & LOOKUP_RCU) {
262 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
264 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
265 parent = dget_parent(entry);
266 fuse_advise_use_readdirplus(parent->d_inode);
277 if (!(flags & LOOKUP_RCU) && check_submounts_and_drop(entry) != 0)
282 static int invalid_nodeid(u64 nodeid)
284 return !nodeid || nodeid == FUSE_ROOT_ID;
287 const struct dentry_operations fuse_dentry_operations = {
288 .d_revalidate = fuse_dentry_revalidate,
291 int fuse_valid_type(int m)
293 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
294 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
297 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
298 struct fuse_entry_out *outarg, struct inode **inode)
300 struct fuse_conn *fc = get_fuse_conn_super(sb);
301 struct fuse_req *req;
302 struct fuse_forget_link *forget;
308 if (name->len > FUSE_NAME_MAX)
311 req = fuse_get_req_nopages(fc);
316 forget = fuse_alloc_forget();
319 fuse_put_request(fc, req);
323 attr_version = fuse_get_attr_version(fc);
325 fuse_lookup_init(fc, req, nodeid, name, outarg);
326 fuse_request_send(fc, req);
327 err = req->out.h.error;
328 fuse_put_request(fc, req);
329 /* Zero nodeid is same as -ENOENT, but with valid timeout */
330 if (err || !outarg->nodeid)
336 if (!fuse_valid_type(outarg->attr.mode))
339 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
340 &outarg->attr, entry_attr_timeout(outarg),
344 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
355 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
359 struct fuse_entry_out outarg;
361 struct dentry *newent;
362 bool outarg_valid = true;
364 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
366 if (err == -ENOENT) {
367 outarg_valid = false;
374 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
377 newent = d_materialise_unique(entry, inode);
378 err = PTR_ERR(newent);
382 entry = newent ? newent : entry;
384 fuse_change_entry_timeout(entry, &outarg);
386 fuse_invalidate_entry_cache(entry);
388 fuse_advise_use_readdirplus(dir);
398 * Atomic create+open operation
400 * If the filesystem doesn't support this, then fall back to separate
401 * 'mknod' + 'open' requests.
403 static int fuse_create_open(struct inode *dir, struct dentry *entry,
404 struct file *file, unsigned flags,
405 umode_t mode, int *opened)
409 struct fuse_conn *fc = get_fuse_conn(dir);
410 struct fuse_req *req;
411 struct fuse_forget_link *forget;
412 struct fuse_create_in inarg;
413 struct fuse_open_out outopen;
414 struct fuse_entry_out outentry;
415 struct fuse_file *ff;
417 /* Userspace expects S_IFREG in create mode */
418 BUG_ON((mode & S_IFMT) != S_IFREG);
420 forget = fuse_alloc_forget();
425 req = fuse_get_req_nopages(fc);
428 goto out_put_forget_req;
431 ff = fuse_file_alloc(fc);
433 goto out_put_request;
436 mode &= ~current_umask();
439 memset(&inarg, 0, sizeof(inarg));
440 memset(&outentry, 0, sizeof(outentry));
443 inarg.umask = current_umask();
444 req->in.h.opcode = FUSE_CREATE;
445 req->in.h.nodeid = get_node_id(dir);
447 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
449 req->in.args[0].value = &inarg;
450 req->in.args[1].size = entry->d_name.len + 1;
451 req->in.args[1].value = entry->d_name.name;
452 req->out.numargs = 2;
454 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
456 req->out.args[0].size = sizeof(outentry);
457 req->out.args[0].value = &outentry;
458 req->out.args[1].size = sizeof(outopen);
459 req->out.args[1].value = &outopen;
460 fuse_request_send(fc, req);
461 err = req->out.h.error;
466 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
469 fuse_put_request(fc, req);
471 ff->nodeid = outentry.nodeid;
472 ff->open_flags = outopen.open_flags;
473 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
474 &outentry.attr, entry_attr_timeout(&outentry), 0);
476 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
477 fuse_sync_release(ff, flags);
478 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
483 d_instantiate(entry, inode);
484 fuse_change_entry_timeout(entry, &outentry);
485 fuse_invalidate_attr(dir);
486 err = finish_open(file, entry, generic_file_open, opened);
488 fuse_sync_release(ff, flags);
490 file->private_data = fuse_file_get(ff);
491 fuse_finish_open(inode, file);
498 fuse_put_request(fc, req);
505 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
506 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
507 struct file *file, unsigned flags,
508 umode_t mode, int *opened)
511 struct fuse_conn *fc = get_fuse_conn(dir);
512 struct dentry *res = NULL;
514 if (d_unhashed(entry)) {
515 res = fuse_lookup(dir, entry, 0);
523 if (!(flags & O_CREAT) || entry->d_inode)
527 *opened |= FILE_CREATED;
532 err = fuse_create_open(dir, entry, file, flags, mode, opened);
533 if (err == -ENOSYS) {
542 err = fuse_mknod(dir, entry, mode, 0);
546 return finish_no_open(file, res);
550 * Code shared between mknod, mkdir, symlink and link
552 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
553 struct inode *dir, struct dentry *entry,
556 struct fuse_entry_out outarg;
559 struct fuse_forget_link *forget;
561 forget = fuse_alloc_forget();
563 fuse_put_request(fc, req);
567 memset(&outarg, 0, sizeof(outarg));
568 req->in.h.nodeid = get_node_id(dir);
569 req->out.numargs = 1;
571 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
573 req->out.args[0].size = sizeof(outarg);
574 req->out.args[0].value = &outarg;
575 fuse_request_send(fc, req);
576 err = req->out.h.error;
577 fuse_put_request(fc, req);
579 goto out_put_forget_req;
582 if (invalid_nodeid(outarg.nodeid))
583 goto out_put_forget_req;
585 if ((outarg.attr.mode ^ mode) & S_IFMT)
586 goto out_put_forget_req;
588 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
589 &outarg.attr, entry_attr_timeout(&outarg), 0);
591 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
596 err = d_instantiate_no_diralias(entry, inode);
600 fuse_change_entry_timeout(entry, &outarg);
601 fuse_invalidate_attr(dir);
609 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
612 struct fuse_mknod_in inarg;
613 struct fuse_conn *fc = get_fuse_conn(dir);
614 struct fuse_req *req = fuse_get_req_nopages(fc);
619 mode &= ~current_umask();
621 memset(&inarg, 0, sizeof(inarg));
623 inarg.rdev = new_encode_dev(rdev);
624 inarg.umask = current_umask();
625 req->in.h.opcode = FUSE_MKNOD;
627 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
629 req->in.args[0].value = &inarg;
630 req->in.args[1].size = entry->d_name.len + 1;
631 req->in.args[1].value = entry->d_name.name;
632 return create_new_entry(fc, req, dir, entry, mode);
635 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
638 return fuse_mknod(dir, entry, mode, 0);
641 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
643 struct fuse_mkdir_in inarg;
644 struct fuse_conn *fc = get_fuse_conn(dir);
645 struct fuse_req *req = fuse_get_req_nopages(fc);
650 mode &= ~current_umask();
652 memset(&inarg, 0, sizeof(inarg));
654 inarg.umask = current_umask();
655 req->in.h.opcode = FUSE_MKDIR;
657 req->in.args[0].size = sizeof(inarg);
658 req->in.args[0].value = &inarg;
659 req->in.args[1].size = entry->d_name.len + 1;
660 req->in.args[1].value = entry->d_name.name;
661 return create_new_entry(fc, req, 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;
669 struct fuse_req *req = fuse_get_req_nopages(fc);
673 req->in.h.opcode = FUSE_SYMLINK;
675 req->in.args[0].size = entry->d_name.len + 1;
676 req->in.args[0].value = entry->d_name.name;
677 req->in.args[1].size = len;
678 req->in.args[1].value = link;
679 return create_new_entry(fc, req, dir, entry, S_IFLNK);
682 static int fuse_unlink(struct inode *dir, struct dentry *entry)
685 struct fuse_conn *fc = get_fuse_conn(dir);
686 struct fuse_req *req = fuse_get_req_nopages(fc);
690 req->in.h.opcode = FUSE_UNLINK;
691 req->in.h.nodeid = get_node_id(dir);
693 req->in.args[0].size = entry->d_name.len + 1;
694 req->in.args[0].value = entry->d_name.name;
695 fuse_request_send(fc, req);
696 err = req->out.h.error;
697 fuse_put_request(fc, req);
699 struct inode *inode = entry->d_inode;
700 struct fuse_inode *fi = get_fuse_inode(inode);
702 spin_lock(&fc->lock);
703 fi->attr_version = ++fc->attr_version;
705 * If i_nlink == 0 then unlink doesn't make sense, yet this can
706 * happen if userspace filesystem is careless. It would be
707 * difficult to enforce correct nlink usage so just ignore this
710 if (inode->i_nlink > 0)
712 spin_unlock(&fc->lock);
713 fuse_invalidate_attr(inode);
714 fuse_invalidate_attr(dir);
715 fuse_invalidate_entry_cache(entry);
716 } else if (err == -EINTR)
717 fuse_invalidate_entry(entry);
721 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
724 struct fuse_conn *fc = get_fuse_conn(dir);
725 struct fuse_req *req = fuse_get_req_nopages(fc);
729 req->in.h.opcode = FUSE_RMDIR;
730 req->in.h.nodeid = get_node_id(dir);
732 req->in.args[0].size = entry->d_name.len + 1;
733 req->in.args[0].value = entry->d_name.name;
734 fuse_request_send(fc, req);
735 err = req->out.h.error;
736 fuse_put_request(fc, req);
738 clear_nlink(entry->d_inode);
739 fuse_invalidate_attr(dir);
740 fuse_invalidate_entry_cache(entry);
741 } else if (err == -EINTR)
742 fuse_invalidate_entry(entry);
746 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
747 struct inode *newdir, struct dentry *newent)
750 struct fuse_rename_in inarg;
751 struct fuse_conn *fc = get_fuse_conn(olddir);
752 struct fuse_req *req = fuse_get_req_nopages(fc);
757 memset(&inarg, 0, sizeof(inarg));
758 inarg.newdir = get_node_id(newdir);
759 req->in.h.opcode = FUSE_RENAME;
760 req->in.h.nodeid = get_node_id(olddir);
762 req->in.args[0].size = sizeof(inarg);
763 req->in.args[0].value = &inarg;
764 req->in.args[1].size = oldent->d_name.len + 1;
765 req->in.args[1].value = oldent->d_name.name;
766 req->in.args[2].size = newent->d_name.len + 1;
767 req->in.args[2].value = newent->d_name.name;
768 fuse_request_send(fc, req);
769 err = req->out.h.error;
770 fuse_put_request(fc, req);
773 fuse_invalidate_attr(oldent->d_inode);
775 fuse_invalidate_attr(olddir);
776 if (olddir != newdir)
777 fuse_invalidate_attr(newdir);
779 /* newent will end up negative */
780 if (newent->d_inode) {
781 fuse_invalidate_attr(newent->d_inode);
782 fuse_invalidate_entry_cache(newent);
784 } else if (err == -EINTR) {
785 /* If request was interrupted, DEITY only knows if the
786 rename actually took place. If the invalidation
787 fails (e.g. some process has CWD under the renamed
788 directory), then there can be inconsistency between
789 the dcache and the real filesystem. Tough luck. */
790 fuse_invalidate_entry(oldent);
792 fuse_invalidate_entry(newent);
798 static int fuse_link(struct dentry *entry, struct inode *newdir,
799 struct dentry *newent)
802 struct fuse_link_in inarg;
803 struct inode *inode = entry->d_inode;
804 struct fuse_conn *fc = get_fuse_conn(inode);
805 struct fuse_req *req = fuse_get_req_nopages(fc);
809 memset(&inarg, 0, sizeof(inarg));
810 inarg.oldnodeid = get_node_id(inode);
811 req->in.h.opcode = FUSE_LINK;
813 req->in.args[0].size = sizeof(inarg);
814 req->in.args[0].value = &inarg;
815 req->in.args[1].size = newent->d_name.len + 1;
816 req->in.args[1].value = newent->d_name.name;
817 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
818 /* Contrary to "normal" filesystems it can happen that link
819 makes two "logical" inodes point to the same "physical"
820 inode. We invalidate the attributes of the old one, so it
821 will reflect changes in the backing inode (link count,
825 struct fuse_inode *fi = get_fuse_inode(inode);
827 spin_lock(&fc->lock);
828 fi->attr_version = ++fc->attr_version;
830 spin_unlock(&fc->lock);
831 fuse_invalidate_attr(inode);
832 } else if (err == -EINTR) {
833 fuse_invalidate_attr(inode);
838 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
841 unsigned int blkbits;
842 struct fuse_conn *fc = get_fuse_conn(inode);
844 /* see the comment in fuse_change_attributes() */
845 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
846 attr->size = i_size_read(inode);
847 attr->mtime = inode->i_mtime.tv_sec;
848 attr->mtimensec = inode->i_mtime.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);
882 struct fuse_req *req;
885 req = fuse_get_req_nopages(fc);
889 attr_version = fuse_get_attr_version(fc);
891 memset(&inarg, 0, sizeof(inarg));
892 memset(&outarg, 0, sizeof(outarg));
893 /* Directories have separate file-handle space */
894 if (file && S_ISREG(inode->i_mode)) {
895 struct fuse_file *ff = file->private_data;
897 inarg.getattr_flags |= FUSE_GETATTR_FH;
900 req->in.h.opcode = FUSE_GETATTR;
901 req->in.h.nodeid = get_node_id(inode);
903 req->in.args[0].size = sizeof(inarg);
904 req->in.args[0].value = &inarg;
905 req->out.numargs = 1;
907 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
909 req->out.args[0].size = sizeof(outarg);
910 req->out.args[0].value = &outarg;
911 fuse_request_send(fc, req);
912 err = req->out.h.error;
913 fuse_put_request(fc, req);
915 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
916 make_bad_inode(inode);
919 fuse_change_attributes(inode, &outarg.attr,
920 attr_timeout(&outarg),
923 fuse_fillattr(inode, &outarg.attr, stat);
929 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
930 struct file *file, bool *refreshed)
932 struct fuse_inode *fi = get_fuse_inode(inode);
936 if (fi->i_time < get_jiffies_64()) {
938 err = fuse_do_getattr(inode, stat, file);
943 generic_fillattr(inode, stat);
944 stat->mode = fi->orig_i_mode;
945 stat->ino = fi->orig_ino;
949 if (refreshed != NULL)
955 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
956 u64 child_nodeid, struct qstr *name)
959 struct inode *parent;
961 struct dentry *entry;
963 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
967 mutex_lock(&parent->i_mutex);
968 if (!S_ISDIR(parent->i_mode))
972 dir = d_find_alias(parent);
976 entry = d_lookup(dir, name);
981 fuse_invalidate_attr(parent);
982 fuse_invalidate_entry(entry);
984 if (child_nodeid != 0 && entry->d_inode) {
985 mutex_lock(&entry->d_inode->i_mutex);
986 if (get_node_id(entry->d_inode) != child_nodeid) {
990 if (d_mountpoint(entry)) {
994 if (S_ISDIR(entry->d_inode->i_mode)) {
995 shrink_dcache_parent(entry);
996 if (!simple_empty(entry)) {
1000 entry->d_inode->i_flags |= S_DEAD;
1003 clear_nlink(entry->d_inode);
1006 mutex_unlock(&entry->d_inode->i_mutex);
1015 mutex_unlock(&parent->i_mutex);
1021 * Calling into a user-controlled filesystem gives the filesystem
1022 * daemon ptrace-like capabilities over the current process. This
1023 * means, that the filesystem daemon is able to record the exact
1024 * filesystem operations performed, and can also control the behavior
1025 * of the requester process in otherwise impossible ways. For example
1026 * it can delay the operation for arbitrary length of time allowing
1027 * DoS against the requester.
1029 * For this reason only those processes can call into the filesystem,
1030 * for which the owner of the mount has ptrace privilege. This
1031 * excludes processes started by other users, suid or sgid processes.
1033 int fuse_allow_current_process(struct fuse_conn *fc)
1035 const struct cred *cred;
1037 if (fc->flags & FUSE_ALLOW_OTHER)
1040 cred = current_cred();
1041 if (uid_eq(cred->euid, fc->user_id) &&
1042 uid_eq(cred->suid, fc->user_id) &&
1043 uid_eq(cred->uid, fc->user_id) &&
1044 gid_eq(cred->egid, fc->group_id) &&
1045 gid_eq(cred->sgid, fc->group_id) &&
1046 gid_eq(cred->gid, fc->group_id))
1052 static int fuse_access(struct inode *inode, int mask)
1054 struct fuse_conn *fc = get_fuse_conn(inode);
1055 struct fuse_req *req;
1056 struct fuse_access_in inarg;
1059 BUG_ON(mask & MAY_NOT_BLOCK);
1064 req = fuse_get_req_nopages(fc);
1066 return PTR_ERR(req);
1068 memset(&inarg, 0, sizeof(inarg));
1069 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1070 req->in.h.opcode = FUSE_ACCESS;
1071 req->in.h.nodeid = get_node_id(inode);
1072 req->in.numargs = 1;
1073 req->in.args[0].size = sizeof(inarg);
1074 req->in.args[0].value = &inarg;
1075 fuse_request_send(fc, req);
1076 err = req->out.h.error;
1077 fuse_put_request(fc, req);
1078 if (err == -ENOSYS) {
1085 static int fuse_perm_getattr(struct inode *inode, int mask)
1087 if (mask & MAY_NOT_BLOCK)
1090 return fuse_do_getattr(inode, NULL, NULL);
1094 * Check permission. The two basic access models of FUSE are:
1096 * 1) Local access checking ('default_permissions' mount option) based
1097 * on file mode. This is the plain old disk filesystem permission
1100 * 2) "Remote" access checking, where server is responsible for
1101 * checking permission in each inode operation. An exception to this
1102 * is if ->permission() was invoked from sys_access() in which case an
1103 * access request is sent. Execute permission is still checked
1104 * locally based on file mode.
1106 static int fuse_permission(struct inode *inode, int mask)
1108 struct fuse_conn *fc = get_fuse_conn(inode);
1109 bool refreshed = false;
1112 if (!fuse_allow_current_process(fc))
1116 * If attributes are needed, refresh them before proceeding
1118 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1119 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1120 struct fuse_inode *fi = get_fuse_inode(inode);
1122 if (fi->i_time < get_jiffies_64()) {
1125 err = fuse_perm_getattr(inode, mask);
1131 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1132 err = generic_permission(inode, mask);
1134 /* If permission is denied, try to refresh file
1135 attributes. This is also needed, because the root
1136 node will at first have no permissions */
1137 if (err == -EACCES && !refreshed) {
1138 err = fuse_perm_getattr(inode, mask);
1140 err = generic_permission(inode, mask);
1143 /* Note: the opposite of the above test does not
1144 exist. So if permissions are revoked this won't be
1145 noticed immediately, only after the attribute
1146 timeout has expired */
1147 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1148 err = fuse_access(inode, mask);
1149 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1150 if (!(inode->i_mode & S_IXUGO)) {
1154 err = fuse_perm_getattr(inode, mask);
1155 if (!err && !(inode->i_mode & S_IXUGO))
1162 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1163 struct dir_context *ctx)
1165 while (nbytes >= FUSE_NAME_OFFSET) {
1166 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1167 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1168 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1170 if (reclen > nbytes)
1172 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1175 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1176 dirent->ino, dirent->type))
1181 ctx->pos = dirent->off;
1187 static int fuse_direntplus_link(struct file *file,
1188 struct fuse_direntplus *direntplus,
1192 struct fuse_entry_out *o = &direntplus->entry_out;
1193 struct fuse_dirent *dirent = &direntplus->dirent;
1194 struct dentry *parent = file->f_path.dentry;
1195 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1196 struct dentry *dentry;
1197 struct dentry *alias;
1198 struct inode *dir = parent->d_inode;
1199 struct fuse_conn *fc;
1200 struct inode *inode;
1204 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1205 * ENOENT. Instead, it only means the userspace filesystem did
1206 * not want to return attributes/handle for this entry.
1213 if (name.name[0] == '.') {
1215 * We could potentially refresh the attributes of the directory
1220 if (name.name[1] == '.' && name.len == 2)
1224 if (invalid_nodeid(o->nodeid))
1226 if (!fuse_valid_type(o->attr.mode))
1229 fc = get_fuse_conn(dir);
1231 name.hash = full_name_hash(name.name, name.len);
1232 dentry = d_lookup(parent, &name);
1234 inode = dentry->d_inode;
1237 } else if (get_node_id(inode) != o->nodeid ||
1238 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1239 err = d_invalidate(dentry);
1242 } else if (is_bad_inode(inode)) {
1246 struct fuse_inode *fi;
1247 fi = get_fuse_inode(inode);
1248 spin_lock(&fc->lock);
1250 spin_unlock(&fc->lock);
1252 fuse_change_attributes(inode, &o->attr,
1253 entry_attr_timeout(o),
1257 * The other branch to 'found' comes via fuse_iget()
1258 * which bumps nlookup inside
1265 dentry = d_alloc(parent, &name);
1270 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1271 &o->attr, entry_attr_timeout(o), attr_version);
1275 alias = d_materialise_unique(dentry, inode);
1276 err = PTR_ERR(alias);
1286 if (fc->readdirplus_auto)
1287 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1288 fuse_change_entry_timeout(dentry, o);
1296 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1297 struct dir_context *ctx, u64 attr_version)
1299 struct fuse_direntplus *direntplus;
1300 struct fuse_dirent *dirent;
1305 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1306 direntplus = (struct fuse_direntplus *) buf;
1307 dirent = &direntplus->dirent;
1308 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1310 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1312 if (reclen > nbytes)
1314 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1318 /* We fill entries into dstbuf only as much as
1319 it can hold. But we still continue iterating
1320 over remaining entries to link them. If not,
1321 we need to send a FORGET for each of those
1322 which we did not link.
1324 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1325 dirent->ino, dirent->type);
1326 ctx->pos = dirent->off;
1332 ret = fuse_direntplus_link(file, direntplus, attr_version);
1334 fuse_force_forget(file, direntplus->entry_out.nodeid);
1340 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1345 struct inode *inode = file_inode(file);
1346 struct fuse_conn *fc = get_fuse_conn(inode);
1347 struct fuse_req *req;
1348 u64 attr_version = 0;
1350 if (is_bad_inode(inode))
1353 req = fuse_get_req(fc, 1);
1355 return PTR_ERR(req);
1357 page = alloc_page(GFP_KERNEL);
1359 fuse_put_request(fc, req);
1363 plus = fuse_use_readdirplus(inode, ctx);
1364 req->out.argpages = 1;
1366 req->pages[0] = page;
1367 req->page_descs[0].length = PAGE_SIZE;
1369 attr_version = fuse_get_attr_version(fc);
1370 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1373 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1376 fuse_request_send(fc, req);
1377 nbytes = req->out.args[0].size;
1378 err = req->out.h.error;
1379 fuse_put_request(fc, req);
1382 err = parse_dirplusfile(page_address(page), nbytes,
1386 err = parse_dirfile(page_address(page), nbytes, file,
1392 fuse_invalidate_atime(inode);
1396 static char *read_link(struct dentry *dentry)
1398 struct inode *inode = dentry->d_inode;
1399 struct fuse_conn *fc = get_fuse_conn(inode);
1400 struct fuse_req *req = fuse_get_req_nopages(fc);
1404 return ERR_CAST(req);
1406 link = (char *) __get_free_page(GFP_KERNEL);
1408 link = ERR_PTR(-ENOMEM);
1411 req->in.h.opcode = FUSE_READLINK;
1412 req->in.h.nodeid = get_node_id(inode);
1413 req->out.argvar = 1;
1414 req->out.numargs = 1;
1415 req->out.args[0].size = PAGE_SIZE - 1;
1416 req->out.args[0].value = link;
1417 fuse_request_send(fc, req);
1418 if (req->out.h.error) {
1419 free_page((unsigned long) link);
1420 link = ERR_PTR(req->out.h.error);
1422 link[req->out.args[0].size] = '\0';
1424 fuse_put_request(fc, req);
1425 fuse_invalidate_atime(inode);
1429 static void free_link(char *link)
1432 free_page((unsigned long) link);
1435 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1437 nd_set_link(nd, read_link(dentry));
1441 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1443 free_link(nd_get_link(nd));
1446 static int fuse_dir_open(struct inode *inode, struct file *file)
1448 return fuse_open_common(inode, file, true);
1451 static int fuse_dir_release(struct inode *inode, struct file *file)
1453 fuse_release_common(file, FUSE_RELEASEDIR);
1458 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1461 return fuse_fsync_common(file, start, end, datasync, 1);
1464 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1467 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1469 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1473 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1476 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1479 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1484 return fuse_ioctl_common(file, cmd, arg,
1485 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1488 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1490 /* Always update if mtime is explicitly set */
1491 if (ivalid & ATTR_MTIME_SET)
1494 /* Or if kernel i_mtime is the official one */
1495 if (trust_local_mtime)
1498 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1499 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1502 /* In all other cases update */
1506 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1507 bool trust_local_mtime)
1509 unsigned ivalid = iattr->ia_valid;
1511 if (ivalid & ATTR_MODE)
1512 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1513 if (ivalid & ATTR_UID)
1514 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1515 if (ivalid & ATTR_GID)
1516 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1517 if (ivalid & ATTR_SIZE)
1518 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1519 if (ivalid & ATTR_ATIME) {
1520 arg->valid |= FATTR_ATIME;
1521 arg->atime = iattr->ia_atime.tv_sec;
1522 arg->atimensec = iattr->ia_atime.tv_nsec;
1523 if (!(ivalid & ATTR_ATIME_SET))
1524 arg->valid |= FATTR_ATIME_NOW;
1526 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_mtime)) {
1527 arg->valid |= FATTR_MTIME;
1528 arg->mtime = iattr->ia_mtime.tv_sec;
1529 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1530 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_mtime)
1531 arg->valid |= FATTR_MTIME_NOW;
1536 * Prevent concurrent writepages on inode
1538 * This is done by adding a negative bias to the inode write counter
1539 * and waiting for all pending writes to finish.
1541 void fuse_set_nowrite(struct inode *inode)
1543 struct fuse_conn *fc = get_fuse_conn(inode);
1544 struct fuse_inode *fi = get_fuse_inode(inode);
1546 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1548 spin_lock(&fc->lock);
1549 BUG_ON(fi->writectr < 0);
1550 fi->writectr += FUSE_NOWRITE;
1551 spin_unlock(&fc->lock);
1552 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1556 * Allow writepages on inode
1558 * Remove the bias from the writecounter and send any queued
1561 static void __fuse_release_nowrite(struct inode *inode)
1563 struct fuse_inode *fi = get_fuse_inode(inode);
1565 BUG_ON(fi->writectr != FUSE_NOWRITE);
1567 fuse_flush_writepages(inode);
1570 void fuse_release_nowrite(struct inode *inode)
1572 struct fuse_conn *fc = get_fuse_conn(inode);
1574 spin_lock(&fc->lock);
1575 __fuse_release_nowrite(inode);
1576 spin_unlock(&fc->lock);
1579 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1580 struct inode *inode,
1581 struct fuse_setattr_in *inarg_p,
1582 struct fuse_attr_out *outarg_p)
1584 req->in.h.opcode = FUSE_SETATTR;
1585 req->in.h.nodeid = get_node_id(inode);
1586 req->in.numargs = 1;
1587 req->in.args[0].size = sizeof(*inarg_p);
1588 req->in.args[0].value = inarg_p;
1589 req->out.numargs = 1;
1591 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1593 req->out.args[0].size = sizeof(*outarg_p);
1594 req->out.args[0].value = outarg_p;
1598 * Flush inode->i_mtime to the server
1600 int fuse_flush_mtime(struct file *file, bool nofail)
1602 struct inode *inode = file->f_mapping->host;
1603 struct fuse_inode *fi = get_fuse_inode(inode);
1604 struct fuse_conn *fc = get_fuse_conn(inode);
1605 struct fuse_req *req = NULL;
1606 struct fuse_setattr_in inarg;
1607 struct fuse_attr_out outarg;
1611 req = fuse_get_req_nofail_nopages(fc, file);
1613 req = fuse_get_req_nopages(fc);
1615 return PTR_ERR(req);
1618 memset(&inarg, 0, sizeof(inarg));
1619 memset(&outarg, 0, sizeof(outarg));
1621 inarg.valid |= FATTR_MTIME;
1622 inarg.mtime = inode->i_mtime.tv_sec;
1623 inarg.mtimensec = inode->i_mtime.tv_nsec;
1625 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1626 fuse_request_send(fc, req);
1627 err = req->out.h.error;
1628 fuse_put_request(fc, req);
1631 clear_bit(FUSE_I_MTIME_DIRTY, &fi->state);
1637 * Set attributes, and at the same time refresh them.
1639 * Truncation is slightly complicated, because the 'truncate' request
1640 * may fail, in which case we don't want to touch the mapping.
1641 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1642 * and the actual truncation by hand.
1644 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1647 struct fuse_conn *fc = get_fuse_conn(inode);
1648 struct fuse_inode *fi = get_fuse_inode(inode);
1649 struct fuse_req *req;
1650 struct fuse_setattr_in inarg;
1651 struct fuse_attr_out outarg;
1652 bool is_truncate = false;
1653 bool is_wb = fc->writeback_cache;
1656 bool trust_local_mtime = is_wb && S_ISREG(inode->i_mode);
1658 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1659 attr->ia_valid |= ATTR_FORCE;
1661 err = inode_change_ok(inode, attr);
1665 if (attr->ia_valid & ATTR_OPEN) {
1666 if (fc->atomic_o_trunc)
1671 if (attr->ia_valid & ATTR_SIZE)
1674 req = fuse_get_req_nopages(fc);
1676 return PTR_ERR(req);
1679 fuse_set_nowrite(inode);
1680 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1683 memset(&inarg, 0, sizeof(inarg));
1684 memset(&outarg, 0, sizeof(outarg));
1685 iattr_to_fattr(attr, &inarg, trust_local_mtime);
1687 struct fuse_file *ff = file->private_data;
1688 inarg.valid |= FATTR_FH;
1691 if (attr->ia_valid & ATTR_SIZE) {
1692 /* For mandatory locking in truncate */
1693 inarg.valid |= FATTR_LOCKOWNER;
1694 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1696 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1697 fuse_request_send(fc, req);
1698 err = req->out.h.error;
1699 fuse_put_request(fc, req);
1702 fuse_invalidate_attr(inode);
1706 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1707 make_bad_inode(inode);
1712 spin_lock(&fc->lock);
1713 /* the kernel maintains i_mtime locally */
1714 if (trust_local_mtime && (attr->ia_valid & ATTR_MTIME)) {
1715 inode->i_mtime = attr->ia_mtime;
1716 clear_bit(FUSE_I_MTIME_DIRTY, &fi->state);
1719 fuse_change_attributes_common(inode, &outarg.attr,
1720 attr_timeout(&outarg));
1721 oldsize = inode->i_size;
1722 /* see the comment in fuse_change_attributes() */
1723 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1724 i_size_write(inode, outarg.attr.size);
1727 /* NOTE: this may release/reacquire fc->lock */
1728 __fuse_release_nowrite(inode);
1730 spin_unlock(&fc->lock);
1733 * Only call invalidate_inode_pages2() after removing
1734 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1736 if ((is_truncate || !is_wb) &&
1737 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1738 truncate_pagecache(inode, outarg.attr.size);
1739 invalidate_inode_pages2(inode->i_mapping);
1742 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1747 fuse_release_nowrite(inode);
1749 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1753 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1755 struct inode *inode = entry->d_inode;
1757 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1760 if (attr->ia_valid & ATTR_FILE)
1761 return fuse_do_setattr(inode, attr, attr->ia_file);
1763 return fuse_do_setattr(inode, attr, NULL);
1766 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1769 struct inode *inode = entry->d_inode;
1770 struct fuse_conn *fc = get_fuse_conn(inode);
1772 if (!fuse_allow_current_process(fc))
1775 return fuse_update_attributes(inode, stat, NULL, NULL);
1778 static int fuse_setxattr(struct dentry *entry, const char *name,
1779 const void *value, size_t size, int flags)
1781 struct inode *inode = entry->d_inode;
1782 struct fuse_conn *fc = get_fuse_conn(inode);
1783 struct fuse_req *req;
1784 struct fuse_setxattr_in inarg;
1787 if (fc->no_setxattr)
1790 req = fuse_get_req_nopages(fc);
1792 return PTR_ERR(req);
1794 memset(&inarg, 0, sizeof(inarg));
1796 inarg.flags = flags;
1797 req->in.h.opcode = FUSE_SETXATTR;
1798 req->in.h.nodeid = get_node_id(inode);
1799 req->in.numargs = 3;
1800 req->in.args[0].size = sizeof(inarg);
1801 req->in.args[0].value = &inarg;
1802 req->in.args[1].size = strlen(name) + 1;
1803 req->in.args[1].value = name;
1804 req->in.args[2].size = size;
1805 req->in.args[2].value = value;
1806 fuse_request_send(fc, req);
1807 err = req->out.h.error;
1808 fuse_put_request(fc, req);
1809 if (err == -ENOSYS) {
1810 fc->no_setxattr = 1;
1814 fuse_invalidate_attr(inode);
1818 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1819 void *value, size_t size)
1821 struct inode *inode = entry->d_inode;
1822 struct fuse_conn *fc = get_fuse_conn(inode);
1823 struct fuse_req *req;
1824 struct fuse_getxattr_in inarg;
1825 struct fuse_getxattr_out outarg;
1828 if (fc->no_getxattr)
1831 req = fuse_get_req_nopages(fc);
1833 return PTR_ERR(req);
1835 memset(&inarg, 0, sizeof(inarg));
1837 req->in.h.opcode = FUSE_GETXATTR;
1838 req->in.h.nodeid = get_node_id(inode);
1839 req->in.numargs = 2;
1840 req->in.args[0].size = sizeof(inarg);
1841 req->in.args[0].value = &inarg;
1842 req->in.args[1].size = strlen(name) + 1;
1843 req->in.args[1].value = name;
1844 /* This is really two different operations rolled into one */
1845 req->out.numargs = 1;
1847 req->out.argvar = 1;
1848 req->out.args[0].size = size;
1849 req->out.args[0].value = value;
1851 req->out.args[0].size = sizeof(outarg);
1852 req->out.args[0].value = &outarg;
1854 fuse_request_send(fc, req);
1855 ret = req->out.h.error;
1857 ret = size ? req->out.args[0].size : outarg.size;
1859 if (ret == -ENOSYS) {
1860 fc->no_getxattr = 1;
1864 fuse_put_request(fc, req);
1868 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1870 struct inode *inode = entry->d_inode;
1871 struct fuse_conn *fc = get_fuse_conn(inode);
1872 struct fuse_req *req;
1873 struct fuse_getxattr_in inarg;
1874 struct fuse_getxattr_out outarg;
1877 if (!fuse_allow_current_process(fc))
1880 if (fc->no_listxattr)
1883 req = fuse_get_req_nopages(fc);
1885 return PTR_ERR(req);
1887 memset(&inarg, 0, sizeof(inarg));
1889 req->in.h.opcode = FUSE_LISTXATTR;
1890 req->in.h.nodeid = get_node_id(inode);
1891 req->in.numargs = 1;
1892 req->in.args[0].size = sizeof(inarg);
1893 req->in.args[0].value = &inarg;
1894 /* This is really two different operations rolled into one */
1895 req->out.numargs = 1;
1897 req->out.argvar = 1;
1898 req->out.args[0].size = size;
1899 req->out.args[0].value = list;
1901 req->out.args[0].size = sizeof(outarg);
1902 req->out.args[0].value = &outarg;
1904 fuse_request_send(fc, req);
1905 ret = req->out.h.error;
1907 ret = size ? req->out.args[0].size : outarg.size;
1909 if (ret == -ENOSYS) {
1910 fc->no_listxattr = 1;
1914 fuse_put_request(fc, req);
1918 static int fuse_removexattr(struct dentry *entry, const char *name)
1920 struct inode *inode = entry->d_inode;
1921 struct fuse_conn *fc = get_fuse_conn(inode);
1922 struct fuse_req *req;
1925 if (fc->no_removexattr)
1928 req = fuse_get_req_nopages(fc);
1930 return PTR_ERR(req);
1932 req->in.h.opcode = FUSE_REMOVEXATTR;
1933 req->in.h.nodeid = get_node_id(inode);
1934 req->in.numargs = 1;
1935 req->in.args[0].size = strlen(name) + 1;
1936 req->in.args[0].value = name;
1937 fuse_request_send(fc, req);
1938 err = req->out.h.error;
1939 fuse_put_request(fc, req);
1940 if (err == -ENOSYS) {
1941 fc->no_removexattr = 1;
1945 fuse_invalidate_attr(inode);
1949 static int fuse_update_time(struct inode *inode, struct timespec *now,
1952 if (flags & S_MTIME) {
1953 inode->i_mtime = *now;
1954 set_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state);
1955 BUG_ON(!S_ISREG(inode->i_mode));
1960 static const struct inode_operations fuse_dir_inode_operations = {
1961 .lookup = fuse_lookup,
1962 .mkdir = fuse_mkdir,
1963 .symlink = fuse_symlink,
1964 .unlink = fuse_unlink,
1965 .rmdir = fuse_rmdir,
1966 .rename = fuse_rename,
1968 .setattr = fuse_setattr,
1969 .create = fuse_create,
1970 .atomic_open = fuse_atomic_open,
1971 .mknod = fuse_mknod,
1972 .permission = fuse_permission,
1973 .getattr = fuse_getattr,
1974 .setxattr = fuse_setxattr,
1975 .getxattr = fuse_getxattr,
1976 .listxattr = fuse_listxattr,
1977 .removexattr = fuse_removexattr,
1980 static const struct file_operations fuse_dir_operations = {
1981 .llseek = generic_file_llseek,
1982 .read = generic_read_dir,
1983 .iterate = fuse_readdir,
1984 .open = fuse_dir_open,
1985 .release = fuse_dir_release,
1986 .fsync = fuse_dir_fsync,
1987 .unlocked_ioctl = fuse_dir_ioctl,
1988 .compat_ioctl = fuse_dir_compat_ioctl,
1991 static const struct inode_operations fuse_common_inode_operations = {
1992 .setattr = fuse_setattr,
1993 .permission = fuse_permission,
1994 .getattr = fuse_getattr,
1995 .setxattr = fuse_setxattr,
1996 .getxattr = fuse_getxattr,
1997 .listxattr = fuse_listxattr,
1998 .removexattr = fuse_removexattr,
1999 .update_time = fuse_update_time,
2002 static const struct inode_operations fuse_symlink_inode_operations = {
2003 .setattr = fuse_setattr,
2004 .follow_link = fuse_follow_link,
2005 .put_link = fuse_put_link,
2006 .readlink = generic_readlink,
2007 .getattr = fuse_getattr,
2008 .setxattr = fuse_setxattr,
2009 .getxattr = fuse_getxattr,
2010 .listxattr = fuse_listxattr,
2011 .removexattr = fuse_removexattr,
2014 void fuse_init_common(struct inode *inode)
2016 inode->i_op = &fuse_common_inode_operations;
2019 void fuse_init_dir(struct inode *inode)
2021 inode->i_op = &fuse_dir_inode_operations;
2022 inode->i_fop = &fuse_dir_operations;
2025 void fuse_init_symlink(struct inode *inode)
2027 inode->i_op = &fuse_symlink_inode_operations;