source: mainline/uspace/srv/bd/hr/raid4.c@ d092d2c

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

hr: RAID{0,4}: cast void * to uint8_t *

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