source: mainline/uspace/srv/bd/hr/util.c@ 75262d2f

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

hr: util.c: hr_create_vol_struct() style

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