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 fibril_mutex_t big_lock;
|
---|
55 | extern loc_srv_t *hr_srv;
|
---|
56 |
|
---|
57 | static errno_t hr_raid4_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
58 | static errno_t hr_raid4_bd_close(bd_srv_t *);
|
---|
59 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
60 | size_t);
|
---|
61 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
62 | static errno_t hr_raid4_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
63 | const void *, size_t);
|
---|
64 | static errno_t hr_raid4_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
65 | static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
66 |
|
---|
67 | static bd_ops_t hr_raid4_bd_ops = {
|
---|
68 | .open = hr_raid4_bd_open,
|
---|
69 | .close = hr_raid4_bd_close,
|
---|
70 | .sync_cache = hr_raid4_bd_sync_cache,
|
---|
71 | .read_blocks = hr_raid4_bd_read_blocks,
|
---|
72 | .write_blocks = hr_raid4_bd_write_blocks,
|
---|
73 | .get_block_size = hr_raid4_bd_get_block_size,
|
---|
74 | .get_num_blocks = hr_raid4_bd_get_num_blocks
|
---|
75 | };
|
---|
76 |
|
---|
77 | static void xor(void *dst, const void *src, size_t size)
|
---|
78 | {
|
---|
79 | size_t i;
|
---|
80 | uint64_t *d = dst;
|
---|
81 | const uint64_t *s = src;
|
---|
82 |
|
---|
83 | for (i = 0; i < size / sizeof(uint64_t); ++i)
|
---|
84 | *d++ ^= *s++;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static errno_t write_parity(hr_volume_t *vol, uint64_t extent, uint64_t block,
|
---|
88 | const void *data)
|
---|
89 | {
|
---|
90 | errno_t rc;
|
---|
91 | size_t i;
|
---|
92 | void *xorbuf;
|
---|
93 | void *buf;
|
---|
94 |
|
---|
95 | xorbuf = calloc(1, vol->bsize);
|
---|
96 | if (xorbuf == NULL)
|
---|
97 | return ENOMEM;
|
---|
98 |
|
---|
99 | buf = malloc(vol->bsize);
|
---|
100 | if (buf == NULL)
|
---|
101 | return ENOMEM;
|
---|
102 |
|
---|
103 | for (i = 1; i < vol->dev_no; i++) {
|
---|
104 | if (i == extent) {
|
---|
105 | xor(xorbuf, data, vol->bsize);
|
---|
106 | } else {
|
---|
107 | rc = block_read_direct(vol->extents[i].svc_id, block, 1, buf);
|
---|
108 | if (rc != EOK)
|
---|
109 | goto end;
|
---|
110 | xor(xorbuf, buf, vol->bsize);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | rc = block_write_direct(vol->extents[0].svc_id, block, 1, xorbuf);
|
---|
115 |
|
---|
116 | end:
|
---|
117 | free(xorbuf);
|
---|
118 | free(buf);
|
---|
119 | return EOK;
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void raid4_geometry(uint64_t x, hr_volume_t *vol, size_t *extent,
|
---|
123 | uint64_t *phys_block)
|
---|
124 | {
|
---|
125 | uint64_t N = vol->dev_no; /* extents */
|
---|
126 | uint64_t L = vol->strip_size / vol->bsize; /* size of strip in blocks */
|
---|
127 |
|
---|
128 | uint64_t i = ((x / L) % (N - 1)) + 1; /* extent */
|
---|
129 | uint64_t j = (x / L) / (N - 1); /* stripe */
|
---|
130 | uint64_t k = x % L; /* strip offset */
|
---|
131 |
|
---|
132 | *extent = i;
|
---|
133 | *phys_block = j * L + k;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static errno_t hr_raid4_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
137 | {
|
---|
138 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static errno_t hr_raid4_bd_close(bd_srv_t *bd)
|
---|
143 | {
|
---|
144 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
145 | return EOK;
|
---|
146 | }
|
---|
147 |
|
---|
148 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
149 | {
|
---|
150 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
151 | errno_t rc;
|
---|
152 | uint64_t phys_block;
|
---|
153 | size_t extent;
|
---|
154 |
|
---|
155 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
156 | if (rc != EOK)
|
---|
157 | return rc;
|
---|
158 |
|
---|
159 | fibril_mutex_lock(&big_lock);
|
---|
160 |
|
---|
161 | size_t left = cnt;
|
---|
162 | while (left != 0) {
|
---|
163 | raid4_geometry(ba, vol, &extent, &phys_block);
|
---|
164 | hr_add_ba_offset(vol, &phys_block);
|
---|
165 | rc = block_sync_cache(vol->extents[extent].svc_id, phys_block, 1);
|
---|
166 | if (rc != EOK)
|
---|
167 | break;
|
---|
168 | left--;
|
---|
169 | ba++;
|
---|
170 | }
|
---|
171 |
|
---|
172 | fibril_mutex_unlock(&big_lock);
|
---|
173 | return rc;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
177 | void *buf, size_t size)
|
---|
178 | {
|
---|
179 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
180 | errno_t rc;
|
---|
181 | uint64_t phys_block;
|
---|
182 | size_t extent;
|
---|
183 |
|
---|
184 | if (size < cnt * vol->bsize)
|
---|
185 | return EINVAL;
|
---|
186 |
|
---|
187 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
188 | if (rc != EOK)
|
---|
189 | return rc;
|
---|
190 |
|
---|
191 | fibril_mutex_lock(&big_lock);
|
---|
192 |
|
---|
193 | size_t left = cnt;
|
---|
194 | while (left != 0) {
|
---|
195 | raid4_geometry(ba, vol, &extent, &phys_block);
|
---|
196 | hr_add_ba_offset(vol, &phys_block);
|
---|
197 | rc = block_read_direct(vol->extents[extent].svc_id, phys_block, 1, buf);
|
---|
198 | buf = buf + vol->bsize;
|
---|
199 | if (rc != EOK)
|
---|
200 | break;
|
---|
201 | left--;
|
---|
202 | ba++;
|
---|
203 | }
|
---|
204 |
|
---|
205 | fibril_mutex_unlock(&big_lock);
|
---|
206 | return rc;
|
---|
207 | }
|
---|
208 |
|
---|
209 | static errno_t hr_raid4_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
210 | const void *data, size_t size)
|
---|
211 | {
|
---|
212 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
213 | errno_t rc;
|
---|
214 | uint64_t phys_block;
|
---|
215 | size_t extent;
|
---|
216 |
|
---|
217 | if (size < cnt * vol->bsize)
|
---|
218 | return EINVAL;
|
---|
219 |
|
---|
220 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
221 | if (rc != EOK)
|
---|
222 | return rc;
|
---|
223 |
|
---|
224 | fibril_mutex_lock(&big_lock);
|
---|
225 |
|
---|
226 | size_t left = cnt;
|
---|
227 | while (left != 0) {
|
---|
228 | raid4_geometry(ba, vol, &extent, &phys_block);
|
---|
229 | hr_add_ba_offset(vol, &phys_block);
|
---|
230 | rc = block_write_direct(vol->extents[extent].svc_id, phys_block, 1, data);
|
---|
231 | if (rc != EOK)
|
---|
232 | break;
|
---|
233 | rc = write_parity(vol, extent, phys_block, data);
|
---|
234 | if (rc != EOK)
|
---|
235 | break;
|
---|
236 | data = data + vol->bsize;
|
---|
237 | left--;
|
---|
238 | ba++;
|
---|
239 | }
|
---|
240 |
|
---|
241 | fibril_mutex_unlock(&big_lock);
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | static errno_t hr_raid4_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
246 | {
|
---|
247 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
248 |
|
---|
249 | *rsize = vol->bsize;
|
---|
250 | return EOK;
|
---|
251 | }
|
---|
252 |
|
---|
253 | static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
254 | {
|
---|
255 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
256 |
|
---|
257 | *rnb = vol->data_blkno;
|
---|
258 | return EOK;
|
---|
259 | }
|
---|
260 |
|
---|
261 | errno_t hr_raid4_create(hr_volume_t *new_volume)
|
---|
262 | {
|
---|
263 | errno_t rc;
|
---|
264 |
|
---|
265 | assert(new_volume->level == hr_l_4);
|
---|
266 |
|
---|
267 | if (new_volume->dev_no < 3) {
|
---|
268 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
269 | "RAID 4 array needs at least 3 devices");
|
---|
270 | return EINVAL;
|
---|
271 | }
|
---|
272 |
|
---|
273 | bd_srvs_init(&new_volume->hr_bds);
|
---|
274 | new_volume->hr_bds.ops = &hr_raid4_bd_ops;
|
---|
275 | new_volume->hr_bds.sarg = new_volume;
|
---|
276 |
|
---|
277 | rc = hr_register_volume(new_volume);
|
---|
278 | if (rc != EOK)
|
---|
279 | return rc;
|
---|
280 |
|
---|
281 | return EOK;
|
---|
282 | }
|
---|
283 |
|
---|
284 | errno_t hr_raid4_init(hr_volume_t *vol)
|
---|
285 | {
|
---|
286 | errno_t rc;
|
---|
287 | size_t bsize;
|
---|
288 | uint64_t total_blkno;
|
---|
289 |
|
---|
290 | assert(vol->level == hr_l_4);
|
---|
291 |
|
---|
292 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
---|
293 | if (rc != EOK)
|
---|
294 | return rc;
|
---|
295 |
|
---|
296 | vol->nblocks = total_blkno;
|
---|
297 | vol->bsize = bsize;
|
---|
298 | vol->data_offset = HR_DATA_OFF;
|
---|
299 | vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no) -
|
---|
300 | (vol->nblocks / vol->dev_no);
|
---|
301 | vol->strip_size = HR_STRIP_SIZE;
|
---|
302 |
|
---|
303 | return EOK;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /** @}
|
---|
307 | */
|
---|