blob: 33fad334ba70e61d9e1ff5db66f68be3672f372d [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
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>
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Filesystem in Userspace");
24MODULE_LICENSE("GPL");
25
26spinlock_t fuse_lock;
27static kmem_cache_t *fuse_inode_cachep;
28static int mount_count;
29
30static int mount_max = 1000;
31module_param(mount_max, int, 0644);
32MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)");
33
34#define FUSE_SUPER_MAGIC 0x65735546
35
36struct fuse_mount_data {
37 int fd;
38 unsigned rootmode;
39 unsigned user_id;
40};
41
42static struct inode *fuse_alloc_inode(struct super_block *sb)
43{
44 struct inode *inode;
45 struct fuse_inode *fi;
46
47 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
48 if (!inode)
49 return NULL;
50
51 fi = get_fuse_inode(inode);
52 fi->i_time = jiffies - 1;
53 fi->nodeid = 0;
54
55 return inode;
56}
57
58static void fuse_destroy_inode(struct inode *inode)
59{
60 kmem_cache_free(fuse_inode_cachep, inode);
61}
62
63static void fuse_read_inode(struct inode *inode)
64{
65 /* No op */
66}
67
68static void fuse_clear_inode(struct inode *inode)
69{
70}
71
72void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
73{
74 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
75 invalidate_inode_pages(inode->i_mapping);
76
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;
91}
92
93static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
94{
95 inode->i_mode = attr->mode & S_IFMT;
96 i_size_write(inode, attr->size);
97}
98
99static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
100{
101 unsigned long nodeid = *(unsigned long *) _nodeidp;
102 if (get_node_id(inode) == nodeid)
103 return 1;
104 else
105 return 0;
106}
107
108static int fuse_inode_set(struct inode *inode, void *_nodeidp)
109{
110 unsigned long nodeid = *(unsigned long *) _nodeidp;
111 get_fuse_inode(inode)->nodeid = nodeid;
112 return 0;
113}
114
115struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
116 int generation, struct fuse_attr *attr, int version)
117{
118 struct inode *inode;
119 struct fuse_conn *fc = get_fuse_conn_super(sb);
120 int retried = 0;
121
122 retry:
123 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
124 if (!inode)
125 return NULL;
126
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;
139 }
140
141 fuse_change_attributes(inode, attr);
142 inode->i_version = version;
143 return inode;
144}
145
146static void fuse_put_super(struct super_block *sb)
147{
148 struct fuse_conn *fc = get_fuse_conn_super(sb);
149
150 spin_lock(&fuse_lock);
151 mount_count --;
152 fc->sb = NULL;
153 fc->user_id = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700154 /* Flush all readers on this fs */
155 wake_up_all(&fc->waitq);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700156 fuse_release_conn(fc);
157 *get_fuse_conn_super_p(sb) = NULL;
158 spin_unlock(&fuse_lock);
159}
160
161enum {
162 OPT_FD,
163 OPT_ROOTMODE,
164 OPT_USER_ID,
165 OPT_DEFAULT_PERMISSIONS,
166 OPT_ALLOW_OTHER,
167 OPT_ALLOW_ROOT,
168 OPT_KERNEL_CACHE,
169 OPT_ERR
170};
171
172static match_table_t tokens = {
173 {OPT_FD, "fd=%u"},
174 {OPT_ROOTMODE, "rootmode=%o"},
175 {OPT_USER_ID, "user_id=%u"},
176 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
177 {OPT_ALLOW_OTHER, "allow_other"},
178 {OPT_ALLOW_ROOT, "allow_root"},
179 {OPT_KERNEL_CACHE, "kernel_cache"},
180 {OPT_ERR, NULL}
181};
182
183static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
184{
185 char *p;
186 memset(d, 0, sizeof(struct fuse_mount_data));
187 d->fd = -1;
188
189 while ((p = strsep(&opt, ",")) != NULL) {
190 int token;
191 int value;
192 substring_t args[MAX_OPT_ARGS];
193 if (!*p)
194 continue;
195
196 token = match_token(p, tokens, args);
197 switch (token) {
198 case OPT_FD:
199 if (match_int(&args[0], &value))
200 return 0;
201 d->fd = value;
202 break;
203
204 case OPT_ROOTMODE:
205 if (match_octal(&args[0], &value))
206 return 0;
207 d->rootmode = value;
208 break;
209
210 case OPT_USER_ID:
211 if (match_int(&args[0], &value))
212 return 0;
213 d->user_id = value;
214 break;
215
216 default:
217 return 0;
218 }
219 }
220 if (d->fd == -1)
221 return 0;
222
223 return 1;
224}
225
226static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
227{
228 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
229
230 seq_printf(m, ",user_id=%u", fc->user_id);
231 return 0;
232}
233
Miklos Szeredi334f4852005-09-09 13:10:27 -0700234static void free_conn(struct fuse_conn *fc)
235{
236 while (!list_empty(&fc->unused_list)) {
237 struct fuse_req *req;
238 req = list_entry(fc->unused_list.next, struct fuse_req, list);
239 list_del(&req->list);
240 fuse_request_free(req);
241 }
242 kfree(fc);
243}
244
245/* Must be called with the fuse lock held */
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700246void fuse_release_conn(struct fuse_conn *fc)
247{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700248 if (!fc->sb && !fc->file)
249 free_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700250}
251
252static struct fuse_conn *new_conn(void)
253{
254 struct fuse_conn *fc;
255
256 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
257 if (fc != NULL) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700258 int i;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700259 memset(fc, 0, sizeof(*fc));
260 fc->sb = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700261 fc->file = NULL;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700262 fc->user_id = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700263 init_waitqueue_head(&fc->waitq);
264 INIT_LIST_HEAD(&fc->pending);
265 INIT_LIST_HEAD(&fc->processing);
266 INIT_LIST_HEAD(&fc->unused_list);
267 sema_init(&fc->outstanding_sem, 0);
268 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
269 struct fuse_req *req = fuse_request_alloc();
270 if (!req) {
271 free_conn(fc);
272 return NULL;
273 }
274 list_add(&req->list, &fc->unused_list);
275 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700276 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
277 fc->bdi.unplug_io_fn = default_unplug_io_fn;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700278 fc->reqctr = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700279 }
280 return fc;
281}
282
283static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
284{
285 struct fuse_conn *fc;
286
Miklos Szeredi334f4852005-09-09 13:10:27 -0700287 if (file->f_op != &fuse_dev_operations)
288 return ERR_PTR(-EINVAL);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700289 fc = new_conn();
290 if (fc == NULL)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700291 return ERR_PTR(-ENOMEM);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700292 spin_lock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700293 if (file->private_data) {
294 free_conn(fc);
295 fc = ERR_PTR(-EINVAL);
296 } else {
297 file->private_data = fc;
298 fc->sb = sb;
299 fc->file = file;
300 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700301 spin_unlock(&fuse_lock);
302 return fc;
303}
304
305static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
306{
307 struct fuse_attr attr;
308 memset(&attr, 0, sizeof(attr));
309
310 attr.mode = mode;
311 attr.ino = FUSE_ROOT_ID;
312 return fuse_iget(sb, 1, 0, &attr, 0);
313}
314
315static struct super_operations fuse_super_operations = {
316 .alloc_inode = fuse_alloc_inode,
317 .destroy_inode = fuse_destroy_inode,
318 .read_inode = fuse_read_inode,
319 .clear_inode = fuse_clear_inode,
320 .put_super = fuse_put_super,
321 .show_options = fuse_show_options,
322};
323
324static int inc_mount_count(void)
325{
326 int success = 0;
327 spin_lock(&fuse_lock);
328 mount_count ++;
329 if (mount_max == -1 || mount_count <= mount_max)
330 success = 1;
331 spin_unlock(&fuse_lock);
332 return success;
333}
334
335static int fuse_fill_super(struct super_block *sb, void *data, int silent)
336{
337 struct fuse_conn *fc;
338 struct inode *root;
339 struct fuse_mount_data d;
340 struct file *file;
341 int err;
342
343 if (!parse_fuse_opt((char *) data, &d))
344 return -EINVAL;
345
346 sb->s_blocksize = PAGE_CACHE_SIZE;
347 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
348 sb->s_magic = FUSE_SUPER_MAGIC;
349 sb->s_op = &fuse_super_operations;
350 sb->s_maxbytes = MAX_LFS_FILESIZE;
351
352 file = fget(d.fd);
353 if (!file)
354 return -EINVAL;
355
356 fc = get_conn(file, sb);
357 fput(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700358 if (IS_ERR(fc))
359 return PTR_ERR(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700360
361 fc->user_id = d.user_id;
362
363 *get_fuse_conn_super_p(sb) = fc;
364
365 err = -ENFILE;
366 if (!inc_mount_count() && current->uid != 0)
367 goto err;
368
369 err = -ENOMEM;
370 root = get_root_inode(sb, d.rootmode);
371 if (root == NULL)
372 goto err;
373
374 sb->s_root = d_alloc_root(root);
375 if (!sb->s_root) {
376 iput(root);
377 goto err;
378 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700379 fuse_send_init(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700380 return 0;
381
382 err:
383 spin_lock(&fuse_lock);
384 mount_count --;
385 fc->sb = NULL;
386 fuse_release_conn(fc);
387 spin_unlock(&fuse_lock);
388 *get_fuse_conn_super_p(sb) = NULL;
389 return err;
390}
391
392static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
393 int flags, const char *dev_name,
394 void *raw_data)
395{
396 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
397}
398
399static struct file_system_type fuse_fs_type = {
400 .owner = THIS_MODULE,
401 .name = "fuse",
402 .get_sb = fuse_get_sb,
403 .kill_sb = kill_anon_super,
404};
405
406static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
407 unsigned long flags)
408{
409 struct inode * inode = foo;
410
411 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
412 SLAB_CTOR_CONSTRUCTOR)
413 inode_init_once(inode);
414}
415
416static int __init fuse_fs_init(void)
417{
418 int err;
419
420 err = register_filesystem(&fuse_fs_type);
421 if (err)
422 printk("fuse: failed to register filesystem\n");
423 else {
424 fuse_inode_cachep = kmem_cache_create("fuse_inode",
425 sizeof(struct fuse_inode),
426 0, SLAB_HWCACHE_ALIGN,
427 fuse_inode_init_once, NULL);
428 if (!fuse_inode_cachep) {
429 unregister_filesystem(&fuse_fs_type);
430 err = -ENOMEM;
431 }
432 }
433
434 return err;
435}
436
437static void fuse_fs_cleanup(void)
438{
439 unregister_filesystem(&fuse_fs_type);
440 kmem_cache_destroy(fuse_inode_cachep);
441}
442
443static int __init fuse_init(void)
444{
445 int res;
446
447 printk("fuse init (API version %i.%i)\n",
448 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
449
450 spin_lock_init(&fuse_lock);
451 res = fuse_fs_init();
452 if (res)
453 goto err;
454
Miklos Szeredi334f4852005-09-09 13:10:27 -0700455 res = fuse_dev_init();
456 if (res)
457 goto err_fs_cleanup;
458
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700459 return 0;
460
Miklos Szeredi334f4852005-09-09 13:10:27 -0700461 err_fs_cleanup:
462 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700463 err:
464 return res;
465}
466
467static void __exit fuse_exit(void)
468{
469 printk(KERN_DEBUG "fuse exit\n");
470
471 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700472 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700473}
474
475module_init(fuse_init);
476module_exit(fuse_exit);