David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 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> |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 18 | #include <linux/seq_file.h> |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 19 | #define CREATE_TRACE_POINTS |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 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; |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 45 | struct workqueue_struct *fscache_object_wq; |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 46 | struct workqueue_struct *fscache_op_wq; |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 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; |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 52 | static unsigned fscache_op_max_active = 2; |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 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 | |
Fabian Frederick | 3e58406 | 2014-08-06 16:03:24 -0700 | [diff] [blame] | 71 | static struct ctl_table fscache_sysctls[] = { |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 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 | }, |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 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 | }, |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 88 | {} |
| 89 | }; |
| 90 | |
Fabian Frederick | 3e58406 | 2014-08-06 16:03:24 -0700 | [diff] [blame] | 91 | static struct ctl_table fscache_sysctls_root[] = { |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 92 | { |
| 93 | .procname = "fscache", |
| 94 | .mode = 0555, |
| 95 | .child = fscache_sysctls, |
| 96 | }, |
| 97 | {} |
| 98 | }; |
| 99 | #endif |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * initialise the fs caching module |
| 103 | */ |
| 104 | static int __init fscache_init(void) |
| 105 | { |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 106 | unsigned int nr_cpus = num_possible_cpus(); |
| 107 | unsigned int cpu; |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 108 | int ret; |
| 109 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 110 | fscache_object_max_active = |
| 111 | clamp_val(nr_cpus, |
| 112 | fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE); |
| 113 | |
| 114 | ret = -ENOMEM; |
| 115 | fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND, |
| 116 | fscache_object_max_active); |
| 117 | if (!fscache_object_wq) |
| 118 | goto error_object_wq; |
| 119 | |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 120 | fscache_op_max_active = |
| 121 | clamp_val(fscache_object_max_active / 2, |
| 122 | fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE); |
| 123 | |
| 124 | ret = -ENOMEM; |
| 125 | fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND, |
| 126 | fscache_op_max_active); |
| 127 | if (!fscache_op_wq) |
| 128 | goto error_op_wq; |
| 129 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 130 | for_each_possible_cpu(cpu) |
| 131 | init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu)); |
| 132 | |
David Howells | 7394daa | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 133 | ret = fscache_proc_init(); |
| 134 | if (ret < 0) |
| 135 | goto error_proc; |
| 136 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 137 | #ifdef CONFIG_SYSCTL |
| 138 | ret = -ENOMEM; |
| 139 | fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root); |
| 140 | if (!fscache_sysctl_header) |
| 141 | goto error_sysctl; |
| 142 | #endif |
| 143 | |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 144 | fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar", |
| 145 | sizeof(struct fscache_cookie), |
David Howells | 1ff2288 | 2018-10-17 15:23:45 +0100 | [diff] [blame] | 146 | 0, 0, NULL); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 147 | if (!fscache_cookie_jar) { |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 148 | pr_notice("Failed to allocate a cookie jar\n"); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 149 | ret = -ENOMEM; |
| 150 | goto error_cookie_jar; |
| 151 | } |
| 152 | |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 153 | fscache_root = kobject_create_and_add("fscache", kernel_kobj); |
| 154 | if (!fscache_root) |
| 155 | goto error_kobj; |
| 156 | |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 157 | pr_notice("Loaded\n"); |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 158 | return 0; |
| 159 | |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 160 | error_kobj: |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 161 | kmem_cache_destroy(fscache_cookie_jar); |
| 162 | error_cookie_jar: |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 163 | #ifdef CONFIG_SYSCTL |
| 164 | unregister_sysctl_table(fscache_sysctl_header); |
| 165 | error_sysctl: |
| 166 | #endif |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 167 | fscache_proc_cleanup(); |
David Howells | 7394daa | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 168 | error_proc: |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 169 | destroy_workqueue(fscache_op_wq); |
| 170 | error_op_wq: |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 171 | destroy_workqueue(fscache_object_wq); |
| 172 | error_object_wq: |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | fs_initcall(fscache_init); |
| 177 | |
| 178 | /* |
| 179 | * clean up on module removal |
| 180 | */ |
| 181 | static void __exit fscache_exit(void) |
| 182 | { |
| 183 | _enter(""); |
| 184 | |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 185 | kobject_put(fscache_root); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 186 | kmem_cache_destroy(fscache_cookie_jar); |
Tejun Heo | 40f2b6f | 2010-07-24 11:10:09 +0200 | [diff] [blame] | 187 | #ifdef CONFIG_SYSCTL |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 188 | unregister_sysctl_table(fscache_sysctl_header); |
Tejun Heo | 40f2b6f | 2010-07-24 11:10:09 +0200 | [diff] [blame] | 189 | #endif |
David Howells | 7394daa | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 190 | fscache_proc_cleanup(); |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 191 | destroy_workqueue(fscache_op_wq); |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 192 | destroy_workqueue(fscache_object_wq); |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 193 | pr_notice("Unloaded\n"); |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | module_exit(fscache_exit); |