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

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

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

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 * Copyright (c) 2025 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
50#include "io.h"
51#include "superblock.h"
52#include "util.h"
53#include "var.h"
54
55extern loc_srv_t *hr_srv;
56
57static void hr_raid0_update_vol_status(hr_volume_t *);
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 */
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
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
88 if (new_volume->extent_no < 2) {
89 HR_ERROR("RAID 0 array needs at least 2 devices\n");
90 return EINVAL;
91 }
92
93 hr_raid0_update_vol_status(new_volume);
94 if (new_volume->status != HR_VOL_ONLINE)
95 return EINVAL;
96
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;
121 vol->data_blkno = vol->nblocks - (vol->data_offset * vol->extent_no);
122 vol->strip_size = HR_STRIP_SIZE;
123
124 return EOK;
125}
126
127void hr_raid0_status_event(hr_volume_t *vol)
128{
129 hr_raid0_update_vol_status(vol);
130}
131
132static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
133{
134 HR_DEBUG("hr_bd_open()\n");
135 return EOK;
136}
137
138static errno_t hr_raid0_bd_close(bd_srv_t *bd)
139{
140 HR_DEBUG("hr_bd_close()\n");
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
177static void hr_raid0_update_vol_status(hr_volume_t *vol)
178{
179 fibril_rwlock_read_lock(&vol->states_lock);
180
181 hr_vol_status_t old_state = vol->status;
182
183 for (size_t i = 0; i < vol->extent_no; i++) {
184 if (vol->extents[i].status != HR_EXT_ONLINE) {
185 fibril_rwlock_read_unlock(&vol->states_lock);
186
187 if (old_state != HR_VOL_FAULTY) {
188 fibril_rwlock_write_lock(&vol->states_lock);
189 hr_update_vol_status(vol, HR_VOL_FAULTY);
190 fibril_rwlock_write_unlock(&vol->states_lock);
191 }
192 return;
193 }
194 }
195 fibril_rwlock_read_unlock(&vol->states_lock);
196
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 }
202}
203
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
224static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
225 size_t cnt, void *dst, const void *src, size_t size)
226{
227 hr_volume_t *vol = bd->srvs->sarg;
228 errno_t rc;
229 uint64_t phys_block, len;
230 size_t left;
231 const uint8_t *data_write = src;
232 uint8_t *data_read = dst;
233
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
241 /* propagate sync */
242 if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
243 hr_fgroup_t *group = hr_fgroup_create(vol->fge,
244 vol->extent_no);
245 if (group == NULL)
246 return ENOMEM;
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;
261 rc = hr_fgroup_wait(group, NULL, &bad);
262 if (rc == ENOMEM)
263 return ENOMEM;
264
265 if (bad > 0)
266 return EIO;
267
268 return EOK;
269 }
270
271 if (type == HR_BD_READ || type == HR_BD_WRITE)
272 if (size < cnt * vol->bsize)
273 return EINVAL;
274
275 rc = hr_check_ba_range(vol, cnt, ba);
276 if (rc != EOK)
277 return rc;
278
279 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
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;
284
285 left = cnt;
286
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;
290
291 hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
292 if (group == NULL)
293 return ENOMEM;
294
295 while (left != 0) {
296 phys_block = stripe * strip_size + strip_off;
297 cnt = min(left, strip_size - strip_off);
298 len = vol->bsize * cnt;
299 hr_add_ba_offset(vol, &phys_block);
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;
305 io->ba = phys_block;
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
313 left -= cnt;
314 if (left == 0)
315 break;
316
317 if (type == HR_BD_READ)
318 data_read += len;
319 else if (type == HR_BD_WRITE)
320 data_write += len;
321
322 strip_off = 0;
323 extent++;
324 if (extent >= vol->extent_no) {
325 stripe++;
326 extent = 0;
327 }
328 }
329
330 size_t bad;
331 rc = hr_fgroup_wait(group, NULL, &bad);
332 if (rc == ENOMEM && type == HR_BD_READ)
333 return ENOMEM;
334
335 if (bad > 0)
336 return EIO;
337
338 return EOK;
339}
340
341/** @}
342 */
Note: See TracBrowser for help on using the repository browser.