g_raid3.h revision 139295
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/raid3/g_raid3.h 139295 2004-12-25 19:17:47Z pjd $
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 */
45#define	G_RAID3_VERSION		3
46
47#define	G_RAID3_DISK_FLAG_DIRTY		0x0000000000000001ULL
48#define	G_RAID3_DISK_FLAG_SYNCHRONIZING	0x0000000000000002ULL
49#define	G_RAID3_DISK_FLAG_FORCE_SYNC	0x0000000000000004ULL
50#define	G_RAID3_DISK_FLAG_HARDCODED	0x0000000000000008ULL
51#define	G_RAID3_DISK_FLAG_MASK		(G_RAID3_DISK_FLAG_DIRTY |	\
52					 G_RAID3_DISK_FLAG_SYNCHRONIZING | \
53					 G_RAID3_DISK_FLAG_FORCE_SYNC)
54
55#define	G_RAID3_DEVICE_FLAG_NOAUTOSYNC	0x0000000000000001ULL
56#define	G_RAID3_DEVICE_FLAG_ROUND_ROBIN	0x0000000000000002ULL
57#define	G_RAID3_DEVICE_FLAG_VERIFY	0x0000000000000004ULL
58#define	G_RAID3_DEVICE_FLAG_MASK	(G_RAID3_DEVICE_FLAG_NOAUTOSYNC | \
59					 G_RAID3_DEVICE_FLAG_ROUND_ROBIN | \
60					 G_RAID3_DEVICE_FLAG_VERIFY)
61
62#ifdef _KERNEL
63extern u_int g_raid3_debug;
64
65#define	G_RAID3_DEBUG(lvl, ...)	do {					\
66	if (g_raid3_debug >= (lvl)) {					\
67		printf("GEOM_RAID3");					\
68		if (g_raid3_debug > 0)					\
69			printf("[%u]", lvl);				\
70		printf(": ");						\
71		printf(__VA_ARGS__);					\
72		printf("\n");						\
73	}								\
74} while (0)
75#define	G_RAID3_LOGREQ(lvl, bp, ...)	do {				\
76	if (g_raid3_debug >= (lvl)) {					\
77		printf("GEOM_RAID3");					\
78		if (g_raid3_debug > 0)					\
79			printf("[%u]", lvl);				\
80		printf(": ");						\
81		printf(__VA_ARGS__);					\
82		printf(" ");						\
83		g_print_bio(bp);					\
84		printf("\n");						\
85	}								\
86} while (0)
87
88#define	G_RAID3_BIO_CFLAG_REGULAR	0x01
89#define	G_RAID3_BIO_CFLAG_SYNC		0x02
90#define	G_RAID3_BIO_CFLAG_PARITY	0x04
91#define	G_RAID3_BIO_CFLAG_NODISK	0x08
92#define	G_RAID3_BIO_CFLAG_REGSYNC	0x10
93#define	G_RAID3_BIO_CFLAG_MASK		(G_RAID3_BIO_CFLAG_REGULAR |	\
94					 G_RAID3_BIO_CFLAG_SYNC |	\
95					 G_RAID3_BIO_CFLAG_PARITY |	\
96					 G_RAID3_BIO_CFLAG_NODISK |	\
97					 G_RAID3_BIO_CFLAG_REGSYNC)
98
99#define	G_RAID3_BIO_PFLAG_DEGRADED	0x01
100#define	G_RAID3_BIO_PFLAG_NOPARITY	0x02
101#define	G_RAID3_BIO_PFLAG_VERIFY	0x04
102#define	G_RAID3_BIO_PFLAG_MASK		(G_RAID3_BIO_PFLAG_DEGRADED |	\
103					 G_RAID3_BIO_PFLAG_NOPARITY |	\
104					 G_RAID3_BIO_PFLAG_VERIFY)
105
106/*
107 * Informations needed for synchronization.
108 */
109struct g_raid3_disk_sync {
110	struct g_consumer *ds_consumer;	/* Consumer connected to our device. */
111	off_t		 ds_offset;	/* Offset of next request to send. */
112	off_t		 ds_offset_done; /* Offset of already synchronized
113					   region. */
114	off_t		 ds_resync;	/* Resynchronize from this offset. */
115	u_int		 ds_syncid;	/* Disk's synchronization ID. */
116	u_char		*ds_data;
117};
118
119/*
120 * Informations needed for synchronization.
121 */
122struct g_raid3_device_sync {
123	struct g_geom	*ds_geom;	/* Synchronization geom. */
124};
125
126#define	G_RAID3_DISK_STATE_NODISK		0
127#define	G_RAID3_DISK_STATE_NONE			1
128#define	G_RAID3_DISK_STATE_NEW			2
129#define	G_RAID3_DISK_STATE_ACTIVE		3
130#define	G_RAID3_DISK_STATE_STALE		4
131#define	G_RAID3_DISK_STATE_SYNCHRONIZING	5
132#define	G_RAID3_DISK_STATE_DISCONNECTED		6
133#define	G_RAID3_DISK_STATE_DESTROY		7
134struct g_raid3_disk {
135	u_int		 d_no;		/* Disk number. */
136	struct g_consumer *d_consumer;	/* Consumer. */
137	struct g_raid3_softc *d_softc;	/* Back-pointer to softc. */
138	int		 d_state;	/* Disk state. */
139	uint64_t	 d_flags;	/* Additional flags. */
140	u_int		 d_genid;	/* Disk's generation ID. */
141	struct g_raid3_disk_sync d_sync; /* Sync information. */
142	LIST_ENTRY(g_raid3_disk) d_next;
143};
144#define	d_name	d_consumer->provider->name
145
146#define	G_RAID3_EVENT_DONTWAIT	0x1
147#define	G_RAID3_EVENT_WAIT	0x2
148#define	G_RAID3_EVENT_DEVICE	0x4
149#define	G_RAID3_EVENT_DONE	0x8
150struct g_raid3_event {
151	struct g_raid3_disk	*e_disk;
152	int			 e_state;
153	int			 e_flags;
154	int			 e_error;
155	TAILQ_ENTRY(g_raid3_event) e_next;
156};
157
158#define	G_RAID3_DEVICE_FLAG_DESTROY	0x0100000000000000ULL
159#define	G_RAID3_DEVICE_FLAG_WAIT	0x0200000000000000ULL
160
161#define	G_RAID3_DEVICE_STATE_STARTING		0
162#define	G_RAID3_DEVICE_STATE_DEGRADED		1
163#define	G_RAID3_DEVICE_STATE_COMPLETE		2
164
165/* Bump syncid on first write. */
166#define	G_RAID3_BUMP_SYNCID_OFW	0x1
167/* Bump syncid immediately. */
168#define	G_RAID3_BUMP_SYNCID_IMM	0x2
169#define	G_RAID3_BUMP_SYNCID	(G_RAID3_BUMP_SYNCID_OFW | \
170				 G_RAID3_BUMP_SYNCID_IMM)
171/* Bump genid immediately. */
172#define	G_RAID3_BUMP_GENID_IMM	0x4
173#define	G_RAID3_BUMP_GENID	(G_RAID3_BUMP_GENID_IMM)
174
175struct g_raid3_softc {
176	u_int		sc_state;	/* Device state. */
177	uint64_t	sc_mediasize;	/* Device size. */
178	uint32_t	sc_sectorsize;	/* Sector size. */
179	uint64_t	sc_flags;	/* Additional flags. */
180
181	struct g_geom	*sc_geom;
182	struct g_provider *sc_provider;
183
184	uint32_t	sc_id;		/* Device unique ID. */
185
186	struct bio_queue_head sc_queue;
187	struct mtx	 sc_queue_mtx;
188	struct proc	*sc_worker;
189
190	struct g_raid3_disk *sc_disks;
191	u_int		sc_ndisks;	/* Number of disks. */
192	u_int		sc_round_robin;
193	struct g_raid3_disk *sc_syncdisk;
194
195	uma_zone_t	sc_zone_64k;
196	uma_zone_t	sc_zone_16k;
197	uma_zone_t	sc_zone_4k;
198
199	u_int		sc_genid;	/* Generation ID. */
200	u_int		sc_syncid;	/* Synchronization ID. */
201	int		sc_bump_id;
202	struct g_raid3_device_sync sc_sync;
203	int		sc_idle;	/* DIRTY flags removed. */
204
205	TAILQ_HEAD(, g_raid3_event) sc_events;
206	struct mtx	sc_events_mtx;
207
208	struct callout	sc_callout;
209};
210#define	sc_name	sc_geom->name
211
212const char *g_raid3_get_diskname(struct g_raid3_disk *disk);
213u_int g_raid3_ndisks(struct g_raid3_softc *sc, int state);
214int g_raid3_destroy(struct g_raid3_softc *sc, boolean_t force);
215int g_raid3_event_send(void *arg, int state, int flags);
216struct g_raid3_metadata;
217void g_raid3_fill_metadata(struct g_raid3_disk *disk,
218    struct g_raid3_metadata *md);
219int g_raid3_clear_metadata(struct g_raid3_disk *disk);
220void g_raid3_update_metadata(struct g_raid3_disk *disk);
221
222g_ctl_req_t g_raid3_config;
223#endif	/* _KERNEL */
224
225struct g_raid3_metadata {
226	char		md_magic[16];	/* Magic value. */
227	uint32_t	md_version;	/* Version number. */
228	char		md_name[16];	/* Device name. */
229	uint32_t	md_id;		/* Device unique ID. */
230	uint16_t	md_no;		/* Component number. */
231	uint16_t	md_all;		/* Number of disks in device. */
232	uint32_t	md_genid;	/* Generation ID. */
233	uint32_t	md_syncid;	/* Synchronization ID. */
234	uint64_t	md_mediasize;	/* Size of whole device. */
235	uint32_t	md_sectorsize;	/* Sector size. */
236	uint64_t	md_sync_offset;	/* Synchronized offset. */
237	uint64_t	md_mflags;	/* Additional device flags. */
238	uint64_t	md_dflags;	/* Additional disk flags. */
239	char		md_provider[16]; /* Hardcoded provider. */
240	u_char		md_hash[16];	/* MD5 hash. */
241};
242static __inline void
243raid3_metadata_encode(struct g_raid3_metadata *md, u_char *data)
244{
245	MD5_CTX ctx;
246
247	bcopy(md->md_magic, data, 16);
248	le32enc(data + 16, md->md_version);
249	bcopy(md->md_name, data + 20, 16);
250	le32enc(data + 36, md->md_id);
251	le16enc(data + 40, md->md_no);
252	le16enc(data + 42, md->md_all);
253	le32enc(data + 44, md->md_genid);
254	le32enc(data + 48, md->md_syncid);
255	le64enc(data + 52, md->md_mediasize);
256	le32enc(data + 60, md->md_sectorsize);
257	le64enc(data + 64, md->md_sync_offset);
258	le64enc(data + 72, md->md_mflags);
259	le64enc(data + 80, md->md_dflags);
260	bcopy(md->md_provider, data + 88, 16);
261	MD5Init(&ctx);
262	MD5Update(&ctx, data, 104);
263	MD5Final(md->md_hash, &ctx);
264	bcopy(md->md_hash, data + 104, 16);
265}
266static __inline int
267raid3_metadata_decode_v0v1v2(const u_char *data, struct g_raid3_metadata *md)
268{
269	MD5_CTX ctx;
270
271	bcopy(data + 20, md->md_name, 16);
272	md->md_id = le32dec(data + 36);
273	md->md_no = le16dec(data + 40);
274	md->md_all = le16dec(data + 42);
275	md->md_syncid = le32dec(data + 44);
276	md->md_mediasize = le64dec(data + 48);
277	md->md_sectorsize = le32dec(data + 56);
278	md->md_sync_offset = le64dec(data + 60);
279	md->md_mflags = le64dec(data + 68);
280	md->md_dflags = le64dec(data + 76);
281	bcopy(data + 84, md->md_provider, 16);
282	bcopy(data + 100, md->md_hash, 16);
283	MD5Init(&ctx);
284	MD5Update(&ctx, data, 100);
285	MD5Final(md->md_hash, &ctx);
286	if (bcmp(md->md_hash, data + 100, 16) != 0)
287		return (EINVAL);
288	return (0);
289}
290static __inline int
291raid3_metadata_decode_v3(const u_char *data, struct g_raid3_metadata *md)
292{
293	MD5_CTX ctx;
294
295	bcopy(data + 20, md->md_name, 16);
296	md->md_id = le32dec(data + 36);
297	md->md_no = le16dec(data + 40);
298	md->md_all = le16dec(data + 42);
299	md->md_genid = le32dec(data + 44);
300	md->md_syncid = le32dec(data + 48);
301	md->md_mediasize = le64dec(data + 52);
302	md->md_sectorsize = le32dec(data + 60);
303	md->md_sync_offset = le64dec(data + 64);
304	md->md_mflags = le64dec(data + 72);
305	md->md_dflags = le64dec(data + 80);
306	bcopy(data + 88, md->md_provider, 16);
307	bcopy(data + 104, md->md_hash, 16);
308	MD5Init(&ctx);
309	MD5Update(&ctx, data, 104);
310	MD5Final(md->md_hash, &ctx);
311	if (bcmp(md->md_hash, data + 104, 16) != 0)
312		return (EINVAL);
313	return (0);
314}
315static __inline int
316raid3_metadata_decode(const u_char *data, struct g_raid3_metadata *md)
317{
318	int error;
319
320	bcopy(data, md->md_magic, 16);
321	md->md_version = le32dec(data + 16);
322	switch (md->md_version) {
323	case 0:
324	case 1:
325	case 2:
326		error = raid3_metadata_decode_v0v1v2(data, md);
327		break;
328	case 3:
329		error = raid3_metadata_decode_v3(data, md);
330		break;
331	default:
332		error = EINVAL;
333		break;
334	}
335	return (error);
336}
337
338static __inline void
339raid3_metadata_dump(const struct g_raid3_metadata *md)
340{
341	static const char hex[] = "0123456789abcdef";
342	char hash[16 * 2 + 1];
343	u_int i;
344
345	printf("     magic: %s\n", md->md_magic);
346	printf("   version: %u\n", (u_int)md->md_version);
347	printf("      name: %s\n", md->md_name);
348	printf("        id: %u\n", (u_int)md->md_id);
349	printf("        no: %u\n", (u_int)md->md_no);
350	printf("       all: %u\n", (u_int)md->md_all);
351	printf("     genid: %u\n", (u_int)md->md_genid);
352	printf("    syncid: %u\n", (u_int)md->md_syncid);
353	printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
354	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
355	printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
356	printf("    mflags:");
357	if (md->md_mflags == 0)
358		printf(" NONE");
359	else {
360		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0)
361			printf(" NOAUTOSYNC");
362		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0)
363			printf(" ROUND-ROBIN");
364		if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0)
365			printf(" VERIFY");
366	}
367	printf("\n");
368	printf("    dflags:");
369	if (md->md_dflags == 0)
370		printf(" NONE");
371	else {
372		if ((md->md_dflags & G_RAID3_DISK_FLAG_DIRTY) != 0)
373			printf(" DIRTY");
374		if ((md->md_dflags & G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0)
375			printf(" SYNCHRONIZING");
376		if ((md->md_dflags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0)
377			printf(" FORCE_SYNC");
378	}
379	printf("\n");
380	printf("hcprovider: %s\n", md->md_provider);
381	bzero(hash, sizeof(hash));
382	for (i = 0; i < 16; i++) {
383		hash[i * 2] = hex[md->md_hash[i] >> 4];
384		hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
385	}
386	printf("  MD5 hash: %s\n", hash);
387}
388#endif	/* !_G_RAID3_H_ */
389