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

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

hr: use <inttypes.h> macro specifiers

  • Property mode set to 100644
File size: 21.1 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 "superblock.h"
53#include "util.h"
54#include "var.h"
55
56static errno_t hr_raid5_vol_usable(hr_volume_t *);
57static ssize_t hr_raid5_get_bad_ext(hr_volume_t *);
58static errno_t hr_raid5_update_vol_status(hr_volume_t *);
59static void hr_raid5_handle_extent_error(hr_volume_t *, size_t, errno_t);
60static void xor(void *, const void *, size_t);
61static errno_t hr_raid5_read_degraded(hr_volume_t *, uint64_t, uint64_t,
62 void *, size_t);
63static errno_t hr_raid5_write(hr_volume_t *, uint64_t, uint64_t, aoff64_t,
64 const void *, size_t);
65static errno_t hr_raid5_write_parity(hr_volume_t *, uint64_t, uint64_t,
66 uint64_t, const void *, size_t);
67static errno_t hr_raid5_bd_op(hr_bd_op_type_t, bd_srv_t *, aoff64_t, size_t,
68 void *, const void *, size_t);
69static errno_t hr_raid5_rebuild(void *);
70
71/* bdops */
72static errno_t hr_raid5_bd_open(bd_srvs_t *, bd_srv_t *);
73static errno_t hr_raid5_bd_close(bd_srv_t *);
74static errno_t hr_raid5_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
75 size_t);
76static errno_t hr_raid5_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
77static errno_t hr_raid5_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
78 const void *, size_t);
79static errno_t hr_raid5_bd_get_block_size(bd_srv_t *, size_t *);
80static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
81
82static bd_ops_t hr_raid5_bd_ops = {
83 .open = hr_raid5_bd_open,
84 .close = hr_raid5_bd_close,
85 .sync_cache = hr_raid5_bd_sync_cache,
86 .read_blocks = hr_raid5_bd_read_blocks,
87 .write_blocks = hr_raid5_bd_write_blocks,
88 .get_block_size = hr_raid5_bd_get_block_size,
89 .get_num_blocks = hr_raid5_bd_get_num_blocks
90};
91
92extern loc_srv_t *hr_srv;
93
94errno_t hr_raid5_create(hr_volume_t *new_volume)
95{
96 HR_DEBUG("%s()", __func__);
97
98 assert(new_volume->level == HR_LVL_5 || new_volume->level == HR_LVL_4);
99
100 if (new_volume->extent_no < 3) {
101 HR_ERROR("RAID 5 array needs at least 3 devices\n");
102 return EINVAL;
103 }
104
105 fibril_rwlock_write_lock(&new_volume->states_lock);
106
107 errno_t rc = hr_raid5_update_vol_status(new_volume);
108 if (rc != EOK) {
109 fibril_rwlock_write_unlock(&new_volume->states_lock);
110 return rc;
111 }
112
113 bd_srvs_init(&new_volume->hr_bds);
114 new_volume->hr_bds.ops = &hr_raid5_bd_ops;
115 new_volume->hr_bds.sarg = new_volume;
116
117 fibril_rwlock_write_unlock(&new_volume->states_lock);
118
119 return EOK;
120}
121
122/*
123 * Called only once in volume's lifetime.
124 */
125errno_t hr_raid5_init(hr_volume_t *vol)
126{
127 HR_DEBUG("%s()", __func__);
128
129 assert(vol->level == HR_LVL_5 || vol->level == HR_LVL_4);
130
131 uint64_t truncated_blkno = vol->extents[0].blkno;
132 for (size_t i = 1; i < vol->extent_no; i++) {
133 if (vol->extents[i].blkno < truncated_blkno)
134 truncated_blkno = vol->extents[i].blkno;
135 }
136
137 uint64_t total_blkno = truncated_blkno * vol->extent_no;
138
139 vol->truncated_blkno = truncated_blkno;
140 vol->nblocks = total_blkno;
141 vol->data_offset = HR_DATA_OFF;
142
143 vol->data_blkno = total_blkno;
144 vol->data_blkno -= HR_META_SIZE * vol->extent_no; /* count md blocks */
145 vol->data_blkno -= truncated_blkno; /* count parity */
146
147 vol->strip_size = HR_STRIP_SIZE;
148
149 return EOK;
150}
151
152void hr_raid5_status_event(hr_volume_t *vol)
153{
154 fibril_mutex_lock(&vol->lock);
155 fibril_rwlock_write_lock(&vol->states_lock);
156 (void)hr_raid5_update_vol_status(vol);
157 fibril_rwlock_write_unlock(&vol->states_lock);
158 fibril_mutex_unlock(&vol->lock);
159}
160
161errno_t hr_raid5_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
162{
163 HR_DEBUG("%s()", __func__);
164
165 fibril_mutex_lock(&vol->lock);
166
167 errno_t rc = hr_util_add_hotspare(vol, hotspare);
168 if (rc != EOK)
169 goto end;
170
171 /*
172 * If the volume is degraded, start rebuild right away.
173 */
174 if (vol->status == HR_VOL_DEGRADED) {
175 HR_DEBUG("hr_raid5_add_hotspare(): volume in DEGRADED state, "
176 "spawning new rebuild fibril\n");
177 fid_t fib = fibril_create(hr_raid5_rebuild, vol);
178 if (fib == 0) {
179 fibril_mutex_unlock(&vol->hotspare_lock);
180 fibril_mutex_unlock(&vol->lock);
181 return ENOMEM;
182 }
183 fibril_start(fib);
184 fibril_detach(fib);
185 }
186
187end:
188 fibril_mutex_unlock(&vol->lock);
189
190 return rc;
191}
192
193static errno_t hr_raid5_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
194{
195 HR_DEBUG("%s()\n", __func__);
196
197 hr_volume_t *vol = bd->srvs->sarg;
198
199 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
200
201 return EOK;
202}
203
204static errno_t hr_raid5_bd_close(bd_srv_t *bd)
205{
206 HR_DEBUG("%s()\n", __func__);
207
208 hr_volume_t *vol = bd->srvs->sarg;
209
210 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
211
212 return EOK;
213}
214
215static errno_t hr_raid5_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
216{
217 return hr_raid5_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
218}
219
220static errno_t hr_raid5_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
221 void *buf, size_t size)
222{
223 return hr_raid5_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
224}
225
226static errno_t hr_raid5_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
227 const void *data, size_t size)
228{
229 return hr_raid5_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
230}
231
232static errno_t hr_raid5_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
233{
234 hr_volume_t *vol = bd->srvs->sarg;
235
236 *rsize = vol->bsize;
237 return EOK;
238}
239
240static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
241{
242 hr_volume_t *vol = bd->srvs->sarg;
243
244 *rnb = vol->data_blkno;
245 return EOK;
246}
247
248static errno_t hr_raid5_vol_usable(hr_volume_t *vol)
249{
250 if (vol->status == HR_VOL_ONLINE ||
251 vol->status == HR_VOL_DEGRADED ||
252 vol->status == HR_VOL_REBUILD)
253 return EOK;
254 return EIO;
255}
256
257/*
258 * Returns (-1) if all extents are online,
259 * else returns index of first bad one.
260 */
261static ssize_t hr_raid5_get_bad_ext(hr_volume_t *vol)
262{
263 for (size_t i = 0; i < vol->extent_no; i++)
264 if (vol->extents[i].status != HR_EXT_ONLINE)
265 return i;
266 return -1;
267}
268
269static errno_t hr_raid5_update_vol_status(hr_volume_t *vol)
270{
271 hr_vol_status_t old_state = vol->status;
272 size_t bad = 0;
273 for (size_t i = 0; i < vol->extent_no; i++)
274 if (vol->extents[i].status != HR_EXT_ONLINE)
275 bad++;
276
277 switch (bad) {
278 case 0:
279 if (old_state != HR_VOL_ONLINE)
280 hr_update_vol_status(vol, HR_VOL_ONLINE);
281 return EOK;
282 case 1:
283 if (old_state != HR_VOL_DEGRADED &&
284 old_state != HR_VOL_REBUILD) {
285
286 hr_update_vol_status(vol, HR_VOL_DEGRADED);
287
288 if (vol->hotspare_no > 0) {
289 fid_t fib = fibril_create(hr_raid5_rebuild,
290 vol);
291 if (fib == 0)
292 return ENOMEM;
293 fibril_start(fib);
294 fibril_detach(fib);
295 }
296 }
297 return EOK;
298 default:
299 if (old_state != HR_VOL_FAULTY)
300 hr_update_vol_status(vol, HR_VOL_FAULTY);
301 return EIO;
302 }
303}
304
305static void hr_raid5_handle_extent_error(hr_volume_t *vol, size_t extent,
306 errno_t rc)
307{
308 if (rc == ENOENT)
309 hr_update_ext_status(vol, extent, HR_EXT_MISSING);
310 else if (rc != EOK)
311 hr_update_ext_status(vol, extent, HR_EXT_FAILED);
312}
313
314static void xor(void *dst, const void *src, size_t size)
315{
316 size_t i;
317 uint64_t *d = dst;
318 const uint64_t *s = src;
319
320 for (i = 0; i < size / sizeof(uint64_t); ++i)
321 *d++ ^= *s++;
322}
323
324static errno_t hr_raid5_read_degraded(hr_volume_t *vol, uint64_t bad,
325 uint64_t block, void *data, size_t cnt)
326{
327 errno_t rc;
328 size_t i;
329 void *xorbuf;
330 void *buf;
331 uint64_t len = vol->bsize * cnt;
332
333 xorbuf = malloc(len);
334 if (xorbuf == NULL)
335 return ENOMEM;
336
337 buf = malloc(len);
338 if (buf == NULL) {
339 free(xorbuf);
340 return ENOMEM;
341 }
342
343 /* read all other extents in the stripe */
344 bool first = true;
345 for (i = 0; i < vol->extent_no; i++) {
346 if (i == bad)
347 continue;
348
349 if (first) {
350 rc = block_read_direct(vol->extents[i].svc_id, block,
351 cnt, xorbuf);
352 if (rc != EOK)
353 goto end;
354
355 first = false;
356 } else {
357 rc = block_read_direct(vol->extents[i].svc_id, block,
358 cnt, buf);
359 if (rc != EOK)
360 goto end;
361 xor(xorbuf, buf, len);
362 }
363 }
364
365 memcpy(data, xorbuf, len);
366end:
367 free(xorbuf);
368 free(buf);
369 return rc;
370}
371
372static errno_t hr_raid5_write(hr_volume_t *vol, uint64_t p_extent,
373 uint64_t extent, aoff64_t ba, const void *data, size_t cnt)
374{
375 errno_t rc;
376 size_t i;
377 void *xorbuf;
378 void *buf;
379 uint64_t len = vol->bsize * cnt;
380
381 ssize_t bad = hr_raid5_get_bad_ext(vol);
382 if (bad == -1 || (size_t)bad == p_extent) {
383 rc = block_write_direct(vol->extents[extent].svc_id, ba, cnt,
384 data);
385 if (rc != EOK)
386 return rc;
387 /*
388 * DEGRADED parity - skip parity write
389 */
390 if ((size_t)bad == p_extent)
391 return EOK;
392
393 rc = hr_raid5_write_parity(vol, p_extent, extent, ba, data,
394 cnt);
395 return rc;
396 }
397
398 xorbuf = malloc(len);
399 if (xorbuf == NULL)
400 return ENOMEM;
401
402 buf = malloc(len);
403 if (buf == NULL) {
404 free(xorbuf);
405 return ENOMEM;
406 }
407
408 if (extent == (size_t)bad) {
409 /*
410 * new parity = read other and xor in new data
411 *
412 * write new parity
413 */
414 bool first = true;
415 for (i = 0; i < vol->extent_no; i++) {
416 if (i == (size_t)bad)
417 continue;
418 if (i == p_extent)
419 continue;
420 if (first) {
421 rc = block_read_direct(vol->extents[i].svc_id,
422 ba, cnt, xorbuf);
423 if (rc != EOK)
424 goto end;
425
426 first = false;
427 } else {
428 rc = block_read_direct(vol->extents[i].svc_id,
429 ba, cnt, buf);
430 if (rc != EOK)
431 goto end;
432 xor(xorbuf, buf, len);
433 }
434 }
435 xor(xorbuf, data, len);
436 rc = block_write_direct(vol->extents[p_extent].svc_id, ba, cnt,
437 xorbuf);
438 if (rc != EOK)
439 goto end;
440 } else {
441 /*
442 * new parity = xor original data and old parity and new data
443 *
444 * write parity, new data
445 */
446 rc = block_read_direct(vol->extents[extent].svc_id, ba, cnt,
447 xorbuf);
448 if (rc != EOK)
449 goto end;
450 rc = block_read_direct(vol->extents[p_extent].svc_id, ba, cnt,
451 buf);
452 if (rc != EOK)
453 goto end;
454
455 xor(xorbuf, buf, len);
456
457 xor(xorbuf, data, len);
458
459 rc = block_write_direct(vol->extents[p_extent].svc_id, ba, cnt,
460 xorbuf);
461 if (rc != EOK)
462 goto end;
463 rc = block_write_direct(vol->extents[extent].svc_id, ba, cnt,
464 data);
465 if (rc != EOK)
466 goto end;
467 }
468end:
469 free(xorbuf);
470 free(buf);
471 return rc;
472}
473
474static errno_t hr_raid5_write_parity(hr_volume_t *vol, uint64_t p_extent,
475 uint64_t extent, uint64_t block, const void *data, size_t cnt)
476{
477 errno_t rc;
478 size_t i;
479 void *xorbuf;
480 void *buf;
481 uint64_t len = vol->bsize * cnt;
482
483 xorbuf = malloc(len);
484 if (xorbuf == NULL)
485 return ENOMEM;
486
487 buf = malloc(len);
488 if (buf == NULL) {
489 free(xorbuf);
490 return ENOMEM;
491 }
492
493 bool first = true;
494 for (i = 0; i < vol->extent_no; i++) {
495 if (i == p_extent)
496 continue;
497
498 if (first) {
499 if (i == extent) {
500 memcpy(xorbuf, data, len);
501 } else {
502 rc = block_read_direct(vol->extents[i].svc_id,
503 block, cnt, xorbuf);
504 if (rc != EOK)
505 goto end;
506 }
507
508 first = false;
509 } else {
510 if (i == extent) {
511 xor(xorbuf, data, len);
512 } else {
513 rc = block_read_direct(vol->extents[i].svc_id,
514 block, cnt, buf);
515 if (rc != EOK)
516 goto end;
517
518 xor(xorbuf, buf, len);
519 }
520 }
521 }
522
523 rc = block_write_direct(vol->extents[p_extent].svc_id, block, cnt,
524 xorbuf);
525end:
526 free(xorbuf);
527 free(buf);
528 return rc;
529}
530
531static errno_t hr_raid5_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
532 size_t cnt, void *dst, const void *src, size_t size)
533{
534 hr_volume_t *vol = bd->srvs->sarg;
535 errno_t rc;
536 uint64_t phys_block, len;
537 size_t left;
538 const uint8_t *data_write = src;
539 uint8_t *data_read = dst;
540
541 /* propagate sync */
542 if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
543 hr_sync_all_extents(vol);
544 rc = hr_raid5_update_vol_status(vol);
545 return rc;
546 }
547
548 if (type == HR_BD_READ || type == HR_BD_WRITE)
549 if (size < cnt * vol->bsize)
550 return EINVAL;
551
552 rc = hr_check_ba_range(vol, cnt, ba);
553 if (rc != EOK)
554 return rc;
555
556 uint8_t layout = vol->layout;
557 hr_level_t level = vol->level;
558
559 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
560 uint64_t stripe = (ba / strip_size); /* stripe number */
561
562 /* parity extent */
563 uint64_t p_extent;
564 if (level == HR_LVL_4 && layout == HR_RLQ_RAID4_0) {
565 p_extent = 0;
566 } else if (level == HR_LVL_4 && layout == HR_RLQ_RAID4_N) {
567 p_extent = vol->extent_no - 1;
568 } else if (level == HR_LVL_5 && layout == HR_RLQ_RAID5_0R) {
569 p_extent = (stripe / (vol->extent_no - 1)) % vol->extent_no;
570 } else if (level == HR_LVL_5 &&
571 (layout == HR_RLQ_RAID5_NR || layout == HR_RLQ_RAID5_NC)) {
572 p_extent = (vol->extent_no - 1) -
573 (stripe / (vol->extent_no - 1)) % vol->extent_no;
574 } else {
575 return EINVAL;
576 }
577
578 uint64_t extent;
579 if (level == HR_LVL_4 && layout == HR_RLQ_RAID4_0) {
580 extent = (stripe % (vol->extent_no - 1)) + 1;
581 } else if (level == HR_LVL_4 && layout == HR_RLQ_RAID4_N) {
582 extent = stripe % (vol->extent_no - 1);
583 } else if (level == HR_LVL_5 &&
584 (layout == HR_RLQ_RAID5_0R || layout == HR_RLQ_RAID5_NR)) {
585 if ((stripe % (vol->extent_no - 1)) < p_extent)
586 extent = stripe % (vol->extent_no - 1);
587 else
588 extent = (stripe % (vol->extent_no - 1)) + 1;
589 } else if (level == HR_LVL_5 && layout == HR_RLQ_RAID5_NC) {
590 extent =
591 ((stripe % (vol->extent_no - 1)) + p_extent + 1) %
592 vol->extent_no;
593 } else {
594 return EINVAL;
595 }
596
597 uint64_t ext_stripe = stripe / (vol->extent_no - 1); /* stripe level */
598 uint64_t strip_off = ba % strip_size; /* strip offset */
599
600 fibril_mutex_lock(&vol->lock);
601
602 rc = hr_raid5_vol_usable(vol);
603 if (rc != EOK) {
604 fibril_mutex_unlock(&vol->lock);
605 return EIO;
606 }
607
608 left = cnt;
609
610 fibril_rwlock_write_lock(&vol->states_lock);
611 while (left != 0) {
612 phys_block = ext_stripe * strip_size + strip_off;
613 cnt = min(left, strip_size - strip_off);
614 len = vol->bsize * cnt;
615 hr_add_ba_offset(vol, &phys_block);
616 switch (type) {
617 case HR_BD_SYNC:
618 if (vol->extents[extent].status != HR_EXT_ONLINE)
619 break;
620 rc = block_sync_cache(vol->extents[extent].svc_id,
621 phys_block, cnt);
622 /* allow unsupported sync */
623 if (rc == ENOTSUP)
624 rc = EOK;
625 break;
626 case HR_BD_READ:
627 retry_read:
628 ssize_t bad = hr_raid5_get_bad_ext(vol);
629 if (bad > -1 && extent == (size_t)bad) {
630 rc = hr_raid5_read_degraded(vol, bad,
631 phys_block, data_read, cnt);
632 } else {
633 rc = block_read_direct(vol->extents[extent].svc_id,
634 phys_block, cnt, data_read);
635 }
636 data_read += len;
637 break;
638 case HR_BD_WRITE:
639 retry_write:
640 rc = hr_raid5_write(vol, p_extent, extent, phys_block,
641 data_write, cnt);
642 data_write += len;
643 break;
644 default:
645 rc = EINVAL;
646 goto error;
647 }
648
649 if (rc == ENOMEM)
650 goto error;
651
652 hr_raid5_handle_extent_error(vol, extent, rc);
653
654 if (rc != EOK) {
655 rc = hr_raid5_update_vol_status(vol);
656 if (rc == EOK) {
657 /*
658 * State changed from ONLINE -> DEGRADED,
659 * rewind and retry
660 */
661 if (type == HR_BD_WRITE) {
662 data_write -= len;
663 goto retry_write;
664 } else if (type == HR_BD_WRITE) {
665 data_read -= len;
666 goto retry_read;
667 }
668 } else {
669 rc = EIO;
670 goto error;
671 }
672 }
673
674 left -= cnt;
675 strip_off = 0;
676 stripe++;
677
678 ext_stripe = stripe / (vol->extent_no - 1); /* stripe level */
679
680 if (level == HR_LVL_5 && layout == HR_RLQ_RAID5_0R) {
681 p_extent =
682 (stripe / (vol->extent_no - 1)) % vol->extent_no;
683 } else if (level == HR_LVL_5 &&
684 (layout == HR_RLQ_RAID5_NR || layout == HR_RLQ_RAID5_NC)) {
685 p_extent = (vol->extent_no - 1) -
686 (stripe / (vol->extent_no - 1)) % vol->extent_no;
687 }
688
689 if (level == HR_LVL_4 && layout == HR_RLQ_RAID4_0) {
690 extent = (stripe % (vol->extent_no - 1)) + 1;
691 } else if (level == HR_LVL_4 && layout == HR_RLQ_RAID4_N) {
692 extent = stripe % (vol->extent_no - 1);
693 } else if (level == HR_LVL_5 &&
694 (layout == HR_RLQ_RAID5_0R || layout == HR_RLQ_RAID5_NR)) {
695 if ((stripe % (vol->extent_no - 1)) < p_extent)
696 extent = stripe % (vol->extent_no - 1);
697 else
698 extent = (stripe % (vol->extent_no - 1)) + 1;
699 } else if (level == HR_LVL_5 && layout == HR_RLQ_RAID5_NC) {
700 extent =
701 ((stripe % (vol->extent_no - 1)) + p_extent + 1) %
702 vol->extent_no;
703 }
704 }
705
706error:
707 (void)hr_raid5_update_vol_status(vol);
708 fibril_rwlock_write_unlock(&vol->states_lock);
709 fibril_mutex_unlock(&vol->lock);
710 return rc;
711}
712
713static errno_t hr_raid5_rebuild(void *arg)
714{
715 HR_DEBUG("hr_raid5_rebuild()\n");
716
717 hr_volume_t *vol = arg;
718 errno_t rc = EOK;
719 void *buf = NULL, *xorbuf = NULL;
720
721 fibril_mutex_lock(&vol->lock);
722 fibril_rwlock_read_lock(&vol->extents_lock);
723 fibril_rwlock_write_lock(&vol->states_lock);
724
725 if (vol->hotspare_no == 0) {
726 HR_WARN("hr_raid5_rebuild(): no free hotspares on \"%s\", "
727 "aborting rebuild\n", vol->devname);
728 /* retval isn't checked for now */
729 goto end;
730 }
731
732 size_t bad = vol->extent_no;
733 for (size_t i = 0; i < vol->extent_no; i++) {
734 if (vol->extents[i].status == HR_EXT_FAILED) {
735 bad = i;
736 break;
737 }
738 }
739
740 if (bad == vol->extent_no) {
741 HR_WARN("hr_raid5_rebuild(): no bad extent on \"%s\", "
742 "aborting rebuild\n", vol->devname);
743 /* retval isn't checked for now */
744 goto end;
745 }
746
747 size_t hotspare_idx = vol->hotspare_no - 1;
748
749 hr_ext_status_t hs_state = vol->hotspares[hotspare_idx].status;
750 if (hs_state != HR_EXT_HOTSPARE) {
751 HR_ERROR("hr_raid5_rebuild(): invalid hotspare state \"%s\", "
752 "aborting rebuild\n", hr_get_ext_status_msg(hs_state));
753 rc = EINVAL;
754 goto end;
755 }
756
757 HR_DEBUG("hr_raid5_rebuild(): swapping in hotspare\n");
758
759 block_fini(vol->extents[bad].svc_id);
760
761 vol->extents[bad].svc_id = vol->hotspares[hotspare_idx].svc_id;
762 hr_update_ext_status(vol, bad, HR_EXT_HOTSPARE);
763
764 vol->hotspares[hotspare_idx].svc_id = 0;
765 fibril_mutex_lock(&vol->hotspare_lock);
766 hr_update_hotspare_status(vol, hotspare_idx, HR_EXT_MISSING);
767 fibril_mutex_unlock(&vol->hotspare_lock);
768
769 vol->hotspare_no--;
770
771 hr_extent_t *rebuild_ext = &vol->extents[bad];
772
773 HR_DEBUG("hr_raid5_rebuild(): starting rebuild on (%" PRIun ")\n",
774 rebuild_ext->svc_id);
775
776 hr_update_ext_status(vol, bad, HR_EXT_REBUILD);
777 hr_update_vol_status(vol, HR_VOL_REBUILD);
778
779 uint64_t max_blks = DATA_XFER_LIMIT / vol->bsize;
780 uint64_t left = vol->data_blkno / (vol->extent_no - 1);
781 buf = malloc(max_blks * vol->bsize);
782 xorbuf = malloc(max_blks * vol->bsize);
783
784 uint64_t ba = 0, cnt;
785 hr_add_ba_offset(vol, &ba);
786
787 while (left != 0) {
788 cnt = min(left, max_blks);
789
790 /*
791 * Almost the same as read_degraded,
792 * but we don't want to allocate new
793 * xorbuf each blk rebuild batch.
794 */
795 bool first = true;
796 for (size_t i = 0; i < vol->extent_no; i++) {
797 if (i == bad)
798 continue;
799 if (first)
800 rc = block_read_direct(vol->extents[i].svc_id,
801 ba, cnt, xorbuf);
802 else
803 rc = block_read_direct(vol->extents[i].svc_id,
804 ba, cnt, buf);
805 if (rc != EOK) {
806 hr_raid5_handle_extent_error(vol, i, rc);
807 HR_ERROR("rebuild on \"%s\" (%" PRIun "), "
808 "failed due to a failed ONLINE extent, "
809 "number %zu\n",
810 vol->devname, vol->svc_id, i);
811 goto end;
812 }
813
814 if (!first)
815 xor(xorbuf, buf, cnt * vol->bsize);
816 else
817 first = false;
818 }
819
820 rc = block_write_direct(rebuild_ext->svc_id, ba, cnt, xorbuf);
821 if (rc != EOK) {
822 hr_raid5_handle_extent_error(vol, bad, rc);
823 HR_ERROR("rebuild on \"%s\" (%" PRIun "), failed due to "
824 "the rebuilt extent number %zu failing\n",
825 vol->devname, vol->svc_id, bad);
826 goto end;
827 }
828
829 ba += cnt;
830 left -= cnt;
831
832 /*
833 * Let other IO requests be served
834 * during rebuild.
835 */
836 fibril_rwlock_write_unlock(&vol->states_lock);
837 fibril_mutex_unlock(&vol->lock);
838 fibril_mutex_lock(&vol->lock);
839 fibril_rwlock_write_lock(&vol->states_lock);
840 }
841
842 HR_DEBUG("hr_raid5_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
843 "extent number %zu\n", vol->devname, vol->svc_id, hotspare_idx);
844
845 hr_update_ext_status(vol, bad, HR_EXT_ONLINE);
846
847 rc = hr_metadata_save(vol);
848
849end:
850 (void)hr_raid5_update_vol_status(vol);
851
852 fibril_rwlock_write_unlock(&vol->states_lock);
853 fibril_rwlock_read_unlock(&vol->extents_lock);
854 fibril_mutex_unlock(&vol->lock);
855
856 if (buf != NULL)
857 free(buf);
858
859 if (xorbuf != NULL)
860 free(xorbuf);
861
862 return rc;
863}
864
865/** @}
866 */
Note: See TracBrowser for help on using the repository browser.