sys/dev/disk/dm: Check if target has registered handlers
[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 #include <sys/vfscache.h>
54
55 #include <libprop/proplib.h>
56
57 #define DM_MAX_TYPE_NAME 16
58 #define DM_NAME_LEN 128
59 #define DM_UUID_LEN 129
60
61 #define DM_VERSION_MAJOR        4
62 #define DM_VERSION_MINOR        16
63
64 #define DM_VERSION_PATCHLEVEL   0
65
66 /*** Internal device-mapper structures ***/
67
68 typedef struct dm_mapping {
69         union {
70                 struct dm_pdev *pdev;
71         } data;
72         TAILQ_ENTRY(dm_mapping) next;
73 } dm_mapping_t;
74
75 /*
76  * A device mapper table is a list of physical ranges plus the mapping target
77  * applied to them.
78  */
79 typedef struct dm_table_entry {
80         struct dm_dev *dev;             /* backlink */
81         uint64_t start;
82         uint64_t length;
83
84         struct dm_target *target;      /* Link to table target. */
85         void *target_config;           /* Target specific data. */
86         TAILQ_ENTRY(dm_table_entry) next;
87
88         TAILQ_HEAD(, dm_mapping) pdev_maps;
89 } dm_table_entry_t;
90
91 TAILQ_HEAD(dm_table, dm_table_entry);
92
93 typedef struct dm_table dm_table_t;
94
95 typedef struct dm_table_head {
96         /* Current active table is selected with this. */
97         int cur_active_table;
98         struct dm_table tables[2];
99
100         struct lock   table_mtx;
101
102         int      io_cnt;
103 } dm_table_head_t;
104
105 #define MAX_DEV_NAME 32
106
107 /*
108  * This structure is used to store opened vnodes for disk with name.
109  * I need this because devices can be opened only once, but I can
110  * have more then one device on one partition.
111  */
112
113 typedef struct dm_pdev {
114         char name[MAX_DEV_NAME];
115         char udev_name[MAX_DEV_NAME];
116         udev_t udev;
117         struct partinfo pdev_pinfo; /* partinfo of the underlying device */
118
119         struct vnode *pdev_vnode;
120         int ref_cnt; /* reference counter for users ofthis pdev */
121
122         TAILQ_ENTRY(dm_pdev) next_pdev;
123 } dm_pdev_t;
124
125 /*
126  * This structure is called for every device-mapper device.
127  * It points to TAILQ of device tables and mirrored, snapshoted etc. devices.
128  */
129 typedef struct dm_dev {
130         char name[DM_NAME_LEN];
131         char uuid[DM_UUID_LEN];
132
133         cdev_t devt; /* pointer to autoconf device_t structure */
134         uint64_t minor;
135         uint32_t flags; /* store communication protocol flags */
136
137         struct lock dev_mtx; /* mutex for general device lock */
138         struct cv dev_cv; /* cv for between ioctl synchronization */
139
140         /* uint32_t event_nr; */
141         uint32_t ref_cnt;
142
143         uint32_t dev_type;
144         uint32_t is_open;
145
146         dm_table_head_t table_head;
147
148         struct disk *diskp;
149
150         struct devstat stats;
151
152         TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
153 } dm_dev_t;
154
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_RESERVED1_DEV       (1 << 7)
163 #define DM_RESERVED2_DEV       (1 << 8)
164 #define DM_CRYPTO_DEV          (1 << 9)
165 #define DM_RAID1_DEV           (1 << 10)
166 #define DM_DELAY_DEV           (1 << 11)
167
168 /* for zero, error : dm_target->target_config == NULL */
169
170 /*
171  * Target config is initiated with target_init function.
172  */
173
174 /* constant dm_target structures for error, zero, linear, stripes etc. */
175 typedef struct dm_target {
176         char name[DM_MAX_TYPE_NAME];
177
178         /*
179          * Mandatory handlers
180          */
181         int (*init)(dm_table_entry_t *, int, char **);
182         int (*destroy)(dm_table_entry_t *);
183         int (*strategy)(dm_table_entry_t *, struct buf *);
184
185         /*
186          * Optional handlers
187          */
188         char *(*table)(void *); /* DM_STATUS_TABLE_FLAG */
189         char *(*info)(void *);  /* !DM_STATUS_TABLE_FLAG */
190         int (*dump)(dm_table_entry_t *, void *data, size_t length, off_t offset);
191         int (*message)(dm_table_entry_t *, char *);
192
193         uint32_t version[3];
194         int ref_cnt;
195         int max_argc;
196
197         TAILQ_ENTRY(dm_target) dm_target_next;
198 } dm_target_t;
199
200 /* Interface structures */
201
202 /* device-mapper */
203 void dmsetdiskinfo(struct disk *, dm_table_head_t *);
204
205 /* dm_ioctl.c */
206 int dm_dev_create_ioctl(prop_dictionary_t);
207 int dm_dev_list_ioctl(prop_dictionary_t);
208 int dm_dev_remove_ioctl(prop_dictionary_t);
209 int dm_dev_remove_all_ioctl(prop_dictionary_t);
210 int dm_dev_rename_ioctl(prop_dictionary_t);
211 int dm_dev_resume_ioctl(prop_dictionary_t);
212 int dm_dev_status_ioctl(prop_dictionary_t);
213 int dm_dev_suspend_ioctl(prop_dictionary_t);
214
215 int dm_check_version(prop_dictionary_t);
216 int dm_list_versions_ioctl(prop_dictionary_t);
217
218 int dm_table_clear_ioctl(prop_dictionary_t);
219 int dm_table_deps_ioctl(prop_dictionary_t);
220 int dm_table_load_ioctl(prop_dictionary_t);
221 int dm_table_status_ioctl(prop_dictionary_t);
222 int dm_message_ioctl(prop_dictionary_t);
223
224 /* dm_target.c */
225 int dm_target_init(void);
226 int dm_target_uninit(void);
227 dm_target_t* dm_target_alloc(const char *);
228 dm_target_t* dm_target_autoload(const char *);
229 int dm_target_insert(dm_target_t *);
230 prop_array_t dm_target_prop_list(void);
231 dm_target_t* dm_target_lookup(const char *);
232 int dm_target_rem(char *);
233 void dm_target_unbusy(dm_target_t *);
234 void dm_target_busy(dm_target_t *);
235
236 #define DM_MAX_PARAMS_SIZE 1024
237
238 /* Generic function used to convert char to string */
239 uint64_t atoi64(const char *);
240
241 char *dm_alloc_string(int len);
242
243 /* dm_table.c  */
244 #define DM_TABLE_ACTIVE 0
245 #define DM_TABLE_INACTIVE 1
246
247 int dm_table_destroy(dm_table_head_t *, uint8_t);
248 uint64_t dm_table_size(dm_table_head_t *);
249 uint64_t dm_inactive_table_size(dm_table_head_t *);
250 dm_table_t *dm_table_get_entry(dm_table_head_t *, uint8_t);
251 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
252 void dm_table_release(dm_table_head_t *, uint8_t s);
253 void dm_table_switch_tables(dm_table_head_t *);
254 void dm_table_head_init(dm_table_head_t *);
255 void dm_table_head_destroy(dm_table_head_t *);
256 void dm_table_init_target(dm_table_entry_t *table_en, uint32_t type, void *cfg);
257 int dm_table_add_deps(dm_table_entry_t *table_en, dm_pdev_t *pdev);
258 void dm_table_free_deps(dm_table_entry_t *table_en);
259
260 /* dm_dev.c */
261 int dm_dev_init(void);
262 int dm_dev_uninit(void);
263 dm_dev_t* dm_dev_alloc(void);
264 void dm_dev_busy(dm_dev_t *);
265 int dm_dev_create(dm_dev_t **, const char *, const char *, int);
266 int dm_dev_remove(dm_dev_t *);
267 int dm_dev_remove_all(int);
268 int dm_dev_destroy(dm_dev_t *);
269 int dm_dev_free(dm_dev_t *);
270 int dm_dev_insert(dm_dev_t *);
271 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
272 prop_array_t dm_dev_prop_list(void);
273 dm_dev_t* dm_dev_rem_dev(dm_dev_t *);
274 dm_dev_t* dm_dev_rem(const char *, const char *, int);
275 void dm_dev_unbusy(dm_dev_t *);
276
277 /* dm_pdev.c */
278 int dm_pdev_init(void);
279 int dm_pdev_uninit(void);
280 int dm_pdev_decr(dm_pdev_t *);
281 uint64_t dm_pdev_get_udev(dm_pdev_t *);
282 int dm_pdev_get_vattr(dm_pdev_t *, struct vattr *);
283 dm_pdev_t* dm_pdev_insert(const char *);
284 off_t dm_pdev_correct_dump_offset(dm_pdev_t *, off_t);
285
286 /* dm builtin magic */
287 void dm_builtin_init(void *);
288 void dm_builtin_uninit(void *);
289
290 extern int dm_debug_level;
291 MALLOC_DECLARE(M_DM);
292
293 #define dmdebug(format, ...)    \
294     do { if (dm_debug_level) kprintf(format, ## __VA_ARGS__); } while(0)
295
296 #define DM_TARGET_MODULE(name, evh)                             \
297     static moduledata_t name##_mod = {                          \
298             #name,                                              \
299             evh,                                                \
300             NULL                                                \
301     };                                                          \
302     DECLARE_MODULE(name, name##_mod, SI_SUB_DM_TARGETS,         \
303                    SI_ORDER_ANY);                               \
304     MODULE_DEPEND(name, dm, 1, 1, 1)
305
306 #define DM_TARGET_BUILTIN(name, evh)                            \
307     SYSINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY,      \
308         dm_builtin_init, evh);                                  \
309     SYSUNINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY,    \
310         dm_builtin_uninit, evh)
311
312 #endif /*_KERNEL*/
313
314 #endif /*_DM_DEV_H_*/