1 /*- 2 * Copyright (c) 2010 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$"); 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/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/mutex.h> 38 #include <sys/systm.h> 39 #include <geom/geom.h> 40 #include "geom/raid/g_raid.h" 41 #include "g_raid_tr_if.h" 42 43 static MALLOC_DEFINE(M_TR_RAID0, "tr_raid0_data", "GEOM_RAID RAID0 data"); 44 45 struct g_raid_tr_raid0_object { 46 struct g_raid_tr_object trso_base; 47 int trso_starting; 48 int trso_stopped; 49 }; 50 51 static g_raid_tr_taste_t g_raid_tr_taste_raid0; 52 static g_raid_tr_event_t g_raid_tr_event_raid0; 53 static g_raid_tr_start_t g_raid_tr_start_raid0; 54 static g_raid_tr_stop_t g_raid_tr_stop_raid0; 55 static g_raid_tr_iostart_t g_raid_tr_iostart_raid0; 56 static g_raid_tr_iodone_t g_raid_tr_iodone_raid0; 57 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid0; 58 static g_raid_tr_free_t g_raid_tr_free_raid0; 59 60 static kobj_method_t g_raid_tr_raid0_methods[] = { 61 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_raid0), 62 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_raid0), 63 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_raid0), 64 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_raid0), 65 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_raid0), 66 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_raid0), 67 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid0), 68 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_raid0), 69 { 0, 0 } 70 }; 71 72 static struct g_raid_tr_class g_raid_tr_raid0_class = { 73 "RAID0", 74 g_raid_tr_raid0_methods, 75 sizeof(struct g_raid_tr_raid0_object), 76 .trc_priority = 100 77 }; 78 79 static int 80 g_raid_tr_taste_raid0(struct g_raid_tr_object *tr, struct g_raid_volume *volume) 81 { 82 struct g_raid_tr_raid0_object *trs; 83 84 trs = (struct g_raid_tr_raid0_object *)tr; 85 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID0 || 86 tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_NONE) 87 return (G_RAID_TR_TASTE_FAIL); 88 trs->trso_starting = 1; 89 return (G_RAID_TR_TASTE_SUCCEED); 90 } 91 92 static int 93 g_raid_tr_update_state_raid0(struct g_raid_volume *vol) 94 { 95 struct g_raid_tr_raid0_object *trs; 96 struct g_raid_softc *sc; 97 u_int s; 98 int n, f; 99 100 sc = vol->v_softc; 101 trs = (struct g_raid_tr_raid0_object *)vol->v_tr; 102 if (trs->trso_stopped) 103 s = G_RAID_VOLUME_S_STOPPED; 104 else if (trs->trso_starting) 105 s = G_RAID_VOLUME_S_STARTING; 106 else { 107 n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE); 108 f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED); 109 if (n + f == vol->v_disks_count) { 110 if (f == 0) 111 s = G_RAID_VOLUME_S_OPTIMAL; 112 else 113 s = G_RAID_VOLUME_S_SUBOPTIMAL; 114 } else 115 s = G_RAID_VOLUME_S_BROKEN; 116 } 117 if (s != vol->v_state) { 118 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ? 119 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN, 120 G_RAID_EVENT_VOLUME); 121 g_raid_change_volume_state(vol, s); 122 if (!trs->trso_starting && !trs->trso_stopped) 123 g_raid_write_metadata(sc, vol, NULL, NULL); 124 } 125 return (0); 126 } 127 128 static int 129 g_raid_tr_event_raid0(struct g_raid_tr_object *tr, 130 struct g_raid_subdisk *sd, u_int event) 131 { 132 struct g_raid_tr_raid0_object *trs; 133 struct g_raid_softc *sc; 134 struct g_raid_volume *vol; 135 int state; 136 137 trs = (struct g_raid_tr_raid0_object *)tr; 138 vol = tr->tro_volume; 139 sc = vol->v_softc; 140 141 state = sd->sd_state; 142 if (state != G_RAID_SUBDISK_S_NONE && 143 state != G_RAID_SUBDISK_S_FAILED && 144 state != G_RAID_SUBDISK_S_ACTIVE) { 145 G_RAID_DEBUG1(1, sc, 146 "Promote subdisk %s:%d from %s to ACTIVE.", 147 vol->v_name, sd->sd_pos, 148 g_raid_subdisk_state2str(sd->sd_state)); 149 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE); 150 } 151 if (state != sd->sd_state && 152 !trs->trso_starting && !trs->trso_stopped) 153 g_raid_write_metadata(sc, vol, sd, NULL); 154 g_raid_tr_update_state_raid0(vol); 155 return (0); 156 } 157 158 static int 159 g_raid_tr_start_raid0(struct g_raid_tr_object *tr) 160 { 161 struct g_raid_tr_raid0_object *trs; 162 struct g_raid_volume *vol; 163 164 trs = (struct g_raid_tr_raid0_object *)tr; 165 vol = tr->tro_volume; 166 trs->trso_starting = 0; 167 g_raid_tr_update_state_raid0(vol); 168 return (0); 169 } 170 171 static int 172 g_raid_tr_stop_raid0(struct g_raid_tr_object *tr) 173 { 174 struct g_raid_tr_raid0_object *trs; 175 struct g_raid_volume *vol; 176 177 trs = (struct g_raid_tr_raid0_object *)tr; 178 vol = tr->tro_volume; 179 trs->trso_starting = 0; 180 trs->trso_stopped = 1; 181 g_raid_tr_update_state_raid0(vol); 182 return (0); 183 } 184 185 static void 186 g_raid_tr_iostart_raid0(struct g_raid_tr_object *tr, struct bio *bp) 187 { 188 struct g_raid_volume *vol; 189 struct g_raid_subdisk *sd; 190 struct bio_queue_head queue; 191 struct bio *cbp; 192 char *addr; 193 off_t offset, start, length, nstripe, remain; 194 u_int no, strip_size; 195 196 vol = tr->tro_volume; 197 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL && 198 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) { 199 g_raid_iodone(bp, EIO); 200 return; 201 } 202 if (bp->bio_cmd == BIO_FLUSH) { 203 g_raid_tr_flush_common(tr, bp); 204 return; 205 } 206 addr = bp->bio_data; 207 strip_size = vol->v_strip_size; 208 209 /* Stripe number. */ 210 nstripe = bp->bio_offset / strip_size; 211 /* Start position in stripe. */ 212 start = bp->bio_offset % strip_size; 213 /* Disk number. */ 214 no = nstripe % vol->v_disks_count; 215 /* Stripe start position in disk. */ 216 offset = (nstripe / vol->v_disks_count) * strip_size; 217 /* Length of data to operate. */ 218 remain = bp->bio_length; 219 220 bioq_init(&queue); 221 do { 222 length = MIN(strip_size - start, remain); 223 cbp = g_clone_bio(bp); 224 if (cbp == NULL) 225 goto failure; 226 cbp->bio_offset = offset + start; 227 cbp->bio_data = addr; 228 cbp->bio_length = length; 229 cbp->bio_caller1 = &vol->v_subdisks[no]; 230 bioq_insert_tail(&queue, cbp); 231 if (++no >= vol->v_disks_count) { 232 no = 0; 233 offset += strip_size; 234 } 235 remain -= length; 236 addr += length; 237 start = 0; 238 } while (remain > 0); 239 for (cbp = bioq_first(&queue); cbp != NULL; 240 cbp = bioq_first(&queue)) { 241 bioq_remove(&queue, cbp); 242 sd = cbp->bio_caller1; 243 cbp->bio_caller1 = NULL; 244 g_raid_subdisk_iostart(sd, cbp); 245 } 246 return; 247 failure: 248 for (cbp = bioq_first(&queue); cbp != NULL; 249 cbp = bioq_first(&queue)) { 250 bioq_remove(&queue, cbp); 251 g_destroy_bio(cbp); 252 } 253 if (bp->bio_error == 0) 254 bp->bio_error = ENOMEM; 255 g_raid_iodone(bp, bp->bio_error); 256 } 257 258 static int 259 g_raid_tr_kerneldump_raid0(struct g_raid_tr_object *tr, 260 void *virtual, vm_offset_t physical, off_t boffset, size_t blength) 261 { 262 struct g_raid_volume *vol; 263 char *addr; 264 off_t offset, start, length, nstripe, remain; 265 u_int no, strip_size; 266 int error; 267 268 vol = tr->tro_volume; 269 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL) 270 return (ENXIO); 271 addr = virtual; 272 strip_size = vol->v_strip_size; 273 274 /* Stripe number. */ 275 nstripe = boffset / strip_size; 276 /* Start position in stripe. */ 277 start = boffset % strip_size; 278 /* Disk number. */ 279 no = nstripe % vol->v_disks_count; 280 /* Stripe tart position in disk. */ 281 offset = (nstripe / vol->v_disks_count) * strip_size; 282 /* Length of data to operate. */ 283 remain = blength; 284 285 do { 286 length = MIN(strip_size - start, remain); 287 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no], 288 addr, 0, offset + start, length); 289 if (error != 0) 290 return (error); 291 if (++no >= vol->v_disks_count) { 292 no = 0; 293 offset += strip_size; 294 } 295 remain -= length; 296 addr += length; 297 start = 0; 298 } while (remain > 0); 299 return (0); 300 } 301 302 static void 303 g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr, 304 struct g_raid_subdisk *sd,struct bio *bp) 305 { 306 struct bio *pbp; 307 308 pbp = bp->bio_parent; 309 if (pbp->bio_error == 0) 310 pbp->bio_error = bp->bio_error; 311 g_destroy_bio(bp); 312 pbp->bio_inbed++; 313 if (pbp->bio_children == pbp->bio_inbed) { 314 pbp->bio_completed = pbp->bio_length; 315 g_raid_iodone(pbp, bp->bio_error); 316 } 317 } 318 319 static int 320 g_raid_tr_free_raid0(struct g_raid_tr_object *tr) 321 { 322 323 return (0); 324 } 325 326 G_RAID_TR_DECLARE(g_raid_tr_raid0); 327