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