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