g_mirror.h revision 135834
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 135834 2004-09-26 20:42:35Z 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_HARDCODED		0x0000000000000010ULL
53#define	G_MIRROR_DISK_FLAG_MASK		(G_MIRROR_DISK_FLAG_DIRTY |	\
54					 G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
55					 G_MIRROR_DISK_FLAG_FORCE_SYNC | \
56					 G_MIRROR_DISK_FLAG_INACTIVE)
57
58#define	G_MIRROR_DEVICE_FLAG_NOAUTOSYNC	0x0000000000000001ULL
59#define	G_MIRROR_DEVICE_FLAG_MASK	(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC)
60
61#ifdef _KERNEL
62extern u_int g_mirror_debug;
63
64#define	G_MIRROR_DEBUG(lvl, ...)	do {				\
65	if (g_mirror_debug >= (lvl)) {					\
66		printf("GEOM_MIRROR");					\
67		if (g_mirror_debug > 0)					\
68			printf("[%u]", lvl);				\
69		printf(": ");						\
70		printf(__VA_ARGS__);					\
71		printf("\n");						\
72	}								\
73} while (0)
74#define	G_MIRROR_LOGREQ(lvl, bp, ...)	do {				\
75	if (g_mirror_debug >= (lvl)) {					\
76		printf("GEOM_MIRROR");					\
77		if (g_mirror_debug > 0)					\
78			printf("[%u]", lvl);				\
79		printf(": ");						\
80		printf(__VA_ARGS__);					\
81		printf(" ");						\
82		g_print_bio(bp);					\
83		printf("\n");						\
84	}								\
85} while (0)
86
87#define	G_MIRROR_SYNC_BLOCK_SIZE	131072
88
89#define	G_MIRROR_BIO_FLAG_REGULAR	0x01
90#define	G_MIRROR_BIO_FLAG_SYNC		0x02
91
92/*
93 * Informations needed for synchronization.
94 */
95struct g_mirror_disk_sync {
96	struct g_consumer *ds_consumer;	/* Consumer connected to our mirror. */
97	off_t		 ds_offset;	/* Offset of next request to send. */
98	off_t		 ds_offset_done; /* Offset of already synchronized
99					   region. */
100	off_t		 ds_resync;	/* Resynchronize from this offset. */
101	u_int		 ds_syncid;	/* Disk's synchronization ID. */
102	u_char		*ds_data;
103};
104
105/*
106 * Informations needed for synchronization.
107 */
108struct g_mirror_device_sync {
109	struct g_geom	*ds_geom;	/* Synchronization geom. */
110	u_int		 ds_ndisks;	/* Number of disks in SYNCHRONIZING
111					   state. */
112};
113
114#define	G_MIRROR_DISK_STATE_NONE		0
115#define	G_MIRROR_DISK_STATE_NEW			1
116#define	G_MIRROR_DISK_STATE_ACTIVE		2
117#define	G_MIRROR_DISK_STATE_STALE		3
118#define	G_MIRROR_DISK_STATE_SYNCHRONIZING	4
119#define	G_MIRROR_DISK_STATE_DISCONNECTED	5
120#define	G_MIRROR_DISK_STATE_DESTROY		6
121struct g_mirror_disk {
122	uint32_t	 d_id;		/* Disk ID. */
123	struct g_consumer *d_consumer;	/* Consumer. */
124	struct g_mirror_softc	*d_softc; /* Back-pointer to softc. */
125	int		 d_state;	/* Disk state. */
126	u_int		 d_priority;	/* Disk priority. */
127	struct bintime	 d_delay;	/* Disk delay. */
128	struct bintime	 d_last_used;	/* When disk was last used. */
129	uint64_t	 d_flags;	/* Additional flags. */
130	struct g_mirror_disk_sync d_sync;/* Sync information. */
131	LIST_ENTRY(g_mirror_disk) d_next;
132};
133#define	d_name	d_consumer->provider->name
134
135#define	G_MIRROR_EVENT_DONTWAIT	0x1
136#define	G_MIRROR_EVENT_WAIT	0x2
137#define	G_MIRROR_EVENT_DEVICE	0x4
138#define	G_MIRROR_EVENT_DONE	0x8
139struct g_mirror_event {
140	struct g_mirror_disk	*e_disk;
141	int			 e_state;
142	int			 e_flags;
143	int			 e_error;
144	TAILQ_ENTRY(g_mirror_event) e_next;
145};
146
147#define	G_MIRROR_DEVICE_FLAG_DESTROY	0x0100000000000000ULL
148#define	G_MIRROR_DEVICE_FLAG_WAIT	0x0200000000000000ULL
149
150#define	G_MIRROR_DEVICE_STATE_STARTING		0
151#define	G_MIRROR_DEVICE_STATE_RUNNING		1
152
153#define	G_MIRROR_BUMP_ON_FIRST_WRITE		1
154#define	G_MIRROR_BUMP_IMMEDIATELY		2
155struct g_mirror_softc {
156	u_int		sc_state;	/* Device state. */
157	uint32_t	sc_slice;	/* Slice size. */
158	uint8_t		sc_balance;	/* Balance algorithm. */
159	uint64_t	sc_mediasize;	/* Device size. */
160	uint32_t	sc_sectorsize;	/* Sector size. */
161	uint64_t	sc_flags;	/* Additional flags. */
162
163	struct g_geom	*sc_geom;
164	struct g_provider *sc_provider;
165
166	uint32_t	sc_id;		/* Mirror unique ID. */
167
168	struct bio_queue_head sc_queue;
169	struct mtx	 sc_queue_mtx;
170	struct proc	*sc_worker;
171
172	LIST_HEAD(, g_mirror_disk) sc_disks;
173	u_int		sc_ndisks;	/* Number of disks. */
174	struct g_mirror_disk *sc_hint;
175
176	u_int		sc_syncid;	/* Synchronization ID. */
177	int		sc_bump_syncid;
178	struct g_mirror_device_sync sc_sync;
179
180	TAILQ_HEAD(, g_mirror_event) sc_events;
181	struct mtx	sc_events_mtx;
182
183	struct callout	sc_callout;
184};
185#define	sc_name	sc_geom->name
186
187u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
188int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force);
189int g_mirror_event_send(void *arg, int state, int flags);
190struct g_mirror_metadata;
191void g_mirror_fill_metadata(struct g_mirror_softc *sc,
192    struct g_mirror_disk *disk, struct g_mirror_metadata *md);
193void g_mirror_update_metadata(struct g_mirror_disk *disk);
194
195g_ctl_req_t g_mirror_config;
196#endif	/* _KERNEL */
197
198struct g_mirror_metadata {
199	char		md_magic[16];	/* Magic value. */
200	uint32_t	md_version;	/* Version number. */
201	char		md_name[16];	/* Mirror name. */
202	uint32_t	md_mid;		/* Mirror unique ID. */
203	uint32_t	md_did;		/* Disk unique ID. */
204	uint8_t		md_all;		/* Number of disks in mirror. */
205	uint32_t	md_syncid;	/* Synchronization ID. */
206	uint8_t		md_priority;	/* Disk priority. */
207	uint32_t	md_slice;	/* Slice size. */
208	uint8_t		md_balance;	/* Balance type. */
209	uint64_t	md_mediasize;	/* Size of the smallest
210					   disk in mirror. */
211	uint32_t	md_sectorsize;	/* Sector size. */
212	uint64_t	md_sync_offset;	/* Synchronized offset. */
213	uint64_t	md_mflags;	/* Additional mirror flags. */
214	uint64_t	md_dflags;	/* Additional disk flags. */
215	char		md_provider[16]; /* Hardcoded provider. */
216	u_char		md_hash[16];	/* MD5 hash. */
217};
218static __inline void
219mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
220{
221	MD5_CTX ctx;
222
223	bcopy(md->md_magic, data, 16);
224	le32enc(data + 16, md->md_version);
225	bcopy(md->md_name, data + 20, 16);
226	le32enc(data + 36, md->md_mid);
227	le32enc(data + 40, md->md_did);
228	*(data + 44) = md->md_all;
229	le32enc(data + 45, md->md_syncid);
230	*(data + 49) = md->md_priority;
231	le32enc(data + 50, md->md_slice);
232	*(data + 54) = md->md_balance;
233	le64enc(data + 55, md->md_mediasize);
234	le32enc(data + 63, md->md_sectorsize);
235	le64enc(data + 67, md->md_sync_offset);
236	le64enc(data + 75, md->md_mflags);
237	le64enc(data + 83, md->md_dflags);
238	bcopy(md->md_provider, data + 91, 16);
239	MD5Init(&ctx);
240	MD5Update(&ctx, data, 107);
241	MD5Final(md->md_hash, &ctx);
242	bcopy(md->md_hash, data + 107, 16);
243}
244static __inline int
245mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
246{
247	MD5_CTX ctx;
248
249	bcopy(data, md->md_magic, 16);
250	md->md_version = le32dec(data + 16);
251	bcopy(data + 20, md->md_name, 16);
252	md->md_mid = le32dec(data + 36);
253	md->md_did = le32dec(data + 40);
254	md->md_all = *(data + 44);
255	md->md_syncid = le32dec(data + 45);
256	md->md_priority = *(data + 49);
257	md->md_slice = le32dec(data + 50);
258	md->md_balance = *(data + 54);
259	md->md_mediasize = le64dec(data + 55);
260	md->md_sectorsize = le32dec(data + 63);
261	md->md_sync_offset = le64dec(data + 67);
262	md->md_mflags = le64dec(data + 75);
263	md->md_dflags = le64dec(data + 83);
264	bcopy(data + 91, md->md_provider, 16);
265	bcopy(data + 107, md->md_hash, 16);
266	MD5Init(&ctx);
267	MD5Update(&ctx, data, 107);
268	MD5Final(md->md_hash, &ctx);
269	if (bcmp(md->md_hash, data + 107, 16) != 0)
270		return (EINVAL);
271	return (0);
272}
273
274static __inline const char *
275balance_name(u_int balance)
276{
277	static const char *algorithms[] = {
278		[G_MIRROR_BALANCE_NONE] = "none",
279		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
280		[G_MIRROR_BALANCE_LOAD] = "load",
281		[G_MIRROR_BALANCE_SPLIT] = "split",
282		[G_MIRROR_BALANCE_PREFER] = "prefer",
283		[G_MIRROR_BALANCE_MAX + 1] = "unknown"
284	};
285
286	if (balance > G_MIRROR_BALANCE_MAX)
287		balance = G_MIRROR_BALANCE_MAX + 1;
288
289	return (algorithms[balance]);
290}
291
292static __inline int
293balance_id(const char *name)
294{
295	static const char *algorithms[] = {
296		[G_MIRROR_BALANCE_NONE] = "none",
297		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
298		[G_MIRROR_BALANCE_LOAD] = "load",
299		[G_MIRROR_BALANCE_SPLIT] = "split",
300		[G_MIRROR_BALANCE_PREFER] = "prefer"
301	};
302	int n;
303
304	for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
305		if (strcmp(name, algorithms[n]) == 0)
306			return (n);
307	}
308	return (-1);
309}
310
311static __inline void
312mirror_metadata_dump(const struct g_mirror_metadata *md)
313{
314	static const char hex[] = "0123456789abcdef";
315	char hash[16 * 2 + 1];
316	u_int i;
317
318	printf("     magic: %s\n", md->md_magic);
319	printf("   version: %u\n", (u_int)md->md_version);
320	printf("      name: %s\n", md->md_name);
321	printf("       mid: %u\n", (u_int)md->md_mid);
322	printf("       did: %u\n", (u_int)md->md_did);
323	printf("       all: %u\n", (u_int)md->md_all);
324	printf("    syncid: %u\n", (u_int)md->md_syncid);
325	printf("  priority: %u\n", (u_int)md->md_priority);
326	printf("     slice: %u\n", (u_int)md->md_slice);
327	printf("   balance: %s\n", balance_name((u_int)md->md_balance));
328	printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
329	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
330	printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
331	printf("    mflags:");
332	if (md->md_mflags == 0)
333		printf(" NONE");
334	else {
335		if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
336			printf(" NOAUTOSYNC");
337	}
338	printf("\n");
339	printf("    dflags:");
340	if (md->md_dflags == 0)
341		printf(" NONE");
342	else {
343		if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
344			printf(" DIRTY");
345		if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
346			printf(" SYNCHRONIZING");
347		if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
348			printf(" FORCE_SYNC");
349		if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
350			printf(" INACTIVE");
351	}
352	printf("\n");
353	printf("hcprovider: %s\n", md->md_provider);
354	bzero(hash, sizeof(hash));
355	for (i = 0; i < 16; i++) {
356		hash[i * 2] = hex[md->md_hash[i] >> 4];
357		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
358	}
359	printf("  MD5 hash: %s\n", hash);
360}
361#endif	/* !_G_MIRROR_H_ */
362