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;
136 * Check whether the dentry is still valid
138 * If the entry validity timeout has expired and the dentry is
139 * positive, try to redo the lookup. If the lookup results in a
140 * different inode, then let the VFS invalidate the dentry and redo
141 * the lookup once more. If the lookup results in the same inode,
142 * then refresh the attributes, timeouts and mark the dentry valid.
144 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
146 struct inode *inode = entry->d_inode;
148 if (inode && is_bad_inode(inode))
150 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
152 struct fuse_entry_out outarg;
153 struct fuse_conn *fc;
154 struct fuse_req *req;
155 struct fuse_req *forget_req;
156 struct dentry *parent;
159 /* For negative dentries, always do a fresh lookup */
163 fc = get_fuse_conn(inode);
164 req = fuse_get_req(fc);
168 forget_req = fuse_get_req(fc);
169 if (IS_ERR(forget_req)) {
170 fuse_put_request(fc, req);
174 spin_lock(&fc->lock);
175 attr_version = fc->attr_version;
176 spin_unlock(&fc->lock);
178 parent = dget_parent(entry);
179 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
180 request_send(fc, req);
182 err = req->out.h.error;
183 fuse_put_request(fc, req);
184 /* Zero nodeid is same as -ENOENT */
185 if (!err && !outarg.nodeid)
188 struct fuse_inode *fi = get_fuse_inode(inode);
189 if (outarg.nodeid != get_node_id(inode)) {
190 fuse_send_forget(fc, forget_req,
194 spin_lock(&fc->lock);
196 spin_unlock(&fc->lock);
198 fuse_put_request(fc, forget_req);
199 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
202 fuse_change_attributes(inode, &outarg.attr,
203 entry_attr_timeout(&outarg),
205 fuse_change_entry_timeout(entry, &outarg);
210 static int invalid_nodeid(u64 nodeid)
212 return !nodeid || nodeid == FUSE_ROOT_ID;
215 static struct dentry_operations fuse_dentry_operations = {
216 .d_revalidate = fuse_dentry_revalidate,
219 int fuse_valid_type(int m)
221 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
222 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
226 * Add a directory inode to a dentry, ensuring that no other dentry
227 * refers to this inode. Called with fc->inst_mutex.
229 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
231 struct dentry *alias = d_find_alias(inode);
233 /* This tries to shrink the subtree below alias */
234 fuse_invalidate_entry(alias);
236 if (!list_empty(&inode->i_dentry))
243 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
244 struct nameidata *nd)
247 struct fuse_entry_out outarg;
248 struct inode *inode = NULL;
249 struct fuse_conn *fc = get_fuse_conn(dir);
250 struct fuse_req *req;
251 struct fuse_req *forget_req;
254 if (entry->d_name.len > FUSE_NAME_MAX)
255 return ERR_PTR(-ENAMETOOLONG);
257 req = fuse_get_req(fc);
259 return ERR_PTR(PTR_ERR(req));
261 forget_req = fuse_get_req(fc);
262 if (IS_ERR(forget_req)) {
263 fuse_put_request(fc, req);
264 return ERR_PTR(PTR_ERR(forget_req));
267 spin_lock(&fc->lock);
268 attr_version = fc->attr_version;
269 spin_unlock(&fc->lock);
271 fuse_lookup_init(req, dir, entry, &outarg);
272 request_send(fc, req);
273 err = req->out.h.error;
274 fuse_put_request(fc, req);
275 /* Zero nodeid is same as -ENOENT, but with valid timeout */
276 if (!err && outarg.nodeid &&
277 (invalid_nodeid(outarg.nodeid) ||
278 !fuse_valid_type(outarg.attr.mode)))
280 if (!err && outarg.nodeid) {
281 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
282 &outarg.attr, entry_attr_timeout(&outarg),
285 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
286 return ERR_PTR(-ENOMEM);
289 fuse_put_request(fc, forget_req);
290 if (err && err != -ENOENT)
293 if (inode && S_ISDIR(inode->i_mode)) {
294 mutex_lock(&fc->inst_mutex);
295 err = fuse_d_add_directory(entry, inode);
296 mutex_unlock(&fc->inst_mutex);
304 entry->d_op = &fuse_dentry_operations;
306 fuse_change_entry_timeout(entry, &outarg);
308 fuse_invalidate_entry_cache(entry);
313 * Synchronous release for the case when something goes wrong in CREATE_OPEN
315 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
316 u64 nodeid, int flags)
318 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
319 ff->reserved_req->force = 1;
320 request_send(fc, ff->reserved_req);
321 fuse_put_request(fc, ff->reserved_req);
326 * Atomic create+open operation
328 * If the filesystem doesn't support this, then fall back to separate
329 * 'mknod' + 'open' requests.
331 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
332 struct nameidata *nd)
336 struct fuse_conn *fc = get_fuse_conn(dir);
337 struct fuse_req *req;
338 struct fuse_req *forget_req;
339 struct fuse_open_in inarg;
340 struct fuse_open_out outopen;
341 struct fuse_entry_out outentry;
342 struct fuse_file *ff;
344 int flags = nd->intent.open.flags - 1;
349 forget_req = fuse_get_req(fc);
350 if (IS_ERR(forget_req))
351 return PTR_ERR(forget_req);
353 req = fuse_get_req(fc);
356 goto out_put_forget_req;
359 ff = fuse_file_alloc();
361 goto out_put_request;
364 memset(&inarg, 0, sizeof(inarg));
365 memset(&outentry, 0, sizeof(outentry));
368 req->in.h.opcode = FUSE_CREATE;
369 req->in.h.nodeid = get_node_id(dir);
371 req->in.args[0].size = sizeof(inarg);
372 req->in.args[0].value = &inarg;
373 req->in.args[1].size = entry->d_name.len + 1;
374 req->in.args[1].value = entry->d_name.name;
375 req->out.numargs = 2;
377 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
379 req->out.args[0].size = sizeof(outentry);
380 req->out.args[0].value = &outentry;
381 req->out.args[1].size = sizeof(outopen);
382 req->out.args[1].value = &outopen;
383 request_send(fc, req);
384 err = req->out.h.error;
392 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
395 fuse_put_request(fc, req);
396 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
397 &outentry.attr, entry_attr_timeout(&outentry), 0);
399 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
401 fuse_sync_release(fc, ff, outentry.nodeid, flags);
402 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
405 fuse_put_request(fc, forget_req);
406 d_instantiate(entry, inode);
407 fuse_change_entry_timeout(entry, &outentry);
408 file = lookup_instantiate_filp(nd, entry, generic_file_open);
411 fuse_sync_release(fc, ff, outentry.nodeid, flags);
412 return PTR_ERR(file);
414 fuse_finish_open(inode, file, ff, &outopen);
420 fuse_put_request(fc, req);
422 fuse_put_request(fc, forget_req);
427 * Code shared between mknod, mkdir, symlink and link
429 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
430 struct inode *dir, struct dentry *entry,
433 struct fuse_entry_out outarg;
436 struct fuse_req *forget_req;
438 forget_req = fuse_get_req(fc);
439 if (IS_ERR(forget_req)) {
440 fuse_put_request(fc, req);
441 return PTR_ERR(forget_req);
444 memset(&outarg, 0, sizeof(outarg));
445 req->in.h.nodeid = get_node_id(dir);
446 req->out.numargs = 1;
448 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
450 req->out.args[0].size = sizeof(outarg);
451 req->out.args[0].value = &outarg;
452 request_send(fc, req);
453 err = req->out.h.error;
454 fuse_put_request(fc, req);
456 goto out_put_forget_req;
459 if (invalid_nodeid(outarg.nodeid))
460 goto out_put_forget_req;
462 if ((outarg.attr.mode ^ mode) & S_IFMT)
463 goto out_put_forget_req;
465 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
466 &outarg.attr, entry_attr_timeout(&outarg), 0);
468 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
471 fuse_put_request(fc, forget_req);
473 if (S_ISDIR(inode->i_mode)) {
474 struct dentry *alias;
475 mutex_lock(&fc->inst_mutex);
476 alias = d_find_alias(inode);
478 /* New directory must have moved since mkdir */
479 mutex_unlock(&fc->inst_mutex);
484 d_instantiate(entry, inode);
485 mutex_unlock(&fc->inst_mutex);
487 d_instantiate(entry, inode);
489 fuse_change_entry_timeout(entry, &outarg);
490 fuse_invalidate_attr(dir);
494 fuse_put_request(fc, forget_req);
498 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
501 struct fuse_mknod_in inarg;
502 struct fuse_conn *fc = get_fuse_conn(dir);
503 struct fuse_req *req = fuse_get_req(fc);
507 memset(&inarg, 0, sizeof(inarg));
509 inarg.rdev = new_encode_dev(rdev);
510 req->in.h.opcode = FUSE_MKNOD;
512 req->in.args[0].size = sizeof(inarg);
513 req->in.args[0].value = &inarg;
514 req->in.args[1].size = entry->d_name.len + 1;
515 req->in.args[1].value = entry->d_name.name;
516 return create_new_entry(fc, req, dir, entry, mode);
519 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
520 struct nameidata *nd)
522 if (nd && (nd->flags & LOOKUP_OPEN)) {
523 int err = fuse_create_open(dir, entry, mode, nd);
526 /* Fall back on mknod */
528 return fuse_mknod(dir, entry, mode, 0);
531 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
533 struct fuse_mkdir_in inarg;
534 struct fuse_conn *fc = get_fuse_conn(dir);
535 struct fuse_req *req = fuse_get_req(fc);
539 memset(&inarg, 0, sizeof(inarg));
541 req->in.h.opcode = FUSE_MKDIR;
543 req->in.args[0].size = sizeof(inarg);
544 req->in.args[0].value = &inarg;
545 req->in.args[1].size = entry->d_name.len + 1;
546 req->in.args[1].value = entry->d_name.name;
547 return create_new_entry(fc, req, dir, entry, S_IFDIR);
550 static int fuse_symlink(struct inode *dir, struct dentry *entry,
553 struct fuse_conn *fc = get_fuse_conn(dir);
554 unsigned len = strlen(link) + 1;
555 struct fuse_req *req = fuse_get_req(fc);
559 req->in.h.opcode = FUSE_SYMLINK;
561 req->in.args[0].size = entry->d_name.len + 1;
562 req->in.args[0].value = entry->d_name.name;
563 req->in.args[1].size = len;
564 req->in.args[1].value = link;
565 return create_new_entry(fc, req, dir, entry, S_IFLNK);
568 static int fuse_unlink(struct inode *dir, struct dentry *entry)
571 struct fuse_conn *fc = get_fuse_conn(dir);
572 struct fuse_req *req = fuse_get_req(fc);
576 req->in.h.opcode = FUSE_UNLINK;
577 req->in.h.nodeid = get_node_id(dir);
579 req->in.args[0].size = entry->d_name.len + 1;
580 req->in.args[0].value = entry->d_name.name;
581 request_send(fc, req);
582 err = req->out.h.error;
583 fuse_put_request(fc, req);
585 struct inode *inode = entry->d_inode;
587 /* Set nlink to zero so the inode can be cleared, if
588 the inode does have more links this will be
589 discovered at the next lookup/getattr */
591 fuse_invalidate_attr(inode);
592 fuse_invalidate_attr(dir);
593 fuse_invalidate_entry_cache(entry);
594 } else if (err == -EINTR)
595 fuse_invalidate_entry(entry);
599 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
602 struct fuse_conn *fc = get_fuse_conn(dir);
603 struct fuse_req *req = fuse_get_req(fc);
607 req->in.h.opcode = FUSE_RMDIR;
608 req->in.h.nodeid = get_node_id(dir);
610 req->in.args[0].size = entry->d_name.len + 1;
611 req->in.args[0].value = entry->d_name.name;
612 request_send(fc, req);
613 err = req->out.h.error;
614 fuse_put_request(fc, req);
616 clear_nlink(entry->d_inode);
617 fuse_invalidate_attr(dir);
618 fuse_invalidate_entry_cache(entry);
619 } else if (err == -EINTR)
620 fuse_invalidate_entry(entry);
624 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
625 struct inode *newdir, struct dentry *newent)
628 struct fuse_rename_in inarg;
629 struct fuse_conn *fc = get_fuse_conn(olddir);
630 struct fuse_req *req = fuse_get_req(fc);
634 memset(&inarg, 0, sizeof(inarg));
635 inarg.newdir = get_node_id(newdir);
636 req->in.h.opcode = FUSE_RENAME;
637 req->in.h.nodeid = get_node_id(olddir);
639 req->in.args[0].size = sizeof(inarg);
640 req->in.args[0].value = &inarg;
641 req->in.args[1].size = oldent->d_name.len + 1;
642 req->in.args[1].value = oldent->d_name.name;
643 req->in.args[2].size = newent->d_name.len + 1;
644 req->in.args[2].value = newent->d_name.name;
645 request_send(fc, req);
646 err = req->out.h.error;
647 fuse_put_request(fc, req);
649 fuse_invalidate_attr(olddir);
650 if (olddir != newdir)
651 fuse_invalidate_attr(newdir);
653 /* newent will end up negative */
655 fuse_invalidate_entry_cache(newent);
656 } else if (err == -EINTR) {
657 /* If request was interrupted, DEITY only knows if the
658 rename actually took place. If the invalidation
659 fails (e.g. some process has CWD under the renamed
660 directory), then there can be inconsistency between
661 the dcache and the real filesystem. Tough luck. */
662 fuse_invalidate_entry(oldent);
664 fuse_invalidate_entry(newent);
670 static int fuse_link(struct dentry *entry, struct inode *newdir,
671 struct dentry *newent)
674 struct fuse_link_in inarg;
675 struct inode *inode = entry->d_inode;
676 struct fuse_conn *fc = get_fuse_conn(inode);
677 struct fuse_req *req = fuse_get_req(fc);
681 memset(&inarg, 0, sizeof(inarg));
682 inarg.oldnodeid = get_node_id(inode);
683 req->in.h.opcode = FUSE_LINK;
685 req->in.args[0].size = sizeof(inarg);
686 req->in.args[0].value = &inarg;
687 req->in.args[1].size = newent->d_name.len + 1;
688 req->in.args[1].value = newent->d_name.name;
689 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
690 /* Contrary to "normal" filesystems it can happen that link
691 makes two "logical" inodes point to the same "physical"
692 inode. We invalidate the attributes of the old one, so it
693 will reflect changes in the backing inode (link count,
696 if (!err || err == -EINTR)
697 fuse_invalidate_attr(inode);
701 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
704 stat->dev = inode->i_sb->s_dev;
705 stat->ino = attr->ino;
706 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
707 stat->nlink = attr->nlink;
708 stat->uid = attr->uid;
709 stat->gid = attr->gid;
710 stat->rdev = inode->i_rdev;
711 stat->atime.tv_sec = attr->atime;
712 stat->atime.tv_nsec = attr->atimensec;
713 stat->mtime.tv_sec = attr->mtime;
714 stat->mtime.tv_nsec = attr->mtimensec;
715 stat->ctime.tv_sec = attr->ctime;
716 stat->ctime.tv_nsec = attr->ctimensec;
717 stat->size = attr->size;
718 stat->blocks = attr->blocks;
719 stat->blksize = (1 << inode->i_blkbits);
722 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
726 struct fuse_getattr_in inarg;
727 struct fuse_attr_out outarg;
728 struct fuse_conn *fc = get_fuse_conn(inode);
729 struct fuse_req *req;
732 req = fuse_get_req(fc);
736 spin_lock(&fc->lock);
737 attr_version = fc->attr_version;
738 spin_unlock(&fc->lock);
740 memset(&inarg, 0, sizeof(inarg));
741 memset(&outarg, 0, sizeof(outarg));
742 /* Directories have separate file-handle space */
743 if (file && S_ISREG(inode->i_mode)) {
744 struct fuse_file *ff = file->private_data;
746 inarg.getattr_flags |= FUSE_GETATTR_FH;
749 req->in.h.opcode = FUSE_GETATTR;
750 req->in.h.nodeid = get_node_id(inode);
752 req->in.args[0].size = sizeof(inarg);
753 req->in.args[0].value = &inarg;
754 req->out.numargs = 1;
756 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
758 req->out.args[0].size = sizeof(outarg);
759 req->out.args[0].value = &outarg;
760 request_send(fc, req);
761 err = req->out.h.error;
762 fuse_put_request(fc, req);
764 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
765 make_bad_inode(inode);
768 fuse_change_attributes(inode, &outarg.attr,
769 attr_timeout(&outarg),
772 fuse_fillattr(inode, &outarg.attr, stat);
779 * Calling into a user-controlled filesystem gives the filesystem
780 * daemon ptrace-like capabilities over the requester process. This
781 * means, that the filesystem daemon is able to record the exact
782 * filesystem operations performed, and can also control the behavior
783 * of the requester process in otherwise impossible ways. For example
784 * it can delay the operation for arbitrary length of time allowing
785 * DoS against the requester.
787 * For this reason only those processes can call into the filesystem,
788 * for which the owner of the mount has ptrace privilege. This
789 * excludes processes started by other users, suid or sgid processes.
791 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
793 if (fc->flags & FUSE_ALLOW_OTHER)
796 if (task->euid == fc->user_id &&
797 task->suid == fc->user_id &&
798 task->uid == fc->user_id &&
799 task->egid == fc->group_id &&
800 task->sgid == fc->group_id &&
801 task->gid == fc->group_id)
807 static int fuse_access(struct inode *inode, int mask)
809 struct fuse_conn *fc = get_fuse_conn(inode);
810 struct fuse_req *req;
811 struct fuse_access_in inarg;
817 req = fuse_get_req(fc);
821 memset(&inarg, 0, sizeof(inarg));
823 req->in.h.opcode = FUSE_ACCESS;
824 req->in.h.nodeid = get_node_id(inode);
826 req->in.args[0].size = sizeof(inarg);
827 req->in.args[0].value = &inarg;
828 request_send(fc, req);
829 err = req->out.h.error;
830 fuse_put_request(fc, req);
831 if (err == -ENOSYS) {
839 * Check permission. The two basic access models of FUSE are:
841 * 1) Local access checking ('default_permissions' mount option) based
842 * on file mode. This is the plain old disk filesystem permission
845 * 2) "Remote" access checking, where server is responsible for
846 * checking permission in each inode operation. An exception to this
847 * is if ->permission() was invoked from sys_access() in which case an
848 * access request is sent. Execute permission is still checked
849 * locally based on file mode.
851 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
853 struct fuse_conn *fc = get_fuse_conn(inode);
854 bool refreshed = false;
857 if (!fuse_allow_task(fc, current))
861 * If attributes are needed, refresh them before proceeding
863 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
864 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
865 struct fuse_inode *fi = get_fuse_inode(inode);
866 if (fi->i_time < get_jiffies_64()) {
867 err = fuse_do_getattr(inode, NULL, NULL);
875 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
876 int err = generic_permission(inode, mask, NULL);
878 /* If permission is denied, try to refresh file
879 attributes. This is also needed, because the root
880 node will at first have no permissions */
881 if (err == -EACCES && !refreshed) {
882 err = fuse_do_getattr(inode, NULL, NULL);
884 err = generic_permission(inode, mask, NULL);
887 /* Note: the opposite of the above test does not
888 exist. So if permissions are revoked this won't be
889 noticed immediately, only after the attribute
890 timeout has expired */
891 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
892 err = fuse_access(inode, mask);
893 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
894 if (!(inode->i_mode & S_IXUGO)) {
898 err = fuse_do_getattr(inode, NULL, NULL);
899 if (!err && !(inode->i_mode & S_IXUGO))
906 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
907 void *dstbuf, filldir_t filldir)
909 while (nbytes >= FUSE_NAME_OFFSET) {
910 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
911 size_t reclen = FUSE_DIRENT_SIZE(dirent);
913 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
918 over = filldir(dstbuf, dirent->name, dirent->namelen,
919 file->f_pos, dirent->ino, dirent->type);
925 file->f_pos = dirent->off;
931 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
936 struct inode *inode = file->f_path.dentry->d_inode;
937 struct fuse_conn *fc = get_fuse_conn(inode);
938 struct fuse_file *ff = file->private_data;
939 struct fuse_req *req;
941 if (is_bad_inode(inode))
944 req = fuse_get_req(fc);
948 page = alloc_page(GFP_KERNEL);
950 fuse_put_request(fc, req);
954 req->pages[0] = page;
955 fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
956 request_send(fc, req);
957 nbytes = req->out.args[0].size;
958 err = req->out.h.error;
959 fuse_put_request(fc, req);
961 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
965 fuse_invalidate_attr(inode); /* atime changed */
969 static char *read_link(struct dentry *dentry)
971 struct inode *inode = dentry->d_inode;
972 struct fuse_conn *fc = get_fuse_conn(inode);
973 struct fuse_req *req = fuse_get_req(fc);
977 return ERR_PTR(PTR_ERR(req));
979 link = (char *) __get_free_page(GFP_KERNEL);
981 link = ERR_PTR(-ENOMEM);
984 req->in.h.opcode = FUSE_READLINK;
985 req->in.h.nodeid = get_node_id(inode);
987 req->out.numargs = 1;
988 req->out.args[0].size = PAGE_SIZE - 1;
989 req->out.args[0].value = link;
990 request_send(fc, req);
991 if (req->out.h.error) {
992 free_page((unsigned long) link);
993 link = ERR_PTR(req->out.h.error);
995 link[req->out.args[0].size] = '\0';
997 fuse_put_request(fc, req);
998 fuse_invalidate_attr(inode); /* atime changed */
1002 static void free_link(char *link)
1005 free_page((unsigned long) link);
1008 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1010 nd_set_link(nd, read_link(dentry));
1014 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1016 free_link(nd_get_link(nd));
1019 static int fuse_dir_open(struct inode *inode, struct file *file)
1021 return fuse_open_common(inode, file, 1);
1024 static int fuse_dir_release(struct inode *inode, struct file *file)
1026 return fuse_release_common(inode, file, 1);
1029 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1031 /* nfsd can call this with no file */
1032 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1035 static bool update_mtime(unsigned ivalid)
1037 /* Always update if mtime is explicitly set */
1038 if (ivalid & ATTR_MTIME_SET)
1041 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1042 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1045 /* In all other cases update */
1049 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1051 unsigned ivalid = iattr->ia_valid;
1053 if (ivalid & ATTR_MODE)
1054 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1055 if (ivalid & ATTR_UID)
1056 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1057 if (ivalid & ATTR_GID)
1058 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1059 if (ivalid & ATTR_SIZE)
1060 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1061 if (ivalid & ATTR_ATIME) {
1062 arg->valid |= FATTR_ATIME;
1063 arg->atime = iattr->ia_atime.tv_sec;
1064 arg->atimensec = iattr->ia_atime.tv_nsec;
1065 if (!(ivalid & ATTR_ATIME_SET))
1066 arg->valid |= FATTR_ATIME_NOW;
1068 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1069 arg->valid |= FATTR_MTIME;
1070 arg->mtime = iattr->ia_mtime.tv_sec;
1071 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1072 if (!(ivalid & ATTR_MTIME_SET))
1073 arg->valid |= FATTR_MTIME_NOW;
1078 * Set attributes, and at the same time refresh them.
1080 * Truncation is slightly complicated, because the 'truncate' request
1081 * may fail, in which case we don't want to touch the mapping.
1082 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1083 * and the actual truncation by hand.
1085 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1088 struct inode *inode = entry->d_inode;
1089 struct fuse_conn *fc = get_fuse_conn(inode);
1090 struct fuse_req *req;
1091 struct fuse_setattr_in inarg;
1092 struct fuse_attr_out outarg;
1095 if (!fuse_allow_task(fc, current))
1098 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1099 err = inode_change_ok(inode, attr);
1104 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1107 if (attr->ia_valid & ATTR_SIZE) {
1108 unsigned long limit;
1109 if (IS_SWAPFILE(inode))
1111 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1112 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1113 send_sig(SIGXFSZ, current, 0);
1118 req = fuse_get_req(fc);
1120 return PTR_ERR(req);
1122 memset(&inarg, 0, sizeof(inarg));
1123 memset(&outarg, 0, sizeof(outarg));
1124 iattr_to_fattr(attr, &inarg);
1126 struct fuse_file *ff = file->private_data;
1127 inarg.valid |= FATTR_FH;
1130 if (attr->ia_valid & ATTR_SIZE) {
1131 /* For mandatory locking in truncate */
1132 inarg.valid |= FATTR_LOCKOWNER;
1133 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1135 req->in.h.opcode = FUSE_SETATTR;
1136 req->in.h.nodeid = get_node_id(inode);
1137 req->in.numargs = 1;
1138 req->in.args[0].size = sizeof(inarg);
1139 req->in.args[0].value = &inarg;
1140 req->out.numargs = 1;
1142 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1144 req->out.args[0].size = sizeof(outarg);
1145 req->out.args[0].value = &outarg;
1146 request_send(fc, req);
1147 err = req->out.h.error;
1148 fuse_put_request(fc, req);
1151 fuse_invalidate_attr(inode);
1155 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1156 make_bad_inode(inode);
1160 fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0);
1164 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1166 if (attr->ia_valid & ATTR_FILE)
1167 return fuse_do_setattr(entry, attr, attr->ia_file);
1169 return fuse_do_setattr(entry, attr, NULL);
1172 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1175 struct inode *inode = entry->d_inode;
1176 struct fuse_inode *fi = get_fuse_inode(inode);
1177 struct fuse_conn *fc = get_fuse_conn(inode);
1180 if (!fuse_allow_task(fc, current))
1183 if (fi->i_time < get_jiffies_64())
1184 err = fuse_do_getattr(inode, stat, NULL);
1187 generic_fillattr(inode, stat);
1188 stat->mode = fi->orig_i_mode;
1194 static int fuse_setxattr(struct dentry *entry, const char *name,
1195 const void *value, size_t size, int flags)
1197 struct inode *inode = entry->d_inode;
1198 struct fuse_conn *fc = get_fuse_conn(inode);
1199 struct fuse_req *req;
1200 struct fuse_setxattr_in inarg;
1203 if (fc->no_setxattr)
1206 req = fuse_get_req(fc);
1208 return PTR_ERR(req);
1210 memset(&inarg, 0, sizeof(inarg));
1212 inarg.flags = flags;
1213 req->in.h.opcode = FUSE_SETXATTR;
1214 req->in.h.nodeid = get_node_id(inode);
1215 req->in.numargs = 3;
1216 req->in.args[0].size = sizeof(inarg);
1217 req->in.args[0].value = &inarg;
1218 req->in.args[1].size = strlen(name) + 1;
1219 req->in.args[1].value = name;
1220 req->in.args[2].size = size;
1221 req->in.args[2].value = value;
1222 request_send(fc, req);
1223 err = req->out.h.error;
1224 fuse_put_request(fc, req);
1225 if (err == -ENOSYS) {
1226 fc->no_setxattr = 1;
1232 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1233 void *value, size_t size)
1235 struct inode *inode = entry->d_inode;
1236 struct fuse_conn *fc = get_fuse_conn(inode);
1237 struct fuse_req *req;
1238 struct fuse_getxattr_in inarg;
1239 struct fuse_getxattr_out outarg;
1242 if (fc->no_getxattr)
1245 req = fuse_get_req(fc);
1247 return PTR_ERR(req);
1249 memset(&inarg, 0, sizeof(inarg));
1251 req->in.h.opcode = FUSE_GETXATTR;
1252 req->in.h.nodeid = get_node_id(inode);
1253 req->in.numargs = 2;
1254 req->in.args[0].size = sizeof(inarg);
1255 req->in.args[0].value = &inarg;
1256 req->in.args[1].size = strlen(name) + 1;
1257 req->in.args[1].value = name;
1258 /* This is really two different operations rolled into one */
1259 req->out.numargs = 1;
1261 req->out.argvar = 1;
1262 req->out.args[0].size = size;
1263 req->out.args[0].value = value;
1265 req->out.args[0].size = sizeof(outarg);
1266 req->out.args[0].value = &outarg;
1268 request_send(fc, req);
1269 ret = req->out.h.error;
1271 ret = size ? req->out.args[0].size : outarg.size;
1273 if (ret == -ENOSYS) {
1274 fc->no_getxattr = 1;
1278 fuse_put_request(fc, req);
1282 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1284 struct inode *inode = entry->d_inode;
1285 struct fuse_conn *fc = get_fuse_conn(inode);
1286 struct fuse_req *req;
1287 struct fuse_getxattr_in inarg;
1288 struct fuse_getxattr_out outarg;
1291 if (!fuse_allow_task(fc, current))
1294 if (fc->no_listxattr)
1297 req = fuse_get_req(fc);
1299 return PTR_ERR(req);
1301 memset(&inarg, 0, sizeof(inarg));
1303 req->in.h.opcode = FUSE_LISTXATTR;
1304 req->in.h.nodeid = get_node_id(inode);
1305 req->in.numargs = 1;
1306 req->in.args[0].size = sizeof(inarg);
1307 req->in.args[0].value = &inarg;
1308 /* This is really two different operations rolled into one */
1309 req->out.numargs = 1;
1311 req->out.argvar = 1;
1312 req->out.args[0].size = size;
1313 req->out.args[0].value = list;
1315 req->out.args[0].size = sizeof(outarg);
1316 req->out.args[0].value = &outarg;
1318 request_send(fc, req);
1319 ret = req->out.h.error;
1321 ret = size ? req->out.args[0].size : outarg.size;
1323 if (ret == -ENOSYS) {
1324 fc->no_listxattr = 1;
1328 fuse_put_request(fc, req);
1332 static int fuse_removexattr(struct dentry *entry, const char *name)
1334 struct inode *inode = entry->d_inode;
1335 struct fuse_conn *fc = get_fuse_conn(inode);
1336 struct fuse_req *req;
1339 if (fc->no_removexattr)
1342 req = fuse_get_req(fc);
1344 return PTR_ERR(req);
1346 req->in.h.opcode = FUSE_REMOVEXATTR;
1347 req->in.h.nodeid = get_node_id(inode);
1348 req->in.numargs = 1;
1349 req->in.args[0].size = strlen(name) + 1;
1350 req->in.args[0].value = name;
1351 request_send(fc, req);
1352 err = req->out.h.error;
1353 fuse_put_request(fc, req);
1354 if (err == -ENOSYS) {
1355 fc->no_removexattr = 1;
1361 static const struct inode_operations fuse_dir_inode_operations = {
1362 .lookup = fuse_lookup,
1363 .mkdir = fuse_mkdir,
1364 .symlink = fuse_symlink,
1365 .unlink = fuse_unlink,
1366 .rmdir = fuse_rmdir,
1367 .rename = fuse_rename,
1369 .setattr = fuse_setattr,
1370 .create = fuse_create,
1371 .mknod = fuse_mknod,
1372 .permission = fuse_permission,
1373 .getattr = fuse_getattr,
1374 .setxattr = fuse_setxattr,
1375 .getxattr = fuse_getxattr,
1376 .listxattr = fuse_listxattr,
1377 .removexattr = fuse_removexattr,
1380 static const struct file_operations fuse_dir_operations = {
1381 .llseek = generic_file_llseek,
1382 .read = generic_read_dir,
1383 .readdir = fuse_readdir,
1384 .open = fuse_dir_open,
1385 .release = fuse_dir_release,
1386 .fsync = fuse_dir_fsync,
1389 static const struct inode_operations fuse_common_inode_operations = {
1390 .setattr = fuse_setattr,
1391 .permission = fuse_permission,
1392 .getattr = fuse_getattr,
1393 .setxattr = fuse_setxattr,
1394 .getxattr = fuse_getxattr,
1395 .listxattr = fuse_listxattr,
1396 .removexattr = fuse_removexattr,
1399 static const struct inode_operations fuse_symlink_inode_operations = {
1400 .setattr = fuse_setattr,
1401 .follow_link = fuse_follow_link,
1402 .put_link = fuse_put_link,
1403 .readlink = generic_readlink,
1404 .getattr = fuse_getattr,
1405 .setxattr = fuse_setxattr,
1406 .getxattr = fuse_getxattr,
1407 .listxattr = fuse_listxattr,
1408 .removexattr = fuse_removexattr,
1411 void fuse_init_common(struct inode *inode)
1413 inode->i_op = &fuse_common_inode_operations;
1416 void fuse_init_dir(struct inode *inode)
1418 inode->i_op = &fuse_dir_inode_operations;
1419 inode->i_fop = &fuse_dir_operations;
1422 void fuse_init_symlink(struct inode *inode)
1424 inode->i_op = &fuse_symlink_inode_operations;