xref: /netbsd-src/sys/dev/dm/dm.h (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*        $NetBSD: dm.h,v 1.12 2009/03/25 23:35:54 dyoung 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 
40 #include <sys/atomic.h>
41 #include <sys/condvar.h>
42 #include <sys/mutex.h>
43 #include <sys/rwlock.h>
44 #include <sys/queue.h>
45 
46 #include <sys/disklabel.h>
47 
48 #include <prop/proplib.h>
49 
50 #define DM_MAX_TYPE_NAME 16
51 #define DM_NAME_LEN 128
52 #define DM_UUID_LEN 129
53 
54 #define DM_VERSION_MAJOR	4
55 #define DM_VERSION_MINOR	13
56 #define DM_VERSION_PATCHLEVEL	0
57 
58 /*** Internal device-mapper structures ***/
59 
60 /*
61  * A table entry describes a physical range of the logical volume.
62  */
63 #define MAX_TARGET_STRING_LEN 32
64 
65 /*
66  * A device mapper table is a list of physical ranges plus the mapping target
67  * applied to them.
68  */
69 
70 typedef struct dm_table_entry {
71 	struct dm_dev *dm_dev;		/* backlink */
72 	uint64_t start;
73 	uint64_t length;
74 
75 	struct dm_target *target;      /* Link to table target. */
76 	void *target_config;           /* Target specific data. */
77 	SLIST_ENTRY(dm_table_entry) next;
78 } dm_table_entry_t;
79 
80 SLIST_HEAD(dm_table, dm_table_entry);
81 
82 typedef struct dm_table dm_table_t;
83 
84 typedef struct dm_table_head {
85 	/* Current active table is selected with this. */
86 	int cur_active_table;
87 	struct dm_table tables[2];
88 
89 	kmutex_t   table_mtx;
90 	kcondvar_t table_cv; /*IO waiting cv */
91 
92 	uint32_t io_cnt;
93 } dm_table_head_t;
94 
95 #define MAX_DEV_NAME 32
96 
97 /*
98  * This structure is used to store opened vnodes for disk with name.
99  * I need this because devices can be opened only once, but I can
100  * have more then one device on one partition.
101  */
102 
103 typedef struct dm_pdev {
104 	char name[MAX_DEV_NAME];
105 
106 	struct vnode *pdev_vnode;
107 	int ref_cnt; /* reference counter for users ofthis pdev */
108 
109 	SLIST_ENTRY(dm_pdev) next_pdev;
110 } dm_pdev_t;
111 
112 /*
113  * This structure is called for every device-mapper device.
114  * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
115  */
116 TAILQ_HEAD(dm_dev_head, dm_dev) dm_devs;
117 
118 typedef struct dm_dev {
119 	char name[DM_NAME_LEN];
120 	char uuid[DM_UUID_LEN];
121 
122 	uint64_t minor;
123 	uint32_t flags; /* store communication protocol flags */
124 
125 	kmutex_t dev_mtx; /* mutex for generall device lock */
126 	kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
127 
128 	uint32_t event_nr;
129 	uint32_t ref_cnt;
130 
131 	uint32_t dev_type;
132 
133 	dm_table_head_t table_head;
134 
135 	struct dm_dev_head upcalls;
136 
137 	struct disk *diskp;
138 
139 	TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
140 
141 	TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
142 } dm_dev_t;
143 
144 /* Device types used for upcalls */
145 #define DM_ZERO_DEV            (1 << 0)
146 #define DM_ERROR_DEV           (1 << 1)
147 #define DM_LINEAR_DEV          (1 << 2)
148 #define DM_MIRROR_DEV          (1 << 3)
149 #define DM_STRIPE_DEV          (1 << 4)
150 #define DM_SNAPSHOT_DEV        (1 << 5)
151 #define DM_SNAPSHOT_ORIG_DEV   (1 << 6)
152 #define DM_SPARE_DEV           (1 << 7)
153 /* Set this device type only during dev remove ioctl. */
154 #define DM_DELETING_DEV        (1 << 8)
155 
156 
157 /* for zero, error : dm_target->target_config == NULL */
158 
159 /*
160  * Target config is initiated with target_init function.
161  */
162 
163 /* for linear : */
164 typedef struct target_linear_config {
165 	dm_pdev_t *pdev;
166 	uint64_t offset;
167 } dm_target_linear_config_t;
168 
169 /* for stripe : */
170 typedef struct target_stripe_config {
171 #define MAX_STRIPES 2
172 	struct target_linear_config stripe_devs[MAX_STRIPES];
173 	uint8_t stripe_num;
174 	uint64_t stripe_chunksize;
175 	size_t params_len;
176 } dm_target_stripe_config_t;
177 
178 /* for mirror : */
179 typedef struct target_mirror_config {
180 #define MAX_MIRROR_COPIES 4
181 	dm_pdev_t *orig;
182 	dm_pdev_t *copies[MAX_MIRROR_COPIES];
183 
184 	/* copied blocks bitmaps administration etc*/
185 	dm_pdev_t *log_pdev;	/* for administration */
186 	uint64_t log_regionsize;	/* blocksize of mirror */
187 
188 	/* list of parts that still need copied etc.; run length encoded? */
189 } dm_target_mirror_config_t;
190 
191 
192 /* for snapshot : */
193 typedef struct target_snapshot_config {
194 	dm_pdev_t *tsc_snap_dev;
195 	/* cow dev is set only for persistent snapshot devices */
196 	dm_pdev_t *tsc_cow_dev;
197 
198 	uint64_t tsc_chunk_size;
199 	uint32_t tsc_persistent_dev;
200 } dm_target_snapshot_config_t;
201 
202 /* for snapshot-origin devices */
203 typedef struct target_snapshot_origin_config {
204 	dm_pdev_t *tsoc_real_dev;
205 	/* list of snapshots ? */
206 } dm_target_snapshot_origin_config_t;
207 
208 /* constant dm_target structures for error, zero, linear, stripes etc. */
209 typedef struct dm_target {
210 	char name[DM_MAX_TYPE_NAME];
211 	/* Initialize target_config area */
212 	int (*init)(dm_dev_t *, void **, char *);
213 
214 	/* Destroy target_config area */
215 	int (*destroy)(dm_table_entry_t *);
216 
217 	int (*deps) (dm_table_entry_t *, prop_array_t);
218 	/*
219 	 * Status routine is called to get params string, which is target
220 	 * specific. When dm_table_status_ioctl is called with flag
221 	 * DM_STATUS_TABLE_FLAG I have to sent params string back.
222 	 */
223 	char * (*status)(void *);
224 	int (*strategy)(dm_table_entry_t *, struct buf *);
225 	int (*upcall)(dm_table_entry_t *, struct buf *);
226 
227 	uint32_t version[3];
228 	int ref_cnt;
229 
230 	TAILQ_ENTRY(dm_target) dm_target_next;
231 } dm_target_t;
232 
233 /* Interface structures */
234 
235 /*
236  * This structure is used to translate command sent to kernel driver in
237  * <key>command</key>
238  * <value></value>
239  * to function which I can call.
240  */
241 struct cmd_function {
242 	const char *cmd;
243 	int  (*fn)(prop_dictionary_t);
244 };
245 
246 /* device-mapper */
247 void dmgetdisklabel(struct disklabel *, dm_table_head_t *);
248 
249 /* dm_ioctl.c */
250 int dm_dev_create_ioctl(prop_dictionary_t);
251 int dm_dev_list_ioctl(prop_dictionary_t);
252 int dm_dev_remove_ioctl(prop_dictionary_t);
253 int dm_dev_rename_ioctl(prop_dictionary_t);
254 int dm_dev_resume_ioctl(prop_dictionary_t);
255 int dm_dev_status_ioctl(prop_dictionary_t);
256 int dm_dev_suspend_ioctl(prop_dictionary_t);
257 
258 int dm_check_version(prop_dictionary_t);
259 int dm_get_version_ioctl(prop_dictionary_t);
260 int dm_list_versions_ioctl(prop_dictionary_t);
261 
262 int dm_table_clear_ioctl(prop_dictionary_t);
263 int dm_table_deps_ioctl(prop_dictionary_t);
264 int dm_table_load_ioctl(prop_dictionary_t);
265 int dm_table_status_ioctl(prop_dictionary_t);
266 
267 /* dm_target.c */
268 dm_target_t* dm_target_alloc(const char *);
269 dm_target_t* dm_target_autoload(const char *);
270 int dm_target_destroy(void);
271 int dm_target_insert(dm_target_t *);
272 prop_array_t dm_target_prop_list(void);
273 dm_target_t* dm_target_lookup(const char *);
274 int dm_target_rem(char *);
275 void dm_target_unbusy(dm_target_t *);
276 void dm_target_busy(dm_target_t *);
277 
278 /* XXX temporally add */
279 int dm_target_init(void);
280 
281 /* dm_target_zero.c */
282 int dm_target_zero_init(dm_dev_t *, void**,  char *);
283 char * dm_target_zero_status(void *);
284 int dm_target_zero_strategy(dm_table_entry_t *, struct buf *);
285 int dm_target_zero_destroy(dm_table_entry_t *);
286 int dm_target_zero_deps(dm_table_entry_t *, prop_array_t);
287 int dm_target_zero_upcall(dm_table_entry_t *, struct buf *);
288 
289 /* dm_target_error.c */
290 int dm_target_error_init(dm_dev_t *, void**, char *);
291 char * dm_target_error_status(void *);
292 int dm_target_error_strategy(dm_table_entry_t *, struct buf *);
293 int dm_target_error_deps(dm_table_entry_t *, prop_array_t);
294 int dm_target_error_destroy(dm_table_entry_t *);
295 int dm_target_error_upcall(dm_table_entry_t *, struct buf *);
296 
297 /* dm_target_linear.c */
298 int dm_target_linear_init(dm_dev_t *, void**, char *);
299 char * dm_target_linear_status(void *);
300 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
301 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
302 int dm_target_linear_destroy(dm_table_entry_t *);
303 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
304 
305 /* Generic function used to convert char to string */
306 uint64_t atoi(const char *);
307 
308 /* dm_target_mirror.c */
309 int dm_target_mirror_init(dm_dev_t *, void**, char *);
310 char * dm_target_mirror_status(void *);
311 int dm_target_mirror_strategy(dm_table_entry_t *, struct buf *);
312 int dm_target_mirror_deps(dm_table_entry_t *, prop_array_t);
313 int dm_target_mirror_destroy(dm_table_entry_t *);
314 int dm_target_mirror_upcall(dm_table_entry_t *, struct buf *);
315 
316 /* dm_target_stripe.c */
317 int dm_target_stripe_init(dm_dev_t *, void**, char *);
318 char * dm_target_stripe_status(void *);
319 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
320 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
321 int dm_target_stripe_destroy(dm_table_entry_t *);
322 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
323 
324 /* dm_target_snapshot.c */
325 int dm_target_snapshot_init(dm_dev_t *, void**, char *);
326 char * dm_target_snapshot_status(void *);
327 int dm_target_snapshot_strategy(dm_table_entry_t *, struct buf *);
328 int dm_target_snapshot_deps(dm_table_entry_t *, prop_array_t);
329 int dm_target_snapshot_destroy(dm_table_entry_t *);
330 int dm_target_snapshot_upcall(dm_table_entry_t *, struct buf *);
331 
332 /* dm snapshot origin driver */
333 int dm_target_snapshot_orig_init(dm_dev_t *, void**, char *);
334 char * dm_target_snapshot_orig_status(void *);
335 int dm_target_snapshot_orig_strategy(dm_table_entry_t *, struct buf *);
336 int dm_target_snapshot_orig_deps(dm_table_entry_t *, prop_array_t);
337 int dm_target_snapshot_orig_destroy(dm_table_entry_t *);
338 int dm_target_snapshot_orig_upcall(dm_table_entry_t *, struct buf *);
339 
340 /* dm_table.c  */
341 #define DM_TABLE_ACTIVE 0
342 #define DM_TABLE_INACTIVE 1
343 
344 int dm_table_destroy(dm_table_head_t *, uint8_t);
345 uint64_t dm_table_size(dm_table_head_t *);
346 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
347 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
348 void dm_table_release(dm_table_head_t *, uint8_t s);
349 void dm_table_switch_tables(dm_table_head_t *);
350 void dm_table_head_init(dm_table_head_t *);
351 void dm_table_head_destroy(dm_table_head_t *);
352 
353 /* dm_dev.c */
354 dm_dev_t* dm_dev_alloc(void);
355 void dm_dev_busy(dm_dev_t *);
356 int dm_dev_destroy(void);
357 int dm_dev_free(dm_dev_t *);
358 int dm_dev_init(void);
359 int dm_dev_insert(dm_dev_t *);
360 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
361 prop_array_t dm_dev_prop_list(void);
362 dm_dev_t* dm_dev_rem(const char *, const char *, int);
363 /*int dm_dev_test_minor(int);*/
364 void dm_dev_unbusy(dm_dev_t *);
365 
366 /* dm_pdev.c */
367 int dm_pdev_decr(dm_pdev_t *);
368 int dm_pdev_destroy(void);
369 int dm_pdev_init(void);
370 dm_pdev_t* dm_pdev_insert(const char *);
371 
372 #endif /*_KERNEL*/
373 
374 #endif /*_DM_DEV_H_*/
375