xref: /titanic_53/usr/src/uts/common/io/lofi.c (revision a423e759b1b3e58affccafde9c320c61d7566a21)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5bd07e074Sheppo  * Common Development and Distribution License (the "License").
6bd07e074Sheppo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2293239addSjohnlev  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
307c478bd9Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
317c478bd9Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
327c478bd9Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
337c478bd9Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
347c478bd9Sstevel@tonic-gate  * mounting images of filesystems.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
377c478bd9Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
387c478bd9Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
397c478bd9Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
407c478bd9Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
417c478bd9Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
427c478bd9Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
437c478bd9Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
447c478bd9Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
457c478bd9Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
487c478bd9Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
497c478bd9Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
507c478bd9Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
517c478bd9Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
527c478bd9Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
557c478bd9Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
567c478bd9Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
577c478bd9Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
587c478bd9Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
597c478bd9Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
607c478bd9Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
617c478bd9Sstevel@tonic-gate  *
623d7072f8Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
633d7072f8Seschrock  * support simulation of hotplug events, an optional force flag is provided.
643d7072f8Seschrock  * If a lofi device is open when a force detach is requested, then the
653d7072f8Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
663d7072f8Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
673d7072f8Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
683d7072f8Seschrock  * detached but not removed.
693d7072f8Seschrock  *
707c478bd9Sstevel@tonic-gate  * Known problems:
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
737c478bd9Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
747c478bd9Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
757c478bd9Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
787c478bd9Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
797c478bd9Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
807c478bd9Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
837c478bd9Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
847c478bd9Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
857c478bd9Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
867c478bd9Sstevel@tonic-gate  *	do. Though it would still be silly.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Interesting things to do:
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
937c478bd9Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
947c478bd9Sstevel@tonic-gate  *	need to fake a geometry. But this is also silly unless you're replacing
957c478bd9Sstevel@tonic-gate  *	metadisk.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	Encryption. tpm would like this. Apparently Windows 2000 has it, and
987c478bd9Sstevel@tonic-gate  *	so does Linux.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #include <sys/types.h>
10287117650Saalok #include <netinet/in.h>
1037c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1047c478bd9Sstevel@tonic-gate #include <sys/uio.h>
1057c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1067c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1077c478bd9Sstevel@tonic-gate #include <sys/mman.h>
1087c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1097c478bd9Sstevel@tonic-gate #include <sys/aio_req.h>
1107c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1117c478bd9Sstevel@tonic-gate #include <sys/file.h>
1127c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1137c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1147c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1157c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
1167c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
1177c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
1187c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1197c478bd9Sstevel@tonic-gate #include <sys/filio.h>
1207c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
1217c478bd9Sstevel@tonic-gate #include <sys/open.h>
1227c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1237c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1247c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1257c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
12687117650Saalok #include <sys/zmod.h>
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
1297c478bd9Sstevel@tonic-gate #define	SIZE_PROP_NAME		"Size"
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static dev_info_t *lofi_dip;
1327c478bd9Sstevel@tonic-gate static void	*lofi_statep;
1337c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1377c478bd9Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1387c478bd9Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
1397c478bd9Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
1407c478bd9Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1417c478bd9Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
1427c478bd9Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
1437c478bd9Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
1447c478bd9Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
1457c478bd9Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
1487c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES;
1517c478bd9Sstevel@tonic-gate 
15287117650Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
15387117650Saalok 	size_t *destlen, int level);
15487117650Saalok 
15587117650Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
15687117650Saalok 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
15787117650Saalok 	{gzip_decompress,	NULL,	6,	"gzip-6"},
15887117650Saalok 	{gzip_decompress,	NULL,	9,	"gzip-9"}
15987117650Saalok };
16087117650Saalok 
1617c478bd9Sstevel@tonic-gate static int
1627c478bd9Sstevel@tonic-gate lofi_busy(void)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	minor_t	minor;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
1687c478bd9Sstevel@tonic-gate 	 * help because the device isn't open.
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
1717c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
1727c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
1737c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
1747c478bd9Sstevel@tonic-gate 			return (EBUSY);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
1787c478bd9Sstevel@tonic-gate 	return (0);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static int
1827c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
1857c478bd9Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static int
1897c478bd9Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
1927c478bd9Sstevel@tonic-gate 	switch (otyp) {
1937c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
1947c478bd9Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
1957c478bd9Sstevel@tonic-gate 		break;
1967c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
1977c478bd9Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
1987c478bd9Sstevel@tonic-gate 		break;
1997c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
2007c478bd9Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 	default:
2037c478bd9Sstevel@tonic-gate 		return (-1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	return (0);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static void
2097c478bd9Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2127c478bd9Sstevel@tonic-gate 	switch (otyp) {
2137c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
2147c478bd9Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
2157c478bd9Sstevel@tonic-gate 		break;
2167c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
2177c478bd9Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
2187c478bd9Sstevel@tonic-gate 		break;
2197c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
2207c478bd9Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
2217c478bd9Sstevel@tonic-gate 		break;
2227c478bd9Sstevel@tonic-gate 	default:
2237c478bd9Sstevel@tonic-gate 		break;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2273d7072f8Seschrock static void
2283d7072f8Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
2293d7072f8Seschrock     cred_t *credp)
2303d7072f8Seschrock {
2313d7072f8Seschrock 	dev_t	newdev;
2323d7072f8Seschrock 	char	namebuf[50];
2333d7072f8Seschrock 
2343d7072f8Seschrock 	if (lsp->ls_vp) {
235da6c28aaSamw 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
236da6c28aaSamw 		    1, 0, credp, NULL);
2373d7072f8Seschrock 		VN_RELE(lsp->ls_vp);
2383d7072f8Seschrock 		lsp->ls_vp = NULL;
2393d7072f8Seschrock 	}
2403d7072f8Seschrock 
2413d7072f8Seschrock 	newdev = makedevice(getmajor(dev), minor);
2423d7072f8Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
2433d7072f8Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
2443d7072f8Seschrock 
2453d7072f8Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
2463d7072f8Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
2473d7072f8Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
2483d7072f8Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
2493d7072f8Seschrock 
2503d7072f8Seschrock 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
2513d7072f8Seschrock 	taskq_destroy(lsp->ls_taskq);
2523d7072f8Seschrock 	if (lsp->ls_kstat) {
2533d7072f8Seschrock 		kstat_delete(lsp->ls_kstat);
2543d7072f8Seschrock 		mutex_destroy(&lsp->ls_kstat_lock);
2553d7072f8Seschrock 	}
256*a423e759Saalok 
257*a423e759Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
258*a423e759Saalok 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
259*a423e759Saalok 		lsp->ls_uncomp_seg_sz = 0;
260*a423e759Saalok 	}
2613d7072f8Seschrock 	ddi_soft_state_free(lofi_statep, minor);
2623d7072f8Seschrock }
2633d7072f8Seschrock 
2643d7072f8Seschrock /*ARGSUSED*/
2657c478bd9Sstevel@tonic-gate static int
2667c478bd9Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	minor_t	minor;
2697c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
2727c478bd9Sstevel@tonic-gate 	minor = getminor(*devp);
2737c478bd9Sstevel@tonic-gate 	if (minor == 0) {
2747c478bd9Sstevel@tonic-gate 		/* master control device */
2757c478bd9Sstevel@tonic-gate 		/* must be opened exclusively */
2767c478bd9Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
2777c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2787c478bd9Sstevel@tonic-gate 			return (EINVAL);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
2817c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
2827c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2837c478bd9Sstevel@tonic-gate 			return (ENXIO);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 		if (is_opened(lsp)) {
2867c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2877c478bd9Sstevel@tonic-gate 			return (EBUSY);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
2907c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
2917c478bd9Sstevel@tonic-gate 		return (0);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
2957c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
2967c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
2977c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
2987c478bd9Sstevel@tonic-gate 		return (EINVAL);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3013d7072f8Seschrock 	if (lsp->ls_vp == NULL) {
3023d7072f8Seschrock 		mutex_exit(&lofi_lock);
3033d7072f8Seschrock 		return (ENXIO);
3043d7072f8Seschrock 	}
3053d7072f8Seschrock 
3067c478bd9Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
3077c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3087c478bd9Sstevel@tonic-gate 		return (EINVAL);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
3127c478bd9Sstevel@tonic-gate 	return (0);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3153d7072f8Seschrock /*ARGSUSED*/
3167c478bd9Sstevel@tonic-gate static int
3177c478bd9Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	minor_t	minor;
3207c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
3237c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
3247c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
3257c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
3267c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3277c478bd9Sstevel@tonic-gate 		return (EINVAL);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	mark_closed(lsp, otyp);
3303d7072f8Seschrock 
3313d7072f8Seschrock 	/*
33293239addSjohnlev 	 * If we forcibly closed the underlying device (li_force), or
33393239addSjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
33493239addSjohnlev 	 * out of the door.
3353d7072f8Seschrock 	 */
33693239addSjohnlev 	if (minor != 0 && !is_opened(lsp) &&
33793239addSjohnlev 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
3383d7072f8Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
33993239addSjohnlev 
3407c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
3417c478bd9Sstevel@tonic-gate 	return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
34487117650Saalok static int
34587117650Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
34687117650Saalok 	struct lofi_state *lsp)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	int error;
34987117650Saalok 	offset_t alignedoffset, mapoffset;
3507c478bd9Sstevel@tonic-gate 	size_t	xfersize;
3517c478bd9Sstevel@tonic-gate 	int	isread;
3527c478bd9Sstevel@tonic-gate 	int 	smflags;
35387117650Saalok 	caddr_t	mapaddr;
35487117650Saalok 	size_t	len;
3557c478bd9Sstevel@tonic-gate 	enum seg_rw srw;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
3597c478bd9Sstevel@tonic-gate 	 * an 8K boundary, but the buf transfer address may not be
36087117650Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
36187117650Saalok 	 * that even though we could). This matters since the initial
36287117650Saalok 	 * part of the transfer may not start at offset 0 within the
36387117650Saalok 	 * segmap'd chunk. So we have to compensate for that with
36487117650Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
36587117650Saalok 	 * beginning, and the last is capped by b_resid
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	mapoffset = offset & MAXBOFFSET;
36887117650Saalok 	alignedoffset = offset - mapoffset;
3697c478bd9Sstevel@tonic-gate 	bp->b_resid = bp->b_bcount;
3707c478bd9Sstevel@tonic-gate 	isread = bp->b_flags & B_READ;
3717c478bd9Sstevel@tonic-gate 	srw = isread ? S_READ : S_WRITE;
3727c478bd9Sstevel@tonic-gate 	do {
37387117650Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
3747c478bd9Sstevel@tonic-gate 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
3757c478bd9Sstevel@tonic-gate 		len = roundup(mapoffset + xfersize, PAGESIZE);
3767c478bd9Sstevel@tonic-gate 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
3777c478bd9Sstevel@tonic-gate 		    alignedoffset, MAXBSIZE, 1, srw);
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * Now fault in the pages. This lets us check
3807c478bd9Sstevel@tonic-gate 		 * for errors before we reference mapaddr and
3817c478bd9Sstevel@tonic-gate 		 * try to resolve the fault in bcopy (which would
3827c478bd9Sstevel@tonic-gate 		 * panic instead). And this can easily happen,
3837c478bd9Sstevel@tonic-gate 		 * particularly if you've lofi'd a file over NFS
3847c478bd9Sstevel@tonic-gate 		 * and someone deletes the file on the server.
3857c478bd9Sstevel@tonic-gate 		 */
3867c478bd9Sstevel@tonic-gate 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
3877c478bd9Sstevel@tonic-gate 		    len, F_SOFTLOCK, srw);
3887c478bd9Sstevel@tonic-gate 		if (error) {
3897c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, mapaddr, 0);
3907c478bd9Sstevel@tonic-gate 			if (FC_CODE(error) == FC_OBJERR)
3917c478bd9Sstevel@tonic-gate 				error = FC_ERRNO(error);
3927c478bd9Sstevel@tonic-gate 			else
3937c478bd9Sstevel@tonic-gate 				error = EIO;
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 		smflags = 0;
3977c478bd9Sstevel@tonic-gate 		if (isread) {
39887117650Saalok 			smflags |= SM_FREE;
39987117650Saalok 			/*
40087117650Saalok 			 * If we're reading an entire page starting
40187117650Saalok 			 * at a page boundary, there's a good chance
40287117650Saalok 			 * we won't need it again. Put it on the
40387117650Saalok 			 * head of the freelist.
40487117650Saalok 			 */
40587117650Saalok 			if (mapoffset == 0 && xfersize == PAGESIZE)
40687117650Saalok 				smflags |= SM_DONTNEED;
4077c478bd9Sstevel@tonic-gate 			bcopy(mapaddr + mapoffset, bufaddr, xfersize);
4087c478bd9Sstevel@tonic-gate 		} else {
4097c478bd9Sstevel@tonic-gate 			smflags |= SM_WRITE;
4107c478bd9Sstevel@tonic-gate 			bcopy(bufaddr, mapaddr + mapoffset, xfersize);
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 		bp->b_resid -= xfersize;
4137c478bd9Sstevel@tonic-gate 		bufaddr += xfersize;
4147c478bd9Sstevel@tonic-gate 		offset += xfersize;
4157c478bd9Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
4167c478bd9Sstevel@tonic-gate 		    len, F_SOFTUNLOCK, srw);
4177c478bd9Sstevel@tonic-gate 		error = segmap_release(segkmap, mapaddr, smflags);
4187c478bd9Sstevel@tonic-gate 		/* only the first map may start partial */
4197c478bd9Sstevel@tonic-gate 		mapoffset = 0;
4207c478bd9Sstevel@tonic-gate 		alignedoffset += MAXBSIZE;
4217c478bd9Sstevel@tonic-gate 	} while ((error == 0) && (bp->b_resid > 0) &&
42287117650Saalok 	    (offset < lsp->ls_vp_comp_size));
42387117650Saalok 
42487117650Saalok 	return (error);
42587117650Saalok }
42687117650Saalok 
42787117650Saalok /*ARGSUSED*/
42887117650Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
42987117650Saalok     size_t *dstlen, int level)
43087117650Saalok {
43187117650Saalok 	ASSERT(*dstlen >= srclen);
43287117650Saalok 
43387117650Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
43487117650Saalok 		return (-1);
43587117650Saalok 	return (0);
43687117650Saalok }
43787117650Saalok 
43887117650Saalok /*
43987117650Saalok  * This is basically what strategy used to be before we found we
44087117650Saalok  * needed task queues.
44187117650Saalok  */
44287117650Saalok static void
44387117650Saalok lofi_strategy_task(void *arg)
44487117650Saalok {
44587117650Saalok 	struct buf *bp = (struct buf *)arg;
44687117650Saalok 	int error;
44787117650Saalok 	struct lofi_state *lsp;
44887117650Saalok 	uint64_t sblkno, eblkno, cmpbytes;
44987117650Saalok 	offset_t offset, sblkoff, eblkoff;
45087117650Saalok 	u_offset_t salign, ealign;
45187117650Saalok 	u_offset_t sdiff;
45287117650Saalok 	uint32_t comp_data_sz;
45387117650Saalok 	caddr_t bufaddr;
45487117650Saalok 	unsigned char *compressed_seg = NULL, *cmpbuf;
45587117650Saalok 	unsigned char *uncompressed_seg = NULL;
45687117650Saalok 	lofi_compress_info_t *li;
45787117650Saalok 	size_t oblkcount, xfersize;
45887117650Saalok 	unsigned long seglen;
45987117650Saalok 
46087117650Saalok 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
46187117650Saalok 	if (lsp->ls_kstat) {
46287117650Saalok 		mutex_enter(lsp->ls_kstat->ks_lock);
46387117650Saalok 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
46487117650Saalok 		mutex_exit(lsp->ls_kstat->ks_lock);
46587117650Saalok 	}
46687117650Saalok 	bp_mapin(bp);
46787117650Saalok 	bufaddr = bp->b_un.b_addr;
46887117650Saalok 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
46987117650Saalok 
47087117650Saalok 	/*
47187117650Saalok 	 * We used to always use vn_rdwr here, but we cannot do that because
47287117650Saalok 	 * we might decide to read or write from the the underlying
47387117650Saalok 	 * file during this call, which would be a deadlock because
47487117650Saalok 	 * we have the rw_lock. So instead we page, unless it's not
47587117650Saalok 	 * mapable or it's a character device.
47687117650Saalok 	 */
47787117650Saalok 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
47887117650Saalok 		error = EIO;
47987117650Saalok 	} else if (((lsp->ls_vp->v_flag & VNOMAP) == 0) &&
48087117650Saalok 	    (lsp->ls_vp->v_type != VCHR)) {
48187117650Saalok 		uint64_t i;
48287117650Saalok 
48387117650Saalok 		/*
48487117650Saalok 		 * Handle uncompressed files with a regular read
48587117650Saalok 		 */
48687117650Saalok 		if (lsp->ls_uncomp_seg_sz == 0) {
48787117650Saalok 			error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
48887117650Saalok 			goto done;
48987117650Saalok 		}
49087117650Saalok 
49187117650Saalok 		/*
49287117650Saalok 		 * From here on we're dealing primarily with compressed files
49387117650Saalok 		 */
49487117650Saalok 
49587117650Saalok 		/*
49687117650Saalok 		 * Compressed files can only be read from and
49787117650Saalok 		 * not written to
49887117650Saalok 		 */
49987117650Saalok 		if (!(bp->b_flags & B_READ)) {
50087117650Saalok 			bp->b_resid = bp->b_bcount;
50187117650Saalok 			error = EROFS;
50287117650Saalok 			goto done;
50387117650Saalok 		}
50487117650Saalok 
50587117650Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
50687117650Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
50787117650Saalok 		/*
50887117650Saalok 		 * Compute starting and ending compressed segment numbers
50987117650Saalok 		 * We use only bitwise operations avoiding division and
51087117650Saalok 		 * modulus because we enforce the compression segment size
51187117650Saalok 		 * to a power of 2
51287117650Saalok 		 */
51387117650Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
51487117650Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
51587117650Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
51687117650Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
51787117650Saalok 
51887117650Saalok 		/*
51987117650Saalok 		 * Align start offset to block boundary for segmap
52087117650Saalok 		 */
52187117650Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
52287117650Saalok 		sdiff = salign & (DEV_BSIZE - 1);
52387117650Saalok 		salign -= sdiff;
52487117650Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
52587117650Saalok 			/*
52687117650Saalok 			 * We're dealing with the last segment of
52787117650Saalok 			 * the compressed file -- the size of this
52887117650Saalok 			 * segment *may not* be the same as the
52987117650Saalok 			 * segment size for the file
53087117650Saalok 			 */
53187117650Saalok 			eblkoff = (offset + bp->b_bcount) &
53287117650Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
53387117650Saalok 			ealign = lsp->ls_vp_comp_size;
53487117650Saalok 		} else {
53587117650Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
53687117650Saalok 		}
53787117650Saalok 
53887117650Saalok 		/*
53987117650Saalok 		 * Preserve original request paramaters
54087117650Saalok 		 */
54187117650Saalok 		oblkcount = bp->b_bcount;
54287117650Saalok 
54387117650Saalok 		/*
54487117650Saalok 		 * Assign the calculated parameters
54587117650Saalok 		 */
54687117650Saalok 		comp_data_sz = ealign - salign;
54787117650Saalok 		bp->b_bcount = comp_data_sz;
54887117650Saalok 
54987117650Saalok 		/*
55087117650Saalok 		 * Allocate fixed size memory blocks to hold compressed
55187117650Saalok 		 * segments and one uncompressed segment since we
55287117650Saalok 		 * uncompress segments one at a time
55387117650Saalok 		 */
55487117650Saalok 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
55587117650Saalok 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
55687117650Saalok 		/*
55787117650Saalok 		 * Map in the calculated number of blocks
55887117650Saalok 		 */
55987117650Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
56087117650Saalok 		    bp, lsp);
56187117650Saalok 
56287117650Saalok 		bp->b_bcount = oblkcount;
56387117650Saalok 		bp->b_resid = oblkcount;
56487117650Saalok 		if (error != 0)
56587117650Saalok 			goto done;
56687117650Saalok 
56787117650Saalok 		/*
56887117650Saalok 		 * We have the compressed blocks, now uncompress them
56987117650Saalok 		 */
57087117650Saalok 		cmpbuf = compressed_seg + sdiff;
57187117650Saalok 		for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz;
57287117650Saalok 		    i++) {
57387117650Saalok 			/*
57487117650Saalok 			 * Each of the segment index entries contains
57587117650Saalok 			 * the starting block number for that segment.
57687117650Saalok 			 * The number of compressed bytes in a segment
57787117650Saalok 			 * is thus the difference between the starting
57887117650Saalok 			 * block number of this segment and the starting
57987117650Saalok 			 * block number of the next segment.
58087117650Saalok 			 */
58187117650Saalok 			if ((i == eblkno) &&
58287117650Saalok 			    (i == lsp->ls_comp_index_sz - 1)) {
58387117650Saalok 				cmpbytes = lsp->ls_vp_comp_size -
58487117650Saalok 				    lsp->ls_comp_seg_index[i];
58587117650Saalok 			} else {
58687117650Saalok 				cmpbytes = lsp->ls_comp_seg_index[i + 1] -
58787117650Saalok 				    lsp->ls_comp_seg_index[i];
58887117650Saalok 			}
58987117650Saalok 
59087117650Saalok 			/*
59187117650Saalok 			 * The first byte in a compressed segment is a flag
59287117650Saalok 			 * that indicates whether this segment is compressed
59387117650Saalok 			 * at all
59487117650Saalok 			 */
59587117650Saalok 			if (*cmpbuf == UNCOMPRESSED) {
59687117650Saalok 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
59787117650Saalok 				    (cmpbytes - SEGHDR));
59887117650Saalok 			} else {
59987117650Saalok 				seglen = lsp->ls_uncomp_seg_sz;
60087117650Saalok 
60187117650Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
60287117650Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
60387117650Saalok 				    &seglen, li->l_level) != 0) {
60487117650Saalok 					error = EIO;
60587117650Saalok 					goto done;
60687117650Saalok 				}
60787117650Saalok 			}
60887117650Saalok 
60987117650Saalok 			/*
61087117650Saalok 			 * Determine how much uncompressed data we
61187117650Saalok 			 * have to copy and copy it
61287117650Saalok 			 */
61387117650Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
61487117650Saalok 			if (i == eblkno) {
61587117650Saalok 				if (i == (lsp->ls_comp_index_sz - 1))
61687117650Saalok 					xfersize -= (lsp->ls_uncomp_last_seg_sz
61787117650Saalok 					    - eblkoff);
61887117650Saalok 				else
61987117650Saalok 					xfersize -=
62087117650Saalok 					    (lsp->ls_uncomp_seg_sz - eblkoff);
62187117650Saalok 			}
62287117650Saalok 
62387117650Saalok 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
62487117650Saalok 
62587117650Saalok 			cmpbuf += cmpbytes;
62687117650Saalok 			bufaddr += xfersize;
62787117650Saalok 			bp->b_resid -= xfersize;
62887117650Saalok 			sblkoff = 0;
62987117650Saalok 
63087117650Saalok 			if (bp->b_resid == 0)
63187117650Saalok 				break;
63287117650Saalok 		}
6337c478bd9Sstevel@tonic-gate 	} else {
6347c478bd9Sstevel@tonic-gate 		ssize_t	resid;
6357c478bd9Sstevel@tonic-gate 		enum uio_rw rw;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ)
6387c478bd9Sstevel@tonic-gate 			rw = UIO_READ;
6397c478bd9Sstevel@tonic-gate 		else
6407c478bd9Sstevel@tonic-gate 			rw = UIO_WRITE;
6417c478bd9Sstevel@tonic-gate 		error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount,
6427c478bd9Sstevel@tonic-gate 		    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
6437c478bd9Sstevel@tonic-gate 		bp->b_resid = resid;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
64687117650Saalok done:
64787117650Saalok 	if (compressed_seg != NULL)
64887117650Saalok 		kmem_free(compressed_seg, comp_data_sz);
64987117650Saalok 	if (uncompressed_seg != NULL)
65087117650Saalok 		kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
65187117650Saalok 
6527c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
6537c478bd9Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
6547c478bd9Sstevel@tonic-gate 		kstat_io_t *kioptr;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
6577c478bd9Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
6587c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
6597c478bd9Sstevel@tonic-gate 			kioptr->nread += n_done;
6607c478bd9Sstevel@tonic-gate 			kioptr->reads++;
6617c478bd9Sstevel@tonic-gate 		} else {
6627c478bd9Sstevel@tonic-gate 			kioptr->nwritten += n_done;
6637c478bd9Sstevel@tonic-gate 			kioptr->writes++;
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
6667c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
6677c478bd9Sstevel@tonic-gate 	}
6683d7072f8Seschrock 
6693d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
6703d7072f8Seschrock 	if (--lsp->ls_vp_iocount == 0)
6713d7072f8Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
6723d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
6733d7072f8Seschrock 
6747c478bd9Sstevel@tonic-gate 	bioerror(bp, error);
6757c478bd9Sstevel@tonic-gate 	biodone(bp);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate static int
6797c478bd9Sstevel@tonic-gate lofi_strategy(struct buf *bp)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
6827c478bd9Sstevel@tonic-gate 	offset_t	offset;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/*
6857c478bd9Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
6867c478bd9Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
6877c478bd9Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
6887c478bd9Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
6897c478bd9Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
6907c478bd9Sstevel@tonic-gate 	 *
6917c478bd9Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
6927c478bd9Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
6937c478bd9Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
6963d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
6973d7072f8Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
6983d7072f8Seschrock 		bioerror(bp, EIO);
6993d7072f8Seschrock 		biodone(bp);
7003d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
7013d7072f8Seschrock 		return (0);
7023d7072f8Seschrock 	}
7033d7072f8Seschrock 
7047c478bd9Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
7057c478bd9Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
7067c478bd9Sstevel@tonic-gate 		/* EOF */
7077c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
7087c478bd9Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
7097c478bd9Sstevel@tonic-gate 			bioerror(bp, 0);
7107c478bd9Sstevel@tonic-gate 		} else {
7117c478bd9Sstevel@tonic-gate 			/* writes should fail */
7127c478bd9Sstevel@tonic-gate 			bioerror(bp, ENXIO);
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		biodone(bp);
7153d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
7167c478bd9Sstevel@tonic-gate 		return (0);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
7197c478bd9Sstevel@tonic-gate 		bioerror(bp, ENXIO);
7207c478bd9Sstevel@tonic-gate 		biodone(bp);
7213d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
7227c478bd9Sstevel@tonic-gate 		return (0);
7237c478bd9Sstevel@tonic-gate 	}
7243d7072f8Seschrock 	lsp->ls_vp_iocount++;
7253d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
7263d7072f8Seschrock 
7277c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
7287c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
7297c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
7307c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
7337c478bd9Sstevel@tonic-gate 	return (0);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7377c478bd9Sstevel@tonic-gate static int
7387c478bd9Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
7417c478bd9Sstevel@tonic-gate 		return (EINVAL);
7427c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7467c478bd9Sstevel@tonic-gate static int
7477c478bd9Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
7507c478bd9Sstevel@tonic-gate 		return (EINVAL);
7517c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7557c478bd9Sstevel@tonic-gate static int
7567c478bd9Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
7597c478bd9Sstevel@tonic-gate 		return (EINVAL);
7607c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7647c478bd9Sstevel@tonic-gate static int
7657c478bd9Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
7687c478bd9Sstevel@tonic-gate 		return (EINVAL);
7697c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7737c478bd9Sstevel@tonic-gate static int
7747c478bd9Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7757c478bd9Sstevel@tonic-gate {
7767c478bd9Sstevel@tonic-gate 	switch (infocmd) {
7777c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
7787c478bd9Sstevel@tonic-gate 		*result = lofi_dip;
7797c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7807c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
7817c478bd9Sstevel@tonic-gate 		*result = 0;
7827c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate static int
7887c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	int	error;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
7937c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7947c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
7957c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
7967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
7997c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
8007c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
8017c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
8027c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8037c478bd9Sstevel@tonic-gate 	}
804843e1988Sjohnlev 	/* driver handles kernel-issued IOCTLs */
805843e1988Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
806843e1988Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
807843e1988Sjohnlev 		ddi_remove_minor_node(dip, NULL);
808843e1988Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
809843e1988Sjohnlev 		return (DDI_FAILURE);
810843e1988Sjohnlev 	}
8117c478bd9Sstevel@tonic-gate 	lofi_dip = dip;
8127c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
8137c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate static int
8177c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
8207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8217c478bd9Sstevel@tonic-gate 	if (lofi_busy())
8227c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8237c478bd9Sstevel@tonic-gate 	lofi_dip = NULL;
8247c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
825843e1988Sjohnlev 	ddi_prop_remove_all(dip);
8267c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
8277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate /*
8317c478bd9Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
8327c478bd9Sstevel@tonic-gate  * the lofi_ioctl structure.
8337c478bd9Sstevel@tonic-gate  */
8347c478bd9Sstevel@tonic-gate struct lofi_ioctl *
835bd07e074Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
8387c478bd9Sstevel@tonic-gate 	int	error;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
841bd07e074Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
8427c478bd9Sstevel@tonic-gate 	if (error) {
8437c478bd9Sstevel@tonic-gate 		kmem_free(klip, sizeof (struct lofi_ioctl));
8447c478bd9Sstevel@tonic-gate 		return (NULL);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
8487c478bd9Sstevel@tonic-gate 	klip->li_filename[MAXPATHLEN] = '\0';
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* validate minor number */
8517c478bd9Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
8527c478bd9Sstevel@tonic-gate 		kmem_free(klip, sizeof (struct lofi_ioctl));
8537c478bd9Sstevel@tonic-gate 		return (NULL);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 	return (klip);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate int
859bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
860bd07e074Sheppo 	int flag)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	int	error;
8637c478bd9Sstevel@tonic-gate 
864bd07e074Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
8657c478bd9Sstevel@tonic-gate 	if (error)
8667c478bd9Sstevel@tonic-gate 		return (EFAULT);
8677c478bd9Sstevel@tonic-gate 	return (0);
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate void
8717c478bd9Sstevel@tonic-gate free_lofi_ioctl(struct lofi_ioctl *klip)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate 	kmem_free(klip, sizeof (struct lofi_ioctl));
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate static int
8807c478bd9Sstevel@tonic-gate file_to_minor(char *filename)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	minor_t	minor;
8837c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
8867c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
8877c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
8887c478bd9Sstevel@tonic-gate 		if (lsp == NULL)
8897c478bd9Sstevel@tonic-gate 			continue;
8907c478bd9Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
8917c478bd9Sstevel@tonic-gate 			return (minor);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	return (0);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /*
8977c478bd9Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
8987c478bd9Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
8997c478bd9Sstevel@tonic-gate  */
9007c478bd9Sstevel@tonic-gate static int
9017c478bd9Sstevel@tonic-gate valid_filename(const char *filename)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
9047c478bd9Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/* must be absolute path */
9077c478bd9Sstevel@tonic-gate 	if (filename[0] != '/')
9087c478bd9Sstevel@tonic-gate 		return (0);
9097c478bd9Sstevel@tonic-gate 	/* must not be lofi */
9107c478bd9Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
9117c478bd9Sstevel@tonic-gate 		return (0);
9127c478bd9Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
9137c478bd9Sstevel@tonic-gate 		return (0);
9147c478bd9Sstevel@tonic-gate 	return (1);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
9197c478bd9Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
9207c478bd9Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
9217c478bd9Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
92263360950Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
9237c478bd9Sstevel@tonic-gate  * have to support it.
9247c478bd9Sstevel@tonic-gate  */
9257c478bd9Sstevel@tonic-gate static void
9267c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
9297c478bd9Sstevel@tonic-gate 	/*
9307c478bd9Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
9317c478bd9Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
9327c478bd9Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
9337c478bd9Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
9347c478bd9Sstevel@tonic-gate 	 * a number between one and one).
9357c478bd9Sstevel@tonic-gate 	 *
9367c478bd9Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
9377c478bd9Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
9387c478bd9Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */
9417c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024);
9427c478bd9Sstevel@tonic-gate 	else
9437c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024);
9447c478bd9Sstevel@tonic-gate 	/* in case file file is < 100k */
9457c478bd9Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
9467c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
9477c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
9487c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
9497c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
9507c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
9517c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
9527c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
9537c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
9547c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
9557c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
9567c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
9577c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size /
9587c478bd9Sstevel@tonic-gate 	    (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
9597c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
9607c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
9637c478bd9Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
9647c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
9657c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
9667c478bd9Sstevel@tonic-gate 	bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7);
9677c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
9687c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
9697c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
97087117650Saalok 
97187117650Saalok 	/*
97287117650Saalok 	 * A compressed file is read-only, other files can
97387117650Saalok 	 * be read-write
97487117650Saalok 	 */
97587117650Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
97687117650Saalok 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
97787117650Saalok 	} else {
9787c478bd9Sstevel@tonic-gate 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
97987117650Saalok 	}
9807c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
9817c478bd9Sstevel@tonic-gate 	/*
9827c478bd9Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
9837c478bd9Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
9847c478bd9Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
9857c478bd9Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
9887c478bd9Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
9917c478bd9Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
9927c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
9937c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
9947c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
9957c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
9967c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
9977c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
9987c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
9997c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
10007c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
10017c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
10027c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
10037c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
10047c478bd9Sstevel@tonic-gate 	/*
10057c478bd9Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
10067c478bd9Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
10077c478bd9Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
10087c478bd9Sstevel@tonic-gate 	 * maxcontig is 0.
10097c478bd9Sstevel@tonic-gate 	 */
10107c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate /*
101487117650Saalok  * map in a compressed file
101587117650Saalok  *
101687117650Saalok  * Read in the header and the index that follows.
101787117650Saalok  *
101887117650Saalok  * The header is as follows -
101987117650Saalok  *
102087117650Saalok  * Signature (name of the compression algorithm)
102187117650Saalok  * Compression segment size (a multiple of 512)
102287117650Saalok  * Number of index entries
102387117650Saalok  * Size of the last block
102487117650Saalok  * The array containing the index entries
102587117650Saalok  *
102687117650Saalok  * The header information is always stored in
102787117650Saalok  * network byte order on disk.
102887117650Saalok  */
102987117650Saalok static int
103087117650Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
103187117650Saalok {
103287117650Saalok 	uint32_t index_sz, header_len, i;
103387117650Saalok 	ssize_t	resid;
103487117650Saalok 	enum uio_rw rw;
103587117650Saalok 	char *tbuf = buf;
103687117650Saalok 	int error;
103787117650Saalok 
103887117650Saalok 	/* The signature has already been read */
103987117650Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
104087117650Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
104187117650Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
104287117650Saalok 
104387117650Saalok 	/*
104487117650Saalok 	 * The compressed segment size must be a power of 2
104587117650Saalok 	 */
104687117650Saalok 	if (lsp->ls_uncomp_seg_sz % 2)
104787117650Saalok 		return (EINVAL);
104887117650Saalok 
104987117650Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
105087117650Saalok 		;
105187117650Saalok 
105287117650Saalok 	lsp->ls_comp_seg_shift = i;
105387117650Saalok 
105487117650Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
105587117650Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
105687117650Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
105787117650Saalok 
105887117650Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
105987117650Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
106087117650Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
106187117650Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
106287117650Saalok 
106387117650Saalok 	/*
106487117650Saalok 	 * Compute the total size of the uncompressed data
106587117650Saalok 	 * for use in fake_disk_geometry and other calculations.
106687117650Saalok 	 * Disk geometry has to be faked with respect to the
106787117650Saalok 	 * actual uncompressed data size rather than the
106887117650Saalok 	 * compressed file size.
106987117650Saalok 	 */
107087117650Saalok 	lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
107187117650Saalok 	    + lsp->ls_uncomp_last_seg_sz;
107287117650Saalok 
107387117650Saalok 	/*
107487117650Saalok 	 * Index size is rounded up to a 512 byte boundary for ease
107587117650Saalok 	 * of segmapping
107687117650Saalok 	 */
107787117650Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
107887117650Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
107987117650Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
108087117650Saalok 	    sizeof (lsp->ls_comp_index_sz) +
108187117650Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
108287117650Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
108387117650Saalok 
108487117650Saalok 	index_sz += header_len;
108587117650Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
108687117650Saalok 
108787117650Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
108887117650Saalok 	lsp->ls_comp_index_data_sz = index_sz;
108987117650Saalok 
109087117650Saalok 	/*
109187117650Saalok 	 * Read in the index -- this has a side-effect
109287117650Saalok 	 * of reading in the header as well
109387117650Saalok 	 */
109487117650Saalok 	rw = UIO_READ;
109587117650Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
109687117650Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
109787117650Saalok 
109887117650Saalok 	if (error != 0)
109987117650Saalok 		return (error);
110087117650Saalok 
110187117650Saalok 	/* Skip the header, this is where the index really begins */
110287117650Saalok 	lsp->ls_comp_seg_index =
110387117650Saalok 	    /*LINTED*/
110487117650Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
110587117650Saalok 
110687117650Saalok 	/*
110787117650Saalok 	 * Now recompute offsets in the index to account for
110887117650Saalok 	 * the header length
110987117650Saalok 	 */
111087117650Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
111187117650Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
111287117650Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
111387117650Saalok 	}
111487117650Saalok 
111587117650Saalok 	return (error);
111687117650Saalok }
111787117650Saalok 
111887117650Saalok /*
111987117650Saalok  * Check to see if the passed in signature is a valid
112087117650Saalok  * one. If it is valid, return the index into
112187117650Saalok  * lofi_compress_table.
112287117650Saalok  *
112387117650Saalok  * Return -1 if it is invalid
112487117650Saalok  */
112587117650Saalok static int lofi_compress_select(char *signature)
112687117650Saalok {
112787117650Saalok 	int i;
112887117650Saalok 
112987117650Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
113087117650Saalok 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
113187117650Saalok 			return (i);
113287117650Saalok 	}
113387117650Saalok 
113487117650Saalok 	return (-1);
113587117650Saalok }
113687117650Saalok 
113787117650Saalok /*
11387c478bd9Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
11397c478bd9Sstevel@tonic-gate  */
11407c478bd9Sstevel@tonic-gate static int
11417c478bd9Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
1142bd07e074Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	minor_t	newminor;
11457c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
11467c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
11477c478bd9Sstevel@tonic-gate 	int	error;
11487c478bd9Sstevel@tonic-gate 	struct vnode *vp;
11497c478bd9Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
11507c478bd9Sstevel@tonic-gate 	int64_t	Size_prop_val;
115187117650Saalok 	int	compress_index;
11527c478bd9Sstevel@tonic-gate 	vattr_t	vattr;
11537c478bd9Sstevel@tonic-gate 	int	flag;
11547c478bd9Sstevel@tonic-gate 	enum vtype v_type;
11557c478bd9Sstevel@tonic-gate 	int zalloced = 0;
11563d7072f8Seschrock 	dev_t	newdev;
11573d7072f8Seschrock 	char	namebuf[50];
115887117650Saalok 	char 	buf[DEV_BSIZE];
115987117650Saalok 	char 	*tbuf;
116087117650Saalok 	ssize_t	resid;
116187117650Saalok 	enum uio_rw rw;
11627c478bd9Sstevel@tonic-gate 
1163bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
11647c478bd9Sstevel@tonic-gate 	if (klip == NULL)
11657c478bd9Sstevel@tonic-gate 		return (EFAULT);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
11707c478bd9Sstevel@tonic-gate 		error = EINVAL;
11717c478bd9Sstevel@tonic-gate 		goto out;
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
11757c478bd9Sstevel@tonic-gate 		error = EBUSY;
11767c478bd9Sstevel@tonic-gate 		goto out;
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (pickminor) {
11807c478bd9Sstevel@tonic-gate 		/* Find a free one */
11817c478bd9Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
11827c478bd9Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
11837c478bd9Sstevel@tonic-gate 				break;
11847c478bd9Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
11857c478bd9Sstevel@tonic-gate 			error = EAGAIN;
11867c478bd9Sstevel@tonic-gate 			goto out;
11877c478bd9Sstevel@tonic-gate 		}
11887c478bd9Sstevel@tonic-gate 	} else {
11897c478bd9Sstevel@tonic-gate 		newminor = klip->li_minor;
11907c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
11917c478bd9Sstevel@tonic-gate 			error = EEXIST;
11927c478bd9Sstevel@tonic-gate 			goto out;
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/* make sure it's valid */
11977c478bd9Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
11987c478bd9Sstevel@tonic-gate 	    NULLVPP, &vp);
11997c478bd9Sstevel@tonic-gate 	if (error) {
12007c478bd9Sstevel@tonic-gate 		goto out;
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 	v_type = vp->v_type;
12037c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
12047c478bd9Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
12057c478bd9Sstevel@tonic-gate 		error = EINVAL;
12067c478bd9Sstevel@tonic-gate 		goto out;
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
12097c478bd9Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
12107c478bd9Sstevel@tonic-gate 	if (error) {
12117c478bd9Sstevel@tonic-gate 		/* try read-only */
12127c478bd9Sstevel@tonic-gate 		flag &= ~FWRITE;
12137c478bd9Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
12147c478bd9Sstevel@tonic-gate 		    &vp, 0, 0);
12157c478bd9Sstevel@tonic-gate 		if (error) {
12167c478bd9Sstevel@tonic-gate 			goto out;
12177c478bd9Sstevel@tonic-gate 		}
12187c478bd9Sstevel@tonic-gate 	}
12197c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
1220da6c28aaSamw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
12217c478bd9Sstevel@tonic-gate 	if (error) {
12227c478bd9Sstevel@tonic-gate 		goto closeout;
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
12257c478bd9Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
12267c478bd9Sstevel@tonic-gate 		error = EINVAL;
12277c478bd9Sstevel@tonic-gate 		goto closeout;
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
12307c478bd9Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
12317c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
12327c478bd9Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
12337c478bd9Sstevel@tonic-gate 		error = EINVAL;
12347c478bd9Sstevel@tonic-gate 		goto closeout;
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
12377c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
12387c478bd9Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
12397c478bd9Sstevel@tonic-gate 		error = EINVAL;
12407c478bd9Sstevel@tonic-gate 		goto propout;
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
12437c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
12447c478bd9Sstevel@tonic-gate 		error = ENOMEM;
12457c478bd9Sstevel@tonic-gate 		goto propout;
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 	zalloced = 1;
12487c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
12497c478bd9Sstevel@tonic-gate 	(void) ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
12507c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
12517c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
12527c478bd9Sstevel@tonic-gate 		error = ENXIO;
12537c478bd9Sstevel@tonic-gate 		goto propout;
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
12567c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
12577c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
12587c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
12597c478bd9Sstevel@tonic-gate 		/* remove block node */
12607c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
12617c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
12627c478bd9Sstevel@tonic-gate 		error = ENXIO;
12637c478bd9Sstevel@tonic-gate 		goto propout;
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
12667c478bd9Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
12677c478bd9Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
12687c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
12697c478bd9Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
12707c478bd9Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
12717c478bd9Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
12727c478bd9Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
12737c478bd9Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
12747c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
12757c478bd9Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
12767c478bd9Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
12777c478bd9Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
12787c478bd9Sstevel@tonic-gate 	}
12793d7072f8Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
12803d7072f8Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
12813d7072f8Seschrock 
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
12847c478bd9Sstevel@tonic-gate 	 * updated correctly.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	lsp->ls_openflag = flag;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
12907c478bd9Sstevel@tonic-gate 	 */
12917c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
1292da6c28aaSamw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
12937c478bd9Sstevel@tonic-gate 			lsp->ls_vp = vp;
12947c478bd9Sstevel@tonic-gate 		} else {
12957c478bd9Sstevel@tonic-gate 			/*
12967c478bd9Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
12977c478bd9Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
12987c478bd9Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
12997c478bd9Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
13007c478bd9Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
13017c478bd9Sstevel@tonic-gate 			 * the realvp so things behave as if we've
13027c478bd9Sstevel@tonic-gate 			 * opened the realvp without any interaction
13037c478bd9Sstevel@tonic-gate 			 * with lofs.
13047c478bd9Sstevel@tonic-gate 			 */
13057c478bd9Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
13067c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
13077c478bd9Sstevel@tonic-gate 		}
13087c478bd9Sstevel@tonic-gate 	} else {
13097c478bd9Sstevel@tonic-gate 		lsp->ls_vp = vp;
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
13127c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
13137c478bd9Sstevel@tonic-gate 	if (rvalp)
13147c478bd9Sstevel@tonic-gate 		*rvalp = (int)newminor;
13157c478bd9Sstevel@tonic-gate 	klip->li_minor = newminor;
13167c478bd9Sstevel@tonic-gate 
131787117650Saalok 	/*
131887117650Saalok 	 * Read the file signature to check if it is compressed.
131987117650Saalok 	 * 'rw' is set to read since only reads are allowed to
132087117650Saalok 	 * a compressed file.
132187117650Saalok 	 */
132287117650Saalok 	rw = UIO_READ;
132387117650Saalok 	error = vn_rdwr(rw, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
132487117650Saalok 	    0, RLIM64_INFINITY, kcred, &resid);
132587117650Saalok 
132687117650Saalok 	if (error != 0)
132787117650Saalok 		goto propout;
132887117650Saalok 
132987117650Saalok 	tbuf = buf;
133087117650Saalok 	lsp->ls_uncomp_seg_sz = 0;
133187117650Saalok 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
133287117650Saalok 	lsp->ls_comp_algorithm[0] = '\0';
133387117650Saalok 
133487117650Saalok 	compress_index = lofi_compress_select(tbuf);
133587117650Saalok 	if (compress_index != -1) {
133687117650Saalok 		lsp->ls_comp_algorithm_index = compress_index;
133787117650Saalok 		(void) strlcpy(lsp->ls_comp_algorithm,
133887117650Saalok 		    lofi_compress_table[compress_index].l_name,
133987117650Saalok 		    sizeof (lsp->ls_comp_algorithm));
134087117650Saalok 		error = lofi_map_compressed_file(lsp, buf);
134187117650Saalok 		if (error != 0)
134287117650Saalok 			goto propout;
134387117650Saalok 
134487117650Saalok 		/* update DDI properties */
134587117650Saalok 		Size_prop_val = lsp->ls_vp_size;
134687117650Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
134787117650Saalok 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
134887117650Saalok 			error = EINVAL;
134987117650Saalok 			goto propout;
135087117650Saalok 		}
135187117650Saalok 
135287117650Saalok 		Nblocks_prop_val = lsp->ls_vp_size / DEV_BSIZE;
135387117650Saalok 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
135487117650Saalok 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
135587117650Saalok 			error = EINVAL;
135687117650Saalok 			goto propout;
135787117650Saalok 		}
135887117650Saalok 	}
135987117650Saalok 
13607c478bd9Sstevel@tonic-gate 	fake_disk_geometry(lsp);
13617c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
1362bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
13637c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
13647c478bd9Sstevel@tonic-gate 	return (0);
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate propout:
13677c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
13687c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
13697c478bd9Sstevel@tonic-gate closeout:
1370da6c28aaSamw 	(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
13717c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
13727c478bd9Sstevel@tonic-gate out:
13737c478bd9Sstevel@tonic-gate 	if (zalloced)
13747c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, newminor);
13757c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
13767c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
13777c478bd9Sstevel@tonic-gate 	return (error);
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate /*
13817c478bd9Sstevel@tonic-gate  * unmap a file.
13827c478bd9Sstevel@tonic-gate  */
13837c478bd9Sstevel@tonic-gate static int
13847c478bd9Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
1385bd07e074Sheppo     struct cred *credp, int ioctl_flag)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
13887c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
13897c478bd9Sstevel@tonic-gate 	minor_t	minor;
13907c478bd9Sstevel@tonic-gate 
1391bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
13927c478bd9Sstevel@tonic-gate 	if (klip == NULL)
13937c478bd9Sstevel@tonic-gate 		return (EFAULT);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
13967c478bd9Sstevel@tonic-gate 	if (byfilename) {
13977c478bd9Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
13987c478bd9Sstevel@tonic-gate 	} else {
13997c478bd9Sstevel@tonic-gate 		minor = klip->li_minor;
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 	if (minor == 0) {
14027c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
14037c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
14047c478bd9Sstevel@tonic-gate 		return (ENXIO);
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
14073d7072f8Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
14087c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
14097c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
14107c478bd9Sstevel@tonic-gate 		return (ENXIO);
14117c478bd9Sstevel@tonic-gate 	}
14123d7072f8Seschrock 
14133d7072f8Seschrock 	/*
141493239addSjohnlev 	 * If it's still held open, we'll do one of three things:
141593239addSjohnlev 	 *
141693239addSjohnlev 	 * If no flag is set, just return EBUSY.
141793239addSjohnlev 	 *
141893239addSjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
141993239addSjohnlev 	 * the last user finishes.
142093239addSjohnlev 	 *
142193239addSjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
142293239addSjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
142393239addSjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
142493239addSjohnlev 	 * device will be cleaned up appropriately.
14253d7072f8Seschrock 	 *
14263d7072f8Seschrock 	 * This is complicated by the fact that we may have outstanding
142793239addSjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
142893239addSjohnlev 	 * I/O, we keep a count of the number of outstanding I/O requests, as
142993239addSjohnlev 	 * well as a flag to indicate that no new I/Os should be dispatched.
143093239addSjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
143193239addSjohnlev 	 * and then close the underlying vnode.
14323d7072f8Seschrock 	 */
143393239addSjohnlev 
143493239addSjohnlev 	if (is_opened(lsp)) {
14353d7072f8Seschrock 		if (klip->li_force) {
14363d7072f8Seschrock 			mutex_enter(&lsp->ls_vp_lock);
14373d7072f8Seschrock 			lsp->ls_vp_closereq = B_TRUE;
14383d7072f8Seschrock 			while (lsp->ls_vp_iocount > 0)
14393d7072f8Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
14403d7072f8Seschrock 			(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
1441da6c28aaSamw 			    credp, NULL);
14423d7072f8Seschrock 			VN_RELE(lsp->ls_vp);
14433d7072f8Seschrock 			lsp->ls_vp = NULL;
14443d7072f8Seschrock 			cv_broadcast(&lsp->ls_vp_cv);
14453d7072f8Seschrock 			mutex_exit(&lsp->ls_vp_lock);
14463d7072f8Seschrock 			mutex_exit(&lofi_lock);
14473d7072f8Seschrock 			klip->li_minor = minor;
14483d7072f8Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
14493d7072f8Seschrock 			free_lofi_ioctl(klip);
14503d7072f8Seschrock 			return (0);
145193239addSjohnlev 		} else if (klip->li_cleanup) {
145293239addSjohnlev 			lsp->ls_cleanup = 1;
145393239addSjohnlev 			mutex_exit(&lofi_lock);
145493239addSjohnlev 			free_lofi_ioctl(klip);
145593239addSjohnlev 			return (0);
14563d7072f8Seschrock 		}
145793239addSjohnlev 
14587c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
14597c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
14607c478bd9Sstevel@tonic-gate 		return (EBUSY);
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 
14633d7072f8Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	klip->li_minor = minor;
14667c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
1467bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
14687c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
14697c478bd9Sstevel@tonic-gate 	return (0);
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate /*
14737c478bd9Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
14747c478bd9Sstevel@tonic-gate  * the name.
14757c478bd9Sstevel@tonic-gate  */
14763d7072f8Seschrock /*ARGSUSED*/
14777c478bd9Sstevel@tonic-gate static int
14787c478bd9Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
1479bd07e074Sheppo     struct cred *credp, int ioctl_flag)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
14827c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
14837c478bd9Sstevel@tonic-gate 	int	error;
14847c478bd9Sstevel@tonic-gate 	minor_t	minor;
14857c478bd9Sstevel@tonic-gate 
1486bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
14877c478bd9Sstevel@tonic-gate 	if (klip == NULL)
14887c478bd9Sstevel@tonic-gate 		return (EFAULT);
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	switch (which) {
14917c478bd9Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
14927c478bd9Sstevel@tonic-gate 		minor = klip->li_minor;
14937c478bd9Sstevel@tonic-gate 		if (minor == 0) {
14947c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
14957c478bd9Sstevel@tonic-gate 			return (EINVAL);
14967c478bd9Sstevel@tonic-gate 		}
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
14997c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
15007c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
15017c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
15027c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
15037c478bd9Sstevel@tonic-gate 			return (ENXIO);
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
150687117650Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
150787117650Saalok 		    sizeof (klip->li_algorithm));
15087c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
1509bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
15107c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
15117c478bd9Sstevel@tonic-gate 		return (error);
15127c478bd9Sstevel@tonic-gate 	case LOFI_GET_MINOR:
15137c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
15147c478bd9Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
15157c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
15167c478bd9Sstevel@tonic-gate 		if (klip->li_minor == 0) {
15177c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
15187c478bd9Sstevel@tonic-gate 			return (ENOENT);
15197c478bd9Sstevel@tonic-gate 		}
1520bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
15217c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
15227c478bd9Sstevel@tonic-gate 		return (error);
152387117650Saalok 	case LOFI_CHECK_COMPRESSED:
152487117650Saalok 		mutex_enter(&lofi_lock);
152587117650Saalok 		klip->li_minor = file_to_minor(klip->li_filename);
152687117650Saalok 		mutex_exit(&lofi_lock);
152787117650Saalok 		if (klip->li_minor == 0) {
152887117650Saalok 			free_lofi_ioctl(klip);
152987117650Saalok 			return (ENOENT);
153087117650Saalok 		}
153187117650Saalok 		mutex_enter(&lofi_lock);
153287117650Saalok 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
153387117650Saalok 		if (lsp == NULL) {
153487117650Saalok 			mutex_exit(&lofi_lock);
153587117650Saalok 			free_lofi_ioctl(klip);
153687117650Saalok 			return (ENXIO);
153787117650Saalok 		}
153887117650Saalok 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
153987117650Saalok 
154087117650Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
154187117650Saalok 		    sizeof (klip->li_algorithm));
154287117650Saalok 		mutex_exit(&lofi_lock);
154387117650Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
154487117650Saalok 		free_lofi_ioctl(klip);
154587117650Saalok 		return (error);
15467c478bd9Sstevel@tonic-gate 	default:
15477c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
15487c478bd9Sstevel@tonic-gate 		return (EINVAL);
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate static int
15547c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
15557c478bd9Sstevel@tonic-gate     int *rvalp)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	int	error;
15587c478bd9Sstevel@tonic-gate 	enum dkio_state dkstate;
15597c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
15607c478bd9Sstevel@tonic-gate 	minor_t	minor;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate #ifdef lint
15637c478bd9Sstevel@tonic-gate 	credp = credp;
15647c478bd9Sstevel@tonic-gate #endif
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
15677c478bd9Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
15687c478bd9Sstevel@tonic-gate 	if (minor == 0) {
15697c478bd9Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 		/*
15727c478bd9Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
15737c478bd9Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
15747c478bd9Sstevel@tonic-gate 		 * long as they can open it read-only.
15757c478bd9Sstevel@tonic-gate 		 */
15767c478bd9Sstevel@tonic-gate 		switch (cmd) {
15777c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE:
15787c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
15797c478bd9Sstevel@tonic-gate 				return (EPERM);
1580bd07e074Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
15817c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
15827c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
15837c478bd9Sstevel@tonic-gate 				return (EPERM);
1584bd07e074Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
15857c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
15867c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
15877c478bd9Sstevel@tonic-gate 				return (EPERM);
1588bd07e074Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
15897c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
15907c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
15917c478bd9Sstevel@tonic-gate 				return (EPERM);
1592bd07e074Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
15937c478bd9Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
15947c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
1595bd07e074Sheppo 			    credp, flag));
15967c478bd9Sstevel@tonic-gate 		case LOFI_GET_MINOR:
15977c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
1598bd07e074Sheppo 			    credp, flag));
15997c478bd9Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
1600bd07e074Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
1601bd07e074Sheppo 			    sizeof (lofi_max_files), flag);
16027c478bd9Sstevel@tonic-gate 			if (error)
16037c478bd9Sstevel@tonic-gate 				return (EFAULT);
16047c478bd9Sstevel@tonic-gate 			return (0);
160587117650Saalok 		case LOFI_CHECK_COMPRESSED:
160687117650Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
160787117650Saalok 			    credp, flag));
16087c478bd9Sstevel@tonic-gate 		default:
16097c478bd9Sstevel@tonic-gate 			break;
16107c478bd9Sstevel@tonic-gate 		}
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
16147c478bd9Sstevel@tonic-gate 	if (lsp == NULL)
16157c478bd9Sstevel@tonic-gate 		return (ENXIO);
16167c478bd9Sstevel@tonic-gate 
16173d7072f8Seschrock 	/*
16183d7072f8Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
16193d7072f8Seschrock 	 * EIO as if the device was no longer present.
16203d7072f8Seschrock 	 */
16213d7072f8Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
16223d7072f8Seschrock 		return (EIO);
16233d7072f8Seschrock 
16247c478bd9Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
16257c478bd9Sstevel@tonic-gate 	switch (cmd) {
16267c478bd9Sstevel@tonic-gate 	case DKIOCGVTOC:
16277c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
16287c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
16297c478bd9Sstevel@tonic-gate 			struct vtoc32 vtoc32;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
16327c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
16337c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
16347c478bd9Sstevel@tonic-gate 				return (EFAULT);
16357c478bd9Sstevel@tonic-gate 				break;
16367c478bd9Sstevel@tonic-gate 			}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
16397c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
16407c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
16417c478bd9Sstevel@tonic-gate 				return (EFAULT);
16427c478bd9Sstevel@tonic-gate 			break;
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 		return (0);
16457c478bd9Sstevel@tonic-gate 	case DKIOCINFO:
1646bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
1647bd07e074Sheppo 		    sizeof (struct dk_cinfo), flag);
16487c478bd9Sstevel@tonic-gate 		if (error)
16497c478bd9Sstevel@tonic-gate 			return (EFAULT);
16507c478bd9Sstevel@tonic-gate 		return (0);
16517c478bd9Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
16527c478bd9Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
16537c478bd9Sstevel@tonic-gate 	case DKIOCGGEOM:
1654bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
1655bd07e074Sheppo 		    sizeof (struct dk_geom), flag);
16567c478bd9Sstevel@tonic-gate 		if (error)
16577c478bd9Sstevel@tonic-gate 			return (EFAULT);
16587c478bd9Sstevel@tonic-gate 		return (0);
16597c478bd9Sstevel@tonic-gate 	case DKIOCSTATE:
16603d7072f8Seschrock 		/*
16613d7072f8Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
16623d7072f8Seschrock 		 * a device is forcefully unmapped, then the device transitions
16633d7072f8Seschrock 		 * to the DKIO_DEV_GONE state.
16643d7072f8Seschrock 		 */
16653d7072f8Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
16663d7072f8Seschrock 		    flag) != 0)
16673d7072f8Seschrock 			return (EFAULT);
16683d7072f8Seschrock 
16693d7072f8Seschrock 		mutex_enter(&lsp->ls_vp_lock);
16703d7072f8Seschrock 		while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
16713d7072f8Seschrock 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
16723d7072f8Seschrock 			/*
16733d7072f8Seschrock 			 * By virtue of having the device open, we know that
16743d7072f8Seschrock 			 * 'lsp' will remain valid when we return.
16753d7072f8Seschrock 			 */
16763d7072f8Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
16773d7072f8Seschrock 			    &lsp->ls_vp_lock)) {
16783d7072f8Seschrock 				mutex_exit(&lsp->ls_vp_lock);
16793d7072f8Seschrock 				return (EINTR);
16803d7072f8Seschrock 			}
16813d7072f8Seschrock 		}
16823d7072f8Seschrock 
16833d7072f8Seschrock 		dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
16843d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
16853d7072f8Seschrock 
16863d7072f8Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
16873d7072f8Seschrock 		    sizeof (dkstate), flag) != 0)
16887c478bd9Sstevel@tonic-gate 			return (EFAULT);
16897c478bd9Sstevel@tonic-gate 		return (0);
16907c478bd9Sstevel@tonic-gate 	default:
16917c478bd9Sstevel@tonic-gate 		return (ENOTTY);
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate }
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
16967c478bd9Sstevel@tonic-gate 	lofi_open,		/* open */
16977c478bd9Sstevel@tonic-gate 	lofi_close,		/* close */
16987c478bd9Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
16997c478bd9Sstevel@tonic-gate 	nodev,			/* print */
17007c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
17017c478bd9Sstevel@tonic-gate 	lofi_read,		/* read */
17027c478bd9Sstevel@tonic-gate 	lofi_write,		/* write */
17037c478bd9Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
17047c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
17057c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
17067c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
17077c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
17087c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
17097c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
17107c478bd9Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
17117c478bd9Sstevel@tonic-gate 	CB_REV,
17127c478bd9Sstevel@tonic-gate 	lofi_aread,
17137c478bd9Sstevel@tonic-gate 	lofi_awrite
17147c478bd9Sstevel@tonic-gate };
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = {
17177c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
17187c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
17197c478bd9Sstevel@tonic-gate 	lofi_info,		/* info */
17207c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
17217c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
17227c478bd9Sstevel@tonic-gate 	lofi_attach,		/* attach */
17237c478bd9Sstevel@tonic-gate 	lofi_detach,		/* detach */
17247c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
17257c478bd9Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
17267c478bd9Sstevel@tonic-gate 	NULL			/* no bus operations */
17277c478bd9Sstevel@tonic-gate };
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
17307c478bd9Sstevel@tonic-gate 	&mod_driverops,
17317c478bd9Sstevel@tonic-gate 	"loopback file driver (%I%)",
17327c478bd9Sstevel@tonic-gate 	&lofi_ops,
17337c478bd9Sstevel@tonic-gate };
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
17367c478bd9Sstevel@tonic-gate 	MODREV_1,
17377c478bd9Sstevel@tonic-gate 	&modldrv,
17387c478bd9Sstevel@tonic-gate 	NULL
17397c478bd9Sstevel@tonic-gate };
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate int
17427c478bd9Sstevel@tonic-gate _init(void)
17437c478bd9Sstevel@tonic-gate {
17447c478bd9Sstevel@tonic-gate 	int error;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
17477c478bd9Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
17487c478bd9Sstevel@tonic-gate 	if (error)
17497c478bd9Sstevel@tonic-gate 		return (error);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
17527c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
17537c478bd9Sstevel@tonic-gate 	if (error) {
17547c478bd9Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
17557c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
17567c478bd9Sstevel@tonic-gate 	}
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	return (error);
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate int
17627c478bd9Sstevel@tonic-gate _fini(void)
17637c478bd9Sstevel@tonic-gate {
17647c478bd9Sstevel@tonic-gate 	int	error;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	if (lofi_busy())
17677c478bd9Sstevel@tonic-gate 		return (EBUSY);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
17707c478bd9Sstevel@tonic-gate 	if (error)
17717c478bd9Sstevel@tonic-gate 		return (error);
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
17747c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	return (error);
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate int
17807c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
17817c478bd9Sstevel@tonic-gate {
17827c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
17837c478bd9Sstevel@tonic-gate }
1784