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 |
|
---|
55 | static 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 */
|
---|
59 | static errno_t hr_raid0_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
60 | static errno_t hr_raid0_bd_close(bd_srv_t *);
|
---|
61 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
62 | size_t);
|
---|
63 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
64 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
65 | const void *, size_t);
|
---|
66 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
67 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
68 |
|
---|
69 | static 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 |
|
---|
79 | extern loc_srv_t *hr_srv;
|
---|
80 |
|
---|
81 | errno_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_OPTIMAL) {
|
---|
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 | */
|
---|
110 | errno_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_closest_pow2(HR_STRIP_SIZE / vol->extent_no);
|
---|
126 |
|
---|
127 | return EOK;
|
---|
128 | }
|
---|
129 |
|
---|
130 | void 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_OPTIMAL) {
|
---|
158 | fibril_rwlock_write_lock(&vol->states_lock);
|
---|
159 | hr_update_vol_state(vol, HR_VOL_OPTIMAL);
|
---|
160 | fibril_rwlock_write_unlock(&vol->states_lock);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void 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 |
|
---|
186 | static 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 |
|
---|
197 | static 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 |
|
---|
208 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
209 | {
|
---|
210 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
211 |
|
---|
212 | return hr_sync_extents(vol);
|
---|
213 | }
|
---|
214 |
|
---|
215 | static 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 |
|
---|
221 | static 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 |
|
---|
227 | static 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 |
|
---|
235 | static 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 |
|
---|
243 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
244 | size_t cnt, void *dst, const void *src, size_t size)
|
---|
245 | {
|
---|
246 | HR_DEBUG("%s()", __func__);
|
---|
247 |
|
---|
248 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
249 | errno_t rc;
|
---|
250 | uint64_t phys_block, len;
|
---|
251 | size_t left;
|
---|
252 | const uint8_t *data_write = src;
|
---|
253 | uint8_t *data_read = dst;
|
---|
254 |
|
---|
255 | if (size < cnt * vol->bsize)
|
---|
256 | return EINVAL;
|
---|
257 |
|
---|
258 | if (vol->vflags & HR_VOL_FLAG_READ_ONLY && type == HR_BD_WRITE)
|
---|
259 | return ENOTSUP;
|
---|
260 |
|
---|
261 | fibril_rwlock_read_lock(&vol->states_lock);
|
---|
262 | if (vol->state != HR_VOL_OPTIMAL) {
|
---|
263 | fibril_rwlock_read_unlock(&vol->states_lock);
|
---|
264 | return EIO;
|
---|
265 | }
|
---|
266 | fibril_rwlock_read_unlock(&vol->states_lock);
|
---|
267 |
|
---|
268 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
269 | if (rc != EOK)
|
---|
270 | return rc;
|
---|
271 |
|
---|
272 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
---|
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;
|
---|
277 |
|
---|
278 | left = cnt;
|
---|
279 |
|
---|
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;
|
---|
283 |
|
---|
284 | hr_fgroup_t *group = hr_fgroup_create(vol->fge, span);
|
---|
285 |
|
---|
286 | while (left != 0) {
|
---|
287 | phys_block = stripe * strip_size + strip_off;
|
---|
288 | cnt = min(left, strip_size - strip_off);
|
---|
289 | len = vol->bsize * cnt;
|
---|
290 | hr_add_data_offset(vol, &phys_block);
|
---|
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;
|
---|
296 | io->ba = phys_block;
|
---|
297 | io->cnt = cnt;
|
---|
298 | io->type = type;
|
---|
299 | io->vol = vol;
|
---|
300 |
|
---|
301 | hr_fgroup_submit(group, hr_io_worker, io);
|
---|
302 |
|
---|
303 | left -= cnt;
|
---|
304 | if (left == 0)
|
---|
305 | break;
|
---|
306 |
|
---|
307 | data_read += len;
|
---|
308 | data_write += len;
|
---|
309 |
|
---|
310 | strip_off = 0;
|
---|
311 | extent++;
|
---|
312 | if (extent >= vol->extent_no) {
|
---|
313 | stripe++;
|
---|
314 | extent = 0;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | size_t bad;
|
---|
319 | (void)hr_fgroup_wait(group, NULL, &bad);
|
---|
320 |
|
---|
321 | if (bad > 0)
|
---|
322 | return EIO;
|
---|
323 |
|
---|
324 | return EOK;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /** @}
|
---|
328 | */
|
---|