summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/cachefs/cachefs_module.c
blob: 12eeb2b83728358a241e7ab4a7b0e29860c45109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
 */

#include <sys/errno.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <rpc/types.h>
#include <sys/mode.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/fs/cachefs_fs.h>

/*
 * This is the loadable module wrapper.
 */
#include <sys/systm.h>
#include <sys/modctl.h>
#include <sys/syscall.h>

extern time_t time;

static int cachefs_init(int, char *);
static void cachefs_fini();

static int cachefs_unloadable = 0; /* tunable */
static boolean_t cachefs_up = B_FALSE;

uint_t cachefs_max_apop_inqueue = CACHEFS_MAX_APOP_INQUEUE;

/*
 * this is a list of possible hash table sizes, for the `double
 * hashing' algorithm described in rosen's `elementary number theory
 * and its applications'.  minimally, this needs to be a list of
 * increasing prime integers, terminated by a 0.  ideally, they should
 * be the larger of twin primes; i.e. P and P-2 are both prime.
 */

int cachefs_hash_sizes[] = {5, 2029, 4093, 8089, 16363, 32719, 0};

/*
 * Module linkage information for the kernel.
 */

static vfsdef_t vfs_z = {
	VFSDEF_VERSION,
	CACHEFS_BASETYPE,
	cachefs_init,
	VSW_CANREMOUNT,
	NULL
};

static struct modlfs modlfs = {
	&mod_fsops,
	"cache filesystem",
	&vfs_z
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlfs, NULL
};

int
_init(void)
{
	int status;

	status = mod_install(&modlinkage);
	if (status != 0) {
		/*
		 * Could not load module, clean up the work performed
		 * by cachefs_init() which was indirectly called by
		 * mod_installfs() which in turn was called by mod_install().
		 */
		cachefs_fini();
	}

	return (status);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

int
_fini(void)
{
	int status;

	if (!cachefs_unloadable)
		return (EBUSY);

	if ((status = mod_remove(&modlinkage)) == 0) {
		/*
		 * Module has been unloaded, now clean up
		 */
		cachefs_fini();
	}

	return (status);
}

extern kmutex_t cachefs_cachelock;		/* Cache list mutex */
extern kmutex_t cachefs_newnum_lock;
extern kmutex_t cachefs_kstat_key_lock;
extern kmutex_t cachefs_rename_lock;
extern kmutex_t cachefs_minor_lock;	/* Lock for minor device map */
extern kmutex_t cachefs_kmem_lock;
extern kmutex_t cachefs_async_lock;	/* global async work count */
extern major_t cachefs_major;

/*
 * Cache initialization routine.  This routine should only be called
 * once.  It performs the following tasks:
 *	- Initalize all global locks
 * 	- Call sub-initialization routines (localize access to variables)
 */
static int
cachefs_init(int fstyp, char *name)
{
	kstat_t *ksp;
	int error;

	ASSERT(cachefs_up == B_FALSE);

	error = cachefs_init_vfsops(fstyp);
	if (error != 0)
		return (error);

	error = cachefs_init_vnops(name);
	if (error != 0)
		return (error);

	mutex_init(&cachefs_cachelock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&cachefs_newnum_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&cachefs_kstat_key_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&cachefs_kmem_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&cachefs_rename_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&cachefs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&cachefs_async_lock, NULL, MUTEX_DEFAULT, NULL);
#ifdef CFSRLDEBUG
	mutex_init(&cachefs_rl_debug_mutex, NULL, MUTEX_DEFAULT, NULL);
#endif /* CFSRLDEBUG */

	/*
	 * set up kmem_cache entities
	 */

	cachefs_cnode_cache = kmem_cache_create("cachefs_cnode_cache",
	    sizeof (struct cnode), 0, NULL, NULL, NULL, NULL, NULL, 0);
	cachefs_req_cache = kmem_cache_create("cachefs_async_request",
	    sizeof (struct cachefs_req), 0,
	    cachefs_req_create, cachefs_req_destroy, NULL, NULL, NULL, 0);
	cachefs_fscache_cache = kmem_cache_create("cachefs_fscache",
	    sizeof (fscache_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
	cachefs_filegrp_cache = kmem_cache_create("cachefs_filegrp",
	    sizeof (filegrp_t), 0,
	    filegrp_cache_create, filegrp_cache_destroy, NULL, NULL, NULL, 0);
	cachefs_cache_kmcache = kmem_cache_create("cachefs_cache_t",
	    sizeof (cachefscache_t), 0, NULL, NULL, NULL, NULL, NULL, 0);

	/*
	 * set up the cachefs.0.key kstat
	 */

	cachefs_kstat_key = NULL;
	cachefs_kstat_key_n = 0;
	ksp = kstat_create("cachefs", 0, "key", "misc", KSTAT_TYPE_RAW, 1,
	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
	if (ksp != NULL) {
		ksp->ks_data = &cachefs_kstat_key;
		ksp->ks_update = cachefs_kstat_key_update;
		ksp->ks_snapshot = cachefs_kstat_key_snapshot;
		ksp->ks_lock = &cachefs_kstat_key_lock;
		kstat_install(ksp);
	}

	/*
	 * Assign unique major number for all nfs mounts
	 */

	if ((cachefs_major = getudev()) == -1) {
		cmn_err(CE_WARN,
			"cachefs: init: can't get unique device number");
		cachefs_major = 0;
	}
	cachefs_up = B_TRUE;
#ifdef CFSRLDEBUG
	cachefs_dbvalid = time;
#endif /* CFSRLDEBUG */

	return (0);
}

/*
 * Cache clean up routine. This routine is called if mod_install() failed
 * and we have to clean up because the module could not be installed,
 * or by _fini() when we're unloading the module.
 */
static void
cachefs_fini()
{
	extern int cachefsfstyp;
	extern struct vnodeops *cachefs_vnodeops;

	if (cachefs_up == B_FALSE) {
		/*
		 * cachefs_init() was not called on _init(),
		 * nothing to deallocate.
		 */
		return;
	}

	/*
	 * Clean up cachefs.0.key kstat.
	 * Currently, you can only do a
	 * modunload if cachefs_unloadable is nonzero, and that's
	 * pretty much just for debugging.  however, if there ever
	 * comes a day when cachefs is more freely unloadable
	 * (e.g. the modunload daemon can do it normally), then we'll
	 * have to make changes in the stats_ API.  this is because a
	 * stats_cookie_t holds the id # derived from here, and it
	 * will all go away at modunload time.  thus, the API will
	 * need to somehow be more robust than is currently necessary.
	 */
	kstat_delete_byname("cachefs", 0, "key");

	if (cachefs_kstat_key != NULL) {
		cachefs_kstat_key_t *key;
		int i;

		for (i = 0; i < cachefs_kstat_key_n; i++) {
			key = cachefs_kstat_key + i;

			cachefs_kmem_free((void *)(uintptr_t)key->ks_mountpoint,
			    strlen((char *)(uintptr_t)key->ks_mountpoint) + 1);
			cachefs_kmem_free((void *)(uintptr_t)key->ks_backfs,
			    strlen((char *)(uintptr_t)key->ks_backfs) + 1);
			cachefs_kmem_free((void *)(uintptr_t)key->ks_cachedir,
			    strlen((char *)(uintptr_t)key->ks_cachedir) + 1);
			cachefs_kmem_free((void *)(uintptr_t)key->ks_cacheid,
			    strlen((char *)(uintptr_t)key->ks_cacheid) + 1);
		}

		cachefs_kmem_free(cachefs_kstat_key,
		    cachefs_kstat_key_n * sizeof (*cachefs_kstat_key));
	}

	/*
	 * Clean up kmem_cache entities
	 */
	kmem_cache_destroy(cachefs_cache_kmcache);
	kmem_cache_destroy(cachefs_filegrp_cache);
	kmem_cache_destroy(cachefs_fscache_cache);
	kmem_cache_destroy(cachefs_req_cache);
	kmem_cache_destroy(cachefs_cnode_cache);
#ifdef CFSRLDEBUG
	if (cachefs_rl_debug_cache != NULL)
		kmem_cache_destroy(cachefs_rl_debug_cache);
#endif /* CFSRLDEBUG */

	/*
	 * Clean up the operations structures
	 */
	(void) vfs_freevfsops_by_type(cachefsfstyp);
	vn_freevnodeops(cachefs_vnodeops);

	/*
	 * Destroy mutexes
	 */
#ifdef CFSRLDEBUG
	mutex_destroy(&cachefs_rl_debug_mutex);
#endif /* CFSRLDEBUG */
	mutex_destroy(&cachefs_async_lock);
	mutex_destroy(&cachefs_minor_lock);
	mutex_destroy(&cachefs_rename_lock);
	mutex_destroy(&cachefs_kmem_lock);
	mutex_destroy(&cachefs_kstat_key_lock);
	mutex_destroy(&cachefs_newnum_lock);
	mutex_destroy(&cachefs_cachelock);
}