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 req->in.h.opcode = FUSE_LOOKUP;
120 req->in.h.nodeid = get_node_id(dir);
122 req->in.args[0].size = entry->d_name.len + 1;
123 req->in.args[0].value = entry->d_name.name;
124 req->out.numargs = 1;
125 req->out.args[0].size = sizeof(struct fuse_entry_out);
126 req->out.args[0].value = outarg;
130 * Check whether the dentry is still valid
132 * If the entry validity timeout has expired and the dentry is
133 * positive, try to redo the lookup. If the lookup results in a
134 * different inode, then let the VFS invalidate the dentry and redo
135 * the lookup once more. If the lookup results in the same inode,
136 * then refresh the attributes, timeouts and mark the dentry valid.
138 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
140 struct inode *inode = entry->d_inode;
142 if (inode && is_bad_inode(inode))
144 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
146 struct fuse_entry_out outarg;
147 struct fuse_conn *fc;
148 struct fuse_req *req;
149 struct fuse_req *forget_req;
150 struct dentry *parent;
153 /* For negative dentries, always do a fresh lookup */
157 fc = get_fuse_conn(inode);
158 req = fuse_get_req(fc);
162 forget_req = fuse_get_req(fc);
163 if (IS_ERR(forget_req)) {
164 fuse_put_request(fc, req);
168 spin_lock(&fc->lock);
169 attr_version = fc->attr_version;
170 spin_unlock(&fc->lock);
172 parent = dget_parent(entry);
173 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
174 request_send(fc, req);
176 err = req->out.h.error;
177 fuse_put_request(fc, req);
178 /* Zero nodeid is same as -ENOENT */
179 if (!err && !outarg.nodeid)
182 struct fuse_inode *fi = get_fuse_inode(inode);
183 if (outarg.nodeid != get_node_id(inode)) {
184 fuse_send_forget(fc, forget_req,
188 spin_lock(&fc->lock);
190 spin_unlock(&fc->lock);
192 fuse_put_request(fc, forget_req);
193 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
196 fuse_change_attributes(inode, &outarg.attr,
197 entry_attr_timeout(&outarg),
199 fuse_change_entry_timeout(entry, &outarg);
204 static int invalid_nodeid(u64 nodeid)
206 return !nodeid || nodeid == FUSE_ROOT_ID;
209 static struct dentry_operations fuse_dentry_operations = {
210 .d_revalidate = fuse_dentry_revalidate,
213 int fuse_valid_type(int m)
215 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
216 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
220 * Add a directory inode to a dentry, ensuring that no other dentry
221 * refers to this inode. Called with fc->inst_mutex.
223 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
225 struct dentry *alias = d_find_alias(inode);
227 /* This tries to shrink the subtree below alias */
228 fuse_invalidate_entry(alias);
230 if (!list_empty(&inode->i_dentry))
237 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
238 struct nameidata *nd)
241 struct fuse_entry_out outarg;
242 struct inode *inode = NULL;
243 struct fuse_conn *fc = get_fuse_conn(dir);
244 struct fuse_req *req;
245 struct fuse_req *forget_req;
248 if (entry->d_name.len > FUSE_NAME_MAX)
249 return ERR_PTR(-ENAMETOOLONG);
251 req = fuse_get_req(fc);
253 return ERR_PTR(PTR_ERR(req));
255 forget_req = fuse_get_req(fc);
256 if (IS_ERR(forget_req)) {
257 fuse_put_request(fc, req);
258 return ERR_PTR(PTR_ERR(forget_req));
261 spin_lock(&fc->lock);
262 attr_version = fc->attr_version;
263 spin_unlock(&fc->lock);
265 fuse_lookup_init(req, dir, entry, &outarg);
266 request_send(fc, req);
267 err = req->out.h.error;
268 fuse_put_request(fc, req);
269 /* Zero nodeid is same as -ENOENT, but with valid timeout */
270 if (!err && outarg.nodeid &&
271 (invalid_nodeid(outarg.nodeid) ||
272 !fuse_valid_type(outarg.attr.mode)))
274 if (!err && outarg.nodeid) {
275 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
276 &outarg.attr, entry_attr_timeout(&outarg),
279 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
280 return ERR_PTR(-ENOMEM);
283 fuse_put_request(fc, forget_req);
284 if (err && err != -ENOENT)
287 if (inode && S_ISDIR(inode->i_mode)) {
288 mutex_lock(&fc->inst_mutex);
289 err = fuse_d_add_directory(entry, inode);
290 mutex_unlock(&fc->inst_mutex);
298 entry->d_op = &fuse_dentry_operations;
300 fuse_change_entry_timeout(entry, &outarg);
302 fuse_invalidate_entry_cache(entry);
307 * Synchronous release for the case when something goes wrong in CREATE_OPEN
309 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
310 u64 nodeid, int flags)
312 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
313 ff->reserved_req->force = 1;
314 request_send(fc, ff->reserved_req);
315 fuse_put_request(fc, ff->reserved_req);
320 * Atomic create+open operation
322 * If the filesystem doesn't support this, then fall back to separate
323 * 'mknod' + 'open' requests.
325 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
326 struct nameidata *nd)
330 struct fuse_conn *fc = get_fuse_conn(dir);
331 struct fuse_req *req;
332 struct fuse_req *forget_req;
333 struct fuse_open_in inarg;
334 struct fuse_open_out outopen;
335 struct fuse_entry_out outentry;
336 struct fuse_file *ff;
338 int flags = nd->intent.open.flags - 1;
343 forget_req = fuse_get_req(fc);
344 if (IS_ERR(forget_req))
345 return PTR_ERR(forget_req);
347 req = fuse_get_req(fc);
350 goto out_put_forget_req;
353 ff = fuse_file_alloc();
355 goto out_put_request;
358 memset(&inarg, 0, sizeof(inarg));
361 req->in.h.opcode = FUSE_CREATE;
362 req->in.h.nodeid = get_node_id(dir);
364 req->in.args[0].size = sizeof(inarg);
365 req->in.args[0].value = &inarg;
366 req->in.args[1].size = entry->d_name.len + 1;
367 req->in.args[1].value = entry->d_name.name;
368 req->out.numargs = 2;
369 req->out.args[0].size = sizeof(outentry);
370 req->out.args[0].value = &outentry;
371 req->out.args[1].size = sizeof(outopen);
372 req->out.args[1].value = &outopen;
373 request_send(fc, req);
374 err = req->out.h.error;
382 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
385 fuse_put_request(fc, req);
386 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
387 &outentry.attr, entry_attr_timeout(&outentry), 0);
389 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
391 fuse_sync_release(fc, ff, outentry.nodeid, flags);
392 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
395 fuse_put_request(fc, forget_req);
396 d_instantiate(entry, inode);
397 fuse_change_entry_timeout(entry, &outentry);
398 file = lookup_instantiate_filp(nd, entry, generic_file_open);
401 fuse_sync_release(fc, ff, outentry.nodeid, flags);
402 return PTR_ERR(file);
404 fuse_finish_open(inode, file, ff, &outopen);
410 fuse_put_request(fc, req);
412 fuse_put_request(fc, forget_req);
417 * Code shared between mknod, mkdir, symlink and link
419 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
420 struct inode *dir, struct dentry *entry,
423 struct fuse_entry_out outarg;
426 struct fuse_req *forget_req;
428 forget_req = fuse_get_req(fc);
429 if (IS_ERR(forget_req)) {
430 fuse_put_request(fc, req);
431 return PTR_ERR(forget_req);
434 req->in.h.nodeid = get_node_id(dir);
435 req->out.numargs = 1;
436 req->out.args[0].size = sizeof(outarg);
437 req->out.args[0].value = &outarg;
438 request_send(fc, req);
439 err = req->out.h.error;
440 fuse_put_request(fc, req);
442 goto out_put_forget_req;
445 if (invalid_nodeid(outarg.nodeid))
446 goto out_put_forget_req;
448 if ((outarg.attr.mode ^ mode) & S_IFMT)
449 goto out_put_forget_req;
451 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
452 &outarg.attr, entry_attr_timeout(&outarg), 0);
454 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
457 fuse_put_request(fc, forget_req);
459 if (S_ISDIR(inode->i_mode)) {
460 struct dentry *alias;
461 mutex_lock(&fc->inst_mutex);
462 alias = d_find_alias(inode);
464 /* New directory must have moved since mkdir */
465 mutex_unlock(&fc->inst_mutex);
470 d_instantiate(entry, inode);
471 mutex_unlock(&fc->inst_mutex);
473 d_instantiate(entry, inode);
475 fuse_change_entry_timeout(entry, &outarg);
476 fuse_invalidate_attr(dir);
480 fuse_put_request(fc, forget_req);
484 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
487 struct fuse_mknod_in inarg;
488 struct fuse_conn *fc = get_fuse_conn(dir);
489 struct fuse_req *req = fuse_get_req(fc);
493 memset(&inarg, 0, sizeof(inarg));
495 inarg.rdev = new_encode_dev(rdev);
496 req->in.h.opcode = FUSE_MKNOD;
498 req->in.args[0].size = sizeof(inarg);
499 req->in.args[0].value = &inarg;
500 req->in.args[1].size = entry->d_name.len + 1;
501 req->in.args[1].value = entry->d_name.name;
502 return create_new_entry(fc, req, dir, entry, mode);
505 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
506 struct nameidata *nd)
508 if (nd && (nd->flags & LOOKUP_OPEN)) {
509 int err = fuse_create_open(dir, entry, mode, nd);
512 /* Fall back on mknod */
514 return fuse_mknod(dir, entry, mode, 0);
517 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
519 struct fuse_mkdir_in inarg;
520 struct fuse_conn *fc = get_fuse_conn(dir);
521 struct fuse_req *req = fuse_get_req(fc);
525 memset(&inarg, 0, sizeof(inarg));
527 req->in.h.opcode = FUSE_MKDIR;
529 req->in.args[0].size = sizeof(inarg);
530 req->in.args[0].value = &inarg;
531 req->in.args[1].size = entry->d_name.len + 1;
532 req->in.args[1].value = entry->d_name.name;
533 return create_new_entry(fc, req, dir, entry, S_IFDIR);
536 static int fuse_symlink(struct inode *dir, struct dentry *entry,
539 struct fuse_conn *fc = get_fuse_conn(dir);
540 unsigned len = strlen(link) + 1;
541 struct fuse_req *req = fuse_get_req(fc);
545 req->in.h.opcode = FUSE_SYMLINK;
547 req->in.args[0].size = entry->d_name.len + 1;
548 req->in.args[0].value = entry->d_name.name;
549 req->in.args[1].size = len;
550 req->in.args[1].value = link;
551 return create_new_entry(fc, req, dir, entry, S_IFLNK);
554 static int fuse_unlink(struct inode *dir, struct dentry *entry)
557 struct fuse_conn *fc = get_fuse_conn(dir);
558 struct fuse_req *req = fuse_get_req(fc);
562 req->in.h.opcode = FUSE_UNLINK;
563 req->in.h.nodeid = get_node_id(dir);
565 req->in.args[0].size = entry->d_name.len + 1;
566 req->in.args[0].value = entry->d_name.name;
567 request_send(fc, req);
568 err = req->out.h.error;
569 fuse_put_request(fc, req);
571 struct inode *inode = entry->d_inode;
573 /* Set nlink to zero so the inode can be cleared, if
574 the inode does have more links this will be
575 discovered at the next lookup/getattr */
577 fuse_invalidate_attr(inode);
578 fuse_invalidate_attr(dir);
579 fuse_invalidate_entry_cache(entry);
580 } else if (err == -EINTR)
581 fuse_invalidate_entry(entry);
585 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
588 struct fuse_conn *fc = get_fuse_conn(dir);
589 struct fuse_req *req = fuse_get_req(fc);
593 req->in.h.opcode = FUSE_RMDIR;
594 req->in.h.nodeid = get_node_id(dir);
596 req->in.args[0].size = entry->d_name.len + 1;
597 req->in.args[0].value = entry->d_name.name;
598 request_send(fc, req);
599 err = req->out.h.error;
600 fuse_put_request(fc, req);
602 clear_nlink(entry->d_inode);
603 fuse_invalidate_attr(dir);
604 fuse_invalidate_entry_cache(entry);
605 } else if (err == -EINTR)
606 fuse_invalidate_entry(entry);
610 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
611 struct inode *newdir, struct dentry *newent)
614 struct fuse_rename_in inarg;
615 struct fuse_conn *fc = get_fuse_conn(olddir);
616 struct fuse_req *req = fuse_get_req(fc);
620 memset(&inarg, 0, sizeof(inarg));
621 inarg.newdir = get_node_id(newdir);
622 req->in.h.opcode = FUSE_RENAME;
623 req->in.h.nodeid = get_node_id(olddir);
625 req->in.args[0].size = sizeof(inarg);
626 req->in.args[0].value = &inarg;
627 req->in.args[1].size = oldent->d_name.len + 1;
628 req->in.args[1].value = oldent->d_name.name;
629 req->in.args[2].size = newent->d_name.len + 1;
630 req->in.args[2].value = newent->d_name.name;
631 request_send(fc, req);
632 err = req->out.h.error;
633 fuse_put_request(fc, req);
635 fuse_invalidate_attr(olddir);
636 if (olddir != newdir)
637 fuse_invalidate_attr(newdir);
639 /* newent will end up negative */
641 fuse_invalidate_entry_cache(newent);
642 } else if (err == -EINTR) {
643 /* If request was interrupted, DEITY only knows if the
644 rename actually took place. If the invalidation
645 fails (e.g. some process has CWD under the renamed
646 directory), then there can be inconsistency between
647 the dcache and the real filesystem. Tough luck. */
648 fuse_invalidate_entry(oldent);
650 fuse_invalidate_entry(newent);
656 static int fuse_link(struct dentry *entry, struct inode *newdir,
657 struct dentry *newent)
660 struct fuse_link_in inarg;
661 struct inode *inode = entry->d_inode;
662 struct fuse_conn *fc = get_fuse_conn(inode);
663 struct fuse_req *req = fuse_get_req(fc);
667 memset(&inarg, 0, sizeof(inarg));
668 inarg.oldnodeid = get_node_id(inode);
669 req->in.h.opcode = FUSE_LINK;
671 req->in.args[0].size = sizeof(inarg);
672 req->in.args[0].value = &inarg;
673 req->in.args[1].size = newent->d_name.len + 1;
674 req->in.args[1].value = newent->d_name.name;
675 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
676 /* Contrary to "normal" filesystems it can happen that link
677 makes two "logical" inodes point to the same "physical"
678 inode. We invalidate the attributes of the old one, so it
679 will reflect changes in the backing inode (link count,
682 if (!err || err == -EINTR)
683 fuse_invalidate_attr(inode);
687 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
690 stat->dev = inode->i_sb->s_dev;
691 stat->ino = attr->ino;
692 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
693 stat->nlink = attr->nlink;
694 stat->uid = attr->uid;
695 stat->gid = attr->gid;
696 stat->rdev = inode->i_rdev;
697 stat->atime.tv_sec = attr->atime;
698 stat->atime.tv_nsec = attr->atimensec;
699 stat->mtime.tv_sec = attr->mtime;
700 stat->mtime.tv_nsec = attr->mtimensec;
701 stat->ctime.tv_sec = attr->ctime;
702 stat->ctime.tv_nsec = attr->ctimensec;
703 stat->size = attr->size;
704 stat->blocks = attr->blocks;
705 stat->blksize = (1 << inode->i_blkbits);
708 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
712 struct fuse_getattr_in inarg;
713 struct fuse_attr_out outarg;
714 struct fuse_conn *fc = get_fuse_conn(inode);
715 struct fuse_req *req;
718 req = fuse_get_req(fc);
722 spin_lock(&fc->lock);
723 attr_version = fc->attr_version;
724 spin_unlock(&fc->lock);
726 memset(&inarg, 0, sizeof(inarg));
727 /* Directories have separate file-handle space */
728 if (file && S_ISREG(inode->i_mode)) {
729 struct fuse_file *ff = file->private_data;
731 inarg.getattr_flags |= FUSE_GETATTR_FH;
734 req->in.h.opcode = FUSE_GETATTR;
735 req->in.h.nodeid = get_node_id(inode);
737 req->in.args[0].size = sizeof(inarg);
738 req->in.args[0].value = &inarg;
739 req->out.numargs = 1;
740 req->out.args[0].size = sizeof(outarg);
741 req->out.args[0].value = &outarg;
742 request_send(fc, req);
743 err = req->out.h.error;
744 fuse_put_request(fc, req);
746 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
747 make_bad_inode(inode);
750 fuse_change_attributes(inode, &outarg.attr,
751 attr_timeout(&outarg),
754 fuse_fillattr(inode, &outarg.attr, stat);
761 * Calling into a user-controlled filesystem gives the filesystem
762 * daemon ptrace-like capabilities over the requester process. This
763 * means, that the filesystem daemon is able to record the exact
764 * filesystem operations performed, and can also control the behavior
765 * of the requester process in otherwise impossible ways. For example
766 * it can delay the operation for arbitrary length of time allowing
767 * DoS against the requester.
769 * For this reason only those processes can call into the filesystem,
770 * for which the owner of the mount has ptrace privilege. This
771 * excludes processes started by other users, suid or sgid processes.
773 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
775 if (fc->flags & FUSE_ALLOW_OTHER)
778 if (task->euid == fc->user_id &&
779 task->suid == fc->user_id &&
780 task->uid == fc->user_id &&
781 task->egid == fc->group_id &&
782 task->sgid == fc->group_id &&
783 task->gid == fc->group_id)
789 static int fuse_access(struct inode *inode, int mask)
791 struct fuse_conn *fc = get_fuse_conn(inode);
792 struct fuse_req *req;
793 struct fuse_access_in inarg;
799 req = fuse_get_req(fc);
803 memset(&inarg, 0, sizeof(inarg));
805 req->in.h.opcode = FUSE_ACCESS;
806 req->in.h.nodeid = get_node_id(inode);
808 req->in.args[0].size = sizeof(inarg);
809 req->in.args[0].value = &inarg;
810 request_send(fc, req);
811 err = req->out.h.error;
812 fuse_put_request(fc, req);
813 if (err == -ENOSYS) {
821 * Check permission. The two basic access models of FUSE are:
823 * 1) Local access checking ('default_permissions' mount option) based
824 * on file mode. This is the plain old disk filesystem permission
827 * 2) "Remote" access checking, where server is responsible for
828 * checking permission in each inode operation. An exception to this
829 * is if ->permission() was invoked from sys_access() in which case an
830 * access request is sent. Execute permission is still checked
831 * locally based on file mode.
833 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
835 struct fuse_conn *fc = get_fuse_conn(inode);
836 bool refreshed = false;
839 if (!fuse_allow_task(fc, current))
843 * If attributes are needed, refresh them before proceeding
845 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
846 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
847 struct fuse_inode *fi = get_fuse_inode(inode);
848 if (fi->i_time < get_jiffies_64()) {
849 err = fuse_do_getattr(inode, NULL, NULL);
857 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
858 int err = generic_permission(inode, mask, NULL);
860 /* If permission is denied, try to refresh file
861 attributes. This is also needed, because the root
862 node will at first have no permissions */
863 if (err == -EACCES && !refreshed) {
864 err = fuse_do_getattr(inode, NULL, NULL);
866 err = generic_permission(inode, mask, NULL);
869 /* Note: the opposite of the above test does not
870 exist. So if permissions are revoked this won't be
871 noticed immediately, only after the attribute
872 timeout has expired */
873 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
874 err = fuse_access(inode, mask);
875 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
876 if (!(inode->i_mode & S_IXUGO)) {
880 err = fuse_do_getattr(inode, NULL, NULL);
881 if (!err && !(inode->i_mode & S_IXUGO))
888 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
889 void *dstbuf, filldir_t filldir)
891 while (nbytes >= FUSE_NAME_OFFSET) {
892 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
893 size_t reclen = FUSE_DIRENT_SIZE(dirent);
895 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
900 over = filldir(dstbuf, dirent->name, dirent->namelen,
901 file->f_pos, dirent->ino, dirent->type);
907 file->f_pos = dirent->off;
913 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
918 struct inode *inode = file->f_path.dentry->d_inode;
919 struct fuse_conn *fc = get_fuse_conn(inode);
920 struct fuse_file *ff = file->private_data;
921 struct fuse_req *req;
923 if (is_bad_inode(inode))
926 req = fuse_get_req(fc);
930 page = alloc_page(GFP_KERNEL);
932 fuse_put_request(fc, req);
936 req->pages[0] = page;
937 fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
938 request_send(fc, req);
939 nbytes = req->out.args[0].size;
940 err = req->out.h.error;
941 fuse_put_request(fc, req);
943 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
947 fuse_invalidate_attr(inode); /* atime changed */
951 static char *read_link(struct dentry *dentry)
953 struct inode *inode = dentry->d_inode;
954 struct fuse_conn *fc = get_fuse_conn(inode);
955 struct fuse_req *req = fuse_get_req(fc);
959 return ERR_PTR(PTR_ERR(req));
961 link = (char *) __get_free_page(GFP_KERNEL);
963 link = ERR_PTR(-ENOMEM);
966 req->in.h.opcode = FUSE_READLINK;
967 req->in.h.nodeid = get_node_id(inode);
969 req->out.numargs = 1;
970 req->out.args[0].size = PAGE_SIZE - 1;
971 req->out.args[0].value = link;
972 request_send(fc, req);
973 if (req->out.h.error) {
974 free_page((unsigned long) link);
975 link = ERR_PTR(req->out.h.error);
977 link[req->out.args[0].size] = '\0';
979 fuse_put_request(fc, req);
980 fuse_invalidate_attr(inode); /* atime changed */
984 static void free_link(char *link)
987 free_page((unsigned long) link);
990 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
992 nd_set_link(nd, read_link(dentry));
996 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
998 free_link(nd_get_link(nd));
1001 static int fuse_dir_open(struct inode *inode, struct file *file)
1003 return fuse_open_common(inode, file, 1);
1006 static int fuse_dir_release(struct inode *inode, struct file *file)
1008 return fuse_release_common(inode, file, 1);
1011 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1013 /* nfsd can call this with no file */
1014 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1017 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1019 unsigned ivalid = iattr->ia_valid;
1021 if (ivalid & ATTR_MODE)
1022 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1023 if (ivalid & ATTR_UID)
1024 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1025 if (ivalid & ATTR_GID)
1026 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1027 if (ivalid & ATTR_SIZE)
1028 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1029 /* You can only _set_ these together (they may change by themselves) */
1030 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
1031 arg->valid |= FATTR_ATIME | FATTR_MTIME;
1032 arg->atime = iattr->ia_atime.tv_sec;
1033 arg->mtime = iattr->ia_mtime.tv_sec;
1038 * Set attributes, and at the same time refresh them.
1040 * Truncation is slightly complicated, because the 'truncate' request
1041 * may fail, in which case we don't want to touch the mapping.
1042 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1043 * and the actual truncation by hand.
1045 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1048 struct inode *inode = entry->d_inode;
1049 struct fuse_conn *fc = get_fuse_conn(inode);
1050 struct fuse_req *req;
1051 struct fuse_setattr_in inarg;
1052 struct fuse_attr_out outarg;
1055 if (!fuse_allow_task(fc, current))
1058 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1059 err = inode_change_ok(inode, attr);
1064 if (attr->ia_valid & ATTR_SIZE) {
1065 unsigned long limit;
1066 if (IS_SWAPFILE(inode))
1068 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1069 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1070 send_sig(SIGXFSZ, current, 0);
1075 req = fuse_get_req(fc);
1077 return PTR_ERR(req);
1079 memset(&inarg, 0, sizeof(inarg));
1080 iattr_to_fattr(attr, &inarg);
1082 struct fuse_file *ff = file->private_data;
1083 inarg.valid |= FATTR_FH;
1086 req->in.h.opcode = FUSE_SETATTR;
1087 req->in.h.nodeid = get_node_id(inode);
1088 req->in.numargs = 1;
1089 req->in.args[0].size = sizeof(inarg);
1090 req->in.args[0].value = &inarg;
1091 req->out.numargs = 1;
1092 req->out.args[0].size = sizeof(outarg);
1093 req->out.args[0].value = &outarg;
1094 request_send(fc, req);
1095 err = req->out.h.error;
1096 fuse_put_request(fc, req);
1099 fuse_invalidate_attr(inode);
1103 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1104 make_bad_inode(inode);
1108 fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0);
1112 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1114 if (attr->ia_valid & ATTR_FILE)
1115 return fuse_do_setattr(entry, attr, attr->ia_file);
1117 return fuse_do_setattr(entry, attr, NULL);
1120 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1123 struct inode *inode = entry->d_inode;
1124 struct fuse_inode *fi = get_fuse_inode(inode);
1125 struct fuse_conn *fc = get_fuse_conn(inode);
1128 if (!fuse_allow_task(fc, current))
1131 if (fi->i_time < get_jiffies_64())
1132 err = fuse_do_getattr(inode, stat, NULL);
1135 generic_fillattr(inode, stat);
1136 stat->mode = fi->orig_i_mode;
1142 static int fuse_setxattr(struct dentry *entry, const char *name,
1143 const void *value, size_t size, int flags)
1145 struct inode *inode = entry->d_inode;
1146 struct fuse_conn *fc = get_fuse_conn(inode);
1147 struct fuse_req *req;
1148 struct fuse_setxattr_in inarg;
1151 if (fc->no_setxattr)
1154 req = fuse_get_req(fc);
1156 return PTR_ERR(req);
1158 memset(&inarg, 0, sizeof(inarg));
1160 inarg.flags = flags;
1161 req->in.h.opcode = FUSE_SETXATTR;
1162 req->in.h.nodeid = get_node_id(inode);
1163 req->in.numargs = 3;
1164 req->in.args[0].size = sizeof(inarg);
1165 req->in.args[0].value = &inarg;
1166 req->in.args[1].size = strlen(name) + 1;
1167 req->in.args[1].value = name;
1168 req->in.args[2].size = size;
1169 req->in.args[2].value = value;
1170 request_send(fc, req);
1171 err = req->out.h.error;
1172 fuse_put_request(fc, req);
1173 if (err == -ENOSYS) {
1174 fc->no_setxattr = 1;
1180 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1181 void *value, size_t size)
1183 struct inode *inode = entry->d_inode;
1184 struct fuse_conn *fc = get_fuse_conn(inode);
1185 struct fuse_req *req;
1186 struct fuse_getxattr_in inarg;
1187 struct fuse_getxattr_out outarg;
1190 if (fc->no_getxattr)
1193 req = fuse_get_req(fc);
1195 return PTR_ERR(req);
1197 memset(&inarg, 0, sizeof(inarg));
1199 req->in.h.opcode = FUSE_GETXATTR;
1200 req->in.h.nodeid = get_node_id(inode);
1201 req->in.numargs = 2;
1202 req->in.args[0].size = sizeof(inarg);
1203 req->in.args[0].value = &inarg;
1204 req->in.args[1].size = strlen(name) + 1;
1205 req->in.args[1].value = name;
1206 /* This is really two different operations rolled into one */
1207 req->out.numargs = 1;
1209 req->out.argvar = 1;
1210 req->out.args[0].size = size;
1211 req->out.args[0].value = value;
1213 req->out.args[0].size = sizeof(outarg);
1214 req->out.args[0].value = &outarg;
1216 request_send(fc, req);
1217 ret = req->out.h.error;
1219 ret = size ? req->out.args[0].size : outarg.size;
1221 if (ret == -ENOSYS) {
1222 fc->no_getxattr = 1;
1226 fuse_put_request(fc, req);
1230 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1232 struct inode *inode = entry->d_inode;
1233 struct fuse_conn *fc = get_fuse_conn(inode);
1234 struct fuse_req *req;
1235 struct fuse_getxattr_in inarg;
1236 struct fuse_getxattr_out outarg;
1239 if (!fuse_allow_task(fc, current))
1242 if (fc->no_listxattr)
1245 req = fuse_get_req(fc);
1247 return PTR_ERR(req);
1249 memset(&inarg, 0, sizeof(inarg));
1251 req->in.h.opcode = FUSE_LISTXATTR;
1252 req->in.h.nodeid = get_node_id(inode);
1253 req->in.numargs = 1;
1254 req->in.args[0].size = sizeof(inarg);
1255 req->in.args[0].value = &inarg;
1256 /* This is really two different operations rolled into one */
1257 req->out.numargs = 1;
1259 req->out.argvar = 1;
1260 req->out.args[0].size = size;
1261 req->out.args[0].value = list;
1263 req->out.args[0].size = sizeof(outarg);
1264 req->out.args[0].value = &outarg;
1266 request_send(fc, req);
1267 ret = req->out.h.error;
1269 ret = size ? req->out.args[0].size : outarg.size;
1271 if (ret == -ENOSYS) {
1272 fc->no_listxattr = 1;
1276 fuse_put_request(fc, req);
1280 static int fuse_removexattr(struct dentry *entry, const char *name)
1282 struct inode *inode = entry->d_inode;
1283 struct fuse_conn *fc = get_fuse_conn(inode);
1284 struct fuse_req *req;
1287 if (fc->no_removexattr)
1290 req = fuse_get_req(fc);
1292 return PTR_ERR(req);
1294 req->in.h.opcode = FUSE_REMOVEXATTR;
1295 req->in.h.nodeid = get_node_id(inode);
1296 req->in.numargs = 1;
1297 req->in.args[0].size = strlen(name) + 1;
1298 req->in.args[0].value = name;
1299 request_send(fc, req);
1300 err = req->out.h.error;
1301 fuse_put_request(fc, req);
1302 if (err == -ENOSYS) {
1303 fc->no_removexattr = 1;
1309 static const struct inode_operations fuse_dir_inode_operations = {
1310 .lookup = fuse_lookup,
1311 .mkdir = fuse_mkdir,
1312 .symlink = fuse_symlink,
1313 .unlink = fuse_unlink,
1314 .rmdir = fuse_rmdir,
1315 .rename = fuse_rename,
1317 .setattr = fuse_setattr,
1318 .create = fuse_create,
1319 .mknod = fuse_mknod,
1320 .permission = fuse_permission,
1321 .getattr = fuse_getattr,
1322 .setxattr = fuse_setxattr,
1323 .getxattr = fuse_getxattr,
1324 .listxattr = fuse_listxattr,
1325 .removexattr = fuse_removexattr,
1328 static const struct file_operations fuse_dir_operations = {
1329 .llseek = generic_file_llseek,
1330 .read = generic_read_dir,
1331 .readdir = fuse_readdir,
1332 .open = fuse_dir_open,
1333 .release = fuse_dir_release,
1334 .fsync = fuse_dir_fsync,
1337 static const struct inode_operations fuse_common_inode_operations = {
1338 .setattr = fuse_setattr,
1339 .permission = fuse_permission,
1340 .getattr = fuse_getattr,
1341 .setxattr = fuse_setxattr,
1342 .getxattr = fuse_getxattr,
1343 .listxattr = fuse_listxattr,
1344 .removexattr = fuse_removexattr,
1347 static const struct inode_operations fuse_symlink_inode_operations = {
1348 .setattr = fuse_setattr,
1349 .follow_link = fuse_follow_link,
1350 .put_link = fuse_put_link,
1351 .readlink = generic_readlink,
1352 .getattr = fuse_getattr,
1353 .setxattr = fuse_setxattr,
1354 .getxattr = fuse_getxattr,
1355 .listxattr = fuse_listxattr,
1356 .removexattr = fuse_removexattr,
1359 void fuse_init_common(struct inode *inode)
1361 inode->i_op = &fuse_common_inode_operations;
1364 void fuse_init_dir(struct inode *inode)
1366 inode->i_op = &fuse_dir_inode_operations;
1367 inode->i_fop = &fuse_dir_operations;
1370 void fuse_init_symlink(struct inode *inode)
1372 inode->i_op = &fuse_symlink_inode_operations;