xref: /freebsd/sys/geom/raid3/g_raid3.h (revision 7750ad47a9a7dbc83f87158464170c8640723293)
1 /*-
2  * Copyright (c) 2004-2006 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_RAID3_H_
30 #define	_G_RAID3_H_
31 
32 #include <sys/endian.h>
33 #include <sys/md5.h>
34 
35 #define	G_RAID3_CLASS_NAME	"RAID3"
36 
37 #define	G_RAID3_MAGIC		"GEOM::RAID3"
38 /*
39  * Version history:
40  * 0 - Initial version number.
41  * 1 - Added 'round-robin reading' algorithm.
42  * 2 - Added 'verify reading' algorithm.
43  * 3 - Added md_genid field to metadata.
44  * 4 - Added md_provsize field to metadata.
45  * 5 - Added 'no failure synchronization' flag.
46  */
47 #define	G_RAID3_VERSION		5
48 
49 #define	G_RAID3_DISK_FLAG_DIRTY		0x0000000000000001ULL
50 #define	G_RAID3_DISK_FLAG_SYNCHRONIZING	0x0000000000000002ULL
51 #define	G_RAID3_DISK_FLAG_FORCE_SYNC	0x0000000000000004ULL
52 #define	G_RAID3_DISK_FLAG_HARDCODED	0x0000000000000008ULL
53 #define	G_RAID3_DISK_FLAG_BROKEN	0x0000000000000010ULL
54 #define	G_RAID3_DISK_FLAG_MASK		(G_RAID3_DISK_FLAG_DIRTY |	\
55 					 G_RAID3_DISK_FLAG_SYNCHRONIZING | \
56 					 G_RAID3_DISK_FLAG_FORCE_SYNC)
57 
58 #define	G_RAID3_DEVICE_FLAG_NOAUTOSYNC	0x0000000000000001ULL
59 #define	G_RAID3_DEVICE_FLAG_ROUND_ROBIN	0x0000000000000002ULL
60 #define	G_RAID3_DEVICE_FLAG_VERIFY	0x0000000000000004ULL
61 #define	G_RAID3_DEVICE_FLAG_NOFAILSYNC	0x0000000000000008ULL
62 #define	G_RAID3_DEVICE_FLAG_MASK	(G_RAID3_DEVICE_FLAG_NOAUTOSYNC | \
63 					 G_RAID3_DEVICE_FLAG_ROUND_ROBIN | \
64 					 G_RAID3_DEVICE_FLAG_VERIFY | \
65 					 G_RAID3_DEVICE_FLAG_NOFAILSYNC)
66 
67 #ifdef _KERNEL
68 extern u_int g_raid3_debug;
69 
70 #define	G_RAID3_DEBUG(lvl, ...)	do {					\
71 	if (g_raid3_debug >= (lvl)) {					\
72 		printf("GEOM_RAID3");					\
73 		if (g_raid3_debug > 0)					\
74 			printf("[%u]", lvl);				\
75 		printf(": ");						\
76 		printf(__VA_ARGS__);					\
77 		printf("\n");						\
78 	}								\
79 } while (0)
80 #define	G_RAID3_LOGREQ(lvl, bp, ...)	do {				\
81 	if (g_raid3_debug >= (lvl)) {					\
82 		printf("GEOM_RAID3");					\
83 		if (g_raid3_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_RAID3_BIO_CFLAG_REGULAR	0x01
94 #define	G_RAID3_BIO_CFLAG_SYNC		0x02
95 #define	G_RAID3_BIO_CFLAG_PARITY	0x04
96 #define	G_RAID3_BIO_CFLAG_NODISK	0x08
97 #define	G_RAID3_BIO_CFLAG_REGSYNC	0x10
98 #define	G_RAID3_BIO_CFLAG_MASK		(G_RAID3_BIO_CFLAG_REGULAR |	\
99 					 G_RAID3_BIO_CFLAG_SYNC |	\
100 					 G_RAID3_BIO_CFLAG_PARITY |	\
101 					 G_RAID3_BIO_CFLAG_NODISK |	\
102 					 G_RAID3_BIO_CFLAG_REGSYNC)
103 
104 #define	G_RAID3_BIO_PFLAG_DEGRADED	0x01
105 #define	G_RAID3_BIO_PFLAG_NOPARITY	0x02
106 #define	G_RAID3_BIO_PFLAG_VERIFY	0x04
107 #define	G_RAID3_BIO_PFLAG_MASK		(G_RAID3_BIO_PFLAG_DEGRADED |	\
108 					 G_RAID3_BIO_PFLAG_NOPARITY |	\
109 					 G_RAID3_BIO_PFLAG_VERIFY)
110 
111 /*
112  * Informations needed for synchronization.
113  */
114 struct g_raid3_disk_sync {
115 	struct g_consumer *ds_consumer;	/* Consumer connected to our device. */
116 	off_t		  ds_offset;	/* Offset of next request to send. */
117 	off_t		  ds_offset_done; /* Offset of already synchronized
118 					   region. */
119 	off_t		  ds_resync;	/* Resynchronize from this offset. */
120 	u_int		  ds_syncid;	/* Disk's synchronization ID. */
121 	u_int		  ds_inflight;	/* Number of in-flight sync requests. */
122 	struct bio	**ds_bios;	/* BIOs for synchronization I/O. */
123 };
124 
125 /*
126  * Informations needed for synchronization.
127  */
128 struct g_raid3_device_sync {
129 	struct g_geom	*ds_geom;	/* Synchronization geom. */
130 };
131 
132 #define	G_RAID3_DISK_STATE_NODISK		0
133 #define	G_RAID3_DISK_STATE_NONE			1
134 #define	G_RAID3_DISK_STATE_NEW			2
135 #define	G_RAID3_DISK_STATE_ACTIVE		3
136 #define	G_RAID3_DISK_STATE_STALE		4
137 #define	G_RAID3_DISK_STATE_SYNCHRONIZING	5
138 #define	G_RAID3_DISK_STATE_DISCONNECTED		6
139 #define	G_RAID3_DISK_STATE_DESTROY		7
140 struct g_raid3_disk {
141 	u_int		 d_no;		/* Disk number. */
142 	struct g_consumer *d_consumer;	/* Consumer. */
143 	struct g_raid3_softc *d_softc;	/* Back-pointer to softc. */
144 	int		 d_state;	/* Disk state. */
145 	uint64_t	 d_flags;	/* Additional flags. */
146 	u_int		 d_genid;	/* Disk's generation ID. */
147 	struct g_raid3_disk_sync d_sync; /* Sync information. */
148 	LIST_ENTRY(g_raid3_disk) d_next;
149 };
150 #define	d_name	d_consumer->provider->name
151 
152 #define	G_RAID3_EVENT_DONTWAIT	0x1
153 #define	G_RAID3_EVENT_WAIT	0x2
154 #define	G_RAID3_EVENT_DEVICE	0x4
155 #define	G_RAID3_EVENT_DONE	0x8
156 struct g_raid3_event {
157 	struct g_raid3_disk	*e_disk;
158 	int			 e_state;
159 	int			 e_flags;
160 	int			 e_error;
161 	TAILQ_ENTRY(g_raid3_event) e_next;
162 };
163 
164 #define	G_RAID3_DEVICE_FLAG_DESTROY	0x0100000000000000ULL
165 #define	G_RAID3_DEVICE_FLAG_WAIT	0x0200000000000000ULL
166 #define	G_RAID3_DEVICE_FLAG_DESTROYING	0x0400000000000000ULL
167 
168 #define	G_RAID3_DEVICE_STATE_STARTING		0
169 #define	G_RAID3_DEVICE_STATE_DEGRADED		1
170 #define	G_RAID3_DEVICE_STATE_COMPLETE		2
171 
172 /* Bump syncid on first write. */
173 #define	G_RAID3_BUMP_SYNCID	0x1
174 /* Bump genid immediately. */
175 #define	G_RAID3_BUMP_GENID	0x2
176 
177 enum g_raid3_zones {
178 	G_RAID3_ZONE_64K,
179 	G_RAID3_ZONE_16K,
180 	G_RAID3_ZONE_4K,
181 	G_RAID3_NUM_ZONES
182 };
183 
184 static __inline enum g_raid3_zones
185 g_raid3_zone(size_t nbytes) {
186 	if (nbytes > 65536)
187 		return (G_RAID3_NUM_ZONES);
188 	else if (nbytes > 16384)
189 		return (G_RAID3_ZONE_64K);
190 	else if (nbytes > 4096)
191 		return (G_RAID3_ZONE_16K);
192 	else
193 		return (G_RAID3_ZONE_4K);
194 };
195 
196 struct g_raid3_softc {
197 	u_int		sc_state;	/* Device state. */
198 	uint64_t	sc_mediasize;	/* Device size. */
199 	uint32_t	sc_sectorsize;	/* Sector size. */
200 	uint64_t	sc_flags;	/* Additional flags. */
201 
202 	struct g_geom	*sc_geom;
203 	struct g_provider *sc_provider;
204 
205 	uint32_t	sc_id;		/* Device unique ID. */
206 
207 	struct sx	 sc_lock;
208 	struct bio_queue_head sc_queue;
209 	struct mtx	 sc_queue_mtx;
210 	struct proc	*sc_worker;
211 	struct bio_queue_head sc_regular_delayed; /* Delayed I/O requests due
212 						     collision with sync
213 						     requests. */
214 	struct bio_queue_head sc_inflight; /* In-flight regular write
215 					      requests. */
216 	struct bio_queue_head sc_sync_delayed; /* Delayed sync requests due
217 						  collision with regular
218 						  requests. */
219 
220 	struct g_raid3_disk *sc_disks;
221 	u_int		sc_ndisks;	/* Number of disks. */
222 	u_int		sc_round_robin;
223 	struct g_raid3_disk *sc_syncdisk;
224 
225 	struct g_raid3_zone {
226 		uma_zone_t	sz_zone;
227 		size_t		sz_inuse;
228 		size_t		sz_max;
229 		u_int		sz_requested;
230 		u_int		sz_failed;
231 	} sc_zones[G_RAID3_NUM_ZONES];
232 
233 	u_int		sc_genid;	/* Generation ID. */
234 	u_int		sc_syncid;	/* Synchronization ID. */
235 	int		sc_bump_id;
236 	struct g_raid3_device_sync sc_sync;
237 	int		sc_idle;	/* DIRTY flags removed. */
238 	time_t		sc_last_write;
239 	u_int		sc_writes;
240 
241 	TAILQ_HEAD(, g_raid3_event) sc_events;
242 	struct mtx	sc_events_mtx;
243 
244 	struct callout	sc_callout;
245 
246 	struct root_hold_token *sc_rootmount;
247 };
248 #define	sc_name	sc_geom->name
249 
250 const char *g_raid3_get_diskname(struct g_raid3_disk *disk);
251 u_int g_raid3_ndisks(struct g_raid3_softc *sc, int state);
252 #define	G_RAID3_DESTROY_SOFT	0
253 #define	G_RAID3_DESTROY_DELAYED	1
254 #define	G_RAID3_DESTROY_HARD	2
255 int g_raid3_destroy(struct g_raid3_softc *sc, int how);
256 int g_raid3_event_send(void *arg, int state, int flags);
257 struct g_raid3_metadata;
258 int g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
259     struct g_raid3_metadata *md);
260 int g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md);
261 void g_raid3_fill_metadata(struct g_raid3_disk *disk,
262     struct g_raid3_metadata *md);
263 int g_raid3_clear_metadata(struct g_raid3_disk *disk);
264 void g_raid3_update_metadata(struct g_raid3_disk *disk);
265 
266 g_ctl_req_t g_raid3_config;
267 #endif	/* _KERNEL */
268 
269 struct g_raid3_metadata {
270 	char		md_magic[16];	/* Magic value. */
271 	uint32_t	md_version;	/* Version number. */
272 	char		md_name[16];	/* Device name. */
273 	uint32_t	md_id;		/* Device unique ID. */
274 	uint16_t	md_no;		/* Component number. */
275 	uint16_t	md_all;		/* Number of disks in device. */
276 	uint32_t	md_genid;	/* Generation ID. */
277 	uint32_t	md_syncid;	/* Synchronization ID. */
278 	uint64_t	md_mediasize;	/* Size of whole device. */
279 	uint32_t	md_sectorsize;	/* Sector size. */
280 	uint64_t	md_sync_offset;	/* Synchronized offset. */
281 	uint64_t	md_mflags;	/* Additional device flags. */
282 	uint64_t	md_dflags;	/* Additional disk flags. */
283 	char		md_provider[16]; /* Hardcoded provider. */
284 	uint64_t	md_provsize;	/* Provider's size. */
285 	u_char		md_hash[16];	/* MD5 hash. */
286 };
287 static __inline void
288 raid3_metadata_encode(struct g_raid3_metadata *md, u_char *data)
289 {
290 	MD5_CTX ctx;
291 
292 	bcopy(md->md_magic, data, 16);
293 	le32enc(data + 16, md->md_version);
294 	bcopy(md->md_name, data + 20, 16);
295 	le32enc(data + 36, md->md_id);
296 	le16enc(data + 40, md->md_no);
297 	le16enc(data + 42, md->md_all);
298 	le32enc(data + 44, md->md_genid);
299 	le32enc(data + 48, md->md_syncid);
300 	le64enc(data + 52, md->md_mediasize);
301 	le32enc(data + 60, md->md_sectorsize);
302 	le64enc(data + 64, md->md_sync_offset);
303 	le64enc(data + 72, md->md_mflags);
304 	le64enc(data + 80, md->md_dflags);
305 	bcopy(md->md_provider, data + 88, 16);
306 	le64enc(data + 104, md->md_provsize);
307 	MD5Init(&ctx);
308 	MD5Update(&ctx, data, 112);
309 	MD5Final(md->md_hash, &ctx);
310 	bcopy(md->md_hash, data + 112, 16);
311 }
312 static __inline int
313 raid3_metadata_decode_v0v1v2(const u_char *data, struct g_raid3_metadata *md)
314 {
315 	MD5_CTX ctx;
316 
317 	bcopy(data + 20, md->md_name, 16);
318 	md->md_id = le32dec(data + 36);
319 	md->md_no = le16dec(data + 40);
320 	md->md_all = le16dec(data + 42);
321 	md->md_syncid = le32dec(data + 44);
322 	md->md_mediasize = le64dec(data + 48);
323 	md->md_sectorsize = le32dec(data + 56);
324 	md->md_sync_offset = le64dec(data + 60);
325 	md->md_mflags = le64dec(data + 68);
326 	md->md_dflags = le64dec(data + 76);
327 	bcopy(data + 84, md->md_provider, 16);
328 	bcopy(data + 100, md->md_hash, 16);
329 	MD5Init(&ctx);
330 	MD5Update(&ctx, data, 100);
331 	MD5Final(md->md_hash, &ctx);
332 	if (bcmp(md->md_hash, data + 100, 16) != 0)
333 		return (EINVAL);
334 
335 	/* New fields. */
336 	md->md_genid = 0;
337 	md->md_provsize = 0;
338 
339 	return (0);
340 }
341 static __inline int
342 raid3_metadata_decode_v3(const u_char *data, struct g_raid3_metadata *md)
343 {
344 	MD5_CTX ctx;
345 
346 	bcopy(data + 20, md->md_name, 16);
347 	md->md_id = le32dec(data + 36);
348 	md->md_no = le16dec(data + 40);
349 	md->md_all = le16dec(data + 42);
350 	md->md_genid = le32dec(data + 44);
351 	md->md_syncid = le32dec(data + 48);
352 	md->md_mediasize = le64dec(data + 52);
353 	md->md_sectorsize = le32dec(data + 60);
354 	md->md_sync_offset = le64dec(data + 64);
355 	md->md_mflags = le64dec(data + 72);
356 	md->md_dflags = le64dec(data + 80);
357 	bcopy(data + 88, md->md_provider, 16);
358 	bcopy(data + 104, md->md_hash, 16);
359 	MD5Init(&ctx);
360 	MD5Update(&ctx, data, 104);
361 	MD5Final(md->md_hash, &ctx);
362 	if (bcmp(md->md_hash, data + 104, 16) != 0)
363 		return (EINVAL);
364 
365 	/* New fields. */
366 	md->md_provsize = 0;
367 
368 	return (0);
369 }
370 static __inline int
371 raid3_metadata_decode_v4v5(const u_char *data, struct g_raid3_metadata *md)
372 {
373 	MD5_CTX ctx;
374 
375 	bcopy(data + 20, md->md_name, 16);
376 	md->md_id = le32dec(data + 36);
377 	md->md_no = le16dec(data + 40);
378 	md->md_all = le16dec(data + 42);
379 	md->md_genid = le32dec(data + 44);
380 	md->md_syncid = le32dec(data + 48);
381 	md->md_mediasize = le64dec(data + 52);
382 	md->md_sectorsize = le32dec(data + 60);
383 	md->md_sync_offset = le64dec(data + 64);
384 	md->md_mflags = le64dec(data + 72);
385 	md->md_dflags = le64dec(data + 80);
386 	bcopy(data + 88, md->md_provider, 16);
387 	md->md_provsize = le64dec(data + 104);
388 	bcopy(data + 112, md->md_hash, 16);
389 	MD5Init(&ctx);
390 	MD5Update(&ctx, data, 112);
391 	MD5Final(md->md_hash, &ctx);
392 	if (bcmp(md->md_hash, data + 112, 16) != 0)
393 		return (EINVAL);
394 	return (0);
395 }
396 static __inline int
397 raid3_metadata_decode(const u_char *data, struct g_raid3_metadata *md)
398 {
399 	int error;
400 
401 	bcopy(data, md->md_magic, 16);
402 	md->md_version = le32dec(data + 16);
403 	switch (md->md_version) {
404 	case 0:
405 	case 1:
406 	case 2:
407 		error = raid3_metadata_decode_v0v1v2(data, md);
408 		break;
409 	case 3:
410 		error = raid3_metadata_decode_v3(data, md);
411 		break;
412 	case 4:
413 	case 5:
414 		error = raid3_metadata_decode_v4v5(data, md);
415 		break;
416 	default:
417 		error = EINVAL;
418 		break;
419 	}
420 	return (error);
421 }
422 
423 static __inline void
424 raid3_metadata_dump(const struct g_raid3_metadata *md)
425 {
426 	static const char hex[] = "0123456789abcdef";
427 	char hash[16 * 2 + 1];
428 	u_int i;
429 
430 	printf("     magic: %s\n", md->md_magic);
431 	printf("   version: %u\n", (u_int)md->md_version);
432 	printf("      name: %s\n", md->md_name);
433 	printf("        id: %u\n", (u_int)md->md_id);
434 	printf("        no: %u\n", (u_int)md->md_no);
435 	printf("       all: %u\n", (u_int)md->md_all);
436 	printf("     genid: %u\n", (u_int)md->md_genid);
437 	printf("    syncid: %u\n", (u_int)md->md_syncid);
438 	printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
439 	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
440 	printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
441 	printf("    mflags:");
442 	if (md->md_mflags == 0)
443 		printf(" NONE");
444 	else {
445 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0)
446 			printf(" NOAUTOSYNC");
447 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0)
448 			printf(" ROUND-ROBIN");
449 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0)
450 			printf(" VERIFY");
451 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
452 			printf(" NOFAILSYNC");
453 	}
454 	printf("\n");
455 	printf("    dflags:");
456 	if (md->md_dflags == 0)
457 		printf(" NONE");
458 	else {
459 		if ((md->md_dflags & G_RAID3_DISK_FLAG_DIRTY) != 0)
460 			printf(" DIRTY");
461 		if ((md->md_dflags & G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0)
462 			printf(" SYNCHRONIZING");
463 		if ((md->md_dflags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0)
464 			printf(" FORCE_SYNC");
465 	}
466 	printf("\n");
467 	printf("hcprovider: %s\n", md->md_provider);
468 	printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
469 	bzero(hash, sizeof(hash));
470 	for (i = 0; i < 16; i++) {
471 		hash[i * 2] = hex[md->md_hash[i] >> 4];
472 		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
473 	}
474 	printf("  MD5 hash: %s\n", hash);
475 }
476 #endif	/* !_G_RAID3_H_ */
477