1/* $NetBSD: dm.h,v 1.27 2014/10/02 21:58:16 justin 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
48#include <sys/device.h>
49#include <sys/disk.h>
50#include <sys/disklabel.h>
51
52#include <miscfs/specfs/specdev.h> /* for v_rdev */
53
54#include <prop/proplib.h>
55
56#define DM_MAX_TYPE_NAME 16
57#define DM_NAME_LEN 128
58#define DM_UUID_LEN 129
59
60#define DM_VERSION_MAJOR 4
61#define DM_VERSION_MINOR 16
62
63#define DM_VERSION_PATCHLEVEL 0
64
65/*** Internal device-mapper structures ***/
66
67/*
68 * A table entry describes a physical range of the logical volume.
69 */
70#define MAX_TARGET_STRING_LEN 32
71
72/*
73 * A device mapper table is a list of physical ranges plus the mapping target
74 * applied to them.
75 */
76
77typedef struct dm_table_entry {
78 struct dm_dev *dm_dev; /* backlink */
79 uint64_t start;
80 uint64_t length;
81
82 struct dm_target *target; /* Link to table target. */
83 void *target_config; /* Target specific data. */
84 SLIST_ENTRY(dm_table_entry) next;
85} dm_table_entry_t;
86
87SLIST_HEAD(dm_table, dm_table_entry);
88
89typedef struct dm_table dm_table_t;
90
91typedef struct dm_table_head {
92 /* Current active table is selected with this. */
93 int cur_active_table;
94 struct dm_table tables[2];
95
96 kmutex_t table_mtx;
97 kcondvar_t table_cv; /*IO waiting cv */
98
99 uint32_t io_cnt;
100} dm_table_head_t;
101
102#define MAX_DEV_NAME 32
103
104/*
105 * This structure is used to store opened vnodes for disk with name.
106 * I need this because devices can be opened only once, but I can
107 * have more than one device on one partition.
108 */
109
110typedef struct dm_pdev {
111 char name[MAX_DEV_NAME];
112
113 struct vnode *pdev_vnode;
114 uint64_t pdev_numsec;
115 unsigned pdev_secsize;
116 int ref_cnt; /* reference counter for users ofthis pdev */
117
118 SLIST_ENTRY(dm_pdev) next_pdev;
119} dm_pdev_t;
120
121/*
122 * This structure is called for every device-mapper device.
123 * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
124 */
125TAILQ_HEAD(dm_dev_head, dm_dev);
126//extern struct dm_dev_head dm_devs;
127
128typedef struct dm_dev {
129 char name[DM_NAME_LEN];
130 char uuid[DM_UUID_LEN];
131
132 device_t devt; /* pointer to autoconf device_t structure */
133 uint64_t minor; /* Device minor number */
134 uint32_t flags; /* store communication protocol flags */
135
136 kmutex_t dev_mtx; /* mutex for generall device lock */
137 kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
138
139 uint32_t event_nr;
140 uint32_t ref_cnt;
141
142 uint32_t dev_type;
143
144 dm_table_head_t table_head;
145
146 struct dm_dev_head upcalls;
147
148 struct disk *diskp;
149 kmutex_t diskp_mtx;
150
151 TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
152
153 TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
154} dm_dev_t;
155
156/* Device types used for upcalls */
157#define DM_ZERO_DEV (1 << 0)
158#define DM_ERROR_DEV (1 << 1)
159#define DM_LINEAR_DEV (1 << 2)
160#define DM_MIRROR_DEV (1 << 3)
161#define DM_STRIPE_DEV (1 << 4)
162#define DM_SNAPSHOT_DEV (1 << 5)
163#define DM_SNAPSHOT_ORIG_DEV (1 << 6)
164#define DM_SPARE_DEV (1 << 7)
165/* Set this device type only during dev remove ioctl. */
166#define DM_DELETING_DEV (1 << 8)
167
168
169/* for zero, error : dm_target->target_config == NULL */
170
171/*
172 * Target config is initiated with target_init function.
173 */
174
175/* for linear : */
176typedef struct target_linear_config {
177 dm_pdev_t *pdev;
178 uint64_t offset;
179 TAILQ_ENTRY(target_linear_config) entries;
180} dm_target_linear_config_t;
181
182/*
183 * Striping devices are stored in a linked list, this might be inefficient
184 * for more than 8 striping devices and can be changed to something more
185 * scalable.
186 * TODO: look for other options than linked list.
187 */
188TAILQ_HEAD(target_linear_devs, target_linear_config);
189
190typedef struct target_linear_devs dm_target_linear_devs_t;
191
192/* for stripe : */
193typedef struct target_stripe_config {
194#define DM_STRIPE_DEV_OFFSET 2
195 struct target_linear_devs stripe_devs;
196 uint8_t stripe_num;
197 uint64_t stripe_chunksize;
198 size_t params_len;
199} dm_target_stripe_config_t;
200
201/* for mirror : */
202typedef struct target_mirror_config {
203#define MAX_MIRROR_COPIES 4
204 dm_pdev_t *orig;
205 dm_pdev_t *copies[MAX_MIRROR_COPIES];
206
207 /* copied blocks bitmaps administration etc*/
208 dm_pdev_t *log_pdev; /* for administration */
209 uint64_t log_regionsize; /* blocksize of mirror */
210
211 /* list of parts that still need copied etc.; run length encoded? */
212} dm_target_mirror_config_t;
213
214
215/* for snapshot : */
216typedef struct target_snapshot_config {
217 dm_pdev_t *tsc_snap_dev;
218 /* cow dev is set only for persistent snapshot devices */
219 dm_pdev_t *tsc_cow_dev;
220
221 uint64_t tsc_chunk_size;
222 uint32_t tsc_persistent_dev;
223} dm_target_snapshot_config_t;
224
225/* for snapshot-origin devices */
226typedef struct target_snapshot_origin_config {
227 dm_pdev_t *tsoc_real_dev;
228 /* list of snapshots ? */
229} dm_target_snapshot_origin_config_t;
230
231/* constant dm_target structures for error, zero, linear, stripes etc. */
232typedef struct dm_target {
233 char name[DM_MAX_TYPE_NAME];
234 /* Initialize target_config area */
235 int (*init)(dm_dev_t *, void **, char *);
236
237 /* Destroy target_config area */
238 int (*destroy)(dm_table_entry_t *);
239
240 int (*deps) (dm_table_entry_t *, prop_array_t);
241 /*
242 * Status routine is called to get params string, which is target
243 * specific. When dm_table_status_ioctl is called with flag
244 * DM_STATUS_TABLE_FLAG I have to sent params string back.
245 */
246 char * (*status)(void *);
247 int (*strategy)(dm_table_entry_t *, struct buf *);
248 int (*sync)(dm_table_entry_t *);
249 int (*upcall)(dm_table_entry_t *, struct buf *);
250 int (*secsize)(dm_table_entry_t *, unsigned *);
251
252 uint32_t version[3];
253 uint32_t ref_cnt;
254
255 TAILQ_ENTRY(dm_target) dm_target_next;
256} dm_target_t;
257
258/* Interface structures */
259
260/*
261 * This structure is used to translate command sent to kernel driver in
262 * <key>command</key>
263 * <value></value>
264 * to function which I can call, and if the command is allowed for
265 * non-superusers.
266 */
267struct cmd_function {
268 const char *cmd;
269 int (*fn)(prop_dictionary_t);
270 int allowed;
271};
272
273/* device-mapper */
274void dmgetproperties(struct disk *, dm_table_head_t *);
275
276/* dm_ioctl.c */
277int dm_dev_create_ioctl(prop_dictionary_t);
278int dm_dev_list_ioctl(prop_dictionary_t);
279int dm_dev_remove_ioctl(prop_dictionary_t);
280int dm_dev_rename_ioctl(prop_dictionary_t);
281int dm_dev_resume_ioctl(prop_dictionary_t);
282int dm_dev_status_ioctl(prop_dictionary_t);
283int dm_dev_suspend_ioctl(prop_dictionary_t);
284
285int dm_check_version(prop_dictionary_t);
286int dm_get_version_ioctl(prop_dictionary_t);
287int dm_list_versions_ioctl(prop_dictionary_t);
288
289int dm_table_clear_ioctl(prop_dictionary_t);
290int dm_table_deps_ioctl(prop_dictionary_t);
291int dm_table_load_ioctl(prop_dictionary_t);
292int dm_table_status_ioctl(prop_dictionary_t);
293
294/* dm_target.c */
295dm_target_t* dm_target_alloc(const char *);
296dm_target_t* dm_target_autoload(const char *);
297int dm_target_destroy(void);
298int dm_target_insert(dm_target_t *);
299prop_array_t dm_target_prop_list(void);
300dm_target_t* dm_target_lookup(const char *);
301int dm_target_rem(char *);
302void dm_target_unbusy(dm_target_t *);
303void dm_target_busy(dm_target_t *);
304
305/* XXX temporally add */
306int dm_target_init(void);
307
308#define DM_MAX_PARAMS_SIZE 1024
309
310/* dm_target_linear.c */
311int dm_target_linear_init(dm_dev_t *, void**, char *);
312char * dm_target_linear_status(void *);
313int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
314int dm_target_linear_sync(dm_table_entry_t *);
315int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
316int dm_target_linear_destroy(dm_table_entry_t *);
317int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
318int dm_target_linear_secsize(dm_table_entry_t *, unsigned *);
319
320/* Generic function used to convert char to string */
321uint64_t atoi(const char *);
322
323/* dm_target_stripe.c */
324int dm_target_stripe_init(dm_dev_t *, void**, char *);
325char * dm_target_stripe_status(void *);
326int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
327int dm_target_stripe_sync(dm_table_entry_t *);
328int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
329int dm_target_stripe_destroy(dm_table_entry_t *);
330int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
331int dm_target_stripe_secsize(dm_table_entry_t *, unsigned *);
332
333/* dm_table.c */
334#define DM_TABLE_ACTIVE 0
335#define DM_TABLE_INACTIVE 1
336
337int dm_table_destroy(dm_table_head_t *, uint8_t);
338uint64_t dm_table_size(dm_table_head_t *);
339uint64_t dm_inactive_table_size(dm_table_head_t *);
340void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned *);
341dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
342int dm_table_get_target_count(dm_table_head_t *, uint8_t);
343void dm_table_release(dm_table_head_t *, uint8_t s);
344void dm_table_switch_tables(dm_table_head_t *);
345void dm_table_head_init(dm_table_head_t *);
346void dm_table_head_destroy(dm_table_head_t *);
347
348/* dm_dev.c */
349dm_dev_t* dm_dev_alloc(void);
350void dm_dev_busy(dm_dev_t *);
351int dm_dev_destroy(void);
352dm_dev_t* dm_dev_detach(device_t);
353int dm_dev_free(dm_dev_t *);
354int dm_dev_init(void);
355int dm_dev_insert(dm_dev_t *);
356dm_dev_t* dm_dev_lookup(const char *, const char *, int);
357prop_array_t dm_dev_prop_list(void);
358dm_dev_t* dm_dev_rem(const char *, const char *, int);
359/*int dm_dev_test_minor(int);*/
360void dm_dev_unbusy(dm_dev_t *);
361
362/* dm_pdev.c */
363int dm_pdev_decr(dm_pdev_t *);
364int dm_pdev_destroy(void);
365int dm_pdev_init(void);
366dm_pdev_t* dm_pdev_insert(const char *);
367
368#endif /*_KERNEL*/
369
370#endif /*_DM_DEV_H_*/
371