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