source: mainline/uspace/lib/device/src/hr.c@ 11111e4

Last change on this file since 11111e4 was 13ce552, checked in by Miroslav Cimerman <mc@…>, 14 months ago

hr: add DEGRADED volume state

Use it for weakened mirror as well.

  • Property mode set to 100644
File size: 6.5 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 libdevice
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <abi/ipc/interfaces.h>
37#include <async.h>
38#include <hr.h>
39#include <ipc/hr.h>
40#include <ipc/services.h>
41#include <loc.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <str.h>
45
46errno_t hr_sess_init(hr_t **rhr)
47{
48 errno_t rc;
49
50 hr_t *hr = calloc(1, sizeof(hr_t));
51 if (hr == NULL) {
52 rc = ENOMEM;
53 goto error;
54 }
55
56 service_id_t hr_svcid;
57
58 rc = loc_service_get_id(SERVICE_NAME_HR, &hr_svcid, IPC_FLAG_BLOCKING);
59 if (rc != EOK) {
60 rc = EIO;
61 goto error;
62 }
63
64 hr->sess = loc_service_connect(hr_svcid, INTERFACE_HR,
65 IPC_FLAG_BLOCKING);
66 if (hr->sess == NULL) {
67 rc = EIO;
68 goto error;
69 }
70
71 *rhr = hr;
72 return EOK;
73error:
74 if (hr != NULL)
75 free(hr);
76 *rhr = NULL;
77 return rc;
78}
79
80void hr_sess_destroy(hr_t *hr)
81{
82 if (hr == NULL)
83 return;
84
85 async_hangup(hr->sess);
86 free(hr);
87}
88
89errno_t hr_create(hr_t *hr, hr_config_t *hr_config, bool assemble)
90{
91 errno_t rc, retval;
92 async_exch_t *exch;
93 aid_t req;
94
95 exch = async_exchange_begin(hr->sess);
96 if (exch == NULL)
97 return EINVAL;
98
99 req = async_send_0(exch, assemble ? HR_ASSEMBLE : HR_CREATE, NULL);
100
101 rc = async_data_write_start(exch, hr_config, sizeof(hr_config_t));
102 if (rc != EOK) {
103 async_exchange_end(exch);
104 async_forget(req);
105 return rc;
106 }
107
108 async_exchange_end(exch);
109 async_wait_for(req, &retval);
110 if (retval != EOK)
111 return retval;
112
113 return EOK;
114}
115
116static errno_t print_vol_info(size_t index, hr_vol_info_t *vol_info)
117{
118 errno_t rc;
119 size_t i;
120 char *devname;
121 hr_extent_t *ext;
122
123 printf("--- vol %zu ---\n", index);
124
125 printf("svc_id: %lu\n", vol_info->svc_id);
126
127 rc = loc_service_get_name(vol_info->svc_id, &devname);
128 if (rc != EOK)
129 return rc;
130 printf("devname: %s\n", devname);
131
132 printf("status: %s\n", hr_get_vol_status_msg(vol_info->status));
133
134 printf("level: %d\n", vol_info->level);
135 if (vol_info->level == HR_LVL_0 || vol_info->level == HR_LVL_4) {
136 if (vol_info->strip_size / 1024 < 1)
137 printf("strip size in bytes: %u\n",
138 vol_info->strip_size);
139 else
140 printf("strip size: %uK\n",
141 vol_info->strip_size / 1024);
142 }
143 printf("size in bytes: %luMiB\n",
144 vol_info->nblocks * vol_info->bsize / 1024 / 1024);
145 printf("size in blocks: %lu\n", vol_info->nblocks);
146 printf("block size: %zu\n", vol_info->bsize);
147
148 if (vol_info->level == HR_LVL_4)
149 printf("extents: [P] [status] [index] [devname]\n");
150 else
151 printf("extents: [status] [index] [devname]\n");
152 for (i = 0; i < vol_info->extent_no; i++) {
153 ext = &vol_info->extents[i];
154 if (ext->status == HR_EXT_MISSING) {
155 devname = (char *) "MISSING-devname";
156 } else {
157 rc = loc_service_get_name(ext->svc_id, &devname);
158 if (rc != EOK)
159 return rc;
160 }
161 if (i == 0 && vol_info->level == HR_LVL_4)
162 printf(" P %s %zu %s\n", hr_get_ext_status_msg(ext->status), i, devname);
163 else if (vol_info->level == HR_LVL_4)
164 printf(" %s %zu %s\n", hr_get_ext_status_msg(ext->status), i, devname);
165 else
166 printf(" %s %zu %s\n", hr_get_ext_status_msg(ext->status), i, devname);
167 }
168 return EOK;
169}
170
171errno_t hr_stop(const char *devname, long extent)
172{
173 hr_t *hr;
174 errno_t rc;
175 async_exch_t *exch;
176 service_id_t svc_id;
177
178 rc = loc_service_get_id(devname, &svc_id, 0);
179 if (rc != EOK)
180 return rc;
181
182 rc = hr_sess_init(&hr);
183 if (rc != EOK)
184 return rc;
185
186 exch = async_exchange_begin(hr->sess);
187 if (exch == NULL) {
188 rc = EINVAL;
189 goto error;
190 }
191 rc = async_req_2_0(exch, HR_STOP, svc_id, extent);
192 async_exchange_end(exch);
193
194 if (rc != EOK)
195 goto error;
196error:
197 hr_sess_destroy(hr);
198 return rc;
199}
200
201errno_t hr_print_status(void)
202{
203 hr_t *hr;
204 errno_t rc, retval;
205 async_exch_t *exch;
206 aid_t req;
207 size_t size, i;
208 hr_vol_info_t *vols = NULL;
209
210 rc = hr_sess_init(&hr);
211 if (rc != EOK)
212 return rc;
213
214 exch = async_exchange_begin(hr->sess);
215 if (exch == NULL) {
216 rc = EINVAL;
217 goto error;
218 }
219
220 req = async_send_0(exch, HR_STATUS, NULL);
221 rc = async_data_read_start(exch, &size, sizeof(size_t));
222 if (rc != EOK) {
223 async_exchange_end(exch);
224 async_forget(req);
225 return rc;
226 }
227
228 vols = calloc(size, sizeof(hr_vol_info_t));
229 if (vols == NULL) {
230 async_exchange_end(exch);
231 async_forget(req);
232 return ENOMEM;
233 }
234
235 for (i = 0; i < size; i++) {
236 rc = async_data_read_start(exch, &vols[i],
237 sizeof(hr_vol_info_t));
238 if (rc != EOK) {
239 async_exchange_end(exch);
240 async_forget(req);
241 goto error;
242 }
243 }
244
245 async_exchange_end(exch);
246 async_wait_for(req, &retval);
247 if (retval != EOK) {
248 rc = retval;
249 goto error;
250 }
251
252 if (size == 0) {
253 printf("no active arrays\n");
254 goto error;
255 }
256
257 for (i = 0; i < size; i++) {
258 rc = print_vol_info(i, &vols[i]);
259 if (rc != EOK)
260 goto error;
261 }
262
263error:
264 hr_sess_destroy(hr);
265 if (vols != NULL)
266 free(vols);
267 return rc;
268}
269
270const char *hr_get_vol_status_msg(hr_vol_status_t status)
271{
272 switch (status) {
273 case HR_VOL_ONLINE:
274 return "ONLINE";
275 case HR_VOL_FAULTY:
276 return "FAULTY";
277 case HR_VOL_DEGRADED:
278 return "DEGRADED";
279 default:
280 return "UNKNOWN";
281 }
282}
283
284const char *hr_get_ext_status_msg(hr_ext_status_t status)
285{
286 switch (status) {
287 case HR_EXT_ONLINE:
288 return "ONLINE";
289 case HR_EXT_MISSING:
290 return "MISSING";
291 case HR_EXT_FAILED:
292 return "FAILED";
293 default:
294 return "UNKNOWN";
295 }
296}
297
298/** @}
299 */
Note: See TracBrowser for help on using the repository browser.