source: mainline/uspace/srv/bd/hr/raid5.c@ 50bed55d

Last change on this file since 50bed55d was 50bed55d, checked in by Miroslav Cimerman <mc@…>, 10 months ago

hr: rename levels to upper case

  • Property mode set to 100644
File size: 7.4 KB
Line 
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
54extern loc_srv_t *hr_srv;
55
56static errno_t hr_raid5_bd_open(bd_srvs_t *, bd_srv_t *);
57static errno_t hr_raid5_bd_close(bd_srv_t *);
58static errno_t hr_raid5_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
59 size_t);
60static errno_t hr_raid5_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
61static errno_t hr_raid5_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
62 const void *, size_t);
63static errno_t hr_raid5_bd_get_block_size(bd_srv_t *, size_t *);
64static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
65
66static bd_ops_t hr_raid5_bd_ops = {
67 .open = hr_raid5_bd_open,
68 .close = hr_raid5_bd_close,
69 .sync_cache = hr_raid5_bd_sync_cache,
70 .read_blocks = hr_raid5_bd_read_blocks,
71 .write_blocks = hr_raid5_bd_write_blocks,
72 .get_block_size = hr_raid5_bd_get_block_size,
73 .get_num_blocks = hr_raid5_bd_get_num_blocks
74};
75
76static 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
86static errno_t write_parity(hr_volume_t *vol, uint64_t p_extent,
87 uint64_t extent, uint64_t block, 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 free(xorbuf);
101 return ENOMEM;
102 }
103
104 for (i = 0; i < vol->dev_no; i++) {
105 if (i == p_extent)
106 continue;
107
108 if (i == extent) {
109 xor(xorbuf, data, vol->bsize);
110 } else {
111 rc = block_read_direct(vol->extents[i].svc_id, block, 1, buf);
112 if (rc != EOK)
113 goto end;
114 xor(xorbuf, buf, vol->bsize);
115 }
116 }
117
118 rc = block_write_direct(vol->extents[p_extent].svc_id, block, 1, xorbuf);
119
120end:
121 free(xorbuf);
122 free(buf);
123 return rc;
124}
125
126static void raid5_geometry(uint64_t x, hr_volume_t *vol, size_t *extent,
127 uint64_t *phys_block, uint64_t *p_extent)
128{
129 uint64_t N = vol->dev_no; /* extents */
130 uint64_t L = vol->strip_size / vol->bsize; /* size of strip in blocks */
131
132 uint64_t p = ((x / L) / (N - 1)) % N;
133
134 uint64_t i; /* extent */
135 if (((x / L) % (N - 1)) < p)
136 i = (x / L) % (N - 1);
137 else
138 i = ((x / L) % (N - 1)) + 1;
139
140 uint64_t j = (x / L) / (N - 1); /* stripe */
141 uint64_t k = x % L; /* strip offset */
142
143 *extent = i;
144 *phys_block = j * L + k;
145 if (p_extent != NULL)
146 *p_extent = p;
147}
148
149static errno_t hr_raid5_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
150{
151 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
152 return EOK;
153}
154
155static errno_t hr_raid5_bd_close(bd_srv_t *bd)
156{
157 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
158 return EOK;
159}
160
161static errno_t hr_raid5_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
162{
163 hr_volume_t *vol = bd->srvs->sarg;
164 errno_t rc;
165 uint64_t phys_block;
166 size_t extent;
167
168 rc = hr_check_ba_range(vol, cnt, ba);
169 if (rc != EOK)
170 return rc;
171
172 fibril_mutex_lock(&vol->lock);
173
174 size_t left = cnt;
175 while (left != 0) {
176 raid5_geometry(ba, vol, &extent, &phys_block, NULL);
177 hr_add_ba_offset(vol, &phys_block);
178 rc = block_sync_cache(vol->extents[extent].svc_id, phys_block, 1);
179 if (rc != EOK)
180 break;
181 left--;
182 ba++;
183 }
184
185 fibril_mutex_unlock(&vol->lock);
186 return rc;
187}
188
189static errno_t hr_raid5_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
190 void *buf, size_t size)
191{
192 hr_volume_t *vol = bd->srvs->sarg;
193 errno_t rc;
194 uint64_t phys_block;
195 size_t extent;
196
197 if (size < cnt * vol->bsize)
198 return EINVAL;
199
200 rc = hr_check_ba_range(vol, cnt, ba);
201 if (rc != EOK)
202 return rc;
203
204 fibril_mutex_lock(&vol->lock);
205
206 size_t left = cnt;
207 while (left != 0) {
208 raid5_geometry(ba, vol, &extent, &phys_block, NULL);
209 hr_add_ba_offset(vol, &phys_block);
210 rc = block_read_direct(vol->extents[extent].svc_id, phys_block, 1, buf);
211 buf = buf + vol->bsize;
212 if (rc != EOK)
213 break;
214 left--;
215 ba++;
216 }
217
218 fibril_mutex_unlock(&vol->lock);
219 return rc;
220}
221
222static errno_t hr_raid5_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
223 const void *data, size_t size)
224{
225 hr_volume_t *vol = bd->srvs->sarg;
226 errno_t rc;
227 uint64_t phys_block, p_extent;
228 size_t extent;
229
230 if (size < cnt * vol->bsize)
231 return EINVAL;
232
233 rc = hr_check_ba_range(vol, cnt, ba);
234 if (rc != EOK)
235 return rc;
236
237 fibril_mutex_lock(&vol->lock);
238
239 size_t left = cnt;
240 while (left != 0) {
241 raid5_geometry(ba, vol, &extent, &phys_block, &p_extent);
242 hr_add_ba_offset(vol, &phys_block);
243 rc = block_write_direct(vol->extents[extent].svc_id, phys_block, 1, data);
244 if (rc != EOK)
245 break;
246 rc = write_parity(vol, p_extent, extent, phys_block, data);
247 if (rc != EOK)
248 break;
249 data = data + vol->bsize;
250 left--;
251 ba++;
252 }
253
254 fibril_mutex_unlock(&vol->lock);
255 return rc;
256}
257
258static errno_t hr_raid5_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
259{
260 hr_volume_t *vol = bd->srvs->sarg;
261
262 *rsize = vol->bsize;
263 return EOK;
264}
265
266static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
267{
268 hr_volume_t *vol = bd->srvs->sarg;
269
270 *rnb = vol->data_blkno;
271 return EOK;
272}
273
274errno_t hr_raid5_create(hr_volume_t *new_volume)
275{
276 errno_t rc;
277
278 assert(new_volume->level == HR_LVL_5);
279
280 if (new_volume->dev_no < 3) {
281 log_msg(LOG_DEFAULT, LVL_ERROR,
282 "RAID 5 array needs at least 3 devices");
283 return EINVAL;
284 }
285
286 bd_srvs_init(&new_volume->hr_bds);
287 new_volume->hr_bds.ops = &hr_raid5_bd_ops;
288 new_volume->hr_bds.sarg = new_volume;
289
290 rc = hr_register_volume(new_volume);
291
292 return rc;
293}
294
295errno_t hr_raid5_init(hr_volume_t *vol)
296{
297 errno_t rc;
298 size_t bsize;
299 uint64_t total_blkno;
300
301 assert(vol->level == HR_LVL_5);
302
303 rc = hr_check_devs(vol, &total_blkno, &bsize);
304 if (rc != EOK)
305 return rc;
306
307 vol->nblocks = total_blkno;
308 vol->bsize = bsize;
309 vol->data_offset = HR_DATA_OFF;
310 vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no) -
311 (vol->nblocks / vol->dev_no);
312 vol->strip_size = HR_STRIP_SIZE;
313
314 return EOK;
315}
316
317/** @}
318 */
Note: See TracBrowser for help on using the repository browser.