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

Last change on this file since 7fba146 was 83ff12f, checked in by Miroslav Cimerman <mc@…>, 3 months ago

hr: raid0.c: no need to increment md_counter

  • Property mode set to 100644
File size: 8.7 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
55static errno_t hr_raid0_bd_op(hr_bd_op_type_t, bd_srv_t *, aoff64_t, size_t,
56 void *, const void *, size_t);
57
58/* bdops */
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 *,
62 size_t);
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,
65 const void *, size_t);
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 *);
68
69static bd_ops_t hr_raid0_bd_ops = {
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
77};
78
79extern loc_srv_t *hr_srv;
80
81errno_t hr_raid0_create(hr_volume_t *new_volume)
82{
83 HR_DEBUG("%s()", __func__);
84
85 if (new_volume->level != HR_LVL_0)
86 return EINVAL;
87
88 if (new_volume->extent_no < 2) {
89 HR_ERROR("RAID 0 volume needs at least 2 devices\n");
90 return EINVAL;
91 }
92
93 hr_raid0_vol_state_eval(new_volume);
94 if (new_volume->state != HR_VOL_ONLINE) {
95 HR_NOTE("\"%s\": unusable state, not creating\n",
96 new_volume->devname);
97 return EINVAL;
98 }
99
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
104 return EOK;
105}
106
107/*
108 * Called only once in volume's lifetime.
109 */
110errno_t hr_raid0_init(hr_volume_t *vol)
111{
112 HR_DEBUG("%s()", __func__);
113
114 if (vol->level != HR_LVL_0)
115 return EINVAL;
116
117 uint64_t total_blkno = vol->truncated_blkno * vol->extent_no;
118
119 vol->data_offset = vol->meta_ops->get_data_offset();
120
121 vol->data_blkno = total_blkno;
122 /* count md blocks */
123 vol->data_blkno -= vol->meta_ops->get_size() * vol->extent_no;
124
125 vol->strip_size = HR_STRIP_SIZE;
126
127 return EOK;
128}
129
130void hr_raid0_vol_state_eval(hr_volume_t *vol)
131{
132 HR_DEBUG("%s()", __func__);
133
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 }
154
155 fibril_rwlock_read_unlock(&vol->states_lock);
156
157 if (old_state != HR_VOL_ONLINE) {
158 fibril_rwlock_write_lock(&vol->states_lock);
159 hr_update_vol_state(vol, HR_VOL_ONLINE);
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);
184}
185
186static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
187{
188 HR_DEBUG("%s()", __func__);
189
190 hr_volume_t *vol = bd->srvs->sarg;
191
192 atomic_fetch_add_explicit(&vol->open_cnt, 1, memory_order_relaxed);
193
194 return EOK;
195}
196
197static errno_t hr_raid0_bd_close(bd_srv_t *bd)
198{
199 HR_DEBUG("%s()", __func__);
200
201 hr_volume_t *vol = bd->srvs->sarg;
202
203 atomic_fetch_sub_explicit(&vol->open_cnt, 1, memory_order_relaxed);
204
205 return EOK;
206}
207
208static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
209{
210 return hr_raid0_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
211}
212
213static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
214 void *buf, size_t size)
215{
216 return hr_raid0_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
217}
218
219static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
220 const void *data, size_t size)
221{
222 return hr_raid0_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
223}
224
225static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
226{
227 hr_volume_t *vol = bd->srvs->sarg;
228
229 *rsize = vol->bsize;
230 return EOK;
231}
232
233static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
234{
235 hr_volume_t *vol = bd->srvs->sarg;
236
237 *rnb = vol->data_blkno;
238 return EOK;
239}
240
241static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
242 size_t cnt, void *dst, const void *src, size_t size)
243{
244 HR_DEBUG("%s()", __func__);
245
246 hr_volume_t *vol = bd->srvs->sarg;
247 errno_t rc;
248 uint64_t phys_block, len;
249 size_t left;
250 const uint8_t *data_write = src;
251 uint8_t *data_read = dst;
252
253 fibril_rwlock_read_lock(&vol->states_lock);
254 if (vol->state != HR_VOL_ONLINE) {
255 fibril_rwlock_read_unlock(&vol->states_lock);
256 return EIO;
257 }
258 fibril_rwlock_read_unlock(&vol->states_lock);
259
260 /* propagate sync */
261 if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
262 hr_fgroup_t *group = hr_fgroup_create(vol->fge,
263 vol->extent_no);
264 if (group == NULL)
265 return ENOMEM;
266
267 for (size_t i = 0; i < vol->extent_no; i++) {
268 hr_io_t *io = hr_fgroup_alloc(group);
269 io->extent = i;
270 io->ba = ba;
271 io->cnt = cnt;
272 io->type = type;
273 io->vol = vol;
274
275 hr_fgroup_submit(group, hr_io_worker, io);
276 }
277
278 size_t bad;
279 rc = hr_fgroup_wait(group, NULL, &bad);
280 if (rc == ENOMEM)
281 return ENOMEM;
282
283 if (bad > 0)
284 return EIO;
285
286 return EOK;
287 }
288
289 if (type == HR_BD_READ || type == HR_BD_WRITE)
290 if (size < cnt * vol->bsize)
291 return EINVAL;
292
293 rc = hr_check_ba_range(vol, cnt, ba);
294 if (rc != EOK)
295 return rc;
296
297 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
298 uint64_t strip_no = ba / strip_size;
299 uint64_t extent = strip_no % vol->extent_no;
300 uint64_t stripe = strip_no / vol->extent_no;
301 uint64_t strip_off = ba % strip_size;
302
303 left = cnt;
304
305 /* calculate how many strips does the IO span */
306 size_t end_strip_no = (ba + cnt - 1) / strip_size;
307 size_t span = end_strip_no - strip_no + 1;
308
309 hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
310 if (group == NULL)
311 return ENOMEM;
312
313 while (left != 0) {
314 phys_block = stripe * strip_size + strip_off;
315 cnt = min(left, strip_size - strip_off);
316 len = vol->bsize * cnt;
317 hr_add_ba_offset(vol, &phys_block);
318
319 hr_io_t *io = hr_fgroup_alloc(group);
320 io->extent = extent;
321 io->data_write = data_write;
322 io->data_read = data_read;
323 io->ba = phys_block;
324 io->cnt = cnt;
325 io->type = type;
326 io->vol = vol;
327
328 hr_fgroup_submit(group, hr_io_worker, io);
329
330 left -= cnt;
331 if (left == 0)
332 break;
333
334 if (type == HR_BD_READ)
335 data_read += len;
336 else if (type == HR_BD_WRITE)
337 data_write += len;
338
339 strip_off = 0;
340 extent++;
341 if (extent >= vol->extent_no) {
342 stripe++;
343 extent = 0;
344 }
345 }
346
347 size_t bad;
348 rc = hr_fgroup_wait(group, NULL, &bad);
349 if (rc == ENOMEM && type == HR_BD_READ)
350 return ENOMEM;
351
352 if (bad > 0)
353 return EIO;
354
355 return EOK;
356}
357
358/** @}
359 */
Note: See TracBrowser for help on using the repository browser.