source: mainline/uspace/srv/bd/hr/raid1.c@ 06f2762

Last change on this file since 06f2762 was 7b359f5, checked in by Miroslav Cimerman <mc@…>, 9 months ago

hr: status/state event function for each RAID

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