blob: b39d487ccfb0e314096f47515d16ea165d215cc5 [file] [log] [blame]
David Howells06b3db12009-04-03 16:42:36 +01001/* 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 Heo8b8edef2010-07-20 22:09:01 +020018#include <linux/seq_file.h>
David Howells06b3db12009-04-03 16:42:36 +010019#include "internal.h"
20
21MODULE_DESCRIPTION("FS Cache Manager");
22MODULE_AUTHOR("Red Hat, Inc.");
23MODULE_LICENSE("GPL");
24
25unsigned fscache_defer_lookup = 1;
26module_param_named(defer_lookup, fscache_defer_lookup, uint,
27 S_IWUSR | S_IRUGO);
28MODULE_PARM_DESC(fscache_defer_lookup,
29 "Defer cookie lookup to background thread");
30
31unsigned fscache_defer_create = 1;
32module_param_named(defer_create, fscache_defer_create, uint,
33 S_IWUSR | S_IRUGO);
34MODULE_PARM_DESC(fscache_defer_create,
35 "Defer cookie creation to background thread");
36
37unsigned fscache_debug;
38module_param_named(debug, fscache_debug, uint,
39 S_IWUSR | S_IRUGO);
40MODULE_PARM_DESC(fscache_debug,
41 "FS-Cache debugging mask");
42
43struct kobject *fscache_root;
Tejun Heo8b8edef2010-07-20 22:09:01 +020044struct workqueue_struct *fscache_object_wq;
Tejun Heo8af7c122010-07-20 22:09:01 +020045struct workqueue_struct *fscache_op_wq;
Tejun Heo8b8edef2010-07-20 22:09:01 +020046
47DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
48
49/* these values serve as lower bounds, will be adjusted in fscache_init() */
50static unsigned fscache_object_max_active = 4;
Tejun Heo8af7c122010-07-20 22:09:01 +020051static unsigned fscache_op_max_active = 2;
Tejun Heo8b8edef2010-07-20 22:09:01 +020052
53#ifdef CONFIG_SYSCTL
54static struct ctl_table_header *fscache_sysctl_header;
55
56static int fscache_max_active_sysctl(struct ctl_table *table, int write,
57 void __user *buffer,
58 size_t *lenp, loff_t *ppos)
59{
60 struct workqueue_struct **wqp = table->extra1;
61 unsigned int *datap = table->data;
62 int ret;
63
64 ret = proc_dointvec(table, write, buffer, lenp, ppos);
65 if (ret == 0)
66 workqueue_set_max_active(*wqp, *datap);
67 return ret;
68}
69
Fabian Frederick3e584062014-08-06 16:03:24 -070070static struct ctl_table fscache_sysctls[] = {
Tejun Heo8b8edef2010-07-20 22:09:01 +020071 {
72 .procname = "object_max_active",
73 .data = &fscache_object_max_active,
74 .maxlen = sizeof(unsigned),
75 .mode = 0644,
76 .proc_handler = fscache_max_active_sysctl,
77 .extra1 = &fscache_object_wq,
78 },
Tejun Heo8af7c122010-07-20 22:09:01 +020079 {
80 .procname = "operation_max_active",
81 .data = &fscache_op_max_active,
82 .maxlen = sizeof(unsigned),
83 .mode = 0644,
84 .proc_handler = fscache_max_active_sysctl,
85 .extra1 = &fscache_op_wq,
86 },
Tejun Heo8b8edef2010-07-20 22:09:01 +020087 {}
88};
89
Fabian Frederick3e584062014-08-06 16:03:24 -070090static struct ctl_table fscache_sysctls_root[] = {
Tejun Heo8b8edef2010-07-20 22:09:01 +020091 {
92 .procname = "fscache",
93 .mode = 0555,
94 .child = fscache_sysctls,
95 },
96 {}
97};
98#endif
David Howells06b3db12009-04-03 16:42:36 +010099
100/*
101 * initialise the fs caching module
102 */
103static int __init fscache_init(void)
104{
Tejun Heo8b8edef2010-07-20 22:09:01 +0200105 unsigned int nr_cpus = num_possible_cpus();
106 unsigned int cpu;
David Howells06b3db12009-04-03 16:42:36 +0100107 int ret;
108
Tejun Heo8b8edef2010-07-20 22:09:01 +0200109 fscache_object_max_active =
110 clamp_val(nr_cpus,
111 fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
112
113 ret = -ENOMEM;
114 fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
115 fscache_object_max_active);
116 if (!fscache_object_wq)
117 goto error_object_wq;
118
Tejun Heo8af7c122010-07-20 22:09:01 +0200119 fscache_op_max_active =
120 clamp_val(fscache_object_max_active / 2,
121 fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
122
123 ret = -ENOMEM;
124 fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
125 fscache_op_max_active);
126 if (!fscache_op_wq)
127 goto error_op_wq;
128
Tejun Heo8b8edef2010-07-20 22:09:01 +0200129 for_each_possible_cpu(cpu)
130 init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
131
David Howells7394daa2009-04-03 16:42:37 +0100132 ret = fscache_proc_init();
133 if (ret < 0)
134 goto error_proc;
135
Tejun Heo8b8edef2010-07-20 22:09:01 +0200136#ifdef CONFIG_SYSCTL
137 ret = -ENOMEM;
138 fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
139 if (!fscache_sysctl_header)
140 goto error_sysctl;
141#endif
142
David Howells955d00912009-04-03 16:42:38 +0100143 fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
144 sizeof(struct fscache_cookie),
145 0,
146 0,
147 fscache_cookie_init_once);
148 if (!fscache_cookie_jar) {
Fabian Frederick36dfd112014-06-04 16:05:38 -0700149 pr_notice("Failed to allocate a cookie jar\n");
David Howells955d00912009-04-03 16:42:38 +0100150 ret = -ENOMEM;
151 goto error_cookie_jar;
152 }
153
David Howells4c515dd2009-04-03 16:42:37 +0100154 fscache_root = kobject_create_and_add("fscache", kernel_kobj);
155 if (!fscache_root)
156 goto error_kobj;
157
Fabian Frederick36dfd112014-06-04 16:05:38 -0700158 pr_notice("Loaded\n");
David Howells06b3db12009-04-03 16:42:36 +0100159 return 0;
160
David Howells4c515dd2009-04-03 16:42:37 +0100161error_kobj:
David Howells955d00912009-04-03 16:42:38 +0100162 kmem_cache_destroy(fscache_cookie_jar);
163error_cookie_jar:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200164#ifdef CONFIG_SYSCTL
165 unregister_sysctl_table(fscache_sysctl_header);
166error_sysctl:
167#endif
David Howells4c515dd2009-04-03 16:42:37 +0100168 fscache_proc_cleanup();
David Howells7394daa2009-04-03 16:42:37 +0100169error_proc:
Tejun Heo8af7c122010-07-20 22:09:01 +0200170 destroy_workqueue(fscache_op_wq);
171error_op_wq:
Tejun Heo8b8edef2010-07-20 22:09:01 +0200172 destroy_workqueue(fscache_object_wq);
173error_object_wq:
David Howells06b3db12009-04-03 16:42:36 +0100174 return ret;
175}
176
177fs_initcall(fscache_init);
178
179/*
180 * clean up on module removal
181 */
182static void __exit fscache_exit(void)
183{
184 _enter("");
185
David Howells4c515dd2009-04-03 16:42:37 +0100186 kobject_put(fscache_root);
David Howells955d00912009-04-03 16:42:38 +0100187 kmem_cache_destroy(fscache_cookie_jar);
Tejun Heo40f2b6f2010-07-24 11:10:09 +0200188#ifdef CONFIG_SYSCTL
Tejun Heo8b8edef2010-07-20 22:09:01 +0200189 unregister_sysctl_table(fscache_sysctl_header);
Tejun Heo40f2b6f2010-07-24 11:10:09 +0200190#endif
David Howells7394daa2009-04-03 16:42:37 +0100191 fscache_proc_cleanup();
Tejun Heo8af7c122010-07-20 22:09:01 +0200192 destroy_workqueue(fscache_op_wq);
Tejun Heo8b8edef2010-07-20 22:09:01 +0200193 destroy_workqueue(fscache_object_wq);
Fabian Frederick36dfd112014-06-04 16:05:38 -0700194 pr_notice("Unloaded\n");
David Howells06b3db12009-04-03 16:42:36 +0100195}
196
197module_exit(fscache_exit);
David Howells2868cbe2009-04-03 16:42:38 +0100198
199/*
David Howells13627292013-05-10 19:50:26 +0100200 * wait_on_atomic_t() sleep function for uninterruptible waiting
201 */
202int fscache_wait_atomic_t(atomic_t *p)
203{
204 schedule();
205 return 0;
206}