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

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

hr: add status printing

  • Property mode set to 100644
File size: 4.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)
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
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
122 printf("--- vol %zu ---\n", index);
123
124 printf("svc_id: %lu\n", vol_info->svc_id);
125
126 rc = loc_service_get_name(vol_info->svc_id, &devname);
127 if (rc != EOK)
128 return rc;
129 printf("devname: %s\n", devname);
130
131 printf("level: %d\n", vol_info->level);
132 printf("nblocks: %lu\n", vol_info->nblocks);
133 printf("bsize: %zu\n", vol_info->bsize);
134
135 printf("extents: [index] [devname]\n");
136 for (i = 0; i < vol_info->extent_no; i++) {
137 rc = loc_service_get_name(vol_info->extents[i], &devname);
138 if (rc != EOK)
139 return rc;
140 printf(" %zu %s\n", i, devname);
141 }
142 return EOK;
143}
144
145errno_t hr_print_status(void)
146{
147 hr_t *hr;
148 errno_t rc, retval;
149 async_exch_t *exch;
150 aid_t req;
151 size_t size, i;
152 hr_vol_info_t *vols;
153
154 rc = hr_sess_init(&hr);
155 if (rc != EOK)
156 return rc;
157
158 exch = async_exchange_begin(hr->sess);
159 if (exch == NULL)
160 return EINVAL;
161
162 req = async_send_0(exch, HR_STATUS, NULL);
163
164 rc = async_data_read_start(exch, &size, sizeof(size_t));
165 if (rc != EOK) {
166 async_exchange_end(exch);
167 async_forget(req);
168 return rc;
169 }
170
171 vols = calloc(size, sizeof(hr_vol_info_t));
172 if (vols == NULL) {
173 async_exchange_end(exch);
174 async_forget(req);
175 return ENOMEM;
176 }
177
178 for (i = 0; i < size; i++) {
179 rc = async_data_read_start(exch, &vols[i],
180 sizeof(hr_vol_info_t));
181 if (rc != EOK) {
182 async_exchange_end(exch);
183 async_forget(req);
184 goto error;
185 }
186 }
187
188 async_exchange_end(exch);
189 async_wait_for(req, &retval);
190 if (retval != EOK) {
191 rc = retval;
192 goto error;
193 }
194
195 for (i = 0; i < size; i++) {
196 rc = print_vol_info(i, &vols[i]);
197 if (rc != EOK)
198 goto error;
199 }
200
201error:
202 if (vols != NULL)
203 free(vols);
204 return rc;
205}
206
207/** @}
208 */
Note: See TracBrowser for help on using the repository browser.