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 <adt/list.h>
|
---|
37 | #include <block.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <hr.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <str_error.h>
|
---|
46 |
|
---|
47 | #include "util.h"
|
---|
48 | #include "var.h"
|
---|
49 |
|
---|
50 | #define HR_RL_LIST_LOCK(vol) (fibril_mutex_lock(&vol->range_lock_list_lock))
|
---|
51 | #define HR_RL_LIST_UNLOCK(vol) \
|
---|
52 | (fibril_mutex_unlock(&vol->range_lock_list_lock))
|
---|
53 |
|
---|
54 | static bool hr_range_lock_overlap(hr_range_lock_t *, hr_range_lock_t *);
|
---|
55 |
|
---|
56 | extern loc_srv_t *hr_srv;
|
---|
57 |
|
---|
58 | errno_t hr_init_devs(hr_volume_t *vol)
|
---|
59 | {
|
---|
60 | HR_DEBUG("hr_init_devs()\n");
|
---|
61 |
|
---|
62 | errno_t rc;
|
---|
63 | size_t i;
|
---|
64 | hr_extent_t *extent;
|
---|
65 |
|
---|
66 | for (i = 0; i < vol->extent_no; i++) {
|
---|
67 | extent = &vol->extents[i];
|
---|
68 | if (extent->svc_id == 0) {
|
---|
69 | extent->status = HR_EXT_MISSING;
|
---|
70 | continue;
|
---|
71 | }
|
---|
72 |
|
---|
73 | HR_DEBUG("hr_init_devs(): block_init() on (%lu)\n",
|
---|
74 | extent->svc_id);
|
---|
75 | rc = block_init(extent->svc_id);
|
---|
76 | extent->status = HR_EXT_ONLINE;
|
---|
77 |
|
---|
78 | if (rc != EOK) {
|
---|
79 | HR_ERROR("hr_init_devs(): initing (%lu) failed, "
|
---|
80 | "aborting\n", extent->svc_id);
|
---|
81 | break;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | return rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | void hr_fini_devs(hr_volume_t *vol)
|
---|
89 | {
|
---|
90 | HR_DEBUG("hr_fini_devs()\n");
|
---|
91 |
|
---|
92 | size_t i;
|
---|
93 |
|
---|
94 | for (i = 0; i < vol->extent_no; i++) {
|
---|
95 | if (vol->extents[i].status != HR_EXT_MISSING) {
|
---|
96 | HR_DEBUG("hr_fini_devs(): block_fini() on (%lu)\n",
|
---|
97 | vol->extents[i].svc_id);
|
---|
98 | block_fini(vol->extents[i].svc_id);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | errno_t hr_register_volume(hr_volume_t *vol)
|
---|
104 | {
|
---|
105 | HR_DEBUG("hr_register_volume()\n");
|
---|
106 |
|
---|
107 | errno_t rc;
|
---|
108 | service_id_t new_id;
|
---|
109 | category_id_t cat_id;
|
---|
110 | char *fullname = NULL;
|
---|
111 | char *devname = vol->devname;
|
---|
112 |
|
---|
113 | if (asprintf(&fullname, "devices/%s", devname) < 0)
|
---|
114 | return ENOMEM;
|
---|
115 |
|
---|
116 | rc = loc_service_register(hr_srv, fullname, &new_id);
|
---|
117 | if (rc != EOK) {
|
---|
118 | HR_ERROR("unable to register device \"%s\": %s\n",
|
---|
119 | fullname, str_error(rc));
|
---|
120 | goto error;
|
---|
121 | }
|
---|
122 |
|
---|
123 | rc = loc_category_get_id("raid", &cat_id, IPC_FLAG_BLOCKING);
|
---|
124 | if (rc != EOK) {
|
---|
125 | HR_ERROR("failed resolving category \"raid\": %s\n",
|
---|
126 | str_error(rc));
|
---|
127 | goto error;
|
---|
128 | }
|
---|
129 |
|
---|
130 | rc = loc_service_add_to_cat(hr_srv, new_id, cat_id);
|
---|
131 | if (rc != EOK) {
|
---|
132 | HR_ERROR("failed adding \"%s\" to category \"raid\": %s\n",
|
---|
133 | fullname, str_error(rc));
|
---|
134 | goto error;
|
---|
135 | }
|
---|
136 |
|
---|
137 | vol->svc_id = new_id;
|
---|
138 | error:
|
---|
139 | free(fullname);
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | errno_t hr_check_devs(hr_volume_t *vol, uint64_t *rblkno, size_t *rbsize)
|
---|
144 | {
|
---|
145 | HR_DEBUG("hr_check_devs()\n");
|
---|
146 |
|
---|
147 | errno_t rc;
|
---|
148 | size_t i, bsize;
|
---|
149 | uint64_t nblocks;
|
---|
150 | size_t last_bsize = 0;
|
---|
151 | uint64_t last_nblocks = 0;
|
---|
152 | uint64_t total_blocks = 0;
|
---|
153 | hr_extent_t *extent;
|
---|
154 |
|
---|
155 | for (i = 0; i < vol->extent_no; i++) {
|
---|
156 | extent = &vol->extents[i];
|
---|
157 | if (extent->status == HR_EXT_MISSING)
|
---|
158 | continue;
|
---|
159 | rc = block_get_nblocks(extent->svc_id, &nblocks);
|
---|
160 | if (rc != EOK)
|
---|
161 | goto error;
|
---|
162 | if (last_nblocks != 0 && nblocks != last_nblocks) {
|
---|
163 | HR_ERROR("number of blocks differs\n");
|
---|
164 | rc = EINVAL;
|
---|
165 | goto error;
|
---|
166 | }
|
---|
167 |
|
---|
168 | total_blocks += nblocks;
|
---|
169 | last_nblocks = nblocks;
|
---|
170 | }
|
---|
171 |
|
---|
172 | for (i = 0; i < vol->extent_no; i++) {
|
---|
173 | extent = &vol->extents[i];
|
---|
174 | if (extent->status == HR_EXT_MISSING)
|
---|
175 | continue;
|
---|
176 | rc = block_get_bsize(extent->svc_id, &bsize);
|
---|
177 | if (rc != EOK)
|
---|
178 | goto error;
|
---|
179 | if (last_bsize != 0 && bsize != last_bsize) {
|
---|
180 | HR_ERROR("block sizes differ\n");
|
---|
181 | rc = EINVAL;
|
---|
182 | goto error;
|
---|
183 | }
|
---|
184 |
|
---|
185 | last_bsize = bsize;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if ((bsize % 512) != 0) {
|
---|
189 | HR_ERROR("block size not multiple of 512\n");
|
---|
190 | return EINVAL;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (rblkno != NULL)
|
---|
194 | *rblkno = total_blocks;
|
---|
195 | if (rbsize != NULL)
|
---|
196 | *rbsize = bsize;
|
---|
197 | error:
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 |
|
---|
201 | errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
|
---|
202 | {
|
---|
203 | if (ba + cnt > vol->data_blkno)
|
---|
204 | return ERANGE;
|
---|
205 | return EOK;
|
---|
206 | }
|
---|
207 |
|
---|
208 | void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
|
---|
209 | {
|
---|
210 | *ba = *ba + vol->data_offset;
|
---|
211 | }
|
---|
212 |
|
---|
213 | void hr_update_ext_status(hr_volume_t *vol, size_t extent, hr_ext_status_t s)
|
---|
214 | {
|
---|
215 | hr_ext_status_t old = vol->extents[extent].status;
|
---|
216 | HR_WARN("\"%s\": changing extent %lu state: %s -> %s\n",
|
---|
217 | vol->devname, extent, hr_get_ext_status_msg(old),
|
---|
218 | hr_get_ext_status_msg(s));
|
---|
219 | vol->extents[extent].status = s;
|
---|
220 | }
|
---|
221 |
|
---|
222 | void hr_update_hotspare_status(hr_volume_t *vol, size_t hs, hr_ext_status_t s)
|
---|
223 | {
|
---|
224 | hr_ext_status_t old = vol->hotspares[hs].status;
|
---|
225 | HR_WARN("\"%s\": changing hotspare %lu state: %s -> %s\n",
|
---|
226 | vol->devname, hs, hr_get_ext_status_msg(old),
|
---|
227 | hr_get_ext_status_msg(s));
|
---|
228 | vol->hotspares[hs].status = s;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void hr_update_vol_status(hr_volume_t *vol, hr_vol_status_t s)
|
---|
232 | {
|
---|
233 | HR_WARN("\"%s\": changing volume state: %s -> %s\n", vol->devname,
|
---|
234 | hr_get_vol_status_msg(vol->status), hr_get_vol_status_msg(s));
|
---|
235 | vol->status = s;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Do a whole sync (ba = 0, cnt = 0) across all extents,
|
---|
240 | * and update extent status. *For now*, the caller has to
|
---|
241 | * update volume status after the syncs.
|
---|
242 | *
|
---|
243 | * TODO: add update_vol_status fcn ptr for each raid
|
---|
244 | */
|
---|
245 | void hr_sync_all_extents(hr_volume_t *vol)
|
---|
246 | {
|
---|
247 | errno_t rc;
|
---|
248 |
|
---|
249 | fibril_mutex_lock(&vol->lock);
|
---|
250 | for (size_t i = 0; i < vol->extent_no; i++) {
|
---|
251 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
252 | continue;
|
---|
253 | rc = block_sync_cache(vol->extents[i].svc_id, 0, 0);
|
---|
254 | if (rc != EOK && rc != ENOTSUP) {
|
---|
255 | if (rc == ENOENT)
|
---|
256 | hr_update_ext_status(vol, i, HR_EXT_MISSING);
|
---|
257 | else if (rc != EOK)
|
---|
258 | hr_update_ext_status(vol, i, HR_EXT_FAILED);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | fibril_mutex_unlock(&vol->lock);
|
---|
262 | }
|
---|
263 |
|
---|
264 | size_t hr_count_extents(hr_volume_t *vol, hr_ext_status_t status)
|
---|
265 | {
|
---|
266 | size_t count = 0;
|
---|
267 | for (size_t i = 0; i < vol->extent_no; i++)
|
---|
268 | if (vol->extents[i].status == status)
|
---|
269 | count++;
|
---|
270 |
|
---|
271 | return count;
|
---|
272 | }
|
---|
273 |
|
---|
274 | hr_range_lock_t *hr_range_lock_acquire(hr_volume_t *vol, uint64_t ba,
|
---|
275 | uint64_t cnt)
|
---|
276 | {
|
---|
277 | hr_range_lock_t *rl = malloc(sizeof(hr_range_lock_t));
|
---|
278 | if (rl == NULL)
|
---|
279 | return NULL;
|
---|
280 |
|
---|
281 | rl->vol = vol;
|
---|
282 | rl->off = ba;
|
---|
283 | rl->len = cnt;
|
---|
284 |
|
---|
285 | rl->pending = 1;
|
---|
286 | rl->ignore = false;
|
---|
287 |
|
---|
288 | link_initialize(&rl->link);
|
---|
289 | fibril_mutex_initialize(&rl->lock);
|
---|
290 |
|
---|
291 | fibril_mutex_lock(&rl->lock);
|
---|
292 |
|
---|
293 | again:
|
---|
294 | HR_RL_LIST_LOCK(vol);
|
---|
295 | list_foreach(vol->range_lock_list, link, hr_range_lock_t, rlp) {
|
---|
296 | if (rlp->ignore)
|
---|
297 | continue;
|
---|
298 | if (hr_range_lock_overlap(rlp, rl)) {
|
---|
299 | rlp->pending++;
|
---|
300 |
|
---|
301 | HR_RL_LIST_UNLOCK(vol);
|
---|
302 |
|
---|
303 | fibril_mutex_lock(&rlp->lock);
|
---|
304 |
|
---|
305 | HR_RL_LIST_LOCK(vol);
|
---|
306 |
|
---|
307 | rlp->pending--;
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * when ignore is set, after HR_RL_LIST_UNLOCK(),
|
---|
311 | * noone new is going to be able to start sleeping
|
---|
312 | * on the ignored range lock, only already waiting
|
---|
313 | * IOs will come through here
|
---|
314 | */
|
---|
315 | rlp->ignore = true;
|
---|
316 |
|
---|
317 | fibril_mutex_unlock(&rlp->lock);
|
---|
318 |
|
---|
319 | if (rlp->pending == 0) {
|
---|
320 | list_remove(&rlp->link);
|
---|
321 | free(rlp);
|
---|
322 | }
|
---|
323 |
|
---|
324 | HR_RL_LIST_UNLOCK(vol);
|
---|
325 | goto again;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | list_append(&rl->link, &vol->range_lock_list);
|
---|
330 |
|
---|
331 | HR_RL_LIST_UNLOCK(vol);
|
---|
332 | return rl;
|
---|
333 | }
|
---|
334 |
|
---|
335 | void hr_range_lock_release(hr_range_lock_t *rl)
|
---|
336 | {
|
---|
337 | HR_RL_LIST_LOCK(rl->vol);
|
---|
338 |
|
---|
339 | rl->pending--;
|
---|
340 |
|
---|
341 | fibril_mutex_unlock(&rl->lock);
|
---|
342 |
|
---|
343 | if (rl->pending == 0) {
|
---|
344 | list_remove(&rl->link);
|
---|
345 | free(rl);
|
---|
346 | }
|
---|
347 |
|
---|
348 | HR_RL_LIST_UNLOCK(rl->vol);
|
---|
349 | }
|
---|
350 |
|
---|
351 | static bool hr_range_lock_overlap(hr_range_lock_t *rl1, hr_range_lock_t *rl2)
|
---|
352 | {
|
---|
353 | uint64_t rl1_start = rl1->off;
|
---|
354 | uint64_t rl1_end = rl1->off + rl1->len - 1;
|
---|
355 | uint64_t rl2_start = rl2->off;
|
---|
356 | uint64_t rl2_end = rl2->off + rl2->len - 1;
|
---|
357 |
|
---|
358 | /* one ends before the other starts */
|
---|
359 | if (rl1_end < rl2_start || rl2_end < rl1_start)
|
---|
360 | return false;
|
---|
361 |
|
---|
362 | return true;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /** @}
|
---|
366 | */
|
---|