xref: /titanic_44/usr/src/uts/common/sys/lofi.h (revision 3f7d54a6b84904c8f4d8daa4c7b577bede7df8b9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 
26 #ifndef	_SYS_LOFI_H
27 #define	_SYS_LOFI_H
28 
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/taskq.h>
32 #include <sys/vtoc.h>
33 #include <sys/dkio.h>
34 #include <sys/vnode.h>
35 #include <sys/list.h>
36 #include <sys/crypto/api.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * /dev names:
44  *	/dev/lofictl	- master control device
45  *	/dev/lofi	- block devices, named by minor number
46  *	/dev/rlofi	- character devices, named by minor number
47  */
48 #define	LOFI_DRIVER_NAME	"lofi"
49 #define	LOFI_CTL_NODE		"ctl"
50 #define	LOFI_CTL_NAME		LOFI_DRIVER_NAME LOFI_CTL_NODE
51 #define	LOFI_BLOCK_NAME		LOFI_DRIVER_NAME
52 #define	LOFI_CHAR_NAME		"r" LOFI_DRIVER_NAME
53 
54 #define	SEGHDR		1
55 #define	COMPRESSED	1
56 #define	UNCOMPRESSED	0
57 #define	MAXALGLEN	36
58 
59 /*
60  *
61  * Use is:
62  *	ld = open("/dev/lofictl", O_RDWR | O_EXCL);
63  *
64  * lofi must be opened exclusively. Access is controlled by permissions on
65  * the device, which is 644 by default. Write-access is required for ioctls
66  * that change state, but only read-access is required for the ioctls that
67  * return information. Basically, only root can add and remove files, but
68  * non-root can look at the current lists.
69  *
70  * ioctl usage:
71  *
72  * kernel ioctls
73  *
74  *	strcpy(li.li_filename, "somefilename");
75  *	ioctl(ld, LOFI_MAP_FILE, &li);
76  *	newminor = li.li_minor;
77  *
78  *	strcpy(li.li_filename, "somefilename");
79  *	ioctl(ld, LOFI_UNMAP_FILE, &li);
80  *
81  *	strcpy(li.li_filename, "somefilename");
82  *	li.li_minor = minor_number;
83  *	ioctl(ld, LOFI_MAP_FILE_MINOR, &li);
84  *
85  *	li.li_minor = minor_number;
86  *	ioctl(ld, LOFI_UNMAP_FILE_MINOR, &li);
87  *
88  *	li.li_minor = minor_number;
89  *	ioctl(ld, LOFI_GET_FILENAME, &li);
90  *	filename = li.li_filename;
91  *	encrypted = li.li_crypto_enabled;
92  *
93  *	strcpy(li.li_filename, "somefilename");
94  *	ioctl(ld, LOFI_GET_MINOR, &li);
95  *	minor = li.li_minor;
96  *
97  *	li.li_minor = 0;
98  *	ioctl(ld, LOFI_GET_MAXMINOR, &li);
99  *	maxminor = li.li_minor;
100  *
101  *	strcpy(li.li_filename, "somefilename");
102  *	li.li_minor = 0;
103  *	ioctl(ld, LOFI_CHECK_COMPRESSED, &li);
104  *
105  * If the 'li_force' flag is set for any of the LOFI_UNMAP_* commands, then if
106  * the device is busy, the underlying vnode will be closed, and any subsequent
107  * operations will fail.  It will behave as if the device had been forcibly
108  * removed, so the DKIOCSTATE ioctl will return DKIO_DEV_GONE.  When the device
109  * is last closed, it will be torn down.
110  *
111  * If the 'li_cleanup' flag is set for any of the LOFI_UNMAP_* commands, then
112  * if the device is busy, it is marked for removal at the next time it is
113  * no longer held open by anybody.  When the device is last closed, it will be
114  * torn down.
115  *
116  * Oh, and last but not least: these ioctls are totally private and only
117  * for use by lofiadm(1M).
118  *
119  */
120 
121 typedef enum	iv_method {
122 	IVM_NONE,	/* no iv needed, iv is null */
123 	IVM_ENC_BLKNO	/* iv is logical block no. encrypted */
124 } iv_method_t;
125 
126 struct lofi_ioctl {
127 	uint32_t 	li_minor;
128 	boolean_t	li_force;
129 	boolean_t	li_cleanup;
130 	char	li_filename[MAXPATHLEN];
131 
132 	/* the following fields are required for compression support */
133 	char	li_algorithm[MAXALGLEN];
134 
135 	/* the following fields are required for encryption support */
136 	boolean_t	li_crypto_enabled;
137 	crypto_mech_name_t	li_cipher;	/* for data */
138 	uint32_t	li_key_len;		/* for data */
139 	char		li_key[56];	/* for data: max 448-bit Blowfish key */
140 	crypto_mech_name_t	li_iv_cipher;	/* for iv derivation */
141 	uint32_t	li_iv_len;		/* for iv derivation */
142 	iv_method_t	li_iv_type;		/* for iv derivation */
143 };
144 
145 #define	LOFI_IOC_BASE		(('L' << 16) | ('F' << 8))
146 
147 #define	LOFI_MAP_FILE		(LOFI_IOC_BASE | 0x01)
148 #define	LOFI_MAP_FILE_MINOR	(LOFI_IOC_BASE | 0x02)
149 #define	LOFI_UNMAP_FILE		(LOFI_IOC_BASE | 0x03)
150 #define	LOFI_UNMAP_FILE_MINOR	(LOFI_IOC_BASE | 0x04)
151 #define	LOFI_GET_FILENAME	(LOFI_IOC_BASE | 0x05)
152 #define	LOFI_GET_MINOR		(LOFI_IOC_BASE | 0x06)
153 #define	LOFI_GET_MAXMINOR	(LOFI_IOC_BASE | 0x07)
154 #define	LOFI_CHECK_COMPRESSED	(LOFI_IOC_BASE | 0x08)
155 
156 /*
157  * file types that might be usable with lofi, maybe. Only regular
158  * files are documented though.
159  */
160 #define	S_ISLOFIABLE(mode) \
161 	(S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode))
162 
163 #if defined(_KERNEL)
164 
165 
166 /*
167  * Cache decompressed data segments for the compressed lofi images.
168  *
169  * To avoid that we have to decompress data of a compressed
170  * segment multiple times when accessing parts of the segment's
171  * data we cache the uncompressed data, using a simple linked list.
172  */
173 struct lofi_comp_cache {
174 	list_node_t	lc_list;		/* linked list */
175 	uchar_t		*lc_data;		/* decompressed segment data */
176 	uint64_t	lc_index;		/* segment index */
177 };
178 
179 /*
180  * We limit the maximum number of active lofi devices to 128, which seems very
181  * large. You can tune this by changing lofi_max_files in /etc/system.
182  * If you change it dynamically, which you probably shouldn't do, make sure
183  * to only _increase_ it.
184  */
185 #define	LOFI_MAX_FILES	128
186 extern uint32_t lofi_max_files;
187 
188 #define	V_ISLOFIABLE(vtype) \
189 	((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR))
190 
191 /*
192  * Pre-allocated memory buffers for the purpose of compression
193  */
194 struct compbuf {
195 	void		*buf;
196 	uint32_t	bufsize;
197 	int		inuse;
198 };
199 
200 /*
201  * Need exactly 6 bytes to identify encrypted lofi image
202  */
203 extern const char lofi_crypto_magic[6];
204 #define	LOFI_CRYPTO_MAGIC	{ 'C', 'F', 'L', 'O', 'F', 'I' }
205 #define	LOFI_CRYPTO_VERSION	((uint16_t)0)
206 #define	LOFI_CRYPTO_DATA_SECTOR	((uint32_t)16)		/* for version 0 */
207 
208 /*
209  * Crypto metadata for encrypted lofi images
210  * The fields here only satisfy initial implementation requirements.
211  */
212 struct crypto_meta {
213 	char		magic[6];		/* LOFI_CRYPTO_MAGIC */
214 	uint16_t	version;		/* version of encrypted lofi */
215 	char		reserved1[96];		/* future use */
216 	uint32_t	data_sector;		/* start of data area */
217 	char		pad[404];		/* end on DEV_BSIZE bdry */
218 	/* second header block is not defined at this time */
219 };
220 
221 struct lofi_state {
222 	char		*ls_filename;	/* filename to open */
223 	size_t		ls_filename_sz;
224 	struct vnode	*ls_vp;		/* open vnode */
225 	kmutex_t	ls_vp_lock;	/* protects ls_vp */
226 	kcondvar_t	ls_vp_cv;	/* signal changes to ls_vp */
227 	uint32_t	ls_vp_iocount;	/* # pending I/O requests */
228 	boolean_t	ls_vp_closereq;	/* force close requested */
229 	u_offset_t	ls_vp_size;
230 	uint32_t	ls_blk_open;
231 	uint32_t	ls_chr_open;
232 	uint32_t	ls_lyr_open_count;
233 	int		ls_openflag;
234 	boolean_t	ls_cleanup;	/* cleanup on close */
235 	taskq_t		*ls_taskq;
236 	kstat_t		*ls_kstat;
237 	kmutex_t	ls_kstat_lock;
238 	struct dk_geom	ls_dkg;
239 	struct vtoc	ls_vtoc;
240 	struct dk_cinfo	ls_ci;
241 
242 	/* the following fields are required for compression support */
243 	int		ls_comp_algorithm_index; /* idx into compress_table */
244 	char		ls_comp_algorithm[MAXALGLEN];
245 	uint32_t	ls_uncomp_seg_sz; /* sz of uncompressed segment */
246 	uint32_t	ls_comp_index_sz; /* number of index entries */
247 	uint32_t	ls_comp_seg_shift; /* exponent for byte shift */
248 	uint32_t	ls_uncomp_last_seg_sz; /* sz of last uncomp segment */
249 	uint64_t	ls_comp_offbase; /* offset of actual compressed data */
250 	uint64_t	*ls_comp_seg_index; /* array of index entries */
251 	caddr_t		ls_comp_index_data; /* index pages loaded from file */
252 	uint32_t	ls_comp_index_data_sz;
253 	u_offset_t	ls_vp_comp_size; /* actual compressed file size */
254 
255 	/* pre-allocated list of buffers for compressed segment data */
256 	kmutex_t	ls_comp_bufs_lock;
257 	struct compbuf	*ls_comp_bufs;
258 
259 	/* lock and anchor for compressed segment caching */
260 	kmutex_t	ls_comp_cache_lock;	/* protects ls_comp_cache */
261 	list_t		ls_comp_cache;		/* cached decompressed segs */
262 	uint32_t	ls_comp_cache_count;
263 
264 	/* the following fields are required for encryption support */
265 	boolean_t		ls_crypto_enabled;
266 	u_offset_t		ls_crypto_offset;	/* crypto meta size */
267 	struct crypto_meta	ls_crypto;
268 	crypto_mechanism_t	ls_mech;	/* for data encr/decr */
269 	crypto_key_t		ls_key;		/* for data encr/decr */
270 	crypto_mechanism_t	ls_iv_mech;	/* for iv derivation */
271 	size_t			ls_iv_len;	/* for iv derivation */
272 	iv_method_t		ls_iv_type;	/* for iv derivation */
273 	kmutex_t		ls_crypto_lock;
274 	crypto_ctx_template_t	ls_ctx_tmpl;
275 
276 };
277 
278 #endif	/* _KERNEL */
279 
280 /*
281  * Common signature for all lofi compress functions
282  */
283 typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst,
284 	size_t *destlen, int level);
285 
286 /*
287  * Information about each compression function
288  */
289 typedef struct lofi_compress_info {
290 	lofi_compress_func_t	*l_decompress;
291 	lofi_compress_func_t	*l_compress;
292 	int			l_level;
293 	char			*l_name;	/* algorithm name */
294 } lofi_compress_info_t;
295 
296 enum lofi_compress {
297 	LOFI_COMPRESS_GZIP = 0,
298 	LOFI_COMPRESS_GZIP_6 = 1,
299 	LOFI_COMPRESS_GZIP_9 = 2,
300 	LOFI_COMPRESS_LZMA = 3,
301 	LOFI_COMPRESS_FUNCTIONS
302 };
303 
304 #ifdef	__cplusplus
305 }
306 #endif
307 
308 #endif	/* _SYS_LOFI_H */
309