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

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

hr: write persistent metadata, assemble array from metadata

  • Property mode set to 100644
File size: 5.0 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)
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
116errno_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
143static 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 printf("nblocks: %lu\n", vol_info->nblocks);
160 printf("bsize: %zu\n", vol_info->bsize);
161
162 printf("extents: [index] [devname]\n");
163 for (i = 0; i < vol_info->extent_no; i++) {
164 rc = loc_service_get_name(vol_info->extents[i], &devname);
165 if (rc != EOK)
166 return rc;
167 printf(" %zu %s\n", i, devname);
168 }
169 return EOK;
170}
171
172errno_t hr_print_status(void)
173{
174 hr_t *hr;
175 errno_t rc, retval;
176 async_exch_t *exch;
177 aid_t req;
178 size_t size, i;
179 hr_vol_info_t *vols;
180
181 rc = hr_sess_init(&hr);
182 if (rc != EOK)
183 return rc;
184
185 exch = async_exchange_begin(hr->sess);
186 if (exch == NULL)
187 return EINVAL;
188
189 req = async_send_0(exch, HR_STATUS, NULL);
190
191 rc = async_data_read_start(exch, &size, sizeof(size_t));
192 if (rc != EOK) {
193 async_exchange_end(exch);
194 async_forget(req);
195 return rc;
196 }
197
198 vols = calloc(size, sizeof(hr_vol_info_t));
199 if (vols == NULL) {
200 async_exchange_end(exch);
201 async_forget(req);
202 return ENOMEM;
203 }
204
205 for (i = 0; i < size; i++) {
206 rc = async_data_read_start(exch, &vols[i],
207 sizeof(hr_vol_info_t));
208 if (rc != EOK) {
209 async_exchange_end(exch);
210 async_forget(req);
211 goto error;
212 }
213 }
214
215 async_exchange_end(exch);
216 async_wait_for(req, &retval);
217 if (retval != EOK) {
218 rc = retval;
219 goto error;
220 }
221
222 for (i = 0; i < size; i++) {
223 rc = print_vol_info(i, &vols[i]);
224 if (rc != EOK)
225 goto error;
226 }
227
228error:
229 if (vols != NULL)
230 free(vols);
231 return rc;
232}
233
234/** @}
235 */
Note: See TracBrowser for help on using the repository browser.