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 static 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_req *req, struct inode *dir,
116 struct dentry *entry,
117 struct fuse_entry_out *outarg)
119 struct fuse_conn *fc = get_fuse_conn(dir);
121 memset(outarg, 0, sizeof(struct fuse_entry_out));
122 req->in.h.opcode = FUSE_LOOKUP;
123 req->in.h.nodeid = get_node_id(dir);
125 req->in.args[0].size = entry->d_name.len + 1;
126 req->in.args[0].value = entry->d_name.name;
127 req->out.numargs = 1;
129 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
131 req->out.args[0].size = sizeof(struct fuse_entry_out);
132 req->out.args[0].value = outarg;
135 u64 fuse_get_attr_version(struct fuse_conn *fc)
140 * The spin lock isn't actually needed on 64bit archs, but we
141 * don't yet care too much about such optimizations.
143 spin_lock(&fc->lock);
144 curr_version = fc->attr_version;
145 spin_unlock(&fc->lock);
151 * Check whether the dentry is still valid
153 * If the entry validity timeout has expired and the dentry is
154 * positive, try to redo the lookup. If the lookup results in a
155 * different inode, then let the VFS invalidate the dentry and redo
156 * the lookup once more. If the lookup results in the same inode,
157 * then refresh the attributes, timeouts and mark the dentry valid.
159 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
161 struct inode *inode = entry->d_inode;
163 if (inode && is_bad_inode(inode))
165 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
167 struct fuse_entry_out outarg;
168 struct fuse_conn *fc;
169 struct fuse_req *req;
170 struct fuse_req *forget_req;
171 struct dentry *parent;
174 /* For negative dentries, always do a fresh lookup */
178 fc = get_fuse_conn(inode);
179 req = fuse_get_req(fc);
183 forget_req = fuse_get_req(fc);
184 if (IS_ERR(forget_req)) {
185 fuse_put_request(fc, req);
189 attr_version = fuse_get_attr_version(fc);
191 parent = dget_parent(entry);
192 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
193 request_send(fc, req);
195 err = req->out.h.error;
196 fuse_put_request(fc, req);
197 /* Zero nodeid is same as -ENOENT */
198 if (!err && !outarg.nodeid)
201 struct fuse_inode *fi = get_fuse_inode(inode);
202 if (outarg.nodeid != get_node_id(inode)) {
203 fuse_send_forget(fc, forget_req,
207 spin_lock(&fc->lock);
209 spin_unlock(&fc->lock);
211 fuse_put_request(fc, forget_req);
212 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
215 fuse_change_attributes(inode, &outarg.attr,
216 entry_attr_timeout(&outarg),
218 fuse_change_entry_timeout(entry, &outarg);
223 static int invalid_nodeid(u64 nodeid)
225 return !nodeid || nodeid == FUSE_ROOT_ID;
228 static struct dentry_operations fuse_dentry_operations = {
229 .d_revalidate = fuse_dentry_revalidate,
232 int fuse_valid_type(int m)
234 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
235 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
239 * Add a directory inode to a dentry, ensuring that no other dentry
240 * refers to this inode. Called with fc->inst_mutex.
242 static struct dentry *fuse_d_add_directory(struct dentry *entry,
245 struct dentry *alias = d_find_alias(inode);
246 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
247 /* This tries to shrink the subtree below alias */
248 fuse_invalidate_entry(alias);
250 if (!list_empty(&inode->i_dentry))
251 return ERR_PTR(-EBUSY);
255 return d_splice_alias(inode, entry);
258 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
259 struct nameidata *nd)
262 struct fuse_entry_out outarg;
263 struct inode *inode = NULL;
264 struct dentry *newent;
265 struct fuse_conn *fc = get_fuse_conn(dir);
266 struct fuse_req *req;
267 struct fuse_req *forget_req;
270 if (entry->d_name.len > FUSE_NAME_MAX)
271 return ERR_PTR(-ENAMETOOLONG);
273 req = fuse_get_req(fc);
275 return ERR_CAST(req);
277 forget_req = fuse_get_req(fc);
278 if (IS_ERR(forget_req)) {
279 fuse_put_request(fc, req);
280 return ERR_CAST(forget_req);
283 attr_version = fuse_get_attr_version(fc);
285 fuse_lookup_init(req, dir, entry, &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 &&
291 (invalid_nodeid(outarg.nodeid) ||
292 !fuse_valid_type(outarg.attr.mode)))
294 if (!err && outarg.nodeid) {
295 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
296 &outarg.attr, entry_attr_timeout(&outarg),
299 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
300 return ERR_PTR(-ENOMEM);
303 fuse_put_request(fc, forget_req);
304 if (err && err != -ENOENT)
307 if (inode && S_ISDIR(inode->i_mode)) {
308 mutex_lock(&fc->inst_mutex);
309 newent = fuse_d_add_directory(entry, inode);
310 mutex_unlock(&fc->inst_mutex);
311 if (IS_ERR(newent)) {
316 newent = d_splice_alias(inode, entry);
318 entry = newent ? newent : entry;
319 entry->d_op = &fuse_dentry_operations;
321 fuse_change_entry_timeout(entry, &outarg);
323 fuse_invalidate_entry_cache(entry);
328 * Synchronous release for the case when something goes wrong in CREATE_OPEN
330 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
331 u64 nodeid, int flags)
333 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
334 ff->reserved_req->force = 1;
335 request_send(fc, ff->reserved_req);
336 fuse_put_request(fc, ff->reserved_req);
341 * Atomic create+open operation
343 * If the filesystem doesn't support this, then fall back to separate
344 * 'mknod' + 'open' requests.
346 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
347 struct nameidata *nd)
351 struct fuse_conn *fc = get_fuse_conn(dir);
352 struct fuse_req *req;
353 struct fuse_req *forget_req;
354 struct fuse_open_in inarg;
355 struct fuse_open_out outopen;
356 struct fuse_entry_out outentry;
357 struct fuse_file *ff;
359 int flags = nd->intent.open.flags - 1;
364 forget_req = fuse_get_req(fc);
365 if (IS_ERR(forget_req))
366 return PTR_ERR(forget_req);
368 req = fuse_get_req(fc);
371 goto out_put_forget_req;
374 ff = fuse_file_alloc();
376 goto out_put_request;
379 memset(&inarg, 0, sizeof(inarg));
380 memset(&outentry, 0, sizeof(outentry));
383 req->in.h.opcode = FUSE_CREATE;
384 req->in.h.nodeid = get_node_id(dir);
386 req->in.args[0].size = sizeof(inarg);
387 req->in.args[0].value = &inarg;
388 req->in.args[1].size = entry->d_name.len + 1;
389 req->in.args[1].value = entry->d_name.name;
390 req->out.numargs = 2;
392 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
394 req->out.args[0].size = sizeof(outentry);
395 req->out.args[0].value = &outentry;
396 req->out.args[1].size = sizeof(outopen);
397 req->out.args[1].value = &outopen;
398 request_send(fc, req);
399 err = req->out.h.error;
407 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
410 fuse_put_request(fc, req);
411 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
412 &outentry.attr, entry_attr_timeout(&outentry), 0);
414 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
416 fuse_sync_release(fc, ff, outentry.nodeid, flags);
417 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
420 fuse_put_request(fc, forget_req);
421 d_instantiate(entry, inode);
422 fuse_change_entry_timeout(entry, &outentry);
423 fuse_invalidate_attr(dir);
424 file = lookup_instantiate_filp(nd, entry, generic_file_open);
427 fuse_sync_release(fc, ff, outentry.nodeid, flags);
428 return PTR_ERR(file);
430 fuse_finish_open(inode, file, ff, &outopen);
436 fuse_put_request(fc, req);
438 fuse_put_request(fc, forget_req);
443 * Code shared between mknod, mkdir, symlink and link
445 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
446 struct inode *dir, struct dentry *entry,
449 struct fuse_entry_out outarg;
452 struct fuse_req *forget_req;
454 forget_req = fuse_get_req(fc);
455 if (IS_ERR(forget_req)) {
456 fuse_put_request(fc, req);
457 return PTR_ERR(forget_req);
460 memset(&outarg, 0, sizeof(outarg));
461 req->in.h.nodeid = get_node_id(dir);
462 req->out.numargs = 1;
464 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
466 req->out.args[0].size = sizeof(outarg);
467 req->out.args[0].value = &outarg;
468 request_send(fc, req);
469 err = req->out.h.error;
470 fuse_put_request(fc, req);
472 goto out_put_forget_req;
475 if (invalid_nodeid(outarg.nodeid))
476 goto out_put_forget_req;
478 if ((outarg.attr.mode ^ mode) & S_IFMT)
479 goto out_put_forget_req;
481 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
482 &outarg.attr, entry_attr_timeout(&outarg), 0);
484 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
487 fuse_put_request(fc, forget_req);
489 if (S_ISDIR(inode->i_mode)) {
490 struct dentry *alias;
491 mutex_lock(&fc->inst_mutex);
492 alias = d_find_alias(inode);
494 /* New directory must have moved since mkdir */
495 mutex_unlock(&fc->inst_mutex);
500 d_instantiate(entry, inode);
501 mutex_unlock(&fc->inst_mutex);
503 d_instantiate(entry, inode);
505 fuse_change_entry_timeout(entry, &outarg);
506 fuse_invalidate_attr(dir);
510 fuse_put_request(fc, forget_req);
514 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
517 struct fuse_mknod_in inarg;
518 struct fuse_conn *fc = get_fuse_conn(dir);
519 struct fuse_req *req = fuse_get_req(fc);
523 memset(&inarg, 0, sizeof(inarg));
525 inarg.rdev = new_encode_dev(rdev);
526 req->in.h.opcode = FUSE_MKNOD;
528 req->in.args[0].size = sizeof(inarg);
529 req->in.args[0].value = &inarg;
530 req->in.args[1].size = entry->d_name.len + 1;
531 req->in.args[1].value = entry->d_name.name;
532 return create_new_entry(fc, req, dir, entry, mode);
535 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
536 struct nameidata *nd)
538 if (nd && (nd->flags & LOOKUP_OPEN)) {
539 int err = fuse_create_open(dir, entry, mode, nd);
542 /* Fall back on mknod */
544 return fuse_mknod(dir, entry, mode, 0);
547 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
549 struct fuse_mkdir_in inarg;
550 struct fuse_conn *fc = get_fuse_conn(dir);
551 struct fuse_req *req = fuse_get_req(fc);
555 memset(&inarg, 0, sizeof(inarg));
557 req->in.h.opcode = FUSE_MKDIR;
559 req->in.args[0].size = sizeof(inarg);
560 req->in.args[0].value = &inarg;
561 req->in.args[1].size = entry->d_name.len + 1;
562 req->in.args[1].value = entry->d_name.name;
563 return create_new_entry(fc, req, dir, entry, S_IFDIR);
566 static int fuse_symlink(struct inode *dir, struct dentry *entry,
569 struct fuse_conn *fc = get_fuse_conn(dir);
570 unsigned len = strlen(link) + 1;
571 struct fuse_req *req = fuse_get_req(fc);
575 req->in.h.opcode = FUSE_SYMLINK;
577 req->in.args[0].size = entry->d_name.len + 1;
578 req->in.args[0].value = entry->d_name.name;
579 req->in.args[1].size = len;
580 req->in.args[1].value = link;
581 return create_new_entry(fc, req, dir, entry, S_IFLNK);
584 static int fuse_unlink(struct inode *dir, struct dentry *entry)
587 struct fuse_conn *fc = get_fuse_conn(dir);
588 struct fuse_req *req = fuse_get_req(fc);
592 req->in.h.opcode = FUSE_UNLINK;
593 req->in.h.nodeid = get_node_id(dir);
595 req->in.args[0].size = entry->d_name.len + 1;
596 req->in.args[0].value = entry->d_name.name;
597 request_send(fc, req);
598 err = req->out.h.error;
599 fuse_put_request(fc, req);
601 struct inode *inode = entry->d_inode;
603 /* Set nlink to zero so the inode can be cleared, if
604 the inode does have more links this will be
605 discovered at the next lookup/getattr */
607 fuse_invalidate_attr(inode);
608 fuse_invalidate_attr(dir);
609 fuse_invalidate_entry_cache(entry);
610 } else if (err == -EINTR)
611 fuse_invalidate_entry(entry);
615 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
618 struct fuse_conn *fc = get_fuse_conn(dir);
619 struct fuse_req *req = fuse_get_req(fc);
623 req->in.h.opcode = FUSE_RMDIR;
624 req->in.h.nodeid = get_node_id(dir);
626 req->in.args[0].size = entry->d_name.len + 1;
627 req->in.args[0].value = entry->d_name.name;
628 request_send(fc, req);
629 err = req->out.h.error;
630 fuse_put_request(fc, req);
632 clear_nlink(entry->d_inode);
633 fuse_invalidate_attr(dir);
634 fuse_invalidate_entry_cache(entry);
635 } else if (err == -EINTR)
636 fuse_invalidate_entry(entry);
640 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
641 struct inode *newdir, struct dentry *newent)
644 struct fuse_rename_in inarg;
645 struct fuse_conn *fc = get_fuse_conn(olddir);
646 struct fuse_req *req = fuse_get_req(fc);
650 memset(&inarg, 0, sizeof(inarg));
651 inarg.newdir = get_node_id(newdir);
652 req->in.h.opcode = FUSE_RENAME;
653 req->in.h.nodeid = get_node_id(olddir);
655 req->in.args[0].size = sizeof(inarg);
656 req->in.args[0].value = &inarg;
657 req->in.args[1].size = oldent->d_name.len + 1;
658 req->in.args[1].value = oldent->d_name.name;
659 req->in.args[2].size = newent->d_name.len + 1;
660 req->in.args[2].value = newent->d_name.name;
661 request_send(fc, req);
662 err = req->out.h.error;
663 fuse_put_request(fc, req);
666 fuse_invalidate_attr(oldent->d_inode);
668 fuse_invalidate_attr(olddir);
669 if (olddir != newdir)
670 fuse_invalidate_attr(newdir);
672 /* newent will end up negative */
674 fuse_invalidate_entry_cache(newent);
675 } else if (err == -EINTR) {
676 /* If request was interrupted, DEITY only knows if the
677 rename actually took place. If the invalidation
678 fails (e.g. some process has CWD under the renamed
679 directory), then there can be inconsistency between
680 the dcache and the real filesystem. Tough luck. */
681 fuse_invalidate_entry(oldent);
683 fuse_invalidate_entry(newent);
689 static int fuse_link(struct dentry *entry, struct inode *newdir,
690 struct dentry *newent)
693 struct fuse_link_in inarg;
694 struct inode *inode = entry->d_inode;
695 struct fuse_conn *fc = get_fuse_conn(inode);
696 struct fuse_req *req = fuse_get_req(fc);
700 memset(&inarg, 0, sizeof(inarg));
701 inarg.oldnodeid = get_node_id(inode);
702 req->in.h.opcode = FUSE_LINK;
704 req->in.args[0].size = sizeof(inarg);
705 req->in.args[0].value = &inarg;
706 req->in.args[1].size = newent->d_name.len + 1;
707 req->in.args[1].value = newent->d_name.name;
708 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
709 /* Contrary to "normal" filesystems it can happen that link
710 makes two "logical" inodes point to the same "physical"
711 inode. We invalidate the attributes of the old one, so it
712 will reflect changes in the backing inode (link count,
715 if (!err || err == -EINTR)
716 fuse_invalidate_attr(inode);
720 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
723 stat->dev = inode->i_sb->s_dev;
724 stat->ino = attr->ino;
725 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
726 stat->nlink = attr->nlink;
727 stat->uid = attr->uid;
728 stat->gid = attr->gid;
729 stat->rdev = inode->i_rdev;
730 stat->atime.tv_sec = attr->atime;
731 stat->atime.tv_nsec = attr->atimensec;
732 stat->mtime.tv_sec = attr->mtime;
733 stat->mtime.tv_nsec = attr->mtimensec;
734 stat->ctime.tv_sec = attr->ctime;
735 stat->ctime.tv_nsec = attr->ctimensec;
736 stat->size = attr->size;
737 stat->blocks = attr->blocks;
738 stat->blksize = (1 << inode->i_blkbits);
741 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
745 struct fuse_getattr_in inarg;
746 struct fuse_attr_out outarg;
747 struct fuse_conn *fc = get_fuse_conn(inode);
748 struct fuse_req *req;
751 req = fuse_get_req(fc);
755 attr_version = fuse_get_attr_version(fc);
757 memset(&inarg, 0, sizeof(inarg));
758 memset(&outarg, 0, sizeof(outarg));
759 /* Directories have separate file-handle space */
760 if (file && S_ISREG(inode->i_mode)) {
761 struct fuse_file *ff = file->private_data;
763 inarg.getattr_flags |= FUSE_GETATTR_FH;
766 req->in.h.opcode = FUSE_GETATTR;
767 req->in.h.nodeid = get_node_id(inode);
769 req->in.args[0].size = sizeof(inarg);
770 req->in.args[0].value = &inarg;
771 req->out.numargs = 1;
773 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
775 req->out.args[0].size = sizeof(outarg);
776 req->out.args[0].value = &outarg;
777 request_send(fc, req);
778 err = req->out.h.error;
779 fuse_put_request(fc, req);
781 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
782 make_bad_inode(inode);
785 fuse_change_attributes(inode, &outarg.attr,
786 attr_timeout(&outarg),
789 fuse_fillattr(inode, &outarg.attr, stat);
795 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
796 struct file *file, bool *refreshed)
798 struct fuse_inode *fi = get_fuse_inode(inode);
802 if (fi->i_time < get_jiffies_64()) {
804 err = fuse_do_getattr(inode, stat, file);
809 generic_fillattr(inode, stat);
810 stat->mode = fi->orig_i_mode;
814 if (refreshed != NULL)
821 * Calling into a user-controlled filesystem gives the filesystem
822 * daemon ptrace-like capabilities over the requester process. This
823 * means, that the filesystem daemon is able to record the exact
824 * filesystem operations performed, and can also control the behavior
825 * of the requester process in otherwise impossible ways. For example
826 * it can delay the operation for arbitrary length of time allowing
827 * DoS against the requester.
829 * For this reason only those processes can call into the filesystem,
830 * for which the owner of the mount has ptrace privilege. This
831 * excludes processes started by other users, suid or sgid processes.
833 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
835 if (fc->flags & FUSE_ALLOW_OTHER)
838 if (task->euid == fc->user_id &&
839 task->suid == fc->user_id &&
840 task->uid == fc->user_id &&
841 task->egid == fc->group_id &&
842 task->sgid == fc->group_id &&
843 task->gid == fc->group_id)
849 static int fuse_access(struct inode *inode, int mask)
851 struct fuse_conn *fc = get_fuse_conn(inode);
852 struct fuse_req *req;
853 struct fuse_access_in inarg;
859 req = fuse_get_req(fc);
863 memset(&inarg, 0, sizeof(inarg));
865 req->in.h.opcode = FUSE_ACCESS;
866 req->in.h.nodeid = get_node_id(inode);
868 req->in.args[0].size = sizeof(inarg);
869 req->in.args[0].value = &inarg;
870 request_send(fc, req);
871 err = req->out.h.error;
872 fuse_put_request(fc, req);
873 if (err == -ENOSYS) {
881 * Check permission. The two basic access models of FUSE are:
883 * 1) Local access checking ('default_permissions' mount option) based
884 * on file mode. This is the plain old disk filesystem permission
887 * 2) "Remote" access checking, where server is responsible for
888 * checking permission in each inode operation. An exception to this
889 * is if ->permission() was invoked from sys_access() in which case an
890 * access request is sent. Execute permission is still checked
891 * locally based on file mode.
893 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
895 struct fuse_conn *fc = get_fuse_conn(inode);
896 bool refreshed = false;
899 if (!fuse_allow_task(fc, current))
903 * If attributes are needed, refresh them before proceeding
905 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
906 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
907 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
912 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
913 err = generic_permission(inode, mask, NULL);
915 /* If permission is denied, try to refresh file
916 attributes. This is also needed, because the root
917 node will at first have no permissions */
918 if (err == -EACCES && !refreshed) {
919 err = fuse_do_getattr(inode, NULL, NULL);
921 err = generic_permission(inode, mask, NULL);
924 /* Note: the opposite of the above test does not
925 exist. So if permissions are revoked this won't be
926 noticed immediately, only after the attribute
927 timeout has expired */
928 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
929 err = fuse_access(inode, mask);
930 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
931 if (!(inode->i_mode & S_IXUGO)) {
935 err = fuse_do_getattr(inode, NULL, NULL);
936 if (!err && !(inode->i_mode & S_IXUGO))
943 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
944 void *dstbuf, filldir_t filldir)
946 while (nbytes >= FUSE_NAME_OFFSET) {
947 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
948 size_t reclen = FUSE_DIRENT_SIZE(dirent);
950 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
955 over = filldir(dstbuf, dirent->name, dirent->namelen,
956 file->f_pos, dirent->ino, dirent->type);
962 file->f_pos = dirent->off;
968 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
973 struct inode *inode = file->f_path.dentry->d_inode;
974 struct fuse_conn *fc = get_fuse_conn(inode);
975 struct fuse_req *req;
977 if (is_bad_inode(inode))
980 req = fuse_get_req(fc);
984 page = alloc_page(GFP_KERNEL);
986 fuse_put_request(fc, req);
990 req->pages[0] = page;
991 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
992 request_send(fc, req);
993 nbytes = req->out.args[0].size;
994 err = req->out.h.error;
995 fuse_put_request(fc, req);
997 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1001 fuse_invalidate_attr(inode); /* atime changed */
1005 static char *read_link(struct dentry *dentry)
1007 struct inode *inode = dentry->d_inode;
1008 struct fuse_conn *fc = get_fuse_conn(inode);
1009 struct fuse_req *req = fuse_get_req(fc);
1013 return ERR_CAST(req);
1015 link = (char *) __get_free_page(GFP_KERNEL);
1017 link = ERR_PTR(-ENOMEM);
1020 req->in.h.opcode = FUSE_READLINK;
1021 req->in.h.nodeid = get_node_id(inode);
1022 req->out.argvar = 1;
1023 req->out.numargs = 1;
1024 req->out.args[0].size = PAGE_SIZE - 1;
1025 req->out.args[0].value = link;
1026 request_send(fc, req);
1027 if (req->out.h.error) {
1028 free_page((unsigned long) link);
1029 link = ERR_PTR(req->out.h.error);
1031 link[req->out.args[0].size] = '\0';
1033 fuse_put_request(fc, req);
1034 fuse_invalidate_attr(inode); /* atime changed */
1038 static void free_link(char *link)
1041 free_page((unsigned long) link);
1044 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1046 nd_set_link(nd, read_link(dentry));
1050 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1052 free_link(nd_get_link(nd));
1055 static int fuse_dir_open(struct inode *inode, struct file *file)
1057 return fuse_open_common(inode, file, 1);
1060 static int fuse_dir_release(struct inode *inode, struct file *file)
1062 return fuse_release_common(inode, file, 1);
1065 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1067 /* nfsd can call this with no file */
1068 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1071 static bool update_mtime(unsigned ivalid)
1073 /* Always update if mtime is explicitly set */
1074 if (ivalid & ATTR_MTIME_SET)
1077 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1078 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1081 /* In all other cases update */
1085 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1087 unsigned ivalid = iattr->ia_valid;
1089 if (ivalid & ATTR_MODE)
1090 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1091 if (ivalid & ATTR_UID)
1092 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1093 if (ivalid & ATTR_GID)
1094 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1095 if (ivalid & ATTR_SIZE)
1096 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1097 if (ivalid & ATTR_ATIME) {
1098 arg->valid |= FATTR_ATIME;
1099 arg->atime = iattr->ia_atime.tv_sec;
1100 arg->atimensec = iattr->ia_atime.tv_nsec;
1101 if (!(ivalid & ATTR_ATIME_SET))
1102 arg->valid |= FATTR_ATIME_NOW;
1104 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1105 arg->valid |= FATTR_MTIME;
1106 arg->mtime = iattr->ia_mtime.tv_sec;
1107 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1108 if (!(ivalid & ATTR_MTIME_SET))
1109 arg->valid |= FATTR_MTIME_NOW;
1114 * Prevent concurrent writepages on inode
1116 * This is done by adding a negative bias to the inode write counter
1117 * and waiting for all pending writes to finish.
1119 void fuse_set_nowrite(struct inode *inode)
1121 struct fuse_conn *fc = get_fuse_conn(inode);
1122 struct fuse_inode *fi = get_fuse_inode(inode);
1124 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1126 spin_lock(&fc->lock);
1127 BUG_ON(fi->writectr < 0);
1128 fi->writectr += FUSE_NOWRITE;
1129 spin_unlock(&fc->lock);
1130 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1134 * Allow writepages on inode
1136 * Remove the bias from the writecounter and send any queued
1139 static void __fuse_release_nowrite(struct inode *inode)
1141 struct fuse_inode *fi = get_fuse_inode(inode);
1143 BUG_ON(fi->writectr != FUSE_NOWRITE);
1145 fuse_flush_writepages(inode);
1148 void fuse_release_nowrite(struct inode *inode)
1150 struct fuse_conn *fc = get_fuse_conn(inode);
1152 spin_lock(&fc->lock);
1153 __fuse_release_nowrite(inode);
1154 spin_unlock(&fc->lock);
1158 * Set attributes, and at the same time refresh them.
1160 * Truncation is slightly complicated, because the 'truncate' request
1161 * may fail, in which case we don't want to touch the mapping.
1162 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1163 * and the actual truncation by hand.
1165 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1168 struct inode *inode = entry->d_inode;
1169 struct fuse_conn *fc = get_fuse_conn(inode);
1170 struct fuse_req *req;
1171 struct fuse_setattr_in inarg;
1172 struct fuse_attr_out outarg;
1173 bool is_truncate = false;
1177 if (!fuse_allow_task(fc, current))
1180 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1181 err = inode_change_ok(inode, attr);
1186 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1189 if (attr->ia_valid & ATTR_SIZE) {
1190 unsigned long limit;
1191 if (IS_SWAPFILE(inode))
1193 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1194 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1195 send_sig(SIGXFSZ, current, 0);
1201 req = fuse_get_req(fc);
1203 return PTR_ERR(req);
1206 fuse_set_nowrite(inode);
1208 memset(&inarg, 0, sizeof(inarg));
1209 memset(&outarg, 0, sizeof(outarg));
1210 iattr_to_fattr(attr, &inarg);
1212 struct fuse_file *ff = file->private_data;
1213 inarg.valid |= FATTR_FH;
1216 if (attr->ia_valid & ATTR_SIZE) {
1217 /* For mandatory locking in truncate */
1218 inarg.valid |= FATTR_LOCKOWNER;
1219 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1221 req->in.h.opcode = FUSE_SETATTR;
1222 req->in.h.nodeid = get_node_id(inode);
1223 req->in.numargs = 1;
1224 req->in.args[0].size = sizeof(inarg);
1225 req->in.args[0].value = &inarg;
1226 req->out.numargs = 1;
1228 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1230 req->out.args[0].size = sizeof(outarg);
1231 req->out.args[0].value = &outarg;
1232 request_send(fc, req);
1233 err = req->out.h.error;
1234 fuse_put_request(fc, req);
1237 fuse_invalidate_attr(inode);
1241 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1242 make_bad_inode(inode);
1247 spin_lock(&fc->lock);
1248 fuse_change_attributes_common(inode, &outarg.attr,
1249 attr_timeout(&outarg));
1250 oldsize = inode->i_size;
1251 i_size_write(inode, outarg.attr.size);
1254 /* NOTE: this may release/reacquire fc->lock */
1255 __fuse_release_nowrite(inode);
1257 spin_unlock(&fc->lock);
1260 * Only call invalidate_inode_pages2() after removing
1261 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1263 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1264 if (outarg.attr.size < oldsize)
1265 fuse_truncate(inode->i_mapping, outarg.attr.size);
1266 invalidate_inode_pages2(inode->i_mapping);
1273 fuse_release_nowrite(inode);
1278 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1280 if (attr->ia_valid & ATTR_FILE)
1281 return fuse_do_setattr(entry, attr, attr->ia_file);
1283 return fuse_do_setattr(entry, attr, NULL);
1286 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1289 struct inode *inode = entry->d_inode;
1290 struct fuse_conn *fc = get_fuse_conn(inode);
1292 if (!fuse_allow_task(fc, current))
1295 return fuse_update_attributes(inode, stat, NULL, NULL);
1298 static int fuse_setxattr(struct dentry *entry, const char *name,
1299 const void *value, size_t size, int flags)
1301 struct inode *inode = entry->d_inode;
1302 struct fuse_conn *fc = get_fuse_conn(inode);
1303 struct fuse_req *req;
1304 struct fuse_setxattr_in inarg;
1307 if (fc->no_setxattr)
1310 req = fuse_get_req(fc);
1312 return PTR_ERR(req);
1314 memset(&inarg, 0, sizeof(inarg));
1316 inarg.flags = flags;
1317 req->in.h.opcode = FUSE_SETXATTR;
1318 req->in.h.nodeid = get_node_id(inode);
1319 req->in.numargs = 3;
1320 req->in.args[0].size = sizeof(inarg);
1321 req->in.args[0].value = &inarg;
1322 req->in.args[1].size = strlen(name) + 1;
1323 req->in.args[1].value = name;
1324 req->in.args[2].size = size;
1325 req->in.args[2].value = value;
1326 request_send(fc, req);
1327 err = req->out.h.error;
1328 fuse_put_request(fc, req);
1329 if (err == -ENOSYS) {
1330 fc->no_setxattr = 1;
1336 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1337 void *value, size_t size)
1339 struct inode *inode = entry->d_inode;
1340 struct fuse_conn *fc = get_fuse_conn(inode);
1341 struct fuse_req *req;
1342 struct fuse_getxattr_in inarg;
1343 struct fuse_getxattr_out outarg;
1346 if (fc->no_getxattr)
1349 req = fuse_get_req(fc);
1351 return PTR_ERR(req);
1353 memset(&inarg, 0, sizeof(inarg));
1355 req->in.h.opcode = FUSE_GETXATTR;
1356 req->in.h.nodeid = get_node_id(inode);
1357 req->in.numargs = 2;
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 /* This is really two different operations rolled into one */
1363 req->out.numargs = 1;
1365 req->out.argvar = 1;
1366 req->out.args[0].size = size;
1367 req->out.args[0].value = value;
1369 req->out.args[0].size = sizeof(outarg);
1370 req->out.args[0].value = &outarg;
1372 request_send(fc, req);
1373 ret = req->out.h.error;
1375 ret = size ? req->out.args[0].size : outarg.size;
1377 if (ret == -ENOSYS) {
1378 fc->no_getxattr = 1;
1382 fuse_put_request(fc, req);
1386 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1388 struct inode *inode = entry->d_inode;
1389 struct fuse_conn *fc = get_fuse_conn(inode);
1390 struct fuse_req *req;
1391 struct fuse_getxattr_in inarg;
1392 struct fuse_getxattr_out outarg;
1395 if (!fuse_allow_task(fc, current))
1398 if (fc->no_listxattr)
1401 req = fuse_get_req(fc);
1403 return PTR_ERR(req);
1405 memset(&inarg, 0, sizeof(inarg));
1407 req->in.h.opcode = FUSE_LISTXATTR;
1408 req->in.h.nodeid = get_node_id(inode);
1409 req->in.numargs = 1;
1410 req->in.args[0].size = sizeof(inarg);
1411 req->in.args[0].value = &inarg;
1412 /* This is really two different operations rolled into one */
1413 req->out.numargs = 1;
1415 req->out.argvar = 1;
1416 req->out.args[0].size = size;
1417 req->out.args[0].value = list;
1419 req->out.args[0].size = sizeof(outarg);
1420 req->out.args[0].value = &outarg;
1422 request_send(fc, req);
1423 ret = req->out.h.error;
1425 ret = size ? req->out.args[0].size : outarg.size;
1427 if (ret == -ENOSYS) {
1428 fc->no_listxattr = 1;
1432 fuse_put_request(fc, req);
1436 static int fuse_removexattr(struct dentry *entry, const char *name)
1438 struct inode *inode = entry->d_inode;
1439 struct fuse_conn *fc = get_fuse_conn(inode);
1440 struct fuse_req *req;
1443 if (fc->no_removexattr)
1446 req = fuse_get_req(fc);
1448 return PTR_ERR(req);
1450 req->in.h.opcode = FUSE_REMOVEXATTR;
1451 req->in.h.nodeid = get_node_id(inode);
1452 req->in.numargs = 1;
1453 req->in.args[0].size = strlen(name) + 1;
1454 req->in.args[0].value = name;
1455 request_send(fc, req);
1456 err = req->out.h.error;
1457 fuse_put_request(fc, req);
1458 if (err == -ENOSYS) {
1459 fc->no_removexattr = 1;
1465 static const struct inode_operations fuse_dir_inode_operations = {
1466 .lookup = fuse_lookup,
1467 .mkdir = fuse_mkdir,
1468 .symlink = fuse_symlink,
1469 .unlink = fuse_unlink,
1470 .rmdir = fuse_rmdir,
1471 .rename = fuse_rename,
1473 .setattr = fuse_setattr,
1474 .create = fuse_create,
1475 .mknod = fuse_mknod,
1476 .permission = fuse_permission,
1477 .getattr = fuse_getattr,
1478 .setxattr = fuse_setxattr,
1479 .getxattr = fuse_getxattr,
1480 .listxattr = fuse_listxattr,
1481 .removexattr = fuse_removexattr,
1484 static const struct file_operations fuse_dir_operations = {
1485 .llseek = generic_file_llseek,
1486 .read = generic_read_dir,
1487 .readdir = fuse_readdir,
1488 .open = fuse_dir_open,
1489 .release = fuse_dir_release,
1490 .fsync = fuse_dir_fsync,
1493 static const struct inode_operations fuse_common_inode_operations = {
1494 .setattr = fuse_setattr,
1495 .permission = fuse_permission,
1496 .getattr = fuse_getattr,
1497 .setxattr = fuse_setxattr,
1498 .getxattr = fuse_getxattr,
1499 .listxattr = fuse_listxattr,
1500 .removexattr = fuse_removexattr,
1503 static const struct inode_operations fuse_symlink_inode_operations = {
1504 .setattr = fuse_setattr,
1505 .follow_link = fuse_follow_link,
1506 .put_link = fuse_put_link,
1507 .readlink = generic_readlink,
1508 .getattr = fuse_getattr,
1509 .setxattr = fuse_setxattr,
1510 .getxattr = fuse_getxattr,
1511 .listxattr = fuse_listxattr,
1512 .removexattr = fuse_removexattr,
1515 void fuse_init_common(struct inode *inode)
1517 inode->i_op = &fuse_common_inode_operations;
1520 void fuse_init_dir(struct inode *inode)
1522 inode->i_op = &fuse_dir_inode_operations;
1523 inode->i_fop = &fuse_dir_operations;
1526 void fuse_init_symlink(struct inode *inode)
1528 inode->i_op = &fuse_symlink_inode_operations;