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