xref: /titanic_53/usr/src/uts/common/io/lofi.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 /*
223d7072f8Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
307c478bd9Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
317c478bd9Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
327c478bd9Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
337c478bd9Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
347c478bd9Sstevel@tonic-gate  * mounting images of filesystems.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
377c478bd9Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
387c478bd9Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
397c478bd9Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
407c478bd9Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
417c478bd9Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
427c478bd9Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
437c478bd9Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
447c478bd9Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
457c478bd9Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
487c478bd9Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
497c478bd9Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
507c478bd9Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
517c478bd9Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
527c478bd9Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
557c478bd9Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
567c478bd9Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
577c478bd9Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
587c478bd9Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
597c478bd9Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
607c478bd9Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
617c478bd9Sstevel@tonic-gate  *
623d7072f8Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
633d7072f8Seschrock  * support simulation of hotplug events, an optional force flag is provided.
643d7072f8Seschrock  * If a lofi device is open when a force detach is requested, then the
653d7072f8Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
663d7072f8Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
673d7072f8Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
683d7072f8Seschrock  * detached but not removed.
693d7072f8Seschrock  *
707c478bd9Sstevel@tonic-gate  * Known problems:
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
737c478bd9Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
747c478bd9Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
757c478bd9Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
787c478bd9Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
797c478bd9Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
807c478bd9Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
837c478bd9Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
847c478bd9Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
857c478bd9Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
867c478bd9Sstevel@tonic-gate  *	do. Though it would still be silly.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Interesting things to do:
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
937c478bd9Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
947c478bd9Sstevel@tonic-gate  *	need to fake a geometry. But this is also silly unless you're replacing
957c478bd9Sstevel@tonic-gate  *	metadisk.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	Encryption. tpm would like this. Apparently Windows 2000 has it, and
987c478bd9Sstevel@tonic-gate  *	so does Linux.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #include <sys/types.h>
1027c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1037c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1047c478bd9Sstevel@tonic-gate #include <sys/uio.h>
1057c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1067c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1077c478bd9Sstevel@tonic-gate #include <sys/mman.h>
1087c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1097c478bd9Sstevel@tonic-gate #include <sys/aio_req.h>
1107c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1117c478bd9Sstevel@tonic-gate #include <sys/file.h>
1127c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1137c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1147c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1157c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
1167c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
1177c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
1187c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1197c478bd9Sstevel@tonic-gate #include <sys/filio.h>
1207c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
1217c478bd9Sstevel@tonic-gate #include <sys/open.h>
1227c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1237c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1247c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1257c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* seems safer than having to get the string right many times */
1287c478bd9Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
1297c478bd9Sstevel@tonic-gate #define	SIZE_PROP_NAME	"Size"
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static dev_info_t *lofi_dip;
1327c478bd9Sstevel@tonic-gate static void	*lofi_statep;
1337c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1377c478bd9Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1387c478bd9Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
1397c478bd9Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
1407c478bd9Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1417c478bd9Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
1427c478bd9Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
1437c478bd9Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
1447c478bd9Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
1457c478bd9Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
1487c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static int
1537c478bd9Sstevel@tonic-gate lofi_busy(void)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	minor_t	minor;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
1597c478bd9Sstevel@tonic-gate 	 * help because the device isn't open.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
1627c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
1637c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
1647c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
1657c478bd9Sstevel@tonic-gate 			return (EBUSY);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
1697c478bd9Sstevel@tonic-gate 	return (0);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static int
1737c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
1767c478bd9Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static int
1807c478bd9Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
1837c478bd9Sstevel@tonic-gate 	switch (otyp) {
1847c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
1857c478bd9Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
1867c478bd9Sstevel@tonic-gate 		break;
1877c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
1887c478bd9Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
1897c478bd9Sstevel@tonic-gate 		break;
1907c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
1917c478bd9Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
1927c478bd9Sstevel@tonic-gate 		break;
1937c478bd9Sstevel@tonic-gate 	default:
1947c478bd9Sstevel@tonic-gate 		return (-1);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static void
2007c478bd9Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
2037c478bd9Sstevel@tonic-gate 	switch (otyp) {
2047c478bd9Sstevel@tonic-gate 	case OTYP_CHR:
2057c478bd9Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
2067c478bd9Sstevel@tonic-gate 		break;
2077c478bd9Sstevel@tonic-gate 	case OTYP_BLK:
2087c478bd9Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
2097c478bd9Sstevel@tonic-gate 		break;
2107c478bd9Sstevel@tonic-gate 	case OTYP_LYR:
2117c478bd9Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
2127c478bd9Sstevel@tonic-gate 		break;
2137c478bd9Sstevel@tonic-gate 	default:
2147c478bd9Sstevel@tonic-gate 		break;
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2183d7072f8Seschrock static void
2193d7072f8Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
2203d7072f8Seschrock     cred_t *credp)
2213d7072f8Seschrock {
2223d7072f8Seschrock 	dev_t	newdev;
2233d7072f8Seschrock 	char	namebuf[50];
2243d7072f8Seschrock 
2253d7072f8Seschrock 	if (lsp->ls_vp) {
226*da6c28aaSamw 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
227*da6c28aaSamw 		    1, 0, credp, NULL);
2283d7072f8Seschrock 		VN_RELE(lsp->ls_vp);
2293d7072f8Seschrock 		lsp->ls_vp = NULL;
2303d7072f8Seschrock 	}
2313d7072f8Seschrock 
2323d7072f8Seschrock 	newdev = makedevice(getmajor(dev), minor);
2333d7072f8Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
2343d7072f8Seschrock 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
2353d7072f8Seschrock 
2363d7072f8Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
2373d7072f8Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
2383d7072f8Seschrock 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
2393d7072f8Seschrock 	ddi_remove_minor_node(lofi_dip, namebuf);
2403d7072f8Seschrock 
2413d7072f8Seschrock 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
2423d7072f8Seschrock 	taskq_destroy(lsp->ls_taskq);
2433d7072f8Seschrock 	if (lsp->ls_kstat) {
2443d7072f8Seschrock 		kstat_delete(lsp->ls_kstat);
2453d7072f8Seschrock 		mutex_destroy(&lsp->ls_kstat_lock);
2463d7072f8Seschrock 	}
2473d7072f8Seschrock 	ddi_soft_state_free(lofi_statep, minor);
2483d7072f8Seschrock }
2493d7072f8Seschrock 
2503d7072f8Seschrock /*ARGSUSED*/
2517c478bd9Sstevel@tonic-gate static int
2527c478bd9Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	minor_t	minor;
2557c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
2587c478bd9Sstevel@tonic-gate 	minor = getminor(*devp);
2597c478bd9Sstevel@tonic-gate 	if (minor == 0) {
2607c478bd9Sstevel@tonic-gate 		/* master control device */
2617c478bd9Sstevel@tonic-gate 		/* must be opened exclusively */
2627c478bd9Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
2637c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2647c478bd9Sstevel@tonic-gate 			return (EINVAL);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
2677c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
2687c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2697c478bd9Sstevel@tonic-gate 			return (ENXIO);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 		if (is_opened(lsp)) {
2727c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
2737c478bd9Sstevel@tonic-gate 			return (EBUSY);
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
2767c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
2777c478bd9Sstevel@tonic-gate 		return (0);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
2817c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
2827c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
2837c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
2847c478bd9Sstevel@tonic-gate 		return (EINVAL);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2873d7072f8Seschrock 	if (lsp->ls_vp == NULL) {
2883d7072f8Seschrock 		mutex_exit(&lofi_lock);
2893d7072f8Seschrock 		return (ENXIO);
2903d7072f8Seschrock 	}
2913d7072f8Seschrock 
2927c478bd9Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
2937c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
2947c478bd9Sstevel@tonic-gate 		return (EINVAL);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
2987c478bd9Sstevel@tonic-gate 	return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3013d7072f8Seschrock /*ARGSUSED*/
3027c478bd9Sstevel@tonic-gate static int
3037c478bd9Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	minor_t	minor;
3067c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
3097c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
3107c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
3117c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
3127c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3137c478bd9Sstevel@tonic-gate 		return (EINVAL);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	mark_closed(lsp, otyp);
3163d7072f8Seschrock 
3173d7072f8Seschrock 	/*
3183d7072f8Seschrock 	 * If we have forcibly closed the underlying device, and this is the
3193d7072f8Seschrock 	 * last close, then tear down the rest of the device.
3203d7072f8Seschrock 	 */
3213d7072f8Seschrock 	if (minor != 0 && lsp->ls_vp == NULL && !is_opened(lsp))
3223d7072f8Seschrock 		lofi_free_handle(dev, minor, lsp, credp);
3237c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
3247c478bd9Sstevel@tonic-gate 	return (0);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * This is basically what strategy used to be before we found we
3297c478bd9Sstevel@tonic-gate  * needed task queues.
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate static void
3327c478bd9Sstevel@tonic-gate lofi_strategy_task(void *arg)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	struct buf *bp = (struct buf *)arg;
3357c478bd9Sstevel@tonic-gate 	int error;
3367c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
3377c478bd9Sstevel@tonic-gate 	offset_t	offset, alignedoffset;
3387c478bd9Sstevel@tonic-gate 	offset_t	mapoffset;
3397c478bd9Sstevel@tonic-gate 	caddr_t	bufaddr;
3407c478bd9Sstevel@tonic-gate 	caddr_t	mapaddr;
3417c478bd9Sstevel@tonic-gate 	size_t	xfersize;
3427c478bd9Sstevel@tonic-gate 	size_t	len;
3437c478bd9Sstevel@tonic-gate 	int	isread;
3447c478bd9Sstevel@tonic-gate 	int	smflags;
3457c478bd9Sstevel@tonic-gate 	enum seg_rw srw;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
3487c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
3497c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
3507c478bd9Sstevel@tonic-gate 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
3517c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	bp_mapin(bp);
3547c478bd9Sstevel@tonic-gate 	bufaddr = bp->b_un.b_addr;
3557c478bd9Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
3597c478bd9Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
3607c478bd9Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
3617c478bd9Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
3627c478bd9Sstevel@tonic-gate 	 * mapable or it's a character device.
3637c478bd9Sstevel@tonic-gate 	 */
3643d7072f8Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
3653d7072f8Seschrock 		error = EIO;
3663d7072f8Seschrock 	} else if (((lsp->ls_vp->v_flag & VNOMAP) == 0) &&
3677c478bd9Sstevel@tonic-gate 	    (lsp->ls_vp->v_type != VCHR)) {
3687c478bd9Sstevel@tonic-gate 		/*
3697c478bd9Sstevel@tonic-gate 		 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
3707c478bd9Sstevel@tonic-gate 		 * an 8K boundary, but the buf transfer address may not be
3717c478bd9Sstevel@tonic-gate 		 * aligned on more than a 512-byte boundary (we don't
3727c478bd9Sstevel@tonic-gate 		 * enforce that, though we could). This matters since the
3737c478bd9Sstevel@tonic-gate 		 * initial part of the transfer may not start at offset 0
3747c478bd9Sstevel@tonic-gate 		 * within the segmap'd chunk. So we have to compensate for
3757c478bd9Sstevel@tonic-gate 		 * that with 'mapoffset'. Subsequent chunks always start
3767c478bd9Sstevel@tonic-gate 		 * off at the beginning, and the last is capped by b_resid.
3777c478bd9Sstevel@tonic-gate 		 */
3787c478bd9Sstevel@tonic-gate 		mapoffset = offset & MAXBOFFSET;
3797c478bd9Sstevel@tonic-gate 		alignedoffset = offset - mapoffset;	/* now map-aligned */
3807c478bd9Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
3817c478bd9Sstevel@tonic-gate 		isread = bp->b_flags & B_READ;
3827c478bd9Sstevel@tonic-gate 		srw = isread ? S_READ : S_WRITE;
3837c478bd9Sstevel@tonic-gate 		do {
3847c478bd9Sstevel@tonic-gate 			xfersize = MIN(lsp->ls_vp_size - offset,
3857c478bd9Sstevel@tonic-gate 			    MIN(MAXBSIZE - mapoffset, bp->b_resid));
3867c478bd9Sstevel@tonic-gate 			len = roundup(mapoffset + xfersize, PAGESIZE);
3877c478bd9Sstevel@tonic-gate 			mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
3887c478bd9Sstevel@tonic-gate 			    alignedoffset, MAXBSIZE, 1, srw);
3897c478bd9Sstevel@tonic-gate 			/*
3907c478bd9Sstevel@tonic-gate 			 * Now fault in the pages. This lets us check
3917c478bd9Sstevel@tonic-gate 			 * for errors before we reference mapaddr and
3927c478bd9Sstevel@tonic-gate 			 * try to resolve the fault in bcopy (which would
3937c478bd9Sstevel@tonic-gate 			 * panic instead). And this can easily happen,
3947c478bd9Sstevel@tonic-gate 			 * particularly if you've lofi'd a file over NFS
3957c478bd9Sstevel@tonic-gate 			 * and someone deletes the file on the server.
3967c478bd9Sstevel@tonic-gate 			 */
3977c478bd9Sstevel@tonic-gate 			error = segmap_fault(kas.a_hat, segkmap, mapaddr,
3987c478bd9Sstevel@tonic-gate 			    len, F_SOFTLOCK, srw);
3997c478bd9Sstevel@tonic-gate 			if (error) {
4007c478bd9Sstevel@tonic-gate 				(void) segmap_release(segkmap, mapaddr, 0);
4017c478bd9Sstevel@tonic-gate 				if (FC_CODE(error) == FC_OBJERR)
4027c478bd9Sstevel@tonic-gate 					error = FC_ERRNO(error);
4037c478bd9Sstevel@tonic-gate 				else
4047c478bd9Sstevel@tonic-gate 					error = EIO;
4057c478bd9Sstevel@tonic-gate 				break;
4067c478bd9Sstevel@tonic-gate 			}
4077c478bd9Sstevel@tonic-gate 			smflags = 0;
4087c478bd9Sstevel@tonic-gate 			if (isread) {
4097c478bd9Sstevel@tonic-gate 				bcopy(mapaddr + mapoffset, bufaddr, xfersize);
4107c478bd9Sstevel@tonic-gate 			} else {
4117c478bd9Sstevel@tonic-gate 				smflags |= SM_WRITE;
4127c478bd9Sstevel@tonic-gate 				bcopy(bufaddr, mapaddr + mapoffset, xfersize);
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 			bp->b_resid -= xfersize;
4157c478bd9Sstevel@tonic-gate 			bufaddr += xfersize;
4167c478bd9Sstevel@tonic-gate 			offset += xfersize;
4177c478bd9Sstevel@tonic-gate 			(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
4187c478bd9Sstevel@tonic-gate 			    len, F_SOFTUNLOCK, srw);
4197c478bd9Sstevel@tonic-gate 			error = segmap_release(segkmap, mapaddr, smflags);
4207c478bd9Sstevel@tonic-gate 			/* only the first map may start partial */
4217c478bd9Sstevel@tonic-gate 			mapoffset = 0;
4227c478bd9Sstevel@tonic-gate 			alignedoffset += MAXBSIZE;
4237c478bd9Sstevel@tonic-gate 		} while ((error == 0) && (bp->b_resid > 0) &&
4247c478bd9Sstevel@tonic-gate 		    (offset < lsp->ls_vp_size));
4257c478bd9Sstevel@tonic-gate 	} else {
4267c478bd9Sstevel@tonic-gate 		ssize_t	resid;
4277c478bd9Sstevel@tonic-gate 		enum uio_rw rw;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ)
4307c478bd9Sstevel@tonic-gate 			rw = UIO_READ;
4317c478bd9Sstevel@tonic-gate 		else
4327c478bd9Sstevel@tonic-gate 			rw = UIO_WRITE;
4337c478bd9Sstevel@tonic-gate 		error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount,
4347c478bd9Sstevel@tonic-gate 		    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
4357c478bd9Sstevel@tonic-gate 		bp->b_resid = resid;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
4397c478bd9Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
4407c478bd9Sstevel@tonic-gate 		kstat_io_t *kioptr;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
4437c478bd9Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
4447c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
4457c478bd9Sstevel@tonic-gate 			kioptr->nread += n_done;
4467c478bd9Sstevel@tonic-gate 			kioptr->reads++;
4477c478bd9Sstevel@tonic-gate 		} else {
4487c478bd9Sstevel@tonic-gate 			kioptr->nwritten += n_done;
4497c478bd9Sstevel@tonic-gate 			kioptr->writes++;
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
4527c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
4537c478bd9Sstevel@tonic-gate 	}
4543d7072f8Seschrock 
4553d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
4563d7072f8Seschrock 	if (--lsp->ls_vp_iocount == 0)
4573d7072f8Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
4583d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
4593d7072f8Seschrock 
4607c478bd9Sstevel@tonic-gate 	bioerror(bp, error);
4617c478bd9Sstevel@tonic-gate 	biodone(bp);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate static int
4657c478bd9Sstevel@tonic-gate lofi_strategy(struct buf *bp)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
4687c478bd9Sstevel@tonic-gate 	offset_t	offset;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
4727c478bd9Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
4737c478bd9Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
4747c478bd9Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
4757c478bd9Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
4767c478bd9Sstevel@tonic-gate 	 *
4777c478bd9Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
4787c478bd9Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
4797c478bd9Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
4823d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
4833d7072f8Seschrock 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
4843d7072f8Seschrock 		bioerror(bp, EIO);
4853d7072f8Seschrock 		biodone(bp);
4863d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
4873d7072f8Seschrock 		return (0);
4883d7072f8Seschrock 	}
4893d7072f8Seschrock 
4907c478bd9Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
4917c478bd9Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
4927c478bd9Sstevel@tonic-gate 		/* EOF */
4937c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
4947c478bd9Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
4957c478bd9Sstevel@tonic-gate 			bioerror(bp, 0);
4967c478bd9Sstevel@tonic-gate 		} else {
4977c478bd9Sstevel@tonic-gate 			/* writes should fail */
4987c478bd9Sstevel@tonic-gate 			bioerror(bp, ENXIO);
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		biodone(bp);
5013d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
5027c478bd9Sstevel@tonic-gate 		return (0);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
5057c478bd9Sstevel@tonic-gate 		bioerror(bp, ENXIO);
5067c478bd9Sstevel@tonic-gate 		biodone(bp);
5073d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
5087c478bd9Sstevel@tonic-gate 		return (0);
5097c478bd9Sstevel@tonic-gate 	}
5103d7072f8Seschrock 	lsp->ls_vp_iocount++;
5113d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
5123d7072f8Seschrock 
5137c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
5147c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
5157c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
5167c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
5197c478bd9Sstevel@tonic-gate 	return (0);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
5237c478bd9Sstevel@tonic-gate static int
5247c478bd9Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
5277c478bd9Sstevel@tonic-gate 		return (EINVAL);
5287c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
5327c478bd9Sstevel@tonic-gate static int
5337c478bd9Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
5367c478bd9Sstevel@tonic-gate 		return (EINVAL);
5377c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
5417c478bd9Sstevel@tonic-gate static int
5427c478bd9Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
5457c478bd9Sstevel@tonic-gate 		return (EINVAL);
5467c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
5507c478bd9Sstevel@tonic-gate static int
5517c478bd9Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
5547c478bd9Sstevel@tonic-gate 		return (EINVAL);
5557c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5597c478bd9Sstevel@tonic-gate static int
5607c478bd9Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	switch (infocmd) {
5637c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5647c478bd9Sstevel@tonic-gate 		*result = lofi_dip;
5657c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5667c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5677c478bd9Sstevel@tonic-gate 		*result = 0;
5687c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate static int
5747c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	int	error;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
5797c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5807c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
5817c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
5827c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
5857c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
5867c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
5877c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
5887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5897c478bd9Sstevel@tonic-gate 	}
590843e1988Sjohnlev 	/* driver handles kernel-issued IOCTLs */
591843e1988Sjohnlev 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
592843e1988Sjohnlev 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
593843e1988Sjohnlev 		ddi_remove_minor_node(dip, NULL);
594843e1988Sjohnlev 		ddi_soft_state_free(lofi_statep, 0);
595843e1988Sjohnlev 		return (DDI_FAILURE);
596843e1988Sjohnlev 	}
5977c478bd9Sstevel@tonic-gate 	lofi_dip = dip;
5987c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
5997c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate static int
6037c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
6067c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6077c478bd9Sstevel@tonic-gate 	if (lofi_busy())
6087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6097c478bd9Sstevel@tonic-gate 	lofi_dip = NULL;
6107c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
611843e1988Sjohnlev 	ddi_prop_remove_all(dip);
6127c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
6137c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
6187c478bd9Sstevel@tonic-gate  * the lofi_ioctl structure.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate struct lofi_ioctl *
621bd07e074Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
6247c478bd9Sstevel@tonic-gate 	int	error;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
627bd07e074Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
6287c478bd9Sstevel@tonic-gate 	if (error) {
6297c478bd9Sstevel@tonic-gate 		kmem_free(klip, sizeof (struct lofi_ioctl));
6307c478bd9Sstevel@tonic-gate 		return (NULL);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
6347c478bd9Sstevel@tonic-gate 	klip->li_filename[MAXPATHLEN] = '\0';
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* validate minor number */
6377c478bd9Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
6387c478bd9Sstevel@tonic-gate 		kmem_free(klip, sizeof (struct lofi_ioctl));
6397c478bd9Sstevel@tonic-gate 		return (NULL);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	return (klip);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate int
645bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
646bd07e074Sheppo 	int flag)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	int	error;
6497c478bd9Sstevel@tonic-gate 
650bd07e074Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
6517c478bd9Sstevel@tonic-gate 	if (error)
6527c478bd9Sstevel@tonic-gate 		return (EFAULT);
6537c478bd9Sstevel@tonic-gate 	return (0);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate void
6577c478bd9Sstevel@tonic-gate free_lofi_ioctl(struct lofi_ioctl *klip)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	kmem_free(klip, sizeof (struct lofi_ioctl));
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate static int
6667c478bd9Sstevel@tonic-gate file_to_minor(char *filename)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	minor_t	minor;
6697c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
6727c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
6737c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
6747c478bd9Sstevel@tonic-gate 		if (lsp == NULL)
6757c478bd9Sstevel@tonic-gate 			continue;
6767c478bd9Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
6777c478bd9Sstevel@tonic-gate 			return (minor);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 	return (0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
6847c478bd9Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate static int
6877c478bd9Sstevel@tonic-gate valid_filename(const char *filename)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
6907c478bd9Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/* must be absolute path */
6937c478bd9Sstevel@tonic-gate 	if (filename[0] != '/')
6947c478bd9Sstevel@tonic-gate 		return (0);
6957c478bd9Sstevel@tonic-gate 	/* must not be lofi */
6967c478bd9Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
6977c478bd9Sstevel@tonic-gate 		return (0);
6987c478bd9Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
6997c478bd9Sstevel@tonic-gate 		return (0);
7007c478bd9Sstevel@tonic-gate 	return (1);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
7057c478bd9Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
7067c478bd9Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
7077c478bd9Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
70863360950Smp204432  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
7097c478bd9Sstevel@tonic-gate  * have to support it.
7107c478bd9Sstevel@tonic-gate  */
7117c478bd9Sstevel@tonic-gate static void
7127c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
7177c478bd9Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
7187c478bd9Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
7197c478bd9Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
7207c478bd9Sstevel@tonic-gate 	 * a number between one and one).
7217c478bd9Sstevel@tonic-gate 	 *
7227c478bd9Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
7237c478bd9Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
7247c478bd9Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */
7277c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024);
7287c478bd9Sstevel@tonic-gate 	else
7297c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024);
7307c478bd9Sstevel@tonic-gate 	/* in case file file is < 100k */
7317c478bd9Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
7327c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
7337c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
7347c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
7357c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
7367c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
7377c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
7387c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
7397c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
7407c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
7417c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
7427c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
7437c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size /
7447c478bd9Sstevel@tonic-gate 	    (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
7457c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
7467c478bd9Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
7497c478bd9Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
7507c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
7517c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
7527c478bd9Sstevel@tonic-gate 	bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7);
7537c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
7547c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
7557c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
7567c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
7577c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
7607c478bd9Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
7617c478bd9Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
7627c478bd9Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
7657c478bd9Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
7687c478bd9Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
7697c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
7707c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
7717c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
7727c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
7737c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
7747c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
7757c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
7767c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
7777c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
7787c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
7797c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
7807c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
7817c478bd9Sstevel@tonic-gate 	/*
7827c478bd9Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
7837c478bd9Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
7847c478bd9Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
7857c478bd9Sstevel@tonic-gate 	 * maxcontig is 0.
7867c478bd9Sstevel@tonic-gate 	 */
7877c478bd9Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate static int
7947c478bd9Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
795bd07e074Sheppo     int *rvalp, struct cred *credp, int ioctl_flag)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	minor_t	newminor;
7987c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
7997c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
8007c478bd9Sstevel@tonic-gate 	int	error;
8017c478bd9Sstevel@tonic-gate 	struct vnode *vp;
8027c478bd9Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
8037c478bd9Sstevel@tonic-gate 	int64_t	Size_prop_val;
8047c478bd9Sstevel@tonic-gate 	vattr_t	vattr;
8057c478bd9Sstevel@tonic-gate 	int	flag;
8067c478bd9Sstevel@tonic-gate 	enum vtype v_type;
8077c478bd9Sstevel@tonic-gate 	int zalloced = 0;
8083d7072f8Seschrock 	dev_t	newdev;
8093d7072f8Seschrock 	char	namebuf[50];
8107c478bd9Sstevel@tonic-gate 
811bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
8127c478bd9Sstevel@tonic-gate 	if (klip == NULL)
8137c478bd9Sstevel@tonic-gate 		return (EFAULT);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
8187c478bd9Sstevel@tonic-gate 		error = EINVAL;
8197c478bd9Sstevel@tonic-gate 		goto out;
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
8237c478bd9Sstevel@tonic-gate 		error = EBUSY;
8247c478bd9Sstevel@tonic-gate 		goto out;
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (pickminor) {
8287c478bd9Sstevel@tonic-gate 		/* Find a free one */
8297c478bd9Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
8307c478bd9Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
8317c478bd9Sstevel@tonic-gate 				break;
8327c478bd9Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
8337c478bd9Sstevel@tonic-gate 			error = EAGAIN;
8347c478bd9Sstevel@tonic-gate 			goto out;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 	} else {
8377c478bd9Sstevel@tonic-gate 		newminor = klip->li_minor;
8387c478bd9Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
8397c478bd9Sstevel@tonic-gate 			error = EEXIST;
8407c478bd9Sstevel@tonic-gate 			goto out;
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/* make sure it's valid */
8457c478bd9Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
8467c478bd9Sstevel@tonic-gate 	    NULLVPP, &vp);
8477c478bd9Sstevel@tonic-gate 	if (error) {
8487c478bd9Sstevel@tonic-gate 		goto out;
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 	v_type = vp->v_type;
8517c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
8527c478bd9Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
8537c478bd9Sstevel@tonic-gate 		error = EINVAL;
8547c478bd9Sstevel@tonic-gate 		goto out;
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
8577c478bd9Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
8587c478bd9Sstevel@tonic-gate 	if (error) {
8597c478bd9Sstevel@tonic-gate 		/* try read-only */
8607c478bd9Sstevel@tonic-gate 		flag &= ~FWRITE;
8617c478bd9Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
8627c478bd9Sstevel@tonic-gate 		    &vp, 0, 0);
8637c478bd9Sstevel@tonic-gate 		if (error) {
8647c478bd9Sstevel@tonic-gate 			goto out;
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
868*da6c28aaSamw 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
8697c478bd9Sstevel@tonic-gate 	if (error) {
8707c478bd9Sstevel@tonic-gate 		goto closeout;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
8737c478bd9Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
8747c478bd9Sstevel@tonic-gate 		error = EINVAL;
8757c478bd9Sstevel@tonic-gate 		goto closeout;
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
8787c478bd9Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
8797c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
8807c478bd9Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
8817c478bd9Sstevel@tonic-gate 		error = EINVAL;
8827c478bd9Sstevel@tonic-gate 		goto closeout;
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
8857c478bd9Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
8867c478bd9Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
8877c478bd9Sstevel@tonic-gate 		error = EINVAL;
8887c478bd9Sstevel@tonic-gate 		goto propout;
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
8917c478bd9Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
8927c478bd9Sstevel@tonic-gate 		error = ENOMEM;
8937c478bd9Sstevel@tonic-gate 		goto propout;
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 	zalloced = 1;
8967c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
8977c478bd9Sstevel@tonic-gate 	(void) ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
8987c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
8997c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
9007c478bd9Sstevel@tonic-gate 		error = ENXIO;
9017c478bd9Sstevel@tonic-gate 		goto propout;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
9047c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
9057c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
9067c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
9077c478bd9Sstevel@tonic-gate 		/* remove block node */
9087c478bd9Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
9097c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
9107c478bd9Sstevel@tonic-gate 		error = ENXIO;
9117c478bd9Sstevel@tonic-gate 		goto propout;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
9147c478bd9Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
9157c478bd9Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
9167c478bd9Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
9177c478bd9Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
9187c478bd9Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
9197c478bd9Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
9207c478bd9Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
9217c478bd9Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
9227c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
9237c478bd9Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
9247c478bd9Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
9257c478bd9Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
9267c478bd9Sstevel@tonic-gate 	}
9273d7072f8Seschrock 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
9283d7072f8Seschrock 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
9293d7072f8Seschrock 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
9327c478bd9Sstevel@tonic-gate 	 * updated correctly.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	lsp->ls_openflag = flag;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/*
9377c478bd9Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
9387c478bd9Sstevel@tonic-gate 	 */
9397c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
940*da6c28aaSamw 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
9417c478bd9Sstevel@tonic-gate 			lsp->ls_vp = vp;
9427c478bd9Sstevel@tonic-gate 		} else {
9437c478bd9Sstevel@tonic-gate 			/*
9447c478bd9Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
9457c478bd9Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
9467c478bd9Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
9477c478bd9Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
9487c478bd9Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
9497c478bd9Sstevel@tonic-gate 			 * the realvp so things behave as if we've
9507c478bd9Sstevel@tonic-gate 			 * opened the realvp without any interaction
9517c478bd9Sstevel@tonic-gate 			 * with lofs.
9527c478bd9Sstevel@tonic-gate 			 */
9537c478bd9Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
9547c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 	} else {
9577c478bd9Sstevel@tonic-gate 		lsp->ls_vp = vp;
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
9607c478bd9Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
9617c478bd9Sstevel@tonic-gate 	if (rvalp)
9627c478bd9Sstevel@tonic-gate 		*rvalp = (int)newminor;
9637c478bd9Sstevel@tonic-gate 	klip->li_minor = newminor;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	fake_disk_geometry(lsp);
9667c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
967bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
9687c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
9697c478bd9Sstevel@tonic-gate 	return (0);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate propout:
9727c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
9737c478bd9Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
9747c478bd9Sstevel@tonic-gate closeout:
975*da6c28aaSamw 	(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
9767c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
9777c478bd9Sstevel@tonic-gate out:
9787c478bd9Sstevel@tonic-gate 	if (zalloced)
9797c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, newminor);
9807c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
9817c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
9827c478bd9Sstevel@tonic-gate 	return (error);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * unmap a file.
9877c478bd9Sstevel@tonic-gate  */
9887c478bd9Sstevel@tonic-gate static int
9897c478bd9Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
990bd07e074Sheppo     struct cred *credp, int ioctl_flag)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
9937c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
9947c478bd9Sstevel@tonic-gate 	minor_t	minor;
9957c478bd9Sstevel@tonic-gate 
996bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
9977c478bd9Sstevel@tonic-gate 	if (klip == NULL)
9987c478bd9Sstevel@tonic-gate 		return (EFAULT);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
10017c478bd9Sstevel@tonic-gate 	if (byfilename) {
10027c478bd9Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
10037c478bd9Sstevel@tonic-gate 	} else {
10047c478bd9Sstevel@tonic-gate 		minor = klip->li_minor;
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 	if (minor == 0) {
10077c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
10087c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
10097c478bd9Sstevel@tonic-gate 		return (ENXIO);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
10123d7072f8Seschrock 	if (lsp == NULL || lsp->ls_vp == NULL) {
10137c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
10147c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
10157c478bd9Sstevel@tonic-gate 		return (ENXIO);
10167c478bd9Sstevel@tonic-gate 	}
10173d7072f8Seschrock 
10187c478bd9Sstevel@tonic-gate 	if (is_opened(lsp)) {
10193d7072f8Seschrock 		/*
10203d7072f8Seschrock 		 * If the 'force' flag is set, then we forcibly close the
10213d7072f8Seschrock 		 * underlying file.  Subsequent operations will fail, and the
10223d7072f8Seschrock 		 * DKIOCSTATE ioctl will return DKIO_DEV_GONE.  When the device
10233d7072f8Seschrock 		 * is last closed, the device will be cleaned up appropriately.
10243d7072f8Seschrock 		 *
10253d7072f8Seschrock 		 * This is complicated by the fact that we may have outstanding
10263d7072f8Seschrock 		 * dispatched I/Os.  Rather than having a single mutex to
10273d7072f8Seschrock 		 * serialize all I/O, we keep a count of the number of
10283d7072f8Seschrock 		 * outstanding I/O requests, as well as a flag to indicate that
10293d7072f8Seschrock 		 * no new I/Os should be dispatched.  We set the flag, wait for
10303d7072f8Seschrock 		 * the number of outstanding I/Os to reach 0, and then close the
10313d7072f8Seschrock 		 * underlying vnode.
10323d7072f8Seschrock 		 */
10333d7072f8Seschrock 		if (klip->li_force) {
10343d7072f8Seschrock 			mutex_enter(&lsp->ls_vp_lock);
10353d7072f8Seschrock 			lsp->ls_vp_closereq = B_TRUE;
10363d7072f8Seschrock 			while (lsp->ls_vp_iocount > 0)
10373d7072f8Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
10383d7072f8Seschrock 			(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
1039*da6c28aaSamw 			    credp, NULL);
10403d7072f8Seschrock 			VN_RELE(lsp->ls_vp);
10413d7072f8Seschrock 			lsp->ls_vp = NULL;
10423d7072f8Seschrock 			cv_broadcast(&lsp->ls_vp_cv);
10433d7072f8Seschrock 			mutex_exit(&lsp->ls_vp_lock);
10443d7072f8Seschrock 			mutex_exit(&lofi_lock);
10453d7072f8Seschrock 			klip->li_minor = minor;
10463d7072f8Seschrock 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
10473d7072f8Seschrock 			free_lofi_ioctl(klip);
10483d7072f8Seschrock 			return (0);
10493d7072f8Seschrock 		}
10507c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
10517c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
10527c478bd9Sstevel@tonic-gate 		return (EBUSY);
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10553d7072f8Seschrock 	lofi_free_handle(dev, minor, lsp, credp);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	klip->li_minor = minor;
10587c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
1059bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
10607c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
10617c478bd9Sstevel@tonic-gate 	return (0);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
10667c478bd9Sstevel@tonic-gate  * the name.
10677c478bd9Sstevel@tonic-gate  */
10683d7072f8Seschrock /*ARGSUSED*/
10697c478bd9Sstevel@tonic-gate static int
10707c478bd9Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
1071bd07e074Sheppo     struct cred *credp, int ioctl_flag)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
10747c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
10757c478bd9Sstevel@tonic-gate 	int	error;
10767c478bd9Sstevel@tonic-gate 	minor_t	minor;
10777c478bd9Sstevel@tonic-gate 
1078bd07e074Sheppo 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
10797c478bd9Sstevel@tonic-gate 	if (klip == NULL)
10807c478bd9Sstevel@tonic-gate 		return (EFAULT);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	switch (which) {
10837c478bd9Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
10847c478bd9Sstevel@tonic-gate 		minor = klip->li_minor;
10857c478bd9Sstevel@tonic-gate 		if (minor == 0) {
10867c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
10877c478bd9Sstevel@tonic-gate 			return (EINVAL);
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
10917c478bd9Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
10927c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
10937c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
10947c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
10957c478bd9Sstevel@tonic-gate 			return (ENXIO);
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
10987c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
1099bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
11007c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
11017c478bd9Sstevel@tonic-gate 		return (error);
11027c478bd9Sstevel@tonic-gate 	case LOFI_GET_MINOR:
11037c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
11047c478bd9Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
11057c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
11067c478bd9Sstevel@tonic-gate 		if (klip->li_minor == 0) {
11077c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
11087c478bd9Sstevel@tonic-gate 			return (ENOENT);
11097c478bd9Sstevel@tonic-gate 		}
1110bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
11117c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
11127c478bd9Sstevel@tonic-gate 		return (error);
11137c478bd9Sstevel@tonic-gate 	default:
11147c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
11157c478bd9Sstevel@tonic-gate 		return (EINVAL);
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate static int
11217c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
11227c478bd9Sstevel@tonic-gate     int *rvalp)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	int	error;
11257c478bd9Sstevel@tonic-gate 	enum dkio_state dkstate;
11267c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
11277c478bd9Sstevel@tonic-gate 	minor_t	minor;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate #ifdef lint
11307c478bd9Sstevel@tonic-gate 	credp = credp;
11317c478bd9Sstevel@tonic-gate #endif
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	minor = getminor(dev);
11347c478bd9Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
11357c478bd9Sstevel@tonic-gate 	if (minor == 0) {
11367c478bd9Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		/*
11397c478bd9Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
11407c478bd9Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
11417c478bd9Sstevel@tonic-gate 		 * long as they can open it read-only.
11427c478bd9Sstevel@tonic-gate 		 */
11437c478bd9Sstevel@tonic-gate 		switch (cmd) {
11447c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE:
11457c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
11467c478bd9Sstevel@tonic-gate 				return (EPERM);
1147bd07e074Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
11487c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
11497c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
11507c478bd9Sstevel@tonic-gate 				return (EPERM);
1151bd07e074Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
11527c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
11537c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
11547c478bd9Sstevel@tonic-gate 				return (EPERM);
1155bd07e074Sheppo 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
11567c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
11577c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
11587c478bd9Sstevel@tonic-gate 				return (EPERM);
1159bd07e074Sheppo 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
11607c478bd9Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
11617c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
1162bd07e074Sheppo 			    credp, flag));
11637c478bd9Sstevel@tonic-gate 		case LOFI_GET_MINOR:
11647c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
1165bd07e074Sheppo 			    credp, flag));
11667c478bd9Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
1167bd07e074Sheppo 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
1168bd07e074Sheppo 			    sizeof (lofi_max_files), flag);
11697c478bd9Sstevel@tonic-gate 			if (error)
11707c478bd9Sstevel@tonic-gate 				return (EFAULT);
11717c478bd9Sstevel@tonic-gate 			return (0);
11727c478bd9Sstevel@tonic-gate 		default:
11737c478bd9Sstevel@tonic-gate 			break;
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
11787c478bd9Sstevel@tonic-gate 	if (lsp == NULL)
11797c478bd9Sstevel@tonic-gate 		return (ENXIO);
11807c478bd9Sstevel@tonic-gate 
11813d7072f8Seschrock 	/*
11823d7072f8Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
11833d7072f8Seschrock 	 * EIO as if the device was no longer present.
11843d7072f8Seschrock 	 */
11853d7072f8Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
11863d7072f8Seschrock 		return (EIO);
11873d7072f8Seschrock 
11887c478bd9Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
11897c478bd9Sstevel@tonic-gate 	switch (cmd) {
11907c478bd9Sstevel@tonic-gate 	case DKIOCGVTOC:
11917c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
11927c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
11937c478bd9Sstevel@tonic-gate 			struct vtoc32 vtoc32;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
11967c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
11977c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
11987c478bd9Sstevel@tonic-gate 				return (EFAULT);
11997c478bd9Sstevel@tonic-gate 				break;
12007c478bd9Sstevel@tonic-gate 			}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
12037c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
12047c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
12057c478bd9Sstevel@tonic-gate 				return (EFAULT);
12067c478bd9Sstevel@tonic-gate 			break;
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 		return (0);
12097c478bd9Sstevel@tonic-gate 	case DKIOCINFO:
1210bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
1211bd07e074Sheppo 		    sizeof (struct dk_cinfo), flag);
12127c478bd9Sstevel@tonic-gate 		if (error)
12137c478bd9Sstevel@tonic-gate 			return (EFAULT);
12147c478bd9Sstevel@tonic-gate 		return (0);
12157c478bd9Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
12167c478bd9Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
12177c478bd9Sstevel@tonic-gate 	case DKIOCGGEOM:
1218bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
1219bd07e074Sheppo 		    sizeof (struct dk_geom), flag);
12207c478bd9Sstevel@tonic-gate 		if (error)
12217c478bd9Sstevel@tonic-gate 			return (EFAULT);
12227c478bd9Sstevel@tonic-gate 		return (0);
12237c478bd9Sstevel@tonic-gate 	case DKIOCSTATE:
12243d7072f8Seschrock 		/*
12253d7072f8Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
12263d7072f8Seschrock 		 * a device is forcefully unmapped, then the device transitions
12273d7072f8Seschrock 		 * to the DKIO_DEV_GONE state.
12283d7072f8Seschrock 		 */
12293d7072f8Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
12303d7072f8Seschrock 		    flag) != 0)
12313d7072f8Seschrock 			return (EFAULT);
12323d7072f8Seschrock 
12333d7072f8Seschrock 		mutex_enter(&lsp->ls_vp_lock);
12343d7072f8Seschrock 		while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
12353d7072f8Seschrock 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
12363d7072f8Seschrock 			/*
12373d7072f8Seschrock 			 * By virtue of having the device open, we know that
12383d7072f8Seschrock 			 * 'lsp' will remain valid when we return.
12393d7072f8Seschrock 			 */
12403d7072f8Seschrock 			if (!cv_wait_sig(&lsp->ls_vp_cv,
12413d7072f8Seschrock 			    &lsp->ls_vp_lock)) {
12423d7072f8Seschrock 				mutex_exit(&lsp->ls_vp_lock);
12433d7072f8Seschrock 				return (EINTR);
12443d7072f8Seschrock 			}
12453d7072f8Seschrock 		}
12463d7072f8Seschrock 
12473d7072f8Seschrock 		dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
12483d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
12493d7072f8Seschrock 
12503d7072f8Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
12513d7072f8Seschrock 		    sizeof (dkstate), flag) != 0)
12527c478bd9Sstevel@tonic-gate 			return (EFAULT);
12537c478bd9Sstevel@tonic-gate 		return (0);
12547c478bd9Sstevel@tonic-gate 	default:
12557c478bd9Sstevel@tonic-gate 		return (ENOTTY);
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
12607c478bd9Sstevel@tonic-gate 	lofi_open,		/* open */
12617c478bd9Sstevel@tonic-gate 	lofi_close,		/* close */
12627c478bd9Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
12637c478bd9Sstevel@tonic-gate 	nodev,			/* print */
12647c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
12657c478bd9Sstevel@tonic-gate 	lofi_read,		/* read */
12667c478bd9Sstevel@tonic-gate 	lofi_write,		/* write */
12677c478bd9Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
12687c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
12697c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
12707c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
12717c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
12727c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
12737c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
12747c478bd9Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
12757c478bd9Sstevel@tonic-gate 	CB_REV,
12767c478bd9Sstevel@tonic-gate 	lofi_aread,
12777c478bd9Sstevel@tonic-gate 	lofi_awrite
12787c478bd9Sstevel@tonic-gate };
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = {
12817c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
12827c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
12837c478bd9Sstevel@tonic-gate 	lofi_info,		/* info */
12847c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
12857c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
12867c478bd9Sstevel@tonic-gate 	lofi_attach,		/* attach */
12877c478bd9Sstevel@tonic-gate 	lofi_detach,		/* detach */
12887c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
12897c478bd9Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
12907c478bd9Sstevel@tonic-gate 	NULL			/* no bus operations */
12917c478bd9Sstevel@tonic-gate };
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
12947c478bd9Sstevel@tonic-gate 	&mod_driverops,
12957c478bd9Sstevel@tonic-gate 	"loopback file driver (%I%)",
12967c478bd9Sstevel@tonic-gate 	&lofi_ops,
12977c478bd9Sstevel@tonic-gate };
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
13007c478bd9Sstevel@tonic-gate 	MODREV_1,
13017c478bd9Sstevel@tonic-gate 	&modldrv,
13027c478bd9Sstevel@tonic-gate 	NULL
13037c478bd9Sstevel@tonic-gate };
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate int
13067c478bd9Sstevel@tonic-gate _init(void)
13077c478bd9Sstevel@tonic-gate {
13087c478bd9Sstevel@tonic-gate 	int error;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
13117c478bd9Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
13127c478bd9Sstevel@tonic-gate 	if (error)
13137c478bd9Sstevel@tonic-gate 		return (error);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
13167c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
13177c478bd9Sstevel@tonic-gate 	if (error) {
13187c478bd9Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
13197c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	return (error);
13237c478bd9Sstevel@tonic-gate }
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate int
13267c478bd9Sstevel@tonic-gate _fini(void)
13277c478bd9Sstevel@tonic-gate {
13287c478bd9Sstevel@tonic-gate 	int	error;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	if (lofi_busy())
13317c478bd9Sstevel@tonic-gate 		return (EBUSY);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
13347c478bd9Sstevel@tonic-gate 	if (error)
13357c478bd9Sstevel@tonic-gate 		return (error);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
13387c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	return (error);
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate int
13447c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
13477c478bd9Sstevel@tonic-gate }
1348