17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*89b43686SBayard Bell * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/errno.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/user.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
367c478bd9Sstevel@tonic-gate #include <rpc/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/mode.h>
387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * This is the loadable module wrapper.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
477c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate extern time_t time;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static int cachefs_init(int, char *);
527c478bd9Sstevel@tonic-gate static void cachefs_fini();
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static int cachefs_unloadable = 0; /* tunable */
557c478bd9Sstevel@tonic-gate static boolean_t cachefs_up = B_FALSE;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate uint_t cachefs_max_apop_inqueue = CACHEFS_MAX_APOP_INQUEUE;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * this is a list of possible hash table sizes, for the `double
617c478bd9Sstevel@tonic-gate * hashing' algorithm described in rosen's `elementary number theory
627c478bd9Sstevel@tonic-gate * and its applications'. minimally, this needs to be a list of
637c478bd9Sstevel@tonic-gate * increasing prime integers, terminated by a 0. ideally, they should
647c478bd9Sstevel@tonic-gate * be the larger of twin primes; i.e. P and P-2 are both prime.
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate int cachefs_hash_sizes[] = {5, 2029, 4093, 8089, 16363, 32719, 0};
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate static vfsdef_t vfs_z = {
747c478bd9Sstevel@tonic-gate VFSDEF_VERSION,
757c478bd9Sstevel@tonic-gate CACHEFS_BASETYPE,
767c478bd9Sstevel@tonic-gate cachefs_init,
777c478bd9Sstevel@tonic-gate VSW_CANREMOUNT,
787c478bd9Sstevel@tonic-gate NULL
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
827c478bd9Sstevel@tonic-gate &mod_fsops,
837c478bd9Sstevel@tonic-gate "cache filesystem",
847c478bd9Sstevel@tonic-gate &vfs_z
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
887c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlfs, NULL
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate int
_init(void)927c478bd9Sstevel@tonic-gate _init(void)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate int status;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate status = mod_install(&modlinkage);
977c478bd9Sstevel@tonic-gate if (status != 0) {
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Could not load module, clean up the work performed
1007c478bd9Sstevel@tonic-gate * by cachefs_init() which was indirectly called by
1017c478bd9Sstevel@tonic-gate * mod_installfs() which in turn was called by mod_install().
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate cachefs_fini();
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate return (status);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1107c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate int
_fini(void)1167c478bd9Sstevel@tonic-gate _fini(void)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate int status;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate if (!cachefs_unloadable)
1217c478bd9Sstevel@tonic-gate return (EBUSY);
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if ((status = mod_remove(&modlinkage)) == 0) {
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * Module has been unloaded, now clean up
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate cachefs_fini();
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate return (status);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_cachelock; /* Cache list mutex */
1347c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_newnum_lock;
1357c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_kstat_key_lock;
1367c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_rename_lock;
1377c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_minor_lock; /* Lock for minor device map */
1387c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_kmem_lock;
1397c478bd9Sstevel@tonic-gate extern kmutex_t cachefs_async_lock; /* global async work count */
1407c478bd9Sstevel@tonic-gate extern major_t cachefs_major;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * Cache initialization routine. This routine should only be called
1447c478bd9Sstevel@tonic-gate * once. It performs the following tasks:
1457c478bd9Sstevel@tonic-gate * - Initalize all global locks
1467c478bd9Sstevel@tonic-gate * - Call sub-initialization routines (localize access to variables)
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate static int
cachefs_init(int fstyp,char * name)1497c478bd9Sstevel@tonic-gate cachefs_init(int fstyp, char *name)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate kstat_t *ksp;
1527c478bd9Sstevel@tonic-gate int error;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate ASSERT(cachefs_up == B_FALSE);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate error = cachefs_init_vfsops(fstyp);
1577c478bd9Sstevel@tonic-gate if (error != 0)
1587c478bd9Sstevel@tonic-gate return (error);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate error = cachefs_init_vnops(name);
1617c478bd9Sstevel@tonic-gate if (error != 0)
1627c478bd9Sstevel@tonic-gate return (error);
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate mutex_init(&cachefs_cachelock, NULL, MUTEX_DEFAULT, NULL);
1657c478bd9Sstevel@tonic-gate mutex_init(&cachefs_newnum_lock, NULL, MUTEX_DEFAULT, NULL);
1667c478bd9Sstevel@tonic-gate mutex_init(&cachefs_kstat_key_lock, NULL, MUTEX_DEFAULT, NULL);
1677c478bd9Sstevel@tonic-gate mutex_init(&cachefs_kmem_lock, NULL, MUTEX_DEFAULT, NULL);
1687c478bd9Sstevel@tonic-gate mutex_init(&cachefs_rename_lock, NULL, MUTEX_DEFAULT, NULL);
1697c478bd9Sstevel@tonic-gate mutex_init(&cachefs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
1707c478bd9Sstevel@tonic-gate mutex_init(&cachefs_async_lock, NULL, MUTEX_DEFAULT, NULL);
1717c478bd9Sstevel@tonic-gate #ifdef CFSRLDEBUG
1727c478bd9Sstevel@tonic-gate mutex_init(&cachefs_rl_debug_mutex, NULL, MUTEX_DEFAULT, NULL);
1737c478bd9Sstevel@tonic-gate #endif /* CFSRLDEBUG */
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * set up kmem_cache entities
1777c478bd9Sstevel@tonic-gate */
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate cachefs_cnode_cache = kmem_cache_create("cachefs_cnode_cache",
1807c478bd9Sstevel@tonic-gate sizeof (struct cnode), 0, NULL, NULL, NULL, NULL, NULL, 0);
1817c478bd9Sstevel@tonic-gate cachefs_req_cache = kmem_cache_create("cachefs_async_request",
1827c478bd9Sstevel@tonic-gate sizeof (struct cachefs_req), 0,
1837c478bd9Sstevel@tonic-gate cachefs_req_create, cachefs_req_destroy, NULL, NULL, NULL, 0);
1847c478bd9Sstevel@tonic-gate cachefs_fscache_cache = kmem_cache_create("cachefs_fscache",
1857c478bd9Sstevel@tonic-gate sizeof (fscache_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
1867c478bd9Sstevel@tonic-gate cachefs_filegrp_cache = kmem_cache_create("cachefs_filegrp",
1877c478bd9Sstevel@tonic-gate sizeof (filegrp_t), 0,
1887c478bd9Sstevel@tonic-gate filegrp_cache_create, filegrp_cache_destroy, NULL, NULL, NULL, 0);
1897c478bd9Sstevel@tonic-gate cachefs_cache_kmcache = kmem_cache_create("cachefs_cache_t",
1907c478bd9Sstevel@tonic-gate sizeof (cachefscache_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * set up the cachefs.0.key kstat
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate cachefs_kstat_key = NULL;
1977c478bd9Sstevel@tonic-gate cachefs_kstat_key_n = 0;
1987c478bd9Sstevel@tonic-gate ksp = kstat_create("cachefs", 0, "key", "misc", KSTAT_TYPE_RAW, 1,
1997c478bd9Sstevel@tonic-gate KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
2007c478bd9Sstevel@tonic-gate if (ksp != NULL) {
2017c478bd9Sstevel@tonic-gate ksp->ks_data = &cachefs_kstat_key;
2027c478bd9Sstevel@tonic-gate ksp->ks_update = cachefs_kstat_key_update;
2037c478bd9Sstevel@tonic-gate ksp->ks_snapshot = cachefs_kstat_key_snapshot;
2047c478bd9Sstevel@tonic-gate ksp->ks_lock = &cachefs_kstat_key_lock;
2057c478bd9Sstevel@tonic-gate kstat_install(ksp);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * Assign unique major number for all nfs mounts
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if ((cachefs_major = getudev()) == -1) {
2137c478bd9Sstevel@tonic-gate cmn_err(CE_WARN,
2147c478bd9Sstevel@tonic-gate "cachefs: init: can't get unique device number");
2157c478bd9Sstevel@tonic-gate cachefs_major = 0;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate cachefs_up = B_TRUE;
2187c478bd9Sstevel@tonic-gate #ifdef CFSRLDEBUG
2197c478bd9Sstevel@tonic-gate cachefs_dbvalid = time;
2207c478bd9Sstevel@tonic-gate #endif /* CFSRLDEBUG */
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * Cache clean up routine. This routine is called if mod_install() failed
2277c478bd9Sstevel@tonic-gate * and we have to clean up because the module could not be installed,
2287c478bd9Sstevel@tonic-gate * or by _fini() when we're unloading the module.
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate static void
cachefs_fini()2317c478bd9Sstevel@tonic-gate cachefs_fini()
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate extern int cachefsfstyp;
2347c478bd9Sstevel@tonic-gate extern struct vnodeops *cachefs_vnodeops;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate if (cachefs_up == B_FALSE) {
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * cachefs_init() was not called on _init(),
2397c478bd9Sstevel@tonic-gate * nothing to deallocate.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate return;
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * Clean up cachefs.0.key kstat.
2467c478bd9Sstevel@tonic-gate * Currently, you can only do a
2477c478bd9Sstevel@tonic-gate * modunload if cachefs_unloadable is nonzero, and that's
2487c478bd9Sstevel@tonic-gate * pretty much just for debugging. however, if there ever
2497c478bd9Sstevel@tonic-gate * comes a day when cachefs is more freely unloadable
2507c478bd9Sstevel@tonic-gate * (e.g. the modunload daemon can do it normally), then we'll
2517c478bd9Sstevel@tonic-gate * have to make changes in the stats_ API. this is because a
2527c478bd9Sstevel@tonic-gate * stats_cookie_t holds the id # derived from here, and it
2537c478bd9Sstevel@tonic-gate * will all go away at modunload time. thus, the API will
2547c478bd9Sstevel@tonic-gate * need to somehow be more robust than is currently necessary.
2557c478bd9Sstevel@tonic-gate */
2567c478bd9Sstevel@tonic-gate kstat_delete_byname("cachefs", 0, "key");
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate if (cachefs_kstat_key != NULL) {
2597c478bd9Sstevel@tonic-gate cachefs_kstat_key_t *key;
2607c478bd9Sstevel@tonic-gate int i;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate for (i = 0; i < cachefs_kstat_key_n; i++) {
2637c478bd9Sstevel@tonic-gate key = cachefs_kstat_key + i;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate cachefs_kmem_free((void *)(uintptr_t)key->ks_mountpoint,
2667c478bd9Sstevel@tonic-gate strlen((char *)(uintptr_t)key->ks_mountpoint) + 1);
2677c478bd9Sstevel@tonic-gate cachefs_kmem_free((void *)(uintptr_t)key->ks_backfs,
2687c478bd9Sstevel@tonic-gate strlen((char *)(uintptr_t)key->ks_backfs) + 1);
2697c478bd9Sstevel@tonic-gate cachefs_kmem_free((void *)(uintptr_t)key->ks_cachedir,
2707c478bd9Sstevel@tonic-gate strlen((char *)(uintptr_t)key->ks_cachedir) + 1);
2717c478bd9Sstevel@tonic-gate cachefs_kmem_free((void *)(uintptr_t)key->ks_cacheid,
2727c478bd9Sstevel@tonic-gate strlen((char *)(uintptr_t)key->ks_cacheid) + 1);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate cachefs_kmem_free(cachefs_kstat_key,
2767c478bd9Sstevel@tonic-gate cachefs_kstat_key_n * sizeof (*cachefs_kstat_key));
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate * Clean up kmem_cache entities
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate kmem_cache_destroy(cachefs_cache_kmcache);
2837c478bd9Sstevel@tonic-gate kmem_cache_destroy(cachefs_filegrp_cache);
2847c478bd9Sstevel@tonic-gate kmem_cache_destroy(cachefs_fscache_cache);
2857c478bd9Sstevel@tonic-gate kmem_cache_destroy(cachefs_req_cache);
2867c478bd9Sstevel@tonic-gate kmem_cache_destroy(cachefs_cnode_cache);
2877c478bd9Sstevel@tonic-gate #ifdef CFSRLDEBUG
2887c478bd9Sstevel@tonic-gate if (cachefs_rl_debug_cache != NULL)
2897c478bd9Sstevel@tonic-gate kmem_cache_destroy(cachefs_rl_debug_cache);
2907c478bd9Sstevel@tonic-gate #endif /* CFSRLDEBUG */
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate * Clean up the operations structures
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate (void) vfs_freevfsops_by_type(cachefsfstyp);
2967c478bd9Sstevel@tonic-gate vn_freevnodeops(cachefs_vnodeops);
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * Destroy mutexes
3007c478bd9Sstevel@tonic-gate */
3017c478bd9Sstevel@tonic-gate #ifdef CFSRLDEBUG
3027c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_rl_debug_mutex);
3037c478bd9Sstevel@tonic-gate #endif /* CFSRLDEBUG */
3047c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_async_lock);
3057c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_minor_lock);
3067c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_rename_lock);
3077c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_kmem_lock);
3087c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_kstat_key_lock);
3097c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_newnum_lock);
3107c478bd9Sstevel@tonic-gate mutex_destroy(&cachefs_cachelock);
3117c478bd9Sstevel@tonic-gate }
312