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 /*
2212368SFrank.Batschulat@Sun.COM * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * lofi (loopback file) driver - allows you to attach a file to a device,
270Sstevel@tonic-gate * which can then be accessed through that device. The simple model is that
280Sstevel@tonic-gate * you tell lofi to open a file, and then use the block device you get as
290Sstevel@tonic-gate * you would any block device. lofi translates access to the block device
300Sstevel@tonic-gate * into I/O on the underlying file. This is mostly useful for
310Sstevel@tonic-gate * mounting images of filesystems.
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * lofi is controlled through /dev/lofictl - this is the only device exported
340Sstevel@tonic-gate * during attach, and is minor number 0. lofiadm communicates with lofi through
350Sstevel@tonic-gate * ioctls on this device. When a file is attached to lofi, block and character
360Sstevel@tonic-gate * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
370Sstevel@tonic-gate * are identified by their minor number, and the minor number is also used
380Sstevel@tonic-gate * as the name in /dev/lofi. If we ever decide to support virtual disks,
390Sstevel@tonic-gate * we'll have to divide the minor number space to identify fdisk partitions
400Sstevel@tonic-gate * and slices, and the name will then be the minor number shifted down a
410Sstevel@tonic-gate * few bits. Minor devices are tracked with state structures handled with
420Sstevel@tonic-gate * ddi_soft_state(9F) for simplicity.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * A file attached to lofi is opened when attached and not closed until
450Sstevel@tonic-gate * explicitly detached from lofi. This seems more sensible than deferring
460Sstevel@tonic-gate * the open until the /dev/lofi device is opened, for a number of reasons.
470Sstevel@tonic-gate * One is that any failure is likely to be noticed by the person (or script)
480Sstevel@tonic-gate * running lofiadm. Another is that it would be a security problem if the
490Sstevel@tonic-gate * file was replaced by another one after being added but before being opened.
500Sstevel@tonic-gate *
510Sstevel@tonic-gate * The only hard part about lofi is the ioctls. In order to support things
520Sstevel@tonic-gate * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
530Sstevel@tonic-gate * So it has to fake disk geometry and partition information. More may need
540Sstevel@tonic-gate * to be faked if your favorite utility doesn't work and you think it should
550Sstevel@tonic-gate * (fdformat doesn't work because it really wants to know the type of floppy
560Sstevel@tonic-gate * controller to talk to, and that didn't seem easy to fake. Or possibly even
570Sstevel@tonic-gate * necessary, since we have mkfs_pcfs now).
580Sstevel@tonic-gate *
594451Seschrock * Normally, a lofi device cannot be detached if it is open (i.e. busy). To
604451Seschrock * support simulation of hotplug events, an optional force flag is provided.
614451Seschrock * If a lofi device is open when a force detach is requested, then the
624451Seschrock * underlying file is closed and any subsequent operations return EIO. When the
634451Seschrock * device is closed for the last time, it will be cleaned up at that time. In
644451Seschrock * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
654451Seschrock * detached but not removed.
664451Seschrock *
670Sstevel@tonic-gate * Known problems:
680Sstevel@tonic-gate *
690Sstevel@tonic-gate * UFS logging. Mounting a UFS filesystem image "logging"
700Sstevel@tonic-gate * works for basic copy testing but wedges during a build of ON through
710Sstevel@tonic-gate * that image. Some deadlock in lufs holding the log mutex and then
720Sstevel@tonic-gate * getting stuck on a buf. So for now, don't do that.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * Direct I/O. Since the filesystem data is being cached in the buffer
750Sstevel@tonic-gate * cache, _and_ again in the underlying filesystem, it's tempting to
760Sstevel@tonic-gate * enable direct I/O on the underlying file. Don't, because that deadlocks.
770Sstevel@tonic-gate * I think to fix the cache-twice problem we might need filesystem support.
780Sstevel@tonic-gate *
790Sstevel@tonic-gate * Interesting things to do:
800Sstevel@tonic-gate *
810Sstevel@tonic-gate * Allow multiple files for each device. A poor-man's metadisk, basically.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * Pass-through ioctls on block devices. You can (though it's not
840Sstevel@tonic-gate * documented), give lofi a block device as a file name. Then we shouldn't
858313SDina.Nimeh@Sun.Com * need to fake a geometry, however, it may be relevant if you're replacing
868313SDina.Nimeh@Sun.Com * metadisk, or using lofi to get crypto.
878313SDina.Nimeh@Sun.Com * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
888313SDina.Nimeh@Sun.Com * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
898313SDina.Nimeh@Sun.Com * In fact this even makes sense if you have lofi "above" metadisk.
900Sstevel@tonic-gate *
918313SDina.Nimeh@Sun.Com * Encryption:
928313SDina.Nimeh@Sun.Com * Each lofi device can have its own symmetric key and cipher.
938313SDina.Nimeh@Sun.Com * They are passed to us by lofiadm(1m) in the correct format for use
948313SDina.Nimeh@Sun.Com * with the misc/kcf crypto_* routines.
958313SDina.Nimeh@Sun.Com *
968313SDina.Nimeh@Sun.Com * Each block has its own IV, that is calculated in lofi_blk_mech(), based
978313SDina.Nimeh@Sun.Com * on the "master" key held in the lsp and the block number of the buffer.
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>
12612633Sjohn.levon@sun.com #include <sys/id_space.h>
12712633Sjohn.levon@sun.com #include <sys/mkdev.h>
1288313SDina.Nimeh@Sun.Com #include <sys/crypto/common.h>
1298313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h>
13012633Sjohn.levon@sun.com #include <sys/rctl.h>
1318996SAlok.Aggarwal@Sun.COM #include <LzmaDec.h>
1328313SDina.Nimeh@Sun.Com
1338313SDina.Nimeh@Sun.Com /*
1348313SDina.Nimeh@Sun.Com * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h.
1358313SDina.Nimeh@Sun.Com * Crypto metadata, if it exists, is located at the end of the boot block
1368313SDina.Nimeh@Sun.Com * (BBOFF + BBSIZE, which is SBOFF). The super block and everything after
1378313SDina.Nimeh@Sun.Com * is offset by the size of the crypto metadata which is handled by
1388313SDina.Nimeh@Sun.Com * lsp->ls_crypto_offset.
1398313SDina.Nimeh@Sun.Com */
1408313SDina.Nimeh@Sun.Com #define CRYOFF ((off_t)8192)
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate #define NBLOCKS_PROP_NAME "Nblocks"
1435643Saalok #define SIZE_PROP_NAME "Size"
14412633Sjohn.levon@sun.com #define ZONE_PROP_NAME "zone"
1450Sstevel@tonic-gate
1468313SDina.Nimeh@Sun.Com #define SETUP_C_DATA(cd, buf, len) \
1478313SDina.Nimeh@Sun.Com (cd).cd_format = CRYPTO_DATA_RAW; \
1488313SDina.Nimeh@Sun.Com (cd).cd_offset = 0; \
1498313SDina.Nimeh@Sun.Com (cd).cd_miscdata = NULL; \
1508313SDina.Nimeh@Sun.Com (cd).cd_length = (len); \
1518313SDina.Nimeh@Sun.Com (cd).cd_raw.iov_base = (buf); \
1528313SDina.Nimeh@Sun.Com (cd).cd_raw.iov_len = (len);
1538313SDina.Nimeh@Sun.Com
1548313SDina.Nimeh@Sun.Com #define UIO_CHECK(uio) \
1558313SDina.Nimeh@Sun.Com if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
1568313SDina.Nimeh@Sun.Com ((uio)->uio_resid % DEV_BSIZE) != 0) { \
1578313SDina.Nimeh@Sun.Com return (EINVAL); \
1588313SDina.Nimeh@Sun.Com }
1598313SDina.Nimeh@Sun.Com
1608313SDina.Nimeh@Sun.Com static dev_info_t *lofi_dip = NULL;
1618313SDina.Nimeh@Sun.Com static void *lofi_statep = NULL;
1620Sstevel@tonic-gate static kmutex_t lofi_lock; /* state lock */
16312633Sjohn.levon@sun.com static id_space_t *lofi_minor_id;
16412633Sjohn.levon@sun.com static list_t lofi_list;
16512633Sjohn.levon@sun.com static zone_key_t lofi_zone_key;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * Because lofi_taskq_nthreads limits the actual swamping of the device, the
1690Sstevel@tonic-gate * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
1700Sstevel@tonic-gate * high. If we want to be assured that the underlying device is always busy,
1710Sstevel@tonic-gate * we must be sure that the number of bytes enqueued when the number of
1720Sstevel@tonic-gate * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
1730Sstevel@tonic-gate * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should
1740Sstevel@tonic-gate * set maxalloc to be the maximum throughput (in bytes per second) of the
1750Sstevel@tonic-gate * underlying device divided by the minimum I/O size. We assume a realistic
1760Sstevel@tonic-gate * maximum throughput of one hundred megabytes per second; we set maxalloc on
1770Sstevel@tonic-gate * the lofi task queue to be 104857600 divided by DEV_BSIZE.
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
1800Sstevel@tonic-gate static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */
1810Sstevel@tonic-gate
1828313SDina.Nimeh@Sun.Com const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
1830Sstevel@tonic-gate
1849048Sjrgn.keil@googlemail.com /*
1859048Sjrgn.keil@googlemail.com * To avoid decompressing data in a compressed segment multiple times
1869048Sjrgn.keil@googlemail.com * when accessing small parts of a segment's data, we cache and reuse
1879048Sjrgn.keil@googlemail.com * the uncompressed segment's data.
1889048Sjrgn.keil@googlemail.com *
1899048Sjrgn.keil@googlemail.com * A single cached segment is sufficient to avoid lots of duplicate
1909048Sjrgn.keil@googlemail.com * segment decompress operations. A small cache size also reduces the
1919048Sjrgn.keil@googlemail.com * memory footprint.
1929048Sjrgn.keil@googlemail.com *
1939048Sjrgn.keil@googlemail.com * lofi_max_comp_cache is the maximum number of decompressed data segments
1949048Sjrgn.keil@googlemail.com * cached for each compressed lofi image. It can be set to 0 to disable
1959048Sjrgn.keil@googlemail.com * caching.
1969048Sjrgn.keil@googlemail.com */
1979048Sjrgn.keil@googlemail.com
1989048Sjrgn.keil@googlemail.com uint32_t lofi_max_comp_cache = 1;
1999048Sjrgn.keil@googlemail.com
2005643Saalok static int gzip_decompress(void *src, size_t srclen, void *dst,
2015643Saalok size_t *destlen, int level);
2025643Saalok
2038996SAlok.Aggarwal@Sun.COM static int lzma_decompress(void *src, size_t srclen, void *dst,
2048996SAlok.Aggarwal@Sun.COM size_t *dstlen, int level);
2058996SAlok.Aggarwal@Sun.COM
2065643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
2075643Saalok {gzip_decompress, NULL, 6, "gzip"}, /* default */
2085643Saalok {gzip_decompress, NULL, 6, "gzip-6"},
2098996SAlok.Aggarwal@Sun.COM {gzip_decompress, NULL, 9, "gzip-9"},
2108996SAlok.Aggarwal@Sun.COM {lzma_decompress, NULL, 0, "lzma"}
2115643Saalok };
2125643Saalok
2138996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2148996SAlok.Aggarwal@Sun.COM static void
SzAlloc(void * p,size_t size)2158996SAlok.Aggarwal@Sun.COM *SzAlloc(void *p, size_t size)
2168996SAlok.Aggarwal@Sun.COM {
2178996SAlok.Aggarwal@Sun.COM return (kmem_alloc(size, KM_SLEEP));
2188996SAlok.Aggarwal@Sun.COM }
2198996SAlok.Aggarwal@Sun.COM
2208996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2218996SAlok.Aggarwal@Sun.COM static void
SzFree(void * p,void * address,size_t size)2228996SAlok.Aggarwal@Sun.COM SzFree(void *p, void *address, size_t size)
2238996SAlok.Aggarwal@Sun.COM {
2248996SAlok.Aggarwal@Sun.COM kmem_free(address, size);
2258996SAlok.Aggarwal@Sun.COM }
2268996SAlok.Aggarwal@Sun.COM
2278996SAlok.Aggarwal@Sun.COM static ISzAlloc g_Alloc = { SzAlloc, SzFree };
2288996SAlok.Aggarwal@Sun.COM
2299048Sjrgn.keil@googlemail.com /*
2309048Sjrgn.keil@googlemail.com * Free data referenced by the linked list of cached uncompressed
2319048Sjrgn.keil@googlemail.com * segments.
2329048Sjrgn.keil@googlemail.com */
2339048Sjrgn.keil@googlemail.com static void
lofi_free_comp_cache(struct lofi_state * lsp)2349048Sjrgn.keil@googlemail.com lofi_free_comp_cache(struct lofi_state *lsp)
2359048Sjrgn.keil@googlemail.com {
2369048Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
2379048Sjrgn.keil@googlemail.com
2389048Sjrgn.keil@googlemail.com while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
2399048Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
2409048Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache));
2419048Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--;
2429048Sjrgn.keil@googlemail.com }
2439048Sjrgn.keil@googlemail.com ASSERT(lsp->ls_comp_cache_count == 0);
2449048Sjrgn.keil@googlemail.com }
2459048Sjrgn.keil@googlemail.com
2460Sstevel@tonic-gate static int
is_opened(struct lofi_state * lsp)2470Sstevel@tonic-gate is_opened(struct lofi_state *lsp)
2480Sstevel@tonic-gate {
24912633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
2500Sstevel@tonic-gate return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate static int
mark_opened(struct lofi_state * lsp,int otyp)2540Sstevel@tonic-gate mark_opened(struct lofi_state *lsp, int otyp)
2550Sstevel@tonic-gate {
25612633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
2570Sstevel@tonic-gate switch (otyp) {
2580Sstevel@tonic-gate case OTYP_CHR:
2590Sstevel@tonic-gate lsp->ls_chr_open = 1;
2600Sstevel@tonic-gate break;
2610Sstevel@tonic-gate case OTYP_BLK:
2620Sstevel@tonic-gate lsp->ls_blk_open = 1;
2630Sstevel@tonic-gate break;
2640Sstevel@tonic-gate case OTYP_LYR:
2650Sstevel@tonic-gate lsp->ls_lyr_open_count++;
2660Sstevel@tonic-gate break;
2670Sstevel@tonic-gate default:
2680Sstevel@tonic-gate return (-1);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate static void
mark_closed(struct lofi_state * lsp,int otyp)2740Sstevel@tonic-gate mark_closed(struct lofi_state *lsp, int otyp)
2750Sstevel@tonic-gate {
27612633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
2770Sstevel@tonic-gate switch (otyp) {
2780Sstevel@tonic-gate case OTYP_CHR:
2790Sstevel@tonic-gate lsp->ls_chr_open = 0;
2800Sstevel@tonic-gate break;
2810Sstevel@tonic-gate case OTYP_BLK:
2820Sstevel@tonic-gate lsp->ls_blk_open = 0;
2830Sstevel@tonic-gate break;
2840Sstevel@tonic-gate case OTYP_LYR:
2850Sstevel@tonic-gate lsp->ls_lyr_open_count--;
2860Sstevel@tonic-gate break;
2870Sstevel@tonic-gate default:
2880Sstevel@tonic-gate break;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2924451Seschrock static void
lofi_free_crypto(struct lofi_state * lsp)2938313SDina.Nimeh@Sun.Com lofi_free_crypto(struct lofi_state *lsp)
2948313SDina.Nimeh@Sun.Com {
29512633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
2968313SDina.Nimeh@Sun.Com
2978313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) {
2988313SDina.Nimeh@Sun.Com /*
2998313SDina.Nimeh@Sun.Com * Clean up the crypto state so that it doesn't hang around
3008313SDina.Nimeh@Sun.Com * in memory after we are done with it.
3018313SDina.Nimeh@Sun.Com */
30212633Sjohn.levon@sun.com if (lsp->ls_key.ck_data != NULL) {
30312633Sjohn.levon@sun.com bzero(lsp->ls_key.ck_data,
30412633Sjohn.levon@sun.com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
30512633Sjohn.levon@sun.com kmem_free(lsp->ls_key.ck_data,
30612633Sjohn.levon@sun.com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
30712633Sjohn.levon@sun.com lsp->ls_key.ck_data = NULL;
30812633Sjohn.levon@sun.com lsp->ls_key.ck_length = 0;
30912633Sjohn.levon@sun.com }
3108313SDina.Nimeh@Sun.Com
3118313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param != NULL) {
3128313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_mech.cm_param,
3138313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len);
3148313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param = NULL;
3158313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len = 0;
3168313SDina.Nimeh@Sun.Com }
3178313SDina.Nimeh@Sun.Com
3188313SDina.Nimeh@Sun.Com if (lsp->ls_iv_mech.cm_param != NULL) {
3198313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_iv_mech.cm_param,
3208313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param_len);
3218313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param = NULL;
3228313SDina.Nimeh@Sun.Com lsp->ls_iv_mech.cm_param_len = 0;
3238313SDina.Nimeh@Sun.Com }
3248313SDina.Nimeh@Sun.Com
3258313SDina.Nimeh@Sun.Com mutex_destroy(&lsp->ls_crypto_lock);
3268313SDina.Nimeh@Sun.Com }
3278313SDina.Nimeh@Sun.Com }
3288313SDina.Nimeh@Sun.Com
3298313SDina.Nimeh@Sun.Com static void
lofi_destroy(struct lofi_state * lsp,cred_t * credp)33012633Sjohn.levon@sun.com lofi_destroy(struct lofi_state *lsp, cred_t *credp)
3314451Seschrock {
33212633Sjohn.levon@sun.com minor_t minor = getminor(lsp->ls_dev);
33312633Sjohn.levon@sun.com int i;
3344451Seschrock
33512633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
33612633Sjohn.levon@sun.com
33712633Sjohn.levon@sun.com list_remove(&lofi_list, lsp);
3388313SDina.Nimeh@Sun.Com
3398313SDina.Nimeh@Sun.Com lofi_free_crypto(lsp);
3408313SDina.Nimeh@Sun.Com
34112384Salok.aggarwal@oracle.com /*
34212384Salok.aggarwal@oracle.com * Free pre-allocated compressed buffers
34312384Salok.aggarwal@oracle.com */
34412384Salok.aggarwal@oracle.com if (lsp->ls_comp_bufs != NULL) {
34512384Salok.aggarwal@oracle.com for (i = 0; i < lofi_taskq_nthreads; i++) {
34612384Salok.aggarwal@oracle.com if (lsp->ls_comp_bufs[i].bufsize > 0)
34712384Salok.aggarwal@oracle.com kmem_free(lsp->ls_comp_bufs[i].buf,
34812384Salok.aggarwal@oracle.com lsp->ls_comp_bufs[i].bufsize);
34912384Salok.aggarwal@oracle.com }
35012384Salok.aggarwal@oracle.com kmem_free(lsp->ls_comp_bufs,
35112384Salok.aggarwal@oracle.com sizeof (struct compbuf) * lofi_taskq_nthreads);
35212633Sjohn.levon@sun.com }
35312633Sjohn.levon@sun.com
35412633Sjohn.levon@sun.com (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
35512633Sjohn.levon@sun.com 1, 0, credp, NULL);
35612633Sjohn.levon@sun.com VN_RELE(lsp->ls_vp);
35712633Sjohn.levon@sun.com if (lsp->ls_stacked_vp != lsp->ls_vp)
35812633Sjohn.levon@sun.com VN_RELE(lsp->ls_stacked_vp);
35912633Sjohn.levon@sun.com
36012633Sjohn.levon@sun.com taskq_destroy(lsp->ls_taskq);
36112633Sjohn.levon@sun.com
36212633Sjohn.levon@sun.com if (lsp->ls_kstat != NULL)
36312633Sjohn.levon@sun.com kstat_delete(lsp->ls_kstat);
36412633Sjohn.levon@sun.com
36512633Sjohn.levon@sun.com /*
36612633Sjohn.levon@sun.com * Free cached decompressed segment data
36712633Sjohn.levon@sun.com */
36812633Sjohn.levon@sun.com lofi_free_comp_cache(lsp);
36912633Sjohn.levon@sun.com list_destroy(&lsp->ls_comp_cache);
37012633Sjohn.levon@sun.com
37112633Sjohn.levon@sun.com if (lsp->ls_uncomp_seg_sz > 0) {
37212633Sjohn.levon@sun.com kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
37312633Sjohn.levon@sun.com lsp->ls_uncomp_seg_sz = 0;
37412384Salok.aggarwal@oracle.com }
37512384Salok.aggarwal@oracle.com
376*13096SJordan.Vaughan@Sun.com rctl_decr_lofi(lsp->ls_zone.zref_zone, 1);
377*13096SJordan.Vaughan@Sun.com zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
37812633Sjohn.levon@sun.com
37912633Sjohn.levon@sun.com mutex_destroy(&lsp->ls_comp_cache_lock);
38012633Sjohn.levon@sun.com mutex_destroy(&lsp->ls_comp_bufs_lock);
38112633Sjohn.levon@sun.com mutex_destroy(&lsp->ls_kstat_lock);
3829048Sjrgn.keil@googlemail.com mutex_destroy(&lsp->ls_vp_lock);
3839048Sjrgn.keil@googlemail.com
38412633Sjohn.levon@sun.com ASSERT(ddi_get_soft_state(lofi_statep, minor) == lsp);
3854451Seschrock ddi_soft_state_free(lofi_statep, minor);
38612633Sjohn.levon@sun.com id_free(lofi_minor_id, minor);
38712633Sjohn.levon@sun.com }
38812633Sjohn.levon@sun.com
38912633Sjohn.levon@sun.com static void
lofi_free_dev(dev_t dev)39012633Sjohn.levon@sun.com lofi_free_dev(dev_t dev)
39112633Sjohn.levon@sun.com {
39212633Sjohn.levon@sun.com minor_t minor = getminor(dev);
39312633Sjohn.levon@sun.com char namebuf[50];
39412633Sjohn.levon@sun.com
39512633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
39612633Sjohn.levon@sun.com
39712633Sjohn.levon@sun.com (void) ddi_prop_remove(dev, lofi_dip, ZONE_PROP_NAME);
39812633Sjohn.levon@sun.com (void) ddi_prop_remove(dev, lofi_dip, SIZE_PROP_NAME);
39912633Sjohn.levon@sun.com (void) ddi_prop_remove(dev, lofi_dip, NBLOCKS_PROP_NAME);
40012633Sjohn.levon@sun.com
40112633Sjohn.levon@sun.com (void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
40212633Sjohn.levon@sun.com ddi_remove_minor_node(lofi_dip, namebuf);
40312633Sjohn.levon@sun.com (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
40412633Sjohn.levon@sun.com ddi_remove_minor_node(lofi_dip, namebuf);
40512633Sjohn.levon@sun.com }
40612633Sjohn.levon@sun.com
40712633Sjohn.levon@sun.com /*ARGSUSED*/
40812633Sjohn.levon@sun.com static void
lofi_zone_shutdown(zoneid_t zoneid,void * arg)40912633Sjohn.levon@sun.com lofi_zone_shutdown(zoneid_t zoneid, void *arg)
41012633Sjohn.levon@sun.com {
41112633Sjohn.levon@sun.com struct lofi_state *lsp;
41212633Sjohn.levon@sun.com struct lofi_state *next;
41312633Sjohn.levon@sun.com
41412633Sjohn.levon@sun.com mutex_enter(&lofi_lock);
41512633Sjohn.levon@sun.com
41612633Sjohn.levon@sun.com for (lsp = list_head(&lofi_list); lsp != NULL; lsp = next) {
41712633Sjohn.levon@sun.com
41812633Sjohn.levon@sun.com /* lofi_destroy() frees lsp */
41912633Sjohn.levon@sun.com next = list_next(&lofi_list, lsp);
42012633Sjohn.levon@sun.com
421*13096SJordan.Vaughan@Sun.com if (lsp->ls_zone.zref_zone->zone_id != zoneid)
42212633Sjohn.levon@sun.com continue;
42312633Sjohn.levon@sun.com
42412633Sjohn.levon@sun.com /*
42512633Sjohn.levon@sun.com * No in-zone processes are running, but something has this
42612633Sjohn.levon@sun.com * open. It's either a global zone process, or a lofi
42712633Sjohn.levon@sun.com * mount. In either case we set ls_cleanup so the last
42812633Sjohn.levon@sun.com * user destroys the device.
42912633Sjohn.levon@sun.com */
43012633Sjohn.levon@sun.com if (is_opened(lsp)) {
43112633Sjohn.levon@sun.com lsp->ls_cleanup = 1;
43212633Sjohn.levon@sun.com } else {
43312633Sjohn.levon@sun.com lofi_free_dev(lsp->ls_dev);
43412633Sjohn.levon@sun.com lofi_destroy(lsp, kcred);
43512633Sjohn.levon@sun.com }
43612633Sjohn.levon@sun.com }
43712633Sjohn.levon@sun.com
43812633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
4394451Seschrock }
4404451Seschrock
4414451Seschrock /*ARGSUSED*/
4420Sstevel@tonic-gate static int
lofi_open(dev_t * devp,int flag,int otyp,struct cred * credp)4430Sstevel@tonic-gate lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate minor_t minor;
4460Sstevel@tonic-gate struct lofi_state *lsp;
4470Sstevel@tonic-gate
44812633Sjohn.levon@sun.com /*
44912633Sjohn.levon@sun.com * lofiadm -a /dev/lofi/1 gets us here.
45012633Sjohn.levon@sun.com */
45112633Sjohn.levon@sun.com if (mutex_owner(&lofi_lock) == curthread)
45212633Sjohn.levon@sun.com return (EINVAL);
45312633Sjohn.levon@sun.com
4540Sstevel@tonic-gate mutex_enter(&lofi_lock);
45512633Sjohn.levon@sun.com
4560Sstevel@tonic-gate minor = getminor(*devp);
45712633Sjohn.levon@sun.com
45812633Sjohn.levon@sun.com /* master control device */
4590Sstevel@tonic-gate if (minor == 0) {
4600Sstevel@tonic-gate mutex_exit(&lofi_lock);
4610Sstevel@tonic-gate return (0);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate /* otherwise, the mapping should already exist */
4650Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor);
4660Sstevel@tonic-gate if (lsp == NULL) {
4670Sstevel@tonic-gate mutex_exit(&lofi_lock);
4680Sstevel@tonic-gate return (EINVAL);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4714451Seschrock if (lsp->ls_vp == NULL) {
4724451Seschrock mutex_exit(&lofi_lock);
4734451Seschrock return (ENXIO);
4744451Seschrock }
4754451Seschrock
4760Sstevel@tonic-gate if (mark_opened(lsp, otyp) == -1) {
4770Sstevel@tonic-gate mutex_exit(&lofi_lock);
4780Sstevel@tonic-gate return (EINVAL);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate mutex_exit(&lofi_lock);
4820Sstevel@tonic-gate return (0);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4854451Seschrock /*ARGSUSED*/
4860Sstevel@tonic-gate static int
lofi_close(dev_t dev,int flag,int otyp,struct cred * credp)4870Sstevel@tonic-gate lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate minor_t minor;
4900Sstevel@tonic-gate struct lofi_state *lsp;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate mutex_enter(&lofi_lock);
4930Sstevel@tonic-gate minor = getminor(dev);
4940Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor);
4950Sstevel@tonic-gate if (lsp == NULL) {
4960Sstevel@tonic-gate mutex_exit(&lofi_lock);
4970Sstevel@tonic-gate return (EINVAL);
4980Sstevel@tonic-gate }
49912633Sjohn.levon@sun.com
50012633Sjohn.levon@sun.com if (minor == 0) {
50112633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
50212633Sjohn.levon@sun.com return (0);
50312633Sjohn.levon@sun.com }
50412633Sjohn.levon@sun.com
5050Sstevel@tonic-gate mark_closed(lsp, otyp);
5064451Seschrock
5074451Seschrock /*
5086734Sjohnlev * If we forcibly closed the underlying device (li_force), or
5096734Sjohnlev * asked for cleanup (li_cleanup), finish up if we're the last
5106734Sjohnlev * out of the door.
5114451Seschrock */
51212633Sjohn.levon@sun.com if (!is_opened(lsp) && (lsp->ls_cleanup || lsp->ls_vp == NULL)) {
51312911Sjohn.levon@sun.com lofi_free_dev(lsp->ls_dev);
51412633Sjohn.levon@sun.com lofi_destroy(lsp, credp);
51512633Sjohn.levon@sun.com }
5166734Sjohnlev
5170Sstevel@tonic-gate mutex_exit(&lofi_lock);
5180Sstevel@tonic-gate return (0);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate
5218313SDina.Nimeh@Sun.Com /*
5228313SDina.Nimeh@Sun.Com * Sets the mechanism's initialization vector (IV) if one is needed.
5238313SDina.Nimeh@Sun.Com * The IV is computed from the data block number. lsp->ls_mech is
5248313SDina.Nimeh@Sun.Com * altered so that:
5258313SDina.Nimeh@Sun.Com * lsp->ls_mech.cm_param_len is set to the IV len.
5268313SDina.Nimeh@Sun.Com * lsp->ls_mech.cm_param is set to the IV.
5278313SDina.Nimeh@Sun.Com */
5288313SDina.Nimeh@Sun.Com static int
lofi_blk_mech(struct lofi_state * lsp,longlong_t lblkno)5298313SDina.Nimeh@Sun.Com lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
5308313SDina.Nimeh@Sun.Com {
5318313SDina.Nimeh@Sun.Com int ret;
5328313SDina.Nimeh@Sun.Com crypto_data_t cdata;
5338313SDina.Nimeh@Sun.Com char *iv;
5348313SDina.Nimeh@Sun.Com size_t iv_len;
5358313SDina.Nimeh@Sun.Com size_t min;
5368313SDina.Nimeh@Sun.Com void *data;
5378313SDina.Nimeh@Sun.Com size_t datasz;
5388313SDina.Nimeh@Sun.Com
53912633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lsp->ls_crypto_lock));
5408313SDina.Nimeh@Sun.Com
5418313SDina.Nimeh@Sun.Com if (lsp == NULL)
5428313SDina.Nimeh@Sun.Com return (CRYPTO_DEVICE_ERROR);
5438313SDina.Nimeh@Sun.Com
5448313SDina.Nimeh@Sun.Com /* lsp->ls_mech.cm_param{_len} has already been set for static iv */
5458313SDina.Nimeh@Sun.Com if (lsp->ls_iv_type == IVM_NONE) {
5468313SDina.Nimeh@Sun.Com return (CRYPTO_SUCCESS);
5478313SDina.Nimeh@Sun.Com }
5488313SDina.Nimeh@Sun.Com
5498313SDina.Nimeh@Sun.Com /*
5508313SDina.Nimeh@Sun.Com * if kmem already alloced from previous call and it's the same size
5518313SDina.Nimeh@Sun.Com * we need now, just recycle it; allocate new kmem only if we have to
5528313SDina.Nimeh@Sun.Com */
5538313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param == NULL ||
5548313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
5558313SDina.Nimeh@Sun.Com iv_len = lsp->ls_iv_len;
5568313SDina.Nimeh@Sun.Com iv = kmem_zalloc(iv_len, KM_SLEEP);
5578313SDina.Nimeh@Sun.Com } else {
5588313SDina.Nimeh@Sun.Com iv_len = lsp->ls_mech.cm_param_len;
5598313SDina.Nimeh@Sun.Com iv = lsp->ls_mech.cm_param;
5608313SDina.Nimeh@Sun.Com bzero(iv, iv_len);
5618313SDina.Nimeh@Sun.Com }
5628313SDina.Nimeh@Sun.Com
5638313SDina.Nimeh@Sun.Com switch (lsp->ls_iv_type) {
5648313SDina.Nimeh@Sun.Com case IVM_ENC_BLKNO:
5658313SDina.Nimeh@Sun.Com /* iv is not static, lblkno changes each time */
5668313SDina.Nimeh@Sun.Com data = &lblkno;
5678313SDina.Nimeh@Sun.Com datasz = sizeof (lblkno);
5688313SDina.Nimeh@Sun.Com break;
5698313SDina.Nimeh@Sun.Com default:
5708313SDina.Nimeh@Sun.Com data = 0;
5718313SDina.Nimeh@Sun.Com datasz = 0;
5728313SDina.Nimeh@Sun.Com break;
5738313SDina.Nimeh@Sun.Com }
5748313SDina.Nimeh@Sun.Com
5758313SDina.Nimeh@Sun.Com /*
5768313SDina.Nimeh@Sun.Com * write blkno into the iv buffer padded on the left in case
5778313SDina.Nimeh@Sun.Com * blkno ever grows bigger than its current longlong_t size
5788313SDina.Nimeh@Sun.Com * or a variation other than blkno is used for the iv data
5798313SDina.Nimeh@Sun.Com */
5808313SDina.Nimeh@Sun.Com min = MIN(datasz, iv_len);
5818313SDina.Nimeh@Sun.Com bcopy(data, iv + (iv_len - min), min);
5828313SDina.Nimeh@Sun.Com
5838313SDina.Nimeh@Sun.Com /* encrypt the data in-place to get the IV */
5848313SDina.Nimeh@Sun.Com SETUP_C_DATA(cdata, iv, iv_len);
5858313SDina.Nimeh@Sun.Com
5868313SDina.Nimeh@Sun.Com ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
5878313SDina.Nimeh@Sun.Com NULL, NULL, NULL);
5888313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS) {
5898313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
5908313SDina.Nimeh@Sun.Com lblkno, ret);
5918313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param != iv)
5928313SDina.Nimeh@Sun.Com kmem_free(iv, iv_len);
5938996SAlok.Aggarwal@Sun.COM
5948313SDina.Nimeh@Sun.Com return (ret);
5958313SDina.Nimeh@Sun.Com }
5968313SDina.Nimeh@Sun.Com
5978313SDina.Nimeh@Sun.Com /* clean up the iv from the last computation */
5988313SDina.Nimeh@Sun.Com if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
5998313SDina.Nimeh@Sun.Com kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
6008996SAlok.Aggarwal@Sun.COM
6018313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param_len = iv_len;
6028313SDina.Nimeh@Sun.Com lsp->ls_mech.cm_param = iv;
6038313SDina.Nimeh@Sun.Com
6048313SDina.Nimeh@Sun.Com return (CRYPTO_SUCCESS);
6058313SDina.Nimeh@Sun.Com }
6068313SDina.Nimeh@Sun.Com
6078313SDina.Nimeh@Sun.Com /*
6088313SDina.Nimeh@Sun.Com * Performs encryption and decryption of a chunk of data of size "len",
6098313SDina.Nimeh@Sun.Com * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of
6108313SDina.Nimeh@Sun.Com * DEV_BSIZE.
6118313SDina.Nimeh@Sun.Com */
6128313SDina.Nimeh@Sun.Com static int
lofi_crypto(struct lofi_state * lsp,struct buf * bp,caddr_t plaintext,caddr_t ciphertext,size_t len,boolean_t op_encrypt)6138313SDina.Nimeh@Sun.Com lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
6148313SDina.Nimeh@Sun.Com caddr_t ciphertext, size_t len, boolean_t op_encrypt)
6158313SDina.Nimeh@Sun.Com {
6168313SDina.Nimeh@Sun.Com crypto_data_t cdata;
6178313SDina.Nimeh@Sun.Com crypto_data_t wdata;
6188313SDina.Nimeh@Sun.Com int ret;
6198313SDina.Nimeh@Sun.Com longlong_t lblkno = bp->b_lblkno;
6208313SDina.Nimeh@Sun.Com
6218313SDina.Nimeh@Sun.Com mutex_enter(&lsp->ls_crypto_lock);
6228313SDina.Nimeh@Sun.Com
6238313SDina.Nimeh@Sun.Com /*
6248313SDina.Nimeh@Sun.Com * though we could encrypt/decrypt entire "len" chunk of data, we need
6258313SDina.Nimeh@Sun.Com * to break it into DEV_BSIZE pieces to capture blkno incrementing
6268313SDina.Nimeh@Sun.Com */
6278313SDina.Nimeh@Sun.Com SETUP_C_DATA(cdata, plaintext, len);
6288313SDina.Nimeh@Sun.Com cdata.cd_length = DEV_BSIZE;
6298313SDina.Nimeh@Sun.Com if (ciphertext != NULL) { /* not in-place crypto */
6308313SDina.Nimeh@Sun.Com SETUP_C_DATA(wdata, ciphertext, len);
6318313SDina.Nimeh@Sun.Com wdata.cd_length = DEV_BSIZE;
6328313SDina.Nimeh@Sun.Com }
6338313SDina.Nimeh@Sun.Com
6348313SDina.Nimeh@Sun.Com do {
6358313SDina.Nimeh@Sun.Com ret = lofi_blk_mech(lsp, lblkno);
6368313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS)
6378313SDina.Nimeh@Sun.Com continue;
6388313SDina.Nimeh@Sun.Com
6398313SDina.Nimeh@Sun.Com if (op_encrypt) {
6408313SDina.Nimeh@Sun.Com ret = crypto_encrypt(&lsp->ls_mech, &cdata,
6418313SDina.Nimeh@Sun.Com &lsp->ls_key, NULL,
6428313SDina.Nimeh@Sun.Com ((ciphertext != NULL) ? &wdata : NULL), NULL);
6438313SDina.Nimeh@Sun.Com } else {
6448313SDina.Nimeh@Sun.Com ret = crypto_decrypt(&lsp->ls_mech, &cdata,
6458313SDina.Nimeh@Sun.Com &lsp->ls_key, NULL,
6468313SDina.Nimeh@Sun.Com ((ciphertext != NULL) ? &wdata : NULL), NULL);
6478313SDina.Nimeh@Sun.Com }
6488313SDina.Nimeh@Sun.Com
6498313SDina.Nimeh@Sun.Com cdata.cd_offset += DEV_BSIZE;
6508313SDina.Nimeh@Sun.Com if (ciphertext != NULL)
6518313SDina.Nimeh@Sun.Com wdata.cd_offset += DEV_BSIZE;
6528313SDina.Nimeh@Sun.Com lblkno++;
6538313SDina.Nimeh@Sun.Com } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
6548313SDina.Nimeh@Sun.Com
6558313SDina.Nimeh@Sun.Com mutex_exit(&lsp->ls_crypto_lock);
6568313SDina.Nimeh@Sun.Com
6578313SDina.Nimeh@Sun.Com if (ret != CRYPTO_SUCCESS) {
6588313SDina.Nimeh@Sun.Com cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)",
6598313SDina.Nimeh@Sun.Com op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
6608313SDina.Nimeh@Sun.Com lblkno, ret);
6618313SDina.Nimeh@Sun.Com }
6628313SDina.Nimeh@Sun.Com
6638313SDina.Nimeh@Sun.Com return (ret);
6648313SDina.Nimeh@Sun.Com }
6658313SDina.Nimeh@Sun.Com
6668313SDina.Nimeh@Sun.Com #define RDWR_RAW 1
6678313SDina.Nimeh@Sun.Com #define RDWR_BCOPY 2
6688313SDina.Nimeh@Sun.Com
6698313SDina.Nimeh@Sun.Com static int
lofi_rdwr(caddr_t bufaddr,offset_t offset,struct buf * bp,struct lofi_state * lsp,size_t len,int method,caddr_t bcopy_locn)6708313SDina.Nimeh@Sun.Com lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
6718313SDina.Nimeh@Sun.Com struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
6728313SDina.Nimeh@Sun.Com {
6738313SDina.Nimeh@Sun.Com ssize_t resid;
6748313SDina.Nimeh@Sun.Com int isread;
6758313SDina.Nimeh@Sun.Com int error;
6768313SDina.Nimeh@Sun.Com
6778313SDina.Nimeh@Sun.Com /*
6788313SDina.Nimeh@Sun.Com * Handles reads/writes for both plain and encrypted lofi
6798313SDina.Nimeh@Sun.Com * Note: offset is already shifted by lsp->ls_crypto_offset
6808313SDina.Nimeh@Sun.Com * when it gets here.
6818313SDina.Nimeh@Sun.Com */
6828313SDina.Nimeh@Sun.Com
6838313SDina.Nimeh@Sun.Com isread = bp->b_flags & B_READ;
6848313SDina.Nimeh@Sun.Com if (isread) {
6858313SDina.Nimeh@Sun.Com if (method == RDWR_BCOPY) {
6868313SDina.Nimeh@Sun.Com /* DO NOT update bp->b_resid for bcopy */
6878313SDina.Nimeh@Sun.Com bcopy(bcopy_locn, bufaddr, len);
6888313SDina.Nimeh@Sun.Com error = 0;
6898313SDina.Nimeh@Sun.Com } else { /* RDWR_RAW */
6908313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
6918313SDina.Nimeh@Sun.Com offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
6928313SDina.Nimeh@Sun.Com &resid);
6938313SDina.Nimeh@Sun.Com bp->b_resid = resid;
6948313SDina.Nimeh@Sun.Com }
6958313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled && error == 0) {
6968313SDina.Nimeh@Sun.Com if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
6978313SDina.Nimeh@Sun.Com B_FALSE) != CRYPTO_SUCCESS) {
6988313SDina.Nimeh@Sun.Com /*
6998313SDina.Nimeh@Sun.Com * XXX: original code didn't set residual
7008313SDina.Nimeh@Sun.Com * back to len because no error was expected
7018313SDina.Nimeh@Sun.Com * from bcopy() if encryption is not enabled
7028313SDina.Nimeh@Sun.Com */
7038313SDina.Nimeh@Sun.Com if (method != RDWR_BCOPY)
7048313SDina.Nimeh@Sun.Com bp->b_resid = len;
7058313SDina.Nimeh@Sun.Com error = EIO;
7068313SDina.Nimeh@Sun.Com }
7078313SDina.Nimeh@Sun.Com }
7088313SDina.Nimeh@Sun.Com return (error);
7098313SDina.Nimeh@Sun.Com } else {
7108313SDina.Nimeh@Sun.Com void *iobuf = bufaddr;
7118313SDina.Nimeh@Sun.Com
7128313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) {
7138313SDina.Nimeh@Sun.Com /* don't do in-place crypto to keep bufaddr intact */
7148313SDina.Nimeh@Sun.Com iobuf = kmem_alloc(len, KM_SLEEP);
7158313SDina.Nimeh@Sun.Com if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
7168313SDina.Nimeh@Sun.Com B_TRUE) != CRYPTO_SUCCESS) {
7178313SDina.Nimeh@Sun.Com kmem_free(iobuf, len);
7188313SDina.Nimeh@Sun.Com if (method != RDWR_BCOPY)
7198313SDina.Nimeh@Sun.Com bp->b_resid = len;
7208313SDina.Nimeh@Sun.Com return (EIO);
7218313SDina.Nimeh@Sun.Com }
7228313SDina.Nimeh@Sun.Com }
7238313SDina.Nimeh@Sun.Com if (method == RDWR_BCOPY) {
7248313SDina.Nimeh@Sun.Com /* DO NOT update bp->b_resid for bcopy */
7258313SDina.Nimeh@Sun.Com bcopy(iobuf, bcopy_locn, len);
7268313SDina.Nimeh@Sun.Com error = 0;
7278313SDina.Nimeh@Sun.Com } else { /* RDWR_RAW */
7288313SDina.Nimeh@Sun.Com error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
7298313SDina.Nimeh@Sun.Com offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
7308313SDina.Nimeh@Sun.Com &resid);
7318313SDina.Nimeh@Sun.Com bp->b_resid = resid;
7328313SDina.Nimeh@Sun.Com }
7338313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) {
7348313SDina.Nimeh@Sun.Com kmem_free(iobuf, len);
7358313SDina.Nimeh@Sun.Com }
7368313SDina.Nimeh@Sun.Com return (error);
7378313SDina.Nimeh@Sun.Com }
7388313SDina.Nimeh@Sun.Com }
7398313SDina.Nimeh@Sun.Com
7405643Saalok static int
lofi_mapped_rdwr(caddr_t bufaddr,offset_t offset,struct buf * bp,struct lofi_state * lsp)7415643Saalok lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
7428313SDina.Nimeh@Sun.Com struct lofi_state *lsp)
7435643Saalok {
7445643Saalok int error;
7455643Saalok offset_t alignedoffset, mapoffset;
7465643Saalok size_t xfersize;
7475643Saalok int isread;
7488313SDina.Nimeh@Sun.Com int smflags;
7495643Saalok caddr_t mapaddr;
7505643Saalok size_t len;
7515643Saalok enum seg_rw srw;
7528313SDina.Nimeh@Sun.Com int save_error;
7538313SDina.Nimeh@Sun.Com
7548313SDina.Nimeh@Sun.Com /*
7558313SDina.Nimeh@Sun.Com * Note: offset is already shifted by lsp->ls_crypto_offset
7568313SDina.Nimeh@Sun.Com * when it gets here.
7578313SDina.Nimeh@Sun.Com */
7588313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled)
7598313SDina.Nimeh@Sun.Com ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
7605643Saalok
7615643Saalok /*
7625643Saalok * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
7635643Saalok * an 8K boundary, but the buf transfer address may not be
7645643Saalok * aligned on more than a 512-byte boundary (we don't enforce
7655643Saalok * that even though we could). This matters since the initial
7665643Saalok * part of the transfer may not start at offset 0 within the
7675643Saalok * segmap'd chunk. So we have to compensate for that with
7685643Saalok * 'mapoffset'. Subsequent chunks always start off at the
7695643Saalok * beginning, and the last is capped by b_resid
7708313SDina.Nimeh@Sun.Com *
7718313SDina.Nimeh@Sun.Com * Visually, where "|" represents page map boundaries:
7728313SDina.Nimeh@Sun.Com * alignedoffset (mapaddr begins at this segmap boundary)
7738313SDina.Nimeh@Sun.Com * | offset (from beginning of file)
7748313SDina.Nimeh@Sun.Com * | | len
7758313SDina.Nimeh@Sun.Com * v v v
7768313SDina.Nimeh@Sun.Com * ===|====X========|====...======|========X====|====
7778313SDina.Nimeh@Sun.Com * /-------------...---------------/
7788313SDina.Nimeh@Sun.Com * ^ bp->b_bcount/bp->b_resid at start
7798313SDina.Nimeh@Sun.Com * /----/--------/----...------/--------/
7808313SDina.Nimeh@Sun.Com * ^ ^ ^ ^ ^
7818313SDina.Nimeh@Sun.Com * | | | | nth xfersize (<= MAXBSIZE)
7828313SDina.Nimeh@Sun.Com * | | 2nd thru n-1st xfersize (= MAXBSIZE)
7838313SDina.Nimeh@Sun.Com * | 1st xfersize (<= MAXBSIZE)
7848313SDina.Nimeh@Sun.Com * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
7858313SDina.Nimeh@Sun.Com *
7868313SDina.Nimeh@Sun.Com * Notes: "alignedoffset" is "offset" rounded down to nearest
7878313SDina.Nimeh@Sun.Com * MAXBSIZE boundary. "len" is next page boundary of size
7888719SDina.Nimeh@Sun.COM * PAGESIZE after "alignedoffset".
7895643Saalok */
7905643Saalok mapoffset = offset & MAXBOFFSET;
7915643Saalok alignedoffset = offset - mapoffset;
7925643Saalok bp->b_resid = bp->b_bcount;
7935643Saalok isread = bp->b_flags & B_READ;
7945643Saalok srw = isread ? S_READ : S_WRITE;
7955643Saalok do {
7965643Saalok xfersize = MIN(lsp->ls_vp_comp_size - offset,
7975643Saalok MIN(MAXBSIZE - mapoffset, bp->b_resid));
7988719SDina.Nimeh@Sun.COM len = roundup(mapoffset + xfersize, PAGESIZE);
7995643Saalok mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
8005643Saalok alignedoffset, MAXBSIZE, 1, srw);
8015643Saalok /*
8025643Saalok * Now fault in the pages. This lets us check
8035643Saalok * for errors before we reference mapaddr and
8045643Saalok * try to resolve the fault in bcopy (which would
8055643Saalok * panic instead). And this can easily happen,
8065643Saalok * particularly if you've lofi'd a file over NFS
8075643Saalok * and someone deletes the file on the server.
8085643Saalok */
8095643Saalok error = segmap_fault(kas.a_hat, segkmap, mapaddr,
8105643Saalok len, F_SOFTLOCK, srw);
8115643Saalok if (error) {
8125643Saalok (void) segmap_release(segkmap, mapaddr, 0);
8135643Saalok if (FC_CODE(error) == FC_OBJERR)
8145643Saalok error = FC_ERRNO(error);
8155643Saalok else
8165643Saalok error = EIO;
8175643Saalok break;
8185643Saalok }
8198313SDina.Nimeh@Sun.Com /* error may be non-zero for encrypted lofi */
8208313SDina.Nimeh@Sun.Com error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
8218313SDina.Nimeh@Sun.Com RDWR_BCOPY, mapaddr + mapoffset);
8228313SDina.Nimeh@Sun.Com if (error == 0) {
8238313SDina.Nimeh@Sun.Com bp->b_resid -= xfersize;
8248313SDina.Nimeh@Sun.Com bufaddr += xfersize;
8258313SDina.Nimeh@Sun.Com offset += xfersize;
8268313SDina.Nimeh@Sun.Com }
8275643Saalok smflags = 0;
8285643Saalok if (isread) {
8295643Saalok smflags |= SM_FREE;
8305643Saalok /*
8315643Saalok * If we're reading an entire page starting
8325643Saalok * at a page boundary, there's a good chance
8335643Saalok * we won't need it again. Put it on the
8345643Saalok * head of the freelist.
8355643Saalok */
8368056SDina.Nimeh@Sun.Com if (mapoffset == 0 && xfersize == MAXBSIZE)
8375643Saalok smflags |= SM_DONTNEED;
8385643Saalok } else {
83912368SFrank.Batschulat@Sun.COM /*
84012368SFrank.Batschulat@Sun.COM * Write back good pages, it is okay to
84112368SFrank.Batschulat@Sun.COM * always release asynchronous here as we'll
84212368SFrank.Batschulat@Sun.COM * follow with VOP_FSYNC for B_SYNC buffers.
84312368SFrank.Batschulat@Sun.COM */
84412368SFrank.Batschulat@Sun.COM if (error == 0)
84512368SFrank.Batschulat@Sun.COM smflags |= SM_WRITE | SM_ASYNC;
8465643Saalok }
8475643Saalok (void) segmap_fault(kas.a_hat, segkmap, mapaddr,
8485643Saalok len, F_SOFTUNLOCK, srw);
8498313SDina.Nimeh@Sun.Com save_error = segmap_release(segkmap, mapaddr, smflags);
8508313SDina.Nimeh@Sun.Com if (error == 0)
8518313SDina.Nimeh@Sun.Com error = save_error;
8525643Saalok /* only the first map may start partial */
8535643Saalok mapoffset = 0;
8545643Saalok alignedoffset += MAXBSIZE;
8555643Saalok } while ((error == 0) && (bp->b_resid > 0) &&
8565643Saalok (offset < lsp->ls_vp_comp_size));
8575643Saalok
8585643Saalok return (error);
8595643Saalok }
8605643Saalok
8619048Sjrgn.keil@googlemail.com /*
8629048Sjrgn.keil@googlemail.com * Check if segment seg_index is present in the decompressed segment
8639048Sjrgn.keil@googlemail.com * data cache.
8649048Sjrgn.keil@googlemail.com *
8659048Sjrgn.keil@googlemail.com * Returns a pointer to the decompressed segment data cache entry if
8669048Sjrgn.keil@googlemail.com * found, and NULL when decompressed data for this segment is not yet
8679048Sjrgn.keil@googlemail.com * cached.
8689048Sjrgn.keil@googlemail.com */
8699048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
lofi_find_comp_data(struct lofi_state * lsp,uint64_t seg_index)8709048Sjrgn.keil@googlemail.com lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
8719048Sjrgn.keil@googlemail.com {
8729048Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
8739048Sjrgn.keil@googlemail.com
87412633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
8759048Sjrgn.keil@googlemail.com
8769048Sjrgn.keil@googlemail.com for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
8779048Sjrgn.keil@googlemail.com lc = list_next(&lsp->ls_comp_cache, lc)) {
8789048Sjrgn.keil@googlemail.com if (lc->lc_index == seg_index) {
8799048Sjrgn.keil@googlemail.com /*
8809048Sjrgn.keil@googlemail.com * Decompressed segment data was found in the
8819048Sjrgn.keil@googlemail.com * cache.
8829048Sjrgn.keil@googlemail.com *
8839048Sjrgn.keil@googlemail.com * The cache uses an LRU replacement strategy;
8849048Sjrgn.keil@googlemail.com * move the entry to head of list.
8859048Sjrgn.keil@googlemail.com */
8869048Sjrgn.keil@googlemail.com list_remove(&lsp->ls_comp_cache, lc);
8879048Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc);
8889048Sjrgn.keil@googlemail.com return (lc);
8899048Sjrgn.keil@googlemail.com }
8909048Sjrgn.keil@googlemail.com }
8919048Sjrgn.keil@googlemail.com return (NULL);
8929048Sjrgn.keil@googlemail.com }
8939048Sjrgn.keil@googlemail.com
8949048Sjrgn.keil@googlemail.com /*
8959048Sjrgn.keil@googlemail.com * Add the data for a decompressed segment at segment index
8969048Sjrgn.keil@googlemail.com * seg_index to the cache of the decompressed segments.
8979048Sjrgn.keil@googlemail.com *
8989048Sjrgn.keil@googlemail.com * Returns a pointer to the cache element structure in case
8999048Sjrgn.keil@googlemail.com * the data was added to the cache; returns NULL when the data
9009048Sjrgn.keil@googlemail.com * wasn't cached.
9019048Sjrgn.keil@googlemail.com */
9029048Sjrgn.keil@googlemail.com static struct lofi_comp_cache *
lofi_add_comp_data(struct lofi_state * lsp,uint64_t seg_index,uchar_t * data)9039048Sjrgn.keil@googlemail.com lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
9049048Sjrgn.keil@googlemail.com uchar_t *data)
9059048Sjrgn.keil@googlemail.com {
9069048Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
9079048Sjrgn.keil@googlemail.com
90812633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
9099048Sjrgn.keil@googlemail.com
9109048Sjrgn.keil@googlemail.com while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
9119048Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache);
9129048Sjrgn.keil@googlemail.com ASSERT(lc != NULL);
9139048Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
9149048Sjrgn.keil@googlemail.com kmem_free(lc, sizeof (struct lofi_comp_cache));
9159048Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count--;
9169048Sjrgn.keil@googlemail.com }
9179048Sjrgn.keil@googlemail.com
9189048Sjrgn.keil@googlemail.com /*
9199048Sjrgn.keil@googlemail.com * Do not cache when disabled by tunable variable
9209048Sjrgn.keil@googlemail.com */
9219048Sjrgn.keil@googlemail.com if (lofi_max_comp_cache == 0)
9229048Sjrgn.keil@googlemail.com return (NULL);
9239048Sjrgn.keil@googlemail.com
9249048Sjrgn.keil@googlemail.com /*
9259048Sjrgn.keil@googlemail.com * When the cache has not yet reached the maximum allowed
9269048Sjrgn.keil@googlemail.com * number of segments, allocate a new cache element.
9279048Sjrgn.keil@googlemail.com * Otherwise the cache is full; reuse the last list element
9289048Sjrgn.keil@googlemail.com * (LRU) for caching the decompressed segment data.
9299048Sjrgn.keil@googlemail.com *
9309048Sjrgn.keil@googlemail.com * The cache element for the new decompressed segment data is
9319048Sjrgn.keil@googlemail.com * added to the head of the list.
9329048Sjrgn.keil@googlemail.com */
9339048Sjrgn.keil@googlemail.com if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
9349048Sjrgn.keil@googlemail.com lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
9359048Sjrgn.keil@googlemail.com lc->lc_data = NULL;
9369048Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc);
9379048Sjrgn.keil@googlemail.com lsp->ls_comp_cache_count++;
9389048Sjrgn.keil@googlemail.com } else {
9399048Sjrgn.keil@googlemail.com lc = list_remove_tail(&lsp->ls_comp_cache);
9409048Sjrgn.keil@googlemail.com if (lc == NULL)
9419048Sjrgn.keil@googlemail.com return (NULL);
9429048Sjrgn.keil@googlemail.com list_insert_head(&lsp->ls_comp_cache, lc);
9439048Sjrgn.keil@googlemail.com }
9449048Sjrgn.keil@googlemail.com
9459048Sjrgn.keil@googlemail.com /*
9469048Sjrgn.keil@googlemail.com * Free old uncompressed segment data when reusing a cache
9479048Sjrgn.keil@googlemail.com * entry.
9489048Sjrgn.keil@googlemail.com */
9499048Sjrgn.keil@googlemail.com if (lc->lc_data != NULL)
9509048Sjrgn.keil@googlemail.com kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
9519048Sjrgn.keil@googlemail.com
9529048Sjrgn.keil@googlemail.com lc->lc_data = data;
9539048Sjrgn.keil@googlemail.com lc->lc_index = seg_index;
9549048Sjrgn.keil@googlemail.com return (lc);
9559048Sjrgn.keil@googlemail.com }
9569048Sjrgn.keil@googlemail.com
9579048Sjrgn.keil@googlemail.com
9585643Saalok /*ARGSUSED*/
9598996SAlok.Aggarwal@Sun.COM static int
gzip_decompress(void * src,size_t srclen,void * dst,size_t * dstlen,int level)9608996SAlok.Aggarwal@Sun.COM gzip_decompress(void *src, size_t srclen, void *dst,
9615643Saalok size_t *dstlen, int level)
9625643Saalok {
9635643Saalok ASSERT(*dstlen >= srclen);
9645643Saalok
9655643Saalok if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
9665643Saalok return (-1);
9675643Saalok return (0);
9685643Saalok }
9695643Saalok
9708996SAlok.Aggarwal@Sun.COM #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
9718996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
9728996SAlok.Aggarwal@Sun.COM static int
lzma_decompress(void * src,size_t srclen,void * dst,size_t * dstlen,int level)9738996SAlok.Aggarwal@Sun.COM lzma_decompress(void *src, size_t srclen, void *dst,
9748996SAlok.Aggarwal@Sun.COM size_t *dstlen, int level)
9758996SAlok.Aggarwal@Sun.COM {
9768996SAlok.Aggarwal@Sun.COM size_t insizepure;
9778996SAlok.Aggarwal@Sun.COM void *actual_src;
9788996SAlok.Aggarwal@Sun.COM ELzmaStatus status;
9798996SAlok.Aggarwal@Sun.COM
9808996SAlok.Aggarwal@Sun.COM insizepure = srclen - LZMA_HEADER_SIZE;
9818996SAlok.Aggarwal@Sun.COM actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
9828996SAlok.Aggarwal@Sun.COM
9838996SAlok.Aggarwal@Sun.COM if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
9848996SAlok.Aggarwal@Sun.COM (const Byte *)actual_src, &insizepure,
9858996SAlok.Aggarwal@Sun.COM (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
9868996SAlok.Aggarwal@Sun.COM &g_Alloc) != SZ_OK) {
9878996SAlok.Aggarwal@Sun.COM return (-1);
9888996SAlok.Aggarwal@Sun.COM }
9898996SAlok.Aggarwal@Sun.COM return (0);
9908996SAlok.Aggarwal@Sun.COM }
9918996SAlok.Aggarwal@Sun.COM
9920Sstevel@tonic-gate /*
9930Sstevel@tonic-gate * This is basically what strategy used to be before we found we
9940Sstevel@tonic-gate * needed task queues.
9950Sstevel@tonic-gate */
9960Sstevel@tonic-gate static void
lofi_strategy_task(void * arg)9970Sstevel@tonic-gate lofi_strategy_task(void *arg)
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate struct buf *bp = (struct buf *)arg;
10000Sstevel@tonic-gate int error;
100112368SFrank.Batschulat@Sun.COM int syncflag = 0;
10020Sstevel@tonic-gate struct lofi_state *lsp;
10038313SDina.Nimeh@Sun.Com offset_t offset;
10048313SDina.Nimeh@Sun.Com caddr_t bufaddr;
10058313SDina.Nimeh@Sun.Com size_t len;
10068313SDina.Nimeh@Sun.Com size_t xfersize;
10078313SDina.Nimeh@Sun.Com boolean_t bufinited = B_FALSE;
10080Sstevel@tonic-gate
10090Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
10108313SDina.Nimeh@Sun.Com if (lsp == NULL) {
10118313SDina.Nimeh@Sun.Com error = ENXIO;
10128313SDina.Nimeh@Sun.Com goto errout;
10138313SDina.Nimeh@Sun.Com }
10140Sstevel@tonic-gate if (lsp->ls_kstat) {
10150Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock);
10160Sstevel@tonic-gate kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
10170Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock);
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate bp_mapin(bp);
10200Sstevel@tonic-gate bufaddr = bp->b_un.b_addr;
10210Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */
10228313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) {
10238313SDina.Nimeh@Sun.Com /* encrypted data really begins after crypto header */
10248313SDina.Nimeh@Sun.Com offset += lsp->ls_crypto_offset;
10258313SDina.Nimeh@Sun.Com }
10268313SDina.Nimeh@Sun.Com len = bp->b_bcount;
10278313SDina.Nimeh@Sun.Com bufinited = B_TRUE;
10288313SDina.Nimeh@Sun.Com
10298313SDina.Nimeh@Sun.Com if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
10308313SDina.Nimeh@Sun.Com error = EIO;
10318313SDina.Nimeh@Sun.Com goto errout;
10328313SDina.Nimeh@Sun.Com }
10330Sstevel@tonic-gate
10340Sstevel@tonic-gate /*
103512368SFrank.Batschulat@Sun.COM * If we're writing and the buffer was not B_ASYNC
103612368SFrank.Batschulat@Sun.COM * we'll follow up with a VOP_FSYNC() to force any
103712368SFrank.Batschulat@Sun.COM * asynchronous I/O to stable storage.
103812368SFrank.Batschulat@Sun.COM */
103912368SFrank.Batschulat@Sun.COM if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
104012368SFrank.Batschulat@Sun.COM syncflag = FSYNC;
104112368SFrank.Batschulat@Sun.COM
104212368SFrank.Batschulat@Sun.COM /*
10430Sstevel@tonic-gate * We used to always use vn_rdwr here, but we cannot do that because
10440Sstevel@tonic-gate * we might decide to read or write from the the underlying
10450Sstevel@tonic-gate * file during this call, which would be a deadlock because
10460Sstevel@tonic-gate * we have the rw_lock. So instead we page, unless it's not
10478313SDina.Nimeh@Sun.Com * mapable or it's a character device or it's an encrypted lofi.
10480Sstevel@tonic-gate */
10498313SDina.Nimeh@Sun.Com if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
10508313SDina.Nimeh@Sun.Com lsp->ls_crypto_enabled) {
10518313SDina.Nimeh@Sun.Com error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
10528313SDina.Nimeh@Sun.Com NULL);
10538313SDina.Nimeh@Sun.Com } else if (lsp->ls_uncomp_seg_sz == 0) {
10548313SDina.Nimeh@Sun.Com error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
10558313SDina.Nimeh@Sun.Com } else {
10569048Sjrgn.keil@googlemail.com uchar_t *compressed_seg = NULL, *cmpbuf;
10579048Sjrgn.keil@googlemail.com uchar_t *uncompressed_seg = NULL;
10588313SDina.Nimeh@Sun.Com lofi_compress_info_t *li;
10598313SDina.Nimeh@Sun.Com size_t oblkcount;
10609048Sjrgn.keil@googlemail.com ulong_t seglen;
10618313SDina.Nimeh@Sun.Com uint64_t sblkno, eblkno, cmpbytes;
10629048Sjrgn.keil@googlemail.com uint64_t uncompressed_seg_index;
10639048Sjrgn.keil@googlemail.com struct lofi_comp_cache *lc;
10648313SDina.Nimeh@Sun.Com offset_t sblkoff, eblkoff;
10658313SDina.Nimeh@Sun.Com u_offset_t salign, ealign;
10668313SDina.Nimeh@Sun.Com u_offset_t sdiff;
10678313SDina.Nimeh@Sun.Com uint32_t comp_data_sz;
10685643Saalok uint64_t i;
106912384Salok.aggarwal@oracle.com int j;
10705643Saalok
10710Sstevel@tonic-gate /*
10725643Saalok * From here on we're dealing primarily with compressed files
10735643Saalok */
10748313SDina.Nimeh@Sun.Com ASSERT(!lsp->ls_crypto_enabled);
10755643Saalok
10765643Saalok /*
10775643Saalok * Compressed files can only be read from and
10785643Saalok * not written to
10790Sstevel@tonic-gate */
10805643Saalok if (!(bp->b_flags & B_READ)) {
10815643Saalok bp->b_resid = bp->b_bcount;
10825643Saalok error = EROFS;
10835643Saalok goto done;
10845643Saalok }
10855643Saalok
10865643Saalok ASSERT(lsp->ls_comp_algorithm_index >= 0);
10875643Saalok li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
10885643Saalok /*
10895643Saalok * Compute starting and ending compressed segment numbers
10905643Saalok * We use only bitwise operations avoiding division and
10915643Saalok * modulus because we enforce the compression segment size
10925643Saalok * to a power of 2
10935643Saalok */
10945643Saalok sblkno = offset >> lsp->ls_comp_seg_shift;
10955643Saalok sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
10965643Saalok eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
10975643Saalok eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
10985643Saalok
10995643Saalok /*
11009048Sjrgn.keil@googlemail.com * Check the decompressed segment cache.
11019048Sjrgn.keil@googlemail.com *
11029048Sjrgn.keil@googlemail.com * The cache is used only when the requested data
11039048Sjrgn.keil@googlemail.com * is within a segment. Requests that cross
11049048Sjrgn.keil@googlemail.com * segment boundaries bypass the cache.
11059048Sjrgn.keil@googlemail.com */
11069048Sjrgn.keil@googlemail.com if (sblkno == eblkno ||
11079048Sjrgn.keil@googlemail.com (sblkno + 1 == eblkno && eblkoff == 0)) {
11089048Sjrgn.keil@googlemail.com /*
11099048Sjrgn.keil@googlemail.com * Request doesn't cross a segment boundary,
11109048Sjrgn.keil@googlemail.com * now check the cache.
11119048Sjrgn.keil@googlemail.com */
11129048Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock);
11139048Sjrgn.keil@googlemail.com lc = lofi_find_comp_data(lsp, sblkno);
11149048Sjrgn.keil@googlemail.com if (lc != NULL) {
11159048Sjrgn.keil@googlemail.com /*
11169048Sjrgn.keil@googlemail.com * We've found the decompressed segment
11179048Sjrgn.keil@googlemail.com * data in the cache; reuse it.
11189048Sjrgn.keil@googlemail.com */
11199048Sjrgn.keil@googlemail.com bcopy(lc->lc_data + sblkoff, bufaddr,
11209048Sjrgn.keil@googlemail.com bp->b_bcount);
11219048Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock);
11229048Sjrgn.keil@googlemail.com bp->b_resid = 0;
11239048Sjrgn.keil@googlemail.com error = 0;
11249048Sjrgn.keil@googlemail.com goto done;
11259048Sjrgn.keil@googlemail.com }
11269048Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock);
11279048Sjrgn.keil@googlemail.com }
11289048Sjrgn.keil@googlemail.com
11299048Sjrgn.keil@googlemail.com /*
11305643Saalok * Align start offset to block boundary for segmap
11315643Saalok */
11325643Saalok salign = lsp->ls_comp_seg_index[sblkno];
11335643Saalok sdiff = salign & (DEV_BSIZE - 1);
11345643Saalok salign -= sdiff;
11355643Saalok if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
11360Sstevel@tonic-gate /*
11375643Saalok * We're dealing with the last segment of
11385643Saalok * the compressed file -- the size of this
11395643Saalok * segment *may not* be the same as the
11405643Saalok * segment size for the file
11410Sstevel@tonic-gate */
11425643Saalok eblkoff = (offset + bp->b_bcount) &
11435643Saalok (lsp->ls_uncomp_last_seg_sz - 1);
11445643Saalok ealign = lsp->ls_vp_comp_size;
11455643Saalok } else {
11465643Saalok ealign = lsp->ls_comp_seg_index[eblkno + 1];
11475643Saalok }
11485643Saalok
11495643Saalok /*
11505643Saalok * Preserve original request paramaters
11515643Saalok */
11525643Saalok oblkcount = bp->b_bcount;
11535643Saalok
11545643Saalok /*
11555643Saalok * Assign the calculated parameters
11565643Saalok */
11575643Saalok comp_data_sz = ealign - salign;
11585643Saalok bp->b_bcount = comp_data_sz;
11595643Saalok
11605643Saalok /*
116112384Salok.aggarwal@oracle.com * Buffers to hold compressed segments are pre-allocated
116212384Salok.aggarwal@oracle.com * on a per-thread basis. Find a pre-allocated buffer
116312384Salok.aggarwal@oracle.com * that is not currently in use and mark it for use.
11645643Saalok */
116512384Salok.aggarwal@oracle.com mutex_enter(&lsp->ls_comp_bufs_lock);
116612384Salok.aggarwal@oracle.com for (j = 0; j < lofi_taskq_nthreads; j++) {
116712384Salok.aggarwal@oracle.com if (lsp->ls_comp_bufs[j].inuse == 0) {
116812384Salok.aggarwal@oracle.com lsp->ls_comp_bufs[j].inuse = 1;
116912384Salok.aggarwal@oracle.com break;
117012384Salok.aggarwal@oracle.com }
117112384Salok.aggarwal@oracle.com }
117212384Salok.aggarwal@oracle.com
117312384Salok.aggarwal@oracle.com mutex_exit(&lsp->ls_comp_bufs_lock);
117412384Salok.aggarwal@oracle.com ASSERT(j < lofi_taskq_nthreads);
117512384Salok.aggarwal@oracle.com
117612384Salok.aggarwal@oracle.com /*
117712384Salok.aggarwal@oracle.com * If the pre-allocated buffer size does not match
117812384Salok.aggarwal@oracle.com * the size of the I/O request, re-allocate it with
117912384Salok.aggarwal@oracle.com * the appropriate size
118012384Salok.aggarwal@oracle.com */
118112384Salok.aggarwal@oracle.com if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) {
118212384Salok.aggarwal@oracle.com if (lsp->ls_comp_bufs[j].bufsize > 0)
118312384Salok.aggarwal@oracle.com kmem_free(lsp->ls_comp_bufs[j].buf,
118412384Salok.aggarwal@oracle.com lsp->ls_comp_bufs[j].bufsize);
118512384Salok.aggarwal@oracle.com lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount,
118612384Salok.aggarwal@oracle.com KM_SLEEP);
118712384Salok.aggarwal@oracle.com lsp->ls_comp_bufs[j].bufsize = bp->b_bcount;
118812384Salok.aggarwal@oracle.com }
118912384Salok.aggarwal@oracle.com compressed_seg = lsp->ls_comp_bufs[j].buf;
119012384Salok.aggarwal@oracle.com
11915643Saalok /*
11925643Saalok * Map in the calculated number of blocks
11935643Saalok */
11945643Saalok error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
11955643Saalok bp, lsp);
11965643Saalok
11975643Saalok bp->b_bcount = oblkcount;
11985643Saalok bp->b_resid = oblkcount;
11995643Saalok if (error != 0)
12005643Saalok goto done;
12015643Saalok
12025643Saalok /*
120312384Salok.aggarwal@oracle.com * decompress compressed blocks start
12045643Saalok */
12055643Saalok cmpbuf = compressed_seg + sdiff;
12068996SAlok.Aggarwal@Sun.COM for (i = sblkno; i <= eblkno; i++) {
12078996SAlok.Aggarwal@Sun.COM ASSERT(i < lsp->ls_comp_index_sz - 1);
120812384Salok.aggarwal@oracle.com uchar_t *useg;
12098996SAlok.Aggarwal@Sun.COM
12108996SAlok.Aggarwal@Sun.COM /*
12118996SAlok.Aggarwal@Sun.COM * The last segment is special in that it is
12128996SAlok.Aggarwal@Sun.COM * most likely not going to be the same
12138996SAlok.Aggarwal@Sun.COM * (uncompressed) size as the other segments.
12148996SAlok.Aggarwal@Sun.COM */
12158996SAlok.Aggarwal@Sun.COM if (i == (lsp->ls_comp_index_sz - 2)) {
12168996SAlok.Aggarwal@Sun.COM seglen = lsp->ls_uncomp_last_seg_sz;
12178996SAlok.Aggarwal@Sun.COM } else {
12188996SAlok.Aggarwal@Sun.COM seglen = lsp->ls_uncomp_seg_sz;
12198996SAlok.Aggarwal@Sun.COM }
12208996SAlok.Aggarwal@Sun.COM
12215643Saalok /*
12225643Saalok * Each of the segment index entries contains
12235643Saalok * the starting block number for that segment.
12245643Saalok * The number of compressed bytes in a segment
12255643Saalok * is thus the difference between the starting
12265643Saalok * block number of this segment and the starting
12275643Saalok * block number of the next segment.
12285643Saalok */
12298996SAlok.Aggarwal@Sun.COM cmpbytes = lsp->ls_comp_seg_index[i + 1] -
12308996SAlok.Aggarwal@Sun.COM lsp->ls_comp_seg_index[i];
12315643Saalok
12325643Saalok /*
12335643Saalok * The first byte in a compressed segment is a flag
12345643Saalok * that indicates whether this segment is compressed
123512384Salok.aggarwal@oracle.com * at all.
123612384Salok.aggarwal@oracle.com *
123712384Salok.aggarwal@oracle.com * The variable 'useg' is used (instead of
123812384Salok.aggarwal@oracle.com * uncompressed_seg) in this loop to keep a
123912384Salok.aggarwal@oracle.com * reference to the uncompressed segment.
124012384Salok.aggarwal@oracle.com *
124112384Salok.aggarwal@oracle.com * N.B. If 'useg' is replaced with uncompressed_seg,
124212384Salok.aggarwal@oracle.com * it leads to memory leaks and heap corruption in
124312384Salok.aggarwal@oracle.com * corner cases where compressed segments lie
124412384Salok.aggarwal@oracle.com * adjacent to uncompressed segments.
12455643Saalok */
12465643Saalok if (*cmpbuf == UNCOMPRESSED) {
124712384Salok.aggarwal@oracle.com useg = cmpbuf + SEGHDR;
12485643Saalok } else {
124912384Salok.aggarwal@oracle.com if (uncompressed_seg == NULL)
125012384Salok.aggarwal@oracle.com uncompressed_seg =
125112384Salok.aggarwal@oracle.com kmem_alloc(lsp->ls_uncomp_seg_sz,
125212384Salok.aggarwal@oracle.com KM_SLEEP);
125312384Salok.aggarwal@oracle.com useg = uncompressed_seg;
125412384Salok.aggarwal@oracle.com uncompressed_seg_index = i;
125512384Salok.aggarwal@oracle.com
12565643Saalok if (li->l_decompress((cmpbuf + SEGHDR),
12575643Saalok (cmpbytes - SEGHDR), uncompressed_seg,
12585643Saalok &seglen, li->l_level) != 0) {
12595643Saalok error = EIO;
12605643Saalok goto done;
12615643Saalok }
12625643Saalok }
12635643Saalok
12645643Saalok /*
12655643Saalok * Determine how much uncompressed data we
12665643Saalok * have to copy and copy it
12675643Saalok */
12685643Saalok xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
12698996SAlok.Aggarwal@Sun.COM if (i == eblkno)
12708996SAlok.Aggarwal@Sun.COM xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
12715643Saalok
127212384Salok.aggarwal@oracle.com bcopy((useg + sblkoff), bufaddr, xfersize);
12735643Saalok
12745643Saalok cmpbuf += cmpbytes;
12750Sstevel@tonic-gate bufaddr += xfersize;
12765643Saalok bp->b_resid -= xfersize;
12775643Saalok sblkoff = 0;
12785643Saalok
12795643Saalok if (bp->b_resid == 0)
12805643Saalok break;
128112384Salok.aggarwal@oracle.com } /* decompress compressed blocks ends */
12829048Sjrgn.keil@googlemail.com
12839048Sjrgn.keil@googlemail.com /*
128412384Salok.aggarwal@oracle.com * Skip to done if there is no uncompressed data to cache
128512384Salok.aggarwal@oracle.com */
128612384Salok.aggarwal@oracle.com if (uncompressed_seg == NULL)
128712384Salok.aggarwal@oracle.com goto done;
128812384Salok.aggarwal@oracle.com
128912384Salok.aggarwal@oracle.com /*
129012384Salok.aggarwal@oracle.com * Add the data for the last decompressed segment to
12919048Sjrgn.keil@googlemail.com * the cache.
12929048Sjrgn.keil@googlemail.com *
12939048Sjrgn.keil@googlemail.com * In case the uncompressed segment data was added to (and
12949048Sjrgn.keil@googlemail.com * is referenced by) the cache, make sure we don't free it
12959048Sjrgn.keil@googlemail.com * here.
12969048Sjrgn.keil@googlemail.com */
12979048Sjrgn.keil@googlemail.com mutex_enter(&lsp->ls_comp_cache_lock);
12989048Sjrgn.keil@googlemail.com if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
12999048Sjrgn.keil@googlemail.com uncompressed_seg)) != NULL) {
13009048Sjrgn.keil@googlemail.com uncompressed_seg = NULL;
13019048Sjrgn.keil@googlemail.com }
13029048Sjrgn.keil@googlemail.com mutex_exit(&lsp->ls_comp_cache_lock);
13039048Sjrgn.keil@googlemail.com
13048313SDina.Nimeh@Sun.Com done:
130512384Salok.aggarwal@oracle.com if (compressed_seg != NULL) {
130612384Salok.aggarwal@oracle.com mutex_enter(&lsp->ls_comp_bufs_lock);
130712384Salok.aggarwal@oracle.com lsp->ls_comp_bufs[j].inuse = 0;
130812384Salok.aggarwal@oracle.com mutex_exit(&lsp->ls_comp_bufs_lock);
130912384Salok.aggarwal@oracle.com }
13108313SDina.Nimeh@Sun.Com if (uncompressed_seg != NULL)
13118313SDina.Nimeh@Sun.Com kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
13128313SDina.Nimeh@Sun.Com } /* end of handling compressed files */
13130Sstevel@tonic-gate
131412368SFrank.Batschulat@Sun.COM if ((error == 0) && (syncflag != 0))
131512368SFrank.Batschulat@Sun.COM error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
131612368SFrank.Batschulat@Sun.COM
13178313SDina.Nimeh@Sun.Com errout:
13188313SDina.Nimeh@Sun.Com if (bufinited && lsp->ls_kstat) {
13190Sstevel@tonic-gate size_t n_done = bp->b_bcount - bp->b_resid;
13200Sstevel@tonic-gate kstat_io_t *kioptr;
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock);
13230Sstevel@tonic-gate kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
13240Sstevel@tonic-gate if (bp->b_flags & B_READ) {
13250Sstevel@tonic-gate kioptr->nread += n_done;
13260Sstevel@tonic-gate kioptr->reads++;
13270Sstevel@tonic-gate } else {
13280Sstevel@tonic-gate kioptr->nwritten += n_done;
13290Sstevel@tonic-gate kioptr->writes++;
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate kstat_runq_exit(kioptr);
13320Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock);
13330Sstevel@tonic-gate }
13344451Seschrock
13354451Seschrock mutex_enter(&lsp->ls_vp_lock);
13364451Seschrock if (--lsp->ls_vp_iocount == 0)
13374451Seschrock cv_broadcast(&lsp->ls_vp_cv);
13384451Seschrock mutex_exit(&lsp->ls_vp_lock);
13394451Seschrock
13400Sstevel@tonic-gate bioerror(bp, error);
13410Sstevel@tonic-gate biodone(bp);
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate
13440Sstevel@tonic-gate static int
lofi_strategy(struct buf * bp)13450Sstevel@tonic-gate lofi_strategy(struct buf *bp)
13460Sstevel@tonic-gate {
13470Sstevel@tonic-gate struct lofi_state *lsp;
13480Sstevel@tonic-gate offset_t offset;
13490Sstevel@tonic-gate
13500Sstevel@tonic-gate /*
13510Sstevel@tonic-gate * We cannot just do I/O here, because the current thread
13520Sstevel@tonic-gate * _might_ end up back in here because the underlying filesystem
13530Sstevel@tonic-gate * wants a buffer, which eventually gets into bio_recycle and
13540Sstevel@tonic-gate * might call into lofi to write out a delayed-write buffer.
13550Sstevel@tonic-gate * This is bad if the filesystem above lofi is the same as below.
13560Sstevel@tonic-gate *
13570Sstevel@tonic-gate * We could come up with a complex strategy using threads to
13580Sstevel@tonic-gate * do the I/O asynchronously, or we could use task queues. task
13590Sstevel@tonic-gate * queues were incredibly easy so they win.
13600Sstevel@tonic-gate */
13610Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
13628313SDina.Nimeh@Sun.Com if (lsp == NULL) {
13638313SDina.Nimeh@Sun.Com bioerror(bp, ENXIO);
13648313SDina.Nimeh@Sun.Com biodone(bp);
13658313SDina.Nimeh@Sun.Com return (0);
13668313SDina.Nimeh@Sun.Com }
13678313SDina.Nimeh@Sun.Com
13684451Seschrock mutex_enter(&lsp->ls_vp_lock);
13694451Seschrock if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
13704451Seschrock bioerror(bp, EIO);
13714451Seschrock biodone(bp);
13724451Seschrock mutex_exit(&lsp->ls_vp_lock);
13734451Seschrock return (0);
13744451Seschrock }
13754451Seschrock
13760Sstevel@tonic-gate offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */
13778313SDina.Nimeh@Sun.Com if (lsp->ls_crypto_enabled) {
13788313SDina.Nimeh@Sun.Com /* encrypted data really begins after crypto header */
13798313SDina.Nimeh@Sun.Com offset += lsp->ls_crypto_offset;
13808313SDina.Nimeh@Sun.Com }
13810Sstevel@tonic-gate if (offset == lsp->ls_vp_size) {
13820Sstevel@tonic-gate /* EOF */
13830Sstevel@tonic-gate if ((bp->b_flags & B_READ) != 0) {
13840Sstevel@tonic-gate bp->b_resid = bp->b_bcount;
13850Sstevel@tonic-gate bioerror(bp, 0);
13860Sstevel@tonic-gate } else {
13870Sstevel@tonic-gate /* writes should fail */
13880Sstevel@tonic-gate bioerror(bp, ENXIO);
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate biodone(bp);
13914451Seschrock mutex_exit(&lsp->ls_vp_lock);
13920Sstevel@tonic-gate return (0);
13930Sstevel@tonic-gate }
13940Sstevel@tonic-gate if (offset > lsp->ls_vp_size) {
13950Sstevel@tonic-gate bioerror(bp, ENXIO);
13960Sstevel@tonic-gate biodone(bp);
13974451Seschrock mutex_exit(&lsp->ls_vp_lock);
13980Sstevel@tonic-gate return (0);
13990Sstevel@tonic-gate }
14004451Seschrock lsp->ls_vp_iocount++;
14014451Seschrock mutex_exit(&lsp->ls_vp_lock);
14024451Seschrock
14030Sstevel@tonic-gate if (lsp->ls_kstat) {
14040Sstevel@tonic-gate mutex_enter(lsp->ls_kstat->ks_lock);
14050Sstevel@tonic-gate kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
14060Sstevel@tonic-gate mutex_exit(lsp->ls_kstat->ks_lock);
14070Sstevel@tonic-gate }
14080Sstevel@tonic-gate (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
14090Sstevel@tonic-gate return (0);
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate
14120Sstevel@tonic-gate /*ARGSUSED2*/
14130Sstevel@tonic-gate static int
lofi_read(dev_t dev,struct uio * uio,struct cred * credp)14140Sstevel@tonic-gate lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate if (getminor(dev) == 0)
14170Sstevel@tonic-gate return (EINVAL);
14188313SDina.Nimeh@Sun.Com UIO_CHECK(uio);
14190Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate /*ARGSUSED2*/
14230Sstevel@tonic-gate static int
lofi_write(dev_t dev,struct uio * uio,struct cred * credp)14240Sstevel@tonic-gate lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate if (getminor(dev) == 0)
14270Sstevel@tonic-gate return (EINVAL);
14288313SDina.Nimeh@Sun.Com UIO_CHECK(uio);
14290Sstevel@tonic-gate return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate /*ARGSUSED2*/
14330Sstevel@tonic-gate static int
lofi_aread(dev_t dev,struct aio_req * aio,struct cred * credp)14340Sstevel@tonic-gate lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
14350Sstevel@tonic-gate {
14360Sstevel@tonic-gate if (getminor(dev) == 0)
14370Sstevel@tonic-gate return (EINVAL);
14388313SDina.Nimeh@Sun.Com UIO_CHECK(aio->aio_uio);
14390Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate
14420Sstevel@tonic-gate /*ARGSUSED2*/
14430Sstevel@tonic-gate static int
lofi_awrite(dev_t dev,struct aio_req * aio,struct cred * credp)14440Sstevel@tonic-gate lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate if (getminor(dev) == 0)
14470Sstevel@tonic-gate return (EINVAL);
14488313SDina.Nimeh@Sun.Com UIO_CHECK(aio->aio_uio);
14490Sstevel@tonic-gate return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate /*ARGSUSED*/
14530Sstevel@tonic-gate static int
lofi_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)14540Sstevel@tonic-gate lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
14550Sstevel@tonic-gate {
14560Sstevel@tonic-gate switch (infocmd) {
14570Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
14580Sstevel@tonic-gate *result = lofi_dip;
14590Sstevel@tonic-gate return (DDI_SUCCESS);
14600Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
14610Sstevel@tonic-gate *result = 0;
14620Sstevel@tonic-gate return (DDI_SUCCESS);
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate return (DDI_FAILURE);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate
14670Sstevel@tonic-gate static int
lofi_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)14680Sstevel@tonic-gate lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
14690Sstevel@tonic-gate {
14700Sstevel@tonic-gate int error;
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate if (cmd != DDI_ATTACH)
14730Sstevel@tonic-gate return (DDI_FAILURE);
147412633Sjohn.levon@sun.com
147512633Sjohn.levon@sun.com lofi_minor_id = id_space_create("lofi_minor_id", 1, L_MAXMIN32 + 1);
147612633Sjohn.levon@sun.com
147712633Sjohn.levon@sun.com if (!lofi_minor_id)
147812633Sjohn.levon@sun.com return (DDI_FAILURE);
147912633Sjohn.levon@sun.com
14800Sstevel@tonic-gate error = ddi_soft_state_zalloc(lofi_statep, 0);
14810Sstevel@tonic-gate if (error == DDI_FAILURE) {
148212633Sjohn.levon@sun.com id_space_destroy(lofi_minor_id);
14830Sstevel@tonic-gate return (DDI_FAILURE);
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
14860Sstevel@tonic-gate DDI_PSEUDO, NULL);
14870Sstevel@tonic-gate if (error == DDI_FAILURE) {
14880Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0);
148912633Sjohn.levon@sun.com id_space_destroy(lofi_minor_id);
14900Sstevel@tonic-gate return (DDI_FAILURE);
14910Sstevel@tonic-gate }
14925084Sjohnlev /* driver handles kernel-issued IOCTLs */
14935084Sjohnlev if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
14945084Sjohnlev DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
14955084Sjohnlev ddi_remove_minor_node(dip, NULL);
14965084Sjohnlev ddi_soft_state_free(lofi_statep, 0);
149712633Sjohn.levon@sun.com id_space_destroy(lofi_minor_id);
14985084Sjohnlev return (DDI_FAILURE);
14995084Sjohnlev }
150012633Sjohn.levon@sun.com
150112633Sjohn.levon@sun.com zone_key_create(&lofi_zone_key, NULL, lofi_zone_shutdown, NULL);
150212633Sjohn.levon@sun.com
15030Sstevel@tonic-gate lofi_dip = dip;
15040Sstevel@tonic-gate ddi_report_dev(dip);
15050Sstevel@tonic-gate return (DDI_SUCCESS);
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate static int
lofi_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)15090Sstevel@tonic-gate lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate if (cmd != DDI_DETACH)
15120Sstevel@tonic-gate return (DDI_FAILURE);
151312633Sjohn.levon@sun.com
151412633Sjohn.levon@sun.com mutex_enter(&lofi_lock);
151512633Sjohn.levon@sun.com
151612633Sjohn.levon@sun.com if (!list_is_empty(&lofi_list)) {
151712633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
15180Sstevel@tonic-gate return (DDI_FAILURE);
151912633Sjohn.levon@sun.com }
152012633Sjohn.levon@sun.com
15210Sstevel@tonic-gate lofi_dip = NULL;
15220Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
15235084Sjohnlev ddi_prop_remove_all(dip);
152412633Sjohn.levon@sun.com
152512633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
152612633Sjohn.levon@sun.com
152712633Sjohn.levon@sun.com if (zone_key_delete(lofi_zone_key) != 0)
152812633Sjohn.levon@sun.com cmn_err(CE_WARN, "failed to delete zone key");
152912633Sjohn.levon@sun.com
15300Sstevel@tonic-gate ddi_soft_state_free(lofi_statep, 0);
153112633Sjohn.levon@sun.com
153212633Sjohn.levon@sun.com id_space_destroy(lofi_minor_id);
153312633Sjohn.levon@sun.com
15340Sstevel@tonic-gate return (DDI_SUCCESS);
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate
15370Sstevel@tonic-gate /*
15388313SDina.Nimeh@Sun.Com * With addition of encryption, be careful that encryption key is wiped before
15398313SDina.Nimeh@Sun.Com * kernel memory structures are freed, and also that key is not accidentally
15408313SDina.Nimeh@Sun.Com * passed out into userland structures.
15418313SDina.Nimeh@Sun.Com */
15428313SDina.Nimeh@Sun.Com static void
free_lofi_ioctl(struct lofi_ioctl * klip)15438313SDina.Nimeh@Sun.Com free_lofi_ioctl(struct lofi_ioctl *klip)
15448313SDina.Nimeh@Sun.Com {
15458313SDina.Nimeh@Sun.Com /* Make sure this encryption key doesn't stick around */
15468313SDina.Nimeh@Sun.Com bzero(klip->li_key, sizeof (klip->li_key));
15478313SDina.Nimeh@Sun.Com kmem_free(klip, sizeof (struct lofi_ioctl));
15488313SDina.Nimeh@Sun.Com }
15498313SDina.Nimeh@Sun.Com
15508313SDina.Nimeh@Sun.Com /*
15510Sstevel@tonic-gate * These two just simplify the rest of the ioctls that need to copyin/out
15520Sstevel@tonic-gate * the lofi_ioctl structure.
15530Sstevel@tonic-gate */
155412633Sjohn.levon@sun.com int
copy_in_lofi_ioctl(const struct lofi_ioctl * ulip,struct lofi_ioctl ** klipp,int flag)155512633Sjohn.levon@sun.com copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, struct lofi_ioctl **klipp,
155612633Sjohn.levon@sun.com int flag)
15570Sstevel@tonic-gate {
15580Sstevel@tonic-gate struct lofi_ioctl *klip;
15590Sstevel@tonic-gate int error;
15600Sstevel@tonic-gate
156112633Sjohn.levon@sun.com klip = *klipp = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
15621657Sheppo error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
156312633Sjohn.levon@sun.com if (error)
156412633Sjohn.levon@sun.com goto err;
156512633Sjohn.levon@sun.com
156612633Sjohn.levon@sun.com /* ensure NULL termination */
156712633Sjohn.levon@sun.com klip->li_filename[MAXPATHLEN-1] = '\0';
156812633Sjohn.levon@sun.com klip->li_algorithm[MAXALGLEN-1] = '\0';
156912633Sjohn.levon@sun.com klip->li_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
157012633Sjohn.levon@sun.com klip->li_iv_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
157112633Sjohn.levon@sun.com
157212633Sjohn.levon@sun.com if (klip->li_minor > L_MAXMIN32) {
157312633Sjohn.levon@sun.com error = EINVAL;
157412633Sjohn.levon@sun.com goto err;
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate
157712633Sjohn.levon@sun.com return (0);
15780Sstevel@tonic-gate
157912633Sjohn.levon@sun.com err:
158012633Sjohn.levon@sun.com free_lofi_ioctl(klip);
158112633Sjohn.levon@sun.com return (error);
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate int
copy_out_lofi_ioctl(const struct lofi_ioctl * klip,struct lofi_ioctl * ulip,int flag)15851657Sheppo copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
15861657Sheppo int flag)
15870Sstevel@tonic-gate {
15880Sstevel@tonic-gate int error;
15890Sstevel@tonic-gate
15908313SDina.Nimeh@Sun.Com /*
15918313SDina.Nimeh@Sun.Com * NOTE: Do NOT copy the crypto_key_t "back" to userland.
15928313SDina.Nimeh@Sun.Com * This ensures that an attacker can't trivially find the
15938313SDina.Nimeh@Sun.Com * key for a mapping just by issuing the ioctl.
15948313SDina.Nimeh@Sun.Com *
15958313SDina.Nimeh@Sun.Com * It can still be found by poking around in kmem with mdb(1),
15968313SDina.Nimeh@Sun.Com * but there is no point in making it easy when the info isn't
15978313SDina.Nimeh@Sun.Com * of any use in this direction anyway.
15988313SDina.Nimeh@Sun.Com *
15998313SDina.Nimeh@Sun.Com * Either way we don't actually have the raw key stored in
16008313SDina.Nimeh@Sun.Com * a form that we can get it anyway, since we just used it
16018313SDina.Nimeh@Sun.Com * to create a ctx template and didn't keep "the original".
16028313SDina.Nimeh@Sun.Com */
16031657Sheppo error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
16040Sstevel@tonic-gate if (error)
16050Sstevel@tonic-gate return (EFAULT);
16060Sstevel@tonic-gate return (0);
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate static int
lofi_access(struct lofi_state * lsp)161012633Sjohn.levon@sun.com lofi_access(struct lofi_state *lsp)
16110Sstevel@tonic-gate {
161212633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
1613*13096SJordan.Vaughan@Sun.com if (INGLOBALZONE(curproc) || lsp->ls_zone.zref_zone == curzone)
161412633Sjohn.levon@sun.com return (0);
161512633Sjohn.levon@sun.com return (EPERM);
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate
16180Sstevel@tonic-gate /*
161912633Sjohn.levon@sun.com * Find the lofi state for the given filename. We compare by vnode to
162012633Sjohn.levon@sun.com * allow the global zone visibility into NGZ lofi nodes.
16210Sstevel@tonic-gate */
16220Sstevel@tonic-gate static int
file_to_lofi_nocheck(char * filename,struct lofi_state ** lspp)162312633Sjohn.levon@sun.com file_to_lofi_nocheck(char *filename, struct lofi_state **lspp)
16240Sstevel@tonic-gate {
162512633Sjohn.levon@sun.com struct lofi_state *lsp;
162612633Sjohn.levon@sun.com vnode_t *vp = NULL;
162712633Sjohn.levon@sun.com int err = 0;
162812633Sjohn.levon@sun.com
162912633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
163012633Sjohn.levon@sun.com
163112633Sjohn.levon@sun.com if ((err = lookupname(filename, UIO_SYSSPACE, FOLLOW,
163212633Sjohn.levon@sun.com NULLVPP, &vp)) != 0)
163312633Sjohn.levon@sun.com goto out;
163412633Sjohn.levon@sun.com
163512633Sjohn.levon@sun.com if (vp->v_type == VREG) {
163612633Sjohn.levon@sun.com vnode_t *realvp;
163712633Sjohn.levon@sun.com if (VOP_REALVP(vp, &realvp, NULL) == 0) {
163812633Sjohn.levon@sun.com VN_HOLD(realvp);
163912633Sjohn.levon@sun.com VN_RELE(vp);
164012633Sjohn.levon@sun.com vp = realvp;
164112633Sjohn.levon@sun.com }
164212633Sjohn.levon@sun.com }
16430Sstevel@tonic-gate
164412633Sjohn.levon@sun.com for (lsp = list_head(&lofi_list); lsp != NULL;
164512633Sjohn.levon@sun.com lsp = list_next(&lofi_list, lsp)) {
164612633Sjohn.levon@sun.com if (lsp->ls_vp == vp) {
164712633Sjohn.levon@sun.com if (lspp != NULL)
164812633Sjohn.levon@sun.com *lspp = lsp;
164912633Sjohn.levon@sun.com goto out;
165012633Sjohn.levon@sun.com }
165112633Sjohn.levon@sun.com }
165212633Sjohn.levon@sun.com
165312633Sjohn.levon@sun.com err = ENOENT;
165412633Sjohn.levon@sun.com
165512633Sjohn.levon@sun.com out:
165612633Sjohn.levon@sun.com if (vp != NULL)
165712633Sjohn.levon@sun.com VN_RELE(vp);
165812633Sjohn.levon@sun.com return (err);
165912633Sjohn.levon@sun.com }
166012633Sjohn.levon@sun.com
166112633Sjohn.levon@sun.com /*
166212633Sjohn.levon@sun.com * Find the minor for the given filename, checking the zone can access
166312633Sjohn.levon@sun.com * it.
166412633Sjohn.levon@sun.com */
166512633Sjohn.levon@sun.com static int
file_to_lofi(char * filename,struct lofi_state ** lspp)166612633Sjohn.levon@sun.com file_to_lofi(char *filename, struct lofi_state **lspp)
166712633Sjohn.levon@sun.com {
166812633Sjohn.levon@sun.com int err = 0;
166912633Sjohn.levon@sun.com
167012633Sjohn.levon@sun.com ASSERT(MUTEX_HELD(&lofi_lock));
167112633Sjohn.levon@sun.com
167212633Sjohn.levon@sun.com if ((err = file_to_lofi_nocheck(filename, lspp)) != 0)
167312633Sjohn.levon@sun.com return (err);
167412633Sjohn.levon@sun.com
167512633Sjohn.levon@sun.com if ((err = lofi_access(*lspp)) != 0)
167612633Sjohn.levon@sun.com return (err);
167712633Sjohn.levon@sun.com
167812633Sjohn.levon@sun.com return (0);
16790Sstevel@tonic-gate }
16800Sstevel@tonic-gate
16810Sstevel@tonic-gate /*
16820Sstevel@tonic-gate * Fakes up a disk geometry, and one big partition, based on the size
16830Sstevel@tonic-gate * of the file. This is needed because we allow newfs'ing the device,
16840Sstevel@tonic-gate * and newfs will do several disk ioctls to figure out the geometry and
16850Sstevel@tonic-gate * partition information. It uses that information to determine the parameters
16863517Smp204432 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
16870Sstevel@tonic-gate * have to support it.
16880Sstevel@tonic-gate */
16890Sstevel@tonic-gate static void
fake_disk_geometry(struct lofi_state * lsp)16900Sstevel@tonic-gate fake_disk_geometry(struct lofi_state *lsp)
16910Sstevel@tonic-gate {
16928313SDina.Nimeh@Sun.Com u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
16938313SDina.Nimeh@Sun.Com
16940Sstevel@tonic-gate /* dk_geom - see dkio(7I) */
16950Sstevel@tonic-gate /*
16960Sstevel@tonic-gate * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
16970Sstevel@tonic-gate * of sectors), but that breaks programs like fdisk which want to
16980Sstevel@tonic-gate * partition a disk by cylinder. With one cylinder, you can't create
16990Sstevel@tonic-gate * an fdisk partition and put pcfs on it for testing (hard to pick
17000Sstevel@tonic-gate * a number between one and one).
17010Sstevel@tonic-gate *
17020Sstevel@tonic-gate * The cheezy floppy test is an attempt to not have too few cylinders
17030Sstevel@tonic-gate * for a small file, or so many on a big file that you waste space
17040Sstevel@tonic-gate * for backup superblocks or cylinder group structures.
17050Sstevel@tonic-gate */
17068313SDina.Nimeh@Sun.Com if (dsize < (2 * 1024 * 1024)) /* floppy? */
17078313SDina.Nimeh@Sun.Com lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
17080Sstevel@tonic-gate else
17098313SDina.Nimeh@Sun.Com lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
17100Sstevel@tonic-gate /* in case file file is < 100k */
17110Sstevel@tonic-gate if (lsp->ls_dkg.dkg_ncyl == 0)
17120Sstevel@tonic-gate lsp->ls_dkg.dkg_ncyl = 1;
17130Sstevel@tonic-gate lsp->ls_dkg.dkg_acyl = 0;
17140Sstevel@tonic-gate lsp->ls_dkg.dkg_bcyl = 0;
17150Sstevel@tonic-gate lsp->ls_dkg.dkg_nhead = 1;
17160Sstevel@tonic-gate lsp->ls_dkg.dkg_obs1 = 0;
17170Sstevel@tonic-gate lsp->ls_dkg.dkg_intrlv = 0;
17180Sstevel@tonic-gate lsp->ls_dkg.dkg_obs2 = 0;
17190Sstevel@tonic-gate lsp->ls_dkg.dkg_obs3 = 0;
17200Sstevel@tonic-gate lsp->ls_dkg.dkg_apc = 0;
17210Sstevel@tonic-gate lsp->ls_dkg.dkg_rpm = 7200;
17220Sstevel@tonic-gate lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
17238313SDina.Nimeh@Sun.Com lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
17240Sstevel@tonic-gate lsp->ls_dkg.dkg_write_reinstruct = 0;
17250Sstevel@tonic-gate lsp->ls_dkg.dkg_read_reinstruct = 0;
17260Sstevel@tonic-gate
17270Sstevel@tonic-gate /* vtoc - see dkio(7I) */
17280Sstevel@tonic-gate bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
17290Sstevel@tonic-gate lsp->ls_vtoc.v_sanity = VTOC_SANE;
17300Sstevel@tonic-gate lsp->ls_vtoc.v_version = V_VERSION;
17318669SDina.Nimeh@Sun.COM (void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME,
17328669SDina.Nimeh@Sun.COM sizeof (lsp->ls_vtoc.v_volume));
17330Sstevel@tonic-gate lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
17340Sstevel@tonic-gate lsp->ls_vtoc.v_nparts = 1;
17350Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
17365643Saalok
17375643Saalok /*
17385643Saalok * A compressed file is read-only, other files can
17395643Saalok * be read-write
17405643Saalok */
17415643Saalok if (lsp->ls_uncomp_seg_sz > 0) {
17425643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
17435643Saalok } else {
17445643Saalok lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
17455643Saalok }
17460Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
17470Sstevel@tonic-gate /*
17480Sstevel@tonic-gate * The partition size cannot just be the number of sectors, because
17490Sstevel@tonic-gate * that might not end on a cylinder boundary. And if that's the case,
17500Sstevel@tonic-gate * newfs/mkfs will print a scary warning. So just figure the size
17510Sstevel@tonic-gate * based on the number of cylinders and sectors/cylinder.
17520Sstevel@tonic-gate */
17530Sstevel@tonic-gate lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
17540Sstevel@tonic-gate lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
17550Sstevel@tonic-gate
17560Sstevel@tonic-gate /* dk_cinfo - see dkio(7I) */
17570Sstevel@tonic-gate bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
17580Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
17590Sstevel@tonic-gate lsp->ls_ci.dki_ctype = DKC_MD;
17600Sstevel@tonic-gate lsp->ls_ci.dki_flags = 0;
17610Sstevel@tonic-gate lsp->ls_ci.dki_cnum = 0;
17620Sstevel@tonic-gate lsp->ls_ci.dki_addr = 0;
17630Sstevel@tonic-gate lsp->ls_ci.dki_space = 0;
17640Sstevel@tonic-gate lsp->ls_ci.dki_prio = 0;
17650Sstevel@tonic-gate lsp->ls_ci.dki_vec = 0;
17660Sstevel@tonic-gate (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
17670Sstevel@tonic-gate lsp->ls_ci.dki_unit = 0;
17680Sstevel@tonic-gate lsp->ls_ci.dki_slave = 0;
17690Sstevel@tonic-gate lsp->ls_ci.dki_partition = 0;
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate * newfs uses this to set maxcontig. Must not be < 16, or it
17720Sstevel@tonic-gate * will be 0 when newfs multiplies it by DEV_BSIZE and divides
17730Sstevel@tonic-gate * it by the block size. Then tunefs doesn't work because
17740Sstevel@tonic-gate * maxcontig is 0.
17750Sstevel@tonic-gate */
17760Sstevel@tonic-gate lsp->ls_ci.dki_maxtransfer = 16;
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate
17790Sstevel@tonic-gate /*
17805643Saalok * map in a compressed file
17815643Saalok *
17825643Saalok * Read in the header and the index that follows.
17835643Saalok *
17845643Saalok * The header is as follows -
17855643Saalok *
17865643Saalok * Signature (name of the compression algorithm)
17875643Saalok * Compression segment size (a multiple of 512)
17885643Saalok * Number of index entries
17895643Saalok * Size of the last block
17905643Saalok * The array containing the index entries
17915643Saalok *
17925643Saalok * The header information is always stored in
17935643Saalok * network byte order on disk.
17945643Saalok */
17955643Saalok static int
lofi_map_compressed_file(struct lofi_state * lsp,char * buf)17965643Saalok lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
17975643Saalok {
17985643Saalok uint32_t index_sz, header_len, i;
17995643Saalok ssize_t resid;
18005643Saalok enum uio_rw rw;
18015643Saalok char *tbuf = buf;
18025643Saalok int error;
18035643Saalok
18045643Saalok /* The signature has already been read */
18055643Saalok tbuf += sizeof (lsp->ls_comp_algorithm);
18065643Saalok bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
18075643Saalok lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
18085643Saalok
18095643Saalok /*
18105643Saalok * The compressed segment size must be a power of 2
18115643Saalok */
18129048Sjrgn.keil@googlemail.com if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
18139048Sjrgn.keil@googlemail.com !ISP2(lsp->ls_uncomp_seg_sz))
18145643Saalok return (EINVAL);
18155643Saalok
18165643Saalok for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
18175643Saalok ;
18185643Saalok
18195643Saalok lsp->ls_comp_seg_shift = i;
18205643Saalok
18215643Saalok tbuf += sizeof (lsp->ls_uncomp_seg_sz);
18225643Saalok bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
18235643Saalok lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
18245643Saalok
18255643Saalok tbuf += sizeof (lsp->ls_comp_index_sz);
18265643Saalok bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
18275643Saalok sizeof (lsp->ls_uncomp_last_seg_sz));
18285643Saalok lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
18295643Saalok
18305643Saalok /*
18315643Saalok * Compute the total size of the uncompressed data
18325643Saalok * for use in fake_disk_geometry and other calculations.
18335643Saalok * Disk geometry has to be faked with respect to the
18345643Saalok * actual uncompressed data size rather than the
18355643Saalok * compressed file size.
18365643Saalok */
183710197Sdminer@opensolaris.org lsp->ls_vp_size =
183810197Sdminer@opensolaris.org (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
18395643Saalok + lsp->ls_uncomp_last_seg_sz;
18405643Saalok
18415643Saalok /*
18428996SAlok.Aggarwal@Sun.COM * Index size is rounded up to DEV_BSIZE for ease
18435643Saalok * of segmapping
18445643Saalok */
18455643Saalok index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
18465643Saalok header_len = sizeof (lsp->ls_comp_algorithm) +
18475643Saalok sizeof (lsp->ls_uncomp_seg_sz) +
18485643Saalok sizeof (lsp->ls_comp_index_sz) +
18495643Saalok sizeof (lsp->ls_uncomp_last_seg_sz);
18505643Saalok lsp->ls_comp_offbase = header_len + index_sz;
18515643Saalok
18525643Saalok index_sz += header_len;
18535643Saalok index_sz = roundup(index_sz, DEV_BSIZE);
18545643Saalok
18555643Saalok lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
18565643Saalok lsp->ls_comp_index_data_sz = index_sz;
18575643Saalok
18585643Saalok /*
18595643Saalok * Read in the index -- this has a side-effect
18605643Saalok * of reading in the header as well
18615643Saalok */
18625643Saalok rw = UIO_READ;
18635643Saalok error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
18645643Saalok 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
18655643Saalok
18665643Saalok if (error != 0)
18675643Saalok return (error);
18685643Saalok
18695643Saalok /* Skip the header, this is where the index really begins */
18705643Saalok lsp->ls_comp_seg_index =
18715643Saalok /*LINTED*/
18725643Saalok (uint64_t *)(lsp->ls_comp_index_data + header_len);
18735643Saalok
18745643Saalok /*
18755643Saalok * Now recompute offsets in the index to account for
18765643Saalok * the header length
18775643Saalok */
18785643Saalok for (i = 0; i < lsp->ls_comp_index_sz; i++) {
18795643Saalok lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
18805643Saalok BE_64(lsp->ls_comp_seg_index[i]);
18815643Saalok }
18825643Saalok
18835643Saalok return (error);
18845643Saalok }
18855643Saalok
188612633Sjohn.levon@sun.com static int
lofi_init_crypto(struct lofi_state * lsp,struct lofi_ioctl * klip)188712633Sjohn.levon@sun.com lofi_init_crypto(struct lofi_state *lsp, struct lofi_ioctl *klip)
18885643Saalok {
188912633Sjohn.levon@sun.com struct crypto_meta chead;
189012633Sjohn.levon@sun.com char buf[DEV_BSIZE];
189112633Sjohn.levon@sun.com ssize_t resid;
189212633Sjohn.levon@sun.com char *marker;
189312633Sjohn.levon@sun.com int error;
189412633Sjohn.levon@sun.com int ret;
18955643Saalok int i;
18965643Saalok
189712633Sjohn.levon@sun.com if (!klip->li_crypto_enabled)
189812633Sjohn.levon@sun.com return (0);
18995643Saalok
190012633Sjohn.levon@sun.com /*
190112633Sjohn.levon@sun.com * All current algorithms have a max of 448 bits.
190212633Sjohn.levon@sun.com */
190312633Sjohn.levon@sun.com if (klip->li_iv_len > CRYPTO_BITS2BYTES(512))
190412633Sjohn.levon@sun.com return (EINVAL);
19055643Saalok
190612633Sjohn.levon@sun.com if (CRYPTO_BITS2BYTES(klip->li_key_len) > sizeof (klip->li_key))
190712633Sjohn.levon@sun.com return (EINVAL);
190812633Sjohn.levon@sun.com
190912633Sjohn.levon@sun.com lsp->ls_crypto_enabled = klip->li_crypto_enabled;
19100Sstevel@tonic-gate
191112633Sjohn.levon@sun.com mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
19120Sstevel@tonic-gate
191312633Sjohn.levon@sun.com lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
191412633Sjohn.levon@sun.com if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
191512633Sjohn.levon@sun.com cmn_err(CE_WARN, "invalid cipher %s requested for %s",
191612633Sjohn.levon@sun.com klip->li_cipher, klip->li_filename);
191712633Sjohn.levon@sun.com return (EINVAL);
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate
192012633Sjohn.levon@sun.com /* this is just initialization here */
192112633Sjohn.levon@sun.com lsp->ls_mech.cm_param = NULL;
192212633Sjohn.levon@sun.com lsp->ls_mech.cm_param_len = 0;
19238313SDina.Nimeh@Sun.Com
192412633Sjohn.levon@sun.com lsp->ls_iv_type = klip->li_iv_type;
192512633Sjohn.levon@sun.com lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
192612633Sjohn.levon@sun.com if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
192712633Sjohn.levon@sun.com cmn_err(CE_WARN, "invalid iv cipher %s requested"
192812633Sjohn.levon@sun.com " for %s", klip->li_iv_cipher, klip->li_filename);
192912633Sjohn.levon@sun.com return (EINVAL);
19300Sstevel@tonic-gate }
19314451Seschrock
193212633Sjohn.levon@sun.com /* iv mech must itself take a null iv */
193312633Sjohn.levon@sun.com lsp->ls_iv_mech.cm_param = NULL;
193412633Sjohn.levon@sun.com lsp->ls_iv_mech.cm_param_len = 0;
193512633Sjohn.levon@sun.com lsp->ls_iv_len = klip->li_iv_len;
19360Sstevel@tonic-gate
19370Sstevel@tonic-gate /*
193812633Sjohn.levon@sun.com * Create ctx using li_cipher & the raw li_key after checking
193912633Sjohn.levon@sun.com * that it isn't a weak key.
19400Sstevel@tonic-gate */
194112633Sjohn.levon@sun.com lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
194212633Sjohn.levon@sun.com lsp->ls_key.ck_length = klip->li_key_len;
194312633Sjohn.levon@sun.com lsp->ls_key.ck_data = kmem_alloc(
194412633Sjohn.levon@sun.com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
194512633Sjohn.levon@sun.com bcopy(klip->li_key, lsp->ls_key.ck_data,
194612633Sjohn.levon@sun.com CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
194712633Sjohn.levon@sun.com
194812633Sjohn.levon@sun.com ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
194912633Sjohn.levon@sun.com if (ret != CRYPTO_SUCCESS) {
195012633Sjohn.levon@sun.com cmn_err(CE_WARN, "weak key check failed for cipher "
195112633Sjohn.levon@sun.com "%s on file %s (0x%x)", klip->li_cipher,
195212633Sjohn.levon@sun.com klip->li_filename, ret);
195312633Sjohn.levon@sun.com return (EINVAL);
19540Sstevel@tonic-gate }
195512633Sjohn.levon@sun.com
195612633Sjohn.levon@sun.com error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE,
195712633Sjohn.levon@sun.com CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
195812633Sjohn.levon@sun.com if (error != 0)
195912633Sjohn.levon@sun.com return (error);
19600Sstevel@tonic-gate
19615643Saalok /*
196212633Sjohn.levon@sun.com * This is the case where the header in the lofi image is already
196312633Sjohn.levon@sun.com * initialized to indicate it is encrypted.
19645643Saalok */
196512633Sjohn.levon@sun.com if (strncmp(buf, lofi_crypto_magic, sizeof (lofi_crypto_magic)) == 0) {
19668313SDina.Nimeh@Sun.Com /*
19678313SDina.Nimeh@Sun.Com * The encryption header information is laid out this way:
19688313SDina.Nimeh@Sun.Com * 6 bytes: hex "CFLOFI"
19698313SDina.Nimeh@Sun.Com * 2 bytes: version = 0 ... for now
19708313SDina.Nimeh@Sun.Com * 96 bytes: reserved1 (not implemented yet)
19718313SDina.Nimeh@Sun.Com * 4 bytes: data_sector = 2 ... for now
19728313SDina.Nimeh@Sun.Com * more... not implemented yet
19738313SDina.Nimeh@Sun.Com */
19748313SDina.Nimeh@Sun.Com
197512633Sjohn.levon@sun.com marker = buf;
197612633Sjohn.levon@sun.com
19778313SDina.Nimeh@Sun.Com /* copy the magic */
19788313SDina.Nimeh@Sun.Com bcopy(marker, lsp->ls_crypto.magic,
19798313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.magic));
19808313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.magic);
19818313SDina.Nimeh@Sun.Com
19828313SDina.Nimeh@Sun.Com /* read the encryption version number */
19838313SDina.Nimeh@Sun.Com bcopy(marker, &(lsp->ls_crypto.version),
19848313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.version));
19858313SDina.Nimeh@Sun.Com lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
19868313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.version);
19878313SDina.Nimeh@Sun.Com
19888313SDina.Nimeh@Sun.Com /* read a chunk of reserved data */
19898313SDina.Nimeh@Sun.Com bcopy(marker, lsp->ls_crypto.reserved1,
19908313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.reserved1));
19918313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.reserved1);
19928313SDina.Nimeh@Sun.Com
19938313SDina.Nimeh@Sun.Com /* read block number where encrypted data begins */
19948313SDina.Nimeh@Sun.Com bcopy(marker, &(lsp->ls_crypto.data_sector),
19958313SDina.Nimeh@Sun.Com sizeof (lsp->ls_crypto.data_sector));
19968313SDina.Nimeh@Sun.Com lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
19978313SDina.Nimeh@Sun.Com marker += sizeof (lsp->ls_crypto.data_sector);
19988313SDina.Nimeh@Sun.Com
19998313SDina.Nimeh@Sun.Com /* and ignore the rest until it is implemented */
20008313SDina.Nimeh@Sun.Com
20018313SDina.Nimeh@Sun.Com lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
200212633Sjohn.levon@sun.com return (0);
20038313SDina.Nimeh@Sun.Com }
20048313SDina.Nimeh@Sun.Com
20058313SDina.Nimeh@Sun.Com /*
200612633Sjohn.levon@sun.com * We've requested encryption, but no magic was found, so it must be
200712633Sjohn.levon@sun.com * a new image.
20088313SDina.Nimeh@Sun.Com */
200912633Sjohn.levon@sun.com
201012633Sjohn.levon@sun.com for (i = 0; i < sizeof (struct crypto_meta); i++) {
201112633Sjohn.levon@sun.com if (buf[i] != '\0')
201212633Sjohn.levon@sun.com return (EINVAL);
201312633Sjohn.levon@sun.com }
201412633Sjohn.levon@sun.com
201512633Sjohn.levon@sun.com marker = buf;
201612633Sjohn.levon@sun.com bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
201712633Sjohn.levon@sun.com marker += sizeof (lofi_crypto_magic);
201812633Sjohn.levon@sun.com chead.version = htons(LOFI_CRYPTO_VERSION);
201912633Sjohn.levon@sun.com bcopy(&(chead.version), marker, sizeof (chead.version));
202012633Sjohn.levon@sun.com marker += sizeof (chead.version);
202112633Sjohn.levon@sun.com marker += sizeof (chead.reserved1);
202212633Sjohn.levon@sun.com chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
202312633Sjohn.levon@sun.com bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
202412633Sjohn.levon@sun.com
202512633Sjohn.levon@sun.com /* write the header */
202612633Sjohn.levon@sun.com error = vn_rdwr(UIO_WRITE, lsp->ls_vp, buf, DEV_BSIZE,
202712633Sjohn.levon@sun.com CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
202812633Sjohn.levon@sun.com if (error != 0)
202912633Sjohn.levon@sun.com return (error);
203012633Sjohn.levon@sun.com
203112633Sjohn.levon@sun.com /* fix things up so it looks like we read this info */
203212633Sjohn.levon@sun.com bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
203312633Sjohn.levon@sun.com sizeof (lofi_crypto_magic));
203412633Sjohn.levon@sun.com lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
203512633Sjohn.levon@sun.com lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
203612633Sjohn.levon@sun.com lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
203712633Sjohn.levon@sun.com return (0);
203812633Sjohn.levon@sun.com }
203912633Sjohn.levon@sun.com
204012633Sjohn.levon@sun.com /*
204112633Sjohn.levon@sun.com * Check to see if the passed in signature is a valid one. If it is
204212633Sjohn.levon@sun.com * valid, return the index into lofi_compress_table.
204312633Sjohn.levon@sun.com *
204412633Sjohn.levon@sun.com * Return -1 if it is invalid
204512633Sjohn.levon@sun.com */
204612633Sjohn.levon@sun.com static int
lofi_compress_select(const char * signature)204712633Sjohn.levon@sun.com lofi_compress_select(const char *signature)
204812633Sjohn.levon@sun.com {
204912633Sjohn.levon@sun.com int i;
205012633Sjohn.levon@sun.com
205112633Sjohn.levon@sun.com for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
205212633Sjohn.levon@sun.com if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
205312633Sjohn.levon@sun.com return (i);
205412633Sjohn.levon@sun.com }
205512633Sjohn.levon@sun.com
205612633Sjohn.levon@sun.com return (-1);
205712633Sjohn.levon@sun.com }
205812633Sjohn.levon@sun.com
205912633Sjohn.levon@sun.com static int
lofi_init_compress(struct lofi_state * lsp)206012633Sjohn.levon@sun.com lofi_init_compress(struct lofi_state *lsp)
206112633Sjohn.levon@sun.com {
206212633Sjohn.levon@sun.com char buf[DEV_BSIZE];
206312633Sjohn.levon@sun.com int compress_index;
206412633Sjohn.levon@sun.com ssize_t resid;
206512633Sjohn.levon@sun.com int error;
206612633Sjohn.levon@sun.com
206712633Sjohn.levon@sun.com error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
206812633Sjohn.levon@sun.com 0, RLIM64_INFINITY, kcred, &resid);
206912633Sjohn.levon@sun.com
207012633Sjohn.levon@sun.com if (error != 0)
207112633Sjohn.levon@sun.com return (error);
207212633Sjohn.levon@sun.com
207312633Sjohn.levon@sun.com if ((compress_index = lofi_compress_select(buf)) == -1)
207412633Sjohn.levon@sun.com return (0);
207512633Sjohn.levon@sun.com
207612633Sjohn.levon@sun.com /* compression and encryption are mutually exclusive */
207712633Sjohn.levon@sun.com if (lsp->ls_crypto_enabled)
207812633Sjohn.levon@sun.com return (ENOTSUP);
207912633Sjohn.levon@sun.com
208012633Sjohn.levon@sun.com /* initialize compression info for compressed lofi */
208112633Sjohn.levon@sun.com lsp->ls_comp_algorithm_index = compress_index;
208212633Sjohn.levon@sun.com (void) strlcpy(lsp->ls_comp_algorithm,
208312633Sjohn.levon@sun.com lofi_compress_table[compress_index].l_name,
208412633Sjohn.levon@sun.com sizeof (lsp->ls_comp_algorithm));
208512633Sjohn.levon@sun.com
208612633Sjohn.levon@sun.com /* Finally setup per-thread pre-allocated buffers */
208712633Sjohn.levon@sun.com lsp->ls_comp_bufs = kmem_zalloc(lofi_taskq_nthreads *
208812633Sjohn.levon@sun.com sizeof (struct compbuf), KM_SLEEP);
208912633Sjohn.levon@sun.com
209012633Sjohn.levon@sun.com return (lofi_map_compressed_file(lsp, buf));
209112633Sjohn.levon@sun.com }
209212633Sjohn.levon@sun.com
209312633Sjohn.levon@sun.com /*
209412633Sjohn.levon@sun.com * map a file to a minor number. Return the minor number.
209512633Sjohn.levon@sun.com */
209612633Sjohn.levon@sun.com static int
lofi_map_file(dev_t dev,struct lofi_ioctl * ulip,int pickminor,int * rvalp,struct cred * credp,int ioctl_flag)209712633Sjohn.levon@sun.com lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
209812633Sjohn.levon@sun.com int *rvalp, struct cred *credp, int ioctl_flag)
209912633Sjohn.levon@sun.com {
210012633Sjohn.levon@sun.com minor_t minor = (minor_t)-1;
210112633Sjohn.levon@sun.com struct lofi_state *lsp = NULL;
210212633Sjohn.levon@sun.com struct lofi_ioctl *klip;
210312633Sjohn.levon@sun.com int error;
210412633Sjohn.levon@sun.com struct vnode *vp = NULL;
210512633Sjohn.levon@sun.com vattr_t vattr;
210612633Sjohn.levon@sun.com int flag;
210712633Sjohn.levon@sun.com dev_t newdev;
210812633Sjohn.levon@sun.com char namebuf[50];
210912633Sjohn.levon@sun.com
211012633Sjohn.levon@sun.com error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
211112633Sjohn.levon@sun.com if (error != 0)
211212633Sjohn.levon@sun.com return (error);
211312633Sjohn.levon@sun.com
211412633Sjohn.levon@sun.com mutex_enter(&lofi_lock);
211512633Sjohn.levon@sun.com
211612633Sjohn.levon@sun.com mutex_enter(&curproc->p_lock);
211712633Sjohn.levon@sun.com if ((error = rctl_incr_lofi(curproc, curproc->p_zone, 1)) != 0) {
211812633Sjohn.levon@sun.com mutex_exit(&curproc->p_lock);
211912633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
212012633Sjohn.levon@sun.com free_lofi_ioctl(klip);
212112633Sjohn.levon@sun.com return (error);
212212633Sjohn.levon@sun.com }
212312633Sjohn.levon@sun.com mutex_exit(&curproc->p_lock);
212412633Sjohn.levon@sun.com
212512633Sjohn.levon@sun.com if (file_to_lofi_nocheck(klip->li_filename, NULL) == 0) {
212612633Sjohn.levon@sun.com error = EBUSY;
212712633Sjohn.levon@sun.com goto err;
212812633Sjohn.levon@sun.com }
212912633Sjohn.levon@sun.com
213012633Sjohn.levon@sun.com if (pickminor) {
213112633Sjohn.levon@sun.com minor = (minor_t)id_allocff_nosleep(lofi_minor_id);
213212633Sjohn.levon@sun.com if (minor == (minor_t)-1) {
213312633Sjohn.levon@sun.com error = EAGAIN;
213412633Sjohn.levon@sun.com goto err;
21355643Saalok }
213612633Sjohn.levon@sun.com } else {
213712633Sjohn.levon@sun.com if (ddi_get_soft_state(lofi_statep, klip->li_minor) != NULL) {
213812633Sjohn.levon@sun.com error = EEXIST;
213912633Sjohn.levon@sun.com goto err;
214012633Sjohn.levon@sun.com }
214112633Sjohn.levon@sun.com
214212633Sjohn.levon@sun.com minor = (minor_t)
214312633Sjohn.levon@sun.com id_alloc_specific_nosleep(lofi_minor_id, klip->li_minor);
214412633Sjohn.levon@sun.com ASSERT(minor != (minor_t)-1);
214512633Sjohn.levon@sun.com }
214612633Sjohn.levon@sun.com
214712633Sjohn.levon@sun.com flag = FREAD | FWRITE | FOFFMAX | FEXCL;
214812633Sjohn.levon@sun.com error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
214912633Sjohn.levon@sun.com if (error) {
215012633Sjohn.levon@sun.com /* try read-only */
215112633Sjohn.levon@sun.com flag &= ~FWRITE;
215212633Sjohn.levon@sun.com error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
215312633Sjohn.levon@sun.com &vp, 0, 0);
215412633Sjohn.levon@sun.com if (error)
215512633Sjohn.levon@sun.com goto err;
215612633Sjohn.levon@sun.com }
215712633Sjohn.levon@sun.com
215812633Sjohn.levon@sun.com if (!V_ISLOFIABLE(vp->v_type)) {
215912633Sjohn.levon@sun.com error = EINVAL;
216012633Sjohn.levon@sun.com goto err;
216112633Sjohn.levon@sun.com }
216212633Sjohn.levon@sun.com
216312633Sjohn.levon@sun.com vattr.va_mask = AT_SIZE;
216412633Sjohn.levon@sun.com error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
216512633Sjohn.levon@sun.com if (error)
216612633Sjohn.levon@sun.com goto err;
216712633Sjohn.levon@sun.com
216812633Sjohn.levon@sun.com /* the file needs to be a multiple of the block size */
216912633Sjohn.levon@sun.com if ((vattr.va_size % DEV_BSIZE) != 0) {
217012633Sjohn.levon@sun.com error = EINVAL;
217112633Sjohn.levon@sun.com goto err;
217212633Sjohn.levon@sun.com }
217312633Sjohn.levon@sun.com
217412633Sjohn.levon@sun.com /* lsp alloc+init */
217512633Sjohn.levon@sun.com
217612633Sjohn.levon@sun.com error = ddi_soft_state_zalloc(lofi_statep, minor);
217712633Sjohn.levon@sun.com if (error == DDI_FAILURE) {
217812633Sjohn.levon@sun.com error = ENOMEM;
217912633Sjohn.levon@sun.com goto err;
218012633Sjohn.levon@sun.com }
218112633Sjohn.levon@sun.com
218212633Sjohn.levon@sun.com lsp = ddi_get_soft_state(lofi_statep, minor);
218312633Sjohn.levon@sun.com list_insert_tail(&lofi_list, lsp);
218412633Sjohn.levon@sun.com
218512633Sjohn.levon@sun.com newdev = makedevice(getmajor(dev), minor);
218612633Sjohn.levon@sun.com lsp->ls_dev = newdev;
2187*13096SJordan.Vaughan@Sun.com zone_init_ref(&lsp->ls_zone);
2188*13096SJordan.Vaughan@Sun.com zone_hold_ref(curzone, &lsp->ls_zone, ZONE_REF_LOFI);
218912633Sjohn.levon@sun.com lsp->ls_uncomp_seg_sz = 0;
219012633Sjohn.levon@sun.com lsp->ls_comp_algorithm[0] = '\0';
219112633Sjohn.levon@sun.com lsp->ls_crypto_offset = 0;
219212633Sjohn.levon@sun.com
219312633Sjohn.levon@sun.com cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
219412633Sjohn.levon@sun.com mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
219512633Sjohn.levon@sun.com mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL);
219612633Sjohn.levon@sun.com mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
219712633Sjohn.levon@sun.com mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
219812633Sjohn.levon@sun.com
219912633Sjohn.levon@sun.com (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
220012633Sjohn.levon@sun.com LOFI_DRIVER_NAME, minor);
220112633Sjohn.levon@sun.com lsp->ls_taskq = taskq_create_proc(namebuf, lofi_taskq_nthreads,
220212633Sjohn.levon@sun.com minclsyspri, 1, lofi_taskq_maxalloc, curzone->zone_zsched, 0);
220312633Sjohn.levon@sun.com
220412633Sjohn.levon@sun.com list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
220512633Sjohn.levon@sun.com offsetof(struct lofi_comp_cache, lc_list));
220612633Sjohn.levon@sun.com
220712633Sjohn.levon@sun.com /*
220812633Sjohn.levon@sun.com * save open mode so file can be closed properly and vnode counts
220912633Sjohn.levon@sun.com * updated correctly.
221012633Sjohn.levon@sun.com */
221112633Sjohn.levon@sun.com lsp->ls_openflag = flag;
221212633Sjohn.levon@sun.com
221312633Sjohn.levon@sun.com lsp->ls_vp = vp;
221412633Sjohn.levon@sun.com lsp->ls_stacked_vp = vp;
221512633Sjohn.levon@sun.com /*
221612633Sjohn.levon@sun.com * Try to handle stacked lofs vnodes.
221712633Sjohn.levon@sun.com */
221812633Sjohn.levon@sun.com if (vp->v_type == VREG) {
221912633Sjohn.levon@sun.com vnode_t *realvp;
222012633Sjohn.levon@sun.com
222112633Sjohn.levon@sun.com if (VOP_REALVP(vp, &realvp, NULL) == 0) {
222212633Sjohn.levon@sun.com /*
222312633Sjohn.levon@sun.com * We need to use the realvp for uniqueness
222412633Sjohn.levon@sun.com * checking, but keep the stacked vp for
222512633Sjohn.levon@sun.com * LOFI_GET_FILENAME display.
222612633Sjohn.levon@sun.com */
222712633Sjohn.levon@sun.com VN_HOLD(realvp);
222812633Sjohn.levon@sun.com lsp->ls_vp = realvp;
22295643Saalok }
22305643Saalok }
22315643Saalok
223212633Sjohn.levon@sun.com lsp->ls_vp_size = vattr.va_size;
223312633Sjohn.levon@sun.com lsp->ls_vp_comp_size = lsp->ls_vp_size;
223412633Sjohn.levon@sun.com
223512633Sjohn.levon@sun.com lsp->ls_kstat = kstat_create_zone(LOFI_DRIVER_NAME, minor,
223612633Sjohn.levon@sun.com NULL, "disk", KSTAT_TYPE_IO, 1, 0, getzoneid());
223712633Sjohn.levon@sun.com
223812633Sjohn.levon@sun.com if (lsp->ls_kstat == NULL) {
223912633Sjohn.levon@sun.com error = ENOMEM;
224012633Sjohn.levon@sun.com goto err;
224112633Sjohn.levon@sun.com }
224212633Sjohn.levon@sun.com
224312633Sjohn.levon@sun.com lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
224412633Sjohn.levon@sun.com kstat_zone_add(lsp->ls_kstat, GLOBAL_ZONEID);
224512633Sjohn.levon@sun.com
224612633Sjohn.levon@sun.com if ((error = lofi_init_crypto(lsp, klip)) != 0)
224712633Sjohn.levon@sun.com goto err;
224812633Sjohn.levon@sun.com
224912633Sjohn.levon@sun.com if ((error = lofi_init_compress(lsp)) != 0)
225012633Sjohn.levon@sun.com goto err;
225112633Sjohn.levon@sun.com
22520Sstevel@tonic-gate fake_disk_geometry(lsp);
225312633Sjohn.levon@sun.com
225412633Sjohn.levon@sun.com /* create minor nodes */
225512633Sjohn.levon@sun.com
225612633Sjohn.levon@sun.com (void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
225712633Sjohn.levon@sun.com error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, minor,
225812633Sjohn.levon@sun.com DDI_PSEUDO, NULL);
225912633Sjohn.levon@sun.com if (error != DDI_SUCCESS) {
226012633Sjohn.levon@sun.com error = ENXIO;
226112633Sjohn.levon@sun.com goto err;
226212633Sjohn.levon@sun.com }
226312633Sjohn.levon@sun.com
226412633Sjohn.levon@sun.com (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
226512633Sjohn.levon@sun.com error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, minor,
226612633Sjohn.levon@sun.com DDI_PSEUDO, NULL);
226712633Sjohn.levon@sun.com if (error != DDI_SUCCESS) {
226812633Sjohn.levon@sun.com /* remove block node */
226912633Sjohn.levon@sun.com (void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
227012633Sjohn.levon@sun.com ddi_remove_minor_node(lofi_dip, namebuf);
227112633Sjohn.levon@sun.com error = ENXIO;
227212633Sjohn.levon@sun.com goto err;
227312633Sjohn.levon@sun.com }
227412633Sjohn.levon@sun.com
227512633Sjohn.levon@sun.com /* create DDI properties */
227612633Sjohn.levon@sun.com
227712633Sjohn.levon@sun.com if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
227812633Sjohn.levon@sun.com lsp->ls_vp_size - lsp->ls_crypto_offset)) != DDI_PROP_SUCCESS) {
227912633Sjohn.levon@sun.com error = EINVAL;
228012633Sjohn.levon@sun.com goto nodeerr;
228112633Sjohn.levon@sun.com }
228212633Sjohn.levon@sun.com
228312633Sjohn.levon@sun.com if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
228412633Sjohn.levon@sun.com (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE))
228512633Sjohn.levon@sun.com != DDI_PROP_SUCCESS) {
228612633Sjohn.levon@sun.com error = EINVAL;
228712633Sjohn.levon@sun.com goto nodeerr;
228812633Sjohn.levon@sun.com }
228912633Sjohn.levon@sun.com
229012633Sjohn.levon@sun.com if (ddi_prop_update_string(newdev, lofi_dip, ZONE_PROP_NAME,
229112633Sjohn.levon@sun.com (char *)curproc->p_zone->zone_name) != DDI_PROP_SUCCESS) {
229212633Sjohn.levon@sun.com error = EINVAL;
229312633Sjohn.levon@sun.com goto nodeerr;
229412633Sjohn.levon@sun.com }
229512633Sjohn.levon@sun.com
229612633Sjohn.levon@sun.com kstat_install(lsp->ls_kstat);
229712633Sjohn.levon@sun.com
22980Sstevel@tonic-gate mutex_exit(&lofi_lock);
229912633Sjohn.levon@sun.com
230012633Sjohn.levon@sun.com if (rvalp)
230112633Sjohn.levon@sun.com *rvalp = (int)minor;
230212633Sjohn.levon@sun.com klip->li_minor = minor;
23031657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
23040Sstevel@tonic-gate free_lofi_ioctl(klip);
23050Sstevel@tonic-gate return (0);
23060Sstevel@tonic-gate
230712633Sjohn.levon@sun.com nodeerr:
230812633Sjohn.levon@sun.com lofi_free_dev(newdev);
230912633Sjohn.levon@sun.com err:
231012633Sjohn.levon@sun.com if (lsp != NULL) {
231112633Sjohn.levon@sun.com lofi_destroy(lsp, credp);
231212633Sjohn.levon@sun.com } else {
231312633Sjohn.levon@sun.com if (vp != NULL) {
231412633Sjohn.levon@sun.com (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
231512633Sjohn.levon@sun.com VN_RELE(vp);
231612633Sjohn.levon@sun.com }
23178313SDina.Nimeh@Sun.Com
231812633Sjohn.levon@sun.com if (minor != (minor_t)-1)
231912633Sjohn.levon@sun.com id_free(lofi_minor_id, minor);
23208313SDina.Nimeh@Sun.Com
232112633Sjohn.levon@sun.com rctl_decr_lofi(curproc->p_zone, 1);
23228313SDina.Nimeh@Sun.Com }
23238313SDina.Nimeh@Sun.Com
23240Sstevel@tonic-gate mutex_exit(&lofi_lock);
23250Sstevel@tonic-gate free_lofi_ioctl(klip);
23260Sstevel@tonic-gate return (error);
23270Sstevel@tonic-gate }
23280Sstevel@tonic-gate
23290Sstevel@tonic-gate /*
23300Sstevel@tonic-gate * unmap a file.
23310Sstevel@tonic-gate */
23320Sstevel@tonic-gate static int
lofi_unmap_file(struct lofi_ioctl * ulip,int byfilename,struct cred * credp,int ioctl_flag)233312911Sjohn.levon@sun.com lofi_unmap_file(struct lofi_ioctl *ulip, int byfilename,
23341657Sheppo struct cred *credp, int ioctl_flag)
23350Sstevel@tonic-gate {
23360Sstevel@tonic-gate struct lofi_state *lsp;
23370Sstevel@tonic-gate struct lofi_ioctl *klip;
233812633Sjohn.levon@sun.com int err;
23390Sstevel@tonic-gate
234012633Sjohn.levon@sun.com err = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
234112633Sjohn.levon@sun.com if (err != 0)
234212633Sjohn.levon@sun.com return (err);
23430Sstevel@tonic-gate
23440Sstevel@tonic-gate mutex_enter(&lofi_lock);
23450Sstevel@tonic-gate if (byfilename) {
234612633Sjohn.levon@sun.com if ((err = file_to_lofi(klip->li_filename, &lsp)) != 0) {
234712633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
234812633Sjohn.levon@sun.com return (err);
234912633Sjohn.levon@sun.com }
235012633Sjohn.levon@sun.com } else if (klip->li_minor == 0) {
235112633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
235212633Sjohn.levon@sun.com free_lofi_ioctl(klip);
235312633Sjohn.levon@sun.com return (ENXIO);
23540Sstevel@tonic-gate } else {
235512633Sjohn.levon@sun.com lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
23560Sstevel@tonic-gate }
235712633Sjohn.levon@sun.com
235812633Sjohn.levon@sun.com if (lsp == NULL || lsp->ls_vp == NULL || lofi_access(lsp) != 0) {
23590Sstevel@tonic-gate mutex_exit(&lofi_lock);
23600Sstevel@tonic-gate free_lofi_ioctl(klip);
23610Sstevel@tonic-gate return (ENXIO);
23620Sstevel@tonic-gate }
236312633Sjohn.levon@sun.com
236412633Sjohn.levon@sun.com klip->li_minor = getminor(lsp->ls_dev);
23654451Seschrock
23666734Sjohnlev /*
23676734Sjohnlev * If it's still held open, we'll do one of three things:
23686734Sjohnlev *
23696734Sjohnlev * If no flag is set, just return EBUSY.
23706734Sjohnlev *
23716734Sjohnlev * If the 'cleanup' flag is set, unmap and remove the device when
23726734Sjohnlev * the last user finishes.
23736734Sjohnlev *
23746734Sjohnlev * If the 'force' flag is set, then we forcibly close the underlying
23756734Sjohnlev * file. Subsequent operations will fail, and the DKIOCSTATE ioctl
23766734Sjohnlev * will return DKIO_DEV_GONE. When the device is last closed, the
23776734Sjohnlev * device will be cleaned up appropriately.
23786734Sjohnlev *
23796734Sjohnlev * This is complicated by the fact that we may have outstanding
23806734Sjohnlev * dispatched I/Os. Rather than having a single mutex to serialize all
23818313SDina.Nimeh@Sun.Com * I/O, we keep a count of the number of outstanding I/O requests
23828313SDina.Nimeh@Sun.Com * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
23838313SDina.Nimeh@Sun.Com * should be dispatched (ls_vp_closereq).
23848313SDina.Nimeh@Sun.Com *
23856734Sjohnlev * We set the flag, wait for the number of outstanding I/Os to reach 0,
23866734Sjohnlev * and then close the underlying vnode.
23876734Sjohnlev */
23880Sstevel@tonic-gate if (is_opened(lsp)) {
23894451Seschrock if (klip->li_force) {
23904451Seschrock mutex_enter(&lsp->ls_vp_lock);
23914451Seschrock lsp->ls_vp_closereq = B_TRUE;
239211041SEric.Taylor@Sun.COM /* wake up any threads waiting on dkiocstate */
239311041SEric.Taylor@Sun.COM cv_broadcast(&lsp->ls_vp_cv);
23944451Seschrock while (lsp->ls_vp_iocount > 0)
23954451Seschrock cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
23964451Seschrock mutex_exit(&lsp->ls_vp_lock);
23978313SDina.Nimeh@Sun.Com
239812633Sjohn.levon@sun.com goto out;
23996734Sjohnlev } else if (klip->li_cleanup) {
24006734Sjohnlev lsp->ls_cleanup = 1;
24016734Sjohnlev mutex_exit(&lofi_lock);
24026734Sjohnlev free_lofi_ioctl(klip);
24036734Sjohnlev return (0);
24044451Seschrock }
24056734Sjohnlev
24060Sstevel@tonic-gate mutex_exit(&lofi_lock);
24070Sstevel@tonic-gate free_lofi_ioctl(klip);
24080Sstevel@tonic-gate return (EBUSY);
24090Sstevel@tonic-gate }
24100Sstevel@tonic-gate
241112633Sjohn.levon@sun.com out:
241212911Sjohn.levon@sun.com lofi_free_dev(lsp->ls_dev);
241312633Sjohn.levon@sun.com lofi_destroy(lsp, credp);
24140Sstevel@tonic-gate
24150Sstevel@tonic-gate mutex_exit(&lofi_lock);
24161657Sheppo (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
24170Sstevel@tonic-gate free_lofi_ioctl(klip);
24180Sstevel@tonic-gate return (0);
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate
24210Sstevel@tonic-gate /*
24220Sstevel@tonic-gate * get the filename given the minor number, or the minor number given
24230Sstevel@tonic-gate * the name.
24240Sstevel@tonic-gate */
24254451Seschrock /*ARGSUSED*/
24260Sstevel@tonic-gate static int
lofi_get_info(dev_t dev,struct lofi_ioctl * ulip,int which,struct cred * credp,int ioctl_flag)24270Sstevel@tonic-gate lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
24281657Sheppo struct cred *credp, int ioctl_flag)
24290Sstevel@tonic-gate {
243012633Sjohn.levon@sun.com struct lofi_ioctl *klip;
24310Sstevel@tonic-gate struct lofi_state *lsp;
24320Sstevel@tonic-gate int error;
24330Sstevel@tonic-gate
243412633Sjohn.levon@sun.com error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
243512633Sjohn.levon@sun.com if (error != 0)
243612633Sjohn.levon@sun.com return (error);
24370Sstevel@tonic-gate
24380Sstevel@tonic-gate switch (which) {
24390Sstevel@tonic-gate case LOFI_GET_FILENAME:
244012633Sjohn.levon@sun.com if (klip->li_minor == 0) {
24410Sstevel@tonic-gate free_lofi_ioctl(klip);
24420Sstevel@tonic-gate return (EINVAL);
24430Sstevel@tonic-gate }
24440Sstevel@tonic-gate
24450Sstevel@tonic-gate mutex_enter(&lofi_lock);
244612633Sjohn.levon@sun.com lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
244712633Sjohn.levon@sun.com if (lsp == NULL || lofi_access(lsp) != 0) {
24480Sstevel@tonic-gate mutex_exit(&lofi_lock);
24490Sstevel@tonic-gate free_lofi_ioctl(klip);
24500Sstevel@tonic-gate return (ENXIO);
24510Sstevel@tonic-gate }
245212633Sjohn.levon@sun.com
245312633Sjohn.levon@sun.com /*
245412633Sjohn.levon@sun.com * This may fail if, for example, we're trying to look
245512633Sjohn.levon@sun.com * up a zoned NFS path from the global zone.
245612633Sjohn.levon@sun.com */
245712633Sjohn.levon@sun.com if (vnodetopath(NULL, lsp->ls_stacked_vp, klip->li_filename,
245812633Sjohn.levon@sun.com sizeof (klip->li_filename), CRED()) != 0) {
245912633Sjohn.levon@sun.com (void) strlcpy(klip->li_filename, "?",
246012633Sjohn.levon@sun.com sizeof (klip->li_filename));
246112633Sjohn.levon@sun.com }
246212633Sjohn.levon@sun.com
24635643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
24645643Saalok sizeof (klip->li_algorithm));
24658313SDina.Nimeh@Sun.Com klip->li_crypto_enabled = lsp->ls_crypto_enabled;
24660Sstevel@tonic-gate mutex_exit(&lofi_lock);
24671657Sheppo error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
24680Sstevel@tonic-gate free_lofi_ioctl(klip);
24690Sstevel@tonic-gate return (error);
24700Sstevel@tonic-gate case LOFI_GET_MINOR:
24710Sstevel@tonic-gate mutex_enter(&lofi_lock);
247212633Sjohn.levon@sun.com error = file_to_lofi(klip->li_filename, &lsp);
247312633Sjohn.levon@sun.com if (error == 0)
247412633Sjohn.levon@sun.com klip->li_minor = getminor(lsp->ls_dev);
24750Sstevel@tonic-gate mutex_exit(&lofi_lock);
247612633Sjohn.levon@sun.com
247712633Sjohn.levon@sun.com if (error == 0)
247812633Sjohn.levon@sun.com error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
247912633Sjohn.levon@sun.com
24800Sstevel@tonic-gate free_lofi_ioctl(klip);
24810Sstevel@tonic-gate return (error);
24825643Saalok case LOFI_CHECK_COMPRESSED:
24835643Saalok mutex_enter(&lofi_lock);
248412633Sjohn.levon@sun.com error = file_to_lofi(klip->li_filename, &lsp);
248512633Sjohn.levon@sun.com if (error != 0) {
24865643Saalok mutex_exit(&lofi_lock);
24875643Saalok free_lofi_ioctl(klip);
248812633Sjohn.levon@sun.com return (error);
24895643Saalok }
24905643Saalok
249112633Sjohn.levon@sun.com klip->li_minor = getminor(lsp->ls_dev);
24925643Saalok (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
24935643Saalok sizeof (klip->li_algorithm));
249412633Sjohn.levon@sun.com
24955643Saalok mutex_exit(&lofi_lock);
24965643Saalok error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
24975643Saalok free_lofi_ioctl(klip);
24985643Saalok return (error);
24990Sstevel@tonic-gate default:
25000Sstevel@tonic-gate free_lofi_ioctl(klip);
25010Sstevel@tonic-gate return (EINVAL);
25020Sstevel@tonic-gate }
25030Sstevel@tonic-gate }
25040Sstevel@tonic-gate
25050Sstevel@tonic-gate static int
lofi_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * credp,int * rvalp)25060Sstevel@tonic-gate lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
25070Sstevel@tonic-gate int *rvalp)
25080Sstevel@tonic-gate {
25090Sstevel@tonic-gate int error;
25100Sstevel@tonic-gate enum dkio_state dkstate;
25110Sstevel@tonic-gate struct lofi_state *lsp;
25120Sstevel@tonic-gate minor_t minor;
25130Sstevel@tonic-gate
25140Sstevel@tonic-gate minor = getminor(dev);
25150Sstevel@tonic-gate /* lofi ioctls only apply to the master device */
25160Sstevel@tonic-gate if (minor == 0) {
25170Sstevel@tonic-gate struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
25180Sstevel@tonic-gate
25190Sstevel@tonic-gate /*
25200Sstevel@tonic-gate * the query command only need read-access - i.e., normal
25210Sstevel@tonic-gate * users are allowed to do those on the ctl device as
25220Sstevel@tonic-gate * long as they can open it read-only.
25230Sstevel@tonic-gate */
25240Sstevel@tonic-gate switch (cmd) {
25250Sstevel@tonic-gate case LOFI_MAP_FILE:
25260Sstevel@tonic-gate if ((flag & FWRITE) == 0)
25270Sstevel@tonic-gate return (EPERM);
25281657Sheppo return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
25290Sstevel@tonic-gate case LOFI_MAP_FILE_MINOR:
25300Sstevel@tonic-gate if ((flag & FWRITE) == 0)
25310Sstevel@tonic-gate return (EPERM);
25321657Sheppo return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
25330Sstevel@tonic-gate case LOFI_UNMAP_FILE:
25340Sstevel@tonic-gate if ((flag & FWRITE) == 0)
25350Sstevel@tonic-gate return (EPERM);
253612911Sjohn.levon@sun.com return (lofi_unmap_file(lip, 1, credp, flag));
25370Sstevel@tonic-gate case LOFI_UNMAP_FILE_MINOR:
25380Sstevel@tonic-gate if ((flag & FWRITE) == 0)
25390Sstevel@tonic-gate return (EPERM);
254012911Sjohn.levon@sun.com return (lofi_unmap_file(lip, 0, credp, flag));
25410Sstevel@tonic-gate case LOFI_GET_FILENAME:
25420Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
25431657Sheppo credp, flag));
25440Sstevel@tonic-gate case LOFI_GET_MINOR:
25450Sstevel@tonic-gate return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
25461657Sheppo credp, flag));
254712633Sjohn.levon@sun.com
254812633Sjohn.levon@sun.com /*
254912633Sjohn.levon@sun.com * This API made limited sense when this value was fixed
255012633Sjohn.levon@sun.com * at LOFI_MAX_FILES. However, its use to iterate
255112633Sjohn.levon@sun.com * across all possible devices in lofiadm means we don't
255212633Sjohn.levon@sun.com * want to return L_MAXMIN32, but the highest
255312633Sjohn.levon@sun.com * *allocated* minor.
255412633Sjohn.levon@sun.com */
25550Sstevel@tonic-gate case LOFI_GET_MAXMINOR:
255612633Sjohn.levon@sun.com minor = 0;
255712633Sjohn.levon@sun.com
255812633Sjohn.levon@sun.com mutex_enter(&lofi_lock);
255912633Sjohn.levon@sun.com
256012633Sjohn.levon@sun.com for (lsp = list_head(&lofi_list); lsp != NULL;
256112633Sjohn.levon@sun.com lsp = list_next(&lofi_list, lsp)) {
256212633Sjohn.levon@sun.com if (lofi_access(lsp) != 0)
256312633Sjohn.levon@sun.com continue;
256412633Sjohn.levon@sun.com
256512633Sjohn.levon@sun.com if (getminor(lsp->ls_dev) > minor)
256612633Sjohn.levon@sun.com minor = getminor(lsp->ls_dev);
256712633Sjohn.levon@sun.com }
256812633Sjohn.levon@sun.com
256912633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
257012633Sjohn.levon@sun.com
257112633Sjohn.levon@sun.com error = ddi_copyout(&minor, &lip->li_minor,
257212633Sjohn.levon@sun.com sizeof (minor), flag);
25730Sstevel@tonic-gate if (error)
25740Sstevel@tonic-gate return (EFAULT);
25750Sstevel@tonic-gate return (0);
257612633Sjohn.levon@sun.com
25775643Saalok case LOFI_CHECK_COMPRESSED:
25785643Saalok return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
25795643Saalok credp, flag));
25800Sstevel@tonic-gate default:
258112633Sjohn.levon@sun.com return (EINVAL);
25820Sstevel@tonic-gate }
25830Sstevel@tonic-gate }
25840Sstevel@tonic-gate
258511041SEric.Taylor@Sun.COM mutex_enter(&lofi_lock);
25860Sstevel@tonic-gate lsp = ddi_get_soft_state(lofi_statep, minor);
258711041SEric.Taylor@Sun.COM if (lsp == NULL || lsp->ls_vp_closereq) {
258811041SEric.Taylor@Sun.COM mutex_exit(&lofi_lock);
25890Sstevel@tonic-gate return (ENXIO);
259011041SEric.Taylor@Sun.COM }
259111041SEric.Taylor@Sun.COM mutex_exit(&lofi_lock);
25920Sstevel@tonic-gate
25934451Seschrock /*
25944451Seschrock * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
25954451Seschrock * EIO as if the device was no longer present.
25964451Seschrock */
25974451Seschrock if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
25984451Seschrock return (EIO);
25994451Seschrock
26000Sstevel@tonic-gate /* these are for faking out utilities like newfs */
26010Sstevel@tonic-gate switch (cmd) {
26020Sstevel@tonic-gate case DKIOCGVTOC:
26030Sstevel@tonic-gate switch (ddi_model_convert_from(flag & FMODELS)) {
26040Sstevel@tonic-gate case DDI_MODEL_ILP32: {
26050Sstevel@tonic-gate struct vtoc32 vtoc32;
26060Sstevel@tonic-gate
26070Sstevel@tonic-gate vtoctovtoc32(lsp->ls_vtoc, vtoc32);
26080Sstevel@tonic-gate if (ddi_copyout(&vtoc32, (void *)arg,
26090Sstevel@tonic-gate sizeof (struct vtoc32), flag))
26100Sstevel@tonic-gate return (EFAULT);
26118719SDina.Nimeh@Sun.COM break;
26120Sstevel@tonic-gate }
26130Sstevel@tonic-gate
26140Sstevel@tonic-gate case DDI_MODEL_NONE:
26150Sstevel@tonic-gate if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
26160Sstevel@tonic-gate sizeof (struct vtoc), flag))
26170Sstevel@tonic-gate return (EFAULT);
26180Sstevel@tonic-gate break;
26190Sstevel@tonic-gate }
26200Sstevel@tonic-gate return (0);
26210Sstevel@tonic-gate case DKIOCINFO:
26221657Sheppo error = ddi_copyout(&lsp->ls_ci, (void *)arg,
26231657Sheppo sizeof (struct dk_cinfo), flag);
26240Sstevel@tonic-gate if (error)
26250Sstevel@tonic-gate return (EFAULT);
26260Sstevel@tonic-gate return (0);
26270Sstevel@tonic-gate case DKIOCG_VIRTGEOM:
26280Sstevel@tonic-gate case DKIOCG_PHYGEOM:
26290Sstevel@tonic-gate case DKIOCGGEOM:
26301657Sheppo error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
26311657Sheppo sizeof (struct dk_geom), flag);
26320Sstevel@tonic-gate if (error)
26330Sstevel@tonic-gate return (EFAULT);
26340Sstevel@tonic-gate return (0);
26350Sstevel@tonic-gate case DKIOCSTATE:
26364451Seschrock /*
26374451Seschrock * Normally, lofi devices are always in the INSERTED state. If
26384451Seschrock * a device is forcefully unmapped, then the device transitions
26394451Seschrock * to the DKIO_DEV_GONE state.
26404451Seschrock */
26414451Seschrock if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
26424451Seschrock flag) != 0)
26434451Seschrock return (EFAULT);
26444451Seschrock
26454451Seschrock mutex_enter(&lsp->ls_vp_lock);
264611041SEric.Taylor@Sun.COM lsp->ls_vp_iocount++;
264711041SEric.Taylor@Sun.COM while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
264811041SEric.Taylor@Sun.COM (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
264911041SEric.Taylor@Sun.COM !lsp->ls_vp_closereq) {
26504451Seschrock /*
26514451Seschrock * By virtue of having the device open, we know that
26524451Seschrock * 'lsp' will remain valid when we return.
26534451Seschrock */
26544451Seschrock if (!cv_wait_sig(&lsp->ls_vp_cv,
26554451Seschrock &lsp->ls_vp_lock)) {
265611041SEric.Taylor@Sun.COM lsp->ls_vp_iocount--;
265711041SEric.Taylor@Sun.COM cv_broadcast(&lsp->ls_vp_cv);
26584451Seschrock mutex_exit(&lsp->ls_vp_lock);
26594451Seschrock return (EINTR);
26604451Seschrock }
26614451Seschrock }
26624451Seschrock
266311041SEric.Taylor@Sun.COM dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ?
266411041SEric.Taylor@Sun.COM DKIO_INSERTED : DKIO_DEV_GONE);
266511041SEric.Taylor@Sun.COM lsp->ls_vp_iocount--;
266611041SEric.Taylor@Sun.COM cv_broadcast(&lsp->ls_vp_cv);
26674451Seschrock mutex_exit(&lsp->ls_vp_lock);
26684451Seschrock
26694451Seschrock if (ddi_copyout(&dkstate, (void *)arg,
26704451Seschrock sizeof (dkstate), flag) != 0)
26710Sstevel@tonic-gate return (EFAULT);
26720Sstevel@tonic-gate return (0);
26730Sstevel@tonic-gate default:
26740Sstevel@tonic-gate return (ENOTTY);
26750Sstevel@tonic-gate }
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate
26780Sstevel@tonic-gate static struct cb_ops lofi_cb_ops = {
26790Sstevel@tonic-gate lofi_open, /* open */
26800Sstevel@tonic-gate lofi_close, /* close */
26810Sstevel@tonic-gate lofi_strategy, /* strategy */
26820Sstevel@tonic-gate nodev, /* print */
26830Sstevel@tonic-gate nodev, /* dump */
26840Sstevel@tonic-gate lofi_read, /* read */
26850Sstevel@tonic-gate lofi_write, /* write */
26860Sstevel@tonic-gate lofi_ioctl, /* ioctl */
26870Sstevel@tonic-gate nodev, /* devmap */
26880Sstevel@tonic-gate nodev, /* mmap */
26890Sstevel@tonic-gate nodev, /* segmap */
26900Sstevel@tonic-gate nochpoll, /* poll */
26910Sstevel@tonic-gate ddi_prop_op, /* prop_op */
26920Sstevel@tonic-gate 0, /* streamtab */
26930Sstevel@tonic-gate D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */
26940Sstevel@tonic-gate CB_REV,
26950Sstevel@tonic-gate lofi_aread,
26960Sstevel@tonic-gate lofi_awrite
26970Sstevel@tonic-gate };
26980Sstevel@tonic-gate
26990Sstevel@tonic-gate static struct dev_ops lofi_ops = {
27000Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
27010Sstevel@tonic-gate 0, /* refcnt */
27020Sstevel@tonic-gate lofi_info, /* info */
27030Sstevel@tonic-gate nulldev, /* identify */
27040Sstevel@tonic-gate nulldev, /* probe */
27050Sstevel@tonic-gate lofi_attach, /* attach */
27060Sstevel@tonic-gate lofi_detach, /* detach */
27070Sstevel@tonic-gate nodev, /* reset */
27080Sstevel@tonic-gate &lofi_cb_ops, /* driver operations */
27097656SSherry.Moore@Sun.COM NULL, /* no bus operations */
27107656SSherry.Moore@Sun.COM NULL, /* power */
27118313SDina.Nimeh@Sun.Com ddi_quiesce_not_needed, /* quiesce */
27120Sstevel@tonic-gate };
27130Sstevel@tonic-gate
27140Sstevel@tonic-gate static struct modldrv modldrv = {
27150Sstevel@tonic-gate &mod_driverops,
27167656SSherry.Moore@Sun.COM "loopback file driver",
27170Sstevel@tonic-gate &lofi_ops,
27180Sstevel@tonic-gate };
27190Sstevel@tonic-gate
27200Sstevel@tonic-gate static struct modlinkage modlinkage = {
27210Sstevel@tonic-gate MODREV_1,
27220Sstevel@tonic-gate &modldrv,
27230Sstevel@tonic-gate NULL
27240Sstevel@tonic-gate };
27250Sstevel@tonic-gate
27260Sstevel@tonic-gate int
_init(void)27270Sstevel@tonic-gate _init(void)
27280Sstevel@tonic-gate {
27290Sstevel@tonic-gate int error;
27300Sstevel@tonic-gate
273112633Sjohn.levon@sun.com list_create(&lofi_list, sizeof (struct lofi_state),
273212633Sjohn.levon@sun.com offsetof(struct lofi_state, ls_list));
273312633Sjohn.levon@sun.com
27340Sstevel@tonic-gate error = ddi_soft_state_init(&lofi_statep,
27350Sstevel@tonic-gate sizeof (struct lofi_state), 0);
27360Sstevel@tonic-gate if (error)
27370Sstevel@tonic-gate return (error);
27380Sstevel@tonic-gate
27390Sstevel@tonic-gate mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
274012633Sjohn.levon@sun.com
27410Sstevel@tonic-gate error = mod_install(&modlinkage);
27420Sstevel@tonic-gate if (error) {
27430Sstevel@tonic-gate mutex_destroy(&lofi_lock);
27440Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep);
274512633Sjohn.levon@sun.com list_destroy(&lofi_list);
27460Sstevel@tonic-gate }
27470Sstevel@tonic-gate
27480Sstevel@tonic-gate return (error);
27490Sstevel@tonic-gate }
27500Sstevel@tonic-gate
27510Sstevel@tonic-gate int
_fini(void)27520Sstevel@tonic-gate _fini(void)
27530Sstevel@tonic-gate {
27540Sstevel@tonic-gate int error;
27550Sstevel@tonic-gate
275612633Sjohn.levon@sun.com mutex_enter(&lofi_lock);
275712633Sjohn.levon@sun.com
275812633Sjohn.levon@sun.com if (!list_is_empty(&lofi_list)) {
275912633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
27600Sstevel@tonic-gate return (EBUSY);
276112633Sjohn.levon@sun.com }
276212633Sjohn.levon@sun.com
276312633Sjohn.levon@sun.com mutex_exit(&lofi_lock);
27640Sstevel@tonic-gate
27650Sstevel@tonic-gate error = mod_remove(&modlinkage);
27660Sstevel@tonic-gate if (error)
27670Sstevel@tonic-gate return (error);
27680Sstevel@tonic-gate
27690Sstevel@tonic-gate mutex_destroy(&lofi_lock);
27700Sstevel@tonic-gate ddi_soft_state_fini(&lofi_statep);
277112633Sjohn.levon@sun.com list_destroy(&lofi_list);
27720Sstevel@tonic-gate
27730Sstevel@tonic-gate return (error);
27740Sstevel@tonic-gate }
27750Sstevel@tonic-gate
27760Sstevel@tonic-gate int
_info(struct modinfo * modinfop)27770Sstevel@tonic-gate _info(struct modinfo *modinfop)
27780Sstevel@tonic-gate {
27790Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
27800Sstevel@tonic-gate }
2781