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

Last change on this file was 09e01d23, checked in by Miroslav Cimerman <mc@…>, 13 days ago

hr: type fixes for 32-bit archs

  • Property mode set to 100644
File size: 18.0 KB
RevLine 
[94d84a0]1/*
[58d82fa]2 * Copyright (c) 2025 Miroslav Cimerman
[94d84a0]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
[50603405]36#include <abi/ipc/ipc.h>
[94d84a0]37#include <bd_srv.h>
38#include <block.h>
39#include <errno.h>
40#include <hr.h>
[ca7fa5b]41#include <inttypes.h>
[94d84a0]42#include <io/log.h>
43#include <ipc/hr.h>
44#include <ipc/services.h>
45#include <loc.h>
46#include <task.h>
[58d82fa]47#include <stdatomic.h>
[94d84a0]48#include <stdio.h>
49#include <stdlib.h>
50#include <str_error.h>
51
[58d82fa]52#include "fge.h"
53#include "io.h"
[6b8e89b0]54#include "superblock.h"
[da5c257]55#include "util.h"
[b0f1366]56#include "var.h"
[94d84a0]57
[7fba146]58static void hr_raid1_vol_state_eval_forced(hr_volume_t *);
[974f9ba]59static size_t hr_raid1_count_good_w_extents(hr_volume_t *, uint64_t, size_t,
[58d82fa]60 uint64_t);
[13f4c85]61static errno_t hr_raid1_bd_op(hr_bd_op_type_t, hr_volume_t *, aoff64_t, size_t,
[733564a]62 void *, const void *, size_t);
[6f13257]63static errno_t hr_raid1_rebuild(void *);
[733564a]64
65/* bdops */
[6f13257]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 *,
[94d84a0]69 size_t);
[6f13257]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,
[94d84a0]72 const void *, size_t);
[6f13257]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 *);
[94d84a0]75
76static bd_ops_t hr_raid1_bd_ops = {
[6f13257]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
[94d84a0]84};
85
[6d0fc11]86extern loc_srv_t *hr_srv;
87
[733564a]88errno_t hr_raid1_create(hr_volume_t *new_volume)
89{
[baa4929]90 HR_DEBUG("%s()", __func__);
91
[b5c95da5]92 if (new_volume->level != HR_LVL_1)
93 return EINVAL;
[733564a]94
[65706f1]95 if (new_volume->extent_no < 2) {
[af73327a]96 HR_ERROR("RAID 1 volume needs at least 2 devices\n");
[733564a]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
[7fba146]104 hr_raid1_vol_state_eval_forced(new_volume);
[23df41b]105
106 fibril_rwlock_read_lock(&new_volume->states_lock);
[56602e0]107 hr_vol_state_t state = new_volume->state;
[23df41b]108 fibril_rwlock_read_unlock(&new_volume->states_lock);
[18c3658]109 if (state == HR_VOL_FAULTY || state == HR_VOL_NONE) {
110 HR_NOTE("\"%s\": unusable state, not creating\n",
111 new_volume->devname);
[58d82fa]112 return EINVAL;
[18c3658]113 }
[58d82fa]114
[8a65373]115 return EOK;
[733564a]116}
117
[746e636]118/*
119 * Called only once in volume's lifetime.
120 */
[733564a]121errno_t hr_raid1_init(hr_volume_t *vol)
122{
[baa4929]123 HR_DEBUG("%s()", __func__);
[733564a]124
[b5c95da5]125 if (vol->level != HR_LVL_1)
126 return EINVAL;
[733564a]127
[50603405]128 vol->data_offset = vol->meta_ops->get_data_offset();
[80c760e]129 vol->data_blkno = vol->truncated_blkno - vol->meta_ops->get_size();
[733564a]130 vol->strip_size = 0;
131
132 return EOK;
133}
134
[da80de9]135void hr_raid1_vol_state_eval(hr_volume_t *vol)
[733564a]136{
[7a80c63]137 HR_DEBUG("%s()", __func__);
138
[401b9e42]139 bool exp = true;
[d2da1be]140 if (!atomic_compare_exchange_strong(&vol->state_dirty, &exp, false))
[401b9e42]141 return;
[5ee041e]142
[7fba146]143 vol->meta_ops->inc_counter(vol);
[e0695ce]144 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
[800d188]145
[7fba146]146 hr_raid1_vol_state_eval_forced(vol);
147}
148
[234212a]149void hr_raid1_ext_state_cb(hr_volume_t *vol, size_t extent, errno_t rc)
[7fba146]150{
151 HR_DEBUG("%s()", __func__);
152
[234212a]153 assert(fibril_rwlock_is_locked(&vol->extents_lock));
154
[7fba146]155 if (rc == EOK)
156 return;
157
158 fibril_rwlock_write_lock(&vol->states_lock);
[800d188]159
[7fba146]160 switch (rc) {
161 case ENOENT:
162 hr_update_ext_state(vol, extent, HR_EXT_MISSING);
163 break;
164 default:
165 hr_update_ext_state(vol, extent, HR_EXT_FAILED);
166 }
167
168 hr_mark_vol_state_dirty(vol);
169
170 fibril_rwlock_write_unlock(&vol->states_lock);
171}
172
173static void hr_raid1_vol_state_eval_forced(hr_volume_t *vol)
174{
175 HR_DEBUG("%s()", __func__);
[800d188]176
[58d82fa]177 fibril_rwlock_read_lock(&vol->extents_lock);
178 fibril_rwlock_read_lock(&vol->states_lock);
[d84773a]179
[56602e0]180 hr_vol_state_t old_state = vol->state;
[5b320ac]181 size_t healthy = hr_count_extents(vol, HR_EXT_ONLINE);
[d84773a]182
[13f4c85]183 size_t invalid_no = hr_count_extents(vol, HR_EXT_INVALID);
184
[e0695ce]185 size_t rebuild_no = hr_count_extents(vol, HR_EXT_REBUILD);
186
[6aafb48]187 fibril_rwlock_read_unlock(&vol->states_lock);
188 fibril_rwlock_read_unlock(&vol->extents_lock);
189
[13f4c85]190 fibril_mutex_lock(&vol->hotspare_lock);
191 size_t hs_no = vol->hotspare_no;
192 fibril_mutex_unlock(&vol->hotspare_lock);
193
[d84773a]194 if (healthy == 0) {
[58d82fa]195 if (old_state != HR_VOL_FAULTY) {
196 fibril_rwlock_write_lock(&vol->states_lock);
[56602e0]197 hr_update_vol_state(vol, HR_VOL_FAULTY);
[58d82fa]198 fibril_rwlock_write_unlock(&vol->states_lock);
199 }
[65706f1]200 } else if (healthy < vol->extent_no) {
[dec4150]201 if (old_state != HR_VOL_REBUILD &&
202 old_state != HR_VOL_DEGRADED) {
203 fibril_rwlock_write_lock(&vol->states_lock);
[56602e0]204 hr_update_vol_state(vol, HR_VOL_DEGRADED);
[dec4150]205 fibril_rwlock_write_unlock(&vol->states_lock);
206 }
[a0c3080]207
[6aafb48]208 if (hs_no > 0 || invalid_no > 0 || rebuild_no > 0) {
209 fid_t fib = fibril_create(hr_raid1_rebuild, vol);
210 if (fib == 0)
211 return;
212 fibril_start(fib);
213 fibril_detach(fib);
[d84773a]214 }
215 } else {
[263a2389]216 if (old_state != HR_VOL_OPTIMAL) {
[58d82fa]217 fibril_rwlock_write_lock(&vol->states_lock);
[263a2389]218 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
[58d82fa]219 fibril_rwlock_write_unlock(&vol->states_lock);
220 }
[d84773a]221 }
222}
223
[da80de9]224static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
225{
226 HR_DEBUG("%s()", __func__);
227
228 hr_volume_t *vol = bd->srvs->sarg;
229
230 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
231
232 return EOK;
233}
234
235static errno_t hr_raid1_bd_close(bd_srv_t *bd)
236{
237 HR_DEBUG("%s()", __func__);
238
239 hr_volume_t *vol = bd->srvs->sarg;
240
241 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
242
243 return EOK;
244}
245
246static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
247{
[137f7cf5]248 hr_volume_t *vol = bd->srvs->sarg;
249
250 return hr_sync_extents(vol);
[da80de9]251}
252
253static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
254 void *buf, size_t size)
255{
[13f4c85]256 hr_volume_t *vol = bd->srvs->sarg;
257
258 return hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL, size);
[da80de9]259}
260
261static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
262 const void *data, size_t size)
263{
[13f4c85]264 hr_volume_t *vol = bd->srvs->sarg;
265
[95ca19d]266 if (vol->vflags & HR_VOL_FLAG_READ_ONLY)
267 return ENOTSUP;
268
[13f4c85]269 return hr_raid1_bd_op(HR_BD_WRITE, vol, ba, cnt, NULL, data, size);
[da80de9]270}
271
272static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
273{
274 hr_volume_t *vol = bd->srvs->sarg;
275
276 *rsize = vol->bsize;
277 return EOK;
278}
279
280static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
281{
282 hr_volume_t *vol = bd->srvs->sarg;
283
284 *rnb = vol->data_blkno;
285 return EOK;
286}
287
[974f9ba]288static size_t hr_raid1_count_good_w_extents(hr_volume_t *vol, uint64_t ba,
[58d82fa]289 size_t cnt, uint64_t rebuild_blk)
290{
291 assert(fibril_rwlock_is_locked(&vol->extents_lock));
292 assert(fibril_rwlock_is_locked(&vol->states_lock));
293
294 size_t count = 0;
295 for (size_t i = 0; i < vol->extent_no; i++) {
[56602e0]296 if (vol->extents[i].state == HR_EXT_ONLINE ||
297 (vol->extents[i].state == HR_EXT_REBUILD &&
[b22ea202]298 rebuild_blk >= ba)) {
[58d82fa]299 count++;
300 }
301 }
302
303 return count;
[d84773a]304}
305
[974f9ba]306#ifdef HR_RAID1_READ_STRATEGY_SPLIT
[c76bf33]307
[974f9ba]308static size_t hr_raid1_count_good_r_extents(hr_volume_t *vol, uint64_t ba,
309 size_t cnt, uint64_t rebuild_blk)
310{
311 assert(fibril_rwlock_is_locked(&vol->extents_lock));
312 assert(fibril_rwlock_is_locked(&vol->states_lock));
313
314 size_t count = 0;
315 for (size_t i = 0; i < vol->extent_no; i++) {
316 if (vol->extents[i].state == HR_EXT_ONLINE ||
317 (vol->extents[i].state == HR_EXT_REBUILD &&
318 rebuild_blk > ba + cnt - 1)) {
319 count++;
320 }
321 }
322
323 return count;
324}
[c76bf33]325
326#endif /* HR_RAID1_READ_STRATEGY_SPLIT */
[974f9ba]327
328#ifdef HR_RAID1_READ_STRATEGY_CLOSEST
[c76bf33]329
[974f9ba]330static size_t get_ext(hr_volume_t *vol, uint64_t ba, size_t cnt,
331 uint64_t rebuild_blk)
332{
333 uint64_t closest_e;
334 uint64_t pos;
335 uint64_t mdiff = UINT64_MAX;
336 hr_ext_state_t state = vol->extents[0].state;
337 if (state != HR_EXT_ONLINE &&
338 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
339 closest_e = 1;
340 } else {
341 closest_e = 0;
342 pos = atomic_load_explicit(&vol->last_ext_pos_arr[0],
343 memory_order_relaxed);
344 mdiff = (pos > ba) ? pos - ba : ba - pos;
345 }
346
347 for (size_t e = 1; e < vol->extent_no; e++) {
348 state = vol->extents[e].state;
349 if (state != HR_EXT_ONLINE &&
350 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
351 continue;
352 }
353
354 pos = atomic_load_explicit(&vol->last_ext_pos_arr[e],
355 memory_order_relaxed);
356 uint64_t diff = (pos > ba) ? pos - ba : ba - pos;
357 if (diff < mdiff) {
358 mdiff = diff;
359 closest_e = e;
360 }
361 }
362
363 return closest_e;
364}
365
366#elif defined(HR_RAID1_READ_STRATEGY_ROUND_ROBIN)
367
368static size_t get_ext(hr_volume_t *vol, uint64_t ba, size_t cnt,
369 uint64_t rebuild_blk)
370{
371 size_t last_e;
372 size_t fail = 0;
373
374 while (true) {
375 last_e = atomic_fetch_add_explicit(&vol->last_ext_used, 1,
376 memory_order_relaxed);
377 last_e %= vol->extent_no;
378
379 hr_ext_state_t state = vol->extents[last_e].state;
380 if (state != HR_EXT_ONLINE &&
381 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
382 if (++fail >= vol->extent_no)
383 return vol->extent_no;
384 continue;
385 }
386
387 break;
388 }
389
390 return last_e;
391}
392
393#elif defined(HR_RAID1_READ_STRATEGY_FIRST)
394
395static size_t get_ext(hr_volume_t *vol, uint64_t ba, size_t cnt,
396 uint64_t rebuild_blk)
397{
398 for (size_t e = 0; e < vol->extent_no; e++) {
399 hr_ext_state_t state = vol->extents[e].state;
400 if (state != HR_EXT_ONLINE &&
401 (state != HR_EXT_REBUILD || ba + cnt - 1 >= rebuild_blk)) {
402 continue;
403 }
404
405 return e;
406 }
407 return vol->extent_no;
408}
409
410#else
411
412#if !defined(HR_RAID1_READ_STRATEGY_SPLIT) || \
413 !defined(HR_RAID1_READ_STRATEGY_SPLIT_THRESHOLD)
414#error "Some RAID 1 read strategy must be used"
415#endif
416
417#endif
418
419static size_t hr_raid1_read(hr_volume_t *vol, uint64_t ba, size_t cnt,
420 void *data_read)
421{
422 uint64_t rebuild_blk;
423 size_t successful = 0;
424
425#if !defined(HR_RAID1_READ_STRATEGY_SPLIT)
426 errno_t rc;
427
428 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
429 memory_order_relaxed);
430 size_t fail = 0;
431 while (fail < vol->extent_no) {
432 fibril_rwlock_read_lock(&vol->states_lock);
433 size_t best_e = get_ext(vol, ba, cnt,
434 rebuild_blk);
435 fibril_rwlock_read_unlock(&vol->states_lock);
436 if (best_e >= vol->extent_no)
437 break;
438 rc = hr_read_direct(vol->extents[best_e].svc_id, ba,
439 cnt, data_read);
440 if (rc != EOK) {
441 hr_raid1_ext_state_cb(vol, best_e, rc);
442 fail++;
443 } else {
444 successful++;
445 break;
446 }
447 }
448
449#else
450
451retry_split:
452 size_t good;
453 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
454 memory_order_relaxed);
455
456 fibril_rwlock_read_lock(&vol->states_lock);
457 good = hr_raid1_count_good_r_extents(vol, ba, cnt,
458 rebuild_blk);
459 fibril_rwlock_read_unlock(&vol->states_lock);
460
[1162b6c]461 if (good == 0)
462 return 0;
463
[974f9ba]464 size_t cnt_per_ext = (cnt + good - 1) / good;
465 if (cnt_per_ext * vol->bsize < HR_RAID1_READ_STRATEGY_SPLIT_THRESHOLD)
466 cnt_per_ext = cnt;
467
468 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
469
470 fibril_rwlock_read_lock(&vol->states_lock);
471 size_t left = cnt;
472 size_t e = 0;
473 uint8_t *data = data_read;
474 size_t submitted = 0;
475 while (left > 0) {
476 if (e >= vol->extent_no) {
477 fibril_rwlock_read_unlock(&vol->states_lock);
478 if (submitted)
479 (void)hr_fgroup_wait(group, NULL, NULL);
480 goto retry_split;
481 }
482
483 hr_ext_state_t state = vol->extents[e].state;
484 if (state != HR_EXT_ONLINE &&
485 (state != HR_EXT_REBUILD ||
486 ba + cnt - 1 >= rebuild_blk)) {
487 e++;
488 continue;
489 }
490
491 hr_io_t *io = hr_fgroup_alloc(group);
492 io->extent = e;
493 io->data_read = data;
494 io->ba = ba + (cnt - left);
495 size_t cnt_to_dispatch = min(left, cnt_per_ext);
496 io->cnt = cnt_to_dispatch;
497 io->type = HR_BD_READ;
498 io->vol = vol;
499
500 hr_fgroup_submit(group, hr_io_worker, io);
501 submitted++;
502
503 data += cnt_to_dispatch * vol->bsize;
504 left -= cnt_to_dispatch;
505 e++;
506 }
507
508 fibril_rwlock_read_unlock(&vol->states_lock);
509
510 (void)hr_fgroup_wait(group, &successful, NULL);
[3b14bda]511 if (successful < submitted)
512 goto retry_split;
513
[974f9ba]514#endif
515
516 return successful;
517}
518
[13f4c85]519static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, hr_volume_t *vol,
520 aoff64_t ba, size_t cnt, void *data_read, const void *data_write,
521 size_t size)
[94d84a0]522{
[da80de9]523 HR_DEBUG("%s()", __func__);
524
[58d82fa]525 hr_range_lock_t *rl = NULL;
[94d84a0]526 errno_t rc;
[58d82fa]527 uint64_t rebuild_blk;
528
[234212a]529 if (size < cnt * vol->bsize)
530 return EINVAL;
531
[58d82fa]532 fibril_rwlock_read_lock(&vol->states_lock);
[56602e0]533 hr_vol_state_t vol_state = vol->state;
[58d82fa]534 fibril_rwlock_read_unlock(&vol->states_lock);
535
[e24c064]536 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
[58d82fa]537 return EIO;
[94d84a0]538
[e0622a6]539 /* increment metadata counter only on first write */
540 bool exp = false;
541 if (type == HR_BD_WRITE &&
[234212a]542 atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
[7fba146]543 vol->meta_ops->inc_counter(vol);
[e0622a6]544 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
[7fba146]545 }
546
[4a2a6b8b]547 rc = hr_check_ba_range(vol, cnt, ba);
548 if (rc != EOK)
[b0f1366]549 return rc;
[4a2a6b8b]550
[3c518fc]551 hr_add_data_offset(vol, &ba);
[4a2a6b8b]552
[58d82fa]553 /*
554 * extent order has to be locked for the whole IO duration,
555 * so that workers have consistent targets
556 */
557 fibril_rwlock_read_lock(&vol->extents_lock);
[d84773a]558
[974f9ba]559 size_t good;
560 size_t successful;
[fad91b9]561 switch (type) {
562 case HR_BD_READ:
[974f9ba]563 successful = hr_raid1_read(vol, ba, cnt, data_read);
[fad91b9]564 break;
565 case HR_BD_WRITE:
[f0360ec]566 rl = hr_range_lock_acquire(vol, ba, cnt);
[58d82fa]567
568 fibril_rwlock_read_lock(&vol->states_lock);
569
570 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
571 memory_order_relaxed);
572
[974f9ba]573 good = hr_raid1_count_good_w_extents(vol, ba, cnt,
[58d82fa]574 rebuild_blk);
575
576 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
577
[974f9ba]578 for (size_t i = 0; i < vol->extent_no; i++) {
[56602e0]579 if (vol->extents[i].state != HR_EXT_ONLINE &&
580 (vol->extents[i].state != HR_EXT_REBUILD ||
[b22ea202]581 ba > rebuild_blk)) {
[b8409b9]582 /*
583 * When the extent is being rebuilt,
584 * we only write to the part that is already
[58d82fa]585 * rebuilt. If IO starts after vol->rebuild_blk
586 * we do not proceed, the write is going to
587 * be replicated later in the rebuild.
[b8409b9]588 */
[d84773a]589 continue;
[58d82fa]590 }
591
592 hr_io_t *io = hr_fgroup_alloc(group);
593 io->extent = i;
594 io->data_write = data_write;
595 io->ba = ba;
596 io->cnt = cnt;
597 io->type = type;
598 io->vol = vol;
599
600 hr_fgroup_submit(group, hr_io_worker, io);
[fad91b9]601 }
[58d82fa]602
603 fibril_rwlock_read_unlock(&vol->states_lock);
604
605 (void)hr_fgroup_wait(group, &successful, NULL);
606
[f0360ec]607 hr_range_lock_release(rl);
[58d82fa]608
[fad91b9]609 break;
610 default:
[f0360ec]611 assert(0);
[94d84a0]612 }
613
[d84773a]614 if (successful > 0)
615 rc = EOK;
616 else
617 rc = EIO;
618
[58d82fa]619 fibril_rwlock_read_unlock(&vol->extents_lock);
620
[da80de9]621 hr_raid1_vol_state_eval(vol);
[58d82fa]622
[94d84a0]623 return rc;
624}
625
[35f2a877]626static errno_t hr_raid1_rebuild(void *arg)
[5b320ac]627{
[a57dde4]628 HR_DEBUG("%s()", __func__);
[5b320ac]629
[35f2a877]630 hr_volume_t *vol = arg;
631 void *buf = NULL;
632 size_t rebuild_idx;
[13f4c85]633 hr_extent_t *rebuild_ext = NULL;
[35f2a877]634 errno_t rc;
[58d82fa]635
[95ca19d]636 if (vol->vflags & HR_VOL_FLAG_READ_ONLY)
637 return ENOTSUP;
[e5c3580]638 if (!(vol->meta_ops->get_flags() & HR_METADATA_ALLOW_REBUILD))
639 return ENOTSUP;
640
[09c195e8]641 rc = hr_init_rebuild(vol, &rebuild_idx);
[35f2a877]642 if (rc != EOK)
[58d82fa]643 return rc;
[35f2a877]644
[13f4c85]645 rebuild_ext = &vol->extents[rebuild_idx];
646
[e0695ce]647 size_t left = vol->data_blkno - vol->rebuild_blk;
[35f2a877]648 size_t max_blks = DATA_XFER_LIMIT / vol->bsize;
[13f4c85]649 buf = hr_malloc_waitok(max_blks * vol->bsize);
[35f2a877]650
651 size_t cnt;
[e0695ce]652 uint64_t ba = vol->rebuild_blk;
[234212a]653 hr_add_data_offset(vol, &ba);
[35f2a877]654
[4d30c475]655 /*
[13f4c85]656 * this is not necessary because a rebuild is
657 * protected by itself, i.e. there can be only
658 * one REBUILD at a time
[4d30c475]659 */
[35f2a877]660 fibril_rwlock_read_lock(&vol->extents_lock);
661
[e0622a6]662 /* increment metadata counter only on first write */
663 bool exp = false;
[234212a]664 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
[e0622a6]665 vol->meta_ops->inc_counter(vol);
666 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
667 }
668
[35f2a877]669 hr_range_lock_t *rl = NULL;
670
[09e01d23]671 HR_NOTE("\"%s\": REBUILD started on extent no. %zu at "
672 "block %" PRIu64 ".\n", vol->devname, rebuild_idx, ba);
[e0695ce]673
674 uint64_t written = 0;
[6123753]675 unsigned int percent, old_percent = 100;
[35f2a877]676 while (left != 0) {
677 cnt = min(max_blks, left);
678
679 rl = hr_range_lock_acquire(vol, ba, cnt);
680
681 atomic_store_explicit(&vol->rebuild_blk, ba,
682 memory_order_relaxed);
683
[13f4c85]684 rc = hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL,
685 cnt * vol->bsize);
686 if (rc != EOK) {
687 hr_range_lock_release(rl);
688 goto end;
689 }
690
691 rc = hr_write_direct(rebuild_ext->svc_id, ba, cnt, buf);
692 if (rc != EOK) {
693 hr_raid1_ext_state_cb(vol, rebuild_idx, rc);
694 hr_range_lock_release(rl);
695 goto end;
696 }
[c76bf33]697 atomic_store_explicit(&vol->last_ext_pos_arr[rebuild_idx],
698 ba + cnt - 1, memory_order_relaxed);
[35f2a877]699
[6123753]700 percent = ((ba + cnt) * 100) / vol->data_blkno;
701 if (percent != old_percent) {
702 if (percent % 5 == 0)
703 HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
704 vol->devname, percent);
705 }
706
[e0695ce]707 if (written * vol->bsize > HR_REBUILD_SAVE_BYTES) {
[6a8c1569]708 vol->meta_ops->save_ext(vol, rebuild_idx,
709 WITH_STATE_CALLBACK);
[e0695ce]710 written = 0;
711 }
712
[35f2a877]713 hr_range_lock_release(rl);
714
[e0695ce]715 written += cnt;
[35f2a877]716 ba += cnt;
717 left -= cnt;
[6123753]718 old_percent = percent;
[58d82fa]719 }
720
[ca7fa5b]721 HR_DEBUG("hr_raid1_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
722 "extent no. %zu\n", vol->devname, vol->svc_id, rebuild_idx);
[58d82fa]723
[35f2a877]724 fibril_rwlock_write_lock(&vol->states_lock);
[58d82fa]725
[56602e0]726 hr_update_ext_state(vol, rebuild_idx, HR_EXT_ONLINE);
[4d30c475]727
[e0695ce]728 atomic_store_explicit(&vol->rebuild_blk, 0, memory_order_relaxed);
729
[d6fe2a1]730 hr_mark_vol_state_dirty(vol);
[58d82fa]731
[6aafb48]732 hr_update_vol_state(vol, HR_VOL_DEGRADED);
733
[35f2a877]734 fibril_rwlock_write_unlock(&vol->states_lock);
735end:
736 fibril_rwlock_read_unlock(&vol->extents_lock);
737
[da80de9]738 hr_raid1_vol_state_eval(vol);
[35f2a877]739
[e0695ce]740 free(buf);
[35f2a877]741
742 return rc;
[58d82fa]743}
744
[94d84a0]745/** @}
746 */
Note: See TracBrowser for help on using the repository browser.