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