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 #if BITS_PER_LONG >= 64
18 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
23 static inline u64 fuse_dentry_time(struct dentry *entry)
29 * On 32 bit archs store the high 32 bits of time in d_fsdata
31 static void fuse_dentry_settime(struct dentry *entry, u64 time)
34 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
37 static u64 fuse_dentry_time(struct dentry *entry)
39 return (u64) entry->d_time +
40 ((u64) (unsigned long) entry->d_fsdata << 32);
45 * FUSE caches dentries and attributes with separate timeout. The
46 * time in jiffies until the dentry/attributes are valid is stored in
47 * dentry->d_time and fuse_inode->i_time respectively.
51 * Calculate the time in jiffies until a dentry/attributes are valid
53 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
56 struct timespec ts = {sec, nsec};
57 return get_jiffies_64() + timespec_to_jiffies(&ts);
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
66 static void fuse_change_entry_timeout(struct dentry *entry,
67 struct fuse_entry_out *o)
69 fuse_dentry_settime(entry,
70 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
73 static u64 attr_timeout(struct fuse_attr_out *o)
75 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
78 static u64 entry_attr_timeout(struct fuse_entry_out *o)
80 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
84 * Mark the attributes as stale, so that at the next call to
85 * ->getattr() they will be fetched from userspace
87 void fuse_invalidate_attr(struct inode *inode)
89 get_fuse_inode(inode)->i_time = 0;
93 * Just mark the entry as stale, so that a next attempt to look it up
94 * will result in a new lookup call to userspace
96 * This is called when a dentry is about to become negative and the
97 * timeout is unknown (unlink, rmdir, rename and in some cases
100 void fuse_invalidate_entry_cache(struct dentry *entry)
102 fuse_dentry_settime(entry, 0);
106 * Same as fuse_invalidate_entry_cache(), but also try to remove the
107 * dentry from the hash
109 static void fuse_invalidate_entry(struct dentry *entry)
112 fuse_invalidate_entry_cache(entry);
115 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
116 u64 nodeid, struct qstr *name,
117 struct fuse_entry_out *outarg)
119 memset(outarg, 0, sizeof(struct fuse_entry_out));
120 req->in.h.opcode = FUSE_LOOKUP;
121 req->in.h.nodeid = nodeid;
123 req->in.args[0].size = name->len + 1;
124 req->in.args[0].value = name->name;
125 req->out.numargs = 1;
127 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
129 req->out.args[0].size = sizeof(struct fuse_entry_out);
130 req->out.args[0].value = outarg;
133 u64 fuse_get_attr_version(struct fuse_conn *fc)
138 * The spin lock isn't actually needed on 64bit archs, but we
139 * don't yet care too much about such optimizations.
141 spin_lock(&fc->lock);
142 curr_version = fc->attr_version;
143 spin_unlock(&fc->lock);
149 * Check whether the dentry is still valid
151 * If the entry validity timeout has expired and the dentry is
152 * positive, try to redo the lookup. If the lookup results in a
153 * different inode, then let the VFS invalidate the dentry and redo
154 * the lookup once more. If the lookup results in the same inode,
155 * then refresh the attributes, timeouts and mark the dentry valid.
157 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
161 inode = ACCESS_ONCE(entry->d_inode);
162 if (inode && is_bad_inode(inode))
164 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
166 struct fuse_entry_out outarg;
167 struct fuse_conn *fc;
168 struct fuse_req *req;
169 struct fuse_forget_link *forget;
170 struct dentry *parent;
173 /* For negative dentries, always do a fresh lookup */
177 if (nd && (nd->flags & LOOKUP_RCU))
180 fc = get_fuse_conn(inode);
181 req = fuse_get_req(fc);
185 forget = fuse_alloc_forget();
187 fuse_put_request(fc, req);
191 attr_version = fuse_get_attr_version(fc);
193 parent = dget_parent(entry);
194 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
195 &entry->d_name, &outarg);
196 fuse_request_send(fc, req);
198 err = req->out.h.error;
199 fuse_put_request(fc, req);
200 /* Zero nodeid is same as -ENOENT */
201 if (!err && !outarg.nodeid)
204 struct fuse_inode *fi = get_fuse_inode(inode);
205 if (outarg.nodeid != get_node_id(inode)) {
206 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
209 spin_lock(&fc->lock);
211 spin_unlock(&fc->lock);
214 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
217 fuse_change_attributes(inode, &outarg.attr,
218 entry_attr_timeout(&outarg),
220 fuse_change_entry_timeout(entry, &outarg);
225 static int invalid_nodeid(u64 nodeid)
227 return !nodeid || nodeid == FUSE_ROOT_ID;
230 const struct dentry_operations fuse_dentry_operations = {
231 .d_revalidate = fuse_dentry_revalidate,
234 int fuse_valid_type(int m)
236 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
237 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
241 * Add a directory inode to a dentry, ensuring that no other dentry
242 * refers to this inode. Called with fc->inst_mutex.
244 static struct dentry *fuse_d_add_directory(struct dentry *entry,
247 struct dentry *alias = d_find_alias(inode);
248 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
249 /* This tries to shrink the subtree below alias */
250 fuse_invalidate_entry(alias);
252 if (!hlist_empty(&inode->i_dentry))
253 return ERR_PTR(-EBUSY);
257 return d_splice_alias(inode, entry);
260 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
261 struct fuse_entry_out *outarg, struct inode **inode)
263 struct fuse_conn *fc = get_fuse_conn_super(sb);
264 struct fuse_req *req;
265 struct fuse_forget_link *forget;
271 if (name->len > FUSE_NAME_MAX)
274 req = fuse_get_req(fc);
279 forget = fuse_alloc_forget();
282 fuse_put_request(fc, req);
286 attr_version = fuse_get_attr_version(fc);
288 fuse_lookup_init(fc, req, nodeid, name, outarg);
289 fuse_request_send(fc, req);
290 err = req->out.h.error;
291 fuse_put_request(fc, req);
292 /* Zero nodeid is same as -ENOENT, but with valid timeout */
293 if (err || !outarg->nodeid)
299 if (!fuse_valid_type(outarg->attr.mode))
302 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
303 &outarg->attr, entry_attr_timeout(outarg),
307 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
318 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
319 struct nameidata *nd)
322 struct fuse_entry_out outarg;
324 struct dentry *newent;
325 struct fuse_conn *fc = get_fuse_conn(dir);
326 bool outarg_valid = true;
328 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
330 if (err == -ENOENT) {
331 outarg_valid = false;
338 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
341 if (inode && S_ISDIR(inode->i_mode)) {
342 mutex_lock(&fc->inst_mutex);
343 newent = fuse_d_add_directory(entry, inode);
344 mutex_unlock(&fc->inst_mutex);
345 err = PTR_ERR(newent);
349 newent = d_splice_alias(inode, entry);
352 entry = newent ? newent : entry;
354 fuse_change_entry_timeout(entry, &outarg);
356 fuse_invalidate_entry_cache(entry);
367 * Atomic create+open operation
369 * If the filesystem doesn't support this, then fall back to separate
370 * 'mknod' + 'open' requests.
372 static struct file *fuse_create_open(struct inode *dir, struct dentry *entry,
373 struct opendata *od, unsigned flags,
378 struct fuse_conn *fc = get_fuse_conn(dir);
379 struct fuse_req *req;
380 struct fuse_forget_link *forget;
381 struct fuse_create_in inarg;
382 struct fuse_open_out outopen;
383 struct fuse_entry_out outentry;
384 struct fuse_file *ff;
387 forget = fuse_alloc_forget();
392 req = fuse_get_req(fc);
395 goto out_put_forget_req;
398 ff = fuse_file_alloc(fc);
400 goto out_put_request;
403 mode &= ~current_umask();
406 memset(&inarg, 0, sizeof(inarg));
407 memset(&outentry, 0, sizeof(outentry));
410 inarg.umask = current_umask();
411 req->in.h.opcode = FUSE_CREATE;
412 req->in.h.nodeid = get_node_id(dir);
414 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
416 req->in.args[0].value = &inarg;
417 req->in.args[1].size = entry->d_name.len + 1;
418 req->in.args[1].value = entry->d_name.name;
419 req->out.numargs = 2;
421 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
423 req->out.args[0].size = sizeof(outentry);
424 req->out.args[0].value = &outentry;
425 req->out.args[1].size = sizeof(outopen);
426 req->out.args[1].value = &outopen;
427 fuse_request_send(fc, req);
428 err = req->out.h.error;
433 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
436 fuse_put_request(fc, req);
438 ff->nodeid = outentry.nodeid;
439 ff->open_flags = outopen.open_flags;
440 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
441 &outentry.attr, entry_attr_timeout(&outentry), 0);
443 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
444 fuse_sync_release(ff, flags);
445 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
450 d_instantiate(entry, inode);
451 fuse_change_entry_timeout(entry, &outentry);
452 fuse_invalidate_attr(dir);
453 file = finish_open(od, entry, generic_file_open);
455 fuse_sync_release(ff, flags);
457 file->private_data = fuse_file_get(ff);
458 fuse_finish_open(inode, file);
465 fuse_put_request(fc, req);
472 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
473 static struct file *fuse_atomic_open(struct inode *dir, struct dentry *entry,
474 struct opendata *od, unsigned flags,
475 umode_t mode, bool *created)
478 struct fuse_conn *fc = get_fuse_conn(dir);
480 struct dentry *res = NULL;
482 if (d_unhashed(entry)) {
483 res = fuse_lookup(dir, entry, NULL);
485 return ERR_CAST(res);
491 if (!(flags & O_CREAT) || entry->d_inode)
500 file = fuse_create_open(dir, entry, od, flags, mode);
501 if (PTR_ERR(file) == -ENOSYS) {
510 err = fuse_mknod(dir, entry, mode, 0);
516 finish_no_open(od, res);
521 * Code shared between mknod, mkdir, symlink and link
523 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
524 struct inode *dir, struct dentry *entry,
527 struct fuse_entry_out outarg;
530 struct fuse_forget_link *forget;
532 forget = fuse_alloc_forget();
534 fuse_put_request(fc, req);
538 memset(&outarg, 0, sizeof(outarg));
539 req->in.h.nodeid = get_node_id(dir);
540 req->out.numargs = 1;
542 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
544 req->out.args[0].size = sizeof(outarg);
545 req->out.args[0].value = &outarg;
546 fuse_request_send(fc, req);
547 err = req->out.h.error;
548 fuse_put_request(fc, req);
550 goto out_put_forget_req;
553 if (invalid_nodeid(outarg.nodeid))
554 goto out_put_forget_req;
556 if ((outarg.attr.mode ^ mode) & S_IFMT)
557 goto out_put_forget_req;
559 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
560 &outarg.attr, entry_attr_timeout(&outarg), 0);
562 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
567 if (S_ISDIR(inode->i_mode)) {
568 struct dentry *alias;
569 mutex_lock(&fc->inst_mutex);
570 alias = d_find_alias(inode);
572 /* New directory must have moved since mkdir */
573 mutex_unlock(&fc->inst_mutex);
578 d_instantiate(entry, inode);
579 mutex_unlock(&fc->inst_mutex);
581 d_instantiate(entry, inode);
583 fuse_change_entry_timeout(entry, &outarg);
584 fuse_invalidate_attr(dir);
592 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
595 struct fuse_mknod_in inarg;
596 struct fuse_conn *fc = get_fuse_conn(dir);
597 struct fuse_req *req = fuse_get_req(fc);
602 mode &= ~current_umask();
604 memset(&inarg, 0, sizeof(inarg));
606 inarg.rdev = new_encode_dev(rdev);
607 inarg.umask = current_umask();
608 req->in.h.opcode = FUSE_MKNOD;
610 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
612 req->in.args[0].value = &inarg;
613 req->in.args[1].size = entry->d_name.len + 1;
614 req->in.args[1].value = entry->d_name.name;
615 return create_new_entry(fc, req, dir, entry, mode);
618 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
619 struct nameidata *nd)
621 return fuse_mknod(dir, entry, mode, 0);
624 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
626 struct fuse_mkdir_in inarg;
627 struct fuse_conn *fc = get_fuse_conn(dir);
628 struct fuse_req *req = fuse_get_req(fc);
633 mode &= ~current_umask();
635 memset(&inarg, 0, sizeof(inarg));
637 inarg.umask = current_umask();
638 req->in.h.opcode = FUSE_MKDIR;
640 req->in.args[0].size = sizeof(inarg);
641 req->in.args[0].value = &inarg;
642 req->in.args[1].size = entry->d_name.len + 1;
643 req->in.args[1].value = entry->d_name.name;
644 return create_new_entry(fc, req, dir, entry, S_IFDIR);
647 static int fuse_symlink(struct inode *dir, struct dentry *entry,
650 struct fuse_conn *fc = get_fuse_conn(dir);
651 unsigned len = strlen(link) + 1;
652 struct fuse_req *req = fuse_get_req(fc);
656 req->in.h.opcode = FUSE_SYMLINK;
658 req->in.args[0].size = entry->d_name.len + 1;
659 req->in.args[0].value = entry->d_name.name;
660 req->in.args[1].size = len;
661 req->in.args[1].value = link;
662 return create_new_entry(fc, req, dir, entry, S_IFLNK);
665 static int fuse_unlink(struct inode *dir, struct dentry *entry)
668 struct fuse_conn *fc = get_fuse_conn(dir);
669 struct fuse_req *req = fuse_get_req(fc);
673 req->in.h.opcode = FUSE_UNLINK;
674 req->in.h.nodeid = get_node_id(dir);
676 req->in.args[0].size = entry->d_name.len + 1;
677 req->in.args[0].value = entry->d_name.name;
678 fuse_request_send(fc, req);
679 err = req->out.h.error;
680 fuse_put_request(fc, req);
682 struct inode *inode = entry->d_inode;
683 struct fuse_inode *fi = get_fuse_inode(inode);
685 spin_lock(&fc->lock);
686 fi->attr_version = ++fc->attr_version;
688 spin_unlock(&fc->lock);
689 fuse_invalidate_attr(inode);
690 fuse_invalidate_attr(dir);
691 fuse_invalidate_entry_cache(entry);
692 } else if (err == -EINTR)
693 fuse_invalidate_entry(entry);
697 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
700 struct fuse_conn *fc = get_fuse_conn(dir);
701 struct fuse_req *req = fuse_get_req(fc);
705 req->in.h.opcode = FUSE_RMDIR;
706 req->in.h.nodeid = get_node_id(dir);
708 req->in.args[0].size = entry->d_name.len + 1;
709 req->in.args[0].value = entry->d_name.name;
710 fuse_request_send(fc, req);
711 err = req->out.h.error;
712 fuse_put_request(fc, req);
714 clear_nlink(entry->d_inode);
715 fuse_invalidate_attr(dir);
716 fuse_invalidate_entry_cache(entry);
717 } else if (err == -EINTR)
718 fuse_invalidate_entry(entry);
722 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
723 struct inode *newdir, struct dentry *newent)
726 struct fuse_rename_in inarg;
727 struct fuse_conn *fc = get_fuse_conn(olddir);
728 struct fuse_req *req = fuse_get_req(fc);
733 memset(&inarg, 0, sizeof(inarg));
734 inarg.newdir = get_node_id(newdir);
735 req->in.h.opcode = FUSE_RENAME;
736 req->in.h.nodeid = get_node_id(olddir);
738 req->in.args[0].size = sizeof(inarg);
739 req->in.args[0].value = &inarg;
740 req->in.args[1].size = oldent->d_name.len + 1;
741 req->in.args[1].value = oldent->d_name.name;
742 req->in.args[2].size = newent->d_name.len + 1;
743 req->in.args[2].value = newent->d_name.name;
744 fuse_request_send(fc, req);
745 err = req->out.h.error;
746 fuse_put_request(fc, req);
749 fuse_invalidate_attr(oldent->d_inode);
751 fuse_invalidate_attr(olddir);
752 if (olddir != newdir)
753 fuse_invalidate_attr(newdir);
755 /* newent will end up negative */
756 if (newent->d_inode) {
757 fuse_invalidate_attr(newent->d_inode);
758 fuse_invalidate_entry_cache(newent);
760 } else if (err == -EINTR) {
761 /* If request was interrupted, DEITY only knows if the
762 rename actually took place. If the invalidation
763 fails (e.g. some process has CWD under the renamed
764 directory), then there can be inconsistency between
765 the dcache and the real filesystem. Tough luck. */
766 fuse_invalidate_entry(oldent);
768 fuse_invalidate_entry(newent);
774 static int fuse_link(struct dentry *entry, struct inode *newdir,
775 struct dentry *newent)
778 struct fuse_link_in inarg;
779 struct inode *inode = entry->d_inode;
780 struct fuse_conn *fc = get_fuse_conn(inode);
781 struct fuse_req *req = fuse_get_req(fc);
785 memset(&inarg, 0, sizeof(inarg));
786 inarg.oldnodeid = get_node_id(inode);
787 req->in.h.opcode = FUSE_LINK;
789 req->in.args[0].size = sizeof(inarg);
790 req->in.args[0].value = &inarg;
791 req->in.args[1].size = newent->d_name.len + 1;
792 req->in.args[1].value = newent->d_name.name;
793 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
794 /* Contrary to "normal" filesystems it can happen that link
795 makes two "logical" inodes point to the same "physical"
796 inode. We invalidate the attributes of the old one, so it
797 will reflect changes in the backing inode (link count,
801 struct fuse_inode *fi = get_fuse_inode(inode);
803 spin_lock(&fc->lock);
804 fi->attr_version = ++fc->attr_version;
806 spin_unlock(&fc->lock);
807 fuse_invalidate_attr(inode);
808 } else if (err == -EINTR) {
809 fuse_invalidate_attr(inode);
814 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
817 unsigned int blkbits;
819 stat->dev = inode->i_sb->s_dev;
820 stat->ino = attr->ino;
821 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
822 stat->nlink = attr->nlink;
823 stat->uid = attr->uid;
824 stat->gid = attr->gid;
825 stat->rdev = inode->i_rdev;
826 stat->atime.tv_sec = attr->atime;
827 stat->atime.tv_nsec = attr->atimensec;
828 stat->mtime.tv_sec = attr->mtime;
829 stat->mtime.tv_nsec = attr->mtimensec;
830 stat->ctime.tv_sec = attr->ctime;
831 stat->ctime.tv_nsec = attr->ctimensec;
832 stat->size = attr->size;
833 stat->blocks = attr->blocks;
835 if (attr->blksize != 0)
836 blkbits = ilog2(attr->blksize);
838 blkbits = inode->i_sb->s_blocksize_bits;
840 stat->blksize = 1 << blkbits;
843 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
847 struct fuse_getattr_in inarg;
848 struct fuse_attr_out outarg;
849 struct fuse_conn *fc = get_fuse_conn(inode);
850 struct fuse_req *req;
853 req = fuse_get_req(fc);
857 attr_version = fuse_get_attr_version(fc);
859 memset(&inarg, 0, sizeof(inarg));
860 memset(&outarg, 0, sizeof(outarg));
861 /* Directories have separate file-handle space */
862 if (file && S_ISREG(inode->i_mode)) {
863 struct fuse_file *ff = file->private_data;
865 inarg.getattr_flags |= FUSE_GETATTR_FH;
868 req->in.h.opcode = FUSE_GETATTR;
869 req->in.h.nodeid = get_node_id(inode);
871 req->in.args[0].size = sizeof(inarg);
872 req->in.args[0].value = &inarg;
873 req->out.numargs = 1;
875 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
877 req->out.args[0].size = sizeof(outarg);
878 req->out.args[0].value = &outarg;
879 fuse_request_send(fc, req);
880 err = req->out.h.error;
881 fuse_put_request(fc, req);
883 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
884 make_bad_inode(inode);
887 fuse_change_attributes(inode, &outarg.attr,
888 attr_timeout(&outarg),
891 fuse_fillattr(inode, &outarg.attr, stat);
897 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
898 struct file *file, bool *refreshed)
900 struct fuse_inode *fi = get_fuse_inode(inode);
904 if (fi->i_time < get_jiffies_64()) {
906 err = fuse_do_getattr(inode, stat, file);
911 generic_fillattr(inode, stat);
912 stat->mode = fi->orig_i_mode;
913 stat->ino = fi->orig_ino;
917 if (refreshed != NULL)
923 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
924 u64 child_nodeid, struct qstr *name)
927 struct inode *parent;
929 struct dentry *entry;
931 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
935 mutex_lock(&parent->i_mutex);
936 if (!S_ISDIR(parent->i_mode))
940 dir = d_find_alias(parent);
944 entry = d_lookup(dir, name);
949 fuse_invalidate_attr(parent);
950 fuse_invalidate_entry(entry);
952 if (child_nodeid != 0 && entry->d_inode) {
953 mutex_lock(&entry->d_inode->i_mutex);
954 if (get_node_id(entry->d_inode) != child_nodeid) {
958 if (d_mountpoint(entry)) {
962 if (S_ISDIR(entry->d_inode->i_mode)) {
963 shrink_dcache_parent(entry);
964 if (!simple_empty(entry)) {
968 entry->d_inode->i_flags |= S_DEAD;
971 clear_nlink(entry->d_inode);
974 mutex_unlock(&entry->d_inode->i_mutex);
983 mutex_unlock(&parent->i_mutex);
989 * Calling into a user-controlled filesystem gives the filesystem
990 * daemon ptrace-like capabilities over the requester process. This
991 * means, that the filesystem daemon is able to record the exact
992 * filesystem operations performed, and can also control the behavior
993 * of the requester process in otherwise impossible ways. For example
994 * it can delay the operation for arbitrary length of time allowing
995 * DoS against the requester.
997 * For this reason only those processes can call into the filesystem,
998 * for which the owner of the mount has ptrace privilege. This
999 * excludes processes started by other users, suid or sgid processes.
1001 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
1003 const struct cred *cred;
1006 if (fc->flags & FUSE_ALLOW_OTHER)
1011 cred = __task_cred(task);
1012 if (cred->euid == fc->user_id &&
1013 cred->suid == fc->user_id &&
1014 cred->uid == fc->user_id &&
1015 cred->egid == fc->group_id &&
1016 cred->sgid == fc->group_id &&
1017 cred->gid == fc->group_id)
1024 static int fuse_access(struct inode *inode, int mask)
1026 struct fuse_conn *fc = get_fuse_conn(inode);
1027 struct fuse_req *req;
1028 struct fuse_access_in inarg;
1034 req = fuse_get_req(fc);
1036 return PTR_ERR(req);
1038 memset(&inarg, 0, sizeof(inarg));
1039 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1040 req->in.h.opcode = FUSE_ACCESS;
1041 req->in.h.nodeid = get_node_id(inode);
1042 req->in.numargs = 1;
1043 req->in.args[0].size = sizeof(inarg);
1044 req->in.args[0].value = &inarg;
1045 fuse_request_send(fc, req);
1046 err = req->out.h.error;
1047 fuse_put_request(fc, req);
1048 if (err == -ENOSYS) {
1055 static int fuse_perm_getattr(struct inode *inode, int mask)
1057 if (mask & MAY_NOT_BLOCK)
1060 return fuse_do_getattr(inode, NULL, NULL);
1064 * Check permission. The two basic access models of FUSE are:
1066 * 1) Local access checking ('default_permissions' mount option) based
1067 * on file mode. This is the plain old disk filesystem permission
1070 * 2) "Remote" access checking, where server is responsible for
1071 * checking permission in each inode operation. An exception to this
1072 * is if ->permission() was invoked from sys_access() in which case an
1073 * access request is sent. Execute permission is still checked
1074 * locally based on file mode.
1076 static int fuse_permission(struct inode *inode, int mask)
1078 struct fuse_conn *fc = get_fuse_conn(inode);
1079 bool refreshed = false;
1082 if (!fuse_allow_task(fc, current))
1086 * If attributes are needed, refresh them before proceeding
1088 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1089 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1090 struct fuse_inode *fi = get_fuse_inode(inode);
1092 if (fi->i_time < get_jiffies_64()) {
1095 err = fuse_perm_getattr(inode, mask);
1101 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1102 err = generic_permission(inode, mask);
1104 /* If permission is denied, try to refresh file
1105 attributes. This is also needed, because the root
1106 node will at first have no permissions */
1107 if (err == -EACCES && !refreshed) {
1108 err = fuse_perm_getattr(inode, mask);
1110 err = generic_permission(inode, mask);
1113 /* Note: the opposite of the above test does not
1114 exist. So if permissions are revoked this won't be
1115 noticed immediately, only after the attribute
1116 timeout has expired */
1117 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1118 if (mask & MAY_NOT_BLOCK)
1121 err = fuse_access(inode, mask);
1122 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1123 if (!(inode->i_mode & S_IXUGO)) {
1127 err = fuse_perm_getattr(inode, mask);
1128 if (!err && !(inode->i_mode & S_IXUGO))
1135 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1136 void *dstbuf, filldir_t filldir)
1138 while (nbytes >= FUSE_NAME_OFFSET) {
1139 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1140 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1142 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1144 if (reclen > nbytes)
1147 over = filldir(dstbuf, dirent->name, dirent->namelen,
1148 file->f_pos, dirent->ino, dirent->type);
1154 file->f_pos = dirent->off;
1160 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1165 struct inode *inode = file->f_path.dentry->d_inode;
1166 struct fuse_conn *fc = get_fuse_conn(inode);
1167 struct fuse_req *req;
1169 if (is_bad_inode(inode))
1172 req = fuse_get_req(fc);
1174 return PTR_ERR(req);
1176 page = alloc_page(GFP_KERNEL);
1178 fuse_put_request(fc, req);
1181 req->out.argpages = 1;
1183 req->pages[0] = page;
1184 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1185 fuse_request_send(fc, req);
1186 nbytes = req->out.args[0].size;
1187 err = req->out.h.error;
1188 fuse_put_request(fc, req);
1190 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1194 fuse_invalidate_attr(inode); /* atime changed */
1198 static char *read_link(struct dentry *dentry)
1200 struct inode *inode = dentry->d_inode;
1201 struct fuse_conn *fc = get_fuse_conn(inode);
1202 struct fuse_req *req = fuse_get_req(fc);
1206 return ERR_CAST(req);
1208 link = (char *) __get_free_page(GFP_KERNEL);
1210 link = ERR_PTR(-ENOMEM);
1213 req->in.h.opcode = FUSE_READLINK;
1214 req->in.h.nodeid = get_node_id(inode);
1215 req->out.argvar = 1;
1216 req->out.numargs = 1;
1217 req->out.args[0].size = PAGE_SIZE - 1;
1218 req->out.args[0].value = link;
1219 fuse_request_send(fc, req);
1220 if (req->out.h.error) {
1221 free_page((unsigned long) link);
1222 link = ERR_PTR(req->out.h.error);
1224 link[req->out.args[0].size] = '\0';
1226 fuse_put_request(fc, req);
1227 fuse_invalidate_attr(inode); /* atime changed */
1231 static void free_link(char *link)
1234 free_page((unsigned long) link);
1237 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1239 nd_set_link(nd, read_link(dentry));
1243 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1245 free_link(nd_get_link(nd));
1248 static int fuse_dir_open(struct inode *inode, struct file *file)
1250 return fuse_open_common(inode, file, true);
1253 static int fuse_dir_release(struct inode *inode, struct file *file)
1255 fuse_release_common(file, FUSE_RELEASEDIR);
1260 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1263 return fuse_fsync_common(file, start, end, datasync, 1);
1266 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1269 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1271 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1275 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1278 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1281 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1286 return fuse_ioctl_common(file, cmd, arg,
1287 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1290 static bool update_mtime(unsigned ivalid)
1292 /* Always update if mtime is explicitly set */
1293 if (ivalid & ATTR_MTIME_SET)
1296 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1297 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1300 /* In all other cases update */
1304 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1306 unsigned ivalid = iattr->ia_valid;
1308 if (ivalid & ATTR_MODE)
1309 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1310 if (ivalid & ATTR_UID)
1311 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1312 if (ivalid & ATTR_GID)
1313 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1314 if (ivalid & ATTR_SIZE)
1315 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1316 if (ivalid & ATTR_ATIME) {
1317 arg->valid |= FATTR_ATIME;
1318 arg->atime = iattr->ia_atime.tv_sec;
1319 arg->atimensec = iattr->ia_atime.tv_nsec;
1320 if (!(ivalid & ATTR_ATIME_SET))
1321 arg->valid |= FATTR_ATIME_NOW;
1323 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1324 arg->valid |= FATTR_MTIME;
1325 arg->mtime = iattr->ia_mtime.tv_sec;
1326 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1327 if (!(ivalid & ATTR_MTIME_SET))
1328 arg->valid |= FATTR_MTIME_NOW;
1333 * Prevent concurrent writepages on inode
1335 * This is done by adding a negative bias to the inode write counter
1336 * and waiting for all pending writes to finish.
1338 void fuse_set_nowrite(struct inode *inode)
1340 struct fuse_conn *fc = get_fuse_conn(inode);
1341 struct fuse_inode *fi = get_fuse_inode(inode);
1343 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1345 spin_lock(&fc->lock);
1346 BUG_ON(fi->writectr < 0);
1347 fi->writectr += FUSE_NOWRITE;
1348 spin_unlock(&fc->lock);
1349 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1353 * Allow writepages on inode
1355 * Remove the bias from the writecounter and send any queued
1358 static void __fuse_release_nowrite(struct inode *inode)
1360 struct fuse_inode *fi = get_fuse_inode(inode);
1362 BUG_ON(fi->writectr != FUSE_NOWRITE);
1364 fuse_flush_writepages(inode);
1367 void fuse_release_nowrite(struct inode *inode)
1369 struct fuse_conn *fc = get_fuse_conn(inode);
1371 spin_lock(&fc->lock);
1372 __fuse_release_nowrite(inode);
1373 spin_unlock(&fc->lock);
1377 * Set attributes, and at the same time refresh them.
1379 * Truncation is slightly complicated, because the 'truncate' request
1380 * may fail, in which case we don't want to touch the mapping.
1381 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1382 * and the actual truncation by hand.
1384 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1387 struct inode *inode = entry->d_inode;
1388 struct fuse_conn *fc = get_fuse_conn(inode);
1389 struct fuse_req *req;
1390 struct fuse_setattr_in inarg;
1391 struct fuse_attr_out outarg;
1392 bool is_truncate = false;
1396 if (!fuse_allow_task(fc, current))
1399 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1400 attr->ia_valid |= ATTR_FORCE;
1402 err = inode_change_ok(inode, attr);
1406 if (attr->ia_valid & ATTR_OPEN) {
1407 if (fc->atomic_o_trunc)
1412 if (attr->ia_valid & ATTR_SIZE)
1415 req = fuse_get_req(fc);
1417 return PTR_ERR(req);
1420 fuse_set_nowrite(inode);
1422 memset(&inarg, 0, sizeof(inarg));
1423 memset(&outarg, 0, sizeof(outarg));
1424 iattr_to_fattr(attr, &inarg);
1426 struct fuse_file *ff = file->private_data;
1427 inarg.valid |= FATTR_FH;
1430 if (attr->ia_valid & ATTR_SIZE) {
1431 /* For mandatory locking in truncate */
1432 inarg.valid |= FATTR_LOCKOWNER;
1433 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1435 req->in.h.opcode = FUSE_SETATTR;
1436 req->in.h.nodeid = get_node_id(inode);
1437 req->in.numargs = 1;
1438 req->in.args[0].size = sizeof(inarg);
1439 req->in.args[0].value = &inarg;
1440 req->out.numargs = 1;
1442 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1444 req->out.args[0].size = sizeof(outarg);
1445 req->out.args[0].value = &outarg;
1446 fuse_request_send(fc, req);
1447 err = req->out.h.error;
1448 fuse_put_request(fc, req);
1451 fuse_invalidate_attr(inode);
1455 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1456 make_bad_inode(inode);
1461 spin_lock(&fc->lock);
1462 fuse_change_attributes_common(inode, &outarg.attr,
1463 attr_timeout(&outarg));
1464 oldsize = inode->i_size;
1465 i_size_write(inode, outarg.attr.size);
1468 /* NOTE: this may release/reacquire fc->lock */
1469 __fuse_release_nowrite(inode);
1471 spin_unlock(&fc->lock);
1474 * Only call invalidate_inode_pages2() after removing
1475 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1477 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1478 truncate_pagecache(inode, oldsize, outarg.attr.size);
1479 invalidate_inode_pages2(inode->i_mapping);
1486 fuse_release_nowrite(inode);
1491 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1493 if (attr->ia_valid & ATTR_FILE)
1494 return fuse_do_setattr(entry, attr, attr->ia_file);
1496 return fuse_do_setattr(entry, attr, NULL);
1499 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1502 struct inode *inode = entry->d_inode;
1503 struct fuse_conn *fc = get_fuse_conn(inode);
1505 if (!fuse_allow_task(fc, current))
1508 return fuse_update_attributes(inode, stat, NULL, NULL);
1511 static int fuse_setxattr(struct dentry *entry, const char *name,
1512 const void *value, size_t size, int flags)
1514 struct inode *inode = entry->d_inode;
1515 struct fuse_conn *fc = get_fuse_conn(inode);
1516 struct fuse_req *req;
1517 struct fuse_setxattr_in inarg;
1520 if (fc->no_setxattr)
1523 req = fuse_get_req(fc);
1525 return PTR_ERR(req);
1527 memset(&inarg, 0, sizeof(inarg));
1529 inarg.flags = flags;
1530 req->in.h.opcode = FUSE_SETXATTR;
1531 req->in.h.nodeid = get_node_id(inode);
1532 req->in.numargs = 3;
1533 req->in.args[0].size = sizeof(inarg);
1534 req->in.args[0].value = &inarg;
1535 req->in.args[1].size = strlen(name) + 1;
1536 req->in.args[1].value = name;
1537 req->in.args[2].size = size;
1538 req->in.args[2].value = value;
1539 fuse_request_send(fc, req);
1540 err = req->out.h.error;
1541 fuse_put_request(fc, req);
1542 if (err == -ENOSYS) {
1543 fc->no_setxattr = 1;
1549 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1550 void *value, size_t size)
1552 struct inode *inode = entry->d_inode;
1553 struct fuse_conn *fc = get_fuse_conn(inode);
1554 struct fuse_req *req;
1555 struct fuse_getxattr_in inarg;
1556 struct fuse_getxattr_out outarg;
1559 if (fc->no_getxattr)
1562 req = fuse_get_req(fc);
1564 return PTR_ERR(req);
1566 memset(&inarg, 0, sizeof(inarg));
1568 req->in.h.opcode = FUSE_GETXATTR;
1569 req->in.h.nodeid = get_node_id(inode);
1570 req->in.numargs = 2;
1571 req->in.args[0].size = sizeof(inarg);
1572 req->in.args[0].value = &inarg;
1573 req->in.args[1].size = strlen(name) + 1;
1574 req->in.args[1].value = name;
1575 /* This is really two different operations rolled into one */
1576 req->out.numargs = 1;
1578 req->out.argvar = 1;
1579 req->out.args[0].size = size;
1580 req->out.args[0].value = value;
1582 req->out.args[0].size = sizeof(outarg);
1583 req->out.args[0].value = &outarg;
1585 fuse_request_send(fc, req);
1586 ret = req->out.h.error;
1588 ret = size ? req->out.args[0].size : outarg.size;
1590 if (ret == -ENOSYS) {
1591 fc->no_getxattr = 1;
1595 fuse_put_request(fc, req);
1599 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1601 struct inode *inode = entry->d_inode;
1602 struct fuse_conn *fc = get_fuse_conn(inode);
1603 struct fuse_req *req;
1604 struct fuse_getxattr_in inarg;
1605 struct fuse_getxattr_out outarg;
1608 if (!fuse_allow_task(fc, current))
1611 if (fc->no_listxattr)
1614 req = fuse_get_req(fc);
1616 return PTR_ERR(req);
1618 memset(&inarg, 0, sizeof(inarg));
1620 req->in.h.opcode = FUSE_LISTXATTR;
1621 req->in.h.nodeid = get_node_id(inode);
1622 req->in.numargs = 1;
1623 req->in.args[0].size = sizeof(inarg);
1624 req->in.args[0].value = &inarg;
1625 /* This is really two different operations rolled into one */
1626 req->out.numargs = 1;
1628 req->out.argvar = 1;
1629 req->out.args[0].size = size;
1630 req->out.args[0].value = list;
1632 req->out.args[0].size = sizeof(outarg);
1633 req->out.args[0].value = &outarg;
1635 fuse_request_send(fc, req);
1636 ret = req->out.h.error;
1638 ret = size ? req->out.args[0].size : outarg.size;
1640 if (ret == -ENOSYS) {
1641 fc->no_listxattr = 1;
1645 fuse_put_request(fc, req);
1649 static int fuse_removexattr(struct dentry *entry, const char *name)
1651 struct inode *inode = entry->d_inode;
1652 struct fuse_conn *fc = get_fuse_conn(inode);
1653 struct fuse_req *req;
1656 if (fc->no_removexattr)
1659 req = fuse_get_req(fc);
1661 return PTR_ERR(req);
1663 req->in.h.opcode = FUSE_REMOVEXATTR;
1664 req->in.h.nodeid = get_node_id(inode);
1665 req->in.numargs = 1;
1666 req->in.args[0].size = strlen(name) + 1;
1667 req->in.args[0].value = name;
1668 fuse_request_send(fc, req);
1669 err = req->out.h.error;
1670 fuse_put_request(fc, req);
1671 if (err == -ENOSYS) {
1672 fc->no_removexattr = 1;
1678 static const struct inode_operations fuse_dir_inode_operations = {
1679 .lookup = fuse_lookup,
1680 .mkdir = fuse_mkdir,
1681 .symlink = fuse_symlink,
1682 .unlink = fuse_unlink,
1683 .rmdir = fuse_rmdir,
1684 .rename = fuse_rename,
1686 .setattr = fuse_setattr,
1687 .create = fuse_create,
1688 .atomic_open = fuse_atomic_open,
1689 .mknod = fuse_mknod,
1690 .permission = fuse_permission,
1691 .getattr = fuse_getattr,
1692 .setxattr = fuse_setxattr,
1693 .getxattr = fuse_getxattr,
1694 .listxattr = fuse_listxattr,
1695 .removexattr = fuse_removexattr,
1698 static const struct file_operations fuse_dir_operations = {
1699 .llseek = generic_file_llseek,
1700 .read = generic_read_dir,
1701 .readdir = fuse_readdir,
1702 .open = fuse_dir_open,
1703 .release = fuse_dir_release,
1704 .fsync = fuse_dir_fsync,
1705 .unlocked_ioctl = fuse_dir_ioctl,
1706 .compat_ioctl = fuse_dir_compat_ioctl,
1709 static const struct inode_operations fuse_common_inode_operations = {
1710 .setattr = fuse_setattr,
1711 .permission = fuse_permission,
1712 .getattr = fuse_getattr,
1713 .setxattr = fuse_setxattr,
1714 .getxattr = fuse_getxattr,
1715 .listxattr = fuse_listxattr,
1716 .removexattr = fuse_removexattr,
1719 static const struct inode_operations fuse_symlink_inode_operations = {
1720 .setattr = fuse_setattr,
1721 .follow_link = fuse_follow_link,
1722 .put_link = fuse_put_link,
1723 .readlink = generic_readlink,
1724 .getattr = fuse_getattr,
1725 .setxattr = fuse_setxattr,
1726 .getxattr = fuse_getxattr,
1727 .listxattr = fuse_listxattr,
1728 .removexattr = fuse_removexattr,
1731 void fuse_init_common(struct inode *inode)
1733 inode->i_op = &fuse_common_inode_operations;
1736 void fuse_init_dir(struct inode *inode)
1738 inode->i_op = &fuse_dir_inode_operations;
1739 inode->i_fop = &fuse_dir_operations;
1742 void fuse_init_symlink(struct inode *inode)
1744 inode->i_op = &fuse_symlink_inode_operations;