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

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

hr: rename all strings status' -> state'

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