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);
168 fuse_put_request(fc, req);
169 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
172 fuse_change_attributes(inode, &outarg.attr);
173 fuse_change_timeout(entry, &outarg);
179 * Check if there's already a hashed alias of this directory inode.
180 * If yes, then lookup and mkdir must not create a new alias.
182 static int dir_alias(struct inode *inode)
184 if (S_ISDIR(inode->i_mode)) {
185 struct dentry *alias = d_find_alias(inode);
194 static int invalid_nodeid(u64 nodeid)
196 return !nodeid || nodeid == FUSE_ROOT_ID;
199 static struct dentry_operations fuse_dentry_operations = {
200 .d_revalidate = fuse_dentry_revalidate,
203 static int valid_mode(int m)
205 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
206 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
209 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
210 struct nameidata *nd)
213 struct fuse_entry_out outarg;
214 struct inode *inode = NULL;
215 struct fuse_conn *fc = get_fuse_conn(dir);
216 struct fuse_req *req;
218 if (entry->d_name.len > FUSE_NAME_MAX)
219 return ERR_PTR(-ENAMETOOLONG);
221 req = fuse_get_req(fc);
223 return ERR_PTR(PTR_ERR(req));
225 fuse_lookup_init(req, dir, entry, &outarg);
226 request_send(fc, req);
227 err = req->out.h.error;
228 /* Zero nodeid is same as -ENOENT, but with valid timeout */
229 if (!err && outarg.nodeid &&
230 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
232 if (!err && outarg.nodeid) {
233 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
236 fuse_send_forget(fc, req, outarg.nodeid, 1);
237 return ERR_PTR(-ENOMEM);
240 fuse_put_request(fc, req);
241 if (err && err != -ENOENT)
244 if (inode && dir_alias(inode)) {
246 return ERR_PTR(-EIO);
249 entry->d_op = &fuse_dentry_operations;
251 fuse_change_timeout(entry, &outarg);
253 fuse_invalidate_entry_cache(entry);
258 * Synchronous release for the case when something goes wrong in CREATE_OPEN
260 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
261 u64 nodeid, int flags)
263 struct fuse_req *req;
265 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
267 request_send(fc, req);
268 fuse_put_request(fc, req);
272 * Atomic create+open operation
274 * If the filesystem doesn't support this, then fall back to separate
275 * 'mknod' + 'open' requests.
277 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
278 struct nameidata *nd)
282 struct fuse_conn *fc = get_fuse_conn(dir);
283 struct fuse_req *req;
284 struct fuse_req *forget_req;
285 struct fuse_open_in inarg;
286 struct fuse_open_out outopen;
287 struct fuse_entry_out outentry;
288 struct fuse_file *ff;
290 int flags = nd->intent.open.flags - 1;
295 forget_req = fuse_get_req(fc);
296 if (IS_ERR(forget_req))
297 return PTR_ERR(forget_req);
299 req = fuse_get_req(fc);
302 goto out_put_forget_req;
305 ff = fuse_file_alloc();
307 goto out_put_request;
310 memset(&inarg, 0, sizeof(inarg));
313 req->in.h.opcode = FUSE_CREATE;
314 req->in.h.nodeid = get_node_id(dir);
316 req->in.args[0].size = sizeof(inarg);
317 req->in.args[0].value = &inarg;
318 req->in.args[1].size = entry->d_name.len + 1;
319 req->in.args[1].value = entry->d_name.name;
320 req->out.numargs = 2;
321 req->out.args[0].size = sizeof(outentry);
322 req->out.args[0].value = &outentry;
323 req->out.args[1].size = sizeof(outopen);
324 req->out.args[1].value = &outopen;
325 request_send(fc, req);
326 err = req->out.h.error;
334 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
337 fuse_put_request(fc, req);
338 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
341 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
343 fuse_sync_release(fc, ff, outentry.nodeid, flags);
344 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
347 fuse_put_request(fc, forget_req);
348 d_instantiate(entry, inode);
349 fuse_change_timeout(entry, &outentry);
350 file = lookup_instantiate_filp(nd, entry, generic_file_open);
353 fuse_sync_release(fc, ff, outentry.nodeid, flags);
354 return PTR_ERR(file);
356 fuse_finish_open(inode, file, ff, &outopen);
362 fuse_put_request(fc, req);
364 fuse_put_request(fc, forget_req);
369 * Code shared between mknod, mkdir, symlink and link
371 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
372 struct inode *dir, struct dentry *entry,
375 struct fuse_entry_out outarg;
379 req->in.h.nodeid = get_node_id(dir);
380 req->out.numargs = 1;
381 req->out.args[0].size = sizeof(outarg);
382 req->out.args[0].value = &outarg;
383 request_send(fc, req);
384 err = req->out.h.error;
386 fuse_put_request(fc, req);
390 if (invalid_nodeid(outarg.nodeid))
391 goto out_put_request;
393 if ((outarg.attr.mode ^ mode) & S_IFMT)
394 goto out_put_request;
396 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
399 fuse_send_forget(fc, req, outarg.nodeid, 1);
402 fuse_put_request(fc, req);
404 if (dir_alias(inode)) {
409 d_instantiate(entry, inode);
410 fuse_change_timeout(entry, &outarg);
411 fuse_invalidate_attr(dir);
415 fuse_put_request(fc, req);
419 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
422 struct fuse_mknod_in inarg;
423 struct fuse_conn *fc = get_fuse_conn(dir);
424 struct fuse_req *req = fuse_get_req(fc);
428 memset(&inarg, 0, sizeof(inarg));
430 inarg.rdev = new_encode_dev(rdev);
431 req->in.h.opcode = FUSE_MKNOD;
433 req->in.args[0].size = sizeof(inarg);
434 req->in.args[0].value = &inarg;
435 req->in.args[1].size = entry->d_name.len + 1;
436 req->in.args[1].value = entry->d_name.name;
437 return create_new_entry(fc, req, dir, entry, mode);
440 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
441 struct nameidata *nd)
443 if (nd && (nd->flags & LOOKUP_CREATE)) {
444 int err = fuse_create_open(dir, entry, mode, nd);
447 /* Fall back on mknod */
449 return fuse_mknod(dir, entry, mode, 0);
452 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
454 struct fuse_mkdir_in inarg;
455 struct fuse_conn *fc = get_fuse_conn(dir);
456 struct fuse_req *req = fuse_get_req(fc);
460 memset(&inarg, 0, sizeof(inarg));
462 req->in.h.opcode = FUSE_MKDIR;
464 req->in.args[0].size = sizeof(inarg);
465 req->in.args[0].value = &inarg;
466 req->in.args[1].size = entry->d_name.len + 1;
467 req->in.args[1].value = entry->d_name.name;
468 return create_new_entry(fc, req, dir, entry, S_IFDIR);
471 static int fuse_symlink(struct inode *dir, struct dentry *entry,
474 struct fuse_conn *fc = get_fuse_conn(dir);
475 unsigned len = strlen(link) + 1;
476 struct fuse_req *req = fuse_get_req(fc);
480 req->in.h.opcode = FUSE_SYMLINK;
482 req->in.args[0].size = entry->d_name.len + 1;
483 req->in.args[0].value = entry->d_name.name;
484 req->in.args[1].size = len;
485 req->in.args[1].value = link;
486 return create_new_entry(fc, req, dir, entry, S_IFLNK);
489 static int fuse_unlink(struct inode *dir, struct dentry *entry)
492 struct fuse_conn *fc = get_fuse_conn(dir);
493 struct fuse_req *req = fuse_get_req(fc);
497 req->in.h.opcode = FUSE_UNLINK;
498 req->in.h.nodeid = get_node_id(dir);
500 req->in.args[0].size = entry->d_name.len + 1;
501 req->in.args[0].value = entry->d_name.name;
502 request_send(fc, req);
503 err = req->out.h.error;
504 fuse_put_request(fc, req);
506 struct inode *inode = entry->d_inode;
508 /* Set nlink to zero so the inode can be cleared, if
509 the inode does have more links this will be
510 discovered at the next lookup/getattr */
512 fuse_invalidate_attr(inode);
513 fuse_invalidate_attr(dir);
514 fuse_invalidate_entry_cache(entry);
515 } else if (err == -EINTR)
516 fuse_invalidate_entry(entry);
520 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
523 struct fuse_conn *fc = get_fuse_conn(dir);
524 struct fuse_req *req = fuse_get_req(fc);
528 req->in.h.opcode = FUSE_RMDIR;
529 req->in.h.nodeid = get_node_id(dir);
531 req->in.args[0].size = entry->d_name.len + 1;
532 req->in.args[0].value = entry->d_name.name;
533 request_send(fc, req);
534 err = req->out.h.error;
535 fuse_put_request(fc, req);
537 clear_nlink(entry->d_inode);
538 fuse_invalidate_attr(dir);
539 fuse_invalidate_entry_cache(entry);
540 } else if (err == -EINTR)
541 fuse_invalidate_entry(entry);
545 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
546 struct inode *newdir, struct dentry *newent)
549 struct fuse_rename_in inarg;
550 struct fuse_conn *fc = get_fuse_conn(olddir);
551 struct fuse_req *req = fuse_get_req(fc);
555 memset(&inarg, 0, sizeof(inarg));
556 inarg.newdir = get_node_id(newdir);
557 req->in.h.opcode = FUSE_RENAME;
558 req->in.h.nodeid = get_node_id(olddir);
560 req->in.args[0].size = sizeof(inarg);
561 req->in.args[0].value = &inarg;
562 req->in.args[1].size = oldent->d_name.len + 1;
563 req->in.args[1].value = oldent->d_name.name;
564 req->in.args[2].size = newent->d_name.len + 1;
565 req->in.args[2].value = newent->d_name.name;
566 request_send(fc, req);
567 err = req->out.h.error;
568 fuse_put_request(fc, req);
570 fuse_invalidate_attr(olddir);
571 if (olddir != newdir)
572 fuse_invalidate_attr(newdir);
574 /* newent will end up negative */
576 fuse_invalidate_entry_cache(newent);
577 } else if (err == -EINTR) {
578 /* If request was interrupted, DEITY only knows if the
579 rename actually took place. If the invalidation
580 fails (e.g. some process has CWD under the renamed
581 directory), then there can be inconsistency between
582 the dcache and the real filesystem. Tough luck. */
583 fuse_invalidate_entry(oldent);
585 fuse_invalidate_entry(newent);
591 static int fuse_link(struct dentry *entry, struct inode *newdir,
592 struct dentry *newent)
595 struct fuse_link_in inarg;
596 struct inode *inode = entry->d_inode;
597 struct fuse_conn *fc = get_fuse_conn(inode);
598 struct fuse_req *req = fuse_get_req(fc);
602 memset(&inarg, 0, sizeof(inarg));
603 inarg.oldnodeid = get_node_id(inode);
604 req->in.h.opcode = FUSE_LINK;
606 req->in.args[0].size = sizeof(inarg);
607 req->in.args[0].value = &inarg;
608 req->in.args[1].size = newent->d_name.len + 1;
609 req->in.args[1].value = newent->d_name.name;
610 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
611 /* Contrary to "normal" filesystems it can happen that link
612 makes two "logical" inodes point to the same "physical"
613 inode. We invalidate the attributes of the old one, so it
614 will reflect changes in the backing inode (link count,
617 if (!err || err == -EINTR)
618 fuse_invalidate_attr(inode);
622 int fuse_do_getattr(struct inode *inode)
625 struct fuse_attr_out arg;
626 struct fuse_conn *fc = get_fuse_conn(inode);
627 struct fuse_req *req = fuse_get_req(fc);
631 req->in.h.opcode = FUSE_GETATTR;
632 req->in.h.nodeid = get_node_id(inode);
633 req->out.numargs = 1;
634 req->out.args[0].size = sizeof(arg);
635 req->out.args[0].value = &arg;
636 request_send(fc, req);
637 err = req->out.h.error;
638 fuse_put_request(fc, req);
640 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
641 make_bad_inode(inode);
644 struct fuse_inode *fi = get_fuse_inode(inode);
645 fuse_change_attributes(inode, &arg.attr);
646 fi->i_time = time_to_jiffies(arg.attr_valid,
647 arg.attr_valid_nsec);
654 * Calling into a user-controlled filesystem gives the filesystem
655 * daemon ptrace-like capabilities over the requester process. This
656 * means, that the filesystem daemon is able to record the exact
657 * filesystem operations performed, and can also control the behavior
658 * of the requester process in otherwise impossible ways. For example
659 * it can delay the operation for arbitrary length of time allowing
660 * DoS against the requester.
662 * For this reason only those processes can call into the filesystem,
663 * for which the owner of the mount has ptrace privilege. This
664 * excludes processes started by other users, suid or sgid processes.
666 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
668 if (fc->flags & FUSE_ALLOW_OTHER)
671 if (task->euid == fc->user_id &&
672 task->suid == fc->user_id &&
673 task->uid == fc->user_id &&
674 task->egid == fc->group_id &&
675 task->sgid == fc->group_id &&
676 task->gid == fc->group_id)
683 * Check whether the inode attributes are still valid
685 * If the attribute validity timeout has expired, then fetch the fresh
686 * attributes with a 'getattr' request
688 * I'm not sure why cached attributes are never returned for the root
689 * inode, this is probably being too cautious.
691 static int fuse_revalidate(struct dentry *entry)
693 struct inode *inode = entry->d_inode;
694 struct fuse_inode *fi = get_fuse_inode(inode);
695 struct fuse_conn *fc = get_fuse_conn(inode);
697 if (!fuse_allow_task(fc, current))
699 if (get_node_id(inode) != FUSE_ROOT_ID &&
700 fi->i_time >= get_jiffies_64())
703 return fuse_do_getattr(inode);
706 static int fuse_access(struct inode *inode, int mask)
708 struct fuse_conn *fc = get_fuse_conn(inode);
709 struct fuse_req *req;
710 struct fuse_access_in inarg;
716 req = fuse_get_req(fc);
720 memset(&inarg, 0, sizeof(inarg));
722 req->in.h.opcode = FUSE_ACCESS;
723 req->in.h.nodeid = get_node_id(inode);
725 req->in.args[0].size = sizeof(inarg);
726 req->in.args[0].value = &inarg;
727 request_send(fc, req);
728 err = req->out.h.error;
729 fuse_put_request(fc, req);
730 if (err == -ENOSYS) {
738 * Check permission. The two basic access models of FUSE are:
740 * 1) Local access checking ('default_permissions' mount option) based
741 * on file mode. This is the plain old disk filesystem permission
744 * 2) "Remote" access checking, where server is responsible for
745 * checking permission in each inode operation. An exception to this
746 * is if ->permission() was invoked from sys_access() in which case an
747 * access request is sent. Execute permission is still checked
748 * locally based on file mode.
750 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
752 struct fuse_conn *fc = get_fuse_conn(inode);
754 if (!fuse_allow_task(fc, current))
756 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
757 int err = generic_permission(inode, mask, NULL);
759 /* If permission is denied, try to refresh file
760 attributes. This is also needed, because the root
761 node will at first have no permissions */
762 if (err == -EACCES) {
763 err = fuse_do_getattr(inode);
765 err = generic_permission(inode, mask, NULL);
768 /* Note: the opposite of the above test does not
769 exist. So if permissions are revoked this won't be
770 noticed immediately, only after the attribute
771 timeout has expired */
775 int mode = inode->i_mode;
776 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
779 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
780 return fuse_access(inode, mask);
785 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
786 void *dstbuf, filldir_t filldir)
788 while (nbytes >= FUSE_NAME_OFFSET) {
789 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
790 size_t reclen = FUSE_DIRENT_SIZE(dirent);
792 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
797 over = filldir(dstbuf, dirent->name, dirent->namelen,
798 file->f_pos, dirent->ino, dirent->type);
804 file->f_pos = dirent->off;
810 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
815 struct inode *inode = file->f_dentry->d_inode;
816 struct fuse_conn *fc = get_fuse_conn(inode);
817 struct fuse_req *req;
819 if (is_bad_inode(inode))
822 req = fuse_get_req(fc);
826 page = alloc_page(GFP_KERNEL);
828 fuse_put_request(fc, req);
832 req->pages[0] = page;
833 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
834 request_send(fc, req);
835 nbytes = req->out.args[0].size;
836 err = req->out.h.error;
837 fuse_put_request(fc, req);
839 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
843 fuse_invalidate_attr(inode); /* atime changed */
847 static char *read_link(struct dentry *dentry)
849 struct inode *inode = dentry->d_inode;
850 struct fuse_conn *fc = get_fuse_conn(inode);
851 struct fuse_req *req = fuse_get_req(fc);
855 return ERR_PTR(PTR_ERR(req));
857 link = (char *) __get_free_page(GFP_KERNEL);
859 link = ERR_PTR(-ENOMEM);
862 req->in.h.opcode = FUSE_READLINK;
863 req->in.h.nodeid = get_node_id(inode);
865 req->out.numargs = 1;
866 req->out.args[0].size = PAGE_SIZE - 1;
867 req->out.args[0].value = link;
868 request_send(fc, req);
869 if (req->out.h.error) {
870 free_page((unsigned long) link);
871 link = ERR_PTR(req->out.h.error);
873 link[req->out.args[0].size] = '\0';
875 fuse_put_request(fc, req);
876 fuse_invalidate_attr(inode); /* atime changed */
880 static void free_link(char *link)
883 free_page((unsigned long) link);
886 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
888 nd_set_link(nd, read_link(dentry));
892 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
894 free_link(nd_get_link(nd));
897 static int fuse_dir_open(struct inode *inode, struct file *file)
899 return fuse_open_common(inode, file, 1);
902 static int fuse_dir_release(struct inode *inode, struct file *file)
904 return fuse_release_common(inode, file, 1);
907 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
909 /* nfsd can call this with no file */
910 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
913 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
915 unsigned ivalid = iattr->ia_valid;
917 if (ivalid & ATTR_MODE)
918 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
919 if (ivalid & ATTR_UID)
920 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
921 if (ivalid & ATTR_GID)
922 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
923 if (ivalid & ATTR_SIZE)
924 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
925 /* You can only _set_ these together (they may change by themselves) */
926 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
927 arg->valid |= FATTR_ATIME | FATTR_MTIME;
928 arg->atime = iattr->ia_atime.tv_sec;
929 arg->mtime = iattr->ia_mtime.tv_sec;
931 if (ivalid & ATTR_FILE) {
932 struct fuse_file *ff = iattr->ia_file->private_data;
933 arg->valid |= FATTR_FH;
938 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
940 struct fuse_conn *fc = get_fuse_conn(inode);
943 spin_lock(&fc->lock);
944 need_trunc = inode->i_size > offset;
945 i_size_write(inode, offset);
946 spin_unlock(&fc->lock);
949 struct address_space *mapping = inode->i_mapping;
950 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
951 truncate_inode_pages(mapping, offset);
956 * Set attributes, and at the same time refresh them.
958 * Truncation is slightly complicated, because the 'truncate' request
959 * may fail, in which case we don't want to touch the mapping.
960 * vmtruncate() doesn't allow for this case, so do the rlimit checking
961 * and the actual truncation by hand.
963 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
965 struct inode *inode = entry->d_inode;
966 struct fuse_conn *fc = get_fuse_conn(inode);
967 struct fuse_inode *fi = get_fuse_inode(inode);
968 struct fuse_req *req;
969 struct fuse_setattr_in inarg;
970 struct fuse_attr_out outarg;
974 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
975 err = inode_change_ok(inode, attr);
980 if (attr->ia_valid & ATTR_SIZE) {
983 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
984 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
985 send_sig(SIGXFSZ, current, 0);
990 req = fuse_get_req(fc);
994 memset(&inarg, 0, sizeof(inarg));
995 iattr_to_fattr(attr, &inarg);
996 req->in.h.opcode = FUSE_SETATTR;
997 req->in.h.nodeid = get_node_id(inode);
999 req->in.args[0].size = sizeof(inarg);
1000 req->in.args[0].value = &inarg;
1001 req->out.numargs = 1;
1002 req->out.args[0].size = sizeof(outarg);
1003 req->out.args[0].value = &outarg;
1004 request_send(fc, req);
1005 err = req->out.h.error;
1006 fuse_put_request(fc, req);
1008 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1009 make_bad_inode(inode);
1013 fuse_vmtruncate(inode, outarg.attr.size);
1014 fuse_change_attributes(inode, &outarg.attr);
1015 fi->i_time = time_to_jiffies(outarg.attr_valid,
1016 outarg.attr_valid_nsec);
1018 } else if (err == -EINTR)
1019 fuse_invalidate_attr(inode);
1024 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1027 struct inode *inode = entry->d_inode;
1028 int err = fuse_revalidate(entry);
1030 generic_fillattr(inode, stat);
1035 static int fuse_setxattr(struct dentry *entry, const char *name,
1036 const void *value, size_t size, int flags)
1038 struct inode *inode = entry->d_inode;
1039 struct fuse_conn *fc = get_fuse_conn(inode);
1040 struct fuse_req *req;
1041 struct fuse_setxattr_in inarg;
1044 if (fc->no_setxattr)
1047 req = fuse_get_req(fc);
1049 return PTR_ERR(req);
1051 memset(&inarg, 0, sizeof(inarg));
1053 inarg.flags = flags;
1054 req->in.h.opcode = FUSE_SETXATTR;
1055 req->in.h.nodeid = get_node_id(inode);
1056 req->in.numargs = 3;
1057 req->in.args[0].size = sizeof(inarg);
1058 req->in.args[0].value = &inarg;
1059 req->in.args[1].size = strlen(name) + 1;
1060 req->in.args[1].value = name;
1061 req->in.args[2].size = size;
1062 req->in.args[2].value = value;
1063 request_send(fc, req);
1064 err = req->out.h.error;
1065 fuse_put_request(fc, req);
1066 if (err == -ENOSYS) {
1067 fc->no_setxattr = 1;
1073 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1074 void *value, size_t size)
1076 struct inode *inode = entry->d_inode;
1077 struct fuse_conn *fc = get_fuse_conn(inode);
1078 struct fuse_req *req;
1079 struct fuse_getxattr_in inarg;
1080 struct fuse_getxattr_out outarg;
1083 if (fc->no_getxattr)
1086 req = fuse_get_req(fc);
1088 return PTR_ERR(req);
1090 memset(&inarg, 0, sizeof(inarg));
1092 req->in.h.opcode = FUSE_GETXATTR;
1093 req->in.h.nodeid = get_node_id(inode);
1094 req->in.numargs = 2;
1095 req->in.args[0].size = sizeof(inarg);
1096 req->in.args[0].value = &inarg;
1097 req->in.args[1].size = strlen(name) + 1;
1098 req->in.args[1].value = name;
1099 /* This is really two different operations rolled into one */
1100 req->out.numargs = 1;
1102 req->out.argvar = 1;
1103 req->out.args[0].size = size;
1104 req->out.args[0].value = value;
1106 req->out.args[0].size = sizeof(outarg);
1107 req->out.args[0].value = &outarg;
1109 request_send(fc, req);
1110 ret = req->out.h.error;
1112 ret = size ? req->out.args[0].size : outarg.size;
1114 if (ret == -ENOSYS) {
1115 fc->no_getxattr = 1;
1119 fuse_put_request(fc, req);
1123 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1125 struct inode *inode = entry->d_inode;
1126 struct fuse_conn *fc = get_fuse_conn(inode);
1127 struct fuse_req *req;
1128 struct fuse_getxattr_in inarg;
1129 struct fuse_getxattr_out outarg;
1132 if (fc->no_listxattr)
1135 req = fuse_get_req(fc);
1137 return PTR_ERR(req);
1139 memset(&inarg, 0, sizeof(inarg));
1141 req->in.h.opcode = FUSE_LISTXATTR;
1142 req->in.h.nodeid = get_node_id(inode);
1143 req->in.numargs = 1;
1144 req->in.args[0].size = sizeof(inarg);
1145 req->in.args[0].value = &inarg;
1146 /* This is really two different operations rolled into one */
1147 req->out.numargs = 1;
1149 req->out.argvar = 1;
1150 req->out.args[0].size = size;
1151 req->out.args[0].value = list;
1153 req->out.args[0].size = sizeof(outarg);
1154 req->out.args[0].value = &outarg;
1156 request_send(fc, req);
1157 ret = req->out.h.error;
1159 ret = size ? req->out.args[0].size : outarg.size;
1161 if (ret == -ENOSYS) {
1162 fc->no_listxattr = 1;
1166 fuse_put_request(fc, req);
1170 static int fuse_removexattr(struct dentry *entry, const char *name)
1172 struct inode *inode = entry->d_inode;
1173 struct fuse_conn *fc = get_fuse_conn(inode);
1174 struct fuse_req *req;
1177 if (fc->no_removexattr)
1180 req = fuse_get_req(fc);
1182 return PTR_ERR(req);
1184 req->in.h.opcode = FUSE_REMOVEXATTR;
1185 req->in.h.nodeid = get_node_id(inode);
1186 req->in.numargs = 1;
1187 req->in.args[0].size = strlen(name) + 1;
1188 req->in.args[0].value = name;
1189 request_send(fc, req);
1190 err = req->out.h.error;
1191 fuse_put_request(fc, req);
1192 if (err == -ENOSYS) {
1193 fc->no_removexattr = 1;
1199 static struct inode_operations fuse_dir_inode_operations = {
1200 .lookup = fuse_lookup,
1201 .mkdir = fuse_mkdir,
1202 .symlink = fuse_symlink,
1203 .unlink = fuse_unlink,
1204 .rmdir = fuse_rmdir,
1205 .rename = fuse_rename,
1207 .setattr = fuse_setattr,
1208 .create = fuse_create,
1209 .mknod = fuse_mknod,
1210 .permission = fuse_permission,
1211 .getattr = fuse_getattr,
1212 .setxattr = fuse_setxattr,
1213 .getxattr = fuse_getxattr,
1214 .listxattr = fuse_listxattr,
1215 .removexattr = fuse_removexattr,
1218 static const struct file_operations fuse_dir_operations = {
1219 .llseek = generic_file_llseek,
1220 .read = generic_read_dir,
1221 .readdir = fuse_readdir,
1222 .open = fuse_dir_open,
1223 .release = fuse_dir_release,
1224 .fsync = fuse_dir_fsync,
1227 static struct inode_operations fuse_common_inode_operations = {
1228 .setattr = fuse_setattr,
1229 .permission = fuse_permission,
1230 .getattr = fuse_getattr,
1231 .setxattr = fuse_setxattr,
1232 .getxattr = fuse_getxattr,
1233 .listxattr = fuse_listxattr,
1234 .removexattr = fuse_removexattr,
1237 static struct inode_operations fuse_symlink_inode_operations = {
1238 .setattr = fuse_setattr,
1239 .follow_link = fuse_follow_link,
1240 .put_link = fuse_put_link,
1241 .readlink = generic_readlink,
1242 .getattr = fuse_getattr,
1243 .setxattr = fuse_setxattr,
1244 .getxattr = fuse_getxattr,
1245 .listxattr = fuse_listxattr,
1246 .removexattr = fuse_removexattr,
1249 void fuse_init_common(struct inode *inode)
1251 inode->i_op = &fuse_common_inode_operations;
1254 void fuse_init_dir(struct inode *inode)
1256 inode->i_op = &fuse_dir_inode_operations;
1257 inode->i_fop = &fuse_dir_operations;
1260 void fuse_init_symlink(struct inode *inode)
1262 inode->i_op = &fuse_symlink_inode_operations;