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 |
|
---|
54 | extern loc_srv_t *hr_srv;
|
---|
55 |
|
---|
56 | static errno_t hr_raid0_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
57 | static errno_t hr_raid0_bd_close(bd_srv_t *);
|
---|
58 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
59 | size_t);
|
---|
60 | static errno_t hr_raid0_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
61 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
62 | const void *, size_t);
|
---|
63 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
64 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
65 |
|
---|
66 | static 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 |
|
---|
76 | static 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 |
|
---|
90 | static 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 |
|
---|
96 | static 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 |
|
---|
102 | static 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, left;
|
---|
108 |
|
---|
109 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
110 | if (rc != EOK)
|
---|
111 | return rc;
|
---|
112 |
|
---|
113 | fibril_mutex_lock(&vol->lock);
|
---|
114 |
|
---|
115 | left = cnt;
|
---|
116 | while (left != 0) {
|
---|
117 | raid0_geometry(ba, vol, &extent, &phys_block);
|
---|
118 | hr_add_ba_offset(vol, &phys_block);
|
---|
119 | rc = block_sync_cache(vol->extents[extent].svc_id, phys_block, 1);
|
---|
120 | if (rc != EOK)
|
---|
121 | break;
|
---|
122 | left--;
|
---|
123 | ba++;
|
---|
124 | }
|
---|
125 |
|
---|
126 | fibril_mutex_unlock(&vol->lock);
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static errno_t hr_raid0_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
131 | void *buf, size_t size)
|
---|
132 | {
|
---|
133 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
134 | errno_t rc;
|
---|
135 | uint64_t phys_block;
|
---|
136 | size_t extent, left;
|
---|
137 |
|
---|
138 | if (size < cnt * vol->bsize)
|
---|
139 | return EINVAL;
|
---|
140 |
|
---|
141 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
142 | if (rc != EOK)
|
---|
143 | return rc;
|
---|
144 |
|
---|
145 | fibril_mutex_lock(&vol->lock);
|
---|
146 |
|
---|
147 | left = cnt;
|
---|
148 | while (left != 0) {
|
---|
149 | raid0_geometry(ba, vol, &extent, &phys_block);
|
---|
150 | hr_add_ba_offset(vol, &phys_block);
|
---|
151 | rc = block_read_direct(vol->extents[extent].svc_id, phys_block, 1, buf);
|
---|
152 | buf = buf + vol->bsize;
|
---|
153 | if (rc != EOK)
|
---|
154 | break;
|
---|
155 | left--;
|
---|
156 | ba++;
|
---|
157 | }
|
---|
158 |
|
---|
159 | fibril_mutex_unlock(&vol->lock);
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 | static errno_t hr_raid0_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
164 | const void *data, size_t size)
|
---|
165 | {
|
---|
166 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
167 | errno_t rc;
|
---|
168 | uint64_t phys_block;
|
---|
169 | size_t extent, left;
|
---|
170 |
|
---|
171 | if (size < cnt * vol->bsize)
|
---|
172 | return EINVAL;
|
---|
173 |
|
---|
174 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
175 | if (rc != EOK)
|
---|
176 | return rc;
|
---|
177 |
|
---|
178 | fibril_mutex_lock(&vol->lock);
|
---|
179 |
|
---|
180 | left = cnt;
|
---|
181 | while (left != 0) {
|
---|
182 | raid0_geometry(ba, vol, &extent, &phys_block);
|
---|
183 | hr_add_ba_offset(vol, &phys_block);
|
---|
184 | rc = block_write_direct(vol->extents[extent].svc_id, phys_block, 1, data);
|
---|
185 | data = data + vol->bsize;
|
---|
186 | if (rc != EOK)
|
---|
187 | break;
|
---|
188 | left--;
|
---|
189 | ba++;
|
---|
190 | }
|
---|
191 |
|
---|
192 | fibril_mutex_unlock(&vol->lock);
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 | static errno_t hr_raid0_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
197 | {
|
---|
198 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
199 |
|
---|
200 | *rsize = vol->bsize;
|
---|
201 | return EOK;
|
---|
202 | }
|
---|
203 |
|
---|
204 | static errno_t hr_raid0_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
205 | {
|
---|
206 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
207 |
|
---|
208 | *rnb = vol->data_blkno;
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | errno_t hr_raid0_create(hr_volume_t *new_volume)
|
---|
213 | {
|
---|
214 | errno_t rc;
|
---|
215 |
|
---|
216 | assert(new_volume->level == hr_l_0);
|
---|
217 |
|
---|
218 | if (new_volume->dev_no < 2) {
|
---|
219 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
220 | "RAID 0 array needs at least 2 devices");
|
---|
221 | return EINVAL;
|
---|
222 | }
|
---|
223 |
|
---|
224 | bd_srvs_init(&new_volume->hr_bds);
|
---|
225 | new_volume->hr_bds.ops = &hr_raid0_bd_ops;
|
---|
226 | new_volume->hr_bds.sarg = new_volume;
|
---|
227 |
|
---|
228 | rc = hr_register_volume(new_volume);
|
---|
229 | if (rc != EOK)
|
---|
230 | return rc;
|
---|
231 |
|
---|
232 | return EOK;
|
---|
233 | }
|
---|
234 |
|
---|
235 | errno_t hr_raid0_init(hr_volume_t *vol)
|
---|
236 | {
|
---|
237 | errno_t rc;
|
---|
238 | size_t bsize;
|
---|
239 | uint64_t total_blkno;
|
---|
240 |
|
---|
241 | assert(vol->level == hr_l_0);
|
---|
242 |
|
---|
243 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
---|
244 | if (rc != EOK)
|
---|
245 | return rc;
|
---|
246 |
|
---|
247 | vol->nblocks = total_blkno;
|
---|
248 | vol->bsize = bsize;
|
---|
249 | vol->data_offset = HR_DATA_OFF;
|
---|
250 | vol->data_blkno = vol->nblocks - (vol->data_offset * vol->dev_no);
|
---|
251 | vol->strip_size = HR_STRIP_SIZE;
|
---|
252 |
|
---|
253 | return EOK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** @}
|
---|
257 | */
|
---|