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 |
|
---|
46 | errno_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;
|
---|
73 | error:
|
---|
74 | if (hr != NULL)
|
---|
75 | free(hr);
|
---|
76 | *rhr = NULL;
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void hr_sess_destroy(hr_t *hr)
|
---|
81 | {
|
---|
82 | if (hr == NULL)
|
---|
83 | return;
|
---|
84 |
|
---|
85 | async_hangup(hr->sess);
|
---|
86 | free(hr);
|
---|
87 | }
|
---|
88 |
|
---|
89 | errno_t hr_create(hr_t *hr, hr_config_t *hr_config)
|
---|
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, 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 |
|
---|
116 | errno_t hr_assemble(hr_t *hr, hr_config_t *hr_config)
|
---|
117 | {
|
---|
118 | errno_t rc, retval;
|
---|
119 | async_exch_t *exch;
|
---|
120 | aid_t req;
|
---|
121 |
|
---|
122 | exch = async_exchange_begin(hr->sess);
|
---|
123 | if (exch == NULL)
|
---|
124 | return EINVAL;
|
---|
125 |
|
---|
126 | req = async_send_0(exch, HR_ASSEMBLE, NULL);
|
---|
127 |
|
---|
128 | rc = async_data_write_start(exch, hr_config, sizeof(hr_config_t));
|
---|
129 | if (rc != EOK) {
|
---|
130 | async_exchange_end(exch);
|
---|
131 | async_forget(req);
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | async_exchange_end(exch);
|
---|
136 | async_wait_for(req, &retval);
|
---|
137 | if (retval != EOK)
|
---|
138 | return retval;
|
---|
139 |
|
---|
140 | return EOK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static errno_t print_vol_info(size_t index, hr_vol_info_t *vol_info)
|
---|
144 | {
|
---|
145 | errno_t rc;
|
---|
146 | size_t i;
|
---|
147 | char *devname;
|
---|
148 |
|
---|
149 | printf("--- vol %zu ---\n", index);
|
---|
150 |
|
---|
151 | printf("svc_id: %lu\n", vol_info->svc_id);
|
---|
152 |
|
---|
153 | rc = loc_service_get_name(vol_info->svc_id, &devname);
|
---|
154 | if (rc != EOK)
|
---|
155 | return rc;
|
---|
156 | printf("devname: %s\n", devname);
|
---|
157 |
|
---|
158 | printf("level: %d\n", vol_info->level);
|
---|
159 | if (vol_info->level == hr_l_0 || vol_info->level == hr_l_4) {
|
---|
160 | if (vol_info->strip_size / 1024 < 1)
|
---|
161 | printf("strip size in bytes: %u\n",
|
---|
162 | vol_info->strip_size);
|
---|
163 | else
|
---|
164 | printf("strip size: %uK\n",
|
---|
165 | vol_info->strip_size / 1024);
|
---|
166 | }
|
---|
167 | printf("size in bytes: %luMiB\n",
|
---|
168 | vol_info->nblocks * vol_info->bsize / 1024 / 1024);
|
---|
169 | printf("size in blocks: %lu\n", vol_info->nblocks);
|
---|
170 | printf("block size: %zu\n", vol_info->bsize);
|
---|
171 |
|
---|
172 | printf("extents: [index] [devname]\n");
|
---|
173 | for (i = 0; i < vol_info->extent_no; i++) {
|
---|
174 | rc = loc_service_get_name(vol_info->extents[i], &devname);
|
---|
175 | if (rc != EOK)
|
---|
176 | return rc;
|
---|
177 | printf(" %zu %s\n", i, devname);
|
---|
178 | }
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | errno_t hr_stop(const char *devname)
|
---|
183 | {
|
---|
184 | hr_t *hr;
|
---|
185 | errno_t rc;
|
---|
186 | async_exch_t *exch;
|
---|
187 | service_id_t svc_id;
|
---|
188 |
|
---|
189 | rc = loc_service_get_id(devname, &svc_id, 0);
|
---|
190 | if (rc != EOK)
|
---|
191 | return rc;
|
---|
192 |
|
---|
193 | rc = hr_sess_init(&hr);
|
---|
194 | if (rc != EOK)
|
---|
195 | return rc;
|
---|
196 |
|
---|
197 | exch = async_exchange_begin(hr->sess);
|
---|
198 | if (exch == NULL) {
|
---|
199 | rc = EINVAL;
|
---|
200 | goto error;
|
---|
201 | }
|
---|
202 | rc = async_req_1_0(exch, HR_STOP, svc_id);
|
---|
203 | async_exchange_end(exch);
|
---|
204 |
|
---|
205 | if (rc != EOK)
|
---|
206 | goto error;
|
---|
207 | error:
|
---|
208 | hr_sess_destroy(hr);
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 | errno_t hr_print_status(void)
|
---|
213 | {
|
---|
214 | hr_t *hr;
|
---|
215 | errno_t rc, retval;
|
---|
216 | async_exch_t *exch;
|
---|
217 | aid_t req;
|
---|
218 | size_t size, i;
|
---|
219 | hr_vol_info_t *vols = NULL;
|
---|
220 |
|
---|
221 | rc = hr_sess_init(&hr);
|
---|
222 | if (rc != EOK)
|
---|
223 | return rc;
|
---|
224 |
|
---|
225 | exch = async_exchange_begin(hr->sess);
|
---|
226 | if (exch == NULL) {
|
---|
227 | rc = EINVAL;
|
---|
228 | goto error;
|
---|
229 | }
|
---|
230 |
|
---|
231 | req = async_send_0(exch, HR_STATUS, NULL);
|
---|
232 | rc = async_data_read_start(exch, &size, sizeof(size_t));
|
---|
233 | if (rc != EOK) {
|
---|
234 | async_exchange_end(exch);
|
---|
235 | async_forget(req);
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 | vols = calloc(size, sizeof(hr_vol_info_t));
|
---|
240 | if (vols == NULL) {
|
---|
241 | async_exchange_end(exch);
|
---|
242 | async_forget(req);
|
---|
243 | return ENOMEM;
|
---|
244 | }
|
---|
245 |
|
---|
246 | for (i = 0; i < size; i++) {
|
---|
247 | rc = async_data_read_start(exch, &vols[i],
|
---|
248 | sizeof(hr_vol_info_t));
|
---|
249 | if (rc != EOK) {
|
---|
250 | async_exchange_end(exch);
|
---|
251 | async_forget(req);
|
---|
252 | goto error;
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | async_exchange_end(exch);
|
---|
257 | async_wait_for(req, &retval);
|
---|
258 | if (retval != EOK) {
|
---|
259 | rc = retval;
|
---|
260 | goto error;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (size == 0) {
|
---|
264 | printf("no active arrays\n");
|
---|
265 | goto error;
|
---|
266 | }
|
---|
267 |
|
---|
268 | for (i = 0; i < size; i++) {
|
---|
269 | rc = print_vol_info(i, &vols[i]);
|
---|
270 | if (rc != EOK)
|
---|
271 | goto error;
|
---|
272 | }
|
---|
273 |
|
---|
274 | error:
|
---|
275 | hr_sess_destroy(hr);
|
---|
276 | if (vols != NULL)
|
---|
277 | free(vols);
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** @}
|
---|
282 | */
|
---|