]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/fscache/main.c
44d13ddab2ccd95faedf3a2e2d4d7f428b527b52
[net-next-2.6.git] / fs / fscache / main.c
1 /* General filesystem local caching manager
2  *
3  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #define FSCACHE_DEBUG_LEVEL CACHE
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/seq_file.h>
19 #include "internal.h"
20
21 MODULE_DESCRIPTION("FS Cache Manager");
22 MODULE_AUTHOR("Red Hat, Inc.");
23 MODULE_LICENSE("GPL");
24
25 unsigned fscache_defer_lookup = 1;
26 module_param_named(defer_lookup, fscache_defer_lookup, uint,
27                    S_IWUSR | S_IRUGO);
28 MODULE_PARM_DESC(fscache_defer_lookup,
29                  "Defer cookie lookup to background thread");
30
31 unsigned fscache_defer_create = 1;
32 module_param_named(defer_create, fscache_defer_create, uint,
33                    S_IWUSR | S_IRUGO);
34 MODULE_PARM_DESC(fscache_defer_create,
35                  "Defer cookie creation to background thread");
36
37 unsigned fscache_debug;
38 module_param_named(debug, fscache_debug, uint,
39                    S_IWUSR | S_IRUGO);
40 MODULE_PARM_DESC(fscache_debug,
41                  "FS-Cache debugging mask");
42
43 struct kobject *fscache_root;
44 struct workqueue_struct *fscache_object_wq;
45 struct workqueue_struct *fscache_op_wq;
46
47 DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
48
49 /* these values serve as lower bounds, will be adjusted in fscache_init() */
50 static unsigned fscache_object_max_active = 4;
51 static unsigned fscache_op_max_active = 2;
52
53 #ifdef CONFIG_SYSCTL
54 static struct ctl_table_header *fscache_sysctl_header;
55
56 static int fscache_max_active_sysctl(struct ctl_table *table, int write,
57                                      void __user *buffer,
58                                      size_t *lenp, loff_t *ppos)
59 {
60         struct workqueue_struct **wqp = table->extra1;
61         unsigned int *datap = table->data;
62         int ret;
63
64         ret = proc_dointvec(table, write, buffer, lenp, ppos);
65         if (ret == 0)
66                 workqueue_set_max_active(*wqp, *datap);
67         return ret;
68 }
69
70 ctl_table fscache_sysctls[] = {
71         {
72                 .procname       = "object_max_active",
73                 .data           = &fscache_object_max_active,
74                 .maxlen         = sizeof(unsigned),
75                 .mode           = 0644,
76                 .proc_handler   = fscache_max_active_sysctl,
77                 .extra1         = &fscache_object_wq,
78         },
79         {
80                 .procname       = "operation_max_active",
81                 .data           = &fscache_op_max_active,
82                 .maxlen         = sizeof(unsigned),
83                 .mode           = 0644,
84                 .proc_handler   = fscache_max_active_sysctl,
85                 .extra1         = &fscache_op_wq,
86         },
87         {}
88 };
89
90 ctl_table fscache_sysctls_root[] = {
91         {
92                 .procname       = "fscache",
93                 .mode           = 0555,
94                 .child          = fscache_sysctls,
95         },
96         {}
97 };
98 #endif
99
100 /*
101  * initialise the fs caching module
102  */
103 static int __init fscache_init(void)
104 {
105         unsigned int nr_cpus = num_possible_cpus();
106         unsigned int cpu;
107         int ret;
108
109         ret = slow_work_register_user(THIS_MODULE);
110         if (ret < 0)
111                 goto error_slow_work;
112
113         fscache_object_max_active =
114                 clamp_val(nr_cpus,
115                           fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
116
117         ret = -ENOMEM;
118         fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
119                                             fscache_object_max_active);
120         if (!fscache_object_wq)
121                 goto error_object_wq;
122
123         fscache_op_max_active =
124                 clamp_val(fscache_object_max_active / 2,
125                           fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
126
127         ret = -ENOMEM;
128         fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
129                                         fscache_op_max_active);
130         if (!fscache_op_wq)
131                 goto error_op_wq;
132
133         for_each_possible_cpu(cpu)
134                 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
135
136         ret = fscache_proc_init();
137         if (ret < 0)
138                 goto error_proc;
139
140 #ifdef CONFIG_SYSCTL
141         ret = -ENOMEM;
142         fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
143         if (!fscache_sysctl_header)
144                 goto error_sysctl;
145 #endif
146
147         fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
148                                                sizeof(struct fscache_cookie),
149                                                0,
150                                                0,
151                                                fscache_cookie_init_once);
152         if (!fscache_cookie_jar) {
153                 printk(KERN_NOTICE
154                        "FS-Cache: Failed to allocate a cookie jar\n");
155                 ret = -ENOMEM;
156                 goto error_cookie_jar;
157         }
158
159         fscache_root = kobject_create_and_add("fscache", kernel_kobj);
160         if (!fscache_root)
161                 goto error_kobj;
162
163         printk(KERN_NOTICE "FS-Cache: Loaded\n");
164         return 0;
165
166 error_kobj:
167         kmem_cache_destroy(fscache_cookie_jar);
168 error_cookie_jar:
169 #ifdef CONFIG_SYSCTL
170         unregister_sysctl_table(fscache_sysctl_header);
171 error_sysctl:
172 #endif
173         fscache_proc_cleanup();
174 error_proc:
175         destroy_workqueue(fscache_op_wq);
176 error_op_wq:
177         destroy_workqueue(fscache_object_wq);
178 error_object_wq:
179         slow_work_unregister_user(THIS_MODULE);
180 error_slow_work:
181         return ret;
182 }
183
184 fs_initcall(fscache_init);
185
186 /*
187  * clean up on module removal
188  */
189 static void __exit fscache_exit(void)
190 {
191         _enter("");
192
193         kobject_put(fscache_root);
194         kmem_cache_destroy(fscache_cookie_jar);
195         unregister_sysctl_table(fscache_sysctl_header);
196         fscache_proc_cleanup();
197         destroy_workqueue(fscache_op_wq);
198         destroy_workqueue(fscache_object_wq);
199         slow_work_unregister_user(THIS_MODULE);
200         printk(KERN_NOTICE "FS-Cache: Unloaded\n");
201 }
202
203 module_exit(fscache_exit);
204
205 /*
206  * wait_on_bit() sleep function for uninterruptible waiting
207  */
208 int fscache_wait_bit(void *flags)
209 {
210         schedule();
211         return 0;
212 }
213 EXPORT_SYMBOL(fscache_wait_bit);
214
215 /*
216  * wait_on_bit() sleep function for interruptible waiting
217  */
218 int fscache_wait_bit_interruptible(void *flags)
219 {
220         schedule();
221         return signal_pending(current);
222 }
223 EXPORT_SYMBOL(fscache_wait_bit_interruptible);