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

Last change on this file was 09e01d23, checked in by Miroslav Cimerman <mc@…>, 13 days ago

hr: type fixes for 32-bit archs

  • 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_w_extents(hr_volume_t *, uint64_t, size_t,
60 uint64_t);
61static errno_t hr_raid1_bd_op(hr_bd_op_type_t, hr_volume_t *, aoff64_t, size_t,
62 void *, const void *, size_t);
63static errno_t hr_raid1_rebuild(void *);
64
65/* bdops */
66static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
67static errno_t hr_raid1_bd_close(bd_srv_t *);
68static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
69 size_t);
70static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
71static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
72 const void *, size_t);
73static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
74static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
75
76static bd_ops_t hr_raid1_bd_ops = {
77 .open = hr_raid1_bd_open,
78 .close = hr_raid1_bd_close,
79 .sync_cache = hr_raid1_bd_sync_cache,
80 .read_blocks = hr_raid1_bd_read_blocks,
81 .write_blocks = hr_raid1_bd_write_blocks,
82 .get_block_size = hr_raid1_bd_get_block_size,
83 .get_num_blocks = hr_raid1_bd_get_num_blocks
84};
85
86extern loc_srv_t *hr_srv;
87
88errno_t hr_raid1_create(hr_volume_t *new_volume)
89{
90 HR_DEBUG("%s()", __func__);
91
92 if (new_volume->level != HR_LVL_1)
93 return EINVAL;
94
95 if (new_volume->extent_no < 2) {
96 HR_ERROR("RAID 1 volume 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 hr_raid1_vol_state_eval_forced(new_volume);
105
106 fibril_rwlock_read_lock(&new_volume->states_lock);
107 hr_vol_state_t state = new_volume->state;
108 fibril_rwlock_read_unlock(&new_volume->states_lock);
109 if (state == HR_VOL_FAULTY || state == HR_VOL_NONE) {
110 HR_NOTE("\"%s\": unusable state, not creating\n",
111 new_volume->devname);
112 return EINVAL;
113 }
114
115 return EOK;
116}
117
118/*
119 * Called only once in volume's lifetime.
120 */
121errno_t hr_raid1_init(hr_volume_t *vol)
122{
123 HR_DEBUG("%s()", __func__);
124
125 if (vol->level != HR_LVL_1)
126 return EINVAL;
127
128 vol->data_offset = vol->meta_ops->get_data_offset();
129 vol->data_blkno = vol->truncated_blkno - vol->meta_ops->get_size();
130 vol->strip_size = 0;
131
132 return EOK;
133}
134
135void hr_raid1_vol_state_eval(hr_volume_t *vol)
136{
137 HR_DEBUG("%s()", __func__);
138
139 bool exp = true;
140 if (!atomic_compare_exchange_strong(&vol->state_dirty, &exp, false))
141 return;
142
143 vol->meta_ops->inc_counter(vol);
144 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
145
146 hr_raid1_vol_state_eval_forced(vol);
147}
148
149void hr_raid1_ext_state_cb(hr_volume_t *vol, size_t extent, errno_t rc)
150{
151 HR_DEBUG("%s()", __func__);
152
153 assert(fibril_rwlock_is_locked(&vol->extents_lock));
154
155 if (rc == EOK)
156 return;
157
158 fibril_rwlock_write_lock(&vol->states_lock);
159
160 switch (rc) {
161 case ENOENT:
162 hr_update_ext_state(vol, extent, HR_EXT_MISSING);
163 break;
164 default:
165 hr_update_ext_state(vol, extent, HR_EXT_FAILED);
166 }
167
168 hr_mark_vol_state_dirty(vol);
169
170 fibril_rwlock_write_unlock(&vol->states_lock);
171}
172
173static void hr_raid1_vol_state_eval_forced(hr_volume_t *vol)
174{
175 HR_DEBUG("%s()", __func__);
176
177 fibril_rwlock_read_lock(&vol->extents_lock);
178 fibril_rwlock_read_lock(&vol->states_lock);
179
180 hr_vol_state_t old_state = vol->state;
181 size_t healthy = hr_count_extents(vol, HR_EXT_ONLINE);
182
183 size_t invalid_no = hr_count_extents(vol, HR_EXT_INVALID);
184
185 size_t rebuild_no = hr_count_extents(vol, HR_EXT_REBUILD);
186
187 fibril_rwlock_read_unlock(&vol->states_lock);
188 fibril_rwlock_read_unlock(&vol->extents_lock);
189
190 fibril_mutex_lock(&vol->hotspare_lock);
191 size_t hs_no = vol->hotspare_no;
192 fibril_mutex_unlock(&vol->hotspare_lock);
193
194 if (healthy == 0) {
195 if (old_state != HR_VOL_FAULTY) {
196 fibril_rwlock_write_lock(&vol->states_lock);
197 hr_update_vol_state(vol, HR_VOL_FAULTY);
198 fibril_rwlock_write_unlock(&vol->states_lock);
199 }
200 } else if (healthy < vol->extent_no) {
201 if (old_state != HR_VOL_REBUILD &&
202 old_state != HR_VOL_DEGRADED) {
203 fibril_rwlock_write_lock(&vol->states_lock);
204 hr_update_vol_state(vol, HR_VOL_DEGRADED);
205 fibril_rwlock_write_unlock(&vol->states_lock);
206 }
207
208 if (hs_no > 0 || invalid_no > 0 || rebuild_no > 0) {
209 fid_t fib = fibril_create(hr_raid1_rebuild, vol);
210 if (fib == 0)
211 return;
212 fibril_start(fib);
213 fibril_detach(fib);
214 }
215 } else {
216 if (old_state != HR_VOL_OPTIMAL) {
217 fibril_rwlock_write_lock(&vol->states_lock);
218 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
219 fibril_rwlock_write_unlock(&vol->states_lock);
220 }
221 }
222}
223
224static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
225{
226 HR_DEBUG("%s()", __func__);
227
228 hr_volume_t *vol = bd->srvs->sarg;
229
230 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
231
232 return EOK;
233}
234
235static errno_t hr_raid1_bd_close(bd_srv_t *bd)
236{
237 HR_DEBUG("%s()", __func__);
238
239 hr_volume_t *vol = bd->srvs->sarg;
240
241 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
242
243 return EOK;
244}
245
246static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
247{
248 hr_volume_t *vol = bd->srvs->sarg;
249
250 return hr_sync_extents(vol);
251}
252
253static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
254 void *buf, size_t size)
255{
256 hr_volume_t *vol = bd->srvs->sarg;
257
258 return hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL, size);
259}
260
261static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
262 const void *data, size_t size)
263{
264 hr_volume_t *vol = bd->srvs->sarg;
265
266 if (vol->vflags & HR_VOL_FLAG_READ_ONLY)
267 return ENOTSUP;
268
269 return hr_raid1_bd_op(HR_BD_WRITE, vol, ba, cnt, NULL, data, size);
270}
271
272static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
273{
274 hr_volume_t *vol = bd->srvs->sarg;
275
276 *rsize = vol->bsize;
277 return EOK;
278}
279
280static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
281{
282 hr_volume_t *vol = bd->srvs->sarg;
283
284 *rnb = vol->data_blkno;
285 return EOK;
286}
287
288static size_t hr_raid1_count_good_w_extents(hr_volume_t *vol, uint64_t ba,
289 size_t cnt, uint64_t rebuild_blk)
290{
291 assert(fibril_rwlock_is_locked(&vol->extents_lock));
292 assert(fibril_rwlock_is_locked(&vol->states_lock));
293
294 size_t count = 0;
295 for (size_t i = 0; i < vol->extent_no; i++) {
296 if (vol->extents[i].state == HR_EXT_ONLINE ||
297 (vol->extents[i].state == HR_EXT_REBUILD &&
298 rebuild_blk >= ba)) {
299 count++;
300 }
301 }
302
303 return count;
304}
305
306#ifdef HR_RAID1_READ_STRATEGY_SPLIT
307
308static size_t hr_raid1_count_good_r_extents(hr_volume_t *vol, uint64_t ba,
309 size_t cnt, uint64_t rebuild_blk)
310{
311 assert(fibril_rwlock_is_locked(&vol->extents_lock));
312 assert(fibril_rwlock_is_locked(&vol->states_lock));
313
314 size_t count = 0;
315 for (size_t i = 0; i < vol->extent_no; i++) {
316 if (vol->extents[i].state == HR_EXT_ONLINE ||
317 (vol->extents[i].state == HR_EXT_REBUILD &&
318 rebuild_blk > ba + cnt - 1)) {
319 count++;
320 }
321 }
322
323 return count;
324}
325
326#endif /* HR_RAID1_READ_STRATEGY_SPLIT */
327
328#ifdef HR_RAID1_READ_STRATEGY_CLOSEST
329
330static size_t get_ext(hr_volume_t *vol, uint64_t ba, size_t cnt,
331 uint64_t rebuild_blk)
332{
333 uint64_t closest_e;
334 uint64_t pos;
335 uint64_t mdiff = UINT64_MAX;
336 hr_ext_state_t state = vol->extents[0].state;
337 if (state != HR_EXT_ONLINE &&
338 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
339 closest_e = 1;
340 } else {
341 closest_e = 0;
342 pos = atomic_load_explicit(&vol->last_ext_pos_arr[0],
343 memory_order_relaxed);
344 mdiff = (pos > ba) ? pos - ba : ba - pos;
345 }
346
347 for (size_t e = 1; e < vol->extent_no; e++) {
348 state = vol->extents[e].state;
349 if (state != HR_EXT_ONLINE &&
350 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
351 continue;
352 }
353
354 pos = atomic_load_explicit(&vol->last_ext_pos_arr[e],
355 memory_order_relaxed);
356 uint64_t diff = (pos > ba) ? pos - ba : ba - pos;
357 if (diff < mdiff) {
358 mdiff = diff;
359 closest_e = e;
360 }
361 }
362
363 return closest_e;
364}
365
366#elif defined(HR_RAID1_READ_STRATEGY_ROUND_ROBIN)
367
368static size_t get_ext(hr_volume_t *vol, uint64_t ba, size_t cnt,
369 uint64_t rebuild_blk)
370{
371 size_t last_e;
372 size_t fail = 0;
373
374 while (true) {
375 last_e = atomic_fetch_add_explicit(&vol->last_ext_used, 1,
376 memory_order_relaxed);
377 last_e %= vol->extent_no;
378
379 hr_ext_state_t state = vol->extents[last_e].state;
380 if (state != HR_EXT_ONLINE &&
381 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
382 if (++fail >= vol->extent_no)
383 return vol->extent_no;
384 continue;
385 }
386
387 break;
388 }
389
390 return last_e;
391}
392
393#elif defined(HR_RAID1_READ_STRATEGY_FIRST)
394
395static size_t get_ext(hr_volume_t *vol, uint64_t ba, size_t cnt,
396 uint64_t rebuild_blk)
397{
398 for (size_t e = 0; e < vol->extent_no; e++) {
399 hr_ext_state_t state = vol->extents[e].state;
400 if (state != HR_EXT_ONLINE &&
401 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
402 continue;
403 }
404
405 return e;
406 }
407 return vol->extent_no;
408}
409
410#else
411
412#if !defined(HR_RAID1_READ_STRATEGY_SPLIT) || \
413 !defined(HR_RAID1_READ_STRATEGY_SPLIT_THRESHOLD)
414#error "Some RAID 1 read strategy must be used"
415#endif
416
417#endif
418
419static size_t hr_raid1_read(hr_volume_t *vol, uint64_t ba, size_t cnt,
420 void *data_read)
421{
422 uint64_t rebuild_blk;
423 size_t successful = 0;
424
425#if !defined(HR_RAID1_READ_STRATEGY_SPLIT)
426 errno_t rc;
427
428 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
429 memory_order_relaxed);
430 size_t fail = 0;
431 while (fail < vol->extent_no) {
432 fibril_rwlock_read_lock(&vol->states_lock);
433 size_t best_e = get_ext(vol, ba, cnt,
434 rebuild_blk);
435 fibril_rwlock_read_unlock(&vol->states_lock);
436 if (best_e >= vol->extent_no)
437 break;
438 rc = hr_read_direct(vol->extents[best_e].svc_id, ba,
439 cnt, data_read);
440 if (rc != EOK) {
441 hr_raid1_ext_state_cb(vol, best_e, rc);
442 fail++;
443 } else {
444 successful++;
445 break;
446 }
447 }
448
449#else
450
451retry_split:
452 size_t good;
453 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
454 memory_order_relaxed);
455
456 fibril_rwlock_read_lock(&vol->states_lock);
457 good = hr_raid1_count_good_r_extents(vol, ba, cnt,
458 rebuild_blk);
459 fibril_rwlock_read_unlock(&vol->states_lock);
460
461 if (good == 0)
462 return 0;
463
464 size_t cnt_per_ext = (cnt + good - 1) / good;
465 if (cnt_per_ext * vol->bsize < HR_RAID1_READ_STRATEGY_SPLIT_THRESHOLD)
466 cnt_per_ext = cnt;
467
468 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
469
470 fibril_rwlock_read_lock(&vol->states_lock);
471 size_t left = cnt;
472 size_t e = 0;
473 uint8_t *data = data_read;
474 size_t submitted = 0;
475 while (left > 0) {
476 if (e >= vol->extent_no) {
477 fibril_rwlock_read_unlock(&vol->states_lock);
478 if (submitted)
479 (void)hr_fgroup_wait(group, NULL, NULL);
480 goto retry_split;
481 }
482
483 hr_ext_state_t state = vol->extents[e].state;
484 if (state != HR_EXT_ONLINE &&
485 (state != HR_EXT_REBUILD ||
486 ba + cnt - 1 >= rebuild_blk)) {
487 e++;
488 continue;
489 }
490
491 hr_io_t *io = hr_fgroup_alloc(group);
492 io->extent = e;
493 io->data_read = data;
494 io->ba = ba + (cnt - left);
495 size_t cnt_to_dispatch = min(left, cnt_per_ext);
496 io->cnt = cnt_to_dispatch;
497 io->type = HR_BD_READ;
498 io->vol = vol;
499
500 hr_fgroup_submit(group, hr_io_worker, io);
501 submitted++;
502
503 data += cnt_to_dispatch * vol->bsize;
504 left -= cnt_to_dispatch;
505 e++;
506 }
507
508 fibril_rwlock_read_unlock(&vol->states_lock);
509
510 (void)hr_fgroup_wait(group, &successful, NULL);
511 if (successful < submitted)
512 goto retry_split;
513
514#endif
515
516 return successful;
517}
518
519static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, hr_volume_t *vol,
520 aoff64_t ba, size_t cnt, void *data_read, const void *data_write,
521 size_t size)
522{
523 HR_DEBUG("%s()", __func__);
524
525 hr_range_lock_t *rl = NULL;
526 errno_t rc;
527 uint64_t rebuild_blk;
528
529 if (size < cnt * vol->bsize)
530 return EINVAL;
531
532 fibril_rwlock_read_lock(&vol->states_lock);
533 hr_vol_state_t vol_state = vol->state;
534 fibril_rwlock_read_unlock(&vol->states_lock);
535
536 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
537 return EIO;
538
539 /* increment metadata counter only on first write */
540 bool exp = false;
541 if (type == HR_BD_WRITE &&
542 atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
543 vol->meta_ops->inc_counter(vol);
544 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
545 }
546
547 rc = hr_check_ba_range(vol, cnt, ba);
548 if (rc != EOK)
549 return rc;
550
551 hr_add_data_offset(vol, &ba);
552
553 /*
554 * extent order has to be locked for the whole IO duration,
555 * so that workers have consistent targets
556 */
557 fibril_rwlock_read_lock(&vol->extents_lock);
558
559 size_t good;
560 size_t successful;
561 switch (type) {
562 case HR_BD_READ:
563 successful = hr_raid1_read(vol, ba, cnt, data_read);
564 break;
565 case HR_BD_WRITE:
566 rl = hr_range_lock_acquire(vol, ba, cnt);
567
568 fibril_rwlock_read_lock(&vol->states_lock);
569
570 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
571 memory_order_relaxed);
572
573 good = hr_raid1_count_good_w_extents(vol, ba, cnt,
574 rebuild_blk);
575
576 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
577
578 for (size_t i = 0; i < vol->extent_no; i++) {
579 if (vol->extents[i].state != HR_EXT_ONLINE &&
580 (vol->extents[i].state != HR_EXT_REBUILD ||
581 ba > rebuild_blk)) {
582 /*
583 * When the extent is being rebuilt,
584 * we only write to the part that is already
585 * rebuilt. If IO starts after vol->rebuild_blk
586 * we do not proceed, the write is going to
587 * be replicated later in the rebuild.
588 */
589 continue;
590 }
591
592 hr_io_t *io = hr_fgroup_alloc(group);
593 io->extent = i;
594 io->data_write = data_write;
595 io->ba = ba;
596 io->cnt = cnt;
597 io->type = type;
598 io->vol = vol;
599
600 hr_fgroup_submit(group, hr_io_worker, io);
601 }
602
603 fibril_rwlock_read_unlock(&vol->states_lock);
604
605 (void)hr_fgroup_wait(group, &successful, NULL);
606
607 hr_range_lock_release(rl);
608
609 break;
610 default:
611 assert(0);
612 }
613
614 if (successful > 0)
615 rc = EOK;
616 else
617 rc = EIO;
618
619 fibril_rwlock_read_unlock(&vol->extents_lock);
620
621 hr_raid1_vol_state_eval(vol);
622
623 return rc;
624}
625
626static errno_t hr_raid1_rebuild(void *arg)
627{
628 HR_DEBUG("%s()", __func__);
629
630 hr_volume_t *vol = arg;
631 void *buf = NULL;
632 size_t rebuild_idx;
633 hr_extent_t *rebuild_ext = NULL;
634 errno_t rc;
635
636 if (vol->vflags & HR_VOL_FLAG_READ_ONLY)
637 return ENOTSUP;
638 if (!(vol->meta_ops->get_flags() & HR_METADATA_ALLOW_REBUILD))
639 return ENOTSUP;
640
641 rc = hr_init_rebuild(vol, &rebuild_idx);
642 if (rc != EOK)
643 return rc;
644
645 rebuild_ext = &vol->extents[rebuild_idx];
646
647 size_t left = vol->data_blkno - vol->rebuild_blk;
648 size_t max_blks = DATA_XFER_LIMIT / vol->bsize;
649 buf = hr_malloc_waitok(max_blks * vol->bsize);
650
651 size_t cnt;
652 uint64_t ba = vol->rebuild_blk;
653 hr_add_data_offset(vol, &ba);
654
655 /*
656 * this is not necessary because a rebuild is
657 * protected by itself, i.e. there can be only
658 * one REBUILD at a time
659 */
660 fibril_rwlock_read_lock(&vol->extents_lock);
661
662 /* increment metadata counter only on first write */
663 bool exp = false;
664 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
665 vol->meta_ops->inc_counter(vol);
666 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
667 }
668
669 hr_range_lock_t *rl = NULL;
670
671 HR_NOTE("\"%s\": REBUILD started on extent no. %zu at "
672 "block %" PRIu64 ".\n", vol->devname, rebuild_idx, ba);
673
674 uint64_t written = 0;
675 unsigned int percent, old_percent = 100;
676 while (left != 0) {
677 cnt = min(max_blks, left);
678
679 rl = hr_range_lock_acquire(vol, ba, cnt);
680
681 atomic_store_explicit(&vol->rebuild_blk, ba,
682 memory_order_relaxed);
683
684 rc = hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL,
685 cnt * vol->bsize);
686 if (rc != EOK) {
687 hr_range_lock_release(rl);
688 goto end;
689 }
690
691 rc = hr_write_direct(rebuild_ext->svc_id, ba, cnt, buf);
692 if (rc != EOK) {
693 hr_raid1_ext_state_cb(vol, rebuild_idx, rc);
694 hr_range_lock_release(rl);
695 goto end;
696 }
697 atomic_store_explicit(&vol->last_ext_pos_arr[rebuild_idx],
698 ba + cnt - 1, memory_order_relaxed);
699
700 percent = ((ba + cnt) * 100) / vol->data_blkno;
701 if (percent != old_percent) {
702 if (percent % 5 == 0)
703 HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
704 vol->devname, percent);
705 }
706
707 if (written * vol->bsize > HR_REBUILD_SAVE_BYTES) {
708 vol->meta_ops->save_ext(vol, rebuild_idx,
709 WITH_STATE_CALLBACK);
710 written = 0;
711 }
712
713 hr_range_lock_release(rl);
714
715 written += cnt;
716 ba += cnt;
717 left -= cnt;
718 old_percent = percent;
719 }
720
721 HR_DEBUG("hr_raid1_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
722 "extent no. %zu\n", vol->devname, vol->svc_id, rebuild_idx);
723
724 fibril_rwlock_write_lock(&vol->states_lock);
725
726 hr_update_ext_state(vol, rebuild_idx, HR_EXT_ONLINE);
727
728 atomic_store_explicit(&vol->rebuild_blk, 0, memory_order_relaxed);
729
730 hr_mark_vol_state_dirty(vol);
731
732 hr_update_vol_state(vol, HR_VOL_DEGRADED);
733
734 fibril_rwlock_write_unlock(&vol->states_lock);
735end:
736 fibril_rwlock_read_unlock(&vol->extents_lock);
737
738 hr_raid1_vol_state_eval(vol);
739
740 free(buf);
741
742 return rc;
743}
744
745/** @}
746 */
Note: See TracBrowser for help on using the repository browser.