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

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

hr: auto assembly: detect bogus label type

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