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