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