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