1 | /*
|
---|
2 | * Copyright (c) 2024 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 "superblock.h"
|
---|
51 | #include "util.h"
|
---|
52 | #include "var.h"
|
---|
53 |
|
---|
54 | extern loc_srv_t *hr_srv;
|
---|
55 |
|
---|
56 | static errno_t hr_raid0_check_vol_status(hr_volume_t *);
|
---|
57 | static errno_t hr_raid0_update_vol_status(hr_volume_t *);
|
---|
58 | static 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 */
|
---|
62 | static errno_t hr_raid0_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
63 | static errno_t hr_raid0_bd_close(bd_srv_t *);
|
---|
64 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
65 | size_t);
|
---|
66 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
67 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
68 | const void *, size_t);
|
---|
69 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
70 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
71 |
|
---|
72 | static 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 |
|
---|
82 | errno_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->dev_no < 2) {
|
---|
89 | HR_ERROR("RAID 0 array needs at least 2 devices\n");
|
---|
90 | return EINVAL;
|
---|
91 | }
|
---|
92 |
|
---|
93 | rc = hr_raid0_update_vol_status(new_volume);
|
---|
94 | if (rc != EOK)
|
---|
95 | return rc;
|
---|
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 |
|
---|
106 | errno_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->dev_no);
|
---|
122 | vol->strip_size = HR_STRIP_SIZE;
|
---|
123 |
|
---|
124 | return EOK;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void hr_raid0_status_event(hr_volume_t *vol)
|
---|
128 | {
|
---|
129 | fibril_mutex_lock(&vol->lock);
|
---|
130 | (void)hr_raid0_update_vol_status(vol);
|
---|
131 | fibril_mutex_unlock(&vol->lock);
|
---|
132 | }
|
---|
133 |
|
---|
134 | static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
135 | {
|
---|
136 | HR_DEBUG("hr_bd_open()\n");
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static errno_t hr_raid0_bd_close(bd_srv_t *bd)
|
---|
141 | {
|
---|
142 | HR_DEBUG("hr_bd_close()\n");
|
---|
143 | return EOK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
147 | {
|
---|
148 | return hr_raid0_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
---|
149 | }
|
---|
150 |
|
---|
151 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
152 | void *buf, size_t size)
|
---|
153 | {
|
---|
154 | return hr_raid0_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
---|
155 | }
|
---|
156 |
|
---|
157 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
158 | const void *data, size_t size)
|
---|
159 | {
|
---|
160 | return hr_raid0_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
---|
161 | }
|
---|
162 |
|
---|
163 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
164 | {
|
---|
165 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
166 |
|
---|
167 | *rsize = vol->bsize;
|
---|
168 | return EOK;
|
---|
169 | }
|
---|
170 |
|
---|
171 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
172 | {
|
---|
173 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
174 |
|
---|
175 | *rnb = vol->data_blkno;
|
---|
176 | return EOK;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static errno_t hr_raid0_check_vol_status(hr_volume_t *vol)
|
---|
180 | {
|
---|
181 | if (vol->status == HR_VOL_ONLINE)
|
---|
182 | return EOK;
|
---|
183 | return EINVAL;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Update vol->status and return EOK if volume
|
---|
188 | * is usable
|
---|
189 | */
|
---|
190 | static errno_t hr_raid0_update_vol_status(hr_volume_t *vol)
|
---|
191 | {
|
---|
192 | for (size_t i = 0; i < vol->dev_no; i++) {
|
---|
193 | if (vol->extents[i].status != HR_EXT_ONLINE) {
|
---|
194 | HR_WARN("RAID 0 needs all extents to be ONLINE, "
|
---|
195 | "marking \"%s\" (%lu) as FAULTY",
|
---|
196 | vol->devname, vol->svc_id);
|
---|
197 | vol->status = HR_VOL_FAULTY;
|
---|
198 | return EINVAL;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | vol->status = HR_VOL_ONLINE;
|
---|
203 | return EOK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static errno_t hr_raid0_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
207 | size_t cnt, void *dst, const void *src, size_t size)
|
---|
208 | {
|
---|
209 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
210 | errno_t rc;
|
---|
211 | uint64_t phys_block, len;
|
---|
212 | size_t left;
|
---|
213 | const uint8_t *data_write = src;
|
---|
214 | uint8_t *data_read = dst;
|
---|
215 |
|
---|
216 | /* propagate sync */
|
---|
217 | if (type == HR_BD_SYNC && ba == 0 && cnt == 0) {
|
---|
218 | hr_sync_all_extents(vol);
|
---|
219 | rc = hr_raid0_update_vol_status(vol);
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
---|
224 | if (size < cnt * vol->bsize)
|
---|
225 | return EINVAL;
|
---|
226 |
|
---|
227 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
228 | if (rc != EOK)
|
---|
229 | return rc;
|
---|
230 |
|
---|
231 | uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
|
---|
232 | uint64_t stripe = ba / strip_size; /* stripe number */
|
---|
233 | uint64_t extent = stripe % vol->dev_no;
|
---|
234 | uint64_t ext_stripe = stripe / vol->dev_no; /* stripe level */
|
---|
235 | uint64_t strip_off = ba % strip_size; /* strip offset */
|
---|
236 |
|
---|
237 | fibril_mutex_lock(&vol->lock);
|
---|
238 |
|
---|
239 | rc = hr_raid0_check_vol_status(vol);
|
---|
240 | if (rc != EOK) {
|
---|
241 | fibril_mutex_unlock(&vol->lock);
|
---|
242 | return EIO;
|
---|
243 | }
|
---|
244 |
|
---|
245 | left = cnt;
|
---|
246 | while (left != 0) {
|
---|
247 | phys_block = ext_stripe * strip_size + strip_off;
|
---|
248 | cnt = min(left, strip_size - strip_off);
|
---|
249 | len = vol->bsize * cnt;
|
---|
250 | hr_add_ba_offset(vol, &phys_block);
|
---|
251 | switch (type) {
|
---|
252 | case HR_BD_SYNC:
|
---|
253 | rc = block_sync_cache(vol->extents[extent].svc_id,
|
---|
254 | phys_block, cnt);
|
---|
255 | /* allow unsupported sync */
|
---|
256 | if (rc == ENOTSUP)
|
---|
257 | rc = EOK;
|
---|
258 | break;
|
---|
259 | case HR_BD_READ:
|
---|
260 | rc = block_read_direct(vol->extents[extent].svc_id,
|
---|
261 | phys_block, cnt, data_read);
|
---|
262 | data_read += len;
|
---|
263 | break;
|
---|
264 | case HR_BD_WRITE:
|
---|
265 | rc = block_write_direct(vol->extents[extent].svc_id,
|
---|
266 | phys_block, cnt, data_write);
|
---|
267 | data_write += len;
|
---|
268 | break;
|
---|
269 | default:
|
---|
270 | rc = EINVAL;
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (rc == ENOENT) {
|
---|
274 | hr_update_ext_status(vol, extent, HR_EXT_MISSING);
|
---|
275 | rc = EIO;
|
---|
276 | goto error;
|
---|
277 | } else if (rc != EOK) {
|
---|
278 | hr_update_ext_status(vol, extent, HR_EXT_FAILED);
|
---|
279 | rc = EIO;
|
---|
280 | goto error;
|
---|
281 | }
|
---|
282 |
|
---|
283 | left -= cnt;
|
---|
284 | strip_off = 0;
|
---|
285 | extent++;
|
---|
286 | if (extent >= vol->dev_no) {
|
---|
287 | ext_stripe++;
|
---|
288 | extent = 0;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | error:
|
---|
293 | (void)hr_raid0_update_vol_status(vol);
|
---|
294 | fibril_mutex_unlock(&vol->lock);
|
---|
295 | return rc;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /** @}
|
---|
299 | */
|
---|