xref: /onnv-gate/usr/src/uts/common/io/lofi.c (revision 8669:85d80867103f)
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*8669SDina.Nimeh@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
280Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
290Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
300Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
310Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
320Sstevel@tonic-gate  * mounting images of filesystems.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
350Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
360Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
370Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
380Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
390Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
400Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
410Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
420Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
430Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
460Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
470Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
480Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
490Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
500Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
530Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
540Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
550Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
560Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
570Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
580Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
590Sstevel@tonic-gate  *
604451Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
614451Seschrock  * support simulation of hotplug events, an optional force flag is provided.
624451Seschrock  * If a lofi device is open when a force detach is requested, then the
634451Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
644451Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
654451Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
664451Seschrock  * detached but not removed.
674451Seschrock  *
680Sstevel@tonic-gate  * Known problems:
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
710Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
720Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
730Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
760Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
770Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
780Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
810Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
820Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
830Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
840Sstevel@tonic-gate  *	do. Though it would still be silly.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * Interesting things to do:
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
910Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
928313SDina.Nimeh@Sun.Com  *	need to fake a geometry, however, it may be relevant if you're replacing
938313SDina.Nimeh@Sun.Com  *	metadisk, or using lofi to get crypto.
948313SDina.Nimeh@Sun.Com  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
958313SDina.Nimeh@Sun.Com  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
968313SDina.Nimeh@Sun.Com  *	In fact this even makes sense if you have lofi "above" metadisk.
970Sstevel@tonic-gate  *
988313SDina.Nimeh@Sun.Com  * Encryption:
998313SDina.Nimeh@Sun.Com  *	Each lofi device can have its own symmetric key and cipher.
1008313SDina.Nimeh@Sun.Com  *	They are passed to us by lofiadm(1m) in the correct format for use
1018313SDina.Nimeh@Sun.Com  *	with the misc/kcf crypto_* routines.
1028313SDina.Nimeh@Sun.Com  *
1038313SDina.Nimeh@Sun.Com  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
1048313SDina.Nimeh@Sun.Com  *	on the "master" key held in the lsp and the block number of the buffer.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #include <sys/types.h>
1085643Saalok #include <netinet/in.h>
1090Sstevel@tonic-gate #include <sys/sysmacros.h>
1100Sstevel@tonic-gate #include <sys/uio.h>
1110Sstevel@tonic-gate #include <sys/kmem.h>
1120Sstevel@tonic-gate #include <sys/cred.h>
1130Sstevel@tonic-gate #include <sys/mman.h>
1140Sstevel@tonic-gate #include <sys/errno.h>
1150Sstevel@tonic-gate #include <sys/aio_req.h>
1160Sstevel@tonic-gate #include <sys/stat.h>
1170Sstevel@tonic-gate #include <sys/file.h>
1180Sstevel@tonic-gate #include <sys/modctl.h>
1190Sstevel@tonic-gate #include <sys/conf.h>
1200Sstevel@tonic-gate #include <sys/debug.h>
1210Sstevel@tonic-gate #include <sys/vnode.h>
1220Sstevel@tonic-gate #include <sys/lofi.h>
1230Sstevel@tonic-gate #include <sys/fcntl.h>
1240Sstevel@tonic-gate #include <sys/pathname.h>
1250Sstevel@tonic-gate #include <sys/filio.h>
1260Sstevel@tonic-gate #include <sys/fdio.h>
1270Sstevel@tonic-gate #include <sys/open.h>
1280Sstevel@tonic-gate #include <sys/disp.h>
1290Sstevel@tonic-gate #include <vm/seg_map.h>
1300Sstevel@tonic-gate #include <sys/ddi.h>
1310Sstevel@tonic-gate #include <sys/sunddi.h>
1325643Saalok #include <sys/zmod.h>
1338313SDina.Nimeh@Sun.Com #include <sys/crypto/common.h>
1348313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h>
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 
1845643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
1855643Saalok 	size_t *destlen, int level);
1865643Saalok 
1875643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
1885643Saalok 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
1895643Saalok 	{gzip_decompress,	NULL,	6,	"gzip-6"},
1905643Saalok 	{gzip_decompress,	NULL,	9,	"gzip-9"}
1915643Saalok };
1925643Saalok 
1930Sstevel@tonic-gate static int
1940Sstevel@tonic-gate lofi_busy(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	minor_t	minor;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/*
1990Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
2000Sstevel@tonic-gate 	 * help because the device isn't open.
2010Sstevel@tonic-gate 	 */
2020Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
2030Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
2040Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
2050Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2060Sstevel@tonic-gate 			return (EBUSY);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2100Sstevel@tonic-gate 	return (0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static int
2140Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2170Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static int
2210Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2240Sstevel@tonic-gate 	switch (otyp) {
2250Sstevel@tonic-gate 	case OTYP_CHR:
2260Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
2270Sstevel@tonic-gate 		break;
2280Sstevel@tonic-gate 	case OTYP_BLK:
2290Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
2300Sstevel@tonic-gate 		break;
2310Sstevel@tonic-gate 	case OTYP_LYR:
2320Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
2330Sstevel@tonic-gate 		break;
2340Sstevel@tonic-gate 	default:
2350Sstevel@tonic-gate 		return (-1);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	return (0);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static void
2410Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2440Sstevel@tonic-gate 	switch (otyp) {
2450Sstevel@tonic-gate 	case OTYP_CHR:
2460Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
2470Sstevel@tonic-gate 		break;
2480Sstevel@tonic-gate 	case OTYP_BLK:
2490Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
2500Sstevel@tonic-gate 		break;
2510Sstevel@tonic-gate 	case OTYP_LYR:
2520Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
2530Sstevel@tonic-gate 		break;
2540Sstevel@tonic-gate 	default:
2550Sstevel@tonic-gate 		break;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2594451Seschrock static void
2608313SDina.Nimeh@Sun.Com lofi_free_crypto(struct lofi_state *lsp)
2618313SDina.Nimeh@Sun.Com {
2628313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
2638313SDina.Nimeh@Sun.Com 
2648313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
2658313SDina.Nimeh@Sun.Com 		/*
2668313SDina.Nimeh@Sun.Com 		 * Clean up the crypto state so that it doesn't hang around
2678313SDina.Nimeh@Sun.Com 		 * in memory after we are done with it.
2688313SDina.Nimeh@Sun.Com 		 */
2698313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
2708313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
2718313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
2728313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
2738313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
2748313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
2758313SDina.Nimeh@Sun.Com 
2768313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != NULL) {
2778313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_mech.cm_param,
2788313SDina.Nimeh@Sun.Com 			    lsp->ls_mech.cm_param_len);
2798313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param = NULL;
2808313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param_len = 0;
2818313SDina.Nimeh@Sun.Com 		}
2828313SDina.Nimeh@Sun.Com 
2838313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_param != NULL) {
2848313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_iv_mech.cm_param,
2858313SDina.Nimeh@Sun.Com 			    lsp->ls_iv_mech.cm_param_len);
2868313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param = NULL;
2878313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param_len = 0;
2888313SDina.Nimeh@Sun.Com 		}
2898313SDina.Nimeh@Sun.Com 
2908313SDina.Nimeh@Sun.Com 		mutex_destroy(&lsp->ls_crypto_lock);
2918313SDina.Nimeh@Sun.Com 	}
2928313SDina.Nimeh@Sun.Com }
2938313SDina.Nimeh@Sun.Com 
2948313SDina.Nimeh@Sun.Com static void
2954451Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
2964451Seschrock     cred_t *credp)
2974451Seschrock {
2984451Seschrock 	dev_t	newdev;
2994451Seschrock 	char	namebuf[50];
3004451Seschrock 
3018313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
3028313SDina.Nimeh@Sun.Com 
3038313SDina.Nimeh@Sun.Com 	lofi_free_crypto(lsp);
3048313SDina.Nimeh@Sun.Com 
3054451Seschrock 	if (lsp->ls_vp) {
3065331Samw 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
3075331Samw 		    1, 0, credp, NULL);
3084451Seschrock 		VN_RELE(lsp->ls_vp);
3094451Seschrock 		lsp->ls_vp = NULL;
3104451Seschrock 	}
3114451Seschrock 
3124451Seschrock 	newdev = makedevice(getmajor(dev), minor);
3134451Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
3144451Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
3154451Seschrock 
3164451Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
3174451Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3184451Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
3194451Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
3204451Seschrock 
3214451Seschrock 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
3224451Seschrock 	taskq_destroy(lsp->ls_taskq);
3234451Seschrock 	if (lsp->ls_kstat) {
3244451Seschrock 		kstat_delete(lsp->ls_kstat);
3254451Seschrock 		mutex_destroy(&lsp->ls_kstat_lock);
3264451Seschrock 	}
3276791Saalok 
3286791Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
3296791Saalok 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
3306791Saalok 		lsp->ls_uncomp_seg_sz = 0;
3316791Saalok 	}
3324451Seschrock 	ddi_soft_state_free(lofi_statep, minor);
3334451Seschrock }
3344451Seschrock 
3354451Seschrock /*ARGSUSED*/
3360Sstevel@tonic-gate static int
3370Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	minor_t	minor;
3400Sstevel@tonic-gate 	struct lofi_state *lsp;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
3430Sstevel@tonic-gate 	minor = getminor(*devp);
3440Sstevel@tonic-gate 	if (minor == 0) {
3450Sstevel@tonic-gate 		/* master control device */
3460Sstevel@tonic-gate 		/* must be opened exclusively */
3470Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
3480Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
3490Sstevel@tonic-gate 			return (EINVAL);
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
3520Sstevel@tonic-gate 		if (lsp == NULL) {
3530Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
3540Sstevel@tonic-gate 			return (ENXIO);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 		if (is_opened(lsp)) {
3570Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
3580Sstevel@tonic-gate 			return (EBUSY);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
3610Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3620Sstevel@tonic-gate 		return (0);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
3660Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
3670Sstevel@tonic-gate 	if (lsp == NULL) {
3680Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3690Sstevel@tonic-gate 		return (EINVAL);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3724451Seschrock 	if (lsp->ls_vp == NULL) {
3734451Seschrock 		mutex_exit(&lofi_lock);
3744451Seschrock 		return (ENXIO);
3754451Seschrock 	}
3764451Seschrock 
3770Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
3780Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3790Sstevel@tonic-gate 		return (EINVAL);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
3830Sstevel@tonic-gate 	return (0);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3864451Seschrock /*ARGSUSED*/
3870Sstevel@tonic-gate static int
3880Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	minor_t	minor;
3910Sstevel@tonic-gate 	struct lofi_state *lsp;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
3940Sstevel@tonic-gate 	minor = getminor(dev);
3950Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
3960Sstevel@tonic-gate 	if (lsp == NULL) {
3970Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3980Sstevel@tonic-gate 		return (EINVAL);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	mark_closed(lsp, otyp);
4014451Seschrock 
4024451Seschrock 	/*
4036734Sjohnlev 	 * If we forcibly closed the underlying device (li_force), or
4046734Sjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
4056734Sjohnlev 	 * out of the door.
4064451Seschrock 	 */
4076734Sjohnlev 	if (minor != 0 && !is_opened(lsp) &&
4086734Sjohnlev 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
4094451Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
4106734Sjohnlev 
4110Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
4120Sstevel@tonic-gate 	return (0);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4158313SDina.Nimeh@Sun.Com /*
4168313SDina.Nimeh@Sun.Com  * Sets the mechanism's initialization vector (IV) if one is needed.
4178313SDina.Nimeh@Sun.Com  * The IV is computed from the data block number.  lsp->ls_mech is
4188313SDina.Nimeh@Sun.Com  * altered so that:
4198313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param_len is set to the IV len.
4208313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param is set to the IV.
4218313SDina.Nimeh@Sun.Com  */
4228313SDina.Nimeh@Sun.Com static int
4238313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
4248313SDina.Nimeh@Sun.Com {
4258313SDina.Nimeh@Sun.Com 	int	ret;
4268313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
4278313SDina.Nimeh@Sun.Com 	char	*iv;
4288313SDina.Nimeh@Sun.Com 	size_t	iv_len;
4298313SDina.Nimeh@Sun.Com 	size_t	min;
4308313SDina.Nimeh@Sun.Com 	void	*data;
4318313SDina.Nimeh@Sun.Com 	size_t	datasz;
4328313SDina.Nimeh@Sun.Com 
4338313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
4348313SDina.Nimeh@Sun.Com 
4358313SDina.Nimeh@Sun.Com 	if (lsp == NULL)
4368313SDina.Nimeh@Sun.Com 		return (CRYPTO_DEVICE_ERROR);
4378313SDina.Nimeh@Sun.Com 
4388313SDina.Nimeh@Sun.Com 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
4398313SDina.Nimeh@Sun.Com 	if (lsp->ls_iv_type == IVM_NONE) {
4408313SDina.Nimeh@Sun.Com 		return (CRYPTO_SUCCESS);
4418313SDina.Nimeh@Sun.Com 	}
4428313SDina.Nimeh@Sun.Com 
4438313SDina.Nimeh@Sun.Com 	/*
4448313SDina.Nimeh@Sun.Com 	 * if kmem already alloced from previous call and it's the same size
4458313SDina.Nimeh@Sun.Com 	 * we need now, just recycle it; allocate new kmem only if we have to
4468313SDina.Nimeh@Sun.Com 	 */
4478313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param == NULL ||
4488313SDina.Nimeh@Sun.Com 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
4498313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_iv_len;
4508313SDina.Nimeh@Sun.Com 		iv = kmem_zalloc(iv_len, KM_SLEEP);
4518313SDina.Nimeh@Sun.Com 	} else {
4528313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_mech.cm_param_len;
4538313SDina.Nimeh@Sun.Com 		iv = lsp->ls_mech.cm_param;
4548313SDina.Nimeh@Sun.Com 		bzero(iv, iv_len);
4558313SDina.Nimeh@Sun.Com 	}
4568313SDina.Nimeh@Sun.Com 
4578313SDina.Nimeh@Sun.Com 	switch (lsp->ls_iv_type) {
4588313SDina.Nimeh@Sun.Com 	case IVM_ENC_BLKNO:
4598313SDina.Nimeh@Sun.Com 		/* iv is not static, lblkno changes each time */
4608313SDina.Nimeh@Sun.Com 		data = &lblkno;
4618313SDina.Nimeh@Sun.Com 		datasz = sizeof (lblkno);
4628313SDina.Nimeh@Sun.Com 		break;
4638313SDina.Nimeh@Sun.Com 	default:
4648313SDina.Nimeh@Sun.Com 		data = 0;
4658313SDina.Nimeh@Sun.Com 		datasz = 0;
4668313SDina.Nimeh@Sun.Com 		break;
4678313SDina.Nimeh@Sun.Com 	}
4688313SDina.Nimeh@Sun.Com 
4698313SDina.Nimeh@Sun.Com 	/*
4708313SDina.Nimeh@Sun.Com 	 * write blkno into the iv buffer padded on the left in case
4718313SDina.Nimeh@Sun.Com 	 * blkno ever grows bigger than its current longlong_t size
4728313SDina.Nimeh@Sun.Com 	 * or a variation other than blkno is used for the iv data
4738313SDina.Nimeh@Sun.Com 	 */
4748313SDina.Nimeh@Sun.Com 	min = MIN(datasz, iv_len);
4758313SDina.Nimeh@Sun.Com 	bcopy(data, iv + (iv_len - min), min);
4768313SDina.Nimeh@Sun.Com 
4778313SDina.Nimeh@Sun.Com 	/* encrypt the data in-place to get the IV */
4788313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, iv, iv_len);
4798313SDina.Nimeh@Sun.Com 
4808313SDina.Nimeh@Sun.Com 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
4818313SDina.Nimeh@Sun.Com 	    NULL, NULL, NULL);
4828313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
4838313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
4848313SDina.Nimeh@Sun.Com 		    lblkno, ret);
4858313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != iv)
4868313SDina.Nimeh@Sun.Com 			kmem_free(iv, iv_len);
4878313SDina.Nimeh@Sun.Com 		return (ret);
4888313SDina.Nimeh@Sun.Com 	}
4898313SDina.Nimeh@Sun.Com 
4908313SDina.Nimeh@Sun.Com 	/* clean up the iv from the last computation */
4918313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
4928313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
4938313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param_len = iv_len;
4948313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param = iv;
4958313SDina.Nimeh@Sun.Com 
4968313SDina.Nimeh@Sun.Com 	return (CRYPTO_SUCCESS);
4978313SDina.Nimeh@Sun.Com }
4988313SDina.Nimeh@Sun.Com 
4998313SDina.Nimeh@Sun.Com /*
5008313SDina.Nimeh@Sun.Com  * Performs encryption and decryption of a chunk of data of size "len",
5018313SDina.Nimeh@Sun.Com  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
5028313SDina.Nimeh@Sun.Com  * DEV_BSIZE.
5038313SDina.Nimeh@Sun.Com  */
5048313SDina.Nimeh@Sun.Com static int
5058313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
5068313SDina.Nimeh@Sun.Com     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
5078313SDina.Nimeh@Sun.Com {
5088313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
5098313SDina.Nimeh@Sun.Com 	crypto_data_t wdata;
5108313SDina.Nimeh@Sun.Com 	int ret;
5118313SDina.Nimeh@Sun.Com 	longlong_t lblkno = bp->b_lblkno;
5128313SDina.Nimeh@Sun.Com 
5138313SDina.Nimeh@Sun.Com 	mutex_enter(&lsp->ls_crypto_lock);
5148313SDina.Nimeh@Sun.Com 
5158313SDina.Nimeh@Sun.Com 	/*
5168313SDina.Nimeh@Sun.Com 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
5178313SDina.Nimeh@Sun.Com 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
5188313SDina.Nimeh@Sun.Com 	 */
5198313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, plaintext, len);
5208313SDina.Nimeh@Sun.Com 	cdata.cd_length = DEV_BSIZE;
5218313SDina.Nimeh@Sun.Com 	if (ciphertext != NULL) {		/* not in-place crypto */
5228313SDina.Nimeh@Sun.Com 		SETUP_C_DATA(wdata, ciphertext, len);
5238313SDina.Nimeh@Sun.Com 		wdata.cd_length = DEV_BSIZE;
5248313SDina.Nimeh@Sun.Com 	}
5258313SDina.Nimeh@Sun.Com 
5268313SDina.Nimeh@Sun.Com 	do {
5278313SDina.Nimeh@Sun.Com 		ret = lofi_blk_mech(lsp, lblkno);
5288313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS)
5298313SDina.Nimeh@Sun.Com 			continue;
5308313SDina.Nimeh@Sun.Com 
5318313SDina.Nimeh@Sun.Com 		if (op_encrypt) {
5328313SDina.Nimeh@Sun.Com 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
5338313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
5348313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
5358313SDina.Nimeh@Sun.Com 		} else {
5368313SDina.Nimeh@Sun.Com 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
5378313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
5388313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
5398313SDina.Nimeh@Sun.Com 		}
5408313SDina.Nimeh@Sun.Com 
5418313SDina.Nimeh@Sun.Com 		cdata.cd_offset += DEV_BSIZE;
5428313SDina.Nimeh@Sun.Com 		if (ciphertext != NULL)
5438313SDina.Nimeh@Sun.Com 			wdata.cd_offset += DEV_BSIZE;
5448313SDina.Nimeh@Sun.Com 		lblkno++;
5458313SDina.Nimeh@Sun.Com 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
5468313SDina.Nimeh@Sun.Com 
5478313SDina.Nimeh@Sun.Com 	mutex_exit(&lsp->ls_crypto_lock);
5488313SDina.Nimeh@Sun.Com 
5498313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
5508313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
5518313SDina.Nimeh@Sun.Com 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
5528313SDina.Nimeh@Sun.Com 		    lblkno, ret);
5538313SDina.Nimeh@Sun.Com 	}
5548313SDina.Nimeh@Sun.Com 
5558313SDina.Nimeh@Sun.Com 	return (ret);
5568313SDina.Nimeh@Sun.Com }
5578313SDina.Nimeh@Sun.Com 
5588313SDina.Nimeh@Sun.Com #define	RDWR_RAW	1
5598313SDina.Nimeh@Sun.Com #define	RDWR_BCOPY	2
5608313SDina.Nimeh@Sun.Com 
5618313SDina.Nimeh@Sun.Com static int
5628313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
5638313SDina.Nimeh@Sun.Com     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
5648313SDina.Nimeh@Sun.Com {
5658313SDina.Nimeh@Sun.Com 	ssize_t resid;
5668313SDina.Nimeh@Sun.Com 	int isread;
5678313SDina.Nimeh@Sun.Com 	int error;
5688313SDina.Nimeh@Sun.Com 
5698313SDina.Nimeh@Sun.Com 	/*
5708313SDina.Nimeh@Sun.Com 	 * Handles reads/writes for both plain and encrypted lofi
5718313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
5728313SDina.Nimeh@Sun.Com 	 * when it gets here.
5738313SDina.Nimeh@Sun.Com 	 */
5748313SDina.Nimeh@Sun.Com 
5758313SDina.Nimeh@Sun.Com 	isread = bp->b_flags & B_READ;
5768313SDina.Nimeh@Sun.Com 	if (isread) {
5778313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
5788313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
5798313SDina.Nimeh@Sun.Com 			bcopy(bcopy_locn, bufaddr, len);
5808313SDina.Nimeh@Sun.Com 			error = 0;
5818313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
5828313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
5838313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
5848313SDina.Nimeh@Sun.Com 			    &resid);
5858313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
5868313SDina.Nimeh@Sun.Com 		}
5878313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled && error == 0) {
5888313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
5898313SDina.Nimeh@Sun.Com 			    B_FALSE) != CRYPTO_SUCCESS) {
5908313SDina.Nimeh@Sun.Com 				/*
5918313SDina.Nimeh@Sun.Com 				 * XXX: original code didn't set residual
5928313SDina.Nimeh@Sun.Com 				 * back to len because no error was expected
5938313SDina.Nimeh@Sun.Com 				 * from bcopy() if encryption is not enabled
5948313SDina.Nimeh@Sun.Com 				 */
5958313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
5968313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
5978313SDina.Nimeh@Sun.Com 				error = EIO;
5988313SDina.Nimeh@Sun.Com 			}
5998313SDina.Nimeh@Sun.Com 		}
6008313SDina.Nimeh@Sun.Com 		return (error);
6018313SDina.Nimeh@Sun.Com 	} else {
6028313SDina.Nimeh@Sun.Com 		void *iobuf = bufaddr;
6038313SDina.Nimeh@Sun.Com 
6048313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6058313SDina.Nimeh@Sun.Com 			/* don't do in-place crypto to keep bufaddr intact */
6068313SDina.Nimeh@Sun.Com 			iobuf = kmem_alloc(len, KM_SLEEP);
6078313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
6088313SDina.Nimeh@Sun.Com 			    B_TRUE) != CRYPTO_SUCCESS) {
6098313SDina.Nimeh@Sun.Com 				kmem_free(iobuf, len);
6108313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
6118313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
6128313SDina.Nimeh@Sun.Com 				return (EIO);
6138313SDina.Nimeh@Sun.Com 			}
6148313SDina.Nimeh@Sun.Com 		}
6158313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
6168313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
6178313SDina.Nimeh@Sun.Com 			bcopy(iobuf, bcopy_locn, len);
6188313SDina.Nimeh@Sun.Com 			error = 0;
6198313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
6208313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
6218313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6228313SDina.Nimeh@Sun.Com 			    &resid);
6238313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
6248313SDina.Nimeh@Sun.Com 		}
6258313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
6268313SDina.Nimeh@Sun.Com 			kmem_free(iobuf, len);
6278313SDina.Nimeh@Sun.Com 		}
6288313SDina.Nimeh@Sun.Com 		return (error);
6298313SDina.Nimeh@Sun.Com 	}
6308313SDina.Nimeh@Sun.Com }
6318313SDina.Nimeh@Sun.Com 
6325643Saalok static int
6335643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6348313SDina.Nimeh@Sun.Com     struct lofi_state *lsp)
6355643Saalok {
6365643Saalok 	int error;
6375643Saalok 	offset_t alignedoffset, mapoffset;
6385643Saalok 	size_t	xfersize;
6395643Saalok 	int	isread;
6408313SDina.Nimeh@Sun.Com 	int	smflags;
6415643Saalok 	caddr_t	mapaddr;
6425643Saalok 	size_t	len;
6435643Saalok 	enum seg_rw srw;
6448313SDina.Nimeh@Sun.Com 	int	save_error;
6458313SDina.Nimeh@Sun.Com 
6468313SDina.Nimeh@Sun.Com 	/*
6478313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
6488313SDina.Nimeh@Sun.Com 	 * when it gets here.
6498313SDina.Nimeh@Sun.Com 	 */
6508313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled)
6518313SDina.Nimeh@Sun.Com 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
6525643Saalok 
6535643Saalok 	/*
6545643Saalok 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
6555643Saalok 	 * an 8K boundary, but the buf transfer address may not be
6565643Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
6575643Saalok 	 * that even though we could). This matters since the initial
6585643Saalok 	 * part of the transfer may not start at offset 0 within the
6595643Saalok 	 * segmap'd chunk. So we have to compensate for that with
6605643Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
6615643Saalok 	 * beginning, and the last is capped by b_resid
6628313SDina.Nimeh@Sun.Com 	 *
6638313SDina.Nimeh@Sun.Com 	 * Visually, where "|" represents page map boundaries:
6648313SDina.Nimeh@Sun.Com 	 *   alignedoffset (mapaddr begins at this segmap boundary)
6658313SDina.Nimeh@Sun.Com 	 *    |   offset (from beginning of file)
6668313SDina.Nimeh@Sun.Com 	 *    |    |	   len
6678313SDina.Nimeh@Sun.Com 	 *    v    v	    v
6688313SDina.Nimeh@Sun.Com 	 * ===|====X========|====...======|========X====|====
6698313SDina.Nimeh@Sun.Com 	 *	   /-------------...---------------/
6708313SDina.Nimeh@Sun.Com 	 *		^ bp->b_bcount/bp->b_resid at start
6718313SDina.Nimeh@Sun.Com 	 *    /----/--------/----...------/--------/
6728313SDina.Nimeh@Sun.Com 	 *	^	^	^   ^		^
6738313SDina.Nimeh@Sun.Com 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
6748313SDina.Nimeh@Sun.Com 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
6758313SDina.Nimeh@Sun.Com 	 *	|	1st xfersize (<= MAXBSIZE)
6768313SDina.Nimeh@Sun.Com 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
6778313SDina.Nimeh@Sun.Com 	 *
6788313SDina.Nimeh@Sun.Com 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
6798313SDina.Nimeh@Sun.Com 	 * MAXBSIZE boundary.  "len" is next page boundary of size
6808313SDina.Nimeh@Sun.Com 	 * MAXBSIZE after "alignedoffset".
6815643Saalok 	 */
6825643Saalok 	mapoffset = offset & MAXBOFFSET;
6835643Saalok 	alignedoffset = offset - mapoffset;
6845643Saalok 	bp->b_resid = bp->b_bcount;
6855643Saalok 	isread = bp->b_flags & B_READ;
6865643Saalok 	srw = isread ? S_READ : S_WRITE;
6875643Saalok 	do {
6885643Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
6895643Saalok 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
6908313SDina.Nimeh@Sun.Com 		len = roundup(mapoffset + xfersize, MAXBSIZE);
6915643Saalok 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
6925643Saalok 		    alignedoffset, MAXBSIZE, 1, srw);
6935643Saalok 		/*
6945643Saalok 		 * Now fault in the pages. This lets us check
6955643Saalok 		 * for errors before we reference mapaddr and
6965643Saalok 		 * try to resolve the fault in bcopy (which would
6975643Saalok 		 * panic instead). And this can easily happen,
6985643Saalok 		 * particularly if you've lofi'd a file over NFS
6995643Saalok 		 * and someone deletes the file on the server.
7005643Saalok 		 */
7015643Saalok 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
7025643Saalok 		    len, F_SOFTLOCK, srw);
7035643Saalok 		if (error) {
7045643Saalok 			(void) segmap_release(segkmap, mapaddr, 0);
7055643Saalok 			if (FC_CODE(error) == FC_OBJERR)
7065643Saalok 				error = FC_ERRNO(error);
7075643Saalok 			else
7085643Saalok 				error = EIO;
7095643Saalok 			break;
7105643Saalok 		}
7118313SDina.Nimeh@Sun.Com 		/* error may be non-zero for encrypted lofi */
7128313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
7138313SDina.Nimeh@Sun.Com 		    RDWR_BCOPY, mapaddr + mapoffset);
7148313SDina.Nimeh@Sun.Com 		if (error == 0) {
7158313SDina.Nimeh@Sun.Com 			bp->b_resid -= xfersize;
7168313SDina.Nimeh@Sun.Com 			bufaddr += xfersize;
7178313SDina.Nimeh@Sun.Com 			offset += xfersize;
7188313SDina.Nimeh@Sun.Com 		}
7195643Saalok 		smflags = 0;
7205643Saalok 		if (isread) {
7215643Saalok 			smflags |= SM_FREE;
7225643Saalok 			/*
7235643Saalok 			 * If we're reading an entire page starting
7245643Saalok 			 * at a page boundary, there's a good chance
7255643Saalok 			 * we won't need it again. Put it on the
7265643Saalok 			 * head of the freelist.
7275643Saalok 			 */
7288056SDina.Nimeh@Sun.Com 			if (mapoffset == 0 && xfersize == MAXBSIZE)
7295643Saalok 				smflags |= SM_DONTNEED;
7305643Saalok 		} else {
7318313SDina.Nimeh@Sun.Com 			if (error == 0)		/* write back good pages */
7328313SDina.Nimeh@Sun.Com 				smflags |= SM_WRITE;
7335643Saalok 		}
7345643Saalok 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
7355643Saalok 		    len, F_SOFTUNLOCK, srw);
7368313SDina.Nimeh@Sun.Com 		save_error = segmap_release(segkmap, mapaddr, smflags);
7378313SDina.Nimeh@Sun.Com 		if (error == 0)
7388313SDina.Nimeh@Sun.Com 			error = save_error;
7395643Saalok 		/* only the first map may start partial */
7405643Saalok 		mapoffset = 0;
7415643Saalok 		alignedoffset += MAXBSIZE;
7425643Saalok 	} while ((error == 0) && (bp->b_resid > 0) &&
7435643Saalok 	    (offset < lsp->ls_vp_comp_size));
7445643Saalok 
7455643Saalok 	return (error);
7465643Saalok }
7475643Saalok 
7485643Saalok /*ARGSUSED*/
7495643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
7505643Saalok     size_t *dstlen, int level)
7515643Saalok {
7525643Saalok 	ASSERT(*dstlen >= srclen);
7535643Saalok 
7545643Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
7555643Saalok 		return (-1);
7565643Saalok 	return (0);
7575643Saalok }
7585643Saalok 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * This is basically what strategy used to be before we found we
7610Sstevel@tonic-gate  * needed task queues.
7620Sstevel@tonic-gate  */
7630Sstevel@tonic-gate static void
7640Sstevel@tonic-gate lofi_strategy_task(void *arg)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	struct buf *bp = (struct buf *)arg;
7670Sstevel@tonic-gate 	int error;
7680Sstevel@tonic-gate 	struct lofi_state *lsp;
7698313SDina.Nimeh@Sun.Com 	offset_t offset;
7708313SDina.Nimeh@Sun.Com 	caddr_t	bufaddr;
7718313SDina.Nimeh@Sun.Com 	size_t	len;
7728313SDina.Nimeh@Sun.Com 	size_t	xfersize;
7738313SDina.Nimeh@Sun.Com 	boolean_t bufinited = B_FALSE;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
7768313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
7778313SDina.Nimeh@Sun.Com 		error = ENXIO;
7788313SDina.Nimeh@Sun.Com 		goto errout;
7798313SDina.Nimeh@Sun.Com 	}
7800Sstevel@tonic-gate 	if (lsp->ls_kstat) {
7810Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
7820Sstevel@tonic-gate 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
7830Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 	bp_mapin(bp);
7860Sstevel@tonic-gate 	bufaddr = bp->b_un.b_addr;
7870Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
7888313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
7898313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
7908313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
7918313SDina.Nimeh@Sun.Com 	}
7928313SDina.Nimeh@Sun.Com 	len = bp->b_bcount;
7938313SDina.Nimeh@Sun.Com 	bufinited = B_TRUE;
7948313SDina.Nimeh@Sun.Com 
7958313SDina.Nimeh@Sun.Com 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
7968313SDina.Nimeh@Sun.Com 		error = EIO;
7978313SDina.Nimeh@Sun.Com 		goto errout;
7988313SDina.Nimeh@Sun.Com 	}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	/*
8010Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
8020Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
8030Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
8040Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
8058313SDina.Nimeh@Sun.Com 	 * mapable or it's a character device or it's an encrypted lofi.
8060Sstevel@tonic-gate 	 */
8078313SDina.Nimeh@Sun.Com 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
8088313SDina.Nimeh@Sun.Com 	    lsp->ls_crypto_enabled) {
8098313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
8108313SDina.Nimeh@Sun.Com 		    NULL);
8118313SDina.Nimeh@Sun.Com 	} else if (lsp->ls_uncomp_seg_sz == 0) {
8128313SDina.Nimeh@Sun.Com 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
8138313SDina.Nimeh@Sun.Com 	} else {
8148313SDina.Nimeh@Sun.Com 		unsigned char *compressed_seg = NULL, *cmpbuf;
8158313SDina.Nimeh@Sun.Com 		unsigned char *uncompressed_seg = NULL;
8168313SDina.Nimeh@Sun.Com 		lofi_compress_info_t *li;
8178313SDina.Nimeh@Sun.Com 		size_t oblkcount;
8188313SDina.Nimeh@Sun.Com 		unsigned long seglen;
8198313SDina.Nimeh@Sun.Com 		uint64_t sblkno, eblkno, cmpbytes;
8208313SDina.Nimeh@Sun.Com 		offset_t sblkoff, eblkoff;
8218313SDina.Nimeh@Sun.Com 		u_offset_t salign, ealign;
8228313SDina.Nimeh@Sun.Com 		u_offset_t sdiff;
8238313SDina.Nimeh@Sun.Com 		uint32_t comp_data_sz;
8245643Saalok 		uint64_t i;
8255643Saalok 
8260Sstevel@tonic-gate 		/*
8275643Saalok 		 * From here on we're dealing primarily with compressed files
8285643Saalok 		 */
8298313SDina.Nimeh@Sun.Com 		ASSERT(!lsp->ls_crypto_enabled);
8305643Saalok 
8315643Saalok 		/*
8325643Saalok 		 * Compressed files can only be read from and
8335643Saalok 		 * not written to
8340Sstevel@tonic-gate 		 */
8355643Saalok 		if (!(bp->b_flags & B_READ)) {
8365643Saalok 			bp->b_resid = bp->b_bcount;
8375643Saalok 			error = EROFS;
8385643Saalok 			goto done;
8395643Saalok 		}
8405643Saalok 
8415643Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
8425643Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
8435643Saalok 		/*
8445643Saalok 		 * Compute starting and ending compressed segment numbers
8455643Saalok 		 * We use only bitwise operations avoiding division and
8465643Saalok 		 * modulus because we enforce the compression segment size
8475643Saalok 		 * to a power of 2
8485643Saalok 		 */
8495643Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
8505643Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
8515643Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
8525643Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
8535643Saalok 
8545643Saalok 		/*
8555643Saalok 		 * Align start offset to block boundary for segmap
8565643Saalok 		 */
8575643Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
8585643Saalok 		sdiff = salign & (DEV_BSIZE - 1);
8595643Saalok 		salign -= sdiff;
8605643Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
8610Sstevel@tonic-gate 			/*
8625643Saalok 			 * We're dealing with the last segment of
8635643Saalok 			 * the compressed file -- the size of this
8645643Saalok 			 * segment *may not* be the same as the
8655643Saalok 			 * segment size for the file
8660Sstevel@tonic-gate 			 */
8675643Saalok 			eblkoff = (offset + bp->b_bcount) &
8685643Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
8695643Saalok 			ealign = lsp->ls_vp_comp_size;
8705643Saalok 		} else {
8715643Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
8725643Saalok 		}
8735643Saalok 
8745643Saalok 		/*
8755643Saalok 		 * Preserve original request paramaters
8765643Saalok 		 */
8775643Saalok 		oblkcount = bp->b_bcount;
8785643Saalok 
8795643Saalok 		/*
8805643Saalok 		 * Assign the calculated parameters
8815643Saalok 		 */
8825643Saalok 		comp_data_sz = ealign - salign;
8835643Saalok 		bp->b_bcount = comp_data_sz;
8845643Saalok 
8855643Saalok 		/*
8865643Saalok 		 * Allocate fixed size memory blocks to hold compressed
8875643Saalok 		 * segments and one uncompressed segment since we
8885643Saalok 		 * uncompress segments one at a time
8895643Saalok 		 */
8905643Saalok 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
8915643Saalok 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
8925643Saalok 		/*
8935643Saalok 		 * Map in the calculated number of blocks
8945643Saalok 		 */
8955643Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
8965643Saalok 		    bp, lsp);
8975643Saalok 
8985643Saalok 		bp->b_bcount = oblkcount;
8995643Saalok 		bp->b_resid = oblkcount;
9005643Saalok 		if (error != 0)
9015643Saalok 			goto done;
9025643Saalok 
9035643Saalok 		/*
9045643Saalok 		 * We have the compressed blocks, now uncompress them
9055643Saalok 		 */
9065643Saalok 		cmpbuf = compressed_seg + sdiff;
9075643Saalok 		for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz;
9085643Saalok 		    i++) {
9095643Saalok 			/*
9105643Saalok 			 * Each of the segment index entries contains
9115643Saalok 			 * the starting block number for that segment.
9125643Saalok 			 * The number of compressed bytes in a segment
9135643Saalok 			 * is thus the difference between the starting
9145643Saalok 			 * block number of this segment and the starting
9155643Saalok 			 * block number of the next segment.
9165643Saalok 			 */
9175643Saalok 			if ((i == eblkno) &&
9185643Saalok 			    (i == lsp->ls_comp_index_sz - 1)) {
9195643Saalok 				cmpbytes = lsp->ls_vp_comp_size -
9205643Saalok 				    lsp->ls_comp_seg_index[i];
9210Sstevel@tonic-gate 			} else {
9225643Saalok 				cmpbytes = lsp->ls_comp_seg_index[i + 1] -
9235643Saalok 				    lsp->ls_comp_seg_index[i];
9240Sstevel@tonic-gate 			}
9255643Saalok 
9265643Saalok 			/*
9275643Saalok 			 * The first byte in a compressed segment is a flag
9285643Saalok 			 * that indicates whether this segment is compressed
9295643Saalok 			 * at all
9305643Saalok 			 */
9315643Saalok 			if (*cmpbuf == UNCOMPRESSED) {
9325643Saalok 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
9335643Saalok 				    (cmpbytes - SEGHDR));
9345643Saalok 			} else {
9355643Saalok 				seglen = lsp->ls_uncomp_seg_sz;
9365643Saalok 
9375643Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
9385643Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
9395643Saalok 				    &seglen, li->l_level) != 0) {
9405643Saalok 					error = EIO;
9415643Saalok 					goto done;
9425643Saalok 				}
9435643Saalok 			}
9445643Saalok 
9455643Saalok 			/*
9465643Saalok 			 * Determine how much uncompressed data we
9475643Saalok 			 * have to copy and copy it
9485643Saalok 			 */
9495643Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
9505643Saalok 			if (i == eblkno) {
9515643Saalok 				if (i == (lsp->ls_comp_index_sz - 1))
9525643Saalok 					xfersize -= (lsp->ls_uncomp_last_seg_sz
9535643Saalok 					    - eblkoff);
9545643Saalok 				else
9555643Saalok 					xfersize -=
9565643Saalok 					    (lsp->ls_uncomp_seg_sz - eblkoff);
9575643Saalok 			}
9585643Saalok 
9595643Saalok 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
9605643Saalok 
9615643Saalok 			cmpbuf += cmpbytes;
9620Sstevel@tonic-gate 			bufaddr += xfersize;
9635643Saalok 			bp->b_resid -= xfersize;
9645643Saalok 			sblkoff = 0;
9655643Saalok 
9665643Saalok 			if (bp->b_resid == 0)
9675643Saalok 				break;
9685643Saalok 		}
9698313SDina.Nimeh@Sun.Com done:
9708313SDina.Nimeh@Sun.Com 		if (compressed_seg != NULL)
9718313SDina.Nimeh@Sun.Com 			kmem_free(compressed_seg, comp_data_sz);
9728313SDina.Nimeh@Sun.Com 		if (uncompressed_seg != NULL)
9738313SDina.Nimeh@Sun.Com 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
9748313SDina.Nimeh@Sun.Com 	} /* end of handling compressed files */
9750Sstevel@tonic-gate 
9768313SDina.Nimeh@Sun.Com errout:
9778313SDina.Nimeh@Sun.Com 	if (bufinited && lsp->ls_kstat) {
9780Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
9790Sstevel@tonic-gate 		kstat_io_t *kioptr;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
9820Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
9830Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
9840Sstevel@tonic-gate 			kioptr->nread += n_done;
9850Sstevel@tonic-gate 			kioptr->reads++;
9860Sstevel@tonic-gate 		} else {
9870Sstevel@tonic-gate 			kioptr->nwritten += n_done;
9880Sstevel@tonic-gate 			kioptr->writes++;
9890Sstevel@tonic-gate 		}
9900Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
9910Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
9920Sstevel@tonic-gate 	}
9934451Seschrock 
9944451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
9954451Seschrock 	if (--lsp->ls_vp_iocount == 0)
9964451Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
9974451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
9984451Seschrock 
9990Sstevel@tonic-gate 	bioerror(bp, error);
10000Sstevel@tonic-gate 	biodone(bp);
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate static int
10040Sstevel@tonic-gate lofi_strategy(struct buf *bp)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	struct lofi_state *lsp;
10070Sstevel@tonic-gate 	offset_t	offset;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
10110Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
10120Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
10130Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
10140Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
10150Sstevel@tonic-gate 	 *
10160Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
10170Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
10180Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
10190Sstevel@tonic-gate 	 */
10200Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
10218313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
10228313SDina.Nimeh@Sun.Com 		bioerror(bp, ENXIO);
10238313SDina.Nimeh@Sun.Com 		biodone(bp);
10248313SDina.Nimeh@Sun.Com 		return (0);
10258313SDina.Nimeh@Sun.Com 	}
10268313SDina.Nimeh@Sun.Com 
10274451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
10284451Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
10294451Seschrock 		bioerror(bp, EIO);
10304451Seschrock 		biodone(bp);
10314451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
10324451Seschrock 		return (0);
10334451Seschrock 	}
10344451Seschrock 
10350Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
10368313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
10378313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
10388313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
10398313SDina.Nimeh@Sun.Com 	}
10400Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
10410Sstevel@tonic-gate 		/* EOF */
10420Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
10430Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
10440Sstevel@tonic-gate 			bioerror(bp, 0);
10450Sstevel@tonic-gate 		} else {
10460Sstevel@tonic-gate 			/* writes should fail */
10470Sstevel@tonic-gate 			bioerror(bp, ENXIO);
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 		biodone(bp);
10504451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
10510Sstevel@tonic-gate 		return (0);
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
10540Sstevel@tonic-gate 		bioerror(bp, ENXIO);
10550Sstevel@tonic-gate 		biodone(bp);
10564451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
10570Sstevel@tonic-gate 		return (0);
10580Sstevel@tonic-gate 	}
10594451Seschrock 	lsp->ls_vp_iocount++;
10604451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
10614451Seschrock 
10620Sstevel@tonic-gate 	if (lsp->ls_kstat) {
10630Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
10640Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
10650Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
10680Sstevel@tonic-gate 	return (0);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate /*ARGSUSED2*/
10720Sstevel@tonic-gate static int
10730Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	if (getminor(dev) == 0)
10760Sstevel@tonic-gate 		return (EINVAL);
10778313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
10780Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*ARGSUSED2*/
10820Sstevel@tonic-gate static int
10830Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
10840Sstevel@tonic-gate {
10850Sstevel@tonic-gate 	if (getminor(dev) == 0)
10860Sstevel@tonic-gate 		return (EINVAL);
10878313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
10880Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate /*ARGSUSED2*/
10920Sstevel@tonic-gate static int
10930Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	if (getminor(dev) == 0)
10960Sstevel@tonic-gate 		return (EINVAL);
10978313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
10980Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate /*ARGSUSED2*/
11020Sstevel@tonic-gate static int
11030Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate 	if (getminor(dev) == 0)
11060Sstevel@tonic-gate 		return (EINVAL);
11078313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
11080Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate /*ARGSUSED*/
11120Sstevel@tonic-gate static int
11130Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	switch (infocmd) {
11160Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
11170Sstevel@tonic-gate 		*result = lofi_dip;
11180Sstevel@tonic-gate 		return (DDI_SUCCESS);
11190Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
11200Sstevel@tonic-gate 		*result = 0;
11210Sstevel@tonic-gate 		return (DDI_SUCCESS);
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 	return (DDI_FAILURE);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate static int
11270Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
11280Sstevel@tonic-gate {
11290Sstevel@tonic-gate 	int	error;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
11320Sstevel@tonic-gate 		return (DDI_FAILURE);
11330Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
11340Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
11350Sstevel@tonic-gate 		return (DDI_FAILURE);
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
11380Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
11390Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
11400Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
11410Sstevel@tonic-gate 		return (DDI_FAILURE);
11420Sstevel@tonic-gate 	}
11435084Sjohnlev 	/* driver handles kernel-issued IOCTLs */
11445084Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
11455084Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
11465084Sjohnlev 		ddi_remove_minor_node(dip, NULL);
11475084Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
11485084Sjohnlev 		return (DDI_FAILURE);
11495084Sjohnlev 	}
11500Sstevel@tonic-gate 	lofi_dip = dip;
11510Sstevel@tonic-gate 	ddi_report_dev(dip);
11520Sstevel@tonic-gate 	return (DDI_SUCCESS);
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate static int
11560Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
11590Sstevel@tonic-gate 		return (DDI_FAILURE);
11600Sstevel@tonic-gate 	if (lofi_busy())
11610Sstevel@tonic-gate 		return (DDI_FAILURE);
11620Sstevel@tonic-gate 	lofi_dip = NULL;
11630Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
11645084Sjohnlev 	ddi_prop_remove_all(dip);
11650Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
11660Sstevel@tonic-gate 	return (DDI_SUCCESS);
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate /*
11708313SDina.Nimeh@Sun.Com  * With addition of encryption, be careful that encryption key is wiped before
11718313SDina.Nimeh@Sun.Com  * kernel memory structures are freed, and also that key is not accidentally
11728313SDina.Nimeh@Sun.Com  * passed out into userland structures.
11738313SDina.Nimeh@Sun.Com  */
11748313SDina.Nimeh@Sun.Com static void
11758313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip)
11768313SDina.Nimeh@Sun.Com {
11778313SDina.Nimeh@Sun.Com 	/* Make sure this encryption key doesn't stick around */
11788313SDina.Nimeh@Sun.Com 	bzero(klip->li_key, sizeof (klip->li_key));
11798313SDina.Nimeh@Sun.Com 	kmem_free(klip, sizeof (struct lofi_ioctl));
11808313SDina.Nimeh@Sun.Com }
11818313SDina.Nimeh@Sun.Com 
11828313SDina.Nimeh@Sun.Com /*
11830Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
11840Sstevel@tonic-gate  * the lofi_ioctl structure.
11850Sstevel@tonic-gate  */
11860Sstevel@tonic-gate struct lofi_ioctl *
11871657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate 	struct lofi_ioctl *klip;
11900Sstevel@tonic-gate 	int	error;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
11931657Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
11940Sstevel@tonic-gate 	if (error) {
11958313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
11960Sstevel@tonic-gate 		return (NULL);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
12008313SDina.Nimeh@Sun.Com 	klip->li_filename[MAXPATHLEN-1] = '\0';
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	/* validate minor number */
12030Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
12048313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
12058313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
12068313SDina.Nimeh@Sun.Com 		    lofi_max_files);
12070Sstevel@tonic-gate 		return (NULL);
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 	return (klip);
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate int
12131657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
12141657Sheppo 	int flag)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	int	error;
12170Sstevel@tonic-gate 
12188313SDina.Nimeh@Sun.Com 	/*
12198313SDina.Nimeh@Sun.Com 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
12208313SDina.Nimeh@Sun.Com 	 * This ensures that an attacker can't trivially find the
12218313SDina.Nimeh@Sun.Com 	 * key for a mapping just by issuing the ioctl.
12228313SDina.Nimeh@Sun.Com 	 *
12238313SDina.Nimeh@Sun.Com 	 * It can still be found by poking around in kmem with mdb(1),
12248313SDina.Nimeh@Sun.Com 	 * but there is no point in making it easy when the info isn't
12258313SDina.Nimeh@Sun.Com 	 * of any use in this direction anyway.
12268313SDina.Nimeh@Sun.Com 	 *
12278313SDina.Nimeh@Sun.Com 	 * Either way we don't actually have the raw key stored in
12288313SDina.Nimeh@Sun.Com 	 * a form that we can get it anyway, since we just used it
12298313SDina.Nimeh@Sun.Com 	 * to create a ctx template and didn't keep "the original".
12308313SDina.Nimeh@Sun.Com 	 */
12311657Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
12320Sstevel@tonic-gate 	if (error)
12330Sstevel@tonic-gate 		return (EFAULT);
12340Sstevel@tonic-gate 	return (0);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate /*
12380Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
12390Sstevel@tonic-gate  */
12400Sstevel@tonic-gate static int
12410Sstevel@tonic-gate file_to_minor(char *filename)
12420Sstevel@tonic-gate {
12430Sstevel@tonic-gate 	minor_t	minor;
12440Sstevel@tonic-gate 	struct lofi_state *lsp;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
12470Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
12480Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
12490Sstevel@tonic-gate 		if (lsp == NULL)
12500Sstevel@tonic-gate 			continue;
12510Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
12520Sstevel@tonic-gate 			return (minor);
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 	return (0);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate /*
12580Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
12590Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
12600Sstevel@tonic-gate  */
12610Sstevel@tonic-gate static int
12620Sstevel@tonic-gate valid_filename(const char *filename)
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
12650Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	/* must be absolute path */
12680Sstevel@tonic-gate 	if (filename[0] != '/')
12690Sstevel@tonic-gate 		return (0);
12700Sstevel@tonic-gate 	/* must not be lofi */
12710Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
12720Sstevel@tonic-gate 		return (0);
12730Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
12740Sstevel@tonic-gate 		return (0);
12750Sstevel@tonic-gate 	return (1);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate /*
12790Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
12800Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
12810Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
12820Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
12833517Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
12840Sstevel@tonic-gate  * have to support it.
12850Sstevel@tonic-gate  */
12860Sstevel@tonic-gate static void
12870Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
12880Sstevel@tonic-gate {
12898313SDina.Nimeh@Sun.Com 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
12908313SDina.Nimeh@Sun.Com 
12910Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
12920Sstevel@tonic-gate 	/*
12930Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
12940Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
12950Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
12960Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
12970Sstevel@tonic-gate 	 * a number between one and one).
12980Sstevel@tonic-gate 	 *
12990Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
13000Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
13010Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
13020Sstevel@tonic-gate 	 */
13038313SDina.Nimeh@Sun.Com 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
13048313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
13050Sstevel@tonic-gate 	else
13068313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
13070Sstevel@tonic-gate 	/* in case file file is < 100k */
13080Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
13090Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
13100Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
13110Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
13120Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
13130Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
13140Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
13150Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
13160Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
13170Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
13180Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
13190Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
13208313SDina.Nimeh@Sun.Com 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
13210Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
13220Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
13250Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
13260Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
13270Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
1328*8669SDina.Nimeh@Sun.COM 	(void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME,
1329*8669SDina.Nimeh@Sun.COM 	    sizeof (lsp->ls_vtoc.v_volume));
13300Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
13310Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
13320Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
13335643Saalok 
13345643Saalok 	/*
13355643Saalok 	 * A compressed file is read-only, other files can
13365643Saalok 	 * be read-write
13375643Saalok 	 */
13385643Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
13395643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
13405643Saalok 	} else {
13415643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
13425643Saalok 	}
13430Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
13460Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
13470Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
13480Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
13490Sstevel@tonic-gate 	 */
13500Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
13510Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
13540Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
13550Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
13560Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
13570Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
13580Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
13590Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
13600Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
13610Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
13620Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
13630Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
13640Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
13650Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
13660Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
13690Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
13700Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
13710Sstevel@tonic-gate 	 * maxcontig is 0.
13720Sstevel@tonic-gate 	 */
13730Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate /*
13775643Saalok  * map in a compressed file
13785643Saalok  *
13795643Saalok  * Read in the header and the index that follows.
13805643Saalok  *
13815643Saalok  * The header is as follows -
13825643Saalok  *
13835643Saalok  * Signature (name of the compression algorithm)
13845643Saalok  * Compression segment size (a multiple of 512)
13855643Saalok  * Number of index entries
13865643Saalok  * Size of the last block
13875643Saalok  * The array containing the index entries
13885643Saalok  *
13895643Saalok  * The header information is always stored in
13905643Saalok  * network byte order on disk.
13915643Saalok  */
13925643Saalok static int
13935643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
13945643Saalok {
13955643Saalok 	uint32_t index_sz, header_len, i;
13965643Saalok 	ssize_t	resid;
13975643Saalok 	enum uio_rw rw;
13985643Saalok 	char *tbuf = buf;
13995643Saalok 	int error;
14005643Saalok 
14015643Saalok 	/* The signature has already been read */
14025643Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
14035643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
14045643Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
14055643Saalok 
14065643Saalok 	/*
14075643Saalok 	 * The compressed segment size must be a power of 2
14085643Saalok 	 */
14095643Saalok 	if (lsp->ls_uncomp_seg_sz % 2)
14105643Saalok 		return (EINVAL);
14115643Saalok 
14125643Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
14135643Saalok 		;
14145643Saalok 
14155643Saalok 	lsp->ls_comp_seg_shift = i;
14165643Saalok 
14175643Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
14185643Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
14195643Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
14205643Saalok 
14215643Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
14225643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
14235643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
14245643Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
14255643Saalok 
14265643Saalok 	/*
14275643Saalok 	 * Compute the total size of the uncompressed data
14285643Saalok 	 * for use in fake_disk_geometry and other calculations.
14295643Saalok 	 * Disk geometry has to be faked with respect to the
14305643Saalok 	 * actual uncompressed data size rather than the
14315643Saalok 	 * compressed file size.
14325643Saalok 	 */
14335643Saalok 	lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
14345643Saalok 	    + lsp->ls_uncomp_last_seg_sz;
14355643Saalok 
14365643Saalok 	/*
14375643Saalok 	 * Index size is rounded up to a 512 byte boundary for ease
14385643Saalok 	 * of segmapping
14395643Saalok 	 */
14405643Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
14415643Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
14425643Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
14435643Saalok 	    sizeof (lsp->ls_comp_index_sz) +
14445643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
14455643Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
14465643Saalok 
14475643Saalok 	index_sz += header_len;
14485643Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
14495643Saalok 
14505643Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
14515643Saalok 	lsp->ls_comp_index_data_sz = index_sz;
14525643Saalok 
14535643Saalok 	/*
14545643Saalok 	 * Read in the index -- this has a side-effect
14555643Saalok 	 * of reading in the header as well
14565643Saalok 	 */
14575643Saalok 	rw = UIO_READ;
14585643Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
14595643Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
14605643Saalok 
14615643Saalok 	if (error != 0)
14625643Saalok 		return (error);
14635643Saalok 
14645643Saalok 	/* Skip the header, this is where the index really begins */
14655643Saalok 	lsp->ls_comp_seg_index =
14665643Saalok 	    /*LINTED*/
14675643Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
14685643Saalok 
14695643Saalok 	/*
14705643Saalok 	 * Now recompute offsets in the index to account for
14715643Saalok 	 * the header length
14725643Saalok 	 */
14735643Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
14745643Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
14755643Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
14765643Saalok 	}
14775643Saalok 
14785643Saalok 	return (error);
14795643Saalok }
14805643Saalok 
14815643Saalok /*
14825643Saalok  * Check to see if the passed in signature is a valid
14838313SDina.Nimeh@Sun.Com  * one.  If it is valid, return the index into
14845643Saalok  * lofi_compress_table.
14855643Saalok  *
14865643Saalok  * Return -1 if it is invalid
14875643Saalok  */
14885643Saalok static int lofi_compress_select(char *signature)
14895643Saalok {
14905643Saalok 	int i;
14915643Saalok 
14925643Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
14935643Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
14945643Saalok 			return (i);
14955643Saalok 	}
14965643Saalok 
14975643Saalok 	return (-1);
14985643Saalok }
14995643Saalok 
15005643Saalok /*
15010Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
15020Sstevel@tonic-gate  */
15030Sstevel@tonic-gate static int
15040Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
15051657Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate 	minor_t	newminor;
15080Sstevel@tonic-gate 	struct lofi_state *lsp;
15090Sstevel@tonic-gate 	struct lofi_ioctl *klip;
15100Sstevel@tonic-gate 	int	error;
15110Sstevel@tonic-gate 	struct vnode *vp;
15120Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
15130Sstevel@tonic-gate 	int64_t	Size_prop_val;
15145643Saalok 	int	compress_index;
15150Sstevel@tonic-gate 	vattr_t	vattr;
15160Sstevel@tonic-gate 	int	flag;
15170Sstevel@tonic-gate 	enum vtype v_type;
15184451Seschrock 	int zalloced = 0;
15190Sstevel@tonic-gate 	dev_t	newdev;
15204451Seschrock 	char	namebuf[50];
15218313SDina.Nimeh@Sun.Com 	char	buf[DEV_BSIZE];
15228313SDina.Nimeh@Sun.Com 	char	crybuf[DEV_BSIZE];
15235643Saalok 	ssize_t	resid;
15248313SDina.Nimeh@Sun.Com 	boolean_t need_vn_close = B_FALSE;
15258313SDina.Nimeh@Sun.Com 	boolean_t keycopied = B_FALSE;
15268313SDina.Nimeh@Sun.Com 	boolean_t need_size_update = B_FALSE;
15270Sstevel@tonic-gate 
15281657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
15290Sstevel@tonic-gate 	if (klip == NULL)
15300Sstevel@tonic-gate 		return (EFAULT);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
15350Sstevel@tonic-gate 		error = EINVAL;
15360Sstevel@tonic-gate 		goto out;
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
15400Sstevel@tonic-gate 		error = EBUSY;
15410Sstevel@tonic-gate 		goto out;
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (pickminor) {
15450Sstevel@tonic-gate 		/* Find a free one */
15460Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
15470Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
15480Sstevel@tonic-gate 				break;
15490Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
15500Sstevel@tonic-gate 			error = EAGAIN;
15510Sstevel@tonic-gate 			goto out;
15520Sstevel@tonic-gate 		}
15530Sstevel@tonic-gate 	} else {
15540Sstevel@tonic-gate 		newminor = klip->li_minor;
15550Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
15560Sstevel@tonic-gate 			error = EEXIST;
15570Sstevel@tonic-gate 			goto out;
15580Sstevel@tonic-gate 		}
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/* make sure it's valid */
15620Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
15630Sstevel@tonic-gate 	    NULLVPP, &vp);
15640Sstevel@tonic-gate 	if (error) {
15650Sstevel@tonic-gate 		goto out;
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	v_type = vp->v_type;
15680Sstevel@tonic-gate 	VN_RELE(vp);
15690Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
15700Sstevel@tonic-gate 		error = EINVAL;
15710Sstevel@tonic-gate 		goto out;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
15740Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
15750Sstevel@tonic-gate 	if (error) {
15760Sstevel@tonic-gate 		/* try read-only */
15770Sstevel@tonic-gate 		flag &= ~FWRITE;
15780Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
15790Sstevel@tonic-gate 		    &vp, 0, 0);
15800Sstevel@tonic-gate 		if (error) {
15810Sstevel@tonic-gate 			goto out;
15820Sstevel@tonic-gate 		}
15830Sstevel@tonic-gate 	}
15848313SDina.Nimeh@Sun.Com 	need_vn_close = B_TRUE;
15858313SDina.Nimeh@Sun.Com 
15860Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
15875331Samw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
15880Sstevel@tonic-gate 	if (error) {
15898313SDina.Nimeh@Sun.Com 		goto out;
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
15920Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
15930Sstevel@tonic-gate 		error = EINVAL;
15948313SDina.Nimeh@Sun.Com 		goto out;
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
15970Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
15980Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
15990Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
16000Sstevel@tonic-gate 		error = EINVAL;
16018313SDina.Nimeh@Sun.Com 		goto out;
16020Sstevel@tonic-gate 	}
16030Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
16040Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
16050Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
16060Sstevel@tonic-gate 		error = EINVAL;
16070Sstevel@tonic-gate 		goto propout;
16080Sstevel@tonic-gate 	}
16090Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
16100Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
16110Sstevel@tonic-gate 		error = ENOMEM;
16120Sstevel@tonic-gate 		goto propout;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 	zalloced = 1;
16150Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
16166883Sgd78059 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
16170Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
16180Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
16190Sstevel@tonic-gate 		error = ENXIO;
16200Sstevel@tonic-gate 		goto propout;
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
16230Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
16240Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
16250Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
16260Sstevel@tonic-gate 		/* remove block node */
16270Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
16280Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
16290Sstevel@tonic-gate 		error = ENXIO;
16300Sstevel@tonic-gate 		goto propout;
16310Sstevel@tonic-gate 	}
16320Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
16330Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
16340Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
16350Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
16360Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
16370Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
16380Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
16390Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
16400Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
16410Sstevel@tonic-gate 	if (lsp->ls_kstat) {
16420Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
16430Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
16440Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
16450Sstevel@tonic-gate 	}
16464451Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
16474451Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
16484451Seschrock 
16490Sstevel@tonic-gate 	/*
16500Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
16510Sstevel@tonic-gate 	 * updated correctly.
16520Sstevel@tonic-gate 	 */
16530Sstevel@tonic-gate 	lsp->ls_openflag = flag;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	/*
16560Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
16570Sstevel@tonic-gate 	 */
16580Sstevel@tonic-gate 	if (vp->v_type == VREG) {
16595331Samw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
16600Sstevel@tonic-gate 			lsp->ls_vp = vp;
16610Sstevel@tonic-gate 		} else {
16620Sstevel@tonic-gate 			/*
16630Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
16640Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
16650Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
16660Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
16670Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
16680Sstevel@tonic-gate 			 * the realvp so things behave as if we've
16690Sstevel@tonic-gate 			 * opened the realvp without any interaction
16700Sstevel@tonic-gate 			 * with lofs.
16710Sstevel@tonic-gate 			 */
16720Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
16730Sstevel@tonic-gate 			VN_RELE(vp);
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 	} else {
16760Sstevel@tonic-gate 		lsp->ls_vp = vp;
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
16790Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
16800Sstevel@tonic-gate 	if (rvalp)
16810Sstevel@tonic-gate 		*rvalp = (int)newminor;
16820Sstevel@tonic-gate 	klip->li_minor = newminor;
16830Sstevel@tonic-gate 
16845643Saalok 	/*
16858313SDina.Nimeh@Sun.Com 	 * Initialize crypto details for encrypted lofi
16865643Saalok 	 */
16878313SDina.Nimeh@Sun.Com 	if (klip->li_crypto_enabled) {
16888313SDina.Nimeh@Sun.Com 		int ret;
16898313SDina.Nimeh@Sun.Com 
16908313SDina.Nimeh@Sun.Com 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
16918313SDina.Nimeh@Sun.Com 
16928313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
16938313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
16948313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
16958313SDina.Nimeh@Sun.Com 			    klip->li_cipher, lsp->ls_filename);
16968313SDina.Nimeh@Sun.Com 			error = EINVAL;
16978313SDina.Nimeh@Sun.Com 			goto propout;
16988313SDina.Nimeh@Sun.Com 		}
16998313SDina.Nimeh@Sun.Com 
17008313SDina.Nimeh@Sun.Com 		/* this is just initialization here */
17018313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param = NULL;
17028313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param_len = 0;
17038313SDina.Nimeh@Sun.Com 
17048313SDina.Nimeh@Sun.Com 		lsp->ls_iv_type = klip->li_iv_type;
17058313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
17068313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
17078313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
17088313SDina.Nimeh@Sun.Com 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
17098313SDina.Nimeh@Sun.Com 			error = EINVAL;
17108313SDina.Nimeh@Sun.Com 			goto propout;
17118313SDina.Nimeh@Sun.Com 		}
17128313SDina.Nimeh@Sun.Com 
17138313SDina.Nimeh@Sun.Com 		/* iv mech must itself take a null iv */
17148313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param = NULL;
17158313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param_len = 0;
17168313SDina.Nimeh@Sun.Com 		lsp->ls_iv_len = klip->li_iv_len;
17178313SDina.Nimeh@Sun.Com 
17188313SDina.Nimeh@Sun.Com 		/*
17198313SDina.Nimeh@Sun.Com 		 * Create ctx using li_cipher & the raw li_key after checking
17208313SDina.Nimeh@Sun.Com 		 * that it isn't a weak key.
17218313SDina.Nimeh@Sun.Com 		 */
17228313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
17238313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = klip->li_key_len;
17248313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = kmem_alloc(
17258313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
17268313SDina.Nimeh@Sun.Com 		bcopy(klip->li_key, lsp->ls_key.ck_data,
17278313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
17288313SDina.Nimeh@Sun.Com 		keycopied = B_TRUE;
17298313SDina.Nimeh@Sun.Com 
17308313SDina.Nimeh@Sun.Com 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
17318313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS) {
17328313SDina.Nimeh@Sun.Com 			error = EINVAL;
17338313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "weak key check failed for cipher "
17348313SDina.Nimeh@Sun.Com 			    "%s on file %s (0x%x)", klip->li_cipher,
17358313SDina.Nimeh@Sun.Com 			    lsp->ls_filename, ret);
17368313SDina.Nimeh@Sun.Com 			goto propout;
17378313SDina.Nimeh@Sun.Com 		}
17388313SDina.Nimeh@Sun.Com 	}
17398313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
17408313SDina.Nimeh@Sun.Com 
17418313SDina.Nimeh@Sun.Com 	/*
17428313SDina.Nimeh@Sun.Com 	 * Read the file signature to check if it is compressed or encrypted.
17438313SDina.Nimeh@Sun.Com 	 * Crypto signature is in a different location; both areas should
17448313SDina.Nimeh@Sun.Com 	 * read to keep compression and encryption mutually exclusive.
17458313SDina.Nimeh@Sun.Com 	 */
17468313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
17478313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
17488313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
17498313SDina.Nimeh@Sun.Com 		if (error != 0)
17508313SDina.Nimeh@Sun.Com 			goto propout;
17518313SDina.Nimeh@Sun.Com 	}
17528313SDina.Nimeh@Sun.Com 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
17535643Saalok 	    0, RLIM64_INFINITY, kcred, &resid);
17545643Saalok 	if (error != 0)
17555643Saalok 		goto propout;
17565643Saalok 
17578313SDina.Nimeh@Sun.Com 	/* initialize these variables for all lofi files */
17585643Saalok 	lsp->ls_uncomp_seg_sz = 0;
17595643Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
17605643Saalok 	lsp->ls_comp_algorithm[0] = '\0';
17615643Saalok 
17628313SDina.Nimeh@Sun.Com 	/* encrypted lofi reads/writes shifted by crypto metadata size */
17638313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_offset = 0;
17648313SDina.Nimeh@Sun.Com 
17658313SDina.Nimeh@Sun.Com 	/* this is a compressed lofi */
17668313SDina.Nimeh@Sun.Com 	if ((compress_index = lofi_compress_select(buf)) != -1) {
17678313SDina.Nimeh@Sun.Com 
17688313SDina.Nimeh@Sun.Com 		/* compression and encryption are mutually exclusive */
17698313SDina.Nimeh@Sun.Com 		if (klip->li_crypto_enabled) {
17708313SDina.Nimeh@Sun.Com 			error = ENOTSUP;
17718313SDina.Nimeh@Sun.Com 			goto propout;
17728313SDina.Nimeh@Sun.Com 		}
17738313SDina.Nimeh@Sun.Com 
17748313SDina.Nimeh@Sun.Com 		/* initialize compression info for compressed lofi */
17755643Saalok 		lsp->ls_comp_algorithm_index = compress_index;
17765643Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
17775643Saalok 		    lofi_compress_table[compress_index].l_name,
17785643Saalok 		    sizeof (lsp->ls_comp_algorithm));
17798313SDina.Nimeh@Sun.Com 
17805643Saalok 		error = lofi_map_compressed_file(lsp, buf);
17815643Saalok 		if (error != 0)
17825643Saalok 			goto propout;
17838313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
17845643Saalok 
17858313SDina.Nimeh@Sun.Com 	/* this is an encrypted lofi */
17868313SDina.Nimeh@Sun.Com 	} else if (strncmp(crybuf, lofi_crypto_magic,
17878313SDina.Nimeh@Sun.Com 	    sizeof (lofi_crypto_magic)) == 0) {
17888313SDina.Nimeh@Sun.Com 
17898313SDina.Nimeh@Sun.Com 		char *marker = crybuf;
17908313SDina.Nimeh@Sun.Com 
17918313SDina.Nimeh@Sun.Com 		/*
17928313SDina.Nimeh@Sun.Com 		 * This is the case where the header in the lofi image is
17938313SDina.Nimeh@Sun.Com 		 * already initialized to indicate it is encrypted.
17948313SDina.Nimeh@Sun.Com 		 * There is another case (see below) where encryption is
17958313SDina.Nimeh@Sun.Com 		 * requested but the lofi image has never been used yet,
17968313SDina.Nimeh@Sun.Com 		 * so the header needs to be written with encryption magic.
17978313SDina.Nimeh@Sun.Com 		 */
17988313SDina.Nimeh@Sun.Com 
17998313SDina.Nimeh@Sun.Com 		/* indicate this must be an encrypted lofi due to magic */
18008313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = B_TRUE;
18018313SDina.Nimeh@Sun.Com 
18028313SDina.Nimeh@Sun.Com 		/*
18038313SDina.Nimeh@Sun.Com 		 * The encryption header information is laid out this way:
18048313SDina.Nimeh@Sun.Com 		 *	6 bytes:	hex "CFLOFI"
18058313SDina.Nimeh@Sun.Com 		 *	2 bytes:	version = 0 ... for now
18068313SDina.Nimeh@Sun.Com 		 *	96 bytes:	reserved1 (not implemented yet)
18078313SDina.Nimeh@Sun.Com 		 *	4 bytes:	data_sector = 2 ... for now
18088313SDina.Nimeh@Sun.Com 		 *	more...		not implemented yet
18098313SDina.Nimeh@Sun.Com 		 */
18108313SDina.Nimeh@Sun.Com 
18118313SDina.Nimeh@Sun.Com 		/* copy the magic */
18128313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.magic,
18138313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.magic));
18148313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.magic);
18158313SDina.Nimeh@Sun.Com 
18168313SDina.Nimeh@Sun.Com 		/* read the encryption version number */
18178313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.version),
18188313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.version));
18198313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
18208313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.version);
18218313SDina.Nimeh@Sun.Com 
18228313SDina.Nimeh@Sun.Com 		/* read a chunk of reserved data */
18238313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.reserved1,
18248313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.reserved1));
18258313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.reserved1);
18268313SDina.Nimeh@Sun.Com 
18278313SDina.Nimeh@Sun.Com 		/* read block number where encrypted data begins */
18288313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.data_sector),
18298313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.data_sector));
18308313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
18318313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.data_sector);
18328313SDina.Nimeh@Sun.Com 
18338313SDina.Nimeh@Sun.Com 		/* and ignore the rest until it is implemented */
18348313SDina.Nimeh@Sun.Com 
18358313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
18368313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
18378313SDina.Nimeh@Sun.Com 
18388313SDina.Nimeh@Sun.Com 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
18398313SDina.Nimeh@Sun.Com 	} else if (klip->li_crypto_enabled) {
18408313SDina.Nimeh@Sun.Com 
18418313SDina.Nimeh@Sun.Com 		/*
18428313SDina.Nimeh@Sun.Com 		 * This is the case where encryption was requested but the
18438313SDina.Nimeh@Sun.Com 		 * appears to be entirely blank where the encryption header
18448313SDina.Nimeh@Sun.Com 		 * would have been in the lofi image.  If it is blank,
18458313SDina.Nimeh@Sun.Com 		 * assume it is a brand new lofi image and initialize the
18468313SDina.Nimeh@Sun.Com 		 * header area with encryption magic and current version
18478313SDina.Nimeh@Sun.Com 		 * header data.  If it is not blank, that's an error.
18488313SDina.Nimeh@Sun.Com 		 */
18498313SDina.Nimeh@Sun.Com 		int	i;
18508313SDina.Nimeh@Sun.Com 		char	*marker;
18518313SDina.Nimeh@Sun.Com 		struct crypto_meta	chead;
18528313SDina.Nimeh@Sun.Com 
18538313SDina.Nimeh@Sun.Com 		for (i = 0; i < sizeof (struct crypto_meta); i++)
18548313SDina.Nimeh@Sun.Com 			if (crybuf[i] != '\0')
18558313SDina.Nimeh@Sun.Com 				break;
18568313SDina.Nimeh@Sun.Com 		if (i != sizeof (struct crypto_meta)) {
18578313SDina.Nimeh@Sun.Com 			error = EINVAL;
18588313SDina.Nimeh@Sun.Com 			goto propout;
18598313SDina.Nimeh@Sun.Com 		}
18608313SDina.Nimeh@Sun.Com 
18618313SDina.Nimeh@Sun.Com 		/* nothing there, initialize as encrypted lofi */
18628313SDina.Nimeh@Sun.Com 		marker = crybuf;
18638313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
18648313SDina.Nimeh@Sun.Com 		marker += sizeof (lofi_crypto_magic);
18658313SDina.Nimeh@Sun.Com 		chead.version = htons(LOFI_CRYPTO_VERSION);
18668313SDina.Nimeh@Sun.Com 		bcopy(&(chead.version), marker, sizeof (chead.version));
18678313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.version);
18688313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.reserved1);
18698313SDina.Nimeh@Sun.Com 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
18708313SDina.Nimeh@Sun.Com 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
18718313SDina.Nimeh@Sun.Com 
18728313SDina.Nimeh@Sun.Com 		/* write the header */
18738313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
18748313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
18758313SDina.Nimeh@Sun.Com 		if (error != 0)
18768313SDina.Nimeh@Sun.Com 			goto propout;
18778313SDina.Nimeh@Sun.Com 
18788313SDina.Nimeh@Sun.Com 		/* fix things up so it looks like we read this info */
18798313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
18808313SDina.Nimeh@Sun.Com 		    sizeof (lofi_crypto_magic));
18818313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
18828313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
18838313SDina.Nimeh@Sun.Com 
18848313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
18858313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
18868313SDina.Nimeh@Sun.Com 	}
18878313SDina.Nimeh@Sun.Com 
18888313SDina.Nimeh@Sun.Com 	/*
18898313SDina.Nimeh@Sun.Com 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
18908313SDina.Nimeh@Sun.Com 	 * for encrypted lofi, advertise that it is somewhat shorter
18918313SDina.Nimeh@Sun.Com 	 * due to embedded crypto metadata section
18928313SDina.Nimeh@Sun.Com 	 */
18938313SDina.Nimeh@Sun.Com 	if (need_size_update) {
18945643Saalok 		/* update DDI properties */
18958313SDina.Nimeh@Sun.Com 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
18965643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
18975643Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
18985643Saalok 			error = EINVAL;
18995643Saalok 			goto propout;
19005643Saalok 		}
19018313SDina.Nimeh@Sun.Com 		Nblocks_prop_val =
19028313SDina.Nimeh@Sun.Com 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
19035643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
19045643Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
19055643Saalok 			error = EINVAL;
19065643Saalok 			goto propout;
19075643Saalok 		}
19085643Saalok 	}
19095643Saalok 
19100Sstevel@tonic-gate 	fake_disk_geometry(lsp);
19110Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
19121657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
19130Sstevel@tonic-gate 	free_lofi_ioctl(klip);
19140Sstevel@tonic-gate 	return (0);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate propout:
19178313SDina.Nimeh@Sun.Com 	if (keycopied) {
19188313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
19198313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
19208313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
19218313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
19228313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
19238313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
19248313SDina.Nimeh@Sun.Com 	}
19258313SDina.Nimeh@Sun.Com 
19268313SDina.Nimeh@Sun.Com 	if (zalloced)
19278313SDina.Nimeh@Sun.Com 		ddi_soft_state_free(lofi_statep, newminor);
19288313SDina.Nimeh@Sun.Com 
19290Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
19300Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
19318313SDina.Nimeh@Sun.Com 
19320Sstevel@tonic-gate out:
19338313SDina.Nimeh@Sun.Com 	if (need_vn_close) {
19348313SDina.Nimeh@Sun.Com 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
19358313SDina.Nimeh@Sun.Com 		VN_RELE(vp);
19368313SDina.Nimeh@Sun.Com 	}
19378313SDina.Nimeh@Sun.Com 
19380Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
19390Sstevel@tonic-gate 	free_lofi_ioctl(klip);
19400Sstevel@tonic-gate 	return (error);
19410Sstevel@tonic-gate }
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate /*
19440Sstevel@tonic-gate  * unmap a file.
19450Sstevel@tonic-gate  */
19460Sstevel@tonic-gate static int
19470Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
19481657Sheppo     struct cred *credp, int ioctl_flag)
19490Sstevel@tonic-gate {
19500Sstevel@tonic-gate 	struct lofi_state *lsp;
19510Sstevel@tonic-gate 	struct lofi_ioctl *klip;
19520Sstevel@tonic-gate 	minor_t	minor;
19530Sstevel@tonic-gate 
19541657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
19550Sstevel@tonic-gate 	if (klip == NULL)
19560Sstevel@tonic-gate 		return (EFAULT);
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
19590Sstevel@tonic-gate 	if (byfilename) {
19600Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
19610Sstevel@tonic-gate 	} else {
19620Sstevel@tonic-gate 		minor = klip->li_minor;
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 	if (minor == 0) {
19650Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
19660Sstevel@tonic-gate 		free_lofi_ioctl(klip);
19670Sstevel@tonic-gate 		return (ENXIO);
19680Sstevel@tonic-gate 	}
19690Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
19704451Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
19710Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
19720Sstevel@tonic-gate 		free_lofi_ioctl(klip);
19730Sstevel@tonic-gate 		return (ENXIO);
19740Sstevel@tonic-gate 	}
19754451Seschrock 
19766734Sjohnlev 	/*
19776734Sjohnlev 	 * If it's still held open, we'll do one of three things:
19786734Sjohnlev 	 *
19796734Sjohnlev 	 * If no flag is set, just return EBUSY.
19806734Sjohnlev 	 *
19816734Sjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
19826734Sjohnlev 	 * the last user finishes.
19836734Sjohnlev 	 *
19846734Sjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
19856734Sjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
19866734Sjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
19876734Sjohnlev 	 * device will be cleaned up appropriately.
19886734Sjohnlev 	 *
19896734Sjohnlev 	 * This is complicated by the fact that we may have outstanding
19906734Sjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
19918313SDina.Nimeh@Sun.Com 	 * I/O, we keep a count of the number of outstanding I/O requests
19928313SDina.Nimeh@Sun.Com 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
19938313SDina.Nimeh@Sun.Com 	 * should be dispatched (ls_vp_closereq).
19948313SDina.Nimeh@Sun.Com 	 *
19956734Sjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
19966734Sjohnlev 	 * and then close the underlying vnode.
19976734Sjohnlev 	 */
19980Sstevel@tonic-gate 	if (is_opened(lsp)) {
19994451Seschrock 		if (klip->li_force) {
20008313SDina.Nimeh@Sun.Com 			/*
20018313SDina.Nimeh@Sun.Com 			 * XXX: the section marked here should probably be
20028313SDina.Nimeh@Sun.Com 			 * carefully incorporated into lofi_free_handle();
20038313SDina.Nimeh@Sun.Com 			 * afterward just replace this section with:
20048313SDina.Nimeh@Sun.Com 			 *	lofi_free_handle(dev, minor, lsp, credp);
20058313SDina.Nimeh@Sun.Com 			 * and clean up lofi_unmap_file() a bit more
20068313SDina.Nimeh@Sun.Com 			 */
20078313SDina.Nimeh@Sun.Com 			lofi_free_crypto(lsp);
20088313SDina.Nimeh@Sun.Com 
20094451Seschrock 			mutex_enter(&lsp->ls_vp_lock);
20104451Seschrock 			lsp->ls_vp_closereq = B_TRUE;
20114451Seschrock 			while (lsp->ls_vp_iocount > 0)
20124451Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
20134451Seschrock 			(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
20145331Samw 			    credp, NULL);
20154451Seschrock 			VN_RELE(lsp->ls_vp);
20164451Seschrock 			lsp->ls_vp = NULL;
20174451Seschrock 			cv_broadcast(&lsp->ls_vp_cv);
20184451Seschrock 			mutex_exit(&lsp->ls_vp_lock);
20198313SDina.Nimeh@Sun.Com 			/*
20208313SDina.Nimeh@Sun.Com 			 * XXX: to here
20218313SDina.Nimeh@Sun.Com 			 */
20228313SDina.Nimeh@Sun.Com 
20238313SDina.Nimeh@Sun.Com 			klip->li_minor = minor;
20244451Seschrock 			mutex_exit(&lofi_lock);
20254451Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
20264451Seschrock 			free_lofi_ioctl(klip);
20274451Seschrock 			return (0);
20286734Sjohnlev 		} else if (klip->li_cleanup) {
20296734Sjohnlev 			lsp->ls_cleanup = 1;
20306734Sjohnlev 			mutex_exit(&lofi_lock);
20316734Sjohnlev 			free_lofi_ioctl(klip);
20326734Sjohnlev 			return (0);
20334451Seschrock 		}
20346734Sjohnlev 
20350Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
20360Sstevel@tonic-gate 		free_lofi_ioctl(klip);
20370Sstevel@tonic-gate 		return (EBUSY);
20380Sstevel@tonic-gate 	}
20390Sstevel@tonic-gate 
20404451Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	klip->li_minor = minor;
20430Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
20441657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
20450Sstevel@tonic-gate 	free_lofi_ioctl(klip);
20460Sstevel@tonic-gate 	return (0);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate /*
20500Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
20510Sstevel@tonic-gate  * the name.
20520Sstevel@tonic-gate  */
20534451Seschrock /*ARGSUSED*/
20540Sstevel@tonic-gate static int
20550Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
20561657Sheppo     struct cred *credp, int ioctl_flag)
20570Sstevel@tonic-gate {
20580Sstevel@tonic-gate 	struct lofi_state *lsp;
20590Sstevel@tonic-gate 	struct lofi_ioctl *klip;
20600Sstevel@tonic-gate 	int	error;
20610Sstevel@tonic-gate 	minor_t	minor;
20620Sstevel@tonic-gate 
20631657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
20640Sstevel@tonic-gate 	if (klip == NULL)
20650Sstevel@tonic-gate 		return (EFAULT);
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	switch (which) {
20680Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
20690Sstevel@tonic-gate 		minor = klip->li_minor;
20700Sstevel@tonic-gate 		if (minor == 0) {
20710Sstevel@tonic-gate 			free_lofi_ioctl(klip);
20720Sstevel@tonic-gate 			return (EINVAL);
20730Sstevel@tonic-gate 		}
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
20760Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
20770Sstevel@tonic-gate 		if (lsp == NULL) {
20780Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
20790Sstevel@tonic-gate 			free_lofi_ioctl(klip);
20800Sstevel@tonic-gate 			return (ENXIO);
20810Sstevel@tonic-gate 		}
20820Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
20835643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
20845643Saalok 		    sizeof (klip->li_algorithm));
20858313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
20860Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
20871657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
20880Sstevel@tonic-gate 		free_lofi_ioctl(klip);
20890Sstevel@tonic-gate 		return (error);
20900Sstevel@tonic-gate 	case LOFI_GET_MINOR:
20910Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
20920Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
20938313SDina.Nimeh@Sun.Com 		/* caller should not depend on klip->li_crypto_enabled here */
20940Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
20950Sstevel@tonic-gate 		if (klip->li_minor == 0) {
20960Sstevel@tonic-gate 			free_lofi_ioctl(klip);
20970Sstevel@tonic-gate 			return (ENOENT);
20980Sstevel@tonic-gate 		}
20991657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21000Sstevel@tonic-gate 		free_lofi_ioctl(klip);
21010Sstevel@tonic-gate 		return (error);
21025643Saalok 	case LOFI_CHECK_COMPRESSED:
21035643Saalok 		mutex_enter(&lofi_lock);
21045643Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
21055643Saalok 		mutex_exit(&lofi_lock);
21065643Saalok 		if (klip->li_minor == 0) {
21075643Saalok 			free_lofi_ioctl(klip);
21085643Saalok 			return (ENOENT);
21095643Saalok 		}
21105643Saalok 		mutex_enter(&lofi_lock);
21115643Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
21125643Saalok 		if (lsp == NULL) {
21135643Saalok 			mutex_exit(&lofi_lock);
21145643Saalok 			free_lofi_ioctl(klip);
21155643Saalok 			return (ENXIO);
21165643Saalok 		}
21175643Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
21185643Saalok 
21195643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
21205643Saalok 		    sizeof (klip->li_algorithm));
21215643Saalok 		mutex_exit(&lofi_lock);
21225643Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21235643Saalok 		free_lofi_ioctl(klip);
21245643Saalok 		return (error);
21250Sstevel@tonic-gate 	default:
21260Sstevel@tonic-gate 		free_lofi_ioctl(klip);
21270Sstevel@tonic-gate 		return (EINVAL);
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate static int
21330Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
21340Sstevel@tonic-gate     int *rvalp)
21350Sstevel@tonic-gate {
21360Sstevel@tonic-gate 	int	error;
21370Sstevel@tonic-gate 	enum dkio_state dkstate;
21380Sstevel@tonic-gate 	struct lofi_state *lsp;
21390Sstevel@tonic-gate 	minor_t	minor;
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	minor = getminor(dev);
21420Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
21430Sstevel@tonic-gate 	if (minor == 0) {
21440Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		/*
21470Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
21480Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
21490Sstevel@tonic-gate 		 * long as they can open it read-only.
21500Sstevel@tonic-gate 		 */
21510Sstevel@tonic-gate 		switch (cmd) {
21520Sstevel@tonic-gate 		case LOFI_MAP_FILE:
21530Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21540Sstevel@tonic-gate 				return (EPERM);
21551657Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
21560Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
21570Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21580Sstevel@tonic-gate 				return (EPERM);
21591657Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
21600Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
21610Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21620Sstevel@tonic-gate 				return (EPERM);
21631657Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
21640Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
21650Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21660Sstevel@tonic-gate 				return (EPERM);
21671657Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
21680Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
21690Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
21701657Sheppo 			    credp, flag));
21710Sstevel@tonic-gate 		case LOFI_GET_MINOR:
21720Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
21731657Sheppo 			    credp, flag));
21740Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
21751657Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
21761657Sheppo 			    sizeof (lofi_max_files), flag);
21770Sstevel@tonic-gate 			if (error)
21780Sstevel@tonic-gate 				return (EFAULT);
21790Sstevel@tonic-gate 			return (0);
21805643Saalok 		case LOFI_CHECK_COMPRESSED:
21815643Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
21825643Saalok 			    credp, flag));
21830Sstevel@tonic-gate 		default:
21840Sstevel@tonic-gate 			break;
21850Sstevel@tonic-gate 		}
21860Sstevel@tonic-gate 	}
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
21890Sstevel@tonic-gate 	if (lsp == NULL)
21900Sstevel@tonic-gate 		return (ENXIO);
21910Sstevel@tonic-gate 
21924451Seschrock 	/*
21934451Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
21944451Seschrock 	 * EIO as if the device was no longer present.
21954451Seschrock 	 */
21964451Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
21974451Seschrock 		return (EIO);
21984451Seschrock 
21990Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
22000Sstevel@tonic-gate 	switch (cmd) {
22010Sstevel@tonic-gate 	case DKIOCGVTOC:
22020Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
22030Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
22040Sstevel@tonic-gate 			struct vtoc32 vtoc32;
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
22070Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
22080Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
22090Sstevel@tonic-gate 				return (EFAULT);
22100Sstevel@tonic-gate 				break;
22110Sstevel@tonic-gate 			}
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		case DDI_MODEL_NONE:
22140Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
22150Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
22160Sstevel@tonic-gate 				return (EFAULT);
22170Sstevel@tonic-gate 			break;
22180Sstevel@tonic-gate 		}
22190Sstevel@tonic-gate 		return (0);
22200Sstevel@tonic-gate 	case DKIOCINFO:
22211657Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
22221657Sheppo 		    sizeof (struct dk_cinfo), flag);
22230Sstevel@tonic-gate 		if (error)
22240Sstevel@tonic-gate 			return (EFAULT);
22250Sstevel@tonic-gate 		return (0);
22260Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
22270Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
22280Sstevel@tonic-gate 	case DKIOCGGEOM:
22291657Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
22301657Sheppo 		    sizeof (struct dk_geom), flag);
22310Sstevel@tonic-gate 		if (error)
22320Sstevel@tonic-gate 			return (EFAULT);
22330Sstevel@tonic-gate 		return (0);
22340Sstevel@tonic-gate 	case DKIOCSTATE:
22354451Seschrock 		/*
22364451Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
22374451Seschrock 		 * a device is forcefully unmapped, then the device transitions
22384451Seschrock 		 * to the DKIO_DEV_GONE state.
22394451Seschrock 		 */
22404451Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
22414451Seschrock 		    flag) != 0)
22424451Seschrock 			return (EFAULT);
22434451Seschrock 
22444451Seschrock 		mutex_enter(&lsp->ls_vp_lock);
22454451Seschrock 		while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
22464451Seschrock 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
22474451Seschrock 			/*
22484451Seschrock 			 * By virtue of having the device open, we know that
22494451Seschrock 			 * 'lsp' will remain valid when we return.
22504451Seschrock 			 */
22514451Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
22524451Seschrock 			    &lsp->ls_vp_lock)) {
22534451Seschrock 				mutex_exit(&lsp->ls_vp_lock);
22544451Seschrock 				return (EINTR);
22554451Seschrock 			}
22564451Seschrock 		}
22574451Seschrock 
22584451Seschrock 		dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
22594451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
22604451Seschrock 
22614451Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
22624451Seschrock 		    sizeof (dkstate), flag) != 0)
22630Sstevel@tonic-gate 			return (EFAULT);
22640Sstevel@tonic-gate 		return (0);
22650Sstevel@tonic-gate 	default:
22660Sstevel@tonic-gate 		return (ENOTTY);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate }
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
22710Sstevel@tonic-gate 	lofi_open,		/* open */
22720Sstevel@tonic-gate 	lofi_close,		/* close */
22730Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
22740Sstevel@tonic-gate 	nodev,			/* print */
22750Sstevel@tonic-gate 	nodev,			/* dump */
22760Sstevel@tonic-gate 	lofi_read,		/* read */
22770Sstevel@tonic-gate 	lofi_write,		/* write */
22780Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
22790Sstevel@tonic-gate 	nodev,			/* devmap */
22800Sstevel@tonic-gate 	nodev,			/* mmap */
22810Sstevel@tonic-gate 	nodev,			/* segmap */
22820Sstevel@tonic-gate 	nochpoll,		/* poll */
22830Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
22840Sstevel@tonic-gate 	0,			/* streamtab  */
22850Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
22860Sstevel@tonic-gate 	CB_REV,
22870Sstevel@tonic-gate 	lofi_aread,
22880Sstevel@tonic-gate 	lofi_awrite
22890Sstevel@tonic-gate };
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate static struct dev_ops lofi_ops = {
22920Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
22930Sstevel@tonic-gate 	0,			/* refcnt  */
22940Sstevel@tonic-gate 	lofi_info,		/* info */
22950Sstevel@tonic-gate 	nulldev,		/* identify */
22960Sstevel@tonic-gate 	nulldev,		/* probe */
22970Sstevel@tonic-gate 	lofi_attach,		/* attach */
22980Sstevel@tonic-gate 	lofi_detach,		/* detach */
22990Sstevel@tonic-gate 	nodev,			/* reset */
23000Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
23017656SSherry.Moore@Sun.COM 	NULL,			/* no bus operations */
23027656SSherry.Moore@Sun.COM 	NULL,			/* power */
23038313SDina.Nimeh@Sun.Com 	ddi_quiesce_not_needed,	/* quiesce */
23040Sstevel@tonic-gate };
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate static struct modldrv modldrv = {
23070Sstevel@tonic-gate 	&mod_driverops,
23087656SSherry.Moore@Sun.COM 	"loopback file driver",
23090Sstevel@tonic-gate 	&lofi_ops,
23100Sstevel@tonic-gate };
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate static struct modlinkage modlinkage = {
23130Sstevel@tonic-gate 	MODREV_1,
23140Sstevel@tonic-gate 	&modldrv,
23150Sstevel@tonic-gate 	NULL
23160Sstevel@tonic-gate };
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate int
23190Sstevel@tonic-gate _init(void)
23200Sstevel@tonic-gate {
23210Sstevel@tonic-gate 	int error;
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
23240Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
23250Sstevel@tonic-gate 	if (error)
23260Sstevel@tonic-gate 		return (error);
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
23290Sstevel@tonic-gate 	error = mod_install(&modlinkage);
23300Sstevel@tonic-gate 	if (error) {
23310Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
23320Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
23330Sstevel@tonic-gate 	}
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	return (error);
23360Sstevel@tonic-gate }
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate int
23390Sstevel@tonic-gate _fini(void)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	int	error;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	if (lofi_busy())
23440Sstevel@tonic-gate 		return (EBUSY);
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
23470Sstevel@tonic-gate 	if (error)
23480Sstevel@tonic-gate 		return (error);
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
23510Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	return (error);
23540Sstevel@tonic-gate }
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate int
23570Sstevel@tonic-gate _info(struct modinfo *modinfop)
23580Sstevel@tonic-gate {
23590Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
23600Sstevel@tonic-gate }
2361