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