Linux 2.6.16.61-rc1
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / fuse / dir.c
blob4782a7b103c5ba1971b9aa8e582ca7c6e2ef2182
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;
900 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
902 int need_trunc;
904 spin_lock(&fuse_lock);
905 need_trunc = inode->i_size > offset;
906 i_size_write(inode, offset);
907 spin_unlock(&fuse_lock);
909 if (need_trunc) {
910 struct address_space *mapping = inode->i_mapping;
911 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
912 truncate_inode_pages(mapping, offset);
917 * Set attributes, and at the same time refresh them.
919 * Truncation is slightly complicated, because the 'truncate' request
920 * may fail, in which case we don't want to touch the mapping.
921 * vmtruncate() doesn't allow for this case, so do the rlimit checking
922 * and the actual truncation by hand.
924 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
926 struct inode *inode = entry->d_inode;
927 struct fuse_conn *fc = get_fuse_conn(inode);
928 struct fuse_inode *fi = get_fuse_inode(inode);
929 struct fuse_req *req;
930 struct fuse_setattr_in inarg;
931 struct fuse_attr_out outarg;
932 int err;
933 int is_truncate = 0;
935 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
936 err = inode_change_ok(inode, attr);
937 if (err)
938 return err;
941 if (attr->ia_valid & ATTR_SIZE) {
942 unsigned long limit;
943 is_truncate = 1;
944 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
945 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
946 send_sig(SIGXFSZ, current, 0);
947 return -EFBIG;
951 req = fuse_get_request(fc);
952 if (!req)
953 return -EINTR;
955 memset(&inarg, 0, sizeof(inarg));
956 iattr_to_fattr(attr, &inarg);
957 req->in.h.opcode = FUSE_SETATTR;
958 req->in.h.nodeid = get_node_id(inode);
959 req->inode = inode;
960 req->in.numargs = 1;
961 req->in.args[0].size = sizeof(inarg);
962 req->in.args[0].value = &inarg;
963 req->out.numargs = 1;
964 req->out.args[0].size = sizeof(outarg);
965 req->out.args[0].value = &outarg;
966 request_send(fc, req);
967 err = req->out.h.error;
968 fuse_put_request(fc, req);
969 if (!err) {
970 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
971 make_bad_inode(inode);
972 err = -EIO;
973 } else {
974 if (is_truncate)
975 fuse_vmtruncate(inode, outarg.attr.size);
976 fuse_change_attributes(inode, &outarg.attr);
977 fi->i_time = time_to_jiffies(outarg.attr_valid,
978 outarg.attr_valid_nsec);
980 } else if (err == -EINTR)
981 fuse_invalidate_attr(inode);
983 return err;
986 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
987 struct kstat *stat)
989 struct inode *inode = entry->d_inode;
990 int err = fuse_revalidate(entry);
991 if (!err)
992 generic_fillattr(inode, stat);
994 return err;
997 static int fuse_setxattr(struct dentry *entry, const char *name,
998 const void *value, size_t size, int flags)
1000 struct inode *inode = entry->d_inode;
1001 struct fuse_conn *fc = get_fuse_conn(inode);
1002 struct fuse_req *req;
1003 struct fuse_setxattr_in inarg;
1004 int err;
1006 if (fc->no_setxattr)
1007 return -EOPNOTSUPP;
1009 req = fuse_get_request(fc);
1010 if (!req)
1011 return -EINTR;
1013 memset(&inarg, 0, sizeof(inarg));
1014 inarg.size = size;
1015 inarg.flags = flags;
1016 req->in.h.opcode = FUSE_SETXATTR;
1017 req->in.h.nodeid = get_node_id(inode);
1018 req->inode = inode;
1019 req->in.numargs = 3;
1020 req->in.args[0].size = sizeof(inarg);
1021 req->in.args[0].value = &inarg;
1022 req->in.args[1].size = strlen(name) + 1;
1023 req->in.args[1].value = name;
1024 req->in.args[2].size = size;
1025 req->in.args[2].value = value;
1026 request_send(fc, req);
1027 err = req->out.h.error;
1028 fuse_put_request(fc, req);
1029 if (err == -ENOSYS) {
1030 fc->no_setxattr = 1;
1031 err = -EOPNOTSUPP;
1033 return err;
1036 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1037 void *value, size_t size)
1039 struct inode *inode = entry->d_inode;
1040 struct fuse_conn *fc = get_fuse_conn(inode);
1041 struct fuse_req *req;
1042 struct fuse_getxattr_in inarg;
1043 struct fuse_getxattr_out outarg;
1044 ssize_t ret;
1046 if (fc->no_getxattr)
1047 return -EOPNOTSUPP;
1049 req = fuse_get_request(fc);
1050 if (!req)
1051 return -EINTR;
1053 memset(&inarg, 0, sizeof(inarg));
1054 inarg.size = size;
1055 req->in.h.opcode = FUSE_GETXATTR;
1056 req->in.h.nodeid = get_node_id(inode);
1057 req->inode = inode;
1058 req->in.numargs = 2;
1059 req->in.args[0].size = sizeof(inarg);
1060 req->in.args[0].value = &inarg;
1061 req->in.args[1].size = strlen(name) + 1;
1062 req->in.args[1].value = name;
1063 /* This is really two different operations rolled into one */
1064 req->out.numargs = 1;
1065 if (size) {
1066 req->out.argvar = 1;
1067 req->out.args[0].size = size;
1068 req->out.args[0].value = value;
1069 } else {
1070 req->out.args[0].size = sizeof(outarg);
1071 req->out.args[0].value = &outarg;
1073 request_send(fc, req);
1074 ret = req->out.h.error;
1075 if (!ret)
1076 ret = size ? req->out.args[0].size : outarg.size;
1077 else {
1078 if (ret == -ENOSYS) {
1079 fc->no_getxattr = 1;
1080 ret = -EOPNOTSUPP;
1083 fuse_put_request(fc, req);
1084 return ret;
1087 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1089 struct inode *inode = entry->d_inode;
1090 struct fuse_conn *fc = get_fuse_conn(inode);
1091 struct fuse_req *req;
1092 struct fuse_getxattr_in inarg;
1093 struct fuse_getxattr_out outarg;
1094 ssize_t ret;
1096 if (fc->no_listxattr)
1097 return -EOPNOTSUPP;
1099 req = fuse_get_request(fc);
1100 if (!req)
1101 return -EINTR;
1103 memset(&inarg, 0, sizeof(inarg));
1104 inarg.size = size;
1105 req->in.h.opcode = FUSE_LISTXATTR;
1106 req->in.h.nodeid = get_node_id(inode);
1107 req->inode = inode;
1108 req->in.numargs = 1;
1109 req->in.args[0].size = sizeof(inarg);
1110 req->in.args[0].value = &inarg;
1111 /* This is really two different operations rolled into one */
1112 req->out.numargs = 1;
1113 if (size) {
1114 req->out.argvar = 1;
1115 req->out.args[0].size = size;
1116 req->out.args[0].value = list;
1117 } else {
1118 req->out.args[0].size = sizeof(outarg);
1119 req->out.args[0].value = &outarg;
1121 request_send(fc, req);
1122 ret = req->out.h.error;
1123 if (!ret)
1124 ret = size ? req->out.args[0].size : outarg.size;
1125 else {
1126 if (ret == -ENOSYS) {
1127 fc->no_listxattr = 1;
1128 ret = -EOPNOTSUPP;
1131 fuse_put_request(fc, req);
1132 return ret;
1135 static int fuse_removexattr(struct dentry *entry, const char *name)
1137 struct inode *inode = entry->d_inode;
1138 struct fuse_conn *fc = get_fuse_conn(inode);
1139 struct fuse_req *req;
1140 int err;
1142 if (fc->no_removexattr)
1143 return -EOPNOTSUPP;
1145 req = fuse_get_request(fc);
1146 if (!req)
1147 return -EINTR;
1149 req->in.h.opcode = FUSE_REMOVEXATTR;
1150 req->in.h.nodeid = get_node_id(inode);
1151 req->inode = inode;
1152 req->in.numargs = 1;
1153 req->in.args[0].size = strlen(name) + 1;
1154 req->in.args[0].value = name;
1155 request_send(fc, req);
1156 err = req->out.h.error;
1157 fuse_put_request(fc, req);
1158 if (err == -ENOSYS) {
1159 fc->no_removexattr = 1;
1160 err = -EOPNOTSUPP;
1162 return err;
1165 static struct inode_operations fuse_dir_inode_operations = {
1166 .lookup = fuse_lookup,
1167 .mkdir = fuse_mkdir,
1168 .symlink = fuse_symlink,
1169 .unlink = fuse_unlink,
1170 .rmdir = fuse_rmdir,
1171 .rename = fuse_rename,
1172 .link = fuse_link,
1173 .setattr = fuse_setattr,
1174 .create = fuse_create,
1175 .mknod = fuse_mknod,
1176 .permission = fuse_permission,
1177 .getattr = fuse_getattr,
1178 .setxattr = fuse_setxattr,
1179 .getxattr = fuse_getxattr,
1180 .listxattr = fuse_listxattr,
1181 .removexattr = fuse_removexattr,
1184 static struct file_operations fuse_dir_operations = {
1185 .llseek = generic_file_llseek,
1186 .read = generic_read_dir,
1187 .readdir = fuse_readdir,
1188 .open = fuse_dir_open,
1189 .release = fuse_dir_release,
1190 .fsync = fuse_dir_fsync,
1193 static struct inode_operations fuse_common_inode_operations = {
1194 .setattr = fuse_setattr,
1195 .permission = fuse_permission,
1196 .getattr = fuse_getattr,
1197 .setxattr = fuse_setxattr,
1198 .getxattr = fuse_getxattr,
1199 .listxattr = fuse_listxattr,
1200 .removexattr = fuse_removexattr,
1203 static struct inode_operations fuse_symlink_inode_operations = {
1204 .setattr = fuse_setattr,
1205 .follow_link = fuse_follow_link,
1206 .put_link = fuse_put_link,
1207 .readlink = generic_readlink,
1208 .getattr = fuse_getattr,
1209 .setxattr = fuse_setxattr,
1210 .getxattr = fuse_getxattr,
1211 .listxattr = fuse_listxattr,
1212 .removexattr = fuse_removexattr,
1215 void fuse_init_common(struct inode *inode)
1217 inode->i_op = &fuse_common_inode_operations;
1220 void fuse_init_dir(struct inode *inode)
1222 inode->i_op = &fuse_dir_inode_operations;
1223 inode->i_fop = &fuse_dir_operations;
1226 void fuse_init_symlink(struct inode *inode)
1228 inode->i_op = &fuse_symlink_inode_operations;