source: mainline/uspace/srv/bd/hr/raid1.c@ 640250b

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

hr: add HR_METADATA_ALLOW_REBUILD flag

For now we want to be conservative with what we
allow foreign metadata volumes to do.

  • Property mode set to 100644
File size: 13.8 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 <task.h>
47#include <stdatomic.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <str_error.h>
51
52#include "fge.h"
53#include "io.h"
54#include "superblock.h"
55#include "util.h"
56#include "var.h"
57
58static void hr_raid1_vol_state_eval_forced(hr_volume_t *);
59static size_t hr_raid1_count_good_extents(hr_volume_t *, uint64_t, size_t,
60 uint64_t);
61static errno_t hr_raid1_bd_op(hr_bd_op_type_t, hr_volume_t *, aoff64_t, size_t,
62 void *, const void *, size_t);
63static errno_t hr_raid1_rebuild(void *);
64
65/* bdops */
66static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
67static errno_t hr_raid1_bd_close(bd_srv_t *);
68static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
69 size_t);
70static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
71static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
72 const void *, size_t);
73static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
74static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
75
76static bd_ops_t hr_raid1_bd_ops = {
77 .open = hr_raid1_bd_open,
78 .close = hr_raid1_bd_close,
79 .sync_cache = hr_raid1_bd_sync_cache,
80 .read_blocks = hr_raid1_bd_read_blocks,
81 .write_blocks = hr_raid1_bd_write_blocks,
82 .get_block_size = hr_raid1_bd_get_block_size,
83 .get_num_blocks = hr_raid1_bd_get_num_blocks
84};
85
86extern loc_srv_t *hr_srv;
87
88errno_t hr_raid1_create(hr_volume_t *new_volume)
89{
90 HR_DEBUG("%s()", __func__);
91
92 if (new_volume->level != HR_LVL_1)
93 return EINVAL;
94
95 if (new_volume->extent_no < 2) {
96 HR_ERROR("RAID 1 volume needs at least 2 devices\n");
97 return EINVAL;
98 }
99
100 bd_srvs_init(&new_volume->hr_bds);
101 new_volume->hr_bds.ops = &hr_raid1_bd_ops;
102 new_volume->hr_bds.sarg = new_volume;
103
104 hr_raid1_vol_state_eval_forced(new_volume);
105
106 fibril_rwlock_read_lock(&new_volume->states_lock);
107 hr_vol_state_t state = new_volume->state;
108 fibril_rwlock_read_unlock(&new_volume->states_lock);
109 if (state == HR_VOL_FAULTY || state == HR_VOL_NONE) {
110 HR_NOTE("\"%s\": unusable state, not creating\n",
111 new_volume->devname);
112 return EINVAL;
113 }
114
115 return EOK;
116}
117
118/*
119 * Called only once in volume's lifetime.
120 */
121errno_t hr_raid1_init(hr_volume_t *vol)
122{
123 HR_DEBUG("%s()", __func__);
124
125 if (vol->level != HR_LVL_1)
126 return EINVAL;
127
128 vol->data_offset = vol->meta_ops->get_data_offset();
129 vol->data_blkno = vol->truncated_blkno - vol->meta_ops->get_size();
130 vol->strip_size = 0;
131
132 return EOK;
133}
134
135errno_t hr_raid1_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
136{
137 HR_DEBUG("%s()", __func__);
138
139 errno_t rc = hr_util_add_hotspare(vol, hotspare);
140
141 hr_raid1_vol_state_eval(vol);
142
143 return rc;
144}
145
146void hr_raid1_vol_state_eval(hr_volume_t *vol)
147{
148 HR_DEBUG("%s()", __func__);
149
150 bool exp = true;
151 if (!atomic_compare_exchange_strong(&vol->state_dirty, &exp, false))
152 return;
153
154 vol->meta_ops->inc_counter(vol);
155 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
156
157 hr_raid1_vol_state_eval_forced(vol);
158}
159
160void hr_raid1_ext_state_cb(hr_volume_t *vol, size_t extent, errno_t rc)
161{
162 HR_DEBUG("%s()", __func__);
163
164 assert(fibril_rwlock_is_locked(&vol->extents_lock));
165
166 if (rc == EOK)
167 return;
168
169 fibril_rwlock_write_lock(&vol->states_lock);
170
171 switch (rc) {
172 case ENOENT:
173 hr_update_ext_state(vol, extent, HR_EXT_MISSING);
174 break;
175 default:
176 hr_update_ext_state(vol, extent, HR_EXT_FAILED);
177 }
178
179 hr_mark_vol_state_dirty(vol);
180
181 fibril_rwlock_write_unlock(&vol->states_lock);
182}
183
184static void hr_raid1_vol_state_eval_forced(hr_volume_t *vol)
185{
186 HR_DEBUG("%s()", __func__);
187
188 fibril_rwlock_read_lock(&vol->extents_lock);
189 fibril_rwlock_read_lock(&vol->states_lock);
190
191 hr_vol_state_t old_state = vol->state;
192 size_t healthy = hr_count_extents(vol, HR_EXT_ONLINE);
193
194 size_t invalid_no = hr_count_extents(vol, HR_EXT_INVALID);
195
196 size_t rebuild_no = hr_count_extents(vol, HR_EXT_REBUILD);
197
198 fibril_mutex_lock(&vol->hotspare_lock);
199 size_t hs_no = vol->hotspare_no;
200 fibril_mutex_unlock(&vol->hotspare_lock);
201
202 fibril_rwlock_read_unlock(&vol->states_lock);
203 fibril_rwlock_read_unlock(&vol->extents_lock);
204
205 if (healthy == 0) {
206 if (old_state != HR_VOL_FAULTY) {
207 fibril_rwlock_write_lock(&vol->states_lock);
208 hr_update_vol_state(vol, HR_VOL_FAULTY);
209 fibril_rwlock_write_unlock(&vol->states_lock);
210 }
211 } else if (healthy < vol->extent_no) {
212 if (old_state != HR_VOL_REBUILD &&
213 old_state != HR_VOL_DEGRADED) {
214 fibril_rwlock_write_lock(&vol->states_lock);
215 hr_update_vol_state(vol, HR_VOL_DEGRADED);
216 fibril_rwlock_write_unlock(&vol->states_lock);
217 }
218
219 if (old_state != HR_VOL_REBUILD) {
220 if (hs_no > 0 || invalid_no > 0 || rebuild_no > 0) {
221 fid_t fib = fibril_create(hr_raid1_rebuild,
222 vol);
223 if (fib == 0)
224 return;
225 fibril_start(fib);
226 fibril_detach(fib);
227 }
228 }
229 } else {
230 if (old_state != HR_VOL_OPTIMAL) {
231 fibril_rwlock_write_lock(&vol->states_lock);
232 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
233 fibril_rwlock_write_unlock(&vol->states_lock);
234 }
235 }
236}
237
238static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
239{
240 HR_DEBUG("%s()", __func__);
241
242 hr_volume_t *vol = bd->srvs->sarg;
243
244 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
245
246 return EOK;
247}
248
249static errno_t hr_raid1_bd_close(bd_srv_t *bd)
250{
251 HR_DEBUG("%s()", __func__);
252
253 hr_volume_t *vol = bd->srvs->sarg;
254
255 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
256
257 return EOK;
258}
259
260static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
261{
262 hr_volume_t *vol = bd->srvs->sarg;
263
264 return hr_sync_extents(vol);
265}
266
267static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
268 void *buf, size_t size)
269{
270 hr_volume_t *vol = bd->srvs->sarg;
271
272 return hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL, size);
273}
274
275static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
276 const void *data, size_t size)
277{
278 hr_volume_t *vol = bd->srvs->sarg;
279
280 return hr_raid1_bd_op(HR_BD_WRITE, vol, ba, cnt, NULL, data, size);
281}
282
283static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
284{
285 hr_volume_t *vol = bd->srvs->sarg;
286
287 *rsize = vol->bsize;
288 return EOK;
289}
290
291static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
292{
293 hr_volume_t *vol = bd->srvs->sarg;
294
295 *rnb = vol->data_blkno;
296 return EOK;
297}
298
299static size_t hr_raid1_count_good_extents(hr_volume_t *vol, uint64_t ba,
300 size_t cnt, uint64_t rebuild_blk)
301{
302 assert(fibril_rwlock_is_locked(&vol->extents_lock));
303 assert(fibril_rwlock_is_locked(&vol->states_lock));
304
305 size_t count = 0;
306 for (size_t i = 0; i < vol->extent_no; i++) {
307 if (vol->extents[i].state == HR_EXT_ONLINE ||
308 (vol->extents[i].state == HR_EXT_REBUILD &&
309 rebuild_blk >= ba)) {
310 count++;
311 }
312 }
313
314 return count;
315}
316
317static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, hr_volume_t *vol,
318 aoff64_t ba, size_t cnt, void *data_read, const void *data_write,
319 size_t size)
320{
321 HR_DEBUG("%s()", __func__);
322
323 hr_range_lock_t *rl = NULL;
324 errno_t rc;
325 size_t i;
326 uint64_t rebuild_blk;
327
328 if (size < cnt * vol->bsize)
329 return EINVAL;
330
331 fibril_rwlock_read_lock(&vol->states_lock);
332 hr_vol_state_t vol_state = vol->state;
333 fibril_rwlock_read_unlock(&vol->states_lock);
334
335 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
336 return EIO;
337
338 /* increment metadata counter only on first write */
339 bool exp = false;
340 if (type == HR_BD_WRITE &&
341 atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
342 vol->meta_ops->inc_counter(vol);
343 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
344 }
345
346 rc = hr_check_ba_range(vol, cnt, ba);
347 if (rc != EOK)
348 return rc;
349
350 hr_add_data_offset(vol, &ba);
351
352 /*
353 * extent order has to be locked for the whole IO duration,
354 * so that workers have consistent targets
355 */
356 fibril_rwlock_read_lock(&vol->extents_lock);
357
358 size_t successful = 0;
359 switch (type) {
360 case HR_BD_READ:
361 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
362 memory_order_relaxed);
363
364 for (i = 0; i < vol->extent_no; i++) {
365 fibril_rwlock_read_lock(&vol->states_lock);
366 hr_ext_state_t state = vol->extents[i].state;
367 fibril_rwlock_read_unlock(&vol->states_lock);
368
369 if (state != HR_EXT_ONLINE &&
370 (state != HR_EXT_REBUILD ||
371 ba + cnt - 1 >= rebuild_blk)) {
372 continue;
373 }
374
375 rc = hr_read_direct(vol->extents[i].svc_id, ba, cnt,
376 data_read);
377 if (rc != EOK) {
378 hr_raid1_ext_state_cb(vol, i, rc);
379 } else {
380 successful++;
381 break;
382 }
383 }
384 break;
385 case HR_BD_WRITE:
386 rl = hr_range_lock_acquire(vol, ba, cnt);
387
388 fibril_rwlock_read_lock(&vol->states_lock);
389
390 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
391 memory_order_relaxed);
392
393 size_t good = hr_raid1_count_good_extents(vol, ba, cnt,
394 rebuild_blk);
395
396 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
397
398 for (i = 0; i < vol->extent_no; i++) {
399 if (vol->extents[i].state != HR_EXT_ONLINE &&
400 (vol->extents[i].state != HR_EXT_REBUILD ||
401 ba > rebuild_blk)) {
402 /*
403 * When the extent is being rebuilt,
404 * we only write to the part that is already
405 * rebuilt. If IO starts after vol->rebuild_blk
406 * we do not proceed, the write is going to
407 * be replicated later in the rebuild.
408 */
409 continue;
410 }
411
412 hr_io_t *io = hr_fgroup_alloc(group);
413 io->extent = i;
414 io->data_write = data_write;
415 io->data_read = data_read;
416 io->ba = ba;
417 io->cnt = cnt;
418 io->type = type;
419 io->vol = vol;
420
421 hr_fgroup_submit(group, hr_io_worker, io);
422 }
423
424 fibril_rwlock_read_unlock(&vol->states_lock);
425
426 (void)hr_fgroup_wait(group, &successful, NULL);
427
428 hr_range_lock_release(rl);
429
430 break;
431 default:
432 assert(0);
433 }
434
435 if (successful > 0)
436 rc = EOK;
437 else
438 rc = EIO;
439
440 fibril_rwlock_read_unlock(&vol->extents_lock);
441
442 hr_raid1_vol_state_eval(vol);
443
444 return rc;
445}
446
447static errno_t hr_raid1_rebuild(void *arg)
448{
449 HR_DEBUG("%s()", __func__);
450
451 hr_volume_t *vol = arg;
452 void *buf = NULL;
453 size_t rebuild_idx;
454 hr_extent_t *rebuild_ext = NULL;
455 errno_t rc;
456
457 if (!(vol->meta_ops->get_flags() & HR_METADATA_ALLOW_REBUILD))
458 return ENOTSUP;
459
460 rc = hr_init_rebuild(vol, &rebuild_idx);
461 if (rc != EOK)
462 return rc;
463
464 rebuild_ext = &vol->extents[rebuild_idx];
465
466 size_t left = vol->data_blkno - vol->rebuild_blk;
467 size_t max_blks = DATA_XFER_LIMIT / vol->bsize;
468 buf = hr_malloc_waitok(max_blks * vol->bsize);
469
470 size_t cnt;
471 uint64_t ba = vol->rebuild_blk;
472 hr_add_data_offset(vol, &ba);
473
474 /*
475 * this is not necessary because a rebuild is
476 * protected by itself, i.e. there can be only
477 * one REBUILD at a time
478 */
479 fibril_rwlock_read_lock(&vol->extents_lock);
480
481 /* increment metadata counter only on first write */
482 bool exp = false;
483 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
484 vol->meta_ops->inc_counter(vol);
485 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
486 }
487
488 hr_range_lock_t *rl = NULL;
489
490 HR_NOTE("\"%s\": REBUILD started on extent no. %zu at block %lu.\n",
491 vol->devname, rebuild_idx, ba);
492
493 uint64_t written = 0;
494 unsigned int percent, old_percent = 100;
495 while (left != 0) {
496 cnt = min(max_blks, left);
497
498 rl = hr_range_lock_acquire(vol, ba, cnt);
499
500 atomic_store_explicit(&vol->rebuild_blk, ba,
501 memory_order_relaxed);
502
503 rc = hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL,
504 cnt * vol->bsize);
505 if (rc != EOK) {
506 hr_range_lock_release(rl);
507 goto end;
508 }
509
510 rc = hr_write_direct(rebuild_ext->svc_id, ba, cnt, buf);
511 if (rc != EOK) {
512 hr_raid1_ext_state_cb(vol, rebuild_idx, rc);
513 hr_range_lock_release(rl);
514 goto end;
515 }
516
517 percent = ((ba + cnt) * 100) / vol->data_blkno;
518 if (percent != old_percent) {
519 if (percent % 5 == 0)
520 HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
521 vol->devname, percent);
522 }
523
524 if (written * vol->bsize > HR_REBUILD_SAVE_BYTES) {
525 vol->meta_ops->save_ext(vol, rebuild_idx,
526 WITH_STATE_CALLBACK);
527 written = 0;
528 }
529
530 hr_range_lock_release(rl);
531
532 written += cnt;
533 ba += cnt;
534 left -= cnt;
535 old_percent = percent;
536 }
537
538 HR_DEBUG("hr_raid1_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
539 "extent no. %zu\n", vol->devname, vol->svc_id, rebuild_idx);
540
541 fibril_rwlock_write_lock(&vol->states_lock);
542
543 hr_update_ext_state(vol, rebuild_idx, HR_EXT_ONLINE);
544
545 atomic_store_explicit(&vol->rebuild_blk, 0, memory_order_relaxed);
546
547 hr_mark_vol_state_dirty(vol);
548
549 fibril_rwlock_write_unlock(&vol->states_lock);
550end:
551 fibril_rwlock_read_unlock(&vol->extents_lock);
552
553 hr_raid1_vol_state_eval(vol);
554
555 free(buf);
556
557 return rc;
558}
559
560/** @}
561 */
Note: See TracBrowser for help on using the repository browser.