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