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

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

hr: hr_mark_vol_state_dirty() helper

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