source: mainline/uspace/srv/bd/hr/raid0.c@ 10005fd

Last change on this file since 10005fd was 0fcb011, checked in by Miroslav Cimerman <mc@…>, 7 months ago

hr: raid0.c: early break if (left == 0)

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