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 /* 226734Sjohnlev * 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 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * lofi (loopback file) driver - allows you to attach a file to a device, 290Sstevel@tonic-gate * which can then be accessed through that device. The simple model is that 300Sstevel@tonic-gate * you tell lofi to open a file, and then use the block device you get as 310Sstevel@tonic-gate * you would any block device. lofi translates access to the block device 320Sstevel@tonic-gate * into I/O on the underlying file. This is mostly useful for 330Sstevel@tonic-gate * mounting images of filesystems. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * lofi is controlled through /dev/lofictl - this is the only device exported 360Sstevel@tonic-gate * during attach, and is minor number 0. lofiadm communicates with lofi through 370Sstevel@tonic-gate * ioctls on this device. When a file is attached to lofi, block and character 380Sstevel@tonic-gate * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices 390Sstevel@tonic-gate * are identified by their minor number, and the minor number is also used 400Sstevel@tonic-gate * as the name in /dev/lofi. If we ever decide to support virtual disks, 410Sstevel@tonic-gate * we'll have to divide the minor number space to identify fdisk partitions 420Sstevel@tonic-gate * and slices, and the name will then be the minor number shifted down a 430Sstevel@tonic-gate * few bits. Minor devices are tracked with state structures handled with 440Sstevel@tonic-gate * ddi_soft_state(9F) for simplicity. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * A file attached to lofi is opened when attached and not closed until 470Sstevel@tonic-gate * explicitly detached from lofi. This seems more sensible than deferring 480Sstevel@tonic-gate * the open until the /dev/lofi device is opened, for a number of reasons. 490Sstevel@tonic-gate * One is that any failure is likely to be noticed by the person (or script) 500Sstevel@tonic-gate * running lofiadm. Another is that it would be a security problem if the 510Sstevel@tonic-gate * file was replaced by another one after being added but before being opened. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * The only hard part about lofi is the ioctls. In order to support things 540Sstevel@tonic-gate * like 'newfs' on a lofi device, it needs to support certain disk ioctls. 550Sstevel@tonic-gate * So it has to fake disk geometry and partition information. More may need 560Sstevel@tonic-gate * to be faked if your favorite utility doesn't work and you think it should 570Sstevel@tonic-gate * (fdformat doesn't work because it really wants to know the type of floppy 580Sstevel@tonic-gate * controller to talk to, and that didn't seem easy to fake. Or possibly even 590Sstevel@tonic-gate * necessary, since we have mkfs_pcfs now). 600Sstevel@tonic-gate * 614451Seschrock * Normally, a lofi device cannot be detached if it is open (i.e. busy). To 624451Seschrock * support simulation of hotplug events, an optional force flag is provided. 634451Seschrock * If a lofi device is open when a force detach is requested, then the 644451Seschrock * underlying file is closed and any subsequent operations return EIO. When the 654451Seschrock * device is closed for the last time, it will be cleaned up at that time. In 664451Seschrock * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is 674451Seschrock * detached but not removed. 684451Seschrock * 690Sstevel@tonic-gate * Known problems: 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * UFS logging. Mounting a UFS filesystem image "logging" 720Sstevel@tonic-gate * works for basic copy testing but wedges during a build of ON through 730Sstevel@tonic-gate * that image. Some deadlock in lufs holding the log mutex and then 740Sstevel@tonic-gate * getting stuck on a buf. So for now, don't do that. 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * Direct I/O. Since the filesystem data is being cached in the buffer 770Sstevel@tonic-gate * cache, _and_ again in the underlying filesystem, it's tempting to 780Sstevel@tonic-gate * enable direct I/O on the underlying file. Don't, because that deadlocks. 790Sstevel@tonic-gate * I think to fix the cache-twice problem we might need filesystem support. 800Sstevel@tonic-gate * 810Sstevel@tonic-gate * lofi on itself. The simple lock strategy (lofi_lock) precludes this 820Sstevel@tonic-gate * because you'll be in lofi_ioctl, holding the lock when you open the 830Sstevel@tonic-gate * file, which, if it's lofi, will grab lofi_lock. We prevent this for 840Sstevel@tonic-gate * now, though not using ddi_soft_state(9F) would make it possible to 850Sstevel@tonic-gate * do. Though it would still be silly. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * Interesting things to do: 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically. 900Sstevel@tonic-gate * 910Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not 920Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't 930Sstevel@tonic-gate * need to fake a geometry. But this is also silly unless you're replacing 940Sstevel@tonic-gate * metadisk. 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * Encryption. tpm would like this. Apparently Windows 2000 has it, and 970Sstevel@tonic-gate * so does Linux. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate #include <sys/types.h> 1015643Saalok #include <netinet/in.h> 1020Sstevel@tonic-gate #include <sys/sysmacros.h> 1030Sstevel@tonic-gate #include <sys/uio.h> 1040Sstevel@tonic-gate #include <sys/kmem.h> 1050Sstevel@tonic-gate #include <sys/cred.h> 1060Sstevel@tonic-gate #include <sys/mman.h> 1070Sstevel@tonic-gate #include <sys/errno.h> 1080Sstevel@tonic-gate #include <sys/aio_req.h> 1090Sstevel@tonic-gate #include <sys/stat.h> 1100Sstevel@tonic-gate #include <sys/file.h> 1110Sstevel@tonic-gate #include <sys/modctl.h> 1120Sstevel@tonic-gate #include <sys/conf.h> 1130Sstevel@tonic-gate #include <sys/debug.h> 1140Sstevel@tonic-gate #include <sys/vnode.h> 1150Sstevel@tonic-gate #include <sys/lofi.h> 1160Sstevel@tonic-gate #include <sys/fcntl.h> 1170Sstevel@tonic-gate #include <sys/pathname.h> 1180Sstevel@tonic-gate #include <sys/filio.h> 1190Sstevel@tonic-gate #include <sys/fdio.h> 1200Sstevel@tonic-gate #include <sys/open.h> 1210Sstevel@tonic-gate #include <sys/disp.h> 1220Sstevel@tonic-gate #include <vm/seg_map.h> 1230Sstevel@tonic-gate #include <sys/ddi.h> 1240Sstevel@tonic-gate #include <sys/sunddi.h> 1255643Saalok #include <sys/zmod.h> 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks" 1285643Saalok #define SIZE_PROP_NAME "Size" 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate static dev_info_t *lofi_dip; 1310Sstevel@tonic-gate static void *lofi_statep; 1320Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */ 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* 1350Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the 1360Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively 1370Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy, 1380Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of 1390Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for 1400Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should 1410Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the 1420Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic 1430Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on 1440Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE. 1450Sstevel@tonic-gate */ 1460Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE; 1470Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */ 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate uint32_t lofi_max_files = LOFI_MAX_FILES; 1500Sstevel@tonic-gate 1515643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 1525643Saalok size_t *destlen, int level); 1535643Saalok 1545643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { 1555643Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */ 1565643Saalok {gzip_decompress, NULL, 6, "gzip-6"}, 1575643Saalok {gzip_decompress, NULL, 9, "gzip-9"} 1585643Saalok }; 1595643Saalok 1600Sstevel@tonic-gate static int 1610Sstevel@tonic-gate lofi_busy(void) 1620Sstevel@tonic-gate { 1630Sstevel@tonic-gate minor_t minor; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * We need to make sure no mappings exist - mod_remove won't 1670Sstevel@tonic-gate * help because the device isn't open. 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate mutex_enter(&lofi_lock); 1700Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 1710Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, minor) != NULL) { 1720Sstevel@tonic-gate mutex_exit(&lofi_lock); 1730Sstevel@tonic-gate return (EBUSY); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate mutex_exit(&lofi_lock); 1770Sstevel@tonic-gate return (0); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate static int 1810Sstevel@tonic-gate is_opened(struct lofi_state *lsp) 1820Sstevel@tonic-gate { 1830Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 1840Sstevel@tonic-gate return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate static int 1880Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp) 1890Sstevel@tonic-gate { 1900Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 1910Sstevel@tonic-gate switch (otyp) { 1920Sstevel@tonic-gate case OTYP_CHR: 1930Sstevel@tonic-gate lsp->ls_chr_open = 1; 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate case OTYP_BLK: 1960Sstevel@tonic-gate lsp->ls_blk_open = 1; 1970Sstevel@tonic-gate break; 1980Sstevel@tonic-gate case OTYP_LYR: 1990Sstevel@tonic-gate lsp->ls_lyr_open_count++; 2000Sstevel@tonic-gate break; 2010Sstevel@tonic-gate default: 2020Sstevel@tonic-gate return (-1); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate return (0); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate static void 2080Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp) 2090Sstevel@tonic-gate { 2100Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 2110Sstevel@tonic-gate switch (otyp) { 2120Sstevel@tonic-gate case OTYP_CHR: 2130Sstevel@tonic-gate lsp->ls_chr_open = 0; 2140Sstevel@tonic-gate break; 2150Sstevel@tonic-gate case OTYP_BLK: 2160Sstevel@tonic-gate lsp->ls_blk_open = 0; 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate case OTYP_LYR: 2190Sstevel@tonic-gate lsp->ls_lyr_open_count--; 2200Sstevel@tonic-gate break; 2210Sstevel@tonic-gate default: 2220Sstevel@tonic-gate break; 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2264451Seschrock static void 2274451Seschrock lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp, 2284451Seschrock cred_t *credp) 2294451Seschrock { 2304451Seschrock dev_t newdev; 2314451Seschrock char namebuf[50]; 2324451Seschrock 2334451Seschrock if (lsp->ls_vp) { 2345331Samw (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 2355331Samw 1, 0, credp, NULL); 2364451Seschrock VN_RELE(lsp->ls_vp); 2374451Seschrock lsp->ls_vp = NULL; 2384451Seschrock } 2394451Seschrock 2404451Seschrock newdev = makedevice(getmajor(dev), minor); 2414451Seschrock (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 2424451Seschrock (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 2434451Seschrock 2444451Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 2454451Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 2464451Seschrock (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 2474451Seschrock ddi_remove_minor_node(lofi_dip, namebuf); 2484451Seschrock 2494451Seschrock kmem_free(lsp->ls_filename, lsp->ls_filename_sz); 2504451Seschrock taskq_destroy(lsp->ls_taskq); 2514451Seschrock if (lsp->ls_kstat) { 2524451Seschrock kstat_delete(lsp->ls_kstat); 2534451Seschrock mutex_destroy(&lsp->ls_kstat_lock); 2544451Seschrock } 2556791Saalok 2566791Saalok if (lsp->ls_uncomp_seg_sz > 0) { 2576791Saalok kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 2586791Saalok lsp->ls_uncomp_seg_sz = 0; 2596791Saalok } 2604451Seschrock ddi_soft_state_free(lofi_statep, minor); 2614451Seschrock } 2624451Seschrock 2634451Seschrock /*ARGSUSED*/ 2640Sstevel@tonic-gate static int 2650Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate minor_t minor; 2680Sstevel@tonic-gate struct lofi_state *lsp; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate mutex_enter(&lofi_lock); 2710Sstevel@tonic-gate minor = getminor(*devp); 2720Sstevel@tonic-gate if (minor == 0) { 2730Sstevel@tonic-gate /* master control device */ 2740Sstevel@tonic-gate /* must be opened exclusively */ 2750Sstevel@tonic-gate if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) { 2760Sstevel@tonic-gate mutex_exit(&lofi_lock); 2770Sstevel@tonic-gate return (EINVAL); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, 0); 2800Sstevel@tonic-gate if (lsp == NULL) { 2810Sstevel@tonic-gate mutex_exit(&lofi_lock); 2820Sstevel@tonic-gate return (ENXIO); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate if (is_opened(lsp)) { 2850Sstevel@tonic-gate mutex_exit(&lofi_lock); 2860Sstevel@tonic-gate return (EBUSY); 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate (void) mark_opened(lsp, OTYP_CHR); 2890Sstevel@tonic-gate mutex_exit(&lofi_lock); 2900Sstevel@tonic-gate return (0); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* otherwise, the mapping should already exist */ 2940Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 2950Sstevel@tonic-gate if (lsp == NULL) { 2960Sstevel@tonic-gate mutex_exit(&lofi_lock); 2970Sstevel@tonic-gate return (EINVAL); 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3004451Seschrock if (lsp->ls_vp == NULL) { 3014451Seschrock mutex_exit(&lofi_lock); 3024451Seschrock return (ENXIO); 3034451Seschrock } 3044451Seschrock 3050Sstevel@tonic-gate if (mark_opened(lsp, otyp) == -1) { 3060Sstevel@tonic-gate mutex_exit(&lofi_lock); 3070Sstevel@tonic-gate return (EINVAL); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate mutex_exit(&lofi_lock); 3110Sstevel@tonic-gate return (0); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3144451Seschrock /*ARGSUSED*/ 3150Sstevel@tonic-gate static int 3160Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate minor_t minor; 3190Sstevel@tonic-gate struct lofi_state *lsp; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate mutex_enter(&lofi_lock); 3220Sstevel@tonic-gate minor = getminor(dev); 3230Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 3240Sstevel@tonic-gate if (lsp == NULL) { 3250Sstevel@tonic-gate mutex_exit(&lofi_lock); 3260Sstevel@tonic-gate return (EINVAL); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate mark_closed(lsp, otyp); 3294451Seschrock 3304451Seschrock /* 3316734Sjohnlev * If we forcibly closed the underlying device (li_force), or 3326734Sjohnlev * asked for cleanup (li_cleanup), finish up if we're the last 3336734Sjohnlev * out of the door. 3344451Seschrock */ 3356734Sjohnlev if (minor != 0 && !is_opened(lsp) && 3366734Sjohnlev (lsp->ls_cleanup || lsp->ls_vp == NULL)) 3374451Seschrock lofi_free_handle(dev, minor, lsp, credp); 3386734Sjohnlev 3390Sstevel@tonic-gate mutex_exit(&lofi_lock); 3400Sstevel@tonic-gate return (0); 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate 3435643Saalok static int 3445643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 3455643Saalok struct lofi_state *lsp) 3465643Saalok { 3475643Saalok int error; 3485643Saalok offset_t alignedoffset, mapoffset; 3495643Saalok size_t xfersize; 3505643Saalok int isread; 3515643Saalok int smflags; 3525643Saalok caddr_t mapaddr; 3535643Saalok size_t len; 3545643Saalok enum seg_rw srw; 3555643Saalok 3565643Saalok /* 3575643Saalok * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 3585643Saalok * an 8K boundary, but the buf transfer address may not be 3595643Saalok * aligned on more than a 512-byte boundary (we don't enforce 3605643Saalok * that even though we could). This matters since the initial 3615643Saalok * part of the transfer may not start at offset 0 within the 3625643Saalok * segmap'd chunk. So we have to compensate for that with 3635643Saalok * 'mapoffset'. Subsequent chunks always start off at the 3645643Saalok * beginning, and the last is capped by b_resid 3655643Saalok */ 3665643Saalok mapoffset = offset & MAXBOFFSET; 3675643Saalok alignedoffset = offset - mapoffset; 3685643Saalok bp->b_resid = bp->b_bcount; 3695643Saalok isread = bp->b_flags & B_READ; 3705643Saalok srw = isread ? S_READ : S_WRITE; 3715643Saalok do { 3725643Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset, 3735643Saalok MIN(MAXBSIZE - mapoffset, bp->b_resid)); 3745643Saalok len = roundup(mapoffset + xfersize, PAGESIZE); 3755643Saalok mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 3765643Saalok alignedoffset, MAXBSIZE, 1, srw); 3775643Saalok /* 3785643Saalok * Now fault in the pages. This lets us check 3795643Saalok * for errors before we reference mapaddr and 3805643Saalok * try to resolve the fault in bcopy (which would 3815643Saalok * panic instead). And this can easily happen, 3825643Saalok * particularly if you've lofi'd a file over NFS 3835643Saalok * and someone deletes the file on the server. 3845643Saalok */ 3855643Saalok error = segmap_fault(kas.a_hat, segkmap, mapaddr, 3865643Saalok len, F_SOFTLOCK, srw); 3875643Saalok if (error) { 3885643Saalok (void) segmap_release(segkmap, mapaddr, 0); 3895643Saalok if (FC_CODE(error) == FC_OBJERR) 3905643Saalok error = FC_ERRNO(error); 3915643Saalok else 3925643Saalok error = EIO; 3935643Saalok break; 3945643Saalok } 3955643Saalok smflags = 0; 3965643Saalok if (isread) { 3975643Saalok smflags |= SM_FREE; 3985643Saalok /* 3995643Saalok * If we're reading an entire page starting 4005643Saalok * at a page boundary, there's a good chance 4015643Saalok * we won't need it again. Put it on the 4025643Saalok * head of the freelist. 4035643Saalok */ 4045643Saalok if (mapoffset == 0 && xfersize == PAGESIZE) 4055643Saalok smflags |= SM_DONTNEED; 4065643Saalok bcopy(mapaddr + mapoffset, bufaddr, xfersize); 4075643Saalok } else { 4085643Saalok smflags |= SM_WRITE; 4095643Saalok bcopy(bufaddr, mapaddr + mapoffset, xfersize); 4105643Saalok } 4115643Saalok bp->b_resid -= xfersize; 4125643Saalok bufaddr += xfersize; 4135643Saalok offset += xfersize; 4145643Saalok (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 4155643Saalok len, F_SOFTUNLOCK, srw); 4165643Saalok error = segmap_release(segkmap, mapaddr, smflags); 4175643Saalok /* only the first map may start partial */ 4185643Saalok mapoffset = 0; 4195643Saalok alignedoffset += MAXBSIZE; 4205643Saalok } while ((error == 0) && (bp->b_resid > 0) && 4215643Saalok (offset < lsp->ls_vp_comp_size)); 4225643Saalok 4235643Saalok return (error); 4245643Saalok } 4255643Saalok 4265643Saalok /*ARGSUSED*/ 4275643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst, 4285643Saalok size_t *dstlen, int level) 4295643Saalok { 4305643Saalok ASSERT(*dstlen >= srclen); 4315643Saalok 4325643Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 4335643Saalok return (-1); 4345643Saalok return (0); 4355643Saalok } 4365643Saalok 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * This is basically what strategy used to be before we found we 4390Sstevel@tonic-gate * needed task queues. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate static void 4420Sstevel@tonic-gate lofi_strategy_task(void *arg) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate struct buf *bp = (struct buf *)arg; 4450Sstevel@tonic-gate int error; 4460Sstevel@tonic-gate struct lofi_state *lsp; 4475643Saalok uint64_t sblkno, eblkno, cmpbytes; 4485643Saalok offset_t offset, sblkoff, eblkoff; 4495643Saalok u_offset_t salign, ealign; 4505643Saalok u_offset_t sdiff; 4515643Saalok uint32_t comp_data_sz; 4525643Saalok caddr_t bufaddr; 4535643Saalok unsigned char *compressed_seg = NULL, *cmpbuf; 4545643Saalok unsigned char *uncompressed_seg = NULL; 4555643Saalok lofi_compress_info_t *li; 4565643Saalok size_t oblkcount, xfersize; 4575643Saalok unsigned long seglen; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 4600Sstevel@tonic-gate if (lsp->ls_kstat) { 4610Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 4620Sstevel@tonic-gate kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 4630Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate bp_mapin(bp); 4660Sstevel@tonic-gate bufaddr = bp->b_un.b_addr; 4670Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate /* 4700Sstevel@tonic-gate * We used to always use vn_rdwr here, but we cannot do that because 4710Sstevel@tonic-gate * we might decide to read or write from the the underlying 4720Sstevel@tonic-gate * file during this call, which would be a deadlock because 4730Sstevel@tonic-gate * we have the rw_lock. So instead we page, unless it's not 4740Sstevel@tonic-gate * mapable or it's a character device. 4750Sstevel@tonic-gate */ 4764451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 4774451Seschrock error = EIO; 4784451Seschrock } else if (((lsp->ls_vp->v_flag & VNOMAP) == 0) && 4790Sstevel@tonic-gate (lsp->ls_vp->v_type != VCHR)) { 4805643Saalok uint64_t i; 4815643Saalok 4820Sstevel@tonic-gate /* 4835643Saalok * Handle uncompressed files with a regular read 4845643Saalok */ 4855643Saalok if (lsp->ls_uncomp_seg_sz == 0) { 4865643Saalok error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 4875643Saalok goto done; 4885643Saalok } 4895643Saalok 4905643Saalok /* 4915643Saalok * From here on we're dealing primarily with compressed files 4925643Saalok */ 4935643Saalok 4945643Saalok /* 4955643Saalok * Compressed files can only be read from and 4965643Saalok * not written to 4970Sstevel@tonic-gate */ 4985643Saalok if (!(bp->b_flags & B_READ)) { 4995643Saalok bp->b_resid = bp->b_bcount; 5005643Saalok error = EROFS; 5015643Saalok goto done; 5025643Saalok } 5035643Saalok 5045643Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0); 5055643Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 5065643Saalok /* 5075643Saalok * Compute starting and ending compressed segment numbers 5085643Saalok * We use only bitwise operations avoiding division and 5095643Saalok * modulus because we enforce the compression segment size 5105643Saalok * to a power of 2 5115643Saalok */ 5125643Saalok sblkno = offset >> lsp->ls_comp_seg_shift; 5135643Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 5145643Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 5155643Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 5165643Saalok 5175643Saalok /* 5185643Saalok * Align start offset to block boundary for segmap 5195643Saalok */ 5205643Saalok salign = lsp->ls_comp_seg_index[sblkno]; 5215643Saalok sdiff = salign & (DEV_BSIZE - 1); 5225643Saalok salign -= sdiff; 5235643Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 5240Sstevel@tonic-gate /* 5255643Saalok * We're dealing with the last segment of 5265643Saalok * the compressed file -- the size of this 5275643Saalok * segment *may not* be the same as the 5285643Saalok * segment size for the file 5290Sstevel@tonic-gate */ 5305643Saalok eblkoff = (offset + bp->b_bcount) & 5315643Saalok (lsp->ls_uncomp_last_seg_sz - 1); 5325643Saalok ealign = lsp->ls_vp_comp_size; 5335643Saalok } else { 5345643Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1]; 5355643Saalok } 5365643Saalok 5375643Saalok /* 5385643Saalok * Preserve original request paramaters 5395643Saalok */ 5405643Saalok oblkcount = bp->b_bcount; 5415643Saalok 5425643Saalok /* 5435643Saalok * Assign the calculated parameters 5445643Saalok */ 5455643Saalok comp_data_sz = ealign - salign; 5465643Saalok bp->b_bcount = comp_data_sz; 5475643Saalok 5485643Saalok /* 5495643Saalok * Allocate fixed size memory blocks to hold compressed 5505643Saalok * segments and one uncompressed segment since we 5515643Saalok * uncompress segments one at a time 5525643Saalok */ 5535643Saalok compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP); 5545643Saalok uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP); 5555643Saalok /* 5565643Saalok * Map in the calculated number of blocks 5575643Saalok */ 5585643Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 5595643Saalok bp, lsp); 5605643Saalok 5615643Saalok bp->b_bcount = oblkcount; 5625643Saalok bp->b_resid = oblkcount; 5635643Saalok if (error != 0) 5645643Saalok goto done; 5655643Saalok 5665643Saalok /* 5675643Saalok * We have the compressed blocks, now uncompress them 5685643Saalok */ 5695643Saalok cmpbuf = compressed_seg + sdiff; 5705643Saalok for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz; 5715643Saalok i++) { 5725643Saalok /* 5735643Saalok * Each of the segment index entries contains 5745643Saalok * the starting block number for that segment. 5755643Saalok * The number of compressed bytes in a segment 5765643Saalok * is thus the difference between the starting 5775643Saalok * block number of this segment and the starting 5785643Saalok * block number of the next segment. 5795643Saalok */ 5805643Saalok if ((i == eblkno) && 5815643Saalok (i == lsp->ls_comp_index_sz - 1)) { 5825643Saalok cmpbytes = lsp->ls_vp_comp_size - 5835643Saalok lsp->ls_comp_seg_index[i]; 5840Sstevel@tonic-gate } else { 5855643Saalok cmpbytes = lsp->ls_comp_seg_index[i + 1] - 5865643Saalok lsp->ls_comp_seg_index[i]; 5870Sstevel@tonic-gate } 5885643Saalok 5895643Saalok /* 5905643Saalok * The first byte in a compressed segment is a flag 5915643Saalok * that indicates whether this segment is compressed 5925643Saalok * at all 5935643Saalok */ 5945643Saalok if (*cmpbuf == UNCOMPRESSED) { 5955643Saalok bcopy((cmpbuf + SEGHDR), uncompressed_seg, 5965643Saalok (cmpbytes - SEGHDR)); 5975643Saalok } else { 5985643Saalok seglen = lsp->ls_uncomp_seg_sz; 5995643Saalok 6005643Saalok if (li->l_decompress((cmpbuf + SEGHDR), 6015643Saalok (cmpbytes - SEGHDR), uncompressed_seg, 6025643Saalok &seglen, li->l_level) != 0) { 6035643Saalok error = EIO; 6045643Saalok goto done; 6055643Saalok } 6065643Saalok } 6075643Saalok 6085643Saalok /* 6095643Saalok * Determine how much uncompressed data we 6105643Saalok * have to copy and copy it 6115643Saalok */ 6125643Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 6135643Saalok if (i == eblkno) { 6145643Saalok if (i == (lsp->ls_comp_index_sz - 1)) 6155643Saalok xfersize -= (lsp->ls_uncomp_last_seg_sz 6165643Saalok - eblkoff); 6175643Saalok else 6185643Saalok xfersize -= 6195643Saalok (lsp->ls_uncomp_seg_sz - eblkoff); 6205643Saalok } 6215643Saalok 6225643Saalok bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize); 6235643Saalok 6245643Saalok cmpbuf += cmpbytes; 6250Sstevel@tonic-gate bufaddr += xfersize; 6265643Saalok bp->b_resid -= xfersize; 6275643Saalok sblkoff = 0; 6285643Saalok 6295643Saalok if (bp->b_resid == 0) 6305643Saalok break; 6315643Saalok } 6320Sstevel@tonic-gate } else { 6330Sstevel@tonic-gate ssize_t resid; 6340Sstevel@tonic-gate enum uio_rw rw; 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (bp->b_flags & B_READ) 6370Sstevel@tonic-gate rw = UIO_READ; 6380Sstevel@tonic-gate else 6390Sstevel@tonic-gate rw = UIO_WRITE; 6400Sstevel@tonic-gate error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount, 6410Sstevel@tonic-gate offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 6420Sstevel@tonic-gate bp->b_resid = resid; 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6455643Saalok done: 6465643Saalok if (compressed_seg != NULL) 6475643Saalok kmem_free(compressed_seg, comp_data_sz); 6485643Saalok if (uncompressed_seg != NULL) 6495643Saalok kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 6505643Saalok 6510Sstevel@tonic-gate if (lsp->ls_kstat) { 6520Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid; 6530Sstevel@tonic-gate kstat_io_t *kioptr; 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 6560Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 6570Sstevel@tonic-gate if (bp->b_flags & B_READ) { 6580Sstevel@tonic-gate kioptr->nread += n_done; 6590Sstevel@tonic-gate kioptr->reads++; 6600Sstevel@tonic-gate } else { 6610Sstevel@tonic-gate kioptr->nwritten += n_done; 6620Sstevel@tonic-gate kioptr->writes++; 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate kstat_runq_exit(kioptr); 6650Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 6660Sstevel@tonic-gate } 6674451Seschrock 6684451Seschrock mutex_enter(&lsp->ls_vp_lock); 6694451Seschrock if (--lsp->ls_vp_iocount == 0) 6704451Seschrock cv_broadcast(&lsp->ls_vp_cv); 6714451Seschrock mutex_exit(&lsp->ls_vp_lock); 6724451Seschrock 6730Sstevel@tonic-gate bioerror(bp, error); 6740Sstevel@tonic-gate biodone(bp); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate static int 6780Sstevel@tonic-gate lofi_strategy(struct buf *bp) 6790Sstevel@tonic-gate { 6800Sstevel@tonic-gate struct lofi_state *lsp; 6810Sstevel@tonic-gate offset_t offset; 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * We cannot just do I/O here, because the current thread 6850Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem 6860Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and 6870Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer. 6880Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below. 6890Sstevel@tonic-gate * 6900Sstevel@tonic-gate * We could come up with a complex strategy using threads to 6910Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task 6920Sstevel@tonic-gate * queues were incredibly easy so they win. 6930Sstevel@tonic-gate */ 6940Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 6954451Seschrock mutex_enter(&lsp->ls_vp_lock); 6964451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 6974451Seschrock bioerror(bp, EIO); 6984451Seschrock biodone(bp); 6994451Seschrock mutex_exit(&lsp->ls_vp_lock); 7004451Seschrock return (0); 7014451Seschrock } 7024451Seschrock 7030Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 7040Sstevel@tonic-gate if (offset == lsp->ls_vp_size) { 7050Sstevel@tonic-gate /* EOF */ 7060Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) { 7070Sstevel@tonic-gate bp->b_resid = bp->b_bcount; 7080Sstevel@tonic-gate bioerror(bp, 0); 7090Sstevel@tonic-gate } else { 7100Sstevel@tonic-gate /* writes should fail */ 7110Sstevel@tonic-gate bioerror(bp, ENXIO); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate biodone(bp); 7144451Seschrock mutex_exit(&lsp->ls_vp_lock); 7150Sstevel@tonic-gate return (0); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate if (offset > lsp->ls_vp_size) { 7180Sstevel@tonic-gate bioerror(bp, ENXIO); 7190Sstevel@tonic-gate biodone(bp); 7204451Seschrock mutex_exit(&lsp->ls_vp_lock); 7210Sstevel@tonic-gate return (0); 7220Sstevel@tonic-gate } 7234451Seschrock lsp->ls_vp_iocount++; 7244451Seschrock mutex_exit(&lsp->ls_vp_lock); 7254451Seschrock 7260Sstevel@tonic-gate if (lsp->ls_kstat) { 7270Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock); 7280Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 7290Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock); 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 7320Sstevel@tonic-gate return (0); 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /*ARGSUSED2*/ 7360Sstevel@tonic-gate static int 7370Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 7380Sstevel@tonic-gate { 7390Sstevel@tonic-gate if (getminor(dev) == 0) 7400Sstevel@tonic-gate return (EINVAL); 7410Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /*ARGSUSED2*/ 7450Sstevel@tonic-gate static int 7460Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 7470Sstevel@tonic-gate { 7480Sstevel@tonic-gate if (getminor(dev) == 0) 7490Sstevel@tonic-gate return (EINVAL); 7500Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate /*ARGSUSED2*/ 7540Sstevel@tonic-gate static int 7550Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 7560Sstevel@tonic-gate { 7570Sstevel@tonic-gate if (getminor(dev) == 0) 7580Sstevel@tonic-gate return (EINVAL); 7590Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate /*ARGSUSED2*/ 7630Sstevel@tonic-gate static int 7640Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 7650Sstevel@tonic-gate { 7660Sstevel@tonic-gate if (getminor(dev) == 0) 7670Sstevel@tonic-gate return (EINVAL); 7680Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /*ARGSUSED*/ 7720Sstevel@tonic-gate static int 7730Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 7740Sstevel@tonic-gate { 7750Sstevel@tonic-gate switch (infocmd) { 7760Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 7770Sstevel@tonic-gate *result = lofi_dip; 7780Sstevel@tonic-gate return (DDI_SUCCESS); 7790Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 7800Sstevel@tonic-gate *result = 0; 7810Sstevel@tonic-gate return (DDI_SUCCESS); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate return (DDI_FAILURE); 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate static int 7870Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 7880Sstevel@tonic-gate { 7890Sstevel@tonic-gate int error; 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate if (cmd != DDI_ATTACH) 7920Sstevel@tonic-gate return (DDI_FAILURE); 7930Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0); 7940Sstevel@tonic-gate if (error == DDI_FAILURE) { 7950Sstevel@tonic-gate return (DDI_FAILURE); 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 7980Sstevel@tonic-gate DDI_PSEUDO, NULL); 7990Sstevel@tonic-gate if (error == DDI_FAILURE) { 8000Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 8010Sstevel@tonic-gate return (DDI_FAILURE); 8020Sstevel@tonic-gate } 8035084Sjohnlev /* driver handles kernel-issued IOCTLs */ 8045084Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 8055084Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 8065084Sjohnlev ddi_remove_minor_node(dip, NULL); 8075084Sjohnlev ddi_soft_state_free(lofi_statep, 0); 8085084Sjohnlev return (DDI_FAILURE); 8095084Sjohnlev } 8100Sstevel@tonic-gate lofi_dip = dip; 8110Sstevel@tonic-gate ddi_report_dev(dip); 8120Sstevel@tonic-gate return (DDI_SUCCESS); 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate static int 8160Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 8170Sstevel@tonic-gate { 8180Sstevel@tonic-gate if (cmd != DDI_DETACH) 8190Sstevel@tonic-gate return (DDI_FAILURE); 8200Sstevel@tonic-gate if (lofi_busy()) 8210Sstevel@tonic-gate return (DDI_FAILURE); 8220Sstevel@tonic-gate lofi_dip = NULL; 8230Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 8245084Sjohnlev ddi_prop_remove_all(dip); 8250Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0); 8260Sstevel@tonic-gate return (DDI_SUCCESS); 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out 8310Sstevel@tonic-gate * the lofi_ioctl structure. 8320Sstevel@tonic-gate */ 8330Sstevel@tonic-gate struct lofi_ioctl * 8341657Sheppo copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag) 8350Sstevel@tonic-gate { 8360Sstevel@tonic-gate struct lofi_ioctl *klip; 8370Sstevel@tonic-gate int error; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 8401657Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 8410Sstevel@tonic-gate if (error) { 8420Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8430Sstevel@tonic-gate return (NULL); 8440Sstevel@tonic-gate } 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate /* make sure filename is always null-terminated */ 8470Sstevel@tonic-gate klip->li_filename[MAXPATHLEN] = '\0'; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate /* validate minor number */ 8500Sstevel@tonic-gate if (klip->li_minor > lofi_max_files) { 8510Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8520Sstevel@tonic-gate return (NULL); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate return (klip); 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate int 8581657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 8591657Sheppo int flag) 8600Sstevel@tonic-gate { 8610Sstevel@tonic-gate int error; 8620Sstevel@tonic-gate 8631657Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 8640Sstevel@tonic-gate if (error) 8650Sstevel@tonic-gate return (EFAULT); 8660Sstevel@tonic-gate return (0); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate void 8700Sstevel@tonic-gate free_lofi_ioctl(struct lofi_ioctl *klip) 8710Sstevel@tonic-gate { 8720Sstevel@tonic-gate kmem_free(klip, sizeof (struct lofi_ioctl)); 8730Sstevel@tonic-gate } 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate /* 8760Sstevel@tonic-gate * Return the minor number 'filename' is mapped to, if it is. 8770Sstevel@tonic-gate */ 8780Sstevel@tonic-gate static int 8790Sstevel@tonic-gate file_to_minor(char *filename) 8800Sstevel@tonic-gate { 8810Sstevel@tonic-gate minor_t minor; 8820Sstevel@tonic-gate struct lofi_state *lsp; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate ASSERT(mutex_owned(&lofi_lock)); 8850Sstevel@tonic-gate for (minor = 1; minor <= lofi_max_files; minor++) { 8860Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 8870Sstevel@tonic-gate if (lsp == NULL) 8880Sstevel@tonic-gate continue; 8890Sstevel@tonic-gate if (strcmp(lsp->ls_filename, filename) == 0) 8900Sstevel@tonic-gate return (minor); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate return (0); 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * lofiadm does some validation, but since Joe Random (or crashme) could 8970Sstevel@tonic-gate * do our ioctls, we need to do some validation too. 8980Sstevel@tonic-gate */ 8990Sstevel@tonic-gate static int 9000Sstevel@tonic-gate valid_filename(const char *filename) 9010Sstevel@tonic-gate { 9020Sstevel@tonic-gate static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/"; 9030Sstevel@tonic-gate static char *charprefix = "/dev/" LOFI_CHAR_NAME "/"; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* must be absolute path */ 9060Sstevel@tonic-gate if (filename[0] != '/') 9070Sstevel@tonic-gate return (0); 9080Sstevel@tonic-gate /* must not be lofi */ 9090Sstevel@tonic-gate if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0) 9100Sstevel@tonic-gate return (0); 9110Sstevel@tonic-gate if (strncmp(filename, charprefix, strlen(charprefix)) == 0) 9120Sstevel@tonic-gate return (0); 9130Sstevel@tonic-gate return (1); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate /* 9170Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size 9180Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device, 9190Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and 9200Sstevel@tonic-gate * partition information. It uses that information to determine the parameters 9213517Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 9220Sstevel@tonic-gate * have to support it. 9230Sstevel@tonic-gate */ 9240Sstevel@tonic-gate static void 9250Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp) 9260Sstevel@tonic-gate { 9270Sstevel@tonic-gate /* dk_geom - see dkio(7I) */ 9280Sstevel@tonic-gate /* 9290Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 9300Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to 9310Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create 9320Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick 9330Sstevel@tonic-gate * a number between one and one). 9340Sstevel@tonic-gate * 9350Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders 9360Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space 9370Sstevel@tonic-gate * for backup superblocks or cylinder group structures. 9380Sstevel@tonic-gate */ 9390Sstevel@tonic-gate if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */ 9400Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024); 9410Sstevel@tonic-gate else 9420Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024); 9430Sstevel@tonic-gate /* in case file file is < 100k */ 9440Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0) 9450Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1; 9460Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0; 9470Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0; 9480Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1; 9490Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0; 9500Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0; 9510Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0; 9520Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0; 9530Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0; 9540Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200; 9550Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 9560Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size / 9570Sstevel@tonic-gate (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 9580Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0; 9590Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0; 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate /* vtoc - see dkio(7I) */ 9620Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 9630Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE; 9640Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION; 9650Sstevel@tonic-gate bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7); 9660Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 9670Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1; 9680Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 9695643Saalok 9705643Saalok /* 9715643Saalok * A compressed file is read-only, other files can 9725643Saalok * be read-write 9735643Saalok */ 9745643Saalok if (lsp->ls_uncomp_seg_sz > 0) { 9755643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 9765643Saalok } else { 9775643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 9785643Saalok } 9790Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because 9820Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case, 9830Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size 9840Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 9870Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */ 9900Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 9910Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 9920Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD; 9930Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0; 9940Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0; 9950Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0; 9960Sstevel@tonic-gate lsp->ls_ci.dki_space = 0; 9970Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0; 9980Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0; 9990Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 10000Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0; 10010Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0; 10020Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0; 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it 10050Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides 10060Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because 10070Sstevel@tonic-gate * maxcontig is 0. 10080Sstevel@tonic-gate */ 10090Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16; 10100Sstevel@tonic-gate } 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate /* 10135643Saalok * map in a compressed file 10145643Saalok * 10155643Saalok * Read in the header and the index that follows. 10165643Saalok * 10175643Saalok * The header is as follows - 10185643Saalok * 10195643Saalok * Signature (name of the compression algorithm) 10205643Saalok * Compression segment size (a multiple of 512) 10215643Saalok * Number of index entries 10225643Saalok * Size of the last block 10235643Saalok * The array containing the index entries 10245643Saalok * 10255643Saalok * The header information is always stored in 10265643Saalok * network byte order on disk. 10275643Saalok */ 10285643Saalok static int 10295643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 10305643Saalok { 10315643Saalok uint32_t index_sz, header_len, i; 10325643Saalok ssize_t resid; 10335643Saalok enum uio_rw rw; 10345643Saalok char *tbuf = buf; 10355643Saalok int error; 10365643Saalok 10375643Saalok /* The signature has already been read */ 10385643Saalok tbuf += sizeof (lsp->ls_comp_algorithm); 10395643Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 10405643Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 10415643Saalok 10425643Saalok /* 10435643Saalok * The compressed segment size must be a power of 2 10445643Saalok */ 10455643Saalok if (lsp->ls_uncomp_seg_sz % 2) 10465643Saalok return (EINVAL); 10475643Saalok 10485643Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 10495643Saalok ; 10505643Saalok 10515643Saalok lsp->ls_comp_seg_shift = i; 10525643Saalok 10535643Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz); 10545643Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 10555643Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 10565643Saalok 10575643Saalok tbuf += sizeof (lsp->ls_comp_index_sz); 10585643Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 10595643Saalok sizeof (lsp->ls_uncomp_last_seg_sz)); 10605643Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 10615643Saalok 10625643Saalok /* 10635643Saalok * Compute the total size of the uncompressed data 10645643Saalok * for use in fake_disk_geometry and other calculations. 10655643Saalok * Disk geometry has to be faked with respect to the 10665643Saalok * actual uncompressed data size rather than the 10675643Saalok * compressed file size. 10685643Saalok */ 10695643Saalok lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 10705643Saalok + lsp->ls_uncomp_last_seg_sz; 10715643Saalok 10725643Saalok /* 10735643Saalok * Index size is rounded up to a 512 byte boundary for ease 10745643Saalok * of segmapping 10755643Saalok */ 10765643Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 10775643Saalok header_len = sizeof (lsp->ls_comp_algorithm) + 10785643Saalok sizeof (lsp->ls_uncomp_seg_sz) + 10795643Saalok sizeof (lsp->ls_comp_index_sz) + 10805643Saalok sizeof (lsp->ls_uncomp_last_seg_sz); 10815643Saalok lsp->ls_comp_offbase = header_len + index_sz; 10825643Saalok 10835643Saalok index_sz += header_len; 10845643Saalok index_sz = roundup(index_sz, DEV_BSIZE); 10855643Saalok 10865643Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 10875643Saalok lsp->ls_comp_index_data_sz = index_sz; 10885643Saalok 10895643Saalok /* 10905643Saalok * Read in the index -- this has a side-effect 10915643Saalok * of reading in the header as well 10925643Saalok */ 10935643Saalok rw = UIO_READ; 10945643Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 10955643Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 10965643Saalok 10975643Saalok if (error != 0) 10985643Saalok return (error); 10995643Saalok 11005643Saalok /* Skip the header, this is where the index really begins */ 11015643Saalok lsp->ls_comp_seg_index = 11025643Saalok /*LINTED*/ 11035643Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len); 11045643Saalok 11055643Saalok /* 11065643Saalok * Now recompute offsets in the index to account for 11075643Saalok * the header length 11085643Saalok */ 11095643Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) { 11105643Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 11115643Saalok BE_64(lsp->ls_comp_seg_index[i]); 11125643Saalok } 11135643Saalok 11145643Saalok return (error); 11155643Saalok } 11165643Saalok 11175643Saalok /* 11185643Saalok * Check to see if the passed in signature is a valid 11195643Saalok * one. If it is valid, return the index into 11205643Saalok * lofi_compress_table. 11215643Saalok * 11225643Saalok * Return -1 if it is invalid 11235643Saalok */ 11245643Saalok static int lofi_compress_select(char *signature) 11255643Saalok { 11265643Saalok int i; 11275643Saalok 11285643Saalok for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 11295643Saalok if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 11305643Saalok return (i); 11315643Saalok } 11325643Saalok 11335643Saalok return (-1); 11345643Saalok } 11355643Saalok 11365643Saalok /* 11370Sstevel@tonic-gate * map a file to a minor number. Return the minor number. 11380Sstevel@tonic-gate */ 11390Sstevel@tonic-gate static int 11400Sstevel@tonic-gate lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 11411657Sheppo int *rvalp, struct cred *credp, int ioctl_flag) 11420Sstevel@tonic-gate { 11430Sstevel@tonic-gate minor_t newminor; 11440Sstevel@tonic-gate struct lofi_state *lsp; 11450Sstevel@tonic-gate struct lofi_ioctl *klip; 11460Sstevel@tonic-gate int error; 11470Sstevel@tonic-gate struct vnode *vp; 11480Sstevel@tonic-gate int64_t Nblocks_prop_val; 11490Sstevel@tonic-gate int64_t Size_prop_val; 11505643Saalok int compress_index; 11510Sstevel@tonic-gate vattr_t vattr; 11520Sstevel@tonic-gate int flag; 11530Sstevel@tonic-gate enum vtype v_type; 11544451Seschrock int zalloced = 0; 11550Sstevel@tonic-gate dev_t newdev; 11564451Seschrock char namebuf[50]; 11575643Saalok char buf[DEV_BSIZE]; 11585643Saalok char *tbuf; 11595643Saalok ssize_t resid; 11605643Saalok enum uio_rw rw; 11610Sstevel@tonic-gate 11621657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 11630Sstevel@tonic-gate if (klip == NULL) 11640Sstevel@tonic-gate return (EFAULT); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate mutex_enter(&lofi_lock); 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate if (!valid_filename(klip->li_filename)) { 11690Sstevel@tonic-gate error = EINVAL; 11700Sstevel@tonic-gate goto out; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate if (file_to_minor(klip->li_filename) != 0) { 11740Sstevel@tonic-gate error = EBUSY; 11750Sstevel@tonic-gate goto out; 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate if (pickminor) { 11790Sstevel@tonic-gate /* Find a free one */ 11800Sstevel@tonic-gate for (newminor = 1; newminor <= lofi_max_files; newminor++) 11810Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) == NULL) 11820Sstevel@tonic-gate break; 11830Sstevel@tonic-gate if (newminor >= lofi_max_files) { 11840Sstevel@tonic-gate error = EAGAIN; 11850Sstevel@tonic-gate goto out; 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate } else { 11880Sstevel@tonic-gate newminor = klip->li_minor; 11890Sstevel@tonic-gate if (ddi_get_soft_state(lofi_statep, newminor) != NULL) { 11900Sstevel@tonic-gate error = EEXIST; 11910Sstevel@tonic-gate goto out; 11920Sstevel@tonic-gate } 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate /* make sure it's valid */ 11960Sstevel@tonic-gate error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW, 11970Sstevel@tonic-gate NULLVPP, &vp); 11980Sstevel@tonic-gate if (error) { 11990Sstevel@tonic-gate goto out; 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate v_type = vp->v_type; 12020Sstevel@tonic-gate VN_RELE(vp); 12030Sstevel@tonic-gate if (!V_ISLOFIABLE(v_type)) { 12040Sstevel@tonic-gate error = EINVAL; 12050Sstevel@tonic-gate goto out; 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate flag = FREAD | FWRITE | FOFFMAX | FEXCL; 12080Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 12090Sstevel@tonic-gate if (error) { 12100Sstevel@tonic-gate /* try read-only */ 12110Sstevel@tonic-gate flag &= ~FWRITE; 12120Sstevel@tonic-gate error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 12130Sstevel@tonic-gate &vp, 0, 0); 12140Sstevel@tonic-gate if (error) { 12150Sstevel@tonic-gate goto out; 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 12195331Samw error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 12200Sstevel@tonic-gate if (error) { 12210Sstevel@tonic-gate goto closeout; 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate /* the file needs to be a multiple of the block size */ 12240Sstevel@tonic-gate if ((vattr.va_size % DEV_BSIZE) != 0) { 12250Sstevel@tonic-gate error = EINVAL; 12260Sstevel@tonic-gate goto closeout; 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate newdev = makedevice(getmajor(dev), newminor); 12290Sstevel@tonic-gate Size_prop_val = vattr.va_size; 12300Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 12310Sstevel@tonic-gate SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) { 12320Sstevel@tonic-gate error = EINVAL; 12330Sstevel@tonic-gate goto closeout; 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate Nblocks_prop_val = vattr.va_size / DEV_BSIZE; 12360Sstevel@tonic-gate if ((ddi_prop_update_int64(newdev, lofi_dip, 12370Sstevel@tonic-gate NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 12380Sstevel@tonic-gate error = EINVAL; 12390Sstevel@tonic-gate goto propout; 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, newminor); 12420Sstevel@tonic-gate if (error == DDI_FAILURE) { 12430Sstevel@tonic-gate error = ENOMEM; 12440Sstevel@tonic-gate goto propout; 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate zalloced = 1; 12470Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 12486883Sgd78059 error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor, 12490Sstevel@tonic-gate DDI_PSEUDO, NULL); 12500Sstevel@tonic-gate if (error != DDI_SUCCESS) { 12510Sstevel@tonic-gate error = ENXIO; 12520Sstevel@tonic-gate goto propout; 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor); 12550Sstevel@tonic-gate error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor, 12560Sstevel@tonic-gate DDI_PSEUDO, NULL); 12570Sstevel@tonic-gate if (error != DDI_SUCCESS) { 12580Sstevel@tonic-gate /* remove block node */ 12590Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 12600Sstevel@tonic-gate ddi_remove_minor_node(lofi_dip, namebuf); 12610Sstevel@tonic-gate error = ENXIO; 12620Sstevel@tonic-gate goto propout; 12630Sstevel@tonic-gate } 12640Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, newminor); 12650Sstevel@tonic-gate lsp->ls_filename_sz = strlen(klip->li_filename) + 1; 12660Sstevel@tonic-gate lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP); 12670Sstevel@tonic-gate (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 12680Sstevel@tonic-gate LOFI_DRIVER_NAME, newminor); 12690Sstevel@tonic-gate lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads, 12700Sstevel@tonic-gate minclsyspri, 1, lofi_taskq_maxalloc, 0); 12710Sstevel@tonic-gate lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor, 12720Sstevel@tonic-gate NULL, "disk", KSTAT_TYPE_IO, 1, 0); 12730Sstevel@tonic-gate if (lsp->ls_kstat) { 12740Sstevel@tonic-gate mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 12750Sstevel@tonic-gate lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 12760Sstevel@tonic-gate kstat_install(lsp->ls_kstat); 12770Sstevel@tonic-gate } 12784451Seschrock cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 12794451Seschrock mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 12804451Seschrock 12810Sstevel@tonic-gate /* 12820Sstevel@tonic-gate * save open mode so file can be closed properly and vnode counts 12830Sstevel@tonic-gate * updated correctly. 12840Sstevel@tonic-gate */ 12850Sstevel@tonic-gate lsp->ls_openflag = flag; 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate /* 12880Sstevel@tonic-gate * Try to handle stacked lofs vnodes. 12890Sstevel@tonic-gate */ 12900Sstevel@tonic-gate if (vp->v_type == VREG) { 12915331Samw if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) { 12920Sstevel@tonic-gate lsp->ls_vp = vp; 12930Sstevel@tonic-gate } else { 12940Sstevel@tonic-gate /* 12950Sstevel@tonic-gate * Even though vp was obtained via vn_open(), we 12960Sstevel@tonic-gate * can't call vn_close() on it, since lofs will 12970Sstevel@tonic-gate * pass the VOP_CLOSE() on down to the realvp 12980Sstevel@tonic-gate * (which we are about to use). Hence we merely 12990Sstevel@tonic-gate * drop the reference to the lofs vnode and hold 13000Sstevel@tonic-gate * the realvp so things behave as if we've 13010Sstevel@tonic-gate * opened the realvp without any interaction 13020Sstevel@tonic-gate * with lofs. 13030Sstevel@tonic-gate */ 13040Sstevel@tonic-gate VN_HOLD(lsp->ls_vp); 13050Sstevel@tonic-gate VN_RELE(vp); 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate } else { 13080Sstevel@tonic-gate lsp->ls_vp = vp; 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate lsp->ls_vp_size = vattr.va_size; 13110Sstevel@tonic-gate (void) strcpy(lsp->ls_filename, klip->li_filename); 13120Sstevel@tonic-gate if (rvalp) 13130Sstevel@tonic-gate *rvalp = (int)newminor; 13140Sstevel@tonic-gate klip->li_minor = newminor; 13150Sstevel@tonic-gate 13165643Saalok /* 13175643Saalok * Read the file signature to check if it is compressed. 13185643Saalok * 'rw' is set to read since only reads are allowed to 13195643Saalok * a compressed file. 13205643Saalok */ 13215643Saalok rw = UIO_READ; 13225643Saalok error = vn_rdwr(rw, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 13235643Saalok 0, RLIM64_INFINITY, kcred, &resid); 13245643Saalok 13255643Saalok if (error != 0) 13265643Saalok goto propout; 13275643Saalok 13285643Saalok tbuf = buf; 13295643Saalok lsp->ls_uncomp_seg_sz = 0; 13305643Saalok lsp->ls_vp_comp_size = lsp->ls_vp_size; 13315643Saalok lsp->ls_comp_algorithm[0] = '\0'; 13325643Saalok 13335643Saalok compress_index = lofi_compress_select(tbuf); 13345643Saalok if (compress_index != -1) { 13355643Saalok lsp->ls_comp_algorithm_index = compress_index; 13365643Saalok (void) strlcpy(lsp->ls_comp_algorithm, 13375643Saalok lofi_compress_table[compress_index].l_name, 13385643Saalok sizeof (lsp->ls_comp_algorithm)); 13395643Saalok error = lofi_map_compressed_file(lsp, buf); 13405643Saalok if (error != 0) 13415643Saalok goto propout; 13425643Saalok 13435643Saalok /* update DDI properties */ 13445643Saalok Size_prop_val = lsp->ls_vp_size; 13455643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 13465643Saalok Size_prop_val)) != DDI_PROP_SUCCESS) { 13475643Saalok error = EINVAL; 13485643Saalok goto propout; 13495643Saalok } 13505643Saalok 13515643Saalok Nblocks_prop_val = lsp->ls_vp_size / DEV_BSIZE; 13525643Saalok if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 13535643Saalok Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 13545643Saalok error = EINVAL; 13555643Saalok goto propout; 13565643Saalok } 13575643Saalok } 13585643Saalok 13590Sstevel@tonic-gate fake_disk_geometry(lsp); 13600Sstevel@tonic-gate mutex_exit(&lofi_lock); 13611657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 13620Sstevel@tonic-gate free_lofi_ioctl(klip); 13630Sstevel@tonic-gate return (0); 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate propout: 13660Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 13670Sstevel@tonic-gate (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 13680Sstevel@tonic-gate closeout: 13695331Samw (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 13700Sstevel@tonic-gate VN_RELE(vp); 13710Sstevel@tonic-gate out: 13720Sstevel@tonic-gate if (zalloced) 13730Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, newminor); 13740Sstevel@tonic-gate mutex_exit(&lofi_lock); 13750Sstevel@tonic-gate free_lofi_ioctl(klip); 13760Sstevel@tonic-gate return (error); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate /* 13800Sstevel@tonic-gate * unmap a file. 13810Sstevel@tonic-gate */ 13820Sstevel@tonic-gate static int 13830Sstevel@tonic-gate lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 13841657Sheppo struct cred *credp, int ioctl_flag) 13850Sstevel@tonic-gate { 13860Sstevel@tonic-gate struct lofi_state *lsp; 13870Sstevel@tonic-gate struct lofi_ioctl *klip; 13880Sstevel@tonic-gate minor_t minor; 13890Sstevel@tonic-gate 13901657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 13910Sstevel@tonic-gate if (klip == NULL) 13920Sstevel@tonic-gate return (EFAULT); 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate mutex_enter(&lofi_lock); 13950Sstevel@tonic-gate if (byfilename) { 13960Sstevel@tonic-gate minor = file_to_minor(klip->li_filename); 13970Sstevel@tonic-gate } else { 13980Sstevel@tonic-gate minor = klip->li_minor; 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate if (minor == 0) { 14010Sstevel@tonic-gate mutex_exit(&lofi_lock); 14020Sstevel@tonic-gate free_lofi_ioctl(klip); 14030Sstevel@tonic-gate return (ENXIO); 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 14064451Seschrock if (lsp == NULL || lsp->ls_vp == NULL) { 14070Sstevel@tonic-gate mutex_exit(&lofi_lock); 14080Sstevel@tonic-gate free_lofi_ioctl(klip); 14090Sstevel@tonic-gate return (ENXIO); 14100Sstevel@tonic-gate } 14114451Seschrock 14126734Sjohnlev /* 14136734Sjohnlev * If it's still held open, we'll do one of three things: 14146734Sjohnlev * 14156734Sjohnlev * If no flag is set, just return EBUSY. 14166734Sjohnlev * 14176734Sjohnlev * If the 'cleanup' flag is set, unmap and remove the device when 14186734Sjohnlev * the last user finishes. 14196734Sjohnlev * 14206734Sjohnlev * If the 'force' flag is set, then we forcibly close the underlying 14216734Sjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl 14226734Sjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the 14236734Sjohnlev * device will be cleaned up appropriately. 14246734Sjohnlev * 14256734Sjohnlev * This is complicated by the fact that we may have outstanding 14266734Sjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all 14276734Sjohnlev * I/O, we keep a count of the number of outstanding I/O requests, as 14286734Sjohnlev * well as a flag to indicate that no new I/Os should be dispatched. 14296734Sjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0, 14306734Sjohnlev * and then close the underlying vnode. 14316734Sjohnlev */ 14326734Sjohnlev 14330Sstevel@tonic-gate if (is_opened(lsp)) { 14344451Seschrock if (klip->li_force) { 14354451Seschrock mutex_enter(&lsp->ls_vp_lock); 14364451Seschrock lsp->ls_vp_closereq = B_TRUE; 14374451Seschrock while (lsp->ls_vp_iocount > 0) 14384451Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 14394451Seschrock (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, 14405331Samw credp, NULL); 14414451Seschrock VN_RELE(lsp->ls_vp); 14424451Seschrock lsp->ls_vp = NULL; 14434451Seschrock cv_broadcast(&lsp->ls_vp_cv); 14444451Seschrock mutex_exit(&lsp->ls_vp_lock); 14454451Seschrock mutex_exit(&lofi_lock); 14464451Seschrock klip->li_minor = minor; 14474451Seschrock (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14484451Seschrock free_lofi_ioctl(klip); 14494451Seschrock return (0); 14506734Sjohnlev } else if (klip->li_cleanup) { 14516734Sjohnlev lsp->ls_cleanup = 1; 14526734Sjohnlev mutex_exit(&lofi_lock); 14536734Sjohnlev free_lofi_ioctl(klip); 14546734Sjohnlev return (0); 14554451Seschrock } 14566734Sjohnlev 14570Sstevel@tonic-gate mutex_exit(&lofi_lock); 14580Sstevel@tonic-gate free_lofi_ioctl(klip); 14590Sstevel@tonic-gate return (EBUSY); 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate 14624451Seschrock lofi_free_handle(dev, minor, lsp, credp); 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate klip->li_minor = minor; 14650Sstevel@tonic-gate mutex_exit(&lofi_lock); 14661657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 14670Sstevel@tonic-gate free_lofi_ioctl(klip); 14680Sstevel@tonic-gate return (0); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate /* 14720Sstevel@tonic-gate * get the filename given the minor number, or the minor number given 14730Sstevel@tonic-gate * the name. 14740Sstevel@tonic-gate */ 14754451Seschrock /*ARGSUSED*/ 14760Sstevel@tonic-gate static int 14770Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 14781657Sheppo struct cred *credp, int ioctl_flag) 14790Sstevel@tonic-gate { 14800Sstevel@tonic-gate struct lofi_state *lsp; 14810Sstevel@tonic-gate struct lofi_ioctl *klip; 14820Sstevel@tonic-gate int error; 14830Sstevel@tonic-gate minor_t minor; 14840Sstevel@tonic-gate 14851657Sheppo klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 14860Sstevel@tonic-gate if (klip == NULL) 14870Sstevel@tonic-gate return (EFAULT); 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate switch (which) { 14900Sstevel@tonic-gate case LOFI_GET_FILENAME: 14910Sstevel@tonic-gate minor = klip->li_minor; 14920Sstevel@tonic-gate if (minor == 0) { 14930Sstevel@tonic-gate free_lofi_ioctl(klip); 14940Sstevel@tonic-gate return (EINVAL); 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate mutex_enter(&lofi_lock); 14980Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 14990Sstevel@tonic-gate if (lsp == NULL) { 15000Sstevel@tonic-gate mutex_exit(&lofi_lock); 15010Sstevel@tonic-gate free_lofi_ioctl(klip); 15020Sstevel@tonic-gate return (ENXIO); 15030Sstevel@tonic-gate } 15040Sstevel@tonic-gate (void) strcpy(klip->li_filename, lsp->ls_filename); 15055643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 15065643Saalok sizeof (klip->li_algorithm)); 15070Sstevel@tonic-gate mutex_exit(&lofi_lock); 15081657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15090Sstevel@tonic-gate free_lofi_ioctl(klip); 15100Sstevel@tonic-gate return (error); 15110Sstevel@tonic-gate case LOFI_GET_MINOR: 15120Sstevel@tonic-gate mutex_enter(&lofi_lock); 15130Sstevel@tonic-gate klip->li_minor = file_to_minor(klip->li_filename); 15140Sstevel@tonic-gate mutex_exit(&lofi_lock); 15150Sstevel@tonic-gate if (klip->li_minor == 0) { 15160Sstevel@tonic-gate free_lofi_ioctl(klip); 15170Sstevel@tonic-gate return (ENOENT); 15180Sstevel@tonic-gate } 15191657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15200Sstevel@tonic-gate free_lofi_ioctl(klip); 15210Sstevel@tonic-gate return (error); 15225643Saalok case LOFI_CHECK_COMPRESSED: 15235643Saalok mutex_enter(&lofi_lock); 15245643Saalok klip->li_minor = file_to_minor(klip->li_filename); 15255643Saalok mutex_exit(&lofi_lock); 15265643Saalok if (klip->li_minor == 0) { 15275643Saalok free_lofi_ioctl(klip); 15285643Saalok return (ENOENT); 15295643Saalok } 15305643Saalok mutex_enter(&lofi_lock); 15315643Saalok lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 15325643Saalok if (lsp == NULL) { 15335643Saalok mutex_exit(&lofi_lock); 15345643Saalok free_lofi_ioctl(klip); 15355643Saalok return (ENXIO); 15365643Saalok } 15375643Saalok ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0); 15385643Saalok 15395643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 15405643Saalok sizeof (klip->li_algorithm)); 15415643Saalok mutex_exit(&lofi_lock); 15425643Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 15435643Saalok free_lofi_ioctl(klip); 15445643Saalok return (error); 15450Sstevel@tonic-gate default: 15460Sstevel@tonic-gate free_lofi_ioctl(klip); 15470Sstevel@tonic-gate return (EINVAL); 15480Sstevel@tonic-gate } 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate static int 15530Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 15540Sstevel@tonic-gate int *rvalp) 15550Sstevel@tonic-gate { 15560Sstevel@tonic-gate int error; 15570Sstevel@tonic-gate enum dkio_state dkstate; 15580Sstevel@tonic-gate struct lofi_state *lsp; 15590Sstevel@tonic-gate minor_t minor; 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate #ifdef lint 15620Sstevel@tonic-gate credp = credp; 15630Sstevel@tonic-gate #endif 15640Sstevel@tonic-gate 15650Sstevel@tonic-gate minor = getminor(dev); 15660Sstevel@tonic-gate /* lofi ioctls only apply to the master device */ 15670Sstevel@tonic-gate if (minor == 0) { 15680Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate /* 15710Sstevel@tonic-gate * the query command only need read-access - i.e., normal 15720Sstevel@tonic-gate * users are allowed to do those on the ctl device as 15730Sstevel@tonic-gate * long as they can open it read-only. 15740Sstevel@tonic-gate */ 15750Sstevel@tonic-gate switch (cmd) { 15760Sstevel@tonic-gate case LOFI_MAP_FILE: 15770Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15780Sstevel@tonic-gate return (EPERM); 15791657Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 15800Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR: 15810Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15820Sstevel@tonic-gate return (EPERM); 15831657Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 15840Sstevel@tonic-gate case LOFI_UNMAP_FILE: 15850Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15860Sstevel@tonic-gate return (EPERM); 15871657Sheppo return (lofi_unmap_file(dev, lip, 1, credp, flag)); 15880Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR: 15890Sstevel@tonic-gate if ((flag & FWRITE) == 0) 15900Sstevel@tonic-gate return (EPERM); 15911657Sheppo return (lofi_unmap_file(dev, lip, 0, credp, flag)); 15920Sstevel@tonic-gate case LOFI_GET_FILENAME: 15930Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 15941657Sheppo credp, flag)); 15950Sstevel@tonic-gate case LOFI_GET_MINOR: 15960Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 15971657Sheppo credp, flag)); 15980Sstevel@tonic-gate case LOFI_GET_MAXMINOR: 15991657Sheppo error = ddi_copyout(&lofi_max_files, &lip->li_minor, 16001657Sheppo sizeof (lofi_max_files), flag); 16010Sstevel@tonic-gate if (error) 16020Sstevel@tonic-gate return (EFAULT); 16030Sstevel@tonic-gate return (0); 16045643Saalok case LOFI_CHECK_COMPRESSED: 16055643Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 16065643Saalok credp, flag)); 16070Sstevel@tonic-gate default: 16080Sstevel@tonic-gate break; 16090Sstevel@tonic-gate } 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor); 16130Sstevel@tonic-gate if (lsp == NULL) 16140Sstevel@tonic-gate return (ENXIO); 16150Sstevel@tonic-gate 16164451Seschrock /* 16174451Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 16184451Seschrock * EIO as if the device was no longer present. 16194451Seschrock */ 16204451Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 16214451Seschrock return (EIO); 16224451Seschrock 16230Sstevel@tonic-gate /* these are for faking out utilities like newfs */ 16240Sstevel@tonic-gate switch (cmd) { 16250Sstevel@tonic-gate case DKIOCGVTOC: 16260Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) { 16270Sstevel@tonic-gate case DDI_MODEL_ILP32: { 16280Sstevel@tonic-gate struct vtoc32 vtoc32; 16290Sstevel@tonic-gate 16300Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32); 16310Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg, 16320Sstevel@tonic-gate sizeof (struct vtoc32), flag)) 16330Sstevel@tonic-gate return (EFAULT); 16340Sstevel@tonic-gate break; 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate case DDI_MODEL_NONE: 16380Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 16390Sstevel@tonic-gate sizeof (struct vtoc), flag)) 16400Sstevel@tonic-gate return (EFAULT); 16410Sstevel@tonic-gate break; 16420Sstevel@tonic-gate } 16430Sstevel@tonic-gate return (0); 16440Sstevel@tonic-gate case DKIOCINFO: 16451657Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg, 16461657Sheppo sizeof (struct dk_cinfo), flag); 16470Sstevel@tonic-gate if (error) 16480Sstevel@tonic-gate return (EFAULT); 16490Sstevel@tonic-gate return (0); 16500Sstevel@tonic-gate case DKIOCG_VIRTGEOM: 16510Sstevel@tonic-gate case DKIOCG_PHYGEOM: 16520Sstevel@tonic-gate case DKIOCGGEOM: 16531657Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 16541657Sheppo sizeof (struct dk_geom), flag); 16550Sstevel@tonic-gate if (error) 16560Sstevel@tonic-gate return (EFAULT); 16570Sstevel@tonic-gate return (0); 16580Sstevel@tonic-gate case DKIOCSTATE: 16594451Seschrock /* 16604451Seschrock * Normally, lofi devices are always in the INSERTED state. If 16614451Seschrock * a device is forcefully unmapped, then the device transitions 16624451Seschrock * to the DKIO_DEV_GONE state. 16634451Seschrock */ 16644451Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 16654451Seschrock flag) != 0) 16664451Seschrock return (EFAULT); 16674451Seschrock 16684451Seschrock mutex_enter(&lsp->ls_vp_lock); 16694451Seschrock while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 16704451Seschrock (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) { 16714451Seschrock /* 16724451Seschrock * By virtue of having the device open, we know that 16734451Seschrock * 'lsp' will remain valid when we return. 16744451Seschrock */ 16754451Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv, 16764451Seschrock &lsp->ls_vp_lock)) { 16774451Seschrock mutex_exit(&lsp->ls_vp_lock); 16784451Seschrock return (EINTR); 16794451Seschrock } 16804451Seschrock } 16814451Seschrock 16824451Seschrock dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE); 16834451Seschrock mutex_exit(&lsp->ls_vp_lock); 16844451Seschrock 16854451Seschrock if (ddi_copyout(&dkstate, (void *)arg, 16864451Seschrock sizeof (dkstate), flag) != 0) 16870Sstevel@tonic-gate return (EFAULT); 16880Sstevel@tonic-gate return (0); 16890Sstevel@tonic-gate default: 16900Sstevel@tonic-gate return (ENOTTY); 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = { 16950Sstevel@tonic-gate lofi_open, /* open */ 16960Sstevel@tonic-gate lofi_close, /* close */ 16970Sstevel@tonic-gate lofi_strategy, /* strategy */ 16980Sstevel@tonic-gate nodev, /* print */ 16990Sstevel@tonic-gate nodev, /* dump */ 17000Sstevel@tonic-gate lofi_read, /* read */ 17010Sstevel@tonic-gate lofi_write, /* write */ 17020Sstevel@tonic-gate lofi_ioctl, /* ioctl */ 17030Sstevel@tonic-gate nodev, /* devmap */ 17040Sstevel@tonic-gate nodev, /* mmap */ 17050Sstevel@tonic-gate nodev, /* segmap */ 17060Sstevel@tonic-gate nochpoll, /* poll */ 17070Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 17080Sstevel@tonic-gate 0, /* streamtab */ 17090Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 17100Sstevel@tonic-gate CB_REV, 17110Sstevel@tonic-gate lofi_aread, 17120Sstevel@tonic-gate lofi_awrite 17130Sstevel@tonic-gate }; 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate static struct dev_ops lofi_ops = { 17160Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 17170Sstevel@tonic-gate 0, /* refcnt */ 17180Sstevel@tonic-gate lofi_info, /* info */ 17190Sstevel@tonic-gate nulldev, /* identify */ 17200Sstevel@tonic-gate nulldev, /* probe */ 17210Sstevel@tonic-gate lofi_attach, /* attach */ 17220Sstevel@tonic-gate lofi_detach, /* detach */ 17230Sstevel@tonic-gate nodev, /* reset */ 17240Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */ 1725*7656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 1726*7656SSherry.Moore@Sun.COM NULL, /* power */ 1727*7656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 17280Sstevel@tonic-gate }; 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate static struct modldrv modldrv = { 17310Sstevel@tonic-gate &mod_driverops, 1732*7656SSherry.Moore@Sun.COM "loopback file driver", 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