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. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate * lofi (loopback file) driver - allows you to attach a file to a device, 277c478bd9Sstevel@tonic-gate * which can then be accessed through that device. The simple model is that 287c478bd9Sstevel@tonic-gate * you tell lofi to open a file, and then use the block device you get as 297c478bd9Sstevel@tonic-gate * you would any block device. lofi translates access to the block device 307c478bd9Sstevel@tonic-gate * into I/O on the underlying file. This is mostly useful for 317c478bd9Sstevel@tonic-gate * mounting images of filesystems. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * lofi is controlled through /dev/lofictl - this is the only device exported 347c478bd9Sstevel@tonic-gate * during attach, and is minor number 0. lofiadm communicates with lofi through 357c478bd9Sstevel@tonic-gate * ioctls on this device. When a file is attached to lofi, block and character 367c478bd9Sstevel@tonic-gate * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices 377c478bd9Sstevel@tonic-gate * are identified by their minor number, and the minor number is also used 387c478bd9Sstevel@tonic-gate * as the name in /dev/lofi. If we ever decide to support virtual disks, 397c478bd9Sstevel@tonic-gate * we'll have to divide the minor number space to identify fdisk partitions 407c478bd9Sstevel@tonic-gate * and slices, and the name will then be the minor number shifted down a 417c478bd9Sstevel@tonic-gate * few bits. Minor devices are tracked with state structures handled with 427c478bd9Sstevel@tonic-gate * ddi_soft_state(9F) for simplicity. 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * A file attached to lofi is opened when attached and not closed until 457c478bd9Sstevel@tonic-gate * explicitly detached from lofi. This seems more sensible than deferring 467c478bd9Sstevel@tonic-gate * the open until the /dev/lofi device is opened, for a number of reasons. 477c478bd9Sstevel@tonic-gate * One is that any failure is likely to be noticed by the person (or script) 487c478bd9Sstevel@tonic-gate * running lofiadm. Another is that it would be a security problem if the 497c478bd9Sstevel@tonic-gate * file was replaced by another one after being added but before being opened. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * The only hard part about lofi is the ioctls. In order to support things 527c478bd9Sstevel@tonic-gate * like 'newfs' on a lofi device, it needs to support certain disk ioctls. 537c478bd9Sstevel@tonic-gate * So it has to fake disk geometry and partition information. More may need 547c478bd9Sstevel@tonic-gate * to be faked if your favorite utility doesn't work and you think it should 557c478bd9Sstevel@tonic-gate * (fdformat doesn't work because it really wants to know the type of floppy 567c478bd9Sstevel@tonic-gate * controller to talk to, and that didn't seem easy to fake. Or possibly even 577c478bd9Sstevel@tonic-gate * necessary, since we have mkfs_pcfs now). 587c478bd9Sstevel@tonic-gate * 593d7072f8Seschrock * Normally, a lofi device cannot be detached if it is open (i.e. busy). To 603d7072f8Seschrock * support simulation of hotplug events, an optional force flag is provided. 613d7072f8Seschrock * If a lofi device is open when a force detach is requested, then the 623d7072f8Seschrock * underlying file is closed and any subsequent operations return EIO. When the 633d7072f8Seschrock * device is closed for the last time, it will be cleaned up at that time. In 643d7072f8Seschrock * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is 653d7072f8Seschrock * detached but not removed. 663d7072f8Seschrock * 677c478bd9Sstevel@tonic-gate * Known problems: 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * UFS logging. Mounting a UFS filesystem image "logging" 707c478bd9Sstevel@tonic-gate * works for basic copy testing but wedges during a build of ON through 717c478bd9Sstevel@tonic-gate * that image. Some deadlock in lufs holding the log mutex and then 727c478bd9Sstevel@tonic-gate * getting stuck on a buf. So for now, don't do that. 737c478bd9Sstevel@tonic-gate * 747c478bd9Sstevel@tonic-gate * Direct I/O. Since the filesystem data is being cached in the buffer 757c478bd9Sstevel@tonic-gate * cache, _and_ again in the underlying filesystem, it's tempting to 767c478bd9Sstevel@tonic-gate * enable direct I/O on the underlying file. Don't, because that deadlocks. 777c478bd9Sstevel@tonic-gate * I think to fix the cache-twice problem we might need filesystem support. 787c478bd9Sstevel@tonic-gate * 797c478bd9Sstevel@tonic-gate * Interesting things to do: 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically. 827c478bd9Sstevel@tonic-gate * 837c478bd9Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not 847c478bd9Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't 857d82f0f8SDina K Nimeh * need to fake a geometry, however, it may be relevant if you're replacing 867d82f0f8SDina K Nimeh * metadisk, or using lofi to get crypto. 877d82f0f8SDina K Nimeh * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1 887d82f0f8SDina K Nimeh * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home. 897d82f0f8SDina K Nimeh * In fact this even makes sense if you have lofi "above" metadisk. 907c478bd9Sstevel@tonic-gate * 917d82f0f8SDina K Nimeh * Encryption: 927d82f0f8SDina K Nimeh * Each lofi device can have its own symmetric key and cipher. 937d82f0f8SDina K Nimeh * They are passed to us by lofiadm(1m) in the correct format for use 947d82f0f8SDina K Nimeh * with the misc/kcf crypto_* routines. 957d82f0f8SDina K Nimeh * 967d82f0f8SDina K Nimeh * Each block has its own IV, that is calculated in lofi_blk_mech(), based 977d82f0f8SDina K Nimeh * on the "master" key held in the lsp and the block number of the buffer. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate #include <sys/types.h> 10187117650Saalok #include <netinet/in.h> 1027c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 1037c478bd9Sstevel@tonic-gate #include <sys/uio.h> 1047c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 1057c478bd9Sstevel@tonic-gate #include <sys/cred.h> 1067c478bd9Sstevel@tonic-gate #include <sys/mman.h> 1077c478bd9Sstevel@tonic-gate #include <sys/errno.h> 1087c478bd9Sstevel@tonic-gate #include <sys/aio_req.h> 1097c478bd9Sstevel@tonic-gate #include <sys/stat.h> 1107c478bd9Sstevel@tonic-gate #include <sys/file.h> 1117c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 1127c478bd9Sstevel@tonic-gate #include <sys/conf.h> 1137c478bd9Sstevel@tonic-gate #include <sys/debug.h> 1147c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 1157c478bd9Sstevel@tonic-gate #include <sys/lofi.h> 1167c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 1177c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 1187c478bd9Sstevel@tonic-gate #include <sys/filio.h> 1197c478bd9Sstevel@tonic-gate #include <sys/fdio.h> 1207c478bd9Sstevel@tonic-gate #include <sys/open.h> 1217c478bd9Sstevel@tonic-gate #include <sys/disp.h> 1227c478bd9Sstevel@tonic-gate #include <vm/seg_map.h> 1237c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 1247c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 12587117650Saalok #include <sys/zmod.h> 126*0fbb751dSJohn Levon #include <sys/id_space.h> 127*0fbb751dSJohn Levon #include <sys/mkdev.h> 1287d82f0f8SDina K Nimeh #include <sys/crypto/common.h> 1297d82f0f8SDina K Nimeh #include <sys/crypto/api.h> 130*0fbb751dSJohn Levon #include <sys/rctl.h> 131b1efbcd6SAlok Aggarwal #include <LzmaDec.h> 1327d82f0f8SDina K Nimeh 1337d82f0f8SDina K Nimeh /* 1347d82f0f8SDina K Nimeh * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h. 1357d82f0f8SDina K Nimeh * Crypto metadata, if it exists, is located at the end of the boot block 1367d82f0f8SDina K Nimeh * (BBOFF + BBSIZE, which is SBOFF). The super block and everything after 1377d82f0f8SDina K Nimeh * is offset by the size of the crypto metadata which is handled by 1387d82f0f8SDina K Nimeh * lsp->ls_crypto_offset. 1397d82f0f8SDina K Nimeh */ 1407d82f0f8SDina K Nimeh #define CRYOFF ((off_t)8192) 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks" 1437c478bd9Sstevel@tonic-gate #define SIZE_PROP_NAME "Size" 144*0fbb751dSJohn Levon #define ZONE_PROP_NAME "zone" 1457c478bd9Sstevel@tonic-gate 1467d82f0f8SDina K Nimeh #define SETUP_C_DATA(cd, buf, len) \ 1477d82f0f8SDina K Nimeh (cd).cd_format = CRYPTO_DATA_RAW; \ 1487d82f0f8SDina K Nimeh (cd).cd_offset = 0; \ 1497d82f0f8SDina K Nimeh (cd).cd_miscdata = NULL; \ 1507d82f0f8SDina K Nimeh (cd).cd_length = (len); \ 1517d82f0f8SDina K Nimeh (cd).cd_raw.iov_base = (buf); \ 1527d82f0f8SDina K Nimeh (cd).cd_raw.iov_len = (len); 1537d82f0f8SDina K Nimeh 1547d82f0f8SDina K Nimeh #define UIO_CHECK(uio) \ 1557d82f0f8SDina K Nimeh if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \ 1567d82f0f8SDina K Nimeh ((uio)->uio_resid % DEV_BSIZE) != 0) { \ 1577d82f0f8SDina K Nimeh return (EINVAL); \ 1587d82f0f8SDina K Nimeh } 1597d82f0f8SDina K Nimeh 1607d82f0f8SDina K Nimeh static dev_info_t *lofi_dip = NULL; 1617d82f0f8SDina K Nimeh static void *lofi_statep = NULL; 1627c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */ 163*0fbb751dSJohn Levon static id_space_t *lofi_minor_id; 164*0fbb751dSJohn Levon static list_t lofi_list; 165*0fbb751dSJohn Levon static zone_key_t lofi_zone_key; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the 1697c478bd9Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively 1707c478bd9Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy, 1717c478bd9Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of 1727c478bd9Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for 1737c478bd9Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should 1747c478bd9Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the 1757c478bd9Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic 1767c478bd9Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on 1777c478bd9Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE; 1807c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */ 1817c478bd9Sstevel@tonic-gate 1827d82f0f8SDina K Nimeh const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC; 1837c478bd9Sstevel@tonic-gate 1844058a205Sjrgn.keil@googlemail.com /* 1854058a205Sjrgn.keil@googlemail.com * To avoid decompressing data in a compressed segment multiple times 1864058a205Sjrgn.keil@googlemail.com * when accessing small parts of a segment's data, we cache and reuse 1874058a205Sjrgn.keil@googlemail.com * the uncompressed segment's data. 1884058a205Sjrgn.keil@googlemail.com * 1894058a205Sjrgn.keil@googlemail.com * A single cached segment is sufficient to avoid lots of duplicate 1904058a205Sjrgn.keil@googlemail.com * segment decompress operations. A small cache size also reduces the 1914058a205Sjrgn.keil@googlemail.com * memory footprint. 1924058a205Sjrgn.keil@googlemail.com * 1934058a205Sjrgn.keil@googlemail.com * lofi_max_comp_cache is the maximum number of decompressed data segments 1944058a205Sjrgn.keil@googlemail.com * cached for each compressed lofi image. It can be set to 0 to disable 1954058a205Sjrgn.keil@googlemail.com * caching. 1964058a205Sjrgn.keil@googlemail.com */ 1974058a205Sjrgn.keil@googlemail.com 1984058a205Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1; 1994058a205Sjrgn.keil@googlemail.com 20087117650Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 20187117650Saalok size_t *destlen, int level); 20287117650Saalok 203b1efbcd6SAlok Aggarwal static int lzma_decompress(void *src, size_t srclen, void *dst, 204b1efbcd6SAlok Aggarwal size_t *dstlen, int level); 205b1efbcd6SAlok Aggarwal 20687117650Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { 20787117650Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */ 20887117650Saalok {gzip_decompress, NULL, 6, "gzip-6"}, 209b1efbcd6SAlok Aggarwal {gzip_decompress, NULL, 9, "gzip-9"}, 210b1efbcd6SAlok Aggarwal {lzma_decompress, NULL, 0, "lzma"} 21187117650Saalok }; 21287117650Saalok 213b1efbcd6SAlok Aggarwal /*ARGSUSED*/ 214b1efbcd6SAlok Aggarwal static void 215b1efbcd6SAlok Aggarwal *SzAlloc(void *p, size_t size) 216b1efbcd6SAlok Aggarwal { 217b1efbcd6SAlok Aggarwal return (kmem_alloc(size, KM_SLEEP)); 218b1efbcd6SAlok Aggarwal } 219b1efbcd6SAlok Aggarwal 220b1efbcd6SAlok Aggarwal /*ARGSUSED*/ 221b1efbcd6SAlok Aggarwal static void 222b1efbcd6SAlok Aggarwal SzFree(void *p, void *address, size_t size) 223b1efbcd6SAlok Aggarwal { 224b1efbcd6SAlok Aggarwal kmem_free(address, size); 225b1efbcd6SAlok Aggarwal } 226b1efbcd6SAlok Aggarwal 227b1efbcd6SAlok Aggarwal static ISzAlloc g_Alloc = { SzAlloc, SzFree }; 228b1efbcd6SAlok Aggarwal 2294058a205Sjrgn.keil@googlemail.com /* 2304058a205Sjrgn.keil@googlemail.com * Free data referenced by the linked list of cached uncompressed 2314058a205Sjrgn.keil@googlemail.com * segments. 2324058a205Sjrgn.keil@googlemail.com */ 2334058a205Sjrgn.keil@googlemail.com static void 2344058a205Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp) 2354058a205Sjrgn.keil@googlemail.com { 2364058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 2374058a205Sjrgn.keil@googlemail.com 2384058a205Sjrgn.keil@googlemail.com while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) { 2394058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz); 2404058a205Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache)); 2414058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--; 2424058a205Sjrgn.keil@googlemail.com } 2434058a205Sjrgn.keil@googlemail.com ASSERT(lsp->ls_comp_cache_count == 0); 2444058a205Sjrgn.keil@googlemail.com } 2454058a205Sjrgn.keil@googlemail.com 2467c478bd9Sstevel@tonic-gate static int 2477c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp) 2487c478bd9Sstevel@tonic-gate { 249*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 2507c478bd9Sstevel@tonic-gate return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate static int 2547c478bd9Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp) 2557c478bd9Sstevel@tonic-gate { 256*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 2577c478bd9Sstevel@tonic-gate switch (otyp) { 2587c478bd9Sstevel@tonic-gate case OTYP_CHR: 2597c478bd9Sstevel@tonic-gate lsp->ls_chr_open = 1; 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate case OTYP_BLK: 2627c478bd9Sstevel@tonic-gate lsp->ls_blk_open = 1; 2637c478bd9Sstevel@tonic-gate break; 2647c478bd9Sstevel@tonic-gate case OTYP_LYR: 2657c478bd9Sstevel@tonic-gate lsp->ls_lyr_open_count++; 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate default: 2687c478bd9Sstevel@tonic-gate return (-1); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate return (0); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate static void 2747c478bd9Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp) 2757c478bd9Sstevel@tonic-gate { 276*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 2777c478bd9Sstevel@tonic-gate switch (otyp) { 2787c478bd9Sstevel@tonic-gate case OTYP_CHR: 2797c478bd9Sstevel@tonic-gate lsp->ls_chr_open = 0; 2807c478bd9Sstevel@tonic-gate break; 2817c478bd9Sstevel@tonic-gate case OTYP_BLK: 2827c478bd9Sstevel@tonic-gate lsp->ls_blk_open = 0; 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate case OTYP_LYR: 2857c478bd9Sstevel@tonic-gate lsp->ls_lyr_open_count--; 2867c478bd9Sstevel@tonic-gate break; 2877c478bd9Sstevel@tonic-gate default: 2887c478bd9Sstevel@tonic-gate break; 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2923d7072f8Seschrock static void 2937d82f0f8SDina K Nimeh lofi_free_crypto(struct lofi_state *lsp) 2947d82f0f8SDina K Nimeh { 295*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 2967d82f0f8SDina K Nimeh 2977d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 2987d82f0f8SDina K Nimeh /* 2997d82f0f8SDina K Nimeh * Clean up the crypto state so that it doesn't hang around 3007d82f0f8SDina K Nimeh * in memory after we are done with it. 3017d82f0f8SDina K Nimeh */ 302*0fbb751dSJohn Levon if (lsp->ls_key.ck_data != NULL) { 3037d82f0f8SDina K Nimeh bzero(lsp->ls_key.ck_data, 3047d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 3057d82f0f8SDina K Nimeh kmem_free(lsp->ls_key.ck_data, 3067d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 3077d82f0f8SDina K Nimeh lsp->ls_key.ck_data = NULL; 3087d82f0f8SDina K Nimeh lsp->ls_key.ck_length = 0; 309*0fbb751dSJohn Levon } 3107d82f0f8SDina K Nimeh 3117d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != NULL) { 3127d82f0f8SDina K Nimeh kmem_free(lsp->ls_mech.cm_param, 3137d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len); 3147d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = NULL; 3157d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = 0; 3167d82f0f8SDina K Nimeh } 3177d82f0f8SDina K Nimeh 3187d82f0f8SDina K Nimeh if (lsp->ls_iv_mech.cm_param != NULL) { 3197d82f0f8SDina K Nimeh kmem_free(lsp->ls_iv_mech.cm_param, 3207d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len); 3217d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param = NULL; 3227d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len = 0; 3237d82f0f8SDina K Nimeh } 3247d82f0f8SDina K Nimeh 3257d82f0f8SDina K Nimeh mutex_destroy(&lsp->ls_crypto_lock); 3267d82f0f8SDina K Nimeh } 3277d82f0f8SDina K Nimeh } 3287d82f0f8SDina K Nimeh 3297d82f0f8SDina K Nimeh static void 330*0fbb751dSJohn Levon lofi_destroy(struct lofi_state *lsp, cred_t *credp) 3313d7072f8Seschrock { 332*0fbb751dSJohn Levon minor_t minor = getminor(lsp->ls_dev); 333b9c7fb03SAlok Aggarwal int i; 3343d7072f8Seschrock 335*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 336*0fbb751dSJohn Levon 337*0fbb751dSJohn Levon list_remove(&lofi_list, lsp); 3387d82f0f8SDina K Nimeh 3397d82f0f8SDina K Nimeh lofi_free_crypto(lsp); 3407d82f0f8SDina K Nimeh 341b9c7fb03SAlok Aggarwal /* 342b9c7fb03SAlok Aggarwal * Free pre-allocated compressed buffers 343b9c7fb03SAlok Aggarwal */ 344b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs != NULL) { 345b9c7fb03SAlok Aggarwal for (i = 0; i < lofi_taskq_nthreads; i++) { 346b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[i].bufsize > 0) 347b9c7fb03SAlok Aggarwal kmem_free(lsp->ls_comp_bufs[i].buf, 348b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[i].bufsize); 349b9c7fb03SAlok Aggarwal } 350b9c7fb03SAlok Aggarwal kmem_free(lsp->ls_comp_bufs, 351b9c7fb03SAlok Aggarwal sizeof (struct compbuf) * lofi_taskq_nthreads); 352b9c7fb03SAlok Aggarwal } 353b9c7fb03SAlok Aggarwal 354*0fbb751dSJohn Levon (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 355*0fbb751dSJohn Levon 1, 0, credp, NULL); 356*0fbb751dSJohn Levon VN_RELE(lsp->ls_vp); 357*0fbb751dSJohn Levon if (lsp->ls_stacked_vp != lsp->ls_vp) 358*0fbb751dSJohn Levon VN_RELE(lsp->ls_stacked_vp); 359*0fbb751dSJohn Levon 360*0fbb751dSJohn Levon taskq_destroy(lsp->ls_taskq); 361*0fbb751dSJohn Levon 362*0fbb751dSJohn Levon if (lsp->ls_kstat != NULL) 363*0fbb751dSJohn Levon kstat_delete(lsp->ls_kstat); 364*0fbb751dSJohn Levon 365*0fbb751dSJohn Levon /* 366*0fbb751dSJohn Levon * Free cached decompressed segment data 367*0fbb751dSJohn Levon */ 368*0fbb751dSJohn Levon lofi_free_comp_cache(lsp); 369*0fbb751dSJohn Levon list_destroy(&lsp->ls_comp_cache); 370*0fbb751dSJohn Levon 371*0fbb751dSJohn Levon if (lsp->ls_uncomp_seg_sz > 0) { 372*0fbb751dSJohn Levon kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 373*0fbb751dSJohn Levon lsp->ls_uncomp_seg_sz = 0; 374*0fbb751dSJohn Levon } 375*0fbb751dSJohn Levon 376*0fbb751dSJohn Levon rctl_decr_lofi(lsp->ls_zone, 1); 377*0fbb751dSJohn Levon zone_rele(lsp->ls_zone); 378*0fbb751dSJohn Levon 379*0fbb751dSJohn Levon mutex_destroy(&lsp->ls_comp_cache_lock); 380*0fbb751dSJohn Levon mutex_destroy(&lsp->ls_comp_bufs_lock); 381*0fbb751dSJohn Levon mutex_destroy(&lsp->ls_kstat_lock); 3824058a205Sjrgn.keil@googlemail.com mutex_destroy(&lsp->ls_vp_lock); 3834058a205Sjrgn.keil@googlemail.com 384*0fbb751dSJohn Levon ASSERT(ddi_get_soft_state(lofi_statep, minor) == lsp); 3853d7072f8Seschrock ddi_soft_state_free(lofi_statep, minor); 386*0fbb751dSJohn Levon id_free(lofi_minor_id, minor); 387*0fbb751dSJohn Levon } 388*0fbb751dSJohn Levon 389*0fbb751dSJohn Levon static void 390*0fbb751dSJohn Levon lofi_free_dev(dev_t dev) 391*0fbb751dSJohn Levon { 392*0fbb751dSJohn Levon minor_t minor = getminor(dev); 393*0fbb751dSJohn Levon char namebuf[50]; 394*0fbb751dSJohn Levon 395*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 396*0fbb751dSJohn Levon 397*0fbb751dSJohn Levon (void) ddi_prop_remove(dev, lofi_dip, ZONE_PROP_NAME); 398*0fbb751dSJohn Levon (void) ddi_prop_remove(dev, lofi_dip, SIZE_PROP_NAME); 399*0fbb751dSJohn Levon (void) ddi_prop_remove(dev, lofi_dip, NBLOCKS_PROP_NAME); 400*0fbb751dSJohn Levon 401*0fbb751dSJohn Levon (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 402*0fbb751dSJohn Levon ddi_remove_minor_node(lofi_dip, namebuf); 403*0fbb751dSJohn Levon (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 404*0fbb751dSJohn Levon ddi_remove_minor_node(lofi_dip, namebuf); 405*0fbb751dSJohn Levon } 406*0fbb751dSJohn Levon 407*0fbb751dSJohn Levon /*ARGSUSED*/ 408*0fbb751dSJohn Levon static void 409*0fbb751dSJohn Levon lofi_zone_shutdown(zoneid_t zoneid, void *arg) 410*0fbb751dSJohn Levon { 411*0fbb751dSJohn Levon struct lofi_state *lsp; 412*0fbb751dSJohn Levon struct lofi_state *next; 413*0fbb751dSJohn Levon 414*0fbb751dSJohn Levon mutex_enter(&lofi_lock); 415*0fbb751dSJohn Levon 416*0fbb751dSJohn Levon for (lsp = list_head(&lofi_list); lsp != NULL; lsp = next) { 417*0fbb751dSJohn Levon 418*0fbb751dSJohn Levon /* lofi_destroy() frees lsp */ 419*0fbb751dSJohn Levon next = list_next(&lofi_list, lsp); 420*0fbb751dSJohn Levon 421*0fbb751dSJohn Levon if (lsp->ls_zone->zone_id != zoneid) 422*0fbb751dSJohn Levon continue; 423*0fbb751dSJohn Levon 424*0fbb751dSJohn Levon /* 425*0fbb751dSJohn Levon * No in-zone processes are running, but something has this 426*0fbb751dSJohn Levon * open. It's either a global zone process, or a lofi 427*0fbb751dSJohn Levon * mount. In either case we set ls_cleanup so the last 428*0fbb751dSJohn Levon * user destroys the device. 429*0fbb751dSJohn Levon */ 430*0fbb751dSJohn Levon if (is_opened(lsp)) { 431*0fbb751dSJohn Levon lsp->ls_cleanup = 1; 432*0fbb751dSJohn Levon } else { 433*0fbb751dSJohn Levon lofi_free_dev(lsp->ls_dev); 434*0fbb751dSJohn Levon lofi_destroy(lsp, kcred); 435*0fbb751dSJohn Levon } 436*0fbb751dSJohn Levon } 437*0fbb751dSJohn Levon 438*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 4393d7072f8Seschrock } 4403d7072f8Seschrock 4413d7072f8Seschrock /*ARGSUSED*/ 4427c478bd9Sstevel@tonic-gate static int 4437c478bd9Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp) 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate minor_t minor; 4467c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 4477c478bd9Sstevel@tonic-gate 448*0fbb751dSJohn Levon /* 449*0fbb751dSJohn Levon * lofiadm -a /dev/lofi/1 gets us here. 450*0fbb751dSJohn Levon */ 451*0fbb751dSJohn Levon if (mutex_owner(&lofi_lock) == curthread) 4527c478bd9Sstevel@tonic-gate return (EINVAL); 453*0fbb751dSJohn Levon 454*0fbb751dSJohn Levon mutex_enter(&lofi_lock); 455*0fbb751dSJohn Levon 456*0fbb751dSJohn Levon minor = getminor(*devp); 457*0fbb751dSJohn Levon 458*0fbb751dSJohn Levon /* master control device */ 459*0fbb751dSJohn Levon if (minor == 0) { 4607c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4617c478bd9Sstevel@tonic-gate return (0); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate /* otherwise, the mapping should already exist */ 4657c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 4667c478bd9Sstevel@tonic-gate if (lsp == NULL) { 4677c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4687c478bd9Sstevel@tonic-gate return (EINVAL); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4713d7072f8Seschrock if (lsp->ls_vp == NULL) { 4723d7072f8Seschrock mutex_exit(&lofi_lock); 4733d7072f8Seschrock return (ENXIO); 4743d7072f8Seschrock } 4753d7072f8Seschrock 4767c478bd9Sstevel@tonic-gate if (mark_opened(lsp, otyp) == -1) { 4777c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4787c478bd9Sstevel@tonic-gate return (EINVAL); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4827c478bd9Sstevel@tonic-gate return (0); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4853d7072f8Seschrock /*ARGSUSED*/ 4867c478bd9Sstevel@tonic-gate static int 4877c478bd9Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate minor_t minor; 4907c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 4937c478bd9Sstevel@tonic-gate minor = getminor(dev); 4947c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 4957c478bd9Sstevel@tonic-gate if (lsp == NULL) { 4967c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4977c478bd9Sstevel@tonic-gate return (EINVAL); 4987c478bd9Sstevel@tonic-gate } 499*0fbb751dSJohn Levon 500*0fbb751dSJohn Levon if (minor == 0) { 501*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 502*0fbb751dSJohn Levon return (0); 503*0fbb751dSJohn Levon } 504*0fbb751dSJohn Levon 5057c478bd9Sstevel@tonic-gate mark_closed(lsp, otyp); 5063d7072f8Seschrock 5073d7072f8Seschrock /* 50893239addSjohnlev * If we forcibly closed the underlying device (li_force), or 50993239addSjohnlev * asked for cleanup (li_cleanup), finish up if we're the last 51093239addSjohnlev * out of the door. 5113d7072f8Seschrock */ 512*0fbb751dSJohn Levon if (!is_opened(lsp) && (lsp->ls_cleanup || lsp->ls_vp == NULL)) { 513*0fbb751dSJohn Levon lofi_free_dev(dev); 514*0fbb751dSJohn Levon lofi_destroy(lsp, credp); 515*0fbb751dSJohn Levon } 51693239addSjohnlev 5177c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 5187c478bd9Sstevel@tonic-gate return (0); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217d82f0f8SDina K Nimeh /* 5227d82f0f8SDina K Nimeh * Sets the mechanism's initialization vector (IV) if one is needed. 5237d82f0f8SDina K Nimeh * The IV is computed from the data block number. lsp->ls_mech is 5247d82f0f8SDina K Nimeh * altered so that: 5257d82f0f8SDina K Nimeh * lsp->ls_mech.cm_param_len is set to the IV len. 5267d82f0f8SDina K Nimeh * lsp->ls_mech.cm_param is set to the IV. 5277d82f0f8SDina K Nimeh */ 5287d82f0f8SDina K Nimeh static int 5297d82f0f8SDina K Nimeh lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno) 5307d82f0f8SDina K Nimeh { 5317d82f0f8SDina K Nimeh int ret; 5327d82f0f8SDina K Nimeh crypto_data_t cdata; 5337d82f0f8SDina K Nimeh char *iv; 5347d82f0f8SDina K Nimeh size_t iv_len; 5357d82f0f8SDina K Nimeh size_t min; 5367d82f0f8SDina K Nimeh void *data; 5377d82f0f8SDina K Nimeh size_t datasz; 5387d82f0f8SDina K Nimeh 539*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lsp->ls_crypto_lock)); 5407d82f0f8SDina K Nimeh 5417d82f0f8SDina K Nimeh if (lsp == NULL) 5427d82f0f8SDina K Nimeh return (CRYPTO_DEVICE_ERROR); 5437d82f0f8SDina K Nimeh 5447d82f0f8SDina K Nimeh /* lsp->ls_mech.cm_param{_len} has already been set for static iv */ 5457d82f0f8SDina K Nimeh if (lsp->ls_iv_type == IVM_NONE) { 5467d82f0f8SDina K Nimeh return (CRYPTO_SUCCESS); 5477d82f0f8SDina K Nimeh } 5487d82f0f8SDina K Nimeh 5497d82f0f8SDina K Nimeh /* 5507d82f0f8SDina K Nimeh * if kmem already alloced from previous call and it's the same size 5517d82f0f8SDina K Nimeh * we need now, just recycle it; allocate new kmem only if we have to 5527d82f0f8SDina K Nimeh */ 5537d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param == NULL || 5547d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len != lsp->ls_iv_len) { 5557d82f0f8SDina K Nimeh iv_len = lsp->ls_iv_len; 5567d82f0f8SDina K Nimeh iv = kmem_zalloc(iv_len, KM_SLEEP); 5577d82f0f8SDina K Nimeh } else { 5587d82f0f8SDina K Nimeh iv_len = lsp->ls_mech.cm_param_len; 5597d82f0f8SDina K Nimeh iv = lsp->ls_mech.cm_param; 5607d82f0f8SDina K Nimeh bzero(iv, iv_len); 5617d82f0f8SDina K Nimeh } 5627d82f0f8SDina K Nimeh 5637d82f0f8SDina K Nimeh switch (lsp->ls_iv_type) { 5647d82f0f8SDina K Nimeh case IVM_ENC_BLKNO: 5657d82f0f8SDina K Nimeh /* iv is not static, lblkno changes each time */ 5667d82f0f8SDina K Nimeh data = &lblkno; 5677d82f0f8SDina K Nimeh datasz = sizeof (lblkno); 5687d82f0f8SDina K Nimeh break; 5697d82f0f8SDina K Nimeh default: 5707d82f0f8SDina K Nimeh data = 0; 5717d82f0f8SDina K Nimeh datasz = 0; 5727d82f0f8SDina K Nimeh break; 5737d82f0f8SDina K Nimeh } 5747d82f0f8SDina K Nimeh 5757d82f0f8SDina K Nimeh /* 5767d82f0f8SDina K Nimeh * write blkno into the iv buffer padded on the left in case 5777d82f0f8SDina K Nimeh * blkno ever grows bigger than its current longlong_t size 5787d82f0f8SDina K Nimeh * or a variation other than blkno is used for the iv data 5797d82f0f8SDina K Nimeh */ 5807d82f0f8SDina K Nimeh min = MIN(datasz, iv_len); 5817d82f0f8SDina K Nimeh bcopy(data, iv + (iv_len - min), min); 5827d82f0f8SDina K Nimeh 5837d82f0f8SDina K Nimeh /* encrypt the data in-place to get the IV */ 5847d82f0f8SDina K Nimeh SETUP_C_DATA(cdata, iv, iv_len); 5857d82f0f8SDina K Nimeh 5867d82f0f8SDina K Nimeh ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key, 5877d82f0f8SDina K Nimeh NULL, NULL, NULL); 5887d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) { 5897d82f0f8SDina K Nimeh cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)", 5907d82f0f8SDina K Nimeh lblkno, ret); 5917d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != iv) 5927d82f0f8SDina K Nimeh kmem_free(iv, iv_len); 593b1efbcd6SAlok Aggarwal 5947d82f0f8SDina K Nimeh return (ret); 5957d82f0f8SDina K Nimeh } 5967d82f0f8SDina K Nimeh 5977d82f0f8SDina K Nimeh /* clean up the iv from the last computation */ 5987d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv) 5997d82f0f8SDina K Nimeh kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len); 600b1efbcd6SAlok Aggarwal 6017d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = iv_len; 6027d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = iv; 6037d82f0f8SDina K Nimeh 6047d82f0f8SDina K Nimeh return (CRYPTO_SUCCESS); 6057d82f0f8SDina K Nimeh } 6067d82f0f8SDina K Nimeh 6077d82f0f8SDina K Nimeh /* 6087d82f0f8SDina K Nimeh * Performs encryption and decryption of a chunk of data of size "len", 6097d82f0f8SDina K Nimeh * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of 6107d82f0f8SDina K Nimeh * DEV_BSIZE. 6117d82f0f8SDina K Nimeh */ 6127d82f0f8SDina K Nimeh static int 6137d82f0f8SDina K Nimeh lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext, 6147d82f0f8SDina K Nimeh caddr_t ciphertext, size_t len, boolean_t op_encrypt) 6157d82f0f8SDina K Nimeh { 6167d82f0f8SDina K Nimeh crypto_data_t cdata; 6177d82f0f8SDina K Nimeh crypto_data_t wdata; 6187d82f0f8SDina K Nimeh int ret; 6197d82f0f8SDina K Nimeh longlong_t lblkno = bp->b_lblkno; 6207d82f0f8SDina K Nimeh 6217d82f0f8SDina K Nimeh mutex_enter(&lsp->ls_crypto_lock); 6227d82f0f8SDina K Nimeh 6237d82f0f8SDina K Nimeh /* 6247d82f0f8SDina K Nimeh * though we could encrypt/decrypt entire "len" chunk of data, we need 6257d82f0f8SDina K Nimeh * to break it into DEV_BSIZE pieces to capture blkno incrementing 6267d82f0f8SDina K Nimeh */ 6277d82f0f8SDina K Nimeh SETUP_C_DATA(cdata, plaintext, len); 6287d82f0f8SDina K Nimeh cdata.cd_length = DEV_BSIZE; 6297d82f0f8SDina K Nimeh if (ciphertext != NULL) { /* not in-place crypto */ 6307d82f0f8SDina K Nimeh SETUP_C_DATA(wdata, ciphertext, len); 6317d82f0f8SDina K Nimeh wdata.cd_length = DEV_BSIZE; 6327d82f0f8SDina K Nimeh } 6337d82f0f8SDina K Nimeh 6347d82f0f8SDina K Nimeh do { 6357d82f0f8SDina K Nimeh ret = lofi_blk_mech(lsp, lblkno); 6367d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) 6377d82f0f8SDina K Nimeh continue; 6387d82f0f8SDina K Nimeh 6397d82f0f8SDina K Nimeh if (op_encrypt) { 6407d82f0f8SDina K Nimeh ret = crypto_encrypt(&lsp->ls_mech, &cdata, 6417d82f0f8SDina K Nimeh &lsp->ls_key, NULL, 6427d82f0f8SDina K Nimeh ((ciphertext != NULL) ? &wdata : NULL), NULL); 6437d82f0f8SDina K Nimeh } else { 6447d82f0f8SDina K Nimeh ret = crypto_decrypt(&lsp->ls_mech, &cdata, 6457d82f0f8SDina K Nimeh &lsp->ls_key, NULL, 6467d82f0f8SDina K Nimeh ((ciphertext != NULL) ? &wdata : NULL), NULL); 6477d82f0f8SDina K Nimeh } 6487d82f0f8SDina K Nimeh 6497d82f0f8SDina K Nimeh cdata.cd_offset += DEV_BSIZE; 6507d82f0f8SDina K Nimeh if (ciphertext != NULL) 6517d82f0f8SDina K Nimeh wdata.cd_offset += DEV_BSIZE; 6527d82f0f8SDina K Nimeh lblkno++; 6537d82f0f8SDina K Nimeh } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len); 6547d82f0f8SDina K Nimeh 6557d82f0f8SDina K Nimeh mutex_exit(&lsp->ls_crypto_lock); 6567d82f0f8SDina K Nimeh 6577d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) { 6587d82f0f8SDina K Nimeh cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)", 6597d82f0f8SDina K Nimeh op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()", 6607d82f0f8SDina K Nimeh lblkno, ret); 6617d82f0f8SDina K Nimeh } 6627d82f0f8SDina K Nimeh 6637d82f0f8SDina K Nimeh return (ret); 6647d82f0f8SDina K Nimeh } 6657d82f0f8SDina K Nimeh 6667d82f0f8SDina K Nimeh #define RDWR_RAW 1 6677d82f0f8SDina K Nimeh #define RDWR_BCOPY 2 6687d82f0f8SDina K Nimeh 6697d82f0f8SDina K Nimeh static int 6707d82f0f8SDina K Nimeh lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 6717d82f0f8SDina K Nimeh struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn) 6727d82f0f8SDina K Nimeh { 6737d82f0f8SDina K Nimeh ssize_t resid; 6747d82f0f8SDina K Nimeh int isread; 6757d82f0f8SDina K Nimeh int error; 6767d82f0f8SDina K Nimeh 6777d82f0f8SDina K Nimeh /* 6787d82f0f8SDina K Nimeh * Handles reads/writes for both plain and encrypted lofi 6797d82f0f8SDina K Nimeh * Note: offset is already shifted by lsp->ls_crypto_offset 6807d82f0f8SDina K Nimeh * when it gets here. 6817d82f0f8SDina K Nimeh */ 6827d82f0f8SDina K Nimeh 6837d82f0f8SDina K Nimeh isread = bp->b_flags & B_READ; 6847d82f0f8SDina K Nimeh if (isread) { 6857d82f0f8SDina K Nimeh if (method == RDWR_BCOPY) { 6867d82f0f8SDina K Nimeh /* DO NOT update bp->b_resid for bcopy */ 6877d82f0f8SDina K Nimeh bcopy(bcopy_locn, bufaddr, len); 6887d82f0f8SDina K Nimeh error = 0; 6897d82f0f8SDina K Nimeh } else { /* RDWR_RAW */ 6907d82f0f8SDina K Nimeh error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len, 6917d82f0f8SDina K Nimeh offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 6927d82f0f8SDina K Nimeh &resid); 6937d82f0f8SDina K Nimeh bp->b_resid = resid; 6947d82f0f8SDina K Nimeh } 6957d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled && error == 0) { 6967d82f0f8SDina K Nimeh if (lofi_crypto(lsp, bp, bufaddr, NULL, len, 6977d82f0f8SDina K Nimeh B_FALSE) != CRYPTO_SUCCESS) { 6987d82f0f8SDina K Nimeh /* 6997d82f0f8SDina K Nimeh * XXX: original code didn't set residual 7007d82f0f8SDina K Nimeh * back to len because no error was expected 7017d82f0f8SDina K Nimeh * from bcopy() if encryption is not enabled 7027d82f0f8SDina K Nimeh */ 7037d82f0f8SDina K Nimeh if (method != RDWR_BCOPY) 7047d82f0f8SDina K Nimeh bp->b_resid = len; 7057d82f0f8SDina K Nimeh error = EIO; 7067d82f0f8SDina K Nimeh } 7077d82f0f8SDina K Nimeh } 7087d82f0f8SDina K Nimeh return (error); 7097d82f0f8SDina K Nimeh } else { 7107d82f0f8SDina K Nimeh void *iobuf = bufaddr; 7117d82f0f8SDina K Nimeh 7127d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 7137d82f0f8SDina K Nimeh /* don't do in-place crypto to keep bufaddr intact */ 7147d82f0f8SDina K Nimeh iobuf = kmem_alloc(len, KM_SLEEP); 7157d82f0f8SDina K Nimeh if (lofi_crypto(lsp, bp, bufaddr, iobuf, len, 7167d82f0f8SDina K Nimeh B_TRUE) != CRYPTO_SUCCESS) { 7177d82f0f8SDina K Nimeh kmem_free(iobuf, len); 7187d82f0f8SDina K Nimeh if (method != RDWR_BCOPY) 7197d82f0f8SDina K Nimeh bp->b_resid = len; 7207d82f0f8SDina K Nimeh return (EIO); 7217d82f0f8SDina K Nimeh } 7227d82f0f8SDina K Nimeh } 7237d82f0f8SDina K Nimeh if (method == RDWR_BCOPY) { 7247d82f0f8SDina K Nimeh /* DO NOT update bp->b_resid for bcopy */ 7257d82f0f8SDina K Nimeh bcopy(iobuf, bcopy_locn, len); 7267d82f0f8SDina K Nimeh error = 0; 7277d82f0f8SDina K Nimeh } else { /* RDWR_RAW */ 7287d82f0f8SDina K Nimeh error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len, 7297d82f0f8SDina K Nimeh offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 7307d82f0f8SDina K Nimeh &resid); 7317d82f0f8SDina K Nimeh bp->b_resid = resid; 7327d82f0f8SDina K Nimeh } 7337d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 7347d82f0f8SDina K Nimeh kmem_free(iobuf, len); 7357d82f0f8SDina K Nimeh } 7367d82f0f8SDina K Nimeh return (error); 7377d82f0f8SDina K Nimeh } 7387d82f0f8SDina K Nimeh } 7397d82f0f8SDina K Nimeh 74087117650Saalok static int 74187117650Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 74287117650Saalok struct lofi_state *lsp) 7437c478bd9Sstevel@tonic-gate { 7447c478bd9Sstevel@tonic-gate int error; 74587117650Saalok offset_t alignedoffset, mapoffset; 7467c478bd9Sstevel@tonic-gate size_t xfersize; 7477c478bd9Sstevel@tonic-gate int isread; 7487c478bd9Sstevel@tonic-gate int smflags; 74987117650Saalok caddr_t mapaddr; 75087117650Saalok size_t len; 7517c478bd9Sstevel@tonic-gate enum seg_rw srw; 7527d82f0f8SDina K Nimeh int save_error; 7537d82f0f8SDina K Nimeh 7547d82f0f8SDina K Nimeh /* 7557d82f0f8SDina K Nimeh * Note: offset is already shifted by lsp->ls_crypto_offset 7567d82f0f8SDina K Nimeh * when it gets here. 7577d82f0f8SDina K Nimeh */ 7587d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) 7597d82f0f8SDina K Nimeh ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size); 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate /* 7627c478bd9Sstevel@tonic-gate * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 7637c478bd9Sstevel@tonic-gate * an 8K boundary, but the buf transfer address may not be 76487117650Saalok * aligned on more than a 512-byte boundary (we don't enforce 76587117650Saalok * that even though we could). This matters since the initial 76687117650Saalok * part of the transfer may not start at offset 0 within the 76787117650Saalok * segmap'd chunk. So we have to compensate for that with 76887117650Saalok * 'mapoffset'. Subsequent chunks always start off at the 76987117650Saalok * beginning, and the last is capped by b_resid 7707d82f0f8SDina K Nimeh * 7717d82f0f8SDina K Nimeh * Visually, where "|" represents page map boundaries: 7727d82f0f8SDina K Nimeh * alignedoffset (mapaddr begins at this segmap boundary) 7737d82f0f8SDina K Nimeh * | offset (from beginning of file) 7747d82f0f8SDina K Nimeh * | | len 7757d82f0f8SDina K Nimeh * v v v 7767d82f0f8SDina K Nimeh * ===|====X========|====...======|========X====|==== 7777d82f0f8SDina K Nimeh * /-------------...---------------/ 7787d82f0f8SDina K Nimeh * ^ bp->b_bcount/bp->b_resid at start 7797d82f0f8SDina K Nimeh * /----/--------/----...------/--------/ 7807d82f0f8SDina K Nimeh * ^ ^ ^ ^ ^ 7817d82f0f8SDina K Nimeh * | | | | nth xfersize (<= MAXBSIZE) 7827d82f0f8SDina K Nimeh * | | 2nd thru n-1st xfersize (= MAXBSIZE) 7837d82f0f8SDina K Nimeh * | 1st xfersize (<= MAXBSIZE) 7847d82f0f8SDina K Nimeh * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter) 7857d82f0f8SDina K Nimeh * 7867d82f0f8SDina K Nimeh * Notes: "alignedoffset" is "offset" rounded down to nearest 7877d82f0f8SDina K Nimeh * MAXBSIZE boundary. "len" is next page boundary of size 788a363f650SDina K Nimeh * PAGESIZE after "alignedoffset". 7897c478bd9Sstevel@tonic-gate */ 7907c478bd9Sstevel@tonic-gate mapoffset = offset & MAXBOFFSET; 79187117650Saalok alignedoffset = offset - mapoffset; 7927c478bd9Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 7937c478bd9Sstevel@tonic-gate isread = bp->b_flags & B_READ; 7947c478bd9Sstevel@tonic-gate srw = isread ? S_READ : S_WRITE; 7957c478bd9Sstevel@tonic-gate do { 79687117650Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset, 7977c478bd9Sstevel@tonic-gate MIN(MAXBSIZE - mapoffset, bp->b_resid)); 798a363f650SDina K Nimeh len = roundup(mapoffset + xfersize, PAGESIZE); 7997c478bd9Sstevel@tonic-gate mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 8007c478bd9Sstevel@tonic-gate alignedoffset, MAXBSIZE, 1, srw); 8017c478bd9Sstevel@tonic-gate /* 8027c478bd9Sstevel@tonic-gate * Now fault in the pages. This lets us check 8037c478bd9Sstevel@tonic-gate * for errors before we reference mapaddr and 8047c478bd9Sstevel@tonic-gate * try to resolve the fault in bcopy (which would 8057c478bd9Sstevel@tonic-gate * panic instead). And this can easily happen, 8067c478bd9Sstevel@tonic-gate * particularly if you've lofi'd a file over NFS 8077c478bd9Sstevel@tonic-gate * and someone deletes the file on the server. 8087c478bd9Sstevel@tonic-gate */ 8097c478bd9Sstevel@tonic-gate error = segmap_fault(kas.a_hat, segkmap, mapaddr, 8107c478bd9Sstevel@tonic-gate len, F_SOFTLOCK, srw); 8117c478bd9Sstevel@tonic-gate if (error) { 8127c478bd9Sstevel@tonic-gate (void) segmap_release(segkmap, mapaddr, 0); 8137c478bd9Sstevel@tonic-gate if (FC_CODE(error) == FC_OBJERR) 8147c478bd9Sstevel@tonic-gate error = FC_ERRNO(error); 8157c478bd9Sstevel@tonic-gate else 8167c478bd9Sstevel@tonic-gate error = EIO; 8177c478bd9Sstevel@tonic-gate break; 8187c478bd9Sstevel@tonic-gate } 8197d82f0f8SDina K Nimeh /* error may be non-zero for encrypted lofi */ 8207d82f0f8SDina K Nimeh error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize, 8217d82f0f8SDina K Nimeh RDWR_BCOPY, mapaddr + mapoffset); 8227d82f0f8SDina K Nimeh if (error == 0) { 8237d82f0f8SDina K Nimeh bp->b_resid -= xfersize; 8247d82f0f8SDina K Nimeh bufaddr += xfersize; 8257d82f0f8SDina K Nimeh offset += xfersize; 8267d82f0f8SDina K Nimeh } 8277c478bd9Sstevel@tonic-gate smflags = 0; 8287c478bd9Sstevel@tonic-gate if (isread) { 82987117650Saalok smflags |= SM_FREE; 83087117650Saalok /* 83187117650Saalok * If we're reading an entire page starting 83287117650Saalok * at a page boundary, there's a good chance 83387117650Saalok * we won't need it again. Put it on the 83487117650Saalok * head of the freelist. 83587117650Saalok */ 836bbd65dd2SDina K Nimeh if (mapoffset == 0 && xfersize == MAXBSIZE) 83787117650Saalok smflags |= SM_DONTNEED; 8387c478bd9Sstevel@tonic-gate } else { 83944a1e32bSbatschul /* 84044a1e32bSbatschul * Write back good pages, it is okay to 84144a1e32bSbatschul * always release asynchronous here as we'll 84244a1e32bSbatschul * follow with VOP_FSYNC for B_SYNC buffers. 84344a1e32bSbatschul */ 84444a1e32bSbatschul if (error == 0) 84544a1e32bSbatschul smflags |= SM_WRITE | SM_ASYNC; 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 8487c478bd9Sstevel@tonic-gate len, F_SOFTUNLOCK, srw); 8497d82f0f8SDina K Nimeh save_error = segmap_release(segkmap, mapaddr, smflags); 8507d82f0f8SDina K Nimeh if (error == 0) 8517d82f0f8SDina K Nimeh error = save_error; 8527c478bd9Sstevel@tonic-gate /* only the first map may start partial */ 8537c478bd9Sstevel@tonic-gate mapoffset = 0; 8547c478bd9Sstevel@tonic-gate alignedoffset += MAXBSIZE; 8557c478bd9Sstevel@tonic-gate } while ((error == 0) && (bp->b_resid > 0) && 85687117650Saalok (offset < lsp->ls_vp_comp_size)); 85787117650Saalok 85887117650Saalok return (error); 85987117650Saalok } 86087117650Saalok 8614058a205Sjrgn.keil@googlemail.com /* 8624058a205Sjrgn.keil@googlemail.com * Check if segment seg_index is present in the decompressed segment 8634058a205Sjrgn.keil@googlemail.com * data cache. 8644058a205Sjrgn.keil@googlemail.com * 8654058a205Sjrgn.keil@googlemail.com * Returns a pointer to the decompressed segment data cache entry if 8664058a205Sjrgn.keil@googlemail.com * found, and NULL when decompressed data for this segment is not yet 8674058a205Sjrgn.keil@googlemail.com * cached. 8684058a205Sjrgn.keil@googlemail.com */ 8694058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache * 8704058a205Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index) 8714058a205Sjrgn.keil@googlemail.com { 8724058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 8734058a205Sjrgn.keil@googlemail.com 874*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock)); 8754058a205Sjrgn.keil@googlemail.com 8764058a205Sjrgn.keil@googlemail.com for (lc = list_head(&lsp->ls_comp_cache); lc != NULL; 8774058a205Sjrgn.keil@googlemail.com lc = list_next(&lsp->ls_comp_cache, lc)) { 8784058a205Sjrgn.keil@googlemail.com if (lc->lc_index == seg_index) { 8794058a205Sjrgn.keil@googlemail.com /* 8804058a205Sjrgn.keil@googlemail.com * Decompressed segment data was found in the 8814058a205Sjrgn.keil@googlemail.com * cache. 8824058a205Sjrgn.keil@googlemail.com * 8834058a205Sjrgn.keil@googlemail.com * The cache uses an LRU replacement strategy; 8844058a205Sjrgn.keil@googlemail.com * move the entry to head of list. 8854058a205Sjrgn.keil@googlemail.com */ 8864058a205Sjrgn.keil@googlemail.com list_remove(&lsp->ls_comp_cache, lc); 8874058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc); 8884058a205Sjrgn.keil@googlemail.com return (lc); 8894058a205Sjrgn.keil@googlemail.com } 8904058a205Sjrgn.keil@googlemail.com } 8914058a205Sjrgn.keil@googlemail.com return (NULL); 8924058a205Sjrgn.keil@googlemail.com } 8934058a205Sjrgn.keil@googlemail.com 8944058a205Sjrgn.keil@googlemail.com /* 8954058a205Sjrgn.keil@googlemail.com * Add the data for a decompressed segment at segment index 8964058a205Sjrgn.keil@googlemail.com * seg_index to the cache of the decompressed segments. 8974058a205Sjrgn.keil@googlemail.com * 8984058a205Sjrgn.keil@googlemail.com * Returns a pointer to the cache element structure in case 8994058a205Sjrgn.keil@googlemail.com * the data was added to the cache; returns NULL when the data 9004058a205Sjrgn.keil@googlemail.com * wasn't cached. 9014058a205Sjrgn.keil@googlemail.com */ 9024058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache * 9034058a205Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index, 9044058a205Sjrgn.keil@googlemail.com uchar_t *data) 9054058a205Sjrgn.keil@googlemail.com { 9064058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 9074058a205Sjrgn.keil@googlemail.com 908*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock)); 9094058a205Sjrgn.keil@googlemail.com 9104058a205Sjrgn.keil@googlemail.com while (lsp->ls_comp_cache_count > lofi_max_comp_cache) { 9114058a205Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache); 9124058a205Sjrgn.keil@googlemail.com ASSERT(lc != NULL); 9134058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz); 9144058a205Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache)); 9154058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--; 9164058a205Sjrgn.keil@googlemail.com } 9174058a205Sjrgn.keil@googlemail.com 9184058a205Sjrgn.keil@googlemail.com /* 9194058a205Sjrgn.keil@googlemail.com * Do not cache when disabled by tunable variable 9204058a205Sjrgn.keil@googlemail.com */ 9214058a205Sjrgn.keil@googlemail.com if (lofi_max_comp_cache == 0) 9224058a205Sjrgn.keil@googlemail.com return (NULL); 9234058a205Sjrgn.keil@googlemail.com 9244058a205Sjrgn.keil@googlemail.com /* 9254058a205Sjrgn.keil@googlemail.com * When the cache has not yet reached the maximum allowed 9264058a205Sjrgn.keil@googlemail.com * number of segments, allocate a new cache element. 9274058a205Sjrgn.keil@googlemail.com * Otherwise the cache is full; reuse the last list element 9284058a205Sjrgn.keil@googlemail.com * (LRU) for caching the decompressed segment data. 9294058a205Sjrgn.keil@googlemail.com * 9304058a205Sjrgn.keil@googlemail.com * The cache element for the new decompressed segment data is 9314058a205Sjrgn.keil@googlemail.com * added to the head of the list. 9324058a205Sjrgn.keil@googlemail.com */ 9334058a205Sjrgn.keil@googlemail.com if (lsp->ls_comp_cache_count < lofi_max_comp_cache) { 9344058a205Sjrgn.keil@googlemail.com lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP); 9354058a205Sjrgn.keil@googlemail.com lc->lc_data = NULL; 9364058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc); 9374058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count++; 9384058a205Sjrgn.keil@googlemail.com } else { 9394058a205Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache); 9404058a205Sjrgn.keil@googlemail.com if (lc == NULL) 9414058a205Sjrgn.keil@googlemail.com return (NULL); 9424058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc); 9434058a205Sjrgn.keil@googlemail.com } 9444058a205Sjrgn.keil@googlemail.com 9454058a205Sjrgn.keil@googlemail.com /* 9464058a205Sjrgn.keil@googlemail.com * Free old uncompressed segment data when reusing a cache 9474058a205Sjrgn.keil@googlemail.com * entry. 9484058a205Sjrgn.keil@googlemail.com */ 9494058a205Sjrgn.keil@googlemail.com if (lc->lc_data != NULL) 9504058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz); 9514058a205Sjrgn.keil@googlemail.com 9524058a205Sjrgn.keil@googlemail.com lc->lc_data = data; 9534058a205Sjrgn.keil@googlemail.com lc->lc_index = seg_index; 9544058a205Sjrgn.keil@googlemail.com return (lc); 9554058a205Sjrgn.keil@googlemail.com } 9564058a205Sjrgn.keil@googlemail.com 9574058a205Sjrgn.keil@googlemail.com 95887117650Saalok /*ARGSUSED*/ 959b1efbcd6SAlok Aggarwal static int 960b1efbcd6SAlok Aggarwal gzip_decompress(void *src, size_t srclen, void *dst, 96187117650Saalok size_t *dstlen, int level) 96287117650Saalok { 96387117650Saalok ASSERT(*dstlen >= srclen); 96487117650Saalok 96587117650Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 96687117650Saalok return (-1); 96787117650Saalok return (0); 96887117650Saalok } 96987117650Saalok 970b1efbcd6SAlok Aggarwal #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8) 971b1efbcd6SAlok Aggarwal /*ARGSUSED*/ 972b1efbcd6SAlok Aggarwal static int 973b1efbcd6SAlok Aggarwal lzma_decompress(void *src, size_t srclen, void *dst, 974b1efbcd6SAlok Aggarwal size_t *dstlen, int level) 975b1efbcd6SAlok Aggarwal { 976b1efbcd6SAlok Aggarwal size_t insizepure; 977b1efbcd6SAlok Aggarwal void *actual_src; 978b1efbcd6SAlok Aggarwal ELzmaStatus status; 979b1efbcd6SAlok Aggarwal 980b1efbcd6SAlok Aggarwal insizepure = srclen - LZMA_HEADER_SIZE; 981b1efbcd6SAlok Aggarwal actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE); 982b1efbcd6SAlok Aggarwal 983b1efbcd6SAlok Aggarwal if (LzmaDecode((Byte *)dst, (size_t *)dstlen, 984b1efbcd6SAlok Aggarwal (const Byte *)actual_src, &insizepure, 985b1efbcd6SAlok Aggarwal (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, 986b1efbcd6SAlok Aggarwal &g_Alloc) != SZ_OK) { 987b1efbcd6SAlok Aggarwal return (-1); 988b1efbcd6SAlok Aggarwal } 989b1efbcd6SAlok Aggarwal return (0); 990b1efbcd6SAlok Aggarwal } 991b1efbcd6SAlok Aggarwal 99287117650Saalok /* 99387117650Saalok * This is basically what strategy used to be before we found we 99487117650Saalok * needed task queues. 99587117650Saalok */ 99687117650Saalok static void 99787117650Saalok lofi_strategy_task(void *arg) 99887117650Saalok { 99987117650Saalok struct buf *bp = (struct buf *)arg; 100087117650Saalok int error; 100144a1e32bSbatschul int syncflag = 0; 100287117650Saalok struct lofi_state *lsp; 10037d82f0f8SDina K Nimeh offset_t offset; 100487117650Saalok caddr_t bufaddr; 10057d82f0f8SDina K Nimeh size_t len; 10067d82f0f8SDina K Nimeh size_t xfersize; 10077d82f0f8SDina K Nimeh boolean_t bufinited = B_FALSE; 100887117650Saalok 100987117650Saalok lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 10107d82f0f8SDina K Nimeh if (lsp == NULL) { 10117d82f0f8SDina K Nimeh error = ENXIO; 10127d82f0f8SDina K Nimeh goto errout; 10137d82f0f8SDina K Nimeh } 101487117650Saalok if (lsp->ls_kstat) { 101587117650Saalok mutex_enter(lsp->ls_kstat->ks_lock); 101687117650Saalok kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 101787117650Saalok mutex_exit(lsp->ls_kstat->ks_lock); 101887117650Saalok } 101987117650Saalok bp_mapin(bp); 102087117650Saalok bufaddr = bp->b_un.b_addr; 102187117650Saalok offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 10227d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 10237d82f0f8SDina K Nimeh /* encrypted data really begins after crypto header */ 10247d82f0f8SDina K Nimeh offset += lsp->ls_crypto_offset; 10257d82f0f8SDina K Nimeh } 10267d82f0f8SDina K Nimeh len = bp->b_bcount; 10277d82f0f8SDina K Nimeh bufinited = B_TRUE; 10287d82f0f8SDina K Nimeh 10297d82f0f8SDina K Nimeh if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 10307d82f0f8SDina K Nimeh error = EIO; 10317d82f0f8SDina K Nimeh goto errout; 10327d82f0f8SDina K Nimeh } 103387117650Saalok 103487117650Saalok /* 103544a1e32bSbatschul * If we're writing and the buffer was not B_ASYNC 103644a1e32bSbatschul * we'll follow up with a VOP_FSYNC() to force any 103744a1e32bSbatschul * asynchronous I/O to stable storage. 103844a1e32bSbatschul */ 103944a1e32bSbatschul if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC)) 104044a1e32bSbatschul syncflag = FSYNC; 104144a1e32bSbatschul 104244a1e32bSbatschul /* 104387117650Saalok * We used to always use vn_rdwr here, but we cannot do that because 104487117650Saalok * we might decide to read or write from the the underlying 104587117650Saalok * file during this call, which would be a deadlock because 104687117650Saalok * we have the rw_lock. So instead we page, unless it's not 10477d82f0f8SDina K Nimeh * mapable or it's a character device or it's an encrypted lofi. 104887117650Saalok */ 10497d82f0f8SDina K Nimeh if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) || 10507d82f0f8SDina K Nimeh lsp->ls_crypto_enabled) { 10517d82f0f8SDina K Nimeh error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW, 10527d82f0f8SDina K Nimeh NULL); 10537d82f0f8SDina K Nimeh } else if (lsp->ls_uncomp_seg_sz == 0) { 105487117650Saalok error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 10557d82f0f8SDina K Nimeh } else { 10564058a205Sjrgn.keil@googlemail.com uchar_t *compressed_seg = NULL, *cmpbuf; 10574058a205Sjrgn.keil@googlemail.com uchar_t *uncompressed_seg = NULL; 10587d82f0f8SDina K Nimeh lofi_compress_info_t *li; 10597d82f0f8SDina K Nimeh size_t oblkcount; 10604058a205Sjrgn.keil@googlemail.com ulong_t seglen; 10617d82f0f8SDina K Nimeh uint64_t sblkno, eblkno, cmpbytes; 10624058a205Sjrgn.keil@googlemail.com uint64_t uncompressed_seg_index; 10634058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 10647d82f0f8SDina K Nimeh offset_t sblkoff, eblkoff; 10657d82f0f8SDina K Nimeh u_offset_t salign, ealign; 10667d82f0f8SDina K Nimeh u_offset_t sdiff; 10677d82f0f8SDina K Nimeh uint32_t comp_data_sz; 10687d82f0f8SDina K Nimeh uint64_t i; 1069b9c7fb03SAlok Aggarwal int j; 107087117650Saalok 107187117650Saalok /* 107287117650Saalok * From here on we're dealing primarily with compressed files 107387117650Saalok */ 10747d82f0f8SDina K Nimeh ASSERT(!lsp->ls_crypto_enabled); 107587117650Saalok 107687117650Saalok /* 107787117650Saalok * Compressed files can only be read from and 107887117650Saalok * not written to 107987117650Saalok */ 108087117650Saalok if (!(bp->b_flags & B_READ)) { 108187117650Saalok bp->b_resid = bp->b_bcount; 108287117650Saalok error = EROFS; 108387117650Saalok goto done; 108487117650Saalok } 108587117650Saalok 108687117650Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0); 108787117650Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 108887117650Saalok /* 108987117650Saalok * Compute starting and ending compressed segment numbers 109087117650Saalok * We use only bitwise operations avoiding division and 109187117650Saalok * modulus because we enforce the compression segment size 109287117650Saalok * to a power of 2 109387117650Saalok */ 109487117650Saalok sblkno = offset >> lsp->ls_comp_seg_shift; 109587117650Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 109687117650Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 109787117650Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 109887117650Saalok 109987117650Saalok /* 11004058a205Sjrgn.keil@googlemail.com * Check the decompressed segment cache. 11014058a205Sjrgn.keil@googlemail.com * 11024058a205Sjrgn.keil@googlemail.com * The cache is used only when the requested data 11034058a205Sjrgn.keil@googlemail.com * is within a segment. Requests that cross 11044058a205Sjrgn.keil@googlemail.com * segment boundaries bypass the cache. 11054058a205Sjrgn.keil@googlemail.com */ 11064058a205Sjrgn.keil@googlemail.com if (sblkno == eblkno || 11074058a205Sjrgn.keil@googlemail.com (sblkno + 1 == eblkno && eblkoff == 0)) { 11084058a205Sjrgn.keil@googlemail.com /* 11094058a205Sjrgn.keil@googlemail.com * Request doesn't cross a segment boundary, 11104058a205Sjrgn.keil@googlemail.com * now check the cache. 11114058a205Sjrgn.keil@googlemail.com */ 11124058a205Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock); 11134058a205Sjrgn.keil@googlemail.com lc = lofi_find_comp_data(lsp, sblkno); 11144058a205Sjrgn.keil@googlemail.com if (lc != NULL) { 11154058a205Sjrgn.keil@googlemail.com /* 11164058a205Sjrgn.keil@googlemail.com * We've found the decompressed segment 11174058a205Sjrgn.keil@googlemail.com * data in the cache; reuse it. 11184058a205Sjrgn.keil@googlemail.com */ 11194058a205Sjrgn.keil@googlemail.com bcopy(lc->lc_data + sblkoff, bufaddr, 11204058a205Sjrgn.keil@googlemail.com bp->b_bcount); 11214058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock); 11224058a205Sjrgn.keil@googlemail.com bp->b_resid = 0; 11234058a205Sjrgn.keil@googlemail.com error = 0; 11244058a205Sjrgn.keil@googlemail.com goto done; 11254058a205Sjrgn.keil@googlemail.com } 11264058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock); 11274058a205Sjrgn.keil@googlemail.com } 11284058a205Sjrgn.keil@googlemail.com 11294058a205Sjrgn.keil@googlemail.com /* 113087117650Saalok * Align start offset to block boundary for segmap 113187117650Saalok */ 113287117650Saalok salign = lsp->ls_comp_seg_index[sblkno]; 113387117650Saalok sdiff = salign & (DEV_BSIZE - 1); 113487117650Saalok salign -= sdiff; 113587117650Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 113687117650Saalok /* 113787117650Saalok * We're dealing with the last segment of 113887117650Saalok * the compressed file -- the size of this 113987117650Saalok * segment *may not* be the same as the 114087117650Saalok * segment size for the file 114187117650Saalok */ 114287117650Saalok eblkoff = (offset + bp->b_bcount) & 114387117650Saalok (lsp->ls_uncomp_last_seg_sz - 1); 114487117650Saalok ealign = lsp->ls_vp_comp_size; 114587117650Saalok } else { 114687117650Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1]; 114787117650Saalok } 114887117650Saalok 114987117650Saalok /* 115087117650Saalok * Preserve original request paramaters 115187117650Saalok */ 115287117650Saalok oblkcount = bp->b_bcount; 115387117650Saalok 115487117650Saalok /* 115587117650Saalok * Assign the calculated parameters 115687117650Saalok */ 115787117650Saalok comp_data_sz = ealign - salign; 115887117650Saalok bp->b_bcount = comp_data_sz; 115987117650Saalok 116087117650Saalok /* 1161b9c7fb03SAlok Aggarwal * Buffers to hold compressed segments are pre-allocated 1162b9c7fb03SAlok Aggarwal * on a per-thread basis. Find a pre-allocated buffer 1163b9c7fb03SAlok Aggarwal * that is not currently in use and mark it for use. 116487117650Saalok */ 1165b9c7fb03SAlok Aggarwal mutex_enter(&lsp->ls_comp_bufs_lock); 1166b9c7fb03SAlok Aggarwal for (j = 0; j < lofi_taskq_nthreads; j++) { 1167b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[j].inuse == 0) { 1168b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].inuse = 1; 1169b9c7fb03SAlok Aggarwal break; 1170b9c7fb03SAlok Aggarwal } 1171b9c7fb03SAlok Aggarwal } 1172b9c7fb03SAlok Aggarwal 1173b9c7fb03SAlok Aggarwal mutex_exit(&lsp->ls_comp_bufs_lock); 1174b9c7fb03SAlok Aggarwal ASSERT(j < lofi_taskq_nthreads); 1175b9c7fb03SAlok Aggarwal 1176b9c7fb03SAlok Aggarwal /* 1177b9c7fb03SAlok Aggarwal * If the pre-allocated buffer size does not match 1178b9c7fb03SAlok Aggarwal * the size of the I/O request, re-allocate it with 1179b9c7fb03SAlok Aggarwal * the appropriate size 1180b9c7fb03SAlok Aggarwal */ 1181b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) { 1182b9c7fb03SAlok Aggarwal if (lsp->ls_comp_bufs[j].bufsize > 0) 1183b9c7fb03SAlok Aggarwal kmem_free(lsp->ls_comp_bufs[j].buf, 1184b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].bufsize); 1185b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount, 1186b9c7fb03SAlok Aggarwal KM_SLEEP); 1187b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].bufsize = bp->b_bcount; 1188b9c7fb03SAlok Aggarwal } 1189b9c7fb03SAlok Aggarwal compressed_seg = lsp->ls_comp_bufs[j].buf; 1190b9c7fb03SAlok Aggarwal 119187117650Saalok /* 119287117650Saalok * Map in the calculated number of blocks 119387117650Saalok */ 119487117650Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 119587117650Saalok bp, lsp); 119687117650Saalok 119787117650Saalok bp->b_bcount = oblkcount; 119887117650Saalok bp->b_resid = oblkcount; 119987117650Saalok if (error != 0) 120087117650Saalok goto done; 120187117650Saalok 120287117650Saalok /* 1203b9c7fb03SAlok Aggarwal * decompress compressed blocks start 120487117650Saalok */ 120587117650Saalok cmpbuf = compressed_seg + sdiff; 1206b1efbcd6SAlok Aggarwal for (i = sblkno; i <= eblkno; i++) { 1207b1efbcd6SAlok Aggarwal ASSERT(i < lsp->ls_comp_index_sz - 1); 1208b9c7fb03SAlok Aggarwal uchar_t *useg; 1209b1efbcd6SAlok Aggarwal 1210b1efbcd6SAlok Aggarwal /* 1211b1efbcd6SAlok Aggarwal * The last segment is special in that it is 1212b1efbcd6SAlok Aggarwal * most likely not going to be the same 1213b1efbcd6SAlok Aggarwal * (uncompressed) size as the other segments. 1214b1efbcd6SAlok Aggarwal */ 1215b1efbcd6SAlok Aggarwal if (i == (lsp->ls_comp_index_sz - 2)) { 1216b1efbcd6SAlok Aggarwal seglen = lsp->ls_uncomp_last_seg_sz; 1217b1efbcd6SAlok Aggarwal } else { 1218b1efbcd6SAlok Aggarwal seglen = lsp->ls_uncomp_seg_sz; 1219b1efbcd6SAlok Aggarwal } 1220b1efbcd6SAlok Aggarwal 122187117650Saalok /* 122287117650Saalok * Each of the segment index entries contains 122387117650Saalok * the starting block number for that segment. 122487117650Saalok * The number of compressed bytes in a segment 122587117650Saalok * is thus the difference between the starting 122687117650Saalok * block number of this segment and the starting 122787117650Saalok * block number of the next segment. 122887117650Saalok */ 122987117650Saalok cmpbytes = lsp->ls_comp_seg_index[i + 1] - 123087117650Saalok lsp->ls_comp_seg_index[i]; 123187117650Saalok 123287117650Saalok /* 123387117650Saalok * The first byte in a compressed segment is a flag 123487117650Saalok * that indicates whether this segment is compressed 1235b9c7fb03SAlok Aggarwal * at all. 1236b9c7fb03SAlok Aggarwal * 1237b9c7fb03SAlok Aggarwal * The variable 'useg' is used (instead of 1238b9c7fb03SAlok Aggarwal * uncompressed_seg) in this loop to keep a 1239b9c7fb03SAlok Aggarwal * reference to the uncompressed segment. 1240b9c7fb03SAlok Aggarwal * 1241b9c7fb03SAlok Aggarwal * N.B. If 'useg' is replaced with uncompressed_seg, 1242b9c7fb03SAlok Aggarwal * it leads to memory leaks and heap corruption in 1243b9c7fb03SAlok Aggarwal * corner cases where compressed segments lie 1244b9c7fb03SAlok Aggarwal * adjacent to uncompressed segments. 124587117650Saalok */ 124687117650Saalok if (*cmpbuf == UNCOMPRESSED) { 1247b9c7fb03SAlok Aggarwal useg = cmpbuf + SEGHDR; 124887117650Saalok } else { 1249b9c7fb03SAlok Aggarwal if (uncompressed_seg == NULL) 1250b9c7fb03SAlok Aggarwal uncompressed_seg = 1251b9c7fb03SAlok Aggarwal kmem_alloc(lsp->ls_uncomp_seg_sz, 1252b9c7fb03SAlok Aggarwal KM_SLEEP); 1253b9c7fb03SAlok Aggarwal useg = uncompressed_seg; 1254b9c7fb03SAlok Aggarwal uncompressed_seg_index = i; 1255b9c7fb03SAlok Aggarwal 125687117650Saalok if (li->l_decompress((cmpbuf + SEGHDR), 125787117650Saalok (cmpbytes - SEGHDR), uncompressed_seg, 125887117650Saalok &seglen, li->l_level) != 0) { 125987117650Saalok error = EIO; 126087117650Saalok goto done; 126187117650Saalok } 126287117650Saalok } 126387117650Saalok 126487117650Saalok /* 126587117650Saalok * Determine how much uncompressed data we 126687117650Saalok * have to copy and copy it 126787117650Saalok */ 126887117650Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 1269b1efbcd6SAlok Aggarwal if (i == eblkno) 1270b1efbcd6SAlok Aggarwal xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff); 127187117650Saalok 1272b9c7fb03SAlok Aggarwal bcopy((useg + sblkoff), bufaddr, xfersize); 127387117650Saalok 127487117650Saalok cmpbuf += cmpbytes; 127587117650Saalok bufaddr += xfersize; 127687117650Saalok bp->b_resid -= xfersize; 127787117650Saalok sblkoff = 0; 127887117650Saalok 127987117650Saalok if (bp->b_resid == 0) 128087117650Saalok break; 1281b9c7fb03SAlok Aggarwal } /* decompress compressed blocks ends */ 12824058a205Sjrgn.keil@googlemail.com 12834058a205Sjrgn.keil@googlemail.com /* 1284b9c7fb03SAlok Aggarwal * Skip to done if there is no uncompressed data to cache 1285b9c7fb03SAlok Aggarwal */ 1286b9c7fb03SAlok Aggarwal if (uncompressed_seg == NULL) 1287b9c7fb03SAlok Aggarwal goto done; 1288b9c7fb03SAlok Aggarwal 1289b9c7fb03SAlok Aggarwal /* 1290b9c7fb03SAlok Aggarwal * Add the data for the last decompressed segment to 12914058a205Sjrgn.keil@googlemail.com * the cache. 12924058a205Sjrgn.keil@googlemail.com * 12934058a205Sjrgn.keil@googlemail.com * In case the uncompressed segment data was added to (and 12944058a205Sjrgn.keil@googlemail.com * is referenced by) the cache, make sure we don't free it 12954058a205Sjrgn.keil@googlemail.com * here. 12964058a205Sjrgn.keil@googlemail.com */ 12974058a205Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock); 12984058a205Sjrgn.keil@googlemail.com if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index, 12994058a205Sjrgn.keil@googlemail.com uncompressed_seg)) != NULL) { 13004058a205Sjrgn.keil@googlemail.com uncompressed_seg = NULL; 13014058a205Sjrgn.keil@googlemail.com } 13024058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock); 13034058a205Sjrgn.keil@googlemail.com 130487117650Saalok done: 1305b9c7fb03SAlok Aggarwal if (compressed_seg != NULL) { 1306b9c7fb03SAlok Aggarwal mutex_enter(&lsp->ls_comp_bufs_lock); 1307b9c7fb03SAlok Aggarwal lsp->ls_comp_bufs[j].inuse = 0; 1308b9c7fb03SAlok Aggarwal mutex_exit(&lsp->ls_comp_bufs_lock); 1309b9c7fb03SAlok Aggarwal } 131087117650Saalok if (uncompressed_seg != NULL) 131187117650Saalok kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 13127d82f0f8SDina K Nimeh } /* end of handling compressed files */ 131387117650Saalok 131444a1e32bSbatschul if ((error == 0) && (syncflag != 0)) 131544a1e32bSbatschul error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL); 131644a1e32bSbatschul 13177d82f0f8SDina K Nimeh errout: 13187d82f0f8SDina K Nimeh if (bufinited && lsp->ls_kstat) { 13197c478bd9Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid; 13207c478bd9Sstevel@tonic-gate kstat_io_t *kioptr; 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 13237c478bd9Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 13247c478bd9Sstevel@tonic-gate if (bp->b_flags & B_READ) { 13257c478bd9Sstevel@tonic-gate kioptr->nread += n_done; 13267c478bd9Sstevel@tonic-gate kioptr->reads++; 13277c478bd9Sstevel@tonic-gate } else { 13287c478bd9Sstevel@tonic-gate kioptr->nwritten += n_done; 13297c478bd9Sstevel@tonic-gate kioptr->writes++; 13307c478bd9Sstevel@tonic-gate } 13317c478bd9Sstevel@tonic-gate kstat_runq_exit(kioptr); 13327c478bd9Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 13337c478bd9Sstevel@tonic-gate } 13343d7072f8Seschrock 13353d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 13363d7072f8Seschrock if (--lsp->ls_vp_iocount == 0) 13373d7072f8Seschrock cv_broadcast(&lsp->ls_vp_cv); 13383d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13393d7072f8Seschrock 13407c478bd9Sstevel@tonic-gate bioerror(bp, error); 13417c478bd9Sstevel@tonic-gate biodone(bp); 13427c478bd9Sstevel@tonic-gate } 13437c478bd9Sstevel@tonic-gate 13447c478bd9Sstevel@tonic-gate static int 13457c478bd9Sstevel@tonic-gate lofi_strategy(struct buf *bp) 13467c478bd9Sstevel@tonic-gate { 13477c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 13487c478bd9Sstevel@tonic-gate offset_t offset; 13497c478bd9Sstevel@tonic-gate 13507c478bd9Sstevel@tonic-gate /* 13517c478bd9Sstevel@tonic-gate * We cannot just do I/O here, because the current thread 13527c478bd9Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem 13537c478bd9Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and 13547c478bd9Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer. 13557c478bd9Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below. 13567c478bd9Sstevel@tonic-gate * 13577c478bd9Sstevel@tonic-gate * We could come up with a complex strategy using threads to 13587c478bd9Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task 13597c478bd9Sstevel@tonic-gate * queues were incredibly easy so they win. 13607c478bd9Sstevel@tonic-gate */ 13617c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 13627d82f0f8SDina K Nimeh if (lsp == NULL) { 13637d82f0f8SDina K Nimeh bioerror(bp, ENXIO); 13647d82f0f8SDina K Nimeh biodone(bp); 13657d82f0f8SDina K Nimeh return (0); 13667d82f0f8SDina K Nimeh } 13677d82f0f8SDina K Nimeh 13683d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 13693d7072f8Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 13703d7072f8Seschrock bioerror(bp, EIO); 13713d7072f8Seschrock biodone(bp); 13723d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13733d7072f8Seschrock return (0); 13743d7072f8Seschrock } 13753d7072f8Seschrock 13767c478bd9Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 13777d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 13787d82f0f8SDina K Nimeh /* encrypted data really begins after crypto header */ 13797d82f0f8SDina K Nimeh offset += lsp->ls_crypto_offset; 13807d82f0f8SDina K Nimeh } 13817c478bd9Sstevel@tonic-gate if (offset == lsp->ls_vp_size) { 13827c478bd9Sstevel@tonic-gate /* EOF */ 13837c478bd9Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) { 13847c478bd9Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 13857c478bd9Sstevel@tonic-gate bioerror(bp, 0); 13867c478bd9Sstevel@tonic-gate } else { 13877c478bd9Sstevel@tonic-gate /* writes should fail */ 13887c478bd9Sstevel@tonic-gate bioerror(bp, ENXIO); 13897c478bd9Sstevel@tonic-gate } 13907c478bd9Sstevel@tonic-gate biodone(bp); 13913d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13927c478bd9Sstevel@tonic-gate return (0); 13937c478bd9Sstevel@tonic-gate } 13947c478bd9Sstevel@tonic-gate if (offset > lsp->ls_vp_size) { 13957c478bd9Sstevel@tonic-gate bioerror(bp, ENXIO); 13967c478bd9Sstevel@tonic-gate biodone(bp); 13973d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13987c478bd9Sstevel@tonic-gate return (0); 13997c478bd9Sstevel@tonic-gate } 14003d7072f8Seschrock lsp->ls_vp_iocount++; 14013d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 14023d7072f8Seschrock 14037c478bd9Sstevel@tonic-gate if (lsp->ls_kstat) { 14047c478bd9Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 14057c478bd9Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 14067c478bd9Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 14077c478bd9Sstevel@tonic-gate } 14087c478bd9Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 14097c478bd9Sstevel@tonic-gate return (0); 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 14137c478bd9Sstevel@tonic-gate static int 14147c478bd9Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 14157c478bd9Sstevel@tonic-gate { 14167c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 14177c478bd9Sstevel@tonic-gate return (EINVAL); 14187d82f0f8SDina K Nimeh UIO_CHECK(uio); 14197c478bd9Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 14237c478bd9Sstevel@tonic-gate static int 14247c478bd9Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 14257c478bd9Sstevel@tonic-gate { 14267c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 14277c478bd9Sstevel@tonic-gate return (EINVAL); 14287d82f0f8SDina K Nimeh UIO_CHECK(uio); 14297c478bd9Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 14307c478bd9Sstevel@tonic-gate } 14317c478bd9Sstevel@tonic-gate 14327c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 14337c478bd9Sstevel@tonic-gate static int 14347c478bd9Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 14357c478bd9Sstevel@tonic-gate { 14367c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 14377c478bd9Sstevel@tonic-gate return (EINVAL); 14387d82f0f8SDina K Nimeh UIO_CHECK(aio->aio_uio); 14397c478bd9Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 14407c478bd9Sstevel@tonic-gate } 14417c478bd9Sstevel@tonic-gate 14427c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 14437c478bd9Sstevel@tonic-gate static int 14447c478bd9Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 14457c478bd9Sstevel@tonic-gate { 14467c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 14477c478bd9Sstevel@tonic-gate return (EINVAL); 14487d82f0f8SDina K Nimeh UIO_CHECK(aio->aio_uio); 14497c478bd9Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 14507c478bd9Sstevel@tonic-gate } 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 14537c478bd9Sstevel@tonic-gate static int 14547c478bd9Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 14557c478bd9Sstevel@tonic-gate { 14567c478bd9Sstevel@tonic-gate switch (infocmd) { 14577c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 14587c478bd9Sstevel@tonic-gate *result = lofi_dip; 14597c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 14607c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 14617c478bd9Sstevel@tonic-gate *result = 0; 14627c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate static int 14687c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 14697c478bd9Sstevel@tonic-gate { 14707c478bd9Sstevel@tonic-gate int error; 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 14737c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1474*0fbb751dSJohn Levon 1475*0fbb751dSJohn Levon lofi_minor_id = id_space_create("lofi_minor_id", 1, L_MAXMIN32 + 1); 1476*0fbb751dSJohn Levon 1477*0fbb751dSJohn Levon if (!lofi_minor_id) 1478*0fbb751dSJohn Levon return (DDI_FAILURE); 1479*0fbb751dSJohn Levon 14807c478bd9Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0); 14817c478bd9Sstevel@tonic-gate if (error == DDI_FAILURE) { 1482*0fbb751dSJohn Levon id_space_destroy(lofi_minor_id); 14837c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 14847c478bd9Sstevel@tonic-gate } 14857c478bd9Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 14867c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL); 14877c478bd9Sstevel@tonic-gate if (error == DDI_FAILURE) { 14887c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 1489*0fbb751dSJohn Levon id_space_destroy(lofi_minor_id); 14907c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 14917c478bd9Sstevel@tonic-gate } 1492843e1988Sjohnlev /* driver handles kernel-issued IOCTLs */ 1493843e1988Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 1494843e1988Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 1495843e1988Sjohnlev ddi_remove_minor_node(dip, NULL); 1496843e1988Sjohnlev ddi_soft_state_free(lofi_statep, 0); 1497*0fbb751dSJohn Levon id_space_destroy(lofi_minor_id); 1498843e1988Sjohnlev return (DDI_FAILURE); 1499843e1988Sjohnlev } 1500*0fbb751dSJohn Levon 1501*0fbb751dSJohn Levon zone_key_create(&lofi_zone_key, NULL, lofi_zone_shutdown, NULL); 1502*0fbb751dSJohn Levon 15037c478bd9Sstevel@tonic-gate lofi_dip = dip; 15047c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 15057c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 15067c478bd9Sstevel@tonic-gate } 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate static int 15097c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 15107c478bd9Sstevel@tonic-gate { 15117c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 15127c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1513*0fbb751dSJohn Levon 1514*0fbb751dSJohn Levon mutex_enter(&lofi_lock); 1515*0fbb751dSJohn Levon 1516*0fbb751dSJohn Levon if (!list_is_empty(&lofi_list)) { 1517*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 15187c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1519*0fbb751dSJohn Levon } 1520*0fbb751dSJohn Levon 15217c478bd9Sstevel@tonic-gate lofi_dip = NULL; 15227c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1523843e1988Sjohnlev ddi_prop_remove_all(dip); 1524*0fbb751dSJohn Levon 1525*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 1526*0fbb751dSJohn Levon 1527*0fbb751dSJohn Levon if (zone_key_delete(lofi_zone_key) != 0) 1528*0fbb751dSJohn Levon cmn_err(CE_WARN, "failed to delete zone key"); 1529*0fbb751dSJohn Levon 15307c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 1531*0fbb751dSJohn Levon 1532*0fbb751dSJohn Levon id_space_destroy(lofi_minor_id); 1533*0fbb751dSJohn Levon 15347c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 15357c478bd9Sstevel@tonic-gate } 15367c478bd9Sstevel@tonic-gate 15377c478bd9Sstevel@tonic-gate /* 15387d82f0f8SDina K Nimeh * With addition of encryption, be careful that encryption key is wiped before 15397d82f0f8SDina K Nimeh * kernel memory structures are freed, and also that key is not accidentally 15407d82f0f8SDina K Nimeh * passed out into userland structures. 15417d82f0f8SDina K Nimeh */ 15427d82f0f8SDina K Nimeh static void 15437d82f0f8SDina K Nimeh free_lofi_ioctl(struct lofi_ioctl *klip) 15447d82f0f8SDina K Nimeh { 15457d82f0f8SDina K Nimeh /* Make sure this encryption key doesn't stick around */ 15467d82f0f8SDina K Nimeh bzero(klip->li_key, sizeof (klip->li_key)); 15477d82f0f8SDina K Nimeh kmem_free(klip, sizeof (struct lofi_ioctl)); 15487d82f0f8SDina K Nimeh } 15497d82f0f8SDina K Nimeh 15507d82f0f8SDina K Nimeh /* 15517c478bd9Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out 15527c478bd9Sstevel@tonic-gate * the lofi_ioctl structure. 15537c478bd9Sstevel@tonic-gate */ 1554*0fbb751dSJohn Levon int 1555*0fbb751dSJohn Levon copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, struct lofi_ioctl **klipp, 1556*0fbb751dSJohn Levon int flag) 15577c478bd9Sstevel@tonic-gate { 15587c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 15597c478bd9Sstevel@tonic-gate int error; 15607c478bd9Sstevel@tonic-gate 1561*0fbb751dSJohn Levon klip = *klipp = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 1562bd07e074Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 1563*0fbb751dSJohn Levon if (error) 1564*0fbb751dSJohn Levon goto err; 15657c478bd9Sstevel@tonic-gate 1566*0fbb751dSJohn Levon /* ensure NULL termination */ 15676f02aa44SDina K Nimeh klip->li_filename[MAXPATHLEN-1] = '\0'; 1568*0fbb751dSJohn Levon klip->li_algorithm[MAXALGLEN-1] = '\0'; 1569*0fbb751dSJohn Levon klip->li_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0'; 1570*0fbb751dSJohn Levon klip->li_iv_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0'; 15717c478bd9Sstevel@tonic-gate 1572*0fbb751dSJohn Levon if (klip->li_minor > L_MAXMIN32) { 1573*0fbb751dSJohn Levon error = EINVAL; 1574*0fbb751dSJohn Levon goto err; 15757c478bd9Sstevel@tonic-gate } 1576*0fbb751dSJohn Levon 1577*0fbb751dSJohn Levon return (0); 1578*0fbb751dSJohn Levon 1579*0fbb751dSJohn Levon err: 1580*0fbb751dSJohn Levon free_lofi_ioctl(klip); 1581*0fbb751dSJohn Levon return (error); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate int 1585bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 1586bd07e074Sheppo int flag) 15877c478bd9Sstevel@tonic-gate { 15887c478bd9Sstevel@tonic-gate int error; 15897c478bd9Sstevel@tonic-gate 15907d82f0f8SDina K Nimeh /* 15917d82f0f8SDina K Nimeh * NOTE: Do NOT copy the crypto_key_t "back" to userland. 15927d82f0f8SDina K Nimeh * This ensures that an attacker can't trivially find the 15937d82f0f8SDina K Nimeh * key for a mapping just by issuing the ioctl. 15947d82f0f8SDina K Nimeh * 15957d82f0f8SDina K Nimeh * It can still be found by poking around in kmem with mdb(1), 15967d82f0f8SDina K Nimeh * but there is no point in making it easy when the info isn't 15977d82f0f8SDina K Nimeh * of any use in this direction anyway. 15987d82f0f8SDina K Nimeh * 15997d82f0f8SDina K Nimeh * Either way we don't actually have the raw key stored in 16007d82f0f8SDina K Nimeh * a form that we can get it anyway, since we just used it 16017d82f0f8SDina K Nimeh * to create a ctx template and didn't keep "the original". 16027d82f0f8SDina K Nimeh */ 1603bd07e074Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 16047c478bd9Sstevel@tonic-gate if (error) 16057c478bd9Sstevel@tonic-gate return (EFAULT); 16067c478bd9Sstevel@tonic-gate return (0); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate 1609*0fbb751dSJohn Levon static int 1610*0fbb751dSJohn Levon lofi_access(struct lofi_state *lsp) 1611*0fbb751dSJohn Levon { 1612*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 1613*0fbb751dSJohn Levon if (INGLOBALZONE(curproc) || lsp->ls_zone == curproc->p_zone) 1614*0fbb751dSJohn Levon return (0); 1615*0fbb751dSJohn Levon return (EPERM); 1616*0fbb751dSJohn Levon } 1617*0fbb751dSJohn Levon 16187c478bd9Sstevel@tonic-gate /* 1619*0fbb751dSJohn Levon * Find the lofi state for the given filename. We compare by vnode to 1620*0fbb751dSJohn Levon * allow the global zone visibility into NGZ lofi nodes. 16217c478bd9Sstevel@tonic-gate */ 16227c478bd9Sstevel@tonic-gate static int 1623*0fbb751dSJohn Levon file_to_lofi_nocheck(char *filename, struct lofi_state **lspp) 16247c478bd9Sstevel@tonic-gate { 16257c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 1626*0fbb751dSJohn Levon vnode_t *vp = NULL; 1627*0fbb751dSJohn Levon int err = 0; 16287c478bd9Sstevel@tonic-gate 1629*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 1630*0fbb751dSJohn Levon 1631*0fbb751dSJohn Levon if ((err = lookupname(filename, UIO_SYSSPACE, FOLLOW, 1632*0fbb751dSJohn Levon NULLVPP, &vp)) != 0) 1633*0fbb751dSJohn Levon goto out; 1634*0fbb751dSJohn Levon 1635*0fbb751dSJohn Levon if (vp->v_type == VREG) { 1636*0fbb751dSJohn Levon vnode_t *realvp; 1637*0fbb751dSJohn Levon if (VOP_REALVP(vp, &realvp, NULL) == 0) { 1638*0fbb751dSJohn Levon VN_HOLD(realvp); 1639*0fbb751dSJohn Levon VN_RELE(vp); 1640*0fbb751dSJohn Levon vp = realvp; 16417c478bd9Sstevel@tonic-gate } 1642*0fbb751dSJohn Levon } 1643*0fbb751dSJohn Levon 1644*0fbb751dSJohn Levon for (lsp = list_head(&lofi_list); lsp != NULL; 1645*0fbb751dSJohn Levon lsp = list_next(&lofi_list, lsp)) { 1646*0fbb751dSJohn Levon if (lsp->ls_vp == vp) { 1647*0fbb751dSJohn Levon if (lspp != NULL) 1648*0fbb751dSJohn Levon *lspp = lsp; 1649*0fbb751dSJohn Levon goto out; 1650*0fbb751dSJohn Levon } 1651*0fbb751dSJohn Levon } 1652*0fbb751dSJohn Levon 1653*0fbb751dSJohn Levon err = ENOENT; 1654*0fbb751dSJohn Levon 1655*0fbb751dSJohn Levon out: 1656*0fbb751dSJohn Levon if (vp != NULL) 1657*0fbb751dSJohn Levon VN_RELE(vp); 1658*0fbb751dSJohn Levon return (err); 16597c478bd9Sstevel@tonic-gate } 16607c478bd9Sstevel@tonic-gate 16617c478bd9Sstevel@tonic-gate /* 1662*0fbb751dSJohn Levon * Find the minor for the given filename, checking the zone can access 1663*0fbb751dSJohn Levon * it. 16647c478bd9Sstevel@tonic-gate */ 16657c478bd9Sstevel@tonic-gate static int 1666*0fbb751dSJohn Levon file_to_lofi(char *filename, struct lofi_state **lspp) 16677c478bd9Sstevel@tonic-gate { 1668*0fbb751dSJohn Levon int err = 0; 16697c478bd9Sstevel@tonic-gate 1670*0fbb751dSJohn Levon ASSERT(MUTEX_HELD(&lofi_lock)); 1671*0fbb751dSJohn Levon 1672*0fbb751dSJohn Levon if ((err = file_to_lofi_nocheck(filename, lspp)) != 0) 1673*0fbb751dSJohn Levon return (err); 1674*0fbb751dSJohn Levon 1675*0fbb751dSJohn Levon if ((err = lofi_access(*lspp)) != 0) 1676*0fbb751dSJohn Levon return (err); 1677*0fbb751dSJohn Levon 16787c478bd9Sstevel@tonic-gate return (0); 16797c478bd9Sstevel@tonic-gate } 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate /* 16827c478bd9Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size 16837c478bd9Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device, 16847c478bd9Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and 16857c478bd9Sstevel@tonic-gate * partition information. It uses that information to determine the parameters 168663360950Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 16877c478bd9Sstevel@tonic-gate * have to support it. 16887c478bd9Sstevel@tonic-gate */ 16897c478bd9Sstevel@tonic-gate static void 16907c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp) 16917c478bd9Sstevel@tonic-gate { 16927d82f0f8SDina K Nimeh u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset; 16937d82f0f8SDina K Nimeh 16947c478bd9Sstevel@tonic-gate /* dk_geom - see dkio(7I) */ 16957c478bd9Sstevel@tonic-gate /* 16967c478bd9Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 16977c478bd9Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to 16987c478bd9Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create 16997c478bd9Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick 17007c478bd9Sstevel@tonic-gate * a number between one and one). 17017c478bd9Sstevel@tonic-gate * 17027c478bd9Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders 17037c478bd9Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space 17047c478bd9Sstevel@tonic-gate * for backup superblocks or cylinder group structures. 17057c478bd9Sstevel@tonic-gate */ 17067d82f0f8SDina K Nimeh if (dsize < (2 * 1024 * 1024)) /* floppy? */ 17077d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024); 17087c478bd9Sstevel@tonic-gate else 17097d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024); 17107c478bd9Sstevel@tonic-gate /* in case file file is < 100k */ 17117c478bd9Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0) 17127c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1; 17137c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0; 17147c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0; 17157c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1; 17167c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0; 17177c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0; 17187c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0; 17197c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0; 17207c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0; 17217c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200; 17227c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 17237d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 17247c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0; 17257c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0; 17267c478bd9Sstevel@tonic-gate 17277c478bd9Sstevel@tonic-gate /* vtoc - see dkio(7I) */ 17287c478bd9Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 17297c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE; 17307c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION; 1731947caa0eSDina K Nimeh (void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME, 1732947caa0eSDina K Nimeh sizeof (lsp->ls_vtoc.v_volume)); 17337c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 17347c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1; 17357c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 173687117650Saalok 173787117650Saalok /* 173887117650Saalok * A compressed file is read-only, other files can 173987117650Saalok * be read-write 174087117650Saalok */ 174187117650Saalok if (lsp->ls_uncomp_seg_sz > 0) { 174287117650Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 174387117650Saalok } else { 17447c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 174587117650Saalok } 17467c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 17477c478bd9Sstevel@tonic-gate /* 17487c478bd9Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because 17497c478bd9Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case, 17507c478bd9Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size 17517c478bd9Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder. 17527c478bd9Sstevel@tonic-gate */ 17537c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 17547c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 17557c478bd9Sstevel@tonic-gate 17567c478bd9Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */ 17577c478bd9Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 17587c478bd9Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 17597c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD; 17607c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0; 17617c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0; 17627c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0; 17637c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_space = 0; 17647c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0; 17657c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0; 17667c478bd9Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 17677c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0; 17687c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0; 17697c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0; 17707c478bd9Sstevel@tonic-gate /* 17717c478bd9Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it 17727c478bd9Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides 17737c478bd9Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because 17747c478bd9Sstevel@tonic-gate * maxcontig is 0. 17757c478bd9Sstevel@tonic-gate */ 17767c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16; 17777c478bd9Sstevel@tonic-gate } 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate /* 178087117650Saalok * map in a compressed file 178187117650Saalok * 178287117650Saalok * Read in the header and the index that follows. 178387117650Saalok * 178487117650Saalok * The header is as follows - 178587117650Saalok * 178687117650Saalok * Signature (name of the compression algorithm) 178787117650Saalok * Compression segment size (a multiple of 512) 178887117650Saalok * Number of index entries 178987117650Saalok * Size of the last block 179087117650Saalok * The array containing the index entries 179187117650Saalok * 179287117650Saalok * The header information is always stored in 179387117650Saalok * network byte order on disk. 179487117650Saalok */ 179587117650Saalok static int 179687117650Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 179787117650Saalok { 179887117650Saalok uint32_t index_sz, header_len, i; 179987117650Saalok ssize_t resid; 180087117650Saalok enum uio_rw rw; 180187117650Saalok char *tbuf = buf; 180287117650Saalok int error; 180387117650Saalok 180487117650Saalok /* The signature has already been read */ 180587117650Saalok tbuf += sizeof (lsp->ls_comp_algorithm); 180687117650Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 180787117650Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 180887117650Saalok 180987117650Saalok /* 181087117650Saalok * The compressed segment size must be a power of 2 181187117650Saalok */ 18124058a205Sjrgn.keil@googlemail.com if (lsp->ls_uncomp_seg_sz < DEV_BSIZE || 18134058a205Sjrgn.keil@googlemail.com !ISP2(lsp->ls_uncomp_seg_sz)) 181487117650Saalok return (EINVAL); 181587117650Saalok 181687117650Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 181787117650Saalok ; 181887117650Saalok 181987117650Saalok lsp->ls_comp_seg_shift = i; 182087117650Saalok 182187117650Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz); 182287117650Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 182387117650Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 182487117650Saalok 182587117650Saalok tbuf += sizeof (lsp->ls_comp_index_sz); 182687117650Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 182787117650Saalok sizeof (lsp->ls_uncomp_last_seg_sz)); 182887117650Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 182987117650Saalok 183087117650Saalok /* 183187117650Saalok * Compute the total size of the uncompressed data 183287117650Saalok * for use in fake_disk_geometry and other calculations. 183387117650Saalok * Disk geometry has to be faked with respect to the 183487117650Saalok * actual uncompressed data size rather than the 183587117650Saalok * compressed file size. 183687117650Saalok */ 18377d11f38eSDavid Miner lsp->ls_vp_size = 18387d11f38eSDavid Miner (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 183987117650Saalok + lsp->ls_uncomp_last_seg_sz; 184087117650Saalok 184187117650Saalok /* 1842b1efbcd6SAlok Aggarwal * Index size is rounded up to DEV_BSIZE for ease 184387117650Saalok * of segmapping 184487117650Saalok */ 184587117650Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 184687117650Saalok header_len = sizeof (lsp->ls_comp_algorithm) + 184787117650Saalok sizeof (lsp->ls_uncomp_seg_sz) + 184887117650Saalok sizeof (lsp->ls_comp_index_sz) + 184987117650Saalok sizeof (lsp->ls_uncomp_last_seg_sz); 185087117650Saalok lsp->ls_comp_offbase = header_len + index_sz; 185187117650Saalok 185287117650Saalok index_sz += header_len; 185387117650Saalok index_sz = roundup(index_sz, DEV_BSIZE); 185487117650Saalok 185587117650Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 185687117650Saalok lsp->ls_comp_index_data_sz = index_sz; 185787117650Saalok 185887117650Saalok /* 185987117650Saalok * Read in the index -- this has a side-effect 186087117650Saalok * of reading in the header as well 186187117650Saalok */ 186287117650Saalok rw = UIO_READ; 186387117650Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 186487117650Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 186587117650Saalok 186687117650Saalok if (error != 0) 186787117650Saalok return (error); 186887117650Saalok 186987117650Saalok /* Skip the header, this is where the index really begins */ 187087117650Saalok lsp->ls_comp_seg_index = 187187117650Saalok /*LINTED*/ 187287117650Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len); 187387117650Saalok 187487117650Saalok /* 187587117650Saalok * Now recompute offsets in the index to account for 187687117650Saalok * the header length 187787117650Saalok */ 187887117650Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) { 187987117650Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 188087117650Saalok BE_64(lsp->ls_comp_seg_index[i]); 188187117650Saalok } 188287117650Saalok 188387117650Saalok return (error); 188487117650Saalok } 188587117650Saalok 1886*0fbb751dSJohn Levon static int 1887*0fbb751dSJohn Levon lofi_init_crypto(struct lofi_state *lsp, struct lofi_ioctl *klip) 188887117650Saalok { 1889*0fbb751dSJohn Levon struct crypto_meta chead; 1890*0fbb751dSJohn Levon char buf[DEV_BSIZE]; 1891*0fbb751dSJohn Levon ssize_t resid; 1892*0fbb751dSJohn Levon char *marker; 1893*0fbb751dSJohn Levon int error; 1894*0fbb751dSJohn Levon int ret; 189587117650Saalok int i; 189687117650Saalok 1897*0fbb751dSJohn Levon if (!klip->li_crypto_enabled) 1898*0fbb751dSJohn Levon return (0); 189987117650Saalok 190087117650Saalok /* 1901*0fbb751dSJohn Levon * All current algorithms have a max of 448 bits. 19027c478bd9Sstevel@tonic-gate */ 1903*0fbb751dSJohn Levon if (klip->li_iv_len > CRYPTO_BITS2BYTES(512)) 1904*0fbb751dSJohn Levon return (EINVAL); 19057c478bd9Sstevel@tonic-gate 1906*0fbb751dSJohn Levon if (CRYPTO_BITS2BYTES(klip->li_key_len) > sizeof (klip->li_key)) 1907*0fbb751dSJohn Levon return (EINVAL); 19087c478bd9Sstevel@tonic-gate 1909*0fbb751dSJohn Levon lsp->ls_crypto_enabled = klip->li_crypto_enabled; 191087117650Saalok 19117d82f0f8SDina K Nimeh mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL); 19127d82f0f8SDina K Nimeh 19137d82f0f8SDina K Nimeh lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher); 19147d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) { 19157d82f0f8SDina K Nimeh cmn_err(CE_WARN, "invalid cipher %s requested for %s", 1916*0fbb751dSJohn Levon klip->li_cipher, klip->li_filename); 1917*0fbb751dSJohn Levon return (EINVAL); 19187d82f0f8SDina K Nimeh } 19197d82f0f8SDina K Nimeh 19207d82f0f8SDina K Nimeh /* this is just initialization here */ 19217d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = NULL; 19227d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = 0; 19237d82f0f8SDina K Nimeh 19247d82f0f8SDina K Nimeh lsp->ls_iv_type = klip->li_iv_type; 19257d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher); 19267d82f0f8SDina K Nimeh if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) { 19277d82f0f8SDina K Nimeh cmn_err(CE_WARN, "invalid iv cipher %s requested" 1928*0fbb751dSJohn Levon " for %s", klip->li_iv_cipher, klip->li_filename); 1929*0fbb751dSJohn Levon return (EINVAL); 19307d82f0f8SDina K Nimeh } 19317d82f0f8SDina K Nimeh 19327d82f0f8SDina K Nimeh /* iv mech must itself take a null iv */ 19337d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param = NULL; 19347d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len = 0; 19357d82f0f8SDina K Nimeh lsp->ls_iv_len = klip->li_iv_len; 19367d82f0f8SDina K Nimeh 19377d82f0f8SDina K Nimeh /* 19387d82f0f8SDina K Nimeh * Create ctx using li_cipher & the raw li_key after checking 19397d82f0f8SDina K Nimeh * that it isn't a weak key. 19407d82f0f8SDina K Nimeh */ 19417d82f0f8SDina K Nimeh lsp->ls_key.ck_format = CRYPTO_KEY_RAW; 19427d82f0f8SDina K Nimeh lsp->ls_key.ck_length = klip->li_key_len; 19437d82f0f8SDina K Nimeh lsp->ls_key.ck_data = kmem_alloc( 19447d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP); 19457d82f0f8SDina K Nimeh bcopy(klip->li_key, lsp->ls_key.ck_data, 19467d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 19477d82f0f8SDina K Nimeh 19487d82f0f8SDina K Nimeh ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key); 19497d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) { 19507d82f0f8SDina K Nimeh cmn_err(CE_WARN, "weak key check failed for cipher " 19517d82f0f8SDina K Nimeh "%s on file %s (0x%x)", klip->li_cipher, 1952*0fbb751dSJohn Levon klip->li_filename, ret); 1953*0fbb751dSJohn Levon return (EINVAL); 19547d82f0f8SDina K Nimeh } 19557d82f0f8SDina K Nimeh 1956*0fbb751dSJohn Levon error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 19577d82f0f8SDina K Nimeh CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 19587d82f0f8SDina K Nimeh if (error != 0) 1959*0fbb751dSJohn Levon return (error); 19607d82f0f8SDina K Nimeh 19617d82f0f8SDina K Nimeh /* 1962*0fbb751dSJohn Levon * This is the case where the header in the lofi image is already 1963*0fbb751dSJohn Levon * initialized to indicate it is encrypted. 19647d82f0f8SDina K Nimeh */ 1965*0fbb751dSJohn Levon if (strncmp(buf, lofi_crypto_magic, sizeof (lofi_crypto_magic)) == 0) { 19667d82f0f8SDina K Nimeh /* 19677d82f0f8SDina K Nimeh * The encryption header information is laid out this way: 19687d82f0f8SDina K Nimeh * 6 bytes: hex "CFLOFI" 19697d82f0f8SDina K Nimeh * 2 bytes: version = 0 ... for now 19707d82f0f8SDina K Nimeh * 96 bytes: reserved1 (not implemented yet) 19717d82f0f8SDina K Nimeh * 4 bytes: data_sector = 2 ... for now 19727d82f0f8SDina K Nimeh * more... not implemented yet 19737d82f0f8SDina K Nimeh */ 19747d82f0f8SDina K Nimeh 1975*0fbb751dSJohn Levon marker = buf; 1976*0fbb751dSJohn Levon 19777d82f0f8SDina K Nimeh /* copy the magic */ 19787d82f0f8SDina K Nimeh bcopy(marker, lsp->ls_crypto.magic, 19797d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.magic)); 19807d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.magic); 19817d82f0f8SDina K Nimeh 19827d82f0f8SDina K Nimeh /* read the encryption version number */ 19837d82f0f8SDina K Nimeh bcopy(marker, &(lsp->ls_crypto.version), 19847d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.version)); 19857d82f0f8SDina K Nimeh lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version); 19867d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.version); 19877d82f0f8SDina K Nimeh 19887d82f0f8SDina K Nimeh /* read a chunk of reserved data */ 19897d82f0f8SDina K Nimeh bcopy(marker, lsp->ls_crypto.reserved1, 19907d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.reserved1)); 19917d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.reserved1); 19927d82f0f8SDina K Nimeh 19937d82f0f8SDina K Nimeh /* read block number where encrypted data begins */ 19947d82f0f8SDina K Nimeh bcopy(marker, &(lsp->ls_crypto.data_sector), 19957d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.data_sector)); 19967d82f0f8SDina K Nimeh lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector); 19977d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.data_sector); 19987d82f0f8SDina K Nimeh 19997d82f0f8SDina K Nimeh /* and ignore the rest until it is implemented */ 20007d82f0f8SDina K Nimeh 20017d82f0f8SDina K Nimeh lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 2002*0fbb751dSJohn Levon return (0); 20037d82f0f8SDina K Nimeh } 20047d82f0f8SDina K Nimeh 2005*0fbb751dSJohn Levon /* 2006*0fbb751dSJohn Levon * We've requested encryption, but no magic was found, so it must be 2007*0fbb751dSJohn Levon * a new image. 2008*0fbb751dSJohn Levon */ 2009*0fbb751dSJohn Levon 2010*0fbb751dSJohn Levon for (i = 0; i < sizeof (struct crypto_meta); i++) { 2011*0fbb751dSJohn Levon if (buf[i] != '\0') 2012*0fbb751dSJohn Levon return (EINVAL); 2013*0fbb751dSJohn Levon } 2014*0fbb751dSJohn Levon 2015*0fbb751dSJohn Levon marker = buf; 20167d82f0f8SDina K Nimeh bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic)); 20177d82f0f8SDina K Nimeh marker += sizeof (lofi_crypto_magic); 20187d82f0f8SDina K Nimeh chead.version = htons(LOFI_CRYPTO_VERSION); 20197d82f0f8SDina K Nimeh bcopy(&(chead.version), marker, sizeof (chead.version)); 20207d82f0f8SDina K Nimeh marker += sizeof (chead.version); 20217d82f0f8SDina K Nimeh marker += sizeof (chead.reserved1); 20227d82f0f8SDina K Nimeh chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR); 20237d82f0f8SDina K Nimeh bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector)); 20247d82f0f8SDina K Nimeh 20257d82f0f8SDina K Nimeh /* write the header */ 2026*0fbb751dSJohn Levon error = vn_rdwr(UIO_WRITE, lsp->ls_vp, buf, DEV_BSIZE, 20277d82f0f8SDina K Nimeh CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 20287d82f0f8SDina K Nimeh if (error != 0) 2029*0fbb751dSJohn Levon return (error); 20307d82f0f8SDina K Nimeh 20317d82f0f8SDina K Nimeh /* fix things up so it looks like we read this info */ 20327d82f0f8SDina K Nimeh bcopy(lofi_crypto_magic, lsp->ls_crypto.magic, 20337d82f0f8SDina K Nimeh sizeof (lofi_crypto_magic)); 20347d82f0f8SDina K Nimeh lsp->ls_crypto.version = LOFI_CRYPTO_VERSION; 20357d82f0f8SDina K Nimeh lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR; 20367d82f0f8SDina K Nimeh lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 2037*0fbb751dSJohn Levon return (0); 20387d82f0f8SDina K Nimeh } 20397d82f0f8SDina K Nimeh 20407d82f0f8SDina K Nimeh /* 2041*0fbb751dSJohn Levon * Check to see if the passed in signature is a valid one. If it is 2042*0fbb751dSJohn Levon * valid, return the index into lofi_compress_table. 2043*0fbb751dSJohn Levon * 2044*0fbb751dSJohn Levon * Return -1 if it is invalid 20457d82f0f8SDina K Nimeh */ 2046*0fbb751dSJohn Levon static int 2047*0fbb751dSJohn Levon lofi_compress_select(const char *signature) 2048*0fbb751dSJohn Levon { 2049*0fbb751dSJohn Levon int i; 2050*0fbb751dSJohn Levon 2051*0fbb751dSJohn Levon for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 2052*0fbb751dSJohn Levon if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 2053*0fbb751dSJohn Levon return (i); 205487117650Saalok } 2055*0fbb751dSJohn Levon 2056*0fbb751dSJohn Levon return (-1); 2057*0fbb751dSJohn Levon } 2058*0fbb751dSJohn Levon 2059*0fbb751dSJohn Levon static int 2060*0fbb751dSJohn Levon lofi_init_compress(struct lofi_state *lsp) 2061*0fbb751dSJohn Levon { 2062*0fbb751dSJohn Levon char buf[DEV_BSIZE]; 2063*0fbb751dSJohn Levon int compress_index; 2064*0fbb751dSJohn Levon ssize_t resid; 2065*0fbb751dSJohn Levon int error; 2066*0fbb751dSJohn Levon 2067*0fbb751dSJohn Levon error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 2068*0fbb751dSJohn Levon 0, RLIM64_INFINITY, kcred, &resid); 2069*0fbb751dSJohn Levon 2070*0fbb751dSJohn Levon if (error != 0) 2071*0fbb751dSJohn Levon return (error); 2072*0fbb751dSJohn Levon 2073*0fbb751dSJohn Levon if ((compress_index = lofi_compress_select(buf)) == -1) 2074*0fbb751dSJohn Levon return (0); 2075*0fbb751dSJohn Levon 2076*0fbb751dSJohn Levon /* compression and encryption are mutually exclusive */ 2077*0fbb751dSJohn Levon if (lsp->ls_crypto_enabled) 2078*0fbb751dSJohn Levon return (ENOTSUP); 2079*0fbb751dSJohn Levon 2080*0fbb751dSJohn Levon /* initialize compression info for compressed lofi */ 2081*0fbb751dSJohn Levon lsp->ls_comp_algorithm_index = compress_index; 2082*0fbb751dSJohn Levon (void) strlcpy(lsp->ls_comp_algorithm, 2083*0fbb751dSJohn Levon lofi_compress_table[compress_index].l_name, 2084*0fbb751dSJohn Levon sizeof (lsp->ls_comp_algorithm)); 2085*0fbb751dSJohn Levon 2086*0fbb751dSJohn Levon /* Finally setup per-thread pre-allocated buffers */ 2087*0fbb751dSJohn Levon lsp->ls_comp_bufs = kmem_zalloc(lofi_taskq_nthreads * 2088*0fbb751dSJohn Levon sizeof (struct compbuf), KM_SLEEP); 2089*0fbb751dSJohn Levon 2090*0fbb751dSJohn Levon return (lofi_map_compressed_file(lsp, buf)); 2091*0fbb751dSJohn Levon } 2092*0fbb751dSJohn Levon 2093*0fbb751dSJohn Levon /* 2094*0fbb751dSJohn Levon * map a file to a minor number. Return the minor number. 2095*0fbb751dSJohn Levon */ 2096*0fbb751dSJohn Levon static int 2097*0fbb751dSJohn Levon lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 2098*0fbb751dSJohn Levon int *rvalp, struct cred *credp, int ioctl_flag) 2099*0fbb751dSJohn Levon { 2100*0fbb751dSJohn Levon minor_t minor = (minor_t)-1; 2101*0fbb751dSJohn Levon struct lofi_state *lsp = NULL; 2102*0fbb751dSJohn Levon struct lofi_ioctl *klip; 2103*0fbb751dSJohn Levon int error; 2104*0fbb751dSJohn Levon struct vnode *vp = NULL; 2105*0fbb751dSJohn Levon vattr_t vattr; 2106*0fbb751dSJohn Levon int flag; 2107*0fbb751dSJohn Levon dev_t newdev; 2108*0fbb751dSJohn Levon char namebuf[50]; 2109*0fbb751dSJohn Levon 2110*0fbb751dSJohn Levon error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag); 2111*0fbb751dSJohn Levon if (error != 0) 2112*0fbb751dSJohn Levon return (error); 2113*0fbb751dSJohn Levon 2114*0fbb751dSJohn Levon mutex_enter(&lofi_lock); 2115*0fbb751dSJohn Levon 2116*0fbb751dSJohn Levon mutex_enter(&curproc->p_lock); 2117*0fbb751dSJohn Levon if ((error = rctl_incr_lofi(curproc, curproc->p_zone, 1)) != 0) { 2118*0fbb751dSJohn Levon mutex_exit(&curproc->p_lock); 2119*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 2120*0fbb751dSJohn Levon free_lofi_ioctl(klip); 2121*0fbb751dSJohn Levon return (error); 2122*0fbb751dSJohn Levon } 2123*0fbb751dSJohn Levon mutex_exit(&curproc->p_lock); 2124*0fbb751dSJohn Levon 2125*0fbb751dSJohn Levon if (file_to_lofi_nocheck(klip->li_filename, NULL) == 0) { 2126*0fbb751dSJohn Levon error = EBUSY; 2127*0fbb751dSJohn Levon goto err; 2128*0fbb751dSJohn Levon } 2129*0fbb751dSJohn Levon 2130*0fbb751dSJohn Levon if (pickminor) { 2131*0fbb751dSJohn Levon minor = (minor_t)id_allocff_nosleep(lofi_minor_id); 2132*0fbb751dSJohn Levon if (minor == (minor_t)-1) { 2133*0fbb751dSJohn Levon error = EAGAIN; 2134*0fbb751dSJohn Levon goto err; 2135*0fbb751dSJohn Levon } 2136*0fbb751dSJohn Levon } else { 2137*0fbb751dSJohn Levon if (ddi_get_soft_state(lofi_statep, klip->li_minor) != NULL) { 2138*0fbb751dSJohn Levon error = EEXIST; 2139*0fbb751dSJohn Levon goto err; 2140*0fbb751dSJohn Levon } 2141*0fbb751dSJohn Levon 2142*0fbb751dSJohn Levon minor = (minor_t) 2143*0fbb751dSJohn Levon id_alloc_specific_nosleep(lofi_minor_id, klip->li_minor); 2144*0fbb751dSJohn Levon ASSERT(minor != (minor_t)-1); 2145*0fbb751dSJohn Levon } 2146*0fbb751dSJohn Levon 2147*0fbb751dSJohn Levon flag = FREAD | FWRITE | FOFFMAX | FEXCL; 2148*0fbb751dSJohn Levon error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 2149*0fbb751dSJohn Levon if (error) { 2150*0fbb751dSJohn Levon /* try read-only */ 2151*0fbb751dSJohn Levon flag &= ~FWRITE; 2152*0fbb751dSJohn Levon error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 2153*0fbb751dSJohn Levon &vp, 0, 0); 2154*0fbb751dSJohn Levon if (error) 2155*0fbb751dSJohn Levon goto err; 2156*0fbb751dSJohn Levon } 2157*0fbb751dSJohn Levon 2158*0fbb751dSJohn Levon if (!V_ISLOFIABLE(vp->v_type)) { 215987117650Saalok error = EINVAL; 2160*0fbb751dSJohn Levon goto err; 2161*0fbb751dSJohn Levon } 2162*0fbb751dSJohn Levon 2163*0fbb751dSJohn Levon vattr.va_mask = AT_SIZE; 2164*0fbb751dSJohn Levon error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 2165*0fbb751dSJohn Levon if (error) 2166*0fbb751dSJohn Levon goto err; 2167*0fbb751dSJohn Levon 2168*0fbb751dSJohn Levon /* the file needs to be a multiple of the block size */ 2169*0fbb751dSJohn Levon if ((vattr.va_size % DEV_BSIZE) != 0) { 2170*0fbb751dSJohn Levon error = EINVAL; 2171*0fbb751dSJohn Levon goto err; 2172*0fbb751dSJohn Levon } 2173*0fbb751dSJohn Levon 2174*0fbb751dSJohn Levon /* lsp alloc+init */ 2175*0fbb751dSJohn Levon 2176*0fbb751dSJohn Levon error = ddi_soft_state_zalloc(lofi_statep, minor); 2177*0fbb751dSJohn Levon if (error == DDI_FAILURE) { 2178*0fbb751dSJohn Levon error = ENOMEM; 2179*0fbb751dSJohn Levon goto err; 2180*0fbb751dSJohn Levon } 2181*0fbb751dSJohn Levon 2182*0fbb751dSJohn Levon lsp = ddi_get_soft_state(lofi_statep, minor); 2183*0fbb751dSJohn Levon list_insert_tail(&lofi_list, lsp); 2184*0fbb751dSJohn Levon 2185*0fbb751dSJohn Levon newdev = makedevice(getmajor(dev), minor); 2186*0fbb751dSJohn Levon lsp->ls_dev = newdev; 2187*0fbb751dSJohn Levon lsp->ls_zone = zone_find_by_id(getzoneid()); 2188*0fbb751dSJohn Levon ASSERT(lsp->ls_zone != NULL); 2189*0fbb751dSJohn Levon lsp->ls_uncomp_seg_sz = 0; 2190*0fbb751dSJohn Levon lsp->ls_comp_algorithm[0] = '\0'; 2191*0fbb751dSJohn Levon lsp->ls_crypto_offset = 0; 2192*0fbb751dSJohn Levon 2193*0fbb751dSJohn Levon cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 2194*0fbb751dSJohn Levon mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL); 2195*0fbb751dSJohn Levon mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL); 2196*0fbb751dSJohn Levon mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 2197*0fbb751dSJohn Levon mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 2198*0fbb751dSJohn Levon 2199*0fbb751dSJohn Levon (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 2200*0fbb751dSJohn Levon LOFI_DRIVER_NAME, minor); 2201*0fbb751dSJohn Levon lsp->ls_taskq = taskq_create_proc(namebuf, lofi_taskq_nthreads, 2202*0fbb751dSJohn Levon minclsyspri, 1, lofi_taskq_maxalloc, curzone->zone_zsched, 0); 2203*0fbb751dSJohn Levon 2204*0fbb751dSJohn Levon list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache), 2205*0fbb751dSJohn Levon offsetof(struct lofi_comp_cache, lc_list)); 2206*0fbb751dSJohn Levon 2207*0fbb751dSJohn Levon /* 2208*0fbb751dSJohn Levon * save open mode so file can be closed properly and vnode counts 2209*0fbb751dSJohn Levon * updated correctly. 2210*0fbb751dSJohn Levon */ 2211*0fbb751dSJohn Levon lsp->ls_openflag = flag; 2212*0fbb751dSJohn Levon 2213*0fbb751dSJohn Levon lsp->ls_vp = vp; 2214*0fbb751dSJohn Levon lsp->ls_stacked_vp = vp; 2215*0fbb751dSJohn Levon /* 2216*0fbb751dSJohn Levon * Try to handle stacked lofs vnodes. 2217*0fbb751dSJohn Levon */ 2218*0fbb751dSJohn Levon if (vp->v_type == VREG) { 2219*0fbb751dSJohn Levon vnode_t *realvp; 2220*0fbb751dSJohn Levon 2221*0fbb751dSJohn Levon if (VOP_REALVP(vp, &realvp, NULL) == 0) { 2222*0fbb751dSJohn Levon /* 2223*0fbb751dSJohn Levon * We need to use the realvp for uniqueness 2224*0fbb751dSJohn Levon * checking, but keep the stacked vp for 2225*0fbb751dSJohn Levon * LOFI_GET_FILENAME display. 2226*0fbb751dSJohn Levon */ 2227*0fbb751dSJohn Levon VN_HOLD(realvp); 2228*0fbb751dSJohn Levon lsp->ls_vp = realvp; 222987117650Saalok } 223087117650Saalok } 223187117650Saalok 2232*0fbb751dSJohn Levon lsp->ls_vp_size = vattr.va_size; 2233*0fbb751dSJohn Levon lsp->ls_vp_comp_size = lsp->ls_vp_size; 2234*0fbb751dSJohn Levon 2235*0fbb751dSJohn Levon lsp->ls_kstat = kstat_create_zone(LOFI_DRIVER_NAME, minor, 2236*0fbb751dSJohn Levon NULL, "disk", KSTAT_TYPE_IO, 1, 0, getzoneid()); 2237*0fbb751dSJohn Levon 2238*0fbb751dSJohn Levon if (lsp->ls_kstat == NULL) { 2239*0fbb751dSJohn Levon error = ENOMEM; 2240*0fbb751dSJohn Levon goto err; 2241*0fbb751dSJohn Levon } 2242*0fbb751dSJohn Levon 2243*0fbb751dSJohn Levon lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 2244*0fbb751dSJohn Levon kstat_zone_add(lsp->ls_kstat, GLOBAL_ZONEID); 2245*0fbb751dSJohn Levon 2246*0fbb751dSJohn Levon if ((error = lofi_init_crypto(lsp, klip)) != 0) 2247*0fbb751dSJohn Levon goto err; 2248*0fbb751dSJohn Levon 2249*0fbb751dSJohn Levon if ((error = lofi_init_compress(lsp)) != 0) 2250*0fbb751dSJohn Levon goto err; 2251*0fbb751dSJohn Levon 22527c478bd9Sstevel@tonic-gate fake_disk_geometry(lsp); 2253*0fbb751dSJohn Levon 2254*0fbb751dSJohn Levon /* create minor nodes */ 2255*0fbb751dSJohn Levon 2256*0fbb751dSJohn Levon (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 2257*0fbb751dSJohn Levon error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, minor, 2258*0fbb751dSJohn Levon DDI_PSEUDO, NULL); 2259*0fbb751dSJohn Levon if (error != DDI_SUCCESS) { 2260*0fbb751dSJohn Levon error = ENXIO; 2261*0fbb751dSJohn Levon goto err; 2262*0fbb751dSJohn Levon } 2263*0fbb751dSJohn Levon 2264*0fbb751dSJohn Levon (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 2265*0fbb751dSJohn Levon error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, minor, 2266*0fbb751dSJohn Levon DDI_PSEUDO, NULL); 2267*0fbb751dSJohn Levon if (error != DDI_SUCCESS) { 2268*0fbb751dSJohn Levon /* remove block node */ 2269*0fbb751dSJohn Levon (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 2270*0fbb751dSJohn Levon ddi_remove_minor_node(lofi_dip, namebuf); 2271*0fbb751dSJohn Levon error = ENXIO; 2272*0fbb751dSJohn Levon goto err; 2273*0fbb751dSJohn Levon } 2274*0fbb751dSJohn Levon 2275*0fbb751dSJohn Levon /* create DDI properties */ 2276*0fbb751dSJohn Levon 2277*0fbb751dSJohn Levon if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 2278*0fbb751dSJohn Levon lsp->ls_vp_size - lsp->ls_crypto_offset)) != DDI_PROP_SUCCESS) { 2279*0fbb751dSJohn Levon error = EINVAL; 2280*0fbb751dSJohn Levon goto nodeerr; 2281*0fbb751dSJohn Levon } 2282*0fbb751dSJohn Levon 2283*0fbb751dSJohn Levon if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 2284*0fbb751dSJohn Levon (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE)) 2285*0fbb751dSJohn Levon != DDI_PROP_SUCCESS) { 2286*0fbb751dSJohn Levon error = EINVAL; 2287*0fbb751dSJohn Levon goto nodeerr; 2288*0fbb751dSJohn Levon } 2289*0fbb751dSJohn Levon 2290*0fbb751dSJohn Levon if (ddi_prop_update_string(newdev, lofi_dip, ZONE_PROP_NAME, 2291*0fbb751dSJohn Levon (char *)curproc->p_zone->zone_name) != DDI_PROP_SUCCESS) { 2292*0fbb751dSJohn Levon error = EINVAL; 2293*0fbb751dSJohn Levon goto nodeerr; 2294*0fbb751dSJohn Levon } 2295*0fbb751dSJohn Levon 2296*0fbb751dSJohn Levon kstat_install(lsp->ls_kstat); 2297*0fbb751dSJohn Levon 22987c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2299*0fbb751dSJohn Levon 2300*0fbb751dSJohn Levon if (rvalp) 2301*0fbb751dSJohn Levon *rvalp = (int)minor; 2302*0fbb751dSJohn Levon klip->li_minor = minor; 2303bd07e074Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 23047c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23057c478bd9Sstevel@tonic-gate return (0); 23067c478bd9Sstevel@tonic-gate 2307*0fbb751dSJohn Levon nodeerr: 2308*0fbb751dSJohn Levon lofi_free_dev(newdev); 2309*0fbb751dSJohn Levon err: 2310*0fbb751dSJohn Levon if (lsp != NULL) { 2311*0fbb751dSJohn Levon lofi_destroy(lsp, credp); 2312*0fbb751dSJohn Levon } else { 2313*0fbb751dSJohn Levon if (vp != NULL) { 23147d82f0f8SDina K Nimeh (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 23157d82f0f8SDina K Nimeh VN_RELE(vp); 23167d82f0f8SDina K Nimeh } 23177d82f0f8SDina K Nimeh 2318*0fbb751dSJohn Levon if (minor != (minor_t)-1) 2319*0fbb751dSJohn Levon id_free(lofi_minor_id, minor); 2320*0fbb751dSJohn Levon 2321*0fbb751dSJohn Levon rctl_decr_lofi(curproc->p_zone, 1); 2322*0fbb751dSJohn Levon } 2323*0fbb751dSJohn Levon 23247c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 23257c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23267c478bd9Sstevel@tonic-gate return (error); 23277c478bd9Sstevel@tonic-gate } 23287c478bd9Sstevel@tonic-gate 23297c478bd9Sstevel@tonic-gate /* 23307c478bd9Sstevel@tonic-gate * unmap a file. 23317c478bd9Sstevel@tonic-gate */ 23327c478bd9Sstevel@tonic-gate static int 23337c478bd9Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 2334bd07e074Sheppo struct cred *credp, int ioctl_flag) 23357c478bd9Sstevel@tonic-gate { 23367c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 23377c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 2338*0fbb751dSJohn Levon int err; 23397c478bd9Sstevel@tonic-gate 2340*0fbb751dSJohn Levon err = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag); 2341*0fbb751dSJohn Levon if (err != 0) 2342*0fbb751dSJohn Levon return (err); 23437c478bd9Sstevel@tonic-gate 23447c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 23457c478bd9Sstevel@tonic-gate if (byfilename) { 2346*0fbb751dSJohn Levon if ((err = file_to_lofi(klip->li_filename, &lsp)) != 0) { 2347*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 2348*0fbb751dSJohn Levon return (err); 2349*0fbb751dSJohn Levon } 2350*0fbb751dSJohn Levon } else if (klip->li_minor == 0) { 2351*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 2352*0fbb751dSJohn Levon free_lofi_ioctl(klip); 2353*0fbb751dSJohn Levon return (ENXIO); 23547c478bd9Sstevel@tonic-gate } else { 2355*0fbb751dSJohn Levon lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 23567c478bd9Sstevel@tonic-gate } 2357*0fbb751dSJohn Levon 2358*0fbb751dSJohn Levon if (lsp == NULL || lsp->ls_vp == NULL || lofi_access(lsp) != 0) { 23597c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 23607c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23617c478bd9Sstevel@tonic-gate return (ENXIO); 23627c478bd9Sstevel@tonic-gate } 2363*0fbb751dSJohn Levon 2364*0fbb751dSJohn Levon klip->li_minor = getminor(lsp->ls_dev); 23653d7072f8Seschrock 23663d7072f8Seschrock /* 236793239addSjohnlev * If it's still held open, we'll do one of three things: 236893239addSjohnlev * 236993239addSjohnlev * If no flag is set, just return EBUSY. 237093239addSjohnlev * 237193239addSjohnlev * If the 'cleanup' flag is set, unmap and remove the device when 237293239addSjohnlev * the last user finishes. 237393239addSjohnlev * 237493239addSjohnlev * If the 'force' flag is set, then we forcibly close the underlying 237593239addSjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl 237693239addSjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the 237793239addSjohnlev * device will be cleaned up appropriately. 23783d7072f8Seschrock * 23793d7072f8Seschrock * This is complicated by the fact that we may have outstanding 238093239addSjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all 23817d82f0f8SDina K Nimeh * I/O, we keep a count of the number of outstanding I/O requests 23827d82f0f8SDina K Nimeh * (ls_vp_iocount), as well as a flag to indicate that no new I/Os 23837d82f0f8SDina K Nimeh * should be dispatched (ls_vp_closereq). 23847d82f0f8SDina K Nimeh * 238593239addSjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0, 238693239addSjohnlev * and then close the underlying vnode. 23873d7072f8Seschrock */ 238893239addSjohnlev if (is_opened(lsp)) { 23893d7072f8Seschrock if (klip->li_force) { 23903d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 23913d7072f8Seschrock lsp->ls_vp_closereq = B_TRUE; 2392b3388e4fSEric Taylor /* wake up any threads waiting on dkiocstate */ 2393b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv); 23943d7072f8Seschrock while (lsp->ls_vp_iocount > 0) 23953d7072f8Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 23963d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 23977d82f0f8SDina K Nimeh 2398*0fbb751dSJohn Levon goto out; 239993239addSjohnlev } else if (klip->li_cleanup) { 240093239addSjohnlev lsp->ls_cleanup = 1; 240193239addSjohnlev mutex_exit(&lofi_lock); 240293239addSjohnlev free_lofi_ioctl(klip); 240393239addSjohnlev return (0); 24043d7072f8Seschrock } 240593239addSjohnlev 24067c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 24077c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 24087c478bd9Sstevel@tonic-gate return (EBUSY); 24097c478bd9Sstevel@tonic-gate } 24107c478bd9Sstevel@tonic-gate 2411*0fbb751dSJohn Levon out: 2412*0fbb751dSJohn Levon lofi_free_dev(dev); 2413*0fbb751dSJohn Levon lofi_destroy(lsp, credp); 24147c478bd9Sstevel@tonic-gate 24157c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2416bd07e074Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 24177c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 24187c478bd9Sstevel@tonic-gate return (0); 24197c478bd9Sstevel@tonic-gate } 24207c478bd9Sstevel@tonic-gate 24217c478bd9Sstevel@tonic-gate /* 24227c478bd9Sstevel@tonic-gate * get the filename given the minor number, or the minor number given 24237c478bd9Sstevel@tonic-gate * the name. 24247c478bd9Sstevel@tonic-gate */ 24253d7072f8Seschrock /*ARGSUSED*/ 24267c478bd9Sstevel@tonic-gate static int 24277c478bd9Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 2428bd07e074Sheppo struct cred *credp, int ioctl_flag) 24297c478bd9Sstevel@tonic-gate { 24307c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 2431*0fbb751dSJohn Levon struct lofi_state *lsp; 24327c478bd9Sstevel@tonic-gate int error; 24337c478bd9Sstevel@tonic-gate 2434*0fbb751dSJohn Levon error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag); 2435*0fbb751dSJohn Levon if (error != 0) 2436*0fbb751dSJohn Levon return (error); 24377c478bd9Sstevel@tonic-gate 24387c478bd9Sstevel@tonic-gate switch (which) { 24397c478bd9Sstevel@tonic-gate case LOFI_GET_FILENAME: 2440*0fbb751dSJohn Levon if (klip->li_minor == 0) { 24417c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 24427c478bd9Sstevel@tonic-gate return (EINVAL); 24437c478bd9Sstevel@tonic-gate } 24447c478bd9Sstevel@tonic-gate 24457c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 2446*0fbb751dSJohn Levon lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 2447*0fbb751dSJohn Levon if (lsp == NULL || lofi_access(lsp) != 0) { 24487c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 24497c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 24507c478bd9Sstevel@tonic-gate return (ENXIO); 24517c478bd9Sstevel@tonic-gate } 2452*0fbb751dSJohn Levon 2453*0fbb751dSJohn Levon /* 2454*0fbb751dSJohn Levon * This may fail if, for example, we're trying to look 2455*0fbb751dSJohn Levon * up a zoned NFS path from the global zone. 2456*0fbb751dSJohn Levon */ 2457*0fbb751dSJohn Levon if (vnodetopath(NULL, lsp->ls_stacked_vp, klip->li_filename, 2458*0fbb751dSJohn Levon sizeof (klip->li_filename), CRED()) != 0) { 2459*0fbb751dSJohn Levon (void) strlcpy(klip->li_filename, "?", 2460*0fbb751dSJohn Levon sizeof (klip->li_filename)); 2461*0fbb751dSJohn Levon } 2462*0fbb751dSJohn Levon 246387117650Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 246487117650Saalok sizeof (klip->li_algorithm)); 24657d82f0f8SDina K Nimeh klip->li_crypto_enabled = lsp->ls_crypto_enabled; 24667c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2467bd07e074Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 24687c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 24697c478bd9Sstevel@tonic-gate return (error); 24707c478bd9Sstevel@tonic-gate case LOFI_GET_MINOR: 24717c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 2472*0fbb751dSJohn Levon error = file_to_lofi(klip->li_filename, &lsp); 2473*0fbb751dSJohn Levon if (error == 0) 2474*0fbb751dSJohn Levon klip->li_minor = getminor(lsp->ls_dev); 24757c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2476*0fbb751dSJohn Levon 2477*0fbb751dSJohn Levon if (error == 0) 2478bd07e074Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 2479*0fbb751dSJohn Levon 24807c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 24817c478bd9Sstevel@tonic-gate return (error); 248287117650Saalok case LOFI_CHECK_COMPRESSED: 248387117650Saalok mutex_enter(&lofi_lock); 2484*0fbb751dSJohn Levon error = file_to_lofi(klip->li_filename, &lsp); 2485*0fbb751dSJohn Levon if (error != 0) { 248687117650Saalok mutex_exit(&lofi_lock); 248787117650Saalok free_lofi_ioctl(klip); 2488*0fbb751dSJohn Levon return (error); 248987117650Saalok } 249087117650Saalok 2491*0fbb751dSJohn Levon klip->li_minor = getminor(lsp->ls_dev); 249287117650Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 249387117650Saalok sizeof (klip->li_algorithm)); 2494*0fbb751dSJohn Levon 249587117650Saalok mutex_exit(&lofi_lock); 249687117650Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 249787117650Saalok free_lofi_ioctl(klip); 249887117650Saalok return (error); 24997c478bd9Sstevel@tonic-gate default: 25007c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 25017c478bd9Sstevel@tonic-gate return (EINVAL); 25027c478bd9Sstevel@tonic-gate } 25037c478bd9Sstevel@tonic-gate } 25047c478bd9Sstevel@tonic-gate 25057c478bd9Sstevel@tonic-gate static int 25067c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 25077c478bd9Sstevel@tonic-gate int *rvalp) 25087c478bd9Sstevel@tonic-gate { 25097c478bd9Sstevel@tonic-gate int error; 25107c478bd9Sstevel@tonic-gate enum dkio_state dkstate; 25117c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 25127c478bd9Sstevel@tonic-gate minor_t minor; 25137c478bd9Sstevel@tonic-gate 25147c478bd9Sstevel@tonic-gate minor = getminor(dev); 25157c478bd9Sstevel@tonic-gate /* lofi ioctls only apply to the master device */ 25167c478bd9Sstevel@tonic-gate if (minor == 0) { 25177c478bd9Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 25187c478bd9Sstevel@tonic-gate 25197c478bd9Sstevel@tonic-gate /* 25207c478bd9Sstevel@tonic-gate * the query command only need read-access - i.e., normal 25217c478bd9Sstevel@tonic-gate * users are allowed to do those on the ctl device as 25227c478bd9Sstevel@tonic-gate * long as they can open it read-only. 25237c478bd9Sstevel@tonic-gate */ 25247c478bd9Sstevel@tonic-gate switch (cmd) { 25257c478bd9Sstevel@tonic-gate case LOFI_MAP_FILE: 25267c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 25277c478bd9Sstevel@tonic-gate return (EPERM); 2528bd07e074Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 25297c478bd9Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR: 25307c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 25317c478bd9Sstevel@tonic-gate return (EPERM); 2532bd07e074Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 25337c478bd9Sstevel@tonic-gate case LOFI_UNMAP_FILE: 25347c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 25357c478bd9Sstevel@tonic-gate return (EPERM); 2536bd07e074Sheppo return (lofi_unmap_file(dev, lip, 1, credp, flag)); 25377c478bd9Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR: 25387c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 25397c478bd9Sstevel@tonic-gate return (EPERM); 2540bd07e074Sheppo return (lofi_unmap_file(dev, lip, 0, credp, flag)); 25417c478bd9Sstevel@tonic-gate case LOFI_GET_FILENAME: 25427c478bd9Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 2543bd07e074Sheppo credp, flag)); 25447c478bd9Sstevel@tonic-gate case LOFI_GET_MINOR: 25457c478bd9Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 2546bd07e074Sheppo credp, flag)); 2547*0fbb751dSJohn Levon 2548*0fbb751dSJohn Levon /* 2549*0fbb751dSJohn Levon * This API made limited sense when this value was fixed 2550*0fbb751dSJohn Levon * at LOFI_MAX_FILES. However, its use to iterate 2551*0fbb751dSJohn Levon * across all possible devices in lofiadm means we don't 2552*0fbb751dSJohn Levon * want to return L_MAXMIN32, but the highest 2553*0fbb751dSJohn Levon * *allocated* minor. 2554*0fbb751dSJohn Levon */ 25557c478bd9Sstevel@tonic-gate case LOFI_GET_MAXMINOR: 2556*0fbb751dSJohn Levon minor = 0; 2557*0fbb751dSJohn Levon 2558*0fbb751dSJohn Levon mutex_enter(&lofi_lock); 2559*0fbb751dSJohn Levon 2560*0fbb751dSJohn Levon for (lsp = list_head(&lofi_list); lsp != NULL; 2561*0fbb751dSJohn Levon lsp = list_next(&lofi_list, lsp)) { 2562*0fbb751dSJohn Levon if (lofi_access(lsp) != 0) 2563*0fbb751dSJohn Levon continue; 2564*0fbb751dSJohn Levon 2565*0fbb751dSJohn Levon if (getminor(lsp->ls_dev) > minor) 2566*0fbb751dSJohn Levon minor = getminor(lsp->ls_dev); 2567*0fbb751dSJohn Levon } 2568*0fbb751dSJohn Levon 2569*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 2570*0fbb751dSJohn Levon 2571*0fbb751dSJohn Levon error = ddi_copyout(&minor, &lip->li_minor, 2572*0fbb751dSJohn Levon sizeof (minor), flag); 25737c478bd9Sstevel@tonic-gate if (error) 25747c478bd9Sstevel@tonic-gate return (EFAULT); 25757c478bd9Sstevel@tonic-gate return (0); 2576*0fbb751dSJohn Levon 257787117650Saalok case LOFI_CHECK_COMPRESSED: 257887117650Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 257987117650Saalok credp, flag)); 25807c478bd9Sstevel@tonic-gate default: 2581*0fbb751dSJohn Levon return (EINVAL); 25827c478bd9Sstevel@tonic-gate } 25837c478bd9Sstevel@tonic-gate } 25847c478bd9Sstevel@tonic-gate 2585b3388e4fSEric Taylor mutex_enter(&lofi_lock); 25867c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 2587b3388e4fSEric Taylor if (lsp == NULL || lsp->ls_vp_closereq) { 2588b3388e4fSEric Taylor mutex_exit(&lofi_lock); 25897c478bd9Sstevel@tonic-gate return (ENXIO); 2590b3388e4fSEric Taylor } 2591b3388e4fSEric Taylor mutex_exit(&lofi_lock); 25927c478bd9Sstevel@tonic-gate 25933d7072f8Seschrock /* 25943d7072f8Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 25953d7072f8Seschrock * EIO as if the device was no longer present. 25963d7072f8Seschrock */ 25973d7072f8Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 25983d7072f8Seschrock return (EIO); 25993d7072f8Seschrock 26007c478bd9Sstevel@tonic-gate /* these are for faking out utilities like newfs */ 26017c478bd9Sstevel@tonic-gate switch (cmd) { 26027c478bd9Sstevel@tonic-gate case DKIOCGVTOC: 26037c478bd9Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) { 26047c478bd9Sstevel@tonic-gate case DDI_MODEL_ILP32: { 26057c478bd9Sstevel@tonic-gate struct vtoc32 vtoc32; 26067c478bd9Sstevel@tonic-gate 26077c478bd9Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32); 26087c478bd9Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg, 26097c478bd9Sstevel@tonic-gate sizeof (struct vtoc32), flag)) 26107c478bd9Sstevel@tonic-gate return (EFAULT); 26117c478bd9Sstevel@tonic-gate break; 26127c478bd9Sstevel@tonic-gate } 26137c478bd9Sstevel@tonic-gate 26147c478bd9Sstevel@tonic-gate case DDI_MODEL_NONE: 26157c478bd9Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 26167c478bd9Sstevel@tonic-gate sizeof (struct vtoc), flag)) 26177c478bd9Sstevel@tonic-gate return (EFAULT); 26187c478bd9Sstevel@tonic-gate break; 26197c478bd9Sstevel@tonic-gate } 26207c478bd9Sstevel@tonic-gate return (0); 26217c478bd9Sstevel@tonic-gate case DKIOCINFO: 2622bd07e074Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg, 2623bd07e074Sheppo sizeof (struct dk_cinfo), flag); 26247c478bd9Sstevel@tonic-gate if (error) 26257c478bd9Sstevel@tonic-gate return (EFAULT); 26267c478bd9Sstevel@tonic-gate return (0); 26277c478bd9Sstevel@tonic-gate case DKIOCG_VIRTGEOM: 26287c478bd9Sstevel@tonic-gate case DKIOCG_PHYGEOM: 26297c478bd9Sstevel@tonic-gate case DKIOCGGEOM: 2630bd07e074Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 2631bd07e074Sheppo sizeof (struct dk_geom), flag); 26327c478bd9Sstevel@tonic-gate if (error) 26337c478bd9Sstevel@tonic-gate return (EFAULT); 26347c478bd9Sstevel@tonic-gate return (0); 26357c478bd9Sstevel@tonic-gate case DKIOCSTATE: 26363d7072f8Seschrock /* 26373d7072f8Seschrock * Normally, lofi devices are always in the INSERTED state. If 26383d7072f8Seschrock * a device is forcefully unmapped, then the device transitions 26393d7072f8Seschrock * to the DKIO_DEV_GONE state. 26403d7072f8Seschrock */ 26413d7072f8Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 26423d7072f8Seschrock flag) != 0) 26433d7072f8Seschrock return (EFAULT); 26443d7072f8Seschrock 26453d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 2646b3388e4fSEric Taylor lsp->ls_vp_iocount++; 2647b3388e4fSEric Taylor while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 2648b3388e4fSEric Taylor (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) && 2649b3388e4fSEric Taylor !lsp->ls_vp_closereq) { 26503d7072f8Seschrock /* 26513d7072f8Seschrock * By virtue of having the device open, we know that 26523d7072f8Seschrock * 'lsp' will remain valid when we return. 26533d7072f8Seschrock */ 26543d7072f8Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv, 26553d7072f8Seschrock &lsp->ls_vp_lock)) { 2656b3388e4fSEric Taylor lsp->ls_vp_iocount--; 2657b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv); 26583d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 26593d7072f8Seschrock return (EINTR); 26603d7072f8Seschrock } 26613d7072f8Seschrock } 26623d7072f8Seschrock 2663b3388e4fSEric Taylor dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ? 2664b3388e4fSEric Taylor DKIO_INSERTED : DKIO_DEV_GONE); 2665b3388e4fSEric Taylor lsp->ls_vp_iocount--; 2666b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv); 26673d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 26683d7072f8Seschrock 26693d7072f8Seschrock if (ddi_copyout(&dkstate, (void *)arg, 26703d7072f8Seschrock sizeof (dkstate), flag) != 0) 26717c478bd9Sstevel@tonic-gate return (EFAULT); 26727c478bd9Sstevel@tonic-gate return (0); 26737c478bd9Sstevel@tonic-gate default: 26747c478bd9Sstevel@tonic-gate return (ENOTTY); 26757c478bd9Sstevel@tonic-gate } 26767c478bd9Sstevel@tonic-gate } 26777c478bd9Sstevel@tonic-gate 26787c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = { 26797c478bd9Sstevel@tonic-gate lofi_open, /* open */ 26807c478bd9Sstevel@tonic-gate lofi_close, /* close */ 26817c478bd9Sstevel@tonic-gate lofi_strategy, /* strategy */ 26827c478bd9Sstevel@tonic-gate nodev, /* print */ 26837c478bd9Sstevel@tonic-gate nodev, /* dump */ 26847c478bd9Sstevel@tonic-gate lofi_read, /* read */ 26857c478bd9Sstevel@tonic-gate lofi_write, /* write */ 26867c478bd9Sstevel@tonic-gate lofi_ioctl, /* ioctl */ 26877c478bd9Sstevel@tonic-gate nodev, /* devmap */ 26887c478bd9Sstevel@tonic-gate nodev, /* mmap */ 26897c478bd9Sstevel@tonic-gate nodev, /* segmap */ 26907c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 26917c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 26927c478bd9Sstevel@tonic-gate 0, /* streamtab */ 26937c478bd9Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 26947c478bd9Sstevel@tonic-gate CB_REV, 26957c478bd9Sstevel@tonic-gate lofi_aread, 26967c478bd9Sstevel@tonic-gate lofi_awrite 26977c478bd9Sstevel@tonic-gate }; 26987c478bd9Sstevel@tonic-gate 26997c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = { 27007c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 27017c478bd9Sstevel@tonic-gate 0, /* refcnt */ 27027c478bd9Sstevel@tonic-gate lofi_info, /* info */ 27037c478bd9Sstevel@tonic-gate nulldev, /* identify */ 27047c478bd9Sstevel@tonic-gate nulldev, /* probe */ 27057c478bd9Sstevel@tonic-gate lofi_attach, /* attach */ 27067c478bd9Sstevel@tonic-gate lofi_detach, /* detach */ 27077c478bd9Sstevel@tonic-gate nodev, /* reset */ 27087c478bd9Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */ 270919397407SSherry Moore NULL, /* no bus operations */ 271019397407SSherry Moore NULL, /* power */ 271119397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 27127c478bd9Sstevel@tonic-gate }; 27137c478bd9Sstevel@tonic-gate 27147c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 27157c478bd9Sstevel@tonic-gate &mod_driverops, 271619397407SSherry Moore "loopback file driver", 27177c478bd9Sstevel@tonic-gate &lofi_ops, 27187c478bd9Sstevel@tonic-gate }; 27197c478bd9Sstevel@tonic-gate 27207c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 27217c478bd9Sstevel@tonic-gate MODREV_1, 27227c478bd9Sstevel@tonic-gate &modldrv, 27237c478bd9Sstevel@tonic-gate NULL 27247c478bd9Sstevel@tonic-gate }; 27257c478bd9Sstevel@tonic-gate 27267c478bd9Sstevel@tonic-gate int 27277c478bd9Sstevel@tonic-gate _init(void) 27287c478bd9Sstevel@tonic-gate { 27297c478bd9Sstevel@tonic-gate int error; 27307c478bd9Sstevel@tonic-gate 2731*0fbb751dSJohn Levon list_create(&lofi_list, sizeof (struct lofi_state), 2732*0fbb751dSJohn Levon offsetof(struct lofi_state, ls_list)); 2733*0fbb751dSJohn Levon 27347c478bd9Sstevel@tonic-gate error = ddi_soft_state_init(&lofi_statep, 27357c478bd9Sstevel@tonic-gate sizeof (struct lofi_state), 0); 27367c478bd9Sstevel@tonic-gate if (error) 27377c478bd9Sstevel@tonic-gate return (error); 27387c478bd9Sstevel@tonic-gate 27397c478bd9Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL); 2740*0fbb751dSJohn Levon 27417c478bd9Sstevel@tonic-gate error = mod_install(&modlinkage); 27427c478bd9Sstevel@tonic-gate if (error) { 27437c478bd9Sstevel@tonic-gate mutex_destroy(&lofi_lock); 27447c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 2745*0fbb751dSJohn Levon list_destroy(&lofi_list); 27467c478bd9Sstevel@tonic-gate } 27477c478bd9Sstevel@tonic-gate 27487c478bd9Sstevel@tonic-gate return (error); 27497c478bd9Sstevel@tonic-gate } 27507c478bd9Sstevel@tonic-gate 27517c478bd9Sstevel@tonic-gate int 27527c478bd9Sstevel@tonic-gate _fini(void) 27537c478bd9Sstevel@tonic-gate { 27547c478bd9Sstevel@tonic-gate int error; 27557c478bd9Sstevel@tonic-gate 2756*0fbb751dSJohn Levon mutex_enter(&lofi_lock); 2757*0fbb751dSJohn Levon 2758*0fbb751dSJohn Levon if (!list_is_empty(&lofi_list)) { 2759*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 27607c478bd9Sstevel@tonic-gate return (EBUSY); 2761*0fbb751dSJohn Levon } 2762*0fbb751dSJohn Levon 2763*0fbb751dSJohn Levon mutex_exit(&lofi_lock); 27647c478bd9Sstevel@tonic-gate 27657c478bd9Sstevel@tonic-gate error = mod_remove(&modlinkage); 27667c478bd9Sstevel@tonic-gate if (error) 27677c478bd9Sstevel@tonic-gate return (error); 27687c478bd9Sstevel@tonic-gate 27697c478bd9Sstevel@tonic-gate mutex_destroy(&lofi_lock); 27707c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 2771*0fbb751dSJohn Levon list_destroy(&lofi_list); 27727c478bd9Sstevel@tonic-gate 27737c478bd9Sstevel@tonic-gate return (error); 27747c478bd9Sstevel@tonic-gate } 27757c478bd9Sstevel@tonic-gate 27767c478bd9Sstevel@tonic-gate int 27777c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 27787c478bd9Sstevel@tonic-gate { 27797c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 27807c478bd9Sstevel@tonic-gate } 2781