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