source: mainline/uspace/srv/bd/hr/util.c@ 746e636

Last change on this file since 746e636 was d082801, checked in by Miroslav Cimerman <mc@…>, 5 months ago

hr: refactor manual assembly

  • Property mode set to 100644
File size: 23.9 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 <io/log.h>
42#include <loc.h>
43#include <mem.h>
44#include <stdatomic.h>
45#include <stdlib.h>
46#include <stdio.h>
47#include <str_error.h>
48#include <vbd.h>
49
50#include "io.h"
51#include "superblock.h"
52#include "util.h"
53#include "var.h"
54
55#define HR_RL_LIST_LOCK(vol) (fibril_mutex_lock(&vol->range_lock_list_lock))
56#define HR_RL_LIST_UNLOCK(vol) \
57 (fibril_mutex_unlock(&vol->range_lock_list_lock))
58
59static bool hr_range_lock_overlap(hr_range_lock_t *, hr_range_lock_t *);
60
61extern loc_srv_t *hr_srv;
62extern list_t hr_volumes;
63extern fibril_rwlock_t hr_volumes_lock;
64
65errno_t hr_create_vol_struct(hr_volume_t **rvol, hr_level_t level)
66{
67 errno_t rc;
68
69 hr_volume_t *vol = calloc(1, sizeof(hr_volume_t));
70 if (vol == NULL)
71 return ENOMEM;
72
73 vol->level = level;
74
75 switch (level) {
76 case HR_LVL_1:
77 vol->hr_ops.create = hr_raid1_create;
78 vol->hr_ops.init = hr_raid1_init;
79 vol->hr_ops.status_event = hr_raid1_status_event;
80 vol->hr_ops.add_hotspare = hr_raid1_add_hotspare;
81 break;
82 case HR_LVL_0:
83 vol->hr_ops.create = hr_raid0_create;
84 vol->hr_ops.init = hr_raid0_init;
85 vol->hr_ops.status_event = hr_raid0_status_event;
86 break;
87 case HR_LVL_4:
88 vol->hr_ops.create = hr_raid5_create;
89 vol->hr_ops.init = hr_raid5_init;
90 vol->hr_ops.status_event = hr_raid5_status_event;
91 vol->hr_ops.add_hotspare = hr_raid5_add_hotspare;
92 break;
93 case HR_LVL_5:
94 vol->hr_ops.create = hr_raid5_create;
95 vol->hr_ops.init = hr_raid5_init;
96 vol->hr_ops.status_event = hr_raid5_status_event;
97 vol->hr_ops.add_hotspare = hr_raid5_add_hotspare;
98 break;
99 default:
100 HR_DEBUG("unkown level: %d, aborting\n", vol->level);
101 rc = EINVAL;
102 goto error;
103 }
104
105 vol->fge = hr_fpool_create(16, 32, sizeof(hr_io_t));
106 if (vol->fge == NULL) {
107 rc = ENOMEM;
108 goto error;
109 }
110
111 vol->in_mem_md = calloc(1, sizeof(hr_metadata_t));
112 if (vol->in_mem_md == NULL) {
113 free(vol->fge);
114 rc = ENOMEM;
115 goto error;
116 }
117
118 vol->status = HR_VOL_NONE;
119
120 for (size_t i = 0; i < HR_MAX_EXTENTS; ++i)
121 vol->extents[i].status = HR_EXT_MISSING;
122
123 for (size_t i = 0; i < HR_MAX_HOTSPARES; ++i)
124 vol->extents[i].status = HR_EXT_MISSING;
125
126 fibril_mutex_initialize(&vol->lock); /* XXX: will remove this */
127
128 fibril_rwlock_initialize(&vol->extents_lock);
129 fibril_rwlock_initialize(&vol->states_lock);
130
131 fibril_mutex_initialize(&vol->hotspare_lock);
132
133 list_initialize(&vol->range_lock_list);
134 fibril_mutex_initialize(&vol->range_lock_list_lock);
135
136 atomic_init(&vol->rebuild_blk, 0);
137 atomic_init(&vol->state_dirty, false);
138 atomic_init(&vol->open_cnt, 0);
139
140 *rvol = vol;
141
142 return EOK;
143error:
144 free(vol);
145 return rc;
146}
147
148void hr_destroy_vol_struct(hr_volume_t *vol)
149{
150 if (vol == NULL)
151 return;
152
153 hr_fpool_destroy(vol->fge);
154 hr_fini_devs(vol);
155 free(vol->in_mem_md);
156 free(vol);
157}
158
159hr_volume_t *hr_get_volume(service_id_t svc_id)
160{
161 HR_DEBUG("hr_get_volume(): (%" PRIun ")\n", svc_id);
162
163 hr_volume_t *rvol = NULL;
164
165 fibril_rwlock_read_lock(&hr_volumes_lock);
166 list_foreach(hr_volumes, lvolumes, hr_volume_t, iter) {
167 if (iter->svc_id == svc_id) {
168 rvol = iter;
169 break;
170 }
171 }
172
173 fibril_rwlock_read_unlock(&hr_volumes_lock);
174 return rvol;
175}
176
177errno_t hr_remove_volume(service_id_t svc_id)
178{
179 HR_DEBUG("hr_remove_volume(): (%" PRIun ")\n", svc_id);
180
181 errno_t rc;
182
183 fibril_rwlock_write_lock(&hr_volumes_lock);
184 list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
185 if (vol->svc_id == svc_id) {
186 int open_cnt = atomic_load_explicit(&vol->open_cnt,
187 memory_order_relaxed);
188 /*
189 * The "atomicity" of this if condition is provided
190 * by the write lock - no new bd connection can
191 * come, because we need to get the bd_srvs_t from
192 * volume, which we get from the list.
193 * (see hr_client_conn() in hr.c)
194 */
195 if (open_cnt > 0) {
196 fibril_rwlock_write_unlock(&hr_volumes_lock);
197 return EBUSY;
198 }
199 list_remove(&vol->lvolumes);
200 fibril_rwlock_write_unlock(&hr_volumes_lock);
201
202 hr_destroy_vol_struct(vol);
203
204 rc = loc_service_unregister(hr_srv, svc_id);
205 return rc;
206 }
207 }
208
209 fibril_rwlock_write_unlock(&hr_volumes_lock);
210 return ENOENT;
211}
212
213errno_t hr_init_devs(hr_volume_t *vol)
214{
215 HR_DEBUG("%s()", __func__);
216
217 errno_t rc;
218 size_t i;
219 hr_extent_t *extent;
220
221 for (i = 0; i < vol->extent_no; i++) {
222 extent = &vol->extents[i];
223 if (extent->svc_id == 0) {
224 extent->status = HR_EXT_MISSING;
225 continue;
226 }
227
228 HR_DEBUG("hr_init_devs(): block_init() on (%lu)\n",
229 extent->svc_id);
230 rc = block_init(extent->svc_id);
231 extent->status = HR_EXT_ONLINE;
232
233 if (rc != EOK) {
234 HR_ERROR("hr_init_devs(): initing (%lu) failed, "
235 "aborting\n", extent->svc_id);
236 break;
237 }
238 }
239
240 for (i = 0; i < HR_MAX_HOTSPARES; i++)
241 vol->hotspares[i].status = HR_EXT_MISSING;
242
243 return rc;
244}
245
246void hr_fini_devs(hr_volume_t *vol)
247{
248 HR_DEBUG("%s()", __func__);
249
250 size_t i;
251
252 for (i = 0; i < vol->extent_no; i++) {
253 if (vol->extents[i].svc_id != 0) {
254 HR_DEBUG("hr_fini_devs(): block_fini() on (%lu)\n",
255 vol->extents[i].svc_id);
256 block_fini(vol->extents[i].svc_id);
257 }
258 }
259
260 for (i = 0; i < vol->hotspare_no; i++) {
261 if (vol->hotspares[i].svc_id != 0) {
262 HR_DEBUG("hr_fini_devs(): block_fini() on (%lu)\n",
263 vol->hotspares[i].svc_id);
264 block_fini(vol->hotspares[i].svc_id);
265 }
266 }
267}
268
269errno_t hr_register_volume(hr_volume_t *vol)
270{
271 HR_DEBUG("%s()", __func__);
272
273 errno_t rc;
274 service_id_t new_id;
275 category_id_t cat_id;
276 char *fullname = NULL;
277 char *devname = vol->devname;
278
279 if (asprintf(&fullname, "devices/%s", devname) < 0)
280 return ENOMEM;
281
282 rc = loc_service_register(hr_srv, fullname, &new_id);
283 if (rc != EOK) {
284 HR_ERROR("unable to register device \"%s\": %s\n",
285 fullname, str_error(rc));
286 free(fullname);
287 return rc;
288 }
289
290 rc = loc_category_get_id("raid", &cat_id, IPC_FLAG_BLOCKING);
291 if (rc != EOK) {
292 HR_ERROR("failed resolving category \"raid\": %s\n",
293 str_error(rc));
294 goto error;
295 }
296
297 rc = loc_service_add_to_cat(hr_srv, new_id, cat_id);
298 if (rc != EOK) {
299 HR_ERROR("failed adding \"%s\" to category \"raid\": %s\n",
300 fullname, str_error(rc));
301 goto error;
302 }
303
304 vol->svc_id = new_id;
305
306 free(fullname);
307 return EOK;
308error:
309 rc = loc_service_unregister(hr_srv, new_id);
310 free(fullname);
311 return rc;
312}
313
314errno_t hr_check_devs(hr_volume_t *vol, uint64_t *rblkno, size_t *rbsize)
315{
316 HR_DEBUG("%s()", __func__);
317
318 errno_t rc;
319 size_t i, bsize;
320 uint64_t nblocks;
321 size_t last_bsize = 0;
322 uint64_t last_nblocks = 0;
323 uint64_t total_blocks = 0;
324 hr_extent_t *extent;
325
326 for (i = 0; i < vol->extent_no; i++) {
327 extent = &vol->extents[i];
328 if (extent->status == HR_EXT_MISSING)
329 continue;
330 rc = block_get_nblocks(extent->svc_id, &nblocks);
331 if (rc != EOK)
332 goto error;
333 if (last_nblocks != 0 && nblocks != last_nblocks) {
334 HR_ERROR("number of blocks differs\n");
335 rc = EINVAL;
336 goto error;
337 }
338
339 total_blocks += nblocks;
340 last_nblocks = nblocks;
341 }
342
343 for (i = 0; i < vol->extent_no; i++) {
344 extent = &vol->extents[i];
345 if (extent->status == HR_EXT_MISSING)
346 continue;
347 rc = block_get_bsize(extent->svc_id, &bsize);
348 if (rc != EOK)
349 goto error;
350 if (last_bsize != 0 && bsize != last_bsize) {
351 HR_ERROR("block sizes differ\n");
352 rc = EINVAL;
353 goto error;
354 }
355
356 last_bsize = bsize;
357 }
358
359 if ((bsize % 512) != 0) {
360 HR_ERROR("block size not multiple of 512\n");
361 return EINVAL;
362 }
363
364 if (rblkno != NULL)
365 *rblkno = total_blocks;
366 if (rbsize != NULL)
367 *rbsize = bsize;
368error:
369 return rc;
370}
371
372errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
373{
374 if (ba + cnt > vol->data_blkno)
375 return ERANGE;
376 return EOK;
377}
378
379void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
380{
381 *ba = *ba + vol->data_offset;
382}
383
384void hr_update_ext_status(hr_volume_t *vol, size_t extent, hr_ext_status_t s)
385{
386 if (vol->level != HR_LVL_0)
387 assert(fibril_rwlock_is_locked(&vol->extents_lock));
388
389 assert(fibril_rwlock_is_write_locked(&vol->states_lock));
390
391 assert(extent < vol->extent_no);
392
393 hr_ext_status_t old = vol->extents[extent].status;
394 HR_WARN("\"%s\": changing extent %lu state: %s -> %s\n",
395 vol->devname, extent, hr_get_ext_status_msg(old),
396 hr_get_ext_status_msg(s));
397 vol->extents[extent].status = s;
398}
399
400void hr_update_hotspare_status(hr_volume_t *vol, size_t hs, hr_ext_status_t s)
401{
402 assert(fibril_mutex_is_locked(&vol->hotspare_lock));
403
404 assert(hs < vol->hotspare_no);
405
406 hr_ext_status_t old = vol->hotspares[hs].status;
407 HR_WARN("\"%s\": changing hotspare %lu state: %s -> %s\n",
408 vol->devname, hs, hr_get_ext_status_msg(old),
409 hr_get_ext_status_msg(s));
410 vol->hotspares[hs].status = s;
411}
412
413void hr_update_vol_status(hr_volume_t *vol, hr_vol_status_t new)
414{
415 assert(fibril_rwlock_is_write_locked(&vol->states_lock));
416
417 HR_WARN("\"%s\": changing volume state: %s -> %s\n", vol->devname,
418 hr_get_vol_status_msg(vol->status), hr_get_vol_status_msg(new));
419 vol->status = new;
420}
421
422void hr_update_ext_svc_id(hr_volume_t *vol, size_t extent, service_id_t new)
423{
424 if (vol->level != HR_LVL_0)
425 assert(fibril_rwlock_is_write_locked(&vol->extents_lock));
426
427 assert(extent < vol->extent_no);
428
429 service_id_t old = vol->extents[extent].svc_id;
430 HR_WARN("\"%s\": changing extent no. %lu svc_id: (%lu) -> (%lu)\n",
431 vol->devname, extent, old, new);
432 vol->extents[extent].svc_id = new;
433}
434
435void hr_update_hotspare_svc_id(hr_volume_t *vol, size_t hs, service_id_t new)
436{
437 assert(fibril_mutex_is_locked(&vol->hotspare_lock));
438
439 assert(hs < vol->hotspare_no);
440
441 service_id_t old = vol->hotspares[hs].svc_id;
442 HR_WARN("\"%s\": changing hotspare no. %lu svc_id: (%lu) -> (%lu)\n",
443 vol->devname, hs, old, new);
444 vol->hotspares[hs].svc_id = new;
445}
446
447/*
448 * Do a whole sync (ba = 0, cnt = 0) across all extents,
449 * and update extent status. *For now*, the caller has to
450 * update volume status after the syncs.
451 *
452 * TODO: add update_vol_status fcn ptr for each raid
453 */
454void hr_sync_all_extents(hr_volume_t *vol)
455{
456 errno_t rc;
457
458 fibril_mutex_lock(&vol->lock);
459 for (size_t i = 0; i < vol->extent_no; i++) {
460 if (vol->extents[i].status != HR_EXT_ONLINE)
461 continue;
462 rc = block_sync_cache(vol->extents[i].svc_id, 0, 0);
463 if (rc == ENOMEM || rc == ENOTSUP)
464 continue;
465 if (rc != EOK) {
466 if (rc == ENOENT)
467 hr_update_ext_status(vol, i, HR_EXT_MISSING);
468 else if (rc != EOK)
469 hr_update_ext_status(vol, i, HR_EXT_FAILED);
470 }
471 }
472 fibril_mutex_unlock(&vol->lock);
473}
474
475size_t hr_count_extents(hr_volume_t *vol, hr_ext_status_t status)
476{
477 if (vol->level != HR_LVL_0)
478 assert(fibril_rwlock_is_locked(&vol->extents_lock));
479 assert(fibril_rwlock_is_locked(&vol->states_lock));
480
481 size_t count = 0;
482 for (size_t i = 0; i < vol->extent_no; i++)
483 if (vol->extents[i].status == status)
484 count++;
485
486 return count;
487}
488
489hr_range_lock_t *hr_range_lock_acquire(hr_volume_t *vol, uint64_t ba,
490 uint64_t cnt)
491{
492 hr_range_lock_t *rl = malloc(sizeof(hr_range_lock_t));
493 if (rl == NULL)
494 return NULL;
495
496 rl->vol = vol;
497 rl->off = ba;
498 rl->len = cnt;
499
500 rl->pending = 1;
501 rl->ignore = false;
502
503 link_initialize(&rl->link);
504 fibril_mutex_initialize(&rl->lock);
505
506 fibril_mutex_lock(&rl->lock);
507
508again:
509 HR_RL_LIST_LOCK(vol);
510 list_foreach(vol->range_lock_list, link, hr_range_lock_t, rlp) {
511 if (rlp->ignore)
512 continue;
513 if (hr_range_lock_overlap(rlp, rl)) {
514 rlp->pending++;
515
516 HR_RL_LIST_UNLOCK(vol);
517
518 fibril_mutex_lock(&rlp->lock);
519
520 HR_RL_LIST_LOCK(vol);
521
522 rlp->pending--;
523
524 /*
525 * when ignore is set, after HR_RL_LIST_UNLOCK(),
526 * noone new is going to be able to start sleeping
527 * on the ignored range lock, only already waiting
528 * IOs will come through here
529 */
530 rlp->ignore = true;
531
532 fibril_mutex_unlock(&rlp->lock);
533
534 if (rlp->pending == 0) {
535 list_remove(&rlp->link);
536 free(rlp);
537 }
538
539 HR_RL_LIST_UNLOCK(vol);
540 goto again;
541 }
542 }
543
544 list_append(&rl->link, &vol->range_lock_list);
545
546 HR_RL_LIST_UNLOCK(vol);
547 return rl;
548}
549
550void hr_range_lock_release(hr_range_lock_t *rl)
551{
552 if (rl == NULL)
553 return;
554
555 HR_RL_LIST_LOCK(rl->vol);
556
557 rl->pending--;
558
559 fibril_mutex_unlock(&rl->lock);
560
561 if (rl->pending == 0) {
562 list_remove(&rl->link);
563 free(rl);
564 }
565
566 HR_RL_LIST_UNLOCK(rl->vol);
567}
568
569static bool hr_range_lock_overlap(hr_range_lock_t *rl1, hr_range_lock_t *rl2)
570{
571 uint64_t rl1_start = rl1->off;
572 uint64_t rl1_end = rl1->off + rl1->len - 1;
573 uint64_t rl2_start = rl2->off;
574 uint64_t rl2_end = rl2->off + rl2->len - 1;
575
576 /* one ends before the other starts */
577 if (rl1_end < rl2_start || rl2_end < rl1_start)
578 return false;
579
580 return true;
581}
582
583void hr_mark_vol_state_dirty(hr_volume_t *vol)
584{
585 atomic_store(&vol->state_dirty, true);
586}
587
588struct svc_id_linked {
589 link_t link;
590 service_id_t svc_id;
591 hr_metadata_t *md;
592 bool inited;
593 bool md_present;
594};
595
596static errno_t hr_add_svc_linked_to_list(list_t *list, service_id_t svc_id,
597 bool inited, hr_metadata_t *md)
598{
599 errno_t rc = EOK;
600 struct svc_id_linked *to_add;
601
602 to_add = malloc(sizeof(struct svc_id_linked));
603 if (to_add == NULL) {
604 rc = ENOMEM;
605 goto error;
606 }
607 to_add->svc_id = svc_id;
608 to_add->inited = inited;
609
610 if (md != NULL) {
611 to_add->md = malloc(sizeof(hr_metadata_t));
612 if (to_add->md == NULL) {
613 rc = ENOMEM;
614 goto error;
615 }
616 to_add->md_present = true;
617 memcpy(to_add->md, md, sizeof(*md));
618 } else {
619 to_add->md_present = false;
620 }
621
622 list_append(&to_add->link, list);
623
624error:
625 return rc;
626}
627
628static void free_svc_id_linked(struct svc_id_linked *p)
629{
630 if (p->md_present)
631 free(p->md);
632 free(p);
633}
634
635static void free_svc_id_list(list_t *list)
636{
637 struct svc_id_linked *dev_id;
638 while (!list_empty(list)) {
639 dev_id = list_pop(list, struct svc_id_linked, link);
640 free_svc_id_linked(dev_id);
641 }
642}
643
644static errno_t hr_fill_disk_part_svcs_list(list_t *list)
645{
646 errno_t rc;
647 size_t disk_count;
648 service_id_t *disk_svcs = NULL;
649 vbd_t *vbd = NULL;
650
651 rc = vbd_create(&vbd);
652 if (rc != EOK)
653 goto error;
654
655 rc = vbd_get_disks(vbd, &disk_svcs, &disk_count);
656 if (rc != EOK)
657 goto error;
658
659 for (size_t i = 0; i < disk_count; i++) {
660 vbd_disk_info_t disk_info;
661 rc = vbd_disk_info(vbd, disk_svcs[i], &disk_info);
662 if (rc != EOK)
663 goto error;
664
665 if (disk_info.ltype == lt_none) {
666 rc = hr_add_svc_linked_to_list(list, disk_svcs[i], false, NULL);
667 if (rc != EOK)
668 goto error;
669 } else {
670 size_t part_count;
671 service_id_t *part_ids = NULL;
672 rc = vbd_label_get_parts(vbd, disk_svcs[i], &part_ids, &part_count);
673 if (rc != EOK)
674 goto error;
675
676 for (size_t j = 0; j < part_count; j++) {
677 vbd_part_info_t part_info;
678 rc = vbd_part_get_info(vbd, part_ids[j], &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 list_foreach_safe(*list, cur_link, next_link) {
711 struct svc_id_linked *iter;
712 iter = list_get_instance(cur_link, struct svc_id_linked, 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_svc_id_linked(iter);
724 continue;
725 }
726
727 if (rc != EOK)
728 return rc;
729
730 iter->inited = true;
731 }
732
733 return EOK;
734}
735
736static void block_fini_dev_list(list_t *list)
737{
738 list_foreach(*list, link, struct svc_id_linked, iter) {
739 if (iter->inited) {
740 block_fini(iter->svc_id);
741 iter->inited = false;
742 }
743 }
744}
745
746static errno_t hr_util_get_matching_md_svcs_list(list_t *rlist, list_t *devlist,
747 service_id_t svc_id, hr_metadata_t *md_main)
748{
749 errno_t rc = EOK;
750
751 list_foreach(*devlist, link, struct svc_id_linked, iter) {
752 if (iter->svc_id == svc_id)
753 continue;
754 void *md_block;
755 hr_metadata_t md;
756 rc = hr_get_metadata_block(iter->svc_id, &md_block);
757 if (rc != EOK)
758 goto error;
759 hr_decode_metadata_from_block(md_block, &md);
760
761 free(md_block);
762
763 if (!hr_valid_md_magic(&md))
764 continue;
765
766 if (memcmp(md_main->uuid, md.uuid, HR_UUID_LEN) != 0)
767 continue;
768
769 /*
770 * XXX: can I assume bsize and everything is fine when
771 * UUID matches?
772 */
773
774 rc = hr_add_svc_linked_to_list(rlist, iter->svc_id, true, &md);
775 if (rc != EOK)
776 goto error;
777 }
778
779 return EOK;
780error:
781 free_svc_id_list(rlist);
782 return rc;
783}
784
785static errno_t hr_util_assemble_from_matching_list(list_t *list)
786{
787 HR_DEBUG("%s()", __func__);
788
789 errno_t rc = EOK;
790
791 hr_metadata_t *main_md = NULL;
792 size_t max_counter_val = 0;
793
794 list_foreach(*list, link, struct svc_id_linked, iter) {
795 hr_metadata_dump(iter->md);
796 if (iter->md->counter >= max_counter_val) {
797 max_counter_val = iter->md->counter;
798 main_md = iter->md;
799 }
800 }
801
802 assert(main_md != NULL);
803
804 hr_volume_t *vol;
805 rc = hr_create_vol_struct(&vol, (hr_level_t)main_md->level);
806 if (rc != EOK)
807 goto error;
808
809 vol->nblocks = main_md->nblocks;
810 vol->data_blkno = main_md->data_blkno;
811 vol->truncated_blkno = main_md->truncated_blkno;
812 vol->data_offset = main_md->data_offset;
813 vol->counter = main_md->counter;
814 vol->metadata_version = main_md->version;
815 vol->extent_no = main_md->extent_no;
816 vol->level = main_md->level;
817 vol->layout = main_md->layout;
818 vol->strip_size = main_md->strip_size;
819 vol->bsize = main_md->bsize;
820 memcpy(vol->devname, main_md->devname, HR_DEVNAME_LEN);
821
822 memcpy(vol->in_mem_md, main_md, sizeof(hr_metadata_t));
823
824 list_foreach(*list, link, struct svc_id_linked, iter) {
825 vol->extents[iter->md->index].svc_id = iter->svc_id;
826 if (iter->md->counter == max_counter_val)
827 vol->extents[iter->md->index].status = HR_EXT_ONLINE;
828 else
829 vol->extents[iter->md->index].status = HR_EXT_INVALID;
830 }
831
832 rc = vol->hr_ops.create(vol);
833 if (rc != EOK)
834 goto error;
835
836 fibril_rwlock_write_lock(&hr_volumes_lock);
837
838 list_foreach(hr_volumes, lvolumes, hr_volume_t, other) {
839 uint8_t *our_uuid = vol->in_mem_md->uuid;
840 uint8_t *other_uuid = other->in_mem_md->uuid;
841 if (memcmp(our_uuid, other_uuid, HR_UUID_LEN) == 0) {
842 rc = EEXIST;
843 fibril_rwlock_write_unlock(&hr_volumes_lock);
844 goto error;
845 }
846 }
847
848 /*
849 * XXX: register it here
850 * ... if it fails on EEXIST try different name... like + 1 on the end
851 *
852 * or have metadata edit utility as a part of hrctl..., or create
853 * the original name + 4 random characters, tell the user that the device
854 * was created with this new name, and add a option to hrctl to rename
855 * an active array, and then write the new dirty metadata...
856 *
857 * or just refuse to assemble a name that is already used...
858 *
859 * TODO: discuss
860 */
861 rc = hr_register_volume(vol);
862 if (rc != EOK) {
863 fibril_rwlock_write_unlock(&hr_volumes_lock);
864 goto error;
865 }
866
867 list_append(&vol->lvolumes, &hr_volumes);
868
869 fibril_rwlock_write_unlock(&hr_volumes_lock);
870
871 return EOK;
872error:
873 hr_destroy_vol_struct(vol);
874 return rc;
875}
876
877static errno_t hr_fill_svcs_list_from_cfg(hr_config_t *cfg, list_t *list)
878{
879 errno_t rc = EOK;
880 for (size_t i = 0; i < cfg->dev_no; ++i) {
881 rc = hr_add_svc_linked_to_list(list, cfg->devs[i], false, 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 svc_id_linked *iter;
928 while (!list_empty(&dev_id_list)) {
929 iter = list_pop(&dev_id_list, struct svc_id_linked, link);
930
931 void *metadata_block;
932 hr_metadata_t metadata;
933
934 rc = hr_get_metadata_block(iter->svc_id, &metadata_block);
935 if (rc != EOK)
936 goto error;
937
938 hr_decode_metadata_from_block(metadata_block, &metadata);
939
940 free(metadata_block);
941
942 if (!hr_valid_md_magic(&metadata)) {
943 block_fini(iter->svc_id);
944 free_svc_id_linked(iter);
945 continue;
946 }
947
948 /* hr_metadata_dump(&metadata); */
949
950 char *svc_name = NULL;
951 rc = loc_service_get_name(iter->svc_id, &svc_name);
952 if (rc != EOK)
953 goto error;
954
955 HR_DEBUG("found valid metadata on %s, "
956 "will try to match other extents\n", svc_name);
957
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, &metadata);
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);
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 svc_id_linked,
978 iter2) {
979 struct svc_id_linked *to_remove;
980 list_foreach_safe(dev_id_list, cur_link, next_link) {
981 to_remove = list_get_instance(cur_link,
982 struct svc_id_linked, link);
983 if (to_remove->svc_id == iter2->svc_id) {
984 list_remove(cur_link);
985 free_svc_id_linked(to_remove);
986 }
987 }
988 }
989
990 rc = hr_util_assemble_from_matching_list(&matching_svcs_list);
991 switch (rc) {
992 case EOK:
993 asm_cnt++;
994 break;
995 case EEXIST:
996 /*
997 * A race is detected this way, because we don't want
998 * to hold the hr_volumes list lock for a long time,
999 * for all assembly attempts. XXX: discuss...
1000 */
1001 rc = EOK;
1002 break;
1003 default:
1004 block_fini_dev_list(&matching_svcs_list);
1005 free_svc_id_list(&matching_svcs_list);
1006 goto error;
1007 }
1008
1009 free_svc_id_list(&matching_svcs_list);
1010 }
1011
1012error:
1013 if (rassembled_cnt != NULL)
1014 *rassembled_cnt = asm_cnt;
1015
1016 block_fini_dev_list(&dev_id_list);
1017 free_svc_id_list(&dev_id_list);
1018
1019 return rc;
1020}
1021
1022errno_t hr_util_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
1023{
1024 HR_DEBUG("%s()", __func__);
1025
1026 errno_t rc = EOK;
1027
1028 fibril_mutex_lock(&vol->hotspare_lock);
1029
1030 if (vol->hotspare_no >= HR_MAX_HOTSPARES) {
1031 HR_ERROR("%s(): cannot add more hotspares "
1032 "to \"%s\"\n", __func__, vol->devname);
1033 rc = ELIMIT;
1034 goto end;
1035 }
1036
1037 rc = block_init(hotspare);
1038 if (rc != EOK)
1039 goto end;
1040
1041 size_t hs_idx = vol->hotspare_no;
1042
1043 vol->hotspare_no++;
1044
1045 hr_update_hotspare_svc_id(vol, hs_idx, hotspare);
1046 hr_update_hotspare_status(vol, hs_idx, HR_EXT_HOTSPARE);
1047
1048 hr_mark_vol_state_dirty(vol);
1049end:
1050 fibril_mutex_unlock(&vol->hotspare_lock);
1051 return rc;
1052}
1053
1054/** @}
1055 */
Note: See TracBrowser for help on using the repository browser.