[94d84a0] | 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 <bd_srv.h>
|
---|
| 37 | #include <block.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <hr.h>
|
---|
| 40 | #include <io/log.h>
|
---|
| 41 | #include <ipc/hr.h>
|
---|
| 42 | #include <ipc/services.h>
|
---|
| 43 | #include <loc.h>
|
---|
| 44 | #include <task.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
| 47 | #include <str_error.h>
|
---|
| 48 |
|
---|
[6b8e89b0] | 49 | #include "superblock.h"
|
---|
[da5c257] | 50 | #include "util.h"
|
---|
[b0f1366] | 51 | #include "var.h"
|
---|
[94d84a0] | 52 |
|
---|
| 53 | extern loc_srv_t *hr_srv;
|
---|
| 54 |
|
---|
| 55 | static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
| 56 | static errno_t hr_raid1_bd_close(bd_srv_t *);
|
---|
| 57 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
| 58 | size_t);
|
---|
| 59 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
| 60 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
| 61 | const void *, size_t);
|
---|
| 62 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 63 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
| 64 |
|
---|
| 65 | static bd_ops_t hr_raid1_bd_ops = {
|
---|
| 66 | .open = hr_raid1_bd_open,
|
---|
| 67 | .close = hr_raid1_bd_close,
|
---|
| 68 | .sync_cache = hr_raid1_bd_sync_cache,
|
---|
| 69 | .read_blocks = hr_raid1_bd_read_blocks,
|
---|
| 70 | .write_blocks = hr_raid1_bd_write_blocks,
|
---|
| 71 | .get_block_size = hr_raid1_bd_get_block_size,
|
---|
| 72 | .get_num_blocks = hr_raid1_bd_get_num_blocks
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
| 76 | {
|
---|
| 77 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
| 78 | return EOK;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | static errno_t hr_raid1_bd_close(bd_srv_t *bd)
|
---|
| 82 | {
|
---|
| 83 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
| 84 | return EOK;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[fad91b9] | 87 | static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
| 88 | size_t cnt, void *data_read, const void *data_write, size_t size)
|
---|
[94d84a0] | 89 | {
|
---|
| 90 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 91 | errno_t rc;
|
---|
| 92 | size_t i;
|
---|
| 93 |
|
---|
[fad91b9] | 94 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
---|
| 95 | if (size < cnt * vol->bsize)
|
---|
| 96 | return EINVAL;
|
---|
| 97 |
|
---|
[4a2a6b8b] | 98 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
| 99 | if (rc != EOK)
|
---|
[b0f1366] | 100 | return rc;
|
---|
[4a2a6b8b] | 101 |
|
---|
| 102 | hr_add_ba_offset(vol, &ba);
|
---|
| 103 |
|
---|
[abc2c4b] | 104 | fibril_mutex_lock(&vol->lock);
|
---|
[b0f1366] | 105 |
|
---|
[fad91b9] | 106 | switch (type) {
|
---|
| 107 | case HR_BD_SYNC:
|
---|
| 108 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 109 | rc = block_sync_cache(vol->extents[i].svc_id, ba, cnt);
|
---|
| 110 | if (rc != EOK)
|
---|
| 111 | goto error;
|
---|
| 112 | }
|
---|
| 113 | break;
|
---|
| 114 | case HR_BD_READ:
|
---|
| 115 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 116 | rc = block_read_direct(vol->extents[i].svc_id, ba, cnt,
|
---|
| 117 | data_read);
|
---|
| 118 | if (rc != EOK)
|
---|
| 119 | goto error;
|
---|
| 120 | }
|
---|
| 121 | break;
|
---|
| 122 | case HR_BD_WRITE:
|
---|
| 123 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 124 | rc = block_write_direct(vol->extents[i].svc_id, ba, cnt,
|
---|
| 125 | data_write);
|
---|
| 126 | if (rc != EOK)
|
---|
| 127 | goto error;
|
---|
| 128 | }
|
---|
| 129 | break;
|
---|
| 130 | default:
|
---|
| 131 | rc = EINVAL;
|
---|
[94d84a0] | 132 | }
|
---|
| 133 |
|
---|
[fad91b9] | 134 | error:
|
---|
[abc2c4b] | 135 | fibril_mutex_unlock(&vol->lock);
|
---|
[94d84a0] | 136 | return rc;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[fad91b9] | 139 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
| 140 | {
|
---|
| 141 | return hr_raid1_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[94d84a0] | 144 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 145 | void *buf, size_t size)
|
---|
| 146 | {
|
---|
[fad91b9] | 147 | return hr_raid1_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
---|
[94d84a0] | 148 | }
|
---|
| 149 |
|
---|
| 150 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 151 | const void *data, size_t size)
|
---|
| 152 | {
|
---|
[fad91b9] | 153 | return hr_raid1_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
---|
[94d84a0] | 154 | }
|
---|
| 155 |
|
---|
| 156 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
| 157 | {
|
---|
| 158 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 159 |
|
---|
[12cbf25e] | 160 | *rsize = vol->bsize;
|
---|
| 161 | return EOK;
|
---|
[94d84a0] | 162 | }
|
---|
| 163 |
|
---|
| 164 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
| 165 | {
|
---|
| 166 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 167 |
|
---|
[b0f1366] | 168 | *rnb = vol->data_blkno;
|
---|
[12cbf25e] | 169 | return EOK;
|
---|
[94d84a0] | 170 | }
|
---|
| 171 |
|
---|
| 172 | errno_t hr_raid1_create(hr_volume_t *new_volume)
|
---|
| 173 | {
|
---|
[b0f1366] | 174 | errno_t rc;
|
---|
| 175 |
|
---|
[50bed55d] | 176 | assert(new_volume->level == HR_LVL_1);
|
---|
[94d84a0] | 177 |
|
---|
[12cbf25e] | 178 | if (new_volume->dev_no < 2) {
|
---|
| 179 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 180 | "RAID 1 array needs at least 2 devices");
|
---|
| 181 | return EINVAL;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[94d84a0] | 184 | bd_srvs_init(&new_volume->hr_bds);
|
---|
| 185 | new_volume->hr_bds.ops = &hr_raid1_bd_ops;
|
---|
| 186 | new_volume->hr_bds.sarg = new_volume;
|
---|
| 187 |
|
---|
[da5c257] | 188 | rc = hr_register_volume(new_volume);
|
---|
[94d84a0] | 189 |
|
---|
[08fa9e8] | 190 | return rc;
|
---|
[94d84a0] | 191 | }
|
---|
| 192 |
|
---|
[6b8e89b0] | 193 | errno_t hr_raid1_init(hr_volume_t *vol)
|
---|
| 194 | {
|
---|
| 195 | errno_t rc;
|
---|
| 196 | size_t bsize;
|
---|
| 197 | uint64_t total_blkno;
|
---|
| 198 |
|
---|
[50bed55d] | 199 | assert(vol->level == HR_LVL_1);
|
---|
[6b8e89b0] | 200 |
|
---|
| 201 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
---|
| 202 | if (rc != EOK)
|
---|
| 203 | return rc;
|
---|
| 204 |
|
---|
| 205 | vol->nblocks = total_blkno / vol->dev_no;
|
---|
| 206 | vol->bsize = bsize;
|
---|
| 207 | vol->data_offset = HR_DATA_OFF;
|
---|
| 208 | vol->data_blkno = vol->nblocks - vol->data_offset;
|
---|
| 209 | vol->strip_size = 0;
|
---|
| 210 |
|
---|
| 211 | return EOK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[94d84a0] | 214 | /** @}
|
---|
| 215 | */
|
---|