xref: /dragonfly/sys/dev/disk/dm/dm.h (revision 0212bfce)
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 #ifdef _KERNEL
36 
37 #include <sys/errno.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 
41 #include <sys/types.h>
42 #include <cpu/inttypes.h>
43 #include <sys/condvar.h>
44 #include <sys/lock.h>
45 #include <sys/queue.h>
46 
47 #include <sys/vnode.h>
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_VERSION_MAJOR	4
58 #define DM_VERSION_MINOR	16
59 #define DM_VERSION_PATCHLEVEL	0
60 
61 #define DM_MAX_TYPE_NAME 16
62 #define DM_MAX_DEV_NAME 32
63 #define DM_MAX_PARAMS_SIZE 1024
64 
65 #define DM_NAME_LEN 128
66 #define DM_UUID_LEN 129
67 
68 #define DM_TABLE_ACTIVE 0
69 #define DM_TABLE_INACTIVE 1
70 
71 typedef struct dm_mapping {
72 	union {
73 		struct dm_pdev *pdev;
74 	} data;
75 	TAILQ_ENTRY(dm_mapping) next;
76 } dm_mapping_t;
77 
78 /*
79  * A device mapper table is a list of physical ranges plus the mapping target
80  * applied to them.
81  */
82 typedef struct dm_table_entry {
83 	struct dm_dev *dev;		/* backlink */
84 	uint64_t start;
85 	uint64_t length;
86 
87 	struct dm_target *target;      /* Link to table target. */
88 	void *target_config;           /* Target specific data. */
89 	TAILQ_ENTRY(dm_table_entry) next;
90 
91 	TAILQ_HEAD(, dm_mapping) pdev_maps;
92 } dm_table_entry_t;
93 
94 TAILQ_HEAD(dm_table, dm_table_entry);
95 
96 typedef struct dm_table dm_table_t;
97 
98 typedef struct dm_table_head {
99 	/* Current active table is selected with this. */
100 	int cur_active_table;
101 	struct dm_table tables[2];
102 
103 	struct lock   table_mtx;
104 
105 	int	 io_cnt;
106 } dm_table_head_t;
107 
108 /*
109  * This structure is used to store opened vnodes for disk with name.
110  * I need this because devices can be opened only once, but I can
111  * have more then one device on one partition.
112  */
113 typedef struct dm_pdev {
114 	char name[DM_MAX_DEV_NAME];
115 	char udev_name[DM_MAX_DEV_NAME];
116 	dev_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 is_open;
144 
145 	dm_table_head_t table_head;
146 
147 	struct disk *diskp;
148 
149 	struct devstat stats;
150 
151 	TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
152 } dm_dev_t;
153 
154 /*
155  * Target config is initiated with dm_target_init function.
156  */
157 
158 /* constant dm_target structures for error, zero, linear, stripes etc. */
159 typedef struct dm_target {
160 	char name[DM_MAX_TYPE_NAME];
161 
162 	int (*init)(dm_table_entry_t *, int, char **);
163 	int (*destroy)(dm_table_entry_t *);
164 	int (*strategy)(dm_table_entry_t *, struct buf *); /* Mandatory */
165 	char *(*table)(void *);	/* DM_STATUS_TABLE_FLAG */
166 	char *(*info)(void *);	/* !DM_STATUS_TABLE_FLAG */
167 	int (*dump)(dm_table_entry_t *, void *data, size_t length, off_t offset);
168 	int (*message)(dm_table_entry_t *, char *);
169 
170 	uint32_t version[3];
171 	int ref_cnt;
172 	int max_argc;
173 
174 	TAILQ_ENTRY(dm_target) dm_target_next;
175 } dm_target_t;
176 
177 /* device-mapper.c */
178 void dmsetdiskinfo(struct disk *, dm_table_head_t *);
179 uint64_t atoi64(const char *);
180 char *dm_alloc_string(int len);
181 void dm_builtin_init(void *);
182 void dm_builtin_uninit(void *);
183 #ifdef MALLOC_DECLARE
184 MALLOC_DECLARE(M_DM);
185 #endif
186 extern int dm_debug_level;
187 
188 /* dm_ioctl.c */
189 int dm_list_versions_ioctl(prop_dictionary_t);
190 int dm_dev_create_ioctl(prop_dictionary_t);
191 int dm_dev_list_ioctl(prop_dictionary_t);
192 int dm_dev_rename_ioctl(prop_dictionary_t);
193 int dm_dev_remove_ioctl(prop_dictionary_t);
194 int dm_dev_remove_all_ioctl(prop_dictionary_t);
195 int dm_dev_status_ioctl(prop_dictionary_t);
196 int dm_dev_suspend_ioctl(prop_dictionary_t);
197 int dm_dev_resume_ioctl(prop_dictionary_t);
198 int dm_table_clear_ioctl(prop_dictionary_t);
199 int dm_table_deps_ioctl(prop_dictionary_t);
200 int dm_table_load_ioctl(prop_dictionary_t);
201 int dm_table_status_ioctl(prop_dictionary_t);
202 int dm_message_ioctl(prop_dictionary_t);
203 int dm_check_version(prop_dictionary_t);
204 
205 /* dm_target.c */
206 void dm_target_busy(dm_target_t *);
207 void dm_target_unbusy(dm_target_t *);
208 dm_target_t* dm_target_autoload(const char *);
209 dm_target_t* dm_target_lookup(const char *);
210 int dm_target_insert(dm_target_t *);
211 int dm_target_remove(char *);
212 dm_target_t* dm_target_alloc(const char *);
213 int dm_target_free(dm_target_t *);
214 prop_array_t dm_target_prop_list(void);
215 int dm_target_init(void);
216 int dm_target_uninit(void);
217 
218 /* dm_table.c  */
219 dm_table_t *dm_table_get_entry(dm_table_head_t *, uint8_t);
220 void dm_table_release(dm_table_head_t *, uint8_t s);
221 void dm_table_switch_tables(dm_table_head_t *);
222 int dm_table_destroy(dm_table_head_t *, uint8_t);
223 uint64_t dm_table_size(dm_table_head_t *);
224 uint64_t dm_inactive_table_size(dm_table_head_t *);
225 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
226 void dm_table_head_init(dm_table_head_t *);
227 void dm_table_head_destroy(dm_table_head_t *);
228 void dm_table_init_target(dm_table_entry_t *table_en, void *cfg);
229 int dm_table_add_deps(dm_table_entry_t *table_en, dm_pdev_t *pdev);
230 void dm_table_free_deps(dm_table_entry_t *table_en);
231 
232 /* dm_dev.c */
233 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
234 int dm_dev_insert(dm_dev_t *);
235 #if 0
236 dm_dev_t* dm_dev_lookup_evict(const char *, const char *, int);
237 #endif
238 int dm_dev_create(dm_dev_t **, const char *, const char *, int);
239 int dm_dev_remove(dm_dev_t *);
240 int dm_dev_remove_all(int);
241 void dm_dev_busy(dm_dev_t *);
242 void dm_dev_unbusy(dm_dev_t *);
243 prop_array_t dm_dev_prop_list(void);
244 int dm_dev_init(void);
245 int dm_dev_uninit(void);
246 
247 /* dm_pdev.c */
248 off_t dm_pdev_correct_dump_offset(dm_pdev_t *, off_t);
249 dm_pdev_t* dm_pdev_insert(const char *);
250 int dm_pdev_decr(dm_pdev_t *);
251 uint64_t dm_pdev_get_udev(dm_pdev_t *);
252 int dm_pdev_get_vattr(dm_pdev_t *, struct vattr *);
253 int dm_pdev_init(void);
254 int dm_pdev_uninit(void);
255 
256 #define dmdebug(format, ...)	\
257     do { if (dm_debug_level) kprintf("%s: "format, __func__, ## __VA_ARGS__); } while(0)
258 
259 #define DM_TARGET_MODULE(name, evh)				\
260     static moduledata_t name##_mod = {				\
261 	    #name,						\
262 	    evh,						\
263 	    NULL						\
264     };								\
265     DECLARE_MODULE(name, name##_mod, SI_SUB_DM_TARGETS,		\
266 		   SI_ORDER_ANY);				\
267     MODULE_DEPEND(name, dm, 1, 1, 1)
268 
269 #define DM_TARGET_BUILTIN(name, evh)				\
270     SYSINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY,	\
271 	dm_builtin_init, evh);					\
272     SYSUNINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY,	\
273 	dm_builtin_uninit, evh)
274 
275 #endif /*_KERNEL*/
276 
277 #endif /*_DM_DEV_H_*/
278