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_create_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 if (flags & O_DIRECT)
391 forget_req = fuse_get_req(fc);
392 if (IS_ERR(forget_req))
393 return PTR_ERR(forget_req);
395 req = fuse_get_req(fc);
398 goto out_put_forget_req;
401 ff = fuse_file_alloc(fc);
403 goto out_put_request;
406 mode &= ~current_umask();
409 memset(&inarg, 0, sizeof(inarg));
410 memset(&outentry, 0, sizeof(outentry));
413 inarg.umask = current_umask();
414 req->in.h.opcode = FUSE_CREATE;
415 req->in.h.nodeid = get_node_id(dir);
417 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
419 req->in.args[0].value = &inarg;
420 req->in.args[1].size = entry->d_name.len + 1;
421 req->in.args[1].value = entry->d_name.name;
422 req->out.numargs = 2;
424 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
426 req->out.args[0].size = sizeof(outentry);
427 req->out.args[0].value = &outentry;
428 req->out.args[1].size = sizeof(outopen);
429 req->out.args[1].value = &outopen;
430 fuse_request_send(fc, req);
431 err = req->out.h.error;
439 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
442 fuse_put_request(fc, req);
444 ff->nodeid = outentry.nodeid;
445 ff->open_flags = outopen.open_flags;
446 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
447 &outentry.attr, entry_attr_timeout(&outentry), 0);
449 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
450 fuse_sync_release(ff, flags);
451 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
454 fuse_put_request(fc, forget_req);
455 d_instantiate(entry, inode);
456 fuse_change_entry_timeout(entry, &outentry);
457 fuse_invalidate_attr(dir);
458 file = lookup_instantiate_filp(nd, entry, generic_file_open);
460 fuse_sync_release(ff, flags);
461 return PTR_ERR(file);
463 file->private_data = fuse_file_get(ff);
464 fuse_finish_open(inode, file);
470 fuse_put_request(fc, req);
472 fuse_put_request(fc, forget_req);
477 * Code shared between mknod, mkdir, symlink and link
479 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
480 struct inode *dir, struct dentry *entry,
483 struct fuse_entry_out outarg;
486 struct fuse_req *forget_req;
488 forget_req = fuse_get_req(fc);
489 if (IS_ERR(forget_req)) {
490 fuse_put_request(fc, req);
491 return PTR_ERR(forget_req);
494 memset(&outarg, 0, sizeof(outarg));
495 req->in.h.nodeid = get_node_id(dir);
496 req->out.numargs = 1;
498 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
500 req->out.args[0].size = sizeof(outarg);
501 req->out.args[0].value = &outarg;
502 fuse_request_send(fc, req);
503 err = req->out.h.error;
504 fuse_put_request(fc, req);
506 goto out_put_forget_req;
509 if (invalid_nodeid(outarg.nodeid))
510 goto out_put_forget_req;
512 if ((outarg.attr.mode ^ mode) & S_IFMT)
513 goto out_put_forget_req;
515 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
516 &outarg.attr, entry_attr_timeout(&outarg), 0);
518 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
521 fuse_put_request(fc, forget_req);
523 if (S_ISDIR(inode->i_mode)) {
524 struct dentry *alias;
525 mutex_lock(&fc->inst_mutex);
526 alias = d_find_alias(inode);
528 /* New directory must have moved since mkdir */
529 mutex_unlock(&fc->inst_mutex);
534 d_instantiate(entry, inode);
535 mutex_unlock(&fc->inst_mutex);
537 d_instantiate(entry, inode);
539 fuse_change_entry_timeout(entry, &outarg);
540 fuse_invalidate_attr(dir);
544 fuse_put_request(fc, forget_req);
548 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
551 struct fuse_mknod_in inarg;
552 struct fuse_conn *fc = get_fuse_conn(dir);
553 struct fuse_req *req = fuse_get_req(fc);
558 mode &= ~current_umask();
560 memset(&inarg, 0, sizeof(inarg));
562 inarg.rdev = new_encode_dev(rdev);
563 inarg.umask = current_umask();
564 req->in.h.opcode = FUSE_MKNOD;
566 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
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);
595 mode &= ~current_umask();
597 memset(&inarg, 0, sizeof(inarg));
599 inarg.umask = current_umask();
600 req->in.h.opcode = FUSE_MKDIR;
602 req->in.args[0].size = sizeof(inarg);
603 req->in.args[0].value = &inarg;
604 req->in.args[1].size = entry->d_name.len + 1;
605 req->in.args[1].value = entry->d_name.name;
606 return create_new_entry(fc, req, dir, entry, S_IFDIR);
609 static int fuse_symlink(struct inode *dir, struct dentry *entry,
612 struct fuse_conn *fc = get_fuse_conn(dir);
613 unsigned len = strlen(link) + 1;
614 struct fuse_req *req = fuse_get_req(fc);
618 req->in.h.opcode = FUSE_SYMLINK;
620 req->in.args[0].size = entry->d_name.len + 1;
621 req->in.args[0].value = entry->d_name.name;
622 req->in.args[1].size = len;
623 req->in.args[1].value = link;
624 return create_new_entry(fc, req, dir, entry, S_IFLNK);
627 static int fuse_unlink(struct inode *dir, struct dentry *entry)
630 struct fuse_conn *fc = get_fuse_conn(dir);
631 struct fuse_req *req = fuse_get_req(fc);
635 req->in.h.opcode = FUSE_UNLINK;
636 req->in.h.nodeid = get_node_id(dir);
638 req->in.args[0].size = entry->d_name.len + 1;
639 req->in.args[0].value = entry->d_name.name;
640 fuse_request_send(fc, req);
641 err = req->out.h.error;
642 fuse_put_request(fc, req);
644 struct inode *inode = entry->d_inode;
647 * Set nlink to zero so the inode can be cleared, if the inode
648 * does have more links this will be discovered at the next
652 fuse_invalidate_attr(inode);
653 fuse_invalidate_attr(dir);
654 fuse_invalidate_entry_cache(entry);
655 } else if (err == -EINTR)
656 fuse_invalidate_entry(entry);
660 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
663 struct fuse_conn *fc = get_fuse_conn(dir);
664 struct fuse_req *req = fuse_get_req(fc);
668 req->in.h.opcode = FUSE_RMDIR;
669 req->in.h.nodeid = get_node_id(dir);
671 req->in.args[0].size = entry->d_name.len + 1;
672 req->in.args[0].value = entry->d_name.name;
673 fuse_request_send(fc, req);
674 err = req->out.h.error;
675 fuse_put_request(fc, req);
677 clear_nlink(entry->d_inode);
678 fuse_invalidate_attr(dir);
679 fuse_invalidate_entry_cache(entry);
680 } else if (err == -EINTR)
681 fuse_invalidate_entry(entry);
685 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
686 struct inode *newdir, struct dentry *newent)
689 struct fuse_rename_in inarg;
690 struct fuse_conn *fc = get_fuse_conn(olddir);
691 struct fuse_req *req = fuse_get_req(fc);
695 memset(&inarg, 0, sizeof(inarg));
696 inarg.newdir = get_node_id(newdir);
697 req->in.h.opcode = FUSE_RENAME;
698 req->in.h.nodeid = get_node_id(olddir);
700 req->in.args[0].size = sizeof(inarg);
701 req->in.args[0].value = &inarg;
702 req->in.args[1].size = oldent->d_name.len + 1;
703 req->in.args[1].value = oldent->d_name.name;
704 req->in.args[2].size = newent->d_name.len + 1;
705 req->in.args[2].value = newent->d_name.name;
706 fuse_request_send(fc, req);
707 err = req->out.h.error;
708 fuse_put_request(fc, req);
711 fuse_invalidate_attr(oldent->d_inode);
713 fuse_invalidate_attr(olddir);
714 if (olddir != newdir)
715 fuse_invalidate_attr(newdir);
717 /* newent will end up negative */
718 if (newent->d_inode) {
719 fuse_invalidate_attr(newent->d_inode);
720 fuse_invalidate_entry_cache(newent);
722 } else if (err == -EINTR) {
723 /* If request was interrupted, DEITY only knows if the
724 rename actually took place. If the invalidation
725 fails (e.g. some process has CWD under the renamed
726 directory), then there can be inconsistency between
727 the dcache and the real filesystem. Tough luck. */
728 fuse_invalidate_entry(oldent);
730 fuse_invalidate_entry(newent);
736 static int fuse_link(struct dentry *entry, struct inode *newdir,
737 struct dentry *newent)
740 struct fuse_link_in inarg;
741 struct inode *inode = entry->d_inode;
742 struct fuse_conn *fc = get_fuse_conn(inode);
743 struct fuse_req *req = fuse_get_req(fc);
747 memset(&inarg, 0, sizeof(inarg));
748 inarg.oldnodeid = get_node_id(inode);
749 req->in.h.opcode = FUSE_LINK;
751 req->in.args[0].size = sizeof(inarg);
752 req->in.args[0].value = &inarg;
753 req->in.args[1].size = newent->d_name.len + 1;
754 req->in.args[1].value = newent->d_name.name;
755 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
756 /* Contrary to "normal" filesystems it can happen that link
757 makes two "logical" inodes point to the same "physical"
758 inode. We invalidate the attributes of the old one, so it
759 will reflect changes in the backing inode (link count,
762 if (!err || err == -EINTR)
763 fuse_invalidate_attr(inode);
767 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
770 stat->dev = inode->i_sb->s_dev;
771 stat->ino = attr->ino;
772 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
773 stat->nlink = attr->nlink;
774 stat->uid = attr->uid;
775 stat->gid = attr->gid;
776 stat->rdev = inode->i_rdev;
777 stat->atime.tv_sec = attr->atime;
778 stat->atime.tv_nsec = attr->atimensec;
779 stat->mtime.tv_sec = attr->mtime;
780 stat->mtime.tv_nsec = attr->mtimensec;
781 stat->ctime.tv_sec = attr->ctime;
782 stat->ctime.tv_nsec = attr->ctimensec;
783 stat->size = attr->size;
784 stat->blocks = attr->blocks;
785 stat->blksize = (1 << inode->i_blkbits);
788 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
792 struct fuse_getattr_in inarg;
793 struct fuse_attr_out outarg;
794 struct fuse_conn *fc = get_fuse_conn(inode);
795 struct fuse_req *req;
798 req = fuse_get_req(fc);
802 attr_version = fuse_get_attr_version(fc);
804 memset(&inarg, 0, sizeof(inarg));
805 memset(&outarg, 0, sizeof(outarg));
806 /* Directories have separate file-handle space */
807 if (file && S_ISREG(inode->i_mode)) {
808 struct fuse_file *ff = file->private_data;
810 inarg.getattr_flags |= FUSE_GETATTR_FH;
813 req->in.h.opcode = FUSE_GETATTR;
814 req->in.h.nodeid = get_node_id(inode);
816 req->in.args[0].size = sizeof(inarg);
817 req->in.args[0].value = &inarg;
818 req->out.numargs = 1;
820 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
822 req->out.args[0].size = sizeof(outarg);
823 req->out.args[0].value = &outarg;
824 fuse_request_send(fc, req);
825 err = req->out.h.error;
826 fuse_put_request(fc, req);
828 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
829 make_bad_inode(inode);
832 fuse_change_attributes(inode, &outarg.attr,
833 attr_timeout(&outarg),
836 fuse_fillattr(inode, &outarg.attr, stat);
842 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
843 struct file *file, bool *refreshed)
845 struct fuse_inode *fi = get_fuse_inode(inode);
849 if (fi->i_time < get_jiffies_64()) {
851 err = fuse_do_getattr(inode, stat, file);
856 generic_fillattr(inode, stat);
857 stat->mode = fi->orig_i_mode;
861 if (refreshed != NULL)
867 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
871 struct inode *parent;
873 struct dentry *entry;
875 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
879 mutex_lock(&parent->i_mutex);
880 if (!S_ISDIR(parent->i_mode))
884 dir = d_find_alias(parent);
888 entry = d_lookup(dir, name);
893 fuse_invalidate_attr(parent);
894 fuse_invalidate_entry(entry);
899 mutex_unlock(&parent->i_mutex);
905 * Calling into a user-controlled filesystem gives the filesystem
906 * daemon ptrace-like capabilities over the requester process. This
907 * means, that the filesystem daemon is able to record the exact
908 * filesystem operations performed, and can also control the behavior
909 * of the requester process in otherwise impossible ways. For example
910 * it can delay the operation for arbitrary length of time allowing
911 * DoS against the requester.
913 * For this reason only those processes can call into the filesystem,
914 * for which the owner of the mount has ptrace privilege. This
915 * excludes processes started by other users, suid or sgid processes.
917 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
919 const struct cred *cred;
922 if (fc->flags & FUSE_ALLOW_OTHER)
927 cred = __task_cred(task);
928 if (cred->euid == fc->user_id &&
929 cred->suid == fc->user_id &&
930 cred->uid == fc->user_id &&
931 cred->egid == fc->group_id &&
932 cred->sgid == fc->group_id &&
933 cred->gid == fc->group_id)
940 static int fuse_access(struct inode *inode, int mask)
942 struct fuse_conn *fc = get_fuse_conn(inode);
943 struct fuse_req *req;
944 struct fuse_access_in inarg;
950 req = fuse_get_req(fc);
954 memset(&inarg, 0, sizeof(inarg));
955 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
956 req->in.h.opcode = FUSE_ACCESS;
957 req->in.h.nodeid = get_node_id(inode);
959 req->in.args[0].size = sizeof(inarg);
960 req->in.args[0].value = &inarg;
961 fuse_request_send(fc, req);
962 err = req->out.h.error;
963 fuse_put_request(fc, req);
964 if (err == -ENOSYS) {
972 * Check permission. The two basic access models of FUSE are:
974 * 1) Local access checking ('default_permissions' mount option) based
975 * on file mode. This is the plain old disk filesystem permission
978 * 2) "Remote" access checking, where server is responsible for
979 * checking permission in each inode operation. An exception to this
980 * is if ->permission() was invoked from sys_access() in which case an
981 * access request is sent. Execute permission is still checked
982 * locally based on file mode.
984 static int fuse_permission(struct inode *inode, int mask)
986 struct fuse_conn *fc = get_fuse_conn(inode);
987 bool refreshed = false;
990 if (!fuse_allow_task(fc, current))
994 * If attributes are needed, refresh them before proceeding
996 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
997 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
998 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
1003 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1004 err = generic_permission(inode, mask, NULL);
1006 /* If permission is denied, try to refresh file
1007 attributes. This is also needed, because the root
1008 node will at first have no permissions */
1009 if (err == -EACCES && !refreshed) {
1010 err = fuse_do_getattr(inode, NULL, NULL);
1012 err = generic_permission(inode, mask, NULL);
1015 /* Note: the opposite of the above test does not
1016 exist. So if permissions are revoked this won't be
1017 noticed immediately, only after the attribute
1018 timeout has expired */
1019 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1020 err = fuse_access(inode, mask);
1021 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1022 if (!(inode->i_mode & S_IXUGO)) {
1026 err = fuse_do_getattr(inode, NULL, NULL);
1027 if (!err && !(inode->i_mode & S_IXUGO))
1034 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1035 void *dstbuf, filldir_t filldir)
1037 while (nbytes >= FUSE_NAME_OFFSET) {
1038 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1039 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1041 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1043 if (reclen > nbytes)
1046 over = filldir(dstbuf, dirent->name, dirent->namelen,
1047 file->f_pos, dirent->ino, dirent->type);
1053 file->f_pos = dirent->off;
1059 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1064 struct inode *inode = file->f_path.dentry->d_inode;
1065 struct fuse_conn *fc = get_fuse_conn(inode);
1066 struct fuse_req *req;
1068 if (is_bad_inode(inode))
1071 req = fuse_get_req(fc);
1073 return PTR_ERR(req);
1075 page = alloc_page(GFP_KERNEL);
1077 fuse_put_request(fc, req);
1080 req->out.argpages = 1;
1082 req->pages[0] = page;
1083 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1084 fuse_request_send(fc, req);
1085 nbytes = req->out.args[0].size;
1086 err = req->out.h.error;
1087 fuse_put_request(fc, req);
1089 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1093 fuse_invalidate_attr(inode); /* atime changed */
1097 static char *read_link(struct dentry *dentry)
1099 struct inode *inode = dentry->d_inode;
1100 struct fuse_conn *fc = get_fuse_conn(inode);
1101 struct fuse_req *req = fuse_get_req(fc);
1105 return ERR_CAST(req);
1107 link = (char *) __get_free_page(GFP_KERNEL);
1109 link = ERR_PTR(-ENOMEM);
1112 req->in.h.opcode = FUSE_READLINK;
1113 req->in.h.nodeid = get_node_id(inode);
1114 req->out.argvar = 1;
1115 req->out.numargs = 1;
1116 req->out.args[0].size = PAGE_SIZE - 1;
1117 req->out.args[0].value = link;
1118 fuse_request_send(fc, req);
1119 if (req->out.h.error) {
1120 free_page((unsigned long) link);
1121 link = ERR_PTR(req->out.h.error);
1123 link[req->out.args[0].size] = '\0';
1125 fuse_put_request(fc, req);
1126 fuse_invalidate_attr(inode); /* atime changed */
1130 static void free_link(char *link)
1133 free_page((unsigned long) link);
1136 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1138 nd_set_link(nd, read_link(dentry));
1142 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1144 free_link(nd_get_link(nd));
1147 static int fuse_dir_open(struct inode *inode, struct file *file)
1149 return fuse_open_common(inode, file, true);
1152 static int fuse_dir_release(struct inode *inode, struct file *file)
1154 fuse_release_common(file, FUSE_RELEASEDIR);
1159 static int fuse_dir_fsync(struct file *file, int datasync)
1161 return fuse_fsync_common(file, datasync, 1);
1164 static bool update_mtime(unsigned ivalid)
1166 /* Always update if mtime is explicitly set */
1167 if (ivalid & ATTR_MTIME_SET)
1170 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1171 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1174 /* In all other cases update */
1178 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1180 unsigned ivalid = iattr->ia_valid;
1182 if (ivalid & ATTR_MODE)
1183 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1184 if (ivalid & ATTR_UID)
1185 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1186 if (ivalid & ATTR_GID)
1187 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1188 if (ivalid & ATTR_SIZE)
1189 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1190 if (ivalid & ATTR_ATIME) {
1191 arg->valid |= FATTR_ATIME;
1192 arg->atime = iattr->ia_atime.tv_sec;
1193 arg->atimensec = iattr->ia_atime.tv_nsec;
1194 if (!(ivalid & ATTR_ATIME_SET))
1195 arg->valid |= FATTR_ATIME_NOW;
1197 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1198 arg->valid |= FATTR_MTIME;
1199 arg->mtime = iattr->ia_mtime.tv_sec;
1200 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1201 if (!(ivalid & ATTR_MTIME_SET))
1202 arg->valid |= FATTR_MTIME_NOW;
1207 * Prevent concurrent writepages on inode
1209 * This is done by adding a negative bias to the inode write counter
1210 * and waiting for all pending writes to finish.
1212 void fuse_set_nowrite(struct inode *inode)
1214 struct fuse_conn *fc = get_fuse_conn(inode);
1215 struct fuse_inode *fi = get_fuse_inode(inode);
1217 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1219 spin_lock(&fc->lock);
1220 BUG_ON(fi->writectr < 0);
1221 fi->writectr += FUSE_NOWRITE;
1222 spin_unlock(&fc->lock);
1223 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1227 * Allow writepages on inode
1229 * Remove the bias from the writecounter and send any queued
1232 static void __fuse_release_nowrite(struct inode *inode)
1234 struct fuse_inode *fi = get_fuse_inode(inode);
1236 BUG_ON(fi->writectr != FUSE_NOWRITE);
1238 fuse_flush_writepages(inode);
1241 void fuse_release_nowrite(struct inode *inode)
1243 struct fuse_conn *fc = get_fuse_conn(inode);
1245 spin_lock(&fc->lock);
1246 __fuse_release_nowrite(inode);
1247 spin_unlock(&fc->lock);
1251 * Set attributes, and at the same time refresh them.
1253 * Truncation is slightly complicated, because the 'truncate' request
1254 * may fail, in which case we don't want to touch the mapping.
1255 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1256 * and the actual truncation by hand.
1258 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1261 struct inode *inode = entry->d_inode;
1262 struct fuse_conn *fc = get_fuse_conn(inode);
1263 struct fuse_req *req;
1264 struct fuse_setattr_in inarg;
1265 struct fuse_attr_out outarg;
1266 bool is_truncate = false;
1270 if (!fuse_allow_task(fc, current))
1273 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1274 err = inode_change_ok(inode, attr);
1279 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1282 if (attr->ia_valid & ATTR_SIZE) {
1283 err = inode_newsize_ok(inode, attr->ia_size);
1289 req = fuse_get_req(fc);
1291 return PTR_ERR(req);
1294 fuse_set_nowrite(inode);
1296 memset(&inarg, 0, sizeof(inarg));
1297 memset(&outarg, 0, sizeof(outarg));
1298 iattr_to_fattr(attr, &inarg);
1300 struct fuse_file *ff = file->private_data;
1301 inarg.valid |= FATTR_FH;
1304 if (attr->ia_valid & ATTR_SIZE) {
1305 /* For mandatory locking in truncate */
1306 inarg.valid |= FATTR_LOCKOWNER;
1307 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1309 req->in.h.opcode = FUSE_SETATTR;
1310 req->in.h.nodeid = get_node_id(inode);
1311 req->in.numargs = 1;
1312 req->in.args[0].size = sizeof(inarg);
1313 req->in.args[0].value = &inarg;
1314 req->out.numargs = 1;
1316 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1318 req->out.args[0].size = sizeof(outarg);
1319 req->out.args[0].value = &outarg;
1320 fuse_request_send(fc, req);
1321 err = req->out.h.error;
1322 fuse_put_request(fc, req);
1325 fuse_invalidate_attr(inode);
1329 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1330 make_bad_inode(inode);
1335 spin_lock(&fc->lock);
1336 fuse_change_attributes_common(inode, &outarg.attr,
1337 attr_timeout(&outarg));
1338 oldsize = inode->i_size;
1339 i_size_write(inode, outarg.attr.size);
1342 /* NOTE: this may release/reacquire fc->lock */
1343 __fuse_release_nowrite(inode);
1345 spin_unlock(&fc->lock);
1348 * Only call invalidate_inode_pages2() after removing
1349 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1351 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1352 truncate_pagecache(inode, oldsize, outarg.attr.size);
1353 invalidate_inode_pages2(inode->i_mapping);
1360 fuse_release_nowrite(inode);
1365 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1367 if (attr->ia_valid & ATTR_FILE)
1368 return fuse_do_setattr(entry, attr, attr->ia_file);
1370 return fuse_do_setattr(entry, attr, NULL);
1373 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1376 struct inode *inode = entry->d_inode;
1377 struct fuse_conn *fc = get_fuse_conn(inode);
1379 if (!fuse_allow_task(fc, current))
1382 return fuse_update_attributes(inode, stat, NULL, NULL);
1385 static int fuse_setxattr(struct dentry *entry, const char *name,
1386 const void *value, size_t size, int flags)
1388 struct inode *inode = entry->d_inode;
1389 struct fuse_conn *fc = get_fuse_conn(inode);
1390 struct fuse_req *req;
1391 struct fuse_setxattr_in inarg;
1394 if (fc->no_setxattr)
1397 req = fuse_get_req(fc);
1399 return PTR_ERR(req);
1401 memset(&inarg, 0, sizeof(inarg));
1403 inarg.flags = flags;
1404 req->in.h.opcode = FUSE_SETXATTR;
1405 req->in.h.nodeid = get_node_id(inode);
1406 req->in.numargs = 3;
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 req->in.args[2].size = size;
1412 req->in.args[2].value = value;
1413 fuse_request_send(fc, req);
1414 err = req->out.h.error;
1415 fuse_put_request(fc, req);
1416 if (err == -ENOSYS) {
1417 fc->no_setxattr = 1;
1423 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1424 void *value, 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 (fc->no_getxattr)
1436 req = fuse_get_req(fc);
1438 return PTR_ERR(req);
1440 memset(&inarg, 0, sizeof(inarg));
1442 req->in.h.opcode = FUSE_GETXATTR;
1443 req->in.h.nodeid = get_node_id(inode);
1444 req->in.numargs = 2;
1445 req->in.args[0].size = sizeof(inarg);
1446 req->in.args[0].value = &inarg;
1447 req->in.args[1].size = strlen(name) + 1;
1448 req->in.args[1].value = name;
1449 /* This is really two different operations rolled into one */
1450 req->out.numargs = 1;
1452 req->out.argvar = 1;
1453 req->out.args[0].size = size;
1454 req->out.args[0].value = value;
1456 req->out.args[0].size = sizeof(outarg);
1457 req->out.args[0].value = &outarg;
1459 fuse_request_send(fc, req);
1460 ret = req->out.h.error;
1462 ret = size ? req->out.args[0].size : outarg.size;
1464 if (ret == -ENOSYS) {
1465 fc->no_getxattr = 1;
1469 fuse_put_request(fc, req);
1473 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1475 struct inode *inode = entry->d_inode;
1476 struct fuse_conn *fc = get_fuse_conn(inode);
1477 struct fuse_req *req;
1478 struct fuse_getxattr_in inarg;
1479 struct fuse_getxattr_out outarg;
1482 if (!fuse_allow_task(fc, current))
1485 if (fc->no_listxattr)
1488 req = fuse_get_req(fc);
1490 return PTR_ERR(req);
1492 memset(&inarg, 0, sizeof(inarg));
1494 req->in.h.opcode = FUSE_LISTXATTR;
1495 req->in.h.nodeid = get_node_id(inode);
1496 req->in.numargs = 1;
1497 req->in.args[0].size = sizeof(inarg);
1498 req->in.args[0].value = &inarg;
1499 /* This is really two different operations rolled into one */
1500 req->out.numargs = 1;
1502 req->out.argvar = 1;
1503 req->out.args[0].size = size;
1504 req->out.args[0].value = list;
1506 req->out.args[0].size = sizeof(outarg);
1507 req->out.args[0].value = &outarg;
1509 fuse_request_send(fc, req);
1510 ret = req->out.h.error;
1512 ret = size ? req->out.args[0].size : outarg.size;
1514 if (ret == -ENOSYS) {
1515 fc->no_listxattr = 1;
1519 fuse_put_request(fc, req);
1523 static int fuse_removexattr(struct dentry *entry, const char *name)
1525 struct inode *inode = entry->d_inode;
1526 struct fuse_conn *fc = get_fuse_conn(inode);
1527 struct fuse_req *req;
1530 if (fc->no_removexattr)
1533 req = fuse_get_req(fc);
1535 return PTR_ERR(req);
1537 req->in.h.opcode = FUSE_REMOVEXATTR;
1538 req->in.h.nodeid = get_node_id(inode);
1539 req->in.numargs = 1;
1540 req->in.args[0].size = strlen(name) + 1;
1541 req->in.args[0].value = name;
1542 fuse_request_send(fc, req);
1543 err = req->out.h.error;
1544 fuse_put_request(fc, req);
1545 if (err == -ENOSYS) {
1546 fc->no_removexattr = 1;
1552 static const struct inode_operations fuse_dir_inode_operations = {
1553 .lookup = fuse_lookup,
1554 .mkdir = fuse_mkdir,
1555 .symlink = fuse_symlink,
1556 .unlink = fuse_unlink,
1557 .rmdir = fuse_rmdir,
1558 .rename = fuse_rename,
1560 .setattr = fuse_setattr,
1561 .create = fuse_create,
1562 .mknod = fuse_mknod,
1563 .permission = fuse_permission,
1564 .getattr = fuse_getattr,
1565 .setxattr = fuse_setxattr,
1566 .getxattr = fuse_getxattr,
1567 .listxattr = fuse_listxattr,
1568 .removexattr = fuse_removexattr,
1571 static const struct file_operations fuse_dir_operations = {
1572 .llseek = generic_file_llseek,
1573 .read = generic_read_dir,
1574 .readdir = fuse_readdir,
1575 .open = fuse_dir_open,
1576 .release = fuse_dir_release,
1577 .fsync = fuse_dir_fsync,
1580 static const struct inode_operations fuse_common_inode_operations = {
1581 .setattr = fuse_setattr,
1582 .permission = fuse_permission,
1583 .getattr = fuse_getattr,
1584 .setxattr = fuse_setxattr,
1585 .getxattr = fuse_getxattr,
1586 .listxattr = fuse_listxattr,
1587 .removexattr = fuse_removexattr,
1590 static const struct inode_operations fuse_symlink_inode_operations = {
1591 .setattr = fuse_setattr,
1592 .follow_link = fuse_follow_link,
1593 .put_link = fuse_put_link,
1594 .readlink = generic_readlink,
1595 .getattr = fuse_getattr,
1596 .setxattr = fuse_setxattr,
1597 .getxattr = fuse_getxattr,
1598 .listxattr = fuse_listxattr,
1599 .removexattr = fuse_removexattr,
1602 void fuse_init_common(struct inode *inode)
1604 inode->i_op = &fuse_common_inode_operations;
1607 void fuse_init_dir(struct inode *inode)
1609 inode->i_op = &fuse_dir_inode_operations;
1610 inode->i_fop = &fuse_dir_operations;
1613 void fuse_init_symlink(struct inode *inode)
1615 inode->i_op = &fuse_symlink_inode_operations;