1 | /*
|
---|
2 | * Copyright (c) 2025 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 <mem.h>
|
---|
44 | #include <stdatomic.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <str_error.h>
|
---|
48 | #include <vbd.h>
|
---|
49 |
|
---|
50 | #include "io.h"
|
---|
51 | #include "superblock.h"
|
---|
52 | #include "util.h"
|
---|
53 | #include "var.h"
|
---|
54 |
|
---|
55 | #define HR_RL_LIST_LOCK(vol) (fibril_mutex_lock(&vol->range_lock_list_lock))
|
---|
56 | #define HR_RL_LIST_UNLOCK(vol) \
|
---|
57 | (fibril_mutex_unlock(&vol->range_lock_list_lock))
|
---|
58 |
|
---|
59 | static bool hr_range_lock_overlap(hr_range_lock_t *, hr_range_lock_t *);
|
---|
60 |
|
---|
61 | extern loc_srv_t *hr_srv;
|
---|
62 | extern list_t hr_volumes;
|
---|
63 | extern fibril_rwlock_t hr_volumes_lock;
|
---|
64 |
|
---|
65 | errno_t hr_create_vol_struct(hr_volume_t **rvol, hr_level_t level)
|
---|
66 | {
|
---|
67 | errno_t rc;
|
---|
68 |
|
---|
69 | hr_volume_t *vol = calloc(1, sizeof(hr_volume_t));
|
---|
70 | if (vol == NULL)
|
---|
71 | return ENOMEM;
|
---|
72 |
|
---|
73 | vol->level = level;
|
---|
74 |
|
---|
75 | switch (level) {
|
---|
76 | case HR_LVL_1:
|
---|
77 | vol->hr_ops.create = hr_raid1_create;
|
---|
78 | vol->hr_ops.init = hr_raid1_init;
|
---|
79 | vol->hr_ops.status_event = hr_raid1_status_event;
|
---|
80 | vol->hr_ops.add_hotspare = hr_raid1_add_hotspare;
|
---|
81 | break;
|
---|
82 | case HR_LVL_0:
|
---|
83 | vol->hr_ops.create = hr_raid0_create;
|
---|
84 | vol->hr_ops.init = hr_raid0_init;
|
---|
85 | vol->hr_ops.status_event = hr_raid0_status_event;
|
---|
86 | break;
|
---|
87 | case HR_LVL_4:
|
---|
88 | vol->hr_ops.create = hr_raid5_create;
|
---|
89 | vol->hr_ops.init = hr_raid5_init;
|
---|
90 | vol->hr_ops.status_event = hr_raid5_status_event;
|
---|
91 | vol->hr_ops.add_hotspare = hr_raid5_add_hotspare;
|
---|
92 | break;
|
---|
93 | case HR_LVL_5:
|
---|
94 | vol->hr_ops.create = hr_raid5_create;
|
---|
95 | vol->hr_ops.init = hr_raid5_init;
|
---|
96 | vol->hr_ops.status_event = hr_raid5_status_event;
|
---|
97 | vol->hr_ops.add_hotspare = hr_raid5_add_hotspare;
|
---|
98 | break;
|
---|
99 | default:
|
---|
100 | HR_DEBUG("unkown level: %d, aborting\n", vol->level);
|
---|
101 | rc = EINVAL;
|
---|
102 | goto error;
|
---|
103 | }
|
---|
104 |
|
---|
105 | vol->fge = hr_fpool_create(16, 32, sizeof(hr_io_t));
|
---|
106 | if (vol->fge == NULL) {
|
---|
107 | rc = ENOMEM;
|
---|
108 | goto error;
|
---|
109 | }
|
---|
110 |
|
---|
111 | vol->status = HR_VOL_NONE;
|
---|
112 |
|
---|
113 | for (size_t i = 0; i < HR_MAX_EXTENTS; ++i)
|
---|
114 | vol->extents[i].status = HR_EXT_MISSING;
|
---|
115 |
|
---|
116 | for (size_t i = 0; i < HR_MAX_HOTSPARES; ++i)
|
---|
117 | vol->extents[i].status = HR_EXT_MISSING;
|
---|
118 |
|
---|
119 | fibril_mutex_initialize(&vol->lock); /* XXX: will remove this */
|
---|
120 |
|
---|
121 | fibril_rwlock_initialize(&vol->extents_lock);
|
---|
122 | fibril_rwlock_initialize(&vol->states_lock);
|
---|
123 |
|
---|
124 | fibril_mutex_initialize(&vol->hotspare_lock);
|
---|
125 |
|
---|
126 | list_initialize(&vol->range_lock_list);
|
---|
127 | fibril_mutex_initialize(&vol->range_lock_list_lock);
|
---|
128 |
|
---|
129 | atomic_init(&vol->rebuild_blk, 0);
|
---|
130 | atomic_init(&vol->state_dirty, false);
|
---|
131 | atomic_init(&vol->open_cnt, 0);
|
---|
132 |
|
---|
133 | *rvol = vol;
|
---|
134 |
|
---|
135 | return EOK;
|
---|
136 | error:
|
---|
137 | free(vol);
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | void hr_destroy_vol_struct(hr_volume_t *vol)
|
---|
142 | {
|
---|
143 | if (vol == NULL)
|
---|
144 | return;
|
---|
145 |
|
---|
146 | hr_fpool_destroy(vol->fge);
|
---|
147 | hr_fini_devs(vol);
|
---|
148 | free(vol->in_mem_md);
|
---|
149 | free(vol);
|
---|
150 | }
|
---|
151 |
|
---|
152 | hr_volume_t *hr_get_volume(service_id_t svc_id)
|
---|
153 | {
|
---|
154 | HR_DEBUG("hr_get_volume(): (%" PRIun ")\n", svc_id);
|
---|
155 |
|
---|
156 | hr_volume_t *rvol = NULL;
|
---|
157 |
|
---|
158 | fibril_rwlock_read_lock(&hr_volumes_lock);
|
---|
159 | list_foreach(hr_volumes, lvolumes, hr_volume_t, iter) {
|
---|
160 | if (iter->svc_id == svc_id) {
|
---|
161 | rvol = iter;
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | fibril_rwlock_read_unlock(&hr_volumes_lock);
|
---|
167 | return rvol;
|
---|
168 | }
|
---|
169 |
|
---|
170 | errno_t hr_remove_volume(service_id_t svc_id)
|
---|
171 | {
|
---|
172 | HR_DEBUG("hr_remove_volume(): (%" PRIun ")\n", svc_id);
|
---|
173 |
|
---|
174 | errno_t rc;
|
---|
175 |
|
---|
176 | fibril_rwlock_write_lock(&hr_volumes_lock);
|
---|
177 | list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
|
---|
178 | if (vol->svc_id == svc_id) {
|
---|
179 | int open_cnt = atomic_load_explicit(&vol->open_cnt,
|
---|
180 | memory_order_relaxed);
|
---|
181 | /*
|
---|
182 | * The "atomicity" of this if condition is provided
|
---|
183 | * by the write lock - no new bd connection can
|
---|
184 | * come, because we need to get the bd_srvs_t from
|
---|
185 | * volume, which we get from the list.
|
---|
186 | * (see hr_client_conn() in hr.c)
|
---|
187 | */
|
---|
188 | if (open_cnt > 0) {
|
---|
189 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
190 | return EBUSY;
|
---|
191 | }
|
---|
192 | list_remove(&vol->lvolumes);
|
---|
193 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
194 |
|
---|
195 | hr_destroy_vol_struct(vol);
|
---|
196 |
|
---|
197 | rc = loc_service_unregister(hr_srv, svc_id);
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
203 | return ENOENT;
|
---|
204 | }
|
---|
205 |
|
---|
206 | errno_t hr_init_devs(hr_volume_t *vol)
|
---|
207 | {
|
---|
208 | HR_DEBUG("%s()", __func__);
|
---|
209 |
|
---|
210 | errno_t rc;
|
---|
211 | size_t i;
|
---|
212 | hr_extent_t *extent;
|
---|
213 |
|
---|
214 | for (i = 0; i < vol->extent_no; i++) {
|
---|
215 | extent = &vol->extents[i];
|
---|
216 | if (extent->svc_id == 0) {
|
---|
217 | extent->status = HR_EXT_MISSING;
|
---|
218 | continue;
|
---|
219 | }
|
---|
220 |
|
---|
221 | HR_DEBUG("hr_init_devs(): block_init() on (%lu)\n",
|
---|
222 | extent->svc_id);
|
---|
223 | rc = block_init(extent->svc_id);
|
---|
224 | extent->status = HR_EXT_ONLINE;
|
---|
225 |
|
---|
226 | if (rc != EOK) {
|
---|
227 | HR_ERROR("hr_init_devs(): initing (%lu) failed, "
|
---|
228 | "aborting\n", extent->svc_id);
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | for (i = 0; i < HR_MAX_HOTSPARES; i++)
|
---|
234 | vol->hotspares[i].status = HR_EXT_MISSING;
|
---|
235 |
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 | void hr_fini_devs(hr_volume_t *vol)
|
---|
240 | {
|
---|
241 | HR_DEBUG("%s()", __func__);
|
---|
242 |
|
---|
243 | size_t i;
|
---|
244 |
|
---|
245 | for (i = 0; i < vol->extent_no; i++) {
|
---|
246 | if (vol->extents[i].svc_id != 0) {
|
---|
247 | HR_DEBUG("hr_fini_devs(): block_fini() on (%lu)\n",
|
---|
248 | vol->extents[i].svc_id);
|
---|
249 | block_fini(vol->extents[i].svc_id);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | for (i = 0; i < vol->hotspare_no; i++) {
|
---|
254 | if (vol->hotspares[i].svc_id != 0) {
|
---|
255 | HR_DEBUG("hr_fini_devs(): block_fini() on (%lu)\n",
|
---|
256 | vol->hotspares[i].svc_id);
|
---|
257 | block_fini(vol->hotspares[i].svc_id);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | errno_t hr_register_volume(hr_volume_t *vol)
|
---|
263 | {
|
---|
264 | HR_DEBUG("%s()", __func__);
|
---|
265 |
|
---|
266 | errno_t rc;
|
---|
267 | service_id_t new_id;
|
---|
268 | category_id_t cat_id;
|
---|
269 | char *fullname = NULL;
|
---|
270 | char *devname = vol->devname;
|
---|
271 |
|
---|
272 | if (asprintf(&fullname, "devices/%s", devname) < 0)
|
---|
273 | return ENOMEM;
|
---|
274 |
|
---|
275 | rc = loc_service_register(hr_srv, fullname, &new_id);
|
---|
276 | if (rc != EOK) {
|
---|
277 | HR_ERROR("unable to register device \"%s\": %s\n",
|
---|
278 | fullname, str_error(rc));
|
---|
279 | goto error;
|
---|
280 | }
|
---|
281 |
|
---|
282 | rc = loc_category_get_id("raid", &cat_id, IPC_FLAG_BLOCKING);
|
---|
283 | if (rc != EOK) {
|
---|
284 | HR_ERROR("failed resolving category \"raid\": %s\n",
|
---|
285 | str_error(rc));
|
---|
286 | goto error;
|
---|
287 | }
|
---|
288 |
|
---|
289 | rc = loc_service_add_to_cat(hr_srv, new_id, cat_id);
|
---|
290 | if (rc != EOK) {
|
---|
291 | HR_ERROR("failed adding \"%s\" to category \"raid\": %s\n",
|
---|
292 | fullname, str_error(rc));
|
---|
293 | goto error;
|
---|
294 | }
|
---|
295 |
|
---|
296 | vol->svc_id = new_id;
|
---|
297 | error:
|
---|
298 | free(fullname);
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | errno_t hr_check_devs(hr_volume_t *vol, uint64_t *rblkno, size_t *rbsize)
|
---|
303 | {
|
---|
304 | HR_DEBUG("%s()", __func__);
|
---|
305 |
|
---|
306 | errno_t rc;
|
---|
307 | size_t i, bsize;
|
---|
308 | uint64_t nblocks;
|
---|
309 | size_t last_bsize = 0;
|
---|
310 | uint64_t last_nblocks = 0;
|
---|
311 | uint64_t total_blocks = 0;
|
---|
312 | hr_extent_t *extent;
|
---|
313 |
|
---|
314 | for (i = 0; i < vol->extent_no; i++) {
|
---|
315 | extent = &vol->extents[i];
|
---|
316 | if (extent->status == HR_EXT_MISSING)
|
---|
317 | continue;
|
---|
318 | rc = block_get_nblocks(extent->svc_id, &nblocks);
|
---|
319 | if (rc != EOK)
|
---|
320 | goto error;
|
---|
321 | if (last_nblocks != 0 && nblocks != last_nblocks) {
|
---|
322 | HR_ERROR("number of blocks differs\n");
|
---|
323 | rc = EINVAL;
|
---|
324 | goto error;
|
---|
325 | }
|
---|
326 |
|
---|
327 | total_blocks += nblocks;
|
---|
328 | last_nblocks = nblocks;
|
---|
329 | }
|
---|
330 |
|
---|
331 | for (i = 0; i < vol->extent_no; i++) {
|
---|
332 | extent = &vol->extents[i];
|
---|
333 | if (extent->status == HR_EXT_MISSING)
|
---|
334 | continue;
|
---|
335 | rc = block_get_bsize(extent->svc_id, &bsize);
|
---|
336 | if (rc != EOK)
|
---|
337 | goto error;
|
---|
338 | if (last_bsize != 0 && bsize != last_bsize) {
|
---|
339 | HR_ERROR("block sizes differ\n");
|
---|
340 | rc = EINVAL;
|
---|
341 | goto error;
|
---|
342 | }
|
---|
343 |
|
---|
344 | last_bsize = bsize;
|
---|
345 | }
|
---|
346 |
|
---|
347 | if ((bsize % 512) != 0) {
|
---|
348 | HR_ERROR("block size not multiple of 512\n");
|
---|
349 | return EINVAL;
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (rblkno != NULL)
|
---|
353 | *rblkno = total_blocks;
|
---|
354 | if (rbsize != NULL)
|
---|
355 | *rbsize = bsize;
|
---|
356 | error:
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 |
|
---|
360 | errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
|
---|
361 | {
|
---|
362 | if (ba + cnt > vol->data_blkno)
|
---|
363 | return ERANGE;
|
---|
364 | return EOK;
|
---|
365 | }
|
---|
366 |
|
---|
367 | void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
|
---|
368 | {
|
---|
369 | *ba = *ba + vol->data_offset;
|
---|
370 | }
|
---|
371 |
|
---|
372 | void hr_update_ext_status(hr_volume_t *vol, size_t extent, hr_ext_status_t s)
|
---|
373 | {
|
---|
374 | if (vol->level != HR_LVL_0)
|
---|
375 | assert(fibril_rwlock_is_locked(&vol->extents_lock));
|
---|
376 |
|
---|
377 | assert(fibril_rwlock_is_write_locked(&vol->states_lock));
|
---|
378 |
|
---|
379 | assert(extent < vol->extent_no);
|
---|
380 |
|
---|
381 | hr_ext_status_t old = vol->extents[extent].status;
|
---|
382 | HR_WARN("\"%s\": changing extent %lu state: %s -> %s\n",
|
---|
383 | vol->devname, extent, hr_get_ext_status_msg(old),
|
---|
384 | hr_get_ext_status_msg(s));
|
---|
385 | vol->extents[extent].status = s;
|
---|
386 | }
|
---|
387 |
|
---|
388 | void hr_update_hotspare_status(hr_volume_t *vol, size_t hs, hr_ext_status_t s)
|
---|
389 | {
|
---|
390 | assert(fibril_mutex_is_locked(&vol->hotspare_lock));
|
---|
391 |
|
---|
392 | assert(hs < vol->hotspare_no);
|
---|
393 |
|
---|
394 | hr_ext_status_t old = vol->hotspares[hs].status;
|
---|
395 | HR_WARN("\"%s\": changing hotspare %lu state: %s -> %s\n",
|
---|
396 | vol->devname, hs, hr_get_ext_status_msg(old),
|
---|
397 | hr_get_ext_status_msg(s));
|
---|
398 | vol->hotspares[hs].status = s;
|
---|
399 | }
|
---|
400 |
|
---|
401 | void hr_update_vol_status(hr_volume_t *vol, hr_vol_status_t new)
|
---|
402 | {
|
---|
403 | assert(fibril_rwlock_is_write_locked(&vol->states_lock));
|
---|
404 |
|
---|
405 | HR_WARN("\"%s\": changing volume state: %s -> %s\n", vol->devname,
|
---|
406 | hr_get_vol_status_msg(vol->status), hr_get_vol_status_msg(new));
|
---|
407 | vol->status = new;
|
---|
408 | }
|
---|
409 |
|
---|
410 | void hr_update_ext_svc_id(hr_volume_t *vol, size_t extent, service_id_t new)
|
---|
411 | {
|
---|
412 | if (vol->level != HR_LVL_0)
|
---|
413 | assert(fibril_rwlock_is_write_locked(&vol->extents_lock));
|
---|
414 |
|
---|
415 | assert(extent < vol->extent_no);
|
---|
416 |
|
---|
417 | service_id_t old = vol->extents[extent].svc_id;
|
---|
418 | HR_WARN("\"%s\": changing extent no. %lu svc_id: (%lu) -> (%lu)\n",
|
---|
419 | vol->devname, extent, old, new);
|
---|
420 | vol->extents[extent].svc_id = new;
|
---|
421 | }
|
---|
422 |
|
---|
423 | void hr_update_hotspare_svc_id(hr_volume_t *vol, size_t hs, service_id_t new)
|
---|
424 | {
|
---|
425 | assert(fibril_mutex_is_locked(&vol->hotspare_lock));
|
---|
426 |
|
---|
427 | assert(hs < vol->hotspare_no);
|
---|
428 |
|
---|
429 | service_id_t old = vol->hotspares[hs].svc_id;
|
---|
430 | HR_WARN("\"%s\": changing hotspare no. %lu svc_id: (%lu) -> (%lu)\n",
|
---|
431 | vol->devname, hs, old, new);
|
---|
432 | vol->hotspares[hs].svc_id = new;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * Do a whole sync (ba = 0, cnt = 0) across all extents,
|
---|
437 | * and update extent status. *For now*, the caller has to
|
---|
438 | * update volume status after the syncs.
|
---|
439 | *
|
---|
440 | * TODO: add update_vol_status fcn ptr for each raid
|
---|
441 | */
|
---|
442 | void hr_sync_all_extents(hr_volume_t *vol)
|
---|
443 | {
|
---|
444 | errno_t rc;
|
---|
445 |
|
---|
446 | fibril_mutex_lock(&vol->lock);
|
---|
447 | for (size_t i = 0; i < vol->extent_no; i++) {
|
---|
448 | if (vol->extents[i].status != HR_EXT_ONLINE)
|
---|
449 | continue;
|
---|
450 | rc = block_sync_cache(vol->extents[i].svc_id, 0, 0);
|
---|
451 | if (rc == ENOMEM || rc == ENOTSUP)
|
---|
452 | continue;
|
---|
453 | if (rc != EOK) {
|
---|
454 | if (rc == ENOENT)
|
---|
455 | hr_update_ext_status(vol, i, HR_EXT_MISSING);
|
---|
456 | else if (rc != EOK)
|
---|
457 | hr_update_ext_status(vol, i, HR_EXT_FAILED);
|
---|
458 | }
|
---|
459 | }
|
---|
460 | fibril_mutex_unlock(&vol->lock);
|
---|
461 | }
|
---|
462 |
|
---|
463 | size_t hr_count_extents(hr_volume_t *vol, hr_ext_status_t status)
|
---|
464 | {
|
---|
465 | if (vol->level != HR_LVL_0)
|
---|
466 | assert(fibril_rwlock_is_locked(&vol->extents_lock));
|
---|
467 | assert(fibril_rwlock_is_locked(&vol->states_lock));
|
---|
468 |
|
---|
469 | size_t count = 0;
|
---|
470 | for (size_t i = 0; i < vol->extent_no; i++)
|
---|
471 | if (vol->extents[i].status == status)
|
---|
472 | count++;
|
---|
473 |
|
---|
474 | return count;
|
---|
475 | }
|
---|
476 |
|
---|
477 | hr_range_lock_t *hr_range_lock_acquire(hr_volume_t *vol, uint64_t ba,
|
---|
478 | uint64_t cnt)
|
---|
479 | {
|
---|
480 | hr_range_lock_t *rl = malloc(sizeof(hr_range_lock_t));
|
---|
481 | if (rl == NULL)
|
---|
482 | return NULL;
|
---|
483 |
|
---|
484 | rl->vol = vol;
|
---|
485 | rl->off = ba;
|
---|
486 | rl->len = cnt;
|
---|
487 |
|
---|
488 | rl->pending = 1;
|
---|
489 | rl->ignore = false;
|
---|
490 |
|
---|
491 | link_initialize(&rl->link);
|
---|
492 | fibril_mutex_initialize(&rl->lock);
|
---|
493 |
|
---|
494 | fibril_mutex_lock(&rl->lock);
|
---|
495 |
|
---|
496 | again:
|
---|
497 | HR_RL_LIST_LOCK(vol);
|
---|
498 | list_foreach(vol->range_lock_list, link, hr_range_lock_t, rlp) {
|
---|
499 | if (rlp->ignore)
|
---|
500 | continue;
|
---|
501 | if (hr_range_lock_overlap(rlp, rl)) {
|
---|
502 | rlp->pending++;
|
---|
503 |
|
---|
504 | HR_RL_LIST_UNLOCK(vol);
|
---|
505 |
|
---|
506 | fibril_mutex_lock(&rlp->lock);
|
---|
507 |
|
---|
508 | HR_RL_LIST_LOCK(vol);
|
---|
509 |
|
---|
510 | rlp->pending--;
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * when ignore is set, after HR_RL_LIST_UNLOCK(),
|
---|
514 | * noone new is going to be able to start sleeping
|
---|
515 | * on the ignored range lock, only already waiting
|
---|
516 | * IOs will come through here
|
---|
517 | */
|
---|
518 | rlp->ignore = true;
|
---|
519 |
|
---|
520 | fibril_mutex_unlock(&rlp->lock);
|
---|
521 |
|
---|
522 | if (rlp->pending == 0) {
|
---|
523 | list_remove(&rlp->link);
|
---|
524 | free(rlp);
|
---|
525 | }
|
---|
526 |
|
---|
527 | HR_RL_LIST_UNLOCK(vol);
|
---|
528 | goto again;
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 | list_append(&rl->link, &vol->range_lock_list);
|
---|
533 |
|
---|
534 | HR_RL_LIST_UNLOCK(vol);
|
---|
535 | return rl;
|
---|
536 | }
|
---|
537 |
|
---|
538 | void hr_range_lock_release(hr_range_lock_t *rl)
|
---|
539 | {
|
---|
540 | if (rl == NULL)
|
---|
541 | return;
|
---|
542 |
|
---|
543 | HR_RL_LIST_LOCK(rl->vol);
|
---|
544 |
|
---|
545 | rl->pending--;
|
---|
546 |
|
---|
547 | fibril_mutex_unlock(&rl->lock);
|
---|
548 |
|
---|
549 | if (rl->pending == 0) {
|
---|
550 | list_remove(&rl->link);
|
---|
551 | free(rl);
|
---|
552 | }
|
---|
553 |
|
---|
554 | HR_RL_LIST_UNLOCK(rl->vol);
|
---|
555 | }
|
---|
556 |
|
---|
557 | static bool hr_range_lock_overlap(hr_range_lock_t *rl1, hr_range_lock_t *rl2)
|
---|
558 | {
|
---|
559 | uint64_t rl1_start = rl1->off;
|
---|
560 | uint64_t rl1_end = rl1->off + rl1->len - 1;
|
---|
561 | uint64_t rl2_start = rl2->off;
|
---|
562 | uint64_t rl2_end = rl2->off + rl2->len - 1;
|
---|
563 |
|
---|
564 | /* one ends before the other starts */
|
---|
565 | if (rl1_end < rl2_start || rl2_end < rl1_start)
|
---|
566 | return false;
|
---|
567 |
|
---|
568 | return true;
|
---|
569 | }
|
---|
570 |
|
---|
571 | void hr_mark_vol_state_dirty(hr_volume_t *vol)
|
---|
572 | {
|
---|
573 | atomic_store(&vol->state_dirty, true);
|
---|
574 | }
|
---|
575 |
|
---|
576 | struct svc_id_linked {
|
---|
577 | link_t link;
|
---|
578 | service_id_t svc_id;
|
---|
579 | hr_metadata_t *md;
|
---|
580 | bool inited;
|
---|
581 | bool md_present;
|
---|
582 | };
|
---|
583 |
|
---|
584 | static errno_t hr_add_svc_linked_to_list(list_t *list, service_id_t svc_id,
|
---|
585 | bool inited, hr_metadata_t *md)
|
---|
586 | {
|
---|
587 | errno_t rc = EOK;
|
---|
588 | struct svc_id_linked *to_add;
|
---|
589 |
|
---|
590 | to_add = malloc(sizeof(struct svc_id_linked));
|
---|
591 | if (to_add == NULL) {
|
---|
592 | rc = ENOMEM;
|
---|
593 | goto error;
|
---|
594 | }
|
---|
595 | to_add->svc_id = svc_id;
|
---|
596 | to_add->inited = inited;
|
---|
597 |
|
---|
598 | if (md != NULL) {
|
---|
599 | to_add->md = malloc(sizeof(hr_metadata_t));
|
---|
600 | if (to_add->md == NULL) {
|
---|
601 | rc = ENOMEM;
|
---|
602 | goto error;
|
---|
603 | }
|
---|
604 | to_add->md_present = true;
|
---|
605 | memcpy(to_add->md, md, sizeof(*md));
|
---|
606 | } else {
|
---|
607 | to_add->md_present = false;
|
---|
608 | }
|
---|
609 |
|
---|
610 | list_append(&to_add->link, list);
|
---|
611 |
|
---|
612 | error:
|
---|
613 | return rc;
|
---|
614 | }
|
---|
615 |
|
---|
616 | static void free_svc_id_linked(struct svc_id_linked *p)
|
---|
617 | {
|
---|
618 | if (p->md_present)
|
---|
619 | free(p->md);
|
---|
620 | free(p);
|
---|
621 | }
|
---|
622 |
|
---|
623 | static void free_svc_id_list(list_t *list)
|
---|
624 | {
|
---|
625 | struct svc_id_linked *dev_id;
|
---|
626 | while (!list_empty(list)) {
|
---|
627 | dev_id = list_pop(list, struct svc_id_linked, link);
|
---|
628 | free_svc_id_linked(dev_id);
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | static errno_t hr_fill_disk_part_svcs_list(list_t *list)
|
---|
633 | {
|
---|
634 | errno_t rc;
|
---|
635 | size_t disk_count;
|
---|
636 | service_id_t *disk_svcs = NULL;
|
---|
637 | vbd_t *vbd = NULL;
|
---|
638 |
|
---|
639 | rc = vbd_create(&vbd);
|
---|
640 | if (rc != EOK)
|
---|
641 | goto error;
|
---|
642 |
|
---|
643 | rc = vbd_get_disks(vbd, &disk_svcs, &disk_count);
|
---|
644 | if (rc != EOK)
|
---|
645 | goto error;
|
---|
646 |
|
---|
647 | for (size_t i = 0; i < disk_count; i++) {
|
---|
648 | vbd_disk_info_t disk_info;
|
---|
649 | rc = vbd_disk_info(vbd, disk_svcs[i], &disk_info);
|
---|
650 | if (rc != EOK)
|
---|
651 | goto error;
|
---|
652 |
|
---|
653 | if (disk_info.ltype == lt_none) {
|
---|
654 | rc = hr_add_svc_linked_to_list(list, disk_svcs[i], false, NULL);
|
---|
655 | if (rc != EOK)
|
---|
656 | goto error;
|
---|
657 | } else {
|
---|
658 | size_t part_count;
|
---|
659 | service_id_t *part_ids = NULL;
|
---|
660 | rc = vbd_label_get_parts(vbd, disk_svcs[i], &part_ids, &part_count);
|
---|
661 | if (rc != EOK)
|
---|
662 | goto error;
|
---|
663 |
|
---|
664 | for (size_t j = 0; j < part_count; j++) {
|
---|
665 | vbd_part_info_t part_info;
|
---|
666 | rc = vbd_part_get_info(vbd, part_ids[j], &part_info);
|
---|
667 | if (rc != EOK) {
|
---|
668 | free(part_ids);
|
---|
669 | goto error;
|
---|
670 | }
|
---|
671 |
|
---|
672 | rc = hr_add_svc_linked_to_list(list,
|
---|
673 | part_info.svc_id, false, NULL);
|
---|
674 | if (rc != EOK) {
|
---|
675 | free(part_ids);
|
---|
676 | goto error;
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | free(part_ids);
|
---|
681 | }
|
---|
682 | }
|
---|
683 |
|
---|
684 | free(disk_svcs);
|
---|
685 | vbd_destroy(vbd);
|
---|
686 | return EOK;
|
---|
687 | error:
|
---|
688 | free_svc_id_list(list);
|
---|
689 | if (disk_svcs != NULL)
|
---|
690 | free(disk_svcs);
|
---|
691 | vbd_destroy(vbd);
|
---|
692 |
|
---|
693 | return rc;
|
---|
694 | }
|
---|
695 |
|
---|
696 | static errno_t block_init_dev_list(list_t *list)
|
---|
697 | {
|
---|
698 | list_foreach_safe(*list, cur_link, next_link) {
|
---|
699 | struct svc_id_linked *iter;
|
---|
700 | iter = list_get_instance(cur_link, struct svc_id_linked, link);
|
---|
701 |
|
---|
702 | if (iter->inited)
|
---|
703 | continue;
|
---|
704 |
|
---|
705 | errno_t rc = block_init(iter->svc_id);
|
---|
706 |
|
---|
707 | /* already used as an extent of active volume */
|
---|
708 | /* XXX: figure out how it is with hotspares too */
|
---|
709 | if (rc == EEXIST) {
|
---|
710 | list_remove(cur_link);
|
---|
711 | free_svc_id_linked(iter);
|
---|
712 | continue;
|
---|
713 | }
|
---|
714 |
|
---|
715 | if (rc != EOK)
|
---|
716 | return rc;
|
---|
717 |
|
---|
718 | iter->inited = true;
|
---|
719 | }
|
---|
720 |
|
---|
721 | return EOK;
|
---|
722 | }
|
---|
723 |
|
---|
724 | static void block_fini_dev_list(list_t *list)
|
---|
725 | {
|
---|
726 | list_foreach(*list, link, struct svc_id_linked, iter) {
|
---|
727 | if (iter->inited) {
|
---|
728 | block_fini(iter->svc_id);
|
---|
729 | iter->inited = false;
|
---|
730 | }
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | static errno_t hr_util_get_matching_md_svcs_list(list_t *rlist, list_t *devlist,
|
---|
735 | service_id_t svc_id, hr_metadata_t *md_main)
|
---|
736 | {
|
---|
737 | errno_t rc = EOK;
|
---|
738 |
|
---|
739 | list_foreach(*devlist, link, struct svc_id_linked, iter) {
|
---|
740 | if (iter->svc_id == svc_id)
|
---|
741 | continue;
|
---|
742 | void *md_block;
|
---|
743 | hr_metadata_t md;
|
---|
744 | rc = hr_get_metadata_block(iter->svc_id, &md_block);
|
---|
745 | if (rc != EOK)
|
---|
746 | goto error;
|
---|
747 | hr_decode_metadata_from_block(md_block, &md);
|
---|
748 |
|
---|
749 | free(md_block);
|
---|
750 |
|
---|
751 | if (!hr_valid_md_magic(&md))
|
---|
752 | continue;
|
---|
753 |
|
---|
754 | if (memcmp(md_main->uuid, md.uuid, HR_UUID_LEN) != 0)
|
---|
755 | continue;
|
---|
756 |
|
---|
757 | /*
|
---|
758 | * XXX: can I assume bsize and everything is fine when
|
---|
759 | * UUID matches?
|
---|
760 | */
|
---|
761 |
|
---|
762 | rc = hr_add_svc_linked_to_list(rlist, iter->svc_id, true, &md);
|
---|
763 | if (rc != EOK)
|
---|
764 | goto error;
|
---|
765 | }
|
---|
766 |
|
---|
767 | return EOK;
|
---|
768 | error:
|
---|
769 | free_svc_id_list(rlist);
|
---|
770 | return rc;
|
---|
771 | }
|
---|
772 |
|
---|
773 | static errno_t hr_util_assemble_from_matching_list(list_t *list)
|
---|
774 | {
|
---|
775 | HR_DEBUG("%s()", __func__);
|
---|
776 |
|
---|
777 | errno_t rc = EOK;
|
---|
778 |
|
---|
779 | hr_metadata_t *main_md = NULL;
|
---|
780 | size_t max_counter_val = 0;
|
---|
781 |
|
---|
782 | list_foreach(*list, link, struct svc_id_linked, iter) {
|
---|
783 | hr_metadata_dump(iter->md);
|
---|
784 | if (iter->md->counter >= max_counter_val) {
|
---|
785 | max_counter_val = iter->md->counter;
|
---|
786 | main_md = iter->md;
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | assert(main_md != NULL);
|
---|
791 |
|
---|
792 | hr_volume_t *vol;
|
---|
793 | rc = hr_create_vol_struct(&vol, (hr_level_t)main_md->level);
|
---|
794 | if (rc != EOK)
|
---|
795 | goto error;
|
---|
796 |
|
---|
797 | vol->nblocks = main_md->nblocks;
|
---|
798 | vol->data_blkno = main_md->data_blkno;
|
---|
799 | vol->truncated_blkno = main_md->truncated_blkno;
|
---|
800 | vol->data_offset = main_md->data_offset;
|
---|
801 | vol->counter = main_md->counter;
|
---|
802 | vol->metadata_version = main_md->version;
|
---|
803 | vol->extent_no = main_md->extent_no;
|
---|
804 | vol->level = main_md->level;
|
---|
805 | vol->layout = main_md->layout;
|
---|
806 | vol->strip_size = main_md->strip_size;
|
---|
807 | vol->bsize = main_md->bsize;
|
---|
808 | memcpy(vol->devname, main_md->devname, HR_DEVNAME_LEN);
|
---|
809 |
|
---|
810 | list_foreach(*list, link, struct svc_id_linked, iter) {
|
---|
811 | vol->extents[iter->md->index].svc_id = iter->svc_id;
|
---|
812 | if (iter->md->counter == max_counter_val)
|
---|
813 | vol->extents[iter->md->index].status = HR_EXT_ONLINE;
|
---|
814 | else
|
---|
815 | vol->extents[iter->md->index].status = HR_EXT_INVALID;
|
---|
816 | }
|
---|
817 |
|
---|
818 | rc = vol->hr_ops.create(vol);
|
---|
819 | if (rc != EOK)
|
---|
820 | goto error;
|
---|
821 |
|
---|
822 | fibril_rwlock_write_lock(&hr_volumes_lock);
|
---|
823 |
|
---|
824 | list_foreach(hr_volumes, lvolumes, hr_volume_t, other) {
|
---|
825 | uint8_t *our_uuid = vol->in_mem_md->uuid;
|
---|
826 | uint8_t *other_uuid = other->in_mem_md->uuid;
|
---|
827 | if (memcmp(our_uuid, other_uuid, HR_UUID_LEN) == 0) {
|
---|
828 | rc = EEXIST;
|
---|
829 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
830 | goto error;
|
---|
831 | }
|
---|
832 | }
|
---|
833 |
|
---|
834 | /*
|
---|
835 | * XXX: register it here
|
---|
836 | * ... if it fails on EEXIST try different name... like + 1 on the end
|
---|
837 | *
|
---|
838 | * TODO: discuss
|
---|
839 | */
|
---|
840 | rc = hr_register_volume(vol);
|
---|
841 | if (rc != EOK) {
|
---|
842 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
843 | goto error;
|
---|
844 | }
|
---|
845 |
|
---|
846 | list_append(&vol->lvolumes, &hr_volumes);
|
---|
847 |
|
---|
848 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
849 |
|
---|
850 | return EOK;
|
---|
851 | error:
|
---|
852 | hr_destroy_vol_struct(vol);
|
---|
853 | return rc;
|
---|
854 | }
|
---|
855 |
|
---|
856 | errno_t hr_util_try_auto_assemble(size_t *rassembled_cnt)
|
---|
857 | {
|
---|
858 | HR_DEBUG("%s()", __func__);
|
---|
859 |
|
---|
860 | /*
|
---|
861 | * scan partitions or disks:
|
---|
862 | *
|
---|
863 | * When we find a metadata block with valid
|
---|
864 | * magic, take UUID and try to find other matching
|
---|
865 | * UUIDs.
|
---|
866 | *
|
---|
867 | * We ignore extents that are a part of already
|
---|
868 | * active volumes. (even when the counter is lower
|
---|
869 | * on active volumes... XXX: use timestamp as initial counter value
|
---|
870 | * when assembling, or writing dirty metadata?)
|
---|
871 | */
|
---|
872 |
|
---|
873 | size_t asm_cnt = 0;
|
---|
874 | errno_t rc;
|
---|
875 | list_t dev_id_list;
|
---|
876 |
|
---|
877 | list_initialize(&dev_id_list);
|
---|
878 | rc = hr_fill_disk_part_svcs_list(&dev_id_list);
|
---|
879 | if (rc != EOK)
|
---|
880 | goto error;
|
---|
881 |
|
---|
882 | rc = block_init_dev_list(&dev_id_list);
|
---|
883 | if (rc != EOK)
|
---|
884 | goto error;
|
---|
885 |
|
---|
886 | struct svc_id_linked *iter;
|
---|
887 | while (!list_empty(&dev_id_list)) {
|
---|
888 | iter = list_pop(&dev_id_list, struct svc_id_linked, link);
|
---|
889 |
|
---|
890 | printf("svc_id: %lu\n", iter->svc_id);
|
---|
891 |
|
---|
892 | void *metadata_block;
|
---|
893 | hr_metadata_t metadata;
|
---|
894 |
|
---|
895 | rc = hr_get_metadata_block(iter->svc_id, &metadata_block);
|
---|
896 | if (rc != EOK)
|
---|
897 | goto error;
|
---|
898 |
|
---|
899 | hr_decode_metadata_from_block(metadata_block, &metadata);
|
---|
900 |
|
---|
901 | free(metadata_block);
|
---|
902 |
|
---|
903 | if (!hr_valid_md_magic(&metadata)) {
|
---|
904 | printf("BAD magic\n");
|
---|
905 | block_fini(iter->svc_id);
|
---|
906 | free_svc_id_linked(iter);
|
---|
907 | continue;
|
---|
908 | }
|
---|
909 |
|
---|
910 | hr_metadata_dump(&metadata);
|
---|
911 |
|
---|
912 | char *svc_name = NULL;
|
---|
913 | rc = loc_service_get_name(iter->svc_id, &svc_name);
|
---|
914 | if (rc != EOK)
|
---|
915 | goto error;
|
---|
916 |
|
---|
917 | HR_DEBUG("found valid metadata on %s, "
|
---|
918 | "will try to match other extents\n", svc_name);
|
---|
919 |
|
---|
920 | free(svc_name);
|
---|
921 |
|
---|
922 | list_t matching_svcs_list;
|
---|
923 | list_initialize(&matching_svcs_list);
|
---|
924 |
|
---|
925 | rc = hr_util_get_matching_md_svcs_list(&matching_svcs_list,
|
---|
926 | &dev_id_list, iter->svc_id, &metadata);
|
---|
927 | if (rc != EOK)
|
---|
928 | goto error;
|
---|
929 |
|
---|
930 | /* add current iter to list as well */
|
---|
931 | rc = hr_add_svc_linked_to_list(&matching_svcs_list,
|
---|
932 | iter->svc_id, true, &metadata);
|
---|
933 | if (rc != EOK) {
|
---|
934 | free_svc_id_list(&matching_svcs_list);
|
---|
935 | goto error;
|
---|
936 | }
|
---|
937 |
|
---|
938 | /* remove matching list members from dev_id_list */
|
---|
939 | list_foreach(matching_svcs_list, link, struct svc_id_linked,
|
---|
940 | iter2) {
|
---|
941 | printf("matching svc_id: %lu\n", iter2->svc_id);
|
---|
942 | struct svc_id_linked *to_remove;
|
---|
943 | list_foreach_safe(dev_id_list, cur_link, next_link) {
|
---|
944 | to_remove = list_get_instance(cur_link,
|
---|
945 | struct svc_id_linked, link);
|
---|
946 | if (to_remove->svc_id == iter2->svc_id) {
|
---|
947 | list_remove(cur_link);
|
---|
948 | free_svc_id_linked(to_remove);
|
---|
949 | }
|
---|
950 | }
|
---|
951 | }
|
---|
952 |
|
---|
953 | rc = hr_util_assemble_from_matching_list(&matching_svcs_list);
|
---|
954 | switch (rc) {
|
---|
955 | case EOK:
|
---|
956 | asm_cnt++;
|
---|
957 | break;
|
---|
958 | case EEXIST:
|
---|
959 | /*
|
---|
960 | * A race is detected this way, because we don't want
|
---|
961 | * to hold the hr_volumes list lock for a long time,
|
---|
962 | * for all assembly attempts. XXX: discuss...
|
---|
963 | */
|
---|
964 | rc = EOK;
|
---|
965 | break;
|
---|
966 | default:
|
---|
967 | block_fini_dev_list(&matching_svcs_list);
|
---|
968 | free_svc_id_list(&matching_svcs_list);
|
---|
969 | goto error;
|
---|
970 | }
|
---|
971 |
|
---|
972 | free_svc_id_list(&matching_svcs_list);
|
---|
973 | }
|
---|
974 |
|
---|
975 | error:
|
---|
976 | if (rassembled_cnt != NULL)
|
---|
977 | *rassembled_cnt = asm_cnt;
|
---|
978 |
|
---|
979 | block_fini_dev_list(&dev_id_list);
|
---|
980 | free_svc_id_list(&dev_id_list);
|
---|
981 |
|
---|
982 | return rc;
|
---|
983 | }
|
---|
984 |
|
---|
985 | /** @}
|
---|
986 | */
|
---|