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