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

Last change on this file since 5fe0b9b5 was f1be66bf, checked in by Miroslav Cimerman <mc@…>, 8 months ago

hr: raid5.c: fast patch to make new asserts pass

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