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

Last change on this file since 5b320ac was 7b359f5, checked in by Miroslav Cimerman <mc@…>, 9 months ago

hr: status/state event function for each RAID

  • Property mode set to 100644
File size: 13.3 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 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);
69
70/* bdops */
71static errno_t hr_raid5_bd_open(bd_srvs_t *, bd_srv_t *);
72static errno_t hr_raid5_bd_close(bd_srv_t *);
73static errno_t hr_raid5_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
74 size_t);
75static errno_t hr_raid5_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
76static errno_t hr_raid5_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
77 const void *, size_t);
78static errno_t hr_raid5_bd_get_block_size(bd_srv_t *, size_t *);
79static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
80
81static bd_ops_t hr_raid5_bd_ops = {
82 .open = hr_raid5_bd_open,
83 .close = hr_raid5_bd_close,
84 .sync_cache = hr_raid5_bd_sync_cache,
85 .read_blocks = hr_raid5_bd_read_blocks,
86 .write_blocks = hr_raid5_bd_write_blocks,
87 .get_block_size = hr_raid5_bd_get_block_size,
88 .get_num_blocks = hr_raid5_bd_get_num_blocks
89};
90
91errno_t hr_raid5_create(hr_volume_t *new_volume)
92{
93 errno_t rc;
94
95 assert(new_volume->level == HR_LVL_5);
96
97 if (new_volume->dev_no < 3) {
98 HR_ERROR("RAID 5 array needs at least 3 devices\n");
99 return EINVAL;
100 }
101
102 rc = hr_raid5_update_vol_status(new_volume);
103 if (rc != EOK)
104 return rc;
105
106 bd_srvs_init(&new_volume->hr_bds);
107 new_volume->hr_bds.ops = &hr_raid5_bd_ops;
108 new_volume->hr_bds.sarg = new_volume;
109
110 rc = hr_register_volume(new_volume);
111
112 return rc;
113}
114
115errno_t hr_raid5_init(hr_volume_t *vol)
116{
117 errno_t rc;
118 size_t bsize;
119 uint64_t total_blkno;
120
121 assert(vol->level == HR_LVL_5);
122
123 rc = hr_check_devs(vol, &total_blkno, &bsize);
124 if (rc != EOK)
125 return rc;
126
127 vol->nblocks = total_blkno;
128 vol->bsize = bsize;
129 vol->data_offset = HR_DATA_OFF;
130 vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no) -
131 (vol->nblocks / vol->dev_no);
132 vol->strip_size = HR_STRIP_SIZE;
133
134 return EOK;
135}
136
137void hr_raid5_status_event(hr_volume_t *vol)
138{
139 fibril_mutex_lock(&vol->lock);
140 (void) hr_raid5_update_vol_status(vol);
141 fibril_mutex_unlock(&vol->lock);
142}
143
144static errno_t hr_raid5_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
145{
146 HR_DEBUG("hr_bd_open()\n");
147 return EOK;
148}
149
150static errno_t hr_raid5_bd_close(bd_srv_t *bd)
151{
152 HR_DEBUG("hr_bd_close()\n");
153 return EOK;
154}
155
156static errno_t hr_raid5_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
157{
158 return hr_raid5_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
159}
160
161static errno_t hr_raid5_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
162 void *buf, size_t size)
163{
164 return hr_raid5_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
165}
166
167static errno_t hr_raid5_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
168 const void *data, size_t size)
169{
170 return hr_raid5_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
171}
172
173static errno_t hr_raid5_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
174{
175 hr_volume_t *vol = bd->srvs->sarg;
176
177 *rsize = vol->bsize;
178 return EOK;
179}
180
181static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
182{
183 hr_volume_t *vol = bd->srvs->sarg;
184
185 *rnb = vol->data_blkno;
186 return EOK;
187}
188
189static errno_t hr_raid5_vol_usable(hr_volume_t *vol)
190{
191 if (vol->status == HR_VOL_ONLINE ||
192 vol->status == HR_VOL_DEGRADED)
193 return EOK;
194 return EINVAL;
195}
196
197/*
198 * Returns (-1) if all extents are online,
199 * else returns index of first bad one.
200 */
201static ssize_t hr_raid5_get_bad_ext(hr_volume_t *vol)
202{
203 for (size_t i = 0; i < vol->dev_no; i++)
204 if (vol->extents[i].status != HR_EXT_ONLINE)
205 return i;
206 return -1;
207}
208
209static errno_t hr_raid5_update_vol_status(hr_volume_t *vol)
210{
211 hr_vol_status_t old_state = vol->status;
212 size_t bad = 0;
213 for (size_t i = 0; i < vol->dev_no; i++)
214 if (vol->extents[i].status != HR_EXT_ONLINE)
215 bad++;
216
217 switch (bad) {
218 case 0:
219 if (old_state != HR_VOL_ONLINE) {
220 HR_WARN("RAID 5 has all extents online, "
221 "marking \"%s\" (%lu) as ONLINE",
222 vol->devname, vol->svc_id);
223 vol->status = HR_VOL_ONLINE;
224 }
225 return EOK;
226 case 1:
227 if (old_state != HR_VOL_DEGRADED) {
228 HR_WARN("RAID 5 array \"%s\" (%lu) has 1 extent "
229 "inactive, marking as DEGRADED",
230 vol->devname, vol->svc_id);
231 vol->status = HR_VOL_DEGRADED;
232 }
233 return EOK;
234 default:
235 if (old_state != HR_VOL_FAULTY) {
236 HR_WARN("RAID 5 array \"%s\" (%lu) has more "
237 "than one 1 extent inactive, marking as FAULTY",
238 vol->devname, vol->svc_id);
239 vol->status = HR_VOL_FAULTY;
240 }
241 return EINVAL;
242 }
243}
244
245static void xor(void *dst, const void *src, size_t size)
246{
247 size_t i;
248 uint64_t *d = dst;
249 const uint64_t *s = src;
250
251 for (i = 0; i < size / sizeof(uint64_t); ++i)
252 *d++ ^= *s++;
253}
254
255static errno_t hr_raid5_read_degraded(hr_volume_t *vol, uint64_t bad,
256 uint64_t block, void *data, size_t cnt)
257{
258 errno_t rc;
259 size_t i;
260 void *xorbuf;
261 void *buf;
262 uint64_t len = vol->bsize * cnt;
263
264 xorbuf = malloc(len);
265 if (xorbuf == NULL)
266 return ENOMEM;
267
268 buf = malloc(len);
269 if (buf == NULL) {
270 free(xorbuf);
271 return ENOMEM;
272 }
273
274 /* read all other extents in the stripe */
275 memset(xorbuf, 0, len);
276 for (i = 0; i < vol->dev_no; i++) {
277 if (i == bad) {
278 continue;
279 } else {
280 rc = block_read_direct(vol->extents[i].svc_id, block,
281 cnt, buf);
282 if (rc != EOK)
283 goto end;
284 xor(xorbuf, buf, len);
285 }
286 }
287
288 memcpy(data, xorbuf, len);
289end:
290 free(xorbuf);
291 free(buf);
292 return rc;
293}
294
295static errno_t hr_raid5_write(hr_volume_t *vol, uint64_t p_extent,
296 uint64_t extent, aoff64_t ba, const void *data, size_t cnt)
297{
298 errno_t rc;
299 size_t i;
300 void *xorbuf;
301 void *buf;
302 uint64_t len = vol->bsize * cnt;
303
304 ssize_t bad = hr_raid5_get_bad_ext(vol);
305 if (bad == -1 || (size_t)bad == p_extent) {
306 rc = block_write_direct(vol->extents[extent].svc_id, ba, cnt,
307 data);
308 if (rc != EOK)
309 return rc;
310 /*
311 * DEGRADED parity - skip parity write
312 */
313 if ((size_t)bad == p_extent)
314 return EOK;
315
316 rc = hr_raid5_write_parity(vol, p_extent, extent, ba, data,
317 cnt);
318 return rc;
319 }
320
321 xorbuf = malloc(len);
322 if (xorbuf == NULL)
323 return ENOMEM;
324
325 buf = malloc(len);
326 if (buf == NULL) {
327 free(xorbuf);
328 return ENOMEM;
329 }
330
331 if (extent == (size_t) bad) {
332 /*
333 * new parity = read other and xor in new data
334 *
335 * write new parity
336 */
337 memset(xorbuf, 0, len);
338 for (i = 1; i < vol->dev_no; i++) {
339 if (i == (size_t) bad) {
340 continue;
341 } else {
342 rc = block_read_direct(vol->extents[i].svc_id,
343 ba, cnt, buf);
344 if (rc != EOK)
345 goto end;
346 xor(xorbuf, buf, len);
347 }
348 }
349 xor(xorbuf, data, len);
350 rc = block_write_direct(vol->extents[p_extent].svc_id, ba, cnt,
351 xorbuf);
352 if (rc != EOK)
353 goto end;
354 } else {
355 /*
356 * new parity = xor original data and old parity and new data
357 *
358 * write parity, new data
359 */
360 rc = block_read_direct(vol->extents[extent].svc_id, ba, cnt,
361 xorbuf);
362 if (rc != EOK)
363 goto end;
364 rc = block_read_direct(vol->extents[p_extent].svc_id, ba, cnt,
365 buf);
366 if (rc != EOK)
367 goto end;
368
369 xor(xorbuf, buf, len);
370
371 xor(xorbuf, data, len);
372
373 rc = block_write_direct(vol->extents[p_extent].svc_id, ba, cnt,
374 xorbuf);
375 if (rc != EOK)
376 goto end;
377 rc = block_write_direct(vol->extents[extent].svc_id, ba, cnt,
378 data);
379 if (rc != EOK)
380 goto end;
381 }
382end:
383 free(xorbuf);
384 free(buf);
385 return rc;
386}
387
388static errno_t hr_raid5_write_parity(hr_volume_t *vol, uint64_t p_extent,
389 uint64_t extent, uint64_t block, const void *data, size_t cnt)
390{
391 errno_t rc;
392 size_t i;
393 void *xorbuf;
394 void *buf;
395 uint64_t len = vol->bsize * cnt;
396
397 xorbuf = malloc(len);
398 if (xorbuf == NULL)
399 return ENOMEM;
400
401 buf = malloc(len);
402 if (buf == NULL) {
403 free(xorbuf);
404 return ENOMEM;
405 }
406
407 memset(xorbuf, 0, len);
408 for (i = 0; i < vol->dev_no; i++) {
409 if (i == p_extent)
410 continue;
411 if (i == extent) {
412 xor(xorbuf, data, vol->bsize);
413 } else {
414 rc = block_read_direct(vol->extents[i].svc_id,
415 block, cnt, buf);
416 if (rc != EOK)
417 goto end;
418 xor(xorbuf, buf, vol->bsize);
419 }
420 }
421
422 rc = block_write_direct(vol->extents[p_extent].svc_id, block, cnt,
423 xorbuf);
424end:
425 free(xorbuf);
426 free(buf);
427 return rc;
428}
429
430static errno_t hr_raid5_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
431 size_t cnt, void *dst, const void *src, size_t size)
432{
433 hr_volume_t *vol = bd->srvs->sarg;
434 errno_t rc;
435 uint64_t phys_block, len;
436 size_t left;
437 const uint8_t *data_write = src;
438 uint8_t *data_read = dst;
439
440 /* propagate sync */
441 if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
442 hr_sync_all_extents(vol);
443 rc = hr_raid5_update_vol_status(vol);
444 return rc;
445 }
446
447 if (type == HR_BD_READ || type == HR_BD_WRITE)
448 if (size < cnt * vol->bsize)
449 return EINVAL;
450
451 rc = hr_check_ba_range(vol, cnt, ba);
452 if (rc != EOK)
453 return rc;
454
455 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
456 uint64_t stripe = (ba / strip_size); /* stripe number */
457 uint64_t p_extent = (stripe / (vol->dev_no - 1)) % vol->dev_no; /* parity extent */
458 uint64_t extent;
459 if ((stripe % (vol->dev_no - 1)) < p_extent)
460 extent = (stripe % (vol->dev_no - 1));
461 else
462 extent = ((stripe % (vol->dev_no - 1)) + 1);
463 uint64_t ext_stripe = stripe / (vol->dev_no - 1); /* stripe level */
464 uint64_t strip_off = ba % strip_size; /* strip offset */
465
466 fibril_mutex_lock(&vol->lock);
467
468 rc = hr_raid5_vol_usable(vol);
469 if (rc != EOK) {
470 fibril_mutex_unlock(&vol->lock);
471 return EIO;
472 }
473
474 left = cnt;
475 while (left != 0) {
476 phys_block = ext_stripe * strip_size + strip_off;
477 cnt = min(left, strip_size - strip_off);
478 len = vol->bsize * cnt;
479 hr_add_ba_offset(vol, &phys_block);
480 switch (type) {
481 case HR_BD_SYNC:
482 if (vol->extents[extent].status != HR_EXT_ONLINE)
483 break;
484 rc = block_sync_cache(vol->extents[extent].svc_id,
485 phys_block, cnt);
486 /* allow unsupported sync */
487 if (rc == ENOTSUP)
488 rc = EOK;
489 break;
490 case HR_BD_READ:
491 retry_read:
492 ssize_t bad = hr_raid5_get_bad_ext(vol);
493 if (bad > 0 && extent == (size_t) bad) {
494 rc = hr_raid5_read_degraded(vol, bad,
495 phys_block, data_read, cnt);
496 } else {
497 rc = block_read_direct(vol->extents[extent].svc_id,
498 phys_block, cnt, data_read);
499 }
500 data_read += len;
501 break;
502 case HR_BD_WRITE:
503 retry_write:
504 rc = hr_raid5_write(vol, p_extent, extent, phys_block,
505 data_write, cnt);
506 data_write += len;
507 break;
508 default:
509 rc = EINVAL;
510 goto error;
511 }
512
513 if (rc == ENOMEM)
514 goto error;
515
516 if (rc == ENOENT)
517 hr_update_ext_status(vol, extent, HR_EXT_MISSING);
518 else if (rc != EOK)
519 hr_update_ext_status(vol, extent, HR_EXT_FAILED);
520
521 if (rc != EOK) {
522 rc = hr_raid5_update_vol_status(vol);
523 if (rc == EOK) {
524 /*
525 * State changed from ONLINE -> DEGRADED,
526 * rewind and retry
527 */
528 if (type == HR_BD_WRITE) {
529 data_write -= len;
530 goto retry_write;
531 } else if (type == HR_BD_WRITE) {
532 data_read -= len;
533 goto retry_read;
534 }
535 } else {
536 rc = EIO;
537 goto error;
538 }
539 }
540
541 left -= cnt;
542 strip_off = 0;
543 if (extent + 1 >= vol->dev_no ||
544 (extent + 1 == p_extent && p_extent + 1 >= vol->dev_no))
545 ext_stripe++;
546 stripe++;
547 p_extent = (stripe / (vol->dev_no - 1)) % vol->dev_no; /* parity extent */
548 if ((stripe % (vol->dev_no - 1)) < p_extent)
549 extent = (stripe % (vol->dev_no - 1));
550 else
551 extent = ((stripe % (vol->dev_no - 1)) + 1);
552 }
553
554error:
555 (void) hr_raid5_update_vol_status(vol);
556 fibril_mutex_unlock(&vol->lock);
557 return rc;
558}
559
560/** @}
561 */
Note: See TracBrowser for help on using the repository browser.