2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 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 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 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 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,
368 u64 nodeid, int flags)
370 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
371 ff->reserved_req->force = 1;
372 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();
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 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);
448 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
449 &outentry.attr, entry_attr_timeout(&outentry), 0);
451 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
453 fuse_sync_release(fc, ff, outentry.nodeid, flags);
454 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
457 fuse_put_request(fc, forget_req);
458 d_instantiate(entry, inode);
459 fuse_change_entry_timeout(entry, &outentry);
460 fuse_invalidate_attr(dir);
461 file = lookup_instantiate_filp(nd, entry, generic_file_open);
464 fuse_sync_release(fc, ff, outentry.nodeid, flags);
465 return PTR_ERR(file);
467 fuse_finish_open(inode, file, ff, &outopen);
473 fuse_put_request(fc, req);
475 fuse_put_request(fc, forget_req);
480 * Code shared between mknod, mkdir, symlink and link
482 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
483 struct inode *dir, struct dentry *entry,
486 struct fuse_entry_out outarg;
489 struct fuse_req *forget_req;
491 forget_req = fuse_get_req(fc);
492 if (IS_ERR(forget_req)) {
493 fuse_put_request(fc, req);
494 return PTR_ERR(forget_req);
497 memset(&outarg, 0, sizeof(outarg));
498 req->in.h.nodeid = get_node_id(dir);
499 req->out.numargs = 1;
501 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
503 req->out.args[0].size = sizeof(outarg);
504 req->out.args[0].value = &outarg;
505 request_send(fc, req);
506 err = req->out.h.error;
507 fuse_put_request(fc, req);
509 goto out_put_forget_req;
512 if (invalid_nodeid(outarg.nodeid))
513 goto out_put_forget_req;
515 if ((outarg.attr.mode ^ mode) & S_IFMT)
516 goto out_put_forget_req;
518 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
519 &outarg.attr, entry_attr_timeout(&outarg), 0);
521 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
524 fuse_put_request(fc, forget_req);
526 if (S_ISDIR(inode->i_mode)) {
527 struct dentry *alias;
528 mutex_lock(&fc->inst_mutex);
529 alias = d_find_alias(inode);
531 /* New directory must have moved since mkdir */
532 mutex_unlock(&fc->inst_mutex);
537 d_instantiate(entry, inode);
538 mutex_unlock(&fc->inst_mutex);
540 d_instantiate(entry, inode);
542 fuse_change_entry_timeout(entry, &outarg);
543 fuse_invalidate_attr(dir);
547 fuse_put_request(fc, forget_req);
551 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
554 struct fuse_mknod_in inarg;
555 struct fuse_conn *fc = get_fuse_conn(dir);
556 struct fuse_req *req = fuse_get_req(fc);
560 memset(&inarg, 0, sizeof(inarg));
562 inarg.rdev = new_encode_dev(rdev);
563 req->in.h.opcode = FUSE_MKNOD;
565 req->in.args[0].size = sizeof(inarg);
566 req->in.args[0].value = &inarg;
567 req->in.args[1].size = entry->d_name.len + 1;
568 req->in.args[1].value = entry->d_name.name;
569 return create_new_entry(fc, req, dir, entry, mode);
572 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
573 struct nameidata *nd)
575 if (nd && (nd->flags & LOOKUP_OPEN)) {
576 int err = fuse_create_open(dir, entry, mode, nd);
579 /* Fall back on mknod */
581 return fuse_mknod(dir, entry, mode, 0);
584 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
586 struct fuse_mkdir_in inarg;
587 struct fuse_conn *fc = get_fuse_conn(dir);
588 struct fuse_req *req = fuse_get_req(fc);
592 memset(&inarg, 0, sizeof(inarg));
594 req->in.h.opcode = FUSE_MKDIR;
596 req->in.args[0].size = sizeof(inarg);
597 req->in.args[0].value = &inarg;
598 req->in.args[1].size = entry->d_name.len + 1;
599 req->in.args[1].value = entry->d_name.name;
600 return create_new_entry(fc, req, dir, entry, S_IFDIR);
603 static int fuse_symlink(struct inode *dir, struct dentry *entry,
606 struct fuse_conn *fc = get_fuse_conn(dir);
607 unsigned len = strlen(link) + 1;
608 struct fuse_req *req = fuse_get_req(fc);
612 req->in.h.opcode = FUSE_SYMLINK;
614 req->in.args[0].size = entry->d_name.len + 1;
615 req->in.args[0].value = entry->d_name.name;
616 req->in.args[1].size = len;
617 req->in.args[1].value = link;
618 return create_new_entry(fc, req, dir, entry, S_IFLNK);
621 static int fuse_unlink(struct inode *dir, struct dentry *entry)
624 struct fuse_conn *fc = get_fuse_conn(dir);
625 struct fuse_req *req = fuse_get_req(fc);
629 req->in.h.opcode = FUSE_UNLINK;
630 req->in.h.nodeid = get_node_id(dir);
632 req->in.args[0].size = entry->d_name.len + 1;
633 req->in.args[0].value = entry->d_name.name;
634 request_send(fc, req);
635 err = req->out.h.error;
636 fuse_put_request(fc, req);
638 struct inode *inode = entry->d_inode;
640 /* Set nlink to zero so the inode can be cleared, if
641 the inode does have more links this will be
642 discovered at the next lookup/getattr */
644 fuse_invalidate_attr(inode);
645 fuse_invalidate_attr(dir);
646 fuse_invalidate_entry_cache(entry);
647 } else if (err == -EINTR)
648 fuse_invalidate_entry(entry);
652 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
655 struct fuse_conn *fc = get_fuse_conn(dir);
656 struct fuse_req *req = fuse_get_req(fc);
660 req->in.h.opcode = FUSE_RMDIR;
661 req->in.h.nodeid = get_node_id(dir);
663 req->in.args[0].size = entry->d_name.len + 1;
664 req->in.args[0].value = entry->d_name.name;
665 request_send(fc, req);
666 err = req->out.h.error;
667 fuse_put_request(fc, req);
669 clear_nlink(entry->d_inode);
670 fuse_invalidate_attr(dir);
671 fuse_invalidate_entry_cache(entry);
672 } else if (err == -EINTR)
673 fuse_invalidate_entry(entry);
677 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
678 struct inode *newdir, struct dentry *newent)
681 struct fuse_rename_in inarg;
682 struct fuse_conn *fc = get_fuse_conn(olddir);
683 struct fuse_req *req = fuse_get_req(fc);
687 memset(&inarg, 0, sizeof(inarg));
688 inarg.newdir = get_node_id(newdir);
689 req->in.h.opcode = FUSE_RENAME;
690 req->in.h.nodeid = get_node_id(olddir);
692 req->in.args[0].size = sizeof(inarg);
693 req->in.args[0].value = &inarg;
694 req->in.args[1].size = oldent->d_name.len + 1;
695 req->in.args[1].value = oldent->d_name.name;
696 req->in.args[2].size = newent->d_name.len + 1;
697 req->in.args[2].value = newent->d_name.name;
698 request_send(fc, req);
699 err = req->out.h.error;
700 fuse_put_request(fc, req);
703 fuse_invalidate_attr(oldent->d_inode);
705 fuse_invalidate_attr(olddir);
706 if (olddir != newdir)
707 fuse_invalidate_attr(newdir);
709 /* newent will end up negative */
711 fuse_invalidate_entry_cache(newent);
712 } else if (err == -EINTR) {
713 /* If request was interrupted, DEITY only knows if the
714 rename actually took place. If the invalidation
715 fails (e.g. some process has CWD under the renamed
716 directory), then there can be inconsistency between
717 the dcache and the real filesystem. Tough luck. */
718 fuse_invalidate_entry(oldent);
720 fuse_invalidate_entry(newent);
726 static int fuse_link(struct dentry *entry, struct inode *newdir,
727 struct dentry *newent)
730 struct fuse_link_in inarg;
731 struct inode *inode = entry->d_inode;
732 struct fuse_conn *fc = get_fuse_conn(inode);
733 struct fuse_req *req = fuse_get_req(fc);
737 memset(&inarg, 0, sizeof(inarg));
738 inarg.oldnodeid = get_node_id(inode);
739 req->in.h.opcode = FUSE_LINK;
741 req->in.args[0].size = sizeof(inarg);
742 req->in.args[0].value = &inarg;
743 req->in.args[1].size = newent->d_name.len + 1;
744 req->in.args[1].value = newent->d_name.name;
745 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
746 /* Contrary to "normal" filesystems it can happen that link
747 makes two "logical" inodes point to the same "physical"
748 inode. We invalidate the attributes of the old one, so it
749 will reflect changes in the backing inode (link count,
752 if (!err || err == -EINTR)
753 fuse_invalidate_attr(inode);
757 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
760 stat->dev = inode->i_sb->s_dev;
761 stat->ino = attr->ino;
762 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
763 stat->nlink = attr->nlink;
764 stat->uid = attr->uid;
765 stat->gid = attr->gid;
766 stat->rdev = inode->i_rdev;
767 stat->atime.tv_sec = attr->atime;
768 stat->atime.tv_nsec = attr->atimensec;
769 stat->mtime.tv_sec = attr->mtime;
770 stat->mtime.tv_nsec = attr->mtimensec;
771 stat->ctime.tv_sec = attr->ctime;
772 stat->ctime.tv_nsec = attr->ctimensec;
773 stat->size = attr->size;
774 stat->blocks = attr->blocks;
775 stat->blksize = (1 << inode->i_blkbits);
778 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
782 struct fuse_getattr_in inarg;
783 struct fuse_attr_out outarg;
784 struct fuse_conn *fc = get_fuse_conn(inode);
785 struct fuse_req *req;
788 req = fuse_get_req(fc);
792 attr_version = fuse_get_attr_version(fc);
794 memset(&inarg, 0, sizeof(inarg));
795 memset(&outarg, 0, sizeof(outarg));
796 /* Directories have separate file-handle space */
797 if (file && S_ISREG(inode->i_mode)) {
798 struct fuse_file *ff = file->private_data;
800 inarg.getattr_flags |= FUSE_GETATTR_FH;
803 req->in.h.opcode = FUSE_GETATTR;
804 req->in.h.nodeid = get_node_id(inode);
806 req->in.args[0].size = sizeof(inarg);
807 req->in.args[0].value = &inarg;
808 req->out.numargs = 1;
810 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
812 req->out.args[0].size = sizeof(outarg);
813 req->out.args[0].value = &outarg;
814 request_send(fc, req);
815 err = req->out.h.error;
816 fuse_put_request(fc, req);
818 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
819 make_bad_inode(inode);
822 fuse_change_attributes(inode, &outarg.attr,
823 attr_timeout(&outarg),
826 fuse_fillattr(inode, &outarg.attr, stat);
832 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
833 struct file *file, bool *refreshed)
835 struct fuse_inode *fi = get_fuse_inode(inode);
839 if (fi->i_time < get_jiffies_64()) {
841 err = fuse_do_getattr(inode, stat, file);
846 generic_fillattr(inode, stat);
847 stat->mode = fi->orig_i_mode;
851 if (refreshed != NULL)
858 * Calling into a user-controlled filesystem gives the filesystem
859 * daemon ptrace-like capabilities over the requester process. This
860 * means, that the filesystem daemon is able to record the exact
861 * filesystem operations performed, and can also control the behavior
862 * of the requester process in otherwise impossible ways. For example
863 * it can delay the operation for arbitrary length of time allowing
864 * DoS against the requester.
866 * For this reason only those processes can call into the filesystem,
867 * for which the owner of the mount has ptrace privilege. This
868 * excludes processes started by other users, suid or sgid processes.
870 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
872 const struct cred *cred;
875 if (fc->flags & FUSE_ALLOW_OTHER)
880 cred = __task_cred(task);
881 if (cred->euid == fc->user_id &&
882 cred->suid == fc->user_id &&
883 cred->uid == fc->user_id &&
884 cred->egid == fc->group_id &&
885 cred->sgid == fc->group_id &&
886 cred->gid == fc->group_id)
893 static int fuse_access(struct inode *inode, int mask)
895 struct fuse_conn *fc = get_fuse_conn(inode);
896 struct fuse_req *req;
897 struct fuse_access_in inarg;
903 req = fuse_get_req(fc);
907 memset(&inarg, 0, sizeof(inarg));
908 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
909 req->in.h.opcode = FUSE_ACCESS;
910 req->in.h.nodeid = get_node_id(inode);
912 req->in.args[0].size = sizeof(inarg);
913 req->in.args[0].value = &inarg;
914 request_send(fc, req);
915 err = req->out.h.error;
916 fuse_put_request(fc, req);
917 if (err == -ENOSYS) {
925 * Check permission. The two basic access models of FUSE are:
927 * 1) Local access checking ('default_permissions' mount option) based
928 * on file mode. This is the plain old disk filesystem permission
931 * 2) "Remote" access checking, where server is responsible for
932 * checking permission in each inode operation. An exception to this
933 * is if ->permission() was invoked from sys_access() in which case an
934 * access request is sent. Execute permission is still checked
935 * locally based on file mode.
937 static int fuse_permission(struct inode *inode, int mask)
939 struct fuse_conn *fc = get_fuse_conn(inode);
940 bool refreshed = false;
943 if (!fuse_allow_task(fc, current))
947 * If attributes are needed, refresh them before proceeding
949 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
950 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
951 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
956 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
957 err = generic_permission(inode, mask, NULL);
959 /* If permission is denied, try to refresh file
960 attributes. This is also needed, because the root
961 node will at first have no permissions */
962 if (err == -EACCES && !refreshed) {
963 err = fuse_do_getattr(inode, NULL, NULL);
965 err = generic_permission(inode, mask, NULL);
968 /* Note: the opposite of the above test does not
969 exist. So if permissions are revoked this won't be
970 noticed immediately, only after the attribute
971 timeout has expired */
972 } else if (mask & MAY_ACCESS) {
973 err = fuse_access(inode, mask);
974 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
975 if (!(inode->i_mode & S_IXUGO)) {
979 err = fuse_do_getattr(inode, NULL, NULL);
980 if (!err && !(inode->i_mode & S_IXUGO))
987 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
988 void *dstbuf, filldir_t filldir)
990 while (nbytes >= FUSE_NAME_OFFSET) {
991 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
992 size_t reclen = FUSE_DIRENT_SIZE(dirent);
994 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
999 over = filldir(dstbuf, dirent->name, dirent->namelen,
1000 file->f_pos, dirent->ino, dirent->type);
1006 file->f_pos = dirent->off;
1012 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1017 struct inode *inode = file->f_path.dentry->d_inode;
1018 struct fuse_conn *fc = get_fuse_conn(inode);
1019 struct fuse_req *req;
1021 if (is_bad_inode(inode))
1024 req = fuse_get_req(fc);
1026 return PTR_ERR(req);
1028 page = alloc_page(GFP_KERNEL);
1030 fuse_put_request(fc, req);
1034 req->pages[0] = page;
1035 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1036 request_send(fc, req);
1037 nbytes = req->out.args[0].size;
1038 err = req->out.h.error;
1039 fuse_put_request(fc, req);
1041 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1045 fuse_invalidate_attr(inode); /* atime changed */
1049 static char *read_link(struct dentry *dentry)
1051 struct inode *inode = dentry->d_inode;
1052 struct fuse_conn *fc = get_fuse_conn(inode);
1053 struct fuse_req *req = fuse_get_req(fc);
1057 return ERR_CAST(req);
1059 link = (char *) __get_free_page(GFP_KERNEL);
1061 link = ERR_PTR(-ENOMEM);
1064 req->in.h.opcode = FUSE_READLINK;
1065 req->in.h.nodeid = get_node_id(inode);
1066 req->out.argvar = 1;
1067 req->out.numargs = 1;
1068 req->out.args[0].size = PAGE_SIZE - 1;
1069 req->out.args[0].value = link;
1070 request_send(fc, req);
1071 if (req->out.h.error) {
1072 free_page((unsigned long) link);
1073 link = ERR_PTR(req->out.h.error);
1075 link[req->out.args[0].size] = '\0';
1077 fuse_put_request(fc, req);
1078 fuse_invalidate_attr(inode); /* atime changed */
1082 static void free_link(char *link)
1085 free_page((unsigned long) link);
1088 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1090 nd_set_link(nd, read_link(dentry));
1094 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1096 free_link(nd_get_link(nd));
1099 static int fuse_dir_open(struct inode *inode, struct file *file)
1101 return fuse_open_common(inode, file, 1);
1104 static int fuse_dir_release(struct inode *inode, struct file *file)
1106 return fuse_release_common(inode, file, 1);
1109 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1111 /* nfsd can call this with no file */
1112 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1115 static bool update_mtime(unsigned ivalid)
1117 /* Always update if mtime is explicitly set */
1118 if (ivalid & ATTR_MTIME_SET)
1121 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1122 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1125 /* In all other cases update */
1129 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1131 unsigned ivalid = iattr->ia_valid;
1133 if (ivalid & ATTR_MODE)
1134 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1135 if (ivalid & ATTR_UID)
1136 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1137 if (ivalid & ATTR_GID)
1138 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1139 if (ivalid & ATTR_SIZE)
1140 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1141 if (ivalid & ATTR_ATIME) {
1142 arg->valid |= FATTR_ATIME;
1143 arg->atime = iattr->ia_atime.tv_sec;
1144 arg->atimensec = iattr->ia_atime.tv_nsec;
1145 if (!(ivalid & ATTR_ATIME_SET))
1146 arg->valid |= FATTR_ATIME_NOW;
1148 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1149 arg->valid |= FATTR_MTIME;
1150 arg->mtime = iattr->ia_mtime.tv_sec;
1151 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1152 if (!(ivalid & ATTR_MTIME_SET))
1153 arg->valid |= FATTR_MTIME_NOW;
1158 * Prevent concurrent writepages on inode
1160 * This is done by adding a negative bias to the inode write counter
1161 * and waiting for all pending writes to finish.
1163 void fuse_set_nowrite(struct inode *inode)
1165 struct fuse_conn *fc = get_fuse_conn(inode);
1166 struct fuse_inode *fi = get_fuse_inode(inode);
1168 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1170 spin_lock(&fc->lock);
1171 BUG_ON(fi->writectr < 0);
1172 fi->writectr += FUSE_NOWRITE;
1173 spin_unlock(&fc->lock);
1174 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1178 * Allow writepages on inode
1180 * Remove the bias from the writecounter and send any queued
1183 static void __fuse_release_nowrite(struct inode *inode)
1185 struct fuse_inode *fi = get_fuse_inode(inode);
1187 BUG_ON(fi->writectr != FUSE_NOWRITE);
1189 fuse_flush_writepages(inode);
1192 void fuse_release_nowrite(struct inode *inode)
1194 struct fuse_conn *fc = get_fuse_conn(inode);
1196 spin_lock(&fc->lock);
1197 __fuse_release_nowrite(inode);
1198 spin_unlock(&fc->lock);
1202 * Set attributes, and at the same time refresh them.
1204 * Truncation is slightly complicated, because the 'truncate' request
1205 * may fail, in which case we don't want to touch the mapping.
1206 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1207 * and the actual truncation by hand.
1209 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1212 struct inode *inode = entry->d_inode;
1213 struct fuse_conn *fc = get_fuse_conn(inode);
1214 struct fuse_req *req;
1215 struct fuse_setattr_in inarg;
1216 struct fuse_attr_out outarg;
1217 bool is_truncate = false;
1221 if (!fuse_allow_task(fc, current))
1224 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1225 err = inode_change_ok(inode, attr);
1230 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1233 if (attr->ia_valid & ATTR_SIZE) {
1234 unsigned long limit;
1235 if (IS_SWAPFILE(inode))
1237 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1238 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1239 send_sig(SIGXFSZ, current, 0);
1245 req = fuse_get_req(fc);
1247 return PTR_ERR(req);
1250 fuse_set_nowrite(inode);
1252 memset(&inarg, 0, sizeof(inarg));
1253 memset(&outarg, 0, sizeof(outarg));
1254 iattr_to_fattr(attr, &inarg);
1256 struct fuse_file *ff = file->private_data;
1257 inarg.valid |= FATTR_FH;
1260 if (attr->ia_valid & ATTR_SIZE) {
1261 /* For mandatory locking in truncate */
1262 inarg.valid |= FATTR_LOCKOWNER;
1263 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1265 req->in.h.opcode = FUSE_SETATTR;
1266 req->in.h.nodeid = get_node_id(inode);
1267 req->in.numargs = 1;
1268 req->in.args[0].size = sizeof(inarg);
1269 req->in.args[0].value = &inarg;
1270 req->out.numargs = 1;
1272 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1274 req->out.args[0].size = sizeof(outarg);
1275 req->out.args[0].value = &outarg;
1276 request_send(fc, req);
1277 err = req->out.h.error;
1278 fuse_put_request(fc, req);
1281 fuse_invalidate_attr(inode);
1285 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1286 make_bad_inode(inode);
1291 spin_lock(&fc->lock);
1292 fuse_change_attributes_common(inode, &outarg.attr,
1293 attr_timeout(&outarg));
1294 oldsize = inode->i_size;
1295 i_size_write(inode, outarg.attr.size);
1298 /* NOTE: this may release/reacquire fc->lock */
1299 __fuse_release_nowrite(inode);
1301 spin_unlock(&fc->lock);
1304 * Only call invalidate_inode_pages2() after removing
1305 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1307 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1308 if (outarg.attr.size < oldsize)
1309 fuse_truncate(inode->i_mapping, outarg.attr.size);
1310 invalidate_inode_pages2(inode->i_mapping);
1317 fuse_release_nowrite(inode);
1322 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1324 if (attr->ia_valid & ATTR_FILE)
1325 return fuse_do_setattr(entry, attr, attr->ia_file);
1327 return fuse_do_setattr(entry, attr, NULL);
1330 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1333 struct inode *inode = entry->d_inode;
1334 struct fuse_conn *fc = get_fuse_conn(inode);
1336 if (!fuse_allow_task(fc, current))
1339 return fuse_update_attributes(inode, stat, NULL, NULL);
1342 static int fuse_setxattr(struct dentry *entry, const char *name,
1343 const void *value, size_t size, int flags)
1345 struct inode *inode = entry->d_inode;
1346 struct fuse_conn *fc = get_fuse_conn(inode);
1347 struct fuse_req *req;
1348 struct fuse_setxattr_in inarg;
1351 if (fc->no_setxattr)
1354 req = fuse_get_req(fc);
1356 return PTR_ERR(req);
1358 memset(&inarg, 0, sizeof(inarg));
1360 inarg.flags = flags;
1361 req->in.h.opcode = FUSE_SETXATTR;
1362 req->in.h.nodeid = get_node_id(inode);
1363 req->in.numargs = 3;
1364 req->in.args[0].size = sizeof(inarg);
1365 req->in.args[0].value = &inarg;
1366 req->in.args[1].size = strlen(name) + 1;
1367 req->in.args[1].value = name;
1368 req->in.args[2].size = size;
1369 req->in.args[2].value = value;
1370 request_send(fc, req);
1371 err = req->out.h.error;
1372 fuse_put_request(fc, req);
1373 if (err == -ENOSYS) {
1374 fc->no_setxattr = 1;
1380 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1381 void *value, size_t size)
1383 struct inode *inode = entry->d_inode;
1384 struct fuse_conn *fc = get_fuse_conn(inode);
1385 struct fuse_req *req;
1386 struct fuse_getxattr_in inarg;
1387 struct fuse_getxattr_out outarg;
1390 if (fc->no_getxattr)
1393 req = fuse_get_req(fc);
1395 return PTR_ERR(req);
1397 memset(&inarg, 0, sizeof(inarg));
1399 req->in.h.opcode = FUSE_GETXATTR;
1400 req->in.h.nodeid = get_node_id(inode);
1401 req->in.numargs = 2;
1402 req->in.args[0].size = sizeof(inarg);
1403 req->in.args[0].value = &inarg;
1404 req->in.args[1].size = strlen(name) + 1;
1405 req->in.args[1].value = name;
1406 /* This is really two different operations rolled into one */
1407 req->out.numargs = 1;
1409 req->out.argvar = 1;
1410 req->out.args[0].size = size;
1411 req->out.args[0].value = value;
1413 req->out.args[0].size = sizeof(outarg);
1414 req->out.args[0].value = &outarg;
1416 request_send(fc, req);
1417 ret = req->out.h.error;
1419 ret = size ? req->out.args[0].size : outarg.size;
1421 if (ret == -ENOSYS) {
1422 fc->no_getxattr = 1;
1426 fuse_put_request(fc, req);
1430 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1432 struct inode *inode = entry->d_inode;
1433 struct fuse_conn *fc = get_fuse_conn(inode);
1434 struct fuse_req *req;
1435 struct fuse_getxattr_in inarg;
1436 struct fuse_getxattr_out outarg;
1439 if (!fuse_allow_task(fc, current))
1442 if (fc->no_listxattr)
1445 req = fuse_get_req(fc);
1447 return PTR_ERR(req);
1449 memset(&inarg, 0, sizeof(inarg));
1451 req->in.h.opcode = FUSE_LISTXATTR;
1452 req->in.h.nodeid = get_node_id(inode);
1453 req->in.numargs = 1;
1454 req->in.args[0].size = sizeof(inarg);
1455 req->in.args[0].value = &inarg;
1456 /* This is really two different operations rolled into one */
1457 req->out.numargs = 1;
1459 req->out.argvar = 1;
1460 req->out.args[0].size = size;
1461 req->out.args[0].value = list;
1463 req->out.args[0].size = sizeof(outarg);
1464 req->out.args[0].value = &outarg;
1466 request_send(fc, req);
1467 ret = req->out.h.error;
1469 ret = size ? req->out.args[0].size : outarg.size;
1471 if (ret == -ENOSYS) {
1472 fc->no_listxattr = 1;
1476 fuse_put_request(fc, req);
1480 static int fuse_removexattr(struct dentry *entry, const char *name)
1482 struct inode *inode = entry->d_inode;
1483 struct fuse_conn *fc = get_fuse_conn(inode);
1484 struct fuse_req *req;
1487 if (fc->no_removexattr)
1490 req = fuse_get_req(fc);
1492 return PTR_ERR(req);
1494 req->in.h.opcode = FUSE_REMOVEXATTR;
1495 req->in.h.nodeid = get_node_id(inode);
1496 req->in.numargs = 1;
1497 req->in.args[0].size = strlen(name) + 1;
1498 req->in.args[0].value = name;
1499 request_send(fc, req);
1500 err = req->out.h.error;
1501 fuse_put_request(fc, req);
1502 if (err == -ENOSYS) {
1503 fc->no_removexattr = 1;
1509 static const struct inode_operations fuse_dir_inode_operations = {
1510 .lookup = fuse_lookup,
1511 .mkdir = fuse_mkdir,
1512 .symlink = fuse_symlink,
1513 .unlink = fuse_unlink,
1514 .rmdir = fuse_rmdir,
1515 .rename = fuse_rename,
1517 .setattr = fuse_setattr,
1518 .create = fuse_create,
1519 .mknod = fuse_mknod,
1520 .permission = fuse_permission,
1521 .getattr = fuse_getattr,
1522 .setxattr = fuse_setxattr,
1523 .getxattr = fuse_getxattr,
1524 .listxattr = fuse_listxattr,
1525 .removexattr = fuse_removexattr,
1528 static const struct file_operations fuse_dir_operations = {
1529 .llseek = generic_file_llseek,
1530 .read = generic_read_dir,
1531 .readdir = fuse_readdir,
1532 .open = fuse_dir_open,
1533 .release = fuse_dir_release,
1534 .fsync = fuse_dir_fsync,
1537 static const struct inode_operations fuse_common_inode_operations = {
1538 .setattr = fuse_setattr,
1539 .permission = fuse_permission,
1540 .getattr = fuse_getattr,
1541 .setxattr = fuse_setxattr,
1542 .getxattr = fuse_getxattr,
1543 .listxattr = fuse_listxattr,
1544 .removexattr = fuse_removexattr,
1547 static const struct inode_operations fuse_symlink_inode_operations = {
1548 .setattr = fuse_setattr,
1549 .follow_link = fuse_follow_link,
1550 .put_link = fuse_put_link,
1551 .readlink = generic_readlink,
1552 .getattr = fuse_getattr,
1553 .setxattr = fuse_setxattr,
1554 .getxattr = fuse_getxattr,
1555 .listxattr = fuse_listxattr,
1556 .removexattr = fuse_removexattr,
1559 void fuse_init_common(struct inode *inode)
1561 inode->i_op = &fuse_common_inode_operations;
1564 void fuse_init_dir(struct inode *inode)
1566 inode->i_op = &fuse_dir_inode_operations;
1567 inode->i_fop = &fuse_dir_operations;
1570 void fuse_init_symlink(struct inode *inode)
1572 inode->i_op = &fuse_symlink_inode_operations;