xref: /illumos-gate/usr/src/uts/common/io/lofi.c (revision 4b2d0200)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5bd07e074Sheppo  * Common Development and Distribution License (the "License").
6bd07e074Sheppo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2244a1e32bSbatschul  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23cd69fabeSAlexander Eremin  *
24cd69fabeSAlexander Eremin  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25a1b1a2a0SAndrey Sokolov  * Copyright (c) 2016 Andrey Sokolov
262e974cb2SPatrick Mooney  * Copyright 2019 Joyent, Inc.
2784ce06ceSAndy Fiddaman  * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
2839846823SToomas Soome  * Copyright 2021 Toomas Soome <tsoome@me.com>
293fe80ca4SDan Cross  * Copyright 2023 Oxide Computer Company
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * lofi (loopback file) driver - allows you to attach a file to a device,
347c478bd9Sstevel@tonic-gate  * which can then be accessed through that device. The simple model is that
357c478bd9Sstevel@tonic-gate  * you tell lofi to open a file, and then use the block device you get as
367c478bd9Sstevel@tonic-gate  * you would any block device. lofi translates access to the block device
377c478bd9Sstevel@tonic-gate  * into I/O on the underlying file. This is mostly useful for
387c478bd9Sstevel@tonic-gate  * mounting images of filesystems.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * lofi is controlled through /dev/lofictl - this is the only device exported
41406fc510SToomas Soome  * during attach, and is instance number 0. lofiadm communicates with lofi
42406fc510SToomas Soome  * through ioctls on this device. When a file is attached to lofi, block and
43406fc510SToomas Soome  * character devices are exported in /dev/lofi and /dev/rlofi. These devices
44406fc510SToomas Soome  * are identified by lofi instance number, and the instance number is also used
45406fc510SToomas Soome  * as the name in /dev/lofi.
46406fc510SToomas Soome  *
47406fc510SToomas Soome  * Virtual disks, or, labeled lofi, implements virtual disk support to
48406fc510SToomas Soome  * support partition table and related tools. Such mappings will cause
49406fc510SToomas Soome  * block and character devices to be exported in /dev/dsk and /dev/rdsk
50406fc510SToomas Soome  * directories.
51406fc510SToomas Soome  *
52406fc510SToomas Soome  * To support virtual disks, the instance number space is divided to two
53406fc510SToomas Soome  * parts, upper part for instance number and lower part for minor number
54406fc510SToomas Soome  * space to identify partitions and slices. The virtual disk support is
55406fc510SToomas Soome  * implemented by stacking cmlb module. For virtual disks, the partition
56406fc510SToomas Soome  * related ioctl calls are routed to cmlb module. Compression and encryption
57406fc510SToomas Soome  * is not supported for virtual disks.
58406fc510SToomas Soome  *
59406fc510SToomas Soome  * Mapped devices are tracked with state structures handled with
607c478bd9Sstevel@tonic-gate  * ddi_soft_state(9F) for simplicity.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * A file attached to lofi is opened when attached and not closed until
637c478bd9Sstevel@tonic-gate  * explicitly detached from lofi. This seems more sensible than deferring
647c478bd9Sstevel@tonic-gate  * the open until the /dev/lofi device is opened, for a number of reasons.
657c478bd9Sstevel@tonic-gate  * One is that any failure is likely to be noticed by the person (or script)
667c478bd9Sstevel@tonic-gate  * running lofiadm. Another is that it would be a security problem if the
677c478bd9Sstevel@tonic-gate  * file was replaced by another one after being added but before being opened.
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * The only hard part about lofi is the ioctls. In order to support things
707c478bd9Sstevel@tonic-gate  * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
717c478bd9Sstevel@tonic-gate  * So it has to fake disk geometry and partition information. More may need
727c478bd9Sstevel@tonic-gate  * to be faked if your favorite utility doesn't work and you think it should
737c478bd9Sstevel@tonic-gate  * (fdformat doesn't work because it really wants to know the type of floppy
747c478bd9Sstevel@tonic-gate  * controller to talk to, and that didn't seem easy to fake. Or possibly even
757c478bd9Sstevel@tonic-gate  * necessary, since we have mkfs_pcfs now).
767c478bd9Sstevel@tonic-gate  *
773d7072f8Seschrock  * Normally, a lofi device cannot be detached if it is open (i.e. busy).  To
783d7072f8Seschrock  * support simulation of hotplug events, an optional force flag is provided.
793d7072f8Seschrock  * If a lofi device is open when a force detach is requested, then the
803d7072f8Seschrock  * underlying file is closed and any subsequent operations return EIO.  When the
813d7072f8Seschrock  * device is closed for the last time, it will be cleaned up at that time.  In
823d7072f8Seschrock  * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
833d7072f8Seschrock  * detached but not removed.
843d7072f8Seschrock  *
85b4844717SToomas Soome  * If detach was requested and lofi device is not open, we will perform
86b4844717SToomas Soome  * unmap and remove the lofi instance.
87b4844717SToomas Soome  *
88b4844717SToomas Soome  * If the lofi device is open and the li_cleanup is set on ioctl request,
89b4844717SToomas Soome  * we set ls_cleanup flag to notify the cleanup is requested, and the
90b4844717SToomas Soome  * last lofi_close will perform the unmapping and this lofi instance will be
91b4844717SToomas Soome  * removed.
92b4844717SToomas Soome  *
93b4844717SToomas Soome  * If the lofi device is open and the li_force is set on ioctl request,
94b4844717SToomas Soome  * we set ls_cleanup flag to notify the cleanup is requested,
95b4844717SToomas Soome  * we also set ls_vp_closereq to notify IO tasks to return EIO on new
96b4844717SToomas Soome  * IO requests and wait in process IO count to become 0, indicating there
97b4844717SToomas Soome  * are no more IO requests. Since ls_cleanup is set, the last lofi_close
98b4844717SToomas Soome  * will perform unmap and this lofi instance will be removed.
99b4844717SToomas Soome  * See also lofi_unmap_file() for details.
100b4844717SToomas Soome  *
101b4844717SToomas Soome  * Once ls_cleanup is set for the instance, we do not allow lofi_open()
102b4844717SToomas Soome  * calls to succeed and can have last lofi_close() to remove the instance.
103b4844717SToomas Soome  *
1047c478bd9Sstevel@tonic-gate  * Known problems:
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  *	UFS logging. Mounting a UFS filesystem image "logging"
1077c478bd9Sstevel@tonic-gate  *	works for basic copy testing but wedges during a build of ON through
1087c478bd9Sstevel@tonic-gate  *	that image. Some deadlock in lufs holding the log mutex and then
1097c478bd9Sstevel@tonic-gate  *	getting stuck on a buf. So for now, don't do that.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *	Direct I/O. Since the filesystem data is being cached in the buffer
1127c478bd9Sstevel@tonic-gate  *	cache, _and_ again in the underlying filesystem, it's tempting to
1137c478bd9Sstevel@tonic-gate  *	enable direct I/O on the underlying file. Don't, because that deadlocks.
1147c478bd9Sstevel@tonic-gate  *	I think to fix the cache-twice problem we might need filesystem support.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * Interesting things to do:
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  *	Allow multiple files for each device. A poor-man's metadisk, basically.
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  *	Pass-through ioctls on block devices. You can (though it's not
1217c478bd9Sstevel@tonic-gate  *	documented), give lofi a block device as a file name. Then we shouldn't
1227d82f0f8SDina K Nimeh  *	need to fake a geometry, however, it may be relevant if you're replacing
1237d82f0f8SDina K Nimeh  *	metadisk, or using lofi to get crypto.
1247d82f0f8SDina K Nimeh  *	It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
1257d82f0f8SDina K Nimeh  *	and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
1267d82f0f8SDina K Nimeh  *	In fact this even makes sense if you have lofi "above" metadisk.
1277c478bd9Sstevel@tonic-gate  *
1287d82f0f8SDina K Nimeh  * Encryption:
1297d82f0f8SDina K Nimeh  *	Each lofi device can have its own symmetric key and cipher.
130bbf21555SRichard Lowe  *	They are passed to us by lofiadm(8) in the correct format for use
1317d82f0f8SDina K Nimeh  *	with the misc/kcf crypto_* routines.
1327d82f0f8SDina K Nimeh  *
1337d82f0f8SDina K Nimeh  *	Each block has its own IV, that is calculated in lofi_blk_mech(), based
1347d82f0f8SDina K Nimeh  *	on the "master" key held in the lsp and the block number of the buffer.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #include <sys/types.h>
13887117650Saalok #include <netinet/in.h>
1397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1407c478bd9Sstevel@tonic-gate #include <sys/uio.h>
1417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1427c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1437c478bd9Sstevel@tonic-gate #include <sys/mman.h>
1447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1457c478bd9Sstevel@tonic-gate #include <sys/aio_req.h>
1467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1477c478bd9Sstevel@tonic-gate #include <sys/file.h>
1487c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1497c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1517c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
1527c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
1538ae05c10SToomas Soome #include <sys/lofi_impl.h>	/* for cache structure */
1547c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
1557c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1567c478bd9Sstevel@tonic-gate #include <sys/filio.h>
1577c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
1587c478bd9Sstevel@tonic-gate #include <sys/open.h>
1597c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1607c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1617c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
16203f27e8bSAndy Fiddaman #include <sys/dkioc_free_util.h>
1637c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
16487117650Saalok #include <sys/zmod.h>
1650fbb751dSJohn Levon #include <sys/id_space.h>
1660fbb751dSJohn Levon #include <sys/mkdev.h>
1677d82f0f8SDina K Nimeh #include <sys/crypto/common.h>
1687d82f0f8SDina K Nimeh #include <sys/crypto/api.h>
1690fbb751dSJohn Levon #include <sys/rctl.h>
170406fc510SToomas Soome #include <sys/vtoc.h>
171406fc510SToomas Soome #include <sys/scsi/scsi.h>	/* for DTYPE_DIRECT */
172406fc510SToomas Soome #include <sys/scsi/impl/uscsi.h>
173406fc510SToomas Soome #include <sys/sysevent/dev.h>
174935ca2ecSJohn Levon #include <sys/efi_partition.h>
175b1efbcd6SAlok Aggarwal #include <LzmaDec.h>
1767d82f0f8SDina K Nimeh 
1777c478bd9Sstevel@tonic-gate #define	NBLOCKS_PROP_NAME	"Nblocks"
1787c478bd9Sstevel@tonic-gate #define	SIZE_PROP_NAME		"Size"
1790fbb751dSJohn Levon #define	ZONE_PROP_NAME		"zone"
1807c478bd9Sstevel@tonic-gate 
1817d82f0f8SDina K Nimeh #define	SETUP_C_DATA(cd, buf, len)		\
1827d82f0f8SDina K Nimeh 	(cd).cd_format = CRYPTO_DATA_RAW;	\
1837d82f0f8SDina K Nimeh 	(cd).cd_offset = 0;			\
1847d82f0f8SDina K Nimeh 	(cd).cd_miscdata = NULL;		\
1857d82f0f8SDina K Nimeh 	(cd).cd_length = (len);			\
1867d82f0f8SDina K Nimeh 	(cd).cd_raw.iov_base = (buf);		\
1877d82f0f8SDina K Nimeh 	(cd).cd_raw.iov_len = (len);
1887d82f0f8SDina K Nimeh 
1897d82f0f8SDina K Nimeh #define	UIO_CHECK(uio)	\
1907d82f0f8SDina K Nimeh 	if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
1917d82f0f8SDina K Nimeh 	    ((uio)->uio_resid % DEV_BSIZE) != 0) { \
1927d82f0f8SDina K Nimeh 		return (EINVAL); \
1937d82f0f8SDina K Nimeh 	}
1947d82f0f8SDina K Nimeh 
1959a1bf7f0SToomas Soome #define	LOFI_TIMEOUT	120
196406fc510SToomas Soome 
1979a1bf7f0SToomas Soome int lofi_timeout = LOFI_TIMEOUT;
198406fc510SToomas Soome static void *lofi_statep;
1997c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock;		/* state lock */
200406fc510SToomas Soome static id_space_t *lofi_id;		/* lofi ID values */
2010fbb751dSJohn Levon static list_t lofi_list;
2020fbb751dSJohn Levon static zone_key_t lofi_zone_key;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Because lofi_taskq_nthreads limits the actual swamping of the device, the
2067c478bd9Sstevel@tonic-gate  * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
2077c478bd9Sstevel@tonic-gate  * high.  If we want to be assured that the underlying device is always busy,
2087c478bd9Sstevel@tonic-gate  * we must be sure that the number of bytes enqueued when the number of
2097c478bd9Sstevel@tonic-gate  * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
2107c478bd9Sstevel@tonic-gate  * the duration of the sleep time in taskq_ent_alloc().  That is, lofi should
2117c478bd9Sstevel@tonic-gate  * set maxalloc to be the maximum throughput (in bytes per second) of the
2127c478bd9Sstevel@tonic-gate  * underlying device divided by the minimum I/O size.  We assume a realistic
2137c478bd9Sstevel@tonic-gate  * maximum throughput of one hundred megabytes per second; we set maxalloc on
2147c478bd9Sstevel@tonic-gate  * the lofi task queue to be 104857600 divided by DEV_BSIZE.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
2177c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4;	/* # of taskq threads per device */
2187c478bd9Sstevel@tonic-gate 
2197d82f0f8SDina K Nimeh const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
2207c478bd9Sstevel@tonic-gate 
2214058a205Sjrgn.keil@googlemail.com /*
2224058a205Sjrgn.keil@googlemail.com  * To avoid decompressing data in a compressed segment multiple times
2234058a205Sjrgn.keil@googlemail.com  * when accessing small parts of a segment's data, we cache and reuse
2244058a205Sjrgn.keil@googlemail.com  * the uncompressed segment's data.
2254058a205Sjrgn.keil@googlemail.com  *
2264058a205Sjrgn.keil@googlemail.com  * A single cached segment is sufficient to avoid lots of duplicate
2274058a205Sjrgn.keil@googlemail.com  * segment decompress operations. A small cache size also reduces the
2284058a205Sjrgn.keil@googlemail.com  * memory footprint.
2294058a205Sjrgn.keil@googlemail.com  *
2304058a205Sjrgn.keil@googlemail.com  * lofi_max_comp_cache is the maximum number of decompressed data segments
2314058a205Sjrgn.keil@googlemail.com  * cached for each compressed lofi image. It can be set to 0 to disable
2324058a205Sjrgn.keil@googlemail.com  * caching.
2334058a205Sjrgn.keil@googlemail.com  */
2344058a205Sjrgn.keil@googlemail.com 
2354058a205Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1;
2364058a205Sjrgn.keil@googlemail.com 
23787117650Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
23887117650Saalok 	size_t *destlen, int level);
23987117650Saalok 
240b1efbcd6SAlok Aggarwal static int lzma_decompress(void *src, size_t srclen, void *dst,
241b1efbcd6SAlok Aggarwal 	size_t *dstlen, int level);
242b1efbcd6SAlok Aggarwal 
24387117650Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
24487117650Saalok 	{gzip_decompress,	NULL,	6,	"gzip"}, /* default */
24587117650Saalok 	{gzip_decompress,	NULL,	6,	"gzip-6"},
246b1efbcd6SAlok Aggarwal 	{gzip_decompress,	NULL,	9,	"gzip-9"},
247b1efbcd6SAlok Aggarwal 	{lzma_decompress,	NULL,	0,	"lzma"}
24887117650Saalok };
24987117650Saalok 
250406fc510SToomas Soome static void lofi_strategy_task(void *);
251406fc510SToomas Soome static int lofi_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t,
252406fc510SToomas Soome     size_t, void *);
253406fc510SToomas Soome static int lofi_tg_getinfo(dev_info_t *, int, void *, void *);
254406fc510SToomas Soome 
255406fc510SToomas Soome struct cmlb_tg_ops lofi_tg_ops = {
256406fc510SToomas Soome 	TG_DK_OPS_VERSION_1,
257406fc510SToomas Soome 	lofi_tg_rdwr,
258406fc510SToomas Soome 	lofi_tg_getinfo
259406fc510SToomas Soome };
260406fc510SToomas Soome 
26103f27e8bSAndy Fiddaman typedef enum {
26203f27e8bSAndy Fiddaman 	RDWR_RAW,
26303f27e8bSAndy Fiddaman 	RDWR_BCOPY
26403f27e8bSAndy Fiddaman } lofi_rdrw_method_t;
26503f27e8bSAndy Fiddaman 
266b1efbcd6SAlok Aggarwal static void
SzAlloc(void * p __unused,size_t size)26703f27e8bSAndy Fiddaman *SzAlloc(void *p __unused, size_t size)
268b1efbcd6SAlok Aggarwal {
269b1efbcd6SAlok Aggarwal 	return (kmem_alloc(size, KM_SLEEP));
270b1efbcd6SAlok Aggarwal }
271b1efbcd6SAlok Aggarwal 
272b1efbcd6SAlok Aggarwal static void
SzFree(void * p __unused,void * address,size_t size)27303f27e8bSAndy Fiddaman SzFree(void *p __unused, void *address, size_t size)
274b1efbcd6SAlok Aggarwal {
275b1efbcd6SAlok Aggarwal 	kmem_free(address, size);
276b1efbcd6SAlok Aggarwal }
277b1efbcd6SAlok Aggarwal 
278b1efbcd6SAlok Aggarwal static ISzAlloc g_Alloc = { SzAlloc, SzFree };
279b1efbcd6SAlok Aggarwal 
2804058a205Sjrgn.keil@googlemail.com /*
2814058a205Sjrgn.keil@googlemail.com  * Free data referenced by the linked list of cached uncompressed
2824058a205Sjrgn.keil@googlemail.com  * segments.
2834058a205Sjrgn.keil@googlemail.com  */
2844058a205Sjrgn.keil@googlemail.com static void
lofi_free_comp_cache(struct lofi_state * lsp)2854058a205Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp)
2864058a205Sjrgn.keil@googlemail.com {
2874058a205Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
2884058a205Sjrgn.keil@googlemail.com 
2894058a205Sjrgn.keil@googlemail.com 	while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
2904058a205Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
2914058a205Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
2924058a205Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
2934058a205Sjrgn.keil@googlemail.com 	}
2944058a205Sjrgn.keil@googlemail.com 	ASSERT(lsp->ls_comp_cache_count == 0);
2954058a205Sjrgn.keil@googlemail.com }
2964058a205Sjrgn.keil@googlemail.com 
2977c478bd9Sstevel@tonic-gate static int
is_opened(struct lofi_state * lsp)2987c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2997c478bd9Sstevel@tonic-gate {
300406fc510SToomas Soome 	int i;
301406fc510SToomas Soome 	boolean_t last = B_TRUE;
302406fc510SToomas Soome 
3030fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
304406fc510SToomas Soome 	for (i = 0; i < LOFI_PART_MAX; i++) {
305406fc510SToomas Soome 		if (lsp->ls_open_lyr[i]) {
306406fc510SToomas Soome 			last = B_FALSE;
307406fc510SToomas Soome 			break;
308406fc510SToomas Soome 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
311406fc510SToomas Soome 	for (i = 0; last && (i < OTYP_LYR); i++) {
312406fc510SToomas Soome 		if (lsp->ls_open_reg[i]) {
313406fc510SToomas Soome 			last = B_FALSE;
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
317406fc510SToomas Soome 	return (!last);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3203d7072f8Seschrock static void
lofi_set_cleanup(struct lofi_state * lsp)321b4844717SToomas Soome lofi_set_cleanup(struct lofi_state *lsp)
322b4844717SToomas Soome {
323b4844717SToomas Soome 	ASSERT(MUTEX_HELD(&lofi_lock));
324b4844717SToomas Soome 
325b4844717SToomas Soome 	lsp->ls_cleanup = B_TRUE;
326b4844717SToomas Soome 
327b4844717SToomas Soome 	/* wake up any threads waiting on dkiocstate */
328b4844717SToomas Soome 	cv_broadcast(&lsp->ls_vp_cv);
329b4844717SToomas Soome }
330b4844717SToomas Soome 
331b4844717SToomas Soome static void
lofi_free_crypto(struct lofi_state * lsp)3327d82f0f8SDina K Nimeh lofi_free_crypto(struct lofi_state *lsp)
3337d82f0f8SDina K Nimeh {
3340fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
3357d82f0f8SDina K Nimeh 
3367d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
3377d82f0f8SDina K Nimeh 		/*
3387d82f0f8SDina K Nimeh 		 * Clean up the crypto state so that it doesn't hang around
3397d82f0f8SDina K Nimeh 		 * in memory after we are done with it.
3407d82f0f8SDina K Nimeh 		 */
3410fbb751dSJohn Levon 		if (lsp->ls_key.ck_data != NULL) {
3427d82f0f8SDina K Nimeh 			bzero(lsp->ls_key.ck_data,
3437d82f0f8SDina K Nimeh 			    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3447d82f0f8SDina K Nimeh 			kmem_free(lsp->ls_key.ck_data,
3457d82f0f8SDina K Nimeh 			    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3467d82f0f8SDina K Nimeh 			lsp->ls_key.ck_data = NULL;
3477d82f0f8SDina K Nimeh 			lsp->ls_key.ck_length = 0;
3480fbb751dSJohn Levon 		}
3497d82f0f8SDina K Nimeh 
3507d82f0f8SDina K Nimeh 		if (lsp->ls_mech.cm_param != NULL) {
3517d82f0f8SDina K Nimeh 			kmem_free(lsp->ls_mech.cm_param,
3527d82f0f8SDina K Nimeh 			    lsp->ls_mech.cm_param_len);
3537d82f0f8SDina K Nimeh 			lsp->ls_mech.cm_param = NULL;
3547d82f0f8SDina K Nimeh 			lsp->ls_mech.cm_param_len = 0;
3557d82f0f8SDina K Nimeh 		}
3567d82f0f8SDina K Nimeh 
3577d82f0f8SDina K Nimeh 		if (lsp->ls_iv_mech.cm_param != NULL) {
3587d82f0f8SDina K Nimeh 			kmem_free(lsp->ls_iv_mech.cm_param,
3597d82f0f8SDina K Nimeh 			    lsp->ls_iv_mech.cm_param_len);
3607d82f0f8SDina K Nimeh 			lsp->ls_iv_mech.cm_param = NULL;
3617d82f0f8SDina K Nimeh 			lsp->ls_iv_mech.cm_param_len = 0;
3627d82f0f8SDina K Nimeh 		}
3637d82f0f8SDina K Nimeh 
3647d82f0f8SDina K Nimeh 		mutex_destroy(&lsp->ls_crypto_lock);
3657d82f0f8SDina K Nimeh 	}
3667d82f0f8SDina K Nimeh }
3677d82f0f8SDina K Nimeh 
368406fc510SToomas Soome static int
lofi_tg_rdwr(dev_info_t * dip,uchar_t cmd,void * bufaddr,diskaddr_t start,size_t length,void * tg_cookie __unused)369406fc510SToomas Soome lofi_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start,
37003f27e8bSAndy Fiddaman     size_t length, void *tg_cookie __unused)
371406fc510SToomas Soome {
372406fc510SToomas Soome 	struct lofi_state *lsp;
373406fc510SToomas Soome 	buf_t	*bp;
374406fc510SToomas Soome 	int	instance;
375406fc510SToomas Soome 	int	rv = 0;
376406fc510SToomas Soome 
377406fc510SToomas Soome 	instance = ddi_get_instance(dip);
378406fc510SToomas Soome 	if (instance == 0)	/* control node does not have disk */
379406fc510SToomas Soome 		return (ENXIO);
380406fc510SToomas Soome 
381406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, instance);
382406fc510SToomas Soome 
383406fc510SToomas Soome 	if (lsp == NULL)
384406fc510SToomas Soome 		return (ENXIO);
385406fc510SToomas Soome 
386406fc510SToomas Soome 	if (cmd != TG_READ && cmd != TG_WRITE)
387406fc510SToomas Soome 		return (EINVAL);
388406fc510SToomas Soome 
389406fc510SToomas Soome 	/*
390406fc510SToomas Soome 	 * Make sure the mapping is set up by checking lsp->ls_vp_ready.
391406fc510SToomas Soome 	 */
392406fc510SToomas Soome 	mutex_enter(&lsp->ls_vp_lock);
393406fc510SToomas Soome 	while (lsp->ls_vp_ready == B_FALSE)
394406fc510SToomas Soome 		cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
395406fc510SToomas Soome 	mutex_exit(&lsp->ls_vp_lock);
396406fc510SToomas Soome 
397406fc510SToomas Soome 	if (P2PHASE(length, (1U << lsp->ls_lbshift)) != 0) {
398406fc510SToomas Soome 		/* We can only transfer whole blocks at a time! */
399406fc510SToomas Soome 		return (EINVAL);
400406fc510SToomas Soome 	}
401406fc510SToomas Soome 
402406fc510SToomas Soome 	bp = getrbuf(KM_SLEEP);
403406fc510SToomas Soome 
404406fc510SToomas Soome 	if (cmd == TG_READ) {
405406fc510SToomas Soome 		bp->b_flags = B_READ;
406406fc510SToomas Soome 	} else {
407406fc510SToomas Soome 		if (lsp->ls_readonly == B_TRUE) {
408406fc510SToomas Soome 			freerbuf(bp);
409406fc510SToomas Soome 			return (EROFS);
410406fc510SToomas Soome 		}
411406fc510SToomas Soome 		bp->b_flags = B_WRITE;
412406fc510SToomas Soome 	}
413406fc510SToomas Soome 
414406fc510SToomas Soome 	bp->b_un.b_addr = bufaddr;
415406fc510SToomas Soome 	bp->b_bcount = length;
416406fc510SToomas Soome 	bp->b_lblkno = start;
417406fc510SToomas Soome 	bp->b_private = NULL;
418406fc510SToomas Soome 	bp->b_edev = lsp->ls_dev;
419406fc510SToomas Soome 
420406fc510SToomas Soome 	if (lsp->ls_kstat) {
421406fc510SToomas Soome 		mutex_enter(lsp->ls_kstat->ks_lock);
422406fc510SToomas Soome 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
423406fc510SToomas Soome 		mutex_exit(lsp->ls_kstat->ks_lock);
424406fc510SToomas Soome 	}
425406fc510SToomas Soome 	(void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
426406fc510SToomas Soome 	(void) biowait(bp);
427406fc510SToomas Soome 
428406fc510SToomas Soome 	rv = geterror(bp);
429406fc510SToomas Soome 	freerbuf(bp);
430406fc510SToomas Soome 	return (rv);
431406fc510SToomas Soome }
432406fc510SToomas Soome 
433406fc510SToomas Soome /*
434406fc510SToomas Soome  * Get device geometry info for cmlb.
435406fc510SToomas Soome  *
436406fc510SToomas Soome  * We have mapped disk image as virtual block device and have to report
437406fc510SToomas Soome  * physical/virtual geometry to cmlb.
438406fc510SToomas Soome  *
439406fc510SToomas Soome  * So we have two principal cases:
440406fc510SToomas Soome  * 1. Uninitialised image without any existing labels,
441406fc510SToomas Soome  *    for this case we fabricate the data based on mapped image.
442406fc510SToomas Soome  * 2. Image with existing label information.
443406fc510SToomas Soome  *    Since we have no information how the image was created (it may be
444406fc510SToomas Soome  *    dump from some physical device), we need to rely on label information
445406fc510SToomas Soome  *    from image, or we get "corrupted label" errors.
446406fc510SToomas Soome  *    NOTE: label can be MBR, MBR+SMI, GPT
447406fc510SToomas Soome  */
448406fc510SToomas Soome static int
lofi_tg_getinfo(dev_info_t * dip,int cmd,void * arg,void * tg_cookie __unused)44903f27e8bSAndy Fiddaman lofi_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie __unused)
450406fc510SToomas Soome {
451406fc510SToomas Soome 	struct lofi_state *lsp;
452406fc510SToomas Soome 	int instance;
453406fc510SToomas Soome 	int ashift;
454406fc510SToomas Soome 
455406fc510SToomas Soome 	instance = ddi_get_instance(dip);
456406fc510SToomas Soome 	if (instance == 0)		/* control device has no storage */
457406fc510SToomas Soome 		return (ENXIO);
458406fc510SToomas Soome 
459406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, instance);
460406fc510SToomas Soome 
461406fc510SToomas Soome 	if (lsp == NULL)
462406fc510SToomas Soome 		return (ENXIO);
463406fc510SToomas Soome 
464406fc510SToomas Soome 	/*
465406fc510SToomas Soome 	 * Make sure the mapping is set up by checking lsp->ls_vp_ready.
466406fc510SToomas Soome 	 *
467406fc510SToomas Soome 	 * When mapping is created, new lofi instance is created and
468406fc510SToomas Soome 	 * lofi_attach() will call cmlb_attach() as part of the procedure
469406fc510SToomas Soome 	 * to set the mapping up. This chain of events will happen in
470406fc510SToomas Soome 	 * the same thread.
471406fc510SToomas Soome 	 * Since cmlb_attach() will call lofi_tg_getinfo to get
472406fc510SToomas Soome 	 * capacity, we return error on that call if cookie is set,
473406fc510SToomas Soome 	 * otherwise lofi_attach will be stuck as the mapping is not yet
474406fc510SToomas Soome 	 * finalized and lofi is not yet ready.
475406fc510SToomas Soome 	 * Note, such error is not fatal for cmlb, as the label setup
476406fc510SToomas Soome 	 * will be finalized when cmlb_validate() is called.
477406fc510SToomas Soome 	 */
478406fc510SToomas Soome 	mutex_enter(&lsp->ls_vp_lock);
479406fc510SToomas Soome 	if (tg_cookie != NULL && lsp->ls_vp_ready == B_FALSE) {
480406fc510SToomas Soome 		mutex_exit(&lsp->ls_vp_lock);
481406fc510SToomas Soome 		return (ENXIO);
482406fc510SToomas Soome 	}
483406fc510SToomas Soome 	while (lsp->ls_vp_ready == B_FALSE)
484406fc510SToomas Soome 		cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
485406fc510SToomas Soome 	mutex_exit(&lsp->ls_vp_lock);
486406fc510SToomas Soome 
487406fc510SToomas Soome 	ashift = lsp->ls_lbshift;
488406fc510SToomas Soome 
489406fc510SToomas Soome 	switch (cmd) {
490406fc510SToomas Soome 	case TG_GETPHYGEOM: {
491406fc510SToomas Soome 		cmlb_geom_t *geomp = arg;
492406fc510SToomas Soome 
493406fc510SToomas Soome 		geomp->g_capacity	=
494406fc510SToomas Soome 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
495406fc510SToomas Soome 		geomp->g_nsect		= lsp->ls_dkg.dkg_nsect;
496406fc510SToomas Soome 		geomp->g_nhead		= lsp->ls_dkg.dkg_nhead;
497406fc510SToomas Soome 		geomp->g_acyl		= lsp->ls_dkg.dkg_acyl;
498406fc510SToomas Soome 		geomp->g_ncyl		= lsp->ls_dkg.dkg_ncyl;
499406fc510SToomas Soome 		geomp->g_secsize	= (1U << ashift);
500406fc510SToomas Soome 		geomp->g_intrlv		= lsp->ls_dkg.dkg_intrlv;
501406fc510SToomas Soome 		geomp->g_rpm		= lsp->ls_dkg.dkg_rpm;
502406fc510SToomas Soome 		return (0);
503406fc510SToomas Soome 	}
504406fc510SToomas Soome 
505406fc510SToomas Soome 	case TG_GETCAPACITY:
506406fc510SToomas Soome 		*(diskaddr_t *)arg =
507406fc510SToomas Soome 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
508406fc510SToomas Soome 		return (0);
509406fc510SToomas Soome 
510406fc510SToomas Soome 	case TG_GETBLOCKSIZE:
511406fc510SToomas Soome 		*(uint32_t *)arg = (1U << ashift);
512406fc510SToomas Soome 		return (0);
513406fc510SToomas Soome 
514406fc510SToomas Soome 	case TG_GETATTR: {
515406fc510SToomas Soome 		tg_attribute_t *tgattr = arg;
516406fc510SToomas Soome 
517406fc510SToomas Soome 		tgattr->media_is_writable = !lsp->ls_readonly;
518406fc510SToomas Soome 		tgattr->media_is_solid_state = B_FALSE;
519acb450ddSYuri Pankov 		tgattr->media_is_rotational = B_FALSE;
520406fc510SToomas Soome 		return (0);
521406fc510SToomas Soome 	}
522406fc510SToomas Soome 
523406fc510SToomas Soome 	default:
524406fc510SToomas Soome 		return (EINVAL);
525406fc510SToomas Soome 	}
526406fc510SToomas Soome }
527406fc510SToomas Soome 
5287d82f0f8SDina K Nimeh static void
lofi_teardown_task(void * arg)52939846823SToomas Soome lofi_teardown_task(void *arg)
53039846823SToomas Soome {
53139846823SToomas Soome 	struct lofi_state *lsp = arg;
53239846823SToomas Soome 	int id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
53339846823SToomas Soome 
53439846823SToomas Soome 	mutex_enter(&lofi_lock);
53539846823SToomas Soome 	while (ndi_devi_offline(lsp->ls_dip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
53639846823SToomas Soome 		mutex_exit(&lofi_lock);
53739846823SToomas Soome 		/* do a sleeping wait for one second */;
53839846823SToomas Soome 		delay(drv_usectohz(MICROSEC));
53939846823SToomas Soome 		mutex_enter(&lofi_lock);
54039846823SToomas Soome 	}
54139846823SToomas Soome 	id_free(lofi_id, id);
54239846823SToomas Soome 	mutex_exit(&lofi_lock);
54339846823SToomas Soome }
54439846823SToomas Soome 
54539846823SToomas Soome static void
lofi_destroy(struct lofi_state * lsp,cred_t * credp)5460fbb751dSJohn Levon lofi_destroy(struct lofi_state *lsp, cred_t *credp)
5473d7072f8Seschrock {
548406fc510SToomas Soome 	int id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
549b9c7fb03SAlok Aggarwal 	int i;
5503d7072f8Seschrock 
5510fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
5520fbb751dSJohn Levon 
553b4844717SToomas Soome 	/*
554b4844717SToomas Soome 	 * Before we can start to release the other resources,
555b4844717SToomas Soome 	 * make sure we have all tasks completed and taskq removed.
556b4844717SToomas Soome 	 */
557b4844717SToomas Soome 	if (lsp->ls_taskq != NULL) {
558b4844717SToomas Soome 		taskq_destroy(lsp->ls_taskq);
559b4844717SToomas Soome 		lsp->ls_taskq = NULL;
560b4844717SToomas Soome 	}
561b4844717SToomas Soome 
5620fbb751dSJohn Levon 	list_remove(&lofi_list, lsp);
5637d82f0f8SDina K Nimeh 
5647d82f0f8SDina K Nimeh 	lofi_free_crypto(lsp);
5657d82f0f8SDina K Nimeh 
566b9c7fb03SAlok Aggarwal 	/*
567b9c7fb03SAlok Aggarwal 	 * Free pre-allocated compressed buffers
568b9c7fb03SAlok Aggarwal 	 */
569b9c7fb03SAlok Aggarwal 	if (lsp->ls_comp_bufs != NULL) {
570b9c7fb03SAlok Aggarwal 		for (i = 0; i < lofi_taskq_nthreads; i++) {
571b9c7fb03SAlok Aggarwal 			if (lsp->ls_comp_bufs[i].bufsize > 0)
572b9c7fb03SAlok Aggarwal 				kmem_free(lsp->ls_comp_bufs[i].buf,
573b9c7fb03SAlok Aggarwal 				    lsp->ls_comp_bufs[i].bufsize);
574b9c7fb03SAlok Aggarwal 		}
575b9c7fb03SAlok Aggarwal 		kmem_free(lsp->ls_comp_bufs,
576b9c7fb03SAlok Aggarwal 		    sizeof (struct compbuf) * lofi_taskq_nthreads);
577b9c7fb03SAlok Aggarwal 	}
578b9c7fb03SAlok Aggarwal 
579406fc510SToomas Soome 	if (lsp->ls_vp != NULL) {
5802e974cb2SPatrick Mooney 		(void) VOP_PUTPAGE(lsp->ls_vp, 0, 0, B_FREE, credp, NULL);
5810fbb751dSJohn Levon 		(void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
5820fbb751dSJohn Levon 		    1, 0, credp, NULL);
5830fbb751dSJohn Levon 		VN_RELE(lsp->ls_vp);
584406fc510SToomas Soome 	}
5850fbb751dSJohn Levon 	if (lsp->ls_stacked_vp != lsp->ls_vp)
5860fbb751dSJohn Levon 		VN_RELE(lsp->ls_stacked_vp);
587b4844717SToomas Soome 	lsp->ls_vp = lsp->ls_stacked_vp = NULL;
5880fbb751dSJohn Levon 
589b4844717SToomas Soome 	if (lsp->ls_kstat != NULL) {
5900fbb751dSJohn Levon 		kstat_delete(lsp->ls_kstat);
591b4844717SToomas Soome 		lsp->ls_kstat = NULL;
592b4844717SToomas Soome 	}
5930fbb751dSJohn Levon 
5940fbb751dSJohn Levon 	/*
5950fbb751dSJohn Levon 	 * Free cached decompressed segment data
5960fbb751dSJohn Levon 	 */
5970fbb751dSJohn Levon 	lofi_free_comp_cache(lsp);
5980fbb751dSJohn Levon 	list_destroy(&lsp->ls_comp_cache);
5990fbb751dSJohn Levon 
6000fbb751dSJohn Levon 	if (lsp->ls_uncomp_seg_sz > 0) {
6010fbb751dSJohn Levon 		kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
6020fbb751dSJohn Levon 		lsp->ls_uncomp_seg_sz = 0;
6030fbb751dSJohn Levon 	}
6040fbb751dSJohn Levon 
605a19609f8Sjv227347 	rctl_decr_lofi(lsp->ls_zone.zref_zone, 1);
606a19609f8Sjv227347 	zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
6070fbb751dSJohn Levon 
6080fbb751dSJohn Levon 	mutex_destroy(&lsp->ls_comp_cache_lock);
6090fbb751dSJohn Levon 	mutex_destroy(&lsp->ls_comp_bufs_lock);
6100fbb751dSJohn Levon 	mutex_destroy(&lsp->ls_kstat_lock);
6114058a205Sjrgn.keil@googlemail.com 	mutex_destroy(&lsp->ls_vp_lock);
612406fc510SToomas Soome 	cv_destroy(&lsp->ls_vp_cv);
613406fc510SToomas Soome 	lsp->ls_vp_ready = B_FALSE;
614b4844717SToomas Soome 	lsp->ls_vp_closereq = B_FALSE;
6154058a205Sjrgn.keil@googlemail.com 
616406fc510SToomas Soome 	ASSERT(ddi_get_soft_state(lofi_statep, id) == lsp);
61739846823SToomas Soome 	/*
61839846823SToomas Soome 	 * Instance state is allocated in lofi_attach() and freed in
61939846823SToomas Soome 	 * lofi_detach(). New instance is created when we create new mapping.
62039846823SToomas Soome 	 * Instance removal is performed by unmap ioctl on lofi control
62139846823SToomas Soome 	 * instance (0).
62239846823SToomas Soome 	 *
62339846823SToomas Soome 	 * If the unmap is performed with instance which is still in use,
62439846823SToomas Soome 	 * we either cancel unmap with error or we can perform delayed unmap
62539846823SToomas Soome 	 * by blocking all IO, waiting the consumers to close access to this
62639846823SToomas Soome 	 * instance and once there are no more consumers, complete the unmap.
62739846823SToomas Soome 	 *
62839846823SToomas Soome 	 * Delayed unmap will trigger instance removal on last lofi_close(),
62939846823SToomas Soome 	 * but we can not remove device instance while the instance is still
63039846823SToomas Soome 	 * in use due to lofi_close() is running.
63139846823SToomas Soome 	 * Spawn task to complete device instance offlining in separate thread.
63239846823SToomas Soome 	 */
63339846823SToomas Soome 	(void) taskq_dispatch(system_taskq, lofi_teardown_task, lsp, KM_SLEEP);
6340fbb751dSJohn Levon }
6350fbb751dSJohn Levon 
6360fbb751dSJohn Levon static void
lofi_free_dev(struct lofi_state * lsp)637406fc510SToomas Soome lofi_free_dev(struct lofi_state *lsp)
6380fbb751dSJohn Levon {
6390fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
6400fbb751dSJohn Levon 
641406fc510SToomas Soome 	if (lsp->ls_cmlbhandle != NULL) {
642406fc510SToomas Soome 		cmlb_invalidate(lsp->ls_cmlbhandle, 0);
643406fc510SToomas Soome 		cmlb_detach(lsp->ls_cmlbhandle, 0);
644406fc510SToomas Soome 		cmlb_free_handle(&lsp->ls_cmlbhandle);
645406fc510SToomas Soome 		lsp->ls_cmlbhandle = NULL;
646406fc510SToomas Soome 	}
647406fc510SToomas Soome 	(void) ddi_prop_remove_all(lsp->ls_dip);
648406fc510SToomas Soome 	ddi_remove_minor_node(lsp->ls_dip, NULL);
6490fbb751dSJohn Levon }
6500fbb751dSJohn Levon 
6510fbb751dSJohn Levon static void
lofi_zone_shutdown(zoneid_t zoneid,void * arg __unused)65203f27e8bSAndy Fiddaman lofi_zone_shutdown(zoneid_t zoneid, void *arg __unused)
6530fbb751dSJohn Levon {
6540fbb751dSJohn Levon 	struct lofi_state *lsp;
6550fbb751dSJohn Levon 	struct lofi_state *next;
6560fbb751dSJohn Levon 
6570fbb751dSJohn Levon 	mutex_enter(&lofi_lock);
6580fbb751dSJohn Levon 
6590fbb751dSJohn Levon 	for (lsp = list_head(&lofi_list); lsp != NULL; lsp = next) {
6600fbb751dSJohn Levon 
6610fbb751dSJohn Levon 		/* lofi_destroy() frees lsp */
6620fbb751dSJohn Levon 		next = list_next(&lofi_list, lsp);
6630fbb751dSJohn Levon 
664a19609f8Sjv227347 		if (lsp->ls_zone.zref_zone->zone_id != zoneid)
6650fbb751dSJohn Levon 			continue;
6660fbb751dSJohn Levon 
6670fbb751dSJohn Levon 		/*
6680fbb751dSJohn Levon 		 * No in-zone processes are running, but something has this
6690fbb751dSJohn Levon 		 * open.  It's either a global zone process, or a lofi
6700fbb751dSJohn Levon 		 * mount.  In either case we set ls_cleanup so the last
6710fbb751dSJohn Levon 		 * user destroys the device.
6720fbb751dSJohn Levon 		 */
6730fbb751dSJohn Levon 		if (is_opened(lsp)) {
674b4844717SToomas Soome 			lofi_set_cleanup(lsp);
6750fbb751dSJohn Levon 		} else {
676406fc510SToomas Soome 			lofi_free_dev(lsp);
6770fbb751dSJohn Levon 			lofi_destroy(lsp, kcred);
6780fbb751dSJohn Levon 		}
6790fbb751dSJohn Levon 	}
6800fbb751dSJohn Levon 
6810fbb751dSJohn Levon 	mutex_exit(&lofi_lock);
6823d7072f8Seschrock }
6833d7072f8Seschrock 
6847c478bd9Sstevel@tonic-gate static int
lofi_open(dev_t * devp,int flag,int otyp,struct cred * credp __unused)68503f27e8bSAndy Fiddaman lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp __unused)
6867c478bd9Sstevel@tonic-gate {
687406fc510SToomas Soome 	int id;
688406fc510SToomas Soome 	minor_t	part;
689406fc510SToomas Soome 	uint64_t mask;
690406fc510SToomas Soome 	diskaddr_t nblks;
691406fc510SToomas Soome 	diskaddr_t lba;
692406fc510SToomas Soome 	boolean_t ndelay;
693406fc510SToomas Soome 
6947c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
6957c478bd9Sstevel@tonic-gate 
696406fc510SToomas Soome 	if (otyp >= OTYPCNT)
697406fc510SToomas Soome 		return (EINVAL);
698406fc510SToomas Soome 
699406fc510SToomas Soome 	ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
700406fc510SToomas Soome 
7010fbb751dSJohn Levon 	/*
7020fbb751dSJohn Levon 	 * lofiadm -a /dev/lofi/1 gets us here.
7030fbb751dSJohn Levon 	 */
7040fbb751dSJohn Levon 	if (mutex_owner(&lofi_lock) == curthread)
7057c478bd9Sstevel@tonic-gate 		return (EINVAL);
7060fbb751dSJohn Levon 
7070fbb751dSJohn Levon 	mutex_enter(&lofi_lock);
7080fbb751dSJohn Levon 
709406fc510SToomas Soome 	id = LOFI_MINOR2ID(getminor(*devp));
710406fc510SToomas Soome 	part = LOFI_PART(getminor(*devp));
711406fc510SToomas Soome 	mask = (1U << part);
7120fbb751dSJohn Levon 
7130fbb751dSJohn Levon 	/* master control device */
714406fc510SToomas Soome 	if (id == 0) {
7157c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
7167c478bd9Sstevel@tonic-gate 		return (0);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/* otherwise, the mapping should already exist */
720406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, id);
7217c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
7227c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
7237c478bd9Sstevel@tonic-gate 		return (EINVAL);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
726b4844717SToomas Soome 	if (lsp->ls_cleanup == B_TRUE) {
727b4844717SToomas Soome 		mutex_exit(&lofi_lock);
728b4844717SToomas Soome 		return (ENXIO);
729b4844717SToomas Soome 	}
730b4844717SToomas Soome 
7313d7072f8Seschrock 	if (lsp->ls_vp == NULL) {
7323d7072f8Seschrock 		mutex_exit(&lofi_lock);
7333d7072f8Seschrock 		return (ENXIO);
7343d7072f8Seschrock 	}
7353d7072f8Seschrock 
736cd69fabeSAlexander Eremin 	if (lsp->ls_readonly && (flag & FWRITE)) {
737cd69fabeSAlexander Eremin 		mutex_exit(&lofi_lock);
738cd69fabeSAlexander Eremin 		return (EROFS);
739cd69fabeSAlexander Eremin 	}
740cd69fabeSAlexander Eremin 
741406fc510SToomas Soome 	if ((lsp->ls_open_excl) & (mask)) {
742a67ac136SAlexander Eremin 		mutex_exit(&lofi_lock);
743406fc510SToomas Soome 		return (EBUSY);
744406fc510SToomas Soome 	}
745406fc510SToomas Soome 
746406fc510SToomas Soome 	if (flag & FEXCL) {
747406fc510SToomas Soome 		if (lsp->ls_open_lyr[part]) {
748406fc510SToomas Soome 			mutex_exit(&lofi_lock);
749406fc510SToomas Soome 			return (EBUSY);
750406fc510SToomas Soome 		}
751406fc510SToomas Soome 		for (int i = 0; i < OTYP_LYR; i++) {
752406fc510SToomas Soome 			if (lsp->ls_open_reg[i] & mask) {
753406fc510SToomas Soome 				mutex_exit(&lofi_lock);
754406fc510SToomas Soome 				return (EBUSY);
755406fc510SToomas Soome 			}
756406fc510SToomas Soome 		}
757406fc510SToomas Soome 	}
758406fc510SToomas Soome 
759406fc510SToomas Soome 	if (lsp->ls_cmlbhandle != NULL) {
760406fc510SToomas Soome 		if (cmlb_validate(lsp->ls_cmlbhandle, 0, 0) != 0) {
761406fc510SToomas Soome 			/*
762406fc510SToomas Soome 			 * non-blocking opens are allowed to succeed to
763406fc510SToomas Soome 			 * support format and fdisk to create partitioning.
764406fc510SToomas Soome 			 */
765406fc510SToomas Soome 			if (!ndelay) {
766406fc510SToomas Soome 				mutex_exit(&lofi_lock);
767406fc510SToomas Soome 				return (ENXIO);
768406fc510SToomas Soome 			}
769406fc510SToomas Soome 		} else if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &nblks, &lba,
770406fc510SToomas Soome 		    NULL, NULL, 0) == 0) {
771406fc510SToomas Soome 			if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
772406fc510SToomas Soome 				mutex_exit(&lofi_lock);
773406fc510SToomas Soome 				return (ENXIO);
774406fc510SToomas Soome 			}
775406fc510SToomas Soome 		} else if (!ndelay) {
776406fc510SToomas Soome 			mutex_exit(&lofi_lock);
777406fc510SToomas Soome 			return (ENXIO);
778406fc510SToomas Soome 		}
779406fc510SToomas Soome 	}
780406fc510SToomas Soome 
781406fc510SToomas Soome 	if (otyp == OTYP_LYR) {
782406fc510SToomas Soome 		lsp->ls_open_lyr[part]++;
783406fc510SToomas Soome 	} else {
784406fc510SToomas Soome 		lsp->ls_open_reg[otyp] |= mask;
785406fc510SToomas Soome 	}
786406fc510SToomas Soome 	if (flag & FEXCL) {
787406fc510SToomas Soome 		lsp->ls_open_excl |= mask;
788a67ac136SAlexander Eremin 	}
789a67ac136SAlexander Eremin 
7907c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
7917c478bd9Sstevel@tonic-gate 	return (0);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate static int
lofi_close(dev_t dev,int flag __unused,int otyp,struct cred * credp)79503f27e8bSAndy Fiddaman lofi_close(dev_t dev, int flag __unused, int otyp, struct cred *credp)
7967c478bd9Sstevel@tonic-gate {
797406fc510SToomas Soome 	minor_t	part;
798406fc510SToomas Soome 	int id;
799406fc510SToomas Soome 	uint64_t mask;
8007c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
8017c478bd9Sstevel@tonic-gate 
802406fc510SToomas Soome 	id = LOFI_MINOR2ID(getminor(dev));
803406fc510SToomas Soome 	part = LOFI_PART(getminor(dev));
804406fc510SToomas Soome 	mask = (1U << part);
805406fc510SToomas Soome 
8067c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
807406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, id);
8087c478bd9Sstevel@tonic-gate 	if (lsp == NULL) {
8097c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
8107c478bd9Sstevel@tonic-gate 		return (EINVAL);
8117c478bd9Sstevel@tonic-gate 	}
8120fbb751dSJohn Levon 
813406fc510SToomas Soome 	if (id == 0) {
8140fbb751dSJohn Levon 		mutex_exit(&lofi_lock);
8150fbb751dSJohn Levon 		return (0);
8160fbb751dSJohn Levon 	}
8170fbb751dSJohn Levon 
818406fc510SToomas Soome 	if (lsp->ls_open_excl & mask)
819406fc510SToomas Soome 		lsp->ls_open_excl &= ~mask;
820406fc510SToomas Soome 
821406fc510SToomas Soome 	if (otyp == OTYP_LYR) {
822406fc510SToomas Soome 		lsp->ls_open_lyr[part]--;
823406fc510SToomas Soome 	} else {
824406fc510SToomas Soome 		lsp->ls_open_reg[otyp] &= ~mask;
825406fc510SToomas Soome 	}
8263d7072f8Seschrock 
8273d7072f8Seschrock 	/*
82893239addSjohnlev 	 * If we forcibly closed the underlying device (li_force), or
82993239addSjohnlev 	 * asked for cleanup (li_cleanup), finish up if we're the last
83093239addSjohnlev 	 * out of the door.
8313d7072f8Seschrock 	 */
832b4844717SToomas Soome 	if (!is_opened(lsp) &&
833b4844717SToomas Soome 	    (lsp->ls_cleanup == B_TRUE || lsp->ls_vp == NULL)) {
834406fc510SToomas Soome 		lofi_free_dev(lsp);
8350fbb751dSJohn Levon 		lofi_destroy(lsp, credp);
8360fbb751dSJohn Levon 	}
83793239addSjohnlev 
8387c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
8397c478bd9Sstevel@tonic-gate 	return (0);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427d82f0f8SDina K Nimeh /*
8437d82f0f8SDina K Nimeh  * Sets the mechanism's initialization vector (IV) if one is needed.
8447d82f0f8SDina K Nimeh  * The IV is computed from the data block number.  lsp->ls_mech is
8457d82f0f8SDina K Nimeh  * altered so that:
8467d82f0f8SDina K Nimeh  *	lsp->ls_mech.cm_param_len is set to the IV len.
8477d82f0f8SDina K Nimeh  *	lsp->ls_mech.cm_param is set to the IV.
8487d82f0f8SDina K Nimeh  */
8497d82f0f8SDina K Nimeh static int
lofi_blk_mech(struct lofi_state * lsp,longlong_t lblkno)8507d82f0f8SDina K Nimeh lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
8517d82f0f8SDina K Nimeh {
8527d82f0f8SDina K Nimeh 	int	ret;
8537d82f0f8SDina K Nimeh 	crypto_data_t cdata;
8547d82f0f8SDina K Nimeh 	char	*iv;
8557d82f0f8SDina K Nimeh 	size_t	iv_len;
8567d82f0f8SDina K Nimeh 	size_t	min;
8577d82f0f8SDina K Nimeh 	void	*data;
8587d82f0f8SDina K Nimeh 	size_t	datasz;
8597d82f0f8SDina K Nimeh 
8600fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lsp->ls_crypto_lock));
8617d82f0f8SDina K Nimeh 
8627d82f0f8SDina K Nimeh 	if (lsp == NULL)
8637d82f0f8SDina K Nimeh 		return (CRYPTO_DEVICE_ERROR);
8647d82f0f8SDina K Nimeh 
8657d82f0f8SDina K Nimeh 	/* lsp->ls_mech.cm_param{_len} has already been set for static iv */
8667d82f0f8SDina K Nimeh 	if (lsp->ls_iv_type == IVM_NONE) {
8677d82f0f8SDina K Nimeh 		return (CRYPTO_SUCCESS);
8687d82f0f8SDina K Nimeh 	}
8697d82f0f8SDina K Nimeh 
8707d82f0f8SDina K Nimeh 	/*
8717d82f0f8SDina K Nimeh 	 * if kmem already alloced from previous call and it's the same size
8727d82f0f8SDina K Nimeh 	 * we need now, just recycle it; allocate new kmem only if we have to
8737d82f0f8SDina K Nimeh 	 */
8747d82f0f8SDina K Nimeh 	if (lsp->ls_mech.cm_param == NULL ||
8757d82f0f8SDina K Nimeh 	    lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
8767d82f0f8SDina K Nimeh 		iv_len = lsp->ls_iv_len;
8777d82f0f8SDina K Nimeh 		iv = kmem_zalloc(iv_len, KM_SLEEP);
8787d82f0f8SDina K Nimeh 	} else {
8797d82f0f8SDina K Nimeh 		iv_len = lsp->ls_mech.cm_param_len;
8807d82f0f8SDina K Nimeh 		iv = lsp->ls_mech.cm_param;
8817d82f0f8SDina K Nimeh 		bzero(iv, iv_len);
8827d82f0f8SDina K Nimeh 	}
8837d82f0f8SDina K Nimeh 
8847d82f0f8SDina K Nimeh 	switch (lsp->ls_iv_type) {
8857d82f0f8SDina K Nimeh 	case IVM_ENC_BLKNO:
8867d82f0f8SDina K Nimeh 		/* iv is not static, lblkno changes each time */
8877d82f0f8SDina K Nimeh 		data = &lblkno;
8887d82f0f8SDina K Nimeh 		datasz = sizeof (lblkno);
8897d82f0f8SDina K Nimeh 		break;
8907d82f0f8SDina K Nimeh 	default:
8917d82f0f8SDina K Nimeh 		data = 0;
8927d82f0f8SDina K Nimeh 		datasz = 0;
8937d82f0f8SDina K Nimeh 		break;
8947d82f0f8SDina K Nimeh 	}
8957d82f0f8SDina K Nimeh 
8967d82f0f8SDina K Nimeh 	/*
8977d82f0f8SDina K Nimeh 	 * write blkno into the iv buffer padded on the left in case
8987d82f0f8SDina K Nimeh 	 * blkno ever grows bigger than its current longlong_t size
8997d82f0f8SDina K Nimeh 	 * or a variation other than blkno is used for the iv data
9007d82f0f8SDina K Nimeh 	 */
9017d82f0f8SDina K Nimeh 	min = MIN(datasz, iv_len);
9027d82f0f8SDina K Nimeh 	bcopy(data, iv + (iv_len - min), min);
9037d82f0f8SDina K Nimeh 
9047d82f0f8SDina K Nimeh 	/* encrypt the data in-place to get the IV */
9057d82f0f8SDina K Nimeh 	SETUP_C_DATA(cdata, iv, iv_len);
9067d82f0f8SDina K Nimeh 
9077d82f0f8SDina K Nimeh 	ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
9087d82f0f8SDina K Nimeh 	    NULL, NULL, NULL);
9097d82f0f8SDina K Nimeh 	if (ret != CRYPTO_SUCCESS) {
9107d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
9117d82f0f8SDina K Nimeh 		    lblkno, ret);
9127d82f0f8SDina K Nimeh 		if (lsp->ls_mech.cm_param != iv)
9137d82f0f8SDina K Nimeh 			kmem_free(iv, iv_len);
914b1efbcd6SAlok Aggarwal 
9157d82f0f8SDina K Nimeh 		return (ret);
9167d82f0f8SDina K Nimeh 	}
9177d82f0f8SDina K Nimeh 
9187d82f0f8SDina K Nimeh 	/* clean up the iv from the last computation */
9197d82f0f8SDina K Nimeh 	if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
9207d82f0f8SDina K Nimeh 		kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
921b1efbcd6SAlok Aggarwal 
9227d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_param_len = iv_len;
9237d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_param = iv;
9247d82f0f8SDina K Nimeh 
9257d82f0f8SDina K Nimeh 	return (CRYPTO_SUCCESS);
9267d82f0f8SDina K Nimeh }
9277d82f0f8SDina K Nimeh 
9287d82f0f8SDina K Nimeh /*
9297d82f0f8SDina K Nimeh  * Performs encryption and decryption of a chunk of data of size "len",
9307d82f0f8SDina K Nimeh  * one DEV_BSIZE block at a time.  "len" is assumed to be a multiple of
9317d82f0f8SDina K Nimeh  * DEV_BSIZE.
9327d82f0f8SDina K Nimeh  */
9337d82f0f8SDina K Nimeh static int
lofi_crypto(struct lofi_state * lsp,struct buf * bp,caddr_t plaintext,caddr_t ciphertext,size_t len,boolean_t op_encrypt)9347d82f0f8SDina K Nimeh lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
9357d82f0f8SDina K Nimeh     caddr_t ciphertext, size_t len, boolean_t op_encrypt)
9367d82f0f8SDina K Nimeh {
9377d82f0f8SDina K Nimeh 	crypto_data_t cdata;
9387d82f0f8SDina K Nimeh 	crypto_data_t wdata;
9397d82f0f8SDina K Nimeh 	int ret;
9407d82f0f8SDina K Nimeh 	longlong_t lblkno = bp->b_lblkno;
9417d82f0f8SDina K Nimeh 
9427d82f0f8SDina K Nimeh 	mutex_enter(&lsp->ls_crypto_lock);
9437d82f0f8SDina K Nimeh 
9447d82f0f8SDina K Nimeh 	/*
9457d82f0f8SDina K Nimeh 	 * though we could encrypt/decrypt entire "len" chunk of data, we need
9467d82f0f8SDina K Nimeh 	 * to break it into DEV_BSIZE pieces to capture blkno incrementing
9477d82f0f8SDina K Nimeh 	 */
9487d82f0f8SDina K Nimeh 	SETUP_C_DATA(cdata, plaintext, len);
9497d82f0f8SDina K Nimeh 	cdata.cd_length = DEV_BSIZE;
9507d82f0f8SDina K Nimeh 	if (ciphertext != NULL) {		/* not in-place crypto */
9517d82f0f8SDina K Nimeh 		SETUP_C_DATA(wdata, ciphertext, len);
9527d82f0f8SDina K Nimeh 		wdata.cd_length = DEV_BSIZE;
9537d82f0f8SDina K Nimeh 	}
9547d82f0f8SDina K Nimeh 
9557d82f0f8SDina K Nimeh 	do {
9567d82f0f8SDina K Nimeh 		ret = lofi_blk_mech(lsp, lblkno);
9577d82f0f8SDina K Nimeh 		if (ret != CRYPTO_SUCCESS)
9587d82f0f8SDina K Nimeh 			continue;
9597d82f0f8SDina K Nimeh 
9607d82f0f8SDina K Nimeh 		if (op_encrypt) {
9617d82f0f8SDina K Nimeh 			ret = crypto_encrypt(&lsp->ls_mech, &cdata,
9627d82f0f8SDina K Nimeh 			    &lsp->ls_key, NULL,
9637d82f0f8SDina K Nimeh 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
9647d82f0f8SDina K Nimeh 		} else {
9657d82f0f8SDina K Nimeh 			ret = crypto_decrypt(&lsp->ls_mech, &cdata,
9667d82f0f8SDina K Nimeh 			    &lsp->ls_key, NULL,
9677d82f0f8SDina K Nimeh 			    ((ciphertext != NULL) ? &wdata : NULL), NULL);
9687d82f0f8SDina K Nimeh 		}
9697d82f0f8SDina K Nimeh 
9707d82f0f8SDina K Nimeh 		cdata.cd_offset += DEV_BSIZE;
9717d82f0f8SDina K Nimeh 		if (ciphertext != NULL)
9727d82f0f8SDina K Nimeh 			wdata.cd_offset += DEV_BSIZE;
9737d82f0f8SDina K Nimeh 		lblkno++;
9747d82f0f8SDina K Nimeh 	} while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
9757d82f0f8SDina K Nimeh 
9767d82f0f8SDina K Nimeh 	mutex_exit(&lsp->ls_crypto_lock);
9777d82f0f8SDina K Nimeh 
9787d82f0f8SDina K Nimeh 	if (ret != CRYPTO_SUCCESS) {
9797d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "%s failed for block %lld:  (0x%x)",
9807d82f0f8SDina K Nimeh 		    op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
9817d82f0f8SDina K Nimeh 		    lblkno, ret);
9827d82f0f8SDina K Nimeh 	}
9837d82f0f8SDina K Nimeh 
9847d82f0f8SDina K Nimeh 	return (ret);
9857d82f0f8SDina K Nimeh }
9867d82f0f8SDina K Nimeh 
9877d82f0f8SDina K Nimeh static int
lofi_rdwr(caddr_t bufaddr,offset_t offset,struct buf * bp,struct lofi_state * lsp,size_t len,lofi_rdrw_method_t method,caddr_t bcopy_locn)9887d82f0f8SDina K Nimeh lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
98903f27e8bSAndy Fiddaman     struct lofi_state *lsp, size_t len, lofi_rdrw_method_t method,
99003f27e8bSAndy Fiddaman     caddr_t bcopy_locn)
9917d82f0f8SDina K Nimeh {
9927d82f0f8SDina K Nimeh 	ssize_t resid;
9937d82f0f8SDina K Nimeh 	int isread;
9947d82f0f8SDina K Nimeh 	int error;
9957d82f0f8SDina K Nimeh 
9967d82f0f8SDina K Nimeh 	/*
9977d82f0f8SDina K Nimeh 	 * Handles reads/writes for both plain and encrypted lofi
9987d82f0f8SDina K Nimeh 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
9997d82f0f8SDina K Nimeh 	 * when it gets here.
10007d82f0f8SDina K Nimeh 	 */
10017d82f0f8SDina K Nimeh 
10027d82f0f8SDina K Nimeh 	isread = bp->b_flags & B_READ;
10037d82f0f8SDina K Nimeh 	if (isread) {
10047d82f0f8SDina K Nimeh 		if (method == RDWR_BCOPY) {
10057d82f0f8SDina K Nimeh 			/* DO NOT update bp->b_resid for bcopy */
10067d82f0f8SDina K Nimeh 			bcopy(bcopy_locn, bufaddr, len);
10077d82f0f8SDina K Nimeh 			error = 0;
10087d82f0f8SDina K Nimeh 		} else {		/* RDWR_RAW */
10097d82f0f8SDina K Nimeh 			error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
10107d82f0f8SDina K Nimeh 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
10117d82f0f8SDina K Nimeh 			    &resid);
10127d82f0f8SDina K Nimeh 			bp->b_resid = resid;
10137d82f0f8SDina K Nimeh 		}
10147d82f0f8SDina K Nimeh 		if (lsp->ls_crypto_enabled && error == 0) {
10157d82f0f8SDina K Nimeh 			if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
10167d82f0f8SDina K Nimeh 			    B_FALSE) != CRYPTO_SUCCESS) {
10177d82f0f8SDina K Nimeh 				/*
10187d82f0f8SDina K Nimeh 				 * XXX: original code didn't set residual
10197d82f0f8SDina K Nimeh 				 * back to len because no error was expected
10207d82f0f8SDina K Nimeh 				 * from bcopy() if encryption is not enabled
10217d82f0f8SDina K Nimeh 				 */
10227d82f0f8SDina K Nimeh 				if (method != RDWR_BCOPY)
10237d82f0f8SDina K Nimeh 					bp->b_resid = len;
10247d82f0f8SDina K Nimeh 				error = EIO;
10257d82f0f8SDina K Nimeh 			}
10267d82f0f8SDina K Nimeh 		}
10277d82f0f8SDina K Nimeh 		return (error);
10287d82f0f8SDina K Nimeh 	} else {
10297d82f0f8SDina K Nimeh 		void *iobuf = bufaddr;
10307d82f0f8SDina K Nimeh 
10317d82f0f8SDina K Nimeh 		if (lsp->ls_crypto_enabled) {
10327d82f0f8SDina K Nimeh 			/* don't do in-place crypto to keep bufaddr intact */
10337d82f0f8SDina K Nimeh 			iobuf = kmem_alloc(len, KM_SLEEP);
10347d82f0f8SDina K Nimeh 			if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
10357d82f0f8SDina K Nimeh 			    B_TRUE) != CRYPTO_SUCCESS) {
10367d82f0f8SDina K Nimeh 				kmem_free(iobuf, len);
10377d82f0f8SDina K Nimeh 				if (method != RDWR_BCOPY)
10387d82f0f8SDina K Nimeh 					bp->b_resid = len;
10397d82f0f8SDina K Nimeh 				return (EIO);
10407d82f0f8SDina K Nimeh 			}
10417d82f0f8SDina K Nimeh 		}
10427d82f0f8SDina K Nimeh 		if (method == RDWR_BCOPY) {
10437d82f0f8SDina K Nimeh 			/* DO NOT update bp->b_resid for bcopy */
10447d82f0f8SDina K Nimeh 			bcopy(iobuf, bcopy_locn, len);
10457d82f0f8SDina K Nimeh 			error = 0;
10467d82f0f8SDina K Nimeh 		} else {		/* RDWR_RAW */
10477d82f0f8SDina K Nimeh 			error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
10487d82f0f8SDina K Nimeh 			    offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
10497d82f0f8SDina K Nimeh 			    &resid);
10507d82f0f8SDina K Nimeh 			bp->b_resid = resid;
10517d82f0f8SDina K Nimeh 		}
10527d82f0f8SDina K Nimeh 		if (lsp->ls_crypto_enabled) {
10537d82f0f8SDina K Nimeh 			kmem_free(iobuf, len);
10547d82f0f8SDina K Nimeh 		}
10557d82f0f8SDina K Nimeh 		return (error);
10567d82f0f8SDina K Nimeh 	}
10577d82f0f8SDina K Nimeh }
10587d82f0f8SDina K Nimeh 
105987117650Saalok static int
lofi_mapped_rdwr(caddr_t bufaddr,offset_t offset,struct buf * bp,struct lofi_state * lsp)106087117650Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
106187117650Saalok     struct lofi_state *lsp)
10627c478bd9Sstevel@tonic-gate {
10637c478bd9Sstevel@tonic-gate 	int error;
106487117650Saalok 	offset_t alignedoffset, mapoffset;
10657c478bd9Sstevel@tonic-gate 	size_t	xfersize;
10667c478bd9Sstevel@tonic-gate 	int	isread;
10677c478bd9Sstevel@tonic-gate 	int	smflags;
106887117650Saalok 	caddr_t	mapaddr;
106987117650Saalok 	size_t	len;
10707c478bd9Sstevel@tonic-gate 	enum seg_rw srw;
10717d82f0f8SDina K Nimeh 	int	save_error;
10727d82f0f8SDina K Nimeh 
10737d82f0f8SDina K Nimeh 	/*
10747d82f0f8SDina K Nimeh 	 * Note:  offset is already shifted by lsp->ls_crypto_offset
10757d82f0f8SDina K Nimeh 	 * when it gets here.
10767d82f0f8SDina K Nimeh 	 */
10777d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled)
10787d82f0f8SDina K Nimeh 		ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	/*
10817c478bd9Sstevel@tonic-gate 	 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
10827c478bd9Sstevel@tonic-gate 	 * an 8K boundary, but the buf transfer address may not be
108387117650Saalok 	 * aligned on more than a 512-byte boundary (we don't enforce
108487117650Saalok 	 * that even though we could). This matters since the initial
108587117650Saalok 	 * part of the transfer may not start at offset 0 within the
108687117650Saalok 	 * segmap'd chunk. So we have to compensate for that with
108787117650Saalok 	 * 'mapoffset'. Subsequent chunks always start off at the
108887117650Saalok 	 * beginning, and the last is capped by b_resid
10897d82f0f8SDina K Nimeh 	 *
10907d82f0f8SDina K Nimeh 	 * Visually, where "|" represents page map boundaries:
10917d82f0f8SDina K Nimeh 	 *   alignedoffset (mapaddr begins at this segmap boundary)
10927d82f0f8SDina K Nimeh 	 *    |   offset (from beginning of file)
10937d82f0f8SDina K Nimeh 	 *    |    |	   len
10947d82f0f8SDina K Nimeh 	 *    v    v	    v
10957d82f0f8SDina K Nimeh 	 * ===|====X========|====...======|========X====|====
10967d82f0f8SDina K Nimeh 	 *	   /-------------...---------------/
10977d82f0f8SDina K Nimeh 	 *		^ bp->b_bcount/bp->b_resid at start
10987d82f0f8SDina K Nimeh 	 *    /----/--------/----...------/--------/
10997d82f0f8SDina K Nimeh 	 *	^	^	^   ^		^
11007d82f0f8SDina K Nimeh 	 *	|	|	|   |		nth xfersize (<= MAXBSIZE)
11017d82f0f8SDina K Nimeh 	 *	|	|	2nd thru n-1st xfersize (= MAXBSIZE)
11027d82f0f8SDina K Nimeh 	 *	|	1st xfersize (<= MAXBSIZE)
11037d82f0f8SDina K Nimeh 	 *    mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
11047d82f0f8SDina K Nimeh 	 *
11057d82f0f8SDina K Nimeh 	 * Notes: "alignedoffset" is "offset" rounded down to nearest
11067d82f0f8SDina K Nimeh 	 * MAXBSIZE boundary.  "len" is next page boundary of size
1107a363f650SDina K Nimeh 	 * PAGESIZE after "alignedoffset".
11087c478bd9Sstevel@tonic-gate 	 */
11097c478bd9Sstevel@tonic-gate 	mapoffset = offset & MAXBOFFSET;
111087117650Saalok 	alignedoffset = offset - mapoffset;
11117c478bd9Sstevel@tonic-gate 	bp->b_resid = bp->b_bcount;
11127c478bd9Sstevel@tonic-gate 	isread = bp->b_flags & B_READ;
11137c478bd9Sstevel@tonic-gate 	srw = isread ? S_READ : S_WRITE;
11147c478bd9Sstevel@tonic-gate 	do {
111587117650Saalok 		xfersize = MIN(lsp->ls_vp_comp_size - offset,
11167c478bd9Sstevel@tonic-gate 		    MIN(MAXBSIZE - mapoffset, bp->b_resid));
1117a363f650SDina K Nimeh 		len = roundup(mapoffset + xfersize, PAGESIZE);
11187c478bd9Sstevel@tonic-gate 		mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
11197c478bd9Sstevel@tonic-gate 		    alignedoffset, MAXBSIZE, 1, srw);
11207c478bd9Sstevel@tonic-gate 		/*
11217c478bd9Sstevel@tonic-gate 		 * Now fault in the pages. This lets us check
11227c478bd9Sstevel@tonic-gate 		 * for errors before we reference mapaddr and
11237c478bd9Sstevel@tonic-gate 		 * try to resolve the fault in bcopy (which would
11247c478bd9Sstevel@tonic-gate 		 * panic instead). And this can easily happen,
11257c478bd9Sstevel@tonic-gate 		 * particularly if you've lofi'd a file over NFS
11267c478bd9Sstevel@tonic-gate 		 * and someone deletes the file on the server.
11277c478bd9Sstevel@tonic-gate 		 */
11287c478bd9Sstevel@tonic-gate 		error = segmap_fault(kas.a_hat, segkmap, mapaddr,
11297c478bd9Sstevel@tonic-gate 		    len, F_SOFTLOCK, srw);
11307c478bd9Sstevel@tonic-gate 		if (error) {
11317c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, mapaddr, 0);
11327c478bd9Sstevel@tonic-gate 			if (FC_CODE(error) == FC_OBJERR)
11337c478bd9Sstevel@tonic-gate 				error = FC_ERRNO(error);
11347c478bd9Sstevel@tonic-gate 			else
11357c478bd9Sstevel@tonic-gate 				error = EIO;
11367c478bd9Sstevel@tonic-gate 			break;
11377c478bd9Sstevel@tonic-gate 		}
11387d82f0f8SDina K Nimeh 		/* error may be non-zero for encrypted lofi */
11397d82f0f8SDina K Nimeh 		error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
11407d82f0f8SDina K Nimeh 		    RDWR_BCOPY, mapaddr + mapoffset);
11417d82f0f8SDina K Nimeh 		if (error == 0) {
11427d82f0f8SDina K Nimeh 			bp->b_resid -= xfersize;
11437d82f0f8SDina K Nimeh 			bufaddr += xfersize;
11447d82f0f8SDina K Nimeh 			offset += xfersize;
11457d82f0f8SDina K Nimeh 		}
11467c478bd9Sstevel@tonic-gate 		smflags = 0;
11477c478bd9Sstevel@tonic-gate 		if (isread) {
114887117650Saalok 			smflags |= SM_FREE;
114987117650Saalok 			/*
115087117650Saalok 			 * If we're reading an entire page starting
115187117650Saalok 			 * at a page boundary, there's a good chance
115287117650Saalok 			 * we won't need it again. Put it on the
115387117650Saalok 			 * head of the freelist.
115487117650Saalok 			 */
1155bbd65dd2SDina K Nimeh 			if (mapoffset == 0 && xfersize == MAXBSIZE)
115687117650Saalok 				smflags |= SM_DONTNEED;
11577c478bd9Sstevel@tonic-gate 		} else {
115844a1e32bSbatschul 			/*
115944a1e32bSbatschul 			 * Write back good pages, it is okay to
116044a1e32bSbatschul 			 * always release asynchronous here as we'll
116144a1e32bSbatschul 			 * follow with VOP_FSYNC for B_SYNC buffers.
116244a1e32bSbatschul 			 */
116344a1e32bSbatschul 			if (error == 0)
116444a1e32bSbatschul 				smflags |= SM_WRITE | SM_ASYNC;
11657c478bd9Sstevel@tonic-gate 		}
11667c478bd9Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap, mapaddr,
11677c478bd9Sstevel@tonic-gate 		    len, F_SOFTUNLOCK, srw);
11687d82f0f8SDina K Nimeh 		save_error = segmap_release(segkmap, mapaddr, smflags);
11697d82f0f8SDina K Nimeh 		if (error == 0)
11707d82f0f8SDina K Nimeh 			error = save_error;
11717c478bd9Sstevel@tonic-gate 		/* only the first map may start partial */
11727c478bd9Sstevel@tonic-gate 		mapoffset = 0;
11737c478bd9Sstevel@tonic-gate 		alignedoffset += MAXBSIZE;
11747c478bd9Sstevel@tonic-gate 	} while ((error == 0) && (bp->b_resid > 0) &&
117587117650Saalok 	    (offset < lsp->ls_vp_comp_size));
117687117650Saalok 
117787117650Saalok 	return (error);
117887117650Saalok }
117987117650Saalok 
11804058a205Sjrgn.keil@googlemail.com /*
11814058a205Sjrgn.keil@googlemail.com  * Check if segment seg_index is present in the decompressed segment
11824058a205Sjrgn.keil@googlemail.com  * data cache.
11834058a205Sjrgn.keil@googlemail.com  *
11844058a205Sjrgn.keil@googlemail.com  * Returns a pointer to the decompressed segment data cache entry if
11854058a205Sjrgn.keil@googlemail.com  * found, and NULL when decompressed data for this segment is not yet
11864058a205Sjrgn.keil@googlemail.com  * cached.
11874058a205Sjrgn.keil@googlemail.com  */
11884058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
lofi_find_comp_data(struct lofi_state * lsp,uint64_t seg_index)11894058a205Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
11904058a205Sjrgn.keil@googlemail.com {
11914058a205Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
11924058a205Sjrgn.keil@googlemail.com 
11930fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
11944058a205Sjrgn.keil@googlemail.com 
11954058a205Sjrgn.keil@googlemail.com 	for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
11964058a205Sjrgn.keil@googlemail.com 	    lc = list_next(&lsp->ls_comp_cache, lc)) {
11974058a205Sjrgn.keil@googlemail.com 		if (lc->lc_index == seg_index) {
11984058a205Sjrgn.keil@googlemail.com 			/*
11994058a205Sjrgn.keil@googlemail.com 			 * Decompressed segment data was found in the
12004058a205Sjrgn.keil@googlemail.com 			 * cache.
12014058a205Sjrgn.keil@googlemail.com 			 *
12024058a205Sjrgn.keil@googlemail.com 			 * The cache uses an LRU replacement strategy;
12034058a205Sjrgn.keil@googlemail.com 			 * move the entry to head of list.
12044058a205Sjrgn.keil@googlemail.com 			 */
12054058a205Sjrgn.keil@googlemail.com 			list_remove(&lsp->ls_comp_cache, lc);
12064058a205Sjrgn.keil@googlemail.com 			list_insert_head(&lsp->ls_comp_cache, lc);
12074058a205Sjrgn.keil@googlemail.com 			return (lc);
12084058a205Sjrgn.keil@googlemail.com 		}
12094058a205Sjrgn.keil@googlemail.com 	}
12104058a205Sjrgn.keil@googlemail.com 	return (NULL);
12114058a205Sjrgn.keil@googlemail.com }
12124058a205Sjrgn.keil@googlemail.com 
12134058a205Sjrgn.keil@googlemail.com /*
12144058a205Sjrgn.keil@googlemail.com  * Add the data for a decompressed segment at segment index
12154058a205Sjrgn.keil@googlemail.com  * seg_index to the cache of the decompressed segments.
12164058a205Sjrgn.keil@googlemail.com  *
12174058a205Sjrgn.keil@googlemail.com  * Returns a pointer to the cache element structure in case
12184058a205Sjrgn.keil@googlemail.com  * the data was added to the cache; returns NULL when the data
12194058a205Sjrgn.keil@googlemail.com  * wasn't cached.
12204058a205Sjrgn.keil@googlemail.com  */
12214058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
lofi_add_comp_data(struct lofi_state * lsp,uint64_t seg_index,uchar_t * data)12224058a205Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
12234058a205Sjrgn.keil@googlemail.com     uchar_t *data)
12244058a205Sjrgn.keil@googlemail.com {
12254058a205Sjrgn.keil@googlemail.com 	struct lofi_comp_cache *lc;
12264058a205Sjrgn.keil@googlemail.com 
12270fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
12284058a205Sjrgn.keil@googlemail.com 
12294058a205Sjrgn.keil@googlemail.com 	while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
12304058a205Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
12314058a205Sjrgn.keil@googlemail.com 		ASSERT(lc != NULL);
12324058a205Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
12334058a205Sjrgn.keil@googlemail.com 		kmem_free(lc, sizeof (struct lofi_comp_cache));
12344058a205Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count--;
12354058a205Sjrgn.keil@googlemail.com 	}
12364058a205Sjrgn.keil@googlemail.com 
12374058a205Sjrgn.keil@googlemail.com 	/*
12384058a205Sjrgn.keil@googlemail.com 	 * Do not cache when disabled by tunable variable
12394058a205Sjrgn.keil@googlemail.com 	 */
12404058a205Sjrgn.keil@googlemail.com 	if (lofi_max_comp_cache == 0)
12414058a205Sjrgn.keil@googlemail.com 		return (NULL);
12424058a205Sjrgn.keil@googlemail.com 
12434058a205Sjrgn.keil@googlemail.com 	/*
12444058a205Sjrgn.keil@googlemail.com 	 * When the cache has not yet reached the maximum allowed
12454058a205Sjrgn.keil@googlemail.com 	 * number of segments, allocate a new cache element.
12464058a205Sjrgn.keil@googlemail.com 	 * Otherwise the cache is full; reuse the last list element
12474058a205Sjrgn.keil@googlemail.com 	 * (LRU) for caching the decompressed segment data.
12484058a205Sjrgn.keil@googlemail.com 	 *
12494058a205Sjrgn.keil@googlemail.com 	 * The cache element for the new decompressed segment data is
12504058a205Sjrgn.keil@googlemail.com 	 * added to the head of the list.
12514058a205Sjrgn.keil@googlemail.com 	 */
12524058a205Sjrgn.keil@googlemail.com 	if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
12534058a205Sjrgn.keil@googlemail.com 		lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
12544058a205Sjrgn.keil@googlemail.com 		lc->lc_data = NULL;
12554058a205Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
12564058a205Sjrgn.keil@googlemail.com 		lsp->ls_comp_cache_count++;
12574058a205Sjrgn.keil@googlemail.com 	} else {
12584058a205Sjrgn.keil@googlemail.com 		lc = list_remove_tail(&lsp->ls_comp_cache);
12594058a205Sjrgn.keil@googlemail.com 		if (lc == NULL)
12604058a205Sjrgn.keil@googlemail.com 			return (NULL);
12614058a205Sjrgn.keil@googlemail.com 		list_insert_head(&lsp->ls_comp_cache, lc);
12624058a205Sjrgn.keil@googlemail.com 	}
12634058a205Sjrgn.keil@googlemail.com 
12644058a205Sjrgn.keil@googlemail.com 	/*
12654058a205Sjrgn.keil@googlemail.com 	 * Free old uncompressed segment data when reusing a cache
12664058a205Sjrgn.keil@googlemail.com 	 * entry.
12674058a205Sjrgn.keil@googlemail.com 	 */
12684058a205Sjrgn.keil@googlemail.com 	if (lc->lc_data != NULL)
12694058a205Sjrgn.keil@googlemail.com 		kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
12704058a205Sjrgn.keil@googlemail.com 
12714058a205Sjrgn.keil@googlemail.com 	lc->lc_data = data;
12724058a205Sjrgn.keil@googlemail.com 	lc->lc_index = seg_index;
12734058a205Sjrgn.keil@googlemail.com 	return (lc);
12744058a205Sjrgn.keil@googlemail.com }
12754058a205Sjrgn.keil@googlemail.com 
1276b1efbcd6SAlok Aggarwal static int
gzip_decompress(void * src,size_t srclen,void * dst,size_t * dstlen,int level __unused)1277b1efbcd6SAlok Aggarwal gzip_decompress(void *src, size_t srclen, void *dst,
127803f27e8bSAndy Fiddaman     size_t *dstlen, int level __unused)
127987117650Saalok {
128087117650Saalok 	ASSERT(*dstlen >= srclen);
128187117650Saalok 
128287117650Saalok 	if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
128387117650Saalok 		return (-1);
128487117650Saalok 	return (0);
128587117650Saalok }
128687117650Saalok 
1287b1efbcd6SAlok Aggarwal #define	LZMA_HEADER_SIZE	(LZMA_PROPS_SIZE + 8)
1288b1efbcd6SAlok Aggarwal static int
lzma_decompress(void * src,size_t srclen,void * dst,size_t * dstlen,int level __unused)1289b1efbcd6SAlok Aggarwal lzma_decompress(void *src, size_t srclen, void *dst,
129003f27e8bSAndy Fiddaman     size_t *dstlen, int level __unused)
1291b1efbcd6SAlok Aggarwal {
1292b1efbcd6SAlok Aggarwal 	size_t insizepure;
1293b1efbcd6SAlok Aggarwal 	void *actual_src;
1294b1efbcd6SAlok Aggarwal 	ELzmaStatus status;
1295b1efbcd6SAlok Aggarwal 
1296b1efbcd6SAlok Aggarwal 	insizepure = srclen - LZMA_HEADER_SIZE;
1297b1efbcd6SAlok Aggarwal 	actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
1298b1efbcd6SAlok Aggarwal 
1299b1efbcd6SAlok Aggarwal 	if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
1300b1efbcd6SAlok Aggarwal 	    (const Byte *)actual_src, &insizepure,
1301b1efbcd6SAlok Aggarwal 	    (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
1302b1efbcd6SAlok Aggarwal 	    &g_Alloc) != SZ_OK) {
1303b1efbcd6SAlok Aggarwal 		return (-1);
1304b1efbcd6SAlok Aggarwal 	}
1305b1efbcd6SAlok Aggarwal 	return (0);
1306b1efbcd6SAlok Aggarwal }
1307b1efbcd6SAlok Aggarwal 
130803f27e8bSAndy Fiddaman static void
lofi_trim_task(void * arg)130903f27e8bSAndy Fiddaman lofi_trim_task(void *arg)
131003f27e8bSAndy Fiddaman {
131103f27e8bSAndy Fiddaman 	struct buf *bp = (struct buf *)arg;
131203f27e8bSAndy Fiddaman 	diskaddr_t p_lba = (diskaddr_t)(uintptr_t)bp->b_private;
131303f27e8bSAndy Fiddaman 	struct lofi_state *lsp;
131403f27e8bSAndy Fiddaman 	off64_t start, length;
131503f27e8bSAndy Fiddaman 	int error;
131603f27e8bSAndy Fiddaman 
131703f27e8bSAndy Fiddaman 	lsp = ddi_get_soft_state(lofi_statep,
131803f27e8bSAndy Fiddaman 	    LOFI_MINOR2ID(getminor(bp->b_edev)));
131903f27e8bSAndy Fiddaman 
132003f27e8bSAndy Fiddaman 	if (lsp == NULL) {
132103f27e8bSAndy Fiddaman 		error = ENXIO;
132203f27e8bSAndy Fiddaman 		goto errout;
132303f27e8bSAndy Fiddaman 	}
132403f27e8bSAndy Fiddaman 
132503f27e8bSAndy Fiddaman 	if (lsp->ls_kstat != NULL) {
132603f27e8bSAndy Fiddaman 		mutex_enter(lsp->ls_kstat->ks_lock);
132703f27e8bSAndy Fiddaman 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
132803f27e8bSAndy Fiddaman 		mutex_exit(lsp->ls_kstat->ks_lock);
132903f27e8bSAndy Fiddaman 	}
133003f27e8bSAndy Fiddaman 
133103f27e8bSAndy Fiddaman 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
133203f27e8bSAndy Fiddaman 		error = EIO;
133303f27e8bSAndy Fiddaman 		goto errout;
133403f27e8bSAndy Fiddaman 	}
133503f27e8bSAndy Fiddaman 
133603f27e8bSAndy Fiddaman 	mutex_enter(&lsp->ls_vp_lock);
133703f27e8bSAndy Fiddaman 	lsp->ls_vp_iocount++;
133803f27e8bSAndy Fiddaman 	mutex_exit(&lsp->ls_vp_lock);
133903f27e8bSAndy Fiddaman 
134003f27e8bSAndy Fiddaman 	start = (bp->b_lblkno + p_lba) << lsp->ls_lbshift;
134103f27e8bSAndy Fiddaman 	length = bp->b_bcount;
134203f27e8bSAndy Fiddaman 
134303f27e8bSAndy Fiddaman 	if (lsp->ls_vp->v_type == VCHR || lsp->ls_vp->v_type == VBLK) {
134403f27e8bSAndy Fiddaman 		int rv;
134503f27e8bSAndy Fiddaman 		dkioc_free_list_t dfl = {
134603f27e8bSAndy Fiddaman 			.dfl_num_exts = 1,
134703f27e8bSAndy Fiddaman 			.dfl_offset = 0,
134803f27e8bSAndy Fiddaman 			.dfl_flags = 0,
134903f27e8bSAndy Fiddaman 			.dfl_exts = {
135003f27e8bSAndy Fiddaman 				{
135103f27e8bSAndy Fiddaman 					.dfle_start = start,
135203f27e8bSAndy Fiddaman 					.dfle_length = length
135303f27e8bSAndy Fiddaman 				}
135403f27e8bSAndy Fiddaman 			}
135503f27e8bSAndy Fiddaman 		};
135603f27e8bSAndy Fiddaman 
135703f27e8bSAndy Fiddaman 		error = VOP_IOCTL(lsp->ls_vp, DKIOCFREE, (intptr_t)&dfl,
135803f27e8bSAndy Fiddaman 		    FKIOCTL, kcred, &rv, NULL);
135903f27e8bSAndy Fiddaman 	} else {
136003f27e8bSAndy Fiddaman 		struct flock64 flck = { 0 };
136103f27e8bSAndy Fiddaman 
136203f27e8bSAndy Fiddaman 		flck.l_start = start;
136303f27e8bSAndy Fiddaman 		flck.l_len = length;
136403f27e8bSAndy Fiddaman 		flck.l_type = F_FREESP;
136503f27e8bSAndy Fiddaman 		flck.l_whence = 0;
136603f27e8bSAndy Fiddaman 
136703f27e8bSAndy Fiddaman 		error = VOP_SPACE(lsp->ls_vp, F_FREESP, &flck, 0, 0, kcred,
136803f27e8bSAndy Fiddaman 		    NULL);
136903f27e8bSAndy Fiddaman 	}
137003f27e8bSAndy Fiddaman 
137103f27e8bSAndy Fiddaman 	mutex_enter(&lsp->ls_vp_lock);
137203f27e8bSAndy Fiddaman 	if (--lsp->ls_vp_iocount == 0)
137303f27e8bSAndy Fiddaman 		cv_broadcast(&lsp->ls_vp_cv);
137403f27e8bSAndy Fiddaman 	mutex_exit(&lsp->ls_vp_lock);
137503f27e8bSAndy Fiddaman 
137603f27e8bSAndy Fiddaman errout:
137703f27e8bSAndy Fiddaman 
137803f27e8bSAndy Fiddaman 	if (lsp != NULL && lsp->ls_kstat != NULL) {
137903f27e8bSAndy Fiddaman 		mutex_enter(lsp->ls_kstat->ks_lock);
138003f27e8bSAndy Fiddaman 		kstat_runq_exit(KSTAT_IO_PTR(lsp->ls_kstat));
138103f27e8bSAndy Fiddaman 		mutex_exit(lsp->ls_kstat->ks_lock);
138203f27e8bSAndy Fiddaman 	}
138303f27e8bSAndy Fiddaman 
138403f27e8bSAndy Fiddaman 	bioerror(bp, error);
138503f27e8bSAndy Fiddaman 	biodone(bp);
138603f27e8bSAndy Fiddaman }
138703f27e8bSAndy Fiddaman 
138887117650Saalok /*
138987117650Saalok  * This is basically what strategy used to be before we found we
139087117650Saalok  * needed task queues.
139187117650Saalok  */
139287117650Saalok static void
lofi_strategy_task(void * arg)139387117650Saalok lofi_strategy_task(void *arg)
139487117650Saalok {
139587117650Saalok 	struct buf *bp = (struct buf *)arg;
139603f27e8bSAndy Fiddaman 	diskaddr_t p_lba = (diskaddr_t)(uintptr_t)bp->b_private;
139787117650Saalok 	int error;
139844a1e32bSbatschul 	int syncflag = 0;
139987117650Saalok 	struct lofi_state *lsp;
14007d82f0f8SDina K Nimeh 	offset_t offset;
140187117650Saalok 	caddr_t	bufaddr;
14027d82f0f8SDina K Nimeh 	size_t	len;
14037d82f0f8SDina K Nimeh 	size_t	xfersize;
14047d82f0f8SDina K Nimeh 	boolean_t bufinited = B_FALSE;
140587117650Saalok 
1406406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep,
1407406fc510SToomas Soome 	    LOFI_MINOR2ID(getminor(bp->b_edev)));
1408406fc510SToomas Soome 
14097d82f0f8SDina K Nimeh 	if (lsp == NULL) {
14107d82f0f8SDina K Nimeh 		error = ENXIO;
14117d82f0f8SDina K Nimeh 		goto errout;
14127d82f0f8SDina K Nimeh 	}
141387117650Saalok 	if (lsp->ls_kstat) {
141487117650Saalok 		mutex_enter(lsp->ls_kstat->ks_lock);
141587117650Saalok 		kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
141687117650Saalok 		mutex_exit(lsp->ls_kstat->ks_lock);
141787117650Saalok 	}
1418406fc510SToomas Soome 
1419406fc510SToomas Soome 	mutex_enter(&lsp->ls_vp_lock);
1420406fc510SToomas Soome 	lsp->ls_vp_iocount++;
1421406fc510SToomas Soome 	mutex_exit(&lsp->ls_vp_lock);
1422406fc510SToomas Soome 
142387117650Saalok 	bp_mapin(bp);
142487117650Saalok 	bufaddr = bp->b_un.b_addr;
142503f27e8bSAndy Fiddaman 	/* offset within file */
142603f27e8bSAndy Fiddaman 	offset = (bp->b_lblkno + p_lba) << lsp->ls_lbshift;
14277d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
14287d82f0f8SDina K Nimeh 		/* encrypted data really begins after crypto header */
14297d82f0f8SDina K Nimeh 		offset += lsp->ls_crypto_offset;
14307d82f0f8SDina K Nimeh 	}
14317d82f0f8SDina K Nimeh 	len = bp->b_bcount;
14327d82f0f8SDina K Nimeh 	bufinited = B_TRUE;
14337d82f0f8SDina K Nimeh 
14347d82f0f8SDina K Nimeh 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
14357d82f0f8SDina K Nimeh 		error = EIO;
14367d82f0f8SDina K Nimeh 		goto errout;
14377d82f0f8SDina K Nimeh 	}
143887117650Saalok 
143987117650Saalok 	/*
144044a1e32bSbatschul 	 * If we're writing and the buffer was not B_ASYNC
144144a1e32bSbatschul 	 * we'll follow up with a VOP_FSYNC() to force any
144244a1e32bSbatschul 	 * asynchronous I/O to stable storage.
144344a1e32bSbatschul 	 */
144444a1e32bSbatschul 	if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
144544a1e32bSbatschul 		syncflag = FSYNC;
144644a1e32bSbatschul 
144744a1e32bSbatschul 	/*
144887117650Saalok 	 * We used to always use vn_rdwr here, but we cannot do that because
144987117650Saalok 	 * we might decide to read or write from the the underlying
145087117650Saalok 	 * file during this call, which would be a deadlock because
145187117650Saalok 	 * we have the rw_lock. So instead we page, unless it's not
14527d82f0f8SDina K Nimeh 	 * mapable or it's a character device or it's an encrypted lofi.
145387117650Saalok 	 */
14547d82f0f8SDina K Nimeh 	if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
14557d82f0f8SDina K Nimeh 	    lsp->ls_crypto_enabled) {
14567d82f0f8SDina K Nimeh 		error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
14577d82f0f8SDina K Nimeh 		    NULL);
14587d82f0f8SDina K Nimeh 	} else if (lsp->ls_uncomp_seg_sz == 0) {
145987117650Saalok 		error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
14607d82f0f8SDina K Nimeh 	} else {
14614058a205Sjrgn.keil@googlemail.com 		uchar_t *compressed_seg = NULL, *cmpbuf;
14624058a205Sjrgn.keil@googlemail.com 		uchar_t *uncompressed_seg = NULL;
14637d82f0f8SDina K Nimeh 		lofi_compress_info_t *li;
14647d82f0f8SDina K Nimeh 		size_t oblkcount;
14654058a205Sjrgn.keil@googlemail.com 		ulong_t seglen;
14667d82f0f8SDina K Nimeh 		uint64_t sblkno, eblkno, cmpbytes;
14674058a205Sjrgn.keil@googlemail.com 		uint64_t uncompressed_seg_index;
14684058a205Sjrgn.keil@googlemail.com 		struct lofi_comp_cache *lc;
14697d82f0f8SDina K Nimeh 		offset_t sblkoff, eblkoff;
14707d82f0f8SDina K Nimeh 		u_offset_t salign, ealign;
14717d82f0f8SDina K Nimeh 		u_offset_t sdiff;
14727d82f0f8SDina K Nimeh 		uint32_t comp_data_sz;
14737d82f0f8SDina K Nimeh 		uint64_t i;
1474b9c7fb03SAlok Aggarwal 		int j;
147587117650Saalok 
147687117650Saalok 		/*
147787117650Saalok 		 * From here on we're dealing primarily with compressed files
147887117650Saalok 		 */
14797d82f0f8SDina K Nimeh 		ASSERT(!lsp->ls_crypto_enabled);
148087117650Saalok 
148187117650Saalok 		/*
148287117650Saalok 		 * Compressed files can only be read from and
148387117650Saalok 		 * not written to
148487117650Saalok 		 */
148587117650Saalok 		if (!(bp->b_flags & B_READ)) {
148687117650Saalok 			bp->b_resid = bp->b_bcount;
148787117650Saalok 			error = EROFS;
148887117650Saalok 			goto done;
148987117650Saalok 		}
149087117650Saalok 
149187117650Saalok 		ASSERT(lsp->ls_comp_algorithm_index >= 0);
149287117650Saalok 		li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
149387117650Saalok 		/*
149487117650Saalok 		 * Compute starting and ending compressed segment numbers
149587117650Saalok 		 * We use only bitwise operations avoiding division and
149687117650Saalok 		 * modulus because we enforce the compression segment size
149787117650Saalok 		 * to a power of 2
149887117650Saalok 		 */
149987117650Saalok 		sblkno = offset >> lsp->ls_comp_seg_shift;
150087117650Saalok 		sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
150187117650Saalok 		eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
150287117650Saalok 		eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
150387117650Saalok 
150487117650Saalok 		/*
15054058a205Sjrgn.keil@googlemail.com 		 * Check the decompressed segment cache.
15064058a205Sjrgn.keil@googlemail.com 		 *
15074058a205Sjrgn.keil@googlemail.com 		 * The cache is used only when the requested data
15084058a205Sjrgn.keil@googlemail.com 		 * is within a segment. Requests that cross
15094058a205Sjrgn.keil@googlemail.com 		 * segment boundaries bypass the cache.
15104058a205Sjrgn.keil@googlemail.com 		 */
15114058a205Sjrgn.keil@googlemail.com 		if (sblkno == eblkno ||
15124058a205Sjrgn.keil@googlemail.com 		    (sblkno + 1 == eblkno && eblkoff == 0)) {
15134058a205Sjrgn.keil@googlemail.com 			/*
15144058a205Sjrgn.keil@googlemail.com 			 * Request doesn't cross a segment boundary,
15154058a205Sjrgn.keil@googlemail.com 			 * now check the cache.
15164058a205Sjrgn.keil@googlemail.com 			 */
15174058a205Sjrgn.keil@googlemail.com 			mutex_enter(&lsp->ls_comp_cache_lock);
15184058a205Sjrgn.keil@googlemail.com 			lc = lofi_find_comp_data(lsp, sblkno);
15194058a205Sjrgn.keil@googlemail.com 			if (lc != NULL) {
15204058a205Sjrgn.keil@googlemail.com 				/*
15214058a205Sjrgn.keil@googlemail.com 				 * We've found the decompressed segment
15224058a205Sjrgn.keil@googlemail.com 				 * data in the cache; reuse it.
15234058a205Sjrgn.keil@googlemail.com 				 */
15244058a205Sjrgn.keil@googlemail.com 				bcopy(lc->lc_data + sblkoff, bufaddr,
15254058a205Sjrgn.keil@googlemail.com 				    bp->b_bcount);
15264058a205Sjrgn.keil@googlemail.com 				mutex_exit(&lsp->ls_comp_cache_lock);
15274058a205Sjrgn.keil@googlemail.com 				bp->b_resid = 0;
15284058a205Sjrgn.keil@googlemail.com 				error = 0;
15294058a205Sjrgn.keil@googlemail.com 				goto done;
15304058a205Sjrgn.keil@googlemail.com 			}
15314058a205Sjrgn.keil@googlemail.com 			mutex_exit(&lsp->ls_comp_cache_lock);
15324058a205Sjrgn.keil@googlemail.com 		}
15334058a205Sjrgn.keil@googlemail.com 
15344058a205Sjrgn.keil@googlemail.com 		/*
153587117650Saalok 		 * Align start offset to block boundary for segmap
153687117650Saalok 		 */
153787117650Saalok 		salign = lsp->ls_comp_seg_index[sblkno];
153887117650Saalok 		sdiff = salign & (DEV_BSIZE - 1);
153987117650Saalok 		salign -= sdiff;
154087117650Saalok 		if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
154187117650Saalok 			/*
154287117650Saalok 			 * We're dealing with the last segment of
154387117650Saalok 			 * the compressed file -- the size of this
154487117650Saalok 			 * segment *may not* be the same as the
154587117650Saalok 			 * segment size for the file
154687117650Saalok 			 */
154787117650Saalok 			eblkoff = (offset + bp->b_bcount) &
154887117650Saalok 			    (lsp->ls_uncomp_last_seg_sz - 1);
154987117650Saalok 			ealign = lsp->ls_vp_comp_size;
155087117650Saalok 		} else {
155187117650Saalok 			ealign = lsp->ls_comp_seg_index[eblkno + 1];
155287117650Saalok 		}
155387117650Saalok 
155487117650Saalok 		/*
155587117650Saalok 		 * Preserve original request paramaters
155687117650Saalok 		 */
155787117650Saalok 		oblkcount = bp->b_bcount;
155887117650Saalok 
155987117650Saalok 		/*
156087117650Saalok 		 * Assign the calculated parameters
156187117650Saalok 		 */
156287117650Saalok 		comp_data_sz = ealign - salign;
156387117650Saalok 		bp->b_bcount = comp_data_sz;
156487117650Saalok 
156587117650Saalok 		/*
1566b9c7fb03SAlok Aggarwal 		 * Buffers to hold compressed segments are pre-allocated
1567b9c7fb03SAlok Aggarwal 		 * on a per-thread basis. Find a pre-allocated buffer
1568b9c7fb03SAlok Aggarwal 		 * that is not currently in use and mark it for use.
156987117650Saalok 		 */
1570b9c7fb03SAlok Aggarwal 		mutex_enter(&lsp->ls_comp_bufs_lock);
1571b9c7fb03SAlok Aggarwal 		for (j = 0; j < lofi_taskq_nthreads; j++) {
1572b9c7fb03SAlok Aggarwal 			if (lsp->ls_comp_bufs[j].inuse == 0) {
1573b9c7fb03SAlok Aggarwal 				lsp->ls_comp_bufs[j].inuse = 1;
1574b9c7fb03SAlok Aggarwal 				break;
1575b9c7fb03SAlok Aggarwal 			}
1576b9c7fb03SAlok Aggarwal 		}
1577b9c7fb03SAlok Aggarwal 
1578b9c7fb03SAlok Aggarwal 		mutex_exit(&lsp->ls_comp_bufs_lock);
1579b9c7fb03SAlok Aggarwal 		ASSERT(j < lofi_taskq_nthreads);
1580b9c7fb03SAlok Aggarwal 
1581b9c7fb03SAlok Aggarwal 		/*
1582b9c7fb03SAlok Aggarwal 		 * If the pre-allocated buffer size does not match
1583b9c7fb03SAlok Aggarwal 		 * the size of the I/O request, re-allocate it with
1584b9c7fb03SAlok Aggarwal 		 * the appropriate size
1585b9c7fb03SAlok Aggarwal 		 */
1586b9c7fb03SAlok Aggarwal 		if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) {
1587b9c7fb03SAlok Aggarwal 			if (lsp->ls_comp_bufs[j].bufsize > 0)
1588b9c7fb03SAlok Aggarwal 				kmem_free(lsp->ls_comp_bufs[j].buf,
1589b9c7fb03SAlok Aggarwal 				    lsp->ls_comp_bufs[j].bufsize);
1590b9c7fb03SAlok Aggarwal 			lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount,
1591b9c7fb03SAlok Aggarwal 			    KM_SLEEP);
1592b9c7fb03SAlok Aggarwal 			lsp->ls_comp_bufs[j].bufsize = bp->b_bcount;
1593b9c7fb03SAlok Aggarwal 		}
1594b9c7fb03SAlok Aggarwal 		compressed_seg = lsp->ls_comp_bufs[j].buf;
1595b9c7fb03SAlok Aggarwal 
159687117650Saalok 		/*
159787117650Saalok 		 * Map in the calculated number of blocks
159887117650Saalok 		 */
159987117650Saalok 		error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
160087117650Saalok 		    bp, lsp);
160187117650Saalok 
160287117650Saalok 		bp->b_bcount = oblkcount;
160387117650Saalok 		bp->b_resid = oblkcount;
160487117650Saalok 		if (error != 0)
160587117650Saalok 			goto done;
160687117650Saalok 
160787117650Saalok 		/*
1608b9c7fb03SAlok Aggarwal 		 * decompress compressed blocks start
160987117650Saalok 		 */
161087117650Saalok 		cmpbuf = compressed_seg + sdiff;
1611b1efbcd6SAlok Aggarwal 		for (i = sblkno; i <= eblkno; i++) {
1612b1efbcd6SAlok Aggarwal 			ASSERT(i < lsp->ls_comp_index_sz - 1);
1613b9c7fb03SAlok Aggarwal 			uchar_t *useg;
1614b1efbcd6SAlok Aggarwal 
1615b1efbcd6SAlok Aggarwal 			/*
1616b1efbcd6SAlok Aggarwal 			 * The last segment is special in that it is
1617b1efbcd6SAlok Aggarwal 			 * most likely not going to be the same
1618b1efbcd6SAlok Aggarwal 			 * (uncompressed) size as the other segments.
1619b1efbcd6SAlok Aggarwal 			 */
1620b1efbcd6SAlok Aggarwal 			if (i == (lsp->ls_comp_index_sz - 2)) {
1621b1efbcd6SAlok Aggarwal 				seglen = lsp->ls_uncomp_last_seg_sz;
1622b1efbcd6SAlok Aggarwal 			} else {
1623b1efbcd6SAlok Aggarwal 				seglen = lsp->ls_uncomp_seg_sz;
1624b1efbcd6SAlok Aggarwal 			}
1625b1efbcd6SAlok Aggarwal 
162687117650Saalok 			/*
162787117650Saalok 			 * Each of the segment index entries contains
162887117650Saalok 			 * the starting block number for that segment.
162987117650Saalok 			 * The number of compressed bytes in a segment
163087117650Saalok 			 * is thus the difference between the starting
163187117650Saalok 			 * block number of this segment and the starting
163287117650Saalok 			 * block number of the next segment.
163387117650Saalok 			 */
163487117650Saalok 			cmpbytes = lsp->ls_comp_seg_index[i + 1] -
163587117650Saalok 			    lsp->ls_comp_seg_index[i];
163687117650Saalok 
163787117650Saalok 			/*
163887117650Saalok 			 * The first byte in a compressed segment is a flag
163987117650Saalok 			 * that indicates whether this segment is compressed
1640b9c7fb03SAlok Aggarwal 			 * at all.
1641b9c7fb03SAlok Aggarwal 			 *
1642b9c7fb03SAlok Aggarwal 			 * The variable 'useg' is used (instead of
1643b9c7fb03SAlok Aggarwal 			 * uncompressed_seg) in this loop to keep a
1644b9c7fb03SAlok Aggarwal 			 * reference to the uncompressed segment.
1645b9c7fb03SAlok Aggarwal 			 *
1646b9c7fb03SAlok Aggarwal 			 * N.B. If 'useg' is replaced with uncompressed_seg,
1647b9c7fb03SAlok Aggarwal 			 * it leads to memory leaks and heap corruption in
1648b9c7fb03SAlok Aggarwal 			 * corner cases where compressed segments lie
1649b9c7fb03SAlok Aggarwal 			 * adjacent to uncompressed segments.
165087117650Saalok 			 */
165187117650Saalok 			if (*cmpbuf == UNCOMPRESSED) {
1652b9c7fb03SAlok Aggarwal 				useg = cmpbuf + SEGHDR;
165387117650Saalok 			} else {
1654b9c7fb03SAlok Aggarwal 				if (uncompressed_seg == NULL)
1655b9c7fb03SAlok Aggarwal 					uncompressed_seg =
1656b9c7fb03SAlok Aggarwal 					    kmem_alloc(lsp->ls_uncomp_seg_sz,
1657b9c7fb03SAlok Aggarwal 					    KM_SLEEP);
1658b9c7fb03SAlok Aggarwal 				useg = uncompressed_seg;
1659b9c7fb03SAlok Aggarwal 				uncompressed_seg_index = i;
1660b9c7fb03SAlok Aggarwal 
166187117650Saalok 				if (li->l_decompress((cmpbuf + SEGHDR),
166287117650Saalok 				    (cmpbytes - SEGHDR), uncompressed_seg,
166387117650Saalok 				    &seglen, li->l_level) != 0) {
166487117650Saalok 					error = EIO;
166587117650Saalok 					goto done;
166687117650Saalok 				}
166787117650Saalok 			}
166887117650Saalok 
166987117650Saalok 			/*
167087117650Saalok 			 * Determine how much uncompressed data we
167187117650Saalok 			 * have to copy and copy it
167287117650Saalok 			 */
167387117650Saalok 			xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
1674b1efbcd6SAlok Aggarwal 			if (i == eblkno)
1675b1efbcd6SAlok Aggarwal 				xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
167687117650Saalok 
1677b9c7fb03SAlok Aggarwal 			bcopy((useg + sblkoff), bufaddr, xfersize);
167887117650Saalok 
167987117650Saalok 			cmpbuf += cmpbytes;
168087117650Saalok 			bufaddr += xfersize;
168187117650Saalok 			bp->b_resid -= xfersize;
168287117650Saalok 			sblkoff = 0;
168387117650Saalok 
168487117650Saalok 			if (bp->b_resid == 0)
168587117650Saalok 				break;
1686b9c7fb03SAlok Aggarwal 		} /* decompress compressed blocks ends */
16874058a205Sjrgn.keil@googlemail.com 
16884058a205Sjrgn.keil@googlemail.com 		/*
1689b9c7fb03SAlok Aggarwal 		 * Skip to done if there is no uncompressed data to cache
1690b9c7fb03SAlok Aggarwal 		 */
1691b9c7fb03SAlok Aggarwal 		if (uncompressed_seg == NULL)
1692b9c7fb03SAlok Aggarwal 			goto done;
1693b9c7fb03SAlok Aggarwal 
1694b9c7fb03SAlok Aggarwal 		/*
1695b9c7fb03SAlok Aggarwal 		 * Add the data for the last decompressed segment to
16964058a205Sjrgn.keil@googlemail.com 		 * the cache.
16974058a205Sjrgn.keil@googlemail.com 		 *
16984058a205Sjrgn.keil@googlemail.com 		 * In case the uncompressed segment data was added to (and
16994058a205Sjrgn.keil@googlemail.com 		 * is referenced by) the cache, make sure we don't free it
17004058a205Sjrgn.keil@googlemail.com 		 * here.
17014058a205Sjrgn.keil@googlemail.com 		 */
17024058a205Sjrgn.keil@googlemail.com 		mutex_enter(&lsp->ls_comp_cache_lock);
17034058a205Sjrgn.keil@googlemail.com 		if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
17044058a205Sjrgn.keil@googlemail.com 		    uncompressed_seg)) != NULL) {
17054058a205Sjrgn.keil@googlemail.com 			uncompressed_seg = NULL;
17064058a205Sjrgn.keil@googlemail.com 		}
17074058a205Sjrgn.keil@googlemail.com 		mutex_exit(&lsp->ls_comp_cache_lock);
17084058a205Sjrgn.keil@googlemail.com 
170987117650Saalok done:
1710b9c7fb03SAlok Aggarwal 		if (compressed_seg != NULL) {
1711b9c7fb03SAlok Aggarwal 			mutex_enter(&lsp->ls_comp_bufs_lock);
1712b9c7fb03SAlok Aggarwal 			lsp->ls_comp_bufs[j].inuse = 0;
1713b9c7fb03SAlok Aggarwal 			mutex_exit(&lsp->ls_comp_bufs_lock);
1714b9c7fb03SAlok Aggarwal 		}
171587117650Saalok 		if (uncompressed_seg != NULL)
171687117650Saalok 			kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
17177d82f0f8SDina K Nimeh 	} /* end of handling compressed files */
171887117650Saalok 
171944a1e32bSbatschul 	if ((error == 0) && (syncflag != 0))
172044a1e32bSbatschul 		error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
172144a1e32bSbatschul 
17227d82f0f8SDina K Nimeh errout:
17237d82f0f8SDina K Nimeh 	if (bufinited && lsp->ls_kstat) {
17247c478bd9Sstevel@tonic-gate 		size_t n_done = bp->b_bcount - bp->b_resid;
17257c478bd9Sstevel@tonic-gate 		kstat_io_t *kioptr;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
17287c478bd9Sstevel@tonic-gate 		kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
17297c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
17307c478bd9Sstevel@tonic-gate 			kioptr->nread += n_done;
17317c478bd9Sstevel@tonic-gate 			kioptr->reads++;
17327c478bd9Sstevel@tonic-gate 		} else {
17337c478bd9Sstevel@tonic-gate 			kioptr->nwritten += n_done;
17347c478bd9Sstevel@tonic-gate 			kioptr->writes++;
17357c478bd9Sstevel@tonic-gate 		}
17367c478bd9Sstevel@tonic-gate 		kstat_runq_exit(kioptr);
17377c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
17387c478bd9Sstevel@tonic-gate 	}
17393d7072f8Seschrock 
17403d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
17413d7072f8Seschrock 	if (--lsp->ls_vp_iocount == 0)
17423d7072f8Seschrock 		cv_broadcast(&lsp->ls_vp_cv);
17433d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
17443d7072f8Seschrock 
17457c478bd9Sstevel@tonic-gate 	bioerror(bp, error);
17467c478bd9Sstevel@tonic-gate 	biodone(bp);
17477c478bd9Sstevel@tonic-gate }
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate static int
lofi_strategy_backend(struct buf * bp,task_func_t taskfunc)175003f27e8bSAndy Fiddaman lofi_strategy_backend(struct buf *bp, task_func_t taskfunc)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
17537c478bd9Sstevel@tonic-gate 	offset_t	offset;
1754406fc510SToomas Soome 	minor_t		part;
1755406fc510SToomas Soome 	diskaddr_t	p_lba;
1756406fc510SToomas Soome 	diskaddr_t	p_nblks;
1757406fc510SToomas Soome 	int		shift;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	/*
17607c478bd9Sstevel@tonic-gate 	 * We cannot just do I/O here, because the current thread
17617c478bd9Sstevel@tonic-gate 	 * _might_ end up back in here because the underlying filesystem
17627c478bd9Sstevel@tonic-gate 	 * wants a buffer, which eventually gets into bio_recycle and
17637c478bd9Sstevel@tonic-gate 	 * might call into lofi to write out a delayed-write buffer.
17647c478bd9Sstevel@tonic-gate 	 * This is bad if the filesystem above lofi is the same as below.
17657c478bd9Sstevel@tonic-gate 	 *
17667c478bd9Sstevel@tonic-gate 	 * We could come up with a complex strategy using threads to
17677c478bd9Sstevel@tonic-gate 	 * do the I/O asynchronously, or we could use task queues. task
17687c478bd9Sstevel@tonic-gate 	 * queues were incredibly easy so they win.
17697c478bd9Sstevel@tonic-gate 	 */
1770406fc510SToomas Soome 
1771406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep,
1772406fc510SToomas Soome 	    LOFI_MINOR2ID(getminor(bp->b_edev)));
1773406fc510SToomas Soome 	part = LOFI_PART(getminor(bp->b_edev));
1774406fc510SToomas Soome 
17757d82f0f8SDina K Nimeh 	if (lsp == NULL) {
17767d82f0f8SDina K Nimeh 		bioerror(bp, ENXIO);
17777d82f0f8SDina K Nimeh 		biodone(bp);
17787d82f0f8SDina K Nimeh 		return (0);
17797d82f0f8SDina K Nimeh 	}
1780406fc510SToomas Soome 
1781b4844717SToomas Soome 	/* Check if we are closing. */
1782b4844717SToomas Soome 	mutex_enter(&lsp->ls_vp_lock);
1783b4844717SToomas Soome 	if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
1784b4844717SToomas Soome 		mutex_exit(&lsp->ls_vp_lock);
1785b4844717SToomas Soome 		bioerror(bp, EIO);
1786b4844717SToomas Soome 		biodone(bp);
1787b4844717SToomas Soome 		return (0);
1788b4844717SToomas Soome 	}
1789b4844717SToomas Soome 	mutex_exit(&lsp->ls_vp_lock);
1790b4844717SToomas Soome 
1791b4844717SToomas Soome 	shift = lsp->ls_lbshift;
1792406fc510SToomas Soome 	p_lba = 0;
1793406fc510SToomas Soome 	p_nblks = lsp->ls_vp_size >> shift;
1794406fc510SToomas Soome 
1795406fc510SToomas Soome 	if (lsp->ls_cmlbhandle != NULL) {
1796406fc510SToomas Soome 		if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &p_nblks, &p_lba,
1797406fc510SToomas Soome 		    NULL, NULL, 0)) {
1798406fc510SToomas Soome 			bioerror(bp, ENXIO);
1799406fc510SToomas Soome 			biodone(bp);
1800406fc510SToomas Soome 			return (0);
1801406fc510SToomas Soome 		}
1802406fc510SToomas Soome 	}
1803406fc510SToomas Soome 
1804406fc510SToomas Soome 	/* start block past partition end? */
1805406fc510SToomas Soome 	if (bp->b_lblkno > p_nblks) {
1806406fc510SToomas Soome 		bioerror(bp, ENXIO);
1807406fc510SToomas Soome 		biodone(bp);
1808406fc510SToomas Soome 		return (0);
1809406fc510SToomas Soome 	}
1810406fc510SToomas Soome 
1811406fc510SToomas Soome 	offset = (bp->b_lblkno + p_lba) << shift; /* offset within file */
18127d82f0f8SDina K Nimeh 
18133d7072f8Seschrock 	mutex_enter(&lsp->ls_vp_lock);
18147d82f0f8SDina K Nimeh 	if (lsp->ls_crypto_enabled) {
18157d82f0f8SDina K Nimeh 		/* encrypted data really begins after crypto header */
18167d82f0f8SDina K Nimeh 		offset += lsp->ls_crypto_offset;
18177d82f0f8SDina K Nimeh 	}
1818406fc510SToomas Soome 
1819406fc510SToomas Soome 	/* make sure we will not pass the file or partition size */
1820406fc510SToomas Soome 	if (offset == lsp->ls_vp_size ||
1821406fc510SToomas Soome 	    offset == (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) {
18227c478bd9Sstevel@tonic-gate 		/* EOF */
18237c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) != 0) {
18247c478bd9Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
18257c478bd9Sstevel@tonic-gate 			bioerror(bp, 0);
18267c478bd9Sstevel@tonic-gate 		} else {
18277c478bd9Sstevel@tonic-gate 			/* writes should fail */
18287c478bd9Sstevel@tonic-gate 			bioerror(bp, ENXIO);
18297c478bd9Sstevel@tonic-gate 		}
18307c478bd9Sstevel@tonic-gate 		biodone(bp);
18313d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
18327c478bd9Sstevel@tonic-gate 		return (0);
18337c478bd9Sstevel@tonic-gate 	}
1834406fc510SToomas Soome 	if ((offset > lsp->ls_vp_size) ||
1835406fc510SToomas Soome 	    (offset > (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) ||
1836406fc510SToomas Soome 	    ((offset + bp->b_bcount) > ((p_lba + p_nblks) << shift))) {
18377c478bd9Sstevel@tonic-gate 		bioerror(bp, ENXIO);
18387c478bd9Sstevel@tonic-gate 		biodone(bp);
18393d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
18407c478bd9Sstevel@tonic-gate 		return (0);
18417c478bd9Sstevel@tonic-gate 	}
1842406fc510SToomas Soome 
18433d7072f8Seschrock 	mutex_exit(&lsp->ls_vp_lock);
18443d7072f8Seschrock 
18457c478bd9Sstevel@tonic-gate 	if (lsp->ls_kstat) {
18467c478bd9Sstevel@tonic-gate 		mutex_enter(lsp->ls_kstat->ks_lock);
18477c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
18487c478bd9Sstevel@tonic-gate 		mutex_exit(lsp->ls_kstat->ks_lock);
18497c478bd9Sstevel@tonic-gate 	}
1850406fc510SToomas Soome 	bp->b_private = (void *)(uintptr_t)p_lba;	/* partition start */
185103f27e8bSAndy Fiddaman 	(void) taskq_dispatch(lsp->ls_taskq, taskfunc, bp, KM_SLEEP);
18527c478bd9Sstevel@tonic-gate 	return (0);
18537c478bd9Sstevel@tonic-gate }
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate static int
lofi_strategy(struct buf * bp)185603f27e8bSAndy Fiddaman lofi_strategy(struct buf *bp)
18577c478bd9Sstevel@tonic-gate {
185803f27e8bSAndy Fiddaman 	return (lofi_strategy_backend(bp, lofi_strategy_task));
185903f27e8bSAndy Fiddaman }
1860935ca2ecSJohn Levon 
186103f27e8bSAndy Fiddaman static int
lofi_read(dev_t dev,struct uio * uio,struct cred * credp __unused)186203f27e8bSAndy Fiddaman lofi_read(dev_t dev, struct uio *uio, struct cred *credp __unused)
186303f27e8bSAndy Fiddaman {
18647c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
18657c478bd9Sstevel@tonic-gate 		return (EINVAL);
18667d82f0f8SDina K Nimeh 	UIO_CHECK(uio);
18677c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static int
lofi_write(dev_t dev,struct uio * uio,struct cred * credp __unused)187103f27e8bSAndy Fiddaman lofi_write(dev_t dev, struct uio *uio, struct cred *credp __unused)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
18747c478bd9Sstevel@tonic-gate 		return (EINVAL);
18757d82f0f8SDina K Nimeh 	UIO_CHECK(uio);
18767c478bd9Sstevel@tonic-gate 	return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
18777c478bd9Sstevel@tonic-gate }
18787c478bd9Sstevel@tonic-gate 
1879935ca2ecSJohn Levon static int
lofi_urw(struct lofi_state * lsp,uint16_t fmode,diskaddr_t off,size_t size,intptr_t arg,int flag,cred_t * credp)1880935ca2ecSJohn Levon lofi_urw(struct lofi_state *lsp, uint16_t fmode, diskaddr_t off, size_t size,
1881935ca2ecSJohn Levon     intptr_t arg, int flag, cred_t *credp)
1882935ca2ecSJohn Levon {
1883935ca2ecSJohn Levon 	struct uio uio;
1884935ca2ecSJohn Levon 	iovec_t iov;
1885935ca2ecSJohn Levon 
1886935ca2ecSJohn Levon 	/*
1887935ca2ecSJohn Levon 	 * 1024 * 1024 apes cmlb_tg_max_efi_xfer as a reasonable max.
1888935ca2ecSJohn Levon 	 */
1889935ca2ecSJohn Levon 	if (size == 0 || size > 1024 * 1024 ||
1890935ca2ecSJohn Levon 	    (size % (1 << lsp->ls_lbshift)) != 0)
1891935ca2ecSJohn Levon 		return (EINVAL);
1892935ca2ecSJohn Levon 
1893935ca2ecSJohn Levon 	iov.iov_base = (void *)arg;
1894935ca2ecSJohn Levon 	iov.iov_len = size;
1895935ca2ecSJohn Levon 	uio.uio_iov = &iov;
1896935ca2ecSJohn Levon 	uio.uio_iovcnt = 1;
1897935ca2ecSJohn Levon 	uio.uio_loffset = off;
1898935ca2ecSJohn Levon 	uio.uio_segflg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE;
1899935ca2ecSJohn Levon 	uio.uio_llimit = MAXOFFSET_T;
1900935ca2ecSJohn Levon 	uio.uio_resid = size;
1901935ca2ecSJohn Levon 	uio.uio_fmode = fmode;
1902935ca2ecSJohn Levon 	uio.uio_extflg = 0;
1903935ca2ecSJohn Levon 
1904935ca2ecSJohn Levon 	return (fmode == FREAD ?
1905935ca2ecSJohn Levon 	    lofi_read(lsp->ls_dev, &uio, credp) :
1906935ca2ecSJohn Levon 	    lofi_write(lsp->ls_dev, &uio, credp));
1907935ca2ecSJohn Levon }
1908935ca2ecSJohn Levon 
190903f27e8bSAndy Fiddaman typedef struct {
191003f27e8bSAndy Fiddaman 	struct lofi_state *lcd_lsp;
191103f27e8bSAndy Fiddaman 	dev_t lcd_dev;
191203f27e8bSAndy Fiddaman } lofi_cb_data_t;
191303f27e8bSAndy Fiddaman 
19147c478bd9Sstevel@tonic-gate static int
lofi_free_space_cb(dkioc_free_list_t * dfl,void * arg,int kmflag __unused)191503f27e8bSAndy Fiddaman lofi_free_space_cb(dkioc_free_list_t *dfl, void *arg, int kmflag __unused)
191603f27e8bSAndy Fiddaman {
191703f27e8bSAndy Fiddaman 	dkioc_free_list_ext_t *ext;
191803f27e8bSAndy Fiddaman 	lofi_cb_data_t *cbd = arg;
191903f27e8bSAndy Fiddaman 	struct lofi_state *lsp = cbd->lcd_lsp;
192003f27e8bSAndy Fiddaman 	buf_t *bp = NULL;
192103f27e8bSAndy Fiddaman 	int error = 0;
192203f27e8bSAndy Fiddaman 
192303f27e8bSAndy Fiddaman 	bp = getrbuf(KM_SLEEP);
192403f27e8bSAndy Fiddaman 
192503f27e8bSAndy Fiddaman 	ext = dfl->dfl_exts;
192603f27e8bSAndy Fiddaman 	for (uint_t i = 0; i < dfl->dfl_num_exts; i++, ext++) {
192703f27e8bSAndy Fiddaman 		uint64_t start = dfl->dfl_offset + ext->dfle_start;
192803f27e8bSAndy Fiddaman 		uint64_t length = ext->dfle_length;
192903f27e8bSAndy Fiddaman 
193003f27e8bSAndy Fiddaman 		bp->b_edev = cbd->lcd_dev;
193103f27e8bSAndy Fiddaman 		bp->b_flags = B_WRITE;
193203f27e8bSAndy Fiddaman 		bp->b_un.b_addr = NULL;
193303f27e8bSAndy Fiddaman 		bp->b_resid = 0;
193403f27e8bSAndy Fiddaman 		bp->b_lblkno = start >> lsp->ls_lbshift;
193503f27e8bSAndy Fiddaman 		bp->b_bcount = length;
193603f27e8bSAndy Fiddaman 
193703f27e8bSAndy Fiddaman 		DTRACE_PROBE2(trim__issued, uint64_t, start, uint64_t, length);
193803f27e8bSAndy Fiddaman 
193903f27e8bSAndy Fiddaman 		error = lofi_strategy_backend(bp, lofi_trim_task);
194003f27e8bSAndy Fiddaman 		if (error != 0)
194103f27e8bSAndy Fiddaman 			break;
194203f27e8bSAndy Fiddaman 		(void) biowait(bp);
194303f27e8bSAndy Fiddaman 	}
194403f27e8bSAndy Fiddaman 
194503f27e8bSAndy Fiddaman 	freerbuf(bp);
194603f27e8bSAndy Fiddaman 	dfl_free(dfl);
194703f27e8bSAndy Fiddaman 	return (error);
194803f27e8bSAndy Fiddaman }
194903f27e8bSAndy Fiddaman 
195003f27e8bSAndy Fiddaman static int
lofi_free_space(struct lofi_state * lsp,dkioc_free_list_t * dfl,dev_t dev)195103f27e8bSAndy Fiddaman lofi_free_space(struct lofi_state *lsp, dkioc_free_list_t *dfl, dev_t dev)
195203f27e8bSAndy Fiddaman {
195303f27e8bSAndy Fiddaman 	dkioc_free_info_t dfi = {
195403f27e8bSAndy Fiddaman 		.dfi_bshift = lsp->ls_lbshift,
195503f27e8bSAndy Fiddaman 		.dfi_align = 1U << lsp->ls_lbshift,
195603f27e8bSAndy Fiddaman 		.dfi_max_bytes = 0,
195703f27e8bSAndy Fiddaman 		.dfi_max_ext = 0,
195803f27e8bSAndy Fiddaman 		.dfi_max_ext_bytes = 0
195903f27e8bSAndy Fiddaman 	};
196003f27e8bSAndy Fiddaman 
196103f27e8bSAndy Fiddaman 	lofi_cb_data_t cbd = {
196203f27e8bSAndy Fiddaman 		.lcd_lsp = lsp,
196303f27e8bSAndy Fiddaman 		.lcd_dev = dev
196403f27e8bSAndy Fiddaman 	};
196503f27e8bSAndy Fiddaman 
196603f27e8bSAndy Fiddaman 	return (dfl_iter(dfl, &dfi, lsp->ls_vp_size, lofi_free_space_cb,
196703f27e8bSAndy Fiddaman 	    &cbd, KM_SLEEP));
196803f27e8bSAndy Fiddaman }
196903f27e8bSAndy Fiddaman 
197003f27e8bSAndy Fiddaman static int
lofi_aread(dev_t dev,struct aio_req * aio,struct cred * credp __unused)197103f27e8bSAndy Fiddaman lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp __unused)
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
19747c478bd9Sstevel@tonic-gate 		return (EINVAL);
19757d82f0f8SDina K Nimeh 	UIO_CHECK(aio->aio_uio);
19767c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate static int
lofi_awrite(dev_t dev,struct aio_req * aio,struct cred * credp __unused)198003f27e8bSAndy Fiddaman lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp __unused)
19817c478bd9Sstevel@tonic-gate {
19827c478bd9Sstevel@tonic-gate 	if (getminor(dev) == 0)
19837c478bd9Sstevel@tonic-gate 		return (EINVAL);
19847d82f0f8SDina K Nimeh 	UIO_CHECK(aio->aio_uio);
19857c478bd9Sstevel@tonic-gate 	return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
19867c478bd9Sstevel@tonic-gate }
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate static int
lofi_info(dev_info_t * dip __unused,ddi_info_cmd_t infocmd,void * arg,void ** result)198903f27e8bSAndy Fiddaman lofi_info(dev_info_t *dip __unused, ddi_info_cmd_t infocmd, void *arg,
199003f27e8bSAndy Fiddaman     void **result)
19917c478bd9Sstevel@tonic-gate {
1992406fc510SToomas Soome 	struct lofi_state *lsp;
1993406fc510SToomas Soome 	dev_t	dev = (dev_t)arg;
1994406fc510SToomas Soome 	int instance;
1995406fc510SToomas Soome 
1996406fc510SToomas Soome 	instance = LOFI_MINOR2ID(getminor(dev));
19977c478bd9Sstevel@tonic-gate 	switch (infocmd) {
19987c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1999406fc510SToomas Soome 		lsp = ddi_get_soft_state(lofi_statep, instance);
2000406fc510SToomas Soome 		if (lsp == NULL)
2001406fc510SToomas Soome 			return (DDI_FAILURE);
2002406fc510SToomas Soome 		*result = lsp->ls_dip;
20037c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
20047c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2005406fc510SToomas Soome 		*result = (void *) (intptr_t)instance;
20067c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate static int
lofi_create_minor_nodes(struct lofi_state * lsp,boolean_t labeled)2012406fc510SToomas Soome lofi_create_minor_nodes(struct lofi_state *lsp, boolean_t labeled)
2013406fc510SToomas Soome {
2014406fc510SToomas Soome 	int error = 0;
2015406fc510SToomas Soome 	int instance = ddi_get_instance(lsp->ls_dip);
2016406fc510SToomas Soome 
2017406fc510SToomas Soome 	if (labeled == B_TRUE) {
2018406fc510SToomas Soome 		cmlb_alloc_handle(&lsp->ls_cmlbhandle);
2019406fc510SToomas Soome 		error = cmlb_attach(lsp->ls_dip, &lofi_tg_ops, DTYPE_DIRECT,
2020406fc510SToomas Soome 		    B_FALSE, B_FALSE, DDI_NT_BLOCK_CHAN,
2021406fc510SToomas Soome 		    CMLB_CREATE_P0_MINOR_NODE, lsp->ls_cmlbhandle, (void *)1);
2022406fc510SToomas Soome 
2023406fc510SToomas Soome 		if (error != DDI_SUCCESS) {
2024406fc510SToomas Soome 			cmlb_free_handle(&lsp->ls_cmlbhandle);
2025406fc510SToomas Soome 			lsp->ls_cmlbhandle = NULL;
2026406fc510SToomas Soome 			error = ENXIO;
2027406fc510SToomas Soome 		}
2028406fc510SToomas Soome 	} else {
2029406fc510SToomas Soome 		/* create minor nodes */
2030406fc510SToomas Soome 		error = ddi_create_minor_node(lsp->ls_dip, LOFI_BLOCK_NODE,
2031406fc510SToomas Soome 		    S_IFBLK, LOFI_ID2MINOR(instance), DDI_PSEUDO, 0);
2032406fc510SToomas Soome 		if (error == DDI_SUCCESS) {
2033406fc510SToomas Soome 			error = ddi_create_minor_node(lsp->ls_dip,
2034406fc510SToomas Soome 			    LOFI_CHAR_NODE, S_IFCHR, LOFI_ID2MINOR(instance),
2035406fc510SToomas Soome 			    DDI_PSEUDO, 0);
2036406fc510SToomas Soome 			if (error != DDI_SUCCESS) {
2037406fc510SToomas Soome 				ddi_remove_minor_node(lsp->ls_dip,
2038406fc510SToomas Soome 				    LOFI_BLOCK_NODE);
2039406fc510SToomas Soome 				error = ENXIO;
2040406fc510SToomas Soome 			}
2041406fc510SToomas Soome 		} else
2042406fc510SToomas Soome 			error = ENXIO;
2043406fc510SToomas Soome 	}
2044406fc510SToomas Soome 	return (error);
2045406fc510SToomas Soome }
2046406fc510SToomas Soome 
2047406fc510SToomas Soome static int
lofi_zone_bind(struct lofi_state * lsp)2048406fc510SToomas Soome lofi_zone_bind(struct lofi_state *lsp)
2049406fc510SToomas Soome {
2050406fc510SToomas Soome 	int error = 0;
2051406fc510SToomas Soome 
2052406fc510SToomas Soome 	mutex_enter(&curproc->p_lock);
2053406fc510SToomas Soome 	if ((error = rctl_incr_lofi(curproc, curproc->p_zone, 1)) != 0) {
2054406fc510SToomas Soome 		mutex_exit(&curproc->p_lock);
2055406fc510SToomas Soome 		return (error);
2056406fc510SToomas Soome 	}
2057406fc510SToomas Soome 	mutex_exit(&curproc->p_lock);
2058406fc510SToomas Soome 
2059450b24a3SToomas Soome 	if (ddi_prop_update_string(DDI_DEV_T_NONE, lsp->ls_dip, ZONE_PROP_NAME,
2060406fc510SToomas Soome 	    (char *)curproc->p_zone->zone_name) != DDI_PROP_SUCCESS) {
2061406fc510SToomas Soome 		rctl_decr_lofi(curproc->p_zone, 1);
2062406fc510SToomas Soome 		error = EINVAL;
2063406fc510SToomas Soome 	} else {
2064406fc510SToomas Soome 		zone_init_ref(&lsp->ls_zone);
2065406fc510SToomas Soome 		zone_hold_ref(curzone, &lsp->ls_zone, ZONE_REF_LOFI);
2066406fc510SToomas Soome 	}
2067406fc510SToomas Soome 	return (error);
2068406fc510SToomas Soome }
2069406fc510SToomas Soome 
2070406fc510SToomas Soome static void
lofi_zone_unbind(struct lofi_state * lsp)2071406fc510SToomas Soome lofi_zone_unbind(struct lofi_state *lsp)
2072406fc510SToomas Soome {
2073406fc510SToomas Soome 	(void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip, ZONE_PROP_NAME);
2074406fc510SToomas Soome 	rctl_decr_lofi(curproc->p_zone, 1);
2075406fc510SToomas Soome 	zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
2076406fc510SToomas Soome }
2077406fc510SToomas Soome 
2078406fc510SToomas Soome static int
lofi_online_dev(dev_info_t * dip)2079406fc510SToomas Soome lofi_online_dev(dev_info_t *dip)
2080406fc510SToomas Soome {
2081406fc510SToomas Soome 	boolean_t labeled;
2082406fc510SToomas Soome 	int	error;
2083406fc510SToomas Soome 	int	instance = ddi_get_instance(dip);
2084406fc510SToomas Soome 	struct lofi_state *lsp;
2085406fc510SToomas Soome 
2086406fc510SToomas Soome 	labeled = B_FALSE;
2087406fc510SToomas Soome 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "labeled"))
2088406fc510SToomas Soome 		labeled = B_TRUE;
2089406fc510SToomas Soome 
2090406fc510SToomas Soome 	/* lsp alloc+init, soft state is freed in lofi_detach */
2091406fc510SToomas Soome 	error = ddi_soft_state_zalloc(lofi_statep, instance);
2092406fc510SToomas Soome 	if (error == DDI_FAILURE) {
2093406fc510SToomas Soome 		return (ENOMEM);
2094406fc510SToomas Soome 	}
2095406fc510SToomas Soome 
2096406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, instance);
2097406fc510SToomas Soome 	lsp->ls_dip = dip;
2098406fc510SToomas Soome 
2099406fc510SToomas Soome 	if ((error = lofi_zone_bind(lsp)) != 0)
2100406fc510SToomas Soome 		goto err;
2101406fc510SToomas Soome 
2102406fc510SToomas Soome 	cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
2103406fc510SToomas Soome 	mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
2104406fc510SToomas Soome 	mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL);
2105406fc510SToomas Soome 	mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
2106406fc510SToomas Soome 	mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
2107406fc510SToomas Soome 
2108406fc510SToomas Soome 	if ((error = lofi_create_minor_nodes(lsp, labeled)) != 0) {
2109406fc510SToomas Soome 		lofi_zone_unbind(lsp);
2110406fc510SToomas Soome 		goto lerr;
2111406fc510SToomas Soome 	}
2112406fc510SToomas Soome 
2113406fc510SToomas Soome 	/* driver handles kernel-issued IOCTLs */
2114406fc510SToomas Soome 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
2115406fc510SToomas Soome 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
2116406fc510SToomas Soome 		error = DDI_FAILURE;
2117406fc510SToomas Soome 		goto merr;
2118406fc510SToomas Soome 	}
2119406fc510SToomas Soome 
2120406fc510SToomas Soome 	lsp->ls_kstat = kstat_create_zone(LOFI_DRIVER_NAME, instance,
2121406fc510SToomas Soome 	    NULL, "disk", KSTAT_TYPE_IO, 1, 0, getzoneid());
2122406fc510SToomas Soome 	if (lsp->ls_kstat == NULL) {
2123406fc510SToomas Soome 		(void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip,
2124406fc510SToomas Soome 		    DDI_KERNEL_IOCTL);
2125406fc510SToomas Soome 		error = ENOMEM;
2126406fc510SToomas Soome 		goto merr;
2127406fc510SToomas Soome 	}
2128406fc510SToomas Soome 
2129406fc510SToomas Soome 	lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
2130406fc510SToomas Soome 	kstat_zone_add(lsp->ls_kstat, GLOBAL_ZONEID);
2131406fc510SToomas Soome 	kstat_install(lsp->ls_kstat);
2132406fc510SToomas Soome 	return (DDI_SUCCESS);
2133406fc510SToomas Soome merr:
2134406fc510SToomas Soome 	if (lsp->ls_cmlbhandle != NULL) {
2135406fc510SToomas Soome 		cmlb_detach(lsp->ls_cmlbhandle, 0);
2136406fc510SToomas Soome 		cmlb_free_handle(&lsp->ls_cmlbhandle);
2137406fc510SToomas Soome 	}
2138406fc510SToomas Soome 	ddi_remove_minor_node(dip, NULL);
2139406fc510SToomas Soome 	lofi_zone_unbind(lsp);
2140406fc510SToomas Soome lerr:
2141406fc510SToomas Soome 	mutex_destroy(&lsp->ls_comp_cache_lock);
2142406fc510SToomas Soome 	mutex_destroy(&lsp->ls_comp_bufs_lock);
2143406fc510SToomas Soome 	mutex_destroy(&lsp->ls_kstat_lock);
2144406fc510SToomas Soome 	mutex_destroy(&lsp->ls_vp_lock);
2145406fc510SToomas Soome 	cv_destroy(&lsp->ls_vp_cv);
2146406fc510SToomas Soome err:
2147406fc510SToomas Soome 	ddi_soft_state_free(lofi_statep, instance);
2148406fc510SToomas Soome 	return (error);
2149406fc510SToomas Soome }
2150406fc510SToomas Soome 
2151406fc510SToomas Soome static int
lofi_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)21527c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
21537c478bd9Sstevel@tonic-gate {
2154406fc510SToomas Soome 	int	rv;
2155406fc510SToomas Soome 	int	instance = ddi_get_instance(dip);
2156406fc510SToomas Soome 	struct lofi_state *lsp;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
21597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21600fbb751dSJohn Levon 
2161406fc510SToomas Soome 	/*
2162406fc510SToomas Soome 	 * Instance 0 is control instance, attaching control instance
2163406fc510SToomas Soome 	 * will set the lofi up and ready.
2164406fc510SToomas Soome 	 */
2165406fc510SToomas Soome 	if (instance == 0) {
2166406fc510SToomas Soome 		rv = ddi_soft_state_zalloc(lofi_statep, 0);
2167406fc510SToomas Soome 		if (rv == DDI_FAILURE) {
21687c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
21697c478bd9Sstevel@tonic-gate 		}
2170406fc510SToomas Soome 		lsp = ddi_get_soft_state(lofi_statep, instance);
2171406fc510SToomas Soome 		rv = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
2172406fc510SToomas Soome 		    DDI_PSEUDO, 0);
2173406fc510SToomas Soome 		if (rv == DDI_FAILURE) {
21747c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(lofi_statep, 0);
21757c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
21767c478bd9Sstevel@tonic-gate 		}
2177843e1988Sjohnlev 		/* driver handles kernel-issued IOCTLs */
2178843e1988Sjohnlev 		if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
2179843e1988Sjohnlev 		    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
2180843e1988Sjohnlev 			ddi_remove_minor_node(dip, NULL);
2181843e1988Sjohnlev 			ddi_soft_state_free(lofi_statep, 0);
2182843e1988Sjohnlev 			return (DDI_FAILURE);
2183843e1988Sjohnlev 		}
21840fbb751dSJohn Levon 
21850fbb751dSJohn Levon 		zone_key_create(&lofi_zone_key, NULL, lofi_zone_shutdown, NULL);
21860fbb751dSJohn Levon 
2187406fc510SToomas Soome 		lsp->ls_dip = dip;
2188406fc510SToomas Soome 	} else {
2189406fc510SToomas Soome 		if (lofi_online_dev(dip) == DDI_FAILURE)
2190406fc510SToomas Soome 			return (DDI_FAILURE);
2191406fc510SToomas Soome 	}
2192406fc510SToomas Soome 
21937c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
21947c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
21957c478bd9Sstevel@tonic-gate }
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate static int
lofi_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)21987c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
21997c478bd9Sstevel@tonic-gate {
2200406fc510SToomas Soome 	struct lofi_state *lsp;
2201406fc510SToomas Soome 	int instance = ddi_get_instance(dip);
2202406fc510SToomas Soome 
22037c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
22047c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
22050fbb751dSJohn Levon 
2206406fc510SToomas Soome 	/*
2207406fc510SToomas Soome 	 * If the instance is not 0, release state.
2208406fc510SToomas Soome 	 * The instance 0 is control device, we can not detach it
2209406fc510SToomas Soome 	 * before other instances are detached.
2210406fc510SToomas Soome 	 */
2211406fc510SToomas Soome 	if (instance != 0) {
2212406fc510SToomas Soome 		lsp = ddi_get_soft_state(lofi_statep, instance);
2213406fc510SToomas Soome 		if (lsp != NULL && lsp->ls_vp_ready == B_FALSE) {
2214406fc510SToomas Soome 			ddi_soft_state_free(lofi_statep, instance);
2215406fc510SToomas Soome 			return (DDI_SUCCESS);
2216406fc510SToomas Soome 		} else
2217406fc510SToomas Soome 			return (DDI_FAILURE);
2218406fc510SToomas Soome 	}
22190fbb751dSJohn Levon 	mutex_enter(&lofi_lock);
22200fbb751dSJohn Levon 
22210fbb751dSJohn Levon 	if (!list_is_empty(&lofi_list)) {
22220fbb751dSJohn Levon 		mutex_exit(&lofi_lock);
22237c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
22240fbb751dSJohn Levon 	}
22250fbb751dSJohn Levon 
22267c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2227843e1988Sjohnlev 	ddi_prop_remove_all(dip);
22280fbb751dSJohn Levon 
22290fbb751dSJohn Levon 	mutex_exit(&lofi_lock);
22300fbb751dSJohn Levon 
22310fbb751dSJohn Levon 	if (zone_key_delete(lofi_zone_key) != 0)
22320fbb751dSJohn Levon 		cmn_err(CE_WARN, "failed to delete zone key");
22330fbb751dSJohn Levon 
22347c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(lofi_statep, 0);
22350fbb751dSJohn Levon 
22367c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate /*
2240406fc510SToomas Soome  * With the addition of encryption, we must be careful that encryption key is
2241406fc510SToomas Soome  * wiped before kernel's data structures are freed so it cannot accidentally
2242406fc510SToomas Soome  * slip out to userland through uninitialized data elsewhere.
22437d82f0f8SDina K Nimeh  */
22447d82f0f8SDina K Nimeh static void
free_lofi_ioctl(struct lofi_ioctl * klip)22457d82f0f8SDina K Nimeh free_lofi_ioctl(struct lofi_ioctl *klip)
22467d82f0f8SDina K Nimeh {
22477d82f0f8SDina K Nimeh 	/* Make sure this encryption key doesn't stick around */
22487d82f0f8SDina K Nimeh 	bzero(klip->li_key, sizeof (klip->li_key));
22497d82f0f8SDina K Nimeh 	kmem_free(klip, sizeof (struct lofi_ioctl));
22507d82f0f8SDina K Nimeh }
22517d82f0f8SDina K Nimeh 
22527d82f0f8SDina K Nimeh /*
2253406fc510SToomas Soome  * These two functions simplify the rest of the ioctls that need to copyin/out
22547c478bd9Sstevel@tonic-gate  * the lofi_ioctl structure.
22557c478bd9Sstevel@tonic-gate  */
22560fbb751dSJohn Levon int
copy_in_lofi_ioctl(const struct lofi_ioctl * ulip,struct lofi_ioctl ** klipp,int flag)22570fbb751dSJohn Levon copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, struct lofi_ioctl **klipp,
22580fbb751dSJohn Levon     int flag)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
22617c478bd9Sstevel@tonic-gate 	int	error;
22627c478bd9Sstevel@tonic-gate 
22630fbb751dSJohn Levon 	klip = *klipp = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
2264bd07e074Sheppo 	error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
22650fbb751dSJohn Levon 	if (error)
22660fbb751dSJohn Levon 		goto err;
22677c478bd9Sstevel@tonic-gate 
22680fbb751dSJohn Levon 	/* ensure NULL termination */
22696f02aa44SDina K Nimeh 	klip->li_filename[MAXPATHLEN-1] = '\0';
2270406fc510SToomas Soome 	klip->li_devpath[MAXPATHLEN-1] = '\0';
22710fbb751dSJohn Levon 	klip->li_algorithm[MAXALGLEN-1] = '\0';
22720fbb751dSJohn Levon 	klip->li_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
22730fbb751dSJohn Levon 	klip->li_iv_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
22747c478bd9Sstevel@tonic-gate 
2275406fc510SToomas Soome 	if (klip->li_id > L_MAXMIN32) {
22760fbb751dSJohn Levon 		error = EINVAL;
22770fbb751dSJohn Levon 		goto err;
22787c478bd9Sstevel@tonic-gate 	}
22790fbb751dSJohn Levon 
22800fbb751dSJohn Levon 	return (0);
22810fbb751dSJohn Levon 
22820fbb751dSJohn Levon err:
22830fbb751dSJohn Levon 	free_lofi_ioctl(klip);
22840fbb751dSJohn Levon 	return (error);
22857c478bd9Sstevel@tonic-gate }
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate int
copy_out_lofi_ioctl(const struct lofi_ioctl * klip,struct lofi_ioctl * ulip,int flag)2288bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
2289bd07e074Sheppo     int flag)
22907c478bd9Sstevel@tonic-gate {
22917c478bd9Sstevel@tonic-gate 	int	error;
22927c478bd9Sstevel@tonic-gate 
22937d82f0f8SDina K Nimeh 	/*
22947d82f0f8SDina K Nimeh 	 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
22957d82f0f8SDina K Nimeh 	 * This ensures that an attacker can't trivially find the
22967d82f0f8SDina K Nimeh 	 * key for a mapping just by issuing the ioctl.
22977d82f0f8SDina K Nimeh 	 *
22987d82f0f8SDina K Nimeh 	 * It can still be found by poking around in kmem with mdb(1),
22997d82f0f8SDina K Nimeh 	 * but there is no point in making it easy when the info isn't
23007d82f0f8SDina K Nimeh 	 * of any use in this direction anyway.
23017d82f0f8SDina K Nimeh 	 *
23027d82f0f8SDina K Nimeh 	 * Either way we don't actually have the raw key stored in
23037d82f0f8SDina K Nimeh 	 * a form that we can get it anyway, since we just used it
23047d82f0f8SDina K Nimeh 	 * to create a ctx template and didn't keep "the original".
23057d82f0f8SDina K Nimeh 	 */
2306bd07e074Sheppo 	error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
23077c478bd9Sstevel@tonic-gate 	if (error)
23087c478bd9Sstevel@tonic-gate 		return (EFAULT);
23097c478bd9Sstevel@tonic-gate 	return (0);
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23120fbb751dSJohn Levon static int
lofi_access(struct lofi_state * lsp)23130fbb751dSJohn Levon lofi_access(struct lofi_state *lsp)
23140fbb751dSJohn Levon {
23150fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
2316a19609f8Sjv227347 	if (INGLOBALZONE(curproc) || lsp->ls_zone.zref_zone == curzone)
23170fbb751dSJohn Levon 		return (0);
23180fbb751dSJohn Levon 	return (EPERM);
23190fbb751dSJohn Levon }
23200fbb751dSJohn Levon 
23217c478bd9Sstevel@tonic-gate /*
23220fbb751dSJohn Levon  * Find the lofi state for the given filename. We compare by vnode to
23230fbb751dSJohn Levon  * allow the global zone visibility into NGZ lofi nodes.
23247c478bd9Sstevel@tonic-gate  */
23257c478bd9Sstevel@tonic-gate static int
file_to_lofi_nocheck(char * filename,boolean_t readonly,struct lofi_state ** lspp)2326cd69fabeSAlexander Eremin file_to_lofi_nocheck(char *filename, boolean_t readonly,
2327cd69fabeSAlexander Eremin     struct lofi_state **lspp)
23287c478bd9Sstevel@tonic-gate {
23297c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
23300fbb751dSJohn Levon 	vnode_t *vp = NULL;
23310fbb751dSJohn Levon 	int err = 0;
2332cd69fabeSAlexander Eremin 	int rdfiles = 0;
23337c478bd9Sstevel@tonic-gate 
23340fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
23350fbb751dSJohn Levon 
23360fbb751dSJohn Levon 	if ((err = lookupname(filename, UIO_SYSSPACE, FOLLOW,
23370fbb751dSJohn Levon 	    NULLVPP, &vp)) != 0)
23380fbb751dSJohn Levon 		goto out;
23390fbb751dSJohn Levon 
23400fbb751dSJohn Levon 	if (vp->v_type == VREG) {
23410fbb751dSJohn Levon 		vnode_t *realvp;
23420fbb751dSJohn Levon 		if (VOP_REALVP(vp, &realvp, NULL) == 0) {
23430fbb751dSJohn Levon 			VN_HOLD(realvp);
23440fbb751dSJohn Levon 			VN_RELE(vp);
23450fbb751dSJohn Levon 			vp = realvp;
23467c478bd9Sstevel@tonic-gate 		}
23470fbb751dSJohn Levon 	}
23480fbb751dSJohn Levon 
23490fbb751dSJohn Levon 	for (lsp = list_head(&lofi_list); lsp != NULL;
23500fbb751dSJohn Levon 	    lsp = list_next(&lofi_list, lsp)) {
23510fbb751dSJohn Levon 		if (lsp->ls_vp == vp) {
23520fbb751dSJohn Levon 			if (lspp != NULL)
23530fbb751dSJohn Levon 				*lspp = lsp;
2354cd69fabeSAlexander Eremin 			if (lsp->ls_readonly) {
2355cd69fabeSAlexander Eremin 				rdfiles++;
2356cd69fabeSAlexander Eremin 				/* Skip if '-r' is specified */
2357cd69fabeSAlexander Eremin 				if (readonly)
2358cd69fabeSAlexander Eremin 					continue;
2359cd69fabeSAlexander Eremin 			}
23600fbb751dSJohn Levon 			goto out;
23610fbb751dSJohn Levon 		}
23620fbb751dSJohn Levon 	}
23630fbb751dSJohn Levon 
23640fbb751dSJohn Levon 	err = ENOENT;
23650fbb751dSJohn Levon 
2366cd69fabeSAlexander Eremin 	/*
2367cd69fabeSAlexander Eremin 	 * If a filename is given as an argument for lofi_unmap, we shouldn't
2368cd69fabeSAlexander Eremin 	 * allow unmap if there are multiple read-only lofi devices associated
2369cd69fabeSAlexander Eremin 	 * with this file.
2370cd69fabeSAlexander Eremin 	 */
2371cd69fabeSAlexander Eremin 	if (lspp != NULL) {
2372cd69fabeSAlexander Eremin 		if (rdfiles == 1)
2373cd69fabeSAlexander Eremin 			err = 0;
2374cd69fabeSAlexander Eremin 		else if (rdfiles > 1)
2375cd69fabeSAlexander Eremin 			err = EBUSY;
2376cd69fabeSAlexander Eremin 	}
2377cd69fabeSAlexander Eremin 
23780fbb751dSJohn Levon out:
23790fbb751dSJohn Levon 	if (vp != NULL)
23800fbb751dSJohn Levon 		VN_RELE(vp);
23810fbb751dSJohn Levon 	return (err);
23827c478bd9Sstevel@tonic-gate }
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate /*
23850fbb751dSJohn Levon  * Find the minor for the given filename, checking the zone can access
23860fbb751dSJohn Levon  * it.
23877c478bd9Sstevel@tonic-gate  */
23887c478bd9Sstevel@tonic-gate static int
file_to_lofi(char * filename,boolean_t readonly,struct lofi_state ** lspp)2389cd69fabeSAlexander Eremin file_to_lofi(char *filename, boolean_t readonly, struct lofi_state **lspp)
23907c478bd9Sstevel@tonic-gate {
23910fbb751dSJohn Levon 	int err = 0;
23927c478bd9Sstevel@tonic-gate 
23930fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&lofi_lock));
23940fbb751dSJohn Levon 
2395cd69fabeSAlexander Eremin 	if ((err = file_to_lofi_nocheck(filename, readonly, lspp)) != 0)
23960fbb751dSJohn Levon 		return (err);
23970fbb751dSJohn Levon 
23980fbb751dSJohn Levon 	if ((err = lofi_access(*lspp)) != 0)
23990fbb751dSJohn Levon 		return (err);
24000fbb751dSJohn Levon 
24017c478bd9Sstevel@tonic-gate 	return (0);
24027c478bd9Sstevel@tonic-gate }
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate /*
2405406fc510SToomas Soome  * Fakes up a disk geometry based on the size of the file. This is needed
2406406fc510SToomas Soome  * to support newfs on traditional lofi device, but also will provide
2407406fc510SToomas Soome  * geometry hint for cmlb.
24087c478bd9Sstevel@tonic-gate  */
24097c478bd9Sstevel@tonic-gate static void
fake_disk_geometry(struct lofi_state * lsp)24107c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
24117c478bd9Sstevel@tonic-gate {
24127d82f0f8SDina K Nimeh 	u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
24137d82f0f8SDina K Nimeh 
2414bbf21555SRichard Lowe 	/* dk_geom - see dkio(4I) */
24157c478bd9Sstevel@tonic-gate 	/*
24167c478bd9Sstevel@tonic-gate 	 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
24177c478bd9Sstevel@tonic-gate 	 * of sectors), but that breaks programs like fdisk which want to
24187c478bd9Sstevel@tonic-gate 	 * partition a disk by cylinder. With one cylinder, you can't create
24197c478bd9Sstevel@tonic-gate 	 * an fdisk partition and put pcfs on it for testing (hard to pick
24207c478bd9Sstevel@tonic-gate 	 * a number between one and one).
24217c478bd9Sstevel@tonic-gate 	 *
24227c478bd9Sstevel@tonic-gate 	 * The cheezy floppy test is an attempt to not have too few cylinders
24237c478bd9Sstevel@tonic-gate 	 * for a small file, or so many on a big file that you waste space
24247c478bd9Sstevel@tonic-gate 	 * for backup superblocks or cylinder group structures.
24257c478bd9Sstevel@tonic-gate 	 */
2426406fc510SToomas Soome 	bzero(&lsp->ls_dkg, sizeof (lsp->ls_dkg));
24277d82f0f8SDina K Nimeh 	if (dsize < (2 * 1024 * 1024)) /* floppy? */
24287d82f0f8SDina K Nimeh 		lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
24297c478bd9Sstevel@tonic-gate 	else
24307d82f0f8SDina K Nimeh 		lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
24317c478bd9Sstevel@tonic-gate 	/* in case file file is < 100k */
24327c478bd9Sstevel@tonic-gate 	if (lsp->ls_dkg.dkg_ncyl == 0)
24337c478bd9Sstevel@tonic-gate 		lsp->ls_dkg.dkg_ncyl = 1;
24347c478bd9Sstevel@tonic-gate 
2435406fc510SToomas Soome 	lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl;
2436406fc510SToomas Soome 	lsp->ls_dkg.dkg_nhead = 1;
2437406fc510SToomas Soome 	lsp->ls_dkg.dkg_rpm = 7200;
2438406fc510SToomas Soome 
2439406fc510SToomas Soome 	lsp->ls_dkg.dkg_nsect = dsize /
2440406fc510SToomas Soome 	    (lsp->ls_dkg.dkg_ncyl << lsp->ls_pbshift);
2441406fc510SToomas Soome }
2442406fc510SToomas Soome 
2443406fc510SToomas Soome /*
2444bbf21555SRichard Lowe  * build vtoc - see dkio(4I)
2445406fc510SToomas Soome  *
2446406fc510SToomas Soome  * Fakes one big partition based on the size of the file. This is needed
2447406fc510SToomas Soome  * because we allow newfs'ing the traditional lofi device and newfs will
2448406fc510SToomas Soome  * do several disk ioctls to figure out the geometry and partition information.
2449406fc510SToomas Soome  * It uses that information to determine the parameters to pass to mkfs.
2450406fc510SToomas Soome  */
2451406fc510SToomas Soome static void
fake_disk_vtoc(struct lofi_state * lsp,struct vtoc * vt)2452406fc510SToomas Soome fake_disk_vtoc(struct lofi_state *lsp, struct vtoc *vt)
2453406fc510SToomas Soome {
2454406fc510SToomas Soome 	bzero(vt, sizeof (struct vtoc));
2455406fc510SToomas Soome 	vt->v_sanity = VTOC_SANE;
2456406fc510SToomas Soome 	vt->v_version = V_VERSION;
2457406fc510SToomas Soome 	(void) strncpy(vt->v_volume, LOFI_DRIVER_NAME,
2458406fc510SToomas Soome 	    sizeof (vt->v_volume));
2459406fc510SToomas Soome 	vt->v_sectorsz = 1 << lsp->ls_pbshift;
2460406fc510SToomas Soome 	vt->v_nparts = 1;
2461406fc510SToomas Soome 	vt->v_part[0].p_tag = V_UNASSIGNED;
246287117650Saalok 
246387117650Saalok 	/*
246487117650Saalok 	 * A compressed file is read-only, other files can
246587117650Saalok 	 * be read-write
246687117650Saalok 	 */
246787117650Saalok 	if (lsp->ls_uncomp_seg_sz > 0) {
2468406fc510SToomas Soome 		vt->v_part[0].p_flag = V_UNMNT | V_RONLY;
246987117650Saalok 	} else {
2470406fc510SToomas Soome 		vt->v_part[0].p_flag = V_UNMNT;
247187117650Saalok 	}
2472406fc510SToomas Soome 	vt->v_part[0].p_start = (daddr_t)0;
24737c478bd9Sstevel@tonic-gate 	/*
24747c478bd9Sstevel@tonic-gate 	 * The partition size cannot just be the number of sectors, because
24757c478bd9Sstevel@tonic-gate 	 * that might not end on a cylinder boundary. And if that's the case,
24767c478bd9Sstevel@tonic-gate 	 * newfs/mkfs will print a scary warning. So just figure the size
24777c478bd9Sstevel@tonic-gate 	 * based on the number of cylinders and sectors/cylinder.
24787c478bd9Sstevel@tonic-gate 	 */
2479406fc510SToomas Soome 	vt->v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
24807c478bd9Sstevel@tonic-gate 	    lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
2481406fc510SToomas Soome }
24827c478bd9Sstevel@tonic-gate 
2483406fc510SToomas Soome /*
2484bbf21555SRichard Lowe  * build dk_cinfo - see dkio(4I)
2485406fc510SToomas Soome  */
2486406fc510SToomas Soome static void
fake_disk_info(dev_t dev,struct dk_cinfo * ci)2487406fc510SToomas Soome fake_disk_info(dev_t dev, struct dk_cinfo *ci)
2488406fc510SToomas Soome {
2489406fc510SToomas Soome 	bzero(ci, sizeof (struct dk_cinfo));
2490406fc510SToomas Soome 	(void) strlcpy(ci->dki_cname, LOFI_DRIVER_NAME, sizeof (ci->dki_cname));
2491406fc510SToomas Soome 	ci->dki_ctype = DKC_SCSI_CCS;
2492406fc510SToomas Soome 	(void) strlcpy(ci->dki_dname, LOFI_DRIVER_NAME, sizeof (ci->dki_dname));
2493406fc510SToomas Soome 	ci->dki_unit = LOFI_MINOR2ID(getminor(dev));
2494406fc510SToomas Soome 	ci->dki_partition = LOFI_PART(getminor(dev));
24957c478bd9Sstevel@tonic-gate 	/*
24967c478bd9Sstevel@tonic-gate 	 * newfs uses this to set maxcontig. Must not be < 16, or it
24977c478bd9Sstevel@tonic-gate 	 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
24987c478bd9Sstevel@tonic-gate 	 * it by the block size. Then tunefs doesn't work because
24997c478bd9Sstevel@tonic-gate 	 * maxcontig is 0.
25007c478bd9Sstevel@tonic-gate 	 */
2501406fc510SToomas Soome 	ci->dki_maxtransfer = 16;
25027c478bd9Sstevel@tonic-gate }
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate /*
250587117650Saalok  * map in a compressed file
250687117650Saalok  *
250787117650Saalok  * Read in the header and the index that follows.
250887117650Saalok  *
250987117650Saalok  * The header is as follows -
251087117650Saalok  *
251187117650Saalok  * Signature (name of the compression algorithm)
251287117650Saalok  * Compression segment size (a multiple of 512)
251387117650Saalok  * Number of index entries
251487117650Saalok  * Size of the last block
251587117650Saalok  * The array containing the index entries
251687117650Saalok  *
251787117650Saalok  * The header information is always stored in
251887117650Saalok  * network byte order on disk.
251987117650Saalok  */
252087117650Saalok static int
lofi_map_compressed_file(struct lofi_state * lsp,char * buf)252187117650Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
252287117650Saalok {
252387117650Saalok 	uint32_t index_sz, header_len, i;
252487117650Saalok 	ssize_t	resid;
252587117650Saalok 	enum uio_rw rw;
252687117650Saalok 	char *tbuf = buf;
252787117650Saalok 	int error;
252887117650Saalok 
252987117650Saalok 	/* The signature has already been read */
253087117650Saalok 	tbuf += sizeof (lsp->ls_comp_algorithm);
253187117650Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
253287117650Saalok 	lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
253387117650Saalok 
253487117650Saalok 	/*
253587117650Saalok 	 * The compressed segment size must be a power of 2
253687117650Saalok 	 */
25374058a205Sjrgn.keil@googlemail.com 	if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
25384058a205Sjrgn.keil@googlemail.com 	    !ISP2(lsp->ls_uncomp_seg_sz))
253987117650Saalok 		return (EINVAL);
254087117650Saalok 
254187117650Saalok 	for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
254287117650Saalok 		;
254387117650Saalok 
254487117650Saalok 	lsp->ls_comp_seg_shift = i;
254587117650Saalok 
254687117650Saalok 	tbuf += sizeof (lsp->ls_uncomp_seg_sz);
254787117650Saalok 	bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
254887117650Saalok 	lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
254987117650Saalok 
255087117650Saalok 	tbuf += sizeof (lsp->ls_comp_index_sz);
255187117650Saalok 	bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
255287117650Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz));
255387117650Saalok 	lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
255487117650Saalok 
255587117650Saalok 	/*
255687117650Saalok 	 * Compute the total size of the uncompressed data
255787117650Saalok 	 * for use in fake_disk_geometry and other calculations.
255887117650Saalok 	 * Disk geometry has to be faked with respect to the
255987117650Saalok 	 * actual uncompressed data size rather than the
256087117650Saalok 	 * compressed file size.
256187117650Saalok 	 */
25627d11f38eSDavid Miner 	lsp->ls_vp_size =
25637d11f38eSDavid Miner 	    (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
256487117650Saalok 	    + lsp->ls_uncomp_last_seg_sz;
256587117650Saalok 
256687117650Saalok 	/*
2567b1efbcd6SAlok Aggarwal 	 * Index size is rounded up to DEV_BSIZE for ease
256887117650Saalok 	 * of segmapping
256987117650Saalok 	 */
257087117650Saalok 	index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
257187117650Saalok 	header_len = sizeof (lsp->ls_comp_algorithm) +
257287117650Saalok 	    sizeof (lsp->ls_uncomp_seg_sz) +
257387117650Saalok 	    sizeof (lsp->ls_comp_index_sz) +
257487117650Saalok 	    sizeof (lsp->ls_uncomp_last_seg_sz);
257587117650Saalok 	lsp->ls_comp_offbase = header_len + index_sz;
257687117650Saalok 
257787117650Saalok 	index_sz += header_len;
257887117650Saalok 	index_sz = roundup(index_sz, DEV_BSIZE);
257987117650Saalok 
258087117650Saalok 	lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
258187117650Saalok 	lsp->ls_comp_index_data_sz = index_sz;
258287117650Saalok 
258387117650Saalok 	/*
258487117650Saalok 	 * Read in the index -- this has a side-effect
258587117650Saalok 	 * of reading in the header as well
258687117650Saalok 	 */
258787117650Saalok 	rw = UIO_READ;
258887117650Saalok 	error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
258987117650Saalok 	    0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
259087117650Saalok 
259187117650Saalok 	if (error != 0)
259287117650Saalok 		return (error);
259387117650Saalok 
259487117650Saalok 	/* Skip the header, this is where the index really begins */
259587117650Saalok 	lsp->ls_comp_seg_index =
259687117650Saalok 	    (uint64_t *)(lsp->ls_comp_index_data + header_len);
259787117650Saalok 
259887117650Saalok 	/*
259987117650Saalok 	 * Now recompute offsets in the index to account for
260087117650Saalok 	 * the header length
260187117650Saalok 	 */
260287117650Saalok 	for (i = 0; i < lsp->ls_comp_index_sz; i++) {
260387117650Saalok 		lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
260487117650Saalok 		    BE_64(lsp->ls_comp_seg_index[i]);
260587117650Saalok 	}
260687117650Saalok 
260787117650Saalok 	return (error);
260887117650Saalok }
260987117650Saalok 
26100fbb751dSJohn Levon static int
lofi_init_crypto(struct lofi_state * lsp,struct lofi_ioctl * klip)26110fbb751dSJohn Levon lofi_init_crypto(struct lofi_state *lsp, struct lofi_ioctl *klip)
261287117650Saalok {
26130fbb751dSJohn Levon 	struct crypto_meta chead;
26140fbb751dSJohn Levon 	char buf[DEV_BSIZE];
26150fbb751dSJohn Levon 	ssize_t	resid;
26160fbb751dSJohn Levon 	char *marker;
26170fbb751dSJohn Levon 	int error;
26180fbb751dSJohn Levon 	int ret;
261987117650Saalok 	int i;
262087117650Saalok 
26210fbb751dSJohn Levon 	if (!klip->li_crypto_enabled)
26220fbb751dSJohn Levon 		return (0);
262387117650Saalok 
262487117650Saalok 	/*
26250fbb751dSJohn Levon 	 * All current algorithms have a max of 448 bits.
26267c478bd9Sstevel@tonic-gate 	 */
26270fbb751dSJohn Levon 	if (klip->li_iv_len > CRYPTO_BITS2BYTES(512))
26280fbb751dSJohn Levon 		return (EINVAL);
26297c478bd9Sstevel@tonic-gate 
26300fbb751dSJohn Levon 	if (CRYPTO_BITS2BYTES(klip->li_key_len) > sizeof (klip->li_key))
26310fbb751dSJohn Levon 		return (EINVAL);
26327c478bd9Sstevel@tonic-gate 
26330fbb751dSJohn Levon 	lsp->ls_crypto_enabled = klip->li_crypto_enabled;
263487117650Saalok 
26357d82f0f8SDina K Nimeh 	mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
26367d82f0f8SDina K Nimeh 
26377d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
26387d82f0f8SDina K Nimeh 	if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
26397d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "invalid cipher %s requested for %s",
26400fbb751dSJohn Levon 		    klip->li_cipher, klip->li_filename);
26410fbb751dSJohn Levon 		return (EINVAL);
26427d82f0f8SDina K Nimeh 	}
26437d82f0f8SDina K Nimeh 
26447d82f0f8SDina K Nimeh 	/* this is just initialization here */
26457d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_param = NULL;
26467d82f0f8SDina K Nimeh 	lsp->ls_mech.cm_param_len = 0;
26477d82f0f8SDina K Nimeh 
26487d82f0f8SDina K Nimeh 	lsp->ls_iv_type = klip->li_iv_type;
26497d82f0f8SDina K Nimeh 	lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
26507d82f0f8SDina K Nimeh 	if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
26517d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "invalid iv cipher %s requested"
26520fbb751dSJohn Levon 		    " for %s", klip->li_iv_cipher, klip->li_filename);
26530fbb751dSJohn Levon 		return (EINVAL);
26547d82f0f8SDina K Nimeh 	}
26557d82f0f8SDina K Nimeh 
26567d82f0f8SDina K Nimeh 	/* iv mech must itself take a null iv */
26577d82f0f8SDina K Nimeh 	lsp->ls_iv_mech.cm_param = NULL;
26587d82f0f8SDina K Nimeh 	lsp->ls_iv_mech.cm_param_len = 0;
26597d82f0f8SDina K Nimeh 	lsp->ls_iv_len = klip->li_iv_len;
26607d82f0f8SDina K Nimeh 
26617d82f0f8SDina K Nimeh 	/*
26627d82f0f8SDina K Nimeh 	 * Create ctx using li_cipher & the raw li_key after checking
26637d82f0f8SDina K Nimeh 	 * that it isn't a weak key.
26647d82f0f8SDina K Nimeh 	 */
26657d82f0f8SDina K Nimeh 	lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
26667d82f0f8SDina K Nimeh 	lsp->ls_key.ck_length = klip->li_key_len;
26677d82f0f8SDina K Nimeh 	lsp->ls_key.ck_data = kmem_alloc(
26687d82f0f8SDina K Nimeh 	    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
26697d82f0f8SDina K Nimeh 	bcopy(klip->li_key, lsp->ls_key.ck_data,
26707d82f0f8SDina K Nimeh 	    CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
26717d82f0f8SDina K Nimeh 
26727d82f0f8SDina K Nimeh 	ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
26737d82f0f8SDina K Nimeh 	if (ret != CRYPTO_SUCCESS) {
26747d82f0f8SDina K Nimeh 		cmn_err(CE_WARN, "weak key check failed for cipher "
26757d82f0f8SDina K Nimeh 		    "%s on file %s (0x%x)", klip->li_cipher,
26760fbb751dSJohn Levon 		    klip->li_filename, ret);
26770fbb751dSJohn Levon 		return (EINVAL);
26787d82f0f8SDina K Nimeh 	}
26797d82f0f8SDina K Nimeh 
26800fbb751dSJohn Levon 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE,
26817d82f0f8SDina K Nimeh 	    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
26827d82f0f8SDina K Nimeh 	if (error != 0)
26830fbb751dSJohn Levon 		return (error);
26847d82f0f8SDina K Nimeh 
26857d82f0f8SDina K Nimeh 	/*
26860fbb751dSJohn Levon 	 * This is the case where the header in the lofi image is already
26870fbb751dSJohn Levon 	 * initialized to indicate it is encrypted.
26887d82f0f8SDina K Nimeh 	 */
26890fbb751dSJohn Levon 	if (strncmp(buf, lofi_crypto_magic, sizeof (lofi_crypto_magic)) == 0) {
26907d82f0f8SDina K Nimeh 		/*
26917d82f0f8SDina K Nimeh 		 * The encryption header information is laid out this way:
26927d82f0f8SDina K Nimeh 		 *	6 bytes:	hex "CFLOFI"
26937d82f0f8SDina K Nimeh 		 *	2 bytes:	version = 0 ... for now
26947d82f0f8SDina K Nimeh 		 *	96 bytes:	reserved1 (not implemented yet)
26957d82f0f8SDina K Nimeh 		 *	4 bytes:	data_sector = 2 ... for now
26967d82f0f8SDina K Nimeh 		 *	more...		not implemented yet
26977d82f0f8SDina K Nimeh 		 */
26987d82f0f8SDina K Nimeh 
26990fbb751dSJohn Levon 		marker = buf;
27000fbb751dSJohn Levon 
27017d82f0f8SDina K Nimeh 		/* copy the magic */
27027d82f0f8SDina K Nimeh 		bcopy(marker, lsp->ls_crypto.magic,
27037d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.magic));
27047d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.magic);
27057d82f0f8SDina K Nimeh 
27067d82f0f8SDina K Nimeh 		/* read the encryption version number */
27077d82f0f8SDina K Nimeh 		bcopy(marker, &(lsp->ls_crypto.version),
27087d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.version));
27097d82f0f8SDina K Nimeh 		lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
27107d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.version);
27117d82f0f8SDina K Nimeh 
27127d82f0f8SDina K Nimeh 		/* read a chunk of reserved data */
27137d82f0f8SDina K Nimeh 		bcopy(marker, lsp->ls_crypto.reserved1,
27147d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.reserved1));
27157d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.reserved1);
27167d82f0f8SDina K Nimeh 
27177d82f0f8SDina K Nimeh 		/* read block number where encrypted data begins */
27187d82f0f8SDina K Nimeh 		bcopy(marker, &(lsp->ls_crypto.data_sector),
27197d82f0f8SDina K Nimeh 		    sizeof (lsp->ls_crypto.data_sector));
27207d82f0f8SDina K Nimeh 		lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
27217d82f0f8SDina K Nimeh 		marker += sizeof (lsp->ls_crypto.data_sector);
27227d82f0f8SDina K Nimeh 
27237d82f0f8SDina K Nimeh 		/* and ignore the rest until it is implemented */
27247d82f0f8SDina K Nimeh 
27257d82f0f8SDina K Nimeh 		lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
27260fbb751dSJohn Levon 		return (0);
27277d82f0f8SDina K Nimeh 	}
27287d82f0f8SDina K Nimeh 
27290fbb751dSJohn Levon 	/*
27300fbb751dSJohn Levon 	 * We've requested encryption, but no magic was found, so it must be
27310fbb751dSJohn Levon 	 * a new image.
27320fbb751dSJohn Levon 	 */
27330fbb751dSJohn Levon 
27340fbb751dSJohn Levon 	for (i = 0; i < sizeof (struct crypto_meta); i++) {
27350fbb751dSJohn Levon 		if (buf[i] != '\0')
27360fbb751dSJohn Levon 			return (EINVAL);
27370fbb751dSJohn Levon 	}
27380fbb751dSJohn Levon 
27390fbb751dSJohn Levon 	marker = buf;
27407d82f0f8SDina K Nimeh 	bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
27417d82f0f8SDina K Nimeh 	marker += sizeof (lofi_crypto_magic);
27427d82f0f8SDina K Nimeh 	chead.version = htons(LOFI_CRYPTO_VERSION);
27437d82f0f8SDina K Nimeh 	bcopy(&(chead.version), marker, sizeof (chead.version));
27447d82f0f8SDina K Nimeh 	marker += sizeof (chead.version);
27457d82f0f8SDina K Nimeh 	marker += sizeof (chead.reserved1);
27467d82f0f8SDina K Nimeh 	chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
27477d82f0f8SDina K Nimeh 	bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
27487d82f0f8SDina K Nimeh 
27497d82f0f8SDina K Nimeh 	/* write the header */
27500fbb751dSJohn Levon 	error = vn_rdwr(UIO_WRITE, lsp->ls_vp, buf, DEV_BSIZE,
27517d82f0f8SDina K Nimeh 	    CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
27527d82f0f8SDina K Nimeh 	if (error != 0)
27530fbb751dSJohn Levon 		return (error);
27547d82f0f8SDina K Nimeh 
27557d82f0f8SDina K Nimeh 	/* fix things up so it looks like we read this info */
27567d82f0f8SDina K Nimeh 	bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
27577d82f0f8SDina K Nimeh 	    sizeof (lofi_crypto_magic));
27587d82f0f8SDina K Nimeh 	lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
27597d82f0f8SDina K Nimeh 	lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
27607d82f0f8SDina K Nimeh 	lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
27610fbb751dSJohn Levon 	return (0);
27627d82f0f8SDina K Nimeh }
27637d82f0f8SDina K Nimeh 
27647d82f0f8SDina K Nimeh /*
27650fbb751dSJohn Levon  * Check to see if the passed in signature is a valid one.  If it is
27660fbb751dSJohn Levon  * valid, return the index into lofi_compress_table.
27670fbb751dSJohn Levon  *
27680fbb751dSJohn Levon  * Return -1 if it is invalid
27697d82f0f8SDina K Nimeh  */
27700fbb751dSJohn Levon static int
lofi_compress_select(const char * signature)27710fbb751dSJohn Levon lofi_compress_select(const char *signature)
27720fbb751dSJohn Levon {
27730fbb751dSJohn Levon 	int i;
27740fbb751dSJohn Levon 
27750fbb751dSJohn Levon 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
27760fbb751dSJohn Levon 		if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
27770fbb751dSJohn Levon 			return (i);
277887117650Saalok 	}
27790fbb751dSJohn Levon 
27800fbb751dSJohn Levon 	return (-1);
27810fbb751dSJohn Levon }
27820fbb751dSJohn Levon 
27830fbb751dSJohn Levon static int
lofi_init_compress(struct lofi_state * lsp)27840fbb751dSJohn Levon lofi_init_compress(struct lofi_state *lsp)
27850fbb751dSJohn Levon {
27860fbb751dSJohn Levon 	char buf[DEV_BSIZE];
27870fbb751dSJohn Levon 	int compress_index;
27880fbb751dSJohn Levon 	ssize_t	resid;
27890fbb751dSJohn Levon 	int error;
27900fbb751dSJohn Levon 
27910fbb751dSJohn Levon 	error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
27920fbb751dSJohn Levon 	    0, RLIM64_INFINITY, kcred, &resid);
27930fbb751dSJohn Levon 
27940fbb751dSJohn Levon 	if (error != 0)
27950fbb751dSJohn Levon 		return (error);
27960fbb751dSJohn Levon 
27970fbb751dSJohn Levon 	if ((compress_index = lofi_compress_select(buf)) == -1)
27980fbb751dSJohn Levon 		return (0);
27990fbb751dSJohn Levon 
28000fbb751dSJohn Levon 	/* compression and encryption are mutually exclusive */
28010fbb751dSJohn Levon 	if (lsp->ls_crypto_enabled)
28020fbb751dSJohn Levon 		return (ENOTSUP);
28030fbb751dSJohn Levon 
28040fbb751dSJohn Levon 	/* initialize compression info for compressed lofi */
28050fbb751dSJohn Levon 	lsp->ls_comp_algorithm_index = compress_index;
28060fbb751dSJohn Levon 	(void) strlcpy(lsp->ls_comp_algorithm,
28070fbb751dSJohn Levon 	    lofi_compress_table[compress_index].l_name,
28080fbb751dSJohn Levon 	    sizeof (lsp->ls_comp_algorithm));
28090fbb751dSJohn Levon 
28100fbb751dSJohn Levon 	/* Finally setup per-thread pre-allocated buffers */
28110fbb751dSJohn Levon 	lsp->ls_comp_bufs = kmem_zalloc(lofi_taskq_nthreads *
28120fbb751dSJohn Levon 	    sizeof (struct compbuf), KM_SLEEP);
28130fbb751dSJohn Levon 
28140fbb751dSJohn Levon 	return (lofi_map_compressed_file(lsp, buf));
28150fbb751dSJohn Levon }
28160fbb751dSJohn Levon 
28170fbb751dSJohn Levon /*
2818406fc510SToomas Soome  * Allocate new or proposed id from lofi_id.
2819406fc510SToomas Soome  *
2820406fc510SToomas Soome  * Special cases for proposed id:
2821406fc510SToomas Soome  * 0: not allowed, 0 is id for control device.
2822406fc510SToomas Soome  * -1: allocate first usable id from lofi_id.
2823406fc510SToomas Soome  * any other value is proposed value from userland
2824406fc510SToomas Soome  *
2825406fc510SToomas Soome  * returns DDI_SUCCESS or errno.
2826406fc510SToomas Soome  */
2827406fc510SToomas Soome static int
lofi_alloc_id(int * idp)2828406fc510SToomas Soome lofi_alloc_id(int *idp)
2829406fc510SToomas Soome {
2830406fc510SToomas Soome 	int id, error = DDI_SUCCESS;
2831406fc510SToomas Soome 
2832406fc510SToomas Soome 	if (*idp == -1) {
2833406fc510SToomas Soome 		id = id_allocff_nosleep(lofi_id);
2834406fc510SToomas Soome 		if (id == -1) {
2835406fc510SToomas Soome 			error = EAGAIN;
2836406fc510SToomas Soome 			goto err;
2837406fc510SToomas Soome 		}
2838406fc510SToomas Soome 	} else if (*idp == 0) {
2839406fc510SToomas Soome 		error = EINVAL;
2840406fc510SToomas Soome 		goto err;
2841406fc510SToomas Soome 	} else if (*idp > ((1 << (L_BITSMINOR - LOFI_CMLB_SHIFT)) - 1)) {
2842406fc510SToomas Soome 		error = ERANGE;
2843406fc510SToomas Soome 		goto err;
2844406fc510SToomas Soome 	} else {
2845406fc510SToomas Soome 		if (ddi_get_soft_state(lofi_statep, *idp) != NULL) {
2846406fc510SToomas Soome 			error = EEXIST;
2847406fc510SToomas Soome 			goto err;
2848406fc510SToomas Soome 		}
2849406fc510SToomas Soome 
2850406fc510SToomas Soome 		id = id_alloc_specific_nosleep(lofi_id, *idp);
2851406fc510SToomas Soome 		if (id == -1) {
2852406fc510SToomas Soome 			error = EAGAIN;
2853406fc510SToomas Soome 			goto err;
2854406fc510SToomas Soome 		}
2855406fc510SToomas Soome 	}
2856406fc510SToomas Soome 	*idp = id;
2857406fc510SToomas Soome err:
2858406fc510SToomas Soome 	return (error);
2859406fc510SToomas Soome }
2860406fc510SToomas Soome 
2861406fc510SToomas Soome static int
lofi_create_dev(struct lofi_ioctl * klip)2862406fc510SToomas Soome lofi_create_dev(struct lofi_ioctl *klip)
2863406fc510SToomas Soome {
2864406fc510SToomas Soome 	dev_info_t *parent, *child;
2865406fc510SToomas Soome 	struct lofi_state *lsp = NULL;
2866406fc510SToomas Soome 	char namebuf[MAXNAMELEN];
28673fe80ca4SDan Cross 	int error;
2868406fc510SToomas Soome 
2869406fc510SToomas Soome 	/* get control device */
2870406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, 0);
2871406fc510SToomas Soome 	parent = ddi_get_parent(lsp->ls_dip);
2872406fc510SToomas Soome 
2873406fc510SToomas Soome 	if ((error = lofi_alloc_id((int *)&klip->li_id)))
2874406fc510SToomas Soome 		return (error);
2875406fc510SToomas Soome 
2876406fc510SToomas Soome 	(void) snprintf(namebuf, sizeof (namebuf), LOFI_DRIVER_NAME "@%d",
2877406fc510SToomas Soome 	    klip->li_id);
2878406fc510SToomas Soome 
28793fe80ca4SDan Cross 	ndi_devi_enter(parent);
2880406fc510SToomas Soome 	child = ndi_devi_findchild(parent, namebuf);
28813fe80ca4SDan Cross 	ndi_devi_exit(parent);
2882406fc510SToomas Soome 
2883406fc510SToomas Soome 	if (child == NULL) {
2884406fc510SToomas Soome 		child = ddi_add_child(parent, LOFI_DRIVER_NAME,
2885406fc510SToomas Soome 		    (pnode_t)DEVI_SID_NODEID, klip->li_id);
2886406fc510SToomas Soome 		if ((error = ddi_prop_update_int(DDI_DEV_T_NONE, child,
2887406fc510SToomas Soome 		    "instance", klip->li_id)) != DDI_PROP_SUCCESS)
2888406fc510SToomas Soome 			goto err;
2889406fc510SToomas Soome 
2890406fc510SToomas Soome 		if (klip->li_labeled == B_TRUE) {
2891406fc510SToomas Soome 			if ((error = ddi_prop_create(DDI_DEV_T_NONE, child,
2892406fc510SToomas Soome 			    DDI_PROP_CANSLEEP, "labeled", 0, 0))
2893406fc510SToomas Soome 			    != DDI_PROP_SUCCESS)
2894406fc510SToomas Soome 				goto err;
2895406fc510SToomas Soome 		}
2896406fc510SToomas Soome 
2897406fc510SToomas Soome 		if ((error = ndi_devi_online(child, NDI_ONLINE_ATTACH))
2898406fc510SToomas Soome 		    != NDI_SUCCESS)
2899406fc510SToomas Soome 			goto err;
2900406fc510SToomas Soome 	} else {
2901406fc510SToomas Soome 		id_free(lofi_id, klip->li_id);
2902406fc510SToomas Soome 		error = EEXIST;
2903406fc510SToomas Soome 		return (error);
2904406fc510SToomas Soome 	}
2905406fc510SToomas Soome 
2906406fc510SToomas Soome 	goto done;
2907406fc510SToomas Soome 
2908406fc510SToomas Soome err:
2909406fc510SToomas Soome 	ddi_prop_remove_all(child);
2910406fc510SToomas Soome 	(void) ndi_devi_offline(child, NDI_DEVI_REMOVE);
2911406fc510SToomas Soome 	id_free(lofi_id, klip->li_id);
2912406fc510SToomas Soome done:
2913406fc510SToomas Soome 
2914406fc510SToomas Soome 	return (error);
2915406fc510SToomas Soome }
2916406fc510SToomas Soome 
2917406fc510SToomas Soome static void
lofi_create_inquiry(struct lofi_state * lsp,struct scsi_inquiry * inq)2918406fc510SToomas Soome lofi_create_inquiry(struct lofi_state *lsp, struct scsi_inquiry *inq)
2919406fc510SToomas Soome {
2920406fc510SToomas Soome 	char *p = NULL;
2921406fc510SToomas Soome 
2922406fc510SToomas Soome 	(void) strlcpy(inq->inq_vid, LOFI_DRIVER_NAME, sizeof (inq->inq_vid));
2923406fc510SToomas Soome 
2924406fc510SToomas Soome 	mutex_enter(&lsp->ls_vp_lock);
2925406fc510SToomas Soome 	if (lsp->ls_vp != NULL)
2926406fc510SToomas Soome 		p = strrchr(lsp->ls_vp->v_path, '/');
2927406fc510SToomas Soome 	if (p != NULL)
2928406fc510SToomas Soome 		(void) strncpy(inq->inq_pid, p + 1, sizeof (inq->inq_pid));
2929406fc510SToomas Soome 	mutex_exit(&lsp->ls_vp_lock);
2930406fc510SToomas Soome 	(void) strlcpy(inq->inq_revision, "1.0", sizeof (inq->inq_revision));
2931406fc510SToomas Soome }
2932406fc510SToomas Soome 
2933406fc510SToomas Soome /*
2934406fc510SToomas Soome  * copy devlink name from event cache
2935406fc510SToomas Soome  */
2936406fc510SToomas Soome static void
lofi_copy_devpath(struct lofi_ioctl * klip)2937406fc510SToomas Soome lofi_copy_devpath(struct lofi_ioctl *klip)
2938406fc510SToomas Soome {
2939406fc510SToomas Soome 	int	error;
2940406fc510SToomas Soome 	char	namebuf[MAXNAMELEN], *str;
2941406fc510SToomas Soome 	clock_t ticks;
29428ae05c10SToomas Soome 	nvlist_t *nvl = NULL;
2943406fc510SToomas Soome 
2944406fc510SToomas Soome 	if (klip->li_labeled == B_TRUE)
2945406fc510SToomas Soome 		klip->li_devpath[0] = '\0';
2946406fc510SToomas Soome 	else {
2947406fc510SToomas Soome 		/* no need to wait for messages */
2948406fc510SToomas Soome 		(void) snprintf(klip->li_devpath, sizeof (klip->li_devpath),
2949406fc510SToomas Soome 		    "/dev/" LOFI_CHAR_NAME "/%d", klip->li_id);
2950406fc510SToomas Soome 		return;
2951406fc510SToomas Soome 	}
2952406fc510SToomas Soome 
2953406fc510SToomas Soome 	(void) snprintf(namebuf, sizeof (namebuf), "%d", klip->li_id);
2954406fc510SToomas Soome 
29558ae05c10SToomas Soome 	mutex_enter(&lofi_devlink_cache.ln_lock);
295684ce06ceSAndy Fiddaman 	for (;;) {
29579a1bf7f0SToomas Soome 		error = nvlist_lookup_nvlist(lofi_devlink_cache.ln_data,
29589a1bf7f0SToomas Soome 		    namebuf, &nvl);
29599a1bf7f0SToomas Soome 
296084ce06ceSAndy Fiddaman 		if (error == 0 &&
296184ce06ceSAndy Fiddaman 		    nvlist_lookup_string(nvl, DEV_NAME, &str) == 0 &&
296284ce06ceSAndy Fiddaman 		    strncmp(str, "/dev/" LOFI_CHAR_NAME,
296384ce06ceSAndy Fiddaman 		    sizeof ("/dev/" LOFI_CHAR_NAME) - 1) != 0) {
296484ce06ceSAndy Fiddaman 			(void) strlcpy(klip->li_devpath, str,
296584ce06ceSAndy Fiddaman 			    sizeof (klip->li_devpath));
296684ce06ceSAndy Fiddaman 			break;
296784ce06ceSAndy Fiddaman 		}
296884ce06ceSAndy Fiddaman 		/*
296984ce06ceSAndy Fiddaman 		 * Either there is no data in the cache, or the
297084ce06ceSAndy Fiddaman 		 * cache entry still has the wrong device name.
297184ce06ceSAndy Fiddaman 		 */
297284ce06ceSAndy Fiddaman 		ticks = ddi_get_lbolt() + lofi_timeout * drv_usectohz(1000000);
29738ae05c10SToomas Soome 		error = cv_timedwait(&lofi_devlink_cache.ln_cv,
29748ae05c10SToomas Soome 		    &lofi_devlink_cache.ln_lock, ticks);
2975406fc510SToomas Soome 		if (error == -1)
29769a1bf7f0SToomas Soome 			break;	/* timeout */
2977406fc510SToomas Soome 	}
29788ae05c10SToomas Soome 	mutex_exit(&lofi_devlink_cache.ln_lock);
2979406fc510SToomas Soome }
2980406fc510SToomas Soome 
2981406fc510SToomas Soome /*
29820fbb751dSJohn Levon  * map a file to a minor number. Return the minor number.
29830fbb751dSJohn Levon  */
29840fbb751dSJohn Levon static int
lofi_map_file(dev_t dev,struct lofi_ioctl * ulip,int pickminor,int * rvalp,struct cred * credp,int ioctl_flag)29850fbb751dSJohn Levon lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
29860fbb751dSJohn Levon     int *rvalp, struct cred *credp, int ioctl_flag)
29870fbb751dSJohn Levon {
2988406fc510SToomas Soome 	int	id = -1;
29890fbb751dSJohn Levon 	struct lofi_state *lsp = NULL;
29900fbb751dSJohn Levon 	struct lofi_ioctl *klip;
299103f27e8bSAndy Fiddaman 	int	error, canfree;
29920fbb751dSJohn Levon 	struct vnode *vp = NULL;
29930fbb751dSJohn Levon 	vattr_t	vattr;
299403f27e8bSAndy Fiddaman 	int	flag = 0;
2995406fc510SToomas Soome 	char	namebuf[MAXNAMELEN];
29960fbb751dSJohn Levon 
29970fbb751dSJohn Levon 	error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
29980fbb751dSJohn Levon 	if (error != 0)
29990fbb751dSJohn Levon 		return (error);
30000fbb751dSJohn Levon 
30010fbb751dSJohn Levon 	mutex_enter(&lofi_lock);
30020fbb751dSJohn Levon 
3003cd69fabeSAlexander Eremin 	if (file_to_lofi_nocheck(klip->li_filename, klip->li_readonly,
3004cd69fabeSAlexander Eremin 	    NULL) == 0) {
30050fbb751dSJohn Levon 		error = EBUSY;
30060fbb751dSJohn Levon 		goto err;
30070fbb751dSJohn Levon 	}
30080fbb751dSJohn Levon 
30090fbb751dSJohn Levon 	flag = FREAD | FWRITE | FOFFMAX | FEXCL;
30100fbb751dSJohn Levon 	error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
30110fbb751dSJohn Levon 	if (error) {
30120fbb751dSJohn Levon 		/* try read-only */
30130fbb751dSJohn Levon 		flag &= ~FWRITE;
30140fbb751dSJohn Levon 		error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
30150fbb751dSJohn Levon 		    &vp, 0, 0);
30160fbb751dSJohn Levon 		if (error)
30170fbb751dSJohn Levon 			goto err;
30180fbb751dSJohn Levon 	}
30190fbb751dSJohn Levon 
30200fbb751dSJohn Levon 	if (!V_ISLOFIABLE(vp->v_type)) {
302187117650Saalok 		error = EINVAL;
30220fbb751dSJohn Levon 		goto err;
30230fbb751dSJohn Levon 	}
30240fbb751dSJohn Levon 
30250fbb751dSJohn Levon 	vattr.va_mask = AT_SIZE;
30260fbb751dSJohn Levon 	error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
30270fbb751dSJohn Levon 	if (error)
30280fbb751dSJohn Levon 		goto err;
30290fbb751dSJohn Levon 
30300fbb751dSJohn Levon 	/* the file needs to be a multiple of the block size */
30310fbb751dSJohn Levon 	if ((vattr.va_size % DEV_BSIZE) != 0) {
30320fbb751dSJohn Levon 		error = EINVAL;
30330fbb751dSJohn Levon 		goto err;
30340fbb751dSJohn Levon 	}
30350fbb751dSJohn Levon 
3036406fc510SToomas Soome 	if (pickminor) {
3037406fc510SToomas Soome 		klip->li_id = (uint32_t)-1;
30380fbb751dSJohn Levon 	}
3039406fc510SToomas Soome 	if ((error = lofi_create_dev(klip)) != 0)
3040406fc510SToomas Soome 		goto err;
30410fbb751dSJohn Levon 
3042406fc510SToomas Soome 	id = klip->li_id;
3043406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, id);
3044406fc510SToomas Soome 	if (lsp == NULL)
3045406fc510SToomas Soome 		goto err;
30460fbb751dSJohn Levon 
3047406fc510SToomas Soome 	/*
3048406fc510SToomas Soome 	 * from this point lofi_destroy() is used to clean up on error
3049406fc510SToomas Soome 	 * make sure the basic data is set
3050406fc510SToomas Soome 	 */
305108950549SAttila Fülöp 	list_insert_tail(&lofi_list, lsp);
3052406fc510SToomas Soome 	lsp->ls_dev = makedevice(getmajor(dev), LOFI_ID2MINOR(id));
30530fbb751dSJohn Levon 
30540fbb751dSJohn Levon 	list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
30550fbb751dSJohn Levon 	    offsetof(struct lofi_comp_cache, lc_list));
30560fbb751dSJohn Levon 
30570fbb751dSJohn Levon 	/*
30580fbb751dSJohn Levon 	 * save open mode so file can be closed properly and vnode counts
30590fbb751dSJohn Levon 	 * updated correctly.
30600fbb751dSJohn Levon 	 */
30610fbb751dSJohn Levon 	lsp->ls_openflag = flag;
30620fbb751dSJohn Levon 
30630fbb751dSJohn Levon 	lsp->ls_vp = vp;
30640fbb751dSJohn Levon 	lsp->ls_stacked_vp = vp;
3065406fc510SToomas Soome 
3066406fc510SToomas Soome 	lsp->ls_vp_size = vattr.va_size;
3067406fc510SToomas Soome 	lsp->ls_vp_comp_size = lsp->ls_vp_size;
3068406fc510SToomas Soome 
30690fbb751dSJohn Levon 	/*
30700fbb751dSJohn Levon 	 * Try to handle stacked lofs vnodes.
30710fbb751dSJohn Levon 	 */
30720fbb751dSJohn Levon 	if (vp->v_type == VREG) {
30730fbb751dSJohn Levon 		vnode_t *realvp;
30740fbb751dSJohn Levon 
30750fbb751dSJohn Levon 		if (VOP_REALVP(vp, &realvp, NULL) == 0) {
30760fbb751dSJohn Levon 			/*
30770fbb751dSJohn Levon 			 * We need to use the realvp for uniqueness
30780fbb751dSJohn Levon 			 * checking, but keep the stacked vp for
30790fbb751dSJohn Levon 			 * LOFI_GET_FILENAME display.
30800fbb751dSJohn Levon 			 */
30810fbb751dSJohn Levon 			VN_HOLD(realvp);
30820fbb751dSJohn Levon 			lsp->ls_vp = realvp;
308387117650Saalok 		}
308487117650Saalok 	}
308587117650Saalok 
3086406fc510SToomas Soome 	lsp->ls_lbshift = highbit(DEV_BSIZE) - 1;
3087406fc510SToomas Soome 	lsp->ls_pbshift = lsp->ls_lbshift;
30880fbb751dSJohn Levon 
3089cd69fabeSAlexander Eremin 	lsp->ls_readonly = klip->li_readonly;
3090406fc510SToomas Soome 	lsp->ls_uncomp_seg_sz = 0;
3091406fc510SToomas Soome 	lsp->ls_comp_algorithm[0] = '\0';
3092406fc510SToomas Soome 	lsp->ls_crypto_offset = 0;
3093406fc510SToomas Soome 
3094406fc510SToomas Soome 	(void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
3095406fc510SToomas Soome 	    LOFI_DRIVER_NAME, id);
3096406fc510SToomas Soome 	lsp->ls_taskq = taskq_create_proc(namebuf, lofi_taskq_nthreads,
3097406fc510SToomas Soome 	    minclsyspri, 1, lofi_taskq_maxalloc, curzone->zone_zsched, 0);
3098cd69fabeSAlexander Eremin 
30990fbb751dSJohn Levon 	if ((error = lofi_init_crypto(lsp, klip)) != 0)
31000fbb751dSJohn Levon 		goto err;
31010fbb751dSJohn Levon 
31020fbb751dSJohn Levon 	if ((error = lofi_init_compress(lsp)) != 0)
31030fbb751dSJohn Levon 		goto err;
31040fbb751dSJohn Levon 
31057c478bd9Sstevel@tonic-gate 	fake_disk_geometry(lsp);
31060fbb751dSJohn Levon 
3107538c7dacSToomas Soome 	/* For unlabeled lofi add Nblocks and Size */
3108538c7dacSToomas Soome 	if (klip->li_labeled == B_FALSE) {
3109538c7dacSToomas Soome 		error = ddi_prop_update_int64(lsp->ls_dev, lsp->ls_dip,
3110538c7dacSToomas Soome 		    SIZE_PROP_NAME, lsp->ls_vp_size - lsp->ls_crypto_offset);
3111538c7dacSToomas Soome 		if (error != DDI_PROP_SUCCESS) {
31120fbb751dSJohn Levon 			error = EINVAL;
3113406fc510SToomas Soome 			goto err;
31140fbb751dSJohn Levon 		}
3115538c7dacSToomas Soome 		error = ddi_prop_update_int64(lsp->ls_dev, lsp->ls_dip,
3116450b24a3SToomas Soome 		    NBLOCKS_PROP_NAME,
3117538c7dacSToomas Soome 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE);
3118538c7dacSToomas Soome 		if (error != DDI_PROP_SUCCESS) {
31190fbb751dSJohn Levon 			error = EINVAL;
3120406fc510SToomas Soome 			goto err;
31210fbb751dSJohn Levon 		}
3122538c7dacSToomas Soome 	}
31230fbb751dSJohn Levon 
312403f27e8bSAndy Fiddaman 	/* Determine if the underlying device supports TRIM/DISCARD */
312503f27e8bSAndy Fiddaman 	if (lsp->ls_vp->v_type == VCHR || lsp->ls_vp->v_type == VBLK) {
312603f27e8bSAndy Fiddaman 		if (VOP_IOCTL(lsp->ls_vp, DKIOC_CANFREE, (intptr_t)&canfree,
312703f27e8bSAndy Fiddaman 		    FKIOCTL, kcred, &error, NULL) != 0) {
312803f27e8bSAndy Fiddaman 			canfree = 0;
312903f27e8bSAndy Fiddaman 		}
313003f27e8bSAndy Fiddaman 	} else {
313103f27e8bSAndy Fiddaman 		/*
313203f27e8bSAndy Fiddaman 		 * We don't have a way to discover if a file supports
313303f27e8bSAndy Fiddaman 		 * the FREESP fcntl cmd (other than trying it).
313403f27e8bSAndy Fiddaman 		 * However, since zfs, ufs, tmpfs, and udfs all have
313503f27e8bSAndy Fiddaman 		 * support, and NFSv4 also forwards these requests to
313603f27e8bSAndy Fiddaman 		 * the server, we always enable it for file based
313703f27e8bSAndy Fiddaman 		 * volumes. When it comes to executing the commands
313803f27e8bSAndy Fiddaman 		 * they may silently fail.
313903f27e8bSAndy Fiddaman 		 */
314003f27e8bSAndy Fiddaman 		canfree = 1;
314103f27e8bSAndy Fiddaman 	}
314203f27e8bSAndy Fiddaman 
314303f27e8bSAndy Fiddaman 	if (lsp->ls_readonly)
314403f27e8bSAndy Fiddaman 		canfree = 0;
314503f27e8bSAndy Fiddaman 
314603f27e8bSAndy Fiddaman 	lsp->ls_canfree = (canfree != 0);
314703f27e8bSAndy Fiddaman 
3148406fc510SToomas Soome 	/*
3149*4b2d0200SRobert Mustacchi 	 * Notify we are ready to rock. If we are a labeled device, we must now
3150*4b2d0200SRobert Mustacchi 	 * ask cmlb to validate the label, which will finally create the right
3151*4b2d0200SRobert Mustacchi 	 * minors for us. In particular, this is a challenge because before
3152*4b2d0200SRobert Mustacchi 	 * ls_vp_ready was set, we couldn't even get the geometry count which
3153*4b2d0200SRobert Mustacchi 	 * means that we'll have the wrong default label if we have a larger
3154*4b2d0200SRobert Mustacchi 	 * device.
3155406fc510SToomas Soome 	 */
3156406fc510SToomas Soome 	mutex_enter(&lsp->ls_vp_lock);
3157406fc510SToomas Soome 	lsp->ls_vp_ready = B_TRUE;
3158406fc510SToomas Soome 	cv_broadcast(&lsp->ls_vp_cv);
3159406fc510SToomas Soome 	mutex_exit(&lsp->ls_vp_lock);
3160*4b2d0200SRobert Mustacchi 	if (lsp->ls_cmlbhandle != NULL) {
3161*4b2d0200SRobert Mustacchi 		(void) cmlb_validate(lsp->ls_cmlbhandle, 0, 0);
3162*4b2d0200SRobert Mustacchi 	}
31637c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
31640fbb751dSJohn Levon 
3165406fc510SToomas Soome 	lofi_copy_devpath(klip);
3166406fc510SToomas Soome 
31670fbb751dSJohn Levon 	if (rvalp)
3168406fc510SToomas Soome 		*rvalp = id;
3169bd07e074Sheppo 	(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
31707c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
31717c478bd9Sstevel@tonic-gate 	return (0);
31727c478bd9Sstevel@tonic-gate 
31730fbb751dSJohn Levon err:
31740fbb751dSJohn Levon 	if (lsp != NULL) {
31750fbb751dSJohn Levon 		lofi_destroy(lsp, credp);
31760fbb751dSJohn Levon 	} else {
31770fbb751dSJohn Levon 		if (vp != NULL) {
31782e974cb2SPatrick Mooney 			(void) VOP_PUTPAGE(vp, 0, 0, B_FREE, credp, NULL);
31797d82f0f8SDina K Nimeh 			(void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
31807d82f0f8SDina K Nimeh 			VN_RELE(vp);
31817d82f0f8SDina K Nimeh 		}
31820fbb751dSJohn Levon 	}
31830fbb751dSJohn Levon 
31847c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
31857c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
31867c478bd9Sstevel@tonic-gate 	return (error);
31877c478bd9Sstevel@tonic-gate }
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate /*
31907c478bd9Sstevel@tonic-gate  * unmap a file.
31917c478bd9Sstevel@tonic-gate  */
31927c478bd9Sstevel@tonic-gate static int
lofi_unmap_file(struct lofi_ioctl * ulip,int byfilename,struct cred * credp,int ioctl_flag)319367323fc4SJohn Levon lofi_unmap_file(struct lofi_ioctl *ulip, int byfilename,
3194bd07e074Sheppo     struct cred *credp, int ioctl_flag)
31957c478bd9Sstevel@tonic-gate {
31967c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
31977c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
319845ca5344SToomas Soome 	char namebuf[MAXNAMELEN];
31990fbb751dSJohn Levon 	int err;
32007c478bd9Sstevel@tonic-gate 
32010fbb751dSJohn Levon 	err = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
32020fbb751dSJohn Levon 	if (err != 0)
32030fbb751dSJohn Levon 		return (err);
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	mutex_enter(&lofi_lock);
32067c478bd9Sstevel@tonic-gate 	if (byfilename) {
3207cd69fabeSAlexander Eremin 		if ((err = file_to_lofi(klip->li_filename, klip->li_readonly,
3208cd69fabeSAlexander Eremin 		    &lsp)) != 0) {
3209b4844717SToomas Soome 			goto done;
32100fbb751dSJohn Levon 		}
3211406fc510SToomas Soome 	} else if (klip->li_id == 0) {
3212b4844717SToomas Soome 		err = ENXIO;
3213b4844717SToomas Soome 		goto done;
32147c478bd9Sstevel@tonic-gate 	} else {
3215406fc510SToomas Soome 		lsp = ddi_get_soft_state(lofi_statep, klip->li_id);
32167c478bd9Sstevel@tonic-gate 	}
32170fbb751dSJohn Levon 
32180fbb751dSJohn Levon 	if (lsp == NULL || lsp->ls_vp == NULL || lofi_access(lsp) != 0) {
3219b4844717SToomas Soome 		err = ENXIO;
3220b4844717SToomas Soome 		goto done;
32217c478bd9Sstevel@tonic-gate 	}
32220fbb751dSJohn Levon 
3223406fc510SToomas Soome 	klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
322445ca5344SToomas Soome 	(void) snprintf(namebuf, sizeof (namebuf), "%u", klip->li_id);
32253d7072f8Seschrock 
32263d7072f8Seschrock 	/*
322793239addSjohnlev 	 * If it's still held open, we'll do one of three things:
322893239addSjohnlev 	 *
322993239addSjohnlev 	 * If no flag is set, just return EBUSY.
323093239addSjohnlev 	 *
323193239addSjohnlev 	 * If the 'cleanup' flag is set, unmap and remove the device when
323293239addSjohnlev 	 * the last user finishes.
323393239addSjohnlev 	 *
323493239addSjohnlev 	 * If the 'force' flag is set, then we forcibly close the underlying
323593239addSjohnlev 	 * file.  Subsequent operations will fail, and the DKIOCSTATE ioctl
323693239addSjohnlev 	 * will return DKIO_DEV_GONE.  When the device is last closed, the
323793239addSjohnlev 	 * device will be cleaned up appropriately.
32383d7072f8Seschrock 	 *
32393d7072f8Seschrock 	 * This is complicated by the fact that we may have outstanding
324093239addSjohnlev 	 * dispatched I/Os.  Rather than having a single mutex to serialize all
32417d82f0f8SDina K Nimeh 	 * I/O, we keep a count of the number of outstanding I/O requests
32427d82f0f8SDina K Nimeh 	 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
32437d82f0f8SDina K Nimeh 	 * should be dispatched (ls_vp_closereq).
32447d82f0f8SDina K Nimeh 	 *
324593239addSjohnlev 	 * We set the flag, wait for the number of outstanding I/Os to reach 0,
324693239addSjohnlev 	 * and then close the underlying vnode.
32473d7072f8Seschrock 	 */
324893239addSjohnlev 	if (is_opened(lsp)) {
32493d7072f8Seschrock 		if (klip->li_force) {
3250b4844717SToomas Soome 			/* Mark the device for cleanup. */
3251b4844717SToomas Soome 			lofi_set_cleanup(lsp);
32523d7072f8Seschrock 			mutex_enter(&lsp->ls_vp_lock);
32533d7072f8Seschrock 			lsp->ls_vp_closereq = B_TRUE;
3254b4844717SToomas Soome 			/* Wake up any threads waiting on dkiocstate. */
3255b3388e4fSEric Taylor 			cv_broadcast(&lsp->ls_vp_cv);
32563d7072f8Seschrock 			while (lsp->ls_vp_iocount > 0)
32573d7072f8Seschrock 				cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
32583d7072f8Seschrock 			mutex_exit(&lsp->ls_vp_lock);
325993239addSjohnlev 		} else if (klip->li_cleanup) {
3260b4844717SToomas Soome 			lofi_set_cleanup(lsp);
3261b4844717SToomas Soome 		} else {
3262b4844717SToomas Soome 			err = EBUSY;
32633d7072f8Seschrock 		}
3264b4844717SToomas Soome 	} else {
3265406fc510SToomas Soome 		lofi_free_dev(lsp);
32660fbb751dSJohn Levon 		lofi_destroy(lsp, credp);
3267406fc510SToomas Soome 	}
3268406fc510SToomas Soome 
326945ca5344SToomas Soome 	/* Remove name from devlink cache */
327045ca5344SToomas Soome 	mutex_enter(&lofi_devlink_cache.ln_lock);
327145ca5344SToomas Soome 	(void) nvlist_remove_all(lofi_devlink_cache.ln_data, namebuf);
32729a1bf7f0SToomas Soome 	cv_broadcast(&lofi_devlink_cache.ln_cv);
327345ca5344SToomas Soome 	mutex_exit(&lofi_devlink_cache.ln_lock);
3274b4844717SToomas Soome done:
32757c478bd9Sstevel@tonic-gate 	mutex_exit(&lofi_lock);
3276b4844717SToomas Soome 	if (err == 0)
3277bd07e074Sheppo 		(void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
32787c478bd9Sstevel@tonic-gate 	free_lofi_ioctl(klip);
3279b4844717SToomas Soome 	return (err);
32807c478bd9Sstevel@tonic-gate }
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate /*
32837c478bd9Sstevel@tonic-gate  * get the filename given the minor number, or the minor number given
32847c478bd9Sstevel@tonic-gate  * the name.
32857c478bd9Sstevel@tonic-gate  */
32867c478bd9Sstevel@tonic-gate static int
lofi_get_info(dev_t dev __unused,struct lofi_ioctl * ulip,int which,struct cred * credp __unused,int ioctl_flag)328703f27e8bSAndy Fiddaman lofi_get_info(dev_t dev __unused, struct lofi_ioctl *ulip, int which,
328803f27e8bSAndy Fiddaman     struct cred *credp __unused, int ioctl_flag)
32897c478bd9Sstevel@tonic-gate {
32907c478bd9Sstevel@tonic-gate 	struct lofi_ioctl *klip;
32910fbb751dSJohn Levon 	struct lofi_state *lsp;
32927c478bd9Sstevel@tonic-gate 	int	error;
32937c478bd9Sstevel@tonic-gate 
32940fbb751dSJohn Levon 	error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
32950fbb751dSJohn Levon 	if (error != 0)
32960fbb751dSJohn Levon 		return (error);
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 	switch (which) {
32997c478bd9Sstevel@tonic-gate 	case LOFI_GET_FILENAME:
3300406fc510SToomas Soome 		if (klip->li_id == 0) {
33017c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
33027c478bd9Sstevel@tonic-gate 			return (EINVAL);
33037c478bd9Sstevel@tonic-gate 		}
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
3306406fc510SToomas Soome 		lsp = ddi_get_soft_state(lofi_statep, klip->li_id);
33070fbb751dSJohn Levon 		if (lsp == NULL || lofi_access(lsp) != 0) {
33087c478bd9Sstevel@tonic-gate 			mutex_exit(&lofi_lock);
33097c478bd9Sstevel@tonic-gate 			free_lofi_ioctl(klip);
33107c478bd9Sstevel@tonic-gate 			return (ENXIO);
33117c478bd9Sstevel@tonic-gate 		}
33120fbb751dSJohn Levon 
33130fbb751dSJohn Levon 		/*
33140fbb751dSJohn Levon 		 * This may fail if, for example, we're trying to look
33150fbb751dSJohn Levon 		 * up a zoned NFS path from the global zone.
33160fbb751dSJohn Levon 		 */
3317e31da757SToomas Soome 		if (lsp->ls_stacked_vp == NULL ||
3318e31da757SToomas Soome 		    vnodetopath(NULL, lsp->ls_stacked_vp, klip->li_filename,
33190fbb751dSJohn Levon 		    sizeof (klip->li_filename), CRED()) != 0) {
33200fbb751dSJohn Levon 			(void) strlcpy(klip->li_filename, "?",
33210fbb751dSJohn Levon 			    sizeof (klip->li_filename));
33220fbb751dSJohn Levon 		}
33230fbb751dSJohn Levon 
3324cd69fabeSAlexander Eremin 		klip->li_readonly = lsp->ls_readonly;
3325406fc510SToomas Soome 		klip->li_labeled = lsp->ls_cmlbhandle != NULL;
3326cd69fabeSAlexander Eremin 
332787117650Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
332887117650Saalok 		    sizeof (klip->li_algorithm));
33297d82f0f8SDina K Nimeh 		klip->li_crypto_enabled = lsp->ls_crypto_enabled;
33307c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
3331406fc510SToomas Soome 
3332406fc510SToomas Soome 		lofi_copy_devpath(klip);
3333bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
33347c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
33357c478bd9Sstevel@tonic-gate 		return (error);
33367c478bd9Sstevel@tonic-gate 	case LOFI_GET_MINOR:
33377c478bd9Sstevel@tonic-gate 		mutex_enter(&lofi_lock);
3338cd69fabeSAlexander Eremin 		error = file_to_lofi(klip->li_filename,
3339cd69fabeSAlexander Eremin 		    klip->li_readonly, &lsp);
3340406fc510SToomas Soome 		if (error != 0) {
3341406fc510SToomas Soome 			mutex_exit(&lofi_lock);
3342406fc510SToomas Soome 			free_lofi_ioctl(klip);
3343406fc510SToomas Soome 			return (error);
3344406fc510SToomas Soome 		}
3345406fc510SToomas Soome 		klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
3346406fc510SToomas Soome 
3347406fc510SToomas Soome 		klip->li_readonly = lsp->ls_readonly;
3348406fc510SToomas Soome 		klip->li_labeled = lsp->ls_cmlbhandle != NULL;
33497c478bd9Sstevel@tonic-gate 		mutex_exit(&lofi_lock);
33500fbb751dSJohn Levon 
3351406fc510SToomas Soome 		lofi_copy_devpath(klip);
3352bd07e074Sheppo 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
33530fbb751dSJohn Levon 
33547c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
33557c478bd9Sstevel@tonic-gate 		return (error);
335687117650Saalok 	case LOFI_CHECK_COMPRESSED:
335787117650Saalok 		mutex_enter(&lofi_lock);
3358cd69fabeSAlexander Eremin 		error = file_to_lofi(klip->li_filename,
3359cd69fabeSAlexander Eremin 		    klip->li_readonly, &lsp);
33600fbb751dSJohn Levon 		if (error != 0) {
336187117650Saalok 			mutex_exit(&lofi_lock);
336287117650Saalok 			free_lofi_ioctl(klip);
33630fbb751dSJohn Levon 			return (error);
336487117650Saalok 		}
336587117650Saalok 
3366406fc510SToomas Soome 		klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
336787117650Saalok 		(void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
336887117650Saalok 		    sizeof (klip->li_algorithm));
33690fbb751dSJohn Levon 
337087117650Saalok 		mutex_exit(&lofi_lock);
337187117650Saalok 		error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
337287117650Saalok 		free_lofi_ioctl(klip);
337387117650Saalok 		return (error);
33747c478bd9Sstevel@tonic-gate 	default:
33757c478bd9Sstevel@tonic-gate 		free_lofi_ioctl(klip);
33767c478bd9Sstevel@tonic-gate 		return (EINVAL);
33777c478bd9Sstevel@tonic-gate 	}
33787c478bd9Sstevel@tonic-gate }
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate static int
uscsi_is_inquiry(intptr_t arg,int flag,union scsi_cdb * cdb,struct uscsi_cmd * uscmd)3381406fc510SToomas Soome uscsi_is_inquiry(intptr_t arg, int flag, union scsi_cdb *cdb,
3382406fc510SToomas Soome     struct uscsi_cmd *uscmd)
3383406fc510SToomas Soome {
3384406fc510SToomas Soome 	int rval;
3385406fc510SToomas Soome 
3386406fc510SToomas Soome #ifdef	_MULTI_DATAMODEL
3387406fc510SToomas Soome 	switch (ddi_model_convert_from(flag & FMODELS)) {
3388406fc510SToomas Soome 	case DDI_MODEL_ILP32: {
3389406fc510SToomas Soome 		struct uscsi_cmd32 ucmd32;
3390406fc510SToomas Soome 
3391406fc510SToomas Soome 		if (ddi_copyin((void *)arg, &ucmd32, sizeof (ucmd32), flag)) {
3392406fc510SToomas Soome 			rval = EFAULT;
3393406fc510SToomas Soome 			goto err;
3394406fc510SToomas Soome 		}
3395406fc510SToomas Soome 		uscsi_cmd32touscsi_cmd((&ucmd32), uscmd);
3396406fc510SToomas Soome 		break;
3397406fc510SToomas Soome 	}
3398406fc510SToomas Soome 	case DDI_MODEL_NONE:
3399406fc510SToomas Soome 		if (ddi_copyin((void *)arg, uscmd, sizeof (*uscmd), flag)) {
3400406fc510SToomas Soome 			rval = EFAULT;
3401406fc510SToomas Soome 			goto err;
3402406fc510SToomas Soome 		}
3403406fc510SToomas Soome 		break;
3404406fc510SToomas Soome 	default:
3405406fc510SToomas Soome 		rval = EFAULT;
3406406fc510SToomas Soome 		goto err;
3407406fc510SToomas Soome 	}
3408406fc510SToomas Soome #else
3409406fc510SToomas Soome 	if (ddi_copyin((void *)arg, uscmd, sizeof (*uscmd), flag)) {
3410406fc510SToomas Soome 		rval = EFAULT;
3411406fc510SToomas Soome 		goto err;
3412406fc510SToomas Soome 	}
3413406fc510SToomas Soome #endif	/* _MULTI_DATAMODEL */
3414406fc510SToomas Soome 	if (ddi_copyin(uscmd->uscsi_cdb, cdb, uscmd->uscsi_cdblen, flag)) {
3415406fc510SToomas Soome 		rval = EFAULT;
3416406fc510SToomas Soome 		goto err;
3417406fc510SToomas Soome 	}
3418406fc510SToomas Soome 	if (cdb->scc_cmd == SCMD_INQUIRY) {
3419406fc510SToomas Soome 		return (0);
3420406fc510SToomas Soome 	}
3421406fc510SToomas Soome err:
3422406fc510SToomas Soome 	return (rval);
3423406fc510SToomas Soome }
3424406fc510SToomas Soome 
3425406fc510SToomas Soome static int
lofi_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * credp,int * rvalp)34267c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
34277c478bd9Sstevel@tonic-gate     int *rvalp)
34287c478bd9Sstevel@tonic-gate {
34297c478bd9Sstevel@tonic-gate 	int error;
34307c478bd9Sstevel@tonic-gate 	enum dkio_state dkstate;
34317c478bd9Sstevel@tonic-gate 	struct lofi_state *lsp;
3432935ca2ecSJohn Levon 	dk_efi_t user_efi;
3433406fc510SToomas Soome 	int id;
34347c478bd9Sstevel@tonic-gate 
3435406fc510SToomas Soome 	id = LOFI_MINOR2ID(getminor(dev));
3436406fc510SToomas Soome 
34377c478bd9Sstevel@tonic-gate 	/* lofi ioctls only apply to the master device */
3438406fc510SToomas Soome 	if (id == 0) {
34397c478bd9Sstevel@tonic-gate 		struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 		/*
34427c478bd9Sstevel@tonic-gate 		 * the query command only need read-access - i.e., normal
34437c478bd9Sstevel@tonic-gate 		 * users are allowed to do those on the ctl device as
34447c478bd9Sstevel@tonic-gate 		 * long as they can open it read-only.
34457c478bd9Sstevel@tonic-gate 		 */
34467c478bd9Sstevel@tonic-gate 		switch (cmd) {
34477c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE:
34487c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
34497c478bd9Sstevel@tonic-gate 				return (EPERM);
3450bd07e074Sheppo 			return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
34517c478bd9Sstevel@tonic-gate 		case LOFI_MAP_FILE_MINOR:
34527c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
34537c478bd9Sstevel@tonic-gate 				return (EPERM);
3454bd07e074Sheppo 			return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
34557c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE:
34567c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
34577c478bd9Sstevel@tonic-gate 				return (EPERM);
345867323fc4SJohn Levon 			return (lofi_unmap_file(lip, 1, credp, flag));
34597c478bd9Sstevel@tonic-gate 		case LOFI_UNMAP_FILE_MINOR:
34607c478bd9Sstevel@tonic-gate 			if ((flag & FWRITE) == 0)
34617c478bd9Sstevel@tonic-gate 				return (EPERM);
346267323fc4SJohn Levon 			return (lofi_unmap_file(lip, 0, credp, flag));
34637c478bd9Sstevel@tonic-gate 		case LOFI_GET_FILENAME:
34647c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
3465bd07e074Sheppo 			    credp, flag));
34667c478bd9Sstevel@tonic-gate 		case LOFI_GET_MINOR:
34677c478bd9Sstevel@tonic-gate 			return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
3468bd07e074Sheppo 			    credp, flag));
34690fbb751dSJohn Levon 
34700fbb751dSJohn Levon 		/*
34710fbb751dSJohn Levon 		 * This API made limited sense when this value was fixed
34720fbb751dSJohn Levon 		 * at LOFI_MAX_FILES.  However, its use to iterate
34730fbb751dSJohn Levon 		 * across all possible devices in lofiadm means we don't
3474406fc510SToomas Soome 		 * want to return L_MAXMIN, but the highest
3475406fc510SToomas Soome 		 * *allocated* id.
34760fbb751dSJohn Levon 		 */
34777c478bd9Sstevel@tonic-gate 		case LOFI_GET_MAXMINOR:
3478406fc510SToomas Soome 			id = 0;
34790fbb751dSJohn Levon 
34800fbb751dSJohn Levon 			mutex_enter(&lofi_lock);
34810fbb751dSJohn Levon 
34820fbb751dSJohn Levon 			for (lsp = list_head(&lofi_list); lsp != NULL;
34830fbb751dSJohn Levon 			    lsp = list_next(&lofi_list, lsp)) {
3484406fc510SToomas Soome 				int i;
34850fbb751dSJohn Levon 				if (lofi_access(lsp) != 0)
34860fbb751dSJohn Levon 					continue;
34870fbb751dSJohn Levon 
3488406fc510SToomas Soome 				i = ddi_get_instance(lsp->ls_dip);
3489406fc510SToomas Soome 				if (i > id)
3490406fc510SToomas Soome 					id = i;
34910fbb751dSJohn Levon 			}
34920fbb751dSJohn Levon 
34930fbb751dSJohn Levon 			mutex_exit(&lofi_lock);
34940fbb751dSJohn Levon 
3495406fc510SToomas Soome 			error = ddi_copyout(&id, &lip->li_id,
3496406fc510SToomas Soome 			    sizeof (id), flag);
34977c478bd9Sstevel@tonic-gate 			if (error)
34987c478bd9Sstevel@tonic-gate 				return (EFAULT);
34997c478bd9Sstevel@tonic-gate 			return (0);
35000fbb751dSJohn Levon 
350187117650Saalok 		case LOFI_CHECK_COMPRESSED:
350287117650Saalok 			return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
350387117650Saalok 			    credp, flag));
35047c478bd9Sstevel@tonic-gate 		default:
35050fbb751dSJohn Levon 			return (EINVAL);
35067c478bd9Sstevel@tonic-gate 		}
35077c478bd9Sstevel@tonic-gate 	}
35087c478bd9Sstevel@tonic-gate 
3509b3388e4fSEric Taylor 	mutex_enter(&lofi_lock);
3510406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, id);
3511b4844717SToomas Soome 	if (lsp == NULL || lsp->ls_cleanup) {
3512b3388e4fSEric Taylor 		mutex_exit(&lofi_lock);
35137c478bd9Sstevel@tonic-gate 		return (ENXIO);
3514b3388e4fSEric Taylor 	}
3515b3388e4fSEric Taylor 	mutex_exit(&lofi_lock);
35167c478bd9Sstevel@tonic-gate 
3517406fc510SToomas Soome 	if (ddi_prop_exists(DDI_DEV_T_ANY, lsp->ls_dip, DDI_PROP_DONTPASS,
3518406fc510SToomas Soome 	    "labeled") == 1) {
3519406fc510SToomas Soome 		error = cmlb_ioctl(lsp->ls_cmlbhandle, dev, cmd, arg, flag,
3520406fc510SToomas Soome 		    credp, rvalp, 0);
3521406fc510SToomas Soome 		if (error != ENOTTY)
3522406fc510SToomas Soome 			return (error);
3523406fc510SToomas Soome 	}
3524406fc510SToomas Soome 
35253d7072f8Seschrock 	/*
35263d7072f8Seschrock 	 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
35273d7072f8Seschrock 	 * EIO as if the device was no longer present.
35283d7072f8Seschrock 	 */
35293d7072f8Seschrock 	if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
35303d7072f8Seschrock 		return (EIO);
35313d7072f8Seschrock 
35327c478bd9Sstevel@tonic-gate 	/* these are for faking out utilities like newfs */
35337c478bd9Sstevel@tonic-gate 	switch (cmd) {
3534406fc510SToomas Soome 	case DKIOCGMEDIAINFO:
3535406fc510SToomas Soome 	case DKIOCGMEDIAINFOEXT: {
3536406fc510SToomas Soome 		struct dk_minfo_ext media_info;
3537406fc510SToomas Soome 		int shift = lsp->ls_lbshift;
3538406fc510SToomas Soome 		int size;
3539406fc510SToomas Soome 
3540406fc510SToomas Soome 		if (cmd == DKIOCGMEDIAINFOEXT) {
3541406fc510SToomas Soome 			media_info.dki_pbsize = 1U << lsp->ls_pbshift;
35427b421453SRobert Mustacchi 			switch (ddi_model_convert_from(flag & FMODELS)) {
35437b421453SRobert Mustacchi 			case DDI_MODEL_ILP32:
35447b421453SRobert Mustacchi 				size = sizeof (struct dk_minfo_ext32);
35457b421453SRobert Mustacchi 				break;
35467b421453SRobert Mustacchi 			default:
3547406fc510SToomas Soome 				size = sizeof (struct dk_minfo_ext);
35487b421453SRobert Mustacchi 				break;
35497b421453SRobert Mustacchi 			}
3550406fc510SToomas Soome 		} else {
3551406fc510SToomas Soome 			size = sizeof (struct dk_minfo);
3552406fc510SToomas Soome 		}
3553406fc510SToomas Soome 
3554406fc510SToomas Soome 		media_info.dki_media_type = DK_FIXED_DISK;
3555406fc510SToomas Soome 		media_info.dki_lbsize = 1U << shift;
3556406fc510SToomas Soome 		media_info.dki_capacity =
3557406fc510SToomas Soome 		    (lsp->ls_vp_size - lsp->ls_crypto_offset) >> shift;
3558406fc510SToomas Soome 
3559406fc510SToomas Soome 		if (ddi_copyout(&media_info, (void *)arg, size, flag))
3560406fc510SToomas Soome 			return (EFAULT);
3561406fc510SToomas Soome 		return (0);
3562406fc510SToomas Soome 	}
3563406fc510SToomas Soome 	case DKIOCREMOVABLE: {
3564406fc510SToomas Soome 		int i = 0;
3565406fc510SToomas Soome 		if (ddi_copyout(&i, (caddr_t)arg, sizeof (int), flag))
3566406fc510SToomas Soome 			return (EFAULT);
3567406fc510SToomas Soome 		return (0);
3568406fc510SToomas Soome 	}
3569406fc510SToomas Soome 
3570406fc510SToomas Soome 	case DKIOCGVTOC: {
3571406fc510SToomas Soome 		struct vtoc vt;
3572406fc510SToomas Soome 		fake_disk_vtoc(lsp, &vt);
3573406fc510SToomas Soome 
35747c478bd9Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
35757c478bd9Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
35767c478bd9Sstevel@tonic-gate 			struct vtoc32 vtoc32;
35777c478bd9Sstevel@tonic-gate 
3578406fc510SToomas Soome 			vtoctovtoc32(vt, vtoc32);
35797c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
35807c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
35817c478bd9Sstevel@tonic-gate 				return (EFAULT);
35827c478bd9Sstevel@tonic-gate 			break;
35837c478bd9Sstevel@tonic-gate 			}
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 		case DDI_MODEL_NONE:
3586406fc510SToomas Soome 			if (ddi_copyout(&vt, (void *)arg,
35877c478bd9Sstevel@tonic-gate 			    sizeof (struct vtoc), flag))
35887c478bd9Sstevel@tonic-gate 				return (EFAULT);
35897c478bd9Sstevel@tonic-gate 			break;
35907c478bd9Sstevel@tonic-gate 		}
35917c478bd9Sstevel@tonic-gate 		return (0);
3592406fc510SToomas Soome 	}
3593406fc510SToomas Soome 	case DKIOCINFO: {
3594406fc510SToomas Soome 		struct dk_cinfo ci;
3595406fc510SToomas Soome 		fake_disk_info(dev, &ci);
3596406fc510SToomas Soome 		if (ddi_copyout(&ci, (void *)arg, sizeof (ci), flag))
35977c478bd9Sstevel@tonic-gate 			return (EFAULT);
35987c478bd9Sstevel@tonic-gate 		return (0);
3599406fc510SToomas Soome 	}
36007c478bd9Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
36017c478bd9Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
36027c478bd9Sstevel@tonic-gate 	case DKIOCGGEOM:
3603bd07e074Sheppo 		error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
3604bd07e074Sheppo 		    sizeof (struct dk_geom), flag);
36057c478bd9Sstevel@tonic-gate 		if (error)
36067c478bd9Sstevel@tonic-gate 			return (EFAULT);
36077c478bd9Sstevel@tonic-gate 		return (0);
36087c478bd9Sstevel@tonic-gate 	case DKIOCSTATE:
36093d7072f8Seschrock 		/*
36103d7072f8Seschrock 		 * Normally, lofi devices are always in the INSERTED state.  If
36113d7072f8Seschrock 		 * a device is forcefully unmapped, then the device transitions
36123d7072f8Seschrock 		 * to the DKIO_DEV_GONE state.
36133d7072f8Seschrock 		 */
36143d7072f8Seschrock 		if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
36153d7072f8Seschrock 		    flag) != 0)
36163d7072f8Seschrock 			return (EFAULT);
36173d7072f8Seschrock 
36183d7072f8Seschrock 		mutex_enter(&lsp->ls_vp_lock);
3619b3388e4fSEric Taylor 		while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
3620b3388e4fSEric Taylor 		    (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
3621b4844717SToomas Soome 		    !lsp->ls_cleanup) {
36223d7072f8Seschrock 			/*
36233d7072f8Seschrock 			 * By virtue of having the device open, we know that
36243d7072f8Seschrock 			 * 'lsp' will remain valid when we return.
36253d7072f8Seschrock 			 */
3626b4844717SToomas Soome 			if (!cv_wait_sig(&lsp->ls_vp_cv, &lsp->ls_vp_lock)) {
36273d7072f8Seschrock 				mutex_exit(&lsp->ls_vp_lock);
36283d7072f8Seschrock 				return (EINTR);
36293d7072f8Seschrock 			}
36303d7072f8Seschrock 		}
36313d7072f8Seschrock 
3632b4844717SToomas Soome 		dkstate = (!lsp->ls_cleanup && lsp->ls_vp != NULL ?
3633b3388e4fSEric Taylor 		    DKIO_INSERTED : DKIO_DEV_GONE);
36343d7072f8Seschrock 		mutex_exit(&lsp->ls_vp_lock);
36353d7072f8Seschrock 
36363d7072f8Seschrock 		if (ddi_copyout(&dkstate, (void *)arg,
36373d7072f8Seschrock 		    sizeof (dkstate), flag) != 0)
36387c478bd9Sstevel@tonic-gate 			return (EFAULT);
36397c478bd9Sstevel@tonic-gate 		return (0);
3640406fc510SToomas Soome 	case USCSICMD: {
3641406fc510SToomas Soome 		struct uscsi_cmd uscmd;
3642406fc510SToomas Soome 		union scsi_cdb cdb;
3643406fc510SToomas Soome 
3644406fc510SToomas Soome 		if (uscsi_is_inquiry(arg, flag, &cdb, &uscmd) == 0) {
3645406fc510SToomas Soome 			struct scsi_inquiry inq = {0};
3646406fc510SToomas Soome 
3647406fc510SToomas Soome 			lofi_create_inquiry(lsp, &inq);
3648406fc510SToomas Soome 			if (ddi_copyout(&inq, uscmd.uscsi_bufaddr,
3649406fc510SToomas Soome 			    uscmd.uscsi_buflen, flag) != 0)
3650406fc510SToomas Soome 				return (EFAULT);
3651406fc510SToomas Soome 			return (0);
3652406fc510SToomas Soome 		} else if (cdb.scc_cmd == SCMD_READ_CAPACITY) {
3653406fc510SToomas Soome 			struct scsi_capacity capacity;
3654406fc510SToomas Soome 
3655406fc510SToomas Soome 			capacity.capacity =
3656406fc510SToomas Soome 			    BE_32((lsp->ls_vp_size - lsp->ls_crypto_offset) >>
3657406fc510SToomas Soome 			    lsp->ls_lbshift);
3658406fc510SToomas Soome 			capacity.lbasize = BE_32(1 << lsp->ls_lbshift);
3659406fc510SToomas Soome 			if (ddi_copyout(&capacity, uscmd.uscsi_bufaddr,
3660406fc510SToomas Soome 			    uscmd.uscsi_buflen, flag) != 0)
3661406fc510SToomas Soome 				return (EFAULT);
3662406fc510SToomas Soome 			return (0);
3663406fc510SToomas Soome 		}
3664406fc510SToomas Soome 
3665406fc510SToomas Soome 		uscmd.uscsi_rqstatus = 0xff;
3666406fc510SToomas Soome #ifdef	_MULTI_DATAMODEL
3667406fc510SToomas Soome 		switch (ddi_model_convert_from(flag & FMODELS)) {
3668406fc510SToomas Soome 		case DDI_MODEL_ILP32: {
3669406fc510SToomas Soome 			struct uscsi_cmd32 ucmd32;
3670406fc510SToomas Soome 			uscsi_cmdtouscsi_cmd32((&uscmd), (&ucmd32));
3671406fc510SToomas Soome 			if (ddi_copyout(&ucmd32, (void *)arg, sizeof (ucmd32),
3672406fc510SToomas Soome 			    flag) != 0)
3673406fc510SToomas Soome 				return (EFAULT);
3674406fc510SToomas Soome 			break;
3675406fc510SToomas Soome 		}
3676406fc510SToomas Soome 		case DDI_MODEL_NONE:
3677406fc510SToomas Soome 			if (ddi_copyout(&uscmd, (void *)arg, sizeof (uscmd),
3678406fc510SToomas Soome 			    flag) != 0)
3679406fc510SToomas Soome 				return (EFAULT);
3680406fc510SToomas Soome 			break;
36817c478bd9Sstevel@tonic-gate 		default:
3682406fc510SToomas Soome 			return (EFAULT);
3683406fc510SToomas Soome 		}
3684406fc510SToomas Soome #else
3685406fc510SToomas Soome 		if (ddi_copyout(&uscmd, (void *)arg, sizeof (uscmd), flag) != 0)
3686406fc510SToomas Soome 			return (EFAULT);
3687406fc510SToomas Soome #endif	/* _MULTI_DATAMODEL */
3688406fc510SToomas Soome 		return (0);
3689406fc510SToomas Soome 	}
3690935ca2ecSJohn Levon 
3691935ca2ecSJohn Levon 	case DKIOCGMBOOT:
3692935ca2ecSJohn Levon 		return (lofi_urw(lsp, FREAD, 0, 1 << lsp->ls_lbshift,
3693935ca2ecSJohn Levon 		    arg, flag, credp));
3694935ca2ecSJohn Levon 
3695935ca2ecSJohn Levon 	case DKIOCSMBOOT:
3696935ca2ecSJohn Levon 		return (lofi_urw(lsp, FWRITE, 0, 1 << lsp->ls_lbshift,
3697935ca2ecSJohn Levon 		    arg, flag, credp));
3698935ca2ecSJohn Levon 
3699935ca2ecSJohn Levon 	case DKIOCGETEFI:
3700935ca2ecSJohn Levon 		if (ddi_copyin((void *)arg, &user_efi,
3701935ca2ecSJohn Levon 		    sizeof (dk_efi_t), flag) != 0)
3702935ca2ecSJohn Levon 			return (EFAULT);
3703935ca2ecSJohn Levon 
3704935ca2ecSJohn Levon 		return (lofi_urw(lsp, FREAD,
3705935ca2ecSJohn Levon 		    user_efi.dki_lba * (1 << lsp->ls_lbshift),
3706935ca2ecSJohn Levon 		    user_efi.dki_length, (intptr_t)user_efi.dki_data,
3707935ca2ecSJohn Levon 		    flag, credp));
3708935ca2ecSJohn Levon 
3709935ca2ecSJohn Levon 	case DKIOCSETEFI:
3710935ca2ecSJohn Levon 		if (ddi_copyin((void *)arg, &user_efi,
3711935ca2ecSJohn Levon 		    sizeof (dk_efi_t), flag) != 0)
3712935ca2ecSJohn Levon 			return (EFAULT);
3713935ca2ecSJohn Levon 
3714935ca2ecSJohn Levon 		return (lofi_urw(lsp, FWRITE,
3715935ca2ecSJohn Levon 		    user_efi.dki_lba * (1 << lsp->ls_lbshift),
3716935ca2ecSJohn Levon 		    user_efi.dki_length, (intptr_t)user_efi.dki_data,
3717935ca2ecSJohn Levon 		    flag, credp));
3718935ca2ecSJohn Levon 
371903f27e8bSAndy Fiddaman 	case DKIOC_CANFREE: {
372003f27e8bSAndy Fiddaman 		int canfree = lsp->ls_canfree ? 1 : 0;
372103f27e8bSAndy Fiddaman 
372203f27e8bSAndy Fiddaman 		if (ddi_copyout(&canfree, (void *)arg, sizeof (canfree), flag))
372303f27e8bSAndy Fiddaman 			return (EFAULT);
372403f27e8bSAndy Fiddaman 
372503f27e8bSAndy Fiddaman 		return (0);
372603f27e8bSAndy Fiddaman 	}
372703f27e8bSAndy Fiddaman 
372803f27e8bSAndy Fiddaman 	case DKIOCFREE: {
372903f27e8bSAndy Fiddaman 		dkioc_free_list_t *dfl;
373003f27e8bSAndy Fiddaman 
373103f27e8bSAndy Fiddaman 		if (!lsp->ls_canfree)
373203f27e8bSAndy Fiddaman 			return (ENOTSUP);
373303f27e8bSAndy Fiddaman 
373403f27e8bSAndy Fiddaman 		error = dfl_copyin((void *)arg, &dfl, flag, KM_SLEEP);
373503f27e8bSAndy Fiddaman 		if (error != 0)
373603f27e8bSAndy Fiddaman 			return (error);
373703f27e8bSAndy Fiddaman 
373803f27e8bSAndy Fiddaman 		/*
373903f27e8bSAndy Fiddaman 		 * lofi_free_space() calls dfl_iter() which consumes dfl;
374003f27e8bSAndy Fiddaman 		 * there is no need to call dfl_free() here.
374103f27e8bSAndy Fiddaman 		 */
374203f27e8bSAndy Fiddaman 		return (lofi_free_space(lsp, dfl, dev));
374303f27e8bSAndy Fiddaman 	}
374403f27e8bSAndy Fiddaman 
3745406fc510SToomas Soome 	default:
3746406fc510SToomas Soome #ifdef DEBUG
3747406fc510SToomas Soome 		cmn_err(CE_WARN, "lofi_ioctl: %d is not implemented\n", cmd);
3748406fc510SToomas Soome #endif	/* DEBUG */
37497c478bd9Sstevel@tonic-gate 		return (ENOTTY);
37507c478bd9Sstevel@tonic-gate 	}
37517c478bd9Sstevel@tonic-gate }
37527c478bd9Sstevel@tonic-gate 
3753406fc510SToomas Soome static int
lofi_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int mod_flags,char * name,caddr_t valuep,int * lengthp)3754406fc510SToomas Soome lofi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
3755406fc510SToomas Soome     char *name, caddr_t valuep, int *lengthp)
3756406fc510SToomas Soome {
3757406fc510SToomas Soome 	struct lofi_state *lsp;
3758450b24a3SToomas Soome 	int rc;
3759406fc510SToomas Soome 
3760406fc510SToomas Soome 	lsp = ddi_get_soft_state(lofi_statep, ddi_get_instance(dip));
3761406fc510SToomas Soome 	if (lsp == NULL) {
3762406fc510SToomas Soome 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
3763406fc510SToomas Soome 		    name, valuep, lengthp));
3764406fc510SToomas Soome 	}
3765406fc510SToomas Soome 
3766450b24a3SToomas Soome 	rc = cmlb_prop_op(lsp->ls_cmlbhandle, dev, dip, prop_op, mod_flags,
3767450b24a3SToomas Soome 	    name, valuep, lengthp, LOFI_PART(getminor(dev)), NULL);
3768450b24a3SToomas Soome 	if (rc == DDI_PROP_SUCCESS)
3769450b24a3SToomas Soome 		return (rc);
3770450b24a3SToomas Soome 
3771450b24a3SToomas Soome 	return (ddi_prop_op(DDI_DEV_T_ANY, dip, prop_op, mod_flags,
3772450b24a3SToomas Soome 	    name, valuep, lengthp));
3773406fc510SToomas Soome }
3774406fc510SToomas Soome 
37757c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
37767c478bd9Sstevel@tonic-gate 	lofi_open,		/* open */
37777c478bd9Sstevel@tonic-gate 	lofi_close,		/* close */
37787c478bd9Sstevel@tonic-gate 	lofi_strategy,		/* strategy */
37797c478bd9Sstevel@tonic-gate 	nodev,			/* print */
37807c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
37817c478bd9Sstevel@tonic-gate 	lofi_read,		/* read */
37827c478bd9Sstevel@tonic-gate 	lofi_write,		/* write */
37837c478bd9Sstevel@tonic-gate 	lofi_ioctl,		/* ioctl */
37847c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
37857c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
37867c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
37877c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
3788406fc510SToomas Soome 	lofi_prop_op,		/* prop_op */
37897c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
37907c478bd9Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP,	/* Driver compatibility flag */
37917c478bd9Sstevel@tonic-gate 	CB_REV,
37927c478bd9Sstevel@tonic-gate 	lofi_aread,
37937c478bd9Sstevel@tonic-gate 	lofi_awrite
37947c478bd9Sstevel@tonic-gate };
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = {
37977c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
37987c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
37997c478bd9Sstevel@tonic-gate 	lofi_info,		/* info */
38007c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
38017c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
38027c478bd9Sstevel@tonic-gate 	lofi_attach,		/* attach */
38037c478bd9Sstevel@tonic-gate 	lofi_detach,		/* detach */
38047c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
38057c478bd9Sstevel@tonic-gate 	&lofi_cb_ops,		/* driver operations */
380619397407SSherry Moore 	NULL,			/* no bus operations */
380719397407SSherry Moore 	NULL,			/* power */
380819397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
38097c478bd9Sstevel@tonic-gate };
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
38127c478bd9Sstevel@tonic-gate 	&mod_driverops,
381319397407SSherry Moore 	"loopback file driver",
38147c478bd9Sstevel@tonic-gate 	&lofi_ops,
38157c478bd9Sstevel@tonic-gate };
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
38187c478bd9Sstevel@tonic-gate 	MODREV_1,
38197c478bd9Sstevel@tonic-gate 	&modldrv,
38207c478bd9Sstevel@tonic-gate 	NULL
38217c478bd9Sstevel@tonic-gate };
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate int
_init(void)38247c478bd9Sstevel@tonic-gate _init(void)
38257c478bd9Sstevel@tonic-gate {
38267c478bd9Sstevel@tonic-gate 	int error;
38277c478bd9Sstevel@tonic-gate 
38280fbb751dSJohn Levon 	list_create(&lofi_list, sizeof (struct lofi_state),
38290fbb751dSJohn Levon 	    offsetof(struct lofi_state, ls_list));
38300fbb751dSJohn Levon 
3831406fc510SToomas Soome 	error = ddi_soft_state_init((void **)&lofi_statep,
38327c478bd9Sstevel@tonic-gate 	    sizeof (struct lofi_state), 0);
3833406fc510SToomas Soome 	if (error) {
3834406fc510SToomas Soome 		list_destroy(&lofi_list);
38357c478bd9Sstevel@tonic-gate 		return (error);
3836406fc510SToomas Soome 	}
3837406fc510SToomas Soome 
3838406fc510SToomas Soome 	/*
3839406fc510SToomas Soome 	 * The minor number is stored as id << LOFI_CMLB_SHIFT as
3840406fc510SToomas Soome 	 * we need to reserve space for cmlb minor numbers.
3841406fc510SToomas Soome 	 * This will leave out 4096 id values on 32bit kernel, which should
3842406fc510SToomas Soome 	 * still suffice.
3843406fc510SToomas Soome 	 */
3844406fc510SToomas Soome 	lofi_id = id_space_create("lofi_id", 1,
3845406fc510SToomas Soome 	    (1 << (L_BITSMINOR - LOFI_CMLB_SHIFT)));
3846406fc510SToomas Soome 
3847406fc510SToomas Soome 	if (lofi_id == NULL) {
3848406fc510SToomas Soome 		ddi_soft_state_fini((void **)&lofi_statep);
3849406fc510SToomas Soome 		list_destroy(&lofi_list);
3850406fc510SToomas Soome 		return (DDI_FAILURE);
3851406fc510SToomas Soome 	}
38527c478bd9Sstevel@tonic-gate 
38537c478bd9Sstevel@tonic-gate 	mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
38540fbb751dSJohn Levon 
38557c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
38568ae05c10SToomas Soome 
38577c478bd9Sstevel@tonic-gate 	if (error) {
3858406fc510SToomas Soome 		id_space_destroy(lofi_id);
38597c478bd9Sstevel@tonic-gate 		mutex_destroy(&lofi_lock);
3860406fc510SToomas Soome 		ddi_soft_state_fini((void **)&lofi_statep);
38610fbb751dSJohn Levon 		list_destroy(&lofi_list);
38627c478bd9Sstevel@tonic-gate 	}
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 	return (error);
38657c478bd9Sstevel@tonic-gate }
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate int
_fini(void)38687c478bd9Sstevel@tonic-gate _fini(void)
38697c478bd9Sstevel@tonic-gate {
38707c478bd9Sstevel@tonic-gate 	int	error;
38717c478bd9Sstevel@tonic-gate 
38720fbb751dSJohn Levon 	mutex_enter(&lofi_lock);
38730fbb751dSJohn Levon 
38740fbb751dSJohn Levon 	if (!list_is_empty(&lofi_list)) {
38750fbb751dSJohn Levon 		mutex_exit(&lofi_lock);
38767c478bd9Sstevel@tonic-gate 		return (EBUSY);
38770fbb751dSJohn Levon 	}
38780fbb751dSJohn Levon 
38790fbb751dSJohn Levon 	mutex_exit(&lofi_lock);
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
38827c478bd9Sstevel@tonic-gate 	if (error)
38837c478bd9Sstevel@tonic-gate 		return (error);
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate 	mutex_destroy(&lofi_lock);
3886406fc510SToomas Soome 	id_space_destroy(lofi_id);
3887406fc510SToomas Soome 	ddi_soft_state_fini((void **)&lofi_statep);
38880fbb751dSJohn Levon 	list_destroy(&lofi_list);
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 	return (error);
38917c478bd9Sstevel@tonic-gate }
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)38947c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
38957c478bd9Sstevel@tonic-gate {
38967c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
38977c478bd9Sstevel@tonic-gate }
3898