| 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 <abi/ipc/ipc.h>
|
|---|
| 37 | #include <bd_srv.h>
|
|---|
| 38 | #include <block.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <hr.h>
|
|---|
| 41 | #include <io/log.h>
|
|---|
| 42 | #include <ipc/hr.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <loc.h>
|
|---|
| 45 | #include <task.h>
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 | #include <stdlib.h>
|
|---|
| 48 | #include <str_error.h>
|
|---|
| 49 |
|
|---|
| 50 | #include "io.h"
|
|---|
| 51 | #include "superblock.h"
|
|---|
| 52 | #include "util.h"
|
|---|
| 53 | #include "var.h"
|
|---|
| 54 |
|
|---|
| 55 | static void hr_raid0_update_vol_status(hr_volume_t *);
|
|---|
| 56 | static void hr_raid0_state_callback(hr_volume_t *, size_t, errno_t);
|
|---|
| 57 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t, bd_srv_t *, aoff64_t, size_t,
|
|---|
| 58 | void *, const void *, size_t);
|
|---|
| 59 |
|
|---|
| 60 | /* bdops */
|
|---|
| 61 | static errno_t hr_raid0_bd_open(bd_srvs_t *, bd_srv_t *);
|
|---|
| 62 | static errno_t hr_raid0_bd_close(bd_srv_t *);
|
|---|
| 63 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
|---|
| 64 | size_t);
|
|---|
| 65 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
|---|
| 66 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
|---|
| 67 | const void *, size_t);
|
|---|
| 68 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *, size_t *);
|
|---|
| 69 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
|---|
| 70 |
|
|---|
| 71 | static bd_ops_t hr_raid0_bd_ops = {
|
|---|
| 72 | .open = hr_raid0_bd_open,
|
|---|
| 73 | .close = hr_raid0_bd_close,
|
|---|
| 74 | .sync_cache = hr_raid0_bd_sync_cache,
|
|---|
| 75 | .read_blocks = hr_raid0_bd_read_blocks,
|
|---|
| 76 | .write_blocks = hr_raid0_bd_write_blocks,
|
|---|
| 77 | .get_block_size = hr_raid0_bd_get_block_size,
|
|---|
| 78 | .get_num_blocks = hr_raid0_bd_get_num_blocks
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | extern loc_srv_t *hr_srv;
|
|---|
| 82 |
|
|---|
| 83 | errno_t hr_raid0_create(hr_volume_t *new_volume)
|
|---|
| 84 | {
|
|---|
| 85 | HR_DEBUG("%s()", __func__);
|
|---|
| 86 |
|
|---|
| 87 | assert(new_volume->level == HR_LVL_0);
|
|---|
| 88 |
|
|---|
| 89 | if (new_volume->extent_no < 2) {
|
|---|
| 90 | HR_ERROR("RAID 0 array needs at least 2 devices\n");
|
|---|
| 91 | return EINVAL;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | hr_raid0_update_vol_status(new_volume);
|
|---|
| 95 | if (new_volume->status != HR_VOL_ONLINE) {
|
|---|
| 96 | HR_NOTE("\"%s\": unusable state, not creating\n",
|
|---|
| 97 | new_volume->devname);
|
|---|
| 98 | return EINVAL;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | bd_srvs_init(&new_volume->hr_bds);
|
|---|
| 102 | new_volume->hr_bds.ops = &hr_raid0_bd_ops;
|
|---|
| 103 | new_volume->hr_bds.sarg = new_volume;
|
|---|
| 104 |
|
|---|
| 105 | new_volume->state_callback = hr_raid0_state_callback;
|
|---|
| 106 |
|
|---|
| 107 | return EOK;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /*
|
|---|
| 111 | * Called only once in volume's lifetime.
|
|---|
| 112 | */
|
|---|
| 113 | errno_t hr_raid0_init(hr_volume_t *vol)
|
|---|
| 114 | {
|
|---|
| 115 | HR_DEBUG("%s()", __func__);
|
|---|
| 116 |
|
|---|
| 117 | assert(vol->level == HR_LVL_0);
|
|---|
| 118 |
|
|---|
| 119 | uint64_t total_blkno = vol->truncated_blkno * vol->extent_no;
|
|---|
| 120 |
|
|---|
| 121 | vol->data_offset = vol->meta_ops->get_data_offset();
|
|---|
| 122 |
|
|---|
| 123 | vol->data_blkno = total_blkno;
|
|---|
| 124 | /* count md blocks */
|
|---|
| 125 | vol->data_blkno -= vol->meta_ops->get_size() * vol->extent_no;
|
|---|
| 126 |
|
|---|
| 127 | vol->strip_size = HR_STRIP_SIZE;
|
|---|
| 128 |
|
|---|
| 129 | return EOK;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void hr_raid0_status_event(hr_volume_t *vol)
|
|---|
| 133 | {
|
|---|
| 134 | HR_DEBUG("%s()", __func__);
|
|---|
| 135 |
|
|---|
| 136 | hr_raid0_update_vol_status(vol);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
|---|
| 140 | {
|
|---|
| 141 | HR_DEBUG("%s()", __func__);
|
|---|
| 142 |
|
|---|
| 143 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 144 |
|
|---|
| 145 | atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
|
|---|
| 146 |
|
|---|
| 147 | return EOK;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | static errno_t hr_raid0_bd_close(bd_srv_t *bd)
|
|---|
| 151 | {
|
|---|
| 152 | HR_DEBUG("%s()", __func__);
|
|---|
| 153 |
|
|---|
| 154 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 155 |
|
|---|
| 156 | atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
|
|---|
| 157 |
|
|---|
| 158 | return EOK;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
|---|
| 162 | {
|
|---|
| 163 | return hr_raid0_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 167 | void *buf, size_t size)
|
|---|
| 168 | {
|
|---|
| 169 | return hr_raid0_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 173 | const void *data, size_t size)
|
|---|
| 174 | {
|
|---|
| 175 | return hr_raid0_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
|---|
| 179 | {
|
|---|
| 180 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 181 |
|
|---|
| 182 | *rsize = vol->bsize;
|
|---|
| 183 | return EOK;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
|---|
| 187 | {
|
|---|
| 188 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 189 |
|
|---|
| 190 | *rnb = vol->data_blkno;
|
|---|
| 191 | return EOK;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | static void hr_raid0_update_vol_status(hr_volume_t *vol)
|
|---|
| 195 | {
|
|---|
| 196 | fibril_mutex_lock(&vol->md_lock);
|
|---|
| 197 |
|
|---|
| 198 | vol->meta_ops->inc_counter(vol->in_mem_md);
|
|---|
| 199 | /* TODO: save right away */
|
|---|
| 200 |
|
|---|
| 201 | fibril_mutex_unlock(&vol->md_lock);
|
|---|
| 202 |
|
|---|
| 203 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 204 |
|
|---|
| 205 | hr_vol_status_t old_state = vol->status;
|
|---|
| 206 |
|
|---|
| 207 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 208 | if (vol->extents[i].status != HR_EXT_ONLINE) {
|
|---|
| 209 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 210 |
|
|---|
| 211 | if (old_state != HR_VOL_FAULTY) {
|
|---|
| 212 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 213 | hr_update_vol_status(vol, HR_VOL_FAULTY);
|
|---|
| 214 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 215 | }
|
|---|
| 216 | return;
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 220 |
|
|---|
| 221 | if (old_state != HR_VOL_ONLINE) {
|
|---|
| 222 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 223 | hr_update_vol_status(vol, HR_VOL_ONLINE);
|
|---|
| 224 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | static void hr_raid0_state_callback(hr_volume_t *vol, size_t extent, errno_t rc)
|
|---|
| 229 | {
|
|---|
| 230 | if (rc == EOK)
|
|---|
| 231 | return;
|
|---|
| 232 |
|
|---|
| 233 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 234 |
|
|---|
| 235 | switch (rc) {
|
|---|
| 236 | case ENOENT:
|
|---|
| 237 | hr_update_ext_status(vol, extent, HR_EXT_MISSING);
|
|---|
| 238 | break;
|
|---|
| 239 | default:
|
|---|
| 240 | hr_update_ext_status(vol, extent, HR_EXT_FAILED);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | hr_update_vol_status(vol, HR_VOL_FAULTY);
|
|---|
| 244 |
|
|---|
| 245 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
|---|
| 249 | size_t cnt, void *dst, const void *src, size_t size)
|
|---|
| 250 | {
|
|---|
| 251 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 252 | errno_t rc;
|
|---|
| 253 | uint64_t phys_block, len;
|
|---|
| 254 | size_t left;
|
|---|
| 255 | const uint8_t *data_write = src;
|
|---|
| 256 | uint8_t *data_read = dst;
|
|---|
| 257 |
|
|---|
| 258 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 259 | if (vol->status != HR_VOL_ONLINE) {
|
|---|
| 260 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 261 | return EIO;
|
|---|
| 262 | }
|
|---|
| 263 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 264 |
|
|---|
| 265 | /* propagate sync */
|
|---|
| 266 | if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
|
|---|
| 267 | hr_fgroup_t *group = hr_fgroup_create(vol->fge,
|
|---|
| 268 | vol->extent_no);
|
|---|
| 269 | if (group == NULL)
|
|---|
| 270 | return ENOMEM;
|
|---|
| 271 |
|
|---|
| 272 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 273 | hr_io_t *io = hr_fgroup_alloc(group);
|
|---|
| 274 | io->extent = i;
|
|---|
| 275 | io->ba = ba;
|
|---|
| 276 | io->cnt = cnt;
|
|---|
| 277 | io->type = type;
|
|---|
| 278 | io->vol = vol;
|
|---|
| 279 |
|
|---|
| 280 | hr_fgroup_submit(group, hr_io_worker, io);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | size_t bad;
|
|---|
| 284 | rc = hr_fgroup_wait(group, NULL, &bad);
|
|---|
| 285 | if (rc == ENOMEM)
|
|---|
| 286 | return ENOMEM;
|
|---|
| 287 |
|
|---|
| 288 | if (bad > 0)
|
|---|
| 289 | return EIO;
|
|---|
| 290 |
|
|---|
| 291 | return EOK;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
|---|
| 295 | if (size < cnt * vol->bsize)
|
|---|
| 296 | return EINVAL;
|
|---|
| 297 |
|
|---|
| 298 | rc = hr_check_ba_range(vol, cnt, ba);
|
|---|
| 299 | if (rc != EOK)
|
|---|
| 300 | return rc;
|
|---|
| 301 |
|
|---|
| 302 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
|---|
| 303 | uint64_t strip_no = ba / strip_size;
|
|---|
| 304 | uint64_t extent = strip_no % vol->extent_no;
|
|---|
| 305 | uint64_t stripe = strip_no / vol->extent_no;
|
|---|
| 306 | uint64_t strip_off = ba % strip_size;
|
|---|
| 307 |
|
|---|
| 308 | left = cnt;
|
|---|
| 309 |
|
|---|
| 310 | /* calculate how many strips does the IO span */
|
|---|
| 311 | size_t end_strip_no = (ba + cnt - 1) / strip_size;
|
|---|
| 312 | size_t span = end_strip_no - strip_no + 1;
|
|---|
| 313 |
|
|---|
| 314 | hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
|
|---|
| 315 | if (group == NULL)
|
|---|
| 316 | return ENOMEM;
|
|---|
| 317 |
|
|---|
| 318 | while (left != 0) {
|
|---|
| 319 | phys_block = stripe * strip_size + strip_off;
|
|---|
| 320 | cnt = min(left, strip_size - strip_off);
|
|---|
| 321 | len = vol->bsize * cnt;
|
|---|
| 322 | hr_add_ba_offset(vol, &phys_block);
|
|---|
| 323 |
|
|---|
| 324 | hr_io_t *io = hr_fgroup_alloc(group);
|
|---|
| 325 | io->extent = extent;
|
|---|
| 326 | io->data_write = data_write;
|
|---|
| 327 | io->data_read = data_read;
|
|---|
| 328 | io->ba = phys_block;
|
|---|
| 329 | io->cnt = cnt;
|
|---|
| 330 | io->type = type;
|
|---|
| 331 | io->vol = vol;
|
|---|
| 332 |
|
|---|
| 333 | hr_fgroup_submit(group, hr_io_worker, io);
|
|---|
| 334 |
|
|---|
| 335 | left -= cnt;
|
|---|
| 336 | if (left == 0)
|
|---|
| 337 | break;
|
|---|
| 338 |
|
|---|
| 339 | if (type == HR_BD_READ)
|
|---|
| 340 | data_read += len;
|
|---|
| 341 | else if (type == HR_BD_WRITE)
|
|---|
| 342 | data_write += len;
|
|---|
| 343 |
|
|---|
| 344 | strip_off = 0;
|
|---|
| 345 | extent++;
|
|---|
| 346 | if (extent >= vol->extent_no) {
|
|---|
| 347 | stripe++;
|
|---|
| 348 | extent = 0;
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | size_t bad;
|
|---|
| 353 | rc = hr_fgroup_wait(group, NULL, &bad);
|
|---|
| 354 | if (rc == ENOMEM && type == HR_BD_READ)
|
|---|
| 355 | return ENOMEM;
|
|---|
| 356 |
|
|---|
| 357 | if (bad > 0)
|
|---|
| 358 | return EIO;
|
|---|
| 359 |
|
|---|
| 360 | return EOK;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /** @}
|
|---|
| 364 | */
|
|---|