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

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

hr: add malloc_waitok() and calloc_waitok()

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