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