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