1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _G_RAID3_H_ 32 #define _G_RAID3_H_ 33 34 #include <sys/endian.h> 35 #include <sys/md5.h> 36 37 #define G_RAID3_CLASS_NAME "RAID3" 38 39 #define G_RAID3_MAGIC "GEOM::RAID3" 40 /* 41 * Version history: 42 * 0 - Initial version number. 43 * 1 - Added 'round-robin reading' algorithm. 44 * 2 - Added 'verify reading' algorithm. 45 * 3 - Added md_genid field to metadata. 46 * 4 - Added md_provsize field to metadata. 47 * 5 - Added 'no failure synchronization' flag. 48 */ 49 #define G_RAID3_VERSION 5 50 51 #define G_RAID3_DISK_FLAG_DIRTY 0x0000000000000001ULL 52 #define G_RAID3_DISK_FLAG_SYNCHRONIZING 0x0000000000000002ULL 53 #define G_RAID3_DISK_FLAG_FORCE_SYNC 0x0000000000000004ULL 54 #define G_RAID3_DISK_FLAG_HARDCODED 0x0000000000000008ULL 55 #define G_RAID3_DISK_FLAG_BROKEN 0x0000000000000010ULL 56 #define G_RAID3_DISK_FLAG_MASK (G_RAID3_DISK_FLAG_DIRTY | \ 57 G_RAID3_DISK_FLAG_SYNCHRONIZING | \ 58 G_RAID3_DISK_FLAG_FORCE_SYNC) 59 60 #define G_RAID3_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL 61 #define G_RAID3_DEVICE_FLAG_ROUND_ROBIN 0x0000000000000002ULL 62 #define G_RAID3_DEVICE_FLAG_VERIFY 0x0000000000000004ULL 63 #define G_RAID3_DEVICE_FLAG_NOFAILSYNC 0x0000000000000008ULL 64 #define G_RAID3_DEVICE_FLAG_MASK (G_RAID3_DEVICE_FLAG_NOAUTOSYNC | \ 65 G_RAID3_DEVICE_FLAG_ROUND_ROBIN | \ 66 G_RAID3_DEVICE_FLAG_VERIFY | \ 67 G_RAID3_DEVICE_FLAG_NOFAILSYNC) 68 69 #ifdef _KERNEL 70 extern u_int g_raid3_debug; 71 72 #define G_RAID3_DEBUG(lvl, ...) \ 73 _GEOM_DEBUG("GEOM_RAID3", g_raid3_debug, (lvl), NULL, __VA_ARGS__) 74 #define G_RAID3_LOGREQ(lvl, bp, ...) \ 75 _GEOM_DEBUG("GEOM_RAID3", g_raid3_debug, (lvl), (bp), __VA_ARGS__) 76 77 #define G_RAID3_BIO_CFLAG_REGULAR 0x01 78 #define G_RAID3_BIO_CFLAG_SYNC 0x02 79 #define G_RAID3_BIO_CFLAG_PARITY 0x04 80 #define G_RAID3_BIO_CFLAG_NODISK 0x08 81 #define G_RAID3_BIO_CFLAG_REGSYNC 0x10 82 #define G_RAID3_BIO_CFLAG_MASK (G_RAID3_BIO_CFLAG_REGULAR | \ 83 G_RAID3_BIO_CFLAG_SYNC | \ 84 G_RAID3_BIO_CFLAG_PARITY | \ 85 G_RAID3_BIO_CFLAG_NODISK | \ 86 G_RAID3_BIO_CFLAG_REGSYNC) 87 88 #define G_RAID3_BIO_PFLAG_DEGRADED 0x01 89 #define G_RAID3_BIO_PFLAG_NOPARITY 0x02 90 #define G_RAID3_BIO_PFLAG_VERIFY 0x04 91 #define G_RAID3_BIO_PFLAG_MASK (G_RAID3_BIO_PFLAG_DEGRADED | \ 92 G_RAID3_BIO_PFLAG_NOPARITY | \ 93 G_RAID3_BIO_PFLAG_VERIFY) 94 95 /* 96 * Informations needed for synchronization. 97 */ 98 struct g_raid3_disk_sync { 99 struct g_consumer *ds_consumer; /* Consumer connected to our device. */ 100 off_t ds_offset; /* Offset of next request to send. */ 101 off_t ds_offset_done; /* Offset of already synchronized 102 region. */ 103 off_t ds_resync; /* Resynchronize from this offset. */ 104 u_int ds_syncid; /* Disk's synchronization ID. */ 105 u_int ds_inflight; /* Number of in-flight sync requests. */ 106 struct bio **ds_bios; /* BIOs for synchronization I/O. */ 107 }; 108 109 /* 110 * Informations needed for synchronization. 111 */ 112 struct g_raid3_device_sync { 113 struct g_geom *ds_geom; /* Synchronization geom. */ 114 }; 115 116 #define G_RAID3_DISK_STATE_NODISK 0 117 #define G_RAID3_DISK_STATE_NONE 1 118 #define G_RAID3_DISK_STATE_NEW 2 119 #define G_RAID3_DISK_STATE_ACTIVE 3 120 #define G_RAID3_DISK_STATE_STALE 4 121 #define G_RAID3_DISK_STATE_SYNCHRONIZING 5 122 #define G_RAID3_DISK_STATE_DISCONNECTED 6 123 #define G_RAID3_DISK_STATE_DESTROY 7 124 struct g_raid3_disk { 125 u_int d_no; /* Disk number. */ 126 struct g_consumer *d_consumer; /* Consumer. */ 127 struct g_raid3_softc *d_softc; /* Back-pointer to softc. */ 128 int d_state; /* Disk state. */ 129 uint64_t d_flags; /* Additional flags. */ 130 u_int d_genid; /* Disk's generation ID. */ 131 struct g_raid3_disk_sync d_sync; /* Sync information. */ 132 LIST_ENTRY(g_raid3_disk) d_next; 133 }; 134 #define d_name d_consumer->provider->name 135 136 #define G_RAID3_EVENT_DONTWAIT 0x1 137 #define G_RAID3_EVENT_WAIT 0x2 138 #define G_RAID3_EVENT_DEVICE 0x4 139 #define G_RAID3_EVENT_DONE 0x8 140 struct g_raid3_event { 141 struct g_raid3_disk *e_disk; 142 int e_state; 143 int e_flags; 144 int e_error; 145 TAILQ_ENTRY(g_raid3_event) e_next; 146 }; 147 148 #define G_RAID3_DEVICE_FLAG_DESTROY 0x0100000000000000ULL 149 #define G_RAID3_DEVICE_FLAG_WAIT 0x0200000000000000ULL 150 #define G_RAID3_DEVICE_FLAG_DESTROYING 0x0400000000000000ULL 151 152 #define G_RAID3_DEVICE_STATE_STARTING 0 153 #define G_RAID3_DEVICE_STATE_DEGRADED 1 154 #define G_RAID3_DEVICE_STATE_COMPLETE 2 155 156 /* Bump syncid on first write. */ 157 #define G_RAID3_BUMP_SYNCID 0x1 158 /* Bump genid immediately. */ 159 #define G_RAID3_BUMP_GENID 0x2 160 161 enum g_raid3_zones { 162 G_RAID3_ZONE_64K, 163 G_RAID3_ZONE_16K, 164 G_RAID3_ZONE_4K, 165 G_RAID3_NUM_ZONES 166 }; 167 168 static __inline enum g_raid3_zones 169 g_raid3_zone(size_t nbytes) { 170 if (nbytes > 65536) 171 return (G_RAID3_NUM_ZONES); 172 else if (nbytes > 16384) 173 return (G_RAID3_ZONE_64K); 174 else if (nbytes > 4096) 175 return (G_RAID3_ZONE_16K); 176 else 177 return (G_RAID3_ZONE_4K); 178 }; 179 180 struct g_raid3_softc { 181 u_int sc_state; /* Device state. */ 182 uint64_t sc_mediasize; /* Device size. */ 183 uint32_t sc_sectorsize; /* Sector size. */ 184 uint64_t sc_flags; /* Additional flags. */ 185 186 struct g_geom *sc_geom; 187 struct g_provider *sc_provider; 188 189 uint32_t sc_id; /* Device unique ID. */ 190 191 struct sx sc_lock; 192 struct bio_queue_head sc_queue; 193 struct mtx sc_queue_mtx; 194 struct proc *sc_worker; 195 struct bio_queue_head sc_regular_delayed; /* Delayed I/O requests due 196 collision with sync 197 requests. */ 198 struct bio_queue_head sc_inflight; /* In-flight regular write 199 requests. */ 200 struct bio_queue_head sc_sync_delayed; /* Delayed sync requests due 201 collision with regular 202 requests. */ 203 204 struct g_raid3_disk *sc_disks; 205 u_int sc_ndisks; /* Number of disks. */ 206 u_int sc_round_robin; 207 struct g_raid3_disk *sc_syncdisk; 208 209 struct g_raid3_zone { 210 uma_zone_t sz_zone; 211 size_t sz_inuse; 212 size_t sz_max; 213 u_int sz_requested; 214 u_int sz_failed; 215 } sc_zones[G_RAID3_NUM_ZONES]; 216 217 u_int sc_genid; /* Generation ID. */ 218 u_int sc_syncid; /* Synchronization ID. */ 219 int sc_bump_id; 220 struct g_raid3_device_sync sc_sync; 221 int sc_idle; /* DIRTY flags removed. */ 222 time_t sc_last_write; 223 u_int sc_writes; 224 225 TAILQ_HEAD(, g_raid3_event) sc_events; 226 struct mtx sc_events_mtx; 227 struct g_raid3_event *sc_timeout_event; 228 229 struct callout sc_callout; 230 231 struct root_hold_token *sc_rootmount; 232 }; 233 #define sc_name sc_geom->name 234 235 const char *g_raid3_get_diskname(struct g_raid3_disk *disk); 236 u_int g_raid3_ndisks(struct g_raid3_softc *sc, int state); 237 #define G_RAID3_DESTROY_SOFT 0 238 #define G_RAID3_DESTROY_DELAYED 1 239 #define G_RAID3_DESTROY_HARD 2 240 int g_raid3_destroy(struct g_raid3_softc *sc, int how); 241 int g_raid3_event_send(void *arg, int state, int flags); 242 struct g_raid3_metadata; 243 int g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp, 244 struct g_raid3_metadata *md); 245 int g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md); 246 void g_raid3_fill_metadata(struct g_raid3_disk *disk, 247 struct g_raid3_metadata *md); 248 int g_raid3_clear_metadata(struct g_raid3_disk *disk); 249 void g_raid3_update_metadata(struct g_raid3_disk *disk); 250 251 g_ctl_req_t g_raid3_config; 252 #endif /* _KERNEL */ 253 254 struct g_raid3_metadata { 255 char md_magic[16]; /* Magic value. */ 256 uint32_t md_version; /* Version number. */ 257 char md_name[16]; /* Device name. */ 258 uint32_t md_id; /* Device unique ID. */ 259 uint16_t md_no; /* Component number. */ 260 uint16_t md_all; /* Number of disks in device. */ 261 uint32_t md_genid; /* Generation ID. */ 262 uint32_t md_syncid; /* Synchronization ID. */ 263 uint64_t md_mediasize; /* Size of whole device. */ 264 uint32_t md_sectorsize; /* Sector size. */ 265 uint64_t md_sync_offset; /* Synchronized offset. */ 266 uint64_t md_mflags; /* Additional device flags. */ 267 uint64_t md_dflags; /* Additional disk flags. */ 268 char md_provider[16]; /* Hardcoded provider. */ 269 uint64_t md_provsize; /* Provider's size. */ 270 u_char md_hash[16]; /* MD5 hash. */ 271 }; 272 static __inline void 273 raid3_metadata_encode(struct g_raid3_metadata *md, u_char *data) 274 { 275 MD5_CTX ctx; 276 277 bcopy(md->md_magic, data, 16); 278 le32enc(data + 16, md->md_version); 279 bcopy(md->md_name, data + 20, 16); 280 le32enc(data + 36, md->md_id); 281 le16enc(data + 40, md->md_no); 282 le16enc(data + 42, md->md_all); 283 le32enc(data + 44, md->md_genid); 284 le32enc(data + 48, md->md_syncid); 285 le64enc(data + 52, md->md_mediasize); 286 le32enc(data + 60, md->md_sectorsize); 287 le64enc(data + 64, md->md_sync_offset); 288 le64enc(data + 72, md->md_mflags); 289 le64enc(data + 80, md->md_dflags); 290 bcopy(md->md_provider, data + 88, 16); 291 le64enc(data + 104, md->md_provsize); 292 MD5Init(&ctx); 293 MD5Update(&ctx, data, 112); 294 MD5Final(md->md_hash, &ctx); 295 bcopy(md->md_hash, data + 112, 16); 296 } 297 static __inline int 298 raid3_metadata_decode_v0v1v2(const u_char *data, struct g_raid3_metadata *md) 299 { 300 MD5_CTX ctx; 301 302 bcopy(data + 20, md->md_name, 16); 303 md->md_id = le32dec(data + 36); 304 md->md_no = le16dec(data + 40); 305 md->md_all = le16dec(data + 42); 306 md->md_syncid = le32dec(data + 44); 307 md->md_mediasize = le64dec(data + 48); 308 md->md_sectorsize = le32dec(data + 56); 309 md->md_sync_offset = le64dec(data + 60); 310 md->md_mflags = le64dec(data + 68); 311 md->md_dflags = le64dec(data + 76); 312 bcopy(data + 84, md->md_provider, 16); 313 bcopy(data + 100, md->md_hash, 16); 314 MD5Init(&ctx); 315 MD5Update(&ctx, data, 100); 316 MD5Final(md->md_hash, &ctx); 317 if (bcmp(md->md_hash, data + 100, 16) != 0) 318 return (EINVAL); 319 320 /* New fields. */ 321 md->md_genid = 0; 322 md->md_provsize = 0; 323 324 return (0); 325 } 326 static __inline int 327 raid3_metadata_decode_v3(const u_char *data, struct g_raid3_metadata *md) 328 { 329 MD5_CTX ctx; 330 331 bcopy(data + 20, md->md_name, 16); 332 md->md_id = le32dec(data + 36); 333 md->md_no = le16dec(data + 40); 334 md->md_all = le16dec(data + 42); 335 md->md_genid = le32dec(data + 44); 336 md->md_syncid = le32dec(data + 48); 337 md->md_mediasize = le64dec(data + 52); 338 md->md_sectorsize = le32dec(data + 60); 339 md->md_sync_offset = le64dec(data + 64); 340 md->md_mflags = le64dec(data + 72); 341 md->md_dflags = le64dec(data + 80); 342 bcopy(data + 88, md->md_provider, 16); 343 bcopy(data + 104, md->md_hash, 16); 344 MD5Init(&ctx); 345 MD5Update(&ctx, data, 104); 346 MD5Final(md->md_hash, &ctx); 347 if (bcmp(md->md_hash, data + 104, 16) != 0) 348 return (EINVAL); 349 350 /* New fields. */ 351 md->md_provsize = 0; 352 353 return (0); 354 } 355 static __inline int 356 raid3_metadata_decode_v4v5(const u_char *data, struct g_raid3_metadata *md) 357 { 358 MD5_CTX ctx; 359 360 bcopy(data + 20, md->md_name, 16); 361 md->md_id = le32dec(data + 36); 362 md->md_no = le16dec(data + 40); 363 md->md_all = le16dec(data + 42); 364 md->md_genid = le32dec(data + 44); 365 md->md_syncid = le32dec(data + 48); 366 md->md_mediasize = le64dec(data + 52); 367 md->md_sectorsize = le32dec(data + 60); 368 md->md_sync_offset = le64dec(data + 64); 369 md->md_mflags = le64dec(data + 72); 370 md->md_dflags = le64dec(data + 80); 371 bcopy(data + 88, md->md_provider, 16); 372 md->md_provsize = le64dec(data + 104); 373 bcopy(data + 112, md->md_hash, 16); 374 MD5Init(&ctx); 375 MD5Update(&ctx, data, 112); 376 MD5Final(md->md_hash, &ctx); 377 if (bcmp(md->md_hash, data + 112, 16) != 0) 378 return (EINVAL); 379 return (0); 380 } 381 static __inline int 382 raid3_metadata_decode(const u_char *data, struct g_raid3_metadata *md) 383 { 384 int error; 385 386 bcopy(data, md->md_magic, 16); 387 md->md_version = le32dec(data + 16); 388 switch (md->md_version) { 389 case 0: 390 case 1: 391 case 2: 392 error = raid3_metadata_decode_v0v1v2(data, md); 393 break; 394 case 3: 395 error = raid3_metadata_decode_v3(data, md); 396 break; 397 case 4: 398 case 5: 399 error = raid3_metadata_decode_v4v5(data, md); 400 break; 401 default: 402 error = EINVAL; 403 break; 404 } 405 return (error); 406 } 407 408 static __inline void 409 raid3_metadata_dump(const struct g_raid3_metadata *md) 410 { 411 static const char hex[] = "0123456789abcdef"; 412 char hash[16 * 2 + 1]; 413 u_int i; 414 415 printf(" magic: %s\n", md->md_magic); 416 printf(" version: %u\n", (u_int)md->md_version); 417 printf(" name: %s\n", md->md_name); 418 printf(" id: %u\n", (u_int)md->md_id); 419 printf(" no: %u\n", (u_int)md->md_no); 420 printf(" all: %u\n", (u_int)md->md_all); 421 printf(" genid: %u\n", (u_int)md->md_genid); 422 printf(" syncid: %u\n", (u_int)md->md_syncid); 423 printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize); 424 printf("sectorsize: %u\n", (u_int)md->md_sectorsize); 425 printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset); 426 printf(" mflags:"); 427 if (md->md_mflags == 0) 428 printf(" NONE"); 429 else { 430 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0) 431 printf(" NOAUTOSYNC"); 432 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) 433 printf(" ROUND-ROBIN"); 434 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0) 435 printf(" VERIFY"); 436 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0) 437 printf(" NOFAILSYNC"); 438 } 439 printf("\n"); 440 printf(" dflags:"); 441 if (md->md_dflags == 0) 442 printf(" NONE"); 443 else { 444 if ((md->md_dflags & G_RAID3_DISK_FLAG_DIRTY) != 0) 445 printf(" DIRTY"); 446 if ((md->md_dflags & G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) 447 printf(" SYNCHRONIZING"); 448 if ((md->md_dflags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) 449 printf(" FORCE_SYNC"); 450 } 451 printf("\n"); 452 printf("hcprovider: %s\n", md->md_provider); 453 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize); 454 bzero(hash, sizeof(hash)); 455 for (i = 0; i < 16; i++) { 456 hash[i * 2] = hex[md->md_hash[i] >> 4]; 457 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 458 } 459 printf(" MD5 hash: %s\n", hash); 460 } 461 #endif /* !_G_RAID3_H_ */ 462