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