[PATCH] FUSE - core
[linux-2.6/sactl.git] / fs / fuse / inode.c
blobea6339c2b6a1ba543937bfde770e22b85cfd85ce
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/moduleparam.h>
19 #include <linux/parser.h>
20 #include <linux/statfs.h>
22 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23 MODULE_DESCRIPTION("Filesystem in Userspace");
24 MODULE_LICENSE("GPL");
26 spinlock_t fuse_lock;
27 static kmem_cache_t *fuse_inode_cachep;
28 static int mount_count;
30 static int mount_max = 1000;
31 module_param(mount_max, int, 0644);
32 MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)");
34 #define FUSE_SUPER_MAGIC 0x65735546
36 struct fuse_mount_data {
37 int fd;
38 unsigned rootmode;
39 unsigned user_id;
42 static struct inode *fuse_alloc_inode(struct super_block *sb)
44 struct inode *inode;
45 struct fuse_inode *fi;
47 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
48 if (!inode)
49 return NULL;
51 fi = get_fuse_inode(inode);
52 fi->i_time = jiffies - 1;
53 fi->nodeid = 0;
55 return inode;
58 static void fuse_destroy_inode(struct inode *inode)
60 kmem_cache_free(fuse_inode_cachep, inode);
63 static void fuse_read_inode(struct inode *inode)
65 /* No op */
68 static void fuse_clear_inode(struct inode *inode)
72 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
74 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
75 invalidate_inode_pages(inode->i_mapping);
77 inode->i_ino = attr->ino;
78 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
79 inode->i_nlink = attr->nlink;
80 inode->i_uid = attr->uid;
81 inode->i_gid = attr->gid;
82 i_size_write(inode, attr->size);
83 inode->i_blksize = PAGE_CACHE_SIZE;
84 inode->i_blocks = attr->blocks;
85 inode->i_atime.tv_sec = attr->atime;
86 inode->i_atime.tv_nsec = attr->atimensec;
87 inode->i_mtime.tv_sec = attr->mtime;
88 inode->i_mtime.tv_nsec = attr->mtimensec;
89 inode->i_ctime.tv_sec = attr->ctime;
90 inode->i_ctime.tv_nsec = attr->ctimensec;
93 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
95 inode->i_mode = attr->mode & S_IFMT;
96 i_size_write(inode, attr->size);
99 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
101 unsigned long nodeid = *(unsigned long *) _nodeidp;
102 if (get_node_id(inode) == nodeid)
103 return 1;
104 else
105 return 0;
108 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
110 unsigned long nodeid = *(unsigned long *) _nodeidp;
111 get_fuse_inode(inode)->nodeid = nodeid;
112 return 0;
115 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
116 int generation, struct fuse_attr *attr, int version)
118 struct inode *inode;
119 struct fuse_conn *fc = get_fuse_conn_super(sb);
120 int retried = 0;
122 retry:
123 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
124 if (!inode)
125 return NULL;
127 if ((inode->i_state & I_NEW)) {
128 inode->i_generation = generation;
129 inode->i_data.backing_dev_info = &fc->bdi;
130 fuse_init_inode(inode, attr);
131 unlock_new_inode(inode);
132 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
133 BUG_ON(retried);
134 /* Inode has changed type, any I/O on the old should fail */
135 make_bad_inode(inode);
136 iput(inode);
137 retried = 1;
138 goto retry;
141 fuse_change_attributes(inode, attr);
142 inode->i_version = version;
143 return inode;
146 static void fuse_put_super(struct super_block *sb)
148 struct fuse_conn *fc = get_fuse_conn_super(sb);
150 spin_lock(&fuse_lock);
151 mount_count --;
152 fc->sb = NULL;
153 fc->user_id = 0;
154 fuse_release_conn(fc);
155 *get_fuse_conn_super_p(sb) = NULL;
156 spin_unlock(&fuse_lock);
159 enum {
160 OPT_FD,
161 OPT_ROOTMODE,
162 OPT_USER_ID,
163 OPT_DEFAULT_PERMISSIONS,
164 OPT_ALLOW_OTHER,
165 OPT_ALLOW_ROOT,
166 OPT_KERNEL_CACHE,
167 OPT_ERR
170 static match_table_t tokens = {
171 {OPT_FD, "fd=%u"},
172 {OPT_ROOTMODE, "rootmode=%o"},
173 {OPT_USER_ID, "user_id=%u"},
174 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
175 {OPT_ALLOW_OTHER, "allow_other"},
176 {OPT_ALLOW_ROOT, "allow_root"},
177 {OPT_KERNEL_CACHE, "kernel_cache"},
178 {OPT_ERR, NULL}
181 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
183 char *p;
184 memset(d, 0, sizeof(struct fuse_mount_data));
185 d->fd = -1;
187 while ((p = strsep(&opt, ",")) != NULL) {
188 int token;
189 int value;
190 substring_t args[MAX_OPT_ARGS];
191 if (!*p)
192 continue;
194 token = match_token(p, tokens, args);
195 switch (token) {
196 case OPT_FD:
197 if (match_int(&args[0], &value))
198 return 0;
199 d->fd = value;
200 break;
202 case OPT_ROOTMODE:
203 if (match_octal(&args[0], &value))
204 return 0;
205 d->rootmode = value;
206 break;
208 case OPT_USER_ID:
209 if (match_int(&args[0], &value))
210 return 0;
211 d->user_id = value;
212 break;
214 default:
215 return 0;
218 if (d->fd == -1)
219 return 0;
221 return 1;
224 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
226 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
228 seq_printf(m, ",user_id=%u", fc->user_id);
229 return 0;
232 void fuse_release_conn(struct fuse_conn *fc)
234 kfree(fc);
237 static struct fuse_conn *new_conn(void)
239 struct fuse_conn *fc;
241 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
242 if (fc != NULL) {
243 memset(fc, 0, sizeof(*fc));
244 fc->sb = NULL;
245 fc->user_id = 0;
246 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
247 fc->bdi.unplug_io_fn = default_unplug_io_fn;
249 return fc;
252 static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
254 struct fuse_conn *fc;
256 fc = new_conn();
257 if (fc == NULL)
258 return NULL;
259 spin_lock(&fuse_lock);
260 fc->sb = sb;
261 spin_unlock(&fuse_lock);
262 return fc;
265 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
267 struct fuse_attr attr;
268 memset(&attr, 0, sizeof(attr));
270 attr.mode = mode;
271 attr.ino = FUSE_ROOT_ID;
272 return fuse_iget(sb, 1, 0, &attr, 0);
275 static struct super_operations fuse_super_operations = {
276 .alloc_inode = fuse_alloc_inode,
277 .destroy_inode = fuse_destroy_inode,
278 .read_inode = fuse_read_inode,
279 .clear_inode = fuse_clear_inode,
280 .put_super = fuse_put_super,
281 .show_options = fuse_show_options,
284 static int inc_mount_count(void)
286 int success = 0;
287 spin_lock(&fuse_lock);
288 mount_count ++;
289 if (mount_max == -1 || mount_count <= mount_max)
290 success = 1;
291 spin_unlock(&fuse_lock);
292 return success;
295 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
297 struct fuse_conn *fc;
298 struct inode *root;
299 struct fuse_mount_data d;
300 struct file *file;
301 int err;
303 if (!parse_fuse_opt((char *) data, &d))
304 return -EINVAL;
306 sb->s_blocksize = PAGE_CACHE_SIZE;
307 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
308 sb->s_magic = FUSE_SUPER_MAGIC;
309 sb->s_op = &fuse_super_operations;
310 sb->s_maxbytes = MAX_LFS_FILESIZE;
312 file = fget(d.fd);
313 if (!file)
314 return -EINVAL;
316 fc = get_conn(file, sb);
317 fput(file);
318 if (fc == NULL)
319 return -EINVAL;
321 fc->user_id = d.user_id;
323 *get_fuse_conn_super_p(sb) = fc;
325 err = -ENFILE;
326 if (!inc_mount_count() && current->uid != 0)
327 goto err;
329 err = -ENOMEM;
330 root = get_root_inode(sb, d.rootmode);
331 if (root == NULL)
332 goto err;
334 sb->s_root = d_alloc_root(root);
335 if (!sb->s_root) {
336 iput(root);
337 goto err;
339 return 0;
341 err:
342 spin_lock(&fuse_lock);
343 mount_count --;
344 fc->sb = NULL;
345 fuse_release_conn(fc);
346 spin_unlock(&fuse_lock);
347 *get_fuse_conn_super_p(sb) = NULL;
348 return err;
351 static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
352 int flags, const char *dev_name,
353 void *raw_data)
355 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
358 static struct file_system_type fuse_fs_type = {
359 .owner = THIS_MODULE,
360 .name = "fuse",
361 .get_sb = fuse_get_sb,
362 .kill_sb = kill_anon_super,
365 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
366 unsigned long flags)
368 struct inode * inode = foo;
370 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
371 SLAB_CTOR_CONSTRUCTOR)
372 inode_init_once(inode);
375 static int __init fuse_fs_init(void)
377 int err;
379 err = register_filesystem(&fuse_fs_type);
380 if (err)
381 printk("fuse: failed to register filesystem\n");
382 else {
383 fuse_inode_cachep = kmem_cache_create("fuse_inode",
384 sizeof(struct fuse_inode),
385 0, SLAB_HWCACHE_ALIGN,
386 fuse_inode_init_once, NULL);
387 if (!fuse_inode_cachep) {
388 unregister_filesystem(&fuse_fs_type);
389 err = -ENOMEM;
393 return err;
396 static void fuse_fs_cleanup(void)
398 unregister_filesystem(&fuse_fs_type);
399 kmem_cache_destroy(fuse_inode_cachep);
402 static int __init fuse_init(void)
404 int res;
406 printk("fuse init (API version %i.%i)\n",
407 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
409 spin_lock_init(&fuse_lock);
410 res = fuse_fs_init();
411 if (res)
412 goto err;
414 return 0;
416 err:
417 return res;
420 static void __exit fuse_exit(void)
422 printk(KERN_DEBUG "fuse exit\n");
424 fuse_fs_cleanup();
427 module_init(fuse_init);
428 module_exit(fuse_exit);