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