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 "util.h"
|
---|
51 | #include "var.h"
|
---|
52 |
|
---|
53 | extern fibril_mutex_t big_lock;
|
---|
54 | extern loc_srv_t *hr_srv;
|
---|
55 |
|
---|
56 | static errno_t hr_raid4_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
57 | static errno_t hr_raid4_bd_close(bd_srv_t *);
|
---|
58 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
59 | size_t);
|
---|
60 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
61 | static errno_t hr_raid4_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
62 | const void *, size_t);
|
---|
63 | static errno_t hr_raid4_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
64 | static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
65 |
|
---|
66 | static bd_ops_t hr_raid4_bd_ops = {
|
---|
67 | .open = hr_raid4_bd_open,
|
---|
68 | .close = hr_raid4_bd_close,
|
---|
69 | .sync_cache = hr_raid4_bd_sync_cache,
|
---|
70 | .read_blocks = hr_raid4_bd_read_blocks,
|
---|
71 | .write_blocks = hr_raid4_bd_write_blocks,
|
---|
72 | .get_block_size = hr_raid4_bd_get_block_size,
|
---|
73 | .get_num_blocks = hr_raid4_bd_get_num_blocks
|
---|
74 | };
|
---|
75 |
|
---|
76 | static void xor(void *dst, const void *src, size_t size)
|
---|
77 | {
|
---|
78 | size_t i;
|
---|
79 | uint64_t *d = dst;
|
---|
80 | const uint64_t *s = src;
|
---|
81 |
|
---|
82 | for (i = 0; i < size / sizeof(uint64_t); ++i)
|
---|
83 | *d++ ^= *s++;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static errno_t write_parity(hr_volume_t *vol, uint64_t extent, uint64_t block,
|
---|
87 | const void *data)
|
---|
88 | {
|
---|
89 | errno_t rc;
|
---|
90 | size_t i;
|
---|
91 | void *xorbuf;
|
---|
92 | void *buf;
|
---|
93 |
|
---|
94 | xorbuf = calloc(1, vol->bsize);
|
---|
95 | if (xorbuf == NULL)
|
---|
96 | return ENOMEM;
|
---|
97 |
|
---|
98 | buf = malloc(vol->bsize);
|
---|
99 | if (buf == NULL)
|
---|
100 | return ENOMEM;
|
---|
101 |
|
---|
102 | for (i = 1; i < vol->dev_no; i++) {
|
---|
103 | if (i == extent) {
|
---|
104 | xor(xorbuf, data, vol->bsize);
|
---|
105 | } else {
|
---|
106 | rc = block_read_direct(vol->devs[i], block, 1, buf);
|
---|
107 | if (rc != EOK)
|
---|
108 | goto end;
|
---|
109 | xor(xorbuf, buf, vol->bsize);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | rc = block_write_direct(vol->devs[0], block, 1, xorbuf);
|
---|
114 |
|
---|
115 | end:
|
---|
116 | free(xorbuf);
|
---|
117 | free(buf);
|
---|
118 | return EOK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | static void raid4_geometry(uint64_t x, hr_volume_t *vol, size_t *extent,
|
---|
122 | uint64_t *phys_block)
|
---|
123 | {
|
---|
124 | uint64_t N = vol->dev_no; /* extents */
|
---|
125 | uint64_t L = vol->strip_size / vol->bsize; /* size of strip in blocks */
|
---|
126 |
|
---|
127 | uint64_t i = ((x / L) % (N - 1)) + 1; /* extent */
|
---|
128 | uint64_t j = (x / L) / (N - 1); /* stripe */
|
---|
129 | uint64_t k = x % L; /* strip offset */
|
---|
130 |
|
---|
131 | *extent = i;
|
---|
132 | *phys_block = j * L + k;
|
---|
133 | }
|
---|
134 |
|
---|
135 | static errno_t hr_raid4_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
136 | {
|
---|
137 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static errno_t hr_raid4_bd_close(bd_srv_t *bd)
|
---|
142 | {
|
---|
143 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
144 | return EOK;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static errno_t hr_raid4_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
148 | {
|
---|
149 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
150 | errno_t rc;
|
---|
151 | uint64_t phys_block;
|
---|
152 | size_t extent;
|
---|
153 |
|
---|
154 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
155 | if (rc != EOK)
|
---|
156 | return rc;
|
---|
157 |
|
---|
158 | fibril_mutex_lock(&big_lock);
|
---|
159 |
|
---|
160 | size_t left = cnt;
|
---|
161 | while (left != 0) {
|
---|
162 | raid4_geometry(ba, vol, &extent, &phys_block);
|
---|
163 | hr_add_ba_offset(vol, &phys_block);
|
---|
164 | rc = block_sync_cache(vol->devs[extent], phys_block, 1);
|
---|
165 | if (rc != EOK)
|
---|
166 | break;
|
---|
167 | left--;
|
---|
168 | ba++;
|
---|
169 | }
|
---|
170 |
|
---|
171 | fibril_mutex_unlock(&big_lock);
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static errno_t hr_raid4_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
176 | void *buf, size_t size)
|
---|
177 | {
|
---|
178 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
179 | errno_t rc;
|
---|
180 | uint64_t phys_block;
|
---|
181 | size_t extent;
|
---|
182 |
|
---|
183 | if (size < cnt * vol->bsize)
|
---|
184 | return EINVAL;
|
---|
185 |
|
---|
186 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
187 | if (rc != EOK)
|
---|
188 | return rc;
|
---|
189 |
|
---|
190 | fibril_mutex_lock(&big_lock);
|
---|
191 |
|
---|
192 | size_t left = cnt;
|
---|
193 | while (left != 0) {
|
---|
194 | raid4_geometry(ba, vol, &extent, &phys_block);
|
---|
195 | hr_add_ba_offset(vol, &phys_block);
|
---|
196 | rc = block_read_direct(vol->devs[extent], phys_block, 1, buf);
|
---|
197 | buf = buf + vol->bsize;
|
---|
198 | if (rc != EOK)
|
---|
199 | break;
|
---|
200 | left--;
|
---|
201 | ba++;
|
---|
202 | }
|
---|
203 |
|
---|
204 | fibril_mutex_unlock(&big_lock);
|
---|
205 | return rc;
|
---|
206 | }
|
---|
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->devs[extent], 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 | /** @}
|
---|
285 | */
|
---|