xref: /titanic_44/usr/src/uts/common/fs/cachefs/cachefs_module.c (revision 95efa359b507db290a2484b8f615822b1e06096f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/errno.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/user.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/vfs.h>
36 #include <sys/vnode.h>
37 #include <rpc/types.h>
38 #include <sys/mode.h>
39 #include <sys/cmn_err.h>
40 #include <sys/debug.h>
41 #include <sys/fs/cachefs_fs.h>
42 
43 /*
44  * This is the loadable module wrapper.
45  */
46 #include <sys/systm.h>
47 #include <sys/modctl.h>
48 #include <sys/syscall.h>
49 
50 extern time_t time;
51 
52 static int cachefs_init(int, char *);
53 static void cachefs_fini();
54 
55 static int cachefs_unloadable = 0; /* tunable */
56 static boolean_t cachefs_up = B_FALSE;
57 
58 uint_t cachefs_max_apop_inqueue = CACHEFS_MAX_APOP_INQUEUE;
59 
60 /*
61  * this is a list of possible hash table sizes, for the `double
62  * hashing' algorithm described in rosen's `elementary number theory
63  * and its applications'.  minimally, this needs to be a list of
64  * increasing prime integers, terminated by a 0.  ideally, they should
65  * be the larger of twin primes; i.e. P and P-2 are both prime.
66  */
67 
68 int cachefs_hash_sizes[] = {5, 2029, 4093, 8089, 16363, 32719, 0};
69 
70 /*
71  * Module linkage information for the kernel.
72  */
73 
74 static vfsdef_t vfs_z = {
75 	VFSDEF_VERSION,
76 	CACHEFS_BASETYPE,
77 	cachefs_init,
78 	VSW_CANREMOUNT,
79 	NULL
80 };
81 
82 static struct modlfs modlfs = {
83 	&mod_fsops,
84 	"cache filesystem",
85 	&vfs_z
86 };
87 
88 static struct modlinkage modlinkage = {
89 	MODREV_1, (void *)&modlfs, NULL
90 };
91 
92 char _depends_on[] = "strmod/rpcmod";
93 
94 int
95 _init(void)
96 {
97 	int status;
98 
99 	status = mod_install(&modlinkage);
100 	if (status != 0) {
101 		/*
102 		 * Could not load module, clean up the work performed
103 		 * by cachefs_init() which was indirectly called by
104 		 * mod_installfs() which in turn was called by mod_install().
105 		 */
106 		cachefs_fini();
107 	}
108 
109 	return (status);
110 }
111 
112 int
113 _info(struct modinfo *modinfop)
114 {
115 	return (mod_info(&modlinkage, modinfop));
116 }
117 
118 int
119 _fini(void)
120 {
121 	int status;
122 
123 	if (!cachefs_unloadable)
124 		return (EBUSY);
125 
126 	if ((status = mod_remove(&modlinkage)) == 0) {
127 		/*
128 		 * Module has been unloaded, now clean up
129 		 */
130 		cachefs_fini();
131 	}
132 
133 	return (status);
134 }
135 
136 extern kmutex_t cachefs_cachelock;		/* Cache list mutex */
137 extern kmutex_t cachefs_newnum_lock;
138 extern kmutex_t cachefs_kstat_key_lock;
139 extern kmutex_t cachefs_rename_lock;
140 extern kmutex_t cachefs_minor_lock;	/* Lock for minor device map */
141 extern kmutex_t cachefs_kmem_lock;
142 extern kmutex_t cachefs_async_lock;	/* global async work count */
143 extern major_t cachefs_major;
144 
145 /*
146  * Cache initialization routine.  This routine should only be called
147  * once.  It performs the following tasks:
148  *	- Initalize all global locks
149  * 	- Call sub-initialization routines (localize access to variables)
150  */
151 static int
152 cachefs_init(int fstyp, char *name)
153 {
154 	kstat_t *ksp;
155 	int error;
156 
157 	ASSERT(cachefs_up == B_FALSE);
158 
159 	error = cachefs_init_vfsops(fstyp);
160 	if (error != 0)
161 		return (error);
162 
163 	error = cachefs_init_vnops(name);
164 	if (error != 0)
165 		return (error);
166 
167 	mutex_init(&cachefs_cachelock, NULL, MUTEX_DEFAULT, NULL);
168 	mutex_init(&cachefs_newnum_lock, NULL, MUTEX_DEFAULT, NULL);
169 	mutex_init(&cachefs_kstat_key_lock, NULL, MUTEX_DEFAULT, NULL);
170 	mutex_init(&cachefs_kmem_lock, NULL, MUTEX_DEFAULT, NULL);
171 	mutex_init(&cachefs_rename_lock, NULL, MUTEX_DEFAULT, NULL);
172 	mutex_init(&cachefs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
173 	mutex_init(&cachefs_async_lock, NULL, MUTEX_DEFAULT, NULL);
174 #ifdef CFSRLDEBUG
175 	mutex_init(&cachefs_rl_debug_mutex, NULL, MUTEX_DEFAULT, NULL);
176 #endif /* CFSRLDEBUG */
177 
178 	/*
179 	 * set up kmem_cache entities
180 	 */
181 
182 	cachefs_cnode_cache = kmem_cache_create("cachefs_cnode_cache",
183 	    sizeof (struct cnode), 0, NULL, NULL, NULL, NULL, NULL, 0);
184 	cachefs_req_cache = kmem_cache_create("cachefs_async_request",
185 	    sizeof (struct cachefs_req), 0,
186 	    cachefs_req_create, cachefs_req_destroy, NULL, NULL, NULL, 0);
187 	cachefs_fscache_cache = kmem_cache_create("cachefs_fscache",
188 	    sizeof (fscache_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
189 	cachefs_filegrp_cache = kmem_cache_create("cachefs_filegrp",
190 	    sizeof (filegrp_t), 0,
191 	    filegrp_cache_create, filegrp_cache_destroy, NULL, NULL, NULL, 0);
192 	cachefs_cache_kmcache = kmem_cache_create("cachefs_cache_t",
193 	    sizeof (cachefscache_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
194 
195 	/*
196 	 * set up the cachefs.0.key kstat
197 	 */
198 
199 	cachefs_kstat_key = NULL;
200 	cachefs_kstat_key_n = 0;
201 	ksp = kstat_create("cachefs", 0, "key", "misc", KSTAT_TYPE_RAW, 1,
202 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
203 	if (ksp != NULL) {
204 		ksp->ks_data = &cachefs_kstat_key;
205 		ksp->ks_update = cachefs_kstat_key_update;
206 		ksp->ks_snapshot = cachefs_kstat_key_snapshot;
207 		ksp->ks_lock = &cachefs_kstat_key_lock;
208 		kstat_install(ksp);
209 	}
210 
211 	/*
212 	 * Assign unique major number for all nfs mounts
213 	 */
214 
215 	if ((cachefs_major = getudev()) == -1) {
216 		cmn_err(CE_WARN,
217 			"cachefs: init: can't get unique device number");
218 		cachefs_major = 0;
219 	}
220 	cachefs_up = B_TRUE;
221 #ifdef CFSRLDEBUG
222 	cachefs_dbvalid = time;
223 #endif /* CFSRLDEBUG */
224 
225 	return (0);
226 }
227 
228 /*
229  * Cache clean up routine. This routine is called if mod_install() failed
230  * and we have to clean up because the module could not be installed,
231  * or by _fini() when we're unloading the module.
232  */
233 static void
234 cachefs_fini()
235 {
236 	extern int cachefsfstyp;
237 	extern struct vnodeops *cachefs_vnodeops;
238 
239 	if (cachefs_up == B_FALSE) {
240 		/*
241 		 * cachefs_init() was not called on _init(),
242 		 * nothing to deallocate.
243 		 */
244 		return;
245 	}
246 
247 	/*
248 	 * Clean up cachefs.0.key kstat.
249 	 * Currently, you can only do a
250 	 * modunload if cachefs_unloadable is nonzero, and that's
251 	 * pretty much just for debugging.  however, if there ever
252 	 * comes a day when cachefs is more freely unloadable
253 	 * (e.g. the modunload daemon can do it normally), then we'll
254 	 * have to make changes in the stats_ API.  this is because a
255 	 * stats_cookie_t holds the id # derived from here, and it
256 	 * will all go away at modunload time.  thus, the API will
257 	 * need to somehow be more robust than is currently necessary.
258 	 */
259 	kstat_delete_byname("cachefs", 0, "key");
260 
261 	if (cachefs_kstat_key != NULL) {
262 		cachefs_kstat_key_t *key;
263 		int i;
264 
265 		for (i = 0; i < cachefs_kstat_key_n; i++) {
266 			key = cachefs_kstat_key + i;
267 
268 			cachefs_kmem_free((void *)(uintptr_t)key->ks_mountpoint,
269 			    strlen((char *)(uintptr_t)key->ks_mountpoint) + 1);
270 			cachefs_kmem_free((void *)(uintptr_t)key->ks_backfs,
271 			    strlen((char *)(uintptr_t)key->ks_backfs) + 1);
272 			cachefs_kmem_free((void *)(uintptr_t)key->ks_cachedir,
273 			    strlen((char *)(uintptr_t)key->ks_cachedir) + 1);
274 			cachefs_kmem_free((void *)(uintptr_t)key->ks_cacheid,
275 			    strlen((char *)(uintptr_t)key->ks_cacheid) + 1);
276 		}
277 
278 		cachefs_kmem_free(cachefs_kstat_key,
279 		    cachefs_kstat_key_n * sizeof (*cachefs_kstat_key));
280 	}
281 
282 	/*
283 	 * Clean up kmem_cache entities
284 	 */
285 	kmem_cache_destroy(cachefs_cache_kmcache);
286 	kmem_cache_destroy(cachefs_filegrp_cache);
287 	kmem_cache_destroy(cachefs_fscache_cache);
288 	kmem_cache_destroy(cachefs_req_cache);
289 	kmem_cache_destroy(cachefs_cnode_cache);
290 #ifdef CFSRLDEBUG
291 	if (cachefs_rl_debug_cache != NULL)
292 		kmem_cache_destroy(cachefs_rl_debug_cache);
293 #endif /* CFSRLDEBUG */
294 
295 	/*
296 	 * Clean up the operations structures
297 	 */
298 	(void) vfs_freevfsops_by_type(cachefsfstyp);
299 	vn_freevnodeops(cachefs_vnodeops);
300 
301 	/*
302 	 * Destroy mutexes
303 	 */
304 #ifdef CFSRLDEBUG
305 	mutex_destroy(&cachefs_rl_debug_mutex);
306 #endif /* CFSRLDEBUG */
307 	mutex_destroy(&cachefs_async_lock);
308 	mutex_destroy(&cachefs_minor_lock);
309 	mutex_destroy(&cachefs_rename_lock);
310 	mutex_destroy(&cachefs_kmem_lock);
311 	mutex_destroy(&cachefs_kstat_key_lock);
312 	mutex_destroy(&cachefs_newnum_lock);
313 	mutex_destroy(&cachefs_cachelock);
314 }
315