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