Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[linux-2.6-block.git] / fs / fscache / main.c
CommitLineData
06b3db1b
DH
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>
8b8edefa 18#include <linux/seq_file.h>
a18feb55 19#define CREATE_TRACE_POINTS
06b3db1b
DH
20#include "internal.h"
21
22MODULE_DESCRIPTION("FS Cache Manager");
23MODULE_AUTHOR("Red Hat, Inc.");
24MODULE_LICENSE("GPL");
25
26unsigned fscache_defer_lookup = 1;
27module_param_named(defer_lookup, fscache_defer_lookup, uint,
28 S_IWUSR | S_IRUGO);
29MODULE_PARM_DESC(fscache_defer_lookup,
30 "Defer cookie lookup to background thread");
31
32unsigned fscache_defer_create = 1;
33module_param_named(defer_create, fscache_defer_create, uint,
34 S_IWUSR | S_IRUGO);
35MODULE_PARM_DESC(fscache_defer_create,
36 "Defer cookie creation to background thread");
37
38unsigned fscache_debug;
39module_param_named(debug, fscache_debug, uint,
40 S_IWUSR | S_IRUGO);
41MODULE_PARM_DESC(fscache_debug,
42 "FS-Cache debugging mask");
43
44struct kobject *fscache_root;
8b8edefa 45struct workqueue_struct *fscache_object_wq;
8af7c124 46struct workqueue_struct *fscache_op_wq;
8b8edefa
TH
47
48DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
49
50/* these values serve as lower bounds, will be adjusted in fscache_init() */
51static unsigned fscache_object_max_active = 4;
8af7c124 52static unsigned fscache_op_max_active = 2;
8b8edefa
TH
53
54#ifdef CONFIG_SYSCTL
55static struct ctl_table_header *fscache_sysctl_header;
56
57static int fscache_max_active_sysctl(struct ctl_table *table, int write,
58 void __user *buffer,
59 size_t *lenp, loff_t *ppos)
60{
61 struct workqueue_struct **wqp = table->extra1;
62 unsigned int *datap = table->data;
63 int ret;
64
65 ret = proc_dointvec(table, write, buffer, lenp, ppos);
66 if (ret == 0)
67 workqueue_set_max_active(*wqp, *datap);
68 return ret;
69}
70
3e584064 71static struct ctl_table fscache_sysctls[] = {
8b8edefa
TH
72 {
73 .procname = "object_max_active",
74 .data = &fscache_object_max_active,
75 .maxlen = sizeof(unsigned),
76 .mode = 0644,
77 .proc_handler = fscache_max_active_sysctl,
78 .extra1 = &fscache_object_wq,
79 },
8af7c124
TH
80 {
81 .procname = "operation_max_active",
82 .data = &fscache_op_max_active,
83 .maxlen = sizeof(unsigned),
84 .mode = 0644,
85 .proc_handler = fscache_max_active_sysctl,
86 .extra1 = &fscache_op_wq,
87 },
8b8edefa
TH
88 {}
89};
90
3e584064 91static struct ctl_table fscache_sysctls_root[] = {
8b8edefa
TH
92 {
93 .procname = "fscache",
94 .mode = 0555,
95 .child = fscache_sysctls,
96 },
97 {}
98};
99#endif
06b3db1b
DH
100
101/*
102 * initialise the fs caching module
103 */
104static int __init fscache_init(void)
105{
8b8edefa
TH
106 unsigned int nr_cpus = num_possible_cpus();
107 unsigned int cpu;
06b3db1b
DH
108 int ret;
109
8b8edefa
TH
110 fscache_object_max_active =
111 clamp_val(nr_cpus,
112 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
113
114 ret = -ENOMEM;
115 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
116 fscache_object_max_active);
117 if (!fscache_object_wq)
118 goto error_object_wq;
119
8af7c124
TH
120 fscache_op_max_active =
121 clamp_val(fscache_object_max_active / 2,
122 fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
123
124 ret = -ENOMEM;
125 fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
126 fscache_op_max_active);
127 if (!fscache_op_wq)
128 goto error_op_wq;
129
8b8edefa
TH
130 for_each_possible_cpu(cpu)
131 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
132
7394daa8
DH
133 ret = fscache_proc_init();
134 if (ret < 0)
135 goto error_proc;
136
8b8edefa
TH
137#ifdef CONFIG_SYSCTL
138 ret = -ENOMEM;
139 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
140 if (!fscache_sysctl_header)
141 goto error_sysctl;
142#endif
143
955d0091
DH
144 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
145 sizeof(struct fscache_cookie),
146 0,
147 0,
148 fscache_cookie_init_once);
149 if (!fscache_cookie_jar) {
36dfd116 150 pr_notice("Failed to allocate a cookie jar\n");
955d0091
DH
151 ret = -ENOMEM;
152 goto error_cookie_jar;
153 }
154
4c515dd4
DH
155 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
156 if (!fscache_root)
157 goto error_kobj;
158
36dfd116 159 pr_notice("Loaded\n");
06b3db1b
DH
160 return 0;
161
4c515dd4 162error_kobj:
955d0091
DH
163 kmem_cache_destroy(fscache_cookie_jar);
164error_cookie_jar:
8b8edefa
TH
165#ifdef CONFIG_SYSCTL
166 unregister_sysctl_table(fscache_sysctl_header);
167error_sysctl:
168#endif
4c515dd4 169 fscache_proc_cleanup();
7394daa8 170error_proc:
8af7c124
TH
171 destroy_workqueue(fscache_op_wq);
172error_op_wq:
8b8edefa
TH
173 destroy_workqueue(fscache_object_wq);
174error_object_wq:
06b3db1b
DH
175 return ret;
176}
177
178fs_initcall(fscache_init);
179
180/*
181 * clean up on module removal
182 */
183static void __exit fscache_exit(void)
184{
185 _enter("");
186
4c515dd4 187 kobject_put(fscache_root);
955d0091 188 kmem_cache_destroy(fscache_cookie_jar);
40f2b6ff 189#ifdef CONFIG_SYSCTL
8b8edefa 190 unregister_sysctl_table(fscache_sysctl_header);
40f2b6ff 191#endif
7394daa8 192 fscache_proc_cleanup();
8af7c124 193 destroy_workqueue(fscache_op_wq);
8b8edefa 194 destroy_workqueue(fscache_object_wq);
36dfd116 195 pr_notice("Unloaded\n");
06b3db1b
DH
196}
197
198module_exit(fscache_exit);