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 /* 22*44a1e32bSbatschul * 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 * lofi on itself. The simple lock strategy (lofi_lock) precludes this 807c478bd9Sstevel@tonic-gate * because you'll be in lofi_ioctl, holding the lock when you open the 817c478bd9Sstevel@tonic-gate * file, which, if it's lofi, will grab lofi_lock. We prevent this for 827c478bd9Sstevel@tonic-gate * now, though not using ddi_soft_state(9F) would make it possible to 837c478bd9Sstevel@tonic-gate * do. Though it would still be silly. 847c478bd9Sstevel@tonic-gate * 857c478bd9Sstevel@tonic-gate * Interesting things to do: 867c478bd9Sstevel@tonic-gate * 877c478bd9Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically. 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not 907c478bd9Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't 917d82f0f8SDina K Nimeh * need to fake a geometry, however, it may be relevant if you're replacing 927d82f0f8SDina K Nimeh * metadisk, or using lofi to get crypto. 937d82f0f8SDina K Nimeh * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1 947d82f0f8SDina K Nimeh * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home. 957d82f0f8SDina K Nimeh * In fact this even makes sense if you have lofi "above" metadisk. 967c478bd9Sstevel@tonic-gate * 977d82f0f8SDina K Nimeh * Encryption: 987d82f0f8SDina K Nimeh * Each lofi device can have its own symmetric key and cipher. 997d82f0f8SDina K Nimeh * They are passed to us by lofiadm(1m) in the correct format for use 1007d82f0f8SDina K Nimeh * with the misc/kcf crypto_* routines. 1017d82f0f8SDina K Nimeh * 1027d82f0f8SDina K Nimeh * Each block has its own IV, that is calculated in lofi_blk_mech(), based 1037d82f0f8SDina K Nimeh * on the "master" key held in the lsp and the block number of the buffer. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate #include <sys/types.h> 10787117650Saalok #include <netinet/in.h> 1087c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 1097c478bd9Sstevel@tonic-gate #include <sys/uio.h> 1107c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 1117c478bd9Sstevel@tonic-gate #include <sys/cred.h> 1127c478bd9Sstevel@tonic-gate #include <sys/mman.h> 1137c478bd9Sstevel@tonic-gate #include <sys/errno.h> 1147c478bd9Sstevel@tonic-gate #include <sys/aio_req.h> 1157c478bd9Sstevel@tonic-gate #include <sys/stat.h> 1167c478bd9Sstevel@tonic-gate #include <sys/file.h> 1177c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 1187c478bd9Sstevel@tonic-gate #include <sys/conf.h> 1197c478bd9Sstevel@tonic-gate #include <sys/debug.h> 1207c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 1217c478bd9Sstevel@tonic-gate #include <sys/lofi.h> 1227c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 1237c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 1247c478bd9Sstevel@tonic-gate #include <sys/filio.h> 1257c478bd9Sstevel@tonic-gate #include <sys/fdio.h> 1267c478bd9Sstevel@tonic-gate #include <sys/open.h> 1277c478bd9Sstevel@tonic-gate #include <sys/disp.h> 1287c478bd9Sstevel@tonic-gate #include <vm/seg_map.h> 1297c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 1307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 13187117650Saalok #include <sys/zmod.h> 1327d82f0f8SDina K Nimeh #include <sys/crypto/common.h> 1337d82f0f8SDina K Nimeh #include <sys/crypto/api.h> 134b1efbcd6SAlok Aggarwal #include <LzmaDec.h> 1357d82f0f8SDina K Nimeh 1367d82f0f8SDina K Nimeh /* 1377d82f0f8SDina K Nimeh * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h. 1387d82f0f8SDina K Nimeh * Crypto metadata, if it exists, is located at the end of the boot block 1397d82f0f8SDina K Nimeh * (BBOFF + BBSIZE, which is SBOFF). The super block and everything after 1407d82f0f8SDina K Nimeh * is offset by the size of the crypto metadata which is handled by 1417d82f0f8SDina K Nimeh * lsp->ls_crypto_offset. 1427d82f0f8SDina K Nimeh */ 1437d82f0f8SDina K Nimeh #define CRYOFF ((off_t)8192) 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks" 1467c478bd9Sstevel@tonic-gate #define SIZE_PROP_NAME "Size" 1477c478bd9Sstevel@tonic-gate 1487d82f0f8SDina K Nimeh #define SETUP_C_DATA(cd, buf, len) \ 1497d82f0f8SDina K Nimeh (cd).cd_format = CRYPTO_DATA_RAW; \ 1507d82f0f8SDina K Nimeh (cd).cd_offset = 0; \ 1517d82f0f8SDina K Nimeh (cd).cd_miscdata = NULL; \ 1527d82f0f8SDina K Nimeh (cd).cd_length = (len); \ 1537d82f0f8SDina K Nimeh (cd).cd_raw.iov_base = (buf); \ 1547d82f0f8SDina K Nimeh (cd).cd_raw.iov_len = (len); 1557d82f0f8SDina K Nimeh 1567d82f0f8SDina K Nimeh #define UIO_CHECK(uio) \ 1577d82f0f8SDina K Nimeh if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \ 1587d82f0f8SDina K Nimeh ((uio)->uio_resid % DEV_BSIZE) != 0) { \ 1597d82f0f8SDina K Nimeh return (EINVAL); \ 1607d82f0f8SDina K Nimeh } 1617d82f0f8SDina K Nimeh 1627d82f0f8SDina K Nimeh static dev_info_t *lofi_dip = NULL; 1637d82f0f8SDina K Nimeh static void *lofi_statep = NULL; 1647c478bd9Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */ 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the 1687c478bd9Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively 1697c478bd9Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy, 1707c478bd9Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of 1717c478bd9Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for 1727c478bd9Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should 1737c478bd9Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the 1747c478bd9Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic 1757c478bd9Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on 1767c478bd9Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE; 1797c478bd9Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES; 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 lofi_busy(void) 2487c478bd9Sstevel@tonic-gate { 2497c478bd9Sstevel@tonic-gate minor_t minor; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * We need to make sure no mappings exist - mod_remove won't 2537c478bd9Sstevel@tonic-gate * help because the device isn't open. 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 2567c478bd9Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 2577c478bd9Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, minor) != NULL) { 2587c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2597c478bd9Sstevel@tonic-gate return (EBUSY); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2637c478bd9Sstevel@tonic-gate return (0); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate static int 2677c478bd9Sstevel@tonic-gate is_opened(struct lofi_state *lsp) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2707c478bd9Sstevel@tonic-gate return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate static int 2747c478bd9Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp) 2757c478bd9Sstevel@tonic-gate { 2767c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2777c478bd9Sstevel@tonic-gate switch (otyp) { 2787c478bd9Sstevel@tonic-gate case OTYP_CHR: 2797c478bd9Sstevel@tonic-gate lsp->ls_chr_open = 1; 2807c478bd9Sstevel@tonic-gate break; 2817c478bd9Sstevel@tonic-gate case OTYP_BLK: 2827c478bd9Sstevel@tonic-gate lsp->ls_blk_open = 1; 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 return (-1); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate return (0); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate static void 2947c478bd9Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp) 2957c478bd9Sstevel@tonic-gate { 2967c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2977c478bd9Sstevel@tonic-gate switch (otyp) { 2987c478bd9Sstevel@tonic-gate case OTYP_CHR: 2997c478bd9Sstevel@tonic-gate lsp->ls_chr_open = 0; 3007c478bd9Sstevel@tonic-gate break; 3017c478bd9Sstevel@tonic-gate case OTYP_BLK: 3027c478bd9Sstevel@tonic-gate lsp->ls_blk_open = 0; 3037c478bd9Sstevel@tonic-gate break; 3047c478bd9Sstevel@tonic-gate case OTYP_LYR: 3057c478bd9Sstevel@tonic-gate lsp->ls_lyr_open_count--; 3067c478bd9Sstevel@tonic-gate break; 3077c478bd9Sstevel@tonic-gate default: 3087c478bd9Sstevel@tonic-gate break; 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3123d7072f8Seschrock static void 3137d82f0f8SDina K Nimeh lofi_free_crypto(struct lofi_state *lsp) 3147d82f0f8SDina K Nimeh { 3157d82f0f8SDina K Nimeh ASSERT(mutex_owned(&lofi_lock)); 3167d82f0f8SDina K Nimeh 3177d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 3187d82f0f8SDina K Nimeh /* 3197d82f0f8SDina K Nimeh * Clean up the crypto state so that it doesn't hang around 3207d82f0f8SDina K Nimeh * in memory after we are done with it. 3217d82f0f8SDina K Nimeh */ 3227d82f0f8SDina K Nimeh bzero(lsp->ls_key.ck_data, 3237d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 3247d82f0f8SDina K Nimeh kmem_free(lsp->ls_key.ck_data, 3257d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 3267d82f0f8SDina K Nimeh lsp->ls_key.ck_data = NULL; 3277d82f0f8SDina K Nimeh lsp->ls_key.ck_length = 0; 3287d82f0f8SDina K Nimeh 3297d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != NULL) { 3307d82f0f8SDina K Nimeh kmem_free(lsp->ls_mech.cm_param, 3317d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len); 3327d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = NULL; 3337d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = 0; 3347d82f0f8SDina K Nimeh } 3357d82f0f8SDina K Nimeh 3367d82f0f8SDina K Nimeh if (lsp->ls_iv_mech.cm_param != NULL) { 3377d82f0f8SDina K Nimeh kmem_free(lsp->ls_iv_mech.cm_param, 3387d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len); 3397d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param = NULL; 3407d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len = 0; 3417d82f0f8SDina K Nimeh } 3427d82f0f8SDina K Nimeh 3437d82f0f8SDina K Nimeh mutex_destroy(&lsp->ls_crypto_lock); 3447d82f0f8SDina K Nimeh } 3457d82f0f8SDina K Nimeh } 3467d82f0f8SDina K Nimeh 3477d82f0f8SDina K Nimeh static void 3483d7072f8Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp, 3493d7072f8Seschrock cred_t *credp) 3503d7072f8Seschrock { 3513d7072f8Seschrock dev_t newdev; 3523d7072f8Seschrock char namebuf[50]; 3533d7072f8Seschrock 3547d82f0f8SDina K Nimeh ASSERT(mutex_owned(&lofi_lock)); 3557d82f0f8SDina K Nimeh 3567d82f0f8SDina K Nimeh lofi_free_crypto(lsp); 3577d82f0f8SDina K Nimeh 3583d7072f8Seschrock if (lsp->ls_vp) { 359da6c28aaSamw (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 360da6c28aaSamw 1, 0, credp, NULL); 3613d7072f8Seschrock VN_RELE(lsp->ls_vp); 3623d7072f8Seschrock lsp->ls_vp = NULL; 3633d7072f8Seschrock } 3643d7072f8Seschrock 3653d7072f8Seschrock newdev = makedevice(getmajor(dev), minor); 3663d7072f8Seschrock (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 3673d7072f8Seschrock (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 3683d7072f8Seschrock 3693d7072f8Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 3703d7072f8Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 3713d7072f8Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 3723d7072f8Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 3733d7072f8Seschrock 3743d7072f8Seschrock kmem_free(lsp->ls_filename, lsp->ls_filename_sz); 3753d7072f8Seschrock taskq_destroy(lsp->ls_taskq); 3763d7072f8Seschrock if (lsp->ls_kstat) { 3773d7072f8Seschrock kstat_delete(lsp->ls_kstat); 3783d7072f8Seschrock mutex_destroy(&lsp->ls_kstat_lock); 3793d7072f8Seschrock } 380a423e759Saalok 3814058a205Sjrgn.keil@googlemail.com /* 3824058a205Sjrgn.keil@googlemail.com * Free cached decompressed segment data 3834058a205Sjrgn.keil@googlemail.com */ 3844058a205Sjrgn.keil@googlemail.com lofi_free_comp_cache(lsp); 3854058a205Sjrgn.keil@googlemail.com list_destroy(&lsp->ls_comp_cache); 3864058a205Sjrgn.keil@googlemail.com mutex_destroy(&lsp->ls_comp_cache_lock); 3874058a205Sjrgn.keil@googlemail.com 388a423e759Saalok if (lsp->ls_uncomp_seg_sz > 0) { 389a423e759Saalok kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 390a423e759Saalok lsp->ls_uncomp_seg_sz = 0; 391a423e759Saalok } 3924058a205Sjrgn.keil@googlemail.com 3934058a205Sjrgn.keil@googlemail.com mutex_destroy(&lsp->ls_vp_lock); 3944058a205Sjrgn.keil@googlemail.com 3953d7072f8Seschrock ddi_soft_state_free(lofi_statep, minor); 3963d7072f8Seschrock } 3973d7072f8Seschrock 3983d7072f8Seschrock /*ARGSUSED*/ 3997c478bd9Sstevel@tonic-gate static int 4007c478bd9Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp) 4017c478bd9Sstevel@tonic-gate { 4027c478bd9Sstevel@tonic-gate minor_t minor; 4037c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 4067c478bd9Sstevel@tonic-gate minor = getminor(*devp); 4077c478bd9Sstevel@tonic-gate if (minor == 0) { 4087c478bd9Sstevel@tonic-gate /* master control device */ 4097c478bd9Sstevel@tonic-gate /* must be opened exclusively */ 4107c478bd9Sstevel@tonic-gate if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) { 4117c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4127c478bd9Sstevel@tonic-gate return (EINVAL); 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, 0); 4157c478bd9Sstevel@tonic-gate if (lsp == NULL) { 4167c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4177c478bd9Sstevel@tonic-gate return (ENXIO); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate if (is_opened(lsp)) { 4207c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4217c478bd9Sstevel@tonic-gate return (EBUSY); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate (void) mark_opened(lsp, OTYP_CHR); 4247c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4257c478bd9Sstevel@tonic-gate return (0); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate /* otherwise, the mapping should already exist */ 4297c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 4307c478bd9Sstevel@tonic-gate if (lsp == NULL) { 4317c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4327c478bd9Sstevel@tonic-gate return (EINVAL); 4337c478bd9Sstevel@tonic-gate } 4347c478bd9Sstevel@tonic-gate 4353d7072f8Seschrock if (lsp->ls_vp == NULL) { 4363d7072f8Seschrock mutex_exit(&lofi_lock); 4373d7072f8Seschrock return (ENXIO); 4383d7072f8Seschrock } 4393d7072f8Seschrock 4407c478bd9Sstevel@tonic-gate if (mark_opened(lsp, otyp) == -1) { 4417c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4427c478bd9Sstevel@tonic-gate return (EINVAL); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4467c478bd9Sstevel@tonic-gate return (0); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4493d7072f8Seschrock /*ARGSUSED*/ 4507c478bd9Sstevel@tonic-gate static int 4517c478bd9Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp) 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate minor_t minor; 4547c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 4577c478bd9Sstevel@tonic-gate minor = getminor(dev); 4587c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 4597c478bd9Sstevel@tonic-gate if (lsp == NULL) { 4607c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4617c478bd9Sstevel@tonic-gate return (EINVAL); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate mark_closed(lsp, otyp); 4643d7072f8Seschrock 4653d7072f8Seschrock /* 46693239addSjohnlev * If we forcibly closed the underlying device (li_force), or 46793239addSjohnlev * asked for cleanup (li_cleanup), finish up if we're the last 46893239addSjohnlev * out of the door. 4693d7072f8Seschrock */ 47093239addSjohnlev if (minor != 0 && !is_opened(lsp) && 47193239addSjohnlev (lsp->ls_cleanup || lsp->ls_vp == NULL)) 4723d7072f8Seschrock lofi_free_handle(dev, minor, lsp, credp); 47393239addSjohnlev 4747c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 4757c478bd9Sstevel@tonic-gate return (0); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787d82f0f8SDina K Nimeh /* 4797d82f0f8SDina K Nimeh * Sets the mechanism's initialization vector (IV) if one is needed. 4807d82f0f8SDina K Nimeh * The IV is computed from the data block number. lsp->ls_mech is 4817d82f0f8SDina K Nimeh * altered so that: 4827d82f0f8SDina K Nimeh * lsp->ls_mech.cm_param_len is set to the IV len. 4837d82f0f8SDina K Nimeh * lsp->ls_mech.cm_param is set to the IV. 4847d82f0f8SDina K Nimeh */ 4857d82f0f8SDina K Nimeh static int 4867d82f0f8SDina K Nimeh lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno) 4877d82f0f8SDina K Nimeh { 4887d82f0f8SDina K Nimeh int ret; 4897d82f0f8SDina K Nimeh crypto_data_t cdata; 4907d82f0f8SDina K Nimeh char *iv; 4917d82f0f8SDina K Nimeh size_t iv_len; 4927d82f0f8SDina K Nimeh size_t min; 4937d82f0f8SDina K Nimeh void *data; 4947d82f0f8SDina K Nimeh size_t datasz; 4957d82f0f8SDina K Nimeh 4967d82f0f8SDina K Nimeh ASSERT(mutex_owned(&lsp->ls_crypto_lock)); 4977d82f0f8SDina K Nimeh 4987d82f0f8SDina K Nimeh if (lsp == NULL) 4997d82f0f8SDina K Nimeh return (CRYPTO_DEVICE_ERROR); 5007d82f0f8SDina K Nimeh 5017d82f0f8SDina K Nimeh /* lsp->ls_mech.cm_param{_len} has already been set for static iv */ 5027d82f0f8SDina K Nimeh if (lsp->ls_iv_type == IVM_NONE) { 5037d82f0f8SDina K Nimeh return (CRYPTO_SUCCESS); 5047d82f0f8SDina K Nimeh } 5057d82f0f8SDina K Nimeh 5067d82f0f8SDina K Nimeh /* 5077d82f0f8SDina K Nimeh * if kmem already alloced from previous call and it's the same size 5087d82f0f8SDina K Nimeh * we need now, just recycle it; allocate new kmem only if we have to 5097d82f0f8SDina K Nimeh */ 5107d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param == NULL || 5117d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len != lsp->ls_iv_len) { 5127d82f0f8SDina K Nimeh iv_len = lsp->ls_iv_len; 5137d82f0f8SDina K Nimeh iv = kmem_zalloc(iv_len, KM_SLEEP); 5147d82f0f8SDina K Nimeh } else { 5157d82f0f8SDina K Nimeh iv_len = lsp->ls_mech.cm_param_len; 5167d82f0f8SDina K Nimeh iv = lsp->ls_mech.cm_param; 5177d82f0f8SDina K Nimeh bzero(iv, iv_len); 5187d82f0f8SDina K Nimeh } 5197d82f0f8SDina K Nimeh 5207d82f0f8SDina K Nimeh switch (lsp->ls_iv_type) { 5217d82f0f8SDina K Nimeh case IVM_ENC_BLKNO: 5227d82f0f8SDina K Nimeh /* iv is not static, lblkno changes each time */ 5237d82f0f8SDina K Nimeh data = &lblkno; 5247d82f0f8SDina K Nimeh datasz = sizeof (lblkno); 5257d82f0f8SDina K Nimeh break; 5267d82f0f8SDina K Nimeh default: 5277d82f0f8SDina K Nimeh data = 0; 5287d82f0f8SDina K Nimeh datasz = 0; 5297d82f0f8SDina K Nimeh break; 5307d82f0f8SDina K Nimeh } 5317d82f0f8SDina K Nimeh 5327d82f0f8SDina K Nimeh /* 5337d82f0f8SDina K Nimeh * write blkno into the iv buffer padded on the left in case 5347d82f0f8SDina K Nimeh * blkno ever grows bigger than its current longlong_t size 5357d82f0f8SDina K Nimeh * or a variation other than blkno is used for the iv data 5367d82f0f8SDina K Nimeh */ 5377d82f0f8SDina K Nimeh min = MIN(datasz, iv_len); 5387d82f0f8SDina K Nimeh bcopy(data, iv + (iv_len - min), min); 5397d82f0f8SDina K Nimeh 5407d82f0f8SDina K Nimeh /* encrypt the data in-place to get the IV */ 5417d82f0f8SDina K Nimeh SETUP_C_DATA(cdata, iv, iv_len); 5427d82f0f8SDina K Nimeh 5437d82f0f8SDina K Nimeh ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key, 5447d82f0f8SDina K Nimeh NULL, NULL, NULL); 5457d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) { 5467d82f0f8SDina K Nimeh cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)", 5477d82f0f8SDina K Nimeh lblkno, ret); 5487d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != iv) 5497d82f0f8SDina K Nimeh kmem_free(iv, iv_len); 550b1efbcd6SAlok Aggarwal 5517d82f0f8SDina K Nimeh return (ret); 5527d82f0f8SDina K Nimeh } 5537d82f0f8SDina K Nimeh 5547d82f0f8SDina K Nimeh /* clean up the iv from the last computation */ 5557d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv) 5567d82f0f8SDina K Nimeh kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len); 557b1efbcd6SAlok Aggarwal 5587d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = iv_len; 5597d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = iv; 5607d82f0f8SDina K Nimeh 5617d82f0f8SDina K Nimeh return (CRYPTO_SUCCESS); 5627d82f0f8SDina K Nimeh } 5637d82f0f8SDina K Nimeh 5647d82f0f8SDina K Nimeh /* 5657d82f0f8SDina K Nimeh * Performs encryption and decryption of a chunk of data of size "len", 5667d82f0f8SDina K Nimeh * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of 5677d82f0f8SDina K Nimeh * DEV_BSIZE. 5687d82f0f8SDina K Nimeh */ 5697d82f0f8SDina K Nimeh static int 5707d82f0f8SDina K Nimeh lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext, 5717d82f0f8SDina K Nimeh caddr_t ciphertext, size_t len, boolean_t op_encrypt) 5727d82f0f8SDina K Nimeh { 5737d82f0f8SDina K Nimeh crypto_data_t cdata; 5747d82f0f8SDina K Nimeh crypto_data_t wdata; 5757d82f0f8SDina K Nimeh int ret; 5767d82f0f8SDina K Nimeh longlong_t lblkno = bp->b_lblkno; 5777d82f0f8SDina K Nimeh 5787d82f0f8SDina K Nimeh mutex_enter(&lsp->ls_crypto_lock); 5797d82f0f8SDina K Nimeh 5807d82f0f8SDina K Nimeh /* 5817d82f0f8SDina K Nimeh * though we could encrypt/decrypt entire "len" chunk of data, we need 5827d82f0f8SDina K Nimeh * to break it into DEV_BSIZE pieces to capture blkno incrementing 5837d82f0f8SDina K Nimeh */ 5847d82f0f8SDina K Nimeh SETUP_C_DATA(cdata, plaintext, len); 5857d82f0f8SDina K Nimeh cdata.cd_length = DEV_BSIZE; 5867d82f0f8SDina K Nimeh if (ciphertext != NULL) { /* not in-place crypto */ 5877d82f0f8SDina K Nimeh SETUP_C_DATA(wdata, ciphertext, len); 5887d82f0f8SDina K Nimeh wdata.cd_length = DEV_BSIZE; 5897d82f0f8SDina K Nimeh } 5907d82f0f8SDina K Nimeh 5917d82f0f8SDina K Nimeh do { 5927d82f0f8SDina K Nimeh ret = lofi_blk_mech(lsp, lblkno); 5937d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) 5947d82f0f8SDina K Nimeh continue; 5957d82f0f8SDina K Nimeh 5967d82f0f8SDina K Nimeh if (op_encrypt) { 5977d82f0f8SDina K Nimeh ret = crypto_encrypt(&lsp->ls_mech, &cdata, 5987d82f0f8SDina K Nimeh &lsp->ls_key, NULL, 5997d82f0f8SDina K Nimeh ((ciphertext != NULL) ? &wdata : NULL), NULL); 6007d82f0f8SDina K Nimeh } else { 6017d82f0f8SDina K Nimeh ret = crypto_decrypt(&lsp->ls_mech, &cdata, 6027d82f0f8SDina K Nimeh &lsp->ls_key, NULL, 6037d82f0f8SDina K Nimeh ((ciphertext != NULL) ? &wdata : NULL), NULL); 6047d82f0f8SDina K Nimeh } 6057d82f0f8SDina K Nimeh 6067d82f0f8SDina K Nimeh cdata.cd_offset += DEV_BSIZE; 6077d82f0f8SDina K Nimeh if (ciphertext != NULL) 6087d82f0f8SDina K Nimeh wdata.cd_offset += DEV_BSIZE; 6097d82f0f8SDina K Nimeh lblkno++; 6107d82f0f8SDina K Nimeh } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len); 6117d82f0f8SDina K Nimeh 6127d82f0f8SDina K Nimeh mutex_exit(&lsp->ls_crypto_lock); 6137d82f0f8SDina K Nimeh 6147d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) { 6157d82f0f8SDina K Nimeh cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)", 6167d82f0f8SDina K Nimeh op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()", 6177d82f0f8SDina K Nimeh lblkno, ret); 6187d82f0f8SDina K Nimeh } 6197d82f0f8SDina K Nimeh 6207d82f0f8SDina K Nimeh return (ret); 6217d82f0f8SDina K Nimeh } 6227d82f0f8SDina K Nimeh 6237d82f0f8SDina K Nimeh #define RDWR_RAW 1 6247d82f0f8SDina K Nimeh #define RDWR_BCOPY 2 6257d82f0f8SDina K Nimeh 6267d82f0f8SDina K Nimeh static int 6277d82f0f8SDina K Nimeh lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 6287d82f0f8SDina K Nimeh struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn) 6297d82f0f8SDina K Nimeh { 6307d82f0f8SDina K Nimeh ssize_t resid; 6317d82f0f8SDina K Nimeh int isread; 6327d82f0f8SDina K Nimeh int error; 6337d82f0f8SDina K Nimeh 6347d82f0f8SDina K Nimeh /* 6357d82f0f8SDina K Nimeh * Handles reads/writes for both plain and encrypted lofi 6367d82f0f8SDina K Nimeh * Note: offset is already shifted by lsp->ls_crypto_offset 6377d82f0f8SDina K Nimeh * when it gets here. 6387d82f0f8SDina K Nimeh */ 6397d82f0f8SDina K Nimeh 6407d82f0f8SDina K Nimeh isread = bp->b_flags & B_READ; 6417d82f0f8SDina K Nimeh if (isread) { 6427d82f0f8SDina K Nimeh if (method == RDWR_BCOPY) { 6437d82f0f8SDina K Nimeh /* DO NOT update bp->b_resid for bcopy */ 6447d82f0f8SDina K Nimeh bcopy(bcopy_locn, bufaddr, len); 6457d82f0f8SDina K Nimeh error = 0; 6467d82f0f8SDina K Nimeh } else { /* RDWR_RAW */ 6477d82f0f8SDina K Nimeh error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len, 6487d82f0f8SDina K Nimeh offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 6497d82f0f8SDina K Nimeh &resid); 6507d82f0f8SDina K Nimeh bp->b_resid = resid; 6517d82f0f8SDina K Nimeh } 6527d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled && error == 0) { 6537d82f0f8SDina K Nimeh if (lofi_crypto(lsp, bp, bufaddr, NULL, len, 6547d82f0f8SDina K Nimeh B_FALSE) != CRYPTO_SUCCESS) { 6557d82f0f8SDina K Nimeh /* 6567d82f0f8SDina K Nimeh * XXX: original code didn't set residual 6577d82f0f8SDina K Nimeh * back to len because no error was expected 6587d82f0f8SDina K Nimeh * from bcopy() if encryption is not enabled 6597d82f0f8SDina K Nimeh */ 6607d82f0f8SDina K Nimeh if (method != RDWR_BCOPY) 6617d82f0f8SDina K Nimeh bp->b_resid = len; 6627d82f0f8SDina K Nimeh error = EIO; 6637d82f0f8SDina K Nimeh } 6647d82f0f8SDina K Nimeh } 6657d82f0f8SDina K Nimeh return (error); 6667d82f0f8SDina K Nimeh } else { 6677d82f0f8SDina K Nimeh void *iobuf = bufaddr; 6687d82f0f8SDina K Nimeh 6697d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 6707d82f0f8SDina K Nimeh /* don't do in-place crypto to keep bufaddr intact */ 6717d82f0f8SDina K Nimeh iobuf = kmem_alloc(len, KM_SLEEP); 6727d82f0f8SDina K Nimeh if (lofi_crypto(lsp, bp, bufaddr, iobuf, len, 6737d82f0f8SDina K Nimeh B_TRUE) != CRYPTO_SUCCESS) { 6747d82f0f8SDina K Nimeh kmem_free(iobuf, len); 6757d82f0f8SDina K Nimeh if (method != RDWR_BCOPY) 6767d82f0f8SDina K Nimeh bp->b_resid = len; 6777d82f0f8SDina K Nimeh return (EIO); 6787d82f0f8SDina K Nimeh } 6797d82f0f8SDina K Nimeh } 6807d82f0f8SDina K Nimeh if (method == RDWR_BCOPY) { 6817d82f0f8SDina K Nimeh /* DO NOT update bp->b_resid for bcopy */ 6827d82f0f8SDina K Nimeh bcopy(iobuf, bcopy_locn, len); 6837d82f0f8SDina K Nimeh error = 0; 6847d82f0f8SDina K Nimeh } else { /* RDWR_RAW */ 6857d82f0f8SDina K Nimeh error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len, 6867d82f0f8SDina K Nimeh offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 6877d82f0f8SDina K Nimeh &resid); 6887d82f0f8SDina K Nimeh bp->b_resid = resid; 6897d82f0f8SDina K Nimeh } 6907d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 6917d82f0f8SDina K Nimeh kmem_free(iobuf, len); 6927d82f0f8SDina K Nimeh } 6937d82f0f8SDina K Nimeh return (error); 6947d82f0f8SDina K Nimeh } 6957d82f0f8SDina K Nimeh } 6967d82f0f8SDina K Nimeh 69787117650Saalok static int 69887117650Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 69987117650Saalok struct lofi_state *lsp) 7007c478bd9Sstevel@tonic-gate { 7017c478bd9Sstevel@tonic-gate int error; 70287117650Saalok offset_t alignedoffset, mapoffset; 7037c478bd9Sstevel@tonic-gate size_t xfersize; 7047c478bd9Sstevel@tonic-gate int isread; 7057c478bd9Sstevel@tonic-gate int smflags; 70687117650Saalok caddr_t mapaddr; 70787117650Saalok size_t len; 7087c478bd9Sstevel@tonic-gate enum seg_rw srw; 7097d82f0f8SDina K Nimeh int save_error; 7107d82f0f8SDina K Nimeh 7117d82f0f8SDina K Nimeh /* 7127d82f0f8SDina K Nimeh * Note: offset is already shifted by lsp->ls_crypto_offset 7137d82f0f8SDina K Nimeh * when it gets here. 7147d82f0f8SDina K Nimeh */ 7157d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) 7167d82f0f8SDina K Nimeh ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size); 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate /* 7197c478bd9Sstevel@tonic-gate * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 7207c478bd9Sstevel@tonic-gate * an 8K boundary, but the buf transfer address may not be 72187117650Saalok * aligned on more than a 512-byte boundary (we don't enforce 72287117650Saalok * that even though we could). This matters since the initial 72387117650Saalok * part of the transfer may not start at offset 0 within the 72487117650Saalok * segmap'd chunk. So we have to compensate for that with 72587117650Saalok * 'mapoffset'. Subsequent chunks always start off at the 72687117650Saalok * beginning, and the last is capped by b_resid 7277d82f0f8SDina K Nimeh * 7287d82f0f8SDina K Nimeh * Visually, where "|" represents page map boundaries: 7297d82f0f8SDina K Nimeh * alignedoffset (mapaddr begins at this segmap boundary) 7307d82f0f8SDina K Nimeh * | offset (from beginning of file) 7317d82f0f8SDina K Nimeh * | | len 7327d82f0f8SDina K Nimeh * v v v 7337d82f0f8SDina K Nimeh * ===|====X========|====...======|========X====|==== 7347d82f0f8SDina K Nimeh * /-------------...---------------/ 7357d82f0f8SDina K Nimeh * ^ bp->b_bcount/bp->b_resid at start 7367d82f0f8SDina K Nimeh * /----/--------/----...------/--------/ 7377d82f0f8SDina K Nimeh * ^ ^ ^ ^ ^ 7387d82f0f8SDina K Nimeh * | | | | nth xfersize (<= MAXBSIZE) 7397d82f0f8SDina K Nimeh * | | 2nd thru n-1st xfersize (= MAXBSIZE) 7407d82f0f8SDina K Nimeh * | 1st xfersize (<= MAXBSIZE) 7417d82f0f8SDina K Nimeh * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter) 7427d82f0f8SDina K Nimeh * 7437d82f0f8SDina K Nimeh * Notes: "alignedoffset" is "offset" rounded down to nearest 7447d82f0f8SDina K Nimeh * MAXBSIZE boundary. "len" is next page boundary of size 745a363f650SDina K Nimeh * PAGESIZE after "alignedoffset". 7467c478bd9Sstevel@tonic-gate */ 7477c478bd9Sstevel@tonic-gate mapoffset = offset & MAXBOFFSET; 74887117650Saalok alignedoffset = offset - mapoffset; 7497c478bd9Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 7507c478bd9Sstevel@tonic-gate isread = bp->b_flags & B_READ; 7517c478bd9Sstevel@tonic-gate srw = isread ? S_READ : S_WRITE; 7527c478bd9Sstevel@tonic-gate do { 75387117650Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset, 7547c478bd9Sstevel@tonic-gate MIN(MAXBSIZE - mapoffset, bp->b_resid)); 755a363f650SDina K Nimeh len = roundup(mapoffset + xfersize, PAGESIZE); 7567c478bd9Sstevel@tonic-gate mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 7577c478bd9Sstevel@tonic-gate alignedoffset, MAXBSIZE, 1, srw); 7587c478bd9Sstevel@tonic-gate /* 7597c478bd9Sstevel@tonic-gate * Now fault in the pages. This lets us check 7607c478bd9Sstevel@tonic-gate * for errors before we reference mapaddr and 7617c478bd9Sstevel@tonic-gate * try to resolve the fault in bcopy (which would 7627c478bd9Sstevel@tonic-gate * panic instead). And this can easily happen, 7637c478bd9Sstevel@tonic-gate * particularly if you've lofi'd a file over NFS 7647c478bd9Sstevel@tonic-gate * and someone deletes the file on the server. 7657c478bd9Sstevel@tonic-gate */ 7667c478bd9Sstevel@tonic-gate error = segmap_fault(kas.a_hat, segkmap, mapaddr, 7677c478bd9Sstevel@tonic-gate len, F_SOFTLOCK, srw); 7687c478bd9Sstevel@tonic-gate if (error) { 7697c478bd9Sstevel@tonic-gate (void) segmap_release(segkmap, mapaddr, 0); 7707c478bd9Sstevel@tonic-gate if (FC_CODE(error) == FC_OBJERR) 7717c478bd9Sstevel@tonic-gate error = FC_ERRNO(error); 7727c478bd9Sstevel@tonic-gate else 7737c478bd9Sstevel@tonic-gate error = EIO; 7747c478bd9Sstevel@tonic-gate break; 7757c478bd9Sstevel@tonic-gate } 7767d82f0f8SDina K Nimeh /* error may be non-zero for encrypted lofi */ 7777d82f0f8SDina K Nimeh error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize, 7787d82f0f8SDina K Nimeh RDWR_BCOPY, mapaddr + mapoffset); 7797d82f0f8SDina K Nimeh if (error == 0) { 7807d82f0f8SDina K Nimeh bp->b_resid -= xfersize; 7817d82f0f8SDina K Nimeh bufaddr += xfersize; 7827d82f0f8SDina K Nimeh offset += xfersize; 7837d82f0f8SDina K Nimeh } 7847c478bd9Sstevel@tonic-gate smflags = 0; 7857c478bd9Sstevel@tonic-gate if (isread) { 78687117650Saalok smflags |= SM_FREE; 78787117650Saalok /* 78887117650Saalok * If we're reading an entire page starting 78987117650Saalok * at a page boundary, there's a good chance 79087117650Saalok * we won't need it again. Put it on the 79187117650Saalok * head of the freelist. 79287117650Saalok */ 793bbd65dd2SDina K Nimeh if (mapoffset == 0 && xfersize == MAXBSIZE) 79487117650Saalok smflags |= SM_DONTNEED; 7957c478bd9Sstevel@tonic-gate } else { 796*44a1e32bSbatschul /* 797*44a1e32bSbatschul * Write back good pages, it is okay to 798*44a1e32bSbatschul * always release asynchronous here as we'll 799*44a1e32bSbatschul * follow with VOP_FSYNC for B_SYNC buffers. 800*44a1e32bSbatschul */ 801*44a1e32bSbatschul if (error == 0) 802*44a1e32bSbatschul smflags |= SM_WRITE | SM_ASYNC; 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 8057c478bd9Sstevel@tonic-gate len, F_SOFTUNLOCK, srw); 8067d82f0f8SDina K Nimeh save_error = segmap_release(segkmap, mapaddr, smflags); 8077d82f0f8SDina K Nimeh if (error == 0) 8087d82f0f8SDina K Nimeh error = save_error; 8097c478bd9Sstevel@tonic-gate /* only the first map may start partial */ 8107c478bd9Sstevel@tonic-gate mapoffset = 0; 8117c478bd9Sstevel@tonic-gate alignedoffset += MAXBSIZE; 8127c478bd9Sstevel@tonic-gate } while ((error == 0) && (bp->b_resid > 0) && 81387117650Saalok (offset < lsp->ls_vp_comp_size)); 81487117650Saalok 81587117650Saalok return (error); 81687117650Saalok } 81787117650Saalok 8184058a205Sjrgn.keil@googlemail.com /* 8194058a205Sjrgn.keil@googlemail.com * Check if segment seg_index is present in the decompressed segment 8204058a205Sjrgn.keil@googlemail.com * data cache. 8214058a205Sjrgn.keil@googlemail.com * 8224058a205Sjrgn.keil@googlemail.com * Returns a pointer to the decompressed segment data cache entry if 8234058a205Sjrgn.keil@googlemail.com * found, and NULL when decompressed data for this segment is not yet 8244058a205Sjrgn.keil@googlemail.com * cached. 8254058a205Sjrgn.keil@googlemail.com */ 8264058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache * 8274058a205Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index) 8284058a205Sjrgn.keil@googlemail.com { 8294058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 8304058a205Sjrgn.keil@googlemail.com 8314058a205Sjrgn.keil@googlemail.com ASSERT(mutex_owned(&lsp->ls_comp_cache_lock)); 8324058a205Sjrgn.keil@googlemail.com 8334058a205Sjrgn.keil@googlemail.com for (lc = list_head(&lsp->ls_comp_cache); lc != NULL; 8344058a205Sjrgn.keil@googlemail.com lc = list_next(&lsp->ls_comp_cache, lc)) { 8354058a205Sjrgn.keil@googlemail.com if (lc->lc_index == seg_index) { 8364058a205Sjrgn.keil@googlemail.com /* 8374058a205Sjrgn.keil@googlemail.com * Decompressed segment data was found in the 8384058a205Sjrgn.keil@googlemail.com * cache. 8394058a205Sjrgn.keil@googlemail.com * 8404058a205Sjrgn.keil@googlemail.com * The cache uses an LRU replacement strategy; 8414058a205Sjrgn.keil@googlemail.com * move the entry to head of list. 8424058a205Sjrgn.keil@googlemail.com */ 8434058a205Sjrgn.keil@googlemail.com list_remove(&lsp->ls_comp_cache, lc); 8444058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc); 8454058a205Sjrgn.keil@googlemail.com return (lc); 8464058a205Sjrgn.keil@googlemail.com } 8474058a205Sjrgn.keil@googlemail.com } 8484058a205Sjrgn.keil@googlemail.com return (NULL); 8494058a205Sjrgn.keil@googlemail.com } 8504058a205Sjrgn.keil@googlemail.com 8514058a205Sjrgn.keil@googlemail.com /* 8524058a205Sjrgn.keil@googlemail.com * Add the data for a decompressed segment at segment index 8534058a205Sjrgn.keil@googlemail.com * seg_index to the cache of the decompressed segments. 8544058a205Sjrgn.keil@googlemail.com * 8554058a205Sjrgn.keil@googlemail.com * Returns a pointer to the cache element structure in case 8564058a205Sjrgn.keil@googlemail.com * the data was added to the cache; returns NULL when the data 8574058a205Sjrgn.keil@googlemail.com * wasn't cached. 8584058a205Sjrgn.keil@googlemail.com */ 8594058a205Sjrgn.keil@googlemail.com static struct lofi_comp_cache * 8604058a205Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index, 8614058a205Sjrgn.keil@googlemail.com uchar_t *data) 8624058a205Sjrgn.keil@googlemail.com { 8634058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 8644058a205Sjrgn.keil@googlemail.com 8654058a205Sjrgn.keil@googlemail.com ASSERT(mutex_owned(&lsp->ls_comp_cache_lock)); 8664058a205Sjrgn.keil@googlemail.com 8674058a205Sjrgn.keil@googlemail.com while (lsp->ls_comp_cache_count > lofi_max_comp_cache) { 8684058a205Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache); 8694058a205Sjrgn.keil@googlemail.com ASSERT(lc != NULL); 8704058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz); 8714058a205Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache)); 8724058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--; 8734058a205Sjrgn.keil@googlemail.com } 8744058a205Sjrgn.keil@googlemail.com 8754058a205Sjrgn.keil@googlemail.com /* 8764058a205Sjrgn.keil@googlemail.com * Do not cache when disabled by tunable variable 8774058a205Sjrgn.keil@googlemail.com */ 8784058a205Sjrgn.keil@googlemail.com if (lofi_max_comp_cache == 0) 8794058a205Sjrgn.keil@googlemail.com return (NULL); 8804058a205Sjrgn.keil@googlemail.com 8814058a205Sjrgn.keil@googlemail.com /* 8824058a205Sjrgn.keil@googlemail.com * When the cache has not yet reached the maximum allowed 8834058a205Sjrgn.keil@googlemail.com * number of segments, allocate a new cache element. 8844058a205Sjrgn.keil@googlemail.com * Otherwise the cache is full; reuse the last list element 8854058a205Sjrgn.keil@googlemail.com * (LRU) for caching the decompressed segment data. 8864058a205Sjrgn.keil@googlemail.com * 8874058a205Sjrgn.keil@googlemail.com * The cache element for the new decompressed segment data is 8884058a205Sjrgn.keil@googlemail.com * added to the head of the list. 8894058a205Sjrgn.keil@googlemail.com */ 8904058a205Sjrgn.keil@googlemail.com if (lsp->ls_comp_cache_count < lofi_max_comp_cache) { 8914058a205Sjrgn.keil@googlemail.com lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP); 8924058a205Sjrgn.keil@googlemail.com lc->lc_data = NULL; 8934058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc); 8944058a205Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count++; 8954058a205Sjrgn.keil@googlemail.com } else { 8964058a205Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache); 8974058a205Sjrgn.keil@googlemail.com if (lc == NULL) 8984058a205Sjrgn.keil@googlemail.com return (NULL); 8994058a205Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc); 9004058a205Sjrgn.keil@googlemail.com } 9014058a205Sjrgn.keil@googlemail.com 9024058a205Sjrgn.keil@googlemail.com /* 9034058a205Sjrgn.keil@googlemail.com * Free old uncompressed segment data when reusing a cache 9044058a205Sjrgn.keil@googlemail.com * entry. 9054058a205Sjrgn.keil@googlemail.com */ 9064058a205Sjrgn.keil@googlemail.com if (lc->lc_data != NULL) 9074058a205Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz); 9084058a205Sjrgn.keil@googlemail.com 9094058a205Sjrgn.keil@googlemail.com lc->lc_data = data; 9104058a205Sjrgn.keil@googlemail.com lc->lc_index = seg_index; 9114058a205Sjrgn.keil@googlemail.com return (lc); 9124058a205Sjrgn.keil@googlemail.com } 9134058a205Sjrgn.keil@googlemail.com 9144058a205Sjrgn.keil@googlemail.com 91587117650Saalok /*ARGSUSED*/ 916b1efbcd6SAlok Aggarwal static int 917b1efbcd6SAlok Aggarwal gzip_decompress(void *src, size_t srclen, void *dst, 91887117650Saalok size_t *dstlen, int level) 91987117650Saalok { 92087117650Saalok ASSERT(*dstlen >= srclen); 92187117650Saalok 92287117650Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 92387117650Saalok return (-1); 92487117650Saalok return (0); 92587117650Saalok } 92687117650Saalok 927b1efbcd6SAlok Aggarwal #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8) 928b1efbcd6SAlok Aggarwal /*ARGSUSED*/ 929b1efbcd6SAlok Aggarwal static int 930b1efbcd6SAlok Aggarwal lzma_decompress(void *src, size_t srclen, void *dst, 931b1efbcd6SAlok Aggarwal size_t *dstlen, int level) 932b1efbcd6SAlok Aggarwal { 933b1efbcd6SAlok Aggarwal size_t insizepure; 934b1efbcd6SAlok Aggarwal void *actual_src; 935b1efbcd6SAlok Aggarwal ELzmaStatus status; 936b1efbcd6SAlok Aggarwal 937b1efbcd6SAlok Aggarwal insizepure = srclen - LZMA_HEADER_SIZE; 938b1efbcd6SAlok Aggarwal actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE); 939b1efbcd6SAlok Aggarwal 940b1efbcd6SAlok Aggarwal if (LzmaDecode((Byte *)dst, (size_t *)dstlen, 941b1efbcd6SAlok Aggarwal (const Byte *)actual_src, &insizepure, 942b1efbcd6SAlok Aggarwal (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, 943b1efbcd6SAlok Aggarwal &g_Alloc) != SZ_OK) { 944b1efbcd6SAlok Aggarwal return (-1); 945b1efbcd6SAlok Aggarwal } 946b1efbcd6SAlok Aggarwal return (0); 947b1efbcd6SAlok Aggarwal } 948b1efbcd6SAlok Aggarwal 94987117650Saalok /* 95087117650Saalok * This is basically what strategy used to be before we found we 95187117650Saalok * needed task queues. 95287117650Saalok */ 95387117650Saalok static void 95487117650Saalok lofi_strategy_task(void *arg) 95587117650Saalok { 95687117650Saalok struct buf *bp = (struct buf *)arg; 95787117650Saalok int error; 958*44a1e32bSbatschul int syncflag = 0; 95987117650Saalok struct lofi_state *lsp; 9607d82f0f8SDina K Nimeh offset_t offset; 96187117650Saalok caddr_t bufaddr; 9627d82f0f8SDina K Nimeh size_t len; 9637d82f0f8SDina K Nimeh size_t xfersize; 9647d82f0f8SDina K Nimeh boolean_t bufinited = B_FALSE; 96587117650Saalok 96687117650Saalok lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 9677d82f0f8SDina K Nimeh if (lsp == NULL) { 9687d82f0f8SDina K Nimeh error = ENXIO; 9697d82f0f8SDina K Nimeh goto errout; 9707d82f0f8SDina K Nimeh } 97187117650Saalok if (lsp->ls_kstat) { 97287117650Saalok mutex_enter(lsp->ls_kstat->ks_lock); 97387117650Saalok kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 97487117650Saalok mutex_exit(lsp->ls_kstat->ks_lock); 97587117650Saalok } 97687117650Saalok bp_mapin(bp); 97787117650Saalok bufaddr = bp->b_un.b_addr; 97887117650Saalok offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 9797d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 9807d82f0f8SDina K Nimeh /* encrypted data really begins after crypto header */ 9817d82f0f8SDina K Nimeh offset += lsp->ls_crypto_offset; 9827d82f0f8SDina K Nimeh } 9837d82f0f8SDina K Nimeh len = bp->b_bcount; 9847d82f0f8SDina K Nimeh bufinited = B_TRUE; 9857d82f0f8SDina K Nimeh 9867d82f0f8SDina K Nimeh if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 9877d82f0f8SDina K Nimeh error = EIO; 9887d82f0f8SDina K Nimeh goto errout; 9897d82f0f8SDina K Nimeh } 99087117650Saalok 99187117650Saalok /* 992*44a1e32bSbatschul * If we're writing and the buffer was not B_ASYNC 993*44a1e32bSbatschul * we'll follow up with a VOP_FSYNC() to force any 994*44a1e32bSbatschul * asynchronous I/O to stable storage. 995*44a1e32bSbatschul */ 996*44a1e32bSbatschul if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC)) 997*44a1e32bSbatschul syncflag = FSYNC; 998*44a1e32bSbatschul 999*44a1e32bSbatschul /* 100087117650Saalok * We used to always use vn_rdwr here, but we cannot do that because 100187117650Saalok * we might decide to read or write from the the underlying 100287117650Saalok * file during this call, which would be a deadlock because 100387117650Saalok * we have the rw_lock. So instead we page, unless it's not 10047d82f0f8SDina K Nimeh * mapable or it's a character device or it's an encrypted lofi. 100587117650Saalok */ 10067d82f0f8SDina K Nimeh if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) || 10077d82f0f8SDina K Nimeh lsp->ls_crypto_enabled) { 10087d82f0f8SDina K Nimeh error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW, 10097d82f0f8SDina K Nimeh NULL); 10107d82f0f8SDina K Nimeh } else if (lsp->ls_uncomp_seg_sz == 0) { 101187117650Saalok error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 10127d82f0f8SDina K Nimeh } else { 10134058a205Sjrgn.keil@googlemail.com uchar_t *compressed_seg = NULL, *cmpbuf; 10144058a205Sjrgn.keil@googlemail.com uchar_t *uncompressed_seg = NULL; 10157d82f0f8SDina K Nimeh lofi_compress_info_t *li; 10167d82f0f8SDina K Nimeh size_t oblkcount; 10174058a205Sjrgn.keil@googlemail.com ulong_t seglen; 10187d82f0f8SDina K Nimeh uint64_t sblkno, eblkno, cmpbytes; 10194058a205Sjrgn.keil@googlemail.com uint64_t uncompressed_seg_index; 10204058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc; 10217d82f0f8SDina K Nimeh offset_t sblkoff, eblkoff; 10227d82f0f8SDina K Nimeh u_offset_t salign, ealign; 10237d82f0f8SDina K Nimeh u_offset_t sdiff; 10247d82f0f8SDina K Nimeh uint32_t comp_data_sz; 10257d82f0f8SDina K Nimeh uint64_t i; 102687117650Saalok 102787117650Saalok /* 102887117650Saalok * From here on we're dealing primarily with compressed files 102987117650Saalok */ 10307d82f0f8SDina K Nimeh ASSERT(!lsp->ls_crypto_enabled); 103187117650Saalok 103287117650Saalok /* 103387117650Saalok * Compressed files can only be read from and 103487117650Saalok * not written to 103587117650Saalok */ 103687117650Saalok if (!(bp->b_flags & B_READ)) { 103787117650Saalok bp->b_resid = bp->b_bcount; 103887117650Saalok error = EROFS; 103987117650Saalok goto done; 104087117650Saalok } 104187117650Saalok 104287117650Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0); 104387117650Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 104487117650Saalok /* 104587117650Saalok * Compute starting and ending compressed segment numbers 104687117650Saalok * We use only bitwise operations avoiding division and 104787117650Saalok * modulus because we enforce the compression segment size 104887117650Saalok * to a power of 2 104987117650Saalok */ 105087117650Saalok sblkno = offset >> lsp->ls_comp_seg_shift; 105187117650Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 105287117650Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 105387117650Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 105487117650Saalok 105587117650Saalok /* 10564058a205Sjrgn.keil@googlemail.com * Check the decompressed segment cache. 10574058a205Sjrgn.keil@googlemail.com * 10584058a205Sjrgn.keil@googlemail.com * The cache is used only when the requested data 10594058a205Sjrgn.keil@googlemail.com * is within a segment. Requests that cross 10604058a205Sjrgn.keil@googlemail.com * segment boundaries bypass the cache. 10614058a205Sjrgn.keil@googlemail.com */ 10624058a205Sjrgn.keil@googlemail.com if (sblkno == eblkno || 10634058a205Sjrgn.keil@googlemail.com (sblkno + 1 == eblkno && eblkoff == 0)) { 10644058a205Sjrgn.keil@googlemail.com /* 10654058a205Sjrgn.keil@googlemail.com * Request doesn't cross a segment boundary, 10664058a205Sjrgn.keil@googlemail.com * now check the cache. 10674058a205Sjrgn.keil@googlemail.com */ 10684058a205Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock); 10694058a205Sjrgn.keil@googlemail.com lc = lofi_find_comp_data(lsp, sblkno); 10704058a205Sjrgn.keil@googlemail.com if (lc != NULL) { 10714058a205Sjrgn.keil@googlemail.com /* 10724058a205Sjrgn.keil@googlemail.com * We've found the decompressed segment 10734058a205Sjrgn.keil@googlemail.com * data in the cache; reuse it. 10744058a205Sjrgn.keil@googlemail.com */ 10754058a205Sjrgn.keil@googlemail.com bcopy(lc->lc_data + sblkoff, bufaddr, 10764058a205Sjrgn.keil@googlemail.com bp->b_bcount); 10774058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock); 10784058a205Sjrgn.keil@googlemail.com bp->b_resid = 0; 10794058a205Sjrgn.keil@googlemail.com error = 0; 10804058a205Sjrgn.keil@googlemail.com goto done; 10814058a205Sjrgn.keil@googlemail.com } 10824058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock); 10834058a205Sjrgn.keil@googlemail.com } 10844058a205Sjrgn.keil@googlemail.com 10854058a205Sjrgn.keil@googlemail.com /* 108687117650Saalok * Align start offset to block boundary for segmap 108787117650Saalok */ 108887117650Saalok salign = lsp->ls_comp_seg_index[sblkno]; 108987117650Saalok sdiff = salign & (DEV_BSIZE - 1); 109087117650Saalok salign -= sdiff; 109187117650Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 109287117650Saalok /* 109387117650Saalok * We're dealing with the last segment of 109487117650Saalok * the compressed file -- the size of this 109587117650Saalok * segment *may not* be the same as the 109687117650Saalok * segment size for the file 109787117650Saalok */ 109887117650Saalok eblkoff = (offset + bp->b_bcount) & 109987117650Saalok (lsp->ls_uncomp_last_seg_sz - 1); 110087117650Saalok ealign = lsp->ls_vp_comp_size; 110187117650Saalok } else { 110287117650Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1]; 110387117650Saalok } 110487117650Saalok 110587117650Saalok /* 110687117650Saalok * Preserve original request paramaters 110787117650Saalok */ 110887117650Saalok oblkcount = bp->b_bcount; 110987117650Saalok 111087117650Saalok /* 111187117650Saalok * Assign the calculated parameters 111287117650Saalok */ 111387117650Saalok comp_data_sz = ealign - salign; 111487117650Saalok bp->b_bcount = comp_data_sz; 111587117650Saalok 111687117650Saalok /* 111787117650Saalok * Allocate fixed size memory blocks to hold compressed 111887117650Saalok * segments and one uncompressed segment since we 111987117650Saalok * uncompress segments one at a time 112087117650Saalok */ 112187117650Saalok compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP); 112287117650Saalok uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP); 112387117650Saalok /* 112487117650Saalok * Map in the calculated number of blocks 112587117650Saalok */ 112687117650Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 112787117650Saalok bp, lsp); 112887117650Saalok 112987117650Saalok bp->b_bcount = oblkcount; 113087117650Saalok bp->b_resid = oblkcount; 113187117650Saalok if (error != 0) 113287117650Saalok goto done; 113387117650Saalok 113487117650Saalok /* 113587117650Saalok * We have the compressed blocks, now uncompress them 113687117650Saalok */ 113787117650Saalok cmpbuf = compressed_seg + sdiff; 1138b1efbcd6SAlok Aggarwal for (i = sblkno; i <= eblkno; i++) { 1139b1efbcd6SAlok Aggarwal ASSERT(i < lsp->ls_comp_index_sz - 1); 1140b1efbcd6SAlok Aggarwal 1141b1efbcd6SAlok Aggarwal /* 1142b1efbcd6SAlok Aggarwal * The last segment is special in that it is 1143b1efbcd6SAlok Aggarwal * most likely not going to be the same 1144b1efbcd6SAlok Aggarwal * (uncompressed) size as the other segments. 1145b1efbcd6SAlok Aggarwal */ 1146b1efbcd6SAlok Aggarwal if (i == (lsp->ls_comp_index_sz - 2)) { 1147b1efbcd6SAlok Aggarwal seglen = lsp->ls_uncomp_last_seg_sz; 1148b1efbcd6SAlok Aggarwal } else { 1149b1efbcd6SAlok Aggarwal seglen = lsp->ls_uncomp_seg_sz; 1150b1efbcd6SAlok Aggarwal } 1151b1efbcd6SAlok Aggarwal 115287117650Saalok /* 115387117650Saalok * Each of the segment index entries contains 115487117650Saalok * the starting block number for that segment. 115587117650Saalok * The number of compressed bytes in a segment 115687117650Saalok * is thus the difference between the starting 115787117650Saalok * block number of this segment and the starting 115887117650Saalok * block number of the next segment. 115987117650Saalok */ 116087117650Saalok cmpbytes = lsp->ls_comp_seg_index[i + 1] - 116187117650Saalok lsp->ls_comp_seg_index[i]; 116287117650Saalok 116387117650Saalok /* 116487117650Saalok * The first byte in a compressed segment is a flag 116587117650Saalok * that indicates whether this segment is compressed 116687117650Saalok * at all 116787117650Saalok */ 116887117650Saalok if (*cmpbuf == UNCOMPRESSED) { 116987117650Saalok bcopy((cmpbuf + SEGHDR), uncompressed_seg, 117087117650Saalok (cmpbytes - SEGHDR)); 117187117650Saalok } else { 117287117650Saalok if (li->l_decompress((cmpbuf + SEGHDR), 117387117650Saalok (cmpbytes - SEGHDR), uncompressed_seg, 117487117650Saalok &seglen, li->l_level) != 0) { 117587117650Saalok error = EIO; 117687117650Saalok goto done; 117787117650Saalok } 117887117650Saalok } 117987117650Saalok 11804058a205Sjrgn.keil@googlemail.com uncompressed_seg_index = i; 11814058a205Sjrgn.keil@googlemail.com 118287117650Saalok /* 118387117650Saalok * Determine how much uncompressed data we 118487117650Saalok * have to copy and copy it 118587117650Saalok */ 118687117650Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 1187b1efbcd6SAlok Aggarwal if (i == eblkno) 1188b1efbcd6SAlok Aggarwal xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff); 118987117650Saalok 119087117650Saalok bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize); 119187117650Saalok 119287117650Saalok cmpbuf += cmpbytes; 119387117650Saalok bufaddr += xfersize; 119487117650Saalok bp->b_resid -= xfersize; 119587117650Saalok sblkoff = 0; 119687117650Saalok 119787117650Saalok if (bp->b_resid == 0) 119887117650Saalok break; 119987117650Saalok } 12004058a205Sjrgn.keil@googlemail.com 12014058a205Sjrgn.keil@googlemail.com /* 12024058a205Sjrgn.keil@googlemail.com * Add the data for the last decopressed segment to 12034058a205Sjrgn.keil@googlemail.com * the cache. 12044058a205Sjrgn.keil@googlemail.com * 12054058a205Sjrgn.keil@googlemail.com * In case the uncompressed segment data was added to (and 12064058a205Sjrgn.keil@googlemail.com * is referenced by) the cache, make sure we don't free it 12074058a205Sjrgn.keil@googlemail.com * here. 12084058a205Sjrgn.keil@googlemail.com */ 12094058a205Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock); 12104058a205Sjrgn.keil@googlemail.com if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index, 12114058a205Sjrgn.keil@googlemail.com uncompressed_seg)) != NULL) { 12124058a205Sjrgn.keil@googlemail.com uncompressed_seg = NULL; 12134058a205Sjrgn.keil@googlemail.com } 12144058a205Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock); 12154058a205Sjrgn.keil@googlemail.com 121687117650Saalok done: 121787117650Saalok if (compressed_seg != NULL) 121887117650Saalok kmem_free(compressed_seg, comp_data_sz); 121987117650Saalok if (uncompressed_seg != NULL) 122087117650Saalok kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 12217d82f0f8SDina K Nimeh } /* end of handling compressed files */ 122287117650Saalok 1223*44a1e32bSbatschul if ((error == 0) && (syncflag != 0)) 1224*44a1e32bSbatschul error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL); 1225*44a1e32bSbatschul 12267d82f0f8SDina K Nimeh errout: 12277d82f0f8SDina K Nimeh if (bufinited && lsp->ls_kstat) { 12287c478bd9Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid; 12297c478bd9Sstevel@tonic-gate kstat_io_t *kioptr; 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 12327c478bd9Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 12337c478bd9Sstevel@tonic-gate if (bp->b_flags & B_READ) { 12347c478bd9Sstevel@tonic-gate kioptr->nread += n_done; 12357c478bd9Sstevel@tonic-gate kioptr->reads++; 12367c478bd9Sstevel@tonic-gate } else { 12377c478bd9Sstevel@tonic-gate kioptr->nwritten += n_done; 12387c478bd9Sstevel@tonic-gate kioptr->writes++; 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate kstat_runq_exit(kioptr); 12417c478bd9Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 12427c478bd9Sstevel@tonic-gate } 12433d7072f8Seschrock 12443d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 12453d7072f8Seschrock if (--lsp->ls_vp_iocount == 0) 12463d7072f8Seschrock cv_broadcast(&lsp->ls_vp_cv); 12473d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 12483d7072f8Seschrock 12497c478bd9Sstevel@tonic-gate bioerror(bp, error); 12507c478bd9Sstevel@tonic-gate biodone(bp); 12517c478bd9Sstevel@tonic-gate } 12527c478bd9Sstevel@tonic-gate 12537c478bd9Sstevel@tonic-gate static int 12547c478bd9Sstevel@tonic-gate lofi_strategy(struct buf *bp) 12557c478bd9Sstevel@tonic-gate { 12567c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 12577c478bd9Sstevel@tonic-gate offset_t offset; 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate /* 12607c478bd9Sstevel@tonic-gate * We cannot just do I/O here, because the current thread 12617c478bd9Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem 12627c478bd9Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and 12637c478bd9Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer. 12647c478bd9Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below. 12657c478bd9Sstevel@tonic-gate * 12667c478bd9Sstevel@tonic-gate * We could come up with a complex strategy using threads to 12677c478bd9Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task 12687c478bd9Sstevel@tonic-gate * queues were incredibly easy so they win. 12697c478bd9Sstevel@tonic-gate */ 12707c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 12717d82f0f8SDina K Nimeh if (lsp == NULL) { 12727d82f0f8SDina K Nimeh bioerror(bp, ENXIO); 12737d82f0f8SDina K Nimeh biodone(bp); 12747d82f0f8SDina K Nimeh return (0); 12757d82f0f8SDina K Nimeh } 12767d82f0f8SDina K Nimeh 12773d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 12783d7072f8Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 12793d7072f8Seschrock bioerror(bp, EIO); 12803d7072f8Seschrock biodone(bp); 12813d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 12823d7072f8Seschrock return (0); 12833d7072f8Seschrock } 12843d7072f8Seschrock 12857c478bd9Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 12867d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 12877d82f0f8SDina K Nimeh /* encrypted data really begins after crypto header */ 12887d82f0f8SDina K Nimeh offset += lsp->ls_crypto_offset; 12897d82f0f8SDina K Nimeh } 12907c478bd9Sstevel@tonic-gate if (offset == lsp->ls_vp_size) { 12917c478bd9Sstevel@tonic-gate /* EOF */ 12927c478bd9Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) { 12937c478bd9Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 12947c478bd9Sstevel@tonic-gate bioerror(bp, 0); 12957c478bd9Sstevel@tonic-gate } else { 12967c478bd9Sstevel@tonic-gate /* writes should fail */ 12977c478bd9Sstevel@tonic-gate bioerror(bp, ENXIO); 12987c478bd9Sstevel@tonic-gate } 12997c478bd9Sstevel@tonic-gate biodone(bp); 13003d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13017c478bd9Sstevel@tonic-gate return (0); 13027c478bd9Sstevel@tonic-gate } 13037c478bd9Sstevel@tonic-gate if (offset > lsp->ls_vp_size) { 13047c478bd9Sstevel@tonic-gate bioerror(bp, ENXIO); 13057c478bd9Sstevel@tonic-gate biodone(bp); 13063d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13077c478bd9Sstevel@tonic-gate return (0); 13087c478bd9Sstevel@tonic-gate } 13093d7072f8Seschrock lsp->ls_vp_iocount++; 13103d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 13113d7072f8Seschrock 13127c478bd9Sstevel@tonic-gate if (lsp->ls_kstat) { 13137c478bd9Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 13147c478bd9Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 13157c478bd9Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 13187c478bd9Sstevel@tonic-gate return (0); 13197c478bd9Sstevel@tonic-gate } 13207c478bd9Sstevel@tonic-gate 13217c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 13227c478bd9Sstevel@tonic-gate static int 13237c478bd9Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 13247c478bd9Sstevel@tonic-gate { 13257c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 13267c478bd9Sstevel@tonic-gate return (EINVAL); 13277d82f0f8SDina K Nimeh UIO_CHECK(uio); 13287c478bd9Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 13327c478bd9Sstevel@tonic-gate static int 13337c478bd9Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 13347c478bd9Sstevel@tonic-gate { 13357c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 13367c478bd9Sstevel@tonic-gate return (EINVAL); 13377d82f0f8SDina K Nimeh UIO_CHECK(uio); 13387c478bd9Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 13397c478bd9Sstevel@tonic-gate } 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 13427c478bd9Sstevel@tonic-gate static int 13437c478bd9Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 13447c478bd9Sstevel@tonic-gate { 13457c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 13467c478bd9Sstevel@tonic-gate return (EINVAL); 13477d82f0f8SDina K Nimeh UIO_CHECK(aio->aio_uio); 13487c478bd9Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 13497c478bd9Sstevel@tonic-gate } 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 13527c478bd9Sstevel@tonic-gate static int 13537c478bd9Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 13547c478bd9Sstevel@tonic-gate { 13557c478bd9Sstevel@tonic-gate if (getminor(dev) == 0) 13567c478bd9Sstevel@tonic-gate return (EINVAL); 13577d82f0f8SDina K Nimeh UIO_CHECK(aio->aio_uio); 13587c478bd9Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 13597c478bd9Sstevel@tonic-gate } 13607c478bd9Sstevel@tonic-gate 13617c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13627c478bd9Sstevel@tonic-gate static int 13637c478bd9Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 13647c478bd9Sstevel@tonic-gate { 13657c478bd9Sstevel@tonic-gate switch (infocmd) { 13667c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 13677c478bd9Sstevel@tonic-gate *result = lofi_dip; 13687c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 13697c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 13707c478bd9Sstevel@tonic-gate *result = 0; 13717c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 13727c478bd9Sstevel@tonic-gate } 13737c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 13747c478bd9Sstevel@tonic-gate } 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate static int 13777c478bd9Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 13787c478bd9Sstevel@tonic-gate { 13797c478bd9Sstevel@tonic-gate int error; 13807c478bd9Sstevel@tonic-gate 13817c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 13827c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 13837c478bd9Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0); 13847c478bd9Sstevel@tonic-gate if (error == DDI_FAILURE) { 13857c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 13867c478bd9Sstevel@tonic-gate } 13877c478bd9Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 13887c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL); 13897c478bd9Sstevel@tonic-gate if (error == DDI_FAILURE) { 13907c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 13917c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 13927c478bd9Sstevel@tonic-gate } 1393843e1988Sjohnlev /* driver handles kernel-issued IOCTLs */ 1394843e1988Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 1395843e1988Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 1396843e1988Sjohnlev ddi_remove_minor_node(dip, NULL); 1397843e1988Sjohnlev ddi_soft_state_free(lofi_statep, 0); 1398843e1988Sjohnlev return (DDI_FAILURE); 1399843e1988Sjohnlev } 14007c478bd9Sstevel@tonic-gate lofi_dip = dip; 14017c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 14027c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 14037c478bd9Sstevel@tonic-gate } 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate static int 14067c478bd9Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 14077c478bd9Sstevel@tonic-gate { 14087c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 14097c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 14107c478bd9Sstevel@tonic-gate if (lofi_busy()) 14117c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 14127c478bd9Sstevel@tonic-gate lofi_dip = NULL; 14137c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1414843e1988Sjohnlev ddi_prop_remove_all(dip); 14157c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 14167c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 14177c478bd9Sstevel@tonic-gate } 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate /* 14207d82f0f8SDina K Nimeh * With addition of encryption, be careful that encryption key is wiped before 14217d82f0f8SDina K Nimeh * kernel memory structures are freed, and also that key is not accidentally 14227d82f0f8SDina K Nimeh * passed out into userland structures. 14237d82f0f8SDina K Nimeh */ 14247d82f0f8SDina K Nimeh static void 14257d82f0f8SDina K Nimeh free_lofi_ioctl(struct lofi_ioctl *klip) 14267d82f0f8SDina K Nimeh { 14277d82f0f8SDina K Nimeh /* Make sure this encryption key doesn't stick around */ 14287d82f0f8SDina K Nimeh bzero(klip->li_key, sizeof (klip->li_key)); 14297d82f0f8SDina K Nimeh kmem_free(klip, sizeof (struct lofi_ioctl)); 14307d82f0f8SDina K Nimeh } 14317d82f0f8SDina K Nimeh 14327d82f0f8SDina K Nimeh /* 14337c478bd9Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out 14347c478bd9Sstevel@tonic-gate * the lofi_ioctl structure. 14357c478bd9Sstevel@tonic-gate */ 14367c478bd9Sstevel@tonic-gate struct lofi_ioctl * 1437bd07e074Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag) 14387c478bd9Sstevel@tonic-gate { 14397c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 14407c478bd9Sstevel@tonic-gate int error; 14417c478bd9Sstevel@tonic-gate 14427c478bd9Sstevel@tonic-gate klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 1443bd07e074Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 14447c478bd9Sstevel@tonic-gate if (error) { 14457d82f0f8SDina K Nimeh free_lofi_ioctl(klip); 14467c478bd9Sstevel@tonic-gate return (NULL); 14477c478bd9Sstevel@tonic-gate } 14487c478bd9Sstevel@tonic-gate 14497c478bd9Sstevel@tonic-gate /* make sure filename is always null-terminated */ 14506f02aa44SDina K Nimeh klip->li_filename[MAXPATHLEN-1] = '\0'; 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate /* validate minor number */ 14537c478bd9Sstevel@tonic-gate if (klip->li_minor > lofi_max_files) { 14547d82f0f8SDina K Nimeh free_lofi_ioctl(klip); 14557d82f0f8SDina K Nimeh cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)", 14567d82f0f8SDina K Nimeh lofi_max_files); 14577c478bd9Sstevel@tonic-gate return (NULL); 14587c478bd9Sstevel@tonic-gate } 14597c478bd9Sstevel@tonic-gate return (klip); 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate int 1463bd07e074Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 1464bd07e074Sheppo int flag) 14657c478bd9Sstevel@tonic-gate { 14667c478bd9Sstevel@tonic-gate int error; 14677c478bd9Sstevel@tonic-gate 14687d82f0f8SDina K Nimeh /* 14697d82f0f8SDina K Nimeh * NOTE: Do NOT copy the crypto_key_t "back" to userland. 14707d82f0f8SDina K Nimeh * This ensures that an attacker can't trivially find the 14717d82f0f8SDina K Nimeh * key for a mapping just by issuing the ioctl. 14727d82f0f8SDina K Nimeh * 14737d82f0f8SDina K Nimeh * It can still be found by poking around in kmem with mdb(1), 14747d82f0f8SDina K Nimeh * but there is no point in making it easy when the info isn't 14757d82f0f8SDina K Nimeh * of any use in this direction anyway. 14767d82f0f8SDina K Nimeh * 14777d82f0f8SDina K Nimeh * Either way we don't actually have the raw key stored in 14787d82f0f8SDina K Nimeh * a form that we can get it anyway, since we just used it 14797d82f0f8SDina K Nimeh * to create a ctx template and didn't keep "the original". 14807d82f0f8SDina K Nimeh */ 1481bd07e074Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 14827c478bd9Sstevel@tonic-gate if (error) 14837c478bd9Sstevel@tonic-gate return (EFAULT); 14847c478bd9Sstevel@tonic-gate return (0); 14857c478bd9Sstevel@tonic-gate } 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate /* 14887c478bd9Sstevel@tonic-gate * Return the minor number 'filename' is mapped to, if it is. 14897c478bd9Sstevel@tonic-gate */ 14907c478bd9Sstevel@tonic-gate static int 14917c478bd9Sstevel@tonic-gate file_to_minor(char *filename) 14927c478bd9Sstevel@tonic-gate { 14937c478bd9Sstevel@tonic-gate minor_t minor; 14947c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 14977c478bd9Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 14987c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 14997c478bd9Sstevel@tonic-gate if (lsp == NULL) 15007c478bd9Sstevel@tonic-gate continue; 15017c478bd9Sstevel@tonic-gate if (strcmp(lsp->ls_filename, filename) == 0) 15027c478bd9Sstevel@tonic-gate return (minor); 15037c478bd9Sstevel@tonic-gate } 15047c478bd9Sstevel@tonic-gate return (0); 15057c478bd9Sstevel@tonic-gate } 15067c478bd9Sstevel@tonic-gate 15077c478bd9Sstevel@tonic-gate /* 15087c478bd9Sstevel@tonic-gate * lofiadm does some validation, but since Joe Random (or crashme) could 15097c478bd9Sstevel@tonic-gate * do our ioctls, we need to do some validation too. 15107c478bd9Sstevel@tonic-gate */ 15117c478bd9Sstevel@tonic-gate static int 15127c478bd9Sstevel@tonic-gate valid_filename(const char *filename) 15137c478bd9Sstevel@tonic-gate { 15147c478bd9Sstevel@tonic-gate static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/"; 15157c478bd9Sstevel@tonic-gate static char *charprefix = "/dev/" LOFI_CHAR_NAME "/"; 15167c478bd9Sstevel@tonic-gate 15177c478bd9Sstevel@tonic-gate /* must be absolute path */ 15187c478bd9Sstevel@tonic-gate if (filename[0] != '/') 15197c478bd9Sstevel@tonic-gate return (0); 15207c478bd9Sstevel@tonic-gate /* must not be lofi */ 15217c478bd9Sstevel@tonic-gate if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0) 15227c478bd9Sstevel@tonic-gate return (0); 15237c478bd9Sstevel@tonic-gate if (strncmp(filename, charprefix, strlen(charprefix)) == 0) 15247c478bd9Sstevel@tonic-gate return (0); 15257c478bd9Sstevel@tonic-gate return (1); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate /* 15297c478bd9Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size 15307c478bd9Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device, 15317c478bd9Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and 15327c478bd9Sstevel@tonic-gate * partition information. It uses that information to determine the parameters 153363360950Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 15347c478bd9Sstevel@tonic-gate * have to support it. 15357c478bd9Sstevel@tonic-gate */ 15367c478bd9Sstevel@tonic-gate static void 15377c478bd9Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp) 15387c478bd9Sstevel@tonic-gate { 15397d82f0f8SDina K Nimeh u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset; 15407d82f0f8SDina K Nimeh 15417c478bd9Sstevel@tonic-gate /* dk_geom - see dkio(7I) */ 15427c478bd9Sstevel@tonic-gate /* 15437c478bd9Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 15447c478bd9Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to 15457c478bd9Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create 15467c478bd9Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick 15477c478bd9Sstevel@tonic-gate * a number between one and one). 15487c478bd9Sstevel@tonic-gate * 15497c478bd9Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders 15507c478bd9Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space 15517c478bd9Sstevel@tonic-gate * for backup superblocks or cylinder group structures. 15527c478bd9Sstevel@tonic-gate */ 15537d82f0f8SDina K Nimeh if (dsize < (2 * 1024 * 1024)) /* floppy? */ 15547d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024); 15557c478bd9Sstevel@tonic-gate else 15567d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024); 15577c478bd9Sstevel@tonic-gate /* in case file file is < 100k */ 15587c478bd9Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0) 15597c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1; 15607c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0; 15617c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0; 15627c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1; 15637c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0; 15647c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0; 15657c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0; 15667c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0; 15677c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0; 15687c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200; 15697c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 15707d82f0f8SDina K Nimeh lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 15717c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0; 15727c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0; 15737c478bd9Sstevel@tonic-gate 15747c478bd9Sstevel@tonic-gate /* vtoc - see dkio(7I) */ 15757c478bd9Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 15767c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE; 15777c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION; 1578947caa0eSDina K Nimeh (void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME, 1579947caa0eSDina K Nimeh sizeof (lsp->ls_vtoc.v_volume)); 15807c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 15817c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1; 15827c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 158387117650Saalok 158487117650Saalok /* 158587117650Saalok * A compressed file is read-only, other files can 158687117650Saalok * be read-write 158787117650Saalok */ 158887117650Saalok if (lsp->ls_uncomp_seg_sz > 0) { 158987117650Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 159087117650Saalok } else { 15917c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 159287117650Saalok } 15937c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 15947c478bd9Sstevel@tonic-gate /* 15957c478bd9Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because 15967c478bd9Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case, 15977c478bd9Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size 15987c478bd9Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder. 15997c478bd9Sstevel@tonic-gate */ 16007c478bd9Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 16017c478bd9Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */ 16047c478bd9Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 16057c478bd9Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 16067c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD; 16077c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0; 16087c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0; 16097c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0; 16107c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_space = 0; 16117c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0; 16127c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0; 16137c478bd9Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 16147c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0; 16157c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0; 16167c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0; 16177c478bd9Sstevel@tonic-gate /* 16187c478bd9Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it 16197c478bd9Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides 16207c478bd9Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because 16217c478bd9Sstevel@tonic-gate * maxcontig is 0. 16227c478bd9Sstevel@tonic-gate */ 16237c478bd9Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16; 16247c478bd9Sstevel@tonic-gate } 16257c478bd9Sstevel@tonic-gate 16267c478bd9Sstevel@tonic-gate /* 162787117650Saalok * map in a compressed file 162887117650Saalok * 162987117650Saalok * Read in the header and the index that follows. 163087117650Saalok * 163187117650Saalok * The header is as follows - 163287117650Saalok * 163387117650Saalok * Signature (name of the compression algorithm) 163487117650Saalok * Compression segment size (a multiple of 512) 163587117650Saalok * Number of index entries 163687117650Saalok * Size of the last block 163787117650Saalok * The array containing the index entries 163887117650Saalok * 163987117650Saalok * The header information is always stored in 164087117650Saalok * network byte order on disk. 164187117650Saalok */ 164287117650Saalok static int 164387117650Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 164487117650Saalok { 164587117650Saalok uint32_t index_sz, header_len, i; 164687117650Saalok ssize_t resid; 164787117650Saalok enum uio_rw rw; 164887117650Saalok char *tbuf = buf; 164987117650Saalok int error; 165087117650Saalok 165187117650Saalok /* The signature has already been read */ 165287117650Saalok tbuf += sizeof (lsp->ls_comp_algorithm); 165387117650Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 165487117650Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 165587117650Saalok 165687117650Saalok /* 165787117650Saalok * The compressed segment size must be a power of 2 165887117650Saalok */ 16594058a205Sjrgn.keil@googlemail.com if (lsp->ls_uncomp_seg_sz < DEV_BSIZE || 16604058a205Sjrgn.keil@googlemail.com !ISP2(lsp->ls_uncomp_seg_sz)) 166187117650Saalok return (EINVAL); 166287117650Saalok 166387117650Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 166487117650Saalok ; 166587117650Saalok 166687117650Saalok lsp->ls_comp_seg_shift = i; 166787117650Saalok 166887117650Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz); 166987117650Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 167087117650Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 167187117650Saalok 167287117650Saalok tbuf += sizeof (lsp->ls_comp_index_sz); 167387117650Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 167487117650Saalok sizeof (lsp->ls_uncomp_last_seg_sz)); 167587117650Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 167687117650Saalok 167787117650Saalok /* 167887117650Saalok * Compute the total size of the uncompressed data 167987117650Saalok * for use in fake_disk_geometry and other calculations. 168087117650Saalok * Disk geometry has to be faked with respect to the 168187117650Saalok * actual uncompressed data size rather than the 168287117650Saalok * compressed file size. 168387117650Saalok */ 16847d11f38eSDavid Miner lsp->ls_vp_size = 16857d11f38eSDavid Miner (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 168687117650Saalok + lsp->ls_uncomp_last_seg_sz; 168787117650Saalok 168887117650Saalok /* 1689b1efbcd6SAlok Aggarwal * Index size is rounded up to DEV_BSIZE for ease 169087117650Saalok * of segmapping 169187117650Saalok */ 169287117650Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 169387117650Saalok header_len = sizeof (lsp->ls_comp_algorithm) + 169487117650Saalok sizeof (lsp->ls_uncomp_seg_sz) + 169587117650Saalok sizeof (lsp->ls_comp_index_sz) + 169687117650Saalok sizeof (lsp->ls_uncomp_last_seg_sz); 169787117650Saalok lsp->ls_comp_offbase = header_len + index_sz; 169887117650Saalok 169987117650Saalok index_sz += header_len; 170087117650Saalok index_sz = roundup(index_sz, DEV_BSIZE); 170187117650Saalok 170287117650Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 170387117650Saalok lsp->ls_comp_index_data_sz = index_sz; 170487117650Saalok 170587117650Saalok /* 170687117650Saalok * Read in the index -- this has a side-effect 170787117650Saalok * of reading in the header as well 170887117650Saalok */ 170987117650Saalok rw = UIO_READ; 171087117650Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 171187117650Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 171287117650Saalok 171387117650Saalok if (error != 0) 171487117650Saalok return (error); 171587117650Saalok 171687117650Saalok /* Skip the header, this is where the index really begins */ 171787117650Saalok lsp->ls_comp_seg_index = 171887117650Saalok /*LINTED*/ 171987117650Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len); 172087117650Saalok 172187117650Saalok /* 172287117650Saalok * Now recompute offsets in the index to account for 172387117650Saalok * the header length 172487117650Saalok */ 172587117650Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) { 172687117650Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 172787117650Saalok BE_64(lsp->ls_comp_seg_index[i]); 172887117650Saalok } 172987117650Saalok 173087117650Saalok return (error); 173187117650Saalok } 173287117650Saalok 173387117650Saalok /* 173487117650Saalok * Check to see if the passed in signature is a valid 173587117650Saalok * one. If it is valid, return the index into 173687117650Saalok * lofi_compress_table. 173787117650Saalok * 173887117650Saalok * Return -1 if it is invalid 173987117650Saalok */ 174087117650Saalok static int lofi_compress_select(char *signature) 174187117650Saalok { 174287117650Saalok int i; 174387117650Saalok 174487117650Saalok for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 174587117650Saalok if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 174687117650Saalok return (i); 174787117650Saalok } 174887117650Saalok 174987117650Saalok return (-1); 175087117650Saalok } 175187117650Saalok 175287117650Saalok /* 17537c478bd9Sstevel@tonic-gate * map a file to a minor number. Return the minor number. 17547c478bd9Sstevel@tonic-gate */ 17557c478bd9Sstevel@tonic-gate static int 17567c478bd9Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 1757bd07e074Sheppo int *rvalp, struct cred *credp, int ioctl_flag) 17587c478bd9Sstevel@tonic-gate { 17597c478bd9Sstevel@tonic-gate minor_t newminor; 17607c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 17617c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 17627c478bd9Sstevel@tonic-gate int error; 17637c478bd9Sstevel@tonic-gate struct vnode *vp; 17647c478bd9Sstevel@tonic-gate int64_t Nblocks_prop_val; 17657c478bd9Sstevel@tonic-gate int64_t Size_prop_val; 176687117650Saalok int compress_index; 17677c478bd9Sstevel@tonic-gate vattr_t vattr; 17687c478bd9Sstevel@tonic-gate int flag; 17697c478bd9Sstevel@tonic-gate enum vtype v_type; 17707c478bd9Sstevel@tonic-gate int zalloced = 0; 17713d7072f8Seschrock dev_t newdev; 17723d7072f8Seschrock char namebuf[50]; 177387117650Saalok char buf[DEV_BSIZE]; 17747d82f0f8SDina K Nimeh char crybuf[DEV_BSIZE]; 177587117650Saalok ssize_t resid; 17767d82f0f8SDina K Nimeh boolean_t need_vn_close = B_FALSE; 17777d82f0f8SDina K Nimeh boolean_t keycopied = B_FALSE; 17787d82f0f8SDina K Nimeh boolean_t need_size_update = B_FALSE; 17797c478bd9Sstevel@tonic-gate 1780bd07e074Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 17817c478bd9Sstevel@tonic-gate if (klip == NULL) 17827c478bd9Sstevel@tonic-gate return (EFAULT); 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 17857c478bd9Sstevel@tonic-gate 17867c478bd9Sstevel@tonic-gate if (!valid_filename(klip->li_filename)) { 17877c478bd9Sstevel@tonic-gate error = EINVAL; 17887c478bd9Sstevel@tonic-gate goto out; 17897c478bd9Sstevel@tonic-gate } 17907c478bd9Sstevel@tonic-gate 17917c478bd9Sstevel@tonic-gate if (file_to_minor(klip->li_filename) != 0) { 17927c478bd9Sstevel@tonic-gate error = EBUSY; 17937c478bd9Sstevel@tonic-gate goto out; 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate if (pickminor) { 17977c478bd9Sstevel@tonic-gate /* Find a free one */ 17987c478bd9Sstevel@tonic-gate for (newminor = 1; newminor <= lofi_max_files; newminor++) 17997c478bd9Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) == NULL) 18007c478bd9Sstevel@tonic-gate break; 18017c478bd9Sstevel@tonic-gate if (newminor >= lofi_max_files) { 18027c478bd9Sstevel@tonic-gate error = EAGAIN; 18037c478bd9Sstevel@tonic-gate goto out; 18047c478bd9Sstevel@tonic-gate } 18057c478bd9Sstevel@tonic-gate } else { 18067c478bd9Sstevel@tonic-gate newminor = klip->li_minor; 18077c478bd9Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) != NULL) { 18087c478bd9Sstevel@tonic-gate error = EEXIST; 18097c478bd9Sstevel@tonic-gate goto out; 18107c478bd9Sstevel@tonic-gate } 18117c478bd9Sstevel@tonic-gate } 18127c478bd9Sstevel@tonic-gate 18137c478bd9Sstevel@tonic-gate /* make sure it's valid */ 18147c478bd9Sstevel@tonic-gate error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW, 18157c478bd9Sstevel@tonic-gate NULLVPP, &vp); 18167c478bd9Sstevel@tonic-gate if (error) { 18177c478bd9Sstevel@tonic-gate goto out; 18187c478bd9Sstevel@tonic-gate } 18197c478bd9Sstevel@tonic-gate v_type = vp->v_type; 18207c478bd9Sstevel@tonic-gate VN_RELE(vp); 18217c478bd9Sstevel@tonic-gate if (!V_ISLOFIABLE(v_type)) { 18227c478bd9Sstevel@tonic-gate error = EINVAL; 18237c478bd9Sstevel@tonic-gate goto out; 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate flag = FREAD | FWRITE | FOFFMAX | FEXCL; 18267c478bd9Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 18277c478bd9Sstevel@tonic-gate if (error) { 18287c478bd9Sstevel@tonic-gate /* try read-only */ 18297c478bd9Sstevel@tonic-gate flag &= ~FWRITE; 18307c478bd9Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 18317c478bd9Sstevel@tonic-gate &vp, 0, 0); 18327c478bd9Sstevel@tonic-gate if (error) { 18337c478bd9Sstevel@tonic-gate goto out; 18347c478bd9Sstevel@tonic-gate } 18357c478bd9Sstevel@tonic-gate } 18367d82f0f8SDina K Nimeh need_vn_close = B_TRUE; 18377d82f0f8SDina K Nimeh 18387c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 1839da6c28aaSamw error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 18407c478bd9Sstevel@tonic-gate if (error) { 18417d82f0f8SDina K Nimeh goto out; 18427c478bd9Sstevel@tonic-gate } 18437c478bd9Sstevel@tonic-gate /* the file needs to be a multiple of the block size */ 18447c478bd9Sstevel@tonic-gate if ((vattr.va_size % DEV_BSIZE) != 0) { 18457c478bd9Sstevel@tonic-gate error = EINVAL; 18467d82f0f8SDina K Nimeh goto out; 18477c478bd9Sstevel@tonic-gate } 18487c478bd9Sstevel@tonic-gate newdev = makedevice(getmajor(dev), newminor); 18497c478bd9Sstevel@tonic-gate Size_prop_val = vattr.va_size; 18507c478bd9Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 18517c478bd9Sstevel@tonic-gate SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) { 18527c478bd9Sstevel@tonic-gate error = EINVAL; 18537d82f0f8SDina K Nimeh goto out; 18547c478bd9Sstevel@tonic-gate } 18557c478bd9Sstevel@tonic-gate Nblocks_prop_val = vattr.va_size / DEV_BSIZE; 18567c478bd9Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 18577c478bd9Sstevel@tonic-gate NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 18587c478bd9Sstevel@tonic-gate error = EINVAL; 18597c478bd9Sstevel@tonic-gate goto propout; 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, newminor); 18627c478bd9Sstevel@tonic-gate if (error == DDI_FAILURE) { 18637c478bd9Sstevel@tonic-gate error = ENOMEM; 18647c478bd9Sstevel@tonic-gate goto propout; 18657c478bd9Sstevel@tonic-gate } 18667c478bd9Sstevel@tonic-gate zalloced = 1; 18677c478bd9Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 18687e45b93eSgd78059 error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor, 18697c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL); 18707c478bd9Sstevel@tonic-gate if (error != DDI_SUCCESS) { 18717c478bd9Sstevel@tonic-gate error = ENXIO; 18727c478bd9Sstevel@tonic-gate goto propout; 18737c478bd9Sstevel@tonic-gate } 18747c478bd9Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor); 18757c478bd9Sstevel@tonic-gate error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor, 18767c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL); 18777c478bd9Sstevel@tonic-gate if (error != DDI_SUCCESS) { 18787c478bd9Sstevel@tonic-gate /* remove block node */ 18797c478bd9Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 18807c478bd9Sstevel@tonic-gate ddi_remove_minor_node(lofi_dip, namebuf); 18817c478bd9Sstevel@tonic-gate error = ENXIO; 18827c478bd9Sstevel@tonic-gate goto propout; 18837c478bd9Sstevel@tonic-gate } 18847c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, newminor); 18857c478bd9Sstevel@tonic-gate lsp->ls_filename_sz = strlen(klip->li_filename) + 1; 18867c478bd9Sstevel@tonic-gate lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP); 18877c478bd9Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 18887c478bd9Sstevel@tonic-gate LOFI_DRIVER_NAME, newminor); 18897c478bd9Sstevel@tonic-gate lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads, 18907c478bd9Sstevel@tonic-gate minclsyspri, 1, lofi_taskq_maxalloc, 0); 18917c478bd9Sstevel@tonic-gate lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor, 18927c478bd9Sstevel@tonic-gate NULL, "disk", KSTAT_TYPE_IO, 1, 0); 18937c478bd9Sstevel@tonic-gate if (lsp->ls_kstat) { 18947c478bd9Sstevel@tonic-gate mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 18957c478bd9Sstevel@tonic-gate lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 18967c478bd9Sstevel@tonic-gate kstat_install(lsp->ls_kstat); 18977c478bd9Sstevel@tonic-gate } 18983d7072f8Seschrock cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 18993d7072f8Seschrock mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 19003d7072f8Seschrock 19014058a205Sjrgn.keil@googlemail.com list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache), 19024058a205Sjrgn.keil@googlemail.com offsetof(struct lofi_comp_cache, lc_list)); 19034058a205Sjrgn.keil@googlemail.com mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL); 19044058a205Sjrgn.keil@googlemail.com 19057c478bd9Sstevel@tonic-gate /* 19067c478bd9Sstevel@tonic-gate * save open mode so file can be closed properly and vnode counts 19077c478bd9Sstevel@tonic-gate * updated correctly. 19087c478bd9Sstevel@tonic-gate */ 19097c478bd9Sstevel@tonic-gate lsp->ls_openflag = flag; 19107c478bd9Sstevel@tonic-gate 19117c478bd9Sstevel@tonic-gate /* 19127c478bd9Sstevel@tonic-gate * Try to handle stacked lofs vnodes. 19137c478bd9Sstevel@tonic-gate */ 19147c478bd9Sstevel@tonic-gate if (vp->v_type == VREG) { 1915da6c28aaSamw if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) { 19167c478bd9Sstevel@tonic-gate lsp->ls_vp = vp; 19177c478bd9Sstevel@tonic-gate } else { 19187c478bd9Sstevel@tonic-gate /* 19197c478bd9Sstevel@tonic-gate * Even though vp was obtained via vn_open(), we 19207c478bd9Sstevel@tonic-gate * can't call vn_close() on it, since lofs will 19217c478bd9Sstevel@tonic-gate * pass the VOP_CLOSE() on down to the realvp 19227c478bd9Sstevel@tonic-gate * (which we are about to use). Hence we merely 19237c478bd9Sstevel@tonic-gate * drop the reference to the lofs vnode and hold 19247c478bd9Sstevel@tonic-gate * the realvp so things behave as if we've 19257c478bd9Sstevel@tonic-gate * opened the realvp without any interaction 19267c478bd9Sstevel@tonic-gate * with lofs. 19277c478bd9Sstevel@tonic-gate */ 19287c478bd9Sstevel@tonic-gate VN_HOLD(lsp->ls_vp); 19297c478bd9Sstevel@tonic-gate VN_RELE(vp); 19307c478bd9Sstevel@tonic-gate } 19317c478bd9Sstevel@tonic-gate } else { 19327c478bd9Sstevel@tonic-gate lsp->ls_vp = vp; 19337c478bd9Sstevel@tonic-gate } 19347c478bd9Sstevel@tonic-gate lsp->ls_vp_size = vattr.va_size; 19357c478bd9Sstevel@tonic-gate (void) strcpy(lsp->ls_filename, klip->li_filename); 19367c478bd9Sstevel@tonic-gate if (rvalp) 19377c478bd9Sstevel@tonic-gate *rvalp = (int)newminor; 19387c478bd9Sstevel@tonic-gate klip->li_minor = newminor; 19397c478bd9Sstevel@tonic-gate 194087117650Saalok /* 19417d82f0f8SDina K Nimeh * Initialize crypto details for encrypted lofi 194287117650Saalok */ 19437d82f0f8SDina K Nimeh if (klip->li_crypto_enabled) { 19447d82f0f8SDina K Nimeh int ret; 194587117650Saalok 19467d82f0f8SDina K Nimeh mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL); 19477d82f0f8SDina K Nimeh 19487d82f0f8SDina K Nimeh lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher); 19497d82f0f8SDina K Nimeh if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) { 19507d82f0f8SDina K Nimeh cmn_err(CE_WARN, "invalid cipher %s requested for %s", 19517d82f0f8SDina K Nimeh klip->li_cipher, lsp->ls_filename); 19527d82f0f8SDina K Nimeh error = EINVAL; 19537d82f0f8SDina K Nimeh goto propout; 19547d82f0f8SDina K Nimeh } 19557d82f0f8SDina K Nimeh 19567d82f0f8SDina K Nimeh /* this is just initialization here */ 19577d82f0f8SDina K Nimeh lsp->ls_mech.cm_param = NULL; 19587d82f0f8SDina K Nimeh lsp->ls_mech.cm_param_len = 0; 19597d82f0f8SDina K Nimeh 19607d82f0f8SDina K Nimeh lsp->ls_iv_type = klip->li_iv_type; 19617d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher); 19627d82f0f8SDina K Nimeh if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) { 19637d82f0f8SDina K Nimeh cmn_err(CE_WARN, "invalid iv cipher %s requested" 19647d82f0f8SDina K Nimeh " for %s", klip->li_iv_cipher, lsp->ls_filename); 19657d82f0f8SDina K Nimeh error = EINVAL; 19667d82f0f8SDina K Nimeh goto propout; 19677d82f0f8SDina K Nimeh } 19687d82f0f8SDina K Nimeh 19697d82f0f8SDina K Nimeh /* iv mech must itself take a null iv */ 19707d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param = NULL; 19717d82f0f8SDina K Nimeh lsp->ls_iv_mech.cm_param_len = 0; 19727d82f0f8SDina K Nimeh lsp->ls_iv_len = klip->li_iv_len; 19737d82f0f8SDina K Nimeh 19747d82f0f8SDina K Nimeh /* 19757d82f0f8SDina K Nimeh * Create ctx using li_cipher & the raw li_key after checking 19767d82f0f8SDina K Nimeh * that it isn't a weak key. 19777d82f0f8SDina K Nimeh */ 19787d82f0f8SDina K Nimeh lsp->ls_key.ck_format = CRYPTO_KEY_RAW; 19797d82f0f8SDina K Nimeh lsp->ls_key.ck_length = klip->li_key_len; 19807d82f0f8SDina K Nimeh lsp->ls_key.ck_data = kmem_alloc( 19817d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP); 19827d82f0f8SDina K Nimeh bcopy(klip->li_key, lsp->ls_key.ck_data, 19837d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 19847d82f0f8SDina K Nimeh keycopied = B_TRUE; 19857d82f0f8SDina K Nimeh 19867d82f0f8SDina K Nimeh ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key); 19877d82f0f8SDina K Nimeh if (ret != CRYPTO_SUCCESS) { 19887d82f0f8SDina K Nimeh error = EINVAL; 19897d82f0f8SDina K Nimeh cmn_err(CE_WARN, "weak key check failed for cipher " 19907d82f0f8SDina K Nimeh "%s on file %s (0x%x)", klip->li_cipher, 19917d82f0f8SDina K Nimeh lsp->ls_filename, ret); 19927d82f0f8SDina K Nimeh goto propout; 19937d82f0f8SDina K Nimeh } 19947d82f0f8SDina K Nimeh } 19957d82f0f8SDina K Nimeh lsp->ls_crypto_enabled = klip->li_crypto_enabled; 19967d82f0f8SDina K Nimeh 19977d82f0f8SDina K Nimeh /* 19987d82f0f8SDina K Nimeh * Read the file signature to check if it is compressed or encrypted. 19997d82f0f8SDina K Nimeh * Crypto signature is in a different location; both areas should 20007d82f0f8SDina K Nimeh * read to keep compression and encryption mutually exclusive. 20017d82f0f8SDina K Nimeh */ 20027d82f0f8SDina K Nimeh if (lsp->ls_crypto_enabled) { 20037d82f0f8SDina K Nimeh error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE, 20047d82f0f8SDina K Nimeh CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 20057d82f0f8SDina K Nimeh if (error != 0) 20067d82f0f8SDina K Nimeh goto propout; 20077d82f0f8SDina K Nimeh } 20087d82f0f8SDina K Nimeh error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 20097d82f0f8SDina K Nimeh 0, RLIM64_INFINITY, kcred, &resid); 201087117650Saalok if (error != 0) 201187117650Saalok goto propout; 201287117650Saalok 20137d82f0f8SDina K Nimeh /* initialize these variables for all lofi files */ 201487117650Saalok lsp->ls_uncomp_seg_sz = 0; 201587117650Saalok lsp->ls_vp_comp_size = lsp->ls_vp_size; 201687117650Saalok lsp->ls_comp_algorithm[0] = '\0'; 201787117650Saalok 20187d82f0f8SDina K Nimeh /* encrypted lofi reads/writes shifted by crypto metadata size */ 20197d82f0f8SDina K Nimeh lsp->ls_crypto_offset = 0; 20207d82f0f8SDina K Nimeh 20217d82f0f8SDina K Nimeh /* this is a compressed lofi */ 20227d82f0f8SDina K Nimeh if ((compress_index = lofi_compress_select(buf)) != -1) { 20237d82f0f8SDina K Nimeh 20247d82f0f8SDina K Nimeh /* compression and encryption are mutually exclusive */ 20257d82f0f8SDina K Nimeh if (klip->li_crypto_enabled) { 20267d82f0f8SDina K Nimeh error = ENOTSUP; 20277d82f0f8SDina K Nimeh goto propout; 20287d82f0f8SDina K Nimeh } 20297d82f0f8SDina K Nimeh 20307d82f0f8SDina K Nimeh /* initialize compression info for compressed lofi */ 203187117650Saalok lsp->ls_comp_algorithm_index = compress_index; 203287117650Saalok (void) strlcpy(lsp->ls_comp_algorithm, 203387117650Saalok lofi_compress_table[compress_index].l_name, 203487117650Saalok sizeof (lsp->ls_comp_algorithm)); 20357d82f0f8SDina K Nimeh 203687117650Saalok error = lofi_map_compressed_file(lsp, buf); 203787117650Saalok if (error != 0) 203887117650Saalok goto propout; 20397d82f0f8SDina K Nimeh need_size_update = B_TRUE; 204087117650Saalok 20417d82f0f8SDina K Nimeh /* this is an encrypted lofi */ 20427d82f0f8SDina K Nimeh } else if (strncmp(crybuf, lofi_crypto_magic, 20437d82f0f8SDina K Nimeh sizeof (lofi_crypto_magic)) == 0) { 20447d82f0f8SDina K Nimeh 20457d82f0f8SDina K Nimeh char *marker = crybuf; 20467d82f0f8SDina K Nimeh 20477d82f0f8SDina K Nimeh /* 20487d82f0f8SDina K Nimeh * This is the case where the header in the lofi image is 20497d82f0f8SDina K Nimeh * already initialized to indicate it is encrypted. 20507d82f0f8SDina K Nimeh * There is another case (see below) where encryption is 20517d82f0f8SDina K Nimeh * requested but the lofi image has never been used yet, 20527d82f0f8SDina K Nimeh * so the header needs to be written with encryption magic. 20537d82f0f8SDina K Nimeh */ 20547d82f0f8SDina K Nimeh 20557d82f0f8SDina K Nimeh /* indicate this must be an encrypted lofi due to magic */ 20567d82f0f8SDina K Nimeh klip->li_crypto_enabled = B_TRUE; 20577d82f0f8SDina K Nimeh 20587d82f0f8SDina K Nimeh /* 20597d82f0f8SDina K Nimeh * The encryption header information is laid out this way: 20607d82f0f8SDina K Nimeh * 6 bytes: hex "CFLOFI" 20617d82f0f8SDina K Nimeh * 2 bytes: version = 0 ... for now 20627d82f0f8SDina K Nimeh * 96 bytes: reserved1 (not implemented yet) 20637d82f0f8SDina K Nimeh * 4 bytes: data_sector = 2 ... for now 20647d82f0f8SDina K Nimeh * more... not implemented yet 20657d82f0f8SDina K Nimeh */ 20667d82f0f8SDina K Nimeh 20677d82f0f8SDina K Nimeh /* copy the magic */ 20687d82f0f8SDina K Nimeh bcopy(marker, lsp->ls_crypto.magic, 20697d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.magic)); 20707d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.magic); 20717d82f0f8SDina K Nimeh 20727d82f0f8SDina K Nimeh /* read the encryption version number */ 20737d82f0f8SDina K Nimeh bcopy(marker, &(lsp->ls_crypto.version), 20747d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.version)); 20757d82f0f8SDina K Nimeh lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version); 20767d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.version); 20777d82f0f8SDina K Nimeh 20787d82f0f8SDina K Nimeh /* read a chunk of reserved data */ 20797d82f0f8SDina K Nimeh bcopy(marker, lsp->ls_crypto.reserved1, 20807d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.reserved1)); 20817d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.reserved1); 20827d82f0f8SDina K Nimeh 20837d82f0f8SDina K Nimeh /* read block number where encrypted data begins */ 20847d82f0f8SDina K Nimeh bcopy(marker, &(lsp->ls_crypto.data_sector), 20857d82f0f8SDina K Nimeh sizeof (lsp->ls_crypto.data_sector)); 20867d82f0f8SDina K Nimeh lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector); 20877d82f0f8SDina K Nimeh marker += sizeof (lsp->ls_crypto.data_sector); 20887d82f0f8SDina K Nimeh 20897d82f0f8SDina K Nimeh /* and ignore the rest until it is implemented */ 20907d82f0f8SDina K Nimeh 20917d82f0f8SDina K Nimeh lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 20927d82f0f8SDina K Nimeh need_size_update = B_TRUE; 20937d82f0f8SDina K Nimeh 20947d82f0f8SDina K Nimeh /* neither compressed nor encrypted, BUT could be new encrypted lofi */ 20957d82f0f8SDina K Nimeh } else if (klip->li_crypto_enabled) { 20967d82f0f8SDina K Nimeh 20977d82f0f8SDina K Nimeh /* 20987d82f0f8SDina K Nimeh * This is the case where encryption was requested but the 20997d82f0f8SDina K Nimeh * appears to be entirely blank where the encryption header 21007d82f0f8SDina K Nimeh * would have been in the lofi image. If it is blank, 21017d82f0f8SDina K Nimeh * assume it is a brand new lofi image and initialize the 21027d82f0f8SDina K Nimeh * header area with encryption magic and current version 21037d82f0f8SDina K Nimeh * header data. If it is not blank, that's an error. 21047d82f0f8SDina K Nimeh */ 21057d82f0f8SDina K Nimeh int i; 21067d82f0f8SDina K Nimeh char *marker; 21077d82f0f8SDina K Nimeh struct crypto_meta chead; 21087d82f0f8SDina K Nimeh 21097d82f0f8SDina K Nimeh for (i = 0; i < sizeof (struct crypto_meta); i++) 21107d82f0f8SDina K Nimeh if (crybuf[i] != '\0') 21117d82f0f8SDina K Nimeh break; 21127d82f0f8SDina K Nimeh if (i != sizeof (struct crypto_meta)) { 21137d82f0f8SDina K Nimeh error = EINVAL; 21147d82f0f8SDina K Nimeh goto propout; 21157d82f0f8SDina K Nimeh } 21167d82f0f8SDina K Nimeh 21177d82f0f8SDina K Nimeh /* nothing there, initialize as encrypted lofi */ 21187d82f0f8SDina K Nimeh marker = crybuf; 21197d82f0f8SDina K Nimeh bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic)); 21207d82f0f8SDina K Nimeh marker += sizeof (lofi_crypto_magic); 21217d82f0f8SDina K Nimeh chead.version = htons(LOFI_CRYPTO_VERSION); 21227d82f0f8SDina K Nimeh bcopy(&(chead.version), marker, sizeof (chead.version)); 21237d82f0f8SDina K Nimeh marker += sizeof (chead.version); 21247d82f0f8SDina K Nimeh marker += sizeof (chead.reserved1); 21257d82f0f8SDina K Nimeh chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR); 21267d82f0f8SDina K Nimeh bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector)); 21277d82f0f8SDina K Nimeh 21287d82f0f8SDina K Nimeh /* write the header */ 21297d82f0f8SDina K Nimeh error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE, 21307d82f0f8SDina K Nimeh CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 21317d82f0f8SDina K Nimeh if (error != 0) 21327d82f0f8SDina K Nimeh goto propout; 21337d82f0f8SDina K Nimeh 21347d82f0f8SDina K Nimeh /* fix things up so it looks like we read this info */ 21357d82f0f8SDina K Nimeh bcopy(lofi_crypto_magic, lsp->ls_crypto.magic, 21367d82f0f8SDina K Nimeh sizeof (lofi_crypto_magic)); 21377d82f0f8SDina K Nimeh lsp->ls_crypto.version = LOFI_CRYPTO_VERSION; 21387d82f0f8SDina K Nimeh lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR; 21397d82f0f8SDina K Nimeh 21407d82f0f8SDina K Nimeh lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 21417d82f0f8SDina K Nimeh need_size_update = B_TRUE; 21427d82f0f8SDina K Nimeh } 21437d82f0f8SDina K Nimeh 21447d82f0f8SDina K Nimeh /* 21457d82f0f8SDina K Nimeh * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed; 21467d82f0f8SDina K Nimeh * for encrypted lofi, advertise that it is somewhat shorter 21477d82f0f8SDina K Nimeh * due to embedded crypto metadata section 21487d82f0f8SDina K Nimeh */ 21497d82f0f8SDina K Nimeh if (need_size_update) { 215087117650Saalok /* update DDI properties */ 21517d82f0f8SDina K Nimeh Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset; 215287117650Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 215387117650Saalok Size_prop_val)) != DDI_PROP_SUCCESS) { 215487117650Saalok error = EINVAL; 215587117650Saalok goto propout; 215687117650Saalok } 21577d82f0f8SDina K Nimeh Nblocks_prop_val = 21587d82f0f8SDina K Nimeh (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE; 215987117650Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 216087117650Saalok Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 216187117650Saalok error = EINVAL; 216287117650Saalok goto propout; 216387117650Saalok } 216487117650Saalok } 216587117650Saalok 21667c478bd9Sstevel@tonic-gate fake_disk_geometry(lsp); 21677c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2168bd07e074Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 21697c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 21707c478bd9Sstevel@tonic-gate return (0); 21717c478bd9Sstevel@tonic-gate 21727c478bd9Sstevel@tonic-gate propout: 21737d82f0f8SDina K Nimeh if (keycopied) { 21747d82f0f8SDina K Nimeh bzero(lsp->ls_key.ck_data, 21757d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 21767d82f0f8SDina K Nimeh kmem_free(lsp->ls_key.ck_data, 21777d82f0f8SDina K Nimeh CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 21787d82f0f8SDina K Nimeh lsp->ls_key.ck_data = NULL; 21797d82f0f8SDina K Nimeh lsp->ls_key.ck_length = 0; 21807d82f0f8SDina K Nimeh } 21817d82f0f8SDina K Nimeh 21827c478bd9Sstevel@tonic-gate if (zalloced) 21837c478bd9Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, newminor); 21847d82f0f8SDina K Nimeh 21857d82f0f8SDina K Nimeh (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 21867d82f0f8SDina K Nimeh (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 21877d82f0f8SDina K Nimeh 21887d82f0f8SDina K Nimeh out: 21897d82f0f8SDina K Nimeh if (need_vn_close) { 21907d82f0f8SDina K Nimeh (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 21917d82f0f8SDina K Nimeh VN_RELE(vp); 21927d82f0f8SDina K Nimeh } 21937d82f0f8SDina K Nimeh 21947c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 21957c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 21967c478bd9Sstevel@tonic-gate return (error); 21977c478bd9Sstevel@tonic-gate } 21987c478bd9Sstevel@tonic-gate 21997c478bd9Sstevel@tonic-gate /* 22007c478bd9Sstevel@tonic-gate * unmap a file. 22017c478bd9Sstevel@tonic-gate */ 22027c478bd9Sstevel@tonic-gate static int 22037c478bd9Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 2204bd07e074Sheppo struct cred *credp, int ioctl_flag) 22057c478bd9Sstevel@tonic-gate { 22067c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 22077c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 22087c478bd9Sstevel@tonic-gate minor_t minor; 22097c478bd9Sstevel@tonic-gate 2210bd07e074Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 22117c478bd9Sstevel@tonic-gate if (klip == NULL) 22127c478bd9Sstevel@tonic-gate return (EFAULT); 22137c478bd9Sstevel@tonic-gate 22147c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 22157c478bd9Sstevel@tonic-gate if (byfilename) { 22167c478bd9Sstevel@tonic-gate minor = file_to_minor(klip->li_filename); 22177c478bd9Sstevel@tonic-gate } else { 22187c478bd9Sstevel@tonic-gate minor = klip->li_minor; 22197c478bd9Sstevel@tonic-gate } 22207c478bd9Sstevel@tonic-gate if (minor == 0) { 22217c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 22227c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 22237c478bd9Sstevel@tonic-gate return (ENXIO); 22247c478bd9Sstevel@tonic-gate } 22257c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 22263d7072f8Seschrock if (lsp == NULL || lsp->ls_vp == NULL) { 22277c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 22287c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 22297c478bd9Sstevel@tonic-gate return (ENXIO); 22307c478bd9Sstevel@tonic-gate } 22313d7072f8Seschrock 22323d7072f8Seschrock /* 223393239addSjohnlev * If it's still held open, we'll do one of three things: 223493239addSjohnlev * 223593239addSjohnlev * If no flag is set, just return EBUSY. 223693239addSjohnlev * 223793239addSjohnlev * If the 'cleanup' flag is set, unmap and remove the device when 223893239addSjohnlev * the last user finishes. 223993239addSjohnlev * 224093239addSjohnlev * If the 'force' flag is set, then we forcibly close the underlying 224193239addSjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl 224293239addSjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the 224393239addSjohnlev * device will be cleaned up appropriately. 22443d7072f8Seschrock * 22453d7072f8Seschrock * This is complicated by the fact that we may have outstanding 224693239addSjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all 22477d82f0f8SDina K Nimeh * I/O, we keep a count of the number of outstanding I/O requests 22487d82f0f8SDina K Nimeh * (ls_vp_iocount), as well as a flag to indicate that no new I/Os 22497d82f0f8SDina K Nimeh * should be dispatched (ls_vp_closereq). 22507d82f0f8SDina K Nimeh * 225193239addSjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0, 225293239addSjohnlev * and then close the underlying vnode. 22533d7072f8Seschrock */ 225493239addSjohnlev if (is_opened(lsp)) { 22553d7072f8Seschrock if (klip->li_force) { 22563d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 22573d7072f8Seschrock lsp->ls_vp_closereq = B_TRUE; 2258b3388e4fSEric Taylor /* wake up any threads waiting on dkiocstate */ 2259b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv); 22603d7072f8Seschrock while (lsp->ls_vp_iocount > 0) 22613d7072f8Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 22623d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 2263b3388e4fSEric Taylor lofi_free_handle(dev, minor, lsp, credp); 22647d82f0f8SDina K Nimeh 22653d7072f8Seschrock klip->li_minor = minor; 22667d82f0f8SDina K Nimeh mutex_exit(&lofi_lock); 22673d7072f8Seschrock (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 22683d7072f8Seschrock free_lofi_ioctl(klip); 22693d7072f8Seschrock return (0); 227093239addSjohnlev } else if (klip->li_cleanup) { 227193239addSjohnlev lsp->ls_cleanup = 1; 227293239addSjohnlev mutex_exit(&lofi_lock); 227393239addSjohnlev free_lofi_ioctl(klip); 227493239addSjohnlev return (0); 22753d7072f8Seschrock } 227693239addSjohnlev 22777c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 22787c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 22797c478bd9Sstevel@tonic-gate return (EBUSY); 22807c478bd9Sstevel@tonic-gate } 22817c478bd9Sstevel@tonic-gate 22823d7072f8Seschrock lofi_free_handle(dev, minor, lsp, credp); 22837c478bd9Sstevel@tonic-gate 22847c478bd9Sstevel@tonic-gate klip->li_minor = minor; 22857c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2286bd07e074Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 22877c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 22887c478bd9Sstevel@tonic-gate return (0); 22897c478bd9Sstevel@tonic-gate } 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate /* 22927c478bd9Sstevel@tonic-gate * get the filename given the minor number, or the minor number given 22937c478bd9Sstevel@tonic-gate * the name. 22947c478bd9Sstevel@tonic-gate */ 22953d7072f8Seschrock /*ARGSUSED*/ 22967c478bd9Sstevel@tonic-gate static int 22977c478bd9Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 2298bd07e074Sheppo struct cred *credp, int ioctl_flag) 22997c478bd9Sstevel@tonic-gate { 23007c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 23017c478bd9Sstevel@tonic-gate struct lofi_ioctl *klip; 23027c478bd9Sstevel@tonic-gate int error; 23037c478bd9Sstevel@tonic-gate minor_t minor; 23047c478bd9Sstevel@tonic-gate 2305bd07e074Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 23067c478bd9Sstevel@tonic-gate if (klip == NULL) 23077c478bd9Sstevel@tonic-gate return (EFAULT); 23087c478bd9Sstevel@tonic-gate 23097c478bd9Sstevel@tonic-gate switch (which) { 23107c478bd9Sstevel@tonic-gate case LOFI_GET_FILENAME: 23117c478bd9Sstevel@tonic-gate minor = klip->li_minor; 23127c478bd9Sstevel@tonic-gate if (minor == 0) { 23137c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23147c478bd9Sstevel@tonic-gate return (EINVAL); 23157c478bd9Sstevel@tonic-gate } 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 23187c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 23197c478bd9Sstevel@tonic-gate if (lsp == NULL) { 23207c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 23217c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23227c478bd9Sstevel@tonic-gate return (ENXIO); 23237c478bd9Sstevel@tonic-gate } 23247c478bd9Sstevel@tonic-gate (void) strcpy(klip->li_filename, lsp->ls_filename); 232587117650Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 232687117650Saalok sizeof (klip->li_algorithm)); 23277d82f0f8SDina K Nimeh klip->li_crypto_enabled = lsp->ls_crypto_enabled; 23287c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 2329bd07e074Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 23307c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23317c478bd9Sstevel@tonic-gate return (error); 23327c478bd9Sstevel@tonic-gate case LOFI_GET_MINOR: 23337c478bd9Sstevel@tonic-gate mutex_enter(&lofi_lock); 23347c478bd9Sstevel@tonic-gate klip->li_minor = file_to_minor(klip->li_filename); 23357d82f0f8SDina K Nimeh /* caller should not depend on klip->li_crypto_enabled here */ 23367c478bd9Sstevel@tonic-gate mutex_exit(&lofi_lock); 23377c478bd9Sstevel@tonic-gate if (klip->li_minor == 0) { 23387c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23397c478bd9Sstevel@tonic-gate return (ENOENT); 23407c478bd9Sstevel@tonic-gate } 2341bd07e074Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 23427c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23437c478bd9Sstevel@tonic-gate return (error); 234487117650Saalok case LOFI_CHECK_COMPRESSED: 234587117650Saalok mutex_enter(&lofi_lock); 234687117650Saalok klip->li_minor = file_to_minor(klip->li_filename); 234787117650Saalok mutex_exit(&lofi_lock); 234887117650Saalok if (klip->li_minor == 0) { 234987117650Saalok free_lofi_ioctl(klip); 235087117650Saalok return (ENOENT); 235187117650Saalok } 235287117650Saalok mutex_enter(&lofi_lock); 235387117650Saalok lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 235487117650Saalok if (lsp == NULL) { 235587117650Saalok mutex_exit(&lofi_lock); 235687117650Saalok free_lofi_ioctl(klip); 235787117650Saalok return (ENXIO); 235887117650Saalok } 235987117650Saalok ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0); 236087117650Saalok 236187117650Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 236287117650Saalok sizeof (klip->li_algorithm)); 236387117650Saalok mutex_exit(&lofi_lock); 236487117650Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 236587117650Saalok free_lofi_ioctl(klip); 236687117650Saalok return (error); 23677c478bd9Sstevel@tonic-gate default: 23687c478bd9Sstevel@tonic-gate free_lofi_ioctl(klip); 23697c478bd9Sstevel@tonic-gate return (EINVAL); 23707c478bd9Sstevel@tonic-gate } 23717c478bd9Sstevel@tonic-gate 23727c478bd9Sstevel@tonic-gate } 23737c478bd9Sstevel@tonic-gate 23747c478bd9Sstevel@tonic-gate static int 23757c478bd9Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 23767c478bd9Sstevel@tonic-gate int *rvalp) 23777c478bd9Sstevel@tonic-gate { 23787c478bd9Sstevel@tonic-gate int error; 23797c478bd9Sstevel@tonic-gate enum dkio_state dkstate; 23807c478bd9Sstevel@tonic-gate struct lofi_state *lsp; 23817c478bd9Sstevel@tonic-gate minor_t minor; 23827c478bd9Sstevel@tonic-gate 23837c478bd9Sstevel@tonic-gate minor = getminor(dev); 23847c478bd9Sstevel@tonic-gate /* lofi ioctls only apply to the master device */ 23857c478bd9Sstevel@tonic-gate if (minor == 0) { 23867c478bd9Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 23877c478bd9Sstevel@tonic-gate 23887c478bd9Sstevel@tonic-gate /* 23897c478bd9Sstevel@tonic-gate * the query command only need read-access - i.e., normal 23907c478bd9Sstevel@tonic-gate * users are allowed to do those on the ctl device as 23917c478bd9Sstevel@tonic-gate * long as they can open it read-only. 23927c478bd9Sstevel@tonic-gate */ 23937c478bd9Sstevel@tonic-gate switch (cmd) { 23947c478bd9Sstevel@tonic-gate case LOFI_MAP_FILE: 23957c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 23967c478bd9Sstevel@tonic-gate return (EPERM); 2397bd07e074Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 23987c478bd9Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR: 23997c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 24007c478bd9Sstevel@tonic-gate return (EPERM); 2401bd07e074Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 24027c478bd9Sstevel@tonic-gate case LOFI_UNMAP_FILE: 24037c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 24047c478bd9Sstevel@tonic-gate return (EPERM); 2405bd07e074Sheppo return (lofi_unmap_file(dev, lip, 1, credp, flag)); 24067c478bd9Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR: 24077c478bd9Sstevel@tonic-gate if ((flag & FWRITE) == 0) 24087c478bd9Sstevel@tonic-gate return (EPERM); 2409bd07e074Sheppo return (lofi_unmap_file(dev, lip, 0, credp, flag)); 24107c478bd9Sstevel@tonic-gate case LOFI_GET_FILENAME: 24117c478bd9Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 2412bd07e074Sheppo credp, flag)); 24137c478bd9Sstevel@tonic-gate case LOFI_GET_MINOR: 24147c478bd9Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 2415bd07e074Sheppo credp, flag)); 24167c478bd9Sstevel@tonic-gate case LOFI_GET_MAXMINOR: 2417bd07e074Sheppo error = ddi_copyout(&lofi_max_files, &lip->li_minor, 2418bd07e074Sheppo sizeof (lofi_max_files), flag); 24197c478bd9Sstevel@tonic-gate if (error) 24207c478bd9Sstevel@tonic-gate return (EFAULT); 24217c478bd9Sstevel@tonic-gate return (0); 242287117650Saalok case LOFI_CHECK_COMPRESSED: 242387117650Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 242487117650Saalok credp, flag)); 24257c478bd9Sstevel@tonic-gate default: 24267c478bd9Sstevel@tonic-gate break; 24277c478bd9Sstevel@tonic-gate } 24287c478bd9Sstevel@tonic-gate } 24297c478bd9Sstevel@tonic-gate 2430b3388e4fSEric Taylor mutex_enter(&lofi_lock); 24317c478bd9Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 2432b3388e4fSEric Taylor if (lsp == NULL || lsp->ls_vp_closereq) { 2433b3388e4fSEric Taylor mutex_exit(&lofi_lock); 24347c478bd9Sstevel@tonic-gate return (ENXIO); 2435b3388e4fSEric Taylor } 2436b3388e4fSEric Taylor mutex_exit(&lofi_lock); 24377c478bd9Sstevel@tonic-gate 24383d7072f8Seschrock /* 24393d7072f8Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 24403d7072f8Seschrock * EIO as if the device was no longer present. 24413d7072f8Seschrock */ 24423d7072f8Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 24433d7072f8Seschrock return (EIO); 24443d7072f8Seschrock 24457c478bd9Sstevel@tonic-gate /* these are for faking out utilities like newfs */ 24467c478bd9Sstevel@tonic-gate switch (cmd) { 24477c478bd9Sstevel@tonic-gate case DKIOCGVTOC: 24487c478bd9Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) { 24497c478bd9Sstevel@tonic-gate case DDI_MODEL_ILP32: { 24507c478bd9Sstevel@tonic-gate struct vtoc32 vtoc32; 24517c478bd9Sstevel@tonic-gate 24527c478bd9Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32); 24537c478bd9Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg, 24547c478bd9Sstevel@tonic-gate sizeof (struct vtoc32), flag)) 24557c478bd9Sstevel@tonic-gate return (EFAULT); 24567c478bd9Sstevel@tonic-gate break; 24577c478bd9Sstevel@tonic-gate } 24587c478bd9Sstevel@tonic-gate 24597c478bd9Sstevel@tonic-gate case DDI_MODEL_NONE: 24607c478bd9Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 24617c478bd9Sstevel@tonic-gate sizeof (struct vtoc), flag)) 24627c478bd9Sstevel@tonic-gate return (EFAULT); 24637c478bd9Sstevel@tonic-gate break; 24647c478bd9Sstevel@tonic-gate } 24657c478bd9Sstevel@tonic-gate return (0); 24667c478bd9Sstevel@tonic-gate case DKIOCINFO: 2467bd07e074Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg, 2468bd07e074Sheppo sizeof (struct dk_cinfo), flag); 24697c478bd9Sstevel@tonic-gate if (error) 24707c478bd9Sstevel@tonic-gate return (EFAULT); 24717c478bd9Sstevel@tonic-gate return (0); 24727c478bd9Sstevel@tonic-gate case DKIOCG_VIRTGEOM: 24737c478bd9Sstevel@tonic-gate case DKIOCG_PHYGEOM: 24747c478bd9Sstevel@tonic-gate case DKIOCGGEOM: 2475bd07e074Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 2476bd07e074Sheppo sizeof (struct dk_geom), flag); 24777c478bd9Sstevel@tonic-gate if (error) 24787c478bd9Sstevel@tonic-gate return (EFAULT); 24797c478bd9Sstevel@tonic-gate return (0); 24807c478bd9Sstevel@tonic-gate case DKIOCSTATE: 24813d7072f8Seschrock /* 24823d7072f8Seschrock * Normally, lofi devices are always in the INSERTED state. If 24833d7072f8Seschrock * a device is forcefully unmapped, then the device transitions 24843d7072f8Seschrock * to the DKIO_DEV_GONE state. 24853d7072f8Seschrock */ 24863d7072f8Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 24873d7072f8Seschrock flag) != 0) 24883d7072f8Seschrock return (EFAULT); 24893d7072f8Seschrock 24903d7072f8Seschrock mutex_enter(&lsp->ls_vp_lock); 2491b3388e4fSEric Taylor lsp->ls_vp_iocount++; 2492b3388e4fSEric Taylor while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 2493b3388e4fSEric Taylor (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) && 2494b3388e4fSEric Taylor !lsp->ls_vp_closereq) { 24953d7072f8Seschrock /* 24963d7072f8Seschrock * By virtue of having the device open, we know that 24973d7072f8Seschrock * 'lsp' will remain valid when we return. 24983d7072f8Seschrock */ 24993d7072f8Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv, 25003d7072f8Seschrock &lsp->ls_vp_lock)) { 2501b3388e4fSEric Taylor lsp->ls_vp_iocount--; 2502b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv); 25033d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 25043d7072f8Seschrock return (EINTR); 25053d7072f8Seschrock } 25063d7072f8Seschrock } 25073d7072f8Seschrock 2508b3388e4fSEric Taylor dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ? 2509b3388e4fSEric Taylor DKIO_INSERTED : DKIO_DEV_GONE); 2510b3388e4fSEric Taylor lsp->ls_vp_iocount--; 2511b3388e4fSEric Taylor cv_broadcast(&lsp->ls_vp_cv); 25123d7072f8Seschrock mutex_exit(&lsp->ls_vp_lock); 25133d7072f8Seschrock 25143d7072f8Seschrock if (ddi_copyout(&dkstate, (void *)arg, 25153d7072f8Seschrock sizeof (dkstate), flag) != 0) 25167c478bd9Sstevel@tonic-gate return (EFAULT); 25177c478bd9Sstevel@tonic-gate return (0); 25187c478bd9Sstevel@tonic-gate default: 25197c478bd9Sstevel@tonic-gate return (ENOTTY); 25207c478bd9Sstevel@tonic-gate } 25217c478bd9Sstevel@tonic-gate } 25227c478bd9Sstevel@tonic-gate 25237c478bd9Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = { 25247c478bd9Sstevel@tonic-gate lofi_open, /* open */ 25257c478bd9Sstevel@tonic-gate lofi_close, /* close */ 25267c478bd9Sstevel@tonic-gate lofi_strategy, /* strategy */ 25277c478bd9Sstevel@tonic-gate nodev, /* print */ 25287c478bd9Sstevel@tonic-gate nodev, /* dump */ 25297c478bd9Sstevel@tonic-gate lofi_read, /* read */ 25307c478bd9Sstevel@tonic-gate lofi_write, /* write */ 25317c478bd9Sstevel@tonic-gate lofi_ioctl, /* ioctl */ 25327c478bd9Sstevel@tonic-gate nodev, /* devmap */ 25337c478bd9Sstevel@tonic-gate nodev, /* mmap */ 25347c478bd9Sstevel@tonic-gate nodev, /* segmap */ 25357c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 25367c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 25377c478bd9Sstevel@tonic-gate 0, /* streamtab */ 25387c478bd9Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 25397c478bd9Sstevel@tonic-gate CB_REV, 25407c478bd9Sstevel@tonic-gate lofi_aread, 25417c478bd9Sstevel@tonic-gate lofi_awrite 25427c478bd9Sstevel@tonic-gate }; 25437c478bd9Sstevel@tonic-gate 25447c478bd9Sstevel@tonic-gate static struct dev_ops lofi_ops = { 25457c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 25467c478bd9Sstevel@tonic-gate 0, /* refcnt */ 25477c478bd9Sstevel@tonic-gate lofi_info, /* info */ 25487c478bd9Sstevel@tonic-gate nulldev, /* identify */ 25497c478bd9Sstevel@tonic-gate nulldev, /* probe */ 25507c478bd9Sstevel@tonic-gate lofi_attach, /* attach */ 25517c478bd9Sstevel@tonic-gate lofi_detach, /* detach */ 25527c478bd9Sstevel@tonic-gate nodev, /* reset */ 25537c478bd9Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */ 255419397407SSherry Moore NULL, /* no bus operations */ 255519397407SSherry Moore NULL, /* power */ 255619397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 25577c478bd9Sstevel@tonic-gate }; 25587c478bd9Sstevel@tonic-gate 25597c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 25607c478bd9Sstevel@tonic-gate &mod_driverops, 256119397407SSherry Moore "loopback file driver", 25627c478bd9Sstevel@tonic-gate &lofi_ops, 25637c478bd9Sstevel@tonic-gate }; 25647c478bd9Sstevel@tonic-gate 25657c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 25667c478bd9Sstevel@tonic-gate MODREV_1, 25677c478bd9Sstevel@tonic-gate &modldrv, 25687c478bd9Sstevel@tonic-gate NULL 25697c478bd9Sstevel@tonic-gate }; 25707c478bd9Sstevel@tonic-gate 25717c478bd9Sstevel@tonic-gate int 25727c478bd9Sstevel@tonic-gate _init(void) 25737c478bd9Sstevel@tonic-gate { 25747c478bd9Sstevel@tonic-gate int error; 25757c478bd9Sstevel@tonic-gate 25767c478bd9Sstevel@tonic-gate error = ddi_soft_state_init(&lofi_statep, 25777c478bd9Sstevel@tonic-gate sizeof (struct lofi_state), 0); 25787c478bd9Sstevel@tonic-gate if (error) 25797c478bd9Sstevel@tonic-gate return (error); 25807c478bd9Sstevel@tonic-gate 25817c478bd9Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL); 25827c478bd9Sstevel@tonic-gate error = mod_install(&modlinkage); 25837c478bd9Sstevel@tonic-gate if (error) { 25847c478bd9Sstevel@tonic-gate mutex_destroy(&lofi_lock); 25857c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 25867c478bd9Sstevel@tonic-gate } 25877c478bd9Sstevel@tonic-gate 25887c478bd9Sstevel@tonic-gate return (error); 25897c478bd9Sstevel@tonic-gate } 25907c478bd9Sstevel@tonic-gate 25917c478bd9Sstevel@tonic-gate int 25927c478bd9Sstevel@tonic-gate _fini(void) 25937c478bd9Sstevel@tonic-gate { 25947c478bd9Sstevel@tonic-gate int error; 25957c478bd9Sstevel@tonic-gate 25967c478bd9Sstevel@tonic-gate if (lofi_busy()) 25977c478bd9Sstevel@tonic-gate return (EBUSY); 25987c478bd9Sstevel@tonic-gate 25997c478bd9Sstevel@tonic-gate error = mod_remove(&modlinkage); 26007c478bd9Sstevel@tonic-gate if (error) 26017c478bd9Sstevel@tonic-gate return (error); 26027c478bd9Sstevel@tonic-gate 26037c478bd9Sstevel@tonic-gate mutex_destroy(&lofi_lock); 26047c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 26057c478bd9Sstevel@tonic-gate 26067c478bd9Sstevel@tonic-gate return (error); 26077c478bd9Sstevel@tonic-gate } 26087c478bd9Sstevel@tonic-gate 26097c478bd9Sstevel@tonic-gate int 26107c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 26117c478bd9Sstevel@tonic-gate { 26127c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 26137c478bd9Sstevel@tonic-gate } 2614