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

Last change on this file since cdfcaea was 09c195e8, checked in by Miroslav Cimerman <mc@…>, 7 weeks ago

hr: move rebuild init to util.c

  • Property mode set to 100644
File size: 13.4 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 (void)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 fibril_mutex_lock(&vol->hotspare_lock);
197 size_t hs_no = vol->hotspare_no;
198 fibril_mutex_unlock(&vol->hotspare_lock);
199
200 fibril_rwlock_read_unlock(&vol->states_lock);
201 fibril_rwlock_read_unlock(&vol->extents_lock);
202
203 if (healthy == 0) {
204 if (old_state != HR_VOL_FAULTY) {
205 fibril_rwlock_write_lock(&vol->states_lock);
206 hr_update_vol_state(vol, HR_VOL_FAULTY);
207 fibril_rwlock_write_unlock(&vol->states_lock);
208 }
209 } else if (healthy < vol->extent_no) {
210 if (old_state != HR_VOL_REBUILD &&
211 old_state != HR_VOL_DEGRADED) {
212 fibril_rwlock_write_lock(&vol->states_lock);
213 hr_update_vol_state(vol, HR_VOL_DEGRADED);
214 fibril_rwlock_write_unlock(&vol->states_lock);
215 }
216
217 if (old_state != HR_VOL_REBUILD) {
218 if (hs_no > 0 || invalid_no > 0) {
219 fid_t fib = fibril_create(hr_raid1_rebuild,
220 vol);
221 if (fib == 0)
222 return;
223 fibril_start(fib);
224 fibril_detach(fib);
225 }
226 }
227 } else {
228 if (old_state != HR_VOL_OPTIMAL) {
229 fibril_rwlock_write_lock(&vol->states_lock);
230 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
231 fibril_rwlock_write_unlock(&vol->states_lock);
232 }
233 }
234}
235
236static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
237{
238 HR_DEBUG("%s()", __func__);
239
240 hr_volume_t *vol = bd->srvs->sarg;
241
242 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
243
244 return EOK;
245}
246
247static errno_t hr_raid1_bd_close(bd_srv_t *bd)
248{
249 HR_DEBUG("%s()", __func__);
250
251 hr_volume_t *vol = bd->srvs->sarg;
252
253 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
254
255 return EOK;
256}
257
258static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
259{
260 hr_volume_t *vol = bd->srvs->sarg;
261
262 return hr_sync_extents(vol);
263}
264
265static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
266 void *buf, size_t size)
267{
268 hr_volume_t *vol = bd->srvs->sarg;
269
270 return hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL, size);
271}
272
273static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
274 const void *data, size_t size)
275{
276 hr_volume_t *vol = bd->srvs->sarg;
277
278 return hr_raid1_bd_op(HR_BD_WRITE, vol, ba, cnt, NULL, data, size);
279}
280
281static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
282{
283 hr_volume_t *vol = bd->srvs->sarg;
284
285 *rsize = vol->bsize;
286 return EOK;
287}
288
289static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
290{
291 hr_volume_t *vol = bd->srvs->sarg;
292
293 *rnb = vol->data_blkno;
294 return EOK;
295}
296
297static size_t hr_raid1_count_good_extents(hr_volume_t *vol, uint64_t ba,
298 size_t cnt, uint64_t rebuild_blk)
299{
300 assert(fibril_rwlock_is_locked(&vol->extents_lock));
301 assert(fibril_rwlock_is_locked(&vol->states_lock));
302
303 size_t count = 0;
304 for (size_t i = 0; i < vol->extent_no; i++) {
305 if (vol->extents[i].state == HR_EXT_ONLINE ||
306 (vol->extents[i].state == HR_EXT_REBUILD &&
307 rebuild_blk >= ba)) {
308 count++;
309 }
310 }
311
312 return count;
313}
314
315static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, hr_volume_t *vol,
316 aoff64_t ba, size_t cnt, void *data_read, const void *data_write,
317 size_t size)
318{
319 HR_DEBUG("%s()", __func__);
320
321 hr_range_lock_t *rl = NULL;
322 errno_t rc;
323 size_t i;
324 uint64_t rebuild_blk;
325
326 if (size < cnt * vol->bsize)
327 return EINVAL;
328
329 fibril_rwlock_read_lock(&vol->states_lock);
330 hr_vol_state_t vol_state = vol->state;
331 fibril_rwlock_read_unlock(&vol->states_lock);
332
333 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
334 return EIO;
335
336 /* increment metadata counter only on first write */
337 bool exp = false;
338 if (type == HR_BD_WRITE &&
339 atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
340 vol->meta_ops->inc_counter(vol);
341 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
342 }
343
344 rc = hr_check_ba_range(vol, cnt, ba);
345 if (rc != EOK)
346 return rc;
347
348 hr_add_data_offset(vol, &ba);
349
350 /*
351 * extent order has to be locked for the whole IO duration,
352 * so that workers have consistent targets
353 */
354 fibril_rwlock_read_lock(&vol->extents_lock);
355
356 size_t successful = 0;
357 switch (type) {
358 case HR_BD_READ:
359 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
360 memory_order_relaxed);
361
362 for (i = 0; i < vol->extent_no; i++) {
363 fibril_rwlock_read_lock(&vol->states_lock);
364 hr_ext_state_t state = vol->extents[i].state;
365 fibril_rwlock_read_unlock(&vol->states_lock);
366
367 if (state != HR_EXT_ONLINE &&
368 (state != HR_EXT_REBUILD ||
369 ba + cnt - 1 >= rebuild_blk)) {
370 continue;
371 }
372
373 rc = hr_read_direct(vol->extents[i].svc_id, ba, cnt,
374 data_read);
375 if (rc != EOK) {
376 hr_raid1_ext_state_cb(vol, i, rc);
377 } else {
378 successful++;
379 break;
380 }
381 }
382 break;
383 case HR_BD_WRITE:
384 rl = hr_range_lock_acquire(vol, ba, cnt);
385
386 fibril_rwlock_read_lock(&vol->states_lock);
387
388 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
389 memory_order_relaxed);
390
391 size_t good = hr_raid1_count_good_extents(vol, ba, cnt,
392 rebuild_blk);
393
394 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
395
396 for (i = 0; i < vol->extent_no; i++) {
397 if (vol->extents[i].state != HR_EXT_ONLINE &&
398 (vol->extents[i].state != HR_EXT_REBUILD ||
399 ba > rebuild_blk)) {
400 /*
401 * When the extent is being rebuilt,
402 * we only write to the part that is already
403 * rebuilt. If IO starts after vol->rebuild_blk
404 * we do not proceed, the write is going to
405 * be replicated later in the rebuild.
406 */
407 continue;
408 }
409
410 hr_io_t *io = hr_fgroup_alloc(group);
411 io->extent = i;
412 io->data_write = data_write;
413 io->data_read = data_read;
414 io->ba = ba;
415 io->cnt = cnt;
416 io->type = type;
417 io->vol = vol;
418
419 hr_fgroup_submit(group, hr_io_worker, io);
420 }
421
422 fibril_rwlock_read_unlock(&vol->states_lock);
423
424 (void)hr_fgroup_wait(group, &successful, NULL);
425
426 hr_range_lock_release(rl);
427
428 break;
429 default:
430 assert(0);
431 }
432
433 if (successful > 0)
434 rc = EOK;
435 else
436 rc = EIO;
437
438 fibril_rwlock_read_unlock(&vol->extents_lock);
439
440 hr_raid1_vol_state_eval(vol);
441
442 return rc;
443}
444
445/*
446 * Put the last HOTSPARE extent in place
447 * of first that != ONLINE, and start the rebuild.
448 */
449static errno_t hr_raid1_rebuild(void *arg)
450{
451 HR_DEBUG("%s()", __func__);
452
453 hr_volume_t *vol = arg;
454 void *buf = NULL;
455 size_t rebuild_idx;
456 hr_extent_t *rebuild_ext = NULL;
457 errno_t rc;
458
459 rc = hr_init_rebuild(vol, &rebuild_idx);
460 if (rc != EOK)
461 return rc;
462
463 rebuild_ext = &vol->extents[rebuild_idx];
464
465 size_t left = vol->data_blkno;
466 size_t max_blks = DATA_XFER_LIMIT / vol->bsize;
467 buf = hr_malloc_waitok(max_blks * vol->bsize);
468
469 size_t cnt;
470 uint64_t ba = 0;
471 hr_add_data_offset(vol, &ba);
472
473 /*
474 * this is not necessary because a rebuild is
475 * protected by itself, i.e. there can be only
476 * one REBUILD at a time
477 */
478 fibril_rwlock_read_lock(&vol->extents_lock);
479
480 /* increment metadata counter only on first write */
481 bool exp = false;
482 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
483 vol->meta_ops->inc_counter(vol);
484 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
485 }
486
487 hr_range_lock_t *rl = NULL;
488
489 unsigned int percent, old_percent = 100;
490 while (left != 0) {
491 cnt = min(max_blks, left);
492
493 rl = hr_range_lock_acquire(vol, ba, cnt);
494
495 atomic_store_explicit(&vol->rebuild_blk, ba,
496 memory_order_relaxed);
497
498 rc = hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL,
499 cnt * vol->bsize);
500 if (rc != EOK) {
501 hr_range_lock_release(rl);
502 goto end;
503 }
504
505 rc = hr_write_direct(rebuild_ext->svc_id, ba, cnt, buf);
506 if (rc != EOK) {
507 hr_raid1_ext_state_cb(vol, rebuild_idx, rc);
508 hr_range_lock_release(rl);
509 goto end;
510 }
511
512 percent = ((ba + cnt) * 100) / vol->data_blkno;
513 if (percent != old_percent) {
514 if (percent % 5 == 0)
515 HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
516 vol->devname, percent);
517 }
518
519 hr_range_lock_release(rl);
520
521 ba += cnt;
522 left -= cnt;
523 old_percent = percent;
524 }
525
526 HR_DEBUG("hr_raid1_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
527 "extent no. %zu\n", vol->devname, vol->svc_id, rebuild_idx);
528
529 fibril_rwlock_write_lock(&vol->states_lock);
530
531 hr_update_ext_state(vol, rebuild_idx, HR_EXT_ONLINE);
532
533 hr_mark_vol_state_dirty(vol);
534
535 fibril_rwlock_write_unlock(&vol->states_lock);
536
537 /* (void)vol->meta_ops->save(vol, WITH_STATE_CALLBACK); */
538
539end:
540 fibril_rwlock_read_unlock(&vol->extents_lock);
541
542 hr_raid1_vol_state_eval(vol);
543
544 if (buf != NULL)
545 free(buf);
546
547 return rc;
548}
549
550/** @}
551 */
Note: See TracBrowser for help on using the repository browser.