g_mirror.h revision 133115
1/*-
2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/geom/mirror/g_mirror.h 133115 2004-08-04 12:09:53Z pjd $
27 */
28
29#ifndef	_G_MIRROR_H_
30#define	_G_MIRROR_H_
31
32#include <sys/endian.h>
33#include <sys/md5.h>
34
35#define	G_MIRROR_CLASS_NAME	"MIRROR"
36
37#define	G_MIRROR_MAGIC		"GEOM::MIRROR"
38#define	G_MIRROR_VERSION	1
39
40#define	G_MIRROR_BALANCE_NONE		0
41#define	G_MIRROR_BALANCE_ROUND_ROBIN	1
42#define	G_MIRROR_BALANCE_LOAD		2
43#define	G_MIRROR_BALANCE_SPLIT		3
44#define	G_MIRROR_BALANCE_PREFER		4
45#define	G_MIRROR_BALANCE_MIN		G_MIRROR_BALANCE_NONE
46#define	G_MIRROR_BALANCE_MAX		G_MIRROR_BALANCE_PREFER
47
48#define	G_MIRROR_DISK_FLAG_DIRTY		0x0000000000000001ULL
49#define	G_MIRROR_DISK_FLAG_SYNCHRONIZING	0x0000000000000002ULL
50#define	G_MIRROR_DISK_FLAG_FORCE_SYNC		0x0000000000000004ULL
51#define	G_MIRROR_DISK_FLAG_INACTIVE		0x0000000000000008ULL
52#define	G_MIRROR_DISK_FLAG_MASK		(G_MIRROR_DISK_FLAG_DIRTY |	\
53					 G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
54					 G_MIRROR_DISK_FLAG_FORCE_SYNC | \
55					 G_MIRROR_DISK_FLAG_INACTIVE)
56
57#define	G_MIRROR_DEVICE_FLAG_NOAUTOSYNC	0x0000000000000001ULL
58#define	G_MIRROR_DEVICE_FLAG_MASK	(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC)
59
60#ifdef _KERNEL
61extern u_int g_mirror_debug;
62
63#define	G_MIRROR_DEBUG(lvl, ...)	do {				\
64	if (g_mirror_debug >= (lvl)) {					\
65		printf("GEOM_MIRROR");					\
66		if (g_mirror_debug > 0)					\
67			printf("[%u]", lvl);				\
68		printf(": ");						\
69		printf(__VA_ARGS__);					\
70		printf("\n");						\
71	}								\
72} while (0)
73#define	G_MIRROR_LOGREQ(lvl, bp, ...)	do {				\
74	if (g_mirror_debug >= (lvl)) {					\
75		printf("GEOM_MIRROR");					\
76		if (g_mirror_debug > 0)					\
77			printf("[%u]", lvl);				\
78		printf(": ");						\
79		printf(__VA_ARGS__);					\
80		printf(" ");						\
81		g_print_bio(bp);					\
82		printf("\n");						\
83	}								\
84} while (0)
85
86/*
87 * Informations needed for synchronization.
88 */
89struct g_mirror_disk_sync {
90	struct g_consumer *ds_consumer;	/* Consumer connected to our mirror. */
91	off_t		ds_offset;	/* Offset of next request to send. */
92	off_t		ds_offset_done;	/* Offset of already synchronized
93					   region. */
94	u_int		ds_syncid;	/* Disk's synchronization ID. */
95};
96
97/*
98 * Informations needed for synchronization.
99 */
100struct g_mirror_device_sync {
101	struct g_geom	*ds_geom;	/* Synchronization geom. */
102	size_t		 ds_block;	/* Synchronization request size. */
103	u_int		 ds_ndisks;	/* Number of disks in SYNCHRONIZING
104					   state. */
105	uma_zone_t	 ds_zone;	/* UMA zone for synchronization
106					   blocks. */
107};
108
109#define	G_MIRROR_DISK_STATE_NONE		0
110#define	G_MIRROR_DISK_STATE_NEW			1
111#define	G_MIRROR_DISK_STATE_ACTIVE		2
112#define	G_MIRROR_DISK_STATE_STALE		3
113#define	G_MIRROR_DISK_STATE_SYNCHRONIZING	4
114#define	G_MIRROR_DISK_STATE_DISCONNECTED	5
115#define	G_MIRROR_DISK_STATE_DESTROY		6
116struct g_mirror_disk {
117	uint32_t	 d_id;		/* Disk ID. */
118	struct g_consumer *d_consumer;	/* Consumer. */
119	struct g_mirror_softc	*d_softc; /* Back-pointer to softc. */
120	int		 d_state;	/* Disk state. */
121	u_int		 d_priority;	/* Disk priority. */
122	struct bintime	 d_delay;	/* Disk delay. */
123	struct bintime	 d_last_used;	/* When disk was last used. */
124	uint64_t	 d_flags;	/* Additional flags. */
125	struct g_mirror_disk_sync d_sync;/* Sync information. */
126	LIST_ENTRY(g_mirror_disk) d_next;
127};
128#define	d_name	d_consumer->provider->name
129
130#define	G_MIRROR_EVENT_DONTWAIT	0x1
131#define	G_MIRROR_EVENT_WAIT	0x2
132#define	G_MIRROR_EVENT_DEVICE	0x4
133#define	G_MIRROR_EVENT_DONE	0x8
134struct g_mirror_event {
135	struct g_mirror_disk	*e_disk;
136	int			 e_state;
137	int			 e_flags;
138	int			 e_error;
139	TAILQ_ENTRY(g_mirror_event) e_next;
140};
141
142#define	G_MIRROR_DEVICE_FLAG_DESTROY	0x0100000000000000ULL
143#define	G_MIRROR_DEVICE_FLAG_WAIT	0x0200000000000000ULL
144
145#define	G_MIRROR_DEVICE_STATE_STARTING		0
146#define	G_MIRROR_DEVICE_STATE_RUNNING		1
147
148#define	G_MIRROR_BUMP_ON_FIRST_WRITE		1
149#define	G_MIRROR_BUMP_IMMEDIATELY		2
150struct g_mirror_softc {
151	u_int		sc_state;	/* Device state. */
152	uint32_t	sc_slice;	/* Slice size. */
153	uint8_t		sc_balance;	/* Balance algorithm. */
154	uint64_t	sc_mediasize;	/* Device size. */
155	uint32_t	sc_sectorsize;	/* Sector size. */
156	uint64_t	sc_flags;	/* Additional flags. */
157
158	struct g_geom	*sc_geom;
159	struct g_provider *sc_provider;
160
161	uint32_t	sc_id;		/* Mirror unique ID. */
162
163	struct bio_queue_head sc_queue;
164	struct mtx	 sc_queue_mtx;
165	struct proc	*sc_worker;
166
167	LIST_HEAD(, g_mirror_disk) sc_disks;
168	u_int		sc_ndisks;	/* Number of disks. */
169	struct g_mirror_disk *sc_hint;
170
171	u_int		sc_syncid;	/* Synchronization ID. */
172	int		sc_bump_syncid;
173	struct g_mirror_device_sync sc_sync;
174
175	TAILQ_HEAD(, g_mirror_event) sc_events;
176	struct mtx	sc_events_mtx;
177
178	struct callout	sc_callout;
179};
180#define	sc_name	sc_geom->name
181
182u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
183int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force);
184int g_mirror_event_send(void *arg, int state, int flags);
185struct g_mirror_metadata;
186void g_mirror_fill_metadata(struct g_mirror_softc *sc,
187    struct g_mirror_disk *disk, struct g_mirror_metadata *md);
188void g_mirror_update_metadata(struct g_mirror_disk *disk);
189
190g_ctl_req_t g_mirror_config;
191#endif	/* _KERNEL */
192
193struct g_mirror_metadata {
194	char		md_magic[16];	/* Magic value. */
195	uint32_t	md_version;	/* Version number. */
196	char		md_name[16];	/* Mirror name. */
197	uint32_t	md_mid;		/* Mirror unique ID. */
198	uint32_t	md_did;		/* Disk unique ID. */
199	uint8_t		md_all;		/* Number of disks in mirror. */
200	uint32_t	md_syncid;	/* Synchronization ID. */
201	uint8_t		md_priority;	/* Disk priority. */
202	uint32_t	md_slice;	/* Slice size. */
203	uint8_t		md_balance;	/* Balance type. */
204	uint64_t	md_mediasize;	/* Size of the smallest
205					   disk in mirror. */
206	uint32_t	md_sectorsize;	/* Sector size. */
207	uint64_t	md_sync_offset;	/* Synchronized offset. */
208	uint64_t	md_mflags;	/* Additional mirror flags. */
209	uint64_t	md_dflags;	/* Additional disk flags. */
210	u_char		md_hash[16];	/* MD5 hash. */
211};
212static __inline void
213mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
214{
215	MD5_CTX ctx;
216
217	bcopy(md->md_magic, data, 16);
218	le32enc(data + 16, md->md_version);
219	bcopy(md->md_name, data + 20, 16);
220	le32enc(data + 36, md->md_mid);
221	le32enc(data + 40, md->md_did);
222	*(data + 44) = md->md_all;
223	le32enc(data + 45, md->md_syncid);
224	*(data + 49) = md->md_priority;
225	le32enc(data + 50, md->md_slice);
226	*(data + 54) = md->md_balance;
227	le64enc(data + 55, md->md_mediasize);
228	le32enc(data + 63, md->md_sectorsize);
229	le64enc(data + 67, md->md_sync_offset);
230	le64enc(data + 75, md->md_mflags);
231	le64enc(data + 83, md->md_dflags);
232	MD5Init(&ctx);
233	MD5Update(&ctx, data, 91);
234	MD5Final(md->md_hash, &ctx);
235	bcopy(md->md_hash, data + 91, 16);
236}
237static __inline int
238mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
239{
240	MD5_CTX ctx;
241
242	bcopy(data, md->md_magic, 16);
243	md->md_version = le32dec(data + 16);
244	bcopy(data + 20, md->md_name, 16);
245	md->md_mid = le32dec(data + 36);
246	md->md_did = le32dec(data + 40);
247	md->md_all = *(data + 44);
248	md->md_syncid = le32dec(data + 45);
249	md->md_priority = *(data + 49);
250	md->md_slice = le32dec(data + 50);
251	md->md_balance = *(data + 54);
252	md->md_mediasize = le64dec(data + 55);
253	md->md_sectorsize = le32dec(data + 63);
254	md->md_sync_offset = le64dec(data + 67);
255	md->md_mflags = le64dec(data + 75);
256	md->md_dflags = le64dec(data + 83);
257	bcopy(data + 91, md->md_hash, 16);
258	MD5Init(&ctx);
259	MD5Update(&ctx, data, 91);
260	MD5Final(md->md_hash, &ctx);
261	if (bcmp(md->md_hash, data + 91, 16) != 0)
262		return (EINVAL);
263	return (0);
264}
265
266static __inline const char *
267balance_name(u_int balance)
268{
269	static const char *algorithms[] = {
270		[G_MIRROR_BALANCE_NONE] = "none",
271		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
272		[G_MIRROR_BALANCE_LOAD] = "load",
273		[G_MIRROR_BALANCE_SPLIT] = "split",
274		[G_MIRROR_BALANCE_PREFER] = "prefer",
275		[G_MIRROR_BALANCE_MAX + 1] = "unknown"
276	};
277
278	if (balance > G_MIRROR_BALANCE_MAX)
279		balance = G_MIRROR_BALANCE_MAX + 1;
280
281	return (algorithms[balance]);
282}
283
284static __inline int
285balance_id(const char *name)
286{
287	static const char *algorithms[] = {
288		[G_MIRROR_BALANCE_NONE] = "none",
289		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
290		[G_MIRROR_BALANCE_LOAD] = "load",
291		[G_MIRROR_BALANCE_SPLIT] = "split",
292		[G_MIRROR_BALANCE_PREFER] = "prefer"
293	};
294	int n;
295
296	for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
297		if (strcmp(name, algorithms[n]) == 0)
298			return (n);
299	}
300	return (-1);
301}
302
303static __inline void
304mirror_metadata_dump(const struct g_mirror_metadata *md)
305{
306	static const char hex[] = "0123456789abcdef";
307	char hash[16 * 2 + 1];
308	u_int i;
309
310	printf("     magic: %s\n", md->md_magic);
311	printf("   version: %u\n", (u_int)md->md_version);
312	printf("      name: %s\n", md->md_name);
313	printf("       mid: %u\n", (u_int)md->md_mid);
314	printf("       did: %u\n", (u_int)md->md_did);
315	printf("       all: %u\n", (u_int)md->md_all);
316	printf("    syncid: %u\n", (u_int)md->md_syncid);
317	printf("  priority: %u\n", (u_int)md->md_priority);
318	printf("     slice: %u\n", (u_int)md->md_slice);
319	printf("   balance: %s\n", balance_name((u_int)md->md_balance));
320	printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
321	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
322	printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
323	printf("    mflags:");
324	if (md->md_mflags == 0)
325		printf(" NONE");
326	else {
327		if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
328			printf(" NOAUTOSYNC");
329	}
330	printf("\n");
331	printf("    dflags:");
332	if (md->md_mflags == 0)
333		printf(" NONE");
334	else {
335		if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
336			printf(" DIRTY");
337		if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
338			printf(" SYNCHRONIZING");
339		if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
340			printf(" FORCE_SYNC");
341		if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
342			printf(" INACTIVE");
343	}
344	printf("\n");
345	bzero(hash, sizeof(hash));
346	for (i = 0; i < 16; i++) {
347		hash[i * 2] = hex[md->md_hash[i] >> 4];
348		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
349	}
350	printf("  MD5 hash: %s\n", hash);
351}
352#endif	/* !_G_MIRROR_H_ */
353