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_timeout(struct dentry *entry, struct fuse_entry_out *o)
68 fuse_dentry_settime(entry,
69 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
71 get_fuse_inode(entry->d_inode)->i_time =
72 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
76 * Mark the attributes as stale, so that at the next call to
77 * ->getattr() they will be fetched from userspace
79 void fuse_invalidate_attr(struct inode *inode)
81 get_fuse_inode(inode)->i_time = 0;
85 * Just mark the entry as stale, so that a next attempt to look it up
86 * will result in a new lookup call to userspace
88 * This is called when a dentry is about to become negative and the
89 * timeout is unknown (unlink, rmdir, rename and in some cases
92 static void fuse_invalidate_entry_cache(struct dentry *entry)
94 fuse_dentry_settime(entry, 0);
98 * Same as fuse_invalidate_entry_cache(), but also try to remove the
99 * dentry from the hash
101 static void fuse_invalidate_entry(struct dentry *entry)
104 fuse_invalidate_entry_cache(entry);
107 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
108 struct dentry *entry,
109 struct fuse_entry_out *outarg)
111 req->in.h.opcode = FUSE_LOOKUP;
112 req->in.h.nodeid = get_node_id(dir);
114 req->in.args[0].size = entry->d_name.len + 1;
115 req->in.args[0].value = entry->d_name.name;
116 req->out.numargs = 1;
117 req->out.args[0].size = sizeof(struct fuse_entry_out);
118 req->out.args[0].value = outarg;
122 * Check whether the dentry is still valid
124 * If the entry validity timeout has expired and the dentry is
125 * positive, try to redo the lookup. If the lookup results in a
126 * different inode, then let the VFS invalidate the dentry and redo
127 * the lookup once more. If the lookup results in the same inode,
128 * then refresh the attributes, timeouts and mark the dentry valid.
130 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
132 struct inode *inode = entry->d_inode;
134 if (inode && is_bad_inode(inode))
136 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
138 struct fuse_entry_out outarg;
139 struct fuse_conn *fc;
140 struct fuse_req *req;
142 /* Doesn't hurt to "reset" the validity timeout */
143 fuse_invalidate_entry_cache(entry);
145 /* For negative dentries, always do a fresh lookup */
149 fc = get_fuse_conn(inode);
150 req = fuse_get_req(fc);
154 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
155 request_send(fc, req);
156 err = req->out.h.error;
157 /* Zero nodeid is same as -ENOENT */
158 if (!err && !outarg.nodeid)
161 struct fuse_inode *fi = get_fuse_inode(inode);
162 if (outarg.nodeid != get_node_id(inode)) {
163 fuse_send_forget(fc, req, outarg.nodeid, 1);
166 spin_lock(&fc->lock);
168 spin_unlock(&fc->lock);
170 fuse_put_request(fc, req);
171 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
174 fuse_change_attributes(inode, &outarg.attr);
175 fuse_change_timeout(entry, &outarg);
180 static int invalid_nodeid(u64 nodeid)
182 return !nodeid || nodeid == FUSE_ROOT_ID;
185 static struct dentry_operations fuse_dentry_operations = {
186 .d_revalidate = fuse_dentry_revalidate,
189 static int valid_mode(int m)
191 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
192 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
196 * Add a directory inode to a dentry, ensuring that no other dentry
197 * refers to this inode. Called with fc->inst_mutex.
199 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
201 struct dentry *alias = d_find_alias(inode);
203 /* This tries to shrink the subtree below alias */
204 fuse_invalidate_entry(alias);
206 if (!list_empty(&inode->i_dentry))
213 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
214 struct nameidata *nd)
217 struct fuse_entry_out outarg;
218 struct inode *inode = NULL;
219 struct fuse_conn *fc = get_fuse_conn(dir);
220 struct fuse_req *req;
222 if (entry->d_name.len > FUSE_NAME_MAX)
223 return ERR_PTR(-ENAMETOOLONG);
225 req = fuse_get_req(fc);
227 return ERR_PTR(PTR_ERR(req));
229 fuse_lookup_init(req, dir, entry, &outarg);
230 request_send(fc, req);
231 err = req->out.h.error;
232 /* Zero nodeid is same as -ENOENT, but with valid timeout */
233 if (!err && outarg.nodeid &&
234 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
236 if (!err && outarg.nodeid) {
237 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
240 fuse_send_forget(fc, req, outarg.nodeid, 1);
241 return ERR_PTR(-ENOMEM);
244 fuse_put_request(fc, req);
245 if (err && err != -ENOENT)
248 if (inode && S_ISDIR(inode->i_mode)) {
249 mutex_lock(&fc->inst_mutex);
250 err = fuse_d_add_directory(entry, inode);
251 mutex_unlock(&fc->inst_mutex);
259 entry->d_op = &fuse_dentry_operations;
261 fuse_change_timeout(entry, &outarg);
263 fuse_invalidate_entry_cache(entry);
268 * Synchronous release for the case when something goes wrong in CREATE_OPEN
270 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
271 u64 nodeid, int flags)
273 struct fuse_req *req;
275 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
277 request_send(fc, req);
278 fuse_put_request(fc, req);
282 * Atomic create+open operation
284 * If the filesystem doesn't support this, then fall back to separate
285 * 'mknod' + 'open' requests.
287 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
288 struct nameidata *nd)
292 struct fuse_conn *fc = get_fuse_conn(dir);
293 struct fuse_req *req;
294 struct fuse_req *forget_req;
295 struct fuse_open_in inarg;
296 struct fuse_open_out outopen;
297 struct fuse_entry_out outentry;
298 struct fuse_file *ff;
300 int flags = nd->intent.open.flags - 1;
305 forget_req = fuse_get_req(fc);
306 if (IS_ERR(forget_req))
307 return PTR_ERR(forget_req);
309 req = fuse_get_req(fc);
312 goto out_put_forget_req;
315 ff = fuse_file_alloc();
317 goto out_put_request;
320 memset(&inarg, 0, sizeof(inarg));
323 req->in.h.opcode = FUSE_CREATE;
324 req->in.h.nodeid = get_node_id(dir);
326 req->in.args[0].size = sizeof(inarg);
327 req->in.args[0].value = &inarg;
328 req->in.args[1].size = entry->d_name.len + 1;
329 req->in.args[1].value = entry->d_name.name;
330 req->out.numargs = 2;
331 req->out.args[0].size = sizeof(outentry);
332 req->out.args[0].value = &outentry;
333 req->out.args[1].size = sizeof(outopen);
334 req->out.args[1].value = &outopen;
335 request_send(fc, req);
336 err = req->out.h.error;
344 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
347 fuse_put_request(fc, req);
348 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
351 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
353 fuse_sync_release(fc, ff, outentry.nodeid, flags);
354 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
357 fuse_put_request(fc, forget_req);
358 d_instantiate(entry, inode);
359 fuse_change_timeout(entry, &outentry);
360 file = lookup_instantiate_filp(nd, entry, generic_file_open);
363 fuse_sync_release(fc, ff, outentry.nodeid, flags);
364 return PTR_ERR(file);
366 fuse_finish_open(inode, file, ff, &outopen);
372 fuse_put_request(fc, req);
374 fuse_put_request(fc, forget_req);
379 * Code shared between mknod, mkdir, symlink and link
381 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
382 struct inode *dir, struct dentry *entry,
385 struct fuse_entry_out outarg;
389 req->in.h.nodeid = get_node_id(dir);
390 req->out.numargs = 1;
391 req->out.args[0].size = sizeof(outarg);
392 req->out.args[0].value = &outarg;
393 request_send(fc, req);
394 err = req->out.h.error;
396 fuse_put_request(fc, req);
400 if (invalid_nodeid(outarg.nodeid))
401 goto out_put_request;
403 if ((outarg.attr.mode ^ mode) & S_IFMT)
404 goto out_put_request;
406 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
409 fuse_send_forget(fc, req, outarg.nodeid, 1);
412 fuse_put_request(fc, req);
414 if (S_ISDIR(inode->i_mode)) {
415 struct dentry *alias;
416 mutex_lock(&fc->inst_mutex);
417 alias = d_find_alias(inode);
419 /* New directory must have moved since mkdir */
420 mutex_unlock(&fc->inst_mutex);
425 d_instantiate(entry, inode);
426 mutex_unlock(&fc->inst_mutex);
428 d_instantiate(entry, inode);
430 fuse_change_timeout(entry, &outarg);
431 fuse_invalidate_attr(dir);
435 fuse_put_request(fc, req);
439 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
442 struct fuse_mknod_in inarg;
443 struct fuse_conn *fc = get_fuse_conn(dir);
444 struct fuse_req *req = fuse_get_req(fc);
448 memset(&inarg, 0, sizeof(inarg));
450 inarg.rdev = new_encode_dev(rdev);
451 req->in.h.opcode = FUSE_MKNOD;
453 req->in.args[0].size = sizeof(inarg);
454 req->in.args[0].value = &inarg;
455 req->in.args[1].size = entry->d_name.len + 1;
456 req->in.args[1].value = entry->d_name.name;
457 return create_new_entry(fc, req, dir, entry, mode);
460 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
461 struct nameidata *nd)
463 if (nd && (nd->flags & LOOKUP_CREATE)) {
464 int err = fuse_create_open(dir, entry, mode, nd);
467 /* Fall back on mknod */
469 return fuse_mknod(dir, entry, mode, 0);
472 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
474 struct fuse_mkdir_in inarg;
475 struct fuse_conn *fc = get_fuse_conn(dir);
476 struct fuse_req *req = fuse_get_req(fc);
480 memset(&inarg, 0, sizeof(inarg));
482 req->in.h.opcode = FUSE_MKDIR;
484 req->in.args[0].size = sizeof(inarg);
485 req->in.args[0].value = &inarg;
486 req->in.args[1].size = entry->d_name.len + 1;
487 req->in.args[1].value = entry->d_name.name;
488 return create_new_entry(fc, req, dir, entry, S_IFDIR);
491 static int fuse_symlink(struct inode *dir, struct dentry *entry,
494 struct fuse_conn *fc = get_fuse_conn(dir);
495 unsigned len = strlen(link) + 1;
496 struct fuse_req *req = fuse_get_req(fc);
500 req->in.h.opcode = FUSE_SYMLINK;
502 req->in.args[0].size = entry->d_name.len + 1;
503 req->in.args[0].value = entry->d_name.name;
504 req->in.args[1].size = len;
505 req->in.args[1].value = link;
506 return create_new_entry(fc, req, dir, entry, S_IFLNK);
509 static int fuse_unlink(struct inode *dir, struct dentry *entry)
512 struct fuse_conn *fc = get_fuse_conn(dir);
513 struct fuse_req *req = fuse_get_req(fc);
517 req->in.h.opcode = FUSE_UNLINK;
518 req->in.h.nodeid = get_node_id(dir);
520 req->in.args[0].size = entry->d_name.len + 1;
521 req->in.args[0].value = entry->d_name.name;
522 request_send(fc, req);
523 err = req->out.h.error;
524 fuse_put_request(fc, req);
526 struct inode *inode = entry->d_inode;
528 /* Set nlink to zero so the inode can be cleared, if
529 the inode does have more links this will be
530 discovered at the next lookup/getattr */
532 fuse_invalidate_attr(inode);
533 fuse_invalidate_attr(dir);
534 fuse_invalidate_entry_cache(entry);
535 } else if (err == -EINTR)
536 fuse_invalidate_entry(entry);
540 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
543 struct fuse_conn *fc = get_fuse_conn(dir);
544 struct fuse_req *req = fuse_get_req(fc);
548 req->in.h.opcode = FUSE_RMDIR;
549 req->in.h.nodeid = get_node_id(dir);
551 req->in.args[0].size = entry->d_name.len + 1;
552 req->in.args[0].value = entry->d_name.name;
553 request_send(fc, req);
554 err = req->out.h.error;
555 fuse_put_request(fc, req);
557 clear_nlink(entry->d_inode);
558 fuse_invalidate_attr(dir);
559 fuse_invalidate_entry_cache(entry);
560 } else if (err == -EINTR)
561 fuse_invalidate_entry(entry);
565 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
566 struct inode *newdir, struct dentry *newent)
569 struct fuse_rename_in inarg;
570 struct fuse_conn *fc = get_fuse_conn(olddir);
571 struct fuse_req *req = fuse_get_req(fc);
575 memset(&inarg, 0, sizeof(inarg));
576 inarg.newdir = get_node_id(newdir);
577 req->in.h.opcode = FUSE_RENAME;
578 req->in.h.nodeid = get_node_id(olddir);
580 req->in.args[0].size = sizeof(inarg);
581 req->in.args[0].value = &inarg;
582 req->in.args[1].size = oldent->d_name.len + 1;
583 req->in.args[1].value = oldent->d_name.name;
584 req->in.args[2].size = newent->d_name.len + 1;
585 req->in.args[2].value = newent->d_name.name;
586 request_send(fc, req);
587 err = req->out.h.error;
588 fuse_put_request(fc, req);
590 fuse_invalidate_attr(olddir);
591 if (olddir != newdir)
592 fuse_invalidate_attr(newdir);
594 /* newent will end up negative */
596 fuse_invalidate_entry_cache(newent);
597 } else if (err == -EINTR) {
598 /* If request was interrupted, DEITY only knows if the
599 rename actually took place. If the invalidation
600 fails (e.g. some process has CWD under the renamed
601 directory), then there can be inconsistency between
602 the dcache and the real filesystem. Tough luck. */
603 fuse_invalidate_entry(oldent);
605 fuse_invalidate_entry(newent);
611 static int fuse_link(struct dentry *entry, struct inode *newdir,
612 struct dentry *newent)
615 struct fuse_link_in inarg;
616 struct inode *inode = entry->d_inode;
617 struct fuse_conn *fc = get_fuse_conn(inode);
618 struct fuse_req *req = fuse_get_req(fc);
622 memset(&inarg, 0, sizeof(inarg));
623 inarg.oldnodeid = get_node_id(inode);
624 req->in.h.opcode = FUSE_LINK;
626 req->in.args[0].size = sizeof(inarg);
627 req->in.args[0].value = &inarg;
628 req->in.args[1].size = newent->d_name.len + 1;
629 req->in.args[1].value = newent->d_name.name;
630 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
631 /* Contrary to "normal" filesystems it can happen that link
632 makes two "logical" inodes point to the same "physical"
633 inode. We invalidate the attributes of the old one, so it
634 will reflect changes in the backing inode (link count,
637 if (!err || err == -EINTR)
638 fuse_invalidate_attr(inode);
642 int fuse_do_getattr(struct inode *inode)
645 struct fuse_attr_out arg;
646 struct fuse_conn *fc = get_fuse_conn(inode);
647 struct fuse_req *req = fuse_get_req(fc);
651 req->in.h.opcode = FUSE_GETATTR;
652 req->in.h.nodeid = get_node_id(inode);
653 req->out.numargs = 1;
654 req->out.args[0].size = sizeof(arg);
655 req->out.args[0].value = &arg;
656 request_send(fc, req);
657 err = req->out.h.error;
658 fuse_put_request(fc, req);
660 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
661 make_bad_inode(inode);
664 struct fuse_inode *fi = get_fuse_inode(inode);
665 fuse_change_attributes(inode, &arg.attr);
666 fi->i_time = time_to_jiffies(arg.attr_valid,
667 arg.attr_valid_nsec);
674 * Calling into a user-controlled filesystem gives the filesystem
675 * daemon ptrace-like capabilities over the requester process. This
676 * means, that the filesystem daemon is able to record the exact
677 * filesystem operations performed, and can also control the behavior
678 * of the requester process in otherwise impossible ways. For example
679 * it can delay the operation for arbitrary length of time allowing
680 * DoS against the requester.
682 * For this reason only those processes can call into the filesystem,
683 * for which the owner of the mount has ptrace privilege. This
684 * excludes processes started by other users, suid or sgid processes.
686 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
688 if (fc->flags & FUSE_ALLOW_OTHER)
691 if (task->euid == fc->user_id &&
692 task->suid == fc->user_id &&
693 task->uid == fc->user_id &&
694 task->egid == fc->group_id &&
695 task->sgid == fc->group_id &&
696 task->gid == fc->group_id)
703 * Check whether the inode attributes are still valid
705 * If the attribute validity timeout has expired, then fetch the fresh
706 * attributes with a 'getattr' request
708 * I'm not sure why cached attributes are never returned for the root
709 * inode, this is probably being too cautious.
711 static int fuse_revalidate(struct dentry *entry)
713 struct inode *inode = entry->d_inode;
714 struct fuse_inode *fi = get_fuse_inode(inode);
715 struct fuse_conn *fc = get_fuse_conn(inode);
717 if (!fuse_allow_task(fc, current))
719 if (get_node_id(inode) != FUSE_ROOT_ID &&
720 fi->i_time >= get_jiffies_64())
723 return fuse_do_getattr(inode);
726 static int fuse_access(struct inode *inode, int mask)
728 struct fuse_conn *fc = get_fuse_conn(inode);
729 struct fuse_req *req;
730 struct fuse_access_in inarg;
736 req = fuse_get_req(fc);
740 memset(&inarg, 0, sizeof(inarg));
742 req->in.h.opcode = FUSE_ACCESS;
743 req->in.h.nodeid = get_node_id(inode);
745 req->in.args[0].size = sizeof(inarg);
746 req->in.args[0].value = &inarg;
747 request_send(fc, req);
748 err = req->out.h.error;
749 fuse_put_request(fc, req);
750 if (err == -ENOSYS) {
758 * Check permission. The two basic access models of FUSE are:
760 * 1) Local access checking ('default_permissions' mount option) based
761 * on file mode. This is the plain old disk filesystem permission
764 * 2) "Remote" access checking, where server is responsible for
765 * checking permission in each inode operation. An exception to this
766 * is if ->permission() was invoked from sys_access() in which case an
767 * access request is sent. Execute permission is still checked
768 * locally based on file mode.
770 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
772 struct fuse_conn *fc = get_fuse_conn(inode);
774 if (!fuse_allow_task(fc, current))
776 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
777 int err = generic_permission(inode, mask, NULL);
779 /* If permission is denied, try to refresh file
780 attributes. This is also needed, because the root
781 node will at first have no permissions */
782 if (err == -EACCES) {
783 err = fuse_do_getattr(inode);
785 err = generic_permission(inode, mask, NULL);
788 /* Note: the opposite of the above test does not
789 exist. So if permissions are revoked this won't be
790 noticed immediately, only after the attribute
791 timeout has expired */
795 int mode = inode->i_mode;
796 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
799 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
800 return fuse_access(inode, mask);
805 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
806 void *dstbuf, filldir_t filldir)
808 while (nbytes >= FUSE_NAME_OFFSET) {
809 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
810 size_t reclen = FUSE_DIRENT_SIZE(dirent);
812 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
817 over = filldir(dstbuf, dirent->name, dirent->namelen,
818 file->f_pos, dirent->ino, dirent->type);
824 file->f_pos = dirent->off;
830 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
835 struct inode *inode = file->f_dentry->d_inode;
836 struct fuse_conn *fc = get_fuse_conn(inode);
837 struct fuse_req *req;
839 if (is_bad_inode(inode))
842 req = fuse_get_req(fc);
846 page = alloc_page(GFP_KERNEL);
848 fuse_put_request(fc, req);
852 req->pages[0] = page;
853 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
854 request_send(fc, req);
855 nbytes = req->out.args[0].size;
856 err = req->out.h.error;
857 fuse_put_request(fc, req);
859 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
863 fuse_invalidate_attr(inode); /* atime changed */
867 static char *read_link(struct dentry *dentry)
869 struct inode *inode = dentry->d_inode;
870 struct fuse_conn *fc = get_fuse_conn(inode);
871 struct fuse_req *req = fuse_get_req(fc);
875 return ERR_PTR(PTR_ERR(req));
877 link = (char *) __get_free_page(GFP_KERNEL);
879 link = ERR_PTR(-ENOMEM);
882 req->in.h.opcode = FUSE_READLINK;
883 req->in.h.nodeid = get_node_id(inode);
885 req->out.numargs = 1;
886 req->out.args[0].size = PAGE_SIZE - 1;
887 req->out.args[0].value = link;
888 request_send(fc, req);
889 if (req->out.h.error) {
890 free_page((unsigned long) link);
891 link = ERR_PTR(req->out.h.error);
893 link[req->out.args[0].size] = '\0';
895 fuse_put_request(fc, req);
896 fuse_invalidate_attr(inode); /* atime changed */
900 static void free_link(char *link)
903 free_page((unsigned long) link);
906 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
908 nd_set_link(nd, read_link(dentry));
912 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
914 free_link(nd_get_link(nd));
917 static int fuse_dir_open(struct inode *inode, struct file *file)
919 return fuse_open_common(inode, file, 1);
922 static int fuse_dir_release(struct inode *inode, struct file *file)
924 return fuse_release_common(inode, file, 1);
927 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
929 /* nfsd can call this with no file */
930 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
933 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
935 unsigned ivalid = iattr->ia_valid;
937 if (ivalid & ATTR_MODE)
938 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
939 if (ivalid & ATTR_UID)
940 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
941 if (ivalid & ATTR_GID)
942 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
943 if (ivalid & ATTR_SIZE)
944 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
945 /* You can only _set_ these together (they may change by themselves) */
946 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
947 arg->valid |= FATTR_ATIME | FATTR_MTIME;
948 arg->atime = iattr->ia_atime.tv_sec;
949 arg->mtime = iattr->ia_mtime.tv_sec;
951 if (ivalid & ATTR_FILE) {
952 struct fuse_file *ff = iattr->ia_file->private_data;
953 arg->valid |= FATTR_FH;
958 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
960 struct fuse_conn *fc = get_fuse_conn(inode);
963 spin_lock(&fc->lock);
964 need_trunc = inode->i_size > offset;
965 i_size_write(inode, offset);
966 spin_unlock(&fc->lock);
969 struct address_space *mapping = inode->i_mapping;
970 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
971 truncate_inode_pages(mapping, offset);
976 * Set attributes, and at the same time refresh them.
978 * Truncation is slightly complicated, because the 'truncate' request
979 * may fail, in which case we don't want to touch the mapping.
980 * vmtruncate() doesn't allow for this case, so do the rlimit checking
981 * and the actual truncation by hand.
983 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
985 struct inode *inode = entry->d_inode;
986 struct fuse_conn *fc = get_fuse_conn(inode);
987 struct fuse_inode *fi = get_fuse_inode(inode);
988 struct fuse_req *req;
989 struct fuse_setattr_in inarg;
990 struct fuse_attr_out outarg;
994 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
995 err = inode_change_ok(inode, attr);
1000 if (attr->ia_valid & ATTR_SIZE) {
1001 unsigned long limit;
1003 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1004 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1005 send_sig(SIGXFSZ, current, 0);
1010 req = fuse_get_req(fc);
1012 return PTR_ERR(req);
1014 memset(&inarg, 0, sizeof(inarg));
1015 iattr_to_fattr(attr, &inarg);
1016 req->in.h.opcode = FUSE_SETATTR;
1017 req->in.h.nodeid = get_node_id(inode);
1018 req->in.numargs = 1;
1019 req->in.args[0].size = sizeof(inarg);
1020 req->in.args[0].value = &inarg;
1021 req->out.numargs = 1;
1022 req->out.args[0].size = sizeof(outarg);
1023 req->out.args[0].value = &outarg;
1024 request_send(fc, req);
1025 err = req->out.h.error;
1026 fuse_put_request(fc, req);
1028 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1029 make_bad_inode(inode);
1033 fuse_vmtruncate(inode, outarg.attr.size);
1034 fuse_change_attributes(inode, &outarg.attr);
1035 fi->i_time = time_to_jiffies(outarg.attr_valid,
1036 outarg.attr_valid_nsec);
1038 } else if (err == -EINTR)
1039 fuse_invalidate_attr(inode);
1044 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1047 struct inode *inode = entry->d_inode;
1048 int err = fuse_revalidate(entry);
1050 generic_fillattr(inode, stat);
1055 static int fuse_setxattr(struct dentry *entry, const char *name,
1056 const void *value, size_t size, int flags)
1058 struct inode *inode = entry->d_inode;
1059 struct fuse_conn *fc = get_fuse_conn(inode);
1060 struct fuse_req *req;
1061 struct fuse_setxattr_in inarg;
1064 if (fc->no_setxattr)
1067 req = fuse_get_req(fc);
1069 return PTR_ERR(req);
1071 memset(&inarg, 0, sizeof(inarg));
1073 inarg.flags = flags;
1074 req->in.h.opcode = FUSE_SETXATTR;
1075 req->in.h.nodeid = get_node_id(inode);
1076 req->in.numargs = 3;
1077 req->in.args[0].size = sizeof(inarg);
1078 req->in.args[0].value = &inarg;
1079 req->in.args[1].size = strlen(name) + 1;
1080 req->in.args[1].value = name;
1081 req->in.args[2].size = size;
1082 req->in.args[2].value = value;
1083 request_send(fc, req);
1084 err = req->out.h.error;
1085 fuse_put_request(fc, req);
1086 if (err == -ENOSYS) {
1087 fc->no_setxattr = 1;
1093 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1094 void *value, size_t size)
1096 struct inode *inode = entry->d_inode;
1097 struct fuse_conn *fc = get_fuse_conn(inode);
1098 struct fuse_req *req;
1099 struct fuse_getxattr_in inarg;
1100 struct fuse_getxattr_out outarg;
1103 if (fc->no_getxattr)
1106 req = fuse_get_req(fc);
1108 return PTR_ERR(req);
1110 memset(&inarg, 0, sizeof(inarg));
1112 req->in.h.opcode = FUSE_GETXATTR;
1113 req->in.h.nodeid = get_node_id(inode);
1114 req->in.numargs = 2;
1115 req->in.args[0].size = sizeof(inarg);
1116 req->in.args[0].value = &inarg;
1117 req->in.args[1].size = strlen(name) + 1;
1118 req->in.args[1].value = name;
1119 /* This is really two different operations rolled into one */
1120 req->out.numargs = 1;
1122 req->out.argvar = 1;
1123 req->out.args[0].size = size;
1124 req->out.args[0].value = value;
1126 req->out.args[0].size = sizeof(outarg);
1127 req->out.args[0].value = &outarg;
1129 request_send(fc, req);
1130 ret = req->out.h.error;
1132 ret = size ? req->out.args[0].size : outarg.size;
1134 if (ret == -ENOSYS) {
1135 fc->no_getxattr = 1;
1139 fuse_put_request(fc, req);
1143 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1145 struct inode *inode = entry->d_inode;
1146 struct fuse_conn *fc = get_fuse_conn(inode);
1147 struct fuse_req *req;
1148 struct fuse_getxattr_in inarg;
1149 struct fuse_getxattr_out outarg;
1152 if (fc->no_listxattr)
1155 req = fuse_get_req(fc);
1157 return PTR_ERR(req);
1159 memset(&inarg, 0, sizeof(inarg));
1161 req->in.h.opcode = FUSE_LISTXATTR;
1162 req->in.h.nodeid = get_node_id(inode);
1163 req->in.numargs = 1;
1164 req->in.args[0].size = sizeof(inarg);
1165 req->in.args[0].value = &inarg;
1166 /* This is really two different operations rolled into one */
1167 req->out.numargs = 1;
1169 req->out.argvar = 1;
1170 req->out.args[0].size = size;
1171 req->out.args[0].value = list;
1173 req->out.args[0].size = sizeof(outarg);
1174 req->out.args[0].value = &outarg;
1176 request_send(fc, req);
1177 ret = req->out.h.error;
1179 ret = size ? req->out.args[0].size : outarg.size;
1181 if (ret == -ENOSYS) {
1182 fc->no_listxattr = 1;
1186 fuse_put_request(fc, req);
1190 static int fuse_removexattr(struct dentry *entry, const char *name)
1192 struct inode *inode = entry->d_inode;
1193 struct fuse_conn *fc = get_fuse_conn(inode);
1194 struct fuse_req *req;
1197 if (fc->no_removexattr)
1200 req = fuse_get_req(fc);
1202 return PTR_ERR(req);
1204 req->in.h.opcode = FUSE_REMOVEXATTR;
1205 req->in.h.nodeid = get_node_id(inode);
1206 req->in.numargs = 1;
1207 req->in.args[0].size = strlen(name) + 1;
1208 req->in.args[0].value = name;
1209 request_send(fc, req);
1210 err = req->out.h.error;
1211 fuse_put_request(fc, req);
1212 if (err == -ENOSYS) {
1213 fc->no_removexattr = 1;
1219 static struct inode_operations fuse_dir_inode_operations = {
1220 .lookup = fuse_lookup,
1221 .mkdir = fuse_mkdir,
1222 .symlink = fuse_symlink,
1223 .unlink = fuse_unlink,
1224 .rmdir = fuse_rmdir,
1225 .rename = fuse_rename,
1227 .setattr = fuse_setattr,
1228 .create = fuse_create,
1229 .mknod = fuse_mknod,
1230 .permission = fuse_permission,
1231 .getattr = fuse_getattr,
1232 .setxattr = fuse_setxattr,
1233 .getxattr = fuse_getxattr,
1234 .listxattr = fuse_listxattr,
1235 .removexattr = fuse_removexattr,
1238 static const struct file_operations fuse_dir_operations = {
1239 .llseek = generic_file_llseek,
1240 .read = generic_read_dir,
1241 .readdir = fuse_readdir,
1242 .open = fuse_dir_open,
1243 .release = fuse_dir_release,
1244 .fsync = fuse_dir_fsync,
1247 static struct inode_operations fuse_common_inode_operations = {
1248 .setattr = fuse_setattr,
1249 .permission = fuse_permission,
1250 .getattr = fuse_getattr,
1251 .setxattr = fuse_setxattr,
1252 .getxattr = fuse_getxattr,
1253 .listxattr = fuse_listxattr,
1254 .removexattr = fuse_removexattr,
1257 static struct inode_operations fuse_symlink_inode_operations = {
1258 .setattr = fuse_setattr,
1259 .follow_link = fuse_follow_link,
1260 .put_link = fuse_put_link,
1261 .readlink = generic_readlink,
1262 .getattr = fuse_getattr,
1263 .setxattr = fuse_setxattr,
1264 .getxattr = fuse_getxattr,
1265 .listxattr = fuse_listxattr,
1266 .removexattr = fuse_removexattr,
1269 void fuse_init_common(struct inode *inode)
1271 inode->i_op = &fuse_common_inode_operations;
1274 void fuse_init_dir(struct inode *inode)
1276 inode->i_op = &fuse_dir_inode_operations;
1277 inode->i_fop = &fuse_dir_operations;
1280 void fuse_init_symlink(struct inode *inode)
1282 inode->i_op = &fuse_symlink_inode_operations;