xref: /freebsd/sys/geom/raid3/g_raid3.h (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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 > 16384)
187 		return (G_RAID3_ZONE_64K);
188 	else if (nbytes > 4096)
189 		return (G_RAID3_ZONE_16K);
190 	else
191 		return (G_RAID3_ZONE_4K);
192 };
193 
194 struct g_raid3_softc {
195 	u_int		sc_state;	/* Device state. */
196 	uint64_t	sc_mediasize;	/* Device size. */
197 	uint32_t	sc_sectorsize;	/* Sector size. */
198 	uint64_t	sc_flags;	/* Additional flags. */
199 
200 	struct g_geom	*sc_geom;
201 	struct g_provider *sc_provider;
202 
203 	uint32_t	sc_id;		/* Device unique ID. */
204 
205 	struct sx	 sc_lock;
206 	struct bio_queue_head sc_queue;
207 	struct mtx	 sc_queue_mtx;
208 	struct proc	*sc_worker;
209 	struct bio_queue_head sc_regular_delayed; /* Delayed I/O requests due
210 						     collision with sync
211 						     requests. */
212 	struct bio_queue_head sc_inflight; /* In-flight regular write
213 					      requests. */
214 	struct bio_queue_head sc_sync_delayed; /* Delayed sync requests due
215 						  collision with regular
216 						  requests. */
217 
218 	struct g_raid3_disk *sc_disks;
219 	u_int		sc_ndisks;	/* Number of disks. */
220 	u_int		sc_round_robin;
221 	struct g_raid3_disk *sc_syncdisk;
222 
223 	struct g_raid3_zone {
224 		uma_zone_t	sz_zone;
225 		size_t		sz_inuse;
226 		size_t		sz_max;
227 		u_int		sz_requested;
228 		u_int		sz_failed;
229 	} sc_zones[G_RAID3_NUM_ZONES];
230 
231 	u_int		sc_genid;	/* Generation ID. */
232 	u_int		sc_syncid;	/* Synchronization ID. */
233 	int		sc_bump_id;
234 	struct g_raid3_device_sync sc_sync;
235 	int		sc_idle;	/* DIRTY flags removed. */
236 	time_t		sc_last_write;
237 	u_int		sc_writes;
238 
239 	TAILQ_HEAD(, g_raid3_event) sc_events;
240 	struct mtx	sc_events_mtx;
241 
242 	struct callout	sc_callout;
243 
244 	struct root_hold_token *sc_rootmount;
245 };
246 #define	sc_name	sc_geom->name
247 
248 const char *g_raid3_get_diskname(struct g_raid3_disk *disk);
249 u_int g_raid3_ndisks(struct g_raid3_softc *sc, int state);
250 #define	G_RAID3_DESTROY_SOFT	0
251 #define	G_RAID3_DESTROY_DELAYED	1
252 #define	G_RAID3_DESTROY_HARD	2
253 int g_raid3_destroy(struct g_raid3_softc *sc, int how);
254 int g_raid3_event_send(void *arg, int state, int flags);
255 struct g_raid3_metadata;
256 int g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
257     struct g_raid3_metadata *md);
258 int g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md);
259 void g_raid3_fill_metadata(struct g_raid3_disk *disk,
260     struct g_raid3_metadata *md);
261 int g_raid3_clear_metadata(struct g_raid3_disk *disk);
262 void g_raid3_update_metadata(struct g_raid3_disk *disk);
263 
264 g_ctl_req_t g_raid3_config;
265 #endif	/* _KERNEL */
266 
267 struct g_raid3_metadata {
268 	char		md_magic[16];	/* Magic value. */
269 	uint32_t	md_version;	/* Version number. */
270 	char		md_name[16];	/* Device name. */
271 	uint32_t	md_id;		/* Device unique ID. */
272 	uint16_t	md_no;		/* Component number. */
273 	uint16_t	md_all;		/* Number of disks in device. */
274 	uint32_t	md_genid;	/* Generation ID. */
275 	uint32_t	md_syncid;	/* Synchronization ID. */
276 	uint64_t	md_mediasize;	/* Size of whole device. */
277 	uint32_t	md_sectorsize;	/* Sector size. */
278 	uint64_t	md_sync_offset;	/* Synchronized offset. */
279 	uint64_t	md_mflags;	/* Additional device flags. */
280 	uint64_t	md_dflags;	/* Additional disk flags. */
281 	char		md_provider[16]; /* Hardcoded provider. */
282 	uint64_t	md_provsize;	/* Provider's size. */
283 	u_char		md_hash[16];	/* MD5 hash. */
284 };
285 static __inline void
286 raid3_metadata_encode(struct g_raid3_metadata *md, u_char *data)
287 {
288 	MD5_CTX ctx;
289 
290 	bcopy(md->md_magic, data, 16);
291 	le32enc(data + 16, md->md_version);
292 	bcopy(md->md_name, data + 20, 16);
293 	le32enc(data + 36, md->md_id);
294 	le16enc(data + 40, md->md_no);
295 	le16enc(data + 42, md->md_all);
296 	le32enc(data + 44, md->md_genid);
297 	le32enc(data + 48, md->md_syncid);
298 	le64enc(data + 52, md->md_mediasize);
299 	le32enc(data + 60, md->md_sectorsize);
300 	le64enc(data + 64, md->md_sync_offset);
301 	le64enc(data + 72, md->md_mflags);
302 	le64enc(data + 80, md->md_dflags);
303 	bcopy(md->md_provider, data + 88, 16);
304 	le64enc(data + 104, md->md_provsize);
305 	MD5Init(&ctx);
306 	MD5Update(&ctx, data, 112);
307 	MD5Final(md->md_hash, &ctx);
308 	bcopy(md->md_hash, data + 112, 16);
309 }
310 static __inline int
311 raid3_metadata_decode_v0v1v2(const u_char *data, struct g_raid3_metadata *md)
312 {
313 	MD5_CTX ctx;
314 
315 	bcopy(data + 20, md->md_name, 16);
316 	md->md_id = le32dec(data + 36);
317 	md->md_no = le16dec(data + 40);
318 	md->md_all = le16dec(data + 42);
319 	md->md_syncid = le32dec(data + 44);
320 	md->md_mediasize = le64dec(data + 48);
321 	md->md_sectorsize = le32dec(data + 56);
322 	md->md_sync_offset = le64dec(data + 60);
323 	md->md_mflags = le64dec(data + 68);
324 	md->md_dflags = le64dec(data + 76);
325 	bcopy(data + 84, md->md_provider, 16);
326 	bcopy(data + 100, md->md_hash, 16);
327 	MD5Init(&ctx);
328 	MD5Update(&ctx, data, 100);
329 	MD5Final(md->md_hash, &ctx);
330 	if (bcmp(md->md_hash, data + 100, 16) != 0)
331 		return (EINVAL);
332 
333 	/* New fields. */
334 	md->md_genid = 0;
335 	md->md_provsize = 0;
336 
337 	return (0);
338 }
339 static __inline int
340 raid3_metadata_decode_v3(const u_char *data, struct g_raid3_metadata *md)
341 {
342 	MD5_CTX ctx;
343 
344 	bcopy(data + 20, md->md_name, 16);
345 	md->md_id = le32dec(data + 36);
346 	md->md_no = le16dec(data + 40);
347 	md->md_all = le16dec(data + 42);
348 	md->md_genid = le32dec(data + 44);
349 	md->md_syncid = le32dec(data + 48);
350 	md->md_mediasize = le64dec(data + 52);
351 	md->md_sectorsize = le32dec(data + 60);
352 	md->md_sync_offset = le64dec(data + 64);
353 	md->md_mflags = le64dec(data + 72);
354 	md->md_dflags = le64dec(data + 80);
355 	bcopy(data + 88, md->md_provider, 16);
356 	bcopy(data + 104, md->md_hash, 16);
357 	MD5Init(&ctx);
358 	MD5Update(&ctx, data, 104);
359 	MD5Final(md->md_hash, &ctx);
360 	if (bcmp(md->md_hash, data + 104, 16) != 0)
361 		return (EINVAL);
362 
363 	/* New fields. */
364 	md->md_provsize = 0;
365 
366 	return (0);
367 }
368 static __inline int
369 raid3_metadata_decode_v4v5(const u_char *data, struct g_raid3_metadata *md)
370 {
371 	MD5_CTX ctx;
372 
373 	bcopy(data + 20, md->md_name, 16);
374 	md->md_id = le32dec(data + 36);
375 	md->md_no = le16dec(data + 40);
376 	md->md_all = le16dec(data + 42);
377 	md->md_genid = le32dec(data + 44);
378 	md->md_syncid = le32dec(data + 48);
379 	md->md_mediasize = le64dec(data + 52);
380 	md->md_sectorsize = le32dec(data + 60);
381 	md->md_sync_offset = le64dec(data + 64);
382 	md->md_mflags = le64dec(data + 72);
383 	md->md_dflags = le64dec(data + 80);
384 	bcopy(data + 88, md->md_provider, 16);
385 	md->md_provsize = le64dec(data + 104);
386 	bcopy(data + 112, md->md_hash, 16);
387 	MD5Init(&ctx);
388 	MD5Update(&ctx, data, 112);
389 	MD5Final(md->md_hash, &ctx);
390 	if (bcmp(md->md_hash, data + 112, 16) != 0)
391 		return (EINVAL);
392 	return (0);
393 }
394 static __inline int
395 raid3_metadata_decode(const u_char *data, struct g_raid3_metadata *md)
396 {
397 	int error;
398 
399 	bcopy(data, md->md_magic, 16);
400 	md->md_version = le32dec(data + 16);
401 	switch (md->md_version) {
402 	case 0:
403 	case 1:
404 	case 2:
405 		error = raid3_metadata_decode_v0v1v2(data, md);
406 		break;
407 	case 3:
408 		error = raid3_metadata_decode_v3(data, md);
409 		break;
410 	case 4:
411 	case 5:
412 		error = raid3_metadata_decode_v4v5(data, md);
413 		break;
414 	default:
415 		error = EINVAL;
416 		break;
417 	}
418 	return (error);
419 }
420 
421 static __inline void
422 raid3_metadata_dump(const struct g_raid3_metadata *md)
423 {
424 	static const char hex[] = "0123456789abcdef";
425 	char hash[16 * 2 + 1];
426 	u_int i;
427 
428 	printf("     magic: %s\n", md->md_magic);
429 	printf("   version: %u\n", (u_int)md->md_version);
430 	printf("      name: %s\n", md->md_name);
431 	printf("        id: %u\n", (u_int)md->md_id);
432 	printf("        no: %u\n", (u_int)md->md_no);
433 	printf("       all: %u\n", (u_int)md->md_all);
434 	printf("     genid: %u\n", (u_int)md->md_genid);
435 	printf("    syncid: %u\n", (u_int)md->md_syncid);
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_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0)
444 			printf(" NOAUTOSYNC");
445 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0)
446 			printf(" ROUND-ROBIN");
447 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0)
448 			printf(" VERIFY");
449 		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
450 			printf(" NOFAILSYNC");
451 	}
452 	printf("\n");
453 	printf("    dflags:");
454 	if (md->md_dflags == 0)
455 		printf(" NONE");
456 	else {
457 		if ((md->md_dflags & G_RAID3_DISK_FLAG_DIRTY) != 0)
458 			printf(" DIRTY");
459 		if ((md->md_dflags & G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0)
460 			printf(" SYNCHRONIZING");
461 		if ((md->md_dflags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0)
462 			printf(" FORCE_SYNC");
463 	}
464 	printf("\n");
465 	printf("hcprovider: %s\n", md->md_provider);
466 	printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
467 	bzero(hash, sizeof(hash));
468 	for (i = 0; i < 16; i++) {
469 		hash[i * 2] = hex[md->md_hash[i] >> 4];
470 		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
471 	}
472 	printf("  MD5 hash: %s\n", hash);
473 }
474 #endif	/* !_G_RAID3_H_ */
475