dm.h revision 1.48 1 1.48 tkusumi /* $NetBSD: dm.h,v 1.48 2019/12/16 15:59:04 tkusumi Exp $ */
2 1.2 haad
3 1.2 haad /*
4 1.2 haad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.2 haad * All rights reserved.
6 1.2 haad *
7 1.2 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 haad * by Adam Hamsik.
9 1.2 haad *
10 1.2 haad * Redistribution and use in source and binary forms, with or without
11 1.2 haad * modification, are permitted provided that the following conditions
12 1.2 haad * are met:
13 1.2 haad * 1. Redistributions of source code must retain the above copyright
14 1.2 haad * notice, this list of conditions and the following disclaimer.
15 1.2 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 haad * notice, this list of conditions and the following disclaimer in the
17 1.2 haad * documentation and/or other materials provided with the distribution.
18 1.2 haad *
19 1.2 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 haad */
31 1.2 haad
32 1.48 tkusumi #ifndef _DM_H_
33 1.48 tkusumi #define _DM_H_
34 1.2 haad
35 1.2 haad #ifdef _KERNEL
36 1.2 haad
37 1.2 haad #include <sys/errno.h>
38 1.2 haad #include <sys/atomic.h>
39 1.18 haad #include <sys/fcntl.h>
40 1.2 haad #include <sys/condvar.h>
41 1.18 haad #include <sys/kauth.h>
42 1.2 haad #include <sys/mutex.h>
43 1.2 haad #include <sys/rwlock.h>
44 1.2 haad #include <sys/queue.h>
45 1.30 tkusumi #include <sys/vnode.h>
46 1.17 haad #include <sys/device.h>
47 1.21 mlelstv #include <sys/disk.h>
48 1.11 haad #include <sys/disklabel.h>
49 1.11 haad
50 1.26 hannken #include <miscfs/specfs/specdev.h> /* for v_rdev */
51 1.26 hannken
52 1.12 dyoung #include <prop/proplib.h>
53 1.12 dyoung
54 1.2 haad #define DM_MAX_TYPE_NAME 16
55 1.2 haad #define DM_NAME_LEN 128
56 1.2 haad #define DM_UUID_LEN 129
57 1.2 haad
58 1.16 haad #define DM_VERSION_MAJOR 4
59 1.16 haad #define DM_VERSION_MINOR 16
60 1.16 haad
61 1.2 haad #define DM_VERSION_PATCHLEVEL 0
62 1.2 haad
63 1.2 haad /*** Internal device-mapper structures ***/
64 1.2 haad
65 1.40 tkusumi extern const struct dkdriver dmdkdriver;
66 1.40 tkusumi extern uint32_t dm_dev_counter;
67 1.40 tkusumi
68 1.2 haad /*
69 1.2 haad * A device mapper table is a list of physical ranges plus the mapping target
70 1.2 haad * applied to them.
71 1.2 haad */
72 1.2 haad typedef struct dm_table_entry {
73 1.2 haad struct dm_dev *dm_dev; /* backlink */
74 1.2 haad uint64_t start;
75 1.2 haad uint64_t length;
76 1.2 haad
77 1.2 haad struct dm_target *target; /* Link to table target. */
78 1.2 haad void *target_config; /* Target specific data. */
79 1.2 haad SLIST_ENTRY(dm_table_entry) next;
80 1.2 haad } dm_table_entry_t;
81 1.2 haad
82 1.2 haad SLIST_HEAD(dm_table, dm_table_entry);
83 1.2 haad
84 1.2 haad typedef struct dm_table dm_table_t;
85 1.2 haad
86 1.2 haad typedef struct dm_table_head {
87 1.2 haad /* Current active table is selected with this. */
88 1.23 ahoka int cur_active_table;
89 1.48 tkusumi dm_table_t tables[2];
90 1.2 haad
91 1.2 haad kmutex_t table_mtx;
92 1.48 tkusumi kcondvar_t table_cv; /* I/O waiting cv */
93 1.2 haad
94 1.2 haad uint32_t io_cnt;
95 1.2 haad } dm_table_head_t;
96 1.2 haad
97 1.2 haad #define MAX_DEV_NAME 32
98 1.2 haad
99 1.2 haad /*
100 1.2 haad * This structure is used to store opened vnodes for disk with name.
101 1.2 haad * I need this because devices can be opened only once, but I can
102 1.25 wiz * have more than one device on one partition.
103 1.2 haad */
104 1.2 haad typedef struct dm_pdev {
105 1.2 haad char name[MAX_DEV_NAME];
106 1.2 haad
107 1.2 haad struct vnode *pdev_vnode;
108 1.21 mlelstv uint64_t pdev_numsec;
109 1.43 tkusumi unsigned int pdev_secsize;
110 1.2 haad int ref_cnt; /* reference counter for users ofthis pdev */
111 1.2 haad
112 1.2 haad SLIST_ENTRY(dm_pdev) next_pdev;
113 1.2 haad } dm_pdev_t;
114 1.2 haad
115 1.2 haad /*
116 1.2 haad * This structure is called for every device-mapper device.
117 1.2 haad * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
118 1.2 haad */
119 1.24 matt TAILQ_HEAD(dm_dev_head, dm_dev);
120 1.24 matt //extern struct dm_dev_head dm_devs;
121 1.23 ahoka
122 1.2 haad typedef struct dm_dev {
123 1.2 haad char name[DM_NAME_LEN];
124 1.2 haad char uuid[DM_UUID_LEN];
125 1.2 haad
126 1.17 haad device_t devt; /* pointer to autoconf device_t structure */
127 1.20 haad uint64_t minor; /* Device minor number */
128 1.2 haad uint32_t flags; /* store communication protocol flags */
129 1.2 haad
130 1.2 haad kmutex_t dev_mtx; /* mutex for generall device lock */
131 1.2 haad kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
132 1.23 ahoka
133 1.2 haad uint32_t event_nr;
134 1.2 haad uint32_t ref_cnt;
135 1.2 haad
136 1.2 haad dm_table_head_t table_head;
137 1.2 haad
138 1.32 tkusumi //struct dm_dev_head upcalls;
139 1.23 ahoka
140 1.8 haad struct disk *diskp;
141 1.17 haad kmutex_t diskp_mtx;
142 1.23 ahoka
143 1.32 tkusumi //TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
144 1.2 haad
145 1.2 haad TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
146 1.2 haad } dm_dev_t;
147 1.2 haad
148 1.48 tkusumi /* For linear target. */
149 1.2 haad typedef struct target_linear_config {
150 1.2 haad dm_pdev_t *pdev;
151 1.2 haad uint64_t offset;
152 1.19 haad TAILQ_ENTRY(target_linear_config) entries;
153 1.2 haad } dm_target_linear_config_t;
154 1.2 haad
155 1.19 haad /*
156 1.19 haad * Striping devices are stored in a linked list, this might be inefficient
157 1.19 haad * for more than 8 striping devices and can be changed to something more
158 1.19 haad * scalable.
159 1.19 haad * TODO: look for other options than linked list.
160 1.19 haad */
161 1.19 haad TAILQ_HEAD(target_linear_devs, target_linear_config);
162 1.19 haad
163 1.19 haad typedef struct target_linear_devs dm_target_linear_devs_t;
164 1.19 haad
165 1.2 haad /* constant dm_target structures for error, zero, linear, stripes etc. */
166 1.2 haad typedef struct dm_target {
167 1.2 haad char name[DM_MAX_TYPE_NAME];
168 1.2 haad /* Initialize target_config area */
169 1.39 tkusumi int (*init)(dm_table_entry_t *, int, char **);
170 1.2 haad
171 1.2 haad /* Destroy target_config area */
172 1.2 haad int (*destroy)(dm_table_entry_t *);
173 1.23 ahoka
174 1.48 tkusumi int (*deps)(dm_table_entry_t *, prop_array_t);
175 1.2 haad /*
176 1.42 tkusumi * Info/table routine are called to get params string, which is target
177 1.2 haad * specific. When dm_table_status_ioctl is called with flag
178 1.2 haad * DM_STATUS_TABLE_FLAG I have to sent params string back.
179 1.2 haad */
180 1.41 tkusumi char *(*info)(void *);
181 1.42 tkusumi char *(*table)(void *);
182 1.2 haad int (*strategy)(dm_table_entry_t *, struct buf *);
183 1.2 haad int (*upcall)(dm_table_entry_t *, struct buf *);
184 1.44 tkusumi /*
185 1.44 tkusumi * Optional routines.
186 1.44 tkusumi */
187 1.45 tkusumi int (*sync)(dm_table_entry_t *);
188 1.43 tkusumi int (*secsize)(dm_table_entry_t *, unsigned int *);
189 1.23 ahoka
190 1.2 haad uint32_t version[3];
191 1.27 justin uint32_t ref_cnt;
192 1.39 tkusumi int max_argc;
193 1.23 ahoka
194 1.2 haad TAILQ_ENTRY(dm_target) dm_target_next;
195 1.2 haad } dm_target_t;
196 1.2 haad
197 1.2 haad /* device-mapper */
198 1.14 haad void dmgetproperties(struct disk *, dm_table_head_t *);
199 1.2 haad
200 1.46 tkusumi /* Generic function used to convert char to string */
201 1.46 tkusumi uint64_t atoi64(const char *);
202 1.46 tkusumi
203 1.2 haad /* dm_ioctl.c */
204 1.2 haad int dm_dev_create_ioctl(prop_dictionary_t);
205 1.2 haad int dm_dev_list_ioctl(prop_dictionary_t);
206 1.2 haad int dm_dev_remove_ioctl(prop_dictionary_t);
207 1.2 haad int dm_dev_rename_ioctl(prop_dictionary_t);
208 1.2 haad int dm_dev_resume_ioctl(prop_dictionary_t);
209 1.2 haad int dm_dev_status_ioctl(prop_dictionary_t);
210 1.2 haad int dm_dev_suspend_ioctl(prop_dictionary_t);
211 1.2 haad
212 1.2 haad int dm_check_version(prop_dictionary_t);
213 1.2 haad int dm_list_versions_ioctl(prop_dictionary_t);
214 1.2 haad
215 1.2 haad int dm_table_clear_ioctl(prop_dictionary_t);
216 1.2 haad int dm_table_deps_ioctl(prop_dictionary_t);
217 1.2 haad int dm_table_load_ioctl(prop_dictionary_t);
218 1.2 haad int dm_table_status_ioctl(prop_dictionary_t);
219 1.2 haad
220 1.2 haad /* dm_target.c */
221 1.4 haad dm_target_t* dm_target_alloc(const char *);
222 1.9 haad dm_target_t* dm_target_autoload(const char *);
223 1.2 haad int dm_target_destroy(void);
224 1.2 haad int dm_target_insert(dm_target_t *);
225 1.2 haad prop_array_t dm_target_prop_list(void);
226 1.3 haad dm_target_t* dm_target_lookup(const char *);
227 1.36 tkusumi int dm_target_rem(const char *);
228 1.3 haad void dm_target_unbusy(dm_target_t *);
229 1.3 haad void dm_target_busy(dm_target_t *);
230 1.2 haad int dm_target_init(void);
231 1.2 haad
232 1.13 haad #define DM_MAX_PARAMS_SIZE 1024
233 1.13 haad
234 1.2 haad /* dm_target_linear.c */
235 1.39 tkusumi int dm_target_linear_init(dm_table_entry_t *, int, char **);
236 1.42 tkusumi char *dm_target_linear_table(void *);
237 1.2 haad int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
238 1.18 haad int dm_target_linear_sync(dm_table_entry_t *);
239 1.2 haad int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
240 1.2 haad int dm_target_linear_destroy(dm_table_entry_t *);
241 1.2 haad int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
242 1.43 tkusumi int dm_target_linear_secsize(dm_table_entry_t *, unsigned int *);
243 1.2 haad
244 1.5 haad /* dm_target_stripe.c */
245 1.39 tkusumi int dm_target_stripe_init(dm_table_entry_t *, int, char **);
246 1.42 tkusumi char *dm_target_stripe_table(void *);
247 1.5 haad int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
248 1.18 haad int dm_target_stripe_sync(dm_table_entry_t *);
249 1.5 haad int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
250 1.5 haad int dm_target_stripe_destroy(dm_table_entry_t *);
251 1.5 haad int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
252 1.43 tkusumi int dm_target_stripe_secsize(dm_table_entry_t *, unsigned int *);
253 1.5 haad
254 1.47 tkusumi /* dm_target_error.c */
255 1.47 tkusumi int dm_target_error_init(dm_table_entry_t*, int, char **);
256 1.47 tkusumi char *dm_target_error_table(void *);
257 1.47 tkusumi int dm_target_error_strategy(dm_table_entry_t *, struct buf *);
258 1.47 tkusumi int dm_target_error_deps(dm_table_entry_t *, prop_array_t);
259 1.47 tkusumi int dm_target_error_destroy(dm_table_entry_t *);
260 1.47 tkusumi int dm_target_error_upcall(dm_table_entry_t *, struct buf *);
261 1.47 tkusumi
262 1.47 tkusumi /* dm_target_zero.c */
263 1.47 tkusumi int dm_target_zero_init(dm_table_entry_t *, int, char **);
264 1.47 tkusumi char *dm_target_zero_table(void *);
265 1.47 tkusumi int dm_target_zero_strategy(dm_table_entry_t *, struct buf *);
266 1.47 tkusumi int dm_target_zero_destroy(dm_table_entry_t *);
267 1.47 tkusumi int dm_target_zero_deps(dm_table_entry_t *, prop_array_t);
268 1.47 tkusumi int dm_target_zero_upcall(dm_table_entry_t *, struct buf *);
269 1.47 tkusumi
270 1.2 haad /* dm_table.c */
271 1.2 haad #define DM_TABLE_ACTIVE 0
272 1.2 haad #define DM_TABLE_INACTIVE 1
273 1.2 haad
274 1.2 haad int dm_table_destroy(dm_table_head_t *, uint8_t);
275 1.2 haad uint64_t dm_table_size(dm_table_head_t *);
276 1.23 ahoka uint64_t dm_inactive_table_size(dm_table_head_t *);
277 1.43 tkusumi void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned int *);
278 1.34 tkusumi dm_table_t *dm_table_get_entry(dm_table_head_t *, uint8_t);
279 1.2 haad int dm_table_get_target_count(dm_table_head_t *, uint8_t);
280 1.2 haad void dm_table_release(dm_table_head_t *, uint8_t s);
281 1.2 haad void dm_table_switch_tables(dm_table_head_t *);
282 1.2 haad void dm_table_head_init(dm_table_head_t *);
283 1.2 haad void dm_table_head_destroy(dm_table_head_t *);
284 1.2 haad
285 1.2 haad /* dm_dev.c */
286 1.2 haad dm_dev_t* dm_dev_alloc(void);
287 1.2 haad void dm_dev_busy(dm_dev_t *);
288 1.2 haad int dm_dev_destroy(void);
289 1.17 haad dm_dev_t* dm_dev_detach(device_t);
290 1.2 haad int dm_dev_free(dm_dev_t *);
291 1.2 haad int dm_dev_init(void);
292 1.2 haad int dm_dev_insert(dm_dev_t *);
293 1.2 haad dm_dev_t* dm_dev_lookup(const char *, const char *, int);
294 1.2 haad prop_array_t dm_dev_prop_list(void);
295 1.2 haad dm_dev_t* dm_dev_rem(const char *, const char *, int);
296 1.2 haad /*int dm_dev_test_minor(int);*/
297 1.2 haad void dm_dev_unbusy(dm_dev_t *);
298 1.2 haad
299 1.2 haad /* dm_pdev.c */
300 1.2 haad int dm_pdev_decr(dm_pdev_t *);
301 1.2 haad int dm_pdev_destroy(void);
302 1.2 haad int dm_pdev_init(void);
303 1.2 haad dm_pdev_t* dm_pdev_insert(const char *);
304 1.2 haad
305 1.2 haad #endif /*_KERNEL*/
306 1.2 haad
307 1.48 tkusumi #endif /*_DM_H_*/
308