10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51657Sheppo * Common Development and Distribution License (the "License"). 61657Sheppo * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 228669SDina.Nimeh@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * lofi (loopback file) driver - allows you to attach a file to a device, 280Sstevel@tonic-gate * which can then be accessed through that device. The simple model is that 290Sstevel@tonic-gate * you tell lofi to open a file, and then use the block device you get as 300Sstevel@tonic-gate * you would any block device. lofi translates access to the block device 310Sstevel@tonic-gate * into I/O on the underlying file. This is mostly useful for 320Sstevel@tonic-gate * mounting images of filesystems. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * lofi is controlled through /dev/lofictl - this is the only device exported 350Sstevel@tonic-gate * during attach, and is minor number 0. lofiadm communicates with lofi through 360Sstevel@tonic-gate * ioctls on this device. When a file is attached to lofi, block and character 370Sstevel@tonic-gate * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices 380Sstevel@tonic-gate * are identified by their minor number, and the minor number is also used 390Sstevel@tonic-gate * as the name in /dev/lofi. If we ever decide to support virtual disks, 400Sstevel@tonic-gate * we'll have to divide the minor number space to identify fdisk partitions 410Sstevel@tonic-gate * and slices, and the name will then be the minor number shifted down a 420Sstevel@tonic-gate * few bits. Minor devices are tracked with state structures handled with 430Sstevel@tonic-gate * ddi_soft_state(9F) for simplicity. 440Sstevel@tonic-gate * 450Sstevel@tonic-gate * A file attached to lofi is opened when attached and not closed until 460Sstevel@tonic-gate * explicitly detached from lofi. This seems more sensible than deferring 470Sstevel@tonic-gate * the open until the /dev/lofi device is opened, for a number of reasons. 480Sstevel@tonic-gate * One is that any failure is likely to be noticed by the person (or script) 490Sstevel@tonic-gate * running lofiadm. Another is that it would be a security problem if the 500Sstevel@tonic-gate * file was replaced by another one after being added but before being opened. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * The only hard part about lofi is the ioctls. In order to support things 530Sstevel@tonic-gate * like 'newfs' on a lofi device, it needs to support certain disk ioctls. 540Sstevel@tonic-gate * So it has to fake disk geometry and partition information. More may need 550Sstevel@tonic-gate * to be faked if your favorite utility doesn't work and you think it should 560Sstevel@tonic-gate * (fdformat doesn't work because it really wants to know the type of floppy 570Sstevel@tonic-gate * controller to talk to, and that didn't seem easy to fake. Or possibly even 580Sstevel@tonic-gate * necessary, since we have mkfs_pcfs now). 590Sstevel@tonic-gate * 604451Seschrock * Normally, a lofi device cannot be detached if it is open (i.e. busy). To 614451Seschrock * support simulation of hotplug events, an optional force flag is provided. 624451Seschrock * If a lofi device is open when a force detach is requested, then the 634451Seschrock * underlying file is closed and any subsequent operations return EIO. When the 644451Seschrock * device is closed for the last time, it will be cleaned up at that time. In 654451Seschrock * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is 664451Seschrock * detached but not removed. 674451Seschrock * 680Sstevel@tonic-gate * Known problems: 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * UFS logging. Mounting a UFS filesystem image "logging" 710Sstevel@tonic-gate * works for basic copy testing but wedges during a build of ON through 720Sstevel@tonic-gate * that image. Some deadlock in lufs holding the log mutex and then 730Sstevel@tonic-gate * getting stuck on a buf. So for now, don't do that. 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * Direct I/O. Since the filesystem data is being cached in the buffer 760Sstevel@tonic-gate * cache, _and_ again in the underlying filesystem, it's tempting to 770Sstevel@tonic-gate * enable direct I/O on the underlying file. Don't, because that deadlocks. 780Sstevel@tonic-gate * I think to fix the cache-twice problem we might need filesystem support. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * lofi on itself. The simple lock strategy (lofi_lock) precludes this 810Sstevel@tonic-gate * because you'll be in lofi_ioctl, holding the lock when you open the 820Sstevel@tonic-gate * file, which, if it's lofi, will grab lofi_lock. We prevent this for 830Sstevel@tonic-gate * now, though not using ddi_soft_state(9F) would make it possible to 840Sstevel@tonic-gate * do. Though it would still be silly. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * Interesting things to do: 870Sstevel@tonic-gate * 880Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically. 890Sstevel@tonic-gate * 900Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not 910Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't 928313SDina.Nimeh@Sun.Com * need to fake a geometry, however, it may be relevant if you're replacing 938313SDina.Nimeh@Sun.Com * metadisk, or using lofi to get crypto. 948313SDina.Nimeh@Sun.Com * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1 958313SDina.Nimeh@Sun.Com * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home. 968313SDina.Nimeh@Sun.Com * In fact this even makes sense if you have lofi "above" metadisk. 970Sstevel@tonic-gate * 988313SDina.Nimeh@Sun.Com * Encryption: 998313SDina.Nimeh@Sun.Com * Each lofi device can have its own symmetric key and cipher. 1008313SDina.Nimeh@Sun.Com * They are passed to us by lofiadm(1m) in the correct format for use 1018313SDina.Nimeh@Sun.Com * with the misc/kcf crypto_* routines. 1028313SDina.Nimeh@Sun.Com * 1038313SDina.Nimeh@Sun.Com * Each block has its own IV, that is calculated in lofi_blk_mech(), based 1048313SDina.Nimeh@Sun.Com * on the "master" key held in the lsp and the block number of the buffer. 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #include <sys/types.h> 1085643Saalok #include <netinet/in.h> 1090Sstevel@tonic-gate #include <sys/sysmacros.h> 1100Sstevel@tonic-gate #include <sys/uio.h> 1110Sstevel@tonic-gate #include <sys/kmem.h> 1120Sstevel@tonic-gate #include <sys/cred.h> 1130Sstevel@tonic-gate #include <sys/mman.h> 1140Sstevel@tonic-gate #include <sys/errno.h> 1150Sstevel@tonic-gate #include <sys/aio_req.h> 1160Sstevel@tonic-gate #include <sys/stat.h> 1170Sstevel@tonic-gate #include <sys/file.h> 1180Sstevel@tonic-gate #include <sys/modctl.h> 1190Sstevel@tonic-gate #include <sys/conf.h> 1200Sstevel@tonic-gate #include <sys/debug.h> 1210Sstevel@tonic-gate #include <sys/vnode.h> 1220Sstevel@tonic-gate #include <sys/lofi.h> 1230Sstevel@tonic-gate #include <sys/fcntl.h> 1240Sstevel@tonic-gate #include <sys/pathname.h> 1250Sstevel@tonic-gate #include <sys/filio.h> 1260Sstevel@tonic-gate #include <sys/fdio.h> 1270Sstevel@tonic-gate #include <sys/open.h> 1280Sstevel@tonic-gate #include <sys/disp.h> 1290Sstevel@tonic-gate #include <vm/seg_map.h> 1300Sstevel@tonic-gate #include <sys/ddi.h> 1310Sstevel@tonic-gate #include <sys/sunddi.h> 1325643Saalok #include <sys/zmod.h> 1338313SDina.Nimeh@Sun.Com #include <sys/crypto/common.h> 1348313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h> 135*8996SAlok.Aggarwal@Sun.COM #include <LzmaDec.h> 1368313SDina.Nimeh@Sun.Com 1378313SDina.Nimeh@Sun.Com /* 1388313SDina.Nimeh@Sun.Com * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h. 1398313SDina.Nimeh@Sun.Com * Crypto metadata, if it exists, is located at the end of the boot block 1408313SDina.Nimeh@Sun.Com * (BBOFF + BBSIZE, which is SBOFF). The super block and everything after 1418313SDina.Nimeh@Sun.Com * is offset by the size of the crypto metadata which is handled by 1428313SDina.Nimeh@Sun.Com * lsp->ls_crypto_offset. 1438313SDina.Nimeh@Sun.Com */ 1448313SDina.Nimeh@Sun.Com #define CRYOFF ((off_t)8192) 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks" 1475643Saalok #define SIZE_PROP_NAME "Size" 1480Sstevel@tonic-gate 1498313SDina.Nimeh@Sun.Com #define SETUP_C_DATA(cd, buf, len) \ 1508313SDina.Nimeh@Sun.Com (cd).cd_format = CRYPTO_DATA_RAW; \ 1518313SDina.Nimeh@Sun.Com (cd).cd_offset = 0; \ 1528313SDina.Nimeh@Sun.Com (cd).cd_miscdata = NULL; \ 1538313SDina.Nimeh@Sun.Com (cd).cd_length = (len); \ 1548313SDina.Nimeh@Sun.Com (cd).cd_raw.iov_base = (buf); \ 1558313SDina.Nimeh@Sun.Com (cd).cd_raw.iov_len = (len); 1568313SDina.Nimeh@Sun.Com 1578313SDina.Nimeh@Sun.Com #define UIO_CHECK(uio) \ 1588313SDina.Nimeh@Sun.Com if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \ 1598313SDina.Nimeh@Sun.Com ((uio)->uio_resid % DEV_BSIZE) != 0) { \ 1608313SDina.Nimeh@Sun.Com return (EINVAL); \ 1618313SDina.Nimeh@Sun.Com } 1628313SDina.Nimeh@Sun.Com 1638313SDina.Nimeh@Sun.Com static dev_info_t *lofi_dip = NULL; 1648313SDina.Nimeh@Sun.Com static void *lofi_statep = NULL; 1650Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */ 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the 1690Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively 1700Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy, 1710Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of 1720Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for 1730Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should 1740Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the 1750Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic 1760Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on 1770Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE. 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE; 1800Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */ 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES; 1838313SDina.Nimeh@Sun.Com const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC; 1840Sstevel@tonic-gate 1855643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 1865643Saalok size_t *destlen, int level); 1875643Saalok 188*8996SAlok.Aggarwal@Sun.COM static int lzma_decompress(void *src, size_t srclen, void *dst, 189*8996SAlok.Aggarwal@Sun.COM size_t *dstlen, int level); 190*8996SAlok.Aggarwal@Sun.COM 1915643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { 1925643Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */ 1935643Saalok {gzip_decompress, NULL, 6, "gzip-6"}, 194*8996SAlok.Aggarwal@Sun.COM {gzip_decompress, NULL, 9, "gzip-9"}, 195*8996SAlok.Aggarwal@Sun.COM {lzma_decompress, NULL, 0, "lzma"} 1965643Saalok }; 1975643Saalok 198*8996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/ 199*8996SAlok.Aggarwal@Sun.COM static void 200*8996SAlok.Aggarwal@Sun.COM *SzAlloc(void *p, size_t size) 201*8996SAlok.Aggarwal@Sun.COM { 202*8996SAlok.Aggarwal@Sun.COM return (kmem_alloc(size, KM_SLEEP)); 203*8996SAlok.Aggarwal@Sun.COM } 204*8996SAlok.Aggarwal@Sun.COM 205*8996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/ 206*8996SAlok.Aggarwal@Sun.COM static void 207*8996SAlok.Aggarwal@Sun.COM SzFree(void *p, void *address, size_t size) 208*8996SAlok.Aggarwal@Sun.COM { 209*8996SAlok.Aggarwal@Sun.COM kmem_free(address, size); 210*8996SAlok.Aggarwal@Sun.COM } 211*8996SAlok.Aggarwal@Sun.COM 212*8996SAlok.Aggarwal@Sun.COM static ISzAlloc g_Alloc = { SzAlloc, SzFree }; 213*8996SAlok.Aggarwal@Sun.COM 2140Sstevel@tonic-gate static int 2150Sstevel@tonic-gate lofi_busy(void) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate minor_t minor; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * We need to make sure no mappings exist - mod_remove won't 2210Sstevel@tonic-gate * help because the device isn't open. 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate mutex_enter(&lofi_lock); 2240Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 2250Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, minor) != NULL) { 2260Sstevel@tonic-gate mutex_exit(&lofi_lock); 2270Sstevel@tonic-gate return (EBUSY); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate mutex_exit(&lofi_lock); 2310Sstevel@tonic-gate return (0); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate static int 2350Sstevel@tonic-gate is_opened(struct lofi_state *lsp) 2360Sstevel@tonic-gate { 2370Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2380Sstevel@tonic-gate return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate static int 2420Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2450Sstevel@tonic-gate switch (otyp) { 2460Sstevel@tonic-gate case OTYP_CHR: 2470Sstevel@tonic-gate lsp->ls_chr_open = 1; 2480Sstevel@tonic-gate break; 2490Sstevel@tonic-gate case OTYP_BLK: 2500Sstevel@tonic-gate lsp->ls_blk_open = 1; 2510Sstevel@tonic-gate break; 2520Sstevel@tonic-gate case OTYP_LYR: 2530Sstevel@tonic-gate lsp->ls_lyr_open_count++; 2540Sstevel@tonic-gate break; 2550Sstevel@tonic-gate default: 2560Sstevel@tonic-gate return (-1); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate return (0); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate static void 2620Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp) 2630Sstevel@tonic-gate { 2640Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2650Sstevel@tonic-gate switch (otyp) { 2660Sstevel@tonic-gate case OTYP_CHR: 2670Sstevel@tonic-gate lsp->ls_chr_open = 0; 2680Sstevel@tonic-gate break; 2690Sstevel@tonic-gate case OTYP_BLK: 2700Sstevel@tonic-gate lsp->ls_blk_open = 0; 2710Sstevel@tonic-gate break; 2720Sstevel@tonic-gate case OTYP_LYR: 2730Sstevel@tonic-gate lsp->ls_lyr_open_count--; 2740Sstevel@tonic-gate break; 2750Sstevel@tonic-gate default: 2760Sstevel@tonic-gate break; 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2804451Seschrock static void 2818313SDina.Nimeh@Sun.Com lofi_free_crypto(struct lofi_state *lsp) 2828313SDina.Nimeh@Sun.Com { 2838313SDina.Nimeh@Sun.Com ASSERT(mutex_owned(&lofi_lock)); 2848313SDina.Nimeh@Sun.Com 2858313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) { 2868313SDina.Nimeh@Sun.Com /* 2878313SDina.Nimeh@Sun.Com * Clean up the crypto state so that it doesn't hang around 2888313SDina.Nimeh@Sun.Com * in memory after we are done with it. 2898313SDina.Nimeh@Sun.Com */ 2908313SDina.Nimeh@Sun.Com bzero(lsp->ls_key.ck_data, 2918313SDina.Nimeh@Sun.Com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 2928313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_key.ck_data, 2938313SDina.Nimeh@Sun.Com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 2948313SDina.Nimeh@Sun.Com lsp->ls_key.ck_data = NULL; 2958313SDina.Nimeh@Sun.Com lsp->ls_key.ck_length = 0; 2968313SDina.Nimeh@Sun.Com 2978313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param != NULL) { 2988313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_mech.cm_param, 2998313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len); 3008313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param = NULL; 3018313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len = 0; 3028313SDina.Nimeh@Sun.Com } 3038313SDina.Nimeh@Sun.Com 3048313SDina.Nimeh@Sun.Com if (lsp->ls_iv_mech.cm_param != NULL) { 3058313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_iv_mech.cm_param, 3068313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param_len); 3078313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param = NULL; 3088313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param_len = 0; 3098313SDina.Nimeh@Sun.Com } 3108313SDina.Nimeh@Sun.Com 3118313SDina.Nimeh@Sun.Com mutex_destroy(&lsp->ls_crypto_lock); 3128313SDina.Nimeh@Sun.Com } 3138313SDina.Nimeh@Sun.Com } 3148313SDina.Nimeh@Sun.Com 3158313SDina.Nimeh@Sun.Com static void 3164451Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp, 3174451Seschrock cred_t *credp) 3184451Seschrock { 3194451Seschrock dev_t newdev; 3204451Seschrock char namebuf[50]; 3214451Seschrock 3228313SDina.Nimeh@Sun.Com ASSERT(mutex_owned(&lofi_lock)); 3238313SDina.Nimeh@Sun.Com 3248313SDina.Nimeh@Sun.Com lofi_free_crypto(lsp); 3258313SDina.Nimeh@Sun.Com 3264451Seschrock if (lsp->ls_vp) { 3275331Samw (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 3285331Samw 1, 0, credp, NULL); 3294451Seschrock VN_RELE(lsp->ls_vp); 3304451Seschrock lsp->ls_vp = NULL; 3314451Seschrock } 3324451Seschrock 3334451Seschrock newdev = makedevice(getmajor(dev), minor); 3344451Seschrock (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 3354451Seschrock (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 3364451Seschrock 3374451Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 3384451Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 3394451Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 3404451Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 3414451Seschrock 3424451Seschrock kmem_free(lsp->ls_filename, lsp->ls_filename_sz); 3434451Seschrock taskq_destroy(lsp->ls_taskq); 3444451Seschrock if (lsp->ls_kstat) { 3454451Seschrock kstat_delete(lsp->ls_kstat); 3464451Seschrock mutex_destroy(&lsp->ls_kstat_lock); 3474451Seschrock } 3486791Saalok 3496791Saalok if (lsp->ls_uncomp_seg_sz > 0) { 3506791Saalok kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 3516791Saalok lsp->ls_uncomp_seg_sz = 0; 3526791Saalok } 3534451Seschrock ddi_soft_state_free(lofi_statep, minor); 3544451Seschrock } 3554451Seschrock 3564451Seschrock /*ARGSUSED*/ 3570Sstevel@tonic-gate static int 3580Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate minor_t minor; 3610Sstevel@tonic-gate struct lofi_state *lsp; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate mutex_enter(&lofi_lock); 3640Sstevel@tonic-gate minor = getminor(*devp); 3650Sstevel@tonic-gate if (minor == 0) { 3660Sstevel@tonic-gate /* master control device */ 3670Sstevel@tonic-gate /* must be opened exclusively */ 3680Sstevel@tonic-gate if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) { 3690Sstevel@tonic-gate mutex_exit(&lofi_lock); 3700Sstevel@tonic-gate return (EINVAL); 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, 0); 3730Sstevel@tonic-gate if (lsp == NULL) { 3740Sstevel@tonic-gate mutex_exit(&lofi_lock); 3750Sstevel@tonic-gate return (ENXIO); 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate if (is_opened(lsp)) { 3780Sstevel@tonic-gate mutex_exit(&lofi_lock); 3790Sstevel@tonic-gate return (EBUSY); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate (void) mark_opened(lsp, OTYP_CHR); 3820Sstevel@tonic-gate mutex_exit(&lofi_lock); 3830Sstevel@tonic-gate return (0); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* otherwise, the mapping should already exist */ 3870Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 3880Sstevel@tonic-gate if (lsp == NULL) { 3890Sstevel@tonic-gate mutex_exit(&lofi_lock); 3900Sstevel@tonic-gate return (EINVAL); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3934451Seschrock if (lsp->ls_vp == NULL) { 3944451Seschrock mutex_exit(&lofi_lock); 3954451Seschrock return (ENXIO); 3964451Seschrock } 3974451Seschrock 3980Sstevel@tonic-gate if (mark_opened(lsp, otyp) == -1) { 3990Sstevel@tonic-gate mutex_exit(&lofi_lock); 4000Sstevel@tonic-gate return (EINVAL); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate mutex_exit(&lofi_lock); 4040Sstevel@tonic-gate return (0); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4074451Seschrock /*ARGSUSED*/ 4080Sstevel@tonic-gate static int 4090Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp) 4100Sstevel@tonic-gate { 4110Sstevel@tonic-gate minor_t minor; 4120Sstevel@tonic-gate struct lofi_state *lsp; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate mutex_enter(&lofi_lock); 4150Sstevel@tonic-gate minor = getminor(dev); 4160Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 4170Sstevel@tonic-gate if (lsp == NULL) { 4180Sstevel@tonic-gate mutex_exit(&lofi_lock); 4190Sstevel@tonic-gate return (EINVAL); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate mark_closed(lsp, otyp); 4224451Seschrock 4234451Seschrock /* 4246734Sjohnlev * If we forcibly closed the underlying device (li_force), or 4256734Sjohnlev * asked for cleanup (li_cleanup), finish up if we're the last 4266734Sjohnlev * out of the door. 4274451Seschrock */ 4286734Sjohnlev if (minor != 0 && !is_opened(lsp) && 4296734Sjohnlev (lsp->ls_cleanup || lsp->ls_vp == NULL)) 4304451Seschrock lofi_free_handle(dev, minor, lsp, credp); 4316734Sjohnlev 4320Sstevel@tonic-gate mutex_exit(&lofi_lock); 4330Sstevel@tonic-gate return (0); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4368313SDina.Nimeh@Sun.Com /* 4378313SDina.Nimeh@Sun.Com * Sets the mechanism's initialization vector (IV) if one is needed. 4388313SDina.Nimeh@Sun.Com * The IV is computed from the data block number. lsp->ls_mech is 4398313SDina.Nimeh@Sun.Com * altered so that: 4408313SDina.Nimeh@Sun.Com * lsp->ls_mech.cm_param_len is set to the IV len. 4418313SDina.Nimeh@Sun.Com * lsp->ls_mech.cm_param is set to the IV. 4428313SDina.Nimeh@Sun.Com */ 4438313SDina.Nimeh@Sun.Com static int 4448313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno) 4458313SDina.Nimeh@Sun.Com { 4468313SDina.Nimeh@Sun.Com int ret; 4478313SDina.Nimeh@Sun.Com crypto_data_t cdata; 4488313SDina.Nimeh@Sun.Com char *iv; 4498313SDina.Nimeh@Sun.Com size_t iv_len; 4508313SDina.Nimeh@Sun.Com size_t min; 4518313SDina.Nimeh@Sun.Com void *data; 4528313SDina.Nimeh@Sun.Com size_t datasz; 4538313SDina.Nimeh@Sun.Com 4548313SDina.Nimeh@Sun.Com ASSERT(mutex_owned(&lsp->ls_crypto_lock)); 4558313SDina.Nimeh@Sun.Com 4568313SDina.Nimeh@Sun.Com if (lsp == NULL) 4578313SDina.Nimeh@Sun.Com return (CRYPTO_DEVICE_ERROR); 4588313SDina.Nimeh@Sun.Com 4598313SDina.Nimeh@Sun.Com /* lsp->ls_mech.cm_param{_len} has already been set for static iv */ 4608313SDina.Nimeh@Sun.Com if (lsp->ls_iv_type == IVM_NONE) { 4618313SDina.Nimeh@Sun.Com return (CRYPTO_SUCCESS); 4628313SDina.Nimeh@Sun.Com } 4638313SDina.Nimeh@Sun.Com 4648313SDina.Nimeh@Sun.Com /* 4658313SDina.Nimeh@Sun.Com * if kmem already alloced from previous call and it's the same size 4668313SDina.Nimeh@Sun.Com * we need now, just recycle it; allocate new kmem only if we have to 4678313SDina.Nimeh@Sun.Com */ 4688313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param == NULL || 4698313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len != lsp->ls_iv_len) { 4708313SDina.Nimeh@Sun.Com iv_len = lsp->ls_iv_len; 4718313SDina.Nimeh@Sun.Com iv = kmem_zalloc(iv_len, KM_SLEEP); 4728313SDina.Nimeh@Sun.Com } else { 4738313SDina.Nimeh@Sun.Com iv_len = lsp->ls_mech.cm_param_len; 4748313SDina.Nimeh@Sun.Com iv = lsp->ls_mech.cm_param; 4758313SDina.Nimeh@Sun.Com bzero(iv, iv_len); 4768313SDina.Nimeh@Sun.Com } 4778313SDina.Nimeh@Sun.Com 4788313SDina.Nimeh@Sun.Com switch (lsp->ls_iv_type) { 4798313SDina.Nimeh@Sun.Com case IVM_ENC_BLKNO: 4808313SDina.Nimeh@Sun.Com /* iv is not static, lblkno changes each time */ 4818313SDina.Nimeh@Sun.Com data = &lblkno; 4828313SDina.Nimeh@Sun.Com datasz = sizeof (lblkno); 4838313SDina.Nimeh@Sun.Com break; 4848313SDina.Nimeh@Sun.Com default: 4858313SDina.Nimeh@Sun.Com data = 0; 4868313SDina.Nimeh@Sun.Com datasz = 0; 4878313SDina.Nimeh@Sun.Com break; 4888313SDina.Nimeh@Sun.Com } 4898313SDina.Nimeh@Sun.Com 4908313SDina.Nimeh@Sun.Com /* 4918313SDina.Nimeh@Sun.Com * write blkno into the iv buffer padded on the left in case 4928313SDina.Nimeh@Sun.Com * blkno ever grows bigger than its current longlong_t size 4938313SDina.Nimeh@Sun.Com * or a variation other than blkno is used for the iv data 4948313SDina.Nimeh@Sun.Com */ 4958313SDina.Nimeh@Sun.Com min = MIN(datasz, iv_len); 4968313SDina.Nimeh@Sun.Com bcopy(data, iv + (iv_len - min), min); 4978313SDina.Nimeh@Sun.Com 4988313SDina.Nimeh@Sun.Com /* encrypt the data in-place to get the IV */ 4998313SDina.Nimeh@Sun.Com SETUP_C_DATA(cdata, iv, iv_len); 5008313SDina.Nimeh@Sun.Com 5018313SDina.Nimeh@Sun.Com ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key, 5028313SDina.Nimeh@Sun.Com NULL, NULL, NULL); 5038313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS) { 5048313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)", 5058313SDina.Nimeh@Sun.Com lblkno, ret); 5068313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param != iv) 5078313SDina.Nimeh@Sun.Com kmem_free(iv, iv_len); 508*8996SAlok.Aggarwal@Sun.COM 5098313SDina.Nimeh@Sun.Com return (ret); 5108313SDina.Nimeh@Sun.Com } 5118313SDina.Nimeh@Sun.Com 5128313SDina.Nimeh@Sun.Com /* clean up the iv from the last computation */ 5138313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv) 5148313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len); 515*8996SAlok.Aggarwal@Sun.COM 5168313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len = iv_len; 5178313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param = iv; 5188313SDina.Nimeh@Sun.Com 5198313SDina.Nimeh@Sun.Com return (CRYPTO_SUCCESS); 5208313SDina.Nimeh@Sun.Com } 5218313SDina.Nimeh@Sun.Com 5228313SDina.Nimeh@Sun.Com /* 5238313SDina.Nimeh@Sun.Com * Performs encryption and decryption of a chunk of data of size "len", 5248313SDina.Nimeh@Sun.Com * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of 5258313SDina.Nimeh@Sun.Com * DEV_BSIZE. 5268313SDina.Nimeh@Sun.Com */ 5278313SDina.Nimeh@Sun.Com static int 5288313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext, 5298313SDina.Nimeh@Sun.Com caddr_t ciphertext, size_t len, boolean_t op_encrypt) 5308313SDina.Nimeh@Sun.Com { 5318313SDina.Nimeh@Sun.Com crypto_data_t cdata; 5328313SDina.Nimeh@Sun.Com crypto_data_t wdata; 5338313SDina.Nimeh@Sun.Com int ret; 5348313SDina.Nimeh@Sun.Com longlong_t lblkno = bp->b_lblkno; 5358313SDina.Nimeh@Sun.Com 5368313SDina.Nimeh@Sun.Com mutex_enter(&lsp->ls_crypto_lock); 5378313SDina.Nimeh@Sun.Com 5388313SDina.Nimeh@Sun.Com /* 5398313SDina.Nimeh@Sun.Com * though we could encrypt/decrypt entire "len" chunk of data, we need 5408313SDina.Nimeh@Sun.Com * to break it into DEV_BSIZE pieces to capture blkno incrementing 5418313SDina.Nimeh@Sun.Com */ 5428313SDina.Nimeh@Sun.Com SETUP_C_DATA(cdata, plaintext, len); 5438313SDina.Nimeh@Sun.Com cdata.cd_length = DEV_BSIZE; 5448313SDina.Nimeh@Sun.Com if (ciphertext != NULL) { /* not in-place crypto */ 5458313SDina.Nimeh@Sun.Com SETUP_C_DATA(wdata, ciphertext, len); 5468313SDina.Nimeh@Sun.Com wdata.cd_length = DEV_BSIZE; 5478313SDina.Nimeh@Sun.Com } 5488313SDina.Nimeh@Sun.Com 5498313SDina.Nimeh@Sun.Com do { 5508313SDina.Nimeh@Sun.Com ret = lofi_blk_mech(lsp, lblkno); 5518313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS) 5528313SDina.Nimeh@Sun.Com continue; 5538313SDina.Nimeh@Sun.Com 5548313SDina.Nimeh@Sun.Com if (op_encrypt) { 5558313SDina.Nimeh@Sun.Com ret = crypto_encrypt(&lsp->ls_mech, &cdata, 5568313SDina.Nimeh@Sun.Com &lsp->ls_key, NULL, 5578313SDina.Nimeh@Sun.Com ((ciphertext != NULL) ? &wdata : NULL), NULL); 5588313SDina.Nimeh@Sun.Com } else { 5598313SDina.Nimeh@Sun.Com ret = crypto_decrypt(&lsp->ls_mech, &cdata, 5608313SDina.Nimeh@Sun.Com &lsp->ls_key, NULL, 5618313SDina.Nimeh@Sun.Com ((ciphertext != NULL) ? &wdata : NULL), NULL); 5628313SDina.Nimeh@Sun.Com } 5638313SDina.Nimeh@Sun.Com 5648313SDina.Nimeh@Sun.Com cdata.cd_offset += DEV_BSIZE; 5658313SDina.Nimeh@Sun.Com if (ciphertext != NULL) 5668313SDina.Nimeh@Sun.Com wdata.cd_offset += DEV_BSIZE; 5678313SDina.Nimeh@Sun.Com lblkno++; 5688313SDina.Nimeh@Sun.Com } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len); 5698313SDina.Nimeh@Sun.Com 5708313SDina.Nimeh@Sun.Com mutex_exit(&lsp->ls_crypto_lock); 5718313SDina.Nimeh@Sun.Com 5728313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS) { 5738313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)", 5748313SDina.Nimeh@Sun.Com op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()", 5758313SDina.Nimeh@Sun.Com lblkno, ret); 5768313SDina.Nimeh@Sun.Com } 5778313SDina.Nimeh@Sun.Com 5788313SDina.Nimeh@Sun.Com return (ret); 5798313SDina.Nimeh@Sun.Com } 5808313SDina.Nimeh@Sun.Com 5818313SDina.Nimeh@Sun.Com #define RDWR_RAW 1 5828313SDina.Nimeh@Sun.Com #define RDWR_BCOPY 2 5838313SDina.Nimeh@Sun.Com 5848313SDina.Nimeh@Sun.Com static int 5858313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 5868313SDina.Nimeh@Sun.Com struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn) 5878313SDina.Nimeh@Sun.Com { 5888313SDina.Nimeh@Sun.Com ssize_t resid; 5898313SDina.Nimeh@Sun.Com int isread; 5908313SDina.Nimeh@Sun.Com int error; 5918313SDina.Nimeh@Sun.Com 5928313SDina.Nimeh@Sun.Com /* 5938313SDina.Nimeh@Sun.Com * Handles reads/writes for both plain and encrypted lofi 5948313SDina.Nimeh@Sun.Com * Note: offset is already shifted by lsp->ls_crypto_offset 5958313SDina.Nimeh@Sun.Com * when it gets here. 5968313SDina.Nimeh@Sun.Com */ 5978313SDina.Nimeh@Sun.Com 5988313SDina.Nimeh@Sun.Com isread = bp->b_flags & B_READ; 5998313SDina.Nimeh@Sun.Com if (isread) { 6008313SDina.Nimeh@Sun.Com if (method == RDWR_BCOPY) { 6018313SDina.Nimeh@Sun.Com /* DO NOT update bp->b_resid for bcopy */ 6028313SDina.Nimeh@Sun.Com bcopy(bcopy_locn, bufaddr, len); 6038313SDina.Nimeh@Sun.Com error = 0; 6048313SDina.Nimeh@Sun.Com } else { /* RDWR_RAW */ 6058313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len, 6068313SDina.Nimeh@Sun.Com offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 6078313SDina.Nimeh@Sun.Com &resid); 6088313SDina.Nimeh@Sun.Com bp->b_resid = resid; 6098313SDina.Nimeh@Sun.Com } 6108313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled && error == 0) { 6118313SDina.Nimeh@Sun.Com if (lofi_crypto(lsp, bp, bufaddr, NULL, len, 6128313SDina.Nimeh@Sun.Com B_FALSE) != CRYPTO_SUCCESS) { 6138313SDina.Nimeh@Sun.Com /* 6148313SDina.Nimeh@Sun.Com * XXX: original code didn't set residual 6158313SDina.Nimeh@Sun.Com * back to len because no error was expected 6168313SDina.Nimeh@Sun.Com * from bcopy() if encryption is not enabled 6178313SDina.Nimeh@Sun.Com */ 6188313SDina.Nimeh@Sun.Com if (method != RDWR_BCOPY) 6198313SDina.Nimeh@Sun.Com bp->b_resid = len; 6208313SDina.Nimeh@Sun.Com error = EIO; 6218313SDina.Nimeh@Sun.Com } 6228313SDina.Nimeh@Sun.Com } 6238313SDina.Nimeh@Sun.Com return (error); 6248313SDina.Nimeh@Sun.Com } else { 6258313SDina.Nimeh@Sun.Com void *iobuf = bufaddr; 6268313SDina.Nimeh@Sun.Com 6278313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) { 6288313SDina.Nimeh@Sun.Com /* don't do in-place crypto to keep bufaddr intact */ 6298313SDina.Nimeh@Sun.Com iobuf = kmem_alloc(len, KM_SLEEP); 6308313SDina.Nimeh@Sun.Com if (lofi_crypto(lsp, bp, bufaddr, iobuf, len, 6318313SDina.Nimeh@Sun.Com B_TRUE) != CRYPTO_SUCCESS) { 6328313SDina.Nimeh@Sun.Com kmem_free(iobuf, len); 6338313SDina.Nimeh@Sun.Com if (method != RDWR_BCOPY) 6348313SDina.Nimeh@Sun.Com bp->b_resid = len; 6358313SDina.Nimeh@Sun.Com return (EIO); 6368313SDina.Nimeh@Sun.Com } 6378313SDina.Nimeh@Sun.Com } 6388313SDina.Nimeh@Sun.Com if (method == RDWR_BCOPY) { 6398313SDina.Nimeh@Sun.Com /* DO NOT update bp->b_resid for bcopy */ 6408313SDina.Nimeh@Sun.Com bcopy(iobuf, bcopy_locn, len); 6418313SDina.Nimeh@Sun.Com error = 0; 6428313SDina.Nimeh@Sun.Com } else { /* RDWR_RAW */ 6438313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len, 6448313SDina.Nimeh@Sun.Com offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 6458313SDina.Nimeh@Sun.Com &resid); 6468313SDina.Nimeh@Sun.Com bp->b_resid = resid; 6478313SDina.Nimeh@Sun.Com } 6488313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) { 6498313SDina.Nimeh@Sun.Com kmem_free(iobuf, len); 6508313SDina.Nimeh@Sun.Com } 6518313SDina.Nimeh@Sun.Com return (error); 6528313SDina.Nimeh@Sun.Com } 6538313SDina.Nimeh@Sun.Com } 6548313SDina.Nimeh@Sun.Com 6555643Saalok static int 6565643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 6578313SDina.Nimeh@Sun.Com struct lofi_state *lsp) 6585643Saalok { 6595643Saalok int error; 6605643Saalok offset_t alignedoffset, mapoffset; 6615643Saalok size_t xfersize; 6625643Saalok int isread; 6638313SDina.Nimeh@Sun.Com int smflags; 6645643Saalok caddr_t mapaddr; 6655643Saalok size_t len; 6665643Saalok enum seg_rw srw; 6678313SDina.Nimeh@Sun.Com int save_error; 6688313SDina.Nimeh@Sun.Com 6698313SDina.Nimeh@Sun.Com /* 6708313SDina.Nimeh@Sun.Com * Note: offset is already shifted by lsp->ls_crypto_offset 6718313SDina.Nimeh@Sun.Com * when it gets here. 6728313SDina.Nimeh@Sun.Com */ 6738313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) 6748313SDina.Nimeh@Sun.Com ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size); 6755643Saalok 6765643Saalok /* 6775643Saalok * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 6785643Saalok * an 8K boundary, but the buf transfer address may not be 6795643Saalok * aligned on more than a 512-byte boundary (we don't enforce 6805643Saalok * that even though we could). This matters since the initial 6815643Saalok * part of the transfer may not start at offset 0 within the 6825643Saalok * segmap'd chunk. So we have to compensate for that with 6835643Saalok * 'mapoffset'. Subsequent chunks always start off at the 6845643Saalok * beginning, and the last is capped by b_resid 6858313SDina.Nimeh@Sun.Com * 6868313SDina.Nimeh@Sun.Com * Visually, where "|" represents page map boundaries: 6878313SDina.Nimeh@Sun.Com * alignedoffset (mapaddr begins at this segmap boundary) 6888313SDina.Nimeh@Sun.Com * | offset (from beginning of file) 6898313SDina.Nimeh@Sun.Com * | | len 6908313SDina.Nimeh@Sun.Com * v v v 6918313SDina.Nimeh@Sun.Com * ===|====X========|====...======|========X====|==== 6928313SDina.Nimeh@Sun.Com * /-------------...---------------/ 6938313SDina.Nimeh@Sun.Com * ^ bp->b_bcount/bp->b_resid at start 6948313SDina.Nimeh@Sun.Com * /----/--------/----...------/--------/ 6958313SDina.Nimeh@Sun.Com * ^ ^ ^ ^ ^ 6968313SDina.Nimeh@Sun.Com * | | | | nth xfersize (<= MAXBSIZE) 6978313SDina.Nimeh@Sun.Com * | | 2nd thru n-1st xfersize (= MAXBSIZE) 6988313SDina.Nimeh@Sun.Com * | 1st xfersize (<= MAXBSIZE) 6998313SDina.Nimeh@Sun.Com * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter) 7008313SDina.Nimeh@Sun.Com * 7018313SDina.Nimeh@Sun.Com * Notes: "alignedoffset" is "offset" rounded down to nearest 7028313SDina.Nimeh@Sun.Com * MAXBSIZE boundary. "len" is next page boundary of size 7038719SDina.Nimeh@Sun.COM * PAGESIZE after "alignedoffset". 7045643Saalok */ 7055643Saalok mapoffset = offset & MAXBOFFSET; 7065643Saalok alignedoffset = offset - mapoffset; 7075643Saalok bp->b_resid = bp->b_bcount; 7085643Saalok isread = bp->b_flags & B_READ; 7095643Saalok srw = isread ? S_READ : S_WRITE; 7105643Saalok do { 7115643Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset, 7125643Saalok MIN(MAXBSIZE - mapoffset, bp->b_resid)); 7138719SDina.Nimeh@Sun.COM len = roundup(mapoffset + xfersize, PAGESIZE); 7145643Saalok mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 7155643Saalok alignedoffset, MAXBSIZE, 1, srw); 7165643Saalok /* 7175643Saalok * Now fault in the pages. This lets us check 7185643Saalok * for errors before we reference mapaddr and 7195643Saalok * try to resolve the fault in bcopy (which would 7205643Saalok * panic instead). And this can easily happen, 7215643Saalok * particularly if you've lofi'd a file over NFS 7225643Saalok * and someone deletes the file on the server. 7235643Saalok */ 7245643Saalok error = segmap_fault(kas.a_hat, segkmap, mapaddr, 7255643Saalok len, F_SOFTLOCK, srw); 7265643Saalok if (error) { 7275643Saalok (void) segmap_release(segkmap, mapaddr, 0); 7285643Saalok if (FC_CODE(error) == FC_OBJERR) 7295643Saalok error = FC_ERRNO(error); 7305643Saalok else 7315643Saalok error = EIO; 7325643Saalok break; 7335643Saalok } 7348313SDina.Nimeh@Sun.Com /* error may be non-zero for encrypted lofi */ 7358313SDina.Nimeh@Sun.Com error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize, 7368313SDina.Nimeh@Sun.Com RDWR_BCOPY, mapaddr + mapoffset); 7378313SDina.Nimeh@Sun.Com if (error == 0) { 7388313SDina.Nimeh@Sun.Com bp->b_resid -= xfersize; 7398313SDina.Nimeh@Sun.Com bufaddr += xfersize; 7408313SDina.Nimeh@Sun.Com offset += xfersize; 7418313SDina.Nimeh@Sun.Com } 7425643Saalok smflags = 0; 7435643Saalok if (isread) { 7445643Saalok smflags |= SM_FREE; 7455643Saalok /* 7465643Saalok * If we're reading an entire page starting 7475643Saalok * at a page boundary, there's a good chance 7485643Saalok * we won't need it again. Put it on the 7495643Saalok * head of the freelist. 7505643Saalok */ 7518056SDina.Nimeh@Sun.Com if (mapoffset == 0 && xfersize == MAXBSIZE) 7525643Saalok smflags |= SM_DONTNEED; 7535643Saalok } else { 7548313SDina.Nimeh@Sun.Com if (error == 0) /* write back good pages */ 7558313SDina.Nimeh@Sun.Com smflags |= SM_WRITE; 7565643Saalok } 7575643Saalok (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 7585643Saalok len, F_SOFTUNLOCK, srw); 7598313SDina.Nimeh@Sun.Com save_error = segmap_release(segkmap, mapaddr, smflags); 7608313SDina.Nimeh@Sun.Com if (error == 0) 7618313SDina.Nimeh@Sun.Com error = save_error; 7625643Saalok /* only the first map may start partial */ 7635643Saalok mapoffset = 0; 7645643Saalok alignedoffset += MAXBSIZE; 7655643Saalok } while ((error == 0) && (bp->b_resid > 0) && 7665643Saalok (offset < lsp->ls_vp_comp_size)); 7675643Saalok 7685643Saalok return (error); 7695643Saalok } 7705643Saalok 7715643Saalok /*ARGSUSED*/ 772*8996SAlok.Aggarwal@Sun.COM static int 773*8996SAlok.Aggarwal@Sun.COM gzip_decompress(void *src, size_t srclen, void *dst, 7745643Saalok size_t *dstlen, int level) 7755643Saalok { 7765643Saalok ASSERT(*dstlen >= srclen); 7775643Saalok 7785643Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 7795643Saalok return (-1); 7805643Saalok return (0); 7815643Saalok } 7825643Saalok 783*8996SAlok.Aggarwal@Sun.COM #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8) 784*8996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/ 785*8996SAlok.Aggarwal@Sun.COM static int 786*8996SAlok.Aggarwal@Sun.COM lzma_decompress(void *src, size_t srclen, void *dst, 787*8996SAlok.Aggarwal@Sun.COM size_t *dstlen, int level) 788*8996SAlok.Aggarwal@Sun.COM { 789*8996SAlok.Aggarwal@Sun.COM size_t insizepure; 790*8996SAlok.Aggarwal@Sun.COM void *actual_src; 791*8996SAlok.Aggarwal@Sun.COM ELzmaStatus status; 792*8996SAlok.Aggarwal@Sun.COM 793*8996SAlok.Aggarwal@Sun.COM insizepure = srclen - LZMA_HEADER_SIZE; 794*8996SAlok.Aggarwal@Sun.COM actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE); 795*8996SAlok.Aggarwal@Sun.COM 796*8996SAlok.Aggarwal@Sun.COM if (LzmaDecode((Byte *)dst, (size_t *)dstlen, 797*8996SAlok.Aggarwal@Sun.COM (const Byte *)actual_src, &insizepure, 798*8996SAlok.Aggarwal@Sun.COM (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, 799*8996SAlok.Aggarwal@Sun.COM &g_Alloc) != SZ_OK) { 800*8996SAlok.Aggarwal@Sun.COM return (-1); 801*8996SAlok.Aggarwal@Sun.COM } 802*8996SAlok.Aggarwal@Sun.COM return (0); 803*8996SAlok.Aggarwal@Sun.COM } 804*8996SAlok.Aggarwal@Sun.COM 8050Sstevel@tonic-gate /* 8060Sstevel@tonic-gate * This is basically what strategy used to be before we found we 8070Sstevel@tonic-gate * needed task queues. 8080Sstevel@tonic-gate */ 8090Sstevel@tonic-gate static void 8100Sstevel@tonic-gate lofi_strategy_task(void *arg) 8110Sstevel@tonic-gate { 8120Sstevel@tonic-gate struct buf *bp = (struct buf *)arg; 8130Sstevel@tonic-gate int error; 8140Sstevel@tonic-gate struct lofi_state *lsp; 8158313SDina.Nimeh@Sun.Com offset_t offset; 8168313SDina.Nimeh@Sun.Com caddr_t bufaddr; 8178313SDina.Nimeh@Sun.Com size_t len; 8188313SDina.Nimeh@Sun.Com size_t xfersize; 8198313SDina.Nimeh@Sun.Com boolean_t bufinited = B_FALSE; 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 8228313SDina.Nimeh@Sun.Com if (lsp == NULL) { 8238313SDina.Nimeh@Sun.Com error = ENXIO; 8248313SDina.Nimeh@Sun.Com goto errout; 8258313SDina.Nimeh@Sun.Com } 8260Sstevel@tonic-gate if (lsp->ls_kstat) { 8270Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 8280Sstevel@tonic-gate kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 8290Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate bp_mapin(bp); 8320Sstevel@tonic-gate bufaddr = bp->b_un.b_addr; 8330Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 8348313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) { 8358313SDina.Nimeh@Sun.Com /* encrypted data really begins after crypto header */ 8368313SDina.Nimeh@Sun.Com offset += lsp->ls_crypto_offset; 8378313SDina.Nimeh@Sun.Com } 8388313SDina.Nimeh@Sun.Com len = bp->b_bcount; 8398313SDina.Nimeh@Sun.Com bufinited = B_TRUE; 8408313SDina.Nimeh@Sun.Com 8418313SDina.Nimeh@Sun.Com if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 8428313SDina.Nimeh@Sun.Com error = EIO; 8438313SDina.Nimeh@Sun.Com goto errout; 8448313SDina.Nimeh@Sun.Com } 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate /* 8470Sstevel@tonic-gate * We used to always use vn_rdwr here, but we cannot do that because 8480Sstevel@tonic-gate * we might decide to read or write from the the underlying 8490Sstevel@tonic-gate * file during this call, which would be a deadlock because 8500Sstevel@tonic-gate * we have the rw_lock. So instead we page, unless it's not 8518313SDina.Nimeh@Sun.Com * mapable or it's a character device or it's an encrypted lofi. 8520Sstevel@tonic-gate */ 8538313SDina.Nimeh@Sun.Com if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) || 8548313SDina.Nimeh@Sun.Com lsp->ls_crypto_enabled) { 8558313SDina.Nimeh@Sun.Com error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW, 8568313SDina.Nimeh@Sun.Com NULL); 8578313SDina.Nimeh@Sun.Com } else if (lsp->ls_uncomp_seg_sz == 0) { 8588313SDina.Nimeh@Sun.Com error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 8598313SDina.Nimeh@Sun.Com } else { 8608313SDina.Nimeh@Sun.Com unsigned char *compressed_seg = NULL, *cmpbuf; 8618313SDina.Nimeh@Sun.Com unsigned char *uncompressed_seg = NULL; 8628313SDina.Nimeh@Sun.Com lofi_compress_info_t *li; 8638313SDina.Nimeh@Sun.Com size_t oblkcount; 8648313SDina.Nimeh@Sun.Com unsigned long seglen; 8658313SDina.Nimeh@Sun.Com uint64_t sblkno, eblkno, cmpbytes; 8668313SDina.Nimeh@Sun.Com offset_t sblkoff, eblkoff; 8678313SDina.Nimeh@Sun.Com u_offset_t salign, ealign; 8688313SDina.Nimeh@Sun.Com u_offset_t sdiff; 8698313SDina.Nimeh@Sun.Com uint32_t comp_data_sz; 8705643Saalok uint64_t i; 8715643Saalok 8720Sstevel@tonic-gate /* 8735643Saalok * From here on we're dealing primarily with compressed files 8745643Saalok */ 8758313SDina.Nimeh@Sun.Com ASSERT(!lsp->ls_crypto_enabled); 8765643Saalok 8775643Saalok /* 8785643Saalok * Compressed files can only be read from and 8795643Saalok * not written to 8800Sstevel@tonic-gate */ 8815643Saalok if (!(bp->b_flags & B_READ)) { 8825643Saalok bp->b_resid = bp->b_bcount; 8835643Saalok error = EROFS; 8845643Saalok goto done; 8855643Saalok } 8865643Saalok 8875643Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0); 8885643Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 8895643Saalok /* 8905643Saalok * Compute starting and ending compressed segment numbers 8915643Saalok * We use only bitwise operations avoiding division and 8925643Saalok * modulus because we enforce the compression segment size 8935643Saalok * to a power of 2 8945643Saalok */ 8955643Saalok sblkno = offset >> lsp->ls_comp_seg_shift; 8965643Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 8975643Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 8985643Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 8995643Saalok 9005643Saalok /* 9015643Saalok * Align start offset to block boundary for segmap 9025643Saalok */ 9035643Saalok salign = lsp->ls_comp_seg_index[sblkno]; 9045643Saalok sdiff = salign & (DEV_BSIZE - 1); 9055643Saalok salign -= sdiff; 9065643Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 9070Sstevel@tonic-gate /* 9085643Saalok * We're dealing with the last segment of 9095643Saalok * the compressed file -- the size of this 9105643Saalok * segment *may not* be the same as the 9115643Saalok * segment size for the file 9120Sstevel@tonic-gate */ 9135643Saalok eblkoff = (offset + bp->b_bcount) & 9145643Saalok (lsp->ls_uncomp_last_seg_sz - 1); 9155643Saalok ealign = lsp->ls_vp_comp_size; 9165643Saalok } else { 9175643Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1]; 9185643Saalok } 9195643Saalok 9205643Saalok /* 9215643Saalok * Preserve original request paramaters 9225643Saalok */ 9235643Saalok oblkcount = bp->b_bcount; 9245643Saalok 9255643Saalok /* 9265643Saalok * Assign the calculated parameters 9275643Saalok */ 9285643Saalok comp_data_sz = ealign - salign; 9295643Saalok bp->b_bcount = comp_data_sz; 9305643Saalok 9315643Saalok /* 9325643Saalok * Allocate fixed size memory blocks to hold compressed 9335643Saalok * segments and one uncompressed segment since we 9345643Saalok * uncompress segments one at a time 9355643Saalok */ 9365643Saalok compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP); 9375643Saalok uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP); 9385643Saalok /* 9395643Saalok * Map in the calculated number of blocks 9405643Saalok */ 9415643Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 9425643Saalok bp, lsp); 9435643Saalok 9445643Saalok bp->b_bcount = oblkcount; 9455643Saalok bp->b_resid = oblkcount; 9465643Saalok if (error != 0) 9475643Saalok goto done; 9485643Saalok 9495643Saalok /* 9505643Saalok * We have the compressed blocks, now uncompress them 9515643Saalok */ 9525643Saalok cmpbuf = compressed_seg + sdiff; 953*8996SAlok.Aggarwal@Sun.COM for (i = sblkno; i <= eblkno; i++) { 954*8996SAlok.Aggarwal@Sun.COM ASSERT(i < lsp->ls_comp_index_sz - 1); 955*8996SAlok.Aggarwal@Sun.COM 956*8996SAlok.Aggarwal@Sun.COM /* 957*8996SAlok.Aggarwal@Sun.COM * The last segment is special in that it is 958*8996SAlok.Aggarwal@Sun.COM * most likely not going to be the same 959*8996SAlok.Aggarwal@Sun.COM * (uncompressed) size as the other segments. 960*8996SAlok.Aggarwal@Sun.COM */ 961*8996SAlok.Aggarwal@Sun.COM if (i == (lsp->ls_comp_index_sz - 2)) { 962*8996SAlok.Aggarwal@Sun.COM seglen = lsp->ls_uncomp_last_seg_sz; 963*8996SAlok.Aggarwal@Sun.COM } else { 964*8996SAlok.Aggarwal@Sun.COM seglen = lsp->ls_uncomp_seg_sz; 965*8996SAlok.Aggarwal@Sun.COM } 966*8996SAlok.Aggarwal@Sun.COM 9675643Saalok /* 9685643Saalok * Each of the segment index entries contains 9695643Saalok * the starting block number for that segment. 9705643Saalok * The number of compressed bytes in a segment 9715643Saalok * is thus the difference between the starting 9725643Saalok * block number of this segment and the starting 9735643Saalok * block number of the next segment. 9745643Saalok */ 975*8996SAlok.Aggarwal@Sun.COM cmpbytes = lsp->ls_comp_seg_index[i + 1] - 976*8996SAlok.Aggarwal@Sun.COM lsp->ls_comp_seg_index[i]; 9775643Saalok 9785643Saalok /* 9795643Saalok * The first byte in a compressed segment is a flag 9805643Saalok * that indicates whether this segment is compressed 9815643Saalok * at all 9825643Saalok */ 9835643Saalok if (*cmpbuf == UNCOMPRESSED) { 9845643Saalok bcopy((cmpbuf + SEGHDR), uncompressed_seg, 9855643Saalok (cmpbytes - SEGHDR)); 9865643Saalok } else { 9875643Saalok if (li->l_decompress((cmpbuf + SEGHDR), 9885643Saalok (cmpbytes - SEGHDR), uncompressed_seg, 9895643Saalok &seglen, li->l_level) != 0) { 9905643Saalok error = EIO; 9915643Saalok goto done; 9925643Saalok } 9935643Saalok } 9945643Saalok 9955643Saalok /* 9965643Saalok * Determine how much uncompressed data we 9975643Saalok * have to copy and copy it 9985643Saalok */ 9995643Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 1000*8996SAlok.Aggarwal@Sun.COM if (i == eblkno) 1001*8996SAlok.Aggarwal@Sun.COM xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff); 10025643Saalok 10035643Saalok bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize); 10045643Saalok 10055643Saalok cmpbuf += cmpbytes; 10060Sstevel@tonic-gate bufaddr += xfersize; 10075643Saalok bp->b_resid -= xfersize; 10085643Saalok sblkoff = 0; 10095643Saalok 10105643Saalok if (bp->b_resid == 0) 10115643Saalok break; 10125643Saalok } 10138313SDina.Nimeh@Sun.Com done: 10148313SDina.Nimeh@Sun.Com if (compressed_seg != NULL) 10158313SDina.Nimeh@Sun.Com kmem_free(compressed_seg, comp_data_sz); 10168313SDina.Nimeh@Sun.Com if (uncompressed_seg != NULL) 10178313SDina.Nimeh@Sun.Com kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 10188313SDina.Nimeh@Sun.Com } /* end of handling compressed files */ 10190Sstevel@tonic-gate 10208313SDina.Nimeh@Sun.Com errout: 10218313SDina.Nimeh@Sun.Com if (bufinited && lsp->ls_kstat) { 10220Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid; 10230Sstevel@tonic-gate kstat_io_t *kioptr; 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 10260Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 10270Sstevel@tonic-gate if (bp->b_flags & B_READ) { 10280Sstevel@tonic-gate kioptr->nread += n_done; 10290Sstevel@tonic-gate kioptr->reads++; 10300Sstevel@tonic-gate } else { 10310Sstevel@tonic-gate kioptr->nwritten += n_done; 10320Sstevel@tonic-gate kioptr->writes++; 10330Sstevel@tonic-gate } 10340Sstevel@tonic-gate kstat_runq_exit(kioptr); 10350Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 10360Sstevel@tonic-gate } 10374451Seschrock 10384451Seschrock mutex_enter(&lsp->ls_vp_lock); 10394451Seschrock if (--lsp->ls_vp_iocount == 0) 10404451Seschrock cv_broadcast(&lsp->ls_vp_cv); 10414451Seschrock mutex_exit(&lsp->ls_vp_lock); 10424451Seschrock 10430Sstevel@tonic-gate bioerror(bp, error); 10440Sstevel@tonic-gate biodone(bp); 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate static int 10480Sstevel@tonic-gate lofi_strategy(struct buf *bp) 10490Sstevel@tonic-gate { 10500Sstevel@tonic-gate struct lofi_state *lsp; 10510Sstevel@tonic-gate offset_t offset; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate /* 10540Sstevel@tonic-gate * We cannot just do I/O here, because the current thread 10550Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem 10560Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and 10570Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer. 10580Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below. 10590Sstevel@tonic-gate * 10600Sstevel@tonic-gate * We could come up with a complex strategy using threads to 10610Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task 10620Sstevel@tonic-gate * queues were incredibly easy so they win. 10630Sstevel@tonic-gate */ 10640Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 10658313SDina.Nimeh@Sun.Com if (lsp == NULL) { 10668313SDina.Nimeh@Sun.Com bioerror(bp, ENXIO); 10678313SDina.Nimeh@Sun.Com biodone(bp); 10688313SDina.Nimeh@Sun.Com return (0); 10698313SDina.Nimeh@Sun.Com } 10708313SDina.Nimeh@Sun.Com 10714451Seschrock mutex_enter(&lsp->ls_vp_lock); 10724451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 10734451Seschrock bioerror(bp, EIO); 10744451Seschrock biodone(bp); 10754451Seschrock mutex_exit(&lsp->ls_vp_lock); 10764451Seschrock return (0); 10774451Seschrock } 10784451Seschrock 10790Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 10808313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) { 10818313SDina.Nimeh@Sun.Com /* encrypted data really begins after crypto header */ 10828313SDina.Nimeh@Sun.Com offset += lsp->ls_crypto_offset; 10838313SDina.Nimeh@Sun.Com } 10840Sstevel@tonic-gate if (offset == lsp->ls_vp_size) { 10850Sstevel@tonic-gate /* EOF */ 10860Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) { 10870Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 10880Sstevel@tonic-gate bioerror(bp, 0); 10890Sstevel@tonic-gate } else { 10900Sstevel@tonic-gate /* writes should fail */ 10910Sstevel@tonic-gate bioerror(bp, ENXIO); 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate biodone(bp); 10944451Seschrock mutex_exit(&lsp->ls_vp_lock); 10950Sstevel@tonic-gate return (0); 10960Sstevel@tonic-gate } 10970Sstevel@tonic-gate if (offset > lsp->ls_vp_size) { 10980Sstevel@tonic-gate bioerror(bp, ENXIO); 10990Sstevel@tonic-gate biodone(bp); 11004451Seschrock mutex_exit(&lsp->ls_vp_lock); 11010Sstevel@tonic-gate return (0); 11020Sstevel@tonic-gate } 11034451Seschrock lsp->ls_vp_iocount++; 11044451Seschrock mutex_exit(&lsp->ls_vp_lock); 11054451Seschrock 11060Sstevel@tonic-gate if (lsp->ls_kstat) { 11070Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 11080Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 11090Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 11120Sstevel@tonic-gate return (0); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /*ARGSUSED2*/ 11160Sstevel@tonic-gate static int 11170Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 11180Sstevel@tonic-gate { 11190Sstevel@tonic-gate if (getminor(dev) == 0) 11200Sstevel@tonic-gate return (EINVAL); 11218313SDina.Nimeh@Sun.Com UIO_CHECK(uio); 11220Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate /*ARGSUSED2*/ 11260Sstevel@tonic-gate static int 11270Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 11280Sstevel@tonic-gate { 11290Sstevel@tonic-gate if (getminor(dev) == 0) 11300Sstevel@tonic-gate return (EINVAL); 11318313SDina.Nimeh@Sun.Com UIO_CHECK(uio); 11320Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate /*ARGSUSED2*/ 11360Sstevel@tonic-gate static int 11370Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 11380Sstevel@tonic-gate { 11390Sstevel@tonic-gate if (getminor(dev) == 0) 11400Sstevel@tonic-gate return (EINVAL); 11418313SDina.Nimeh@Sun.Com UIO_CHECK(aio->aio_uio); 11420Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /*ARGSUSED2*/ 11460Sstevel@tonic-gate static int 11470Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 11480Sstevel@tonic-gate { 11490Sstevel@tonic-gate if (getminor(dev) == 0) 11500Sstevel@tonic-gate return (EINVAL); 11518313SDina.Nimeh@Sun.Com UIO_CHECK(aio->aio_uio); 11520Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 11530Sstevel@tonic-gate } 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate /*ARGSUSED*/ 11560Sstevel@tonic-gate static int 11570Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 11580Sstevel@tonic-gate { 11590Sstevel@tonic-gate switch (infocmd) { 11600Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 11610Sstevel@tonic-gate *result = lofi_dip; 11620Sstevel@tonic-gate return (DDI_SUCCESS); 11630Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 11640Sstevel@tonic-gate *result = 0; 11650Sstevel@tonic-gate return (DDI_SUCCESS); 11660Sstevel@tonic-gate } 11670Sstevel@tonic-gate return (DDI_FAILURE); 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate static int 11710Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 11720Sstevel@tonic-gate { 11730Sstevel@tonic-gate int error; 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate if (cmd != DDI_ATTACH) 11760Sstevel@tonic-gate return (DDI_FAILURE); 11770Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0); 11780Sstevel@tonic-gate if (error == DDI_FAILURE) { 11790Sstevel@tonic-gate return (DDI_FAILURE); 11800Sstevel@tonic-gate } 11810Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 11820Sstevel@tonic-gate DDI_PSEUDO, NULL); 11830Sstevel@tonic-gate if (error == DDI_FAILURE) { 11840Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 11850Sstevel@tonic-gate return (DDI_FAILURE); 11860Sstevel@tonic-gate } 11875084Sjohnlev /* driver handles kernel-issued IOCTLs */ 11885084Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 11895084Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 11905084Sjohnlev ddi_remove_minor_node(dip, NULL); 11915084Sjohnlev ddi_soft_state_free(lofi_statep, 0); 11925084Sjohnlev return (DDI_FAILURE); 11935084Sjohnlev } 11940Sstevel@tonic-gate lofi_dip = dip; 11950Sstevel@tonic-gate ddi_report_dev(dip); 11960Sstevel@tonic-gate return (DDI_SUCCESS); 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate static int 12000Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 12010Sstevel@tonic-gate { 12020Sstevel@tonic-gate if (cmd != DDI_DETACH) 12030Sstevel@tonic-gate return (DDI_FAILURE); 12040Sstevel@tonic-gate if (lofi_busy()) 12050Sstevel@tonic-gate return (DDI_FAILURE); 12060Sstevel@tonic-gate lofi_dip = NULL; 12070Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 12085084Sjohnlev ddi_prop_remove_all(dip); 12090Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 12100Sstevel@tonic-gate return (DDI_SUCCESS); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate /* 12148313SDina.Nimeh@Sun.Com * With addition of encryption, be careful that encryption key is wiped before 12158313SDina.Nimeh@Sun.Com * kernel memory structures are freed, and also that key is not accidentally 12168313SDina.Nimeh@Sun.Com * passed out into userland structures. 12178313SDina.Nimeh@Sun.Com */ 12188313SDina.Nimeh@Sun.Com static void 12198313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip) 12208313SDina.Nimeh@Sun.Com { 12218313SDina.Nimeh@Sun.Com /* Make sure this encryption key doesn't stick around */ 12228313SDina.Nimeh@Sun.Com bzero(klip->li_key, sizeof (klip->li_key)); 12238313SDina.Nimeh@Sun.Com kmem_free(klip, sizeof (struct lofi_ioctl)); 12248313SDina.Nimeh@Sun.Com } 12258313SDina.Nimeh@Sun.Com 12268313SDina.Nimeh@Sun.Com /* 12270Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out 12280Sstevel@tonic-gate * the lofi_ioctl structure. 12290Sstevel@tonic-gate */ 12300Sstevel@tonic-gate struct lofi_ioctl * 12311657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag) 12320Sstevel@tonic-gate { 12330Sstevel@tonic-gate struct lofi_ioctl *klip; 12340Sstevel@tonic-gate int error; 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 12371657Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 12380Sstevel@tonic-gate if (error) { 12398313SDina.Nimeh@Sun.Com free_lofi_ioctl(klip); 12400Sstevel@tonic-gate return (NULL); 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate /* make sure filename is always null-terminated */ 12448313SDina.Nimeh@Sun.Com klip->li_filename[MAXPATHLEN-1] = '\0'; 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate /* validate minor number */ 12470Sstevel@tonic-gate if (klip->li_minor > lofi_max_files) { 12488313SDina.Nimeh@Sun.Com free_lofi_ioctl(klip); 12498313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)", 12508313SDina.Nimeh@Sun.Com lofi_max_files); 12510Sstevel@tonic-gate return (NULL); 12520Sstevel@tonic-gate } 12530Sstevel@tonic-gate return (klip); 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate int 12571657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 12581657Sheppo int flag) 12590Sstevel@tonic-gate { 12600Sstevel@tonic-gate int error; 12610Sstevel@tonic-gate 12628313SDina.Nimeh@Sun.Com /* 12638313SDina.Nimeh@Sun.Com * NOTE: Do NOT copy the crypto_key_t "back" to userland. 12648313SDina.Nimeh@Sun.Com * This ensures that an attacker can't trivially find the 12658313SDina.Nimeh@Sun.Com * key for a mapping just by issuing the ioctl. 12668313SDina.Nimeh@Sun.Com * 12678313SDina.Nimeh@Sun.Com * It can still be found by poking around in kmem with mdb(1), 12688313SDina.Nimeh@Sun.Com * but there is no point in making it easy when the info isn't 12698313SDina.Nimeh@Sun.Com * of any use in this direction anyway. 12708313SDina.Nimeh@Sun.Com * 12718313SDina.Nimeh@Sun.Com * Either way we don't actually have the raw key stored in 12728313SDina.Nimeh@Sun.Com * a form that we can get it anyway, since we just used it 12738313SDina.Nimeh@Sun.Com * to create a ctx template and didn't keep "the original". 12748313SDina.Nimeh@Sun.Com */ 12751657Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 12760Sstevel@tonic-gate if (error) 12770Sstevel@tonic-gate return (EFAULT); 12780Sstevel@tonic-gate return (0); 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate /* 12820Sstevel@tonic-gate * Return the minor number 'filename' is mapped to, if it is. 12830Sstevel@tonic-gate */ 12840Sstevel@tonic-gate static int 12850Sstevel@tonic-gate file_to_minor(char *filename) 12860Sstevel@tonic-gate { 12870Sstevel@tonic-gate minor_t minor; 12880Sstevel@tonic-gate struct lofi_state *lsp; 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 12910Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 12920Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 12930Sstevel@tonic-gate if (lsp == NULL) 12940Sstevel@tonic-gate continue; 12950Sstevel@tonic-gate if (strcmp(lsp->ls_filename, filename) == 0) 12960Sstevel@tonic-gate return (minor); 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate return (0); 12990Sstevel@tonic-gate } 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate /* 13020Sstevel@tonic-gate * lofiadm does some validation, but since Joe Random (or crashme) could 13030Sstevel@tonic-gate * do our ioctls, we need to do some validation too. 13040Sstevel@tonic-gate */ 13050Sstevel@tonic-gate static int 13060Sstevel@tonic-gate valid_filename(const char *filename) 13070Sstevel@tonic-gate { 13080Sstevel@tonic-gate static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/"; 13090Sstevel@tonic-gate static char *charprefix = "/dev/" LOFI_CHAR_NAME "/"; 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate /* must be absolute path */ 13120Sstevel@tonic-gate if (filename[0] != '/') 13130Sstevel@tonic-gate return (0); 13140Sstevel@tonic-gate /* must not be lofi */ 13150Sstevel@tonic-gate if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0) 13160Sstevel@tonic-gate return (0); 13170Sstevel@tonic-gate if (strncmp(filename, charprefix, strlen(charprefix)) == 0) 13180Sstevel@tonic-gate return (0); 13190Sstevel@tonic-gate return (1); 13200Sstevel@tonic-gate } 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate /* 13230Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size 13240Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device, 13250Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and 13260Sstevel@tonic-gate * partition information. It uses that information to determine the parameters 13273517Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 13280Sstevel@tonic-gate * have to support it. 13290Sstevel@tonic-gate */ 13300Sstevel@tonic-gate static void 13310Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp) 13320Sstevel@tonic-gate { 13338313SDina.Nimeh@Sun.Com u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset; 13348313SDina.Nimeh@Sun.Com 13350Sstevel@tonic-gate /* dk_geom - see dkio(7I) */ 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 13380Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to 13390Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create 13400Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick 13410Sstevel@tonic-gate * a number between one and one). 13420Sstevel@tonic-gate * 13430Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders 13440Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space 13450Sstevel@tonic-gate * for backup superblocks or cylinder group structures. 13460Sstevel@tonic-gate */ 13478313SDina.Nimeh@Sun.Com if (dsize < (2 * 1024 * 1024)) /* floppy? */ 13488313SDina.Nimeh@Sun.Com lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024); 13490Sstevel@tonic-gate else 13508313SDina.Nimeh@Sun.Com lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024); 13510Sstevel@tonic-gate /* in case file file is < 100k */ 13520Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0) 13530Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1; 13540Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0; 13550Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0; 13560Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1; 13570Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0; 13580Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0; 13590Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0; 13600Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0; 13610Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0; 13620Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200; 13630Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 13648313SDina.Nimeh@Sun.Com lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 13650Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0; 13660Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0; 13670Sstevel@tonic-gate 13680Sstevel@tonic-gate /* vtoc - see dkio(7I) */ 13690Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 13700Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE; 13710Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION; 13728669SDina.Nimeh@Sun.COM (void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME, 13738669SDina.Nimeh@Sun.COM sizeof (lsp->ls_vtoc.v_volume)); 13740Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 13750Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1; 13760Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 13775643Saalok 13785643Saalok /* 13795643Saalok * A compressed file is read-only, other files can 13805643Saalok * be read-write 13815643Saalok */ 13825643Saalok if (lsp->ls_uncomp_seg_sz > 0) { 13835643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 13845643Saalok } else { 13855643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 13865643Saalok } 13870Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 13880Sstevel@tonic-gate /* 13890Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because 13900Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case, 13910Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size 13920Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder. 13930Sstevel@tonic-gate */ 13940Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 13950Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */ 13980Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 13990Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 14000Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD; 14010Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0; 14020Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0; 14030Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0; 14040Sstevel@tonic-gate lsp->ls_ci.dki_space = 0; 14050Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0; 14060Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0; 14070Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 14080Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0; 14090Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0; 14100Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0; 14110Sstevel@tonic-gate /* 14120Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it 14130Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides 14140Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because 14150Sstevel@tonic-gate * maxcontig is 0. 14160Sstevel@tonic-gate */ 14170Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16; 14180Sstevel@tonic-gate } 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate /* 14215643Saalok * map in a compressed file 14225643Saalok * 14235643Saalok * Read in the header and the index that follows. 14245643Saalok * 14255643Saalok * The header is as follows - 14265643Saalok * 14275643Saalok * Signature (name of the compression algorithm) 14285643Saalok * Compression segment size (a multiple of 512) 14295643Saalok * Number of index entries 14305643Saalok * Size of the last block 14315643Saalok * The array containing the index entries 14325643Saalok * 14335643Saalok * The header information is always stored in 14345643Saalok * network byte order on disk. 14355643Saalok */ 14365643Saalok static int 14375643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 14385643Saalok { 14395643Saalok uint32_t index_sz, header_len, i; 14405643Saalok ssize_t resid; 14415643Saalok enum uio_rw rw; 14425643Saalok char *tbuf = buf; 14435643Saalok int error; 14445643Saalok 14455643Saalok /* The signature has already been read */ 14465643Saalok tbuf += sizeof (lsp->ls_comp_algorithm); 14475643Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 14485643Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 14495643Saalok 14505643Saalok /* 14515643Saalok * The compressed segment size must be a power of 2 14525643Saalok */ 14535643Saalok if (lsp->ls_uncomp_seg_sz % 2) 14545643Saalok return (EINVAL); 14555643Saalok 14565643Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 14575643Saalok ; 14585643Saalok 14595643Saalok lsp->ls_comp_seg_shift = i; 14605643Saalok 14615643Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz); 14625643Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 14635643Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 14645643Saalok 14655643Saalok tbuf += sizeof (lsp->ls_comp_index_sz); 14665643Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 14675643Saalok sizeof (lsp->ls_uncomp_last_seg_sz)); 14685643Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 14695643Saalok 14705643Saalok /* 14715643Saalok * Compute the total size of the uncompressed data 14725643Saalok * for use in fake_disk_geometry and other calculations. 14735643Saalok * Disk geometry has to be faked with respect to the 14745643Saalok * actual uncompressed data size rather than the 14755643Saalok * compressed file size. 14765643Saalok */ 14775643Saalok lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 14785643Saalok + lsp->ls_uncomp_last_seg_sz; 14795643Saalok 14805643Saalok /* 1481*8996SAlok.Aggarwal@Sun.COM * Index size is rounded up to DEV_BSIZE for ease 14825643Saalok * of segmapping 14835643Saalok */ 14845643Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 14855643Saalok header_len = sizeof (lsp->ls_comp_algorithm) + 14865643Saalok sizeof (lsp->ls_uncomp_seg_sz) + 14875643Saalok sizeof (lsp->ls_comp_index_sz) + 14885643Saalok sizeof (lsp->ls_uncomp_last_seg_sz); 14895643Saalok lsp->ls_comp_offbase = header_len + index_sz; 14905643Saalok 14915643Saalok index_sz += header_len; 14925643Saalok index_sz = roundup(index_sz, DEV_BSIZE); 14935643Saalok 14945643Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 14955643Saalok lsp->ls_comp_index_data_sz = index_sz; 14965643Saalok 14975643Saalok /* 14985643Saalok * Read in the index -- this has a side-effect 14995643Saalok * of reading in the header as well 15005643Saalok */ 15015643Saalok rw = UIO_READ; 15025643Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 15035643Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 15045643Saalok 15055643Saalok if (error != 0) 15065643Saalok return (error); 15075643Saalok 15085643Saalok /* Skip the header, this is where the index really begins */ 15095643Saalok lsp->ls_comp_seg_index = 15105643Saalok /*LINTED*/ 15115643Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len); 15125643Saalok 15135643Saalok /* 15145643Saalok * Now recompute offsets in the index to account for 15155643Saalok * the header length 15165643Saalok */ 15175643Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) { 15185643Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 15195643Saalok BE_64(lsp->ls_comp_seg_index[i]); 15205643Saalok } 15215643Saalok 15225643Saalok return (error); 15235643Saalok } 15245643Saalok 15255643Saalok /* 15265643Saalok * Check to see if the passed in signature is a valid 15278313SDina.Nimeh@Sun.Com * one. If it is valid, return the index into 15285643Saalok * lofi_compress_table. 15295643Saalok * 15305643Saalok * Return -1 if it is invalid 15315643Saalok */ 15325643Saalok static int lofi_compress_select(char *signature) 15335643Saalok { 15345643Saalok int i; 15355643Saalok 15365643Saalok for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 15375643Saalok if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 15385643Saalok return (i); 15395643Saalok } 15405643Saalok 15415643Saalok return (-1); 15425643Saalok } 15435643Saalok 15445643Saalok /* 15450Sstevel@tonic-gate * map a file to a minor number. Return the minor number. 15460Sstevel@tonic-gate */ 15470Sstevel@tonic-gate static int 15480Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 15491657Sheppo int *rvalp, struct cred *credp, int ioctl_flag) 15500Sstevel@tonic-gate { 15510Sstevel@tonic-gate minor_t newminor; 15520Sstevel@tonic-gate struct lofi_state *lsp; 15530Sstevel@tonic-gate struct lofi_ioctl *klip; 15540Sstevel@tonic-gate int error; 15550Sstevel@tonic-gate struct vnode *vp; 15560Sstevel@tonic-gate int64_t Nblocks_prop_val; 15570Sstevel@tonic-gate int64_t Size_prop_val; 15585643Saalok int compress_index; 15590Sstevel@tonic-gate vattr_t vattr; 15600Sstevel@tonic-gate int flag; 15610Sstevel@tonic-gate enum vtype v_type; 15624451Seschrock int zalloced = 0; 15630Sstevel@tonic-gate dev_t newdev; 15644451Seschrock char namebuf[50]; 15658313SDina.Nimeh@Sun.Com char buf[DEV_BSIZE]; 15668313SDina.Nimeh@Sun.Com char crybuf[DEV_BSIZE]; 15675643Saalok ssize_t resid; 15688313SDina.Nimeh@Sun.Com boolean_t need_vn_close = B_FALSE; 15698313SDina.Nimeh@Sun.Com boolean_t keycopied = B_FALSE; 15708313SDina.Nimeh@Sun.Com boolean_t need_size_update = B_FALSE; 15710Sstevel@tonic-gate 15721657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 15730Sstevel@tonic-gate if (klip == NULL) 15740Sstevel@tonic-gate return (EFAULT); 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate mutex_enter(&lofi_lock); 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate if (!valid_filename(klip->li_filename)) { 15790Sstevel@tonic-gate error = EINVAL; 15800Sstevel@tonic-gate goto out; 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate if (file_to_minor(klip->li_filename) != 0) { 15840Sstevel@tonic-gate error = EBUSY; 15850Sstevel@tonic-gate goto out; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate if (pickminor) { 15890Sstevel@tonic-gate /* Find a free one */ 15900Sstevel@tonic-gate for (newminor = 1; newminor <= lofi_max_files; newminor++) 15910Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) == NULL) 15920Sstevel@tonic-gate break; 15930Sstevel@tonic-gate if (newminor >= lofi_max_files) { 15940Sstevel@tonic-gate error = EAGAIN; 15950Sstevel@tonic-gate goto out; 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate } else { 15980Sstevel@tonic-gate newminor = klip->li_minor; 15990Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) != NULL) { 16000Sstevel@tonic-gate error = EEXIST; 16010Sstevel@tonic-gate goto out; 16020Sstevel@tonic-gate } 16030Sstevel@tonic-gate } 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate /* make sure it's valid */ 16060Sstevel@tonic-gate error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW, 16070Sstevel@tonic-gate NULLVPP, &vp); 16080Sstevel@tonic-gate if (error) { 16090Sstevel@tonic-gate goto out; 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate v_type = vp->v_type; 16120Sstevel@tonic-gate VN_RELE(vp); 16130Sstevel@tonic-gate if (!V_ISLOFIABLE(v_type)) { 16140Sstevel@tonic-gate error = EINVAL; 16150Sstevel@tonic-gate goto out; 16160Sstevel@tonic-gate } 16170Sstevel@tonic-gate flag = FREAD | FWRITE | FOFFMAX | FEXCL; 16180Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 16190Sstevel@tonic-gate if (error) { 16200Sstevel@tonic-gate /* try read-only */ 16210Sstevel@tonic-gate flag &= ~FWRITE; 16220Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 16230Sstevel@tonic-gate &vp, 0, 0); 16240Sstevel@tonic-gate if (error) { 16250Sstevel@tonic-gate goto out; 16260Sstevel@tonic-gate } 16270Sstevel@tonic-gate } 16288313SDina.Nimeh@Sun.Com need_vn_close = B_TRUE; 16298313SDina.Nimeh@Sun.Com 16300Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 16315331Samw error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 16320Sstevel@tonic-gate if (error) { 16338313SDina.Nimeh@Sun.Com goto out; 16340Sstevel@tonic-gate } 16350Sstevel@tonic-gate /* the file needs to be a multiple of the block size */ 16360Sstevel@tonic-gate if ((vattr.va_size % DEV_BSIZE) != 0) { 16370Sstevel@tonic-gate error = EINVAL; 16388313SDina.Nimeh@Sun.Com goto out; 16390Sstevel@tonic-gate } 16400Sstevel@tonic-gate newdev = makedevice(getmajor(dev), newminor); 16410Sstevel@tonic-gate Size_prop_val = vattr.va_size; 16420Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 16430Sstevel@tonic-gate SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) { 16440Sstevel@tonic-gate error = EINVAL; 16458313SDina.Nimeh@Sun.Com goto out; 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate Nblocks_prop_val = vattr.va_size / DEV_BSIZE; 16480Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 16490Sstevel@tonic-gate NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 16500Sstevel@tonic-gate error = EINVAL; 16510Sstevel@tonic-gate goto propout; 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, newminor); 16540Sstevel@tonic-gate if (error == DDI_FAILURE) { 16550Sstevel@tonic-gate error = ENOMEM; 16560Sstevel@tonic-gate goto propout; 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate zalloced = 1; 16590Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 16606883Sgd78059 error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor, 16610Sstevel@tonic-gate DDI_PSEUDO, NULL); 16620Sstevel@tonic-gate if (error != DDI_SUCCESS) { 16630Sstevel@tonic-gate error = ENXIO; 16640Sstevel@tonic-gate goto propout; 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor); 16670Sstevel@tonic-gate error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor, 16680Sstevel@tonic-gate DDI_PSEUDO, NULL); 16690Sstevel@tonic-gate if (error != DDI_SUCCESS) { 16700Sstevel@tonic-gate /* remove block node */ 16710Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 16720Sstevel@tonic-gate ddi_remove_minor_node(lofi_dip, namebuf); 16730Sstevel@tonic-gate error = ENXIO; 16740Sstevel@tonic-gate goto propout; 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, newminor); 16770Sstevel@tonic-gate lsp->ls_filename_sz = strlen(klip->li_filename) + 1; 16780Sstevel@tonic-gate lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP); 16790Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 16800Sstevel@tonic-gate LOFI_DRIVER_NAME, newminor); 16810Sstevel@tonic-gate lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads, 16820Sstevel@tonic-gate minclsyspri, 1, lofi_taskq_maxalloc, 0); 16830Sstevel@tonic-gate lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor, 16840Sstevel@tonic-gate NULL, "disk", KSTAT_TYPE_IO, 1, 0); 16850Sstevel@tonic-gate if (lsp->ls_kstat) { 16860Sstevel@tonic-gate mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 16870Sstevel@tonic-gate lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 16880Sstevel@tonic-gate kstat_install(lsp->ls_kstat); 16890Sstevel@tonic-gate } 16904451Seschrock cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 16914451Seschrock mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 16924451Seschrock 16930Sstevel@tonic-gate /* 16940Sstevel@tonic-gate * save open mode so file can be closed properly and vnode counts 16950Sstevel@tonic-gate * updated correctly. 16960Sstevel@tonic-gate */ 16970Sstevel@tonic-gate lsp->ls_openflag = flag; 16980Sstevel@tonic-gate 16990Sstevel@tonic-gate /* 17000Sstevel@tonic-gate * Try to handle stacked lofs vnodes. 17010Sstevel@tonic-gate */ 17020Sstevel@tonic-gate if (vp->v_type == VREG) { 17035331Samw if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) { 17040Sstevel@tonic-gate lsp->ls_vp = vp; 17050Sstevel@tonic-gate } else { 17060Sstevel@tonic-gate /* 17070Sstevel@tonic-gate * Even though vp was obtained via vn_open(), we 17080Sstevel@tonic-gate * can't call vn_close() on it, since lofs will 17090Sstevel@tonic-gate * pass the VOP_CLOSE() on down to the realvp 17100Sstevel@tonic-gate * (which we are about to use). Hence we merely 17110Sstevel@tonic-gate * drop the reference to the lofs vnode and hold 17120Sstevel@tonic-gate * the realvp so things behave as if we've 17130Sstevel@tonic-gate * opened the realvp without any interaction 17140Sstevel@tonic-gate * with lofs. 17150Sstevel@tonic-gate */ 17160Sstevel@tonic-gate VN_HOLD(lsp->ls_vp); 17170Sstevel@tonic-gate VN_RELE(vp); 17180Sstevel@tonic-gate } 17190Sstevel@tonic-gate } else { 17200Sstevel@tonic-gate lsp->ls_vp = vp; 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate lsp->ls_vp_size = vattr.va_size; 17230Sstevel@tonic-gate (void) strcpy(lsp->ls_filename, klip->li_filename); 17240Sstevel@tonic-gate if (rvalp) 17250Sstevel@tonic-gate *rvalp = (int)newminor; 17260Sstevel@tonic-gate klip->li_minor = newminor; 17270Sstevel@tonic-gate 17285643Saalok /* 17298313SDina.Nimeh@Sun.Com * Initialize crypto details for encrypted lofi 17305643Saalok */ 17318313SDina.Nimeh@Sun.Com if (klip->li_crypto_enabled) { 17328313SDina.Nimeh@Sun.Com int ret; 17338313SDina.Nimeh@Sun.Com 17348313SDina.Nimeh@Sun.Com mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL); 17358313SDina.Nimeh@Sun.Com 17368313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher); 17378313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) { 17388313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "invalid cipher %s requested for %s", 17398313SDina.Nimeh@Sun.Com klip->li_cipher, lsp->ls_filename); 17408313SDina.Nimeh@Sun.Com error = EINVAL; 17418313SDina.Nimeh@Sun.Com goto propout; 17428313SDina.Nimeh@Sun.Com } 17438313SDina.Nimeh@Sun.Com 17448313SDina.Nimeh@Sun.Com /* this is just initialization here */ 17458313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param = NULL; 17468313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len = 0; 17478313SDina.Nimeh@Sun.Com 17488313SDina.Nimeh@Sun.Com lsp->ls_iv_type = klip->li_iv_type; 17498313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher); 17508313SDina.Nimeh@Sun.Com if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) { 17518313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "invalid iv cipher %s requested" 17528313SDina.Nimeh@Sun.Com " for %s", klip->li_iv_cipher, lsp->ls_filename); 17538313SDina.Nimeh@Sun.Com error = EINVAL; 17548313SDina.Nimeh@Sun.Com goto propout; 17558313SDina.Nimeh@Sun.Com } 17568313SDina.Nimeh@Sun.Com 17578313SDina.Nimeh@Sun.Com /* iv mech must itself take a null iv */ 17588313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param = NULL; 17598313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param_len = 0; 17608313SDina.Nimeh@Sun.Com lsp->ls_iv_len = klip->li_iv_len; 17618313SDina.Nimeh@Sun.Com 17628313SDina.Nimeh@Sun.Com /* 17638313SDina.Nimeh@Sun.Com * Create ctx using li_cipher & the raw li_key after checking 17648313SDina.Nimeh@Sun.Com * that it isn't a weak key. 17658313SDina.Nimeh@Sun.Com */ 17668313SDina.Nimeh@Sun.Com lsp->ls_key.ck_format = CRYPTO_KEY_RAW; 17678313SDina.Nimeh@Sun.Com lsp->ls_key.ck_length = klip->li_key_len; 17688313SDina.Nimeh@Sun.Com lsp->ls_key.ck_data = kmem_alloc( 17698313SDina.Nimeh@Sun.Com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP); 17708313SDina.Nimeh@Sun.Com bcopy(klip->li_key, lsp->ls_key.ck_data, 17718313SDina.Nimeh@Sun.Com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 17728313SDina.Nimeh@Sun.Com keycopied = B_TRUE; 17738313SDina.Nimeh@Sun.Com 17748313SDina.Nimeh@Sun.Com ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key); 17758313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS) { 17768313SDina.Nimeh@Sun.Com error = EINVAL; 17778313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "weak key check failed for cipher " 17788313SDina.Nimeh@Sun.Com "%s on file %s (0x%x)", klip->li_cipher, 17798313SDina.Nimeh@Sun.Com lsp->ls_filename, ret); 17808313SDina.Nimeh@Sun.Com goto propout; 17818313SDina.Nimeh@Sun.Com } 17828313SDina.Nimeh@Sun.Com } 17838313SDina.Nimeh@Sun.Com lsp->ls_crypto_enabled = klip->li_crypto_enabled; 17848313SDina.Nimeh@Sun.Com 17858313SDina.Nimeh@Sun.Com /* 17868313SDina.Nimeh@Sun.Com * Read the file signature to check if it is compressed or encrypted. 17878313SDina.Nimeh@Sun.Com * Crypto signature is in a different location; both areas should 17888313SDina.Nimeh@Sun.Com * read to keep compression and encryption mutually exclusive. 17898313SDina.Nimeh@Sun.Com */ 17908313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) { 17918313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE, 17928313SDina.Nimeh@Sun.Com CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 17938313SDina.Nimeh@Sun.Com if (error != 0) 17948313SDina.Nimeh@Sun.Com goto propout; 17958313SDina.Nimeh@Sun.Com } 17968313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 17975643Saalok 0, RLIM64_INFINITY, kcred, &resid); 17985643Saalok if (error != 0) 17995643Saalok goto propout; 18005643Saalok 18018313SDina.Nimeh@Sun.Com /* initialize these variables for all lofi files */ 18025643Saalok lsp->ls_uncomp_seg_sz = 0; 18035643Saalok lsp->ls_vp_comp_size = lsp->ls_vp_size; 18045643Saalok lsp->ls_comp_algorithm[0] = '\0'; 18055643Saalok 18068313SDina.Nimeh@Sun.Com /* encrypted lofi reads/writes shifted by crypto metadata size */ 18078313SDina.Nimeh@Sun.Com lsp->ls_crypto_offset = 0; 18088313SDina.Nimeh@Sun.Com 18098313SDina.Nimeh@Sun.Com /* this is a compressed lofi */ 18108313SDina.Nimeh@Sun.Com if ((compress_index = lofi_compress_select(buf)) != -1) { 18118313SDina.Nimeh@Sun.Com 18128313SDina.Nimeh@Sun.Com /* compression and encryption are mutually exclusive */ 18138313SDina.Nimeh@Sun.Com if (klip->li_crypto_enabled) { 18148313SDina.Nimeh@Sun.Com error = ENOTSUP; 18158313SDina.Nimeh@Sun.Com goto propout; 18168313SDina.Nimeh@Sun.Com } 18178313SDina.Nimeh@Sun.Com 18188313SDina.Nimeh@Sun.Com /* initialize compression info for compressed lofi */ 18195643Saalok lsp->ls_comp_algorithm_index = compress_index; 18205643Saalok (void) strlcpy(lsp->ls_comp_algorithm, 18215643Saalok lofi_compress_table[compress_index].l_name, 18225643Saalok sizeof (lsp->ls_comp_algorithm)); 18238313SDina.Nimeh@Sun.Com 18245643Saalok error = lofi_map_compressed_file(lsp, buf); 18255643Saalok if (error != 0) 18265643Saalok goto propout; 18278313SDina.Nimeh@Sun.Com need_size_update = B_TRUE; 18285643Saalok 18298313SDina.Nimeh@Sun.Com /* this is an encrypted lofi */ 18308313SDina.Nimeh@Sun.Com } else if (strncmp(crybuf, lofi_crypto_magic, 18318313SDina.Nimeh@Sun.Com sizeof (lofi_crypto_magic)) == 0) { 18328313SDina.Nimeh@Sun.Com 18338313SDina.Nimeh@Sun.Com char *marker = crybuf; 18348313SDina.Nimeh@Sun.Com 18358313SDina.Nimeh@Sun.Com /* 18368313SDina.Nimeh@Sun.Com * This is the case where the header in the lofi image is 18378313SDina.Nimeh@Sun.Com * already initialized to indicate it is encrypted. 18388313SDina.Nimeh@Sun.Com * There is another case (see below) where encryption is 18398313SDina.Nimeh@Sun.Com * requested but the lofi image has never been used yet, 18408313SDina.Nimeh@Sun.Com * so the header needs to be written with encryption magic. 18418313SDina.Nimeh@Sun.Com */ 18428313SDina.Nimeh@Sun.Com 18438313SDina.Nimeh@Sun.Com /* indicate this must be an encrypted lofi due to magic */ 18448313SDina.Nimeh@Sun.Com klip->li_crypto_enabled = B_TRUE; 18458313SDina.Nimeh@Sun.Com 18468313SDina.Nimeh@Sun.Com /* 18478313SDina.Nimeh@Sun.Com * The encryption header information is laid out this way: 18488313SDina.Nimeh@Sun.Com * 6 bytes: hex "CFLOFI" 18498313SDina.Nimeh@Sun.Com * 2 bytes: version = 0 ... for now 18508313SDina.Nimeh@Sun.Com * 96 bytes: reserved1 (not implemented yet) 18518313SDina.Nimeh@Sun.Com * 4 bytes: data_sector = 2 ... for now 18528313SDina.Nimeh@Sun.Com * more... not implemented yet 18538313SDina.Nimeh@Sun.Com */ 18548313SDina.Nimeh@Sun.Com 18558313SDina.Nimeh@Sun.Com /* copy the magic */ 18568313SDina.Nimeh@Sun.Com bcopy(marker, lsp->ls_crypto.magic, 18578313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.magic)); 18588313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.magic); 18598313SDina.Nimeh@Sun.Com 18608313SDina.Nimeh@Sun.Com /* read the encryption version number */ 18618313SDina.Nimeh@Sun.Com bcopy(marker, &(lsp->ls_crypto.version), 18628313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.version)); 18638313SDina.Nimeh@Sun.Com lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version); 18648313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.version); 18658313SDina.Nimeh@Sun.Com 18668313SDina.Nimeh@Sun.Com /* read a chunk of reserved data */ 18678313SDina.Nimeh@Sun.Com bcopy(marker, lsp->ls_crypto.reserved1, 18688313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.reserved1)); 18698313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.reserved1); 18708313SDina.Nimeh@Sun.Com 18718313SDina.Nimeh@Sun.Com /* read block number where encrypted data begins */ 18728313SDina.Nimeh@Sun.Com bcopy(marker, &(lsp->ls_crypto.data_sector), 18738313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.data_sector)); 18748313SDina.Nimeh@Sun.Com lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector); 18758313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.data_sector); 18768313SDina.Nimeh@Sun.Com 18778313SDina.Nimeh@Sun.Com /* and ignore the rest until it is implemented */ 18788313SDina.Nimeh@Sun.Com 18798313SDina.Nimeh@Sun.Com lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 18808313SDina.Nimeh@Sun.Com need_size_update = B_TRUE; 18818313SDina.Nimeh@Sun.Com 18828313SDina.Nimeh@Sun.Com /* neither compressed nor encrypted, BUT could be new encrypted lofi */ 18838313SDina.Nimeh@Sun.Com } else if (klip->li_crypto_enabled) { 18848313SDina.Nimeh@Sun.Com 18858313SDina.Nimeh@Sun.Com /* 18868313SDina.Nimeh@Sun.Com * This is the case where encryption was requested but the 18878313SDina.Nimeh@Sun.Com * appears to be entirely blank where the encryption header 18888313SDina.Nimeh@Sun.Com * would have been in the lofi image. If it is blank, 18898313SDina.Nimeh@Sun.Com * assume it is a brand new lofi image and initialize the 18908313SDina.Nimeh@Sun.Com * header area with encryption magic and current version 18918313SDina.Nimeh@Sun.Com * header data. If it is not blank, that's an error. 18928313SDina.Nimeh@Sun.Com */ 18938313SDina.Nimeh@Sun.Com int i; 18948313SDina.Nimeh@Sun.Com char *marker; 18958313SDina.Nimeh@Sun.Com struct crypto_meta chead; 18968313SDina.Nimeh@Sun.Com 18978313SDina.Nimeh@Sun.Com for (i = 0; i < sizeof (struct crypto_meta); i++) 18988313SDina.Nimeh@Sun.Com if (crybuf[i] != '\0') 18998313SDina.Nimeh@Sun.Com break; 19008313SDina.Nimeh@Sun.Com if (i != sizeof (struct crypto_meta)) { 19018313SDina.Nimeh@Sun.Com error = EINVAL; 19028313SDina.Nimeh@Sun.Com goto propout; 19038313SDina.Nimeh@Sun.Com } 19048313SDina.Nimeh@Sun.Com 19058313SDina.Nimeh@Sun.Com /* nothing there, initialize as encrypted lofi */ 19068313SDina.Nimeh@Sun.Com marker = crybuf; 19078313SDina.Nimeh@Sun.Com bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic)); 19088313SDina.Nimeh@Sun.Com marker += sizeof (lofi_crypto_magic); 19098313SDina.Nimeh@Sun.Com chead.version = htons(LOFI_CRYPTO_VERSION); 19108313SDina.Nimeh@Sun.Com bcopy(&(chead.version), marker, sizeof (chead.version)); 19118313SDina.Nimeh@Sun.Com marker += sizeof (chead.version); 19128313SDina.Nimeh@Sun.Com marker += sizeof (chead.reserved1); 19138313SDina.Nimeh@Sun.Com chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR); 19148313SDina.Nimeh@Sun.Com bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector)); 19158313SDina.Nimeh@Sun.Com 19168313SDina.Nimeh@Sun.Com /* write the header */ 19178313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE, 19188313SDina.Nimeh@Sun.Com CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 19198313SDina.Nimeh@Sun.Com if (error != 0) 19208313SDina.Nimeh@Sun.Com goto propout; 19218313SDina.Nimeh@Sun.Com 19228313SDina.Nimeh@Sun.Com /* fix things up so it looks like we read this info */ 19238313SDina.Nimeh@Sun.Com bcopy(lofi_crypto_magic, lsp->ls_crypto.magic, 19248313SDina.Nimeh@Sun.Com sizeof (lofi_crypto_magic)); 19258313SDina.Nimeh@Sun.Com lsp->ls_crypto.version = LOFI_CRYPTO_VERSION; 19268313SDina.Nimeh@Sun.Com lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR; 19278313SDina.Nimeh@Sun.Com 19288313SDina.Nimeh@Sun.Com lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 19298313SDina.Nimeh@Sun.Com need_size_update = B_TRUE; 19308313SDina.Nimeh@Sun.Com } 19318313SDina.Nimeh@Sun.Com 19328313SDina.Nimeh@Sun.Com /* 19338313SDina.Nimeh@Sun.Com * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed; 19348313SDina.Nimeh@Sun.Com * for encrypted lofi, advertise that it is somewhat shorter 19358313SDina.Nimeh@Sun.Com * due to embedded crypto metadata section 19368313SDina.Nimeh@Sun.Com */ 19378313SDina.Nimeh@Sun.Com if (need_size_update) { 19385643Saalok /* update DDI properties */ 19398313SDina.Nimeh@Sun.Com Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset; 19405643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 19415643Saalok Size_prop_val)) != DDI_PROP_SUCCESS) { 19425643Saalok error = EINVAL; 19435643Saalok goto propout; 19445643Saalok } 19458313SDina.Nimeh@Sun.Com Nblocks_prop_val = 19468313SDina.Nimeh@Sun.Com (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE; 19475643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 19485643Saalok Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 19495643Saalok error = EINVAL; 19505643Saalok goto propout; 19515643Saalok } 19525643Saalok } 19535643Saalok 19540Sstevel@tonic-gate fake_disk_geometry(lsp); 19550Sstevel@tonic-gate mutex_exit(&lofi_lock); 19561657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 19570Sstevel@tonic-gate free_lofi_ioctl(klip); 19580Sstevel@tonic-gate return (0); 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate propout: 19618313SDina.Nimeh@Sun.Com if (keycopied) { 19628313SDina.Nimeh@Sun.Com bzero(lsp->ls_key.ck_data, 19638313SDina.Nimeh@Sun.Com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 19648313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_key.ck_data, 19658313SDina.Nimeh@Sun.Com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 19668313SDina.Nimeh@Sun.Com lsp->ls_key.ck_data = NULL; 19678313SDina.Nimeh@Sun.Com lsp->ls_key.ck_length = 0; 19688313SDina.Nimeh@Sun.Com } 19698313SDina.Nimeh@Sun.Com 19708313SDina.Nimeh@Sun.Com if (zalloced) 19718313SDina.Nimeh@Sun.Com ddi_soft_state_free(lofi_statep, newminor); 19728313SDina.Nimeh@Sun.Com 19730Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 19740Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 19758313SDina.Nimeh@Sun.Com 19760Sstevel@tonic-gate out: 19778313SDina.Nimeh@Sun.Com if (need_vn_close) { 19788313SDina.Nimeh@Sun.Com (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 19798313SDina.Nimeh@Sun.Com VN_RELE(vp); 19808313SDina.Nimeh@Sun.Com } 19818313SDina.Nimeh@Sun.Com 19820Sstevel@tonic-gate mutex_exit(&lofi_lock); 19830Sstevel@tonic-gate free_lofi_ioctl(klip); 19840Sstevel@tonic-gate return (error); 19850Sstevel@tonic-gate } 19860Sstevel@tonic-gate 19870Sstevel@tonic-gate /* 19880Sstevel@tonic-gate * unmap a file. 19890Sstevel@tonic-gate */ 19900Sstevel@tonic-gate static int 19910Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 19921657Sheppo struct cred *credp, int ioctl_flag) 19930Sstevel@tonic-gate { 19940Sstevel@tonic-gate struct lofi_state *lsp; 19950Sstevel@tonic-gate struct lofi_ioctl *klip; 19960Sstevel@tonic-gate minor_t minor; 19970Sstevel@tonic-gate 19981657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 19990Sstevel@tonic-gate if (klip == NULL) 20000Sstevel@tonic-gate return (EFAULT); 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate mutex_enter(&lofi_lock); 20030Sstevel@tonic-gate if (byfilename) { 20040Sstevel@tonic-gate minor = file_to_minor(klip->li_filename); 20050Sstevel@tonic-gate } else { 20060Sstevel@tonic-gate minor = klip->li_minor; 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate if (minor == 0) { 20090Sstevel@tonic-gate mutex_exit(&lofi_lock); 20100Sstevel@tonic-gate free_lofi_ioctl(klip); 20110Sstevel@tonic-gate return (ENXIO); 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 20144451Seschrock if (lsp == NULL || lsp->ls_vp == NULL) { 20150Sstevel@tonic-gate mutex_exit(&lofi_lock); 20160Sstevel@tonic-gate free_lofi_ioctl(klip); 20170Sstevel@tonic-gate return (ENXIO); 20180Sstevel@tonic-gate } 20194451Seschrock 20206734Sjohnlev /* 20216734Sjohnlev * If it's still held open, we'll do one of three things: 20226734Sjohnlev * 20236734Sjohnlev * If no flag is set, just return EBUSY. 20246734Sjohnlev * 20256734Sjohnlev * If the 'cleanup' flag is set, unmap and remove the device when 20266734Sjohnlev * the last user finishes. 20276734Sjohnlev * 20286734Sjohnlev * If the 'force' flag is set, then we forcibly close the underlying 20296734Sjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl 20306734Sjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the 20316734Sjohnlev * device will be cleaned up appropriately. 20326734Sjohnlev * 20336734Sjohnlev * This is complicated by the fact that we may have outstanding 20346734Sjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all 20358313SDina.Nimeh@Sun.Com * I/O, we keep a count of the number of outstanding I/O requests 20368313SDina.Nimeh@Sun.Com * (ls_vp_iocount), as well as a flag to indicate that no new I/Os 20378313SDina.Nimeh@Sun.Com * should be dispatched (ls_vp_closereq). 20388313SDina.Nimeh@Sun.Com * 20396734Sjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0, 20406734Sjohnlev * and then close the underlying vnode. 20416734Sjohnlev */ 20420Sstevel@tonic-gate if (is_opened(lsp)) { 20434451Seschrock if (klip->li_force) { 20448313SDina.Nimeh@Sun.Com /* 20458313SDina.Nimeh@Sun.Com * XXX: the section marked here should probably be 20468313SDina.Nimeh@Sun.Com * carefully incorporated into lofi_free_handle(); 20478313SDina.Nimeh@Sun.Com * afterward just replace this section with: 20488313SDina.Nimeh@Sun.Com * lofi_free_handle(dev, minor, lsp, credp); 20498313SDina.Nimeh@Sun.Com * and clean up lofi_unmap_file() a bit more 20508313SDina.Nimeh@Sun.Com */ 20518313SDina.Nimeh@Sun.Com lofi_free_crypto(lsp); 20528313SDina.Nimeh@Sun.Com 20534451Seschrock mutex_enter(&lsp->ls_vp_lock); 20544451Seschrock lsp->ls_vp_closereq = B_TRUE; 20554451Seschrock while (lsp->ls_vp_iocount > 0) 20564451Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 20574451Seschrock (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, 20585331Samw credp, NULL); 20594451Seschrock VN_RELE(lsp->ls_vp); 20604451Seschrock lsp->ls_vp = NULL; 20614451Seschrock cv_broadcast(&lsp->ls_vp_cv); 20624451Seschrock mutex_exit(&lsp->ls_vp_lock); 20638313SDina.Nimeh@Sun.Com /* 20648313SDina.Nimeh@Sun.Com * XXX: to here 20658313SDina.Nimeh@Sun.Com */ 20668313SDina.Nimeh@Sun.Com 20678313SDina.Nimeh@Sun.Com klip->li_minor = minor; 20684451Seschrock mutex_exit(&lofi_lock); 20694451Seschrock (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 20704451Seschrock free_lofi_ioctl(klip); 20714451Seschrock return (0); 20726734Sjohnlev } else if (klip->li_cleanup) { 20736734Sjohnlev lsp->ls_cleanup = 1; 20746734Sjohnlev mutex_exit(&lofi_lock); 20756734Sjohnlev free_lofi_ioctl(klip); 20766734Sjohnlev return (0); 20774451Seschrock } 20786734Sjohnlev 20790Sstevel@tonic-gate mutex_exit(&lofi_lock); 20800Sstevel@tonic-gate free_lofi_ioctl(klip); 20810Sstevel@tonic-gate return (EBUSY); 20820Sstevel@tonic-gate } 20830Sstevel@tonic-gate 20844451Seschrock lofi_free_handle(dev, minor, lsp, credp); 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate klip->li_minor = minor; 20870Sstevel@tonic-gate mutex_exit(&lofi_lock); 20881657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 20890Sstevel@tonic-gate free_lofi_ioctl(klip); 20900Sstevel@tonic-gate return (0); 20910Sstevel@tonic-gate } 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate /* 20940Sstevel@tonic-gate * get the filename given the minor number, or the minor number given 20950Sstevel@tonic-gate * the name. 20960Sstevel@tonic-gate */ 20974451Seschrock /*ARGSUSED*/ 20980Sstevel@tonic-gate static int 20990Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 21001657Sheppo struct cred *credp, int ioctl_flag) 21010Sstevel@tonic-gate { 21020Sstevel@tonic-gate struct lofi_state *lsp; 21030Sstevel@tonic-gate struct lofi_ioctl *klip; 21040Sstevel@tonic-gate int error; 21050Sstevel@tonic-gate minor_t minor; 21060Sstevel@tonic-gate 21071657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 21080Sstevel@tonic-gate if (klip == NULL) 21090Sstevel@tonic-gate return (EFAULT); 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate switch (which) { 21120Sstevel@tonic-gate case LOFI_GET_FILENAME: 21130Sstevel@tonic-gate minor = klip->li_minor; 21140Sstevel@tonic-gate if (minor == 0) { 21150Sstevel@tonic-gate free_lofi_ioctl(klip); 21160Sstevel@tonic-gate return (EINVAL); 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate mutex_enter(&lofi_lock); 21200Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 21210Sstevel@tonic-gate if (lsp == NULL) { 21220Sstevel@tonic-gate mutex_exit(&lofi_lock); 21230Sstevel@tonic-gate free_lofi_ioctl(klip); 21240Sstevel@tonic-gate return (ENXIO); 21250Sstevel@tonic-gate } 21260Sstevel@tonic-gate (void) strcpy(klip->li_filename, lsp->ls_filename); 21275643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 21285643Saalok sizeof (klip->li_algorithm)); 21298313SDina.Nimeh@Sun.Com klip->li_crypto_enabled = lsp->ls_crypto_enabled; 21300Sstevel@tonic-gate mutex_exit(&lofi_lock); 21311657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 21320Sstevel@tonic-gate free_lofi_ioctl(klip); 21330Sstevel@tonic-gate return (error); 21340Sstevel@tonic-gate case LOFI_GET_MINOR: 21350Sstevel@tonic-gate mutex_enter(&lofi_lock); 21360Sstevel@tonic-gate klip->li_minor = file_to_minor(klip->li_filename); 21378313SDina.Nimeh@Sun.Com /* caller should not depend on klip->li_crypto_enabled here */ 21380Sstevel@tonic-gate mutex_exit(&lofi_lock); 21390Sstevel@tonic-gate if (klip->li_minor == 0) { 21400Sstevel@tonic-gate free_lofi_ioctl(klip); 21410Sstevel@tonic-gate return (ENOENT); 21420Sstevel@tonic-gate } 21431657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 21440Sstevel@tonic-gate free_lofi_ioctl(klip); 21450Sstevel@tonic-gate return (error); 21465643Saalok case LOFI_CHECK_COMPRESSED: 21475643Saalok mutex_enter(&lofi_lock); 21485643Saalok klip->li_minor = file_to_minor(klip->li_filename); 21495643Saalok mutex_exit(&lofi_lock); 21505643Saalok if (klip->li_minor == 0) { 21515643Saalok free_lofi_ioctl(klip); 21525643Saalok return (ENOENT); 21535643Saalok } 21545643Saalok mutex_enter(&lofi_lock); 21555643Saalok lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 21565643Saalok if (lsp == NULL) { 21575643Saalok mutex_exit(&lofi_lock); 21585643Saalok free_lofi_ioctl(klip); 21595643Saalok return (ENXIO); 21605643Saalok } 21615643Saalok ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0); 21625643Saalok 21635643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 21645643Saalok sizeof (klip->li_algorithm)); 21655643Saalok mutex_exit(&lofi_lock); 21665643Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 21675643Saalok free_lofi_ioctl(klip); 21685643Saalok return (error); 21690Sstevel@tonic-gate default: 21700Sstevel@tonic-gate free_lofi_ioctl(klip); 21710Sstevel@tonic-gate return (EINVAL); 21720Sstevel@tonic-gate } 21730Sstevel@tonic-gate 21740Sstevel@tonic-gate } 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate static int 21770Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 21780Sstevel@tonic-gate int *rvalp) 21790Sstevel@tonic-gate { 21800Sstevel@tonic-gate int error; 21810Sstevel@tonic-gate enum dkio_state dkstate; 21820Sstevel@tonic-gate struct lofi_state *lsp; 21830Sstevel@tonic-gate minor_t minor; 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate minor = getminor(dev); 21860Sstevel@tonic-gate /* lofi ioctls only apply to the master device */ 21870Sstevel@tonic-gate if (minor == 0) { 21880Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate /* 21910Sstevel@tonic-gate * the query command only need read-access - i.e., normal 21920Sstevel@tonic-gate * users are allowed to do those on the ctl device as 21930Sstevel@tonic-gate * long as they can open it read-only. 21940Sstevel@tonic-gate */ 21950Sstevel@tonic-gate switch (cmd) { 21960Sstevel@tonic-gate case LOFI_MAP_FILE: 21970Sstevel@tonic-gate if ((flag & FWRITE) == 0) 21980Sstevel@tonic-gate return (EPERM); 21991657Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 22000Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR: 22010Sstevel@tonic-gate if ((flag & FWRITE) == 0) 22020Sstevel@tonic-gate return (EPERM); 22031657Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 22040Sstevel@tonic-gate case LOFI_UNMAP_FILE: 22050Sstevel@tonic-gate if ((flag & FWRITE) == 0) 22060Sstevel@tonic-gate return (EPERM); 22071657Sheppo return (lofi_unmap_file(dev, lip, 1, credp, flag)); 22080Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR: 22090Sstevel@tonic-gate if ((flag & FWRITE) == 0) 22100Sstevel@tonic-gate return (EPERM); 22111657Sheppo return (lofi_unmap_file(dev, lip, 0, credp, flag)); 22120Sstevel@tonic-gate case LOFI_GET_FILENAME: 22130Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 22141657Sheppo credp, flag)); 22150Sstevel@tonic-gate case LOFI_GET_MINOR: 22160Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 22171657Sheppo credp, flag)); 22180Sstevel@tonic-gate case LOFI_GET_MAXMINOR: 22191657Sheppo error = ddi_copyout(&lofi_max_files, &lip->li_minor, 22201657Sheppo sizeof (lofi_max_files), flag); 22210Sstevel@tonic-gate if (error) 22220Sstevel@tonic-gate return (EFAULT); 22230Sstevel@tonic-gate return (0); 22245643Saalok case LOFI_CHECK_COMPRESSED: 22255643Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 22265643Saalok credp, flag)); 22270Sstevel@tonic-gate default: 22280Sstevel@tonic-gate break; 22290Sstevel@tonic-gate } 22300Sstevel@tonic-gate } 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 22330Sstevel@tonic-gate if (lsp == NULL) 22340Sstevel@tonic-gate return (ENXIO); 22350Sstevel@tonic-gate 22364451Seschrock /* 22374451Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 22384451Seschrock * EIO as if the device was no longer present. 22394451Seschrock */ 22404451Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 22414451Seschrock return (EIO); 22424451Seschrock 22430Sstevel@tonic-gate /* these are for faking out utilities like newfs */ 22440Sstevel@tonic-gate switch (cmd) { 22450Sstevel@tonic-gate case DKIOCGVTOC: 22460Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) { 22470Sstevel@tonic-gate case DDI_MODEL_ILP32: { 22480Sstevel@tonic-gate struct vtoc32 vtoc32; 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32); 22510Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg, 22520Sstevel@tonic-gate sizeof (struct vtoc32), flag)) 22530Sstevel@tonic-gate return (EFAULT); 22548719SDina.Nimeh@Sun.COM break; 22550Sstevel@tonic-gate } 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate case DDI_MODEL_NONE: 22580Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 22590Sstevel@tonic-gate sizeof (struct vtoc), flag)) 22600Sstevel@tonic-gate return (EFAULT); 22610Sstevel@tonic-gate break; 22620Sstevel@tonic-gate } 22630Sstevel@tonic-gate return (0); 22640Sstevel@tonic-gate case DKIOCINFO: 22651657Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg, 22661657Sheppo sizeof (struct dk_cinfo), flag); 22670Sstevel@tonic-gate if (error) 22680Sstevel@tonic-gate return (EFAULT); 22690Sstevel@tonic-gate return (0); 22700Sstevel@tonic-gate case DKIOCG_VIRTGEOM: 22710Sstevel@tonic-gate case DKIOCG_PHYGEOM: 22720Sstevel@tonic-gate case DKIOCGGEOM: 22731657Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 22741657Sheppo sizeof (struct dk_geom), flag); 22750Sstevel@tonic-gate if (error) 22760Sstevel@tonic-gate return (EFAULT); 22770Sstevel@tonic-gate return (0); 22780Sstevel@tonic-gate case DKIOCSTATE: 22794451Seschrock /* 22804451Seschrock * Normally, lofi devices are always in the INSERTED state. If 22814451Seschrock * a device is forcefully unmapped, then the device transitions 22824451Seschrock * to the DKIO_DEV_GONE state. 22834451Seschrock */ 22844451Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 22854451Seschrock flag) != 0) 22864451Seschrock return (EFAULT); 22874451Seschrock 22884451Seschrock mutex_enter(&lsp->ls_vp_lock); 22894451Seschrock while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 22904451Seschrock (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) { 22914451Seschrock /* 22924451Seschrock * By virtue of having the device open, we know that 22934451Seschrock * 'lsp' will remain valid when we return. 22944451Seschrock */ 22954451Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv, 22964451Seschrock &lsp->ls_vp_lock)) { 22974451Seschrock mutex_exit(&lsp->ls_vp_lock); 22984451Seschrock return (EINTR); 22994451Seschrock } 23004451Seschrock } 23014451Seschrock 23024451Seschrock dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE); 23034451Seschrock mutex_exit(&lsp->ls_vp_lock); 23044451Seschrock 23054451Seschrock if (ddi_copyout(&dkstate, (void *)arg, 23064451Seschrock sizeof (dkstate), flag) != 0) 23070Sstevel@tonic-gate return (EFAULT); 23080Sstevel@tonic-gate return (0); 23090Sstevel@tonic-gate default: 23100Sstevel@tonic-gate return (ENOTTY); 23110Sstevel@tonic-gate } 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = { 23150Sstevel@tonic-gate lofi_open, /* open */ 23160Sstevel@tonic-gate lofi_close, /* close */ 23170Sstevel@tonic-gate lofi_strategy, /* strategy */ 23180Sstevel@tonic-gate nodev, /* print */ 23190Sstevel@tonic-gate nodev, /* dump */ 23200Sstevel@tonic-gate lofi_read, /* read */ 23210Sstevel@tonic-gate lofi_write, /* write */ 23220Sstevel@tonic-gate lofi_ioctl, /* ioctl */ 23230Sstevel@tonic-gate nodev, /* devmap */ 23240Sstevel@tonic-gate nodev, /* mmap */ 23250Sstevel@tonic-gate nodev, /* segmap */ 23260Sstevel@tonic-gate nochpoll, /* poll */ 23270Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 23280Sstevel@tonic-gate 0, /* streamtab */ 23290Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 23300Sstevel@tonic-gate CB_REV, 23310Sstevel@tonic-gate lofi_aread, 23320Sstevel@tonic-gate lofi_awrite 23330Sstevel@tonic-gate }; 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate static struct dev_ops lofi_ops = { 23360Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 23370Sstevel@tonic-gate 0, /* refcnt */ 23380Sstevel@tonic-gate lofi_info, /* info */ 23390Sstevel@tonic-gate nulldev, /* identify */ 23400Sstevel@tonic-gate nulldev, /* probe */ 23410Sstevel@tonic-gate lofi_attach, /* attach */ 23420Sstevel@tonic-gate lofi_detach, /* detach */ 23430Sstevel@tonic-gate nodev, /* reset */ 23440Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */ 23457656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 23467656SSherry.Moore@Sun.COM NULL, /* power */ 23478313SDina.Nimeh@Sun.Com ddi_quiesce_not_needed, /* quiesce */ 23480Sstevel@tonic-gate }; 23490Sstevel@tonic-gate 23500Sstevel@tonic-gate static struct modldrv modldrv = { 23510Sstevel@tonic-gate &mod_driverops, 23527656SSherry.Moore@Sun.COM "loopback file driver", 23530Sstevel@tonic-gate &lofi_ops, 23540Sstevel@tonic-gate }; 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate static struct modlinkage modlinkage = { 23570Sstevel@tonic-gate MODREV_1, 23580Sstevel@tonic-gate &modldrv, 23590Sstevel@tonic-gate NULL 23600Sstevel@tonic-gate }; 23610Sstevel@tonic-gate 23620Sstevel@tonic-gate int 23630Sstevel@tonic-gate _init(void) 23640Sstevel@tonic-gate { 23650Sstevel@tonic-gate int error; 23660Sstevel@tonic-gate 23670Sstevel@tonic-gate error = ddi_soft_state_init(&lofi_statep, 23680Sstevel@tonic-gate sizeof (struct lofi_state), 0); 23690Sstevel@tonic-gate if (error) 23700Sstevel@tonic-gate return (error); 23710Sstevel@tonic-gate 23720Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL); 23730Sstevel@tonic-gate error = mod_install(&modlinkage); 23740Sstevel@tonic-gate if (error) { 23750Sstevel@tonic-gate mutex_destroy(&lofi_lock); 23760Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 23770Sstevel@tonic-gate } 23780Sstevel@tonic-gate 23790Sstevel@tonic-gate return (error); 23800Sstevel@tonic-gate } 23810Sstevel@tonic-gate 23820Sstevel@tonic-gate int 23830Sstevel@tonic-gate _fini(void) 23840Sstevel@tonic-gate { 23850Sstevel@tonic-gate int error; 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate if (lofi_busy()) 23880Sstevel@tonic-gate return (EBUSY); 23890Sstevel@tonic-gate 23900Sstevel@tonic-gate error = mod_remove(&modlinkage); 23910Sstevel@tonic-gate if (error) 23920Sstevel@tonic-gate return (error); 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate mutex_destroy(&lofi_lock); 23950Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 23960Sstevel@tonic-gate 23970Sstevel@tonic-gate return (error); 23980Sstevel@tonic-gate } 23990Sstevel@tonic-gate 24000Sstevel@tonic-gate int 24010Sstevel@tonic-gate _info(struct modinfo *modinfop) 24020Sstevel@tonic-gate { 24030Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 24040Sstevel@tonic-gate } 2405