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