1 /* General filesystem local caching manager
3 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
21 MODULE_DESCRIPTION("FS Cache Manager");
22 MODULE_AUTHOR("Red Hat, Inc.");
23 MODULE_LICENSE("GPL");
25 unsigned fscache_defer_lookup
= 1;
26 module_param_named(defer_lookup
, fscache_defer_lookup
, uint
,
28 MODULE_PARM_DESC(fscache_defer_lookup
,
29 "Defer cookie lookup to background thread");
31 unsigned fscache_defer_create
= 1;
32 module_param_named(defer_create
, fscache_defer_create
, uint
,
34 MODULE_PARM_DESC(fscache_defer_create
,
35 "Defer cookie creation to background thread");
37 unsigned fscache_debug
;
38 module_param_named(debug
, fscache_debug
, uint
,
40 MODULE_PARM_DESC(fscache_debug
,
41 "FS-Cache debugging mask");
43 struct kobject
*fscache_root
;
44 struct workqueue_struct
*fscache_object_wq
;
45 struct workqueue_struct
*fscache_op_wq
;
47 DEFINE_PER_CPU(wait_queue_head_t
, fscache_object_cong_wait
);
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;
54 static struct ctl_table_header
*fscache_sysctl_header
;
56 static int fscache_max_active_sysctl(struct ctl_table
*table
, int write
,
58 size_t *lenp
, loff_t
*ppos
)
60 struct workqueue_struct
**wqp
= table
->extra1
;
61 unsigned int *datap
= table
->data
;
64 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
66 workqueue_set_max_active(*wqp
, *datap
);
70 ctl_table fscache_sysctls
[] = {
72 .procname
= "object_max_active",
73 .data
= &fscache_object_max_active
,
74 .maxlen
= sizeof(unsigned),
76 .proc_handler
= fscache_max_active_sysctl
,
77 .extra1
= &fscache_object_wq
,
80 .procname
= "operation_max_active",
81 .data
= &fscache_op_max_active
,
82 .maxlen
= sizeof(unsigned),
84 .proc_handler
= fscache_max_active_sysctl
,
85 .extra1
= &fscache_op_wq
,
90 ctl_table fscache_sysctls_root
[] = {
92 .procname
= "fscache",
94 .child
= fscache_sysctls
,
101 * initialise the fs caching module
103 static int __init
fscache_init(void)
105 unsigned int nr_cpus
= num_possible_cpus();
109 fscache_object_max_active
=
111 fscache_object_max_active
, WQ_UNBOUND_MAX_ACTIVE
);
114 fscache_object_wq
= alloc_workqueue("fscache_object", WQ_UNBOUND
,
115 fscache_object_max_active
);
116 if (!fscache_object_wq
)
117 goto error_object_wq
;
119 fscache_op_max_active
=
120 clamp_val(fscache_object_max_active
/ 2,
121 fscache_op_max_active
, WQ_UNBOUND_MAX_ACTIVE
);
124 fscache_op_wq
= alloc_workqueue("fscache_operation", WQ_UNBOUND
,
125 fscache_op_max_active
);
129 for_each_possible_cpu(cpu
)
130 init_waitqueue_head(&per_cpu(fscache_object_cong_wait
, cpu
));
132 ret
= fscache_proc_init();
138 fscache_sysctl_header
= register_sysctl_table(fscache_sysctls_root
);
139 if (!fscache_sysctl_header
)
143 fscache_cookie_jar
= kmem_cache_create("fscache_cookie_jar",
144 sizeof(struct fscache_cookie
),
147 fscache_cookie_init_once
);
148 if (!fscache_cookie_jar
) {
150 "FS-Cache: Failed to allocate a cookie jar\n");
152 goto error_cookie_jar
;
155 fscache_root
= kobject_create_and_add("fscache", kernel_kobj
);
159 printk(KERN_NOTICE
"FS-Cache: Loaded\n");
163 kmem_cache_destroy(fscache_cookie_jar
);
166 unregister_sysctl_table(fscache_sysctl_header
);
169 fscache_proc_cleanup();
171 destroy_workqueue(fscache_op_wq
);
173 destroy_workqueue(fscache_object_wq
);
178 fs_initcall(fscache_init
);
181 * clean up on module removal
183 static void __exit
fscache_exit(void)
187 kobject_put(fscache_root
);
188 kmem_cache_destroy(fscache_cookie_jar
);
190 unregister_sysctl_table(fscache_sysctl_header
);
192 fscache_proc_cleanup();
193 destroy_workqueue(fscache_op_wq
);
194 destroy_workqueue(fscache_object_wq
);
195 printk(KERN_NOTICE
"FS-Cache: Unloaded\n");
198 module_exit(fscache_exit
);
201 * wait_on_bit() sleep function for uninterruptible waiting
203 int fscache_wait_bit(void *flags
)
210 * wait_on_bit() sleep function for interruptible waiting
212 int fscache_wait_bit_interruptible(void *flags
)
215 return signal_pending(current
);
219 * wait_on_atomic_t() sleep function for uninterruptible waiting
221 int fscache_wait_atomic_t(atomic_t
*p
)