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

Last change on this file since 59ec1c50 was 59ec1c50, checked in by Miroslav Cimerman <mc@…>, 3 months ago

hr: util.c: fix possible null deref

  • Property mode set to 100644
File size: 24.0 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, hr_metadata_type_t, void *);
67static errno_t hr_util_assemble_from_matching_list(list_t *, 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.status_event = hr_raid0_status_event;
101 break;
102 case HR_LVL_1:
103 vol->hr_ops.create = hr_raid1_create;
104 vol->hr_ops.init = hr_raid1_init;
105 vol->hr_ops.status_event = hr_raid1_status_event;
106 if (meta_flags & HR_METADATA_HOTSPARE_SUPPORT)
107 vol->hr_ops.add_hotspare = hr_raid1_add_hotspare;
108 break;
109 case HR_LVL_4:
110 case HR_LVL_5:
111 vol->hr_ops.create = hr_raid5_create;
112 vol->hr_ops.init = hr_raid5_init;
113 vol->hr_ops.status_event = hr_raid5_status_event;
114 if (meta_flags & HR_METADATA_HOTSPARE_SUPPORT)
115 vol->hr_ops.add_hotspare = hr_raid5_add_hotspare;
116 break;
117 default:
118 HR_DEBUG("unkown level: %d, aborting\n", vol->level);
119 rc = EINVAL;
120 goto error;
121 }
122
123 vol->fge = hr_fpool_create(16, 32, sizeof(hr_io_t));
124 if (vol->fge == NULL) {
125 rc = ENOMEM;
126 goto error;
127 }
128
129 vol->in_mem_md = vol->meta_ops->alloc_struct();
130 if (vol->in_mem_md == NULL) {
131 free(vol->fge);
132 rc = ENOMEM;
133 goto error;
134 }
135
136 vol->status = HR_VOL_NONE;
137
138 fibril_mutex_initialize(&vol->lock); /* XXX: will remove this */
139
140 fibril_mutex_initialize(&vol->md_lock);
141
142 fibril_rwlock_initialize(&vol->extents_lock);
143 fibril_rwlock_initialize(&vol->states_lock);
144
145 fibril_mutex_initialize(&vol->hotspare_lock);
146
147 list_initialize(&vol->range_lock_list);
148 fibril_mutex_initialize(&vol->range_lock_list_lock);
149
150 atomic_init(&vol->rebuild_blk, 0);
151 atomic_init(&vol->state_dirty, false);
152 atomic_init(&vol->open_cnt, 0);
153
154 *rvol = vol;
155
156 return EOK;
157error:
158 free(vol);
159 return rc;
160}
161
162void hr_destroy_vol_struct(hr_volume_t *vol)
163{
164 if (vol == NULL)
165 return;
166
167 hr_fpool_destroy(vol->fge);
168 hr_fini_devs(vol);
169 free(vol->in_mem_md);
170 free(vol);
171}
172
173hr_volume_t *hr_get_volume(service_id_t svc_id)
174{
175 HR_DEBUG("hr_get_volume(): (%" PRIun ")\n", svc_id);
176
177 hr_volume_t *rvol = NULL;
178
179 fibril_rwlock_read_lock(&hr_volumes_lock);
180 list_foreach(hr_volumes, lvolumes, hr_volume_t, iter) {
181 if (iter->svc_id == svc_id) {
182 rvol = iter;
183 break;
184 }
185 }
186
187 fibril_rwlock_read_unlock(&hr_volumes_lock);
188 return rvol;
189}
190
191errno_t hr_remove_volume(service_id_t svc_id)
192{
193 HR_DEBUG("hr_remove_volume(): (%" PRIun ")\n", svc_id);
194
195 errno_t rc;
196
197 fibril_rwlock_write_lock(&hr_volumes_lock);
198 list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
199 if (vol->svc_id == svc_id) {
200 int open_cnt = atomic_load_explicit(&vol->open_cnt,
201 memory_order_relaxed);
202 /*
203 * The "atomicity" of this if condition is provided
204 * by the write lock - no new bd connection can
205 * come, because we need to get the bd_srvs_t from
206 * volume, which we get from the list.
207 * (see hr_client_conn() in hr.c)
208 */
209 if (open_cnt > 0) {
210 fibril_rwlock_write_unlock(&hr_volumes_lock);
211 return EBUSY;
212 }
213 list_remove(&vol->lvolumes);
214 fibril_rwlock_write_unlock(&hr_volumes_lock);
215
216 vol->meta_ops->save(vol, NO_STATE_CALLBACK);
217
218 hr_destroy_vol_struct(vol);
219
220 rc = loc_service_unregister(hr_srv, svc_id);
221 return rc;
222 }
223 }
224
225 fibril_rwlock_write_unlock(&hr_volumes_lock);
226 return ENOENT;
227}
228
229errno_t hr_init_extents_from_cfg(hr_volume_t *vol, hr_config_t *cfg)
230{
231 HR_DEBUG("%s()", __func__);
232
233 errno_t rc;
234 uint64_t blkno, smallest_blkno = ~0ULL;
235 size_t i, bsize;
236 size_t last_bsize = 0;
237
238 for (i = 0; i < cfg->dev_no; i++) {
239 service_id_t svc_id = cfg->devs[i];
240 if (svc_id == 0) {
241 rc = EINVAL;
242 goto error;
243 }
244
245 HR_DEBUG("%s(): block_init() on (%" PRIun ")\n", __func__,
246 svc_id);
247 rc = block_init(svc_id);
248 if (rc != EOK) {
249 HR_DEBUG("%s(): initing (%" PRIun ") failed, "
250 "aborting\n", __func__, svc_id);
251 goto error;
252 }
253
254 rc = block_get_nblocks(svc_id, &blkno);
255 if (rc != EOK)
256 goto error;
257
258 rc = block_get_bsize(svc_id, &bsize);
259 if (rc != EOK)
260 goto error;
261
262 if (last_bsize != 0 && bsize != last_bsize) {
263 HR_DEBUG("block sizes differ\n");
264 rc = EINVAL;
265 goto error;
266 }
267
268 vol->extents[i].svc_id = svc_id;
269 vol->extents[i].status = HR_EXT_ONLINE;
270
271 if (blkno < smallest_blkno)
272 smallest_blkno = blkno;
273 last_bsize = bsize;
274 }
275
276 vol->bsize = last_bsize;
277 vol->extent_no = cfg->dev_no;
278 vol->truncated_blkno = smallest_blkno;
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 iter->fini = true;
731 }
732
733 return EOK;
734}
735
736static void block_fini_dev_list(list_t *list)
737{
738 HR_DEBUG("%s()", __func__);
739
740 list_foreach(*list, link, struct dev_list_member, iter) {
741 if (iter->inited && iter->fini) {
742 block_fini(iter->svc_id);
743 iter->inited = false;
744 iter->fini = false;
745 }
746 }
747}
748
749static errno_t hr_util_get_matching_md_svcs_list(list_t *rlist, list_t *list,
750 service_id_t svc_id, hr_metadata_type_t type_main,
751 void *metadata_struct_main)
752{
753 HR_DEBUG("%s()", __func__);
754
755 errno_t rc = EOK;
756
757 hr_superblock_ops_t *meta_ops = get_type_ops(type_main);
758
759 list_foreach(*list, link, struct dev_list_member, iter) {
760 if (iter->svc_id == svc_id)
761 continue;
762
763 void *metadata_struct;
764 hr_metadata_type_t type;
765
766 rc = find_metadata(iter->svc_id, &metadata_struct, &type);
767 if (rc == ENOFS)
768 continue;
769 if (rc != EOK)
770 goto error;
771
772 if (type != type_main) {
773 free(metadata_struct);
774 continue;
775 }
776
777 if (!meta_ops->compare_uuids(metadata_struct_main,
778 metadata_struct)) {
779 free(metadata_struct);
780 continue;
781 }
782
783 rc = hr_add_svc_linked_to_list(rlist, iter->svc_id, true,
784 metadata_struct);
785 if (rc != EOK)
786 goto error;
787 }
788
789 return EOK;
790error:
791 free_svc_id_list(rlist);
792 return rc;
793}
794
795static errno_t hr_util_assemble_from_matching_list(list_t *list,
796 hr_metadata_type_t type)
797{
798 HR_DEBUG("%s()", __func__);
799
800 errno_t rc = EOK;
801
802 hr_superblock_ops_t *meta_ops = get_type_ops(type);
803
804 link_t *memb_l = list_first(list);
805 struct dev_list_member *memb = list_get_instance(memb_l,
806 struct dev_list_member, link);
807
808 hr_level_t level = meta_ops->get_level(memb->md);
809 const char *devname = meta_ops->get_devname(memb->md);
810
811 hr_volume_t *vol;
812 rc = hr_create_vol_struct(&vol, level, devname, type);
813 if (rc != EOK)
814 return rc;
815
816 meta_ops->init_meta2vol(list, vol);
817
818 /*
819 * TODO: something like mark md dirty or whatever
820 * - probably will be handled by each md type differently,
821 * by specific function pointers
822 * - deal with this when foreign md will be handled
823 *
824 * XXX: if thats the only thing that can change in metadata
825 * during volume runtime, then whatever, but if more
826 * things will need to be synced, think of something more clever
827 *
828 * TODO: remove from here and increment it the "first" time (if nothing
829 * happens - no state changes, no rebuild, etc) - only after the first
830 * write... but for now leave it here
831 */
832 (void)vol->meta_ops->inc_counter(vol->in_mem_md);
833
834 rc = vol->hr_ops.create(vol);
835 if (rc != EOK)
836 goto error;
837
838 fibril_rwlock_write_lock(&hr_volumes_lock);
839
840 /*
841 * XXX: register it here
842 * ... if it fails on EEXIST try different name... like + 1 on the end
843 *
844 * or have metadata edit utility as a part of hrctl..., or create
845 * the original name + 4 random characters, tell the user that the device
846 * was created with this new name, and add a option to hrctl to rename
847 * an active array, and then write the new dirty metadata...
848 *
849 * or just refuse to assemble a name that is already used...
850 *
851 * TODO: discuss
852 */
853 rc = hr_register_volume(vol);
854 if (rc != EOK) {
855 fibril_rwlock_write_unlock(&hr_volumes_lock);
856 goto error;
857 }
858
859 (void)vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
860
861 list_append(&vol->lvolumes, &hr_volumes);
862
863 fibril_rwlock_write_unlock(&hr_volumes_lock);
864
865 return EOK;
866error:
867 hr_destroy_vol_struct(vol);
868 return rc;
869}
870
871static errno_t hr_fill_svcs_list_from_cfg(hr_config_t *cfg, list_t *list)
872{
873 HR_DEBUG("%s()", __func__);
874
875 errno_t rc = EOK;
876 for (size_t i = 0; i < cfg->dev_no; ++i) {
877 rc = hr_add_svc_linked_to_list(list, cfg->devs[i], false,
878 NULL);
879 if (rc != EOK)
880 goto error;
881 }
882
883 return EOK;
884error:
885 free_svc_id_list(list);
886 return rc;
887}
888
889errno_t hr_util_try_assemble(hr_config_t *cfg, size_t *rassembled_cnt)
890{
891 HR_DEBUG("%s()", __func__);
892
893 /*
894 * scan partitions or disks:
895 *
896 * When we find a metadata block with valid
897 * magic, take UUID and try to find other matching
898 * UUIDs.
899 *
900 * We ignore extents that are a part of already
901 * active volumes. (even when the counter is lower
902 * on active volumes... XXX: use timestamp as initial counter value
903 * when assembling, or writing dirty metadata?)
904 */
905
906 size_t asm_cnt = 0;
907 errno_t rc;
908 list_t dev_id_list;
909
910 list_initialize(&dev_id_list);
911
912 if (cfg == NULL)
913 rc = hr_fill_disk_part_svcs_list(&dev_id_list);
914 else
915 rc = hr_fill_svcs_list_from_cfg(cfg, &dev_id_list);
916
917 if (rc != EOK)
918 goto error;
919
920 rc = block_init_dev_list(&dev_id_list);
921 if (rc != EOK)
922 goto error;
923
924 struct dev_list_member *iter;
925 while (!list_empty(&dev_id_list)) {
926 iter = list_pop(&dev_id_list, struct dev_list_member, link);
927
928 void *metadata_struct_main;
929 hr_metadata_type_t type;
930
931 rc = find_metadata(iter->svc_id, &metadata_struct_main, &type);
932 if (rc == ENOFS) {
933 block_fini(iter->svc_id);
934 free_dev_list_member(iter);
935 rc = EOK;
936 continue;
937 }
938
939 if (rc != EOK)
940 goto error;
941
942 char *svc_name = NULL;
943 rc = loc_service_get_name(iter->svc_id, &svc_name);
944 if (rc != EOK)
945 goto error;
946 HR_DEBUG("found valid metadata on %s (type = %s), matching "
947 "other extents\n",
948 svc_name, hr_get_metadata_type_str(type));
949 free(svc_name);
950
951 list_t matching_svcs_list;
952 list_initialize(&matching_svcs_list);
953
954 rc = hr_util_get_matching_md_svcs_list(&matching_svcs_list,
955 &dev_id_list, iter->svc_id, type, metadata_struct_main);
956 if (rc != EOK)
957 goto error;
958
959 /* add current iter to list as well */
960 rc = hr_add_svc_linked_to_list(&matching_svcs_list,
961 iter->svc_id, true, metadata_struct_main);
962 if (rc != EOK) {
963 free_svc_id_list(&matching_svcs_list);
964 goto error;
965 }
966
967 /* remove matching list members from dev_id_list */
968 list_foreach(matching_svcs_list, link, struct dev_list_member,
969 iter2) {
970 struct dev_list_member *to_remove;
971 list_foreach_safe(dev_id_list, cur_link, next_link) {
972 to_remove = list_get_instance(cur_link,
973 struct dev_list_member, link);
974 if (to_remove->svc_id == iter2->svc_id) {
975 list_remove(cur_link);
976 free_dev_list_member(to_remove);
977 }
978 }
979 }
980
981 rc = hr_util_assemble_from_matching_list(&matching_svcs_list,
982 type);
983 switch (rc) {
984 case EOK:
985 asm_cnt++;
986 break;
987 case ENOMEM:
988 goto error;
989 default:
990 rc = EOK;
991 }
992 block_fini_dev_list(&matching_svcs_list);
993 free_svc_id_list(&matching_svcs_list);
994 }
995
996error:
997 if (rassembled_cnt != NULL)
998 *rassembled_cnt = asm_cnt;
999
1000 block_fini_dev_list(&dev_id_list);
1001 free_svc_id_list(&dev_id_list);
1002
1003 return rc;
1004}
1005
1006errno_t hr_util_add_hotspare(hr_volume_t *vol, service_id_t hotspare)
1007{
1008 HR_DEBUG("%s()", __func__);
1009
1010 errno_t rc = EOK;
1011
1012 fibril_mutex_lock(&vol->hotspare_lock);
1013
1014 if (vol->hotspare_no >= HR_MAX_HOTSPARES) {
1015 HR_ERROR("%s(): cannot add more hotspares "
1016 "to \"%s\"\n", __func__, vol->devname);
1017 rc = ELIMIT;
1018 goto error;
1019 }
1020
1021 rc = block_init(hotspare);
1022 if (rc != EOK)
1023 goto error;
1024
1025 uint64_t hs_blkno;
1026 rc = block_get_nblocks(hotspare, &hs_blkno);
1027 if (rc != EOK) {
1028 block_fini(hotspare);
1029 goto error;
1030 }
1031
1032 if (hs_blkno < vol->truncated_blkno - vol->meta_ops->get_size()) {
1033 rc = EINVAL;
1034 block_fini(hotspare);
1035 goto error;
1036 }
1037
1038 size_t hs_idx = vol->hotspare_no;
1039
1040 vol->hotspare_no++;
1041
1042 hr_update_hotspare_svc_id(vol, hs_idx, hotspare);
1043 hr_update_hotspare_status(vol, hs_idx, HR_EXT_HOTSPARE);
1044
1045 hr_mark_vol_state_dirty(vol);
1046error:
1047 fibril_mutex_unlock(&vol->hotspare_lock);
1048 return rc;
1049}
1050
1051/** @}
1052 */
Note: See TracBrowser for help on using the repository browser.