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