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 54451Seschrock * Common Development and Distribution License (the "License"). 64451Seschrock * 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 /* 228996SAlok.Aggarwal@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate 270Sstevel@tonic-gate #ifndef _SYS_LOFI_H 280Sstevel@tonic-gate #define _SYS_LOFI_H 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/time.h> 320Sstevel@tonic-gate #include <sys/taskq.h> 330Sstevel@tonic-gate #include <sys/vtoc.h> 340Sstevel@tonic-gate #include <sys/dkio.h> 350Sstevel@tonic-gate #include <sys/vnode.h> 36*9048Sjrgn.keil@googlemail.com #include <sys/list.h> 378313SDina.Nimeh@Sun.Com #include <sys/crypto/api.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate #ifdef __cplusplus 400Sstevel@tonic-gate extern "C" { 410Sstevel@tonic-gate #endif 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * /dev names: 450Sstevel@tonic-gate * /dev/lofictl - master control device 460Sstevel@tonic-gate * /dev/lofi - block devices, named by minor number 470Sstevel@tonic-gate * /dev/rlofi - character devices, named by minor number 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate #define LOFI_DRIVER_NAME "lofi" 500Sstevel@tonic-gate #define LOFI_CTL_NODE "ctl" 510Sstevel@tonic-gate #define LOFI_CTL_NAME LOFI_DRIVER_NAME LOFI_CTL_NODE 520Sstevel@tonic-gate #define LOFI_BLOCK_NAME LOFI_DRIVER_NAME 530Sstevel@tonic-gate #define LOFI_CHAR_NAME "r" LOFI_DRIVER_NAME 540Sstevel@tonic-gate 555643Saalok #define SEGHDR 1 565643Saalok #define COMPRESSED 1 575643Saalok #define UNCOMPRESSED 0 585643Saalok #define MAXALGLEN 36 595643Saalok 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * 620Sstevel@tonic-gate * Use is: 630Sstevel@tonic-gate * ld = open("/dev/lofictl", O_RDWR | O_EXCL); 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * lofi must be opened exclusively. Access is controlled by permissions on 660Sstevel@tonic-gate * the device, which is 644 by default. Write-access is required for ioctls 670Sstevel@tonic-gate * that change state, but only read-access is required for the ioctls that 680Sstevel@tonic-gate * return information. Basically, only root can add and remove files, but 690Sstevel@tonic-gate * non-root can look at the current lists. 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * ioctl usage: 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * kernel ioctls 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * strcpy(li.li_filename, "somefilename"); 760Sstevel@tonic-gate * ioctl(ld, LOFI_MAP_FILE, &li); 770Sstevel@tonic-gate * newminor = li.li_minor; 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * strcpy(li.li_filename, "somefilename"); 800Sstevel@tonic-gate * ioctl(ld, LOFI_UNMAP_FILE, &li); 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * strcpy(li.li_filename, "somefilename"); 830Sstevel@tonic-gate * li.li_minor = minor_number; 840Sstevel@tonic-gate * ioctl(ld, LOFI_MAP_FILE_MINOR, &li); 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * li.li_minor = minor_number; 870Sstevel@tonic-gate * ioctl(ld, LOFI_UNMAP_FILE_MINOR, &li); 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * li.li_minor = minor_number; 900Sstevel@tonic-gate * ioctl(ld, LOFI_GET_FILENAME, &li); 918313SDina.Nimeh@Sun.Com * filename = li.li_filename; 928313SDina.Nimeh@Sun.Com * encrypted = li.li_crypto_enabled; 930Sstevel@tonic-gate * 940Sstevel@tonic-gate * strcpy(li.li_filename, "somefilename"); 950Sstevel@tonic-gate * ioctl(ld, LOFI_GET_MINOR, &li); 968313SDina.Nimeh@Sun.Com * minor = li.li_minor; 970Sstevel@tonic-gate * 980Sstevel@tonic-gate * li.li_minor = 0; 990Sstevel@tonic-gate * ioctl(ld, LOFI_GET_MAXMINOR, &li); 1000Sstevel@tonic-gate * maxminor = li.li_minor; 1010Sstevel@tonic-gate * 1025643Saalok * strcpy(li.li_filename, "somefilename"); 1035643Saalok * li.li_minor = 0; 1045643Saalok * ioctl(ld, LOFI_CHECK_COMPRESSED, &li); 1055643Saalok * 1064451Seschrock * If the 'li_force' flag is set for any of the LOFI_UNMAP_* commands, then if 1074451Seschrock * the device is busy, the underlying vnode will be closed, and any subsequent 1084451Seschrock * operations will fail. It will behave as if the device had been forcibly 1094451Seschrock * removed, so the DKIOCSTATE ioctl will return DKIO_DEV_GONE. When the device 1104451Seschrock * is last closed, it will be torn down. 1114451Seschrock * 1126734Sjohnlev * If the 'li_cleanup' flag is set for any of the LOFI_UNMAP_* commands, then 1136734Sjohnlev * if the device is busy, it is marked for removal at the next time it is 1146734Sjohnlev * no longer held open by anybody. When the device is last closed, it will be 1156734Sjohnlev * torn down. 1166734Sjohnlev * 1170Sstevel@tonic-gate * Oh, and last but not least: these ioctls are totally private and only 1180Sstevel@tonic-gate * for use by lofiadm(1M). 1190Sstevel@tonic-gate * 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate 1228313SDina.Nimeh@Sun.Com typedef enum iv_method { 1238313SDina.Nimeh@Sun.Com IVM_NONE, /* no iv needed, iv is null */ 1248313SDina.Nimeh@Sun.Com IVM_ENC_BLKNO /* iv is logical block no. encrypted */ 1258313SDina.Nimeh@Sun.Com } iv_method_t; 1268313SDina.Nimeh@Sun.Com 1270Sstevel@tonic-gate struct lofi_ioctl { 1284451Seschrock uint32_t li_minor; 1294451Seschrock boolean_t li_force; 1306734Sjohnlev boolean_t li_cleanup; 1318081SDina.Nimeh@Sun.Com char li_filename[MAXPATHLEN]; 1328313SDina.Nimeh@Sun.Com 1338313SDina.Nimeh@Sun.Com /* the following fields are required for compression support */ 1345643Saalok char li_algorithm[MAXALGLEN]; 1358313SDina.Nimeh@Sun.Com 1368313SDina.Nimeh@Sun.Com /* the following fields are required for encryption support */ 1378313SDina.Nimeh@Sun.Com boolean_t li_crypto_enabled; 1388313SDina.Nimeh@Sun.Com crypto_mech_name_t li_cipher; /* for data */ 1398313SDina.Nimeh@Sun.Com uint32_t li_key_len; /* for data */ 1408313SDina.Nimeh@Sun.Com char li_key[56]; /* for data: max 448-bit Blowfish key */ 1418313SDina.Nimeh@Sun.Com crypto_mech_name_t li_iv_cipher; /* for iv derivation */ 1428313SDina.Nimeh@Sun.Com uint32_t li_iv_len; /* for iv derivation */ 1438313SDina.Nimeh@Sun.Com iv_method_t li_iv_type; /* for iv derivation */ 1440Sstevel@tonic-gate }; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate #define LOFI_IOC_BASE (('L' << 16) | ('F' << 8)) 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate #define LOFI_MAP_FILE (LOFI_IOC_BASE | 0x01) 1490Sstevel@tonic-gate #define LOFI_MAP_FILE_MINOR (LOFI_IOC_BASE | 0x02) 1500Sstevel@tonic-gate #define LOFI_UNMAP_FILE (LOFI_IOC_BASE | 0x03) 1510Sstevel@tonic-gate #define LOFI_UNMAP_FILE_MINOR (LOFI_IOC_BASE | 0x04) 1520Sstevel@tonic-gate #define LOFI_GET_FILENAME (LOFI_IOC_BASE | 0x05) 1530Sstevel@tonic-gate #define LOFI_GET_MINOR (LOFI_IOC_BASE | 0x06) 1540Sstevel@tonic-gate #define LOFI_GET_MAXMINOR (LOFI_IOC_BASE | 0x07) 1555643Saalok #define LOFI_CHECK_COMPRESSED (LOFI_IOC_BASE | 0x08) 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * file types that might be usable with lofi, maybe. Only regular 1590Sstevel@tonic-gate * files are documented though. 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate #define S_ISLOFIABLE(mode) \ 1620Sstevel@tonic-gate (S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode)) 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate #if defined(_KERNEL) 1650Sstevel@tonic-gate 166*9048Sjrgn.keil@googlemail.com 167*9048Sjrgn.keil@googlemail.com /* 168*9048Sjrgn.keil@googlemail.com * Cache decompressed data segments for the compressed lofi images. 169*9048Sjrgn.keil@googlemail.com * 170*9048Sjrgn.keil@googlemail.com * To avoid that we have to decompress data of a compressed 171*9048Sjrgn.keil@googlemail.com * segment multiple times when accessing parts of the segment's 172*9048Sjrgn.keil@googlemail.com * data we cache the uncompressed data, using a simple linked list. 173*9048Sjrgn.keil@googlemail.com */ 174*9048Sjrgn.keil@googlemail.com struct lofi_comp_cache { 175*9048Sjrgn.keil@googlemail.com list_node_t lc_list; /* linked list */ 176*9048Sjrgn.keil@googlemail.com uchar_t *lc_data; /* decompressed segment data */ 177*9048Sjrgn.keil@googlemail.com uint64_t lc_index; /* segment index */ 178*9048Sjrgn.keil@googlemail.com }; 179*9048Sjrgn.keil@googlemail.com 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * We limit the maximum number of active lofi devices to 128, which seems very 1820Sstevel@tonic-gate * large. You can tune this by changing lofi_max_files in /etc/system. 1830Sstevel@tonic-gate * If you change it dynamically, which you probably shouldn't do, make sure 1840Sstevel@tonic-gate * to only _increase_ it. 1850Sstevel@tonic-gate */ 1860Sstevel@tonic-gate #define LOFI_MAX_FILES 128 1870Sstevel@tonic-gate extern uint32_t lofi_max_files; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate #define V_ISLOFIABLE(vtype) \ 1900Sstevel@tonic-gate ((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR)) 1910Sstevel@tonic-gate 1928313SDina.Nimeh@Sun.Com /* 1938313SDina.Nimeh@Sun.Com * Need exactly 6 bytes to identify encrypted lofi image 1948313SDina.Nimeh@Sun.Com */ 1958313SDina.Nimeh@Sun.Com extern const char lofi_crypto_magic[6]; 1968313SDina.Nimeh@Sun.Com #define LOFI_CRYPTO_MAGIC { 'C', 'F', 'L', 'O', 'F', 'I' } 1978313SDina.Nimeh@Sun.Com #define LOFI_CRYPTO_VERSION ((uint16_t)0) 1988313SDina.Nimeh@Sun.Com #define LOFI_CRYPTO_DATA_SECTOR ((uint32_t)16) /* for version 0 */ 1998313SDina.Nimeh@Sun.Com 2008313SDina.Nimeh@Sun.Com /* 2018313SDina.Nimeh@Sun.Com * Crypto metadata for encrypted lofi images 2028313SDina.Nimeh@Sun.Com * The fields here only satisfy initial implementation requirements. 2038313SDina.Nimeh@Sun.Com */ 2048313SDina.Nimeh@Sun.Com struct crypto_meta { 2058313SDina.Nimeh@Sun.Com char magic[6]; /* LOFI_CRYPTO_MAGIC */ 2068313SDina.Nimeh@Sun.Com uint16_t version; /* version of encrypted lofi */ 2078313SDina.Nimeh@Sun.Com char reserved1[96]; /* future use */ 2088313SDina.Nimeh@Sun.Com uint32_t data_sector; /* start of data area */ 2098313SDina.Nimeh@Sun.Com char pad[404]; /* end on DEV_BSIZE bdry */ 2108313SDina.Nimeh@Sun.Com /* second header block is not defined at this time */ 2118313SDina.Nimeh@Sun.Com }; 2128313SDina.Nimeh@Sun.Com 2130Sstevel@tonic-gate struct lofi_state { 2144451Seschrock char *ls_filename; /* filename to open */ 2154451Seschrock size_t ls_filename_sz; 2164451Seschrock struct vnode *ls_vp; /* open vnode */ 2174451Seschrock kmutex_t ls_vp_lock; /* protects ls_vp */ 2184451Seschrock kcondvar_t ls_vp_cv; /* signal changes to ls_vp */ 2194451Seschrock uint32_t ls_vp_iocount; /* # pending I/O requests */ 2204451Seschrock boolean_t ls_vp_closereq; /* force close requested */ 2210Sstevel@tonic-gate u_offset_t ls_vp_size; 2220Sstevel@tonic-gate uint32_t ls_blk_open; 2230Sstevel@tonic-gate uint32_t ls_chr_open; 2240Sstevel@tonic-gate uint32_t ls_lyr_open_count; 2250Sstevel@tonic-gate int ls_openflag; 2266734Sjohnlev boolean_t ls_cleanup; /* cleanup on close */ 2270Sstevel@tonic-gate taskq_t *ls_taskq; 2280Sstevel@tonic-gate kstat_t *ls_kstat; 2290Sstevel@tonic-gate kmutex_t ls_kstat_lock; 2300Sstevel@tonic-gate struct dk_geom ls_dkg; 2310Sstevel@tonic-gate struct vtoc ls_vtoc; 2320Sstevel@tonic-gate struct dk_cinfo ls_ci; 2335643Saalok 2345643Saalok /* the following fields are required for compression support */ 2355643Saalok int ls_comp_algorithm_index; /* idx into compress_table */ 2365643Saalok char ls_comp_algorithm[MAXALGLEN]; 2375643Saalok uint32_t ls_uncomp_seg_sz; /* sz of uncompressed segment */ 2385643Saalok uint32_t ls_comp_index_sz; /* number of index entries */ 2395643Saalok uint32_t ls_comp_seg_shift; /* exponent for byte shift */ 2405643Saalok uint32_t ls_uncomp_last_seg_sz; /* sz of last uncomp segment */ 2415643Saalok uint64_t ls_comp_offbase; /* offset of actual compressed data */ 2425643Saalok uint64_t *ls_comp_seg_index; /* array of index entries */ 2435643Saalok caddr_t ls_comp_index_data; /* index pages loaded from file */ 2445643Saalok uint32_t ls_comp_index_data_sz; 2455643Saalok u_offset_t ls_vp_comp_size; /* actual compressed file size */ 2468313SDina.Nimeh@Sun.Com 247*9048Sjrgn.keil@googlemail.com /* lock and anchor for compressed segment caching */ 248*9048Sjrgn.keil@googlemail.com kmutex_t ls_comp_cache_lock; /* protects ls_comp_cache */ 249*9048Sjrgn.keil@googlemail.com list_t ls_comp_cache; /* cached decompressed segs */ 250*9048Sjrgn.keil@googlemail.com uint32_t ls_comp_cache_count; 251*9048Sjrgn.keil@googlemail.com 2528313SDina.Nimeh@Sun.Com /* the following fields are required for encryption support */ 2538313SDina.Nimeh@Sun.Com boolean_t ls_crypto_enabled; 2548313SDina.Nimeh@Sun.Com u_offset_t ls_crypto_offset; /* crypto meta size */ 2558313SDina.Nimeh@Sun.Com struct crypto_meta ls_crypto; 2568313SDina.Nimeh@Sun.Com crypto_mechanism_t ls_mech; /* for data encr/decr */ 2578313SDina.Nimeh@Sun.Com crypto_key_t ls_key; /* for data encr/decr */ 2588313SDina.Nimeh@Sun.Com crypto_mechanism_t ls_iv_mech; /* for iv derivation */ 2598313SDina.Nimeh@Sun.Com size_t ls_iv_len; /* for iv derivation */ 2608313SDina.Nimeh@Sun.Com iv_method_t ls_iv_type; /* for iv derivation */ 2618313SDina.Nimeh@Sun.Com kmutex_t ls_crypto_lock; 2628313SDina.Nimeh@Sun.Com crypto_ctx_template_t ls_ctx_tmpl; 2638313SDina.Nimeh@Sun.Com 2640Sstevel@tonic-gate }; 2650Sstevel@tonic-gate 2665643Saalok #endif /* _KERNEL */ 2675643Saalok 2685643Saalok /* 2695643Saalok * Common signature for all lofi compress functions 2705643Saalok */ 2715643Saalok typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst, 2725643Saalok size_t *destlen, int level); 2735643Saalok 2745643Saalok /* 2755643Saalok * Information about each compression function 2765643Saalok */ 2775643Saalok typedef struct lofi_compress_info { 2785643Saalok lofi_compress_func_t *l_decompress; 2795643Saalok lofi_compress_func_t *l_compress; 2805643Saalok int l_level; 2815643Saalok char *l_name; /* algorithm name */ 2825643Saalok } lofi_compress_info_t; 2835643Saalok 2845643Saalok enum lofi_compress { 2855643Saalok LOFI_COMPRESS_GZIP = 0, 2865643Saalok LOFI_COMPRESS_GZIP_6 = 1, 2875643Saalok LOFI_COMPRESS_GZIP_9 = 2, 2888996SAlok.Aggarwal@Sun.COM LOFI_COMPRESS_LZMA = 3, 2895643Saalok LOFI_COMPRESS_FUNCTIONS 2905643Saalok }; 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate #ifdef __cplusplus 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate #endif 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate #endif /* _SYS_LOFI_H */ 297