blob: 59c2494efda34e7f066db3f6a6250e8419d2953d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells06b3db12009-04-03 16:42:36 +01002/* 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 Howells06b3db12009-04-03 16:42:36 +01006 */
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 Heo8b8edef2010-07-20 22:09:01 +020014#include <linux/seq_file.h>
David Howellsa18feb52018-04-04 13:41:27 +010015#define CREATE_TRACE_POINTS
David Howells06b3db12009-04-03 16:42:36 +010016#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;
Tejun Heo8b8edef2010-07-20 22:09:01 +020041struct workqueue_struct *fscache_object_wq;
Tejun Heo8af7c122010-07-20 22:09:01 +020042struct workqueue_struct *fscache_op_wq;
Tejun Heo8b8edef2010-07-20 22:09:01 +020043
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;
Tejun Heo8af7c122010-07-20 22:09:01 +020048static unsigned fscache_op_max_active = 2;
Tejun Heo8b8edef2010-07-20 22:09:01 +020049
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
Fabian Frederick3e584062014-08-06 16:03:24 -070067static struct ctl_table fscache_sysctls[] = {
Tejun Heo8b8edef2010-07-20 22:09:01 +020068 {
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 },
Tejun Heo8af7c122010-07-20 22:09:01 +020076 {
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 },
Tejun Heo8b8edef2010-07-20 22:09:01 +020084 {}
85};
86
Fabian Frederick3e584062014-08-06 16:03:24 -070087static struct ctl_table fscache_sysctls_root[] = {
Tejun Heo8b8edef2010-07-20 22:09:01 +020088 {
89 .procname = "fscache",
90 .mode = 0555,
91 .child = fscache_sysctls,
92 },
93 {}
94};
95#endif
David Howells06b3db12009-04-03 16:42:36 +010096
97/*
98 * initialise the fs caching module
99 */
100static int __init fscache_init(void)
101{
Tejun Heo8b8edef2010-07-20 22:09:01 +0200102 unsigned int nr_cpus = num_possible_cpus();
103 unsigned int cpu;
David Howells06b3db12009-04-03 16:42:36 +0100104 int ret;
105
Tejun Heo8b8edef2010-07-20 22:09:01 +0200106 fscache_object_max_active =
107 clamp_val(nr_cpus,
108 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
109
110 ret = -ENOMEM;
111 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
112 fscache_object_max_active);
113 if (!fscache_object_wq)
114 goto error_object_wq;
115
Tejun Heo8af7c122010-07-20 22:09:01 +0200116 fscache_op_max_active =
117 clamp_val(fscache_object_max_active / 2,
118 fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
119
120 ret = -ENOMEM;
121 fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
122 fscache_op_max_active);
123 if (!fscache_op_wq)
124 goto error_op_wq;
125
Tejun Heo8b8edef2010-07-20 22:09:01 +0200126 for_each_possible_cpu(cpu)
127 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
128
David Howells7394daa2009-04-03 16:42:37 +0100129 ret = fscache_proc_init();
130 if (ret < 0)
131 goto error_proc;
132
Tejun Heo8b8edef2010-07-20 22:09:01 +0200133#ifdef CONFIG_SYSCTL
134 ret = -ENOMEM;
135 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
136 if (!fscache_sysctl_header)
137 goto error_sysctl;
138#endif
139
David Howells955d00912009-04-03 16:42:38 +0100140 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
141 sizeof(struct fscache_cookie),
David Howells1ff22882018-10-17 15:23:45 +0100142 0, 0, NULL);
David Howells955d00912009-04-03 16:42:38 +0100143 if (!fscache_cookie_jar) {
Fabian Frederick36dfd112014-06-04 16:05:38 -0700144 pr_notice("Failed to allocate a cookie jar\n");
David Howells955d00912009-04-03 16:42:38 +0100145 ret = -ENOMEM;
146 goto error_cookie_jar;
147 }
148
David Howells4c515dd2009-04-03 16:42:37 +0100149 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
150 if (!fscache_root)
151 goto error_kobj;
152
Fabian Frederick36dfd112014-06-04 16:05:38 -0700153 pr_notice("Loaded\n");
David Howells06b3db12009-04-03 16:42:36 +0100154 return 0;
155
David Howells4c515dd2009-04-03 16:42:37 +0100156error_kobj:
David Howells955d00912009-04-03 16:42:38 +0100157 kmem_cache_destroy(fscache_cookie_jar);
158error_cookie_jar:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200159#ifdef CONFIG_SYSCTL
160 unregister_sysctl_table(fscache_sysctl_header);
161error_sysctl:
162#endif
David Howells4c515dd2009-04-03 16:42:37 +0100163 fscache_proc_cleanup();
David Howells7394daa2009-04-03 16:42:37 +0100164error_proc:
Tejun Heo8af7c122010-07-20 22:09:01 +0200165 destroy_workqueue(fscache_op_wq);
166error_op_wq:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200167 destroy_workqueue(fscache_object_wq);
168error_object_wq:
David Howells06b3db12009-04-03 16:42:36 +0100169 return ret;
170}
171
172fs_initcall(fscache_init);
173
174/*
175 * clean up on module removal
176 */
177static void __exit fscache_exit(void)
178{
179 _enter("");
180
David Howells4c515dd2009-04-03 16:42:37 +0100181 kobject_put(fscache_root);
David Howells955d00912009-04-03 16:42:38 +0100182 kmem_cache_destroy(fscache_cookie_jar);
Tejun Heo40f2b6f2010-07-24 11:10:09 +0200183#ifdef CONFIG_SYSCTL
Tejun Heo8b8edef2010-07-20 22:09:01 +0200184 unregister_sysctl_table(fscache_sysctl_header);
Tejun Heo40f2b6f2010-07-24 11:10:09 +0200185#endif
David Howells7394daa2009-04-03 16:42:37 +0100186 fscache_proc_cleanup();
Tejun Heo8af7c122010-07-20 22:09:01 +0200187 destroy_workqueue(fscache_op_wq);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200188 destroy_workqueue(fscache_object_wq);
Fabian Frederick36dfd112014-06-04 16:05:38 -0700189 pr_notice("Unloaded\n");
David Howells06b3db12009-04-03 16:42:36 +0100190}
191
192module_exit(fscache_exit);