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)
28 struct timespec ts = {sec, nsec};
29 return jiffies + timespec_to_jiffies(&ts);
33 * Set dentry and possibly attribute timeouts from the lookup/mk*
36 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
38 entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
40 get_fuse_inode(entry->d_inode)->i_time =
41 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
45 * Mark the attributes as stale, so that at the next call to
46 * ->getattr() they will be fetched from userspace
48 void fuse_invalidate_attr(struct inode *inode)
50 get_fuse_inode(inode)->i_time = jiffies - 1;
54 * Just mark the entry as stale, so that a next attempt to look it up
55 * will result in a new lookup call to userspace
57 * This is called when a dentry is about to become negative and the
58 * timeout is unknown (unlink, rmdir, rename and in some cases
61 static void fuse_invalidate_entry_cache(struct dentry *entry)
63 entry->d_time = jiffies - 1;
67 * Same as fuse_invalidate_entry_cache(), but also try to remove the
68 * dentry from the hash
70 static void fuse_invalidate_entry(struct dentry *entry)
73 fuse_invalidate_entry_cache(entry);
76 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
78 struct fuse_entry_out *outarg)
80 req->in.h.opcode = FUSE_LOOKUP;
81 req->in.h.nodeid = get_node_id(dir);
83 req->in.args[0].size = entry->d_name.len + 1;
84 req->in.args[0].value = entry->d_name.name;
86 req->out.args[0].size = sizeof(struct fuse_entry_out);
87 req->out.args[0].value = outarg;
91 * Check whether the dentry is still valid
93 * If the entry validity timeout has expired and the dentry is
94 * positive, try to redo the lookup. If the lookup results in a
95 * different inode, then let the VFS invalidate the dentry and redo
96 * the lookup once more. If the lookup results in the same inode,
97 * then refresh the attributes, timeouts and mark the dentry valid.
99 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
101 struct inode *inode = entry->d_inode;
103 if (inode && is_bad_inode(inode))
105 else if (time_after(jiffies, entry->d_time)) {
107 struct fuse_entry_out outarg;
108 struct fuse_conn *fc;
109 struct fuse_req *req;
111 /* Doesn't hurt to "reset" the validity timeout */
112 fuse_invalidate_entry_cache(entry);
114 /* For negative dentries, always do a fresh lookup */
118 fc = get_fuse_conn(inode);
119 req = fuse_get_req(fc);
123 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
124 request_send(fc, req);
125 err = req->out.h.error;
126 /* Zero nodeid is same as -ENOENT */
127 if (!err && !outarg.nodeid)
130 struct fuse_inode *fi = get_fuse_inode(inode);
131 if (outarg.nodeid != get_node_id(inode)) {
132 fuse_send_forget(fc, req, outarg.nodeid, 1);
137 fuse_put_request(fc, req);
138 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
141 fuse_change_attributes(inode, &outarg.attr);
142 fuse_change_timeout(entry, &outarg);
148 * Check if there's already a hashed alias of this directory inode.
149 * If yes, then lookup and mkdir must not create a new alias.
151 static int dir_alias(struct inode *inode)
153 if (S_ISDIR(inode->i_mode)) {
154 struct dentry *alias = d_find_alias(inode);
163 static int invalid_nodeid(u64 nodeid)
165 return !nodeid || nodeid == FUSE_ROOT_ID;
168 static struct dentry_operations fuse_dentry_operations = {
169 .d_revalidate = fuse_dentry_revalidate,
172 static int valid_mode(int m)
174 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
175 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
178 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
179 struct nameidata *nd)
182 struct fuse_entry_out outarg;
183 struct inode *inode = NULL;
184 struct fuse_conn *fc = get_fuse_conn(dir);
185 struct fuse_req *req;
187 if (entry->d_name.len > FUSE_NAME_MAX)
188 return ERR_PTR(-ENAMETOOLONG);
190 req = fuse_get_req(fc);
192 return ERR_PTR(PTR_ERR(req));
194 fuse_lookup_init(req, dir, entry, &outarg);
195 request_send(fc, req);
196 err = req->out.h.error;
197 /* Zero nodeid is same as -ENOENT, but with valid timeout */
198 if (!err && outarg.nodeid &&
199 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
201 if (!err && outarg.nodeid) {
202 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
205 fuse_send_forget(fc, req, outarg.nodeid, 1);
206 return ERR_PTR(-ENOMEM);
209 fuse_put_request(fc, req);
210 if (err && err != -ENOENT)
213 if (inode && dir_alias(inode)) {
215 return ERR_PTR(-EIO);
218 entry->d_op = &fuse_dentry_operations;
220 fuse_change_timeout(entry, &outarg);
222 fuse_invalidate_entry_cache(entry);
227 * Synchronous release for the case when something goes wrong in CREATE_OPEN
229 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
230 u64 nodeid, int flags)
232 struct fuse_req *req;
234 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
236 request_send(fc, req);
237 fuse_put_request(fc, req);
241 * Atomic create+open operation
243 * If the filesystem doesn't support this, then fall back to separate
244 * 'mknod' + 'open' requests.
246 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
247 struct nameidata *nd)
251 struct fuse_conn *fc = get_fuse_conn(dir);
252 struct fuse_req *req;
253 struct fuse_req *forget_req;
254 struct fuse_open_in inarg;
255 struct fuse_open_out outopen;
256 struct fuse_entry_out outentry;
257 struct fuse_file *ff;
259 int flags = nd->intent.open.flags - 1;
264 forget_req = fuse_get_req(fc);
265 if (IS_ERR(forget_req))
266 return PTR_ERR(forget_req);
268 req = fuse_get_req(fc);
271 goto out_put_forget_req;
274 ff = fuse_file_alloc();
276 goto out_put_request;
279 memset(&inarg, 0, sizeof(inarg));
282 req->in.h.opcode = FUSE_CREATE;
283 req->in.h.nodeid = get_node_id(dir);
285 req->in.args[0].size = sizeof(inarg);
286 req->in.args[0].value = &inarg;
287 req->in.args[1].size = entry->d_name.len + 1;
288 req->in.args[1].value = entry->d_name.name;
289 req->out.numargs = 2;
290 req->out.args[0].size = sizeof(outentry);
291 req->out.args[0].value = &outentry;
292 req->out.args[1].size = sizeof(outopen);
293 req->out.args[1].value = &outopen;
294 request_send(fc, req);
295 err = req->out.h.error;
303 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
306 fuse_put_request(fc, req);
307 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
310 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
312 fuse_sync_release(fc, ff, outentry.nodeid, flags);
313 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
316 fuse_put_request(fc, forget_req);
317 d_instantiate(entry, inode);
318 fuse_change_timeout(entry, &outentry);
319 file = lookup_instantiate_filp(nd, entry, generic_file_open);
322 fuse_sync_release(fc, ff, outentry.nodeid, flags);
323 return PTR_ERR(file);
325 fuse_finish_open(inode, file, ff, &outopen);
331 fuse_put_request(fc, req);
333 fuse_put_request(fc, forget_req);
338 * Code shared between mknod, mkdir, symlink and link
340 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
341 struct inode *dir, struct dentry *entry,
344 struct fuse_entry_out outarg;
348 req->in.h.nodeid = get_node_id(dir);
349 req->out.numargs = 1;
350 req->out.args[0].size = sizeof(outarg);
351 req->out.args[0].value = &outarg;
352 request_send(fc, req);
353 err = req->out.h.error;
355 fuse_put_request(fc, req);
359 if (invalid_nodeid(outarg.nodeid))
360 goto out_put_request;
362 if ((outarg.attr.mode ^ mode) & S_IFMT)
363 goto out_put_request;
365 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
368 fuse_send_forget(fc, req, outarg.nodeid, 1);
371 fuse_put_request(fc, req);
373 if (dir_alias(inode)) {
378 d_instantiate(entry, inode);
379 fuse_change_timeout(entry, &outarg);
380 fuse_invalidate_attr(dir);
384 fuse_put_request(fc, req);
388 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
391 struct fuse_mknod_in inarg;
392 struct fuse_conn *fc = get_fuse_conn(dir);
393 struct fuse_req *req = fuse_get_req(fc);
397 memset(&inarg, 0, sizeof(inarg));
399 inarg.rdev = new_encode_dev(rdev);
400 req->in.h.opcode = FUSE_MKNOD;
402 req->in.args[0].size = sizeof(inarg);
403 req->in.args[0].value = &inarg;
404 req->in.args[1].size = entry->d_name.len + 1;
405 req->in.args[1].value = entry->d_name.name;
406 return create_new_entry(fc, req, dir, entry, mode);
409 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
410 struct nameidata *nd)
412 if (nd && (nd->flags & LOOKUP_CREATE)) {
413 int err = fuse_create_open(dir, entry, mode, nd);
416 /* Fall back on mknod */
418 return fuse_mknod(dir, entry, mode, 0);
421 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
423 struct fuse_mkdir_in inarg;
424 struct fuse_conn *fc = get_fuse_conn(dir);
425 struct fuse_req *req = fuse_get_req(fc);
429 memset(&inarg, 0, sizeof(inarg));
431 req->in.h.opcode = FUSE_MKDIR;
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, S_IFDIR);
440 static int fuse_symlink(struct inode *dir, struct dentry *entry,
443 struct fuse_conn *fc = get_fuse_conn(dir);
444 unsigned len = strlen(link) + 1;
445 struct fuse_req *req = fuse_get_req(fc);
449 req->in.h.opcode = FUSE_SYMLINK;
451 req->in.args[0].size = entry->d_name.len + 1;
452 req->in.args[0].value = entry->d_name.name;
453 req->in.args[1].size = len;
454 req->in.args[1].value = link;
455 return create_new_entry(fc, req, dir, entry, S_IFLNK);
458 static int fuse_unlink(struct inode *dir, struct dentry *entry)
461 struct fuse_conn *fc = get_fuse_conn(dir);
462 struct fuse_req *req = fuse_get_req(fc);
466 req->in.h.opcode = FUSE_UNLINK;
467 req->in.h.nodeid = get_node_id(dir);
469 req->in.args[0].size = entry->d_name.len + 1;
470 req->in.args[0].value = entry->d_name.name;
471 request_send(fc, req);
472 err = req->out.h.error;
473 fuse_put_request(fc, req);
475 struct inode *inode = entry->d_inode;
477 /* Set nlink to zero so the inode can be cleared, if
478 the inode does have more links this will be
479 discovered at the next lookup/getattr */
481 fuse_invalidate_attr(inode);
482 fuse_invalidate_attr(dir);
483 fuse_invalidate_entry_cache(entry);
484 } else if (err == -EINTR)
485 fuse_invalidate_entry(entry);
489 static int fuse_rmdir(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_RMDIR;
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 entry->d_inode->i_nlink = 0;
507 fuse_invalidate_attr(dir);
508 fuse_invalidate_entry_cache(entry);
509 } else if (err == -EINTR)
510 fuse_invalidate_entry(entry);
514 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
515 struct inode *newdir, struct dentry *newent)
518 struct fuse_rename_in inarg;
519 struct fuse_conn *fc = get_fuse_conn(olddir);
520 struct fuse_req *req = fuse_get_req(fc);
524 memset(&inarg, 0, sizeof(inarg));
525 inarg.newdir = get_node_id(newdir);
526 req->in.h.opcode = FUSE_RENAME;
527 req->in.h.nodeid = get_node_id(olddir);
529 req->in.args[0].size = sizeof(inarg);
530 req->in.args[0].value = &inarg;
531 req->in.args[1].size = oldent->d_name.len + 1;
532 req->in.args[1].value = oldent->d_name.name;
533 req->in.args[2].size = newent->d_name.len + 1;
534 req->in.args[2].value = newent->d_name.name;
535 request_send(fc, req);
536 err = req->out.h.error;
537 fuse_put_request(fc, req);
539 fuse_invalidate_attr(olddir);
540 if (olddir != newdir)
541 fuse_invalidate_attr(newdir);
543 /* newent will end up negative */
545 fuse_invalidate_entry_cache(newent);
546 } else if (err == -EINTR) {
547 /* If request was interrupted, DEITY only knows if the
548 rename actually took place. If the invalidation
549 fails (e.g. some process has CWD under the renamed
550 directory), then there can be inconsistency between
551 the dcache and the real filesystem. Tough luck. */
552 fuse_invalidate_entry(oldent);
554 fuse_invalidate_entry(newent);
560 static int fuse_link(struct dentry *entry, struct inode *newdir,
561 struct dentry *newent)
564 struct fuse_link_in inarg;
565 struct inode *inode = entry->d_inode;
566 struct fuse_conn *fc = get_fuse_conn(inode);
567 struct fuse_req *req = fuse_get_req(fc);
571 memset(&inarg, 0, sizeof(inarg));
572 inarg.oldnodeid = get_node_id(inode);
573 req->in.h.opcode = FUSE_LINK;
575 req->in.args[0].size = sizeof(inarg);
576 req->in.args[0].value = &inarg;
577 req->in.args[1].size = newent->d_name.len + 1;
578 req->in.args[1].value = newent->d_name.name;
579 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
580 /* Contrary to "normal" filesystems it can happen that link
581 makes two "logical" inodes point to the same "physical"
582 inode. We invalidate the attributes of the old one, so it
583 will reflect changes in the backing inode (link count,
586 if (!err || err == -EINTR)
587 fuse_invalidate_attr(inode);
591 int fuse_do_getattr(struct inode *inode)
594 struct fuse_attr_out arg;
595 struct fuse_conn *fc = get_fuse_conn(inode);
596 struct fuse_req *req = fuse_get_req(fc);
600 req->in.h.opcode = FUSE_GETATTR;
601 req->in.h.nodeid = get_node_id(inode);
602 req->out.numargs = 1;
603 req->out.args[0].size = sizeof(arg);
604 req->out.args[0].value = &arg;
605 request_send(fc, req);
606 err = req->out.h.error;
607 fuse_put_request(fc, req);
609 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
610 make_bad_inode(inode);
613 struct fuse_inode *fi = get_fuse_inode(inode);
614 fuse_change_attributes(inode, &arg.attr);
615 fi->i_time = time_to_jiffies(arg.attr_valid,
616 arg.attr_valid_nsec);
623 * Calling into a user-controlled filesystem gives the filesystem
624 * daemon ptrace-like capabilities over the requester process. This
625 * means, that the filesystem daemon is able to record the exact
626 * filesystem operations performed, and can also control the behavior
627 * of the requester process in otherwise impossible ways. For example
628 * it can delay the operation for arbitrary length of time allowing
629 * DoS against the requester.
631 * For this reason only those processes can call into the filesystem,
632 * for which the owner of the mount has ptrace privilege. This
633 * excludes processes started by other users, suid or sgid processes.
635 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
637 if (fc->flags & FUSE_ALLOW_OTHER)
640 if (task->euid == fc->user_id &&
641 task->suid == fc->user_id &&
642 task->uid == fc->user_id &&
643 task->egid == fc->group_id &&
644 task->sgid == fc->group_id &&
645 task->gid == fc->group_id)
652 * Check whether the inode attributes are still valid
654 * If the attribute validity timeout has expired, then fetch the fresh
655 * attributes with a 'getattr' request
657 * I'm not sure why cached attributes are never returned for the root
658 * inode, this is probably being too cautious.
660 static int fuse_revalidate(struct dentry *entry)
662 struct inode *inode = entry->d_inode;
663 struct fuse_inode *fi = get_fuse_inode(inode);
664 struct fuse_conn *fc = get_fuse_conn(inode);
666 if (!fuse_allow_task(fc, current))
668 if (get_node_id(inode) != FUSE_ROOT_ID &&
669 time_before_eq(jiffies, fi->i_time))
672 return fuse_do_getattr(inode);
675 static int fuse_access(struct inode *inode, int mask)
677 struct fuse_conn *fc = get_fuse_conn(inode);
678 struct fuse_req *req;
679 struct fuse_access_in inarg;
685 req = fuse_get_req(fc);
689 memset(&inarg, 0, sizeof(inarg));
691 req->in.h.opcode = FUSE_ACCESS;
692 req->in.h.nodeid = get_node_id(inode);
694 req->in.args[0].size = sizeof(inarg);
695 req->in.args[0].value = &inarg;
696 request_send(fc, req);
697 err = req->out.h.error;
698 fuse_put_request(fc, req);
699 if (err == -ENOSYS) {
707 * Check permission. The two basic access models of FUSE are:
709 * 1) Local access checking ('default_permissions' mount option) based
710 * on file mode. This is the plain old disk filesystem permission
713 * 2) "Remote" access checking, where server is responsible for
714 * checking permission in each inode operation. An exception to this
715 * is if ->permission() was invoked from sys_access() in which case an
716 * access request is sent. Execute permission is still checked
717 * locally based on file mode.
719 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
721 struct fuse_conn *fc = get_fuse_conn(inode);
723 if (!fuse_allow_task(fc, current))
725 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
726 int err = generic_permission(inode, mask, NULL);
728 /* If permission is denied, try to refresh file
729 attributes. This is also needed, because the root
730 node will at first have no permissions */
731 if (err == -EACCES) {
732 err = fuse_do_getattr(inode);
734 err = generic_permission(inode, mask, NULL);
737 /* Note: the opposite of the above test does not
738 exist. So if permissions are revoked this won't be
739 noticed immediately, only after the attribute
740 timeout has expired */
744 int mode = inode->i_mode;
745 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
748 if (nd && (nd->flags & LOOKUP_ACCESS))
749 return fuse_access(inode, mask);
754 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
755 void *dstbuf, filldir_t filldir)
757 while (nbytes >= FUSE_NAME_OFFSET) {
758 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
759 size_t reclen = FUSE_DIRENT_SIZE(dirent);
761 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
766 over = filldir(dstbuf, dirent->name, dirent->namelen,
767 file->f_pos, dirent->ino, dirent->type);
773 file->f_pos = dirent->off;
779 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
784 struct inode *inode = file->f_dentry->d_inode;
785 struct fuse_conn *fc = get_fuse_conn(inode);
786 struct fuse_req *req;
788 if (is_bad_inode(inode))
791 req = fuse_get_req(fc);
795 page = alloc_page(GFP_KERNEL);
797 fuse_put_request(fc, req);
801 req->pages[0] = page;
802 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
803 request_send(fc, req);
804 nbytes = req->out.args[0].size;
805 err = req->out.h.error;
806 fuse_put_request(fc, req);
808 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
812 fuse_invalidate_attr(inode); /* atime changed */
816 static char *read_link(struct dentry *dentry)
818 struct inode *inode = dentry->d_inode;
819 struct fuse_conn *fc = get_fuse_conn(inode);
820 struct fuse_req *req = fuse_get_req(fc);
824 return ERR_PTR(PTR_ERR(req));
826 link = (char *) __get_free_page(GFP_KERNEL);
828 link = ERR_PTR(-ENOMEM);
831 req->in.h.opcode = FUSE_READLINK;
832 req->in.h.nodeid = get_node_id(inode);
834 req->out.numargs = 1;
835 req->out.args[0].size = PAGE_SIZE - 1;
836 req->out.args[0].value = link;
837 request_send(fc, req);
838 if (req->out.h.error) {
839 free_page((unsigned long) link);
840 link = ERR_PTR(req->out.h.error);
842 link[req->out.args[0].size] = '\0';
844 fuse_put_request(fc, req);
845 fuse_invalidate_attr(inode); /* atime changed */
849 static void free_link(char *link)
852 free_page((unsigned long) link);
855 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
857 nd_set_link(nd, read_link(dentry));
861 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
863 free_link(nd_get_link(nd));
866 static int fuse_dir_open(struct inode *inode, struct file *file)
868 return fuse_open_common(inode, file, 1);
871 static int fuse_dir_release(struct inode *inode, struct file *file)
873 return fuse_release_common(inode, file, 1);
876 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
878 /* nfsd can call this with no file */
879 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
882 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
884 unsigned ivalid = iattr->ia_valid;
886 if (ivalid & ATTR_MODE)
887 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
888 if (ivalid & ATTR_UID)
889 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
890 if (ivalid & ATTR_GID)
891 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
892 if (ivalid & ATTR_SIZE)
893 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
894 /* You can only _set_ these together (they may change by themselves) */
895 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
896 arg->valid |= FATTR_ATIME | FATTR_MTIME;
897 arg->atime = iattr->ia_atime.tv_sec;
898 arg->mtime = iattr->ia_mtime.tv_sec;
900 if (ivalid & ATTR_FILE) {
901 struct fuse_file *ff = iattr->ia_file->private_data;
902 arg->valid |= FATTR_FH;
908 * Set attributes, and at the same time refresh them.
910 * Truncation is slightly complicated, because the 'truncate' request
911 * may fail, in which case we don't want to touch the mapping.
912 * vmtruncate() doesn't allow for this case. So do the rlimit
913 * checking by hand and call vmtruncate() only after the file has
914 * actually been truncated.
916 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
918 struct inode *inode = entry->d_inode;
919 struct fuse_conn *fc = get_fuse_conn(inode);
920 struct fuse_inode *fi = get_fuse_inode(inode);
921 struct fuse_req *req;
922 struct fuse_setattr_in inarg;
923 struct fuse_attr_out outarg;
927 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
928 err = inode_change_ok(inode, attr);
933 if (attr->ia_valid & ATTR_SIZE) {
936 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
937 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
938 send_sig(SIGXFSZ, current, 0);
943 req = fuse_get_req(fc);
947 memset(&inarg, 0, sizeof(inarg));
948 iattr_to_fattr(attr, &inarg);
949 req->in.h.opcode = FUSE_SETATTR;
950 req->in.h.nodeid = get_node_id(inode);
952 req->in.args[0].size = sizeof(inarg);
953 req->in.args[0].value = &inarg;
954 req->out.numargs = 1;
955 req->out.args[0].size = sizeof(outarg);
956 req->out.args[0].value = &outarg;
957 request_send(fc, req);
958 err = req->out.h.error;
959 fuse_put_request(fc, req);
961 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
962 make_bad_inode(inode);
966 loff_t origsize = i_size_read(inode);
967 i_size_write(inode, outarg.attr.size);
968 if (origsize > outarg.attr.size)
969 vmtruncate(inode, outarg.attr.size);
971 fuse_change_attributes(inode, &outarg.attr);
972 fi->i_time = time_to_jiffies(outarg.attr_valid,
973 outarg.attr_valid_nsec);
975 } else if (err == -EINTR)
976 fuse_invalidate_attr(inode);
981 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
984 struct inode *inode = entry->d_inode;
985 int err = fuse_revalidate(entry);
987 generic_fillattr(inode, stat);
992 static int fuse_setxattr(struct dentry *entry, const char *name,
993 const void *value, size_t size, int flags)
995 struct inode *inode = entry->d_inode;
996 struct fuse_conn *fc = get_fuse_conn(inode);
997 struct fuse_req *req;
998 struct fuse_setxattr_in inarg;
1001 if (fc->no_setxattr)
1004 req = fuse_get_req(fc);
1006 return PTR_ERR(req);
1008 memset(&inarg, 0, sizeof(inarg));
1010 inarg.flags = flags;
1011 req->in.h.opcode = FUSE_SETXATTR;
1012 req->in.h.nodeid = get_node_id(inode);
1013 req->in.numargs = 3;
1014 req->in.args[0].size = sizeof(inarg);
1015 req->in.args[0].value = &inarg;
1016 req->in.args[1].size = strlen(name) + 1;
1017 req->in.args[1].value = name;
1018 req->in.args[2].size = size;
1019 req->in.args[2].value = value;
1020 request_send(fc, req);
1021 err = req->out.h.error;
1022 fuse_put_request(fc, req);
1023 if (err == -ENOSYS) {
1024 fc->no_setxattr = 1;
1030 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1031 void *value, size_t size)
1033 struct inode *inode = entry->d_inode;
1034 struct fuse_conn *fc = get_fuse_conn(inode);
1035 struct fuse_req *req;
1036 struct fuse_getxattr_in inarg;
1037 struct fuse_getxattr_out outarg;
1040 if (fc->no_getxattr)
1043 req = fuse_get_req(fc);
1045 return PTR_ERR(req);
1047 memset(&inarg, 0, sizeof(inarg));
1049 req->in.h.opcode = FUSE_GETXATTR;
1050 req->in.h.nodeid = get_node_id(inode);
1051 req->in.numargs = 2;
1052 req->in.args[0].size = sizeof(inarg);
1053 req->in.args[0].value = &inarg;
1054 req->in.args[1].size = strlen(name) + 1;
1055 req->in.args[1].value = name;
1056 /* This is really two different operations rolled into one */
1057 req->out.numargs = 1;
1059 req->out.argvar = 1;
1060 req->out.args[0].size = size;
1061 req->out.args[0].value = value;
1063 req->out.args[0].size = sizeof(outarg);
1064 req->out.args[0].value = &outarg;
1066 request_send(fc, req);
1067 ret = req->out.h.error;
1069 ret = size ? req->out.args[0].size : outarg.size;
1071 if (ret == -ENOSYS) {
1072 fc->no_getxattr = 1;
1076 fuse_put_request(fc, req);
1080 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1082 struct inode *inode = entry->d_inode;
1083 struct fuse_conn *fc = get_fuse_conn(inode);
1084 struct fuse_req *req;
1085 struct fuse_getxattr_in inarg;
1086 struct fuse_getxattr_out outarg;
1089 if (fc->no_listxattr)
1092 req = fuse_get_req(fc);
1094 return PTR_ERR(req);
1096 memset(&inarg, 0, sizeof(inarg));
1098 req->in.h.opcode = FUSE_LISTXATTR;
1099 req->in.h.nodeid = get_node_id(inode);
1100 req->in.numargs = 1;
1101 req->in.args[0].size = sizeof(inarg);
1102 req->in.args[0].value = &inarg;
1103 /* This is really two different operations rolled into one */
1104 req->out.numargs = 1;
1106 req->out.argvar = 1;
1107 req->out.args[0].size = size;
1108 req->out.args[0].value = list;
1110 req->out.args[0].size = sizeof(outarg);
1111 req->out.args[0].value = &outarg;
1113 request_send(fc, req);
1114 ret = req->out.h.error;
1116 ret = size ? req->out.args[0].size : outarg.size;
1118 if (ret == -ENOSYS) {
1119 fc->no_listxattr = 1;
1123 fuse_put_request(fc, req);
1127 static int fuse_removexattr(struct dentry *entry, const char *name)
1129 struct inode *inode = entry->d_inode;
1130 struct fuse_conn *fc = get_fuse_conn(inode);
1131 struct fuse_req *req;
1134 if (fc->no_removexattr)
1137 req = fuse_get_req(fc);
1139 return PTR_ERR(req);
1141 req->in.h.opcode = FUSE_REMOVEXATTR;
1142 req->in.h.nodeid = get_node_id(inode);
1143 req->in.numargs = 1;
1144 req->in.args[0].size = strlen(name) + 1;
1145 req->in.args[0].value = name;
1146 request_send(fc, req);
1147 err = req->out.h.error;
1148 fuse_put_request(fc, req);
1149 if (err == -ENOSYS) {
1150 fc->no_removexattr = 1;
1156 static struct inode_operations fuse_dir_inode_operations = {
1157 .lookup = fuse_lookup,
1158 .mkdir = fuse_mkdir,
1159 .symlink = fuse_symlink,
1160 .unlink = fuse_unlink,
1161 .rmdir = fuse_rmdir,
1162 .rename = fuse_rename,
1164 .setattr = fuse_setattr,
1165 .create = fuse_create,
1166 .mknod = fuse_mknod,
1167 .permission = fuse_permission,
1168 .getattr = fuse_getattr,
1169 .setxattr = fuse_setxattr,
1170 .getxattr = fuse_getxattr,
1171 .listxattr = fuse_listxattr,
1172 .removexattr = fuse_removexattr,
1175 static const struct file_operations fuse_dir_operations = {
1176 .llseek = generic_file_llseek,
1177 .read = generic_read_dir,
1178 .readdir = fuse_readdir,
1179 .open = fuse_dir_open,
1180 .release = fuse_dir_release,
1181 .fsync = fuse_dir_fsync,
1184 static struct inode_operations fuse_common_inode_operations = {
1185 .setattr = fuse_setattr,
1186 .permission = fuse_permission,
1187 .getattr = fuse_getattr,
1188 .setxattr = fuse_setxattr,
1189 .getxattr = fuse_getxattr,
1190 .listxattr = fuse_listxattr,
1191 .removexattr = fuse_removexattr,
1194 static struct inode_operations fuse_symlink_inode_operations = {
1195 .setattr = fuse_setattr,
1196 .follow_link = fuse_follow_link,
1197 .put_link = fuse_put_link,
1198 .readlink = generic_readlink,
1199 .getattr = fuse_getattr,
1200 .setxattr = fuse_setxattr,
1201 .getxattr = fuse_getxattr,
1202 .listxattr = fuse_listxattr,
1203 .removexattr = fuse_removexattr,
1206 void fuse_init_common(struct inode *inode)
1208 inode->i_op = &fuse_common_inode_operations;
1211 void fuse_init_dir(struct inode *inode)
1213 inode->i_op = &fuse_dir_inode_operations;
1214 inode->i_fop = &fuse_dir_operations;
1217 void fuse_init_symlink(struct inode *inode)
1219 inode->i_op = &fuse_symlink_inode_operations;