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>
18 * FUSE caches dentries and attributes with separate timeout. The
19 * time in jiffies until the dentry/attributes are valid is stored in
20 * dentry->d_time and fuse_inode->i_time respectively.
24 * Calculate the time in jiffies until a dentry/attributes are valid
26 static unsigned long time_to_jiffies(unsigned long sec, unsigned long nsec)
29 struct timespec ts = {sec, nsec};
30 return jiffies + timespec_to_jiffies(&ts);
36 * Set dentry and possibly attribute timeouts from the lookup/mk*
39 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
41 entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
43 get_fuse_inode(entry->d_inode)->i_time =
44 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
48 * Mark the attributes as stale, so that at the next call to
49 * ->getattr() they will be fetched from userspace
51 void fuse_invalidate_attr(struct inode *inode)
53 get_fuse_inode(inode)->i_time = jiffies - 1;
57 * Just mark the entry as stale, so that a next attempt to look it up
58 * will result in a new lookup call to userspace
60 * This is called when a dentry is about to become negative and the
61 * timeout is unknown (unlink, rmdir, rename and in some cases
64 static void fuse_invalidate_entry_cache(struct dentry *entry)
66 entry->d_time = jiffies - 1;
70 * Same as fuse_invalidate_entry_cache(), but also try to remove the
71 * dentry from the hash
73 static void fuse_invalidate_entry(struct dentry *entry)
76 fuse_invalidate_entry_cache(entry);
79 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
81 struct fuse_entry_out *outarg)
83 req->in.h.opcode = FUSE_LOOKUP;
84 req->in.h.nodeid = get_node_id(dir);
86 req->in.args[0].size = entry->d_name.len + 1;
87 req->in.args[0].value = entry->d_name.name;
89 req->out.args[0].size = sizeof(struct fuse_entry_out);
90 req->out.args[0].value = outarg;
94 * Check whether the dentry is still valid
96 * If the entry validity timeout has expired and the dentry is
97 * positive, try to redo the lookup. If the lookup results in a
98 * different inode, then let the VFS invalidate the dentry and redo
99 * the lookup once more. If the lookup results in the same inode,
100 * then refresh the attributes, timeouts and mark the dentry valid.
102 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
104 struct inode *inode = entry->d_inode;
106 if (inode && is_bad_inode(inode))
108 else if (time_after(jiffies, entry->d_time)) {
110 struct fuse_entry_out outarg;
111 struct fuse_conn *fc;
112 struct fuse_req *req;
114 /* Doesn't hurt to "reset" the validity timeout */
115 fuse_invalidate_entry_cache(entry);
117 /* For negative dentries, always do a fresh lookup */
121 fc = get_fuse_conn(inode);
122 req = fuse_get_req(fc);
126 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
127 request_send(fc, req);
128 err = req->out.h.error;
129 /* Zero nodeid is same as -ENOENT */
130 if (!err && !outarg.nodeid)
133 struct fuse_inode *fi = get_fuse_inode(inode);
134 if (outarg.nodeid != get_node_id(inode)) {
135 fuse_send_forget(fc, req, outarg.nodeid, 1);
140 fuse_put_request(fc, req);
141 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
144 fuse_change_attributes(inode, &outarg.attr);
145 fuse_change_timeout(entry, &outarg);
151 * Check if there's already a hashed alias of this directory inode.
152 * If yes, then lookup and mkdir must not create a new alias.
154 static int dir_alias(struct inode *inode)
156 if (S_ISDIR(inode->i_mode)) {
157 struct dentry *alias = d_find_alias(inode);
166 static int invalid_nodeid(u64 nodeid)
168 return !nodeid || nodeid == FUSE_ROOT_ID;
171 static struct dentry_operations fuse_dentry_operations = {
172 .d_revalidate = fuse_dentry_revalidate,
175 static int valid_mode(int m)
177 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
178 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
181 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
182 struct nameidata *nd)
185 struct fuse_entry_out outarg;
186 struct inode *inode = NULL;
187 struct fuse_conn *fc = get_fuse_conn(dir);
188 struct fuse_req *req;
190 if (entry->d_name.len > FUSE_NAME_MAX)
191 return ERR_PTR(-ENAMETOOLONG);
193 req = fuse_get_req(fc);
195 return ERR_PTR(PTR_ERR(req));
197 fuse_lookup_init(req, dir, entry, &outarg);
198 request_send(fc, req);
199 err = req->out.h.error;
200 /* Zero nodeid is same as -ENOENT, but with valid timeout */
201 if (!err && outarg.nodeid &&
202 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
204 if (!err && outarg.nodeid) {
205 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
208 fuse_send_forget(fc, req, outarg.nodeid, 1);
209 return ERR_PTR(-ENOMEM);
212 fuse_put_request(fc, req);
213 if (err && err != -ENOENT)
216 if (inode && dir_alias(inode)) {
218 return ERR_PTR(-EIO);
221 entry->d_op = &fuse_dentry_operations;
223 fuse_change_timeout(entry, &outarg);
225 fuse_invalidate_entry_cache(entry);
230 * Synchronous release for the case when something goes wrong in CREATE_OPEN
232 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
233 u64 nodeid, int flags)
235 struct fuse_req *req;
237 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
239 request_send(fc, req);
240 fuse_put_request(fc, req);
244 * Atomic create+open operation
246 * If the filesystem doesn't support this, then fall back to separate
247 * 'mknod' + 'open' requests.
249 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
250 struct nameidata *nd)
254 struct fuse_conn *fc = get_fuse_conn(dir);
255 struct fuse_req *req;
256 struct fuse_req *forget_req;
257 struct fuse_open_in inarg;
258 struct fuse_open_out outopen;
259 struct fuse_entry_out outentry;
260 struct fuse_file *ff;
262 int flags = nd->intent.open.flags - 1;
267 forget_req = fuse_get_req(fc);
268 if (IS_ERR(forget_req))
269 return PTR_ERR(forget_req);
271 req = fuse_get_req(fc);
274 goto out_put_forget_req;
277 ff = fuse_file_alloc();
279 goto out_put_request;
282 memset(&inarg, 0, sizeof(inarg));
285 req->in.h.opcode = FUSE_CREATE;
286 req->in.h.nodeid = get_node_id(dir);
288 req->in.args[0].size = sizeof(inarg);
289 req->in.args[0].value = &inarg;
290 req->in.args[1].size = entry->d_name.len + 1;
291 req->in.args[1].value = entry->d_name.name;
292 req->out.numargs = 2;
293 req->out.args[0].size = sizeof(outentry);
294 req->out.args[0].value = &outentry;
295 req->out.args[1].size = sizeof(outopen);
296 req->out.args[1].value = &outopen;
297 request_send(fc, req);
298 err = req->out.h.error;
306 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
309 fuse_put_request(fc, req);
310 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
313 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
315 fuse_sync_release(fc, ff, outentry.nodeid, flags);
316 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
319 fuse_put_request(fc, forget_req);
320 d_instantiate(entry, inode);
321 fuse_change_timeout(entry, &outentry);
322 file = lookup_instantiate_filp(nd, entry, generic_file_open);
325 fuse_sync_release(fc, ff, outentry.nodeid, flags);
326 return PTR_ERR(file);
328 fuse_finish_open(inode, file, ff, &outopen);
334 fuse_put_request(fc, req);
336 fuse_put_request(fc, forget_req);
341 * Code shared between mknod, mkdir, symlink and link
343 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
344 struct inode *dir, struct dentry *entry,
347 struct fuse_entry_out outarg;
351 req->in.h.nodeid = get_node_id(dir);
352 req->out.numargs = 1;
353 req->out.args[0].size = sizeof(outarg);
354 req->out.args[0].value = &outarg;
355 request_send(fc, req);
356 err = req->out.h.error;
358 fuse_put_request(fc, req);
362 if (invalid_nodeid(outarg.nodeid))
363 goto out_put_request;
365 if ((outarg.attr.mode ^ mode) & S_IFMT)
366 goto out_put_request;
368 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
371 fuse_send_forget(fc, req, outarg.nodeid, 1);
374 fuse_put_request(fc, req);
376 if (dir_alias(inode)) {
381 d_instantiate(entry, inode);
382 fuse_change_timeout(entry, &outarg);
383 fuse_invalidate_attr(dir);
387 fuse_put_request(fc, req);
391 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
394 struct fuse_mknod_in inarg;
395 struct fuse_conn *fc = get_fuse_conn(dir);
396 struct fuse_req *req = fuse_get_req(fc);
400 memset(&inarg, 0, sizeof(inarg));
402 inarg.rdev = new_encode_dev(rdev);
403 req->in.h.opcode = FUSE_MKNOD;
405 req->in.args[0].size = sizeof(inarg);
406 req->in.args[0].value = &inarg;
407 req->in.args[1].size = entry->d_name.len + 1;
408 req->in.args[1].value = entry->d_name.name;
409 return create_new_entry(fc, req, dir, entry, mode);
412 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
413 struct nameidata *nd)
415 if (nd && (nd->flags & LOOKUP_CREATE)) {
416 int err = fuse_create_open(dir, entry, mode, nd);
419 /* Fall back on mknod */
421 return fuse_mknod(dir, entry, mode, 0);
424 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
426 struct fuse_mkdir_in inarg;
427 struct fuse_conn *fc = get_fuse_conn(dir);
428 struct fuse_req *req = fuse_get_req(fc);
432 memset(&inarg, 0, sizeof(inarg));
434 req->in.h.opcode = FUSE_MKDIR;
436 req->in.args[0].size = sizeof(inarg);
437 req->in.args[0].value = &inarg;
438 req->in.args[1].size = entry->d_name.len + 1;
439 req->in.args[1].value = entry->d_name.name;
440 return create_new_entry(fc, req, dir, entry, S_IFDIR);
443 static int fuse_symlink(struct inode *dir, struct dentry *entry,
446 struct fuse_conn *fc = get_fuse_conn(dir);
447 unsigned len = strlen(link) + 1;
448 struct fuse_req *req = fuse_get_req(fc);
452 req->in.h.opcode = FUSE_SYMLINK;
454 req->in.args[0].size = entry->d_name.len + 1;
455 req->in.args[0].value = entry->d_name.name;
456 req->in.args[1].size = len;
457 req->in.args[1].value = link;
458 return create_new_entry(fc, req, dir, entry, S_IFLNK);
461 static int fuse_unlink(struct inode *dir, struct dentry *entry)
464 struct fuse_conn *fc = get_fuse_conn(dir);
465 struct fuse_req *req = fuse_get_req(fc);
469 req->in.h.opcode = FUSE_UNLINK;
470 req->in.h.nodeid = get_node_id(dir);
472 req->in.args[0].size = entry->d_name.len + 1;
473 req->in.args[0].value = entry->d_name.name;
474 request_send(fc, req);
475 err = req->out.h.error;
476 fuse_put_request(fc, req);
478 struct inode *inode = entry->d_inode;
480 /* Set nlink to zero so the inode can be cleared, if
481 the inode does have more links this will be
482 discovered at the next lookup/getattr */
484 fuse_invalidate_attr(inode);
485 fuse_invalidate_attr(dir);
486 fuse_invalidate_entry_cache(entry);
487 } else if (err == -EINTR)
488 fuse_invalidate_entry(entry);
492 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
495 struct fuse_conn *fc = get_fuse_conn(dir);
496 struct fuse_req *req = fuse_get_req(fc);
500 req->in.h.opcode = FUSE_RMDIR;
501 req->in.h.nodeid = get_node_id(dir);
503 req->in.args[0].size = entry->d_name.len + 1;
504 req->in.args[0].value = entry->d_name.name;
505 request_send(fc, req);
506 err = req->out.h.error;
507 fuse_put_request(fc, req);
509 entry->d_inode->i_nlink = 0;
510 fuse_invalidate_attr(dir);
511 fuse_invalidate_entry_cache(entry);
512 } else if (err == -EINTR)
513 fuse_invalidate_entry(entry);
517 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
518 struct inode *newdir, struct dentry *newent)
521 struct fuse_rename_in inarg;
522 struct fuse_conn *fc = get_fuse_conn(olddir);
523 struct fuse_req *req = fuse_get_req(fc);
527 memset(&inarg, 0, sizeof(inarg));
528 inarg.newdir = get_node_id(newdir);
529 req->in.h.opcode = FUSE_RENAME;
530 req->in.h.nodeid = get_node_id(olddir);
532 req->in.args[0].size = sizeof(inarg);
533 req->in.args[0].value = &inarg;
534 req->in.args[1].size = oldent->d_name.len + 1;
535 req->in.args[1].value = oldent->d_name.name;
536 req->in.args[2].size = newent->d_name.len + 1;
537 req->in.args[2].value = newent->d_name.name;
538 request_send(fc, req);
539 err = req->out.h.error;
540 fuse_put_request(fc, req);
542 fuse_invalidate_attr(olddir);
543 if (olddir != newdir)
544 fuse_invalidate_attr(newdir);
546 /* newent will end up negative */
548 fuse_invalidate_entry_cache(newent);
549 } else if (err == -EINTR) {
550 /* If request was interrupted, DEITY only knows if the
551 rename actually took place. If the invalidation
552 fails (e.g. some process has CWD under the renamed
553 directory), then there can be inconsistency between
554 the dcache and the real filesystem. Tough luck. */
555 fuse_invalidate_entry(oldent);
557 fuse_invalidate_entry(newent);
563 static int fuse_link(struct dentry *entry, struct inode *newdir,
564 struct dentry *newent)
567 struct fuse_link_in inarg;
568 struct inode *inode = entry->d_inode;
569 struct fuse_conn *fc = get_fuse_conn(inode);
570 struct fuse_req *req = fuse_get_req(fc);
574 memset(&inarg, 0, sizeof(inarg));
575 inarg.oldnodeid = get_node_id(inode);
576 req->in.h.opcode = FUSE_LINK;
578 req->in.args[0].size = sizeof(inarg);
579 req->in.args[0].value = &inarg;
580 req->in.args[1].size = newent->d_name.len + 1;
581 req->in.args[1].value = newent->d_name.name;
582 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
583 /* Contrary to "normal" filesystems it can happen that link
584 makes two "logical" inodes point to the same "physical"
585 inode. We invalidate the attributes of the old one, so it
586 will reflect changes in the backing inode (link count,
589 if (!err || err == -EINTR)
590 fuse_invalidate_attr(inode);
594 int fuse_do_getattr(struct inode *inode)
597 struct fuse_attr_out arg;
598 struct fuse_conn *fc = get_fuse_conn(inode);
599 struct fuse_req *req = fuse_get_req(fc);
603 req->in.h.opcode = FUSE_GETATTR;
604 req->in.h.nodeid = get_node_id(inode);
605 req->out.numargs = 1;
606 req->out.args[0].size = sizeof(arg);
607 req->out.args[0].value = &arg;
608 request_send(fc, req);
609 err = req->out.h.error;
610 fuse_put_request(fc, req);
612 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
613 make_bad_inode(inode);
616 struct fuse_inode *fi = get_fuse_inode(inode);
617 fuse_change_attributes(inode, &arg.attr);
618 fi->i_time = time_to_jiffies(arg.attr_valid,
619 arg.attr_valid_nsec);
626 * Calling into a user-controlled filesystem gives the filesystem
627 * daemon ptrace-like capabilities over the requester process. This
628 * means, that the filesystem daemon is able to record the exact
629 * filesystem operations performed, and can also control the behavior
630 * of the requester process in otherwise impossible ways. For example
631 * it can delay the operation for arbitrary length of time allowing
632 * DoS against the requester.
634 * For this reason only those processes can call into the filesystem,
635 * for which the owner of the mount has ptrace privilege. This
636 * excludes processes started by other users, suid or sgid processes.
638 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
640 if (fc->flags & FUSE_ALLOW_OTHER)
643 if (task->euid == fc->user_id &&
644 task->suid == fc->user_id &&
645 task->uid == fc->user_id &&
646 task->egid == fc->group_id &&
647 task->sgid == fc->group_id &&
648 task->gid == fc->group_id)
655 * Check whether the inode attributes are still valid
657 * If the attribute validity timeout has expired, then fetch the fresh
658 * attributes with a 'getattr' request
660 * I'm not sure why cached attributes are never returned for the root
661 * inode, this is probably being too cautious.
663 static int fuse_revalidate(struct dentry *entry)
665 struct inode *inode = entry->d_inode;
666 struct fuse_inode *fi = get_fuse_inode(inode);
667 struct fuse_conn *fc = get_fuse_conn(inode);
669 if (!fuse_allow_task(fc, current))
671 if (get_node_id(inode) != FUSE_ROOT_ID &&
672 time_before_eq(jiffies, fi->i_time))
675 return fuse_do_getattr(inode);
678 static int fuse_access(struct inode *inode, int mask)
680 struct fuse_conn *fc = get_fuse_conn(inode);
681 struct fuse_req *req;
682 struct fuse_access_in inarg;
688 req = fuse_get_req(fc);
692 memset(&inarg, 0, sizeof(inarg));
694 req->in.h.opcode = FUSE_ACCESS;
695 req->in.h.nodeid = get_node_id(inode);
697 req->in.args[0].size = sizeof(inarg);
698 req->in.args[0].value = &inarg;
699 request_send(fc, req);
700 err = req->out.h.error;
701 fuse_put_request(fc, req);
702 if (err == -ENOSYS) {
710 * Check permission. The two basic access models of FUSE are:
712 * 1) Local access checking ('default_permissions' mount option) based
713 * on file mode. This is the plain old disk filesystem permission
716 * 2) "Remote" access checking, where server is responsible for
717 * checking permission in each inode operation. An exception to this
718 * is if ->permission() was invoked from sys_access() in which case an
719 * access request is sent. Execute permission is still checked
720 * locally based on file mode.
722 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
724 struct fuse_conn *fc = get_fuse_conn(inode);
726 if (!fuse_allow_task(fc, current))
728 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
729 int err = generic_permission(inode, mask, NULL);
731 /* If permission is denied, try to refresh file
732 attributes. This is also needed, because the root
733 node will at first have no permissions */
734 if (err == -EACCES) {
735 err = fuse_do_getattr(inode);
737 err = generic_permission(inode, mask, NULL);
740 /* Note: the opposite of the above test does not
741 exist. So if permissions are revoked this won't be
742 noticed immediately, only after the attribute
743 timeout has expired */
747 int mode = inode->i_mode;
748 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
751 if (nd && (nd->flags & LOOKUP_ACCESS))
752 return fuse_access(inode, mask);
757 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
758 void *dstbuf, filldir_t filldir)
760 while (nbytes >= FUSE_NAME_OFFSET) {
761 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
762 size_t reclen = FUSE_DIRENT_SIZE(dirent);
764 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
769 over = filldir(dstbuf, dirent->name, dirent->namelen,
770 file->f_pos, dirent->ino, dirent->type);
776 file->f_pos = dirent->off;
782 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
787 struct inode *inode = file->f_dentry->d_inode;
788 struct fuse_conn *fc = get_fuse_conn(inode);
789 struct fuse_req *req;
791 if (is_bad_inode(inode))
794 req = fuse_get_req(fc);
798 page = alloc_page(GFP_KERNEL);
800 fuse_put_request(fc, req);
804 req->pages[0] = page;
805 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
806 request_send(fc, req);
807 nbytes = req->out.args[0].size;
808 err = req->out.h.error;
809 fuse_put_request(fc, req);
811 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
815 fuse_invalidate_attr(inode); /* atime changed */
819 static char *read_link(struct dentry *dentry)
821 struct inode *inode = dentry->d_inode;
822 struct fuse_conn *fc = get_fuse_conn(inode);
823 struct fuse_req *req = fuse_get_req(fc);
827 return ERR_PTR(PTR_ERR(req));
829 link = (char *) __get_free_page(GFP_KERNEL);
831 link = ERR_PTR(-ENOMEM);
834 req->in.h.opcode = FUSE_READLINK;
835 req->in.h.nodeid = get_node_id(inode);
837 req->out.numargs = 1;
838 req->out.args[0].size = PAGE_SIZE - 1;
839 req->out.args[0].value = link;
840 request_send(fc, req);
841 if (req->out.h.error) {
842 free_page((unsigned long) link);
843 link = ERR_PTR(req->out.h.error);
845 link[req->out.args[0].size] = '\0';
847 fuse_put_request(fc, req);
848 fuse_invalidate_attr(inode); /* atime changed */
852 static void free_link(char *link)
855 free_page((unsigned long) link);
858 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
860 nd_set_link(nd, read_link(dentry));
864 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
866 free_link(nd_get_link(nd));
869 static int fuse_dir_open(struct inode *inode, struct file *file)
871 return fuse_open_common(inode, file, 1);
874 static int fuse_dir_release(struct inode *inode, struct file *file)
876 return fuse_release_common(inode, file, 1);
879 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
881 /* nfsd can call this with no file */
882 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
885 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
887 unsigned ivalid = iattr->ia_valid;
889 if (ivalid & ATTR_MODE)
890 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
891 if (ivalid & ATTR_UID)
892 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
893 if (ivalid & ATTR_GID)
894 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
895 if (ivalid & ATTR_SIZE)
896 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
897 /* You can only _set_ these together (they may change by themselves) */
898 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
899 arg->valid |= FATTR_ATIME | FATTR_MTIME;
900 arg->atime = iattr->ia_atime.tv_sec;
901 arg->mtime = iattr->ia_mtime.tv_sec;
903 if (ivalid & ATTR_FILE) {
904 struct fuse_file *ff = iattr->ia_file->private_data;
905 arg->valid |= FATTR_FH;
911 * Set attributes, and at the same time refresh them.
913 * Truncation is slightly complicated, because the 'truncate' request
914 * may fail, in which case we don't want to touch the mapping.
915 * vmtruncate() doesn't allow for this case. So do the rlimit
916 * checking by hand and call vmtruncate() only after the file has
917 * actually been truncated.
919 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
921 struct inode *inode = entry->d_inode;
922 struct fuse_conn *fc = get_fuse_conn(inode);
923 struct fuse_inode *fi = get_fuse_inode(inode);
924 struct fuse_req *req;
925 struct fuse_setattr_in inarg;
926 struct fuse_attr_out outarg;
930 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
931 err = inode_change_ok(inode, attr);
936 if (attr->ia_valid & ATTR_SIZE) {
939 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
940 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
941 send_sig(SIGXFSZ, current, 0);
946 req = fuse_get_req(fc);
950 memset(&inarg, 0, sizeof(inarg));
951 iattr_to_fattr(attr, &inarg);
952 req->in.h.opcode = FUSE_SETATTR;
953 req->in.h.nodeid = get_node_id(inode);
955 req->in.args[0].size = sizeof(inarg);
956 req->in.args[0].value = &inarg;
957 req->out.numargs = 1;
958 req->out.args[0].size = sizeof(outarg);
959 req->out.args[0].value = &outarg;
960 request_send(fc, req);
961 err = req->out.h.error;
962 fuse_put_request(fc, req);
964 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
965 make_bad_inode(inode);
969 loff_t origsize = i_size_read(inode);
970 i_size_write(inode, outarg.attr.size);
971 if (origsize > outarg.attr.size)
972 vmtruncate(inode, outarg.attr.size);
974 fuse_change_attributes(inode, &outarg.attr);
975 fi->i_time = time_to_jiffies(outarg.attr_valid,
976 outarg.attr_valid_nsec);
978 } else if (err == -EINTR)
979 fuse_invalidate_attr(inode);
984 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
987 struct inode *inode = entry->d_inode;
988 int err = fuse_revalidate(entry);
990 generic_fillattr(inode, stat);
995 static int fuse_setxattr(struct dentry *entry, const char *name,
996 const void *value, size_t size, int flags)
998 struct inode *inode = entry->d_inode;
999 struct fuse_conn *fc = get_fuse_conn(inode);
1000 struct fuse_req *req;
1001 struct fuse_setxattr_in inarg;
1004 if (fc->no_setxattr)
1007 req = fuse_get_req(fc);
1009 return PTR_ERR(req);
1011 memset(&inarg, 0, sizeof(inarg));
1013 inarg.flags = flags;
1014 req->in.h.opcode = FUSE_SETXATTR;
1015 req->in.h.nodeid = get_node_id(inode);
1016 req->in.numargs = 3;
1017 req->in.args[0].size = sizeof(inarg);
1018 req->in.args[0].value = &inarg;
1019 req->in.args[1].size = strlen(name) + 1;
1020 req->in.args[1].value = name;
1021 req->in.args[2].size = size;
1022 req->in.args[2].value = value;
1023 request_send(fc, req);
1024 err = req->out.h.error;
1025 fuse_put_request(fc, req);
1026 if (err == -ENOSYS) {
1027 fc->no_setxattr = 1;
1033 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1034 void *value, size_t size)
1036 struct inode *inode = entry->d_inode;
1037 struct fuse_conn *fc = get_fuse_conn(inode);
1038 struct fuse_req *req;
1039 struct fuse_getxattr_in inarg;
1040 struct fuse_getxattr_out outarg;
1043 if (fc->no_getxattr)
1046 req = fuse_get_req(fc);
1048 return PTR_ERR(req);
1050 memset(&inarg, 0, sizeof(inarg));
1052 req->in.h.opcode = FUSE_GETXATTR;
1053 req->in.h.nodeid = get_node_id(inode);
1054 req->in.numargs = 2;
1055 req->in.args[0].size = sizeof(inarg);
1056 req->in.args[0].value = &inarg;
1057 req->in.args[1].size = strlen(name) + 1;
1058 req->in.args[1].value = name;
1059 /* This is really two different operations rolled into one */
1060 req->out.numargs = 1;
1062 req->out.argvar = 1;
1063 req->out.args[0].size = size;
1064 req->out.args[0].value = value;
1066 req->out.args[0].size = sizeof(outarg);
1067 req->out.args[0].value = &outarg;
1069 request_send(fc, req);
1070 ret = req->out.h.error;
1072 ret = size ? req->out.args[0].size : outarg.size;
1074 if (ret == -ENOSYS) {
1075 fc->no_getxattr = 1;
1079 fuse_put_request(fc, req);
1083 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1085 struct inode *inode = entry->d_inode;
1086 struct fuse_conn *fc = get_fuse_conn(inode);
1087 struct fuse_req *req;
1088 struct fuse_getxattr_in inarg;
1089 struct fuse_getxattr_out outarg;
1092 if (fc->no_listxattr)
1095 req = fuse_get_req(fc);
1097 return PTR_ERR(req);
1099 memset(&inarg, 0, sizeof(inarg));
1101 req->in.h.opcode = FUSE_LISTXATTR;
1102 req->in.h.nodeid = get_node_id(inode);
1103 req->in.numargs = 1;
1104 req->in.args[0].size = sizeof(inarg);
1105 req->in.args[0].value = &inarg;
1106 /* This is really two different operations rolled into one */
1107 req->out.numargs = 1;
1109 req->out.argvar = 1;
1110 req->out.args[0].size = size;
1111 req->out.args[0].value = list;
1113 req->out.args[0].size = sizeof(outarg);
1114 req->out.args[0].value = &outarg;
1116 request_send(fc, req);
1117 ret = req->out.h.error;
1119 ret = size ? req->out.args[0].size : outarg.size;
1121 if (ret == -ENOSYS) {
1122 fc->no_listxattr = 1;
1126 fuse_put_request(fc, req);
1130 static int fuse_removexattr(struct dentry *entry, const char *name)
1132 struct inode *inode = entry->d_inode;
1133 struct fuse_conn *fc = get_fuse_conn(inode);
1134 struct fuse_req *req;
1137 if (fc->no_removexattr)
1140 req = fuse_get_req(fc);
1142 return PTR_ERR(req);
1144 req->in.h.opcode = FUSE_REMOVEXATTR;
1145 req->in.h.nodeid = get_node_id(inode);
1146 req->in.numargs = 1;
1147 req->in.args[0].size = strlen(name) + 1;
1148 req->in.args[0].value = name;
1149 request_send(fc, req);
1150 err = req->out.h.error;
1151 fuse_put_request(fc, req);
1152 if (err == -ENOSYS) {
1153 fc->no_removexattr = 1;
1159 static struct inode_operations fuse_dir_inode_operations = {
1160 .lookup = fuse_lookup,
1161 .mkdir = fuse_mkdir,
1162 .symlink = fuse_symlink,
1163 .unlink = fuse_unlink,
1164 .rmdir = fuse_rmdir,
1165 .rename = fuse_rename,
1167 .setattr = fuse_setattr,
1168 .create = fuse_create,
1169 .mknod = fuse_mknod,
1170 .permission = fuse_permission,
1171 .getattr = fuse_getattr,
1172 .setxattr = fuse_setxattr,
1173 .getxattr = fuse_getxattr,
1174 .listxattr = fuse_listxattr,
1175 .removexattr = fuse_removexattr,
1178 static const struct file_operations fuse_dir_operations = {
1179 .llseek = generic_file_llseek,
1180 .read = generic_read_dir,
1181 .readdir = fuse_readdir,
1182 .open = fuse_dir_open,
1183 .release = fuse_dir_release,
1184 .fsync = fuse_dir_fsync,
1187 static struct inode_operations fuse_common_inode_operations = {
1188 .setattr = fuse_setattr,
1189 .permission = fuse_permission,
1190 .getattr = fuse_getattr,
1191 .setxattr = fuse_setxattr,
1192 .getxattr = fuse_getxattr,
1193 .listxattr = fuse_listxattr,
1194 .removexattr = fuse_removexattr,
1197 static struct inode_operations fuse_symlink_inode_operations = {
1198 .setattr = fuse_setattr,
1199 .follow_link = fuse_follow_link,
1200 .put_link = fuse_put_link,
1201 .readlink = generic_readlink,
1202 .getattr = fuse_getattr,
1203 .setxattr = fuse_setxattr,
1204 .getxattr = fuse_getxattr,
1205 .listxattr = fuse_listxattr,
1206 .removexattr = fuse_removexattr,
1209 void fuse_init_common(struct inode *inode)
1211 inode->i_op = &fuse_common_inode_operations;
1214 void fuse_init_dir(struct inode *inode)
1216 inode->i_op = &fuse_dir_inode_operations;
1217 inode->i_fop = &fuse_dir_operations;
1220 void fuse_init_symlink(struct inode *inode)
1222 inode->i_op = &fuse_symlink_inode_operations;