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 /* 22*6734Sjohnlev * Copyright 2008 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> 1025643Saalok #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> 1275643Saalok #include <sys/zmod.h> 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks" 1305643Saalok #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 1535643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 1545643Saalok size_t *destlen, int level); 1555643Saalok 1565643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { 1575643Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */ 1585643Saalok {gzip_decompress, NULL, 6, "gzip-6"}, 1595643Saalok {gzip_decompress, NULL, 9, "gzip-9"} 1605643Saalok }; 1615643Saalok 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 /* 328*6734Sjohnlev * If we forcibly closed the underlying device (li_force), or 329*6734Sjohnlev * asked for cleanup (li_cleanup), finish up if we're the last 330*6734Sjohnlev * out of the door. 3314451Seschrock */ 332*6734Sjohnlev if (minor != 0 && !is_opened(lsp) && 333*6734Sjohnlev (lsp->ls_cleanup || lsp->ls_vp == NULL)) 3344451Seschrock lofi_free_handle(dev, minor, lsp, credp); 335*6734Sjohnlev 3360Sstevel@tonic-gate mutex_exit(&lofi_lock); 3370Sstevel@tonic-gate return (0); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3405643Saalok static int 3415643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 3425643Saalok struct lofi_state *lsp) 3435643Saalok { 3445643Saalok int error; 3455643Saalok offset_t alignedoffset, mapoffset; 3465643Saalok size_t xfersize; 3475643Saalok int isread; 3485643Saalok int smflags; 3495643Saalok caddr_t mapaddr; 3505643Saalok size_t len; 3515643Saalok enum seg_rw srw; 3525643Saalok 3535643Saalok /* 3545643Saalok * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 3555643Saalok * an 8K boundary, but the buf transfer address may not be 3565643Saalok * aligned on more than a 512-byte boundary (we don't enforce 3575643Saalok * that even though we could). This matters since the initial 3585643Saalok * part of the transfer may not start at offset 0 within the 3595643Saalok * segmap'd chunk. So we have to compensate for that with 3605643Saalok * 'mapoffset'. Subsequent chunks always start off at the 3615643Saalok * beginning, and the last is capped by b_resid 3625643Saalok */ 3635643Saalok mapoffset = offset & MAXBOFFSET; 3645643Saalok alignedoffset = offset - mapoffset; 3655643Saalok bp->b_resid = bp->b_bcount; 3665643Saalok isread = bp->b_flags & B_READ; 3675643Saalok srw = isread ? S_READ : S_WRITE; 3685643Saalok do { 3695643Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset, 3705643Saalok MIN(MAXBSIZE - mapoffset, bp->b_resid)); 3715643Saalok len = roundup(mapoffset + xfersize, PAGESIZE); 3725643Saalok mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 3735643Saalok alignedoffset, MAXBSIZE, 1, srw); 3745643Saalok /* 3755643Saalok * Now fault in the pages. This lets us check 3765643Saalok * for errors before we reference mapaddr and 3775643Saalok * try to resolve the fault in bcopy (which would 3785643Saalok * panic instead). And this can easily happen, 3795643Saalok * particularly if you've lofi'd a file over NFS 3805643Saalok * and someone deletes the file on the server. 3815643Saalok */ 3825643Saalok error = segmap_fault(kas.a_hat, segkmap, mapaddr, 3835643Saalok len, F_SOFTLOCK, srw); 3845643Saalok if (error) { 3855643Saalok (void) segmap_release(segkmap, mapaddr, 0); 3865643Saalok if (FC_CODE(error) == FC_OBJERR) 3875643Saalok error = FC_ERRNO(error); 3885643Saalok else 3895643Saalok error = EIO; 3905643Saalok break; 3915643Saalok } 3925643Saalok smflags = 0; 3935643Saalok if (isread) { 3945643Saalok smflags |= SM_FREE; 3955643Saalok /* 3965643Saalok * If we're reading an entire page starting 3975643Saalok * at a page boundary, there's a good chance 3985643Saalok * we won't need it again. Put it on the 3995643Saalok * head of the freelist. 4005643Saalok */ 4015643Saalok if (mapoffset == 0 && xfersize == PAGESIZE) 4025643Saalok smflags |= SM_DONTNEED; 4035643Saalok bcopy(mapaddr + mapoffset, bufaddr, xfersize); 4045643Saalok } else { 4055643Saalok smflags |= SM_WRITE; 4065643Saalok bcopy(bufaddr, mapaddr + mapoffset, xfersize); 4075643Saalok } 4085643Saalok bp->b_resid -= xfersize; 4095643Saalok bufaddr += xfersize; 4105643Saalok offset += xfersize; 4115643Saalok (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 4125643Saalok len, F_SOFTUNLOCK, srw); 4135643Saalok error = segmap_release(segkmap, mapaddr, smflags); 4145643Saalok /* only the first map may start partial */ 4155643Saalok mapoffset = 0; 4165643Saalok alignedoffset += MAXBSIZE; 4175643Saalok } while ((error == 0) && (bp->b_resid > 0) && 4185643Saalok (offset < lsp->ls_vp_comp_size)); 4195643Saalok 4205643Saalok return (error); 4215643Saalok } 4225643Saalok 4235643Saalok /*ARGSUSED*/ 4245643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 4255643Saalok size_t *dstlen, int level) 4265643Saalok { 4275643Saalok ASSERT(*dstlen >= srclen); 4285643Saalok 4295643Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 4305643Saalok return (-1); 4315643Saalok return (0); 4325643Saalok } 4335643Saalok 4340Sstevel@tonic-gate /* 4350Sstevel@tonic-gate * This is basically what strategy used to be before we found we 4360Sstevel@tonic-gate * needed task queues. 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate static void 4390Sstevel@tonic-gate lofi_strategy_task(void *arg) 4400Sstevel@tonic-gate { 4410Sstevel@tonic-gate struct buf *bp = (struct buf *)arg; 4420Sstevel@tonic-gate int error; 4430Sstevel@tonic-gate struct lofi_state *lsp; 4445643Saalok uint64_t sblkno, eblkno, cmpbytes; 4455643Saalok offset_t offset, sblkoff, eblkoff; 4465643Saalok u_offset_t salign, ealign; 4475643Saalok u_offset_t sdiff; 4485643Saalok uint32_t comp_data_sz; 4495643Saalok caddr_t bufaddr; 4505643Saalok unsigned char *compressed_seg = NULL, *cmpbuf; 4515643Saalok unsigned char *uncompressed_seg = NULL; 4525643Saalok lofi_compress_info_t *li; 4535643Saalok size_t oblkcount, xfersize; 4545643Saalok unsigned long seglen; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 4570Sstevel@tonic-gate if (lsp->ls_kstat) { 4580Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 4590Sstevel@tonic-gate kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 4600Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate bp_mapin(bp); 4630Sstevel@tonic-gate bufaddr = bp->b_un.b_addr; 4640Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * We used to always use vn_rdwr here, but we cannot do that because 4680Sstevel@tonic-gate * we might decide to read or write from the the underlying 4690Sstevel@tonic-gate * file during this call, which would be a deadlock because 4700Sstevel@tonic-gate * we have the rw_lock. So instead we page, unless it's not 4710Sstevel@tonic-gate * mapable or it's a character device. 4720Sstevel@tonic-gate */ 4734451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 4744451Seschrock error = EIO; 4754451Seschrock } else if (((lsp->ls_vp->v_flag & VNOMAP) == 0) && 4760Sstevel@tonic-gate (lsp->ls_vp->v_type != VCHR)) { 4775643Saalok uint64_t i; 4785643Saalok 4790Sstevel@tonic-gate /* 4805643Saalok * Handle uncompressed files with a regular read 4815643Saalok */ 4825643Saalok if (lsp->ls_uncomp_seg_sz == 0) { 4835643Saalok error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 4845643Saalok goto done; 4855643Saalok } 4865643Saalok 4875643Saalok /* 4885643Saalok * From here on we're dealing primarily with compressed files 4895643Saalok */ 4905643Saalok 4915643Saalok /* 4925643Saalok * Compressed files can only be read from and 4935643Saalok * not written to 4940Sstevel@tonic-gate */ 4955643Saalok if (!(bp->b_flags & B_READ)) { 4965643Saalok bp->b_resid = bp->b_bcount; 4975643Saalok error = EROFS; 4985643Saalok goto done; 4995643Saalok } 5005643Saalok 5015643Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0); 5025643Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 5035643Saalok /* 5045643Saalok * Compute starting and ending compressed segment numbers 5055643Saalok * We use only bitwise operations avoiding division and 5065643Saalok * modulus because we enforce the compression segment size 5075643Saalok * to a power of 2 5085643Saalok */ 5095643Saalok sblkno = offset >> lsp->ls_comp_seg_shift; 5105643Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 5115643Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 5125643Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 5135643Saalok 5145643Saalok /* 5155643Saalok * Align start offset to block boundary for segmap 5165643Saalok */ 5175643Saalok salign = lsp->ls_comp_seg_index[sblkno]; 5185643Saalok sdiff = salign & (DEV_BSIZE - 1); 5195643Saalok salign -= sdiff; 5205643Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 5210Sstevel@tonic-gate /* 5225643Saalok * We're dealing with the last segment of 5235643Saalok * the compressed file -- the size of this 5245643Saalok * segment *may not* be the same as the 5255643Saalok * segment size for the file 5260Sstevel@tonic-gate */ 5275643Saalok eblkoff = (offset + bp->b_bcount) & 5285643Saalok (lsp->ls_uncomp_last_seg_sz - 1); 5295643Saalok ealign = lsp->ls_vp_comp_size; 5305643Saalok } else { 5315643Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1]; 5325643Saalok } 5335643Saalok 5345643Saalok /* 5355643Saalok * Preserve original request paramaters 5365643Saalok */ 5375643Saalok oblkcount = bp->b_bcount; 5385643Saalok 5395643Saalok /* 5405643Saalok * Assign the calculated parameters 5415643Saalok */ 5425643Saalok comp_data_sz = ealign - salign; 5435643Saalok bp->b_bcount = comp_data_sz; 5445643Saalok 5455643Saalok /* 5465643Saalok * Allocate fixed size memory blocks to hold compressed 5475643Saalok * segments and one uncompressed segment since we 5485643Saalok * uncompress segments one at a time 5495643Saalok */ 5505643Saalok compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP); 5515643Saalok uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP); 5525643Saalok /* 5535643Saalok * Map in the calculated number of blocks 5545643Saalok */ 5555643Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 5565643Saalok bp, lsp); 5575643Saalok 5585643Saalok bp->b_bcount = oblkcount; 5595643Saalok bp->b_resid = oblkcount; 5605643Saalok if (error != 0) 5615643Saalok goto done; 5625643Saalok 5635643Saalok /* 5645643Saalok * We have the compressed blocks, now uncompress them 5655643Saalok */ 5665643Saalok cmpbuf = compressed_seg + sdiff; 5675643Saalok for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz; 5685643Saalok i++) { 5695643Saalok /* 5705643Saalok * Each of the segment index entries contains 5715643Saalok * the starting block number for that segment. 5725643Saalok * The number of compressed bytes in a segment 5735643Saalok * is thus the difference between the starting 5745643Saalok * block number of this segment and the starting 5755643Saalok * block number of the next segment. 5765643Saalok */ 5775643Saalok if ((i == eblkno) && 5785643Saalok (i == lsp->ls_comp_index_sz - 1)) { 5795643Saalok cmpbytes = lsp->ls_vp_comp_size - 5805643Saalok lsp->ls_comp_seg_index[i]; 5810Sstevel@tonic-gate } else { 5825643Saalok cmpbytes = lsp->ls_comp_seg_index[i + 1] - 5835643Saalok lsp->ls_comp_seg_index[i]; 5840Sstevel@tonic-gate } 5855643Saalok 5865643Saalok /* 5875643Saalok * The first byte in a compressed segment is a flag 5885643Saalok * that indicates whether this segment is compressed 5895643Saalok * at all 5905643Saalok */ 5915643Saalok if (*cmpbuf == UNCOMPRESSED) { 5925643Saalok bcopy((cmpbuf + SEGHDR), uncompressed_seg, 5935643Saalok (cmpbytes - SEGHDR)); 5945643Saalok } else { 5955643Saalok seglen = lsp->ls_uncomp_seg_sz; 5965643Saalok 5975643Saalok if (li->l_decompress((cmpbuf + SEGHDR), 5985643Saalok (cmpbytes - SEGHDR), uncompressed_seg, 5995643Saalok &seglen, li->l_level) != 0) { 6005643Saalok error = EIO; 6015643Saalok goto done; 6025643Saalok } 6035643Saalok } 6045643Saalok 6055643Saalok /* 6065643Saalok * Determine how much uncompressed data we 6075643Saalok * have to copy and copy it 6085643Saalok */ 6095643Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 6105643Saalok if (i == eblkno) { 6115643Saalok if (i == (lsp->ls_comp_index_sz - 1)) 6125643Saalok xfersize -= (lsp->ls_uncomp_last_seg_sz 6135643Saalok - eblkoff); 6145643Saalok else 6155643Saalok xfersize -= 6165643Saalok (lsp->ls_uncomp_seg_sz - eblkoff); 6175643Saalok } 6185643Saalok 6195643Saalok bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize); 6205643Saalok 6215643Saalok cmpbuf += cmpbytes; 6220Sstevel@tonic-gate bufaddr += xfersize; 6235643Saalok bp->b_resid -= xfersize; 6245643Saalok sblkoff = 0; 6255643Saalok 6265643Saalok if (bp->b_resid == 0) 6275643Saalok break; 6285643Saalok } 6290Sstevel@tonic-gate } else { 6300Sstevel@tonic-gate ssize_t resid; 6310Sstevel@tonic-gate enum uio_rw rw; 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate if (bp->b_flags & B_READ) 6340Sstevel@tonic-gate rw = UIO_READ; 6350Sstevel@tonic-gate else 6360Sstevel@tonic-gate rw = UIO_WRITE; 6370Sstevel@tonic-gate error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount, 6380Sstevel@tonic-gate offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 6390Sstevel@tonic-gate bp->b_resid = resid; 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6425643Saalok done: 6435643Saalok if (compressed_seg != NULL) 6445643Saalok kmem_free(compressed_seg, comp_data_sz); 6455643Saalok if (uncompressed_seg != NULL) 6465643Saalok kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 6475643Saalok 6480Sstevel@tonic-gate if (lsp->ls_kstat) { 6490Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid; 6500Sstevel@tonic-gate kstat_io_t *kioptr; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 6530Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 6540Sstevel@tonic-gate if (bp->b_flags & B_READ) { 6550Sstevel@tonic-gate kioptr->nread += n_done; 6560Sstevel@tonic-gate kioptr->reads++; 6570Sstevel@tonic-gate } else { 6580Sstevel@tonic-gate kioptr->nwritten += n_done; 6590Sstevel@tonic-gate kioptr->writes++; 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate kstat_runq_exit(kioptr); 6620Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 6630Sstevel@tonic-gate } 6644451Seschrock 6654451Seschrock mutex_enter(&lsp->ls_vp_lock); 6664451Seschrock if (--lsp->ls_vp_iocount == 0) 6674451Seschrock cv_broadcast(&lsp->ls_vp_cv); 6684451Seschrock mutex_exit(&lsp->ls_vp_lock); 6694451Seschrock 6700Sstevel@tonic-gate bioerror(bp, error); 6710Sstevel@tonic-gate biodone(bp); 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate static int 6750Sstevel@tonic-gate lofi_strategy(struct buf *bp) 6760Sstevel@tonic-gate { 6770Sstevel@tonic-gate struct lofi_state *lsp; 6780Sstevel@tonic-gate offset_t offset; 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * We cannot just do I/O here, because the current thread 6820Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem 6830Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and 6840Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer. 6850Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below. 6860Sstevel@tonic-gate * 6870Sstevel@tonic-gate * We could come up with a complex strategy using threads to 6880Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task 6890Sstevel@tonic-gate * queues were incredibly easy so they win. 6900Sstevel@tonic-gate */ 6910Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 6924451Seschrock mutex_enter(&lsp->ls_vp_lock); 6934451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 6944451Seschrock bioerror(bp, EIO); 6954451Seschrock biodone(bp); 6964451Seschrock mutex_exit(&lsp->ls_vp_lock); 6974451Seschrock return (0); 6984451Seschrock } 6994451Seschrock 7000Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 7010Sstevel@tonic-gate if (offset == lsp->ls_vp_size) { 7020Sstevel@tonic-gate /* EOF */ 7030Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) { 7040Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 7050Sstevel@tonic-gate bioerror(bp, 0); 7060Sstevel@tonic-gate } else { 7070Sstevel@tonic-gate /* writes should fail */ 7080Sstevel@tonic-gate bioerror(bp, ENXIO); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate biodone(bp); 7114451Seschrock mutex_exit(&lsp->ls_vp_lock); 7120Sstevel@tonic-gate return (0); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate if (offset > lsp->ls_vp_size) { 7150Sstevel@tonic-gate bioerror(bp, ENXIO); 7160Sstevel@tonic-gate biodone(bp); 7174451Seschrock mutex_exit(&lsp->ls_vp_lock); 7180Sstevel@tonic-gate return (0); 7190Sstevel@tonic-gate } 7204451Seschrock lsp->ls_vp_iocount++; 7214451Seschrock mutex_exit(&lsp->ls_vp_lock); 7224451Seschrock 7230Sstevel@tonic-gate if (lsp->ls_kstat) { 7240Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 7250Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 7260Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 7290Sstevel@tonic-gate return (0); 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate /*ARGSUSED2*/ 7330Sstevel@tonic-gate static int 7340Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 7350Sstevel@tonic-gate { 7360Sstevel@tonic-gate if (getminor(dev) == 0) 7370Sstevel@tonic-gate return (EINVAL); 7380Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate /*ARGSUSED2*/ 7420Sstevel@tonic-gate static int 7430Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate if (getminor(dev) == 0) 7460Sstevel@tonic-gate return (EINVAL); 7470Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate /*ARGSUSED2*/ 7510Sstevel@tonic-gate static int 7520Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 7530Sstevel@tonic-gate { 7540Sstevel@tonic-gate if (getminor(dev) == 0) 7550Sstevel@tonic-gate return (EINVAL); 7560Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /*ARGSUSED2*/ 7600Sstevel@tonic-gate static int 7610Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 7620Sstevel@tonic-gate { 7630Sstevel@tonic-gate if (getminor(dev) == 0) 7640Sstevel@tonic-gate return (EINVAL); 7650Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate /*ARGSUSED*/ 7690Sstevel@tonic-gate static int 7700Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 7710Sstevel@tonic-gate { 7720Sstevel@tonic-gate switch (infocmd) { 7730Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 7740Sstevel@tonic-gate *result = lofi_dip; 7750Sstevel@tonic-gate return (DDI_SUCCESS); 7760Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 7770Sstevel@tonic-gate *result = 0; 7780Sstevel@tonic-gate return (DDI_SUCCESS); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate return (DDI_FAILURE); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate static int 7840Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 7850Sstevel@tonic-gate { 7860Sstevel@tonic-gate int error; 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate if (cmd != DDI_ATTACH) 7890Sstevel@tonic-gate return (DDI_FAILURE); 7900Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0); 7910Sstevel@tonic-gate if (error == DDI_FAILURE) { 7920Sstevel@tonic-gate return (DDI_FAILURE); 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 7950Sstevel@tonic-gate DDI_PSEUDO, NULL); 7960Sstevel@tonic-gate if (error == DDI_FAILURE) { 7970Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 7980Sstevel@tonic-gate return (DDI_FAILURE); 7990Sstevel@tonic-gate } 8005084Sjohnlev /* driver handles kernel-issued IOCTLs */ 8015084Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 8025084Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 8035084Sjohnlev ddi_remove_minor_node(dip, NULL); 8045084Sjohnlev ddi_soft_state_free(lofi_statep, 0); 8055084Sjohnlev return (DDI_FAILURE); 8065084Sjohnlev } 8070Sstevel@tonic-gate lofi_dip = dip; 8080Sstevel@tonic-gate ddi_report_dev(dip); 8090Sstevel@tonic-gate return (DDI_SUCCESS); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate static int 8130Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 8140Sstevel@tonic-gate { 8150Sstevel@tonic-gate if (cmd != DDI_DETACH) 8160Sstevel@tonic-gate return (DDI_FAILURE); 8170Sstevel@tonic-gate if (lofi_busy()) 8180Sstevel@tonic-gate return (DDI_FAILURE); 8190Sstevel@tonic-gate lofi_dip = NULL; 8200Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 8215084Sjohnlev ddi_prop_remove_all(dip); 8220Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 8230Sstevel@tonic-gate return (DDI_SUCCESS); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out 8280Sstevel@tonic-gate * the lofi_ioctl structure. 8290Sstevel@tonic-gate */ 8300Sstevel@tonic-gate struct lofi_ioctl * 8311657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag) 8320Sstevel@tonic-gate { 8330Sstevel@tonic-gate struct lofi_ioctl *klip; 8340Sstevel@tonic-gate int error; 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 8371657Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 8380Sstevel@tonic-gate if (error) { 8390Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8400Sstevel@tonic-gate return (NULL); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /* make sure filename is always null-terminated */ 8440Sstevel@tonic-gate klip->li_filename[MAXPATHLEN] = '\0'; 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate /* validate minor number */ 8470Sstevel@tonic-gate if (klip->li_minor > lofi_max_files) { 8480Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8490Sstevel@tonic-gate return (NULL); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate return (klip); 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate int 8551657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 8561657Sheppo int flag) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate int error; 8590Sstevel@tonic-gate 8601657Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 8610Sstevel@tonic-gate if (error) 8620Sstevel@tonic-gate return (EFAULT); 8630Sstevel@tonic-gate return (0); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate void 8670Sstevel@tonic-gate free_lofi_ioctl(struct lofi_ioctl *klip) 8680Sstevel@tonic-gate { 8690Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate /* 8730Sstevel@tonic-gate * Return the minor number 'filename' is mapped to, if it is. 8740Sstevel@tonic-gate */ 8750Sstevel@tonic-gate static int 8760Sstevel@tonic-gate file_to_minor(char *filename) 8770Sstevel@tonic-gate { 8780Sstevel@tonic-gate minor_t minor; 8790Sstevel@tonic-gate struct lofi_state *lsp; 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 8820Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 8830Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 8840Sstevel@tonic-gate if (lsp == NULL) 8850Sstevel@tonic-gate continue; 8860Sstevel@tonic-gate if (strcmp(lsp->ls_filename, filename) == 0) 8870Sstevel@tonic-gate return (minor); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate return (0); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * lofiadm does some validation, but since Joe Random (or crashme) could 8940Sstevel@tonic-gate * do our ioctls, we need to do some validation too. 8950Sstevel@tonic-gate */ 8960Sstevel@tonic-gate static int 8970Sstevel@tonic-gate valid_filename(const char *filename) 8980Sstevel@tonic-gate { 8990Sstevel@tonic-gate static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/"; 9000Sstevel@tonic-gate static char *charprefix = "/dev/" LOFI_CHAR_NAME "/"; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* must be absolute path */ 9030Sstevel@tonic-gate if (filename[0] != '/') 9040Sstevel@tonic-gate return (0); 9050Sstevel@tonic-gate /* must not be lofi */ 9060Sstevel@tonic-gate if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0) 9070Sstevel@tonic-gate return (0); 9080Sstevel@tonic-gate if (strncmp(filename, charprefix, strlen(charprefix)) == 0) 9090Sstevel@tonic-gate return (0); 9100Sstevel@tonic-gate return (1); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate /* 9140Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size 9150Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device, 9160Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and 9170Sstevel@tonic-gate * partition information. It uses that information to determine the parameters 9183517Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 9190Sstevel@tonic-gate * have to support it. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate static void 9220Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp) 9230Sstevel@tonic-gate { 9240Sstevel@tonic-gate /* dk_geom - see dkio(7I) */ 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 9270Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to 9280Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create 9290Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick 9300Sstevel@tonic-gate * a number between one and one). 9310Sstevel@tonic-gate * 9320Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders 9330Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space 9340Sstevel@tonic-gate * for backup superblocks or cylinder group structures. 9350Sstevel@tonic-gate */ 9360Sstevel@tonic-gate if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */ 9370Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024); 9380Sstevel@tonic-gate else 9390Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024); 9400Sstevel@tonic-gate /* in case file file is < 100k */ 9410Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0) 9420Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1; 9430Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0; 9440Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0; 9450Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1; 9460Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0; 9470Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0; 9480Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0; 9490Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0; 9500Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0; 9510Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200; 9520Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 9530Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size / 9540Sstevel@tonic-gate (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 9550Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0; 9560Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0; 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate /* vtoc - see dkio(7I) */ 9590Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 9600Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE; 9610Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION; 9620Sstevel@tonic-gate bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7); 9630Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 9640Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1; 9650Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 9665643Saalok 9675643Saalok /* 9685643Saalok * A compressed file is read-only, other files can 9695643Saalok * be read-write 9705643Saalok */ 9715643Saalok if (lsp->ls_uncomp_seg_sz > 0) { 9725643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 9735643Saalok } else { 9745643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 9755643Saalok } 9760Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 9770Sstevel@tonic-gate /* 9780Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because 9790Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case, 9800Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size 9810Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder. 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 9840Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */ 9870Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 9880Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 9890Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD; 9900Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0; 9910Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0; 9920Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0; 9930Sstevel@tonic-gate lsp->ls_ci.dki_space = 0; 9940Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0; 9950Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0; 9960Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 9970Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0; 9980Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0; 9990Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0; 10000Sstevel@tonic-gate /* 10010Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it 10020Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides 10030Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because 10040Sstevel@tonic-gate * maxcontig is 0. 10050Sstevel@tonic-gate */ 10060Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16; 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate /* 10105643Saalok * map in a compressed file 10115643Saalok * 10125643Saalok * Read in the header and the index that follows. 10135643Saalok * 10145643Saalok * The header is as follows - 10155643Saalok * 10165643Saalok * Signature (name of the compression algorithm) 10175643Saalok * Compression segment size (a multiple of 512) 10185643Saalok * Number of index entries 10195643Saalok * Size of the last block 10205643Saalok * The array containing the index entries 10215643Saalok * 10225643Saalok * The header information is always stored in 10235643Saalok * network byte order on disk. 10245643Saalok */ 10255643Saalok static int 10265643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 10275643Saalok { 10285643Saalok uint32_t index_sz, header_len, i; 10295643Saalok ssize_t resid; 10305643Saalok enum uio_rw rw; 10315643Saalok char *tbuf = buf; 10325643Saalok int error; 10335643Saalok 10345643Saalok /* The signature has already been read */ 10355643Saalok tbuf += sizeof (lsp->ls_comp_algorithm); 10365643Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 10375643Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 10385643Saalok 10395643Saalok /* 10405643Saalok * The compressed segment size must be a power of 2 10415643Saalok */ 10425643Saalok if (lsp->ls_uncomp_seg_sz % 2) 10435643Saalok return (EINVAL); 10445643Saalok 10455643Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 10465643Saalok ; 10475643Saalok 10485643Saalok lsp->ls_comp_seg_shift = i; 10495643Saalok 10505643Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz); 10515643Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 10525643Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 10535643Saalok 10545643Saalok tbuf += sizeof (lsp->ls_comp_index_sz); 10555643Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 10565643Saalok sizeof (lsp->ls_uncomp_last_seg_sz)); 10575643Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 10585643Saalok 10595643Saalok /* 10605643Saalok * Compute the total size of the uncompressed data 10615643Saalok * for use in fake_disk_geometry and other calculations. 10625643Saalok * Disk geometry has to be faked with respect to the 10635643Saalok * actual uncompressed data size rather than the 10645643Saalok * compressed file size. 10655643Saalok */ 10665643Saalok lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 10675643Saalok + lsp->ls_uncomp_last_seg_sz; 10685643Saalok 10695643Saalok /* 10705643Saalok * Index size is rounded up to a 512 byte boundary for ease 10715643Saalok * of segmapping 10725643Saalok */ 10735643Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 10745643Saalok header_len = sizeof (lsp->ls_comp_algorithm) + 10755643Saalok sizeof (lsp->ls_uncomp_seg_sz) + 10765643Saalok sizeof (lsp->ls_comp_index_sz) + 10775643Saalok sizeof (lsp->ls_uncomp_last_seg_sz); 10785643Saalok lsp->ls_comp_offbase = header_len + index_sz; 10795643Saalok 10805643Saalok index_sz += header_len; 10815643Saalok index_sz = roundup(index_sz, DEV_BSIZE); 10825643Saalok 10835643Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 10845643Saalok lsp->ls_comp_index_data_sz = index_sz; 10855643Saalok 10865643Saalok /* 10875643Saalok * Read in the index -- this has a side-effect 10885643Saalok * of reading in the header as well 10895643Saalok */ 10905643Saalok rw = UIO_READ; 10915643Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 10925643Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 10935643Saalok 10945643Saalok if (error != 0) 10955643Saalok return (error); 10965643Saalok 10975643Saalok /* Skip the header, this is where the index really begins */ 10985643Saalok lsp->ls_comp_seg_index = 10995643Saalok /*LINTED*/ 11005643Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len); 11015643Saalok 11025643Saalok /* 11035643Saalok * Now recompute offsets in the index to account for 11045643Saalok * the header length 11055643Saalok */ 11065643Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) { 11075643Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 11085643Saalok BE_64(lsp->ls_comp_seg_index[i]); 11095643Saalok } 11105643Saalok 11115643Saalok return (error); 11125643Saalok } 11135643Saalok 11145643Saalok /* 11155643Saalok * Check to see if the passed in signature is a valid 11165643Saalok * one. If it is valid, return the index into 11175643Saalok * lofi_compress_table. 11185643Saalok * 11195643Saalok * Return -1 if it is invalid 11205643Saalok */ 11215643Saalok static int lofi_compress_select(char *signature) 11225643Saalok { 11235643Saalok int i; 11245643Saalok 11255643Saalok for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 11265643Saalok if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 11275643Saalok return (i); 11285643Saalok } 11295643Saalok 11305643Saalok return (-1); 11315643Saalok } 11325643Saalok 11335643Saalok /* 11340Sstevel@tonic-gate * map a file to a minor number. Return the minor number. 11350Sstevel@tonic-gate */ 11360Sstevel@tonic-gate static int 11370Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 11381657Sheppo int *rvalp, struct cred *credp, int ioctl_flag) 11390Sstevel@tonic-gate { 11400Sstevel@tonic-gate minor_t newminor; 11410Sstevel@tonic-gate struct lofi_state *lsp; 11420Sstevel@tonic-gate struct lofi_ioctl *klip; 11430Sstevel@tonic-gate int error; 11440Sstevel@tonic-gate struct vnode *vp; 11450Sstevel@tonic-gate int64_t Nblocks_prop_val; 11460Sstevel@tonic-gate int64_t Size_prop_val; 11475643Saalok int compress_index; 11480Sstevel@tonic-gate vattr_t vattr; 11490Sstevel@tonic-gate int flag; 11500Sstevel@tonic-gate enum vtype v_type; 11514451Seschrock int zalloced = 0; 11520Sstevel@tonic-gate dev_t newdev; 11534451Seschrock char namebuf[50]; 11545643Saalok char buf[DEV_BSIZE]; 11555643Saalok char *tbuf; 11565643Saalok ssize_t resid; 11575643Saalok enum uio_rw rw; 11580Sstevel@tonic-gate 11591657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 11600Sstevel@tonic-gate if (klip == NULL) 11610Sstevel@tonic-gate return (EFAULT); 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate mutex_enter(&lofi_lock); 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate if (!valid_filename(klip->li_filename)) { 11660Sstevel@tonic-gate error = EINVAL; 11670Sstevel@tonic-gate goto out; 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate if (file_to_minor(klip->li_filename) != 0) { 11710Sstevel@tonic-gate error = EBUSY; 11720Sstevel@tonic-gate goto out; 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate if (pickminor) { 11760Sstevel@tonic-gate /* Find a free one */ 11770Sstevel@tonic-gate for (newminor = 1; newminor <= lofi_max_files; newminor++) 11780Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) == NULL) 11790Sstevel@tonic-gate break; 11800Sstevel@tonic-gate if (newminor >= lofi_max_files) { 11810Sstevel@tonic-gate error = EAGAIN; 11820Sstevel@tonic-gate goto out; 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate } else { 11850Sstevel@tonic-gate newminor = klip->li_minor; 11860Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) != NULL) { 11870Sstevel@tonic-gate error = EEXIST; 11880Sstevel@tonic-gate goto out; 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* make sure it's valid */ 11930Sstevel@tonic-gate error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW, 11940Sstevel@tonic-gate NULLVPP, &vp); 11950Sstevel@tonic-gate if (error) { 11960Sstevel@tonic-gate goto out; 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate v_type = vp->v_type; 11990Sstevel@tonic-gate VN_RELE(vp); 12000Sstevel@tonic-gate if (!V_ISLOFIABLE(v_type)) { 12010Sstevel@tonic-gate error = EINVAL; 12020Sstevel@tonic-gate goto out; 12030Sstevel@tonic-gate } 12040Sstevel@tonic-gate flag = FREAD | FWRITE | FOFFMAX | FEXCL; 12050Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 12060Sstevel@tonic-gate if (error) { 12070Sstevel@tonic-gate /* try read-only */ 12080Sstevel@tonic-gate flag &= ~FWRITE; 12090Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 12100Sstevel@tonic-gate &vp, 0, 0); 12110Sstevel@tonic-gate if (error) { 12120Sstevel@tonic-gate goto out; 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate } 12150Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 12165331Samw error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 12170Sstevel@tonic-gate if (error) { 12180Sstevel@tonic-gate goto closeout; 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate /* the file needs to be a multiple of the block size */ 12210Sstevel@tonic-gate if ((vattr.va_size % DEV_BSIZE) != 0) { 12220Sstevel@tonic-gate error = EINVAL; 12230Sstevel@tonic-gate goto closeout; 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate newdev = makedevice(getmajor(dev), newminor); 12260Sstevel@tonic-gate Size_prop_val = vattr.va_size; 12270Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 12280Sstevel@tonic-gate SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) { 12290Sstevel@tonic-gate error = EINVAL; 12300Sstevel@tonic-gate goto closeout; 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate Nblocks_prop_val = vattr.va_size / DEV_BSIZE; 12330Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 12340Sstevel@tonic-gate NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 12350Sstevel@tonic-gate error = EINVAL; 12360Sstevel@tonic-gate goto propout; 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, newminor); 12390Sstevel@tonic-gate if (error == DDI_FAILURE) { 12400Sstevel@tonic-gate error = ENOMEM; 12410Sstevel@tonic-gate goto propout; 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate zalloced = 1; 12440Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 12450Sstevel@tonic-gate (void) ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor, 12460Sstevel@tonic-gate DDI_PSEUDO, NULL); 12470Sstevel@tonic-gate if (error != DDI_SUCCESS) { 12480Sstevel@tonic-gate error = ENXIO; 12490Sstevel@tonic-gate goto propout; 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor); 12520Sstevel@tonic-gate error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor, 12530Sstevel@tonic-gate DDI_PSEUDO, NULL); 12540Sstevel@tonic-gate if (error != DDI_SUCCESS) { 12550Sstevel@tonic-gate /* remove block node */ 12560Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 12570Sstevel@tonic-gate ddi_remove_minor_node(lofi_dip, namebuf); 12580Sstevel@tonic-gate error = ENXIO; 12590Sstevel@tonic-gate goto propout; 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, newminor); 12620Sstevel@tonic-gate lsp->ls_filename_sz = strlen(klip->li_filename) + 1; 12630Sstevel@tonic-gate lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP); 12640Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 12650Sstevel@tonic-gate LOFI_DRIVER_NAME, newminor); 12660Sstevel@tonic-gate lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads, 12670Sstevel@tonic-gate minclsyspri, 1, lofi_taskq_maxalloc, 0); 12680Sstevel@tonic-gate lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor, 12690Sstevel@tonic-gate NULL, "disk", KSTAT_TYPE_IO, 1, 0); 12700Sstevel@tonic-gate if (lsp->ls_kstat) { 12710Sstevel@tonic-gate mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 12720Sstevel@tonic-gate lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 12730Sstevel@tonic-gate kstat_install(lsp->ls_kstat); 12740Sstevel@tonic-gate } 12754451Seschrock cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 12764451Seschrock mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 12774451Seschrock 12780Sstevel@tonic-gate /* 12790Sstevel@tonic-gate * save open mode so file can be closed properly and vnode counts 12800Sstevel@tonic-gate * updated correctly. 12810Sstevel@tonic-gate */ 12820Sstevel@tonic-gate lsp->ls_openflag = flag; 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate /* 12850Sstevel@tonic-gate * Try to handle stacked lofs vnodes. 12860Sstevel@tonic-gate */ 12870Sstevel@tonic-gate if (vp->v_type == VREG) { 12885331Samw if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) { 12890Sstevel@tonic-gate lsp->ls_vp = vp; 12900Sstevel@tonic-gate } else { 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * Even though vp was obtained via vn_open(), we 12930Sstevel@tonic-gate * can't call vn_close() on it, since lofs will 12940Sstevel@tonic-gate * pass the VOP_CLOSE() on down to the realvp 12950Sstevel@tonic-gate * (which we are about to use). Hence we merely 12960Sstevel@tonic-gate * drop the reference to the lofs vnode and hold 12970Sstevel@tonic-gate * the realvp so things behave as if we've 12980Sstevel@tonic-gate * opened the realvp without any interaction 12990Sstevel@tonic-gate * with lofs. 13000Sstevel@tonic-gate */ 13010Sstevel@tonic-gate VN_HOLD(lsp->ls_vp); 13020Sstevel@tonic-gate VN_RELE(vp); 13030Sstevel@tonic-gate } 13040Sstevel@tonic-gate } else { 13050Sstevel@tonic-gate lsp->ls_vp = vp; 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate lsp->ls_vp_size = vattr.va_size; 13080Sstevel@tonic-gate (void) strcpy(lsp->ls_filename, klip->li_filename); 13090Sstevel@tonic-gate if (rvalp) 13100Sstevel@tonic-gate *rvalp = (int)newminor; 13110Sstevel@tonic-gate klip->li_minor = newminor; 13120Sstevel@tonic-gate 13135643Saalok /* 13145643Saalok * Read the file signature to check if it is compressed. 13155643Saalok * 'rw' is set to read since only reads are allowed to 13165643Saalok * a compressed file. 13175643Saalok */ 13185643Saalok rw = UIO_READ; 13195643Saalok error = vn_rdwr(rw, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 13205643Saalok 0, RLIM64_INFINITY, kcred, &resid); 13215643Saalok 13225643Saalok if (error != 0) 13235643Saalok goto propout; 13245643Saalok 13255643Saalok tbuf = buf; 13265643Saalok lsp->ls_uncomp_seg_sz = 0; 13275643Saalok lsp->ls_vp_comp_size = lsp->ls_vp_size; 13285643Saalok lsp->ls_comp_algorithm[0] = '\0'; 13295643Saalok 13305643Saalok compress_index = lofi_compress_select(tbuf); 13315643Saalok if (compress_index != -1) { 13325643Saalok lsp->ls_comp_algorithm_index = compress_index; 13335643Saalok (void) strlcpy(lsp->ls_comp_algorithm, 13345643Saalok lofi_compress_table[compress_index].l_name, 13355643Saalok sizeof (lsp->ls_comp_algorithm)); 13365643Saalok error = lofi_map_compressed_file(lsp, buf); 13375643Saalok if (error != 0) 13385643Saalok goto propout; 13395643Saalok 13405643Saalok /* update DDI properties */ 13415643Saalok Size_prop_val = lsp->ls_vp_size; 13425643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 13435643Saalok Size_prop_val)) != DDI_PROP_SUCCESS) { 13445643Saalok error = EINVAL; 13455643Saalok goto propout; 13465643Saalok } 13475643Saalok 13485643Saalok Nblocks_prop_val = lsp->ls_vp_size / DEV_BSIZE; 13495643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 13505643Saalok Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 13515643Saalok error = EINVAL; 13525643Saalok goto propout; 13535643Saalok } 13545643Saalok } 13555643Saalok 13560Sstevel@tonic-gate fake_disk_geometry(lsp); 13570Sstevel@tonic-gate mutex_exit(&lofi_lock); 13581657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 13590Sstevel@tonic-gate free_lofi_ioctl(klip); 13600Sstevel@tonic-gate return (0); 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate propout: 13630Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 13640Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 13650Sstevel@tonic-gate closeout: 13665331Samw (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 13670Sstevel@tonic-gate VN_RELE(vp); 13680Sstevel@tonic-gate out: 13690Sstevel@tonic-gate if (zalloced) 13700Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, newminor); 13710Sstevel@tonic-gate mutex_exit(&lofi_lock); 13720Sstevel@tonic-gate free_lofi_ioctl(klip); 13730Sstevel@tonic-gate return (error); 13740Sstevel@tonic-gate } 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate /* 13770Sstevel@tonic-gate * unmap a file. 13780Sstevel@tonic-gate */ 13790Sstevel@tonic-gate static int 13800Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 13811657Sheppo struct cred *credp, int ioctl_flag) 13820Sstevel@tonic-gate { 13830Sstevel@tonic-gate struct lofi_state *lsp; 13840Sstevel@tonic-gate struct lofi_ioctl *klip; 13850Sstevel@tonic-gate minor_t minor; 13860Sstevel@tonic-gate 13871657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 13880Sstevel@tonic-gate if (klip == NULL) 13890Sstevel@tonic-gate return (EFAULT); 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate mutex_enter(&lofi_lock); 13920Sstevel@tonic-gate if (byfilename) { 13930Sstevel@tonic-gate minor = file_to_minor(klip->li_filename); 13940Sstevel@tonic-gate } else { 13950Sstevel@tonic-gate minor = klip->li_minor; 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate if (minor == 0) { 13980Sstevel@tonic-gate mutex_exit(&lofi_lock); 13990Sstevel@tonic-gate free_lofi_ioctl(klip); 14000Sstevel@tonic-gate return (ENXIO); 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 14034451Seschrock if (lsp == NULL || lsp->ls_vp == NULL) { 14040Sstevel@tonic-gate mutex_exit(&lofi_lock); 14050Sstevel@tonic-gate free_lofi_ioctl(klip); 14060Sstevel@tonic-gate return (ENXIO); 14070Sstevel@tonic-gate } 14084451Seschrock 1409*6734Sjohnlev /* 1410*6734Sjohnlev * If it's still held open, we'll do one of three things: 1411*6734Sjohnlev * 1412*6734Sjohnlev * If no flag is set, just return EBUSY. 1413*6734Sjohnlev * 1414*6734Sjohnlev * If the 'cleanup' flag is set, unmap and remove the device when 1415*6734Sjohnlev * the last user finishes. 1416*6734Sjohnlev * 1417*6734Sjohnlev * If the 'force' flag is set, then we forcibly close the underlying 1418*6734Sjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl 1419*6734Sjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the 1420*6734Sjohnlev * device will be cleaned up appropriately. 1421*6734Sjohnlev * 1422*6734Sjohnlev * This is complicated by the fact that we may have outstanding 1423*6734Sjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all 1424*6734Sjohnlev * I/O, we keep a count of the number of outstanding I/O requests, as 1425*6734Sjohnlev * well as a flag to indicate that no new I/Os should be dispatched. 1426*6734Sjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0, 1427*6734Sjohnlev * and then close the underlying vnode. 1428*6734Sjohnlev */ 1429*6734Sjohnlev 14300Sstevel@tonic-gate if (is_opened(lsp)) { 14314451Seschrock if (klip->li_force) { 14324451Seschrock mutex_enter(&lsp->ls_vp_lock); 14334451Seschrock lsp->ls_vp_closereq = B_TRUE; 14344451Seschrock while (lsp->ls_vp_iocount > 0) 14354451Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 14364451Seschrock (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, 14375331Samw credp, NULL); 14384451Seschrock VN_RELE(lsp->ls_vp); 14394451Seschrock lsp->ls_vp = NULL; 14404451Seschrock cv_broadcast(&lsp->ls_vp_cv); 14414451Seschrock mutex_exit(&lsp->ls_vp_lock); 14424451Seschrock mutex_exit(&lofi_lock); 14434451Seschrock klip->li_minor = minor; 14444451Seschrock (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14454451Seschrock free_lofi_ioctl(klip); 14464451Seschrock return (0); 1447*6734Sjohnlev } else if (klip->li_cleanup) { 1448*6734Sjohnlev lsp->ls_cleanup = 1; 1449*6734Sjohnlev mutex_exit(&lofi_lock); 1450*6734Sjohnlev free_lofi_ioctl(klip); 1451*6734Sjohnlev return (0); 14524451Seschrock } 1453*6734Sjohnlev 14540Sstevel@tonic-gate mutex_exit(&lofi_lock); 14550Sstevel@tonic-gate free_lofi_ioctl(klip); 14560Sstevel@tonic-gate return (EBUSY); 14570Sstevel@tonic-gate } 14580Sstevel@tonic-gate 14595643Saalok if (lsp->ls_uncomp_seg_sz > 0) { 14605643Saalok kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 14615643Saalok lsp->ls_uncomp_seg_sz = 0; 14625643Saalok } 14635643Saalok 14644451Seschrock lofi_free_handle(dev, minor, lsp, credp); 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate klip->li_minor = minor; 14670Sstevel@tonic-gate mutex_exit(&lofi_lock); 14681657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14690Sstevel@tonic-gate free_lofi_ioctl(klip); 14700Sstevel@tonic-gate return (0); 14710Sstevel@tonic-gate } 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* 14740Sstevel@tonic-gate * get the filename given the minor number, or the minor number given 14750Sstevel@tonic-gate * the name. 14760Sstevel@tonic-gate */ 14774451Seschrock /*ARGSUSED*/ 14780Sstevel@tonic-gate static int 14790Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 14801657Sheppo struct cred *credp, int ioctl_flag) 14810Sstevel@tonic-gate { 14820Sstevel@tonic-gate struct lofi_state *lsp; 14830Sstevel@tonic-gate struct lofi_ioctl *klip; 14840Sstevel@tonic-gate int error; 14850Sstevel@tonic-gate minor_t minor; 14860Sstevel@tonic-gate 14871657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 14880Sstevel@tonic-gate if (klip == NULL) 14890Sstevel@tonic-gate return (EFAULT); 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate switch (which) { 14920Sstevel@tonic-gate case LOFI_GET_FILENAME: 14930Sstevel@tonic-gate minor = klip->li_minor; 14940Sstevel@tonic-gate if (minor == 0) { 14950Sstevel@tonic-gate free_lofi_ioctl(klip); 14960Sstevel@tonic-gate return (EINVAL); 14970Sstevel@tonic-gate } 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate mutex_enter(&lofi_lock); 15000Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 15010Sstevel@tonic-gate if (lsp == NULL) { 15020Sstevel@tonic-gate mutex_exit(&lofi_lock); 15030Sstevel@tonic-gate free_lofi_ioctl(klip); 15040Sstevel@tonic-gate return (ENXIO); 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate (void) strcpy(klip->li_filename, lsp->ls_filename); 15075643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 15085643Saalok sizeof (klip->li_algorithm)); 15090Sstevel@tonic-gate mutex_exit(&lofi_lock); 15101657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15110Sstevel@tonic-gate free_lofi_ioctl(klip); 15120Sstevel@tonic-gate return (error); 15130Sstevel@tonic-gate case LOFI_GET_MINOR: 15140Sstevel@tonic-gate mutex_enter(&lofi_lock); 15150Sstevel@tonic-gate klip->li_minor = file_to_minor(klip->li_filename); 15160Sstevel@tonic-gate mutex_exit(&lofi_lock); 15170Sstevel@tonic-gate if (klip->li_minor == 0) { 15180Sstevel@tonic-gate free_lofi_ioctl(klip); 15190Sstevel@tonic-gate return (ENOENT); 15200Sstevel@tonic-gate } 15211657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15220Sstevel@tonic-gate free_lofi_ioctl(klip); 15230Sstevel@tonic-gate return (error); 15245643Saalok case LOFI_CHECK_COMPRESSED: 15255643Saalok mutex_enter(&lofi_lock); 15265643Saalok klip->li_minor = file_to_minor(klip->li_filename); 15275643Saalok mutex_exit(&lofi_lock); 15285643Saalok if (klip->li_minor == 0) { 15295643Saalok free_lofi_ioctl(klip); 15305643Saalok return (ENOENT); 15315643Saalok } 15325643Saalok mutex_enter(&lofi_lock); 15335643Saalok lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 15345643Saalok if (lsp == NULL) { 15355643Saalok mutex_exit(&lofi_lock); 15365643Saalok free_lofi_ioctl(klip); 15375643Saalok return (ENXIO); 15385643Saalok } 15395643Saalok ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0); 15405643Saalok 15415643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 15425643Saalok sizeof (klip->li_algorithm)); 15435643Saalok mutex_exit(&lofi_lock); 15445643Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15455643Saalok free_lofi_ioctl(klip); 15465643Saalok return (error); 15470Sstevel@tonic-gate default: 15480Sstevel@tonic-gate free_lofi_ioctl(klip); 15490Sstevel@tonic-gate return (EINVAL); 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate static int 15550Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 15560Sstevel@tonic-gate int *rvalp) 15570Sstevel@tonic-gate { 15580Sstevel@tonic-gate int error; 15590Sstevel@tonic-gate enum dkio_state dkstate; 15600Sstevel@tonic-gate struct lofi_state *lsp; 15610Sstevel@tonic-gate minor_t minor; 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate #ifdef lint 15640Sstevel@tonic-gate credp = credp; 15650Sstevel@tonic-gate #endif 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate minor = getminor(dev); 15680Sstevel@tonic-gate /* lofi ioctls only apply to the master device */ 15690Sstevel@tonic-gate if (minor == 0) { 15700Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate /* 15730Sstevel@tonic-gate * the query command only need read-access - i.e., normal 15740Sstevel@tonic-gate * users are allowed to do those on the ctl device as 15750Sstevel@tonic-gate * long as they can open it read-only. 15760Sstevel@tonic-gate */ 15770Sstevel@tonic-gate switch (cmd) { 15780Sstevel@tonic-gate case LOFI_MAP_FILE: 15790Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15800Sstevel@tonic-gate return (EPERM); 15811657Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 15820Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR: 15830Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15840Sstevel@tonic-gate return (EPERM); 15851657Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 15860Sstevel@tonic-gate case LOFI_UNMAP_FILE: 15870Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15880Sstevel@tonic-gate return (EPERM); 15891657Sheppo return (lofi_unmap_file(dev, lip, 1, credp, flag)); 15900Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR: 15910Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15920Sstevel@tonic-gate return (EPERM); 15931657Sheppo return (lofi_unmap_file(dev, lip, 0, credp, flag)); 15940Sstevel@tonic-gate case LOFI_GET_FILENAME: 15950Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 15961657Sheppo credp, flag)); 15970Sstevel@tonic-gate case LOFI_GET_MINOR: 15980Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 15991657Sheppo credp, flag)); 16000Sstevel@tonic-gate case LOFI_GET_MAXMINOR: 16011657Sheppo error = ddi_copyout(&lofi_max_files, &lip->li_minor, 16021657Sheppo sizeof (lofi_max_files), flag); 16030Sstevel@tonic-gate if (error) 16040Sstevel@tonic-gate return (EFAULT); 16050Sstevel@tonic-gate return (0); 16065643Saalok case LOFI_CHECK_COMPRESSED: 16075643Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 16085643Saalok credp, flag)); 16090Sstevel@tonic-gate default: 16100Sstevel@tonic-gate break; 16110Sstevel@tonic-gate } 16120Sstevel@tonic-gate } 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 16150Sstevel@tonic-gate if (lsp == NULL) 16160Sstevel@tonic-gate return (ENXIO); 16170Sstevel@tonic-gate 16184451Seschrock /* 16194451Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 16204451Seschrock * EIO as if the device was no longer present. 16214451Seschrock */ 16224451Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 16234451Seschrock return (EIO); 16244451Seschrock 16250Sstevel@tonic-gate /* these are for faking out utilities like newfs */ 16260Sstevel@tonic-gate switch (cmd) { 16270Sstevel@tonic-gate case DKIOCGVTOC: 16280Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) { 16290Sstevel@tonic-gate case DDI_MODEL_ILP32: { 16300Sstevel@tonic-gate struct vtoc32 vtoc32; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32); 16330Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg, 16340Sstevel@tonic-gate sizeof (struct vtoc32), flag)) 16350Sstevel@tonic-gate return (EFAULT); 16360Sstevel@tonic-gate break; 16370Sstevel@tonic-gate } 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate case DDI_MODEL_NONE: 16400Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 16410Sstevel@tonic-gate sizeof (struct vtoc), flag)) 16420Sstevel@tonic-gate return (EFAULT); 16430Sstevel@tonic-gate break; 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate return (0); 16460Sstevel@tonic-gate case DKIOCINFO: 16471657Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg, 16481657Sheppo sizeof (struct dk_cinfo), flag); 16490Sstevel@tonic-gate if (error) 16500Sstevel@tonic-gate return (EFAULT); 16510Sstevel@tonic-gate return (0); 16520Sstevel@tonic-gate case DKIOCG_VIRTGEOM: 16530Sstevel@tonic-gate case DKIOCG_PHYGEOM: 16540Sstevel@tonic-gate case DKIOCGGEOM: 16551657Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 16561657Sheppo sizeof (struct dk_geom), flag); 16570Sstevel@tonic-gate if (error) 16580Sstevel@tonic-gate return (EFAULT); 16590Sstevel@tonic-gate return (0); 16600Sstevel@tonic-gate case DKIOCSTATE: 16614451Seschrock /* 16624451Seschrock * Normally, lofi devices are always in the INSERTED state. If 16634451Seschrock * a device is forcefully unmapped, then the device transitions 16644451Seschrock * to the DKIO_DEV_GONE state. 16654451Seschrock */ 16664451Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 16674451Seschrock flag) != 0) 16684451Seschrock return (EFAULT); 16694451Seschrock 16704451Seschrock mutex_enter(&lsp->ls_vp_lock); 16714451Seschrock while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 16724451Seschrock (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) { 16734451Seschrock /* 16744451Seschrock * By virtue of having the device open, we know that 16754451Seschrock * 'lsp' will remain valid when we return. 16764451Seschrock */ 16774451Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv, 16784451Seschrock &lsp->ls_vp_lock)) { 16794451Seschrock mutex_exit(&lsp->ls_vp_lock); 16804451Seschrock return (EINTR); 16814451Seschrock } 16824451Seschrock } 16834451Seschrock 16844451Seschrock dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE); 16854451Seschrock mutex_exit(&lsp->ls_vp_lock); 16864451Seschrock 16874451Seschrock if (ddi_copyout(&dkstate, (void *)arg, 16884451Seschrock sizeof (dkstate), flag) != 0) 16890Sstevel@tonic-gate return (EFAULT); 16900Sstevel@tonic-gate return (0); 16910Sstevel@tonic-gate default: 16920Sstevel@tonic-gate return (ENOTTY); 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate } 16950Sstevel@tonic-gate 16960Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = { 16970Sstevel@tonic-gate lofi_open, /* open */ 16980Sstevel@tonic-gate lofi_close, /* close */ 16990Sstevel@tonic-gate lofi_strategy, /* strategy */ 17000Sstevel@tonic-gate nodev, /* print */ 17010Sstevel@tonic-gate nodev, /* dump */ 17020Sstevel@tonic-gate lofi_read, /* read */ 17030Sstevel@tonic-gate lofi_write, /* write */ 17040Sstevel@tonic-gate lofi_ioctl, /* ioctl */ 17050Sstevel@tonic-gate nodev, /* devmap */ 17060Sstevel@tonic-gate nodev, /* mmap */ 17070Sstevel@tonic-gate nodev, /* segmap */ 17080Sstevel@tonic-gate nochpoll, /* poll */ 17090Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 17100Sstevel@tonic-gate 0, /* streamtab */ 17110Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 17120Sstevel@tonic-gate CB_REV, 17130Sstevel@tonic-gate lofi_aread, 17140Sstevel@tonic-gate lofi_awrite 17150Sstevel@tonic-gate }; 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate static struct dev_ops lofi_ops = { 17180Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 17190Sstevel@tonic-gate 0, /* refcnt */ 17200Sstevel@tonic-gate lofi_info, /* info */ 17210Sstevel@tonic-gate nulldev, /* identify */ 17220Sstevel@tonic-gate nulldev, /* probe */ 17230Sstevel@tonic-gate lofi_attach, /* attach */ 17240Sstevel@tonic-gate lofi_detach, /* detach */ 17250Sstevel@tonic-gate nodev, /* reset */ 17260Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */ 17270Sstevel@tonic-gate NULL /* no bus operations */ 17280Sstevel@tonic-gate }; 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate static struct modldrv modldrv = { 17310Sstevel@tonic-gate &mod_driverops, 17320Sstevel@tonic-gate "loopback file driver (%I%)", 17330Sstevel@tonic-gate &lofi_ops, 17340Sstevel@tonic-gate }; 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate static struct modlinkage modlinkage = { 17370Sstevel@tonic-gate MODREV_1, 17380Sstevel@tonic-gate &modldrv, 17390Sstevel@tonic-gate NULL 17400Sstevel@tonic-gate }; 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate int 17430Sstevel@tonic-gate _init(void) 17440Sstevel@tonic-gate { 17450Sstevel@tonic-gate int error; 17460Sstevel@tonic-gate 17470Sstevel@tonic-gate error = ddi_soft_state_init(&lofi_statep, 17480Sstevel@tonic-gate sizeof (struct lofi_state), 0); 17490Sstevel@tonic-gate if (error) 17500Sstevel@tonic-gate return (error); 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL); 17530Sstevel@tonic-gate error = mod_install(&modlinkage); 17540Sstevel@tonic-gate if (error) { 17550Sstevel@tonic-gate mutex_destroy(&lofi_lock); 17560Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 17570Sstevel@tonic-gate } 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate return (error); 17600Sstevel@tonic-gate } 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate int 17630Sstevel@tonic-gate _fini(void) 17640Sstevel@tonic-gate { 17650Sstevel@tonic-gate int error; 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate if (lofi_busy()) 17680Sstevel@tonic-gate return (EBUSY); 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate error = mod_remove(&modlinkage); 17710Sstevel@tonic-gate if (error) 17720Sstevel@tonic-gate return (error); 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate mutex_destroy(&lofi_lock); 17750Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep); 17760Sstevel@tonic-gate 17770Sstevel@tonic-gate return (error); 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate 17800Sstevel@tonic-gate int 17810Sstevel@tonic-gate _info(struct modinfo *modinfop) 17820Sstevel@tonic-gate { 17830Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 17840Sstevel@tonic-gate } 1785