[PATCH] Common compat_sys_sysinfo
[linux-2.6/mini2440.git] / fs / fuse / dir.c
blob40080477ceb4fa5a2fed48f0da0c446c10614bb5
1 /*
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.
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>
17 #if BITS_PER_LONG >= 64
18 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
20 entry->d_time = time;
23 static inline u64 fuse_dentry_time(struct dentry *entry)
25 return entry->d_time;
27 #else
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)
33 entry->d_time = 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);
42 #endif
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)
55 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
57 return get_jiffies_64() + timespec_to_jiffies(&ts);
58 } else
59 return 0;
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
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));
70 if (entry->d_inode)
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
90 * lookup)
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)
103 d_invalidate(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);
113 req->in.numargs = 1;
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))
135 return 0;
136 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
137 int err;
138 struct fuse_entry_out outarg;
139 struct fuse_conn *fc;
140 struct fuse_req *req;
141 struct fuse_req *forget_req;
142 struct dentry *parent;
144 /* For negative dentries, always do a fresh lookup */
145 if (!inode)
146 return 0;
148 fc = get_fuse_conn(inode);
149 req = fuse_get_req(fc);
150 if (IS_ERR(req))
151 return 0;
153 forget_req = fuse_get_req(fc);
154 if (IS_ERR(forget_req)) {
155 fuse_put_request(fc, req);
156 return 0;
159 parent = dget_parent(entry);
160 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
161 request_send(fc, req);
162 dput(parent);
163 err = req->out.h.error;
164 fuse_put_request(fc, req);
165 /* Zero nodeid is same as -ENOENT */
166 if (!err && !outarg.nodeid)
167 err = -ENOENT;
168 if (!err) {
169 struct fuse_inode *fi = get_fuse_inode(inode);
170 if (outarg.nodeid != get_node_id(inode)) {
171 fuse_send_forget(fc, forget_req,
172 outarg.nodeid, 1);
173 return 0;
175 spin_lock(&fc->lock);
176 fi->nlookup ++;
177 spin_unlock(&fc->lock);
179 fuse_put_request(fc, forget_req);
180 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
181 return 0;
183 fuse_change_attributes(inode, &outarg.attr);
184 fuse_change_timeout(entry, &outarg);
186 return 1;
189 static int invalid_nodeid(u64 nodeid)
191 return !nodeid || nodeid == FUSE_ROOT_ID;
194 static struct dentry_operations fuse_dentry_operations = {
195 .d_revalidate = fuse_dentry_revalidate,
198 static int valid_mode(int m)
200 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
201 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
205 * Add a directory inode to a dentry, ensuring that no other dentry
206 * refers to this inode. Called with fc->inst_mutex.
208 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
210 struct dentry *alias = d_find_alias(inode);
211 if (alias) {
212 /* This tries to shrink the subtree below alias */
213 fuse_invalidate_entry(alias);
214 dput(alias);
215 if (!list_empty(&inode->i_dentry))
216 return -EBUSY;
218 d_add(entry, inode);
219 return 0;
222 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
223 struct nameidata *nd)
225 int err;
226 struct fuse_entry_out outarg;
227 struct inode *inode = NULL;
228 struct fuse_conn *fc = get_fuse_conn(dir);
229 struct fuse_req *req;
230 struct fuse_req *forget_req;
232 if (entry->d_name.len > FUSE_NAME_MAX)
233 return ERR_PTR(-ENAMETOOLONG);
235 req = fuse_get_req(fc);
236 if (IS_ERR(req))
237 return ERR_PTR(PTR_ERR(req));
239 forget_req = fuse_get_req(fc);
240 if (IS_ERR(forget_req)) {
241 fuse_put_request(fc, req);
242 return ERR_PTR(PTR_ERR(forget_req));
245 fuse_lookup_init(req, dir, entry, &outarg);
246 request_send(fc, req);
247 err = req->out.h.error;
248 fuse_put_request(fc, req);
249 /* Zero nodeid is same as -ENOENT, but with valid timeout */
250 if (!err && outarg.nodeid &&
251 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
252 err = -EIO;
253 if (!err && outarg.nodeid) {
254 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
255 &outarg.attr);
256 if (!inode) {
257 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
258 return ERR_PTR(-ENOMEM);
261 fuse_put_request(fc, forget_req);
262 if (err && err != -ENOENT)
263 return ERR_PTR(err);
265 if (inode && S_ISDIR(inode->i_mode)) {
266 mutex_lock(&fc->inst_mutex);
267 err = fuse_d_add_directory(entry, inode);
268 mutex_unlock(&fc->inst_mutex);
269 if (err) {
270 iput(inode);
271 return ERR_PTR(err);
273 } else
274 d_add(entry, inode);
276 entry->d_op = &fuse_dentry_operations;
277 if (!err)
278 fuse_change_timeout(entry, &outarg);
279 else
280 fuse_invalidate_entry_cache(entry);
281 return NULL;
285 * Synchronous release for the case when something goes wrong in CREATE_OPEN
287 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
288 u64 nodeid, int flags)
290 struct fuse_req *req;
292 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
293 req->force = 1;
294 request_send(fc, req);
295 fuse_put_request(fc, req);
299 * Atomic create+open operation
301 * If the filesystem doesn't support this, then fall back to separate
302 * 'mknod' + 'open' requests.
304 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
305 struct nameidata *nd)
307 int err;
308 struct inode *inode;
309 struct fuse_conn *fc = get_fuse_conn(dir);
310 struct fuse_req *req;
311 struct fuse_req *forget_req;
312 struct fuse_open_in inarg;
313 struct fuse_open_out outopen;
314 struct fuse_entry_out outentry;
315 struct fuse_file *ff;
316 struct file *file;
317 int flags = nd->intent.open.flags - 1;
319 if (fc->no_create)
320 return -ENOSYS;
322 forget_req = fuse_get_req(fc);
323 if (IS_ERR(forget_req))
324 return PTR_ERR(forget_req);
326 req = fuse_get_req(fc);
327 err = PTR_ERR(req);
328 if (IS_ERR(req))
329 goto out_put_forget_req;
331 err = -ENOMEM;
332 ff = fuse_file_alloc();
333 if (!ff)
334 goto out_put_request;
336 flags &= ~O_NOCTTY;
337 memset(&inarg, 0, sizeof(inarg));
338 inarg.flags = flags;
339 inarg.mode = mode;
340 req->in.h.opcode = FUSE_CREATE;
341 req->in.h.nodeid = get_node_id(dir);
342 req->in.numargs = 2;
343 req->in.args[0].size = sizeof(inarg);
344 req->in.args[0].value = &inarg;
345 req->in.args[1].size = entry->d_name.len + 1;
346 req->in.args[1].value = entry->d_name.name;
347 req->out.numargs = 2;
348 req->out.args[0].size = sizeof(outentry);
349 req->out.args[0].value = &outentry;
350 req->out.args[1].size = sizeof(outopen);
351 req->out.args[1].value = &outopen;
352 request_send(fc, req);
353 err = req->out.h.error;
354 if (err) {
355 if (err == -ENOSYS)
356 fc->no_create = 1;
357 goto out_free_ff;
360 err = -EIO;
361 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
362 goto out_free_ff;
364 fuse_put_request(fc, req);
365 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
366 &outentry.attr);
367 if (!inode) {
368 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
369 ff->fh = outopen.fh;
370 fuse_sync_release(fc, ff, outentry.nodeid, flags);
371 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
372 return -ENOMEM;
374 fuse_put_request(fc, forget_req);
375 d_instantiate(entry, inode);
376 fuse_change_timeout(entry, &outentry);
377 file = lookup_instantiate_filp(nd, entry, generic_file_open);
378 if (IS_ERR(file)) {
379 ff->fh = outopen.fh;
380 fuse_sync_release(fc, ff, outentry.nodeid, flags);
381 return PTR_ERR(file);
383 fuse_finish_open(inode, file, ff, &outopen);
384 return 0;
386 out_free_ff:
387 fuse_file_free(ff);
388 out_put_request:
389 fuse_put_request(fc, req);
390 out_put_forget_req:
391 fuse_put_request(fc, forget_req);
392 return err;
396 * Code shared between mknod, mkdir, symlink and link
398 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
399 struct inode *dir, struct dentry *entry,
400 int mode)
402 struct fuse_entry_out outarg;
403 struct inode *inode;
404 int err;
405 struct fuse_req *forget_req;
407 forget_req = fuse_get_req(fc);
408 if (IS_ERR(forget_req)) {
409 fuse_put_request(fc, req);
410 return PTR_ERR(forget_req);
413 req->in.h.nodeid = get_node_id(dir);
414 req->out.numargs = 1;
415 req->out.args[0].size = sizeof(outarg);
416 req->out.args[0].value = &outarg;
417 request_send(fc, req);
418 err = req->out.h.error;
419 fuse_put_request(fc, req);
420 if (err)
421 goto out_put_forget_req;
423 err = -EIO;
424 if (invalid_nodeid(outarg.nodeid))
425 goto out_put_forget_req;
427 if ((outarg.attr.mode ^ mode) & S_IFMT)
428 goto out_put_forget_req;
430 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
431 &outarg.attr);
432 if (!inode) {
433 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
434 return -ENOMEM;
436 fuse_put_request(fc, forget_req);
438 if (S_ISDIR(inode->i_mode)) {
439 struct dentry *alias;
440 mutex_lock(&fc->inst_mutex);
441 alias = d_find_alias(inode);
442 if (alias) {
443 /* New directory must have moved since mkdir */
444 mutex_unlock(&fc->inst_mutex);
445 dput(alias);
446 iput(inode);
447 return -EBUSY;
449 d_instantiate(entry, inode);
450 mutex_unlock(&fc->inst_mutex);
451 } else
452 d_instantiate(entry, inode);
454 fuse_change_timeout(entry, &outarg);
455 fuse_invalidate_attr(dir);
456 return 0;
458 out_put_forget_req:
459 fuse_put_request(fc, forget_req);
460 return err;
463 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
464 dev_t rdev)
466 struct fuse_mknod_in inarg;
467 struct fuse_conn *fc = get_fuse_conn(dir);
468 struct fuse_req *req = fuse_get_req(fc);
469 if (IS_ERR(req))
470 return PTR_ERR(req);
472 memset(&inarg, 0, sizeof(inarg));
473 inarg.mode = mode;
474 inarg.rdev = new_encode_dev(rdev);
475 req->in.h.opcode = FUSE_MKNOD;
476 req->in.numargs = 2;
477 req->in.args[0].size = sizeof(inarg);
478 req->in.args[0].value = &inarg;
479 req->in.args[1].size = entry->d_name.len + 1;
480 req->in.args[1].value = entry->d_name.name;
481 return create_new_entry(fc, req, dir, entry, mode);
484 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
485 struct nameidata *nd)
487 if (nd && (nd->flags & LOOKUP_CREATE)) {
488 int err = fuse_create_open(dir, entry, mode, nd);
489 if (err != -ENOSYS)
490 return err;
491 /* Fall back on mknod */
493 return fuse_mknod(dir, entry, mode, 0);
496 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
498 struct fuse_mkdir_in inarg;
499 struct fuse_conn *fc = get_fuse_conn(dir);
500 struct fuse_req *req = fuse_get_req(fc);
501 if (IS_ERR(req))
502 return PTR_ERR(req);
504 memset(&inarg, 0, sizeof(inarg));
505 inarg.mode = mode;
506 req->in.h.opcode = FUSE_MKDIR;
507 req->in.numargs = 2;
508 req->in.args[0].size = sizeof(inarg);
509 req->in.args[0].value = &inarg;
510 req->in.args[1].size = entry->d_name.len + 1;
511 req->in.args[1].value = entry->d_name.name;
512 return create_new_entry(fc, req, dir, entry, S_IFDIR);
515 static int fuse_symlink(struct inode *dir, struct dentry *entry,
516 const char *link)
518 struct fuse_conn *fc = get_fuse_conn(dir);
519 unsigned len = strlen(link) + 1;
520 struct fuse_req *req = fuse_get_req(fc);
521 if (IS_ERR(req))
522 return PTR_ERR(req);
524 req->in.h.opcode = FUSE_SYMLINK;
525 req->in.numargs = 2;
526 req->in.args[0].size = entry->d_name.len + 1;
527 req->in.args[0].value = entry->d_name.name;
528 req->in.args[1].size = len;
529 req->in.args[1].value = link;
530 return create_new_entry(fc, req, dir, entry, S_IFLNK);
533 static int fuse_unlink(struct inode *dir, struct dentry *entry)
535 int err;
536 struct fuse_conn *fc = get_fuse_conn(dir);
537 struct fuse_req *req = fuse_get_req(fc);
538 if (IS_ERR(req))
539 return PTR_ERR(req);
541 req->in.h.opcode = FUSE_UNLINK;
542 req->in.h.nodeid = get_node_id(dir);
543 req->in.numargs = 1;
544 req->in.args[0].size = entry->d_name.len + 1;
545 req->in.args[0].value = entry->d_name.name;
546 request_send(fc, req);
547 err = req->out.h.error;
548 fuse_put_request(fc, req);
549 if (!err) {
550 struct inode *inode = entry->d_inode;
552 /* Set nlink to zero so the inode can be cleared, if
553 the inode does have more links this will be
554 discovered at the next lookup/getattr */
555 clear_nlink(inode);
556 fuse_invalidate_attr(inode);
557 fuse_invalidate_attr(dir);
558 fuse_invalidate_entry_cache(entry);
559 } else if (err == -EINTR)
560 fuse_invalidate_entry(entry);
561 return err;
564 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
566 int err;
567 struct fuse_conn *fc = get_fuse_conn(dir);
568 struct fuse_req *req = fuse_get_req(fc);
569 if (IS_ERR(req))
570 return PTR_ERR(req);
572 req->in.h.opcode = FUSE_RMDIR;
573 req->in.h.nodeid = get_node_id(dir);
574 req->in.numargs = 1;
575 req->in.args[0].size = entry->d_name.len + 1;
576 req->in.args[0].value = entry->d_name.name;
577 request_send(fc, req);
578 err = req->out.h.error;
579 fuse_put_request(fc, req);
580 if (!err) {
581 clear_nlink(entry->d_inode);
582 fuse_invalidate_attr(dir);
583 fuse_invalidate_entry_cache(entry);
584 } else if (err == -EINTR)
585 fuse_invalidate_entry(entry);
586 return err;
589 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
590 struct inode *newdir, struct dentry *newent)
592 int err;
593 struct fuse_rename_in inarg;
594 struct fuse_conn *fc = get_fuse_conn(olddir);
595 struct fuse_req *req = fuse_get_req(fc);
596 if (IS_ERR(req))
597 return PTR_ERR(req);
599 memset(&inarg, 0, sizeof(inarg));
600 inarg.newdir = get_node_id(newdir);
601 req->in.h.opcode = FUSE_RENAME;
602 req->in.h.nodeid = get_node_id(olddir);
603 req->in.numargs = 3;
604 req->in.args[0].size = sizeof(inarg);
605 req->in.args[0].value = &inarg;
606 req->in.args[1].size = oldent->d_name.len + 1;
607 req->in.args[1].value = oldent->d_name.name;
608 req->in.args[2].size = newent->d_name.len + 1;
609 req->in.args[2].value = newent->d_name.name;
610 request_send(fc, req);
611 err = req->out.h.error;
612 fuse_put_request(fc, req);
613 if (!err) {
614 fuse_invalidate_attr(olddir);
615 if (olddir != newdir)
616 fuse_invalidate_attr(newdir);
618 /* newent will end up negative */
619 if (newent->d_inode)
620 fuse_invalidate_entry_cache(newent);
621 } else if (err == -EINTR) {
622 /* If request was interrupted, DEITY only knows if the
623 rename actually took place. If the invalidation
624 fails (e.g. some process has CWD under the renamed
625 directory), then there can be inconsistency between
626 the dcache and the real filesystem. Tough luck. */
627 fuse_invalidate_entry(oldent);
628 if (newent->d_inode)
629 fuse_invalidate_entry(newent);
632 return err;
635 static int fuse_link(struct dentry *entry, struct inode *newdir,
636 struct dentry *newent)
638 int err;
639 struct fuse_link_in inarg;
640 struct inode *inode = entry->d_inode;
641 struct fuse_conn *fc = get_fuse_conn(inode);
642 struct fuse_req *req = fuse_get_req(fc);
643 if (IS_ERR(req))
644 return PTR_ERR(req);
646 memset(&inarg, 0, sizeof(inarg));
647 inarg.oldnodeid = get_node_id(inode);
648 req->in.h.opcode = FUSE_LINK;
649 req->in.numargs = 2;
650 req->in.args[0].size = sizeof(inarg);
651 req->in.args[0].value = &inarg;
652 req->in.args[1].size = newent->d_name.len + 1;
653 req->in.args[1].value = newent->d_name.name;
654 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
655 /* Contrary to "normal" filesystems it can happen that link
656 makes two "logical" inodes point to the same "physical"
657 inode. We invalidate the attributes of the old one, so it
658 will reflect changes in the backing inode (link count,
659 etc.)
661 if (!err || err == -EINTR)
662 fuse_invalidate_attr(inode);
663 return err;
666 int fuse_do_getattr(struct inode *inode)
668 int err;
669 struct fuse_attr_out arg;
670 struct fuse_conn *fc = get_fuse_conn(inode);
671 struct fuse_req *req = fuse_get_req(fc);
672 if (IS_ERR(req))
673 return PTR_ERR(req);
675 req->in.h.opcode = FUSE_GETATTR;
676 req->in.h.nodeid = get_node_id(inode);
677 req->out.numargs = 1;
678 req->out.args[0].size = sizeof(arg);
679 req->out.args[0].value = &arg;
680 request_send(fc, req);
681 err = req->out.h.error;
682 fuse_put_request(fc, req);
683 if (!err) {
684 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
685 make_bad_inode(inode);
686 err = -EIO;
687 } else {
688 struct fuse_inode *fi = get_fuse_inode(inode);
689 fuse_change_attributes(inode, &arg.attr);
690 fi->i_time = time_to_jiffies(arg.attr_valid,
691 arg.attr_valid_nsec);
694 return err;
698 * Calling into a user-controlled filesystem gives the filesystem
699 * daemon ptrace-like capabilities over the requester process. This
700 * means, that the filesystem daemon is able to record the exact
701 * filesystem operations performed, and can also control the behavior
702 * of the requester process in otherwise impossible ways. For example
703 * it can delay the operation for arbitrary length of time allowing
704 * DoS against the requester.
706 * For this reason only those processes can call into the filesystem,
707 * for which the owner of the mount has ptrace privilege. This
708 * excludes processes started by other users, suid or sgid processes.
710 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
712 if (fc->flags & FUSE_ALLOW_OTHER)
713 return 1;
715 if (task->euid == fc->user_id &&
716 task->suid == fc->user_id &&
717 task->uid == fc->user_id &&
718 task->egid == fc->group_id &&
719 task->sgid == fc->group_id &&
720 task->gid == fc->group_id)
721 return 1;
723 return 0;
727 * Check whether the inode attributes are still valid
729 * If the attribute validity timeout has expired, then fetch the fresh
730 * attributes with a 'getattr' request
732 * I'm not sure why cached attributes are never returned for the root
733 * inode, this is probably being too cautious.
735 static int fuse_revalidate(struct dentry *entry)
737 struct inode *inode = entry->d_inode;
738 struct fuse_inode *fi = get_fuse_inode(inode);
739 struct fuse_conn *fc = get_fuse_conn(inode);
741 if (!fuse_allow_task(fc, current))
742 return -EACCES;
743 if (get_node_id(inode) != FUSE_ROOT_ID &&
744 fi->i_time >= get_jiffies_64())
745 return 0;
747 return fuse_do_getattr(inode);
750 static int fuse_access(struct inode *inode, int mask)
752 struct fuse_conn *fc = get_fuse_conn(inode);
753 struct fuse_req *req;
754 struct fuse_access_in inarg;
755 int err;
757 if (fc->no_access)
758 return 0;
760 req = fuse_get_req(fc);
761 if (IS_ERR(req))
762 return PTR_ERR(req);
764 memset(&inarg, 0, sizeof(inarg));
765 inarg.mask = mask;
766 req->in.h.opcode = FUSE_ACCESS;
767 req->in.h.nodeid = get_node_id(inode);
768 req->in.numargs = 1;
769 req->in.args[0].size = sizeof(inarg);
770 req->in.args[0].value = &inarg;
771 request_send(fc, req);
772 err = req->out.h.error;
773 fuse_put_request(fc, req);
774 if (err == -ENOSYS) {
775 fc->no_access = 1;
776 err = 0;
778 return err;
782 * Check permission. The two basic access models of FUSE are:
784 * 1) Local access checking ('default_permissions' mount option) based
785 * on file mode. This is the plain old disk filesystem permission
786 * modell.
788 * 2) "Remote" access checking, where server is responsible for
789 * checking permission in each inode operation. An exception to this
790 * is if ->permission() was invoked from sys_access() in which case an
791 * access request is sent. Execute permission is still checked
792 * locally based on file mode.
794 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
796 struct fuse_conn *fc = get_fuse_conn(inode);
798 if (!fuse_allow_task(fc, current))
799 return -EACCES;
800 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
801 int err = generic_permission(inode, mask, NULL);
803 /* If permission is denied, try to refresh file
804 attributes. This is also needed, because the root
805 node will at first have no permissions */
806 if (err == -EACCES) {
807 err = fuse_do_getattr(inode);
808 if (!err)
809 err = generic_permission(inode, mask, NULL);
812 /* Note: the opposite of the above test does not
813 exist. So if permissions are revoked this won't be
814 noticed immediately, only after the attribute
815 timeout has expired */
817 return err;
818 } else {
819 int mode = inode->i_mode;
820 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
821 return -EACCES;
823 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
824 return fuse_access(inode, mask);
825 return 0;
829 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
830 void *dstbuf, filldir_t filldir)
832 while (nbytes >= FUSE_NAME_OFFSET) {
833 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
834 size_t reclen = FUSE_DIRENT_SIZE(dirent);
835 int over;
836 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
837 return -EIO;
838 if (reclen > nbytes)
839 break;
841 over = filldir(dstbuf, dirent->name, dirent->namelen,
842 file->f_pos, dirent->ino, dirent->type);
843 if (over)
844 break;
846 buf += reclen;
847 nbytes -= reclen;
848 file->f_pos = dirent->off;
851 return 0;
854 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
856 int err;
857 size_t nbytes;
858 struct page *page;
859 struct inode *inode = file->f_path.dentry->d_inode;
860 struct fuse_conn *fc = get_fuse_conn(inode);
861 struct fuse_req *req;
863 if (is_bad_inode(inode))
864 return -EIO;
866 req = fuse_get_req(fc);
867 if (IS_ERR(req))
868 return PTR_ERR(req);
870 page = alloc_page(GFP_KERNEL);
871 if (!page) {
872 fuse_put_request(fc, req);
873 return -ENOMEM;
875 req->num_pages = 1;
876 req->pages[0] = page;
877 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
878 request_send(fc, req);
879 nbytes = req->out.args[0].size;
880 err = req->out.h.error;
881 fuse_put_request(fc, req);
882 if (!err)
883 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
884 filldir);
886 __free_page(page);
887 fuse_invalidate_attr(inode); /* atime changed */
888 return err;
891 static char *read_link(struct dentry *dentry)
893 struct inode *inode = dentry->d_inode;
894 struct fuse_conn *fc = get_fuse_conn(inode);
895 struct fuse_req *req = fuse_get_req(fc);
896 char *link;
898 if (IS_ERR(req))
899 return ERR_PTR(PTR_ERR(req));
901 link = (char *) __get_free_page(GFP_KERNEL);
902 if (!link) {
903 link = ERR_PTR(-ENOMEM);
904 goto out;
906 req->in.h.opcode = FUSE_READLINK;
907 req->in.h.nodeid = get_node_id(inode);
908 req->out.argvar = 1;
909 req->out.numargs = 1;
910 req->out.args[0].size = PAGE_SIZE - 1;
911 req->out.args[0].value = link;
912 request_send(fc, req);
913 if (req->out.h.error) {
914 free_page((unsigned long) link);
915 link = ERR_PTR(req->out.h.error);
916 } else
917 link[req->out.args[0].size] = '\0';
918 out:
919 fuse_put_request(fc, req);
920 fuse_invalidate_attr(inode); /* atime changed */
921 return link;
924 static void free_link(char *link)
926 if (!IS_ERR(link))
927 free_page((unsigned long) link);
930 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
932 nd_set_link(nd, read_link(dentry));
933 return NULL;
936 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
938 free_link(nd_get_link(nd));
941 static int fuse_dir_open(struct inode *inode, struct file *file)
943 return fuse_open_common(inode, file, 1);
946 static int fuse_dir_release(struct inode *inode, struct file *file)
948 return fuse_release_common(inode, file, 1);
951 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
953 /* nfsd can call this with no file */
954 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
957 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
959 unsigned ivalid = iattr->ia_valid;
961 if (ivalid & ATTR_MODE)
962 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
963 if (ivalid & ATTR_UID)
964 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
965 if (ivalid & ATTR_GID)
966 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
967 if (ivalid & ATTR_SIZE)
968 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
969 /* You can only _set_ these together (they may change by themselves) */
970 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
971 arg->valid |= FATTR_ATIME | FATTR_MTIME;
972 arg->atime = iattr->ia_atime.tv_sec;
973 arg->mtime = iattr->ia_mtime.tv_sec;
975 if (ivalid & ATTR_FILE) {
976 struct fuse_file *ff = iattr->ia_file->private_data;
977 arg->valid |= FATTR_FH;
978 arg->fh = ff->fh;
982 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
984 struct fuse_conn *fc = get_fuse_conn(inode);
985 int need_trunc;
987 spin_lock(&fc->lock);
988 need_trunc = inode->i_size > offset;
989 i_size_write(inode, offset);
990 spin_unlock(&fc->lock);
992 if (need_trunc) {
993 struct address_space *mapping = inode->i_mapping;
994 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
995 truncate_inode_pages(mapping, offset);
1000 * Set attributes, and at the same time refresh them.
1002 * Truncation is slightly complicated, because the 'truncate' request
1003 * may fail, in which case we don't want to touch the mapping.
1004 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1005 * and the actual truncation by hand.
1007 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1009 struct inode *inode = entry->d_inode;
1010 struct fuse_conn *fc = get_fuse_conn(inode);
1011 struct fuse_inode *fi = get_fuse_inode(inode);
1012 struct fuse_req *req;
1013 struct fuse_setattr_in inarg;
1014 struct fuse_attr_out outarg;
1015 int err;
1016 int is_truncate = 0;
1018 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1019 err = inode_change_ok(inode, attr);
1020 if (err)
1021 return err;
1024 if (attr->ia_valid & ATTR_SIZE) {
1025 unsigned long limit;
1026 is_truncate = 1;
1027 if (IS_SWAPFILE(inode))
1028 return -ETXTBSY;
1029 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1030 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1031 send_sig(SIGXFSZ, current, 0);
1032 return -EFBIG;
1036 req = fuse_get_req(fc);
1037 if (IS_ERR(req))
1038 return PTR_ERR(req);
1040 memset(&inarg, 0, sizeof(inarg));
1041 iattr_to_fattr(attr, &inarg);
1042 req->in.h.opcode = FUSE_SETATTR;
1043 req->in.h.nodeid = get_node_id(inode);
1044 req->in.numargs = 1;
1045 req->in.args[0].size = sizeof(inarg);
1046 req->in.args[0].value = &inarg;
1047 req->out.numargs = 1;
1048 req->out.args[0].size = sizeof(outarg);
1049 req->out.args[0].value = &outarg;
1050 request_send(fc, req);
1051 err = req->out.h.error;
1052 fuse_put_request(fc, req);
1053 if (!err) {
1054 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1055 make_bad_inode(inode);
1056 err = -EIO;
1057 } else {
1058 if (is_truncate)
1059 fuse_vmtruncate(inode, outarg.attr.size);
1060 fuse_change_attributes(inode, &outarg.attr);
1061 fi->i_time = time_to_jiffies(outarg.attr_valid,
1062 outarg.attr_valid_nsec);
1064 } else if (err == -EINTR)
1065 fuse_invalidate_attr(inode);
1067 return err;
1070 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1071 struct kstat *stat)
1073 struct inode *inode = entry->d_inode;
1074 int err = fuse_revalidate(entry);
1075 if (!err)
1076 generic_fillattr(inode, stat);
1078 return err;
1081 static int fuse_setxattr(struct dentry *entry, const char *name,
1082 const void *value, size_t size, int flags)
1084 struct inode *inode = entry->d_inode;
1085 struct fuse_conn *fc = get_fuse_conn(inode);
1086 struct fuse_req *req;
1087 struct fuse_setxattr_in inarg;
1088 int err;
1090 if (fc->no_setxattr)
1091 return -EOPNOTSUPP;
1093 req = fuse_get_req(fc);
1094 if (IS_ERR(req))
1095 return PTR_ERR(req);
1097 memset(&inarg, 0, sizeof(inarg));
1098 inarg.size = size;
1099 inarg.flags = flags;
1100 req->in.h.opcode = FUSE_SETXATTR;
1101 req->in.h.nodeid = get_node_id(inode);
1102 req->in.numargs = 3;
1103 req->in.args[0].size = sizeof(inarg);
1104 req->in.args[0].value = &inarg;
1105 req->in.args[1].size = strlen(name) + 1;
1106 req->in.args[1].value = name;
1107 req->in.args[2].size = size;
1108 req->in.args[2].value = value;
1109 request_send(fc, req);
1110 err = req->out.h.error;
1111 fuse_put_request(fc, req);
1112 if (err == -ENOSYS) {
1113 fc->no_setxattr = 1;
1114 err = -EOPNOTSUPP;
1116 return err;
1119 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1120 void *value, size_t size)
1122 struct inode *inode = entry->d_inode;
1123 struct fuse_conn *fc = get_fuse_conn(inode);
1124 struct fuse_req *req;
1125 struct fuse_getxattr_in inarg;
1126 struct fuse_getxattr_out outarg;
1127 ssize_t ret;
1129 if (fc->no_getxattr)
1130 return -EOPNOTSUPP;
1132 req = fuse_get_req(fc);
1133 if (IS_ERR(req))
1134 return PTR_ERR(req);
1136 memset(&inarg, 0, sizeof(inarg));
1137 inarg.size = size;
1138 req->in.h.opcode = FUSE_GETXATTR;
1139 req->in.h.nodeid = get_node_id(inode);
1140 req->in.numargs = 2;
1141 req->in.args[0].size = sizeof(inarg);
1142 req->in.args[0].value = &inarg;
1143 req->in.args[1].size = strlen(name) + 1;
1144 req->in.args[1].value = name;
1145 /* This is really two different operations rolled into one */
1146 req->out.numargs = 1;
1147 if (size) {
1148 req->out.argvar = 1;
1149 req->out.args[0].size = size;
1150 req->out.args[0].value = value;
1151 } else {
1152 req->out.args[0].size = sizeof(outarg);
1153 req->out.args[0].value = &outarg;
1155 request_send(fc, req);
1156 ret = req->out.h.error;
1157 if (!ret)
1158 ret = size ? req->out.args[0].size : outarg.size;
1159 else {
1160 if (ret == -ENOSYS) {
1161 fc->no_getxattr = 1;
1162 ret = -EOPNOTSUPP;
1165 fuse_put_request(fc, req);
1166 return ret;
1169 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1171 struct inode *inode = entry->d_inode;
1172 struct fuse_conn *fc = get_fuse_conn(inode);
1173 struct fuse_req *req;
1174 struct fuse_getxattr_in inarg;
1175 struct fuse_getxattr_out outarg;
1176 ssize_t ret;
1178 if (fc->no_listxattr)
1179 return -EOPNOTSUPP;
1181 req = fuse_get_req(fc);
1182 if (IS_ERR(req))
1183 return PTR_ERR(req);
1185 memset(&inarg, 0, sizeof(inarg));
1186 inarg.size = size;
1187 req->in.h.opcode = FUSE_LISTXATTR;
1188 req->in.h.nodeid = get_node_id(inode);
1189 req->in.numargs = 1;
1190 req->in.args[0].size = sizeof(inarg);
1191 req->in.args[0].value = &inarg;
1192 /* This is really two different operations rolled into one */
1193 req->out.numargs = 1;
1194 if (size) {
1195 req->out.argvar = 1;
1196 req->out.args[0].size = size;
1197 req->out.args[0].value = list;
1198 } else {
1199 req->out.args[0].size = sizeof(outarg);
1200 req->out.args[0].value = &outarg;
1202 request_send(fc, req);
1203 ret = req->out.h.error;
1204 if (!ret)
1205 ret = size ? req->out.args[0].size : outarg.size;
1206 else {
1207 if (ret == -ENOSYS) {
1208 fc->no_listxattr = 1;
1209 ret = -EOPNOTSUPP;
1212 fuse_put_request(fc, req);
1213 return ret;
1216 static int fuse_removexattr(struct dentry *entry, const char *name)
1218 struct inode *inode = entry->d_inode;
1219 struct fuse_conn *fc = get_fuse_conn(inode);
1220 struct fuse_req *req;
1221 int err;
1223 if (fc->no_removexattr)
1224 return -EOPNOTSUPP;
1226 req = fuse_get_req(fc);
1227 if (IS_ERR(req))
1228 return PTR_ERR(req);
1230 req->in.h.opcode = FUSE_REMOVEXATTR;
1231 req->in.h.nodeid = get_node_id(inode);
1232 req->in.numargs = 1;
1233 req->in.args[0].size = strlen(name) + 1;
1234 req->in.args[0].value = name;
1235 request_send(fc, req);
1236 err = req->out.h.error;
1237 fuse_put_request(fc, req);
1238 if (err == -ENOSYS) {
1239 fc->no_removexattr = 1;
1240 err = -EOPNOTSUPP;
1242 return err;
1245 static struct inode_operations fuse_dir_inode_operations = {
1246 .lookup = fuse_lookup,
1247 .mkdir = fuse_mkdir,
1248 .symlink = fuse_symlink,
1249 .unlink = fuse_unlink,
1250 .rmdir = fuse_rmdir,
1251 .rename = fuse_rename,
1252 .link = fuse_link,
1253 .setattr = fuse_setattr,
1254 .create = fuse_create,
1255 .mknod = fuse_mknod,
1256 .permission = fuse_permission,
1257 .getattr = fuse_getattr,
1258 .setxattr = fuse_setxattr,
1259 .getxattr = fuse_getxattr,
1260 .listxattr = fuse_listxattr,
1261 .removexattr = fuse_removexattr,
1264 static const struct file_operations fuse_dir_operations = {
1265 .llseek = generic_file_llseek,
1266 .read = generic_read_dir,
1267 .readdir = fuse_readdir,
1268 .open = fuse_dir_open,
1269 .release = fuse_dir_release,
1270 .fsync = fuse_dir_fsync,
1273 static struct inode_operations fuse_common_inode_operations = {
1274 .setattr = fuse_setattr,
1275 .permission = fuse_permission,
1276 .getattr = fuse_getattr,
1277 .setxattr = fuse_setxattr,
1278 .getxattr = fuse_getxattr,
1279 .listxattr = fuse_listxattr,
1280 .removexattr = fuse_removexattr,
1283 static struct inode_operations fuse_symlink_inode_operations = {
1284 .setattr = fuse_setattr,
1285 .follow_link = fuse_follow_link,
1286 .put_link = fuse_put_link,
1287 .readlink = generic_readlink,
1288 .getattr = fuse_getattr,
1289 .setxattr = fuse_setxattr,
1290 .getxattr = fuse_getxattr,
1291 .listxattr = fuse_listxattr,
1292 .removexattr = fuse_removexattr,
1295 void fuse_init_common(struct inode *inode)
1297 inode->i_op = &fuse_common_inode_operations;
1300 void fuse_init_dir(struct inode *inode)
1302 inode->i_op = &fuse_dir_inode_operations;
1303 inode->i_fop = &fuse_dir_operations;
1306 void fuse_init_symlink(struct inode *inode)
1308 inode->i_op = &fuse_symlink_inode_operations;