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