source: mainline/uspace/srv/bd/hr/raid0.c

Last change on this file was ca212a51, checked in by Miroslav Cimerman <mc@…>, 11 days ago

hr: RAID 0, 5: init strip size to closest (down) pow of 2

  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[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
[6f13257]55static errno_t hr_raid0_bd_op(hr_bd_op_type_t, bd_srv_t *, aoff64_t, size_t,
[733564a]56 void *, const void *, size_t);
57
58/* bdops */
[6f13257]59static errno_t hr_raid0_bd_open(bd_srvs_t *, bd_srv_t *);
60static errno_t hr_raid0_bd_close(bd_srv_t *);
61static errno_t hr_raid0_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
[a8b2d9e7]62 size_t);
[6f13257]63static errno_t hr_raid0_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
64static errno_t hr_raid0_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
[a8b2d9e7]65 const void *, size_t);
[6f13257]66static errno_t hr_raid0_bd_get_block_size(bd_srv_t *, size_t *);
67static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
[a8b2d9e7]68
69static bd_ops_t hr_raid0_bd_ops = {
[6f13257]70 .open = hr_raid0_bd_open,
71 .close = hr_raid0_bd_close,
72 .sync_cache = hr_raid0_bd_sync_cache,
73 .read_blocks = hr_raid0_bd_read_blocks,
74 .write_blocks = hr_raid0_bd_write_blocks,
75 .get_block_size = hr_raid0_bd_get_block_size,
76 .get_num_blocks = hr_raid0_bd_get_num_blocks
[a8b2d9e7]77};
[6d0fc11]78
79extern loc_srv_t *hr_srv;
[a8b2d9e7]80
[733564a]81errno_t hr_raid0_create(hr_volume_t *new_volume)
82{
[baa4929]83 HR_DEBUG("%s()", __func__);
84
[b5c95da5]85 if (new_volume->level != HR_LVL_0)
86 return EINVAL;
[733564a]87
[65706f1]88 if (new_volume->extent_no < 2) {
[af73327a]89 HR_ERROR("RAID 0 volume needs at least 2 devices\n");
[733564a]90 return EINVAL;
91 }
92
[da80de9]93 hr_raid0_vol_state_eval(new_volume);
[263a2389]94 if (new_volume->state != HR_VOL_OPTIMAL) {
[18c3658]95 HR_NOTE("\"%s\": unusable state, not creating\n",
96 new_volume->devname);
[ee47537]97 return EINVAL;
[18c3658]98 }
[bc3d695]99
[733564a]100 bd_srvs_init(&new_volume->hr_bds);
101 new_volume->hr_bds.ops = &hr_raid0_bd_ops;
102 new_volume->hr_bds.sarg = new_volume;
103
[8a65373]104 return EOK;
[733564a]105}
106
[746e636]107/*
108 * Called only once in volume's lifetime.
109 */
[733564a]110errno_t hr_raid0_init(hr_volume_t *vol)
111{
[baa4929]112 HR_DEBUG("%s()", __func__);
[733564a]113
[b5c95da5]114 if (vol->level != HR_LVL_0)
115 return EINVAL;
[733564a]116
[80c760e]117 uint64_t total_blkno = vol->truncated_blkno * vol->extent_no;
[733564a]118
[50603405]119 vol->data_offset = vol->meta_ops->get_data_offset();
[baa4929]120
121 vol->data_blkno = total_blkno;
[50603405]122 /* count md blocks */
123 vol->data_blkno -= vol->meta_ops->get_size() * vol->extent_no;
[baa4929]124
[ca212a51]125 vol->strip_size = hr_closest_pow2(HR_STRIP_SIZE / vol->extent_no);
[733564a]126
127 return EOK;
128}
129
[da80de9]130void hr_raid0_vol_state_eval(hr_volume_t *vol)
[7b359f5]131{
[baa4929]132 HR_DEBUG("%s()", __func__);
133
[da80de9]134 fibril_mutex_lock(&vol->md_lock);
135
136 fibril_mutex_unlock(&vol->md_lock);
137
138 fibril_rwlock_read_lock(&vol->states_lock);
139
140 hr_vol_state_t old_state = vol->state;
141
142 for (size_t i = 0; i < vol->extent_no; i++) {
143 if (vol->extents[i].state != HR_EXT_ONLINE) {
144 fibril_rwlock_read_unlock(&vol->states_lock);
145
146 if (old_state != HR_VOL_FAULTY) {
147 fibril_rwlock_write_lock(&vol->states_lock);
148 hr_update_vol_state(vol, HR_VOL_FAULTY);
149 fibril_rwlock_write_unlock(&vol->states_lock);
150 }
151 return;
152 }
153 }
[83ff12f]154
[da80de9]155 fibril_rwlock_read_unlock(&vol->states_lock);
156
[263a2389]157 if (old_state != HR_VOL_OPTIMAL) {
[da80de9]158 fibril_rwlock_write_lock(&vol->states_lock);
[263a2389]159 hr_update_vol_state(vol, HR_VOL_OPTIMAL);
[da80de9]160 fibril_rwlock_write_unlock(&vol->states_lock);
161 }
162}
163
164void hr_raid0_ext_state_cb(hr_volume_t *vol, size_t extent, errno_t rc)
165{
166 HR_DEBUG("%s()", __func__);
167
168 if (rc == EOK)
169 return;
170
171 fibril_rwlock_write_lock(&vol->states_lock);
172
173 switch (rc) {
174 case ENOENT:
175 hr_update_ext_state(vol, extent, HR_EXT_MISSING);
176 break;
177 default:
178 hr_update_ext_state(vol, extent, HR_EXT_FAILED);
179 }
180
181 hr_update_vol_state(vol, HR_VOL_FAULTY);
182
183 fibril_rwlock_write_unlock(&vol->states_lock);
[7b359f5]184}
185
[733564a]186static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
187{
[a57dde4]188 HR_DEBUG("%s()", __func__);
[7a80c63]189
190 hr_volume_t *vol = bd->srvs->sarg;
191
192 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
193
[733564a]194 return EOK;
195}
196
197static errno_t hr_raid0_bd_close(bd_srv_t *bd)
198{
[a57dde4]199 HR_DEBUG("%s()", __func__);
[7a80c63]200
201 hr_volume_t *vol = bd->srvs->sarg;
202
203 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
204
[733564a]205 return EOK;
206}
207
208static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
209{
[137f7cf5]210 hr_volume_t *vol = bd->srvs->sarg;
211
212 return hr_sync_extents(vol);
[733564a]213}
214
215static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
216 void *buf, size_t size)
217{
218 return hr_raid0_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
219}
220
221static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
222 const void *data, size_t size)
223{
224 return hr_raid0_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
225}
226
227static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
228{
229 hr_volume_t *vol = bd->srvs->sarg;
230
231 *rsize = vol->bsize;
232 return EOK;
233}
234
235static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
236{
237 hr_volume_t *vol = bd->srvs->sarg;
238
239 *rnb = vol->data_blkno;
240 return EOK;
241}
242
[fad91b9]243static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
[d092d2c]244 size_t cnt, void *dst, const void *src, size_t size)
[a8b2d9e7]245{
[da80de9]246 HR_DEBUG("%s()", __func__);
247
[a8b2d9e7]248 hr_volume_t *vol = bd->srvs->sarg;
249 errno_t rc;
[d092d2c]250 uint64_t phys_block, len;
[978130a]251 size_t left;
[d092d2c]252 const uint8_t *data_write = src;
253 uint8_t *data_read = dst;
[4a2a6b8b]254
[3c518fc]255 if (size < cnt * vol->bsize)
256 return EINVAL;
257
[95ca19d]258 if (vol->vflags & HR_VOL_FLAG_READ_ONLY && type == HR_BD_WRITE)
259 return ENOTSUP;
260
[bc3d695]261 fibril_rwlock_read_lock(&vol->states_lock);
[263a2389]262 if (vol->state != HR_VOL_OPTIMAL) {
[bc3d695]263 fibril_rwlock_read_unlock(&vol->states_lock);
264 return EIO;
265 }
266 fibril_rwlock_read_unlock(&vol->states_lock);
267
[4a2a6b8b]268 rc = hr_check_ba_range(vol, cnt, ba);
269 if (rc != EOK)
270 return rc;
[a8b2d9e7]271
[978130a]272 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
[bc3d695]273 uint64_t strip_no = ba / strip_size;
274 uint64_t extent = strip_no % vol->extent_no;
275 uint64_t stripe = strip_no / vol->extent_no;
276 uint64_t strip_off = ba % strip_size;
[978130a]277
[bc3d695]278 left = cnt;
[a8b2d9e7]279
[bc3d695]280 /* calculate how many strips does the IO span */
281 size_t end_strip_no = (ba + cnt - 1) / strip_size;
282 size_t span = end_strip_no - strip_no + 1;
[9fc1d36d]283
[bc3d695]284 hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
[a0c3080]285
[a8b2d9e7]286 while (left != 0) {
[bc3d695]287 phys_block = stripe * strip_size + strip_off;
[978130a]288 cnt = min(left, strip_size - strip_off);
[d092d2c]289 len = vol->bsize * cnt;
[73425d4]290 hr_add_data_offset(vol, &phys_block);
[bc3d695]291
292 hr_io_t *io = hr_fgroup_alloc(group);
293 io->extent = extent;
294 io->data_write = data_write;
295 io->data_read = data_read;
[a5c2960e]296 io->ba = phys_block;
[bc3d695]297 io->cnt = cnt;
298 io->type = type;
299 io->vol = vol;
300
301 hr_fgroup_submit(group, hr_io_worker, io);
302
[0fcb011]303 left -= cnt;
304 if (left == 0)
305 break;
306
[3c518fc]307 data_read += len;
308 data_write += len;
[fad91b9]309
[978130a]310 strip_off = 0;
311 extent++;
[65706f1]312 if (extent >= vol->extent_no) {
[bc3d695]313 stripe++;
[978130a]314 extent = 0;
315 }
[a8b2d9e7]316 }
317
[bc3d695]318 size_t bad;
[f0360ec]319 (void)hr_fgroup_wait(group, NULL, &bad);
[4660649]320
[bc3d695]321 if (bad > 0)
322 return EIO;
323
324 return EOK;
[a8b2d9e7]325}
326
327/** @}
328 */
Note: See TracBrowser for help on using the repository browser.