source: mainline/uspace/srv/bd/hr/util.c@ 7fba146

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

hr: RAID 1: inc meta counter on first write

  • Property mode set to 100644
File size: 24.1 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 <adt/list.h>
37#include <block.h>
38#include <errno.h>
39#include <fibril_synch.h>
40#include <hr.h>
41#include <inttypes.h>
42#include <io/log.h>
43#include <loc.h>
44#include <mem.h>
45#include <stdatomic.h>
46#include <stdlib.h>
47#include <stdio.h>
48#include <str.h>
49#include <str_error.h>
50#include <vbd.h>
51
52#include "io.h"
53#include "superblock.h"
54#include "util.h"
55#include "var.h"
56
57static bool hr_range_lock_overlap(hr_range_lock_t *, hr_range_lock_t *);
58static errno_t hr_add_svc_linked_to_list(list_t *, service_id_t, bool, void *);
59static void free_dev_list_member(struct dev_list_member *);
60static void free_svc_id_list(list_t *);
61static errno_t hr_fill_disk_part_svcs_list(list_t *);
62static errno_t block_init_dev_list(list_t *);
63static void block_fini_dev_list(list_t *);
64static errno_t hr_util_get_matching_md_svcs_list(list_t *, list_t *,
65 service_id_t, hr_metadata_type_t, void *);
66static errno_t hr_util_assemble_from_matching_list(list_t *,
67 hr_metadata_type_t);
68static errno_t hr_fill_svcs_list_from_cfg(hr_config_t *, list_t *);
69
70#define HR_RL_LIST_LOCK(vol) (fibril_mutex_lock(&(vol)->range_lock_list_lock))
71#define HR_RL_LIST_UNLOCK(vol) \
72 (fibril_mutex_unlock(&(vol)->range_lock_list_lock))
73
74extern loc_srv_t *hr_srv;
75extern list_t hr_volumes;
76extern fibril_rwlock_t hr_volumes_lock;
77
78errno_t hr_create_vol_struct(hr_volume_t **rvol, hr_level_t level,
79 const char *devname, hr_metadata_type_t metadata_type)
80{
81 HR_DEBUG("%s()", __func__);
82
83 errno_t rc;
84
85 hr_volume_t *vol = calloc(1, sizeof(hr_volume_t));
86 if (vol == NULL)
87 return ENOMEM;
88
89 str_cpy(vol->devname, HR_DEVNAME_LEN, devname);
90 vol->level = level;
91
92 vol->meta_ops = get_type_ops(metadata_type);
93
94 uint8_t meta_flags = vol->meta_ops->get_flags();
95
96 switch (level) {
97 case HR_LVL_0:
98 vol->hr_ops.create = hr_raid0_create;
99 vol->hr_ops.init = hr_raid0_init;
100 vol->hr_ops.vol_state_eval = hr_raid0_vol_state_eval;
101 vol->hr_ops.ext_state_cb = hr_raid0_ext_state_cb;
102 break;
103 case HR_LVL_1:
104 vol->hr_ops.create = hr_raid1_create;
105 vol->hr_ops.init = hr_raid1_init;
106 vol->hr_ops.vol_state_eval = hr_raid1_vol_state_eval;
107 vol->hr_ops.ext_state_cb = hr_raid1_ext_state_cb;
108 if (meta_flags & HR_METADATA_HOTSPARE_SUPPORT)
109 vol->hr_ops.add_hotspare = hr_raid1_add_hotspare;
110 break;
111 case HR_LVL_4:
112 case HR_LVL_5:
113 vol->hr_ops.create = hr_raid5_create;
114 vol->hr_ops.init = hr_raid5_init;
115 vol->hr_ops.vol_state_eval = hr_raid5_vol_state_eval;
116 vol->hr_ops.ext_state_cb = hr_raid5_ext_state_cb;
117 if (meta_flags & HR_METADATA_HOTSPARE_SUPPORT)
118 vol->hr_ops.add_hotspare = hr_raid5_add_hotspare;
119 break;
120 default:
121 HR_DEBUG("unkown level: %d, aborting\n", vol->level);
122 rc = EINVAL;
123 goto error;
124 }
125
126 vol->fge = hr_fpool_create(16, 32, sizeof(hr_io_t));
127 if (vol->fge == NULL) {
128 rc = ENOMEM;
129 goto error;
130 }
131
132 vol->in_mem_md = vol->meta_ops->alloc_struct();
133 if (vol->in_mem_md == NULL) {
134 free(vol->fge);
135 rc = ENOMEM;
136 goto error;
137 }
138
139 vol->state = HR_VOL_NONE;
140
141 fibril_mutex_initialize(&vol->lock); /* XXX: will remove this */
142
143 fibril_mutex_initialize(&vol->md_lock);
144
145 fibril_rwlock_initialize(&vol->extents_lock);
146 fibril_rwlock_initialize(&vol->states_lock);
147
148 fibril_mutex_initialize(&vol->hotspare_lock);
149
150 list_initialize(&vol->range_lock_list);
151 fibril_mutex_initialize(&vol->range_lock_list_lock);
152
153 atomic_init(&vol->state_dirty, false);
154 atomic_init(&vol->data_dirty, false);
155 atomic_init(&vol->rebuild_blk, 0);
156 atomic_init(&vol->open_cnt, 0);
157
158 *rvol = vol;
159
160 return EOK;
161error:
162 free(vol);
163 return rc;
164}
165
166void hr_destroy_vol_struct(hr_volume_t *vol)
167{
168 HR_DEBUG("%s()", __func__);
169
170 if (vol == NULL)
171 return;
172
173 hr_fpool_destroy(vol->fge);
174 hr_fini_devs(vol);
175 free(vol->in_mem_md);
176 free(vol);
177}
178
179errno_t hr_get_volume_svcs(size_t *rcnt, service_id_t **rsvcs)
180{
181 size_t i;
182 service_id_t *vol_svcs;
183
184 if (rcnt == NULL || rsvcs == NULL)
185 return EINVAL;
186
187 fibril_rwlock_read_lock(&hr_volumes_lock);
188
189 size_t vol_cnt = list_count(&hr_volumes);
190 vol_svcs = malloc(vol_cnt * sizeof(service_id_t));
191 if (vol_svcs == NULL) {
192 fibril_rwlock_read_unlock(&hr_volumes_lock);
193 return ENOMEM;
194 }
195
196 i = 0;
197 list_foreach(hr_volumes, lvolumes, hr_volume_t, iter)
198 vol_svcs[i++] = iter->svc_id;
199
200 fibril_rwlock_read_unlock(&hr_volumes_lock);
201
202 *rcnt = vol_cnt;
203 *rsvcs = vol_svcs;
204
205 return EOK;
206}
207
208hr_volume_t *hr_get_volume(service_id_t svc_id)
209{
210 HR_DEBUG("%s()", __func__);
211
212 hr_volume_t *rvol = NULL;
213
214 fibril_rwlock_read_lock(&hr_volumes_lock);
215 list_foreach(hr_volumes, lvolumes, hr_volume_t, iter) {
216 if (iter->svc_id == svc_id) {
217 rvol = iter;
218 break;
219 }
220 }
221 fibril_rwlock_read_unlock(&hr_volumes_lock);
222
223 return rvol;
224}
225
226errno_t hr_remove_volume(service_id_t svc_id)
227{
228 HR_DEBUG("%s()", __func__);
229
230 hr_volume_t *vol = hr_get_volume(svc_id);
231 if (vol == NULL)
232 return ENOENT;
233
234 fibril_rwlock_write_lock(&hr_volumes_lock);
235
236 int open_cnt = atomic_load_explicit(&vol->open_cnt,
237 memory_order_relaxed);
238
239 /*
240 * The atomicity of this if condition (and this whole
241 * operation) is provided by the write lock - no new
242 * bd connection can come, because we need to get the
243 * bd_srvs_t from the volume, which we get from the list.
244 * (see hr_client_conn() in hr.c)
245 */
246 if (open_cnt > 0) {
247 fibril_rwlock_write_unlock(&hr_volumes_lock);
248 return EBUSY;
249 }
250
251 list_remove(&vol->lvolumes);
252
253 fibril_rwlock_write_unlock(&hr_volumes_lock);
254
255 /* save metadata, but we don't care about states anymore */
256 (void)vol->meta_ops->save(vol, NO_STATE_CALLBACK);
257
258 HR_NOTE("deactivating volume \"%s\"\n", vol->devname);
259
260 hr_destroy_vol_struct(vol);
261
262 errno_t rc = loc_service_unregister(hr_srv, svc_id);
263 return rc;
264}
265
266errno_t hr_init_extents_from_cfg(hr_volume_t *vol, hr_config_t *cfg)
267{
268 HR_DEBUG("%s()", __func__);
269
270 errno_t rc;
271 uint64_t blkno, smallest_blkno = ~0ULL;
272 size_t i, bsize;
273 size_t last_bsize = 0;
274
275 for (i = 0; i < cfg->dev_no; i++) {
276 service_id_t svc_id = cfg->devs[i];
277 if (svc_id == 0) {
278 rc = EINVAL;
279 goto error;
280 }
281
282 HR_DEBUG("%s(): block_init() on (%" PRIun ")\n", __func__,
283 svc_id);
284 rc = block_init(svc_id);
285 if (rc != EOK) {
286 HR_DEBUG("%s(): initing (%" PRIun ") failed, "
287 "aborting\n", __func__, svc_id);
288 goto error;
289 }
290
291 rc = block_get_nblocks(svc_id, &blkno);
292 if (rc != EOK)
293 goto error;
294
295 rc = block_get_bsize(svc_id, &bsize);
296 if (rc != EOK)
297 goto error;
298
299 if (last_bsize != 0 && bsize != last_bsize) {
300 HR_DEBUG("block sizes differ\n");
301 rc = EINVAL;
302 goto error;
303 }
304
305 vol->extents[i].svc_id = svc_id;
306 vol->extents[i].state = HR_EXT_ONLINE;
307
308 if (blkno < smallest_blkno)
309 smallest_blkno = blkno;
310 last_bsize = bsize;
311 }
312
313 vol->bsize = last_bsize;
314 vol->extent_no = cfg->dev_no;
315 vol->truncated_blkno = smallest_blkno;
316
317 for (i = 0; i < HR_MAX_HOTSPARES; i++)
318 vol->hotspares[i].state = HR_EXT_MISSING;
319
320 return EOK;
321
322error:
323 for (i = 0; i < HR_MAX_EXTENTS; i++) {
324 if (vol->extents[i].svc_id != 0)
325 block_fini(vol->extents[i].svc_id);
326 }
327
328 return rc;
329}
330
331void hr_fini_devs(hr_volume_t *vol)
332{
333 HR_DEBUG("%s()", __func__);
334
335 size_t i;
336
337 for (i = 0; i < vol->extent_no; i++) {
338 if (vol->extents[i].svc_id != 0) {
339 HR_DEBUG("hr_fini_devs(): block_fini() on "
340 "(%" PRIun ")\n", vol->extents[i].svc_id);
341 block_fini(vol->extents[i].svc_id);
342 }
343 }
344
345 for (i = 0; i < vol->hotspare_no; i++) {
346 if (vol->hotspares[i].svc_id != 0) {
347 HR_DEBUG("hr_fini_devs(): block_fini() on "
348 "(%" PRIun ")\n",
349 vol->hotspares[i].svc_id);
350 block_fini(vol->hotspares[i].svc_id);
351 }
352 }
353}
354
355errno_t hr_register_volume(hr_volume_t *vol)
356{
357 HR_DEBUG("%s()", __func__);
358
359 errno_t rc;
360 service_id_t new_id;
361 category_id_t cat_id;
362 const char *devname = vol->devname;
363
364 rc = loc_service_register(hr_srv, devname, &new_id);
365 if (rc != EOK) {
366 HR_ERROR("unable to register device \"%s\": %s\n",
367 devname, str_error(rc));
368 return rc;
369 }
370
371 rc = loc_category_get_id("raid", &cat_id, IPC_FLAG_BLOCKING);
372 if (rc != EOK) {
373 HR_ERROR("failed resolving category \"raid\": %s\n",
374 str_error(rc));
375 goto error;
376 }
377
378 rc = loc_service_add_to_cat(hr_srv, new_id, cat_id);
379 if (rc != EOK) {
380 HR_ERROR("failed adding \"%s\" to category \"raid\": %s\n",
381 devname, str_error(rc));
382 goto error;
383 }
384
385 vol->svc_id = new_id;
386 return EOK;
387error:
388 rc = loc_service_unregister(hr_srv, new_id);
389 return rc;
390}
391
392errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
393{
394 if (ba + cnt > vol->data_blkno)
395 return ERANGE;
396 return EOK;
397}
398
399void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
400{
401 *ba = *ba + vol->data_offset;
402}
403
404void hr_update_ext_state(hr_volume_t *vol, size_t ext_idx, hr_ext_state_t s)
405{
406 if (vol->level != HR_LVL_0)
407 assert(fibril_rwlock_is_locked(&vol->extents_lock));
408
409 assert(fibril_rwlock_is_write_locked(&vol->states_lock));
410
411 assert(ext_idx < vol->extent_no);
412
413 hr_ext_state_t old = vol->extents[ext_idx].state;
414 HR_NOTE("\"%s\": changing extent %zu state: %s -> %s\n",
415 vol->devname, ext_idx, hr_get_ext_state_str(old),
416 hr_get_ext_state_str(s));
417 vol->extents[ext_idx].state = s;
418}
419
420void hr_update_hotspare_state(hr_volume_t *vol, size_t hs_idx,
421 hr_ext_state_t s)
422{
423 assert(fibril_mutex_is_locked(&vol->hotspare_lock));
424
425 assert(hs_idx < vol->hotspare_no);
426
427 hr_ext_state_t old = vol->hotspares[hs_idx].state;
428 HR_NOTE("\"%s\": changing hotspare %zu state: %s -> %s\n",
429 vol->devname, hs_idx, hr_get_ext_state_str(old),
430 hr_get_ext_state_str(s));
431 vol->hotspares[hs_idx].state = s;
432}
433
434void hr_update_vol_state(hr_volume_t *vol, hr_vol_state_t new)
435{
436 assert(fibril_rwlock_is_write_locked(&vol->states_lock));
437
438 HR_NOTE("\"%s\": changing volume state: %s -> %s\n", vol->devname,
439 hr_get_vol_state_str(vol->state), hr_get_vol_state_str(new));
440 vol->state = new;
441}
442
443void hr_update_ext_svc_id(hr_volume_t *vol, size_t ext_idx, service_id_t new)
444{
445 if (vol->level != HR_LVL_0)
446 assert(fibril_rwlock_is_write_locked(&vol->extents_lock));
447
448 assert(ext_idx < vol->extent_no);
449
450 service_id_t old = vol->extents[ext_idx].svc_id;
451 HR_NOTE("\"%s\": changing extent no. %zu svc_id: (%" PRIun ") -> "
452 "(%" PRIun ")\n", vol->devname, ext_idx, old, new);
453 vol->extents[ext_idx].svc_id = new;
454}
455
456void hr_update_hotspare_svc_id(hr_volume_t *vol, size_t hs_idx,
457 service_id_t new)
458{
459 assert(fibril_mutex_is_locked(&vol->hotspare_lock));
460
461 assert(hs_idx < vol->hotspare_no);
462
463 service_id_t old = vol->hotspares[hs_idx].svc_id;
464 HR_NOTE("\"%s\": changing hotspare no. %zu svc_id: (%" PRIun ") -> "
465 "(%" PRIun ")\n", vol->devname, hs_idx, old, new);
466 vol->hotspares[hs_idx].svc_id = new;
467}
468
469/*
470 * Do a whole sync (ba = 0, cnt = 0) across all extents,
471 * and update extent state. *For now*, the caller has to
472 * update volume state after the syncs.
473 *
474 * TODO: add update_vol_state fcn ptr for each raid
475 */
476void hr_sync_all_extents(hr_volume_t *vol)
477{
478 errno_t rc;
479
480 fibril_mutex_lock(&vol->lock);
481 for (size_t i = 0; i < vol->extent_no; i++) {
482 if (vol->extents[i].state != HR_EXT_ONLINE)
483 continue;
484 rc = block_sync_cache(vol->extents[i].svc_id, 0, 0);
485 if (rc == ENOMEM || rc == ENOTSUP)
486 continue;
487 if (rc != EOK) {
488 if (rc == ENOENT)
489 hr_update_ext_state(vol, i, HR_EXT_MISSING);
490 else if (rc != EOK)
491 hr_update_ext_state(vol, i, HR_EXT_FAILED);
492 }
493 }
494 fibril_mutex_unlock(&vol->lock);
495}
496
497size_t hr_count_extents(hr_volume_t *vol, hr_ext_state_t state)
498{
499 if (vol->level != HR_LVL_0)
500 assert(fibril_rwlock_is_locked(&vol->extents_lock));
501 assert(fibril_rwlock_is_locked(&vol->states_lock));
502
503 size_t count = 0;
504 for (size_t i = 0; i < vol->extent_no; i++)
505 if (vol->extents[i].state == state)
506 count++;
507
508 return count;
509}
510
511hr_range_lock_t *hr_range_lock_acquire(hr_volume_t *vol, uint64_t ba,
512 uint64_t cnt)
513{
514 hr_range_lock_t *rl = malloc(sizeof(hr_range_lock_t));
515 if (rl == NULL)
516 return NULL;
517
518 rl->vol = vol;
519 rl->off = ba;
520 rl->len = cnt;
521
522 rl->pending = 1;
523 rl->ignore = false;
524
525 link_initialize(&rl->link);
526 fibril_mutex_initialize(&rl->lock);
527
528 fibril_mutex_lock(&rl->lock);
529
530again:
531 HR_RL_LIST_LOCK(vol);
532 list_foreach(vol->range_lock_list, link, hr_range_lock_t, rlp) {
533 if (rlp->ignore)
534 continue;
535 if (hr_range_lock_overlap(rlp, rl)) {
536 rlp->pending++;
537
538 HR_RL_LIST_UNLOCK(vol);
539
540 fibril_mutex_lock(&rlp->lock);
541
542 HR_RL_LIST_LOCK(vol);
543
544 rlp->pending--;
545
546 /*
547 * when ignore is set, after HR_RL_LIST_UNLOCK(),
548 * noone new is going to be able to start sleeping
549 * on the ignored range lock, only already waiting
550 * IOs will come through here
551 */
552 rlp->ignore = true;
553
554 fibril_mutex_unlock(&rlp->lock);
555
556 if (rlp->pending == 0) {
557 list_remove(&rlp->link);
558 free(rlp);
559 }
560
561 HR_RL_LIST_UNLOCK(vol);
562 goto again;
563 }
564 }
565
566 list_append(&rl->link, &vol->range_lock_list);
567
568 HR_RL_LIST_UNLOCK(vol);
569 return rl;
570}
571
572void hr_range_lock_release(hr_range_lock_t *rl)
573{
574 if (rl == NULL)
575 return;
576
577 HR_RL_LIST_LOCK(rl->vol);
578
579 rl->pending--;
580
581 fibril_mutex_unlock(&rl->lock);
582
583 if (rl->pending == 0) {
584 list_remove(&rl->link);
585 free(rl);
586 }
587
588 HR_RL_LIST_UNLOCK(rl->vol);
589}
590
591static bool hr_range_lock_overlap(hr_range_lock_t *rl1, hr_range_lock_t *rl2)
592{
593 uint64_t rl1_start = rl1->off;
594 uint64_t rl1_end = rl1->off + rl1->len - 1;
595 uint64_t rl2_start = rl2->off;
596 uint64_t rl2_end = rl2->off + rl2->len - 1;
597
598 /* one ends before the other starts */
599 if (rl1_end < rl2_start || rl2_end < rl1_start)
600 return false;
601
602 return true;
603}
604
605void hr_mark_vol_state_dirty(hr_volume_t *vol)
606{
607 atomic_store(&vol->state_dirty, true);
608}
609
610static errno_t hr_add_svc_linked_to_list(list_t *list, service_id_t svc_id,
611 bool inited, void *md)
612{
613 HR_DEBUG("%s()", __func__);
614
615 errno_t rc = EOK;
616 struct dev_list_member *to_add;
617
618 if (list == NULL)
619 return EINVAL;
620
621 to_add = malloc(sizeof(struct dev_list_member));
622 if (to_add == NULL) {
623 rc = ENOMEM;
624 goto error;
625 }
626
627 to_add->svc_id = svc_id;
628 to_add->inited = inited;
629
630 if (md != NULL) {
631 to_add->md = md;
632 to_add->md_present = true;
633 } else {
634 to_add->md_present = false;
635 }
636
637 list_append(&to_add->link, list);
638
639error:
640 return rc;
641}
642
643static void free_dev_list_member(struct dev_list_member *p)
644{
645 HR_DEBUG("%s()", __func__);
646
647 if (p->md_present)
648 free(p->md);
649 free(p);
650}
651
652static void free_svc_id_list(list_t *list)
653{
654 HR_DEBUG("%s()", __func__);
655
656 struct dev_list_member *dev_id;
657 while (!list_empty(list)) {
658 dev_id = list_pop(list, struct dev_list_member, link);
659
660 free_dev_list_member(dev_id);
661 }
662}
663
664static errno_t hr_fill_disk_part_svcs_list(list_t *list)
665{
666 HR_DEBUG("%s()", __func__);
667
668 errno_t rc;
669 size_t disk_count;
670 service_id_t *disk_svcs = NULL;
671 vbd_t *vbd = NULL;
672
673 rc = vbd_create(&vbd);
674 if (rc != EOK)
675 goto error;
676
677 rc = vbd_get_disks(vbd, &disk_svcs, &disk_count);
678 if (rc != EOK)
679 goto error;
680
681 for (size_t i = 0; i < disk_count; i++) {
682 vbd_disk_info_t disk_info;
683 rc = vbd_disk_info(vbd, disk_svcs[i], &disk_info);
684 if (rc != EOK)
685 goto error;
686
687 if (disk_info.ltype != lt_none) {
688 size_t part_count;
689 service_id_t *part_ids = NULL;
690 rc = vbd_label_get_parts(vbd, disk_svcs[i], &part_ids,
691 &part_count);
692 if (rc != EOK)
693 goto error;
694
695 for (size_t j = 0; j < part_count; j++) {
696 vbd_part_info_t part_info;
697 rc = vbd_part_get_info(vbd, part_ids[j],
698 &part_info);
699 if (rc != EOK) {
700 free(part_ids);
701 goto error;
702 }
703
704 rc = hr_add_svc_linked_to_list(list,
705 part_info.svc_id, false, NULL);
706 if (rc != EOK) {
707 free(part_ids);
708 goto error;
709 }
710 }
711
712 free(part_ids);
713
714 /*
715 * vbd can detect some bogus label type, but
716 * no partitions. In that case we handle the
717 * svc_id as a label-less disk.
718 *
719 * This can happen when creating an exfat fs
720 * in FreeBSD for example.
721 */
722 if (part_count == 0)
723 disk_info.ltype = lt_none;
724 }
725
726 if (disk_info.ltype == lt_none) {
727 rc = hr_add_svc_linked_to_list(list, disk_svcs[i],
728 false, NULL);
729 if (rc != EOK)
730 goto error;
731 }
732 }
733
734 free(disk_svcs);
735 vbd_destroy(vbd);
736 return EOK;
737error:
738 free_svc_id_list(list);
739 if (disk_svcs != NULL)
740 free(disk_svcs);
741 vbd_destroy(vbd);
742
743 return rc;
744}
745
746static errno_t block_init_dev_list(list_t *list)
747{
748 HR_DEBUG("%s()", __func__);
749
750 list_foreach_safe(*list, cur_link, next_link) {
751 struct dev_list_member *iter;
752 iter = list_get_instance(cur_link, struct dev_list_member,
753 link);
754
755 if (iter->inited)
756 continue;
757
758 errno_t rc = block_init(iter->svc_id);
759
760 /* already used as an extent of active volume */
761 /* XXX: figure out how it is with hotspares too */
762 if (rc == EEXIST) {
763 list_remove(cur_link);
764 free_dev_list_member(iter);
765 continue;
766 }
767
768 if (rc != EOK)
769 return rc;
770
771 iter->inited = true;
772 iter->fini = true;
773 }
774
775 return EOK;
776}
777
778static void block_fini_dev_list(list_t *list)
779{
780 HR_DEBUG("%s()", __func__);
781
782 list_foreach(*list, link, struct dev_list_member, iter) {
783 if (iter->inited && iter->fini) {
784 block_fini(iter->svc_id);
785 iter->inited = false;
786 iter->fini = false;
787 }
788 }
789}
790
791static errno_t hr_util_get_matching_md_svcs_list(list_t *rlist, list_t *list,
792 service_id_t svc_id, hr_metadata_type_t type_main,
793 void *metadata_struct_main)
794{
795 HR_DEBUG("%s()", __func__);
796
797 errno_t rc = EOK;
798
799 hr_superblock_ops_t *meta_ops = get_type_ops(type_main);
800
801 list_foreach(*list, link, struct dev_list_member, iter) {
802 if (iter->svc_id == svc_id)
803 continue;
804
805 void *metadata_struct;
806 hr_metadata_type_t type;
807
808 rc = find_metadata(iter->svc_id, &metadata_struct, &type);
809 if (rc == ENOFS)
810 continue;
811 if (rc != EOK)
812 goto error;
813
814 if (type != type_main) {
815 free(metadata_struct);
816 continue;
817 }
818
819 if (!meta_ops->compare_uuids(metadata_struct_main,
820 metadata_struct)) {
821 free(metadata_struct);
822 continue;
823 }
824
825 rc = hr_add_svc_linked_to_list(rlist, iter->svc_id, true,
826 metadata_struct);
827 if (rc != EOK)
828 goto error;
829 }
830
831 return EOK;
832error:
833 free_svc_id_list(rlist);
834 return rc;
835}
836
837static errno_t hr_util_assemble_from_matching_list(list_t *list,
838 hr_metadata_type_t type)
839{
840 HR_DEBUG("%s()", __func__);
841
842 errno_t rc = EOK;
843
844 hr_superblock_ops_t *meta_ops = get_type_ops(type);
845
846 link_t *memb_l = list_first(list);
847 struct dev_list_member *memb = list_get_instance(memb_l,
848 struct dev_list_member, link);
849
850 hr_level_t level = meta_ops->get_level(memb->md);
851 const char *devname = meta_ops->get_devname(memb->md);
852
853 hr_volume_t *vol;
854 rc = hr_create_vol_struct(&vol, level, devname, type);
855 if (rc != EOK)
856 return rc;
857
858 meta_ops->init_meta2vol(list, vol);
859
860 rc = vol->hr_ops.create(vol);
861 if (rc != EOK)
862 goto error;
863
864 rc = hr_register_volume(vol);
865 if (rc != EOK)
866 goto error;
867
868 fibril_rwlock_write_lock(&hr_volumes_lock);
869 list_append(&vol->lvolumes, &hr_volumes);
870 fibril_rwlock_write_unlock(&hr_volumes_lock);
871
872 HR_NOTE("assembled volume \"%s\"\n", vol->devname);
873
874 return EOK;
875error:
876 hr_destroy_vol_struct(vol);
877 return rc;
878}
879
880static errno_t hr_fill_svcs_list_from_cfg(hr_config_t *cfg, list_t *list)
881{
882 HR_DEBUG("%s()", __func__);
883
884 errno_t rc = EOK;
885 for (size_t i = 0; i < cfg->dev_no; ++i) {
886 rc = hr_add_svc_linked_to_list(list, cfg->devs[i], false,
887 NULL);
888 if (rc != EOK)
889 goto error;
890 }
891
892 return EOK;
893error:
894 free_svc_id_list(list);
895 return rc;
896}
897
898errno_t hr_util_try_assemble(hr_config_t *cfg, size_t *rassembled_cnt)
899{
900 HR_DEBUG("%s()", __func__);
901
902 /*
903 * scan partitions or disks:
904 *
905 * When we find a metadata block with valid
906 * magic, take UUID and try to find other matching
907 * UUIDs.
908 *
909 * We ignore extents that are a part of already
910 * active volumes. (even when the counter is lower
911 * on active volumes... XXX: use timestamp as initial counter value
912 * when assembling, or writing dirty metadata?)
913 */
914
915 size_t asm_cnt = 0;
916 errno_t rc;
917 list_t dev_id_list;
918
919 list_initialize(&dev_id_list);
920
921 if (cfg == NULL)
922 rc = hr_fill_disk_part_svcs_list(&dev_id_list);
923 else
924 rc = hr_fill_svcs_list_from_cfg(cfg, &dev_id_list);
925
926 if (rc != EOK)
927 goto error;
928
929 rc = block_init_dev_list(&dev_id_list);
930 if (rc != EOK)
931 goto error;
932
933 struct dev_list_member *iter;
934 while (!list_empty(&dev_id_list)) {
935 iter = list_pop(&dev_id_list, struct dev_list_member, link);
936
937 void *metadata_struct_main;
938 hr_metadata_type_t type;
939
940 rc = find_metadata(iter->svc_id, &metadata_struct_main, &type);
941 if (rc == ENOFS) {
942 block_fini(iter->svc_id);
943 free_dev_list_member(iter);
944 rc = EOK;
945 continue;
946 }
947
948 if (rc != EOK)
949 goto error;
950
951 char *svc_name = NULL;
952 rc = loc_service_get_name(iter->svc_id, &svc_name);
953 if (rc != EOK)
954 goto error;
955 HR_DEBUG("found valid metadata on %s (type = %s), matching "
956 "other extents\n",
957 svc_name, hr_get_metadata_type_str(type));
958 free(svc_name);
959
960 list_t matching_svcs_list;
961 list_initialize(&matching_svcs_list);
962
963 rc = hr_util_get_matching_md_svcs_list(&matching_svcs_list,
964 &dev_id_list, iter->svc_id, type, metadata_struct_main);
965 if (rc != EOK)
966 goto error;
967
968 /* add current iter to list as well */
969 rc = hr_add_svc_linked_to_list(&matching_svcs_list,
970 iter->svc_id, true, metadata_struct_main);
971 if (rc != EOK) {
972 free_svc_id_list(&matching_svcs_list);
973 goto error;
974 }
975
976 /* remove matching list members from dev_id_list */
977 list_foreach(matching_svcs_list, link, struct dev_list_member,
978 iter2) {
979 struct dev_list_member *to_remove;
980 list_foreach_safe(dev_id_list, cur_link, next_link) {
981 to_remove = list_get_instance(cur_link,
982 struct dev_list_member, link);
983 if (to_remove->svc_id == iter2->svc_id) {
984 list_remove(cur_link);
985 free_dev_list_member(to_remove);
986 }
987 }
988 }
989
990 rc = hr_util_assemble_from_matching_list(&matching_svcs_list,
991 type);
992 switch (rc) {
993 case EOK:
994 asm_cnt++;
995 break;
996 case ENOMEM:
997 goto error;
998 default:
999 rc = EOK;
1000 }
1001 block_fini_dev_list(&matching_svcs_list);
1002 free_svc_id_list(&matching_svcs_list);
1003 }
1004
1005error:
1006 if (rassembled_cnt != NULL)
1007 *rassembled_cnt = asm_cnt;
1008
1009 block_fini_dev_list(&dev_id_list);
1010 free_svc_id_list(&dev_id_list);
1011
1012 return rc;
1013}
1014
1015errno_t hr_util_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
1016{
1017 HR_DEBUG("%s()", __func__);
1018
1019 errno_t rc = EOK;
1020
1021 fibril_mutex_lock(&vol->hotspare_lock);
1022
1023 if (vol->hotspare_no >= HR_MAX_HOTSPARES) {
1024 HR_ERROR("%s(): cannot add more hotspares "
1025 "to \"%s\"\n", __func__, vol->devname);
1026 rc = ELIMIT;
1027 goto error;
1028 }
1029
1030 for (size_t i = 0; i < vol->hotspare_no; i++) {
1031 if (vol->hotspares[i].svc_id == hotspare) {
1032 HR_ERROR("%s(): hotspare (%" PRIun ") already used in "
1033 "%s\n", __func__, hotspare, vol->devname);
1034 rc = EEXIST;
1035 goto error;
1036 }
1037 }
1038
1039 rc = block_init(hotspare);
1040 if (rc != EOK)
1041 goto error;
1042
1043 uint64_t hs_blkno;
1044 rc = block_get_nblocks(hotspare, &hs_blkno);
1045 if (rc != EOK) {
1046 block_fini(hotspare);
1047 goto error;
1048 }
1049
1050 if (hs_blkno < vol->truncated_blkno) {
1051 HR_ERROR("%s(): hotspare (%" PRIun ") doesn't have enough "
1052 "blocks\n", __func__, hotspare);
1053
1054 rc = EINVAL;
1055 block_fini(hotspare);
1056 goto error;
1057 }
1058
1059 size_t hs_idx = vol->hotspare_no;
1060
1061 vol->hotspare_no++;
1062
1063 hr_update_hotspare_svc_id(vol, hs_idx, hotspare);
1064 hr_update_hotspare_state(vol, hs_idx, HR_EXT_HOTSPARE);
1065
1066 hr_mark_vol_state_dirty(vol);
1067error:
1068 fibril_mutex_unlock(&vol->hotspare_lock);
1069 return rc;
1070}
1071
1072/** @}
1073 */
Note: See TracBrowser for help on using the repository browser.