[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 |
|
---|
| 67 | static bd_ops_t hr_raid4_bd_ops = {
|
---|
| 68 | .open = hr_raid4_bd_open,
|
---|
| 69 | .close = hr_raid4_bd_close,
|
---|
| 70 | .sync_cache = hr_raid4_bd_sync_cache,
|
---|
| 71 | .read_blocks = hr_raid4_bd_read_blocks,
|
---|
| 72 | .write_blocks = hr_raid4_bd_write_blocks,
|
---|
| 73 | .get_block_size = hr_raid4_bd_get_block_size,
|
---|
| 74 | .get_num_blocks = hr_raid4_bd_get_num_blocks
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | static void xor(void *dst, const void *src, size_t size)
|
---|
| 78 | {
|
---|
| 79 | size_t i;
|
---|
| 80 | uint64_t *d = dst;
|
---|
| 81 | const uint64_t *s = src;
|
---|
| 82 |
|
---|
| 83 | for (i = 0; i < size / sizeof(uint64_t); ++i)
|
---|
| 84 | *d++ ^= *s++;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | static errno_t write_parity(hr_volume_t *vol, uint64_t extent, uint64_t block,
|
---|
[978130a] | 88 | const void *data, size_t cnt)
|
---|
[4a2a6b8b] | 89 | {
|
---|
| 90 | errno_t rc;
|
---|
[978130a] | 91 | size_t i, j;
|
---|
[4a2a6b8b] | 92 | void *xorbuf;
|
---|
| 93 | void *buf;
|
---|
| 94 |
|
---|
[978130a] | 95 | xorbuf = malloc(vol->bsize);
|
---|
[4a2a6b8b] | 96 | if (xorbuf == NULL)
|
---|
| 97 | return ENOMEM;
|
---|
| 98 |
|
---|
| 99 | buf = malloc(vol->bsize);
|
---|
[c7b4452] | 100 | if (buf == NULL) {
|
---|
| 101 | free(xorbuf);
|
---|
[4a2a6b8b] | 102 | return ENOMEM;
|
---|
[c7b4452] | 103 | }
|
---|
[4a2a6b8b] | 104 |
|
---|
[978130a] | 105 | for (j = 0; j < cnt; j++) {
|
---|
| 106 | memset(xorbuf, 0, vol->bsize);
|
---|
| 107 | for (i = 1; i < vol->dev_no; i++) {
|
---|
| 108 | if (i == extent) {
|
---|
| 109 | xor(xorbuf, data, vol->bsize);
|
---|
| 110 | } else {
|
---|
| 111 | rc = block_read_direct(vol->extents[i].svc_id,
|
---|
| 112 | block, 1, buf);
|
---|
| 113 | if (rc != EOK)
|
---|
| 114 | goto end;
|
---|
| 115 | xor(xorbuf, buf, vol->bsize);
|
---|
| 116 | }
|
---|
[4a2a6b8b] | 117 | }
|
---|
[978130a] | 118 | rc = block_write_direct(vol->extents[0].svc_id, block, 1,
|
---|
| 119 | xorbuf);
|
---|
| 120 | if (rc != EOK)
|
---|
| 121 | goto end;
|
---|
| 122 | data = (void *) ((uintptr_t) data + vol->bsize);
|
---|
| 123 | block++;
|
---|
[4a2a6b8b] | 124 | }
|
---|
| 125 | end:
|
---|
| 126 | free(xorbuf);
|
---|
| 127 | free(buf);
|
---|
[12321f8] | 128 | return rc;
|
---|
[4a2a6b8b] | 129 | }
|
---|
| 130 |
|
---|
| 131 | static errno_t hr_raid4_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
| 132 | {
|
---|
| 133 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
| 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | static errno_t hr_raid4_bd_close(bd_srv_t *bd)
|
---|
| 138 | {
|
---|
| 139 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
| 140 | return EOK;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[fad91b9] | 143 | static errno_t hr_raid4_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
| 144 | size_t cnt, void *data_read, const void *data_write, size_t size)
|
---|
[4a2a6b8b] | 145 | {
|
---|
| 146 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 147 | errno_t rc;
|
---|
| 148 | uint64_t phys_block;
|
---|
[978130a] | 149 | size_t left;
|
---|
[fad91b9] | 150 |
|
---|
| 151 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
---|
| 152 | if (size < cnt * vol->bsize)
|
---|
| 153 | return EINVAL;
|
---|
[4a2a6b8b] | 154 |
|
---|
| 155 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
| 156 | if (rc != EOK)
|
---|
| 157 | return rc;
|
---|
| 158 |
|
---|
[978130a] | 159 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
---|
| 160 | uint64_t stripe = (ba / strip_size); /* stripe number */
|
---|
| 161 | uint64_t extent = (stripe % (vol->dev_no - 1)) + 1;
|
---|
| 162 | uint64_t ext_stripe = stripe / (vol->dev_no - 1); /* stripe level */
|
---|
| 163 | uint64_t strip_off = ba % strip_size; /* strip offset */
|
---|
| 164 |
|
---|
[abc2c4b] | 165 | fibril_mutex_lock(&vol->lock);
|
---|
[4a2a6b8b] | 166 |
|
---|
[fad91b9] | 167 | left = cnt;
|
---|
[4a2a6b8b] | 168 | while (left != 0) {
|
---|
[978130a] | 169 | phys_block = ext_stripe * strip_size + strip_off;
|
---|
| 170 | cnt = min(left, strip_size - strip_off);
|
---|
[4a2a6b8b] | 171 | hr_add_ba_offset(vol, &phys_block);
|
---|
[fad91b9] | 172 | switch (type) {
|
---|
| 173 | case HR_BD_SYNC:
|
---|
| 174 | rc = block_sync_cache(vol->extents[extent].svc_id,
|
---|
[978130a] | 175 | phys_block, cnt);
|
---|
[fad91b9] | 176 | break;
|
---|
| 177 | case HR_BD_READ:
|
---|
| 178 | rc = block_read_direct(vol->extents[extent].svc_id,
|
---|
[978130a] | 179 | phys_block, cnt, data_read);
|
---|
| 180 | data_read = (void *) ((uintptr_t) data_read +
|
---|
| 181 | (vol->bsize * cnt));
|
---|
[4a2a6b8b] | 182 | break;
|
---|
[fad91b9] | 183 | case HR_BD_WRITE:
|
---|
| 184 | rc = block_write_direct(vol->extents[extent].svc_id,
|
---|
[978130a] | 185 | phys_block, cnt, data_write);
|
---|
[fad91b9] | 186 | if (rc != EOK)
|
---|
| 187 | goto error;
|
---|
[978130a] | 188 | rc = write_parity(vol, extent, phys_block, data_write,
|
---|
| 189 | cnt);
|
---|
[fad91b9] | 190 | if (rc != EOK)
|
---|
| 191 | goto error;
|
---|
[978130a] | 192 | data_write = (void *) ((uintptr_t) data_write +
|
---|
| 193 | (vol->bsize * cnt));
|
---|
[fad91b9] | 194 | break;
|
---|
| 195 | default:
|
---|
| 196 | rc = EINVAL;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | if (rc != EOK)
|
---|
| 200 | goto error;
|
---|
| 201 |
|
---|
[978130a] | 202 | left -= cnt;
|
---|
| 203 | strip_off = 0;
|
---|
| 204 | extent++;
|
---|
| 205 | if (extent >= vol->dev_no) {
|
---|
| 206 | ext_stripe++;
|
---|
| 207 | extent = 1;
|
---|
| 208 | }
|
---|
[4a2a6b8b] | 209 | }
|
---|
| 210 |
|
---|
[fad91b9] | 211 | error:
|
---|
[abc2c4b] | 212 | fibril_mutex_unlock(&vol->lock);
|
---|
[4a2a6b8b] | 213 | return rc;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[fad91b9] | 216 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
| 217 | {
|
---|
| 218 | return hr_raid4_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[4a2a6b8b] | 221 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 222 | void *buf, size_t size)
|
---|
| 223 | {
|
---|
[fad91b9] | 224 | return hr_raid4_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
---|
[4a2a6b8b] | 225 | }
|
---|
| 226 |
|
---|
| 227 | static errno_t hr_raid4_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 228 | const void *data, size_t size)
|
---|
| 229 | {
|
---|
[fad91b9] | 230 | return hr_raid4_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
---|
[4a2a6b8b] | 231 | }
|
---|
| 232 |
|
---|
| 233 | static errno_t hr_raid4_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
| 234 | {
|
---|
| 235 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 236 |
|
---|
| 237 | *rsize = vol->bsize;
|
---|
| 238 | return EOK;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
| 242 | {
|
---|
| 243 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 244 |
|
---|
| 245 | *rnb = vol->data_blkno;
|
---|
| 246 | return EOK;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | errno_t hr_raid4_create(hr_volume_t *new_volume)
|
---|
| 250 | {
|
---|
| 251 | errno_t rc;
|
---|
| 252 |
|
---|
[50bed55d] | 253 | assert(new_volume->level == HR_LVL_4);
|
---|
[4a2a6b8b] | 254 |
|
---|
| 255 | if (new_volume->dev_no < 3) {
|
---|
| 256 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 257 | "RAID 4 array needs at least 3 devices");
|
---|
| 258 | return EINVAL;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | bd_srvs_init(&new_volume->hr_bds);
|
---|
| 262 | new_volume->hr_bds.ops = &hr_raid4_bd_ops;
|
---|
| 263 | new_volume->hr_bds.sarg = new_volume;
|
---|
| 264 |
|
---|
| 265 | rc = hr_register_volume(new_volume);
|
---|
| 266 |
|
---|
[08fa9e8] | 267 | return rc;
|
---|
[4a2a6b8b] | 268 | }
|
---|
| 269 |
|
---|
[6b8e89b0] | 270 | errno_t hr_raid4_init(hr_volume_t *vol)
|
---|
| 271 | {
|
---|
| 272 | errno_t rc;
|
---|
| 273 | size_t bsize;
|
---|
| 274 | uint64_t total_blkno;
|
---|
| 275 |
|
---|
[50bed55d] | 276 | assert(vol->level == HR_LVL_4);
|
---|
[6b8e89b0] | 277 |
|
---|
| 278 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
---|
| 279 | if (rc != EOK)
|
---|
| 280 | return rc;
|
---|
| 281 |
|
---|
| 282 | vol->nblocks = total_blkno;
|
---|
| 283 | vol->bsize = bsize;
|
---|
| 284 | vol->data_offset = HR_DATA_OFF;
|
---|
| 285 | vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no) -
|
---|
| 286 | (vol->nblocks / vol->dev_no);
|
---|
| 287 | vol->strip_size = HR_STRIP_SIZE;
|
---|
| 288 |
|
---|
| 289 | return EOK;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[4a2a6b8b] | 292 | /** @}
|
---|
| 293 | */
|
---|