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

Last change on this file since 6aafb48 was 6aafb48, checked in by Miroslav Cimerman <mc@…>, 5 weeks ago

hr: rebuild: fix deadlock on extents_lock

  • Property mode set to 100644
File size: 13.7 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
135void hr_raid1_vol_state_eval(hr_volume_t *vol)
136{
137 HR_DEBUG("%s()", __func__);
138
139 bool exp = true;
140 if (!atomic_compare_exchange_strong(&vol->state_dirty, &exp, false))
141 return;
142
143 vol->meta_ops->inc_counter(vol);
144 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
145
146 hr_raid1_vol_state_eval_forced(vol);
147}
148
149void hr_raid1_ext_state_cb(hr_volume_t *vol, size_t extent, errno_t rc)
150{
151 HR_DEBUG("%s()", __func__);
152
153 assert(fibril_rwlock_is_locked(&vol->extents_lock));
154
155 if (rc == EOK)
156 return;
157
158 fibril_rwlock_write_lock(&vol->states_lock);
159
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__);
176
177 fibril_rwlock_read_lock(&vol->extents_lock);
178 fibril_rwlock_read_lock(&vol->states_lock);
179
180 hr_vol_state_t old_state = vol->state;
181 size_t healthy = hr_count_extents(vol, HR_EXT_ONLINE);
182
183 size_t invalid_no = hr_count_extents(vol, HR_EXT_INVALID);
184
185 size_t rebuild_no = hr_count_extents(vol, HR_EXT_REBUILD);
186
187 fibril_rwlock_read_unlock(&vol->states_lock);
188 fibril_rwlock_read_unlock(&vol->extents_lock);
189
190 fibril_mutex_lock(&vol->hotspare_lock);
191 size_t hs_no = vol->hotspare_no;
192 fibril_mutex_unlock(&vol->hotspare_lock);
193
194 if (healthy == 0) {
195 if (old_state != HR_VOL_FAULTY) {
196 fibril_rwlock_write_lock(&vol->states_lock);
197 hr_update_vol_state(vol, HR_VOL_FAULTY);
198 fibril_rwlock_write_unlock(&vol->states_lock);
199 }
200 } else if (healthy < vol->extent_no) {
201 if (old_state != HR_VOL_REBUILD &&
202 old_state != HR_VOL_DEGRADED) {
203 fibril_rwlock_write_lock(&vol->states_lock);
204 hr_update_vol_state(vol, HR_VOL_DEGRADED);
205 fibril_rwlock_write_unlock(&vol->states_lock);
206 }
207
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);
214 }
215 } else {
216 if (old_state != HR_VOL_OPTIMAL) {
217 fibril_rwlock_write_lock(&vol->states_lock);
218 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
219 fibril_rwlock_write_unlock(&vol->states_lock);
220 }
221 }
222}
223
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{
248 hr_volume_t *vol = bd->srvs->sarg;
249
250 return hr_sync_extents(vol);
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{
256 hr_volume_t *vol = bd->srvs->sarg;
257
258 return hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL, size);
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{
264 hr_volume_t *vol = bd->srvs->sarg;
265
266 if (vol->vflags & HR_VOL_FLAG_READ_ONLY)
267 return ENOTSUP;
268
269 return hr_raid1_bd_op(HR_BD_WRITE, vol, ba, cnt, NULL, data, size);
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
288static size_t hr_raid1_count_good_extents(hr_volume_t *vol, uint64_t ba,
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++) {
296 if (vol->extents[i].state == HR_EXT_ONLINE ||
297 (vol->extents[i].state == HR_EXT_REBUILD &&
298 rebuild_blk >= ba)) {
299 count++;
300 }
301 }
302
303 return count;
304}
305
306static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, hr_volume_t *vol,
307 aoff64_t ba, size_t cnt, void *data_read, const void *data_write,
308 size_t size)
309{
310 HR_DEBUG("%s()", __func__);
311
312 hr_range_lock_t *rl = NULL;
313 errno_t rc;
314 size_t i;
315 uint64_t rebuild_blk;
316
317 if (size < cnt * vol->bsize)
318 return EINVAL;
319
320 fibril_rwlock_read_lock(&vol->states_lock);
321 hr_vol_state_t vol_state = vol->state;
322 fibril_rwlock_read_unlock(&vol->states_lock);
323
324 if (vol_state == HR_VOL_FAULTY || vol_state == HR_VOL_NONE)
325 return EIO;
326
327 /* increment metadata counter only on first write */
328 bool exp = false;
329 if (type == HR_BD_WRITE &&
330 atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
331 vol->meta_ops->inc_counter(vol);
332 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
333 }
334
335 rc = hr_check_ba_range(vol, cnt, ba);
336 if (rc != EOK)
337 return rc;
338
339 hr_add_data_offset(vol, &ba);
340
341 /*
342 * extent order has to be locked for the whole IO duration,
343 * so that workers have consistent targets
344 */
345 fibril_rwlock_read_lock(&vol->extents_lock);
346
347 size_t successful = 0;
348 switch (type) {
349 case HR_BD_READ:
350 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
351 memory_order_relaxed);
352
353 for (i = 0; i < vol->extent_no; i++) {
354 fibril_rwlock_read_lock(&vol->states_lock);
355 hr_ext_state_t state = vol->extents[i].state;
356 fibril_rwlock_read_unlock(&vol->states_lock);
357
358 if (state != HR_EXT_ONLINE &&
359 (state != HR_EXT_REBUILD ||
360 ba + cnt - 1 >= rebuild_blk)) {
361 continue;
362 }
363
364 rc = hr_read_direct(vol->extents[i].svc_id, ba, cnt,
365 data_read);
366 if (rc != EOK) {
367 hr_raid1_ext_state_cb(vol, i, rc);
368 } else {
369 successful++;
370 break;
371 }
372 }
373 break;
374 case HR_BD_WRITE:
375 rl = hr_range_lock_acquire(vol, ba, cnt);
376
377 fibril_rwlock_read_lock(&vol->states_lock);
378
379 rebuild_blk = atomic_load_explicit(&vol->rebuild_blk,
380 memory_order_relaxed);
381
382 size_t good = hr_raid1_count_good_extents(vol, ba, cnt,
383 rebuild_blk);
384
385 hr_fgroup_t *group = hr_fgroup_create(vol->fge, good);
386
387 for (i = 0; i < vol->extent_no; i++) {
388 if (vol->extents[i].state != HR_EXT_ONLINE &&
389 (vol->extents[i].state != HR_EXT_REBUILD ||
390 ba > rebuild_blk)) {
391 /*
392 * When the extent is being rebuilt,
393 * we only write to the part that is already
394 * rebuilt. If IO starts after vol->rebuild_blk
395 * we do not proceed, the write is going to
396 * be replicated later in the rebuild.
397 */
398 continue;
399 }
400
401 hr_io_t *io = hr_fgroup_alloc(group);
402 io->extent = i;
403 io->data_write = data_write;
404 io->data_read = data_read;
405 io->ba = ba;
406 io->cnt = cnt;
407 io->type = type;
408 io->vol = vol;
409
410 hr_fgroup_submit(group, hr_io_worker, io);
411 }
412
413 fibril_rwlock_read_unlock(&vol->states_lock);
414
415 (void)hr_fgroup_wait(group, &successful, NULL);
416
417 hr_range_lock_release(rl);
418
419 break;
420 default:
421 assert(0);
422 }
423
424 if (successful > 0)
425 rc = EOK;
426 else
427 rc = EIO;
428
429 fibril_rwlock_read_unlock(&vol->extents_lock);
430
431 hr_raid1_vol_state_eval(vol);
432
433 return rc;
434}
435
436static errno_t hr_raid1_rebuild(void *arg)
437{
438 HR_DEBUG("%s()", __func__);
439
440 hr_volume_t *vol = arg;
441 void *buf = NULL;
442 size_t rebuild_idx;
443 hr_extent_t *rebuild_ext = NULL;
444 errno_t rc;
445
446 if (vol->vflags & HR_VOL_FLAG_READ_ONLY)
447 return ENOTSUP;
448 if (!(vol->meta_ops->get_flags() & HR_METADATA_ALLOW_REBUILD))
449 return ENOTSUP;
450
451 rc = hr_init_rebuild(vol, &rebuild_idx);
452 if (rc != EOK)
453 return rc;
454
455 rebuild_ext = &vol->extents[rebuild_idx];
456
457 size_t left = vol->data_blkno - vol->rebuild_blk;
458 size_t max_blks = DATA_XFER_LIMIT / vol->bsize;
459 buf = hr_malloc_waitok(max_blks * vol->bsize);
460
461 size_t cnt;
462 uint64_t ba = vol->rebuild_blk;
463 hr_add_data_offset(vol, &ba);
464
465 /*
466 * this is not necessary because a rebuild is
467 * protected by itself, i.e. there can be only
468 * one REBUILD at a time
469 */
470 fibril_rwlock_read_lock(&vol->extents_lock);
471
472 /* increment metadata counter only on first write */
473 bool exp = false;
474 if (atomic_compare_exchange_strong(&vol->first_write, &exp, true)) {
475 vol->meta_ops->inc_counter(vol);
476 vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
477 }
478
479 hr_range_lock_t *rl = NULL;
480
481 HR_NOTE("\"%s\": REBUILD started on extent no. %zu at block %lu.\n",
482 vol->devname, rebuild_idx, ba);
483
484 uint64_t written = 0;
485 unsigned int percent, old_percent = 100;
486 while (left != 0) {
487 cnt = min(max_blks, left);
488
489 rl = hr_range_lock_acquire(vol, ba, cnt);
490
491 atomic_store_explicit(&vol->rebuild_blk, ba,
492 memory_order_relaxed);
493
494 rc = hr_raid1_bd_op(HR_BD_READ, vol, ba, cnt, buf, NULL,
495 cnt * vol->bsize);
496 if (rc != EOK) {
497 hr_range_lock_release(rl);
498 goto end;
499 }
500
501 rc = hr_write_direct(rebuild_ext->svc_id, ba, cnt, buf);
502 if (rc != EOK) {
503 hr_raid1_ext_state_cb(vol, rebuild_idx, rc);
504 hr_range_lock_release(rl);
505 goto end;
506 }
507
508 percent = ((ba + cnt) * 100) / vol->data_blkno;
509 if (percent != old_percent) {
510 if (percent % 5 == 0)
511 HR_DEBUG("\"%s\" REBUILD progress: %u%%\n",
512 vol->devname, percent);
513 }
514
515 if (written * vol->bsize > HR_REBUILD_SAVE_BYTES) {
516 vol->meta_ops->save_ext(vol, rebuild_idx,
517 WITH_STATE_CALLBACK);
518 written = 0;
519 }
520
521 hr_range_lock_release(rl);
522
523 written += cnt;
524 ba += cnt;
525 left -= cnt;
526 old_percent = percent;
527 }
528
529 HR_DEBUG("hr_raid1_rebuild(): rebuild finished on \"%s\" (%" PRIun "), "
530 "extent no. %zu\n", vol->devname, vol->svc_id, rebuild_idx);
531
532 fibril_rwlock_write_lock(&vol->states_lock);
533
534 hr_update_ext_state(vol, rebuild_idx, HR_EXT_ONLINE);
535
536 atomic_store_explicit(&vol->rebuild_blk, 0, memory_order_relaxed);
537
538 hr_mark_vol_state_dirty(vol);
539
540 hr_update_vol_state(vol, HR_VOL_DEGRADED);
541
542 fibril_rwlock_write_unlock(&vol->states_lock);
543end:
544 fibril_rwlock_read_unlock(&vol->extents_lock);
545
546 hr_raid1_vol_state_eval(vol);
547
548 free(buf);
549
550 return rc;
551}
552
553/** @}
554 */
Note: See TracBrowser for help on using the repository browser.