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 * Atomic create+open operation
367 * If the filesystem doesn't support this, then fall back to separate
368 * 'mknod' + 'open' requests.
370 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
371 struct nameidata *nd)
375 struct fuse_conn *fc = get_fuse_conn(dir);
376 struct fuse_req *req;
377 struct fuse_req *forget_req;
378 struct fuse_open_in inarg;
379 struct fuse_open_out outopen;
380 struct fuse_entry_out outentry;
381 struct fuse_file *ff;
383 int flags = nd->intent.open.flags - 1;
388 forget_req = fuse_get_req(fc);
389 if (IS_ERR(forget_req))
390 return PTR_ERR(forget_req);
392 req = fuse_get_req(fc);
395 goto out_put_forget_req;
398 ff = fuse_file_alloc(fc);
400 goto out_put_request;
403 memset(&inarg, 0, sizeof(inarg));
404 memset(&outentry, 0, sizeof(outentry));
407 req->in.h.opcode = FUSE_CREATE;
408 req->in.h.nodeid = get_node_id(dir);
410 req->in.args[0].size = sizeof(inarg);
411 req->in.args[0].value = &inarg;
412 req->in.args[1].size = entry->d_name.len + 1;
413 req->in.args[1].value = entry->d_name.name;
414 req->out.numargs = 2;
416 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
418 req->out.args[0].size = sizeof(outentry);
419 req->out.args[0].value = &outentry;
420 req->out.args[1].size = sizeof(outopen);
421 req->out.args[1].value = &outopen;
422 fuse_request_send(fc, req);
423 err = req->out.h.error;
431 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
434 fuse_put_request(fc, req);
436 ff->nodeid = outentry.nodeid;
437 ff->open_flags = outopen.open_flags;
438 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
439 &outentry.attr, entry_attr_timeout(&outentry), 0);
441 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
442 fuse_sync_release(ff, flags);
443 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
446 fuse_put_request(fc, forget_req);
447 d_instantiate(entry, inode);
448 fuse_change_entry_timeout(entry, &outentry);
449 fuse_invalidate_attr(dir);
450 file = lookup_instantiate_filp(nd, entry, generic_file_open);
452 fuse_sync_release(ff, flags);
453 return PTR_ERR(file);
455 file->private_data = fuse_file_get(ff);
456 fuse_finish_open(inode, file);
462 fuse_put_request(fc, req);
464 fuse_put_request(fc, forget_req);
469 * Code shared between mknod, mkdir, symlink and link
471 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
472 struct inode *dir, struct dentry *entry,
475 struct fuse_entry_out outarg;
478 struct fuse_req *forget_req;
480 forget_req = fuse_get_req(fc);
481 if (IS_ERR(forget_req)) {
482 fuse_put_request(fc, req);
483 return PTR_ERR(forget_req);
486 memset(&outarg, 0, sizeof(outarg));
487 req->in.h.nodeid = get_node_id(dir);
488 req->out.numargs = 1;
490 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
492 req->out.args[0].size = sizeof(outarg);
493 req->out.args[0].value = &outarg;
494 fuse_request_send(fc, req);
495 err = req->out.h.error;
496 fuse_put_request(fc, req);
498 goto out_put_forget_req;
501 if (invalid_nodeid(outarg.nodeid))
502 goto out_put_forget_req;
504 if ((outarg.attr.mode ^ mode) & S_IFMT)
505 goto out_put_forget_req;
507 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
508 &outarg.attr, entry_attr_timeout(&outarg), 0);
510 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
513 fuse_put_request(fc, forget_req);
515 if (S_ISDIR(inode->i_mode)) {
516 struct dentry *alias;
517 mutex_lock(&fc->inst_mutex);
518 alias = d_find_alias(inode);
520 /* New directory must have moved since mkdir */
521 mutex_unlock(&fc->inst_mutex);
526 d_instantiate(entry, inode);
527 mutex_unlock(&fc->inst_mutex);
529 d_instantiate(entry, inode);
531 fuse_change_entry_timeout(entry, &outarg);
532 fuse_invalidate_attr(dir);
536 fuse_put_request(fc, forget_req);
540 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
543 struct fuse_mknod_in inarg;
544 struct fuse_conn *fc = get_fuse_conn(dir);
545 struct fuse_req *req = fuse_get_req(fc);
549 memset(&inarg, 0, sizeof(inarg));
551 inarg.rdev = new_encode_dev(rdev);
552 req->in.h.opcode = FUSE_MKNOD;
554 req->in.args[0].size = sizeof(inarg);
555 req->in.args[0].value = &inarg;
556 req->in.args[1].size = entry->d_name.len + 1;
557 req->in.args[1].value = entry->d_name.name;
558 return create_new_entry(fc, req, dir, entry, mode);
561 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
562 struct nameidata *nd)
564 if (nd && (nd->flags & LOOKUP_OPEN)) {
565 int err = fuse_create_open(dir, entry, mode, nd);
568 /* Fall back on mknod */
570 return fuse_mknod(dir, entry, mode, 0);
573 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
575 struct fuse_mkdir_in inarg;
576 struct fuse_conn *fc = get_fuse_conn(dir);
577 struct fuse_req *req = fuse_get_req(fc);
581 memset(&inarg, 0, sizeof(inarg));
583 req->in.h.opcode = FUSE_MKDIR;
585 req->in.args[0].size = sizeof(inarg);
586 req->in.args[0].value = &inarg;
587 req->in.args[1].size = entry->d_name.len + 1;
588 req->in.args[1].value = entry->d_name.name;
589 return create_new_entry(fc, req, dir, entry, S_IFDIR);
592 static int fuse_symlink(struct inode *dir, struct dentry *entry,
595 struct fuse_conn *fc = get_fuse_conn(dir);
596 unsigned len = strlen(link) + 1;
597 struct fuse_req *req = fuse_get_req(fc);
601 req->in.h.opcode = FUSE_SYMLINK;
603 req->in.args[0].size = entry->d_name.len + 1;
604 req->in.args[0].value = entry->d_name.name;
605 req->in.args[1].size = len;
606 req->in.args[1].value = link;
607 return create_new_entry(fc, req, dir, entry, S_IFLNK);
610 static int fuse_unlink(struct inode *dir, struct dentry *entry)
613 struct fuse_conn *fc = get_fuse_conn(dir);
614 struct fuse_req *req = fuse_get_req(fc);
618 req->in.h.opcode = FUSE_UNLINK;
619 req->in.h.nodeid = get_node_id(dir);
621 req->in.args[0].size = entry->d_name.len + 1;
622 req->in.args[0].value = entry->d_name.name;
623 fuse_request_send(fc, req);
624 err = req->out.h.error;
625 fuse_put_request(fc, req);
627 struct inode *inode = entry->d_inode;
630 * Set nlink to zero so the inode can be cleared, if the inode
631 * does have more links this will be discovered at the next
635 fuse_invalidate_attr(inode);
636 fuse_invalidate_attr(dir);
637 fuse_invalidate_entry_cache(entry);
638 } else if (err == -EINTR)
639 fuse_invalidate_entry(entry);
643 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
646 struct fuse_conn *fc = get_fuse_conn(dir);
647 struct fuse_req *req = fuse_get_req(fc);
651 req->in.h.opcode = FUSE_RMDIR;
652 req->in.h.nodeid = get_node_id(dir);
654 req->in.args[0].size = entry->d_name.len + 1;
655 req->in.args[0].value = entry->d_name.name;
656 fuse_request_send(fc, req);
657 err = req->out.h.error;
658 fuse_put_request(fc, req);
660 clear_nlink(entry->d_inode);
661 fuse_invalidate_attr(dir);
662 fuse_invalidate_entry_cache(entry);
663 } else if (err == -EINTR)
664 fuse_invalidate_entry(entry);
668 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
669 struct inode *newdir, struct dentry *newent)
672 struct fuse_rename_in inarg;
673 struct fuse_conn *fc = get_fuse_conn(olddir);
674 struct fuse_req *req = fuse_get_req(fc);
678 memset(&inarg, 0, sizeof(inarg));
679 inarg.newdir = get_node_id(newdir);
680 req->in.h.opcode = FUSE_RENAME;
681 req->in.h.nodeid = get_node_id(olddir);
683 req->in.args[0].size = sizeof(inarg);
684 req->in.args[0].value = &inarg;
685 req->in.args[1].size = oldent->d_name.len + 1;
686 req->in.args[1].value = oldent->d_name.name;
687 req->in.args[2].size = newent->d_name.len + 1;
688 req->in.args[2].value = newent->d_name.name;
689 fuse_request_send(fc, req);
690 err = req->out.h.error;
691 fuse_put_request(fc, req);
694 fuse_invalidate_attr(oldent->d_inode);
696 fuse_invalidate_attr(olddir);
697 if (olddir != newdir)
698 fuse_invalidate_attr(newdir);
700 /* newent will end up negative */
702 fuse_invalidate_entry_cache(newent);
703 } else if (err == -EINTR) {
704 /* If request was interrupted, DEITY only knows if the
705 rename actually took place. If the invalidation
706 fails (e.g. some process has CWD under the renamed
707 directory), then there can be inconsistency between
708 the dcache and the real filesystem. Tough luck. */
709 fuse_invalidate_entry(oldent);
711 fuse_invalidate_entry(newent);
717 static int fuse_link(struct dentry *entry, struct inode *newdir,
718 struct dentry *newent)
721 struct fuse_link_in inarg;
722 struct inode *inode = entry->d_inode;
723 struct fuse_conn *fc = get_fuse_conn(inode);
724 struct fuse_req *req = fuse_get_req(fc);
728 memset(&inarg, 0, sizeof(inarg));
729 inarg.oldnodeid = get_node_id(inode);
730 req->in.h.opcode = FUSE_LINK;
732 req->in.args[0].size = sizeof(inarg);
733 req->in.args[0].value = &inarg;
734 req->in.args[1].size = newent->d_name.len + 1;
735 req->in.args[1].value = newent->d_name.name;
736 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
737 /* Contrary to "normal" filesystems it can happen that link
738 makes two "logical" inodes point to the same "physical"
739 inode. We invalidate the attributes of the old one, so it
740 will reflect changes in the backing inode (link count,
743 if (!err || err == -EINTR)
744 fuse_invalidate_attr(inode);
748 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
751 stat->dev = inode->i_sb->s_dev;
752 stat->ino = attr->ino;
753 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
754 stat->nlink = attr->nlink;
755 stat->uid = attr->uid;
756 stat->gid = attr->gid;
757 stat->rdev = inode->i_rdev;
758 stat->atime.tv_sec = attr->atime;
759 stat->atime.tv_nsec = attr->atimensec;
760 stat->mtime.tv_sec = attr->mtime;
761 stat->mtime.tv_nsec = attr->mtimensec;
762 stat->ctime.tv_sec = attr->ctime;
763 stat->ctime.tv_nsec = attr->ctimensec;
764 stat->size = attr->size;
765 stat->blocks = attr->blocks;
766 stat->blksize = (1 << inode->i_blkbits);
769 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
773 struct fuse_getattr_in inarg;
774 struct fuse_attr_out outarg;
775 struct fuse_conn *fc = get_fuse_conn(inode);
776 struct fuse_req *req;
779 req = fuse_get_req(fc);
783 attr_version = fuse_get_attr_version(fc);
785 memset(&inarg, 0, sizeof(inarg));
786 memset(&outarg, 0, sizeof(outarg));
787 /* Directories have separate file-handle space */
788 if (file && S_ISREG(inode->i_mode)) {
789 struct fuse_file *ff = file->private_data;
791 inarg.getattr_flags |= FUSE_GETATTR_FH;
794 req->in.h.opcode = FUSE_GETATTR;
795 req->in.h.nodeid = get_node_id(inode);
797 req->in.args[0].size = sizeof(inarg);
798 req->in.args[0].value = &inarg;
799 req->out.numargs = 1;
801 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
803 req->out.args[0].size = sizeof(outarg);
804 req->out.args[0].value = &outarg;
805 fuse_request_send(fc, req);
806 err = req->out.h.error;
807 fuse_put_request(fc, req);
809 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
810 make_bad_inode(inode);
813 fuse_change_attributes(inode, &outarg.attr,
814 attr_timeout(&outarg),
817 fuse_fillattr(inode, &outarg.attr, stat);
823 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
824 struct file *file, bool *refreshed)
826 struct fuse_inode *fi = get_fuse_inode(inode);
830 if (fi->i_time < get_jiffies_64()) {
832 err = fuse_do_getattr(inode, stat, file);
837 generic_fillattr(inode, stat);
838 stat->mode = fi->orig_i_mode;
842 if (refreshed != NULL)
849 * Calling into a user-controlled filesystem gives the filesystem
850 * daemon ptrace-like capabilities over the requester process. This
851 * means, that the filesystem daemon is able to record the exact
852 * filesystem operations performed, and can also control the behavior
853 * of the requester process in otherwise impossible ways. For example
854 * it can delay the operation for arbitrary length of time allowing
855 * DoS against the requester.
857 * For this reason only those processes can call into the filesystem,
858 * for which the owner of the mount has ptrace privilege. This
859 * excludes processes started by other users, suid or sgid processes.
861 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
863 const struct cred *cred;
866 if (fc->flags & FUSE_ALLOW_OTHER)
871 cred = __task_cred(task);
872 if (cred->euid == fc->user_id &&
873 cred->suid == fc->user_id &&
874 cred->uid == fc->user_id &&
875 cred->egid == fc->group_id &&
876 cred->sgid == fc->group_id &&
877 cred->gid == fc->group_id)
884 static int fuse_access(struct inode *inode, int mask)
886 struct fuse_conn *fc = get_fuse_conn(inode);
887 struct fuse_req *req;
888 struct fuse_access_in inarg;
894 req = fuse_get_req(fc);
898 memset(&inarg, 0, sizeof(inarg));
899 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
900 req->in.h.opcode = FUSE_ACCESS;
901 req->in.h.nodeid = get_node_id(inode);
903 req->in.args[0].size = sizeof(inarg);
904 req->in.args[0].value = &inarg;
905 fuse_request_send(fc, req);
906 err = req->out.h.error;
907 fuse_put_request(fc, req);
908 if (err == -ENOSYS) {
916 * Check permission. The two basic access models of FUSE are:
918 * 1) Local access checking ('default_permissions' mount option) based
919 * on file mode. This is the plain old disk filesystem permission
922 * 2) "Remote" access checking, where server is responsible for
923 * checking permission in each inode operation. An exception to this
924 * is if ->permission() was invoked from sys_access() in which case an
925 * access request is sent. Execute permission is still checked
926 * locally based on file mode.
928 static int fuse_permission(struct inode *inode, int mask)
930 struct fuse_conn *fc = get_fuse_conn(inode);
931 bool refreshed = false;
934 if (!fuse_allow_task(fc, current))
938 * If attributes are needed, refresh them before proceeding
940 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
941 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
942 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
947 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
948 err = generic_permission(inode, mask, NULL);
950 /* If permission is denied, try to refresh file
951 attributes. This is also needed, because the root
952 node will at first have no permissions */
953 if (err == -EACCES && !refreshed) {
954 err = fuse_do_getattr(inode, NULL, NULL);
956 err = generic_permission(inode, mask, NULL);
959 /* Note: the opposite of the above test does not
960 exist. So if permissions are revoked this won't be
961 noticed immediately, only after the attribute
962 timeout has expired */
963 } else if (mask & MAY_ACCESS) {
964 err = fuse_access(inode, mask);
965 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
966 if (!(inode->i_mode & S_IXUGO)) {
970 err = fuse_do_getattr(inode, NULL, NULL);
971 if (!err && !(inode->i_mode & S_IXUGO))
978 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
979 void *dstbuf, filldir_t filldir)
981 while (nbytes >= FUSE_NAME_OFFSET) {
982 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
983 size_t reclen = FUSE_DIRENT_SIZE(dirent);
985 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
990 over = filldir(dstbuf, dirent->name, dirent->namelen,
991 file->f_pos, dirent->ino, dirent->type);
997 file->f_pos = dirent->off;
1003 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1008 struct inode *inode = file->f_path.dentry->d_inode;
1009 struct fuse_conn *fc = get_fuse_conn(inode);
1010 struct fuse_req *req;
1012 if (is_bad_inode(inode))
1015 req = fuse_get_req(fc);
1017 return PTR_ERR(req);
1019 page = alloc_page(GFP_KERNEL);
1021 fuse_put_request(fc, req);
1024 req->out.argpages = 1;
1026 req->pages[0] = page;
1027 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1028 fuse_request_send(fc, req);
1029 nbytes = req->out.args[0].size;
1030 err = req->out.h.error;
1031 fuse_put_request(fc, req);
1033 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1037 fuse_invalidate_attr(inode); /* atime changed */
1041 static char *read_link(struct dentry *dentry)
1043 struct inode *inode = dentry->d_inode;
1044 struct fuse_conn *fc = get_fuse_conn(inode);
1045 struct fuse_req *req = fuse_get_req(fc);
1049 return ERR_CAST(req);
1051 link = (char *) __get_free_page(GFP_KERNEL);
1053 link = ERR_PTR(-ENOMEM);
1056 req->in.h.opcode = FUSE_READLINK;
1057 req->in.h.nodeid = get_node_id(inode);
1058 req->out.argvar = 1;
1059 req->out.numargs = 1;
1060 req->out.args[0].size = PAGE_SIZE - 1;
1061 req->out.args[0].value = link;
1062 fuse_request_send(fc, req);
1063 if (req->out.h.error) {
1064 free_page((unsigned long) link);
1065 link = ERR_PTR(req->out.h.error);
1067 link[req->out.args[0].size] = '\0';
1069 fuse_put_request(fc, req);
1070 fuse_invalidate_attr(inode); /* atime changed */
1074 static void free_link(char *link)
1077 free_page((unsigned long) link);
1080 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1082 nd_set_link(nd, read_link(dentry));
1086 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1088 free_link(nd_get_link(nd));
1091 static int fuse_dir_open(struct inode *inode, struct file *file)
1093 return fuse_open_common(inode, file, true);
1096 static int fuse_dir_release(struct inode *inode, struct file *file)
1098 fuse_release_common(file, FUSE_RELEASEDIR);
1103 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1105 /* nfsd can call this with no file */
1106 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1109 static bool update_mtime(unsigned ivalid)
1111 /* Always update if mtime is explicitly set */
1112 if (ivalid & ATTR_MTIME_SET)
1115 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1116 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1119 /* In all other cases update */
1123 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1125 unsigned ivalid = iattr->ia_valid;
1127 if (ivalid & ATTR_MODE)
1128 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1129 if (ivalid & ATTR_UID)
1130 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1131 if (ivalid & ATTR_GID)
1132 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1133 if (ivalid & ATTR_SIZE)
1134 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1135 if (ivalid & ATTR_ATIME) {
1136 arg->valid |= FATTR_ATIME;
1137 arg->atime = iattr->ia_atime.tv_sec;
1138 arg->atimensec = iattr->ia_atime.tv_nsec;
1139 if (!(ivalid & ATTR_ATIME_SET))
1140 arg->valid |= FATTR_ATIME_NOW;
1142 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1143 arg->valid |= FATTR_MTIME;
1144 arg->mtime = iattr->ia_mtime.tv_sec;
1145 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1146 if (!(ivalid & ATTR_MTIME_SET))
1147 arg->valid |= FATTR_MTIME_NOW;
1152 * Prevent concurrent writepages on inode
1154 * This is done by adding a negative bias to the inode write counter
1155 * and waiting for all pending writes to finish.
1157 void fuse_set_nowrite(struct inode *inode)
1159 struct fuse_conn *fc = get_fuse_conn(inode);
1160 struct fuse_inode *fi = get_fuse_inode(inode);
1162 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1164 spin_lock(&fc->lock);
1165 BUG_ON(fi->writectr < 0);
1166 fi->writectr += FUSE_NOWRITE;
1167 spin_unlock(&fc->lock);
1168 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1172 * Allow writepages on inode
1174 * Remove the bias from the writecounter and send any queued
1177 static void __fuse_release_nowrite(struct inode *inode)
1179 struct fuse_inode *fi = get_fuse_inode(inode);
1181 BUG_ON(fi->writectr != FUSE_NOWRITE);
1183 fuse_flush_writepages(inode);
1186 void fuse_release_nowrite(struct inode *inode)
1188 struct fuse_conn *fc = get_fuse_conn(inode);
1190 spin_lock(&fc->lock);
1191 __fuse_release_nowrite(inode);
1192 spin_unlock(&fc->lock);
1196 * Set attributes, and at the same time refresh them.
1198 * Truncation is slightly complicated, because the 'truncate' request
1199 * may fail, in which case we don't want to touch the mapping.
1200 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1201 * and the actual truncation by hand.
1203 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1206 struct inode *inode = entry->d_inode;
1207 struct fuse_conn *fc = get_fuse_conn(inode);
1208 struct fuse_req *req;
1209 struct fuse_setattr_in inarg;
1210 struct fuse_attr_out outarg;
1211 bool is_truncate = false;
1215 if (!fuse_allow_task(fc, current))
1218 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1219 err = inode_change_ok(inode, attr);
1224 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1227 if (attr->ia_valid & ATTR_SIZE) {
1228 unsigned long limit;
1229 if (IS_SWAPFILE(inode))
1231 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1232 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1233 send_sig(SIGXFSZ, current, 0);
1239 req = fuse_get_req(fc);
1241 return PTR_ERR(req);
1244 fuse_set_nowrite(inode);
1246 memset(&inarg, 0, sizeof(inarg));
1247 memset(&outarg, 0, sizeof(outarg));
1248 iattr_to_fattr(attr, &inarg);
1250 struct fuse_file *ff = file->private_data;
1251 inarg.valid |= FATTR_FH;
1254 if (attr->ia_valid & ATTR_SIZE) {
1255 /* For mandatory locking in truncate */
1256 inarg.valid |= FATTR_LOCKOWNER;
1257 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1259 req->in.h.opcode = FUSE_SETATTR;
1260 req->in.h.nodeid = get_node_id(inode);
1261 req->in.numargs = 1;
1262 req->in.args[0].size = sizeof(inarg);
1263 req->in.args[0].value = &inarg;
1264 req->out.numargs = 1;
1266 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1268 req->out.args[0].size = sizeof(outarg);
1269 req->out.args[0].value = &outarg;
1270 fuse_request_send(fc, req);
1271 err = req->out.h.error;
1272 fuse_put_request(fc, req);
1275 fuse_invalidate_attr(inode);
1279 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1280 make_bad_inode(inode);
1285 spin_lock(&fc->lock);
1286 fuse_change_attributes_common(inode, &outarg.attr,
1287 attr_timeout(&outarg));
1288 oldsize = inode->i_size;
1289 i_size_write(inode, outarg.attr.size);
1292 /* NOTE: this may release/reacquire fc->lock */
1293 __fuse_release_nowrite(inode);
1295 spin_unlock(&fc->lock);
1298 * Only call invalidate_inode_pages2() after removing
1299 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1301 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1302 if (outarg.attr.size < oldsize)
1303 fuse_truncate(inode->i_mapping, outarg.attr.size);
1304 invalidate_inode_pages2(inode->i_mapping);
1311 fuse_release_nowrite(inode);
1316 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1318 if (attr->ia_valid & ATTR_FILE)
1319 return fuse_do_setattr(entry, attr, attr->ia_file);
1321 return fuse_do_setattr(entry, attr, NULL);
1324 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1327 struct inode *inode = entry->d_inode;
1328 struct fuse_conn *fc = get_fuse_conn(inode);
1330 if (!fuse_allow_task(fc, current))
1333 return fuse_update_attributes(inode, stat, NULL, NULL);
1336 static int fuse_setxattr(struct dentry *entry, const char *name,
1337 const void *value, size_t size, int flags)
1339 struct inode *inode = entry->d_inode;
1340 struct fuse_conn *fc = get_fuse_conn(inode);
1341 struct fuse_req *req;
1342 struct fuse_setxattr_in inarg;
1345 if (fc->no_setxattr)
1348 req = fuse_get_req(fc);
1350 return PTR_ERR(req);
1352 memset(&inarg, 0, sizeof(inarg));
1354 inarg.flags = flags;
1355 req->in.h.opcode = FUSE_SETXATTR;
1356 req->in.h.nodeid = get_node_id(inode);
1357 req->in.numargs = 3;
1358 req->in.args[0].size = sizeof(inarg);
1359 req->in.args[0].value = &inarg;
1360 req->in.args[1].size = strlen(name) + 1;
1361 req->in.args[1].value = name;
1362 req->in.args[2].size = size;
1363 req->in.args[2].value = value;
1364 fuse_request_send(fc, req);
1365 err = req->out.h.error;
1366 fuse_put_request(fc, req);
1367 if (err == -ENOSYS) {
1368 fc->no_setxattr = 1;
1374 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1375 void *value, size_t size)
1377 struct inode *inode = entry->d_inode;
1378 struct fuse_conn *fc = get_fuse_conn(inode);
1379 struct fuse_req *req;
1380 struct fuse_getxattr_in inarg;
1381 struct fuse_getxattr_out outarg;
1384 if (fc->no_getxattr)
1387 req = fuse_get_req(fc);
1389 return PTR_ERR(req);
1391 memset(&inarg, 0, sizeof(inarg));
1393 req->in.h.opcode = FUSE_GETXATTR;
1394 req->in.h.nodeid = get_node_id(inode);
1395 req->in.numargs = 2;
1396 req->in.args[0].size = sizeof(inarg);
1397 req->in.args[0].value = &inarg;
1398 req->in.args[1].size = strlen(name) + 1;
1399 req->in.args[1].value = name;
1400 /* This is really two different operations rolled into one */
1401 req->out.numargs = 1;
1403 req->out.argvar = 1;
1404 req->out.args[0].size = size;
1405 req->out.args[0].value = value;
1407 req->out.args[0].size = sizeof(outarg);
1408 req->out.args[0].value = &outarg;
1410 fuse_request_send(fc, req);
1411 ret = req->out.h.error;
1413 ret = size ? req->out.args[0].size : outarg.size;
1415 if (ret == -ENOSYS) {
1416 fc->no_getxattr = 1;
1420 fuse_put_request(fc, req);
1424 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1426 struct inode *inode = entry->d_inode;
1427 struct fuse_conn *fc = get_fuse_conn(inode);
1428 struct fuse_req *req;
1429 struct fuse_getxattr_in inarg;
1430 struct fuse_getxattr_out outarg;
1433 if (!fuse_allow_task(fc, current))
1436 if (fc->no_listxattr)
1439 req = fuse_get_req(fc);
1441 return PTR_ERR(req);
1443 memset(&inarg, 0, sizeof(inarg));
1445 req->in.h.opcode = FUSE_LISTXATTR;
1446 req->in.h.nodeid = get_node_id(inode);
1447 req->in.numargs = 1;
1448 req->in.args[0].size = sizeof(inarg);
1449 req->in.args[0].value = &inarg;
1450 /* This is really two different operations rolled into one */
1451 req->out.numargs = 1;
1453 req->out.argvar = 1;
1454 req->out.args[0].size = size;
1455 req->out.args[0].value = list;
1457 req->out.args[0].size = sizeof(outarg);
1458 req->out.args[0].value = &outarg;
1460 fuse_request_send(fc, req);
1461 ret = req->out.h.error;
1463 ret = size ? req->out.args[0].size : outarg.size;
1465 if (ret == -ENOSYS) {
1466 fc->no_listxattr = 1;
1470 fuse_put_request(fc, req);
1474 static int fuse_removexattr(struct dentry *entry, const char *name)
1476 struct inode *inode = entry->d_inode;
1477 struct fuse_conn *fc = get_fuse_conn(inode);
1478 struct fuse_req *req;
1481 if (fc->no_removexattr)
1484 req = fuse_get_req(fc);
1486 return PTR_ERR(req);
1488 req->in.h.opcode = FUSE_REMOVEXATTR;
1489 req->in.h.nodeid = get_node_id(inode);
1490 req->in.numargs = 1;
1491 req->in.args[0].size = strlen(name) + 1;
1492 req->in.args[0].value = name;
1493 fuse_request_send(fc, req);
1494 err = req->out.h.error;
1495 fuse_put_request(fc, req);
1496 if (err == -ENOSYS) {
1497 fc->no_removexattr = 1;
1503 static const struct inode_operations fuse_dir_inode_operations = {
1504 .lookup = fuse_lookup,
1505 .mkdir = fuse_mkdir,
1506 .symlink = fuse_symlink,
1507 .unlink = fuse_unlink,
1508 .rmdir = fuse_rmdir,
1509 .rename = fuse_rename,
1511 .setattr = fuse_setattr,
1512 .create = fuse_create,
1513 .mknod = fuse_mknod,
1514 .permission = fuse_permission,
1515 .getattr = fuse_getattr,
1516 .setxattr = fuse_setxattr,
1517 .getxattr = fuse_getxattr,
1518 .listxattr = fuse_listxattr,
1519 .removexattr = fuse_removexattr,
1522 static const struct file_operations fuse_dir_operations = {
1523 .llseek = generic_file_llseek,
1524 .read = generic_read_dir,
1525 .readdir = fuse_readdir,
1526 .open = fuse_dir_open,
1527 .release = fuse_dir_release,
1528 .fsync = fuse_dir_fsync,
1531 static const struct inode_operations fuse_common_inode_operations = {
1532 .setattr = fuse_setattr,
1533 .permission = fuse_permission,
1534 .getattr = fuse_getattr,
1535 .setxattr = fuse_setxattr,
1536 .getxattr = fuse_getxattr,
1537 .listxattr = fuse_listxattr,
1538 .removexattr = fuse_removexattr,
1541 static const struct inode_operations fuse_symlink_inode_operations = {
1542 .setattr = fuse_setattr,
1543 .follow_link = fuse_follow_link,
1544 .put_link = fuse_put_link,
1545 .readlink = generic_readlink,
1546 .getattr = fuse_getattr,
1547 .setxattr = fuse_setxattr,
1548 .getxattr = fuse_getxattr,
1549 .listxattr = fuse_listxattr,
1550 .removexattr = fuse_removexattr,
1553 void fuse_init_common(struct inode *inode)
1555 inode->i_op = &fuse_common_inode_operations;
1558 void fuse_init_dir(struct inode *inode)
1560 inode->i_op = &fuse_dir_inode_operations;
1561 inode->i_fop = &fuse_dir_operations;
1564 void fuse_init_symlink(struct inode *inode)
1566 inode->i_op = &fuse_symlink_inode_operations;