2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/file.h>
13 #include <linux/gfp.h>
14 #include <linux/sched.h>
15 #include <linux/namei.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)
159 struct inode *inode = entry->d_inode;
161 if (inode && is_bad_inode(inode))
163 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
165 struct fuse_entry_out outarg;
166 struct fuse_conn *fc;
167 struct fuse_req *req;
168 struct fuse_req *forget_req;
169 struct dentry *parent;
172 /* For negative dentries, always do a fresh lookup */
176 fc = get_fuse_conn(inode);
177 req = fuse_get_req(fc);
181 forget_req = fuse_get_req(fc);
182 if (IS_ERR(forget_req)) {
183 fuse_put_request(fc, req);
187 attr_version = fuse_get_attr_version(fc);
189 parent = dget_parent(entry);
190 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
191 &entry->d_name, &outarg);
192 fuse_request_send(fc, req);
194 err = req->out.h.error;
195 fuse_put_request(fc, req);
196 /* Zero nodeid is same as -ENOENT */
197 if (!err && !outarg.nodeid)
200 struct fuse_inode *fi = get_fuse_inode(inode);
201 if (outarg.nodeid != get_node_id(inode)) {
202 fuse_send_forget(fc, forget_req,
206 spin_lock(&fc->lock);
208 spin_unlock(&fc->lock);
210 fuse_put_request(fc, forget_req);
211 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
214 fuse_change_attributes(inode, &outarg.attr,
215 entry_attr_timeout(&outarg),
217 fuse_change_entry_timeout(entry, &outarg);
222 static int invalid_nodeid(u64 nodeid)
224 return !nodeid || nodeid == FUSE_ROOT_ID;
227 const struct dentry_operations fuse_dentry_operations = {
228 .d_revalidate = fuse_dentry_revalidate,
231 int fuse_valid_type(int m)
233 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
234 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
238 * Add a directory inode to a dentry, ensuring that no other dentry
239 * refers to this inode. Called with fc->inst_mutex.
241 static struct dentry *fuse_d_add_directory(struct dentry *entry,
244 struct dentry *alias = d_find_alias(inode);
245 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
246 /* This tries to shrink the subtree below alias */
247 fuse_invalidate_entry(alias);
249 if (!list_empty(&inode->i_dentry))
250 return ERR_PTR(-EBUSY);
254 return d_splice_alias(inode, entry);
257 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
258 struct fuse_entry_out *outarg, struct inode **inode)
260 struct fuse_conn *fc = get_fuse_conn_super(sb);
261 struct fuse_req *req;
262 struct fuse_req *forget_req;
268 if (name->len > FUSE_NAME_MAX)
271 req = fuse_get_req(fc);
276 forget_req = fuse_get_req(fc);
277 err = PTR_ERR(forget_req);
278 if (IS_ERR(forget_req)) {
279 fuse_put_request(fc, req);
283 attr_version = fuse_get_attr_version(fc);
285 fuse_lookup_init(fc, req, nodeid, name, outarg);
286 fuse_request_send(fc, req);
287 err = req->out.h.error;
288 fuse_put_request(fc, req);
289 /* Zero nodeid is same as -ENOENT, but with valid timeout */
290 if (err || !outarg->nodeid)
296 if (!fuse_valid_type(outarg->attr.mode))
299 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
300 &outarg->attr, entry_attr_timeout(outarg),
304 fuse_send_forget(fc, forget_req, outarg->nodeid, 1);
310 fuse_put_request(fc, forget_req);
315 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
316 struct nameidata *nd)
319 struct fuse_entry_out outarg;
321 struct dentry *newent;
322 struct fuse_conn *fc = get_fuse_conn(dir);
323 bool outarg_valid = true;
325 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
327 if (err == -ENOENT) {
328 outarg_valid = false;
335 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
338 if (inode && S_ISDIR(inode->i_mode)) {
339 mutex_lock(&fc->inst_mutex);
340 newent = fuse_d_add_directory(entry, inode);
341 mutex_unlock(&fc->inst_mutex);
342 err = PTR_ERR(newent);
346 newent = d_splice_alias(inode, entry);
349 entry = newent ? newent : entry;
350 entry->d_op = &fuse_dentry_operations;
352 fuse_change_entry_timeout(entry, &outarg);
354 fuse_invalidate_entry_cache(entry);
365 * Synchronous release for the case when something goes wrong in CREATE_OPEN
367 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
370 fuse_release_fill(ff, flags, FUSE_RELEASE);
371 ff->reserved_req->force = 1;
372 fuse_request_send(fc, ff->reserved_req);
373 fuse_put_request(fc, ff->reserved_req);
378 * Atomic create+open operation
380 * If the filesystem doesn't support this, then fall back to separate
381 * 'mknod' + 'open' requests.
383 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
384 struct nameidata *nd)
388 struct fuse_conn *fc = get_fuse_conn(dir);
389 struct fuse_req *req;
390 struct fuse_req *forget_req;
391 struct fuse_open_in inarg;
392 struct fuse_open_out outopen;
393 struct fuse_entry_out outentry;
394 struct fuse_file *ff;
396 int flags = nd->intent.open.flags - 1;
401 forget_req = fuse_get_req(fc);
402 if (IS_ERR(forget_req))
403 return PTR_ERR(forget_req);
405 req = fuse_get_req(fc);
408 goto out_put_forget_req;
411 ff = fuse_file_alloc(fc);
413 goto out_put_request;
416 memset(&inarg, 0, sizeof(inarg));
417 memset(&outentry, 0, sizeof(outentry));
420 req->in.h.opcode = FUSE_CREATE;
421 req->in.h.nodeid = get_node_id(dir);
423 req->in.args[0].size = sizeof(inarg);
424 req->in.args[0].value = &inarg;
425 req->in.args[1].size = entry->d_name.len + 1;
426 req->in.args[1].value = entry->d_name.name;
427 req->out.numargs = 2;
429 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
431 req->out.args[0].size = sizeof(outentry);
432 req->out.args[0].value = &outentry;
433 req->out.args[1].size = sizeof(outopen);
434 req->out.args[1].value = &outopen;
435 fuse_request_send(fc, req);
436 err = req->out.h.error;
444 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
447 fuse_put_request(fc, req);
449 ff->nodeid = outentry.nodeid;
450 ff->open_flags = outopen.open_flags;
451 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
452 &outentry.attr, entry_attr_timeout(&outentry), 0);
454 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
455 fuse_sync_release(fc, ff, flags);
456 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
459 fuse_put_request(fc, forget_req);
460 d_instantiate(entry, inode);
461 fuse_change_entry_timeout(entry, &outentry);
462 fuse_invalidate_attr(dir);
463 file = lookup_instantiate_filp(nd, entry, generic_file_open);
465 fuse_sync_release(fc, ff, flags);
466 return PTR_ERR(file);
468 file->private_data = fuse_file_get(ff);
469 fuse_finish_open(inode, file);
475 fuse_put_request(fc, req);
477 fuse_put_request(fc, forget_req);
482 * Code shared between mknod, mkdir, symlink and link
484 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
485 struct inode *dir, struct dentry *entry,
488 struct fuse_entry_out outarg;
491 struct fuse_req *forget_req;
493 forget_req = fuse_get_req(fc);
494 if (IS_ERR(forget_req)) {
495 fuse_put_request(fc, req);
496 return PTR_ERR(forget_req);
499 memset(&outarg, 0, sizeof(outarg));
500 req->in.h.nodeid = get_node_id(dir);
501 req->out.numargs = 1;
503 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
505 req->out.args[0].size = sizeof(outarg);
506 req->out.args[0].value = &outarg;
507 fuse_request_send(fc, req);
508 err = req->out.h.error;
509 fuse_put_request(fc, req);
511 goto out_put_forget_req;
514 if (invalid_nodeid(outarg.nodeid))
515 goto out_put_forget_req;
517 if ((outarg.attr.mode ^ mode) & S_IFMT)
518 goto out_put_forget_req;
520 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
521 &outarg.attr, entry_attr_timeout(&outarg), 0);
523 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
526 fuse_put_request(fc, forget_req);
528 if (S_ISDIR(inode->i_mode)) {
529 struct dentry *alias;
530 mutex_lock(&fc->inst_mutex);
531 alias = d_find_alias(inode);
533 /* New directory must have moved since mkdir */
534 mutex_unlock(&fc->inst_mutex);
539 d_instantiate(entry, inode);
540 mutex_unlock(&fc->inst_mutex);
542 d_instantiate(entry, inode);
544 fuse_change_entry_timeout(entry, &outarg);
545 fuse_invalidate_attr(dir);
549 fuse_put_request(fc, forget_req);
553 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
556 struct fuse_mknod_in inarg;
557 struct fuse_conn *fc = get_fuse_conn(dir);
558 struct fuse_req *req = fuse_get_req(fc);
562 memset(&inarg, 0, sizeof(inarg));
564 inarg.rdev = new_encode_dev(rdev);
565 req->in.h.opcode = FUSE_MKNOD;
567 req->in.args[0].size = sizeof(inarg);
568 req->in.args[0].value = &inarg;
569 req->in.args[1].size = entry->d_name.len + 1;
570 req->in.args[1].value = entry->d_name.name;
571 return create_new_entry(fc, req, dir, entry, mode);
574 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
575 struct nameidata *nd)
577 if (nd && (nd->flags & LOOKUP_OPEN)) {
578 int err = fuse_create_open(dir, entry, mode, nd);
581 /* Fall back on mknod */
583 return fuse_mknod(dir, entry, mode, 0);
586 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
588 struct fuse_mkdir_in inarg;
589 struct fuse_conn *fc = get_fuse_conn(dir);
590 struct fuse_req *req = fuse_get_req(fc);
594 memset(&inarg, 0, sizeof(inarg));
596 req->in.h.opcode = FUSE_MKDIR;
598 req->in.args[0].size = sizeof(inarg);
599 req->in.args[0].value = &inarg;
600 req->in.args[1].size = entry->d_name.len + 1;
601 req->in.args[1].value = entry->d_name.name;
602 return create_new_entry(fc, req, dir, entry, S_IFDIR);
605 static int fuse_symlink(struct inode *dir, struct dentry *entry,
608 struct fuse_conn *fc = get_fuse_conn(dir);
609 unsigned len = strlen(link) + 1;
610 struct fuse_req *req = fuse_get_req(fc);
614 req->in.h.opcode = FUSE_SYMLINK;
616 req->in.args[0].size = entry->d_name.len + 1;
617 req->in.args[0].value = entry->d_name.name;
618 req->in.args[1].size = len;
619 req->in.args[1].value = link;
620 return create_new_entry(fc, req, dir, entry, S_IFLNK);
623 static int fuse_unlink(struct inode *dir, struct dentry *entry)
626 struct fuse_conn *fc = get_fuse_conn(dir);
627 struct fuse_req *req = fuse_get_req(fc);
631 req->in.h.opcode = FUSE_UNLINK;
632 req->in.h.nodeid = get_node_id(dir);
634 req->in.args[0].size = entry->d_name.len + 1;
635 req->in.args[0].value = entry->d_name.name;
636 fuse_request_send(fc, req);
637 err = req->out.h.error;
638 fuse_put_request(fc, req);
640 struct inode *inode = entry->d_inode;
643 * Set nlink to zero so the inode can be cleared, if the inode
644 * does have more links this will be discovered at the next
648 fuse_invalidate_attr(inode);
649 fuse_invalidate_attr(dir);
650 fuse_invalidate_entry_cache(entry);
651 } else if (err == -EINTR)
652 fuse_invalidate_entry(entry);
656 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
659 struct fuse_conn *fc = get_fuse_conn(dir);
660 struct fuse_req *req = fuse_get_req(fc);
664 req->in.h.opcode = FUSE_RMDIR;
665 req->in.h.nodeid = get_node_id(dir);
667 req->in.args[0].size = entry->d_name.len + 1;
668 req->in.args[0].value = entry->d_name.name;
669 fuse_request_send(fc, req);
670 err = req->out.h.error;
671 fuse_put_request(fc, req);
673 clear_nlink(entry->d_inode);
674 fuse_invalidate_attr(dir);
675 fuse_invalidate_entry_cache(entry);
676 } else if (err == -EINTR)
677 fuse_invalidate_entry(entry);
681 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
682 struct inode *newdir, struct dentry *newent)
685 struct fuse_rename_in inarg;
686 struct fuse_conn *fc = get_fuse_conn(olddir);
687 struct fuse_req *req = fuse_get_req(fc);
691 memset(&inarg, 0, sizeof(inarg));
692 inarg.newdir = get_node_id(newdir);
693 req->in.h.opcode = FUSE_RENAME;
694 req->in.h.nodeid = get_node_id(olddir);
696 req->in.args[0].size = sizeof(inarg);
697 req->in.args[0].value = &inarg;
698 req->in.args[1].size = oldent->d_name.len + 1;
699 req->in.args[1].value = oldent->d_name.name;
700 req->in.args[2].size = newent->d_name.len + 1;
701 req->in.args[2].value = newent->d_name.name;
702 fuse_request_send(fc, req);
703 err = req->out.h.error;
704 fuse_put_request(fc, req);
707 fuse_invalidate_attr(oldent->d_inode);
709 fuse_invalidate_attr(olddir);
710 if (olddir != newdir)
711 fuse_invalidate_attr(newdir);
713 /* newent will end up negative */
715 fuse_invalidate_entry_cache(newent);
716 } else if (err == -EINTR) {
717 /* If request was interrupted, DEITY only knows if the
718 rename actually took place. If the invalidation
719 fails (e.g. some process has CWD under the renamed
720 directory), then there can be inconsistency between
721 the dcache and the real filesystem. Tough luck. */
722 fuse_invalidate_entry(oldent);
724 fuse_invalidate_entry(newent);
730 static int fuse_link(struct dentry *entry, struct inode *newdir,
731 struct dentry *newent)
734 struct fuse_link_in inarg;
735 struct inode *inode = entry->d_inode;
736 struct fuse_conn *fc = get_fuse_conn(inode);
737 struct fuse_req *req = fuse_get_req(fc);
741 memset(&inarg, 0, sizeof(inarg));
742 inarg.oldnodeid = get_node_id(inode);
743 req->in.h.opcode = FUSE_LINK;
745 req->in.args[0].size = sizeof(inarg);
746 req->in.args[0].value = &inarg;
747 req->in.args[1].size = newent->d_name.len + 1;
748 req->in.args[1].value = newent->d_name.name;
749 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
750 /* Contrary to "normal" filesystems it can happen that link
751 makes two "logical" inodes point to the same "physical"
752 inode. We invalidate the attributes of the old one, so it
753 will reflect changes in the backing inode (link count,
756 if (!err || err == -EINTR)
757 fuse_invalidate_attr(inode);
761 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
764 stat->dev = inode->i_sb->s_dev;
765 stat->ino = attr->ino;
766 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
767 stat->nlink = attr->nlink;
768 stat->uid = attr->uid;
769 stat->gid = attr->gid;
770 stat->rdev = inode->i_rdev;
771 stat->atime.tv_sec = attr->atime;
772 stat->atime.tv_nsec = attr->atimensec;
773 stat->mtime.tv_sec = attr->mtime;
774 stat->mtime.tv_nsec = attr->mtimensec;
775 stat->ctime.tv_sec = attr->ctime;
776 stat->ctime.tv_nsec = attr->ctimensec;
777 stat->size = attr->size;
778 stat->blocks = attr->blocks;
779 stat->blksize = (1 << inode->i_blkbits);
782 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
786 struct fuse_getattr_in inarg;
787 struct fuse_attr_out outarg;
788 struct fuse_conn *fc = get_fuse_conn(inode);
789 struct fuse_req *req;
792 req = fuse_get_req(fc);
796 attr_version = fuse_get_attr_version(fc);
798 memset(&inarg, 0, sizeof(inarg));
799 memset(&outarg, 0, sizeof(outarg));
800 /* Directories have separate file-handle space */
801 if (file && S_ISREG(inode->i_mode)) {
802 struct fuse_file *ff = file->private_data;
804 inarg.getattr_flags |= FUSE_GETATTR_FH;
807 req->in.h.opcode = FUSE_GETATTR;
808 req->in.h.nodeid = get_node_id(inode);
810 req->in.args[0].size = sizeof(inarg);
811 req->in.args[0].value = &inarg;
812 req->out.numargs = 1;
814 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
816 req->out.args[0].size = sizeof(outarg);
817 req->out.args[0].value = &outarg;
818 fuse_request_send(fc, req);
819 err = req->out.h.error;
820 fuse_put_request(fc, req);
822 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
823 make_bad_inode(inode);
826 fuse_change_attributes(inode, &outarg.attr,
827 attr_timeout(&outarg),
830 fuse_fillattr(inode, &outarg.attr, stat);
836 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
837 struct file *file, bool *refreshed)
839 struct fuse_inode *fi = get_fuse_inode(inode);
843 if (fi->i_time < get_jiffies_64()) {
845 err = fuse_do_getattr(inode, stat, file);
850 generic_fillattr(inode, stat);
851 stat->mode = fi->orig_i_mode;
855 if (refreshed != NULL)
862 * Calling into a user-controlled filesystem gives the filesystem
863 * daemon ptrace-like capabilities over the requester process. This
864 * means, that the filesystem daemon is able to record the exact
865 * filesystem operations performed, and can also control the behavior
866 * of the requester process in otherwise impossible ways. For example
867 * it can delay the operation for arbitrary length of time allowing
868 * DoS against the requester.
870 * For this reason only those processes can call into the filesystem,
871 * for which the owner of the mount has ptrace privilege. This
872 * excludes processes started by other users, suid or sgid processes.
874 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
876 const struct cred *cred;
879 if (fc->flags & FUSE_ALLOW_OTHER)
884 cred = __task_cred(task);
885 if (cred->euid == fc->user_id &&
886 cred->suid == fc->user_id &&
887 cred->uid == fc->user_id &&
888 cred->egid == fc->group_id &&
889 cred->sgid == fc->group_id &&
890 cred->gid == fc->group_id)
897 static int fuse_access(struct inode *inode, int mask)
899 struct fuse_conn *fc = get_fuse_conn(inode);
900 struct fuse_req *req;
901 struct fuse_access_in inarg;
907 req = fuse_get_req(fc);
911 memset(&inarg, 0, sizeof(inarg));
912 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
913 req->in.h.opcode = FUSE_ACCESS;
914 req->in.h.nodeid = get_node_id(inode);
916 req->in.args[0].size = sizeof(inarg);
917 req->in.args[0].value = &inarg;
918 fuse_request_send(fc, req);
919 err = req->out.h.error;
920 fuse_put_request(fc, req);
921 if (err == -ENOSYS) {
929 * Check permission. The two basic access models of FUSE are:
931 * 1) Local access checking ('default_permissions' mount option) based
932 * on file mode. This is the plain old disk filesystem permission
935 * 2) "Remote" access checking, where server is responsible for
936 * checking permission in each inode operation. An exception to this
937 * is if ->permission() was invoked from sys_access() in which case an
938 * access request is sent. Execute permission is still checked
939 * locally based on file mode.
941 static int fuse_permission(struct inode *inode, int mask)
943 struct fuse_conn *fc = get_fuse_conn(inode);
944 bool refreshed = false;
947 if (!fuse_allow_task(fc, current))
951 * If attributes are needed, refresh them before proceeding
953 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
954 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
955 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
960 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
961 err = generic_permission(inode, mask, NULL);
963 /* If permission is denied, try to refresh file
964 attributes. This is also needed, because the root
965 node will at first have no permissions */
966 if (err == -EACCES && !refreshed) {
967 err = fuse_do_getattr(inode, NULL, NULL);
969 err = generic_permission(inode, mask, NULL);
972 /* Note: the opposite of the above test does not
973 exist. So if permissions are revoked this won't be
974 noticed immediately, only after the attribute
975 timeout has expired */
976 } else if (mask & MAY_ACCESS) {
977 err = fuse_access(inode, mask);
978 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
979 if (!(inode->i_mode & S_IXUGO)) {
983 err = fuse_do_getattr(inode, NULL, NULL);
984 if (!err && !(inode->i_mode & S_IXUGO))
991 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
992 void *dstbuf, filldir_t filldir)
994 while (nbytes >= FUSE_NAME_OFFSET) {
995 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
996 size_t reclen = FUSE_DIRENT_SIZE(dirent);
998 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1000 if (reclen > nbytes)
1003 over = filldir(dstbuf, dirent->name, dirent->namelen,
1004 file->f_pos, dirent->ino, dirent->type);
1010 file->f_pos = dirent->off;
1016 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1021 struct inode *inode = file->f_path.dentry->d_inode;
1022 struct fuse_conn *fc = get_fuse_conn(inode);
1023 struct fuse_req *req;
1025 if (is_bad_inode(inode))
1028 req = fuse_get_req(fc);
1030 return PTR_ERR(req);
1032 page = alloc_page(GFP_KERNEL);
1034 fuse_put_request(fc, req);
1037 req->out.argpages = 1;
1039 req->pages[0] = page;
1040 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1041 fuse_request_send(fc, req);
1042 nbytes = req->out.args[0].size;
1043 err = req->out.h.error;
1044 fuse_put_request(fc, req);
1046 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1050 fuse_invalidate_attr(inode); /* atime changed */
1054 static char *read_link(struct dentry *dentry)
1056 struct inode *inode = dentry->d_inode;
1057 struct fuse_conn *fc = get_fuse_conn(inode);
1058 struct fuse_req *req = fuse_get_req(fc);
1062 return ERR_CAST(req);
1064 link = (char *) __get_free_page(GFP_KERNEL);
1066 link = ERR_PTR(-ENOMEM);
1069 req->in.h.opcode = FUSE_READLINK;
1070 req->in.h.nodeid = get_node_id(inode);
1071 req->out.argvar = 1;
1072 req->out.numargs = 1;
1073 req->out.args[0].size = PAGE_SIZE - 1;
1074 req->out.args[0].value = link;
1075 fuse_request_send(fc, req);
1076 if (req->out.h.error) {
1077 free_page((unsigned long) link);
1078 link = ERR_PTR(req->out.h.error);
1080 link[req->out.args[0].size] = '\0';
1082 fuse_put_request(fc, req);
1083 fuse_invalidate_attr(inode); /* atime changed */
1087 static void free_link(char *link)
1090 free_page((unsigned long) link);
1093 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1095 nd_set_link(nd, read_link(dentry));
1099 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1101 free_link(nd_get_link(nd));
1104 static int fuse_dir_open(struct inode *inode, struct file *file)
1106 return fuse_open_common(inode, file, true);
1109 static int fuse_dir_release(struct inode *inode, struct file *file)
1111 return fuse_release_common(inode, file, 1);
1114 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1116 /* nfsd can call this with no file */
1117 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1120 static bool update_mtime(unsigned ivalid)
1122 /* Always update if mtime is explicitly set */
1123 if (ivalid & ATTR_MTIME_SET)
1126 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1127 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1130 /* In all other cases update */
1134 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1136 unsigned ivalid = iattr->ia_valid;
1138 if (ivalid & ATTR_MODE)
1139 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1140 if (ivalid & ATTR_UID)
1141 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1142 if (ivalid & ATTR_GID)
1143 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1144 if (ivalid & ATTR_SIZE)
1145 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1146 if (ivalid & ATTR_ATIME) {
1147 arg->valid |= FATTR_ATIME;
1148 arg->atime = iattr->ia_atime.tv_sec;
1149 arg->atimensec = iattr->ia_atime.tv_nsec;
1150 if (!(ivalid & ATTR_ATIME_SET))
1151 arg->valid |= FATTR_ATIME_NOW;
1153 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1154 arg->valid |= FATTR_MTIME;
1155 arg->mtime = iattr->ia_mtime.tv_sec;
1156 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1157 if (!(ivalid & ATTR_MTIME_SET))
1158 arg->valid |= FATTR_MTIME_NOW;
1163 * Prevent concurrent writepages on inode
1165 * This is done by adding a negative bias to the inode write counter
1166 * and waiting for all pending writes to finish.
1168 void fuse_set_nowrite(struct inode *inode)
1170 struct fuse_conn *fc = get_fuse_conn(inode);
1171 struct fuse_inode *fi = get_fuse_inode(inode);
1173 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1175 spin_lock(&fc->lock);
1176 BUG_ON(fi->writectr < 0);
1177 fi->writectr += FUSE_NOWRITE;
1178 spin_unlock(&fc->lock);
1179 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1183 * Allow writepages on inode
1185 * Remove the bias from the writecounter and send any queued
1188 static void __fuse_release_nowrite(struct inode *inode)
1190 struct fuse_inode *fi = get_fuse_inode(inode);
1192 BUG_ON(fi->writectr != FUSE_NOWRITE);
1194 fuse_flush_writepages(inode);
1197 void fuse_release_nowrite(struct inode *inode)
1199 struct fuse_conn *fc = get_fuse_conn(inode);
1201 spin_lock(&fc->lock);
1202 __fuse_release_nowrite(inode);
1203 spin_unlock(&fc->lock);
1207 * Set attributes, and at the same time refresh them.
1209 * Truncation is slightly complicated, because the 'truncate' request
1210 * may fail, in which case we don't want to touch the mapping.
1211 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1212 * and the actual truncation by hand.
1214 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1217 struct inode *inode = entry->d_inode;
1218 struct fuse_conn *fc = get_fuse_conn(inode);
1219 struct fuse_req *req;
1220 struct fuse_setattr_in inarg;
1221 struct fuse_attr_out outarg;
1222 bool is_truncate = false;
1226 if (!fuse_allow_task(fc, current))
1229 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1230 err = inode_change_ok(inode, attr);
1235 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1238 if (attr->ia_valid & ATTR_SIZE) {
1239 unsigned long limit;
1240 if (IS_SWAPFILE(inode))
1242 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1243 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1244 send_sig(SIGXFSZ, current, 0);
1250 req = fuse_get_req(fc);
1252 return PTR_ERR(req);
1255 fuse_set_nowrite(inode);
1257 memset(&inarg, 0, sizeof(inarg));
1258 memset(&outarg, 0, sizeof(outarg));
1259 iattr_to_fattr(attr, &inarg);
1261 struct fuse_file *ff = file->private_data;
1262 inarg.valid |= FATTR_FH;
1265 if (attr->ia_valid & ATTR_SIZE) {
1266 /* For mandatory locking in truncate */
1267 inarg.valid |= FATTR_LOCKOWNER;
1268 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1270 req->in.h.opcode = FUSE_SETATTR;
1271 req->in.h.nodeid = get_node_id(inode);
1272 req->in.numargs = 1;
1273 req->in.args[0].size = sizeof(inarg);
1274 req->in.args[0].value = &inarg;
1275 req->out.numargs = 1;
1277 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1279 req->out.args[0].size = sizeof(outarg);
1280 req->out.args[0].value = &outarg;
1281 fuse_request_send(fc, req);
1282 err = req->out.h.error;
1283 fuse_put_request(fc, req);
1286 fuse_invalidate_attr(inode);
1290 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1291 make_bad_inode(inode);
1296 spin_lock(&fc->lock);
1297 fuse_change_attributes_common(inode, &outarg.attr,
1298 attr_timeout(&outarg));
1299 oldsize = inode->i_size;
1300 i_size_write(inode, outarg.attr.size);
1303 /* NOTE: this may release/reacquire fc->lock */
1304 __fuse_release_nowrite(inode);
1306 spin_unlock(&fc->lock);
1309 * Only call invalidate_inode_pages2() after removing
1310 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1312 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1313 if (outarg.attr.size < oldsize)
1314 fuse_truncate(inode->i_mapping, outarg.attr.size);
1315 invalidate_inode_pages2(inode->i_mapping);
1322 fuse_release_nowrite(inode);
1327 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1329 if (attr->ia_valid & ATTR_FILE)
1330 return fuse_do_setattr(entry, attr, attr->ia_file);
1332 return fuse_do_setattr(entry, attr, NULL);
1335 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1338 struct inode *inode = entry->d_inode;
1339 struct fuse_conn *fc = get_fuse_conn(inode);
1341 if (!fuse_allow_task(fc, current))
1344 return fuse_update_attributes(inode, stat, NULL, NULL);
1347 static int fuse_setxattr(struct dentry *entry, const char *name,
1348 const void *value, size_t size, int flags)
1350 struct inode *inode = entry->d_inode;
1351 struct fuse_conn *fc = get_fuse_conn(inode);
1352 struct fuse_req *req;
1353 struct fuse_setxattr_in inarg;
1356 if (fc->no_setxattr)
1359 req = fuse_get_req(fc);
1361 return PTR_ERR(req);
1363 memset(&inarg, 0, sizeof(inarg));
1365 inarg.flags = flags;
1366 req->in.h.opcode = FUSE_SETXATTR;
1367 req->in.h.nodeid = get_node_id(inode);
1368 req->in.numargs = 3;
1369 req->in.args[0].size = sizeof(inarg);
1370 req->in.args[0].value = &inarg;
1371 req->in.args[1].size = strlen(name) + 1;
1372 req->in.args[1].value = name;
1373 req->in.args[2].size = size;
1374 req->in.args[2].value = value;
1375 fuse_request_send(fc, req);
1376 err = req->out.h.error;
1377 fuse_put_request(fc, req);
1378 if (err == -ENOSYS) {
1379 fc->no_setxattr = 1;
1385 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1386 void *value, size_t size)
1388 struct inode *inode = entry->d_inode;
1389 struct fuse_conn *fc = get_fuse_conn(inode);
1390 struct fuse_req *req;
1391 struct fuse_getxattr_in inarg;
1392 struct fuse_getxattr_out outarg;
1395 if (fc->no_getxattr)
1398 req = fuse_get_req(fc);
1400 return PTR_ERR(req);
1402 memset(&inarg, 0, sizeof(inarg));
1404 req->in.h.opcode = FUSE_GETXATTR;
1405 req->in.h.nodeid = get_node_id(inode);
1406 req->in.numargs = 2;
1407 req->in.args[0].size = sizeof(inarg);
1408 req->in.args[0].value = &inarg;
1409 req->in.args[1].size = strlen(name) + 1;
1410 req->in.args[1].value = name;
1411 /* This is really two different operations rolled into one */
1412 req->out.numargs = 1;
1414 req->out.argvar = 1;
1415 req->out.args[0].size = size;
1416 req->out.args[0].value = value;
1418 req->out.args[0].size = sizeof(outarg);
1419 req->out.args[0].value = &outarg;
1421 fuse_request_send(fc, req);
1422 ret = req->out.h.error;
1424 ret = size ? req->out.args[0].size : outarg.size;
1426 if (ret == -ENOSYS) {
1427 fc->no_getxattr = 1;
1431 fuse_put_request(fc, req);
1435 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1437 struct inode *inode = entry->d_inode;
1438 struct fuse_conn *fc = get_fuse_conn(inode);
1439 struct fuse_req *req;
1440 struct fuse_getxattr_in inarg;
1441 struct fuse_getxattr_out outarg;
1444 if (!fuse_allow_task(fc, current))
1447 if (fc->no_listxattr)
1450 req = fuse_get_req(fc);
1452 return PTR_ERR(req);
1454 memset(&inarg, 0, sizeof(inarg));
1456 req->in.h.opcode = FUSE_LISTXATTR;
1457 req->in.h.nodeid = get_node_id(inode);
1458 req->in.numargs = 1;
1459 req->in.args[0].size = sizeof(inarg);
1460 req->in.args[0].value = &inarg;
1461 /* This is really two different operations rolled into one */
1462 req->out.numargs = 1;
1464 req->out.argvar = 1;
1465 req->out.args[0].size = size;
1466 req->out.args[0].value = list;
1468 req->out.args[0].size = sizeof(outarg);
1469 req->out.args[0].value = &outarg;
1471 fuse_request_send(fc, req);
1472 ret = req->out.h.error;
1474 ret = size ? req->out.args[0].size : outarg.size;
1476 if (ret == -ENOSYS) {
1477 fc->no_listxattr = 1;
1481 fuse_put_request(fc, req);
1485 static int fuse_removexattr(struct dentry *entry, const char *name)
1487 struct inode *inode = entry->d_inode;
1488 struct fuse_conn *fc = get_fuse_conn(inode);
1489 struct fuse_req *req;
1492 if (fc->no_removexattr)
1495 req = fuse_get_req(fc);
1497 return PTR_ERR(req);
1499 req->in.h.opcode = FUSE_REMOVEXATTR;
1500 req->in.h.nodeid = get_node_id(inode);
1501 req->in.numargs = 1;
1502 req->in.args[0].size = strlen(name) + 1;
1503 req->in.args[0].value = name;
1504 fuse_request_send(fc, req);
1505 err = req->out.h.error;
1506 fuse_put_request(fc, req);
1507 if (err == -ENOSYS) {
1508 fc->no_removexattr = 1;
1514 static const struct inode_operations fuse_dir_inode_operations = {
1515 .lookup = fuse_lookup,
1516 .mkdir = fuse_mkdir,
1517 .symlink = fuse_symlink,
1518 .unlink = fuse_unlink,
1519 .rmdir = fuse_rmdir,
1520 .rename = fuse_rename,
1522 .setattr = fuse_setattr,
1523 .create = fuse_create,
1524 .mknod = fuse_mknod,
1525 .permission = fuse_permission,
1526 .getattr = fuse_getattr,
1527 .setxattr = fuse_setxattr,
1528 .getxattr = fuse_getxattr,
1529 .listxattr = fuse_listxattr,
1530 .removexattr = fuse_removexattr,
1533 static const struct file_operations fuse_dir_operations = {
1534 .llseek = generic_file_llseek,
1535 .read = generic_read_dir,
1536 .readdir = fuse_readdir,
1537 .open = fuse_dir_open,
1538 .release = fuse_dir_release,
1539 .fsync = fuse_dir_fsync,
1542 static const struct inode_operations fuse_common_inode_operations = {
1543 .setattr = fuse_setattr,
1544 .permission = fuse_permission,
1545 .getattr = fuse_getattr,
1546 .setxattr = fuse_setxattr,
1547 .getxattr = fuse_getxattr,
1548 .listxattr = fuse_listxattr,
1549 .removexattr = fuse_removexattr,
1552 static const struct inode_operations fuse_symlink_inode_operations = {
1553 .setattr = fuse_setattr,
1554 .follow_link = fuse_follow_link,
1555 .put_link = fuse_put_link,
1556 .readlink = generic_readlink,
1557 .getattr = fuse_getattr,
1558 .setxattr = fuse_setxattr,
1559 .getxattr = fuse_getxattr,
1560 .listxattr = fuse_listxattr,
1561 .removexattr = fuse_removexattr,
1564 void fuse_init_common(struct inode *inode)
1566 inode->i_op = &fuse_common_inode_operations;
1569 void fuse_init_dir(struct inode *inode)
1571 inode->i_op = &fuse_dir_inode_operations;
1572 inode->i_fop = &fuse_dir_operations;
1575 void fuse_init_symlink(struct inode *inode)
1577 inode->i_op = &fuse_symlink_inode_operations;