xref: /onnv-gate/usr/src/uts/common/io/lofi.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
31*0Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
32*0Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
33*0Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
34*0Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
35*0Sstevel@tonic-gate  * mounting images of filesystems.
36*0Sstevel@tonic-gate  *
37*0Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
38*0Sstevel@tonic-gate  * during attach, and is minor number 0. lofiadm communicates with lofi through
39*0Sstevel@tonic-gate  * ioctls on this device. When a file is attached to lofi, block and character
40*0Sstevel@tonic-gate  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
41*0Sstevel@tonic-gate  * are identified by their minor number, and the minor number is also used
42*0Sstevel@tonic-gate  * as the name in /dev/lofi. If we ever decide to support virtual disks,
43*0Sstevel@tonic-gate  * we'll have to divide the minor number space to identify fdisk partitions
44*0Sstevel@tonic-gate  * and slices, and the name will then be the minor number shifted down a
45*0Sstevel@tonic-gate  * few bits. Minor devices are tracked with state structures handled with
46*0Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
49*0Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
50*0Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
51*0Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
52*0Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
53*0Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
56*0Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
57*0Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
58*0Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
59*0Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
60*0Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
61*0Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
62*0Sstevel@tonic-gate  *
63*0Sstevel@tonic-gate  * Known problems:
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
66*0Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
67*0Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
68*0Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
71*0Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
72*0Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
73*0Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
76*0Sstevel@tonic-gate  *	because you'll be in lofi_ioctl, holding the lock when you open the
77*0Sstevel@tonic-gate  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
78*0Sstevel@tonic-gate  *	now, though not using ddi_soft_state(9F) would make it possible to
79*0Sstevel@tonic-gate  *	do. Though it would still be silly.
80*0Sstevel@tonic-gate  *
81*0Sstevel@tonic-gate  * Interesting things to do:
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
86*0Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
87*0Sstevel@tonic-gate  *	need to fake a geometry. But this is also silly unless you're replacing
88*0Sstevel@tonic-gate  *	metadisk.
89*0Sstevel@tonic-gate  *
90*0Sstevel@tonic-gate  *	Encryption. tpm would like this. Apparently Windows 2000 has it, and
91*0Sstevel@tonic-gate  *	so does Linux.
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #include <sys/types.h>
95*0Sstevel@tonic-gate #include <sys/sysmacros.h>
96*0Sstevel@tonic-gate #include <sys/cmn_err.h>
97*0Sstevel@tonic-gate #include <sys/uio.h>
98*0Sstevel@tonic-gate #include <sys/kmem.h>
99*0Sstevel@tonic-gate #include <sys/cred.h>
100*0Sstevel@tonic-gate #include <sys/mman.h>
101*0Sstevel@tonic-gate #include <sys/errno.h>
102*0Sstevel@tonic-gate #include <sys/aio_req.h>
103*0Sstevel@tonic-gate #include <sys/stat.h>
104*0Sstevel@tonic-gate #include <sys/file.h>
105*0Sstevel@tonic-gate #include <sys/modctl.h>
106*0Sstevel@tonic-gate #include <sys/conf.h>
107*0Sstevel@tonic-gate #include <sys/debug.h>
108*0Sstevel@tonic-gate #include <sys/vnode.h>
109*0Sstevel@tonic-gate #include <sys/lofi.h>
110*0Sstevel@tonic-gate #include <sys/vol.h>
111*0Sstevel@tonic-gate #include <sys/fcntl.h>
112*0Sstevel@tonic-gate #include <sys/pathname.h>
113*0Sstevel@tonic-gate #include <sys/filio.h>
114*0Sstevel@tonic-gate #include <sys/fdio.h>
115*0Sstevel@tonic-gate #include <sys/open.h>
116*0Sstevel@tonic-gate #include <sys/disp.h>
117*0Sstevel@tonic-gate #include <vm/seg_map.h>
118*0Sstevel@tonic-gate #include <sys/ddi.h>
119*0Sstevel@tonic-gate #include <sys/sunddi.h>
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /* seems safer than having to get the string right many times */
122*0Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
123*0Sstevel@tonic-gate #define	SIZE_PROP_NAME	"Size"
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate static dev_info_t *lofi_dip;
126*0Sstevel@tonic-gate static void	*lofi_statep;
127*0Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
131*0Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
132*0Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
133*0Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
134*0Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
135*0Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
136*0Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
137*0Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
138*0Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
139*0Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
140*0Sstevel@tonic-gate  */
141*0Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
142*0Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate static int
147*0Sstevel@tonic-gate lofi_busy(void)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	minor_t	minor;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	/*
152*0Sstevel@tonic-gate 	 * We need to make sure no mappings exist - mod_remove won't
153*0Sstevel@tonic-gate 	 * help because the device isn't open.
154*0Sstevel@tonic-gate 	 */
155*0Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
156*0Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
157*0Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
158*0Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
159*0Sstevel@tonic-gate 			return (EBUSY);
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
163*0Sstevel@tonic-gate 	return (0);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate static int
167*0Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
170*0Sstevel@tonic-gate 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate static int
174*0Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
177*0Sstevel@tonic-gate 	switch (otyp) {
178*0Sstevel@tonic-gate 	case OTYP_CHR:
179*0Sstevel@tonic-gate 		lsp->ls_chr_open = 1;
180*0Sstevel@tonic-gate 		break;
181*0Sstevel@tonic-gate 	case OTYP_BLK:
182*0Sstevel@tonic-gate 		lsp->ls_blk_open = 1;
183*0Sstevel@tonic-gate 		break;
184*0Sstevel@tonic-gate 	case OTYP_LYR:
185*0Sstevel@tonic-gate 		lsp->ls_lyr_open_count++;
186*0Sstevel@tonic-gate 		break;
187*0Sstevel@tonic-gate 	default:
188*0Sstevel@tonic-gate 		return (-1);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 	return (0);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate static void
194*0Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
197*0Sstevel@tonic-gate 	switch (otyp) {
198*0Sstevel@tonic-gate 	case OTYP_CHR:
199*0Sstevel@tonic-gate 		lsp->ls_chr_open = 0;
200*0Sstevel@tonic-gate 		break;
201*0Sstevel@tonic-gate 	case OTYP_BLK:
202*0Sstevel@tonic-gate 		lsp->ls_blk_open = 0;
203*0Sstevel@tonic-gate 		break;
204*0Sstevel@tonic-gate 	case OTYP_LYR:
205*0Sstevel@tonic-gate 		lsp->ls_lyr_open_count--;
206*0Sstevel@tonic-gate 		break;
207*0Sstevel@tonic-gate 	default:
208*0Sstevel@tonic-gate 		break;
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /*ARGSUSED3*/
213*0Sstevel@tonic-gate static int
214*0Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	minor_t	minor;
217*0Sstevel@tonic-gate 	struct lofi_state *lsp;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
220*0Sstevel@tonic-gate 	minor = getminor(*devp);
221*0Sstevel@tonic-gate 	if (minor == 0) {
222*0Sstevel@tonic-gate 		/* master control device */
223*0Sstevel@tonic-gate 		/* must be opened exclusively */
224*0Sstevel@tonic-gate 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
225*0Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
226*0Sstevel@tonic-gate 			return (EINVAL);
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, 0);
229*0Sstevel@tonic-gate 		if (lsp == NULL) {
230*0Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
231*0Sstevel@tonic-gate 			return (ENXIO);
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 		if (is_opened(lsp)) {
234*0Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
235*0Sstevel@tonic-gate 			return (EBUSY);
236*0Sstevel@tonic-gate 		}
237*0Sstevel@tonic-gate 		(void) mark_opened(lsp, OTYP_CHR);
238*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
239*0Sstevel@tonic-gate 		return (0);
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
243*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
244*0Sstevel@tonic-gate 	if (lsp == NULL) {
245*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
246*0Sstevel@tonic-gate 		return (EINVAL);
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	if (mark_opened(lsp, otyp) == -1) {
250*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
251*0Sstevel@tonic-gate 		return (EINVAL);
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
255*0Sstevel@tonic-gate 	return (0);
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate /*ARGSUSED3*/
259*0Sstevel@tonic-gate static int
260*0Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	minor_t	minor;
263*0Sstevel@tonic-gate 	struct lofi_state *lsp;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate #ifdef lint
266*0Sstevel@tonic-gate 	flag = flag;
267*0Sstevel@tonic-gate #endif
268*0Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
269*0Sstevel@tonic-gate 	minor = getminor(dev);
270*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
271*0Sstevel@tonic-gate 	if (lsp == NULL) {
272*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
273*0Sstevel@tonic-gate 		return (EINVAL);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 	mark_closed(lsp, otyp);
276*0Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
277*0Sstevel@tonic-gate 	return (0);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate /*
281*0Sstevel@tonic-gate  * This is basically what strategy used to be before we found we
282*0Sstevel@tonic-gate  * needed task queues.
283*0Sstevel@tonic-gate  */
284*0Sstevel@tonic-gate static void
285*0Sstevel@tonic-gate lofi_strategy_task(void *arg)
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	struct buf *bp = (struct buf *)arg;
288*0Sstevel@tonic-gate 	int error;
289*0Sstevel@tonic-gate 	struct lofi_state *lsp;
290*0Sstevel@tonic-gate 	offset_t	offset, alignedoffset;
291*0Sstevel@tonic-gate 	offset_t	mapoffset;
292*0Sstevel@tonic-gate 	caddr_t	bufaddr;
293*0Sstevel@tonic-gate 	caddr_t	mapaddr;
294*0Sstevel@tonic-gate 	size_t	xfersize;
295*0Sstevel@tonic-gate 	size_t	len;
296*0Sstevel@tonic-gate 	int	isread;
297*0Sstevel@tonic-gate 	int	smflags;
298*0Sstevel@tonic-gate 	enum seg_rw srw;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
301*0Sstevel@tonic-gate 	if (lsp->ls_kstat) {
302*0Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
303*0Sstevel@tonic-gate 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
304*0Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
305*0Sstevel@tonic-gate 	}
306*0Sstevel@tonic-gate 	bp_mapin(bp);
307*0Sstevel@tonic-gate 	bufaddr = bp->b_un.b_addr;
308*0Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	/*
311*0Sstevel@tonic-gate 	 * We used to always use vn_rdwr here, but we cannot do that because
312*0Sstevel@tonic-gate 	 * we might decide to read or write from the the underlying
313*0Sstevel@tonic-gate 	 * file during this call, which would be a deadlock because
314*0Sstevel@tonic-gate 	 * we have the rw_lock. So instead we page, unless it's not
315*0Sstevel@tonic-gate 	 * mapable or it's a character device.
316*0Sstevel@tonic-gate 	 */
317*0Sstevel@tonic-gate 	if (((lsp->ls_vp->v_flag & VNOMAP) == 0) &&
318*0Sstevel@tonic-gate 	    (lsp->ls_vp->v_type != VCHR)) {
319*0Sstevel@tonic-gate 		/*
320*0Sstevel@tonic-gate 		 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
321*0Sstevel@tonic-gate 		 * an 8K boundary, but the buf transfer address may not be
322*0Sstevel@tonic-gate 		 * aligned on more than a 512-byte boundary (we don't
323*0Sstevel@tonic-gate 		 * enforce that, though we could). This matters since the
324*0Sstevel@tonic-gate 		 * initial part of the transfer may not start at offset 0
325*0Sstevel@tonic-gate 		 * within the segmap'd chunk. So we have to compensate for
326*0Sstevel@tonic-gate 		 * that with 'mapoffset'. Subsequent chunks always start
327*0Sstevel@tonic-gate 		 * off at the beginning, and the last is capped by b_resid.
328*0Sstevel@tonic-gate 		 */
329*0Sstevel@tonic-gate 		mapoffset = offset & MAXBOFFSET;
330*0Sstevel@tonic-gate 		alignedoffset = offset - mapoffset;	/* now map-aligned */
331*0Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
332*0Sstevel@tonic-gate 		isread = bp->b_flags & B_READ;
333*0Sstevel@tonic-gate 		srw = isread ? S_READ : S_WRITE;
334*0Sstevel@tonic-gate 		do {
335*0Sstevel@tonic-gate 			xfersize = MIN(lsp->ls_vp_size - offset,
336*0Sstevel@tonic-gate 			    MIN(MAXBSIZE - mapoffset, bp->b_resid));
337*0Sstevel@tonic-gate 			len = roundup(mapoffset + xfersize, PAGESIZE);
338*0Sstevel@tonic-gate 			mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
339*0Sstevel@tonic-gate 			    alignedoffset, MAXBSIZE, 1, srw);
340*0Sstevel@tonic-gate 			/*
341*0Sstevel@tonic-gate 			 * Now fault in the pages. This lets us check
342*0Sstevel@tonic-gate 			 * for errors before we reference mapaddr and
343*0Sstevel@tonic-gate 			 * try to resolve the fault in bcopy (which would
344*0Sstevel@tonic-gate 			 * panic instead). And this can easily happen,
345*0Sstevel@tonic-gate 			 * particularly if you've lofi'd a file over NFS
346*0Sstevel@tonic-gate 			 * and someone deletes the file on the server.
347*0Sstevel@tonic-gate 			 */
348*0Sstevel@tonic-gate 			error = segmap_fault(kas.a_hat, segkmap, mapaddr,
349*0Sstevel@tonic-gate 			    len, F_SOFTLOCK, srw);
350*0Sstevel@tonic-gate 			if (error) {
351*0Sstevel@tonic-gate 				(void) segmap_release(segkmap, mapaddr, 0);
352*0Sstevel@tonic-gate 				if (FC_CODE(error) == FC_OBJERR)
353*0Sstevel@tonic-gate 					error = FC_ERRNO(error);
354*0Sstevel@tonic-gate 				else
355*0Sstevel@tonic-gate 					error = EIO;
356*0Sstevel@tonic-gate 				break;
357*0Sstevel@tonic-gate 			}
358*0Sstevel@tonic-gate 			smflags = 0;
359*0Sstevel@tonic-gate 			if (isread) {
360*0Sstevel@tonic-gate 				bcopy(mapaddr + mapoffset, bufaddr, xfersize);
361*0Sstevel@tonic-gate 			} else {
362*0Sstevel@tonic-gate 				smflags |= SM_WRITE;
363*0Sstevel@tonic-gate 				bcopy(bufaddr, mapaddr + mapoffset, xfersize);
364*0Sstevel@tonic-gate 			}
365*0Sstevel@tonic-gate 			bp->b_resid -= xfersize;
366*0Sstevel@tonic-gate 			bufaddr += xfersize;
367*0Sstevel@tonic-gate 			offset += xfersize;
368*0Sstevel@tonic-gate 			(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
369*0Sstevel@tonic-gate 			    len, F_SOFTUNLOCK, srw);
370*0Sstevel@tonic-gate 			error = segmap_release(segkmap, mapaddr, smflags);
371*0Sstevel@tonic-gate 			/* only the first map may start partial */
372*0Sstevel@tonic-gate 			mapoffset = 0;
373*0Sstevel@tonic-gate 			alignedoffset += MAXBSIZE;
374*0Sstevel@tonic-gate 		} while ((error == 0) && (bp->b_resid > 0) &&
375*0Sstevel@tonic-gate 		    (offset < lsp->ls_vp_size));
376*0Sstevel@tonic-gate 	} else {
377*0Sstevel@tonic-gate 		ssize_t	resid;
378*0Sstevel@tonic-gate 		enum uio_rw rw;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 		if (bp->b_flags & B_READ)
381*0Sstevel@tonic-gate 			rw = UIO_READ;
382*0Sstevel@tonic-gate 		else
383*0Sstevel@tonic-gate 			rw = UIO_WRITE;
384*0Sstevel@tonic-gate 		error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount,
385*0Sstevel@tonic-gate 		    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
386*0Sstevel@tonic-gate 		bp->b_resid = resid;
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if (lsp->ls_kstat) {
390*0Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
391*0Sstevel@tonic-gate 		kstat_io_t *kioptr;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
394*0Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
395*0Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
396*0Sstevel@tonic-gate 			kioptr->nread += n_done;
397*0Sstevel@tonic-gate 			kioptr->reads++;
398*0Sstevel@tonic-gate 		} else {
399*0Sstevel@tonic-gate 			kioptr->nwritten += n_done;
400*0Sstevel@tonic-gate 			kioptr->writes++;
401*0Sstevel@tonic-gate 		}
402*0Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
403*0Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
404*0Sstevel@tonic-gate 	}
405*0Sstevel@tonic-gate 	bioerror(bp, error);
406*0Sstevel@tonic-gate 	biodone(bp);
407*0Sstevel@tonic-gate }
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate static int
410*0Sstevel@tonic-gate lofi_strategy(struct buf *bp)
411*0Sstevel@tonic-gate {
412*0Sstevel@tonic-gate 	struct lofi_state *lsp;
413*0Sstevel@tonic-gate 	offset_t	offset;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	/*
416*0Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
417*0Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
418*0Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
419*0Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
420*0Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
421*0Sstevel@tonic-gate 	 *
422*0Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
423*0Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
424*0Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
425*0Sstevel@tonic-gate 	 */
426*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
427*0Sstevel@tonic-gate 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
428*0Sstevel@tonic-gate 	if (offset == lsp->ls_vp_size) {
429*0Sstevel@tonic-gate 		/* EOF */
430*0Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
431*0Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
432*0Sstevel@tonic-gate 			bioerror(bp, 0);
433*0Sstevel@tonic-gate 		} else {
434*0Sstevel@tonic-gate 			/* writes should fail */
435*0Sstevel@tonic-gate 			bioerror(bp, ENXIO);
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 		biodone(bp);
438*0Sstevel@tonic-gate 		return (0);
439*0Sstevel@tonic-gate 	}
440*0Sstevel@tonic-gate 	if (offset > lsp->ls_vp_size) {
441*0Sstevel@tonic-gate 		bioerror(bp, ENXIO);
442*0Sstevel@tonic-gate 		biodone(bp);
443*0Sstevel@tonic-gate 		return (0);
444*0Sstevel@tonic-gate 	}
445*0Sstevel@tonic-gate 	if (lsp->ls_kstat) {
446*0Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
447*0Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
448*0Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
449*0Sstevel@tonic-gate 	}
450*0Sstevel@tonic-gate 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
451*0Sstevel@tonic-gate 	return (0);
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate /*ARGSUSED2*/
455*0Sstevel@tonic-gate static int
456*0Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate 	if (getminor(dev) == 0)
459*0Sstevel@tonic-gate 		return (EINVAL);
460*0Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
461*0Sstevel@tonic-gate }
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate /*ARGSUSED2*/
464*0Sstevel@tonic-gate static int
465*0Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
466*0Sstevel@tonic-gate {
467*0Sstevel@tonic-gate 	if (getminor(dev) == 0)
468*0Sstevel@tonic-gate 		return (EINVAL);
469*0Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate /*ARGSUSED2*/
473*0Sstevel@tonic-gate static int
474*0Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
475*0Sstevel@tonic-gate {
476*0Sstevel@tonic-gate 	if (getminor(dev) == 0)
477*0Sstevel@tonic-gate 		return (EINVAL);
478*0Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*ARGSUSED2*/
482*0Sstevel@tonic-gate static int
483*0Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
484*0Sstevel@tonic-gate {
485*0Sstevel@tonic-gate 	if (getminor(dev) == 0)
486*0Sstevel@tonic-gate 		return (EINVAL);
487*0Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
488*0Sstevel@tonic-gate }
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate /*ARGSUSED*/
491*0Sstevel@tonic-gate static int
492*0Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
493*0Sstevel@tonic-gate {
494*0Sstevel@tonic-gate 	switch (infocmd) {
495*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
496*0Sstevel@tonic-gate 		*result = lofi_dip;
497*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
498*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
499*0Sstevel@tonic-gate 		*result = 0;
500*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 	return (DDI_FAILURE);
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate static int
506*0Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
507*0Sstevel@tonic-gate {
508*0Sstevel@tonic-gate 	int	error;
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
511*0Sstevel@tonic-gate 		return (DDI_FAILURE);
512*0Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, 0);
513*0Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
514*0Sstevel@tonic-gate 		return (DDI_FAILURE);
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
517*0Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
518*0Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
519*0Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, 0);
520*0Sstevel@tonic-gate 		return (DDI_FAILURE);
521*0Sstevel@tonic-gate 	}
522*0Sstevel@tonic-gate 	lofi_dip = dip;
523*0Sstevel@tonic-gate 	ddi_report_dev(dip);
524*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate static int
528*0Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
529*0Sstevel@tonic-gate {
530*0Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
531*0Sstevel@tonic-gate 		return (DDI_FAILURE);
532*0Sstevel@tonic-gate 	if (lofi_busy())
533*0Sstevel@tonic-gate 		return (DDI_FAILURE);
534*0Sstevel@tonic-gate 	lofi_dip = NULL;
535*0Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
536*0Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
537*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * These two just simplify the rest of the ioctls that need to copyin/out
542*0Sstevel@tonic-gate  * the lofi_ioctl structure.
543*0Sstevel@tonic-gate  */
544*0Sstevel@tonic-gate struct lofi_ioctl *
545*0Sstevel@tonic-gate copy_in_lofi_ioctl(const struct lofi_ioctl *ulip)
546*0Sstevel@tonic-gate {
547*0Sstevel@tonic-gate 	struct lofi_ioctl *klip;
548*0Sstevel@tonic-gate 	int	error;
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
551*0Sstevel@tonic-gate 	error = copyin(ulip, klip, sizeof (struct lofi_ioctl));
552*0Sstevel@tonic-gate 	if (error) {
553*0Sstevel@tonic-gate 		kmem_free(klip, sizeof (struct lofi_ioctl));
554*0Sstevel@tonic-gate 		return (NULL);
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	/* make sure filename is always null-terminated */
558*0Sstevel@tonic-gate 	klip->li_filename[MAXPATHLEN] = '\0';
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	/* validate minor number */
561*0Sstevel@tonic-gate 	if (klip->li_minor > lofi_max_files) {
562*0Sstevel@tonic-gate 		kmem_free(klip, sizeof (struct lofi_ioctl));
563*0Sstevel@tonic-gate 		return (NULL);
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 	return (klip);
566*0Sstevel@tonic-gate }
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate int
569*0Sstevel@tonic-gate copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip)
570*0Sstevel@tonic-gate {
571*0Sstevel@tonic-gate 	int	error;
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	error = copyout(klip, ulip, sizeof (struct lofi_ioctl));
574*0Sstevel@tonic-gate 	if (error)
575*0Sstevel@tonic-gate 		return (EFAULT);
576*0Sstevel@tonic-gate 	return (0);
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate void
580*0Sstevel@tonic-gate free_lofi_ioctl(struct lofi_ioctl *klip)
581*0Sstevel@tonic-gate {
582*0Sstevel@tonic-gate 	kmem_free(klip, sizeof (struct lofi_ioctl));
583*0Sstevel@tonic-gate }
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate /*
586*0Sstevel@tonic-gate  * Return the minor number 'filename' is mapped to, if it is.
587*0Sstevel@tonic-gate  */
588*0Sstevel@tonic-gate static int
589*0Sstevel@tonic-gate file_to_minor(char *filename)
590*0Sstevel@tonic-gate {
591*0Sstevel@tonic-gate 	minor_t	minor;
592*0Sstevel@tonic-gate 	struct lofi_state *lsp;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	ASSERT(mutex_owned(&lofi_lock));
595*0Sstevel@tonic-gate 	for (minor = 1; minor <= lofi_max_files; minor++) {
596*0Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
597*0Sstevel@tonic-gate 		if (lsp == NULL)
598*0Sstevel@tonic-gate 			continue;
599*0Sstevel@tonic-gate 		if (strcmp(lsp->ls_filename, filename) == 0)
600*0Sstevel@tonic-gate 			return (minor);
601*0Sstevel@tonic-gate 	}
602*0Sstevel@tonic-gate 	return (0);
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate /*
606*0Sstevel@tonic-gate  * lofiadm does some validation, but since Joe Random (or crashme) could
607*0Sstevel@tonic-gate  * do our ioctls, we need to do some validation too.
608*0Sstevel@tonic-gate  */
609*0Sstevel@tonic-gate static int
610*0Sstevel@tonic-gate valid_filename(const char *filename)
611*0Sstevel@tonic-gate {
612*0Sstevel@tonic-gate 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
613*0Sstevel@tonic-gate 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	/* must be absolute path */
616*0Sstevel@tonic-gate 	if (filename[0] != '/')
617*0Sstevel@tonic-gate 		return (0);
618*0Sstevel@tonic-gate 	/* must not be lofi */
619*0Sstevel@tonic-gate 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
620*0Sstevel@tonic-gate 		return (0);
621*0Sstevel@tonic-gate 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
622*0Sstevel@tonic-gate 		return (0);
623*0Sstevel@tonic-gate 	return (1);
624*0Sstevel@tonic-gate }
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate /*
627*0Sstevel@tonic-gate  * Fakes up a disk geometry, and one big partition, based on the size
628*0Sstevel@tonic-gate  * of the file. This is needed because we allow newfs'ing the device,
629*0Sstevel@tonic-gate  * and newfs will do several disk ioctls to figure out the geometry and
630*0Sstevel@tonic-gate  * partition information. It uses that information to determine the parameters
631*0Sstevel@tonic-gate  * to pass to mkfs. Geometry is pretty much irrelevent these days, but we
632*0Sstevel@tonic-gate  * have to support it.
633*0Sstevel@tonic-gate  */
634*0Sstevel@tonic-gate static void
635*0Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
636*0Sstevel@tonic-gate {
637*0Sstevel@tonic-gate 	/* dk_geom - see dkio(7I) */
638*0Sstevel@tonic-gate 	/*
639*0Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
640*0Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
641*0Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
642*0Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
643*0Sstevel@tonic-gate 	 * a number between one and one).
644*0Sstevel@tonic-gate 	 *
645*0Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
646*0Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
647*0Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
648*0Sstevel@tonic-gate 	 */
649*0Sstevel@tonic-gate 	if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */
650*0Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024);
651*0Sstevel@tonic-gate 	else
652*0Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024);
653*0Sstevel@tonic-gate 	/* in case file file is < 100k */
654*0Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
655*0Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
656*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_acyl = 0;
657*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_bcyl = 0;
658*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nhead = 1;
659*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs1 = 0;
660*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_intrlv = 0;
661*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs2 = 0;
662*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_obs3 = 0;
663*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_apc = 0;
664*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_rpm = 7200;
665*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
666*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size /
667*0Sstevel@tonic-gate 	    (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
668*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_write_reinstruct = 0;
669*0Sstevel@tonic-gate 	lsp->ls_dkg.dkg_read_reinstruct = 0;
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 	/* vtoc - see dkio(7I) */
672*0Sstevel@tonic-gate 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
673*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
674*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_version = V_VERSION;
675*0Sstevel@tonic-gate 	bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7);
676*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
677*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_nparts = 1;
678*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
679*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
680*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
681*0Sstevel@tonic-gate 	/*
682*0Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
683*0Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
684*0Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
685*0Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
686*0Sstevel@tonic-gate 	 */
687*0Sstevel@tonic-gate 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
688*0Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	/* dk_cinfo - see dkio(7I) */
691*0Sstevel@tonic-gate 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
692*0Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
693*0Sstevel@tonic-gate 	lsp->ls_ci.dki_ctype = DKC_MD;
694*0Sstevel@tonic-gate 	lsp->ls_ci.dki_flags = 0;
695*0Sstevel@tonic-gate 	lsp->ls_ci.dki_cnum = 0;
696*0Sstevel@tonic-gate 	lsp->ls_ci.dki_addr = 0;
697*0Sstevel@tonic-gate 	lsp->ls_ci.dki_space = 0;
698*0Sstevel@tonic-gate 	lsp->ls_ci.dki_prio = 0;
699*0Sstevel@tonic-gate 	lsp->ls_ci.dki_vec = 0;
700*0Sstevel@tonic-gate 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
701*0Sstevel@tonic-gate 	lsp->ls_ci.dki_unit = 0;
702*0Sstevel@tonic-gate 	lsp->ls_ci.dki_slave = 0;
703*0Sstevel@tonic-gate 	lsp->ls_ci.dki_partition = 0;
704*0Sstevel@tonic-gate 	/*
705*0Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
706*0Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
707*0Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
708*0Sstevel@tonic-gate 	 * maxcontig is 0.
709*0Sstevel@tonic-gate 	 */
710*0Sstevel@tonic-gate 	lsp->ls_ci.dki_maxtransfer = 16;
711*0Sstevel@tonic-gate }
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate /*
714*0Sstevel@tonic-gate  * map a file to a minor number. Return the minor number.
715*0Sstevel@tonic-gate  */
716*0Sstevel@tonic-gate static int
717*0Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
718*0Sstevel@tonic-gate     int *rvalp, struct cred *credp)
719*0Sstevel@tonic-gate {
720*0Sstevel@tonic-gate 	minor_t	newminor;
721*0Sstevel@tonic-gate 	struct lofi_state *lsp;
722*0Sstevel@tonic-gate 	struct lofi_ioctl *klip;
723*0Sstevel@tonic-gate 	int	error;
724*0Sstevel@tonic-gate 	char	namebuf[50];
725*0Sstevel@tonic-gate 	struct vnode *vp;
726*0Sstevel@tonic-gate 	int64_t	Nblocks_prop_val;
727*0Sstevel@tonic-gate 	int64_t	Size_prop_val;
728*0Sstevel@tonic-gate 	vattr_t	vattr;
729*0Sstevel@tonic-gate 	int	flag;
730*0Sstevel@tonic-gate 	enum vtype v_type;
731*0Sstevel@tonic-gate 	dev_t	newdev;
732*0Sstevel@tonic-gate 	int zalloced = 0;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	klip = copy_in_lofi_ioctl(ulip);
735*0Sstevel@tonic-gate 	if (klip == NULL)
736*0Sstevel@tonic-gate 		return (EFAULT);
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	if (!valid_filename(klip->li_filename)) {
741*0Sstevel@tonic-gate 		error = EINVAL;
742*0Sstevel@tonic-gate 		goto out;
743*0Sstevel@tonic-gate 	}
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	if (file_to_minor(klip->li_filename) != 0) {
746*0Sstevel@tonic-gate 		error = EBUSY;
747*0Sstevel@tonic-gate 		goto out;
748*0Sstevel@tonic-gate 	}
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	if (pickminor) {
751*0Sstevel@tonic-gate 		/* Find a free one */
752*0Sstevel@tonic-gate 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
753*0Sstevel@tonic-gate 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
754*0Sstevel@tonic-gate 				break;
755*0Sstevel@tonic-gate 		if (newminor >= lofi_max_files) {
756*0Sstevel@tonic-gate 			error = EAGAIN;
757*0Sstevel@tonic-gate 			goto out;
758*0Sstevel@tonic-gate 		}
759*0Sstevel@tonic-gate 	} else {
760*0Sstevel@tonic-gate 		newminor = klip->li_minor;
761*0Sstevel@tonic-gate 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
762*0Sstevel@tonic-gate 			error = EEXIST;
763*0Sstevel@tonic-gate 			goto out;
764*0Sstevel@tonic-gate 		}
765*0Sstevel@tonic-gate 	}
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	/* make sure it's valid */
768*0Sstevel@tonic-gate 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
769*0Sstevel@tonic-gate 	    NULLVPP, &vp);
770*0Sstevel@tonic-gate 	if (error) {
771*0Sstevel@tonic-gate 		goto out;
772*0Sstevel@tonic-gate 	}
773*0Sstevel@tonic-gate 	v_type = vp->v_type;
774*0Sstevel@tonic-gate 	VN_RELE(vp);
775*0Sstevel@tonic-gate 	if (!V_ISLOFIABLE(v_type)) {
776*0Sstevel@tonic-gate 		error = EINVAL;
777*0Sstevel@tonic-gate 		goto out;
778*0Sstevel@tonic-gate 	}
779*0Sstevel@tonic-gate 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
780*0Sstevel@tonic-gate 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
781*0Sstevel@tonic-gate 	if (error) {
782*0Sstevel@tonic-gate 		/* try read-only */
783*0Sstevel@tonic-gate 		flag &= ~FWRITE;
784*0Sstevel@tonic-gate 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
785*0Sstevel@tonic-gate 		    &vp, 0, 0);
786*0Sstevel@tonic-gate 		if (error) {
787*0Sstevel@tonic-gate 			goto out;
788*0Sstevel@tonic-gate 		}
789*0Sstevel@tonic-gate 	}
790*0Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
791*0Sstevel@tonic-gate 	error = VOP_GETATTR(vp, &vattr, 0, credp);
792*0Sstevel@tonic-gate 	if (error) {
793*0Sstevel@tonic-gate 		goto closeout;
794*0Sstevel@tonic-gate 	}
795*0Sstevel@tonic-gate 	/* the file needs to be a multiple of the block size */
796*0Sstevel@tonic-gate 	if ((vattr.va_size % DEV_BSIZE) != 0) {
797*0Sstevel@tonic-gate 		error = EINVAL;
798*0Sstevel@tonic-gate 		goto closeout;
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), newminor);
801*0Sstevel@tonic-gate 	Size_prop_val = vattr.va_size;
802*0Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
803*0Sstevel@tonic-gate 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
804*0Sstevel@tonic-gate 		error = EINVAL;
805*0Sstevel@tonic-gate 		goto closeout;
806*0Sstevel@tonic-gate 	}
807*0Sstevel@tonic-gate 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
808*0Sstevel@tonic-gate 	if ((ddi_prop_update_int64(newdev, lofi_dip,
809*0Sstevel@tonic-gate 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
810*0Sstevel@tonic-gate 		error = EINVAL;
811*0Sstevel@tonic-gate 		goto propout;
812*0Sstevel@tonic-gate 	}
813*0Sstevel@tonic-gate 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
814*0Sstevel@tonic-gate 	if (error == DDI_FAILURE) {
815*0Sstevel@tonic-gate 		error = ENOMEM;
816*0Sstevel@tonic-gate 		goto propout;
817*0Sstevel@tonic-gate 	}
818*0Sstevel@tonic-gate 	zalloced = 1;
819*0Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
820*0Sstevel@tonic-gate 	(void) ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
821*0Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
822*0Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
823*0Sstevel@tonic-gate 		error = ENXIO;
824*0Sstevel@tonic-gate 		goto propout;
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
827*0Sstevel@tonic-gate 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
828*0Sstevel@tonic-gate 	    DDI_PSEUDO, NULL);
829*0Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
830*0Sstevel@tonic-gate 		/* remove block node */
831*0Sstevel@tonic-gate 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
832*0Sstevel@tonic-gate 		ddi_remove_minor_node(lofi_dip, namebuf);
833*0Sstevel@tonic-gate 		error = ENXIO;
834*0Sstevel@tonic-gate 		goto propout;
835*0Sstevel@tonic-gate 	}
836*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, newminor);
837*0Sstevel@tonic-gate 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
838*0Sstevel@tonic-gate 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
839*0Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
840*0Sstevel@tonic-gate 	    LOFI_DRIVER_NAME, newminor);
841*0Sstevel@tonic-gate 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
842*0Sstevel@tonic-gate 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
843*0Sstevel@tonic-gate 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
844*0Sstevel@tonic-gate 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
845*0Sstevel@tonic-gate 	if (lsp->ls_kstat) {
846*0Sstevel@tonic-gate 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
847*0Sstevel@tonic-gate 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
848*0Sstevel@tonic-gate 		kstat_install(lsp->ls_kstat);
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 	/*
851*0Sstevel@tonic-gate 	 * save open mode so file can be closed properly and vnode counts
852*0Sstevel@tonic-gate 	 * updated correctly.
853*0Sstevel@tonic-gate 	 */
854*0Sstevel@tonic-gate 	lsp->ls_openflag = flag;
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	/*
857*0Sstevel@tonic-gate 	 * Try to handle stacked lofs vnodes.
858*0Sstevel@tonic-gate 	 */
859*0Sstevel@tonic-gate 	if (vp->v_type == VREG) {
860*0Sstevel@tonic-gate 		if (VOP_REALVP(vp, &lsp->ls_vp) != 0) {
861*0Sstevel@tonic-gate 			lsp->ls_vp = vp;
862*0Sstevel@tonic-gate 		} else {
863*0Sstevel@tonic-gate 			/*
864*0Sstevel@tonic-gate 			 * Even though vp was obtained via vn_open(), we
865*0Sstevel@tonic-gate 			 * can't call vn_close() on it, since lofs will
866*0Sstevel@tonic-gate 			 * pass the VOP_CLOSE() on down to the realvp
867*0Sstevel@tonic-gate 			 * (which we are about to use). Hence we merely
868*0Sstevel@tonic-gate 			 * drop the reference to the lofs vnode and hold
869*0Sstevel@tonic-gate 			 * the realvp so things behave as if we've
870*0Sstevel@tonic-gate 			 * opened the realvp without any interaction
871*0Sstevel@tonic-gate 			 * with lofs.
872*0Sstevel@tonic-gate 			 */
873*0Sstevel@tonic-gate 			VN_HOLD(lsp->ls_vp);
874*0Sstevel@tonic-gate 			VN_RELE(vp);
875*0Sstevel@tonic-gate 		}
876*0Sstevel@tonic-gate 	} else {
877*0Sstevel@tonic-gate 		lsp->ls_vp = vp;
878*0Sstevel@tonic-gate 	}
879*0Sstevel@tonic-gate 	lsp->ls_vp_size = vattr.va_size;
880*0Sstevel@tonic-gate 	(void) strcpy(lsp->ls_filename, klip->li_filename);
881*0Sstevel@tonic-gate 	if (rvalp)
882*0Sstevel@tonic-gate 		*rvalp = (int)newminor;
883*0Sstevel@tonic-gate 	klip->li_minor = newminor;
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	fake_disk_geometry(lsp);
886*0Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
887*0Sstevel@tonic-gate 	(void) copy_out_lofi_ioctl(klip, ulip);
888*0Sstevel@tonic-gate 	free_lofi_ioctl(klip);
889*0Sstevel@tonic-gate 	return (0);
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate propout:
892*0Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
893*0Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
894*0Sstevel@tonic-gate closeout:
895*0Sstevel@tonic-gate 	(void) VOP_CLOSE(vp, flag, 1, 0, credp);
896*0Sstevel@tonic-gate 	VN_RELE(vp);
897*0Sstevel@tonic-gate out:
898*0Sstevel@tonic-gate 	if (zalloced)
899*0Sstevel@tonic-gate 		ddi_soft_state_free(lofi_statep, newminor);
900*0Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
901*0Sstevel@tonic-gate 	free_lofi_ioctl(klip);
902*0Sstevel@tonic-gate 	return (error);
903*0Sstevel@tonic-gate }
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate /*
906*0Sstevel@tonic-gate  * unmap a file.
907*0Sstevel@tonic-gate  */
908*0Sstevel@tonic-gate static int
909*0Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
910*0Sstevel@tonic-gate     struct cred *credp)
911*0Sstevel@tonic-gate {
912*0Sstevel@tonic-gate 	struct lofi_state *lsp;
913*0Sstevel@tonic-gate 	struct lofi_ioctl *klip;
914*0Sstevel@tonic-gate 	minor_t	minor;
915*0Sstevel@tonic-gate 	char	namebuf[20];
916*0Sstevel@tonic-gate 	dev_t	newdev;
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	klip = copy_in_lofi_ioctl(ulip);
919*0Sstevel@tonic-gate 	if (klip == NULL)
920*0Sstevel@tonic-gate 		return (EFAULT);
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
923*0Sstevel@tonic-gate 	if (byfilename) {
924*0Sstevel@tonic-gate 		minor = file_to_minor(klip->li_filename);
925*0Sstevel@tonic-gate 	} else {
926*0Sstevel@tonic-gate 		minor = klip->li_minor;
927*0Sstevel@tonic-gate 	}
928*0Sstevel@tonic-gate 	if (minor == 0) {
929*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
930*0Sstevel@tonic-gate 		free_lofi_ioctl(klip);
931*0Sstevel@tonic-gate 		return (ENXIO);
932*0Sstevel@tonic-gate 	}
933*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
934*0Sstevel@tonic-gate 	if (lsp == NULL) {
935*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
936*0Sstevel@tonic-gate 		free_lofi_ioctl(klip);
937*0Sstevel@tonic-gate 		return (ENXIO);
938*0Sstevel@tonic-gate 	}
939*0Sstevel@tonic-gate 	if (is_opened(lsp)) {
940*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
941*0Sstevel@tonic-gate 		free_lofi_ioctl(klip);
942*0Sstevel@tonic-gate 		return (EBUSY);
943*0Sstevel@tonic-gate 	}
944*0Sstevel@tonic-gate 	/*
945*0Sstevel@tonic-gate 	 * Use saved open mode to properly update vnode counts
946*0Sstevel@tonic-gate 	 */
947*0Sstevel@tonic-gate 	(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, credp);
948*0Sstevel@tonic-gate 	VN_RELE(lsp->ls_vp);
949*0Sstevel@tonic-gate 	lsp->ls_vp = NULL;
950*0Sstevel@tonic-gate 	newdev = makedevice(getmajor(dev), minor);
951*0Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
952*0Sstevel@tonic-gate 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
955*0Sstevel@tonic-gate 	ddi_remove_minor_node(lofi_dip, namebuf);
956*0Sstevel@tonic-gate 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
957*0Sstevel@tonic-gate 	ddi_remove_minor_node(lofi_dip, namebuf);
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
960*0Sstevel@tonic-gate 	taskq_destroy(lsp->ls_taskq);
961*0Sstevel@tonic-gate 	if (lsp->ls_kstat) {
962*0Sstevel@tonic-gate 		kstat_delete(lsp->ls_kstat);
963*0Sstevel@tonic-gate 		mutex_destroy(&lsp->ls_kstat_lock);
964*0Sstevel@tonic-gate 	}
965*0Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, minor);
966*0Sstevel@tonic-gate 	klip->li_minor = minor;
967*0Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
968*0Sstevel@tonic-gate 	(void) copy_out_lofi_ioctl(klip, ulip);
969*0Sstevel@tonic-gate 	free_lofi_ioctl(klip);
970*0Sstevel@tonic-gate 	return (0);
971*0Sstevel@tonic-gate }
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate /*
974*0Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
975*0Sstevel@tonic-gate  * the name.
976*0Sstevel@tonic-gate  */
977*0Sstevel@tonic-gate /*ARGSUSED3*/
978*0Sstevel@tonic-gate static int
979*0Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
980*0Sstevel@tonic-gate     struct cred *credp)
981*0Sstevel@tonic-gate {
982*0Sstevel@tonic-gate 	struct lofi_state *lsp;
983*0Sstevel@tonic-gate 	struct lofi_ioctl *klip;
984*0Sstevel@tonic-gate 	int	error;
985*0Sstevel@tonic-gate 	minor_t	minor;
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate #ifdef lint
988*0Sstevel@tonic-gate 	dev = dev;
989*0Sstevel@tonic-gate #endif
990*0Sstevel@tonic-gate 	klip = copy_in_lofi_ioctl(ulip);
991*0Sstevel@tonic-gate 	if (klip == NULL)
992*0Sstevel@tonic-gate 		return (EFAULT);
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 	switch (which) {
995*0Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
996*0Sstevel@tonic-gate 		minor = klip->li_minor;
997*0Sstevel@tonic-gate 		if (minor == 0) {
998*0Sstevel@tonic-gate 			free_lofi_ioctl(klip);
999*0Sstevel@tonic-gate 			return (EINVAL);
1000*0Sstevel@tonic-gate 		}
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
1003*0Sstevel@tonic-gate 		lsp = ddi_get_soft_state(lofi_statep, minor);
1004*0Sstevel@tonic-gate 		if (lsp == NULL) {
1005*0Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
1006*0Sstevel@tonic-gate 			free_lofi_ioctl(klip);
1007*0Sstevel@tonic-gate 			return (ENXIO);
1008*0Sstevel@tonic-gate 		}
1009*0Sstevel@tonic-gate 		(void) strcpy(klip->li_filename, lsp->ls_filename);
1010*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
1011*0Sstevel@tonic-gate 		error = copy_out_lofi_ioctl(klip, ulip);
1012*0Sstevel@tonic-gate 		free_lofi_ioctl(klip);
1013*0Sstevel@tonic-gate 		return (error);
1014*0Sstevel@tonic-gate 	case LOFI_GET_MINOR:
1015*0Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
1016*0Sstevel@tonic-gate 		klip->li_minor = file_to_minor(klip->li_filename);
1017*0Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
1018*0Sstevel@tonic-gate 		if (klip->li_minor == 0) {
1019*0Sstevel@tonic-gate 			free_lofi_ioctl(klip);
1020*0Sstevel@tonic-gate 			return (ENOENT);
1021*0Sstevel@tonic-gate 		}
1022*0Sstevel@tonic-gate 		error = copy_out_lofi_ioctl(klip, ulip);
1023*0Sstevel@tonic-gate 		free_lofi_ioctl(klip);
1024*0Sstevel@tonic-gate 		return (error);
1025*0Sstevel@tonic-gate 	default:
1026*0Sstevel@tonic-gate 		free_lofi_ioctl(klip);
1027*0Sstevel@tonic-gate 		return (EINVAL);
1028*0Sstevel@tonic-gate 	}
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate }
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate static int
1033*0Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
1034*0Sstevel@tonic-gate     int *rvalp)
1035*0Sstevel@tonic-gate {
1036*0Sstevel@tonic-gate 	int	error;
1037*0Sstevel@tonic-gate 	enum dkio_state dkstate;
1038*0Sstevel@tonic-gate 	struct lofi_state *lsp;
1039*0Sstevel@tonic-gate 	minor_t	minor;
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate #ifdef lint
1042*0Sstevel@tonic-gate 	credp = credp;
1043*0Sstevel@tonic-gate #endif
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 	minor = getminor(dev);
1046*0Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
1047*0Sstevel@tonic-gate 	if (minor == 0) {
1048*0Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
1049*0Sstevel@tonic-gate 
1050*0Sstevel@tonic-gate 		/*
1051*0Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
1052*0Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
1053*0Sstevel@tonic-gate 		 * long as they can open it read-only.
1054*0Sstevel@tonic-gate 		 */
1055*0Sstevel@tonic-gate 		switch (cmd) {
1056*0Sstevel@tonic-gate 		case LOFI_MAP_FILE:
1057*0Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
1058*0Sstevel@tonic-gate 				return (EPERM);
1059*0Sstevel@tonic-gate 			return (lofi_map_file(dev, lip, 1, rvalp, credp));
1060*0Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
1061*0Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
1062*0Sstevel@tonic-gate 				return (EPERM);
1063*0Sstevel@tonic-gate 			return (lofi_map_file(dev, lip, 0, rvalp, credp));
1064*0Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
1065*0Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
1066*0Sstevel@tonic-gate 				return (EPERM);
1067*0Sstevel@tonic-gate 			return (lofi_unmap_file(dev, lip, 1, credp));
1068*0Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
1069*0Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
1070*0Sstevel@tonic-gate 				return (EPERM);
1071*0Sstevel@tonic-gate 			return (lofi_unmap_file(dev, lip, 0, credp));
1072*0Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
1073*0Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
1074*0Sstevel@tonic-gate 			    credp));
1075*0Sstevel@tonic-gate 		case LOFI_GET_MINOR:
1076*0Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
1077*0Sstevel@tonic-gate 			    credp));
1078*0Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
1079*0Sstevel@tonic-gate 			error = copyout(&lofi_max_files, &lip->li_minor,
1080*0Sstevel@tonic-gate 			    sizeof (lofi_max_files));
1081*0Sstevel@tonic-gate 			if (error)
1082*0Sstevel@tonic-gate 				return (EFAULT);
1083*0Sstevel@tonic-gate 			return (0);
1084*0Sstevel@tonic-gate 		default:
1085*0Sstevel@tonic-gate 			break;
1086*0Sstevel@tonic-gate 		}
1087*0Sstevel@tonic-gate 	}
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 	lsp = ddi_get_soft_state(lofi_statep, minor);
1090*0Sstevel@tonic-gate 	if (lsp == NULL)
1091*0Sstevel@tonic-gate 		return (ENXIO);
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
1094*0Sstevel@tonic-gate 	switch (cmd) {
1095*0Sstevel@tonic-gate 	case VOLIOCINFO:
1096*0Sstevel@tonic-gate 		/* pcfs does this to see if it needs to set PCFS_NOCHK */
1097*0Sstevel@tonic-gate 		/* 0 means it should set it */
1098*0Sstevel@tonic-gate 		return (0);
1099*0Sstevel@tonic-gate 	case DKIOCGVTOC:
1100*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
1101*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
1102*0Sstevel@tonic-gate 			struct vtoc32 vtoc32;
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
1105*0Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
1106*0Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
1107*0Sstevel@tonic-gate 				return (EFAULT);
1108*0Sstevel@tonic-gate 				break;
1109*0Sstevel@tonic-gate 			}
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
1112*0Sstevel@tonic-gate 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
1113*0Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
1114*0Sstevel@tonic-gate 				return (EFAULT);
1115*0Sstevel@tonic-gate 			break;
1116*0Sstevel@tonic-gate 		}
1117*0Sstevel@tonic-gate 		return (0);
1118*0Sstevel@tonic-gate 	case DKIOCINFO:
1119*0Sstevel@tonic-gate 		error = copyout(&lsp->ls_ci, (void *)arg,
1120*0Sstevel@tonic-gate 		    sizeof (struct dk_cinfo));
1121*0Sstevel@tonic-gate 		if (error)
1122*0Sstevel@tonic-gate 			return (EFAULT);
1123*0Sstevel@tonic-gate 		return (0);
1124*0Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
1125*0Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
1126*0Sstevel@tonic-gate 	case DKIOCGGEOM:
1127*0Sstevel@tonic-gate 		error = copyout(&lsp->ls_dkg, (void *)arg,
1128*0Sstevel@tonic-gate 		    sizeof (struct dk_geom));
1129*0Sstevel@tonic-gate 		if (error)
1130*0Sstevel@tonic-gate 			return (EFAULT);
1131*0Sstevel@tonic-gate 		return (0);
1132*0Sstevel@tonic-gate 	case DKIOCSTATE:
1133*0Sstevel@tonic-gate 		/* the file is always there */
1134*0Sstevel@tonic-gate 		dkstate = DKIO_INSERTED;
1135*0Sstevel@tonic-gate 		error = copyout(&dkstate, (void *)arg,
1136*0Sstevel@tonic-gate 		    sizeof (enum dkio_state));
1137*0Sstevel@tonic-gate 		if (error)
1138*0Sstevel@tonic-gate 			return (EFAULT);
1139*0Sstevel@tonic-gate 		return (0);
1140*0Sstevel@tonic-gate 	default:
1141*0Sstevel@tonic-gate 		return (ENOTTY);
1142*0Sstevel@tonic-gate 	}
1143*0Sstevel@tonic-gate }
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
1146*0Sstevel@tonic-gate 	lofi_open,		/* open */
1147*0Sstevel@tonic-gate 	lofi_close,		/* close */
1148*0Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
1149*0Sstevel@tonic-gate 	nodev,			/* print */
1150*0Sstevel@tonic-gate 	nodev,			/* dump */
1151*0Sstevel@tonic-gate 	lofi_read,		/* read */
1152*0Sstevel@tonic-gate 	lofi_write,		/* write */
1153*0Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
1154*0Sstevel@tonic-gate 	nodev,			/* devmap */
1155*0Sstevel@tonic-gate 	nodev,			/* mmap */
1156*0Sstevel@tonic-gate 	nodev,			/* segmap */
1157*0Sstevel@tonic-gate 	nochpoll,		/* poll */
1158*0Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1159*0Sstevel@tonic-gate 	0,			/* streamtab  */
1160*0Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
1161*0Sstevel@tonic-gate 	CB_REV,
1162*0Sstevel@tonic-gate 	lofi_aread,
1163*0Sstevel@tonic-gate 	lofi_awrite
1164*0Sstevel@tonic-gate };
1165*0Sstevel@tonic-gate 
1166*0Sstevel@tonic-gate static struct dev_ops lofi_ops = {
1167*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1168*0Sstevel@tonic-gate 	0,			/* refcnt  */
1169*0Sstevel@tonic-gate 	lofi_info,		/* info */
1170*0Sstevel@tonic-gate 	nulldev,		/* identify */
1171*0Sstevel@tonic-gate 	nulldev,		/* probe */
1172*0Sstevel@tonic-gate 	lofi_attach,		/* attach */
1173*0Sstevel@tonic-gate 	lofi_detach,		/* detach */
1174*0Sstevel@tonic-gate 	nodev,			/* reset */
1175*0Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
1176*0Sstevel@tonic-gate 	NULL			/* no bus operations */
1177*0Sstevel@tonic-gate };
1178*0Sstevel@tonic-gate 
1179*0Sstevel@tonic-gate static struct modldrv modldrv = {
1180*0Sstevel@tonic-gate 	&mod_driverops,
1181*0Sstevel@tonic-gate 	"loopback file driver (%I%)",
1182*0Sstevel@tonic-gate 	&lofi_ops,
1183*0Sstevel@tonic-gate };
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
1186*0Sstevel@tonic-gate 	MODREV_1,
1187*0Sstevel@tonic-gate 	&modldrv,
1188*0Sstevel@tonic-gate 	NULL
1189*0Sstevel@tonic-gate };
1190*0Sstevel@tonic-gate 
1191*0Sstevel@tonic-gate int
1192*0Sstevel@tonic-gate _init(void)
1193*0Sstevel@tonic-gate {
1194*0Sstevel@tonic-gate 	int error;
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate 	error = ddi_soft_state_init(&lofi_statep,
1197*0Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
1198*0Sstevel@tonic-gate 	if (error)
1199*0Sstevel@tonic-gate 		return (error);
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
1202*0Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1203*0Sstevel@tonic-gate 	if (error) {
1204*0Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
1205*0Sstevel@tonic-gate 		ddi_soft_state_fini(&lofi_statep);
1206*0Sstevel@tonic-gate 	}
1207*0Sstevel@tonic-gate 
1208*0Sstevel@tonic-gate 	return (error);
1209*0Sstevel@tonic-gate }
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate int
1212*0Sstevel@tonic-gate _fini(void)
1213*0Sstevel@tonic-gate {
1214*0Sstevel@tonic-gate 	int	error;
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 	if (lofi_busy())
1217*0Sstevel@tonic-gate 		return (EBUSY);
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1220*0Sstevel@tonic-gate 	if (error)
1221*0Sstevel@tonic-gate 		return (error);
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
1224*0Sstevel@tonic-gate 	ddi_soft_state_fini(&lofi_statep);
1225*0Sstevel@tonic-gate 
1226*0Sstevel@tonic-gate 	return (error);
1227*0Sstevel@tonic-gate }
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate int
1230*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
1231*0Sstevel@tonic-gate {
1232*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1233*0Sstevel@tonic-gate }
1234