source: mainline/uspace/srv/bd/hr/raid5.c@ 93ea452

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

hr: add HR_METADATA_ALLOW_REBUILD flag

For now we want to be conservative with what we
allow foreign metadata volumes to do.

  • Property mode set to 100644
File size: 21.8 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 <mem.h>
47#include <task.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <str_error.h>
51
52#include "io.h"
53#include "parity_stripe.h"
54#include "superblock.h"
55#include "util.h"
56#include "var.h"
57
58static void hr_raid5_vol_state_eval_forced(hr_volume_t *);
59static size_t hr_raid5_parity_extent(hr_level_t, hr_layout_t, size_t,
60 uint64_t);
61static size_t hr_raid5_data_extent(hr_level_t, hr_layout_t, size_t, uint64_t,
62 uint64_t);
63static errno_t hr_raid5_rebuild(void *);
64
65/* bdops */
66static errno_t hr_raid5_bd_open(bd_srvs_t *, bd_srv_t *);
67static errno_t hr_raid5_bd_close(bd_srv_t *);
68static errno_t hr_raid5_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
69 size_t);
70static errno_t hr_raid5_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
71static errno_t hr_raid5_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
72 const void *, size_t);
73static errno_t hr_raid5_bd_get_block_size(bd_srv_t *, size_t *);
74static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
75
76static bd_ops_t hr_raid5_bd_ops = {
77 .open = hr_raid5_bd_open,
78 .close = hr_raid5_bd_close,
79 .sync_cache = hr_raid5_bd_sync_cache,
80 .read_blocks = hr_raid5_bd_read_blocks,
81 .write_blocks = hr_raid5_bd_write_blocks,
82 .get_block_size = hr_raid5_bd_get_block_size,
83 .get_num_blocks = hr_raid5_bd_get_num_blocks
84};
85
86extern loc_srv_t *hr_srv;
87
88errno_t hr_raid5_create(hr_volume_t *new_volume)
89{
90 HR_DEBUG("%s()", __func__);
91
92 if (new_volume->level != HR_LVL_5 && new_volume->level != HR_LVL_4)
93 return EINVAL;
94
95 if (new_volume->extent_no < 3) {
96 HR_ERROR("RAID 5 volume needs at least 3 devices\n");
97 return EINVAL;
98 }
99
100 bd_srvs_init(&new_volume->hr_bds);
101 new_volume->hr_bds.ops = &hr_raid5_bd_ops;
102 new_volume->hr_bds.sarg = new_volume;
103
104 hr_raid5_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_raid5_init(hr_volume_t *vol)
122{
123 HR_DEBUG("%s()", __func__);
124
125 if (vol->level != HR_LVL_5 && vol->level != HR_LVL_4)
126 return EINVAL;
127
128 vol->data_offset = vol->meta_ops->get_data_offset();
129
130 uint64_t single_sz = vol->truncated_blkno - vol->meta_ops->get_size();
131 vol->data_blkno = single_sz * (vol->extent_no - 1);
132
133 vol->strip_size = HR_STRIP_SIZE;
134
135 if (vol->level == HR_LVL_4)
136 vol->layout = HR_LAYOUT_RAID4_N;
137 else
138 vol->layout = HR_LAYOUT_RAID5_NR;
139
140 return EOK;
141}
142
143void hr_raid5_vol_state_eval(hr_volume_t *vol)
144{
145 HR_DEBUG("%s()", __func__);
146
147 bool exp = true;
148 if (!atomic_compare_exchange_strong(&vol->state_dirty, &exp, false))
149 return;
150
151 vol->meta_ops->inc_counter(vol);
152 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
153
154 hr_raid5_vol_state_eval_forced(vol);
155}
156
157errno_t hr_raid5_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
158{
159 HR_DEBUG("%s()", __func__);
160
161 errno_t rc = hr_util_add_hotspare(vol, hotspare);
162
163 hr_raid5_vol_state_eval(vol);
164
165 return rc;
166}
167
168void hr_raid5_ext_state_cb(hr_volume_t *vol, size_t extent, errno_t rc)
169{
170 HR_DEBUG("%s()", __func__);
171
172 assert(fibril_rwlock_is_locked(&vol->extents_lock));
173
174 if (rc == EOK)
175 return;
176
177 fibril_rwlock_write_lock(&vol->states_lock);
178
179 switch (rc) {
180 case ENOENT:
181 hr_update_ext_state(vol, extent, HR_EXT_MISSING);
182 break;
183 default:
184 hr_update_ext_state(vol, extent, HR_EXT_FAILED);
185 }
186
187 hr_mark_vol_state_dirty(vol);
188
189 fibril_rwlock_write_unlock(&vol->states_lock);
190}
191
192static errno_t hr_raid5_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
193{
194 HR_DEBUG("%s()\n", __func__);
195
196 hr_volume_t *vol = bd->srvs->sarg;
197
198 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
199
200 return EOK;
201}
202
203static errno_t hr_raid5_bd_close(bd_srv_t *bd)
204{
205 HR_DEBUG("%s()\n", __func__);
206
207 hr_volume_t *vol = bd->srvs->sarg;
208
209 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
210
211 return EOK;
212}
213
214static errno_t hr_raid5_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
215{
216 hr_volume_t *vol = bd->srvs->sarg;
217
218 return hr_sync_extents(vol);
219}
220
221static errno_t hr_raid5_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
222 void *data_read, size_t size)
223{
224 hr_volume_t *vol = bd->srvs->sarg;
225 errno_t rc;
226
227 if (size < cnt * vol->bsize)
228 return EINVAL;
229
230 fibril_rwlock_read_lock(&vol->states_lock);
231 hr_vol_state_t vol_state = vol->state;
232 fibril_rwlock_read_unlock(&vol->states_lock);
233
234 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
235 return EIO;
236
237 rc = hr_check_ba_range(vol, cnt, ba);
238 if (rc != EOK)
239 return rc;
240
241 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
242 uint64_t strip_no = ba / strip_size;
243
244 /* calculate number of stripes touched */
245 uint64_t last_ba = ba + cnt - 1;
246 uint64_t end_strip_no = last_ba / strip_size;
247 uint64_t start_stripe = strip_no / (vol->extent_no - 1);
248 uint64_t end_stripe = end_strip_no / (vol->extent_no - 1);
249 size_t stripes_cnt = end_stripe - start_stripe + 1;
250
251 hr_stripe_t *stripes = hr_create_stripes(vol, vol->strip_size,
252 stripes_cnt, false);
253
254 hr_range_lock_t **rlps = hr_malloc_waitok(stripes_cnt * sizeof(*rlps));
255
256 /*
257 * extent order has to be locked for the whole IO duration,
258 * so that workers have consistent targets
259 */
260 fibril_rwlock_read_lock(&vol->extents_lock);
261
262 for (uint64_t s = start_stripe; s <= end_stripe; s++) {
263 uint64_t relative = s - start_stripe;
264 rlps[relative] = hr_range_lock_acquire(vol, s, 1);
265 }
266
267 uint64_t phys_block, len;
268 size_t left;
269
270 hr_layout_t layout = vol->layout;
271 hr_level_t level = vol->level;
272
273 /* parity extent */
274 size_t p_extent = hr_raid5_parity_extent(level, layout,
275 vol->extent_no, strip_no);
276
277 uint64_t strip_off = ba % strip_size;
278
279 left = cnt;
280
281 while (left != 0) {
282 if (level == HR_LVL_5) {
283 p_extent = hr_raid5_parity_extent(level, layout,
284 vol->extent_no, strip_no);
285 }
286
287 size_t extent = hr_raid5_data_extent(level, layout,
288 vol->extent_no, strip_no, p_extent);
289
290 uint64_t stripe_no = strip_no / (vol->extent_no - 1);
291 size_t relative_si = stripe_no - start_stripe; /* relative stripe index */
292 hr_stripe_t *stripe = &stripes[relative_si];
293 stripe->p_extent = p_extent;
294
295 stripe->strips_touched++;
296
297 phys_block = stripe_no * strip_size + strip_off;
298 cnt = min(left, strip_size - strip_off);
299 len = vol->bsize * cnt;
300 hr_add_data_offset(vol, &phys_block);
301
302 stripe->extent_span[extent].range.start = phys_block;
303 stripe->extent_span[extent].range.end = phys_block + cnt - 1;
304 stripe->extent_span[extent].cnt = cnt;
305 stripe->extent_span[extent].data_read = data_read;
306 stripe->extent_span[extent].strip_off = strip_off;
307
308 data_read += len;
309 left -= cnt;
310 strip_off = 0;
311 strip_no++;
312 }
313
314retry:
315 size_t bad_extent = vol->extent_no;
316
317 uint64_t rebuild_pos = atomic_load_explicit(&vol->rebuild_blk,
318 memory_order_relaxed);
319
320 fibril_rwlock_read_lock(&vol->states_lock);
321
322 for (size_t e = 0; e < vol->extent_no; e++) {
323 hr_ext_state_t s = vol->extents[e].state;
324 if ((vol->state == HR_VOL_DEGRADED && s != HR_EXT_ONLINE) ||
325 (s == HR_EXT_REBUILD && end_stripe >= rebuild_pos)) {
326 bad_extent = e;
327 break;
328 }
329 }
330
331 fibril_rwlock_read_unlock(&vol->states_lock);
332
333 for (size_t s = 0; s < stripes_cnt; s++) {
334 if (stripes[s].done)
335 continue;
336 execute_stripe(&stripes[s], bad_extent);
337 }
338
339 for (size_t s = 0; s < stripes_cnt; s++) {
340 if (stripes[s].done)
341 continue;
342 wait_for_stripe(&stripes[s]);
343 }
344
345 hr_raid5_vol_state_eval(vol);
346
347 rc = EOK;
348
349 fibril_rwlock_read_lock(&vol->states_lock);
350
351 if (vol->state == HR_VOL_FAULTY) {
352 fibril_rwlock_read_unlock(&vol->states_lock);
353 rc = EIO;
354 goto end;
355 }
356
357 fibril_rwlock_read_unlock(&vol->states_lock);
358
359 for (size_t s = 0; s < stripes_cnt; s++)
360 if (stripes[s].rc == EAGAIN)
361 goto retry;
362
363 /* all stripes are done */
364end:
365 fibril_rwlock_read_unlock(&vol->extents_lock);
366
367 for (size_t i = 0; i < stripes_cnt; i++)
368 hr_range_lock_release(rlps[i]);
369
370 free(rlps);
371
372 hr_destroy_stripes(stripes, stripes_cnt);
373
374 return rc;
375}
376
377static errno_t hr_raid5_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
378 const void *data_write, size_t size)
379{
380 hr_volume_t *vol = bd->srvs->sarg;
381 errno_t rc;
382
383 if (size < cnt * vol->bsize)
384 return EINVAL;
385
386 fibril_rwlock_read_lock(&vol->states_lock);
387 hr_vol_state_t vol_state = vol->state;
388 fibril_rwlock_read_unlock(&vol->states_lock);
389
390 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
391 return EIO;
392
393 /* increment metadata counter only on first write */
394 bool exp = false;
395 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
396 vol->meta_ops->inc_counter(vol);
397 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
398 }
399
400 rc = hr_check_ba_range(vol, cnt, ba);
401 if (rc != EOK)
402 return rc;
403
404 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
405 uint64_t strip_no = ba / strip_size;
406
407 /* calculate number of stripes touched */
408 uint64_t last_ba = ba + cnt - 1;
409 uint64_t end_strip_no = last_ba / strip_size;
410 uint64_t start_stripe = strip_no / (vol->extent_no - 1);
411 uint64_t end_stripe = end_strip_no / (vol->extent_no - 1);
412 size_t stripes_cnt = end_stripe - start_stripe + 1;
413
414 hr_stripe_t *stripes = hr_create_stripes(vol, vol->strip_size,
415 stripes_cnt, true);
416
417 uint64_t stripe_size = strip_size * (vol->extent_no - 1);
418
419 for (uint64_t stripe = start_stripe; stripe <= end_stripe; stripe++) {
420 uint64_t relative_stripe = stripe - start_stripe;
421
422 uint64_t s_start = stripe * stripe_size;
423 uint64_t s_end = s_start + stripe_size - 1;
424
425 uint64_t overlap_start;
426 if (ba > s_start)
427 overlap_start = ba;
428 else
429 overlap_start = s_start;
430
431 uint64_t overlap_end;
432 if (last_ba < s_end)
433 overlap_end = last_ba;
434 else
435 overlap_end = s_end;
436
437 uint64_t start_strip_index =
438 (overlap_start - s_start) / strip_size;
439 uint64_t end_strip_index = (overlap_end - s_start) / strip_size;
440 size_t strips_touched = end_strip_index - start_strip_index + 1;
441
442 stripes[relative_stripe].strips_touched = strips_touched;
443
444 uint64_t first_offset = (overlap_start - s_start) % strip_size;
445 uint64_t last_offset = (overlap_end - s_start) % strip_size;
446
447 size_t partials = 0;
448 if (first_offset != 0)
449 partials++;
450 if (last_offset != strip_size - 1)
451 partials++;
452 if (start_strip_index == end_strip_index && partials == 2)
453 partials = 1;
454
455 stripes[relative_stripe].strips_touched = strips_touched;
456 stripes[relative_stripe].partial_strips_touched = partials;
457
458 if (strips_touched < (vol->extent_no - 1) / 2)
459 stripes[relative_stripe].subtract = true;
460 }
461
462 hr_range_lock_t **rlps = hr_malloc_waitok(stripes_cnt * sizeof(*rlps));
463
464 /*
465 * extent order has to be locked for the whole IO duration,
466 * so that workers have consistent targets
467 */
468 fibril_rwlock_read_lock(&vol->extents_lock);
469
470 for (uint64_t s = start_stripe; s <= end_stripe; s++) {
471 uint64_t relative = s - start_stripe;
472 rlps[relative] = hr_range_lock_acquire(vol, s, 1);
473 }
474
475 uint64_t phys_block, len;
476 size_t left;
477
478 hr_layout_t layout = vol->layout;
479 hr_level_t level = vol->level;
480
481 /* parity extent */
482 size_t p_extent = hr_raid5_parity_extent(level, layout,
483 vol->extent_no, strip_no);
484
485 uint64_t strip_off = ba % strip_size;
486
487 left = cnt;
488
489 while (left != 0) {
490 if (level == HR_LVL_5) {
491 p_extent = hr_raid5_parity_extent(level, layout,
492 vol->extent_no, strip_no);
493 }
494
495 size_t extent = hr_raid5_data_extent(level, layout,
496 vol->extent_no, strip_no, p_extent);
497
498 uint64_t stripe_no = strip_no / (vol->extent_no - 1);
499 size_t relative_si = stripe_no - start_stripe; /* relative stripe index */
500 hr_stripe_t *stripe = &stripes[relative_si];
501 stripe->p_extent = p_extent;
502
503 phys_block = stripe_no * strip_size + strip_off;
504 cnt = min(left, strip_size - strip_off);
505 len = vol->bsize * cnt;
506 hr_add_data_offset(vol, &phys_block);
507
508 stripe->extent_span[extent].range.start = phys_block;
509 stripe->extent_span[extent].range.end = phys_block + cnt - 1;
510 stripe->extent_span[extent].cnt = cnt;
511 stripe->extent_span[extent].data_write = data_write;
512 stripe->extent_span[extent].strip_off = strip_off;
513
514 data_write += len;
515 left -= cnt;
516 strip_off = 0;
517 strip_no++;
518 }
519
520retry:
521 size_t bad_extent = vol->extent_no;
522
523 uint64_t rebuild_pos = atomic_load_explicit(&vol->rebuild_blk,
524 memory_order_relaxed);
525
526 fibril_rwlock_read_lock(&vol->states_lock);
527
528 for (size_t e = 0; e < vol->extent_no; e++) {
529 hr_ext_state_t s = vol->extents[e].state;
530 if ((vol->state == HR_VOL_DEGRADED && s != HR_EXT_ONLINE) ||
531 (s == HR_EXT_REBUILD && start_stripe > rebuild_pos)) {
532 bad_extent = e;
533 break;
534 }
535 }
536
537 fibril_rwlock_read_unlock(&vol->states_lock);
538
539 for (size_t s = 0; s < stripes_cnt; s++) {
540 if (stripes[s].done)
541 continue;
542 execute_stripe(&stripes[s], bad_extent);
543 }
544
545 for (size_t s = 0; s < stripes_cnt; s++) {
546 if (stripes[s].done)
547 continue;
548 wait_for_stripe(&stripes[s]);
549 }
550
551 hr_raid5_vol_state_eval(vol);
552
553 rc = EOK;
554
555 fibril_rwlock_read_lock(&vol->states_lock);
556
557 if (vol->state == HR_VOL_FAULTY) {
558 fibril_rwlock_read_unlock(&vol->states_lock);
559 rc = EIO;
560 goto end;
561 }
562
563 fibril_rwlock_read_unlock(&vol->states_lock);
564
565 for (size_t s = 0; s < stripes_cnt; s++)
566 if (stripes[s].rc == EAGAIN)
567 goto retry;
568
569 /* all stripes are done */
570end:
571 fibril_rwlock_read_unlock(&vol->extents_lock);
572
573 for (size_t i = 0; i < stripes_cnt; i++)
574 hr_range_lock_release(rlps[i]);
575
576 free(rlps);
577
578 hr_destroy_stripes(stripes, stripes_cnt);
579
580 return rc;
581}
582
583static errno_t hr_raid5_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
584{
585 hr_volume_t *vol = bd->srvs->sarg;
586
587 *rsize = vol->bsize;
588 return EOK;
589}
590
591static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
592{
593 hr_volume_t *vol = bd->srvs->sarg;
594
595 *rnb = vol->data_blkno;
596 return EOK;
597}
598
599static void hr_raid5_vol_state_eval_forced(hr_volume_t *vol)
600{
601 fibril_rwlock_read_lock(&vol->extents_lock);
602 fibril_rwlock_write_lock(&vol->states_lock);
603
604 hr_vol_state_t state = vol->state;
605
606 size_t bad = 0;
607 for (size_t i = 0; i < vol->extent_no; i++)
608 if (vol->extents[i].state != HR_EXT_ONLINE)
609 bad++;
610
611 size_t invalid_no = hr_count_extents(vol, HR_EXT_INVALID);
612
613 size_t rebuild_no = hr_count_extents(vol, HR_EXT_REBUILD);
614
615 fibril_mutex_lock(&vol->hotspare_lock);
616 size_t hs_no = vol->hotspare_no;
617 fibril_mutex_unlock(&vol->hotspare_lock);
618
619 switch (bad) {
620 case 0:
621 if (state != HR_VOL_OPTIMAL)
622 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
623 break;
624 case 1:
625 if (state != HR_VOL_DEGRADED && state != HR_VOL_REBUILD)
626 hr_update_vol_state(vol, HR_VOL_DEGRADED);
627
628 if (state != HR_VOL_REBUILD) {
629 if (hs_no > 0 || invalid_no > 0 || rebuild_no > 0) {
630 fid_t fib = fibril_create(hr_raid5_rebuild,
631 vol);
632 if (fib == 0)
633 break;
634 fibril_start(fib);
635 fibril_detach(fib);
636 }
637 }
638 break;
639 default:
640 if (state != HR_VOL_FAULTY)
641 hr_update_vol_state(vol, HR_VOL_FAULTY);
642 break;
643 }
644
645 fibril_rwlock_write_unlock(&vol->states_lock);
646 fibril_rwlock_read_unlock(&vol->extents_lock);
647}
648
649static size_t hr_raid5_parity_extent(hr_level_t level,
650 hr_layout_t layout, size_t extent_no, uint64_t strip_no)
651{
652 switch (level) {
653 case HR_LVL_4:
654 switch (layout) {
655 case HR_LAYOUT_RAID4_0:
656 return (0);
657 case HR_LAYOUT_RAID4_N:
658 return (extent_no - 1);
659 default:
660 assert(0 && "invalid layout configuration");
661 }
662 case HR_LVL_5:
663 switch (layout) {
664 case HR_LAYOUT_RAID5_0R:
665 return ((strip_no / (extent_no - 1)) % extent_no);
666 case HR_LAYOUT_RAID5_NR:
667 case HR_LAYOUT_RAID5_NC:
668 return ((extent_no - 1) -
669 (strip_no / (extent_no - 1)) % extent_no);
670 default:
671 assert(0 && "invalid layout configuration");
672 }
673 default:
674 assert(0 && "invalid layout configuration");
675 }
676}
677
678static size_t hr_raid5_data_extent(hr_level_t level,
679 hr_layout_t layout, size_t extent_no, uint64_t strip_no, size_t p_extent)
680{
681 switch (level) {
682 case HR_LVL_4:
683 switch (layout) {
684 case HR_LAYOUT_RAID4_0:
685 return ((strip_no % (extent_no - 1)) + 1);
686 case HR_LAYOUT_RAID4_N:
687 return (strip_no % (extent_no - 1));
688 default:
689 assert(0 && "invalid layout configuration");
690 }
691 case HR_LVL_5:
692 switch (layout) {
693 case HR_LAYOUT_RAID5_0R:
694 case HR_LAYOUT_RAID5_NR:
695 if ((strip_no % (extent_no - 1)) < p_extent)
696 return (strip_no % (extent_no - 1));
697 else
698 return ((strip_no % (extent_no - 1)) + 1);
699 case HR_LAYOUT_RAID5_NC:
700 return (((strip_no % (extent_no - 1)) + p_extent + 1) %
701 extent_no);
702 default:
703 assert(0 && "invalid layout configuration");
704 }
705 default:
706 assert(0 && "invalid layout configuration");
707 }
708}
709
710static errno_t hr_raid5_rebuild(void *arg)
711{
712 HR_DEBUG("%s()", __func__);
713
714 hr_volume_t *vol = arg;
715 errno_t rc = EOK;
716 size_t rebuild_idx;
717 void *buf = NULL, *xorbuf = NULL;
718
719 if (!(vol->meta_ops->get_flags() & HR_METADATA_ALLOW_REBUILD))
720 return ENOTSUP;
721
722 rc = hr_init_rebuild(vol, &rebuild_idx);
723 if (rc != EOK)
724 return rc;
725
726 uint64_t max_blks = DATA_XFER_LIMIT / vol->bsize;
727 uint64_t left =
728 vol->data_blkno / (vol->extent_no - 1) - vol->rebuild_blk;
729 buf = hr_malloc_waitok(max_blks * vol->bsize);
730 xorbuf = hr_malloc_waitok(max_blks * vol->bsize);
731
732 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
733
734 size_t cnt;
735 uint64_t ba = vol->rebuild_blk;
736 hr_add_data_offset(vol, &ba);
737
738 /*
739 * this is not necessary because a rebuild is
740 * protected by itself, i.e. there can be only
741 * one REBUILD at a time
742 */
743 fibril_rwlock_read_lock(&vol->extents_lock);
744
745 /* increment metadata counter only on first write */
746 bool exp = false;
747 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
748 vol->meta_ops->inc_counter(vol);
749 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
750 }
751
752 hr_range_lock_t *rl = NULL;
753 hr_stripe_t *stripe = hr_create_stripes(vol, max_blks * vol->bsize, 1,
754 false);
755
756 HR_NOTE("\"%s\": REBUILD started on extent no. %zu at block %lu.\n",
757 vol->devname, rebuild_idx, ba);
758
759 uint64_t written = 0;
760 unsigned int percent, old_percent = 100;
761 while (left != 0) {
762 cnt = min(left, max_blks);
763
764 uint64_t strip_no = ba / strip_size;
765 uint64_t last_ba = ba + cnt - 1;
766 uint64_t end_strip_no = last_ba / strip_size;
767 uint64_t start_stripe = strip_no / (vol->extent_no - 1);
768 uint64_t end_stripe = end_strip_no / (vol->extent_no - 1);
769 size_t stripes_cnt = end_stripe - start_stripe + 1;
770
771 stripe->ps_to_be_added = vol->extent_no - 1;
772 stripe->p_count_final = true;
773
774 hr_fgroup_t *worker_group =
775 hr_fgroup_create(vol->fge, vol->extent_no);
776
777 rl = hr_range_lock_acquire(vol, start_stripe, stripes_cnt);
778
779 atomic_store_explicit(&vol->rebuild_blk, ba,
780 memory_order_relaxed);
781
782 for (size_t e = 0; e < vol->extent_no; e++) {
783 if (e == rebuild_idx)
784 continue;
785
786 hr_io_raid5_t *io = hr_fgroup_alloc(worker_group);
787 io->extent = e;
788 io->ba = ba;
789 io->cnt = cnt;
790 io->strip_off = 0;
791 io->vol = vol;
792 io->stripe = stripe;
793
794 hr_fgroup_submit(worker_group,
795 hr_io_raid5_reconstruct_reader, io);
796 }
797
798 hr_io_raid5_t *io = hr_fgroup_alloc(worker_group);
799 io->extent = rebuild_idx;
800 io->ba = ba;
801 io->cnt = cnt;
802 io->strip_off = 0;
803 io->vol = vol;
804 io->stripe = stripe;
805
806 hr_fgroup_submit(worker_group, hr_io_raid5_parity_writer, io);
807
808 size_t failed;
809 (void)hr_fgroup_wait(worker_group, NULL, &failed);
810 if (failed > 0) {
811 hr_range_lock_release(rl);
812 HR_NOTE("\"%s\": REBUILD aborted.\n", vol->devname);
813 goto end;
814 }
815
816 percent = ((ba + cnt) * 100) / vol->data_blkno;
817 if (percent != old_percent) {
818 if (percent % 5 == 0)
819 HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
820 vol->devname, percent);
821 }
822
823 if (written * vol->bsize > HR_REBUILD_SAVE_BYTES) {
824 vol->meta_ops->save_ext(vol, rebuild_idx,
825 WITH_STATE_CALLBACK);
826 written = 0;
827 }
828
829 hr_range_lock_release(rl);
830 hr_reset_stripe(stripe);
831
832 written += cnt;
833 ba += cnt;
834 left -= cnt;
835 old_percent = percent;
836
837 /*
838 * Let other IO requests be served
839 * during rebuild.
840 */
841 }
842
843 HR_DEBUG("hr_raid5_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
844 "extent number %zu\n", vol->devname, vol->svc_id, rebuild_idx);
845
846 fibril_rwlock_write_lock(&vol->states_lock);
847
848 hr_update_ext_state(vol, rebuild_idx, HR_EXT_ONLINE);
849
850 atomic_store_explicit(&vol->rebuild_blk, 0, memory_order_relaxed);
851
852 hr_mark_vol_state_dirty(vol);
853
854 fibril_rwlock_write_unlock(&vol->states_lock);
855end:
856 fibril_rwlock_read_unlock(&vol->extents_lock);
857
858 hr_raid1_vol_state_eval(vol);
859
860 hr_destroy_stripes(stripe, 1);
861 free(buf);
862 free(xorbuf);
863
864 return rc;
865}
866
867/** @}
868 */
Note: See TracBrowser for help on using the repository browser.