xref: /titanic_53/usr/src/uts/common/sys/lofi.h (revision b9c7fb0341177452c7a4f2e0e27a07610e9087a9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53d7072f8Seschrock  * Common Development and Distribution License (the "License").
63d7072f8Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b9c7fb03SAlok Aggarwal  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef	_SYS_LOFI_H
277c478bd9Sstevel@tonic-gate #define	_SYS_LOFI_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/time.h>
317c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
327c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
337c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
354058a205Sjrgn.keil@googlemail.com #include <sys/list.h>
367d82f0f8SDina K Nimeh #include <sys/crypto/api.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
397c478bd9Sstevel@tonic-gate extern "C" {
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * /dev names:
447c478bd9Sstevel@tonic-gate  *	/dev/lofictl	- master control device
457c478bd9Sstevel@tonic-gate  *	/dev/lofi	- block devices, named by minor number
467c478bd9Sstevel@tonic-gate  *	/dev/rlofi	- character devices, named by minor number
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate #define	LOFI_DRIVER_NAME	"lofi"
497c478bd9Sstevel@tonic-gate #define	LOFI_CTL_NODE		"ctl"
507c478bd9Sstevel@tonic-gate #define	LOFI_CTL_NAME		LOFI_DRIVER_NAME LOFI_CTL_NODE
517c478bd9Sstevel@tonic-gate #define	LOFI_BLOCK_NAME		LOFI_DRIVER_NAME
527c478bd9Sstevel@tonic-gate #define	LOFI_CHAR_NAME		"r" LOFI_DRIVER_NAME
537c478bd9Sstevel@tonic-gate 
5487117650Saalok #define	SEGHDR		1
5587117650Saalok #define	COMPRESSED	1
5687117650Saalok #define	UNCOMPRESSED	0
5787117650Saalok #define	MAXALGLEN	36
5887117650Saalok 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * Use is:
627c478bd9Sstevel@tonic-gate  *	ld = open("/dev/lofictl", O_RDWR | O_EXCL);
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * lofi must be opened exclusively. Access is controlled by permissions on
657c478bd9Sstevel@tonic-gate  * the device, which is 644 by default. Write-access is required for ioctls
667c478bd9Sstevel@tonic-gate  * that change state, but only read-access is required for the ioctls that
677c478bd9Sstevel@tonic-gate  * return information. Basically, only root can add and remove files, but
687c478bd9Sstevel@tonic-gate  * non-root can look at the current lists.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * ioctl usage:
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * kernel ioctls
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
757c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_MAP_FILE, &li);
767c478bd9Sstevel@tonic-gate  *	newminor = li.li_minor;
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
797c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_UNMAP_FILE, &li);
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
827c478bd9Sstevel@tonic-gate  *	li.li_minor = minor_number;
837c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_MAP_FILE_MINOR, &li);
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *	li.li_minor = minor_number;
867c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_UNMAP_FILE_MINOR, &li);
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *	li.li_minor = minor_number;
897c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_GET_FILENAME, &li);
907d82f0f8SDina K Nimeh  *	filename = li.li_filename;
917d82f0f8SDina K Nimeh  *	encrypted = li.li_crypto_enabled;
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  *	strcpy(li.li_filename, "somefilename");
947c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_GET_MINOR, &li);
957d82f0f8SDina K Nimeh  *	minor = li.li_minor;
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	li.li_minor = 0;
987c478bd9Sstevel@tonic-gate  *	ioctl(ld, LOFI_GET_MAXMINOR, &li);
997c478bd9Sstevel@tonic-gate  *	maxminor = li.li_minor;
1007c478bd9Sstevel@tonic-gate  *
10187117650Saalok  *	strcpy(li.li_filename, "somefilename");
10287117650Saalok  *	li.li_minor = 0;
10387117650Saalok  *	ioctl(ld, LOFI_CHECK_COMPRESSED, &li);
10487117650Saalok  *
1053d7072f8Seschrock  * If the 'li_force' flag is set for any of the LOFI_UNMAP_* commands, then if
1063d7072f8Seschrock  * the device is busy, the underlying vnode will be closed, and any subsequent
1073d7072f8Seschrock  * operations will fail.  It will behave as if the device had been forcibly
1083d7072f8Seschrock  * removed, so the DKIOCSTATE ioctl will return DKIO_DEV_GONE.  When the device
1093d7072f8Seschrock  * is last closed, it will be torn down.
1103d7072f8Seschrock  *
11193239addSjohnlev  * If the 'li_cleanup' flag is set for any of the LOFI_UNMAP_* commands, then
11293239addSjohnlev  * if the device is busy, it is marked for removal at the next time it is
11393239addSjohnlev  * no longer held open by anybody.  When the device is last closed, it will be
11493239addSjohnlev  * torn down.
11593239addSjohnlev  *
1167c478bd9Sstevel@tonic-gate  * Oh, and last but not least: these ioctls are totally private and only
1177c478bd9Sstevel@tonic-gate  * for use by lofiadm(1M).
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217d82f0f8SDina K Nimeh typedef enum	iv_method {
1227d82f0f8SDina K Nimeh 	IVM_NONE,	/* no iv needed, iv is null */
1237d82f0f8SDina K Nimeh 	IVM_ENC_BLKNO	/* iv is logical block no. encrypted */
1247d82f0f8SDina K Nimeh } iv_method_t;
1257d82f0f8SDina K Nimeh 
1267c478bd9Sstevel@tonic-gate struct lofi_ioctl {
1277c478bd9Sstevel@tonic-gate 	uint32_t 	li_minor;
1283d7072f8Seschrock 	boolean_t	li_force;
12993239addSjohnlev 	boolean_t	li_cleanup;
1306f02aa44SDina K Nimeh 	char	li_filename[MAXPATHLEN];
1317d82f0f8SDina K Nimeh 
1327d82f0f8SDina K Nimeh 	/* the following fields are required for compression support */
13387117650Saalok 	char	li_algorithm[MAXALGLEN];
1347d82f0f8SDina K Nimeh 
1357d82f0f8SDina K Nimeh 	/* the following fields are required for encryption support */
1367d82f0f8SDina K Nimeh 	boolean_t	li_crypto_enabled;
1377d82f0f8SDina K Nimeh 	crypto_mech_name_t	li_cipher;	/* for data */
1387d82f0f8SDina K Nimeh 	uint32_t	li_key_len;		/* for data */
1397d82f0f8SDina K Nimeh 	char		li_key[56];	/* for data: max 448-bit Blowfish key */
1407d82f0f8SDina K Nimeh 	crypto_mech_name_t	li_iv_cipher;	/* for iv derivation */
1417d82f0f8SDina K Nimeh 	uint32_t	li_iv_len;		/* for iv derivation */
1427d82f0f8SDina K Nimeh 	iv_method_t	li_iv_type;		/* for iv derivation */
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #define	LOFI_IOC_BASE		(('L' << 16) | ('F' << 8))
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #define	LOFI_MAP_FILE		(LOFI_IOC_BASE | 0x01)
1487c478bd9Sstevel@tonic-gate #define	LOFI_MAP_FILE_MINOR	(LOFI_IOC_BASE | 0x02)
1497c478bd9Sstevel@tonic-gate #define	LOFI_UNMAP_FILE		(LOFI_IOC_BASE | 0x03)
1507c478bd9Sstevel@tonic-gate #define	LOFI_UNMAP_FILE_MINOR	(LOFI_IOC_BASE | 0x04)
1517c478bd9Sstevel@tonic-gate #define	LOFI_GET_FILENAME	(LOFI_IOC_BASE | 0x05)
1527c478bd9Sstevel@tonic-gate #define	LOFI_GET_MINOR		(LOFI_IOC_BASE | 0x06)
1537c478bd9Sstevel@tonic-gate #define	LOFI_GET_MAXMINOR	(LOFI_IOC_BASE | 0x07)
15487117650Saalok #define	LOFI_CHECK_COMPRESSED	(LOFI_IOC_BASE | 0x08)
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * file types that might be usable with lofi, maybe. Only regular
1587c478bd9Sstevel@tonic-gate  * files are documented though.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate #define	S_ISLOFIABLE(mode) \
1617c478bd9Sstevel@tonic-gate 	(S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode))
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
1647c478bd9Sstevel@tonic-gate 
1654058a205Sjrgn.keil@googlemail.com 
1664058a205Sjrgn.keil@googlemail.com /*
1674058a205Sjrgn.keil@googlemail.com  * Cache decompressed data segments for the compressed lofi images.
1684058a205Sjrgn.keil@googlemail.com  *
1694058a205Sjrgn.keil@googlemail.com  * To avoid that we have to decompress data of a compressed
1704058a205Sjrgn.keil@googlemail.com  * segment multiple times when accessing parts of the segment's
1714058a205Sjrgn.keil@googlemail.com  * data we cache the uncompressed data, using a simple linked list.
1724058a205Sjrgn.keil@googlemail.com  */
1734058a205Sjrgn.keil@googlemail.com struct lofi_comp_cache {
1744058a205Sjrgn.keil@googlemail.com 	list_node_t	lc_list;		/* linked list */
1754058a205Sjrgn.keil@googlemail.com 	uchar_t		*lc_data;		/* decompressed segment data */
1764058a205Sjrgn.keil@googlemail.com 	uint64_t	lc_index;		/* segment index */
1774058a205Sjrgn.keil@googlemail.com };
1784058a205Sjrgn.keil@googlemail.com 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * We limit the maximum number of active lofi devices to 128, which seems very
1817c478bd9Sstevel@tonic-gate  * large. You can tune this by changing lofi_max_files in /etc/system.
1827c478bd9Sstevel@tonic-gate  * If you change it dynamically, which you probably shouldn't do, make sure
1837c478bd9Sstevel@tonic-gate  * to only _increase_ it.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate #define	LOFI_MAX_FILES	128
1867c478bd9Sstevel@tonic-gate extern uint32_t lofi_max_files;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #define	V_ISLOFIABLE(vtype) \
1897c478bd9Sstevel@tonic-gate 	((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR))
1907c478bd9Sstevel@tonic-gate 
1917d82f0f8SDina K Nimeh /*
192*b9c7fb03SAlok Aggarwal  * Pre-allocated memory buffers for the purpose of compression
193*b9c7fb03SAlok Aggarwal  */
194*b9c7fb03SAlok Aggarwal struct compbuf {
195*b9c7fb03SAlok Aggarwal 	void		*buf;
196*b9c7fb03SAlok Aggarwal 	uint32_t	bufsize;
197*b9c7fb03SAlok Aggarwal 	int		inuse;
198*b9c7fb03SAlok Aggarwal };
199*b9c7fb03SAlok Aggarwal 
200*b9c7fb03SAlok Aggarwal /*
2017d82f0f8SDina K Nimeh  * Need exactly 6 bytes to identify encrypted lofi image
2027d82f0f8SDina K Nimeh  */
2037d82f0f8SDina K Nimeh extern const char lofi_crypto_magic[6];
2047d82f0f8SDina K Nimeh #define	LOFI_CRYPTO_MAGIC	{ 'C', 'F', 'L', 'O', 'F', 'I' }
2057d82f0f8SDina K Nimeh #define	LOFI_CRYPTO_VERSION	((uint16_t)0)
2067d82f0f8SDina K Nimeh #define	LOFI_CRYPTO_DATA_SECTOR	((uint32_t)16)		/* for version 0 */
2077d82f0f8SDina K Nimeh 
2087d82f0f8SDina K Nimeh /*
2097d82f0f8SDina K Nimeh  * Crypto metadata for encrypted lofi images
2107d82f0f8SDina K Nimeh  * The fields here only satisfy initial implementation requirements.
2117d82f0f8SDina K Nimeh  */
2127d82f0f8SDina K Nimeh struct crypto_meta {
2137d82f0f8SDina K Nimeh 	char		magic[6];		/* LOFI_CRYPTO_MAGIC */
2147d82f0f8SDina K Nimeh 	uint16_t	version;		/* version of encrypted lofi */
2157d82f0f8SDina K Nimeh 	char		reserved1[96];		/* future use */
2167d82f0f8SDina K Nimeh 	uint32_t	data_sector;		/* start of data area */
2177d82f0f8SDina K Nimeh 	char		pad[404];		/* end on DEV_BSIZE bdry */
2187d82f0f8SDina K Nimeh 	/* second header block is not defined at this time */
2197d82f0f8SDina K Nimeh };
2207d82f0f8SDina K Nimeh 
2217c478bd9Sstevel@tonic-gate struct lofi_state {
2227c478bd9Sstevel@tonic-gate 	char		*ls_filename;	/* filename to open */
2237c478bd9Sstevel@tonic-gate 	size_t		ls_filename_sz;
2247c478bd9Sstevel@tonic-gate 	struct vnode	*ls_vp;		/* open vnode */
2253d7072f8Seschrock 	kmutex_t	ls_vp_lock;	/* protects ls_vp */
2263d7072f8Seschrock 	kcondvar_t	ls_vp_cv;	/* signal changes to ls_vp */
2273d7072f8Seschrock 	uint32_t	ls_vp_iocount;	/* # pending I/O requests */
2283d7072f8Seschrock 	boolean_t	ls_vp_closereq;	/* force close requested */
2297c478bd9Sstevel@tonic-gate 	u_offset_t	ls_vp_size;
2307c478bd9Sstevel@tonic-gate 	uint32_t	ls_blk_open;
2317c478bd9Sstevel@tonic-gate 	uint32_t	ls_chr_open;
2327c478bd9Sstevel@tonic-gate 	uint32_t	ls_lyr_open_count;
2337c478bd9Sstevel@tonic-gate 	int		ls_openflag;
23493239addSjohnlev 	boolean_t	ls_cleanup;	/* cleanup on close */
2357c478bd9Sstevel@tonic-gate 	taskq_t		*ls_taskq;
2367c478bd9Sstevel@tonic-gate 	kstat_t		*ls_kstat;
2377c478bd9Sstevel@tonic-gate 	kmutex_t	ls_kstat_lock;
2387c478bd9Sstevel@tonic-gate 	struct dk_geom	ls_dkg;
2397c478bd9Sstevel@tonic-gate 	struct vtoc	ls_vtoc;
2407c478bd9Sstevel@tonic-gate 	struct dk_cinfo	ls_ci;
24187117650Saalok 
24287117650Saalok 	/* the following fields are required for compression support */
24387117650Saalok 	int		ls_comp_algorithm_index; /* idx into compress_table */
24487117650Saalok 	char		ls_comp_algorithm[MAXALGLEN];
24587117650Saalok 	uint32_t	ls_uncomp_seg_sz; /* sz of uncompressed segment */
24687117650Saalok 	uint32_t	ls_comp_index_sz; /* number of index entries */
24787117650Saalok 	uint32_t	ls_comp_seg_shift; /* exponent for byte shift */
24887117650Saalok 	uint32_t	ls_uncomp_last_seg_sz; /* sz of last uncomp segment */
24987117650Saalok 	uint64_t	ls_comp_offbase; /* offset of actual compressed data */
25087117650Saalok 	uint64_t	*ls_comp_seg_index; /* array of index entries */
25187117650Saalok 	caddr_t		ls_comp_index_data; /* index pages loaded from file */
25287117650Saalok 	uint32_t	ls_comp_index_data_sz;
25387117650Saalok 	u_offset_t	ls_vp_comp_size; /* actual compressed file size */
2547d82f0f8SDina K Nimeh 
255*b9c7fb03SAlok Aggarwal 	/* pre-allocated list of buffers for compressed segment data */
256*b9c7fb03SAlok Aggarwal 	kmutex_t	ls_comp_bufs_lock;
257*b9c7fb03SAlok Aggarwal 	struct compbuf	*ls_comp_bufs;
258*b9c7fb03SAlok Aggarwal 
2594058a205Sjrgn.keil@googlemail.com 	/* lock and anchor for compressed segment caching */
2604058a205Sjrgn.keil@googlemail.com 	kmutex_t	ls_comp_cache_lock;	/* protects ls_comp_cache */
2614058a205Sjrgn.keil@googlemail.com 	list_t		ls_comp_cache;		/* cached decompressed segs */
2624058a205Sjrgn.keil@googlemail.com 	uint32_t	ls_comp_cache_count;
2634058a205Sjrgn.keil@googlemail.com 
2647d82f0f8SDina K Nimeh 	/* the following fields are required for encryption support */
2657d82f0f8SDina K Nimeh 	boolean_t		ls_crypto_enabled;
2667d82f0f8SDina K Nimeh 	u_offset_t		ls_crypto_offset;	/* crypto meta size */
2677d82f0f8SDina K Nimeh 	struct crypto_meta	ls_crypto;
2687d82f0f8SDina K Nimeh 	crypto_mechanism_t	ls_mech;	/* for data encr/decr */
2697d82f0f8SDina K Nimeh 	crypto_key_t		ls_key;		/* for data encr/decr */
2707d82f0f8SDina K Nimeh 	crypto_mechanism_t	ls_iv_mech;	/* for iv derivation */
2717d82f0f8SDina K Nimeh 	size_t			ls_iv_len;	/* for iv derivation */
2727d82f0f8SDina K Nimeh 	iv_method_t		ls_iv_type;	/* for iv derivation */
2737d82f0f8SDina K Nimeh 	kmutex_t		ls_crypto_lock;
2747d82f0f8SDina K Nimeh 	crypto_ctx_template_t	ls_ctx_tmpl;
2757d82f0f8SDina K Nimeh 
2767c478bd9Sstevel@tonic-gate };
2777c478bd9Sstevel@tonic-gate 
27887117650Saalok #endif	/* _KERNEL */
27987117650Saalok 
28087117650Saalok /*
28187117650Saalok  * Common signature for all lofi compress functions
28287117650Saalok  */
28387117650Saalok typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst,
28487117650Saalok 	size_t *destlen, int level);
28587117650Saalok 
28687117650Saalok /*
28787117650Saalok  * Information about each compression function
28887117650Saalok  */
28987117650Saalok typedef struct lofi_compress_info {
29087117650Saalok 	lofi_compress_func_t	*l_decompress;
29187117650Saalok 	lofi_compress_func_t	*l_compress;
29287117650Saalok 	int			l_level;
29387117650Saalok 	char			*l_name;	/* algorithm name */
29487117650Saalok } lofi_compress_info_t;
29587117650Saalok 
29687117650Saalok enum lofi_compress {
29787117650Saalok 	LOFI_COMPRESS_GZIP = 0,
29887117650Saalok 	LOFI_COMPRESS_GZIP_6 = 1,
29987117650Saalok 	LOFI_COMPRESS_GZIP_9 = 2,
300b1efbcd6SAlok Aggarwal 	LOFI_COMPRESS_LZMA = 3,
30187117650Saalok 	LOFI_COMPRESS_FUNCTIONS
30287117650Saalok };
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate #endif
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate #endif	/* _SYS_LOFI_H */
309