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 (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);
278 if (!(flags & LOOKUP_RCU) && check_submounts_and_drop(entry) != 0)
283 static int invalid_nodeid(u64 nodeid)
285 return !nodeid || nodeid == FUSE_ROOT_ID;
288 const struct dentry_operations fuse_dentry_operations = {
289 .d_revalidate = fuse_dentry_revalidate,
292 int fuse_valid_type(int m)
294 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
295 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
298 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
299 struct fuse_entry_out *outarg, struct inode **inode)
301 struct fuse_conn *fc = get_fuse_conn_super(sb);
302 struct fuse_req *req;
303 struct fuse_forget_link *forget;
309 if (name->len > FUSE_NAME_MAX)
312 req = fuse_get_req_nopages(fc);
317 forget = fuse_alloc_forget();
320 fuse_put_request(fc, req);
324 attr_version = fuse_get_attr_version(fc);
326 fuse_lookup_init(fc, req, nodeid, name, outarg);
327 fuse_request_send(fc, req);
328 err = req->out.h.error;
329 fuse_put_request(fc, req);
330 /* Zero nodeid is same as -ENOENT, but with valid timeout */
331 if (err || !outarg->nodeid)
337 if (!fuse_valid_type(outarg->attr.mode))
340 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
341 &outarg->attr, entry_attr_timeout(outarg),
345 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
356 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
360 struct fuse_entry_out outarg;
362 struct dentry *newent;
363 bool outarg_valid = true;
365 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
367 if (err == -ENOENT) {
368 outarg_valid = false;
375 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
378 newent = d_materialise_unique(entry, inode);
379 err = PTR_ERR(newent);
383 entry = newent ? newent : entry;
385 fuse_change_entry_timeout(entry, &outarg);
387 fuse_invalidate_entry_cache(entry);
389 fuse_advise_use_readdirplus(dir);
399 * Atomic create+open operation
401 * If the filesystem doesn't support this, then fall back to separate
402 * 'mknod' + 'open' requests.
404 static int fuse_create_open(struct inode *dir, struct dentry *entry,
405 struct file *file, unsigned flags,
406 umode_t mode, int *opened)
410 struct fuse_conn *fc = get_fuse_conn(dir);
411 struct fuse_req *req;
412 struct fuse_forget_link *forget;
413 struct fuse_create_in inarg;
414 struct fuse_open_out outopen;
415 struct fuse_entry_out outentry;
416 struct fuse_file *ff;
418 /* Userspace expects S_IFREG in create mode */
419 BUG_ON((mode & S_IFMT) != S_IFREG);
421 forget = fuse_alloc_forget();
426 req = fuse_get_req_nopages(fc);
429 goto out_put_forget_req;
432 ff = fuse_file_alloc(fc);
434 goto out_put_request;
437 mode &= ~current_umask();
440 memset(&inarg, 0, sizeof(inarg));
441 memset(&outentry, 0, sizeof(outentry));
444 inarg.umask = current_umask();
445 req->in.h.opcode = FUSE_CREATE;
446 req->in.h.nodeid = get_node_id(dir);
448 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
450 req->in.args[0].value = &inarg;
451 req->in.args[1].size = entry->d_name.len + 1;
452 req->in.args[1].value = entry->d_name.name;
453 req->out.numargs = 2;
455 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
457 req->out.args[0].size = sizeof(outentry);
458 req->out.args[0].value = &outentry;
459 req->out.args[1].size = sizeof(outopen);
460 req->out.args[1].value = &outopen;
461 fuse_request_send(fc, req);
462 err = req->out.h.error;
467 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
470 fuse_put_request(fc, req);
472 ff->nodeid = outentry.nodeid;
473 ff->open_flags = outopen.open_flags;
474 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
475 &outentry.attr, entry_attr_timeout(&outentry), 0);
477 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
478 fuse_sync_release(ff, flags);
479 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
484 d_instantiate(entry, inode);
485 fuse_change_entry_timeout(entry, &outentry);
486 fuse_invalidate_attr(dir);
487 err = finish_open(file, entry, generic_file_open, opened);
489 fuse_sync_release(ff, flags);
491 file->private_data = fuse_file_get(ff);
492 fuse_finish_open(inode, file);
499 fuse_put_request(fc, req);
506 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
507 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
508 struct file *file, unsigned flags,
509 umode_t mode, int *opened)
512 struct fuse_conn *fc = get_fuse_conn(dir);
513 struct dentry *res = NULL;
515 if (d_unhashed(entry)) {
516 res = fuse_lookup(dir, entry, 0);
524 if (!(flags & O_CREAT) || entry->d_inode)
528 *opened |= FILE_CREATED;
533 err = fuse_create_open(dir, entry, file, flags, mode, opened);
534 if (err == -ENOSYS) {
543 err = fuse_mknod(dir, entry, mode, 0);
547 return finish_no_open(file, res);
551 * Code shared between mknod, mkdir, symlink and link
553 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
554 struct inode *dir, struct dentry *entry,
557 struct fuse_entry_out outarg;
560 struct fuse_forget_link *forget;
562 forget = fuse_alloc_forget();
564 fuse_put_request(fc, req);
568 memset(&outarg, 0, sizeof(outarg));
569 req->in.h.nodeid = get_node_id(dir);
570 req->out.numargs = 1;
572 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
574 req->out.args[0].size = sizeof(outarg);
575 req->out.args[0].value = &outarg;
576 fuse_request_send(fc, req);
577 err = req->out.h.error;
578 fuse_put_request(fc, req);
580 goto out_put_forget_req;
583 if (invalid_nodeid(outarg.nodeid))
584 goto out_put_forget_req;
586 if ((outarg.attr.mode ^ mode) & S_IFMT)
587 goto out_put_forget_req;
589 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
590 &outarg.attr, entry_attr_timeout(&outarg), 0);
592 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
597 err = d_instantiate_no_diralias(entry, inode);
601 fuse_change_entry_timeout(entry, &outarg);
602 fuse_invalidate_attr(dir);
610 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
613 struct fuse_mknod_in inarg;
614 struct fuse_conn *fc = get_fuse_conn(dir);
615 struct fuse_req *req = fuse_get_req_nopages(fc);
620 mode &= ~current_umask();
622 memset(&inarg, 0, sizeof(inarg));
624 inarg.rdev = new_encode_dev(rdev);
625 inarg.umask = current_umask();
626 req->in.h.opcode = FUSE_MKNOD;
628 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
630 req->in.args[0].value = &inarg;
631 req->in.args[1].size = entry->d_name.len + 1;
632 req->in.args[1].value = entry->d_name.name;
633 return create_new_entry(fc, req, dir, entry, mode);
636 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
639 return fuse_mknod(dir, entry, mode, 0);
642 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
644 struct fuse_mkdir_in inarg;
645 struct fuse_conn *fc = get_fuse_conn(dir);
646 struct fuse_req *req = fuse_get_req_nopages(fc);
651 mode &= ~current_umask();
653 memset(&inarg, 0, sizeof(inarg));
655 inarg.umask = current_umask();
656 req->in.h.opcode = FUSE_MKDIR;
658 req->in.args[0].size = sizeof(inarg);
659 req->in.args[0].value = &inarg;
660 req->in.args[1].size = entry->d_name.len + 1;
661 req->in.args[1].value = entry->d_name.name;
662 return create_new_entry(fc, req, dir, entry, S_IFDIR);
665 static int fuse_symlink(struct inode *dir, struct dentry *entry,
668 struct fuse_conn *fc = get_fuse_conn(dir);
669 unsigned len = strlen(link) + 1;
670 struct fuse_req *req = fuse_get_req_nopages(fc);
674 req->in.h.opcode = FUSE_SYMLINK;
676 req->in.args[0].size = entry->d_name.len + 1;
677 req->in.args[0].value = entry->d_name.name;
678 req->in.args[1].size = len;
679 req->in.args[1].value = link;
680 return create_new_entry(fc, req, dir, entry, S_IFLNK);
683 static inline void fuse_update_ctime(struct inode *inode)
685 if (!IS_NOCMTIME(inode)) {
686 inode->i_ctime = current_fs_time(inode->i_sb);
687 mark_inode_dirty_sync(inode);
691 static int fuse_unlink(struct inode *dir, struct dentry *entry)
694 struct fuse_conn *fc = get_fuse_conn(dir);
695 struct fuse_req *req = fuse_get_req_nopages(fc);
699 req->in.h.opcode = FUSE_UNLINK;
700 req->in.h.nodeid = get_node_id(dir);
702 req->in.args[0].size = entry->d_name.len + 1;
703 req->in.args[0].value = entry->d_name.name;
704 fuse_request_send(fc, req);
705 err = req->out.h.error;
706 fuse_put_request(fc, req);
708 struct inode *inode = entry->d_inode;
709 struct fuse_inode *fi = get_fuse_inode(inode);
711 spin_lock(&fc->lock);
712 fi->attr_version = ++fc->attr_version;
714 * If i_nlink == 0 then unlink doesn't make sense, yet this can
715 * happen if userspace filesystem is careless. It would be
716 * difficult to enforce correct nlink usage so just ignore this
719 if (inode->i_nlink > 0)
721 spin_unlock(&fc->lock);
722 fuse_invalidate_attr(inode);
723 fuse_invalidate_attr(dir);
724 fuse_invalidate_entry_cache(entry);
725 fuse_update_ctime(inode);
726 } else if (err == -EINTR)
727 fuse_invalidate_entry(entry);
731 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
734 struct fuse_conn *fc = get_fuse_conn(dir);
735 struct fuse_req *req = fuse_get_req_nopages(fc);
739 req->in.h.opcode = FUSE_RMDIR;
740 req->in.h.nodeid = get_node_id(dir);
742 req->in.args[0].size = entry->d_name.len + 1;
743 req->in.args[0].value = entry->d_name.name;
744 fuse_request_send(fc, req);
745 err = req->out.h.error;
746 fuse_put_request(fc, req);
748 clear_nlink(entry->d_inode);
749 fuse_invalidate_attr(dir);
750 fuse_invalidate_entry_cache(entry);
751 } else if (err == -EINTR)
752 fuse_invalidate_entry(entry);
756 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
757 struct inode *newdir, struct dentry *newent,
758 unsigned int flags, int opcode, size_t argsize)
761 struct fuse_rename2_in inarg;
762 struct fuse_conn *fc = get_fuse_conn(olddir);
763 struct fuse_req *req;
765 req = fuse_get_req_nopages(fc);
769 memset(&inarg, 0, argsize);
770 inarg.newdir = get_node_id(newdir);
772 req->in.h.opcode = opcode;
773 req->in.h.nodeid = get_node_id(olddir);
775 req->in.args[0].size = argsize;
776 req->in.args[0].value = &inarg;
777 req->in.args[1].size = oldent->d_name.len + 1;
778 req->in.args[1].value = oldent->d_name.name;
779 req->in.args[2].size = newent->d_name.len + 1;
780 req->in.args[2].value = newent->d_name.name;
781 fuse_request_send(fc, req);
782 err = req->out.h.error;
783 fuse_put_request(fc, req);
786 fuse_invalidate_attr(oldent->d_inode);
787 fuse_update_ctime(oldent->d_inode);
789 if (flags & RENAME_EXCHANGE) {
790 fuse_invalidate_attr(newent->d_inode);
791 fuse_update_ctime(newent->d_inode);
794 fuse_invalidate_attr(olddir);
795 if (olddir != newdir)
796 fuse_invalidate_attr(newdir);
798 /* newent will end up negative */
799 if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
800 fuse_invalidate_attr(newent->d_inode);
801 fuse_invalidate_entry_cache(newent);
802 fuse_update_ctime(newent->d_inode);
804 } else if (err == -EINTR) {
805 /* If request was interrupted, DEITY only knows if the
806 rename actually took place. If the invalidation
807 fails (e.g. some process has CWD under the renamed
808 directory), then there can be inconsistency between
809 the dcache and the real filesystem. Tough luck. */
810 fuse_invalidate_entry(oldent);
812 fuse_invalidate_entry(newent);
818 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
819 struct inode *newdir, struct dentry *newent,
822 struct fuse_conn *fc = get_fuse_conn(olddir);
825 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
829 if (fc->no_rename2 || fc->minor < 23)
832 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
834 sizeof(struct fuse_rename2_in));
835 if (err == -ENOSYS) {
840 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
842 sizeof(struct fuse_rename_in));
848 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
849 struct inode *newdir, struct dentry *newent)
851 return fuse_rename2(olddir, oldent, newdir, newent, 0);
854 static int fuse_link(struct dentry *entry, struct inode *newdir,
855 struct dentry *newent)
858 struct fuse_link_in inarg;
859 struct inode *inode = entry->d_inode;
860 struct fuse_conn *fc = get_fuse_conn(inode);
861 struct fuse_req *req = fuse_get_req_nopages(fc);
865 memset(&inarg, 0, sizeof(inarg));
866 inarg.oldnodeid = get_node_id(inode);
867 req->in.h.opcode = FUSE_LINK;
869 req->in.args[0].size = sizeof(inarg);
870 req->in.args[0].value = &inarg;
871 req->in.args[1].size = newent->d_name.len + 1;
872 req->in.args[1].value = newent->d_name.name;
873 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
874 /* Contrary to "normal" filesystems it can happen that link
875 makes two "logical" inodes point to the same "physical"
876 inode. We invalidate the attributes of the old one, so it
877 will reflect changes in the backing inode (link count,
881 struct fuse_inode *fi = get_fuse_inode(inode);
883 spin_lock(&fc->lock);
884 fi->attr_version = ++fc->attr_version;
886 spin_unlock(&fc->lock);
887 fuse_invalidate_attr(inode);
888 fuse_update_ctime(inode);
889 } else if (err == -EINTR) {
890 fuse_invalidate_attr(inode);
895 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
898 unsigned int blkbits;
899 struct fuse_conn *fc = get_fuse_conn(inode);
901 /* see the comment in fuse_change_attributes() */
902 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
903 attr->size = i_size_read(inode);
904 attr->mtime = inode->i_mtime.tv_sec;
905 attr->mtimensec = inode->i_mtime.tv_nsec;
906 attr->ctime = inode->i_ctime.tv_sec;
907 attr->ctimensec = inode->i_ctime.tv_nsec;
910 stat->dev = inode->i_sb->s_dev;
911 stat->ino = attr->ino;
912 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
913 stat->nlink = attr->nlink;
914 stat->uid = make_kuid(&init_user_ns, attr->uid);
915 stat->gid = make_kgid(&init_user_ns, attr->gid);
916 stat->rdev = inode->i_rdev;
917 stat->atime.tv_sec = attr->atime;
918 stat->atime.tv_nsec = attr->atimensec;
919 stat->mtime.tv_sec = attr->mtime;
920 stat->mtime.tv_nsec = attr->mtimensec;
921 stat->ctime.tv_sec = attr->ctime;
922 stat->ctime.tv_nsec = attr->ctimensec;
923 stat->size = attr->size;
924 stat->blocks = attr->blocks;
926 if (attr->blksize != 0)
927 blkbits = ilog2(attr->blksize);
929 blkbits = inode->i_sb->s_blocksize_bits;
931 stat->blksize = 1 << blkbits;
934 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
938 struct fuse_getattr_in inarg;
939 struct fuse_attr_out outarg;
940 struct fuse_conn *fc = get_fuse_conn(inode);
941 struct fuse_req *req;
944 req = fuse_get_req_nopages(fc);
948 attr_version = fuse_get_attr_version(fc);
950 memset(&inarg, 0, sizeof(inarg));
951 memset(&outarg, 0, sizeof(outarg));
952 /* Directories have separate file-handle space */
953 if (file && S_ISREG(inode->i_mode)) {
954 struct fuse_file *ff = file->private_data;
956 inarg.getattr_flags |= FUSE_GETATTR_FH;
959 req->in.h.opcode = FUSE_GETATTR;
960 req->in.h.nodeid = get_node_id(inode);
962 req->in.args[0].size = sizeof(inarg);
963 req->in.args[0].value = &inarg;
964 req->out.numargs = 1;
966 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
968 req->out.args[0].size = sizeof(outarg);
969 req->out.args[0].value = &outarg;
970 fuse_request_send(fc, req);
971 err = req->out.h.error;
972 fuse_put_request(fc, req);
974 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
975 make_bad_inode(inode);
978 fuse_change_attributes(inode, &outarg.attr,
979 attr_timeout(&outarg),
982 fuse_fillattr(inode, &outarg.attr, stat);
988 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
989 struct file *file, bool *refreshed)
991 struct fuse_inode *fi = get_fuse_inode(inode);
995 if (time_before64(fi->i_time, get_jiffies_64())) {
997 err = fuse_do_getattr(inode, stat, file);
1002 generic_fillattr(inode, stat);
1003 stat->mode = fi->orig_i_mode;
1004 stat->ino = fi->orig_ino;
1008 if (refreshed != NULL)
1014 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
1015 u64 child_nodeid, struct qstr *name)
1018 struct inode *parent;
1020 struct dentry *entry;
1022 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1026 mutex_lock(&parent->i_mutex);
1027 if (!S_ISDIR(parent->i_mode))
1031 dir = d_find_alias(parent);
1035 entry = d_lookup(dir, name);
1040 fuse_invalidate_attr(parent);
1041 fuse_invalidate_entry(entry);
1043 if (child_nodeid != 0 && entry->d_inode) {
1044 mutex_lock(&entry->d_inode->i_mutex);
1045 if (get_node_id(entry->d_inode) != child_nodeid) {
1049 if (d_mountpoint(entry)) {
1053 if (S_ISDIR(entry->d_inode->i_mode)) {
1054 shrink_dcache_parent(entry);
1055 if (!simple_empty(entry)) {
1059 entry->d_inode->i_flags |= S_DEAD;
1062 clear_nlink(entry->d_inode);
1065 mutex_unlock(&entry->d_inode->i_mutex);
1074 mutex_unlock(&parent->i_mutex);
1080 * Calling into a user-controlled filesystem gives the filesystem
1081 * daemon ptrace-like capabilities over the current process. This
1082 * means, that the filesystem daemon is able to record the exact
1083 * filesystem operations performed, and can also control the behavior
1084 * of the requester process in otherwise impossible ways. For example
1085 * it can delay the operation for arbitrary length of time allowing
1086 * DoS against the requester.
1088 * For this reason only those processes can call into the filesystem,
1089 * for which the owner of the mount has ptrace privilege. This
1090 * excludes processes started by other users, suid or sgid processes.
1092 int fuse_allow_current_process(struct fuse_conn *fc)
1094 const struct cred *cred;
1096 if (fc->flags & FUSE_ALLOW_OTHER)
1099 cred = current_cred();
1100 if (uid_eq(cred->euid, fc->user_id) &&
1101 uid_eq(cred->suid, fc->user_id) &&
1102 uid_eq(cred->uid, fc->user_id) &&
1103 gid_eq(cred->egid, fc->group_id) &&
1104 gid_eq(cred->sgid, fc->group_id) &&
1105 gid_eq(cred->gid, fc->group_id))
1111 static int fuse_access(struct inode *inode, int mask)
1113 struct fuse_conn *fc = get_fuse_conn(inode);
1114 struct fuse_req *req;
1115 struct fuse_access_in inarg;
1118 BUG_ON(mask & MAY_NOT_BLOCK);
1123 req = fuse_get_req_nopages(fc);
1125 return PTR_ERR(req);
1127 memset(&inarg, 0, sizeof(inarg));
1128 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1129 req->in.h.opcode = FUSE_ACCESS;
1130 req->in.h.nodeid = get_node_id(inode);
1131 req->in.numargs = 1;
1132 req->in.args[0].size = sizeof(inarg);
1133 req->in.args[0].value = &inarg;
1134 fuse_request_send(fc, req);
1135 err = req->out.h.error;
1136 fuse_put_request(fc, req);
1137 if (err == -ENOSYS) {
1144 static int fuse_perm_getattr(struct inode *inode, int mask)
1146 if (mask & MAY_NOT_BLOCK)
1149 return fuse_do_getattr(inode, NULL, NULL);
1153 * Check permission. The two basic access models of FUSE are:
1155 * 1) Local access checking ('default_permissions' mount option) based
1156 * on file mode. This is the plain old disk filesystem permission
1159 * 2) "Remote" access checking, where server is responsible for
1160 * checking permission in each inode operation. An exception to this
1161 * is if ->permission() was invoked from sys_access() in which case an
1162 * access request is sent. Execute permission is still checked
1163 * locally based on file mode.
1165 static int fuse_permission(struct inode *inode, int mask)
1167 struct fuse_conn *fc = get_fuse_conn(inode);
1168 bool refreshed = false;
1171 if (!fuse_allow_current_process(fc))
1175 * If attributes are needed, refresh them before proceeding
1177 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1178 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1179 struct fuse_inode *fi = get_fuse_inode(inode);
1181 if (time_before64(fi->i_time, get_jiffies_64())) {
1184 err = fuse_perm_getattr(inode, mask);
1190 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1191 err = generic_permission(inode, mask);
1193 /* If permission is denied, try to refresh file
1194 attributes. This is also needed, because the root
1195 node will at first have no permissions */
1196 if (err == -EACCES && !refreshed) {
1197 err = fuse_perm_getattr(inode, mask);
1199 err = generic_permission(inode, mask);
1202 /* Note: the opposite of the above test does not
1203 exist. So if permissions are revoked this won't be
1204 noticed immediately, only after the attribute
1205 timeout has expired */
1206 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1207 err = fuse_access(inode, mask);
1208 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1209 if (!(inode->i_mode & S_IXUGO)) {
1213 err = fuse_perm_getattr(inode, mask);
1214 if (!err && !(inode->i_mode & S_IXUGO))
1221 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1222 struct dir_context *ctx)
1224 while (nbytes >= FUSE_NAME_OFFSET) {
1225 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1226 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1227 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1229 if (reclen > nbytes)
1231 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1234 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1235 dirent->ino, dirent->type))
1240 ctx->pos = dirent->off;
1246 static int fuse_direntplus_link(struct file *file,
1247 struct fuse_direntplus *direntplus,
1251 struct fuse_entry_out *o = &direntplus->entry_out;
1252 struct fuse_dirent *dirent = &direntplus->dirent;
1253 struct dentry *parent = file->f_path.dentry;
1254 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1255 struct dentry *dentry;
1256 struct dentry *alias;
1257 struct inode *dir = parent->d_inode;
1258 struct fuse_conn *fc;
1259 struct inode *inode;
1263 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1264 * ENOENT. Instead, it only means the userspace filesystem did
1265 * not want to return attributes/handle for this entry.
1272 if (name.name[0] == '.') {
1274 * We could potentially refresh the attributes of the directory
1279 if (name.name[1] == '.' && name.len == 2)
1283 if (invalid_nodeid(o->nodeid))
1285 if (!fuse_valid_type(o->attr.mode))
1288 fc = get_fuse_conn(dir);
1290 name.hash = full_name_hash(name.name, name.len);
1291 dentry = d_lookup(parent, &name);
1293 inode = dentry->d_inode;
1296 } else if (get_node_id(inode) != o->nodeid ||
1297 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1298 err = d_invalidate(dentry);
1301 } else if (is_bad_inode(inode)) {
1305 struct fuse_inode *fi;
1306 fi = get_fuse_inode(inode);
1307 spin_lock(&fc->lock);
1309 spin_unlock(&fc->lock);
1311 fuse_change_attributes(inode, &o->attr,
1312 entry_attr_timeout(o),
1316 * The other branch to 'found' comes via fuse_iget()
1317 * which bumps nlookup inside
1324 dentry = d_alloc(parent, &name);
1329 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1330 &o->attr, entry_attr_timeout(o), attr_version);
1334 alias = d_materialise_unique(dentry, inode);
1335 err = PTR_ERR(alias);
1345 if (fc->readdirplus_auto)
1346 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1347 fuse_change_entry_timeout(dentry, o);
1355 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1356 struct dir_context *ctx, u64 attr_version)
1358 struct fuse_direntplus *direntplus;
1359 struct fuse_dirent *dirent;
1364 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1365 direntplus = (struct fuse_direntplus *) buf;
1366 dirent = &direntplus->dirent;
1367 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1369 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1371 if (reclen > nbytes)
1373 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1377 /* We fill entries into dstbuf only as much as
1378 it can hold. But we still continue iterating
1379 over remaining entries to link them. If not,
1380 we need to send a FORGET for each of those
1381 which we did not link.
1383 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1384 dirent->ino, dirent->type);
1385 ctx->pos = dirent->off;
1391 ret = fuse_direntplus_link(file, direntplus, attr_version);
1393 fuse_force_forget(file, direntplus->entry_out.nodeid);
1399 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1404 struct inode *inode = file_inode(file);
1405 struct fuse_conn *fc = get_fuse_conn(inode);
1406 struct fuse_req *req;
1407 u64 attr_version = 0;
1409 if (is_bad_inode(inode))
1412 req = fuse_get_req(fc, 1);
1414 return PTR_ERR(req);
1416 page = alloc_page(GFP_KERNEL);
1418 fuse_put_request(fc, req);
1422 plus = fuse_use_readdirplus(inode, ctx);
1423 req->out.argpages = 1;
1425 req->pages[0] = page;
1426 req->page_descs[0].length = PAGE_SIZE;
1428 attr_version = fuse_get_attr_version(fc);
1429 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1432 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1435 fuse_request_send(fc, req);
1436 nbytes = req->out.args[0].size;
1437 err = req->out.h.error;
1438 fuse_put_request(fc, req);
1441 err = parse_dirplusfile(page_address(page), nbytes,
1445 err = parse_dirfile(page_address(page), nbytes, file,
1451 fuse_invalidate_atime(inode);
1455 static char *read_link(struct dentry *dentry)
1457 struct inode *inode = dentry->d_inode;
1458 struct fuse_conn *fc = get_fuse_conn(inode);
1459 struct fuse_req *req = fuse_get_req_nopages(fc);
1463 return ERR_CAST(req);
1465 link = (char *) __get_free_page(GFP_KERNEL);
1467 link = ERR_PTR(-ENOMEM);
1470 req->in.h.opcode = FUSE_READLINK;
1471 req->in.h.nodeid = get_node_id(inode);
1472 req->out.argvar = 1;
1473 req->out.numargs = 1;
1474 req->out.args[0].size = PAGE_SIZE - 1;
1475 req->out.args[0].value = link;
1476 fuse_request_send(fc, req);
1477 if (req->out.h.error) {
1478 free_page((unsigned long) link);
1479 link = ERR_PTR(req->out.h.error);
1481 link[req->out.args[0].size] = '\0';
1483 fuse_put_request(fc, req);
1484 fuse_invalidate_atime(inode);
1488 static void free_link(char *link)
1491 free_page((unsigned long) link);
1494 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1496 nd_set_link(nd, read_link(dentry));
1500 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1502 free_link(nd_get_link(nd));
1505 static int fuse_dir_open(struct inode *inode, struct file *file)
1507 return fuse_open_common(inode, file, true);
1510 static int fuse_dir_release(struct inode *inode, struct file *file)
1512 fuse_release_common(file, FUSE_RELEASEDIR);
1517 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1520 return fuse_fsync_common(file, start, end, datasync, 1);
1523 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1526 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1528 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1532 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1535 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1538 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1543 return fuse_ioctl_common(file, cmd, arg,
1544 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1547 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1549 /* Always update if mtime is explicitly set */
1550 if (ivalid & ATTR_MTIME_SET)
1553 /* Or if kernel i_mtime is the official one */
1554 if (trust_local_mtime)
1557 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1558 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1561 /* In all other cases update */
1565 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1566 bool trust_local_cmtime)
1568 unsigned ivalid = iattr->ia_valid;
1570 if (ivalid & ATTR_MODE)
1571 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1572 if (ivalid & ATTR_UID)
1573 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1574 if (ivalid & ATTR_GID)
1575 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1576 if (ivalid & ATTR_SIZE)
1577 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1578 if (ivalid & ATTR_ATIME) {
1579 arg->valid |= FATTR_ATIME;
1580 arg->atime = iattr->ia_atime.tv_sec;
1581 arg->atimensec = iattr->ia_atime.tv_nsec;
1582 if (!(ivalid & ATTR_ATIME_SET))
1583 arg->valid |= FATTR_ATIME_NOW;
1585 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1586 arg->valid |= FATTR_MTIME;
1587 arg->mtime = iattr->ia_mtime.tv_sec;
1588 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1589 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1590 arg->valid |= FATTR_MTIME_NOW;
1592 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1593 arg->valid |= FATTR_CTIME;
1594 arg->ctime = iattr->ia_ctime.tv_sec;
1595 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1600 * Prevent concurrent writepages on inode
1602 * This is done by adding a negative bias to the inode write counter
1603 * and waiting for all pending writes to finish.
1605 void fuse_set_nowrite(struct inode *inode)
1607 struct fuse_conn *fc = get_fuse_conn(inode);
1608 struct fuse_inode *fi = get_fuse_inode(inode);
1610 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1612 spin_lock(&fc->lock);
1613 BUG_ON(fi->writectr < 0);
1614 fi->writectr += FUSE_NOWRITE;
1615 spin_unlock(&fc->lock);
1616 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1620 * Allow writepages on inode
1622 * Remove the bias from the writecounter and send any queued
1625 static void __fuse_release_nowrite(struct inode *inode)
1627 struct fuse_inode *fi = get_fuse_inode(inode);
1629 BUG_ON(fi->writectr != FUSE_NOWRITE);
1631 fuse_flush_writepages(inode);
1634 void fuse_release_nowrite(struct inode *inode)
1636 struct fuse_conn *fc = get_fuse_conn(inode);
1638 spin_lock(&fc->lock);
1639 __fuse_release_nowrite(inode);
1640 spin_unlock(&fc->lock);
1643 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1644 struct inode *inode,
1645 struct fuse_setattr_in *inarg_p,
1646 struct fuse_attr_out *outarg_p)
1648 req->in.h.opcode = FUSE_SETATTR;
1649 req->in.h.nodeid = get_node_id(inode);
1650 req->in.numargs = 1;
1651 req->in.args[0].size = sizeof(*inarg_p);
1652 req->in.args[0].value = inarg_p;
1653 req->out.numargs = 1;
1655 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1657 req->out.args[0].size = sizeof(*outarg_p);
1658 req->out.args[0].value = outarg_p;
1662 * Flush inode->i_mtime to the server
1664 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1666 struct fuse_conn *fc = get_fuse_conn(inode);
1667 struct fuse_req *req;
1668 struct fuse_setattr_in inarg;
1669 struct fuse_attr_out outarg;
1672 req = fuse_get_req_nopages(fc);
1674 return PTR_ERR(req);
1676 memset(&inarg, 0, sizeof(inarg));
1677 memset(&outarg, 0, sizeof(outarg));
1679 inarg.valid = FATTR_MTIME;
1680 inarg.mtime = inode->i_mtime.tv_sec;
1681 inarg.mtimensec = inode->i_mtime.tv_nsec;
1682 if (fc->minor >= 23) {
1683 inarg.valid |= FATTR_CTIME;
1684 inarg.ctime = inode->i_ctime.tv_sec;
1685 inarg.ctimensec = inode->i_ctime.tv_nsec;
1688 inarg.valid |= FATTR_FH;
1691 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1692 fuse_request_send(fc, req);
1693 err = req->out.h.error;
1694 fuse_put_request(fc, req);
1700 * Set attributes, and at the same time refresh them.
1702 * Truncation is slightly complicated, because the 'truncate' request
1703 * may fail, in which case we don't want to touch the mapping.
1704 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1705 * and the actual truncation by hand.
1707 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1710 struct fuse_conn *fc = get_fuse_conn(inode);
1711 struct fuse_inode *fi = get_fuse_inode(inode);
1712 struct fuse_req *req;
1713 struct fuse_setattr_in inarg;
1714 struct fuse_attr_out outarg;
1715 bool is_truncate = false;
1716 bool is_wb = fc->writeback_cache;
1719 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1721 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1722 attr->ia_valid |= ATTR_FORCE;
1724 err = inode_change_ok(inode, attr);
1728 if (attr->ia_valid & ATTR_OPEN) {
1729 if (fc->atomic_o_trunc)
1734 if (attr->ia_valid & ATTR_SIZE)
1737 req = fuse_get_req_nopages(fc);
1739 return PTR_ERR(req);
1742 fuse_set_nowrite(inode);
1743 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1744 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1745 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1748 memset(&inarg, 0, sizeof(inarg));
1749 memset(&outarg, 0, sizeof(outarg));
1750 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1752 struct fuse_file *ff = file->private_data;
1753 inarg.valid |= FATTR_FH;
1756 if (attr->ia_valid & ATTR_SIZE) {
1757 /* For mandatory locking in truncate */
1758 inarg.valid |= FATTR_LOCKOWNER;
1759 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1761 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1762 fuse_request_send(fc, req);
1763 err = req->out.h.error;
1764 fuse_put_request(fc, req);
1767 fuse_invalidate_attr(inode);
1771 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1772 make_bad_inode(inode);
1777 spin_lock(&fc->lock);
1778 /* the kernel maintains i_mtime locally */
1779 if (trust_local_cmtime) {
1780 if (attr->ia_valid & ATTR_MTIME)
1781 inode->i_mtime = attr->ia_mtime;
1782 if (attr->ia_valid & ATTR_CTIME)
1783 inode->i_ctime = attr->ia_ctime;
1784 /* FIXME: clear I_DIRTY_SYNC? */
1787 fuse_change_attributes_common(inode, &outarg.attr,
1788 attr_timeout(&outarg));
1789 oldsize = inode->i_size;
1790 /* see the comment in fuse_change_attributes() */
1791 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1792 i_size_write(inode, outarg.attr.size);
1795 /* NOTE: this may release/reacquire fc->lock */
1796 __fuse_release_nowrite(inode);
1798 spin_unlock(&fc->lock);
1801 * Only call invalidate_inode_pages2() after removing
1802 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1804 if ((is_truncate || !is_wb) &&
1805 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1806 truncate_pagecache(inode, outarg.attr.size);
1807 invalidate_inode_pages2(inode->i_mapping);
1810 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1815 fuse_release_nowrite(inode);
1817 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1821 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1823 struct inode *inode = entry->d_inode;
1825 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1828 if (attr->ia_valid & ATTR_FILE)
1829 return fuse_do_setattr(inode, attr, attr->ia_file);
1831 return fuse_do_setattr(inode, attr, NULL);
1834 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1837 struct inode *inode = entry->d_inode;
1838 struct fuse_conn *fc = get_fuse_conn(inode);
1840 if (!fuse_allow_current_process(fc))
1843 return fuse_update_attributes(inode, stat, NULL, NULL);
1846 static int fuse_setxattr(struct dentry *entry, const char *name,
1847 const void *value, size_t size, int flags)
1849 struct inode *inode = entry->d_inode;
1850 struct fuse_conn *fc = get_fuse_conn(inode);
1851 struct fuse_req *req;
1852 struct fuse_setxattr_in inarg;
1855 if (fc->no_setxattr)
1858 req = fuse_get_req_nopages(fc);
1860 return PTR_ERR(req);
1862 memset(&inarg, 0, sizeof(inarg));
1864 inarg.flags = flags;
1865 req->in.h.opcode = FUSE_SETXATTR;
1866 req->in.h.nodeid = get_node_id(inode);
1867 req->in.numargs = 3;
1868 req->in.args[0].size = sizeof(inarg);
1869 req->in.args[0].value = &inarg;
1870 req->in.args[1].size = strlen(name) + 1;
1871 req->in.args[1].value = name;
1872 req->in.args[2].size = size;
1873 req->in.args[2].value = value;
1874 fuse_request_send(fc, req);
1875 err = req->out.h.error;
1876 fuse_put_request(fc, req);
1877 if (err == -ENOSYS) {
1878 fc->no_setxattr = 1;
1882 fuse_invalidate_attr(inode);
1883 fuse_update_ctime(inode);
1888 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1889 void *value, size_t size)
1891 struct inode *inode = entry->d_inode;
1892 struct fuse_conn *fc = get_fuse_conn(inode);
1893 struct fuse_req *req;
1894 struct fuse_getxattr_in inarg;
1895 struct fuse_getxattr_out outarg;
1898 if (fc->no_getxattr)
1901 req = fuse_get_req_nopages(fc);
1903 return PTR_ERR(req);
1905 memset(&inarg, 0, sizeof(inarg));
1907 req->in.h.opcode = FUSE_GETXATTR;
1908 req->in.h.nodeid = get_node_id(inode);
1909 req->in.numargs = 2;
1910 req->in.args[0].size = sizeof(inarg);
1911 req->in.args[0].value = &inarg;
1912 req->in.args[1].size = strlen(name) + 1;
1913 req->in.args[1].value = name;
1914 /* This is really two different operations rolled into one */
1915 req->out.numargs = 1;
1917 req->out.argvar = 1;
1918 req->out.args[0].size = size;
1919 req->out.args[0].value = value;
1921 req->out.args[0].size = sizeof(outarg);
1922 req->out.args[0].value = &outarg;
1924 fuse_request_send(fc, req);
1925 ret = req->out.h.error;
1927 ret = size ? req->out.args[0].size : outarg.size;
1929 if (ret == -ENOSYS) {
1930 fc->no_getxattr = 1;
1934 fuse_put_request(fc, req);
1938 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1940 struct inode *inode = entry->d_inode;
1941 struct fuse_conn *fc = get_fuse_conn(inode);
1942 struct fuse_req *req;
1943 struct fuse_getxattr_in inarg;
1944 struct fuse_getxattr_out outarg;
1947 if (!fuse_allow_current_process(fc))
1950 if (fc->no_listxattr)
1953 req = fuse_get_req_nopages(fc);
1955 return PTR_ERR(req);
1957 memset(&inarg, 0, sizeof(inarg));
1959 req->in.h.opcode = FUSE_LISTXATTR;
1960 req->in.h.nodeid = get_node_id(inode);
1961 req->in.numargs = 1;
1962 req->in.args[0].size = sizeof(inarg);
1963 req->in.args[0].value = &inarg;
1964 /* This is really two different operations rolled into one */
1965 req->out.numargs = 1;
1967 req->out.argvar = 1;
1968 req->out.args[0].size = size;
1969 req->out.args[0].value = list;
1971 req->out.args[0].size = sizeof(outarg);
1972 req->out.args[0].value = &outarg;
1974 fuse_request_send(fc, req);
1975 ret = req->out.h.error;
1977 ret = size ? req->out.args[0].size : outarg.size;
1979 if (ret == -ENOSYS) {
1980 fc->no_listxattr = 1;
1984 fuse_put_request(fc, req);
1988 static int fuse_removexattr(struct dentry *entry, const char *name)
1990 struct inode *inode = entry->d_inode;
1991 struct fuse_conn *fc = get_fuse_conn(inode);
1992 struct fuse_req *req;
1995 if (fc->no_removexattr)
1998 req = fuse_get_req_nopages(fc);
2000 return PTR_ERR(req);
2002 req->in.h.opcode = FUSE_REMOVEXATTR;
2003 req->in.h.nodeid = get_node_id(inode);
2004 req->in.numargs = 1;
2005 req->in.args[0].size = strlen(name) + 1;
2006 req->in.args[0].value = name;
2007 fuse_request_send(fc, req);
2008 err = req->out.h.error;
2009 fuse_put_request(fc, req);
2010 if (err == -ENOSYS) {
2011 fc->no_removexattr = 1;
2015 fuse_invalidate_attr(inode);
2016 fuse_update_ctime(inode);
2021 static const struct inode_operations fuse_dir_inode_operations = {
2022 .lookup = fuse_lookup,
2023 .mkdir = fuse_mkdir,
2024 .symlink = fuse_symlink,
2025 .unlink = fuse_unlink,
2026 .rmdir = fuse_rmdir,
2027 .rename = fuse_rename,
2028 .rename2 = fuse_rename2,
2030 .setattr = fuse_setattr,
2031 .create = fuse_create,
2032 .atomic_open = fuse_atomic_open,
2033 .mknod = fuse_mknod,
2034 .permission = fuse_permission,
2035 .getattr = fuse_getattr,
2036 .setxattr = fuse_setxattr,
2037 .getxattr = fuse_getxattr,
2038 .listxattr = fuse_listxattr,
2039 .removexattr = fuse_removexattr,
2042 static const struct file_operations fuse_dir_operations = {
2043 .llseek = generic_file_llseek,
2044 .read = generic_read_dir,
2045 .iterate = fuse_readdir,
2046 .open = fuse_dir_open,
2047 .release = fuse_dir_release,
2048 .fsync = fuse_dir_fsync,
2049 .unlocked_ioctl = fuse_dir_ioctl,
2050 .compat_ioctl = fuse_dir_compat_ioctl,
2053 static const struct inode_operations fuse_common_inode_operations = {
2054 .setattr = fuse_setattr,
2055 .permission = fuse_permission,
2056 .getattr = fuse_getattr,
2057 .setxattr = fuse_setxattr,
2058 .getxattr = fuse_getxattr,
2059 .listxattr = fuse_listxattr,
2060 .removexattr = fuse_removexattr,
2063 static const struct inode_operations fuse_symlink_inode_operations = {
2064 .setattr = fuse_setattr,
2065 .follow_link = fuse_follow_link,
2066 .put_link = fuse_put_link,
2067 .readlink = generic_readlink,
2068 .getattr = fuse_getattr,
2069 .setxattr = fuse_setxattr,
2070 .getxattr = fuse_getxattr,
2071 .listxattr = fuse_listxattr,
2072 .removexattr = fuse_removexattr,
2075 void fuse_init_common(struct inode *inode)
2077 inode->i_op = &fuse_common_inode_operations;
2080 void fuse_init_dir(struct inode *inode)
2082 inode->i_op = &fuse_dir_inode_operations;
2083 inode->i_fop = &fuse_dir_operations;
2086 void fuse_init_symlink(struct inode *inode)
2088 inode->i_op = &fuse_symlink_inode_operations;