1 /* $NetBSD: dm.h,v 1.17 2009/12/29 23:37:48 haad Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
38 #include <sys/errno.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <cpu/inttypes.h>
43 #include <cpu/atomic.h>
44 #include <sys/condvar.h>
46 #include <sys/queue.h>
48 #include <sys/device.h>
49 #include <sys/diskslice.h>
50 #include <sys/disklabel.h>
52 #include <libprop/proplib.h>
54 #define DM_MAX_TYPE_NAME 16
55 #define DM_NAME_LEN 128
56 #define DM_UUID_LEN 129
58 #define DM_VERSION_MAJOR 4
59 #define DM_VERSION_MINOR 16
61 #define DM_VERSION_PATCHLEVEL 0
63 /*** Internal device-mapper structures ***/
66 * A table entry describes a physical range of the logical volume.
68 #define MAX_TARGET_STRING_LEN 32
71 * A device mapper table is a list of physical ranges plus the mapping target
77 typedef struct dm_table_entry {
78 struct dm_dev *dm_dev; /* backlink */
82 struct dm_target *target; /* Link to table target. */
83 void *target_config; /* Target specific data. */
84 SLIST_ENTRY(dm_table_entry) next;
87 SLIST_HEAD(dm_table, dm_table_entry);
89 typedef struct dm_table dm_table_t;
91 typedef struct dm_table_head {
92 /* Current active table is selected with this. */
94 struct dm_table tables[2];
96 struct lock table_mtx;
97 struct cv table_cv; /*IO waiting cv */
102 #define MAX_DEV_NAME 32
105 * This structure is used to store opened vnodes for disk with name.
106 * I need this because devices can be opened only once, but I can
107 * have more then one device on one partition.
110 typedef struct dm_pdev {
111 char name[MAX_DEV_NAME];
112 struct partinfo pdev_pinfo; /* partinfo of the underlying device */
114 struct vnode *pdev_vnode;
115 int ref_cnt; /* reference counter for users ofthis pdev */
117 SLIST_ENTRY(dm_pdev) next_pdev;
121 * This structure is called for every device-mapper device.
122 * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
124 TAILQ_HEAD(dm_dev_head, dm_dev);
126 typedef struct dm_dev {
127 char name[DM_NAME_LEN];
128 char uuid[DM_UUID_LEN];
130 cdev_t devt; /* pointer to autoconf device_t structure */
132 uint32_t flags; /* store communication protocol flags */
134 struct lock dev_mtx; /* mutex for generall device lock */
135 struct cv dev_cv; /* cv for between ioctl synchronisation */
142 dm_table_head_t table_head;
144 struct dm_dev_head upcalls;
147 struct lock diskp_mtx;
149 TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
151 TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
154 /* Device types used for upcalls */
155 #define DM_ZERO_DEV (1 << 0)
156 #define DM_ERROR_DEV (1 << 1)
157 #define DM_LINEAR_DEV (1 << 2)
158 #define DM_MIRROR_DEV (1 << 3)
159 #define DM_STRIPE_DEV (1 << 4)
160 #define DM_SNAPSHOT_DEV (1 << 5)
161 #define DM_SNAPSHOT_ORIG_DEV (1 << 6)
162 #define DM_SPARE_DEV (1 << 7)
163 /* Set this device type only during dev remove ioctl. */
164 #define DM_DELETING_DEV (1 << 8)
165 #define DM_CRYPTO_DEV (1 << 9)
167 /* for zero, error : dm_target->target_config == NULL */
170 * Target config is initiated with target_init function.
174 typedef struct target_linear_config {
177 } dm_target_linear_config_t;
181 #define MAX_STRIPES 32
183 typedef struct target_stripe_config {
184 struct target_linear_config stripe_devs[MAX_STRIPES];
186 uint64_t stripe_chunksize;
187 } dm_target_stripe_config_t;
190 typedef struct target_mirror_config {
191 #define MAX_MIRROR_COPIES 4
193 dm_pdev_t *copies[MAX_MIRROR_COPIES];
195 /* copied blocks bitmaps administration etc*/
196 dm_pdev_t *log_pdev; /* for administration */
197 uint64_t log_regionsize; /* blocksize of mirror */
199 /* list of parts that still need copied etc.; run length encoded? */
200 } dm_target_mirror_config_t;
204 typedef struct target_snapshot_config {
205 dm_pdev_t *tsc_snap_dev;
206 /* cow dev is set only for persistent snapshot devices */
207 dm_pdev_t *tsc_cow_dev;
209 uint64_t tsc_chunk_size;
210 uint32_t tsc_persistent_dev;
211 } dm_target_snapshot_config_t;
213 /* for snapshot-origin devices */
214 typedef struct target_snapshot_origin_config {
215 dm_pdev_t *tsoc_real_dev;
216 /* list of snapshots ? */
217 } dm_target_snapshot_origin_config_t;
219 /* constant dm_target structures for error, zero, linear, stripes etc. */
220 typedef struct dm_target {
221 char name[DM_MAX_TYPE_NAME];
222 /* Initialize target_config area */
223 int (*init)(dm_dev_t *, void **, char *);
225 /* Destroy target_config area */
226 int (*destroy)(dm_table_entry_t *);
228 int (*deps) (dm_table_entry_t *, prop_array_t);
230 * Status routine is called to get params string, which is target
231 * specific. When dm_table_status_ioctl is called with flag
232 * DM_STATUS_TABLE_FLAG I have to sent params string back.
234 char * (*status)(void *);
235 int (*strategy)(dm_table_entry_t *, struct buf *);
236 int (*upcall)(dm_table_entry_t *, struct buf *);
237 int (*dump)(dm_table_entry_t *, void *data, size_t length, off_t offset);
242 TAILQ_ENTRY(dm_target) dm_target_next;
245 /* Interface structures */
248 * This structure is used to translate command sent to kernel driver in
251 * to function which I can call.
253 struct cmd_function {
255 int (*fn)(prop_dictionary_t);
259 void dmsetdiskinfo(struct disk *, dm_table_head_t *);
260 prop_dictionary_t dmgetdiskinfo(struct disk *);
261 void dmgetproperties(struct disk *, dm_table_head_t *);
262 int dm_detach(dm_dev_t *);
265 int dm_dev_create_ioctl(prop_dictionary_t);
266 int dm_dev_list_ioctl(prop_dictionary_t);
267 int dm_dev_remove_ioctl(prop_dictionary_t);
268 int dm_dev_rename_ioctl(prop_dictionary_t);
269 int dm_dev_resume_ioctl(prop_dictionary_t);
270 int dm_dev_status_ioctl(prop_dictionary_t);
271 int dm_dev_suspend_ioctl(prop_dictionary_t);
273 int dm_check_version(prop_dictionary_t);
274 int dm_get_version_ioctl(prop_dictionary_t);
275 int dm_list_versions_ioctl(prop_dictionary_t);
277 int dm_table_clear_ioctl(prop_dictionary_t);
278 int dm_table_deps_ioctl(prop_dictionary_t);
279 int dm_table_load_ioctl(prop_dictionary_t);
280 int dm_table_status_ioctl(prop_dictionary_t);
283 dm_target_t* dm_target_alloc(const char *);
284 dm_target_t* dm_target_autoload(const char *);
285 int dm_target_destroy(void);
286 int dm_target_insert(dm_target_t *);
287 prop_array_t dm_target_prop_list(void);
288 dm_target_t* dm_target_lookup(const char *);
289 int dm_target_rem(char *);
290 void dm_target_unbusy(dm_target_t *);
291 void dm_target_busy(dm_target_t *);
293 /* XXX temporally add */
294 int dm_target_init(void);
296 #define DM_MAX_PARAMS_SIZE 1024
298 /* dm_target_zero.c */
299 int dm_target_zero_init(dm_dev_t *, void**, char *);
300 char * dm_target_zero_status(void *);
301 int dm_target_zero_strategy(dm_table_entry_t *, struct buf *);
302 int dm_target_zero_destroy(dm_table_entry_t *);
303 int dm_target_zero_deps(dm_table_entry_t *, prop_array_t);
304 int dm_target_zero_upcall(dm_table_entry_t *, struct buf *);
306 /* dm_target_error.c */
307 int dm_target_error_init(dm_dev_t *, void**, char *);
308 char * dm_target_error_status(void *);
309 int dm_target_error_strategy(dm_table_entry_t *, struct buf *);
310 int dm_target_error_deps(dm_table_entry_t *, prop_array_t);
311 int dm_target_error_destroy(dm_table_entry_t *);
312 int dm_target_error_upcall(dm_table_entry_t *, struct buf *);
314 /* dm_target_linear.c */
315 int dm_target_linear_init(dm_dev_t *, void**, char *);
316 char * dm_target_linear_status(void *);
317 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
318 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
319 int dm_target_linear_destroy(dm_table_entry_t *);
320 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
321 int dm_target_linear_dump(dm_table_entry_t *, void *, size_t, off_t);
323 /* dm_target_crypt.c */
324 int dm_target_crypt_init(dm_dev_t *, void**, char *);
325 char * dm_target_crypt_status(void *);
326 int dm_target_crypt_strategy(dm_table_entry_t *, struct buf *);
327 int dm_target_crypt_deps(dm_table_entry_t *, prop_array_t);
328 int dm_target_crypt_destroy(dm_table_entry_t *);
329 int dm_target_crypt_upcall(dm_table_entry_t *, struct buf *);
330 int dm_target_crypt_dump(dm_table_entry_t *, void *, size_t, off_t);
332 /* Generic function used to convert char to string */
333 uint64_t atoi64(const char *);
335 /* dm_target_mirror.c */
336 int dm_target_mirror_init(dm_dev_t *, void**, char *);
337 char * dm_target_mirror_status(void *);
338 int dm_target_mirror_strategy(dm_table_entry_t *, struct buf *);
339 int dm_target_mirror_deps(dm_table_entry_t *, prop_array_t);
340 int dm_target_mirror_destroy(dm_table_entry_t *);
341 int dm_target_mirror_upcall(dm_table_entry_t *, struct buf *);
343 /* dm_target_stripe.c */
344 int dm_target_stripe_init(dm_dev_t *, void**, char *);
345 char * dm_target_stripe_status(void *);
346 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
347 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
348 int dm_target_stripe_destroy(dm_table_entry_t *);
349 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
350 int dm_target_stripe_dump(dm_table_entry_t *, void *, size_t, off_t);
352 /* dm_target_snapshot.c */
353 int dm_target_snapshot_init(dm_dev_t *, void**, char *);
354 char * dm_target_snapshot_status(void *);
355 int dm_target_snapshot_strategy(dm_table_entry_t *, struct buf *);
356 int dm_target_snapshot_deps(dm_table_entry_t *, prop_array_t);
357 int dm_target_snapshot_destroy(dm_table_entry_t *);
358 int dm_target_snapshot_upcall(dm_table_entry_t *, struct buf *);
360 /* dm snapshot origin driver */
361 int dm_target_snapshot_orig_init(dm_dev_t *, void**, char *);
362 char * dm_target_snapshot_orig_status(void *);
363 int dm_target_snapshot_orig_strategy(dm_table_entry_t *, struct buf *);
364 int dm_target_snapshot_orig_deps(dm_table_entry_t *, prop_array_t);
365 int dm_target_snapshot_orig_destroy(dm_table_entry_t *);
366 int dm_target_snapshot_orig_upcall(dm_table_entry_t *, struct buf *);
369 #define DM_TABLE_ACTIVE 0
370 #define DM_TABLE_INACTIVE 1
372 int dm_table_destroy(dm_table_head_t *, uint8_t);
373 uint64_t dm_table_size(dm_table_head_t *);
374 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
375 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
376 void dm_table_release(dm_table_head_t *, uint8_t s);
377 void dm_table_switch_tables(dm_table_head_t *);
378 void dm_table_head_init(dm_table_head_t *);
379 void dm_table_head_destroy(dm_table_head_t *);
382 dm_dev_t* dm_dev_alloc(void);
383 void dm_dev_busy(dm_dev_t *);
384 int dm_dev_destroy(void);
385 void disable_dev(dm_dev_t *);
386 int dm_dev_free(dm_dev_t *);
387 int dm_dev_init(void);
388 int dm_dev_insert(dm_dev_t *);
389 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
390 prop_array_t dm_dev_prop_list(void);
391 dm_dev_t* dm_dev_rem(const char *, const char *, int);
392 /*int dm_dev_test_minor(int);*/
393 void dm_dev_unbusy(dm_dev_t *);
396 int dm_pdev_decr(dm_pdev_t *);
397 int dm_pdev_destroy(void);
398 int dm_pdev_init(void);
399 dm_pdev_t* dm_pdev_insert(const char *);
400 off_t dm_pdev_correct_dump_offset(dm_pdev_t *, off_t);
402 extern int dm_debug_level;
403 MALLOC_DECLARE(M_DM);
405 #define aprint_debug(format, ...) \
406 do { if (dm_debug_level) kprintf(format, ## __VA_ARGS__); } while(0)
407 #define aprint_normal kprintf
411 #endif /*_DM_DEV_H_*/