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/devicestat.h>
50 #include <sys/diskslice.h>
51 #include <sys/disklabel.h>
53 #include <libprop/proplib.h>
55 #define DM_MAX_TYPE_NAME 16
56 #define DM_NAME_LEN 128
57 #define DM_UUID_LEN 129
59 #define DM_VERSION_MAJOR 4
60 #define DM_VERSION_MINOR 16
62 #define DM_VERSION_PATCHLEVEL 0
64 /*** Internal device-mapper structures ***/
67 * A table entry describes a physical range of the logical volume.
69 #define MAX_TARGET_STRING_LEN 32
72 * A device mapper table is a list of physical ranges plus the mapping target
78 typedef struct dm_table_entry {
79 struct dm_dev *dm_dev; /* backlink */
83 struct dm_target *target; /* Link to table target. */
84 void *target_config; /* Target specific data. */
85 SLIST_ENTRY(dm_table_entry) next;
88 SLIST_HEAD(dm_table, dm_table_entry);
90 typedef struct dm_table dm_table_t;
92 typedef struct dm_table_head {
93 /* Current active table is selected with this. */
95 struct dm_table tables[2];
97 struct lock table_mtx;
98 struct cv table_cv; /*IO waiting cv */
103 #define MAX_DEV_NAME 32
106 * This structure is used to store opened vnodes for disk with name.
107 * I need this because devices can be opened only once, but I can
108 * have more then one device on one partition.
111 typedef struct dm_pdev {
112 char name[MAX_DEV_NAME];
113 struct partinfo pdev_pinfo; /* partinfo of the underlying device */
115 struct vnode *pdev_vnode;
116 int ref_cnt; /* reference counter for users ofthis pdev */
118 SLIST_ENTRY(dm_pdev) next_pdev;
122 * This structure is called for every device-mapper device.
123 * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
125 TAILQ_HEAD(dm_dev_head, dm_dev);
127 typedef struct dm_dev {
128 char name[DM_NAME_LEN];
129 char uuid[DM_UUID_LEN];
131 cdev_t devt; /* pointer to autoconf device_t structure */
133 uint32_t flags; /* store communication protocol flags */
135 struct lock dev_mtx; /* mutex for generall device lock */
136 struct cv dev_cv; /* cv for between ioctl synchronisation */
144 dm_table_head_t table_head;
146 struct dm_dev_head upcalls;
149 struct lock diskp_mtx;
151 struct devstat stats;
153 TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
155 TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
158 /* Device types used for upcalls */
159 #define DM_ZERO_DEV (1 << 0)
160 #define DM_ERROR_DEV (1 << 1)
161 #define DM_LINEAR_DEV (1 << 2)
162 #define DM_MIRROR_DEV (1 << 3)
163 #define DM_STRIPE_DEV (1 << 4)
164 #define DM_SNAPSHOT_DEV (1 << 5)
165 #define DM_SNAPSHOT_ORIG_DEV (1 << 6)
166 #define DM_SPARE_DEV (1 << 7)
167 /* Set this device type only during dev remove ioctl. */
168 #define DM_DELETING_DEV (1 << 8)
169 #define DM_CRYPTO_DEV (1 << 9)
171 /* for zero, error : dm_target->target_config == NULL */
174 * Target config is initiated with target_init function.
178 typedef struct target_linear_config {
181 } dm_target_linear_config_t;
185 #define MAX_STRIPES 32
187 typedef struct target_stripe_config {
188 struct target_linear_config stripe_devs[MAX_STRIPES];
190 uint64_t stripe_chunksize;
191 } dm_target_stripe_config_t;
194 typedef struct target_mirror_config {
195 #define MAX_MIRROR_COPIES 4
197 dm_pdev_t *copies[MAX_MIRROR_COPIES];
199 /* copied blocks bitmaps administration etc*/
200 dm_pdev_t *log_pdev; /* for administration */
201 uint64_t log_regionsize; /* blocksize of mirror */
203 /* list of parts that still need copied etc.; run length encoded? */
204 } dm_target_mirror_config_t;
208 typedef struct target_snapshot_config {
209 dm_pdev_t *tsc_snap_dev;
210 /* cow dev is set only for persistent snapshot devices */
211 dm_pdev_t *tsc_cow_dev;
213 uint64_t tsc_chunk_size;
214 uint32_t tsc_persistent_dev;
215 } dm_target_snapshot_config_t;
217 /* for snapshot-origin devices */
218 typedef struct target_snapshot_origin_config {
219 dm_pdev_t *tsoc_real_dev;
220 /* list of snapshots ? */
221 } dm_target_snapshot_origin_config_t;
223 /* constant dm_target structures for error, zero, linear, stripes etc. */
224 typedef struct dm_target {
225 char name[DM_MAX_TYPE_NAME];
226 /* Initialize target_config area */
227 int (*init)(dm_dev_t *, void **, char *);
229 /* Destroy target_config area */
230 int (*destroy)(dm_table_entry_t *);
232 int (*deps) (dm_table_entry_t *, prop_array_t);
234 * Status routine is called to get params string, which is target
235 * specific. When dm_table_status_ioctl is called with flag
236 * DM_STATUS_TABLE_FLAG I have to sent params string back.
238 char * (*status)(void *);
239 int (*strategy)(dm_table_entry_t *, struct buf *);
240 int (*upcall)(dm_table_entry_t *, struct buf *);
241 int (*dump)(dm_table_entry_t *, void *data, size_t length, off_t offset);
246 TAILQ_ENTRY(dm_target) dm_target_next;
249 /* Interface structures */
252 * This structure is used to translate command sent to kernel driver in
255 * to function which I can call.
257 struct cmd_function {
259 int (*fn)(prop_dictionary_t);
263 void dmsetdiskinfo(struct disk *, dm_table_head_t *);
264 int dm_detach(dm_dev_t *);
267 int dm_dev_create_ioctl(prop_dictionary_t);
268 int dm_dev_list_ioctl(prop_dictionary_t);
269 int dm_dev_remove_ioctl(prop_dictionary_t);
270 int dm_dev_rename_ioctl(prop_dictionary_t);
271 int dm_dev_resume_ioctl(prop_dictionary_t);
272 int dm_dev_status_ioctl(prop_dictionary_t);
273 int dm_dev_suspend_ioctl(prop_dictionary_t);
275 int dm_check_version(prop_dictionary_t);
276 int dm_get_version_ioctl(prop_dictionary_t);
277 int dm_list_versions_ioctl(prop_dictionary_t);
279 int dm_table_clear_ioctl(prop_dictionary_t);
280 int dm_table_deps_ioctl(prop_dictionary_t);
281 int dm_table_load_ioctl(prop_dictionary_t);
282 int dm_table_status_ioctl(prop_dictionary_t);
285 dm_target_t* dm_target_alloc(const char *);
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 *);
294 /* XXX temporally add */
295 int dm_target_init(void);
297 #define DM_MAX_PARAMS_SIZE 1024
299 /* Generic function used to convert char to string */
300 uint64_t atoi64(const char *);
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 *);
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 *);
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 *);
327 #define DM_TABLE_ACTIVE 0
328 #define DM_TABLE_INACTIVE 1
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 *);
340 dm_dev_t* dm_dev_alloc(void);
341 void dm_dev_busy(dm_dev_t *);
342 int dm_dev_destroy(void);
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);
348 dm_dev_t* dm_dev_rem(dm_dev_t *, const char *, const char *, int);
349 /*int dm_dev_test_minor(int);*/
350 void dm_dev_unbusy(dm_dev_t *);
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 *);
357 off_t dm_pdev_correct_dump_offset(dm_pdev_t *, off_t);
359 extern int dm_debug_level;
360 MALLOC_DECLARE(M_DM);
362 #define aprint_debug(format, ...) \
363 do { if (dm_debug_level) kprintf(format, ## __VA_ARGS__); } while(0)
364 #define aprint_normal kprintf
366 #define DM_TARGET_MODULE(name, evh) \
367 static moduledata_t name##_mod = { \
372 DECLARE_MODULE(name, name##_mod, SI_SUB_DM_TARGETS, SI_ORDER_ANY)
376 #endif /*_DM_DEV_H_*/