xref: /titanic_51/usr/src/uts/common/sys/lofi.h (revision a29e56d91db891741f1af9f6bbd3e3c3cac5f19b)
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  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25  * Copyright 2016 Toomas Soome <tsoome@me.com>
26  */
27 
28 #ifndef	_SYS_LOFI_H
29 #define	_SYS_LOFI_H
30 
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/taskq.h>
34 #include <sys/dkio.h>
35 #include <sys/vnode.h>
36 #include <sys/list.h>
37 #include <sys/crypto/api.h>
38 #include <sys/zone.h>
39 #ifdef _KERNEL
40 #include <sys/cmlb.h>
41 #include <sys/open.h>
42 #endif	/* _KERNEL */
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 /*
49  * /dev names:
50  *	/dev/lofictl	- master control device
51  *	/dev/lofi	- block devices, named by minor number
52  *	/dev/rlofi	- character devices, named by minor number
53  */
54 #define	LOFI_DRIVER_NAME	"lofi"
55 #define	LOFI_CTL_NODE		"ctl"
56 #define	LOFI_CTL_NAME		LOFI_DRIVER_NAME LOFI_CTL_NODE
57 #define	LOFI_BLOCK_NODE		"disk"
58 #define	LOFI_CHAR_NODE		LOFI_BLOCK_NODE ",raw"
59 #define	LOFI_BLOCK_NAME		LOFI_DRIVER_NAME
60 #define	LOFI_CHAR_NAME		"r" LOFI_DRIVER_NAME
61 
62 #define	SEGHDR		1
63 #define	COMPRESSED	1
64 #define	UNCOMPRESSED	0
65 #define	MAXALGLEN	36
66 
67 #define	LOFI_CMLB_SHIFT		CMLBUNIT_FORCE_P0_SHIFT
68 #define	LOFI_PART_MASK		((1 << LOFI_CMLB_SHIFT) - 1)
69 #define	LOFI_PART_MAX		(1 << LOFI_CMLB_SHIFT)
70 #define	LOFI_PART(x)		((x) & LOFI_PART_MASK)
71 
72 /*
73  * The cmlb is using its own range of minor numbers for partitions, for
74  * unlabeled lofi devices, we need to use another range.
75  */
76 /* unlabeled lofi device id to minor number. */
77 #define	LOFI_ID2MINOR(x)	((x) << LOFI_CMLB_SHIFT)
78 /* lofi id from minor number. */
79 #define	LOFI_MINOR2ID(x)	((x) >> LOFI_CMLB_SHIFT)
80 
81 /*
82  *
83  * Use is:
84  *	ld = open("/dev/lofictl", O_RDWR | O_EXCL);
85  *
86  * lofi must be opened exclusively. Access is controlled by permissions on
87  * the device, which is 644 by default. Write-access is required for ioctls
88  * that change state, but only read-access is required for the ioctls that
89  * return information. Basically, only root can add and remove files, but
90  * non-root can look at the current lists.
91  *
92  * ioctl usage:
93  *
94  * kernel ioctls
95  *
96  *	strcpy(li.li_filename, "somefilename");
97  *	ioctl(ld, LOFI_MAP_FILE, &li);
98  *	newminor = li.li_minor;
99  *
100  *	strcpy(li.li_filename, "somefilename");
101  *	ioctl(ld, LOFI_UNMAP_FILE, &li);
102  *
103  *	strcpy(li.li_filename, "somefilename");
104  *	li.li_minor = minor_number;
105  *	ioctl(ld, LOFI_MAP_FILE_MINOR, &li);
106  *
107  *	li.li_minor = minor_number;
108  *	ioctl(ld, LOFI_UNMAP_FILE_MINOR, &li);
109  *
110  *	li.li_minor = minor_number;
111  *	ioctl(ld, LOFI_GET_FILENAME, &li);
112  *	filename = li.li_filename;
113  *	encrypted = li.li_crypto_enabled;
114  *
115  *	strcpy(li.li_filename, "somefilename");
116  *	ioctl(ld, LOFI_GET_MINOR, &li);
117  *	minor = li.li_minor;
118  *
119  *	li.li_minor = 0;
120  *	ioctl(ld, LOFI_GET_MAXMINOR, &li);
121  *	maxminor = li.li_minor;
122  *
123  *	strcpy(li.li_filename, "somefilename");
124  *	li.li_minor = 0;
125  *	ioctl(ld, LOFI_CHECK_COMPRESSED, &li);
126  *
127  * If the 'li_force' flag is set for any of the LOFI_UNMAP_* commands, then if
128  * the device is busy, the underlying vnode will be closed, and any subsequent
129  * operations will fail.  It will behave as if the device had been forcibly
130  * removed, so the DKIOCSTATE ioctl will return DKIO_DEV_GONE.  When the device
131  * is last closed, it will be torn down.
132  *
133  * If the 'li_cleanup' flag is set for any of the LOFI_UNMAP_* commands, then
134  * if the device is busy, it is marked for removal at the next time it is
135  * no longer held open by anybody.  When the device is last closed, it will be
136  * torn down.
137  *
138  * Oh, and last but not least: these ioctls are totally private and only
139  * for use by lofiadm(1M).
140  *
141  */
142 
143 typedef enum	iv_method {
144 	IVM_NONE,	/* no iv needed, iv is null */
145 	IVM_ENC_BLKNO	/* iv is logical block no. encrypted */
146 } iv_method_t;
147 
148 struct lofi_ioctl {
149 	uint32_t	li_id;			/* lofi ID */
150 	boolean_t	li_force;
151 	boolean_t	li_cleanup;
152 	boolean_t	li_readonly;
153 	boolean_t	li_labeled;
154 	char	li_filename[MAXPATHLEN];
155 	char	li_devpath[MAXPATHLEN];
156 
157 	/* the following fields are required for compression support */
158 	char	li_algorithm[MAXALGLEN];
159 
160 	/* the following fields are required for encryption support */
161 	boolean_t	li_crypto_enabled;
162 	crypto_mech_name_t	li_cipher;	/* for data */
163 	uint32_t	li_key_len;		/* for data */
164 	char		li_key[56];	/* for data: max 448-bit Blowfish key */
165 	crypto_mech_name_t	li_iv_cipher;	/* for iv derivation */
166 	uint32_t	li_iv_len;		/* for iv derivation */
167 	iv_method_t	li_iv_type;		/* for iv derivation */
168 };
169 
170 #define	LOFI_IOC_BASE		(('L' << 16) | ('F' << 8))
171 
172 #define	LOFI_MAP_FILE		(LOFI_IOC_BASE | 0x01)
173 #define	LOFI_MAP_FILE_MINOR	(LOFI_IOC_BASE | 0x02)
174 #define	LOFI_UNMAP_FILE		(LOFI_IOC_BASE | 0x03)
175 #define	LOFI_UNMAP_FILE_MINOR	(LOFI_IOC_BASE | 0x04)
176 #define	LOFI_GET_FILENAME	(LOFI_IOC_BASE | 0x05)
177 #define	LOFI_GET_MINOR		(LOFI_IOC_BASE | 0x06)
178 #define	LOFI_GET_MAXMINOR	(LOFI_IOC_BASE | 0x07)
179 #define	LOFI_CHECK_COMPRESSED	(LOFI_IOC_BASE | 0x08)
180 
181 /*
182  * file types that might be usable with lofi, maybe. Only regular
183  * files are documented though.
184  */
185 #define	S_ISLOFIABLE(mode) \
186 	(S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode))
187 
188 #if defined(_KERNEL)
189 
190 
191 /*
192  * Cache decompressed data segments for the compressed lofi images.
193  *
194  * To avoid that we have to decompress data of a compressed
195  * segment multiple times when accessing parts of the segment's
196  * data we cache the uncompressed data, using a simple linked list.
197  */
198 struct lofi_comp_cache {
199 	list_node_t	lc_list;		/* linked list */
200 	uchar_t		*lc_data;		/* decompressed segment data */
201 	uint64_t	lc_index;		/* segment index */
202 };
203 
204 #define	V_ISLOFIABLE(vtype) \
205 	((vtype == VREG) || (vtype == VBLK) || (vtype == VCHR))
206 
207 /*
208  * Pre-allocated memory buffers for the purpose of compression
209  */
210 struct compbuf {
211 	void		*buf;
212 	uint32_t	bufsize;
213 	int		inuse;
214 };
215 
216 /*
217  * Need exactly 6 bytes to identify encrypted lofi image
218  */
219 extern const char lofi_crypto_magic[6];
220 #define	LOFI_CRYPTO_MAGIC	{ 'C', 'F', 'L', 'O', 'F', 'I' }
221 #define	LOFI_CRYPTO_VERSION	((uint16_t)0)
222 #define	LOFI_CRYPTO_DATA_SECTOR	((uint32_t)16)		/* for version 0 */
223 
224 /*
225  * Crypto metadata for encrypted lofi images
226  * The fields here only satisfy initial implementation requirements.
227  */
228 struct crypto_meta {
229 	char		magic[6];		/* LOFI_CRYPTO_MAGIC */
230 	uint16_t	version;		/* version of encrypted lofi */
231 	char		reserved1[96];		/* future use */
232 	uint32_t	data_sector;		/* start of data area */
233 	char		pad[404];		/* end on DEV_BSIZE bdry */
234 	/* second header block is not defined at this time */
235 };
236 
237 struct lofi_state {
238 	vnode_t		*ls_vp;		/* open real vnode */
239 	vnode_t		*ls_stacked_vp;	/* open vnode */
240 	kmutex_t	ls_vp_lock;	/* protects ls_vp */
241 	kcondvar_t	ls_vp_cv;	/* signal changes to ls_vp */
242 	uint32_t	ls_vp_iocount;	/* # pending I/O requests */
243 	boolean_t	ls_vp_closereq;	/* force close requested */
244 	boolean_t	ls_vp_ready;	/* is vp ready for use? */
245 	u_offset_t	ls_vp_size;
246 	uint32_t	ls_open_lyr[LOFI_PART_MAX];	/* open count */
247 	uint64_t	ls_open_reg[OTYPCNT];		/* bitmask */
248 	uint64_t	ls_open_excl;			/* bitmask */
249 	int		ls_openflag;
250 	boolean_t	ls_cleanup;	/* cleanup on close */
251 	boolean_t	ls_readonly;
252 	taskq_t		*ls_taskq;
253 	kstat_t		*ls_kstat;
254 	kmutex_t	ls_kstat_lock;
255 	struct dk_geom	ls_dkg;
256 	zone_ref_t	ls_zone;
257 	list_node_t	ls_list;	/* all lofis */
258 	dev_info_t	*ls_dip;
259 	dev_t		ls_dev;		/* this node's dev_t */
260 
261 	cmlb_handle_t	ls_cmlbhandle;
262 	uint32_t	ls_lbshift;	/* logical block shift */
263 	uint32_t	ls_pbshift;	/* physical block shift */
264 
265 	/* the following fields are required for compression support */
266 	int		ls_comp_algorithm_index; /* idx into compress_table */
267 	char		ls_comp_algorithm[MAXALGLEN];
268 	uint32_t	ls_uncomp_seg_sz; /* sz of uncompressed segment */
269 	uint32_t	ls_comp_index_sz; /* number of index entries */
270 	uint32_t	ls_comp_seg_shift; /* exponent for byte shift */
271 	uint32_t	ls_uncomp_last_seg_sz; /* sz of last uncomp segment */
272 	uint64_t	ls_comp_offbase; /* offset of actual compressed data */
273 	uint64_t	*ls_comp_seg_index; /* array of index entries */
274 	caddr_t		ls_comp_index_data; /* index pages loaded from file */
275 	uint32_t	ls_comp_index_data_sz;
276 	u_offset_t	ls_vp_comp_size; /* actual compressed file size */
277 
278 	/* pre-allocated list of buffers for compressed segment data */
279 	kmutex_t	ls_comp_bufs_lock;
280 	struct compbuf	*ls_comp_bufs;
281 
282 	/* lock and anchor for compressed segment caching */
283 	kmutex_t	ls_comp_cache_lock;	/* protects ls_comp_cache */
284 	list_t		ls_comp_cache;		/* cached decompressed segs */
285 	uint32_t	ls_comp_cache_count;
286 
287 	/* the following fields are required for encryption support */
288 	boolean_t		ls_crypto_enabled;
289 	u_offset_t		ls_crypto_offset;	/* crypto meta size */
290 	struct crypto_meta	ls_crypto;
291 	crypto_mechanism_t	ls_mech;	/* for data encr/decr */
292 	crypto_key_t		ls_key;		/* for data encr/decr */
293 	crypto_mechanism_t	ls_iv_mech;	/* for iv derivation */
294 	size_t			ls_iv_len;	/* for iv derivation */
295 	iv_method_t		ls_iv_type;	/* for iv derivation */
296 	kmutex_t		ls_crypto_lock;
297 	crypto_ctx_template_t	ls_ctx_tmpl;
298 };
299 
300 #endif	/* _KERNEL */
301 
302 /*
303  * Common signature for all lofi compress functions
304  */
305 typedef int lofi_compress_func_t(void *src, size_t srclen, void *dst,
306 	size_t *destlen, int level);
307 
308 /*
309  * Information about each compression function
310  */
311 typedef struct lofi_compress_info {
312 	lofi_compress_func_t	*l_decompress;
313 	lofi_compress_func_t	*l_compress;
314 	int			l_level;
315 	char			*l_name;	/* algorithm name */
316 } lofi_compress_info_t;
317 
318 enum lofi_compress {
319 	LOFI_COMPRESS_GZIP = 0,
320 	LOFI_COMPRESS_GZIP_6 = 1,
321 	LOFI_COMPRESS_GZIP_9 = 2,
322 	LOFI_COMPRESS_LZMA = 3,
323 	LOFI_COMPRESS_FUNCTIONS
324 };
325 
326 #ifdef	__cplusplus
327 }
328 #endif
329 
330 #endif	/* _SYS_LOFI_H */
331