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);
181 * Check if there's already a hashed alias of this directory inode.
182 * If yes, then lookup and mkdir must not create a new alias.
184 static int dir_alias(struct inode *inode)
186 if (S_ISDIR(inode->i_mode)) {
187 struct dentry *alias = d_find_alias(inode);
196 static int invalid_nodeid(u64 nodeid)
198 return !nodeid || nodeid == FUSE_ROOT_ID;
201 static struct dentry_operations fuse_dentry_operations = {
202 .d_revalidate = fuse_dentry_revalidate,
205 static int valid_mode(int m)
207 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
208 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
211 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
212 struct nameidata *nd)
215 struct fuse_entry_out outarg;
216 struct inode *inode = NULL;
217 struct fuse_conn *fc = get_fuse_conn(dir);
218 struct fuse_req *req;
220 if (entry->d_name.len > FUSE_NAME_MAX)
221 return ERR_PTR(-ENAMETOOLONG);
223 req = fuse_get_req(fc);
225 return ERR_PTR(PTR_ERR(req));
227 fuse_lookup_init(req, dir, entry, &outarg);
228 request_send(fc, req);
229 err = req->out.h.error;
230 /* Zero nodeid is same as -ENOENT, but with valid timeout */
231 if (!err && outarg.nodeid &&
232 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
234 if (!err && outarg.nodeid) {
235 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
238 fuse_send_forget(fc, req, outarg.nodeid, 1);
239 return ERR_PTR(-ENOMEM);
242 fuse_put_request(fc, req);
243 if (err && err != -ENOENT)
246 if (inode && dir_alias(inode)) {
248 return ERR_PTR(-EIO);
251 entry->d_op = &fuse_dentry_operations;
253 fuse_change_timeout(entry, &outarg);
255 fuse_invalidate_entry_cache(entry);
260 * Synchronous release for the case when something goes wrong in CREATE_OPEN
262 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
263 u64 nodeid, int flags)
265 struct fuse_req *req;
267 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
269 request_send(fc, req);
270 fuse_put_request(fc, req);
274 * Atomic create+open operation
276 * If the filesystem doesn't support this, then fall back to separate
277 * 'mknod' + 'open' requests.
279 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
280 struct nameidata *nd)
284 struct fuse_conn *fc = get_fuse_conn(dir);
285 struct fuse_req *req;
286 struct fuse_req *forget_req;
287 struct fuse_open_in inarg;
288 struct fuse_open_out outopen;
289 struct fuse_entry_out outentry;
290 struct fuse_file *ff;
292 int flags = nd->intent.open.flags - 1;
297 forget_req = fuse_get_req(fc);
298 if (IS_ERR(forget_req))
299 return PTR_ERR(forget_req);
301 req = fuse_get_req(fc);
304 goto out_put_forget_req;
307 ff = fuse_file_alloc();
309 goto out_put_request;
312 memset(&inarg, 0, sizeof(inarg));
315 req->in.h.opcode = FUSE_CREATE;
316 req->in.h.nodeid = get_node_id(dir);
318 req->in.args[0].size = sizeof(inarg);
319 req->in.args[0].value = &inarg;
320 req->in.args[1].size = entry->d_name.len + 1;
321 req->in.args[1].value = entry->d_name.name;
322 req->out.numargs = 2;
323 req->out.args[0].size = sizeof(outentry);
324 req->out.args[0].value = &outentry;
325 req->out.args[1].size = sizeof(outopen);
326 req->out.args[1].value = &outopen;
327 request_send(fc, req);
328 err = req->out.h.error;
336 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
339 fuse_put_request(fc, req);
340 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
343 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
345 fuse_sync_release(fc, ff, outentry.nodeid, flags);
346 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
349 fuse_put_request(fc, forget_req);
350 d_instantiate(entry, inode);
351 fuse_change_timeout(entry, &outentry);
352 file = lookup_instantiate_filp(nd, entry, generic_file_open);
355 fuse_sync_release(fc, ff, outentry.nodeid, flags);
356 return PTR_ERR(file);
358 fuse_finish_open(inode, file, ff, &outopen);
364 fuse_put_request(fc, req);
366 fuse_put_request(fc, forget_req);
371 * Code shared between mknod, mkdir, symlink and link
373 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
374 struct inode *dir, struct dentry *entry,
377 struct fuse_entry_out outarg;
381 req->in.h.nodeid = get_node_id(dir);
382 req->out.numargs = 1;
383 req->out.args[0].size = sizeof(outarg);
384 req->out.args[0].value = &outarg;
385 request_send(fc, req);
386 err = req->out.h.error;
388 fuse_put_request(fc, req);
392 if (invalid_nodeid(outarg.nodeid))
393 goto out_put_request;
395 if ((outarg.attr.mode ^ mode) & S_IFMT)
396 goto out_put_request;
398 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
401 fuse_send_forget(fc, req, outarg.nodeid, 1);
404 fuse_put_request(fc, req);
406 if (dir_alias(inode)) {
411 d_instantiate(entry, inode);
412 fuse_change_timeout(entry, &outarg);
413 fuse_invalidate_attr(dir);
417 fuse_put_request(fc, req);
421 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
424 struct fuse_mknod_in inarg;
425 struct fuse_conn *fc = get_fuse_conn(dir);
426 struct fuse_req *req = fuse_get_req(fc);
430 memset(&inarg, 0, sizeof(inarg));
432 inarg.rdev = new_encode_dev(rdev);
433 req->in.h.opcode = FUSE_MKNOD;
435 req->in.args[0].size = sizeof(inarg);
436 req->in.args[0].value = &inarg;
437 req->in.args[1].size = entry->d_name.len + 1;
438 req->in.args[1].value = entry->d_name.name;
439 return create_new_entry(fc, req, dir, entry, mode);
442 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
443 struct nameidata *nd)
445 if (nd && (nd->flags & LOOKUP_CREATE)) {
446 int err = fuse_create_open(dir, entry, mode, nd);
449 /* Fall back on mknod */
451 return fuse_mknod(dir, entry, mode, 0);
454 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
456 struct fuse_mkdir_in inarg;
457 struct fuse_conn *fc = get_fuse_conn(dir);
458 struct fuse_req *req = fuse_get_req(fc);
462 memset(&inarg, 0, sizeof(inarg));
464 req->in.h.opcode = FUSE_MKDIR;
466 req->in.args[0].size = sizeof(inarg);
467 req->in.args[0].value = &inarg;
468 req->in.args[1].size = entry->d_name.len + 1;
469 req->in.args[1].value = entry->d_name.name;
470 return create_new_entry(fc, req, dir, entry, S_IFDIR);
473 static int fuse_symlink(struct inode *dir, struct dentry *entry,
476 struct fuse_conn *fc = get_fuse_conn(dir);
477 unsigned len = strlen(link) + 1;
478 struct fuse_req *req = fuse_get_req(fc);
482 req->in.h.opcode = FUSE_SYMLINK;
484 req->in.args[0].size = entry->d_name.len + 1;
485 req->in.args[0].value = entry->d_name.name;
486 req->in.args[1].size = len;
487 req->in.args[1].value = link;
488 return create_new_entry(fc, req, dir, entry, S_IFLNK);
491 static int fuse_unlink(struct inode *dir, struct dentry *entry)
494 struct fuse_conn *fc = get_fuse_conn(dir);
495 struct fuse_req *req = fuse_get_req(fc);
499 req->in.h.opcode = FUSE_UNLINK;
500 req->in.h.nodeid = get_node_id(dir);
502 req->in.args[0].size = entry->d_name.len + 1;
503 req->in.args[0].value = entry->d_name.name;
504 request_send(fc, req);
505 err = req->out.h.error;
506 fuse_put_request(fc, req);
508 struct inode *inode = entry->d_inode;
510 /* Set nlink to zero so the inode can be cleared, if
511 the inode does have more links this will be
512 discovered at the next lookup/getattr */
514 fuse_invalidate_attr(inode);
515 fuse_invalidate_attr(dir);
516 fuse_invalidate_entry_cache(entry);
517 } else if (err == -EINTR)
518 fuse_invalidate_entry(entry);
522 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
525 struct fuse_conn *fc = get_fuse_conn(dir);
526 struct fuse_req *req = fuse_get_req(fc);
530 req->in.h.opcode = FUSE_RMDIR;
531 req->in.h.nodeid = get_node_id(dir);
533 req->in.args[0].size = entry->d_name.len + 1;
534 req->in.args[0].value = entry->d_name.name;
535 request_send(fc, req);
536 err = req->out.h.error;
537 fuse_put_request(fc, req);
539 clear_nlink(entry->d_inode);
540 fuse_invalidate_attr(dir);
541 fuse_invalidate_entry_cache(entry);
542 } else if (err == -EINTR)
543 fuse_invalidate_entry(entry);
547 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
548 struct inode *newdir, struct dentry *newent)
551 struct fuse_rename_in inarg;
552 struct fuse_conn *fc = get_fuse_conn(olddir);
553 struct fuse_req *req = fuse_get_req(fc);
557 memset(&inarg, 0, sizeof(inarg));
558 inarg.newdir = get_node_id(newdir);
559 req->in.h.opcode = FUSE_RENAME;
560 req->in.h.nodeid = get_node_id(olddir);
562 req->in.args[0].size = sizeof(inarg);
563 req->in.args[0].value = &inarg;
564 req->in.args[1].size = oldent->d_name.len + 1;
565 req->in.args[1].value = oldent->d_name.name;
566 req->in.args[2].size = newent->d_name.len + 1;
567 req->in.args[2].value = newent->d_name.name;
568 request_send(fc, req);
569 err = req->out.h.error;
570 fuse_put_request(fc, req);
572 fuse_invalidate_attr(olddir);
573 if (olddir != newdir)
574 fuse_invalidate_attr(newdir);
576 /* newent will end up negative */
578 fuse_invalidate_entry_cache(newent);
579 } else if (err == -EINTR) {
580 /* If request was interrupted, DEITY only knows if the
581 rename actually took place. If the invalidation
582 fails (e.g. some process has CWD under the renamed
583 directory), then there can be inconsistency between
584 the dcache and the real filesystem. Tough luck. */
585 fuse_invalidate_entry(oldent);
587 fuse_invalidate_entry(newent);
593 static int fuse_link(struct dentry *entry, struct inode *newdir,
594 struct dentry *newent)
597 struct fuse_link_in inarg;
598 struct inode *inode = entry->d_inode;
599 struct fuse_conn *fc = get_fuse_conn(inode);
600 struct fuse_req *req = fuse_get_req(fc);
604 memset(&inarg, 0, sizeof(inarg));
605 inarg.oldnodeid = get_node_id(inode);
606 req->in.h.opcode = FUSE_LINK;
608 req->in.args[0].size = sizeof(inarg);
609 req->in.args[0].value = &inarg;
610 req->in.args[1].size = newent->d_name.len + 1;
611 req->in.args[1].value = newent->d_name.name;
612 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
613 /* Contrary to "normal" filesystems it can happen that link
614 makes two "logical" inodes point to the same "physical"
615 inode. We invalidate the attributes of the old one, so it
616 will reflect changes in the backing inode (link count,
619 if (!err || err == -EINTR)
620 fuse_invalidate_attr(inode);
624 int fuse_do_getattr(struct inode *inode)
627 struct fuse_attr_out arg;
628 struct fuse_conn *fc = get_fuse_conn(inode);
629 struct fuse_req *req = fuse_get_req(fc);
633 req->in.h.opcode = FUSE_GETATTR;
634 req->in.h.nodeid = get_node_id(inode);
635 req->out.numargs = 1;
636 req->out.args[0].size = sizeof(arg);
637 req->out.args[0].value = &arg;
638 request_send(fc, req);
639 err = req->out.h.error;
640 fuse_put_request(fc, req);
642 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
643 make_bad_inode(inode);
646 struct fuse_inode *fi = get_fuse_inode(inode);
647 fuse_change_attributes(inode, &arg.attr);
648 fi->i_time = time_to_jiffies(arg.attr_valid,
649 arg.attr_valid_nsec);
656 * Calling into a user-controlled filesystem gives the filesystem
657 * daemon ptrace-like capabilities over the requester process. This
658 * means, that the filesystem daemon is able to record the exact
659 * filesystem operations performed, and can also control the behavior
660 * of the requester process in otherwise impossible ways. For example
661 * it can delay the operation for arbitrary length of time allowing
662 * DoS against the requester.
664 * For this reason only those processes can call into the filesystem,
665 * for which the owner of the mount has ptrace privilege. This
666 * excludes processes started by other users, suid or sgid processes.
668 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
670 if (fc->flags & FUSE_ALLOW_OTHER)
673 if (task->euid == fc->user_id &&
674 task->suid == fc->user_id &&
675 task->uid == fc->user_id &&
676 task->egid == fc->group_id &&
677 task->sgid == fc->group_id &&
678 task->gid == fc->group_id)
685 * Check whether the inode attributes are still valid
687 * If the attribute validity timeout has expired, then fetch the fresh
688 * attributes with a 'getattr' request
690 * I'm not sure why cached attributes are never returned for the root
691 * inode, this is probably being too cautious.
693 static int fuse_revalidate(struct dentry *entry)
695 struct inode *inode = entry->d_inode;
696 struct fuse_inode *fi = get_fuse_inode(inode);
697 struct fuse_conn *fc = get_fuse_conn(inode);
699 if (!fuse_allow_task(fc, current))
701 if (get_node_id(inode) != FUSE_ROOT_ID &&
702 fi->i_time >= get_jiffies_64())
705 return fuse_do_getattr(inode);
708 static int fuse_access(struct inode *inode, int mask)
710 struct fuse_conn *fc = get_fuse_conn(inode);
711 struct fuse_req *req;
712 struct fuse_access_in inarg;
718 req = fuse_get_req(fc);
722 memset(&inarg, 0, sizeof(inarg));
724 req->in.h.opcode = FUSE_ACCESS;
725 req->in.h.nodeid = get_node_id(inode);
727 req->in.args[0].size = sizeof(inarg);
728 req->in.args[0].value = &inarg;
729 request_send(fc, req);
730 err = req->out.h.error;
731 fuse_put_request(fc, req);
732 if (err == -ENOSYS) {
740 * Check permission. The two basic access models of FUSE are:
742 * 1) Local access checking ('default_permissions' mount option) based
743 * on file mode. This is the plain old disk filesystem permission
746 * 2) "Remote" access checking, where server is responsible for
747 * checking permission in each inode operation. An exception to this
748 * is if ->permission() was invoked from sys_access() in which case an
749 * access request is sent. Execute permission is still checked
750 * locally based on file mode.
752 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
754 struct fuse_conn *fc = get_fuse_conn(inode);
756 if (!fuse_allow_task(fc, current))
758 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
759 int err = generic_permission(inode, mask, NULL);
761 /* If permission is denied, try to refresh file
762 attributes. This is also needed, because the root
763 node will at first have no permissions */
764 if (err == -EACCES) {
765 err = fuse_do_getattr(inode);
767 err = generic_permission(inode, mask, NULL);
770 /* Note: the opposite of the above test does not
771 exist. So if permissions are revoked this won't be
772 noticed immediately, only after the attribute
773 timeout has expired */
777 int mode = inode->i_mode;
778 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
781 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
782 return fuse_access(inode, mask);
787 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
788 void *dstbuf, filldir_t filldir)
790 while (nbytes >= FUSE_NAME_OFFSET) {
791 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
792 size_t reclen = FUSE_DIRENT_SIZE(dirent);
794 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
799 over = filldir(dstbuf, dirent->name, dirent->namelen,
800 file->f_pos, dirent->ino, dirent->type);
806 file->f_pos = dirent->off;
812 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
817 struct inode *inode = file->f_dentry->d_inode;
818 struct fuse_conn *fc = get_fuse_conn(inode);
819 struct fuse_req *req;
821 if (is_bad_inode(inode))
824 req = fuse_get_req(fc);
828 page = alloc_page(GFP_KERNEL);
830 fuse_put_request(fc, req);
834 req->pages[0] = page;
835 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
836 request_send(fc, req);
837 nbytes = req->out.args[0].size;
838 err = req->out.h.error;
839 fuse_put_request(fc, req);
841 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
845 fuse_invalidate_attr(inode); /* atime changed */
849 static char *read_link(struct dentry *dentry)
851 struct inode *inode = dentry->d_inode;
852 struct fuse_conn *fc = get_fuse_conn(inode);
853 struct fuse_req *req = fuse_get_req(fc);
857 return ERR_PTR(PTR_ERR(req));
859 link = (char *) __get_free_page(GFP_KERNEL);
861 link = ERR_PTR(-ENOMEM);
864 req->in.h.opcode = FUSE_READLINK;
865 req->in.h.nodeid = get_node_id(inode);
867 req->out.numargs = 1;
868 req->out.args[0].size = PAGE_SIZE - 1;
869 req->out.args[0].value = link;
870 request_send(fc, req);
871 if (req->out.h.error) {
872 free_page((unsigned long) link);
873 link = ERR_PTR(req->out.h.error);
875 link[req->out.args[0].size] = '\0';
877 fuse_put_request(fc, req);
878 fuse_invalidate_attr(inode); /* atime changed */
882 static void free_link(char *link)
885 free_page((unsigned long) link);
888 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
890 nd_set_link(nd, read_link(dentry));
894 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
896 free_link(nd_get_link(nd));
899 static int fuse_dir_open(struct inode *inode, struct file *file)
901 return fuse_open_common(inode, file, 1);
904 static int fuse_dir_release(struct inode *inode, struct file *file)
906 return fuse_release_common(inode, file, 1);
909 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
911 /* nfsd can call this with no file */
912 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
915 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
917 unsigned ivalid = iattr->ia_valid;
919 if (ivalid & ATTR_MODE)
920 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
921 if (ivalid & ATTR_UID)
922 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
923 if (ivalid & ATTR_GID)
924 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
925 if (ivalid & ATTR_SIZE)
926 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
927 /* You can only _set_ these together (they may change by themselves) */
928 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
929 arg->valid |= FATTR_ATIME | FATTR_MTIME;
930 arg->atime = iattr->ia_atime.tv_sec;
931 arg->mtime = iattr->ia_mtime.tv_sec;
933 if (ivalid & ATTR_FILE) {
934 struct fuse_file *ff = iattr->ia_file->private_data;
935 arg->valid |= FATTR_FH;
940 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
942 struct fuse_conn *fc = get_fuse_conn(inode);
945 spin_lock(&fc->lock);
946 need_trunc = inode->i_size > offset;
947 i_size_write(inode, offset);
948 spin_unlock(&fc->lock);
951 struct address_space *mapping = inode->i_mapping;
952 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
953 truncate_inode_pages(mapping, offset);
958 * Set attributes, and at the same time refresh them.
960 * Truncation is slightly complicated, because the 'truncate' request
961 * may fail, in which case we don't want to touch the mapping.
962 * vmtruncate() doesn't allow for this case, so do the rlimit checking
963 * and the actual truncation by hand.
965 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
967 struct inode *inode = entry->d_inode;
968 struct fuse_conn *fc = get_fuse_conn(inode);
969 struct fuse_inode *fi = get_fuse_inode(inode);
970 struct fuse_req *req;
971 struct fuse_setattr_in inarg;
972 struct fuse_attr_out outarg;
976 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
977 err = inode_change_ok(inode, attr);
982 if (attr->ia_valid & ATTR_SIZE) {
985 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
986 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
987 send_sig(SIGXFSZ, current, 0);
992 req = fuse_get_req(fc);
996 memset(&inarg, 0, sizeof(inarg));
997 iattr_to_fattr(attr, &inarg);
998 req->in.h.opcode = FUSE_SETATTR;
999 req->in.h.nodeid = get_node_id(inode);
1000 req->in.numargs = 1;
1001 req->in.args[0].size = sizeof(inarg);
1002 req->in.args[0].value = &inarg;
1003 req->out.numargs = 1;
1004 req->out.args[0].size = sizeof(outarg);
1005 req->out.args[0].value = &outarg;
1006 request_send(fc, req);
1007 err = req->out.h.error;
1008 fuse_put_request(fc, req);
1010 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1011 make_bad_inode(inode);
1015 fuse_vmtruncate(inode, outarg.attr.size);
1016 fuse_change_attributes(inode, &outarg.attr);
1017 fi->i_time = time_to_jiffies(outarg.attr_valid,
1018 outarg.attr_valid_nsec);
1020 } else if (err == -EINTR)
1021 fuse_invalidate_attr(inode);
1026 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1029 struct inode *inode = entry->d_inode;
1030 int err = fuse_revalidate(entry);
1032 generic_fillattr(inode, stat);
1037 static int fuse_setxattr(struct dentry *entry, const char *name,
1038 const void *value, size_t size, int flags)
1040 struct inode *inode = entry->d_inode;
1041 struct fuse_conn *fc = get_fuse_conn(inode);
1042 struct fuse_req *req;
1043 struct fuse_setxattr_in inarg;
1046 if (fc->no_setxattr)
1049 req = fuse_get_req(fc);
1051 return PTR_ERR(req);
1053 memset(&inarg, 0, sizeof(inarg));
1055 inarg.flags = flags;
1056 req->in.h.opcode = FUSE_SETXATTR;
1057 req->in.h.nodeid = get_node_id(inode);
1058 req->in.numargs = 3;
1059 req->in.args[0].size = sizeof(inarg);
1060 req->in.args[0].value = &inarg;
1061 req->in.args[1].size = strlen(name) + 1;
1062 req->in.args[1].value = name;
1063 req->in.args[2].size = size;
1064 req->in.args[2].value = value;
1065 request_send(fc, req);
1066 err = req->out.h.error;
1067 fuse_put_request(fc, req);
1068 if (err == -ENOSYS) {
1069 fc->no_setxattr = 1;
1075 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1076 void *value, size_t size)
1078 struct inode *inode = entry->d_inode;
1079 struct fuse_conn *fc = get_fuse_conn(inode);
1080 struct fuse_req *req;
1081 struct fuse_getxattr_in inarg;
1082 struct fuse_getxattr_out outarg;
1085 if (fc->no_getxattr)
1088 req = fuse_get_req(fc);
1090 return PTR_ERR(req);
1092 memset(&inarg, 0, sizeof(inarg));
1094 req->in.h.opcode = FUSE_GETXATTR;
1095 req->in.h.nodeid = get_node_id(inode);
1096 req->in.numargs = 2;
1097 req->in.args[0].size = sizeof(inarg);
1098 req->in.args[0].value = &inarg;
1099 req->in.args[1].size = strlen(name) + 1;
1100 req->in.args[1].value = name;
1101 /* This is really two different operations rolled into one */
1102 req->out.numargs = 1;
1104 req->out.argvar = 1;
1105 req->out.args[0].size = size;
1106 req->out.args[0].value = value;
1108 req->out.args[0].size = sizeof(outarg);
1109 req->out.args[0].value = &outarg;
1111 request_send(fc, req);
1112 ret = req->out.h.error;
1114 ret = size ? req->out.args[0].size : outarg.size;
1116 if (ret == -ENOSYS) {
1117 fc->no_getxattr = 1;
1121 fuse_put_request(fc, req);
1125 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1127 struct inode *inode = entry->d_inode;
1128 struct fuse_conn *fc = get_fuse_conn(inode);
1129 struct fuse_req *req;
1130 struct fuse_getxattr_in inarg;
1131 struct fuse_getxattr_out outarg;
1134 if (fc->no_listxattr)
1137 req = fuse_get_req(fc);
1139 return PTR_ERR(req);
1141 memset(&inarg, 0, sizeof(inarg));
1143 req->in.h.opcode = FUSE_LISTXATTR;
1144 req->in.h.nodeid = get_node_id(inode);
1145 req->in.numargs = 1;
1146 req->in.args[0].size = sizeof(inarg);
1147 req->in.args[0].value = &inarg;
1148 /* This is really two different operations rolled into one */
1149 req->out.numargs = 1;
1151 req->out.argvar = 1;
1152 req->out.args[0].size = size;
1153 req->out.args[0].value = list;
1155 req->out.args[0].size = sizeof(outarg);
1156 req->out.args[0].value = &outarg;
1158 request_send(fc, req);
1159 ret = req->out.h.error;
1161 ret = size ? req->out.args[0].size : outarg.size;
1163 if (ret == -ENOSYS) {
1164 fc->no_listxattr = 1;
1168 fuse_put_request(fc, req);
1172 static int fuse_removexattr(struct dentry *entry, const char *name)
1174 struct inode *inode = entry->d_inode;
1175 struct fuse_conn *fc = get_fuse_conn(inode);
1176 struct fuse_req *req;
1179 if (fc->no_removexattr)
1182 req = fuse_get_req(fc);
1184 return PTR_ERR(req);
1186 req->in.h.opcode = FUSE_REMOVEXATTR;
1187 req->in.h.nodeid = get_node_id(inode);
1188 req->in.numargs = 1;
1189 req->in.args[0].size = strlen(name) + 1;
1190 req->in.args[0].value = name;
1191 request_send(fc, req);
1192 err = req->out.h.error;
1193 fuse_put_request(fc, req);
1194 if (err == -ENOSYS) {
1195 fc->no_removexattr = 1;
1201 static struct inode_operations fuse_dir_inode_operations = {
1202 .lookup = fuse_lookup,
1203 .mkdir = fuse_mkdir,
1204 .symlink = fuse_symlink,
1205 .unlink = fuse_unlink,
1206 .rmdir = fuse_rmdir,
1207 .rename = fuse_rename,
1209 .setattr = fuse_setattr,
1210 .create = fuse_create,
1211 .mknod = fuse_mknod,
1212 .permission = fuse_permission,
1213 .getattr = fuse_getattr,
1214 .setxattr = fuse_setxattr,
1215 .getxattr = fuse_getxattr,
1216 .listxattr = fuse_listxattr,
1217 .removexattr = fuse_removexattr,
1220 static const struct file_operations fuse_dir_operations = {
1221 .llseek = generic_file_llseek,
1222 .read = generic_read_dir,
1223 .readdir = fuse_readdir,
1224 .open = fuse_dir_open,
1225 .release = fuse_dir_release,
1226 .fsync = fuse_dir_fsync,
1229 static struct inode_operations fuse_common_inode_operations = {
1230 .setattr = fuse_setattr,
1231 .permission = fuse_permission,
1232 .getattr = fuse_getattr,
1233 .setxattr = fuse_setxattr,
1234 .getxattr = fuse_getxattr,
1235 .listxattr = fuse_listxattr,
1236 .removexattr = fuse_removexattr,
1239 static struct inode_operations fuse_symlink_inode_operations = {
1240 .setattr = fuse_setattr,
1241 .follow_link = fuse_follow_link,
1242 .put_link = fuse_put_link,
1243 .readlink = generic_readlink,
1244 .getattr = fuse_getattr,
1245 .setxattr = fuse_setxattr,
1246 .getxattr = fuse_getxattr,
1247 .listxattr = fuse_listxattr,
1248 .removexattr = fuse_removexattr,
1251 void fuse_init_common(struct inode *inode)
1253 inode->i_op = &fuse_common_inode_operations;
1256 void fuse_init_dir(struct inode *inode)
1258 inode->i_op = &fuse_dir_inode_operations;
1259 inode->i_fop = &fuse_dir_operations;
1262 void fuse_init_symlink(struct inode *inode)
1264 inode->i_op = &fuse_symlink_inode_operations;