[NET_SCHED]: Fix endless loops caused by inaccurate qlen counters (part 1)
[linux-2.6.22.y-op.git] / fs / fuse / dir.c
blobc71a6c092ad9d4f372553dac847c1af91355d048
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 /* Doesn't hurt to "reset" the validity timeout */
145 fuse_invalidate_entry_cache(entry);
147 /* For negative dentries, always do a fresh lookup */
148 if (!inode)
149 return 0;
151 fc = get_fuse_conn(inode);
152 req = fuse_get_req(fc);
153 if (IS_ERR(req))
154 return 0;
156 forget_req = fuse_get_req(fc);
157 if (IS_ERR(forget_req)) {
158 fuse_put_request(fc, req);
159 return 0;
162 parent = dget_parent(entry);
163 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
164 request_send(fc, req);
165 dput(parent);
166 err = req->out.h.error;
167 fuse_put_request(fc, req);
168 /* Zero nodeid is same as -ENOENT */
169 if (!err && !outarg.nodeid)
170 err = -ENOENT;
171 if (!err) {
172 struct fuse_inode *fi = get_fuse_inode(inode);
173 if (outarg.nodeid != get_node_id(inode)) {
174 fuse_send_forget(fc, forget_req,
175 outarg.nodeid, 1);
176 return 0;
178 spin_lock(&fc->lock);
179 fi->nlookup ++;
180 spin_unlock(&fc->lock);
182 fuse_put_request(fc, forget_req);
183 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
184 return 0;
186 fuse_change_attributes(inode, &outarg.attr);
187 fuse_change_timeout(entry, &outarg);
189 return 1;
192 static int invalid_nodeid(u64 nodeid)
194 return !nodeid || nodeid == FUSE_ROOT_ID;
197 static struct dentry_operations fuse_dentry_operations = {
198 .d_revalidate = fuse_dentry_revalidate,
201 static int valid_mode(int m)
203 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
204 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
208 * Add a directory inode to a dentry, ensuring that no other dentry
209 * refers to this inode. Called with fc->inst_mutex.
211 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
213 struct dentry *alias = d_find_alias(inode);
214 if (alias) {
215 /* This tries to shrink the subtree below alias */
216 fuse_invalidate_entry(alias);
217 dput(alias);
218 if (!list_empty(&inode->i_dentry))
219 return -EBUSY;
221 d_add(entry, inode);
222 return 0;
225 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
226 struct nameidata *nd)
228 int err;
229 struct fuse_entry_out outarg;
230 struct inode *inode = NULL;
231 struct fuse_conn *fc = get_fuse_conn(dir);
232 struct fuse_req *req;
233 struct fuse_req *forget_req;
235 if (entry->d_name.len > FUSE_NAME_MAX)
236 return ERR_PTR(-ENAMETOOLONG);
238 req = fuse_get_req(fc);
239 if (IS_ERR(req))
240 return ERR_PTR(PTR_ERR(req));
242 forget_req = fuse_get_req(fc);
243 if (IS_ERR(forget_req)) {
244 fuse_put_request(fc, req);
245 return ERR_PTR(PTR_ERR(forget_req));
248 fuse_lookup_init(req, dir, entry, &outarg);
249 request_send(fc, req);
250 err = req->out.h.error;
251 fuse_put_request(fc, req);
252 /* Zero nodeid is same as -ENOENT, but with valid timeout */
253 if (!err && outarg.nodeid &&
254 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
255 err = -EIO;
256 if (!err && outarg.nodeid) {
257 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
258 &outarg.attr);
259 if (!inode) {
260 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
261 return ERR_PTR(-ENOMEM);
264 fuse_put_request(fc, forget_req);
265 if (err && err != -ENOENT)
266 return ERR_PTR(err);
268 if (inode && S_ISDIR(inode->i_mode)) {
269 mutex_lock(&fc->inst_mutex);
270 err = fuse_d_add_directory(entry, inode);
271 mutex_unlock(&fc->inst_mutex);
272 if (err) {
273 iput(inode);
274 return ERR_PTR(err);
276 } else
277 d_add(entry, inode);
279 entry->d_op = &fuse_dentry_operations;
280 if (!err)
281 fuse_change_timeout(entry, &outarg);
282 else
283 fuse_invalidate_entry_cache(entry);
284 return NULL;
288 * Synchronous release for the case when something goes wrong in CREATE_OPEN
290 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
291 u64 nodeid, int flags)
293 struct fuse_req *req;
295 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
296 req->force = 1;
297 request_send(fc, req);
298 fuse_put_request(fc, req);
302 * Atomic create+open operation
304 * If the filesystem doesn't support this, then fall back to separate
305 * 'mknod' + 'open' requests.
307 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
308 struct nameidata *nd)
310 int err;
311 struct inode *inode;
312 struct fuse_conn *fc = get_fuse_conn(dir);
313 struct fuse_req *req;
314 struct fuse_req *forget_req;
315 struct fuse_open_in inarg;
316 struct fuse_open_out outopen;
317 struct fuse_entry_out outentry;
318 struct fuse_file *ff;
319 struct file *file;
320 int flags = nd->intent.open.flags - 1;
322 if (fc->no_create)
323 return -ENOSYS;
325 forget_req = fuse_get_req(fc);
326 if (IS_ERR(forget_req))
327 return PTR_ERR(forget_req);
329 req = fuse_get_req(fc);
330 err = PTR_ERR(req);
331 if (IS_ERR(req))
332 goto out_put_forget_req;
334 err = -ENOMEM;
335 ff = fuse_file_alloc();
336 if (!ff)
337 goto out_put_request;
339 flags &= ~O_NOCTTY;
340 memset(&inarg, 0, sizeof(inarg));
341 inarg.flags = flags;
342 inarg.mode = mode;
343 req->in.h.opcode = FUSE_CREATE;
344 req->in.h.nodeid = get_node_id(dir);
345 req->in.numargs = 2;
346 req->in.args[0].size = sizeof(inarg);
347 req->in.args[0].value = &inarg;
348 req->in.args[1].size = entry->d_name.len + 1;
349 req->in.args[1].value = entry->d_name.name;
350 req->out.numargs = 2;
351 req->out.args[0].size = sizeof(outentry);
352 req->out.args[0].value = &outentry;
353 req->out.args[1].size = sizeof(outopen);
354 req->out.args[1].value = &outopen;
355 request_send(fc, req);
356 err = req->out.h.error;
357 if (err) {
358 if (err == -ENOSYS)
359 fc->no_create = 1;
360 goto out_free_ff;
363 err = -EIO;
364 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
365 goto out_free_ff;
367 fuse_put_request(fc, req);
368 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
369 &outentry.attr);
370 if (!inode) {
371 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
372 ff->fh = outopen.fh;
373 fuse_sync_release(fc, ff, outentry.nodeid, flags);
374 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
375 return -ENOMEM;
377 fuse_put_request(fc, forget_req);
378 d_instantiate(entry, inode);
379 fuse_change_timeout(entry, &outentry);
380 file = lookup_instantiate_filp(nd, entry, generic_file_open);
381 if (IS_ERR(file)) {
382 ff->fh = outopen.fh;
383 fuse_sync_release(fc, ff, outentry.nodeid, flags);
384 return PTR_ERR(file);
386 fuse_finish_open(inode, file, ff, &outopen);
387 return 0;
389 out_free_ff:
390 fuse_file_free(ff);
391 out_put_request:
392 fuse_put_request(fc, req);
393 out_put_forget_req:
394 fuse_put_request(fc, forget_req);
395 return err;
399 * Code shared between mknod, mkdir, symlink and link
401 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
402 struct inode *dir, struct dentry *entry,
403 int mode)
405 struct fuse_entry_out outarg;
406 struct inode *inode;
407 int err;
408 struct fuse_req *forget_req;
410 forget_req = fuse_get_req(fc);
411 if (IS_ERR(forget_req)) {
412 fuse_put_request(fc, req);
413 return PTR_ERR(forget_req);
416 req->in.h.nodeid = get_node_id(dir);
417 req->out.numargs = 1;
418 req->out.args[0].size = sizeof(outarg);
419 req->out.args[0].value = &outarg;
420 request_send(fc, req);
421 err = req->out.h.error;
422 fuse_put_request(fc, req);
423 if (err)
424 goto out_put_forget_req;
426 err = -EIO;
427 if (invalid_nodeid(outarg.nodeid))
428 goto out_put_forget_req;
430 if ((outarg.attr.mode ^ mode) & S_IFMT)
431 goto out_put_forget_req;
433 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
434 &outarg.attr);
435 if (!inode) {
436 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
437 return -ENOMEM;
439 fuse_put_request(fc, forget_req);
441 if (S_ISDIR(inode->i_mode)) {
442 struct dentry *alias;
443 mutex_lock(&fc->inst_mutex);
444 alias = d_find_alias(inode);
445 if (alias) {
446 /* New directory must have moved since mkdir */
447 mutex_unlock(&fc->inst_mutex);
448 dput(alias);
449 iput(inode);
450 return -EBUSY;
452 d_instantiate(entry, inode);
453 mutex_unlock(&fc->inst_mutex);
454 } else
455 d_instantiate(entry, inode);
457 fuse_change_timeout(entry, &outarg);
458 fuse_invalidate_attr(dir);
459 return 0;
461 out_put_forget_req:
462 fuse_put_request(fc, forget_req);
463 return err;
466 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
467 dev_t rdev)
469 struct fuse_mknod_in inarg;
470 struct fuse_conn *fc = get_fuse_conn(dir);
471 struct fuse_req *req = fuse_get_req(fc);
472 if (IS_ERR(req))
473 return PTR_ERR(req);
475 memset(&inarg, 0, sizeof(inarg));
476 inarg.mode = mode;
477 inarg.rdev = new_encode_dev(rdev);
478 req->in.h.opcode = FUSE_MKNOD;
479 req->in.numargs = 2;
480 req->in.args[0].size = sizeof(inarg);
481 req->in.args[0].value = &inarg;
482 req->in.args[1].size = entry->d_name.len + 1;
483 req->in.args[1].value = entry->d_name.name;
484 return create_new_entry(fc, req, dir, entry, mode);
487 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
488 struct nameidata *nd)
490 if (nd && (nd->flags & LOOKUP_CREATE)) {
491 int err = fuse_create_open(dir, entry, mode, nd);
492 if (err != -ENOSYS)
493 return err;
494 /* Fall back on mknod */
496 return fuse_mknod(dir, entry, mode, 0);
499 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
501 struct fuse_mkdir_in inarg;
502 struct fuse_conn *fc = get_fuse_conn(dir);
503 struct fuse_req *req = fuse_get_req(fc);
504 if (IS_ERR(req))
505 return PTR_ERR(req);
507 memset(&inarg, 0, sizeof(inarg));
508 inarg.mode = mode;
509 req->in.h.opcode = FUSE_MKDIR;
510 req->in.numargs = 2;
511 req->in.args[0].size = sizeof(inarg);
512 req->in.args[0].value = &inarg;
513 req->in.args[1].size = entry->d_name.len + 1;
514 req->in.args[1].value = entry->d_name.name;
515 return create_new_entry(fc, req, dir, entry, S_IFDIR);
518 static int fuse_symlink(struct inode *dir, struct dentry *entry,
519 const char *link)
521 struct fuse_conn *fc = get_fuse_conn(dir);
522 unsigned len = strlen(link) + 1;
523 struct fuse_req *req = fuse_get_req(fc);
524 if (IS_ERR(req))
525 return PTR_ERR(req);
527 req->in.h.opcode = FUSE_SYMLINK;
528 req->in.numargs = 2;
529 req->in.args[0].size = entry->d_name.len + 1;
530 req->in.args[0].value = entry->d_name.name;
531 req->in.args[1].size = len;
532 req->in.args[1].value = link;
533 return create_new_entry(fc, req, dir, entry, S_IFLNK);
536 static int fuse_unlink(struct inode *dir, struct dentry *entry)
538 int err;
539 struct fuse_conn *fc = get_fuse_conn(dir);
540 struct fuse_req *req = fuse_get_req(fc);
541 if (IS_ERR(req))
542 return PTR_ERR(req);
544 req->in.h.opcode = FUSE_UNLINK;
545 req->in.h.nodeid = get_node_id(dir);
546 req->in.numargs = 1;
547 req->in.args[0].size = entry->d_name.len + 1;
548 req->in.args[0].value = entry->d_name.name;
549 request_send(fc, req);
550 err = req->out.h.error;
551 fuse_put_request(fc, req);
552 if (!err) {
553 struct inode *inode = entry->d_inode;
555 /* Set nlink to zero so the inode can be cleared, if
556 the inode does have more links this will be
557 discovered at the next lookup/getattr */
558 clear_nlink(inode);
559 fuse_invalidate_attr(inode);
560 fuse_invalidate_attr(dir);
561 fuse_invalidate_entry_cache(entry);
562 } else if (err == -EINTR)
563 fuse_invalidate_entry(entry);
564 return err;
567 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
569 int err;
570 struct fuse_conn *fc = get_fuse_conn(dir);
571 struct fuse_req *req = fuse_get_req(fc);
572 if (IS_ERR(req))
573 return PTR_ERR(req);
575 req->in.h.opcode = FUSE_RMDIR;
576 req->in.h.nodeid = get_node_id(dir);
577 req->in.numargs = 1;
578 req->in.args[0].size = entry->d_name.len + 1;
579 req->in.args[0].value = entry->d_name.name;
580 request_send(fc, req);
581 err = req->out.h.error;
582 fuse_put_request(fc, req);
583 if (!err) {
584 clear_nlink(entry->d_inode);
585 fuse_invalidate_attr(dir);
586 fuse_invalidate_entry_cache(entry);
587 } else if (err == -EINTR)
588 fuse_invalidate_entry(entry);
589 return err;
592 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
593 struct inode *newdir, struct dentry *newent)
595 int err;
596 struct fuse_rename_in inarg;
597 struct fuse_conn *fc = get_fuse_conn(olddir);
598 struct fuse_req *req = fuse_get_req(fc);
599 if (IS_ERR(req))
600 return PTR_ERR(req);
602 memset(&inarg, 0, sizeof(inarg));
603 inarg.newdir = get_node_id(newdir);
604 req->in.h.opcode = FUSE_RENAME;
605 req->in.h.nodeid = get_node_id(olddir);
606 req->in.numargs = 3;
607 req->in.args[0].size = sizeof(inarg);
608 req->in.args[0].value = &inarg;
609 req->in.args[1].size = oldent->d_name.len + 1;
610 req->in.args[1].value = oldent->d_name.name;
611 req->in.args[2].size = newent->d_name.len + 1;
612 req->in.args[2].value = newent->d_name.name;
613 request_send(fc, req);
614 err = req->out.h.error;
615 fuse_put_request(fc, req);
616 if (!err) {
617 fuse_invalidate_attr(olddir);
618 if (olddir != newdir)
619 fuse_invalidate_attr(newdir);
621 /* newent will end up negative */
622 if (newent->d_inode)
623 fuse_invalidate_entry_cache(newent);
624 } else if (err == -EINTR) {
625 /* If request was interrupted, DEITY only knows if the
626 rename actually took place. If the invalidation
627 fails (e.g. some process has CWD under the renamed
628 directory), then there can be inconsistency between
629 the dcache and the real filesystem. Tough luck. */
630 fuse_invalidate_entry(oldent);
631 if (newent->d_inode)
632 fuse_invalidate_entry(newent);
635 return err;
638 static int fuse_link(struct dentry *entry, struct inode *newdir,
639 struct dentry *newent)
641 int err;
642 struct fuse_link_in inarg;
643 struct inode *inode = entry->d_inode;
644 struct fuse_conn *fc = get_fuse_conn(inode);
645 struct fuse_req *req = fuse_get_req(fc);
646 if (IS_ERR(req))
647 return PTR_ERR(req);
649 memset(&inarg, 0, sizeof(inarg));
650 inarg.oldnodeid = get_node_id(inode);
651 req->in.h.opcode = FUSE_LINK;
652 req->in.numargs = 2;
653 req->in.args[0].size = sizeof(inarg);
654 req->in.args[0].value = &inarg;
655 req->in.args[1].size = newent->d_name.len + 1;
656 req->in.args[1].value = newent->d_name.name;
657 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
658 /* Contrary to "normal" filesystems it can happen that link
659 makes two "logical" inodes point to the same "physical"
660 inode. We invalidate the attributes of the old one, so it
661 will reflect changes in the backing inode (link count,
662 etc.)
664 if (!err || err == -EINTR)
665 fuse_invalidate_attr(inode);
666 return err;
669 int fuse_do_getattr(struct inode *inode)
671 int err;
672 struct fuse_attr_out arg;
673 struct fuse_conn *fc = get_fuse_conn(inode);
674 struct fuse_req *req = fuse_get_req(fc);
675 if (IS_ERR(req))
676 return PTR_ERR(req);
678 req->in.h.opcode = FUSE_GETATTR;
679 req->in.h.nodeid = get_node_id(inode);
680 req->out.numargs = 1;
681 req->out.args[0].size = sizeof(arg);
682 req->out.args[0].value = &arg;
683 request_send(fc, req);
684 err = req->out.h.error;
685 fuse_put_request(fc, req);
686 if (!err) {
687 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
688 make_bad_inode(inode);
689 err = -EIO;
690 } else {
691 struct fuse_inode *fi = get_fuse_inode(inode);
692 fuse_change_attributes(inode, &arg.attr);
693 fi->i_time = time_to_jiffies(arg.attr_valid,
694 arg.attr_valid_nsec);
697 return err;
701 * Calling into a user-controlled filesystem gives the filesystem
702 * daemon ptrace-like capabilities over the requester process. This
703 * means, that the filesystem daemon is able to record the exact
704 * filesystem operations performed, and can also control the behavior
705 * of the requester process in otherwise impossible ways. For example
706 * it can delay the operation for arbitrary length of time allowing
707 * DoS against the requester.
709 * For this reason only those processes can call into the filesystem,
710 * for which the owner of the mount has ptrace privilege. This
711 * excludes processes started by other users, suid or sgid processes.
713 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
715 if (fc->flags & FUSE_ALLOW_OTHER)
716 return 1;
718 if (task->euid == fc->user_id &&
719 task->suid == fc->user_id &&
720 task->uid == fc->user_id &&
721 task->egid == fc->group_id &&
722 task->sgid == fc->group_id &&
723 task->gid == fc->group_id)
724 return 1;
726 return 0;
730 * Check whether the inode attributes are still valid
732 * If the attribute validity timeout has expired, then fetch the fresh
733 * attributes with a 'getattr' request
735 * I'm not sure why cached attributes are never returned for the root
736 * inode, this is probably being too cautious.
738 static int fuse_revalidate(struct dentry *entry)
740 struct inode *inode = entry->d_inode;
741 struct fuse_inode *fi = get_fuse_inode(inode);
742 struct fuse_conn *fc = get_fuse_conn(inode);
744 if (!fuse_allow_task(fc, current))
745 return -EACCES;
746 if (get_node_id(inode) != FUSE_ROOT_ID &&
747 fi->i_time >= get_jiffies_64())
748 return 0;
750 return fuse_do_getattr(inode);
753 static int fuse_access(struct inode *inode, int mask)
755 struct fuse_conn *fc = get_fuse_conn(inode);
756 struct fuse_req *req;
757 struct fuse_access_in inarg;
758 int err;
760 if (fc->no_access)
761 return 0;
763 req = fuse_get_req(fc);
764 if (IS_ERR(req))
765 return PTR_ERR(req);
767 memset(&inarg, 0, sizeof(inarg));
768 inarg.mask = mask;
769 req->in.h.opcode = FUSE_ACCESS;
770 req->in.h.nodeid = get_node_id(inode);
771 req->in.numargs = 1;
772 req->in.args[0].size = sizeof(inarg);
773 req->in.args[0].value = &inarg;
774 request_send(fc, req);
775 err = req->out.h.error;
776 fuse_put_request(fc, req);
777 if (err == -ENOSYS) {
778 fc->no_access = 1;
779 err = 0;
781 return err;
785 * Check permission. The two basic access models of FUSE are:
787 * 1) Local access checking ('default_permissions' mount option) based
788 * on file mode. This is the plain old disk filesystem permission
789 * modell.
791 * 2) "Remote" access checking, where server is responsible for
792 * checking permission in each inode operation. An exception to this
793 * is if ->permission() was invoked from sys_access() in which case an
794 * access request is sent. Execute permission is still checked
795 * locally based on file mode.
797 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
799 struct fuse_conn *fc = get_fuse_conn(inode);
801 if (!fuse_allow_task(fc, current))
802 return -EACCES;
803 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
804 int err = generic_permission(inode, mask, NULL);
806 /* If permission is denied, try to refresh file
807 attributes. This is also needed, because the root
808 node will at first have no permissions */
809 if (err == -EACCES) {
810 err = fuse_do_getattr(inode);
811 if (!err)
812 err = generic_permission(inode, mask, NULL);
815 /* Note: the opposite of the above test does not
816 exist. So if permissions are revoked this won't be
817 noticed immediately, only after the attribute
818 timeout has expired */
820 return err;
821 } else {
822 int mode = inode->i_mode;
823 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
824 return -EACCES;
826 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
827 return fuse_access(inode, mask);
828 return 0;
832 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
833 void *dstbuf, filldir_t filldir)
835 while (nbytes >= FUSE_NAME_OFFSET) {
836 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
837 size_t reclen = FUSE_DIRENT_SIZE(dirent);
838 int over;
839 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
840 return -EIO;
841 if (reclen > nbytes)
842 break;
844 over = filldir(dstbuf, dirent->name, dirent->namelen,
845 file->f_pos, dirent->ino, dirent->type);
846 if (over)
847 break;
849 buf += reclen;
850 nbytes -= reclen;
851 file->f_pos = dirent->off;
854 return 0;
857 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
859 int err;
860 size_t nbytes;
861 struct page *page;
862 struct inode *inode = file->f_dentry->d_inode;
863 struct fuse_conn *fc = get_fuse_conn(inode);
864 struct fuse_req *req;
866 if (is_bad_inode(inode))
867 return -EIO;
869 req = fuse_get_req(fc);
870 if (IS_ERR(req))
871 return PTR_ERR(req);
873 page = alloc_page(GFP_KERNEL);
874 if (!page) {
875 fuse_put_request(fc, req);
876 return -ENOMEM;
878 req->num_pages = 1;
879 req->pages[0] = page;
880 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
881 request_send(fc, req);
882 nbytes = req->out.args[0].size;
883 err = req->out.h.error;
884 fuse_put_request(fc, req);
885 if (!err)
886 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
887 filldir);
889 __free_page(page);
890 fuse_invalidate_attr(inode); /* atime changed */
891 return err;
894 static char *read_link(struct dentry *dentry)
896 struct inode *inode = dentry->d_inode;
897 struct fuse_conn *fc = get_fuse_conn(inode);
898 struct fuse_req *req = fuse_get_req(fc);
899 char *link;
901 if (IS_ERR(req))
902 return ERR_PTR(PTR_ERR(req));
904 link = (char *) __get_free_page(GFP_KERNEL);
905 if (!link) {
906 link = ERR_PTR(-ENOMEM);
907 goto out;
909 req->in.h.opcode = FUSE_READLINK;
910 req->in.h.nodeid = get_node_id(inode);
911 req->out.argvar = 1;
912 req->out.numargs = 1;
913 req->out.args[0].size = PAGE_SIZE - 1;
914 req->out.args[0].value = link;
915 request_send(fc, req);
916 if (req->out.h.error) {
917 free_page((unsigned long) link);
918 link = ERR_PTR(req->out.h.error);
919 } else
920 link[req->out.args[0].size] = '\0';
921 out:
922 fuse_put_request(fc, req);
923 fuse_invalidate_attr(inode); /* atime changed */
924 return link;
927 static void free_link(char *link)
929 if (!IS_ERR(link))
930 free_page((unsigned long) link);
933 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
935 nd_set_link(nd, read_link(dentry));
936 return NULL;
939 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
941 free_link(nd_get_link(nd));
944 static int fuse_dir_open(struct inode *inode, struct file *file)
946 return fuse_open_common(inode, file, 1);
949 static int fuse_dir_release(struct inode *inode, struct file *file)
951 return fuse_release_common(inode, file, 1);
954 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
956 /* nfsd can call this with no file */
957 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
960 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
962 unsigned ivalid = iattr->ia_valid;
964 if (ivalid & ATTR_MODE)
965 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
966 if (ivalid & ATTR_UID)
967 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
968 if (ivalid & ATTR_GID)
969 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
970 if (ivalid & ATTR_SIZE)
971 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
972 /* You can only _set_ these together (they may change by themselves) */
973 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
974 arg->valid |= FATTR_ATIME | FATTR_MTIME;
975 arg->atime = iattr->ia_atime.tv_sec;
976 arg->mtime = iattr->ia_mtime.tv_sec;
978 if (ivalid & ATTR_FILE) {
979 struct fuse_file *ff = iattr->ia_file->private_data;
980 arg->valid |= FATTR_FH;
981 arg->fh = ff->fh;
985 static void fuse_vmtruncate(struct inode *inode, loff_t offset)
987 struct fuse_conn *fc = get_fuse_conn(inode);
988 int need_trunc;
990 spin_lock(&fc->lock);
991 need_trunc = inode->i_size > offset;
992 i_size_write(inode, offset);
993 spin_unlock(&fc->lock);
995 if (need_trunc) {
996 struct address_space *mapping = inode->i_mapping;
997 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
998 truncate_inode_pages(mapping, offset);
1003 * Set attributes, and at the same time refresh them.
1005 * Truncation is slightly complicated, because the 'truncate' request
1006 * may fail, in which case we don't want to touch the mapping.
1007 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1008 * and the actual truncation by hand.
1010 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1012 struct inode *inode = entry->d_inode;
1013 struct fuse_conn *fc = get_fuse_conn(inode);
1014 struct fuse_inode *fi = get_fuse_inode(inode);
1015 struct fuse_req *req;
1016 struct fuse_setattr_in inarg;
1017 struct fuse_attr_out outarg;
1018 int err;
1019 int is_truncate = 0;
1021 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1022 err = inode_change_ok(inode, attr);
1023 if (err)
1024 return err;
1027 if (attr->ia_valid & ATTR_SIZE) {
1028 unsigned long limit;
1029 is_truncate = 1;
1030 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1031 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1032 send_sig(SIGXFSZ, current, 0);
1033 return -EFBIG;
1037 req = fuse_get_req(fc);
1038 if (IS_ERR(req))
1039 return PTR_ERR(req);
1041 memset(&inarg, 0, sizeof(inarg));
1042 iattr_to_fattr(attr, &inarg);
1043 req->in.h.opcode = FUSE_SETATTR;
1044 req->in.h.nodeid = get_node_id(inode);
1045 req->in.numargs = 1;
1046 req->in.args[0].size = sizeof(inarg);
1047 req->in.args[0].value = &inarg;
1048 req->out.numargs = 1;
1049 req->out.args[0].size = sizeof(outarg);
1050 req->out.args[0].value = &outarg;
1051 request_send(fc, req);
1052 err = req->out.h.error;
1053 fuse_put_request(fc, req);
1054 if (!err) {
1055 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1056 make_bad_inode(inode);
1057 err = -EIO;
1058 } else {
1059 if (is_truncate)
1060 fuse_vmtruncate(inode, outarg.attr.size);
1061 fuse_change_attributes(inode, &outarg.attr);
1062 fi->i_time = time_to_jiffies(outarg.attr_valid,
1063 outarg.attr_valid_nsec);
1065 } else if (err == -EINTR)
1066 fuse_invalidate_attr(inode);
1068 return err;
1071 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1072 struct kstat *stat)
1074 struct inode *inode = entry->d_inode;
1075 int err = fuse_revalidate(entry);
1076 if (!err)
1077 generic_fillattr(inode, stat);
1079 return err;
1082 static int fuse_setxattr(struct dentry *entry, const char *name,
1083 const void *value, size_t size, int flags)
1085 struct inode *inode = entry->d_inode;
1086 struct fuse_conn *fc = get_fuse_conn(inode);
1087 struct fuse_req *req;
1088 struct fuse_setxattr_in inarg;
1089 int err;
1091 if (fc->no_setxattr)
1092 return -EOPNOTSUPP;
1094 req = fuse_get_req(fc);
1095 if (IS_ERR(req))
1096 return PTR_ERR(req);
1098 memset(&inarg, 0, sizeof(inarg));
1099 inarg.size = size;
1100 inarg.flags = flags;
1101 req->in.h.opcode = FUSE_SETXATTR;
1102 req->in.h.nodeid = get_node_id(inode);
1103 req->in.numargs = 3;
1104 req->in.args[0].size = sizeof(inarg);
1105 req->in.args[0].value = &inarg;
1106 req->in.args[1].size = strlen(name) + 1;
1107 req->in.args[1].value = name;
1108 req->in.args[2].size = size;
1109 req->in.args[2].value = value;
1110 request_send(fc, req);
1111 err = req->out.h.error;
1112 fuse_put_request(fc, req);
1113 if (err == -ENOSYS) {
1114 fc->no_setxattr = 1;
1115 err = -EOPNOTSUPP;
1117 return err;
1120 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1121 void *value, size_t size)
1123 struct inode *inode = entry->d_inode;
1124 struct fuse_conn *fc = get_fuse_conn(inode);
1125 struct fuse_req *req;
1126 struct fuse_getxattr_in inarg;
1127 struct fuse_getxattr_out outarg;
1128 ssize_t ret;
1130 if (fc->no_getxattr)
1131 return -EOPNOTSUPP;
1133 req = fuse_get_req(fc);
1134 if (IS_ERR(req))
1135 return PTR_ERR(req);
1137 memset(&inarg, 0, sizeof(inarg));
1138 inarg.size = size;
1139 req->in.h.opcode = FUSE_GETXATTR;
1140 req->in.h.nodeid = get_node_id(inode);
1141 req->in.numargs = 2;
1142 req->in.args[0].size = sizeof(inarg);
1143 req->in.args[0].value = &inarg;
1144 req->in.args[1].size = strlen(name) + 1;
1145 req->in.args[1].value = name;
1146 /* This is really two different operations rolled into one */
1147 req->out.numargs = 1;
1148 if (size) {
1149 req->out.argvar = 1;
1150 req->out.args[0].size = size;
1151 req->out.args[0].value = value;
1152 } else {
1153 req->out.args[0].size = sizeof(outarg);
1154 req->out.args[0].value = &outarg;
1156 request_send(fc, req);
1157 ret = req->out.h.error;
1158 if (!ret)
1159 ret = size ? req->out.args[0].size : outarg.size;
1160 else {
1161 if (ret == -ENOSYS) {
1162 fc->no_getxattr = 1;
1163 ret = -EOPNOTSUPP;
1166 fuse_put_request(fc, req);
1167 return ret;
1170 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1172 struct inode *inode = entry->d_inode;
1173 struct fuse_conn *fc = get_fuse_conn(inode);
1174 struct fuse_req *req;
1175 struct fuse_getxattr_in inarg;
1176 struct fuse_getxattr_out outarg;
1177 ssize_t ret;
1179 if (fc->no_listxattr)
1180 return -EOPNOTSUPP;
1182 req = fuse_get_req(fc);
1183 if (IS_ERR(req))
1184 return PTR_ERR(req);
1186 memset(&inarg, 0, sizeof(inarg));
1187 inarg.size = size;
1188 req->in.h.opcode = FUSE_LISTXATTR;
1189 req->in.h.nodeid = get_node_id(inode);
1190 req->in.numargs = 1;
1191 req->in.args[0].size = sizeof(inarg);
1192 req->in.args[0].value = &inarg;
1193 /* This is really two different operations rolled into one */
1194 req->out.numargs = 1;
1195 if (size) {
1196 req->out.argvar = 1;
1197 req->out.args[0].size = size;
1198 req->out.args[0].value = list;
1199 } else {
1200 req->out.args[0].size = sizeof(outarg);
1201 req->out.args[0].value = &outarg;
1203 request_send(fc, req);
1204 ret = req->out.h.error;
1205 if (!ret)
1206 ret = size ? req->out.args[0].size : outarg.size;
1207 else {
1208 if (ret == -ENOSYS) {
1209 fc->no_listxattr = 1;
1210 ret = -EOPNOTSUPP;
1213 fuse_put_request(fc, req);
1214 return ret;
1217 static int fuse_removexattr(struct dentry *entry, const char *name)
1219 struct inode *inode = entry->d_inode;
1220 struct fuse_conn *fc = get_fuse_conn(inode);
1221 struct fuse_req *req;
1222 int err;
1224 if (fc->no_removexattr)
1225 return -EOPNOTSUPP;
1227 req = fuse_get_req(fc);
1228 if (IS_ERR(req))
1229 return PTR_ERR(req);
1231 req->in.h.opcode = FUSE_REMOVEXATTR;
1232 req->in.h.nodeid = get_node_id(inode);
1233 req->in.numargs = 1;
1234 req->in.args[0].size = strlen(name) + 1;
1235 req->in.args[0].value = name;
1236 request_send(fc, req);
1237 err = req->out.h.error;
1238 fuse_put_request(fc, req);
1239 if (err == -ENOSYS) {
1240 fc->no_removexattr = 1;
1241 err = -EOPNOTSUPP;
1243 return err;
1246 static struct inode_operations fuse_dir_inode_operations = {
1247 .lookup = fuse_lookup,
1248 .mkdir = fuse_mkdir,
1249 .symlink = fuse_symlink,
1250 .unlink = fuse_unlink,
1251 .rmdir = fuse_rmdir,
1252 .rename = fuse_rename,
1253 .link = fuse_link,
1254 .setattr = fuse_setattr,
1255 .create = fuse_create,
1256 .mknod = fuse_mknod,
1257 .permission = fuse_permission,
1258 .getattr = fuse_getattr,
1259 .setxattr = fuse_setxattr,
1260 .getxattr = fuse_getxattr,
1261 .listxattr = fuse_listxattr,
1262 .removexattr = fuse_removexattr,
1265 static const struct file_operations fuse_dir_operations = {
1266 .llseek = generic_file_llseek,
1267 .read = generic_read_dir,
1268 .readdir = fuse_readdir,
1269 .open = fuse_dir_open,
1270 .release = fuse_dir_release,
1271 .fsync = fuse_dir_fsync,
1274 static struct inode_operations fuse_common_inode_operations = {
1275 .setattr = fuse_setattr,
1276 .permission = fuse_permission,
1277 .getattr = fuse_getattr,
1278 .setxattr = fuse_setxattr,
1279 .getxattr = fuse_getxattr,
1280 .listxattr = fuse_listxattr,
1281 .removexattr = fuse_removexattr,
1284 static struct inode_operations fuse_symlink_inode_operations = {
1285 .setattr = fuse_setattr,
1286 .follow_link = fuse_follow_link,
1287 .put_link = fuse_put_link,
1288 .readlink = generic_readlink,
1289 .getattr = fuse_getattr,
1290 .setxattr = fuse_setxattr,
1291 .getxattr = fuse_getxattr,
1292 .listxattr = fuse_listxattr,
1293 .removexattr = fuse_removexattr,
1296 void fuse_init_common(struct inode *inode)
1298 inode->i_op = &fuse_common_inode_operations;
1301 void fuse_init_dir(struct inode *inode)
1303 inode->i_op = &fuse_dir_inode_operations;
1304 inode->i_fop = &fuse_dir_operations;
1307 void fuse_init_symlink(struct inode *inode)
1309 inode->i_op = &fuse_symlink_inode_operations;