xref: /titanic_52/usr/src/uts/common/io/lofi.c (revision c623edd31cf7c582de6a3f26cc42c651d6002f73)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * lofi (loopback file) driver - allows you to attach a file to a device,
28  * which can then be accessed through that device. The simple model is that
29  * you tell lofi to open a file, and then use the block device you get as
30  * you would any block device. lofi translates access to the block device
31  * into I/O on the underlying file. This is mostly useful for
32  * mounting images of filesystems.
33  *
34  * lofi is controlled through /dev/lofictl - this is the only device exported
35  * during attach, and is minor number 0. lofiadm communicates with lofi through
36  * ioctls on this device. When a file is attached to lofi, block and character
37  * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
38  * are identified by their minor number, and the minor number is also used
39  * as the name in /dev/lofi. If we ever decide to support virtual disks,
40  * we'll have to divide the minor number space to identify fdisk partitions
41  * and slices, and the name will then be the minor number shifted down a
42  * few bits. Minor devices are tracked with state structures handled with
43  * ddi_soft_state(9F) for simplicity.
44  *
45  * A file attached to lofi is opened when attached and not closed until
46  * explicitly detached from lofi. This seems more sensible than deferring
47  * the open until the /dev/lofi device is opened, for a number of reasons.
48  * One is that any failure is likely to be noticed by the person (or script)
49  * running lofiadm. Another is that it would be a security problem if the
50  * file was replaced by another one after being added but before being opened.
51  *
52  * The only hard part about lofi is the ioctls. In order to support things
53  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
54  * So it has to fake disk geometry and partition information. More may need
55  * to be faked if your favorite utility doesn't work and you think it should
56  * (fdformat doesn't work because it really wants to know the type of floppy
57  * controller to talk to, and that didn't seem easy to fake. Or possibly even
58  * necessary, since we have mkfs_pcfs now).
59  *
60  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
61  * support simulation of hotplug events, an optional force flag is provided.
62  * If a lofi device is open when a force detach is requested, then the
63  * underlying file is closed and any subsequent operations return EIO.  When the
64  * device is closed for the last time, it will be cleaned up at that time.  In
65  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
66  * detached but not removed.
67  *
68  * Known problems:
69  *
70  *	UFS logging. Mounting a UFS filesystem image "logging"
71  *	works for basic copy testing but wedges during a build of ON through
72  *	that image. Some deadlock in lufs holding the log mutex and then
73  *	getting stuck on a buf. So for now, don't do that.
74  *
75  *	Direct I/O. Since the filesystem data is being cached in the buffer
76  *	cache, _and_ again in the underlying filesystem, it's tempting to
77  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
78  *	I think to fix the cache-twice problem we might need filesystem support.
79  *
80  *	lofi on itself. The simple lock strategy (lofi_lock) precludes this
81  *	because you'll be in lofi_ioctl, holding the lock when you open the
82  *	file, which, if it's lofi, will grab lofi_lock. We prevent this for
83  *	now, though not using ddi_soft_state(9F) would make it possible to
84  *	do. Though it would still be silly.
85  *
86  * Interesting things to do:
87  *
88  *	Allow multiple files for each device. A poor-man's metadisk, basically.
89  *
90  *	Pass-through ioctls on block devices. You can (though it's not
91  *	documented), give lofi a block device as a file name. Then we shouldn't
92  *	need to fake a geometry, however, it may be relevant if you're replacing
93  *	metadisk, or using lofi to get crypto.
94  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
95  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
96  *	In fact this even makes sense if you have lofi "above" metadisk.
97  *
98  * Encryption:
99  *	Each lofi device can have its own symmetric key and cipher.
100  *	They are passed to us by lofiadm(1m) in the correct format for use
101  *	with the misc/kcf crypto_* routines.
102  *
103  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
104  *	on the "master" key held in the lsp and the block number of the buffer.
105  */
106 
107 #include <sys/types.h>
108 #include <netinet/in.h>
109 #include <sys/sysmacros.h>
110 #include <sys/uio.h>
111 #include <sys/kmem.h>
112 #include <sys/cred.h>
113 #include <sys/mman.h>
114 #include <sys/errno.h>
115 #include <sys/aio_req.h>
116 #include <sys/stat.h>
117 #include <sys/file.h>
118 #include <sys/modctl.h>
119 #include <sys/conf.h>
120 #include <sys/debug.h>
121 #include <sys/vnode.h>
122 #include <sys/lofi.h>
123 #include <sys/fcntl.h>
124 #include <sys/pathname.h>
125 #include <sys/filio.h>
126 #include <sys/fdio.h>
127 #include <sys/open.h>
128 #include <sys/disp.h>
129 #include <vm/seg_map.h>
130 #include <sys/ddi.h>
131 #include <sys/sunddi.h>
132 #include <sys/zmod.h>
133 #include <sys/crypto/common.h>
134 #include <sys/crypto/api.h>
135 
136 /*
137  * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
138  * Crypto metadata, if it exists, is located at the end of the boot block
139  * (BBOFF + BBSIZE, which is SBOFF).  The super block and everything after
140  * is offset by the size of the crypto metadata which is handled by
141  * lsp->ls_crypto_offset.
142  */
143 #define	CRYOFF	((off_t)8192)
144 
145 #define	NBLOCKS_PROP_NAME	"Nblocks"
146 #define	SIZE_PROP_NAME		"Size"
147 
148 #define	SETUP_C_DATA(cd, buf, len) 		\
149 	(cd).cd_format = CRYPTO_DATA_RAW;	\
150 	(cd).cd_offset = 0;			\
151 	(cd).cd_miscdata = NULL;		\
152 	(cd).cd_length = (len);			\
153 	(cd).cd_raw.iov_base = (buf);		\
154 	(cd).cd_raw.iov_len = (len);
155 
156 #define	UIO_CHECK(uio)	\
157 	if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
158 	    ((uio)->uio_resid % DEV_BSIZE) != 0) { \
159 		return (EINVAL); \
160 	}
161 
162 static dev_info_t *lofi_dip = NULL;
163 static void *lofi_statep = NULL;
164 static kmutex_t lofi_lock;		/* state lock */
165 
166 /*
167  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
168  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
169  * high.  If we want to be assured that the underlying device is always busy,
170  * we must be sure that the number of bytes enqueued when the number of
171  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
172  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
173  * set maxalloc to be the maximum throughput (in bytes per second) of the
174  * underlying device divided by the minimum I/O size.  We assume a realistic
175  * maximum throughput of one hundred megabytes per second; we set maxalloc on
176  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
177  */
178 static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
179 static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
180 
181 uint32_t lofi_max_files = LOFI_MAX_FILES;
182 const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
183 
184 static int gzip_decompress(void *src, size_t srclen, void *dst,
185 	size_t *destlen, int level);
186 
187 lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
188 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
189 	{gzip_decompress,	NULL,	6,	"gzip-6"},
190 	{gzip_decompress,	NULL,	9,	"gzip-9"}
191 };
192 
193 static int
194 lofi_busy(void)
195 {
196 	minor_t	minor;
197 
198 	/*
199 	 * We need to make sure no mappings exist - mod_remove won't
200 	 * help because the device isn't open.
201 	 */
202 	mutex_enter(&lofi_lock);
203 	for (minor = 1; minor <= lofi_max_files; minor++) {
204 		if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
205 			mutex_exit(&lofi_lock);
206 			return (EBUSY);
207 		}
208 	}
209 	mutex_exit(&lofi_lock);
210 	return (0);
211 }
212 
213 static int
214 is_opened(struct lofi_state *lsp)
215 {
216 	ASSERT(mutex_owned(&lofi_lock));
217 	return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
218 }
219 
220 static int
221 mark_opened(struct lofi_state *lsp, int otyp)
222 {
223 	ASSERT(mutex_owned(&lofi_lock));
224 	switch (otyp) {
225 	case OTYP_CHR:
226 		lsp->ls_chr_open = 1;
227 		break;
228 	case OTYP_BLK:
229 		lsp->ls_blk_open = 1;
230 		break;
231 	case OTYP_LYR:
232 		lsp->ls_lyr_open_count++;
233 		break;
234 	default:
235 		return (-1);
236 	}
237 	return (0);
238 }
239 
240 static void
241 mark_closed(struct lofi_state *lsp, int otyp)
242 {
243 	ASSERT(mutex_owned(&lofi_lock));
244 	switch (otyp) {
245 	case OTYP_CHR:
246 		lsp->ls_chr_open = 0;
247 		break;
248 	case OTYP_BLK:
249 		lsp->ls_blk_open = 0;
250 		break;
251 	case OTYP_LYR:
252 		lsp->ls_lyr_open_count--;
253 		break;
254 	default:
255 		break;
256 	}
257 }
258 
259 static void
260 lofi_free_crypto(struct lofi_state *lsp)
261 {
262 	ASSERT(mutex_owned(&lofi_lock));
263 
264 	if (lsp->ls_crypto_enabled) {
265 		/*
266 		 * Clean up the crypto state so that it doesn't hang around
267 		 * in memory after we are done with it.
268 		 */
269 		bzero(lsp->ls_key.ck_data,
270 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
271 		kmem_free(lsp->ls_key.ck_data,
272 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
273 		lsp->ls_key.ck_data = NULL;
274 		lsp->ls_key.ck_length = 0;
275 
276 		if (lsp->ls_mech.cm_param != NULL) {
277 			kmem_free(lsp->ls_mech.cm_param,
278 			    lsp->ls_mech.cm_param_len);
279 			lsp->ls_mech.cm_param = NULL;
280 			lsp->ls_mech.cm_param_len = 0;
281 		}
282 
283 		if (lsp->ls_iv_mech.cm_param != NULL) {
284 			kmem_free(lsp->ls_iv_mech.cm_param,
285 			    lsp->ls_iv_mech.cm_param_len);
286 			lsp->ls_iv_mech.cm_param = NULL;
287 			lsp->ls_iv_mech.cm_param_len = 0;
288 		}
289 
290 		mutex_destroy(&lsp->ls_crypto_lock);
291 	}
292 }
293 
294 static void
295 lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
296     cred_t *credp)
297 {
298 	dev_t	newdev;
299 	char	namebuf[50];
300 
301 	ASSERT(mutex_owned(&lofi_lock));
302 
303 	lofi_free_crypto(lsp);
304 
305 	if (lsp->ls_vp) {
306 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
307 		    1, 0, credp, NULL);
308 		VN_RELE(lsp->ls_vp);
309 		lsp->ls_vp = NULL;
310 	}
311 
312 	newdev = makedevice(getmajor(dev), minor);
313 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
314 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
315 
316 	(void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
317 	ddi_remove_minor_node(lofi_dip, namebuf);
318 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
319 	ddi_remove_minor_node(lofi_dip, namebuf);
320 
321 	kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
322 	taskq_destroy(lsp->ls_taskq);
323 	if (lsp->ls_kstat) {
324 		kstat_delete(lsp->ls_kstat);
325 		mutex_destroy(&lsp->ls_kstat_lock);
326 	}
327 
328 	if (lsp->ls_uncomp_seg_sz > 0) {
329 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
330 		lsp->ls_uncomp_seg_sz = 0;
331 	}
332 	ddi_soft_state_free(lofi_statep, minor);
333 }
334 
335 /*ARGSUSED*/
336 static int
337 lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
338 {
339 	minor_t	minor;
340 	struct lofi_state *lsp;
341 
342 	mutex_enter(&lofi_lock);
343 	minor = getminor(*devp);
344 	if (minor == 0) {
345 		/* master control device */
346 		/* must be opened exclusively */
347 		if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
348 			mutex_exit(&lofi_lock);
349 			return (EINVAL);
350 		}
351 		lsp = ddi_get_soft_state(lofi_statep, 0);
352 		if (lsp == NULL) {
353 			mutex_exit(&lofi_lock);
354 			return (ENXIO);
355 		}
356 		if (is_opened(lsp)) {
357 			mutex_exit(&lofi_lock);
358 			return (EBUSY);
359 		}
360 		(void) mark_opened(lsp, OTYP_CHR);
361 		mutex_exit(&lofi_lock);
362 		return (0);
363 	}
364 
365 	/* otherwise, the mapping should already exist */
366 	lsp = ddi_get_soft_state(lofi_statep, minor);
367 	if (lsp == NULL) {
368 		mutex_exit(&lofi_lock);
369 		return (EINVAL);
370 	}
371 
372 	if (lsp->ls_vp == NULL) {
373 		mutex_exit(&lofi_lock);
374 		return (ENXIO);
375 	}
376 
377 	if (mark_opened(lsp, otyp) == -1) {
378 		mutex_exit(&lofi_lock);
379 		return (EINVAL);
380 	}
381 
382 	mutex_exit(&lofi_lock);
383 	return (0);
384 }
385 
386 /*ARGSUSED*/
387 static int
388 lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
389 {
390 	minor_t	minor;
391 	struct lofi_state *lsp;
392 
393 	mutex_enter(&lofi_lock);
394 	minor = getminor(dev);
395 	lsp = ddi_get_soft_state(lofi_statep, minor);
396 	if (lsp == NULL) {
397 		mutex_exit(&lofi_lock);
398 		return (EINVAL);
399 	}
400 	mark_closed(lsp, otyp);
401 
402 	/*
403 	 * If we forcibly closed the underlying device (li_force), or
404 	 * asked for cleanup (li_cleanup), finish up if we're the last
405 	 * out of the door.
406 	 */
407 	if (minor != 0 && !is_opened(lsp) &&
408 	    (lsp->ls_cleanup || lsp->ls_vp == NULL))
409 		lofi_free_handle(dev, minor, lsp, credp);
410 
411 	mutex_exit(&lofi_lock);
412 	return (0);
413 }
414 
415 /*
416  * Sets the mechanism's initialization vector (IV) if one is needed.
417  * The IV is computed from the data block number.  lsp->ls_mech is
418  * altered so that:
419  *	lsp->ls_mech.cm_param_len is set to the IV len.
420  *	lsp->ls_mech.cm_param is set to the IV.
421  */
422 static int
423 lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
424 {
425 	int	ret;
426 	crypto_data_t cdata;
427 	char	*iv;
428 	size_t	iv_len;
429 	size_t	min;
430 	void	*data;
431 	size_t	datasz;
432 
433 	ASSERT(mutex_owned(&lsp->ls_crypto_lock));
434 
435 	if (lsp == NULL)
436 		return (CRYPTO_DEVICE_ERROR);
437 
438 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
439 	if (lsp->ls_iv_type == IVM_NONE) {
440 		return (CRYPTO_SUCCESS);
441 	}
442 
443 	/*
444 	 * if kmem already alloced from previous call and it's the same size
445 	 * we need now, just recycle it; allocate new kmem only if we have to
446 	 */
447 	if (lsp->ls_mech.cm_param == NULL ||
448 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
449 		iv_len = lsp->ls_iv_len;
450 		iv = kmem_zalloc(iv_len, KM_SLEEP);
451 	} else {
452 		iv_len = lsp->ls_mech.cm_param_len;
453 		iv = lsp->ls_mech.cm_param;
454 		bzero(iv, iv_len);
455 	}
456 
457 	switch (lsp->ls_iv_type) {
458 	case IVM_ENC_BLKNO:
459 		/* iv is not static, lblkno changes each time */
460 		data = &lblkno;
461 		datasz = sizeof (lblkno);
462 		break;
463 	default:
464 		data = 0;
465 		datasz = 0;
466 		break;
467 	}
468 
469 	/*
470 	 * write blkno into the iv buffer padded on the left in case
471 	 * blkno ever grows bigger than its current longlong_t size
472 	 * or a variation other than blkno is used for the iv data
473 	 */
474 	min = MIN(datasz, iv_len);
475 	bcopy(data, iv + (iv_len - min), min);
476 
477 	/* encrypt the data in-place to get the IV */
478 	SETUP_C_DATA(cdata, iv, iv_len);
479 
480 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
481 	    NULL, NULL, NULL);
482 	if (ret != CRYPTO_SUCCESS) {
483 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
484 		    lblkno, ret);
485 		if (lsp->ls_mech.cm_param != iv)
486 			kmem_free(iv, iv_len);
487 		return (ret);
488 	}
489 
490 	/* clean up the iv from the last computation */
491 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
492 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
493 	lsp->ls_mech.cm_param_len = iv_len;
494 	lsp->ls_mech.cm_param = iv;
495 
496 	return (CRYPTO_SUCCESS);
497 }
498 
499 /*
500  * Performs encryption and decryption of a chunk of data of size "len",
501  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
502  * DEV_BSIZE.
503  */
504 static int
505 lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
506     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
507 {
508 	crypto_data_t cdata;
509 	crypto_data_t wdata;
510 	int ret;
511 	longlong_t lblkno = bp->b_lblkno;
512 
513 	mutex_enter(&lsp->ls_crypto_lock);
514 
515 	/*
516 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
517 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
518 	 */
519 	SETUP_C_DATA(cdata, plaintext, len);
520 	cdata.cd_length = DEV_BSIZE;
521 	if (ciphertext != NULL) {		/* not in-place crypto */
522 		SETUP_C_DATA(wdata, ciphertext, len);
523 		wdata.cd_length = DEV_BSIZE;
524 	}
525 
526 	do {
527 		ret = lofi_blk_mech(lsp, lblkno);
528 		if (ret != CRYPTO_SUCCESS)
529 			continue;
530 
531 		if (op_encrypt) {
532 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
533 			    &lsp->ls_key, NULL,
534 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
535 		} else {
536 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
537 			    &lsp->ls_key, NULL,
538 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
539 		}
540 
541 		cdata.cd_offset += DEV_BSIZE;
542 		if (ciphertext != NULL)
543 			wdata.cd_offset += DEV_BSIZE;
544 		lblkno++;
545 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
546 
547 	mutex_exit(&lsp->ls_crypto_lock);
548 
549 	if (ret != CRYPTO_SUCCESS) {
550 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
551 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
552 		    lblkno, ret);
553 	}
554 
555 	return (ret);
556 }
557 
558 #define	RDWR_RAW	1
559 #define	RDWR_BCOPY	2
560 
561 static int
562 lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
563     struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
564 {
565 	ssize_t resid;
566 	int isread;
567 	int error;
568 
569 	/*
570 	 * Handles reads/writes for both plain and encrypted lofi
571 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
572 	 * when it gets here.
573 	 */
574 
575 	isread = bp->b_flags & B_READ;
576 	if (isread) {
577 		if (method == RDWR_BCOPY) {
578 			/* DO NOT update bp->b_resid for bcopy */
579 			bcopy(bcopy_locn, bufaddr, len);
580 			error = 0;
581 		} else {		/* RDWR_RAW */
582 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
583 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
584 			    &resid);
585 			bp->b_resid = resid;
586 		}
587 		if (lsp->ls_crypto_enabled && error == 0) {
588 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
589 			    B_FALSE) != CRYPTO_SUCCESS) {
590 				/*
591 				 * XXX: original code didn't set residual
592 				 * back to len because no error was expected
593 				 * from bcopy() if encryption is not enabled
594 				 */
595 				if (method != RDWR_BCOPY)
596 					bp->b_resid = len;
597 				error = EIO;
598 			}
599 		}
600 		return (error);
601 	} else {
602 		void *iobuf = bufaddr;
603 
604 		if (lsp->ls_crypto_enabled) {
605 			/* don't do in-place crypto to keep bufaddr intact */
606 			iobuf = kmem_alloc(len, KM_SLEEP);
607 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
608 			    B_TRUE) != CRYPTO_SUCCESS) {
609 				kmem_free(iobuf, len);
610 				if (method != RDWR_BCOPY)
611 					bp->b_resid = len;
612 				return (EIO);
613 			}
614 		}
615 		if (method == RDWR_BCOPY) {
616 			/* DO NOT update bp->b_resid for bcopy */
617 			bcopy(iobuf, bcopy_locn, len);
618 			error = 0;
619 		} else {		/* RDWR_RAW */
620 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
621 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
622 			    &resid);
623 			bp->b_resid = resid;
624 		}
625 		if (lsp->ls_crypto_enabled) {
626 			kmem_free(iobuf, len);
627 		}
628 		return (error);
629 	}
630 }
631 
632 static int
633 lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
634     struct lofi_state *lsp)
635 {
636 	int error;
637 	offset_t alignedoffset, mapoffset;
638 	size_t	xfersize;
639 	int	isread;
640 	int	smflags;
641 	caddr_t	mapaddr;
642 	size_t	len;
643 	enum seg_rw srw;
644 	int	save_error;
645 
646 	/*
647 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
648 	 * when it gets here.
649 	 */
650 	if (lsp->ls_crypto_enabled)
651 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
652 
653 	/*
654 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
655 	 * an 8K boundary, but the buf transfer address may not be
656 	 * aligned on more than a 512-byte boundary (we don't enforce
657 	 * that even though we could). This matters since the initial
658 	 * part of the transfer may not start at offset 0 within the
659 	 * segmap'd chunk. So we have to compensate for that with
660 	 * 'mapoffset'. Subsequent chunks always start off at the
661 	 * beginning, and the last is capped by b_resid
662 	 *
663 	 * Visually, where "|" represents page map boundaries:
664 	 *   alignedoffset (mapaddr begins at this segmap boundary)
665 	 *    |   offset (from beginning of file)
666 	 *    |    |	   len
667 	 *    v    v	    v
668 	 * ===|====X========|====...======|========X====|====
669 	 *	   /-------------...---------------/
670 	 *		^ bp->b_bcount/bp->b_resid at start
671 	 *    /----/--------/----...------/--------/
672 	 *	^	^	^   ^		^
673 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
674 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
675 	 *	|	1st xfersize (<= MAXBSIZE)
676 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
677 	 *
678 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
679 	 * MAXBSIZE boundary.  "len" is next page boundary of size
680 	 * MAXBSIZE after "alignedoffset".
681 	 */
682 	mapoffset = offset & MAXBOFFSET;
683 	alignedoffset = offset - mapoffset;
684 	bp->b_resid = bp->b_bcount;
685 	isread = bp->b_flags & B_READ;
686 	srw = isread ? S_READ : S_WRITE;
687 	do {
688 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
689 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
690 		len = roundup(mapoffset + xfersize, MAXBSIZE);
691 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
692 		    alignedoffset, MAXBSIZE, 1, srw);
693 		/*
694 		 * Now fault in the pages. This lets us check
695 		 * for errors before we reference mapaddr and
696 		 * try to resolve the fault in bcopy (which would
697 		 * panic instead). And this can easily happen,
698 		 * particularly if you've lofi'd a file over NFS
699 		 * and someone deletes the file on the server.
700 		 */
701 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
702 		    len, F_SOFTLOCK, srw);
703 		if (error) {
704 			(void) segmap_release(segkmap, mapaddr, 0);
705 			if (FC_CODE(error) == FC_OBJERR)
706 				error = FC_ERRNO(error);
707 			else
708 				error = EIO;
709 			break;
710 		}
711 		/* error may be non-zero for encrypted lofi */
712 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
713 		    RDWR_BCOPY, mapaddr + mapoffset);
714 		if (error == 0) {
715 			bp->b_resid -= xfersize;
716 			bufaddr += xfersize;
717 			offset += xfersize;
718 		}
719 		smflags = 0;
720 		if (isread) {
721 			smflags |= SM_FREE;
722 			/*
723 			 * If we're reading an entire page starting
724 			 * at a page boundary, there's a good chance
725 			 * we won't need it again. Put it on the
726 			 * head of the freelist.
727 			 */
728 			if (mapoffset == 0 && xfersize == MAXBSIZE)
729 				smflags |= SM_DONTNEED;
730 		} else {
731 			if (error == 0)		/* write back good pages */
732 				smflags |= SM_WRITE;
733 		}
734 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
735 		    len, F_SOFTUNLOCK, srw);
736 		save_error = segmap_release(segkmap, mapaddr, smflags);
737 		if (error == 0)
738 			error = save_error;
739 		/* only the first map may start partial */
740 		mapoffset = 0;
741 		alignedoffset += MAXBSIZE;
742 	} while ((error == 0) && (bp->b_resid > 0) &&
743 	    (offset < lsp->ls_vp_comp_size));
744 
745 	return (error);
746 }
747 
748 /*ARGSUSED*/
749 static int gzip_decompress(void *src, size_t srclen, void *dst,
750     size_t *dstlen, int level)
751 {
752 	ASSERT(*dstlen >= srclen);
753 
754 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
755 		return (-1);
756 	return (0);
757 }
758 
759 /*
760  * This is basically what strategy used to be before we found we
761  * needed task queues.
762  */
763 static void
764 lofi_strategy_task(void *arg)
765 {
766 	struct buf *bp = (struct buf *)arg;
767 	int error;
768 	struct lofi_state *lsp;
769 	offset_t offset;
770 	caddr_t	bufaddr;
771 	size_t	len;
772 	size_t	xfersize;
773 	boolean_t bufinited = B_FALSE;
774 
775 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
776 	if (lsp == NULL) {
777 		error = ENXIO;
778 		goto errout;
779 	}
780 	if (lsp->ls_kstat) {
781 		mutex_enter(lsp->ls_kstat->ks_lock);
782 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
783 		mutex_exit(lsp->ls_kstat->ks_lock);
784 	}
785 	bp_mapin(bp);
786 	bufaddr = bp->b_un.b_addr;
787 #ifdef _LP64
788 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
789 #else
790 	offset = bp->b_blkno * DEV_BSIZE;	/* offset within file */
791 #endif /* _LP64 */
792 	if (lsp->ls_crypto_enabled) {
793 		/* encrypted data really begins after crypto header */
794 		offset += lsp->ls_crypto_offset;
795 	}
796 	len = bp->b_bcount;
797 	bufinited = B_TRUE;
798 
799 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
800 		error = EIO;
801 		goto errout;
802 	}
803 
804 	/*
805 	 * We used to always use vn_rdwr here, but we cannot do that because
806 	 * we might decide to read or write from the the underlying
807 	 * file during this call, which would be a deadlock because
808 	 * we have the rw_lock. So instead we page, unless it's not
809 	 * mapable or it's a character device or it's an encrypted lofi.
810 	 */
811 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
812 	    lsp->ls_crypto_enabled) {
813 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
814 		    NULL);
815 	} else if (lsp->ls_uncomp_seg_sz == 0) {
816 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
817 	} else {
818 		unsigned char *compressed_seg = NULL, *cmpbuf;
819 		unsigned char *uncompressed_seg = NULL;
820 		lofi_compress_info_t *li;
821 		size_t oblkcount;
822 		unsigned long seglen;
823 		uint64_t sblkno, eblkno, cmpbytes;
824 		offset_t sblkoff, eblkoff;
825 		u_offset_t salign, ealign;
826 		u_offset_t sdiff;
827 		uint32_t comp_data_sz;
828 		uint64_t i;
829 
830 		/*
831 		 * From here on we're dealing primarily with compressed files
832 		 */
833 		ASSERT(!lsp->ls_crypto_enabled);
834 
835 		/*
836 		 * Compressed files can only be read from and
837 		 * not written to
838 		 */
839 		if (!(bp->b_flags & B_READ)) {
840 			bp->b_resid = bp->b_bcount;
841 			error = EROFS;
842 			goto done;
843 		}
844 
845 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
846 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
847 		/*
848 		 * Compute starting and ending compressed segment numbers
849 		 * We use only bitwise operations avoiding division and
850 		 * modulus because we enforce the compression segment size
851 		 * to a power of 2
852 		 */
853 		sblkno = offset >> lsp->ls_comp_seg_shift;
854 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
855 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
856 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
857 
858 		/*
859 		 * Align start offset to block boundary for segmap
860 		 */
861 		salign = lsp->ls_comp_seg_index[sblkno];
862 		sdiff = salign & (DEV_BSIZE - 1);
863 		salign -= sdiff;
864 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
865 			/*
866 			 * We're dealing with the last segment of
867 			 * the compressed file -- the size of this
868 			 * segment *may not* be the same as the
869 			 * segment size for the file
870 			 */
871 			eblkoff = (offset + bp->b_bcount) &
872 			    (lsp->ls_uncomp_last_seg_sz - 1);
873 			ealign = lsp->ls_vp_comp_size;
874 		} else {
875 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
876 		}
877 
878 		/*
879 		 * Preserve original request paramaters
880 		 */
881 		oblkcount = bp->b_bcount;
882 
883 		/*
884 		 * Assign the calculated parameters
885 		 */
886 		comp_data_sz = ealign - salign;
887 		bp->b_bcount = comp_data_sz;
888 
889 		/*
890 		 * Allocate fixed size memory blocks to hold compressed
891 		 * segments and one uncompressed segment since we
892 		 * uncompress segments one at a time
893 		 */
894 		compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
895 		uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
896 		/*
897 		 * Map in the calculated number of blocks
898 		 */
899 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
900 		    bp, lsp);
901 
902 		bp->b_bcount = oblkcount;
903 		bp->b_resid = oblkcount;
904 		if (error != 0)
905 			goto done;
906 
907 		/*
908 		 * We have the compressed blocks, now uncompress them
909 		 */
910 		cmpbuf = compressed_seg + sdiff;
911 		for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz;
912 		    i++) {
913 			/*
914 			 * Each of the segment index entries contains
915 			 * the starting block number for that segment.
916 			 * The number of compressed bytes in a segment
917 			 * is thus the difference between the starting
918 			 * block number of this segment and the starting
919 			 * block number of the next segment.
920 			 */
921 			if ((i == eblkno) &&
922 			    (i == lsp->ls_comp_index_sz - 1)) {
923 				cmpbytes = lsp->ls_vp_comp_size -
924 				    lsp->ls_comp_seg_index[i];
925 			} else {
926 				cmpbytes = lsp->ls_comp_seg_index[i + 1] -
927 				    lsp->ls_comp_seg_index[i];
928 			}
929 
930 			/*
931 			 * The first byte in a compressed segment is a flag
932 			 * that indicates whether this segment is compressed
933 			 * at all
934 			 */
935 			if (*cmpbuf == UNCOMPRESSED) {
936 				bcopy((cmpbuf + SEGHDR), uncompressed_seg,
937 				    (cmpbytes - SEGHDR));
938 			} else {
939 				seglen = lsp->ls_uncomp_seg_sz;
940 
941 				if (li->l_decompress((cmpbuf + SEGHDR),
942 				    (cmpbytes - SEGHDR), uncompressed_seg,
943 				    &seglen, li->l_level) != 0) {
944 					error = EIO;
945 					goto done;
946 				}
947 			}
948 
949 			/*
950 			 * Determine how much uncompressed data we
951 			 * have to copy and copy it
952 			 */
953 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
954 			if (i == eblkno) {
955 				if (i == (lsp->ls_comp_index_sz - 1))
956 					xfersize -= (lsp->ls_uncomp_last_seg_sz
957 					    - eblkoff);
958 				else
959 					xfersize -=
960 					    (lsp->ls_uncomp_seg_sz - eblkoff);
961 			}
962 
963 			bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
964 
965 			cmpbuf += cmpbytes;
966 			bufaddr += xfersize;
967 			bp->b_resid -= xfersize;
968 			sblkoff = 0;
969 
970 			if (bp->b_resid == 0)
971 				break;
972 		}
973 done:
974 		if (compressed_seg != NULL)
975 			kmem_free(compressed_seg, comp_data_sz);
976 		if (uncompressed_seg != NULL)
977 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
978 	} /* end of handling compressed files */
979 
980 errout:
981 	if (bufinited && lsp->ls_kstat) {
982 		size_t n_done = bp->b_bcount - bp->b_resid;
983 		kstat_io_t *kioptr;
984 
985 		mutex_enter(lsp->ls_kstat->ks_lock);
986 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
987 		if (bp->b_flags & B_READ) {
988 			kioptr->nread += n_done;
989 			kioptr->reads++;
990 		} else {
991 			kioptr->nwritten += n_done;
992 			kioptr->writes++;
993 		}
994 		kstat_runq_exit(kioptr);
995 		mutex_exit(lsp->ls_kstat->ks_lock);
996 	}
997 
998 	mutex_enter(&lsp->ls_vp_lock);
999 	if (--lsp->ls_vp_iocount == 0)
1000 		cv_broadcast(&lsp->ls_vp_cv);
1001 	mutex_exit(&lsp->ls_vp_lock);
1002 
1003 	bioerror(bp, error);
1004 	biodone(bp);
1005 }
1006 
1007 static int
1008 lofi_strategy(struct buf *bp)
1009 {
1010 	struct lofi_state *lsp;
1011 	offset_t	offset;
1012 
1013 	/*
1014 	 * We cannot just do I/O here, because the current thread
1015 	 * _might_ end up back in here because the underlying filesystem
1016 	 * wants a buffer, which eventually gets into bio_recycle and
1017 	 * might call into lofi to write out a delayed-write buffer.
1018 	 * This is bad if the filesystem above lofi is the same as below.
1019 	 *
1020 	 * We could come up with a complex strategy using threads to
1021 	 * do the I/O asynchronously, or we could use task queues. task
1022 	 * queues were incredibly easy so they win.
1023 	 */
1024 	lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
1025 	if (lsp == NULL) {
1026 		bioerror(bp, ENXIO);
1027 		biodone(bp);
1028 		return (0);
1029 	}
1030 
1031 	mutex_enter(&lsp->ls_vp_lock);
1032 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
1033 		bioerror(bp, EIO);
1034 		biodone(bp);
1035 		mutex_exit(&lsp->ls_vp_lock);
1036 		return (0);
1037 	}
1038 
1039 #ifdef _LP64
1040 	offset = bp->b_lblkno * DEV_BSIZE;	/* offset within file */
1041 #else
1042 	offset = bp->b_blkno * DEV_BSIZE;	/* offset within file */
1043 #endif /* _LP64 */
1044 	if (lsp->ls_crypto_enabled) {
1045 		/* encrypted data really begins after crypto header */
1046 		offset += lsp->ls_crypto_offset;
1047 	}
1048 	if (offset == lsp->ls_vp_size) {
1049 		/* EOF */
1050 		if ((bp->b_flags & B_READ) != 0) {
1051 			bp->b_resid = bp->b_bcount;
1052 			bioerror(bp, 0);
1053 		} else {
1054 			/* writes should fail */
1055 			bioerror(bp, ENXIO);
1056 		}
1057 		biodone(bp);
1058 		mutex_exit(&lsp->ls_vp_lock);
1059 		return (0);
1060 	}
1061 	if (offset > lsp->ls_vp_size) {
1062 		bioerror(bp, ENXIO);
1063 		biodone(bp);
1064 		mutex_exit(&lsp->ls_vp_lock);
1065 		return (0);
1066 	}
1067 	lsp->ls_vp_iocount++;
1068 	mutex_exit(&lsp->ls_vp_lock);
1069 
1070 	if (lsp->ls_kstat) {
1071 		mutex_enter(lsp->ls_kstat->ks_lock);
1072 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
1073 		mutex_exit(lsp->ls_kstat->ks_lock);
1074 	}
1075 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
1076 	return (0);
1077 }
1078 
1079 /*ARGSUSED2*/
1080 static int
1081 lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
1082 {
1083 	if (getminor(dev) == 0)
1084 		return (EINVAL);
1085 	UIO_CHECK(uio);
1086 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
1087 }
1088 
1089 /*ARGSUSED2*/
1090 static int
1091 lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
1092 {
1093 	if (getminor(dev) == 0)
1094 		return (EINVAL);
1095 	UIO_CHECK(uio);
1096 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
1097 }
1098 
1099 /*ARGSUSED2*/
1100 static int
1101 lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
1102 {
1103 	if (getminor(dev) == 0)
1104 		return (EINVAL);
1105 	UIO_CHECK(aio->aio_uio);
1106 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
1107 }
1108 
1109 /*ARGSUSED2*/
1110 static int
1111 lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
1112 {
1113 	if (getminor(dev) == 0)
1114 		return (EINVAL);
1115 	UIO_CHECK(aio->aio_uio);
1116 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
1117 }
1118 
1119 /*ARGSUSED*/
1120 static int
1121 lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1122 {
1123 	switch (infocmd) {
1124 	case DDI_INFO_DEVT2DEVINFO:
1125 		*result = lofi_dip;
1126 		return (DDI_SUCCESS);
1127 	case DDI_INFO_DEVT2INSTANCE:
1128 		*result = 0;
1129 		return (DDI_SUCCESS);
1130 	}
1131 	return (DDI_FAILURE);
1132 }
1133 
1134 static int
1135 lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1136 {
1137 	int	error;
1138 
1139 	if (cmd != DDI_ATTACH)
1140 		return (DDI_FAILURE);
1141 	error = ddi_soft_state_zalloc(lofi_statep, 0);
1142 	if (error == DDI_FAILURE) {
1143 		return (DDI_FAILURE);
1144 	}
1145 	error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
1146 	    DDI_PSEUDO, NULL);
1147 	if (error == DDI_FAILURE) {
1148 		ddi_soft_state_free(lofi_statep, 0);
1149 		return (DDI_FAILURE);
1150 	}
1151 	/* driver handles kernel-issued IOCTLs */
1152 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1153 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1154 		ddi_remove_minor_node(dip, NULL);
1155 		ddi_soft_state_free(lofi_statep, 0);
1156 		return (DDI_FAILURE);
1157 	}
1158 	lofi_dip = dip;
1159 	ddi_report_dev(dip);
1160 	return (DDI_SUCCESS);
1161 }
1162 
1163 static int
1164 lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1165 {
1166 	if (cmd != DDI_DETACH)
1167 		return (DDI_FAILURE);
1168 	if (lofi_busy())
1169 		return (DDI_FAILURE);
1170 	lofi_dip = NULL;
1171 	ddi_remove_minor_node(dip, NULL);
1172 	ddi_prop_remove_all(dip);
1173 	ddi_soft_state_free(lofi_statep, 0);
1174 	return (DDI_SUCCESS);
1175 }
1176 
1177 /*
1178  * With addition of encryption, be careful that encryption key is wiped before
1179  * kernel memory structures are freed, and also that key is not accidentally
1180  * passed out into userland structures.
1181  */
1182 static void
1183 free_lofi_ioctl(struct lofi_ioctl *klip)
1184 {
1185 	/* Make sure this encryption key doesn't stick around */
1186 	bzero(klip->li_key, sizeof (klip->li_key));
1187 	kmem_free(klip, sizeof (struct lofi_ioctl));
1188 }
1189 
1190 /*
1191  * These two just simplify the rest of the ioctls that need to copyin/out
1192  * the lofi_ioctl structure.
1193  */
1194 struct lofi_ioctl *
1195 copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
1196 {
1197 	struct lofi_ioctl *klip;
1198 	int	error;
1199 
1200 	klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
1201 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
1202 	if (error) {
1203 		free_lofi_ioctl(klip);
1204 		return (NULL);
1205 	}
1206 
1207 	/* make sure filename is always null-terminated */
1208 	klip->li_filename[MAXPATHLEN-1] = '\0';
1209 
1210 	/* validate minor number */
1211 	if (klip->li_minor > lofi_max_files) {
1212 		free_lofi_ioctl(klip);
1213 		cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)",
1214 		    lofi_max_files);
1215 		return (NULL);
1216 	}
1217 	return (klip);
1218 }
1219 
1220 int
1221 copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
1222 	int flag)
1223 {
1224 	int	error;
1225 
1226 	/*
1227 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
1228 	 * This ensures that an attacker can't trivially find the
1229 	 * key for a mapping just by issuing the ioctl.
1230 	 *
1231 	 * It can still be found by poking around in kmem with mdb(1),
1232 	 * but there is no point in making it easy when the info isn't
1233 	 * of any use in this direction anyway.
1234 	 *
1235 	 * Either way we don't actually have the raw key stored in
1236 	 * a form that we can get it anyway, since we just used it
1237 	 * to create a ctx template and didn't keep "the original".
1238 	 */
1239 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
1240 	if (error)
1241 		return (EFAULT);
1242 	return (0);
1243 }
1244 
1245 /*
1246  * Return the minor number 'filename' is mapped to, if it is.
1247  */
1248 static int
1249 file_to_minor(char *filename)
1250 {
1251 	minor_t	minor;
1252 	struct lofi_state *lsp;
1253 
1254 	ASSERT(mutex_owned(&lofi_lock));
1255 	for (minor = 1; minor <= lofi_max_files; minor++) {
1256 		lsp = ddi_get_soft_state(lofi_statep, minor);
1257 		if (lsp == NULL)
1258 			continue;
1259 		if (strcmp(lsp->ls_filename, filename) == 0)
1260 			return (minor);
1261 	}
1262 	return (0);
1263 }
1264 
1265 /*
1266  * lofiadm does some validation, but since Joe Random (or crashme) could
1267  * do our ioctls, we need to do some validation too.
1268  */
1269 static int
1270 valid_filename(const char *filename)
1271 {
1272 	static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
1273 	static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
1274 
1275 	/* must be absolute path */
1276 	if (filename[0] != '/')
1277 		return (0);
1278 	/* must not be lofi */
1279 	if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
1280 		return (0);
1281 	if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
1282 		return (0);
1283 	return (1);
1284 }
1285 
1286 /*
1287  * Fakes up a disk geometry, and one big partition, based on the size
1288  * of the file. This is needed because we allow newfs'ing the device,
1289  * and newfs will do several disk ioctls to figure out the geometry and
1290  * partition information. It uses that information to determine the parameters
1291  * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
1292  * have to support it.
1293  */
1294 static void
1295 fake_disk_geometry(struct lofi_state *lsp)
1296 {
1297 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
1298 
1299 	/* dk_geom - see dkio(7I) */
1300 	/*
1301 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
1302 	 * of sectors), but that breaks programs like fdisk which want to
1303 	 * partition a disk by cylinder. With one cylinder, you can't create
1304 	 * an fdisk partition and put pcfs on it for testing (hard to pick
1305 	 * a number between one and one).
1306 	 *
1307 	 * The cheezy floppy test is an attempt to not have too few cylinders
1308 	 * for a small file, or so many on a big file that you waste space
1309 	 * for backup superblocks or cylinder group structures.
1310 	 */
1311 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
1312 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
1313 	else
1314 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
1315 	/* in case file file is < 100k */
1316 	if (lsp->ls_dkg.dkg_ncyl == 0)
1317 		lsp->ls_dkg.dkg_ncyl = 1;
1318 	lsp->ls_dkg.dkg_acyl = 0;
1319 	lsp->ls_dkg.dkg_bcyl = 0;
1320 	lsp->ls_dkg.dkg_nhead = 1;
1321 	lsp->ls_dkg.dkg_obs1 = 0;
1322 	lsp->ls_dkg.dkg_intrlv = 0;
1323 	lsp->ls_dkg.dkg_obs2 = 0;
1324 	lsp->ls_dkg.dkg_obs3 = 0;
1325 	lsp->ls_dkg.dkg_apc = 0;
1326 	lsp->ls_dkg.dkg_rpm = 7200;
1327 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
1328 	lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
1329 	lsp->ls_dkg.dkg_write_reinstruct = 0;
1330 	lsp->ls_dkg.dkg_read_reinstruct = 0;
1331 
1332 	/* vtoc - see dkio(7I) */
1333 	bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
1334 	lsp->ls_vtoc.v_sanity = VTOC_SANE;
1335 	lsp->ls_vtoc.v_version = V_VERSION;
1336 	bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7);
1337 	lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
1338 	lsp->ls_vtoc.v_nparts = 1;
1339 	lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
1340 
1341 	/*
1342 	 * A compressed file is read-only, other files can
1343 	 * be read-write
1344 	 */
1345 	if (lsp->ls_uncomp_seg_sz > 0) {
1346 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
1347 	} else {
1348 		lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
1349 	}
1350 	lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
1351 	/*
1352 	 * The partition size cannot just be the number of sectors, because
1353 	 * that might not end on a cylinder boundary. And if that's the case,
1354 	 * newfs/mkfs will print a scary warning. So just figure the size
1355 	 * based on the number of cylinders and sectors/cylinder.
1356 	 */
1357 	lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
1358 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
1359 
1360 	/* dk_cinfo - see dkio(7I) */
1361 	bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
1362 	(void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
1363 	lsp->ls_ci.dki_ctype = DKC_MD;
1364 	lsp->ls_ci.dki_flags = 0;
1365 	lsp->ls_ci.dki_cnum = 0;
1366 	lsp->ls_ci.dki_addr = 0;
1367 	lsp->ls_ci.dki_space = 0;
1368 	lsp->ls_ci.dki_prio = 0;
1369 	lsp->ls_ci.dki_vec = 0;
1370 	(void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
1371 	lsp->ls_ci.dki_unit = 0;
1372 	lsp->ls_ci.dki_slave = 0;
1373 	lsp->ls_ci.dki_partition = 0;
1374 	/*
1375 	 * newfs uses this to set maxcontig. Must not be < 16, or it
1376 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
1377 	 * it by the block size. Then tunefs doesn't work because
1378 	 * maxcontig is 0.
1379 	 */
1380 	lsp->ls_ci.dki_maxtransfer = 16;
1381 }
1382 
1383 /*
1384  * map in a compressed file
1385  *
1386  * Read in the header and the index that follows.
1387  *
1388  * The header is as follows -
1389  *
1390  * Signature (name of the compression algorithm)
1391  * Compression segment size (a multiple of 512)
1392  * Number of index entries
1393  * Size of the last block
1394  * The array containing the index entries
1395  *
1396  * The header information is always stored in
1397  * network byte order on disk.
1398  */
1399 static int
1400 lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
1401 {
1402 	uint32_t index_sz, header_len, i;
1403 	ssize_t	resid;
1404 	enum uio_rw rw;
1405 	char *tbuf = buf;
1406 	int error;
1407 
1408 	/* The signature has already been read */
1409 	tbuf += sizeof (lsp->ls_comp_algorithm);
1410 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
1411 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
1412 
1413 	/*
1414 	 * The compressed segment size must be a power of 2
1415 	 */
1416 	if (lsp->ls_uncomp_seg_sz % 2)
1417 		return (EINVAL);
1418 
1419 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
1420 		;
1421 
1422 	lsp->ls_comp_seg_shift = i;
1423 
1424 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
1425 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
1426 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
1427 
1428 	tbuf += sizeof (lsp->ls_comp_index_sz);
1429 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
1430 	    sizeof (lsp->ls_uncomp_last_seg_sz));
1431 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
1432 
1433 	/*
1434 	 * Compute the total size of the uncompressed data
1435 	 * for use in fake_disk_geometry and other calculations.
1436 	 * Disk geometry has to be faked with respect to the
1437 	 * actual uncompressed data size rather than the
1438 	 * compressed file size.
1439 	 */
1440 	lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
1441 	    + lsp->ls_uncomp_last_seg_sz;
1442 
1443 	/*
1444 	 * Index size is rounded up to a 512 byte boundary for ease
1445 	 * of segmapping
1446 	 */
1447 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
1448 	header_len = sizeof (lsp->ls_comp_algorithm) +
1449 	    sizeof (lsp->ls_uncomp_seg_sz) +
1450 	    sizeof (lsp->ls_comp_index_sz) +
1451 	    sizeof (lsp->ls_uncomp_last_seg_sz);
1452 	lsp->ls_comp_offbase = header_len + index_sz;
1453 
1454 	index_sz += header_len;
1455 	index_sz = roundup(index_sz, DEV_BSIZE);
1456 
1457 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
1458 	lsp->ls_comp_index_data_sz = index_sz;
1459 
1460 	/*
1461 	 * Read in the index -- this has a side-effect
1462 	 * of reading in the header as well
1463 	 */
1464 	rw = UIO_READ;
1465 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
1466 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
1467 
1468 	if (error != 0)
1469 		return (error);
1470 
1471 	/* Skip the header, this is where the index really begins */
1472 	lsp->ls_comp_seg_index =
1473 	    /*LINTED*/
1474 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
1475 
1476 	/*
1477 	 * Now recompute offsets in the index to account for
1478 	 * the header length
1479 	 */
1480 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
1481 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
1482 		    BE_64(lsp->ls_comp_seg_index[i]);
1483 	}
1484 
1485 	return (error);
1486 }
1487 
1488 /*
1489  * Check to see if the passed in signature is a valid
1490  * one.  If it is valid, return the index into
1491  * lofi_compress_table.
1492  *
1493  * Return -1 if it is invalid
1494  */
1495 static int lofi_compress_select(char *signature)
1496 {
1497 	int i;
1498 
1499 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
1500 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
1501 			return (i);
1502 	}
1503 
1504 	return (-1);
1505 }
1506 
1507 /*
1508  * map a file to a minor number. Return the minor number.
1509  */
1510 static int
1511 lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
1512     int *rvalp, struct cred *credp, int ioctl_flag)
1513 {
1514 	minor_t	newminor;
1515 	struct lofi_state *lsp;
1516 	struct lofi_ioctl *klip;
1517 	int	error;
1518 	struct vnode *vp;
1519 	int64_t	Nblocks_prop_val;
1520 	int64_t	Size_prop_val;
1521 	int	compress_index;
1522 	vattr_t	vattr;
1523 	int	flag;
1524 	enum vtype v_type;
1525 	int zalloced = 0;
1526 	dev_t	newdev;
1527 	char	namebuf[50];
1528 	char	buf[DEV_BSIZE];
1529 	char	crybuf[DEV_BSIZE];
1530 	ssize_t	resid;
1531 	boolean_t need_vn_close = B_FALSE;
1532 	boolean_t keycopied = B_FALSE;
1533 	boolean_t need_size_update = B_FALSE;
1534 
1535 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
1536 	if (klip == NULL)
1537 		return (EFAULT);
1538 
1539 	mutex_enter(&lofi_lock);
1540 
1541 	if (!valid_filename(klip->li_filename)) {
1542 		error = EINVAL;
1543 		goto out;
1544 	}
1545 
1546 	if (file_to_minor(klip->li_filename) != 0) {
1547 		error = EBUSY;
1548 		goto out;
1549 	}
1550 
1551 	if (pickminor) {
1552 		/* Find a free one */
1553 		for (newminor = 1; newminor <= lofi_max_files; newminor++)
1554 			if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
1555 				break;
1556 		if (newminor >= lofi_max_files) {
1557 			error = EAGAIN;
1558 			goto out;
1559 		}
1560 	} else {
1561 		newminor = klip->li_minor;
1562 		if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
1563 			error = EEXIST;
1564 			goto out;
1565 		}
1566 	}
1567 
1568 	/* make sure it's valid */
1569 	error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
1570 	    NULLVPP, &vp);
1571 	if (error) {
1572 		goto out;
1573 	}
1574 	v_type = vp->v_type;
1575 	VN_RELE(vp);
1576 	if (!V_ISLOFIABLE(v_type)) {
1577 		error = EINVAL;
1578 		goto out;
1579 	}
1580 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
1581 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
1582 	if (error) {
1583 		/* try read-only */
1584 		flag &= ~FWRITE;
1585 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
1586 		    &vp, 0, 0);
1587 		if (error) {
1588 			goto out;
1589 		}
1590 	}
1591 	need_vn_close = B_TRUE;
1592 
1593 	vattr.va_mask = AT_SIZE;
1594 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
1595 	if (error) {
1596 		goto out;
1597 	}
1598 	/* the file needs to be a multiple of the block size */
1599 	if ((vattr.va_size % DEV_BSIZE) != 0) {
1600 		error = EINVAL;
1601 		goto out;
1602 	}
1603 	newdev = makedevice(getmajor(dev), newminor);
1604 	Size_prop_val = vattr.va_size;
1605 	if ((ddi_prop_update_int64(newdev, lofi_dip,
1606 	    SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
1607 		error = EINVAL;
1608 		goto out;
1609 	}
1610 	Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
1611 	if ((ddi_prop_update_int64(newdev, lofi_dip,
1612 	    NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
1613 		error = EINVAL;
1614 		goto propout;
1615 	}
1616 	error = ddi_soft_state_zalloc(lofi_statep, newminor);
1617 	if (error == DDI_FAILURE) {
1618 		error = ENOMEM;
1619 		goto propout;
1620 	}
1621 	zalloced = 1;
1622 	(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
1623 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
1624 	    DDI_PSEUDO, NULL);
1625 	if (error != DDI_SUCCESS) {
1626 		error = ENXIO;
1627 		goto propout;
1628 	}
1629 	(void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
1630 	error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
1631 	    DDI_PSEUDO, NULL);
1632 	if (error != DDI_SUCCESS) {
1633 		/* remove block node */
1634 		(void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
1635 		ddi_remove_minor_node(lofi_dip, namebuf);
1636 		error = ENXIO;
1637 		goto propout;
1638 	}
1639 	lsp = ddi_get_soft_state(lofi_statep, newminor);
1640 	lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
1641 	lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
1642 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
1643 	    LOFI_DRIVER_NAME, newminor);
1644 	lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
1645 	    minclsyspri, 1, lofi_taskq_maxalloc, 0);
1646 	lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
1647 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0);
1648 	if (lsp->ls_kstat) {
1649 		mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
1650 		lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
1651 		kstat_install(lsp->ls_kstat);
1652 	}
1653 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
1654 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
1655 
1656 	/*
1657 	 * save open mode so file can be closed properly and vnode counts
1658 	 * updated correctly.
1659 	 */
1660 	lsp->ls_openflag = flag;
1661 
1662 	/*
1663 	 * Try to handle stacked lofs vnodes.
1664 	 */
1665 	if (vp->v_type == VREG) {
1666 		if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
1667 			lsp->ls_vp = vp;
1668 		} else {
1669 			/*
1670 			 * Even though vp was obtained via vn_open(), we
1671 			 * can't call vn_close() on it, since lofs will
1672 			 * pass the VOP_CLOSE() on down to the realvp
1673 			 * (which we are about to use). Hence we merely
1674 			 * drop the reference to the lofs vnode and hold
1675 			 * the realvp so things behave as if we've
1676 			 * opened the realvp without any interaction
1677 			 * with lofs.
1678 			 */
1679 			VN_HOLD(lsp->ls_vp);
1680 			VN_RELE(vp);
1681 		}
1682 	} else {
1683 		lsp->ls_vp = vp;
1684 	}
1685 	lsp->ls_vp_size = vattr.va_size;
1686 	(void) strcpy(lsp->ls_filename, klip->li_filename);
1687 	if (rvalp)
1688 		*rvalp = (int)newminor;
1689 	klip->li_minor = newminor;
1690 
1691 	/*
1692 	 * Initialize crypto details for encrypted lofi
1693 	 */
1694 	if (klip->li_crypto_enabled) {
1695 		int ret;
1696 
1697 		mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
1698 
1699 		lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
1700 		if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
1701 			cmn_err(CE_WARN, "invalid cipher %s requested for %s",
1702 			    klip->li_cipher, lsp->ls_filename);
1703 			error = EINVAL;
1704 			goto propout;
1705 		}
1706 
1707 		/* this is just initialization here */
1708 		lsp->ls_mech.cm_param = NULL;
1709 		lsp->ls_mech.cm_param_len = 0;
1710 
1711 		lsp->ls_iv_type = klip->li_iv_type;
1712 		lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
1713 		if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
1714 			cmn_err(CE_WARN, "invalid iv cipher %s requested"
1715 			    " for %s", klip->li_iv_cipher, lsp->ls_filename);
1716 			error = EINVAL;
1717 			goto propout;
1718 		}
1719 
1720 		/* iv mech must itself take a null iv */
1721 		lsp->ls_iv_mech.cm_param = NULL;
1722 		lsp->ls_iv_mech.cm_param_len = 0;
1723 		lsp->ls_iv_len = klip->li_iv_len;
1724 
1725 		/*
1726 		 * Create ctx using li_cipher & the raw li_key after checking
1727 		 * that it isn't a weak key.
1728 		 */
1729 		lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
1730 		lsp->ls_key.ck_length = klip->li_key_len;
1731 		lsp->ls_key.ck_data = kmem_alloc(
1732 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
1733 		bcopy(klip->li_key, lsp->ls_key.ck_data,
1734 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
1735 		keycopied = B_TRUE;
1736 
1737 		ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
1738 		if (ret != CRYPTO_SUCCESS) {
1739 			error = EINVAL;
1740 			cmn_err(CE_WARN, "weak key check failed for cipher "
1741 			    "%s on file %s (0x%x)", klip->li_cipher,
1742 			    lsp->ls_filename, ret);
1743 			goto propout;
1744 		}
1745 	}
1746 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
1747 
1748 	/*
1749 	 * Read the file signature to check if it is compressed or encrypted.
1750 	 * Crypto signature is in a different location; both areas should
1751 	 * read to keep compression and encryption mutually exclusive.
1752 	 */
1753 	if (lsp->ls_crypto_enabled) {
1754 		error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE,
1755 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
1756 		if (error != 0)
1757 			goto propout;
1758 	}
1759 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
1760 	    0, RLIM64_INFINITY, kcred, &resid);
1761 	if (error != 0)
1762 		goto propout;
1763 
1764 	/* initialize these variables for all lofi files */
1765 	lsp->ls_uncomp_seg_sz = 0;
1766 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
1767 	lsp->ls_comp_algorithm[0] = '\0';
1768 
1769 	/* encrypted lofi reads/writes shifted by crypto metadata size */
1770 	lsp->ls_crypto_offset = 0;
1771 
1772 	/* this is a compressed lofi */
1773 	if ((compress_index = lofi_compress_select(buf)) != -1) {
1774 
1775 		/* compression and encryption are mutually exclusive */
1776 		if (klip->li_crypto_enabled) {
1777 			error = ENOTSUP;
1778 			goto propout;
1779 		}
1780 
1781 		/* initialize compression info for compressed lofi */
1782 		lsp->ls_comp_algorithm_index = compress_index;
1783 		(void) strlcpy(lsp->ls_comp_algorithm,
1784 		    lofi_compress_table[compress_index].l_name,
1785 		    sizeof (lsp->ls_comp_algorithm));
1786 
1787 		error = lofi_map_compressed_file(lsp, buf);
1788 		if (error != 0)
1789 			goto propout;
1790 		need_size_update = B_TRUE;
1791 
1792 	/* this is an encrypted lofi */
1793 	} else if (strncmp(crybuf, lofi_crypto_magic,
1794 	    sizeof (lofi_crypto_magic)) == 0) {
1795 
1796 		char *marker = crybuf;
1797 
1798 		/*
1799 		 * This is the case where the header in the lofi image is
1800 		 * already initialized to indicate it is encrypted.
1801 		 * There is another case (see below) where encryption is
1802 		 * requested but the lofi image has never been used yet,
1803 		 * so the header needs to be written with encryption magic.
1804 		 */
1805 
1806 		/* indicate this must be an encrypted lofi due to magic */
1807 		klip->li_crypto_enabled = B_TRUE;
1808 
1809 		/*
1810 		 * The encryption header information is laid out this way:
1811 		 *	6 bytes:	hex "CFLOFI"
1812 		 *	2 bytes:	version = 0 ... for now
1813 		 *	96 bytes:	reserved1 (not implemented yet)
1814 		 *	4 bytes:	data_sector = 2 ... for now
1815 		 *	more...		not implemented yet
1816 		 */
1817 
1818 		/* copy the magic */
1819 		bcopy(marker, lsp->ls_crypto.magic,
1820 		    sizeof (lsp->ls_crypto.magic));
1821 		marker += sizeof (lsp->ls_crypto.magic);
1822 
1823 		/* read the encryption version number */
1824 		bcopy(marker, &(lsp->ls_crypto.version),
1825 		    sizeof (lsp->ls_crypto.version));
1826 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
1827 		marker += sizeof (lsp->ls_crypto.version);
1828 
1829 		/* read a chunk of reserved data */
1830 		bcopy(marker, lsp->ls_crypto.reserved1,
1831 		    sizeof (lsp->ls_crypto.reserved1));
1832 		marker += sizeof (lsp->ls_crypto.reserved1);
1833 
1834 		/* read block number where encrypted data begins */
1835 		bcopy(marker, &(lsp->ls_crypto.data_sector),
1836 		    sizeof (lsp->ls_crypto.data_sector));
1837 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
1838 		marker += sizeof (lsp->ls_crypto.data_sector);
1839 
1840 		/* and ignore the rest until it is implemented */
1841 
1842 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
1843 		need_size_update = B_TRUE;
1844 
1845 	/* neither compressed nor encrypted, BUT could be new encrypted lofi */
1846 	} else if (klip->li_crypto_enabled) {
1847 
1848 		/*
1849 		 * This is the case where encryption was requested but the
1850 		 * appears to be entirely blank where the encryption header
1851 		 * would have been in the lofi image.  If it is blank,
1852 		 * assume it is a brand new lofi image and initialize the
1853 		 * header area with encryption magic and current version
1854 		 * header data.  If it is not blank, that's an error.
1855 		 */
1856 		int	i;
1857 		char	*marker;
1858 		struct crypto_meta	chead;
1859 
1860 		for (i = 0; i < sizeof (struct crypto_meta); i++)
1861 			if (crybuf[i] != '\0')
1862 				break;
1863 		if (i != sizeof (struct crypto_meta)) {
1864 			error = EINVAL;
1865 			goto propout;
1866 		}
1867 
1868 		/* nothing there, initialize as encrypted lofi */
1869 		marker = crybuf;
1870 		bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
1871 		marker += sizeof (lofi_crypto_magic);
1872 		chead.version = htons(LOFI_CRYPTO_VERSION);
1873 		bcopy(&(chead.version), marker, sizeof (chead.version));
1874 		marker += sizeof (chead.version);
1875 		marker += sizeof (chead.reserved1);
1876 		chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
1877 		bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
1878 
1879 		/* write the header */
1880 		error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE,
1881 		    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
1882 		if (error != 0)
1883 			goto propout;
1884 
1885 		/* fix things up so it looks like we read this info */
1886 		bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
1887 		    sizeof (lofi_crypto_magic));
1888 		lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
1889 		lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
1890 
1891 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
1892 		need_size_update = B_TRUE;
1893 	}
1894 
1895 	/*
1896 	 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed;
1897 	 * for encrypted lofi, advertise that it is somewhat shorter
1898 	 * due to embedded crypto metadata section
1899 	 */
1900 	if (need_size_update) {
1901 		/* update DDI properties */
1902 		Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset;
1903 		if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
1904 		    Size_prop_val)) != DDI_PROP_SUCCESS) {
1905 			error = EINVAL;
1906 			goto propout;
1907 		}
1908 		Nblocks_prop_val =
1909 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE;
1910 		if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
1911 		    Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
1912 			error = EINVAL;
1913 			goto propout;
1914 		}
1915 	}
1916 
1917 	fake_disk_geometry(lsp);
1918 	mutex_exit(&lofi_lock);
1919 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
1920 	free_lofi_ioctl(klip);
1921 	return (0);
1922 
1923 propout:
1924 	if (keycopied) {
1925 		bzero(lsp->ls_key.ck_data,
1926 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
1927 		kmem_free(lsp->ls_key.ck_data,
1928 		    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
1929 		lsp->ls_key.ck_data = NULL;
1930 		lsp->ls_key.ck_length = 0;
1931 	}
1932 
1933 	if (zalloced)
1934 		ddi_soft_state_free(lofi_statep, newminor);
1935 
1936 	(void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
1937 	(void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
1938 
1939 out:
1940 	if (need_vn_close) {
1941 		(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
1942 		VN_RELE(vp);
1943 	}
1944 
1945 	mutex_exit(&lofi_lock);
1946 	free_lofi_ioctl(klip);
1947 	return (error);
1948 }
1949 
1950 /*
1951  * unmap a file.
1952  */
1953 static int
1954 lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
1955     struct cred *credp, int ioctl_flag)
1956 {
1957 	struct lofi_state *lsp;
1958 	struct lofi_ioctl *klip;
1959 	minor_t	minor;
1960 
1961 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
1962 	if (klip == NULL)
1963 		return (EFAULT);
1964 
1965 	mutex_enter(&lofi_lock);
1966 	if (byfilename) {
1967 		minor = file_to_minor(klip->li_filename);
1968 	} else {
1969 		minor = klip->li_minor;
1970 	}
1971 	if (minor == 0) {
1972 		mutex_exit(&lofi_lock);
1973 		free_lofi_ioctl(klip);
1974 		return (ENXIO);
1975 	}
1976 	lsp = ddi_get_soft_state(lofi_statep, minor);
1977 	if (lsp == NULL || lsp->ls_vp == NULL) {
1978 		mutex_exit(&lofi_lock);
1979 		free_lofi_ioctl(klip);
1980 		return (ENXIO);
1981 	}
1982 
1983 	/*
1984 	 * If it's still held open, we'll do one of three things:
1985 	 *
1986 	 * If no flag is set, just return EBUSY.
1987 	 *
1988 	 * If the 'cleanup' flag is set, unmap and remove the device when
1989 	 * the last user finishes.
1990 	 *
1991 	 * If the 'force' flag is set, then we forcibly close the underlying
1992 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
1993 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
1994 	 * device will be cleaned up appropriately.
1995 	 *
1996 	 * This is complicated by the fact that we may have outstanding
1997 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
1998 	 * I/O, we keep a count of the number of outstanding I/O requests
1999 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
2000 	 * should be dispatched (ls_vp_closereq).
2001 	 *
2002 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
2003 	 * and then close the underlying vnode.
2004 	 */
2005 	if (is_opened(lsp)) {
2006 		if (klip->li_force) {
2007 			/*
2008 			 * XXX: the section marked here should probably be
2009 			 * carefully incorporated into lofi_free_handle();
2010 			 * afterward just replace this section with:
2011 			 *	lofi_free_handle(dev, minor, lsp, credp);
2012 			 * and clean up lofi_unmap_file() a bit more
2013 			 */
2014 			lofi_free_crypto(lsp);
2015 
2016 			mutex_enter(&lsp->ls_vp_lock);
2017 			lsp->ls_vp_closereq = B_TRUE;
2018 			while (lsp->ls_vp_iocount > 0)
2019 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
2020 			(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
2021 			    credp, NULL);
2022 			VN_RELE(lsp->ls_vp);
2023 			lsp->ls_vp = NULL;
2024 			cv_broadcast(&lsp->ls_vp_cv);
2025 			mutex_exit(&lsp->ls_vp_lock);
2026 			/*
2027 			 * XXX: to here
2028 			 */
2029 
2030 			klip->li_minor = minor;
2031 			mutex_exit(&lofi_lock);
2032 			(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
2033 			free_lofi_ioctl(klip);
2034 			return (0);
2035 		} else if (klip->li_cleanup) {
2036 			lsp->ls_cleanup = 1;
2037 			mutex_exit(&lofi_lock);
2038 			free_lofi_ioctl(klip);
2039 			return (0);
2040 		}
2041 
2042 		mutex_exit(&lofi_lock);
2043 		free_lofi_ioctl(klip);
2044 		return (EBUSY);
2045 	}
2046 
2047 	lofi_free_handle(dev, minor, lsp, credp);
2048 
2049 	klip->li_minor = minor;
2050 	mutex_exit(&lofi_lock);
2051 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
2052 	free_lofi_ioctl(klip);
2053 	return (0);
2054 }
2055 
2056 /*
2057  * get the filename given the minor number, or the minor number given
2058  * the name.
2059  */
2060 /*ARGSUSED*/
2061 static int
2062 lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
2063     struct cred *credp, int ioctl_flag)
2064 {
2065 	struct lofi_state *lsp;
2066 	struct lofi_ioctl *klip;
2067 	int	error;
2068 	minor_t	minor;
2069 
2070 	klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
2071 	if (klip == NULL)
2072 		return (EFAULT);
2073 
2074 	switch (which) {
2075 	case LOFI_GET_FILENAME:
2076 		minor = klip->li_minor;
2077 		if (minor == 0) {
2078 			free_lofi_ioctl(klip);
2079 			return (EINVAL);
2080 		}
2081 
2082 		mutex_enter(&lofi_lock);
2083 		lsp = ddi_get_soft_state(lofi_statep, minor);
2084 		if (lsp == NULL) {
2085 			mutex_exit(&lofi_lock);
2086 			free_lofi_ioctl(klip);
2087 			return (ENXIO);
2088 		}
2089 		(void) strcpy(klip->li_filename, lsp->ls_filename);
2090 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
2091 		    sizeof (klip->li_algorithm));
2092 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
2093 		mutex_exit(&lofi_lock);
2094 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
2095 		free_lofi_ioctl(klip);
2096 		return (error);
2097 	case LOFI_GET_MINOR:
2098 		mutex_enter(&lofi_lock);
2099 		klip->li_minor = file_to_minor(klip->li_filename);
2100 		/* caller should not depend on klip->li_crypto_enabled here */
2101 		mutex_exit(&lofi_lock);
2102 		if (klip->li_minor == 0) {
2103 			free_lofi_ioctl(klip);
2104 			return (ENOENT);
2105 		}
2106 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
2107 		free_lofi_ioctl(klip);
2108 		return (error);
2109 	case LOFI_CHECK_COMPRESSED:
2110 		mutex_enter(&lofi_lock);
2111 		klip->li_minor = file_to_minor(klip->li_filename);
2112 		mutex_exit(&lofi_lock);
2113 		if (klip->li_minor == 0) {
2114 			free_lofi_ioctl(klip);
2115 			return (ENOENT);
2116 		}
2117 		mutex_enter(&lofi_lock);
2118 		lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
2119 		if (lsp == NULL) {
2120 			mutex_exit(&lofi_lock);
2121 			free_lofi_ioctl(klip);
2122 			return (ENXIO);
2123 		}
2124 		ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
2125 
2126 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
2127 		    sizeof (klip->li_algorithm));
2128 		mutex_exit(&lofi_lock);
2129 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
2130 		free_lofi_ioctl(klip);
2131 		return (error);
2132 	default:
2133 		free_lofi_ioctl(klip);
2134 		return (EINVAL);
2135 	}
2136 
2137 }
2138 
2139 static int
2140 lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
2141     int *rvalp)
2142 {
2143 	int	error;
2144 	enum dkio_state dkstate;
2145 	struct lofi_state *lsp;
2146 	minor_t	minor;
2147 
2148 	minor = getminor(dev);
2149 	/* lofi ioctls only apply to the master device */
2150 	if (minor == 0) {
2151 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
2152 
2153 		/*
2154 		 * the query command only need read-access - i.e., normal
2155 		 * users are allowed to do those on the ctl device as
2156 		 * long as they can open it read-only.
2157 		 */
2158 		switch (cmd) {
2159 		case LOFI_MAP_FILE:
2160 			if ((flag & FWRITE) == 0)
2161 				return (EPERM);
2162 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
2163 		case LOFI_MAP_FILE_MINOR:
2164 			if ((flag & FWRITE) == 0)
2165 				return (EPERM);
2166 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
2167 		case LOFI_UNMAP_FILE:
2168 			if ((flag & FWRITE) == 0)
2169 				return (EPERM);
2170 			return (lofi_unmap_file(dev, lip, 1, credp, flag));
2171 		case LOFI_UNMAP_FILE_MINOR:
2172 			if ((flag & FWRITE) == 0)
2173 				return (EPERM);
2174 			return (lofi_unmap_file(dev, lip, 0, credp, flag));
2175 		case LOFI_GET_FILENAME:
2176 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
2177 			    credp, flag));
2178 		case LOFI_GET_MINOR:
2179 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
2180 			    credp, flag));
2181 		case LOFI_GET_MAXMINOR:
2182 			error = ddi_copyout(&lofi_max_files, &lip->li_minor,
2183 			    sizeof (lofi_max_files), flag);
2184 			if (error)
2185 				return (EFAULT);
2186 			return (0);
2187 		case LOFI_CHECK_COMPRESSED:
2188 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
2189 			    credp, flag));
2190 		default:
2191 			break;
2192 		}
2193 	}
2194 
2195 	lsp = ddi_get_soft_state(lofi_statep, minor);
2196 	if (lsp == NULL)
2197 		return (ENXIO);
2198 
2199 	/*
2200 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
2201 	 * EIO as if the device was no longer present.
2202 	 */
2203 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
2204 		return (EIO);
2205 
2206 	/* these are for faking out utilities like newfs */
2207 	switch (cmd) {
2208 	case DKIOCGVTOC:
2209 		switch (ddi_model_convert_from(flag & FMODELS)) {
2210 		case DDI_MODEL_ILP32: {
2211 			struct vtoc32 vtoc32;
2212 
2213 			vtoctovtoc32(lsp->ls_vtoc, vtoc32);
2214 			if (ddi_copyout(&vtoc32, (void *)arg,
2215 			    sizeof (struct vtoc32), flag))
2216 				return (EFAULT);
2217 				break;
2218 			}
2219 
2220 		case DDI_MODEL_NONE:
2221 			if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
2222 			    sizeof (struct vtoc), flag))
2223 				return (EFAULT);
2224 			break;
2225 		}
2226 		return (0);
2227 	case DKIOCINFO:
2228 		error = ddi_copyout(&lsp->ls_ci, (void *)arg,
2229 		    sizeof (struct dk_cinfo), flag);
2230 		if (error)
2231 			return (EFAULT);
2232 		return (0);
2233 	case DKIOCG_VIRTGEOM:
2234 	case DKIOCG_PHYGEOM:
2235 	case DKIOCGGEOM:
2236 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
2237 		    sizeof (struct dk_geom), flag);
2238 		if (error)
2239 			return (EFAULT);
2240 		return (0);
2241 	case DKIOCSTATE:
2242 		/*
2243 		 * Normally, lofi devices are always in the INSERTED state.  If
2244 		 * a device is forcefully unmapped, then the device transitions
2245 		 * to the DKIO_DEV_GONE state.
2246 		 */
2247 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
2248 		    flag) != 0)
2249 			return (EFAULT);
2250 
2251 		mutex_enter(&lsp->ls_vp_lock);
2252 		while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
2253 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
2254 			/*
2255 			 * By virtue of having the device open, we know that
2256 			 * 'lsp' will remain valid when we return.
2257 			 */
2258 			if (!cv_wait_sig(&lsp->ls_vp_cv,
2259 			    &lsp->ls_vp_lock)) {
2260 				mutex_exit(&lsp->ls_vp_lock);
2261 				return (EINTR);
2262 			}
2263 		}
2264 
2265 		dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
2266 		mutex_exit(&lsp->ls_vp_lock);
2267 
2268 		if (ddi_copyout(&dkstate, (void *)arg,
2269 		    sizeof (dkstate), flag) != 0)
2270 			return (EFAULT);
2271 		return (0);
2272 	default:
2273 		return (ENOTTY);
2274 	}
2275 }
2276 
2277 static struct cb_ops lofi_cb_ops = {
2278 	lofi_open,		/* open */
2279 	lofi_close,		/* close */
2280 	lofi_strategy,		/* strategy */
2281 	nodev,			/* print */
2282 	nodev,			/* dump */
2283 	lofi_read,		/* read */
2284 	lofi_write,		/* write */
2285 	lofi_ioctl,		/* ioctl */
2286 	nodev,			/* devmap */
2287 	nodev,			/* mmap */
2288 	nodev,			/* segmap */
2289 	nochpoll,		/* poll */
2290 	ddi_prop_op,		/* prop_op */
2291 	0,			/* streamtab  */
2292 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
2293 	CB_REV,
2294 	lofi_aread,
2295 	lofi_awrite
2296 };
2297 
2298 static struct dev_ops lofi_ops = {
2299 	DEVO_REV,		/* devo_rev, */
2300 	0,			/* refcnt  */
2301 	lofi_info,		/* info */
2302 	nulldev,		/* identify */
2303 	nulldev,		/* probe */
2304 	lofi_attach,		/* attach */
2305 	lofi_detach,		/* detach */
2306 	nodev,			/* reset */
2307 	&lofi_cb_ops,		/* driver operations */
2308 	NULL,			/* no bus operations */
2309 	NULL,			/* power */
2310 	ddi_quiesce_not_needed,	/* quiesce */
2311 };
2312 
2313 static struct modldrv modldrv = {
2314 	&mod_driverops,
2315 	"loopback file driver",
2316 	&lofi_ops,
2317 };
2318 
2319 static struct modlinkage modlinkage = {
2320 	MODREV_1,
2321 	&modldrv,
2322 	NULL
2323 };
2324 
2325 int
2326 _init(void)
2327 {
2328 	int error;
2329 
2330 	error = ddi_soft_state_init(&lofi_statep,
2331 	    sizeof (struct lofi_state), 0);
2332 	if (error)
2333 		return (error);
2334 
2335 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
2336 	error = mod_install(&modlinkage);
2337 	if (error) {
2338 		mutex_destroy(&lofi_lock);
2339 		ddi_soft_state_fini(&lofi_statep);
2340 	}
2341 
2342 	return (error);
2343 }
2344 
2345 int
2346 _fini(void)
2347 {
2348 	int	error;
2349 
2350 	if (lofi_busy())
2351 		return (EBUSY);
2352 
2353 	error = mod_remove(&modlinkage);
2354 	if (error)
2355 		return (error);
2356 
2357 	mutex_destroy(&lofi_lock);
2358 	ddi_soft_state_fini(&lofi_statep);
2359 
2360 	return (error);
2361 }
2362 
2363 int
2364 _info(struct modinfo *modinfop)
2365 {
2366 	return (mod_info(&modlinkage, modinfop));
2367 }
2368