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;
141 struct dentry *parent;
143 /* Doesn't hurt to "reset" the validity timeout */
144 fuse_invalidate_entry_cache(entry);
146 /* For negative dentries, always do a fresh lookup */
150 fc = get_fuse_conn(inode);
151 req = fuse_get_req(fc);
155 parent = dget_parent(entry);
156 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
157 request_send(fc, req);
159 err = req->out.h.error;
160 /* Zero nodeid is same as -ENOENT */
161 if (!err && !outarg.nodeid)
164 struct fuse_inode *fi = get_fuse_inode(inode);
165 if (outarg.nodeid != get_node_id(inode)) {
166 fuse_send_forget(fc, req, outarg.nodeid, 1);
169 spin_lock(&fc->lock);
171 spin_unlock(&fc->lock);
173 fuse_put_request(fc, req);
174 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
177 fuse_change_attributes(inode, &outarg.attr);
178 fuse_change_timeout(entry, &outarg);
183 static int invalid_nodeid(u64 nodeid)
185 return !nodeid || nodeid == FUSE_ROOT_ID;
188 static struct dentry_operations fuse_dentry_operations = {
189 .d_revalidate = fuse_dentry_revalidate,
192 static int valid_mode(int m)
194 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
195 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
199 * Add a directory inode to a dentry, ensuring that no other dentry
200 * refers to this inode. Called with fc->inst_mutex.
202 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
204 struct dentry *alias = d_find_alias(inode);
206 /* This tries to shrink the subtree below alias */
207 fuse_invalidate_entry(alias);
209 if (!list_empty(&inode->i_dentry))
216 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
217 struct nameidata *nd)
220 struct fuse_entry_out outarg;
221 struct inode *inode = NULL;
222 struct fuse_conn *fc = get_fuse_conn(dir);
223 struct fuse_req *req;
225 if (entry->d_name.len > FUSE_NAME_MAX)
226 return ERR_PTR(-ENAMETOOLONG);
228 req = fuse_get_req(fc);
230 return ERR_PTR(PTR_ERR(req));
232 fuse_lookup_init(req, dir, entry, &outarg);
233 request_send(fc, req);
234 err = req->out.h.error;
235 /* Zero nodeid is same as -ENOENT, but with valid timeout */
236 if (!err && outarg.nodeid &&
237 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
239 if (!err && outarg.nodeid) {
240 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
243 fuse_send_forget(fc, req, outarg.nodeid, 1);
244 return ERR_PTR(-ENOMEM);
247 fuse_put_request(fc, req);
248 if (err && err != -ENOENT)
251 if (inode && S_ISDIR(inode->i_mode)) {
252 mutex_lock(&fc->inst_mutex);
253 err = fuse_d_add_directory(entry, inode);
254 mutex_unlock(&fc->inst_mutex);
262 entry->d_op = &fuse_dentry_operations;
264 fuse_change_timeout(entry, &outarg);
266 fuse_invalidate_entry_cache(entry);
271 * Synchronous release for the case when something goes wrong in CREATE_OPEN
273 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
274 u64 nodeid, int flags)
276 struct fuse_req *req;
278 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
280 request_send(fc, req);
281 fuse_put_request(fc, req);
285 * Atomic create+open operation
287 * If the filesystem doesn't support this, then fall back to separate
288 * 'mknod' + 'open' requests.
290 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
291 struct nameidata *nd)
295 struct fuse_conn *fc = get_fuse_conn(dir);
296 struct fuse_req *req;
297 struct fuse_req *forget_req;
298 struct fuse_open_in inarg;
299 struct fuse_open_out outopen;
300 struct fuse_entry_out outentry;
301 struct fuse_file *ff;
303 int flags = nd->intent.open.flags - 1;
308 forget_req = fuse_get_req(fc);
309 if (IS_ERR(forget_req))
310 return PTR_ERR(forget_req);
312 req = fuse_get_req(fc);
315 goto out_put_forget_req;
318 ff = fuse_file_alloc();
320 goto out_put_request;
323 memset(&inarg, 0, sizeof(inarg));
326 req->in.h.opcode = FUSE_CREATE;
327 req->in.h.nodeid = get_node_id(dir);
329 req->in.args[0].size = sizeof(inarg);
330 req->in.args[0].value = &inarg;
331 req->in.args[1].size = entry->d_name.len + 1;
332 req->in.args[1].value = entry->d_name.name;
333 req->out.numargs = 2;
334 req->out.args[0].size = sizeof(outentry);
335 req->out.args[0].value = &outentry;
336 req->out.args[1].size = sizeof(outopen);
337 req->out.args[1].value = &outopen;
338 request_send(fc, req);
339 err = req->out.h.error;
347 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
350 fuse_put_request(fc, req);
351 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
354 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
356 fuse_sync_release(fc, ff, outentry.nodeid, flags);
357 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
360 fuse_put_request(fc, forget_req);
361 d_instantiate(entry, inode);
362 fuse_change_timeout(entry, &outentry);
363 file = lookup_instantiate_filp(nd, entry, generic_file_open);
366 fuse_sync_release(fc, ff, outentry.nodeid, flags);
367 return PTR_ERR(file);
369 fuse_finish_open(inode, file, ff, &outopen);
375 fuse_put_request(fc, req);
377 fuse_put_request(fc, forget_req);
382 * Code shared between mknod, mkdir, symlink and link
384 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
385 struct inode *dir, struct dentry *entry,
388 struct fuse_entry_out outarg;
392 req->in.h.nodeid = get_node_id(dir);
393 req->out.numargs = 1;
394 req->out.args[0].size = sizeof(outarg);
395 req->out.args[0].value = &outarg;
396 request_send(fc, req);
397 err = req->out.h.error;
399 fuse_put_request(fc, req);
403 if (invalid_nodeid(outarg.nodeid))
404 goto out_put_request;
406 if ((outarg.attr.mode ^ mode) & S_IFMT)
407 goto out_put_request;
409 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
412 fuse_send_forget(fc, req, outarg.nodeid, 1);
415 fuse_put_request(fc, req);
417 if (S_ISDIR(inode->i_mode)) {
418 struct dentry *alias;
419 mutex_lock(&fc->inst_mutex);
420 alias = d_find_alias(inode);
422 /* New directory must have moved since mkdir */
423 mutex_unlock(&fc->inst_mutex);
428 d_instantiate(entry, inode);
429 mutex_unlock(&fc->inst_mutex);
431 d_instantiate(entry, inode);
433 fuse_change_timeout(entry, &outarg);
434 fuse_invalidate_attr(dir);
438 fuse_put_request(fc, req);
442 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
445 struct fuse_mknod_in inarg;
446 struct fuse_conn *fc = get_fuse_conn(dir);
447 struct fuse_req *req = fuse_get_req(fc);
451 memset(&inarg, 0, sizeof(inarg));
453 inarg.rdev = new_encode_dev(rdev);
454 req->in.h.opcode = FUSE_MKNOD;
456 req->in.args[0].size = sizeof(inarg);
457 req->in.args[0].value = &inarg;
458 req->in.args[1].size = entry->d_name.len + 1;
459 req->in.args[1].value = entry->d_name.name;
460 return create_new_entry(fc, req, dir, entry, mode);
463 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
464 struct nameidata *nd)
466 if (nd && (nd->flags & LOOKUP_CREATE)) {
467 int err = fuse_create_open(dir, entry, mode, nd);
470 /* Fall back on mknod */
472 return fuse_mknod(dir, entry, mode, 0);
475 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
477 struct fuse_mkdir_in inarg;
478 struct fuse_conn *fc = get_fuse_conn(dir);
479 struct fuse_req *req = fuse_get_req(fc);
483 memset(&inarg, 0, sizeof(inarg));
485 req->in.h.opcode = FUSE_MKDIR;
487 req->in.args[0].size = sizeof(inarg);
488 req->in.args[0].value = &inarg;
489 req->in.args[1].size = entry->d_name.len + 1;
490 req->in.args[1].value = entry->d_name.name;
491 return create_new_entry(fc, req, dir, entry, S_IFDIR);
494 static int fuse_symlink(struct inode *dir, struct dentry *entry,
497 struct fuse_conn *fc = get_fuse_conn(dir);
498 unsigned len = strlen(link) + 1;
499 struct fuse_req *req = fuse_get_req(fc);
503 req->in.h.opcode = FUSE_SYMLINK;
505 req->in.args[0].size = entry->d_name.len + 1;
506 req->in.args[0].value = entry->d_name.name;
507 req->in.args[1].size = len;
508 req->in.args[1].value = link;
509 return create_new_entry(fc, req, dir, entry, S_IFLNK);
512 static int fuse_unlink(struct inode *dir, struct dentry *entry)
515 struct fuse_conn *fc = get_fuse_conn(dir);
516 struct fuse_req *req = fuse_get_req(fc);
520 req->in.h.opcode = FUSE_UNLINK;
521 req->in.h.nodeid = get_node_id(dir);
523 req->in.args[0].size = entry->d_name.len + 1;
524 req->in.args[0].value = entry->d_name.name;
525 request_send(fc, req);
526 err = req->out.h.error;
527 fuse_put_request(fc, req);
529 struct inode *inode = entry->d_inode;
531 /* Set nlink to zero so the inode can be cleared, if
532 the inode does have more links this will be
533 discovered at the next lookup/getattr */
535 fuse_invalidate_attr(inode);
536 fuse_invalidate_attr(dir);
537 fuse_invalidate_entry_cache(entry);
538 } else if (err == -EINTR)
539 fuse_invalidate_entry(entry);
543 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
546 struct fuse_conn *fc = get_fuse_conn(dir);
547 struct fuse_req *req = fuse_get_req(fc);
551 req->in.h.opcode = FUSE_RMDIR;
552 req->in.h.nodeid = get_node_id(dir);
554 req->in.args[0].size = entry->d_name.len + 1;
555 req->in.args[0].value = entry->d_name.name;
556 request_send(fc, req);
557 err = req->out.h.error;
558 fuse_put_request(fc, req);
560 clear_nlink(entry->d_inode);
561 fuse_invalidate_attr(dir);
562 fuse_invalidate_entry_cache(entry);
563 } else if (err == -EINTR)
564 fuse_invalidate_entry(entry);
568 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
569 struct inode *newdir, struct dentry *newent)
572 struct fuse_rename_in inarg;
573 struct fuse_conn *fc = get_fuse_conn(olddir);
574 struct fuse_req *req = fuse_get_req(fc);
578 memset(&inarg, 0, sizeof(inarg));
579 inarg.newdir = get_node_id(newdir);
580 req->in.h.opcode = FUSE_RENAME;
581 req->in.h.nodeid = get_node_id(olddir);
583 req->in.args[0].size = sizeof(inarg);
584 req->in.args[0].value = &inarg;
585 req->in.args[1].size = oldent->d_name.len + 1;
586 req->in.args[1].value = oldent->d_name.name;
587 req->in.args[2].size = newent->d_name.len + 1;
588 req->in.args[2].value = newent->d_name.name;
589 request_send(fc, req);
590 err = req->out.h.error;
591 fuse_put_request(fc, req);
593 fuse_invalidate_attr(olddir);
594 if (olddir != newdir)
595 fuse_invalidate_attr(newdir);
597 /* newent will end up negative */
599 fuse_invalidate_entry_cache(newent);
600 } else if (err == -EINTR) {
601 /* If request was interrupted, DEITY only knows if the
602 rename actually took place. If the invalidation
603 fails (e.g. some process has CWD under the renamed
604 directory), then there can be inconsistency between
605 the dcache and the real filesystem. Tough luck. */
606 fuse_invalidate_entry(oldent);
608 fuse_invalidate_entry(newent);
614 static int fuse_link(struct dentry *entry, struct inode *newdir,
615 struct dentry *newent)
618 struct fuse_link_in inarg;
619 struct inode *inode = entry->d_inode;
620 struct fuse_conn *fc = get_fuse_conn(inode);
621 struct fuse_req *req = fuse_get_req(fc);
625 memset(&inarg, 0, sizeof(inarg));
626 inarg.oldnodeid = get_node_id(inode);
627 req->in.h.opcode = FUSE_LINK;
629 req->in.args[0].size = sizeof(inarg);
630 req->in.args[0].value = &inarg;
631 req->in.args[1].size = newent->d_name.len + 1;
632 req->in.args[1].value = newent->d_name.name;
633 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
634 /* Contrary to "normal" filesystems it can happen that link
635 makes two "logical" inodes point to the same "physical"
636 inode. We invalidate the attributes of the old one, so it
637 will reflect changes in the backing inode (link count,
640 if (!err || err == -EINTR)
641 fuse_invalidate_attr(inode);
645 int fuse_do_getattr(struct inode *inode)
648 struct fuse_attr_out arg;
649 struct fuse_conn *fc = get_fuse_conn(inode);
650 struct fuse_req *req = fuse_get_req(fc);
654 req->in.h.opcode = FUSE_GETATTR;
655 req->in.h.nodeid = get_node_id(inode);
656 req->out.numargs = 1;
657 req->out.args[0].size = sizeof(arg);
658 req->out.args[0].value = &arg;
659 request_send(fc, req);
660 err = req->out.h.error;
661 fuse_put_request(fc, req);
663 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
664 make_bad_inode(inode);
667 struct fuse_inode *fi = get_fuse_inode(inode);
668 fuse_change_attributes(inode, &arg.attr);
669 fi->i_time = time_to_jiffies(arg.attr_valid,
670 arg.attr_valid_nsec);
677 * Calling into a user-controlled filesystem gives the filesystem
678 * daemon ptrace-like capabilities over the requester process. This
679 * means, that the filesystem daemon is able to record the exact
680 * filesystem operations performed, and can also control the behavior
681 * of the requester process in otherwise impossible ways. For example
682 * it can delay the operation for arbitrary length of time allowing
683 * DoS against the requester.
685 * For this reason only those processes can call into the filesystem,
686 * for which the owner of the mount has ptrace privilege. This
687 * excludes processes started by other users, suid or sgid processes.
689 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
691 if (fc->flags & FUSE_ALLOW_OTHER)
694 if (task->euid == fc->user_id &&
695 task->suid == fc->user_id &&
696 task->uid == fc->user_id &&
697 task->egid == fc->group_id &&
698 task->sgid == fc->group_id &&
699 task->gid == fc->group_id)
706 * Check whether the inode attributes are still valid
708 * If the attribute validity timeout has expired, then fetch the fresh
709 * attributes with a 'getattr' request
711 * I'm not sure why cached attributes are never returned for the root
712 * inode, this is probably being too cautious.
714 static int fuse_revalidate(struct dentry *entry)
716 struct inode *inode = entry->d_inode;
717 struct fuse_inode *fi = get_fuse_inode(inode);
718 struct fuse_conn *fc = get_fuse_conn(inode);
720 if (!fuse_allow_task(fc, current))
722 if (get_node_id(inode) != FUSE_ROOT_ID &&
723 fi->i_time >= get_jiffies_64())
726 return fuse_do_getattr(inode);
729 static int fuse_access(struct inode *inode, int mask)
731 struct fuse_conn *fc = get_fuse_conn(inode);
732 struct fuse_req *req;
733 struct fuse_access_in inarg;
739 req = fuse_get_req(fc);
743 memset(&inarg, 0, sizeof(inarg));
745 req->in.h.opcode = FUSE_ACCESS;
746 req->in.h.nodeid = get_node_id(inode);
748 req->in.args[0].size = sizeof(inarg);
749 req->in.args[0].value = &inarg;
750 request_send(fc, req);
751 err = req->out.h.error;
752 fuse_put_request(fc, req);
753 if (err == -ENOSYS) {
761 * Check permission. The two basic access models of FUSE are:
763 * 1) Local access checking ('default_permissions' mount option) based
764 * on file mode. This is the plain old disk filesystem permission
767 * 2) "Remote" access checking, where server is responsible for
768 * checking permission in each inode operation. An exception to this
769 * is if ->permission() was invoked from sys_access() in which case an
770 * access request is sent. Execute permission is still checked
771 * locally based on file mode.
773 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
775 struct fuse_conn *fc = get_fuse_conn(inode);
777 if (!fuse_allow_task(fc, current))
779 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
780 int err = generic_permission(inode, mask, NULL);
782 /* If permission is denied, try to refresh file
783 attributes. This is also needed, because the root
784 node will at first have no permissions */
785 if (err == -EACCES) {
786 err = fuse_do_getattr(inode);
788 err = generic_permission(inode, mask, NULL);
791 /* Note: the opposite of the above test does not
792 exist. So if permissions are revoked this won't be
793 noticed immediately, only after the attribute
794 timeout has expired */
798 int mode = inode->i_mode;
799 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
802 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
803 return fuse_access(inode, mask);
808 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
809 void *dstbuf, filldir_t filldir)
811 while (nbytes >= FUSE_NAME_OFFSET) {
812 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
813 size_t reclen = FUSE_DIRENT_SIZE(dirent);
815 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
820 over = filldir(dstbuf, dirent->name, dirent->namelen,
821 file->f_pos, dirent->ino, dirent->type);
827 file->f_pos = dirent->off;
833 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
838 struct inode *inode = file->f_dentry->d_inode;
839 struct fuse_conn *fc = get_fuse_conn(inode);
840 struct fuse_req *req;
842 if (is_bad_inode(inode))
845 req = fuse_get_req(fc);
849 page = alloc_page(GFP_KERNEL);
851 fuse_put_request(fc, req);
855 req->pages[0] = page;
856 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
857 request_send(fc, req);
858 nbytes = req->out.args[0].size;
859 err = req->out.h.error;
860 fuse_put_request(fc, req);
862 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
866 fuse_invalidate_attr(inode); /* atime changed */
870 static char *read_link(struct dentry *dentry)
872 struct inode *inode = dentry->d_inode;
873 struct fuse_conn *fc = get_fuse_conn(inode);
874 struct fuse_req *req = fuse_get_req(fc);
878 return ERR_PTR(PTR_ERR(req));
880 link = (char *) __get_free_page(GFP_KERNEL);
882 link = ERR_PTR(-ENOMEM);
885 req->in.h.opcode = FUSE_READLINK;
886 req->in.h.nodeid = get_node_id(inode);
888 req->out.numargs = 1;
889 req->out.args[0].size = PAGE_SIZE - 1;
890 req->out.args[0].value = link;
891 request_send(fc, req);
892 if (req->out.h.error) {
893 free_page((unsigned long) link);
894 link = ERR_PTR(req->out.h.error);
896 link[req->out.args[0].size] = '\0';
898 fuse_put_request(fc, req);
899 fuse_invalidate_attr(inode); /* atime changed */
903 static void free_link(char *link)
906 free_page((unsigned long) link);
909 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
911 nd_set_link(nd, read_link(dentry));
915 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
917 free_link(nd_get_link(nd));
920 static int fuse_dir_open(struct inode *inode, struct file *file)
922 return fuse_open_common(inode, file, 1);
925 static int fuse_dir_release(struct inode *inode, struct file *file)
927 return fuse_release_common(inode, file, 1);
930 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
932 /* nfsd can call this with no file */
933 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
936 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
938 unsigned ivalid = iattr->ia_valid;
940 if (ivalid & ATTR_MODE)
941 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
942 if (ivalid & ATTR_UID)
943 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
944 if (ivalid & ATTR_GID)
945 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
946 if (ivalid & ATTR_SIZE)
947 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
948 /* You can only _set_ these together (they may change by themselves) */
949 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
950 arg->valid |= FATTR_ATIME | FATTR_MTIME;
951 arg->atime = iattr->ia_atime.tv_sec;
952 arg->mtime = iattr->ia_mtime.tv_sec;
954 if (ivalid & ATTR_FILE) {
955 struct fuse_file *ff = iattr->ia_file->private_data;
956 arg->valid |= FATTR_FH;
961 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
963 struct fuse_conn *fc = get_fuse_conn(inode);
966 spin_lock(&fc->lock);
967 need_trunc = inode->i_size > offset;
968 i_size_write(inode, offset);
969 spin_unlock(&fc->lock);
972 struct address_space *mapping = inode->i_mapping;
973 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
974 truncate_inode_pages(mapping, offset);
979 * Set attributes, and at the same time refresh them.
981 * Truncation is slightly complicated, because the 'truncate' request
982 * may fail, in which case we don't want to touch the mapping.
983 * vmtruncate() doesn't allow for this case, so do the rlimit checking
984 * and the actual truncation by hand.
986 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
988 struct inode *inode = entry->d_inode;
989 struct fuse_conn *fc = get_fuse_conn(inode);
990 struct fuse_inode *fi = get_fuse_inode(inode);
991 struct fuse_req *req;
992 struct fuse_setattr_in inarg;
993 struct fuse_attr_out outarg;
997 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
998 err = inode_change_ok(inode, attr);
1003 if (attr->ia_valid & ATTR_SIZE) {
1004 unsigned long limit;
1006 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1007 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1008 send_sig(SIGXFSZ, current, 0);
1013 req = fuse_get_req(fc);
1015 return PTR_ERR(req);
1017 memset(&inarg, 0, sizeof(inarg));
1018 iattr_to_fattr(attr, &inarg);
1019 req->in.h.opcode = FUSE_SETATTR;
1020 req->in.h.nodeid = get_node_id(inode);
1021 req->in.numargs = 1;
1022 req->in.args[0].size = sizeof(inarg);
1023 req->in.args[0].value = &inarg;
1024 req->out.numargs = 1;
1025 req->out.args[0].size = sizeof(outarg);
1026 req->out.args[0].value = &outarg;
1027 request_send(fc, req);
1028 err = req->out.h.error;
1029 fuse_put_request(fc, req);
1031 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1032 make_bad_inode(inode);
1036 fuse_vmtruncate(inode, outarg.attr.size);
1037 fuse_change_attributes(inode, &outarg.attr);
1038 fi->i_time = time_to_jiffies(outarg.attr_valid,
1039 outarg.attr_valid_nsec);
1041 } else if (err == -EINTR)
1042 fuse_invalidate_attr(inode);
1047 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1050 struct inode *inode = entry->d_inode;
1051 int err = fuse_revalidate(entry);
1053 generic_fillattr(inode, stat);
1058 static int fuse_setxattr(struct dentry *entry, const char *name,
1059 const void *value, size_t size, int flags)
1061 struct inode *inode = entry->d_inode;
1062 struct fuse_conn *fc = get_fuse_conn(inode);
1063 struct fuse_req *req;
1064 struct fuse_setxattr_in inarg;
1067 if (fc->no_setxattr)
1070 req = fuse_get_req(fc);
1072 return PTR_ERR(req);
1074 memset(&inarg, 0, sizeof(inarg));
1076 inarg.flags = flags;
1077 req->in.h.opcode = FUSE_SETXATTR;
1078 req->in.h.nodeid = get_node_id(inode);
1079 req->in.numargs = 3;
1080 req->in.args[0].size = sizeof(inarg);
1081 req->in.args[0].value = &inarg;
1082 req->in.args[1].size = strlen(name) + 1;
1083 req->in.args[1].value = name;
1084 req->in.args[2].size = size;
1085 req->in.args[2].value = value;
1086 request_send(fc, req);
1087 err = req->out.h.error;
1088 fuse_put_request(fc, req);
1089 if (err == -ENOSYS) {
1090 fc->no_setxattr = 1;
1096 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1097 void *value, size_t size)
1099 struct inode *inode = entry->d_inode;
1100 struct fuse_conn *fc = get_fuse_conn(inode);
1101 struct fuse_req *req;
1102 struct fuse_getxattr_in inarg;
1103 struct fuse_getxattr_out outarg;
1106 if (fc->no_getxattr)
1109 req = fuse_get_req(fc);
1111 return PTR_ERR(req);
1113 memset(&inarg, 0, sizeof(inarg));
1115 req->in.h.opcode = FUSE_GETXATTR;
1116 req->in.h.nodeid = get_node_id(inode);
1117 req->in.numargs = 2;
1118 req->in.args[0].size = sizeof(inarg);
1119 req->in.args[0].value = &inarg;
1120 req->in.args[1].size = strlen(name) + 1;
1121 req->in.args[1].value = name;
1122 /* This is really two different operations rolled into one */
1123 req->out.numargs = 1;
1125 req->out.argvar = 1;
1126 req->out.args[0].size = size;
1127 req->out.args[0].value = value;
1129 req->out.args[0].size = sizeof(outarg);
1130 req->out.args[0].value = &outarg;
1132 request_send(fc, req);
1133 ret = req->out.h.error;
1135 ret = size ? req->out.args[0].size : outarg.size;
1137 if (ret == -ENOSYS) {
1138 fc->no_getxattr = 1;
1142 fuse_put_request(fc, req);
1146 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1148 struct inode *inode = entry->d_inode;
1149 struct fuse_conn *fc = get_fuse_conn(inode);
1150 struct fuse_req *req;
1151 struct fuse_getxattr_in inarg;
1152 struct fuse_getxattr_out outarg;
1155 if (fc->no_listxattr)
1158 req = fuse_get_req(fc);
1160 return PTR_ERR(req);
1162 memset(&inarg, 0, sizeof(inarg));
1164 req->in.h.opcode = FUSE_LISTXATTR;
1165 req->in.h.nodeid = get_node_id(inode);
1166 req->in.numargs = 1;
1167 req->in.args[0].size = sizeof(inarg);
1168 req->in.args[0].value = &inarg;
1169 /* This is really two different operations rolled into one */
1170 req->out.numargs = 1;
1172 req->out.argvar = 1;
1173 req->out.args[0].size = size;
1174 req->out.args[0].value = list;
1176 req->out.args[0].size = sizeof(outarg);
1177 req->out.args[0].value = &outarg;
1179 request_send(fc, req);
1180 ret = req->out.h.error;
1182 ret = size ? req->out.args[0].size : outarg.size;
1184 if (ret == -ENOSYS) {
1185 fc->no_listxattr = 1;
1189 fuse_put_request(fc, req);
1193 static int fuse_removexattr(struct dentry *entry, const char *name)
1195 struct inode *inode = entry->d_inode;
1196 struct fuse_conn *fc = get_fuse_conn(inode);
1197 struct fuse_req *req;
1200 if (fc->no_removexattr)
1203 req = fuse_get_req(fc);
1205 return PTR_ERR(req);
1207 req->in.h.opcode = FUSE_REMOVEXATTR;
1208 req->in.h.nodeid = get_node_id(inode);
1209 req->in.numargs = 1;
1210 req->in.args[0].size = strlen(name) + 1;
1211 req->in.args[0].value = name;
1212 request_send(fc, req);
1213 err = req->out.h.error;
1214 fuse_put_request(fc, req);
1215 if (err == -ENOSYS) {
1216 fc->no_removexattr = 1;
1222 static struct inode_operations fuse_dir_inode_operations = {
1223 .lookup = fuse_lookup,
1224 .mkdir = fuse_mkdir,
1225 .symlink = fuse_symlink,
1226 .unlink = fuse_unlink,
1227 .rmdir = fuse_rmdir,
1228 .rename = fuse_rename,
1230 .setattr = fuse_setattr,
1231 .create = fuse_create,
1232 .mknod = fuse_mknod,
1233 .permission = fuse_permission,
1234 .getattr = fuse_getattr,
1235 .setxattr = fuse_setxattr,
1236 .getxattr = fuse_getxattr,
1237 .listxattr = fuse_listxattr,
1238 .removexattr = fuse_removexattr,
1241 static const struct file_operations fuse_dir_operations = {
1242 .llseek = generic_file_llseek,
1243 .read = generic_read_dir,
1244 .readdir = fuse_readdir,
1245 .open = fuse_dir_open,
1246 .release = fuse_dir_release,
1247 .fsync = fuse_dir_fsync,
1250 static struct inode_operations fuse_common_inode_operations = {
1251 .setattr = fuse_setattr,
1252 .permission = fuse_permission,
1253 .getattr = fuse_getattr,
1254 .setxattr = fuse_setxattr,
1255 .getxattr = fuse_getxattr,
1256 .listxattr = fuse_listxattr,
1257 .removexattr = fuse_removexattr,
1260 static struct inode_operations fuse_symlink_inode_operations = {
1261 .setattr = fuse_setattr,
1262 .follow_link = fuse_follow_link,
1263 .put_link = fuse_put_link,
1264 .readlink = generic_readlink,
1265 .getattr = fuse_getattr,
1266 .setxattr = fuse_setxattr,
1267 .getxattr = fuse_getxattr,
1268 .listxattr = fuse_listxattr,
1269 .removexattr = fuse_removexattr,
1272 void fuse_init_common(struct inode *inode)
1274 inode->i_op = &fuse_common_inode_operations;
1277 void fuse_init_dir(struct inode *inode)
1279 inode->i_op = &fuse_dir_inode_operations;
1280 inode->i_fop = &fuse_dir_operations;
1283 void fuse_init_symlink(struct inode *inode)
1285 inode->i_op = &fuse_symlink_inode_operations;