xref: /onnv-gate/usr/src/uts/common/io/lofi.c (revision 12368:679b134e4515)
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 /*
22*12368SFrank.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];
3534451Seschrock 
3548313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
3558313SDina.Nimeh@Sun.Com 
3568313SDina.Nimeh@Sun.Com 	lofi_free_crypto(lsp);
3578313SDina.Nimeh@Sun.Com 
3584451Seschrock 	if (lsp->ls_vp) {
3595331Samw 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
3605331Samw 		    1, 0, credp, NULL);
3614451Seschrock 		VN_RELE(lsp->ls_vp);
3624451Seschrock 		lsp->ls_vp = NULL;
3634451Seschrock 	}
3644451Seschrock 
3654451Seschrock 	newdev = makedevice(getmajor(dev), minor);
3664451Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
3674451Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
3684451Seschrock 
3694451Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
3704451Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3714451Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
3724451Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3734451Seschrock 
3744451Seschrock 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
3754451Seschrock 	taskq_destroy(lsp->ls_taskq);
3764451Seschrock 	if (lsp->ls_kstat) {
3774451Seschrock 		kstat_delete(lsp->ls_kstat);
3784451Seschrock 		mutex_destroy(&lsp->ls_kstat_lock);
3794451Seschrock 	}
3806791Saalok 
3819048Sjrgn.keil@googlemail.com 	/*
3829048Sjrgn.keil@googlemail.com 	 * Free cached decompressed segment data
3839048Sjrgn.keil@googlemail.com 	 */
3849048Sjrgn.keil@googlemail.com 	lofi_free_comp_cache(lsp);
3859048Sjrgn.keil@googlemail.com 	list_destroy(&lsp->ls_comp_cache);
3869048Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_comp_cache_lock);
3879048Sjrgn.keil@googlemail.com 
3886791Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
3896791Saalok 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
3906791Saalok 		lsp->ls_uncomp_seg_sz = 0;
3916791Saalok 	}
3929048Sjrgn.keil@googlemail.com 
3939048Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_vp_lock);
3949048Sjrgn.keil@googlemail.com 
3954451Seschrock 	ddi_soft_state_free(lofi_statep, minor);
3964451Seschrock }
3974451Seschrock 
3984451Seschrock /*ARGSUSED*/
3990Sstevel@tonic-gate static int
4000Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	minor_t	minor;
4030Sstevel@tonic-gate 	struct lofi_state *lsp;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4060Sstevel@tonic-gate 	minor = getminor(*devp);
4070Sstevel@tonic-gate 	if (minor == 0) {
4080Sstevel@tonic-gate 		/* master control device */
4090Sstevel@tonic-gate 		/* must be opened exclusively */
4100Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
4110Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4120Sstevel@tonic-gate 			return (EINVAL);
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
4150Sstevel@tonic-gate 		if (lsp == NULL) {
4160Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4170Sstevel@tonic-gate 			return (ENXIO);
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 		if (is_opened(lsp)) {
4200Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
4210Sstevel@tonic-gate 			return (EBUSY);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
4240Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4250Sstevel@tonic-gate 		return (0);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
4290Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4300Sstevel@tonic-gate 	if (lsp == NULL) {
4310Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4320Sstevel@tonic-gate 		return (EINVAL);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4354451Seschrock 	if (lsp->ls_vp == NULL) {
4364451Seschrock 		mutex_exit(&lofi_lock);
4374451Seschrock 		return (ENXIO);
4384451Seschrock 	}
4394451Seschrock 
4400Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
4410Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4420Sstevel@tonic-gate 		return (EINVAL);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4460Sstevel@tonic-gate 	return (0);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4494451Seschrock /*ARGSUSED*/
4500Sstevel@tonic-gate static int
4510Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	minor_t	minor;
4540Sstevel@tonic-gate 	struct lofi_state *lsp;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
4570Sstevel@tonic-gate 	minor = getminor(dev);
4580Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
4590Sstevel@tonic-gate 	if (lsp == NULL) {
4600Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
4610Sstevel@tonic-gate 		return (EINVAL);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 	mark_closed(lsp, otyp);
4644451Seschrock 
4654451Seschrock 	/*
4666734Sjohnlev 	 * If we forcibly closed the underlying device (li_force), or
4676734Sjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
4686734Sjohnlev 	 * out of the door.
4694451Seschrock 	 */
4706734Sjohnlev 	if (minor != 0 && !is_opened(lsp) &&
4716734Sjohnlev 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
4724451Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
4736734Sjohnlev 
4740Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4750Sstevel@tonic-gate 	return (0);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4788313SDina.Nimeh@Sun.Com /*
4798313SDina.Nimeh@Sun.Com  * Sets the mechanism's initialization vector (IV) if one is needed.
4808313SDina.Nimeh@Sun.Com  * The IV is computed from the data block number.  lsp->ls_mech is
4818313SDina.Nimeh@Sun.Com  * altered so that:
4828313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param_len is set to the IV len.
4838313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param is set to the IV.
4848313SDina.Nimeh@Sun.Com  */
4858313SDina.Nimeh@Sun.Com static int
4868313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
4878313SDina.Nimeh@Sun.Com {
4888313SDina.Nimeh@Sun.Com 	int	ret;
4898313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
4908313SDina.Nimeh@Sun.Com 	char	*iv;
4918313SDina.Nimeh@Sun.Com 	size_t	iv_len;
4928313SDina.Nimeh@Sun.Com 	size_t	min;
4938313SDina.Nimeh@Sun.Com 	void	*data;
4948313SDina.Nimeh@Sun.Com 	size_t	datasz;
4958313SDina.Nimeh@Sun.Com 
4968313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
4978313SDina.Nimeh@Sun.Com 
4988313SDina.Nimeh@Sun.Com 	if (lsp == NULL)
4998313SDina.Nimeh@Sun.Com 		return (CRYPTO_DEVICE_ERROR);
5008313SDina.Nimeh@Sun.Com 
5018313SDina.Nimeh@Sun.Com 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
5028313SDina.Nimeh@Sun.Com 	if (lsp->ls_iv_type == IVM_NONE) {
5038313SDina.Nimeh@Sun.Com 		return (CRYPTO_SUCCESS);
5048313SDina.Nimeh@Sun.Com 	}
5058313SDina.Nimeh@Sun.Com 
5068313SDina.Nimeh@Sun.Com 	/*
5078313SDina.Nimeh@Sun.Com 	 * if kmem already alloced from previous call and it's the same size
5088313SDina.Nimeh@Sun.Com 	 * we need now, just recycle it; allocate new kmem only if we have to
5098313SDina.Nimeh@Sun.Com 	 */
5108313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param == NULL ||
5118313SDina.Nimeh@Sun.Com 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
5128313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_iv_len;
5138313SDina.Nimeh@Sun.Com 		iv = kmem_zalloc(iv_len, KM_SLEEP);
5148313SDina.Nimeh@Sun.Com 	} else {
5158313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_mech.cm_param_len;
5168313SDina.Nimeh@Sun.Com 		iv = lsp->ls_mech.cm_param;
5178313SDina.Nimeh@Sun.Com 		bzero(iv, iv_len);
5188313SDina.Nimeh@Sun.Com 	}
5198313SDina.Nimeh@Sun.Com 
5208313SDina.Nimeh@Sun.Com 	switch (lsp->ls_iv_type) {
5218313SDina.Nimeh@Sun.Com 	case IVM_ENC_BLKNO:
5228313SDina.Nimeh@Sun.Com 		/* iv is not static, lblkno changes each time */
5238313SDina.Nimeh@Sun.Com 		data = &lblkno;
5248313SDina.Nimeh@Sun.Com 		datasz = sizeof (lblkno);
5258313SDina.Nimeh@Sun.Com 		break;
5268313SDina.Nimeh@Sun.Com 	default:
5278313SDina.Nimeh@Sun.Com 		data = 0;
5288313SDina.Nimeh@Sun.Com 		datasz = 0;
5298313SDina.Nimeh@Sun.Com 		break;
5308313SDina.Nimeh@Sun.Com 	}
5318313SDina.Nimeh@Sun.Com 
5328313SDina.Nimeh@Sun.Com 	/*
5338313SDina.Nimeh@Sun.Com 	 * write blkno into the iv buffer padded on the left in case
5348313SDina.Nimeh@Sun.Com 	 * blkno ever grows bigger than its current longlong_t size
5358313SDina.Nimeh@Sun.Com 	 * or a variation other than blkno is used for the iv data
5368313SDina.Nimeh@Sun.Com 	 */
5378313SDina.Nimeh@Sun.Com 	min = MIN(datasz, iv_len);
5388313SDina.Nimeh@Sun.Com 	bcopy(data, iv + (iv_len - min), min);
5398313SDina.Nimeh@Sun.Com 
5408313SDina.Nimeh@Sun.Com 	/* encrypt the data in-place to get the IV */
5418313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, iv, iv_len);
5428313SDina.Nimeh@Sun.Com 
5438313SDina.Nimeh@Sun.Com 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
5448313SDina.Nimeh@Sun.Com 	    NULL, NULL, NULL);
5458313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
5468313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
5478313SDina.Nimeh@Sun.Com 		    lblkno, ret);
5488313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != iv)
5498313SDina.Nimeh@Sun.Com 			kmem_free(iv, iv_len);
5508996SAlok.Aggarwal@Sun.COM 
5518313SDina.Nimeh@Sun.Com 		return (ret);
5528313SDina.Nimeh@Sun.Com 	}
5538313SDina.Nimeh@Sun.Com 
5548313SDina.Nimeh@Sun.Com 	/* clean up the iv from the last computation */
5558313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
5568313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
5578996SAlok.Aggarwal@Sun.COM 
5588313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param_len = iv_len;
5598313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param = iv;
5608313SDina.Nimeh@Sun.Com 
5618313SDina.Nimeh@Sun.Com 	return (CRYPTO_SUCCESS);
5628313SDina.Nimeh@Sun.Com }
5638313SDina.Nimeh@Sun.Com 
5648313SDina.Nimeh@Sun.Com /*
5658313SDina.Nimeh@Sun.Com  * Performs encryption and decryption of a chunk of data of size "len",
5668313SDina.Nimeh@Sun.Com  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
5678313SDina.Nimeh@Sun.Com  * DEV_BSIZE.
5688313SDina.Nimeh@Sun.Com  */
5698313SDina.Nimeh@Sun.Com static int
5708313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
5718313SDina.Nimeh@Sun.Com     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
5728313SDina.Nimeh@Sun.Com {
5738313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
5748313SDina.Nimeh@Sun.Com 	crypto_data_t wdata;
5758313SDina.Nimeh@Sun.Com 	int ret;
5768313SDina.Nimeh@Sun.Com 	longlong_t lblkno = bp->b_lblkno;
5778313SDina.Nimeh@Sun.Com 
5788313SDina.Nimeh@Sun.Com 	mutex_enter(&lsp->ls_crypto_lock);
5798313SDina.Nimeh@Sun.Com 
5808313SDina.Nimeh@Sun.Com 	/*
5818313SDina.Nimeh@Sun.Com 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
5828313SDina.Nimeh@Sun.Com 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
5838313SDina.Nimeh@Sun.Com 	 */
5848313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, plaintext, len);
5858313SDina.Nimeh@Sun.Com 	cdata.cd_length = DEV_BSIZE;
5868313SDina.Nimeh@Sun.Com 	if (ciphertext != NULL) {		/* not in-place crypto */
5878313SDina.Nimeh@Sun.Com 		SETUP_C_DATA(wdata, ciphertext, len);
5888313SDina.Nimeh@Sun.Com 		wdata.cd_length = DEV_BSIZE;
5898313SDina.Nimeh@Sun.Com 	}
5908313SDina.Nimeh@Sun.Com 
5918313SDina.Nimeh@Sun.Com 	do {
5928313SDina.Nimeh@Sun.Com 		ret = lofi_blk_mech(lsp, lblkno);
5938313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS)
5948313SDina.Nimeh@Sun.Com 			continue;
5958313SDina.Nimeh@Sun.Com 
5968313SDina.Nimeh@Sun.Com 		if (op_encrypt) {
5978313SDina.Nimeh@Sun.Com 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
5988313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
5998313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6008313SDina.Nimeh@Sun.Com 		} else {
6018313SDina.Nimeh@Sun.Com 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
6028313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
6038313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
6048313SDina.Nimeh@Sun.Com 		}
6058313SDina.Nimeh@Sun.Com 
6068313SDina.Nimeh@Sun.Com 		cdata.cd_offset += DEV_BSIZE;
6078313SDina.Nimeh@Sun.Com 		if (ciphertext != NULL)
6088313SDina.Nimeh@Sun.Com 			wdata.cd_offset += DEV_BSIZE;
6098313SDina.Nimeh@Sun.Com 		lblkno++;
6108313SDina.Nimeh@Sun.Com 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
6118313SDina.Nimeh@Sun.Com 
6128313SDina.Nimeh@Sun.Com 	mutex_exit(&lsp->ls_crypto_lock);
6138313SDina.Nimeh@Sun.Com 
6148313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
6158313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
6168313SDina.Nimeh@Sun.Com 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
6178313SDina.Nimeh@Sun.Com 		    lblkno, ret);
6188313SDina.Nimeh@Sun.Com 	}
6198313SDina.Nimeh@Sun.Com 
6208313SDina.Nimeh@Sun.Com 	return (ret);
6218313SDina.Nimeh@Sun.Com }
6228313SDina.Nimeh@Sun.Com 
6238313SDina.Nimeh@Sun.Com #define	RDWR_RAW	1
6248313SDina.Nimeh@Sun.Com #define	RDWR_BCOPY	2
6258313SDina.Nimeh@Sun.Com 
6268313SDina.Nimeh@Sun.Com static int
6278313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6288313SDina.Nimeh@Sun.Com     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
6298313SDina.Nimeh@Sun.Com {
6308313SDina.Nimeh@Sun.Com 	ssize_t resid;
6318313SDina.Nimeh@Sun.Com 	int isread;
6328313SDina.Nimeh@Sun.Com 	int error;
6338313SDina.Nimeh@Sun.Com 
6348313SDina.Nimeh@Sun.Com 	/*
6358313SDina.Nimeh@Sun.Com 	 * Handles reads/writes for both plain and encrypted lofi
6368313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
6378313SDina.Nimeh@Sun.Com 	 * when it gets here.
6388313SDina.Nimeh@Sun.Com 	 */
6398313SDina.Nimeh@Sun.Com 
6408313SDina.Nimeh@Sun.Com 	isread = bp->b_flags & B_READ;
6418313SDina.Nimeh@Sun.Com 	if (isread) {
6428313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6438313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6448313SDina.Nimeh@Sun.Com 			bcopy(bcopy_locn, bufaddr, len);
6458313SDina.Nimeh@Sun.Com 			error = 0;
6468313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
6478313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
6488313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6498313SDina.Nimeh@Sun.Com 			    &resid);
6508313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
6518313SDina.Nimeh@Sun.Com 		}
6528313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled && error == 0) {
6538313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
6548313SDina.Nimeh@Sun.Com 			    B_FALSE) != CRYPTO_SUCCESS) {
6558313SDina.Nimeh@Sun.Com 				/*
6568313SDina.Nimeh@Sun.Com 				 * XXX: original code didn't set residual
6578313SDina.Nimeh@Sun.Com 				 * back to len because no error was expected
6588313SDina.Nimeh@Sun.Com 				 * from bcopy() if encryption is not enabled
6598313SDina.Nimeh@Sun.Com 				 */
6608313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6618313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6628313SDina.Nimeh@Sun.Com 				error = EIO;
6638313SDina.Nimeh@Sun.Com 			}
6648313SDina.Nimeh@Sun.Com 		}
6658313SDina.Nimeh@Sun.Com 		return (error);
6668313SDina.Nimeh@Sun.Com 	} else {
6678313SDina.Nimeh@Sun.Com 		void *iobuf = bufaddr;
6688313SDina.Nimeh@Sun.Com 
6698313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6708313SDina.Nimeh@Sun.Com 			/* don't do in-place crypto to keep bufaddr intact */
6718313SDina.Nimeh@Sun.Com 			iobuf = kmem_alloc(len, KM_SLEEP);
6728313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
6738313SDina.Nimeh@Sun.Com 			    B_TRUE) != CRYPTO_SUCCESS) {
6748313SDina.Nimeh@Sun.Com 				kmem_free(iobuf, len);
6758313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6768313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6778313SDina.Nimeh@Sun.Com 				return (EIO);
6788313SDina.Nimeh@Sun.Com 			}
6798313SDina.Nimeh@Sun.Com 		}
6808313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6818313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6828313SDina.Nimeh@Sun.Com 			bcopy(iobuf, bcopy_locn, len);
6838313SDina.Nimeh@Sun.Com 			error = 0;
6848313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
6858313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
6868313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6878313SDina.Nimeh@Sun.Com 			    &resid);
6888313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
6898313SDina.Nimeh@Sun.Com 		}
6908313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6918313SDina.Nimeh@Sun.Com 			kmem_free(iobuf, len);
6928313SDina.Nimeh@Sun.Com 		}
6938313SDina.Nimeh@Sun.Com 		return (error);
6948313SDina.Nimeh@Sun.Com 	}
6958313SDina.Nimeh@Sun.Com }
6968313SDina.Nimeh@Sun.Com 
6975643Saalok static int
6985643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6998313SDina.Nimeh@Sun.Com     struct lofi_state *lsp)
7005643Saalok {
7015643Saalok 	int error;
7025643Saalok 	offset_t alignedoffset, mapoffset;
7035643Saalok 	size_t	xfersize;
7045643Saalok 	int	isread;
7058313SDina.Nimeh@Sun.Com 	int	smflags;
7065643Saalok 	caddr_t	mapaddr;
7075643Saalok 	size_t	len;
7085643Saalok 	enum seg_rw srw;
7098313SDina.Nimeh@Sun.Com 	int	save_error;
7108313SDina.Nimeh@Sun.Com 
7118313SDina.Nimeh@Sun.Com 	/*
7128313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
7138313SDina.Nimeh@Sun.Com 	 * when it gets here.
7148313SDina.Nimeh@Sun.Com 	 */
7158313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled)
7168313SDina.Nimeh@Sun.Com 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
7175643Saalok 
7185643Saalok 	/*
7195643Saalok 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
7205643Saalok 	 * an 8K boundary, but the buf transfer address may not be
7215643Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
7225643Saalok 	 * that even though we could). This matters since the initial
7235643Saalok 	 * part of the transfer may not start at offset 0 within the
7245643Saalok 	 * segmap'd chunk. So we have to compensate for that with
7255643Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
7265643Saalok 	 * beginning, and the last is capped by b_resid
7278313SDina.Nimeh@Sun.Com 	 *
7288313SDina.Nimeh@Sun.Com 	 * Visually, where "|" represents page map boundaries:
7298313SDina.Nimeh@Sun.Com 	 *   alignedoffset (mapaddr begins at this segmap boundary)
7308313SDina.Nimeh@Sun.Com 	 *    |   offset (from beginning of file)
7318313SDina.Nimeh@Sun.Com 	 *    |    |	   len
7328313SDina.Nimeh@Sun.Com 	 *    v    v	    v
7338313SDina.Nimeh@Sun.Com 	 * ===|====X========|====...======|========X====|====
7348313SDina.Nimeh@Sun.Com 	 *	   /-------------...---------------/
7358313SDina.Nimeh@Sun.Com 	 *		^ bp->b_bcount/bp->b_resid at start
7368313SDina.Nimeh@Sun.Com 	 *    /----/--------/----...------/--------/
7378313SDina.Nimeh@Sun.Com 	 *	^	^	^   ^		^
7388313SDina.Nimeh@Sun.Com 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
7398313SDina.Nimeh@Sun.Com 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
7408313SDina.Nimeh@Sun.Com 	 *	|	1st xfersize (<= MAXBSIZE)
7418313SDina.Nimeh@Sun.Com 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
7428313SDina.Nimeh@Sun.Com 	 *
7438313SDina.Nimeh@Sun.Com 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
7448313SDina.Nimeh@Sun.Com 	 * MAXBSIZE boundary.  "len" is next page boundary of size
7458719SDina.Nimeh@Sun.COM 	 * PAGESIZE after "alignedoffset".
7465643Saalok 	 */
7475643Saalok 	mapoffset = offset & MAXBOFFSET;
7485643Saalok 	alignedoffset = offset - mapoffset;
7495643Saalok 	bp->b_resid = bp->b_bcount;
7505643Saalok 	isread = bp->b_flags & B_READ;
7515643Saalok 	srw = isread ? S_READ : S_WRITE;
7525643Saalok 	do {
7535643Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
7545643Saalok 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
7558719SDina.Nimeh@Sun.COM 		len = roundup(mapoffset + xfersize, PAGESIZE);
7565643Saalok 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
7575643Saalok 		    alignedoffset, MAXBSIZE, 1, srw);
7585643Saalok 		/*
7595643Saalok 		 * Now fault in the pages. This lets us check
7605643Saalok 		 * for errors before we reference mapaddr and
7615643Saalok 		 * try to resolve the fault in bcopy (which would
7625643Saalok 		 * panic instead). And this can easily happen,
7635643Saalok 		 * particularly if you've lofi'd a file over NFS
7645643Saalok 		 * and someone deletes the file on the server.
7655643Saalok 		 */
7665643Saalok 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
7675643Saalok 		    len, F_SOFTLOCK, srw);
7685643Saalok 		if (error) {
7695643Saalok 			(void) segmap_release(segkmap, mapaddr, 0);
7705643Saalok 			if (FC_CODE(error) == FC_OBJERR)
7715643Saalok 				error = FC_ERRNO(error);
7725643Saalok 			else
7735643Saalok 				error = EIO;
7745643Saalok 			break;
7755643Saalok 		}
7768313SDina.Nimeh@Sun.Com 		/* error may be non-zero for encrypted lofi */
7778313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
7788313SDina.Nimeh@Sun.Com 		    RDWR_BCOPY, mapaddr + mapoffset);
7798313SDina.Nimeh@Sun.Com 		if (error == 0) {
7808313SDina.Nimeh@Sun.Com 			bp->b_resid -= xfersize;
7818313SDina.Nimeh@Sun.Com 			bufaddr += xfersize;
7828313SDina.Nimeh@Sun.Com 			offset += xfersize;
7838313SDina.Nimeh@Sun.Com 		}
7845643Saalok 		smflags = 0;
7855643Saalok 		if (isread) {
7865643Saalok 			smflags |= SM_FREE;
7875643Saalok 			/*
7885643Saalok 			 * If we're reading an entire page starting
7895643Saalok 			 * at a page boundary, there's a good chance
7905643Saalok 			 * we won't need it again. Put it on the
7915643Saalok 			 * head of the freelist.
7925643Saalok 			 */
7938056SDina.Nimeh@Sun.Com 			if (mapoffset == 0 && xfersize == MAXBSIZE)
7945643Saalok 				smflags |= SM_DONTNEED;
7955643Saalok 		} else {
796*12368SFrank.Batschulat@Sun.COM 			/*
797*12368SFrank.Batschulat@Sun.COM 			 * Write back good pages, it is okay to
798*12368SFrank.Batschulat@Sun.COM 			 * always release asynchronous here as we'll
799*12368SFrank.Batschulat@Sun.COM 			 * follow with VOP_FSYNC for B_SYNC buffers.
800*12368SFrank.Batschulat@Sun.COM 			 */
801*12368SFrank.Batschulat@Sun.COM 			if (error == 0)
802*12368SFrank.Batschulat@Sun.COM 				smflags |= SM_WRITE | SM_ASYNC;
8035643Saalok 		}
8045643Saalok 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
8055643Saalok 		    len, F_SOFTUNLOCK, srw);
8068313SDina.Nimeh@Sun.Com 		save_error = segmap_release(segkmap, mapaddr, smflags);
8078313SDina.Nimeh@Sun.Com 		if (error == 0)
8088313SDina.Nimeh@Sun.Com 			error = save_error;
8095643Saalok 		/* only the first map may start partial */
8105643Saalok 		mapoffset = 0;
8115643Saalok 		alignedoffset += MAXBSIZE;
8125643Saalok 	} while ((error == 0) && (bp->b_resid > 0) &&
8135643Saalok 	    (offset < lsp->ls_vp_comp_size));
8145643Saalok 
8155643Saalok 	return (error);
8165643Saalok }
8175643Saalok 
8189048Sjrgn.keil@googlemail.com /*
8199048Sjrgn.keil@googlemail.com  * Check if segment seg_index is present in the decompressed segment
8209048Sjrgn.keil@googlemail.com  * data cache.
8219048Sjrgn.keil@googlemail.com  *
8229048Sjrgn.keil@googlemail.com  * Returns a pointer to the decompressed segment data cache entry if
8239048Sjrgn.keil@googlemail.com  * found, and NULL when decompressed data for this segment is not yet
8249048Sjrgn.keil@googlemail.com  * cached.
8259048Sjrgn.keil@googlemail.com  */
8269048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8279048Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
8289048Sjrgn.keil@googlemail.com {
8299048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8309048Sjrgn.keil@googlemail.com 
8319048Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8329048Sjrgn.keil@googlemail.com 
8339048Sjrgn.keil@googlemail.com 	for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
8349048Sjrgn.keil@googlemail.com 	    lc = list_next(&lsp->ls_comp_cache, lc)) {
8359048Sjrgn.keil@googlemail.com 		if (lc->lc_index == seg_index) {
8369048Sjrgn.keil@googlemail.com 			/*
8379048Sjrgn.keil@googlemail.com 			 * Decompressed segment data was found in the
8389048Sjrgn.keil@googlemail.com 			 * cache.
8399048Sjrgn.keil@googlemail.com 			 *
8409048Sjrgn.keil@googlemail.com 			 * The cache uses an LRU replacement strategy;
8419048Sjrgn.keil@googlemail.com 			 * move the entry to head of list.
8429048Sjrgn.keil@googlemail.com 			 */
8439048Sjrgn.keil@googlemail.com 			list_remove(&lsp->ls_comp_cache, lc);
8449048Sjrgn.keil@googlemail.com 			list_insert_head(&lsp->ls_comp_cache, lc);
8459048Sjrgn.keil@googlemail.com 			return (lc);
8469048Sjrgn.keil@googlemail.com 		}
8479048Sjrgn.keil@googlemail.com 	}
8489048Sjrgn.keil@googlemail.com 	return (NULL);
8499048Sjrgn.keil@googlemail.com }
8509048Sjrgn.keil@googlemail.com 
8519048Sjrgn.keil@googlemail.com /*
8529048Sjrgn.keil@googlemail.com  * Add the data for a decompressed segment at segment index
8539048Sjrgn.keil@googlemail.com  * seg_index to the cache of the decompressed segments.
8549048Sjrgn.keil@googlemail.com  *
8559048Sjrgn.keil@googlemail.com  * Returns a pointer to the cache element structure in case
8569048Sjrgn.keil@googlemail.com  * the data was added to the cache; returns NULL when the data
8579048Sjrgn.keil@googlemail.com  * wasn't cached.
8589048Sjrgn.keil@googlemail.com  */
8599048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
8609048Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
8619048Sjrgn.keil@googlemail.com     uchar_t *data)
8629048Sjrgn.keil@googlemail.com {
8639048Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
8649048Sjrgn.keil@googlemail.com 
8659048Sjrgn.keil@googlemail.com 	ASSERT(mutex_owned(&lsp->ls_comp_cache_lock));
8669048Sjrgn.keil@googlemail.com 
8679048Sjrgn.keil@googlemail.com 	while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
8689048Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8699048Sjrgn.keil@googlemail.com 		ASSERT(lc != NULL);
8709048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
8719048Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
8729048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
8739048Sjrgn.keil@googlemail.com 	}
8749048Sjrgn.keil@googlemail.com 
8759048Sjrgn.keil@googlemail.com 	/*
8769048Sjrgn.keil@googlemail.com 	 * Do not cache when disabled by tunable variable
8779048Sjrgn.keil@googlemail.com 	 */
8789048Sjrgn.keil@googlemail.com 	if (lofi_max_comp_cache == 0)
8799048Sjrgn.keil@googlemail.com 		return (NULL);
8809048Sjrgn.keil@googlemail.com 
8819048Sjrgn.keil@googlemail.com 	/*
8829048Sjrgn.keil@googlemail.com 	 * When the cache has not yet reached the maximum allowed
8839048Sjrgn.keil@googlemail.com 	 * number of segments, allocate a new cache element.
8849048Sjrgn.keil@googlemail.com 	 * Otherwise the cache is full; reuse the last list element
8859048Sjrgn.keil@googlemail.com 	 * (LRU) for caching the decompressed segment data.
8869048Sjrgn.keil@googlemail.com 	 *
8879048Sjrgn.keil@googlemail.com 	 * The cache element for the new decompressed segment data is
8889048Sjrgn.keil@googlemail.com 	 * added to the head of the list.
8899048Sjrgn.keil@googlemail.com 	 */
8909048Sjrgn.keil@googlemail.com 	if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
8919048Sjrgn.keil@googlemail.com 		lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
8929048Sjrgn.keil@googlemail.com 		lc->lc_data = NULL;
8939048Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
8949048Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count++;
8959048Sjrgn.keil@googlemail.com 	} else {
8969048Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
8979048Sjrgn.keil@googlemail.com 		if (lc == NULL)
8989048Sjrgn.keil@googlemail.com 			return (NULL);
8999048Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
9009048Sjrgn.keil@googlemail.com 	}
9019048Sjrgn.keil@googlemail.com 
9029048Sjrgn.keil@googlemail.com 	/*
9039048Sjrgn.keil@googlemail.com 	 * Free old uncompressed segment data when reusing a cache
9049048Sjrgn.keil@googlemail.com 	 * entry.
9059048Sjrgn.keil@googlemail.com 	 */
9069048Sjrgn.keil@googlemail.com 	if (lc->lc_data != NULL)
9079048Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
9089048Sjrgn.keil@googlemail.com 
9099048Sjrgn.keil@googlemail.com 	lc->lc_data = data;
9109048Sjrgn.keil@googlemail.com 	lc->lc_index = seg_index;
9119048Sjrgn.keil@googlemail.com 	return (lc);
9129048Sjrgn.keil@googlemail.com }
9139048Sjrgn.keil@googlemail.com 
9149048Sjrgn.keil@googlemail.com 
9155643Saalok /*ARGSUSED*/
9168996SAlok.Aggarwal@Sun.COM static int
9178996SAlok.Aggarwal@Sun.COM gzip_decompress(void *src, size_t srclen, void *dst,
9185643Saalok     size_t *dstlen, int level)
9195643Saalok {
9205643Saalok 	ASSERT(*dstlen >= srclen);
9215643Saalok 
9225643Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
9235643Saalok 		return (-1);
9245643Saalok 	return (0);
9255643Saalok }
9265643Saalok 
9278996SAlok.Aggarwal@Sun.COM #define	LZMA_HEADER_SIZE	(LZMA_PROPS_SIZE + 8)
9288996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
9298996SAlok.Aggarwal@Sun.COM static int
9308996SAlok.Aggarwal@Sun.COM lzma_decompress(void *src, size_t srclen, void *dst,
9318996SAlok.Aggarwal@Sun.COM 	size_t *dstlen, int level)
9328996SAlok.Aggarwal@Sun.COM {
9338996SAlok.Aggarwal@Sun.COM 	size_t insizepure;
9348996SAlok.Aggarwal@Sun.COM 	void *actual_src;
9358996SAlok.Aggarwal@Sun.COM 	ELzmaStatus status;
9368996SAlok.Aggarwal@Sun.COM 
9378996SAlok.Aggarwal@Sun.COM 	insizepure = srclen - LZMA_HEADER_SIZE;
9388996SAlok.Aggarwal@Sun.COM 	actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
9398996SAlok.Aggarwal@Sun.COM 
9408996SAlok.Aggarwal@Sun.COM 	if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
9418996SAlok.Aggarwal@Sun.COM 	    (const Byte *)actual_src, &insizepure,
9428996SAlok.Aggarwal@Sun.COM 	    (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
9438996SAlok.Aggarwal@Sun.COM 	    &g_Alloc) != SZ_OK) {
9448996SAlok.Aggarwal@Sun.COM 		return (-1);
9458996SAlok.Aggarwal@Sun.COM 	}
9468996SAlok.Aggarwal@Sun.COM 	return (0);
9478996SAlok.Aggarwal@Sun.COM }
9488996SAlok.Aggarwal@Sun.COM 
9490Sstevel@tonic-gate /*
9500Sstevel@tonic-gate  * This is basically what strategy used to be before we found we
9510Sstevel@tonic-gate  * needed task queues.
9520Sstevel@tonic-gate  */
9530Sstevel@tonic-gate static void
9540Sstevel@tonic-gate lofi_strategy_task(void *arg)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	struct buf *bp = (struct buf *)arg;
9570Sstevel@tonic-gate 	int error;
958*12368SFrank.Batschulat@Sun.COM 	int syncflag = 0;
9590Sstevel@tonic-gate 	struct lofi_state *lsp;
9608313SDina.Nimeh@Sun.Com 	offset_t offset;
9618313SDina.Nimeh@Sun.Com 	caddr_t	bufaddr;
9628313SDina.Nimeh@Sun.Com 	size_t	len;
9638313SDina.Nimeh@Sun.Com 	size_t	xfersize;
9648313SDina.Nimeh@Sun.Com 	boolean_t bufinited = B_FALSE;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
9678313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
9688313SDina.Nimeh@Sun.Com 		error = ENXIO;
9698313SDina.Nimeh@Sun.Com 		goto errout;
9708313SDina.Nimeh@Sun.Com 	}
9710Sstevel@tonic-gate 	if (lsp->ls_kstat) {
9720Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
9730Sstevel@tonic-gate 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
9740Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 	bp_mapin(bp);
9770Sstevel@tonic-gate 	bufaddr = bp->b_un.b_addr;
9780Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
9798313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
9808313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
9818313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
9828313SDina.Nimeh@Sun.Com 	}
9838313SDina.Nimeh@Sun.Com 	len = bp->b_bcount;
9848313SDina.Nimeh@Sun.Com 	bufinited = B_TRUE;
9858313SDina.Nimeh@Sun.Com 
9868313SDina.Nimeh@Sun.Com 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
9878313SDina.Nimeh@Sun.Com 		error = EIO;
9888313SDina.Nimeh@Sun.Com 		goto errout;
9898313SDina.Nimeh@Sun.Com 	}
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	/*
992*12368SFrank.Batschulat@Sun.COM 	 * If we're writing and the buffer was not B_ASYNC
993*12368SFrank.Batschulat@Sun.COM 	 * we'll follow up with a VOP_FSYNC() to force any
994*12368SFrank.Batschulat@Sun.COM 	 * asynchronous I/O to stable storage.
995*12368SFrank.Batschulat@Sun.COM 	 */
996*12368SFrank.Batschulat@Sun.COM 	if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
997*12368SFrank.Batschulat@Sun.COM 		syncflag = FSYNC;
998*12368SFrank.Batschulat@Sun.COM 
999*12368SFrank.Batschulat@Sun.COM 	/*
10000Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
10010Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
10020Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
10030Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
10048313SDina.Nimeh@Sun.Com 	 * mapable or it's a character device or it's an encrypted lofi.
10050Sstevel@tonic-gate 	 */
10068313SDina.Nimeh@Sun.Com 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
10078313SDina.Nimeh@Sun.Com 	    lsp->ls_crypto_enabled) {
10088313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
10098313SDina.Nimeh@Sun.Com 		    NULL);
10108313SDina.Nimeh@Sun.Com 	} else if (lsp->ls_uncomp_seg_sz == 0) {
10118313SDina.Nimeh@Sun.Com 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
10128313SDina.Nimeh@Sun.Com 	} else {
10139048Sjrgn.keil@googlemail.com 		uchar_t *compressed_seg = NULL, *cmpbuf;
10149048Sjrgn.keil@googlemail.com 		uchar_t *uncompressed_seg = NULL;
10158313SDina.Nimeh@Sun.Com 		lofi_compress_info_t *li;
10168313SDina.Nimeh@Sun.Com 		size_t oblkcount;
10179048Sjrgn.keil@googlemail.com 		ulong_t seglen;
10188313SDina.Nimeh@Sun.Com 		uint64_t sblkno, eblkno, cmpbytes;
10199048Sjrgn.keil@googlemail.com 		uint64_t uncompressed_seg_index;
10209048Sjrgn.keil@googlemail.com 		struct lofi_comp_cache *lc;
10218313SDina.Nimeh@Sun.Com 		offset_t sblkoff, eblkoff;
10228313SDina.Nimeh@Sun.Com 		u_offset_t salign, ealign;
10238313SDina.Nimeh@Sun.Com 		u_offset_t sdiff;
10248313SDina.Nimeh@Sun.Com 		uint32_t comp_data_sz;
10255643Saalok 		uint64_t i;
10265643Saalok 
10270Sstevel@tonic-gate 		/*
10285643Saalok 		 * From here on we're dealing primarily with compressed files
10295643Saalok 		 */
10308313SDina.Nimeh@Sun.Com 		ASSERT(!lsp->ls_crypto_enabled);
10315643Saalok 
10325643Saalok 		/*
10335643Saalok 		 * Compressed files can only be read from and
10345643Saalok 		 * not written to
10350Sstevel@tonic-gate 		 */
10365643Saalok 		if (!(bp->b_flags & B_READ)) {
10375643Saalok 			bp->b_resid = bp->b_bcount;
10385643Saalok 			error = EROFS;
10395643Saalok 			goto done;
10405643Saalok 		}
10415643Saalok 
10425643Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
10435643Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
10445643Saalok 		/*
10455643Saalok 		 * Compute starting and ending compressed segment numbers
10465643Saalok 		 * We use only bitwise operations avoiding division and
10475643Saalok 		 * modulus because we enforce the compression segment size
10485643Saalok 		 * to a power of 2
10495643Saalok 		 */
10505643Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
10515643Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
10525643Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
10535643Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
10545643Saalok 
10555643Saalok 		/*
10569048Sjrgn.keil@googlemail.com 		 * Check the decompressed segment cache.
10579048Sjrgn.keil@googlemail.com 		 *
10589048Sjrgn.keil@googlemail.com 		 * The cache is used only when the requested data
10599048Sjrgn.keil@googlemail.com 		 * is within a segment. Requests that cross
10609048Sjrgn.keil@googlemail.com 		 * segment boundaries bypass the cache.
10619048Sjrgn.keil@googlemail.com 		 */
10629048Sjrgn.keil@googlemail.com 		if (sblkno == eblkno ||
10639048Sjrgn.keil@googlemail.com 		    (sblkno + 1 == eblkno && eblkoff == 0)) {
10649048Sjrgn.keil@googlemail.com 			/*
10659048Sjrgn.keil@googlemail.com 			 * Request doesn't cross a segment boundary,
10669048Sjrgn.keil@googlemail.com 			 * now check the cache.
10679048Sjrgn.keil@googlemail.com 			 */
10689048Sjrgn.keil@googlemail.com 			mutex_enter(&lsp->ls_comp_cache_lock);
10699048Sjrgn.keil@googlemail.com 			lc = lofi_find_comp_data(lsp, sblkno);
10709048Sjrgn.keil@googlemail.com 			if (lc != NULL) {
10719048Sjrgn.keil@googlemail.com 				/*
10729048Sjrgn.keil@googlemail.com 				 * We've found the decompressed segment
10739048Sjrgn.keil@googlemail.com 				 * data in the cache; reuse it.
10749048Sjrgn.keil@googlemail.com 				 */
10759048Sjrgn.keil@googlemail.com 				bcopy(lc->lc_data + sblkoff, bufaddr,
10769048Sjrgn.keil@googlemail.com 				    bp->b_bcount);
10779048Sjrgn.keil@googlemail.com 				mutex_exit(&lsp->ls_comp_cache_lock);
10789048Sjrgn.keil@googlemail.com 				bp->b_resid = 0;
10799048Sjrgn.keil@googlemail.com 				error = 0;
10809048Sjrgn.keil@googlemail.com 				goto done;
10819048Sjrgn.keil@googlemail.com 			}
10829048Sjrgn.keil@googlemail.com 			mutex_exit(&lsp->ls_comp_cache_lock);
10839048Sjrgn.keil@googlemail.com 		}
10849048Sjrgn.keil@googlemail.com 
10859048Sjrgn.keil@googlemail.com 		/*
10865643Saalok 		 * Align start offset to block boundary for segmap
10875643Saalok 		 */
10885643Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
10895643Saalok 		sdiff = salign & (DEV_BSIZE - 1);
10905643Saalok 		salign -= sdiff;
10915643Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
10920Sstevel@tonic-gate 			/*
10935643Saalok 			 * We're dealing with the last segment of
10945643Saalok 			 * the compressed file -- the size of this
10955643Saalok 			 * segment *may not* be the same as the
10965643Saalok 			 * segment size for the file
10970Sstevel@tonic-gate 			 */
10985643Saalok 			eblkoff = (offset + bp->b_bcount) &
10995643Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
11005643Saalok 			ealign = lsp->ls_vp_comp_size;
11015643Saalok 		} else {
11025643Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
11035643Saalok 		}
11045643Saalok 
11055643Saalok 		/*
11065643Saalok 		 * Preserve original request paramaters
11075643Saalok 		 */
11085643Saalok 		oblkcount = bp->b_bcount;
11095643Saalok 
11105643Saalok 		/*
11115643Saalok 		 * Assign the calculated parameters
11125643Saalok 		 */
11135643Saalok 		comp_data_sz = ealign - salign;
11145643Saalok 		bp->b_bcount = comp_data_sz;
11155643Saalok 
11165643Saalok 		/*
11175643Saalok 		 * Allocate fixed size memory blocks to hold compressed
11185643Saalok 		 * segments and one uncompressed segment since we
11195643Saalok 		 * uncompress segments one at a time
11205643Saalok 		 */
11215643Saalok 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
11225643Saalok 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
11235643Saalok 		/*
11245643Saalok 		 * Map in the calculated number of blocks
11255643Saalok 		 */
11265643Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
11275643Saalok 		    bp, lsp);
11285643Saalok 
11295643Saalok 		bp->b_bcount = oblkcount;
11305643Saalok 		bp->b_resid = oblkcount;
11315643Saalok 		if (error != 0)
11325643Saalok 			goto done;
11335643Saalok 
11345643Saalok 		/*
11355643Saalok 		 * We have the compressed blocks, now uncompress them
11365643Saalok 		 */
11375643Saalok 		cmpbuf = compressed_seg + sdiff;
11388996SAlok.Aggarwal@Sun.COM 		for (i = sblkno; i <= eblkno; i++) {
11398996SAlok.Aggarwal@Sun.COM 			ASSERT(i < lsp->ls_comp_index_sz - 1);
11408996SAlok.Aggarwal@Sun.COM 
11418996SAlok.Aggarwal@Sun.COM 			/*
11428996SAlok.Aggarwal@Sun.COM 			 * The last segment is special in that it is
11438996SAlok.Aggarwal@Sun.COM 			 * most likely not going to be the same
11448996SAlok.Aggarwal@Sun.COM 			 * (uncompressed) size as the other segments.
11458996SAlok.Aggarwal@Sun.COM 			 */
11468996SAlok.Aggarwal@Sun.COM 			if (i == (lsp->ls_comp_index_sz - 2)) {
11478996SAlok.Aggarwal@Sun.COM 				seglen = lsp->ls_uncomp_last_seg_sz;
11488996SAlok.Aggarwal@Sun.COM 			} else {
11498996SAlok.Aggarwal@Sun.COM 				seglen = lsp->ls_uncomp_seg_sz;
11508996SAlok.Aggarwal@Sun.COM 			}
11518996SAlok.Aggarwal@Sun.COM 
11525643Saalok 			/*
11535643Saalok 			 * Each of the segment index entries contains
11545643Saalok 			 * the starting block number for that segment.
11555643Saalok 			 * The number of compressed bytes in a segment
11565643Saalok 			 * is thus the difference between the starting
11575643Saalok 			 * block number of this segment and the starting
11585643Saalok 			 * block number of the next segment.
11595643Saalok 			 */
11608996SAlok.Aggarwal@Sun.COM 			cmpbytes = lsp->ls_comp_seg_index[i + 1] -
11618996SAlok.Aggarwal@Sun.COM 			    lsp->ls_comp_seg_index[i];
11625643Saalok 
11635643Saalok 			/*
11645643Saalok 			 * The first byte in a compressed segment is a flag
11655643Saalok 			 * that indicates whether this segment is compressed
11665643Saalok 			 * at all
11675643Saalok 			 */
11685643Saalok 			if (*cmpbuf == UNCOMPRESSED) {
11695643Saalok 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
11705643Saalok 				    (cmpbytes - SEGHDR));
11715643Saalok 			} else {
11725643Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
11735643Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
11745643Saalok 				    &seglen, li->l_level) != 0) {
11755643Saalok 					error = EIO;
11765643Saalok 					goto done;
11775643Saalok 				}
11785643Saalok 			}
11795643Saalok 
11809048Sjrgn.keil@googlemail.com 			uncompressed_seg_index = i;
11819048Sjrgn.keil@googlemail.com 
11825643Saalok 			/*
11835643Saalok 			 * Determine how much uncompressed data we
11845643Saalok 			 * have to copy and copy it
11855643Saalok 			 */
11865643Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
11878996SAlok.Aggarwal@Sun.COM 			if (i == eblkno)
11888996SAlok.Aggarwal@Sun.COM 				xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
11895643Saalok 
11905643Saalok 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
11915643Saalok 
11925643Saalok 			cmpbuf += cmpbytes;
11930Sstevel@tonic-gate 			bufaddr += xfersize;
11945643Saalok 			bp->b_resid -= xfersize;
11955643Saalok 			sblkoff = 0;
11965643Saalok 
11975643Saalok 			if (bp->b_resid == 0)
11985643Saalok 				break;
11995643Saalok 		}
12009048Sjrgn.keil@googlemail.com 
12019048Sjrgn.keil@googlemail.com 		/*
12029048Sjrgn.keil@googlemail.com 		 * Add the data for the last decopressed segment to
12039048Sjrgn.keil@googlemail.com 		 * the cache.
12049048Sjrgn.keil@googlemail.com 		 *
12059048Sjrgn.keil@googlemail.com 		 * In case the uncompressed segment data was added to (and
12069048Sjrgn.keil@googlemail.com 		 * is referenced by) the cache, make sure we don't free it
12079048Sjrgn.keil@googlemail.com 		 * here.
12089048Sjrgn.keil@googlemail.com 		 */
12099048Sjrgn.keil@googlemail.com 		mutex_enter(&lsp->ls_comp_cache_lock);
12109048Sjrgn.keil@googlemail.com 		if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
12119048Sjrgn.keil@googlemail.com 		    uncompressed_seg)) != NULL) {
12129048Sjrgn.keil@googlemail.com 			uncompressed_seg = NULL;
12139048Sjrgn.keil@googlemail.com 		}
12149048Sjrgn.keil@googlemail.com 		mutex_exit(&lsp->ls_comp_cache_lock);
12159048Sjrgn.keil@googlemail.com 
12168313SDina.Nimeh@Sun.Com done:
12178313SDina.Nimeh@Sun.Com 		if (compressed_seg != NULL)
12188313SDina.Nimeh@Sun.Com 			kmem_free(compressed_seg, comp_data_sz);
12198313SDina.Nimeh@Sun.Com 		if (uncompressed_seg != NULL)
12208313SDina.Nimeh@Sun.Com 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
12218313SDina.Nimeh@Sun.Com 	} /* end of handling compressed files */
12220Sstevel@tonic-gate 
1223*12368SFrank.Batschulat@Sun.COM 	if ((error == 0) && (syncflag != 0))
1224*12368SFrank.Batschulat@Sun.COM 		error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
1225*12368SFrank.Batschulat@Sun.COM 
12268313SDina.Nimeh@Sun.Com errout:
12278313SDina.Nimeh@Sun.Com 	if (bufinited && lsp->ls_kstat) {
12280Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
12290Sstevel@tonic-gate 		kstat_io_t *kioptr;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
12320Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
12330Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
12340Sstevel@tonic-gate 			kioptr->nread += n_done;
12350Sstevel@tonic-gate 			kioptr->reads++;
12360Sstevel@tonic-gate 		} else {
12370Sstevel@tonic-gate 			kioptr->nwritten += n_done;
12380Sstevel@tonic-gate 			kioptr->writes++;
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
12410Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
12420Sstevel@tonic-gate 	}
12434451Seschrock 
12444451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
12454451Seschrock 	if (--lsp->ls_vp_iocount == 0)
12464451Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
12474451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
12484451Seschrock 
12490Sstevel@tonic-gate 	bioerror(bp, error);
12500Sstevel@tonic-gate 	biodone(bp);
12510Sstevel@tonic-gate }
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate static int
12540Sstevel@tonic-gate lofi_strategy(struct buf *bp)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	struct lofi_state *lsp;
12570Sstevel@tonic-gate 	offset_t	offset;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/*
12600Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
12610Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
12620Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
12630Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
12640Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
12650Sstevel@tonic-gate 	 *
12660Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
12670Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
12680Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
12718313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
12728313SDina.Nimeh@Sun.Com 		bioerror(bp, ENXIO);
12738313SDina.Nimeh@Sun.Com 		biodone(bp);
12748313SDina.Nimeh@Sun.Com 		return (0);
12758313SDina.Nimeh@Sun.Com 	}
12768313SDina.Nimeh@Sun.Com 
12774451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
12784451Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
12794451Seschrock 		bioerror(bp, EIO);
12804451Seschrock 		biodone(bp);
12814451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12824451Seschrock 		return (0);
12834451Seschrock 	}
12844451Seschrock 
12850Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
12868313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
12878313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
12888313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
12898313SDina.Nimeh@Sun.Com 	}
12900Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
12910Sstevel@tonic-gate 		/* EOF */
12920Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
12930Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
12940Sstevel@tonic-gate 			bioerror(bp, 0);
12950Sstevel@tonic-gate 		} else {
12960Sstevel@tonic-gate 			/* writes should fail */
12970Sstevel@tonic-gate 			bioerror(bp, ENXIO);
12980Sstevel@tonic-gate 		}
12990Sstevel@tonic-gate 		biodone(bp);
13004451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
13010Sstevel@tonic-gate 		return (0);
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
13040Sstevel@tonic-gate 		bioerror(bp, ENXIO);
13050Sstevel@tonic-gate 		biodone(bp);
13064451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
13070Sstevel@tonic-gate 		return (0);
13080Sstevel@tonic-gate 	}
13094451Seschrock 	lsp->ls_vp_iocount++;
13104451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
13114451Seschrock 
13120Sstevel@tonic-gate 	if (lsp->ls_kstat) {
13130Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
13140Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
13150Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
13180Sstevel@tonic-gate 	return (0);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate /*ARGSUSED2*/
13220Sstevel@tonic-gate static int
13230Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
13240Sstevel@tonic-gate {
13250Sstevel@tonic-gate 	if (getminor(dev) == 0)
13260Sstevel@tonic-gate 		return (EINVAL);
13278313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
13280Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate /*ARGSUSED2*/
13320Sstevel@tonic-gate static int
13330Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
13340Sstevel@tonic-gate {
13350Sstevel@tonic-gate 	if (getminor(dev) == 0)
13360Sstevel@tonic-gate 		return (EINVAL);
13378313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
13380Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate /*ARGSUSED2*/
13420Sstevel@tonic-gate static int
13430Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate 	if (getminor(dev) == 0)
13460Sstevel@tonic-gate 		return (EINVAL);
13478313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
13480Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
13490Sstevel@tonic-gate }
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate /*ARGSUSED2*/
13520Sstevel@tonic-gate static int
13530Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
13540Sstevel@tonic-gate {
13550Sstevel@tonic-gate 	if (getminor(dev) == 0)
13560Sstevel@tonic-gate 		return (EINVAL);
13578313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
13580Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate /*ARGSUSED*/
13620Sstevel@tonic-gate static int
13630Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
13640Sstevel@tonic-gate {
13650Sstevel@tonic-gate 	switch (infocmd) {
13660Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
13670Sstevel@tonic-gate 		*result = lofi_dip;
13680Sstevel@tonic-gate 		return (DDI_SUCCESS);
13690Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
13700Sstevel@tonic-gate 		*result = 0;
13710Sstevel@tonic-gate 		return (DDI_SUCCESS);
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 	return (DDI_FAILURE);
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate static int
13770Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
13780Sstevel@tonic-gate {
13790Sstevel@tonic-gate 	int	error;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
13820Sstevel@tonic-gate 		return (DDI_FAILURE);
13830Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
13840Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
13850Sstevel@tonic-gate 		return (DDI_FAILURE);
13860Sstevel@tonic-gate 	}
13870Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
13880Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
13890Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
13900Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
13910Sstevel@tonic-gate 		return (DDI_FAILURE);
13920Sstevel@tonic-gate 	}
13935084Sjohnlev 	/* driver handles kernel-issued IOCTLs */
13945084Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
13955084Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
13965084Sjohnlev 		ddi_remove_minor_node(dip, NULL);
13975084Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
13985084Sjohnlev 		return (DDI_FAILURE);
13995084Sjohnlev 	}
14000Sstevel@tonic-gate 	lofi_dip = dip;
14010Sstevel@tonic-gate 	ddi_report_dev(dip);
14020Sstevel@tonic-gate 	return (DDI_SUCCESS);
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate static int
14060Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
14090Sstevel@tonic-gate 		return (DDI_FAILURE);
14100Sstevel@tonic-gate 	if (lofi_busy())
14110Sstevel@tonic-gate 		return (DDI_FAILURE);
14120Sstevel@tonic-gate 	lofi_dip = NULL;
14130Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
14145084Sjohnlev 	ddi_prop_remove_all(dip);
14150Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
14160Sstevel@tonic-gate 	return (DDI_SUCCESS);
14170Sstevel@tonic-gate }
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate /*
14208313SDina.Nimeh@Sun.Com  * With addition of encryption, be careful that encryption key is wiped before
14218313SDina.Nimeh@Sun.Com  * kernel memory structures are freed, and also that key is not accidentally
14228313SDina.Nimeh@Sun.Com  * passed out into userland structures.
14238313SDina.Nimeh@Sun.Com  */
14248313SDina.Nimeh@Sun.Com static void
14258313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip)
14268313SDina.Nimeh@Sun.Com {
14278313SDina.Nimeh@Sun.Com 	/* Make sure this encryption key doesn't stick around */
14288313SDina.Nimeh@Sun.Com 	bzero(klip->li_key, sizeof (klip->li_key));
14298313SDina.Nimeh@Sun.Com 	kmem_free(klip, sizeof (struct lofi_ioctl));
14308313SDina.Nimeh@Sun.Com }
14318313SDina.Nimeh@Sun.Com 
14328313SDina.Nimeh@Sun.Com /*
14330Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
14340Sstevel@tonic-gate  * the lofi_ioctl structure.
14350Sstevel@tonic-gate  */
14360Sstevel@tonic-gate struct lofi_ioctl *
14371657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	struct lofi_ioctl *klip;
14400Sstevel@tonic-gate 	int	error;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
14431657Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
14440Sstevel@tonic-gate 	if (error) {
14458313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
14460Sstevel@tonic-gate 		return (NULL);
14470Sstevel@tonic-gate 	}
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
14508313SDina.Nimeh@Sun.Com 	klip->li_filename[MAXPATHLEN-1] = '\0';
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	/* validate minor number */
14530Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
14548313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
14558313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
14568313SDina.Nimeh@Sun.Com 		    lofi_max_files);
14570Sstevel@tonic-gate 		return (NULL);
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 	return (klip);
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate int
14631657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
14641657Sheppo 	int flag)
14650Sstevel@tonic-gate {
14660Sstevel@tonic-gate 	int	error;
14670Sstevel@tonic-gate 
14688313SDina.Nimeh@Sun.Com 	/*
14698313SDina.Nimeh@Sun.Com 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
14708313SDina.Nimeh@Sun.Com 	 * This ensures that an attacker can't trivially find the
14718313SDina.Nimeh@Sun.Com 	 * key for a mapping just by issuing the ioctl.
14728313SDina.Nimeh@Sun.Com 	 *
14738313SDina.Nimeh@Sun.Com 	 * It can still be found by poking around in kmem with mdb(1),
14748313SDina.Nimeh@Sun.Com 	 * but there is no point in making it easy when the info isn't
14758313SDina.Nimeh@Sun.Com 	 * of any use in this direction anyway.
14768313SDina.Nimeh@Sun.Com 	 *
14778313SDina.Nimeh@Sun.Com 	 * Either way we don't actually have the raw key stored in
14788313SDina.Nimeh@Sun.Com 	 * a form that we can get it anyway, since we just used it
14798313SDina.Nimeh@Sun.Com 	 * to create a ctx template and didn't keep "the original".
14808313SDina.Nimeh@Sun.Com 	 */
14811657Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
14820Sstevel@tonic-gate 	if (error)
14830Sstevel@tonic-gate 		return (EFAULT);
14840Sstevel@tonic-gate 	return (0);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate /*
14880Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
14890Sstevel@tonic-gate  */
14900Sstevel@tonic-gate static int
14910Sstevel@tonic-gate file_to_minor(char *filename)
14920Sstevel@tonic-gate {
14930Sstevel@tonic-gate 	minor_t	minor;
14940Sstevel@tonic-gate 	struct lofi_state *lsp;
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
14970Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
14980Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
14990Sstevel@tonic-gate 		if (lsp == NULL)
15000Sstevel@tonic-gate 			continue;
15010Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
15020Sstevel@tonic-gate 			return (minor);
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 	return (0);
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate /*
15080Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
15090Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
15100Sstevel@tonic-gate  */
15110Sstevel@tonic-gate static int
15120Sstevel@tonic-gate valid_filename(const char *filename)
15130Sstevel@tonic-gate {
15140Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
15150Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/* must be absolute path */
15180Sstevel@tonic-gate 	if (filename[0] != '/')
15190Sstevel@tonic-gate 		return (0);
15200Sstevel@tonic-gate 	/* must not be lofi */
15210Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
15220Sstevel@tonic-gate 		return (0);
15230Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
15240Sstevel@tonic-gate 		return (0);
15250Sstevel@tonic-gate 	return (1);
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate /*
15290Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
15300Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
15310Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
15320Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
15333517Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
15340Sstevel@tonic-gate  * have to support it.
15350Sstevel@tonic-gate  */
15360Sstevel@tonic-gate static void
15370Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
15380Sstevel@tonic-gate {
15398313SDina.Nimeh@Sun.Com 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
15408313SDina.Nimeh@Sun.Com 
15410Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
15440Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
15450Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
15460Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
15470Sstevel@tonic-gate 	 * a number between one and one).
15480Sstevel@tonic-gate 	 *
15490Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
15500Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
15510Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
15520Sstevel@tonic-gate 	 */
15538313SDina.Nimeh@Sun.Com 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
15548313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
15550Sstevel@tonic-gate 	else
15568313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
15570Sstevel@tonic-gate 	/* in case file file is < 100k */
15580Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
15590Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
15600Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
15610Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
15620Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
15630Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
15640Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
15650Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
15660Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
15670Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
15680Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
15690Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
15708313SDina.Nimeh@Sun.Com 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
15710Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
15720Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
15750Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
15760Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
15770Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
15788669SDina.Nimeh@Sun.COM 	(void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME,
15798669SDina.Nimeh@Sun.COM 	    sizeof (lsp->ls_vtoc.v_volume));
15800Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
15810Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
15820Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
15835643Saalok 
15845643Saalok 	/*
15855643Saalok 	 * A compressed file is read-only, other files can
15865643Saalok 	 * be read-write
15875643Saalok 	 */
15885643Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
15895643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
15905643Saalok 	} else {
15915643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
15925643Saalok 	}
15930Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
15940Sstevel@tonic-gate 	/*
15950Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
15960Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
15970Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
15980Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
15990Sstevel@tonic-gate 	 */
16000Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
16010Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
16040Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
16050Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
16060Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
16070Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
16080Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
16090Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
16100Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
16110Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
16120Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
16130Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
16140Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
16150Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
16160Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
16170Sstevel@tonic-gate 	/*
16180Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
16190Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
16200Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
16210Sstevel@tonic-gate 	 * maxcontig is 0.
16220Sstevel@tonic-gate 	 */
16230Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate /*
16275643Saalok  * map in a compressed file
16285643Saalok  *
16295643Saalok  * Read in the header and the index that follows.
16305643Saalok  *
16315643Saalok  * The header is as follows -
16325643Saalok  *
16335643Saalok  * Signature (name of the compression algorithm)
16345643Saalok  * Compression segment size (a multiple of 512)
16355643Saalok  * Number of index entries
16365643Saalok  * Size of the last block
16375643Saalok  * The array containing the index entries
16385643Saalok  *
16395643Saalok  * The header information is always stored in
16405643Saalok  * network byte order on disk.
16415643Saalok  */
16425643Saalok static int
16435643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
16445643Saalok {
16455643Saalok 	uint32_t index_sz, header_len, i;
16465643Saalok 	ssize_t	resid;
16475643Saalok 	enum uio_rw rw;
16485643Saalok 	char *tbuf = buf;
16495643Saalok 	int error;
16505643Saalok 
16515643Saalok 	/* The signature has already been read */
16525643Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
16535643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
16545643Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
16555643Saalok 
16565643Saalok 	/*
16575643Saalok 	 * The compressed segment size must be a power of 2
16585643Saalok 	 */
16599048Sjrgn.keil@googlemail.com 	if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
16609048Sjrgn.keil@googlemail.com 	    !ISP2(lsp->ls_uncomp_seg_sz))
16615643Saalok 		return (EINVAL);
16625643Saalok 
16635643Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
16645643Saalok 		;
16655643Saalok 
16665643Saalok 	lsp->ls_comp_seg_shift = i;
16675643Saalok 
16685643Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
16695643Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
16705643Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
16715643Saalok 
16725643Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
16735643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
16745643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
16755643Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
16765643Saalok 
16775643Saalok 	/*
16785643Saalok 	 * Compute the total size of the uncompressed data
16795643Saalok 	 * for use in fake_disk_geometry and other calculations.
16805643Saalok 	 * Disk geometry has to be faked with respect to the
16815643Saalok 	 * actual uncompressed data size rather than the
16825643Saalok 	 * compressed file size.
16835643Saalok 	 */
168410197Sdminer@opensolaris.org 	lsp->ls_vp_size =
168510197Sdminer@opensolaris.org 	    (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
16865643Saalok 	    + lsp->ls_uncomp_last_seg_sz;
16875643Saalok 
16885643Saalok 	/*
16898996SAlok.Aggarwal@Sun.COM 	 * Index size is rounded up to DEV_BSIZE for ease
16905643Saalok 	 * of segmapping
16915643Saalok 	 */
16925643Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
16935643Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
16945643Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
16955643Saalok 	    sizeof (lsp->ls_comp_index_sz) +
16965643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
16975643Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
16985643Saalok 
16995643Saalok 	index_sz += header_len;
17005643Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
17015643Saalok 
17025643Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
17035643Saalok 	lsp->ls_comp_index_data_sz = index_sz;
17045643Saalok 
17055643Saalok 	/*
17065643Saalok 	 * Read in the index -- this has a side-effect
17075643Saalok 	 * of reading in the header as well
17085643Saalok 	 */
17095643Saalok 	rw = UIO_READ;
17105643Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
17115643Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
17125643Saalok 
17135643Saalok 	if (error != 0)
17145643Saalok 		return (error);
17155643Saalok 
17165643Saalok 	/* Skip the header, this is where the index really begins */
17175643Saalok 	lsp->ls_comp_seg_index =
17185643Saalok 	    /*LINTED*/
17195643Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
17205643Saalok 
17215643Saalok 	/*
17225643Saalok 	 * Now recompute offsets in the index to account for
17235643Saalok 	 * the header length
17245643Saalok 	 */
17255643Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
17265643Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
17275643Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
17285643Saalok 	}
17295643Saalok 
17305643Saalok 	return (error);
17315643Saalok }
17325643Saalok 
17335643Saalok /*
17345643Saalok  * Check to see if the passed in signature is a valid
17358313SDina.Nimeh@Sun.Com  * one.  If it is valid, return the index into
17365643Saalok  * lofi_compress_table.
17375643Saalok  *
17385643Saalok  * Return -1 if it is invalid
17395643Saalok  */
17405643Saalok static int lofi_compress_select(char *signature)
17415643Saalok {
17425643Saalok 	int i;
17435643Saalok 
17445643Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
17455643Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
17465643Saalok 			return (i);
17475643Saalok 	}
17485643Saalok 
17495643Saalok 	return (-1);
17505643Saalok }
17515643Saalok 
17525643Saalok /*
17530Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
17540Sstevel@tonic-gate  */
17550Sstevel@tonic-gate static int
17560Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
17571657Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
17580Sstevel@tonic-gate {
17590Sstevel@tonic-gate 	minor_t	newminor;
17600Sstevel@tonic-gate 	struct lofi_state *lsp;
17610Sstevel@tonic-gate 	struct lofi_ioctl *klip;
17620Sstevel@tonic-gate 	int	error;
17630Sstevel@tonic-gate 	struct vnode *vp;
17640Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
17650Sstevel@tonic-gate 	int64_t	Size_prop_val;
17665643Saalok 	int	compress_index;
17670Sstevel@tonic-gate 	vattr_t	vattr;
17680Sstevel@tonic-gate 	int	flag;
17690Sstevel@tonic-gate 	enum vtype v_type;
17704451Seschrock 	int zalloced = 0;
17710Sstevel@tonic-gate 	dev_t	newdev;
17724451Seschrock 	char	namebuf[50];
17738313SDina.Nimeh@Sun.Com 	char	buf[DEV_BSIZE];
17748313SDina.Nimeh@Sun.Com 	char	crybuf[DEV_BSIZE];
17755643Saalok 	ssize_t	resid;
17768313SDina.Nimeh@Sun.Com 	boolean_t need_vn_close = B_FALSE;
17778313SDina.Nimeh@Sun.Com 	boolean_t keycopied = B_FALSE;
17788313SDina.Nimeh@Sun.Com 	boolean_t need_size_update = B_FALSE;
17790Sstevel@tonic-gate 
17801657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
17810Sstevel@tonic-gate 	if (klip == NULL)
17820Sstevel@tonic-gate 		return (EFAULT);
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
17870Sstevel@tonic-gate 		error = EINVAL;
17880Sstevel@tonic-gate 		goto out;
17890Sstevel@tonic-gate 	}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
17920Sstevel@tonic-gate 		error = EBUSY;
17930Sstevel@tonic-gate 		goto out;
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	if (pickminor) {
17970Sstevel@tonic-gate 		/* Find a free one */
17980Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
17990Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
18000Sstevel@tonic-gate 				break;
18010Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
18020Sstevel@tonic-gate 			error = EAGAIN;
18030Sstevel@tonic-gate 			goto out;
18040Sstevel@tonic-gate 		}
18050Sstevel@tonic-gate 	} else {
18060Sstevel@tonic-gate 		newminor = klip->li_minor;
18070Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
18080Sstevel@tonic-gate 			error = EEXIST;
18090Sstevel@tonic-gate 			goto out;
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	/* make sure it's valid */
18140Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
18150Sstevel@tonic-gate 	    NULLVPP, &vp);
18160Sstevel@tonic-gate 	if (error) {
18170Sstevel@tonic-gate 		goto out;
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 	v_type = vp->v_type;
18200Sstevel@tonic-gate 	VN_RELE(vp);
18210Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
18220Sstevel@tonic-gate 		error = EINVAL;
18230Sstevel@tonic-gate 		goto out;
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
18260Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
18270Sstevel@tonic-gate 	if (error) {
18280Sstevel@tonic-gate 		/* try read-only */
18290Sstevel@tonic-gate 		flag &= ~FWRITE;
18300Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
18310Sstevel@tonic-gate 		    &vp, 0, 0);
18320Sstevel@tonic-gate 		if (error) {
18330Sstevel@tonic-gate 			goto out;
18340Sstevel@tonic-gate 		}
18350Sstevel@tonic-gate 	}
18368313SDina.Nimeh@Sun.Com 	need_vn_close = B_TRUE;
18378313SDina.Nimeh@Sun.Com 
18380Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
18395331Samw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
18400Sstevel@tonic-gate 	if (error) {
18418313SDina.Nimeh@Sun.Com 		goto out;
18420Sstevel@tonic-gate 	}
18430Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
18440Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
18450Sstevel@tonic-gate 		error = EINVAL;
18468313SDina.Nimeh@Sun.Com 		goto out;
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
18490Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
18500Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
18510Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
18520Sstevel@tonic-gate 		error = EINVAL;
18538313SDina.Nimeh@Sun.Com 		goto out;
18540Sstevel@tonic-gate 	}
18550Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
18560Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
18570Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
18580Sstevel@tonic-gate 		error = EINVAL;
18590Sstevel@tonic-gate 		goto propout;
18600Sstevel@tonic-gate 	}
18610Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
18620Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
18630Sstevel@tonic-gate 		error = ENOMEM;
18640Sstevel@tonic-gate 		goto propout;
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 	zalloced = 1;
18670Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
18686883Sgd78059 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
18690Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
18700Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
18710Sstevel@tonic-gate 		error = ENXIO;
18720Sstevel@tonic-gate 		goto propout;
18730Sstevel@tonic-gate 	}
18740Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
18750Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
18760Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
18770Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
18780Sstevel@tonic-gate 		/* remove block node */
18790Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
18800Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
18810Sstevel@tonic-gate 		error = ENXIO;
18820Sstevel@tonic-gate 		goto propout;
18830Sstevel@tonic-gate 	}
18840Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
18850Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
18860Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
18870Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
18880Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
18890Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
18900Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
18910Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
18920Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
18930Sstevel@tonic-gate 	if (lsp->ls_kstat) {
18940Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
18950Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
18960Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
18970Sstevel@tonic-gate 	}
18984451Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
18994451Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
19004451Seschrock 
19019048Sjrgn.keil@googlemail.com 	list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
19029048Sjrgn.keil@googlemail.com 	    offsetof(struct lofi_comp_cache, lc_list));
19039048Sjrgn.keil@googlemail.com 	mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
19049048Sjrgn.keil@googlemail.com 
19050Sstevel@tonic-gate 	/*
19060Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
19070Sstevel@tonic-gate 	 * updated correctly.
19080Sstevel@tonic-gate 	 */
19090Sstevel@tonic-gate 	lsp->ls_openflag = flag;
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	/*
19120Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
19130Sstevel@tonic-gate 	 */
19140Sstevel@tonic-gate 	if (vp->v_type == VREG) {
19155331Samw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
19160Sstevel@tonic-gate 			lsp->ls_vp = vp;
19170Sstevel@tonic-gate 		} else {
19180Sstevel@tonic-gate 			/*
19190Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
19200Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
19210Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
19220Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
19230Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
19240Sstevel@tonic-gate 			 * the realvp so things behave as if we've
19250Sstevel@tonic-gate 			 * opened the realvp without any interaction
19260Sstevel@tonic-gate 			 * with lofs.
19270Sstevel@tonic-gate 			 */
19280Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
19290Sstevel@tonic-gate 			VN_RELE(vp);
19300Sstevel@tonic-gate 		}
19310Sstevel@tonic-gate 	} else {
19320Sstevel@tonic-gate 		lsp->ls_vp = vp;
19330Sstevel@tonic-gate 	}
19340Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
19350Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
19360Sstevel@tonic-gate 	if (rvalp)
19370Sstevel@tonic-gate 		*rvalp = (int)newminor;
19380Sstevel@tonic-gate 	klip->li_minor = newminor;
19390Sstevel@tonic-gate 
19405643Saalok 	/*
19418313SDina.Nimeh@Sun.Com 	 * Initialize crypto details for encrypted lofi
19425643Saalok 	 */
19438313SDina.Nimeh@Sun.Com 	if (klip->li_crypto_enabled) {
19448313SDina.Nimeh@Sun.Com 		int ret;
19458313SDina.Nimeh@Sun.Com 
19468313SDina.Nimeh@Sun.Com 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
19478313SDina.Nimeh@Sun.Com 
19488313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
19498313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
19508313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
19518313SDina.Nimeh@Sun.Com 			    klip->li_cipher, lsp->ls_filename);
19528313SDina.Nimeh@Sun.Com 			error = EINVAL;
19538313SDina.Nimeh@Sun.Com 			goto propout;
19548313SDina.Nimeh@Sun.Com 		}
19558313SDina.Nimeh@Sun.Com 
19568313SDina.Nimeh@Sun.Com 		/* this is just initialization here */
19578313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param = NULL;
19588313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param_len = 0;
19598313SDina.Nimeh@Sun.Com 
19608313SDina.Nimeh@Sun.Com 		lsp->ls_iv_type = klip->li_iv_type;
19618313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
19628313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
19638313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
19648313SDina.Nimeh@Sun.Com 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
19658313SDina.Nimeh@Sun.Com 			error = EINVAL;
19668313SDina.Nimeh@Sun.Com 			goto propout;
19678313SDina.Nimeh@Sun.Com 		}
19688313SDina.Nimeh@Sun.Com 
19698313SDina.Nimeh@Sun.Com 		/* iv mech must itself take a null iv */
19708313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param = NULL;
19718313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param_len = 0;
19728313SDina.Nimeh@Sun.Com 		lsp->ls_iv_len = klip->li_iv_len;
19738313SDina.Nimeh@Sun.Com 
19748313SDina.Nimeh@Sun.Com 		/*
19758313SDina.Nimeh@Sun.Com 		 * Create ctx using li_cipher & the raw li_key after checking
19768313SDina.Nimeh@Sun.Com 		 * that it isn't a weak key.
19778313SDina.Nimeh@Sun.Com 		 */
19788313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
19798313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = klip->li_key_len;
19808313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = kmem_alloc(
19818313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
19828313SDina.Nimeh@Sun.Com 		bcopy(klip->li_key, lsp->ls_key.ck_data,
19838313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
19848313SDina.Nimeh@Sun.Com 		keycopied = B_TRUE;
19858313SDina.Nimeh@Sun.Com 
19868313SDina.Nimeh@Sun.Com 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
19878313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS) {
19888313SDina.Nimeh@Sun.Com 			error = EINVAL;
19898313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "weak key check failed for cipher "
19908313SDina.Nimeh@Sun.Com 			    "%s on file %s (0x%x)", klip->li_cipher,
19918313SDina.Nimeh@Sun.Com 			    lsp->ls_filename, ret);
19928313SDina.Nimeh@Sun.Com 			goto propout;
19938313SDina.Nimeh@Sun.Com 		}
19948313SDina.Nimeh@Sun.Com 	}
19958313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
19968313SDina.Nimeh@Sun.Com 
19978313SDina.Nimeh@Sun.Com 	/*
19988313SDina.Nimeh@Sun.Com 	 * Read the file signature to check if it is compressed or encrypted.
19998313SDina.Nimeh@Sun.Com 	 * Crypto signature is in a different location; both areas should
20008313SDina.Nimeh@Sun.Com 	 * read to keep compression and encryption mutually exclusive.
20018313SDina.Nimeh@Sun.Com 	 */
20028313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
20038313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
20048313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
20058313SDina.Nimeh@Sun.Com 		if (error != 0)
20068313SDina.Nimeh@Sun.Com 			goto propout;
20078313SDina.Nimeh@Sun.Com 	}
20088313SDina.Nimeh@Sun.Com 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
20095643Saalok 	    0, RLIM64_INFINITY, kcred, &resid);
20105643Saalok 	if (error != 0)
20115643Saalok 		goto propout;
20125643Saalok 
20138313SDina.Nimeh@Sun.Com 	/* initialize these variables for all lofi files */
20145643Saalok 	lsp->ls_uncomp_seg_sz = 0;
20155643Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
20165643Saalok 	lsp->ls_comp_algorithm[0] = '\0';
20175643Saalok 
20188313SDina.Nimeh@Sun.Com 	/* encrypted lofi reads/writes shifted by crypto metadata size */
20198313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_offset = 0;
20208313SDina.Nimeh@Sun.Com 
20218313SDina.Nimeh@Sun.Com 	/* this is a compressed lofi */
20228313SDina.Nimeh@Sun.Com 	if ((compress_index = lofi_compress_select(buf)) != -1) {
20238313SDina.Nimeh@Sun.Com 
20248313SDina.Nimeh@Sun.Com 		/* compression and encryption are mutually exclusive */
20258313SDina.Nimeh@Sun.Com 		if (klip->li_crypto_enabled) {
20268313SDina.Nimeh@Sun.Com 			error = ENOTSUP;
20278313SDina.Nimeh@Sun.Com 			goto propout;
20288313SDina.Nimeh@Sun.Com 		}
20298313SDina.Nimeh@Sun.Com 
20308313SDina.Nimeh@Sun.Com 		/* initialize compression info for compressed lofi */
20315643Saalok 		lsp->ls_comp_algorithm_index = compress_index;
20325643Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
20335643Saalok 		    lofi_compress_table[compress_index].l_name,
20345643Saalok 		    sizeof (lsp->ls_comp_algorithm));
20358313SDina.Nimeh@Sun.Com 
20365643Saalok 		error = lofi_map_compressed_file(lsp, buf);
20375643Saalok 		if (error != 0)
20385643Saalok 			goto propout;
20398313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
20405643Saalok 
20418313SDina.Nimeh@Sun.Com 	/* this is an encrypted lofi */
20428313SDina.Nimeh@Sun.Com 	} else if (strncmp(crybuf, lofi_crypto_magic,
20438313SDina.Nimeh@Sun.Com 	    sizeof (lofi_crypto_magic)) == 0) {
20448313SDina.Nimeh@Sun.Com 
20458313SDina.Nimeh@Sun.Com 		char *marker = crybuf;
20468313SDina.Nimeh@Sun.Com 
20478313SDina.Nimeh@Sun.Com 		/*
20488313SDina.Nimeh@Sun.Com 		 * This is the case where the header in the lofi image is
20498313SDina.Nimeh@Sun.Com 		 * already initialized to indicate it is encrypted.
20508313SDina.Nimeh@Sun.Com 		 * There is another case (see below) where encryption is
20518313SDina.Nimeh@Sun.Com 		 * requested but the lofi image has never been used yet,
20528313SDina.Nimeh@Sun.Com 		 * so the header needs to be written with encryption magic.
20538313SDina.Nimeh@Sun.Com 		 */
20548313SDina.Nimeh@Sun.Com 
20558313SDina.Nimeh@Sun.Com 		/* indicate this must be an encrypted lofi due to magic */
20568313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = B_TRUE;
20578313SDina.Nimeh@Sun.Com 
20588313SDina.Nimeh@Sun.Com 		/*
20598313SDina.Nimeh@Sun.Com 		 * The encryption header information is laid out this way:
20608313SDina.Nimeh@Sun.Com 		 *	6 bytes:	hex "CFLOFI"
20618313SDina.Nimeh@Sun.Com 		 *	2 bytes:	version = 0 ... for now
20628313SDina.Nimeh@Sun.Com 		 *	96 bytes:	reserved1 (not implemented yet)
20638313SDina.Nimeh@Sun.Com 		 *	4 bytes:	data_sector = 2 ... for now
20648313SDina.Nimeh@Sun.Com 		 *	more...		not implemented yet
20658313SDina.Nimeh@Sun.Com 		 */
20668313SDina.Nimeh@Sun.Com 
20678313SDina.Nimeh@Sun.Com 		/* copy the magic */
20688313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.magic,
20698313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.magic));
20708313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.magic);
20718313SDina.Nimeh@Sun.Com 
20728313SDina.Nimeh@Sun.Com 		/* read the encryption version number */
20738313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.version),
20748313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.version));
20758313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
20768313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.version);
20778313SDina.Nimeh@Sun.Com 
20788313SDina.Nimeh@Sun.Com 		/* read a chunk of reserved data */
20798313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.reserved1,
20808313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.reserved1));
20818313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.reserved1);
20828313SDina.Nimeh@Sun.Com 
20838313SDina.Nimeh@Sun.Com 		/* read block number where encrypted data begins */
20848313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.data_sector),
20858313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.data_sector));
20868313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
20878313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.data_sector);
20888313SDina.Nimeh@Sun.Com 
20898313SDina.Nimeh@Sun.Com 		/* and ignore the rest until it is implemented */
20908313SDina.Nimeh@Sun.Com 
20918313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
20928313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
20938313SDina.Nimeh@Sun.Com 
20948313SDina.Nimeh@Sun.Com 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
20958313SDina.Nimeh@Sun.Com 	} else if (klip->li_crypto_enabled) {
20968313SDina.Nimeh@Sun.Com 
20978313SDina.Nimeh@Sun.Com 		/*
20988313SDina.Nimeh@Sun.Com 		 * This is the case where encryption was requested but the
20998313SDina.Nimeh@Sun.Com 		 * appears to be entirely blank where the encryption header
21008313SDina.Nimeh@Sun.Com 		 * would have been in the lofi image.  If it is blank,
21018313SDina.Nimeh@Sun.Com 		 * assume it is a brand new lofi image and initialize the
21028313SDina.Nimeh@Sun.Com 		 * header area with encryption magic and current version
21038313SDina.Nimeh@Sun.Com 		 * header data.  If it is not blank, that's an error.
21048313SDina.Nimeh@Sun.Com 		 */
21058313SDina.Nimeh@Sun.Com 		int	i;
21068313SDina.Nimeh@Sun.Com 		char	*marker;
21078313SDina.Nimeh@Sun.Com 		struct crypto_meta	chead;
21088313SDina.Nimeh@Sun.Com 
21098313SDina.Nimeh@Sun.Com 		for (i = 0; i < sizeof (struct crypto_meta); i++)
21108313SDina.Nimeh@Sun.Com 			if (crybuf[i] != '\0')
21118313SDina.Nimeh@Sun.Com 				break;
21128313SDina.Nimeh@Sun.Com 		if (i != sizeof (struct crypto_meta)) {
21138313SDina.Nimeh@Sun.Com 			error = EINVAL;
21148313SDina.Nimeh@Sun.Com 			goto propout;
21158313SDina.Nimeh@Sun.Com 		}
21168313SDina.Nimeh@Sun.Com 
21178313SDina.Nimeh@Sun.Com 		/* nothing there, initialize as encrypted lofi */
21188313SDina.Nimeh@Sun.Com 		marker = crybuf;
21198313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
21208313SDina.Nimeh@Sun.Com 		marker += sizeof (lofi_crypto_magic);
21218313SDina.Nimeh@Sun.Com 		chead.version = htons(LOFI_CRYPTO_VERSION);
21228313SDina.Nimeh@Sun.Com 		bcopy(&(chead.version), marker, sizeof (chead.version));
21238313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.version);
21248313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.reserved1);
21258313SDina.Nimeh@Sun.Com 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
21268313SDina.Nimeh@Sun.Com 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
21278313SDina.Nimeh@Sun.Com 
21288313SDina.Nimeh@Sun.Com 		/* write the header */
21298313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
21308313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
21318313SDina.Nimeh@Sun.Com 		if (error != 0)
21328313SDina.Nimeh@Sun.Com 			goto propout;
21338313SDina.Nimeh@Sun.Com 
21348313SDina.Nimeh@Sun.Com 		/* fix things up so it looks like we read this info */
21358313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
21368313SDina.Nimeh@Sun.Com 		    sizeof (lofi_crypto_magic));
21378313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
21388313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
21398313SDina.Nimeh@Sun.Com 
21408313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
21418313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
21428313SDina.Nimeh@Sun.Com 	}
21438313SDina.Nimeh@Sun.Com 
21448313SDina.Nimeh@Sun.Com 	/*
21458313SDina.Nimeh@Sun.Com 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
21468313SDina.Nimeh@Sun.Com 	 * for encrypted lofi, advertise that it is somewhat shorter
21478313SDina.Nimeh@Sun.Com 	 * due to embedded crypto metadata section
21488313SDina.Nimeh@Sun.Com 	 */
21498313SDina.Nimeh@Sun.Com 	if (need_size_update) {
21505643Saalok 		/* update DDI properties */
21518313SDina.Nimeh@Sun.Com 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
21525643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
21535643Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
21545643Saalok 			error = EINVAL;
21555643Saalok 			goto propout;
21565643Saalok 		}
21578313SDina.Nimeh@Sun.Com 		Nblocks_prop_val =
21588313SDina.Nimeh@Sun.Com 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
21595643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
21605643Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
21615643Saalok 			error = EINVAL;
21625643Saalok 			goto propout;
21635643Saalok 		}
21645643Saalok 	}
21655643Saalok 
21660Sstevel@tonic-gate 	fake_disk_geometry(lsp);
21670Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
21681657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21690Sstevel@tonic-gate 	free_lofi_ioctl(klip);
21700Sstevel@tonic-gate 	return (0);
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate propout:
21738313SDina.Nimeh@Sun.Com 	if (keycopied) {
21748313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
21758313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
21768313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
21778313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
21788313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
21798313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
21808313SDina.Nimeh@Sun.Com 	}
21818313SDina.Nimeh@Sun.Com 
21828313SDina.Nimeh@Sun.Com 	if (zalloced)
21838313SDina.Nimeh@Sun.Com 		ddi_soft_state_free(lofi_statep, newminor);
21848313SDina.Nimeh@Sun.Com 
21850Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
21860Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
21878313SDina.Nimeh@Sun.Com 
21880Sstevel@tonic-gate out:
21898313SDina.Nimeh@Sun.Com 	if (need_vn_close) {
21908313SDina.Nimeh@Sun.Com 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
21918313SDina.Nimeh@Sun.Com 		VN_RELE(vp);
21928313SDina.Nimeh@Sun.Com 	}
21938313SDina.Nimeh@Sun.Com 
21940Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
21950Sstevel@tonic-gate 	free_lofi_ioctl(klip);
21960Sstevel@tonic-gate 	return (error);
21970Sstevel@tonic-gate }
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate /*
22000Sstevel@tonic-gate  * unmap a file.
22010Sstevel@tonic-gate  */
22020Sstevel@tonic-gate static int
22030Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
22041657Sheppo     struct cred *credp, int ioctl_flag)
22050Sstevel@tonic-gate {
22060Sstevel@tonic-gate 	struct lofi_state *lsp;
22070Sstevel@tonic-gate 	struct lofi_ioctl *klip;
22080Sstevel@tonic-gate 	minor_t	minor;
22090Sstevel@tonic-gate 
22101657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
22110Sstevel@tonic-gate 	if (klip == NULL)
22120Sstevel@tonic-gate 		return (EFAULT);
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
22150Sstevel@tonic-gate 	if (byfilename) {
22160Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
22170Sstevel@tonic-gate 	} else {
22180Sstevel@tonic-gate 		minor = klip->li_minor;
22190Sstevel@tonic-gate 	}
22200Sstevel@tonic-gate 	if (minor == 0) {
22210Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22220Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22230Sstevel@tonic-gate 		return (ENXIO);
22240Sstevel@tonic-gate 	}
22250Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
22264451Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
22270Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22280Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22290Sstevel@tonic-gate 		return (ENXIO);
22300Sstevel@tonic-gate 	}
22314451Seschrock 
22326734Sjohnlev 	/*
22336734Sjohnlev 	 * If it's still held open, we'll do one of three things:
22346734Sjohnlev 	 *
22356734Sjohnlev 	 * If no flag is set, just return EBUSY.
22366734Sjohnlev 	 *
22376734Sjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
22386734Sjohnlev 	 * the last user finishes.
22396734Sjohnlev 	 *
22406734Sjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
22416734Sjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
22426734Sjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
22436734Sjohnlev 	 * device will be cleaned up appropriately.
22446734Sjohnlev 	 *
22456734Sjohnlev 	 * This is complicated by the fact that we may have outstanding
22466734Sjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
22478313SDina.Nimeh@Sun.Com 	 * I/O, we keep a count of the number of outstanding I/O requests
22488313SDina.Nimeh@Sun.Com 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
22498313SDina.Nimeh@Sun.Com 	 * should be dispatched (ls_vp_closereq).
22508313SDina.Nimeh@Sun.Com 	 *
22516734Sjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
22526734Sjohnlev 	 * and then close the underlying vnode.
22536734Sjohnlev 	 */
22540Sstevel@tonic-gate 	if (is_opened(lsp)) {
22554451Seschrock 		if (klip->li_force) {
22564451Seschrock 			mutex_enter(&lsp->ls_vp_lock);
22574451Seschrock 			lsp->ls_vp_closereq = B_TRUE;
225811041SEric.Taylor@Sun.COM 			/* wake up any threads waiting on dkiocstate */
225911041SEric.Taylor@Sun.COM 			cv_broadcast(&lsp->ls_vp_cv);
22604451Seschrock 			while (lsp->ls_vp_iocount > 0)
22614451Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
22624451Seschrock 			mutex_exit(&lsp->ls_vp_lock);
226311041SEric.Taylor@Sun.COM 			lofi_free_handle(dev, minor, lsp, credp);
22648313SDina.Nimeh@Sun.Com 
22658313SDina.Nimeh@Sun.Com 			klip->li_minor = minor;
22664451Seschrock 			mutex_exit(&lofi_lock);
22674451Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22684451Seschrock 			free_lofi_ioctl(klip);
22694451Seschrock 			return (0);
22706734Sjohnlev 		} else if (klip->li_cleanup) {
22716734Sjohnlev 			lsp->ls_cleanup = 1;
22726734Sjohnlev 			mutex_exit(&lofi_lock);
22736734Sjohnlev 			free_lofi_ioctl(klip);
22746734Sjohnlev 			return (0);
22754451Seschrock 		}
22766734Sjohnlev 
22770Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
22780Sstevel@tonic-gate 		free_lofi_ioctl(klip);
22790Sstevel@tonic-gate 		return (EBUSY);
22800Sstevel@tonic-gate 	}
22810Sstevel@tonic-gate 
22824451Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	klip->li_minor = minor;
22850Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
22861657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
22870Sstevel@tonic-gate 	free_lofi_ioctl(klip);
22880Sstevel@tonic-gate 	return (0);
22890Sstevel@tonic-gate }
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate /*
22920Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
22930Sstevel@tonic-gate  * the name.
22940Sstevel@tonic-gate  */
22954451Seschrock /*ARGSUSED*/
22960Sstevel@tonic-gate static int
22970Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
22981657Sheppo     struct cred *credp, int ioctl_flag)
22990Sstevel@tonic-gate {
23000Sstevel@tonic-gate 	struct lofi_state *lsp;
23010Sstevel@tonic-gate 	struct lofi_ioctl *klip;
23020Sstevel@tonic-gate 	int	error;
23030Sstevel@tonic-gate 	minor_t	minor;
23040Sstevel@tonic-gate 
23051657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
23060Sstevel@tonic-gate 	if (klip == NULL)
23070Sstevel@tonic-gate 		return (EFAULT);
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 	switch (which) {
23100Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
23110Sstevel@tonic-gate 		minor = klip->li_minor;
23120Sstevel@tonic-gate 		if (minor == 0) {
23130Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23140Sstevel@tonic-gate 			return (EINVAL);
23150Sstevel@tonic-gate 		}
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23180Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
23190Sstevel@tonic-gate 		if (lsp == NULL) {
23200Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
23210Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23220Sstevel@tonic-gate 			return (ENXIO);
23230Sstevel@tonic-gate 		}
23240Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
23255643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
23265643Saalok 		    sizeof (klip->li_algorithm));
23278313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
23280Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
23291657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23300Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23310Sstevel@tonic-gate 		return (error);
23320Sstevel@tonic-gate 	case LOFI_GET_MINOR:
23330Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
23340Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
23358313SDina.Nimeh@Sun.Com 		/* caller should not depend on klip->li_crypto_enabled here */
23360Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
23370Sstevel@tonic-gate 		if (klip->li_minor == 0) {
23380Sstevel@tonic-gate 			free_lofi_ioctl(klip);
23390Sstevel@tonic-gate 			return (ENOENT);
23400Sstevel@tonic-gate 		}
23411657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23420Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23430Sstevel@tonic-gate 		return (error);
23445643Saalok 	case LOFI_CHECK_COMPRESSED:
23455643Saalok 		mutex_enter(&lofi_lock);
23465643Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
23475643Saalok 		mutex_exit(&lofi_lock);
23485643Saalok 		if (klip->li_minor == 0) {
23495643Saalok 			free_lofi_ioctl(klip);
23505643Saalok 			return (ENOENT);
23515643Saalok 		}
23525643Saalok 		mutex_enter(&lofi_lock);
23535643Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
23545643Saalok 		if (lsp == NULL) {
23555643Saalok 			mutex_exit(&lofi_lock);
23565643Saalok 			free_lofi_ioctl(klip);
23575643Saalok 			return (ENXIO);
23585643Saalok 		}
23595643Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
23605643Saalok 
23615643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
23625643Saalok 		    sizeof (klip->li_algorithm));
23635643Saalok 		mutex_exit(&lofi_lock);
23645643Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23655643Saalok 		free_lofi_ioctl(klip);
23665643Saalok 		return (error);
23670Sstevel@tonic-gate 	default:
23680Sstevel@tonic-gate 		free_lofi_ioctl(klip);
23690Sstevel@tonic-gate 		return (EINVAL);
23700Sstevel@tonic-gate 	}
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate static int
23750Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
23760Sstevel@tonic-gate     int *rvalp)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	int	error;
23790Sstevel@tonic-gate 	enum dkio_state dkstate;
23800Sstevel@tonic-gate 	struct lofi_state *lsp;
23810Sstevel@tonic-gate 	minor_t	minor;
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	minor = getminor(dev);
23840Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
23850Sstevel@tonic-gate 	if (minor == 0) {
23860Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 		/*
23890Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
23900Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
23910Sstevel@tonic-gate 		 * long as they can open it read-only.
23920Sstevel@tonic-gate 		 */
23930Sstevel@tonic-gate 		switch (cmd) {
23940Sstevel@tonic-gate 		case LOFI_MAP_FILE:
23950Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
23960Sstevel@tonic-gate 				return (EPERM);
23971657Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
23980Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
23990Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24000Sstevel@tonic-gate 				return (EPERM);
24011657Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
24020Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
24030Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24040Sstevel@tonic-gate 				return (EPERM);
24051657Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
24060Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
24070Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
24080Sstevel@tonic-gate 				return (EPERM);
24091657Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
24100Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
24110Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
24121657Sheppo 			    credp, flag));
24130Sstevel@tonic-gate 		case LOFI_GET_MINOR:
24140Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
24151657Sheppo 			    credp, flag));
24160Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
24171657Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
24181657Sheppo 			    sizeof (lofi_max_files), flag);
24190Sstevel@tonic-gate 			if (error)
24200Sstevel@tonic-gate 				return (EFAULT);
24210Sstevel@tonic-gate 			return (0);
24225643Saalok 		case LOFI_CHECK_COMPRESSED:
24235643Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
24245643Saalok 			    credp, flag));
24250Sstevel@tonic-gate 		default:
24260Sstevel@tonic-gate 			break;
24270Sstevel@tonic-gate 		}
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 
243011041SEric.Taylor@Sun.COM 	mutex_enter(&lofi_lock);
24310Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
243211041SEric.Taylor@Sun.COM 	if (lsp == NULL || lsp->ls_vp_closereq) {
243311041SEric.Taylor@Sun.COM 		mutex_exit(&lofi_lock);
24340Sstevel@tonic-gate 		return (ENXIO);
243511041SEric.Taylor@Sun.COM 	}
243611041SEric.Taylor@Sun.COM 	mutex_exit(&lofi_lock);
24370Sstevel@tonic-gate 
24384451Seschrock 	/*
24394451Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
24404451Seschrock 	 * EIO as if the device was no longer present.
24414451Seschrock 	 */
24424451Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
24434451Seschrock 		return (EIO);
24444451Seschrock 
24450Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
24460Sstevel@tonic-gate 	switch (cmd) {
24470Sstevel@tonic-gate 	case DKIOCGVTOC:
24480Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
24490Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
24500Sstevel@tonic-gate 			struct vtoc32 vtoc32;
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
24530Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
24540Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
24550Sstevel@tonic-gate 				return (EFAULT);
24568719SDina.Nimeh@Sun.COM 			break;
24570Sstevel@tonic-gate 			}
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 		case DDI_MODEL_NONE:
24600Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
24610Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
24620Sstevel@tonic-gate 				return (EFAULT);
24630Sstevel@tonic-gate 			break;
24640Sstevel@tonic-gate 		}
24650Sstevel@tonic-gate 		return (0);
24660Sstevel@tonic-gate 	case DKIOCINFO:
24671657Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
24681657Sheppo 		    sizeof (struct dk_cinfo), flag);
24690Sstevel@tonic-gate 		if (error)
24700Sstevel@tonic-gate 			return (EFAULT);
24710Sstevel@tonic-gate 		return (0);
24720Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
24730Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
24740Sstevel@tonic-gate 	case DKIOCGGEOM:
24751657Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
24761657Sheppo 		    sizeof (struct dk_geom), flag);
24770Sstevel@tonic-gate 		if (error)
24780Sstevel@tonic-gate 			return (EFAULT);
24790Sstevel@tonic-gate 		return (0);
24800Sstevel@tonic-gate 	case DKIOCSTATE:
24814451Seschrock 		/*
24824451Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
24834451Seschrock 		 * a device is forcefully unmapped, then the device transitions
24844451Seschrock 		 * to the DKIO_DEV_GONE state.
24854451Seschrock 		 */
24864451Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
24874451Seschrock 		    flag) != 0)
24884451Seschrock 			return (EFAULT);
24894451Seschrock 
24904451Seschrock 		mutex_enter(&lsp->ls_vp_lock);
249111041SEric.Taylor@Sun.COM 		lsp->ls_vp_iocount++;
249211041SEric.Taylor@Sun.COM 		while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
249311041SEric.Taylor@Sun.COM 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
249411041SEric.Taylor@Sun.COM 		    !lsp->ls_vp_closereq) {
24954451Seschrock 			/*
24964451Seschrock 			 * By virtue of having the device open, we know that
24974451Seschrock 			 * 'lsp' will remain valid when we return.
24984451Seschrock 			 */
24994451Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
25004451Seschrock 			    &lsp->ls_vp_lock)) {
250111041SEric.Taylor@Sun.COM 				lsp->ls_vp_iocount--;
250211041SEric.Taylor@Sun.COM 				cv_broadcast(&lsp->ls_vp_cv);
25034451Seschrock 				mutex_exit(&lsp->ls_vp_lock);
25044451Seschrock 				return (EINTR);
25054451Seschrock 			}
25064451Seschrock 		}
25074451Seschrock 
250811041SEric.Taylor@Sun.COM 		dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ?
250911041SEric.Taylor@Sun.COM 		    DKIO_INSERTED : DKIO_DEV_GONE);
251011041SEric.Taylor@Sun.COM 		lsp->ls_vp_iocount--;
251111041SEric.Taylor@Sun.COM 		cv_broadcast(&lsp->ls_vp_cv);
25124451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
25134451Seschrock 
25144451Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
25154451Seschrock 		    sizeof (dkstate), flag) != 0)
25160Sstevel@tonic-gate 			return (EFAULT);
25170Sstevel@tonic-gate 		return (0);
25180Sstevel@tonic-gate 	default:
25190Sstevel@tonic-gate 		return (ENOTTY);
25200Sstevel@tonic-gate 	}
25210Sstevel@tonic-gate }
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
25240Sstevel@tonic-gate 	lofi_open,		/* open */
25250Sstevel@tonic-gate 	lofi_close,		/* close */
25260Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
25270Sstevel@tonic-gate 	nodev,			/* print */
25280Sstevel@tonic-gate 	nodev,			/* dump */
25290Sstevel@tonic-gate 	lofi_read,		/* read */
25300Sstevel@tonic-gate 	lofi_write,		/* write */
25310Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
25320Sstevel@tonic-gate 	nodev,			/* devmap */
25330Sstevel@tonic-gate 	nodev,			/* mmap */
25340Sstevel@tonic-gate 	nodev,			/* segmap */
25350Sstevel@tonic-gate 	nochpoll,		/* poll */
25360Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
25370Sstevel@tonic-gate 	0,			/* streamtab  */
25380Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
25390Sstevel@tonic-gate 	CB_REV,
25400Sstevel@tonic-gate 	lofi_aread,
25410Sstevel@tonic-gate 	lofi_awrite
25420Sstevel@tonic-gate };
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate static struct dev_ops lofi_ops = {
25450Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
25460Sstevel@tonic-gate 	0,			/* refcnt  */
25470Sstevel@tonic-gate 	lofi_info,		/* info */
25480Sstevel@tonic-gate 	nulldev,		/* identify */
25490Sstevel@tonic-gate 	nulldev,		/* probe */
25500Sstevel@tonic-gate 	lofi_attach,		/* attach */
25510Sstevel@tonic-gate 	lofi_detach,		/* detach */
25520Sstevel@tonic-gate 	nodev,			/* reset */
25530Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
25547656SSherry.Moore@Sun.COM 	NULL,			/* no bus operations */
25557656SSherry.Moore@Sun.COM 	NULL,			/* power */
25568313SDina.Nimeh@Sun.Com 	ddi_quiesce_not_needed,	/* quiesce */
25570Sstevel@tonic-gate };
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate static struct modldrv modldrv = {
25600Sstevel@tonic-gate 	&mod_driverops,
25617656SSherry.Moore@Sun.COM 	"loopback file driver",
25620Sstevel@tonic-gate 	&lofi_ops,
25630Sstevel@tonic-gate };
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate static struct modlinkage modlinkage = {
25660Sstevel@tonic-gate 	MODREV_1,
25670Sstevel@tonic-gate 	&modldrv,
25680Sstevel@tonic-gate 	NULL
25690Sstevel@tonic-gate };
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate int
25720Sstevel@tonic-gate _init(void)
25730Sstevel@tonic-gate {
25740Sstevel@tonic-gate 	int error;
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
25770Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
25780Sstevel@tonic-gate 	if (error)
25790Sstevel@tonic-gate 		return (error);
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
25820Sstevel@tonic-gate 	error = mod_install(&modlinkage);
25830Sstevel@tonic-gate 	if (error) {
25840Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
25850Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	return (error);
25890Sstevel@tonic-gate }
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate int
25920Sstevel@tonic-gate _fini(void)
25930Sstevel@tonic-gate {
25940Sstevel@tonic-gate 	int	error;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	if (lofi_busy())
25970Sstevel@tonic-gate 		return (EBUSY);
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
26000Sstevel@tonic-gate 	if (error)
26010Sstevel@tonic-gate 		return (error);
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
26040Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	return (error);
26070Sstevel@tonic-gate }
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate int
26100Sstevel@tonic-gate _info(struct modinfo *modinfop)
26110Sstevel@tonic-gate {
26120Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
26130Sstevel@tonic-gate }
2614