blob: c1e6cc9091aac8bc9b7fc44119ca19418b32d8df [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,
Christoph Hellwig32927392020-04-24 08:43:38 +020054 void *buffer, size_t *lenp, loff_t *ppos)
Tejun Heo8b8edef2010-07-20 22:09:01 +020055{
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 Frederick3e584062014-08-06 16:03:24 -070066static struct ctl_table fscache_sysctls[] = {
Tejun Heo8b8edef2010-07-20 22:09:01 +020067 {
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 Heo8af7c122010-07-20 22:09:01 +020075 {
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 Heo8b8edef2010-07-20 22:09:01 +020083 {}
84};
85
Fabian Frederick3e584062014-08-06 16:03:24 -070086static struct ctl_table fscache_sysctls_root[] = {
Tejun Heo8b8edef2010-07-20 22:09:01 +020087 {
88 .procname = "fscache",
89 .mode = 0555,
90 .child = fscache_sysctls,
91 },
92 {}
93};
94#endif
David Howells06b3db12009-04-03 16:42:36 +010095
96/*
97 * initialise the fs caching module
98 */
99static int __init fscache_init(void)
100{
Tejun Heo8b8edef2010-07-20 22:09:01 +0200101 unsigned int nr_cpus = num_possible_cpus();
102 unsigned int cpu;
David Howells06b3db12009-04-03 16:42:36 +0100103 int ret;
104
Tejun Heo8b8edef2010-07-20 22:09:01 +0200105 fscache_object_max_active =
106 clamp_val(nr_cpus,
107 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
108
109 ret = -ENOMEM;
110 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
111 fscache_object_max_active);
112 if (!fscache_object_wq)
113 goto error_object_wq;
114
Tejun Heo8af7c122010-07-20 22:09:01 +0200115 fscache_op_max_active =
116 clamp_val(fscache_object_max_active / 2,
117 fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
118
119 ret = -ENOMEM;
120 fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
121 fscache_op_max_active);
122 if (!fscache_op_wq)
123 goto error_op_wq;
124
Tejun Heo8b8edef2010-07-20 22:09:01 +0200125 for_each_possible_cpu(cpu)
126 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
127
David Howells7394daa2009-04-03 16:42:37 +0100128 ret = fscache_proc_init();
129 if (ret < 0)
130 goto error_proc;
131
Tejun Heo8b8edef2010-07-20 22:09:01 +0200132#ifdef CONFIG_SYSCTL
133 ret = -ENOMEM;
134 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
135 if (!fscache_sysctl_header)
136 goto error_sysctl;
137#endif
138
David Howells955d00912009-04-03 16:42:38 +0100139 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
140 sizeof(struct fscache_cookie),
David Howells1ff22882018-10-17 15:23:45 +0100141 0, 0, NULL);
David Howells955d00912009-04-03 16:42:38 +0100142 if (!fscache_cookie_jar) {
Fabian Frederick36dfd112014-06-04 16:05:38 -0700143 pr_notice("Failed to allocate a cookie jar\n");
David Howells955d00912009-04-03 16:42:38 +0100144 ret = -ENOMEM;
145 goto error_cookie_jar;
146 }
147
David Howells4c515dd2009-04-03 16:42:37 +0100148 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
149 if (!fscache_root)
150 goto error_kobj;
151
Fabian Frederick36dfd112014-06-04 16:05:38 -0700152 pr_notice("Loaded\n");
David Howells06b3db12009-04-03 16:42:36 +0100153 return 0;
154
David Howells4c515dd2009-04-03 16:42:37 +0100155error_kobj:
David Howells955d00912009-04-03 16:42:38 +0100156 kmem_cache_destroy(fscache_cookie_jar);
157error_cookie_jar:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200158#ifdef CONFIG_SYSCTL
159 unregister_sysctl_table(fscache_sysctl_header);
160error_sysctl:
161#endif
David Howells4c515dd2009-04-03 16:42:37 +0100162 fscache_proc_cleanup();
David Howells7394daa2009-04-03 16:42:37 +0100163error_proc:
Tejun Heo8af7c122010-07-20 22:09:01 +0200164 destroy_workqueue(fscache_op_wq);
165error_op_wq:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200166 destroy_workqueue(fscache_object_wq);
167error_object_wq:
David Howells06b3db12009-04-03 16:42:36 +0100168 return ret;
169}
170
171fs_initcall(fscache_init);
172
173/*
174 * clean up on module removal
175 */
176static void __exit fscache_exit(void)
177{
178 _enter("");
179
David Howells4c515dd2009-04-03 16:42:37 +0100180 kobject_put(fscache_root);
David Howells955d00912009-04-03 16:42:38 +0100181 kmem_cache_destroy(fscache_cookie_jar);
Tejun Heo40f2b6f2010-07-24 11:10:09 +0200182#ifdef CONFIG_SYSCTL
Tejun Heo8b8edef2010-07-20 22:09:01 +0200183 unregister_sysctl_table(fscache_sysctl_header);
Tejun Heo40f2b6f2010-07-24 11:10:09 +0200184#endif
David Howells7394daa2009-04-03 16:42:37 +0100185 fscache_proc_cleanup();
Tejun Heo8af7c122010-07-20 22:09:01 +0200186 destroy_workqueue(fscache_op_wq);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200187 destroy_workqueue(fscache_object_wq);
Fabian Frederick36dfd112014-06-04 16:05:38 -0700188 pr_notice("Unloaded\n");
David Howells06b3db12009-04-03 16:42:36 +0100189}
190
191module_exit(fscache_exit);