xref: /freebsd/sys/geom/mirror/g_mirror.h (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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$
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
69 extern 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  */
100 struct 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  */
113 struct 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
126 struct 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
145 struct 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
163 struct 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 	struct root_hold_token *sc_rootmount;
196 };
197 #define	sc_name	sc_geom->name
198 
199 u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
200 int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force);
201 int g_mirror_event_send(void *arg, int state, int flags);
202 struct g_mirror_metadata;
203 int g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
204     struct g_mirror_metadata *md);
205 int g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md);
206 void g_mirror_fill_metadata(struct g_mirror_softc *sc,
207     struct g_mirror_disk *disk, struct g_mirror_metadata *md);
208 void g_mirror_update_metadata(struct g_mirror_disk *disk);
209 
210 g_ctl_req_t g_mirror_config;
211 #endif	/* _KERNEL */
212 
213 struct g_mirror_metadata {
214 	char		md_magic[16];	/* Magic value. */
215 	uint32_t	md_version;	/* Version number. */
216 	char		md_name[16];	/* Mirror name. */
217 	uint32_t	md_mid;		/* Mirror unique ID. */
218 	uint32_t	md_did;		/* Disk unique ID. */
219 	uint8_t		md_all;		/* Number of disks in mirror. */
220 	uint32_t	md_genid;	/* Generation ID. */
221 	uint32_t	md_syncid;	/* Synchronization ID. */
222 	uint8_t		md_priority;	/* Disk priority. */
223 	uint32_t	md_slice;	/* Slice size. */
224 	uint8_t		md_balance;	/* Balance type. */
225 	uint64_t	md_mediasize;	/* Size of the smallest
226 					   disk in mirror. */
227 	uint32_t	md_sectorsize;	/* Sector size. */
228 	uint64_t	md_sync_offset;	/* Synchronized offset. */
229 	uint64_t	md_mflags;	/* Additional mirror flags. */
230 	uint64_t	md_dflags;	/* Additional disk flags. */
231 	char		md_provider[16]; /* Hardcoded provider. */
232 	uint64_t	md_provsize;	/* Provider's size. */
233 	u_char		md_hash[16];	/* MD5 hash. */
234 };
235 static __inline void
236 mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
237 {
238 	MD5_CTX ctx;
239 
240 	bcopy(md->md_magic, data, 16);
241 	le32enc(data + 16, md->md_version);
242 	bcopy(md->md_name, data + 20, 16);
243 	le32enc(data + 36, md->md_mid);
244 	le32enc(data + 40, md->md_did);
245 	*(data + 44) = md->md_all;
246 	le32enc(data + 45, md->md_genid);
247 	le32enc(data + 49, md->md_syncid);
248 	*(data + 53) = md->md_priority;
249 	le32enc(data + 54, md->md_slice);
250 	*(data + 58) = md->md_balance;
251 	le64enc(data + 59, md->md_mediasize);
252 	le32enc(data + 67, md->md_sectorsize);
253 	le64enc(data + 71, md->md_sync_offset);
254 	le64enc(data + 79, md->md_mflags);
255 	le64enc(data + 87, md->md_dflags);
256 	bcopy(md->md_provider, data + 95, 16);
257 	le64enc(data + 111, md->md_provsize);
258 	MD5Init(&ctx);
259 	MD5Update(&ctx, data, 119);
260 	MD5Final(md->md_hash, &ctx);
261 	bcopy(md->md_hash, data + 119, 16);
262 }
263 static __inline int
264 mirror_metadata_decode_v0v1(const u_char *data, struct g_mirror_metadata *md)
265 {
266 	MD5_CTX ctx;
267 
268 	bcopy(data + 20, md->md_name, 16);
269 	md->md_mid = le32dec(data + 36);
270 	md->md_did = le32dec(data + 40);
271 	md->md_all = *(data + 44);
272 	md->md_syncid = le32dec(data + 45);
273 	md->md_priority = *(data + 49);
274 	md->md_slice = le32dec(data + 50);
275 	md->md_balance = *(data + 54);
276 	md->md_mediasize = le64dec(data + 55);
277 	md->md_sectorsize = le32dec(data + 63);
278 	md->md_sync_offset = le64dec(data + 67);
279 	md->md_mflags = le64dec(data + 75);
280 	md->md_dflags = le64dec(data + 83);
281 	bcopy(data + 91, md->md_provider, 16);
282 	bcopy(data + 107, md->md_hash, 16);
283 	MD5Init(&ctx);
284 	MD5Update(&ctx, data, 107);
285 	MD5Final(md->md_hash, &ctx);
286 	if (bcmp(md->md_hash, data + 107, 16) != 0)
287 		return (EINVAL);
288 
289 	/* New fields. */
290 	md->md_genid = 0;
291 	md->md_provsize = 0;
292 
293 	return (0);
294 }
295 static __inline int
296 mirror_metadata_decode_v2(const u_char *data, struct g_mirror_metadata *md)
297 {
298 	MD5_CTX ctx;
299 
300 	bcopy(data + 20, md->md_name, 16);
301 	md->md_mid = le32dec(data + 36);
302 	md->md_did = le32dec(data + 40);
303 	md->md_all = *(data + 44);
304 	md->md_genid = le32dec(data + 45);
305 	md->md_syncid = le32dec(data + 49);
306 	md->md_priority = *(data + 53);
307 	md->md_slice = le32dec(data + 54);
308 	md->md_balance = *(data + 58);
309 	md->md_mediasize = le64dec(data + 59);
310 	md->md_sectorsize = le32dec(data + 67);
311 	md->md_sync_offset = le64dec(data + 71);
312 	md->md_mflags = le64dec(data + 79);
313 	md->md_dflags = le64dec(data + 87);
314 	bcopy(data + 95, md->md_provider, 16);
315 	bcopy(data + 111, md->md_hash, 16);
316 	MD5Init(&ctx);
317 	MD5Update(&ctx, data, 111);
318 	MD5Final(md->md_hash, &ctx);
319 	if (bcmp(md->md_hash, data + 111, 16) != 0)
320 		return (EINVAL);
321 
322 	/* New fields. */
323 	md->md_provsize = 0;
324 
325 	return (0);
326 }
327 static __inline int
328 mirror_metadata_decode_v3(const u_char *data, struct g_mirror_metadata *md)
329 {
330 	MD5_CTX ctx;
331 
332 	bcopy(data + 20, md->md_name, 16);
333 	md->md_mid = le32dec(data + 36);
334 	md->md_did = le32dec(data + 40);
335 	md->md_all = *(data + 44);
336 	md->md_genid = le32dec(data + 45);
337 	md->md_syncid = le32dec(data + 49);
338 	md->md_priority = *(data + 53);
339 	md->md_slice = le32dec(data + 54);
340 	md->md_balance = *(data + 58);
341 	md->md_mediasize = le64dec(data + 59);
342 	md->md_sectorsize = le32dec(data + 67);
343 	md->md_sync_offset = le64dec(data + 71);
344 	md->md_mflags = le64dec(data + 79);
345 	md->md_dflags = le64dec(data + 87);
346 	bcopy(data + 95, md->md_provider, 16);
347 	md->md_provsize = le64dec(data + 111);
348 	bcopy(data + 119, md->md_hash, 16);
349 	MD5Init(&ctx);
350 	MD5Update(&ctx, data, 119);
351 	MD5Final(md->md_hash, &ctx);
352 	if (bcmp(md->md_hash, data + 119, 16) != 0)
353 		return (EINVAL);
354 	return (0);
355 }
356 static __inline int
357 mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
358 {
359 	int error;
360 
361 	bcopy(data, md->md_magic, 16);
362 	md->md_version = le32dec(data + 16);
363 	switch (md->md_version) {
364 	case 0:
365 	case 1:
366 		error = mirror_metadata_decode_v0v1(data, md);
367 		break;
368 	case 2:
369 		error = mirror_metadata_decode_v2(data, md);
370 		break;
371 	case 3:
372 		error = mirror_metadata_decode_v3(data, md);
373 		break;
374 	default:
375 		error = EINVAL;
376 		break;
377 	}
378 	return (error);
379 }
380 
381 static __inline const char *
382 balance_name(u_int balance)
383 {
384 	static const char *algorithms[] = {
385 		[G_MIRROR_BALANCE_NONE] = "none",
386 		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
387 		[G_MIRROR_BALANCE_LOAD] = "load",
388 		[G_MIRROR_BALANCE_SPLIT] = "split",
389 		[G_MIRROR_BALANCE_PREFER] = "prefer",
390 		[G_MIRROR_BALANCE_MAX + 1] = "unknown"
391 	};
392 
393 	if (balance > G_MIRROR_BALANCE_MAX)
394 		balance = G_MIRROR_BALANCE_MAX + 1;
395 
396 	return (algorithms[balance]);
397 }
398 
399 static __inline int
400 balance_id(const char *name)
401 {
402 	static const char *algorithms[] = {
403 		[G_MIRROR_BALANCE_NONE] = "none",
404 		[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
405 		[G_MIRROR_BALANCE_LOAD] = "load",
406 		[G_MIRROR_BALANCE_SPLIT] = "split",
407 		[G_MIRROR_BALANCE_PREFER] = "prefer"
408 	};
409 	int n;
410 
411 	for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
412 		if (strcmp(name, algorithms[n]) == 0)
413 			return (n);
414 	}
415 	return (-1);
416 }
417 
418 static __inline void
419 mirror_metadata_dump(const struct g_mirror_metadata *md)
420 {
421 	static const char hex[] = "0123456789abcdef";
422 	char hash[16 * 2 + 1];
423 	u_int i;
424 
425 	printf("     magic: %s\n", md->md_magic);
426 	printf("   version: %u\n", (u_int)md->md_version);
427 	printf("      name: %s\n", md->md_name);
428 	printf("       mid: %u\n", (u_int)md->md_mid);
429 	printf("       did: %u\n", (u_int)md->md_did);
430 	printf("       all: %u\n", (u_int)md->md_all);
431 	printf("     genid: %u\n", (u_int)md->md_genid);
432 	printf("    syncid: %u\n", (u_int)md->md_syncid);
433 	printf("  priority: %u\n", (u_int)md->md_priority);
434 	printf("     slice: %u\n", (u_int)md->md_slice);
435 	printf("   balance: %s\n", balance_name((u_int)md->md_balance));
436 	printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
437 	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
438 	printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
439 	printf("    mflags:");
440 	if (md->md_mflags == 0)
441 		printf(" NONE");
442 	else {
443 		if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
444 			printf(" NOAUTOSYNC");
445 	}
446 	printf("\n");
447 	printf("    dflags:");
448 	if (md->md_dflags == 0)
449 		printf(" NONE");
450 	else {
451 		if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
452 			printf(" DIRTY");
453 		if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
454 			printf(" SYNCHRONIZING");
455 		if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
456 			printf(" FORCE_SYNC");
457 		if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
458 			printf(" INACTIVE");
459 	}
460 	printf("\n");
461 	printf("hcprovider: %s\n", md->md_provider);
462 	printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
463 	bzero(hash, sizeof(hash));
464 	for (i = 0; i < 16; i++) {
465 		hash[i * 2] = hex[md->md_hash[i] >> 4];
466 		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
467 	}
468 	printf("  MD5 hash: %s\n", hash);
469 }
470 #endif	/* !_G_MIRROR_H_ */
471