source: mainline/uspace/srv/bd/hr/raid4.c@ 066fed9

Last change on this file since 066fed9 was 978130a, checked in by Miroslav Cimerman <mc@…>, 12 months ago

hr: optimize RAID 0, 4, 5 to write whole strip

  • Property mode set to 100644
File size: 7.3 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 <mem.h>
46#include <task.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <str_error.h>
50
51#include "superblock.h"
52#include "util.h"
53#include "var.h"
54
55extern loc_srv_t *hr_srv;
56
57static errno_t hr_raid4_bd_open(bd_srvs_t *, bd_srv_t *);
58static errno_t hr_raid4_bd_close(bd_srv_t *);
59static errno_t hr_raid4_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
60 size_t);
61static errno_t hr_raid4_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
62static errno_t hr_raid4_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
63 const void *, size_t);
64static errno_t hr_raid4_bd_get_block_size(bd_srv_t *, size_t *);
65static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
66
67static 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
77static 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
87static errno_t write_parity(hr_volume_t *vol, uint64_t extent, uint64_t block,
88 const void *data, size_t cnt)
89{
90 errno_t rc;
91 size_t i, j;
92 void *xorbuf;
93 void *buf;
94
95 xorbuf = malloc(vol->bsize);
96 if (xorbuf == NULL)
97 return ENOMEM;
98
99 buf = malloc(vol->bsize);
100 if (buf == NULL) {
101 free(xorbuf);
102 return ENOMEM;
103 }
104
105 for (j = 0; j < cnt; j++) {
106 memset(xorbuf, 0, vol->bsize);
107 for (i = 1; i < vol->dev_no; i++) {
108 if (i == extent) {
109 xor(xorbuf, data, vol->bsize);
110 } else {
111 rc = block_read_direct(vol->extents[i].svc_id,
112 block, 1, buf);
113 if (rc != EOK)
114 goto end;
115 xor(xorbuf, buf, vol->bsize);
116 }
117 }
118 rc = block_write_direct(vol->extents[0].svc_id, block, 1,
119 xorbuf);
120 if (rc != EOK)
121 goto end;
122 data = (void *) ((uintptr_t) data + vol->bsize);
123 block++;
124 }
125end:
126 free(xorbuf);
127 free(buf);
128 return rc;
129}
130
131static errno_t hr_raid4_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
132{
133 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
134 return EOK;
135}
136
137static errno_t hr_raid4_bd_close(bd_srv_t *bd)
138{
139 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
140 return EOK;
141}
142
143static errno_t hr_raid4_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
144 size_t cnt, void *data_read, const void *data_write, size_t size)
145{
146 hr_volume_t *vol = bd->srvs->sarg;
147 errno_t rc;
148 uint64_t phys_block;
149 size_t left;
150
151 if (type == HR_BD_READ || type == HR_BD_WRITE)
152 if (size < cnt * vol->bsize)
153 return EINVAL;
154
155 rc = hr_check_ba_range(vol, cnt, ba);
156 if (rc != EOK)
157 return rc;
158
159 uint64_t strip_size = vol->strip_size / vol->bsize; /* in blocks */
160 uint64_t stripe = (ba / strip_size); /* stripe number */
161 uint64_t extent = (stripe % (vol->dev_no - 1)) + 1;
162 uint64_t ext_stripe = stripe / (vol->dev_no - 1); /* stripe level */
163 uint64_t strip_off = ba % strip_size; /* strip offset */
164
165 fibril_mutex_lock(&vol->lock);
166
167 left = cnt;
168 while (left != 0) {
169 phys_block = ext_stripe * strip_size + strip_off;
170 cnt = min(left, strip_size - strip_off);
171 hr_add_ba_offset(vol, &phys_block);
172 switch (type) {
173 case HR_BD_SYNC:
174 rc = block_sync_cache(vol->extents[extent].svc_id,
175 phys_block, cnt);
176 break;
177 case HR_BD_READ:
178 rc = block_read_direct(vol->extents[extent].svc_id,
179 phys_block, cnt, data_read);
180 data_read = (void *) ((uintptr_t) data_read +
181 (vol->bsize * cnt));
182 break;
183 case HR_BD_WRITE:
184 rc = block_write_direct(vol->extents[extent].svc_id,
185 phys_block, cnt, data_write);
186 if (rc != EOK)
187 goto error;
188 rc = write_parity(vol, extent, phys_block, data_write,
189 cnt);
190 if (rc != EOK)
191 goto error;
192 data_write = (void *) ((uintptr_t) data_write +
193 (vol->bsize * cnt));
194 break;
195 default:
196 rc = EINVAL;
197 }
198
199 if (rc != EOK)
200 goto error;
201
202 left -= cnt;
203 strip_off = 0;
204 extent++;
205 if (extent >= vol->dev_no) {
206 ext_stripe++;
207 extent = 1;
208 }
209 }
210
211error:
212 fibril_mutex_unlock(&vol->lock);
213 return rc;
214}
215
216static errno_t hr_raid4_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
217{
218 return hr_raid4_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
219}
220
221static errno_t hr_raid4_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
222 void *buf, size_t size)
223{
224 return hr_raid4_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
225}
226
227static errno_t hr_raid4_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
228 const void *data, size_t size)
229{
230 return hr_raid4_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
231}
232
233static errno_t hr_raid4_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
234{
235 hr_volume_t *vol = bd->srvs->sarg;
236
237 *rsize = vol->bsize;
238 return EOK;
239}
240
241static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
242{
243 hr_volume_t *vol = bd->srvs->sarg;
244
245 *rnb = vol->data_blkno;
246 return EOK;
247}
248
249errno_t hr_raid4_create(hr_volume_t *new_volume)
250{
251 errno_t rc;
252
253 assert(new_volume->level == HR_LVL_4);
254
255 if (new_volume->dev_no < 3) {
256 log_msg(LOG_DEFAULT, LVL_ERROR,
257 "RAID 4 array needs at least 3 devices");
258 return EINVAL;
259 }
260
261 bd_srvs_init(&new_volume->hr_bds);
262 new_volume->hr_bds.ops = &hr_raid4_bd_ops;
263 new_volume->hr_bds.sarg = new_volume;
264
265 rc = hr_register_volume(new_volume);
266
267 return rc;
268}
269
270errno_t hr_raid4_init(hr_volume_t *vol)
271{
272 errno_t rc;
273 size_t bsize;
274 uint64_t total_blkno;
275
276 assert(vol->level == HR_LVL_4);
277
278 rc = hr_check_devs(vol, &total_blkno, &bsize);
279 if (rc != EOK)
280 return rc;
281
282 vol->nblocks = total_blkno;
283 vol->bsize = bsize;
284 vol->data_offset = HR_DATA_OFF;
285 vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no) -
286 (vol->nblocks / vol->dev_no);
287 vol->strip_size = HR_STRIP_SIZE;
288
289 return EOK;
290}
291
292/** @}
293 */
Note: See TracBrowser for help on using the repository browser.