[a8b2d9e7] | 1 | /*
|
---|
[bc3d695] | 2 | * Copyright (c) 2025 Miroslav Cimerman
|
---|
[a8b2d9e7] | 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 |
|
---|
[bc3d695] | 50 | #include "io.h"
|
---|
[6b8e89b0] | 51 | #include "superblock.h"
|
---|
[a8b2d9e7] | 52 | #include "util.h"
|
---|
[b0f1366] | 53 | #include "var.h"
|
---|
[a8b2d9e7] | 54 |
|
---|
[6d0fc11] | 55 | static void hr_raid0_update_vol_status(hr_volume_t *);
|
---|
[800d188] | 56 | static void hr_raid0_state_callback(hr_volume_t *, size_t, errno_t);
|
---|
[6d0fc11] | 57 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t, bd_srv_t *, aoff64_t, size_t,
|
---|
[733564a] | 58 | void *, const void *, size_t);
|
---|
| 59 |
|
---|
| 60 | /* bdops */
|
---|
[6d0fc11] | 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 *,
|
---|
[a8b2d9e7] | 64 | size_t);
|
---|
[6d0fc11] | 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,
|
---|
[a8b2d9e7] | 67 | const void *, size_t);
|
---|
[6d0fc11] | 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 *);
|
---|
[a8b2d9e7] | 70 |
|
---|
| 71 | static bd_ops_t hr_raid0_bd_ops = {
|
---|
[6d0fc11] | 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
|
---|
[a8b2d9e7] | 79 | };
|
---|
[6d0fc11] | 80 |
|
---|
| 81 | extern loc_srv_t *hr_srv;
|
---|
[a8b2d9e7] | 82 |
|
---|
[733564a] | 83 | errno_t hr_raid0_create(hr_volume_t *new_volume)
|
---|
| 84 | {
|
---|
[baa4929] | 85 | HR_DEBUG("%s()", __func__);
|
---|
| 86 |
|
---|
[733564a] | 87 | assert(new_volume->level == HR_LVL_0);
|
---|
| 88 |
|
---|
[65706f1] | 89 | if (new_volume->extent_no < 2) {
|
---|
[d199a6f] | 90 | HR_ERROR("RAID 0 array needs at least 2 devices\n");
|
---|
[733564a] | 91 | return EINVAL;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[ee47537] | 94 | hr_raid0_update_vol_status(new_volume);
|
---|
[18c3658] | 95 | if (new_volume->status != HR_VOL_ONLINE) {
|
---|
| 96 | HR_NOTE("\"%s\": unusable state, not creating\n",
|
---|
| 97 | new_volume->devname);
|
---|
[ee47537] | 98 | return EINVAL;
|
---|
[18c3658] | 99 | }
|
---|
[bc3d695] | 100 |
|
---|
[733564a] | 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 |
|
---|
[800d188] | 105 | new_volume->state_callback = hr_raid0_state_callback;
|
---|
| 106 |
|
---|
[8a65373] | 107 | return EOK;
|
---|
[733564a] | 108 | }
|
---|
| 109 |
|
---|
[746e636] | 110 | /*
|
---|
| 111 | * Called only once in volume's lifetime.
|
---|
| 112 | */
|
---|
[733564a] | 113 | errno_t hr_raid0_init(hr_volume_t *vol)
|
---|
| 114 | {
|
---|
[baa4929] | 115 | HR_DEBUG("%s()", __func__);
|
---|
[733564a] | 116 |
|
---|
| 117 | assert(vol->level == HR_LVL_0);
|
---|
| 118 |
|
---|
[80c760e] | 119 | uint64_t total_blkno = vol->truncated_blkno * vol->extent_no;
|
---|
[733564a] | 120 |
|
---|
[50603405] | 121 | vol->data_offset = vol->meta_ops->get_data_offset();
|
---|
[baa4929] | 122 |
|
---|
| 123 | vol->data_blkno = total_blkno;
|
---|
[50603405] | 124 | /* count md blocks */
|
---|
| 125 | vol->data_blkno -= vol->meta_ops->get_size() * vol->extent_no;
|
---|
[baa4929] | 126 |
|
---|
[733564a] | 127 | vol->strip_size = HR_STRIP_SIZE;
|
---|
| 128 |
|
---|
| 129 | return EOK;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[7b359f5] | 132 | void hr_raid0_status_event(hr_volume_t *vol)
|
---|
| 133 | {
|
---|
[baa4929] | 134 | HR_DEBUG("%s()", __func__);
|
---|
| 135 |
|
---|
[ee47537] | 136 | hr_raid0_update_vol_status(vol);
|
---|
[7b359f5] | 137 | }
|
---|
| 138 |
|
---|
[733564a] | 139 | static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
| 140 | {
|
---|
[a57dde4] | 141 | HR_DEBUG("%s()", __func__);
|
---|
[7a80c63] | 142 |
|
---|
| 143 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 144 |
|
---|
| 145 | atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
|
---|
| 146 |
|
---|
[733564a] | 147 | return EOK;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static errno_t hr_raid0_bd_close(bd_srv_t *bd)
|
---|
| 151 | {
|
---|
[a57dde4] | 152 | HR_DEBUG("%s()", __func__);
|
---|
[7a80c63] | 153 |
|
---|
| 154 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 155 |
|
---|
| 156 | atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
|
---|
| 157 |
|
---|
[733564a] | 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 |
|
---|
[ee47537] | 194 | static void hr_raid0_update_vol_status(hr_volume_t *vol)
|
---|
[9fc1d36d] | 195 | {
|
---|
[800d188] | 196 | fibril_mutex_lock(&vol->md_lock);
|
---|
| 197 |
|
---|
[50603405] | 198 | vol->meta_ops->inc_counter(vol->in_mem_md);
|
---|
| 199 | /* TODO: save right away */
|
---|
[800d188] | 200 |
|
---|
| 201 | fibril_mutex_unlock(&vol->md_lock);
|
---|
| 202 |
|
---|
[bc3d695] | 203 | fibril_rwlock_read_lock(&vol->states_lock);
|
---|
[ee47537] | 204 |
|
---|
[a0c3080] | 205 | hr_vol_status_t old_state = vol->status;
|
---|
| 206 |
|
---|
[65706f1] | 207 | for (size_t i = 0; i < vol->extent_no; i++) {
|
---|
[9fc1d36d] | 208 | if (vol->extents[i].status != HR_EXT_ONLINE) {
|
---|
[bc3d695] | 209 | fibril_rwlock_read_unlock(&vol->states_lock);
|
---|
[ee47537] | 210 |
|
---|
| 211 | if (old_state != HR_VOL_FAULTY) {
|
---|
| 212 | fibril_rwlock_write_lock(&vol->states_lock);
|
---|
[a0c3080] | 213 | hr_update_vol_status(vol, HR_VOL_FAULTY);
|
---|
[ee47537] | 214 | fibril_rwlock_write_unlock(&vol->states_lock);
|
---|
| 215 | }
|
---|
| 216 | return;
|
---|
[9fc1d36d] | 217 | }
|
---|
| 218 | }
|
---|
[bc3d695] | 219 | fibril_rwlock_read_unlock(&vol->states_lock);
|
---|
[a0c3080] | 220 |
|
---|
[ee47537] | 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 | }
|
---|
[9fc1d36d] | 226 | }
|
---|
| 227 |
|
---|
[800d188] | 228 | static void hr_raid0_state_callback(hr_volume_t *vol, size_t extent, errno_t rc)
|
---|
[bc3d695] | 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 |
|
---|
[fad91b9] | 248 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
[d092d2c] | 249 | size_t cnt, void *dst, const void *src, size_t size)
|
---|
[a8b2d9e7] | 250 | {
|
---|
| 251 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 252 | errno_t rc;
|
---|
[d092d2c] | 253 | uint64_t phys_block, len;
|
---|
[978130a] | 254 | size_t left;
|
---|
[d092d2c] | 255 | const uint8_t *data_write = src;
|
---|
| 256 | uint8_t *data_read = dst;
|
---|
[4a2a6b8b] | 257 |
|
---|
[bc3d695] | 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 |
|
---|
[5f543e9] | 265 | /* propagate sync */
|
---|
| 266 | if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
|
---|
[4660649] | 267 | hr_fgroup_t *group = hr_fgroup_create(vol->fge,
|
---|
| 268 | vol->extent_no);
|
---|
[a6fb937] | 269 | if (group == NULL)
|
---|
| 270 | return ENOMEM;
|
---|
[bc3d695] | 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;
|
---|
[4660649] | 284 | rc = hr_fgroup_wait(group, NULL, &bad);
|
---|
| 285 | if (rc == ENOMEM)
|
---|
| 286 | return ENOMEM;
|
---|
| 287 |
|
---|
[bc3d695] | 288 | if (bad > 0)
|
---|
| 289 | return EIO;
|
---|
[4660649] | 290 |
|
---|
[bc3d695] | 291 | return EOK;
|
---|
[5f543e9] | 292 | }
|
---|
| 293 |
|
---|
[fad91b9] | 294 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
---|
| 295 | if (size < cnt * vol->bsize)
|
---|
| 296 | return EINVAL;
|
---|
| 297 |
|
---|
[4a2a6b8b] | 298 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
| 299 | if (rc != EOK)
|
---|
| 300 | return rc;
|
---|
[a8b2d9e7] | 301 |
|
---|
[978130a] | 302 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
---|
[bc3d695] | 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;
|
---|
[978130a] | 307 |
|
---|
[bc3d695] | 308 | left = cnt;
|
---|
[a8b2d9e7] | 309 |
|
---|
[bc3d695] | 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;
|
---|
[9fc1d36d] | 313 |
|
---|
[bc3d695] | 314 | hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
|
---|
[a6fb937] | 315 | if (group == NULL)
|
---|
| 316 | return ENOMEM;
|
---|
[a0c3080] | 317 |
|
---|
[a8b2d9e7] | 318 | while (left != 0) {
|
---|
[bc3d695] | 319 | phys_block = stripe * strip_size + strip_off;
|
---|
[978130a] | 320 | cnt = min(left, strip_size - strip_off);
|
---|
[d092d2c] | 321 | len = vol->bsize * cnt;
|
---|
[4a2a6b8b] | 322 | hr_add_ba_offset(vol, &phys_block);
|
---|
[bc3d695] | 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;
|
---|
[a5c2960e] | 328 | io->ba = phys_block;
|
---|
[bc3d695] | 329 | io->cnt = cnt;
|
---|
| 330 | io->type = type;
|
---|
| 331 | io->vol = vol;
|
---|
| 332 |
|
---|
| 333 | hr_fgroup_submit(group, hr_io_worker, io);
|
---|
| 334 |
|
---|
[0fcb011] | 335 | left -= cnt;
|
---|
| 336 | if (left == 0)
|
---|
| 337 | break;
|
---|
| 338 |
|
---|
[bc3d695] | 339 | if (type == HR_BD_READ)
|
---|
[d092d2c] | 340 | data_read += len;
|
---|
[bc3d695] | 341 | else if (type == HR_BD_WRITE)
|
---|
[d092d2c] | 342 | data_write += len;
|
---|
[fad91b9] | 343 |
|
---|
[978130a] | 344 | strip_off = 0;
|
---|
| 345 | extent++;
|
---|
[65706f1] | 346 | if (extent >= vol->extent_no) {
|
---|
[bc3d695] | 347 | stripe++;
|
---|
[978130a] | 348 | extent = 0;
|
---|
| 349 | }
|
---|
[a8b2d9e7] | 350 | }
|
---|
| 351 |
|
---|
[bc3d695] | 352 | size_t bad;
|
---|
[4660649] | 353 | rc = hr_fgroup_wait(group, NULL, &bad);
|
---|
| 354 | if (rc == ENOMEM && type == HR_BD_READ)
|
---|
| 355 | return ENOMEM;
|
---|
| 356 |
|
---|
[bc3d695] | 357 | if (bad > 0)
|
---|
| 358 | return EIO;
|
---|
| 359 |
|
---|
| 360 | return EOK;
|
---|
[a8b2d9e7] | 361 | }
|
---|
| 362 |
|
---|
| 363 | /** @}
|
---|
| 364 | */
|
---|