source: mainline/uspace/srv/bd/hr/raid1.c@ 263a2389

Last change on this file since 263a2389 was 263a2389, checked in by Miroslav Cimerman <mc@…>, 6 weeks ago

hr: rename volume state ONLINE to OPTIMAL

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