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