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