source: mainline/uspace/srv/bd/hr/raid0.c@ e192339

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

hr: add RAID 0 (striping)

  • Property mode set to 100644
File size: 6.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 "var.h"
51#include "util.h"
52
53extern fibril_mutex_t big_lock;
54extern loc_srv_t *hr_srv;
55
56static errno_t hr_raid0_bd_open(bd_srvs_t *, bd_srv_t *);
57static errno_t hr_raid0_bd_close(bd_srv_t *);
58static errno_t hr_raid0_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
59 size_t);
60static errno_t hr_raid0_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
61static errno_t hr_raid0_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
62 const void *, size_t);
63static errno_t hr_raid0_bd_get_block_size(bd_srv_t *, size_t *);
64static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
65
66#define strip_size DATA_XFER_LIMIT
67
68static bd_ops_t hr_raid0_bd_ops = {
69 .open = hr_raid0_bd_open,
70 .close = hr_raid0_bd_close,
71 .sync_cache = hr_raid0_bd_sync_cache,
72 .read_blocks = hr_raid0_bd_read_blocks,
73 .write_blocks = hr_raid0_bd_write_blocks,
74 .get_block_size = hr_raid0_bd_get_block_size,
75 .get_num_blocks = hr_raid0_bd_get_num_blocks
76};
77
78static void raid0_geometry(uint64_t x, hr_volume_t *vol, size_t *extent,
79 uint64_t *phys_block)
80{
81 uint64_t N = vol->dev_no; /* extents */
82 uint64_t L = strip_size / vol->bsize; /* size of strip in blocks */
83
84 uint64_t i = (x / L) % N; /* extent */
85 uint64_t j = (x / L) / N; /* stripe */
86 uint64_t k = x % L; /* strip offset */
87
88 *extent = i;
89 *phys_block = j * L + k;
90}
91
92static errno_t hr_raid0_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
93{
94 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
95 return EOK;
96}
97
98static errno_t hr_raid0_bd_close(bd_srv_t *bd)
99{
100 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
101 return EOK;
102}
103
104static errno_t hr_raid0_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
105{
106 hr_volume_t *vol = bd->srvs->sarg;
107 errno_t rc;
108 uint64_t phys_block;
109 size_t extent;
110
111 fibril_mutex_lock(&big_lock);
112
113 size_t left = cnt;
114 while (left != 0) {
115 raid0_geometry(ba++, vol, &extent, &phys_block);
116 rc = block_sync_cache(vol->devs[extent], phys_block, 1);
117 if (rc != EOK)
118 break;
119 left--;
120 }
121
122 fibril_mutex_unlock(&big_lock);
123 return rc;
124}
125
126static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
127 void *buf, size_t size)
128{
129 hr_volume_t *vol = bd->srvs->sarg;
130 errno_t rc;
131 uint64_t phys_block;
132 size_t extent;
133
134 if (size < cnt * vol->bsize)
135 return EINVAL;
136
137 fibril_mutex_lock(&big_lock);
138
139 size_t left = cnt;
140 while (left != 0) {
141 raid0_geometry(ba++, vol, &extent, &phys_block);
142 rc = block_read_direct(vol->devs[extent], phys_block, 1, buf);
143 buf = buf + vol->bsize;
144 if (rc != EOK)
145 break;
146 left--;
147 }
148
149 fibril_mutex_unlock(&big_lock);
150 return rc;
151}
152
153static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
154 const void *data, size_t size)
155{
156 hr_volume_t *vol = bd->srvs->sarg;
157 errno_t rc;
158 uint64_t phys_block;
159 size_t extent;
160
161 if (size < cnt * vol->bsize)
162 return EINVAL;
163
164 fibril_mutex_lock(&big_lock);
165
166 size_t left = cnt;
167 while (left != 0) {
168 raid0_geometry(ba++, vol, &extent, &phys_block);
169 rc = block_write_direct(vol->devs[extent], phys_block, 1, data);
170 data = data + vol->bsize;
171 if (rc != EOK)
172 break;
173 left--;
174 }
175
176 fibril_mutex_unlock(&big_lock);
177 return rc;
178}
179
180static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
181{
182 hr_volume_t *vol = bd->srvs->sarg;
183
184 *rsize = vol->bsize;
185 return EOK;
186}
187
188static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
189{
190 hr_volume_t *vol = bd->srvs->sarg;
191
192 *rnb = vol->nblocks;
193 return EOK;
194}
195
196errno_t hr_raid0_create(hr_volume_t *new_volume)
197{
198 assert(new_volume->level == hr_l_0);
199
200 if (new_volume->dev_no < 2) {
201 log_msg(LOG_DEFAULT, LVL_ERROR,
202 "RAID 0 array needs at least 2 devices");
203 return EINVAL;
204 }
205
206 errno_t rc;
207 size_t i, bsize, last_bsize;
208 uint64_t nblocks, last_nblocks;
209 uint64_t total_blocks = 0;
210
211 rc = hr_init_devs(new_volume);
212 if (rc != EOK)
213 return rc;
214
215 for (i = 0; i < new_volume->dev_no; i++) {
216 rc = block_get_nblocks(new_volume->devs[i], &nblocks);
217 if (rc != EOK)
218 goto error;
219 if (i != 0 && nblocks != last_nblocks) {
220 log_msg(LOG_DEFAULT, LVL_ERROR,
221 "number of blocks differs");
222 rc = EINVAL;
223 goto error;
224 }
225 total_blocks += nblocks;
226 last_nblocks = nblocks;
227 }
228
229 for (i = 0; i < new_volume->dev_no; i++) {
230 rc = block_get_bsize(new_volume->devs[i], &bsize);
231 if (rc != EOK)
232 goto error;
233 if (i != 0 && bsize != last_bsize) {
234 log_msg(LOG_DEFAULT, LVL_ERROR, "block sizes differ");
235 rc = EINVAL;
236 goto error;
237 }
238 last_bsize = bsize;
239 }
240
241 bd_srvs_init(&new_volume->hr_bds);
242 new_volume->hr_bds.ops = &hr_raid0_bd_ops;
243 new_volume->hr_bds.sarg = new_volume;
244 new_volume->nblocks = total_blocks;
245 new_volume->bsize = bsize;
246
247 rc = hr_register_volume(new_volume);
248 if (rc != EOK)
249 goto error;
250
251 return EOK;
252error:
253 hr_fini_devs(new_volume);
254 return rc;
255}
256
257/** @}
258 */
Note: See TracBrowser for help on using the repository browser.