xref: /onnv-gate/usr/src/uts/common/io/lofi.c (revision 12384:1153e2887a7b)
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 /*
2212368SFrank.Batschulat@Sun.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
270Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
280Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
290Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
300Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
310Sstevel@tonic-gate  * mounting images of filesystems.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
340Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
350Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
360Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
370Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
380Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
390Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
400Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
410Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
420Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
450Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
460Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
470Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
480Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
490Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
520Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
530Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
540Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
550Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
560Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
570Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
580Sstevel@tonic-gate  *
594451Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
604451Seschrock  * support simulation of hotplug events, an optional force flag is provided.
614451Seschrock  * If a lofi device is open when a force detach is requested, then the
624451Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
634451Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
644451Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
654451Seschrock  * detached but not removed.
664451Seschrock  *
670Sstevel@tonic-gate  * Known problems:
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
700Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
710Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
720Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
750Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
760Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
770Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
800Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
810Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
820Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
830Sstevel@tonic-gate  *	do. Though it would still be silly.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * Interesting things to do:
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
900Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
918313SDina.Nimeh@Sun.Com  *	need to fake a geometry, however, it may be relevant if you're replacing
928313SDina.Nimeh@Sun.Com  *	metadisk, or using lofi to get crypto.
938313SDina.Nimeh@Sun.Com  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
948313SDina.Nimeh@Sun.Com  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
958313SDina.Nimeh@Sun.Com  *	In fact this even makes sense if you have lofi "above" metadisk.
960Sstevel@tonic-gate  *
978313SDina.Nimeh@Sun.Com  * Encryption:
988313SDina.Nimeh@Sun.Com  *	Each lofi device can have its own symmetric key and cipher.
998313SDina.Nimeh@Sun.Com  *	They are passed to us by lofiadm(1m) in the correct format for use
1008313SDina.Nimeh@Sun.Com  *	with the misc/kcf crypto_* routines.
1018313SDina.Nimeh@Sun.Com  *
1028313SDina.Nimeh@Sun.Com  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
1038313SDina.Nimeh@Sun.Com  *	on the "master" key held in the lsp and the block number of the buffer.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #include <sys/types.h>
1075643Saalok #include <netinet/in.h>
1080Sstevel@tonic-gate #include <sys/sysmacros.h>
1090Sstevel@tonic-gate #include <sys/uio.h>
1100Sstevel@tonic-gate #include <sys/kmem.h>
1110Sstevel@tonic-gate #include <sys/cred.h>
1120Sstevel@tonic-gate #include <sys/mman.h>
1130Sstevel@tonic-gate #include <sys/errno.h>
1140Sstevel@tonic-gate #include <sys/aio_req.h>
1150Sstevel@tonic-gate #include <sys/stat.h>
1160Sstevel@tonic-gate #include <sys/file.h>
1170Sstevel@tonic-gate #include <sys/modctl.h>
1180Sstevel@tonic-gate #include <sys/conf.h>
1190Sstevel@tonic-gate #include <sys/debug.h>
1200Sstevel@tonic-gate #include <sys/vnode.h>
1210Sstevel@tonic-gate #include <sys/lofi.h>
1220Sstevel@tonic-gate #include <sys/fcntl.h>
1230Sstevel@tonic-gate #include <sys/pathname.h>
1240Sstevel@tonic-gate #include <sys/filio.h>
1250Sstevel@tonic-gate #include <sys/fdio.h>
1260Sstevel@tonic-gate #include <sys/open.h>
1270Sstevel@tonic-gate #include <sys/disp.h>
1280Sstevel@tonic-gate #include <vm/seg_map.h>
1290Sstevel@tonic-gate #include <sys/ddi.h>
1300Sstevel@tonic-gate #include <sys/sunddi.h>
1315643Saalok #include <sys/zmod.h>
1328313SDina.Nimeh@Sun.Com #include <sys/crypto/common.h>
1338313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h>
1348996SAlok.Aggarwal@Sun.COM #include <LzmaDec.h>
1358313SDina.Nimeh@Sun.Com 
1368313SDina.Nimeh@Sun.Com /*
1378313SDina.Nimeh@Sun.Com  * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
1388313SDina.Nimeh@Sun.Com  * Crypto metadata, if it exists, is located at the end of the boot block
1398313SDina.Nimeh@Sun.Com  * (BBOFF + BBSIZE, which is SBOFF).  The super block and everything after
1408313SDina.Nimeh@Sun.Com  * is offset by the size of the crypto metadata which is handled by
1418313SDina.Nimeh@Sun.Com  * lsp->ls_crypto_offset.
1428313SDina.Nimeh@Sun.Com  */
1438313SDina.Nimeh@Sun.Com #define	CRYOFF	((off_t)8192)
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
1465643Saalok #define	SIZE_PROP_NAME		"Size"
1470Sstevel@tonic-gate 
1488313SDina.Nimeh@Sun.Com #define	SETUP_C_DATA(cd, buf, len) 		\
1498313SDina.Nimeh@Sun.Com 	(cd).cd_format = CRYPTO_DATA_RAW;	\
1508313SDina.Nimeh@Sun.Com 	(cd).cd_offset = 0;			\
1518313SDina.Nimeh@Sun.Com 	(cd).cd_miscdata = NULL;		\
1528313SDina.Nimeh@Sun.Com 	(cd).cd_length = (len);			\
1538313SDina.Nimeh@Sun.Com 	(cd).cd_raw.iov_base = (buf);		\
1548313SDina.Nimeh@Sun.Com 	(cd).cd_raw.iov_len = (len);
1558313SDina.Nimeh@Sun.Com 
1568313SDina.Nimeh@Sun.Com #define	UIO_CHECK(uio)	\
1578313SDina.Nimeh@Sun.Com 	if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
1588313SDina.Nimeh@Sun.Com 	    ((uio)->uio_resid % DEV_BSIZE) != 0) { \
1598313SDina.Nimeh@Sun.Com 		return (EINVAL); \
1608313SDina.Nimeh@Sun.Com 	}
1618313SDina.Nimeh@Sun.Com 
1628313SDina.Nimeh@Sun.Com static dev_info_t *lofi_dip = NULL;
1638313SDina.Nimeh@Sun.Com static void *lofi_statep = NULL;
1640Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1680Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1690Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
1700Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
1710Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1720Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
1730Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
1740Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
1750Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
1760Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
1790Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES;
1828313SDina.Nimeh@Sun.Com const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
1830Sstevel@tonic-gate 
1849048Sjrgn.keil@googlemail.com /*
1859048Sjrgn.keil@googlemail.com  * To avoid decompressing data in a compressed segment multiple times
1869048Sjrgn.keil@googlemail.com  * when accessing small parts of a segment's data, we cache and reuse
1879048Sjrgn.keil@googlemail.com  * the uncompressed segment's data.
1889048Sjrgn.keil@googlemail.com  *
1899048Sjrgn.keil@googlemail.com  * A single cached segment is sufficient to avoid lots of duplicate
1909048Sjrgn.keil@googlemail.com  * segment decompress operations. A small cache size also reduces the
1919048Sjrgn.keil@googlemail.com  * memory footprint.
1929048Sjrgn.keil@googlemail.com  *
1939048Sjrgn.keil@googlemail.com  * lofi_max_comp_cache is the maximum number of decompressed data segments
1949048Sjrgn.keil@googlemail.com  * cached for each compressed lofi image. It can be set to 0 to disable
1959048Sjrgn.keil@googlemail.com  * caching.
1969048Sjrgn.keil@googlemail.com  */
1979048Sjrgn.keil@googlemail.com 
1989048Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1;
1999048Sjrgn.keil@googlemail.com 
2005643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
2015643Saalok 	size_t *destlen, int level);
2025643Saalok 
2038996SAlok.Aggarwal@Sun.COM static int lzma_decompress(void *src, size_t srclen, void *dst,
2048996SAlok.Aggarwal@Sun.COM 	size_t *dstlen, int level);
2058996SAlok.Aggarwal@Sun.COM 
2065643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
2075643Saalok 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
2085643Saalok 	{gzip_decompress,	NULL,	6,	"gzip-6"},
2098996SAlok.Aggarwal@Sun.COM 	{gzip_decompress,	NULL,	9,	"gzip-9"},
2108996SAlok.Aggarwal@Sun.COM 	{lzma_decompress,	NULL,	0,	"lzma"}
2115643Saalok };
2125643Saalok 
2138996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2148996SAlok.Aggarwal@Sun.COM static void
2158996SAlok.Aggarwal@Sun.COM *SzAlloc(void *p, size_t size)
2168996SAlok.Aggarwal@Sun.COM {
2178996SAlok.Aggarwal@Sun.COM 	return (kmem_alloc(size, KM_SLEEP));
2188996SAlok.Aggarwal@Sun.COM }
2198996SAlok.Aggarwal@Sun.COM 
2208996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2218996SAlok.Aggarwal@Sun.COM static void
2228996SAlok.Aggarwal@Sun.COM SzFree(void *p, void *address, size_t size)
2238996SAlok.Aggarwal@Sun.COM {
2248996SAlok.Aggarwal@Sun.COM 	kmem_free(address, size);
2258996SAlok.Aggarwal@Sun.COM }
2268996SAlok.Aggarwal@Sun.COM 
2278996SAlok.Aggarwal@Sun.COM static ISzAlloc g_Alloc = { SzAlloc, SzFree };
2288996SAlok.Aggarwal@Sun.COM 
2299048Sjrgn.keil@googlemail.com /*
2309048Sjrgn.keil@googlemail.com  * Free data referenced by the linked list of cached uncompressed
2319048Sjrgn.keil@googlemail.com  * segments.
2329048Sjrgn.keil@googlemail.com  */
2339048Sjrgn.keil@googlemail.com static void
2349048Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp)
2359048Sjrgn.keil@googlemail.com {
2369048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
2379048Sjrgn.keil@googlemail.com 
2389048Sjrgn.keil@googlemail.com 	while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
2399048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
2409048Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
2419048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
2429048Sjrgn.keil@googlemail.com 	}
2439048Sjrgn.keil@googlemail.com 	ASSERT(lsp->ls_comp_cache_count == 0);
2449048Sjrgn.keil@googlemail.com }
2459048Sjrgn.keil@googlemail.com 
2460Sstevel@tonic-gate static int
2470Sstevel@tonic-gate lofi_busy(void)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	minor_t	minor;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
2530Sstevel@tonic-gate 	 * help because the device isn't open.
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
2560Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
2570Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
2580Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2590Sstevel@tonic-gate 			return (EBUSY);
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2630Sstevel@tonic-gate 	return (0);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static int
2670Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2700Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static int
2740Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2770Sstevel@tonic-gate 	switch (otyp) {
2780Sstevel@tonic-gate 	case OTYP_CHR:
2790Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
2800Sstevel@tonic-gate 		break;
2810Sstevel@tonic-gate 	case OTYP_BLK:
2820Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
2830Sstevel@tonic-gate 		break;
2840Sstevel@tonic-gate 	case OTYP_LYR:
2850Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
2860Sstevel@tonic-gate 		break;
2870Sstevel@tonic-gate 	default:
2880Sstevel@tonic-gate 		return (-1);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 	return (0);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static void
2940Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2970Sstevel@tonic-gate 	switch (otyp) {
2980Sstevel@tonic-gate 	case OTYP_CHR:
2990Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
3000Sstevel@tonic-gate 		break;
3010Sstevel@tonic-gate 	case OTYP_BLK:
3020Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
3030Sstevel@tonic-gate 		break;
3040Sstevel@tonic-gate 	case OTYP_LYR:
3050Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	default:
3080Sstevel@tonic-gate 		break;
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3124451Seschrock static void
3138313SDina.Nimeh@Sun.Com lofi_free_crypto(struct lofi_state *lsp)
3148313SDina.Nimeh@Sun.Com {
3158313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
3168313SDina.Nimeh@Sun.Com 
3178313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
3188313SDina.Nimeh@Sun.Com 		/*
3198313SDina.Nimeh@Sun.Com 		 * Clean up the crypto state so that it doesn't hang around
3208313SDina.Nimeh@Sun.Com 		 * in memory after we are done with it.
3218313SDina.Nimeh@Sun.Com 		 */
3228313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
3238313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3248313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
3258313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3268313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
3278313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
3288313SDina.Nimeh@Sun.Com 
3298313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != NULL) {
3308313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_mech.cm_param,
3318313SDina.Nimeh@Sun.Com 			    lsp->ls_mech.cm_param_len);
3328313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param = NULL;
3338313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param_len = 0;
3348313SDina.Nimeh@Sun.Com 		}
3358313SDina.Nimeh@Sun.Com 
3368313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_param != NULL) {
3378313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_iv_mech.cm_param,
3388313SDina.Nimeh@Sun.Com 			    lsp->ls_iv_mech.cm_param_len);
3398313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param = NULL;
3408313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param_len = 0;
3418313SDina.Nimeh@Sun.Com 		}
3428313SDina.Nimeh@Sun.Com 
3438313SDina.Nimeh@Sun.Com 		mutex_destroy(&lsp->ls_crypto_lock);
3448313SDina.Nimeh@Sun.Com 	}
3458313SDina.Nimeh@Sun.Com }
3468313SDina.Nimeh@Sun.Com 
3478313SDina.Nimeh@Sun.Com static void
3484451Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
3494451Seschrock     cred_t *credp)
3504451Seschrock {
3514451Seschrock 	dev_t	newdev;
3524451Seschrock 	char	namebuf[50];
353*12384Salok.aggarwal@oracle.com 	int	i;
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 
394*12384Salok.aggarwal@oracle.com 	/*
395*12384Salok.aggarwal@oracle.com 	 * Free pre-allocated compressed buffers
396*12384Salok.aggarwal@oracle.com 	 */
397*12384Salok.aggarwal@oracle.com 	if (lsp->ls_comp_bufs != NULL) {
398*12384Salok.aggarwal@oracle.com 		for (i = 0; i < lofi_taskq_nthreads; i++) {
399*12384Salok.aggarwal@oracle.com 			if (lsp->ls_comp_bufs[i].bufsize > 0)
400*12384Salok.aggarwal@oracle.com 				kmem_free(lsp->ls_comp_bufs[i].buf,
401*12384Salok.aggarwal@oracle.com 				    lsp->ls_comp_bufs[i].bufsize);
402*12384Salok.aggarwal@oracle.com 		}
403*12384Salok.aggarwal@oracle.com 		kmem_free(lsp->ls_comp_bufs,
404*12384Salok.aggarwal@oracle.com 		    sizeof (struct compbuf) * lofi_taskq_nthreads);
405*12384Salok.aggarwal@oracle.com 		mutex_destroy(&lsp->ls_comp_bufs_lock);
406*12384Salok.aggarwal@oracle.com 	}
407*12384Salok.aggarwal@oracle.com 
4089048Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_vp_lock);
4099048Sjrgn.keil@googlemail.com 
4104451Seschrock 	ddi_soft_state_free(lofi_statep, minor);
4114451Seschrock }
4124451Seschrock 
4134451Seschrock /*ARGSUSED*/
4140Sstevel@tonic-gate static int
4150Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	minor_t	minor;
4180Sstevel@tonic-gate 	struct lofi_state *lsp;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4210Sstevel@tonic-gate 	minor = getminor(*devp);
4220Sstevel@tonic-gate 	if (minor == 0) {
4230Sstevel@tonic-gate 		/* master control device */
4240Sstevel@tonic-gate 		/* must be opened exclusively */
4250Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
4260Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4270Sstevel@tonic-gate 			return (EINVAL);
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
4300Sstevel@tonic-gate 		if (lsp == NULL) {
4310Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4320Sstevel@tonic-gate 			return (ENXIO);
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 		if (is_opened(lsp)) {
4350Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4360Sstevel@tonic-gate 			return (EBUSY);
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
4390Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4400Sstevel@tonic-gate 		return (0);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
4440Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4450Sstevel@tonic-gate 	if (lsp == NULL) {
4460Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4470Sstevel@tonic-gate 		return (EINVAL);
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4504451Seschrock 	if (lsp->ls_vp == NULL) {
4514451Seschrock 		mutex_exit(&lofi_lock);
4524451Seschrock 		return (ENXIO);
4534451Seschrock 	}
4544451Seschrock 
4550Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
4560Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4570Sstevel@tonic-gate 		return (EINVAL);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4610Sstevel@tonic-gate 	return (0);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4644451Seschrock /*ARGSUSED*/
4650Sstevel@tonic-gate static int
4660Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate 	minor_t	minor;
4690Sstevel@tonic-gate 	struct lofi_state *lsp;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4720Sstevel@tonic-gate 	minor = getminor(dev);
4730Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4740Sstevel@tonic-gate 	if (lsp == NULL) {
4750Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4760Sstevel@tonic-gate 		return (EINVAL);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	mark_closed(lsp, otyp);
4794451Seschrock 
4804451Seschrock 	/*
4816734Sjohnlev 	 * If we forcibly closed the underlying device (li_force), or
4826734Sjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
4836734Sjohnlev 	 * out of the door.
4844451Seschrock 	 */
4856734Sjohnlev 	if (minor != 0 && !is_opened(lsp) &&
4866734Sjohnlev 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
4874451Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
4886734Sjohnlev 
4890Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4900Sstevel@tonic-gate 	return (0);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4938313SDina.Nimeh@Sun.Com /*
4948313SDina.Nimeh@Sun.Com  * Sets the mechanism's initialization vector (IV) if one is needed.
4958313SDina.Nimeh@Sun.Com  * The IV is computed from the data block number.  lsp->ls_mech is
4968313SDina.Nimeh@Sun.Com  * altered so that:
4978313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param_len is set to the IV len.
4988313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param is set to the IV.
4998313SDina.Nimeh@Sun.Com  */
5008313SDina.Nimeh@Sun.Com static int
5018313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
5028313SDina.Nimeh@Sun.Com {
5038313SDina.Nimeh@Sun.Com 	int	ret;
5048313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
5058313SDina.Nimeh@Sun.Com 	char	*iv;
5068313SDina.Nimeh@Sun.Com 	size_t	iv_len;
5078313SDina.Nimeh@Sun.Com 	size_t	min;
5088313SDina.Nimeh@Sun.Com 	void	*data;
5098313SDina.Nimeh@Sun.Com 	size_t	datasz;
5108313SDina.Nimeh@Sun.Com 
5118313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
5128313SDina.Nimeh@Sun.Com 
5138313SDina.Nimeh@Sun.Com 	if (lsp == NULL)
5148313SDina.Nimeh@Sun.Com 		return (CRYPTO_DEVICE_ERROR);
5158313SDina.Nimeh@Sun.Com 
5168313SDina.Nimeh@Sun.Com 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
5178313SDina.Nimeh@Sun.Com 	if (lsp->ls_iv_type == IVM_NONE) {
5188313SDina.Nimeh@Sun.Com 		return (CRYPTO_SUCCESS);
5198313SDina.Nimeh@Sun.Com 	}
5208313SDina.Nimeh@Sun.Com 
5218313SDina.Nimeh@Sun.Com 	/*
5228313SDina.Nimeh@Sun.Com 	 * if kmem already alloced from previous call and it's the same size
5238313SDina.Nimeh@Sun.Com 	 * we need now, just recycle it; allocate new kmem only if we have to
5248313SDina.Nimeh@Sun.Com 	 */
5258313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param == NULL ||
5268313SDina.Nimeh@Sun.Com 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
5278313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_iv_len;
5288313SDina.Nimeh@Sun.Com 		iv = kmem_zalloc(iv_len, KM_SLEEP);
5298313SDina.Nimeh@Sun.Com 	} else {
5308313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_mech.cm_param_len;
5318313SDina.Nimeh@Sun.Com 		iv = lsp->ls_mech.cm_param;
5328313SDina.Nimeh@Sun.Com 		bzero(iv, iv_len);
5338313SDina.Nimeh@Sun.Com 	}
5348313SDina.Nimeh@Sun.Com 
5358313SDina.Nimeh@Sun.Com 	switch (lsp->ls_iv_type) {
5368313SDina.Nimeh@Sun.Com 	case IVM_ENC_BLKNO:
5378313SDina.Nimeh@Sun.Com 		/* iv is not static, lblkno changes each time */
5388313SDina.Nimeh@Sun.Com 		data = &lblkno;
5398313SDina.Nimeh@Sun.Com 		datasz = sizeof (lblkno);
5408313SDina.Nimeh@Sun.Com 		break;
5418313SDina.Nimeh@Sun.Com 	default:
5428313SDina.Nimeh@Sun.Com 		data = 0;
5438313SDina.Nimeh@Sun.Com 		datasz = 0;
5448313SDina.Nimeh@Sun.Com 		break;
5458313SDina.Nimeh@Sun.Com 	}
5468313SDina.Nimeh@Sun.Com 
5478313SDina.Nimeh@Sun.Com 	/*
5488313SDina.Nimeh@Sun.Com 	 * write blkno into the iv buffer padded on the left in case
5498313SDina.Nimeh@Sun.Com 	 * blkno ever grows bigger than its current longlong_t size
5508313SDina.Nimeh@Sun.Com 	 * or a variation other than blkno is used for the iv data
5518313SDina.Nimeh@Sun.Com 	 */
5528313SDina.Nimeh@Sun.Com 	min = MIN(datasz, iv_len);
5538313SDina.Nimeh@Sun.Com 	bcopy(data, iv + (iv_len - min), min);
5548313SDina.Nimeh@Sun.Com 
5558313SDina.Nimeh@Sun.Com 	/* encrypt the data in-place to get the IV */
5568313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, iv, iv_len);
5578313SDina.Nimeh@Sun.Com 
5588313SDina.Nimeh@Sun.Com 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
5598313SDina.Nimeh@Sun.Com 	    NULL, NULL, NULL);
5608313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
5618313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
5628313SDina.Nimeh@Sun.Com 		    lblkno, ret);
5638313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != iv)
5648313SDina.Nimeh@Sun.Com 			kmem_free(iv, iv_len);
5658996SAlok.Aggarwal@Sun.COM 
5668313SDina.Nimeh@Sun.Com 		return (ret);
5678313SDina.Nimeh@Sun.Com 	}
5688313SDina.Nimeh@Sun.Com 
5698313SDina.Nimeh@Sun.Com 	/* clean up the iv from the last computation */
5708313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
5718313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
5728996SAlok.Aggarwal@Sun.COM 
5738313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param_len = iv_len;
5748313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param = iv;
5758313SDina.Nimeh@Sun.Com 
5768313SDina.Nimeh@Sun.Com 	return (CRYPTO_SUCCESS);
5778313SDina.Nimeh@Sun.Com }
5788313SDina.Nimeh@Sun.Com 
5798313SDina.Nimeh@Sun.Com /*
5808313SDina.Nimeh@Sun.Com  * Performs encryption and decryption of a chunk of data of size "len",
5818313SDina.Nimeh@Sun.Com  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
5828313SDina.Nimeh@Sun.Com  * DEV_BSIZE.
5838313SDina.Nimeh@Sun.Com  */
5848313SDina.Nimeh@Sun.Com static int
5858313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
5868313SDina.Nimeh@Sun.Com     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
5878313SDina.Nimeh@Sun.Com {
5888313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
5898313SDina.Nimeh@Sun.Com 	crypto_data_t wdata;
5908313SDina.Nimeh@Sun.Com 	int ret;
5918313SDina.Nimeh@Sun.Com 	longlong_t lblkno = bp->b_lblkno;
5928313SDina.Nimeh@Sun.Com 
5938313SDina.Nimeh@Sun.Com 	mutex_enter(&lsp->ls_crypto_lock);
5948313SDina.Nimeh@Sun.Com 
5958313SDina.Nimeh@Sun.Com 	/*
5968313SDina.Nimeh@Sun.Com 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
5978313SDina.Nimeh@Sun.Com 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
5988313SDina.Nimeh@Sun.Com 	 */
5998313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, plaintext, len);
6008313SDina.Nimeh@Sun.Com 	cdata.cd_length = DEV_BSIZE;
6018313SDina.Nimeh@Sun.Com 	if (ciphertext != NULL) {		/* not in-place crypto */
6028313SDina.Nimeh@Sun.Com 		SETUP_C_DATA(wdata, ciphertext, len);
6038313SDina.Nimeh@Sun.Com 		wdata.cd_length = DEV_BSIZE;
6048313SDina.Nimeh@Sun.Com 	}
6058313SDina.Nimeh@Sun.Com 
6068313SDina.Nimeh@Sun.Com 	do {
6078313SDina.Nimeh@Sun.Com 		ret = lofi_blk_mech(lsp, lblkno);
6088313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS)
6098313SDina.Nimeh@Sun.Com 			continue;
6108313SDina.Nimeh@Sun.Com 
6118313SDina.Nimeh@Sun.Com 		if (op_encrypt) {
6128313SDina.Nimeh@Sun.Com 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
6138313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
6148313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6158313SDina.Nimeh@Sun.Com 		} else {
6168313SDina.Nimeh@Sun.Com 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
6178313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
6188313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6198313SDina.Nimeh@Sun.Com 		}
6208313SDina.Nimeh@Sun.Com 
6218313SDina.Nimeh@Sun.Com 		cdata.cd_offset += DEV_BSIZE;
6228313SDina.Nimeh@Sun.Com 		if (ciphertext != NULL)
6238313SDina.Nimeh@Sun.Com 			wdata.cd_offset += DEV_BSIZE;
6248313SDina.Nimeh@Sun.Com 		lblkno++;
6258313SDina.Nimeh@Sun.Com 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
6268313SDina.Nimeh@Sun.Com 
6278313SDina.Nimeh@Sun.Com 	mutex_exit(&lsp->ls_crypto_lock);
6288313SDina.Nimeh@Sun.Com 
6298313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
6308313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
6318313SDina.Nimeh@Sun.Com 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
6328313SDina.Nimeh@Sun.Com 		    lblkno, ret);
6338313SDina.Nimeh@Sun.Com 	}
6348313SDina.Nimeh@Sun.Com 
6358313SDina.Nimeh@Sun.Com 	return (ret);
6368313SDina.Nimeh@Sun.Com }
6378313SDina.Nimeh@Sun.Com 
6388313SDina.Nimeh@Sun.Com #define	RDWR_RAW	1
6398313SDina.Nimeh@Sun.Com #define	RDWR_BCOPY	2
6408313SDina.Nimeh@Sun.Com 
6418313SDina.Nimeh@Sun.Com static int
6428313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6438313SDina.Nimeh@Sun.Com     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
6448313SDina.Nimeh@Sun.Com {
6458313SDina.Nimeh@Sun.Com 	ssize_t resid;
6468313SDina.Nimeh@Sun.Com 	int isread;
6478313SDina.Nimeh@Sun.Com 	int error;
6488313SDina.Nimeh@Sun.Com 
6498313SDina.Nimeh@Sun.Com 	/*
6508313SDina.Nimeh@Sun.Com 	 * Handles reads/writes for both plain and encrypted lofi
6518313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
6528313SDina.Nimeh@Sun.Com 	 * when it gets here.
6538313SDina.Nimeh@Sun.Com 	 */
6548313SDina.Nimeh@Sun.Com 
6558313SDina.Nimeh@Sun.Com 	isread = bp->b_flags & B_READ;
6568313SDina.Nimeh@Sun.Com 	if (isread) {
6578313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6588313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6598313SDina.Nimeh@Sun.Com 			bcopy(bcopy_locn, bufaddr, len);
6608313SDina.Nimeh@Sun.Com 			error = 0;
6618313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
6628313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
6638313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6648313SDina.Nimeh@Sun.Com 			    &resid);
6658313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
6668313SDina.Nimeh@Sun.Com 		}
6678313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled && error == 0) {
6688313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
6698313SDina.Nimeh@Sun.Com 			    B_FALSE) != CRYPTO_SUCCESS) {
6708313SDina.Nimeh@Sun.Com 				/*
6718313SDina.Nimeh@Sun.Com 				 * XXX: original code didn't set residual
6728313SDina.Nimeh@Sun.Com 				 * back to len because no error was expected
6738313SDina.Nimeh@Sun.Com 				 * from bcopy() if encryption is not enabled
6748313SDina.Nimeh@Sun.Com 				 */
6758313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6768313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6778313SDina.Nimeh@Sun.Com 				error = EIO;
6788313SDina.Nimeh@Sun.Com 			}
6798313SDina.Nimeh@Sun.Com 		}
6808313SDina.Nimeh@Sun.Com 		return (error);
6818313SDina.Nimeh@Sun.Com 	} else {
6828313SDina.Nimeh@Sun.Com 		void *iobuf = bufaddr;
6838313SDina.Nimeh@Sun.Com 
6848313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6858313SDina.Nimeh@Sun.Com 			/* don't do in-place crypto to keep bufaddr intact */
6868313SDina.Nimeh@Sun.Com 			iobuf = kmem_alloc(len, KM_SLEEP);
6878313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
6888313SDina.Nimeh@Sun.Com 			    B_TRUE) != CRYPTO_SUCCESS) {
6898313SDina.Nimeh@Sun.Com 				kmem_free(iobuf, len);
6908313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6918313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6928313SDina.Nimeh@Sun.Com 				return (EIO);
6938313SDina.Nimeh@Sun.Com 			}
6948313SDina.Nimeh@Sun.Com 		}
6958313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6968313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6978313SDina.Nimeh@Sun.Com 			bcopy(iobuf, bcopy_locn, len);
6988313SDina.Nimeh@Sun.Com 			error = 0;
6998313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
7008313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
7018313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
7028313SDina.Nimeh@Sun.Com 			    &resid);
7038313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
7048313SDina.Nimeh@Sun.Com 		}
7058313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
7068313SDina.Nimeh@Sun.Com 			kmem_free(iobuf, len);
7078313SDina.Nimeh@Sun.Com 		}
7088313SDina.Nimeh@Sun.Com 		return (error);
7098313SDina.Nimeh@Sun.Com 	}
7108313SDina.Nimeh@Sun.Com }
7118313SDina.Nimeh@Sun.Com 
7125643Saalok static int
7135643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
7148313SDina.Nimeh@Sun.Com     struct lofi_state *lsp)
7155643Saalok {
7165643Saalok 	int error;
7175643Saalok 	offset_t alignedoffset, mapoffset;
7185643Saalok 	size_t	xfersize;
7195643Saalok 	int	isread;
7208313SDina.Nimeh@Sun.Com 	int	smflags;
7215643Saalok 	caddr_t	mapaddr;
7225643Saalok 	size_t	len;
7235643Saalok 	enum seg_rw srw;
7248313SDina.Nimeh@Sun.Com 	int	save_error;
7258313SDina.Nimeh@Sun.Com 
7268313SDina.Nimeh@Sun.Com 	/*
7278313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
7288313SDina.Nimeh@Sun.Com 	 * when it gets here.
7298313SDina.Nimeh@Sun.Com 	 */
7308313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled)
7318313SDina.Nimeh@Sun.Com 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
7325643Saalok 
7335643Saalok 	/*
7345643Saalok 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
7355643Saalok 	 * an 8K boundary, but the buf transfer address may not be
7365643Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
7375643Saalok 	 * that even though we could). This matters since the initial
7385643Saalok 	 * part of the transfer may not start at offset 0 within the
7395643Saalok 	 * segmap'd chunk. So we have to compensate for that with
7405643Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
7415643Saalok 	 * beginning, and the last is capped by b_resid
7428313SDina.Nimeh@Sun.Com 	 *
7438313SDina.Nimeh@Sun.Com 	 * Visually, where "|" represents page map boundaries:
7448313SDina.Nimeh@Sun.Com 	 *   alignedoffset (mapaddr begins at this segmap boundary)
7458313SDina.Nimeh@Sun.Com 	 *    |   offset (from beginning of file)
7468313SDina.Nimeh@Sun.Com 	 *    |    |	   len
7478313SDina.Nimeh@Sun.Com 	 *    v    v	    v
7488313SDina.Nimeh@Sun.Com 	 * ===|====X========|====...======|========X====|====
7498313SDina.Nimeh@Sun.Com 	 *	   /-------------...---------------/
7508313SDina.Nimeh@Sun.Com 	 *		^ bp->b_bcount/bp->b_resid at start
7518313SDina.Nimeh@Sun.Com 	 *    /----/--------/----...------/--------/
7528313SDina.Nimeh@Sun.Com 	 *	^	^	^   ^		^
7538313SDina.Nimeh@Sun.Com 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
7548313SDina.Nimeh@Sun.Com 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
7558313SDina.Nimeh@Sun.Com 	 *	|	1st xfersize (<= MAXBSIZE)
7568313SDina.Nimeh@Sun.Com 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
7578313SDina.Nimeh@Sun.Com 	 *
7588313SDina.Nimeh@Sun.Com 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
7598313SDina.Nimeh@Sun.Com 	 * MAXBSIZE boundary.  "len" is next page boundary of size
7608719SDina.Nimeh@Sun.COM 	 * PAGESIZE after "alignedoffset".
7615643Saalok 	 */
7625643Saalok 	mapoffset = offset & MAXBOFFSET;
7635643Saalok 	alignedoffset = offset - mapoffset;
7645643Saalok 	bp->b_resid = bp->b_bcount;
7655643Saalok 	isread = bp->b_flags & B_READ;
7665643Saalok 	srw = isread ? S_READ : S_WRITE;
7675643Saalok 	do {
7685643Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
7695643Saalok 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
7708719SDina.Nimeh@Sun.COM 		len = roundup(mapoffset + xfersize, PAGESIZE);
7715643Saalok 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
7725643Saalok 		    alignedoffset, MAXBSIZE, 1, srw);
7735643Saalok 		/*
7745643Saalok 		 * Now fault in the pages. This lets us check
7755643Saalok 		 * for errors before we reference mapaddr and
7765643Saalok 		 * try to resolve the fault in bcopy (which would
7775643Saalok 		 * panic instead). And this can easily happen,
7785643Saalok 		 * particularly if you've lofi'd a file over NFS
7795643Saalok 		 * and someone deletes the file on the server.
7805643Saalok 		 */
7815643Saalok 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
7825643Saalok 		    len, F_SOFTLOCK, srw);
7835643Saalok 		if (error) {
7845643Saalok 			(void) segmap_release(segkmap, mapaddr, 0);
7855643Saalok 			if (FC_CODE(error) == FC_OBJERR)
7865643Saalok 				error = FC_ERRNO(error);
7875643Saalok 			else
7885643Saalok 				error = EIO;
7895643Saalok 			break;
7905643Saalok 		}
7918313SDina.Nimeh@Sun.Com 		/* error may be non-zero for encrypted lofi */
7928313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
7938313SDina.Nimeh@Sun.Com 		    RDWR_BCOPY, mapaddr + mapoffset);
7948313SDina.Nimeh@Sun.Com 		if (error == 0) {
7958313SDina.Nimeh@Sun.Com 			bp->b_resid -= xfersize;
7968313SDina.Nimeh@Sun.Com 			bufaddr += xfersize;
7978313SDina.Nimeh@Sun.Com 			offset += xfersize;
7988313SDina.Nimeh@Sun.Com 		}
7995643Saalok 		smflags = 0;
8005643Saalok 		if (isread) {
8015643Saalok 			smflags |= SM_FREE;
8025643Saalok 			/*
8035643Saalok 			 * If we're reading an entire page starting
8045643Saalok 			 * at a page boundary, there's a good chance
8055643Saalok 			 * we won't need it again. Put it on the
8065643Saalok 			 * head of the freelist.
8075643Saalok 			 */
8088056SDina.Nimeh@Sun.Com 			if (mapoffset == 0 && xfersize == MAXBSIZE)
8095643Saalok 				smflags |= SM_DONTNEED;
8105643Saalok 		} else {
81112368SFrank.Batschulat@Sun.COM 			/*
81212368SFrank.Batschulat@Sun.COM 			 * Write back good pages, it is okay to
81312368SFrank.Batschulat@Sun.COM 			 * always release asynchronous here as we'll
81412368SFrank.Batschulat@Sun.COM 			 * follow with VOP_FSYNC for B_SYNC buffers.
81512368SFrank.Batschulat@Sun.COM 			 */
81612368SFrank.Batschulat@Sun.COM 			if (error == 0)
81712368SFrank.Batschulat@Sun.COM 				smflags |= SM_WRITE | SM_ASYNC;
8185643Saalok 		}
8195643Saalok 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
8205643Saalok 		    len, F_SOFTUNLOCK, srw);
8218313SDina.Nimeh@Sun.Com 		save_error = segmap_release(segkmap, mapaddr, smflags);
8228313SDina.Nimeh@Sun.Com 		if (error == 0)
8238313SDina.Nimeh@Sun.Com 			error = save_error;
8245643Saalok 		/* only the first map may start partial */
8255643Saalok 		mapoffset = 0;
8265643Saalok 		alignedoffset += MAXBSIZE;
8275643Saalok 	} while ((error == 0) && (bp->b_resid > 0) &&
8285643Saalok 	    (offset < lsp->ls_vp_comp_size));
8295643Saalok 
8305643Saalok 	return (error);
8315643Saalok }
8325643Saalok 
8339048Sjrgn.keil@googlemail.com /*
8349048Sjrgn.keil@googlemail.com  * Check if segment seg_index is present in the decompressed segment
8359048Sjrgn.keil@googlemail.com  * data cache.
8369048Sjrgn.keil@googlemail.com  *
8379048Sjrgn.keil@googlemail.com  * Returns a pointer to the decompressed segment data cache entry if
8389048Sjrgn.keil@googlemail.com  * found, and NULL when decompressed data for this segment is not yet
8399048Sjrgn.keil@googlemail.com  * cached.
8409048Sjrgn.keil@googlemail.com  */
8419048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8429048Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
8439048Sjrgn.keil@googlemail.com {
8449048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8459048Sjrgn.keil@googlemail.com 
8469048Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8479048Sjrgn.keil@googlemail.com 
8489048Sjrgn.keil@googlemail.com 	for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
8499048Sjrgn.keil@googlemail.com 	    lc = list_next(&lsp->ls_comp_cache, lc)) {
8509048Sjrgn.keil@googlemail.com 		if (lc->lc_index == seg_index) {
8519048Sjrgn.keil@googlemail.com 			/*
8529048Sjrgn.keil@googlemail.com 			 * Decompressed segment data was found in the
8539048Sjrgn.keil@googlemail.com 			 * cache.
8549048Sjrgn.keil@googlemail.com 			 *
8559048Sjrgn.keil@googlemail.com 			 * The cache uses an LRU replacement strategy;
8569048Sjrgn.keil@googlemail.com 			 * move the entry to head of list.
8579048Sjrgn.keil@googlemail.com 			 */
8589048Sjrgn.keil@googlemail.com 			list_remove(&lsp->ls_comp_cache, lc);
8599048Sjrgn.keil@googlemail.com 			list_insert_head(&lsp->ls_comp_cache, lc);
8609048Sjrgn.keil@googlemail.com 			return (lc);
8619048Sjrgn.keil@googlemail.com 		}
8629048Sjrgn.keil@googlemail.com 	}
8639048Sjrgn.keil@googlemail.com 	return (NULL);
8649048Sjrgn.keil@googlemail.com }
8659048Sjrgn.keil@googlemail.com 
8669048Sjrgn.keil@googlemail.com /*
8679048Sjrgn.keil@googlemail.com  * Add the data for a decompressed segment at segment index
8689048Sjrgn.keil@googlemail.com  * seg_index to the cache of the decompressed segments.
8699048Sjrgn.keil@googlemail.com  *
8709048Sjrgn.keil@googlemail.com  * Returns a pointer to the cache element structure in case
8719048Sjrgn.keil@googlemail.com  * the data was added to the cache; returns NULL when the data
8729048Sjrgn.keil@googlemail.com  * wasn't cached.
8739048Sjrgn.keil@googlemail.com  */
8749048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8759048Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
8769048Sjrgn.keil@googlemail.com     uchar_t *data)
8779048Sjrgn.keil@googlemail.com {
8789048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8799048Sjrgn.keil@googlemail.com 
8809048Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8819048Sjrgn.keil@googlemail.com 
8829048Sjrgn.keil@googlemail.com 	while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
8839048Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8849048Sjrgn.keil@googlemail.com 		ASSERT(lc != NULL);
8859048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
8869048Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
8879048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
8889048Sjrgn.keil@googlemail.com 	}
8899048Sjrgn.keil@googlemail.com 
8909048Sjrgn.keil@googlemail.com 	/*
8919048Sjrgn.keil@googlemail.com 	 * Do not cache when disabled by tunable variable
8929048Sjrgn.keil@googlemail.com 	 */
8939048Sjrgn.keil@googlemail.com 	if (lofi_max_comp_cache == 0)
8949048Sjrgn.keil@googlemail.com 		return (NULL);
8959048Sjrgn.keil@googlemail.com 
8969048Sjrgn.keil@googlemail.com 	/*
8979048Sjrgn.keil@googlemail.com 	 * When the cache has not yet reached the maximum allowed
8989048Sjrgn.keil@googlemail.com 	 * number of segments, allocate a new cache element.
8999048Sjrgn.keil@googlemail.com 	 * Otherwise the cache is full; reuse the last list element
9009048Sjrgn.keil@googlemail.com 	 * (LRU) for caching the decompressed segment data.
9019048Sjrgn.keil@googlemail.com 	 *
9029048Sjrgn.keil@googlemail.com 	 * The cache element for the new decompressed segment data is
9039048Sjrgn.keil@googlemail.com 	 * added to the head of the list.
9049048Sjrgn.keil@googlemail.com 	 */
9059048Sjrgn.keil@googlemail.com 	if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
9069048Sjrgn.keil@googlemail.com 		lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
9079048Sjrgn.keil@googlemail.com 		lc->lc_data = NULL;
9089048Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
9099048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count++;
9109048Sjrgn.keil@googlemail.com 	} else {
9119048Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
9129048Sjrgn.keil@googlemail.com 		if (lc == NULL)
9139048Sjrgn.keil@googlemail.com 			return (NULL);
9149048Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
9159048Sjrgn.keil@googlemail.com 	}
9169048Sjrgn.keil@googlemail.com 
9179048Sjrgn.keil@googlemail.com 	/*
9189048Sjrgn.keil@googlemail.com 	 * Free old uncompressed segment data when reusing a cache
9199048Sjrgn.keil@googlemail.com 	 * entry.
9209048Sjrgn.keil@googlemail.com 	 */
9219048Sjrgn.keil@googlemail.com 	if (lc->lc_data != NULL)
9229048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
9239048Sjrgn.keil@googlemail.com 
9249048Sjrgn.keil@googlemail.com 	lc->lc_data = data;
9259048Sjrgn.keil@googlemail.com 	lc->lc_index = seg_index;
9269048Sjrgn.keil@googlemail.com 	return (lc);
9279048Sjrgn.keil@googlemail.com }
9289048Sjrgn.keil@googlemail.com 
9299048Sjrgn.keil@googlemail.com 
9305643Saalok /*ARGSUSED*/
9318996SAlok.Aggarwal@Sun.COM static int
9328996SAlok.Aggarwal@Sun.COM gzip_decompress(void *src, size_t srclen, void *dst,
9335643Saalok     size_t *dstlen, int level)
9345643Saalok {
9355643Saalok 	ASSERT(*dstlen >= srclen);
9365643Saalok 
9375643Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
9385643Saalok 		return (-1);
9395643Saalok 	return (0);
9405643Saalok }
9415643Saalok 
9428996SAlok.Aggarwal@Sun.COM #define	LZMA_HEADER_SIZE	(LZMA_PROPS_SIZE + 8)
9438996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
9448996SAlok.Aggarwal@Sun.COM static int
9458996SAlok.Aggarwal@Sun.COM lzma_decompress(void *src, size_t srclen, void *dst,
9468996SAlok.Aggarwal@Sun.COM 	size_t *dstlen, int level)
9478996SAlok.Aggarwal@Sun.COM {
9488996SAlok.Aggarwal@Sun.COM 	size_t insizepure;
9498996SAlok.Aggarwal@Sun.COM 	void *actual_src;
9508996SAlok.Aggarwal@Sun.COM 	ELzmaStatus status;
9518996SAlok.Aggarwal@Sun.COM 
9528996SAlok.Aggarwal@Sun.COM 	insizepure = srclen - LZMA_HEADER_SIZE;
9538996SAlok.Aggarwal@Sun.COM 	actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
9548996SAlok.Aggarwal@Sun.COM 
9558996SAlok.Aggarwal@Sun.COM 	if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
9568996SAlok.Aggarwal@Sun.COM 	    (const Byte *)actual_src, &insizepure,
9578996SAlok.Aggarwal@Sun.COM 	    (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
9588996SAlok.Aggarwal@Sun.COM 	    &g_Alloc) != SZ_OK) {
9598996SAlok.Aggarwal@Sun.COM 		return (-1);
9608996SAlok.Aggarwal@Sun.COM 	}
9618996SAlok.Aggarwal@Sun.COM 	return (0);
9628996SAlok.Aggarwal@Sun.COM }
9638996SAlok.Aggarwal@Sun.COM 
9640Sstevel@tonic-gate /*
9650Sstevel@tonic-gate  * This is basically what strategy used to be before we found we
9660Sstevel@tonic-gate  * needed task queues.
9670Sstevel@tonic-gate  */
9680Sstevel@tonic-gate static void
9690Sstevel@tonic-gate lofi_strategy_task(void *arg)
9700Sstevel@tonic-gate {
9710Sstevel@tonic-gate 	struct buf *bp = (struct buf *)arg;
9720Sstevel@tonic-gate 	int error;
97312368SFrank.Batschulat@Sun.COM 	int syncflag = 0;
9740Sstevel@tonic-gate 	struct lofi_state *lsp;
9758313SDina.Nimeh@Sun.Com 	offset_t offset;
9768313SDina.Nimeh@Sun.Com 	caddr_t	bufaddr;
9778313SDina.Nimeh@Sun.Com 	size_t	len;
9788313SDina.Nimeh@Sun.Com 	size_t	xfersize;
9798313SDina.Nimeh@Sun.Com 	boolean_t bufinited = B_FALSE;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
9828313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
9838313SDina.Nimeh@Sun.Com 		error = ENXIO;
9848313SDina.Nimeh@Sun.Com 		goto errout;
9858313SDina.Nimeh@Sun.Com 	}
9860Sstevel@tonic-gate 	if (lsp->ls_kstat) {
9870Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
9880Sstevel@tonic-gate 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
9890Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 	bp_mapin(bp);
9920Sstevel@tonic-gate 	bufaddr = bp->b_un.b_addr;
9930Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
9948313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
9958313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
9968313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
9978313SDina.Nimeh@Sun.Com 	}
9988313SDina.Nimeh@Sun.Com 	len = bp->b_bcount;
9998313SDina.Nimeh@Sun.Com 	bufinited = B_TRUE;
10008313SDina.Nimeh@Sun.Com 
10018313SDina.Nimeh@Sun.Com 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
10028313SDina.Nimeh@Sun.Com 		error = EIO;
10038313SDina.Nimeh@Sun.Com 		goto errout;
10048313SDina.Nimeh@Sun.Com 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
100712368SFrank.Batschulat@Sun.COM 	 * If we're writing and the buffer was not B_ASYNC
100812368SFrank.Batschulat@Sun.COM 	 * we'll follow up with a VOP_FSYNC() to force any
100912368SFrank.Batschulat@Sun.COM 	 * asynchronous I/O to stable storage.
101012368SFrank.Batschulat@Sun.COM 	 */
101112368SFrank.Batschulat@Sun.COM 	if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
101212368SFrank.Batschulat@Sun.COM 		syncflag = FSYNC;
101312368SFrank.Batschulat@Sun.COM 
101412368SFrank.Batschulat@Sun.COM 	/*
10150Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
10160Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
10170Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
10180Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
10198313SDina.Nimeh@Sun.Com 	 * mapable or it's a character device or it's an encrypted lofi.
10200Sstevel@tonic-gate 	 */
10218313SDina.Nimeh@Sun.Com 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
10228313SDina.Nimeh@Sun.Com 	    lsp->ls_crypto_enabled) {
10238313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
10248313SDina.Nimeh@Sun.Com 		    NULL);
10258313SDina.Nimeh@Sun.Com 	} else if (lsp->ls_uncomp_seg_sz == 0) {
10268313SDina.Nimeh@Sun.Com 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
10278313SDina.Nimeh@Sun.Com 	} else {
10289048Sjrgn.keil@googlemail.com 		uchar_t *compressed_seg = NULL, *cmpbuf;
10299048Sjrgn.keil@googlemail.com 		uchar_t *uncompressed_seg = NULL;
10308313SDina.Nimeh@Sun.Com 		lofi_compress_info_t *li;
10318313SDina.Nimeh@Sun.Com 		size_t oblkcount;
10329048Sjrgn.keil@googlemail.com 		ulong_t seglen;
10338313SDina.Nimeh@Sun.Com 		uint64_t sblkno, eblkno, cmpbytes;
10349048Sjrgn.keil@googlemail.com 		uint64_t uncompressed_seg_index;
10359048Sjrgn.keil@googlemail.com 		struct lofi_comp_cache *lc;
10368313SDina.Nimeh@Sun.Com 		offset_t sblkoff, eblkoff;
10378313SDina.Nimeh@Sun.Com 		u_offset_t salign, ealign;
10388313SDina.Nimeh@Sun.Com 		u_offset_t sdiff;
10398313SDina.Nimeh@Sun.Com 		uint32_t comp_data_sz;
10405643Saalok 		uint64_t i;
1041*12384Salok.aggarwal@oracle.com 		int j;
10425643Saalok 
10430Sstevel@tonic-gate 		/*
10445643Saalok 		 * From here on we're dealing primarily with compressed files
10455643Saalok 		 */
10468313SDina.Nimeh@Sun.Com 		ASSERT(!lsp->ls_crypto_enabled);
10475643Saalok 
10485643Saalok 		/*
10495643Saalok 		 * Compressed files can only be read from and
10505643Saalok 		 * not written to
10510Sstevel@tonic-gate 		 */
10525643Saalok 		if (!(bp->b_flags & B_READ)) {
10535643Saalok 			bp->b_resid = bp->b_bcount;
10545643Saalok 			error = EROFS;
10555643Saalok 			goto done;
10565643Saalok 		}
10575643Saalok 
10585643Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
10595643Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
10605643Saalok 		/*
10615643Saalok 		 * Compute starting and ending compressed segment numbers
10625643Saalok 		 * We use only bitwise operations avoiding division and
10635643Saalok 		 * modulus because we enforce the compression segment size
10645643Saalok 		 * to a power of 2
10655643Saalok 		 */
10665643Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
10675643Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
10685643Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
10695643Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
10705643Saalok 
10715643Saalok 		/*
10729048Sjrgn.keil@googlemail.com 		 * Check the decompressed segment cache.
10739048Sjrgn.keil@googlemail.com 		 *
10749048Sjrgn.keil@googlemail.com 		 * The cache is used only when the requested data
10759048Sjrgn.keil@googlemail.com 		 * is within a segment. Requests that cross
10769048Sjrgn.keil@googlemail.com 		 * segment boundaries bypass the cache.
10779048Sjrgn.keil@googlemail.com 		 */
10789048Sjrgn.keil@googlemail.com 		if (sblkno == eblkno ||
10799048Sjrgn.keil@googlemail.com 		    (sblkno + 1 == eblkno && eblkoff == 0)) {
10809048Sjrgn.keil@googlemail.com 			/*
10819048Sjrgn.keil@googlemail.com 			 * Request doesn't cross a segment boundary,
10829048Sjrgn.keil@googlemail.com 			 * now check the cache.
10839048Sjrgn.keil@googlemail.com 			 */
10849048Sjrgn.keil@googlemail.com 			mutex_enter(&lsp->ls_comp_cache_lock);
10859048Sjrgn.keil@googlemail.com 			lc = lofi_find_comp_data(lsp, sblkno);
10869048Sjrgn.keil@googlemail.com 			if (lc != NULL) {
10879048Sjrgn.keil@googlemail.com 				/*
10889048Sjrgn.keil@googlemail.com 				 * We've found the decompressed segment
10899048Sjrgn.keil@googlemail.com 				 * data in the cache; reuse it.
10909048Sjrgn.keil@googlemail.com 				 */
10919048Sjrgn.keil@googlemail.com 				bcopy(lc->lc_data + sblkoff, bufaddr,
10929048Sjrgn.keil@googlemail.com 				    bp->b_bcount);
10939048Sjrgn.keil@googlemail.com 				mutex_exit(&lsp->ls_comp_cache_lock);
10949048Sjrgn.keil@googlemail.com 				bp->b_resid = 0;
10959048Sjrgn.keil@googlemail.com 				error = 0;
10969048Sjrgn.keil@googlemail.com 				goto done;
10979048Sjrgn.keil@googlemail.com 			}
10989048Sjrgn.keil@googlemail.com 			mutex_exit(&lsp->ls_comp_cache_lock);
10999048Sjrgn.keil@googlemail.com 		}
11009048Sjrgn.keil@googlemail.com 
11019048Sjrgn.keil@googlemail.com 		/*
11025643Saalok 		 * Align start offset to block boundary for segmap
11035643Saalok 		 */
11045643Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
11055643Saalok 		sdiff = salign & (DEV_BSIZE - 1);
11065643Saalok 		salign -= sdiff;
11075643Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
11080Sstevel@tonic-gate 			/*
11095643Saalok 			 * We're dealing with the last segment of
11105643Saalok 			 * the compressed file -- the size of this
11115643Saalok 			 * segment *may not* be the same as the
11125643Saalok 			 * segment size for the file
11130Sstevel@tonic-gate 			 */
11145643Saalok 			eblkoff = (offset + bp->b_bcount) &
11155643Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
11165643Saalok 			ealign = lsp->ls_vp_comp_size;
11175643Saalok 		} else {
11185643Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
11195643Saalok 		}
11205643Saalok 
11215643Saalok 		/*
11225643Saalok 		 * Preserve original request paramaters
11235643Saalok 		 */
11245643Saalok 		oblkcount = bp->b_bcount;
11255643Saalok 
11265643Saalok 		/*
11275643Saalok 		 * Assign the calculated parameters
11285643Saalok 		 */
11295643Saalok 		comp_data_sz = ealign - salign;
11305643Saalok 		bp->b_bcount = comp_data_sz;
11315643Saalok 
11325643Saalok 		/*
1133*12384Salok.aggarwal@oracle.com 		 * Buffers to hold compressed segments are pre-allocated
1134*12384Salok.aggarwal@oracle.com 		 * on a per-thread basis. Find a pre-allocated buffer
1135*12384Salok.aggarwal@oracle.com 		 * that is not currently in use and mark it for use.
11365643Saalok 		 */
1137*12384Salok.aggarwal@oracle.com 		mutex_enter(&lsp->ls_comp_bufs_lock);
1138*12384Salok.aggarwal@oracle.com 		for (j = 0; j < lofi_taskq_nthreads; j++) {
1139*12384Salok.aggarwal@oracle.com 			if (lsp->ls_comp_bufs[j].inuse == 0) {
1140*12384Salok.aggarwal@oracle.com 				lsp->ls_comp_bufs[j].inuse = 1;
1141*12384Salok.aggarwal@oracle.com 				break;
1142*12384Salok.aggarwal@oracle.com 			}
1143*12384Salok.aggarwal@oracle.com 		}
1144*12384Salok.aggarwal@oracle.com 
1145*12384Salok.aggarwal@oracle.com 		mutex_exit(&lsp->ls_comp_bufs_lock);
1146*12384Salok.aggarwal@oracle.com 		ASSERT(j < lofi_taskq_nthreads);
1147*12384Salok.aggarwal@oracle.com 
1148*12384Salok.aggarwal@oracle.com 		/*
1149*12384Salok.aggarwal@oracle.com 		 * If the pre-allocated buffer size does not match
1150*12384Salok.aggarwal@oracle.com 		 * the size of the I/O request, re-allocate it with
1151*12384Salok.aggarwal@oracle.com 		 * the appropriate size
1152*12384Salok.aggarwal@oracle.com 		 */
1153*12384Salok.aggarwal@oracle.com 		if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) {
1154*12384Salok.aggarwal@oracle.com 			if (lsp->ls_comp_bufs[j].bufsize > 0)
1155*12384Salok.aggarwal@oracle.com 				kmem_free(lsp->ls_comp_bufs[j].buf,
1156*12384Salok.aggarwal@oracle.com 				    lsp->ls_comp_bufs[j].bufsize);
1157*12384Salok.aggarwal@oracle.com 			lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount,
1158*12384Salok.aggarwal@oracle.com 			    KM_SLEEP);
1159*12384Salok.aggarwal@oracle.com 			lsp->ls_comp_bufs[j].bufsize = bp->b_bcount;
1160*12384Salok.aggarwal@oracle.com 		}
1161*12384Salok.aggarwal@oracle.com 		compressed_seg = lsp->ls_comp_bufs[j].buf;
1162*12384Salok.aggarwal@oracle.com 
11635643Saalok 		/*
11645643Saalok 		 * Map in the calculated number of blocks
11655643Saalok 		 */
11665643Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
11675643Saalok 		    bp, lsp);
11685643Saalok 
11695643Saalok 		bp->b_bcount = oblkcount;
11705643Saalok 		bp->b_resid = oblkcount;
11715643Saalok 		if (error != 0)
11725643Saalok 			goto done;
11735643Saalok 
11745643Saalok 		/*
1175*12384Salok.aggarwal@oracle.com 		 * decompress compressed blocks start
11765643Saalok 		 */
11775643Saalok 		cmpbuf = compressed_seg + sdiff;
11788996SAlok.Aggarwal@Sun.COM 		for (i = sblkno; i <= eblkno; i++) {
11798996SAlok.Aggarwal@Sun.COM 			ASSERT(i < lsp->ls_comp_index_sz - 1);
1180*12384Salok.aggarwal@oracle.com 			uchar_t *useg;
11818996SAlok.Aggarwal@Sun.COM 
11828996SAlok.Aggarwal@Sun.COM 			/*
11838996SAlok.Aggarwal@Sun.COM 			 * The last segment is special in that it is
11848996SAlok.Aggarwal@Sun.COM 			 * most likely not going to be the same
11858996SAlok.Aggarwal@Sun.COM 			 * (uncompressed) size as the other segments.
11868996SAlok.Aggarwal@Sun.COM 			 */
11878996SAlok.Aggarwal@Sun.COM 			if (i == (lsp->ls_comp_index_sz - 2)) {
11888996SAlok.Aggarwal@Sun.COM 				seglen = lsp->ls_uncomp_last_seg_sz;
11898996SAlok.Aggarwal@Sun.COM 			} else {
11908996SAlok.Aggarwal@Sun.COM 				seglen = lsp->ls_uncomp_seg_sz;
11918996SAlok.Aggarwal@Sun.COM 			}
11928996SAlok.Aggarwal@Sun.COM 
11935643Saalok 			/*
11945643Saalok 			 * Each of the segment index entries contains
11955643Saalok 			 * the starting block number for that segment.
11965643Saalok 			 * The number of compressed bytes in a segment
11975643Saalok 			 * is thus the difference between the starting
11985643Saalok 			 * block number of this segment and the starting
11995643Saalok 			 * block number of the next segment.
12005643Saalok 			 */
12018996SAlok.Aggarwal@Sun.COM 			cmpbytes = lsp->ls_comp_seg_index[i + 1] -
12028996SAlok.Aggarwal@Sun.COM 			    lsp->ls_comp_seg_index[i];
12035643Saalok 
12045643Saalok 			/*
12055643Saalok 			 * The first byte in a compressed segment is a flag
12065643Saalok 			 * that indicates whether this segment is compressed
1207*12384Salok.aggarwal@oracle.com 			 * at all.
1208*12384Salok.aggarwal@oracle.com 			 *
1209*12384Salok.aggarwal@oracle.com 			 * The variable 'useg' is used (instead of
1210*12384Salok.aggarwal@oracle.com 			 * uncompressed_seg) in this loop to keep a
1211*12384Salok.aggarwal@oracle.com 			 * reference to the uncompressed segment.
1212*12384Salok.aggarwal@oracle.com 			 *
1213*12384Salok.aggarwal@oracle.com 			 * N.B. If 'useg' is replaced with uncompressed_seg,
1214*12384Salok.aggarwal@oracle.com 			 * it leads to memory leaks and heap corruption in
1215*12384Salok.aggarwal@oracle.com 			 * corner cases where compressed segments lie
1216*12384Salok.aggarwal@oracle.com 			 * adjacent to uncompressed segments.
12175643Saalok 			 */
12185643Saalok 			if (*cmpbuf == UNCOMPRESSED) {
1219*12384Salok.aggarwal@oracle.com 				useg = cmpbuf + SEGHDR;
12205643Saalok 			} else {
1221*12384Salok.aggarwal@oracle.com 				if (uncompressed_seg == NULL)
1222*12384Salok.aggarwal@oracle.com 					uncompressed_seg =
1223*12384Salok.aggarwal@oracle.com 					    kmem_alloc(lsp->ls_uncomp_seg_sz,
1224*12384Salok.aggarwal@oracle.com 					    KM_SLEEP);
1225*12384Salok.aggarwal@oracle.com 				useg = uncompressed_seg;
1226*12384Salok.aggarwal@oracle.com 				uncompressed_seg_index = i;
1227*12384Salok.aggarwal@oracle.com 
12285643Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
12295643Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
12305643Saalok 				    &seglen, li->l_level) != 0) {
12315643Saalok 					error = EIO;
12325643Saalok 					goto done;
12335643Saalok 				}
12345643Saalok 			}
12355643Saalok 
12365643Saalok 			/*
12375643Saalok 			 * Determine how much uncompressed data we
12385643Saalok 			 * have to copy and copy it
12395643Saalok 			 */
12405643Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
12418996SAlok.Aggarwal@Sun.COM 			if (i == eblkno)
12428996SAlok.Aggarwal@Sun.COM 				xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
12435643Saalok 
1244*12384Salok.aggarwal@oracle.com 			bcopy((useg + sblkoff), bufaddr, xfersize);
12455643Saalok 
12465643Saalok 			cmpbuf += cmpbytes;
12470Sstevel@tonic-gate 			bufaddr += xfersize;
12485643Saalok 			bp->b_resid -= xfersize;
12495643Saalok 			sblkoff = 0;
12505643Saalok 
12515643Saalok 			if (bp->b_resid == 0)
12525643Saalok 				break;
1253*12384Salok.aggarwal@oracle.com 		} /* decompress compressed blocks ends */
12549048Sjrgn.keil@googlemail.com 
12559048Sjrgn.keil@googlemail.com 		/*
1256*12384Salok.aggarwal@oracle.com 		 * Skip to done if there is no uncompressed data to cache
1257*12384Salok.aggarwal@oracle.com 		 */
1258*12384Salok.aggarwal@oracle.com 		if (uncompressed_seg == NULL)
1259*12384Salok.aggarwal@oracle.com 			goto done;
1260*12384Salok.aggarwal@oracle.com 
1261*12384Salok.aggarwal@oracle.com 		/*
1262*12384Salok.aggarwal@oracle.com 		 * Add the data for the last decompressed segment to
12639048Sjrgn.keil@googlemail.com 		 * the cache.
12649048Sjrgn.keil@googlemail.com 		 *
12659048Sjrgn.keil@googlemail.com 		 * In case the uncompressed segment data was added to (and
12669048Sjrgn.keil@googlemail.com 		 * is referenced by) the cache, make sure we don't free it
12679048Sjrgn.keil@googlemail.com 		 * here.
12689048Sjrgn.keil@googlemail.com 		 */
12699048Sjrgn.keil@googlemail.com 		mutex_enter(&lsp->ls_comp_cache_lock);
12709048Sjrgn.keil@googlemail.com 		if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
12719048Sjrgn.keil@googlemail.com 		    uncompressed_seg)) != NULL) {
12729048Sjrgn.keil@googlemail.com 			uncompressed_seg = NULL;
12739048Sjrgn.keil@googlemail.com 		}
12749048Sjrgn.keil@googlemail.com 		mutex_exit(&lsp->ls_comp_cache_lock);
12759048Sjrgn.keil@googlemail.com 
12768313SDina.Nimeh@Sun.Com done:
1277*12384Salok.aggarwal@oracle.com 		if (compressed_seg != NULL) {
1278*12384Salok.aggarwal@oracle.com 			mutex_enter(&lsp->ls_comp_bufs_lock);
1279*12384Salok.aggarwal@oracle.com 			lsp->ls_comp_bufs[j].inuse = 0;
1280*12384Salok.aggarwal@oracle.com 			mutex_exit(&lsp->ls_comp_bufs_lock);
1281*12384Salok.aggarwal@oracle.com 		}
12828313SDina.Nimeh@Sun.Com 		if (uncompressed_seg != NULL)
12838313SDina.Nimeh@Sun.Com 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
12848313SDina.Nimeh@Sun.Com 	} /* end of handling compressed files */
12850Sstevel@tonic-gate 
128612368SFrank.Batschulat@Sun.COM 	if ((error == 0) && (syncflag != 0))
128712368SFrank.Batschulat@Sun.COM 		error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
128812368SFrank.Batschulat@Sun.COM 
12898313SDina.Nimeh@Sun.Com errout:
12908313SDina.Nimeh@Sun.Com 	if (bufinited && lsp->ls_kstat) {
12910Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
12920Sstevel@tonic-gate 		kstat_io_t *kioptr;
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
12950Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
12960Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
12970Sstevel@tonic-gate 			kioptr->nread += n_done;
12980Sstevel@tonic-gate 			kioptr->reads++;
12990Sstevel@tonic-gate 		} else {
13000Sstevel@tonic-gate 			kioptr->nwritten += n_done;
13010Sstevel@tonic-gate 			kioptr->writes++;
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
13040Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
13050Sstevel@tonic-gate 	}
13064451Seschrock 
13074451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
13084451Seschrock 	if (--lsp->ls_vp_iocount == 0)
13094451Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
13104451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
13114451Seschrock 
13120Sstevel@tonic-gate 	bioerror(bp, error);
13130Sstevel@tonic-gate 	biodone(bp);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate static int
13170Sstevel@tonic-gate lofi_strategy(struct buf *bp)
13180Sstevel@tonic-gate {
13190Sstevel@tonic-gate 	struct lofi_state *lsp;
13200Sstevel@tonic-gate 	offset_t	offset;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
13240Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
13250Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
13260Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
13270Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
13280Sstevel@tonic-gate 	 *
13290Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
13300Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
13310Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
13320Sstevel@tonic-gate 	 */
13330Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
13348313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
13358313SDina.Nimeh@Sun.Com 		bioerror(bp, ENXIO);
13368313SDina.Nimeh@Sun.Com 		biodone(bp);
13378313SDina.Nimeh@Sun.Com 		return (0);
13388313SDina.Nimeh@Sun.Com 	}
13398313SDina.Nimeh@Sun.Com 
13404451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
13414451Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
13424451Seschrock 		bioerror(bp, EIO);
13434451Seschrock 		biodone(bp);
13444451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
13454451Seschrock 		return (0);
13464451Seschrock 	}
13474451Seschrock 
13480Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
13498313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
13508313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
13518313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
13528313SDina.Nimeh@Sun.Com 	}
13530Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
13540Sstevel@tonic-gate 		/* EOF */
13550Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
13560Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
13570Sstevel@tonic-gate 			bioerror(bp, 0);
13580Sstevel@tonic-gate 		} else {
13590Sstevel@tonic-gate 			/* writes should fail */
13600Sstevel@tonic-gate 			bioerror(bp, ENXIO);
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 		biodone(bp);
13634451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
13640Sstevel@tonic-gate 		return (0);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
13670Sstevel@tonic-gate 		bioerror(bp, ENXIO);
13680Sstevel@tonic-gate 		biodone(bp);
13694451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
13700Sstevel@tonic-gate 		return (0);
13710Sstevel@tonic-gate 	}
13724451Seschrock 	lsp->ls_vp_iocount++;
13734451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
13744451Seschrock 
13750Sstevel@tonic-gate 	if (lsp->ls_kstat) {
13760Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
13770Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
13780Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
13790Sstevel@tonic-gate 	}
13800Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
13810Sstevel@tonic-gate 	return (0);
13820Sstevel@tonic-gate }
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate /*ARGSUSED2*/
13850Sstevel@tonic-gate static int
13860Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
13870Sstevel@tonic-gate {
13880Sstevel@tonic-gate 	if (getminor(dev) == 0)
13890Sstevel@tonic-gate 		return (EINVAL);
13908313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
13910Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate /*ARGSUSED2*/
13950Sstevel@tonic-gate static int
13960Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
13970Sstevel@tonic-gate {
13980Sstevel@tonic-gate 	if (getminor(dev) == 0)
13990Sstevel@tonic-gate 		return (EINVAL);
14008313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
14010Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate /*ARGSUSED2*/
14050Sstevel@tonic-gate static int
14060Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 	if (getminor(dev) == 0)
14090Sstevel@tonic-gate 		return (EINVAL);
14108313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
14110Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate /*ARGSUSED2*/
14150Sstevel@tonic-gate static int
14160Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	if (getminor(dev) == 0)
14190Sstevel@tonic-gate 		return (EINVAL);
14208313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
14210Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
14220Sstevel@tonic-gate }
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate /*ARGSUSED*/
14250Sstevel@tonic-gate static int
14260Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	switch (infocmd) {
14290Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
14300Sstevel@tonic-gate 		*result = lofi_dip;
14310Sstevel@tonic-gate 		return (DDI_SUCCESS);
14320Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
14330Sstevel@tonic-gate 		*result = 0;
14340Sstevel@tonic-gate 		return (DDI_SUCCESS);
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 	return (DDI_FAILURE);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate static int
14400Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
14410Sstevel@tonic-gate {
14420Sstevel@tonic-gate 	int	error;
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
14450Sstevel@tonic-gate 		return (DDI_FAILURE);
14460Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
14470Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
14480Sstevel@tonic-gate 		return (DDI_FAILURE);
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
14510Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
14520Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
14530Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
14540Sstevel@tonic-gate 		return (DDI_FAILURE);
14550Sstevel@tonic-gate 	}
14565084Sjohnlev 	/* driver handles kernel-issued IOCTLs */
14575084Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
14585084Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
14595084Sjohnlev 		ddi_remove_minor_node(dip, NULL);
14605084Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
14615084Sjohnlev 		return (DDI_FAILURE);
14625084Sjohnlev 	}
14630Sstevel@tonic-gate 	lofi_dip = dip;
14640Sstevel@tonic-gate 	ddi_report_dev(dip);
14650Sstevel@tonic-gate 	return (DDI_SUCCESS);
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate static int
14690Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
14700Sstevel@tonic-gate {
14710Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
14720Sstevel@tonic-gate 		return (DDI_FAILURE);
14730Sstevel@tonic-gate 	if (lofi_busy())
14740Sstevel@tonic-gate 		return (DDI_FAILURE);
14750Sstevel@tonic-gate 	lofi_dip = NULL;
14760Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
14775084Sjohnlev 	ddi_prop_remove_all(dip);
14780Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
14790Sstevel@tonic-gate 	return (DDI_SUCCESS);
14800Sstevel@tonic-gate }
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate /*
14838313SDina.Nimeh@Sun.Com  * With addition of encryption, be careful that encryption key is wiped before
14848313SDina.Nimeh@Sun.Com  * kernel memory structures are freed, and also that key is not accidentally
14858313SDina.Nimeh@Sun.Com  * passed out into userland structures.
14868313SDina.Nimeh@Sun.Com  */
14878313SDina.Nimeh@Sun.Com static void
14888313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip)
14898313SDina.Nimeh@Sun.Com {
14908313SDina.Nimeh@Sun.Com 	/* Make sure this encryption key doesn't stick around */
14918313SDina.Nimeh@Sun.Com 	bzero(klip->li_key, sizeof (klip->li_key));
14928313SDina.Nimeh@Sun.Com 	kmem_free(klip, sizeof (struct lofi_ioctl));
14938313SDina.Nimeh@Sun.Com }
14948313SDina.Nimeh@Sun.Com 
14958313SDina.Nimeh@Sun.Com /*
14960Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
14970Sstevel@tonic-gate  * the lofi_ioctl structure.
14980Sstevel@tonic-gate  */
14990Sstevel@tonic-gate struct lofi_ioctl *
15001657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
15010Sstevel@tonic-gate {
15020Sstevel@tonic-gate 	struct lofi_ioctl *klip;
15030Sstevel@tonic-gate 	int	error;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
15061657Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
15070Sstevel@tonic-gate 	if (error) {
15088313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
15090Sstevel@tonic-gate 		return (NULL);
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
15138313SDina.Nimeh@Sun.Com 	klip->li_filename[MAXPATHLEN-1] = '\0';
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/* validate minor number */
15160Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
15178313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
15188313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
15198313SDina.Nimeh@Sun.Com 		    lofi_max_files);
15200Sstevel@tonic-gate 		return (NULL);
15210Sstevel@tonic-gate 	}
15220Sstevel@tonic-gate 	return (klip);
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate int
15261657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
15271657Sheppo 	int flag)
15280Sstevel@tonic-gate {
15290Sstevel@tonic-gate 	int	error;
15300Sstevel@tonic-gate 
15318313SDina.Nimeh@Sun.Com 	/*
15328313SDina.Nimeh@Sun.Com 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
15338313SDina.Nimeh@Sun.Com 	 * This ensures that an attacker can't trivially find the
15348313SDina.Nimeh@Sun.Com 	 * key for a mapping just by issuing the ioctl.
15358313SDina.Nimeh@Sun.Com 	 *
15368313SDina.Nimeh@Sun.Com 	 * It can still be found by poking around in kmem with mdb(1),
15378313SDina.Nimeh@Sun.Com 	 * but there is no point in making it easy when the info isn't
15388313SDina.Nimeh@Sun.Com 	 * of any use in this direction anyway.
15398313SDina.Nimeh@Sun.Com 	 *
15408313SDina.Nimeh@Sun.Com 	 * Either way we don't actually have the raw key stored in
15418313SDina.Nimeh@Sun.Com 	 * a form that we can get it anyway, since we just used it
15428313SDina.Nimeh@Sun.Com 	 * to create a ctx template and didn't keep "the original".
15438313SDina.Nimeh@Sun.Com 	 */
15441657Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
15450Sstevel@tonic-gate 	if (error)
15460Sstevel@tonic-gate 		return (EFAULT);
15470Sstevel@tonic-gate 	return (0);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate /*
15510Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
15520Sstevel@tonic-gate  */
15530Sstevel@tonic-gate static int
15540Sstevel@tonic-gate file_to_minor(char *filename)
15550Sstevel@tonic-gate {
15560Sstevel@tonic-gate 	minor_t	minor;
15570Sstevel@tonic-gate 	struct lofi_state *lsp;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
15600Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
15610Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
15620Sstevel@tonic-gate 		if (lsp == NULL)
15630Sstevel@tonic-gate 			continue;
15640Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
15650Sstevel@tonic-gate 			return (minor);
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	return (0);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
15720Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
15730Sstevel@tonic-gate  */
15740Sstevel@tonic-gate static int
15750Sstevel@tonic-gate valid_filename(const char *filename)
15760Sstevel@tonic-gate {
15770Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
15780Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/* must be absolute path */
15810Sstevel@tonic-gate 	if (filename[0] != '/')
15820Sstevel@tonic-gate 		return (0);
15830Sstevel@tonic-gate 	/* must not be lofi */
15840Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
15850Sstevel@tonic-gate 		return (0);
15860Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
15870Sstevel@tonic-gate 		return (0);
15880Sstevel@tonic-gate 	return (1);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate /*
15920Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
15930Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
15940Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
15950Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
15963517Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
15970Sstevel@tonic-gate  * have to support it.
15980Sstevel@tonic-gate  */
15990Sstevel@tonic-gate static void
16000Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
16010Sstevel@tonic-gate {
16028313SDina.Nimeh@Sun.Com 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
16038313SDina.Nimeh@Sun.Com 
16040Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
16050Sstevel@tonic-gate 	/*
16060Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
16070Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
16080Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
16090Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
16100Sstevel@tonic-gate 	 * a number between one and one).
16110Sstevel@tonic-gate 	 *
16120Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
16130Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
16140Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
16150Sstevel@tonic-gate 	 */
16168313SDina.Nimeh@Sun.Com 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
16178313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
16180Sstevel@tonic-gate 	else
16198313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
16200Sstevel@tonic-gate 	/* in case file file is < 100k */
16210Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
16220Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
16230Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
16240Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
16250Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
16260Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
16270Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
16280Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
16290Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
16300Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
16310Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
16320Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
16338313SDina.Nimeh@Sun.Com 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
16340Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
16350Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
16380Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
16390Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
16400Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
16418669SDina.Nimeh@Sun.COM 	(void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME,
16428669SDina.Nimeh@Sun.COM 	    sizeof (lsp->ls_vtoc.v_volume));
16430Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
16440Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
16450Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
16465643Saalok 
16475643Saalok 	/*
16485643Saalok 	 * A compressed file is read-only, other files can
16495643Saalok 	 * be read-write
16505643Saalok 	 */
16515643Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
16525643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
16535643Saalok 	} else {
16545643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
16555643Saalok 	}
16560Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
16570Sstevel@tonic-gate 	/*
16580Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
16590Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
16600Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
16610Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
16620Sstevel@tonic-gate 	 */
16630Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
16640Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
16670Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
16680Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
16690Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
16700Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
16710Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
16720Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
16730Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
16740Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
16750Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
16760Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
16770Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
16780Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
16790Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
16800Sstevel@tonic-gate 	/*
16810Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
16820Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
16830Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
16840Sstevel@tonic-gate 	 * maxcontig is 0.
16850Sstevel@tonic-gate 	 */
16860Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate /*
16905643Saalok  * map in a compressed file
16915643Saalok  *
16925643Saalok  * Read in the header and the index that follows.
16935643Saalok  *
16945643Saalok  * The header is as follows -
16955643Saalok  *
16965643Saalok  * Signature (name of the compression algorithm)
16975643Saalok  * Compression segment size (a multiple of 512)
16985643Saalok  * Number of index entries
16995643Saalok  * Size of the last block
17005643Saalok  * The array containing the index entries
17015643Saalok  *
17025643Saalok  * The header information is always stored in
17035643Saalok  * network byte order on disk.
17045643Saalok  */
17055643Saalok static int
17065643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
17075643Saalok {
17085643Saalok 	uint32_t index_sz, header_len, i;
17095643Saalok 	ssize_t	resid;
17105643Saalok 	enum uio_rw rw;
17115643Saalok 	char *tbuf = buf;
17125643Saalok 	int error;
17135643Saalok 
17145643Saalok 	/* The signature has already been read */
17155643Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
17165643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
17175643Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
17185643Saalok 
17195643Saalok 	/*
17205643Saalok 	 * The compressed segment size must be a power of 2
17215643Saalok 	 */
17229048Sjrgn.keil@googlemail.com 	if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
17239048Sjrgn.keil@googlemail.com 	    !ISP2(lsp->ls_uncomp_seg_sz))
17245643Saalok 		return (EINVAL);
17255643Saalok 
17265643Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
17275643Saalok 		;
17285643Saalok 
17295643Saalok 	lsp->ls_comp_seg_shift = i;
17305643Saalok 
17315643Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
17325643Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
17335643Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
17345643Saalok 
17355643Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
17365643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
17375643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
17385643Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
17395643Saalok 
17405643Saalok 	/*
17415643Saalok 	 * Compute the total size of the uncompressed data
17425643Saalok 	 * for use in fake_disk_geometry and other calculations.
17435643Saalok 	 * Disk geometry has to be faked with respect to the
17445643Saalok 	 * actual uncompressed data size rather than the
17455643Saalok 	 * compressed file size.
17465643Saalok 	 */
174710197Sdminer@opensolaris.org 	lsp->ls_vp_size =
174810197Sdminer@opensolaris.org 	    (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
17495643Saalok 	    + lsp->ls_uncomp_last_seg_sz;
17505643Saalok 
17515643Saalok 	/*
17528996SAlok.Aggarwal@Sun.COM 	 * Index size is rounded up to DEV_BSIZE for ease
17535643Saalok 	 * of segmapping
17545643Saalok 	 */
17555643Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
17565643Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
17575643Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
17585643Saalok 	    sizeof (lsp->ls_comp_index_sz) +
17595643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
17605643Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
17615643Saalok 
17625643Saalok 	index_sz += header_len;
17635643Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
17645643Saalok 
17655643Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
17665643Saalok 	lsp->ls_comp_index_data_sz = index_sz;
17675643Saalok 
17685643Saalok 	/*
17695643Saalok 	 * Read in the index -- this has a side-effect
17705643Saalok 	 * of reading in the header as well
17715643Saalok 	 */
17725643Saalok 	rw = UIO_READ;
17735643Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
17745643Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
17755643Saalok 
17765643Saalok 	if (error != 0)
17775643Saalok 		return (error);
17785643Saalok 
17795643Saalok 	/* Skip the header, this is where the index really begins */
17805643Saalok 	lsp->ls_comp_seg_index =
17815643Saalok 	    /*LINTED*/
17825643Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
17835643Saalok 
17845643Saalok 	/*
17855643Saalok 	 * Now recompute offsets in the index to account for
17865643Saalok 	 * the header length
17875643Saalok 	 */
17885643Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
17895643Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
17905643Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
17915643Saalok 	}
17925643Saalok 
1793*12384Salok.aggarwal@oracle.com 	/*
1794*12384Salok.aggarwal@oracle.com 	 * Finally setup per-thread pre-allocated buffers
1795*12384Salok.aggarwal@oracle.com 	 */
1796*12384Salok.aggarwal@oracle.com 	lsp->ls_comp_bufs = kmem_zalloc(lofi_taskq_nthreads *
1797*12384Salok.aggarwal@oracle.com 	    sizeof (struct compbuf), KM_SLEEP);
1798*12384Salok.aggarwal@oracle.com 	mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL);
1799*12384Salok.aggarwal@oracle.com 
18005643Saalok 	return (error);
18015643Saalok }
18025643Saalok 
18035643Saalok /*
18045643Saalok  * Check to see if the passed in signature is a valid
18058313SDina.Nimeh@Sun.Com  * one.  If it is valid, return the index into
18065643Saalok  * lofi_compress_table.
18075643Saalok  *
18085643Saalok  * Return -1 if it is invalid
18095643Saalok  */
18105643Saalok static int lofi_compress_select(char *signature)
18115643Saalok {
18125643Saalok 	int i;
18135643Saalok 
18145643Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
18155643Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
18165643Saalok 			return (i);
18175643Saalok 	}
18185643Saalok 
18195643Saalok 	return (-1);
18205643Saalok }
18215643Saalok 
18225643Saalok /*
18230Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
18240Sstevel@tonic-gate  */
18250Sstevel@tonic-gate static int
18260Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
18271657Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
18280Sstevel@tonic-gate {
18290Sstevel@tonic-gate 	minor_t	newminor;
18300Sstevel@tonic-gate 	struct lofi_state *lsp;
18310Sstevel@tonic-gate 	struct lofi_ioctl *klip;
18320Sstevel@tonic-gate 	int	error;
18330Sstevel@tonic-gate 	struct vnode *vp;
18340Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
18350Sstevel@tonic-gate 	int64_t	Size_prop_val;
18365643Saalok 	int	compress_index;
18370Sstevel@tonic-gate 	vattr_t	vattr;
18380Sstevel@tonic-gate 	int	flag;
18390Sstevel@tonic-gate 	enum vtype v_type;
18404451Seschrock 	int zalloced = 0;
18410Sstevel@tonic-gate 	dev_t	newdev;
18424451Seschrock 	char	namebuf[50];
18438313SDina.Nimeh@Sun.Com 	char	buf[DEV_BSIZE];
18448313SDina.Nimeh@Sun.Com 	char	crybuf[DEV_BSIZE];
18455643Saalok 	ssize_t	resid;
18468313SDina.Nimeh@Sun.Com 	boolean_t need_vn_close = B_FALSE;
18478313SDina.Nimeh@Sun.Com 	boolean_t keycopied = B_FALSE;
18488313SDina.Nimeh@Sun.Com 	boolean_t need_size_update = B_FALSE;
18490Sstevel@tonic-gate 
18501657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
18510Sstevel@tonic-gate 	if (klip == NULL)
18520Sstevel@tonic-gate 		return (EFAULT);
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
18570Sstevel@tonic-gate 		error = EINVAL;
18580Sstevel@tonic-gate 		goto out;
18590Sstevel@tonic-gate 	}
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
18620Sstevel@tonic-gate 		error = EBUSY;
18630Sstevel@tonic-gate 		goto out;
18640Sstevel@tonic-gate 	}
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	if (pickminor) {
18670Sstevel@tonic-gate 		/* Find a free one */
18680Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
18690Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
18700Sstevel@tonic-gate 				break;
18710Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
18720Sstevel@tonic-gate 			error = EAGAIN;
18730Sstevel@tonic-gate 			goto out;
18740Sstevel@tonic-gate 		}
18750Sstevel@tonic-gate 	} else {
18760Sstevel@tonic-gate 		newminor = klip->li_minor;
18770Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
18780Sstevel@tonic-gate 			error = EEXIST;
18790Sstevel@tonic-gate 			goto out;
18800Sstevel@tonic-gate 		}
18810Sstevel@tonic-gate 	}
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	/* make sure it's valid */
18840Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
18850Sstevel@tonic-gate 	    NULLVPP, &vp);
18860Sstevel@tonic-gate 	if (error) {
18870Sstevel@tonic-gate 		goto out;
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 	v_type = vp->v_type;
18900Sstevel@tonic-gate 	VN_RELE(vp);
18910Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
18920Sstevel@tonic-gate 		error = EINVAL;
18930Sstevel@tonic-gate 		goto out;
18940Sstevel@tonic-gate 	}
18950Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
18960Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
18970Sstevel@tonic-gate 	if (error) {
18980Sstevel@tonic-gate 		/* try read-only */
18990Sstevel@tonic-gate 		flag &= ~FWRITE;
19000Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
19010Sstevel@tonic-gate 		    &vp, 0, 0);
19020Sstevel@tonic-gate 		if (error) {
19030Sstevel@tonic-gate 			goto out;
19040Sstevel@tonic-gate 		}
19050Sstevel@tonic-gate 	}
19068313SDina.Nimeh@Sun.Com 	need_vn_close = B_TRUE;
19078313SDina.Nimeh@Sun.Com 
19080Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
19095331Samw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
19100Sstevel@tonic-gate 	if (error) {
19118313SDina.Nimeh@Sun.Com 		goto out;
19120Sstevel@tonic-gate 	}
19130Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
19140Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
19150Sstevel@tonic-gate 		error = EINVAL;
19168313SDina.Nimeh@Sun.Com 		goto out;
19170Sstevel@tonic-gate 	}
19180Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
19190Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
19200Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
19210Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
19220Sstevel@tonic-gate 		error = EINVAL;
19238313SDina.Nimeh@Sun.Com 		goto out;
19240Sstevel@tonic-gate 	}
19250Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
19260Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
19270Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
19280Sstevel@tonic-gate 		error = EINVAL;
19290Sstevel@tonic-gate 		goto propout;
19300Sstevel@tonic-gate 	}
19310Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
19320Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
19330Sstevel@tonic-gate 		error = ENOMEM;
19340Sstevel@tonic-gate 		goto propout;
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate 	zalloced = 1;
19370Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
19386883Sgd78059 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
19390Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
19400Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
19410Sstevel@tonic-gate 		error = ENXIO;
19420Sstevel@tonic-gate 		goto propout;
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
19450Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
19460Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
19470Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
19480Sstevel@tonic-gate 		/* remove block node */
19490Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
19500Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
19510Sstevel@tonic-gate 		error = ENXIO;
19520Sstevel@tonic-gate 		goto propout;
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
19550Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
19560Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
19570Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
19580Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
19590Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
19600Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
19610Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
19620Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
19630Sstevel@tonic-gate 	if (lsp->ls_kstat) {
19640Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
19650Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
19660Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
19670Sstevel@tonic-gate 	}
19684451Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
19694451Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
19704451Seschrock 
19719048Sjrgn.keil@googlemail.com 	list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
19729048Sjrgn.keil@googlemail.com 	    offsetof(struct lofi_comp_cache, lc_list));
19739048Sjrgn.keil@googlemail.com 	mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
19749048Sjrgn.keil@googlemail.com 
19750Sstevel@tonic-gate 	/*
19760Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
19770Sstevel@tonic-gate 	 * updated correctly.
19780Sstevel@tonic-gate 	 */
19790Sstevel@tonic-gate 	lsp->ls_openflag = flag;
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	/*
19820Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
19830Sstevel@tonic-gate 	 */
19840Sstevel@tonic-gate 	if (vp->v_type == VREG) {
19855331Samw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
19860Sstevel@tonic-gate 			lsp->ls_vp = vp;
19870Sstevel@tonic-gate 		} else {
19880Sstevel@tonic-gate 			/*
19890Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
19900Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
19910Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
19920Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
19930Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
19940Sstevel@tonic-gate 			 * the realvp so things behave as if we've
19950Sstevel@tonic-gate 			 * opened the realvp without any interaction
19960Sstevel@tonic-gate 			 * with lofs.
19970Sstevel@tonic-gate 			 */
19980Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
19990Sstevel@tonic-gate 			VN_RELE(vp);
20000Sstevel@tonic-gate 		}
20010Sstevel@tonic-gate 	} else {
20020Sstevel@tonic-gate 		lsp->ls_vp = vp;
20030Sstevel@tonic-gate 	}
20040Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
20050Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
20060Sstevel@tonic-gate 	if (rvalp)
20070Sstevel@tonic-gate 		*rvalp = (int)newminor;
20080Sstevel@tonic-gate 	klip->li_minor = newminor;
20090Sstevel@tonic-gate 
20105643Saalok 	/*
20118313SDina.Nimeh@Sun.Com 	 * Initialize crypto details for encrypted lofi
20125643Saalok 	 */
20138313SDina.Nimeh@Sun.Com 	if (klip->li_crypto_enabled) {
20148313SDina.Nimeh@Sun.Com 		int ret;
20158313SDina.Nimeh@Sun.Com 
20168313SDina.Nimeh@Sun.Com 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
20178313SDina.Nimeh@Sun.Com 
20188313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
20198313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
20208313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
20218313SDina.Nimeh@Sun.Com 			    klip->li_cipher, lsp->ls_filename);
20228313SDina.Nimeh@Sun.Com 			error = EINVAL;
20238313SDina.Nimeh@Sun.Com 			goto propout;
20248313SDina.Nimeh@Sun.Com 		}
20258313SDina.Nimeh@Sun.Com 
20268313SDina.Nimeh@Sun.Com 		/* this is just initialization here */
20278313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param = NULL;
20288313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param_len = 0;
20298313SDina.Nimeh@Sun.Com 
20308313SDina.Nimeh@Sun.Com 		lsp->ls_iv_type = klip->li_iv_type;
20318313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
20328313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
20338313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
20348313SDina.Nimeh@Sun.Com 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
20358313SDina.Nimeh@Sun.Com 			error = EINVAL;
20368313SDina.Nimeh@Sun.Com 			goto propout;
20378313SDina.Nimeh@Sun.Com 		}
20388313SDina.Nimeh@Sun.Com 
20398313SDina.Nimeh@Sun.Com 		/* iv mech must itself take a null iv */
20408313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param = NULL;
20418313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param_len = 0;
20428313SDina.Nimeh@Sun.Com 		lsp->ls_iv_len = klip->li_iv_len;
20438313SDina.Nimeh@Sun.Com 
20448313SDina.Nimeh@Sun.Com 		/*
20458313SDina.Nimeh@Sun.Com 		 * Create ctx using li_cipher & the raw li_key after checking
20468313SDina.Nimeh@Sun.Com 		 * that it isn't a weak key.
20478313SDina.Nimeh@Sun.Com 		 */
20488313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
20498313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = klip->li_key_len;
20508313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = kmem_alloc(
20518313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
20528313SDina.Nimeh@Sun.Com 		bcopy(klip->li_key, lsp->ls_key.ck_data,
20538313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
20548313SDina.Nimeh@Sun.Com 		keycopied = B_TRUE;
20558313SDina.Nimeh@Sun.Com 
20568313SDina.Nimeh@Sun.Com 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
20578313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS) {
20588313SDina.Nimeh@Sun.Com 			error = EINVAL;
20598313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "weak key check failed for cipher "
20608313SDina.Nimeh@Sun.Com 			    "%s on file %s (0x%x)", klip->li_cipher,
20618313SDina.Nimeh@Sun.Com 			    lsp->ls_filename, ret);
20628313SDina.Nimeh@Sun.Com 			goto propout;
20638313SDina.Nimeh@Sun.Com 		}
20648313SDina.Nimeh@Sun.Com 	}
20658313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
20668313SDina.Nimeh@Sun.Com 
20678313SDina.Nimeh@Sun.Com 	/*
20688313SDina.Nimeh@Sun.Com 	 * Read the file signature to check if it is compressed or encrypted.
20698313SDina.Nimeh@Sun.Com 	 * Crypto signature is in a different location; both areas should
20708313SDina.Nimeh@Sun.Com 	 * read to keep compression and encryption mutually exclusive.
20718313SDina.Nimeh@Sun.Com 	 */
20728313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
20738313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
20748313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
20758313SDina.Nimeh@Sun.Com 		if (error != 0)
20768313SDina.Nimeh@Sun.Com 			goto propout;
20778313SDina.Nimeh@Sun.Com 	}
20788313SDina.Nimeh@Sun.Com 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
20795643Saalok 	    0, RLIM64_INFINITY, kcred, &resid);
20805643Saalok 	if (error != 0)
20815643Saalok 		goto propout;
20825643Saalok 
20838313SDina.Nimeh@Sun.Com 	/* initialize these variables for all lofi files */
2084*12384Salok.aggarwal@oracle.com 	lsp->ls_comp_bufs = NULL;
20855643Saalok 	lsp->ls_uncomp_seg_sz = 0;
20865643Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
20875643Saalok 	lsp->ls_comp_algorithm[0] = '\0';
20885643Saalok 
20898313SDina.Nimeh@Sun.Com 	/* encrypted lofi reads/writes shifted by crypto metadata size */
20908313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_offset = 0;
20918313SDina.Nimeh@Sun.Com 
20928313SDina.Nimeh@Sun.Com 	/* this is a compressed lofi */
20938313SDina.Nimeh@Sun.Com 	if ((compress_index = lofi_compress_select(buf)) != -1) {
20948313SDina.Nimeh@Sun.Com 
20958313SDina.Nimeh@Sun.Com 		/* compression and encryption are mutually exclusive */
20968313SDina.Nimeh@Sun.Com 		if (klip->li_crypto_enabled) {
20978313SDina.Nimeh@Sun.Com 			error = ENOTSUP;
20988313SDina.Nimeh@Sun.Com 			goto propout;
20998313SDina.Nimeh@Sun.Com 		}
21008313SDina.Nimeh@Sun.Com 
21018313SDina.Nimeh@Sun.Com 		/* initialize compression info for compressed lofi */
21025643Saalok 		lsp->ls_comp_algorithm_index = compress_index;
21035643Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
21045643Saalok 		    lofi_compress_table[compress_index].l_name,
21055643Saalok 		    sizeof (lsp->ls_comp_algorithm));
21068313SDina.Nimeh@Sun.Com 
21075643Saalok 		error = lofi_map_compressed_file(lsp, buf);
21085643Saalok 		if (error != 0)
21095643Saalok 			goto propout;
21108313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
21115643Saalok 
21128313SDina.Nimeh@Sun.Com 	/* this is an encrypted lofi */
21138313SDina.Nimeh@Sun.Com 	} else if (strncmp(crybuf, lofi_crypto_magic,
21148313SDina.Nimeh@Sun.Com 	    sizeof (lofi_crypto_magic)) == 0) {
21158313SDina.Nimeh@Sun.Com 
21168313SDina.Nimeh@Sun.Com 		char *marker = crybuf;
21178313SDina.Nimeh@Sun.Com 
21188313SDina.Nimeh@Sun.Com 		/*
21198313SDina.Nimeh@Sun.Com 		 * This is the case where the header in the lofi image is
21208313SDina.Nimeh@Sun.Com 		 * already initialized to indicate it is encrypted.
21218313SDina.Nimeh@Sun.Com 		 * There is another case (see below) where encryption is
21228313SDina.Nimeh@Sun.Com 		 * requested but the lofi image has never been used yet,
21238313SDina.Nimeh@Sun.Com 		 * so the header needs to be written with encryption magic.
21248313SDina.Nimeh@Sun.Com 		 */
21258313SDina.Nimeh@Sun.Com 
21268313SDina.Nimeh@Sun.Com 		/* indicate this must be an encrypted lofi due to magic */
21278313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = B_TRUE;
21288313SDina.Nimeh@Sun.Com 
21298313SDina.Nimeh@Sun.Com 		/*
21308313SDina.Nimeh@Sun.Com 		 * The encryption header information is laid out this way:
21318313SDina.Nimeh@Sun.Com 		 *	6 bytes:	hex "CFLOFI"
21328313SDina.Nimeh@Sun.Com 		 *	2 bytes:	version = 0 ... for now
21338313SDina.Nimeh@Sun.Com 		 *	96 bytes:	reserved1 (not implemented yet)
21348313SDina.Nimeh@Sun.Com 		 *	4 bytes:	data_sector = 2 ... for now
21358313SDina.Nimeh@Sun.Com 		 *	more...		not implemented yet
21368313SDina.Nimeh@Sun.Com 		 */
21378313SDina.Nimeh@Sun.Com 
21388313SDina.Nimeh@Sun.Com 		/* copy the magic */
21398313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.magic,
21408313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.magic));
21418313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.magic);
21428313SDina.Nimeh@Sun.Com 
21438313SDina.Nimeh@Sun.Com 		/* read the encryption version number */
21448313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.version),
21458313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.version));
21468313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
21478313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.version);
21488313SDina.Nimeh@Sun.Com 
21498313SDina.Nimeh@Sun.Com 		/* read a chunk of reserved data */
21508313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.reserved1,
21518313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.reserved1));
21528313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.reserved1);
21538313SDina.Nimeh@Sun.Com 
21548313SDina.Nimeh@Sun.Com 		/* read block number where encrypted data begins */
21558313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.data_sector),
21568313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.data_sector));
21578313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
21588313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.data_sector);
21598313SDina.Nimeh@Sun.Com 
21608313SDina.Nimeh@Sun.Com 		/* and ignore the rest until it is implemented */
21618313SDina.Nimeh@Sun.Com 
21628313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
21638313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
21648313SDina.Nimeh@Sun.Com 
21658313SDina.Nimeh@Sun.Com 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
21668313SDina.Nimeh@Sun.Com 	} else if (klip->li_crypto_enabled) {
21678313SDina.Nimeh@Sun.Com 
21688313SDina.Nimeh@Sun.Com 		/*
21698313SDina.Nimeh@Sun.Com 		 * This is the case where encryption was requested but the
21708313SDina.Nimeh@Sun.Com 		 * appears to be entirely blank where the encryption header
21718313SDina.Nimeh@Sun.Com 		 * would have been in the lofi image.  If it is blank,
21728313SDina.Nimeh@Sun.Com 		 * assume it is a brand new lofi image and initialize the
21738313SDina.Nimeh@Sun.Com 		 * header area with encryption magic and current version
21748313SDina.Nimeh@Sun.Com 		 * header data.  If it is not blank, that's an error.
21758313SDina.Nimeh@Sun.Com 		 */
21768313SDina.Nimeh@Sun.Com 		int	i;
21778313SDina.Nimeh@Sun.Com 		char	*marker;
21788313SDina.Nimeh@Sun.Com 		struct crypto_meta	chead;
21798313SDina.Nimeh@Sun.Com 
21808313SDina.Nimeh@Sun.Com 		for (i = 0; i < sizeof (struct crypto_meta); i++)
21818313SDina.Nimeh@Sun.Com 			if (crybuf[i] != '\0')
21828313SDina.Nimeh@Sun.Com 				break;
21838313SDina.Nimeh@Sun.Com 		if (i != sizeof (struct crypto_meta)) {
21848313SDina.Nimeh@Sun.Com 			error = EINVAL;
21858313SDina.Nimeh@Sun.Com 			goto propout;
21868313SDina.Nimeh@Sun.Com 		}
21878313SDina.Nimeh@Sun.Com 
21888313SDina.Nimeh@Sun.Com 		/* nothing there, initialize as encrypted lofi */
21898313SDina.Nimeh@Sun.Com 		marker = crybuf;
21908313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
21918313SDina.Nimeh@Sun.Com 		marker += sizeof (lofi_crypto_magic);
21928313SDina.Nimeh@Sun.Com 		chead.version = htons(LOFI_CRYPTO_VERSION);
21938313SDina.Nimeh@Sun.Com 		bcopy(&(chead.version), marker, sizeof (chead.version));
21948313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.version);
21958313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.reserved1);
21968313SDina.Nimeh@Sun.Com 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
21978313SDina.Nimeh@Sun.Com 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
21988313SDina.Nimeh@Sun.Com 
21998313SDina.Nimeh@Sun.Com 		/* write the header */
22008313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
22018313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
22028313SDina.Nimeh@Sun.Com 		if (error != 0)
22038313SDina.Nimeh@Sun.Com 			goto propout;
22048313SDina.Nimeh@Sun.Com 
22058313SDina.Nimeh@Sun.Com 		/* fix things up so it looks like we read this info */
22068313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
22078313SDina.Nimeh@Sun.Com 		    sizeof (lofi_crypto_magic));
22088313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
22098313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
22108313SDina.Nimeh@Sun.Com 
22118313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
22128313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
22138313SDina.Nimeh@Sun.Com 	}
22148313SDina.Nimeh@Sun.Com 
22158313SDina.Nimeh@Sun.Com 	/*
22168313SDina.Nimeh@Sun.Com 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
22178313SDina.Nimeh@Sun.Com 	 * for encrypted lofi, advertise that it is somewhat shorter
22188313SDina.Nimeh@Sun.Com 	 * due to embedded crypto metadata section
22198313SDina.Nimeh@Sun.Com 	 */
22208313SDina.Nimeh@Sun.Com 	if (need_size_update) {
22215643Saalok 		/* update DDI properties */
22228313SDina.Nimeh@Sun.Com 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
22235643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
22245643Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
22255643Saalok 			error = EINVAL;
22265643Saalok 			goto propout;
22275643Saalok 		}
22288313SDina.Nimeh@Sun.Com 		Nblocks_prop_val =
22298313SDina.Nimeh@Sun.Com 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
22305643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
22315643Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
22325643Saalok 			error = EINVAL;
22335643Saalok 			goto propout;
22345643Saalok 		}
22355643Saalok 	}
22365643Saalok 
22370Sstevel@tonic-gate 	fake_disk_geometry(lsp);
22380Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
22391657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22400Sstevel@tonic-gate 	free_lofi_ioctl(klip);
22410Sstevel@tonic-gate 	return (0);
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate propout:
22448313SDina.Nimeh@Sun.Com 	if (keycopied) {
22458313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
22468313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
22478313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
22488313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
22498313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
22508313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
22518313SDina.Nimeh@Sun.Com 	}
22528313SDina.Nimeh@Sun.Com 
22538313SDina.Nimeh@Sun.Com 	if (zalloced)
22548313SDina.Nimeh@Sun.Com 		ddi_soft_state_free(lofi_statep, newminor);
22558313SDina.Nimeh@Sun.Com 
22560Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
22570Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
22588313SDina.Nimeh@Sun.Com 
22590Sstevel@tonic-gate out:
22608313SDina.Nimeh@Sun.Com 	if (need_vn_close) {
22618313SDina.Nimeh@Sun.Com 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
22628313SDina.Nimeh@Sun.Com 		VN_RELE(vp);
22638313SDina.Nimeh@Sun.Com 	}
22648313SDina.Nimeh@Sun.Com 
22650Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
22660Sstevel@tonic-gate 	free_lofi_ioctl(klip);
22670Sstevel@tonic-gate 	return (error);
22680Sstevel@tonic-gate }
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate /*
22710Sstevel@tonic-gate  * unmap a file.
22720Sstevel@tonic-gate  */
22730Sstevel@tonic-gate static int
22740Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
22751657Sheppo     struct cred *credp, int ioctl_flag)
22760Sstevel@tonic-gate {
22770Sstevel@tonic-gate 	struct lofi_state *lsp;
22780Sstevel@tonic-gate 	struct lofi_ioctl *klip;
22790Sstevel@tonic-gate 	minor_t	minor;
22800Sstevel@tonic-gate 
22811657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
22820Sstevel@tonic-gate 	if (klip == NULL)
22830Sstevel@tonic-gate 		return (EFAULT);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
22860Sstevel@tonic-gate 	if (byfilename) {
22870Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
22880Sstevel@tonic-gate 	} else {
22890Sstevel@tonic-gate 		minor = klip->li_minor;
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate 	if (minor == 0) {
22920Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22930Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22940Sstevel@tonic-gate 		return (ENXIO);
22950Sstevel@tonic-gate 	}
22960Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
22974451Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
22980Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22990Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23000Sstevel@tonic-gate 		return (ENXIO);
23010Sstevel@tonic-gate 	}
23024451Seschrock 
23036734Sjohnlev 	/*
23046734Sjohnlev 	 * If it's still held open, we'll do one of three things:
23056734Sjohnlev 	 *
23066734Sjohnlev 	 * If no flag is set, just return EBUSY.
23076734Sjohnlev 	 *
23086734Sjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
23096734Sjohnlev 	 * the last user finishes.
23106734Sjohnlev 	 *
23116734Sjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
23126734Sjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
23136734Sjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
23146734Sjohnlev 	 * device will be cleaned up appropriately.
23156734Sjohnlev 	 *
23166734Sjohnlev 	 * This is complicated by the fact that we may have outstanding
23176734Sjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
23188313SDina.Nimeh@Sun.Com 	 * I/O, we keep a count of the number of outstanding I/O requests
23198313SDina.Nimeh@Sun.Com 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
23208313SDina.Nimeh@Sun.Com 	 * should be dispatched (ls_vp_closereq).
23218313SDina.Nimeh@Sun.Com 	 *
23226734Sjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
23236734Sjohnlev 	 * and then close the underlying vnode.
23246734Sjohnlev 	 */
23250Sstevel@tonic-gate 	if (is_opened(lsp)) {
23264451Seschrock 		if (klip->li_force) {
23274451Seschrock 			mutex_enter(&lsp->ls_vp_lock);
23284451Seschrock 			lsp->ls_vp_closereq = B_TRUE;
232911041SEric.Taylor@Sun.COM 			/* wake up any threads waiting on dkiocstate */
233011041SEric.Taylor@Sun.COM 			cv_broadcast(&lsp->ls_vp_cv);
23314451Seschrock 			while (lsp->ls_vp_iocount > 0)
23324451Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
23334451Seschrock 			mutex_exit(&lsp->ls_vp_lock);
233411041SEric.Taylor@Sun.COM 			lofi_free_handle(dev, minor, lsp, credp);
23358313SDina.Nimeh@Sun.Com 
23368313SDina.Nimeh@Sun.Com 			klip->li_minor = minor;
23374451Seschrock 			mutex_exit(&lofi_lock);
23384451Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23394451Seschrock 			free_lofi_ioctl(klip);
23404451Seschrock 			return (0);
23416734Sjohnlev 		} else if (klip->li_cleanup) {
23426734Sjohnlev 			lsp->ls_cleanup = 1;
23436734Sjohnlev 			mutex_exit(&lofi_lock);
23446734Sjohnlev 			free_lofi_ioctl(klip);
23456734Sjohnlev 			return (0);
23464451Seschrock 		}
23476734Sjohnlev 
23480Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
23490Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23500Sstevel@tonic-gate 		return (EBUSY);
23510Sstevel@tonic-gate 	}
23520Sstevel@tonic-gate 
23534451Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 	klip->li_minor = minor;
23560Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
23571657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23580Sstevel@tonic-gate 	free_lofi_ioctl(klip);
23590Sstevel@tonic-gate 	return (0);
23600Sstevel@tonic-gate }
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate /*
23630Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
23640Sstevel@tonic-gate  * the name.
23650Sstevel@tonic-gate  */
23664451Seschrock /*ARGSUSED*/
23670Sstevel@tonic-gate static int
23680Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
23691657Sheppo     struct cred *credp, int ioctl_flag)
23700Sstevel@tonic-gate {
23710Sstevel@tonic-gate 	struct lofi_state *lsp;
23720Sstevel@tonic-gate 	struct lofi_ioctl *klip;
23730Sstevel@tonic-gate 	int	error;
23740Sstevel@tonic-gate 	minor_t	minor;
23750Sstevel@tonic-gate 
23761657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
23770Sstevel@tonic-gate 	if (klip == NULL)
23780Sstevel@tonic-gate 		return (EFAULT);
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	switch (which) {
23810Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
23820Sstevel@tonic-gate 		minor = klip->li_minor;
23830Sstevel@tonic-gate 		if (minor == 0) {
23840Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23850Sstevel@tonic-gate 			return (EINVAL);
23860Sstevel@tonic-gate 		}
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23890Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
23900Sstevel@tonic-gate 		if (lsp == NULL) {
23910Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
23920Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23930Sstevel@tonic-gate 			return (ENXIO);
23940Sstevel@tonic-gate 		}
23950Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
23965643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
23975643Saalok 		    sizeof (klip->li_algorithm));
23988313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
23990Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
24001657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
24010Sstevel@tonic-gate 		free_lofi_ioctl(klip);
24020Sstevel@tonic-gate 		return (error);
24030Sstevel@tonic-gate 	case LOFI_GET_MINOR:
24040Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
24050Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
24068313SDina.Nimeh@Sun.Com 		/* caller should not depend on klip->li_crypto_enabled here */
24070Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
24080Sstevel@tonic-gate 		if (klip->li_minor == 0) {
24090Sstevel@tonic-gate 			free_lofi_ioctl(klip);
24100Sstevel@tonic-gate 			return (ENOENT);
24110Sstevel@tonic-gate 		}
24121657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
24130Sstevel@tonic-gate 		free_lofi_ioctl(klip);
24140Sstevel@tonic-gate 		return (error);
24155643Saalok 	case LOFI_CHECK_COMPRESSED:
24165643Saalok 		mutex_enter(&lofi_lock);
24175643Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
24185643Saalok 		mutex_exit(&lofi_lock);
24195643Saalok 		if (klip->li_minor == 0) {
24205643Saalok 			free_lofi_ioctl(klip);
24215643Saalok 			return (ENOENT);
24225643Saalok 		}
24235643Saalok 		mutex_enter(&lofi_lock);
24245643Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
24255643Saalok 		if (lsp == NULL) {
24265643Saalok 			mutex_exit(&lofi_lock);
24275643Saalok 			free_lofi_ioctl(klip);
24285643Saalok 			return (ENXIO);
24295643Saalok 		}
24305643Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
24315643Saalok 
24325643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
24335643Saalok 		    sizeof (klip->li_algorithm));
24345643Saalok 		mutex_exit(&lofi_lock);
24355643Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
24365643Saalok 		free_lofi_ioctl(klip);
24375643Saalok 		return (error);
24380Sstevel@tonic-gate 	default:
24390Sstevel@tonic-gate 		free_lofi_ioctl(klip);
24400Sstevel@tonic-gate 		return (EINVAL);
24410Sstevel@tonic-gate 	}
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate }
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate static int
24460Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
24470Sstevel@tonic-gate     int *rvalp)
24480Sstevel@tonic-gate {
24490Sstevel@tonic-gate 	int	error;
24500Sstevel@tonic-gate 	enum dkio_state dkstate;
24510Sstevel@tonic-gate 	struct lofi_state *lsp;
24520Sstevel@tonic-gate 	minor_t	minor;
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	minor = getminor(dev);
24550Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
24560Sstevel@tonic-gate 	if (minor == 0) {
24570Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 		/*
24600Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
24610Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
24620Sstevel@tonic-gate 		 * long as they can open it read-only.
24630Sstevel@tonic-gate 		 */
24640Sstevel@tonic-gate 		switch (cmd) {
24650Sstevel@tonic-gate 		case LOFI_MAP_FILE:
24660Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24670Sstevel@tonic-gate 				return (EPERM);
24681657Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
24690Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
24700Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24710Sstevel@tonic-gate 				return (EPERM);
24721657Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
24730Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
24740Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24750Sstevel@tonic-gate 				return (EPERM);
24761657Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
24770Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
24780Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24790Sstevel@tonic-gate 				return (EPERM);
24801657Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
24810Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
24820Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
24831657Sheppo 			    credp, flag));
24840Sstevel@tonic-gate 		case LOFI_GET_MINOR:
24850Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
24861657Sheppo 			    credp, flag));
24870Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
24881657Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
24891657Sheppo 			    sizeof (lofi_max_files), flag);
24900Sstevel@tonic-gate 			if (error)
24910Sstevel@tonic-gate 				return (EFAULT);
24920Sstevel@tonic-gate 			return (0);
24935643Saalok 		case LOFI_CHECK_COMPRESSED:
24945643Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
24955643Saalok 			    credp, flag));
24960Sstevel@tonic-gate 		default:
24970Sstevel@tonic-gate 			break;
24980Sstevel@tonic-gate 		}
24990Sstevel@tonic-gate 	}
25000Sstevel@tonic-gate 
250111041SEric.Taylor@Sun.COM 	mutex_enter(&lofi_lock);
25020Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
250311041SEric.Taylor@Sun.COM 	if (lsp == NULL || lsp->ls_vp_closereq) {
250411041SEric.Taylor@Sun.COM 		mutex_exit(&lofi_lock);
25050Sstevel@tonic-gate 		return (ENXIO);
250611041SEric.Taylor@Sun.COM 	}
250711041SEric.Taylor@Sun.COM 	mutex_exit(&lofi_lock);
25080Sstevel@tonic-gate 
25094451Seschrock 	/*
25104451Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
25114451Seschrock 	 * EIO as if the device was no longer present.
25124451Seschrock 	 */
25134451Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
25144451Seschrock 		return (EIO);
25154451Seschrock 
25160Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
25170Sstevel@tonic-gate 	switch (cmd) {
25180Sstevel@tonic-gate 	case DKIOCGVTOC:
25190Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
25200Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
25210Sstevel@tonic-gate 			struct vtoc32 vtoc32;
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
25240Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
25250Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
25260Sstevel@tonic-gate 				return (EFAULT);
25278719SDina.Nimeh@Sun.COM 			break;
25280Sstevel@tonic-gate 			}
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 		case DDI_MODEL_NONE:
25310Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
25320Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
25330Sstevel@tonic-gate 				return (EFAULT);
25340Sstevel@tonic-gate 			break;
25350Sstevel@tonic-gate 		}
25360Sstevel@tonic-gate 		return (0);
25370Sstevel@tonic-gate 	case DKIOCINFO:
25381657Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
25391657Sheppo 		    sizeof (struct dk_cinfo), flag);
25400Sstevel@tonic-gate 		if (error)
25410Sstevel@tonic-gate 			return (EFAULT);
25420Sstevel@tonic-gate 		return (0);
25430Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
25440Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
25450Sstevel@tonic-gate 	case DKIOCGGEOM:
25461657Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
25471657Sheppo 		    sizeof (struct dk_geom), flag);
25480Sstevel@tonic-gate 		if (error)
25490Sstevel@tonic-gate 			return (EFAULT);
25500Sstevel@tonic-gate 		return (0);
25510Sstevel@tonic-gate 	case DKIOCSTATE:
25524451Seschrock 		/*
25534451Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
25544451Seschrock 		 * a device is forcefully unmapped, then the device transitions
25554451Seschrock 		 * to the DKIO_DEV_GONE state.
25564451Seschrock 		 */
25574451Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
25584451Seschrock 		    flag) != 0)
25594451Seschrock 			return (EFAULT);
25604451Seschrock 
25614451Seschrock 		mutex_enter(&lsp->ls_vp_lock);
256211041SEric.Taylor@Sun.COM 		lsp->ls_vp_iocount++;
256311041SEric.Taylor@Sun.COM 		while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
256411041SEric.Taylor@Sun.COM 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
256511041SEric.Taylor@Sun.COM 		    !lsp->ls_vp_closereq) {
25664451Seschrock 			/*
25674451Seschrock 			 * By virtue of having the device open, we know that
25684451Seschrock 			 * 'lsp' will remain valid when we return.
25694451Seschrock 			 */
25704451Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
25714451Seschrock 			    &lsp->ls_vp_lock)) {
257211041SEric.Taylor@Sun.COM 				lsp->ls_vp_iocount--;
257311041SEric.Taylor@Sun.COM 				cv_broadcast(&lsp->ls_vp_cv);
25744451Seschrock 				mutex_exit(&lsp->ls_vp_lock);
25754451Seschrock 				return (EINTR);
25764451Seschrock 			}
25774451Seschrock 		}
25784451Seschrock 
257911041SEric.Taylor@Sun.COM 		dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ?
258011041SEric.Taylor@Sun.COM 		    DKIO_INSERTED : DKIO_DEV_GONE);
258111041SEric.Taylor@Sun.COM 		lsp->ls_vp_iocount--;
258211041SEric.Taylor@Sun.COM 		cv_broadcast(&lsp->ls_vp_cv);
25834451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
25844451Seschrock 
25854451Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
25864451Seschrock 		    sizeof (dkstate), flag) != 0)
25870Sstevel@tonic-gate 			return (EFAULT);
25880Sstevel@tonic-gate 		return (0);
25890Sstevel@tonic-gate 	default:
25900Sstevel@tonic-gate 		return (ENOTTY);
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate }
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
25950Sstevel@tonic-gate 	lofi_open,		/* open */
25960Sstevel@tonic-gate 	lofi_close,		/* close */
25970Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
25980Sstevel@tonic-gate 	nodev,			/* print */
25990Sstevel@tonic-gate 	nodev,			/* dump */
26000Sstevel@tonic-gate 	lofi_read,		/* read */
26010Sstevel@tonic-gate 	lofi_write,		/* write */
26020Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
26030Sstevel@tonic-gate 	nodev,			/* devmap */
26040Sstevel@tonic-gate 	nodev,			/* mmap */
26050Sstevel@tonic-gate 	nodev,			/* segmap */
26060Sstevel@tonic-gate 	nochpoll,		/* poll */
26070Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
26080Sstevel@tonic-gate 	0,			/* streamtab  */
26090Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
26100Sstevel@tonic-gate 	CB_REV,
26110Sstevel@tonic-gate 	lofi_aread,
26120Sstevel@tonic-gate 	lofi_awrite
26130Sstevel@tonic-gate };
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate static struct dev_ops lofi_ops = {
26160Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
26170Sstevel@tonic-gate 	0,			/* refcnt  */
26180Sstevel@tonic-gate 	lofi_info,		/* info */
26190Sstevel@tonic-gate 	nulldev,		/* identify */
26200Sstevel@tonic-gate 	nulldev,		/* probe */
26210Sstevel@tonic-gate 	lofi_attach,		/* attach */
26220Sstevel@tonic-gate 	lofi_detach,		/* detach */
26230Sstevel@tonic-gate 	nodev,			/* reset */
26240Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
26257656SSherry.Moore@Sun.COM 	NULL,			/* no bus operations */
26267656SSherry.Moore@Sun.COM 	NULL,			/* power */
26278313SDina.Nimeh@Sun.Com 	ddi_quiesce_not_needed,	/* quiesce */
26280Sstevel@tonic-gate };
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate static struct modldrv modldrv = {
26310Sstevel@tonic-gate 	&mod_driverops,
26327656SSherry.Moore@Sun.COM 	"loopback file driver",
26330Sstevel@tonic-gate 	&lofi_ops,
26340Sstevel@tonic-gate };
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate static struct modlinkage modlinkage = {
26370Sstevel@tonic-gate 	MODREV_1,
26380Sstevel@tonic-gate 	&modldrv,
26390Sstevel@tonic-gate 	NULL
26400Sstevel@tonic-gate };
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate int
26430Sstevel@tonic-gate _init(void)
26440Sstevel@tonic-gate {
26450Sstevel@tonic-gate 	int error;
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
26480Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
26490Sstevel@tonic-gate 	if (error)
26500Sstevel@tonic-gate 		return (error);
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
26530Sstevel@tonic-gate 	error = mod_install(&modlinkage);
26540Sstevel@tonic-gate 	if (error) {
26550Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
26560Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
26570Sstevel@tonic-gate 	}
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	return (error);
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate int
26630Sstevel@tonic-gate _fini(void)
26640Sstevel@tonic-gate {
26650Sstevel@tonic-gate 	int	error;
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	if (lofi_busy())
26680Sstevel@tonic-gate 		return (EBUSY);
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
26710Sstevel@tonic-gate 	if (error)
26720Sstevel@tonic-gate 		return (error);
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
26750Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	return (error);
26780Sstevel@tonic-gate }
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate int
26810Sstevel@tonic-gate _info(struct modinfo *modinfop)
26820Sstevel@tonic-gate {
26830Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
26840Sstevel@tonic-gate }
2685