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

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

hr: add strip size to metadata and hr_volume_t

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