[PATCH] FUSE - readpages operation
[linux-2.6/btrfs-unstable.git] / fs / fuse / inode.c
blob0b75c73386e9733b8e0182c4c76b8c94c31672f7
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/slab.h>
13 #include <linux/file.h>
14 #include <linux/mount.h>
15 #include <linux/seq_file.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/parser.h>
19 #include <linux/statfs.h>
21 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
22 MODULE_DESCRIPTION("Filesystem in Userspace");
23 MODULE_LICENSE("GPL");
25 spinlock_t fuse_lock;
26 static kmem_cache_t *fuse_inode_cachep;
28 #define FUSE_SUPER_MAGIC 0x65735546
30 struct fuse_mount_data {
31 int fd;
32 unsigned rootmode;
33 unsigned user_id;
34 unsigned flags;
35 unsigned max_read;
38 static struct inode *fuse_alloc_inode(struct super_block *sb)
40 struct inode *inode;
41 struct fuse_inode *fi;
43 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
44 if (!inode)
45 return NULL;
47 fi = get_fuse_inode(inode);
48 fi->i_time = jiffies - 1;
49 fi->nodeid = 0;
50 fi->nlookup = 0;
51 fi->forget_req = fuse_request_alloc();
52 if (!fi->forget_req) {
53 kmem_cache_free(fuse_inode_cachep, inode);
54 return NULL;
57 return inode;
60 static void fuse_destroy_inode(struct inode *inode)
62 struct fuse_inode *fi = get_fuse_inode(inode);
63 if (fi->forget_req)
64 fuse_request_free(fi->forget_req);
65 kmem_cache_free(fuse_inode_cachep, inode);
68 static void fuse_read_inode(struct inode *inode)
70 /* No op */
73 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
74 unsigned long nodeid, u64 nlookup)
76 struct fuse_forget_in *inarg = &req->misc.forget_in;
77 inarg->nlookup = nlookup;
78 req->in.h.opcode = FUSE_FORGET;
79 req->in.h.nodeid = nodeid;
80 req->in.numargs = 1;
81 req->in.args[0].size = sizeof(struct fuse_forget_in);
82 req->in.args[0].value = inarg;
83 request_send_noreply(fc, req);
86 static void fuse_clear_inode(struct inode *inode)
88 if (inode->i_sb->s_flags & MS_ACTIVE) {
89 struct fuse_conn *fc = get_fuse_conn(inode);
90 struct fuse_inode *fi = get_fuse_inode(inode);
91 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
92 fi->forget_req = NULL;
96 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
98 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
99 invalidate_inode_pages(inode->i_mapping);
101 inode->i_ino = attr->ino;
102 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
103 inode->i_nlink = attr->nlink;
104 inode->i_uid = attr->uid;
105 inode->i_gid = attr->gid;
106 i_size_write(inode, attr->size);
107 inode->i_blksize = PAGE_CACHE_SIZE;
108 inode->i_blocks = attr->blocks;
109 inode->i_atime.tv_sec = attr->atime;
110 inode->i_atime.tv_nsec = attr->atimensec;
111 inode->i_mtime.tv_sec = attr->mtime;
112 inode->i_mtime.tv_nsec = attr->mtimensec;
113 inode->i_ctime.tv_sec = attr->ctime;
114 inode->i_ctime.tv_nsec = attr->ctimensec;
117 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
119 inode->i_mode = attr->mode & S_IFMT;
120 i_size_write(inode, attr->size);
121 if (S_ISREG(inode->i_mode)) {
122 fuse_init_common(inode);
123 fuse_init_file_inode(inode);
124 } else if (S_ISDIR(inode->i_mode))
125 fuse_init_dir(inode);
126 else if (S_ISLNK(inode->i_mode))
127 fuse_init_symlink(inode);
128 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
129 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
130 fuse_init_common(inode);
131 init_special_inode(inode, inode->i_mode,
132 new_decode_dev(attr->rdev));
133 } else {
134 /* Don't let user create weird files */
135 inode->i_mode = S_IFREG;
136 fuse_init_common(inode);
137 fuse_init_file_inode(inode);
141 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
143 unsigned long nodeid = *(unsigned long *) _nodeidp;
144 if (get_node_id(inode) == nodeid)
145 return 1;
146 else
147 return 0;
150 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
152 unsigned long nodeid = *(unsigned long *) _nodeidp;
153 get_fuse_inode(inode)->nodeid = nodeid;
154 return 0;
157 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
158 int generation, struct fuse_attr *attr)
160 struct inode *inode;
161 struct fuse_inode *fi;
162 struct fuse_conn *fc = get_fuse_conn_super(sb);
163 int retried = 0;
165 retry:
166 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
167 if (!inode)
168 return NULL;
170 if ((inode->i_state & I_NEW)) {
171 inode->i_generation = generation;
172 inode->i_data.backing_dev_info = &fc->bdi;
173 fuse_init_inode(inode, attr);
174 unlock_new_inode(inode);
175 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
176 BUG_ON(retried);
177 /* Inode has changed type, any I/O on the old should fail */
178 make_bad_inode(inode);
179 iput(inode);
180 retried = 1;
181 goto retry;
184 fi = get_fuse_inode(inode);
185 fi->nlookup ++;
186 fuse_change_attributes(inode, attr);
187 return inode;
190 static void fuse_put_super(struct super_block *sb)
192 struct fuse_conn *fc = get_fuse_conn_super(sb);
194 down_write(&fc->sbput_sem);
195 while (!list_empty(&fc->background))
196 fuse_release_background(list_entry(fc->background.next,
197 struct fuse_req, bg_entry));
199 spin_lock(&fuse_lock);
200 fc->mounted = 0;
201 fc->user_id = 0;
202 fc->flags = 0;
203 /* Flush all readers on this fs */
204 wake_up_all(&fc->waitq);
205 up_write(&fc->sbput_sem);
206 fuse_release_conn(fc);
207 spin_unlock(&fuse_lock);
210 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
212 stbuf->f_type = FUSE_SUPER_MAGIC;
213 stbuf->f_bsize = attr->bsize;
214 stbuf->f_blocks = attr->blocks;
215 stbuf->f_bfree = attr->bfree;
216 stbuf->f_bavail = attr->bavail;
217 stbuf->f_files = attr->files;
218 stbuf->f_ffree = attr->ffree;
219 stbuf->f_namelen = attr->namelen;
220 /* fsid is left zero */
223 static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
225 struct fuse_conn *fc = get_fuse_conn_super(sb);
226 struct fuse_req *req;
227 struct fuse_statfs_out outarg;
228 int err;
230 req = fuse_get_request(fc);
231 if (!req)
232 return -ERESTARTSYS;
234 req->in.numargs = 0;
235 req->in.h.opcode = FUSE_STATFS;
236 req->out.numargs = 1;
237 req->out.args[0].size = sizeof(outarg);
238 req->out.args[0].value = &outarg;
239 request_send(fc, req);
240 err = req->out.h.error;
241 if (!err)
242 convert_fuse_statfs(buf, &outarg.st);
243 fuse_put_request(fc, req);
244 return err;
247 enum {
248 OPT_FD,
249 OPT_ROOTMODE,
250 OPT_USER_ID,
251 OPT_DEFAULT_PERMISSIONS,
252 OPT_ALLOW_OTHER,
253 OPT_KERNEL_CACHE,
254 OPT_MAX_READ,
255 OPT_ERR
258 static match_table_t tokens = {
259 {OPT_FD, "fd=%u"},
260 {OPT_ROOTMODE, "rootmode=%o"},
261 {OPT_USER_ID, "user_id=%u"},
262 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
263 {OPT_ALLOW_OTHER, "allow_other"},
264 {OPT_KERNEL_CACHE, "kernel_cache"},
265 {OPT_MAX_READ, "max_read=%u"},
266 {OPT_ERR, NULL}
269 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
271 char *p;
272 memset(d, 0, sizeof(struct fuse_mount_data));
273 d->fd = -1;
274 d->max_read = ~0;
276 while ((p = strsep(&opt, ",")) != NULL) {
277 int token;
278 int value;
279 substring_t args[MAX_OPT_ARGS];
280 if (!*p)
281 continue;
283 token = match_token(p, tokens, args);
284 switch (token) {
285 case OPT_FD:
286 if (match_int(&args[0], &value))
287 return 0;
288 d->fd = value;
289 break;
291 case OPT_ROOTMODE:
292 if (match_octal(&args[0], &value))
293 return 0;
294 d->rootmode = value;
295 break;
297 case OPT_USER_ID:
298 if (match_int(&args[0], &value))
299 return 0;
300 d->user_id = value;
301 break;
303 case OPT_DEFAULT_PERMISSIONS:
304 d->flags |= FUSE_DEFAULT_PERMISSIONS;
305 break;
307 case OPT_ALLOW_OTHER:
308 d->flags |= FUSE_ALLOW_OTHER;
309 break;
311 case OPT_KERNEL_CACHE:
312 d->flags |= FUSE_KERNEL_CACHE;
313 break;
315 case OPT_MAX_READ:
316 if (match_int(&args[0], &value))
317 return 0;
318 d->max_read = value;
319 break;
321 default:
322 return 0;
325 if (d->fd == -1)
326 return 0;
328 return 1;
331 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
333 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
335 seq_printf(m, ",user_id=%u", fc->user_id);
336 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
337 seq_puts(m, ",default_permissions");
338 if (fc->flags & FUSE_ALLOW_OTHER)
339 seq_puts(m, ",allow_other");
340 if (fc->flags & FUSE_KERNEL_CACHE)
341 seq_puts(m, ",kernel_cache");
342 if (fc->max_read != ~0)
343 seq_printf(m, ",max_read=%u", fc->max_read);
344 return 0;
347 static void free_conn(struct fuse_conn *fc)
349 while (!list_empty(&fc->unused_list)) {
350 struct fuse_req *req;
351 req = list_entry(fc->unused_list.next, struct fuse_req, list);
352 list_del(&req->list);
353 fuse_request_free(req);
355 kfree(fc);
358 /* Must be called with the fuse lock held */
359 void fuse_release_conn(struct fuse_conn *fc)
361 fc->count--;
362 if (!fc->count)
363 free_conn(fc);
366 static struct fuse_conn *new_conn(void)
368 struct fuse_conn *fc;
370 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
371 if (fc != NULL) {
372 int i;
373 memset(fc, 0, sizeof(*fc));
374 init_waitqueue_head(&fc->waitq);
375 INIT_LIST_HEAD(&fc->pending);
376 INIT_LIST_HEAD(&fc->processing);
377 INIT_LIST_HEAD(&fc->unused_list);
378 INIT_LIST_HEAD(&fc->background);
379 sema_init(&fc->outstanding_sem, 0);
380 init_rwsem(&fc->sbput_sem);
381 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
382 struct fuse_req *req = fuse_request_alloc();
383 if (!req) {
384 free_conn(fc);
385 return NULL;
387 list_add(&req->list, &fc->unused_list);
389 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
390 fc->bdi.unplug_io_fn = default_unplug_io_fn;
391 fc->reqctr = 0;
393 return fc;
396 static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
398 struct fuse_conn *fc;
400 if (file->f_op != &fuse_dev_operations)
401 return ERR_PTR(-EINVAL);
402 fc = new_conn();
403 if (fc == NULL)
404 return ERR_PTR(-ENOMEM);
405 spin_lock(&fuse_lock);
406 if (file->private_data) {
407 free_conn(fc);
408 fc = ERR_PTR(-EINVAL);
409 } else {
410 file->private_data = fc;
411 *get_fuse_conn_super_p(sb) = fc;
412 fc->mounted = 1;
413 fc->connected = 1;
414 fc->count = 2;
416 spin_unlock(&fuse_lock);
417 return fc;
420 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
422 struct fuse_attr attr;
423 memset(&attr, 0, sizeof(attr));
425 attr.mode = mode;
426 attr.ino = FUSE_ROOT_ID;
427 return fuse_iget(sb, 1, 0, &attr);
430 static struct super_operations fuse_super_operations = {
431 .alloc_inode = fuse_alloc_inode,
432 .destroy_inode = fuse_destroy_inode,
433 .read_inode = fuse_read_inode,
434 .clear_inode = fuse_clear_inode,
435 .put_super = fuse_put_super,
436 .statfs = fuse_statfs,
437 .show_options = fuse_show_options,
440 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
442 struct fuse_conn *fc;
443 struct inode *root;
444 struct fuse_mount_data d;
445 struct file *file;
446 int err;
448 if (!parse_fuse_opt((char *) data, &d))
449 return -EINVAL;
451 sb->s_blocksize = PAGE_CACHE_SIZE;
452 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
453 sb->s_magic = FUSE_SUPER_MAGIC;
454 sb->s_op = &fuse_super_operations;
455 sb->s_maxbytes = MAX_LFS_FILESIZE;
457 file = fget(d.fd);
458 if (!file)
459 return -EINVAL;
461 fc = get_conn(file, sb);
462 fput(file);
463 if (IS_ERR(fc))
464 return PTR_ERR(fc);
466 fc->flags = d.flags;
467 fc->user_id = d.user_id;
468 fc->max_read = d.max_read;
469 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
470 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
472 err = -ENOMEM;
473 root = get_root_inode(sb, d.rootmode);
474 if (root == NULL)
475 goto err;
477 sb->s_root = d_alloc_root(root);
478 if (!sb->s_root) {
479 iput(root);
480 goto err;
482 fuse_send_init(fc);
483 return 0;
485 err:
486 spin_lock(&fuse_lock);
487 fuse_release_conn(fc);
488 spin_unlock(&fuse_lock);
489 return err;
492 static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
493 int flags, const char *dev_name,
494 void *raw_data)
496 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
499 static struct file_system_type fuse_fs_type = {
500 .owner = THIS_MODULE,
501 .name = "fuse",
502 .get_sb = fuse_get_sb,
503 .kill_sb = kill_anon_super,
506 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
507 unsigned long flags)
509 struct inode * inode = foo;
511 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
512 SLAB_CTOR_CONSTRUCTOR)
513 inode_init_once(inode);
516 static int __init fuse_fs_init(void)
518 int err;
520 err = register_filesystem(&fuse_fs_type);
521 if (err)
522 printk("fuse: failed to register filesystem\n");
523 else {
524 fuse_inode_cachep = kmem_cache_create("fuse_inode",
525 sizeof(struct fuse_inode),
526 0, SLAB_HWCACHE_ALIGN,
527 fuse_inode_init_once, NULL);
528 if (!fuse_inode_cachep) {
529 unregister_filesystem(&fuse_fs_type);
530 err = -ENOMEM;
534 return err;
537 static void fuse_fs_cleanup(void)
539 unregister_filesystem(&fuse_fs_type);
540 kmem_cache_destroy(fuse_inode_cachep);
543 static int __init fuse_init(void)
545 int res;
547 printk("fuse init (API version %i.%i)\n",
548 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
550 spin_lock_init(&fuse_lock);
551 res = fuse_fs_init();
552 if (res)
553 goto err;
555 res = fuse_dev_init();
556 if (res)
557 goto err_fs_cleanup;
559 return 0;
561 err_fs_cleanup:
562 fuse_fs_cleanup();
563 err:
564 return res;
567 static void __exit fuse_exit(void)
569 printk(KERN_DEBUG "fuse exit\n");
571 fuse_fs_cleanup();
572 fuse_dev_cleanup();
575 module_init(fuse_init);
576 module_exit(fuse_exit);