| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 Miroslav Cimerman
|
|---|
| 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 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup hr
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <bd_srv.h>
|
|---|
| 37 | #include <block.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <hr.h>
|
|---|
| 40 | #include <io/log.h>
|
|---|
| 41 | #include <ipc/hr.h>
|
|---|
| 42 | #include <ipc/services.h>
|
|---|
| 43 | #include <loc.h>
|
|---|
| 44 | #include <task.h>
|
|---|
| 45 | #include <stdatomic.h>
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 | #include <stdlib.h>
|
|---|
| 48 | #include <str_error.h>
|
|---|
| 49 |
|
|---|
| 50 | #include "fge.h"
|
|---|
| 51 | #include "io.h"
|
|---|
| 52 | #include "superblock.h"
|
|---|
| 53 | #include "util.h"
|
|---|
| 54 | #include "var.h"
|
|---|
| 55 |
|
|---|
| 56 | extern loc_srv_t *hr_srv;
|
|---|
| 57 |
|
|---|
| 58 | static void hr_raid1_update_vol_status(hr_volume_t *);
|
|---|
| 59 | static void hr_raid1_ext_state_callback(hr_volume_t *, size_t, errno_t);
|
|---|
| 60 | static size_t hr_raid1_count_good_extents(hr_volume_t *, uint64_t, size_t,
|
|---|
| 61 | uint64_t);
|
|---|
| 62 | static errno_t hr_raid1_bd_op(hr_bd_op_type_t, bd_srv_t *, aoff64_t, size_t,
|
|---|
| 63 | void *, const void *, size_t);
|
|---|
| 64 | static errno_t hr_raid1_rebuild(void *);
|
|---|
| 65 | static errno_t init_rebuild(hr_volume_t *, size_t *);
|
|---|
| 66 | static errno_t swap_hs(hr_volume_t *, size_t, size_t);
|
|---|
| 67 | static errno_t hr_raid1_restore_blocks(hr_volume_t *, size_t, uint64_t, size_t,
|
|---|
| 68 | void *);
|
|---|
| 69 |
|
|---|
| 70 | /* bdops */
|
|---|
| 71 | static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
|
|---|
| 72 | static errno_t hr_raid1_bd_close(bd_srv_t *);
|
|---|
| 73 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
|---|
| 74 | size_t);
|
|---|
| 75 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
|---|
| 76 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
|---|
| 77 | const void *, size_t);
|
|---|
| 78 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
|
|---|
| 79 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
|---|
| 80 |
|
|---|
| 81 | static bd_ops_t hr_raid1_bd_ops = {
|
|---|
| 82 | .open = hr_raid1_bd_open,
|
|---|
| 83 | .close = hr_raid1_bd_close,
|
|---|
| 84 | .sync_cache = hr_raid1_bd_sync_cache,
|
|---|
| 85 | .read_blocks = hr_raid1_bd_read_blocks,
|
|---|
| 86 | .write_blocks = hr_raid1_bd_write_blocks,
|
|---|
| 87 | .get_block_size = hr_raid1_bd_get_block_size,
|
|---|
| 88 | .get_num_blocks = hr_raid1_bd_get_num_blocks
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | errno_t hr_raid1_create(hr_volume_t *new_volume)
|
|---|
| 92 | {
|
|---|
| 93 | errno_t rc;
|
|---|
| 94 |
|
|---|
| 95 | assert(new_volume->level == HR_LVL_1);
|
|---|
| 96 |
|
|---|
| 97 | if (new_volume->extent_no < 2) {
|
|---|
| 98 | HR_ERROR("RAID 1 array needs at least 2 devices\n");
|
|---|
| 99 | return EINVAL;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | bd_srvs_init(&new_volume->hr_bds);
|
|---|
| 103 | new_volume->hr_bds.ops = &hr_raid1_bd_ops;
|
|---|
| 104 | new_volume->hr_bds.sarg = new_volume;
|
|---|
| 105 |
|
|---|
| 106 | /* force volume state update */
|
|---|
| 107 | hr_mark_vol_state_dirty(new_volume);
|
|---|
| 108 | hr_raid1_update_vol_status(new_volume);
|
|---|
| 109 |
|
|---|
| 110 | fibril_rwlock_read_lock(&new_volume->states_lock);
|
|---|
| 111 | hr_vol_status_t state = new_volume->status;
|
|---|
| 112 | fibril_rwlock_read_unlock(&new_volume->states_lock);
|
|---|
| 113 | if (state == HR_VOL_FAULTY || state == HR_VOL_NONE)
|
|---|
| 114 | return EINVAL;
|
|---|
| 115 |
|
|---|
| 116 | rc = hr_register_volume(new_volume);
|
|---|
| 117 |
|
|---|
| 118 | return rc;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | errno_t hr_raid1_init(hr_volume_t *vol)
|
|---|
| 122 | {
|
|---|
| 123 | errno_t rc;
|
|---|
| 124 | size_t bsize;
|
|---|
| 125 | uint64_t total_blkno;
|
|---|
| 126 |
|
|---|
| 127 | assert(vol->level == HR_LVL_1);
|
|---|
| 128 |
|
|---|
| 129 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
|---|
| 130 | if (rc != EOK)
|
|---|
| 131 | return rc;
|
|---|
| 132 |
|
|---|
| 133 | vol->nblocks = total_blkno / vol->extent_no;
|
|---|
| 134 | vol->bsize = bsize;
|
|---|
| 135 | vol->data_offset = HR_DATA_OFF;
|
|---|
| 136 | vol->data_blkno = vol->nblocks - vol->data_offset;
|
|---|
| 137 | vol->strip_size = 0;
|
|---|
| 138 |
|
|---|
| 139 | return EOK;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | void hr_raid1_status_event(hr_volume_t *vol)
|
|---|
| 143 | {
|
|---|
| 144 | hr_raid1_update_vol_status(vol);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | errno_t hr_raid1_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
|
|---|
| 148 | {
|
|---|
| 149 | HR_DEBUG("hr_raid1_add_hotspare()\n");
|
|---|
| 150 |
|
|---|
| 151 | errno_t rc = EOK;
|
|---|
| 152 |
|
|---|
| 153 | fibril_mutex_lock(&vol->hotspare_lock);
|
|---|
| 154 |
|
|---|
| 155 | if (vol->hotspare_no >= HR_MAX_HOTSPARES) {
|
|---|
| 156 | HR_ERROR("hr_raid1_add_hotspare(): cannot add more hotspares "
|
|---|
| 157 | "to \"%s\"\n", vol->devname);
|
|---|
| 158 | rc = ELIMIT;
|
|---|
| 159 | goto error;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | size_t hs_idx = vol->hotspare_no;
|
|---|
| 163 |
|
|---|
| 164 | vol->hotspare_no++;
|
|---|
| 165 |
|
|---|
| 166 | hr_update_hotspare_svc_id(vol, hs_idx, hotspare);
|
|---|
| 167 | hr_update_hotspare_status(vol, hs_idx, HR_EXT_HOTSPARE);
|
|---|
| 168 |
|
|---|
| 169 | hr_mark_vol_state_dirty(vol);
|
|---|
| 170 | error:
|
|---|
| 171 | fibril_mutex_unlock(&vol->hotspare_lock);
|
|---|
| 172 |
|
|---|
| 173 | hr_raid1_update_vol_status(vol);
|
|---|
| 174 |
|
|---|
| 175 | return rc;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
|---|
| 179 | {
|
|---|
| 180 | HR_DEBUG("hr_bd_open()\n");
|
|---|
| 181 | return EOK;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | static errno_t hr_raid1_bd_close(bd_srv_t *bd)
|
|---|
| 185 | {
|
|---|
| 186 | HR_DEBUG("hr_bd_close()\n");
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
|---|
| 191 | {
|
|---|
| 192 | return hr_raid1_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 196 | void *buf, size_t size)
|
|---|
| 197 | {
|
|---|
| 198 | return hr_raid1_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 202 | const void *data, size_t size)
|
|---|
| 203 | {
|
|---|
| 204 | return hr_raid1_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
|---|
| 208 | {
|
|---|
| 209 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 210 |
|
|---|
| 211 | *rsize = vol->bsize;
|
|---|
| 212 | return EOK;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
|---|
| 216 | {
|
|---|
| 217 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 218 |
|
|---|
| 219 | *rnb = vol->data_blkno;
|
|---|
| 220 | return EOK;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | static void hr_raid1_update_vol_status(hr_volume_t *vol)
|
|---|
| 224 | {
|
|---|
| 225 | bool exp = true;
|
|---|
| 226 |
|
|---|
| 227 | /* TODO: could also wrap this */
|
|---|
| 228 | if (!atomic_compare_exchange_strong(&vol->state_dirty, &exp, false))
|
|---|
| 229 | return;
|
|---|
| 230 |
|
|---|
| 231 | fibril_rwlock_read_lock(&vol->extents_lock);
|
|---|
| 232 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 233 |
|
|---|
| 234 | hr_vol_status_t old_state = vol->status;
|
|---|
| 235 | size_t healthy = hr_count_extents(vol, HR_EXT_ONLINE);
|
|---|
| 236 |
|
|---|
| 237 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 238 | fibril_rwlock_read_unlock(&vol->extents_lock);
|
|---|
| 239 |
|
|---|
| 240 | if (healthy == 0) {
|
|---|
| 241 | if (old_state != HR_VOL_FAULTY) {
|
|---|
| 242 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 243 | hr_update_vol_status(vol, HR_VOL_FAULTY);
|
|---|
| 244 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 245 | }
|
|---|
| 246 | } else if (healthy < vol->extent_no) {
|
|---|
| 247 | if (old_state != HR_VOL_REBUILD &&
|
|---|
| 248 | old_state != HR_VOL_DEGRADED) {
|
|---|
| 249 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 250 | hr_update_vol_status(vol, HR_VOL_DEGRADED);
|
|---|
| 251 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | if (old_state != HR_VOL_REBUILD) {
|
|---|
| 255 | if (vol->hotspare_no > 0) {
|
|---|
| 256 | fid_t fib = fibril_create(hr_raid1_rebuild,
|
|---|
| 257 | vol);
|
|---|
| 258 | if (fib == 0)
|
|---|
| 259 | return;
|
|---|
| 260 | fibril_start(fib);
|
|---|
| 261 | fibril_detach(fib);
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 | } else {
|
|---|
| 265 | if (old_state != HR_VOL_ONLINE) {
|
|---|
| 266 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 267 | hr_update_vol_status(vol, HR_VOL_ONLINE);
|
|---|
| 268 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | static void hr_raid1_ext_state_callback(hr_volume_t *vol, size_t extent,
|
|---|
| 274 | errno_t rc)
|
|---|
| 275 | {
|
|---|
| 276 | if (rc == EOK)
|
|---|
| 277 | return;
|
|---|
| 278 |
|
|---|
| 279 | assert(fibril_rwlock_is_locked(&vol->extents_lock));
|
|---|
| 280 |
|
|---|
| 281 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 282 |
|
|---|
| 283 | switch (rc) {
|
|---|
| 284 | case ENOMEM:
|
|---|
| 285 | hr_update_ext_status(vol, extent, HR_EXT_INVALID);
|
|---|
| 286 | break;
|
|---|
| 287 | case ENOENT:
|
|---|
| 288 | hr_update_ext_status(vol, extent, HR_EXT_MISSING);
|
|---|
| 289 | break;
|
|---|
| 290 | default:
|
|---|
| 291 | hr_update_ext_status(vol, extent, HR_EXT_FAILED);
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | hr_mark_vol_state_dirty(vol);
|
|---|
| 295 |
|
|---|
| 296 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | static size_t hr_raid1_count_good_extents(hr_volume_t *vol, uint64_t ba,
|
|---|
| 300 | size_t cnt, uint64_t rebuild_blk)
|
|---|
| 301 | {
|
|---|
| 302 | assert(fibril_rwlock_is_locked(&vol->extents_lock));
|
|---|
| 303 | assert(fibril_rwlock_is_locked(&vol->states_lock));
|
|---|
| 304 |
|
|---|
| 305 | size_t count = 0;
|
|---|
| 306 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 307 | if (vol->extents[i].status == HR_EXT_ONLINE ||
|
|---|
| 308 | (vol->extents[i].status == HR_EXT_REBUILD &&
|
|---|
| 309 | ba < rebuild_blk)) {
|
|---|
| 310 | count++;
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | return count;
|
|---|
| 315 |
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
|---|
| 319 | size_t cnt, void *data_read, const void *data_write, size_t size)
|
|---|
| 320 | {
|
|---|
| 321 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 322 | hr_range_lock_t *rl = NULL;
|
|---|
| 323 | errno_t rc;
|
|---|
| 324 | size_t i;
|
|---|
| 325 | uint64_t rebuild_blk;
|
|---|
| 326 |
|
|---|
| 327 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 328 | hr_vol_status_t vol_state = vol->status;
|
|---|
| 329 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 330 |
|
|---|
| 331 | if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
|
|---|
| 332 | return EIO;
|
|---|
| 333 |
|
|---|
| 334 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
|---|
| 335 | if (size < cnt * vol->bsize)
|
|---|
| 336 | return EINVAL;
|
|---|
| 337 |
|
|---|
| 338 | rc = hr_check_ba_range(vol, cnt, ba);
|
|---|
| 339 | if (rc != EOK)
|
|---|
| 340 | return rc;
|
|---|
| 341 |
|
|---|
| 342 | /* allow full dev sync */
|
|---|
| 343 | if (type != HR_BD_SYNC || ba != 0)
|
|---|
| 344 | hr_add_ba_offset(vol, &ba);
|
|---|
| 345 |
|
|---|
| 346 | /*
|
|---|
| 347 | * extent order has to be locked for the whole IO duration,
|
|---|
| 348 | * so that workers have consistent targets
|
|---|
| 349 | */
|
|---|
| 350 | fibril_rwlock_read_lock(&vol->extents_lock);
|
|---|
| 351 |
|
|---|
| 352 | size_t successful = 0;
|
|---|
| 353 | switch (type) {
|
|---|
| 354 | case HR_BD_READ:
|
|---|
| 355 | rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
|
|---|
| 356 | memory_order_relaxed);
|
|---|
| 357 |
|
|---|
| 358 | for (i = 0; i < vol->extent_no; i++) {
|
|---|
| 359 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 360 | hr_ext_status_t state = vol->extents[i].status;
|
|---|
| 361 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 362 |
|
|---|
| 363 | if (state != HR_EXT_ONLINE &&
|
|---|
| 364 | (state != HR_EXT_REBUILD ||
|
|---|
| 365 | ba + cnt - 1 >= rebuild_blk)) {
|
|---|
| 366 | continue;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | rc = block_read_direct(vol->extents[i].svc_id, ba, cnt,
|
|---|
| 370 | data_read);
|
|---|
| 371 |
|
|---|
| 372 | if (rc == ENOMEM && i + 1 == vol->extent_no)
|
|---|
| 373 | goto end;
|
|---|
| 374 |
|
|---|
| 375 | if (rc == ENOMEM)
|
|---|
| 376 | continue;
|
|---|
| 377 |
|
|---|
| 378 | if (rc != EOK) {
|
|---|
| 379 | hr_raid1_ext_state_callback(vol, i, rc);
|
|---|
| 380 | } else {
|
|---|
| 381 | successful++;
|
|---|
| 382 | break;
|
|---|
| 383 | }
|
|---|
| 384 | }
|
|---|
| 385 | break;
|
|---|
| 386 | case HR_BD_SYNC:
|
|---|
| 387 | case HR_BD_WRITE:
|
|---|
| 388 | if (type == HR_BD_WRITE) {
|
|---|
| 389 | rl = hr_range_lock_acquire(vol, ba, cnt);
|
|---|
| 390 | if (rl == NULL) {
|
|---|
| 391 | rc = ENOMEM;
|
|---|
| 392 | goto end;
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 397 |
|
|---|
| 398 | rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
|
|---|
| 399 | memory_order_relaxed);
|
|---|
| 400 |
|
|---|
| 401 | size_t good = hr_raid1_count_good_extents(vol, ba, cnt,
|
|---|
| 402 | rebuild_blk);
|
|---|
| 403 |
|
|---|
| 404 | hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
|
|---|
| 405 | if (group == NULL) {
|
|---|
| 406 | if (type == HR_BD_WRITE)
|
|---|
| 407 | hr_range_lock_release(rl);
|
|---|
| 408 | rc = ENOMEM;
|
|---|
| 409 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 410 | goto end;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | for (i = 0; i < vol->extent_no; i++) {
|
|---|
| 414 | if (vol->extents[i].status != HR_EXT_ONLINE &&
|
|---|
| 415 | (vol->extents[i].status != HR_EXT_REBUILD ||
|
|---|
| 416 | ba >= rebuild_blk)) {
|
|---|
| 417 | /*
|
|---|
| 418 | * When the extent is being rebuilt,
|
|---|
| 419 | * we only write to the part that is already
|
|---|
| 420 | * rebuilt. If IO starts after vol->rebuild_blk
|
|---|
| 421 | * we do not proceed, the write is going to
|
|---|
| 422 | * be replicated later in the rebuild.
|
|---|
| 423 | */
|
|---|
| 424 | continue;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | hr_io_t *io = hr_fgroup_alloc(group);
|
|---|
| 428 | io->extent = i;
|
|---|
| 429 | io->data_write = data_write;
|
|---|
| 430 | io->data_read = data_read;
|
|---|
| 431 | io->ba = ba;
|
|---|
| 432 | io->cnt = cnt;
|
|---|
| 433 | io->type = type;
|
|---|
| 434 | io->vol = vol;
|
|---|
| 435 | io->state_callback = hr_raid1_ext_state_callback;
|
|---|
| 436 |
|
|---|
| 437 | hr_fgroup_submit(group, hr_io_worker, io);
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 441 |
|
|---|
| 442 | (void)hr_fgroup_wait(group, &successful, NULL);
|
|---|
| 443 |
|
|---|
| 444 | if (type == HR_BD_WRITE)
|
|---|
| 445 | hr_range_lock_release(rl);
|
|---|
| 446 |
|
|---|
| 447 | break;
|
|---|
| 448 | default:
|
|---|
| 449 | rc = EINVAL;
|
|---|
| 450 | goto end;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | if (successful > 0)
|
|---|
| 454 | rc = EOK;
|
|---|
| 455 | else
|
|---|
| 456 | rc = EIO;
|
|---|
| 457 |
|
|---|
| 458 | end:
|
|---|
| 459 | fibril_rwlock_read_unlock(&vol->extents_lock);
|
|---|
| 460 |
|
|---|
| 461 | hr_raid1_update_vol_status(vol);
|
|---|
| 462 |
|
|---|
| 463 | return rc;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | /*
|
|---|
| 467 | * Put the last HOTSPARE extent in place
|
|---|
| 468 | * of first that != ONLINE, and start the rebuild.
|
|---|
| 469 | */
|
|---|
| 470 | static errno_t hr_raid1_rebuild(void *arg)
|
|---|
| 471 | {
|
|---|
| 472 | HR_DEBUG("hr_raid1_rebuild()\n");
|
|---|
| 473 |
|
|---|
| 474 | hr_volume_t *vol = arg;
|
|---|
| 475 | void *buf = NULL;
|
|---|
| 476 | size_t rebuild_idx;
|
|---|
| 477 | errno_t rc;
|
|---|
| 478 |
|
|---|
| 479 | rc = init_rebuild(vol, &rebuild_idx);
|
|---|
| 480 | if (rc != EOK)
|
|---|
| 481 | return rc;
|
|---|
| 482 |
|
|---|
| 483 | size_t left = vol->data_blkno;
|
|---|
| 484 | size_t max_blks = DATA_XFER_LIMIT / vol->bsize;
|
|---|
| 485 | buf = malloc(max_blks * vol->bsize);
|
|---|
| 486 |
|
|---|
| 487 | size_t cnt;
|
|---|
| 488 | uint64_t ba = 0;
|
|---|
| 489 | hr_add_ba_offset(vol, &ba);
|
|---|
| 490 |
|
|---|
| 491 | /*
|
|---|
| 492 | * XXX: this is useless here after simplified DI, because
|
|---|
| 493 | * rebuild cannot be triggered while ongoing rebuild
|
|---|
| 494 | */
|
|---|
| 495 | fibril_rwlock_read_lock(&vol->extents_lock);
|
|---|
| 496 |
|
|---|
| 497 | hr_range_lock_t *rl = NULL;
|
|---|
| 498 |
|
|---|
| 499 | unsigned int percent, old_percent = 100;
|
|---|
| 500 | while (left != 0) {
|
|---|
| 501 | cnt = min(max_blks, left);
|
|---|
| 502 |
|
|---|
| 503 | rl = hr_range_lock_acquire(vol, ba, cnt);
|
|---|
| 504 | if (rl == NULL) {
|
|---|
| 505 | rc = ENOMEM;
|
|---|
| 506 | goto end;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | atomic_store_explicit(&vol->rebuild_blk, ba,
|
|---|
| 510 | memory_order_relaxed);
|
|---|
| 511 |
|
|---|
| 512 | rc = hr_raid1_restore_blocks(vol, rebuild_idx, ba, cnt, buf);
|
|---|
| 513 |
|
|---|
| 514 | percent = ((ba + cnt) * 100) / vol->data_blkno;
|
|---|
| 515 | if (percent != old_percent) {
|
|---|
| 516 | if (percent % 5 == 0)
|
|---|
| 517 | HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
|
|---|
| 518 | vol->devname, percent);
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | hr_range_lock_release(rl);
|
|---|
| 522 |
|
|---|
| 523 | if (rc != EOK)
|
|---|
| 524 | goto end;
|
|---|
| 525 |
|
|---|
| 526 | ba += cnt;
|
|---|
| 527 | left -= cnt;
|
|---|
| 528 | old_percent = percent;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | HR_DEBUG("hr_raid1_rebuild(): rebuild finished on \"%s\" (%lu), "
|
|---|
| 532 | "extent no. %lu\n", vol->devname, vol->svc_id, rebuild_idx);
|
|---|
| 533 |
|
|---|
| 534 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 535 |
|
|---|
| 536 | hr_update_ext_status(vol, rebuild_idx, HR_EXT_ONLINE);
|
|---|
| 537 |
|
|---|
| 538 | /*
|
|---|
| 539 | * We can be optimistic here, if some extents are
|
|---|
| 540 | * still INVALID, FAULTY or MISSING, the update vol
|
|---|
| 541 | * function will pick them up, and set the volume
|
|---|
| 542 | * state accordingly.
|
|---|
| 543 | */
|
|---|
| 544 | hr_update_vol_status(vol, HR_VOL_ONLINE);
|
|---|
| 545 | hr_mark_vol_state_dirty(vol);
|
|---|
| 546 |
|
|---|
| 547 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 548 |
|
|---|
| 549 | /*
|
|---|
| 550 | * For now write metadata at the end, because
|
|---|
| 551 | * we don't sync metada accross extents yet.
|
|---|
| 552 | */
|
|---|
| 553 | hr_write_meta_to_ext(vol, rebuild_idx);
|
|---|
| 554 | end:
|
|---|
| 555 | if (rc != EOK) {
|
|---|
| 556 | /*
|
|---|
| 557 | * We can fail either because:
|
|---|
| 558 | * - the rebuild extent failing or invalidation
|
|---|
| 559 | * - there is are no ONLINE extents (vol is FAULTY)
|
|---|
| 560 | * - we got ENOMEM on all READs (we also invalidate the
|
|---|
| 561 | * rebuild extent here, for now)
|
|---|
| 562 | */
|
|---|
| 563 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 564 | hr_update_vol_status(vol, HR_VOL_DEGRADED);
|
|---|
| 565 | hr_mark_vol_state_dirty(vol);
|
|---|
| 566 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | fibril_rwlock_read_unlock(&vol->extents_lock);
|
|---|
| 570 |
|
|---|
| 571 | hr_raid1_update_vol_status(vol);
|
|---|
| 572 |
|
|---|
| 573 | if (buf != NULL)
|
|---|
| 574 | free(buf);
|
|---|
| 575 |
|
|---|
| 576 | return rc;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | static errno_t init_rebuild(hr_volume_t *vol, size_t *rebuild_idx)
|
|---|
| 580 | {
|
|---|
| 581 | errno_t rc = EOK;
|
|---|
| 582 |
|
|---|
| 583 | fibril_rwlock_write_lock(&vol->extents_lock);
|
|---|
| 584 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 585 | fibril_mutex_lock(&vol->hotspare_lock);
|
|---|
| 586 |
|
|---|
| 587 | if (vol->hotspare_no == 0) {
|
|---|
| 588 | HR_WARN("hr_raid1_rebuild(): no free hotspares on \"%s\", "
|
|---|
| 589 | "aborting rebuild\n", vol->devname);
|
|---|
| 590 | rc = EINVAL;
|
|---|
| 591 | goto error;
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | size_t bad = vol->extent_no;
|
|---|
| 595 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 596 | if (vol->extents[i].status != HR_EXT_ONLINE) {
|
|---|
| 597 | bad = i;
|
|---|
| 598 | break;
|
|---|
| 599 | }
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | if (bad == vol->extent_no) {
|
|---|
| 603 | HR_WARN("hr_raid1_rebuild(): no bad extent on \"%s\", "
|
|---|
| 604 | "aborting rebuild\n", vol->devname);
|
|---|
| 605 | rc = EINVAL;
|
|---|
| 606 | goto error;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | size_t hotspare_idx = vol->hotspare_no - 1;
|
|---|
| 610 |
|
|---|
| 611 | hr_ext_status_t hs_state = vol->hotspares[hotspare_idx].status;
|
|---|
| 612 | if (hs_state != HR_EXT_HOTSPARE) {
|
|---|
| 613 | HR_ERROR("hr_raid1_rebuild(): invalid hotspare state \"%s\", "
|
|---|
| 614 | "aborting rebuild\n", hr_get_ext_status_msg(hs_state));
|
|---|
| 615 | rc = EINVAL;
|
|---|
| 616 | goto error;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | rc = swap_hs(vol, bad, hotspare_idx);
|
|---|
| 620 | if (rc != EOK) {
|
|---|
| 621 | HR_ERROR("hr_raid1_rebuild(): swapping hotspare failed, "
|
|---|
| 622 | "aborting rebuild\n");
|
|---|
| 623 | goto error;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | hr_extent_t *rebuild_ext = &vol->extents[bad];
|
|---|
| 627 |
|
|---|
| 628 | HR_DEBUG("hr_raid1_rebuild(): starting REBUILD on extent no. %lu (%lu)"
|
|---|
| 629 | "\n", bad, rebuild_ext->svc_id);
|
|---|
| 630 |
|
|---|
| 631 | atomic_store_explicit(&vol->rebuild_blk, 0, memory_order_relaxed);
|
|---|
| 632 |
|
|---|
| 633 | hr_update_ext_status(vol, bad, HR_EXT_REBUILD);
|
|---|
| 634 | hr_update_vol_status(vol, HR_VOL_REBUILD);
|
|---|
| 635 |
|
|---|
| 636 | *rebuild_idx = bad;
|
|---|
| 637 | error:
|
|---|
| 638 | fibril_mutex_unlock(&vol->hotspare_lock);
|
|---|
| 639 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 640 | fibril_rwlock_write_unlock(&vol->extents_lock);
|
|---|
| 641 |
|
|---|
| 642 | return rc;
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | static errno_t swap_hs(hr_volume_t *vol, size_t bad, size_t hs)
|
|---|
| 646 | {
|
|---|
| 647 | HR_DEBUG("hr_raid1_rebuild(): swapping in hotspare\n");
|
|---|
| 648 |
|
|---|
| 649 | service_id_t faulty_svc_id = vol->extents[bad].svc_id;
|
|---|
| 650 | service_id_t hs_svc_id = vol->hotspares[hs].svc_id;
|
|---|
| 651 |
|
|---|
| 652 | /* TODO: if rc != EOK, try next hotspare */
|
|---|
| 653 | errno_t rc = block_init(hs_svc_id);
|
|---|
| 654 | if (rc != EOK) {
|
|---|
| 655 | HR_ERROR("hr_raid1_rebuild(): initing hotspare (%lu) failed\n",
|
|---|
| 656 | hs_svc_id);
|
|---|
| 657 | return rc;
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | hr_update_ext_svc_id(vol, bad, hs_svc_id);
|
|---|
| 661 | hr_update_ext_status(vol, bad, HR_EXT_HOTSPARE);
|
|---|
| 662 |
|
|---|
| 663 | hr_update_hotspare_svc_id(vol, hs, 0);
|
|---|
| 664 | hr_update_hotspare_status(vol, hs, HR_EXT_MISSING);
|
|---|
| 665 |
|
|---|
| 666 | vol->hotspare_no--;
|
|---|
| 667 |
|
|---|
| 668 | if (faulty_svc_id != 0)
|
|---|
| 669 | block_fini(faulty_svc_id);
|
|---|
| 670 |
|
|---|
| 671 | return EOK;
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | static errno_t hr_raid1_restore_blocks(hr_volume_t *vol, size_t rebuild_idx,
|
|---|
| 675 | uint64_t ba, size_t cnt, void *buf)
|
|---|
| 676 | {
|
|---|
| 677 | assert(fibril_rwlock_is_locked(&vol->extents_lock));
|
|---|
| 678 |
|
|---|
| 679 | errno_t rc = ENOENT;
|
|---|
| 680 | hr_extent_t *ext, *rebuild_ext = &vol->extents[rebuild_idx];
|
|---|
| 681 |
|
|---|
| 682 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 683 | hr_ext_status_t rebuild_ext_status = rebuild_ext->status;
|
|---|
| 684 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 685 |
|
|---|
| 686 | if (rebuild_ext_status != HR_EXT_REBUILD)
|
|---|
| 687 | return EINVAL;
|
|---|
| 688 |
|
|---|
| 689 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 690 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 691 | ext = &vol->extents[i];
|
|---|
| 692 | if (ext->status != HR_EXT_ONLINE) {
|
|---|
| 693 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 694 | continue;
|
|---|
| 695 | }
|
|---|
| 696 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 697 |
|
|---|
| 698 | rc = block_read_direct(ext->svc_id, ba, cnt, buf);
|
|---|
| 699 | if (rc == EOK)
|
|---|
| 700 | break;
|
|---|
| 701 |
|
|---|
| 702 | if (rc != ENOMEM)
|
|---|
| 703 | hr_raid1_ext_state_callback(vol, i, rc);
|
|---|
| 704 |
|
|---|
| 705 | if (i + 1 >= vol->extent_no) {
|
|---|
| 706 | if (rc != ENOMEM) {
|
|---|
| 707 | HR_ERROR("rebuild on \"%s\" (%lu), failed due "
|
|---|
| 708 | "to too many failed extents\n",
|
|---|
| 709 | vol->devname, vol->svc_id);
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 | /* for now we have to invalidate the rebuild extent */
|
|---|
| 713 | if (rc == ENOMEM) {
|
|---|
| 714 | HR_ERROR("rebuild on \"%s\" (%lu), failed due "
|
|---|
| 715 | "to too many failed reads, because of not "
|
|---|
| 716 | "enough memory\n",
|
|---|
| 717 | vol->devname, vol->svc_id);
|
|---|
| 718 | hr_raid1_ext_state_callback(vol, rebuild_idx,
|
|---|
| 719 | ENOMEM);
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | return rc;
|
|---|
| 723 | }
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | rc = block_write_direct(rebuild_ext->svc_id, ba, cnt, buf);
|
|---|
| 727 | if (rc != EOK) {
|
|---|
| 728 | /*
|
|---|
| 729 | * Here we dont handle ENOMEM, because maybe in the
|
|---|
| 730 | * future, there is going to be M_WAITOK, or we are
|
|---|
| 731 | * going to wait for more memory, so that we don't
|
|---|
| 732 | * have to invalidate it...
|
|---|
| 733 | *
|
|---|
| 734 | * XXX: for now we do
|
|---|
| 735 | */
|
|---|
| 736 | hr_raid1_ext_state_callback(vol, rebuild_idx, rc);
|
|---|
| 737 |
|
|---|
| 738 | HR_ERROR("rebuild on \"%s\" (%lu), failed due to "
|
|---|
| 739 | "the rebuilt extent no. %lu WRITE (rc: %s)\n",
|
|---|
| 740 | vol->devname, vol->svc_id, rebuild_idx, str_error(rc));
|
|---|
| 741 |
|
|---|
| 742 | return rc;
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | return EOK;
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | /** @}
|
|---|
| 749 | */
|
|---|