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

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

hr: remove big lock, add lock for individual volumes

  • 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 return ENOMEM;
101
102 for (i = 0; i < vol->dev_no; i++) {
103 if (i == p_extent)
104 continue;
105
106 if (i == extent) {
107 xor(xorbuf, data, vol->bsize);
108 } else {
109 rc = block_read_direct(vol->extents[i].svc_id, block, 1, buf);
110 if (rc != EOK)
111 goto end;
112 xor(xorbuf, buf, vol->bsize);
113 }
114 }
115
116 rc = block_write_direct(vol->extents[p_extent].svc_id, block, 1, xorbuf);
117
118end:
119 free(xorbuf);
120 free(buf);
121 return EOK;
122}
123
124static void raid5_geometry(uint64_t x, hr_volume_t *vol, size_t *extent,
125 uint64_t *phys_block, uint64_t *p_extent)
126{
127 uint64_t N = vol->dev_no; /* extents */
128 uint64_t L = vol->strip_size / vol->bsize; /* size of strip in blocks */
129
130 uint64_t p = ((x / L) / (N - 1)) % N;
131
132 uint64_t i; /* extent */
133 if (((x / L) % (N - 1)) < p)
134 i = (x / L) % (N - 1);
135 else
136 i = ((x / L) % (N - 1)) + 1;
137
138 uint64_t j = (x / L) / (N - 1); /* stripe */
139 uint64_t k = x % L; /* strip offset */
140
141 *extent = i;
142 *phys_block = j * L + k;
143 if (p_extent != NULL)
144 *p_extent = p;
145}
146
147static errno_t hr_raid5_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
148{
149 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
150 return EOK;
151}
152
153static errno_t hr_raid5_bd_close(bd_srv_t *bd)
154{
155 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
156 return EOK;
157}
158
159static errno_t hr_raid5_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
160{
161 hr_volume_t *vol = bd->srvs->sarg;
162 errno_t rc;
163 uint64_t phys_block;
164 size_t extent;
165
166 rc = hr_check_ba_range(vol, cnt, ba);
167 if (rc != EOK)
168 return rc;
169
170 fibril_mutex_lock(&vol->lock);
171
172 size_t left = cnt;
173 while (left != 0) {
174 raid5_geometry(ba, vol, &extent, &phys_block, NULL);
175 hr_add_ba_offset(vol, &phys_block);
176 rc = block_sync_cache(vol->extents[extent].svc_id, phys_block, 1);
177 if (rc != EOK)
178 break;
179 left--;
180 ba++;
181 }
182
183 fibril_mutex_unlock(&vol->lock);
184 return rc;
185}
186
187static errno_t hr_raid5_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
188 void *buf, size_t size)
189{
190 hr_volume_t *vol = bd->srvs->sarg;
191 errno_t rc;
192 uint64_t phys_block;
193 size_t extent;
194
195 if (size < cnt * vol->bsize)
196 return EINVAL;
197
198 rc = hr_check_ba_range(vol, cnt, ba);
199 if (rc != EOK)
200 return rc;
201
202 fibril_mutex_lock(&vol->lock);
203
204 size_t left = cnt;
205 while (left != 0) {
206 raid5_geometry(ba, vol, &extent, &phys_block, NULL);
207 hr_add_ba_offset(vol, &phys_block);
208 rc = block_read_direct(vol->extents[extent].svc_id, phys_block, 1, buf);
209 buf = buf + vol->bsize;
210 if (rc != EOK)
211 break;
212 left--;
213 ba++;
214 }
215
216 fibril_mutex_unlock(&vol->lock);
217 return rc;
218}
219
220static errno_t hr_raid5_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
221 const void *data, size_t size)
222{
223 hr_volume_t *vol = bd->srvs->sarg;
224 errno_t rc;
225 uint64_t phys_block, p_extent;
226 size_t extent;
227
228 if (size < cnt * vol->bsize)
229 return EINVAL;
230
231 rc = hr_check_ba_range(vol, cnt, ba);
232 if (rc != EOK)
233 return rc;
234
235 fibril_mutex_lock(&vol->lock);
236
237 size_t left = cnt;
238 while (left != 0) {
239 raid5_geometry(ba, vol, &extent, &phys_block, &p_extent);
240 hr_add_ba_offset(vol, &phys_block);
241 rc = block_write_direct(vol->extents[extent].svc_id, phys_block, 1, data);
242 if (rc != EOK)
243 break;
244 rc = write_parity(vol, p_extent, extent, phys_block, data);
245 if (rc != EOK)
246 break;
247 data = data + vol->bsize;
248 left--;
249 ba++;
250 }
251
252 fibril_mutex_unlock(&vol->lock);
253 return rc;
254}
255
256static errno_t hr_raid5_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
257{
258 hr_volume_t *vol = bd->srvs->sarg;
259
260 *rsize = vol->bsize;
261 return EOK;
262}
263
264static errno_t hr_raid5_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
265{
266 hr_volume_t *vol = bd->srvs->sarg;
267
268 *rnb = vol->data_blkno;
269 return EOK;
270}
271
272errno_t hr_raid5_create(hr_volume_t *new_volume)
273{
274 errno_t rc;
275
276 assert(new_volume->level == hr_l_5);
277
278 if (new_volume->dev_no < 3) {
279 log_msg(LOG_DEFAULT, LVL_ERROR,
280 "RAID 5 array needs at least 3 devices");
281 return EINVAL;
282 }
283
284 bd_srvs_init(&new_volume->hr_bds);
285 new_volume->hr_bds.ops = &hr_raid5_bd_ops;
286 new_volume->hr_bds.sarg = new_volume;
287
288 rc = hr_register_volume(new_volume);
289 if (rc != EOK)
290 return rc;
291
292 return EOK;
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_l_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.