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