source: mainline/uspace/srv/bd/hr/util.c@ 5d96f427

Last change on this file since 5d96f427 was 5d96f427, checked in by Miroslav Cimerman <mc@…>, 9 months ago

hr: use DPRINTF and ERR_PRINTF macros from util.h

  • Property mode set to 100644
File size: 5.7 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 <block.h>
37#include <errno.h>
38#include <hr.h>
39#include <io/log.h>
40#include <loc.h>
41#include <stdlib.h>
42#include <stdio.h>
43#include <str_error.h>
44
45#include "util.h"
46#include "var.h"
47
48extern loc_srv_t *hr_srv;
49
50errno_t hr_init_devs(hr_volume_t *vol)
51{
52 DPRINTF("hr_init_devs()\n");
53
54 errno_t rc;
55 size_t i;
56 hr_extent_t *extent;
57
58 for (i = 0; i < vol->dev_no; i++) {
59 extent = &vol->extents[i];
60 if (extent->svc_id == 0) {
61 extent->status = HR_EXT_MISSING;
62 continue;
63 }
64
65 DPRINTF("hr_init_devs(): block_init() on (%lu)\n",
66 extent->svc_id);
67 rc = block_init(extent->svc_id);
68 extent->status = HR_EXT_ONLINE;
69
70 if (rc != EOK) {
71 ERR_PRINTF("hr_init_devs(): initing (%lu) failed, "
72 "aborting\n", extent->svc_id);
73 break;
74 }
75 }
76
77 return rc;
78}
79
80void hr_fini_devs(hr_volume_t *vol)
81{
82 DPRINTF("hr_fini_devs()\n");
83
84 size_t i;
85
86 for (i = 0; i < vol->dev_no; i++) {
87 if (vol->extents[i].status != HR_EXT_MISSING) {
88 DPRINTF("hr_fini_devs(): block_fini() on (%lu)\n",
89 vol->extents[i].svc_id);
90 block_fini(vol->extents[i].svc_id);
91 }
92 }
93}
94
95errno_t hr_register_volume(hr_volume_t *vol)
96{
97 DPRINTF("hr_register_volume()\n");
98
99 errno_t rc;
100 service_id_t new_id;
101 category_id_t cat_id;
102 char *fullname = NULL;
103 char *devname = vol->devname;
104
105 if (asprintf(&fullname, "devices/%s", devname) < 0)
106 return ENOMEM;
107
108 rc = loc_service_register(hr_srv, fullname, &new_id);
109 if (rc != EOK) {
110 ERR_PRINTF("unable to register device \"%s\": %s\n",
111 fullname, str_error(rc));
112 goto error;
113 }
114
115 rc = loc_category_get_id("raid", &cat_id, IPC_FLAG_BLOCKING);
116 if (rc != EOK) {
117 ERR_PRINTF("failed resolving category \"raid\": %s\n",
118 str_error(rc));
119 goto error;
120 }
121
122 rc = loc_service_add_to_cat(hr_srv, new_id, cat_id);
123 if (rc != EOK) {
124 ERR_PRINTF("failed adding \"%s\" to category \"raid\": %s\n",
125 fullname, str_error(rc));
126 goto error;
127 }
128
129 vol->svc_id = new_id;
130
131error:
132 free(fullname);
133 return rc;
134}
135
136errno_t hr_check_devs(hr_volume_t *vol, uint64_t *rblkno, size_t *rbsize)
137{
138 DPRINTF("hr_check_devs()\n");
139
140 errno_t rc;
141 size_t i, bsize;
142 uint64_t nblocks;
143 size_t last_bsize = 0;
144 uint64_t last_nblocks = 0;
145 uint64_t total_blocks = 0;
146 hr_extent_t *extent;
147
148 for (i = 0; i < vol->dev_no; i++) {
149 extent = &vol->extents[i];
150 if (extent->status == HR_EXT_MISSING)
151 continue;
152 rc = block_get_nblocks(extent->svc_id, &nblocks);
153 if (rc != EOK)
154 goto error;
155 if (last_nblocks != 0 && nblocks != last_nblocks) {
156 ERR_PRINTF("number of blocks differs\n");
157 rc = EINVAL;
158 goto error;
159 }
160
161 total_blocks += nblocks;
162 last_nblocks = nblocks;
163 }
164
165 for (i = 0; i < vol->dev_no; i++) {
166 extent = &vol->extents[i];
167 if (extent->status == HR_EXT_MISSING)
168 continue;
169 rc = block_get_bsize(extent->svc_id, &bsize);
170 if (rc != EOK)
171 goto error;
172 if (last_bsize != 0 && bsize != last_bsize) {
173 ERR_PRINTF("block sizes differ\n");
174 rc = EINVAL;
175 goto error;
176 }
177
178 last_bsize = bsize;
179 }
180
181 if ((bsize % 512) != 0) {
182 ERR_PRINTF("block size not multiple of 512\n");
183 return EINVAL;
184 }
185
186 if (rblkno != NULL)
187 *rblkno = total_blocks;
188 if (rbsize != NULL)
189 *rbsize = bsize;
190error:
191 return rc;
192}
193
194errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
195{
196 if (ba + cnt > vol->data_blkno)
197 return ERANGE;
198 return EOK;
199}
200
201void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
202{
203 *ba = *ba + vol->data_offset;
204}
205
206void hr_update_ext_status(hr_volume_t *vol, uint64_t extent, hr_ext_status_t s)
207{
208 log_msg(LOG_DEFAULT, LVL_WARN,
209 "vol %s, changing extent: %lu, to status: %s",
210 vol->devname, extent, hr_get_ext_status_msg(s));
211 vol->extents[extent].status = s;
212}
213
214/*
215 * Do a whole sync (ba = 0, cnt = 0) across all extents,
216 * and update extent status. *For now*, the caller has to
217 * update volume status after the syncs.
218 *
219 * TODO: add update_vol_status fcn ptr for each raid
220 */
221void hr_sync_all_extents(hr_volume_t *vol)
222{
223 errno_t rc;
224
225 fibril_mutex_lock(&vol->lock);
226 for (size_t i = 0; i < vol->dev_no; i++) {
227 if (vol->extents[i].status != HR_EXT_ONLINE)
228 continue;
229 rc = block_sync_cache(vol->extents[i].svc_id, 0, 0);
230 if (rc != EOK && rc != ENOTSUP) {
231 if (rc == ENOENT)
232 hr_update_ext_status(vol, i, HR_EXT_MISSING);
233 else if (rc != EOK)
234 hr_update_ext_status(vol, i, HR_EXT_FAILED);
235 }
236 }
237 fibril_mutex_unlock(&vol->lock);
238}
239
240/** @}
241 */
Note: See TracBrowser for help on using the repository browser.