[da5c257] | 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 <block.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <hr.h>
|
---|
| 39 | #include <io/log.h>
|
---|
| 40 | #include <loc.h>
|
---|
[44ea48e] | 41 | #include <stdlib.h>
|
---|
| 42 | #include <stdio.h>
|
---|
[da5c257] | 43 | #include <str_error.h>
|
---|
| 44 |
|
---|
| 45 | #include "util.h"
|
---|
[b0f1366] | 46 | #include "var.h"
|
---|
[da5c257] | 47 |
|
---|
| 48 | extern loc_srv_t *hr_srv;
|
---|
| 49 |
|
---|
| 50 | errno_t hr_init_devs(hr_volume_t *vol)
|
---|
| 51 | {
|
---|
| 52 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_init_devs()");
|
---|
| 53 |
|
---|
| 54 | errno_t rc;
|
---|
| 55 | size_t i;
|
---|
| 56 |
|
---|
| 57 | for (i = 0; i < vol->dev_no; i++) {
|
---|
[dbd91da] | 58 | rc = block_init(vol->extents[i].svc_id);
|
---|
[da5c257] | 59 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
[dbd91da] | 60 | "hr_init_devs(): initing (%" PRIun ")", vol->extents[i].svc_id);
|
---|
[da5c257] | 61 | if (rc != EOK) {
|
---|
| 62 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 63 | "hr_init_devs(): initing (%" PRIun ") failed, aborting",
|
---|
[dbd91da] | 64 | vol->extents[i].svc_id);
|
---|
[da5c257] | 65 | break;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return rc;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void hr_fini_devs(hr_volume_t *vol)
|
---|
| 73 | {
|
---|
| 74 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_fini_devs()");
|
---|
| 75 |
|
---|
| 76 | size_t i;
|
---|
| 77 |
|
---|
| 78 | for (i = 0; i < vol->dev_no; i++)
|
---|
[dbd91da] | 79 | block_fini(vol->extents[i].svc_id);
|
---|
[da5c257] | 80 | }
|
---|
| 81 |
|
---|
| 82 | errno_t hr_register_volume(hr_volume_t *new_volume)
|
---|
| 83 | {
|
---|
[b0f1366] | 84 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_register_volume()");
|
---|
| 85 |
|
---|
[da5c257] | 86 | errno_t rc;
|
---|
| 87 | service_id_t new_id;
|
---|
| 88 | category_id_t cat_id;
|
---|
[44ea48e] | 89 | char *fullname = NULL;
|
---|
[da5c257] | 90 |
|
---|
[44ea48e] | 91 | if (asprintf(&fullname, "devices/%s", new_volume->devname) < 0)
|
---|
| 92 | return ENOMEM;
|
---|
| 93 |
|
---|
| 94 | rc = loc_service_register(hr_srv, fullname, &new_id);
|
---|
[da5c257] | 95 | if (rc != EOK) {
|
---|
| 96 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 97 | "unable to register device \"%s\": %s\n",
|
---|
| 98 | new_volume->devname, str_error(rc));
|
---|
| 99 | goto error;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | rc = loc_category_get_id("raid", &cat_id, IPC_FLAG_BLOCKING);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 105 | "failed resolving category \"raid\": %s\n", str_error(rc));
|
---|
| 106 | goto error;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | rc = loc_service_add_to_cat(hr_srv, new_id, cat_id);
|
---|
| 110 | if (rc != EOK) {
|
---|
| 111 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 112 | "failed adding \"%s\" to category \"raid\": %s\n",
|
---|
| 113 | new_volume->devname, str_error(rc));
|
---|
| 114 | goto error;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | new_volume->svc_id = new_id;
|
---|
| 118 |
|
---|
| 119 | error:
|
---|
[44ea48e] | 120 | free(fullname);
|
---|
[da5c257] | 121 | return rc;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[6b8e89b0] | 124 | errno_t hr_check_devs(hr_volume_t *vol, uint64_t *rblkno, size_t *rbsize)
|
---|
[b0f1366] | 125 | {
|
---|
| 126 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_check_devs()");
|
---|
| 127 |
|
---|
| 128 | errno_t rc;
|
---|
| 129 | size_t i, bsize, last_bsize;
|
---|
| 130 | uint64_t nblocks, last_nblocks;
|
---|
| 131 | uint64_t total_blocks = 0;
|
---|
| 132 |
|
---|
| 133 | for (i = 0; i < vol->dev_no; i++) {
|
---|
[dbd91da] | 134 | rc = block_get_nblocks(vol->extents[i].svc_id, &nblocks);
|
---|
[b0f1366] | 135 | if (rc != EOK)
|
---|
| 136 | goto error;
|
---|
| 137 | if (i != 0 && nblocks != last_nblocks) {
|
---|
| 138 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 139 | "number of blocks differs");
|
---|
| 140 | rc = EINVAL;
|
---|
| 141 | goto error;
|
---|
| 142 | }
|
---|
| 143 | total_blocks += nblocks;
|
---|
| 144 | last_nblocks = nblocks;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | for (i = 0; i < vol->dev_no; i++) {
|
---|
[dbd91da] | 148 | rc = block_get_bsize(vol->extents[i].svc_id, &bsize);
|
---|
[b0f1366] | 149 | if (rc != EOK)
|
---|
| 150 | goto error;
|
---|
| 151 | if (i != 0 && bsize != last_bsize) {
|
---|
| 152 | log_msg(LOG_DEFAULT, LVL_ERROR, "block sizes differ");
|
---|
| 153 | rc = EINVAL;
|
---|
| 154 | goto error;
|
---|
| 155 | }
|
---|
| 156 | last_bsize = bsize;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[6b8e89b0] | 159 | if (rblkno != NULL)
|
---|
| 160 | *rblkno = total_blocks;
|
---|
| 161 | if (rbsize != NULL)
|
---|
| 162 | *rbsize = bsize;
|
---|
[b0f1366] | 163 | error:
|
---|
| 164 | return rc;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[4a2a6b8b] | 167 | errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
|
---|
[b0f1366] | 168 | {
|
---|
[4a2a6b8b] | 169 | if (ba + cnt > vol->data_blkno)
|
---|
[b0f1366] | 170 | return ERANGE;
|
---|
[4a2a6b8b] | 171 | return EOK;
|
---|
| 172 | }
|
---|
[b0f1366] | 173 |
|
---|
[4a2a6b8b] | 174 | void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
|
---|
| 175 | {
|
---|
[b0f1366] | 176 | *ba = *ba + vol->data_offset;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[da5c257] | 179 | /** @}
|
---|
| 180 | */
|
---|