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

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

srv/bd/hr: remove unused nblocks variable

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