This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / cachefiles / cf-main.c
1 /* cf-main.c: network filesystem caching backend to use cache files on a
2  *            premounted filesystem
3  *
4  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/completion.h>
17 #include <linux/slab.h>
18 #include <linux/fs.h>
19 #include <linux/file.h>
20 #include <linux/namei.h>
21 #include <linux/mount.h>
22 #include <linux/statfs.h>
23 #include <linux/proc_fs.h>
24 #include <linux/sysctl.h>
25 #include "internal.h"
26
27 unsigned long cachefiles_debug;
28
29 static int cachefiles_init(void);
30 static void cachefiles_exit(void);
31
32 fs_initcall(cachefiles_init);
33 module_exit(cachefiles_exit);
34
35 MODULE_DESCRIPTION("Mounted-filesystem based cache");
36 MODULE_AUTHOR("Red Hat, Inc.");
37 MODULE_LICENSE("GPL");
38
39 kmem_cache_t *cachefiles_object_jar;
40
41 static void cachefiles_object_init_once(void *_object, kmem_cache_t *cachep,
42                                         unsigned long flags)
43 {
44         struct cachefiles_object *object = _object;
45
46         switch (flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) {
47         case SLAB_CTOR_CONSTRUCTOR:
48                 memset(object, 0, sizeof(*object));
49                 fscache_object_init(&object->fscache);
50                 init_rwsem(&object->sem);
51                 spin_lock_init(&object->work_lock);
52                 INIT_LIST_HEAD(&object->read_list);
53                 INIT_LIST_HEAD(&object->read_pend_list);
54                 INIT_WORK(&object->read_work, &cachefiles_read_copier_work,
55                           object);
56                 INIT_LIST_HEAD(&object->write_list);
57                 INIT_WORK(&object->write_work, &cachefiles_write_work, object);
58                 break;
59
60         default:
61                 break;
62         }
63 }
64
65 /*****************************************************************************/
66 /*
67  * initialise the fs caching module
68  */
69 static int __init cachefiles_init(void)
70 {
71         struct proc_dir_entry *pde;
72         int ret;
73
74         /* create a proc entry to use as a handle for the userspace daemon */
75         ret = -ENOMEM;
76
77         pde = create_proc_entry("cachefiles", 0600, proc_root_fs);
78         if (!pde) {
79                 kerror("Unable to create /proc/fs/cachefiles");
80                 goto error_proc;
81         }
82
83         if (cachefiles_sysctl_init() < 0) {
84                 kerror("Unable to create sysctl parameters");
85                 goto error_object_jar;
86         }
87
88         pde->owner = THIS_MODULE;
89         pde->proc_fops = &cachefiles_proc_fops;
90         cachefiles_proc = pde;
91
92         /* create an object jar */
93         cachefiles_object_jar =
94                 kmem_cache_create("cachefiles_object_jar",
95                                   sizeof(struct cachefiles_object),
96                                   0,
97                                   SLAB_HWCACHE_ALIGN,
98                                   cachefiles_object_init_once,
99                                   NULL);
100         if (!cachefiles_object_jar) {
101                 printk(KERN_NOTICE
102                        "CacheFiles: Failed to allocate an object jar\n");
103                 goto error_sysctl;
104         }
105
106         printk(KERN_INFO "CacheFiles: Loaded\n");
107         return 0;
108
109 error_sysctl:
110         cachefiles_sysctl_cleanup();
111 error_object_jar:
112         remove_proc_entry("cachefiles", proc_root_fs);
113 error_proc:
114         kerror("failed to register: %d", ret);
115         return ret;
116 }
117
118 /*****************************************************************************/
119 /*
120  * clean up on module removal
121  */
122 static void __exit cachefiles_exit(void)
123 {
124         printk(KERN_INFO "CacheFiles: Unloading\n");
125
126         kmem_cache_destroy(cachefiles_object_jar);
127         remove_proc_entry("cachefiles", proc_root_fs);
128         cachefiles_sysctl_cleanup();
129 }