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

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

srv/hr/util.c: change log level LVL_WARN to LVL_NOTE

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