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