g_mirror.h revision 142727
1/*-
2 * Copyright (c) 2004-2005 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 142727 2005-02-27 23:07:47Z 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/*
39 * Version history:
40 * 0 - Initial version number.
41 * 1 - Added 'prefer' balance algorithm.
42 * 2 - Added md_genid field to metadata.
43 * 3 - Added md_provsize field to metadata.
44 */
45#define	G_MIRROR_VERSION	3
46
47#define	G_MIRROR_BALANCE_NONE		0
48#define	G_MIRROR_BALANCE_ROUND_ROBIN	1
49#define	G_MIRROR_BALANCE_LOAD		2
50#define	G_MIRROR_BALANCE_SPLIT		3
51#define	G_MIRROR_BALANCE_PREFER		4
52#define	G_MIRROR_BALANCE_MIN		G_MIRROR_BALANCE_NONE
53#define	G_MIRROR_BALANCE_MAX		G_MIRROR_BALANCE_PREFER
54
55#define	G_MIRROR_DISK_FLAG_DIRTY		0x0000000000000001ULL
56#define	G_MIRROR_DISK_FLAG_SYNCHRONIZING	0x0000000000000002ULL
57#define	G_MIRROR_DISK_FLAG_FORCE_SYNC		0x0000000000000004ULL
58#define	G_MIRROR_DISK_FLAG_INACTIVE		0x0000000000000008ULL
59#define	G_MIRROR_DISK_FLAG_HARDCODED		0x0000000000000010ULL
60#define	G_MIRROR_DISK_FLAG_MASK		(G_MIRROR_DISK_FLAG_DIRTY |	\
61					 G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
62					 G_MIRROR_DISK_FLAG_FORCE_SYNC | \
63					 G_MIRROR_DISK_FLAG_INACTIVE)
64
65#define	G_MIRROR_DEVICE_FLAG_NOAUTOSYNC	0x0000000000000001ULL
66#define	G_MIRROR_DEVICE_FLAG_MASK	(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC)
67
68#ifdef _KERNEL
69extern u_int g_mirror_debug;
70
71#define	G_MIRROR_DEBUG(lvl, ...)	do {				\
72	if (g_mirror_debug >= (lvl)) {					\
73		printf("GEOM_MIRROR");					\
74		if (g_mirror_debug > 0)					\
75			printf("[%u]", lvl);				\
76		printf(": ");						\
77		printf(__VA_ARGS__);					\
78		printf("\n");						\
79	}								\
80} while (0)
81#define	G_MIRROR_LOGREQ(lvl, bp, ...)	do {				\
82	if (g_mirror_debug >= (lvl)) {					\
83		printf("GEOM_MIRROR");					\
84		if (g_mirror_debug > 0)					\
85			printf("[%u]", lvl);				\
86		printf(": ");						\
87		printf(__VA_ARGS__);					\
88		printf(" ");						\
89		g_print_bio(bp);					\
90		printf("\n");						\
91	}								\
92} while (0)
93
94#define	G_MIRROR_BIO_FLAG_REGULAR	0x01
95#define	G_MIRROR_BIO_FLAG_SYNC		0x02
96
97/*
98 * Informations needed for synchronization.
99 */
100struct g_mirror_disk_sync {
101	struct g_consumer *ds_consumer;	/* Consumer connected to our mirror. */
102	off_t		 ds_offset;	/* Offset of next request to send. */
103	off_t		 ds_offset_done; /* Offset of already synchronized
104					   region. */
105	off_t		 ds_resync;	/* Resynchronize from this offset. */
106	u_int		 ds_syncid;	/* Disk's synchronization ID. */
107	u_char		*ds_data;
108};
109
110/*
111 * Informations needed for synchronization.
112 */
113struct g_mirror_device_sync {
114	struct g_geom	*ds_geom;	/* Synchronization geom. */
115	u_int		 ds_ndisks;	/* Number of disks in SYNCHRONIZING
116					   state. */
117};
118
119#define	G_MIRROR_DISK_STATE_NONE		0
120#define	G_MIRROR_DISK_STATE_NEW			1
121#define	G_MIRROR_DISK_STATE_ACTIVE		2
122#define	G_MIRROR_DISK_STATE_STALE		3
123#define	G_MIRROR_DISK_STATE_SYNCHRONIZING	4
124#define	G_MIRROR_DISK_STATE_DISCONNECTED	5
125#define	G_MIRROR_DISK_STATE_DESTROY		6
126struct g_mirror_disk {
127	uint32_t	 d_id;		/* Disk ID. */
128	struct g_consumer *d_consumer;	/* Consumer. */
129	struct g_mirror_softc	*d_softc; /* Back-pointer to softc. */
130	int		 d_state;	/* Disk state. */
131	u_int		 d_priority;	/* Disk priority. */
132	struct bintime	 d_delay;	/* Disk delay. */
133	struct bintime	 d_last_used;	/* When disk was last used. */
134	uint64_t	 d_flags;	/* Additional flags. */
135	u_int		 d_genid;	/* Disk's generation ID. */
136	struct g_mirror_disk_sync d_sync;/* Sync information. */
137	LIST_ENTRY(g_mirror_disk) d_next;
138};
139#define	d_name	d_consumer->provider->name
140
141#define	G_MIRROR_EVENT_DONTWAIT	0x1
142#define	G_MIRROR_EVENT_WAIT	0x2
143#define	G_MIRROR_EVENT_DEVICE	0x4
144#define	G_MIRROR_EVENT_DONE	0x8
145struct g_mirror_event {
146	struct g_mirror_disk	*e_disk;
147	int			 e_state;
148	int			 e_flags;
149	int			 e_error;
150	TAILQ_ENTRY(g_mirror_event) e_next;
151};
152
153#define	G_MIRROR_DEVICE_FLAG_DESTROY	0x0100000000000000ULL
154#define	G_MIRROR_DEVICE_FLAG_WAIT	0x0200000000000000ULL
155
156#define	G_MIRROR_DEVICE_STATE_STARTING		0
157#define	G_MIRROR_DEVICE_STATE_RUNNING		1
158
159/* Bump syncid on first write. */
160#define	G_MIRROR_BUMP_SYNCID	0x1
161/* Bump genid immediately. */
162#define	G_MIRROR_BUMP_GENID	0x2
163struct g_mirror_softc {
164	u_int		sc_state;	/* Device state. */
165	uint32_t	sc_slice;	/* Slice size. */
166	uint8_t		sc_balance;	/* Balance algorithm. */
167	uint64_t	sc_mediasize;	/* Device size. */
168	uint32_t	sc_sectorsize;	/* Sector size. */
169	uint64_t	sc_flags;	/* Additional flags. */
170
171	struct g_geom	*sc_geom;
172	struct g_provider *sc_provider;
173
174	uint32_t	sc_id;		/* Mirror unique ID. */
175
176	struct bio_queue_head sc_queue;
177	struct mtx	 sc_queue_mtx;
178	struct proc	*sc_worker;
179
180	LIST_HEAD(, g_mirror_disk) sc_disks;
181	u_int		sc_ndisks;	/* Number of disks. */
182	struct g_mirror_disk *sc_hint;
183
184	u_int		sc_genid;	/* Generation ID. */
185	u_int		sc_syncid;	/* Synchronization ID. */
186	int		sc_bump_id;
187	struct g_mirror_device_sync sc_sync;
188	int		sc_idle;	/* DIRTY flags removed. */
189
190	TAILQ_HEAD(, g_mirror_event) sc_events;
191	struct mtx	sc_events_mtx;
192
193	struct callout	sc_callout;
194};
195#define	sc_name	sc_geom->name
196
197u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
198int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force);
199int g_mirror_event_send(void *arg, int state, int flags);
200struct g_mirror_metadata;
201int g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
202    struct g_mirror_metadata *md);
203int g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md);
204void g_mirror_fill_metadata(struct g_mirror_softc *sc,
205    struct g_mirror_disk *disk, struct g_mirror_metadata *md);
206void g_mirror_update_metadata(struct g_mirror_disk *disk);
207
208g_ctl_req_t g_mirror_config;
209#endif	/* _KERNEL */
210
211struct g_mirror_metadata {
212	char		md_magic[16];	/* Magic value. */
213	uint32_t	md_version;	/* Version number. */
214	char		md_name[16];	/* Mirror name. */
215	uint32_t	md_mid;		/* Mirror unique ID. */
216	uint32_t	md_did;		/* Disk unique ID. */
217	uint8_t		md_all;		/* Number of disks in mirror. */
218	uint32_t	md_genid;	/* Generation ID. */
219	uint32_t	md_syncid;	/* Synchronization ID. */
220	uint8_t		md_priority;	/* Disk priority. */
221	uint32_t	md_slice;	/* Slice size. */
222	uint8_t		md_balance;	/* Balance type. */
223	uint64_t	md_mediasize;	/* Size of the smallest
224					   disk in mirror. */
225	uint32_t	md_sectorsize;	/* Sector size. */
226	uint64_t	md_sync_offset;	/* Synchronized offset. */
227	uint64_t	md_mflags;	/* Additional mirror flags. */
228	uint64_t	md_dflags;	/* Additional disk flags. */
229	char		md_provider[16]; /* Hardcoded provider. */
230	uint64_t	md_provsize;	/* Provider's size. */
231	u_char		md_hash[16];	/* MD5 hash. */
232};
233static __inline void
234mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
235{
236	MD5_CTX ctx;
237
238	bcopy(md->md_magic, data, 16);
239	le32enc(data + 16, md->md_version);
240	bcopy(md->md_name, data + 20, 16);
241	le32enc(data + 36, md->md_mid);
242	le32enc(data + 40, md->md_did);
243	*(data + 44) = md->md_all;
244	le32enc(data + 45, md->md_genid);
245	le32enc(data + 49, md->md_syncid);
246	*(data + 53) = md->md_priority;
247	le32enc(data + 54, md->md_slice);
248	*(data + 58) = md->md_balance;
249	le64enc(data + 59, md->md_mediasize);
250	le32enc(data + 67, md->md_sectorsize);
251	le64enc(data + 71, md->md_sync_offset);
252	le64enc(data + 79, md->md_mflags);
253	le64enc(data + 87, md->md_dflags);
254	bcopy(md->md_provider, data + 95, 16);
255	le64enc(data + 111, md->md_provsize);
256	MD5Init(&ctx);
257	MD5Update(&ctx, data, 119);
258	MD5Final(md->md_hash, &ctx);
259	bcopy(md->md_hash, data + 119, 16);
260}
261static __inline int
262mirror_metadata_decode_v0v1(const u_char *data, struct g_mirror_metadata *md)
263{
264	MD5_CTX ctx;
265
266	bcopy(data + 20, md->md_name, 16);
267	md->md_mid = le32dec(data + 36);
268	md->md_did = le32dec(data + 40);
269	md->md_all = *(data + 44);
270	md->md_syncid = le32dec(data + 45);
271	md->md_priority = *(data + 49);
272	md->md_slice = le32dec(data + 50);
273	md->md_balance = *(data + 54);
274	md->md_mediasize = le64dec(data + 55);
275	md->md_sectorsize = le32dec(data + 63);
276	md->md_sync_offset = le64dec(data + 67);
277	md->md_mflags = le64dec(data + 75);
278	md->md_dflags = le64dec(data + 83);
279	bcopy(data + 91, md->md_provider, 16);
280	bcopy(data + 107, md->md_hash, 16);
281	MD5Init(&ctx);
282	MD5Update(&ctx, data, 107);
283	MD5Final(md->md_hash, &ctx);
284	if (bcmp(md->md_hash, data + 107, 16) != 0)
285		return (EINVAL);
286
287	/* New fields. */
288	md->md_genid = 0;
289	md->md_provsize = 0;
290
291	return (0);
292}
293static __inline int
294mirror_metadata_decode_v2(const u_char *data, struct g_mirror_metadata *md)
295{
296	MD5_CTX ctx;
297
298	bcopy(data + 20, md->md_name, 16);
299	md->md_mid = le32dec(data + 36);
300	md->md_did = le32dec(data + 40);
301	md->md_all = *(data + 44);
302	md->md_genid = le32dec(data + 45);
303	md->md_syncid = le32dec(data + 49);
304	md->md_priority = *(data + 53);
305	md->md_slice = le32dec(data + 54);
306	md->md_balance = *(data + 58);
307	md->md_mediasize = le64dec(data + 59);
308	md->md_sectorsize = le32dec(data + 67);
309	md->md_sync_offset = le64dec(data + 71);
310	md->md_mflags = le64dec(data + 79);
311	md->md_dflags = le64dec(data + 87);
312	bcopy(data + 95, md->md_provider, 16);
313	bcopy(data + 111, md->md_hash, 16);
314	MD5Init(&ctx);
315	MD5Update(&ctx, data, 111);
316	MD5Final(md->md_hash, &ctx);
317	if (bcmp(md->md_hash, data + 111, 16) != 0)
318		return (EINVAL);
319
320	/* New fields. */
321	md->md_provsize = 0;
322
323	return (0);
324}
325static __inline int
326mirror_metadata_decode_v3(const u_char *data, struct g_mirror_metadata *md)
327{
328	MD5_CTX ctx;
329
330	bcopy(data + 20, md->md_name, 16);
331	md->md_mid = le32dec(data + 36);
332	md->md_did = le32dec(data + 40);
333	md->md_all = *(data + 44);
334	md->md_genid = le32dec(data + 45);
335	md->md_syncid = le32dec(data + 49);
336	md->md_priority = *(data + 53);
337	md->md_slice = le32dec(data + 54);
338	md->md_balance = *(data + 58);
339	md->md_mediasize = le64dec(data + 59);
340	md->md_sectorsize = le32dec(data + 67);
341	md->md_sync_offset = le64dec(data + 71);
342	md->md_mflags = le64dec(data + 79);
343	md->md_dflags = le64dec(data + 87);
344	bcopy(data + 95, md->md_provider, 16);
345	md->md_provsize = le64dec(data + 111);
346	bcopy(data + 119, md->md_hash, 16);
347	MD5Init(&ctx);
348	MD5Update(&ctx, data, 119);
349	MD5Final(md->md_hash, &ctx);
350	if (bcmp(md->md_hash, data + 119, 16) != 0)
351		return (EINVAL);
352	return (0);
353}
354static __inline int
355mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
356{
357	int error;
358
359	bcopy(data, md->md_magic, 16);
360	md->md_version = le32dec(data + 16);
361	switch (md->md_version) {
362	case 0:
363	case 1:
364		error = mirror_metadata_decode_v0v1(data, md);
365		break;
366	case 2:
367		error = mirror_metadata_decode_v2(data, md);
368		break;
369	case 3:
370		error = mirror_metadata_decode_v3(data, md);
371		break;
372	default:
373		error = EINVAL;
374		break;
375	}
376	return (error);
377}
378
379static __inline const char *
380balance_name(u_int balance)
381{
382	static const char *algorithms[] = {
383		[G_MIRROR_BALANCE_NONE] = "none",
384		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
385		[G_MIRROR_BALANCE_LOAD] = "load",
386		[G_MIRROR_BALANCE_SPLIT] = "split",
387		[G_MIRROR_BALANCE_PREFER] = "prefer",
388		[G_MIRROR_BALANCE_MAX + 1] = "unknown"
389	};
390
391	if (balance > G_MIRROR_BALANCE_MAX)
392		balance = G_MIRROR_BALANCE_MAX + 1;
393
394	return (algorithms[balance]);
395}
396
397static __inline int
398balance_id(const char *name)
399{
400	static const char *algorithms[] = {
401		[G_MIRROR_BALANCE_NONE] = "none",
402		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
403		[G_MIRROR_BALANCE_LOAD] = "load",
404		[G_MIRROR_BALANCE_SPLIT] = "split",
405		[G_MIRROR_BALANCE_PREFER] = "prefer"
406	};
407	int n;
408
409	for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
410		if (strcmp(name, algorithms[n]) == 0)
411			return (n);
412	}
413	return (-1);
414}
415
416static __inline void
417mirror_metadata_dump(const struct g_mirror_metadata *md)
418{
419	static const char hex[] = "0123456789abcdef";
420	char hash[16 * 2 + 1];
421	u_int i;
422
423	printf("     magic: %s\n", md->md_magic);
424	printf("   version: %u\n", (u_int)md->md_version);
425	printf("      name: %s\n", md->md_name);
426	printf("       mid: %u\n", (u_int)md->md_mid);
427	printf("       did: %u\n", (u_int)md->md_did);
428	printf("       all: %u\n", (u_int)md->md_all);
429	printf("     genid: %u\n", (u_int)md->md_genid);
430	printf("    syncid: %u\n", (u_int)md->md_syncid);
431	printf("  priority: %u\n", (u_int)md->md_priority);
432	printf("     slice: %u\n", (u_int)md->md_slice);
433	printf("   balance: %s\n", balance_name((u_int)md->md_balance));
434	printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
435	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
436	printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
437	printf("    mflags:");
438	if (md->md_mflags == 0)
439		printf(" NONE");
440	else {
441		if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
442			printf(" NOAUTOSYNC");
443	}
444	printf("\n");
445	printf("    dflags:");
446	if (md->md_dflags == 0)
447		printf(" NONE");
448	else {
449		if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
450			printf(" DIRTY");
451		if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
452			printf(" SYNCHRONIZING");
453		if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
454			printf(" FORCE_SYNC");
455		if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
456			printf(" INACTIVE");
457	}
458	printf("\n");
459	printf("hcprovider: %s\n", md->md_provider);
460	printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
461	bzero(hash, sizeof(hash));
462	for (i = 0; i < 16; i++) {
463		hash[i * 2] = hex[md->md_hash[i] >> 4];
464		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
465	}
466	printf("  MD5 hash: %s\n", hash);
467}
468#endif	/* !_G_MIRROR_H_ */
469