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 <bd_srv.h>
|
---|
37 | #include <block.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <hr.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <ipc/hr.h>
|
---|
42 | #include <ipc/services.h>
|
---|
43 | #include <loc.h>
|
---|
44 | #include <task.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <str_error.h>
|
---|
48 |
|
---|
49 | #include "superblock.h"
|
---|
50 | #include "util.h"
|
---|
51 | #include "var.h"
|
---|
52 |
|
---|
53 | extern loc_srv_t *hr_srv;
|
---|
54 |
|
---|
55 | static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
56 | static errno_t hr_raid1_bd_close(bd_srv_t *);
|
---|
57 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
58 | size_t);
|
---|
59 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
60 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
61 | const void *, size_t);
|
---|
62 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
63 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
64 |
|
---|
65 | static bd_ops_t hr_raid1_bd_ops = {
|
---|
66 | .open = hr_raid1_bd_open,
|
---|
67 | .close = hr_raid1_bd_close,
|
---|
68 | .sync_cache = hr_raid1_bd_sync_cache,
|
---|
69 | .read_blocks = hr_raid1_bd_read_blocks,
|
---|
70 | .write_blocks = hr_raid1_bd_write_blocks,
|
---|
71 | .get_block_size = hr_raid1_bd_get_block_size,
|
---|
72 | .get_num_blocks = hr_raid1_bd_get_num_blocks
|
---|
73 | };
|
---|
74 |
|
---|
75 | static errno_t hr_raid1_check_vol_status(hr_volume_t *vol)
|
---|
76 | {
|
---|
77 | if (vol->status == HR_VOL_ONLINE ||
|
---|
78 | vol->status == HR_VOL_DEGRADED)
|
---|
79 | return EOK;
|
---|
80 | return EINVAL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Update vol->status and return EOK if volume
|
---|
85 | * is usable
|
---|
86 | */
|
---|
87 | static errno_t hr_raid1_update_vol_status(hr_volume_t *vol)
|
---|
88 | {
|
---|
89 | hr_vol_status_t old_state = vol->status;
|
---|
90 | size_t healthy = 0;
|
---|
91 | for (size_t i = 0; i < vol->dev_no; i++)
|
---|
92 | if (vol->extents[i].status == HR_EXT_ONLINE)
|
---|
93 | healthy++;
|
---|
94 |
|
---|
95 | if (healthy == 0) {
|
---|
96 | if (old_state != HR_VOL_FAULTY) {
|
---|
97 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
98 | "RAID 1 needs at least 1 extent to be ONLINE, "
|
---|
99 | "marking \"%s\" (%lu) as FAULTY",
|
---|
100 | vol->devname, vol->svc_id);
|
---|
101 | vol->status = HR_VOL_FAULTY;
|
---|
102 | }
|
---|
103 | return EINVAL;
|
---|
104 | } else if (healthy < vol->dev_no) {
|
---|
105 | if (old_state != HR_VOL_DEGRADED) {
|
---|
106 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
107 | "RAID 1 array \"%s\" (%lu) has some inactive "
|
---|
108 | "extents, marking as DEGRADED",
|
---|
109 | vol->devname, vol->svc_id);
|
---|
110 | vol->status = HR_VOL_DEGRADED;
|
---|
111 | }
|
---|
112 | return EOK;
|
---|
113 | } else {
|
---|
114 | if (old_state != HR_VOL_ONLINE) {
|
---|
115 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
116 | "RAID 1 array \"%s\" (%lu) has all extents active, "
|
---|
117 | "marking as ONLINE",
|
---|
118 | vol->devname, vol->svc_id);
|
---|
119 | vol->status = HR_VOL_ONLINE;
|
---|
120 | }
|
---|
121 | return EOK;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
126 | {
|
---|
127 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
128 | return EOK;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static errno_t hr_raid1_bd_close(bd_srv_t *bd)
|
---|
132 | {
|
---|
133 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void handle_extent_error(hr_volume_t *vol, size_t extent,
|
---|
138 | errno_t rc)
|
---|
139 | {
|
---|
140 | if (rc == ENOENT)
|
---|
141 | hr_update_ext_status(vol, extent, HR_EXT_MISSING);
|
---|
142 | else if (rc != EOK)
|
---|
143 | hr_update_ext_status(vol, extent, HR_EXT_FAILED);
|
---|
144 | }
|
---|
145 |
|
---|
146 | static errno_t hr_raid1_bd_op(hr_bd_op_type_t type, bd_srv_t *bd, aoff64_t ba,
|
---|
147 | size_t cnt, void *data_read, const void *data_write, size_t size)
|
---|
148 | {
|
---|
149 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
150 | errno_t rc;
|
---|
151 | size_t i;
|
---|
152 |
|
---|
153 | if (type == HR_BD_READ || type == HR_BD_WRITE)
|
---|
154 | if (size < cnt * vol->bsize)
|
---|
155 | return EINVAL;
|
---|
156 |
|
---|
157 | rc = hr_check_ba_range(vol, cnt, ba);
|
---|
158 | if (rc != EOK)
|
---|
159 | return rc;
|
---|
160 |
|
---|
161 | hr_add_ba_offset(vol, &ba);
|
---|
162 |
|
---|
163 | fibril_mutex_lock(&vol->lock);
|
---|
164 |
|
---|
165 | rc = hr_raid1_check_vol_status(vol);
|
---|
166 | if (rc != EOK) {
|
---|
167 | fibril_mutex_unlock(&vol->lock);
|
---|
168 | return EIO;
|
---|
169 | }
|
---|
170 |
|
---|
171 | size_t successful = 0;
|
---|
172 | switch (type) {
|
---|
173 | case HR_BD_SYNC:
|
---|
174 | for (i = 0; i < vol->dev_no; i++) {
|
---|
175 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
176 | continue;
|
---|
177 | rc = block_sync_cache(vol->extents[i].svc_id, ba, cnt);
|
---|
178 | if (rc != EOK && rc != ENOTSUP)
|
---|
179 | handle_extent_error(vol, i, rc);
|
---|
180 | else
|
---|
181 | successful++;
|
---|
182 | }
|
---|
183 | break;
|
---|
184 | case HR_BD_READ:
|
---|
185 | for (i = 0; i < vol->dev_no; i++) {
|
---|
186 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
187 | continue;
|
---|
188 | rc = block_read_direct(vol->extents[i].svc_id, ba, cnt,
|
---|
189 | data_read);
|
---|
190 | if (rc != EOK)
|
---|
191 | handle_extent_error(vol, i, rc);
|
---|
192 | else
|
---|
193 | successful++;
|
---|
194 | }
|
---|
195 | break;
|
---|
196 | case HR_BD_WRITE:
|
---|
197 | for (i = 0; i < vol->dev_no; i++) {
|
---|
198 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
199 | continue;
|
---|
200 | rc = block_write_direct(vol->extents[i].svc_id, ba, cnt,
|
---|
201 | data_write);
|
---|
202 | if (rc != EOK)
|
---|
203 | handle_extent_error(vol, i, rc);
|
---|
204 | else
|
---|
205 | successful++;
|
---|
206 | }
|
---|
207 | break;
|
---|
208 | default:
|
---|
209 | rc = EINVAL;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (successful > 0)
|
---|
213 | rc = EOK;
|
---|
214 | else
|
---|
215 | rc = EIO;
|
---|
216 |
|
---|
217 | (void) hr_raid1_update_vol_status(vol);
|
---|
218 | fibril_mutex_unlock(&vol->lock);
|
---|
219 | return rc;
|
---|
220 | }
|
---|
221 |
|
---|
222 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
|
---|
223 | {
|
---|
224 | return hr_raid1_bd_op(HR_BD_SYNC, bd, ba, cnt, NULL, NULL, 0);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
228 | void *buf, size_t size)
|
---|
229 | {
|
---|
230 | return hr_raid1_bd_op(HR_BD_READ, bd, ba, cnt, buf, NULL, size);
|
---|
231 | }
|
---|
232 |
|
---|
233 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
234 | const void *data, size_t size)
|
---|
235 | {
|
---|
236 | return hr_raid1_bd_op(HR_BD_WRITE, bd, ba, cnt, NULL, data, size);
|
---|
237 | }
|
---|
238 |
|
---|
239 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
240 | {
|
---|
241 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
242 |
|
---|
243 | *rsize = vol->bsize;
|
---|
244 | return EOK;
|
---|
245 | }
|
---|
246 |
|
---|
247 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
248 | {
|
---|
249 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
250 |
|
---|
251 | *rnb = vol->data_blkno;
|
---|
252 | return EOK;
|
---|
253 | }
|
---|
254 |
|
---|
255 | errno_t hr_raid1_create(hr_volume_t *new_volume)
|
---|
256 | {
|
---|
257 | errno_t rc;
|
---|
258 |
|
---|
259 | assert(new_volume->level == HR_LVL_1);
|
---|
260 |
|
---|
261 | if (new_volume->dev_no < 2) {
|
---|
262 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
263 | "RAID 1 array needs at least 2 devices");
|
---|
264 | return EINVAL;
|
---|
265 | }
|
---|
266 |
|
---|
267 | rc = hr_raid1_update_vol_status(new_volume);
|
---|
268 | if (rc != EOK)
|
---|
269 | return rc;
|
---|
270 |
|
---|
271 | bd_srvs_init(&new_volume->hr_bds);
|
---|
272 | new_volume->hr_bds.ops = &hr_raid1_bd_ops;
|
---|
273 | new_volume->hr_bds.sarg = new_volume;
|
---|
274 |
|
---|
275 | rc = hr_register_volume(new_volume);
|
---|
276 |
|
---|
277 | return rc;
|
---|
278 | }
|
---|
279 |
|
---|
280 | errno_t hr_raid1_init(hr_volume_t *vol)
|
---|
281 | {
|
---|
282 | errno_t rc;
|
---|
283 | size_t bsize;
|
---|
284 | uint64_t total_blkno;
|
---|
285 |
|
---|
286 | assert(vol->level == HR_LVL_1);
|
---|
287 |
|
---|
288 | rc = hr_check_devs(vol, &total_blkno, &bsize);
|
---|
289 | if (rc != EOK)
|
---|
290 | return rc;
|
---|
291 |
|
---|
292 | vol->nblocks = total_blkno / vol->dev_no;
|
---|
293 | vol->bsize = bsize;
|
---|
294 | vol->data_offset = HR_DATA_OFF;
|
---|
295 | vol->data_blkno = vol->nblocks - vol->data_offset;
|
---|
296 | vol->strip_size = 0;
|
---|
297 |
|
---|
298 | return EOK;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /** @}
|
---|
302 | */
|
---|