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