blob: 3aa3756c717614605e01ca65d834de29d3183fba [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/* General filesystem local caching manager
3 *
4 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
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
16#include "internal.h"
17
18MODULE_DESCRIPTION("FS Cache Manager");
19MODULE_AUTHOR("Red Hat, Inc.");
20MODULE_LICENSE("GPL");
21
22unsigned fscache_defer_lookup = 1;
23module_param_named(defer_lookup, fscache_defer_lookup, uint,
24 S_IWUSR | S_IRUGO);
25MODULE_PARM_DESC(fscache_defer_lookup,
26 "Defer cookie lookup to background thread");
27
28unsigned fscache_defer_create = 1;
29module_param_named(defer_create, fscache_defer_create, uint,
30 S_IWUSR | S_IRUGO);
31MODULE_PARM_DESC(fscache_defer_create,
32 "Defer cookie creation to background thread");
33
34unsigned fscache_debug;
35module_param_named(debug, fscache_debug, uint,
36 S_IWUSR | S_IRUGO);
37MODULE_PARM_DESC(fscache_debug,
38 "FS-Cache debugging mask");
39
40struct kobject *fscache_root;
41struct workqueue_struct *fscache_object_wq;
42struct workqueue_struct *fscache_op_wq;
43
44DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
45
46/* these values serve as lower bounds, will be adjusted in fscache_init() */
47static unsigned fscache_object_max_active = 4;
48static unsigned fscache_op_max_active = 2;
49
50#ifdef CONFIG_SYSCTL
51static struct ctl_table_header *fscache_sysctl_header;
52
53static int fscache_max_active_sysctl(struct ctl_table *table, int write,
54 void __user *buffer,
55 size_t *lenp, loff_t *ppos)
56{
57 struct workqueue_struct **wqp = table->extra1;
58 unsigned int *datap = table->data;
59 int ret;
60
61 ret = proc_dointvec(table, write, buffer, lenp, ppos);
62 if (ret == 0)
63 workqueue_set_max_active(*wqp, *datap);
64 return ret;
65}
66
67static struct ctl_table fscache_sysctls[] = {
68 {
69 .procname = "object_max_active",
70 .data = &fscache_object_max_active,
71 .maxlen = sizeof(unsigned),
72 .mode = 0644,
73 .proc_handler = fscache_max_active_sysctl,
74 .extra1 = &fscache_object_wq,
75 },
76 {
77 .procname = "operation_max_active",
78 .data = &fscache_op_max_active,
79 .maxlen = sizeof(unsigned),
80 .mode = 0644,
81 .proc_handler = fscache_max_active_sysctl,
82 .extra1 = &fscache_op_wq,
83 },
84 {}
85};
86
87static struct ctl_table fscache_sysctls_root[] = {
88 {
89 .procname = "fscache",
90 .mode = 0555,
91 .child = fscache_sysctls,
92 },
93 {}
94};
95#endif
96
97/*
98 * Mixing scores (in bits) for (7,20):
99 * Input delta: 1-bit 2-bit
100 * 1 round: 330.3 9201.6
101 * 2 rounds: 1246.4 25475.4
102 * 3 rounds: 1907.1 31295.1
103 * 4 rounds: 2042.3 31718.6
104 * Perfect: 2048 31744
105 * (32*64) (32*31/2 * 64)
106 */
107#define HASH_MIX(x, y, a) \
108 ( x ^= (a), \
109 y ^= x, x = rol32(x, 7),\
110 x += y, y = rol32(y,20),\
111 y *= 9 )
112
113static inline unsigned int fold_hash(unsigned long x, unsigned long y)
114{
115 /* Use arch-optimized multiply if one exists */
116 return __hash_32(y ^ __hash_32(x));
117}
118
119/*
120 * Generate a hash. This is derived from full_name_hash(), but we want to be
121 * sure it is arch independent and that it doesn't change as bits of the
122 * computed hash value might appear on disk. The caller also guarantees that
123 * the hashed data will be a series of aligned 32-bit words.
124 */
125unsigned int fscache_hash(unsigned int salt, unsigned int *data, unsigned int n)
126{
127 unsigned int a, x = 0, y = salt;
128
129 for (; n; n--) {
130 a = *data++;
131 HASH_MIX(x, y, a);
132 }
133 return fold_hash(x, y);
134}
135
136/*
137 * initialise the fs caching module
138 */
139static int __init fscache_init(void)
140{
141 unsigned int nr_cpus = num_possible_cpus();
142 unsigned int cpu;
143 int ret;
144
145 fscache_object_max_active =
146 clamp_val(nr_cpus,
147 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
148
149 ret = -ENOMEM;
150 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
151 fscache_object_max_active);
152 if (!fscache_object_wq)
153 goto error_object_wq;
154
155 fscache_op_max_active =
156 clamp_val(fscache_object_max_active / 2,
157 fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
158
159 ret = -ENOMEM;
160 fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
161 fscache_op_max_active);
162 if (!fscache_op_wq)
163 goto error_op_wq;
164
165 for_each_possible_cpu(cpu)
166 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
167
168 ret = fscache_proc_init();
169 if (ret < 0)
170 goto error_proc;
171
172#ifdef CONFIG_SYSCTL
173 ret = -ENOMEM;
174 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
175 if (!fscache_sysctl_header)
176 goto error_sysctl;
177#endif
178
179 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
180 sizeof(struct fscache_cookie),
181 0, 0, NULL);
182 if (!fscache_cookie_jar) {
183 pr_notice("Failed to allocate a cookie jar\n");
184 ret = -ENOMEM;
185 goto error_cookie_jar;
186 }
187
188 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
189 if (!fscache_root)
190 goto error_kobj;
191
192 pr_notice("Loaded\n");
193 return 0;
194
195error_kobj:
196 kmem_cache_destroy(fscache_cookie_jar);
197error_cookie_jar:
198#ifdef CONFIG_SYSCTL
199 unregister_sysctl_table(fscache_sysctl_header);
200error_sysctl:
201#endif
202 fscache_proc_cleanup();
203error_proc:
204 destroy_workqueue(fscache_op_wq);
205error_op_wq:
206 destroy_workqueue(fscache_object_wq);
207error_object_wq:
208 return ret;
209}
210
211fs_initcall(fscache_init);
212
213/*
214 * clean up on module removal
215 */
216static void __exit fscache_exit(void)
217{
218 _enter("");
219
220 kobject_put(fscache_root);
221 kmem_cache_destroy(fscache_cookie_jar);
222#ifdef CONFIG_SYSCTL
223 unregister_sysctl_table(fscache_sysctl_header);
224#endif
225 fscache_proc_cleanup();
226 destroy_workqueue(fscache_op_wq);
227 destroy_workqueue(fscache_object_wq);
228 pr_notice("Unloaded\n");
229}
230
231module_exit(fscache_exit);