source: mainline/uspace/srv/bd/hr/util.c@ 56602e0

Last change on this file since 56602e0 was 56602e0, checked in by Miroslav Cimerman <mc@…>, 3 months ago

hr: rename all strings status' -> state'

  • Property mode set to 100644
File size: 24.3 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 rc = hr_add_svc_linked_to_list(list, disk_svcs[i],
661 false, NULL);
662 if (rc != EOK)
663 goto error;
664 } else {
665 size_t part_count;
666 service_id_t *part_ids = NULL;
667 rc = vbd_label_get_parts(vbd, disk_svcs[i], &part_ids,
668 &part_count);
669 if (rc != EOK)
670 goto error;
671
672 for (size_t j = 0; j < part_count; j++) {
673 vbd_part_info_t part_info;
674 rc = vbd_part_get_info(vbd, part_ids[j],
675 &part_info);
676 if (rc != EOK) {
677 free(part_ids);
678 goto error;
679 }
680
681 rc = hr_add_svc_linked_to_list(list,
682 part_info.svc_id, false, NULL);
683 if (rc != EOK) {
684 free(part_ids);
685 goto error;
686 }
687 }
688
689 free(part_ids);
690 }
691 }
692
693 free(disk_svcs);
694 vbd_destroy(vbd);
695 return EOK;
696error:
697 free_svc_id_list(list);
698 if (disk_svcs != NULL)
699 free(disk_svcs);
700 vbd_destroy(vbd);
701
702 return rc;
703}
704
705static errno_t block_init_dev_list(list_t *list)
706{
707 HR_DEBUG("%s()", __func__);
708
709 list_foreach_safe(*list, cur_link, next_link) {
710 struct dev_list_member *iter;
711 iter = list_get_instance(cur_link, struct dev_list_member,
712 link);
713
714 if (iter->inited)
715 continue;
716
717 errno_t rc = block_init(iter->svc_id);
718
719 /* already used as an extent of active volume */
720 /* XXX: figure out how it is with hotspares too */
721 if (rc == EEXIST) {
722 list_remove(cur_link);
723 free_dev_list_member(iter);
724 continue;
725 }
726
727 if (rc != EOK)
728 return rc;
729
730 iter->inited = true;
731 iter->fini = true;
732 }
733
734 return EOK;
735}
736
737static void block_fini_dev_list(list_t *list)
738{
739 HR_DEBUG("%s()", __func__);
740
741 list_foreach(*list, link, struct dev_list_member, iter) {
742 if (iter->inited && iter->fini) {
743 block_fini(iter->svc_id);
744 iter->inited = false;
745 iter->fini = false;
746 }
747 }
748}
749
750static errno_t hr_util_get_matching_md_svcs_list(list_t *rlist, list_t *list,
751 service_id_t svc_id, hr_metadata_type_t type_main,
752 void *metadata_struct_main)
753{
754 HR_DEBUG("%s()", __func__);
755
756 errno_t rc = EOK;
757
758 hr_superblock_ops_t *meta_ops = get_type_ops(type_main);
759
760 list_foreach(*list, link, struct dev_list_member, iter) {
761 if (iter->svc_id == svc_id)
762 continue;
763
764 void *metadata_struct;
765 hr_metadata_type_t type;
766
767 rc = find_metadata(iter->svc_id, &metadata_struct, &type);
768 if (rc == ENOFS)
769 continue;
770 if (rc != EOK)
771 goto error;
772
773 if (type != type_main) {
774 free(metadata_struct);
775 continue;
776 }
777
778 if (!meta_ops->compare_uuids(metadata_struct_main,
779 metadata_struct)) {
780 free(metadata_struct);
781 continue;
782 }
783
784 rc = hr_add_svc_linked_to_list(rlist, iter->svc_id, true,
785 metadata_struct);
786 if (rc != EOK)
787 goto error;
788 }
789
790 return EOK;
791error:
792 free_svc_id_list(rlist);
793 return rc;
794}
795
796static errno_t hr_util_assemble_from_matching_list(list_t *list,
797 hr_metadata_type_t type)
798{
799 HR_DEBUG("%s()", __func__);
800
801 errno_t rc = EOK;
802
803 hr_superblock_ops_t *meta_ops = get_type_ops(type);
804
805 link_t *memb_l = list_first(list);
806 struct dev_list_member *memb = list_get_instance(memb_l,
807 struct dev_list_member, link);
808
809 hr_level_t level = meta_ops->get_level(memb->md);
810 const char *devname = meta_ops->get_devname(memb->md);
811
812 hr_volume_t *vol;
813 rc = hr_create_vol_struct(&vol, level, devname, type);
814 if (rc != EOK)
815 return rc;
816
817 meta_ops->init_meta2vol(list, vol);
818
819 /*
820 * TODO: something like mark md dirty or whatever
821 * - probably will be handled by each md type differently,
822 * by specific function pointers
823 * - deal with this when foreign md will be handled
824 *
825 * XXX: if thats the only thing that can change in metadata
826 * during volume runtime, then whatever, but if more
827 * things will need to be synced, think of something more clever
828 *
829 * TODO: remove from here and increment it the "first" time (if nothing
830 * happens - no state changes, no rebuild, etc) - only after the first
831 * write... but for now leave it here
832 */
833 (void)vol->meta_ops->inc_counter(vol->in_mem_md);
834
835 rc = vol->hr_ops.create(vol);
836 if (rc != EOK)
837 goto error;
838
839 /*
840 * XXX: register it here
841 * ... if it fails on EEXIST try different name... like + 1 on the end
842 *
843 * or have metadata edit utility as a part of hrctl..., or create
844 * the original name + 4 random characters, tell the user that the device
845 * was created with this new name, and add a option to hrctl to rename
846 * an active array, and then write the new dirty metadata...
847 *
848 * or just refuse to assemble a name that is already used...
849 *
850 * TODO: discuss
851 */
852 rc = hr_register_volume(vol);
853 if (rc != EOK)
854 goto error;
855
856 (void)vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
857
858 fibril_rwlock_write_lock(&hr_volumes_lock);
859 list_append(&vol->lvolumes, &hr_volumes);
860 fibril_rwlock_write_unlock(&hr_volumes_lock);
861
862 HR_NOTE("assembled volume \"%s\"\n", vol->devname);
863
864 return EOK;
865error:
866 hr_destroy_vol_struct(vol);
867 return rc;
868}
869
870static errno_t hr_fill_svcs_list_from_cfg(hr_config_t *cfg, list_t *list)
871{
872 HR_DEBUG("%s()", __func__);
873
874 errno_t rc = EOK;
875 for (size_t i = 0; i < cfg->dev_no; ++i) {
876 rc = hr_add_svc_linked_to_list(list, cfg->devs[i], false,
877 NULL);
878 if (rc != EOK)
879 goto error;
880 }
881
882 return EOK;
883error:
884 free_svc_id_list(list);
885 return rc;
886}
887
888errno_t hr_util_try_assemble(hr_config_t *cfg, size_t *rassembled_cnt)
889{
890 HR_DEBUG("%s()", __func__);
891
892 /*
893 * scan partitions or disks:
894 *
895 * When we find a metadata block with valid
896 * magic, take UUID and try to find other matching
897 * UUIDs.
898 *
899 * We ignore extents that are a part of already
900 * active volumes. (even when the counter is lower
901 * on active volumes... XXX: use timestamp as initial counter value
902 * when assembling, or writing dirty metadata?)
903 */
904
905 size_t asm_cnt = 0;
906 errno_t rc;
907 list_t dev_id_list;
908
909 list_initialize(&dev_id_list);
910
911 if (cfg == NULL)
912 rc = hr_fill_disk_part_svcs_list(&dev_id_list);
913 else
914 rc = hr_fill_svcs_list_from_cfg(cfg, &dev_id_list);
915
916 if (rc != EOK)
917 goto error;
918
919 rc = block_init_dev_list(&dev_id_list);
920 if (rc != EOK)
921 goto error;
922
923 struct dev_list_member *iter;
924 while (!list_empty(&dev_id_list)) {
925 iter = list_pop(&dev_id_list, struct dev_list_member, link);
926
927 void *metadata_struct_main;
928 hr_metadata_type_t type;
929
930 rc = find_metadata(iter->svc_id, &metadata_struct_main, &type);
931 if (rc == ENOFS) {
932 block_fini(iter->svc_id);
933 free_dev_list_member(iter);
934 rc = EOK;
935 continue;
936 }
937
938 if (rc != EOK)
939 goto error;
940
941 char *svc_name = NULL;
942 rc = loc_service_get_name(iter->svc_id, &svc_name);
943 if (rc != EOK)
944 goto error;
945 HR_DEBUG("found valid metadata on %s (type = %s), matching "
946 "other extents\n",
947 svc_name, hr_get_metadata_type_str(type));
948 free(svc_name);
949
950 list_t matching_svcs_list;
951 list_initialize(&matching_svcs_list);
952
953 rc = hr_util_get_matching_md_svcs_list(&matching_svcs_list,
954 &dev_id_list, iter->svc_id, type, metadata_struct_main);
955 if (rc != EOK)
956 goto error;
957
958 /* add current iter to list as well */
959 rc = hr_add_svc_linked_to_list(&matching_svcs_list,
960 iter->svc_id, true, metadata_struct_main);
961 if (rc != EOK) {
962 free_svc_id_list(&matching_svcs_list);
963 goto error;
964 }
965
966 /* remove matching list members from dev_id_list */
967 list_foreach(matching_svcs_list, link, struct dev_list_member,
968 iter2) {
969 struct dev_list_member *to_remove;
970 list_foreach_safe(dev_id_list, cur_link, next_link) {
971 to_remove = list_get_instance(cur_link,
972 struct dev_list_member, link);
973 if (to_remove->svc_id == iter2->svc_id) {
974 list_remove(cur_link);
975 free_dev_list_member(to_remove);
976 }
977 }
978 }
979
980 rc = hr_util_assemble_from_matching_list(&matching_svcs_list,
981 type);
982 switch (rc) {
983 case EOK:
984 asm_cnt++;
985 break;
986 case ENOMEM:
987 goto error;
988 default:
989 rc = EOK;
990 }
991 block_fini_dev_list(&matching_svcs_list);
992 free_svc_id_list(&matching_svcs_list);
993 }
994
995error:
996 if (rassembled_cnt != NULL)
997 *rassembled_cnt = asm_cnt;
998
999 block_fini_dev_list(&dev_id_list);
1000 free_svc_id_list(&dev_id_list);
1001
1002 return rc;
1003}
1004
1005errno_t hr_util_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
1006{
1007 HR_DEBUG("%s()", __func__);
1008
1009 errno_t rc = EOK;
1010
1011 fibril_mutex_lock(&vol->hotspare_lock);
1012
1013 if (vol->hotspare_no >= HR_MAX_HOTSPARES) {
1014 HR_ERROR("%s(): cannot add more hotspares "
1015 "to \"%s\"\n", __func__, vol->devname);
1016 rc = ELIMIT;
1017 goto error;
1018 }
1019
1020 for (size_t i = 0; i < vol->hotspare_no; i++) {
1021 if (vol->hotspares[i].svc_id == hotspare) {
1022 HR_ERROR("%s(): hotspare (%" PRIun ") already used in "
1023 "%s\n", __func__, hotspare, vol->devname);
1024 rc = EEXIST;
1025 goto error;
1026 }
1027 }
1028
1029 rc = block_init(hotspare);
1030 if (rc != EOK)
1031 goto error;
1032
1033 uint64_t hs_blkno;
1034 rc = block_get_nblocks(hotspare, &hs_blkno);
1035 if (rc != EOK) {
1036 block_fini(hotspare);
1037 goto error;
1038 }
1039
1040 if (hs_blkno - vol->meta_ops->get_size() < vol->truncated_blkno) {
1041 HR_ERROR("%s(): hotspare (%" PRIun ") doesn't have enough "
1042 "blocks\n", __func__, hotspare);
1043
1044 rc = EINVAL;
1045 block_fini(hotspare);
1046 goto error;
1047 }
1048
1049 size_t hs_idx = vol->hotspare_no;
1050
1051 vol->hotspare_no++;
1052
1053 hr_update_hotspare_svc_id(vol, hs_idx, hotspare);
1054 hr_update_hotspare_state(vol, hs_idx, HR_EXT_HOTSPARE);
1055
1056 hr_mark_vol_state_dirty(vol);
1057error:
1058 fibril_mutex_unlock(&vol->hotspare_lock);
1059 return rc;
1060}
1061
1062/** @}
1063 */
Note: See TracBrowser for help on using the repository browser.