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