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>
49 #include <sys/device.h>
50 #include <sys/devicestat.h>
51 #include <sys/diskslice.h>
52 #include <sys/disklabel.h>
54 #include <libprop/proplib.h>
56 #define DM_MAX_TYPE_NAME 16
57 #define DM_NAME_LEN 128
58 #define DM_UUID_LEN 129
60 #define DM_VERSION_MAJOR 4
61 #define DM_VERSION_MINOR 16
63 #define DM_VERSION_PATCHLEVEL 0
65 /*** Internal device-mapper structures ***/
68 * A device mapper table is a list of physical ranges plus the mapping target
71 typedef struct dm_table_entry {
72 struct dm_dev *dm_dev; /* backlink */
76 struct dm_target *target; /* Link to table target. */
77 void *target_config; /* Target specific data. */
78 SLIST_ENTRY(dm_table_entry) next;
81 SLIST_HEAD(dm_table, dm_table_entry);
83 typedef struct dm_table dm_table_t;
85 typedef struct dm_table_head {
86 /* Current active table is selected with this. */
88 struct dm_table tables[2];
90 struct lock table_mtx;
95 #define MAX_DEV_NAME 32
98 * This structure is used to store opened vnodes for disk with name.
99 * I need this because devices can be opened only once, but I can
100 * have more then one device on one partition.
103 typedef struct dm_pdev {
104 char name[MAX_DEV_NAME];
105 struct partinfo pdev_pinfo; /* partinfo of the underlying device */
107 struct vnode *pdev_vnode;
108 int ref_cnt; /* reference counter for users ofthis pdev */
110 SLIST_ENTRY(dm_pdev) next_pdev;
114 * This structure is called for every device-mapper device.
115 * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
117 TAILQ_HEAD(dm_dev_head, dm_dev);
119 typedef struct dm_dev {
120 char name[DM_NAME_LEN];
121 char uuid[DM_UUID_LEN];
123 cdev_t devt; /* pointer to autoconf device_t structure */
125 uint32_t flags; /* store communication protocol flags */
127 struct lock dev_mtx; /* mutex for generall device lock */
128 struct cv dev_cv; /* cv for between ioctl synchronisation */
136 dm_table_head_t table_head;
138 struct dm_dev_head upcalls;
142 struct devstat stats;
144 TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
146 TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
149 /* Device types used for upcalls */
150 #define DM_ZERO_DEV (1 << 0)
151 #define DM_ERROR_DEV (1 << 1)
152 #define DM_LINEAR_DEV (1 << 2)
153 #define DM_MIRROR_DEV (1 << 3)
154 #define DM_STRIPE_DEV (1 << 4)
155 #define DM_SNAPSHOT_DEV (1 << 5)
156 #define DM_SNAPSHOT_ORIG_DEV (1 << 6)
157 #define DM_SPARE_DEV (1 << 7)
158 /* Set this device type only during dev remove ioctl. */
159 #define DM_DELETING_DEV (1 << 8)
160 #define DM_CRYPTO_DEV (1 << 9)
161 #define DM_RAID1_DEV (1 << 10)
163 /* for zero, error : dm_target->target_config == NULL */
166 * Target config is initiated with target_init function.
169 /* constant dm_target structures for error, zero, linear, stripes etc. */
170 typedef struct dm_target {
171 char name[DM_MAX_TYPE_NAME];
172 /* Initialize target_config area */
173 int (*init)(dm_dev_t *, void **, char *);
175 /* Message interface */
176 int (*message)(dm_table_entry_t *, char *);
178 /* Destroy target_config area */
179 int (*destroy)(dm_table_entry_t *);
181 int (*deps) (dm_table_entry_t *, prop_array_t);
183 * Status routine is called to get params string, which is target
184 * specific. When dm_table_status_ioctl is called with flag
185 * DM_STATUS_TABLE_FLAG I have to sent params string back.
187 char * (*status)(void *);
188 int (*strategy)(dm_table_entry_t *, struct buf *);
189 int (*upcall)(dm_table_entry_t *, struct buf *);
190 int (*dump)(dm_table_entry_t *, void *data, size_t length, off_t offset);
195 TAILQ_ENTRY(dm_target) dm_target_next;
198 /* Interface structures */
201 * This structure is used to translate command sent to kernel driver in
204 * to function which I can call.
206 struct cmd_function {
208 int (*fn)(prop_dictionary_t);
212 void dmsetdiskinfo(struct disk *, dm_table_head_t *);
213 int dm_detach(dm_dev_t *);
216 int dm_dev_create_ioctl(prop_dictionary_t);
217 int dm_dev_list_ioctl(prop_dictionary_t);
218 int dm_dev_remove_ioctl(prop_dictionary_t);
219 int dm_dev_remove_all_ioctl(prop_dictionary_t);
220 int dm_dev_rename_ioctl(prop_dictionary_t);
221 int dm_dev_resume_ioctl(prop_dictionary_t);
222 int dm_dev_status_ioctl(prop_dictionary_t);
223 int dm_dev_suspend_ioctl(prop_dictionary_t);
225 int dm_check_version(prop_dictionary_t);
226 int dm_get_version_ioctl(prop_dictionary_t);
227 int dm_list_versions_ioctl(prop_dictionary_t);
229 int dm_table_clear_ioctl(prop_dictionary_t);
230 int dm_table_deps_ioctl(prop_dictionary_t);
231 int dm_table_load_ioctl(prop_dictionary_t);
232 int dm_table_status_ioctl(prop_dictionary_t);
233 int dm_message_ioctl(prop_dictionary_t);
236 int dm_target_init(void);
237 int dm_target_uninit(void);
238 dm_target_t* dm_target_alloc(const char *);
239 dm_target_t* dm_target_autoload(const char *);
240 int dm_target_insert(dm_target_t *);
241 prop_array_t dm_target_prop_list(void);
242 dm_target_t* dm_target_lookup(const char *);
243 int dm_target_rem(char *);
244 void dm_target_unbusy(dm_target_t *);
245 void dm_target_busy(dm_target_t *);
247 #define DM_MAX_PARAMS_SIZE 1024
249 /* Generic function used to convert char to string */
250 uint64_t atoi64(const char *);
253 #define DM_TABLE_ACTIVE 0
254 #define DM_TABLE_INACTIVE 1
256 int dm_table_destroy(dm_table_head_t *, uint8_t);
257 uint64_t dm_table_size(dm_table_head_t *);
258 uint64_t dm_inactive_table_size(dm_table_head_t *);
259 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
260 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
261 void dm_table_release(dm_table_head_t *, uint8_t s);
262 void dm_table_switch_tables(dm_table_head_t *);
263 void dm_table_head_init(dm_table_head_t *);
264 void dm_table_head_destroy(dm_table_head_t *);
267 int dm_dev_init(void);
268 int dm_dev_uninit(void);
269 dm_dev_t* dm_dev_alloc(void);
270 void dm_dev_busy(dm_dev_t *);
271 int dm_dev_create(dm_dev_t **, const char *, const char *, int);
272 int dm_dev_remove(dm_dev_t *);
273 int dm_dev_remove_all(int);
274 int dm_dev_destroy(dm_dev_t *);
275 int dm_dev_free(dm_dev_t *);
276 int dm_dev_insert(dm_dev_t *);
277 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
278 prop_array_t dm_dev_prop_list(void);
279 dm_dev_t* dm_dev_rem(dm_dev_t *, const char *, const char *, int);
280 void dm_dev_unbusy(dm_dev_t *);
283 int dm_pdev_init(void);
284 int dm_pdev_uninit(void);
285 int dm_pdev_decr(dm_pdev_t *);
286 dm_pdev_t* dm_pdev_insert(const char *);
287 off_t dm_pdev_correct_dump_offset(dm_pdev_t *, off_t);
289 /* dm builtin magic */
290 void dm_builtin_init(void *);
291 void dm_builtin_uninit(void *);
293 extern int dm_debug_level;
294 MALLOC_DECLARE(M_DM);
296 #define aprint_debug(format, ...) \
297 do { if (dm_debug_level) kprintf(format, ## __VA_ARGS__); } while(0)
298 #define aprint_normal kprintf
300 #define DM_TARGET_MODULE(name, evh) \
301 static moduledata_t name##_mod = { \
306 DECLARE_MODULE(name, name##_mod, SI_SUB_DM_TARGETS, \
308 MODULE_DEPEND(name, dm, 1, 1, 1)
310 #define DM_TARGET_BUILTIN(name, evh) \
311 SYSINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY, \
312 dm_builtin_init, evh); \
313 SYSUNINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY, \
314 dm_builtin_uninit, evh)
318 #endif /*_DM_DEV_H_*/