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.
25*a29e56d9SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com>
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * lofi (loopback file) driver - allows you to attach a file to a device,
307c478bd9Sstevel@tonic-gate * which can then be accessed through that device. The simple model is that
317c478bd9Sstevel@tonic-gate * you tell lofi to open a file, and then use the block device you get as
327c478bd9Sstevel@tonic-gate * you would any block device. lofi translates access to the block device
337c478bd9Sstevel@tonic-gate * into I/O on the underlying file. This is mostly useful for
347c478bd9Sstevel@tonic-gate * mounting images of filesystems.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * lofi is controlled through /dev/lofictl - this is the only device exported
37*a29e56d9SToomas Soome * during attach, and is instance number 0. lofiadm communicates with lofi
38*a29e56d9SToomas Soome * through ioctls on this device. When a file is attached to lofi, block and
39*a29e56d9SToomas Soome * character devices are exported in /dev/lofi and /dev/rlofi. These devices
40*a29e56d9SToomas Soome * are identified by lofi instance number, and the instance number is also used
41*a29e56d9SToomas Soome * as the name in /dev/lofi.
42*a29e56d9SToomas Soome *
43*a29e56d9SToomas Soome * Virtual disks, or, labeled lofi, implements virtual disk support to
44*a29e56d9SToomas Soome * support partition table and related tools. Such mappings will cause
45*a29e56d9SToomas Soome * block and character devices to be exported in /dev/dsk and /dev/rdsk
46*a29e56d9SToomas Soome * directories.
47*a29e56d9SToomas Soome *
48*a29e56d9SToomas Soome * To support virtual disks, the instance number space is divided to two
49*a29e56d9SToomas Soome * parts, upper part for instance number and lower part for minor number
50*a29e56d9SToomas Soome * space to identify partitions and slices. The virtual disk support is
51*a29e56d9SToomas Soome * implemented by stacking cmlb module. For virtual disks, the partition
52*a29e56d9SToomas Soome * related ioctl calls are routed to cmlb module. Compression and encryption
53*a29e56d9SToomas Soome * is not supported for virtual disks.
54*a29e56d9SToomas Soome *
55*a29e56d9SToomas Soome * Mapped devices are tracked with state structures handled with
567c478bd9Sstevel@tonic-gate * ddi_soft_state(9F) for simplicity.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * A file attached to lofi is opened when attached and not closed until
597c478bd9Sstevel@tonic-gate * explicitly detached from lofi. This seems more sensible than deferring
607c478bd9Sstevel@tonic-gate * the open until the /dev/lofi device is opened, for a number of reasons.
617c478bd9Sstevel@tonic-gate * One is that any failure is likely to be noticed by the person (or script)
627c478bd9Sstevel@tonic-gate * running lofiadm. Another is that it would be a security problem if the
637c478bd9Sstevel@tonic-gate * file was replaced by another one after being added but before being opened.
647c478bd9Sstevel@tonic-gate *
657c478bd9Sstevel@tonic-gate * The only hard part about lofi is the ioctls. In order to support things
667c478bd9Sstevel@tonic-gate * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
677c478bd9Sstevel@tonic-gate * So it has to fake disk geometry and partition information. More may need
687c478bd9Sstevel@tonic-gate * to be faked if your favorite utility doesn't work and you think it should
697c478bd9Sstevel@tonic-gate * (fdformat doesn't work because it really wants to know the type of floppy
707c478bd9Sstevel@tonic-gate * controller to talk to, and that didn't seem easy to fake. Or possibly even
717c478bd9Sstevel@tonic-gate * necessary, since we have mkfs_pcfs now).
727c478bd9Sstevel@tonic-gate *
733d7072f8Seschrock * Normally, a lofi device cannot be detached if it is open (i.e. busy). To
743d7072f8Seschrock * support simulation of hotplug events, an optional force flag is provided.
753d7072f8Seschrock * If a lofi device is open when a force detach is requested, then the
763d7072f8Seschrock * underlying file is closed and any subsequent operations return EIO. When the
773d7072f8Seschrock * device is closed for the last time, it will be cleaned up at that time. In
783d7072f8Seschrock * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
793d7072f8Seschrock * detached but not removed.
803d7072f8Seschrock *
817c478bd9Sstevel@tonic-gate * Known problems:
827c478bd9Sstevel@tonic-gate *
837c478bd9Sstevel@tonic-gate * UFS logging. Mounting a UFS filesystem image "logging"
847c478bd9Sstevel@tonic-gate * works for basic copy testing but wedges during a build of ON through
857c478bd9Sstevel@tonic-gate * that image. Some deadlock in lufs holding the log mutex and then
867c478bd9Sstevel@tonic-gate * getting stuck on a buf. So for now, don't do that.
877c478bd9Sstevel@tonic-gate *
887c478bd9Sstevel@tonic-gate * Direct I/O. Since the filesystem data is being cached in the buffer
897c478bd9Sstevel@tonic-gate * cache, _and_ again in the underlying filesystem, it's tempting to
907c478bd9Sstevel@tonic-gate * enable direct I/O on the underlying file. Don't, because that deadlocks.
917c478bd9Sstevel@tonic-gate * I think to fix the cache-twice problem we might need filesystem support.
927c478bd9Sstevel@tonic-gate *
937c478bd9Sstevel@tonic-gate * Interesting things to do:
947c478bd9Sstevel@tonic-gate *
957c478bd9Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically.
967c478bd9Sstevel@tonic-gate *
977c478bd9Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not
987c478bd9Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't
997d82f0f8SDina K Nimeh * need to fake a geometry, however, it may be relevant if you're replacing
1007d82f0f8SDina K Nimeh * metadisk, or using lofi to get crypto.
1017d82f0f8SDina K Nimeh * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
1027d82f0f8SDina K Nimeh * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
1037d82f0f8SDina K Nimeh * In fact this even makes sense if you have lofi "above" metadisk.
1047c478bd9Sstevel@tonic-gate *
1057d82f0f8SDina K Nimeh * Encryption:
1067d82f0f8SDina K Nimeh * Each lofi device can have its own symmetric key and cipher.
1077d82f0f8SDina K Nimeh * They are passed to us by lofiadm(1m) in the correct format for use
1087d82f0f8SDina K Nimeh * with the misc/kcf crypto_* routines.
1097d82f0f8SDina K Nimeh *
1107d82f0f8SDina K Nimeh * Each block has its own IV, that is calculated in lofi_blk_mech(), based
1117d82f0f8SDina K Nimeh * on the "master" key held in the lsp and the block number of the buffer.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate #include <sys/types.h>
11587117650Saalok #include <netinet/in.h>
1167c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1177c478bd9Sstevel@tonic-gate #include <sys/uio.h>
1187c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1197c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1207c478bd9Sstevel@tonic-gate #include <sys/mman.h>
1217c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1227c478bd9Sstevel@tonic-gate #include <sys/aio_req.h>
1237c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1247c478bd9Sstevel@tonic-gate #include <sys/file.h>
1257c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1267c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1277c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1287c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
1297c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
1307c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
1317c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1327c478bd9Sstevel@tonic-gate #include <sys/filio.h>
1337c478bd9Sstevel@tonic-gate #include <sys/fdio.h>
1347c478bd9Sstevel@tonic-gate #include <sys/open.h>
1357c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1367c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
13987117650Saalok #include <sys/zmod.h>
1400fbb751dSJohn Levon #include <sys/id_space.h>
1410fbb751dSJohn Levon #include <sys/mkdev.h>
1427d82f0f8SDina K Nimeh #include <sys/crypto/common.h>
1437d82f0f8SDina K Nimeh #include <sys/crypto/api.h>
1440fbb751dSJohn Levon #include <sys/rctl.h>
145*a29e56d9SToomas Soome #include <sys/vtoc.h>
146*a29e56d9SToomas Soome #include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */
147*a29e56d9SToomas Soome #include <sys/scsi/impl/uscsi.h>
148*a29e56d9SToomas Soome #include <sys/sysevent/dev.h>
149b1efbcd6SAlok Aggarwal #include <LzmaDec.h>
1507d82f0f8SDina K Nimeh
1517d82f0f8SDina K Nimeh /*
1527d82f0f8SDina K Nimeh * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
1537d82f0f8SDina K Nimeh * Crypto metadata, if it exists, is located at the end of the boot block
1547d82f0f8SDina K Nimeh * (BBOFF + BBSIZE, which is SBOFF). The super block and everything after
1557d82f0f8SDina K Nimeh * is offset by the size of the crypto metadata which is handled by
1567d82f0f8SDina K Nimeh * lsp->ls_crypto_offset.
1577d82f0f8SDina K Nimeh */
1587d82f0f8SDina K Nimeh #define CRYOFF ((off_t)8192)
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks"
1617c478bd9Sstevel@tonic-gate #define SIZE_PROP_NAME "Size"
1620fbb751dSJohn Levon #define ZONE_PROP_NAME "zone"
1637c478bd9Sstevel@tonic-gate
1647d82f0f8SDina K Nimeh #define SETUP_C_DATA(cd, buf, len) \
1657d82f0f8SDina K Nimeh (cd).cd_format = CRYPTO_DATA_RAW; \
1667d82f0f8SDina K Nimeh (cd).cd_offset = 0; \
1677d82f0f8SDina K Nimeh (cd).cd_miscdata = NULL; \
1687d82f0f8SDina K Nimeh (cd).cd_length = (len); \
1697d82f0f8SDina K Nimeh (cd).cd_raw.iov_base = (buf); \
1707d82f0f8SDina K Nimeh (cd).cd_raw.iov_len = (len);
1717d82f0f8SDina K Nimeh
1727d82f0f8SDina K Nimeh #define UIO_CHECK(uio) \
1737d82f0f8SDina K Nimeh if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
1747d82f0f8SDina K Nimeh ((uio)->uio_resid % DEV_BSIZE) != 0) { \
1757d82f0f8SDina K Nimeh return (EINVAL); \
1767d82f0f8SDina K Nimeh }
1777d82f0f8SDina K Nimeh
178*a29e56d9SToomas Soome #define DEVFS_CHANNEL "devfsadm_event_channel"
179*a29e56d9SToomas Soome #define LOFI_TIMEOUT 30
180*a29e56d9SToomas Soome static evchan_t *lofi_chan;
181*a29e56d9SToomas Soome static kmutex_t lofi_chan_lock;
182*a29e56d9SToomas Soome static kcondvar_t lofi_chan_cv;
183*a29e56d9SToomas Soome static nvlist_t *lofi_devlink_cache;
184*a29e56d9SToomas Soome
185*a29e56d9SToomas Soome static void *lofi_statep;
1867c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */
187*a29e56d9SToomas Soome static id_space_t *lofi_id; /* lofi ID values */
1880fbb751dSJohn Levon static list_t lofi_list;
1890fbb751dSJohn Levon static zone_key_t lofi_zone_key;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1937c478bd9Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1947c478bd9Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy,
1957c478bd9Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of
1967c478bd9Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1977c478bd9Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should
1987c478bd9Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the
1997c478bd9Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic
2007c478bd9Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on
2017c478bd9Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE.
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
2047c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */
2057c478bd9Sstevel@tonic-gate
2067d82f0f8SDina K Nimeh const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
2077c478bd9Sstevel@tonic-gate
2084058a205Sjrgn.keil@googlemail.com /*
2094058a205Sjrgn.keil@googlemail.com * To avoid decompressing data in a compressed segment multiple times
2104058a205Sjrgn.keil@googlemail.com * when accessing small parts of a segment's data, we cache and reuse
2114058a205Sjrgn.keil@googlemail.com * the uncompressed segment's data.
2124058a205Sjrgn.keil@googlemail.com *
2134058a205Sjrgn.keil@googlemail.com * A single cached segment is sufficient to avoid lots of duplicate
2144058a205Sjrgn.keil@googlemail.com * segment decompress operations. A small cache size also reduces the
2154058a205Sjrgn.keil@googlemail.com * memory footprint.
2164058a205Sjrgn.keil@googlemail.com *
2174058a205Sjrgn.keil@googlemail.com * lofi_max_comp_cache is the maximum number of decompressed data segments
2184058a205Sjrgn.keil@googlemail.com * cached for each compressed lofi image. It can be set to 0 to disable
2194058a205Sjrgn.keil@googlemail.com * caching.
2204058a205Sjrgn.keil@googlemail.com */
2214058a205Sjrgn.keil@googlemail.com
2224058a205Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1;
2234058a205Sjrgn.keil@googlemail.com
22487117650Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
22587117650Saalok size_t *destlen, int level);
22687117650Saalok
227b1efbcd6SAlok Aggarwal static int lzma_decompress(void *src, size_t srclen, void *dst,
228b1efbcd6SAlok Aggarwal size_t *dstlen, int level);
229b1efbcd6SAlok Aggarwal
23087117650Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
23187117650Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */
23287117650Saalok {gzip_decompress, NULL, 6, "gzip-6"},
233b1efbcd6SAlok Aggarwal {gzip_decompress, NULL, 9, "gzip-9"},
234b1efbcd6SAlok Aggarwal {lzma_decompress, NULL, 0, "lzma"}
23587117650Saalok };
23687117650Saalok
237*a29e56d9SToomas Soome static void lofi_strategy_task(void *);
238*a29e56d9SToomas Soome static int lofi_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t,
239*a29e56d9SToomas Soome size_t, void *);
240*a29e56d9SToomas Soome static int lofi_tg_getinfo(dev_info_t *, int, void *, void *);
241*a29e56d9SToomas Soome
242*a29e56d9SToomas Soome struct cmlb_tg_ops lofi_tg_ops = {
243*a29e56d9SToomas Soome TG_DK_OPS_VERSION_1,
244*a29e56d9SToomas Soome lofi_tg_rdwr,
245*a29e56d9SToomas Soome lofi_tg_getinfo
246*a29e56d9SToomas Soome };
247*a29e56d9SToomas Soome
248b1efbcd6SAlok Aggarwal /*ARGSUSED*/
249b1efbcd6SAlok Aggarwal static void
SzAlloc(void * p,size_t size)250b1efbcd6SAlok Aggarwal *SzAlloc(void *p, size_t size)
251b1efbcd6SAlok Aggarwal {
252b1efbcd6SAlok Aggarwal return (kmem_alloc(size, KM_SLEEP));
253b1efbcd6SAlok Aggarwal }
254b1efbcd6SAlok Aggarwal
255b1efbcd6SAlok Aggarwal /*ARGSUSED*/
256b1efbcd6SAlok Aggarwal static void
SzFree(void * p,void * address,size_t size)257b1efbcd6SAlok Aggarwal SzFree(void *p, void *address, size_t size)
258b1efbcd6SAlok Aggarwal {
259b1efbcd6SAlok Aggarwal kmem_free(address, size);
260b1efbcd6SAlok Aggarwal }
261b1efbcd6SAlok Aggarwal
262b1efbcd6SAlok Aggarwal static ISzAlloc g_Alloc = { SzAlloc, SzFree };
263b1efbcd6SAlok Aggarwal
2644058a205Sjrgn.keil@googlemail.com /*
2654058a205Sjrgn.keil@googlemail.com * Free data referenced by the linked list of cached uncompressed
2664058a205Sjrgn.keil@googlemail.com * segments.
2674058a205Sjrgn.keil@googlemail.com */
2684058a205Sjrgn.keil@googlemail.com static void
lofi_free_comp_cache(struct lofi_state * lsp)2694058a205Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp)
2704058a205Sjrgn.keil@googlemail.com {
2714058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
2724058a205Sjrgn.keil@googlemail.com
2734058a205Sjrgn.keil@googlemail.com while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
2744058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
2754058a205Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache));
2764058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--;
2774058a205Sjrgn.keil@googlemail.com }
2784058a205Sjrgn.keil@googlemail.com ASSERT(lsp->ls_comp_cache_count == 0);
2794058a205Sjrgn.keil@googlemail.com }
2804058a205Sjrgn.keil@googlemail.com
2817c478bd9Sstevel@tonic-gate static int
is_opened(struct lofi_state * lsp)2827c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2837c478bd9Sstevel@tonic-gate {
284*a29e56d9SToomas Soome int i;
285*a29e56d9SToomas Soome boolean_t last = B_TRUE;
286*a29e56d9SToomas Soome
2870fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
288*a29e56d9SToomas Soome for (i = 0; i < LOFI_PART_MAX; i++) {
289*a29e56d9SToomas Soome if (lsp->ls_open_lyr[i]) {
290*a29e56d9SToomas Soome last = B_FALSE;
291*a29e56d9SToomas Soome break;
292*a29e56d9SToomas Soome }
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
295*a29e56d9SToomas Soome for (i = 0; last && (i < OTYP_LYR); i++) {
296*a29e56d9SToomas Soome if (lsp->ls_open_reg[i]) {
297*a29e56d9SToomas Soome last = B_FALSE;
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
301*a29e56d9SToomas Soome return (!last);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate
3043d7072f8Seschrock static void
lofi_free_crypto(struct lofi_state * lsp)3057d82f0f8SDina K Nimeh lofi_free_crypto(struct lofi_state *lsp)
3067d82f0f8SDina K Nimeh {
3070fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
3087d82f0f8SDina K Nimeh
3097d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) {
3107d82f0f8SDina K Nimeh /*
3117d82f0f8SDina K Nimeh * Clean up the crypto state so that it doesn't hang around
3127d82f0f8SDina K Nimeh * in memory after we are done with it.
3137d82f0f8SDina K Nimeh */
3140fbb751dSJohn Levon if (lsp->ls_key.ck_data != NULL) {
3157d82f0f8SDina K Nimeh bzero(lsp->ls_key.ck_data,
3167d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3177d82f0f8SDina K Nimeh kmem_free(lsp->ls_key.ck_data,
3187d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
3197d82f0f8SDina K Nimeh lsp->ls_key.ck_data = NULL;
3207d82f0f8SDina K Nimeh lsp->ls_key.ck_length = 0;
3210fbb751dSJohn Levon }
3227d82f0f8SDina K Nimeh
3237d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != NULL) {
3247d82f0f8SDina K Nimeh kmem_free(lsp->ls_mech.cm_param,
3257d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len);
3267d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = NULL;
3277d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = 0;
3287d82f0f8SDina K Nimeh }
3297d82f0f8SDina K Nimeh
3307d82f0f8SDina K Nimeh if (lsp->ls_iv_mech.cm_param != NULL) {
3317d82f0f8SDina K Nimeh kmem_free(lsp->ls_iv_mech.cm_param,
3327d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len);
3337d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param = NULL;
3347d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len = 0;
3357d82f0f8SDina K Nimeh }
3367d82f0f8SDina K Nimeh
3377d82f0f8SDina K Nimeh mutex_destroy(&lsp->ls_crypto_lock);
3387d82f0f8SDina K Nimeh }
3397d82f0f8SDina K Nimeh }
3407d82f0f8SDina K Nimeh
341*a29e56d9SToomas Soome /* ARGSUSED */
342*a29e56d9SToomas Soome static int
lofi_tg_rdwr(dev_info_t * dip,uchar_t cmd,void * bufaddr,diskaddr_t start,size_t length,void * tg_cookie)343*a29e56d9SToomas Soome lofi_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start,
344*a29e56d9SToomas Soome size_t length, void *tg_cookie)
345*a29e56d9SToomas Soome {
346*a29e56d9SToomas Soome struct lofi_state *lsp;
347*a29e56d9SToomas Soome buf_t *bp;
348*a29e56d9SToomas Soome int instance;
349*a29e56d9SToomas Soome int rv = 0;
350*a29e56d9SToomas Soome
351*a29e56d9SToomas Soome instance = ddi_get_instance(dip);
352*a29e56d9SToomas Soome if (instance == 0) /* control node does not have disk */
353*a29e56d9SToomas Soome return (ENXIO);
354*a29e56d9SToomas Soome
355*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, instance);
356*a29e56d9SToomas Soome
357*a29e56d9SToomas Soome if (lsp == NULL)
358*a29e56d9SToomas Soome return (ENXIO);
359*a29e56d9SToomas Soome
360*a29e56d9SToomas Soome if (cmd != TG_READ && cmd != TG_WRITE)
361*a29e56d9SToomas Soome return (EINVAL);
362*a29e56d9SToomas Soome
363*a29e56d9SToomas Soome /*
364*a29e56d9SToomas Soome * Make sure the mapping is set up by checking lsp->ls_vp_ready.
365*a29e56d9SToomas Soome */
366*a29e56d9SToomas Soome mutex_enter(&lsp->ls_vp_lock);
367*a29e56d9SToomas Soome while (lsp->ls_vp_ready == B_FALSE)
368*a29e56d9SToomas Soome cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
369*a29e56d9SToomas Soome mutex_exit(&lsp->ls_vp_lock);
370*a29e56d9SToomas Soome
371*a29e56d9SToomas Soome if (P2PHASE(length, (1U << lsp->ls_lbshift)) != 0) {
372*a29e56d9SToomas Soome /* We can only transfer whole blocks at a time! */
373*a29e56d9SToomas Soome return (EINVAL);
374*a29e56d9SToomas Soome }
375*a29e56d9SToomas Soome
376*a29e56d9SToomas Soome bp = getrbuf(KM_SLEEP);
377*a29e56d9SToomas Soome
378*a29e56d9SToomas Soome if (cmd == TG_READ) {
379*a29e56d9SToomas Soome bp->b_flags = B_READ;
380*a29e56d9SToomas Soome } else {
381*a29e56d9SToomas Soome if (lsp->ls_readonly == B_TRUE) {
382*a29e56d9SToomas Soome freerbuf(bp);
383*a29e56d9SToomas Soome return (EROFS);
384*a29e56d9SToomas Soome }
385*a29e56d9SToomas Soome bp->b_flags = B_WRITE;
386*a29e56d9SToomas Soome }
387*a29e56d9SToomas Soome
388*a29e56d9SToomas Soome bp->b_un.b_addr = bufaddr;
389*a29e56d9SToomas Soome bp->b_bcount = length;
390*a29e56d9SToomas Soome bp->b_lblkno = start;
391*a29e56d9SToomas Soome bp->b_private = NULL;
392*a29e56d9SToomas Soome bp->b_edev = lsp->ls_dev;
393*a29e56d9SToomas Soome
394*a29e56d9SToomas Soome if (lsp->ls_kstat) {
395*a29e56d9SToomas Soome mutex_enter(lsp->ls_kstat->ks_lock);
396*a29e56d9SToomas Soome kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
397*a29e56d9SToomas Soome mutex_exit(lsp->ls_kstat->ks_lock);
398*a29e56d9SToomas Soome }
399*a29e56d9SToomas Soome (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
400*a29e56d9SToomas Soome (void) biowait(bp);
401*a29e56d9SToomas Soome
402*a29e56d9SToomas Soome rv = geterror(bp);
403*a29e56d9SToomas Soome freerbuf(bp);
404*a29e56d9SToomas Soome return (rv);
405*a29e56d9SToomas Soome }
406*a29e56d9SToomas Soome
407*a29e56d9SToomas Soome /*
408*a29e56d9SToomas Soome * Get device geometry info for cmlb.
409*a29e56d9SToomas Soome *
410*a29e56d9SToomas Soome * We have mapped disk image as virtual block device and have to report
411*a29e56d9SToomas Soome * physical/virtual geometry to cmlb.
412*a29e56d9SToomas Soome *
413*a29e56d9SToomas Soome * So we have two principal cases:
414*a29e56d9SToomas Soome * 1. Uninitialised image without any existing labels,
415*a29e56d9SToomas Soome * for this case we fabricate the data based on mapped image.
416*a29e56d9SToomas Soome * 2. Image with existing label information.
417*a29e56d9SToomas Soome * Since we have no information how the image was created (it may be
418*a29e56d9SToomas Soome * dump from some physical device), we need to rely on label information
419*a29e56d9SToomas Soome * from image, or we get "corrupted label" errors.
420*a29e56d9SToomas Soome * NOTE: label can be MBR, MBR+SMI, GPT
421*a29e56d9SToomas Soome */
422*a29e56d9SToomas Soome static int
lofi_tg_getinfo(dev_info_t * dip,int cmd,void * arg,void * tg_cookie)423*a29e56d9SToomas Soome lofi_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie)
424*a29e56d9SToomas Soome {
425*a29e56d9SToomas Soome struct lofi_state *lsp;
426*a29e56d9SToomas Soome int instance;
427*a29e56d9SToomas Soome int ashift;
428*a29e56d9SToomas Soome
429*a29e56d9SToomas Soome _NOTE(ARGUNUSED(tg_cookie));
430*a29e56d9SToomas Soome instance = ddi_get_instance(dip);
431*a29e56d9SToomas Soome if (instance == 0) /* control device has no storage */
432*a29e56d9SToomas Soome return (ENXIO);
433*a29e56d9SToomas Soome
434*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, instance);
435*a29e56d9SToomas Soome
436*a29e56d9SToomas Soome if (lsp == NULL)
437*a29e56d9SToomas Soome return (ENXIO);
438*a29e56d9SToomas Soome
439*a29e56d9SToomas Soome /*
440*a29e56d9SToomas Soome * Make sure the mapping is set up by checking lsp->ls_vp_ready.
441*a29e56d9SToomas Soome *
442*a29e56d9SToomas Soome * When mapping is created, new lofi instance is created and
443*a29e56d9SToomas Soome * lofi_attach() will call cmlb_attach() as part of the procedure
444*a29e56d9SToomas Soome * to set the mapping up. This chain of events will happen in
445*a29e56d9SToomas Soome * the same thread.
446*a29e56d9SToomas Soome * Since cmlb_attach() will call lofi_tg_getinfo to get
447*a29e56d9SToomas Soome * capacity, we return error on that call if cookie is set,
448*a29e56d9SToomas Soome * otherwise lofi_attach will be stuck as the mapping is not yet
449*a29e56d9SToomas Soome * finalized and lofi is not yet ready.
450*a29e56d9SToomas Soome * Note, such error is not fatal for cmlb, as the label setup
451*a29e56d9SToomas Soome * will be finalized when cmlb_validate() is called.
452*a29e56d9SToomas Soome */
453*a29e56d9SToomas Soome mutex_enter(&lsp->ls_vp_lock);
454*a29e56d9SToomas Soome if (tg_cookie != NULL && lsp->ls_vp_ready == B_FALSE) {
455*a29e56d9SToomas Soome mutex_exit(&lsp->ls_vp_lock);
456*a29e56d9SToomas Soome return (ENXIO);
457*a29e56d9SToomas Soome }
458*a29e56d9SToomas Soome while (lsp->ls_vp_ready == B_FALSE)
459*a29e56d9SToomas Soome cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
460*a29e56d9SToomas Soome mutex_exit(&lsp->ls_vp_lock);
461*a29e56d9SToomas Soome
462*a29e56d9SToomas Soome ashift = lsp->ls_lbshift;
463*a29e56d9SToomas Soome
464*a29e56d9SToomas Soome switch (cmd) {
465*a29e56d9SToomas Soome case TG_GETPHYGEOM: {
466*a29e56d9SToomas Soome cmlb_geom_t *geomp = arg;
467*a29e56d9SToomas Soome
468*a29e56d9SToomas Soome geomp->g_capacity =
469*a29e56d9SToomas Soome (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
470*a29e56d9SToomas Soome geomp->g_nsect = lsp->ls_dkg.dkg_nsect;
471*a29e56d9SToomas Soome geomp->g_nhead = lsp->ls_dkg.dkg_nhead;
472*a29e56d9SToomas Soome geomp->g_acyl = lsp->ls_dkg.dkg_acyl;
473*a29e56d9SToomas Soome geomp->g_ncyl = lsp->ls_dkg.dkg_ncyl;
474*a29e56d9SToomas Soome geomp->g_secsize = (1U << ashift);
475*a29e56d9SToomas Soome geomp->g_intrlv = lsp->ls_dkg.dkg_intrlv;
476*a29e56d9SToomas Soome geomp->g_rpm = lsp->ls_dkg.dkg_rpm;
477*a29e56d9SToomas Soome return (0);
478*a29e56d9SToomas Soome }
479*a29e56d9SToomas Soome
480*a29e56d9SToomas Soome case TG_GETCAPACITY:
481*a29e56d9SToomas Soome *(diskaddr_t *)arg =
482*a29e56d9SToomas Soome (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
483*a29e56d9SToomas Soome return (0);
484*a29e56d9SToomas Soome
485*a29e56d9SToomas Soome case TG_GETBLOCKSIZE:
486*a29e56d9SToomas Soome *(uint32_t *)arg = (1U << ashift);
487*a29e56d9SToomas Soome return (0);
488*a29e56d9SToomas Soome
489*a29e56d9SToomas Soome case TG_GETATTR: {
490*a29e56d9SToomas Soome tg_attribute_t *tgattr = arg;
491*a29e56d9SToomas Soome
492*a29e56d9SToomas Soome tgattr->media_is_writable = !lsp->ls_readonly;
493*a29e56d9SToomas Soome tgattr->media_is_solid_state = B_FALSE;
494*a29e56d9SToomas Soome return (0);
495*a29e56d9SToomas Soome }
496*a29e56d9SToomas Soome
497*a29e56d9SToomas Soome default:
498*a29e56d9SToomas Soome return (EINVAL);
499*a29e56d9SToomas Soome }
500*a29e56d9SToomas Soome }
501*a29e56d9SToomas Soome
5027d82f0f8SDina K Nimeh static void
lofi_destroy(struct lofi_state * lsp,cred_t * credp)5030fbb751dSJohn Levon lofi_destroy(struct lofi_state *lsp, cred_t *credp)
5043d7072f8Seschrock {
505*a29e56d9SToomas Soome int id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
506b9c7fb03SAlok Aggarwal int i;
5073d7072f8Seschrock
5080fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
5090fbb751dSJohn Levon
5100fbb751dSJohn Levon list_remove(&lofi_list, lsp);
5117d82f0f8SDina K Nimeh
5127d82f0f8SDina K Nimeh lofi_free_crypto(lsp);
5137d82f0f8SDina K Nimeh
514b9c7fb03SAlok Aggarwal /*
515b9c7fb03SAlok Aggarwal * Free pre-allocated compressed buffers
516b9c7fb03SAlok Aggarwal */
517b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs != NULL) {
518b9c7fb03SAlok Aggarwal for (i = 0; i < lofi_taskq_nthreads; i++) {
519b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[i].bufsize > 0)
520b9c7fb03SAlok Aggarwal kmem_free(lsp->ls_comp_bufs[i].buf,
521b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[i].bufsize);
522b9c7fb03SAlok Aggarwal }
523b9c7fb03SAlok Aggarwal kmem_free(lsp->ls_comp_bufs,
524b9c7fb03SAlok Aggarwal sizeof (struct compbuf) * lofi_taskq_nthreads);
525b9c7fb03SAlok Aggarwal }
526b9c7fb03SAlok Aggarwal
527*a29e56d9SToomas Soome if (lsp->ls_vp != NULL) {
528*a29e56d9SToomas Soome (void) VOP_PUTPAGE(lsp->ls_vp, 0, 0, B_INVAL, credp, NULL);
5290fbb751dSJohn Levon (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
5300fbb751dSJohn Levon 1, 0, credp, NULL);
5310fbb751dSJohn Levon VN_RELE(lsp->ls_vp);
532*a29e56d9SToomas Soome }
5330fbb751dSJohn Levon if (lsp->ls_stacked_vp != lsp->ls_vp)
5340fbb751dSJohn Levon VN_RELE(lsp->ls_stacked_vp);
5350fbb751dSJohn Levon
536*a29e56d9SToomas Soome if (lsp->ls_taskq != NULL)
5370fbb751dSJohn Levon taskq_destroy(lsp->ls_taskq);
5380fbb751dSJohn Levon
5390fbb751dSJohn Levon if (lsp->ls_kstat != NULL)
5400fbb751dSJohn Levon kstat_delete(lsp->ls_kstat);
5410fbb751dSJohn Levon
5420fbb751dSJohn Levon /*
5430fbb751dSJohn Levon * Free cached decompressed segment data
5440fbb751dSJohn Levon */
5450fbb751dSJohn Levon lofi_free_comp_cache(lsp);
5460fbb751dSJohn Levon list_destroy(&lsp->ls_comp_cache);
5470fbb751dSJohn Levon
5480fbb751dSJohn Levon if (lsp->ls_uncomp_seg_sz > 0) {
5490fbb751dSJohn Levon kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
5500fbb751dSJohn Levon lsp->ls_uncomp_seg_sz = 0;
5510fbb751dSJohn Levon }
5520fbb751dSJohn Levon
553a19609f8Sjv227347 rctl_decr_lofi(lsp->ls_zone.zref_zone, 1);
554a19609f8Sjv227347 zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
5550fbb751dSJohn Levon
5560fbb751dSJohn Levon mutex_destroy(&lsp->ls_comp_cache_lock);
5570fbb751dSJohn Levon mutex_destroy(&lsp->ls_comp_bufs_lock);
5580fbb751dSJohn Levon mutex_destroy(&lsp->ls_kstat_lock);
5594058a205Sjrgn.keil@googlemail.com mutex_destroy(&lsp->ls_vp_lock);
560*a29e56d9SToomas Soome cv_destroy(&lsp->ls_vp_cv);
561*a29e56d9SToomas Soome lsp->ls_vp_ready = B_FALSE;
5624058a205Sjrgn.keil@googlemail.com
563*a29e56d9SToomas Soome ASSERT(ddi_get_soft_state(lofi_statep, id) == lsp);
564*a29e56d9SToomas Soome (void) ndi_devi_offline(lsp->ls_dip, NDI_DEVI_REMOVE);
565*a29e56d9SToomas Soome id_free(lofi_id, id);
5660fbb751dSJohn Levon }
5670fbb751dSJohn Levon
5680fbb751dSJohn Levon static void
lofi_free_dev(struct lofi_state * lsp)569*a29e56d9SToomas Soome lofi_free_dev(struct lofi_state *lsp)
5700fbb751dSJohn Levon {
5710fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
5720fbb751dSJohn Levon
573*a29e56d9SToomas Soome if (lsp->ls_cmlbhandle != NULL) {
574*a29e56d9SToomas Soome cmlb_invalidate(lsp->ls_cmlbhandle, 0);
575*a29e56d9SToomas Soome cmlb_detach(lsp->ls_cmlbhandle, 0);
576*a29e56d9SToomas Soome cmlb_free_handle(&lsp->ls_cmlbhandle);
577*a29e56d9SToomas Soome lsp->ls_cmlbhandle = NULL;
578*a29e56d9SToomas Soome }
579*a29e56d9SToomas Soome (void) ddi_prop_remove_all(lsp->ls_dip);
580*a29e56d9SToomas Soome ddi_remove_minor_node(lsp->ls_dip, NULL);
5810fbb751dSJohn Levon }
5820fbb751dSJohn Levon
5830fbb751dSJohn Levon /*ARGSUSED*/
5840fbb751dSJohn Levon static void
lofi_zone_shutdown(zoneid_t zoneid,void * arg)5850fbb751dSJohn Levon lofi_zone_shutdown(zoneid_t zoneid, void *arg)
5860fbb751dSJohn Levon {
5870fbb751dSJohn Levon struct lofi_state *lsp;
5880fbb751dSJohn Levon struct lofi_state *next;
5890fbb751dSJohn Levon
5900fbb751dSJohn Levon mutex_enter(&lofi_lock);
5910fbb751dSJohn Levon
5920fbb751dSJohn Levon for (lsp = list_head(&lofi_list); lsp != NULL; lsp = next) {
5930fbb751dSJohn Levon
5940fbb751dSJohn Levon /* lofi_destroy() frees lsp */
5950fbb751dSJohn Levon next = list_next(&lofi_list, lsp);
5960fbb751dSJohn Levon
597a19609f8Sjv227347 if (lsp->ls_zone.zref_zone->zone_id != zoneid)
5980fbb751dSJohn Levon continue;
5990fbb751dSJohn Levon
6000fbb751dSJohn Levon /*
6010fbb751dSJohn Levon * No in-zone processes are running, but something has this
6020fbb751dSJohn Levon * open. It's either a global zone process, or a lofi
6030fbb751dSJohn Levon * mount. In either case we set ls_cleanup so the last
6040fbb751dSJohn Levon * user destroys the device.
6050fbb751dSJohn Levon */
6060fbb751dSJohn Levon if (is_opened(lsp)) {
6070fbb751dSJohn Levon lsp->ls_cleanup = 1;
6080fbb751dSJohn Levon } else {
609*a29e56d9SToomas Soome lofi_free_dev(lsp);
6100fbb751dSJohn Levon lofi_destroy(lsp, kcred);
6110fbb751dSJohn Levon }
6120fbb751dSJohn Levon }
6130fbb751dSJohn Levon
6140fbb751dSJohn Levon mutex_exit(&lofi_lock);
6153d7072f8Seschrock }
6163d7072f8Seschrock
6173d7072f8Seschrock /*ARGSUSED*/
6187c478bd9Sstevel@tonic-gate static int
lofi_open(dev_t * devp,int flag,int otyp,struct cred * credp)6197c478bd9Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
6207c478bd9Sstevel@tonic-gate {
621*a29e56d9SToomas Soome int id;
622*a29e56d9SToomas Soome minor_t part;
623*a29e56d9SToomas Soome uint64_t mask;
624*a29e56d9SToomas Soome diskaddr_t nblks;
625*a29e56d9SToomas Soome diskaddr_t lba;
626*a29e56d9SToomas Soome boolean_t ndelay;
627*a29e56d9SToomas Soome
6287c478bd9Sstevel@tonic-gate struct lofi_state *lsp;
6297c478bd9Sstevel@tonic-gate
630*a29e56d9SToomas Soome if (otyp >= OTYPCNT)
631*a29e56d9SToomas Soome return (EINVAL);
632*a29e56d9SToomas Soome
633*a29e56d9SToomas Soome ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
634*a29e56d9SToomas Soome
6350fbb751dSJohn Levon /*
6360fbb751dSJohn Levon * lofiadm -a /dev/lofi/1 gets us here.
6370fbb751dSJohn Levon */
6380fbb751dSJohn Levon if (mutex_owner(&lofi_lock) == curthread)
6397c478bd9Sstevel@tonic-gate return (EINVAL);
6400fbb751dSJohn Levon
6410fbb751dSJohn Levon mutex_enter(&lofi_lock);
6420fbb751dSJohn Levon
643*a29e56d9SToomas Soome id = LOFI_MINOR2ID(getminor(*devp));
644*a29e56d9SToomas Soome part = LOFI_PART(getminor(*devp));
645*a29e56d9SToomas Soome mask = (1U << part);
6460fbb751dSJohn Levon
6470fbb751dSJohn Levon /* master control device */
648*a29e56d9SToomas Soome if (id == 0) {
6497c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
6507c478bd9Sstevel@tonic-gate return (0);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate /* otherwise, the mapping should already exist */
654*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, id);
6557c478bd9Sstevel@tonic-gate if (lsp == NULL) {
6567c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
6577c478bd9Sstevel@tonic-gate return (EINVAL);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6603d7072f8Seschrock if (lsp->ls_vp == NULL) {
6613d7072f8Seschrock mutex_exit(&lofi_lock);
6623d7072f8Seschrock return (ENXIO);
6633d7072f8Seschrock }
6643d7072f8Seschrock
665cd69fabeSAlexander Eremin if (lsp->ls_readonly && (flag & FWRITE)) {
666cd69fabeSAlexander Eremin mutex_exit(&lofi_lock);
667cd69fabeSAlexander Eremin return (EROFS);
668cd69fabeSAlexander Eremin }
669cd69fabeSAlexander Eremin
670*a29e56d9SToomas Soome if ((lsp->ls_open_excl) & (mask)) {
671a67ac136SAlexander Eremin mutex_exit(&lofi_lock);
672*a29e56d9SToomas Soome return (EBUSY);
673*a29e56d9SToomas Soome }
674*a29e56d9SToomas Soome
675*a29e56d9SToomas Soome if (flag & FEXCL) {
676*a29e56d9SToomas Soome if (lsp->ls_open_lyr[part]) {
677*a29e56d9SToomas Soome mutex_exit(&lofi_lock);
678*a29e56d9SToomas Soome return (EBUSY);
679*a29e56d9SToomas Soome }
680*a29e56d9SToomas Soome for (int i = 0; i < OTYP_LYR; i++) {
681*a29e56d9SToomas Soome if (lsp->ls_open_reg[i] & mask) {
682*a29e56d9SToomas Soome mutex_exit(&lofi_lock);
683*a29e56d9SToomas Soome return (EBUSY);
684*a29e56d9SToomas Soome }
685*a29e56d9SToomas Soome }
686*a29e56d9SToomas Soome }
687*a29e56d9SToomas Soome
688*a29e56d9SToomas Soome if (lsp->ls_cmlbhandle != NULL) {
689*a29e56d9SToomas Soome if (cmlb_validate(lsp->ls_cmlbhandle, 0, 0) != 0) {
690*a29e56d9SToomas Soome /*
691*a29e56d9SToomas Soome * non-blocking opens are allowed to succeed to
692*a29e56d9SToomas Soome * support format and fdisk to create partitioning.
693*a29e56d9SToomas Soome */
694*a29e56d9SToomas Soome if (!ndelay) {
695*a29e56d9SToomas Soome mutex_exit(&lofi_lock);
696*a29e56d9SToomas Soome return (ENXIO);
697*a29e56d9SToomas Soome }
698*a29e56d9SToomas Soome } else if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &nblks, &lba,
699*a29e56d9SToomas Soome NULL, NULL, 0) == 0) {
700*a29e56d9SToomas Soome if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
701*a29e56d9SToomas Soome mutex_exit(&lofi_lock);
702*a29e56d9SToomas Soome return (ENXIO);
703*a29e56d9SToomas Soome }
704*a29e56d9SToomas Soome } else if (!ndelay) {
705*a29e56d9SToomas Soome mutex_exit(&lofi_lock);
706*a29e56d9SToomas Soome return (ENXIO);
707*a29e56d9SToomas Soome }
708*a29e56d9SToomas Soome }
709*a29e56d9SToomas Soome
710*a29e56d9SToomas Soome if (otyp == OTYP_LYR) {
711*a29e56d9SToomas Soome lsp->ls_open_lyr[part]++;
712*a29e56d9SToomas Soome } else {
713*a29e56d9SToomas Soome lsp->ls_open_reg[otyp] |= mask;
714*a29e56d9SToomas Soome }
715*a29e56d9SToomas Soome if (flag & FEXCL) {
716*a29e56d9SToomas Soome lsp->ls_open_excl |= mask;
717a67ac136SAlexander Eremin }
718a67ac136SAlexander Eremin
7197c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
7207c478bd9Sstevel@tonic-gate return (0);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate
7233d7072f8Seschrock /*ARGSUSED*/
7247c478bd9Sstevel@tonic-gate static int
lofi_close(dev_t dev,int flag,int otyp,struct cred * credp)7257c478bd9Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
7267c478bd9Sstevel@tonic-gate {
727*a29e56d9SToomas Soome minor_t part;
728*a29e56d9SToomas Soome int id;
729*a29e56d9SToomas Soome uint64_t mask;
7307c478bd9Sstevel@tonic-gate struct lofi_state *lsp;
7317c478bd9Sstevel@tonic-gate
732*a29e56d9SToomas Soome id = LOFI_MINOR2ID(getminor(dev));
733*a29e56d9SToomas Soome part = LOFI_PART(getminor(dev));
734*a29e56d9SToomas Soome mask = (1U << part);
735*a29e56d9SToomas Soome
7367c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock);
737*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, id);
7387c478bd9Sstevel@tonic-gate if (lsp == NULL) {
7397c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
7407c478bd9Sstevel@tonic-gate return (EINVAL);
7417c478bd9Sstevel@tonic-gate }
7420fbb751dSJohn Levon
743*a29e56d9SToomas Soome if (id == 0) {
7440fbb751dSJohn Levon mutex_exit(&lofi_lock);
7450fbb751dSJohn Levon return (0);
7460fbb751dSJohn Levon }
7470fbb751dSJohn Levon
748*a29e56d9SToomas Soome if (lsp->ls_open_excl & mask)
749*a29e56d9SToomas Soome lsp->ls_open_excl &= ~mask;
750*a29e56d9SToomas Soome
751*a29e56d9SToomas Soome if (otyp == OTYP_LYR) {
752*a29e56d9SToomas Soome lsp->ls_open_lyr[part]--;
753*a29e56d9SToomas Soome } else {
754*a29e56d9SToomas Soome lsp->ls_open_reg[otyp] &= ~mask;
755*a29e56d9SToomas Soome }
7563d7072f8Seschrock
7573d7072f8Seschrock /*
75893239addSjohnlev * If we forcibly closed the underlying device (li_force), or
75993239addSjohnlev * asked for cleanup (li_cleanup), finish up if we're the last
76093239addSjohnlev * out of the door.
7613d7072f8Seschrock */
7620fbb751dSJohn Levon if (!is_opened(lsp) && (lsp->ls_cleanup || lsp->ls_vp == NULL)) {
763*a29e56d9SToomas Soome lofi_free_dev(lsp);
7640fbb751dSJohn Levon lofi_destroy(lsp, credp);
7650fbb751dSJohn Levon }
76693239addSjohnlev
7677c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
7687c478bd9Sstevel@tonic-gate return (0);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate
7717d82f0f8SDina K Nimeh /*
7727d82f0f8SDina K Nimeh * Sets the mechanism's initialization vector (IV) if one is needed.
7737d82f0f8SDina K Nimeh * The IV is computed from the data block number. lsp->ls_mech is
7747d82f0f8SDina K Nimeh * altered so that:
7757d82f0f8SDina K Nimeh * lsp->ls_mech.cm_param_len is set to the IV len.
7767d82f0f8SDina K Nimeh * lsp->ls_mech.cm_param is set to the IV.
7777d82f0f8SDina K Nimeh */
7787d82f0f8SDina K Nimeh static int
lofi_blk_mech(struct lofi_state * lsp,longlong_t lblkno)7797d82f0f8SDina K Nimeh lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
7807d82f0f8SDina K Nimeh {
7817d82f0f8SDina K Nimeh int ret;
7827d82f0f8SDina K Nimeh crypto_data_t cdata;
7837d82f0f8SDina K Nimeh char *iv;
7847d82f0f8SDina K Nimeh size_t iv_len;
7857d82f0f8SDina K Nimeh size_t min;
7867d82f0f8SDina K Nimeh void *data;
7877d82f0f8SDina K Nimeh size_t datasz;
7887d82f0f8SDina K Nimeh
7890fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lsp->ls_crypto_lock));
7907d82f0f8SDina K Nimeh
7917d82f0f8SDina K Nimeh if (lsp == NULL)
7927d82f0f8SDina K Nimeh return (CRYPTO_DEVICE_ERROR);
7937d82f0f8SDina K Nimeh
7947d82f0f8SDina K Nimeh /* lsp->ls_mech.cm_param{_len} has already been set for static iv */
7957d82f0f8SDina K Nimeh if (lsp->ls_iv_type == IVM_NONE) {
7967d82f0f8SDina K Nimeh return (CRYPTO_SUCCESS);
7977d82f0f8SDina K Nimeh }
7987d82f0f8SDina K Nimeh
7997d82f0f8SDina K Nimeh /*
8007d82f0f8SDina K Nimeh * if kmem already alloced from previous call and it's the same size
8017d82f0f8SDina K Nimeh * we need now, just recycle it; allocate new kmem only if we have to
8027d82f0f8SDina K Nimeh */
8037d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param == NULL ||
8047d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
8057d82f0f8SDina K Nimeh iv_len = lsp->ls_iv_len;
8067d82f0f8SDina K Nimeh iv = kmem_zalloc(iv_len, KM_SLEEP);
8077d82f0f8SDina K Nimeh } else {
8087d82f0f8SDina K Nimeh iv_len = lsp->ls_mech.cm_param_len;
8097d82f0f8SDina K Nimeh iv = lsp->ls_mech.cm_param;
8107d82f0f8SDina K Nimeh bzero(iv, iv_len);
8117d82f0f8SDina K Nimeh }
8127d82f0f8SDina K Nimeh
8137d82f0f8SDina K Nimeh switch (lsp->ls_iv_type) {
8147d82f0f8SDina K Nimeh case IVM_ENC_BLKNO:
8157d82f0f8SDina K Nimeh /* iv is not static, lblkno changes each time */
8167d82f0f8SDina K Nimeh data = &lblkno;
8177d82f0f8SDina K Nimeh datasz = sizeof (lblkno);
8187d82f0f8SDina K Nimeh break;
8197d82f0f8SDina K Nimeh default:
8207d82f0f8SDina K Nimeh data = 0;
8217d82f0f8SDina K Nimeh datasz = 0;
8227d82f0f8SDina K Nimeh break;
8237d82f0f8SDina K Nimeh }
8247d82f0f8SDina K Nimeh
8257d82f0f8SDina K Nimeh /*
8267d82f0f8SDina K Nimeh * write blkno into the iv buffer padded on the left in case
8277d82f0f8SDina K Nimeh * blkno ever grows bigger than its current longlong_t size
8287d82f0f8SDina K Nimeh * or a variation other than blkno is used for the iv data
8297d82f0f8SDina K Nimeh */
8307d82f0f8SDina K Nimeh min = MIN(datasz, iv_len);
8317d82f0f8SDina K Nimeh bcopy(data, iv + (iv_len - min), min);
8327d82f0f8SDina K Nimeh
8337d82f0f8SDina K Nimeh /* encrypt the data in-place to get the IV */
8347d82f0f8SDina K Nimeh SETUP_C_DATA(cdata, iv, iv_len);
8357d82f0f8SDina K Nimeh
8367d82f0f8SDina K Nimeh ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
8377d82f0f8SDina K Nimeh NULL, NULL, NULL);
8387d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) {
8397d82f0f8SDina K Nimeh cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
8407d82f0f8SDina K Nimeh lblkno, ret);
8417d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != iv)
8427d82f0f8SDina K Nimeh kmem_free(iv, iv_len);
843b1efbcd6SAlok Aggarwal
8447d82f0f8SDina K Nimeh return (ret);
8457d82f0f8SDina K Nimeh }
8467d82f0f8SDina K Nimeh
8477d82f0f8SDina K Nimeh /* clean up the iv from the last computation */
8487d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
8497d82f0f8SDina K Nimeh kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
850b1efbcd6SAlok Aggarwal
8517d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = iv_len;
8527d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = iv;
8537d82f0f8SDina K Nimeh
8547d82f0f8SDina K Nimeh return (CRYPTO_SUCCESS);
8557d82f0f8SDina K Nimeh }
8567d82f0f8SDina K Nimeh
8577d82f0f8SDina K Nimeh /*
8587d82f0f8SDina K Nimeh * Performs encryption and decryption of a chunk of data of size "len",
8597d82f0f8SDina K Nimeh * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of
8607d82f0f8SDina K Nimeh * DEV_BSIZE.
8617d82f0f8SDina K Nimeh */
8627d82f0f8SDina 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)8637d82f0f8SDina K Nimeh lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
8647d82f0f8SDina K Nimeh caddr_t ciphertext, size_t len, boolean_t op_encrypt)
8657d82f0f8SDina K Nimeh {
8667d82f0f8SDina K Nimeh crypto_data_t cdata;
8677d82f0f8SDina K Nimeh crypto_data_t wdata;
8687d82f0f8SDina K Nimeh int ret;
8697d82f0f8SDina K Nimeh longlong_t lblkno = bp->b_lblkno;
8707d82f0f8SDina K Nimeh
8717d82f0f8SDina K Nimeh mutex_enter(&lsp->ls_crypto_lock);
8727d82f0f8SDina K Nimeh
8737d82f0f8SDina K Nimeh /*
8747d82f0f8SDina K Nimeh * though we could encrypt/decrypt entire "len" chunk of data, we need
8757d82f0f8SDina K Nimeh * to break it into DEV_BSIZE pieces to capture blkno incrementing
8767d82f0f8SDina K Nimeh */
8777d82f0f8SDina K Nimeh SETUP_C_DATA(cdata, plaintext, len);
8787d82f0f8SDina K Nimeh cdata.cd_length = DEV_BSIZE;
8797d82f0f8SDina K Nimeh if (ciphertext != NULL) { /* not in-place crypto */
8807d82f0f8SDina K Nimeh SETUP_C_DATA(wdata, ciphertext, len);
8817d82f0f8SDina K Nimeh wdata.cd_length = DEV_BSIZE;
8827d82f0f8SDina K Nimeh }
8837d82f0f8SDina K Nimeh
8847d82f0f8SDina K Nimeh do {
8857d82f0f8SDina K Nimeh ret = lofi_blk_mech(lsp, lblkno);
8867d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS)
8877d82f0f8SDina K Nimeh continue;
8887d82f0f8SDina K Nimeh
8897d82f0f8SDina K Nimeh if (op_encrypt) {
8907d82f0f8SDina K Nimeh ret = crypto_encrypt(&lsp->ls_mech, &cdata,
8917d82f0f8SDina K Nimeh &lsp->ls_key, NULL,
8927d82f0f8SDina K Nimeh ((ciphertext != NULL) ? &wdata : NULL), NULL);
8937d82f0f8SDina K Nimeh } else {
8947d82f0f8SDina K Nimeh ret = crypto_decrypt(&lsp->ls_mech, &cdata,
8957d82f0f8SDina K Nimeh &lsp->ls_key, NULL,
8967d82f0f8SDina K Nimeh ((ciphertext != NULL) ? &wdata : NULL), NULL);
8977d82f0f8SDina K Nimeh }
8987d82f0f8SDina K Nimeh
8997d82f0f8SDina K Nimeh cdata.cd_offset += DEV_BSIZE;
9007d82f0f8SDina K Nimeh if (ciphertext != NULL)
9017d82f0f8SDina K Nimeh wdata.cd_offset += DEV_BSIZE;
9027d82f0f8SDina K Nimeh lblkno++;
9037d82f0f8SDina K Nimeh } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
9047d82f0f8SDina K Nimeh
9057d82f0f8SDina K Nimeh mutex_exit(&lsp->ls_crypto_lock);
9067d82f0f8SDina K Nimeh
9077d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) {
9087d82f0f8SDina K Nimeh cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)",
9097d82f0f8SDina K Nimeh op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
9107d82f0f8SDina K Nimeh lblkno, ret);
9117d82f0f8SDina K Nimeh }
9127d82f0f8SDina K Nimeh
9137d82f0f8SDina K Nimeh return (ret);
9147d82f0f8SDina K Nimeh }
9157d82f0f8SDina K Nimeh
9167d82f0f8SDina K Nimeh #define RDWR_RAW 1
9177d82f0f8SDina K Nimeh #define RDWR_BCOPY 2
9187d82f0f8SDina K Nimeh
9197d82f0f8SDina K Nimeh static int
lofi_rdwr(caddr_t bufaddr,offset_t offset,struct buf * bp,struct lofi_state * lsp,size_t len,int method,caddr_t bcopy_locn)9207d82f0f8SDina K Nimeh lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
9217d82f0f8SDina K Nimeh struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
9227d82f0f8SDina K Nimeh {
9237d82f0f8SDina K Nimeh ssize_t resid;
9247d82f0f8SDina K Nimeh int isread;
9257d82f0f8SDina K Nimeh int error;
9267d82f0f8SDina K Nimeh
9277d82f0f8SDina K Nimeh /*
9287d82f0f8SDina K Nimeh * Handles reads/writes for both plain and encrypted lofi
9297d82f0f8SDina K Nimeh * Note: offset is already shifted by lsp->ls_crypto_offset
9307d82f0f8SDina K Nimeh * when it gets here.
9317d82f0f8SDina K Nimeh */
9327d82f0f8SDina K Nimeh
9337d82f0f8SDina K Nimeh isread = bp->b_flags & B_READ;
9347d82f0f8SDina K Nimeh if (isread) {
9357d82f0f8SDina K Nimeh if (method == RDWR_BCOPY) {
9367d82f0f8SDina K Nimeh /* DO NOT update bp->b_resid for bcopy */
9377d82f0f8SDina K Nimeh bcopy(bcopy_locn, bufaddr, len);
9387d82f0f8SDina K Nimeh error = 0;
9397d82f0f8SDina K Nimeh } else { /* RDWR_RAW */
9407d82f0f8SDina K Nimeh error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
9417d82f0f8SDina K Nimeh offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
9427d82f0f8SDina K Nimeh &resid);
9437d82f0f8SDina K Nimeh bp->b_resid = resid;
9447d82f0f8SDina K Nimeh }
9457d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled && error == 0) {
9467d82f0f8SDina K Nimeh if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
9477d82f0f8SDina K Nimeh B_FALSE) != CRYPTO_SUCCESS) {
9487d82f0f8SDina K Nimeh /*
9497d82f0f8SDina K Nimeh * XXX: original code didn't set residual
9507d82f0f8SDina K Nimeh * back to len because no error was expected
9517d82f0f8SDina K Nimeh * from bcopy() if encryption is not enabled
9527d82f0f8SDina K Nimeh */
9537d82f0f8SDina K Nimeh if (method != RDWR_BCOPY)
9547d82f0f8SDina K Nimeh bp->b_resid = len;
9557d82f0f8SDina K Nimeh error = EIO;
9567d82f0f8SDina K Nimeh }
9577d82f0f8SDina K Nimeh }
9587d82f0f8SDina K Nimeh return (error);
9597d82f0f8SDina K Nimeh } else {
9607d82f0f8SDina K Nimeh void *iobuf = bufaddr;
9617d82f0f8SDina K Nimeh
9627d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) {
9637d82f0f8SDina K Nimeh /* don't do in-place crypto to keep bufaddr intact */
9647d82f0f8SDina K Nimeh iobuf = kmem_alloc(len, KM_SLEEP);
9657d82f0f8SDina K Nimeh if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
9667d82f0f8SDina K Nimeh B_TRUE) != CRYPTO_SUCCESS) {
9677d82f0f8SDina K Nimeh kmem_free(iobuf, len);
9687d82f0f8SDina K Nimeh if (method != RDWR_BCOPY)
9697d82f0f8SDina K Nimeh bp->b_resid = len;
9707d82f0f8SDina K Nimeh return (EIO);
9717d82f0f8SDina K Nimeh }
9727d82f0f8SDina K Nimeh }
9737d82f0f8SDina K Nimeh if (method == RDWR_BCOPY) {
9747d82f0f8SDina K Nimeh /* DO NOT update bp->b_resid for bcopy */
9757d82f0f8SDina K Nimeh bcopy(iobuf, bcopy_locn, len);
9767d82f0f8SDina K Nimeh error = 0;
9777d82f0f8SDina K Nimeh } else { /* RDWR_RAW */
9787d82f0f8SDina K Nimeh error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
9797d82f0f8SDina K Nimeh offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
9807d82f0f8SDina K Nimeh &resid);
9817d82f0f8SDina K Nimeh bp->b_resid = resid;
9827d82f0f8SDina K Nimeh }
9837d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) {
9847d82f0f8SDina K Nimeh kmem_free(iobuf, len);
9857d82f0f8SDina K Nimeh }
9867d82f0f8SDina K Nimeh return (error);
9877d82f0f8SDina K Nimeh }
9887d82f0f8SDina K Nimeh }
9897d82f0f8SDina K Nimeh
99087117650Saalok static int
lofi_mapped_rdwr(caddr_t bufaddr,offset_t offset,struct buf * bp,struct lofi_state * lsp)99187117650Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
99287117650Saalok struct lofi_state *lsp)
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate int error;
99587117650Saalok offset_t alignedoffset, mapoffset;
9967c478bd9Sstevel@tonic-gate size_t xfersize;
9977c478bd9Sstevel@tonic-gate int isread;
9987c478bd9Sstevel@tonic-gate int smflags;
99987117650Saalok caddr_t mapaddr;
100087117650Saalok size_t len;
10017c478bd9Sstevel@tonic-gate enum seg_rw srw;
10027d82f0f8SDina K Nimeh int save_error;
10037d82f0f8SDina K Nimeh
10047d82f0f8SDina K Nimeh /*
10057d82f0f8SDina K Nimeh * Note: offset is already shifted by lsp->ls_crypto_offset
10067d82f0f8SDina K Nimeh * when it gets here.
10077d82f0f8SDina K Nimeh */
10087d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled)
10097d82f0f8SDina K Nimeh ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
10107c478bd9Sstevel@tonic-gate
10117c478bd9Sstevel@tonic-gate /*
10127c478bd9Sstevel@tonic-gate * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
10137c478bd9Sstevel@tonic-gate * an 8K boundary, but the buf transfer address may not be
101487117650Saalok * aligned on more than a 512-byte boundary (we don't enforce
101587117650Saalok * that even though we could). This matters since the initial
101687117650Saalok * part of the transfer may not start at offset 0 within the
101787117650Saalok * segmap'd chunk. So we have to compensate for that with
101887117650Saalok * 'mapoffset'. Subsequent chunks always start off at the
101987117650Saalok * beginning, and the last is capped by b_resid
10207d82f0f8SDina K Nimeh *
10217d82f0f8SDina K Nimeh * Visually, where "|" represents page map boundaries:
10227d82f0f8SDina K Nimeh * alignedoffset (mapaddr begins at this segmap boundary)
10237d82f0f8SDina K Nimeh * | offset (from beginning of file)
10247d82f0f8SDina K Nimeh * | | len
10257d82f0f8SDina K Nimeh * v v v
10267d82f0f8SDina K Nimeh * ===|====X========|====...======|========X====|====
10277d82f0f8SDina K Nimeh * /-------------...---------------/
10287d82f0f8SDina K Nimeh * ^ bp->b_bcount/bp->b_resid at start
10297d82f0f8SDina K Nimeh * /----/--------/----...------/--------/
10307d82f0f8SDina K Nimeh * ^ ^ ^ ^ ^
10317d82f0f8SDina K Nimeh * | | | | nth xfersize (<= MAXBSIZE)
10327d82f0f8SDina K Nimeh * | | 2nd thru n-1st xfersize (= MAXBSIZE)
10337d82f0f8SDina K Nimeh * | 1st xfersize (<= MAXBSIZE)
10347d82f0f8SDina K Nimeh * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
10357d82f0f8SDina K Nimeh *
10367d82f0f8SDina K Nimeh * Notes: "alignedoffset" is "offset" rounded down to nearest
10377d82f0f8SDina K Nimeh * MAXBSIZE boundary. "len" is next page boundary of size
1038a363f650SDina K Nimeh * PAGESIZE after "alignedoffset".
10397c478bd9Sstevel@tonic-gate */
10407c478bd9Sstevel@tonic-gate mapoffset = offset & MAXBOFFSET;
104187117650Saalok alignedoffset = offset - mapoffset;
10427c478bd9Sstevel@tonic-gate bp->b_resid = bp->b_bcount;
10437c478bd9Sstevel@tonic-gate isread = bp->b_flags & B_READ;
10447c478bd9Sstevel@tonic-gate srw = isread ? S_READ : S_WRITE;
10457c478bd9Sstevel@tonic-gate do {
104687117650Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset,
10477c478bd9Sstevel@tonic-gate MIN(MAXBSIZE - mapoffset, bp->b_resid));
1048a363f650SDina K Nimeh len = roundup(mapoffset + xfersize, PAGESIZE);
10497c478bd9Sstevel@tonic-gate mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
10507c478bd9Sstevel@tonic-gate alignedoffset, MAXBSIZE, 1, srw);
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate * Now fault in the pages. This lets us check
10537c478bd9Sstevel@tonic-gate * for errors before we reference mapaddr and
10547c478bd9Sstevel@tonic-gate * try to resolve the fault in bcopy (which would
10557c478bd9Sstevel@tonic-gate * panic instead). And this can easily happen,
10567c478bd9Sstevel@tonic-gate * particularly if you've lofi'd a file over NFS
10577c478bd9Sstevel@tonic-gate * and someone deletes the file on the server.
10587c478bd9Sstevel@tonic-gate */
10597c478bd9Sstevel@tonic-gate error = segmap_fault(kas.a_hat, segkmap, mapaddr,
10607c478bd9Sstevel@tonic-gate len, F_SOFTLOCK, srw);
10617c478bd9Sstevel@tonic-gate if (error) {
10627c478bd9Sstevel@tonic-gate (void) segmap_release(segkmap, mapaddr, 0);
10637c478bd9Sstevel@tonic-gate if (FC_CODE(error) == FC_OBJERR)
10647c478bd9Sstevel@tonic-gate error = FC_ERRNO(error);
10657c478bd9Sstevel@tonic-gate else
10667c478bd9Sstevel@tonic-gate error = EIO;
10677c478bd9Sstevel@tonic-gate break;
10687c478bd9Sstevel@tonic-gate }
10697d82f0f8SDina K Nimeh /* error may be non-zero for encrypted lofi */
10707d82f0f8SDina K Nimeh error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
10717d82f0f8SDina K Nimeh RDWR_BCOPY, mapaddr + mapoffset);
10727d82f0f8SDina K Nimeh if (error == 0) {
10737d82f0f8SDina K Nimeh bp->b_resid -= xfersize;
10747d82f0f8SDina K Nimeh bufaddr += xfersize;
10757d82f0f8SDina K Nimeh offset += xfersize;
10767d82f0f8SDina K Nimeh }
10777c478bd9Sstevel@tonic-gate smflags = 0;
10787c478bd9Sstevel@tonic-gate if (isread) {
107987117650Saalok smflags |= SM_FREE;
108087117650Saalok /*
108187117650Saalok * If we're reading an entire page starting
108287117650Saalok * at a page boundary, there's a good chance
108387117650Saalok * we won't need it again. Put it on the
108487117650Saalok * head of the freelist.
108587117650Saalok */
1086bbd65dd2SDina K Nimeh if (mapoffset == 0 && xfersize == MAXBSIZE)
108787117650Saalok smflags |= SM_DONTNEED;
10887c478bd9Sstevel@tonic-gate } else {
108944a1e32bSbatschul /*
109044a1e32bSbatschul * Write back good pages, it is okay to
109144a1e32bSbatschul * always release asynchronous here as we'll
109244a1e32bSbatschul * follow with VOP_FSYNC for B_SYNC buffers.
109344a1e32bSbatschul */
109444a1e32bSbatschul if (error == 0)
109544a1e32bSbatschul smflags |= SM_WRITE | SM_ASYNC;
10967c478bd9Sstevel@tonic-gate }
10977c478bd9Sstevel@tonic-gate (void) segmap_fault(kas.a_hat, segkmap, mapaddr,
10987c478bd9Sstevel@tonic-gate len, F_SOFTUNLOCK, srw);
10997d82f0f8SDina K Nimeh save_error = segmap_release(segkmap, mapaddr, smflags);
11007d82f0f8SDina K Nimeh if (error == 0)
11017d82f0f8SDina K Nimeh error = save_error;
11027c478bd9Sstevel@tonic-gate /* only the first map may start partial */
11037c478bd9Sstevel@tonic-gate mapoffset = 0;
11047c478bd9Sstevel@tonic-gate alignedoffset += MAXBSIZE;
11057c478bd9Sstevel@tonic-gate } while ((error == 0) && (bp->b_resid > 0) &&
110687117650Saalok (offset < lsp->ls_vp_comp_size));
110787117650Saalok
110887117650Saalok return (error);
110987117650Saalok }
111087117650Saalok
11114058a205Sjrgn.keil@googlemail.com /*
11124058a205Sjrgn.keil@googlemail.com * Check if segment seg_index is present in the decompressed segment
11134058a205Sjrgn.keil@googlemail.com * data cache.
11144058a205Sjrgn.keil@googlemail.com *
11154058a205Sjrgn.keil@googlemail.com * Returns a pointer to the decompressed segment data cache entry if
11164058a205Sjrgn.keil@googlemail.com * found, and NULL when decompressed data for this segment is not yet
11174058a205Sjrgn.keil@googlemail.com * cached.
11184058a205Sjrgn.keil@googlemail.com */
11194058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
lofi_find_comp_data(struct lofi_state * lsp,uint64_t seg_index)11204058a205Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
11214058a205Sjrgn.keil@googlemail.com {
11224058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
11234058a205Sjrgn.keil@googlemail.com
11240fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
11254058a205Sjrgn.keil@googlemail.com
11264058a205Sjrgn.keil@googlemail.com for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
11274058a205Sjrgn.keil@googlemail.com lc = list_next(&lsp->ls_comp_cache, lc)) {
11284058a205Sjrgn.keil@googlemail.com if (lc->lc_index == seg_index) {
11294058a205Sjrgn.keil@googlemail.com /*
11304058a205Sjrgn.keil@googlemail.com * Decompressed segment data was found in the
11314058a205Sjrgn.keil@googlemail.com * cache.
11324058a205Sjrgn.keil@googlemail.com *
11334058a205Sjrgn.keil@googlemail.com * The cache uses an LRU replacement strategy;
11344058a205Sjrgn.keil@googlemail.com * move the entry to head of list.
11354058a205Sjrgn.keil@googlemail.com */
11364058a205Sjrgn.keil@googlemail.com list_remove(&lsp->ls_comp_cache, lc);
11374058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc);
11384058a205Sjrgn.keil@googlemail.com return (lc);
11394058a205Sjrgn.keil@googlemail.com }
11404058a205Sjrgn.keil@googlemail.com }
11414058a205Sjrgn.keil@googlemail.com return (NULL);
11424058a205Sjrgn.keil@googlemail.com }
11434058a205Sjrgn.keil@googlemail.com
11444058a205Sjrgn.keil@googlemail.com /*
11454058a205Sjrgn.keil@googlemail.com * Add the data for a decompressed segment at segment index
11464058a205Sjrgn.keil@googlemail.com * seg_index to the cache of the decompressed segments.
11474058a205Sjrgn.keil@googlemail.com *
11484058a205Sjrgn.keil@googlemail.com * Returns a pointer to the cache element structure in case
11494058a205Sjrgn.keil@googlemail.com * the data was added to the cache; returns NULL when the data
11504058a205Sjrgn.keil@googlemail.com * wasn't cached.
11514058a205Sjrgn.keil@googlemail.com */
11524058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
lofi_add_comp_data(struct lofi_state * lsp,uint64_t seg_index,uchar_t * data)11534058a205Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
11544058a205Sjrgn.keil@googlemail.com uchar_t *data)
11554058a205Sjrgn.keil@googlemail.com {
11564058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
11574058a205Sjrgn.keil@googlemail.com
11580fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
11594058a205Sjrgn.keil@googlemail.com
11604058a205Sjrgn.keil@googlemail.com while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
11614058a205Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache);
11624058a205Sjrgn.keil@googlemail.com ASSERT(lc != NULL);
11634058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
11644058a205Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache));
11654058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--;
11664058a205Sjrgn.keil@googlemail.com }
11674058a205Sjrgn.keil@googlemail.com
11684058a205Sjrgn.keil@googlemail.com /*
11694058a205Sjrgn.keil@googlemail.com * Do not cache when disabled by tunable variable
11704058a205Sjrgn.keil@googlemail.com */
11714058a205Sjrgn.keil@googlemail.com if (lofi_max_comp_cache == 0)
11724058a205Sjrgn.keil@googlemail.com return (NULL);
11734058a205Sjrgn.keil@googlemail.com
11744058a205Sjrgn.keil@googlemail.com /*
11754058a205Sjrgn.keil@googlemail.com * When the cache has not yet reached the maximum allowed
11764058a205Sjrgn.keil@googlemail.com * number of segments, allocate a new cache element.
11774058a205Sjrgn.keil@googlemail.com * Otherwise the cache is full; reuse the last list element
11784058a205Sjrgn.keil@googlemail.com * (LRU) for caching the decompressed segment data.
11794058a205Sjrgn.keil@googlemail.com *
11804058a205Sjrgn.keil@googlemail.com * The cache element for the new decompressed segment data is
11814058a205Sjrgn.keil@googlemail.com * added to the head of the list.
11824058a205Sjrgn.keil@googlemail.com */
11834058a205Sjrgn.keil@googlemail.com if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
11844058a205Sjrgn.keil@googlemail.com lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
11854058a205Sjrgn.keil@googlemail.com lc->lc_data = NULL;
11864058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc);
11874058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count++;
11884058a205Sjrgn.keil@googlemail.com } else {
11894058a205Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache);
11904058a205Sjrgn.keil@googlemail.com if (lc == NULL)
11914058a205Sjrgn.keil@googlemail.com return (NULL);
11924058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc);
11934058a205Sjrgn.keil@googlemail.com }
11944058a205Sjrgn.keil@googlemail.com
11954058a205Sjrgn.keil@googlemail.com /*
11964058a205Sjrgn.keil@googlemail.com * Free old uncompressed segment data when reusing a cache
11974058a205Sjrgn.keil@googlemail.com * entry.
11984058a205Sjrgn.keil@googlemail.com */
11994058a205Sjrgn.keil@googlemail.com if (lc->lc_data != NULL)
12004058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
12014058a205Sjrgn.keil@googlemail.com
12024058a205Sjrgn.keil@googlemail.com lc->lc_data = data;
12034058a205Sjrgn.keil@googlemail.com lc->lc_index = seg_index;
12044058a205Sjrgn.keil@googlemail.com return (lc);
12054058a205Sjrgn.keil@googlemail.com }
12064058a205Sjrgn.keil@googlemail.com
12074058a205Sjrgn.keil@googlemail.com
120887117650Saalok /*ARGSUSED*/
1209b1efbcd6SAlok Aggarwal static int
gzip_decompress(void * src,size_t srclen,void * dst,size_t * dstlen,int level)1210b1efbcd6SAlok Aggarwal gzip_decompress(void *src, size_t srclen, void *dst,
121187117650Saalok size_t *dstlen, int level)
121287117650Saalok {
121387117650Saalok ASSERT(*dstlen >= srclen);
121487117650Saalok
121587117650Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
121687117650Saalok return (-1);
121787117650Saalok return (0);
121887117650Saalok }
121987117650Saalok
1220b1efbcd6SAlok Aggarwal #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
1221b1efbcd6SAlok Aggarwal /*ARGSUSED*/
1222b1efbcd6SAlok Aggarwal static int
lzma_decompress(void * src,size_t srclen,void * dst,size_t * dstlen,int level)1223b1efbcd6SAlok Aggarwal lzma_decompress(void *src, size_t srclen, void *dst,
1224b1efbcd6SAlok Aggarwal size_t *dstlen, int level)
1225b1efbcd6SAlok Aggarwal {
1226b1efbcd6SAlok Aggarwal size_t insizepure;
1227b1efbcd6SAlok Aggarwal void *actual_src;
1228b1efbcd6SAlok Aggarwal ELzmaStatus status;
1229b1efbcd6SAlok Aggarwal
1230b1efbcd6SAlok Aggarwal insizepure = srclen - LZMA_HEADER_SIZE;
1231b1efbcd6SAlok Aggarwal actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
1232b1efbcd6SAlok Aggarwal
1233b1efbcd6SAlok Aggarwal if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
1234b1efbcd6SAlok Aggarwal (const Byte *)actual_src, &insizepure,
1235b1efbcd6SAlok Aggarwal (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
1236b1efbcd6SAlok Aggarwal &g_Alloc) != SZ_OK) {
1237b1efbcd6SAlok Aggarwal return (-1);
1238b1efbcd6SAlok Aggarwal }
1239b1efbcd6SAlok Aggarwal return (0);
1240b1efbcd6SAlok Aggarwal }
1241b1efbcd6SAlok Aggarwal
124287117650Saalok /*
124387117650Saalok * This is basically what strategy used to be before we found we
124487117650Saalok * needed task queues.
124587117650Saalok */
124687117650Saalok static void
lofi_strategy_task(void * arg)124787117650Saalok lofi_strategy_task(void *arg)
124887117650Saalok {
124987117650Saalok struct buf *bp = (struct buf *)arg;
125087117650Saalok int error;
125144a1e32bSbatschul int syncflag = 0;
125287117650Saalok struct lofi_state *lsp;
12537d82f0f8SDina K Nimeh offset_t offset;
125487117650Saalok caddr_t bufaddr;
12557d82f0f8SDina K Nimeh size_t len;
12567d82f0f8SDina K Nimeh size_t xfersize;
12577d82f0f8SDina K Nimeh boolean_t bufinited = B_FALSE;
125887117650Saalok
1259*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep,
1260*a29e56d9SToomas Soome LOFI_MINOR2ID(getminor(bp->b_edev)));
1261*a29e56d9SToomas Soome
12627d82f0f8SDina K Nimeh if (lsp == NULL) {
12637d82f0f8SDina K Nimeh error = ENXIO;
12647d82f0f8SDina K Nimeh goto errout;
12657d82f0f8SDina K Nimeh }
126687117650Saalok if (lsp->ls_kstat) {
126787117650Saalok mutex_enter(lsp->ls_kstat->ks_lock);
126887117650Saalok kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
126987117650Saalok mutex_exit(lsp->ls_kstat->ks_lock);
127087117650Saalok }
1271*a29e56d9SToomas Soome
1272*a29e56d9SToomas Soome mutex_enter(&lsp->ls_vp_lock);
1273*a29e56d9SToomas Soome lsp->ls_vp_iocount++;
1274*a29e56d9SToomas Soome mutex_exit(&lsp->ls_vp_lock);
1275*a29e56d9SToomas Soome
127687117650Saalok bp_mapin(bp);
127787117650Saalok bufaddr = bp->b_un.b_addr;
1278*a29e56d9SToomas Soome offset = (bp->b_lblkno + (diskaddr_t)(uintptr_t)bp->b_private)
1279*a29e56d9SToomas Soome << lsp->ls_lbshift; /* offset within file */
12807d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) {
12817d82f0f8SDina K Nimeh /* encrypted data really begins after crypto header */
12827d82f0f8SDina K Nimeh offset += lsp->ls_crypto_offset;
12837d82f0f8SDina K Nimeh }
12847d82f0f8SDina K Nimeh len = bp->b_bcount;
12857d82f0f8SDina K Nimeh bufinited = B_TRUE;
12867d82f0f8SDina K Nimeh
12877d82f0f8SDina K Nimeh if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
12887d82f0f8SDina K Nimeh error = EIO;
12897d82f0f8SDina K Nimeh goto errout;
12907d82f0f8SDina K Nimeh }
129187117650Saalok
129287117650Saalok /*
129344a1e32bSbatschul * If we're writing and the buffer was not B_ASYNC
129444a1e32bSbatschul * we'll follow up with a VOP_FSYNC() to force any
129544a1e32bSbatschul * asynchronous I/O to stable storage.
129644a1e32bSbatschul */
129744a1e32bSbatschul if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
129844a1e32bSbatschul syncflag = FSYNC;
129944a1e32bSbatschul
130044a1e32bSbatschul /*
130187117650Saalok * We used to always use vn_rdwr here, but we cannot do that because
130287117650Saalok * we might decide to read or write from the the underlying
130387117650Saalok * file during this call, which would be a deadlock because
130487117650Saalok * we have the rw_lock. So instead we page, unless it's not
13057d82f0f8SDina K Nimeh * mapable or it's a character device or it's an encrypted lofi.
130687117650Saalok */
13077d82f0f8SDina K Nimeh if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
13087d82f0f8SDina K Nimeh lsp->ls_crypto_enabled) {
13097d82f0f8SDina K Nimeh error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
13107d82f0f8SDina K Nimeh NULL);
13117d82f0f8SDina K Nimeh } else if (lsp->ls_uncomp_seg_sz == 0) {
131287117650Saalok error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
13137d82f0f8SDina K Nimeh } else {
13144058a205Sjrgn.keil@googlemail.com uchar_t *compressed_seg = NULL, *cmpbuf;
13154058a205Sjrgn.keil@googlemail.com uchar_t *uncompressed_seg = NULL;
13167d82f0f8SDina K Nimeh lofi_compress_info_t *li;
13177d82f0f8SDina K Nimeh size_t oblkcount;
13184058a205Sjrgn.keil@googlemail.com ulong_t seglen;
13197d82f0f8SDina K Nimeh uint64_t sblkno, eblkno, cmpbytes;
13204058a205Sjrgn.keil@googlemail.com uint64_t uncompressed_seg_index;
13214058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
13227d82f0f8SDina K Nimeh offset_t sblkoff, eblkoff;
13237d82f0f8SDina K Nimeh u_offset_t salign, ealign;
13247d82f0f8SDina K Nimeh u_offset_t sdiff;
13257d82f0f8SDina K Nimeh uint32_t comp_data_sz;
13267d82f0f8SDina K Nimeh uint64_t i;
1327b9c7fb03SAlok Aggarwal int j;
132887117650Saalok
132987117650Saalok /*
133087117650Saalok * From here on we're dealing primarily with compressed files
133187117650Saalok */
13327d82f0f8SDina K Nimeh ASSERT(!lsp->ls_crypto_enabled);
133387117650Saalok
133487117650Saalok /*
133587117650Saalok * Compressed files can only be read from and
133687117650Saalok * not written to
133787117650Saalok */
133887117650Saalok if (!(bp->b_flags & B_READ)) {
133987117650Saalok bp->b_resid = bp->b_bcount;
134087117650Saalok error = EROFS;
134187117650Saalok goto done;
134287117650Saalok }
134387117650Saalok
134487117650Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0);
134587117650Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
134687117650Saalok /*
134787117650Saalok * Compute starting and ending compressed segment numbers
134887117650Saalok * We use only bitwise operations avoiding division and
134987117650Saalok * modulus because we enforce the compression segment size
135087117650Saalok * to a power of 2
135187117650Saalok */
135287117650Saalok sblkno = offset >> lsp->ls_comp_seg_shift;
135387117650Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
135487117650Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
135587117650Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
135687117650Saalok
135787117650Saalok /*
13584058a205Sjrgn.keil@googlemail.com * Check the decompressed segment cache.
13594058a205Sjrgn.keil@googlemail.com *
13604058a205Sjrgn.keil@googlemail.com * The cache is used only when the requested data
13614058a205Sjrgn.keil@googlemail.com * is within a segment. Requests that cross
13624058a205Sjrgn.keil@googlemail.com * segment boundaries bypass the cache.
13634058a205Sjrgn.keil@googlemail.com */
13644058a205Sjrgn.keil@googlemail.com if (sblkno == eblkno ||
13654058a205Sjrgn.keil@googlemail.com (sblkno + 1 == eblkno && eblkoff == 0)) {
13664058a205Sjrgn.keil@googlemail.com /*
13674058a205Sjrgn.keil@googlemail.com * Request doesn't cross a segment boundary,
13684058a205Sjrgn.keil@googlemail.com * now check the cache.
13694058a205Sjrgn.keil@googlemail.com */
13704058a205Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock);
13714058a205Sjrgn.keil@googlemail.com lc = lofi_find_comp_data(lsp, sblkno);
13724058a205Sjrgn.keil@googlemail.com if (lc != NULL) {
13734058a205Sjrgn.keil@googlemail.com /*
13744058a205Sjrgn.keil@googlemail.com * We've found the decompressed segment
13754058a205Sjrgn.keil@googlemail.com * data in the cache; reuse it.
13764058a205Sjrgn.keil@googlemail.com */
13774058a205Sjrgn.keil@googlemail.com bcopy(lc->lc_data + sblkoff, bufaddr,
13784058a205Sjrgn.keil@googlemail.com bp->b_bcount);
13794058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock);
13804058a205Sjrgn.keil@googlemail.com bp->b_resid = 0;
13814058a205Sjrgn.keil@googlemail.com error = 0;
13824058a205Sjrgn.keil@googlemail.com goto done;
13834058a205Sjrgn.keil@googlemail.com }
13844058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock);
13854058a205Sjrgn.keil@googlemail.com }
13864058a205Sjrgn.keil@googlemail.com
13874058a205Sjrgn.keil@googlemail.com /*
138887117650Saalok * Align start offset to block boundary for segmap
138987117650Saalok */
139087117650Saalok salign = lsp->ls_comp_seg_index[sblkno];
139187117650Saalok sdiff = salign & (DEV_BSIZE - 1);
139287117650Saalok salign -= sdiff;
139387117650Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
139487117650Saalok /*
139587117650Saalok * We're dealing with the last segment of
139687117650Saalok * the compressed file -- the size of this
139787117650Saalok * segment *may not* be the same as the
139887117650Saalok * segment size for the file
139987117650Saalok */
140087117650Saalok eblkoff = (offset + bp->b_bcount) &
140187117650Saalok (lsp->ls_uncomp_last_seg_sz - 1);
140287117650Saalok ealign = lsp->ls_vp_comp_size;
140387117650Saalok } else {
140487117650Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1];
140587117650Saalok }
140687117650Saalok
140787117650Saalok /*
140887117650Saalok * Preserve original request paramaters
140987117650Saalok */
141087117650Saalok oblkcount = bp->b_bcount;
141187117650Saalok
141287117650Saalok /*
141387117650Saalok * Assign the calculated parameters
141487117650Saalok */
141587117650Saalok comp_data_sz = ealign - salign;
141687117650Saalok bp->b_bcount = comp_data_sz;
141787117650Saalok
141887117650Saalok /*
1419b9c7fb03SAlok Aggarwal * Buffers to hold compressed segments are pre-allocated
1420b9c7fb03SAlok Aggarwal * on a per-thread basis. Find a pre-allocated buffer
1421b9c7fb03SAlok Aggarwal * that is not currently in use and mark it for use.
142287117650Saalok */
1423b9c7fb03SAlok Aggarwal mutex_enter(&lsp->ls_comp_bufs_lock);
1424b9c7fb03SAlok Aggarwal for (j = 0; j < lofi_taskq_nthreads; j++) {
1425b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[j].inuse == 0) {
1426b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].inuse = 1;
1427b9c7fb03SAlok Aggarwal break;
1428b9c7fb03SAlok Aggarwal }
1429b9c7fb03SAlok Aggarwal }
1430b9c7fb03SAlok Aggarwal
1431b9c7fb03SAlok Aggarwal mutex_exit(&lsp->ls_comp_bufs_lock);
1432b9c7fb03SAlok Aggarwal ASSERT(j < lofi_taskq_nthreads);
1433b9c7fb03SAlok Aggarwal
1434b9c7fb03SAlok Aggarwal /*
1435b9c7fb03SAlok Aggarwal * If the pre-allocated buffer size does not match
1436b9c7fb03SAlok Aggarwal * the size of the I/O request, re-allocate it with
1437b9c7fb03SAlok Aggarwal * the appropriate size
1438b9c7fb03SAlok Aggarwal */
1439b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) {
1440b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[j].bufsize > 0)
1441b9c7fb03SAlok Aggarwal kmem_free(lsp->ls_comp_bufs[j].buf,
1442b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].bufsize);
1443b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount,
1444b9c7fb03SAlok Aggarwal KM_SLEEP);
1445b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].bufsize = bp->b_bcount;
1446b9c7fb03SAlok Aggarwal }
1447b9c7fb03SAlok Aggarwal compressed_seg = lsp->ls_comp_bufs[j].buf;
1448b9c7fb03SAlok Aggarwal
144987117650Saalok /*
145087117650Saalok * Map in the calculated number of blocks
145187117650Saalok */
145287117650Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
145387117650Saalok bp, lsp);
145487117650Saalok
145587117650Saalok bp->b_bcount = oblkcount;
145687117650Saalok bp->b_resid = oblkcount;
145787117650Saalok if (error != 0)
145887117650Saalok goto done;
145987117650Saalok
146087117650Saalok /*
1461b9c7fb03SAlok Aggarwal * decompress compressed blocks start
146287117650Saalok */
146387117650Saalok cmpbuf = compressed_seg + sdiff;
1464b1efbcd6SAlok Aggarwal for (i = sblkno; i <= eblkno; i++) {
1465b1efbcd6SAlok Aggarwal ASSERT(i < lsp->ls_comp_index_sz - 1);
1466b9c7fb03SAlok Aggarwal uchar_t *useg;
1467b1efbcd6SAlok Aggarwal
1468b1efbcd6SAlok Aggarwal /*
1469b1efbcd6SAlok Aggarwal * The last segment is special in that it is
1470b1efbcd6SAlok Aggarwal * most likely not going to be the same
1471b1efbcd6SAlok Aggarwal * (uncompressed) size as the other segments.
1472b1efbcd6SAlok Aggarwal */
1473b1efbcd6SAlok Aggarwal if (i == (lsp->ls_comp_index_sz - 2)) {
1474b1efbcd6SAlok Aggarwal seglen = lsp->ls_uncomp_last_seg_sz;
1475b1efbcd6SAlok Aggarwal } else {
1476b1efbcd6SAlok Aggarwal seglen = lsp->ls_uncomp_seg_sz;
1477b1efbcd6SAlok Aggarwal }
1478b1efbcd6SAlok Aggarwal
147987117650Saalok /*
148087117650Saalok * Each of the segment index entries contains
148187117650Saalok * the starting block number for that segment.
148287117650Saalok * The number of compressed bytes in a segment
148387117650Saalok * is thus the difference between the starting
148487117650Saalok * block number of this segment and the starting
148587117650Saalok * block number of the next segment.
148687117650Saalok */
148787117650Saalok cmpbytes = lsp->ls_comp_seg_index[i + 1] -
148887117650Saalok lsp->ls_comp_seg_index[i];
148987117650Saalok
149087117650Saalok /*
149187117650Saalok * The first byte in a compressed segment is a flag
149287117650Saalok * that indicates whether this segment is compressed
1493b9c7fb03SAlok Aggarwal * at all.
1494b9c7fb03SAlok Aggarwal *
1495b9c7fb03SAlok Aggarwal * The variable 'useg' is used (instead of
1496b9c7fb03SAlok Aggarwal * uncompressed_seg) in this loop to keep a
1497b9c7fb03SAlok Aggarwal * reference to the uncompressed segment.
1498b9c7fb03SAlok Aggarwal *
1499b9c7fb03SAlok Aggarwal * N.B. If 'useg' is replaced with uncompressed_seg,
1500b9c7fb03SAlok Aggarwal * it leads to memory leaks and heap corruption in
1501b9c7fb03SAlok Aggarwal * corner cases where compressed segments lie
1502b9c7fb03SAlok Aggarwal * adjacent to uncompressed segments.
150387117650Saalok */
150487117650Saalok if (*cmpbuf == UNCOMPRESSED) {
1505b9c7fb03SAlok Aggarwal useg = cmpbuf + SEGHDR;
150687117650Saalok } else {
1507b9c7fb03SAlok Aggarwal if (uncompressed_seg == NULL)
1508b9c7fb03SAlok Aggarwal uncompressed_seg =
1509b9c7fb03SAlok Aggarwal kmem_alloc(lsp->ls_uncomp_seg_sz,
1510b9c7fb03SAlok Aggarwal KM_SLEEP);
1511b9c7fb03SAlok Aggarwal useg = uncompressed_seg;
1512b9c7fb03SAlok Aggarwal uncompressed_seg_index = i;
1513b9c7fb03SAlok Aggarwal
151487117650Saalok if (li->l_decompress((cmpbuf + SEGHDR),
151587117650Saalok (cmpbytes - SEGHDR), uncompressed_seg,
151687117650Saalok &seglen, li->l_level) != 0) {
151787117650Saalok error = EIO;
151887117650Saalok goto done;
151987117650Saalok }
152087117650Saalok }
152187117650Saalok
152287117650Saalok /*
152387117650Saalok * Determine how much uncompressed data we
152487117650Saalok * have to copy and copy it
152587117650Saalok */
152687117650Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
1527b1efbcd6SAlok Aggarwal if (i == eblkno)
1528b1efbcd6SAlok Aggarwal xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
152987117650Saalok
1530b9c7fb03SAlok Aggarwal bcopy((useg + sblkoff), bufaddr, xfersize);
153187117650Saalok
153287117650Saalok cmpbuf += cmpbytes;
153387117650Saalok bufaddr += xfersize;
153487117650Saalok bp->b_resid -= xfersize;
153587117650Saalok sblkoff = 0;
153687117650Saalok
153787117650Saalok if (bp->b_resid == 0)
153887117650Saalok break;
1539b9c7fb03SAlok Aggarwal } /* decompress compressed blocks ends */
15404058a205Sjrgn.keil@googlemail.com
15414058a205Sjrgn.keil@googlemail.com /*
1542b9c7fb03SAlok Aggarwal * Skip to done if there is no uncompressed data to cache
1543b9c7fb03SAlok Aggarwal */
1544b9c7fb03SAlok Aggarwal if (uncompressed_seg == NULL)
1545b9c7fb03SAlok Aggarwal goto done;
1546b9c7fb03SAlok Aggarwal
1547b9c7fb03SAlok Aggarwal /*
1548b9c7fb03SAlok Aggarwal * Add the data for the last decompressed segment to
15494058a205Sjrgn.keil@googlemail.com * the cache.
15504058a205Sjrgn.keil@googlemail.com *
15514058a205Sjrgn.keil@googlemail.com * In case the uncompressed segment data was added to (and
15524058a205Sjrgn.keil@googlemail.com * is referenced by) the cache, make sure we don't free it
15534058a205Sjrgn.keil@googlemail.com * here.
15544058a205Sjrgn.keil@googlemail.com */
15554058a205Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock);
15564058a205Sjrgn.keil@googlemail.com if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
15574058a205Sjrgn.keil@googlemail.com uncompressed_seg)) != NULL) {
15584058a205Sjrgn.keil@googlemail.com uncompressed_seg = NULL;
15594058a205Sjrgn.keil@googlemail.com }
15604058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock);
15614058a205Sjrgn.keil@googlemail.com
156287117650Saalok done:
1563b9c7fb03SAlok Aggarwal if (compressed_seg != NULL) {
1564b9c7fb03SAlok Aggarwal mutex_enter(&lsp->ls_comp_bufs_lock);
1565b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].inuse = 0;
1566b9c7fb03SAlok Aggarwal mutex_exit(&lsp->ls_comp_bufs_lock);
1567b9c7fb03SAlok Aggarwal }
156887117650Saalok if (uncompressed_seg != NULL)
156987117650Saalok kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
15707d82f0f8SDina K Nimeh } /* end of handling compressed files */
157187117650Saalok
157244a1e32bSbatschul if ((error == 0) && (syncflag != 0))
157344a1e32bSbatschul error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
157444a1e32bSbatschul
15757d82f0f8SDina K Nimeh errout:
15767d82f0f8SDina K Nimeh if (bufinited && lsp->ls_kstat) {
15777c478bd9Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid;
15787c478bd9Sstevel@tonic-gate kstat_io_t *kioptr;
15797c478bd9Sstevel@tonic-gate
15807c478bd9Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock);
15817c478bd9Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
15827c478bd9Sstevel@tonic-gate if (bp->b_flags & B_READ) {
15837c478bd9Sstevel@tonic-gate kioptr->nread += n_done;
15847c478bd9Sstevel@tonic-gate kioptr->reads++;
15857c478bd9Sstevel@tonic-gate } else {
15867c478bd9Sstevel@tonic-gate kioptr->nwritten += n_done;
15877c478bd9Sstevel@tonic-gate kioptr->writes++;
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate kstat_runq_exit(kioptr);
15907c478bd9Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock);
15917c478bd9Sstevel@tonic-gate }
15923d7072f8Seschrock
15933d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock);
15943d7072f8Seschrock if (--lsp->ls_vp_iocount == 0)
15953d7072f8Seschrock cv_broadcast(&lsp->ls_vp_cv);
15963d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
15973d7072f8Seschrock
15987c478bd9Sstevel@tonic-gate bioerror(bp, error);
15997c478bd9Sstevel@tonic-gate biodone(bp);
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate
16027c478bd9Sstevel@tonic-gate static int
lofi_strategy(struct buf * bp)16037c478bd9Sstevel@tonic-gate lofi_strategy(struct buf *bp)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate struct lofi_state *lsp;
16067c478bd9Sstevel@tonic-gate offset_t offset;
1607*a29e56d9SToomas Soome minor_t part;
1608*a29e56d9SToomas Soome diskaddr_t p_lba;
1609*a29e56d9SToomas Soome diskaddr_t p_nblks;
1610*a29e56d9SToomas Soome int shift;
16117c478bd9Sstevel@tonic-gate
16127c478bd9Sstevel@tonic-gate /*
16137c478bd9Sstevel@tonic-gate * We cannot just do I/O here, because the current thread
16147c478bd9Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem
16157c478bd9Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and
16167c478bd9Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer.
16177c478bd9Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below.
16187c478bd9Sstevel@tonic-gate *
16197c478bd9Sstevel@tonic-gate * We could come up with a complex strategy using threads to
16207c478bd9Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task
16217c478bd9Sstevel@tonic-gate * queues were incredibly easy so they win.
16227c478bd9Sstevel@tonic-gate */
1623*a29e56d9SToomas Soome
1624*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep,
1625*a29e56d9SToomas Soome LOFI_MINOR2ID(getminor(bp->b_edev)));
1626*a29e56d9SToomas Soome part = LOFI_PART(getminor(bp->b_edev));
1627*a29e56d9SToomas Soome
16287d82f0f8SDina K Nimeh if (lsp == NULL) {
16297d82f0f8SDina K Nimeh bioerror(bp, ENXIO);
16307d82f0f8SDina K Nimeh biodone(bp);
16317d82f0f8SDina K Nimeh return (0);
16327d82f0f8SDina K Nimeh }
1633*a29e56d9SToomas Soome shift = lsp->ls_lbshift;
1634*a29e56d9SToomas Soome
1635*a29e56d9SToomas Soome p_lba = 0;
1636*a29e56d9SToomas Soome p_nblks = lsp->ls_vp_size >> shift;
1637*a29e56d9SToomas Soome
1638*a29e56d9SToomas Soome if (lsp->ls_cmlbhandle != NULL) {
1639*a29e56d9SToomas Soome if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &p_nblks, &p_lba,
1640*a29e56d9SToomas Soome NULL, NULL, 0)) {
1641*a29e56d9SToomas Soome bioerror(bp, ENXIO);
1642*a29e56d9SToomas Soome biodone(bp);
1643*a29e56d9SToomas Soome return (0);
1644*a29e56d9SToomas Soome }
1645*a29e56d9SToomas Soome }
1646*a29e56d9SToomas Soome
1647*a29e56d9SToomas Soome /* start block past partition end? */
1648*a29e56d9SToomas Soome if (bp->b_lblkno > p_nblks) {
1649*a29e56d9SToomas Soome bioerror(bp, ENXIO);
1650*a29e56d9SToomas Soome biodone(bp);
1651*a29e56d9SToomas Soome return (0);
1652*a29e56d9SToomas Soome }
1653*a29e56d9SToomas Soome
1654*a29e56d9SToomas Soome offset = (bp->b_lblkno+p_lba) << shift; /* offset within file */
16557d82f0f8SDina K Nimeh
16563d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock);
16573d7072f8Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
16583d7072f8Seschrock bioerror(bp, EIO);
16593d7072f8Seschrock biodone(bp);
16603d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
16613d7072f8Seschrock return (0);
16623d7072f8Seschrock }
16633d7072f8Seschrock
16647d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) {
16657d82f0f8SDina K Nimeh /* encrypted data really begins after crypto header */
16667d82f0f8SDina K Nimeh offset += lsp->ls_crypto_offset;
16677d82f0f8SDina K Nimeh }
1668*a29e56d9SToomas Soome
1669*a29e56d9SToomas Soome /* make sure we will not pass the file or partition size */
1670*a29e56d9SToomas Soome if (offset == lsp->ls_vp_size ||
1671*a29e56d9SToomas Soome offset == (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) {
16727c478bd9Sstevel@tonic-gate /* EOF */
16737c478bd9Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) {
16747c478bd9Sstevel@tonic-gate bp->b_resid = bp->b_bcount;
16757c478bd9Sstevel@tonic-gate bioerror(bp, 0);
16767c478bd9Sstevel@tonic-gate } else {
16777c478bd9Sstevel@tonic-gate /* writes should fail */
16787c478bd9Sstevel@tonic-gate bioerror(bp, ENXIO);
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate biodone(bp);
16813d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
16827c478bd9Sstevel@tonic-gate return (0);
16837c478bd9Sstevel@tonic-gate }
1684*a29e56d9SToomas Soome if ((offset > lsp->ls_vp_size) ||
1685*a29e56d9SToomas Soome (offset > (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) ||
1686*a29e56d9SToomas Soome ((offset + bp->b_bcount) > ((p_lba + p_nblks) << shift))) {
16877c478bd9Sstevel@tonic-gate bioerror(bp, ENXIO);
16887c478bd9Sstevel@tonic-gate biodone(bp);
16893d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
16907c478bd9Sstevel@tonic-gate return (0);
16917c478bd9Sstevel@tonic-gate }
1692*a29e56d9SToomas Soome
16933d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
16943d7072f8Seschrock
16957c478bd9Sstevel@tonic-gate if (lsp->ls_kstat) {
16967c478bd9Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock);
16977c478bd9Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
16987c478bd9Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock);
16997c478bd9Sstevel@tonic-gate }
1700*a29e56d9SToomas Soome bp->b_private = (void *)(uintptr_t)p_lba; /* partition start */
17017c478bd9Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
17027c478bd9Sstevel@tonic-gate return (0);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate
17057c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
17067c478bd9Sstevel@tonic-gate static int
lofi_read(dev_t dev,struct uio * uio,struct cred * credp)17077c478bd9Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate if (getminor(dev) == 0)
17107c478bd9Sstevel@tonic-gate return (EINVAL);
17117d82f0f8SDina K Nimeh UIO_CHECK(uio);
17127c478bd9Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate
17157c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
17167c478bd9Sstevel@tonic-gate static int
lofi_write(dev_t dev,struct uio * uio,struct cred * credp)17177c478bd9Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate if (getminor(dev) == 0)
17207c478bd9Sstevel@tonic-gate return (EINVAL);
17217d82f0f8SDina K Nimeh UIO_CHECK(uio);
17227c478bd9Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
17237c478bd9Sstevel@tonic-gate }
17247c478bd9Sstevel@tonic-gate
17257c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
17267c478bd9Sstevel@tonic-gate static int
lofi_aread(dev_t dev,struct aio_req * aio,struct cred * credp)17277c478bd9Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
17287c478bd9Sstevel@tonic-gate {
17297c478bd9Sstevel@tonic-gate if (getminor(dev) == 0)
17307c478bd9Sstevel@tonic-gate return (EINVAL);
17317d82f0f8SDina K Nimeh UIO_CHECK(aio->aio_uio);
17327c478bd9Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
17337c478bd9Sstevel@tonic-gate }
17347c478bd9Sstevel@tonic-gate
17357c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
17367c478bd9Sstevel@tonic-gate static int
lofi_awrite(dev_t dev,struct aio_req * aio,struct cred * credp)17377c478bd9Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate if (getminor(dev) == 0)
17407c478bd9Sstevel@tonic-gate return (EINVAL);
17417d82f0f8SDina K Nimeh UIO_CHECK(aio->aio_uio);
17427c478bd9Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate
17457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17467c478bd9Sstevel@tonic-gate static int
lofi_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)17477c478bd9Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
17487c478bd9Sstevel@tonic-gate {
1749*a29e56d9SToomas Soome struct lofi_state *lsp;
1750*a29e56d9SToomas Soome dev_t dev = (dev_t)arg;
1751*a29e56d9SToomas Soome int instance;
1752*a29e56d9SToomas Soome
1753*a29e56d9SToomas Soome instance = LOFI_MINOR2ID(getminor(dev));
17547c478bd9Sstevel@tonic-gate switch (infocmd) {
17557c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
1756*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, instance);
1757*a29e56d9SToomas Soome if (lsp == NULL)
1758*a29e56d9SToomas Soome return (DDI_FAILURE);
1759*a29e56d9SToomas Soome *result = lsp->ls_dip;
17607c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
17617c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
1762*a29e56d9SToomas Soome *result = (void *) (intptr_t)instance;
17637c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
17647c478bd9Sstevel@tonic-gate }
17657c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate
17687c478bd9Sstevel@tonic-gate static int
lofi_create_minor_nodes(struct lofi_state * lsp,boolean_t labeled)1769*a29e56d9SToomas Soome lofi_create_minor_nodes(struct lofi_state *lsp, boolean_t labeled)
1770*a29e56d9SToomas Soome {
1771*a29e56d9SToomas Soome int error = 0;
1772*a29e56d9SToomas Soome int instance = ddi_get_instance(lsp->ls_dip);
1773*a29e56d9SToomas Soome
1774*a29e56d9SToomas Soome if (labeled == B_TRUE) {
1775*a29e56d9SToomas Soome cmlb_alloc_handle(&lsp->ls_cmlbhandle);
1776*a29e56d9SToomas Soome error = cmlb_attach(lsp->ls_dip, &lofi_tg_ops, DTYPE_DIRECT,
1777*a29e56d9SToomas Soome B_FALSE, B_FALSE, DDI_NT_BLOCK_CHAN,
1778*a29e56d9SToomas Soome CMLB_CREATE_P0_MINOR_NODE, lsp->ls_cmlbhandle, (void *)1);
1779*a29e56d9SToomas Soome
1780*a29e56d9SToomas Soome if (error != DDI_SUCCESS) {
1781*a29e56d9SToomas Soome cmlb_free_handle(&lsp->ls_cmlbhandle);
1782*a29e56d9SToomas Soome lsp->ls_cmlbhandle = NULL;
1783*a29e56d9SToomas Soome error = ENXIO;
1784*a29e56d9SToomas Soome }
1785*a29e56d9SToomas Soome } else {
1786*a29e56d9SToomas Soome /* create minor nodes */
1787*a29e56d9SToomas Soome error = ddi_create_minor_node(lsp->ls_dip, LOFI_BLOCK_NODE,
1788*a29e56d9SToomas Soome S_IFBLK, LOFI_ID2MINOR(instance), DDI_PSEUDO, 0);
1789*a29e56d9SToomas Soome if (error == DDI_SUCCESS) {
1790*a29e56d9SToomas Soome error = ddi_create_minor_node(lsp->ls_dip,
1791*a29e56d9SToomas Soome LOFI_CHAR_NODE, S_IFCHR, LOFI_ID2MINOR(instance),
1792*a29e56d9SToomas Soome DDI_PSEUDO, 0);
1793*a29e56d9SToomas Soome if (error != DDI_SUCCESS) {
1794*a29e56d9SToomas Soome ddi_remove_minor_node(lsp->ls_dip,
1795*a29e56d9SToomas Soome LOFI_BLOCK_NODE);
1796*a29e56d9SToomas Soome error = ENXIO;
1797*a29e56d9SToomas Soome }
1798*a29e56d9SToomas Soome } else
1799*a29e56d9SToomas Soome error = ENXIO;
1800*a29e56d9SToomas Soome }
1801*a29e56d9SToomas Soome return (error);
1802*a29e56d9SToomas Soome }
1803*a29e56d9SToomas Soome
1804*a29e56d9SToomas Soome static int
lofi_zone_bind(struct lofi_state * lsp)1805*a29e56d9SToomas Soome lofi_zone_bind(struct lofi_state *lsp)
1806*a29e56d9SToomas Soome {
1807*a29e56d9SToomas Soome int error = 0;
1808*a29e56d9SToomas Soome
1809*a29e56d9SToomas Soome mutex_enter(&curproc->p_lock);
1810*a29e56d9SToomas Soome if ((error = rctl_incr_lofi(curproc, curproc->p_zone, 1)) != 0) {
1811*a29e56d9SToomas Soome mutex_exit(&curproc->p_lock);
1812*a29e56d9SToomas Soome return (error);
1813*a29e56d9SToomas Soome }
1814*a29e56d9SToomas Soome mutex_exit(&curproc->p_lock);
1815*a29e56d9SToomas Soome
1816*a29e56d9SToomas Soome if (ddi_prop_update_string(lsp->ls_dev, lsp->ls_dip, ZONE_PROP_NAME,
1817*a29e56d9SToomas Soome (char *)curproc->p_zone->zone_name) != DDI_PROP_SUCCESS) {
1818*a29e56d9SToomas Soome rctl_decr_lofi(curproc->p_zone, 1);
1819*a29e56d9SToomas Soome error = EINVAL;
1820*a29e56d9SToomas Soome } else {
1821*a29e56d9SToomas Soome zone_init_ref(&lsp->ls_zone);
1822*a29e56d9SToomas Soome zone_hold_ref(curzone, &lsp->ls_zone, ZONE_REF_LOFI);
1823*a29e56d9SToomas Soome }
1824*a29e56d9SToomas Soome return (error);
1825*a29e56d9SToomas Soome }
1826*a29e56d9SToomas Soome
1827*a29e56d9SToomas Soome static void
lofi_zone_unbind(struct lofi_state * lsp)1828*a29e56d9SToomas Soome lofi_zone_unbind(struct lofi_state *lsp)
1829*a29e56d9SToomas Soome {
1830*a29e56d9SToomas Soome (void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip, ZONE_PROP_NAME);
1831*a29e56d9SToomas Soome rctl_decr_lofi(curproc->p_zone, 1);
1832*a29e56d9SToomas Soome zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
1833*a29e56d9SToomas Soome }
1834*a29e56d9SToomas Soome
1835*a29e56d9SToomas Soome static int
lofi_online_dev(dev_info_t * dip)1836*a29e56d9SToomas Soome lofi_online_dev(dev_info_t *dip)
1837*a29e56d9SToomas Soome {
1838*a29e56d9SToomas Soome boolean_t labeled;
1839*a29e56d9SToomas Soome int error;
1840*a29e56d9SToomas Soome int instance = ddi_get_instance(dip);
1841*a29e56d9SToomas Soome struct lofi_state *lsp;
1842*a29e56d9SToomas Soome
1843*a29e56d9SToomas Soome labeled = B_FALSE;
1844*a29e56d9SToomas Soome if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "labeled"))
1845*a29e56d9SToomas Soome labeled = B_TRUE;
1846*a29e56d9SToomas Soome
1847*a29e56d9SToomas Soome /* lsp alloc+init, soft state is freed in lofi_detach */
1848*a29e56d9SToomas Soome error = ddi_soft_state_zalloc(lofi_statep, instance);
1849*a29e56d9SToomas Soome if (error == DDI_FAILURE) {
1850*a29e56d9SToomas Soome return (ENOMEM);
1851*a29e56d9SToomas Soome }
1852*a29e56d9SToomas Soome
1853*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, instance);
1854*a29e56d9SToomas Soome lsp->ls_dip = dip;
1855*a29e56d9SToomas Soome
1856*a29e56d9SToomas Soome if ((error = lofi_zone_bind(lsp)) != 0)
1857*a29e56d9SToomas Soome goto err;
1858*a29e56d9SToomas Soome
1859*a29e56d9SToomas Soome cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
1860*a29e56d9SToomas Soome mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
1861*a29e56d9SToomas Soome mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL);
1862*a29e56d9SToomas Soome mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
1863*a29e56d9SToomas Soome mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
1864*a29e56d9SToomas Soome
1865*a29e56d9SToomas Soome if ((error = lofi_create_minor_nodes(lsp, labeled)) != 0) {
1866*a29e56d9SToomas Soome lofi_zone_unbind(lsp);
1867*a29e56d9SToomas Soome goto lerr;
1868*a29e56d9SToomas Soome }
1869*a29e56d9SToomas Soome
1870*a29e56d9SToomas Soome /* driver handles kernel-issued IOCTLs */
1871*a29e56d9SToomas Soome if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1872*a29e56d9SToomas Soome DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1873*a29e56d9SToomas Soome error = DDI_FAILURE;
1874*a29e56d9SToomas Soome goto merr;
1875*a29e56d9SToomas Soome }
1876*a29e56d9SToomas Soome
1877*a29e56d9SToomas Soome lsp->ls_kstat = kstat_create_zone(LOFI_DRIVER_NAME, instance,
1878*a29e56d9SToomas Soome NULL, "disk", KSTAT_TYPE_IO, 1, 0, getzoneid());
1879*a29e56d9SToomas Soome if (lsp->ls_kstat == NULL) {
1880*a29e56d9SToomas Soome (void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip,
1881*a29e56d9SToomas Soome DDI_KERNEL_IOCTL);
1882*a29e56d9SToomas Soome error = ENOMEM;
1883*a29e56d9SToomas Soome goto merr;
1884*a29e56d9SToomas Soome }
1885*a29e56d9SToomas Soome
1886*a29e56d9SToomas Soome lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
1887*a29e56d9SToomas Soome kstat_zone_add(lsp->ls_kstat, GLOBAL_ZONEID);
1888*a29e56d9SToomas Soome kstat_install(lsp->ls_kstat);
1889*a29e56d9SToomas Soome return (DDI_SUCCESS);
1890*a29e56d9SToomas Soome merr:
1891*a29e56d9SToomas Soome if (lsp->ls_cmlbhandle != NULL) {
1892*a29e56d9SToomas Soome cmlb_detach(lsp->ls_cmlbhandle, 0);
1893*a29e56d9SToomas Soome cmlb_free_handle(&lsp->ls_cmlbhandle);
1894*a29e56d9SToomas Soome }
1895*a29e56d9SToomas Soome ddi_remove_minor_node(dip, NULL);
1896*a29e56d9SToomas Soome lofi_zone_unbind(lsp);
1897*a29e56d9SToomas Soome lerr:
1898*a29e56d9SToomas Soome mutex_destroy(&lsp->ls_comp_cache_lock);
1899*a29e56d9SToomas Soome mutex_destroy(&lsp->ls_comp_bufs_lock);
1900*a29e56d9SToomas Soome mutex_destroy(&lsp->ls_kstat_lock);
1901*a29e56d9SToomas Soome mutex_destroy(&lsp->ls_vp_lock);
1902*a29e56d9SToomas Soome cv_destroy(&lsp->ls_vp_cv);
1903*a29e56d9SToomas Soome err:
1904*a29e56d9SToomas Soome ddi_soft_state_free(lofi_statep, instance);
1905*a29e56d9SToomas Soome return (error);
1906*a29e56d9SToomas Soome }
1907*a29e56d9SToomas Soome
1908*a29e56d9SToomas Soome /*ARGSUSED*/
1909*a29e56d9SToomas Soome static int
lofi_dev_callback(sysevent_t * ev,void * cookie)1910*a29e56d9SToomas Soome lofi_dev_callback(sysevent_t *ev, void *cookie)
1911*a29e56d9SToomas Soome {
1912*a29e56d9SToomas Soome nvlist_t *nvlist;
1913*a29e56d9SToomas Soome char *class, *driver;
1914*a29e56d9SToomas Soome char name[10];
1915*a29e56d9SToomas Soome int32_t instance;
1916*a29e56d9SToomas Soome
1917*a29e56d9SToomas Soome class = sysevent_get_class_name(ev);
1918*a29e56d9SToomas Soome if (strcmp(class, EC_DEV_ADD) && strcmp(class, EC_DEV_REMOVE))
1919*a29e56d9SToomas Soome return (0);
1920*a29e56d9SToomas Soome
1921*a29e56d9SToomas Soome (void) sysevent_get_attr_list(ev, &nvlist);
1922*a29e56d9SToomas Soome driver = fnvlist_lookup_string(nvlist, DEV_DRIVER_NAME);
1923*a29e56d9SToomas Soome instance = fnvlist_lookup_int32(nvlist, DEV_INSTANCE);
1924*a29e56d9SToomas Soome
1925*a29e56d9SToomas Soome if (strcmp(driver, LOFI_DRIVER_NAME) != 0) {
1926*a29e56d9SToomas Soome fnvlist_free(nvlist);
1927*a29e56d9SToomas Soome return (0);
1928*a29e56d9SToomas Soome }
1929*a29e56d9SToomas Soome
1930*a29e56d9SToomas Soome /*
1931*a29e56d9SToomas Soome * insert or remove device info, then announce the change
1932*a29e56d9SToomas Soome * via cv_broadcast.
1933*a29e56d9SToomas Soome * This allows the MAP/UNMAP to monitor device change.
1934*a29e56d9SToomas Soome */
1935*a29e56d9SToomas Soome (void) snprintf(name, sizeof (name), "%d", instance);
1936*a29e56d9SToomas Soome if (strcmp(class, EC_DEV_ADD) == 0) {
1937*a29e56d9SToomas Soome mutex_enter(&lofi_chan_lock);
1938*a29e56d9SToomas Soome fnvlist_add_nvlist(lofi_devlink_cache, name, nvlist);
1939*a29e56d9SToomas Soome cv_broadcast(&lofi_chan_cv);
1940*a29e56d9SToomas Soome mutex_exit(&lofi_chan_lock);
1941*a29e56d9SToomas Soome } else if (strcmp(class, EC_DEV_REMOVE) == 0) {
1942*a29e56d9SToomas Soome mutex_enter(&lofi_chan_lock);
1943*a29e56d9SToomas Soome /* Can not use fnvlist_remove() as we can get ENOENT. */
1944*a29e56d9SToomas Soome (void) nvlist_remove_all(lofi_devlink_cache, name);
1945*a29e56d9SToomas Soome cv_broadcast(&lofi_chan_cv);
1946*a29e56d9SToomas Soome mutex_exit(&lofi_chan_lock);
1947*a29e56d9SToomas Soome }
1948*a29e56d9SToomas Soome
1949*a29e56d9SToomas Soome fnvlist_free(nvlist);
1950*a29e56d9SToomas Soome return (0);
1951*a29e56d9SToomas Soome }
1952*a29e56d9SToomas Soome
1953*a29e56d9SToomas Soome static int
lofi_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)19547c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
19557c478bd9Sstevel@tonic-gate {
1956*a29e56d9SToomas Soome int rv;
1957*a29e56d9SToomas Soome int instance = ddi_get_instance(dip);
1958*a29e56d9SToomas Soome struct lofi_state *lsp;
19597c478bd9Sstevel@tonic-gate
19607c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH)
19617c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
19620fbb751dSJohn Levon
1963*a29e56d9SToomas Soome /*
1964*a29e56d9SToomas Soome * Instance 0 is control instance, attaching control instance
1965*a29e56d9SToomas Soome * will set the lofi up and ready.
1966*a29e56d9SToomas Soome */
1967*a29e56d9SToomas Soome if (instance == 0) {
1968*a29e56d9SToomas Soome rv = ddi_soft_state_zalloc(lofi_statep, 0);
1969*a29e56d9SToomas Soome if (rv == DDI_FAILURE) {
19707c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
19717c478bd9Sstevel@tonic-gate }
1972*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, instance);
1973*a29e56d9SToomas Soome rv = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
1974*a29e56d9SToomas Soome DDI_PSEUDO, 0);
1975*a29e56d9SToomas Soome if (rv == DDI_FAILURE) {
19767c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0);
19777c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
19787c478bd9Sstevel@tonic-gate }
1979843e1988Sjohnlev /* driver handles kernel-issued IOCTLs */
1980843e1988Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1981843e1988Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1982843e1988Sjohnlev ddi_remove_minor_node(dip, NULL);
1983843e1988Sjohnlev ddi_soft_state_free(lofi_statep, 0);
1984843e1988Sjohnlev return (DDI_FAILURE);
1985843e1988Sjohnlev }
19860fbb751dSJohn Levon
1987*a29e56d9SToomas Soome rv = sysevent_evc_bind(DEVFS_CHANNEL, &lofi_chan,
1988*a29e56d9SToomas Soome EVCH_CREAT | EVCH_HOLD_PEND);
1989*a29e56d9SToomas Soome if (rv == 0) {
1990*a29e56d9SToomas Soome rv = sysevent_evc_subscribe(lofi_chan, "lofi",
1991*a29e56d9SToomas Soome EC_ALL, lofi_dev_callback, NULL, 0);
1992*a29e56d9SToomas Soome rv |= sysevent_evc_subscribe(lofi_chan, "disk",
1993*a29e56d9SToomas Soome EC_ALL, lofi_dev_callback, NULL, 0);
1994*a29e56d9SToomas Soome } else
1995*a29e56d9SToomas Soome lofi_chan = NULL;
1996*a29e56d9SToomas Soome if (rv != 0) {
1997*a29e56d9SToomas Soome if (lofi_chan != NULL)
1998*a29e56d9SToomas Soome (void) sysevent_evc_unbind(lofi_chan);
1999*a29e56d9SToomas Soome ddi_prop_remove_all(dip);
2000*a29e56d9SToomas Soome ddi_remove_minor_node(dip, NULL);
2001*a29e56d9SToomas Soome ddi_soft_state_free(lofi_statep, 0);
2002*a29e56d9SToomas Soome return (DDI_FAILURE);
2003*a29e56d9SToomas Soome }
20040fbb751dSJohn Levon zone_key_create(&lofi_zone_key, NULL, lofi_zone_shutdown, NULL);
20050fbb751dSJohn Levon
2006*a29e56d9SToomas Soome lsp->ls_dip = dip;
2007*a29e56d9SToomas Soome } else {
2008*a29e56d9SToomas Soome if (lofi_online_dev(dip) == DDI_FAILURE)
2009*a29e56d9SToomas Soome return (DDI_FAILURE);
2010*a29e56d9SToomas Soome }
2011*a29e56d9SToomas Soome
20127c478bd9Sstevel@tonic-gate ddi_report_dev(dip);
20137c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
20147c478bd9Sstevel@tonic-gate }
20157c478bd9Sstevel@tonic-gate
20167c478bd9Sstevel@tonic-gate static int
lofi_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)20177c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
20187c478bd9Sstevel@tonic-gate {
2019*a29e56d9SToomas Soome struct lofi_state *lsp;
2020*a29e56d9SToomas Soome int instance = ddi_get_instance(dip);
2021*a29e56d9SToomas Soome
20227c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH)
20237c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
20240fbb751dSJohn Levon
2025*a29e56d9SToomas Soome /*
2026*a29e56d9SToomas Soome * If the instance is not 0, release state.
2027*a29e56d9SToomas Soome * The instance 0 is control device, we can not detach it
2028*a29e56d9SToomas Soome * before other instances are detached.
2029*a29e56d9SToomas Soome */
2030*a29e56d9SToomas Soome if (instance != 0) {
2031*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, instance);
2032*a29e56d9SToomas Soome if (lsp != NULL && lsp->ls_vp_ready == B_FALSE) {
2033*a29e56d9SToomas Soome ddi_soft_state_free(lofi_statep, instance);
2034*a29e56d9SToomas Soome return (DDI_SUCCESS);
2035*a29e56d9SToomas Soome } else
2036*a29e56d9SToomas Soome return (DDI_FAILURE);
2037*a29e56d9SToomas Soome }
20380fbb751dSJohn Levon mutex_enter(&lofi_lock);
20390fbb751dSJohn Levon
20400fbb751dSJohn Levon if (!list_is_empty(&lofi_list)) {
20410fbb751dSJohn Levon mutex_exit(&lofi_lock);
20427c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
20430fbb751dSJohn Levon }
20440fbb751dSJohn Levon
20457c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
2046843e1988Sjohnlev ddi_prop_remove_all(dip);
20470fbb751dSJohn Levon
20480fbb751dSJohn Levon mutex_exit(&lofi_lock);
20490fbb751dSJohn Levon
2050*a29e56d9SToomas Soome (void) sysevent_evc_unbind(lofi_chan);
20510fbb751dSJohn Levon if (zone_key_delete(lofi_zone_key) != 0)
20520fbb751dSJohn Levon cmn_err(CE_WARN, "failed to delete zone key");
20530fbb751dSJohn Levon
20547c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0);
20550fbb751dSJohn Levon
20567c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate
20597c478bd9Sstevel@tonic-gate /*
2060*a29e56d9SToomas Soome * With the addition of encryption, we must be careful that encryption key is
2061*a29e56d9SToomas Soome * wiped before kernel's data structures are freed so it cannot accidentally
2062*a29e56d9SToomas Soome * slip out to userland through uninitialized data elsewhere.
20637d82f0f8SDina K Nimeh */
20647d82f0f8SDina K Nimeh static void
free_lofi_ioctl(struct lofi_ioctl * klip)20657d82f0f8SDina K Nimeh free_lofi_ioctl(struct lofi_ioctl *klip)
20667d82f0f8SDina K Nimeh {
20677d82f0f8SDina K Nimeh /* Make sure this encryption key doesn't stick around */
20687d82f0f8SDina K Nimeh bzero(klip->li_key, sizeof (klip->li_key));
20697d82f0f8SDina K Nimeh kmem_free(klip, sizeof (struct lofi_ioctl));
20707d82f0f8SDina K Nimeh }
20717d82f0f8SDina K Nimeh
20727d82f0f8SDina K Nimeh /*
2073*a29e56d9SToomas Soome * These two functions simplify the rest of the ioctls that need to copyin/out
20747c478bd9Sstevel@tonic-gate * the lofi_ioctl structure.
20757c478bd9Sstevel@tonic-gate */
20760fbb751dSJohn Levon int
copy_in_lofi_ioctl(const struct lofi_ioctl * ulip,struct lofi_ioctl ** klipp,int flag)20770fbb751dSJohn Levon copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, struct lofi_ioctl **klipp,
20780fbb751dSJohn Levon int flag)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip;
20817c478bd9Sstevel@tonic-gate int error;
20827c478bd9Sstevel@tonic-gate
20830fbb751dSJohn Levon klip = *klipp = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
2084bd07e074Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
20850fbb751dSJohn Levon if (error)
20860fbb751dSJohn Levon goto err;
20877c478bd9Sstevel@tonic-gate
20880fbb751dSJohn Levon /* ensure NULL termination */
20896f02aa44SDina K Nimeh klip->li_filename[MAXPATHLEN-1] = '\0';
2090*a29e56d9SToomas Soome klip->li_devpath[MAXPATHLEN-1] = '\0';
20910fbb751dSJohn Levon klip->li_algorithm[MAXALGLEN-1] = '\0';
20920fbb751dSJohn Levon klip->li_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
20930fbb751dSJohn Levon klip->li_iv_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
20947c478bd9Sstevel@tonic-gate
2095*a29e56d9SToomas Soome if (klip->li_id > L_MAXMIN32) {
20960fbb751dSJohn Levon error = EINVAL;
20970fbb751dSJohn Levon goto err;
20987c478bd9Sstevel@tonic-gate }
20990fbb751dSJohn Levon
21000fbb751dSJohn Levon return (0);
21010fbb751dSJohn Levon
21020fbb751dSJohn Levon err:
21030fbb751dSJohn Levon free_lofi_ioctl(klip);
21040fbb751dSJohn Levon return (error);
21057c478bd9Sstevel@tonic-gate }
21067c478bd9Sstevel@tonic-gate
21077c478bd9Sstevel@tonic-gate int
copy_out_lofi_ioctl(const struct lofi_ioctl * klip,struct lofi_ioctl * ulip,int flag)2108bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
2109bd07e074Sheppo int flag)
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate int error;
21127c478bd9Sstevel@tonic-gate
21137d82f0f8SDina K Nimeh /*
21147d82f0f8SDina K Nimeh * NOTE: Do NOT copy the crypto_key_t "back" to userland.
21157d82f0f8SDina K Nimeh * This ensures that an attacker can't trivially find the
21167d82f0f8SDina K Nimeh * key for a mapping just by issuing the ioctl.
21177d82f0f8SDina K Nimeh *
21187d82f0f8SDina K Nimeh * It can still be found by poking around in kmem with mdb(1),
21197d82f0f8SDina K Nimeh * but there is no point in making it easy when the info isn't
21207d82f0f8SDina K Nimeh * of any use in this direction anyway.
21217d82f0f8SDina K Nimeh *
21227d82f0f8SDina K Nimeh * Either way we don't actually have the raw key stored in
21237d82f0f8SDina K Nimeh * a form that we can get it anyway, since we just used it
21247d82f0f8SDina K Nimeh * to create a ctx template and didn't keep "the original".
21257d82f0f8SDina K Nimeh */
2126bd07e074Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
21277c478bd9Sstevel@tonic-gate if (error)
21287c478bd9Sstevel@tonic-gate return (EFAULT);
21297c478bd9Sstevel@tonic-gate return (0);
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate
21320fbb751dSJohn Levon static int
lofi_access(struct lofi_state * lsp)21330fbb751dSJohn Levon lofi_access(struct lofi_state *lsp)
21340fbb751dSJohn Levon {
21350fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
2136a19609f8Sjv227347 if (INGLOBALZONE(curproc) || lsp->ls_zone.zref_zone == curzone)
21370fbb751dSJohn Levon return (0);
21380fbb751dSJohn Levon return (EPERM);
21390fbb751dSJohn Levon }
21400fbb751dSJohn Levon
21417c478bd9Sstevel@tonic-gate /*
21420fbb751dSJohn Levon * Find the lofi state for the given filename. We compare by vnode to
21430fbb751dSJohn Levon * allow the global zone visibility into NGZ lofi nodes.
21447c478bd9Sstevel@tonic-gate */
21457c478bd9Sstevel@tonic-gate static int
file_to_lofi_nocheck(char * filename,boolean_t readonly,struct lofi_state ** lspp)2146cd69fabeSAlexander Eremin file_to_lofi_nocheck(char *filename, boolean_t readonly,
2147cd69fabeSAlexander Eremin struct lofi_state **lspp)
21487c478bd9Sstevel@tonic-gate {
21497c478bd9Sstevel@tonic-gate struct lofi_state *lsp;
21500fbb751dSJohn Levon vnode_t *vp = NULL;
21510fbb751dSJohn Levon int err = 0;
2152cd69fabeSAlexander Eremin int rdfiles = 0;
21537c478bd9Sstevel@tonic-gate
21540fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
21550fbb751dSJohn Levon
21560fbb751dSJohn Levon if ((err = lookupname(filename, UIO_SYSSPACE, FOLLOW,
21570fbb751dSJohn Levon NULLVPP, &vp)) != 0)
21580fbb751dSJohn Levon goto out;
21590fbb751dSJohn Levon
21600fbb751dSJohn Levon if (vp->v_type == VREG) {
21610fbb751dSJohn Levon vnode_t *realvp;
21620fbb751dSJohn Levon if (VOP_REALVP(vp, &realvp, NULL) == 0) {
21630fbb751dSJohn Levon VN_HOLD(realvp);
21640fbb751dSJohn Levon VN_RELE(vp);
21650fbb751dSJohn Levon vp = realvp;
21667c478bd9Sstevel@tonic-gate }
21670fbb751dSJohn Levon }
21680fbb751dSJohn Levon
21690fbb751dSJohn Levon for (lsp = list_head(&lofi_list); lsp != NULL;
21700fbb751dSJohn Levon lsp = list_next(&lofi_list, lsp)) {
21710fbb751dSJohn Levon if (lsp->ls_vp == vp) {
21720fbb751dSJohn Levon if (lspp != NULL)
21730fbb751dSJohn Levon *lspp = lsp;
2174cd69fabeSAlexander Eremin if (lsp->ls_readonly) {
2175cd69fabeSAlexander Eremin rdfiles++;
2176cd69fabeSAlexander Eremin /* Skip if '-r' is specified */
2177cd69fabeSAlexander Eremin if (readonly)
2178cd69fabeSAlexander Eremin continue;
2179cd69fabeSAlexander Eremin }
21800fbb751dSJohn Levon goto out;
21810fbb751dSJohn Levon }
21820fbb751dSJohn Levon }
21830fbb751dSJohn Levon
21840fbb751dSJohn Levon err = ENOENT;
21850fbb751dSJohn Levon
2186cd69fabeSAlexander Eremin /*
2187cd69fabeSAlexander Eremin * If a filename is given as an argument for lofi_unmap, we shouldn't
2188cd69fabeSAlexander Eremin * allow unmap if there are multiple read-only lofi devices associated
2189cd69fabeSAlexander Eremin * with this file.
2190cd69fabeSAlexander Eremin */
2191cd69fabeSAlexander Eremin if (lspp != NULL) {
2192cd69fabeSAlexander Eremin if (rdfiles == 1)
2193cd69fabeSAlexander Eremin err = 0;
2194cd69fabeSAlexander Eremin else if (rdfiles > 1)
2195cd69fabeSAlexander Eremin err = EBUSY;
2196cd69fabeSAlexander Eremin }
2197cd69fabeSAlexander Eremin
21980fbb751dSJohn Levon out:
21990fbb751dSJohn Levon if (vp != NULL)
22000fbb751dSJohn Levon VN_RELE(vp);
22010fbb751dSJohn Levon return (err);
22027c478bd9Sstevel@tonic-gate }
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate /*
22050fbb751dSJohn Levon * Find the minor for the given filename, checking the zone can access
22060fbb751dSJohn Levon * it.
22077c478bd9Sstevel@tonic-gate */
22087c478bd9Sstevel@tonic-gate static int
file_to_lofi(char * filename,boolean_t readonly,struct lofi_state ** lspp)2209cd69fabeSAlexander Eremin file_to_lofi(char *filename, boolean_t readonly, struct lofi_state **lspp)
22107c478bd9Sstevel@tonic-gate {
22110fbb751dSJohn Levon int err = 0;
22127c478bd9Sstevel@tonic-gate
22130fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock));
22140fbb751dSJohn Levon
2215cd69fabeSAlexander Eremin if ((err = file_to_lofi_nocheck(filename, readonly, lspp)) != 0)
22160fbb751dSJohn Levon return (err);
22170fbb751dSJohn Levon
22180fbb751dSJohn Levon if ((err = lofi_access(*lspp)) != 0)
22190fbb751dSJohn Levon return (err);
22200fbb751dSJohn Levon
22217c478bd9Sstevel@tonic-gate return (0);
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate
22247c478bd9Sstevel@tonic-gate /*
2225*a29e56d9SToomas Soome * Fakes up a disk geometry based on the size of the file. This is needed
2226*a29e56d9SToomas Soome * to support newfs on traditional lofi device, but also will provide
2227*a29e56d9SToomas Soome * geometry hint for cmlb.
22287c478bd9Sstevel@tonic-gate */
22297c478bd9Sstevel@tonic-gate static void
fake_disk_geometry(struct lofi_state * lsp)22307c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
22317c478bd9Sstevel@tonic-gate {
22327d82f0f8SDina K Nimeh u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
22337d82f0f8SDina K Nimeh
22347c478bd9Sstevel@tonic-gate /* dk_geom - see dkio(7I) */
22357c478bd9Sstevel@tonic-gate /*
22367c478bd9Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
22377c478bd9Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to
22387c478bd9Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create
22397c478bd9Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick
22407c478bd9Sstevel@tonic-gate * a number between one and one).
22417c478bd9Sstevel@tonic-gate *
22427c478bd9Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders
22437c478bd9Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space
22447c478bd9Sstevel@tonic-gate * for backup superblocks or cylinder group structures.
22457c478bd9Sstevel@tonic-gate */
2246*a29e56d9SToomas Soome bzero(&lsp->ls_dkg, sizeof (lsp->ls_dkg));
22477d82f0f8SDina K Nimeh if (dsize < (2 * 1024 * 1024)) /* floppy? */
22487d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
22497c478bd9Sstevel@tonic-gate else
22507d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
22517c478bd9Sstevel@tonic-gate /* in case file file is < 100k */
22527c478bd9Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0)
22537c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1;
22547c478bd9Sstevel@tonic-gate
2255*a29e56d9SToomas Soome lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl;
2256*a29e56d9SToomas Soome lsp->ls_dkg.dkg_nhead = 1;
2257*a29e56d9SToomas Soome lsp->ls_dkg.dkg_rpm = 7200;
2258*a29e56d9SToomas Soome
2259*a29e56d9SToomas Soome lsp->ls_dkg.dkg_nsect = dsize /
2260*a29e56d9SToomas Soome (lsp->ls_dkg.dkg_ncyl << lsp->ls_pbshift);
2261*a29e56d9SToomas Soome }
2262*a29e56d9SToomas Soome
2263*a29e56d9SToomas Soome /*
2264*a29e56d9SToomas Soome * build vtoc - see dkio(7I)
2265*a29e56d9SToomas Soome *
2266*a29e56d9SToomas Soome * Fakes one big partition based on the size of the file. This is needed
2267*a29e56d9SToomas Soome * because we allow newfs'ing the traditional lofi device and newfs will
2268*a29e56d9SToomas Soome * do several disk ioctls to figure out the geometry and partition information.
2269*a29e56d9SToomas Soome * It uses that information to determine the parameters to pass to mkfs.
2270*a29e56d9SToomas Soome */
2271*a29e56d9SToomas Soome static void
fake_disk_vtoc(struct lofi_state * lsp,struct vtoc * vt)2272*a29e56d9SToomas Soome fake_disk_vtoc(struct lofi_state *lsp, struct vtoc *vt)
2273*a29e56d9SToomas Soome {
2274*a29e56d9SToomas Soome bzero(vt, sizeof (struct vtoc));
2275*a29e56d9SToomas Soome vt->v_sanity = VTOC_SANE;
2276*a29e56d9SToomas Soome vt->v_version = V_VERSION;
2277*a29e56d9SToomas Soome (void) strncpy(vt->v_volume, LOFI_DRIVER_NAME,
2278*a29e56d9SToomas Soome sizeof (vt->v_volume));
2279*a29e56d9SToomas Soome vt->v_sectorsz = 1 << lsp->ls_pbshift;
2280*a29e56d9SToomas Soome vt->v_nparts = 1;
2281*a29e56d9SToomas Soome vt->v_part[0].p_tag = V_UNASSIGNED;
228287117650Saalok
228387117650Saalok /*
228487117650Saalok * A compressed file is read-only, other files can
228587117650Saalok * be read-write
228687117650Saalok */
228787117650Saalok if (lsp->ls_uncomp_seg_sz > 0) {
2288*a29e56d9SToomas Soome vt->v_part[0].p_flag = V_UNMNT | V_RONLY;
228987117650Saalok } else {
2290*a29e56d9SToomas Soome vt->v_part[0].p_flag = V_UNMNT;
229187117650Saalok }
2292*a29e56d9SToomas Soome vt->v_part[0].p_start = (daddr_t)0;
22937c478bd9Sstevel@tonic-gate /*
22947c478bd9Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because
22957c478bd9Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case,
22967c478bd9Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size
22977c478bd9Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder.
22987c478bd9Sstevel@tonic-gate */
2299*a29e56d9SToomas Soome vt->v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
23007c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
2301*a29e56d9SToomas Soome }
23027c478bd9Sstevel@tonic-gate
2303*a29e56d9SToomas Soome /*
2304*a29e56d9SToomas Soome * build dk_cinfo - see dkio(7I)
2305*a29e56d9SToomas Soome */
2306*a29e56d9SToomas Soome static void
fake_disk_info(dev_t dev,struct dk_cinfo * ci)2307*a29e56d9SToomas Soome fake_disk_info(dev_t dev, struct dk_cinfo *ci)
2308*a29e56d9SToomas Soome {
2309*a29e56d9SToomas Soome bzero(ci, sizeof (struct dk_cinfo));
2310*a29e56d9SToomas Soome (void) strlcpy(ci->dki_cname, LOFI_DRIVER_NAME, sizeof (ci->dki_cname));
2311*a29e56d9SToomas Soome ci->dki_ctype = DKC_SCSI_CCS;
2312*a29e56d9SToomas Soome (void) strlcpy(ci->dki_dname, LOFI_DRIVER_NAME, sizeof (ci->dki_dname));
2313*a29e56d9SToomas Soome ci->dki_unit = LOFI_MINOR2ID(getminor(dev));
2314*a29e56d9SToomas Soome ci->dki_partition = LOFI_PART(getminor(dev));
23157c478bd9Sstevel@tonic-gate /*
23167c478bd9Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it
23177c478bd9Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides
23187c478bd9Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because
23197c478bd9Sstevel@tonic-gate * maxcontig is 0.
23207c478bd9Sstevel@tonic-gate */
2321*a29e56d9SToomas Soome ci->dki_maxtransfer = 16;
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate
23247c478bd9Sstevel@tonic-gate /*
232587117650Saalok * map in a compressed file
232687117650Saalok *
232787117650Saalok * Read in the header and the index that follows.
232887117650Saalok *
232987117650Saalok * The header is as follows -
233087117650Saalok *
233187117650Saalok * Signature (name of the compression algorithm)
233287117650Saalok * Compression segment size (a multiple of 512)
233387117650Saalok * Number of index entries
233487117650Saalok * Size of the last block
233587117650Saalok * The array containing the index entries
233687117650Saalok *
233787117650Saalok * The header information is always stored in
233887117650Saalok * network byte order on disk.
233987117650Saalok */
234087117650Saalok static int
lofi_map_compressed_file(struct lofi_state * lsp,char * buf)234187117650Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
234287117650Saalok {
234387117650Saalok uint32_t index_sz, header_len, i;
234487117650Saalok ssize_t resid;
234587117650Saalok enum uio_rw rw;
234687117650Saalok char *tbuf = buf;
234787117650Saalok int error;
234887117650Saalok
234987117650Saalok /* The signature has already been read */
235087117650Saalok tbuf += sizeof (lsp->ls_comp_algorithm);
235187117650Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
235287117650Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
235387117650Saalok
235487117650Saalok /*
235587117650Saalok * The compressed segment size must be a power of 2
235687117650Saalok */
23574058a205Sjrgn.keil@googlemail.com if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
23584058a205Sjrgn.keil@googlemail.com !ISP2(lsp->ls_uncomp_seg_sz))
235987117650Saalok return (EINVAL);
236087117650Saalok
236187117650Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
236287117650Saalok ;
236387117650Saalok
236487117650Saalok lsp->ls_comp_seg_shift = i;
236587117650Saalok
236687117650Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz);
236787117650Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
236887117650Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
236987117650Saalok
237087117650Saalok tbuf += sizeof (lsp->ls_comp_index_sz);
237187117650Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
237287117650Saalok sizeof (lsp->ls_uncomp_last_seg_sz));
237387117650Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
237487117650Saalok
237587117650Saalok /*
237687117650Saalok * Compute the total size of the uncompressed data
237787117650Saalok * for use in fake_disk_geometry and other calculations.
237887117650Saalok * Disk geometry has to be faked with respect to the
237987117650Saalok * actual uncompressed data size rather than the
238087117650Saalok * compressed file size.
238187117650Saalok */
23827d11f38eSDavid Miner lsp->ls_vp_size =
23837d11f38eSDavid Miner (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
238487117650Saalok + lsp->ls_uncomp_last_seg_sz;
238587117650Saalok
238687117650Saalok /*
2387b1efbcd6SAlok Aggarwal * Index size is rounded up to DEV_BSIZE for ease
238887117650Saalok * of segmapping
238987117650Saalok */
239087117650Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
239187117650Saalok header_len = sizeof (lsp->ls_comp_algorithm) +
239287117650Saalok sizeof (lsp->ls_uncomp_seg_sz) +
239387117650Saalok sizeof (lsp->ls_comp_index_sz) +
239487117650Saalok sizeof (lsp->ls_uncomp_last_seg_sz);
239587117650Saalok lsp->ls_comp_offbase = header_len + index_sz;
239687117650Saalok
239787117650Saalok index_sz += header_len;
239887117650Saalok index_sz = roundup(index_sz, DEV_BSIZE);
239987117650Saalok
240087117650Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
240187117650Saalok lsp->ls_comp_index_data_sz = index_sz;
240287117650Saalok
240387117650Saalok /*
240487117650Saalok * Read in the index -- this has a side-effect
240587117650Saalok * of reading in the header as well
240687117650Saalok */
240787117650Saalok rw = UIO_READ;
240887117650Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
240987117650Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
241087117650Saalok
241187117650Saalok if (error != 0)
241287117650Saalok return (error);
241387117650Saalok
241487117650Saalok /* Skip the header, this is where the index really begins */
241587117650Saalok lsp->ls_comp_seg_index =
241687117650Saalok /*LINTED*/
241787117650Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len);
241887117650Saalok
241987117650Saalok /*
242087117650Saalok * Now recompute offsets in the index to account for
242187117650Saalok * the header length
242287117650Saalok */
242387117650Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) {
242487117650Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
242587117650Saalok BE_64(lsp->ls_comp_seg_index[i]);
242687117650Saalok }
242787117650Saalok
242887117650Saalok return (error);
242987117650Saalok }
243087117650Saalok
24310fbb751dSJohn Levon static int
lofi_init_crypto(struct lofi_state * lsp,struct lofi_ioctl * klip)24320fbb751dSJohn Levon lofi_init_crypto(struct lofi_state *lsp, struct lofi_ioctl *klip)
243387117650Saalok {
24340fbb751dSJohn Levon struct crypto_meta chead;
24350fbb751dSJohn Levon char buf[DEV_BSIZE];
24360fbb751dSJohn Levon ssize_t resid;
24370fbb751dSJohn Levon char *marker;
24380fbb751dSJohn Levon int error;
24390fbb751dSJohn Levon int ret;
244087117650Saalok int i;
244187117650Saalok
24420fbb751dSJohn Levon if (!klip->li_crypto_enabled)
24430fbb751dSJohn Levon return (0);
244487117650Saalok
244587117650Saalok /*
24460fbb751dSJohn Levon * All current algorithms have a max of 448 bits.
24477c478bd9Sstevel@tonic-gate */
24480fbb751dSJohn Levon if (klip->li_iv_len > CRYPTO_BITS2BYTES(512))
24490fbb751dSJohn Levon return (EINVAL);
24507c478bd9Sstevel@tonic-gate
24510fbb751dSJohn Levon if (CRYPTO_BITS2BYTES(klip->li_key_len) > sizeof (klip->li_key))
24520fbb751dSJohn Levon return (EINVAL);
24537c478bd9Sstevel@tonic-gate
24540fbb751dSJohn Levon lsp->ls_crypto_enabled = klip->li_crypto_enabled;
245587117650Saalok
24567d82f0f8SDina K Nimeh mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
24577d82f0f8SDina K Nimeh
24587d82f0f8SDina K Nimeh lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
24597d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
24607d82f0f8SDina K Nimeh cmn_err(CE_WARN, "invalid cipher %s requested for %s",
24610fbb751dSJohn Levon klip->li_cipher, klip->li_filename);
24620fbb751dSJohn Levon return (EINVAL);
24637d82f0f8SDina K Nimeh }
24647d82f0f8SDina K Nimeh
24657d82f0f8SDina K Nimeh /* this is just initialization here */
24667d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = NULL;
24677d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = 0;
24687d82f0f8SDina K Nimeh
24697d82f0f8SDina K Nimeh lsp->ls_iv_type = klip->li_iv_type;
24707d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
24717d82f0f8SDina K Nimeh if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
24727d82f0f8SDina K Nimeh cmn_err(CE_WARN, "invalid iv cipher %s requested"
24730fbb751dSJohn Levon " for %s", klip->li_iv_cipher, klip->li_filename);
24740fbb751dSJohn Levon return (EINVAL);
24757d82f0f8SDina K Nimeh }
24767d82f0f8SDina K Nimeh
24777d82f0f8SDina K Nimeh /* iv mech must itself take a null iv */
24787d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param = NULL;
24797d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len = 0;
24807d82f0f8SDina K Nimeh lsp->ls_iv_len = klip->li_iv_len;
24817d82f0f8SDina K Nimeh
24827d82f0f8SDina K Nimeh /*
24837d82f0f8SDina K Nimeh * Create ctx using li_cipher & the raw li_key after checking
24847d82f0f8SDina K Nimeh * that it isn't a weak key.
24857d82f0f8SDina K Nimeh */
24867d82f0f8SDina K Nimeh lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
24877d82f0f8SDina K Nimeh lsp->ls_key.ck_length = klip->li_key_len;
24887d82f0f8SDina K Nimeh lsp->ls_key.ck_data = kmem_alloc(
24897d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
24907d82f0f8SDina K Nimeh bcopy(klip->li_key, lsp->ls_key.ck_data,
24917d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
24927d82f0f8SDina K Nimeh
24937d82f0f8SDina K Nimeh ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
24947d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) {
24957d82f0f8SDina K Nimeh cmn_err(CE_WARN, "weak key check failed for cipher "
24967d82f0f8SDina K Nimeh "%s on file %s (0x%x)", klip->li_cipher,
24970fbb751dSJohn Levon klip->li_filename, ret);
24980fbb751dSJohn Levon return (EINVAL);
24997d82f0f8SDina K Nimeh }
25007d82f0f8SDina K Nimeh
25010fbb751dSJohn Levon error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE,
25027d82f0f8SDina K Nimeh CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
25037d82f0f8SDina K Nimeh if (error != 0)
25040fbb751dSJohn Levon return (error);
25057d82f0f8SDina K Nimeh
25067d82f0f8SDina K Nimeh /*
25070fbb751dSJohn Levon * This is the case where the header in the lofi image is already
25080fbb751dSJohn Levon * initialized to indicate it is encrypted.
25097d82f0f8SDina K Nimeh */
25100fbb751dSJohn Levon if (strncmp(buf, lofi_crypto_magic, sizeof (lofi_crypto_magic)) == 0) {
25117d82f0f8SDina K Nimeh /*
25127d82f0f8SDina K Nimeh * The encryption header information is laid out this way:
25137d82f0f8SDina K Nimeh * 6 bytes: hex "CFLOFI"
25147d82f0f8SDina K Nimeh * 2 bytes: version = 0 ... for now
25157d82f0f8SDina K Nimeh * 96 bytes: reserved1 (not implemented yet)
25167d82f0f8SDina K Nimeh * 4 bytes: data_sector = 2 ... for now
25177d82f0f8SDina K Nimeh * more... not implemented yet
25187d82f0f8SDina K Nimeh */
25197d82f0f8SDina K Nimeh
25200fbb751dSJohn Levon marker = buf;
25210fbb751dSJohn Levon
25227d82f0f8SDina K Nimeh /* copy the magic */
25237d82f0f8SDina K Nimeh bcopy(marker, lsp->ls_crypto.magic,
25247d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.magic));
25257d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.magic);
25267d82f0f8SDina K Nimeh
25277d82f0f8SDina K Nimeh /* read the encryption version number */
25287d82f0f8SDina K Nimeh bcopy(marker, &(lsp->ls_crypto.version),
25297d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.version));
25307d82f0f8SDina K Nimeh lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
25317d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.version);
25327d82f0f8SDina K Nimeh
25337d82f0f8SDina K Nimeh /* read a chunk of reserved data */
25347d82f0f8SDina K Nimeh bcopy(marker, lsp->ls_crypto.reserved1,
25357d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.reserved1));
25367d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.reserved1);
25377d82f0f8SDina K Nimeh
25387d82f0f8SDina K Nimeh /* read block number where encrypted data begins */
25397d82f0f8SDina K Nimeh bcopy(marker, &(lsp->ls_crypto.data_sector),
25407d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.data_sector));
25417d82f0f8SDina K Nimeh lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
25427d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.data_sector);
25437d82f0f8SDina K Nimeh
25447d82f0f8SDina K Nimeh /* and ignore the rest until it is implemented */
25457d82f0f8SDina K Nimeh
25467d82f0f8SDina K Nimeh lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
25470fbb751dSJohn Levon return (0);
25487d82f0f8SDina K Nimeh }
25497d82f0f8SDina K Nimeh
25500fbb751dSJohn Levon /*
25510fbb751dSJohn Levon * We've requested encryption, but no magic was found, so it must be
25520fbb751dSJohn Levon * a new image.
25530fbb751dSJohn Levon */
25540fbb751dSJohn Levon
25550fbb751dSJohn Levon for (i = 0; i < sizeof (struct crypto_meta); i++) {
25560fbb751dSJohn Levon if (buf[i] != '\0')
25570fbb751dSJohn Levon return (EINVAL);
25580fbb751dSJohn Levon }
25590fbb751dSJohn Levon
25600fbb751dSJohn Levon marker = buf;
25617d82f0f8SDina K Nimeh bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
25627d82f0f8SDina K Nimeh marker += sizeof (lofi_crypto_magic);
25637d82f0f8SDina K Nimeh chead.version = htons(LOFI_CRYPTO_VERSION);
25647d82f0f8SDina K Nimeh bcopy(&(chead.version), marker, sizeof (chead.version));
25657d82f0f8SDina K Nimeh marker += sizeof (chead.version);
25667d82f0f8SDina K Nimeh marker += sizeof (chead.reserved1);
25677d82f0f8SDina K Nimeh chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
25687d82f0f8SDina K Nimeh bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
25697d82f0f8SDina K Nimeh
25707d82f0f8SDina K Nimeh /* write the header */
25710fbb751dSJohn Levon error = vn_rdwr(UIO_WRITE, lsp->ls_vp, buf, DEV_BSIZE,
25727d82f0f8SDina K Nimeh CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
25737d82f0f8SDina K Nimeh if (error != 0)
25740fbb751dSJohn Levon return (error);
25757d82f0f8SDina K Nimeh
25767d82f0f8SDina K Nimeh /* fix things up so it looks like we read this info */
25777d82f0f8SDina K Nimeh bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
25787d82f0f8SDina K Nimeh sizeof (lofi_crypto_magic));
25797d82f0f8SDina K Nimeh lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
25807d82f0f8SDina K Nimeh lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
25817d82f0f8SDina K Nimeh lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
25820fbb751dSJohn Levon return (0);
25837d82f0f8SDina K Nimeh }
25847d82f0f8SDina K Nimeh
25857d82f0f8SDina K Nimeh /*
25860fbb751dSJohn Levon * Check to see if the passed in signature is a valid one. If it is
25870fbb751dSJohn Levon * valid, return the index into lofi_compress_table.
25880fbb751dSJohn Levon *
25890fbb751dSJohn Levon * Return -1 if it is invalid
25907d82f0f8SDina K Nimeh */
25910fbb751dSJohn Levon static int
lofi_compress_select(const char * signature)25920fbb751dSJohn Levon lofi_compress_select(const char *signature)
25930fbb751dSJohn Levon {
25940fbb751dSJohn Levon int i;
25950fbb751dSJohn Levon
25960fbb751dSJohn Levon for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
25970fbb751dSJohn Levon if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
25980fbb751dSJohn Levon return (i);
259987117650Saalok }
26000fbb751dSJohn Levon
26010fbb751dSJohn Levon return (-1);
26020fbb751dSJohn Levon }
26030fbb751dSJohn Levon
26040fbb751dSJohn Levon static int
lofi_init_compress(struct lofi_state * lsp)26050fbb751dSJohn Levon lofi_init_compress(struct lofi_state *lsp)
26060fbb751dSJohn Levon {
26070fbb751dSJohn Levon char buf[DEV_BSIZE];
26080fbb751dSJohn Levon int compress_index;
26090fbb751dSJohn Levon ssize_t resid;
26100fbb751dSJohn Levon int error;
26110fbb751dSJohn Levon
26120fbb751dSJohn Levon error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
26130fbb751dSJohn Levon 0, RLIM64_INFINITY, kcred, &resid);
26140fbb751dSJohn Levon
26150fbb751dSJohn Levon if (error != 0)
26160fbb751dSJohn Levon return (error);
26170fbb751dSJohn Levon
26180fbb751dSJohn Levon if ((compress_index = lofi_compress_select(buf)) == -1)
26190fbb751dSJohn Levon return (0);
26200fbb751dSJohn Levon
26210fbb751dSJohn Levon /* compression and encryption are mutually exclusive */
26220fbb751dSJohn Levon if (lsp->ls_crypto_enabled)
26230fbb751dSJohn Levon return (ENOTSUP);
26240fbb751dSJohn Levon
26250fbb751dSJohn Levon /* initialize compression info for compressed lofi */
26260fbb751dSJohn Levon lsp->ls_comp_algorithm_index = compress_index;
26270fbb751dSJohn Levon (void) strlcpy(lsp->ls_comp_algorithm,
26280fbb751dSJohn Levon lofi_compress_table[compress_index].l_name,
26290fbb751dSJohn Levon sizeof (lsp->ls_comp_algorithm));
26300fbb751dSJohn Levon
26310fbb751dSJohn Levon /* Finally setup per-thread pre-allocated buffers */
26320fbb751dSJohn Levon lsp->ls_comp_bufs = kmem_zalloc(lofi_taskq_nthreads *
26330fbb751dSJohn Levon sizeof (struct compbuf), KM_SLEEP);
26340fbb751dSJohn Levon
26350fbb751dSJohn Levon return (lofi_map_compressed_file(lsp, buf));
26360fbb751dSJohn Levon }
26370fbb751dSJohn Levon
26380fbb751dSJohn Levon /*
2639*a29e56d9SToomas Soome * Allocate new or proposed id from lofi_id.
2640*a29e56d9SToomas Soome *
2641*a29e56d9SToomas Soome * Special cases for proposed id:
2642*a29e56d9SToomas Soome * 0: not allowed, 0 is id for control device.
2643*a29e56d9SToomas Soome * -1: allocate first usable id from lofi_id.
2644*a29e56d9SToomas Soome * any other value is proposed value from userland
2645*a29e56d9SToomas Soome *
2646*a29e56d9SToomas Soome * returns DDI_SUCCESS or errno.
2647*a29e56d9SToomas Soome */
2648*a29e56d9SToomas Soome static int
lofi_alloc_id(int * idp)2649*a29e56d9SToomas Soome lofi_alloc_id(int *idp)
2650*a29e56d9SToomas Soome {
2651*a29e56d9SToomas Soome int id, error = DDI_SUCCESS;
2652*a29e56d9SToomas Soome
2653*a29e56d9SToomas Soome if (*idp == -1) {
2654*a29e56d9SToomas Soome id = id_allocff_nosleep(lofi_id);
2655*a29e56d9SToomas Soome if (id == -1) {
2656*a29e56d9SToomas Soome error = EAGAIN;
2657*a29e56d9SToomas Soome goto err;
2658*a29e56d9SToomas Soome }
2659*a29e56d9SToomas Soome } else if (*idp == 0) {
2660*a29e56d9SToomas Soome error = EINVAL;
2661*a29e56d9SToomas Soome goto err;
2662*a29e56d9SToomas Soome } else if (*idp > ((1 << (L_BITSMINOR - LOFI_CMLB_SHIFT)) - 1)) {
2663*a29e56d9SToomas Soome error = ERANGE;
2664*a29e56d9SToomas Soome goto err;
2665*a29e56d9SToomas Soome } else {
2666*a29e56d9SToomas Soome if (ddi_get_soft_state(lofi_statep, *idp) != NULL) {
2667*a29e56d9SToomas Soome error = EEXIST;
2668*a29e56d9SToomas Soome goto err;
2669*a29e56d9SToomas Soome }
2670*a29e56d9SToomas Soome
2671*a29e56d9SToomas Soome id = id_alloc_specific_nosleep(lofi_id, *idp);
2672*a29e56d9SToomas Soome if (id == -1) {
2673*a29e56d9SToomas Soome error = EAGAIN;
2674*a29e56d9SToomas Soome goto err;
2675*a29e56d9SToomas Soome }
2676*a29e56d9SToomas Soome }
2677*a29e56d9SToomas Soome *idp = id;
2678*a29e56d9SToomas Soome err:
2679*a29e56d9SToomas Soome return (error);
2680*a29e56d9SToomas Soome }
2681*a29e56d9SToomas Soome
2682*a29e56d9SToomas Soome static int
lofi_create_dev(struct lofi_ioctl * klip)2683*a29e56d9SToomas Soome lofi_create_dev(struct lofi_ioctl *klip)
2684*a29e56d9SToomas Soome {
2685*a29e56d9SToomas Soome dev_info_t *parent, *child;
2686*a29e56d9SToomas Soome struct lofi_state *lsp = NULL;
2687*a29e56d9SToomas Soome char namebuf[MAXNAMELEN];
2688*a29e56d9SToomas Soome int error, circ;
2689*a29e56d9SToomas Soome
2690*a29e56d9SToomas Soome /* get control device */
2691*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, 0);
2692*a29e56d9SToomas Soome parent = ddi_get_parent(lsp->ls_dip);
2693*a29e56d9SToomas Soome
2694*a29e56d9SToomas Soome if ((error = lofi_alloc_id((int *)&klip->li_id)))
2695*a29e56d9SToomas Soome return (error);
2696*a29e56d9SToomas Soome
2697*a29e56d9SToomas Soome (void) snprintf(namebuf, sizeof (namebuf), LOFI_DRIVER_NAME "@%d",
2698*a29e56d9SToomas Soome klip->li_id);
2699*a29e56d9SToomas Soome
2700*a29e56d9SToomas Soome ndi_devi_enter(parent, &circ);
2701*a29e56d9SToomas Soome child = ndi_devi_findchild(parent, namebuf);
2702*a29e56d9SToomas Soome ndi_devi_exit(parent, circ);
2703*a29e56d9SToomas Soome
2704*a29e56d9SToomas Soome if (child == NULL) {
2705*a29e56d9SToomas Soome child = ddi_add_child(parent, LOFI_DRIVER_NAME,
2706*a29e56d9SToomas Soome (pnode_t)DEVI_SID_NODEID, klip->li_id);
2707*a29e56d9SToomas Soome if ((error = ddi_prop_update_int(DDI_DEV_T_NONE, child,
2708*a29e56d9SToomas Soome "instance", klip->li_id)) != DDI_PROP_SUCCESS)
2709*a29e56d9SToomas Soome goto err;
2710*a29e56d9SToomas Soome
2711*a29e56d9SToomas Soome if (klip->li_labeled == B_TRUE) {
2712*a29e56d9SToomas Soome if ((error = ddi_prop_create(DDI_DEV_T_NONE, child,
2713*a29e56d9SToomas Soome DDI_PROP_CANSLEEP, "labeled", 0, 0))
2714*a29e56d9SToomas Soome != DDI_PROP_SUCCESS)
2715*a29e56d9SToomas Soome goto err;
2716*a29e56d9SToomas Soome }
2717*a29e56d9SToomas Soome
2718*a29e56d9SToomas Soome if ((error = ndi_devi_online(child, NDI_ONLINE_ATTACH))
2719*a29e56d9SToomas Soome != NDI_SUCCESS)
2720*a29e56d9SToomas Soome goto err;
2721*a29e56d9SToomas Soome } else {
2722*a29e56d9SToomas Soome id_free(lofi_id, klip->li_id);
2723*a29e56d9SToomas Soome error = EEXIST;
2724*a29e56d9SToomas Soome return (error);
2725*a29e56d9SToomas Soome }
2726*a29e56d9SToomas Soome
2727*a29e56d9SToomas Soome goto done;
2728*a29e56d9SToomas Soome
2729*a29e56d9SToomas Soome err:
2730*a29e56d9SToomas Soome ddi_prop_remove_all(child);
2731*a29e56d9SToomas Soome (void) ndi_devi_offline(child, NDI_DEVI_REMOVE);
2732*a29e56d9SToomas Soome id_free(lofi_id, klip->li_id);
2733*a29e56d9SToomas Soome done:
2734*a29e56d9SToomas Soome
2735*a29e56d9SToomas Soome return (error);
2736*a29e56d9SToomas Soome }
2737*a29e56d9SToomas Soome
2738*a29e56d9SToomas Soome static void
lofi_create_inquiry(struct lofi_state * lsp,struct scsi_inquiry * inq)2739*a29e56d9SToomas Soome lofi_create_inquiry(struct lofi_state *lsp, struct scsi_inquiry *inq)
2740*a29e56d9SToomas Soome {
2741*a29e56d9SToomas Soome char *p = NULL;
2742*a29e56d9SToomas Soome
2743*a29e56d9SToomas Soome (void) strlcpy(inq->inq_vid, LOFI_DRIVER_NAME, sizeof (inq->inq_vid));
2744*a29e56d9SToomas Soome
2745*a29e56d9SToomas Soome mutex_enter(&lsp->ls_vp_lock);
2746*a29e56d9SToomas Soome if (lsp->ls_vp != NULL)
2747*a29e56d9SToomas Soome p = strrchr(lsp->ls_vp->v_path, '/');
2748*a29e56d9SToomas Soome if (p != NULL)
2749*a29e56d9SToomas Soome (void) strncpy(inq->inq_pid, p + 1, sizeof (inq->inq_pid));
2750*a29e56d9SToomas Soome mutex_exit(&lsp->ls_vp_lock);
2751*a29e56d9SToomas Soome (void) strlcpy(inq->inq_revision, "1.0", sizeof (inq->inq_revision));
2752*a29e56d9SToomas Soome }
2753*a29e56d9SToomas Soome
2754*a29e56d9SToomas Soome /*
2755*a29e56d9SToomas Soome * copy devlink name from event cache
2756*a29e56d9SToomas Soome */
2757*a29e56d9SToomas Soome static void
lofi_copy_devpath(struct lofi_ioctl * klip)2758*a29e56d9SToomas Soome lofi_copy_devpath(struct lofi_ioctl *klip)
2759*a29e56d9SToomas Soome {
2760*a29e56d9SToomas Soome int error;
2761*a29e56d9SToomas Soome char namebuf[MAXNAMELEN], *str;
2762*a29e56d9SToomas Soome clock_t ticks;
2763*a29e56d9SToomas Soome nvlist_t *nvl;
2764*a29e56d9SToomas Soome
2765*a29e56d9SToomas Soome if (klip->li_labeled == B_TRUE)
2766*a29e56d9SToomas Soome klip->li_devpath[0] = '\0';
2767*a29e56d9SToomas Soome else {
2768*a29e56d9SToomas Soome /* no need to wait for messages */
2769*a29e56d9SToomas Soome (void) snprintf(klip->li_devpath, sizeof (klip->li_devpath),
2770*a29e56d9SToomas Soome "/dev/" LOFI_CHAR_NAME "/%d", klip->li_id);
2771*a29e56d9SToomas Soome return;
2772*a29e56d9SToomas Soome }
2773*a29e56d9SToomas Soome
2774*a29e56d9SToomas Soome (void) snprintf(namebuf, sizeof (namebuf), "%d", klip->li_id);
2775*a29e56d9SToomas Soome ticks = ddi_get_lbolt() + LOFI_TIMEOUT * drv_usectohz(1000000);
2776*a29e56d9SToomas Soome
2777*a29e56d9SToomas Soome nvl = NULL;
2778*a29e56d9SToomas Soome
2779*a29e56d9SToomas Soome mutex_enter(&lofi_chan_lock);
2780*a29e56d9SToomas Soome while (nvlist_lookup_nvlist(lofi_devlink_cache, namebuf, &nvl) != 0) {
2781*a29e56d9SToomas Soome error = cv_timedwait(&lofi_chan_cv, &lofi_chan_lock, ticks);
2782*a29e56d9SToomas Soome if (error == -1)
2783*a29e56d9SToomas Soome break;
2784*a29e56d9SToomas Soome }
2785*a29e56d9SToomas Soome
2786*a29e56d9SToomas Soome if (nvl != NULL) {
2787*a29e56d9SToomas Soome if (nvlist_lookup_string(nvl, DEV_NAME, &str) == 0) {
2788*a29e56d9SToomas Soome (void) strlcpy(klip->li_devpath, str,
2789*a29e56d9SToomas Soome sizeof (klip->li_devpath));
2790*a29e56d9SToomas Soome }
2791*a29e56d9SToomas Soome }
2792*a29e56d9SToomas Soome mutex_exit(&lofi_chan_lock);
2793*a29e56d9SToomas Soome }
2794*a29e56d9SToomas Soome
2795*a29e56d9SToomas Soome /*
27960fbb751dSJohn Levon * map a file to a minor number. Return the minor number.
27970fbb751dSJohn Levon */
27980fbb751dSJohn Levon static int
lofi_map_file(dev_t dev,struct lofi_ioctl * ulip,int pickminor,int * rvalp,struct cred * credp,int ioctl_flag)27990fbb751dSJohn Levon lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
28000fbb751dSJohn Levon int *rvalp, struct cred *credp, int ioctl_flag)
28010fbb751dSJohn Levon {
2802*a29e56d9SToomas Soome int id = -1;
28030fbb751dSJohn Levon struct lofi_state *lsp = NULL;
28040fbb751dSJohn Levon struct lofi_ioctl *klip;
28050fbb751dSJohn Levon int error;
28060fbb751dSJohn Levon struct vnode *vp = NULL;
28070fbb751dSJohn Levon vattr_t vattr;
28080fbb751dSJohn Levon int flag;
2809*a29e56d9SToomas Soome char namebuf[MAXNAMELEN];
28100fbb751dSJohn Levon
28110fbb751dSJohn Levon error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
28120fbb751dSJohn Levon if (error != 0)
28130fbb751dSJohn Levon return (error);
28140fbb751dSJohn Levon
28150fbb751dSJohn Levon mutex_enter(&lofi_lock);
28160fbb751dSJohn Levon
2817cd69fabeSAlexander Eremin if (file_to_lofi_nocheck(klip->li_filename, klip->li_readonly,
2818cd69fabeSAlexander Eremin NULL) == 0) {
28190fbb751dSJohn Levon error = EBUSY;
28200fbb751dSJohn Levon goto err;
28210fbb751dSJohn Levon }
28220fbb751dSJohn Levon
28230fbb751dSJohn Levon flag = FREAD | FWRITE | FOFFMAX | FEXCL;
28240fbb751dSJohn Levon error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
28250fbb751dSJohn Levon if (error) {
28260fbb751dSJohn Levon /* try read-only */
28270fbb751dSJohn Levon flag &= ~FWRITE;
28280fbb751dSJohn Levon error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
28290fbb751dSJohn Levon &vp, 0, 0);
28300fbb751dSJohn Levon if (error)
28310fbb751dSJohn Levon goto err;
28320fbb751dSJohn Levon }
28330fbb751dSJohn Levon
28340fbb751dSJohn Levon if (!V_ISLOFIABLE(vp->v_type)) {
283587117650Saalok error = EINVAL;
28360fbb751dSJohn Levon goto err;
28370fbb751dSJohn Levon }
28380fbb751dSJohn Levon
28390fbb751dSJohn Levon vattr.va_mask = AT_SIZE;
28400fbb751dSJohn Levon error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
28410fbb751dSJohn Levon if (error)
28420fbb751dSJohn Levon goto err;
28430fbb751dSJohn Levon
28440fbb751dSJohn Levon /* the file needs to be a multiple of the block size */
28450fbb751dSJohn Levon if ((vattr.va_size % DEV_BSIZE) != 0) {
28460fbb751dSJohn Levon error = EINVAL;
28470fbb751dSJohn Levon goto err;
28480fbb751dSJohn Levon }
28490fbb751dSJohn Levon
2850*a29e56d9SToomas Soome if (pickminor) {
2851*a29e56d9SToomas Soome klip->li_id = (uint32_t)-1;
28520fbb751dSJohn Levon }
2853*a29e56d9SToomas Soome if ((error = lofi_create_dev(klip)) != 0)
2854*a29e56d9SToomas Soome goto err;
28550fbb751dSJohn Levon
2856*a29e56d9SToomas Soome id = klip->li_id;
2857*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, id);
2858*a29e56d9SToomas Soome if (lsp == NULL)
2859*a29e56d9SToomas Soome goto err;
28600fbb751dSJohn Levon
2861*a29e56d9SToomas Soome /*
2862*a29e56d9SToomas Soome * from this point lofi_destroy() is used to clean up on error
2863*a29e56d9SToomas Soome * make sure the basic data is set
2864*a29e56d9SToomas Soome */
2865*a29e56d9SToomas Soome lsp->ls_dev = makedevice(getmajor(dev), LOFI_ID2MINOR(id));
28660fbb751dSJohn Levon
28670fbb751dSJohn Levon list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
28680fbb751dSJohn Levon offsetof(struct lofi_comp_cache, lc_list));
28690fbb751dSJohn Levon
28700fbb751dSJohn Levon /*
28710fbb751dSJohn Levon * save open mode so file can be closed properly and vnode counts
28720fbb751dSJohn Levon * updated correctly.
28730fbb751dSJohn Levon */
28740fbb751dSJohn Levon lsp->ls_openflag = flag;
28750fbb751dSJohn Levon
28760fbb751dSJohn Levon lsp->ls_vp = vp;
28770fbb751dSJohn Levon lsp->ls_stacked_vp = vp;
2878*a29e56d9SToomas Soome
2879*a29e56d9SToomas Soome lsp->ls_vp_size = vattr.va_size;
2880*a29e56d9SToomas Soome lsp->ls_vp_comp_size = lsp->ls_vp_size;
2881*a29e56d9SToomas Soome
28820fbb751dSJohn Levon /*
28830fbb751dSJohn Levon * Try to handle stacked lofs vnodes.
28840fbb751dSJohn Levon */
28850fbb751dSJohn Levon if (vp->v_type == VREG) {
28860fbb751dSJohn Levon vnode_t *realvp;
28870fbb751dSJohn Levon
28880fbb751dSJohn Levon if (VOP_REALVP(vp, &realvp, NULL) == 0) {
28890fbb751dSJohn Levon /*
28900fbb751dSJohn Levon * We need to use the realvp for uniqueness
28910fbb751dSJohn Levon * checking, but keep the stacked vp for
28920fbb751dSJohn Levon * LOFI_GET_FILENAME display.
28930fbb751dSJohn Levon */
28940fbb751dSJohn Levon VN_HOLD(realvp);
28950fbb751dSJohn Levon lsp->ls_vp = realvp;
289687117650Saalok }
289787117650Saalok }
289887117650Saalok
2899*a29e56d9SToomas Soome lsp->ls_lbshift = highbit(DEV_BSIZE) - 1;
2900*a29e56d9SToomas Soome lsp->ls_pbshift = lsp->ls_lbshift;
29010fbb751dSJohn Levon
2902cd69fabeSAlexander Eremin lsp->ls_readonly = klip->li_readonly;
2903*a29e56d9SToomas Soome lsp->ls_uncomp_seg_sz = 0;
2904*a29e56d9SToomas Soome lsp->ls_comp_algorithm[0] = '\0';
2905*a29e56d9SToomas Soome lsp->ls_crypto_offset = 0;
2906*a29e56d9SToomas Soome
2907*a29e56d9SToomas Soome (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
2908*a29e56d9SToomas Soome LOFI_DRIVER_NAME, id);
2909*a29e56d9SToomas Soome lsp->ls_taskq = taskq_create_proc(namebuf, lofi_taskq_nthreads,
2910*a29e56d9SToomas Soome minclsyspri, 1, lofi_taskq_maxalloc, curzone->zone_zsched, 0);
2911cd69fabeSAlexander Eremin
29120fbb751dSJohn Levon if ((error = lofi_init_crypto(lsp, klip)) != 0)
29130fbb751dSJohn Levon goto err;
29140fbb751dSJohn Levon
29150fbb751dSJohn Levon if ((error = lofi_init_compress(lsp)) != 0)
29160fbb751dSJohn Levon goto err;
29170fbb751dSJohn Levon
29187c478bd9Sstevel@tonic-gate fake_disk_geometry(lsp);
29190fbb751dSJohn Levon
2920*a29e56d9SToomas Soome if ((ddi_prop_update_int64(lsp->ls_dev, lsp->ls_dip, SIZE_PROP_NAME,
29210fbb751dSJohn Levon lsp->ls_vp_size - lsp->ls_crypto_offset)) != DDI_PROP_SUCCESS) {
29220fbb751dSJohn Levon error = EINVAL;
2923*a29e56d9SToomas Soome goto err;
29240fbb751dSJohn Levon }
29250fbb751dSJohn Levon
2926*a29e56d9SToomas Soome if ((ddi_prop_update_int64(lsp->ls_dev, lsp->ls_dip, NBLOCKS_PROP_NAME,
29270fbb751dSJohn Levon (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE))
29280fbb751dSJohn Levon != DDI_PROP_SUCCESS) {
29290fbb751dSJohn Levon error = EINVAL;
2930*a29e56d9SToomas Soome goto err;
29310fbb751dSJohn Levon }
29320fbb751dSJohn Levon
2933*a29e56d9SToomas Soome list_insert_tail(&lofi_list, lsp);
2934*a29e56d9SToomas Soome /*
2935*a29e56d9SToomas Soome * Notify we are ready to rock.
2936*a29e56d9SToomas Soome */
2937*a29e56d9SToomas Soome mutex_enter(&lsp->ls_vp_lock);
2938*a29e56d9SToomas Soome lsp->ls_vp_ready = B_TRUE;
2939*a29e56d9SToomas Soome cv_broadcast(&lsp->ls_vp_cv);
2940*a29e56d9SToomas Soome mutex_exit(&lsp->ls_vp_lock);
29417c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
29420fbb751dSJohn Levon
2943*a29e56d9SToomas Soome lofi_copy_devpath(klip);
2944*a29e56d9SToomas Soome
29450fbb751dSJohn Levon if (rvalp)
2946*a29e56d9SToomas Soome *rvalp = id;
2947bd07e074Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
29487c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
29497c478bd9Sstevel@tonic-gate return (0);
29507c478bd9Sstevel@tonic-gate
29510fbb751dSJohn Levon err:
29520fbb751dSJohn Levon if (lsp != NULL) {
29530fbb751dSJohn Levon lofi_destroy(lsp, credp);
29540fbb751dSJohn Levon } else {
29550fbb751dSJohn Levon if (vp != NULL) {
2956*a29e56d9SToomas Soome (void) VOP_PUTPAGE(vp, 0, 0, B_INVAL, credp, NULL);
29577d82f0f8SDina K Nimeh (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
29587d82f0f8SDina K Nimeh VN_RELE(vp);
29597d82f0f8SDina K Nimeh }
29600fbb751dSJohn Levon }
29610fbb751dSJohn Levon
29627c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
29637c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
29647c478bd9Sstevel@tonic-gate return (error);
29657c478bd9Sstevel@tonic-gate }
29667c478bd9Sstevel@tonic-gate
29677c478bd9Sstevel@tonic-gate /*
29687c478bd9Sstevel@tonic-gate * unmap a file.
29697c478bd9Sstevel@tonic-gate */
29707c478bd9Sstevel@tonic-gate static int
lofi_unmap_file(struct lofi_ioctl * ulip,int byfilename,struct cred * credp,int ioctl_flag)297167323fc4SJohn Levon lofi_unmap_file(struct lofi_ioctl *ulip, int byfilename,
2972bd07e074Sheppo struct cred *credp, int ioctl_flag)
29737c478bd9Sstevel@tonic-gate {
29747c478bd9Sstevel@tonic-gate struct lofi_state *lsp;
29757c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip;
2976*a29e56d9SToomas Soome nvlist_t *nvl = NULL;
2977*a29e56d9SToomas Soome clock_t ticks;
2978*a29e56d9SToomas Soome char name[MAXNAMELEN];
29790fbb751dSJohn Levon int err;
29807c478bd9Sstevel@tonic-gate
29810fbb751dSJohn Levon err = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
29820fbb751dSJohn Levon if (err != 0)
29830fbb751dSJohn Levon return (err);
29847c478bd9Sstevel@tonic-gate
29857c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock);
29867c478bd9Sstevel@tonic-gate if (byfilename) {
2987cd69fabeSAlexander Eremin if ((err = file_to_lofi(klip->li_filename, klip->li_readonly,
2988cd69fabeSAlexander Eremin &lsp)) != 0) {
29890fbb751dSJohn Levon mutex_exit(&lofi_lock);
29900fbb751dSJohn Levon return (err);
29910fbb751dSJohn Levon }
2992*a29e56d9SToomas Soome } else if (klip->li_id == 0) {
29930fbb751dSJohn Levon mutex_exit(&lofi_lock);
29940fbb751dSJohn Levon free_lofi_ioctl(klip);
29950fbb751dSJohn Levon return (ENXIO);
29967c478bd9Sstevel@tonic-gate } else {
2997*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, klip->li_id);
29987c478bd9Sstevel@tonic-gate }
29990fbb751dSJohn Levon
30000fbb751dSJohn Levon if (lsp == NULL || lsp->ls_vp == NULL || lofi_access(lsp) != 0) {
30017c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
30027c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
30037c478bd9Sstevel@tonic-gate return (ENXIO);
30047c478bd9Sstevel@tonic-gate }
30050fbb751dSJohn Levon
3006*a29e56d9SToomas Soome klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
30073d7072f8Seschrock
30083d7072f8Seschrock /*
300993239addSjohnlev * If it's still held open, we'll do one of three things:
301093239addSjohnlev *
301193239addSjohnlev * If no flag is set, just return EBUSY.
301293239addSjohnlev *
301393239addSjohnlev * If the 'cleanup' flag is set, unmap and remove the device when
301493239addSjohnlev * the last user finishes.
301593239addSjohnlev *
301693239addSjohnlev * If the 'force' flag is set, then we forcibly close the underlying
301793239addSjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl
301893239addSjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the
301993239addSjohnlev * device will be cleaned up appropriately.
30203d7072f8Seschrock *
30213d7072f8Seschrock * This is complicated by the fact that we may have outstanding
302293239addSjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all
30237d82f0f8SDina K Nimeh * I/O, we keep a count of the number of outstanding I/O requests
30247d82f0f8SDina K Nimeh * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
30257d82f0f8SDina K Nimeh * should be dispatched (ls_vp_closereq).
30267d82f0f8SDina K Nimeh *
302793239addSjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0,
302893239addSjohnlev * and then close the underlying vnode.
30293d7072f8Seschrock */
303093239addSjohnlev if (is_opened(lsp)) {
30313d7072f8Seschrock if (klip->li_force) {
30323d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock);
30333d7072f8Seschrock lsp->ls_vp_closereq = B_TRUE;
3034b3388e4fSEric Taylor /* wake up any threads waiting on dkiocstate */
3035b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv);
30363d7072f8Seschrock while (lsp->ls_vp_iocount > 0)
30373d7072f8Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
30383d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
30397d82f0f8SDina K Nimeh
30400fbb751dSJohn Levon goto out;
304193239addSjohnlev } else if (klip->li_cleanup) {
304293239addSjohnlev lsp->ls_cleanup = 1;
304393239addSjohnlev mutex_exit(&lofi_lock);
304493239addSjohnlev free_lofi_ioctl(klip);
304593239addSjohnlev return (0);
30463d7072f8Seschrock }
304793239addSjohnlev
30487c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
30497c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
30507c478bd9Sstevel@tonic-gate return (EBUSY);
30517c478bd9Sstevel@tonic-gate }
30527c478bd9Sstevel@tonic-gate
30530fbb751dSJohn Levon out:
3054*a29e56d9SToomas Soome lofi_free_dev(lsp);
30550fbb751dSJohn Levon lofi_destroy(lsp, credp);
30567c478bd9Sstevel@tonic-gate
3057*a29e56d9SToomas Soome /*
3058*a29e56d9SToomas Soome * check the lofi_devlink_cache if device is really gone.
3059*a29e56d9SToomas Soome * note: we just wait for timeout here and dont give error if
3060*a29e56d9SToomas Soome * timer will expire. This check is to try to ensure the unmap is
3061*a29e56d9SToomas Soome * really done when lofiadm -d completes.
3062*a29e56d9SToomas Soome * Since lofi_lock is held, also hopefully the lofiadm -a calls
3063*a29e56d9SToomas Soome * wont interfere the the unmap.
3064*a29e56d9SToomas Soome */
3065*a29e56d9SToomas Soome (void) snprintf(name, sizeof (name), "%d", klip->li_id);
3066*a29e56d9SToomas Soome ticks = ddi_get_lbolt() + LOFI_TIMEOUT * drv_usectohz(1000000);
3067*a29e56d9SToomas Soome mutex_enter(&lofi_chan_lock);
3068*a29e56d9SToomas Soome while (nvlist_lookup_nvlist(lofi_devlink_cache, name, &nvl) == 0) {
3069*a29e56d9SToomas Soome err = cv_timedwait(&lofi_chan_cv, &lofi_chan_lock, ticks);
3070*a29e56d9SToomas Soome if (err == -1)
3071*a29e56d9SToomas Soome break;
3072*a29e56d9SToomas Soome }
3073*a29e56d9SToomas Soome mutex_exit(&lofi_chan_lock);
3074*a29e56d9SToomas Soome
30757c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
3076bd07e074Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
30777c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
30787c478bd9Sstevel@tonic-gate return (0);
30797c478bd9Sstevel@tonic-gate }
30807c478bd9Sstevel@tonic-gate
30817c478bd9Sstevel@tonic-gate /*
30827c478bd9Sstevel@tonic-gate * get the filename given the minor number, or the minor number given
30837c478bd9Sstevel@tonic-gate * the name.
30847c478bd9Sstevel@tonic-gate */
30853d7072f8Seschrock /*ARGSUSED*/
30867c478bd9Sstevel@tonic-gate static int
lofi_get_info(dev_t dev,struct lofi_ioctl * ulip,int which,struct cred * credp,int ioctl_flag)30877c478bd9Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
3088bd07e074Sheppo struct cred *credp, int ioctl_flag)
30897c478bd9Sstevel@tonic-gate {
30907c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip;
30910fbb751dSJohn Levon struct lofi_state *lsp;
30927c478bd9Sstevel@tonic-gate int error;
30937c478bd9Sstevel@tonic-gate
30940fbb751dSJohn Levon error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
30950fbb751dSJohn Levon if (error != 0)
30960fbb751dSJohn Levon return (error);
30977c478bd9Sstevel@tonic-gate
30987c478bd9Sstevel@tonic-gate switch (which) {
30997c478bd9Sstevel@tonic-gate case LOFI_GET_FILENAME:
3100*a29e56d9SToomas Soome if (klip->li_id == 0) {
31017c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
31027c478bd9Sstevel@tonic-gate return (EINVAL);
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate
31057c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock);
3106*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, klip->li_id);
31070fbb751dSJohn Levon if (lsp == NULL || lofi_access(lsp) != 0) {
31087c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
31097c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
31107c478bd9Sstevel@tonic-gate return (ENXIO);
31117c478bd9Sstevel@tonic-gate }
31120fbb751dSJohn Levon
31130fbb751dSJohn Levon /*
31140fbb751dSJohn Levon * This may fail if, for example, we're trying to look
31150fbb751dSJohn Levon * up a zoned NFS path from the global zone.
31160fbb751dSJohn Levon */
31170fbb751dSJohn Levon if (vnodetopath(NULL, lsp->ls_stacked_vp, klip->li_filename,
31180fbb751dSJohn Levon sizeof (klip->li_filename), CRED()) != 0) {
31190fbb751dSJohn Levon (void) strlcpy(klip->li_filename, "?",
31200fbb751dSJohn Levon sizeof (klip->li_filename));
31210fbb751dSJohn Levon }
31220fbb751dSJohn Levon
3123cd69fabeSAlexander Eremin klip->li_readonly = lsp->ls_readonly;
3124*a29e56d9SToomas Soome klip->li_labeled = lsp->ls_cmlbhandle != NULL;
3125cd69fabeSAlexander Eremin
312687117650Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
312787117650Saalok sizeof (klip->li_algorithm));
31287d82f0f8SDina K Nimeh klip->li_crypto_enabled = lsp->ls_crypto_enabled;
31297c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
3130*a29e56d9SToomas Soome
3131*a29e56d9SToomas Soome lofi_copy_devpath(klip);
3132bd07e074Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
31337c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
31347c478bd9Sstevel@tonic-gate return (error);
31357c478bd9Sstevel@tonic-gate case LOFI_GET_MINOR:
31367c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock);
3137cd69fabeSAlexander Eremin error = file_to_lofi(klip->li_filename,
3138cd69fabeSAlexander Eremin klip->li_readonly, &lsp);
3139*a29e56d9SToomas Soome if (error != 0) {
3140*a29e56d9SToomas Soome mutex_exit(&lofi_lock);
3141*a29e56d9SToomas Soome free_lofi_ioctl(klip);
3142*a29e56d9SToomas Soome return (error);
3143*a29e56d9SToomas Soome }
3144*a29e56d9SToomas Soome klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
3145*a29e56d9SToomas Soome
3146*a29e56d9SToomas Soome klip->li_readonly = lsp->ls_readonly;
3147*a29e56d9SToomas Soome klip->li_labeled = lsp->ls_cmlbhandle != NULL;
31487c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock);
31490fbb751dSJohn Levon
3150*a29e56d9SToomas Soome lofi_copy_devpath(klip);
3151bd07e074Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
31520fbb751dSJohn Levon
31537c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
31547c478bd9Sstevel@tonic-gate return (error);
315587117650Saalok case LOFI_CHECK_COMPRESSED:
315687117650Saalok mutex_enter(&lofi_lock);
3157cd69fabeSAlexander Eremin error = file_to_lofi(klip->li_filename,
3158cd69fabeSAlexander Eremin klip->li_readonly, &lsp);
31590fbb751dSJohn Levon if (error != 0) {
316087117650Saalok mutex_exit(&lofi_lock);
316187117650Saalok free_lofi_ioctl(klip);
31620fbb751dSJohn Levon return (error);
316387117650Saalok }
316487117650Saalok
3165*a29e56d9SToomas Soome klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
316687117650Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
316787117650Saalok sizeof (klip->li_algorithm));
31680fbb751dSJohn Levon
316987117650Saalok mutex_exit(&lofi_lock);
317087117650Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
317187117650Saalok free_lofi_ioctl(klip);
317287117650Saalok return (error);
31737c478bd9Sstevel@tonic-gate default:
31747c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip);
31757c478bd9Sstevel@tonic-gate return (EINVAL);
31767c478bd9Sstevel@tonic-gate }
31777c478bd9Sstevel@tonic-gate }
31787c478bd9Sstevel@tonic-gate
31797c478bd9Sstevel@tonic-gate static int
uscsi_is_inquiry(intptr_t arg,int flag,union scsi_cdb * cdb,struct uscsi_cmd * uscmd)3180*a29e56d9SToomas Soome uscsi_is_inquiry(intptr_t arg, int flag, union scsi_cdb *cdb,
3181*a29e56d9SToomas Soome struct uscsi_cmd *uscmd)
3182*a29e56d9SToomas Soome {
3183*a29e56d9SToomas Soome int rval;
3184*a29e56d9SToomas Soome
3185*a29e56d9SToomas Soome #ifdef _MULTI_DATAMODEL
3186*a29e56d9SToomas Soome switch (ddi_model_convert_from(flag & FMODELS)) {
3187*a29e56d9SToomas Soome case DDI_MODEL_ILP32: {
3188*a29e56d9SToomas Soome struct uscsi_cmd32 ucmd32;
3189*a29e56d9SToomas Soome
3190*a29e56d9SToomas Soome if (ddi_copyin((void *)arg, &ucmd32, sizeof (ucmd32), flag)) {
3191*a29e56d9SToomas Soome rval = EFAULT;
3192*a29e56d9SToomas Soome goto err;
3193*a29e56d9SToomas Soome }
3194*a29e56d9SToomas Soome uscsi_cmd32touscsi_cmd((&ucmd32), uscmd);
3195*a29e56d9SToomas Soome break;
3196*a29e56d9SToomas Soome }
3197*a29e56d9SToomas Soome case DDI_MODEL_NONE:
3198*a29e56d9SToomas Soome if (ddi_copyin((void *)arg, uscmd, sizeof (*uscmd), flag)) {
3199*a29e56d9SToomas Soome rval = EFAULT;
3200*a29e56d9SToomas Soome goto err;
3201*a29e56d9SToomas Soome }
3202*a29e56d9SToomas Soome break;
3203*a29e56d9SToomas Soome default:
3204*a29e56d9SToomas Soome rval = EFAULT;
3205*a29e56d9SToomas Soome goto err;
3206*a29e56d9SToomas Soome }
3207*a29e56d9SToomas Soome #else
3208*a29e56d9SToomas Soome if (ddi_copyin((void *)arg, uscmd, sizeof (*uscmd), flag)) {
3209*a29e56d9SToomas Soome rval = EFAULT;
3210*a29e56d9SToomas Soome goto err;
3211*a29e56d9SToomas Soome }
3212*a29e56d9SToomas Soome #endif /* _MULTI_DATAMODEL */
3213*a29e56d9SToomas Soome if (ddi_copyin(uscmd->uscsi_cdb, cdb, uscmd->uscsi_cdblen, flag)) {
3214*a29e56d9SToomas Soome rval = EFAULT;
3215*a29e56d9SToomas Soome goto err;
3216*a29e56d9SToomas Soome }
3217*a29e56d9SToomas Soome if (cdb->scc_cmd == SCMD_INQUIRY) {
3218*a29e56d9SToomas Soome return (0);
3219*a29e56d9SToomas Soome }
3220*a29e56d9SToomas Soome err:
3221*a29e56d9SToomas Soome return (rval);
3222*a29e56d9SToomas Soome }
3223*a29e56d9SToomas Soome
3224*a29e56d9SToomas Soome static int
lofi_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * credp,int * rvalp)32257c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
32267c478bd9Sstevel@tonic-gate int *rvalp)
32277c478bd9Sstevel@tonic-gate {
32287c478bd9Sstevel@tonic-gate int error;
32297c478bd9Sstevel@tonic-gate enum dkio_state dkstate;
32307c478bd9Sstevel@tonic-gate struct lofi_state *lsp;
3231*a29e56d9SToomas Soome int id;
32327c478bd9Sstevel@tonic-gate
3233*a29e56d9SToomas Soome id = LOFI_MINOR2ID(getminor(dev));
3234*a29e56d9SToomas Soome
32357c478bd9Sstevel@tonic-gate /* lofi ioctls only apply to the master device */
3236*a29e56d9SToomas Soome if (id == 0) {
32377c478bd9Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
32387c478bd9Sstevel@tonic-gate
32397c478bd9Sstevel@tonic-gate /*
32407c478bd9Sstevel@tonic-gate * the query command only need read-access - i.e., normal
32417c478bd9Sstevel@tonic-gate * users are allowed to do those on the ctl device as
32427c478bd9Sstevel@tonic-gate * long as they can open it read-only.
32437c478bd9Sstevel@tonic-gate */
32447c478bd9Sstevel@tonic-gate switch (cmd) {
32457c478bd9Sstevel@tonic-gate case LOFI_MAP_FILE:
32467c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0)
32477c478bd9Sstevel@tonic-gate return (EPERM);
3248bd07e074Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
32497c478bd9Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR:
32507c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0)
32517c478bd9Sstevel@tonic-gate return (EPERM);
3252bd07e074Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
32537c478bd9Sstevel@tonic-gate case LOFI_UNMAP_FILE:
32547c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0)
32557c478bd9Sstevel@tonic-gate return (EPERM);
325667323fc4SJohn Levon return (lofi_unmap_file(lip, 1, credp, flag));
32577c478bd9Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR:
32587c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0)
32597c478bd9Sstevel@tonic-gate return (EPERM);
326067323fc4SJohn Levon return (lofi_unmap_file(lip, 0, credp, flag));
32617c478bd9Sstevel@tonic-gate case LOFI_GET_FILENAME:
32627c478bd9Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
3263bd07e074Sheppo credp, flag));
32647c478bd9Sstevel@tonic-gate case LOFI_GET_MINOR:
32657c478bd9Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
3266bd07e074Sheppo credp, flag));
32670fbb751dSJohn Levon
32680fbb751dSJohn Levon /*
32690fbb751dSJohn Levon * This API made limited sense when this value was fixed
32700fbb751dSJohn Levon * at LOFI_MAX_FILES. However, its use to iterate
32710fbb751dSJohn Levon * across all possible devices in lofiadm means we don't
3272*a29e56d9SToomas Soome * want to return L_MAXMIN, but the highest
3273*a29e56d9SToomas Soome * *allocated* id.
32740fbb751dSJohn Levon */
32757c478bd9Sstevel@tonic-gate case LOFI_GET_MAXMINOR:
3276*a29e56d9SToomas Soome id = 0;
32770fbb751dSJohn Levon
32780fbb751dSJohn Levon mutex_enter(&lofi_lock);
32790fbb751dSJohn Levon
32800fbb751dSJohn Levon for (lsp = list_head(&lofi_list); lsp != NULL;
32810fbb751dSJohn Levon lsp = list_next(&lofi_list, lsp)) {
3282*a29e56d9SToomas Soome int i;
32830fbb751dSJohn Levon if (lofi_access(lsp) != 0)
32840fbb751dSJohn Levon continue;
32850fbb751dSJohn Levon
3286*a29e56d9SToomas Soome i = ddi_get_instance(lsp->ls_dip);
3287*a29e56d9SToomas Soome if (i > id)
3288*a29e56d9SToomas Soome id = i;
32890fbb751dSJohn Levon }
32900fbb751dSJohn Levon
32910fbb751dSJohn Levon mutex_exit(&lofi_lock);
32920fbb751dSJohn Levon
3293*a29e56d9SToomas Soome error = ddi_copyout(&id, &lip->li_id,
3294*a29e56d9SToomas Soome sizeof (id), flag);
32957c478bd9Sstevel@tonic-gate if (error)
32967c478bd9Sstevel@tonic-gate return (EFAULT);
32977c478bd9Sstevel@tonic-gate return (0);
32980fbb751dSJohn Levon
329987117650Saalok case LOFI_CHECK_COMPRESSED:
330087117650Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
330187117650Saalok credp, flag));
33027c478bd9Sstevel@tonic-gate default:
33030fbb751dSJohn Levon return (EINVAL);
33047c478bd9Sstevel@tonic-gate }
33057c478bd9Sstevel@tonic-gate }
33067c478bd9Sstevel@tonic-gate
3307b3388e4fSEric Taylor mutex_enter(&lofi_lock);
3308*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, id);
3309b3388e4fSEric Taylor if (lsp == NULL || lsp->ls_vp_closereq) {
3310b3388e4fSEric Taylor mutex_exit(&lofi_lock);
33117c478bd9Sstevel@tonic-gate return (ENXIO);
3312b3388e4fSEric Taylor }
3313b3388e4fSEric Taylor mutex_exit(&lofi_lock);
33147c478bd9Sstevel@tonic-gate
3315*a29e56d9SToomas Soome if (ddi_prop_exists(DDI_DEV_T_ANY, lsp->ls_dip, DDI_PROP_DONTPASS,
3316*a29e56d9SToomas Soome "labeled") == 1) {
3317*a29e56d9SToomas Soome error = cmlb_ioctl(lsp->ls_cmlbhandle, dev, cmd, arg, flag,
3318*a29e56d9SToomas Soome credp, rvalp, 0);
3319*a29e56d9SToomas Soome if (error != ENOTTY)
3320*a29e56d9SToomas Soome return (error);
3321*a29e56d9SToomas Soome }
3322*a29e56d9SToomas Soome
33233d7072f8Seschrock /*
33243d7072f8Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
33253d7072f8Seschrock * EIO as if the device was no longer present.
33263d7072f8Seschrock */
33273d7072f8Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
33283d7072f8Seschrock return (EIO);
33293d7072f8Seschrock
33307c478bd9Sstevel@tonic-gate /* these are for faking out utilities like newfs */
33317c478bd9Sstevel@tonic-gate switch (cmd) {
3332*a29e56d9SToomas Soome case DKIOCGMEDIAINFO:
3333*a29e56d9SToomas Soome case DKIOCGMEDIAINFOEXT: {
3334*a29e56d9SToomas Soome struct dk_minfo_ext media_info;
3335*a29e56d9SToomas Soome int shift = lsp->ls_lbshift;
3336*a29e56d9SToomas Soome int size;
3337*a29e56d9SToomas Soome
3338*a29e56d9SToomas Soome if (cmd == DKIOCGMEDIAINFOEXT) {
3339*a29e56d9SToomas Soome media_info.dki_pbsize = 1U << lsp->ls_pbshift;
3340*a29e56d9SToomas Soome size = sizeof (struct dk_minfo_ext);
3341*a29e56d9SToomas Soome } else {
3342*a29e56d9SToomas Soome size = sizeof (struct dk_minfo);
3343*a29e56d9SToomas Soome }
3344*a29e56d9SToomas Soome
3345*a29e56d9SToomas Soome media_info.dki_media_type = DK_FIXED_DISK;
3346*a29e56d9SToomas Soome media_info.dki_lbsize = 1U << shift;
3347*a29e56d9SToomas Soome media_info.dki_capacity =
3348*a29e56d9SToomas Soome (lsp->ls_vp_size - lsp->ls_crypto_offset) >> shift;
3349*a29e56d9SToomas Soome
3350*a29e56d9SToomas Soome if (ddi_copyout(&media_info, (void *)arg, size, flag))
3351*a29e56d9SToomas Soome return (EFAULT);
3352*a29e56d9SToomas Soome return (0);
3353*a29e56d9SToomas Soome }
3354*a29e56d9SToomas Soome case DKIOCREMOVABLE: {
3355*a29e56d9SToomas Soome int i = 0;
3356*a29e56d9SToomas Soome if (ddi_copyout(&i, (caddr_t)arg, sizeof (int), flag))
3357*a29e56d9SToomas Soome return (EFAULT);
3358*a29e56d9SToomas Soome return (0);
3359*a29e56d9SToomas Soome }
3360*a29e56d9SToomas Soome
3361*a29e56d9SToomas Soome case DKIOCGVTOC: {
3362*a29e56d9SToomas Soome struct vtoc vt;
3363*a29e56d9SToomas Soome fake_disk_vtoc(lsp, &vt);
3364*a29e56d9SToomas Soome
33657c478bd9Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) {
33667c478bd9Sstevel@tonic-gate case DDI_MODEL_ILP32: {
33677c478bd9Sstevel@tonic-gate struct vtoc32 vtoc32;
33687c478bd9Sstevel@tonic-gate
3369*a29e56d9SToomas Soome vtoctovtoc32(vt, vtoc32);
33707c478bd9Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg,
33717c478bd9Sstevel@tonic-gate sizeof (struct vtoc32), flag))
33727c478bd9Sstevel@tonic-gate return (EFAULT);
33737c478bd9Sstevel@tonic-gate break;
33747c478bd9Sstevel@tonic-gate }
33757c478bd9Sstevel@tonic-gate
33767c478bd9Sstevel@tonic-gate case DDI_MODEL_NONE:
3377*a29e56d9SToomas Soome if (ddi_copyout(&vt, (void *)arg,
33787c478bd9Sstevel@tonic-gate sizeof (struct vtoc), flag))
33797c478bd9Sstevel@tonic-gate return (EFAULT);
33807c478bd9Sstevel@tonic-gate break;
33817c478bd9Sstevel@tonic-gate }
33827c478bd9Sstevel@tonic-gate return (0);
3383*a29e56d9SToomas Soome }
3384*a29e56d9SToomas Soome case DKIOCINFO: {
3385*a29e56d9SToomas Soome struct dk_cinfo ci;
3386*a29e56d9SToomas Soome fake_disk_info(dev, &ci);
3387*a29e56d9SToomas Soome if (ddi_copyout(&ci, (void *)arg, sizeof (ci), flag))
33887c478bd9Sstevel@tonic-gate return (EFAULT);
33897c478bd9Sstevel@tonic-gate return (0);
3390*a29e56d9SToomas Soome }
33917c478bd9Sstevel@tonic-gate case DKIOCG_VIRTGEOM:
33927c478bd9Sstevel@tonic-gate case DKIOCG_PHYGEOM:
33937c478bd9Sstevel@tonic-gate case DKIOCGGEOM:
3394bd07e074Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
3395bd07e074Sheppo sizeof (struct dk_geom), flag);
33967c478bd9Sstevel@tonic-gate if (error)
33977c478bd9Sstevel@tonic-gate return (EFAULT);
33987c478bd9Sstevel@tonic-gate return (0);
33997c478bd9Sstevel@tonic-gate case DKIOCSTATE:
34003d7072f8Seschrock /*
34013d7072f8Seschrock * Normally, lofi devices are always in the INSERTED state. If
34023d7072f8Seschrock * a device is forcefully unmapped, then the device transitions
34033d7072f8Seschrock * to the DKIO_DEV_GONE state.
34043d7072f8Seschrock */
34053d7072f8Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
34063d7072f8Seschrock flag) != 0)
34073d7072f8Seschrock return (EFAULT);
34083d7072f8Seschrock
34093d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock);
3410b3388e4fSEric Taylor lsp->ls_vp_iocount++;
3411b3388e4fSEric Taylor while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
3412b3388e4fSEric Taylor (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
3413b3388e4fSEric Taylor !lsp->ls_vp_closereq) {
34143d7072f8Seschrock /*
34153d7072f8Seschrock * By virtue of having the device open, we know that
34163d7072f8Seschrock * 'lsp' will remain valid when we return.
34173d7072f8Seschrock */
34183d7072f8Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv,
34193d7072f8Seschrock &lsp->ls_vp_lock)) {
3420b3388e4fSEric Taylor lsp->ls_vp_iocount--;
3421b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv);
34223d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
34233d7072f8Seschrock return (EINTR);
34243d7072f8Seschrock }
34253d7072f8Seschrock }
34263d7072f8Seschrock
3427b3388e4fSEric Taylor dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ?
3428b3388e4fSEric Taylor DKIO_INSERTED : DKIO_DEV_GONE);
3429b3388e4fSEric Taylor lsp->ls_vp_iocount--;
3430b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv);
34313d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock);
34323d7072f8Seschrock
34333d7072f8Seschrock if (ddi_copyout(&dkstate, (void *)arg,
34343d7072f8Seschrock sizeof (dkstate), flag) != 0)
34357c478bd9Sstevel@tonic-gate return (EFAULT);
34367c478bd9Sstevel@tonic-gate return (0);
3437*a29e56d9SToomas Soome case USCSICMD: {
3438*a29e56d9SToomas Soome struct uscsi_cmd uscmd;
3439*a29e56d9SToomas Soome union scsi_cdb cdb;
3440*a29e56d9SToomas Soome
3441*a29e56d9SToomas Soome if (uscsi_is_inquiry(arg, flag, &cdb, &uscmd) == 0) {
3442*a29e56d9SToomas Soome struct scsi_inquiry inq = {0};
3443*a29e56d9SToomas Soome
3444*a29e56d9SToomas Soome lofi_create_inquiry(lsp, &inq);
3445*a29e56d9SToomas Soome if (ddi_copyout(&inq, uscmd.uscsi_bufaddr,
3446*a29e56d9SToomas Soome uscmd.uscsi_buflen, flag) != 0)
3447*a29e56d9SToomas Soome return (EFAULT);
3448*a29e56d9SToomas Soome return (0);
3449*a29e56d9SToomas Soome } else if (cdb.scc_cmd == SCMD_READ_CAPACITY) {
3450*a29e56d9SToomas Soome struct scsi_capacity capacity;
3451*a29e56d9SToomas Soome
3452*a29e56d9SToomas Soome capacity.capacity =
3453*a29e56d9SToomas Soome BE_32((lsp->ls_vp_size - lsp->ls_crypto_offset) >>
3454*a29e56d9SToomas Soome lsp->ls_lbshift);
3455*a29e56d9SToomas Soome capacity.lbasize = BE_32(1 << lsp->ls_lbshift);
3456*a29e56d9SToomas Soome if (ddi_copyout(&capacity, uscmd.uscsi_bufaddr,
3457*a29e56d9SToomas Soome uscmd.uscsi_buflen, flag) != 0)
3458*a29e56d9SToomas Soome return (EFAULT);
3459*a29e56d9SToomas Soome return (0);
3460*a29e56d9SToomas Soome }
3461*a29e56d9SToomas Soome
3462*a29e56d9SToomas Soome uscmd.uscsi_rqstatus = 0xff;
3463*a29e56d9SToomas Soome #ifdef _MULTI_DATAMODEL
3464*a29e56d9SToomas Soome switch (ddi_model_convert_from(flag & FMODELS)) {
3465*a29e56d9SToomas Soome case DDI_MODEL_ILP32: {
3466*a29e56d9SToomas Soome struct uscsi_cmd32 ucmd32;
3467*a29e56d9SToomas Soome uscsi_cmdtouscsi_cmd32((&uscmd), (&ucmd32));
3468*a29e56d9SToomas Soome if (ddi_copyout(&ucmd32, (void *)arg, sizeof (ucmd32),
3469*a29e56d9SToomas Soome flag) != 0)
3470*a29e56d9SToomas Soome return (EFAULT);
3471*a29e56d9SToomas Soome break;
3472*a29e56d9SToomas Soome }
3473*a29e56d9SToomas Soome case DDI_MODEL_NONE:
3474*a29e56d9SToomas Soome if (ddi_copyout(&uscmd, (void *)arg, sizeof (uscmd),
3475*a29e56d9SToomas Soome flag) != 0)
3476*a29e56d9SToomas Soome return (EFAULT);
3477*a29e56d9SToomas Soome break;
34787c478bd9Sstevel@tonic-gate default:
3479*a29e56d9SToomas Soome return (EFAULT);
3480*a29e56d9SToomas Soome }
3481*a29e56d9SToomas Soome #else
3482*a29e56d9SToomas Soome if (ddi_copyout(&uscmd, (void *)arg, sizeof (uscmd), flag) != 0)
3483*a29e56d9SToomas Soome return (EFAULT);
3484*a29e56d9SToomas Soome #endif /* _MULTI_DATAMODEL */
3485*a29e56d9SToomas Soome return (0);
3486*a29e56d9SToomas Soome }
3487*a29e56d9SToomas Soome default:
3488*a29e56d9SToomas Soome #ifdef DEBUG
3489*a29e56d9SToomas Soome cmn_err(CE_WARN, "lofi_ioctl: %d is not implemented\n", cmd);
3490*a29e56d9SToomas Soome #endif /* DEBUG */
34917c478bd9Sstevel@tonic-gate return (ENOTTY);
34927c478bd9Sstevel@tonic-gate }
34937c478bd9Sstevel@tonic-gate }
34947c478bd9Sstevel@tonic-gate
3495*a29e56d9SToomas 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)3496*a29e56d9SToomas Soome lofi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
3497*a29e56d9SToomas Soome char *name, caddr_t valuep, int *lengthp)
3498*a29e56d9SToomas Soome {
3499*a29e56d9SToomas Soome struct lofi_state *lsp;
3500*a29e56d9SToomas Soome
3501*a29e56d9SToomas Soome lsp = ddi_get_soft_state(lofi_statep, ddi_get_instance(dip));
3502*a29e56d9SToomas Soome if (lsp == NULL) {
3503*a29e56d9SToomas Soome return (ddi_prop_op(dev, dip, prop_op, mod_flags,
3504*a29e56d9SToomas Soome name, valuep, lengthp));
3505*a29e56d9SToomas Soome }
3506*a29e56d9SToomas Soome
3507*a29e56d9SToomas Soome return (cmlb_prop_op(lsp->ls_cmlbhandle, dev, dip, prop_op, mod_flags,
3508*a29e56d9SToomas Soome name, valuep, lengthp, LOFI_PART(getminor(dev)), NULL));
3509*a29e56d9SToomas Soome }
3510*a29e56d9SToomas Soome
35117c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
35127c478bd9Sstevel@tonic-gate lofi_open, /* open */
35137c478bd9Sstevel@tonic-gate lofi_close, /* close */
35147c478bd9Sstevel@tonic-gate lofi_strategy, /* strategy */
35157c478bd9Sstevel@tonic-gate nodev, /* print */
35167c478bd9Sstevel@tonic-gate nodev, /* dump */
35177c478bd9Sstevel@tonic-gate lofi_read, /* read */
35187c478bd9Sstevel@tonic-gate lofi_write, /* write */
35197c478bd9Sstevel@tonic-gate lofi_ioctl, /* ioctl */
35207c478bd9Sstevel@tonic-gate nodev, /* devmap */
35217c478bd9Sstevel@tonic-gate nodev, /* mmap */
35227c478bd9Sstevel@tonic-gate nodev, /* segmap */
35237c478bd9Sstevel@tonic-gate nochpoll, /* poll */
3524*a29e56d9SToomas Soome lofi_prop_op, /* prop_op */
35257c478bd9Sstevel@tonic-gate 0, /* streamtab */
35267c478bd9Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */
35277c478bd9Sstevel@tonic-gate CB_REV,
35287c478bd9Sstevel@tonic-gate lofi_aread,
35297c478bd9Sstevel@tonic-gate lofi_awrite
35307c478bd9Sstevel@tonic-gate };
35317c478bd9Sstevel@tonic-gate
35327c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = {
35337c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
35347c478bd9Sstevel@tonic-gate 0, /* refcnt */
35357c478bd9Sstevel@tonic-gate lofi_info, /* info */
35367c478bd9Sstevel@tonic-gate nulldev, /* identify */
35377c478bd9Sstevel@tonic-gate nulldev, /* probe */
35387c478bd9Sstevel@tonic-gate lofi_attach, /* attach */
35397c478bd9Sstevel@tonic-gate lofi_detach, /* detach */
35407c478bd9Sstevel@tonic-gate nodev, /* reset */
35417c478bd9Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */
354219397407SSherry Moore NULL, /* no bus operations */
354319397407SSherry Moore NULL, /* power */
354419397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
35457c478bd9Sstevel@tonic-gate };
35467c478bd9Sstevel@tonic-gate
35477c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
35487c478bd9Sstevel@tonic-gate &mod_driverops,
354919397407SSherry Moore "loopback file driver",
35507c478bd9Sstevel@tonic-gate &lofi_ops,
35517c478bd9Sstevel@tonic-gate };
35527c478bd9Sstevel@tonic-gate
35537c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
35547c478bd9Sstevel@tonic-gate MODREV_1,
35557c478bd9Sstevel@tonic-gate &modldrv,
35567c478bd9Sstevel@tonic-gate NULL
35577c478bd9Sstevel@tonic-gate };
35587c478bd9Sstevel@tonic-gate
35597c478bd9Sstevel@tonic-gate int
_init(void)35607c478bd9Sstevel@tonic-gate _init(void)
35617c478bd9Sstevel@tonic-gate {
35627c478bd9Sstevel@tonic-gate int error;
35637c478bd9Sstevel@tonic-gate
35640fbb751dSJohn Levon list_create(&lofi_list, sizeof (struct lofi_state),
35650fbb751dSJohn Levon offsetof(struct lofi_state, ls_list));
35660fbb751dSJohn Levon
3567*a29e56d9SToomas Soome error = ddi_soft_state_init((void **)&lofi_statep,
35687c478bd9Sstevel@tonic-gate sizeof (struct lofi_state), 0);
3569*a29e56d9SToomas Soome if (error) {
3570*a29e56d9SToomas Soome list_destroy(&lofi_list);
35717c478bd9Sstevel@tonic-gate return (error);
3572*a29e56d9SToomas Soome }
3573*a29e56d9SToomas Soome
3574*a29e56d9SToomas Soome /*
3575*a29e56d9SToomas Soome * The minor number is stored as id << LOFI_CMLB_SHIFT as
3576*a29e56d9SToomas Soome * we need to reserve space for cmlb minor numbers.
3577*a29e56d9SToomas Soome * This will leave out 4096 id values on 32bit kernel, which should
3578*a29e56d9SToomas Soome * still suffice.
3579*a29e56d9SToomas Soome */
3580*a29e56d9SToomas Soome lofi_id = id_space_create("lofi_id", 1,
3581*a29e56d9SToomas Soome (1 << (L_BITSMINOR - LOFI_CMLB_SHIFT)));
3582*a29e56d9SToomas Soome
3583*a29e56d9SToomas Soome if (lofi_id == NULL) {
3584*a29e56d9SToomas Soome ddi_soft_state_fini((void **)&lofi_statep);
3585*a29e56d9SToomas Soome list_destroy(&lofi_list);
3586*a29e56d9SToomas Soome return (DDI_FAILURE);
3587*a29e56d9SToomas Soome }
35887c478bd9Sstevel@tonic-gate
35897c478bd9Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
3590*a29e56d9SToomas Soome mutex_init(&lofi_chan_lock, NULL, MUTEX_DRIVER, NULL);
3591*a29e56d9SToomas Soome cv_init(&lofi_chan_cv, NULL, CV_DRIVER, NULL);
3592*a29e56d9SToomas Soome error = nvlist_alloc(&lofi_devlink_cache, NV_UNIQUE_NAME, KM_SLEEP);
35930fbb751dSJohn Levon
3594*a29e56d9SToomas Soome if (error == 0)
35957c478bd9Sstevel@tonic-gate error = mod_install(&modlinkage);
35967c478bd9Sstevel@tonic-gate if (error) {
3597*a29e56d9SToomas Soome id_space_destroy(lofi_id);
3598*a29e56d9SToomas Soome if (lofi_devlink_cache != NULL)
3599*a29e56d9SToomas Soome nvlist_free(lofi_devlink_cache);
3600*a29e56d9SToomas Soome mutex_destroy(&lofi_chan_lock);
3601*a29e56d9SToomas Soome cv_destroy(&lofi_chan_cv);
36027c478bd9Sstevel@tonic-gate mutex_destroy(&lofi_lock);
3603*a29e56d9SToomas Soome ddi_soft_state_fini((void **)&lofi_statep);
36040fbb751dSJohn Levon list_destroy(&lofi_list);
36057c478bd9Sstevel@tonic-gate }
36067c478bd9Sstevel@tonic-gate
36077c478bd9Sstevel@tonic-gate return (error);
36087c478bd9Sstevel@tonic-gate }
36097c478bd9Sstevel@tonic-gate
36107c478bd9Sstevel@tonic-gate int
_fini(void)36117c478bd9Sstevel@tonic-gate _fini(void)
36127c478bd9Sstevel@tonic-gate {
36137c478bd9Sstevel@tonic-gate int error;
36147c478bd9Sstevel@tonic-gate
36150fbb751dSJohn Levon mutex_enter(&lofi_lock);
36160fbb751dSJohn Levon
36170fbb751dSJohn Levon if (!list_is_empty(&lofi_list)) {
36180fbb751dSJohn Levon mutex_exit(&lofi_lock);
36197c478bd9Sstevel@tonic-gate return (EBUSY);
36200fbb751dSJohn Levon }
36210fbb751dSJohn Levon
36220fbb751dSJohn Levon mutex_exit(&lofi_lock);
36237c478bd9Sstevel@tonic-gate
36247c478bd9Sstevel@tonic-gate error = mod_remove(&modlinkage);
36257c478bd9Sstevel@tonic-gate if (error)
36267c478bd9Sstevel@tonic-gate return (error);
36277c478bd9Sstevel@tonic-gate
3628*a29e56d9SToomas Soome mutex_enter(&lofi_chan_lock);
3629*a29e56d9SToomas Soome nvlist_free(lofi_devlink_cache);
3630*a29e56d9SToomas Soome lofi_devlink_cache = NULL;
3631*a29e56d9SToomas Soome mutex_exit(&lofi_chan_lock);
3632*a29e56d9SToomas Soome
3633*a29e56d9SToomas Soome mutex_destroy(&lofi_chan_lock);
3634*a29e56d9SToomas Soome cv_destroy(&lofi_chan_cv);
36357c478bd9Sstevel@tonic-gate mutex_destroy(&lofi_lock);
3636*a29e56d9SToomas Soome id_space_destroy(lofi_id);
3637*a29e56d9SToomas Soome ddi_soft_state_fini((void **)&lofi_statep);
36380fbb751dSJohn Levon list_destroy(&lofi_list);
36397c478bd9Sstevel@tonic-gate
36407c478bd9Sstevel@tonic-gate return (error);
36417c478bd9Sstevel@tonic-gate }
36427c478bd9Sstevel@tonic-gate
36437c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)36447c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
36457c478bd9Sstevel@tonic-gate {
36467c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
36477c478bd9Sstevel@tonic-gate }
3648