blob: bb8d4c35c7a285faeabd2d01061cebbe090bb1da [file] [log] [blame]
David Howells06b3db12009-04-03 16:42:36 +01001/* 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>
Tejun Heo8b8edef2010-07-20 22:09:01 +020018#include <linux/seq_file.h>
David Howells06b3db12009-04-03 16:42:36 +010019#include "internal.h"
20
21MODULE_DESCRIPTION("FS Cache Manager");
22MODULE_AUTHOR("Red Hat, Inc.");
23MODULE_LICENSE("GPL");
24
25unsigned fscache_defer_lookup = 1;
26module_param_named(defer_lookup, fscache_defer_lookup, uint,
27 S_IWUSR | S_IRUGO);
28MODULE_PARM_DESC(fscache_defer_lookup,
29 "Defer cookie lookup to background thread");
30
31unsigned fscache_defer_create = 1;
32module_param_named(defer_create, fscache_defer_create, uint,
33 S_IWUSR | S_IRUGO);
34MODULE_PARM_DESC(fscache_defer_create,
35 "Defer cookie creation to background thread");
36
37unsigned fscache_debug;
38module_param_named(debug, fscache_debug, uint,
39 S_IWUSR | S_IRUGO);
40MODULE_PARM_DESC(fscache_debug,
41 "FS-Cache debugging mask");
42
43struct kobject *fscache_root;
Tejun Heo8b8edef2010-07-20 22:09:01 +020044struct workqueue_struct *fscache_object_wq;
45
46DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
47
48/* these values serve as lower bounds, will be adjusted in fscache_init() */
49static unsigned fscache_object_max_active = 4;
50
51#ifdef CONFIG_SYSCTL
52static struct ctl_table_header *fscache_sysctl_header;
53
54static int fscache_max_active_sysctl(struct ctl_table *table, int write,
55 void __user *buffer,
56 size_t *lenp, loff_t *ppos)
57{
58 struct workqueue_struct **wqp = table->extra1;
59 unsigned int *datap = table->data;
60 int ret;
61
62 ret = proc_dointvec(table, write, buffer, lenp, ppos);
63 if (ret == 0)
64 workqueue_set_max_active(*wqp, *datap);
65 return ret;
66}
67
68ctl_table fscache_sysctls[] = {
69 {
70 .procname = "object_max_active",
71 .data = &fscache_object_max_active,
72 .maxlen = sizeof(unsigned),
73 .mode = 0644,
74 .proc_handler = fscache_max_active_sysctl,
75 .extra1 = &fscache_object_wq,
76 },
77 {}
78};
79
80ctl_table fscache_sysctls_root[] = {
81 {
82 .procname = "fscache",
83 .mode = 0555,
84 .child = fscache_sysctls,
85 },
86 {}
87};
88#endif
David Howells06b3db12009-04-03 16:42:36 +010089
90/*
91 * initialise the fs caching module
92 */
93static int __init fscache_init(void)
94{
Tejun Heo8b8edef2010-07-20 22:09:01 +020095 unsigned int nr_cpus = num_possible_cpus();
96 unsigned int cpu;
David Howells06b3db12009-04-03 16:42:36 +010097 int ret;
98
David Howells3d7a6412009-11-19 18:10:23 +000099 ret = slow_work_register_user(THIS_MODULE);
David Howells06b3db12009-04-03 16:42:36 +0100100 if (ret < 0)
101 goto error_slow_work;
102
Tejun Heo8b8edef2010-07-20 22:09:01 +0200103 fscache_object_max_active =
104 clamp_val(nr_cpus,
105 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
106
107 ret = -ENOMEM;
108 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
109 fscache_object_max_active);
110 if (!fscache_object_wq)
111 goto error_object_wq;
112
113 for_each_possible_cpu(cpu)
114 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
115
David Howells7394daa2009-04-03 16:42:37 +0100116 ret = fscache_proc_init();
117 if (ret < 0)
118 goto error_proc;
119
Tejun Heo8b8edef2010-07-20 22:09:01 +0200120#ifdef CONFIG_SYSCTL
121 ret = -ENOMEM;
122 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
123 if (!fscache_sysctl_header)
124 goto error_sysctl;
125#endif
126
David Howells955d00912009-04-03 16:42:38 +0100127 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
128 sizeof(struct fscache_cookie),
129 0,
130 0,
131 fscache_cookie_init_once);
132 if (!fscache_cookie_jar) {
133 printk(KERN_NOTICE
134 "FS-Cache: Failed to allocate a cookie jar\n");
135 ret = -ENOMEM;
136 goto error_cookie_jar;
137 }
138
David Howells4c515dd2009-04-03 16:42:37 +0100139 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
140 if (!fscache_root)
141 goto error_kobj;
142
David Howells06b3db12009-04-03 16:42:36 +0100143 printk(KERN_NOTICE "FS-Cache: Loaded\n");
144 return 0;
145
David Howells4c515dd2009-04-03 16:42:37 +0100146error_kobj:
David Howells955d00912009-04-03 16:42:38 +0100147 kmem_cache_destroy(fscache_cookie_jar);
148error_cookie_jar:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200149#ifdef CONFIG_SYSCTL
150 unregister_sysctl_table(fscache_sysctl_header);
151error_sysctl:
152#endif
David Howells4c515dd2009-04-03 16:42:37 +0100153 fscache_proc_cleanup();
David Howells7394daa2009-04-03 16:42:37 +0100154error_proc:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200155 destroy_workqueue(fscache_object_wq);
156error_object_wq:
David Howells3d7a6412009-11-19 18:10:23 +0000157 slow_work_unregister_user(THIS_MODULE);
David Howells06b3db12009-04-03 16:42:36 +0100158error_slow_work:
159 return ret;
160}
161
162fs_initcall(fscache_init);
163
164/*
165 * clean up on module removal
166 */
167static void __exit fscache_exit(void)
168{
169 _enter("");
170
David Howells4c515dd2009-04-03 16:42:37 +0100171 kobject_put(fscache_root);
David Howells955d00912009-04-03 16:42:38 +0100172 kmem_cache_destroy(fscache_cookie_jar);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200173 unregister_sysctl_table(fscache_sysctl_header);
David Howells7394daa2009-04-03 16:42:37 +0100174 fscache_proc_cleanup();
Tejun Heo8b8edef2010-07-20 22:09:01 +0200175 destroy_workqueue(fscache_object_wq);
David Howells3d7a6412009-11-19 18:10:23 +0000176 slow_work_unregister_user(THIS_MODULE);
David Howells06b3db12009-04-03 16:42:36 +0100177 printk(KERN_NOTICE "FS-Cache: Unloaded\n");
178}
179
180module_exit(fscache_exit);
David Howells2868cbe2009-04-03 16:42:38 +0100181
182/*
183 * wait_on_bit() sleep function for uninterruptible waiting
184 */
185int fscache_wait_bit(void *flags)
186{
187 schedule();
188 return 0;
189}
190EXPORT_SYMBOL(fscache_wait_bit);
191
192/*
193 * wait_on_bit() sleep function for interruptible waiting
194 */
195int fscache_wait_bit_interruptible(void *flags)
196{
197 schedule();
198 return signal_pending(current);
199}
200EXPORT_SYMBOL(fscache_wait_bit_interruptible);