dm.h revision 1.46 1 /* $NetBSD: dm.h,v 1.46 2019/12/15 16:14:27 tkusumi 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 #include <sys/vnode.h>
48
49 #include <sys/device.h>
50 #include <sys/disk.h>
51 #include <sys/disklabel.h>
52
53 #include <miscfs/specfs/specdev.h> /* for v_rdev */
54
55 #include <prop/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 extern const struct dkdriver dmdkdriver;
69 extern uint32_t dm_dev_counter;
70
71 /*
72 * A device mapper table is a list of physical ranges plus the mapping target
73 * applied to them.
74 */
75
76 typedef struct dm_table_entry {
77 struct dm_dev *dm_dev; /* backlink */
78 uint64_t start;
79 uint64_t length;
80
81 struct dm_target *target; /* Link to table target. */
82 void *target_config; /* Target specific data. */
83 SLIST_ENTRY(dm_table_entry) next;
84 } dm_table_entry_t;
85
86 SLIST_HEAD(dm_table, dm_table_entry);
87
88 typedef struct dm_table dm_table_t;
89
90 typedef struct dm_table_head {
91 /* Current active table is selected with this. */
92 int cur_active_table;
93 struct dm_table tables[2];
94
95 kmutex_t table_mtx;
96 kcondvar_t table_cv; /*IO waiting cv */
97
98 uint32_t io_cnt;
99 } dm_table_head_t;
100
101 #define MAX_DEV_NAME 32
102
103 /*
104 * This structure is used to store opened vnodes for disk with name.
105 * I need this because devices can be opened only once, but I can
106 * have more than one device on one partition.
107 */
108
109 typedef struct dm_pdev {
110 char name[MAX_DEV_NAME];
111
112 struct vnode *pdev_vnode;
113 uint64_t pdev_numsec;
114 unsigned int pdev_secsize;
115 int ref_cnt; /* reference counter for users ofthis pdev */
116
117 SLIST_ENTRY(dm_pdev) next_pdev;
118 } dm_pdev_t;
119
120 /*
121 * This structure is called for every device-mapper device.
122 * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
123 */
124 TAILQ_HEAD(dm_dev_head, dm_dev);
125 //extern struct dm_dev_head dm_devs;
126
127 typedef struct dm_dev {
128 char name[DM_NAME_LEN];
129 char uuid[DM_UUID_LEN];
130
131 device_t devt; /* pointer to autoconf device_t structure */
132 uint64_t minor; /* Device minor number */
133 uint32_t flags; /* store communication protocol flags */
134
135 kmutex_t dev_mtx; /* mutex for generall device lock */
136 kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
137
138 uint32_t event_nr;
139 uint32_t ref_cnt;
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 /* for zero, error : dm_target->target_config == NULL */
154
155 /*
156 * Target config is initiated with target_init function.
157 */
158
159 /* for linear : */
160 typedef struct target_linear_config {
161 dm_pdev_t *pdev;
162 uint64_t offset;
163 TAILQ_ENTRY(target_linear_config) entries;
164 } dm_target_linear_config_t;
165
166 /*
167 * Striping devices are stored in a linked list, this might be inefficient
168 * for more than 8 striping devices and can be changed to something more
169 * scalable.
170 * TODO: look for other options than linked list.
171 */
172 TAILQ_HEAD(target_linear_devs, target_linear_config);
173
174 typedef struct target_linear_devs dm_target_linear_devs_t;
175
176 /* constant dm_target structures for error, zero, linear, stripes etc. */
177 typedef struct dm_target {
178 char name[DM_MAX_TYPE_NAME];
179 /* Initialize target_config area */
180 int (*init)(dm_table_entry_t *, int, char **);
181
182 /* Destroy target_config area */
183 int (*destroy)(dm_table_entry_t *);
184
185 int (*deps) (dm_table_entry_t *, prop_array_t);
186 /*
187 * Info/table routine are called to get params string, which is target
188 * specific. When dm_table_status_ioctl is called with flag
189 * DM_STATUS_TABLE_FLAG I have to sent params string back.
190 */
191 char *(*info)(void *);
192 char *(*table)(void *);
193 int (*strategy)(dm_table_entry_t *, struct buf *);
194 int (*upcall)(dm_table_entry_t *, struct buf *);
195 /*
196 * Optional routines.
197 */
198 int (*sync)(dm_table_entry_t *);
199 int (*secsize)(dm_table_entry_t *, unsigned int *);
200
201 uint32_t version[3];
202 uint32_t ref_cnt;
203 int max_argc;
204
205 TAILQ_ENTRY(dm_target) dm_target_next;
206 } dm_target_t;
207
208 /* Interface structures */
209
210 /* device-mapper */
211 void dmgetproperties(struct disk *, dm_table_head_t *);
212
213 /* Generic function used to convert char to string */
214 uint64_t atoi64(const char *);
215
216 /* dm_ioctl.c */
217 int dm_dev_create_ioctl(prop_dictionary_t);
218 int dm_dev_list_ioctl(prop_dictionary_t);
219 int dm_dev_remove_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);
224
225 int dm_check_version(prop_dictionary_t);
226 int dm_list_versions_ioctl(prop_dictionary_t);
227
228 int dm_table_clear_ioctl(prop_dictionary_t);
229 int dm_table_deps_ioctl(prop_dictionary_t);
230 int dm_table_load_ioctl(prop_dictionary_t);
231 int dm_table_status_ioctl(prop_dictionary_t);
232
233 /* dm_target.c */
234 dm_target_t* dm_target_alloc(const char *);
235 dm_target_t* dm_target_autoload(const char *);
236 int dm_target_destroy(void);
237 int dm_target_insert(dm_target_t *);
238 prop_array_t dm_target_prop_list(void);
239 dm_target_t* dm_target_lookup(const char *);
240 int dm_target_rem(const char *);
241 void dm_target_unbusy(dm_target_t *);
242 void dm_target_busy(dm_target_t *);
243
244 /* XXX temporally add */
245 int dm_target_init(void);
246
247 #define DM_MAX_PARAMS_SIZE 1024
248
249 /* dm_target_linear.c */
250 int dm_target_linear_init(dm_table_entry_t *, int, char **);
251 char *dm_target_linear_table(void *);
252 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
253 int dm_target_linear_sync(dm_table_entry_t *);
254 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
255 int dm_target_linear_destroy(dm_table_entry_t *);
256 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
257 int dm_target_linear_secsize(dm_table_entry_t *, unsigned int *);
258
259 /* dm_target_stripe.c */
260 int dm_target_stripe_init(dm_table_entry_t *, int, char **);
261 char *dm_target_stripe_table(void *);
262 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
263 int dm_target_stripe_sync(dm_table_entry_t *);
264 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
265 int dm_target_stripe_destroy(dm_table_entry_t *);
266 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
267 int dm_target_stripe_secsize(dm_table_entry_t *, unsigned int *);
268
269 /* dm_table.c */
270 #define DM_TABLE_ACTIVE 0
271 #define DM_TABLE_INACTIVE 1
272
273 int dm_table_destroy(dm_table_head_t *, uint8_t);
274 uint64_t dm_table_size(dm_table_head_t *);
275 uint64_t dm_inactive_table_size(dm_table_head_t *);
276 void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned int *);
277 dm_table_t *dm_table_get_entry(dm_table_head_t *, uint8_t);
278 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
279 void dm_table_release(dm_table_head_t *, uint8_t s);
280 void dm_table_switch_tables(dm_table_head_t *);
281 void dm_table_head_init(dm_table_head_t *);
282 void dm_table_head_destroy(dm_table_head_t *);
283
284 /* dm_dev.c */
285 dm_dev_t* dm_dev_alloc(void);
286 void dm_dev_busy(dm_dev_t *);
287 int dm_dev_destroy(void);
288 dm_dev_t* dm_dev_detach(device_t);
289 int dm_dev_free(dm_dev_t *);
290 int dm_dev_init(void);
291 int dm_dev_insert(dm_dev_t *);
292 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
293 prop_array_t dm_dev_prop_list(void);
294 dm_dev_t* dm_dev_rem(const char *, const char *, int);
295 /*int dm_dev_test_minor(int);*/
296 void dm_dev_unbusy(dm_dev_t *);
297
298 /* dm_pdev.c */
299 int dm_pdev_decr(dm_pdev_t *);
300 int dm_pdev_destroy(void);
301 int dm_pdev_init(void);
302 dm_pdev_t* dm_pdev_insert(const char *);
303
304 #endif /*_KERNEL*/
305
306 #endif /*_DM_DEV_H_*/
307