xref: /onnv-gate/usr/src/uts/common/io/lofi.c (revision 8313:88e485ada83a)
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 /*
226734Sjohnlev  * Copyright 2008 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
92*8313SDina.Nimeh@Sun.Com  *	need to fake a geometry, however, it may be relevant if you're replacing
93*8313SDina.Nimeh@Sun.Com  *	metadisk, or using lofi to get crypto.
94*8313SDina.Nimeh@Sun.Com  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
95*8313SDina.Nimeh@Sun.Com  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
96*8313SDina.Nimeh@Sun.Com  *	In fact this even makes sense if you have lofi "above" metadisk.
970Sstevel@tonic-gate  *
98*8313SDina.Nimeh@Sun.Com  * Encryption:
99*8313SDina.Nimeh@Sun.Com  *	Each lofi device can have its own symmetric key and cipher.
100*8313SDina.Nimeh@Sun.Com  *	They are passed to us by lofiadm(1m) in the correct format for use
101*8313SDina.Nimeh@Sun.Com  *	with the misc/kcf crypto_* routines.
102*8313SDina.Nimeh@Sun.Com  *
103*8313SDina.Nimeh@Sun.Com  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
104*8313SDina.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>
133*8313SDina.Nimeh@Sun.Com #include <sys/crypto/common.h>
134*8313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h>
135*8313SDina.Nimeh@Sun.Com 
136*8313SDina.Nimeh@Sun.Com /*
137*8313SDina.Nimeh@Sun.Com  * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
138*8313SDina.Nimeh@Sun.Com  * Crypto metadata, if it exists, is located at the end of the boot block
139*8313SDina.Nimeh@Sun.Com  * (BBOFF + BBSIZE, which is SBOFF).  The super block and everything after
140*8313SDina.Nimeh@Sun.Com  * is offset by the size of the crypto metadata which is handled by
141*8313SDina.Nimeh@Sun.Com  * lsp->ls_crypto_offset.
142*8313SDina.Nimeh@Sun.Com  */
143*8313SDina.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 
148*8313SDina.Nimeh@Sun.Com #define	SETUP_C_DATA(cd, buf, len) 		\
149*8313SDina.Nimeh@Sun.Com 	(cd).cd_format = CRYPTO_DATA_RAW;	\
150*8313SDina.Nimeh@Sun.Com 	(cd).cd_offset = 0;			\
151*8313SDina.Nimeh@Sun.Com 	(cd).cd_miscdata = NULL;		\
152*8313SDina.Nimeh@Sun.Com 	(cd).cd_length = (len);			\
153*8313SDina.Nimeh@Sun.Com 	(cd).cd_raw.iov_base = (buf);		\
154*8313SDina.Nimeh@Sun.Com 	(cd).cd_raw.iov_len = (len);
155*8313SDina.Nimeh@Sun.Com 
156*8313SDina.Nimeh@Sun.Com #define	UIO_CHECK(uio)	\
157*8313SDina.Nimeh@Sun.Com 	if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
158*8313SDina.Nimeh@Sun.Com 	    ((uio)->uio_resid % DEV_BSIZE) != 0) { \
159*8313SDina.Nimeh@Sun.Com 		return (EINVAL); \
160*8313SDina.Nimeh@Sun.Com 	}
161*8313SDina.Nimeh@Sun.Com 
162*8313SDina.Nimeh@Sun.Com static dev_info_t *lofi_dip = NULL;
163*8313SDina.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;
182*8313SDina.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
260*8313SDina.Nimeh@Sun.Com lofi_free_crypto(struct lofi_state *lsp)
261*8313SDina.Nimeh@Sun.Com {
262*8313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
263*8313SDina.Nimeh@Sun.Com 
264*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
265*8313SDina.Nimeh@Sun.Com 		/*
266*8313SDina.Nimeh@Sun.Com 		 * Clean up the crypto state so that it doesn't hang around
267*8313SDina.Nimeh@Sun.Com 		 * in memory after we are done with it.
268*8313SDina.Nimeh@Sun.Com 		 */
269*8313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
270*8313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
271*8313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
272*8313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
273*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
274*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
275*8313SDina.Nimeh@Sun.Com 
276*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != NULL) {
277*8313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_mech.cm_param,
278*8313SDina.Nimeh@Sun.Com 			    lsp->ls_mech.cm_param_len);
279*8313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param = NULL;
280*8313SDina.Nimeh@Sun.Com 			lsp->ls_mech.cm_param_len = 0;
281*8313SDina.Nimeh@Sun.Com 		}
282*8313SDina.Nimeh@Sun.Com 
283*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_param != NULL) {
284*8313SDina.Nimeh@Sun.Com 			kmem_free(lsp->ls_iv_mech.cm_param,
285*8313SDina.Nimeh@Sun.Com 			    lsp->ls_iv_mech.cm_param_len);
286*8313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param = NULL;
287*8313SDina.Nimeh@Sun.Com 			lsp->ls_iv_mech.cm_param_len = 0;
288*8313SDina.Nimeh@Sun.Com 		}
289*8313SDina.Nimeh@Sun.Com 
290*8313SDina.Nimeh@Sun.Com 		mutex_destroy(&lsp->ls_crypto_lock);
291*8313SDina.Nimeh@Sun.Com 	}
292*8313SDina.Nimeh@Sun.Com }
293*8313SDina.Nimeh@Sun.Com 
294*8313SDina.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 
301*8313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lofi_lock));
302*8313SDina.Nimeh@Sun.Com 
303*8313SDina.Nimeh@Sun.Com 	lofi_free_crypto(lsp);
304*8313SDina.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 
415*8313SDina.Nimeh@Sun.Com /*
416*8313SDina.Nimeh@Sun.Com  * Sets the mechanism's initialization vector (IV) if one is needed.
417*8313SDina.Nimeh@Sun.Com  * The IV is computed from the data block number.  lsp->ls_mech is
418*8313SDina.Nimeh@Sun.Com  * altered so that:
419*8313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param_len is set to the IV len.
420*8313SDina.Nimeh@Sun.Com  *	lsp->ls_mech.cm_param is set to the IV.
421*8313SDina.Nimeh@Sun.Com  */
422*8313SDina.Nimeh@Sun.Com static int
423*8313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
424*8313SDina.Nimeh@Sun.Com {
425*8313SDina.Nimeh@Sun.Com 	int	ret;
426*8313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
427*8313SDina.Nimeh@Sun.Com 	char	*iv;
428*8313SDina.Nimeh@Sun.Com 	size_t	iv_len;
429*8313SDina.Nimeh@Sun.Com 	size_t	min;
430*8313SDina.Nimeh@Sun.Com 	void	*data;
431*8313SDina.Nimeh@Sun.Com 	size_t	datasz;
432*8313SDina.Nimeh@Sun.Com 
433*8313SDina.Nimeh@Sun.Com 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
434*8313SDina.Nimeh@Sun.Com 
435*8313SDina.Nimeh@Sun.Com 	if (lsp == NULL)
436*8313SDina.Nimeh@Sun.Com 		return (CRYPTO_DEVICE_ERROR);
437*8313SDina.Nimeh@Sun.Com 
438*8313SDina.Nimeh@Sun.Com 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
439*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_iv_type == IVM_NONE) {
440*8313SDina.Nimeh@Sun.Com 		return (CRYPTO_SUCCESS);
441*8313SDina.Nimeh@Sun.Com 	}
442*8313SDina.Nimeh@Sun.Com 
443*8313SDina.Nimeh@Sun.Com 	/*
444*8313SDina.Nimeh@Sun.Com 	 * if kmem already alloced from previous call and it's the same size
445*8313SDina.Nimeh@Sun.Com 	 * we need now, just recycle it; allocate new kmem only if we have to
446*8313SDina.Nimeh@Sun.Com 	 */
447*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param == NULL ||
448*8313SDina.Nimeh@Sun.Com 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
449*8313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_iv_len;
450*8313SDina.Nimeh@Sun.Com 		iv = kmem_zalloc(iv_len, KM_SLEEP);
451*8313SDina.Nimeh@Sun.Com 	} else {
452*8313SDina.Nimeh@Sun.Com 		iv_len = lsp->ls_mech.cm_param_len;
453*8313SDina.Nimeh@Sun.Com 		iv = lsp->ls_mech.cm_param;
454*8313SDina.Nimeh@Sun.Com 		bzero(iv, iv_len);
455*8313SDina.Nimeh@Sun.Com 	}
456*8313SDina.Nimeh@Sun.Com 
457*8313SDina.Nimeh@Sun.Com 	switch (lsp->ls_iv_type) {
458*8313SDina.Nimeh@Sun.Com 	case IVM_ENC_BLKNO:
459*8313SDina.Nimeh@Sun.Com 		/* iv is not static, lblkno changes each time */
460*8313SDina.Nimeh@Sun.Com 		data = &lblkno;
461*8313SDina.Nimeh@Sun.Com 		datasz = sizeof (lblkno);
462*8313SDina.Nimeh@Sun.Com 		break;
463*8313SDina.Nimeh@Sun.Com 	default:
464*8313SDina.Nimeh@Sun.Com 		data = 0;
465*8313SDina.Nimeh@Sun.Com 		datasz = 0;
466*8313SDina.Nimeh@Sun.Com 		break;
467*8313SDina.Nimeh@Sun.Com 	}
468*8313SDina.Nimeh@Sun.Com 
469*8313SDina.Nimeh@Sun.Com 	/*
470*8313SDina.Nimeh@Sun.Com 	 * write blkno into the iv buffer padded on the left in case
471*8313SDina.Nimeh@Sun.Com 	 * blkno ever grows bigger than its current longlong_t size
472*8313SDina.Nimeh@Sun.Com 	 * or a variation other than blkno is used for the iv data
473*8313SDina.Nimeh@Sun.Com 	 */
474*8313SDina.Nimeh@Sun.Com 	min = MIN(datasz, iv_len);
475*8313SDina.Nimeh@Sun.Com 	bcopy(data, iv + (iv_len - min), min);
476*8313SDina.Nimeh@Sun.Com 
477*8313SDina.Nimeh@Sun.Com 	/* encrypt the data in-place to get the IV */
478*8313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, iv, iv_len);
479*8313SDina.Nimeh@Sun.Com 
480*8313SDina.Nimeh@Sun.Com 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
481*8313SDina.Nimeh@Sun.Com 	    NULL, NULL, NULL);
482*8313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
483*8313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
484*8313SDina.Nimeh@Sun.Com 		    lblkno, ret);
485*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_param != iv)
486*8313SDina.Nimeh@Sun.Com 			kmem_free(iv, iv_len);
487*8313SDina.Nimeh@Sun.Com 		return (ret);
488*8313SDina.Nimeh@Sun.Com 	}
489*8313SDina.Nimeh@Sun.Com 
490*8313SDina.Nimeh@Sun.Com 	/* clean up the iv from the last computation */
491*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
492*8313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
493*8313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param_len = iv_len;
494*8313SDina.Nimeh@Sun.Com 	lsp->ls_mech.cm_param = iv;
495*8313SDina.Nimeh@Sun.Com 
496*8313SDina.Nimeh@Sun.Com 	return (CRYPTO_SUCCESS);
497*8313SDina.Nimeh@Sun.Com }
498*8313SDina.Nimeh@Sun.Com 
499*8313SDina.Nimeh@Sun.Com /*
500*8313SDina.Nimeh@Sun.Com  * Performs encryption and decryption of a chunk of data of size "len",
501*8313SDina.Nimeh@Sun.Com  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
502*8313SDina.Nimeh@Sun.Com  * DEV_BSIZE.
503*8313SDina.Nimeh@Sun.Com  */
504*8313SDina.Nimeh@Sun.Com static int
505*8313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
506*8313SDina.Nimeh@Sun.Com     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
507*8313SDina.Nimeh@Sun.Com {
508*8313SDina.Nimeh@Sun.Com 	crypto_data_t cdata;
509*8313SDina.Nimeh@Sun.Com 	crypto_data_t wdata;
510*8313SDina.Nimeh@Sun.Com 	int ret;
511*8313SDina.Nimeh@Sun.Com 	longlong_t lblkno = bp->b_lblkno;
512*8313SDina.Nimeh@Sun.Com 
513*8313SDina.Nimeh@Sun.Com 	mutex_enter(&lsp->ls_crypto_lock);
514*8313SDina.Nimeh@Sun.Com 
515*8313SDina.Nimeh@Sun.Com 	/*
516*8313SDina.Nimeh@Sun.Com 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
517*8313SDina.Nimeh@Sun.Com 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
518*8313SDina.Nimeh@Sun.Com 	 */
519*8313SDina.Nimeh@Sun.Com 	SETUP_C_DATA(cdata, plaintext, len);
520*8313SDina.Nimeh@Sun.Com 	cdata.cd_length = DEV_BSIZE;
521*8313SDina.Nimeh@Sun.Com 	if (ciphertext != NULL) {		/* not in-place crypto */
522*8313SDina.Nimeh@Sun.Com 		SETUP_C_DATA(wdata, ciphertext, len);
523*8313SDina.Nimeh@Sun.Com 		wdata.cd_length = DEV_BSIZE;
524*8313SDina.Nimeh@Sun.Com 	}
525*8313SDina.Nimeh@Sun.Com 
526*8313SDina.Nimeh@Sun.Com 	do {
527*8313SDina.Nimeh@Sun.Com 		ret = lofi_blk_mech(lsp, lblkno);
528*8313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS)
529*8313SDina.Nimeh@Sun.Com 			continue;
530*8313SDina.Nimeh@Sun.Com 
531*8313SDina.Nimeh@Sun.Com 		if (op_encrypt) {
532*8313SDina.Nimeh@Sun.Com 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
533*8313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
534*8313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
535*8313SDina.Nimeh@Sun.Com 		} else {
536*8313SDina.Nimeh@Sun.Com 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
537*8313SDina.Nimeh@Sun.Com 			    &lsp->ls_key, NULL,
538*8313SDina.Nimeh@Sun.Com 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
539*8313SDina.Nimeh@Sun.Com 		}
540*8313SDina.Nimeh@Sun.Com 
541*8313SDina.Nimeh@Sun.Com 		cdata.cd_offset += DEV_BSIZE;
542*8313SDina.Nimeh@Sun.Com 		if (ciphertext != NULL)
543*8313SDina.Nimeh@Sun.Com 			wdata.cd_offset += DEV_BSIZE;
544*8313SDina.Nimeh@Sun.Com 		lblkno++;
545*8313SDina.Nimeh@Sun.Com 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
546*8313SDina.Nimeh@Sun.Com 
547*8313SDina.Nimeh@Sun.Com 	mutex_exit(&lsp->ls_crypto_lock);
548*8313SDina.Nimeh@Sun.Com 
549*8313SDina.Nimeh@Sun.Com 	if (ret != CRYPTO_SUCCESS) {
550*8313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
551*8313SDina.Nimeh@Sun.Com 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
552*8313SDina.Nimeh@Sun.Com 		    lblkno, ret);
553*8313SDina.Nimeh@Sun.Com 	}
554*8313SDina.Nimeh@Sun.Com 
555*8313SDina.Nimeh@Sun.Com 	return (ret);
556*8313SDina.Nimeh@Sun.Com }
557*8313SDina.Nimeh@Sun.Com 
558*8313SDina.Nimeh@Sun.Com #define	RDWR_RAW	1
559*8313SDina.Nimeh@Sun.Com #define	RDWR_BCOPY	2
560*8313SDina.Nimeh@Sun.Com 
561*8313SDina.Nimeh@Sun.Com static int
562*8313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
563*8313SDina.Nimeh@Sun.Com     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
564*8313SDina.Nimeh@Sun.Com {
565*8313SDina.Nimeh@Sun.Com 	ssize_t resid;
566*8313SDina.Nimeh@Sun.Com 	int isread;
567*8313SDina.Nimeh@Sun.Com 	int error;
568*8313SDina.Nimeh@Sun.Com 
569*8313SDina.Nimeh@Sun.Com 	/*
570*8313SDina.Nimeh@Sun.Com 	 * Handles reads/writes for both plain and encrypted lofi
571*8313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
572*8313SDina.Nimeh@Sun.Com 	 * when it gets here.
573*8313SDina.Nimeh@Sun.Com 	 */
574*8313SDina.Nimeh@Sun.Com 
575*8313SDina.Nimeh@Sun.Com 	isread = bp->b_flags & B_READ;
576*8313SDina.Nimeh@Sun.Com 	if (isread) {
577*8313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
578*8313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
579*8313SDina.Nimeh@Sun.Com 			bcopy(bcopy_locn, bufaddr, len);
580*8313SDina.Nimeh@Sun.Com 			error = 0;
581*8313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
582*8313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
583*8313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
584*8313SDina.Nimeh@Sun.Com 			    &resid);
585*8313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
586*8313SDina.Nimeh@Sun.Com 		}
587*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled && error == 0) {
588*8313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
589*8313SDina.Nimeh@Sun.Com 			    B_FALSE) != CRYPTO_SUCCESS) {
590*8313SDina.Nimeh@Sun.Com 				/*
591*8313SDina.Nimeh@Sun.Com 				 * XXX: original code didn't set residual
592*8313SDina.Nimeh@Sun.Com 				 * back to len because no error was expected
593*8313SDina.Nimeh@Sun.Com 				 * from bcopy() if encryption is not enabled
594*8313SDina.Nimeh@Sun.Com 				 */
595*8313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
596*8313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
597*8313SDina.Nimeh@Sun.Com 				error = EIO;
598*8313SDina.Nimeh@Sun.Com 			}
599*8313SDina.Nimeh@Sun.Com 		}
600*8313SDina.Nimeh@Sun.Com 		return (error);
601*8313SDina.Nimeh@Sun.Com 	} else {
602*8313SDina.Nimeh@Sun.Com 		void *iobuf = bufaddr;
603*8313SDina.Nimeh@Sun.Com 
604*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
605*8313SDina.Nimeh@Sun.Com 			/* don't do in-place crypto to keep bufaddr intact */
606*8313SDina.Nimeh@Sun.Com 			iobuf = kmem_alloc(len, KM_SLEEP);
607*8313SDina.Nimeh@Sun.Com 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
608*8313SDina.Nimeh@Sun.Com 			    B_TRUE) != CRYPTO_SUCCESS) {
609*8313SDina.Nimeh@Sun.Com 				kmem_free(iobuf, len);
610*8313SDina.Nimeh@Sun.Com 				if (method != RDWR_BCOPY)
611*8313SDina.Nimeh@Sun.Com 					bp->b_resid = len;
612*8313SDina.Nimeh@Sun.Com 				return (EIO);
613*8313SDina.Nimeh@Sun.Com 			}
614*8313SDina.Nimeh@Sun.Com 		}
615*8313SDina.Nimeh@Sun.Com 		if (method == RDWR_BCOPY) {
616*8313SDina.Nimeh@Sun.Com 			/* DO NOT update bp->b_resid for bcopy */
617*8313SDina.Nimeh@Sun.Com 			bcopy(iobuf, bcopy_locn, len);
618*8313SDina.Nimeh@Sun.Com 			error = 0;
619*8313SDina.Nimeh@Sun.Com 		} else {		/* RDWR_RAW */
620*8313SDina.Nimeh@Sun.Com 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
621*8313SDina.Nimeh@Sun.Com 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
622*8313SDina.Nimeh@Sun.Com 			    &resid);
623*8313SDina.Nimeh@Sun.Com 			bp->b_resid = resid;
624*8313SDina.Nimeh@Sun.Com 		}
625*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_crypto_enabled) {
626*8313SDina.Nimeh@Sun.Com 			kmem_free(iobuf, len);
627*8313SDina.Nimeh@Sun.Com 		}
628*8313SDina.Nimeh@Sun.Com 		return (error);
629*8313SDina.Nimeh@Sun.Com 	}
630*8313SDina.Nimeh@Sun.Com }
631*8313SDina.Nimeh@Sun.Com 
6325643Saalok static int
6335643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
634*8313SDina.Nimeh@Sun.Com     struct lofi_state *lsp)
6355643Saalok {
6365643Saalok 	int error;
6375643Saalok 	offset_t alignedoffset, mapoffset;
6385643Saalok 	size_t	xfersize;
6395643Saalok 	int	isread;
640*8313SDina.Nimeh@Sun.Com 	int	smflags;
6415643Saalok 	caddr_t	mapaddr;
6425643Saalok 	size_t	len;
6435643Saalok 	enum seg_rw srw;
644*8313SDina.Nimeh@Sun.Com 	int	save_error;
645*8313SDina.Nimeh@Sun.Com 
646*8313SDina.Nimeh@Sun.Com 	/*
647*8313SDina.Nimeh@Sun.Com 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
648*8313SDina.Nimeh@Sun.Com 	 * when it gets here.
649*8313SDina.Nimeh@Sun.Com 	 */
650*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled)
651*8313SDina.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
662*8313SDina.Nimeh@Sun.Com 	 *
663*8313SDina.Nimeh@Sun.Com 	 * Visually, where "|" represents page map boundaries:
664*8313SDina.Nimeh@Sun.Com 	 *   alignedoffset (mapaddr begins at this segmap boundary)
665*8313SDina.Nimeh@Sun.Com 	 *    |   offset (from beginning of file)
666*8313SDina.Nimeh@Sun.Com 	 *    |    |	   len
667*8313SDina.Nimeh@Sun.Com 	 *    v    v	    v
668*8313SDina.Nimeh@Sun.Com 	 * ===|====X========|====...======|========X====|====
669*8313SDina.Nimeh@Sun.Com 	 *	   /-------------...---------------/
670*8313SDina.Nimeh@Sun.Com 	 *		^ bp->b_bcount/bp->b_resid at start
671*8313SDina.Nimeh@Sun.Com 	 *    /----/--------/----...------/--------/
672*8313SDina.Nimeh@Sun.Com 	 *	^	^	^   ^		^
673*8313SDina.Nimeh@Sun.Com 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
674*8313SDina.Nimeh@Sun.Com 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
675*8313SDina.Nimeh@Sun.Com 	 *	|	1st xfersize (<= MAXBSIZE)
676*8313SDina.Nimeh@Sun.Com 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
677*8313SDina.Nimeh@Sun.Com 	 *
678*8313SDina.Nimeh@Sun.Com 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
679*8313SDina.Nimeh@Sun.Com 	 * MAXBSIZE boundary.  "len" is next page boundary of size
680*8313SDina.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));
690*8313SDina.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 		}
711*8313SDina.Nimeh@Sun.Com 		/* error may be non-zero for encrypted lofi */
712*8313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
713*8313SDina.Nimeh@Sun.Com 		    RDWR_BCOPY, mapaddr + mapoffset);
714*8313SDina.Nimeh@Sun.Com 		if (error == 0) {
715*8313SDina.Nimeh@Sun.Com 			bp->b_resid -= xfersize;
716*8313SDina.Nimeh@Sun.Com 			bufaddr += xfersize;
717*8313SDina.Nimeh@Sun.Com 			offset += xfersize;
718*8313SDina.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 {
731*8313SDina.Nimeh@Sun.Com 			if (error == 0)		/* write back good pages */
732*8313SDina.Nimeh@Sun.Com 				smflags |= SM_WRITE;
7335643Saalok 		}
7345643Saalok 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
7355643Saalok 		    len, F_SOFTUNLOCK, srw);
736*8313SDina.Nimeh@Sun.Com 		save_error = segmap_release(segkmap, mapaddr, smflags);
737*8313SDina.Nimeh@Sun.Com 		if (error == 0)
738*8313SDina.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;
769*8313SDina.Nimeh@Sun.Com 	offset_t offset;
770*8313SDina.Nimeh@Sun.Com 	caddr_t	bufaddr;
771*8313SDina.Nimeh@Sun.Com 	size_t	len;
772*8313SDina.Nimeh@Sun.Com 	size_t	xfersize;
773*8313SDina.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));
776*8313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
777*8313SDina.Nimeh@Sun.Com 		error = ENXIO;
778*8313SDina.Nimeh@Sun.Com 		goto errout;
779*8313SDina.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;
787*8313SDina.Nimeh@Sun.Com #ifdef _LP64
7880Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
789*8313SDina.Nimeh@Sun.Com #else
790*8313SDina.Nimeh@Sun.Com 	offset = bp->b_blkno * DEV_BSIZE;	/* offset within file */
791*8313SDina.Nimeh@Sun.Com #endif /* _LP64 */
792*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
793*8313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
794*8313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
795*8313SDina.Nimeh@Sun.Com 	}
796*8313SDina.Nimeh@Sun.Com 	len = bp->b_bcount;
797*8313SDina.Nimeh@Sun.Com 	bufinited = B_TRUE;
798*8313SDina.Nimeh@Sun.Com 
799*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
800*8313SDina.Nimeh@Sun.Com 		error = EIO;
801*8313SDina.Nimeh@Sun.Com 		goto errout;
802*8313SDina.Nimeh@Sun.Com 	}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
8060Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
8070Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
8080Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
809*8313SDina.Nimeh@Sun.Com 	 * mapable or it's a character device or it's an encrypted lofi.
8100Sstevel@tonic-gate 	 */
811*8313SDina.Nimeh@Sun.Com 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
812*8313SDina.Nimeh@Sun.Com 	    lsp->ls_crypto_enabled) {
813*8313SDina.Nimeh@Sun.Com 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
814*8313SDina.Nimeh@Sun.Com 		    NULL);
815*8313SDina.Nimeh@Sun.Com 	} else if (lsp->ls_uncomp_seg_sz == 0) {
816*8313SDina.Nimeh@Sun.Com 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
817*8313SDina.Nimeh@Sun.Com 	} else {
818*8313SDina.Nimeh@Sun.Com 		unsigned char *compressed_seg = NULL, *cmpbuf;
819*8313SDina.Nimeh@Sun.Com 		unsigned char *uncompressed_seg = NULL;
820*8313SDina.Nimeh@Sun.Com 		lofi_compress_info_t *li;
821*8313SDina.Nimeh@Sun.Com 		size_t oblkcount;
822*8313SDina.Nimeh@Sun.Com 		unsigned long seglen;
823*8313SDina.Nimeh@Sun.Com 		uint64_t sblkno, eblkno, cmpbytes;
824*8313SDina.Nimeh@Sun.Com 		offset_t sblkoff, eblkoff;
825*8313SDina.Nimeh@Sun.Com 		u_offset_t salign, ealign;
826*8313SDina.Nimeh@Sun.Com 		u_offset_t sdiff;
827*8313SDina.Nimeh@Sun.Com 		uint32_t comp_data_sz;
8285643Saalok 		uint64_t i;
8295643Saalok 
8300Sstevel@tonic-gate 		/*
8315643Saalok 		 * From here on we're dealing primarily with compressed files
8325643Saalok 		 */
833*8313SDina.Nimeh@Sun.Com 		ASSERT(!lsp->ls_crypto_enabled);
8345643Saalok 
8355643Saalok 		/*
8365643Saalok 		 * Compressed files can only be read from and
8375643Saalok 		 * not written to
8380Sstevel@tonic-gate 		 */
8395643Saalok 		if (!(bp->b_flags & B_READ)) {
8405643Saalok 			bp->b_resid = bp->b_bcount;
8415643Saalok 			error = EROFS;
8425643Saalok 			goto done;
8435643Saalok 		}
8445643Saalok 
8455643Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
8465643Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
8475643Saalok 		/*
8485643Saalok 		 * Compute starting and ending compressed segment numbers
8495643Saalok 		 * We use only bitwise operations avoiding division and
8505643Saalok 		 * modulus because we enforce the compression segment size
8515643Saalok 		 * to a power of 2
8525643Saalok 		 */
8535643Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
8545643Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
8555643Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
8565643Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
8575643Saalok 
8585643Saalok 		/*
8595643Saalok 		 * Align start offset to block boundary for segmap
8605643Saalok 		 */
8615643Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
8625643Saalok 		sdiff = salign & (DEV_BSIZE - 1);
8635643Saalok 		salign -= sdiff;
8645643Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
8650Sstevel@tonic-gate 			/*
8665643Saalok 			 * We're dealing with the last segment of
8675643Saalok 			 * the compressed file -- the size of this
8685643Saalok 			 * segment *may not* be the same as the
8695643Saalok 			 * segment size for the file
8700Sstevel@tonic-gate 			 */
8715643Saalok 			eblkoff = (offset + bp->b_bcount) &
8725643Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
8735643Saalok 			ealign = lsp->ls_vp_comp_size;
8745643Saalok 		} else {
8755643Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
8765643Saalok 		}
8775643Saalok 
8785643Saalok 		/*
8795643Saalok 		 * Preserve original request paramaters
8805643Saalok 		 */
8815643Saalok 		oblkcount = bp->b_bcount;
8825643Saalok 
8835643Saalok 		/*
8845643Saalok 		 * Assign the calculated parameters
8855643Saalok 		 */
8865643Saalok 		comp_data_sz = ealign - salign;
8875643Saalok 		bp->b_bcount = comp_data_sz;
8885643Saalok 
8895643Saalok 		/*
8905643Saalok 		 * Allocate fixed size memory blocks to hold compressed
8915643Saalok 		 * segments and one uncompressed segment since we
8925643Saalok 		 * uncompress segments one at a time
8935643Saalok 		 */
8945643Saalok 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
8955643Saalok 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
8965643Saalok 		/*
8975643Saalok 		 * Map in the calculated number of blocks
8985643Saalok 		 */
8995643Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
9005643Saalok 		    bp, lsp);
9015643Saalok 
9025643Saalok 		bp->b_bcount = oblkcount;
9035643Saalok 		bp->b_resid = oblkcount;
9045643Saalok 		if (error != 0)
9055643Saalok 			goto done;
9065643Saalok 
9075643Saalok 		/*
9085643Saalok 		 * We have the compressed blocks, now uncompress them
9095643Saalok 		 */
9105643Saalok 		cmpbuf = compressed_seg + sdiff;
9115643Saalok 		for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz;
9125643Saalok 		    i++) {
9135643Saalok 			/*
9145643Saalok 			 * Each of the segment index entries contains
9155643Saalok 			 * the starting block number for that segment.
9165643Saalok 			 * The number of compressed bytes in a segment
9175643Saalok 			 * is thus the difference between the starting
9185643Saalok 			 * block number of this segment and the starting
9195643Saalok 			 * block number of the next segment.
9205643Saalok 			 */
9215643Saalok 			if ((i == eblkno) &&
9225643Saalok 			    (i == lsp->ls_comp_index_sz - 1)) {
9235643Saalok 				cmpbytes = lsp->ls_vp_comp_size -
9245643Saalok 				    lsp->ls_comp_seg_index[i];
9250Sstevel@tonic-gate 			} else {
9265643Saalok 				cmpbytes = lsp->ls_comp_seg_index[i + 1] -
9275643Saalok 				    lsp->ls_comp_seg_index[i];
9280Sstevel@tonic-gate 			}
9295643Saalok 
9305643Saalok 			/*
9315643Saalok 			 * The first byte in a compressed segment is a flag
9325643Saalok 			 * that indicates whether this segment is compressed
9335643Saalok 			 * at all
9345643Saalok 			 */
9355643Saalok 			if (*cmpbuf == UNCOMPRESSED) {
9365643Saalok 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
9375643Saalok 				    (cmpbytes - SEGHDR));
9385643Saalok 			} else {
9395643Saalok 				seglen = lsp->ls_uncomp_seg_sz;
9405643Saalok 
9415643Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
9425643Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
9435643Saalok 				    &seglen, li->l_level) != 0) {
9445643Saalok 					error = EIO;
9455643Saalok 					goto done;
9465643Saalok 				}
9475643Saalok 			}
9485643Saalok 
9495643Saalok 			/*
9505643Saalok 			 * Determine how much uncompressed data we
9515643Saalok 			 * have to copy and copy it
9525643Saalok 			 */
9535643Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
9545643Saalok 			if (i == eblkno) {
9555643Saalok 				if (i == (lsp->ls_comp_index_sz - 1))
9565643Saalok 					xfersize -= (lsp->ls_uncomp_last_seg_sz
9575643Saalok 					    - eblkoff);
9585643Saalok 				else
9595643Saalok 					xfersize -=
9605643Saalok 					    (lsp->ls_uncomp_seg_sz - eblkoff);
9615643Saalok 			}
9625643Saalok 
9635643Saalok 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
9645643Saalok 
9655643Saalok 			cmpbuf += cmpbytes;
9660Sstevel@tonic-gate 			bufaddr += xfersize;
9675643Saalok 			bp->b_resid -= xfersize;
9685643Saalok 			sblkoff = 0;
9695643Saalok 
9705643Saalok 			if (bp->b_resid == 0)
9715643Saalok 				break;
9725643Saalok 		}
973*8313SDina.Nimeh@Sun.Com done:
974*8313SDina.Nimeh@Sun.Com 		if (compressed_seg != NULL)
975*8313SDina.Nimeh@Sun.Com 			kmem_free(compressed_seg, comp_data_sz);
976*8313SDina.Nimeh@Sun.Com 		if (uncompressed_seg != NULL)
977*8313SDina.Nimeh@Sun.Com 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
978*8313SDina.Nimeh@Sun.Com 	} /* end of handling compressed files */
9790Sstevel@tonic-gate 
980*8313SDina.Nimeh@Sun.Com errout:
981*8313SDina.Nimeh@Sun.Com 	if (bufinited && lsp->ls_kstat) {
9820Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
9830Sstevel@tonic-gate 		kstat_io_t *kioptr;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
9860Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
9870Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
9880Sstevel@tonic-gate 			kioptr->nread += n_done;
9890Sstevel@tonic-gate 			kioptr->reads++;
9900Sstevel@tonic-gate 		} else {
9910Sstevel@tonic-gate 			kioptr->nwritten += n_done;
9920Sstevel@tonic-gate 			kioptr->writes++;
9930Sstevel@tonic-gate 		}
9940Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
9950Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
9960Sstevel@tonic-gate 	}
9974451Seschrock 
9984451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
9994451Seschrock 	if (--lsp->ls_vp_iocount == 0)
10004451Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
10014451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
10024451Seschrock 
10030Sstevel@tonic-gate 	bioerror(bp, error);
10040Sstevel@tonic-gate 	biodone(bp);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate static int
10080Sstevel@tonic-gate lofi_strategy(struct buf *bp)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	struct lofi_state *lsp;
10110Sstevel@tonic-gate 	offset_t	offset;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	/*
10140Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
10150Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
10160Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
10170Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
10180Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
10190Sstevel@tonic-gate 	 *
10200Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
10210Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
10220Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
1025*8313SDina.Nimeh@Sun.Com 	if (lsp == NULL) {
1026*8313SDina.Nimeh@Sun.Com 		bioerror(bp, ENXIO);
1027*8313SDina.Nimeh@Sun.Com 		biodone(bp);
1028*8313SDina.Nimeh@Sun.Com 		return (0);
1029*8313SDina.Nimeh@Sun.Com 	}
1030*8313SDina.Nimeh@Sun.Com 
10314451Seschrock 	mutex_enter(&lsp->ls_vp_lock);
10324451Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
10334451Seschrock 		bioerror(bp, EIO);
10344451Seschrock 		biodone(bp);
10354451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
10364451Seschrock 		return (0);
10374451Seschrock 	}
10384451Seschrock 
1039*8313SDina.Nimeh@Sun.Com #ifdef _LP64
10400Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
1041*8313SDina.Nimeh@Sun.Com #else
1042*8313SDina.Nimeh@Sun.Com 	offset = bp->b_blkno * DEV_BSIZE;	/* offset within file */
1043*8313SDina.Nimeh@Sun.Com #endif /* _LP64 */
1044*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
1045*8313SDina.Nimeh@Sun.Com 		/* encrypted data really begins after crypto header */
1046*8313SDina.Nimeh@Sun.Com 		offset += lsp->ls_crypto_offset;
1047*8313SDina.Nimeh@Sun.Com 	}
10480Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
10490Sstevel@tonic-gate 		/* EOF */
10500Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
10510Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
10520Sstevel@tonic-gate 			bioerror(bp, 0);
10530Sstevel@tonic-gate 		} else {
10540Sstevel@tonic-gate 			/* writes should fail */
10550Sstevel@tonic-gate 			bioerror(bp, ENXIO);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 		biodone(bp);
10584451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
10590Sstevel@tonic-gate 		return (0);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
10620Sstevel@tonic-gate 		bioerror(bp, ENXIO);
10630Sstevel@tonic-gate 		biodone(bp);
10644451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
10650Sstevel@tonic-gate 		return (0);
10660Sstevel@tonic-gate 	}
10674451Seschrock 	lsp->ls_vp_iocount++;
10684451Seschrock 	mutex_exit(&lsp->ls_vp_lock);
10694451Seschrock 
10700Sstevel@tonic-gate 	if (lsp->ls_kstat) {
10710Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
10720Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
10730Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
10760Sstevel@tonic-gate 	return (0);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate /*ARGSUSED2*/
10800Sstevel@tonic-gate static int
10810Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	if (getminor(dev) == 0)
10840Sstevel@tonic-gate 		return (EINVAL);
1085*8313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
10860Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*ARGSUSED2*/
10900Sstevel@tonic-gate static int
10910Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	if (getminor(dev) == 0)
10940Sstevel@tonic-gate 		return (EINVAL);
1095*8313SDina.Nimeh@Sun.Com 	UIO_CHECK(uio);
10960Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate /*ARGSUSED2*/
11000Sstevel@tonic-gate static int
11010Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate 	if (getminor(dev) == 0)
11040Sstevel@tonic-gate 		return (EINVAL);
1105*8313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
11060Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*ARGSUSED2*/
11100Sstevel@tonic-gate static int
11110Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	if (getminor(dev) == 0)
11140Sstevel@tonic-gate 		return (EINVAL);
1115*8313SDina.Nimeh@Sun.Com 	UIO_CHECK(aio->aio_uio);
11160Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate /*ARGSUSED*/
11200Sstevel@tonic-gate static int
11210Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	switch (infocmd) {
11240Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
11250Sstevel@tonic-gate 		*result = lofi_dip;
11260Sstevel@tonic-gate 		return (DDI_SUCCESS);
11270Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
11280Sstevel@tonic-gate 		*result = 0;
11290Sstevel@tonic-gate 		return (DDI_SUCCESS);
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	return (DDI_FAILURE);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate static int
11350Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	int	error;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
11400Sstevel@tonic-gate 		return (DDI_FAILURE);
11410Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
11420Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
11430Sstevel@tonic-gate 		return (DDI_FAILURE);
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
11460Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
11470Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
11480Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
11490Sstevel@tonic-gate 		return (DDI_FAILURE);
11500Sstevel@tonic-gate 	}
11515084Sjohnlev 	/* driver handles kernel-issued IOCTLs */
11525084Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
11535084Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
11545084Sjohnlev 		ddi_remove_minor_node(dip, NULL);
11555084Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
11565084Sjohnlev 		return (DDI_FAILURE);
11575084Sjohnlev 	}
11580Sstevel@tonic-gate 	lofi_dip = dip;
11590Sstevel@tonic-gate 	ddi_report_dev(dip);
11600Sstevel@tonic-gate 	return (DDI_SUCCESS);
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate static int
11640Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
11670Sstevel@tonic-gate 		return (DDI_FAILURE);
11680Sstevel@tonic-gate 	if (lofi_busy())
11690Sstevel@tonic-gate 		return (DDI_FAILURE);
11700Sstevel@tonic-gate 	lofi_dip = NULL;
11710Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
11725084Sjohnlev 	ddi_prop_remove_all(dip);
11730Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
11740Sstevel@tonic-gate 	return (DDI_SUCCESS);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate /*
1178*8313SDina.Nimeh@Sun.Com  * With addition of encryption, be careful that encryption key is wiped before
1179*8313SDina.Nimeh@Sun.Com  * kernel memory structures are freed, and also that key is not accidentally
1180*8313SDina.Nimeh@Sun.Com  * passed out into userland structures.
1181*8313SDina.Nimeh@Sun.Com  */
1182*8313SDina.Nimeh@Sun.Com static void
1183*8313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip)
1184*8313SDina.Nimeh@Sun.Com {
1185*8313SDina.Nimeh@Sun.Com 	/* Make sure this encryption key doesn't stick around */
1186*8313SDina.Nimeh@Sun.Com 	bzero(klip->li_key, sizeof (klip->li_key));
1187*8313SDina.Nimeh@Sun.Com 	kmem_free(klip, sizeof (struct lofi_ioctl));
1188*8313SDina.Nimeh@Sun.Com }
1189*8313SDina.Nimeh@Sun.Com 
1190*8313SDina.Nimeh@Sun.Com /*
11910Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
11920Sstevel@tonic-gate  * the lofi_ioctl structure.
11930Sstevel@tonic-gate  */
11940Sstevel@tonic-gate struct lofi_ioctl *
11951657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
11960Sstevel@tonic-gate {
11970Sstevel@tonic-gate 	struct lofi_ioctl *klip;
11980Sstevel@tonic-gate 	int	error;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
12011657Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
12020Sstevel@tonic-gate 	if (error) {
1203*8313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
12040Sstevel@tonic-gate 		return (NULL);
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
1208*8313SDina.Nimeh@Sun.Com 	klip->li_filename[MAXPATHLEN-1] = '\0';
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/* validate minor number */
12110Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
1212*8313SDina.Nimeh@Sun.Com 		free_lofi_ioctl(klip);
1213*8313SDina.Nimeh@Sun.Com 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
1214*8313SDina.Nimeh@Sun.Com 		    lofi_max_files);
12150Sstevel@tonic-gate 		return (NULL);
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 	return (klip);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate int
12211657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
12221657Sheppo 	int flag)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate 	int	error;
12250Sstevel@tonic-gate 
1226*8313SDina.Nimeh@Sun.Com 	/*
1227*8313SDina.Nimeh@Sun.Com 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
1228*8313SDina.Nimeh@Sun.Com 	 * This ensures that an attacker can't trivially find the
1229*8313SDina.Nimeh@Sun.Com 	 * key for a mapping just by issuing the ioctl.
1230*8313SDina.Nimeh@Sun.Com 	 *
1231*8313SDina.Nimeh@Sun.Com 	 * It can still be found by poking around in kmem with mdb(1),
1232*8313SDina.Nimeh@Sun.Com 	 * but there is no point in making it easy when the info isn't
1233*8313SDina.Nimeh@Sun.Com 	 * of any use in this direction anyway.
1234*8313SDina.Nimeh@Sun.Com 	 *
1235*8313SDina.Nimeh@Sun.Com 	 * Either way we don't actually have the raw key stored in
1236*8313SDina.Nimeh@Sun.Com 	 * a form that we can get it anyway, since we just used it
1237*8313SDina.Nimeh@Sun.Com 	 * to create a ctx template and didn't keep "the original".
1238*8313SDina.Nimeh@Sun.Com 	 */
12391657Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
12400Sstevel@tonic-gate 	if (error)
12410Sstevel@tonic-gate 		return (EFAULT);
12420Sstevel@tonic-gate 	return (0);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
12470Sstevel@tonic-gate  */
12480Sstevel@tonic-gate static int
12490Sstevel@tonic-gate file_to_minor(char *filename)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	minor_t	minor;
12520Sstevel@tonic-gate 	struct lofi_state *lsp;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
12550Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
12560Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
12570Sstevel@tonic-gate 		if (lsp == NULL)
12580Sstevel@tonic-gate 			continue;
12590Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
12600Sstevel@tonic-gate 			return (minor);
12610Sstevel@tonic-gate 	}
12620Sstevel@tonic-gate 	return (0);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate /*
12660Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
12670Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
12680Sstevel@tonic-gate  */
12690Sstevel@tonic-gate static int
12700Sstevel@tonic-gate valid_filename(const char *filename)
12710Sstevel@tonic-gate {
12720Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
12730Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	/* must be absolute path */
12760Sstevel@tonic-gate 	if (filename[0] != '/')
12770Sstevel@tonic-gate 		return (0);
12780Sstevel@tonic-gate 	/* must not be lofi */
12790Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
12800Sstevel@tonic-gate 		return (0);
12810Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
12820Sstevel@tonic-gate 		return (0);
12830Sstevel@tonic-gate 	return (1);
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
12880Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
12890Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
12900Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
12913517Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
12920Sstevel@tonic-gate  * have to support it.
12930Sstevel@tonic-gate  */
12940Sstevel@tonic-gate static void
12950Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
12960Sstevel@tonic-gate {
1297*8313SDina.Nimeh@Sun.Com 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
1298*8313SDina.Nimeh@Sun.Com 
12990Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
13000Sstevel@tonic-gate 	/*
13010Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
13020Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
13030Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
13040Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
13050Sstevel@tonic-gate 	 * a number between one and one).
13060Sstevel@tonic-gate 	 *
13070Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
13080Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
13090Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
13100Sstevel@tonic-gate 	 */
1311*8313SDina.Nimeh@Sun.Com 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
1312*8313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
13130Sstevel@tonic-gate 	else
1314*8313SDina.Nimeh@Sun.Com 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
13150Sstevel@tonic-gate 	/* in case file file is < 100k */
13160Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
13170Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
13180Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
13190Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
13200Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
13210Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
13220Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
13230Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
13240Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
13250Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
13260Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
13270Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
1328*8313SDina.Nimeh@Sun.Com 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
13290Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
13300Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
13330Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
13340Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
13350Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
13360Sstevel@tonic-gate 	bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7);
13370Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
13380Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
13390Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
13405643Saalok 
13415643Saalok 	/*
13425643Saalok 	 * A compressed file is read-only, other files can
13435643Saalok 	 * be read-write
13445643Saalok 	 */
13455643Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
13465643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
13475643Saalok 	} else {
13485643Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
13495643Saalok 	}
13500Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
13530Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
13540Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
13550Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
13560Sstevel@tonic-gate 	 */
13570Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
13580Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
13610Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
13620Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
13630Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
13640Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
13650Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
13660Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
13670Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
13680Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
13690Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
13700Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
13710Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
13720Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
13730Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
13760Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
13770Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
13780Sstevel@tonic-gate 	 * maxcontig is 0.
13790Sstevel@tonic-gate 	 */
13800Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13845643Saalok  * map in a compressed file
13855643Saalok  *
13865643Saalok  * Read in the header and the index that follows.
13875643Saalok  *
13885643Saalok  * The header is as follows -
13895643Saalok  *
13905643Saalok  * Signature (name of the compression algorithm)
13915643Saalok  * Compression segment size (a multiple of 512)
13925643Saalok  * Number of index entries
13935643Saalok  * Size of the last block
13945643Saalok  * The array containing the index entries
13955643Saalok  *
13965643Saalok  * The header information is always stored in
13975643Saalok  * network byte order on disk.
13985643Saalok  */
13995643Saalok static int
14005643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
14015643Saalok {
14025643Saalok 	uint32_t index_sz, header_len, i;
14035643Saalok 	ssize_t	resid;
14045643Saalok 	enum uio_rw rw;
14055643Saalok 	char *tbuf = buf;
14065643Saalok 	int error;
14075643Saalok 
14085643Saalok 	/* The signature has already been read */
14095643Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
14105643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
14115643Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
14125643Saalok 
14135643Saalok 	/*
14145643Saalok 	 * The compressed segment size must be a power of 2
14155643Saalok 	 */
14165643Saalok 	if (lsp->ls_uncomp_seg_sz % 2)
14175643Saalok 		return (EINVAL);
14185643Saalok 
14195643Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
14205643Saalok 		;
14215643Saalok 
14225643Saalok 	lsp->ls_comp_seg_shift = i;
14235643Saalok 
14245643Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
14255643Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
14265643Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
14275643Saalok 
14285643Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
14295643Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
14305643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
14315643Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
14325643Saalok 
14335643Saalok 	/*
14345643Saalok 	 * Compute the total size of the uncompressed data
14355643Saalok 	 * for use in fake_disk_geometry and other calculations.
14365643Saalok 	 * Disk geometry has to be faked with respect to the
14375643Saalok 	 * actual uncompressed data size rather than the
14385643Saalok 	 * compressed file size.
14395643Saalok 	 */
14405643Saalok 	lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
14415643Saalok 	    + lsp->ls_uncomp_last_seg_sz;
14425643Saalok 
14435643Saalok 	/*
14445643Saalok 	 * Index size is rounded up to a 512 byte boundary for ease
14455643Saalok 	 * of segmapping
14465643Saalok 	 */
14475643Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
14485643Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
14495643Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
14505643Saalok 	    sizeof (lsp->ls_comp_index_sz) +
14515643Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
14525643Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
14535643Saalok 
14545643Saalok 	index_sz += header_len;
14555643Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
14565643Saalok 
14575643Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
14585643Saalok 	lsp->ls_comp_index_data_sz = index_sz;
14595643Saalok 
14605643Saalok 	/*
14615643Saalok 	 * Read in the index -- this has a side-effect
14625643Saalok 	 * of reading in the header as well
14635643Saalok 	 */
14645643Saalok 	rw = UIO_READ;
14655643Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
14665643Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
14675643Saalok 
14685643Saalok 	if (error != 0)
14695643Saalok 		return (error);
14705643Saalok 
14715643Saalok 	/* Skip the header, this is where the index really begins */
14725643Saalok 	lsp->ls_comp_seg_index =
14735643Saalok 	    /*LINTED*/
14745643Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
14755643Saalok 
14765643Saalok 	/*
14775643Saalok 	 * Now recompute offsets in the index to account for
14785643Saalok 	 * the header length
14795643Saalok 	 */
14805643Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
14815643Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
14825643Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
14835643Saalok 	}
14845643Saalok 
14855643Saalok 	return (error);
14865643Saalok }
14875643Saalok 
14885643Saalok /*
14895643Saalok  * Check to see if the passed in signature is a valid
1490*8313SDina.Nimeh@Sun.Com  * one.  If it is valid, return the index into
14915643Saalok  * lofi_compress_table.
14925643Saalok  *
14935643Saalok  * Return -1 if it is invalid
14945643Saalok  */
14955643Saalok static int lofi_compress_select(char *signature)
14965643Saalok {
14975643Saalok 	int i;
14985643Saalok 
14995643Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
15005643Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
15015643Saalok 			return (i);
15025643Saalok 	}
15035643Saalok 
15045643Saalok 	return (-1);
15055643Saalok }
15065643Saalok 
15075643Saalok /*
15080Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
15090Sstevel@tonic-gate  */
15100Sstevel@tonic-gate static int
15110Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
15121657Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
15130Sstevel@tonic-gate {
15140Sstevel@tonic-gate 	minor_t	newminor;
15150Sstevel@tonic-gate 	struct lofi_state *lsp;
15160Sstevel@tonic-gate 	struct lofi_ioctl *klip;
15170Sstevel@tonic-gate 	int	error;
15180Sstevel@tonic-gate 	struct vnode *vp;
15190Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
15200Sstevel@tonic-gate 	int64_t	Size_prop_val;
15215643Saalok 	int	compress_index;
15220Sstevel@tonic-gate 	vattr_t	vattr;
15230Sstevel@tonic-gate 	int	flag;
15240Sstevel@tonic-gate 	enum vtype v_type;
15254451Seschrock 	int zalloced = 0;
15260Sstevel@tonic-gate 	dev_t	newdev;
15274451Seschrock 	char	namebuf[50];
1528*8313SDina.Nimeh@Sun.Com 	char	buf[DEV_BSIZE];
1529*8313SDina.Nimeh@Sun.Com 	char	crybuf[DEV_BSIZE];
15305643Saalok 	ssize_t	resid;
1531*8313SDina.Nimeh@Sun.Com 	boolean_t need_vn_close = B_FALSE;
1532*8313SDina.Nimeh@Sun.Com 	boolean_t keycopied = B_FALSE;
1533*8313SDina.Nimeh@Sun.Com 	boolean_t need_size_update = B_FALSE;
15340Sstevel@tonic-gate 
15351657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
15360Sstevel@tonic-gate 	if (klip == NULL)
15370Sstevel@tonic-gate 		return (EFAULT);
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
15420Sstevel@tonic-gate 		error = EINVAL;
15430Sstevel@tonic-gate 		goto out;
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
15470Sstevel@tonic-gate 		error = EBUSY;
15480Sstevel@tonic-gate 		goto out;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	if (pickminor) {
15520Sstevel@tonic-gate 		/* Find a free one */
15530Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
15540Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
15550Sstevel@tonic-gate 				break;
15560Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
15570Sstevel@tonic-gate 			error = EAGAIN;
15580Sstevel@tonic-gate 			goto out;
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 	} else {
15610Sstevel@tonic-gate 		newminor = klip->li_minor;
15620Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
15630Sstevel@tonic-gate 			error = EEXIST;
15640Sstevel@tonic-gate 			goto out;
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	/* make sure it's valid */
15690Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
15700Sstevel@tonic-gate 	    NULLVPP, &vp);
15710Sstevel@tonic-gate 	if (error) {
15720Sstevel@tonic-gate 		goto out;
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 	v_type = vp->v_type;
15750Sstevel@tonic-gate 	VN_RELE(vp);
15760Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
15770Sstevel@tonic-gate 		error = EINVAL;
15780Sstevel@tonic-gate 		goto out;
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
15810Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
15820Sstevel@tonic-gate 	if (error) {
15830Sstevel@tonic-gate 		/* try read-only */
15840Sstevel@tonic-gate 		flag &= ~FWRITE;
15850Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
15860Sstevel@tonic-gate 		    &vp, 0, 0);
15870Sstevel@tonic-gate 		if (error) {
15880Sstevel@tonic-gate 			goto out;
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 	}
1591*8313SDina.Nimeh@Sun.Com 	need_vn_close = B_TRUE;
1592*8313SDina.Nimeh@Sun.Com 
15930Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
15945331Samw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
15950Sstevel@tonic-gate 	if (error) {
1596*8313SDina.Nimeh@Sun.Com 		goto out;
15970Sstevel@tonic-gate 	}
15980Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
15990Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
16000Sstevel@tonic-gate 		error = EINVAL;
1601*8313SDina.Nimeh@Sun.Com 		goto out;
16020Sstevel@tonic-gate 	}
16030Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
16040Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
16050Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
16060Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
16070Sstevel@tonic-gate 		error = EINVAL;
1608*8313SDina.Nimeh@Sun.Com 		goto out;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
16110Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
16120Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
16130Sstevel@tonic-gate 		error = EINVAL;
16140Sstevel@tonic-gate 		goto propout;
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
16170Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
16180Sstevel@tonic-gate 		error = ENOMEM;
16190Sstevel@tonic-gate 		goto propout;
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 	zalloced = 1;
16220Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
16236883Sgd78059 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
16240Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
16250Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
16260Sstevel@tonic-gate 		error = ENXIO;
16270Sstevel@tonic-gate 		goto propout;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
16300Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
16310Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
16320Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
16330Sstevel@tonic-gate 		/* remove block node */
16340Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
16350Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
16360Sstevel@tonic-gate 		error = ENXIO;
16370Sstevel@tonic-gate 		goto propout;
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
16400Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
16410Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
16420Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
16430Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
16440Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
16450Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
16460Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
16470Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
16480Sstevel@tonic-gate 	if (lsp->ls_kstat) {
16490Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
16500Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
16510Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
16520Sstevel@tonic-gate 	}
16534451Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
16544451Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
16554451Seschrock 
16560Sstevel@tonic-gate 	/*
16570Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
16580Sstevel@tonic-gate 	 * updated correctly.
16590Sstevel@tonic-gate 	 */
16600Sstevel@tonic-gate 	lsp->ls_openflag = flag;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	/*
16630Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
16640Sstevel@tonic-gate 	 */
16650Sstevel@tonic-gate 	if (vp->v_type == VREG) {
16665331Samw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
16670Sstevel@tonic-gate 			lsp->ls_vp = vp;
16680Sstevel@tonic-gate 		} else {
16690Sstevel@tonic-gate 			/*
16700Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
16710Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
16720Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
16730Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
16740Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
16750Sstevel@tonic-gate 			 * the realvp so things behave as if we've
16760Sstevel@tonic-gate 			 * opened the realvp without any interaction
16770Sstevel@tonic-gate 			 * with lofs.
16780Sstevel@tonic-gate 			 */
16790Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
16800Sstevel@tonic-gate 			VN_RELE(vp);
16810Sstevel@tonic-gate 		}
16820Sstevel@tonic-gate 	} else {
16830Sstevel@tonic-gate 		lsp->ls_vp = vp;
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
16860Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
16870Sstevel@tonic-gate 	if (rvalp)
16880Sstevel@tonic-gate 		*rvalp = (int)newminor;
16890Sstevel@tonic-gate 	klip->li_minor = newminor;
16900Sstevel@tonic-gate 
16915643Saalok 	/*
1692*8313SDina.Nimeh@Sun.Com 	 * Initialize crypto details for encrypted lofi
16935643Saalok 	 */
1694*8313SDina.Nimeh@Sun.Com 	if (klip->li_crypto_enabled) {
1695*8313SDina.Nimeh@Sun.Com 		int ret;
1696*8313SDina.Nimeh@Sun.Com 
1697*8313SDina.Nimeh@Sun.Com 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
1698*8313SDina.Nimeh@Sun.Com 
1699*8313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
1700*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
1701*8313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
1702*8313SDina.Nimeh@Sun.Com 			    klip->li_cipher, lsp->ls_filename);
1703*8313SDina.Nimeh@Sun.Com 			error = EINVAL;
1704*8313SDina.Nimeh@Sun.Com 			goto propout;
1705*8313SDina.Nimeh@Sun.Com 		}
1706*8313SDina.Nimeh@Sun.Com 
1707*8313SDina.Nimeh@Sun.Com 		/* this is just initialization here */
1708*8313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param = NULL;
1709*8313SDina.Nimeh@Sun.Com 		lsp->ls_mech.cm_param_len = 0;
1710*8313SDina.Nimeh@Sun.Com 
1711*8313SDina.Nimeh@Sun.Com 		lsp->ls_iv_type = klip->li_iv_type;
1712*8313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
1713*8313SDina.Nimeh@Sun.Com 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
1714*8313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
1715*8313SDina.Nimeh@Sun.Com 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
1716*8313SDina.Nimeh@Sun.Com 			error = EINVAL;
1717*8313SDina.Nimeh@Sun.Com 			goto propout;
1718*8313SDina.Nimeh@Sun.Com 		}
1719*8313SDina.Nimeh@Sun.Com 
1720*8313SDina.Nimeh@Sun.Com 		/* iv mech must itself take a null iv */
1721*8313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param = NULL;
1722*8313SDina.Nimeh@Sun.Com 		lsp->ls_iv_mech.cm_param_len = 0;
1723*8313SDina.Nimeh@Sun.Com 		lsp->ls_iv_len = klip->li_iv_len;
1724*8313SDina.Nimeh@Sun.Com 
1725*8313SDina.Nimeh@Sun.Com 		/*
1726*8313SDina.Nimeh@Sun.Com 		 * Create ctx using li_cipher & the raw li_key after checking
1727*8313SDina.Nimeh@Sun.Com 		 * that it isn't a weak key.
1728*8313SDina.Nimeh@Sun.Com 		 */
1729*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
1730*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = klip->li_key_len;
1731*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = kmem_alloc(
1732*8313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
1733*8313SDina.Nimeh@Sun.Com 		bcopy(klip->li_key, lsp->ls_key.ck_data,
1734*8313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
1735*8313SDina.Nimeh@Sun.Com 		keycopied = B_TRUE;
1736*8313SDina.Nimeh@Sun.Com 
1737*8313SDina.Nimeh@Sun.Com 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
1738*8313SDina.Nimeh@Sun.Com 		if (ret != CRYPTO_SUCCESS) {
1739*8313SDina.Nimeh@Sun.Com 			error = EINVAL;
1740*8313SDina.Nimeh@Sun.Com 			cmn_err(CE_WARN, "weak key check failed for cipher "
1741*8313SDina.Nimeh@Sun.Com 			    "%s on file %s (0x%x)", klip->li_cipher,
1742*8313SDina.Nimeh@Sun.Com 			    lsp->ls_filename, ret);
1743*8313SDina.Nimeh@Sun.Com 			goto propout;
1744*8313SDina.Nimeh@Sun.Com 		}
1745*8313SDina.Nimeh@Sun.Com 	}
1746*8313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
1747*8313SDina.Nimeh@Sun.Com 
1748*8313SDina.Nimeh@Sun.Com 	/*
1749*8313SDina.Nimeh@Sun.Com 	 * Read the file signature to check if it is compressed or encrypted.
1750*8313SDina.Nimeh@Sun.Com 	 * Crypto signature is in a different location; both areas should
1751*8313SDina.Nimeh@Sun.Com 	 * read to keep compression and encryption mutually exclusive.
1752*8313SDina.Nimeh@Sun.Com 	 */
1753*8313SDina.Nimeh@Sun.Com 	if (lsp->ls_crypto_enabled) {
1754*8313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
1755*8313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
1756*8313SDina.Nimeh@Sun.Com 		if (error != 0)
1757*8313SDina.Nimeh@Sun.Com 			goto propout;
1758*8313SDina.Nimeh@Sun.Com 	}
1759*8313SDina.Nimeh@Sun.Com 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
17605643Saalok 	    0, RLIM64_INFINITY, kcred, &resid);
17615643Saalok 	if (error != 0)
17625643Saalok 		goto propout;
17635643Saalok 
1764*8313SDina.Nimeh@Sun.Com 	/* initialize these variables for all lofi files */
17655643Saalok 	lsp->ls_uncomp_seg_sz = 0;
17665643Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
17675643Saalok 	lsp->ls_comp_algorithm[0] = '\0';
17685643Saalok 
1769*8313SDina.Nimeh@Sun.Com 	/* encrypted lofi reads/writes shifted by crypto metadata size */
1770*8313SDina.Nimeh@Sun.Com 	lsp->ls_crypto_offset = 0;
1771*8313SDina.Nimeh@Sun.Com 
1772*8313SDina.Nimeh@Sun.Com 	/* this is a compressed lofi */
1773*8313SDina.Nimeh@Sun.Com 	if ((compress_index = lofi_compress_select(buf)) != -1) {
1774*8313SDina.Nimeh@Sun.Com 
1775*8313SDina.Nimeh@Sun.Com 		/* compression and encryption are mutually exclusive */
1776*8313SDina.Nimeh@Sun.Com 		if (klip->li_crypto_enabled) {
1777*8313SDina.Nimeh@Sun.Com 			error = ENOTSUP;
1778*8313SDina.Nimeh@Sun.Com 			goto propout;
1779*8313SDina.Nimeh@Sun.Com 		}
1780*8313SDina.Nimeh@Sun.Com 
1781*8313SDina.Nimeh@Sun.Com 		/* initialize compression info for compressed lofi */
17825643Saalok 		lsp->ls_comp_algorithm_index = compress_index;
17835643Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
17845643Saalok 		    lofi_compress_table[compress_index].l_name,
17855643Saalok 		    sizeof (lsp->ls_comp_algorithm));
1786*8313SDina.Nimeh@Sun.Com 
17875643Saalok 		error = lofi_map_compressed_file(lsp, buf);
17885643Saalok 		if (error != 0)
17895643Saalok 			goto propout;
1790*8313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
17915643Saalok 
1792*8313SDina.Nimeh@Sun.Com 	/* this is an encrypted lofi */
1793*8313SDina.Nimeh@Sun.Com 	} else if (strncmp(crybuf, lofi_crypto_magic,
1794*8313SDina.Nimeh@Sun.Com 	    sizeof (lofi_crypto_magic)) == 0) {
1795*8313SDina.Nimeh@Sun.Com 
1796*8313SDina.Nimeh@Sun.Com 		char *marker = crybuf;
1797*8313SDina.Nimeh@Sun.Com 
1798*8313SDina.Nimeh@Sun.Com 		/*
1799*8313SDina.Nimeh@Sun.Com 		 * This is the case where the header in the lofi image is
1800*8313SDina.Nimeh@Sun.Com 		 * already initialized to indicate it is encrypted.
1801*8313SDina.Nimeh@Sun.Com 		 * There is another case (see below) where encryption is
1802*8313SDina.Nimeh@Sun.Com 		 * requested but the lofi image has never been used yet,
1803*8313SDina.Nimeh@Sun.Com 		 * so the header needs to be written with encryption magic.
1804*8313SDina.Nimeh@Sun.Com 		 */
1805*8313SDina.Nimeh@Sun.Com 
1806*8313SDina.Nimeh@Sun.Com 		/* indicate this must be an encrypted lofi due to magic */
1807*8313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = B_TRUE;
1808*8313SDina.Nimeh@Sun.Com 
1809*8313SDina.Nimeh@Sun.Com 		/*
1810*8313SDina.Nimeh@Sun.Com 		 * The encryption header information is laid out this way:
1811*8313SDina.Nimeh@Sun.Com 		 *	6 bytes:	hex "CFLOFI"
1812*8313SDina.Nimeh@Sun.Com 		 *	2 bytes:	version = 0 ... for now
1813*8313SDina.Nimeh@Sun.Com 		 *	96 bytes:	reserved1 (not implemented yet)
1814*8313SDina.Nimeh@Sun.Com 		 *	4 bytes:	data_sector = 2 ... for now
1815*8313SDina.Nimeh@Sun.Com 		 *	more...		not implemented yet
1816*8313SDina.Nimeh@Sun.Com 		 */
1817*8313SDina.Nimeh@Sun.Com 
1818*8313SDina.Nimeh@Sun.Com 		/* copy the magic */
1819*8313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.magic,
1820*8313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.magic));
1821*8313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.magic);
1822*8313SDina.Nimeh@Sun.Com 
1823*8313SDina.Nimeh@Sun.Com 		/* read the encryption version number */
1824*8313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.version),
1825*8313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.version));
1826*8313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
1827*8313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.version);
1828*8313SDina.Nimeh@Sun.Com 
1829*8313SDina.Nimeh@Sun.Com 		/* read a chunk of reserved data */
1830*8313SDina.Nimeh@Sun.Com 		bcopy(marker, lsp->ls_crypto.reserved1,
1831*8313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.reserved1));
1832*8313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.reserved1);
1833*8313SDina.Nimeh@Sun.Com 
1834*8313SDina.Nimeh@Sun.Com 		/* read block number where encrypted data begins */
1835*8313SDina.Nimeh@Sun.Com 		bcopy(marker, &(lsp->ls_crypto.data_sector),
1836*8313SDina.Nimeh@Sun.Com 		    sizeof (lsp->ls_crypto.data_sector));
1837*8313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
1838*8313SDina.Nimeh@Sun.Com 		marker += sizeof (lsp->ls_crypto.data_sector);
1839*8313SDina.Nimeh@Sun.Com 
1840*8313SDina.Nimeh@Sun.Com 		/* and ignore the rest until it is implemented */
1841*8313SDina.Nimeh@Sun.Com 
1842*8313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
1843*8313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
1844*8313SDina.Nimeh@Sun.Com 
1845*8313SDina.Nimeh@Sun.Com 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
1846*8313SDina.Nimeh@Sun.Com 	} else if (klip->li_crypto_enabled) {
1847*8313SDina.Nimeh@Sun.Com 
1848*8313SDina.Nimeh@Sun.Com 		/*
1849*8313SDina.Nimeh@Sun.Com 		 * This is the case where encryption was requested but the
1850*8313SDina.Nimeh@Sun.Com 		 * appears to be entirely blank where the encryption header
1851*8313SDina.Nimeh@Sun.Com 		 * would have been in the lofi image.  If it is blank,
1852*8313SDina.Nimeh@Sun.Com 		 * assume it is a brand new lofi image and initialize the
1853*8313SDina.Nimeh@Sun.Com 		 * header area with encryption magic and current version
1854*8313SDina.Nimeh@Sun.Com 		 * header data.  If it is not blank, that's an error.
1855*8313SDina.Nimeh@Sun.Com 		 */
1856*8313SDina.Nimeh@Sun.Com 		int	i;
1857*8313SDina.Nimeh@Sun.Com 		char	*marker;
1858*8313SDina.Nimeh@Sun.Com 		struct crypto_meta	chead;
1859*8313SDina.Nimeh@Sun.Com 
1860*8313SDina.Nimeh@Sun.Com 		for (i = 0; i < sizeof (struct crypto_meta); i++)
1861*8313SDina.Nimeh@Sun.Com 			if (crybuf[i] != '\0')
1862*8313SDina.Nimeh@Sun.Com 				break;
1863*8313SDina.Nimeh@Sun.Com 		if (i != sizeof (struct crypto_meta)) {
1864*8313SDina.Nimeh@Sun.Com 			error = EINVAL;
1865*8313SDina.Nimeh@Sun.Com 			goto propout;
1866*8313SDina.Nimeh@Sun.Com 		}
1867*8313SDina.Nimeh@Sun.Com 
1868*8313SDina.Nimeh@Sun.Com 		/* nothing there, initialize as encrypted lofi */
1869*8313SDina.Nimeh@Sun.Com 		marker = crybuf;
1870*8313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
1871*8313SDina.Nimeh@Sun.Com 		marker += sizeof (lofi_crypto_magic);
1872*8313SDina.Nimeh@Sun.Com 		chead.version = htons(LOFI_CRYPTO_VERSION);
1873*8313SDina.Nimeh@Sun.Com 		bcopy(&(chead.version), marker, sizeof (chead.version));
1874*8313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.version);
1875*8313SDina.Nimeh@Sun.Com 		marker += sizeof (chead.reserved1);
1876*8313SDina.Nimeh@Sun.Com 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
1877*8313SDina.Nimeh@Sun.Com 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
1878*8313SDina.Nimeh@Sun.Com 
1879*8313SDina.Nimeh@Sun.Com 		/* write the header */
1880*8313SDina.Nimeh@Sun.Com 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
1881*8313SDina.Nimeh@Sun.Com 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
1882*8313SDina.Nimeh@Sun.Com 		if (error != 0)
1883*8313SDina.Nimeh@Sun.Com 			goto propout;
1884*8313SDina.Nimeh@Sun.Com 
1885*8313SDina.Nimeh@Sun.Com 		/* fix things up so it looks like we read this info */
1886*8313SDina.Nimeh@Sun.Com 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
1887*8313SDina.Nimeh@Sun.Com 		    sizeof (lofi_crypto_magic));
1888*8313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
1889*8313SDina.Nimeh@Sun.Com 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
1890*8313SDina.Nimeh@Sun.Com 
1891*8313SDina.Nimeh@Sun.Com 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
1892*8313SDina.Nimeh@Sun.Com 		need_size_update = B_TRUE;
1893*8313SDina.Nimeh@Sun.Com 	}
1894*8313SDina.Nimeh@Sun.Com 
1895*8313SDina.Nimeh@Sun.Com 	/*
1896*8313SDina.Nimeh@Sun.Com 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
1897*8313SDina.Nimeh@Sun.Com 	 * for encrypted lofi, advertise that it is somewhat shorter
1898*8313SDina.Nimeh@Sun.Com 	 * due to embedded crypto metadata section
1899*8313SDina.Nimeh@Sun.Com 	 */
1900*8313SDina.Nimeh@Sun.Com 	if (need_size_update) {
19015643Saalok 		/* update DDI properties */
1902*8313SDina.Nimeh@Sun.Com 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
19035643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
19045643Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
19055643Saalok 			error = EINVAL;
19065643Saalok 			goto propout;
19075643Saalok 		}
1908*8313SDina.Nimeh@Sun.Com 		Nblocks_prop_val =
1909*8313SDina.Nimeh@Sun.Com 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
19105643Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
19115643Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
19125643Saalok 			error = EINVAL;
19135643Saalok 			goto propout;
19145643Saalok 		}
19155643Saalok 	}
19165643Saalok 
19170Sstevel@tonic-gate 	fake_disk_geometry(lsp);
19180Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
19191657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
19200Sstevel@tonic-gate 	free_lofi_ioctl(klip);
19210Sstevel@tonic-gate 	return (0);
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate propout:
1924*8313SDina.Nimeh@Sun.Com 	if (keycopied) {
1925*8313SDina.Nimeh@Sun.Com 		bzero(lsp->ls_key.ck_data,
1926*8313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
1927*8313SDina.Nimeh@Sun.Com 		kmem_free(lsp->ls_key.ck_data,
1928*8313SDina.Nimeh@Sun.Com 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
1929*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_data = NULL;
1930*8313SDina.Nimeh@Sun.Com 		lsp->ls_key.ck_length = 0;
1931*8313SDina.Nimeh@Sun.Com 	}
1932*8313SDina.Nimeh@Sun.Com 
1933*8313SDina.Nimeh@Sun.Com 	if (zalloced)
1934*8313SDina.Nimeh@Sun.Com 		ddi_soft_state_free(lofi_statep, newminor);
1935*8313SDina.Nimeh@Sun.Com 
19360Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
19370Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
1938*8313SDina.Nimeh@Sun.Com 
19390Sstevel@tonic-gate out:
1940*8313SDina.Nimeh@Sun.Com 	if (need_vn_close) {
1941*8313SDina.Nimeh@Sun.Com 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
1942*8313SDina.Nimeh@Sun.Com 		VN_RELE(vp);
1943*8313SDina.Nimeh@Sun.Com 	}
1944*8313SDina.Nimeh@Sun.Com 
19450Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
19460Sstevel@tonic-gate 	free_lofi_ioctl(klip);
19470Sstevel@tonic-gate 	return (error);
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate  * unmap a file.
19520Sstevel@tonic-gate  */
19530Sstevel@tonic-gate static int
19540Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
19551657Sheppo     struct cred *credp, int ioctl_flag)
19560Sstevel@tonic-gate {
19570Sstevel@tonic-gate 	struct lofi_state *lsp;
19580Sstevel@tonic-gate 	struct lofi_ioctl *klip;
19590Sstevel@tonic-gate 	minor_t	minor;
19600Sstevel@tonic-gate 
19611657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
19620Sstevel@tonic-gate 	if (klip == NULL)
19630Sstevel@tonic-gate 		return (EFAULT);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
19660Sstevel@tonic-gate 	if (byfilename) {
19670Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
19680Sstevel@tonic-gate 	} else {
19690Sstevel@tonic-gate 		minor = klip->li_minor;
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 	if (minor == 0) {
19720Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
19730Sstevel@tonic-gate 		free_lofi_ioctl(klip);
19740Sstevel@tonic-gate 		return (ENXIO);
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
19774451Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
19780Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
19790Sstevel@tonic-gate 		free_lofi_ioctl(klip);
19800Sstevel@tonic-gate 		return (ENXIO);
19810Sstevel@tonic-gate 	}
19824451Seschrock 
19836734Sjohnlev 	/*
19846734Sjohnlev 	 * If it's still held open, we'll do one of three things:
19856734Sjohnlev 	 *
19866734Sjohnlev 	 * If no flag is set, just return EBUSY.
19876734Sjohnlev 	 *
19886734Sjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
19896734Sjohnlev 	 * the last user finishes.
19906734Sjohnlev 	 *
19916734Sjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
19926734Sjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
19936734Sjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
19946734Sjohnlev 	 * device will be cleaned up appropriately.
19956734Sjohnlev 	 *
19966734Sjohnlev 	 * This is complicated by the fact that we may have outstanding
19976734Sjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
1998*8313SDina.Nimeh@Sun.Com 	 * I/O, we keep a count of the number of outstanding I/O requests
1999*8313SDina.Nimeh@Sun.Com 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
2000*8313SDina.Nimeh@Sun.Com 	 * should be dispatched (ls_vp_closereq).
2001*8313SDina.Nimeh@Sun.Com 	 *
20026734Sjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
20036734Sjohnlev 	 * and then close the underlying vnode.
20046734Sjohnlev 	 */
20050Sstevel@tonic-gate 	if (is_opened(lsp)) {
20064451Seschrock 		if (klip->li_force) {
2007*8313SDina.Nimeh@Sun.Com 			/*
2008*8313SDina.Nimeh@Sun.Com 			 * XXX: the section marked here should probably be
2009*8313SDina.Nimeh@Sun.Com 			 * carefully incorporated into lofi_free_handle();
2010*8313SDina.Nimeh@Sun.Com 			 * afterward just replace this section with:
2011*8313SDina.Nimeh@Sun.Com 			 *	lofi_free_handle(dev, minor, lsp, credp);
2012*8313SDina.Nimeh@Sun.Com 			 * and clean up lofi_unmap_file() a bit more
2013*8313SDina.Nimeh@Sun.Com 			 */
2014*8313SDina.Nimeh@Sun.Com 			lofi_free_crypto(lsp);
2015*8313SDina.Nimeh@Sun.Com 
20164451Seschrock 			mutex_enter(&lsp->ls_vp_lock);
20174451Seschrock 			lsp->ls_vp_closereq = B_TRUE;
20184451Seschrock 			while (lsp->ls_vp_iocount > 0)
20194451Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
20204451Seschrock 			(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
20215331Samw 			    credp, NULL);
20224451Seschrock 			VN_RELE(lsp->ls_vp);
20234451Seschrock 			lsp->ls_vp = NULL;
20244451Seschrock 			cv_broadcast(&lsp->ls_vp_cv);
20254451Seschrock 			mutex_exit(&lsp->ls_vp_lock);
2026*8313SDina.Nimeh@Sun.Com 			/*
2027*8313SDina.Nimeh@Sun.Com 			 * XXX: to here
2028*8313SDina.Nimeh@Sun.Com 			 */
2029*8313SDina.Nimeh@Sun.Com 
2030*8313SDina.Nimeh@Sun.Com 			klip->li_minor = minor;
20314451Seschrock 			mutex_exit(&lofi_lock);
20324451Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
20334451Seschrock 			free_lofi_ioctl(klip);
20344451Seschrock 			return (0);
20356734Sjohnlev 		} else if (klip->li_cleanup) {
20366734Sjohnlev 			lsp->ls_cleanup = 1;
20376734Sjohnlev 			mutex_exit(&lofi_lock);
20386734Sjohnlev 			free_lofi_ioctl(klip);
20396734Sjohnlev 			return (0);
20404451Seschrock 		}
20416734Sjohnlev 
20420Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
20430Sstevel@tonic-gate 		free_lofi_ioctl(klip);
20440Sstevel@tonic-gate 		return (EBUSY);
20450Sstevel@tonic-gate 	}
20460Sstevel@tonic-gate 
20474451Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	klip->li_minor = minor;
20500Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
20511657Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
20520Sstevel@tonic-gate 	free_lofi_ioctl(klip);
20530Sstevel@tonic-gate 	return (0);
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate /*
20570Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
20580Sstevel@tonic-gate  * the name.
20590Sstevel@tonic-gate  */
20604451Seschrock /*ARGSUSED*/
20610Sstevel@tonic-gate static int
20620Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
20631657Sheppo     struct cred *credp, int ioctl_flag)
20640Sstevel@tonic-gate {
20650Sstevel@tonic-gate 	struct lofi_state *lsp;
20660Sstevel@tonic-gate 	struct lofi_ioctl *klip;
20670Sstevel@tonic-gate 	int	error;
20680Sstevel@tonic-gate 	minor_t	minor;
20690Sstevel@tonic-gate 
20701657Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
20710Sstevel@tonic-gate 	if (klip == NULL)
20720Sstevel@tonic-gate 		return (EFAULT);
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	switch (which) {
20750Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
20760Sstevel@tonic-gate 		minor = klip->li_minor;
20770Sstevel@tonic-gate 		if (minor == 0) {
20780Sstevel@tonic-gate 			free_lofi_ioctl(klip);
20790Sstevel@tonic-gate 			return (EINVAL);
20800Sstevel@tonic-gate 		}
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
20830Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
20840Sstevel@tonic-gate 		if (lsp == NULL) {
20850Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
20860Sstevel@tonic-gate 			free_lofi_ioctl(klip);
20870Sstevel@tonic-gate 			return (ENXIO);
20880Sstevel@tonic-gate 		}
20890Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
20905643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
20915643Saalok 		    sizeof (klip->li_algorithm));
2092*8313SDina.Nimeh@Sun.Com 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
20930Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
20941657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
20950Sstevel@tonic-gate 		free_lofi_ioctl(klip);
20960Sstevel@tonic-gate 		return (error);
20970Sstevel@tonic-gate 	case LOFI_GET_MINOR:
20980Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
20990Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
2100*8313SDina.Nimeh@Sun.Com 		/* caller should not depend on klip->li_crypto_enabled here */
21010Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
21020Sstevel@tonic-gate 		if (klip->li_minor == 0) {
21030Sstevel@tonic-gate 			free_lofi_ioctl(klip);
21040Sstevel@tonic-gate 			return (ENOENT);
21050Sstevel@tonic-gate 		}
21061657Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21070Sstevel@tonic-gate 		free_lofi_ioctl(klip);
21080Sstevel@tonic-gate 		return (error);
21095643Saalok 	case LOFI_CHECK_COMPRESSED:
21105643Saalok 		mutex_enter(&lofi_lock);
21115643Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
21125643Saalok 		mutex_exit(&lofi_lock);
21135643Saalok 		if (klip->li_minor == 0) {
21145643Saalok 			free_lofi_ioctl(klip);
21155643Saalok 			return (ENOENT);
21165643Saalok 		}
21175643Saalok 		mutex_enter(&lofi_lock);
21185643Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
21195643Saalok 		if (lsp == NULL) {
21205643Saalok 			mutex_exit(&lofi_lock);
21215643Saalok 			free_lofi_ioctl(klip);
21225643Saalok 			return (ENXIO);
21235643Saalok 		}
21245643Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
21255643Saalok 
21265643Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
21275643Saalok 		    sizeof (klip->li_algorithm));
21285643Saalok 		mutex_exit(&lofi_lock);
21295643Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
21305643Saalok 		free_lofi_ioctl(klip);
21315643Saalok 		return (error);
21320Sstevel@tonic-gate 	default:
21330Sstevel@tonic-gate 		free_lofi_ioctl(klip);
21340Sstevel@tonic-gate 		return (EINVAL);
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate }
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate static int
21400Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
21410Sstevel@tonic-gate     int *rvalp)
21420Sstevel@tonic-gate {
21430Sstevel@tonic-gate 	int	error;
21440Sstevel@tonic-gate 	enum dkio_state dkstate;
21450Sstevel@tonic-gate 	struct lofi_state *lsp;
21460Sstevel@tonic-gate 	minor_t	minor;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	minor = getminor(dev);
21490Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
21500Sstevel@tonic-gate 	if (minor == 0) {
21510Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		/*
21540Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
21550Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
21560Sstevel@tonic-gate 		 * long as they can open it read-only.
21570Sstevel@tonic-gate 		 */
21580Sstevel@tonic-gate 		switch (cmd) {
21590Sstevel@tonic-gate 		case LOFI_MAP_FILE:
21600Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21610Sstevel@tonic-gate 				return (EPERM);
21621657Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
21630Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
21640Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21650Sstevel@tonic-gate 				return (EPERM);
21661657Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
21670Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
21680Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21690Sstevel@tonic-gate 				return (EPERM);
21701657Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
21710Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
21720Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
21730Sstevel@tonic-gate 				return (EPERM);
21741657Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
21750Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
21760Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
21771657Sheppo 			    credp, flag));
21780Sstevel@tonic-gate 		case LOFI_GET_MINOR:
21790Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
21801657Sheppo 			    credp, flag));
21810Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
21821657Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
21831657Sheppo 			    sizeof (lofi_max_files), flag);
21840Sstevel@tonic-gate 			if (error)
21850Sstevel@tonic-gate 				return (EFAULT);
21860Sstevel@tonic-gate 			return (0);
21875643Saalok 		case LOFI_CHECK_COMPRESSED:
21885643Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
21895643Saalok 			    credp, flag));
21900Sstevel@tonic-gate 		default:
21910Sstevel@tonic-gate 			break;
21920Sstevel@tonic-gate 		}
21930Sstevel@tonic-gate 	}
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
21960Sstevel@tonic-gate 	if (lsp == NULL)
21970Sstevel@tonic-gate 		return (ENXIO);
21980Sstevel@tonic-gate 
21994451Seschrock 	/*
22004451Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
22014451Seschrock 	 * EIO as if the device was no longer present.
22024451Seschrock 	 */
22034451Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
22044451Seschrock 		return (EIO);
22054451Seschrock 
22060Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
22070Sstevel@tonic-gate 	switch (cmd) {
22080Sstevel@tonic-gate 	case DKIOCGVTOC:
22090Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
22100Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
22110Sstevel@tonic-gate 			struct vtoc32 vtoc32;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
22140Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
22150Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
22160Sstevel@tonic-gate 				return (EFAULT);
22170Sstevel@tonic-gate 				break;
22180Sstevel@tonic-gate 			}
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 		case DDI_MODEL_NONE:
22210Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
22220Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
22230Sstevel@tonic-gate 				return (EFAULT);
22240Sstevel@tonic-gate 			break;
22250Sstevel@tonic-gate 		}
22260Sstevel@tonic-gate 		return (0);
22270Sstevel@tonic-gate 	case DKIOCINFO:
22281657Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
22291657Sheppo 		    sizeof (struct dk_cinfo), flag);
22300Sstevel@tonic-gate 		if (error)
22310Sstevel@tonic-gate 			return (EFAULT);
22320Sstevel@tonic-gate 		return (0);
22330Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
22340Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
22350Sstevel@tonic-gate 	case DKIOCGGEOM:
22361657Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
22371657Sheppo 		    sizeof (struct dk_geom), flag);
22380Sstevel@tonic-gate 		if (error)
22390Sstevel@tonic-gate 			return (EFAULT);
22400Sstevel@tonic-gate 		return (0);
22410Sstevel@tonic-gate 	case DKIOCSTATE:
22424451Seschrock 		/*
22434451Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
22444451Seschrock 		 * a device is forcefully unmapped, then the device transitions
22454451Seschrock 		 * to the DKIO_DEV_GONE state.
22464451Seschrock 		 */
22474451Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
22484451Seschrock 		    flag) != 0)
22494451Seschrock 			return (EFAULT);
22504451Seschrock 
22514451Seschrock 		mutex_enter(&lsp->ls_vp_lock);
22524451Seschrock 		while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
22534451Seschrock 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
22544451Seschrock 			/*
22554451Seschrock 			 * By virtue of having the device open, we know that
22564451Seschrock 			 * 'lsp' will remain valid when we return.
22574451Seschrock 			 */
22584451Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
22594451Seschrock 			    &lsp->ls_vp_lock)) {
22604451Seschrock 				mutex_exit(&lsp->ls_vp_lock);
22614451Seschrock 				return (EINTR);
22624451Seschrock 			}
22634451Seschrock 		}
22644451Seschrock 
22654451Seschrock 		dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
22664451Seschrock 		mutex_exit(&lsp->ls_vp_lock);
22674451Seschrock 
22684451Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
22694451Seschrock 		    sizeof (dkstate), flag) != 0)
22700Sstevel@tonic-gate 			return (EFAULT);
22710Sstevel@tonic-gate 		return (0);
22720Sstevel@tonic-gate 	default:
22730Sstevel@tonic-gate 		return (ENOTTY);
22740Sstevel@tonic-gate 	}
22750Sstevel@tonic-gate }
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
22780Sstevel@tonic-gate 	lofi_open,		/* open */
22790Sstevel@tonic-gate 	lofi_close,		/* close */
22800Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
22810Sstevel@tonic-gate 	nodev,			/* print */
22820Sstevel@tonic-gate 	nodev,			/* dump */
22830Sstevel@tonic-gate 	lofi_read,		/* read */
22840Sstevel@tonic-gate 	lofi_write,		/* write */
22850Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
22860Sstevel@tonic-gate 	nodev,			/* devmap */
22870Sstevel@tonic-gate 	nodev,			/* mmap */
22880Sstevel@tonic-gate 	nodev,			/* segmap */
22890Sstevel@tonic-gate 	nochpoll,		/* poll */
22900Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
22910Sstevel@tonic-gate 	0,			/* streamtab  */
22920Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
22930Sstevel@tonic-gate 	CB_REV,
22940Sstevel@tonic-gate 	lofi_aread,
22950Sstevel@tonic-gate 	lofi_awrite
22960Sstevel@tonic-gate };
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate static struct dev_ops lofi_ops = {
22990Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
23000Sstevel@tonic-gate 	0,			/* refcnt  */
23010Sstevel@tonic-gate 	lofi_info,		/* info */
23020Sstevel@tonic-gate 	nulldev,		/* identify */
23030Sstevel@tonic-gate 	nulldev,		/* probe */
23040Sstevel@tonic-gate 	lofi_attach,		/* attach */
23050Sstevel@tonic-gate 	lofi_detach,		/* detach */
23060Sstevel@tonic-gate 	nodev,			/* reset */
23070Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
23087656SSherry.Moore@Sun.COM 	NULL,			/* no bus operations */
23097656SSherry.Moore@Sun.COM 	NULL,			/* power */
2310*8313SDina.Nimeh@Sun.Com 	ddi_quiesce_not_needed,	/* quiesce */
23110Sstevel@tonic-gate };
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate static struct modldrv modldrv = {
23140Sstevel@tonic-gate 	&mod_driverops,
23157656SSherry.Moore@Sun.COM 	"loopback file driver",
23160Sstevel@tonic-gate 	&lofi_ops,
23170Sstevel@tonic-gate };
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate static struct modlinkage modlinkage = {
23200Sstevel@tonic-gate 	MODREV_1,
23210Sstevel@tonic-gate 	&modldrv,
23220Sstevel@tonic-gate 	NULL
23230Sstevel@tonic-gate };
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate int
23260Sstevel@tonic-gate _init(void)
23270Sstevel@tonic-gate {
23280Sstevel@tonic-gate 	int error;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
23310Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
23320Sstevel@tonic-gate 	if (error)
23330Sstevel@tonic-gate 		return (error);
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
23360Sstevel@tonic-gate 	error = mod_install(&modlinkage);
23370Sstevel@tonic-gate 	if (error) {
23380Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
23390Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
23400Sstevel@tonic-gate 	}
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	return (error);
23430Sstevel@tonic-gate }
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate int
23460Sstevel@tonic-gate _fini(void)
23470Sstevel@tonic-gate {
23480Sstevel@tonic-gate 	int	error;
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	if (lofi_busy())
23510Sstevel@tonic-gate 		return (EBUSY);
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
23540Sstevel@tonic-gate 	if (error)
23550Sstevel@tonic-gate 		return (error);
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
23580Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	return (error);
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate int
23640Sstevel@tonic-gate _info(struct modinfo *modinfop)
23650Sstevel@tonic-gate {
23660Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
23670Sstevel@tonic-gate }
2368