2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/endian.h>
37 #define G_MIRROR_CLASS_NAME "MIRROR"
39 #define G_MIRROR_MAGIC "GEOM::MIRROR"
42 * 0 - Initial version number.
43 * 1 - Added 'prefer' balance algorithm.
44 * 2 - Added md_genid field to metadata.
45 * 3 - Added md_provsize field to metadata.
46 * 4 - Added 'no failure synchronization' flag.
48 #define G_MIRROR_VERSION 4
50 #define G_MIRROR_BALANCE_NONE 0
51 #define G_MIRROR_BALANCE_ROUND_ROBIN 1
52 #define G_MIRROR_BALANCE_LOAD 2
53 #define G_MIRROR_BALANCE_SPLIT 3
54 #define G_MIRROR_BALANCE_PREFER 4
55 #define G_MIRROR_BALANCE_MIN G_MIRROR_BALANCE_NONE
56 #define G_MIRROR_BALANCE_MAX G_MIRROR_BALANCE_PREFER
58 #define G_MIRROR_DISK_FLAG_DIRTY 0x0000000000000001ULL
59 #define G_MIRROR_DISK_FLAG_SYNCHRONIZING 0x0000000000000002ULL
60 #define G_MIRROR_DISK_FLAG_FORCE_SYNC 0x0000000000000004ULL
61 #define G_MIRROR_DISK_FLAG_INACTIVE 0x0000000000000008ULL
62 #define G_MIRROR_DISK_FLAG_HARDCODED 0x0000000000000010ULL
63 #define G_MIRROR_DISK_FLAG_BROKEN 0x0000000000000020ULL
64 #define G_MIRROR_DISK_FLAG_CANDELETE 0x0000000000000040ULL
66 /* Per-disk flags which are recorded in on-disk metadata. */
67 #define G_MIRROR_DISK_FLAG_MASK (G_MIRROR_DISK_FLAG_DIRTY | \
68 G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
69 G_MIRROR_DISK_FLAG_FORCE_SYNC | \
70 G_MIRROR_DISK_FLAG_INACTIVE | \
71 G_MIRROR_DISK_FLAG_CANDELETE)
73 #define G_MIRROR_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL
74 #define G_MIRROR_DEVICE_FLAG_NOFAILSYNC 0x0000000000000002ULL
76 /* Mirror flags which are recorded in on-disk metadata. */
77 #define G_MIRROR_DEVICE_FLAG_MASK (G_MIRROR_DEVICE_FLAG_NOAUTOSYNC | \
78 G_MIRROR_DEVICE_FLAG_NOFAILSYNC)
81 #define G_MIRROR_DEVICE_FLAG_DESTROY 0x0100000000000000ULL
82 #define G_MIRROR_DEVICE_FLAG_DRAIN 0x0200000000000000ULL
83 #define G_MIRROR_DEVICE_FLAG_CLOSEWAIT 0x0400000000000000ULL
84 #define G_MIRROR_DEVICE_FLAG_TASTING 0x0800000000000000ULL
85 #define G_MIRROR_DEVICE_FLAG_WIPE 0x1000000000000000ULL
87 extern int g_mirror_debug;
89 #define G_MIRROR_DEBUG(lvl, ...) do { \
90 if (g_mirror_debug >= (lvl)) { \
91 printf("GEOM_MIRROR"); \
92 if (g_mirror_debug > 0) \
93 printf("[%u]", lvl); \
95 printf(__VA_ARGS__); \
99 #define G_MIRROR_LOGREQ(lvl, bp, ...) do { \
100 if (g_mirror_debug >= (lvl)) { \
101 printf("GEOM_MIRROR"); \
102 if (g_mirror_debug > 0) \
103 printf("[%u]", lvl); \
105 printf(__VA_ARGS__); \
112 #define G_MIRROR_BIO_FLAG_REGULAR 0x01
113 #define G_MIRROR_BIO_FLAG_SYNC 0x02
116 * Informations needed for synchronization.
118 struct g_mirror_disk_sync {
119 struct g_consumer *ds_consumer; /* Consumer connected to our mirror. */
120 off_t ds_offset; /* Offset of next request to send. */
121 off_t ds_offset_done; /* Offset of already synchronized
123 time_t ds_update_ts; /* Time of last metadata update. */
124 u_int ds_syncid; /* Disk's synchronization ID. */
125 u_int ds_inflight; /* Number of in-flight sync requests. */
126 struct bio **ds_bios; /* BIOs for synchronization I/O. */
130 * Informations needed for synchronization.
132 struct g_mirror_device_sync {
133 struct g_geom *ds_geom; /* Synchronization geom. */
134 u_int ds_ndisks; /* Number of disks in SYNCHRONIZING
138 #define G_MIRROR_DISK_STATE_NONE 0
139 #define G_MIRROR_DISK_STATE_NEW 1
140 #define G_MIRROR_DISK_STATE_ACTIVE 2
141 #define G_MIRROR_DISK_STATE_STALE 3
142 #define G_MIRROR_DISK_STATE_SYNCHRONIZING 4
143 #define G_MIRROR_DISK_STATE_DISCONNECTED 5
144 #define G_MIRROR_DISK_STATE_DESTROY 6
145 struct g_mirror_disk {
146 uint32_t d_id; /* Disk ID. */
147 struct g_consumer *d_consumer; /* Consumer. */
148 struct g_mirror_softc *d_softc; /* Back-pointer to softc. */
149 int d_state; /* Disk state. */
150 u_int d_priority; /* Disk priority. */
151 u_int load; /* Averaged queue length */
152 off_t d_last_offset; /* Last read offset */
153 uint64_t d_flags; /* Additional flags. */
154 u_int d_genid; /* Disk's generation ID. */
155 struct g_mirror_disk_sync d_sync;/* Sync information. */
156 LIST_ENTRY(g_mirror_disk) d_next;
157 u_int d_init_ndisks; /* Initial number of mirror components */
158 uint32_t d_init_slice; /* Initial slice size */
159 uint8_t d_init_balance;/* Initial balance */
160 uint64_t d_init_mediasize;/* Initial mediasize */
162 #define d_name d_consumer->provider->name
164 #define G_MIRROR_EVENT_DONTWAIT 0x1
165 #define G_MIRROR_EVENT_WAIT 0x2
166 #define G_MIRROR_EVENT_DEVICE 0x4
167 #define G_MIRROR_EVENT_DONE 0x8
168 struct g_mirror_event {
169 struct g_mirror_disk *e_disk;
173 TAILQ_ENTRY(g_mirror_event) e_next;
176 #define G_MIRROR_DEVICE_STATE_STARTING 0
177 #define G_MIRROR_DEVICE_STATE_RUNNING 1
179 #define G_MIRROR_TYPE_MANUAL 0
180 #define G_MIRROR_TYPE_AUTOMATIC 1
182 /* Bump syncid on first write. */
183 #define G_MIRROR_BUMP_SYNCID 0x1
184 /* Bump genid immediately. */
185 #define G_MIRROR_BUMP_GENID 0x2
186 /* Bump syncid immediately. */
187 #define G_MIRROR_BUMP_SYNCID_NOW 0x4
188 struct g_mirror_softc {
189 u_int sc_type; /* Device type (manual/automatic). */
190 u_int sc_state; /* Device state. */
191 uint32_t sc_slice; /* Slice size. */
192 uint8_t sc_balance; /* Balance algorithm. */
193 uint64_t sc_mediasize; /* Device size. */
194 uint32_t sc_sectorsize; /* Sector size. */
195 uint64_t sc_flags; /* Additional flags. */
197 struct g_geom *sc_geom;
198 struct g_provider *sc_provider;
199 int sc_provider_open;
201 uint32_t sc_id; /* Mirror unique ID. */
204 struct bio_queue sc_queue;
205 struct mtx sc_queue_mtx;
206 struct proc *sc_worker;
207 struct bio_queue sc_inflight; /* In-flight regular write requests. */
208 struct bio_queue sc_regular_delayed; /* Delayed I/O requests due to
209 collision with sync requests. */
210 struct bio_queue sc_sync_delayed; /* Delayed sync requests due to
211 collision with regular requests. */
213 LIST_HEAD(, g_mirror_disk) sc_disks;
214 u_int sc_ndisks; /* Number of disks. */
215 struct g_mirror_disk *sc_hint;
217 u_int sc_genid; /* Generation ID. */
218 u_int sc_syncid; /* Synchronization ID. */
220 struct g_mirror_device_sync sc_sync;
221 int sc_idle; /* DIRTY flags removed. */
222 time_t sc_last_write;
224 u_int sc_refcnt; /* Number of softc references */
226 TAILQ_HEAD(, g_mirror_event) sc_events;
227 struct mtx sc_events_mtx;
229 struct callout sc_callout;
231 struct root_hold_token *sc_rootmount;
233 struct mtx sc_done_mtx;
235 #define sc_name sc_geom->name
237 struct g_mirror_metadata;
239 u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
240 struct g_geom * g_mirror_create(struct g_class *mp,
241 const struct g_mirror_metadata *md, u_int type);
242 #define G_MIRROR_DESTROY_SOFT 0
243 #define G_MIRROR_DESTROY_DELAYED 1
244 #define G_MIRROR_DESTROY_HARD 2
245 int g_mirror_destroy(struct g_mirror_softc *sc, int how);
246 int g_mirror_event_send(void *arg, int state, int flags);
247 struct g_mirror_metadata;
248 int g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
249 struct g_mirror_metadata *md);
250 int g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md);
251 void g_mirror_fill_metadata(struct g_mirror_softc *sc,
252 struct g_mirror_disk *disk, struct g_mirror_metadata *md);
253 void g_mirror_update_metadata(struct g_mirror_disk *disk);
255 g_ctl_req_t g_mirror_config;
258 struct g_mirror_metadata {
259 char md_magic[16]; /* Magic value. */
260 uint32_t md_version; /* Version number. */
261 char md_name[16]; /* Mirror name. */
262 uint32_t md_mid; /* Mirror unique ID. */
263 uint32_t md_did; /* Disk unique ID. */
264 uint8_t md_all; /* Number of disks in mirror. */
265 uint32_t md_genid; /* Generation ID. */
266 uint32_t md_syncid; /* Synchronization ID. */
267 uint8_t md_priority; /* Disk priority. */
268 uint32_t md_slice; /* Slice size. */
269 uint8_t md_balance; /* Balance type. */
270 uint64_t md_mediasize; /* Size of the smallest
272 uint32_t md_sectorsize; /* Sector size. */
273 uint64_t md_sync_offset; /* Synchronized offset. */
274 uint64_t md_mflags; /* Additional mirror flags. */
275 uint64_t md_dflags; /* Additional disk flags. */
276 char md_provider[16]; /* Hardcoded provider. */
277 uint64_t md_provsize; /* Provider's size. */
278 u_char md_hash[16]; /* MD5 hash. */
281 mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
285 bcopy(md->md_magic, data, 16);
286 le32enc(data + 16, md->md_version);
287 bcopy(md->md_name, data + 20, 16);
288 le32enc(data + 36, md->md_mid);
289 le32enc(data + 40, md->md_did);
290 *(data + 44) = md->md_all;
291 le32enc(data + 45, md->md_genid);
292 le32enc(data + 49, md->md_syncid);
293 *(data + 53) = md->md_priority;
294 le32enc(data + 54, md->md_slice);
295 *(data + 58) = md->md_balance;
296 le64enc(data + 59, md->md_mediasize);
297 le32enc(data + 67, md->md_sectorsize);
298 le64enc(data + 71, md->md_sync_offset);
299 le64enc(data + 79, md->md_mflags);
300 le64enc(data + 87, md->md_dflags);
301 bcopy(md->md_provider, data + 95, 16);
302 le64enc(data + 111, md->md_provsize);
304 MD5Update(&ctx, data, 119);
305 MD5Final(md->md_hash, &ctx);
306 bcopy(md->md_hash, data + 119, 16);
309 mirror_metadata_decode_v0v1(const u_char *data, struct g_mirror_metadata *md)
313 bcopy(data + 20, md->md_name, 16);
314 md->md_mid = le32dec(data + 36);
315 md->md_did = le32dec(data + 40);
316 md->md_all = *(data + 44);
317 md->md_syncid = le32dec(data + 45);
318 md->md_priority = *(data + 49);
319 md->md_slice = le32dec(data + 50);
320 md->md_balance = *(data + 54);
321 md->md_mediasize = le64dec(data + 55);
322 md->md_sectorsize = le32dec(data + 63);
323 md->md_sync_offset = le64dec(data + 67);
324 md->md_mflags = le64dec(data + 75);
325 md->md_dflags = le64dec(data + 83);
326 bcopy(data + 91, md->md_provider, 16);
327 bcopy(data + 107, md->md_hash, 16);
329 MD5Update(&ctx, data, 107);
330 MD5Final(md->md_hash, &ctx);
331 if (bcmp(md->md_hash, data + 107, 16) != 0)
341 mirror_metadata_decode_v2(const u_char *data, struct g_mirror_metadata *md)
345 bcopy(data + 20, md->md_name, 16);
346 md->md_mid = le32dec(data + 36);
347 md->md_did = le32dec(data + 40);
348 md->md_all = *(data + 44);
349 md->md_genid = le32dec(data + 45);
350 md->md_syncid = le32dec(data + 49);
351 md->md_priority = *(data + 53);
352 md->md_slice = le32dec(data + 54);
353 md->md_balance = *(data + 58);
354 md->md_mediasize = le64dec(data + 59);
355 md->md_sectorsize = le32dec(data + 67);
356 md->md_sync_offset = le64dec(data + 71);
357 md->md_mflags = le64dec(data + 79);
358 md->md_dflags = le64dec(data + 87);
359 bcopy(data + 95, md->md_provider, 16);
360 bcopy(data + 111, md->md_hash, 16);
362 MD5Update(&ctx, data, 111);
363 MD5Final(md->md_hash, &ctx);
364 if (bcmp(md->md_hash, data + 111, 16) != 0)
373 mirror_metadata_decode_v3v4(const u_char *data, struct g_mirror_metadata *md)
377 bcopy(data + 20, md->md_name, 16);
378 md->md_mid = le32dec(data + 36);
379 md->md_did = le32dec(data + 40);
380 md->md_all = *(data + 44);
381 md->md_genid = le32dec(data + 45);
382 md->md_syncid = le32dec(data + 49);
383 md->md_priority = *(data + 53);
384 md->md_slice = le32dec(data + 54);
385 md->md_balance = *(data + 58);
386 md->md_mediasize = le64dec(data + 59);
387 md->md_sectorsize = le32dec(data + 67);
388 md->md_sync_offset = le64dec(data + 71);
389 md->md_mflags = le64dec(data + 79);
390 md->md_dflags = le64dec(data + 87);
391 bcopy(data + 95, md->md_provider, 16);
392 md->md_provsize = le64dec(data + 111);
393 bcopy(data + 119, md->md_hash, 16);
395 MD5Update(&ctx, data, 119);
396 MD5Final(md->md_hash, &ctx);
397 if (bcmp(md->md_hash, data + 119, 16) != 0)
402 mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
406 bcopy(data, md->md_magic, 16);
407 md->md_version = le32dec(data + 16);
408 switch (md->md_version) {
411 error = mirror_metadata_decode_v0v1(data, md);
414 error = mirror_metadata_decode_v2(data, md);
418 error = mirror_metadata_decode_v3v4(data, md);
427 static __inline const char *
428 balance_name(u_int balance)
430 static const char *algorithms[] = {
431 [G_MIRROR_BALANCE_NONE] = "none",
432 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
433 [G_MIRROR_BALANCE_LOAD] = "load",
434 [G_MIRROR_BALANCE_SPLIT] = "split",
435 [G_MIRROR_BALANCE_PREFER] = "prefer",
436 [G_MIRROR_BALANCE_MAX + 1] = "unknown"
439 if (balance > G_MIRROR_BALANCE_MAX)
440 balance = G_MIRROR_BALANCE_MAX + 1;
442 return (algorithms[balance]);
446 balance_id(const char *name)
448 static const char *algorithms[] = {
449 [G_MIRROR_BALANCE_NONE] = "none",
450 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
451 [G_MIRROR_BALANCE_LOAD] = "load",
452 [G_MIRROR_BALANCE_SPLIT] = "split",
453 [G_MIRROR_BALANCE_PREFER] = "prefer"
457 for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
458 if (strcmp(name, algorithms[n]) == 0)
465 mirror_metadata_dump(const struct g_mirror_metadata *md)
467 static const char hex[] = "0123456789abcdef";
468 char hash[16 * 2 + 1];
471 printf(" magic: %s\n", md->md_magic);
472 printf(" version: %u\n", (u_int)md->md_version);
473 printf(" name: %s\n", md->md_name);
474 printf(" mid: %u\n", (u_int)md->md_mid);
475 printf(" did: %u\n", (u_int)md->md_did);
476 printf(" all: %u\n", (u_int)md->md_all);
477 printf(" genid: %u\n", (u_int)md->md_genid);
478 printf(" syncid: %u\n", (u_int)md->md_syncid);
479 printf(" priority: %u\n", (u_int)md->md_priority);
480 printf(" slice: %u\n", (u_int)md->md_slice);
481 printf(" balance: %s\n", balance_name((u_int)md->md_balance));
482 printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
483 printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
484 printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
486 if (md->md_mflags == 0)
489 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
490 printf(" NOFAILSYNC");
491 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
492 printf(" NOAUTOSYNC");
496 if (md->md_dflags == 0)
499 if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
501 if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
502 printf(" SYNCHRONIZING");
503 if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
504 printf(" FORCE_SYNC");
505 if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
509 printf("hcprovider: %s\n", md->md_provider);
510 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize);
511 bzero(hash, sizeof(hash));
512 for (i = 0; i < 16; i++) {
513 hash[i * 2] = hex[md->md_hash[i] >> 4];
514 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
516 printf(" MD5 hash: %s\n", hash);
518 #endif /* !_G_MIRROR_H_ */