[SPARC64]: Update defconfig.
[linux-2.6/verdex.git] / fs / fuse / dir.c
blobc72a8a97935c9733af8522eeb2533bd83c930ba1
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
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*
34 * replies
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);
39 if (entry->d_inode)
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
59 * lookup)
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)
72 d_invalidate(entry);
73 fuse_invalidate_entry_cache(entry);
76 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
77 struct dentry *entry,
78 struct fuse_entry_out *outarg)
80 req->in.h.opcode = FUSE_LOOKUP;
81 req->in.h.nodeid = get_node_id(dir);
82 req->inode = dir;
83 req->in.numargs = 1;
84 req->in.args[0].size = entry->d_name.len + 1;
85 req->in.args[0].value = entry->d_name.name;
86 req->out.numargs = 1;
87 req->out.args[0].size = sizeof(struct fuse_entry_out);
88 req->out.args[0].value = outarg;
92 * Check whether the dentry is still valid
94 * If the entry validity timeout has expired and the dentry is
95 * positive, try to redo the lookup. If the lookup results in a
96 * different inode, then let the VFS invalidate the dentry and redo
97 * the lookup once more. If the lookup results in the same inode,
98 * then refresh the attributes, timeouts and mark the dentry valid.
100 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
102 struct inode *inode = entry->d_inode;
104 if (inode && is_bad_inode(inode))
105 return 0;
106 else if (time_after(jiffies, entry->d_time)) {
107 int err;
108 struct fuse_entry_out outarg;
109 struct fuse_conn *fc;
110 struct fuse_req *req;
112 /* Doesn't hurt to "reset" the validity timeout */
113 fuse_invalidate_entry_cache(entry);
115 /* For negative dentries, always do a fresh lookup */
116 if (!inode)
117 return 0;
119 fc = get_fuse_conn(inode);
120 req = fuse_get_request(fc);
121 if (!req)
122 return 0;
124 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
125 request_send(fc, req);
126 err = req->out.h.error;
127 /* Zero nodeid is same as -ENOENT */
128 if (!err && !outarg.nodeid)
129 err = -ENOENT;
130 if (!err) {
131 struct fuse_inode *fi = get_fuse_inode(inode);
132 if (outarg.nodeid != get_node_id(inode)) {
133 fuse_send_forget(fc, req, outarg.nodeid, 1);
134 return 0;
136 fi->nlookup ++;
138 fuse_put_request(fc, req);
139 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
140 return 0;
142 fuse_change_attributes(inode, &outarg.attr);
143 fuse_change_timeout(entry, &outarg);
145 return 1;
149 * Check if there's already a hashed alias of this directory inode.
150 * If yes, then lookup and mkdir must not create a new alias.
152 static int dir_alias(struct inode *inode)
154 if (S_ISDIR(inode->i_mode)) {
155 struct dentry *alias = d_find_alias(inode);
156 if (alias) {
157 dput(alias);
158 return 1;
161 return 0;
164 static int invalid_nodeid(u64 nodeid)
166 return !nodeid || nodeid == FUSE_ROOT_ID;
169 static struct dentry_operations fuse_dentry_operations = {
170 .d_revalidate = fuse_dentry_revalidate,
173 static int valid_mode(int m)
175 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
176 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
179 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
180 struct nameidata *nd)
182 int err;
183 struct fuse_entry_out outarg;
184 struct inode *inode = NULL;
185 struct fuse_conn *fc = get_fuse_conn(dir);
186 struct fuse_req *req;
188 if (entry->d_name.len > FUSE_NAME_MAX)
189 return ERR_PTR(-ENAMETOOLONG);
191 req = fuse_get_request(fc);
192 if (!req)
193 return ERR_PTR(-EINTR);
195 fuse_lookup_init(req, dir, entry, &outarg);
196 request_send(fc, req);
197 err = req->out.h.error;
198 /* Zero nodeid is same as -ENOENT, but with valid timeout */
199 if (!err && outarg.nodeid &&
200 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
201 err = -EIO;
202 if (!err && outarg.nodeid) {
203 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
204 &outarg.attr);
205 if (!inode) {
206 fuse_send_forget(fc, req, outarg.nodeid, 1);
207 return ERR_PTR(-ENOMEM);
210 fuse_put_request(fc, req);
211 if (err && err != -ENOENT)
212 return ERR_PTR(err);
214 if (inode && dir_alias(inode)) {
215 iput(inode);
216 return ERR_PTR(-EIO);
218 d_add(entry, inode);
219 entry->d_op = &fuse_dentry_operations;
220 if (!err)
221 fuse_change_timeout(entry, &outarg);
222 else
223 fuse_invalidate_entry_cache(entry);
224 return NULL;
228 * Atomic create+open operation
230 * If the filesystem doesn't support this, then fall back to separate
231 * 'mknod' + 'open' requests.
233 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
234 struct nameidata *nd)
236 int err;
237 struct inode *inode;
238 struct fuse_conn *fc = get_fuse_conn(dir);
239 struct fuse_req *req;
240 struct fuse_open_in inarg;
241 struct fuse_open_out outopen;
242 struct fuse_entry_out outentry;
243 struct fuse_file *ff;
244 struct file *file;
245 int flags = nd->intent.open.flags - 1;
247 err = -ENOSYS;
248 if (fc->no_create)
249 goto out;
251 err = -EINTR;
252 req = fuse_get_request(fc);
253 if (!req)
254 goto out;
256 ff = fuse_file_alloc();
257 if (!ff)
258 goto out_put_request;
260 flags &= ~O_NOCTTY;
261 memset(&inarg, 0, sizeof(inarg));
262 inarg.flags = flags;
263 inarg.mode = mode;
264 req->in.h.opcode = FUSE_CREATE;
265 req->in.h.nodeid = get_node_id(dir);
266 req->inode = dir;
267 req->in.numargs = 2;
268 req->in.args[0].size = sizeof(inarg);
269 req->in.args[0].value = &inarg;
270 req->in.args[1].size = entry->d_name.len + 1;
271 req->in.args[1].value = entry->d_name.name;
272 req->out.numargs = 2;
273 req->out.args[0].size = sizeof(outentry);
274 req->out.args[0].value = &outentry;
275 req->out.args[1].size = sizeof(outopen);
276 req->out.args[1].value = &outopen;
277 request_send(fc, req);
278 err = req->out.h.error;
279 if (err) {
280 if (err == -ENOSYS)
281 fc->no_create = 1;
282 goto out_free_ff;
285 err = -EIO;
286 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
287 goto out_free_ff;
289 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
290 &outentry.attr);
291 err = -ENOMEM;
292 if (!inode) {
293 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
294 ff->fh = outopen.fh;
295 /* Special release, with inode = NULL, this will
296 trigger a 'forget' request when the release is
297 complete */
298 fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0);
299 goto out_put_request;
301 fuse_put_request(fc, req);
302 d_instantiate(entry, inode);
303 fuse_change_timeout(entry, &outentry);
304 file = lookup_instantiate_filp(nd, entry, generic_file_open);
305 if (IS_ERR(file)) {
306 ff->fh = outopen.fh;
307 fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0);
308 return PTR_ERR(file);
310 fuse_finish_open(inode, file, ff, &outopen);
311 return 0;
313 out_free_ff:
314 fuse_file_free(ff);
315 out_put_request:
316 fuse_put_request(fc, req);
317 out:
318 return err;
322 * Code shared between mknod, mkdir, symlink and link
324 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
325 struct inode *dir, struct dentry *entry,
326 int mode)
328 struct fuse_entry_out outarg;
329 struct inode *inode;
330 int err;
332 req->in.h.nodeid = get_node_id(dir);
333 req->inode = dir;
334 req->out.numargs = 1;
335 req->out.args[0].size = sizeof(outarg);
336 req->out.args[0].value = &outarg;
337 request_send(fc, req);
338 err = req->out.h.error;
339 if (err) {
340 fuse_put_request(fc, req);
341 return err;
343 err = -EIO;
344 if (invalid_nodeid(outarg.nodeid))
345 goto out_put_request;
347 if ((outarg.attr.mode ^ mode) & S_IFMT)
348 goto out_put_request;
350 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
351 &outarg.attr);
352 if (!inode) {
353 fuse_send_forget(fc, req, outarg.nodeid, 1);
354 return -ENOMEM;
356 fuse_put_request(fc, req);
358 if (dir_alias(inode)) {
359 iput(inode);
360 return -EIO;
363 d_instantiate(entry, inode);
364 fuse_change_timeout(entry, &outarg);
365 fuse_invalidate_attr(dir);
366 return 0;
368 out_put_request:
369 fuse_put_request(fc, req);
370 return err;
373 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
374 dev_t rdev)
376 struct fuse_mknod_in inarg;
377 struct fuse_conn *fc = get_fuse_conn(dir);
378 struct fuse_req *req = fuse_get_request(fc);
379 if (!req)
380 return -EINTR;
382 memset(&inarg, 0, sizeof(inarg));
383 inarg.mode = mode;
384 inarg.rdev = new_encode_dev(rdev);
385 req->in.h.opcode = FUSE_MKNOD;
386 req->in.numargs = 2;
387 req->in.args[0].size = sizeof(inarg);
388 req->in.args[0].value = &inarg;
389 req->in.args[1].size = entry->d_name.len + 1;
390 req->in.args[1].value = entry->d_name.name;
391 return create_new_entry(fc, req, dir, entry, mode);
394 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
395 struct nameidata *nd)
397 if (nd && (nd->flags & LOOKUP_CREATE)) {
398 int err = fuse_create_open(dir, entry, mode, nd);
399 if (err != -ENOSYS)
400 return err;
401 /* Fall back on mknod */
403 return fuse_mknod(dir, entry, mode, 0);
406 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
408 struct fuse_mkdir_in inarg;
409 struct fuse_conn *fc = get_fuse_conn(dir);
410 struct fuse_req *req = fuse_get_request(fc);
411 if (!req)
412 return -EINTR;
414 memset(&inarg, 0, sizeof(inarg));
415 inarg.mode = mode;
416 req->in.h.opcode = FUSE_MKDIR;
417 req->in.numargs = 2;
418 req->in.args[0].size = sizeof(inarg);
419 req->in.args[0].value = &inarg;
420 req->in.args[1].size = entry->d_name.len + 1;
421 req->in.args[1].value = entry->d_name.name;
422 return create_new_entry(fc, req, dir, entry, S_IFDIR);
425 static int fuse_symlink(struct inode *dir, struct dentry *entry,
426 const char *link)
428 struct fuse_conn *fc = get_fuse_conn(dir);
429 unsigned len = strlen(link) + 1;
430 struct fuse_req *req = fuse_get_request(fc);
431 if (!req)
432 return -EINTR;
434 req->in.h.opcode = FUSE_SYMLINK;
435 req->in.numargs = 2;
436 req->in.args[0].size = entry->d_name.len + 1;
437 req->in.args[0].value = entry->d_name.name;
438 req->in.args[1].size = len;
439 req->in.args[1].value = link;
440 return create_new_entry(fc, req, dir, entry, S_IFLNK);
443 static int fuse_unlink(struct inode *dir, struct dentry *entry)
445 int err;
446 struct fuse_conn *fc = get_fuse_conn(dir);
447 struct fuse_req *req = fuse_get_request(fc);
448 if (!req)
449 return -EINTR;
451 req->in.h.opcode = FUSE_UNLINK;
452 req->in.h.nodeid = get_node_id(dir);
453 req->inode = dir;
454 req->in.numargs = 1;
455 req->in.args[0].size = entry->d_name.len + 1;
456 req->in.args[0].value = entry->d_name.name;
457 request_send(fc, req);
458 err = req->out.h.error;
459 fuse_put_request(fc, req);
460 if (!err) {
461 struct inode *inode = entry->d_inode;
463 /* Set nlink to zero so the inode can be cleared, if
464 the inode does have more links this will be
465 discovered at the next lookup/getattr */
466 inode->i_nlink = 0;
467 fuse_invalidate_attr(inode);
468 fuse_invalidate_attr(dir);
469 fuse_invalidate_entry_cache(entry);
470 } else if (err == -EINTR)
471 fuse_invalidate_entry(entry);
472 return err;
475 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
477 int err;
478 struct fuse_conn *fc = get_fuse_conn(dir);
479 struct fuse_req *req = fuse_get_request(fc);
480 if (!req)
481 return -EINTR;
483 req->in.h.opcode = FUSE_RMDIR;
484 req->in.h.nodeid = get_node_id(dir);
485 req->inode = dir;
486 req->in.numargs = 1;
487 req->in.args[0].size = entry->d_name.len + 1;
488 req->in.args[0].value = entry->d_name.name;
489 request_send(fc, req);
490 err = req->out.h.error;
491 fuse_put_request(fc, req);
492 if (!err) {
493 entry->d_inode->i_nlink = 0;
494 fuse_invalidate_attr(dir);
495 fuse_invalidate_entry_cache(entry);
496 } else if (err == -EINTR)
497 fuse_invalidate_entry(entry);
498 return err;
501 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
502 struct inode *newdir, struct dentry *newent)
504 int err;
505 struct fuse_rename_in inarg;
506 struct fuse_conn *fc = get_fuse_conn(olddir);
507 struct fuse_req *req = fuse_get_request(fc);
508 if (!req)
509 return -EINTR;
511 memset(&inarg, 0, sizeof(inarg));
512 inarg.newdir = get_node_id(newdir);
513 req->in.h.opcode = FUSE_RENAME;
514 req->in.h.nodeid = get_node_id(olddir);
515 req->inode = olddir;
516 req->inode2 = newdir;
517 req->in.numargs = 3;
518 req->in.args[0].size = sizeof(inarg);
519 req->in.args[0].value = &inarg;
520 req->in.args[1].size = oldent->d_name.len + 1;
521 req->in.args[1].value = oldent->d_name.name;
522 req->in.args[2].size = newent->d_name.len + 1;
523 req->in.args[2].value = newent->d_name.name;
524 request_send(fc, req);
525 err = req->out.h.error;
526 fuse_put_request(fc, req);
527 if (!err) {
528 fuse_invalidate_attr(olddir);
529 if (olddir != newdir)
530 fuse_invalidate_attr(newdir);
532 /* newent will end up negative */
533 if (newent->d_inode)
534 fuse_invalidate_entry_cache(newent);
535 } else if (err == -EINTR) {
536 /* If request was interrupted, DEITY only knows if the
537 rename actually took place. If the invalidation
538 fails (e.g. some process has CWD under the renamed
539 directory), then there can be inconsistency between
540 the dcache and the real filesystem. Tough luck. */
541 fuse_invalidate_entry(oldent);
542 if (newent->d_inode)
543 fuse_invalidate_entry(newent);
546 return err;
549 static int fuse_link(struct dentry *entry, struct inode *newdir,
550 struct dentry *newent)
552 int err;
553 struct fuse_link_in inarg;
554 struct inode *inode = entry->d_inode;
555 struct fuse_conn *fc = get_fuse_conn(inode);
556 struct fuse_req *req = fuse_get_request(fc);
557 if (!req)
558 return -EINTR;
560 memset(&inarg, 0, sizeof(inarg));
561 inarg.oldnodeid = get_node_id(inode);
562 req->in.h.opcode = FUSE_LINK;
563 req->inode2 = inode;
564 req->in.numargs = 2;
565 req->in.args[0].size = sizeof(inarg);
566 req->in.args[0].value = &inarg;
567 req->in.args[1].size = newent->d_name.len + 1;
568 req->in.args[1].value = newent->d_name.name;
569 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
570 /* Contrary to "normal" filesystems it can happen that link
571 makes two "logical" inodes point to the same "physical"
572 inode. We invalidate the attributes of the old one, so it
573 will reflect changes in the backing inode (link count,
574 etc.)
576 if (!err || err == -EINTR)
577 fuse_invalidate_attr(inode);
578 return err;
581 int fuse_do_getattr(struct inode *inode)
583 int err;
584 struct fuse_attr_out arg;
585 struct fuse_conn *fc = get_fuse_conn(inode);
586 struct fuse_req *req = fuse_get_request(fc);
587 if (!req)
588 return -EINTR;
590 req->in.h.opcode = FUSE_GETATTR;
591 req->in.h.nodeid = get_node_id(inode);
592 req->inode = inode;
593 req->out.numargs = 1;
594 req->out.args[0].size = sizeof(arg);
595 req->out.args[0].value = &arg;
596 request_send(fc, req);
597 err = req->out.h.error;
598 fuse_put_request(fc, req);
599 if (!err) {
600 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
601 make_bad_inode(inode);
602 err = -EIO;
603 } else {
604 struct fuse_inode *fi = get_fuse_inode(inode);
605 fuse_change_attributes(inode, &arg.attr);
606 fi->i_time = time_to_jiffies(arg.attr_valid,
607 arg.attr_valid_nsec);
610 return err;
614 * Calling into a user-controlled filesystem gives the filesystem
615 * daemon ptrace-like capabilities over the requester process. This
616 * means, that the filesystem daemon is able to record the exact
617 * filesystem operations performed, and can also control the behavior
618 * of the requester process in otherwise impossible ways. For example
619 * it can delay the operation for arbitrary length of time allowing
620 * DoS against the requester.
622 * For this reason only those processes can call into the filesystem,
623 * for which the owner of the mount has ptrace privilege. This
624 * excludes processes started by other users, suid or sgid processes.
626 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
628 if (fc->flags & FUSE_ALLOW_OTHER)
629 return 1;
631 if (task->euid == fc->user_id &&
632 task->suid == fc->user_id &&
633 task->uid == fc->user_id &&
634 task->egid == fc->group_id &&
635 task->sgid == fc->group_id &&
636 task->gid == fc->group_id)
637 return 1;
639 return 0;
643 * Check whether the inode attributes are still valid
645 * If the attribute validity timeout has expired, then fetch the fresh
646 * attributes with a 'getattr' request
648 * I'm not sure why cached attributes are never returned for the root
649 * inode, this is probably being too cautious.
651 static int fuse_revalidate(struct dentry *entry)
653 struct inode *inode = entry->d_inode;
654 struct fuse_inode *fi = get_fuse_inode(inode);
655 struct fuse_conn *fc = get_fuse_conn(inode);
657 if (!fuse_allow_task(fc, current))
658 return -EACCES;
659 if (get_node_id(inode) != FUSE_ROOT_ID &&
660 time_before_eq(jiffies, fi->i_time))
661 return 0;
663 return fuse_do_getattr(inode);
666 static int fuse_access(struct inode *inode, int mask)
668 struct fuse_conn *fc = get_fuse_conn(inode);
669 struct fuse_req *req;
670 struct fuse_access_in inarg;
671 int err;
673 if (fc->no_access)
674 return 0;
676 req = fuse_get_request(fc);
677 if (!req)
678 return -EINTR;
680 memset(&inarg, 0, sizeof(inarg));
681 inarg.mask = mask;
682 req->in.h.opcode = FUSE_ACCESS;
683 req->in.h.nodeid = get_node_id(inode);
684 req->inode = inode;
685 req->in.numargs = 1;
686 req->in.args[0].size = sizeof(inarg);
687 req->in.args[0].value = &inarg;
688 request_send(fc, req);
689 err = req->out.h.error;
690 fuse_put_request(fc, req);
691 if (err == -ENOSYS) {
692 fc->no_access = 1;
693 err = 0;
695 return err;
699 * Check permission. The two basic access models of FUSE are:
701 * 1) Local access checking ('default_permissions' mount option) based
702 * on file mode. This is the plain old disk filesystem permission
703 * modell.
705 * 2) "Remote" access checking, where server is responsible for
706 * checking permission in each inode operation. An exception to this
707 * is if ->permission() was invoked from sys_access() in which case an
708 * access request is sent. Execute permission is still checked
709 * locally based on file mode.
711 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
713 struct fuse_conn *fc = get_fuse_conn(inode);
715 if (!fuse_allow_task(fc, current))
716 return -EACCES;
717 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
718 int err = generic_permission(inode, mask, NULL);
720 /* If permission is denied, try to refresh file
721 attributes. This is also needed, because the root
722 node will at first have no permissions */
723 if (err == -EACCES) {
724 err = fuse_do_getattr(inode);
725 if (!err)
726 err = generic_permission(inode, mask, NULL);
729 /* Note: the opposite of the above test does not
730 exist. So if permissions are revoked this won't be
731 noticed immediately, only after the attribute
732 timeout has expired */
734 return err;
735 } else {
736 int mode = inode->i_mode;
737 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
738 return -EACCES;
740 if (nd && (nd->flags & LOOKUP_ACCESS))
741 return fuse_access(inode, mask);
742 return 0;
746 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
747 void *dstbuf, filldir_t filldir)
749 while (nbytes >= FUSE_NAME_OFFSET) {
750 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
751 size_t reclen = FUSE_DIRENT_SIZE(dirent);
752 int over;
753 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
754 return -EIO;
755 if (reclen > nbytes)
756 break;
758 over = filldir(dstbuf, dirent->name, dirent->namelen,
759 file->f_pos, dirent->ino, dirent->type);
760 if (over)
761 break;
763 buf += reclen;
764 nbytes -= reclen;
765 file->f_pos = dirent->off;
768 return 0;
771 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
773 int err;
774 size_t nbytes;
775 struct page *page;
776 struct inode *inode = file->f_dentry->d_inode;
777 struct fuse_conn *fc = get_fuse_conn(inode);
778 struct fuse_req *req;
780 if (is_bad_inode(inode))
781 return -EIO;
783 req = fuse_get_request(fc);
784 if (!req)
785 return -EINTR;
787 page = alloc_page(GFP_KERNEL);
788 if (!page) {
789 fuse_put_request(fc, req);
790 return -ENOMEM;
792 req->num_pages = 1;
793 req->pages[0] = page;
794 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
795 request_send(fc, req);
796 nbytes = req->out.args[0].size;
797 err = req->out.h.error;
798 fuse_put_request(fc, req);
799 if (!err)
800 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
801 filldir);
803 __free_page(page);
804 fuse_invalidate_attr(inode); /* atime changed */
805 return err;
808 static char *read_link(struct dentry *dentry)
810 struct inode *inode = dentry->d_inode;
811 struct fuse_conn *fc = get_fuse_conn(inode);
812 struct fuse_req *req = fuse_get_request(fc);
813 char *link;
815 if (!req)
816 return ERR_PTR(-EINTR);
818 link = (char *) __get_free_page(GFP_KERNEL);
819 if (!link) {
820 link = ERR_PTR(-ENOMEM);
821 goto out;
823 req->in.h.opcode = FUSE_READLINK;
824 req->in.h.nodeid = get_node_id(inode);
825 req->inode = inode;
826 req->out.argvar = 1;
827 req->out.numargs = 1;
828 req->out.args[0].size = PAGE_SIZE - 1;
829 req->out.args[0].value = link;
830 request_send(fc, req);
831 if (req->out.h.error) {
832 free_page((unsigned long) link);
833 link = ERR_PTR(req->out.h.error);
834 } else
835 link[req->out.args[0].size] = '\0';
836 out:
837 fuse_put_request(fc, req);
838 fuse_invalidate_attr(inode); /* atime changed */
839 return link;
842 static void free_link(char *link)
844 if (!IS_ERR(link))
845 free_page((unsigned long) link);
848 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
850 nd_set_link(nd, read_link(dentry));
851 return NULL;
854 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
856 free_link(nd_get_link(nd));
859 static int fuse_dir_open(struct inode *inode, struct file *file)
861 return fuse_open_common(inode, file, 1);
864 static int fuse_dir_release(struct inode *inode, struct file *file)
866 return fuse_release_common(inode, file, 1);
869 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
871 /* nfsd can call this with no file */
872 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
875 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
877 unsigned ivalid = iattr->ia_valid;
879 if (ivalid & ATTR_MODE)
880 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
881 if (ivalid & ATTR_UID)
882 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
883 if (ivalid & ATTR_GID)
884 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
885 if (ivalid & ATTR_SIZE)
886 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
887 /* You can only _set_ these together (they may change by themselves) */
888 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
889 arg->valid |= FATTR_ATIME | FATTR_MTIME;
890 arg->atime = iattr->ia_atime.tv_sec;
891 arg->mtime = iattr->ia_mtime.tv_sec;
893 if (ivalid & ATTR_FILE) {
894 struct fuse_file *ff = iattr->ia_file->private_data;
895 arg->valid |= FATTR_FH;
896 arg->fh = ff->fh;
901 * Set attributes, and at the same time refresh them.
903 * Truncation is slightly complicated, because the 'truncate' request
904 * may fail, in which case we don't want to touch the mapping.
905 * vmtruncate() doesn't allow for this case. So do the rlimit
906 * checking by hand and call vmtruncate() only after the file has
907 * actually been truncated.
909 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
911 struct inode *inode = entry->d_inode;
912 struct fuse_conn *fc = get_fuse_conn(inode);
913 struct fuse_inode *fi = get_fuse_inode(inode);
914 struct fuse_req *req;
915 struct fuse_setattr_in inarg;
916 struct fuse_attr_out outarg;
917 int err;
918 int is_truncate = 0;
920 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
921 err = inode_change_ok(inode, attr);
922 if (err)
923 return err;
926 if (attr->ia_valid & ATTR_SIZE) {
927 unsigned long limit;
928 is_truncate = 1;
929 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
930 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
931 send_sig(SIGXFSZ, current, 0);
932 return -EFBIG;
936 req = fuse_get_request(fc);
937 if (!req)
938 return -EINTR;
940 memset(&inarg, 0, sizeof(inarg));
941 iattr_to_fattr(attr, &inarg);
942 req->in.h.opcode = FUSE_SETATTR;
943 req->in.h.nodeid = get_node_id(inode);
944 req->inode = inode;
945 req->in.numargs = 1;
946 req->in.args[0].size = sizeof(inarg);
947 req->in.args[0].value = &inarg;
948 req->out.numargs = 1;
949 req->out.args[0].size = sizeof(outarg);
950 req->out.args[0].value = &outarg;
951 request_send(fc, req);
952 err = req->out.h.error;
953 fuse_put_request(fc, req);
954 if (!err) {
955 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
956 make_bad_inode(inode);
957 err = -EIO;
958 } else {
959 if (is_truncate) {
960 loff_t origsize = i_size_read(inode);
961 i_size_write(inode, outarg.attr.size);
962 if (origsize > outarg.attr.size)
963 vmtruncate(inode, outarg.attr.size);
965 fuse_change_attributes(inode, &outarg.attr);
966 fi->i_time = time_to_jiffies(outarg.attr_valid,
967 outarg.attr_valid_nsec);
969 } else if (err == -EINTR)
970 fuse_invalidate_attr(inode);
972 return err;
975 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
976 struct kstat *stat)
978 struct inode *inode = entry->d_inode;
979 int err = fuse_revalidate(entry);
980 if (!err)
981 generic_fillattr(inode, stat);
983 return err;
986 static int fuse_setxattr(struct dentry *entry, const char *name,
987 const void *value, size_t size, int flags)
989 struct inode *inode = entry->d_inode;
990 struct fuse_conn *fc = get_fuse_conn(inode);
991 struct fuse_req *req;
992 struct fuse_setxattr_in inarg;
993 int err;
995 if (fc->no_setxattr)
996 return -EOPNOTSUPP;
998 req = fuse_get_request(fc);
999 if (!req)
1000 return -EINTR;
1002 memset(&inarg, 0, sizeof(inarg));
1003 inarg.size = size;
1004 inarg.flags = flags;
1005 req->in.h.opcode = FUSE_SETXATTR;
1006 req->in.h.nodeid = get_node_id(inode);
1007 req->inode = inode;
1008 req->in.numargs = 3;
1009 req->in.args[0].size = sizeof(inarg);
1010 req->in.args[0].value = &inarg;
1011 req->in.args[1].size = strlen(name) + 1;
1012 req->in.args[1].value = name;
1013 req->in.args[2].size = size;
1014 req->in.args[2].value = value;
1015 request_send(fc, req);
1016 err = req->out.h.error;
1017 fuse_put_request(fc, req);
1018 if (err == -ENOSYS) {
1019 fc->no_setxattr = 1;
1020 err = -EOPNOTSUPP;
1022 return err;
1025 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1026 void *value, size_t size)
1028 struct inode *inode = entry->d_inode;
1029 struct fuse_conn *fc = get_fuse_conn(inode);
1030 struct fuse_req *req;
1031 struct fuse_getxattr_in inarg;
1032 struct fuse_getxattr_out outarg;
1033 ssize_t ret;
1035 if (fc->no_getxattr)
1036 return -EOPNOTSUPP;
1038 req = fuse_get_request(fc);
1039 if (!req)
1040 return -EINTR;
1042 memset(&inarg, 0, sizeof(inarg));
1043 inarg.size = size;
1044 req->in.h.opcode = FUSE_GETXATTR;
1045 req->in.h.nodeid = get_node_id(inode);
1046 req->inode = inode;
1047 req->in.numargs = 2;
1048 req->in.args[0].size = sizeof(inarg);
1049 req->in.args[0].value = &inarg;
1050 req->in.args[1].size = strlen(name) + 1;
1051 req->in.args[1].value = name;
1052 /* This is really two different operations rolled into one */
1053 req->out.numargs = 1;
1054 if (size) {
1055 req->out.argvar = 1;
1056 req->out.args[0].size = size;
1057 req->out.args[0].value = value;
1058 } else {
1059 req->out.args[0].size = sizeof(outarg);
1060 req->out.args[0].value = &outarg;
1062 request_send(fc, req);
1063 ret = req->out.h.error;
1064 if (!ret)
1065 ret = size ? req->out.args[0].size : outarg.size;
1066 else {
1067 if (ret == -ENOSYS) {
1068 fc->no_getxattr = 1;
1069 ret = -EOPNOTSUPP;
1072 fuse_put_request(fc, req);
1073 return ret;
1076 static ssize_t fuse_listxattr(struct dentry *entry, char *list, 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;
1083 ssize_t ret;
1085 if (fc->no_listxattr)
1086 return -EOPNOTSUPP;
1088 req = fuse_get_request(fc);
1089 if (!req)
1090 return -EINTR;
1092 memset(&inarg, 0, sizeof(inarg));
1093 inarg.size = size;
1094 req->in.h.opcode = FUSE_LISTXATTR;
1095 req->in.h.nodeid = get_node_id(inode);
1096 req->inode = inode;
1097 req->in.numargs = 1;
1098 req->in.args[0].size = sizeof(inarg);
1099 req->in.args[0].value = &inarg;
1100 /* This is really two different operations rolled into one */
1101 req->out.numargs = 1;
1102 if (size) {
1103 req->out.argvar = 1;
1104 req->out.args[0].size = size;
1105 req->out.args[0].value = list;
1106 } else {
1107 req->out.args[0].size = sizeof(outarg);
1108 req->out.args[0].value = &outarg;
1110 request_send(fc, req);
1111 ret = req->out.h.error;
1112 if (!ret)
1113 ret = size ? req->out.args[0].size : outarg.size;
1114 else {
1115 if (ret == -ENOSYS) {
1116 fc->no_listxattr = 1;
1117 ret = -EOPNOTSUPP;
1120 fuse_put_request(fc, req);
1121 return ret;
1124 static int fuse_removexattr(struct dentry *entry, const char *name)
1126 struct inode *inode = entry->d_inode;
1127 struct fuse_conn *fc = get_fuse_conn(inode);
1128 struct fuse_req *req;
1129 int err;
1131 if (fc->no_removexattr)
1132 return -EOPNOTSUPP;
1134 req = fuse_get_request(fc);
1135 if (!req)
1136 return -EINTR;
1138 req->in.h.opcode = FUSE_REMOVEXATTR;
1139 req->in.h.nodeid = get_node_id(inode);
1140 req->inode = inode;
1141 req->in.numargs = 1;
1142 req->in.args[0].size = strlen(name) + 1;
1143 req->in.args[0].value = name;
1144 request_send(fc, req);
1145 err = req->out.h.error;
1146 fuse_put_request(fc, req);
1147 if (err == -ENOSYS) {
1148 fc->no_removexattr = 1;
1149 err = -EOPNOTSUPP;
1151 return err;
1154 static struct inode_operations fuse_dir_inode_operations = {
1155 .lookup = fuse_lookup,
1156 .mkdir = fuse_mkdir,
1157 .symlink = fuse_symlink,
1158 .unlink = fuse_unlink,
1159 .rmdir = fuse_rmdir,
1160 .rename = fuse_rename,
1161 .link = fuse_link,
1162 .setattr = fuse_setattr,
1163 .create = fuse_create,
1164 .mknod = fuse_mknod,
1165 .permission = fuse_permission,
1166 .getattr = fuse_getattr,
1167 .setxattr = fuse_setxattr,
1168 .getxattr = fuse_getxattr,
1169 .listxattr = fuse_listxattr,
1170 .removexattr = fuse_removexattr,
1173 static struct file_operations fuse_dir_operations = {
1174 .llseek = generic_file_llseek,
1175 .read = generic_read_dir,
1176 .readdir = fuse_readdir,
1177 .open = fuse_dir_open,
1178 .release = fuse_dir_release,
1179 .fsync = fuse_dir_fsync,
1182 static struct inode_operations fuse_common_inode_operations = {
1183 .setattr = fuse_setattr,
1184 .permission = fuse_permission,
1185 .getattr = fuse_getattr,
1186 .setxattr = fuse_setxattr,
1187 .getxattr = fuse_getxattr,
1188 .listxattr = fuse_listxattr,
1189 .removexattr = fuse_removexattr,
1192 static struct inode_operations fuse_symlink_inode_operations = {
1193 .setattr = fuse_setattr,
1194 .follow_link = fuse_follow_link,
1195 .put_link = fuse_put_link,
1196 .readlink = generic_readlink,
1197 .getattr = fuse_getattr,
1198 .setxattr = fuse_setxattr,
1199 .getxattr = fuse_getxattr,
1200 .listxattr = fuse_listxattr,
1201 .removexattr = fuse_removexattr,
1204 void fuse_init_common(struct inode *inode)
1206 inode->i_op = &fuse_common_inode_operations;
1209 void fuse_init_dir(struct inode *inode)
1211 inode->i_op = &fuse_dir_inode_operations;
1212 inode->i_fop = &fuse_dir_operations;
1215 void fuse_init_symlink(struct inode *inode)
1217 inode->i_op = &fuse_symlink_inode_operations;