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 /* 224451Seschrock * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * lofi (loopback file) driver - allows you to attach a file to a device, 300Sstevel@tonic-gate * which can then be accessed through that device. The simple model is that 310Sstevel@tonic-gate * you tell lofi to open a file, and then use the block device you get as 320Sstevel@tonic-gate * you would any block device. lofi translates access to the block device 330Sstevel@tonic-gate * into I/O on the underlying file. This is mostly useful for 340Sstevel@tonic-gate * mounting images of filesystems. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * lofi is controlled through /dev/lofictl - this is the only device exported 370Sstevel@tonic-gate * during attach, and is minor number 0. lofiadm communicates with lofi through 380Sstevel@tonic-gate * ioctls on this device. When a file is attached to lofi, block and character 390Sstevel@tonic-gate * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices 400Sstevel@tonic-gate * are identified by their minor number, and the minor number is also used 410Sstevel@tonic-gate * as the name in /dev/lofi. If we ever decide to support virtual disks, 420Sstevel@tonic-gate * we'll have to divide the minor number space to identify fdisk partitions 430Sstevel@tonic-gate * and slices, and the name will then be the minor number shifted down a 440Sstevel@tonic-gate * few bits. Minor devices are tracked with state structures handled with 450Sstevel@tonic-gate * ddi_soft_state(9F) for simplicity. 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * A file attached to lofi is opened when attached and not closed until 480Sstevel@tonic-gate * explicitly detached from lofi. This seems more sensible than deferring 490Sstevel@tonic-gate * the open until the /dev/lofi device is opened, for a number of reasons. 500Sstevel@tonic-gate * One is that any failure is likely to be noticed by the person (or script) 510Sstevel@tonic-gate * running lofiadm. Another is that it would be a security problem if the 520Sstevel@tonic-gate * file was replaced by another one after being added but before being opened. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * The only hard part about lofi is the ioctls. In order to support things 550Sstevel@tonic-gate * like 'newfs' on a lofi device, it needs to support certain disk ioctls. 560Sstevel@tonic-gate * So it has to fake disk geometry and partition information. More may need 570Sstevel@tonic-gate * to be faked if your favorite utility doesn't work and you think it should 580Sstevel@tonic-gate * (fdformat doesn't work because it really wants to know the type of floppy 590Sstevel@tonic-gate * controller to talk to, and that didn't seem easy to fake. Or possibly even 600Sstevel@tonic-gate * necessary, since we have mkfs_pcfs now). 610Sstevel@tonic-gate * 624451Seschrock * Normally, a lofi device cannot be detached if it is open (i.e. busy). To 634451Seschrock * support simulation of hotplug events, an optional force flag is provided. 644451Seschrock * If a lofi device is open when a force detach is requested, then the 654451Seschrock * underlying file is closed and any subsequent operations return EIO. When the 664451Seschrock * device is closed for the last time, it will be cleaned up at that time. In 674451Seschrock * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is 684451Seschrock * detached but not removed. 694451Seschrock * 700Sstevel@tonic-gate * Known problems: 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * UFS logging. Mounting a UFS filesystem image "logging" 730Sstevel@tonic-gate * works for basic copy testing but wedges during a build of ON through 740Sstevel@tonic-gate * that image. Some deadlock in lufs holding the log mutex and then 750Sstevel@tonic-gate * getting stuck on a buf. So for now, don't do that. 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * Direct I/O. Since the filesystem data is being cached in the buffer 780Sstevel@tonic-gate * cache, _and_ again in the underlying filesystem, it's tempting to 790Sstevel@tonic-gate * enable direct I/O on the underlying file. Don't, because that deadlocks. 800Sstevel@tonic-gate * I think to fix the cache-twice problem we might need filesystem support. 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * lofi on itself. The simple lock strategy (lofi_lock) precludes this 830Sstevel@tonic-gate * because you'll be in lofi_ioctl, holding the lock when you open the 840Sstevel@tonic-gate * file, which, if it's lofi, will grab lofi_lock. We prevent this for 850Sstevel@tonic-gate * now, though not using ddi_soft_state(9F) would make it possible to 860Sstevel@tonic-gate * do. Though it would still be silly. 870Sstevel@tonic-gate * 880Sstevel@tonic-gate * Interesting things to do: 890Sstevel@tonic-gate * 900Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically. 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not 930Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't 940Sstevel@tonic-gate * need to fake a geometry. But this is also silly unless you're replacing 950Sstevel@tonic-gate * metadisk. 960Sstevel@tonic-gate * 970Sstevel@tonic-gate * Encryption. tpm would like this. Apparently Windows 2000 has it, and 980Sstevel@tonic-gate * so does Linux. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate #include <sys/types.h> 102*5643Saalok #include <netinet/in.h> 1030Sstevel@tonic-gate #include <sys/sysmacros.h> 1040Sstevel@tonic-gate #include <sys/cmn_err.h> 1050Sstevel@tonic-gate #include <sys/uio.h> 1060Sstevel@tonic-gate #include <sys/kmem.h> 1070Sstevel@tonic-gate #include <sys/cred.h> 1080Sstevel@tonic-gate #include <sys/mman.h> 1090Sstevel@tonic-gate #include <sys/errno.h> 1100Sstevel@tonic-gate #include <sys/aio_req.h> 1110Sstevel@tonic-gate #include <sys/stat.h> 1120Sstevel@tonic-gate #include <sys/file.h> 1130Sstevel@tonic-gate #include <sys/modctl.h> 1140Sstevel@tonic-gate #include <sys/conf.h> 1150Sstevel@tonic-gate #include <sys/debug.h> 1160Sstevel@tonic-gate #include <sys/vnode.h> 1170Sstevel@tonic-gate #include <sys/lofi.h> 1180Sstevel@tonic-gate #include <sys/fcntl.h> 1190Sstevel@tonic-gate #include <sys/pathname.h> 1200Sstevel@tonic-gate #include <sys/filio.h> 1210Sstevel@tonic-gate #include <sys/fdio.h> 1220Sstevel@tonic-gate #include <sys/open.h> 1230Sstevel@tonic-gate #include <sys/disp.h> 1240Sstevel@tonic-gate #include <vm/seg_map.h> 1250Sstevel@tonic-gate #include <sys/ddi.h> 1260Sstevel@tonic-gate #include <sys/sunddi.h> 127*5643Saalok #include <sys/zmod.h> 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks" 130*5643Saalok #define SIZE_PROP_NAME "Size" 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate static dev_info_t *lofi_dip; 1330Sstevel@tonic-gate static void *lofi_statep; 1340Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */ 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the 1380Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively 1390Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy, 1400Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of 1410Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for 1420Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should 1430Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the 1440Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic 1450Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on 1460Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE. 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE; 1490Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES; 1520Sstevel@tonic-gate 153*5643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 154*5643Saalok size_t *destlen, int level); 155*5643Saalok 156*5643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { 157*5643Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */ 158*5643Saalok {gzip_decompress, NULL, 6, "gzip-6"}, 159*5643Saalok {gzip_decompress, NULL, 9, "gzip-9"} 160*5643Saalok }; 161*5643Saalok 1620Sstevel@tonic-gate static int 1630Sstevel@tonic-gate lofi_busy(void) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate minor_t minor; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * We need to make sure no mappings exist - mod_remove won't 1690Sstevel@tonic-gate * help because the device isn't open. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate mutex_enter(&lofi_lock); 1720Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 1730Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, minor) != NULL) { 1740Sstevel@tonic-gate mutex_exit(&lofi_lock); 1750Sstevel@tonic-gate return (EBUSY); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate mutex_exit(&lofi_lock); 1790Sstevel@tonic-gate return (0); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate static int 1830Sstevel@tonic-gate is_opened(struct lofi_state *lsp) 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 1860Sstevel@tonic-gate return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate static int 1900Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp) 1910Sstevel@tonic-gate { 1920Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 1930Sstevel@tonic-gate switch (otyp) { 1940Sstevel@tonic-gate case OTYP_CHR: 1950Sstevel@tonic-gate lsp->ls_chr_open = 1; 1960Sstevel@tonic-gate break; 1970Sstevel@tonic-gate case OTYP_BLK: 1980Sstevel@tonic-gate lsp->ls_blk_open = 1; 1990Sstevel@tonic-gate break; 2000Sstevel@tonic-gate case OTYP_LYR: 2010Sstevel@tonic-gate lsp->ls_lyr_open_count++; 2020Sstevel@tonic-gate break; 2030Sstevel@tonic-gate default: 2040Sstevel@tonic-gate return (-1); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate return (0); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate static void 2100Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp) 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2130Sstevel@tonic-gate switch (otyp) { 2140Sstevel@tonic-gate case OTYP_CHR: 2150Sstevel@tonic-gate lsp->ls_chr_open = 0; 2160Sstevel@tonic-gate break; 2170Sstevel@tonic-gate case OTYP_BLK: 2180Sstevel@tonic-gate lsp->ls_blk_open = 0; 2190Sstevel@tonic-gate break; 2200Sstevel@tonic-gate case OTYP_LYR: 2210Sstevel@tonic-gate lsp->ls_lyr_open_count--; 2220Sstevel@tonic-gate break; 2230Sstevel@tonic-gate default: 2240Sstevel@tonic-gate break; 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2284451Seschrock static void 2294451Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp, 2304451Seschrock cred_t *credp) 2314451Seschrock { 2324451Seschrock dev_t newdev; 2334451Seschrock char namebuf[50]; 2344451Seschrock 2354451Seschrock if (lsp->ls_vp) { 2365331Samw (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 2375331Samw 1, 0, credp, NULL); 2384451Seschrock VN_RELE(lsp->ls_vp); 2394451Seschrock lsp->ls_vp = NULL; 2404451Seschrock } 2414451Seschrock 2424451Seschrock newdev = makedevice(getmajor(dev), minor); 2434451Seschrock (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 2444451Seschrock (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 2454451Seschrock 2464451Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 2474451Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 2484451Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 2494451Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 2504451Seschrock 2514451Seschrock kmem_free(lsp->ls_filename, lsp->ls_filename_sz); 2524451Seschrock taskq_destroy(lsp->ls_taskq); 2534451Seschrock if (lsp->ls_kstat) { 2544451Seschrock kstat_delete(lsp->ls_kstat); 2554451Seschrock mutex_destroy(&lsp->ls_kstat_lock); 2564451Seschrock } 2574451Seschrock ddi_soft_state_free(lofi_statep, minor); 2584451Seschrock } 2594451Seschrock 2604451Seschrock /*ARGSUSED*/ 2610Sstevel@tonic-gate static int 2620Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp) 2630Sstevel@tonic-gate { 2640Sstevel@tonic-gate minor_t minor; 2650Sstevel@tonic-gate struct lofi_state *lsp; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate mutex_enter(&lofi_lock); 2680Sstevel@tonic-gate minor = getminor(*devp); 2690Sstevel@tonic-gate if (minor == 0) { 2700Sstevel@tonic-gate /* master control device */ 2710Sstevel@tonic-gate /* must be opened exclusively */ 2720Sstevel@tonic-gate if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) { 2730Sstevel@tonic-gate mutex_exit(&lofi_lock); 2740Sstevel@tonic-gate return (EINVAL); 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, 0); 2770Sstevel@tonic-gate if (lsp == NULL) { 2780Sstevel@tonic-gate mutex_exit(&lofi_lock); 2790Sstevel@tonic-gate return (ENXIO); 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate if (is_opened(lsp)) { 2820Sstevel@tonic-gate mutex_exit(&lofi_lock); 2830Sstevel@tonic-gate return (EBUSY); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate (void) mark_opened(lsp, OTYP_CHR); 2860Sstevel@tonic-gate mutex_exit(&lofi_lock); 2870Sstevel@tonic-gate return (0); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* otherwise, the mapping should already exist */ 2910Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 2920Sstevel@tonic-gate if (lsp == NULL) { 2930Sstevel@tonic-gate mutex_exit(&lofi_lock); 2940Sstevel@tonic-gate return (EINVAL); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2974451Seschrock if (lsp->ls_vp == NULL) { 2984451Seschrock mutex_exit(&lofi_lock); 2994451Seschrock return (ENXIO); 3004451Seschrock } 3014451Seschrock 3020Sstevel@tonic-gate if (mark_opened(lsp, otyp) == -1) { 3030Sstevel@tonic-gate mutex_exit(&lofi_lock); 3040Sstevel@tonic-gate return (EINVAL); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate mutex_exit(&lofi_lock); 3080Sstevel@tonic-gate return (0); 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3114451Seschrock /*ARGSUSED*/ 3120Sstevel@tonic-gate static int 3130Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate minor_t minor; 3160Sstevel@tonic-gate struct lofi_state *lsp; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate mutex_enter(&lofi_lock); 3190Sstevel@tonic-gate minor = getminor(dev); 3200Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 3210Sstevel@tonic-gate if (lsp == NULL) { 3220Sstevel@tonic-gate mutex_exit(&lofi_lock); 3230Sstevel@tonic-gate return (EINVAL); 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate mark_closed(lsp, otyp); 3264451Seschrock 3274451Seschrock /* 3284451Seschrock * If we have forcibly closed the underlying device, and this is the 3294451Seschrock * last close, then tear down the rest of the device. 3304451Seschrock */ 3314451Seschrock if (minor != 0 && lsp->ls_vp == NULL && !is_opened(lsp)) 3324451Seschrock lofi_free_handle(dev, minor, lsp, credp); 3330Sstevel@tonic-gate mutex_exit(&lofi_lock); 3340Sstevel@tonic-gate return (0); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 337*5643Saalok static int 338*5643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 339*5643Saalok struct lofi_state *lsp) 340*5643Saalok { 341*5643Saalok int error; 342*5643Saalok offset_t alignedoffset, mapoffset; 343*5643Saalok size_t xfersize; 344*5643Saalok int isread; 345*5643Saalok int smflags; 346*5643Saalok caddr_t mapaddr; 347*5643Saalok size_t len; 348*5643Saalok enum seg_rw srw; 349*5643Saalok 350*5643Saalok /* 351*5643Saalok * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 352*5643Saalok * an 8K boundary, but the buf transfer address may not be 353*5643Saalok * aligned on more than a 512-byte boundary (we don't enforce 354*5643Saalok * that even though we could). This matters since the initial 355*5643Saalok * part of the transfer may not start at offset 0 within the 356*5643Saalok * segmap'd chunk. So we have to compensate for that with 357*5643Saalok * 'mapoffset'. Subsequent chunks always start off at the 358*5643Saalok * beginning, and the last is capped by b_resid 359*5643Saalok */ 360*5643Saalok mapoffset = offset & MAXBOFFSET; 361*5643Saalok alignedoffset = offset - mapoffset; 362*5643Saalok bp->b_resid = bp->b_bcount; 363*5643Saalok isread = bp->b_flags & B_READ; 364*5643Saalok srw = isread ? S_READ : S_WRITE; 365*5643Saalok do { 366*5643Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset, 367*5643Saalok MIN(MAXBSIZE - mapoffset, bp->b_resid)); 368*5643Saalok len = roundup(mapoffset + xfersize, PAGESIZE); 369*5643Saalok mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 370*5643Saalok alignedoffset, MAXBSIZE, 1, srw); 371*5643Saalok /* 372*5643Saalok * Now fault in the pages. This lets us check 373*5643Saalok * for errors before we reference mapaddr and 374*5643Saalok * try to resolve the fault in bcopy (which would 375*5643Saalok * panic instead). And this can easily happen, 376*5643Saalok * particularly if you've lofi'd a file over NFS 377*5643Saalok * and someone deletes the file on the server. 378*5643Saalok */ 379*5643Saalok error = segmap_fault(kas.a_hat, segkmap, mapaddr, 380*5643Saalok len, F_SOFTLOCK, srw); 381*5643Saalok if (error) { 382*5643Saalok (void) segmap_release(segkmap, mapaddr, 0); 383*5643Saalok if (FC_CODE(error) == FC_OBJERR) 384*5643Saalok error = FC_ERRNO(error); 385*5643Saalok else 386*5643Saalok error = EIO; 387*5643Saalok break; 388*5643Saalok } 389*5643Saalok smflags = 0; 390*5643Saalok if (isread) { 391*5643Saalok smflags |= SM_FREE; 392*5643Saalok /* 393*5643Saalok * If we're reading an entire page starting 394*5643Saalok * at a page boundary, there's a good chance 395*5643Saalok * we won't need it again. Put it on the 396*5643Saalok * head of the freelist. 397*5643Saalok */ 398*5643Saalok if (mapoffset == 0 && xfersize == PAGESIZE) 399*5643Saalok smflags |= SM_DONTNEED; 400*5643Saalok bcopy(mapaddr + mapoffset, bufaddr, xfersize); 401*5643Saalok } else { 402*5643Saalok smflags |= SM_WRITE; 403*5643Saalok bcopy(bufaddr, mapaddr + mapoffset, xfersize); 404*5643Saalok } 405*5643Saalok bp->b_resid -= xfersize; 406*5643Saalok bufaddr += xfersize; 407*5643Saalok offset += xfersize; 408*5643Saalok (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 409*5643Saalok len, F_SOFTUNLOCK, srw); 410*5643Saalok error = segmap_release(segkmap, mapaddr, smflags); 411*5643Saalok /* only the first map may start partial */ 412*5643Saalok mapoffset = 0; 413*5643Saalok alignedoffset += MAXBSIZE; 414*5643Saalok } while ((error == 0) && (bp->b_resid > 0) && 415*5643Saalok (offset < lsp->ls_vp_comp_size)); 416*5643Saalok 417*5643Saalok return (error); 418*5643Saalok } 419*5643Saalok 420*5643Saalok /*ARGSUSED*/ 421*5643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 422*5643Saalok size_t *dstlen, int level) 423*5643Saalok { 424*5643Saalok ASSERT(*dstlen >= srclen); 425*5643Saalok 426*5643Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 427*5643Saalok return (-1); 428*5643Saalok return (0); 429*5643Saalok } 430*5643Saalok 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * This is basically what strategy used to be before we found we 4330Sstevel@tonic-gate * needed task queues. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate static void 4360Sstevel@tonic-gate lofi_strategy_task(void *arg) 4370Sstevel@tonic-gate { 4380Sstevel@tonic-gate struct buf *bp = (struct buf *)arg; 4390Sstevel@tonic-gate int error; 4400Sstevel@tonic-gate struct lofi_state *lsp; 441*5643Saalok uint64_t sblkno, eblkno, cmpbytes; 442*5643Saalok offset_t offset, sblkoff, eblkoff; 443*5643Saalok u_offset_t salign, ealign; 444*5643Saalok u_offset_t sdiff; 445*5643Saalok uint32_t comp_data_sz; 446*5643Saalok caddr_t bufaddr; 447*5643Saalok unsigned char *compressed_seg = NULL, *cmpbuf; 448*5643Saalok unsigned char *uncompressed_seg = NULL; 449*5643Saalok lofi_compress_info_t *li; 450*5643Saalok size_t oblkcount, xfersize; 451*5643Saalok unsigned long seglen; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 4540Sstevel@tonic-gate if (lsp->ls_kstat) { 4550Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 4560Sstevel@tonic-gate kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 4570Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate bp_mapin(bp); 4600Sstevel@tonic-gate bufaddr = bp->b_un.b_addr; 4610Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * We used to always use vn_rdwr here, but we cannot do that because 4650Sstevel@tonic-gate * we might decide to read or write from the the underlying 4660Sstevel@tonic-gate * file during this call, which would be a deadlock because 4670Sstevel@tonic-gate * we have the rw_lock. So instead we page, unless it's not 4680Sstevel@tonic-gate * mapable or it's a character device. 4690Sstevel@tonic-gate */ 4704451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 4714451Seschrock error = EIO; 4724451Seschrock } else if (((lsp->ls_vp->v_flag & VNOMAP) == 0) && 4730Sstevel@tonic-gate (lsp->ls_vp->v_type != VCHR)) { 474*5643Saalok uint64_t i; 475*5643Saalok 4760Sstevel@tonic-gate /* 477*5643Saalok * Handle uncompressed files with a regular read 478*5643Saalok */ 479*5643Saalok if (lsp->ls_uncomp_seg_sz == 0) { 480*5643Saalok error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 481*5643Saalok goto done; 482*5643Saalok } 483*5643Saalok 484*5643Saalok /* 485*5643Saalok * From here on we're dealing primarily with compressed files 486*5643Saalok */ 487*5643Saalok 488*5643Saalok /* 489*5643Saalok * Compressed files can only be read from and 490*5643Saalok * not written to 4910Sstevel@tonic-gate */ 492*5643Saalok if (!(bp->b_flags & B_READ)) { 493*5643Saalok bp->b_resid = bp->b_bcount; 494*5643Saalok error = EROFS; 495*5643Saalok goto done; 496*5643Saalok } 497*5643Saalok 498*5643Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0); 499*5643Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 500*5643Saalok /* 501*5643Saalok * Compute starting and ending compressed segment numbers 502*5643Saalok * We use only bitwise operations avoiding division and 503*5643Saalok * modulus because we enforce the compression segment size 504*5643Saalok * to a power of 2 505*5643Saalok */ 506*5643Saalok sblkno = offset >> lsp->ls_comp_seg_shift; 507*5643Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 508*5643Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 509*5643Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 510*5643Saalok 511*5643Saalok /* 512*5643Saalok * Align start offset to block boundary for segmap 513*5643Saalok */ 514*5643Saalok salign = lsp->ls_comp_seg_index[sblkno]; 515*5643Saalok sdiff = salign & (DEV_BSIZE - 1); 516*5643Saalok salign -= sdiff; 517*5643Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 5180Sstevel@tonic-gate /* 519*5643Saalok * We're dealing with the last segment of 520*5643Saalok * the compressed file -- the size of this 521*5643Saalok * segment *may not* be the same as the 522*5643Saalok * segment size for the file 5230Sstevel@tonic-gate */ 524*5643Saalok eblkoff = (offset + bp->b_bcount) & 525*5643Saalok (lsp->ls_uncomp_last_seg_sz - 1); 526*5643Saalok ealign = lsp->ls_vp_comp_size; 527*5643Saalok } else { 528*5643Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1]; 529*5643Saalok } 530*5643Saalok 531*5643Saalok /* 532*5643Saalok * Preserve original request paramaters 533*5643Saalok */ 534*5643Saalok oblkcount = bp->b_bcount; 535*5643Saalok 536*5643Saalok /* 537*5643Saalok * Assign the calculated parameters 538*5643Saalok */ 539*5643Saalok comp_data_sz = ealign - salign; 540*5643Saalok bp->b_bcount = comp_data_sz; 541*5643Saalok 542*5643Saalok /* 543*5643Saalok * Allocate fixed size memory blocks to hold compressed 544*5643Saalok * segments and one uncompressed segment since we 545*5643Saalok * uncompress segments one at a time 546*5643Saalok */ 547*5643Saalok compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP); 548*5643Saalok uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP); 549*5643Saalok /* 550*5643Saalok * Map in the calculated number of blocks 551*5643Saalok */ 552*5643Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 553*5643Saalok bp, lsp); 554*5643Saalok 555*5643Saalok bp->b_bcount = oblkcount; 556*5643Saalok bp->b_resid = oblkcount; 557*5643Saalok if (error != 0) 558*5643Saalok goto done; 559*5643Saalok 560*5643Saalok /* 561*5643Saalok * We have the compressed blocks, now uncompress them 562*5643Saalok */ 563*5643Saalok cmpbuf = compressed_seg + sdiff; 564*5643Saalok for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz; 565*5643Saalok i++) { 566*5643Saalok /* 567*5643Saalok * Each of the segment index entries contains 568*5643Saalok * the starting block number for that segment. 569*5643Saalok * The number of compressed bytes in a segment 570*5643Saalok * is thus the difference between the starting 571*5643Saalok * block number of this segment and the starting 572*5643Saalok * block number of the next segment. 573*5643Saalok */ 574*5643Saalok if ((i == eblkno) && 575*5643Saalok (i == lsp->ls_comp_index_sz - 1)) { 576*5643Saalok cmpbytes = lsp->ls_vp_comp_size - 577*5643Saalok lsp->ls_comp_seg_index[i]; 5780Sstevel@tonic-gate } else { 579*5643Saalok cmpbytes = lsp->ls_comp_seg_index[i + 1] - 580*5643Saalok lsp->ls_comp_seg_index[i]; 5810Sstevel@tonic-gate } 582*5643Saalok 583*5643Saalok /* 584*5643Saalok * The first byte in a compressed segment is a flag 585*5643Saalok * that indicates whether this segment is compressed 586*5643Saalok * at all 587*5643Saalok */ 588*5643Saalok if (*cmpbuf == UNCOMPRESSED) { 589*5643Saalok bcopy((cmpbuf + SEGHDR), uncompressed_seg, 590*5643Saalok (cmpbytes - SEGHDR)); 591*5643Saalok } else { 592*5643Saalok seglen = lsp->ls_uncomp_seg_sz; 593*5643Saalok 594*5643Saalok if (li->l_decompress((cmpbuf + SEGHDR), 595*5643Saalok (cmpbytes - SEGHDR), uncompressed_seg, 596*5643Saalok &seglen, li->l_level) != 0) { 597*5643Saalok error = EIO; 598*5643Saalok goto done; 599*5643Saalok } 600*5643Saalok } 601*5643Saalok 602*5643Saalok /* 603*5643Saalok * Determine how much uncompressed data we 604*5643Saalok * have to copy and copy it 605*5643Saalok */ 606*5643Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 607*5643Saalok if (i == eblkno) { 608*5643Saalok if (i == (lsp->ls_comp_index_sz - 1)) 609*5643Saalok xfersize -= (lsp->ls_uncomp_last_seg_sz 610*5643Saalok - eblkoff); 611*5643Saalok else 612*5643Saalok xfersize -= 613*5643Saalok (lsp->ls_uncomp_seg_sz - eblkoff); 614*5643Saalok } 615*5643Saalok 616*5643Saalok bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize); 617*5643Saalok 618*5643Saalok cmpbuf += cmpbytes; 6190Sstevel@tonic-gate bufaddr += xfersize; 620*5643Saalok bp->b_resid -= xfersize; 621*5643Saalok sblkoff = 0; 622*5643Saalok 623*5643Saalok if (bp->b_resid == 0) 624*5643Saalok break; 625*5643Saalok } 6260Sstevel@tonic-gate } else { 6270Sstevel@tonic-gate ssize_t resid; 6280Sstevel@tonic-gate enum uio_rw rw; 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate if (bp->b_flags & B_READ) 6310Sstevel@tonic-gate rw = UIO_READ; 6320Sstevel@tonic-gate else 6330Sstevel@tonic-gate rw = UIO_WRITE; 6340Sstevel@tonic-gate error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount, 6350Sstevel@tonic-gate offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 6360Sstevel@tonic-gate bp->b_resid = resid; 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 639*5643Saalok done: 640*5643Saalok if (compressed_seg != NULL) 641*5643Saalok kmem_free(compressed_seg, comp_data_sz); 642*5643Saalok if (uncompressed_seg != NULL) 643*5643Saalok kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 644*5643Saalok 6450Sstevel@tonic-gate if (lsp->ls_kstat) { 6460Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid; 6470Sstevel@tonic-gate kstat_io_t *kioptr; 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 6500Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 6510Sstevel@tonic-gate if (bp->b_flags & B_READ) { 6520Sstevel@tonic-gate kioptr->nread += n_done; 6530Sstevel@tonic-gate kioptr->reads++; 6540Sstevel@tonic-gate } else { 6550Sstevel@tonic-gate kioptr->nwritten += n_done; 6560Sstevel@tonic-gate kioptr->writes++; 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate kstat_runq_exit(kioptr); 6590Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 6600Sstevel@tonic-gate } 6614451Seschrock 6624451Seschrock mutex_enter(&lsp->ls_vp_lock); 6634451Seschrock if (--lsp->ls_vp_iocount == 0) 6644451Seschrock cv_broadcast(&lsp->ls_vp_cv); 6654451Seschrock mutex_exit(&lsp->ls_vp_lock); 6664451Seschrock 6670Sstevel@tonic-gate bioerror(bp, error); 6680Sstevel@tonic-gate biodone(bp); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate static int 6720Sstevel@tonic-gate lofi_strategy(struct buf *bp) 6730Sstevel@tonic-gate { 6740Sstevel@tonic-gate struct lofi_state *lsp; 6750Sstevel@tonic-gate offset_t offset; 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* 6780Sstevel@tonic-gate * We cannot just do I/O here, because the current thread 6790Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem 6800Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and 6810Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer. 6820Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below. 6830Sstevel@tonic-gate * 6840Sstevel@tonic-gate * We could come up with a complex strategy using threads to 6850Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task 6860Sstevel@tonic-gate * queues were incredibly easy so they win. 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 6894451Seschrock mutex_enter(&lsp->ls_vp_lock); 6904451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 6914451Seschrock bioerror(bp, EIO); 6924451Seschrock biodone(bp); 6934451Seschrock mutex_exit(&lsp->ls_vp_lock); 6944451Seschrock return (0); 6954451Seschrock } 6964451Seschrock 6970Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 6980Sstevel@tonic-gate if (offset == lsp->ls_vp_size) { 6990Sstevel@tonic-gate /* EOF */ 7000Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) { 7010Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 7020Sstevel@tonic-gate bioerror(bp, 0); 7030Sstevel@tonic-gate } else { 7040Sstevel@tonic-gate /* writes should fail */ 7050Sstevel@tonic-gate bioerror(bp, ENXIO); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate biodone(bp); 7084451Seschrock mutex_exit(&lsp->ls_vp_lock); 7090Sstevel@tonic-gate return (0); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate if (offset > lsp->ls_vp_size) { 7120Sstevel@tonic-gate bioerror(bp, ENXIO); 7130Sstevel@tonic-gate biodone(bp); 7144451Seschrock mutex_exit(&lsp->ls_vp_lock); 7150Sstevel@tonic-gate return (0); 7160Sstevel@tonic-gate } 7174451Seschrock lsp->ls_vp_iocount++; 7184451Seschrock mutex_exit(&lsp->ls_vp_lock); 7194451Seschrock 7200Sstevel@tonic-gate if (lsp->ls_kstat) { 7210Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 7220Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 7230Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 7260Sstevel@tonic-gate return (0); 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate /*ARGSUSED2*/ 7300Sstevel@tonic-gate static int 7310Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 7320Sstevel@tonic-gate { 7330Sstevel@tonic-gate if (getminor(dev) == 0) 7340Sstevel@tonic-gate return (EINVAL); 7350Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate /*ARGSUSED2*/ 7390Sstevel@tonic-gate static int 7400Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 7410Sstevel@tonic-gate { 7420Sstevel@tonic-gate if (getminor(dev) == 0) 7430Sstevel@tonic-gate return (EINVAL); 7440Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate /*ARGSUSED2*/ 7480Sstevel@tonic-gate static int 7490Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 7500Sstevel@tonic-gate { 7510Sstevel@tonic-gate if (getminor(dev) == 0) 7520Sstevel@tonic-gate return (EINVAL); 7530Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate /*ARGSUSED2*/ 7570Sstevel@tonic-gate static int 7580Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 7590Sstevel@tonic-gate { 7600Sstevel@tonic-gate if (getminor(dev) == 0) 7610Sstevel@tonic-gate return (EINVAL); 7620Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /*ARGSUSED*/ 7660Sstevel@tonic-gate static int 7670Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 7680Sstevel@tonic-gate { 7690Sstevel@tonic-gate switch (infocmd) { 7700Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 7710Sstevel@tonic-gate *result = lofi_dip; 7720Sstevel@tonic-gate return (DDI_SUCCESS); 7730Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 7740Sstevel@tonic-gate *result = 0; 7750Sstevel@tonic-gate return (DDI_SUCCESS); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate return (DDI_FAILURE); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate static int 7810Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 7820Sstevel@tonic-gate { 7830Sstevel@tonic-gate int error; 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate if (cmd != DDI_ATTACH) 7860Sstevel@tonic-gate return (DDI_FAILURE); 7870Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0); 7880Sstevel@tonic-gate if (error == DDI_FAILURE) { 7890Sstevel@tonic-gate return (DDI_FAILURE); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 7920Sstevel@tonic-gate DDI_PSEUDO, NULL); 7930Sstevel@tonic-gate if (error == DDI_FAILURE) { 7940Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 7950Sstevel@tonic-gate return (DDI_FAILURE); 7960Sstevel@tonic-gate } 7975084Sjohnlev /* driver handles kernel-issued IOCTLs */ 7985084Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 7995084Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 8005084Sjohnlev ddi_remove_minor_node(dip, NULL); 8015084Sjohnlev ddi_soft_state_free(lofi_statep, 0); 8025084Sjohnlev return (DDI_FAILURE); 8035084Sjohnlev } 8040Sstevel@tonic-gate lofi_dip = dip; 8050Sstevel@tonic-gate ddi_report_dev(dip); 8060Sstevel@tonic-gate return (DDI_SUCCESS); 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate static int 8100Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 8110Sstevel@tonic-gate { 8120Sstevel@tonic-gate if (cmd != DDI_DETACH) 8130Sstevel@tonic-gate return (DDI_FAILURE); 8140Sstevel@tonic-gate if (lofi_busy()) 8150Sstevel@tonic-gate return (DDI_FAILURE); 8160Sstevel@tonic-gate lofi_dip = NULL; 8170Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 8185084Sjohnlev ddi_prop_remove_all(dip); 8190Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 8200Sstevel@tonic-gate return (DDI_SUCCESS); 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate /* 8240Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out 8250Sstevel@tonic-gate * the lofi_ioctl structure. 8260Sstevel@tonic-gate */ 8270Sstevel@tonic-gate struct lofi_ioctl * 8281657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag) 8290Sstevel@tonic-gate { 8300Sstevel@tonic-gate struct lofi_ioctl *klip; 8310Sstevel@tonic-gate int error; 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 8341657Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 8350Sstevel@tonic-gate if (error) { 8360Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8370Sstevel@tonic-gate return (NULL); 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate /* make sure filename is always null-terminated */ 8410Sstevel@tonic-gate klip->li_filename[MAXPATHLEN] = '\0'; 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /* validate minor number */ 8440Sstevel@tonic-gate if (klip->li_minor > lofi_max_files) { 8450Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8460Sstevel@tonic-gate return (NULL); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate return (klip); 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate int 8521657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 8531657Sheppo int flag) 8540Sstevel@tonic-gate { 8550Sstevel@tonic-gate int error; 8560Sstevel@tonic-gate 8571657Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 8580Sstevel@tonic-gate if (error) 8590Sstevel@tonic-gate return (EFAULT); 8600Sstevel@tonic-gate return (0); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate void 8640Sstevel@tonic-gate free_lofi_ioctl(struct lofi_ioctl *klip) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate /* 8700Sstevel@tonic-gate * Return the minor number 'filename' is mapped to, if it is. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate static int 8730Sstevel@tonic-gate file_to_minor(char *filename) 8740Sstevel@tonic-gate { 8750Sstevel@tonic-gate minor_t minor; 8760Sstevel@tonic-gate struct lofi_state *lsp; 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 8790Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 8800Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 8810Sstevel@tonic-gate if (lsp == NULL) 8820Sstevel@tonic-gate continue; 8830Sstevel@tonic-gate if (strcmp(lsp->ls_filename, filename) == 0) 8840Sstevel@tonic-gate return (minor); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate return (0); 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate /* 8900Sstevel@tonic-gate * lofiadm does some validation, but since Joe Random (or crashme) could 8910Sstevel@tonic-gate * do our ioctls, we need to do some validation too. 8920Sstevel@tonic-gate */ 8930Sstevel@tonic-gate static int 8940Sstevel@tonic-gate valid_filename(const char *filename) 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/"; 8970Sstevel@tonic-gate static char *charprefix = "/dev/" LOFI_CHAR_NAME "/"; 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate /* must be absolute path */ 9000Sstevel@tonic-gate if (filename[0] != '/') 9010Sstevel@tonic-gate return (0); 9020Sstevel@tonic-gate /* must not be lofi */ 9030Sstevel@tonic-gate if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0) 9040Sstevel@tonic-gate return (0); 9050Sstevel@tonic-gate if (strncmp(filename, charprefix, strlen(charprefix)) == 0) 9060Sstevel@tonic-gate return (0); 9070Sstevel@tonic-gate return (1); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate /* 9110Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size 9120Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device, 9130Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and 9140Sstevel@tonic-gate * partition information. It uses that information to determine the parameters 9153517Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 9160Sstevel@tonic-gate * have to support it. 9170Sstevel@tonic-gate */ 9180Sstevel@tonic-gate static void 9190Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp) 9200Sstevel@tonic-gate { 9210Sstevel@tonic-gate /* dk_geom - see dkio(7I) */ 9220Sstevel@tonic-gate /* 9230Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 9240Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to 9250Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create 9260Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick 9270Sstevel@tonic-gate * a number between one and one). 9280Sstevel@tonic-gate * 9290Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders 9300Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space 9310Sstevel@tonic-gate * for backup superblocks or cylinder group structures. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */ 9340Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024); 9350Sstevel@tonic-gate else 9360Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024); 9370Sstevel@tonic-gate /* in case file file is < 100k */ 9380Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0) 9390Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1; 9400Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0; 9410Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0; 9420Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1; 9430Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0; 9440Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0; 9450Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0; 9460Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0; 9470Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0; 9480Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200; 9490Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 9500Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size / 9510Sstevel@tonic-gate (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 9520Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0; 9530Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0; 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate /* vtoc - see dkio(7I) */ 9560Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 9570Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE; 9580Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION; 9590Sstevel@tonic-gate bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7); 9600Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 9610Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1; 9620Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 963*5643Saalok 964*5643Saalok /* 965*5643Saalok * A compressed file is read-only, other files can 966*5643Saalok * be read-write 967*5643Saalok */ 968*5643Saalok if (lsp->ls_uncomp_seg_sz > 0) { 969*5643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 970*5643Saalok } else { 971*5643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 972*5643Saalok } 9730Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 9740Sstevel@tonic-gate /* 9750Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because 9760Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case, 9770Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size 9780Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder. 9790Sstevel@tonic-gate */ 9800Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 9810Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */ 9840Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 9850Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 9860Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD; 9870Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0; 9880Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0; 9890Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0; 9900Sstevel@tonic-gate lsp->ls_ci.dki_space = 0; 9910Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0; 9920Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0; 9930Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 9940Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0; 9950Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0; 9960Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0; 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it 9990Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides 10000Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because 10010Sstevel@tonic-gate * maxcontig is 0. 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16; 10040Sstevel@tonic-gate } 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate /* 1007*5643Saalok * map in a compressed file 1008*5643Saalok * 1009*5643Saalok * Read in the header and the index that follows. 1010*5643Saalok * 1011*5643Saalok * The header is as follows - 1012*5643Saalok * 1013*5643Saalok * Signature (name of the compression algorithm) 1014*5643Saalok * Compression segment size (a multiple of 512) 1015*5643Saalok * Number of index entries 1016*5643Saalok * Size of the last block 1017*5643Saalok * The array containing the index entries 1018*5643Saalok * 1019*5643Saalok * The header information is always stored in 1020*5643Saalok * network byte order on disk. 1021*5643Saalok */ 1022*5643Saalok static int 1023*5643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 1024*5643Saalok { 1025*5643Saalok uint32_t index_sz, header_len, i; 1026*5643Saalok ssize_t resid; 1027*5643Saalok enum uio_rw rw; 1028*5643Saalok char *tbuf = buf; 1029*5643Saalok int error; 1030*5643Saalok 1031*5643Saalok /* The signature has already been read */ 1032*5643Saalok tbuf += sizeof (lsp->ls_comp_algorithm); 1033*5643Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 1034*5643Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 1035*5643Saalok 1036*5643Saalok /* 1037*5643Saalok * The compressed segment size must be a power of 2 1038*5643Saalok */ 1039*5643Saalok if (lsp->ls_uncomp_seg_sz % 2) 1040*5643Saalok return (EINVAL); 1041*5643Saalok 1042*5643Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 1043*5643Saalok ; 1044*5643Saalok 1045*5643Saalok lsp->ls_comp_seg_shift = i; 1046*5643Saalok 1047*5643Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz); 1048*5643Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 1049*5643Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 1050*5643Saalok 1051*5643Saalok tbuf += sizeof (lsp->ls_comp_index_sz); 1052*5643Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 1053*5643Saalok sizeof (lsp->ls_uncomp_last_seg_sz)); 1054*5643Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 1055*5643Saalok 1056*5643Saalok /* 1057*5643Saalok * Compute the total size of the uncompressed data 1058*5643Saalok * for use in fake_disk_geometry and other calculations. 1059*5643Saalok * Disk geometry has to be faked with respect to the 1060*5643Saalok * actual uncompressed data size rather than the 1061*5643Saalok * compressed file size. 1062*5643Saalok */ 1063*5643Saalok lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 1064*5643Saalok + lsp->ls_uncomp_last_seg_sz; 1065*5643Saalok 1066*5643Saalok /* 1067*5643Saalok * Index size is rounded up to a 512 byte boundary for ease 1068*5643Saalok * of segmapping 1069*5643Saalok */ 1070*5643Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 1071*5643Saalok header_len = sizeof (lsp->ls_comp_algorithm) + 1072*5643Saalok sizeof (lsp->ls_uncomp_seg_sz) + 1073*5643Saalok sizeof (lsp->ls_comp_index_sz) + 1074*5643Saalok sizeof (lsp->ls_uncomp_last_seg_sz); 1075*5643Saalok lsp->ls_comp_offbase = header_len + index_sz; 1076*5643Saalok 1077*5643Saalok index_sz += header_len; 1078*5643Saalok index_sz = roundup(index_sz, DEV_BSIZE); 1079*5643Saalok 1080*5643Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 1081*5643Saalok lsp->ls_comp_index_data_sz = index_sz; 1082*5643Saalok 1083*5643Saalok /* 1084*5643Saalok * Read in the index -- this has a side-effect 1085*5643Saalok * of reading in the header as well 1086*5643Saalok */ 1087*5643Saalok rw = UIO_READ; 1088*5643Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 1089*5643Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 1090*5643Saalok 1091*5643Saalok if (error != 0) 1092*5643Saalok return (error); 1093*5643Saalok 1094*5643Saalok /* Skip the header, this is where the index really begins */ 1095*5643Saalok lsp->ls_comp_seg_index = 1096*5643Saalok /*LINTED*/ 1097*5643Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len); 1098*5643Saalok 1099*5643Saalok /* 1100*5643Saalok * Now recompute offsets in the index to account for 1101*5643Saalok * the header length 1102*5643Saalok */ 1103*5643Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) { 1104*5643Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 1105*5643Saalok BE_64(lsp->ls_comp_seg_index[i]); 1106*5643Saalok } 1107*5643Saalok 1108*5643Saalok return (error); 1109*5643Saalok } 1110*5643Saalok 1111*5643Saalok /* 1112*5643Saalok * Check to see if the passed in signature is a valid 1113*5643Saalok * one. If it is valid, return the index into 1114*5643Saalok * lofi_compress_table. 1115*5643Saalok * 1116*5643Saalok * Return -1 if it is invalid 1117*5643Saalok */ 1118*5643Saalok static int lofi_compress_select(char *signature) 1119*5643Saalok { 1120*5643Saalok int i; 1121*5643Saalok 1122*5643Saalok for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 1123*5643Saalok if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 1124*5643Saalok return (i); 1125*5643Saalok } 1126*5643Saalok 1127*5643Saalok return (-1); 1128*5643Saalok } 1129*5643Saalok 1130*5643Saalok /* 11310Sstevel@tonic-gate * map a file to a minor number. Return the minor number. 11320Sstevel@tonic-gate */ 11330Sstevel@tonic-gate static int 11340Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 11351657Sheppo int *rvalp, struct cred *credp, int ioctl_flag) 11360Sstevel@tonic-gate { 11370Sstevel@tonic-gate minor_t newminor; 11380Sstevel@tonic-gate struct lofi_state *lsp; 11390Sstevel@tonic-gate struct lofi_ioctl *klip; 11400Sstevel@tonic-gate int error; 11410Sstevel@tonic-gate struct vnode *vp; 11420Sstevel@tonic-gate int64_t Nblocks_prop_val; 11430Sstevel@tonic-gate int64_t Size_prop_val; 1144*5643Saalok int compress_index; 11450Sstevel@tonic-gate vattr_t vattr; 11460Sstevel@tonic-gate int flag; 11470Sstevel@tonic-gate enum vtype v_type; 11484451Seschrock int zalloced = 0; 11490Sstevel@tonic-gate dev_t newdev; 11504451Seschrock char namebuf[50]; 1151*5643Saalok char buf[DEV_BSIZE]; 1152*5643Saalok char *tbuf; 1153*5643Saalok ssize_t resid; 1154*5643Saalok enum uio_rw rw; 11550Sstevel@tonic-gate 11561657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 11570Sstevel@tonic-gate if (klip == NULL) 11580Sstevel@tonic-gate return (EFAULT); 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate mutex_enter(&lofi_lock); 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate if (!valid_filename(klip->li_filename)) { 11630Sstevel@tonic-gate error = EINVAL; 11640Sstevel@tonic-gate goto out; 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate if (file_to_minor(klip->li_filename) != 0) { 11680Sstevel@tonic-gate error = EBUSY; 11690Sstevel@tonic-gate goto out; 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate if (pickminor) { 11730Sstevel@tonic-gate /* Find a free one */ 11740Sstevel@tonic-gate for (newminor = 1; newminor <= lofi_max_files; newminor++) 11750Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) == NULL) 11760Sstevel@tonic-gate break; 11770Sstevel@tonic-gate if (newminor >= lofi_max_files) { 11780Sstevel@tonic-gate error = EAGAIN; 11790Sstevel@tonic-gate goto out; 11800Sstevel@tonic-gate } 11810Sstevel@tonic-gate } else { 11820Sstevel@tonic-gate newminor = klip->li_minor; 11830Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) != NULL) { 11840Sstevel@tonic-gate error = EEXIST; 11850Sstevel@tonic-gate goto out; 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate /* make sure it's valid */ 11900Sstevel@tonic-gate error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW, 11910Sstevel@tonic-gate NULLVPP, &vp); 11920Sstevel@tonic-gate if (error) { 11930Sstevel@tonic-gate goto out; 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate v_type = vp->v_type; 11960Sstevel@tonic-gate VN_RELE(vp); 11970Sstevel@tonic-gate if (!V_ISLOFIABLE(v_type)) { 11980Sstevel@tonic-gate error = EINVAL; 11990Sstevel@tonic-gate goto out; 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate flag = FREAD | FWRITE | FOFFMAX | FEXCL; 12020Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 12030Sstevel@tonic-gate if (error) { 12040Sstevel@tonic-gate /* try read-only */ 12050Sstevel@tonic-gate flag &= ~FWRITE; 12060Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 12070Sstevel@tonic-gate &vp, 0, 0); 12080Sstevel@tonic-gate if (error) { 12090Sstevel@tonic-gate goto out; 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 12135331Samw error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 12140Sstevel@tonic-gate if (error) { 12150Sstevel@tonic-gate goto closeout; 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate /* the file needs to be a multiple of the block size */ 12180Sstevel@tonic-gate if ((vattr.va_size % DEV_BSIZE) != 0) { 12190Sstevel@tonic-gate error = EINVAL; 12200Sstevel@tonic-gate goto closeout; 12210Sstevel@tonic-gate } 12220Sstevel@tonic-gate newdev = makedevice(getmajor(dev), newminor); 12230Sstevel@tonic-gate Size_prop_val = vattr.va_size; 12240Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 12250Sstevel@tonic-gate SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) { 12260Sstevel@tonic-gate error = EINVAL; 12270Sstevel@tonic-gate goto closeout; 12280Sstevel@tonic-gate } 12290Sstevel@tonic-gate Nblocks_prop_val = vattr.va_size / DEV_BSIZE; 12300Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 12310Sstevel@tonic-gate NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 12320Sstevel@tonic-gate error = EINVAL; 12330Sstevel@tonic-gate goto propout; 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, newminor); 12360Sstevel@tonic-gate if (error == DDI_FAILURE) { 12370Sstevel@tonic-gate error = ENOMEM; 12380Sstevel@tonic-gate goto propout; 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate zalloced = 1; 12410Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 12420Sstevel@tonic-gate (void) ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor, 12430Sstevel@tonic-gate DDI_PSEUDO, NULL); 12440Sstevel@tonic-gate if (error != DDI_SUCCESS) { 12450Sstevel@tonic-gate error = ENXIO; 12460Sstevel@tonic-gate goto propout; 12470Sstevel@tonic-gate } 12480Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor); 12490Sstevel@tonic-gate error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor, 12500Sstevel@tonic-gate DDI_PSEUDO, NULL); 12510Sstevel@tonic-gate if (error != DDI_SUCCESS) { 12520Sstevel@tonic-gate /* remove block node */ 12530Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 12540Sstevel@tonic-gate ddi_remove_minor_node(lofi_dip, namebuf); 12550Sstevel@tonic-gate error = ENXIO; 12560Sstevel@tonic-gate goto propout; 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, newminor); 12590Sstevel@tonic-gate lsp->ls_filename_sz = strlen(klip->li_filename) + 1; 12600Sstevel@tonic-gate lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP); 12610Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 12620Sstevel@tonic-gate LOFI_DRIVER_NAME, newminor); 12630Sstevel@tonic-gate lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads, 12640Sstevel@tonic-gate minclsyspri, 1, lofi_taskq_maxalloc, 0); 12650Sstevel@tonic-gate lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor, 12660Sstevel@tonic-gate NULL, "disk", KSTAT_TYPE_IO, 1, 0); 12670Sstevel@tonic-gate if (lsp->ls_kstat) { 12680Sstevel@tonic-gate mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 12690Sstevel@tonic-gate lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 12700Sstevel@tonic-gate kstat_install(lsp->ls_kstat); 12710Sstevel@tonic-gate } 12724451Seschrock cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 12734451Seschrock mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 12744451Seschrock 12750Sstevel@tonic-gate /* 12760Sstevel@tonic-gate * save open mode so file can be closed properly and vnode counts 12770Sstevel@tonic-gate * updated correctly. 12780Sstevel@tonic-gate */ 12790Sstevel@tonic-gate lsp->ls_openflag = flag; 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate /* 12820Sstevel@tonic-gate * Try to handle stacked lofs vnodes. 12830Sstevel@tonic-gate */ 12840Sstevel@tonic-gate if (vp->v_type == VREG) { 12855331Samw if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) { 12860Sstevel@tonic-gate lsp->ls_vp = vp; 12870Sstevel@tonic-gate } else { 12880Sstevel@tonic-gate /* 12890Sstevel@tonic-gate * Even though vp was obtained via vn_open(), we 12900Sstevel@tonic-gate * can't call vn_close() on it, since lofs will 12910Sstevel@tonic-gate * pass the VOP_CLOSE() on down to the realvp 12920Sstevel@tonic-gate * (which we are about to use). Hence we merely 12930Sstevel@tonic-gate * drop the reference to the lofs vnode and hold 12940Sstevel@tonic-gate * the realvp so things behave as if we've 12950Sstevel@tonic-gate * opened the realvp without any interaction 12960Sstevel@tonic-gate * with lofs. 12970Sstevel@tonic-gate */ 12980Sstevel@tonic-gate VN_HOLD(lsp->ls_vp); 12990Sstevel@tonic-gate VN_RELE(vp); 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate } else { 13020Sstevel@tonic-gate lsp->ls_vp = vp; 13030Sstevel@tonic-gate } 13040Sstevel@tonic-gate lsp->ls_vp_size = vattr.va_size; 13050Sstevel@tonic-gate (void) strcpy(lsp->ls_filename, klip->li_filename); 13060Sstevel@tonic-gate if (rvalp) 13070Sstevel@tonic-gate *rvalp = (int)newminor; 13080Sstevel@tonic-gate klip->li_minor = newminor; 13090Sstevel@tonic-gate 1310*5643Saalok /* 1311*5643Saalok * Read the file signature to check if it is compressed. 1312*5643Saalok * 'rw' is set to read since only reads are allowed to 1313*5643Saalok * a compressed file. 1314*5643Saalok */ 1315*5643Saalok rw = UIO_READ; 1316*5643Saalok error = vn_rdwr(rw, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 1317*5643Saalok 0, RLIM64_INFINITY, kcred, &resid); 1318*5643Saalok 1319*5643Saalok if (error != 0) 1320*5643Saalok goto propout; 1321*5643Saalok 1322*5643Saalok tbuf = buf; 1323*5643Saalok lsp->ls_uncomp_seg_sz = 0; 1324*5643Saalok lsp->ls_vp_comp_size = lsp->ls_vp_size; 1325*5643Saalok lsp->ls_comp_algorithm[0] = '\0'; 1326*5643Saalok 1327*5643Saalok compress_index = lofi_compress_select(tbuf); 1328*5643Saalok if (compress_index != -1) { 1329*5643Saalok lsp->ls_comp_algorithm_index = compress_index; 1330*5643Saalok (void) strlcpy(lsp->ls_comp_algorithm, 1331*5643Saalok lofi_compress_table[compress_index].l_name, 1332*5643Saalok sizeof (lsp->ls_comp_algorithm)); 1333*5643Saalok error = lofi_map_compressed_file(lsp, buf); 1334*5643Saalok if (error != 0) 1335*5643Saalok goto propout; 1336*5643Saalok 1337*5643Saalok /* update DDI properties */ 1338*5643Saalok Size_prop_val = lsp->ls_vp_size; 1339*5643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 1340*5643Saalok Size_prop_val)) != DDI_PROP_SUCCESS) { 1341*5643Saalok error = EINVAL; 1342*5643Saalok goto propout; 1343*5643Saalok } 1344*5643Saalok 1345*5643Saalok Nblocks_prop_val = lsp->ls_vp_size / DEV_BSIZE; 1346*5643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 1347*5643Saalok Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 1348*5643Saalok error = EINVAL; 1349*5643Saalok goto propout; 1350*5643Saalok } 1351*5643Saalok } 1352*5643Saalok 13530Sstevel@tonic-gate fake_disk_geometry(lsp); 13540Sstevel@tonic-gate mutex_exit(&lofi_lock); 13551657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 13560Sstevel@tonic-gate free_lofi_ioctl(klip); 13570Sstevel@tonic-gate return (0); 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate propout: 13600Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 13610Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 13620Sstevel@tonic-gate closeout: 13635331Samw (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 13640Sstevel@tonic-gate VN_RELE(vp); 13650Sstevel@tonic-gate out: 13660Sstevel@tonic-gate if (zalloced) 13670Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, newminor); 13680Sstevel@tonic-gate mutex_exit(&lofi_lock); 13690Sstevel@tonic-gate free_lofi_ioctl(klip); 13700Sstevel@tonic-gate return (error); 13710Sstevel@tonic-gate } 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate /* 13740Sstevel@tonic-gate * unmap a file. 13750Sstevel@tonic-gate */ 13760Sstevel@tonic-gate static int 13770Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 13781657Sheppo struct cred *credp, int ioctl_flag) 13790Sstevel@tonic-gate { 13800Sstevel@tonic-gate struct lofi_state *lsp; 13810Sstevel@tonic-gate struct lofi_ioctl *klip; 13820Sstevel@tonic-gate minor_t minor; 13830Sstevel@tonic-gate 13841657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 13850Sstevel@tonic-gate if (klip == NULL) 13860Sstevel@tonic-gate return (EFAULT); 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate mutex_enter(&lofi_lock); 13890Sstevel@tonic-gate if (byfilename) { 13900Sstevel@tonic-gate minor = file_to_minor(klip->li_filename); 13910Sstevel@tonic-gate } else { 13920Sstevel@tonic-gate minor = klip->li_minor; 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate if (minor == 0) { 13950Sstevel@tonic-gate mutex_exit(&lofi_lock); 13960Sstevel@tonic-gate free_lofi_ioctl(klip); 13970Sstevel@tonic-gate return (ENXIO); 13980Sstevel@tonic-gate } 13990Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 14004451Seschrock if (lsp == NULL || lsp->ls_vp == NULL) { 14010Sstevel@tonic-gate mutex_exit(&lofi_lock); 14020Sstevel@tonic-gate free_lofi_ioctl(klip); 14030Sstevel@tonic-gate return (ENXIO); 14040Sstevel@tonic-gate } 14054451Seschrock 14060Sstevel@tonic-gate if (is_opened(lsp)) { 14074451Seschrock /* 14084451Seschrock * If the 'force' flag is set, then we forcibly close the 14094451Seschrock * underlying file. Subsequent operations will fail, and the 14104451Seschrock * DKIOCSTATE ioctl will return DKIO_DEV_GONE. When the device 14114451Seschrock * is last closed, the device will be cleaned up appropriately. 14124451Seschrock * 14134451Seschrock * This is complicated by the fact that we may have outstanding 14144451Seschrock * dispatched I/Os. Rather than having a single mutex to 14154451Seschrock * serialize all I/O, we keep a count of the number of 14164451Seschrock * outstanding I/O requests, as well as a flag to indicate that 14174451Seschrock * no new I/Os should be dispatched. We set the flag, wait for 14184451Seschrock * the number of outstanding I/Os to reach 0, and then close the 14194451Seschrock * underlying vnode. 14204451Seschrock */ 14214451Seschrock if (klip->li_force) { 14224451Seschrock mutex_enter(&lsp->ls_vp_lock); 14234451Seschrock lsp->ls_vp_closereq = B_TRUE; 14244451Seschrock while (lsp->ls_vp_iocount > 0) 14254451Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 14264451Seschrock (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, 14275331Samw credp, NULL); 14284451Seschrock VN_RELE(lsp->ls_vp); 14294451Seschrock lsp->ls_vp = NULL; 14304451Seschrock cv_broadcast(&lsp->ls_vp_cv); 14314451Seschrock mutex_exit(&lsp->ls_vp_lock); 14324451Seschrock mutex_exit(&lofi_lock); 14334451Seschrock klip->li_minor = minor; 14344451Seschrock (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14354451Seschrock free_lofi_ioctl(klip); 14364451Seschrock return (0); 14374451Seschrock } 14380Sstevel@tonic-gate mutex_exit(&lofi_lock); 14390Sstevel@tonic-gate free_lofi_ioctl(klip); 14400Sstevel@tonic-gate return (EBUSY); 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate 1443*5643Saalok if (lsp->ls_uncomp_seg_sz > 0) { 1444*5643Saalok kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 1445*5643Saalok lsp->ls_uncomp_seg_sz = 0; 1446*5643Saalok } 1447*5643Saalok 14484451Seschrock lofi_free_handle(dev, minor, lsp, credp); 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate klip->li_minor = minor; 14510Sstevel@tonic-gate mutex_exit(&lofi_lock); 14521657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14530Sstevel@tonic-gate free_lofi_ioctl(klip); 14540Sstevel@tonic-gate return (0); 14550Sstevel@tonic-gate } 14560Sstevel@tonic-gate 14570Sstevel@tonic-gate /* 14580Sstevel@tonic-gate * get the filename given the minor number, or the minor number given 14590Sstevel@tonic-gate * the name. 14600Sstevel@tonic-gate */ 14614451Seschrock /*ARGSUSED*/ 14620Sstevel@tonic-gate static int 14630Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 14641657Sheppo struct cred *credp, int ioctl_flag) 14650Sstevel@tonic-gate { 14660Sstevel@tonic-gate struct lofi_state *lsp; 14670Sstevel@tonic-gate struct lofi_ioctl *klip; 14680Sstevel@tonic-gate int error; 14690Sstevel@tonic-gate minor_t minor; 14700Sstevel@tonic-gate 14711657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 14720Sstevel@tonic-gate if (klip == NULL) 14730Sstevel@tonic-gate return (EFAULT); 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate switch (which) { 14760Sstevel@tonic-gate case LOFI_GET_FILENAME: 14770Sstevel@tonic-gate minor = klip->li_minor; 14780Sstevel@tonic-gate if (minor == 0) { 14790Sstevel@tonic-gate free_lofi_ioctl(klip); 14800Sstevel@tonic-gate return (EINVAL); 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate mutex_enter(&lofi_lock); 14840Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 14850Sstevel@tonic-gate if (lsp == NULL) { 14860Sstevel@tonic-gate mutex_exit(&lofi_lock); 14870Sstevel@tonic-gate free_lofi_ioctl(klip); 14880Sstevel@tonic-gate return (ENXIO); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate (void) strcpy(klip->li_filename, lsp->ls_filename); 1491*5643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 1492*5643Saalok sizeof (klip->li_algorithm)); 14930Sstevel@tonic-gate mutex_exit(&lofi_lock); 14941657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14950Sstevel@tonic-gate free_lofi_ioctl(klip); 14960Sstevel@tonic-gate return (error); 14970Sstevel@tonic-gate case LOFI_GET_MINOR: 14980Sstevel@tonic-gate mutex_enter(&lofi_lock); 14990Sstevel@tonic-gate klip->li_minor = file_to_minor(klip->li_filename); 15000Sstevel@tonic-gate mutex_exit(&lofi_lock); 15010Sstevel@tonic-gate if (klip->li_minor == 0) { 15020Sstevel@tonic-gate free_lofi_ioctl(klip); 15030Sstevel@tonic-gate return (ENOENT); 15040Sstevel@tonic-gate } 15051657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15060Sstevel@tonic-gate free_lofi_ioctl(klip); 15070Sstevel@tonic-gate return (error); 1508*5643Saalok case LOFI_CHECK_COMPRESSED: 1509*5643Saalok mutex_enter(&lofi_lock); 1510*5643Saalok klip->li_minor = file_to_minor(klip->li_filename); 1511*5643Saalok mutex_exit(&lofi_lock); 1512*5643Saalok if (klip->li_minor == 0) { 1513*5643Saalok free_lofi_ioctl(klip); 1514*5643Saalok return (ENOENT); 1515*5643Saalok } 1516*5643Saalok mutex_enter(&lofi_lock); 1517*5643Saalok lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 1518*5643Saalok if (lsp == NULL) { 1519*5643Saalok mutex_exit(&lofi_lock); 1520*5643Saalok free_lofi_ioctl(klip); 1521*5643Saalok return (ENXIO); 1522*5643Saalok } 1523*5643Saalok ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0); 1524*5643Saalok 1525*5643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 1526*5643Saalok sizeof (klip->li_algorithm)); 1527*5643Saalok mutex_exit(&lofi_lock); 1528*5643Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 1529*5643Saalok free_lofi_ioctl(klip); 1530*5643Saalok return (error); 15310Sstevel@tonic-gate default: 15320Sstevel@tonic-gate free_lofi_ioctl(klip); 15330Sstevel@tonic-gate return (EINVAL); 15340Sstevel@tonic-gate } 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate static int 15390Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 15400Sstevel@tonic-gate int *rvalp) 15410Sstevel@tonic-gate { 15420Sstevel@tonic-gate int error; 15430Sstevel@tonic-gate enum dkio_state dkstate; 15440Sstevel@tonic-gate struct lofi_state *lsp; 15450Sstevel@tonic-gate minor_t minor; 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate #ifdef lint 15480Sstevel@tonic-gate credp = credp; 15490Sstevel@tonic-gate #endif 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate minor = getminor(dev); 15520Sstevel@tonic-gate /* lofi ioctls only apply to the master device */ 15530Sstevel@tonic-gate if (minor == 0) { 15540Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate /* 15570Sstevel@tonic-gate * the query command only need read-access - i.e., normal 15580Sstevel@tonic-gate * users are allowed to do those on the ctl device as 15590Sstevel@tonic-gate * long as they can open it read-only. 15600Sstevel@tonic-gate */ 15610Sstevel@tonic-gate switch (cmd) { 15620Sstevel@tonic-gate case LOFI_MAP_FILE: 15630Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15640Sstevel@tonic-gate return (EPERM); 15651657Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 15660Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR: 15670Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15680Sstevel@tonic-gate return (EPERM); 15691657Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 15700Sstevel@tonic-gate case LOFI_UNMAP_FILE: 15710Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15720Sstevel@tonic-gate return (EPERM); 15731657Sheppo return (lofi_unmap_file(dev, lip, 1, credp, flag)); 15740Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR: 15750Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15760Sstevel@tonic-gate return (EPERM); 15771657Sheppo return (lofi_unmap_file(dev, lip, 0, credp, flag)); 15780Sstevel@tonic-gate case LOFI_GET_FILENAME: 15790Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 15801657Sheppo credp, flag)); 15810Sstevel@tonic-gate case LOFI_GET_MINOR: 15820Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 15831657Sheppo credp, flag)); 15840Sstevel@tonic-gate case LOFI_GET_MAXMINOR: 15851657Sheppo error = ddi_copyout(&lofi_max_files, &lip->li_minor, 15861657Sheppo sizeof (lofi_max_files), flag); 15870Sstevel@tonic-gate if (error) 15880Sstevel@tonic-gate return (EFAULT); 15890Sstevel@tonic-gate return (0); 1590*5643Saalok case LOFI_CHECK_COMPRESSED: 1591*5643Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 1592*5643Saalok credp, flag)); 15930Sstevel@tonic-gate default: 15940Sstevel@tonic-gate break; 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 15990Sstevel@tonic-gate if (lsp == NULL) 16000Sstevel@tonic-gate return (ENXIO); 16010Sstevel@tonic-gate 16024451Seschrock /* 16034451Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 16044451Seschrock * EIO as if the device was no longer present. 16054451Seschrock */ 16064451Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 16074451Seschrock return (EIO); 16084451Seschrock 16090Sstevel@tonic-gate /* these are for faking out utilities like newfs */ 16100Sstevel@tonic-gate switch (cmd) { 16110Sstevel@tonic-gate case DKIOCGVTOC: 16120Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) { 16130Sstevel@tonic-gate case DDI_MODEL_ILP32: { 16140Sstevel@tonic-gate struct vtoc32 vtoc32; 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32); 16170Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg, 16180Sstevel@tonic-gate sizeof (struct vtoc32), flag)) 16190Sstevel@tonic-gate return (EFAULT); 16200Sstevel@tonic-gate break; 16210Sstevel@tonic-gate } 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate case DDI_MODEL_NONE: 16240Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 16250Sstevel@tonic-gate sizeof (struct vtoc), flag)) 16260Sstevel@tonic-gate return (EFAULT); 16270Sstevel@tonic-gate break; 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate return (0); 16300Sstevel@tonic-gate case DKIOCINFO: 16311657Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg, 16321657Sheppo sizeof (struct dk_cinfo), flag); 16330Sstevel@tonic-gate if (error) 16340Sstevel@tonic-gate return (EFAULT); 16350Sstevel@tonic-gate return (0); 16360Sstevel@tonic-gate case DKIOCG_VIRTGEOM: 16370Sstevel@tonic-gate case DKIOCG_PHYGEOM: 16380Sstevel@tonic-gate case DKIOCGGEOM: 16391657Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 16401657Sheppo sizeof (struct dk_geom), flag); 16410Sstevel@tonic-gate if (error) 16420Sstevel@tonic-gate return (EFAULT); 16430Sstevel@tonic-gate return (0); 16440Sstevel@tonic-gate case DKIOCSTATE: 16454451Seschrock /* 16464451Seschrock * Normally, lofi devices are always in the INSERTED state. If 16474451Seschrock * a device is forcefully unmapped, then the device transitions 16484451Seschrock * to the DKIO_DEV_GONE state. 16494451Seschrock */ 16504451Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 16514451Seschrock flag) != 0) 16524451Seschrock return (EFAULT); 16534451Seschrock 16544451Seschrock mutex_enter(&lsp->ls_vp_lock); 16554451Seschrock while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 16564451Seschrock (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) { 16574451Seschrock /* 16584451Seschrock * By virtue of having the device open, we know that 16594451Seschrock * 'lsp' will remain valid when we return. 16604451Seschrock */ 16614451Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv, 16624451Seschrock &lsp->ls_vp_lock)) { 16634451Seschrock mutex_exit(&lsp->ls_vp_lock); 16644451Seschrock return (EINTR); 16654451Seschrock } 16664451Seschrock } 16674451Seschrock 16684451Seschrock dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE); 16694451Seschrock mutex_exit(&lsp->ls_vp_lock); 16704451Seschrock 16714451Seschrock if (ddi_copyout(&dkstate, (void *)arg, 16724451Seschrock sizeof (dkstate), flag) != 0) 16730Sstevel@tonic-gate return (EFAULT); 16740Sstevel@tonic-gate return (0); 16750Sstevel@tonic-gate default: 16760Sstevel@tonic-gate return (ENOTTY); 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate } 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = { 16810Sstevel@tonic-gate lofi_open, /* open */ 16820Sstevel@tonic-gate lofi_close, /* close */ 16830Sstevel@tonic-gate lofi_strategy, /* strategy */ 16840Sstevel@tonic-gate nodev, /* print */ 16850Sstevel@tonic-gate nodev, /* dump */ 16860Sstevel@tonic-gate lofi_read, /* read */ 16870Sstevel@tonic-gate lofi_write, /* write */ 16880Sstevel@tonic-gate lofi_ioctl, /* ioctl */ 16890Sstevel@tonic-gate nodev, /* devmap */ 16900Sstevel@tonic-gate nodev, /* mmap */ 16910Sstevel@tonic-gate nodev, /* segmap */ 16920Sstevel@tonic-gate nochpoll, /* poll */ 16930Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 16940Sstevel@tonic-gate 0, /* streamtab */ 16950Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 16960Sstevel@tonic-gate CB_REV, 16970Sstevel@tonic-gate lofi_aread, 16980Sstevel@tonic-gate lofi_awrite 16990Sstevel@tonic-gate }; 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate static struct dev_ops lofi_ops = { 17020Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 17030Sstevel@tonic-gate 0, /* refcnt */ 17040Sstevel@tonic-gate lofi_info, /* info */ 17050Sstevel@tonic-gate nulldev, /* identify */ 17060Sstevel@tonic-gate nulldev, /* probe */ 17070Sstevel@tonic-gate lofi_attach, /* attach */ 17080Sstevel@tonic-gate lofi_detach, /* detach */ 17090Sstevel@tonic-gate nodev, /* reset */ 17100Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */ 17110Sstevel@tonic-gate NULL /* no bus operations */ 17120Sstevel@tonic-gate }; 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate static struct modldrv modldrv = { 17150Sstevel@tonic-gate &mod_driverops, 17160Sstevel@tonic-gate "loopback file driver (%I%)", 17170Sstevel@tonic-gate &lofi_ops, 17180Sstevel@tonic-gate }; 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate static struct modlinkage modlinkage = { 17210Sstevel@tonic-gate MODREV_1, 17220Sstevel@tonic-gate &modldrv, 17230Sstevel@tonic-gate NULL 17240Sstevel@tonic-gate }; 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate int 17270Sstevel@tonic-gate _init(void) 17280Sstevel@tonic-gate { 17290Sstevel@tonic-gate int error; 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate error = ddi_soft_state_init(&lofi_statep, 17320Sstevel@tonic-gate sizeof (struct lofi_state), 0); 17330Sstevel@tonic-gate if (error) 17340Sstevel@tonic-gate return (error); 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL); 17370Sstevel@tonic-gate error = mod_install(&modlinkage); 17380Sstevel@tonic-gate if (error) { 17390Sstevel@tonic-gate mutex_destroy(&lofi_lock); 17400Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 17410Sstevel@tonic-gate } 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate return (error); 17440Sstevel@tonic-gate } 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate int 17470Sstevel@tonic-gate _fini(void) 17480Sstevel@tonic-gate { 17490Sstevel@tonic-gate int error; 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate if (lofi_busy()) 17520Sstevel@tonic-gate return (EBUSY); 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate error = mod_remove(&modlinkage); 17550Sstevel@tonic-gate if (error) 17560Sstevel@tonic-gate return (error); 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate mutex_destroy(&lofi_lock); 17590Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate return (error); 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate int 17650Sstevel@tonic-gate _info(struct modinfo *modinfop) 17660Sstevel@tonic-gate { 17670Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 17680Sstevel@tonic-gate } 1769