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