Linux Audio

Check our new training course

Loading...
  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>
 18#include <linux/seq_file.h>
 19#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;
 44struct workqueue_struct *fscache_object_wq;
 45struct workqueue_struct *fscache_op_wq;
 46
 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;
 51static unsigned fscache_op_max_active = 2;
 52
 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
 70ctl_table fscache_sysctls[] = {
 71	{
 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	},
 79	{
 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	},
 87	{}
 88};
 89
 90ctl_table fscache_sysctls_root[] = {
 91	{
 92		.procname	= "fscache",
 93		.mode		= 0555,
 94		.child		= fscache_sysctls,
 95	},
 96	{}
 97};
 98#endif
 99
100/*
101 * initialise the fs caching module
102 */
103static int __init fscache_init(void)
104{
105	unsigned int nr_cpus = num_possible_cpus();
106	unsigned int cpu;
107	int ret;
108
109	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
119	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
129	for_each_possible_cpu(cpu)
130		init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
131
132	ret = fscache_proc_init();
133	if (ret < 0)
134		goto error_proc;
135
136#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
143	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) {
149		printk(KERN_NOTICE
150		       "FS-Cache: Failed to allocate a cookie jar\n");
151		ret = -ENOMEM;
152		goto error_cookie_jar;
153	}
154
155	fscache_root = kobject_create_and_add("fscache", kernel_kobj);
156	if (!fscache_root)
157		goto error_kobj;
158
159	printk(KERN_NOTICE "FS-Cache: Loaded\n");
160	return 0;
161
162error_kobj:
163	kmem_cache_destroy(fscache_cookie_jar);
164error_cookie_jar:
165#ifdef CONFIG_SYSCTL
166	unregister_sysctl_table(fscache_sysctl_header);
167error_sysctl:
168#endif
169	fscache_proc_cleanup();
170error_proc:
171	destroy_workqueue(fscache_op_wq);
172error_op_wq:
173	destroy_workqueue(fscache_object_wq);
174error_object_wq:
175	return ret;
176}
177
178fs_initcall(fscache_init);
179
180/*
181 * clean up on module removal
182 */
183static void __exit fscache_exit(void)
184{
185	_enter("");
186
187	kobject_put(fscache_root);
188	kmem_cache_destroy(fscache_cookie_jar);
189#ifdef CONFIG_SYSCTL
190	unregister_sysctl_table(fscache_sysctl_header);
191#endif
192	fscache_proc_cleanup();
193	destroy_workqueue(fscache_op_wq);
194	destroy_workqueue(fscache_object_wq);
195	printk(KERN_NOTICE "FS-Cache: Unloaded\n");
196}
197
198module_exit(fscache_exit);
199
200/*
201 * wait_on_bit() sleep function for uninterruptible waiting
202 */
203int fscache_wait_bit(void *flags)
204{
205	schedule();
206	return 0;
207}
208
209/*
210 * wait_on_bit() sleep function for interruptible waiting
211 */
212int fscache_wait_bit_interruptible(void *flags)
213{
214	schedule();
215	return signal_pending(current);
216}
217
218/*
219 * wait_on_atomic_t() sleep function for uninterruptible waiting
220 */
221int fscache_wait_atomic_t(atomic_t *p)
222{
223	schedule();
224	return 0;
225}
  1// SPDX-License-Identifier: GPL-2.0-or-later
  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)
  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>
 14#include <linux/seq_file.h>
 15#define CREATE_TRACE_POINTS
 16#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;
 41struct workqueue_struct *fscache_object_wq;
 42struct workqueue_struct *fscache_op_wq;
 43
 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;
 48static unsigned fscache_op_max_active = 2;
 49
 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 *buffer, size_t *lenp, loff_t *ppos)
 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
 66static struct ctl_table fscache_sysctls[] = {
 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	},
 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	},
 83	{}
 84};
 85
 86static struct ctl_table fscache_sysctls_root[] = {
 87	{
 88		.procname	= "fscache",
 89		.mode		= 0555,
 90		.child		= fscache_sysctls,
 91	},
 92	{}
 93};
 94#endif
 95
 96/*
 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
112static 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 */
124unsigned 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/*
136 * initialise the fs caching module
137 */
138static int __init fscache_init(void)
139{
140	unsigned int nr_cpus = num_possible_cpus();
141	unsigned int cpu;
142	int ret;
143
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
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
164	for_each_possible_cpu(cpu)
165		init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
166
167	ret = fscache_proc_init();
168	if (ret < 0)
169		goto error_proc;
170
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
178	fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
179					       sizeof(struct fscache_cookie),
180					       0, 0, NULL);
181	if (!fscache_cookie_jar) {
182		pr_notice("Failed to allocate a cookie jar\n");
183		ret = -ENOMEM;
184		goto error_cookie_jar;
185	}
186
187	fscache_root = kobject_create_and_add("fscache", kernel_kobj);
188	if (!fscache_root)
189		goto error_kobj;
190
191	pr_notice("Loaded\n");
192	return 0;
193
194error_kobj:
195	kmem_cache_destroy(fscache_cookie_jar);
196error_cookie_jar:
197#ifdef CONFIG_SYSCTL
198	unregister_sysctl_table(fscache_sysctl_header);
199error_sysctl:
200#endif
201	fscache_proc_cleanup();
202error_proc:
203	destroy_workqueue(fscache_op_wq);
204error_op_wq:
205	destroy_workqueue(fscache_object_wq);
206error_object_wq:
207	return ret;
208}
209
210fs_initcall(fscache_init);
211
212/*
213 * clean up on module removal
214 */
215static void __exit fscache_exit(void)
216{
217	_enter("");
218
219	kobject_put(fscache_root);
220	kmem_cache_destroy(fscache_cookie_jar);
221#ifdef CONFIG_SYSCTL
222	unregister_sysctl_table(fscache_sysctl_header);
223#endif
224	fscache_proc_cleanup();
225	destroy_workqueue(fscache_op_wq);
226	destroy_workqueue(fscache_object_wq);
227	pr_notice("Unloaded\n");
228}
229
230module_exit(fscache_exit);