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