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

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

hr: raid5.c: cstyle

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