[4a2a6b8b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2024 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>
|
---|
[978130a] | 45 | #include <mem.h>
|
---|
[4a2a6b8b] | 46 | #include <task.h>
|
---|
| 47 | #include <stdio.h>
|
---|
| 48 | #include <stdlib.h>
|
---|
| 49 | #include <str_error.h>
|
---|
| 50 |
|
---|
[6b8e89b0] | 51 | #include "superblock.h"
|
---|
[4a2a6b8b] | 52 | #include "util.h"
|
---|
| 53 | #include "var.h"
|
---|
| 54 |
|
---|
| 55 | extern loc_srv_t *hr_srv;
|
---|
| 56 |
|
---|
| 57 | static errno_t hr_raid4_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
| 58 | static errno_t hr_raid4_bd_close(bd_srv_t *);
|
---|
| 59 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
| 60 | size_t);
|
---|
| 61 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
| 62 | static errno_t hr_raid4_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
| 63 | const void *, size_t);
|
---|
| 64 | static errno_t hr_raid4_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 65 | static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
| 66 |
|
---|
[11111e4] | 67 | static errno_t hr_raid4_write_parity(hr_volume_t *, uint64_t, uint64_t,
|
---|
| 68 | const void *, size_t);
|
---|
| 69 |
|
---|
[4a2a6b8b] | 70 | static bd_ops_t hr_raid4_bd_ops = {
|
---|
| 71 | .open = hr_raid4_bd_open,
|
---|
| 72 | .close = hr_raid4_bd_close,
|
---|
| 73 | .sync_cache = hr_raid4_bd_sync_cache,
|
---|
| 74 | .read_blocks = hr_raid4_bd_read_blocks,
|
---|
| 75 | .write_blocks = hr_raid4_bd_write_blocks,
|
---|
| 76 | .get_block_size = hr_raid4_bd_get_block_size,
|
---|
| 77 | .get_num_blocks = hr_raid4_bd_get_num_blocks
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[11111e4] | 80 | static errno_t hr_raid4_vol_usable(hr_volume_t *vol)
|
---|
| 81 | {
|
---|
| 82 | if (vol->status == HR_VOL_ONLINE ||
|
---|
| 83 | vol->status == HR_VOL_DEGRADED)
|
---|
| 84 | return EOK;
|
---|
| 85 | return EINVAL;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /*
|
---|
[90eec9c0] | 89 | * Returns (-1) if all extents are online,
|
---|
| 90 | * else returns index of first bad one.
|
---|
[11111e4] | 91 | */
|
---|
| 92 | static ssize_t hr_raid4_get_bad_ext(hr_volume_t *vol)
|
---|
| 93 | {
|
---|
| 94 | for (size_t i = 0; i < vol->dev_no; i++)
|
---|
| 95 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
| 96 | return i;
|
---|
| 97 | return -1;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | static errno_t hr_raid4_update_vol_status(hr_volume_t *vol)
|
---|
| 101 | {
|
---|
| 102 | hr_vol_status_t old_state = vol->status;
|
---|
| 103 | size_t bad = 0;
|
---|
| 104 | for (size_t i = 0; i < vol->dev_no; i++)
|
---|
| 105 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
| 106 | bad++;
|
---|
| 107 |
|
---|
| 108 | switch (bad) {
|
---|
| 109 | case 0:
|
---|
| 110 | if (old_state != HR_VOL_ONLINE) {
|
---|
| 111 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 112 | "RAID 4 has all extents online, "
|
---|
| 113 | "marking \"%s\" (%lu) as ONLINE",
|
---|
| 114 | vol->devname, vol->svc_id);
|
---|
| 115 | vol->status = HR_VOL_ONLINE;
|
---|
| 116 | }
|
---|
| 117 | return EOK;
|
---|
| 118 | case 1:
|
---|
| 119 | if (old_state != HR_VOL_DEGRADED) {
|
---|
| 120 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 121 | "RAID 4 array \"%s\" (%lu) has 1 extent inactive, "
|
---|
| 122 | "marking as DEGRADED",
|
---|
| 123 | vol->devname, vol->svc_id);
|
---|
| 124 | vol->status = HR_VOL_DEGRADED;
|
---|
| 125 | }
|
---|
| 126 | return EOK;
|
---|
| 127 | default:
|
---|
| 128 | if (old_state != HR_VOL_FAULTY) {
|
---|
| 129 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 130 | "RAID 4 array \"%s\" (%lu) has more than one 1 "
|
---|
| 131 | "extent inactive, marking as FAULTY",
|
---|
| 132 | vol->devname, vol->svc_id);
|
---|
| 133 | vol->status = HR_VOL_FAULTY;
|
---|
| 134 | }
|
---|
| 135 | return EINVAL;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[4a2a6b8b] | 139 | static void xor(void *dst, const void *src, size_t size)
|
---|
| 140 | {
|
---|
| 141 | size_t i;
|
---|
| 142 | uint64_t *d = dst;
|
---|
| 143 | const uint64_t *s = src;
|
---|
| 144 |
|
---|
| 145 | for (i = 0; i < size / sizeof(uint64_t); ++i)
|
---|
| 146 | *d++ ^= *s++;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[11111e4] | 149 | static errno_t hr_raid4_read_degraded(hr_volume_t *vol, uint64_t bad,
|
---|
| 150 | uint64_t block, void *data, size_t cnt)
|
---|
| 151 | {
|
---|
| 152 | errno_t rc;
|
---|
[90eec9c0] | 153 | size_t i;
|
---|
[11111e4] | 154 | void *xorbuf;
|
---|
| 155 | void *buf;
|
---|
[90eec9c0] | 156 | uint64_t len = vol->bsize * cnt;
|
---|
[11111e4] | 157 |
|
---|
[90eec9c0] | 158 | xorbuf = malloc(len);
|
---|
[11111e4] | 159 | if (xorbuf == NULL)
|
---|
| 160 | return ENOMEM;
|
---|
| 161 |
|
---|
[90eec9c0] | 162 | buf = malloc(len);
|
---|
[11111e4] | 163 | if (buf == NULL) {
|
---|
| 164 | free(xorbuf);
|
---|
| 165 | return ENOMEM;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[90eec9c0] | 168 | /* read all other extents in the stripe */
|
---|
| 169 | memset(xorbuf, 0, len);
|
---|
| 170 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 171 | if (i == bad) {
|
---|
| 172 | continue;
|
---|
| 173 | } else {
|
---|
| 174 | rc = block_read_direct(vol->extents[i].svc_id, block,
|
---|
| 175 | cnt, buf);
|
---|
| 176 | if (rc != EOK)
|
---|
| 177 | goto end;
|
---|
| 178 | xor(xorbuf, buf, len);
|
---|
[11111e4] | 179 | }
|
---|
| 180 | }
|
---|
[90eec9c0] | 181 |
|
---|
| 182 | memcpy(data, xorbuf, len);
|
---|
[11111e4] | 183 | end:
|
---|
| 184 | free(xorbuf);
|
---|
| 185 | free(buf);
|
---|
| 186 | return rc;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | static errno_t hr_raid4_write(hr_volume_t *vol, uint64_t extent, aoff64_t ba,
|
---|
[978130a] | 190 | const void *data, size_t cnt)
|
---|
[4a2a6b8b] | 191 | {
|
---|
| 192 | errno_t rc;
|
---|
[90eec9c0] | 193 | size_t i;
|
---|
[4a2a6b8b] | 194 | void *xorbuf;
|
---|
| 195 | void *buf;
|
---|
[90eec9c0] | 196 | uint64_t len = vol->bsize * cnt;
|
---|
[4a2a6b8b] | 197 |
|
---|
[11111e4] | 198 | ssize_t bad = hr_raid4_get_bad_ext(vol);
|
---|
| 199 | if (bad < 1) {
|
---|
| 200 | rc = block_write_direct(vol->extents[extent].svc_id, ba, cnt,
|
---|
| 201 | data);
|
---|
| 202 | if (rc != EOK)
|
---|
| 203 | return rc;
|
---|
| 204 | /*
|
---|
| 205 | * DEGRADED parity - skip parity write
|
---|
| 206 | */
|
---|
| 207 | if (bad == 0)
|
---|
| 208 | return EOK;
|
---|
| 209 |
|
---|
| 210 | rc = hr_raid4_write_parity(vol, extent, ba, data, cnt);
|
---|
| 211 | return rc;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[90eec9c0] | 214 | xorbuf = malloc(len);
|
---|
[11111e4] | 215 | if (xorbuf == NULL)
|
---|
| 216 | return ENOMEM;
|
---|
| 217 |
|
---|
[90eec9c0] | 218 | buf = malloc(len);
|
---|
[11111e4] | 219 | if (buf == NULL) {
|
---|
| 220 | free(xorbuf);
|
---|
| 221 | return ENOMEM;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | if (extent == (size_t) bad) {
|
---|
| 225 | /*
|
---|
| 226 | * new parity = read other and xor in new data
|
---|
| 227 | *
|
---|
| 228 | * write new parity
|
---|
| 229 | */
|
---|
[90eec9c0] | 230 | memset(xorbuf, 0, len);
|
---|
| 231 | for (i = 1; i < vol->dev_no; i++) {
|
---|
| 232 | if (i == (size_t) bad) {
|
---|
| 233 | continue;
|
---|
| 234 | } else {
|
---|
| 235 | rc = block_read_direct(vol->extents[i].svc_id,
|
---|
| 236 | ba, cnt, buf);
|
---|
| 237 | if (rc != EOK)
|
---|
| 238 | goto end;
|
---|
| 239 | xor(xorbuf, buf, len);
|
---|
[11111e4] | 240 | }
|
---|
| 241 | }
|
---|
[90eec9c0] | 242 | xor(xorbuf, data, len);
|
---|
| 243 | rc = block_write_direct(vol->extents[0].svc_id, ba, cnt,
|
---|
| 244 | xorbuf);
|
---|
| 245 | if (rc != EOK)
|
---|
| 246 | goto end;
|
---|
[11111e4] | 247 | } else {
|
---|
| 248 | /*
|
---|
| 249 | * new parity = xor original data and old parity and new data
|
---|
| 250 | *
|
---|
| 251 | * write parity, new data
|
---|
| 252 | */
|
---|
[90eec9c0] | 253 | rc = block_read_direct(vol->extents[extent].svc_id, ba, cnt,
|
---|
| 254 | xorbuf);
|
---|
| 255 | if (rc != EOK)
|
---|
| 256 | goto end;
|
---|
| 257 | rc = block_read_direct(vol->extents[0].svc_id, ba, cnt, buf);
|
---|
| 258 | if (rc != EOK)
|
---|
| 259 | goto end;
|
---|
[11111e4] | 260 |
|
---|
[90eec9c0] | 261 | xor(xorbuf, buf, len);
|
---|
[11111e4] | 262 |
|
---|
[90eec9c0] | 263 | xor(xorbuf, data, len);
|
---|
| 264 |
|
---|
| 265 | rc = block_write_direct(vol->extents[0].svc_id, ba, cnt,
|
---|
| 266 | xorbuf);
|
---|
| 267 | if (rc != EOK)
|
---|
| 268 | goto end;
|
---|
| 269 | rc = block_write_direct(vol->extents[extent].svc_id, ba, cnt,
|
---|
| 270 | data);
|
---|
| 271 | if (rc != EOK)
|
---|
| 272 | goto end;
|
---|
[11111e4] | 273 | }
|
---|
| 274 | end:
|
---|
| 275 | free(xorbuf);
|
---|
| 276 | free(buf);
|
---|
| 277 | return rc;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | static errno_t hr_raid4_write_parity(hr_volume_t *vol, uint64_t extent,
|
---|
| 281 | uint64_t block, const void *data, size_t cnt)
|
---|
| 282 | {
|
---|
| 283 | errno_t rc;
|
---|
[90eec9c0] | 284 | size_t i;
|
---|
[11111e4] | 285 | void *xorbuf;
|
---|
| 286 | void *buf;
|
---|
[90eec9c0] | 287 | uint64_t len = vol->bsize * cnt;
|
---|
[11111e4] | 288 |
|
---|
[90eec9c0] | 289 | xorbuf = malloc(len);
|
---|
[4a2a6b8b] | 290 | if (xorbuf == NULL)
|
---|
| 291 | return ENOMEM;
|
---|
| 292 |
|
---|
[90eec9c0] | 293 | buf = malloc(len);
|
---|
[c7b4452] | 294 | if (buf == NULL) {
|
---|
| 295 | free(xorbuf);
|
---|
[4a2a6b8b] | 296 | return ENOMEM;
|
---|
[c7b4452] | 297 | }
|
---|
[4a2a6b8b] | 298 |
|
---|
[90eec9c0] | 299 | /*
|
---|
| 300 | * parity = read and xor all other data extents, xor in new data
|
---|
| 301 | *
|
---|
| 302 | * XXX: subtract method
|
---|
| 303 | */
|
---|
| 304 | memset(xorbuf, 0, len);
|
---|
| 305 | for (i = 1; i < vol->dev_no; i++) {
|
---|
| 306 | if (i == extent) {
|
---|
| 307 | xor(xorbuf, data, vol->bsize);
|
---|
| 308 | } else {
|
---|
| 309 | rc = block_read_direct(vol->extents[i].svc_id, block,
|
---|
| 310 | cnt, buf);
|
---|
| 311 | if (rc != EOK)
|
---|
| 312 | goto end;
|
---|
| 313 | xor(xorbuf, buf, len);
|
---|
[4a2a6b8b] | 314 | }
|
---|
| 315 | }
|
---|
[90eec9c0] | 316 |
|
---|
| 317 | rc = block_write_direct(vol->extents[0].svc_id, block, cnt, xorbuf);
|
---|
[4a2a6b8b] | 318 | end:
|
---|
| 319 | free(xorbuf);
|
---|
| 320 | free(buf);
|
---|
[12321f8] | 321 | return rc;
|
---|
[4a2a6b8b] | 322 | }
|
---|
| 323 |
|
---|
| 324 | static errno_t hr_raid4_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
| 325 | {
|
---|
| 326 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
| 327 | return EOK;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | static errno_t hr_raid4_bd_close(bd_srv_t *bd)
|
---|
| 331 | {
|
---|
| 332 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
| 333 | return EOK;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[fad91b9] | 336 | static errno_t hr_raid4_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
[d092d2c] | 337 | size_t cnt, void *dst, const void *src, size_t size)
|
---|
[4a2a6b8b] | 338 | {
|
---|
| 339 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 340 | errno_t rc;
|
---|
[d092d2c] | 341 | uint64_t phys_block, len;
|
---|
[978130a] | 342 | size_t left;
|
---|
[d092d2c] | 343 | const uint8_t *data_write = src;
|
---|
| 344 | uint8_t *data_read = dst;
|
---|
[fad91b9] | 345 |
|
---|
[11111e4] | 346 | /* propagate sync */
|
---|
| 347 | if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
|
---|
| 348 | hr_sync_all_extents(vol);
|
---|
| 349 | rc = hr_raid4_update_vol_status(vol);
|
---|
| 350 | return rc;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[fad91b9] | 353 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
---|
| 354 | if (size < cnt * vol->bsize)
|
---|
| 355 | return EINVAL;
|
---|
[4a2a6b8b] | 356 |
|
---|
| 357 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
| 358 | if (rc != EOK)
|
---|
| 359 | return rc;
|
---|
| 360 |
|
---|
[978130a] | 361 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
---|
| 362 | uint64_t stripe = (ba / strip_size); /* stripe number */
|
---|
| 363 | uint64_t extent = (stripe % (vol->dev_no - 1)) + 1;
|
---|
| 364 | uint64_t ext_stripe = stripe / (vol->dev_no - 1); /* stripe level */
|
---|
| 365 | uint64_t strip_off = ba % strip_size; /* strip offset */
|
---|
| 366 |
|
---|
[abc2c4b] | 367 | fibril_mutex_lock(&vol->lock);
|
---|
[4a2a6b8b] | 368 |
|
---|
[11111e4] | 369 | rc = hr_raid4_vol_usable(vol);
|
---|
| 370 | if (rc != EOK) {
|
---|
| 371 | fibril_mutex_unlock(&vol->lock);
|
---|
| 372 | return EIO;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[fad91b9] | 375 | left = cnt;
|
---|
[4a2a6b8b] | 376 | while (left != 0) {
|
---|
[978130a] | 377 | phys_block = ext_stripe * strip_size + strip_off;
|
---|
| 378 | cnt = min(left, strip_size - strip_off);
|
---|
[d092d2c] | 379 | len = vol->bsize * cnt;
|
---|
[4a2a6b8b] | 380 | hr_add_ba_offset(vol, &phys_block);
|
---|
[fad91b9] | 381 | switch (type) {
|
---|
| 382 | case HR_BD_SYNC:
|
---|
[11111e4] | 383 | if (vol->extents[extent].status != HR_EXT_ONLINE)
|
---|
| 384 | break;
|
---|
[fad91b9] | 385 | rc = block_sync_cache(vol->extents[extent].svc_id,
|
---|
[978130a] | 386 | phys_block, cnt);
|
---|
[11111e4] | 387 | /* allow unsupported sync */
|
---|
| 388 | if (rc == ENOTSUP)
|
---|
| 389 | rc = EOK;
|
---|
[fad91b9] | 390 | break;
|
---|
| 391 | case HR_BD_READ:
|
---|
[11111e4] | 392 | retry_read:
|
---|
| 393 | ssize_t bad = hr_raid4_get_bad_ext(vol);
|
---|
| 394 | if (bad > 0 && extent == (size_t) bad) {
|
---|
| 395 | rc = hr_raid4_read_degraded(vol, bad,
|
---|
| 396 | phys_block, data_read, cnt);
|
---|
| 397 | } else {
|
---|
| 398 | rc = block_read_direct(vol->extents[extent].svc_id,
|
---|
| 399 | phys_block, cnt, data_read);
|
---|
| 400 | }
|
---|
[d092d2c] | 401 | data_read += len;
|
---|
[4a2a6b8b] | 402 | break;
|
---|
[fad91b9] | 403 | case HR_BD_WRITE:
|
---|
[11111e4] | 404 | retry_write:
|
---|
| 405 | rc = hr_raid4_write(vol, extent, phys_block,
|
---|
| 406 | data_write, cnt);
|
---|
[d092d2c] | 407 | data_write += len;
|
---|
[fad91b9] | 408 | break;
|
---|
| 409 | default:
|
---|
| 410 | rc = EINVAL;
|
---|
[11111e4] | 411 | goto error;
|
---|
[fad91b9] | 412 | }
|
---|
| 413 |
|
---|
[1a60e645] | 414 | if (rc == ENOMEM)
|
---|
| 415 | goto error;
|
---|
| 416 |
|
---|
[11111e4] | 417 | if (rc == ENOENT)
|
---|
| 418 | hr_update_ext_status(vol, extent, HR_EXT_MISSING);
|
---|
| 419 | else if (rc != EOK)
|
---|
| 420 | hr_update_ext_status(vol, extent, HR_EXT_FAILED);
|
---|
| 421 |
|
---|
| 422 | if (rc != EOK) {
|
---|
| 423 | rc = hr_raid4_update_vol_status(vol);
|
---|
| 424 | if (rc == EOK) {
|
---|
| 425 | /*
|
---|
| 426 | * State changed from ONLINE -> DEGRADED,
|
---|
| 427 | * rewind and retry
|
---|
| 428 | */
|
---|
| 429 | if (type == HR_BD_WRITE) {
|
---|
[d092d2c] | 430 | data_write -= len;
|
---|
[11111e4] | 431 | goto retry_write;
|
---|
| 432 | } else if (type == HR_BD_WRITE) {
|
---|
[d092d2c] | 433 | data_read -= len;
|
---|
[11111e4] | 434 | goto retry_read;
|
---|
| 435 | }
|
---|
| 436 | } else {
|
---|
| 437 | rc = EIO;
|
---|
| 438 | goto error;
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
[fad91b9] | 441 |
|
---|
[978130a] | 442 | left -= cnt;
|
---|
| 443 | strip_off = 0;
|
---|
| 444 | extent++;
|
---|
| 445 | if (extent >= vol->dev_no) {
|
---|
| 446 | ext_stripe++;
|
---|
| 447 | extent = 1;
|
---|
| 448 | }
|
---|
[4a2a6b8b] | 449 | }
|
---|
| 450 |
|
---|
[fad91b9] | 451 | error:
|
---|
[11111e4] | 452 | (void) hr_raid4_update_vol_status(vol);
|
---|
[abc2c4b] | 453 | fibril_mutex_unlock(&vol->lock);
|
---|
[4a2a6b8b] | 454 | return rc;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[fad91b9] | 457 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
| 458 | {
|
---|
| 459 | return hr_raid4_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[4a2a6b8b] | 462 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 463 | void *buf, size_t size)
|
---|
| 464 | {
|
---|
[fad91b9] | 465 | return hr_raid4_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
---|
[4a2a6b8b] | 466 | }
|
---|
| 467 |
|
---|
| 468 | static errno_t hr_raid4_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 469 | const void *data, size_t size)
|
---|
| 470 | {
|
---|
[fad91b9] | 471 | return hr_raid4_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
---|
[4a2a6b8b] | 472 | }
|
---|
| 473 |
|
---|
| 474 | static errno_t hr_raid4_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
| 475 | {
|
---|
| 476 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 477 |
|
---|
| 478 | *rsize = vol->bsize;
|
---|
| 479 | return EOK;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
| 483 | {
|
---|
| 484 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 485 |
|
---|
| 486 | *rnb = vol->data_blkno;
|
---|
| 487 | return EOK;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | errno_t hr_raid4_create(hr_volume_t *new_volume)
|
---|
| 491 | {
|
---|
| 492 | errno_t rc;
|
---|
| 493 |
|
---|
[50bed55d] | 494 | assert(new_volume->level == HR_LVL_4);
|
---|
[4a2a6b8b] | 495 |
|
---|
| 496 | if (new_volume->dev_no < 3) {
|
---|
| 497 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 498 | "RAID 4 array needs at least 3 devices");
|
---|
| 499 | return EINVAL;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[11111e4] | 502 | rc = hr_raid4_update_vol_status(new_volume);
|
---|
| 503 | if (rc != EOK)
|
---|
| 504 | return rc;
|
---|
| 505 |
|
---|
[4a2a6b8b] | 506 | bd_srvs_init(&new_volume->hr_bds);
|
---|
| 507 | new_volume->hr_bds.ops = &hr_raid4_bd_ops;
|
---|
| 508 | new_volume->hr_bds.sarg = new_volume;
|
---|
| 509 |
|
---|
| 510 | rc = hr_register_volume(new_volume);
|
---|
| 511 |
|
---|
[08fa9e8] | 512 | return rc;
|
---|
[4a2a6b8b] | 513 | }
|
---|
| 514 |
|
---|
[6b8e89b0] | 515 | errno_t hr_raid4_init(hr_volume_t *vol)
|
---|
| 516 | {
|
---|
| 517 | errno_t rc;
|
---|
| 518 | size_t bsize;
|
---|
| 519 | uint64_t total_blkno;
|
---|
| 520 |
|
---|
[50bed55d] | 521 | assert(vol->level == HR_LVL_4);
|
---|
[6b8e89b0] | 522 |
|
---|
| 523 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
---|
| 524 | if (rc != EOK)
|
---|
| 525 | return rc;
|
---|
| 526 |
|
---|
| 527 | vol->nblocks = total_blkno;
|
---|
| 528 | vol->bsize = bsize;
|
---|
| 529 | vol->data_offset = HR_DATA_OFF;
|
---|
| 530 | vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no) -
|
---|
| 531 | (vol->nblocks / vol->dev_no);
|
---|
| 532 | vol->strip_size = HR_STRIP_SIZE;
|
---|
| 533 |
|
---|
| 534 | return EOK;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[4a2a6b8b] | 537 | /** @}
|
---|
| 538 | */
|
---|