xref: /dragonfly/sys/dev/disk/dm/dm.h (revision dcd37f7d)
1 /*        $NetBSD: dm.h,v 1.17 2009/12/29 23:37:48 haad Exp $      */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Hamsik.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _DM_DEV_H_
33 #define _DM_DEV_H_
34 
35 
36 #ifdef _KERNEL
37 
38 #include <sys/errno.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 
42 #include <cpu/inttypes.h>
43 #include <cpu/atomic.h>
44 #include <sys/condvar.h>
45 #include <sys/lock.h>
46 #include <sys/queue.h>
47 
48 #include <sys/device.h>
49 #include <sys/disklabel.h>
50 
51 #include <libprop/proplib.h>
52 
53 #define DM_MAX_TYPE_NAME 16
54 #define DM_NAME_LEN 128
55 #define DM_UUID_LEN 129
56 
57 #define DM_VERSION_MAJOR	4
58 #define DM_VERSION_MINOR	16
59 
60 #define DM_VERSION_PATCHLEVEL	0
61 
62 /*** Internal device-mapper structures ***/
63 
64 /*
65  * A table entry describes a physical range of the logical volume.
66  */
67 #define MAX_TARGET_STRING_LEN 32
68 
69 /*
70  * A device mapper table is a list of physical ranges plus the mapping target
71  * applied to them.
72  */
73 struct buf;
74 struct bio;
75 
76 typedef struct dm_table_entry {
77 	struct dm_dev *dm_dev;		/* backlink */
78 	uint64_t start;
79 	uint64_t length;
80 
81 	struct dm_target *target;      /* Link to table target. */
82 	void *target_config;           /* Target specific data. */
83 	SLIST_ENTRY(dm_table_entry) next;
84 } dm_table_entry_t;
85 
86 SLIST_HEAD(dm_table, dm_table_entry);
87 
88 typedef struct dm_table dm_table_t;
89 
90 typedef struct dm_table_head {
91 	/* Current active table is selected with this. */
92 	int cur_active_table;
93 	struct dm_table tables[2];
94 
95 	struct lock   table_mtx;
96 	struct cv table_cv; /*IO waiting cv */
97 
98 	uint32_t io_cnt;
99 } dm_table_head_t;
100 
101 #define MAX_DEV_NAME 32
102 
103 /*
104  * This structure is used to store opened vnodes for disk with name.
105  * I need this because devices can be opened only once, but I can
106  * have more then one device on one partition.
107  */
108 
109 typedef struct dm_pdev {
110 	char name[MAX_DEV_NAME];
111 
112 	struct vnode *pdev_vnode;
113 	int ref_cnt; /* reference counter for users ofthis pdev */
114 
115 	SLIST_ENTRY(dm_pdev) next_pdev;
116 } dm_pdev_t;
117 
118 /*
119  * This structure is called for every device-mapper device.
120  * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
121  */
122 TAILQ_HEAD(dm_dev_head, dm_dev);
123 
124 typedef struct dm_dev {
125 	char name[DM_NAME_LEN];
126 	char uuid[DM_UUID_LEN];
127 
128 	cdev_t devt; /* pointer to autoconf device_t structure */
129 	uint64_t minor;
130 	uint32_t flags; /* store communication protocol flags */
131 
132 	struct lock dev_mtx; /* mutex for generall device lock */
133 	struct cv dev_cv; /* cv for between ioctl synchronisation */
134 
135 	uint32_t event_nr;
136 	uint32_t ref_cnt;
137 
138 	uint32_t dev_type;
139 
140 	dm_table_head_t table_head;
141 
142 	struct dm_dev_head upcalls;
143 
144 	struct disk *diskp;
145 	struct lock diskp_mtx;
146 
147 	TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
148 
149 	TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
150 } dm_dev_t;
151 
152 /* Device types used for upcalls */
153 #define DM_ZERO_DEV            (1 << 0)
154 #define DM_ERROR_DEV           (1 << 1)
155 #define DM_LINEAR_DEV          (1 << 2)
156 #define DM_MIRROR_DEV          (1 << 3)
157 #define DM_STRIPE_DEV          (1 << 4)
158 #define DM_SNAPSHOT_DEV        (1 << 5)
159 #define DM_SNAPSHOT_ORIG_DEV   (1 << 6)
160 #define DM_SPARE_DEV           (1 << 7)
161 /* Set this device type only during dev remove ioctl. */
162 #define DM_DELETING_DEV        (1 << 8)
163 #define DM_CRYPTO_DEV		(1 << 9)
164 
165 /* for zero, error : dm_target->target_config == NULL */
166 
167 /*
168  * Target config is initiated with target_init function.
169  */
170 
171 /* for linear : */
172 typedef struct target_linear_config {
173 	dm_pdev_t *pdev;
174 	uint64_t offset;
175 } dm_target_linear_config_t;
176 
177 /* for stripe : */
178 typedef struct target_stripe_config {
179 #define MAX_STRIPES 2
180 	struct target_linear_config stripe_devs[MAX_STRIPES];
181 	uint8_t stripe_num;
182 	uint64_t stripe_chunksize;
183 	size_t params_len;
184 } dm_target_stripe_config_t;
185 
186 /* for mirror : */
187 typedef struct target_mirror_config {
188 #define MAX_MIRROR_COPIES 4
189 	dm_pdev_t *orig;
190 	dm_pdev_t *copies[MAX_MIRROR_COPIES];
191 
192 	/* copied blocks bitmaps administration etc*/
193 	dm_pdev_t *log_pdev;	/* for administration */
194 	uint64_t log_regionsize;	/* blocksize of mirror */
195 
196 	/* list of parts that still need copied etc.; run length encoded? */
197 } dm_target_mirror_config_t;
198 
199 
200 /* for snapshot : */
201 typedef struct target_snapshot_config {
202 	dm_pdev_t *tsc_snap_dev;
203 	/* cow dev is set only for persistent snapshot devices */
204 	dm_pdev_t *tsc_cow_dev;
205 
206 	uint64_t tsc_chunk_size;
207 	uint32_t tsc_persistent_dev;
208 } dm_target_snapshot_config_t;
209 
210 /* for snapshot-origin devices */
211 typedef struct target_snapshot_origin_config {
212 	dm_pdev_t *tsoc_real_dev;
213 	/* list of snapshots ? */
214 } dm_target_snapshot_origin_config_t;
215 
216 /* constant dm_target structures for error, zero, linear, stripes etc. */
217 typedef struct dm_target {
218 	char name[DM_MAX_TYPE_NAME];
219 	/* Initialize target_config area */
220 	int (*init)(dm_dev_t *, void **, char *);
221 
222 	/* Destroy target_config area */
223 	int (*destroy)(dm_table_entry_t *);
224 
225 	int (*deps) (dm_table_entry_t *, prop_array_t);
226 	/*
227 	 * Status routine is called to get params string, which is target
228 	 * specific. When dm_table_status_ioctl is called with flag
229 	 * DM_STATUS_TABLE_FLAG I have to sent params string back.
230 	 */
231 	char * (*status)(void *);
232 	int (*strategy)(dm_table_entry_t *, struct buf *);
233 	int (*upcall)(dm_table_entry_t *, struct buf *);
234 
235 	uint32_t version[3];
236 	int ref_cnt;
237 
238 	TAILQ_ENTRY(dm_target) dm_target_next;
239 } dm_target_t;
240 
241 /* Interface structures */
242 
243 /*
244  * This structure is used to translate command sent to kernel driver in
245  * <key>command</key>
246  * <value></value>
247  * to function which I can call.
248  */
249 struct cmd_function {
250 	const char *cmd;
251 	int  (*fn)(prop_dictionary_t);
252 };
253 
254 /* device-mapper */
255 void dmsetdiskinfo(struct disk *, dm_table_head_t *);
256 prop_dictionary_t dmgetdiskinfo(struct disk *);
257 void dmgetproperties(struct disk *, dm_table_head_t *);
258 int dm_detach(dm_dev_t *);
259 
260 /* dm_ioctl.c */
261 int dm_dev_create_ioctl(prop_dictionary_t);
262 int dm_dev_list_ioctl(prop_dictionary_t);
263 int dm_dev_remove_ioctl(prop_dictionary_t);
264 int dm_dev_rename_ioctl(prop_dictionary_t);
265 int dm_dev_resume_ioctl(prop_dictionary_t);
266 int dm_dev_status_ioctl(prop_dictionary_t);
267 int dm_dev_suspend_ioctl(prop_dictionary_t);
268 
269 int dm_check_version(prop_dictionary_t);
270 int dm_get_version_ioctl(prop_dictionary_t);
271 int dm_list_versions_ioctl(prop_dictionary_t);
272 
273 int dm_table_clear_ioctl(prop_dictionary_t);
274 int dm_table_deps_ioctl(prop_dictionary_t);
275 int dm_table_load_ioctl(prop_dictionary_t);
276 int dm_table_status_ioctl(prop_dictionary_t);
277 
278 /* dm_target.c */
279 dm_target_t* dm_target_alloc(const char *);
280 dm_target_t* dm_target_autoload(const char *);
281 int dm_target_destroy(void);
282 int dm_target_insert(dm_target_t *);
283 prop_array_t dm_target_prop_list(void);
284 dm_target_t* dm_target_lookup(const char *);
285 int dm_target_rem(char *);
286 void dm_target_unbusy(dm_target_t *);
287 void dm_target_busy(dm_target_t *);
288 
289 /* XXX temporally add */
290 int dm_target_init(void);
291 
292 #define DM_MAX_PARAMS_SIZE 1024
293 
294 /* dm_target_zero.c */
295 int dm_target_zero_init(dm_dev_t *, void**,  char *);
296 char * dm_target_zero_status(void *);
297 int dm_target_zero_strategy(dm_table_entry_t *, struct buf *);
298 int dm_target_zero_destroy(dm_table_entry_t *);
299 int dm_target_zero_deps(dm_table_entry_t *, prop_array_t);
300 int dm_target_zero_upcall(dm_table_entry_t *, struct buf *);
301 
302 /* dm_target_error.c */
303 int dm_target_error_init(dm_dev_t *, void**, char *);
304 char * dm_target_error_status(void *);
305 int dm_target_error_strategy(dm_table_entry_t *, struct buf *);
306 int dm_target_error_deps(dm_table_entry_t *, prop_array_t);
307 int dm_target_error_destroy(dm_table_entry_t *);
308 int dm_target_error_upcall(dm_table_entry_t *, struct buf *);
309 
310 /* dm_target_linear.c */
311 int dm_target_linear_init(dm_dev_t *, void**, char *);
312 char * dm_target_linear_status(void *);
313 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
314 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
315 int dm_target_linear_destroy(dm_table_entry_t *);
316 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
317 
318 /* dm_target_crypt.c */
319 int dm_target_crypt_init(dm_dev_t *, void**, char *);
320 char * dm_target_crypt_status(void *);
321 int dm_target_crypt_strategy(dm_table_entry_t *, struct buf *);
322 int dm_target_crypt_deps(dm_table_entry_t *, prop_array_t);
323 int dm_target_crypt_destroy(dm_table_entry_t *);
324 int dm_target_crypt_upcall(dm_table_entry_t *, struct buf *);
325 
326 /* Generic function used to convert char to string */
327 uint64_t atoi(const char *);
328 
329 /* dm_target_mirror.c */
330 int dm_target_mirror_init(dm_dev_t *, void**, char *);
331 char * dm_target_mirror_status(void *);
332 int dm_target_mirror_strategy(dm_table_entry_t *, struct buf *);
333 int dm_target_mirror_deps(dm_table_entry_t *, prop_array_t);
334 int dm_target_mirror_destroy(dm_table_entry_t *);
335 int dm_target_mirror_upcall(dm_table_entry_t *, struct buf *);
336 
337 /* dm_target_stripe.c */
338 int dm_target_stripe_init(dm_dev_t *, void**, char *);
339 char * dm_target_stripe_status(void *);
340 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
341 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
342 int dm_target_stripe_destroy(dm_table_entry_t *);
343 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
344 
345 /* dm_target_snapshot.c */
346 int dm_target_snapshot_init(dm_dev_t *, void**, char *);
347 char * dm_target_snapshot_status(void *);
348 int dm_target_snapshot_strategy(dm_table_entry_t *, struct buf *);
349 int dm_target_snapshot_deps(dm_table_entry_t *, prop_array_t);
350 int dm_target_snapshot_destroy(dm_table_entry_t *);
351 int dm_target_snapshot_upcall(dm_table_entry_t *, struct buf *);
352 
353 /* dm snapshot origin driver */
354 int dm_target_snapshot_orig_init(dm_dev_t *, void**, char *);
355 char * dm_target_snapshot_orig_status(void *);
356 int dm_target_snapshot_orig_strategy(dm_table_entry_t *, struct buf *);
357 int dm_target_snapshot_orig_deps(dm_table_entry_t *, prop_array_t);
358 int dm_target_snapshot_orig_destroy(dm_table_entry_t *);
359 int dm_target_snapshot_orig_upcall(dm_table_entry_t *, struct buf *);
360 
361 /* dm_table.c  */
362 #define DM_TABLE_ACTIVE 0
363 #define DM_TABLE_INACTIVE 1
364 
365 int dm_table_destroy(dm_table_head_t *, uint8_t);
366 uint64_t dm_table_size(dm_table_head_t *);
367 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
368 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
369 void dm_table_release(dm_table_head_t *, uint8_t s);
370 void dm_table_switch_tables(dm_table_head_t *);
371 void dm_table_head_init(dm_table_head_t *);
372 void dm_table_head_destroy(dm_table_head_t *);
373 
374 /* dm_dev.c */
375 dm_dev_t* dm_dev_alloc(void);
376 void dm_dev_busy(dm_dev_t *);
377 int dm_dev_destroy(void);
378 void disable_dev(dm_dev_t *);
379 int dm_dev_free(dm_dev_t *);
380 int dm_dev_init(void);
381 int dm_dev_insert(dm_dev_t *);
382 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
383 prop_array_t dm_dev_prop_list(void);
384 dm_dev_t* dm_dev_rem(const char *, const char *, int);
385 /*int dm_dev_test_minor(int);*/
386 void dm_dev_unbusy(dm_dev_t *);
387 
388 /* dm_pdev.c */
389 int dm_pdev_decr(dm_pdev_t *);
390 int dm_pdev_destroy(void);
391 int dm_pdev_init(void);
392 dm_pdev_t* dm_pdev_insert(const char *);
393 
394 extern int dm_debug_level;
395 
396 #define aprint_debug(format, ...)	\
397     do { if (dm_debug_level) kprintf(format, ## __VA_ARGS__); } while(0)
398 #define aprint_normal	kprintf
399 
400 #endif /*_KERNEL*/
401 
402 #endif /*_DM_DEV_H_*/
403