1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* General filesystem local caching manager
4 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
8 #define FSCACHE_DEBUG_LEVEL CACHE
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/sched.h>
12 #include <linux/completion.h>
13 #include <linux/slab.h>
14 #include <linux/seq_file.h>
15 #define CREATE_TRACE_POINTS
18 MODULE_DESCRIPTION("FS Cache Manager");
19 MODULE_AUTHOR("Red Hat, Inc.");
20 MODULE_LICENSE("GPL");
22 unsigned fscache_defer_lookup = 1;
23 module_param_named(defer_lookup, fscache_defer_lookup, uint,
25 MODULE_PARM_DESC(fscache_defer_lookup,
26 "Defer cookie lookup to background thread");
28 unsigned fscache_defer_create = 1;
29 module_param_named(defer_create, fscache_defer_create, uint,
31 MODULE_PARM_DESC(fscache_defer_create,
32 "Defer cookie creation to background thread");
34 unsigned fscache_debug;
35 module_param_named(debug, fscache_debug, uint,
37 MODULE_PARM_DESC(fscache_debug,
38 "FS-Cache debugging mask");
40 struct kobject *fscache_root;
41 struct workqueue_struct *fscache_object_wq;
42 struct workqueue_struct *fscache_op_wq;
44 DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
46 /* these values serve as lower bounds, will be adjusted in fscache_init() */
47 static unsigned fscache_object_max_active = 4;
48 static unsigned fscache_op_max_active = 2;
51 static struct ctl_table_header *fscache_sysctl_header;
53 static int fscache_max_active_sysctl(struct ctl_table *table, int write,
54 void *buffer, size_t *lenp, loff_t *ppos)
56 struct workqueue_struct **wqp = table->extra1;
57 unsigned int *datap = table->data;
60 ret = proc_dointvec(table, write, buffer, lenp, ppos);
62 workqueue_set_max_active(*wqp, *datap);
66 static struct ctl_table fscache_sysctls[] = {
68 .procname = "object_max_active",
69 .data = &fscache_object_max_active,
70 .maxlen = sizeof(unsigned),
72 .proc_handler = fscache_max_active_sysctl,
73 .extra1 = &fscache_object_wq,
76 .procname = "operation_max_active",
77 .data = &fscache_op_max_active,
78 .maxlen = sizeof(unsigned),
80 .proc_handler = fscache_max_active_sysctl,
81 .extra1 = &fscache_op_wq,
86 static struct ctl_table fscache_sysctls_root[] = {
88 .procname = "fscache",
90 .child = fscache_sysctls,
97 * initialise the fs caching module
99 static int __init fscache_init(void)
101 unsigned int nr_cpus = num_possible_cpus();
105 fscache_object_max_active =
107 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
110 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
111 fscache_object_max_active);
112 if (!fscache_object_wq)
113 goto error_object_wq;
115 fscache_op_max_active =
116 clamp_val(fscache_object_max_active / 2,
117 fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
120 fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
121 fscache_op_max_active);
125 for_each_possible_cpu(cpu)
126 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
128 ret = fscache_proc_init();
134 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
135 if (!fscache_sysctl_header)
139 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
140 sizeof(struct fscache_cookie),
142 if (!fscache_cookie_jar) {
143 pr_notice("Failed to allocate a cookie jar\n");
145 goto error_cookie_jar;
148 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
152 pr_notice("Loaded\n");
156 kmem_cache_destroy(fscache_cookie_jar);
159 unregister_sysctl_table(fscache_sysctl_header);
162 fscache_proc_cleanup();
164 destroy_workqueue(fscache_op_wq);
166 destroy_workqueue(fscache_object_wq);
171 fs_initcall(fscache_init);
174 * clean up on module removal
176 static void __exit fscache_exit(void)
180 kobject_put(fscache_root);
181 kmem_cache_destroy(fscache_cookie_jar);
183 unregister_sysctl_table(fscache_sysctl_header);
185 fscache_proc_cleanup();
186 destroy_workqueue(fscache_op_wq);
187 destroy_workqueue(fscache_object_wq);
188 pr_notice("Unloaded\n");
191 module_exit(fscache_exit);