xref: /onnv-gate/usr/src/uts/common/io/lofi.c (revision 10197:254a1a1c96d5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51657Sheppo  * Common Development and Distribution License (the "License").
61657Sheppo  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228669SDina.Nimeh@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
280Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
290Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
300Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
310Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
320Sstevel@tonic-gate  * mounting images of filesystems.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
350Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
360Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
370Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
380Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
390Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
400Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
410Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
420Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
430Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
460Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
470Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
480Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
490Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
500Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
530Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
540Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
550Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
560Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
570Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
580Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
590Sstevel@tonic-gate  *
604451Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
614451Seschrock  * support simulation of hotplug events, an optional force flag is provided.
624451Seschrock  * If a lofi device is open when a force detach is requested, then the
634451Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
644451Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
654451Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
664451Seschrock  * detached but not removed.
674451Seschrock  *
680Sstevel@tonic-gate  * Known problems:
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
710Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
720Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
730Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
760Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
770Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
780Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
810Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
820Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
830Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
840Sstevel@tonic-gate  *	do. Though it would still be silly.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * Interesting things to do:
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
910Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
928313SDina.Nimeh@Sun.Com  *	need to fake a geometry, however, it may be relevant if you're replacing
938313SDina.Nimeh@Sun.Com  *	metadisk, or using lofi to get crypto.
948313SDina.Nimeh@Sun.Com  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
958313SDina.Nimeh@Sun.Com  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
968313SDina.Nimeh@Sun.Com  *	In fact this even makes sense if you have lofi "above" metadisk.
970Sstevel@tonic-gate  *
988313SDina.Nimeh@Sun.Com  * Encryption:
998313SDina.Nimeh@Sun.Com  *	Each lofi device can have its own symmetric key and cipher.
1008313SDina.Nimeh@Sun.Com  *	They are passed to us by lofiadm(1m) in the correct format for use
1018313SDina.Nimeh@Sun.Com  *	with the misc/kcf crypto_* routines.
1028313SDina.Nimeh@Sun.Com  *
1038313SDina.Nimeh@Sun.Com  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
1048313SDina.Nimeh@Sun.Com  *	on the "master" key held in the lsp and the block number of the buffer.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #include <sys/types.h>
1085643Saalok #include <netinet/in.h>
1090Sstevel@tonic-gate #include <sys/sysmacros.h>
1100Sstevel@tonic-gate #include <sys/uio.h>
1110Sstevel@tonic-gate #include <sys/kmem.h>
1120Sstevel@tonic-gate #include <sys/cred.h>
1130Sstevel@tonic-gate #include <sys/mman.h>
1140Sstevel@tonic-gate #include <sys/errno.h>
1150Sstevel@tonic-gate #include <sys/aio_req.h>
1160Sstevel@tonic-gate #include <sys/stat.h>
1170Sstevel@tonic-gate #include <sys/file.h>
1180Sstevel@tonic-gate #include <sys/modctl.h>
1190Sstevel@tonic-gate #include <sys/conf.h>
1200Sstevel@tonic-gate #include <sys/debug.h>
1210Sstevel@tonic-gate #include <sys/vnode.h>
1220Sstevel@tonic-gate #include <sys/lofi.h>
1230Sstevel@tonic-gate #include <sys/fcntl.h>
1240Sstevel@tonic-gate #include <sys/pathname.h>
1250Sstevel@tonic-gate #include <sys/filio.h>
1260Sstevel@tonic-gate #include <sys/fdio.h>
1270Sstevel@tonic-gate #include <sys/open.h>
1280Sstevel@tonic-gate #include <sys/disp.h>
1290Sstevel@tonic-gate #include <vm/seg_map.h>
1300Sstevel@tonic-gate #include <sys/ddi.h>
1310Sstevel@tonic-gate #include <sys/sunddi.h>
1325643Saalok #include <sys/zmod.h>
1338313SDina.Nimeh@Sun.Com #include <sys/crypto/common.h>
1348313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h>
1358996SAlok.Aggarwal@Sun.COM #include <LzmaDec.h>
1368313SDina.Nimeh@Sun.Com 
1378313SDina.Nimeh@Sun.Com /*
1388313SDina.Nimeh@Sun.Com  * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
1398313SDina.Nimeh@Sun.Com  * Crypto metadata, if it exists, is located at the end of the boot block
1408313SDina.Nimeh@Sun.Com  * (BBOFF + BBSIZE, which is SBOFF).  The super block and everything after
1418313SDina.Nimeh@Sun.Com  * is offset by the size of the crypto metadata which is handled by
1428313SDina.Nimeh@Sun.Com  * lsp->ls_crypto_offset.
1438313SDina.Nimeh@Sun.Com  */
1448313SDina.Nimeh@Sun.Com #define	CRYOFF	((off_t)8192)
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
1475643Saalok #define	SIZE_PROP_NAME		"Size"
1480Sstevel@tonic-gate 
1498313SDina.Nimeh@Sun.Com #define	SETUP_C_DATA(cd, buf, len) 		\
1508313SDina.Nimeh@Sun.Com 	(cd).cd_format = CRYPTO_DATA_RAW;	\
1518313SDina.Nimeh@Sun.Com 	(cd).cd_offset = 0;			\
1528313SDina.Nimeh@Sun.Com 	(cd).cd_miscdata = NULL;		\
1538313SDina.Nimeh@Sun.Com 	(cd).cd_length = (len);			\
1548313SDina.Nimeh@Sun.Com 	(cd).cd_raw.iov_base = (buf);		\
1558313SDina.Nimeh@Sun.Com 	(cd).cd_raw.iov_len = (len);
1568313SDina.Nimeh@Sun.Com 
1578313SDina.Nimeh@Sun.Com #define	UIO_CHECK(uio)	\
1588313SDina.Nimeh@Sun.Com 	if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
1598313SDina.Nimeh@Sun.Com 	    ((uio)->uio_resid % DEV_BSIZE) != 0) { \
1608313SDina.Nimeh@Sun.Com 		return (EINVAL); \
1618313SDina.Nimeh@Sun.Com 	}
1628313SDina.Nimeh@Sun.Com 
1638313SDina.Nimeh@Sun.Com static dev_info_t *lofi_dip = NULL;
1648313SDina.Nimeh@Sun.Com static void *lofi_statep = NULL;
1650Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1690Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1700Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
1710Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
1720Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1730Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
1740Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
1750Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
1760Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
1770Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
1800Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES;
1838313SDina.Nimeh@Sun.Com const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
1840Sstevel@tonic-gate 
1859048Sjrgn.keil@googlemail.com /*
1869048Sjrgn.keil@googlemail.com  * To avoid decompressing data in a compressed segment multiple times
1879048Sjrgn.keil@googlemail.com  * when accessing small parts of a segment's data, we cache and reuse
1889048Sjrgn.keil@googlemail.com  * the uncompressed segment's data.
1899048Sjrgn.keil@googlemail.com  *
1909048Sjrgn.keil@googlemail.com  * A single cached segment is sufficient to avoid lots of duplicate
1919048Sjrgn.keil@googlemail.com  * segment decompress operations. A small cache size also reduces the
1929048Sjrgn.keil@googlemail.com  * memory footprint.
1939048Sjrgn.keil@googlemail.com  *
1949048Sjrgn.keil@googlemail.com  * lofi_max_comp_cache is the maximum number of decompressed data segments
1959048Sjrgn.keil@googlemail.com  * cached for each compressed lofi image. It can be set to 0 to disable
1969048Sjrgn.keil@googlemail.com  * caching.
1979048Sjrgn.keil@googlemail.com  */
1989048Sjrgn.keil@googlemail.com 
1999048Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1;
2009048Sjrgn.keil@googlemail.com 
2015643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
2025643Saalok 	size_t *destlen, int level);
2035643Saalok 
2048996SAlok.Aggarwal@Sun.COM static int lzma_decompress(void *src, size_t srclen, void *dst,
2058996SAlok.Aggarwal@Sun.COM 	size_t *dstlen, int level);
2068996SAlok.Aggarwal@Sun.COM 
2075643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
2085643Saalok 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
2095643Saalok 	{gzip_decompress,	NULL,	6,	"gzip-6"},
2108996SAlok.Aggarwal@Sun.COM 	{gzip_decompress,	NULL,	9,	"gzip-9"},
2118996SAlok.Aggarwal@Sun.COM 	{lzma_decompress,	NULL,	0,	"lzma"}
2125643Saalok };
2135643Saalok 
2148996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2158996SAlok.Aggarwal@Sun.COM static void
2168996SAlok.Aggarwal@Sun.COM *SzAlloc(void *p, size_t size)
2178996SAlok.Aggarwal@Sun.COM {
2188996SAlok.Aggarwal@Sun.COM 	return (kmem_alloc(size, KM_SLEEP));
2198996SAlok.Aggarwal@Sun.COM }
2208996SAlok.Aggarwal@Sun.COM 
2218996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2228996SAlok.Aggarwal@Sun.COM static void
2238996SAlok.Aggarwal@Sun.COM SzFree(void *p, void *address, size_t size)
2248996SAlok.Aggarwal@Sun.COM {
2258996SAlok.Aggarwal@Sun.COM 	kmem_free(address, size);
2268996SAlok.Aggarwal@Sun.COM }
2278996SAlok.Aggarwal@Sun.COM 
2288996SAlok.Aggarwal@Sun.COM static ISzAlloc g_Alloc = { SzAlloc, SzFree };
2298996SAlok.Aggarwal@Sun.COM 
2309048Sjrgn.keil@googlemail.com /*
2319048Sjrgn.keil@googlemail.com  * Free data referenced by the linked list of cached uncompressed
2329048Sjrgn.keil@googlemail.com  * segments.
2339048Sjrgn.keil@googlemail.com  */
2349048Sjrgn.keil@googlemail.com static void
2359048Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp)
2369048Sjrgn.keil@googlemail.com {
2379048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
2389048Sjrgn.keil@googlemail.com 
2399048Sjrgn.keil@googlemail.com 	while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
2409048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
2419048Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
2429048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
2439048Sjrgn.keil@googlemail.com 	}
2449048Sjrgn.keil@googlemail.com 	ASSERT(lsp->ls_comp_cache_count == 0);
2459048Sjrgn.keil@googlemail.com }
2469048Sjrgn.keil@googlemail.com 
2470Sstevel@tonic-gate static int
2480Sstevel@tonic-gate lofi_busy(void)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	minor_t	minor;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
2540Sstevel@tonic-gate 	 * help because the device isn't open.
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
2570Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
2580Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
2590Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2600Sstevel@tonic-gate 			return (EBUSY);
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2640Sstevel@tonic-gate 	return (0);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static int
2680Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2710Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate static int
2750Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2780Sstevel@tonic-gate 	switch (otyp) {
2790Sstevel@tonic-gate 	case OTYP_CHR:
2800Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
2810Sstevel@tonic-gate 		break;
2820Sstevel@tonic-gate 	case OTYP_BLK:
2830Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
2840Sstevel@tonic-gate 		break;
2850Sstevel@tonic-gate 	case OTYP_LYR:
2860Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
2870Sstevel@tonic-gate 		break;
2880Sstevel@tonic-gate 	default:
2890Sstevel@tonic-gate 		return (-1);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	return (0);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static void
2950Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2980Sstevel@tonic-gate 	switch (otyp) {
2990Sstevel@tonic-gate 	case OTYP_CHR:
3000Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
3010Sstevel@tonic-gate 		break;
3020Sstevel@tonic-gate 	case OTYP_BLK:
3030Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
3040Sstevel@tonic-gate 		break;
3050Sstevel@tonic-gate 	case OTYP_LYR:
3060Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
3070Sstevel@tonic-gate 		break;
3080Sstevel@tonic-gate 	default:
3090Sstevel@tonic-gate 		break;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3134451Seschrock static void
3148313SDina.Nimeh@Sun.Com lofi_free_crypto(struct lofi_state *lsp)
3158313SDina.Nimeh@Sun.Com {
3168313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
3178313SDina.Nimeh@Sun.Com 
3188313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
3198313SDina.Nimeh@Sun.Com 		/*
3208313SDina.Nimeh@Sun.Com 		 * Clean up the crypto state so that it doesn't hang around
3218313SDina.Nimeh@Sun.Com 		 * in memory after we are done with it.
3228313SDina.Nimeh@Sun.Com 		 */
3238313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
3248313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3258313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
3268313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3278313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
3288313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
3298313SDina.Nimeh@Sun.Com 
3308313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != NULL) {
3318313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_mech.cm_param,
3328313SDina.Nimeh@Sun.Com 			    lsp->ls_mech.cm_param_len);
3338313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param = NULL;
3348313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param_len = 0;
3358313SDina.Nimeh@Sun.Com 		}
3368313SDina.Nimeh@Sun.Com 
3378313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_param != NULL) {
3388313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_iv_mech.cm_param,
3398313SDina.Nimeh@Sun.Com 			    lsp->ls_iv_mech.cm_param_len);
3408313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param = NULL;
3418313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param_len = 0;
3428313SDina.Nimeh@Sun.Com 		}
3438313SDina.Nimeh@Sun.Com 
3448313SDina.Nimeh@Sun.Com 		mutex_destroy(&lsp->ls_crypto_lock);
3458313SDina.Nimeh@Sun.Com 	}
3468313SDina.Nimeh@Sun.Com }
3478313SDina.Nimeh@Sun.Com 
3488313SDina.Nimeh@Sun.Com static void
3494451Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
3504451Seschrock     cred_t *credp)
3514451Seschrock {
3524451Seschrock 	dev_t	newdev;
3534451Seschrock 	char	namebuf[50];
3544451Seschrock 
3558313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
3568313SDina.Nimeh@Sun.Com 
3578313SDina.Nimeh@Sun.Com 	lofi_free_crypto(lsp);
3588313SDina.Nimeh@Sun.Com 
3594451Seschrock 	if (lsp->ls_vp) {
3605331Samw 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
3615331Samw 		    1, 0, credp, NULL);
3624451Seschrock 		VN_RELE(lsp->ls_vp);
3634451Seschrock 		lsp->ls_vp = NULL;
3644451Seschrock 	}
3654451Seschrock 
3664451Seschrock 	newdev = makedevice(getmajor(dev), minor);
3674451Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
3684451Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
3694451Seschrock 
3704451Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
3714451Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3724451Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
3734451Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3744451Seschrock 
3754451Seschrock 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
3764451Seschrock 	taskq_destroy(lsp->ls_taskq);
3774451Seschrock 	if (lsp->ls_kstat) {
3784451Seschrock 		kstat_delete(lsp->ls_kstat);
3794451Seschrock 		mutex_destroy(&lsp->ls_kstat_lock);
3804451Seschrock 	}
3816791Saalok 
3829048Sjrgn.keil@googlemail.com 	/*
3839048Sjrgn.keil@googlemail.com 	 * Free cached decompressed segment data
3849048Sjrgn.keil@googlemail.com 	 */
3859048Sjrgn.keil@googlemail.com 	lofi_free_comp_cache(lsp);
3869048Sjrgn.keil@googlemail.com 	list_destroy(&lsp->ls_comp_cache);
3879048Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_comp_cache_lock);
3889048Sjrgn.keil@googlemail.com 
3896791Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
3906791Saalok 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
3916791Saalok 		lsp->ls_uncomp_seg_sz = 0;
3926791Saalok 	}
3939048Sjrgn.keil@googlemail.com 
3949048Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_vp_lock);
3959048Sjrgn.keil@googlemail.com 
3964451Seschrock 	ddi_soft_state_free(lofi_statep, minor);
3974451Seschrock }
3984451Seschrock 
3994451Seschrock /*ARGSUSED*/
4000Sstevel@tonic-gate static int
4010Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	minor_t	minor;
4040Sstevel@tonic-gate 	struct lofi_state *lsp;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4070Sstevel@tonic-gate 	minor = getminor(*devp);
4080Sstevel@tonic-gate 	if (minor == 0) {
4090Sstevel@tonic-gate 		/* master control device */
4100Sstevel@tonic-gate 		/* must be opened exclusively */
4110Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
4120Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4130Sstevel@tonic-gate 			return (EINVAL);
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
4160Sstevel@tonic-gate 		if (lsp == NULL) {
4170Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4180Sstevel@tonic-gate 			return (ENXIO);
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 		if (is_opened(lsp)) {
4210Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4220Sstevel@tonic-gate 			return (EBUSY);
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
4250Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4260Sstevel@tonic-gate 		return (0);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
4300Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4310Sstevel@tonic-gate 	if (lsp == NULL) {
4320Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4330Sstevel@tonic-gate 		return (EINVAL);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4364451Seschrock 	if (lsp->ls_vp == NULL) {
4374451Seschrock 		mutex_exit(&lofi_lock);
4384451Seschrock 		return (ENXIO);
4394451Seschrock 	}
4404451Seschrock 
4410Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
4420Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4430Sstevel@tonic-gate 		return (EINVAL);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4470Sstevel@tonic-gate 	return (0);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4504451Seschrock /*ARGSUSED*/
4510Sstevel@tonic-gate static int
4520Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	minor_t	minor;
4550Sstevel@tonic-gate 	struct lofi_state *lsp;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4580Sstevel@tonic-gate 	minor = getminor(dev);
4590Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4600Sstevel@tonic-gate 	if (lsp == NULL) {
4610Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4620Sstevel@tonic-gate 		return (EINVAL);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	mark_closed(lsp, otyp);
4654451Seschrock 
4664451Seschrock 	/*
4676734Sjohnlev 	 * If we forcibly closed the underlying device (li_force), or
4686734Sjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
4696734Sjohnlev 	 * out of the door.
4704451Seschrock 	 */
4716734Sjohnlev 	if (minor != 0 && !is_opened(lsp) &&
4726734Sjohnlev 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
4734451Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
4746734Sjohnlev 
4750Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4760Sstevel@tonic-gate 	return (0);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4798313SDina.Nimeh@Sun.Com /*
4808313SDina.Nimeh@Sun.Com  * Sets the mechanism's initialization vector (IV) if one is needed.
4818313SDina.Nimeh@Sun.Com  * The IV is computed from the data block number.  lsp->ls_mech is
4828313SDina.Nimeh@Sun.Com  * altered so that:
4838313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param_len is set to the IV len.
4848313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param is set to the IV.
4858313SDina.Nimeh@Sun.Com  */
4868313SDina.Nimeh@Sun.Com static int
4878313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
4888313SDina.Nimeh@Sun.Com {
4898313SDina.Nimeh@Sun.Com 	int	ret;
4908313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
4918313SDina.Nimeh@Sun.Com 	char	*iv;
4928313SDina.Nimeh@Sun.Com 	size_t	iv_len;
4938313SDina.Nimeh@Sun.Com 	size_t	min;
4948313SDina.Nimeh@Sun.Com 	void	*data;
4958313SDina.Nimeh@Sun.Com 	size_t	datasz;
4968313SDina.Nimeh@Sun.Com 
4978313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
4988313SDina.Nimeh@Sun.Com 
4998313SDina.Nimeh@Sun.Com 	if (lsp == NULL)
5008313SDina.Nimeh@Sun.Com 		return (CRYPTO_DEVICE_ERROR);
5018313SDina.Nimeh@Sun.Com 
5028313SDina.Nimeh@Sun.Com 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
5038313SDina.Nimeh@Sun.Com 	if (lsp->ls_iv_type == IVM_NONE) {
5048313SDina.Nimeh@Sun.Com 		return (CRYPTO_SUCCESS);
5058313SDina.Nimeh@Sun.Com 	}
5068313SDina.Nimeh@Sun.Com 
5078313SDina.Nimeh@Sun.Com 	/*
5088313SDina.Nimeh@Sun.Com 	 * if kmem already alloced from previous call and it's the same size
5098313SDina.Nimeh@Sun.Com 	 * we need now, just recycle it; allocate new kmem only if we have to
5108313SDina.Nimeh@Sun.Com 	 */
5118313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param == NULL ||
5128313SDina.Nimeh@Sun.Com 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
5138313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_iv_len;
5148313SDina.Nimeh@Sun.Com 		iv = kmem_zalloc(iv_len, KM_SLEEP);
5158313SDina.Nimeh@Sun.Com 	} else {
5168313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_mech.cm_param_len;
5178313SDina.Nimeh@Sun.Com 		iv = lsp->ls_mech.cm_param;
5188313SDina.Nimeh@Sun.Com 		bzero(iv, iv_len);
5198313SDina.Nimeh@Sun.Com 	}
5208313SDina.Nimeh@Sun.Com 
5218313SDina.Nimeh@Sun.Com 	switch (lsp->ls_iv_type) {
5228313SDina.Nimeh@Sun.Com 	case IVM_ENC_BLKNO:
5238313SDina.Nimeh@Sun.Com 		/* iv is not static, lblkno changes each time */
5248313SDina.Nimeh@Sun.Com 		data = &lblkno;
5258313SDina.Nimeh@Sun.Com 		datasz = sizeof (lblkno);
5268313SDina.Nimeh@Sun.Com 		break;
5278313SDina.Nimeh@Sun.Com 	default:
5288313SDina.Nimeh@Sun.Com 		data = 0;
5298313SDina.Nimeh@Sun.Com 		datasz = 0;
5308313SDina.Nimeh@Sun.Com 		break;
5318313SDina.Nimeh@Sun.Com 	}
5328313SDina.Nimeh@Sun.Com 
5338313SDina.Nimeh@Sun.Com 	/*
5348313SDina.Nimeh@Sun.Com 	 * write blkno into the iv buffer padded on the left in case
5358313SDina.Nimeh@Sun.Com 	 * blkno ever grows bigger than its current longlong_t size
5368313SDina.Nimeh@Sun.Com 	 * or a variation other than blkno is used for the iv data
5378313SDina.Nimeh@Sun.Com 	 */
5388313SDina.Nimeh@Sun.Com 	min = MIN(datasz, iv_len);
5398313SDina.Nimeh@Sun.Com 	bcopy(data, iv + (iv_len - min), min);
5408313SDina.Nimeh@Sun.Com 
5418313SDina.Nimeh@Sun.Com 	/* encrypt the data in-place to get the IV */
5428313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, iv, iv_len);
5438313SDina.Nimeh@Sun.Com 
5448313SDina.Nimeh@Sun.Com 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
5458313SDina.Nimeh@Sun.Com 	    NULL, NULL, NULL);
5468313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
5478313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
5488313SDina.Nimeh@Sun.Com 		    lblkno, ret);
5498313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != iv)
5508313SDina.Nimeh@Sun.Com 			kmem_free(iv, iv_len);
5518996SAlok.Aggarwal@Sun.COM 
5528313SDina.Nimeh@Sun.Com 		return (ret);
5538313SDina.Nimeh@Sun.Com 	}
5548313SDina.Nimeh@Sun.Com 
5558313SDina.Nimeh@Sun.Com 	/* clean up the iv from the last computation */
5568313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
5578313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
5588996SAlok.Aggarwal@Sun.COM 
5598313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param_len = iv_len;
5608313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param = iv;
5618313SDina.Nimeh@Sun.Com 
5628313SDina.Nimeh@Sun.Com 	return (CRYPTO_SUCCESS);
5638313SDina.Nimeh@Sun.Com }
5648313SDina.Nimeh@Sun.Com 
5658313SDina.Nimeh@Sun.Com /*
5668313SDina.Nimeh@Sun.Com  * Performs encryption and decryption of a chunk of data of size "len",
5678313SDina.Nimeh@Sun.Com  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
5688313SDina.Nimeh@Sun.Com  * DEV_BSIZE.
5698313SDina.Nimeh@Sun.Com  */
5708313SDina.Nimeh@Sun.Com static int
5718313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
5728313SDina.Nimeh@Sun.Com     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
5738313SDina.Nimeh@Sun.Com {
5748313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
5758313SDina.Nimeh@Sun.Com 	crypto_data_t wdata;
5768313SDina.Nimeh@Sun.Com 	int ret;
5778313SDina.Nimeh@Sun.Com 	longlong_t lblkno = bp->b_lblkno;
5788313SDina.Nimeh@Sun.Com 
5798313SDina.Nimeh@Sun.Com 	mutex_enter(&lsp->ls_crypto_lock);
5808313SDina.Nimeh@Sun.Com 
5818313SDina.Nimeh@Sun.Com 	/*
5828313SDina.Nimeh@Sun.Com 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
5838313SDina.Nimeh@Sun.Com 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
5848313SDina.Nimeh@Sun.Com 	 */
5858313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, plaintext, len);
5868313SDina.Nimeh@Sun.Com 	cdata.cd_length = DEV_BSIZE;
5878313SDina.Nimeh@Sun.Com 	if (ciphertext != NULL) {		/* not in-place crypto */
5888313SDina.Nimeh@Sun.Com 		SETUP_C_DATA(wdata, ciphertext, len);
5898313SDina.Nimeh@Sun.Com 		wdata.cd_length = DEV_BSIZE;
5908313SDina.Nimeh@Sun.Com 	}
5918313SDina.Nimeh@Sun.Com 
5928313SDina.Nimeh@Sun.Com 	do {
5938313SDina.Nimeh@Sun.Com 		ret = lofi_blk_mech(lsp, lblkno);
5948313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS)
5958313SDina.Nimeh@Sun.Com 			continue;
5968313SDina.Nimeh@Sun.Com 
5978313SDina.Nimeh@Sun.Com 		if (op_encrypt) {
5988313SDina.Nimeh@Sun.Com 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
5998313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
6008313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6018313SDina.Nimeh@Sun.Com 		} else {
6028313SDina.Nimeh@Sun.Com 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
6038313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
6048313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6058313SDina.Nimeh@Sun.Com 		}
6068313SDina.Nimeh@Sun.Com 
6078313SDina.Nimeh@Sun.Com 		cdata.cd_offset += DEV_BSIZE;
6088313SDina.Nimeh@Sun.Com 		if (ciphertext != NULL)
6098313SDina.Nimeh@Sun.Com 			wdata.cd_offset += DEV_BSIZE;
6108313SDina.Nimeh@Sun.Com 		lblkno++;
6118313SDina.Nimeh@Sun.Com 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
6128313SDina.Nimeh@Sun.Com 
6138313SDina.Nimeh@Sun.Com 	mutex_exit(&lsp->ls_crypto_lock);
6148313SDina.Nimeh@Sun.Com 
6158313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
6168313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
6178313SDina.Nimeh@Sun.Com 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
6188313SDina.Nimeh@Sun.Com 		    lblkno, ret);
6198313SDina.Nimeh@Sun.Com 	}
6208313SDina.Nimeh@Sun.Com 
6218313SDina.Nimeh@Sun.Com 	return (ret);
6228313SDina.Nimeh@Sun.Com }
6238313SDina.Nimeh@Sun.Com 
6248313SDina.Nimeh@Sun.Com #define	RDWR_RAW	1
6258313SDina.Nimeh@Sun.Com #define	RDWR_BCOPY	2
6268313SDina.Nimeh@Sun.Com 
6278313SDina.Nimeh@Sun.Com static int
6288313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6298313SDina.Nimeh@Sun.Com     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
6308313SDina.Nimeh@Sun.Com {
6318313SDina.Nimeh@Sun.Com 	ssize_t resid;
6328313SDina.Nimeh@Sun.Com 	int isread;
6338313SDina.Nimeh@Sun.Com 	int error;
6348313SDina.Nimeh@Sun.Com 
6358313SDina.Nimeh@Sun.Com 	/*
6368313SDina.Nimeh@Sun.Com 	 * Handles reads/writes for both plain and encrypted lofi
6378313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
6388313SDina.Nimeh@Sun.Com 	 * when it gets here.
6398313SDina.Nimeh@Sun.Com 	 */
6408313SDina.Nimeh@Sun.Com 
6418313SDina.Nimeh@Sun.Com 	isread = bp->b_flags & B_READ;
6428313SDina.Nimeh@Sun.Com 	if (isread) {
6438313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6448313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6458313SDina.Nimeh@Sun.Com 			bcopy(bcopy_locn, bufaddr, len);
6468313SDina.Nimeh@Sun.Com 			error = 0;
6478313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
6488313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
6498313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6508313SDina.Nimeh@Sun.Com 			    &resid);
6518313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
6528313SDina.Nimeh@Sun.Com 		}
6538313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled && error == 0) {
6548313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
6558313SDina.Nimeh@Sun.Com 			    B_FALSE) != CRYPTO_SUCCESS) {
6568313SDina.Nimeh@Sun.Com 				/*
6578313SDina.Nimeh@Sun.Com 				 * XXX: original code didn't set residual
6588313SDina.Nimeh@Sun.Com 				 * back to len because no error was expected
6598313SDina.Nimeh@Sun.Com 				 * from bcopy() if encryption is not enabled
6608313SDina.Nimeh@Sun.Com 				 */
6618313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6628313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6638313SDina.Nimeh@Sun.Com 				error = EIO;
6648313SDina.Nimeh@Sun.Com 			}
6658313SDina.Nimeh@Sun.Com 		}
6668313SDina.Nimeh@Sun.Com 		return (error);
6678313SDina.Nimeh@Sun.Com 	} else {
6688313SDina.Nimeh@Sun.Com 		void *iobuf = bufaddr;
6698313SDina.Nimeh@Sun.Com 
6708313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6718313SDina.Nimeh@Sun.Com 			/* don't do in-place crypto to keep bufaddr intact */
6728313SDina.Nimeh@Sun.Com 			iobuf = kmem_alloc(len, KM_SLEEP);
6738313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
6748313SDina.Nimeh@Sun.Com 			    B_TRUE) != CRYPTO_SUCCESS) {
6758313SDina.Nimeh@Sun.Com 				kmem_free(iobuf, len);
6768313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6778313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6788313SDina.Nimeh@Sun.Com 				return (EIO);
6798313SDina.Nimeh@Sun.Com 			}
6808313SDina.Nimeh@Sun.Com 		}
6818313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6828313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6838313SDina.Nimeh@Sun.Com 			bcopy(iobuf, bcopy_locn, len);
6848313SDina.Nimeh@Sun.Com 			error = 0;
6858313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
6868313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
6878313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6888313SDina.Nimeh@Sun.Com 			    &resid);
6898313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
6908313SDina.Nimeh@Sun.Com 		}
6918313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6928313SDina.Nimeh@Sun.Com 			kmem_free(iobuf, len);
6938313SDina.Nimeh@Sun.Com 		}
6948313SDina.Nimeh@Sun.Com 		return (error);
6958313SDina.Nimeh@Sun.Com 	}
6968313SDina.Nimeh@Sun.Com }
6978313SDina.Nimeh@Sun.Com 
6985643Saalok static int
6995643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
7008313SDina.Nimeh@Sun.Com     struct lofi_state *lsp)
7015643Saalok {
7025643Saalok 	int error;
7035643Saalok 	offset_t alignedoffset, mapoffset;
7045643Saalok 	size_t	xfersize;
7055643Saalok 	int	isread;
7068313SDina.Nimeh@Sun.Com 	int	smflags;
7075643Saalok 	caddr_t	mapaddr;
7085643Saalok 	size_t	len;
7095643Saalok 	enum seg_rw srw;
7108313SDina.Nimeh@Sun.Com 	int	save_error;
7118313SDina.Nimeh@Sun.Com 
7128313SDina.Nimeh@Sun.Com 	/*
7138313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
7148313SDina.Nimeh@Sun.Com 	 * when it gets here.
7158313SDina.Nimeh@Sun.Com 	 */
7168313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled)
7178313SDina.Nimeh@Sun.Com 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
7185643Saalok 
7195643Saalok 	/*
7205643Saalok 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
7215643Saalok 	 * an 8K boundary, but the buf transfer address may not be
7225643Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
7235643Saalok 	 * that even though we could). This matters since the initial
7245643Saalok 	 * part of the transfer may not start at offset 0 within the
7255643Saalok 	 * segmap'd chunk. So we have to compensate for that with
7265643Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
7275643Saalok 	 * beginning, and the last is capped by b_resid
7288313SDina.Nimeh@Sun.Com 	 *
7298313SDina.Nimeh@Sun.Com 	 * Visually, where "|" represents page map boundaries:
7308313SDina.Nimeh@Sun.Com 	 *   alignedoffset (mapaddr begins at this segmap boundary)
7318313SDina.Nimeh@Sun.Com 	 *    |   offset (from beginning of file)
7328313SDina.Nimeh@Sun.Com 	 *    |    |	   len
7338313SDina.Nimeh@Sun.Com 	 *    v    v	    v
7348313SDina.Nimeh@Sun.Com 	 * ===|====X========|====...======|========X====|====
7358313SDina.Nimeh@Sun.Com 	 *	   /-------------...---------------/
7368313SDina.Nimeh@Sun.Com 	 *		^ bp->b_bcount/bp->b_resid at start
7378313SDina.Nimeh@Sun.Com 	 *    /----/--------/----...------/--------/
7388313SDina.Nimeh@Sun.Com 	 *	^	^	^   ^		^
7398313SDina.Nimeh@Sun.Com 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
7408313SDina.Nimeh@Sun.Com 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
7418313SDina.Nimeh@Sun.Com 	 *	|	1st xfersize (<= MAXBSIZE)
7428313SDina.Nimeh@Sun.Com 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
7438313SDina.Nimeh@Sun.Com 	 *
7448313SDina.Nimeh@Sun.Com 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
7458313SDina.Nimeh@Sun.Com 	 * MAXBSIZE boundary.  "len" is next page boundary of size
7468719SDina.Nimeh@Sun.COM 	 * PAGESIZE after "alignedoffset".
7475643Saalok 	 */
7485643Saalok 	mapoffset = offset & MAXBOFFSET;
7495643Saalok 	alignedoffset = offset - mapoffset;
7505643Saalok 	bp->b_resid = bp->b_bcount;
7515643Saalok 	isread = bp->b_flags & B_READ;
7525643Saalok 	srw = isread ? S_READ : S_WRITE;
7535643Saalok 	do {
7545643Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
7555643Saalok 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
7568719SDina.Nimeh@Sun.COM 		len = roundup(mapoffset + xfersize, PAGESIZE);
7575643Saalok 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
7585643Saalok 		    alignedoffset, MAXBSIZE, 1, srw);
7595643Saalok 		/*
7605643Saalok 		 * Now fault in the pages. This lets us check
7615643Saalok 		 * for errors before we reference mapaddr and
7625643Saalok 		 * try to resolve the fault in bcopy (which would
7635643Saalok 		 * panic instead). And this can easily happen,
7645643Saalok 		 * particularly if you've lofi'd a file over NFS
7655643Saalok 		 * and someone deletes the file on the server.
7665643Saalok 		 */
7675643Saalok 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
7685643Saalok 		    len, F_SOFTLOCK, srw);
7695643Saalok 		if (error) {
7705643Saalok 			(void) segmap_release(segkmap, mapaddr, 0);
7715643Saalok 			if (FC_CODE(error) == FC_OBJERR)
7725643Saalok 				error = FC_ERRNO(error);
7735643Saalok 			else
7745643Saalok 				error = EIO;
7755643Saalok 			break;
7765643Saalok 		}
7778313SDina.Nimeh@Sun.Com 		/* error may be non-zero for encrypted lofi */
7788313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
7798313SDina.Nimeh@Sun.Com 		    RDWR_BCOPY, mapaddr + mapoffset);
7808313SDina.Nimeh@Sun.Com 		if (error == 0) {
7818313SDina.Nimeh@Sun.Com 			bp->b_resid -= xfersize;
7828313SDina.Nimeh@Sun.Com 			bufaddr += xfersize;
7838313SDina.Nimeh@Sun.Com 			offset += xfersize;
7848313SDina.Nimeh@Sun.Com 		}
7855643Saalok 		smflags = 0;
7865643Saalok 		if (isread) {
7875643Saalok 			smflags |= SM_FREE;
7885643Saalok 			/*
7895643Saalok 			 * If we're reading an entire page starting
7905643Saalok 			 * at a page boundary, there's a good chance
7915643Saalok 			 * we won't need it again. Put it on the
7925643Saalok 			 * head of the freelist.
7935643Saalok 			 */
7948056SDina.Nimeh@Sun.Com 			if (mapoffset == 0 && xfersize == MAXBSIZE)
7955643Saalok 				smflags |= SM_DONTNEED;
7965643Saalok 		} else {
7978313SDina.Nimeh@Sun.Com 			if (error == 0)		/* write back good pages */
7988313SDina.Nimeh@Sun.Com 				smflags |= SM_WRITE;
7995643Saalok 		}
8005643Saalok 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
8015643Saalok 		    len, F_SOFTUNLOCK, srw);
8028313SDina.Nimeh@Sun.Com 		save_error = segmap_release(segkmap, mapaddr, smflags);
8038313SDina.Nimeh@Sun.Com 		if (error == 0)
8048313SDina.Nimeh@Sun.Com 			error = save_error;
8055643Saalok 		/* only the first map may start partial */
8065643Saalok 		mapoffset = 0;
8075643Saalok 		alignedoffset += MAXBSIZE;
8085643Saalok 	} while ((error == 0) && (bp->b_resid > 0) &&
8095643Saalok 	    (offset < lsp->ls_vp_comp_size));
8105643Saalok 
8115643Saalok 	return (error);
8125643Saalok }
8135643Saalok 
8149048Sjrgn.keil@googlemail.com /*
8159048Sjrgn.keil@googlemail.com  * Check if segment seg_index is present in the decompressed segment
8169048Sjrgn.keil@googlemail.com  * data cache.
8179048Sjrgn.keil@googlemail.com  *
8189048Sjrgn.keil@googlemail.com  * Returns a pointer to the decompressed segment data cache entry if
8199048Sjrgn.keil@googlemail.com  * found, and NULL when decompressed data for this segment is not yet
8209048Sjrgn.keil@googlemail.com  * cached.
8219048Sjrgn.keil@googlemail.com  */
8229048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8239048Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
8249048Sjrgn.keil@googlemail.com {
8259048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8269048Sjrgn.keil@googlemail.com 
8279048Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8289048Sjrgn.keil@googlemail.com 
8299048Sjrgn.keil@googlemail.com 	for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
8309048Sjrgn.keil@googlemail.com 	    lc = list_next(&lsp->ls_comp_cache, lc)) {
8319048Sjrgn.keil@googlemail.com 		if (lc->lc_index == seg_index) {
8329048Sjrgn.keil@googlemail.com 			/*
8339048Sjrgn.keil@googlemail.com 			 * Decompressed segment data was found in the
8349048Sjrgn.keil@googlemail.com 			 * cache.
8359048Sjrgn.keil@googlemail.com 			 *
8369048Sjrgn.keil@googlemail.com 			 * The cache uses an LRU replacement strategy;
8379048Sjrgn.keil@googlemail.com 			 * move the entry to head of list.
8389048Sjrgn.keil@googlemail.com 			 */
8399048Sjrgn.keil@googlemail.com 			list_remove(&lsp->ls_comp_cache, lc);
8409048Sjrgn.keil@googlemail.com 			list_insert_head(&lsp->ls_comp_cache, lc);
8419048Sjrgn.keil@googlemail.com 			return (lc);
8429048Sjrgn.keil@googlemail.com 		}
8439048Sjrgn.keil@googlemail.com 	}
8449048Sjrgn.keil@googlemail.com 	return (NULL);
8459048Sjrgn.keil@googlemail.com }
8469048Sjrgn.keil@googlemail.com 
8479048Sjrgn.keil@googlemail.com /*
8489048Sjrgn.keil@googlemail.com  * Add the data for a decompressed segment at segment index
8499048Sjrgn.keil@googlemail.com  * seg_index to the cache of the decompressed segments.
8509048Sjrgn.keil@googlemail.com  *
8519048Sjrgn.keil@googlemail.com  * Returns a pointer to the cache element structure in case
8529048Sjrgn.keil@googlemail.com  * the data was added to the cache; returns NULL when the data
8539048Sjrgn.keil@googlemail.com  * wasn't cached.
8549048Sjrgn.keil@googlemail.com  */
8559048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8569048Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
8579048Sjrgn.keil@googlemail.com     uchar_t *data)
8589048Sjrgn.keil@googlemail.com {
8599048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8609048Sjrgn.keil@googlemail.com 
8619048Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8629048Sjrgn.keil@googlemail.com 
8639048Sjrgn.keil@googlemail.com 	while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
8649048Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8659048Sjrgn.keil@googlemail.com 		ASSERT(lc != NULL);
8669048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
8679048Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
8689048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
8699048Sjrgn.keil@googlemail.com 	}
8709048Sjrgn.keil@googlemail.com 
8719048Sjrgn.keil@googlemail.com 	/*
8729048Sjrgn.keil@googlemail.com 	 * Do not cache when disabled by tunable variable
8739048Sjrgn.keil@googlemail.com 	 */
8749048Sjrgn.keil@googlemail.com 	if (lofi_max_comp_cache == 0)
8759048Sjrgn.keil@googlemail.com 		return (NULL);
8769048Sjrgn.keil@googlemail.com 
8779048Sjrgn.keil@googlemail.com 	/*
8789048Sjrgn.keil@googlemail.com 	 * When the cache has not yet reached the maximum allowed
8799048Sjrgn.keil@googlemail.com 	 * number of segments, allocate a new cache element.
8809048Sjrgn.keil@googlemail.com 	 * Otherwise the cache is full; reuse the last list element
8819048Sjrgn.keil@googlemail.com 	 * (LRU) for caching the decompressed segment data.
8829048Sjrgn.keil@googlemail.com 	 *
8839048Sjrgn.keil@googlemail.com 	 * The cache element for the new decompressed segment data is
8849048Sjrgn.keil@googlemail.com 	 * added to the head of the list.
8859048Sjrgn.keil@googlemail.com 	 */
8869048Sjrgn.keil@googlemail.com 	if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
8879048Sjrgn.keil@googlemail.com 		lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
8889048Sjrgn.keil@googlemail.com 		lc->lc_data = NULL;
8899048Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
8909048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count++;
8919048Sjrgn.keil@googlemail.com 	} else {
8929048Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8939048Sjrgn.keil@googlemail.com 		if (lc == NULL)
8949048Sjrgn.keil@googlemail.com 			return (NULL);
8959048Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
8969048Sjrgn.keil@googlemail.com 	}
8979048Sjrgn.keil@googlemail.com 
8989048Sjrgn.keil@googlemail.com 	/*
8999048Sjrgn.keil@googlemail.com 	 * Free old uncompressed segment data when reusing a cache
9009048Sjrgn.keil@googlemail.com 	 * entry.
9019048Sjrgn.keil@googlemail.com 	 */
9029048Sjrgn.keil@googlemail.com 	if (lc->lc_data != NULL)
9039048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
9049048Sjrgn.keil@googlemail.com 
9059048Sjrgn.keil@googlemail.com 	lc->lc_data = data;
9069048Sjrgn.keil@googlemail.com 	lc->lc_index = seg_index;
9079048Sjrgn.keil@googlemail.com 	return (lc);
9089048Sjrgn.keil@googlemail.com }
9099048Sjrgn.keil@googlemail.com 
9109048Sjrgn.keil@googlemail.com 
9115643Saalok /*ARGSUSED*/
9128996SAlok.Aggarwal@Sun.COM static int
9138996SAlok.Aggarwal@Sun.COM gzip_decompress(void *src, size_t srclen, void *dst,
9145643Saalok     size_t *dstlen, int level)
9155643Saalok {
9165643Saalok 	ASSERT(*dstlen >= srclen);
9175643Saalok 
9185643Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
9195643Saalok 		return (-1);
9205643Saalok 	return (0);
9215643Saalok }
9225643Saalok 
9238996SAlok.Aggarwal@Sun.COM #define	LZMA_HEADER_SIZE	(LZMA_PROPS_SIZE + 8)
9248996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
9258996SAlok.Aggarwal@Sun.COM static int
9268996SAlok.Aggarwal@Sun.COM lzma_decompress(void *src, size_t srclen, void *dst,
9278996SAlok.Aggarwal@Sun.COM 	size_t *dstlen, int level)
9288996SAlok.Aggarwal@Sun.COM {
9298996SAlok.Aggarwal@Sun.COM 	size_t insizepure;
9308996SAlok.Aggarwal@Sun.COM 	void *actual_src;
9318996SAlok.Aggarwal@Sun.COM 	ELzmaStatus status;
9328996SAlok.Aggarwal@Sun.COM 
9338996SAlok.Aggarwal@Sun.COM 	insizepure = srclen - LZMA_HEADER_SIZE;
9348996SAlok.Aggarwal@Sun.COM 	actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
9358996SAlok.Aggarwal@Sun.COM 
9368996SAlok.Aggarwal@Sun.COM 	if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
9378996SAlok.Aggarwal@Sun.COM 	    (const Byte *)actual_src, &insizepure,
9388996SAlok.Aggarwal@Sun.COM 	    (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
9398996SAlok.Aggarwal@Sun.COM 	    &g_Alloc) != SZ_OK) {
9408996SAlok.Aggarwal@Sun.COM 		return (-1);
9418996SAlok.Aggarwal@Sun.COM 	}
9428996SAlok.Aggarwal@Sun.COM 	return (0);
9438996SAlok.Aggarwal@Sun.COM }
9448996SAlok.Aggarwal@Sun.COM 
9450Sstevel@tonic-gate /*
9460Sstevel@tonic-gate  * This is basically what strategy used to be before we found we
9470Sstevel@tonic-gate  * needed task queues.
9480Sstevel@tonic-gate  */
9490Sstevel@tonic-gate static void
9500Sstevel@tonic-gate lofi_strategy_task(void *arg)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	struct buf *bp = (struct buf *)arg;
9530Sstevel@tonic-gate 	int error;
9540Sstevel@tonic-gate 	struct lofi_state *lsp;
9558313SDina.Nimeh@Sun.Com 	offset_t offset;
9568313SDina.Nimeh@Sun.Com 	caddr_t	bufaddr;
9578313SDina.Nimeh@Sun.Com 	size_t	len;
9588313SDina.Nimeh@Sun.Com 	size_t	xfersize;
9598313SDina.Nimeh@Sun.Com 	boolean_t bufinited = B_FALSE;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
9628313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
9638313SDina.Nimeh@Sun.Com 		error = ENXIO;
9648313SDina.Nimeh@Sun.Com 		goto errout;
9658313SDina.Nimeh@Sun.Com 	}
9660Sstevel@tonic-gate 	if (lsp->ls_kstat) {
9670Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
9680Sstevel@tonic-gate 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
9690Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	bp_mapin(bp);
9720Sstevel@tonic-gate 	bufaddr = bp->b_un.b_addr;
9730Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
9748313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
9758313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
9768313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
9778313SDina.Nimeh@Sun.Com 	}
9788313SDina.Nimeh@Sun.Com 	len = bp->b_bcount;
9798313SDina.Nimeh@Sun.Com 	bufinited = B_TRUE;
9808313SDina.Nimeh@Sun.Com 
9818313SDina.Nimeh@Sun.Com 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
9828313SDina.Nimeh@Sun.Com 		error = EIO;
9838313SDina.Nimeh@Sun.Com 		goto errout;
9848313SDina.Nimeh@Sun.Com 	}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	/*
9870Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
9880Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
9890Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
9900Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
9918313SDina.Nimeh@Sun.Com 	 * mapable or it's a character device or it's an encrypted lofi.
9920Sstevel@tonic-gate 	 */
9938313SDina.Nimeh@Sun.Com 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
9948313SDina.Nimeh@Sun.Com 	    lsp->ls_crypto_enabled) {
9958313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
9968313SDina.Nimeh@Sun.Com 		    NULL);
9978313SDina.Nimeh@Sun.Com 	} else if (lsp->ls_uncomp_seg_sz == 0) {
9988313SDina.Nimeh@Sun.Com 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
9998313SDina.Nimeh@Sun.Com 	} else {
10009048Sjrgn.keil@googlemail.com 		uchar_t *compressed_seg = NULL, *cmpbuf;
10019048Sjrgn.keil@googlemail.com 		uchar_t *uncompressed_seg = NULL;
10028313SDina.Nimeh@Sun.Com 		lofi_compress_info_t *li;
10038313SDina.Nimeh@Sun.Com 		size_t oblkcount;
10049048Sjrgn.keil@googlemail.com 		ulong_t seglen;
10058313SDina.Nimeh@Sun.Com 		uint64_t sblkno, eblkno, cmpbytes;
10069048Sjrgn.keil@googlemail.com 		uint64_t uncompressed_seg_index;
10079048Sjrgn.keil@googlemail.com 		struct lofi_comp_cache *lc;
10088313SDina.Nimeh@Sun.Com 		offset_t sblkoff, eblkoff;
10098313SDina.Nimeh@Sun.Com 		u_offset_t salign, ealign;
10108313SDina.Nimeh@Sun.Com 		u_offset_t sdiff;
10118313SDina.Nimeh@Sun.Com 		uint32_t comp_data_sz;
10125643Saalok 		uint64_t i;
10135643Saalok 
10140Sstevel@tonic-gate 		/*
10155643Saalok 		 * From here on we're dealing primarily with compressed files
10165643Saalok 		 */
10178313SDina.Nimeh@Sun.Com 		ASSERT(!lsp->ls_crypto_enabled);
10185643Saalok 
10195643Saalok 		/*
10205643Saalok 		 * Compressed files can only be read from and
10215643Saalok 		 * not written to
10220Sstevel@tonic-gate 		 */
10235643Saalok 		if (!(bp->b_flags & B_READ)) {
10245643Saalok 			bp->b_resid = bp->b_bcount;
10255643Saalok 			error = EROFS;
10265643Saalok 			goto done;
10275643Saalok 		}
10285643Saalok 
10295643Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
10305643Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
10315643Saalok 		/*
10325643Saalok 		 * Compute starting and ending compressed segment numbers
10335643Saalok 		 * We use only bitwise operations avoiding division and
10345643Saalok 		 * modulus because we enforce the compression segment size
10355643Saalok 		 * to a power of 2
10365643Saalok 		 */
10375643Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
10385643Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
10395643Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
10405643Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
10415643Saalok 
10425643Saalok 		/*
10439048Sjrgn.keil@googlemail.com 		 * Check the decompressed segment cache.
10449048Sjrgn.keil@googlemail.com 		 *
10459048Sjrgn.keil@googlemail.com 		 * The cache is used only when the requested data
10469048Sjrgn.keil@googlemail.com 		 * is within a segment. Requests that cross
10479048Sjrgn.keil@googlemail.com 		 * segment boundaries bypass the cache.
10489048Sjrgn.keil@googlemail.com 		 */
10499048Sjrgn.keil@googlemail.com 		if (sblkno == eblkno ||
10509048Sjrgn.keil@googlemail.com 		    (sblkno + 1 == eblkno && eblkoff == 0)) {
10519048Sjrgn.keil@googlemail.com 			/*
10529048Sjrgn.keil@googlemail.com 			 * Request doesn't cross a segment boundary,
10539048Sjrgn.keil@googlemail.com 			 * now check the cache.
10549048Sjrgn.keil@googlemail.com 			 */
10559048Sjrgn.keil@googlemail.com 			mutex_enter(&lsp->ls_comp_cache_lock);
10569048Sjrgn.keil@googlemail.com 			lc = lofi_find_comp_data(lsp, sblkno);
10579048Sjrgn.keil@googlemail.com 			if (lc != NULL) {
10589048Sjrgn.keil@googlemail.com 				/*
10599048Sjrgn.keil@googlemail.com 				 * We've found the decompressed segment
10609048Sjrgn.keil@googlemail.com 				 * data in the cache; reuse it.
10619048Sjrgn.keil@googlemail.com 				 */
10629048Sjrgn.keil@googlemail.com 				bcopy(lc->lc_data + sblkoff, bufaddr,
10639048Sjrgn.keil@googlemail.com 				    bp->b_bcount);
10649048Sjrgn.keil@googlemail.com 				mutex_exit(&lsp->ls_comp_cache_lock);
10659048Sjrgn.keil@googlemail.com 				bp->b_resid = 0;
10669048Sjrgn.keil@googlemail.com 				error = 0;
10679048Sjrgn.keil@googlemail.com 				goto done;
10689048Sjrgn.keil@googlemail.com 			}
10699048Sjrgn.keil@googlemail.com 			mutex_exit(&lsp->ls_comp_cache_lock);
10709048Sjrgn.keil@googlemail.com 		}
10719048Sjrgn.keil@googlemail.com 
10729048Sjrgn.keil@googlemail.com 		/*
10735643Saalok 		 * Align start offset to block boundary for segmap
10745643Saalok 		 */
10755643Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
10765643Saalok 		sdiff = salign & (DEV_BSIZE - 1);
10775643Saalok 		salign -= sdiff;
10785643Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
10790Sstevel@tonic-gate 			/*
10805643Saalok 			 * We're dealing with the last segment of
10815643Saalok 			 * the compressed file -- the size of this
10825643Saalok 			 * segment *may not* be the same as the
10835643Saalok 			 * segment size for the file
10840Sstevel@tonic-gate 			 */
10855643Saalok 			eblkoff = (offset + bp->b_bcount) &
10865643Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
10875643Saalok 			ealign = lsp->ls_vp_comp_size;
10885643Saalok 		} else {
10895643Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
10905643Saalok 		}
10915643Saalok 
10925643Saalok 		/*
10935643Saalok 		 * Preserve original request paramaters
10945643Saalok 		 */
10955643Saalok 		oblkcount = bp->b_bcount;
10965643Saalok 
10975643Saalok 		/*
10985643Saalok 		 * Assign the calculated parameters
10995643Saalok 		 */
11005643Saalok 		comp_data_sz = ealign - salign;
11015643Saalok 		bp->b_bcount = comp_data_sz;
11025643Saalok 
11035643Saalok 		/*
11045643Saalok 		 * Allocate fixed size memory blocks to hold compressed
11055643Saalok 		 * segments and one uncompressed segment since we
11065643Saalok 		 * uncompress segments one at a time
11075643Saalok 		 */
11085643Saalok 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
11095643Saalok 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
11105643Saalok 		/*
11115643Saalok 		 * Map in the calculated number of blocks
11125643Saalok 		 */
11135643Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
11145643Saalok 		    bp, lsp);
11155643Saalok 
11165643Saalok 		bp->b_bcount = oblkcount;
11175643Saalok 		bp->b_resid = oblkcount;
11185643Saalok 		if (error != 0)
11195643Saalok 			goto done;
11205643Saalok 
11215643Saalok 		/*
11225643Saalok 		 * We have the compressed blocks, now uncompress them
11235643Saalok 		 */
11245643Saalok 		cmpbuf = compressed_seg + sdiff;
11258996SAlok.Aggarwal@Sun.COM 		for (i = sblkno; i <= eblkno; i++) {
11268996SAlok.Aggarwal@Sun.COM 			ASSERT(i < lsp->ls_comp_index_sz - 1);
11278996SAlok.Aggarwal@Sun.COM 
11288996SAlok.Aggarwal@Sun.COM 			/*
11298996SAlok.Aggarwal@Sun.COM 			 * The last segment is special in that it is
11308996SAlok.Aggarwal@Sun.COM 			 * most likely not going to be the same
11318996SAlok.Aggarwal@Sun.COM 			 * (uncompressed) size as the other segments.
11328996SAlok.Aggarwal@Sun.COM 			 */
11338996SAlok.Aggarwal@Sun.COM 			if (i == (lsp->ls_comp_index_sz - 2)) {
11348996SAlok.Aggarwal@Sun.COM 				seglen = lsp->ls_uncomp_last_seg_sz;
11358996SAlok.Aggarwal@Sun.COM 			} else {
11368996SAlok.Aggarwal@Sun.COM 				seglen = lsp->ls_uncomp_seg_sz;
11378996SAlok.Aggarwal@Sun.COM 			}
11388996SAlok.Aggarwal@Sun.COM 
11395643Saalok 			/*
11405643Saalok 			 * Each of the segment index entries contains
11415643Saalok 			 * the starting block number for that segment.
11425643Saalok 			 * The number of compressed bytes in a segment
11435643Saalok 			 * is thus the difference between the starting
11445643Saalok 			 * block number of this segment and the starting
11455643Saalok 			 * block number of the next segment.
11465643Saalok 			 */
11478996SAlok.Aggarwal@Sun.COM 			cmpbytes = lsp->ls_comp_seg_index[i + 1] -
11488996SAlok.Aggarwal@Sun.COM 			    lsp->ls_comp_seg_index[i];
11495643Saalok 
11505643Saalok 			/*
11515643Saalok 			 * The first byte in a compressed segment is a flag
11525643Saalok 			 * that indicates whether this segment is compressed
11535643Saalok 			 * at all
11545643Saalok 			 */
11555643Saalok 			if (*cmpbuf == UNCOMPRESSED) {
11565643Saalok 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
11575643Saalok 				    (cmpbytes - SEGHDR));
11585643Saalok 			} else {
11595643Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
11605643Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
11615643Saalok 				    &seglen, li->l_level) != 0) {
11625643Saalok 					error = EIO;
11635643Saalok 					goto done;
11645643Saalok 				}
11655643Saalok 			}
11665643Saalok 
11679048Sjrgn.keil@googlemail.com 			uncompressed_seg_index = i;
11689048Sjrgn.keil@googlemail.com 
11695643Saalok 			/*
11705643Saalok 			 * Determine how much uncompressed data we
11715643Saalok 			 * have to copy and copy it
11725643Saalok 			 */
11735643Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
11748996SAlok.Aggarwal@Sun.COM 			if (i == eblkno)
11758996SAlok.Aggarwal@Sun.COM 				xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
11765643Saalok 
11775643Saalok 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
11785643Saalok 
11795643Saalok 			cmpbuf += cmpbytes;
11800Sstevel@tonic-gate 			bufaddr += xfersize;
11815643Saalok 			bp->b_resid -= xfersize;
11825643Saalok 			sblkoff = 0;
11835643Saalok 
11845643Saalok 			if (bp->b_resid == 0)
11855643Saalok 				break;
11865643Saalok 		}
11879048Sjrgn.keil@googlemail.com 
11889048Sjrgn.keil@googlemail.com 		/*
11899048Sjrgn.keil@googlemail.com 		 * Add the data for the last decopressed segment to
11909048Sjrgn.keil@googlemail.com 		 * the cache.
11919048Sjrgn.keil@googlemail.com 		 *
11929048Sjrgn.keil@googlemail.com 		 * In case the uncompressed segment data was added to (and
11939048Sjrgn.keil@googlemail.com 		 * is referenced by) the cache, make sure we don't free it
11949048Sjrgn.keil@googlemail.com 		 * here.
11959048Sjrgn.keil@googlemail.com 		 */
11969048Sjrgn.keil@googlemail.com 		mutex_enter(&lsp->ls_comp_cache_lock);
11979048Sjrgn.keil@googlemail.com 		if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
11989048Sjrgn.keil@googlemail.com 		    uncompressed_seg)) != NULL) {
11999048Sjrgn.keil@googlemail.com 			uncompressed_seg = NULL;
12009048Sjrgn.keil@googlemail.com 		}
12019048Sjrgn.keil@googlemail.com 		mutex_exit(&lsp->ls_comp_cache_lock);
12029048Sjrgn.keil@googlemail.com 
12038313SDina.Nimeh@Sun.Com done:
12048313SDina.Nimeh@Sun.Com 		if (compressed_seg != NULL)
12058313SDina.Nimeh@Sun.Com 			kmem_free(compressed_seg, comp_data_sz);
12068313SDina.Nimeh@Sun.Com 		if (uncompressed_seg != NULL)
12078313SDina.Nimeh@Sun.Com 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
12088313SDina.Nimeh@Sun.Com 	} /* end of handling compressed files */
12090Sstevel@tonic-gate 
12108313SDina.Nimeh@Sun.Com errout:
12118313SDina.Nimeh@Sun.Com 	if (bufinited && lsp->ls_kstat) {
12120Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
12130Sstevel@tonic-gate 		kstat_io_t *kioptr;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
12160Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
12170Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
12180Sstevel@tonic-gate 			kioptr->nread += n_done;
12190Sstevel@tonic-gate 			kioptr->reads++;
12200Sstevel@tonic-gate 		} else {
12210Sstevel@tonic-gate 			kioptr->nwritten += n_done;
12220Sstevel@tonic-gate 			kioptr->writes++;
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
12250Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
12260Sstevel@tonic-gate 	}
12274451Seschrock 
12284451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
12294451Seschrock 	if (--lsp->ls_vp_iocount == 0)
12304451Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
12314451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
12324451Seschrock 
12330Sstevel@tonic-gate 	bioerror(bp, error);
12340Sstevel@tonic-gate 	biodone(bp);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate static int
12380Sstevel@tonic-gate lofi_strategy(struct buf *bp)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	struct lofi_state *lsp;
12410Sstevel@tonic-gate 	offset_t	offset;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/*
12440Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
12450Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
12460Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
12470Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
12480Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
12490Sstevel@tonic-gate 	 *
12500Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
12510Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
12520Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
12530Sstevel@tonic-gate 	 */
12540Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
12558313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
12568313SDina.Nimeh@Sun.Com 		bioerror(bp, ENXIO);
12578313SDina.Nimeh@Sun.Com 		biodone(bp);
12588313SDina.Nimeh@Sun.Com 		return (0);
12598313SDina.Nimeh@Sun.Com 	}
12608313SDina.Nimeh@Sun.Com 
12614451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
12624451Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
12634451Seschrock 		bioerror(bp, EIO);
12644451Seschrock 		biodone(bp);
12654451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12664451Seschrock 		return (0);
12674451Seschrock 	}
12684451Seschrock 
12690Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
12708313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
12718313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
12728313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
12738313SDina.Nimeh@Sun.Com 	}
12740Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
12750Sstevel@tonic-gate 		/* EOF */
12760Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
12770Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
12780Sstevel@tonic-gate 			bioerror(bp, 0);
12790Sstevel@tonic-gate 		} else {
12800Sstevel@tonic-gate 			/* writes should fail */
12810Sstevel@tonic-gate 			bioerror(bp, ENXIO);
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 		biodone(bp);
12844451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12850Sstevel@tonic-gate 		return (0);
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
12880Sstevel@tonic-gate 		bioerror(bp, ENXIO);
12890Sstevel@tonic-gate 		biodone(bp);
12904451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12910Sstevel@tonic-gate 		return (0);
12920Sstevel@tonic-gate 	}
12934451Seschrock 	lsp->ls_vp_iocount++;
12944451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
12954451Seschrock 
12960Sstevel@tonic-gate 	if (lsp->ls_kstat) {
12970Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
12980Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
12990Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
13020Sstevel@tonic-gate 	return (0);
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate /*ARGSUSED2*/
13060Sstevel@tonic-gate static int
13070Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate 	if (getminor(dev) == 0)
13100Sstevel@tonic-gate 		return (EINVAL);
13118313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
13120Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate /*ARGSUSED2*/
13160Sstevel@tonic-gate static int
13170Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
13180Sstevel@tonic-gate {
13190Sstevel@tonic-gate 	if (getminor(dev) == 0)
13200Sstevel@tonic-gate 		return (EINVAL);
13218313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
13220Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
13230Sstevel@tonic-gate }
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate /*ARGSUSED2*/
13260Sstevel@tonic-gate static int
13270Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	if (getminor(dev) == 0)
13300Sstevel@tonic-gate 		return (EINVAL);
13318313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
13320Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate /*ARGSUSED2*/
13360Sstevel@tonic-gate static int
13370Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	if (getminor(dev) == 0)
13400Sstevel@tonic-gate 		return (EINVAL);
13418313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
13420Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate /*ARGSUSED*/
13460Sstevel@tonic-gate static int
13470Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
13480Sstevel@tonic-gate {
13490Sstevel@tonic-gate 	switch (infocmd) {
13500Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
13510Sstevel@tonic-gate 		*result = lofi_dip;
13520Sstevel@tonic-gate 		return (DDI_SUCCESS);
13530Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
13540Sstevel@tonic-gate 		*result = 0;
13550Sstevel@tonic-gate 		return (DDI_SUCCESS);
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 	return (DDI_FAILURE);
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate static int
13610Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
13620Sstevel@tonic-gate {
13630Sstevel@tonic-gate 	int	error;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
13660Sstevel@tonic-gate 		return (DDI_FAILURE);
13670Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
13680Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
13690Sstevel@tonic-gate 		return (DDI_FAILURE);
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
13720Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
13730Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
13740Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
13750Sstevel@tonic-gate 		return (DDI_FAILURE);
13760Sstevel@tonic-gate 	}
13775084Sjohnlev 	/* driver handles kernel-issued IOCTLs */
13785084Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
13795084Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
13805084Sjohnlev 		ddi_remove_minor_node(dip, NULL);
13815084Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
13825084Sjohnlev 		return (DDI_FAILURE);
13835084Sjohnlev 	}
13840Sstevel@tonic-gate 	lofi_dip = dip;
13850Sstevel@tonic-gate 	ddi_report_dev(dip);
13860Sstevel@tonic-gate 	return (DDI_SUCCESS);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate static int
13900Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
13930Sstevel@tonic-gate 		return (DDI_FAILURE);
13940Sstevel@tonic-gate 	if (lofi_busy())
13950Sstevel@tonic-gate 		return (DDI_FAILURE);
13960Sstevel@tonic-gate 	lofi_dip = NULL;
13970Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
13985084Sjohnlev 	ddi_prop_remove_all(dip);
13990Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
14000Sstevel@tonic-gate 	return (DDI_SUCCESS);
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate /*
14048313SDina.Nimeh@Sun.Com  * With addition of encryption, be careful that encryption key is wiped before
14058313SDina.Nimeh@Sun.Com  * kernel memory structures are freed, and also that key is not accidentally
14068313SDina.Nimeh@Sun.Com  * passed out into userland structures.
14078313SDina.Nimeh@Sun.Com  */
14088313SDina.Nimeh@Sun.Com static void
14098313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip)
14108313SDina.Nimeh@Sun.Com {
14118313SDina.Nimeh@Sun.Com 	/* Make sure this encryption key doesn't stick around */
14128313SDina.Nimeh@Sun.Com 	bzero(klip->li_key, sizeof (klip->li_key));
14138313SDina.Nimeh@Sun.Com 	kmem_free(klip, sizeof (struct lofi_ioctl));
14148313SDina.Nimeh@Sun.Com }
14158313SDina.Nimeh@Sun.Com 
14168313SDina.Nimeh@Sun.Com /*
14170Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
14180Sstevel@tonic-gate  * the lofi_ioctl structure.
14190Sstevel@tonic-gate  */
14200Sstevel@tonic-gate struct lofi_ioctl *
14211657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
14220Sstevel@tonic-gate {
14230Sstevel@tonic-gate 	struct lofi_ioctl *klip;
14240Sstevel@tonic-gate 	int	error;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
14271657Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
14280Sstevel@tonic-gate 	if (error) {
14298313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
14300Sstevel@tonic-gate 		return (NULL);
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
14348313SDina.Nimeh@Sun.Com 	klip->li_filename[MAXPATHLEN-1] = '\0';
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	/* validate minor number */
14370Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
14388313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
14398313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
14408313SDina.Nimeh@Sun.Com 		    lofi_max_files);
14410Sstevel@tonic-gate 		return (NULL);
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 	return (klip);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate int
14471657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
14481657Sheppo 	int flag)
14490Sstevel@tonic-gate {
14500Sstevel@tonic-gate 	int	error;
14510Sstevel@tonic-gate 
14528313SDina.Nimeh@Sun.Com 	/*
14538313SDina.Nimeh@Sun.Com 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
14548313SDina.Nimeh@Sun.Com 	 * This ensures that an attacker can't trivially find the
14558313SDina.Nimeh@Sun.Com 	 * key for a mapping just by issuing the ioctl.
14568313SDina.Nimeh@Sun.Com 	 *
14578313SDina.Nimeh@Sun.Com 	 * It can still be found by poking around in kmem with mdb(1),
14588313SDina.Nimeh@Sun.Com 	 * but there is no point in making it easy when the info isn't
14598313SDina.Nimeh@Sun.Com 	 * of any use in this direction anyway.
14608313SDina.Nimeh@Sun.Com 	 *
14618313SDina.Nimeh@Sun.Com 	 * Either way we don't actually have the raw key stored in
14628313SDina.Nimeh@Sun.Com 	 * a form that we can get it anyway, since we just used it
14638313SDina.Nimeh@Sun.Com 	 * to create a ctx template and didn't keep "the original".
14648313SDina.Nimeh@Sun.Com 	 */
14651657Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
14660Sstevel@tonic-gate 	if (error)
14670Sstevel@tonic-gate 		return (EFAULT);
14680Sstevel@tonic-gate 	return (0);
14690Sstevel@tonic-gate }
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate /*
14720Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
14730Sstevel@tonic-gate  */
14740Sstevel@tonic-gate static int
14750Sstevel@tonic-gate file_to_minor(char *filename)
14760Sstevel@tonic-gate {
14770Sstevel@tonic-gate 	minor_t	minor;
14780Sstevel@tonic-gate 	struct lofi_state *lsp;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
14810Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
14820Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
14830Sstevel@tonic-gate 		if (lsp == NULL)
14840Sstevel@tonic-gate 			continue;
14850Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
14860Sstevel@tonic-gate 			return (minor);
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 	return (0);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate /*
14920Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
14930Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
14940Sstevel@tonic-gate  */
14950Sstevel@tonic-gate static int
14960Sstevel@tonic-gate valid_filename(const char *filename)
14970Sstevel@tonic-gate {
14980Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
14990Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/* must be absolute path */
15020Sstevel@tonic-gate 	if (filename[0] != '/')
15030Sstevel@tonic-gate 		return (0);
15040Sstevel@tonic-gate 	/* must not be lofi */
15050Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
15060Sstevel@tonic-gate 		return (0);
15070Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
15080Sstevel@tonic-gate 		return (0);
15090Sstevel@tonic-gate 	return (1);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate /*
15130Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
15140Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
15150Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
15160Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
15173517Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
15180Sstevel@tonic-gate  * have to support it.
15190Sstevel@tonic-gate  */
15200Sstevel@tonic-gate static void
15210Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
15220Sstevel@tonic-gate {
15238313SDina.Nimeh@Sun.Com 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
15248313SDina.Nimeh@Sun.Com 
15250Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
15260Sstevel@tonic-gate 	/*
15270Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
15280Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
15290Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
15300Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
15310Sstevel@tonic-gate 	 * a number between one and one).
15320Sstevel@tonic-gate 	 *
15330Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
15340Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
15350Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
15360Sstevel@tonic-gate 	 */
15378313SDina.Nimeh@Sun.Com 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
15388313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
15390Sstevel@tonic-gate 	else
15408313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
15410Sstevel@tonic-gate 	/* in case file file is < 100k */
15420Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
15430Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
15440Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
15450Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
15460Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
15470Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
15480Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
15490Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
15500Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
15510Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
15520Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
15530Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
15548313SDina.Nimeh@Sun.Com 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
15550Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
15560Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
15590Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
15600Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
15610Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
15628669SDina.Nimeh@Sun.COM 	(void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME,
15638669SDina.Nimeh@Sun.COM 	    sizeof (lsp->ls_vtoc.v_volume));
15640Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
15650Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
15660Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
15675643Saalok 
15685643Saalok 	/*
15695643Saalok 	 * A compressed file is read-only, other files can
15705643Saalok 	 * be read-write
15715643Saalok 	 */
15725643Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
15735643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
15745643Saalok 	} else {
15755643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
15765643Saalok 	}
15770Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
15780Sstevel@tonic-gate 	/*
15790Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
15800Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
15810Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
15820Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
15830Sstevel@tonic-gate 	 */
15840Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
15850Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
15880Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
15890Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
15900Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
15910Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
15920Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
15930Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
15940Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
15950Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
15960Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
15970Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
15980Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
15990Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
16000Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
16010Sstevel@tonic-gate 	/*
16020Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
16030Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
16040Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
16050Sstevel@tonic-gate 	 * maxcontig is 0.
16060Sstevel@tonic-gate 	 */
16070Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate /*
16115643Saalok  * map in a compressed file
16125643Saalok  *
16135643Saalok  * Read in the header and the index that follows.
16145643Saalok  *
16155643Saalok  * The header is as follows -
16165643Saalok  *
16175643Saalok  * Signature (name of the compression algorithm)
16185643Saalok  * Compression segment size (a multiple of 512)
16195643Saalok  * Number of index entries
16205643Saalok  * Size of the last block
16215643Saalok  * The array containing the index entries
16225643Saalok  *
16235643Saalok  * The header information is always stored in
16245643Saalok  * network byte order on disk.
16255643Saalok  */
16265643Saalok static int
16275643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
16285643Saalok {
16295643Saalok 	uint32_t index_sz, header_len, i;
16305643Saalok 	ssize_t	resid;
16315643Saalok 	enum uio_rw rw;
16325643Saalok 	char *tbuf = buf;
16335643Saalok 	int error;
16345643Saalok 
16355643Saalok 	/* The signature has already been read */
16365643Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
16375643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
16385643Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
16395643Saalok 
16405643Saalok 	/*
16415643Saalok 	 * The compressed segment size must be a power of 2
16425643Saalok 	 */
16439048Sjrgn.keil@googlemail.com 	if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
16449048Sjrgn.keil@googlemail.com 	    !ISP2(lsp->ls_uncomp_seg_sz))
16455643Saalok 		return (EINVAL);
16465643Saalok 
16475643Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
16485643Saalok 		;
16495643Saalok 
16505643Saalok 	lsp->ls_comp_seg_shift = i;
16515643Saalok 
16525643Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
16535643Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
16545643Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
16555643Saalok 
16565643Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
16575643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
16585643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
16595643Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
16605643Saalok 
16615643Saalok 	/*
16625643Saalok 	 * Compute the total size of the uncompressed data
16635643Saalok 	 * for use in fake_disk_geometry and other calculations.
16645643Saalok 	 * Disk geometry has to be faked with respect to the
16655643Saalok 	 * actual uncompressed data size rather than the
16665643Saalok 	 * compressed file size.
16675643Saalok 	 */
1668*10197Sdminer@opensolaris.org 	lsp->ls_vp_size =
1669*10197Sdminer@opensolaris.org 	    (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
16705643Saalok 	    + lsp->ls_uncomp_last_seg_sz;
16715643Saalok 
16725643Saalok 	/*
16738996SAlok.Aggarwal@Sun.COM 	 * Index size is rounded up to DEV_BSIZE for ease
16745643Saalok 	 * of segmapping
16755643Saalok 	 */
16765643Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
16775643Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
16785643Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
16795643Saalok 	    sizeof (lsp->ls_comp_index_sz) +
16805643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
16815643Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
16825643Saalok 
16835643Saalok 	index_sz += header_len;
16845643Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
16855643Saalok 
16865643Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
16875643Saalok 	lsp->ls_comp_index_data_sz = index_sz;
16885643Saalok 
16895643Saalok 	/*
16905643Saalok 	 * Read in the index -- this has a side-effect
16915643Saalok 	 * of reading in the header as well
16925643Saalok 	 */
16935643Saalok 	rw = UIO_READ;
16945643Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
16955643Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
16965643Saalok 
16975643Saalok 	if (error != 0)
16985643Saalok 		return (error);
16995643Saalok 
17005643Saalok 	/* Skip the header, this is where the index really begins */
17015643Saalok 	lsp->ls_comp_seg_index =
17025643Saalok 	    /*LINTED*/
17035643Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
17045643Saalok 
17055643Saalok 	/*
17065643Saalok 	 * Now recompute offsets in the index to account for
17075643Saalok 	 * the header length
17085643Saalok 	 */
17095643Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
17105643Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
17115643Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
17125643Saalok 	}
17135643Saalok 
17145643Saalok 	return (error);
17155643Saalok }
17165643Saalok 
17175643Saalok /*
17185643Saalok  * Check to see if the passed in signature is a valid
17198313SDina.Nimeh@Sun.Com  * one.  If it is valid, return the index into
17205643Saalok  * lofi_compress_table.
17215643Saalok  *
17225643Saalok  * Return -1 if it is invalid
17235643Saalok  */
17245643Saalok static int lofi_compress_select(char *signature)
17255643Saalok {
17265643Saalok 	int i;
17275643Saalok 
17285643Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
17295643Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
17305643Saalok 			return (i);
17315643Saalok 	}
17325643Saalok 
17335643Saalok 	return (-1);
17345643Saalok }
17355643Saalok 
17365643Saalok /*
17370Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
17380Sstevel@tonic-gate  */
17390Sstevel@tonic-gate static int
17400Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
17411657Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
17420Sstevel@tonic-gate {
17430Sstevel@tonic-gate 	minor_t	newminor;
17440Sstevel@tonic-gate 	struct lofi_state *lsp;
17450Sstevel@tonic-gate 	struct lofi_ioctl *klip;
17460Sstevel@tonic-gate 	int	error;
17470Sstevel@tonic-gate 	struct vnode *vp;
17480Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
17490Sstevel@tonic-gate 	int64_t	Size_prop_val;
17505643Saalok 	int	compress_index;
17510Sstevel@tonic-gate 	vattr_t	vattr;
17520Sstevel@tonic-gate 	int	flag;
17530Sstevel@tonic-gate 	enum vtype v_type;
17544451Seschrock 	int zalloced = 0;
17550Sstevel@tonic-gate 	dev_t	newdev;
17564451Seschrock 	char	namebuf[50];
17578313SDina.Nimeh@Sun.Com 	char	buf[DEV_BSIZE];
17588313SDina.Nimeh@Sun.Com 	char	crybuf[DEV_BSIZE];
17595643Saalok 	ssize_t	resid;
17608313SDina.Nimeh@Sun.Com 	boolean_t need_vn_close = B_FALSE;
17618313SDina.Nimeh@Sun.Com 	boolean_t keycopied = B_FALSE;
17628313SDina.Nimeh@Sun.Com 	boolean_t need_size_update = B_FALSE;
17630Sstevel@tonic-gate 
17641657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
17650Sstevel@tonic-gate 	if (klip == NULL)
17660Sstevel@tonic-gate 		return (EFAULT);
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
17710Sstevel@tonic-gate 		error = EINVAL;
17720Sstevel@tonic-gate 		goto out;
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
17760Sstevel@tonic-gate 		error = EBUSY;
17770Sstevel@tonic-gate 		goto out;
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	if (pickminor) {
17810Sstevel@tonic-gate 		/* Find a free one */
17820Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
17830Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
17840Sstevel@tonic-gate 				break;
17850Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
17860Sstevel@tonic-gate 			error = EAGAIN;
17870Sstevel@tonic-gate 			goto out;
17880Sstevel@tonic-gate 		}
17890Sstevel@tonic-gate 	} else {
17900Sstevel@tonic-gate 		newminor = klip->li_minor;
17910Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
17920Sstevel@tonic-gate 			error = EEXIST;
17930Sstevel@tonic-gate 			goto out;
17940Sstevel@tonic-gate 		}
17950Sstevel@tonic-gate 	}
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	/* make sure it's valid */
17980Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
17990Sstevel@tonic-gate 	    NULLVPP, &vp);
18000Sstevel@tonic-gate 	if (error) {
18010Sstevel@tonic-gate 		goto out;
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 	v_type = vp->v_type;
18040Sstevel@tonic-gate 	VN_RELE(vp);
18050Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
18060Sstevel@tonic-gate 		error = EINVAL;
18070Sstevel@tonic-gate 		goto out;
18080Sstevel@tonic-gate 	}
18090Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
18100Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
18110Sstevel@tonic-gate 	if (error) {
18120Sstevel@tonic-gate 		/* try read-only */
18130Sstevel@tonic-gate 		flag &= ~FWRITE;
18140Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
18150Sstevel@tonic-gate 		    &vp, 0, 0);
18160Sstevel@tonic-gate 		if (error) {
18170Sstevel@tonic-gate 			goto out;
18180Sstevel@tonic-gate 		}
18190Sstevel@tonic-gate 	}
18208313SDina.Nimeh@Sun.Com 	need_vn_close = B_TRUE;
18218313SDina.Nimeh@Sun.Com 
18220Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
18235331Samw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
18240Sstevel@tonic-gate 	if (error) {
18258313SDina.Nimeh@Sun.Com 		goto out;
18260Sstevel@tonic-gate 	}
18270Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
18280Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
18290Sstevel@tonic-gate 		error = EINVAL;
18308313SDina.Nimeh@Sun.Com 		goto out;
18310Sstevel@tonic-gate 	}
18320Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
18330Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
18340Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
18350Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
18360Sstevel@tonic-gate 		error = EINVAL;
18378313SDina.Nimeh@Sun.Com 		goto out;
18380Sstevel@tonic-gate 	}
18390Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
18400Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
18410Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
18420Sstevel@tonic-gate 		error = EINVAL;
18430Sstevel@tonic-gate 		goto propout;
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
18460Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
18470Sstevel@tonic-gate 		error = ENOMEM;
18480Sstevel@tonic-gate 		goto propout;
18490Sstevel@tonic-gate 	}
18500Sstevel@tonic-gate 	zalloced = 1;
18510Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
18526883Sgd78059 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
18530Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
18540Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
18550Sstevel@tonic-gate 		error = ENXIO;
18560Sstevel@tonic-gate 		goto propout;
18570Sstevel@tonic-gate 	}
18580Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
18590Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
18600Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
18610Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
18620Sstevel@tonic-gate 		/* remove block node */
18630Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
18640Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
18650Sstevel@tonic-gate 		error = ENXIO;
18660Sstevel@tonic-gate 		goto propout;
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
18690Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
18700Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
18710Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
18720Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
18730Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
18740Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
18750Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
18760Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
18770Sstevel@tonic-gate 	if (lsp->ls_kstat) {
18780Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
18790Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
18800Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
18810Sstevel@tonic-gate 	}
18824451Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
18834451Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
18844451Seschrock 
18859048Sjrgn.keil@googlemail.com 	list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
18869048Sjrgn.keil@googlemail.com 	    offsetof(struct lofi_comp_cache, lc_list));
18879048Sjrgn.keil@googlemail.com 	mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
18889048Sjrgn.keil@googlemail.com 
18890Sstevel@tonic-gate 	/*
18900Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
18910Sstevel@tonic-gate 	 * updated correctly.
18920Sstevel@tonic-gate 	 */
18930Sstevel@tonic-gate 	lsp->ls_openflag = flag;
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	/*
18960Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
18970Sstevel@tonic-gate 	 */
18980Sstevel@tonic-gate 	if (vp->v_type == VREG) {
18995331Samw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
19000Sstevel@tonic-gate 			lsp->ls_vp = vp;
19010Sstevel@tonic-gate 		} else {
19020Sstevel@tonic-gate 			/*
19030Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
19040Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
19050Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
19060Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
19070Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
19080Sstevel@tonic-gate 			 * the realvp so things behave as if we've
19090Sstevel@tonic-gate 			 * opened the realvp without any interaction
19100Sstevel@tonic-gate 			 * with lofs.
19110Sstevel@tonic-gate 			 */
19120Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
19130Sstevel@tonic-gate 			VN_RELE(vp);
19140Sstevel@tonic-gate 		}
19150Sstevel@tonic-gate 	} else {
19160Sstevel@tonic-gate 		lsp->ls_vp = vp;
19170Sstevel@tonic-gate 	}
19180Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
19190Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
19200Sstevel@tonic-gate 	if (rvalp)
19210Sstevel@tonic-gate 		*rvalp = (int)newminor;
19220Sstevel@tonic-gate 	klip->li_minor = newminor;
19230Sstevel@tonic-gate 
19245643Saalok 	/*
19258313SDina.Nimeh@Sun.Com 	 * Initialize crypto details for encrypted lofi
19265643Saalok 	 */
19278313SDina.Nimeh@Sun.Com 	if (klip->li_crypto_enabled) {
19288313SDina.Nimeh@Sun.Com 		int ret;
19298313SDina.Nimeh@Sun.Com 
19308313SDina.Nimeh@Sun.Com 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
19318313SDina.Nimeh@Sun.Com 
19328313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
19338313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
19348313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
19358313SDina.Nimeh@Sun.Com 			    klip->li_cipher, lsp->ls_filename);
19368313SDina.Nimeh@Sun.Com 			error = EINVAL;
19378313SDina.Nimeh@Sun.Com 			goto propout;
19388313SDina.Nimeh@Sun.Com 		}
19398313SDina.Nimeh@Sun.Com 
19408313SDina.Nimeh@Sun.Com 		/* this is just initialization here */
19418313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param = NULL;
19428313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param_len = 0;
19438313SDina.Nimeh@Sun.Com 
19448313SDina.Nimeh@Sun.Com 		lsp->ls_iv_type = klip->li_iv_type;
19458313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
19468313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
19478313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
19488313SDina.Nimeh@Sun.Com 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
19498313SDina.Nimeh@Sun.Com 			error = EINVAL;
19508313SDina.Nimeh@Sun.Com 			goto propout;
19518313SDina.Nimeh@Sun.Com 		}
19528313SDina.Nimeh@Sun.Com 
19538313SDina.Nimeh@Sun.Com 		/* iv mech must itself take a null iv */
19548313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param = NULL;
19558313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param_len = 0;
19568313SDina.Nimeh@Sun.Com 		lsp->ls_iv_len = klip->li_iv_len;
19578313SDina.Nimeh@Sun.Com 
19588313SDina.Nimeh@Sun.Com 		/*
19598313SDina.Nimeh@Sun.Com 		 * Create ctx using li_cipher & the raw li_key after checking
19608313SDina.Nimeh@Sun.Com 		 * that it isn't a weak key.
19618313SDina.Nimeh@Sun.Com 		 */
19628313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
19638313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = klip->li_key_len;
19648313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = kmem_alloc(
19658313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
19668313SDina.Nimeh@Sun.Com 		bcopy(klip->li_key, lsp->ls_key.ck_data,
19678313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
19688313SDina.Nimeh@Sun.Com 		keycopied = B_TRUE;
19698313SDina.Nimeh@Sun.Com 
19708313SDina.Nimeh@Sun.Com 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
19718313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS) {
19728313SDina.Nimeh@Sun.Com 			error = EINVAL;
19738313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "weak key check failed for cipher "
19748313SDina.Nimeh@Sun.Com 			    "%s on file %s (0x%x)", klip->li_cipher,
19758313SDina.Nimeh@Sun.Com 			    lsp->ls_filename, ret);
19768313SDina.Nimeh@Sun.Com 			goto propout;
19778313SDina.Nimeh@Sun.Com 		}
19788313SDina.Nimeh@Sun.Com 	}
19798313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
19808313SDina.Nimeh@Sun.Com 
19818313SDina.Nimeh@Sun.Com 	/*
19828313SDina.Nimeh@Sun.Com 	 * Read the file signature to check if it is compressed or encrypted.
19838313SDina.Nimeh@Sun.Com 	 * Crypto signature is in a different location; both areas should
19848313SDina.Nimeh@Sun.Com 	 * read to keep compression and encryption mutually exclusive.
19858313SDina.Nimeh@Sun.Com 	 */
19868313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
19878313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
19888313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
19898313SDina.Nimeh@Sun.Com 		if (error != 0)
19908313SDina.Nimeh@Sun.Com 			goto propout;
19918313SDina.Nimeh@Sun.Com 	}
19928313SDina.Nimeh@Sun.Com 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
19935643Saalok 	    0, RLIM64_INFINITY, kcred, &resid);
19945643Saalok 	if (error != 0)
19955643Saalok 		goto propout;
19965643Saalok 
19978313SDina.Nimeh@Sun.Com 	/* initialize these variables for all lofi files */
19985643Saalok 	lsp->ls_uncomp_seg_sz = 0;
19995643Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
20005643Saalok 	lsp->ls_comp_algorithm[0] = '\0';
20015643Saalok 
20028313SDina.Nimeh@Sun.Com 	/* encrypted lofi reads/writes shifted by crypto metadata size */
20038313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_offset = 0;
20048313SDina.Nimeh@Sun.Com 
20058313SDina.Nimeh@Sun.Com 	/* this is a compressed lofi */
20068313SDina.Nimeh@Sun.Com 	if ((compress_index = lofi_compress_select(buf)) != -1) {
20078313SDina.Nimeh@Sun.Com 
20088313SDina.Nimeh@Sun.Com 		/* compression and encryption are mutually exclusive */
20098313SDina.Nimeh@Sun.Com 		if (klip->li_crypto_enabled) {
20108313SDina.Nimeh@Sun.Com 			error = ENOTSUP;
20118313SDina.Nimeh@Sun.Com 			goto propout;
20128313SDina.Nimeh@Sun.Com 		}
20138313SDina.Nimeh@Sun.Com 
20148313SDina.Nimeh@Sun.Com 		/* initialize compression info for compressed lofi */
20155643Saalok 		lsp->ls_comp_algorithm_index = compress_index;
20165643Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
20175643Saalok 		    lofi_compress_table[compress_index].l_name,
20185643Saalok 		    sizeof (lsp->ls_comp_algorithm));
20198313SDina.Nimeh@Sun.Com 
20205643Saalok 		error = lofi_map_compressed_file(lsp, buf);
20215643Saalok 		if (error != 0)
20225643Saalok 			goto propout;
20238313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
20245643Saalok 
20258313SDina.Nimeh@Sun.Com 	/* this is an encrypted lofi */
20268313SDina.Nimeh@Sun.Com 	} else if (strncmp(crybuf, lofi_crypto_magic,
20278313SDina.Nimeh@Sun.Com 	    sizeof (lofi_crypto_magic)) == 0) {
20288313SDina.Nimeh@Sun.Com 
20298313SDina.Nimeh@Sun.Com 		char *marker = crybuf;
20308313SDina.Nimeh@Sun.Com 
20318313SDina.Nimeh@Sun.Com 		/*
20328313SDina.Nimeh@Sun.Com 		 * This is the case where the header in the lofi image is
20338313SDina.Nimeh@Sun.Com 		 * already initialized to indicate it is encrypted.
20348313SDina.Nimeh@Sun.Com 		 * There is another case (see below) where encryption is
20358313SDina.Nimeh@Sun.Com 		 * requested but the lofi image has never been used yet,
20368313SDina.Nimeh@Sun.Com 		 * so the header needs to be written with encryption magic.
20378313SDina.Nimeh@Sun.Com 		 */
20388313SDina.Nimeh@Sun.Com 
20398313SDina.Nimeh@Sun.Com 		/* indicate this must be an encrypted lofi due to magic */
20408313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = B_TRUE;
20418313SDina.Nimeh@Sun.Com 
20428313SDina.Nimeh@Sun.Com 		/*
20438313SDina.Nimeh@Sun.Com 		 * The encryption header information is laid out this way:
20448313SDina.Nimeh@Sun.Com 		 *	6 bytes:	hex "CFLOFI"
20458313SDina.Nimeh@Sun.Com 		 *	2 bytes:	version = 0 ... for now
20468313SDina.Nimeh@Sun.Com 		 *	96 bytes:	reserved1 (not implemented yet)
20478313SDina.Nimeh@Sun.Com 		 *	4 bytes:	data_sector = 2 ... for now
20488313SDina.Nimeh@Sun.Com 		 *	more...		not implemented yet
20498313SDina.Nimeh@Sun.Com 		 */
20508313SDina.Nimeh@Sun.Com 
20518313SDina.Nimeh@Sun.Com 		/* copy the magic */
20528313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.magic,
20538313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.magic));
20548313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.magic);
20558313SDina.Nimeh@Sun.Com 
20568313SDina.Nimeh@Sun.Com 		/* read the encryption version number */
20578313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.version),
20588313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.version));
20598313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
20608313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.version);
20618313SDina.Nimeh@Sun.Com 
20628313SDina.Nimeh@Sun.Com 		/* read a chunk of reserved data */
20638313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.reserved1,
20648313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.reserved1));
20658313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.reserved1);
20668313SDina.Nimeh@Sun.Com 
20678313SDina.Nimeh@Sun.Com 		/* read block number where encrypted data begins */
20688313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.data_sector),
20698313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.data_sector));
20708313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
20718313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.data_sector);
20728313SDina.Nimeh@Sun.Com 
20738313SDina.Nimeh@Sun.Com 		/* and ignore the rest until it is implemented */
20748313SDina.Nimeh@Sun.Com 
20758313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
20768313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
20778313SDina.Nimeh@Sun.Com 
20788313SDina.Nimeh@Sun.Com 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
20798313SDina.Nimeh@Sun.Com 	} else if (klip->li_crypto_enabled) {
20808313SDina.Nimeh@Sun.Com 
20818313SDina.Nimeh@Sun.Com 		/*
20828313SDina.Nimeh@Sun.Com 		 * This is the case where encryption was requested but the
20838313SDina.Nimeh@Sun.Com 		 * appears to be entirely blank where the encryption header
20848313SDina.Nimeh@Sun.Com 		 * would have been in the lofi image.  If it is blank,
20858313SDina.Nimeh@Sun.Com 		 * assume it is a brand new lofi image and initialize the
20868313SDina.Nimeh@Sun.Com 		 * header area with encryption magic and current version
20878313SDina.Nimeh@Sun.Com 		 * header data.  If it is not blank, that's an error.
20888313SDina.Nimeh@Sun.Com 		 */
20898313SDina.Nimeh@Sun.Com 		int	i;
20908313SDina.Nimeh@Sun.Com 		char	*marker;
20918313SDina.Nimeh@Sun.Com 		struct crypto_meta	chead;
20928313SDina.Nimeh@Sun.Com 
20938313SDina.Nimeh@Sun.Com 		for (i = 0; i < sizeof (struct crypto_meta); i++)
20948313SDina.Nimeh@Sun.Com 			if (crybuf[i] != '\0')
20958313SDina.Nimeh@Sun.Com 				break;
20968313SDina.Nimeh@Sun.Com 		if (i != sizeof (struct crypto_meta)) {
20978313SDina.Nimeh@Sun.Com 			error = EINVAL;
20988313SDina.Nimeh@Sun.Com 			goto propout;
20998313SDina.Nimeh@Sun.Com 		}
21008313SDina.Nimeh@Sun.Com 
21018313SDina.Nimeh@Sun.Com 		/* nothing there, initialize as encrypted lofi */
21028313SDina.Nimeh@Sun.Com 		marker = crybuf;
21038313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
21048313SDina.Nimeh@Sun.Com 		marker += sizeof (lofi_crypto_magic);
21058313SDina.Nimeh@Sun.Com 		chead.version = htons(LOFI_CRYPTO_VERSION);
21068313SDina.Nimeh@Sun.Com 		bcopy(&(chead.version), marker, sizeof (chead.version));
21078313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.version);
21088313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.reserved1);
21098313SDina.Nimeh@Sun.Com 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
21108313SDina.Nimeh@Sun.Com 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
21118313SDina.Nimeh@Sun.Com 
21128313SDina.Nimeh@Sun.Com 		/* write the header */
21138313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
21148313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
21158313SDina.Nimeh@Sun.Com 		if (error != 0)
21168313SDina.Nimeh@Sun.Com 			goto propout;
21178313SDina.Nimeh@Sun.Com 
21188313SDina.Nimeh@Sun.Com 		/* fix things up so it looks like we read this info */
21198313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
21208313SDina.Nimeh@Sun.Com 		    sizeof (lofi_crypto_magic));
21218313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
21228313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
21238313SDina.Nimeh@Sun.Com 
21248313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
21258313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
21268313SDina.Nimeh@Sun.Com 	}
21278313SDina.Nimeh@Sun.Com 
21288313SDina.Nimeh@Sun.Com 	/*
21298313SDina.Nimeh@Sun.Com 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
21308313SDina.Nimeh@Sun.Com 	 * for encrypted lofi, advertise that it is somewhat shorter
21318313SDina.Nimeh@Sun.Com 	 * due to embedded crypto metadata section
21328313SDina.Nimeh@Sun.Com 	 */
21338313SDina.Nimeh@Sun.Com 	if (need_size_update) {
21345643Saalok 		/* update DDI properties */
21358313SDina.Nimeh@Sun.Com 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
21365643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
21375643Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
21385643Saalok 			error = EINVAL;
21395643Saalok 			goto propout;
21405643Saalok 		}
21418313SDina.Nimeh@Sun.Com 		Nblocks_prop_val =
21428313SDina.Nimeh@Sun.Com 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
21435643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
21445643Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
21455643Saalok 			error = EINVAL;
21465643Saalok 			goto propout;
21475643Saalok 		}
21485643Saalok 	}
21495643Saalok 
21500Sstevel@tonic-gate 	fake_disk_geometry(lsp);
21510Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
21521657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21530Sstevel@tonic-gate 	free_lofi_ioctl(klip);
21540Sstevel@tonic-gate 	return (0);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate propout:
21578313SDina.Nimeh@Sun.Com 	if (keycopied) {
21588313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
21598313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
21608313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
21618313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
21628313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
21638313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
21648313SDina.Nimeh@Sun.Com 	}
21658313SDina.Nimeh@Sun.Com 
21668313SDina.Nimeh@Sun.Com 	if (zalloced)
21678313SDina.Nimeh@Sun.Com 		ddi_soft_state_free(lofi_statep, newminor);
21688313SDina.Nimeh@Sun.Com 
21690Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
21700Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
21718313SDina.Nimeh@Sun.Com 
21720Sstevel@tonic-gate out:
21738313SDina.Nimeh@Sun.Com 	if (need_vn_close) {
21748313SDina.Nimeh@Sun.Com 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
21758313SDina.Nimeh@Sun.Com 		VN_RELE(vp);
21768313SDina.Nimeh@Sun.Com 	}
21778313SDina.Nimeh@Sun.Com 
21780Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
21790Sstevel@tonic-gate 	free_lofi_ioctl(klip);
21800Sstevel@tonic-gate 	return (error);
21810Sstevel@tonic-gate }
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate /*
21840Sstevel@tonic-gate  * unmap a file.
21850Sstevel@tonic-gate  */
21860Sstevel@tonic-gate static int
21870Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
21881657Sheppo     struct cred *credp, int ioctl_flag)
21890Sstevel@tonic-gate {
21900Sstevel@tonic-gate 	struct lofi_state *lsp;
21910Sstevel@tonic-gate 	struct lofi_ioctl *klip;
21920Sstevel@tonic-gate 	minor_t	minor;
21930Sstevel@tonic-gate 
21941657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
21950Sstevel@tonic-gate 	if (klip == NULL)
21960Sstevel@tonic-gate 		return (EFAULT);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
21990Sstevel@tonic-gate 	if (byfilename) {
22000Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
22010Sstevel@tonic-gate 	} else {
22020Sstevel@tonic-gate 		minor = klip->li_minor;
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 	if (minor == 0) {
22050Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22060Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22070Sstevel@tonic-gate 		return (ENXIO);
22080Sstevel@tonic-gate 	}
22090Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
22104451Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
22110Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22120Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22130Sstevel@tonic-gate 		return (ENXIO);
22140Sstevel@tonic-gate 	}
22154451Seschrock 
22166734Sjohnlev 	/*
22176734Sjohnlev 	 * If it's still held open, we'll do one of three things:
22186734Sjohnlev 	 *
22196734Sjohnlev 	 * If no flag is set, just return EBUSY.
22206734Sjohnlev 	 *
22216734Sjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
22226734Sjohnlev 	 * the last user finishes.
22236734Sjohnlev 	 *
22246734Sjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
22256734Sjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
22266734Sjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
22276734Sjohnlev 	 * device will be cleaned up appropriately.
22286734Sjohnlev 	 *
22296734Sjohnlev 	 * This is complicated by the fact that we may have outstanding
22306734Sjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
22318313SDina.Nimeh@Sun.Com 	 * I/O, we keep a count of the number of outstanding I/O requests
22328313SDina.Nimeh@Sun.Com 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
22338313SDina.Nimeh@Sun.Com 	 * should be dispatched (ls_vp_closereq).
22348313SDina.Nimeh@Sun.Com 	 *
22356734Sjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
22366734Sjohnlev 	 * and then close the underlying vnode.
22376734Sjohnlev 	 */
22380Sstevel@tonic-gate 	if (is_opened(lsp)) {
22394451Seschrock 		if (klip->li_force) {
22408313SDina.Nimeh@Sun.Com 			/*
22418313SDina.Nimeh@Sun.Com 			 * XXX: the section marked here should probably be
22428313SDina.Nimeh@Sun.Com 			 * carefully incorporated into lofi_free_handle();
22438313SDina.Nimeh@Sun.Com 			 * afterward just replace this section with:
22448313SDina.Nimeh@Sun.Com 			 *	lofi_free_handle(dev, minor, lsp, credp);
22458313SDina.Nimeh@Sun.Com 			 * and clean up lofi_unmap_file() a bit more
22468313SDina.Nimeh@Sun.Com 			 */
22478313SDina.Nimeh@Sun.Com 			lofi_free_crypto(lsp);
22488313SDina.Nimeh@Sun.Com 
22494451Seschrock 			mutex_enter(&lsp->ls_vp_lock);
22504451Seschrock 			lsp->ls_vp_closereq = B_TRUE;
22514451Seschrock 			while (lsp->ls_vp_iocount > 0)
22524451Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
22534451Seschrock 			(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
22545331Samw 			    credp, NULL);
22554451Seschrock 			VN_RELE(lsp->ls_vp);
22564451Seschrock 			lsp->ls_vp = NULL;
22574451Seschrock 			cv_broadcast(&lsp->ls_vp_cv);
22584451Seschrock 			mutex_exit(&lsp->ls_vp_lock);
22598313SDina.Nimeh@Sun.Com 			/*
22608313SDina.Nimeh@Sun.Com 			 * XXX: to here
22618313SDina.Nimeh@Sun.Com 			 */
22628313SDina.Nimeh@Sun.Com 
22638313SDina.Nimeh@Sun.Com 			klip->li_minor = minor;
22644451Seschrock 			mutex_exit(&lofi_lock);
22654451Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22664451Seschrock 			free_lofi_ioctl(klip);
22674451Seschrock 			return (0);
22686734Sjohnlev 		} else if (klip->li_cleanup) {
22696734Sjohnlev 			lsp->ls_cleanup = 1;
22706734Sjohnlev 			mutex_exit(&lofi_lock);
22716734Sjohnlev 			free_lofi_ioctl(klip);
22726734Sjohnlev 			return (0);
22734451Seschrock 		}
22746734Sjohnlev 
22750Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22760Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22770Sstevel@tonic-gate 		return (EBUSY);
22780Sstevel@tonic-gate 	}
22790Sstevel@tonic-gate 
22804451Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	klip->li_minor = minor;
22830Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
22841657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22850Sstevel@tonic-gate 	free_lofi_ioctl(klip);
22860Sstevel@tonic-gate 	return (0);
22870Sstevel@tonic-gate }
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate /*
22900Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
22910Sstevel@tonic-gate  * the name.
22920Sstevel@tonic-gate  */
22934451Seschrock /*ARGSUSED*/
22940Sstevel@tonic-gate static int
22950Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
22961657Sheppo     struct cred *credp, int ioctl_flag)
22970Sstevel@tonic-gate {
22980Sstevel@tonic-gate 	struct lofi_state *lsp;
22990Sstevel@tonic-gate 	struct lofi_ioctl *klip;
23000Sstevel@tonic-gate 	int	error;
23010Sstevel@tonic-gate 	minor_t	minor;
23020Sstevel@tonic-gate 
23031657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
23040Sstevel@tonic-gate 	if (klip == NULL)
23050Sstevel@tonic-gate 		return (EFAULT);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	switch (which) {
23080Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
23090Sstevel@tonic-gate 		minor = klip->li_minor;
23100Sstevel@tonic-gate 		if (minor == 0) {
23110Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23120Sstevel@tonic-gate 			return (EINVAL);
23130Sstevel@tonic-gate 		}
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23160Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
23170Sstevel@tonic-gate 		if (lsp == NULL) {
23180Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
23190Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23200Sstevel@tonic-gate 			return (ENXIO);
23210Sstevel@tonic-gate 		}
23220Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
23235643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
23245643Saalok 		    sizeof (klip->li_algorithm));
23258313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
23260Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
23271657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23280Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23290Sstevel@tonic-gate 		return (error);
23300Sstevel@tonic-gate 	case LOFI_GET_MINOR:
23310Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23320Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
23338313SDina.Nimeh@Sun.Com 		/* caller should not depend on klip->li_crypto_enabled here */
23340Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
23350Sstevel@tonic-gate 		if (klip->li_minor == 0) {
23360Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23370Sstevel@tonic-gate 			return (ENOENT);
23380Sstevel@tonic-gate 		}
23391657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23400Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23410Sstevel@tonic-gate 		return (error);
23425643Saalok 	case LOFI_CHECK_COMPRESSED:
23435643Saalok 		mutex_enter(&lofi_lock);
23445643Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
23455643Saalok 		mutex_exit(&lofi_lock);
23465643Saalok 		if (klip->li_minor == 0) {
23475643Saalok 			free_lofi_ioctl(klip);
23485643Saalok 			return (ENOENT);
23495643Saalok 		}
23505643Saalok 		mutex_enter(&lofi_lock);
23515643Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
23525643Saalok 		if (lsp == NULL) {
23535643Saalok 			mutex_exit(&lofi_lock);
23545643Saalok 			free_lofi_ioctl(klip);
23555643Saalok 			return (ENXIO);
23565643Saalok 		}
23575643Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
23585643Saalok 
23595643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
23605643Saalok 		    sizeof (klip->li_algorithm));
23615643Saalok 		mutex_exit(&lofi_lock);
23625643Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23635643Saalok 		free_lofi_ioctl(klip);
23645643Saalok 		return (error);
23650Sstevel@tonic-gate 	default:
23660Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23670Sstevel@tonic-gate 		return (EINVAL);
23680Sstevel@tonic-gate 	}
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate static int
23730Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
23740Sstevel@tonic-gate     int *rvalp)
23750Sstevel@tonic-gate {
23760Sstevel@tonic-gate 	int	error;
23770Sstevel@tonic-gate 	enum dkio_state dkstate;
23780Sstevel@tonic-gate 	struct lofi_state *lsp;
23790Sstevel@tonic-gate 	minor_t	minor;
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	minor = getminor(dev);
23820Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
23830Sstevel@tonic-gate 	if (minor == 0) {
23840Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 		/*
23870Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
23880Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
23890Sstevel@tonic-gate 		 * long as they can open it read-only.
23900Sstevel@tonic-gate 		 */
23910Sstevel@tonic-gate 		switch (cmd) {
23920Sstevel@tonic-gate 		case LOFI_MAP_FILE:
23930Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23940Sstevel@tonic-gate 				return (EPERM);
23951657Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
23960Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
23970Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23980Sstevel@tonic-gate 				return (EPERM);
23991657Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
24000Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
24010Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24020Sstevel@tonic-gate 				return (EPERM);
24031657Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
24040Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
24050Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24060Sstevel@tonic-gate 				return (EPERM);
24071657Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
24080Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
24090Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
24101657Sheppo 			    credp, flag));
24110Sstevel@tonic-gate 		case LOFI_GET_MINOR:
24120Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
24131657Sheppo 			    credp, flag));
24140Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
24151657Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
24161657Sheppo 			    sizeof (lofi_max_files), flag);
24170Sstevel@tonic-gate 			if (error)
24180Sstevel@tonic-gate 				return (EFAULT);
24190Sstevel@tonic-gate 			return (0);
24205643Saalok 		case LOFI_CHECK_COMPRESSED:
24215643Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
24225643Saalok 			    credp, flag));
24230Sstevel@tonic-gate 		default:
24240Sstevel@tonic-gate 			break;
24250Sstevel@tonic-gate 		}
24260Sstevel@tonic-gate 	}
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
24290Sstevel@tonic-gate 	if (lsp == NULL)
24300Sstevel@tonic-gate 		return (ENXIO);
24310Sstevel@tonic-gate 
24324451Seschrock 	/*
24334451Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
24344451Seschrock 	 * EIO as if the device was no longer present.
24354451Seschrock 	 */
24364451Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
24374451Seschrock 		return (EIO);
24384451Seschrock 
24390Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
24400Sstevel@tonic-gate 	switch (cmd) {
24410Sstevel@tonic-gate 	case DKIOCGVTOC:
24420Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
24430Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
24440Sstevel@tonic-gate 			struct vtoc32 vtoc32;
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
24470Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
24480Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
24490Sstevel@tonic-gate 				return (EFAULT);
24508719SDina.Nimeh@Sun.COM 			break;
24510Sstevel@tonic-gate 			}
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 		case DDI_MODEL_NONE:
24540Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
24550Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
24560Sstevel@tonic-gate 				return (EFAULT);
24570Sstevel@tonic-gate 			break;
24580Sstevel@tonic-gate 		}
24590Sstevel@tonic-gate 		return (0);
24600Sstevel@tonic-gate 	case DKIOCINFO:
24611657Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
24621657Sheppo 		    sizeof (struct dk_cinfo), flag);
24630Sstevel@tonic-gate 		if (error)
24640Sstevel@tonic-gate 			return (EFAULT);
24650Sstevel@tonic-gate 		return (0);
24660Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
24670Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
24680Sstevel@tonic-gate 	case DKIOCGGEOM:
24691657Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
24701657Sheppo 		    sizeof (struct dk_geom), flag);
24710Sstevel@tonic-gate 		if (error)
24720Sstevel@tonic-gate 			return (EFAULT);
24730Sstevel@tonic-gate 		return (0);
24740Sstevel@tonic-gate 	case DKIOCSTATE:
24754451Seschrock 		/*
24764451Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
24774451Seschrock 		 * a device is forcefully unmapped, then the device transitions
24784451Seschrock 		 * to the DKIO_DEV_GONE state.
24794451Seschrock 		 */
24804451Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
24814451Seschrock 		    flag) != 0)
24824451Seschrock 			return (EFAULT);
24834451Seschrock 
24844451Seschrock 		mutex_enter(&lsp->ls_vp_lock);
24854451Seschrock 		while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
24864451Seschrock 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
24874451Seschrock 			/*
24884451Seschrock 			 * By virtue of having the device open, we know that
24894451Seschrock 			 * 'lsp' will remain valid when we return.
24904451Seschrock 			 */
24914451Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
24924451Seschrock 			    &lsp->ls_vp_lock)) {
24934451Seschrock 				mutex_exit(&lsp->ls_vp_lock);
24944451Seschrock 				return (EINTR);
24954451Seschrock 			}
24964451Seschrock 		}
24974451Seschrock 
24984451Seschrock 		dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
24994451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
25004451Seschrock 
25014451Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
25024451Seschrock 		    sizeof (dkstate), flag) != 0)
25030Sstevel@tonic-gate 			return (EFAULT);
25040Sstevel@tonic-gate 		return (0);
25050Sstevel@tonic-gate 	default:
25060Sstevel@tonic-gate 		return (ENOTTY);
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate }
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
25110Sstevel@tonic-gate 	lofi_open,		/* open */
25120Sstevel@tonic-gate 	lofi_close,		/* close */
25130Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
25140Sstevel@tonic-gate 	nodev,			/* print */
25150Sstevel@tonic-gate 	nodev,			/* dump */
25160Sstevel@tonic-gate 	lofi_read,		/* read */
25170Sstevel@tonic-gate 	lofi_write,		/* write */
25180Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
25190Sstevel@tonic-gate 	nodev,			/* devmap */
25200Sstevel@tonic-gate 	nodev,			/* mmap */
25210Sstevel@tonic-gate 	nodev,			/* segmap */
25220Sstevel@tonic-gate 	nochpoll,		/* poll */
25230Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
25240Sstevel@tonic-gate 	0,			/* streamtab  */
25250Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
25260Sstevel@tonic-gate 	CB_REV,
25270Sstevel@tonic-gate 	lofi_aread,
25280Sstevel@tonic-gate 	lofi_awrite
25290Sstevel@tonic-gate };
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate static struct dev_ops lofi_ops = {
25320Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
25330Sstevel@tonic-gate 	0,			/* refcnt  */
25340Sstevel@tonic-gate 	lofi_info,		/* info */
25350Sstevel@tonic-gate 	nulldev,		/* identify */
25360Sstevel@tonic-gate 	nulldev,		/* probe */
25370Sstevel@tonic-gate 	lofi_attach,		/* attach */
25380Sstevel@tonic-gate 	lofi_detach,		/* detach */
25390Sstevel@tonic-gate 	nodev,			/* reset */
25400Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
25417656SSherry.Moore@Sun.COM 	NULL,			/* no bus operations */
25427656SSherry.Moore@Sun.COM 	NULL,			/* power */
25438313SDina.Nimeh@Sun.Com 	ddi_quiesce_not_needed,	/* quiesce */
25440Sstevel@tonic-gate };
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate static struct modldrv modldrv = {
25470Sstevel@tonic-gate 	&mod_driverops,
25487656SSherry.Moore@Sun.COM 	"loopback file driver",
25490Sstevel@tonic-gate 	&lofi_ops,
25500Sstevel@tonic-gate };
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate static struct modlinkage modlinkage = {
25530Sstevel@tonic-gate 	MODREV_1,
25540Sstevel@tonic-gate 	&modldrv,
25550Sstevel@tonic-gate 	NULL
25560Sstevel@tonic-gate };
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate int
25590Sstevel@tonic-gate _init(void)
25600Sstevel@tonic-gate {
25610Sstevel@tonic-gate 	int error;
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
25640Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
25650Sstevel@tonic-gate 	if (error)
25660Sstevel@tonic-gate 		return (error);
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
25690Sstevel@tonic-gate 	error = mod_install(&modlinkage);
25700Sstevel@tonic-gate 	if (error) {
25710Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
25720Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
25730Sstevel@tonic-gate 	}
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	return (error);
25760Sstevel@tonic-gate }
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate int
25790Sstevel@tonic-gate _fini(void)
25800Sstevel@tonic-gate {
25810Sstevel@tonic-gate 	int	error;
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	if (lofi_busy())
25840Sstevel@tonic-gate 		return (EBUSY);
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
25870Sstevel@tonic-gate 	if (error)
25880Sstevel@tonic-gate 		return (error);
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
25910Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	return (error);
25940Sstevel@tonic-gate }
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate int
25970Sstevel@tonic-gate _info(struct modinfo *modinfop)
25980Sstevel@tonic-gate {
25990Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
26000Sstevel@tonic-gate }
2601