source: mainline/uspace/srv/bd/hr/raid1.c@ 8a65373

Last change on this file since 8a65373 was 8a65373, checked in by Miroslav Cimerman <mc@…>, 7 months ago

hr: move registering out of specific RAIDs

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