dm.h revision 1.26 1 /* $NetBSD: dm.h,v 1.26 2014/06/14 07:39:00 hannken 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
40 #include <sys/atomic.h>
41 #include <sys/fcntl.h>
42 #include <sys/condvar.h>
43 #include <sys/kauth.h>
44 #include <sys/mutex.h>
45 #include <sys/rwlock.h>
46 #include <sys/queue.h>
47
48 #include <sys/device.h>
49 #include <sys/disk.h>
50 #include <sys/disklabel.h>
51
52 #include <miscfs/specfs/specdev.h> /* for v_rdev */
53
54 #include <prop/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
77 typedef struct dm_table_entry {
78 struct dm_dev *dm_dev; /* backlink */
79 uint64_t start;
80 uint64_t length;
81
82 struct dm_target *target; /* Link to table target. */
83 void *target_config; /* Target specific data. */
84 SLIST_ENTRY(dm_table_entry) next;
85 } dm_table_entry_t;
86
87 SLIST_HEAD(dm_table, dm_table_entry);
88
89 typedef struct dm_table dm_table_t;
90
91 typedef struct dm_table_head {
92 /* Current active table is selected with this. */
93 int cur_active_table;
94 struct dm_table tables[2];
95
96 kmutex_t table_mtx;
97 kcondvar_t table_cv; /*IO waiting cv */
98
99 uint32_t io_cnt;
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 than one device on one partition.
108 */
109
110 typedef struct dm_pdev {
111 char name[MAX_DEV_NAME];
112
113 struct vnode *pdev_vnode;
114 uint64_t pdev_numsec;
115 unsigned pdev_secsize;
116 int ref_cnt; /* reference counter for users ofthis pdev */
117
118 SLIST_ENTRY(dm_pdev) next_pdev;
119 } dm_pdev_t;
120
121 /*
122 * This structure is called for every device-mapper device.
123 * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
124 */
125 TAILQ_HEAD(dm_dev_head, dm_dev);
126 //extern struct dm_dev_head dm_devs;
127
128 typedef struct dm_dev {
129 char name[DM_NAME_LEN];
130 char uuid[DM_UUID_LEN];
131
132 device_t devt; /* pointer to autoconf device_t structure */
133 uint64_t minor; /* Device minor number */
134 uint32_t flags; /* store communication protocol flags */
135
136 kmutex_t dev_mtx; /* mutex for generall device lock */
137 kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
138
139 uint32_t event_nr;
140 uint32_t ref_cnt;
141
142 uint32_t dev_type;
143
144 dm_table_head_t table_head;
145
146 struct dm_dev_head upcalls;
147
148 struct disk *diskp;
149 kmutex_t diskp_mtx;
150
151 TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
152
153 TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
154 } dm_dev_t;
155
156 /* Device types used for upcalls */
157 #define DM_ZERO_DEV (1 << 0)
158 #define DM_ERROR_DEV (1 << 1)
159 #define DM_LINEAR_DEV (1 << 2)
160 #define DM_MIRROR_DEV (1 << 3)
161 #define DM_STRIPE_DEV (1 << 4)
162 #define DM_SNAPSHOT_DEV (1 << 5)
163 #define DM_SNAPSHOT_ORIG_DEV (1 << 6)
164 #define DM_SPARE_DEV (1 << 7)
165 /* Set this device type only during dev remove ioctl. */
166 #define DM_DELETING_DEV (1 << 8)
167
168
169 /* for zero, error : dm_target->target_config == NULL */
170
171 /*
172 * Target config is initiated with target_init function.
173 */
174
175 /* for linear : */
176 typedef struct target_linear_config {
177 dm_pdev_t *pdev;
178 uint64_t offset;
179 TAILQ_ENTRY(target_linear_config) entries;
180 } dm_target_linear_config_t;
181
182 /*
183 * Striping devices are stored in a linked list, this might be inefficient
184 * for more than 8 striping devices and can be changed to something more
185 * scalable.
186 * TODO: look for other options than linked list.
187 */
188 TAILQ_HEAD(target_linear_devs, target_linear_config);
189
190 typedef struct target_linear_devs dm_target_linear_devs_t;
191
192 /* for stripe : */
193 typedef struct target_stripe_config {
194 #define DM_STRIPE_DEV_OFFSET 2
195 struct target_linear_devs stripe_devs;
196 uint8_t stripe_num;
197 uint64_t stripe_chunksize;
198 size_t params_len;
199 } dm_target_stripe_config_t;
200
201 /* for mirror : */
202 typedef struct target_mirror_config {
203 #define MAX_MIRROR_COPIES 4
204 dm_pdev_t *orig;
205 dm_pdev_t *copies[MAX_MIRROR_COPIES];
206
207 /* copied blocks bitmaps administration etc*/
208 dm_pdev_t *log_pdev; /* for administration */
209 uint64_t log_regionsize; /* blocksize of mirror */
210
211 /* list of parts that still need copied etc.; run length encoded? */
212 } dm_target_mirror_config_t;
213
214
215 /* for snapshot : */
216 typedef struct target_snapshot_config {
217 dm_pdev_t *tsc_snap_dev;
218 /* cow dev is set only for persistent snapshot devices */
219 dm_pdev_t *tsc_cow_dev;
220
221 uint64_t tsc_chunk_size;
222 uint32_t tsc_persistent_dev;
223 } dm_target_snapshot_config_t;
224
225 /* for snapshot-origin devices */
226 typedef struct target_snapshot_origin_config {
227 dm_pdev_t *tsoc_real_dev;
228 /* list of snapshots ? */
229 } dm_target_snapshot_origin_config_t;
230
231 /* constant dm_target structures for error, zero, linear, stripes etc. */
232 typedef struct dm_target {
233 char name[DM_MAX_TYPE_NAME];
234 /* Initialize target_config area */
235 int (*init)(dm_dev_t *, void **, char *);
236
237 /* Destroy target_config area */
238 int (*destroy)(dm_table_entry_t *);
239
240 int (*deps) (dm_table_entry_t *, prop_array_t);
241 /*
242 * Status routine is called to get params string, which is target
243 * specific. When dm_table_status_ioctl is called with flag
244 * DM_STATUS_TABLE_FLAG I have to sent params string back.
245 */
246 char * (*status)(void *);
247 int (*strategy)(dm_table_entry_t *, struct buf *);
248 int (*sync)(dm_table_entry_t *);
249 int (*upcall)(dm_table_entry_t *, struct buf *);
250 int (*secsize)(dm_table_entry_t *, unsigned *);
251
252 uint32_t version[3];
253 int ref_cnt;
254
255 TAILQ_ENTRY(dm_target) dm_target_next;
256 } dm_target_t;
257
258 /* Interface structures */
259
260 /*
261 * This structure is used to translate command sent to kernel driver in
262 * <key>command</key>
263 * <value></value>
264 * to function which I can call, and if the command is allowed for
265 * non-superusers.
266 */
267 struct cmd_function {
268 const char *cmd;
269 int (*fn)(prop_dictionary_t);
270 int allowed;
271 };
272
273 /* device-mapper */
274 void dmgetproperties(struct disk *, dm_table_head_t *);
275
276 /* dm_ioctl.c */
277 int dm_dev_create_ioctl(prop_dictionary_t);
278 int dm_dev_list_ioctl(prop_dictionary_t);
279 int dm_dev_remove_ioctl(prop_dictionary_t);
280 int dm_dev_rename_ioctl(prop_dictionary_t);
281 int dm_dev_resume_ioctl(prop_dictionary_t);
282 int dm_dev_status_ioctl(prop_dictionary_t);
283 int dm_dev_suspend_ioctl(prop_dictionary_t);
284
285 int dm_check_version(prop_dictionary_t);
286 int dm_get_version_ioctl(prop_dictionary_t);
287 int dm_list_versions_ioctl(prop_dictionary_t);
288
289 int dm_table_clear_ioctl(prop_dictionary_t);
290 int dm_table_deps_ioctl(prop_dictionary_t);
291 int dm_table_load_ioctl(prop_dictionary_t);
292 int dm_table_status_ioctl(prop_dictionary_t);
293
294 /* dm_target.c */
295 dm_target_t* dm_target_alloc(const char *);
296 dm_target_t* dm_target_autoload(const char *);
297 int dm_target_destroy(void);
298 int dm_target_insert(dm_target_t *);
299 prop_array_t dm_target_prop_list(void);
300 dm_target_t* dm_target_lookup(const char *);
301 int dm_target_rem(char *);
302 void dm_target_unbusy(dm_target_t *);
303 void dm_target_busy(dm_target_t *);
304
305 /* XXX temporally add */
306 int dm_target_init(void);
307
308 #define DM_MAX_PARAMS_SIZE 1024
309
310 /* dm_target_linear.c */
311 int dm_target_linear_init(dm_dev_t *, void**, char *);
312 char * dm_target_linear_status(void *);
313 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
314 int dm_target_linear_sync(dm_table_entry_t *);
315 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
316 int dm_target_linear_destroy(dm_table_entry_t *);
317 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
318 int dm_target_linear_secsize(dm_table_entry_t *, unsigned *);
319
320 /* Generic function used to convert char to string */
321 uint64_t atoi(const char *);
322
323 /* dm_target_stripe.c */
324 int dm_target_stripe_init(dm_dev_t *, void**, char *);
325 char * dm_target_stripe_status(void *);
326 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
327 int dm_target_stripe_sync(dm_table_entry_t *);
328 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
329 int dm_target_stripe_destroy(dm_table_entry_t *);
330 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
331 int dm_target_stripe_secsize(dm_table_entry_t *, unsigned *);
332
333 /* dm_table.c */
334 #define DM_TABLE_ACTIVE 0
335 #define DM_TABLE_INACTIVE 1
336
337 int dm_table_destroy(dm_table_head_t *, uint8_t);
338 uint64_t dm_table_size(dm_table_head_t *);
339 uint64_t dm_inactive_table_size(dm_table_head_t *);
340 void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned *);
341 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
342 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
343 void dm_table_release(dm_table_head_t *, uint8_t s);
344 void dm_table_switch_tables(dm_table_head_t *);
345 void dm_table_head_init(dm_table_head_t *);
346 void dm_table_head_destroy(dm_table_head_t *);
347
348 /* dm_dev.c */
349 dm_dev_t* dm_dev_alloc(void);
350 void dm_dev_busy(dm_dev_t *);
351 int dm_dev_destroy(void);
352 dm_dev_t* dm_dev_detach(device_t);
353 int dm_dev_free(dm_dev_t *);
354 int dm_dev_init(void);
355 int dm_dev_insert(dm_dev_t *);
356 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
357 prop_array_t dm_dev_prop_list(void);
358 dm_dev_t* dm_dev_rem(const char *, const char *, int);
359 /*int dm_dev_test_minor(int);*/
360 void dm_dev_unbusy(dm_dev_t *);
361
362 /* dm_pdev.c */
363 int dm_pdev_decr(dm_pdev_t *);
364 int dm_pdev_destroy(void);
365 int dm_pdev_init(void);
366 dm_pdev_t* dm_pdev_insert(const char *);
367
368 #endif /*_KERNEL*/
369
370 #endif /*_DM_DEV_H_*/
371