xref: /titanic_53/usr/src/uts/common/io/lofi.c (revision b3388e4fc5f5c24c8a39fbe132a00b02dae5b717)
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
5bd07e074Sheppo  * Common Development and Distribution License (the "License").
6bd07e074Sheppo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22947caa0eSDina K Nimeh  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
287c478bd9Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
297c478bd9Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
307c478bd9Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
317c478bd9Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
327c478bd9Sstevel@tonic-gate  * mounting images of filesystems.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
357c478bd9Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
367c478bd9Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
377c478bd9Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
387c478bd9Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
397c478bd9Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
407c478bd9Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
417c478bd9Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
427c478bd9Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
437c478bd9Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
467c478bd9Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
477c478bd9Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
487c478bd9Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
497c478bd9Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
507c478bd9Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
537c478bd9Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
547c478bd9Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
557c478bd9Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
567c478bd9Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
577c478bd9Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
587c478bd9Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
597c478bd9Sstevel@tonic-gate  *
603d7072f8Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
613d7072f8Seschrock  * support simulation of hotplug events, an optional force flag is provided.
623d7072f8Seschrock  * If a lofi device is open when a force detach is requested, then the
633d7072f8Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
643d7072f8Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
653d7072f8Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
663d7072f8Seschrock  * detached but not removed.
673d7072f8Seschrock  *
687c478bd9Sstevel@tonic-gate  * Known problems:
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
717c478bd9Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
727c478bd9Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
737c478bd9Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
767c478bd9Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
777c478bd9Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
787c478bd9Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
817c478bd9Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
827c478bd9Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
837c478bd9Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
847c478bd9Sstevel@tonic-gate  *	do. Though it would still be silly.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * Interesting things to do:
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
917c478bd9Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
927d82f0f8SDina K Nimeh  *	need to fake a geometry, however, it may be relevant if you're replacing
937d82f0f8SDina K Nimeh  *	metadisk, or using lofi to get crypto.
947d82f0f8SDina K Nimeh  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
957d82f0f8SDina K Nimeh  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
967d82f0f8SDina K Nimeh  *	In fact this even makes sense if you have lofi "above" metadisk.
977c478bd9Sstevel@tonic-gate  *
987d82f0f8SDina K Nimeh  * Encryption:
997d82f0f8SDina K Nimeh  *	Each lofi device can have its own symmetric key and cipher.
1007d82f0f8SDina K Nimeh  *	They are passed to us by lofiadm(1m) in the correct format for use
1017d82f0f8SDina K Nimeh  *	with the misc/kcf crypto_* routines.
1027d82f0f8SDina K Nimeh  *
1037d82f0f8SDina K Nimeh  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
1047d82f0f8SDina K Nimeh  *	on the "master" key held in the lsp and the block number of the buffer.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #include <sys/types.h>
10887117650Saalok #include <netinet/in.h>
1097c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1107c478bd9Sstevel@tonic-gate #include <sys/uio.h>
1117c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1127c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1137c478bd9Sstevel@tonic-gate #include <sys/mman.h>
1147c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1157c478bd9Sstevel@tonic-gate #include <sys/aio_req.h>
1167c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1177c478bd9Sstevel@tonic-gate #include <sys/file.h>
1187c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1197c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1207c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1217c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
1227c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
1237c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
1247c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1257c478bd9Sstevel@tonic-gate #include <sys/filio.h>
1267c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
1277c478bd9Sstevel@tonic-gate #include <sys/open.h>
1287c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1297c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1307c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
13287117650Saalok #include <sys/zmod.h>
1337d82f0f8SDina K Nimeh #include <sys/crypto/common.h>
1347d82f0f8SDina K Nimeh #include <sys/crypto/api.h>
135b1efbcd6SAlok Aggarwal #include <LzmaDec.h>
1367d82f0f8SDina K Nimeh 
1377d82f0f8SDina K Nimeh /*
1387d82f0f8SDina K Nimeh  * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
1397d82f0f8SDina K Nimeh  * Crypto metadata, if it exists, is located at the end of the boot block
1407d82f0f8SDina K Nimeh  * (BBOFF + BBSIZE, which is SBOFF).  The super block and everything after
1417d82f0f8SDina K Nimeh  * is offset by the size of the crypto metadata which is handled by
1427d82f0f8SDina K Nimeh  * lsp->ls_crypto_offset.
1437d82f0f8SDina K Nimeh  */
1447d82f0f8SDina K Nimeh #define	CRYOFF	((off_t)8192)
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
1477c478bd9Sstevel@tonic-gate #define	SIZE_PROP_NAME		"Size"
1487c478bd9Sstevel@tonic-gate 
1497d82f0f8SDina K Nimeh #define	SETUP_C_DATA(cd, buf, len) 		\
1507d82f0f8SDina K Nimeh 	(cd).cd_format = CRYPTO_DATA_RAW;	\
1517d82f0f8SDina K Nimeh 	(cd).cd_offset = 0;			\
1527d82f0f8SDina K Nimeh 	(cd).cd_miscdata = NULL;		\
1537d82f0f8SDina K Nimeh 	(cd).cd_length = (len);			\
1547d82f0f8SDina K Nimeh 	(cd).cd_raw.iov_base = (buf);		\
1557d82f0f8SDina K Nimeh 	(cd).cd_raw.iov_len = (len);
1567d82f0f8SDina K Nimeh 
1577d82f0f8SDina K Nimeh #define	UIO_CHECK(uio)	\
1587d82f0f8SDina K Nimeh 	if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
1597d82f0f8SDina K Nimeh 	    ((uio)->uio_resid % DEV_BSIZE) != 0) { \
1607d82f0f8SDina K Nimeh 		return (EINVAL); \
1617d82f0f8SDina K Nimeh 	}
1627d82f0f8SDina K Nimeh 
1637d82f0f8SDina K Nimeh static dev_info_t *lofi_dip = NULL;
1647d82f0f8SDina K Nimeh static void *lofi_statep = NULL;
1657c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1697c478bd9Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1707c478bd9Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
1717c478bd9Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
1727c478bd9Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1737c478bd9Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
1747c478bd9Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
1757c478bd9Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
1767c478bd9Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
1777c478bd9Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
1807c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES;
1837d82f0f8SDina K Nimeh const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
1847c478bd9Sstevel@tonic-gate 
1854058a205Sjrgn.keil@googlemail.com /*
1864058a205Sjrgn.keil@googlemail.com  * To avoid decompressing data in a compressed segment multiple times
1874058a205Sjrgn.keil@googlemail.com  * when accessing small parts of a segment's data, we cache and reuse
1884058a205Sjrgn.keil@googlemail.com  * the uncompressed segment's data.
1894058a205Sjrgn.keil@googlemail.com  *
1904058a205Sjrgn.keil@googlemail.com  * A single cached segment is sufficient to avoid lots of duplicate
1914058a205Sjrgn.keil@googlemail.com  * segment decompress operations. A small cache size also reduces the
1924058a205Sjrgn.keil@googlemail.com  * memory footprint.
1934058a205Sjrgn.keil@googlemail.com  *
1944058a205Sjrgn.keil@googlemail.com  * lofi_max_comp_cache is the maximum number of decompressed data segments
1954058a205Sjrgn.keil@googlemail.com  * cached for each compressed lofi image. It can be set to 0 to disable
1964058a205Sjrgn.keil@googlemail.com  * caching.
1974058a205Sjrgn.keil@googlemail.com  */
1984058a205Sjrgn.keil@googlemail.com 
1994058a205Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1;
2004058a205Sjrgn.keil@googlemail.com 
20187117650Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
20287117650Saalok 	size_t *destlen, int level);
20387117650Saalok 
204b1efbcd6SAlok Aggarwal static int lzma_decompress(void *src, size_t srclen, void *dst,
205b1efbcd6SAlok Aggarwal 	size_t *dstlen, int level);
206b1efbcd6SAlok Aggarwal 
20787117650Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
20887117650Saalok 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
20987117650Saalok 	{gzip_decompress,	NULL,	6,	"gzip-6"},
210b1efbcd6SAlok Aggarwal 	{gzip_decompress,	NULL,	9,	"gzip-9"},
211b1efbcd6SAlok Aggarwal 	{lzma_decompress,	NULL,	0,	"lzma"}
21287117650Saalok };
21387117650Saalok 
214b1efbcd6SAlok Aggarwal /*ARGSUSED*/
215b1efbcd6SAlok Aggarwal static void
216b1efbcd6SAlok Aggarwal *SzAlloc(void *p, size_t size)
217b1efbcd6SAlok Aggarwal {
218b1efbcd6SAlok Aggarwal 	return (kmem_alloc(size, KM_SLEEP));
219b1efbcd6SAlok Aggarwal }
220b1efbcd6SAlok Aggarwal 
221b1efbcd6SAlok Aggarwal /*ARGSUSED*/
222b1efbcd6SAlok Aggarwal static void
223b1efbcd6SAlok Aggarwal SzFree(void *p, void *address, size_t size)
224b1efbcd6SAlok Aggarwal {
225b1efbcd6SAlok Aggarwal 	kmem_free(address, size);
226b1efbcd6SAlok Aggarwal }
227b1efbcd6SAlok Aggarwal 
228b1efbcd6SAlok Aggarwal static ISzAlloc g_Alloc = { SzAlloc, SzFree };
229b1efbcd6SAlok Aggarwal 
2304058a205Sjrgn.keil@googlemail.com /*
2314058a205Sjrgn.keil@googlemail.com  * Free data referenced by the linked list of cached uncompressed
2324058a205Sjrgn.keil@googlemail.com  * segments.
2334058a205Sjrgn.keil@googlemail.com  */
2344058a205Sjrgn.keil@googlemail.com static void
2354058a205Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp)
2364058a205Sjrgn.keil@googlemail.com {
2374058a205Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
2384058a205Sjrgn.keil@googlemail.com 
2394058a205Sjrgn.keil@googlemail.com 	while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
2404058a205Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
2414058a205Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
2424058a205Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
2434058a205Sjrgn.keil@googlemail.com 	}
2444058a205Sjrgn.keil@googlemail.com 	ASSERT(lsp->ls_comp_cache_count == 0);
2454058a205Sjrgn.keil@googlemail.com }
2464058a205Sjrgn.keil@googlemail.com 
2477c478bd9Sstevel@tonic-gate static int
2487c478bd9Sstevel@tonic-gate lofi_busy(void)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	minor_t	minor;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
2547c478bd9Sstevel@tonic-gate 	 * help because the device isn't open.
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
2577c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
2587c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
2597c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2607c478bd9Sstevel@tonic-gate 			return (EBUSY);
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2647c478bd9Sstevel@tonic-gate 	return (0);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static int
2687c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2717c478bd9Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate static int
2757c478bd9Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2787c478bd9Sstevel@tonic-gate 	switch (otyp) {
2797c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
2807c478bd9Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
2817c478bd9Sstevel@tonic-gate 		break;
2827c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
2837c478bd9Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
2847c478bd9Sstevel@tonic-gate 		break;
2857c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
2867c478bd9Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
2877c478bd9Sstevel@tonic-gate 		break;
2887c478bd9Sstevel@tonic-gate 	default:
2897c478bd9Sstevel@tonic-gate 		return (-1);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	return (0);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static void
2957c478bd9Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2987c478bd9Sstevel@tonic-gate 	switch (otyp) {
2997c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
3007c478bd9Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
3017c478bd9Sstevel@tonic-gate 		break;
3027c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
3037c478bd9Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
3067c478bd9Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
3077c478bd9Sstevel@tonic-gate 		break;
3087c478bd9Sstevel@tonic-gate 	default:
3097c478bd9Sstevel@tonic-gate 		break;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3133d7072f8Seschrock static void
3147d82f0f8SDina K Nimeh lofi_free_crypto(struct lofi_state *lsp)
3157d82f0f8SDina K Nimeh {
3167d82f0f8SDina K Nimeh 	ASSERT(mutex_owned(&lofi_lock));
3177d82f0f8SDina K Nimeh 
3187d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
3197d82f0f8SDina K Nimeh 		/*
3207d82f0f8SDina K Nimeh 		 * Clean up the crypto state so that it doesn't hang around
3217d82f0f8SDina K Nimeh 		 * in memory after we are done with it.
3227d82f0f8SDina K Nimeh 		 */
3237d82f0f8SDina K Nimeh 		bzero(lsp->ls_key.ck_data,
3247d82f0f8SDina K Nimeh 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3257d82f0f8SDina K Nimeh 		kmem_free(lsp->ls_key.ck_data,
3267d82f0f8SDina K Nimeh 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3277d82f0f8SDina K Nimeh 		lsp->ls_key.ck_data = NULL;
3287d82f0f8SDina K Nimeh 		lsp->ls_key.ck_length = 0;
3297d82f0f8SDina K Nimeh 
3307d82f0f8SDina K Nimeh 		if (lsp->ls_mech.cm_param != NULL) {
3317d82f0f8SDina K Nimeh 			kmem_free(lsp->ls_mech.cm_param,
3327d82f0f8SDina K Nimeh 			    lsp->ls_mech.cm_param_len);
3337d82f0f8SDina K Nimeh 			lsp->ls_mech.cm_param = NULL;
3347d82f0f8SDina K Nimeh 			lsp->ls_mech.cm_param_len = 0;
3357d82f0f8SDina K Nimeh 		}
3367d82f0f8SDina K Nimeh 
3377d82f0f8SDina K Nimeh 		if (lsp->ls_iv_mech.cm_param != NULL) {
3387d82f0f8SDina K Nimeh 			kmem_free(lsp->ls_iv_mech.cm_param,
3397d82f0f8SDina K Nimeh 			    lsp->ls_iv_mech.cm_param_len);
3407d82f0f8SDina K Nimeh 			lsp->ls_iv_mech.cm_param = NULL;
3417d82f0f8SDina K Nimeh 			lsp->ls_iv_mech.cm_param_len = 0;
3427d82f0f8SDina K Nimeh 		}
3437d82f0f8SDina K Nimeh 
3447d82f0f8SDina K Nimeh 		mutex_destroy(&lsp->ls_crypto_lock);
3457d82f0f8SDina K Nimeh 	}
3467d82f0f8SDina K Nimeh }
3477d82f0f8SDina K Nimeh 
3487d82f0f8SDina K Nimeh static void
3493d7072f8Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
3503d7072f8Seschrock     cred_t *credp)
3513d7072f8Seschrock {
3523d7072f8Seschrock 	dev_t	newdev;
3533d7072f8Seschrock 	char	namebuf[50];
3543d7072f8Seschrock 
3557d82f0f8SDina K Nimeh 	ASSERT(mutex_owned(&lofi_lock));
3567d82f0f8SDina K Nimeh 
3577d82f0f8SDina K Nimeh 	lofi_free_crypto(lsp);
3587d82f0f8SDina K Nimeh 
3593d7072f8Seschrock 	if (lsp->ls_vp) {
360da6c28aaSamw 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
361da6c28aaSamw 		    1, 0, credp, NULL);
3623d7072f8Seschrock 		VN_RELE(lsp->ls_vp);
3633d7072f8Seschrock 		lsp->ls_vp = NULL;
3643d7072f8Seschrock 	}
3653d7072f8Seschrock 
3663d7072f8Seschrock 	newdev = makedevice(getmajor(dev), minor);
3673d7072f8Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
3683d7072f8Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
3693d7072f8Seschrock 
3703d7072f8Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
3713d7072f8Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3723d7072f8Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
3733d7072f8Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3743d7072f8Seschrock 
3753d7072f8Seschrock 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
3763d7072f8Seschrock 	taskq_destroy(lsp->ls_taskq);
3773d7072f8Seschrock 	if (lsp->ls_kstat) {
3783d7072f8Seschrock 		kstat_delete(lsp->ls_kstat);
3793d7072f8Seschrock 		mutex_destroy(&lsp->ls_kstat_lock);
3803d7072f8Seschrock 	}
381a423e759Saalok 
3824058a205Sjrgn.keil@googlemail.com 	/*
3834058a205Sjrgn.keil@googlemail.com 	 * Free cached decompressed segment data
3844058a205Sjrgn.keil@googlemail.com 	 */
3854058a205Sjrgn.keil@googlemail.com 	lofi_free_comp_cache(lsp);
3864058a205Sjrgn.keil@googlemail.com 	list_destroy(&lsp->ls_comp_cache);
3874058a205Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_comp_cache_lock);
3884058a205Sjrgn.keil@googlemail.com 
389a423e759Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
390a423e759Saalok 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
391a423e759Saalok 		lsp->ls_uncomp_seg_sz = 0;
392a423e759Saalok 	}
3934058a205Sjrgn.keil@googlemail.com 
3944058a205Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_vp_lock);
3954058a205Sjrgn.keil@googlemail.com 
3963d7072f8Seschrock 	ddi_soft_state_free(lofi_statep, minor);
3973d7072f8Seschrock }
3983d7072f8Seschrock 
3993d7072f8Seschrock /*ARGSUSED*/
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	minor_t	minor;
4047c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4077c478bd9Sstevel@tonic-gate 	minor = getminor(*devp);
4087c478bd9Sstevel@tonic-gate 	if (minor == 0) {
4097c478bd9Sstevel@tonic-gate 		/* master control device */
4107c478bd9Sstevel@tonic-gate 		/* must be opened exclusively */
4117c478bd9Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
4127c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4137c478bd9Sstevel@tonic-gate 			return (EINVAL);
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
4167c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
4177c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4187c478bd9Sstevel@tonic-gate 			return (ENXIO);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 		if (is_opened(lsp)) {
4217c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4227c478bd9Sstevel@tonic-gate 			return (EBUSY);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
4257c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4267c478bd9Sstevel@tonic-gate 		return (0);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
4307c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4317c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
4327c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4337c478bd9Sstevel@tonic-gate 		return (EINVAL);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4363d7072f8Seschrock 	if (lsp->ls_vp == NULL) {
4373d7072f8Seschrock 		mutex_exit(&lofi_lock);
4383d7072f8Seschrock 		return (ENXIO);
4393d7072f8Seschrock 	}
4403d7072f8Seschrock 
4417c478bd9Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
4427c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4437c478bd9Sstevel@tonic-gate 		return (EINVAL);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4477c478bd9Sstevel@tonic-gate 	return (0);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4503d7072f8Seschrock /*ARGSUSED*/
4517c478bd9Sstevel@tonic-gate static int
4527c478bd9Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	minor_t	minor;
4557c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4587c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
4597c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4607c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
4617c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4627c478bd9Sstevel@tonic-gate 		return (EINVAL);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	mark_closed(lsp, otyp);
4653d7072f8Seschrock 
4663d7072f8Seschrock 	/*
46793239addSjohnlev 	 * If we forcibly closed the underlying device (li_force), or
46893239addSjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
46993239addSjohnlev 	 * out of the door.
4703d7072f8Seschrock 	 */
47193239addSjohnlev 	if (minor != 0 && !is_opened(lsp) &&
47293239addSjohnlev 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
4733d7072f8Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
47493239addSjohnlev 
4757c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4767c478bd9Sstevel@tonic-gate 	return (0);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797d82f0f8SDina K Nimeh /*
4807d82f0f8SDina K Nimeh  * Sets the mechanism's initialization vector (IV) if one is needed.
4817d82f0f8SDina K Nimeh  * The IV is computed from the data block number.  lsp->ls_mech is
4827d82f0f8SDina K Nimeh  * altered so that:
4837d82f0f8SDina K Nimeh  *	lsp->ls_mech.cm_param_len is set to the IV len.
4847d82f0f8SDina K Nimeh  *	lsp->ls_mech.cm_param is set to the IV.
4857d82f0f8SDina K Nimeh  */
4867d82f0f8SDina K Nimeh static int
4877d82f0f8SDina K Nimeh lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
4887d82f0f8SDina K Nimeh {
4897d82f0f8SDina K Nimeh 	int	ret;
4907d82f0f8SDina K Nimeh 	crypto_data_t cdata;
4917d82f0f8SDina K Nimeh 	char	*iv;
4927d82f0f8SDina K Nimeh 	size_t	iv_len;
4937d82f0f8SDina K Nimeh 	size_t	min;
4947d82f0f8SDina K Nimeh 	void	*data;
4957d82f0f8SDina K Nimeh 	size_t	datasz;
4967d82f0f8SDina K Nimeh 
4977d82f0f8SDina K Nimeh 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
4987d82f0f8SDina K Nimeh 
4997d82f0f8SDina K Nimeh 	if (lsp == NULL)
5007d82f0f8SDina K Nimeh 		return (CRYPTO_DEVICE_ERROR);
5017d82f0f8SDina K Nimeh 
5027d82f0f8SDina K Nimeh 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
5037d82f0f8SDina K Nimeh 	if (lsp->ls_iv_type == IVM_NONE) {
5047d82f0f8SDina K Nimeh 		return (CRYPTO_SUCCESS);
5057d82f0f8SDina K Nimeh 	}
5067d82f0f8SDina K Nimeh 
5077d82f0f8SDina K Nimeh 	/*
5087d82f0f8SDina K Nimeh 	 * if kmem already alloced from previous call and it's the same size
5097d82f0f8SDina K Nimeh 	 * we need now, just recycle it; allocate new kmem only if we have to
5107d82f0f8SDina K Nimeh 	 */
5117d82f0f8SDina K Nimeh 	if (lsp->ls_mech.cm_param == NULL ||
5127d82f0f8SDina K Nimeh 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
5137d82f0f8SDina K Nimeh 		iv_len = lsp->ls_iv_len;
5147d82f0f8SDina K Nimeh 		iv = kmem_zalloc(iv_len, KM_SLEEP);
5157d82f0f8SDina K Nimeh 	} else {
5167d82f0f8SDina K Nimeh 		iv_len = lsp->ls_mech.cm_param_len;
5177d82f0f8SDina K Nimeh 		iv = lsp->ls_mech.cm_param;
5187d82f0f8SDina K Nimeh 		bzero(iv, iv_len);
5197d82f0f8SDina K Nimeh 	}
5207d82f0f8SDina K Nimeh 
5217d82f0f8SDina K Nimeh 	switch (lsp->ls_iv_type) {
5227d82f0f8SDina K Nimeh 	case IVM_ENC_BLKNO:
5237d82f0f8SDina K Nimeh 		/* iv is not static, lblkno changes each time */
5247d82f0f8SDina K Nimeh 		data = &lblkno;
5257d82f0f8SDina K Nimeh 		datasz = sizeof (lblkno);
5267d82f0f8SDina K Nimeh 		break;
5277d82f0f8SDina K Nimeh 	default:
5287d82f0f8SDina K Nimeh 		data = 0;
5297d82f0f8SDina K Nimeh 		datasz = 0;
5307d82f0f8SDina K Nimeh 		break;
5317d82f0f8SDina K Nimeh 	}
5327d82f0f8SDina K Nimeh 
5337d82f0f8SDina K Nimeh 	/*
5347d82f0f8SDina K Nimeh 	 * write blkno into the iv buffer padded on the left in case
5357d82f0f8SDina K Nimeh 	 * blkno ever grows bigger than its current longlong_t size
5367d82f0f8SDina K Nimeh 	 * or a variation other than blkno is used for the iv data
5377d82f0f8SDina K Nimeh 	 */
5387d82f0f8SDina K Nimeh 	min = MIN(datasz, iv_len);
5397d82f0f8SDina K Nimeh 	bcopy(data, iv + (iv_len - min), min);
5407d82f0f8SDina K Nimeh 
5417d82f0f8SDina K Nimeh 	/* encrypt the data in-place to get the IV */
5427d82f0f8SDina K Nimeh 	SETUP_C_DATA(cdata, iv, iv_len);
5437d82f0f8SDina K Nimeh 
5447d82f0f8SDina K Nimeh 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
5457d82f0f8SDina K Nimeh 	    NULL, NULL, NULL);
5467d82f0f8SDina K Nimeh 	if (ret != CRYPTO_SUCCESS) {
5477d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
5487d82f0f8SDina K Nimeh 		    lblkno, ret);
5497d82f0f8SDina K Nimeh 		if (lsp->ls_mech.cm_param != iv)
5507d82f0f8SDina K Nimeh 			kmem_free(iv, iv_len);
551b1efbcd6SAlok Aggarwal 
5527d82f0f8SDina K Nimeh 		return (ret);
5537d82f0f8SDina K Nimeh 	}
5547d82f0f8SDina K Nimeh 
5557d82f0f8SDina K Nimeh 	/* clean up the iv from the last computation */
5567d82f0f8SDina K Nimeh 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
5577d82f0f8SDina K Nimeh 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
558b1efbcd6SAlok Aggarwal 
5597d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_param_len = iv_len;
5607d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_param = iv;
5617d82f0f8SDina K Nimeh 
5627d82f0f8SDina K Nimeh 	return (CRYPTO_SUCCESS);
5637d82f0f8SDina K Nimeh }
5647d82f0f8SDina K Nimeh 
5657d82f0f8SDina K Nimeh /*
5667d82f0f8SDina K Nimeh  * Performs encryption and decryption of a chunk of data of size "len",
5677d82f0f8SDina K Nimeh  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
5687d82f0f8SDina K Nimeh  * DEV_BSIZE.
5697d82f0f8SDina K Nimeh  */
5707d82f0f8SDina K Nimeh static int
5717d82f0f8SDina K Nimeh lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
5727d82f0f8SDina K Nimeh     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
5737d82f0f8SDina K Nimeh {
5747d82f0f8SDina K Nimeh 	crypto_data_t cdata;
5757d82f0f8SDina K Nimeh 	crypto_data_t wdata;
5767d82f0f8SDina K Nimeh 	int ret;
5777d82f0f8SDina K Nimeh 	longlong_t lblkno = bp->b_lblkno;
5787d82f0f8SDina K Nimeh 
5797d82f0f8SDina K Nimeh 	mutex_enter(&lsp->ls_crypto_lock);
5807d82f0f8SDina K Nimeh 
5817d82f0f8SDina K Nimeh 	/*
5827d82f0f8SDina K Nimeh 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
5837d82f0f8SDina K Nimeh 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
5847d82f0f8SDina K Nimeh 	 */
5857d82f0f8SDina K Nimeh 	SETUP_C_DATA(cdata, plaintext, len);
5867d82f0f8SDina K Nimeh 	cdata.cd_length = DEV_BSIZE;
5877d82f0f8SDina K Nimeh 	if (ciphertext != NULL) {		/* not in-place crypto */
5887d82f0f8SDina K Nimeh 		SETUP_C_DATA(wdata, ciphertext, len);
5897d82f0f8SDina K Nimeh 		wdata.cd_length = DEV_BSIZE;
5907d82f0f8SDina K Nimeh 	}
5917d82f0f8SDina K Nimeh 
5927d82f0f8SDina K Nimeh 	do {
5937d82f0f8SDina K Nimeh 		ret = lofi_blk_mech(lsp, lblkno);
5947d82f0f8SDina K Nimeh 		if (ret != CRYPTO_SUCCESS)
5957d82f0f8SDina K Nimeh 			continue;
5967d82f0f8SDina K Nimeh 
5977d82f0f8SDina K Nimeh 		if (op_encrypt) {
5987d82f0f8SDina K Nimeh 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
5997d82f0f8SDina K Nimeh 			    &lsp->ls_key, NULL,
6007d82f0f8SDina K Nimeh 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6017d82f0f8SDina K Nimeh 		} else {
6027d82f0f8SDina K Nimeh 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
6037d82f0f8SDina K Nimeh 			    &lsp->ls_key, NULL,
6047d82f0f8SDina K Nimeh 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6057d82f0f8SDina K Nimeh 		}
6067d82f0f8SDina K Nimeh 
6077d82f0f8SDina K Nimeh 		cdata.cd_offset += DEV_BSIZE;
6087d82f0f8SDina K Nimeh 		if (ciphertext != NULL)
6097d82f0f8SDina K Nimeh 			wdata.cd_offset += DEV_BSIZE;
6107d82f0f8SDina K Nimeh 		lblkno++;
6117d82f0f8SDina K Nimeh 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
6127d82f0f8SDina K Nimeh 
6137d82f0f8SDina K Nimeh 	mutex_exit(&lsp->ls_crypto_lock);
6147d82f0f8SDina K Nimeh 
6157d82f0f8SDina K Nimeh 	if (ret != CRYPTO_SUCCESS) {
6167d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
6177d82f0f8SDina K Nimeh 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
6187d82f0f8SDina K Nimeh 		    lblkno, ret);
6197d82f0f8SDina K Nimeh 	}
6207d82f0f8SDina K Nimeh 
6217d82f0f8SDina K Nimeh 	return (ret);
6227d82f0f8SDina K Nimeh }
6237d82f0f8SDina K Nimeh 
6247d82f0f8SDina K Nimeh #define	RDWR_RAW	1
6257d82f0f8SDina K Nimeh #define	RDWR_BCOPY	2
6267d82f0f8SDina K Nimeh 
6277d82f0f8SDina K Nimeh static int
6287d82f0f8SDina K Nimeh lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6297d82f0f8SDina K Nimeh     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
6307d82f0f8SDina K Nimeh {
6317d82f0f8SDina K Nimeh 	ssize_t resid;
6327d82f0f8SDina K Nimeh 	int isread;
6337d82f0f8SDina K Nimeh 	int error;
6347d82f0f8SDina K Nimeh 
6357d82f0f8SDina K Nimeh 	/*
6367d82f0f8SDina K Nimeh 	 * Handles reads/writes for both plain and encrypted lofi
6377d82f0f8SDina K Nimeh 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
6387d82f0f8SDina K Nimeh 	 * when it gets here.
6397d82f0f8SDina K Nimeh 	 */
6407d82f0f8SDina K Nimeh 
6417d82f0f8SDina K Nimeh 	isread = bp->b_flags & B_READ;
6427d82f0f8SDina K Nimeh 	if (isread) {
6437d82f0f8SDina K Nimeh 		if (method == RDWR_BCOPY) {
6447d82f0f8SDina K Nimeh 			/* DO NOT update bp->b_resid for bcopy */
6457d82f0f8SDina K Nimeh 			bcopy(bcopy_locn, bufaddr, len);
6467d82f0f8SDina K Nimeh 			error = 0;
6477d82f0f8SDina K Nimeh 		} else {		/* RDWR_RAW */
6487d82f0f8SDina K Nimeh 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
6497d82f0f8SDina K Nimeh 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6507d82f0f8SDina K Nimeh 			    &resid);
6517d82f0f8SDina K Nimeh 			bp->b_resid = resid;
6527d82f0f8SDina K Nimeh 		}
6537d82f0f8SDina K Nimeh 		if (lsp->ls_crypto_enabled && error == 0) {
6547d82f0f8SDina K Nimeh 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
6557d82f0f8SDina K Nimeh 			    B_FALSE) != CRYPTO_SUCCESS) {
6567d82f0f8SDina K Nimeh 				/*
6577d82f0f8SDina K Nimeh 				 * XXX: original code didn't set residual
6587d82f0f8SDina K Nimeh 				 * back to len because no error was expected
6597d82f0f8SDina K Nimeh 				 * from bcopy() if encryption is not enabled
6607d82f0f8SDina K Nimeh 				 */
6617d82f0f8SDina K Nimeh 				if (method != RDWR_BCOPY)
6627d82f0f8SDina K Nimeh 					bp->b_resid = len;
6637d82f0f8SDina K Nimeh 				error = EIO;
6647d82f0f8SDina K Nimeh 			}
6657d82f0f8SDina K Nimeh 		}
6667d82f0f8SDina K Nimeh 		return (error);
6677d82f0f8SDina K Nimeh 	} else {
6687d82f0f8SDina K Nimeh 		void *iobuf = bufaddr;
6697d82f0f8SDina K Nimeh 
6707d82f0f8SDina K Nimeh 		if (lsp->ls_crypto_enabled) {
6717d82f0f8SDina K Nimeh 			/* don't do in-place crypto to keep bufaddr intact */
6727d82f0f8SDina K Nimeh 			iobuf = kmem_alloc(len, KM_SLEEP);
6737d82f0f8SDina K Nimeh 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
6747d82f0f8SDina K Nimeh 			    B_TRUE) != CRYPTO_SUCCESS) {
6757d82f0f8SDina K Nimeh 				kmem_free(iobuf, len);
6767d82f0f8SDina K Nimeh 				if (method != RDWR_BCOPY)
6777d82f0f8SDina K Nimeh 					bp->b_resid = len;
6787d82f0f8SDina K Nimeh 				return (EIO);
6797d82f0f8SDina K Nimeh 			}
6807d82f0f8SDina K Nimeh 		}
6817d82f0f8SDina K Nimeh 		if (method == RDWR_BCOPY) {
6827d82f0f8SDina K Nimeh 			/* DO NOT update bp->b_resid for bcopy */
6837d82f0f8SDina K Nimeh 			bcopy(iobuf, bcopy_locn, len);
6847d82f0f8SDina K Nimeh 			error = 0;
6857d82f0f8SDina K Nimeh 		} else {		/* RDWR_RAW */
6867d82f0f8SDina K Nimeh 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
6877d82f0f8SDina K Nimeh 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6887d82f0f8SDina K Nimeh 			    &resid);
6897d82f0f8SDina K Nimeh 			bp->b_resid = resid;
6907d82f0f8SDina K Nimeh 		}
6917d82f0f8SDina K Nimeh 		if (lsp->ls_crypto_enabled) {
6927d82f0f8SDina K Nimeh 			kmem_free(iobuf, len);
6937d82f0f8SDina K Nimeh 		}
6947d82f0f8SDina K Nimeh 		return (error);
6957d82f0f8SDina K Nimeh 	}
6967d82f0f8SDina K Nimeh }
6977d82f0f8SDina K Nimeh 
69887117650Saalok static int
69987117650Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
70087117650Saalok     struct lofi_state *lsp)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	int error;
70387117650Saalok 	offset_t alignedoffset, mapoffset;
7047c478bd9Sstevel@tonic-gate 	size_t	xfersize;
7057c478bd9Sstevel@tonic-gate 	int	isread;
7067c478bd9Sstevel@tonic-gate 	int	smflags;
70787117650Saalok 	caddr_t	mapaddr;
70887117650Saalok 	size_t	len;
7097c478bd9Sstevel@tonic-gate 	enum seg_rw srw;
7107d82f0f8SDina K Nimeh 	int	save_error;
7117d82f0f8SDina K Nimeh 
7127d82f0f8SDina K Nimeh 	/*
7137d82f0f8SDina K Nimeh 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
7147d82f0f8SDina K Nimeh 	 * when it gets here.
7157d82f0f8SDina K Nimeh 	 */
7167d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled)
7177d82f0f8SDina K Nimeh 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/*
7207c478bd9Sstevel@tonic-gate 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
7217c478bd9Sstevel@tonic-gate 	 * an 8K boundary, but the buf transfer address may not be
72287117650Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
72387117650Saalok 	 * that even though we could). This matters since the initial
72487117650Saalok 	 * part of the transfer may not start at offset 0 within the
72587117650Saalok 	 * segmap'd chunk. So we have to compensate for that with
72687117650Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
72787117650Saalok 	 * beginning, and the last is capped by b_resid
7287d82f0f8SDina K Nimeh 	 *
7297d82f0f8SDina K Nimeh 	 * Visually, where "|" represents page map boundaries:
7307d82f0f8SDina K Nimeh 	 *   alignedoffset (mapaddr begins at this segmap boundary)
7317d82f0f8SDina K Nimeh 	 *    |   offset (from beginning of file)
7327d82f0f8SDina K Nimeh 	 *    |    |	   len
7337d82f0f8SDina K Nimeh 	 *    v    v	    v
7347d82f0f8SDina K Nimeh 	 * ===|====X========|====...======|========X====|====
7357d82f0f8SDina K Nimeh 	 *	   /-------------...---------------/
7367d82f0f8SDina K Nimeh 	 *		^ bp->b_bcount/bp->b_resid at start
7377d82f0f8SDina K Nimeh 	 *    /----/--------/----...------/--------/
7387d82f0f8SDina K Nimeh 	 *	^	^	^   ^		^
7397d82f0f8SDina K Nimeh 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
7407d82f0f8SDina K Nimeh 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
7417d82f0f8SDina K Nimeh 	 *	|	1st xfersize (<= MAXBSIZE)
7427d82f0f8SDina K Nimeh 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
7437d82f0f8SDina K Nimeh 	 *
7447d82f0f8SDina K Nimeh 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
7457d82f0f8SDina K Nimeh 	 * MAXBSIZE boundary.  "len" is next page boundary of size
746a363f650SDina K Nimeh 	 * PAGESIZE after "alignedoffset".
7477c478bd9Sstevel@tonic-gate 	 */
7487c478bd9Sstevel@tonic-gate 	mapoffset = offset & MAXBOFFSET;
74987117650Saalok 	alignedoffset = offset - mapoffset;
7507c478bd9Sstevel@tonic-gate 	bp->b_resid = bp->b_bcount;
7517c478bd9Sstevel@tonic-gate 	isread = bp->b_flags & B_READ;
7527c478bd9Sstevel@tonic-gate 	srw = isread ? S_READ : S_WRITE;
7537c478bd9Sstevel@tonic-gate 	do {
75487117650Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
7557c478bd9Sstevel@tonic-gate 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
756a363f650SDina K Nimeh 		len = roundup(mapoffset + xfersize, PAGESIZE);
7577c478bd9Sstevel@tonic-gate 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
7587c478bd9Sstevel@tonic-gate 		    alignedoffset, MAXBSIZE, 1, srw);
7597c478bd9Sstevel@tonic-gate 		/*
7607c478bd9Sstevel@tonic-gate 		 * Now fault in the pages. This lets us check
7617c478bd9Sstevel@tonic-gate 		 * for errors before we reference mapaddr and
7627c478bd9Sstevel@tonic-gate 		 * try to resolve the fault in bcopy (which would
7637c478bd9Sstevel@tonic-gate 		 * panic instead). And this can easily happen,
7647c478bd9Sstevel@tonic-gate 		 * particularly if you've lofi'd a file over NFS
7657c478bd9Sstevel@tonic-gate 		 * and someone deletes the file on the server.
7667c478bd9Sstevel@tonic-gate 		 */
7677c478bd9Sstevel@tonic-gate 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
7687c478bd9Sstevel@tonic-gate 		    len, F_SOFTLOCK, srw);
7697c478bd9Sstevel@tonic-gate 		if (error) {
7707c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, mapaddr, 0);
7717c478bd9Sstevel@tonic-gate 			if (FC_CODE(error) == FC_OBJERR)
7727c478bd9Sstevel@tonic-gate 				error = FC_ERRNO(error);
7737c478bd9Sstevel@tonic-gate 			else
7747c478bd9Sstevel@tonic-gate 				error = EIO;
7757c478bd9Sstevel@tonic-gate 			break;
7767c478bd9Sstevel@tonic-gate 		}
7777d82f0f8SDina K Nimeh 		/* error may be non-zero for encrypted lofi */
7787d82f0f8SDina K Nimeh 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
7797d82f0f8SDina K Nimeh 		    RDWR_BCOPY, mapaddr + mapoffset);
7807d82f0f8SDina K Nimeh 		if (error == 0) {
7817d82f0f8SDina K Nimeh 			bp->b_resid -= xfersize;
7827d82f0f8SDina K Nimeh 			bufaddr += xfersize;
7837d82f0f8SDina K Nimeh 			offset += xfersize;
7847d82f0f8SDina K Nimeh 		}
7857c478bd9Sstevel@tonic-gate 		smflags = 0;
7867c478bd9Sstevel@tonic-gate 		if (isread) {
78787117650Saalok 			smflags |= SM_FREE;
78887117650Saalok 			/*
78987117650Saalok 			 * If we're reading an entire page starting
79087117650Saalok 			 * at a page boundary, there's a good chance
79187117650Saalok 			 * we won't need it again. Put it on the
79287117650Saalok 			 * head of the freelist.
79387117650Saalok 			 */
794bbd65dd2SDina K Nimeh 			if (mapoffset == 0 && xfersize == MAXBSIZE)
79587117650Saalok 				smflags |= SM_DONTNEED;
7967c478bd9Sstevel@tonic-gate 		} else {
7977d82f0f8SDina K Nimeh 			if (error == 0)		/* write back good pages */
7987c478bd9Sstevel@tonic-gate 				smflags |= SM_WRITE;
7997c478bd9Sstevel@tonic-gate 		}
8007c478bd9Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
8017c478bd9Sstevel@tonic-gate 		    len, F_SOFTUNLOCK, srw);
8027d82f0f8SDina K Nimeh 		save_error = segmap_release(segkmap, mapaddr, smflags);
8037d82f0f8SDina K Nimeh 		if (error == 0)
8047d82f0f8SDina K Nimeh 			error = save_error;
8057c478bd9Sstevel@tonic-gate 		/* only the first map may start partial */
8067c478bd9Sstevel@tonic-gate 		mapoffset = 0;
8077c478bd9Sstevel@tonic-gate 		alignedoffset += MAXBSIZE;
8087c478bd9Sstevel@tonic-gate 	} while ((error == 0) && (bp->b_resid > 0) &&
80987117650Saalok 	    (offset < lsp->ls_vp_comp_size));
81087117650Saalok 
81187117650Saalok 	return (error);
81287117650Saalok }
81387117650Saalok 
8144058a205Sjrgn.keil@googlemail.com /*
8154058a205Sjrgn.keil@googlemail.com  * Check if segment seg_index is present in the decompressed segment
8164058a205Sjrgn.keil@googlemail.com  * data cache.
8174058a205Sjrgn.keil@googlemail.com  *
8184058a205Sjrgn.keil@googlemail.com  * Returns a pointer to the decompressed segment data cache entry if
8194058a205Sjrgn.keil@googlemail.com  * found, and NULL when decompressed data for this segment is not yet
8204058a205Sjrgn.keil@googlemail.com  * cached.
8214058a205Sjrgn.keil@googlemail.com  */
8224058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8234058a205Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
8244058a205Sjrgn.keil@googlemail.com {
8254058a205Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8264058a205Sjrgn.keil@googlemail.com 
8274058a205Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8284058a205Sjrgn.keil@googlemail.com 
8294058a205Sjrgn.keil@googlemail.com 	for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
8304058a205Sjrgn.keil@googlemail.com 	    lc = list_next(&lsp->ls_comp_cache, lc)) {
8314058a205Sjrgn.keil@googlemail.com 		if (lc->lc_index == seg_index) {
8324058a205Sjrgn.keil@googlemail.com 			/*
8334058a205Sjrgn.keil@googlemail.com 			 * Decompressed segment data was found in the
8344058a205Sjrgn.keil@googlemail.com 			 * cache.
8354058a205Sjrgn.keil@googlemail.com 			 *
8364058a205Sjrgn.keil@googlemail.com 			 * The cache uses an LRU replacement strategy;
8374058a205Sjrgn.keil@googlemail.com 			 * move the entry to head of list.
8384058a205Sjrgn.keil@googlemail.com 			 */
8394058a205Sjrgn.keil@googlemail.com 			list_remove(&lsp->ls_comp_cache, lc);
8404058a205Sjrgn.keil@googlemail.com 			list_insert_head(&lsp->ls_comp_cache, lc);
8414058a205Sjrgn.keil@googlemail.com 			return (lc);
8424058a205Sjrgn.keil@googlemail.com 		}
8434058a205Sjrgn.keil@googlemail.com 	}
8444058a205Sjrgn.keil@googlemail.com 	return (NULL);
8454058a205Sjrgn.keil@googlemail.com }
8464058a205Sjrgn.keil@googlemail.com 
8474058a205Sjrgn.keil@googlemail.com /*
8484058a205Sjrgn.keil@googlemail.com  * Add the data for a decompressed segment at segment index
8494058a205Sjrgn.keil@googlemail.com  * seg_index to the cache of the decompressed segments.
8504058a205Sjrgn.keil@googlemail.com  *
8514058a205Sjrgn.keil@googlemail.com  * Returns a pointer to the cache element structure in case
8524058a205Sjrgn.keil@googlemail.com  * the data was added to the cache; returns NULL when the data
8534058a205Sjrgn.keil@googlemail.com  * wasn't cached.
8544058a205Sjrgn.keil@googlemail.com  */
8554058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8564058a205Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
8574058a205Sjrgn.keil@googlemail.com     uchar_t *data)
8584058a205Sjrgn.keil@googlemail.com {
8594058a205Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8604058a205Sjrgn.keil@googlemail.com 
8614058a205Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8624058a205Sjrgn.keil@googlemail.com 
8634058a205Sjrgn.keil@googlemail.com 	while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
8644058a205Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8654058a205Sjrgn.keil@googlemail.com 		ASSERT(lc != NULL);
8664058a205Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
8674058a205Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
8684058a205Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
8694058a205Sjrgn.keil@googlemail.com 	}
8704058a205Sjrgn.keil@googlemail.com 
8714058a205Sjrgn.keil@googlemail.com 	/*
8724058a205Sjrgn.keil@googlemail.com 	 * Do not cache when disabled by tunable variable
8734058a205Sjrgn.keil@googlemail.com 	 */
8744058a205Sjrgn.keil@googlemail.com 	if (lofi_max_comp_cache == 0)
8754058a205Sjrgn.keil@googlemail.com 		return (NULL);
8764058a205Sjrgn.keil@googlemail.com 
8774058a205Sjrgn.keil@googlemail.com 	/*
8784058a205Sjrgn.keil@googlemail.com 	 * When the cache has not yet reached the maximum allowed
8794058a205Sjrgn.keil@googlemail.com 	 * number of segments, allocate a new cache element.
8804058a205Sjrgn.keil@googlemail.com 	 * Otherwise the cache is full; reuse the last list element
8814058a205Sjrgn.keil@googlemail.com 	 * (LRU) for caching the decompressed segment data.
8824058a205Sjrgn.keil@googlemail.com 	 *
8834058a205Sjrgn.keil@googlemail.com 	 * The cache element for the new decompressed segment data is
8844058a205Sjrgn.keil@googlemail.com 	 * added to the head of the list.
8854058a205Sjrgn.keil@googlemail.com 	 */
8864058a205Sjrgn.keil@googlemail.com 	if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
8874058a205Sjrgn.keil@googlemail.com 		lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
8884058a205Sjrgn.keil@googlemail.com 		lc->lc_data = NULL;
8894058a205Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
8904058a205Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count++;
8914058a205Sjrgn.keil@googlemail.com 	} else {
8924058a205Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8934058a205Sjrgn.keil@googlemail.com 		if (lc == NULL)
8944058a205Sjrgn.keil@googlemail.com 			return (NULL);
8954058a205Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
8964058a205Sjrgn.keil@googlemail.com 	}
8974058a205Sjrgn.keil@googlemail.com 
8984058a205Sjrgn.keil@googlemail.com 	/*
8994058a205Sjrgn.keil@googlemail.com 	 * Free old uncompressed segment data when reusing a cache
9004058a205Sjrgn.keil@googlemail.com 	 * entry.
9014058a205Sjrgn.keil@googlemail.com 	 */
9024058a205Sjrgn.keil@googlemail.com 	if (lc->lc_data != NULL)
9034058a205Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
9044058a205Sjrgn.keil@googlemail.com 
9054058a205Sjrgn.keil@googlemail.com 	lc->lc_data = data;
9064058a205Sjrgn.keil@googlemail.com 	lc->lc_index = seg_index;
9074058a205Sjrgn.keil@googlemail.com 	return (lc);
9084058a205Sjrgn.keil@googlemail.com }
9094058a205Sjrgn.keil@googlemail.com 
9104058a205Sjrgn.keil@googlemail.com 
91187117650Saalok /*ARGSUSED*/
912b1efbcd6SAlok Aggarwal static int
913b1efbcd6SAlok Aggarwal gzip_decompress(void *src, size_t srclen, void *dst,
91487117650Saalok     size_t *dstlen, int level)
91587117650Saalok {
91687117650Saalok 	ASSERT(*dstlen >= srclen);
91787117650Saalok 
91887117650Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
91987117650Saalok 		return (-1);
92087117650Saalok 	return (0);
92187117650Saalok }
92287117650Saalok 
923b1efbcd6SAlok Aggarwal #define	LZMA_HEADER_SIZE	(LZMA_PROPS_SIZE + 8)
924b1efbcd6SAlok Aggarwal /*ARGSUSED*/
925b1efbcd6SAlok Aggarwal static int
926b1efbcd6SAlok Aggarwal lzma_decompress(void *src, size_t srclen, void *dst,
927b1efbcd6SAlok Aggarwal 	size_t *dstlen, int level)
928b1efbcd6SAlok Aggarwal {
929b1efbcd6SAlok Aggarwal 	size_t insizepure;
930b1efbcd6SAlok Aggarwal 	void *actual_src;
931b1efbcd6SAlok Aggarwal 	ELzmaStatus status;
932b1efbcd6SAlok Aggarwal 
933b1efbcd6SAlok Aggarwal 	insizepure = srclen - LZMA_HEADER_SIZE;
934b1efbcd6SAlok Aggarwal 	actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
935b1efbcd6SAlok Aggarwal 
936b1efbcd6SAlok Aggarwal 	if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
937b1efbcd6SAlok Aggarwal 	    (const Byte *)actual_src, &insizepure,
938b1efbcd6SAlok Aggarwal 	    (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
939b1efbcd6SAlok Aggarwal 	    &g_Alloc) != SZ_OK) {
940b1efbcd6SAlok Aggarwal 		return (-1);
941b1efbcd6SAlok Aggarwal 	}
942b1efbcd6SAlok Aggarwal 	return (0);
943b1efbcd6SAlok Aggarwal }
944b1efbcd6SAlok Aggarwal 
94587117650Saalok /*
94687117650Saalok  * This is basically what strategy used to be before we found we
94787117650Saalok  * needed task queues.
94887117650Saalok  */
94987117650Saalok static void
95087117650Saalok lofi_strategy_task(void *arg)
95187117650Saalok {
95287117650Saalok 	struct buf *bp = (struct buf *)arg;
95387117650Saalok 	int error;
95487117650Saalok 	struct lofi_state *lsp;
9557d82f0f8SDina K Nimeh 	offset_t offset;
95687117650Saalok 	caddr_t	bufaddr;
9577d82f0f8SDina K Nimeh 	size_t	len;
9587d82f0f8SDina K Nimeh 	size_t	xfersize;
9597d82f0f8SDina K Nimeh 	boolean_t bufinited = B_FALSE;
96087117650Saalok 
96187117650Saalok 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
9627d82f0f8SDina K Nimeh 	if (lsp == NULL) {
9637d82f0f8SDina K Nimeh 		error = ENXIO;
9647d82f0f8SDina K Nimeh 		goto errout;
9657d82f0f8SDina K Nimeh 	}
96687117650Saalok 	if (lsp->ls_kstat) {
96787117650Saalok 		mutex_enter(lsp->ls_kstat->ks_lock);
96887117650Saalok 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
96987117650Saalok 		mutex_exit(lsp->ls_kstat->ks_lock);
97087117650Saalok 	}
97187117650Saalok 	bp_mapin(bp);
97287117650Saalok 	bufaddr = bp->b_un.b_addr;
97387117650Saalok 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
9747d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
9757d82f0f8SDina K Nimeh 		/* encrypted data really begins after crypto header */
9767d82f0f8SDina K Nimeh 		offset += lsp->ls_crypto_offset;
9777d82f0f8SDina K Nimeh 	}
9787d82f0f8SDina K Nimeh 	len = bp->b_bcount;
9797d82f0f8SDina K Nimeh 	bufinited = B_TRUE;
9807d82f0f8SDina K Nimeh 
9817d82f0f8SDina K Nimeh 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
9827d82f0f8SDina K Nimeh 		error = EIO;
9837d82f0f8SDina K Nimeh 		goto errout;
9847d82f0f8SDina K Nimeh 	}
98587117650Saalok 
98687117650Saalok 	/*
98787117650Saalok 	 * We used to always use vn_rdwr here, but we cannot do that because
98887117650Saalok 	 * we might decide to read or write from the the underlying
98987117650Saalok 	 * file during this call, which would be a deadlock because
99087117650Saalok 	 * we have the rw_lock. So instead we page, unless it's not
9917d82f0f8SDina K Nimeh 	 * mapable or it's a character device or it's an encrypted lofi.
99287117650Saalok 	 */
9937d82f0f8SDina K Nimeh 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
9947d82f0f8SDina K Nimeh 	    lsp->ls_crypto_enabled) {
9957d82f0f8SDina K Nimeh 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
9967d82f0f8SDina K Nimeh 		    NULL);
9977d82f0f8SDina K Nimeh 	} else if (lsp->ls_uncomp_seg_sz == 0) {
99887117650Saalok 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
9997d82f0f8SDina K Nimeh 	} else {
10004058a205Sjrgn.keil@googlemail.com 		uchar_t *compressed_seg = NULL, *cmpbuf;
10014058a205Sjrgn.keil@googlemail.com 		uchar_t *uncompressed_seg = NULL;
10027d82f0f8SDina K Nimeh 		lofi_compress_info_t *li;
10037d82f0f8SDina K Nimeh 		size_t oblkcount;
10044058a205Sjrgn.keil@googlemail.com 		ulong_t seglen;
10057d82f0f8SDina K Nimeh 		uint64_t sblkno, eblkno, cmpbytes;
10064058a205Sjrgn.keil@googlemail.com 		uint64_t uncompressed_seg_index;
10074058a205Sjrgn.keil@googlemail.com 		struct lofi_comp_cache *lc;
10087d82f0f8SDina K Nimeh 		offset_t sblkoff, eblkoff;
10097d82f0f8SDina K Nimeh 		u_offset_t salign, ealign;
10107d82f0f8SDina K Nimeh 		u_offset_t sdiff;
10117d82f0f8SDina K Nimeh 		uint32_t comp_data_sz;
10127d82f0f8SDina K Nimeh 		uint64_t i;
101387117650Saalok 
101487117650Saalok 		/*
101587117650Saalok 		 * From here on we're dealing primarily with compressed files
101687117650Saalok 		 */
10177d82f0f8SDina K Nimeh 		ASSERT(!lsp->ls_crypto_enabled);
101887117650Saalok 
101987117650Saalok 		/*
102087117650Saalok 		 * Compressed files can only be read from and
102187117650Saalok 		 * not written to
102287117650Saalok 		 */
102387117650Saalok 		if (!(bp->b_flags & B_READ)) {
102487117650Saalok 			bp->b_resid = bp->b_bcount;
102587117650Saalok 			error = EROFS;
102687117650Saalok 			goto done;
102787117650Saalok 		}
102887117650Saalok 
102987117650Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
103087117650Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
103187117650Saalok 		/*
103287117650Saalok 		 * Compute starting and ending compressed segment numbers
103387117650Saalok 		 * We use only bitwise operations avoiding division and
103487117650Saalok 		 * modulus because we enforce the compression segment size
103587117650Saalok 		 * to a power of 2
103687117650Saalok 		 */
103787117650Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
103887117650Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
103987117650Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
104087117650Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
104187117650Saalok 
104287117650Saalok 		/*
10434058a205Sjrgn.keil@googlemail.com 		 * Check the decompressed segment cache.
10444058a205Sjrgn.keil@googlemail.com 		 *
10454058a205Sjrgn.keil@googlemail.com 		 * The cache is used only when the requested data
10464058a205Sjrgn.keil@googlemail.com 		 * is within a segment. Requests that cross
10474058a205Sjrgn.keil@googlemail.com 		 * segment boundaries bypass the cache.
10484058a205Sjrgn.keil@googlemail.com 		 */
10494058a205Sjrgn.keil@googlemail.com 		if (sblkno == eblkno ||
10504058a205Sjrgn.keil@googlemail.com 		    (sblkno + 1 == eblkno && eblkoff == 0)) {
10514058a205Sjrgn.keil@googlemail.com 			/*
10524058a205Sjrgn.keil@googlemail.com 			 * Request doesn't cross a segment boundary,
10534058a205Sjrgn.keil@googlemail.com 			 * now check the cache.
10544058a205Sjrgn.keil@googlemail.com 			 */
10554058a205Sjrgn.keil@googlemail.com 			mutex_enter(&lsp->ls_comp_cache_lock);
10564058a205Sjrgn.keil@googlemail.com 			lc = lofi_find_comp_data(lsp, sblkno);
10574058a205Sjrgn.keil@googlemail.com 			if (lc != NULL) {
10584058a205Sjrgn.keil@googlemail.com 				/*
10594058a205Sjrgn.keil@googlemail.com 				 * We've found the decompressed segment
10604058a205Sjrgn.keil@googlemail.com 				 * data in the cache; reuse it.
10614058a205Sjrgn.keil@googlemail.com 				 */
10624058a205Sjrgn.keil@googlemail.com 				bcopy(lc->lc_data + sblkoff, bufaddr,
10634058a205Sjrgn.keil@googlemail.com 				    bp->b_bcount);
10644058a205Sjrgn.keil@googlemail.com 				mutex_exit(&lsp->ls_comp_cache_lock);
10654058a205Sjrgn.keil@googlemail.com 				bp->b_resid = 0;
10664058a205Sjrgn.keil@googlemail.com 				error = 0;
10674058a205Sjrgn.keil@googlemail.com 				goto done;
10684058a205Sjrgn.keil@googlemail.com 			}
10694058a205Sjrgn.keil@googlemail.com 			mutex_exit(&lsp->ls_comp_cache_lock);
10704058a205Sjrgn.keil@googlemail.com 		}
10714058a205Sjrgn.keil@googlemail.com 
10724058a205Sjrgn.keil@googlemail.com 		/*
107387117650Saalok 		 * Align start offset to block boundary for segmap
107487117650Saalok 		 */
107587117650Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
107687117650Saalok 		sdiff = salign & (DEV_BSIZE - 1);
107787117650Saalok 		salign -= sdiff;
107887117650Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
107987117650Saalok 			/*
108087117650Saalok 			 * We're dealing with the last segment of
108187117650Saalok 			 * the compressed file -- the size of this
108287117650Saalok 			 * segment *may not* be the same as the
108387117650Saalok 			 * segment size for the file
108487117650Saalok 			 */
108587117650Saalok 			eblkoff = (offset + bp->b_bcount) &
108687117650Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
108787117650Saalok 			ealign = lsp->ls_vp_comp_size;
108887117650Saalok 		} else {
108987117650Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
109087117650Saalok 		}
109187117650Saalok 
109287117650Saalok 		/*
109387117650Saalok 		 * Preserve original request paramaters
109487117650Saalok 		 */
109587117650Saalok 		oblkcount = bp->b_bcount;
109687117650Saalok 
109787117650Saalok 		/*
109887117650Saalok 		 * Assign the calculated parameters
109987117650Saalok 		 */
110087117650Saalok 		comp_data_sz = ealign - salign;
110187117650Saalok 		bp->b_bcount = comp_data_sz;
110287117650Saalok 
110387117650Saalok 		/*
110487117650Saalok 		 * Allocate fixed size memory blocks to hold compressed
110587117650Saalok 		 * segments and one uncompressed segment since we
110687117650Saalok 		 * uncompress segments one at a time
110787117650Saalok 		 */
110887117650Saalok 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
110987117650Saalok 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
111087117650Saalok 		/*
111187117650Saalok 		 * Map in the calculated number of blocks
111287117650Saalok 		 */
111387117650Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
111487117650Saalok 		    bp, lsp);
111587117650Saalok 
111687117650Saalok 		bp->b_bcount = oblkcount;
111787117650Saalok 		bp->b_resid = oblkcount;
111887117650Saalok 		if (error != 0)
111987117650Saalok 			goto done;
112087117650Saalok 
112187117650Saalok 		/*
112287117650Saalok 		 * We have the compressed blocks, now uncompress them
112387117650Saalok 		 */
112487117650Saalok 		cmpbuf = compressed_seg + sdiff;
1125b1efbcd6SAlok Aggarwal 		for (i = sblkno; i <= eblkno; i++) {
1126b1efbcd6SAlok Aggarwal 			ASSERT(i < lsp->ls_comp_index_sz - 1);
1127b1efbcd6SAlok Aggarwal 
1128b1efbcd6SAlok Aggarwal 			/*
1129b1efbcd6SAlok Aggarwal 			 * The last segment is special in that it is
1130b1efbcd6SAlok Aggarwal 			 * most likely not going to be the same
1131b1efbcd6SAlok Aggarwal 			 * (uncompressed) size as the other segments.
1132b1efbcd6SAlok Aggarwal 			 */
1133b1efbcd6SAlok Aggarwal 			if (i == (lsp->ls_comp_index_sz - 2)) {
1134b1efbcd6SAlok Aggarwal 				seglen = lsp->ls_uncomp_last_seg_sz;
1135b1efbcd6SAlok Aggarwal 			} else {
1136b1efbcd6SAlok Aggarwal 				seglen = lsp->ls_uncomp_seg_sz;
1137b1efbcd6SAlok Aggarwal 			}
1138b1efbcd6SAlok Aggarwal 
113987117650Saalok 			/*
114087117650Saalok 			 * Each of the segment index entries contains
114187117650Saalok 			 * the starting block number for that segment.
114287117650Saalok 			 * The number of compressed bytes in a segment
114387117650Saalok 			 * is thus the difference between the starting
114487117650Saalok 			 * block number of this segment and the starting
114587117650Saalok 			 * block number of the next segment.
114687117650Saalok 			 */
114787117650Saalok 			cmpbytes = lsp->ls_comp_seg_index[i + 1] -
114887117650Saalok 			    lsp->ls_comp_seg_index[i];
114987117650Saalok 
115087117650Saalok 			/*
115187117650Saalok 			 * The first byte in a compressed segment is a flag
115287117650Saalok 			 * that indicates whether this segment is compressed
115387117650Saalok 			 * at all
115487117650Saalok 			 */
115587117650Saalok 			if (*cmpbuf == UNCOMPRESSED) {
115687117650Saalok 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
115787117650Saalok 				    (cmpbytes - SEGHDR));
115887117650Saalok 			} else {
115987117650Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
116087117650Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
116187117650Saalok 				    &seglen, li->l_level) != 0) {
116287117650Saalok 					error = EIO;
116387117650Saalok 					goto done;
116487117650Saalok 				}
116587117650Saalok 			}
116687117650Saalok 
11674058a205Sjrgn.keil@googlemail.com 			uncompressed_seg_index = i;
11684058a205Sjrgn.keil@googlemail.com 
116987117650Saalok 			/*
117087117650Saalok 			 * Determine how much uncompressed data we
117187117650Saalok 			 * have to copy and copy it
117287117650Saalok 			 */
117387117650Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
1174b1efbcd6SAlok Aggarwal 			if (i == eblkno)
1175b1efbcd6SAlok Aggarwal 				xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
117687117650Saalok 
117787117650Saalok 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
117887117650Saalok 
117987117650Saalok 			cmpbuf += cmpbytes;
118087117650Saalok 			bufaddr += xfersize;
118187117650Saalok 			bp->b_resid -= xfersize;
118287117650Saalok 			sblkoff = 0;
118387117650Saalok 
118487117650Saalok 			if (bp->b_resid == 0)
118587117650Saalok 				break;
118687117650Saalok 		}
11874058a205Sjrgn.keil@googlemail.com 
11884058a205Sjrgn.keil@googlemail.com 		/*
11894058a205Sjrgn.keil@googlemail.com 		 * Add the data for the last decopressed segment to
11904058a205Sjrgn.keil@googlemail.com 		 * the cache.
11914058a205Sjrgn.keil@googlemail.com 		 *
11924058a205Sjrgn.keil@googlemail.com 		 * In case the uncompressed segment data was added to (and
11934058a205Sjrgn.keil@googlemail.com 		 * is referenced by) the cache, make sure we don't free it
11944058a205Sjrgn.keil@googlemail.com 		 * here.
11954058a205Sjrgn.keil@googlemail.com 		 */
11964058a205Sjrgn.keil@googlemail.com 		mutex_enter(&lsp->ls_comp_cache_lock);
11974058a205Sjrgn.keil@googlemail.com 		if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
11984058a205Sjrgn.keil@googlemail.com 		    uncompressed_seg)) != NULL) {
11994058a205Sjrgn.keil@googlemail.com 			uncompressed_seg = NULL;
12004058a205Sjrgn.keil@googlemail.com 		}
12014058a205Sjrgn.keil@googlemail.com 		mutex_exit(&lsp->ls_comp_cache_lock);
12024058a205Sjrgn.keil@googlemail.com 
120387117650Saalok done:
120487117650Saalok 		if (compressed_seg != NULL)
120587117650Saalok 			kmem_free(compressed_seg, comp_data_sz);
120687117650Saalok 		if (uncompressed_seg != NULL)
120787117650Saalok 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
12087d82f0f8SDina K Nimeh 	} /* end of handling compressed files */
120987117650Saalok 
12107d82f0f8SDina K Nimeh errout:
12117d82f0f8SDina K Nimeh 	if (bufinited && lsp->ls_kstat) {
12127c478bd9Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
12137c478bd9Sstevel@tonic-gate 		kstat_io_t *kioptr;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
12167c478bd9Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
12177c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
12187c478bd9Sstevel@tonic-gate 			kioptr->nread += n_done;
12197c478bd9Sstevel@tonic-gate 			kioptr->reads++;
12207c478bd9Sstevel@tonic-gate 		} else {
12217c478bd9Sstevel@tonic-gate 			kioptr->nwritten += n_done;
12227c478bd9Sstevel@tonic-gate 			kioptr->writes++;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
12257c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
12267c478bd9Sstevel@tonic-gate 	}
12273d7072f8Seschrock 
12283d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
12293d7072f8Seschrock 	if (--lsp->ls_vp_iocount == 0)
12303d7072f8Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
12313d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
12323d7072f8Seschrock 
12337c478bd9Sstevel@tonic-gate 	bioerror(bp, error);
12347c478bd9Sstevel@tonic-gate 	biodone(bp);
12357c478bd9Sstevel@tonic-gate }
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate static int
12387c478bd9Sstevel@tonic-gate lofi_strategy(struct buf *bp)
12397c478bd9Sstevel@tonic-gate {
12407c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
12417c478bd9Sstevel@tonic-gate 	offset_t	offset;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
12457c478bd9Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
12467c478bd9Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
12477c478bd9Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
12487c478bd9Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
12497c478bd9Sstevel@tonic-gate 	 *
12507c478bd9Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
12517c478bd9Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
12527c478bd9Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
12537c478bd9Sstevel@tonic-gate 	 */
12547c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
12557d82f0f8SDina K Nimeh 	if (lsp == NULL) {
12567d82f0f8SDina K Nimeh 		bioerror(bp, ENXIO);
12577d82f0f8SDina K Nimeh 		biodone(bp);
12587d82f0f8SDina K Nimeh 		return (0);
12597d82f0f8SDina K Nimeh 	}
12607d82f0f8SDina K Nimeh 
12613d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
12623d7072f8Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
12633d7072f8Seschrock 		bioerror(bp, EIO);
12643d7072f8Seschrock 		biodone(bp);
12653d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12663d7072f8Seschrock 		return (0);
12673d7072f8Seschrock 	}
12683d7072f8Seschrock 
12697c478bd9Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
12707d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
12717d82f0f8SDina K Nimeh 		/* encrypted data really begins after crypto header */
12727d82f0f8SDina K Nimeh 		offset += lsp->ls_crypto_offset;
12737d82f0f8SDina K Nimeh 	}
12747c478bd9Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
12757c478bd9Sstevel@tonic-gate 		/* EOF */
12767c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
12777c478bd9Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
12787c478bd9Sstevel@tonic-gate 			bioerror(bp, 0);
12797c478bd9Sstevel@tonic-gate 		} else {
12807c478bd9Sstevel@tonic-gate 			/* writes should fail */
12817c478bd9Sstevel@tonic-gate 			bioerror(bp, ENXIO);
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate 		biodone(bp);
12843d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12857c478bd9Sstevel@tonic-gate 		return (0);
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
12887c478bd9Sstevel@tonic-gate 		bioerror(bp, ENXIO);
12897c478bd9Sstevel@tonic-gate 		biodone(bp);
12903d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12917c478bd9Sstevel@tonic-gate 		return (0);
12927c478bd9Sstevel@tonic-gate 	}
12933d7072f8Seschrock 	lsp->ls_vp_iocount++;
12943d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
12953d7072f8Seschrock 
12967c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
12977c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
12987c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
12997c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
13027c478bd9Sstevel@tonic-gate 	return (0);
13037c478bd9Sstevel@tonic-gate }
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
13067c478bd9Sstevel@tonic-gate static int
13077c478bd9Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
13107c478bd9Sstevel@tonic-gate 		return (EINVAL);
13117d82f0f8SDina K Nimeh 	UIO_CHECK(uio);
13127c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
13167c478bd9Sstevel@tonic-gate static int
13177c478bd9Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
13207c478bd9Sstevel@tonic-gate 		return (EINVAL);
13217d82f0f8SDina K Nimeh 	UIO_CHECK(uio);
13227c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
13237c478bd9Sstevel@tonic-gate }
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
13267c478bd9Sstevel@tonic-gate static int
13277c478bd9Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
13307c478bd9Sstevel@tonic-gate 		return (EINVAL);
13317d82f0f8SDina K Nimeh 	UIO_CHECK(aio->aio_uio);
13327c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
13337c478bd9Sstevel@tonic-gate }
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
13367c478bd9Sstevel@tonic-gate static int
13377c478bd9Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
13387c478bd9Sstevel@tonic-gate {
13397c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
13407c478bd9Sstevel@tonic-gate 		return (EINVAL);
13417d82f0f8SDina K Nimeh 	UIO_CHECK(aio->aio_uio);
13427c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13467c478bd9Sstevel@tonic-gate static int
13477c478bd9Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate 	switch (infocmd) {
13507c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
13517c478bd9Sstevel@tonic-gate 		*result = lofi_dip;
13527c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13537c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
13547c478bd9Sstevel@tonic-gate 		*result = 0;
13557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate static int
13617c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	int	error;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
13667c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13677c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
13687c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
13697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
13727c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
13737c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
13747c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
13757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13767c478bd9Sstevel@tonic-gate 	}
1377843e1988Sjohnlev 	/* driver handles kernel-issued IOCTLs */
1378843e1988Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1379843e1988Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1380843e1988Sjohnlev 		ddi_remove_minor_node(dip, NULL);
1381843e1988Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
1382843e1988Sjohnlev 		return (DDI_FAILURE);
1383843e1988Sjohnlev 	}
13847c478bd9Sstevel@tonic-gate 	lofi_dip = dip;
13857c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
13867c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate static int
13907c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
13917c478bd9Sstevel@tonic-gate {
13927c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
13937c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13947c478bd9Sstevel@tonic-gate 	if (lofi_busy())
13957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13967c478bd9Sstevel@tonic-gate 	lofi_dip = NULL;
13977c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
1398843e1988Sjohnlev 	ddi_prop_remove_all(dip);
13997c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
14007c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate /*
14047d82f0f8SDina K Nimeh  * With addition of encryption, be careful that encryption key is wiped before
14057d82f0f8SDina K Nimeh  * kernel memory structures are freed, and also that key is not accidentally
14067d82f0f8SDina K Nimeh  * passed out into userland structures.
14077d82f0f8SDina K Nimeh  */
14087d82f0f8SDina K Nimeh static void
14097d82f0f8SDina K Nimeh free_lofi_ioctl(struct lofi_ioctl *klip)
14107d82f0f8SDina K Nimeh {
14117d82f0f8SDina K Nimeh 	/* Make sure this encryption key doesn't stick around */
14127d82f0f8SDina K Nimeh 	bzero(klip->li_key, sizeof (klip->li_key));
14137d82f0f8SDina K Nimeh 	kmem_free(klip, sizeof (struct lofi_ioctl));
14147d82f0f8SDina K Nimeh }
14157d82f0f8SDina K Nimeh 
14167d82f0f8SDina K Nimeh /*
14177c478bd9Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
14187c478bd9Sstevel@tonic-gate  * the lofi_ioctl structure.
14197c478bd9Sstevel@tonic-gate  */
14207c478bd9Sstevel@tonic-gate struct lofi_ioctl *
1421bd07e074Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
14227c478bd9Sstevel@tonic-gate {
14237c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
14247c478bd9Sstevel@tonic-gate 	int	error;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
1427bd07e074Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
14287c478bd9Sstevel@tonic-gate 	if (error) {
14297d82f0f8SDina K Nimeh 		free_lofi_ioctl(klip);
14307c478bd9Sstevel@tonic-gate 		return (NULL);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
14346f02aa44SDina K Nimeh 	klip->li_filename[MAXPATHLEN-1] = '\0';
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	/* validate minor number */
14377c478bd9Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
14387d82f0f8SDina K Nimeh 		free_lofi_ioctl(klip);
14397d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
14407d82f0f8SDina K Nimeh 		    lofi_max_files);
14417c478bd9Sstevel@tonic-gate 		return (NULL);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	return (klip);
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate int
1447bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
1448bd07e074Sheppo 	int flag)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	int	error;
14517c478bd9Sstevel@tonic-gate 
14527d82f0f8SDina K Nimeh 	/*
14537d82f0f8SDina K Nimeh 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
14547d82f0f8SDina K Nimeh 	 * This ensures that an attacker can't trivially find the
14557d82f0f8SDina K Nimeh 	 * key for a mapping just by issuing the ioctl.
14567d82f0f8SDina K Nimeh 	 *
14577d82f0f8SDina K Nimeh 	 * It can still be found by poking around in kmem with mdb(1),
14587d82f0f8SDina K Nimeh 	 * but there is no point in making it easy when the info isn't
14597d82f0f8SDina K Nimeh 	 * of any use in this direction anyway.
14607d82f0f8SDina K Nimeh 	 *
14617d82f0f8SDina K Nimeh 	 * Either way we don't actually have the raw key stored in
14627d82f0f8SDina K Nimeh 	 * a form that we can get it anyway, since we just used it
14637d82f0f8SDina K Nimeh 	 * to create a ctx template and didn't keep "the original".
14647d82f0f8SDina K Nimeh 	 */
1465bd07e074Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
14667c478bd9Sstevel@tonic-gate 	if (error)
14677c478bd9Sstevel@tonic-gate 		return (EFAULT);
14687c478bd9Sstevel@tonic-gate 	return (0);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
14737c478bd9Sstevel@tonic-gate  */
14747c478bd9Sstevel@tonic-gate static int
14757c478bd9Sstevel@tonic-gate file_to_minor(char *filename)
14767c478bd9Sstevel@tonic-gate {
14777c478bd9Sstevel@tonic-gate 	minor_t	minor;
14787c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
14817c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
14827c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
14837c478bd9Sstevel@tonic-gate 		if (lsp == NULL)
14847c478bd9Sstevel@tonic-gate 			continue;
14857c478bd9Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
14867c478bd9Sstevel@tonic-gate 			return (minor);
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate 	return (0);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
14937c478bd9Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
14947c478bd9Sstevel@tonic-gate  */
14957c478bd9Sstevel@tonic-gate static int
14967c478bd9Sstevel@tonic-gate valid_filename(const char *filename)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
14997c478bd9Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/* must be absolute path */
15027c478bd9Sstevel@tonic-gate 	if (filename[0] != '/')
15037c478bd9Sstevel@tonic-gate 		return (0);
15047c478bd9Sstevel@tonic-gate 	/* must not be lofi */
15057c478bd9Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
15067c478bd9Sstevel@tonic-gate 		return (0);
15077c478bd9Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
15087c478bd9Sstevel@tonic-gate 		return (0);
15097c478bd9Sstevel@tonic-gate 	return (1);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate /*
15137c478bd9Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
15147c478bd9Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
15157c478bd9Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
15167c478bd9Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
151763360950Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
15187c478bd9Sstevel@tonic-gate  * have to support it.
15197c478bd9Sstevel@tonic-gate  */
15207c478bd9Sstevel@tonic-gate static void
15217c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
15227c478bd9Sstevel@tonic-gate {
15237d82f0f8SDina K Nimeh 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
15247d82f0f8SDina K Nimeh 
15257c478bd9Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
15267c478bd9Sstevel@tonic-gate 	/*
15277c478bd9Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
15287c478bd9Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
15297c478bd9Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
15307c478bd9Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
15317c478bd9Sstevel@tonic-gate 	 * a number between one and one).
15327c478bd9Sstevel@tonic-gate 	 *
15337c478bd9Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
15347c478bd9Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
15357c478bd9Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
15367c478bd9Sstevel@tonic-gate 	 */
15377d82f0f8SDina K Nimeh 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
15387d82f0f8SDina K Nimeh 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
15397c478bd9Sstevel@tonic-gate 	else
15407d82f0f8SDina K Nimeh 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
15417c478bd9Sstevel@tonic-gate 	/* in case file file is < 100k */
15427c478bd9Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
15437c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
15447c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
15457c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
15467c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
15477c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
15487c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
15497c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
15507c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
15517c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
15527c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
15537c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
15547d82f0f8SDina K Nimeh 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
15557c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
15567c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
15597c478bd9Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
15607c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
15617c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
1562947caa0eSDina K Nimeh 	(void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME,
1563947caa0eSDina K Nimeh 	    sizeof (lsp->ls_vtoc.v_volume));
15647c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
15657c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
15667c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
156787117650Saalok 
156887117650Saalok 	/*
156987117650Saalok 	 * A compressed file is read-only, other files can
157087117650Saalok 	 * be read-write
157187117650Saalok 	 */
157287117650Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
157387117650Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
157487117650Saalok 	} else {
15757c478bd9Sstevel@tonic-gate 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
157687117650Saalok 	}
15777c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
15787c478bd9Sstevel@tonic-gate 	/*
15797c478bd9Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
15807c478bd9Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
15817c478bd9Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
15827c478bd9Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
15837c478bd9Sstevel@tonic-gate 	 */
15847c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
15857c478bd9Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
15887c478bd9Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
15897c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
15907c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
15917c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
15927c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
15937c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
15947c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
15957c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
15967c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
15977c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
15987c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
15997c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
16007c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
16017c478bd9Sstevel@tonic-gate 	/*
16027c478bd9Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
16037c478bd9Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
16047c478bd9Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
16057c478bd9Sstevel@tonic-gate 	 * maxcontig is 0.
16067c478bd9Sstevel@tonic-gate 	 */
16077c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate /*
161187117650Saalok  * map in a compressed file
161287117650Saalok  *
161387117650Saalok  * Read in the header and the index that follows.
161487117650Saalok  *
161587117650Saalok  * The header is as follows -
161687117650Saalok  *
161787117650Saalok  * Signature (name of the compression algorithm)
161887117650Saalok  * Compression segment size (a multiple of 512)
161987117650Saalok  * Number of index entries
162087117650Saalok  * Size of the last block
162187117650Saalok  * The array containing the index entries
162287117650Saalok  *
162387117650Saalok  * The header information is always stored in
162487117650Saalok  * network byte order on disk.
162587117650Saalok  */
162687117650Saalok static int
162787117650Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
162887117650Saalok {
162987117650Saalok 	uint32_t index_sz, header_len, i;
163087117650Saalok 	ssize_t	resid;
163187117650Saalok 	enum uio_rw rw;
163287117650Saalok 	char *tbuf = buf;
163387117650Saalok 	int error;
163487117650Saalok 
163587117650Saalok 	/* The signature has already been read */
163687117650Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
163787117650Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
163887117650Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
163987117650Saalok 
164087117650Saalok 	/*
164187117650Saalok 	 * The compressed segment size must be a power of 2
164287117650Saalok 	 */
16434058a205Sjrgn.keil@googlemail.com 	if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
16444058a205Sjrgn.keil@googlemail.com 	    !ISP2(lsp->ls_uncomp_seg_sz))
164587117650Saalok 		return (EINVAL);
164687117650Saalok 
164787117650Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
164887117650Saalok 		;
164987117650Saalok 
165087117650Saalok 	lsp->ls_comp_seg_shift = i;
165187117650Saalok 
165287117650Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
165387117650Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
165487117650Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
165587117650Saalok 
165687117650Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
165787117650Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
165887117650Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
165987117650Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
166087117650Saalok 
166187117650Saalok 	/*
166287117650Saalok 	 * Compute the total size of the uncompressed data
166387117650Saalok 	 * for use in fake_disk_geometry and other calculations.
166487117650Saalok 	 * Disk geometry has to be faked with respect to the
166587117650Saalok 	 * actual uncompressed data size rather than the
166687117650Saalok 	 * compressed file size.
166787117650Saalok 	 */
16687d11f38eSDavid Miner 	lsp->ls_vp_size =
16697d11f38eSDavid Miner 	    (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
167087117650Saalok 	    + lsp->ls_uncomp_last_seg_sz;
167187117650Saalok 
167287117650Saalok 	/*
1673b1efbcd6SAlok Aggarwal 	 * Index size is rounded up to DEV_BSIZE for ease
167487117650Saalok 	 * of segmapping
167587117650Saalok 	 */
167687117650Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
167787117650Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
167887117650Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
167987117650Saalok 	    sizeof (lsp->ls_comp_index_sz) +
168087117650Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
168187117650Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
168287117650Saalok 
168387117650Saalok 	index_sz += header_len;
168487117650Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
168587117650Saalok 
168687117650Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
168787117650Saalok 	lsp->ls_comp_index_data_sz = index_sz;
168887117650Saalok 
168987117650Saalok 	/*
169087117650Saalok 	 * Read in the index -- this has a side-effect
169187117650Saalok 	 * of reading in the header as well
169287117650Saalok 	 */
169387117650Saalok 	rw = UIO_READ;
169487117650Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
169587117650Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
169687117650Saalok 
169787117650Saalok 	if (error != 0)
169887117650Saalok 		return (error);
169987117650Saalok 
170087117650Saalok 	/* Skip the header, this is where the index really begins */
170187117650Saalok 	lsp->ls_comp_seg_index =
170287117650Saalok 	    /*LINTED*/
170387117650Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
170487117650Saalok 
170587117650Saalok 	/*
170687117650Saalok 	 * Now recompute offsets in the index to account for
170787117650Saalok 	 * the header length
170887117650Saalok 	 */
170987117650Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
171087117650Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
171187117650Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
171287117650Saalok 	}
171387117650Saalok 
171487117650Saalok 	return (error);
171587117650Saalok }
171687117650Saalok 
171787117650Saalok /*
171887117650Saalok  * Check to see if the passed in signature is a valid
171987117650Saalok  * one.  If it is valid, return the index into
172087117650Saalok  * lofi_compress_table.
172187117650Saalok  *
172287117650Saalok  * Return -1 if it is invalid
172387117650Saalok  */
172487117650Saalok static int lofi_compress_select(char *signature)
172587117650Saalok {
172687117650Saalok 	int i;
172787117650Saalok 
172887117650Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
172987117650Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
173087117650Saalok 			return (i);
173187117650Saalok 	}
173287117650Saalok 
173387117650Saalok 	return (-1);
173487117650Saalok }
173587117650Saalok 
173687117650Saalok /*
17377c478bd9Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
17387c478bd9Sstevel@tonic-gate  */
17397c478bd9Sstevel@tonic-gate static int
17407c478bd9Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
1741bd07e074Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
17427c478bd9Sstevel@tonic-gate {
17437c478bd9Sstevel@tonic-gate 	minor_t	newminor;
17447c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
17457c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
17467c478bd9Sstevel@tonic-gate 	int	error;
17477c478bd9Sstevel@tonic-gate 	struct vnode *vp;
17487c478bd9Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
17497c478bd9Sstevel@tonic-gate 	int64_t	Size_prop_val;
175087117650Saalok 	int	compress_index;
17517c478bd9Sstevel@tonic-gate 	vattr_t	vattr;
17527c478bd9Sstevel@tonic-gate 	int	flag;
17537c478bd9Sstevel@tonic-gate 	enum vtype v_type;
17547c478bd9Sstevel@tonic-gate 	int zalloced = 0;
17553d7072f8Seschrock 	dev_t	newdev;
17563d7072f8Seschrock 	char	namebuf[50];
175787117650Saalok 	char	buf[DEV_BSIZE];
17587d82f0f8SDina K Nimeh 	char	crybuf[DEV_BSIZE];
175987117650Saalok 	ssize_t	resid;
17607d82f0f8SDina K Nimeh 	boolean_t need_vn_close = B_FALSE;
17617d82f0f8SDina K Nimeh 	boolean_t keycopied = B_FALSE;
17627d82f0f8SDina K Nimeh 	boolean_t need_size_update = B_FALSE;
17637c478bd9Sstevel@tonic-gate 
1764bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
17657c478bd9Sstevel@tonic-gate 	if (klip == NULL)
17667c478bd9Sstevel@tonic-gate 		return (EFAULT);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
17717c478bd9Sstevel@tonic-gate 		error = EINVAL;
17727c478bd9Sstevel@tonic-gate 		goto out;
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
17767c478bd9Sstevel@tonic-gate 		error = EBUSY;
17777c478bd9Sstevel@tonic-gate 		goto out;
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	if (pickminor) {
17817c478bd9Sstevel@tonic-gate 		/* Find a free one */
17827c478bd9Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
17837c478bd9Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
17847c478bd9Sstevel@tonic-gate 				break;
17857c478bd9Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
17867c478bd9Sstevel@tonic-gate 			error = EAGAIN;
17877c478bd9Sstevel@tonic-gate 			goto out;
17887c478bd9Sstevel@tonic-gate 		}
17897c478bd9Sstevel@tonic-gate 	} else {
17907c478bd9Sstevel@tonic-gate 		newminor = klip->li_minor;
17917c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
17927c478bd9Sstevel@tonic-gate 			error = EEXIST;
17937c478bd9Sstevel@tonic-gate 			goto out;
17947c478bd9Sstevel@tonic-gate 		}
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	/* make sure it's valid */
17987c478bd9Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
17997c478bd9Sstevel@tonic-gate 	    NULLVPP, &vp);
18007c478bd9Sstevel@tonic-gate 	if (error) {
18017c478bd9Sstevel@tonic-gate 		goto out;
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 	v_type = vp->v_type;
18047c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
18057c478bd9Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
18067c478bd9Sstevel@tonic-gate 		error = EINVAL;
18077c478bd9Sstevel@tonic-gate 		goto out;
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
18107c478bd9Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
18117c478bd9Sstevel@tonic-gate 	if (error) {
18127c478bd9Sstevel@tonic-gate 		/* try read-only */
18137c478bd9Sstevel@tonic-gate 		flag &= ~FWRITE;
18147c478bd9Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
18157c478bd9Sstevel@tonic-gate 		    &vp, 0, 0);
18167c478bd9Sstevel@tonic-gate 		if (error) {
18177c478bd9Sstevel@tonic-gate 			goto out;
18187c478bd9Sstevel@tonic-gate 		}
18197c478bd9Sstevel@tonic-gate 	}
18207d82f0f8SDina K Nimeh 	need_vn_close = B_TRUE;
18217d82f0f8SDina K Nimeh 
18227c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
1823da6c28aaSamw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
18247c478bd9Sstevel@tonic-gate 	if (error) {
18257d82f0f8SDina K Nimeh 		goto out;
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
18287c478bd9Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
18297c478bd9Sstevel@tonic-gate 		error = EINVAL;
18307d82f0f8SDina K Nimeh 		goto out;
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
18337c478bd9Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
18347c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
18357c478bd9Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
18367c478bd9Sstevel@tonic-gate 		error = EINVAL;
18377d82f0f8SDina K Nimeh 		goto out;
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
18407c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
18417c478bd9Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
18427c478bd9Sstevel@tonic-gate 		error = EINVAL;
18437c478bd9Sstevel@tonic-gate 		goto propout;
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
18467c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
18477c478bd9Sstevel@tonic-gate 		error = ENOMEM;
18487c478bd9Sstevel@tonic-gate 		goto propout;
18497c478bd9Sstevel@tonic-gate 	}
18507c478bd9Sstevel@tonic-gate 	zalloced = 1;
18517c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
18527e45b93eSgd78059 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
18537c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
18547c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
18557c478bd9Sstevel@tonic-gate 		error = ENXIO;
18567c478bd9Sstevel@tonic-gate 		goto propout;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
18597c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
18607c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
18617c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
18627c478bd9Sstevel@tonic-gate 		/* remove block node */
18637c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
18647c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
18657c478bd9Sstevel@tonic-gate 		error = ENXIO;
18667c478bd9Sstevel@tonic-gate 		goto propout;
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
18697c478bd9Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
18707c478bd9Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
18717c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
18727c478bd9Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
18737c478bd9Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
18747c478bd9Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
18757c478bd9Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
18767c478bd9Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
18777c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
18787c478bd9Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
18797c478bd9Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
18807c478bd9Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
18817c478bd9Sstevel@tonic-gate 	}
18823d7072f8Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
18833d7072f8Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
18843d7072f8Seschrock 
18854058a205Sjrgn.keil@googlemail.com 	list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
18864058a205Sjrgn.keil@googlemail.com 	    offsetof(struct lofi_comp_cache, lc_list));
18874058a205Sjrgn.keil@googlemail.com 	mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
18884058a205Sjrgn.keil@googlemail.com 
18897c478bd9Sstevel@tonic-gate 	/*
18907c478bd9Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
18917c478bd9Sstevel@tonic-gate 	 * updated correctly.
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	lsp->ls_openflag = flag;
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	/*
18967c478bd9Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
18977c478bd9Sstevel@tonic-gate 	 */
18987c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
1899da6c28aaSamw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
19007c478bd9Sstevel@tonic-gate 			lsp->ls_vp = vp;
19017c478bd9Sstevel@tonic-gate 		} else {
19027c478bd9Sstevel@tonic-gate 			/*
19037c478bd9Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
19047c478bd9Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
19057c478bd9Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
19067c478bd9Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
19077c478bd9Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
19087c478bd9Sstevel@tonic-gate 			 * the realvp so things behave as if we've
19097c478bd9Sstevel@tonic-gate 			 * opened the realvp without any interaction
19107c478bd9Sstevel@tonic-gate 			 * with lofs.
19117c478bd9Sstevel@tonic-gate 			 */
19127c478bd9Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
19137c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 	} else {
19167c478bd9Sstevel@tonic-gate 		lsp->ls_vp = vp;
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
19197c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
19207c478bd9Sstevel@tonic-gate 	if (rvalp)
19217c478bd9Sstevel@tonic-gate 		*rvalp = (int)newminor;
19227c478bd9Sstevel@tonic-gate 	klip->li_minor = newminor;
19237c478bd9Sstevel@tonic-gate 
192487117650Saalok 	/*
19257d82f0f8SDina K Nimeh 	 * Initialize crypto details for encrypted lofi
192687117650Saalok 	 */
19277d82f0f8SDina K Nimeh 	if (klip->li_crypto_enabled) {
19287d82f0f8SDina K Nimeh 		int ret;
192987117650Saalok 
19307d82f0f8SDina K Nimeh 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
19317d82f0f8SDina K Nimeh 
19327d82f0f8SDina K Nimeh 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
19337d82f0f8SDina K Nimeh 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
19347d82f0f8SDina K Nimeh 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
19357d82f0f8SDina K Nimeh 			    klip->li_cipher, lsp->ls_filename);
19367d82f0f8SDina K Nimeh 			error = EINVAL;
19377d82f0f8SDina K Nimeh 			goto propout;
19387d82f0f8SDina K Nimeh 		}
19397d82f0f8SDina K Nimeh 
19407d82f0f8SDina K Nimeh 		/* this is just initialization here */
19417d82f0f8SDina K Nimeh 		lsp->ls_mech.cm_param = NULL;
19427d82f0f8SDina K Nimeh 		lsp->ls_mech.cm_param_len = 0;
19437d82f0f8SDina K Nimeh 
19447d82f0f8SDina K Nimeh 		lsp->ls_iv_type = klip->li_iv_type;
19457d82f0f8SDina K Nimeh 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
19467d82f0f8SDina K Nimeh 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
19477d82f0f8SDina K Nimeh 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
19487d82f0f8SDina K Nimeh 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
19497d82f0f8SDina K Nimeh 			error = EINVAL;
19507d82f0f8SDina K Nimeh 			goto propout;
19517d82f0f8SDina K Nimeh 		}
19527d82f0f8SDina K Nimeh 
19537d82f0f8SDina K Nimeh 		/* iv mech must itself take a null iv */
19547d82f0f8SDina K Nimeh 		lsp->ls_iv_mech.cm_param = NULL;
19557d82f0f8SDina K Nimeh 		lsp->ls_iv_mech.cm_param_len = 0;
19567d82f0f8SDina K Nimeh 		lsp->ls_iv_len = klip->li_iv_len;
19577d82f0f8SDina K Nimeh 
19587d82f0f8SDina K Nimeh 		/*
19597d82f0f8SDina K Nimeh 		 * Create ctx using li_cipher & the raw li_key after checking
19607d82f0f8SDina K Nimeh 		 * that it isn't a weak key.
19617d82f0f8SDina K Nimeh 		 */
19627d82f0f8SDina K Nimeh 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
19637d82f0f8SDina K Nimeh 		lsp->ls_key.ck_length = klip->li_key_len;
19647d82f0f8SDina K Nimeh 		lsp->ls_key.ck_data = kmem_alloc(
19657d82f0f8SDina K Nimeh 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
19667d82f0f8SDina K Nimeh 		bcopy(klip->li_key, lsp->ls_key.ck_data,
19677d82f0f8SDina K Nimeh 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
19687d82f0f8SDina K Nimeh 		keycopied = B_TRUE;
19697d82f0f8SDina K Nimeh 
19707d82f0f8SDina K Nimeh 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
19717d82f0f8SDina K Nimeh 		if (ret != CRYPTO_SUCCESS) {
19727d82f0f8SDina K Nimeh 			error = EINVAL;
19737d82f0f8SDina K Nimeh 			cmn_err(CE_WARN, "weak key check failed for cipher "
19747d82f0f8SDina K Nimeh 			    "%s on file %s (0x%x)", klip->li_cipher,
19757d82f0f8SDina K Nimeh 			    lsp->ls_filename, ret);
19767d82f0f8SDina K Nimeh 			goto propout;
19777d82f0f8SDina K Nimeh 		}
19787d82f0f8SDina K Nimeh 	}
19797d82f0f8SDina K Nimeh 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
19807d82f0f8SDina K Nimeh 
19817d82f0f8SDina K Nimeh 	/*
19827d82f0f8SDina K Nimeh 	 * Read the file signature to check if it is compressed or encrypted.
19837d82f0f8SDina K Nimeh 	 * Crypto signature is in a different location; both areas should
19847d82f0f8SDina K Nimeh 	 * read to keep compression and encryption mutually exclusive.
19857d82f0f8SDina K Nimeh 	 */
19867d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
19877d82f0f8SDina K Nimeh 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
19887d82f0f8SDina K Nimeh 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
19897d82f0f8SDina K Nimeh 		if (error != 0)
19907d82f0f8SDina K Nimeh 			goto propout;
19917d82f0f8SDina K Nimeh 	}
19927d82f0f8SDina K Nimeh 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
19937d82f0f8SDina K Nimeh 	    0, RLIM64_INFINITY, kcred, &resid);
199487117650Saalok 	if (error != 0)
199587117650Saalok 		goto propout;
199687117650Saalok 
19977d82f0f8SDina K Nimeh 	/* initialize these variables for all lofi files */
199887117650Saalok 	lsp->ls_uncomp_seg_sz = 0;
199987117650Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
200087117650Saalok 	lsp->ls_comp_algorithm[0] = '\0';
200187117650Saalok 
20027d82f0f8SDina K Nimeh 	/* encrypted lofi reads/writes shifted by crypto metadata size */
20037d82f0f8SDina K Nimeh 	lsp->ls_crypto_offset = 0;
20047d82f0f8SDina K Nimeh 
20057d82f0f8SDina K Nimeh 	/* this is a compressed lofi */
20067d82f0f8SDina K Nimeh 	if ((compress_index = lofi_compress_select(buf)) != -1) {
20077d82f0f8SDina K Nimeh 
20087d82f0f8SDina K Nimeh 		/* compression and encryption are mutually exclusive */
20097d82f0f8SDina K Nimeh 		if (klip->li_crypto_enabled) {
20107d82f0f8SDina K Nimeh 			error = ENOTSUP;
20117d82f0f8SDina K Nimeh 			goto propout;
20127d82f0f8SDina K Nimeh 		}
20137d82f0f8SDina K Nimeh 
20147d82f0f8SDina K Nimeh 		/* initialize compression info for compressed lofi */
201587117650Saalok 		lsp->ls_comp_algorithm_index = compress_index;
201687117650Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
201787117650Saalok 		    lofi_compress_table[compress_index].l_name,
201887117650Saalok 		    sizeof (lsp->ls_comp_algorithm));
20197d82f0f8SDina K Nimeh 
202087117650Saalok 		error = lofi_map_compressed_file(lsp, buf);
202187117650Saalok 		if (error != 0)
202287117650Saalok 			goto propout;
20237d82f0f8SDina K Nimeh 		need_size_update = B_TRUE;
202487117650Saalok 
20257d82f0f8SDina K Nimeh 	/* this is an encrypted lofi */
20267d82f0f8SDina K Nimeh 	} else if (strncmp(crybuf, lofi_crypto_magic,
20277d82f0f8SDina K Nimeh 	    sizeof (lofi_crypto_magic)) == 0) {
20287d82f0f8SDina K Nimeh 
20297d82f0f8SDina K Nimeh 		char *marker = crybuf;
20307d82f0f8SDina K Nimeh 
20317d82f0f8SDina K Nimeh 		/*
20327d82f0f8SDina K Nimeh 		 * This is the case where the header in the lofi image is
20337d82f0f8SDina K Nimeh 		 * already initialized to indicate it is encrypted.
20347d82f0f8SDina K Nimeh 		 * There is another case (see below) where encryption is
20357d82f0f8SDina K Nimeh 		 * requested but the lofi image has never been used yet,
20367d82f0f8SDina K Nimeh 		 * so the header needs to be written with encryption magic.
20377d82f0f8SDina K Nimeh 		 */
20387d82f0f8SDina K Nimeh 
20397d82f0f8SDina K Nimeh 		/* indicate this must be an encrypted lofi due to magic */
20407d82f0f8SDina K Nimeh 		klip->li_crypto_enabled = B_TRUE;
20417d82f0f8SDina K Nimeh 
20427d82f0f8SDina K Nimeh 		/*
20437d82f0f8SDina K Nimeh 		 * The encryption header information is laid out this way:
20447d82f0f8SDina K Nimeh 		 *	6 bytes:	hex "CFLOFI"
20457d82f0f8SDina K Nimeh 		 *	2 bytes:	version = 0 ... for now
20467d82f0f8SDina K Nimeh 		 *	96 bytes:	reserved1 (not implemented yet)
20477d82f0f8SDina K Nimeh 		 *	4 bytes:	data_sector = 2 ... for now
20487d82f0f8SDina K Nimeh 		 *	more...		not implemented yet
20497d82f0f8SDina K Nimeh 		 */
20507d82f0f8SDina K Nimeh 
20517d82f0f8SDina K Nimeh 		/* copy the magic */
20527d82f0f8SDina K Nimeh 		bcopy(marker, lsp->ls_crypto.magic,
20537d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.magic));
20547d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.magic);
20557d82f0f8SDina K Nimeh 
20567d82f0f8SDina K Nimeh 		/* read the encryption version number */
20577d82f0f8SDina K Nimeh 		bcopy(marker, &(lsp->ls_crypto.version),
20587d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.version));
20597d82f0f8SDina K Nimeh 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
20607d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.version);
20617d82f0f8SDina K Nimeh 
20627d82f0f8SDina K Nimeh 		/* read a chunk of reserved data */
20637d82f0f8SDina K Nimeh 		bcopy(marker, lsp->ls_crypto.reserved1,
20647d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.reserved1));
20657d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.reserved1);
20667d82f0f8SDina K Nimeh 
20677d82f0f8SDina K Nimeh 		/* read block number where encrypted data begins */
20687d82f0f8SDina K Nimeh 		bcopy(marker, &(lsp->ls_crypto.data_sector),
20697d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.data_sector));
20707d82f0f8SDina K Nimeh 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
20717d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.data_sector);
20727d82f0f8SDina K Nimeh 
20737d82f0f8SDina K Nimeh 		/* and ignore the rest until it is implemented */
20747d82f0f8SDina K Nimeh 
20757d82f0f8SDina K Nimeh 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
20767d82f0f8SDina K Nimeh 		need_size_update = B_TRUE;
20777d82f0f8SDina K Nimeh 
20787d82f0f8SDina K Nimeh 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
20797d82f0f8SDina K Nimeh 	} else if (klip->li_crypto_enabled) {
20807d82f0f8SDina K Nimeh 
20817d82f0f8SDina K Nimeh 		/*
20827d82f0f8SDina K Nimeh 		 * This is the case where encryption was requested but the
20837d82f0f8SDina K Nimeh 		 * appears to be entirely blank where the encryption header
20847d82f0f8SDina K Nimeh 		 * would have been in the lofi image.  If it is blank,
20857d82f0f8SDina K Nimeh 		 * assume it is a brand new lofi image and initialize the
20867d82f0f8SDina K Nimeh 		 * header area with encryption magic and current version
20877d82f0f8SDina K Nimeh 		 * header data.  If it is not blank, that's an error.
20887d82f0f8SDina K Nimeh 		 */
20897d82f0f8SDina K Nimeh 		int	i;
20907d82f0f8SDina K Nimeh 		char	*marker;
20917d82f0f8SDina K Nimeh 		struct crypto_meta	chead;
20927d82f0f8SDina K Nimeh 
20937d82f0f8SDina K Nimeh 		for (i = 0; i < sizeof (struct crypto_meta); i++)
20947d82f0f8SDina K Nimeh 			if (crybuf[i] != '\0')
20957d82f0f8SDina K Nimeh 				break;
20967d82f0f8SDina K Nimeh 		if (i != sizeof (struct crypto_meta)) {
20977d82f0f8SDina K Nimeh 			error = EINVAL;
20987d82f0f8SDina K Nimeh 			goto propout;
20997d82f0f8SDina K Nimeh 		}
21007d82f0f8SDina K Nimeh 
21017d82f0f8SDina K Nimeh 		/* nothing there, initialize as encrypted lofi */
21027d82f0f8SDina K Nimeh 		marker = crybuf;
21037d82f0f8SDina K Nimeh 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
21047d82f0f8SDina K Nimeh 		marker += sizeof (lofi_crypto_magic);
21057d82f0f8SDina K Nimeh 		chead.version = htons(LOFI_CRYPTO_VERSION);
21067d82f0f8SDina K Nimeh 		bcopy(&(chead.version), marker, sizeof (chead.version));
21077d82f0f8SDina K Nimeh 		marker += sizeof (chead.version);
21087d82f0f8SDina K Nimeh 		marker += sizeof (chead.reserved1);
21097d82f0f8SDina K Nimeh 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
21107d82f0f8SDina K Nimeh 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
21117d82f0f8SDina K Nimeh 
21127d82f0f8SDina K Nimeh 		/* write the header */
21137d82f0f8SDina K Nimeh 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
21147d82f0f8SDina K Nimeh 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
21157d82f0f8SDina K Nimeh 		if (error != 0)
21167d82f0f8SDina K Nimeh 			goto propout;
21177d82f0f8SDina K Nimeh 
21187d82f0f8SDina K Nimeh 		/* fix things up so it looks like we read this info */
21197d82f0f8SDina K Nimeh 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
21207d82f0f8SDina K Nimeh 		    sizeof (lofi_crypto_magic));
21217d82f0f8SDina K Nimeh 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
21227d82f0f8SDina K Nimeh 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
21237d82f0f8SDina K Nimeh 
21247d82f0f8SDina K Nimeh 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
21257d82f0f8SDina K Nimeh 		need_size_update = B_TRUE;
21267d82f0f8SDina K Nimeh 	}
21277d82f0f8SDina K Nimeh 
21287d82f0f8SDina K Nimeh 	/*
21297d82f0f8SDina K Nimeh 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
21307d82f0f8SDina K Nimeh 	 * for encrypted lofi, advertise that it is somewhat shorter
21317d82f0f8SDina K Nimeh 	 * due to embedded crypto metadata section
21327d82f0f8SDina K Nimeh 	 */
21337d82f0f8SDina K Nimeh 	if (need_size_update) {
213487117650Saalok 		/* update DDI properties */
21357d82f0f8SDina K Nimeh 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
213687117650Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
213787117650Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
213887117650Saalok 			error = EINVAL;
213987117650Saalok 			goto propout;
214087117650Saalok 		}
21417d82f0f8SDina K Nimeh 		Nblocks_prop_val =
21427d82f0f8SDina K Nimeh 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
214387117650Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
214487117650Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
214587117650Saalok 			error = EINVAL;
214687117650Saalok 			goto propout;
214787117650Saalok 		}
214887117650Saalok 	}
214987117650Saalok 
21507c478bd9Sstevel@tonic-gate 	fake_disk_geometry(lsp);
21517c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2152bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21537c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
21547c478bd9Sstevel@tonic-gate 	return (0);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate propout:
21577d82f0f8SDina K Nimeh 	if (keycopied) {
21587d82f0f8SDina K Nimeh 		bzero(lsp->ls_key.ck_data,
21597d82f0f8SDina K Nimeh 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
21607d82f0f8SDina K Nimeh 		kmem_free(lsp->ls_key.ck_data,
21617d82f0f8SDina K Nimeh 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
21627d82f0f8SDina K Nimeh 		lsp->ls_key.ck_data = NULL;
21637d82f0f8SDina K Nimeh 		lsp->ls_key.ck_length = 0;
21647d82f0f8SDina K Nimeh 	}
21657d82f0f8SDina K Nimeh 
21667c478bd9Sstevel@tonic-gate 	if (zalloced)
21677c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, newminor);
21687d82f0f8SDina K Nimeh 
21697d82f0f8SDina K Nimeh 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
21707d82f0f8SDina K Nimeh 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
21717d82f0f8SDina K Nimeh 
21727d82f0f8SDina K Nimeh out:
21737d82f0f8SDina K Nimeh 	if (need_vn_close) {
21747d82f0f8SDina K Nimeh 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
21757d82f0f8SDina K Nimeh 		VN_RELE(vp);
21767d82f0f8SDina K Nimeh 	}
21777d82f0f8SDina K Nimeh 
21787c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
21797c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
21807c478bd9Sstevel@tonic-gate 	return (error);
21817c478bd9Sstevel@tonic-gate }
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate /*
21847c478bd9Sstevel@tonic-gate  * unmap a file.
21857c478bd9Sstevel@tonic-gate  */
21867c478bd9Sstevel@tonic-gate static int
21877c478bd9Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
2188bd07e074Sheppo     struct cred *credp, int ioctl_flag)
21897c478bd9Sstevel@tonic-gate {
21907c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
21917c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
21927c478bd9Sstevel@tonic-gate 	minor_t	minor;
21937c478bd9Sstevel@tonic-gate 
2194bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
21957c478bd9Sstevel@tonic-gate 	if (klip == NULL)
21967c478bd9Sstevel@tonic-gate 		return (EFAULT);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
21997c478bd9Sstevel@tonic-gate 	if (byfilename) {
22007c478bd9Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
22017c478bd9Sstevel@tonic-gate 	} else {
22027c478bd9Sstevel@tonic-gate 		minor = klip->li_minor;
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 	if (minor == 0) {
22057c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22067c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22077c478bd9Sstevel@tonic-gate 		return (ENXIO);
22087c478bd9Sstevel@tonic-gate 	}
22097c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
22103d7072f8Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
22117c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22127c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22137c478bd9Sstevel@tonic-gate 		return (ENXIO);
22147c478bd9Sstevel@tonic-gate 	}
22153d7072f8Seschrock 
22163d7072f8Seschrock 	/*
221793239addSjohnlev 	 * If it's still held open, we'll do one of three things:
221893239addSjohnlev 	 *
221993239addSjohnlev 	 * If no flag is set, just return EBUSY.
222093239addSjohnlev 	 *
222193239addSjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
222293239addSjohnlev 	 * the last user finishes.
222393239addSjohnlev 	 *
222493239addSjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
222593239addSjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
222693239addSjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
222793239addSjohnlev 	 * device will be cleaned up appropriately.
22283d7072f8Seschrock 	 *
22293d7072f8Seschrock 	 * This is complicated by the fact that we may have outstanding
223093239addSjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
22317d82f0f8SDina K Nimeh 	 * I/O, we keep a count of the number of outstanding I/O requests
22327d82f0f8SDina K Nimeh 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
22337d82f0f8SDina K Nimeh 	 * should be dispatched (ls_vp_closereq).
22347d82f0f8SDina K Nimeh 	 *
223593239addSjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
223693239addSjohnlev 	 * and then close the underlying vnode.
22373d7072f8Seschrock 	 */
223893239addSjohnlev 	if (is_opened(lsp)) {
22393d7072f8Seschrock 		if (klip->li_force) {
22403d7072f8Seschrock 			mutex_enter(&lsp->ls_vp_lock);
22413d7072f8Seschrock 			lsp->ls_vp_closereq = B_TRUE;
2242*b3388e4fSEric Taylor 			/* wake up any threads waiting on dkiocstate */
2243*b3388e4fSEric Taylor 			cv_broadcast(&lsp->ls_vp_cv);
22443d7072f8Seschrock 			while (lsp->ls_vp_iocount > 0)
22453d7072f8Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
22463d7072f8Seschrock 			mutex_exit(&lsp->ls_vp_lock);
2247*b3388e4fSEric Taylor 			lofi_free_handle(dev, minor, lsp, credp);
22487d82f0f8SDina K Nimeh 
22493d7072f8Seschrock 			klip->li_minor = minor;
22507d82f0f8SDina K Nimeh 			mutex_exit(&lofi_lock);
22513d7072f8Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22523d7072f8Seschrock 			free_lofi_ioctl(klip);
22533d7072f8Seschrock 			return (0);
225493239addSjohnlev 		} else if (klip->li_cleanup) {
225593239addSjohnlev 			lsp->ls_cleanup = 1;
225693239addSjohnlev 			mutex_exit(&lofi_lock);
225793239addSjohnlev 			free_lofi_ioctl(klip);
225893239addSjohnlev 			return (0);
22593d7072f8Seschrock 		}
226093239addSjohnlev 
22617c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22627c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22637c478bd9Sstevel@tonic-gate 		return (EBUSY);
22647c478bd9Sstevel@tonic-gate 	}
22657c478bd9Sstevel@tonic-gate 
22663d7072f8Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	klip->li_minor = minor;
22697c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2270bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22717c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
22727c478bd9Sstevel@tonic-gate 	return (0);
22737c478bd9Sstevel@tonic-gate }
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate /*
22767c478bd9Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
22777c478bd9Sstevel@tonic-gate  * the name.
22787c478bd9Sstevel@tonic-gate  */
22793d7072f8Seschrock /*ARGSUSED*/
22807c478bd9Sstevel@tonic-gate static int
22817c478bd9Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
2282bd07e074Sheppo     struct cred *credp, int ioctl_flag)
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
22857c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
22867c478bd9Sstevel@tonic-gate 	int	error;
22877c478bd9Sstevel@tonic-gate 	minor_t	minor;
22887c478bd9Sstevel@tonic-gate 
2289bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
22907c478bd9Sstevel@tonic-gate 	if (klip == NULL)
22917c478bd9Sstevel@tonic-gate 		return (EFAULT);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	switch (which) {
22947c478bd9Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
22957c478bd9Sstevel@tonic-gate 		minor = klip->li_minor;
22967c478bd9Sstevel@tonic-gate 		if (minor == 0) {
22977c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
22987c478bd9Sstevel@tonic-gate 			return (EINVAL);
22997c478bd9Sstevel@tonic-gate 		}
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23027c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
23037c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
23047c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
23057c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23067c478bd9Sstevel@tonic-gate 			return (ENXIO);
23077c478bd9Sstevel@tonic-gate 		}
23087c478bd9Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
230987117650Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
231087117650Saalok 		    sizeof (klip->li_algorithm));
23117d82f0f8SDina K Nimeh 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
23127c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
2313bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23147c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23157c478bd9Sstevel@tonic-gate 		return (error);
23167c478bd9Sstevel@tonic-gate 	case LOFI_GET_MINOR:
23177c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23187c478bd9Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
23197d82f0f8SDina K Nimeh 		/* caller should not depend on klip->li_crypto_enabled here */
23207c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
23217c478bd9Sstevel@tonic-gate 		if (klip->li_minor == 0) {
23227c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23237c478bd9Sstevel@tonic-gate 			return (ENOENT);
23247c478bd9Sstevel@tonic-gate 		}
2325bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23267c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23277c478bd9Sstevel@tonic-gate 		return (error);
232887117650Saalok 	case LOFI_CHECK_COMPRESSED:
232987117650Saalok 		mutex_enter(&lofi_lock);
233087117650Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
233187117650Saalok 		mutex_exit(&lofi_lock);
233287117650Saalok 		if (klip->li_minor == 0) {
233387117650Saalok 			free_lofi_ioctl(klip);
233487117650Saalok 			return (ENOENT);
233587117650Saalok 		}
233687117650Saalok 		mutex_enter(&lofi_lock);
233787117650Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
233887117650Saalok 		if (lsp == NULL) {
233987117650Saalok 			mutex_exit(&lofi_lock);
234087117650Saalok 			free_lofi_ioctl(klip);
234187117650Saalok 			return (ENXIO);
234287117650Saalok 		}
234387117650Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
234487117650Saalok 
234587117650Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
234687117650Saalok 		    sizeof (klip->li_algorithm));
234787117650Saalok 		mutex_exit(&lofi_lock);
234887117650Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
234987117650Saalok 		free_lofi_ioctl(klip);
235087117650Saalok 		return (error);
23517c478bd9Sstevel@tonic-gate 	default:
23527c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23537c478bd9Sstevel@tonic-gate 		return (EINVAL);
23547c478bd9Sstevel@tonic-gate 	}
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate static int
23597c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
23607c478bd9Sstevel@tonic-gate     int *rvalp)
23617c478bd9Sstevel@tonic-gate {
23627c478bd9Sstevel@tonic-gate 	int	error;
23637c478bd9Sstevel@tonic-gate 	enum dkio_state dkstate;
23647c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
23657c478bd9Sstevel@tonic-gate 	minor_t	minor;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
23687c478bd9Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
23697c478bd9Sstevel@tonic-gate 	if (minor == 0) {
23707c478bd9Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 		/*
23737c478bd9Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
23747c478bd9Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
23757c478bd9Sstevel@tonic-gate 		 * long as they can open it read-only.
23767c478bd9Sstevel@tonic-gate 		 */
23777c478bd9Sstevel@tonic-gate 		switch (cmd) {
23787c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE:
23797c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23807c478bd9Sstevel@tonic-gate 				return (EPERM);
2381bd07e074Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
23827c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
23837c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23847c478bd9Sstevel@tonic-gate 				return (EPERM);
2385bd07e074Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
23867c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
23877c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23887c478bd9Sstevel@tonic-gate 				return (EPERM);
2389bd07e074Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
23907c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
23917c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23927c478bd9Sstevel@tonic-gate 				return (EPERM);
2393bd07e074Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
23947c478bd9Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
23957c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
2396bd07e074Sheppo 			    credp, flag));
23977c478bd9Sstevel@tonic-gate 		case LOFI_GET_MINOR:
23987c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
2399bd07e074Sheppo 			    credp, flag));
24007c478bd9Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
2401bd07e074Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
2402bd07e074Sheppo 			    sizeof (lofi_max_files), flag);
24037c478bd9Sstevel@tonic-gate 			if (error)
24047c478bd9Sstevel@tonic-gate 				return (EFAULT);
24057c478bd9Sstevel@tonic-gate 			return (0);
240687117650Saalok 		case LOFI_CHECK_COMPRESSED:
240787117650Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
240887117650Saalok 			    credp, flag));
24097c478bd9Sstevel@tonic-gate 		default:
24107c478bd9Sstevel@tonic-gate 			break;
24117c478bd9Sstevel@tonic-gate 		}
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
2414*b3388e4fSEric Taylor 	mutex_enter(&lofi_lock);
24157c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
2416*b3388e4fSEric Taylor 	if (lsp == NULL || lsp->ls_vp_closereq) {
2417*b3388e4fSEric Taylor 		mutex_exit(&lofi_lock);
24187c478bd9Sstevel@tonic-gate 		return (ENXIO);
2419*b3388e4fSEric Taylor 	}
2420*b3388e4fSEric Taylor 	mutex_exit(&lofi_lock);
24217c478bd9Sstevel@tonic-gate 
24223d7072f8Seschrock 	/*
24233d7072f8Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
24243d7072f8Seschrock 	 * EIO as if the device was no longer present.
24253d7072f8Seschrock 	 */
24263d7072f8Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
24273d7072f8Seschrock 		return (EIO);
24283d7072f8Seschrock 
24297c478bd9Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
24307c478bd9Sstevel@tonic-gate 	switch (cmd) {
24317c478bd9Sstevel@tonic-gate 	case DKIOCGVTOC:
24327c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
24337c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
24347c478bd9Sstevel@tonic-gate 			struct vtoc32 vtoc32;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
24377c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
24387c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
24397c478bd9Sstevel@tonic-gate 				return (EFAULT);
24407c478bd9Sstevel@tonic-gate 			break;
24417c478bd9Sstevel@tonic-gate 			}
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
24447c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
24457c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
24467c478bd9Sstevel@tonic-gate 				return (EFAULT);
24477c478bd9Sstevel@tonic-gate 			break;
24487c478bd9Sstevel@tonic-gate 		}
24497c478bd9Sstevel@tonic-gate 		return (0);
24507c478bd9Sstevel@tonic-gate 	case DKIOCINFO:
2451bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
2452bd07e074Sheppo 		    sizeof (struct dk_cinfo), flag);
24537c478bd9Sstevel@tonic-gate 		if (error)
24547c478bd9Sstevel@tonic-gate 			return (EFAULT);
24557c478bd9Sstevel@tonic-gate 		return (0);
24567c478bd9Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
24577c478bd9Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
24587c478bd9Sstevel@tonic-gate 	case DKIOCGGEOM:
2459bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
2460bd07e074Sheppo 		    sizeof (struct dk_geom), flag);
24617c478bd9Sstevel@tonic-gate 		if (error)
24627c478bd9Sstevel@tonic-gate 			return (EFAULT);
24637c478bd9Sstevel@tonic-gate 		return (0);
24647c478bd9Sstevel@tonic-gate 	case DKIOCSTATE:
24653d7072f8Seschrock 		/*
24663d7072f8Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
24673d7072f8Seschrock 		 * a device is forcefully unmapped, then the device transitions
24683d7072f8Seschrock 		 * to the DKIO_DEV_GONE state.
24693d7072f8Seschrock 		 */
24703d7072f8Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
24713d7072f8Seschrock 		    flag) != 0)
24723d7072f8Seschrock 			return (EFAULT);
24733d7072f8Seschrock 
24743d7072f8Seschrock 		mutex_enter(&lsp->ls_vp_lock);
2475*b3388e4fSEric Taylor 		lsp->ls_vp_iocount++;
2476*b3388e4fSEric Taylor 		while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
2477*b3388e4fSEric Taylor 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
2478*b3388e4fSEric Taylor 		    !lsp->ls_vp_closereq) {
24793d7072f8Seschrock 			/*
24803d7072f8Seschrock 			 * By virtue of having the device open, we know that
24813d7072f8Seschrock 			 * 'lsp' will remain valid when we return.
24823d7072f8Seschrock 			 */
24833d7072f8Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
24843d7072f8Seschrock 			    &lsp->ls_vp_lock)) {
2485*b3388e4fSEric Taylor 				lsp->ls_vp_iocount--;
2486*b3388e4fSEric Taylor 				cv_broadcast(&lsp->ls_vp_cv);
24873d7072f8Seschrock 				mutex_exit(&lsp->ls_vp_lock);
24883d7072f8Seschrock 				return (EINTR);
24893d7072f8Seschrock 			}
24903d7072f8Seschrock 		}
24913d7072f8Seschrock 
2492*b3388e4fSEric Taylor 		dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ?
2493*b3388e4fSEric Taylor 		    DKIO_INSERTED : DKIO_DEV_GONE);
2494*b3388e4fSEric Taylor 		lsp->ls_vp_iocount--;
2495*b3388e4fSEric Taylor 		cv_broadcast(&lsp->ls_vp_cv);
24963d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
24973d7072f8Seschrock 
24983d7072f8Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
24993d7072f8Seschrock 		    sizeof (dkstate), flag) != 0)
25007c478bd9Sstevel@tonic-gate 			return (EFAULT);
25017c478bd9Sstevel@tonic-gate 		return (0);
25027c478bd9Sstevel@tonic-gate 	default:
25037c478bd9Sstevel@tonic-gate 		return (ENOTTY);
25047c478bd9Sstevel@tonic-gate 	}
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
25087c478bd9Sstevel@tonic-gate 	lofi_open,		/* open */
25097c478bd9Sstevel@tonic-gate 	lofi_close,		/* close */
25107c478bd9Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
25117c478bd9Sstevel@tonic-gate 	nodev,			/* print */
25127c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
25137c478bd9Sstevel@tonic-gate 	lofi_read,		/* read */
25147c478bd9Sstevel@tonic-gate 	lofi_write,		/* write */
25157c478bd9Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
25167c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
25177c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
25187c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
25197c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
25207c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
25217c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
25227c478bd9Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
25237c478bd9Sstevel@tonic-gate 	CB_REV,
25247c478bd9Sstevel@tonic-gate 	lofi_aread,
25257c478bd9Sstevel@tonic-gate 	lofi_awrite
25267c478bd9Sstevel@tonic-gate };
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = {
25297c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
25307c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
25317c478bd9Sstevel@tonic-gate 	lofi_info,		/* info */
25327c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
25337c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
25347c478bd9Sstevel@tonic-gate 	lofi_attach,		/* attach */
25357c478bd9Sstevel@tonic-gate 	lofi_detach,		/* detach */
25367c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
25377c478bd9Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
253819397407SSherry Moore 	NULL,			/* no bus operations */
253919397407SSherry Moore 	NULL,			/* power */
254019397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
25417c478bd9Sstevel@tonic-gate };
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
25447c478bd9Sstevel@tonic-gate 	&mod_driverops,
254519397407SSherry Moore 	"loopback file driver",
25467c478bd9Sstevel@tonic-gate 	&lofi_ops,
25477c478bd9Sstevel@tonic-gate };
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
25507c478bd9Sstevel@tonic-gate 	MODREV_1,
25517c478bd9Sstevel@tonic-gate 	&modldrv,
25527c478bd9Sstevel@tonic-gate 	NULL
25537c478bd9Sstevel@tonic-gate };
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate int
25567c478bd9Sstevel@tonic-gate _init(void)
25577c478bd9Sstevel@tonic-gate {
25587c478bd9Sstevel@tonic-gate 	int error;
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
25617c478bd9Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
25627c478bd9Sstevel@tonic-gate 	if (error)
25637c478bd9Sstevel@tonic-gate 		return (error);
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
25667c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
25677c478bd9Sstevel@tonic-gate 	if (error) {
25687c478bd9Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
25697c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
25707c478bd9Sstevel@tonic-gate 	}
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	return (error);
25737c478bd9Sstevel@tonic-gate }
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate int
25767c478bd9Sstevel@tonic-gate _fini(void)
25777c478bd9Sstevel@tonic-gate {
25787c478bd9Sstevel@tonic-gate 	int	error;
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	if (lofi_busy())
25817c478bd9Sstevel@tonic-gate 		return (EBUSY);
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
25847c478bd9Sstevel@tonic-gate 	if (error)
25857c478bd9Sstevel@tonic-gate 		return (error);
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
25887c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	return (error);
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate int
25947c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
25957c478bd9Sstevel@tonic-gate {
25967c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
25977c478bd9Sstevel@tonic-gate }
2598