tr_raid5.c revision 254271
1/*- 2 * Copyright (c) 2012 Alexander Motin <mav@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 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/geom/raid/tr_raid5.c 254271 2013-08-13 06:25:34Z mav $"); 29 30#include <sys/param.h> 31#include <sys/bio.h> 32#include <sys/endian.h> 33#include <sys/kernel.h> 34#include <sys/kobj.h> 35#include <sys/limits.h> 36#include <sys/lock.h> 37#include <sys/malloc.h> 38#include <sys/mutex.h> 39#include <sys/sysctl.h> 40#include <sys/systm.h> 41#include <geom/geom.h> 42#include "geom/raid/g_raid.h" 43#include "g_raid_tr_if.h" 44 45static MALLOC_DEFINE(M_TR_RAID5, "tr_raid5_data", "GEOM_RAID RAID5 data"); 46 47#define TR_RAID5_NONE 0 48#define TR_RAID5_REBUILD 1 49#define TR_RAID5_RESYNC 2 50 51#define TR_RAID5_F_DOING_SOME 0x1 52#define TR_RAID5_F_LOCKED 0x2 53#define TR_RAID5_F_ABORT 0x4 54 55struct g_raid_tr_raid5_object { 56 struct g_raid_tr_object trso_base; 57 int trso_starting; 58 int trso_stopping; 59 int trso_type; 60 int trso_recover_slabs; /* slabs before rest */ 61 int trso_fair_io; 62 int trso_meta_update; 63 int trso_flags; 64 struct g_raid_subdisk *trso_failed_sd; /* like per volume */ 65 void *trso_buffer; /* Buffer space */ 66 struct bio trso_bio; 67}; 68 69static g_raid_tr_taste_t g_raid_tr_taste_raid5; 70static g_raid_tr_event_t g_raid_tr_event_raid5; 71static g_raid_tr_start_t g_raid_tr_start_raid5; 72static g_raid_tr_stop_t g_raid_tr_stop_raid5; 73static g_raid_tr_iostart_t g_raid_tr_iostart_raid5; 74static g_raid_tr_iodone_t g_raid_tr_iodone_raid5; 75static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid5; 76static g_raid_tr_locked_t g_raid_tr_locked_raid5; 77static g_raid_tr_free_t g_raid_tr_free_raid5; 78 79static kobj_method_t g_raid_tr_raid5_methods[] = { 80 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_raid5), 81 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_raid5), 82 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_raid5), 83 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_raid5), 84 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_raid5), 85 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_raid5), 86 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid5), 87 KOBJMETHOD(g_raid_tr_locked, g_raid_tr_locked_raid5), 88 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_raid5), 89 { 0, 0 } 90}; 91 92static struct g_raid_tr_class g_raid_tr_raid5_class = { 93 "RAID5", 94 g_raid_tr_raid5_methods, 95 sizeof(struct g_raid_tr_raid5_object), 96 .trc_enable = 1, 97 .trc_priority = 100 98}; 99 100static int 101g_raid_tr_taste_raid5(struct g_raid_tr_object *tr, struct g_raid_volume *vol) 102{ 103 struct g_raid_tr_raid5_object *trs; 104 u_int qual; 105 106 trs = (struct g_raid_tr_raid5_object *)tr; 107 qual = tr->tro_volume->v_raid_level_qualifier; 108 if (tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID4 && 109 (qual == G_RAID_VOLUME_RLQ_R4P0 || 110 qual == G_RAID_VOLUME_RLQ_R4PN)) { 111 /* RAID4 */ 112 } else if ((tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5 || 113 tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5E || 114 tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5EE || 115 tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5R || 116 tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID6 || 117 tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAIDMDF) && 118 (qual == G_RAID_VOLUME_RLQ_R5RA || 119 qual == G_RAID_VOLUME_RLQ_R5RS || 120 qual == G_RAID_VOLUME_RLQ_R5LA || 121 qual == G_RAID_VOLUME_RLQ_R5LS)) { 122 /* RAID5/5E/5EE/5R/6/MDF */ 123 } else 124 return (G_RAID_TR_TASTE_FAIL); 125 trs->trso_starting = 1; 126 return (G_RAID_TR_TASTE_SUCCEED); 127} 128 129static int 130g_raid_tr_update_state_raid5(struct g_raid_volume *vol, 131 struct g_raid_subdisk *sd) 132{ 133 struct g_raid_tr_raid5_object *trs; 134 struct g_raid_softc *sc; 135 u_int s; 136 int na, ns, nu; 137 138 sc = vol->v_softc; 139 trs = (struct g_raid_tr_raid5_object *)vol->v_tr; 140 if (trs->trso_stopping && 141 (trs->trso_flags & TR_RAID5_F_DOING_SOME) == 0) 142 s = G_RAID_VOLUME_S_STOPPED; 143 else if (trs->trso_starting) 144 s = G_RAID_VOLUME_S_STARTING; 145 else { 146 na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE); 147 ns = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) + 148 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC); 149 nu = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED); 150 if (na == vol->v_disks_count) 151 s = G_RAID_VOLUME_S_OPTIMAL; 152 else if (na + ns == vol->v_disks_count || 153 na + ns + nu == vol->v_disks_count /* XXX: Temporary. */) 154 s = G_RAID_VOLUME_S_SUBOPTIMAL; 155 else if (na == vol->v_disks_count - 1 || 156 na + ns + nu == vol->v_disks_count) 157 s = G_RAID_VOLUME_S_DEGRADED; 158 else 159 s = G_RAID_VOLUME_S_BROKEN; 160 } 161 if (s != vol->v_state) { 162 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ? 163 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN, 164 G_RAID_EVENT_VOLUME); 165 g_raid_change_volume_state(vol, s); 166 if (!trs->trso_starting && !trs->trso_stopping) 167 g_raid_write_metadata(sc, vol, NULL, NULL); 168 } 169 return (0); 170} 171 172static int 173g_raid_tr_event_raid5(struct g_raid_tr_object *tr, 174 struct g_raid_subdisk *sd, u_int event) 175{ 176 177 g_raid_tr_update_state_raid5(tr->tro_volume, sd); 178 return (0); 179} 180 181static int 182g_raid_tr_start_raid5(struct g_raid_tr_object *tr) 183{ 184 struct g_raid_tr_raid5_object *trs; 185 struct g_raid_volume *vol; 186 187 trs = (struct g_raid_tr_raid5_object *)tr; 188 vol = tr->tro_volume; 189 trs->trso_starting = 0; 190 g_raid_tr_update_state_raid5(vol, NULL); 191 return (0); 192} 193 194static int 195g_raid_tr_stop_raid5(struct g_raid_tr_object *tr) 196{ 197 struct g_raid_tr_raid5_object *trs; 198 struct g_raid_volume *vol; 199 200 trs = (struct g_raid_tr_raid5_object *)tr; 201 vol = tr->tro_volume; 202 trs->trso_starting = 0; 203 trs->trso_stopping = 1; 204 g_raid_tr_update_state_raid5(vol, NULL); 205 return (0); 206} 207 208static void 209g_raid_tr_iostart_raid5_read(struct g_raid_tr_object *tr, struct bio *bp) 210{ 211 struct g_raid_volume *vol; 212 struct g_raid_subdisk *sd; 213 struct bio_queue_head queue; 214 struct bio *cbp; 215 char *addr; 216 off_t offset, start, length, nstripe, remain; 217 int no, pno, ddisks, pdisks, protate, pleft; 218 u_int strip_size, lvl, qual; 219 220 vol = tr->tro_volume; 221 addr = bp->bio_data; 222 strip_size = vol->v_strip_size; 223 lvl = tr->tro_volume->v_raid_level; 224 qual = tr->tro_volume->v_raid_level_qualifier; 225 protate = tr->tro_volume->v_rotate_parity; 226 227 /* Stripe number. */ 228 nstripe = bp->bio_offset / strip_size; 229 /* Start position in stripe. */ 230 start = bp->bio_offset % strip_size; 231 /* Number of data and parity disks. */ 232 if (lvl == G_RAID_VOLUME_RL_RAIDMDF) 233 pdisks = tr->tro_volume->v_mdf_pdisks; 234 else if (lvl == G_RAID_VOLUME_RL_RAID5EE || 235 lvl == G_RAID_VOLUME_RL_RAID6) 236 pdisks = 2; 237 else 238 pdisks = 1; 239 ddisks = vol->v_disks_count - pdisks; 240 /* Parity disk number. */ 241 if (lvl == G_RAID_VOLUME_RL_RAID4) { 242 if (qual == 0) /* P0 */ 243 pno = 0; 244 else /* PN */ 245 pno = ddisks; 246 pleft = -1; 247 } else { 248 pno = (nstripe / (ddisks * protate)) % vol->v_disks_count; 249 pleft = protate - (nstripe / ddisks) % protate; 250 if (qual >= 2) { /* PN/Left */ 251 pno = ddisks - pno; 252 if (pno < 0) 253 pno += vol->v_disks_count; 254 } 255 } 256 /* Data disk number. */ 257 no = nstripe % ddisks; 258 if (lvl == G_RAID_VOLUME_RL_RAID4) { 259 if (qual == 0) 260 no += pdisks; 261 } else if (qual & 1) { /* Continuation/Symmetric */ 262 no = (pno + pdisks + no) % vol->v_disks_count; 263 } else if (no >= pno) /* Restart/Asymmetric */ 264 no += pdisks; 265 else 266 no += imax(0, pno + pdisks - vol->v_disks_count); 267 /* Stripe start position in disk. */ 268 offset = (nstripe / ddisks) * strip_size; 269 /* Length of data to operate. */ 270 remain = bp->bio_length; 271 272 bioq_init(&queue); 273 do { 274 length = MIN(strip_size - start, remain); 275 cbp = g_clone_bio(bp); 276 if (cbp == NULL) 277 goto failure; 278 cbp->bio_offset = offset + start; 279 cbp->bio_data = addr; 280 cbp->bio_length = length; 281 cbp->bio_caller1 = &vol->v_subdisks[no]; 282 bioq_insert_tail(&queue, cbp); 283 no++; 284 if (lvl == G_RAID_VOLUME_RL_RAID4) { 285 no %= vol->v_disks_count; 286 if (no == pno) 287 no = (no + pdisks) % vol->v_disks_count; 288 } else if (qual & 1) { /* Continuation/Symmetric */ 289 no %= vol->v_disks_count; 290 if (no == pno) { 291 if ((--pleft) <= 0) { 292 pleft += protate; 293 if (qual < 2) /* P0/Right */ 294 pno++; 295 else /* PN/Left */ 296 pno += vol->v_disks_count - 1; 297 pno %= vol->v_disks_count; 298 } 299 no = (pno + pdisks) % vol->v_disks_count; 300 offset += strip_size; 301 } 302 } else { /* Restart/Asymmetric */ 303 if (no == pno) 304 no += pdisks; 305 if (no >= vol->v_disks_count) { 306 no -= vol->v_disks_count; 307 if ((--pleft) <= 0) { 308 pleft += protate; 309 if (qual < 2) /* P0/Right */ 310 pno++; 311 else /* PN/Left */ 312 pno += vol->v_disks_count - 1; 313 pno %= vol->v_disks_count; 314 } 315 if (no == pno) 316 no += pdisks; 317 else 318 no += imax(0, pno + pdisks - vol->v_disks_count); 319 offset += strip_size; 320 } 321 } 322 remain -= length; 323 addr += length; 324 start = 0; 325 } while (remain > 0); 326 for (cbp = bioq_first(&queue); cbp != NULL; 327 cbp = bioq_first(&queue)) { 328 bioq_remove(&queue, cbp); 329 sd = cbp->bio_caller1; 330 cbp->bio_caller1 = NULL; 331 g_raid_subdisk_iostart(sd, cbp); 332 } 333 return; 334failure: 335 for (cbp = bioq_first(&queue); cbp != NULL; 336 cbp = bioq_first(&queue)) { 337 bioq_remove(&queue, cbp); 338 g_destroy_bio(cbp); 339 } 340 if (bp->bio_error == 0) 341 bp->bio_error = ENOMEM; 342 g_raid_iodone(bp, bp->bio_error); 343} 344 345static void 346g_raid_tr_iostart_raid5(struct g_raid_tr_object *tr, struct bio *bp) 347{ 348 struct g_raid_volume *vol; 349 struct g_raid_tr_raid5_object *trs; 350 351 vol = tr->tro_volume; 352 trs = (struct g_raid_tr_raid5_object *)tr; 353 if (vol->v_state < G_RAID_VOLUME_S_SUBOPTIMAL) { 354 g_raid_iodone(bp, EIO); 355 return; 356 } 357 switch (bp->bio_cmd) { 358 case BIO_READ: 359 g_raid_tr_iostart_raid5_read(tr, bp); 360 break; 361 case BIO_WRITE: 362 case BIO_DELETE: 363 case BIO_FLUSH: 364 g_raid_iodone(bp, ENODEV); 365 break; 366 default: 367 KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)", 368 bp->bio_cmd, vol->v_name)); 369 break; 370 } 371} 372 373static void 374g_raid_tr_iodone_raid5(struct g_raid_tr_object *tr, 375 struct g_raid_subdisk *sd, struct bio *bp) 376{ 377 struct bio *pbp; 378 int error; 379 380 pbp = bp->bio_parent; 381 pbp->bio_inbed++; 382 error = bp->bio_error; 383 g_destroy_bio(bp); 384 if (pbp->bio_children == pbp->bio_inbed) { 385 pbp->bio_completed = pbp->bio_length; 386 g_raid_iodone(pbp, error); 387 } 388} 389 390static int 391g_raid_tr_kerneldump_raid5(struct g_raid_tr_object *tr, 392 void *virtual, vm_offset_t physical, off_t offset, size_t length) 393{ 394 395 return (ENODEV); 396} 397 398static int 399g_raid_tr_locked_raid5(struct g_raid_tr_object *tr, void *argp) 400{ 401 struct bio *bp; 402 struct g_raid_subdisk *sd; 403 404 bp = (struct bio *)argp; 405 sd = (struct g_raid_subdisk *)bp->bio_caller1; 406 g_raid_subdisk_iostart(sd, bp); 407 408 return (0); 409} 410 411static int 412g_raid_tr_free_raid5(struct g_raid_tr_object *tr) 413{ 414 struct g_raid_tr_raid5_object *trs; 415 416 trs = (struct g_raid_tr_raid5_object *)tr; 417 418 if (trs->trso_buffer != NULL) { 419 free(trs->trso_buffer, M_TR_RAID5); 420 trs->trso_buffer = NULL; 421 } 422 return (0); 423} 424 425G_RAID_TR_DECLARE(raid5, "RAID5"); 426