| 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 | return EINVAL;
|
|---|
| 97 |
|
|---|
| 98 | bd_srvs_init(&new_volume->hr_bds);
|
|---|
| 99 | new_volume->hr_bds.ops = &hr_raid0_bd_ops;
|
|---|
| 100 | new_volume->hr_bds.sarg = new_volume;
|
|---|
| 101 |
|
|---|
| 102 | new_volume->state_callback = hr_raid0_state_callback;
|
|---|
| 103 |
|
|---|
| 104 | return EOK;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /*
|
|---|
| 108 | * Called only once in volume's lifetime.
|
|---|
| 109 | */
|
|---|
| 110 | errno_t hr_raid0_init(hr_volume_t *vol)
|
|---|
| 111 | {
|
|---|
| 112 | HR_DEBUG("%s()", __func__);
|
|---|
| 113 |
|
|---|
| 114 | assert(vol->level == HR_LVL_0);
|
|---|
| 115 |
|
|---|
| 116 | uint64_t truncated_blkno = vol->extents[0].blkno;
|
|---|
| 117 | for (size_t i = 1; i < vol->extent_no; i++) {
|
|---|
| 118 | if (vol->extents[i].blkno < truncated_blkno)
|
|---|
| 119 | truncated_blkno = vol->extents[i].blkno;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | uint64_t total_blkno = truncated_blkno * vol->extent_no;
|
|---|
| 123 |
|
|---|
| 124 | vol->truncated_blkno = truncated_blkno;
|
|---|
| 125 | vol->nblocks = total_blkno;
|
|---|
| 126 | vol->data_offset = vol->meta_ops->get_data_offset();
|
|---|
| 127 |
|
|---|
| 128 | vol->data_blkno = total_blkno;
|
|---|
| 129 | /* count md blocks */
|
|---|
| 130 | vol->data_blkno -= vol->meta_ops->get_size() * vol->extent_no;
|
|---|
| 131 |
|
|---|
| 132 | vol->strip_size = HR_STRIP_SIZE;
|
|---|
| 133 |
|
|---|
| 134 | return EOK;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void hr_raid0_status_event(hr_volume_t *vol)
|
|---|
| 138 | {
|
|---|
| 139 | HR_DEBUG("%s()", __func__);
|
|---|
| 140 |
|
|---|
| 141 | hr_raid0_update_vol_status(vol);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
|---|
| 145 | {
|
|---|
| 146 | HR_DEBUG("%s()", __func__);
|
|---|
| 147 |
|
|---|
| 148 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 149 |
|
|---|
| 150 | atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
|
|---|
| 151 |
|
|---|
| 152 | return EOK;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | static errno_t hr_raid0_bd_close(bd_srv_t *bd)
|
|---|
| 156 | {
|
|---|
| 157 | HR_DEBUG("%s()", __func__);
|
|---|
| 158 |
|
|---|
| 159 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 160 |
|
|---|
| 161 | atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
|
|---|
| 162 |
|
|---|
| 163 | return EOK;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
|---|
| 167 | {
|
|---|
| 168 | return hr_raid0_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 172 | void *buf, size_t size)
|
|---|
| 173 | {
|
|---|
| 174 | return hr_raid0_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 178 | const void *data, size_t size)
|
|---|
| 179 | {
|
|---|
| 180 | return hr_raid0_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
|---|
| 184 | {
|
|---|
| 185 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 186 |
|
|---|
| 187 | *rsize = vol->bsize;
|
|---|
| 188 | return EOK;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
|---|
| 192 | {
|
|---|
| 193 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 194 |
|
|---|
| 195 | *rnb = vol->data_blkno;
|
|---|
| 196 | return EOK;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | static void hr_raid0_update_vol_status(hr_volume_t *vol)
|
|---|
| 200 | {
|
|---|
| 201 | fibril_mutex_lock(&vol->md_lock);
|
|---|
| 202 |
|
|---|
| 203 | vol->meta_ops->inc_counter(vol->in_mem_md);
|
|---|
| 204 | /* TODO: save right away */
|
|---|
| 205 |
|
|---|
| 206 | fibril_mutex_unlock(&vol->md_lock);
|
|---|
| 207 |
|
|---|
| 208 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 209 |
|
|---|
| 210 | hr_vol_status_t old_state = vol->status;
|
|---|
| 211 |
|
|---|
| 212 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 213 | if (vol->extents[i].status != HR_EXT_ONLINE) {
|
|---|
| 214 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 215 |
|
|---|
| 216 | if (old_state != HR_VOL_FAULTY) {
|
|---|
| 217 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 218 | hr_update_vol_status(vol, HR_VOL_FAULTY);
|
|---|
| 219 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 220 | }
|
|---|
| 221 | return;
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 225 |
|
|---|
| 226 | if (old_state != HR_VOL_ONLINE) {
|
|---|
| 227 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 228 | hr_update_vol_status(vol, HR_VOL_ONLINE);
|
|---|
| 229 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | static void hr_raid0_state_callback(hr_volume_t *vol, size_t extent, errno_t rc)
|
|---|
| 234 | {
|
|---|
| 235 | if (rc == EOK)
|
|---|
| 236 | return;
|
|---|
| 237 |
|
|---|
| 238 | fibril_rwlock_write_lock(&vol->states_lock);
|
|---|
| 239 |
|
|---|
| 240 | switch (rc) {
|
|---|
| 241 | case ENOENT:
|
|---|
| 242 | hr_update_ext_status(vol, extent, HR_EXT_MISSING);
|
|---|
| 243 | break;
|
|---|
| 244 | default:
|
|---|
| 245 | hr_update_ext_status(vol, extent, HR_EXT_FAILED);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | hr_update_vol_status(vol, HR_VOL_FAULTY);
|
|---|
| 249 |
|
|---|
| 250 | fibril_rwlock_write_unlock(&vol->states_lock);
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
|---|
| 254 | size_t cnt, void *dst, const void *src, size_t size)
|
|---|
| 255 | {
|
|---|
| 256 | hr_volume_t *vol = bd->srvs->sarg;
|
|---|
| 257 | errno_t rc;
|
|---|
| 258 | uint64_t phys_block, len;
|
|---|
| 259 | size_t left;
|
|---|
| 260 | const uint8_t *data_write = src;
|
|---|
| 261 | uint8_t *data_read = dst;
|
|---|
| 262 |
|
|---|
| 263 | fibril_rwlock_read_lock(&vol->states_lock);
|
|---|
| 264 | if (vol->status != HR_VOL_ONLINE) {
|
|---|
| 265 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 266 | return EIO;
|
|---|
| 267 | }
|
|---|
| 268 | fibril_rwlock_read_unlock(&vol->states_lock);
|
|---|
| 269 |
|
|---|
| 270 | /* propagate sync */
|
|---|
| 271 | if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
|
|---|
| 272 | hr_fgroup_t *group = hr_fgroup_create(vol->fge,
|
|---|
| 273 | vol->extent_no);
|
|---|
| 274 | if (group == NULL)
|
|---|
| 275 | return ENOMEM;
|
|---|
| 276 |
|
|---|
| 277 | for (size_t i = 0; i < vol->extent_no; i++) {
|
|---|
| 278 | hr_io_t *io = hr_fgroup_alloc(group);
|
|---|
| 279 | io->extent = i;
|
|---|
| 280 | io->ba = ba;
|
|---|
| 281 | io->cnt = cnt;
|
|---|
| 282 | io->type = type;
|
|---|
| 283 | io->vol = vol;
|
|---|
| 284 |
|
|---|
| 285 | hr_fgroup_submit(group, hr_io_worker, io);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | size_t bad;
|
|---|
| 289 | rc = hr_fgroup_wait(group, NULL, &bad);
|
|---|
| 290 | if (rc == ENOMEM)
|
|---|
| 291 | return ENOMEM;
|
|---|
| 292 |
|
|---|
| 293 | if (bad > 0)
|
|---|
| 294 | return EIO;
|
|---|
| 295 |
|
|---|
| 296 | return EOK;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
|---|
| 300 | if (size < cnt * vol->bsize)
|
|---|
| 301 | return EINVAL;
|
|---|
| 302 |
|
|---|
| 303 | rc = hr_check_ba_range(vol, cnt, ba);
|
|---|
| 304 | if (rc != EOK)
|
|---|
| 305 | return rc;
|
|---|
| 306 |
|
|---|
| 307 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
|---|
| 308 | uint64_t strip_no = ba / strip_size;
|
|---|
| 309 | uint64_t extent = strip_no % vol->extent_no;
|
|---|
| 310 | uint64_t stripe = strip_no / vol->extent_no;
|
|---|
| 311 | uint64_t strip_off = ba % strip_size;
|
|---|
| 312 |
|
|---|
| 313 | left = cnt;
|
|---|
| 314 |
|
|---|
| 315 | /* calculate how many strips does the IO span */
|
|---|
| 316 | size_t end_strip_no = (ba + cnt - 1) / strip_size;
|
|---|
| 317 | size_t span = end_strip_no - strip_no + 1;
|
|---|
| 318 |
|
|---|
| 319 | hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
|
|---|
| 320 | if (group == NULL)
|
|---|
| 321 | return ENOMEM;
|
|---|
| 322 |
|
|---|
| 323 | while (left != 0) {
|
|---|
| 324 | phys_block = stripe * strip_size + strip_off;
|
|---|
| 325 | cnt = min(left, strip_size - strip_off);
|
|---|
| 326 | len = vol->bsize * cnt;
|
|---|
| 327 | hr_add_ba_offset(vol, &phys_block);
|
|---|
| 328 |
|
|---|
| 329 | hr_io_t *io = hr_fgroup_alloc(group);
|
|---|
| 330 | io->extent = extent;
|
|---|
| 331 | io->data_write = data_write;
|
|---|
| 332 | io->data_read = data_read;
|
|---|
| 333 | io->ba = phys_block;
|
|---|
| 334 | io->cnt = cnt;
|
|---|
| 335 | io->type = type;
|
|---|
| 336 | io->vol = vol;
|
|---|
| 337 |
|
|---|
| 338 | hr_fgroup_submit(group, hr_io_worker, io);
|
|---|
| 339 |
|
|---|
| 340 | left -= cnt;
|
|---|
| 341 | if (left == 0)
|
|---|
| 342 | break;
|
|---|
| 343 |
|
|---|
| 344 | if (type == HR_BD_READ)
|
|---|
| 345 | data_read += len;
|
|---|
| 346 | else if (type == HR_BD_WRITE)
|
|---|
| 347 | data_write += len;
|
|---|
| 348 |
|
|---|
| 349 | strip_off = 0;
|
|---|
| 350 | extent++;
|
|---|
| 351 | if (extent >= vol->extent_no) {
|
|---|
| 352 | stripe++;
|
|---|
| 353 | extent = 0;
|
|---|
| 354 | }
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | size_t bad;
|
|---|
| 358 | rc = hr_fgroup_wait(group, NULL, &bad);
|
|---|
| 359 | if (rc == ENOMEM && type == HR_BD_READ)
|
|---|
| 360 | return ENOMEM;
|
|---|
| 361 |
|
|---|
| 362 | if (bad > 0)
|
|---|
| 363 | return EIO;
|
|---|
| 364 |
|
|---|
| 365 | return EOK;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | /** @}
|
|---|
| 369 | */
|
|---|