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

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

hr: rename data_dirty to first_write

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