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

Last change on this file since 4b759dc was 6b8e89b0, checked in by Miroslav Cimerman <mc@…>, 11 months ago

hr: init fuction for each RAID level

Compute total blocks, data blocks and set block size, data offset, strip
size there.

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