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