1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
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 libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Virtual Block Device client API
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <ipc/services.h>
|
---|
37 | #include <ipc/vbd.h>
|
---|
38 | #include <loc.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <types/label.h>
|
---|
43 | #include <vbd.h>
|
---|
44 |
|
---|
45 | static int vbd_get_ids_internal(vbd_t *, sysarg_t, sysarg_t, sysarg_t **,
|
---|
46 | size_t *);
|
---|
47 |
|
---|
48 | int vbd_create(vbd_t **rvbd)
|
---|
49 | {
|
---|
50 | vbd_t *vbd;
|
---|
51 | service_id_t vbd_svcid;
|
---|
52 | int rc;
|
---|
53 |
|
---|
54 | vbd = calloc(1, sizeof(vbd_t));
|
---|
55 | if (vbd == NULL) {
|
---|
56 | rc = ENOMEM;
|
---|
57 | goto error;
|
---|
58 | }
|
---|
59 |
|
---|
60 | rc = loc_service_get_id(SERVICE_NAME_VBD, &vbd_svcid,
|
---|
61 | IPC_FLAG_BLOCKING);
|
---|
62 | if (rc != EOK) {
|
---|
63 | rc = EIO;
|
---|
64 | goto error;
|
---|
65 | }
|
---|
66 |
|
---|
67 | vbd->sess = loc_service_connect(EXCHANGE_SERIALIZE, vbd_svcid,
|
---|
68 | IPC_FLAG_BLOCKING);
|
---|
69 | if (vbd->sess == NULL) {
|
---|
70 | rc = EIO;
|
---|
71 | goto error;
|
---|
72 | }
|
---|
73 |
|
---|
74 | *rvbd = vbd;
|
---|
75 | return EOK;
|
---|
76 | error:
|
---|
77 | free(vbd);
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | void vbd_destroy(vbd_t *vbd)
|
---|
82 | {
|
---|
83 | if (vbd == NULL)
|
---|
84 | return;
|
---|
85 |
|
---|
86 | async_hangup(vbd->sess);
|
---|
87 | free(vbd);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Get list of partitions as array of service IDs.
|
---|
91 | *
|
---|
92 | * @param vbd Virtual block device service
|
---|
93 | * @param data Place to store pointer to array
|
---|
94 | * @param count Place to store length of array (number of entries)
|
---|
95 | *
|
---|
96 | * @return EOK on success or negative error code
|
---|
97 | */
|
---|
98 | int vbd_get_disks(vbd_t *vbd, service_id_t **data, size_t *count)
|
---|
99 | {
|
---|
100 | return vbd_get_ids_internal(vbd, VBD_GET_DISKS, 0, data, count);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Get disk information. */
|
---|
104 | int vbd_disk_info(vbd_t *vbd, service_id_t sid, vbd_disk_info_t *vinfo)
|
---|
105 | {
|
---|
106 | async_exch_t *exch;
|
---|
107 | sysarg_t retval;
|
---|
108 | ipc_call_t answer;
|
---|
109 |
|
---|
110 | exch = async_exchange_begin(vbd->sess);
|
---|
111 | aid_t req = async_send_1(exch, VBD_DISK_INFO, sid, &answer);
|
---|
112 | int rc = async_data_read_start(exch, vinfo, sizeof(vbd_disk_info_t));
|
---|
113 | async_exchange_end(exch);
|
---|
114 |
|
---|
115 | if (rc != EOK) {
|
---|
116 | async_forget(req);
|
---|
117 | return EIO;
|
---|
118 | }
|
---|
119 |
|
---|
120 | async_wait_for(req, &retval);
|
---|
121 | if (retval != EOK)
|
---|
122 | return EIO;
|
---|
123 |
|
---|
124 | return EOK;
|
---|
125 | }
|
---|
126 |
|
---|
127 | int vbd_label_create(vbd_t *vbd, service_id_t sid, label_type_t ltype)
|
---|
128 | {
|
---|
129 | async_exch_t *exch;
|
---|
130 | int retval;
|
---|
131 |
|
---|
132 | exch = async_exchange_begin(vbd->sess);
|
---|
133 | retval = async_req_2_0(exch, VBD_LABEL_CREATE, sid, ltype);
|
---|
134 | async_exchange_end(exch);
|
---|
135 |
|
---|
136 | if (retval != EOK)
|
---|
137 | return EIO;
|
---|
138 |
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | int vbd_label_delete(vbd_t *vbd, service_id_t sid)
|
---|
143 | {
|
---|
144 | async_exch_t *exch;
|
---|
145 | int retval;
|
---|
146 |
|
---|
147 | exch = async_exchange_begin(vbd->sess);
|
---|
148 | retval = async_req_1_0(exch, VBD_LABEL_DELETE, sid);
|
---|
149 | async_exchange_end(exch);
|
---|
150 |
|
---|
151 | if (retval != EOK)
|
---|
152 | return EIO;
|
---|
153 |
|
---|
154 | return EOK;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Get list of IDs into a buffer of fixed size.
|
---|
158 | *
|
---|
159 | * @param vbd Virtual Block Device
|
---|
160 | * @param method IPC method
|
---|
161 | * @param arg1 First argument
|
---|
162 | * @param id_buf Buffer to store IDs
|
---|
163 | * @param buf_size Buffer size
|
---|
164 | * @param act_size Place to store actual size of complete data.
|
---|
165 | *
|
---|
166 | * @return EOK on success or negative error code.
|
---|
167 | */
|
---|
168 | static int vbd_get_ids_once(vbd_t *vbd, sysarg_t method, sysarg_t arg1,
|
---|
169 | sysarg_t *id_buf, size_t buf_size, size_t *act_size)
|
---|
170 | {
|
---|
171 | async_exch_t *exch = async_exchange_begin(vbd->sess);
|
---|
172 |
|
---|
173 | ipc_call_t answer;
|
---|
174 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
175 | int rc = async_data_read_start(exch, id_buf, buf_size);
|
---|
176 |
|
---|
177 | async_exchange_end(exch);
|
---|
178 |
|
---|
179 | if (rc != EOK) {
|
---|
180 | async_forget(req);
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 | sysarg_t retval;
|
---|
185 | async_wait_for(req, &retval);
|
---|
186 |
|
---|
187 | if (retval != EOK) {
|
---|
188 | return retval;
|
---|
189 | }
|
---|
190 |
|
---|
191 | *act_size = IPC_GET_ARG1(answer);
|
---|
192 | return EOK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Get list of IDs.
|
---|
196 | *
|
---|
197 | * Returns an allocated array of service IDs.
|
---|
198 | *
|
---|
199 | * @param vbd Virtual Block Device
|
---|
200 | * @param method IPC method
|
---|
201 | * @param arg1 IPC argument 1
|
---|
202 | * @param data Place to store pointer to array of IDs
|
---|
203 | * @param count Place to store number of IDs
|
---|
204 | * @return EOK on success or negative error code
|
---|
205 | */
|
---|
206 | static int vbd_get_ids_internal(vbd_t *vbd, sysarg_t method, sysarg_t arg1,
|
---|
207 | sysarg_t **data, size_t *count)
|
---|
208 | {
|
---|
209 | *data = NULL;
|
---|
210 | *count = 0;
|
---|
211 |
|
---|
212 | size_t act_size = 0;
|
---|
213 | int rc = vbd_get_ids_once(vbd, method, arg1, NULL, 0, &act_size);
|
---|
214 | if (rc != EOK)
|
---|
215 | return rc;
|
---|
216 |
|
---|
217 | size_t alloc_size = act_size;
|
---|
218 | service_id_t *ids = malloc(alloc_size);
|
---|
219 | if (ids == NULL)
|
---|
220 | return ENOMEM;
|
---|
221 |
|
---|
222 | while (true) {
|
---|
223 | rc = vbd_get_ids_once(vbd, method, arg1, ids, alloc_size,
|
---|
224 | &act_size);
|
---|
225 | if (rc != EOK)
|
---|
226 | return rc;
|
---|
227 |
|
---|
228 | if (act_size <= alloc_size)
|
---|
229 | break;
|
---|
230 |
|
---|
231 | alloc_size = act_size;
|
---|
232 | ids = realloc(ids, alloc_size);
|
---|
233 | if (ids == NULL)
|
---|
234 | return ENOMEM;
|
---|
235 | }
|
---|
236 |
|
---|
237 | *count = act_size / sizeof(service_id_t);
|
---|
238 | *data = ids;
|
---|
239 | return EOK;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** Get list of disks as array of service IDs.
|
---|
243 | *
|
---|
244 | * @param vbd Virtual Block Device
|
---|
245 | * @param data Place to store pointer to array
|
---|
246 | * @param count Place to store length of array (number of entries)
|
---|
247 | *
|
---|
248 | * @return EOK on success or negative error code
|
---|
249 | */
|
---|
250 | int vbd_label_get_parts(vbd_t *vbd, service_id_t disk,
|
---|
251 | service_id_t **data, size_t *count)
|
---|
252 | {
|
---|
253 | return vbd_get_ids_internal(vbd, VBD_LABEL_GET_PARTS, disk,
|
---|
254 | data, count);
|
---|
255 | }
|
---|
256 |
|
---|
257 | int vbd_part_get_info(vbd_t *vbd, vbd_part_id_t part, vbd_part_info_t *pinfo)
|
---|
258 | {
|
---|
259 | async_exch_t *exch;
|
---|
260 | sysarg_t retval;
|
---|
261 | ipc_call_t answer;
|
---|
262 |
|
---|
263 | exch = async_exchange_begin(vbd->sess);
|
---|
264 | aid_t req = async_send_1(exch, VBD_PART_GET_INFO, part, &answer);
|
---|
265 | int rc = async_data_read_start(exch, pinfo, sizeof(vbd_part_info_t));
|
---|
266 | async_exchange_end(exch);
|
---|
267 |
|
---|
268 | if (rc != EOK) {
|
---|
269 | async_forget(req);
|
---|
270 | return EIO;
|
---|
271 | }
|
---|
272 |
|
---|
273 | async_wait_for(req, &retval);
|
---|
274 | if (retval != EOK)
|
---|
275 | return EIO;
|
---|
276 |
|
---|
277 | return EOK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | int vbd_part_create(vbd_t *vbd, service_id_t disk, vbd_part_spec_t *pspec,
|
---|
281 | vbd_part_id_t *rpart)
|
---|
282 | {
|
---|
283 | async_exch_t *exch;
|
---|
284 | sysarg_t retval;
|
---|
285 | ipc_call_t answer;
|
---|
286 |
|
---|
287 | exch = async_exchange_begin(vbd->sess);
|
---|
288 | aid_t req = async_send_1(exch, VBD_PART_CREATE, disk, &answer);
|
---|
289 | int rc = async_data_write_start(exch, pspec, sizeof(vbd_part_spec_t));
|
---|
290 | async_exchange_end(exch);
|
---|
291 |
|
---|
292 | if (rc != EOK) {
|
---|
293 | async_forget(req);
|
---|
294 | return EIO;
|
---|
295 | }
|
---|
296 |
|
---|
297 | async_wait_for(req, &retval);
|
---|
298 | if (retval != EOK)
|
---|
299 | return EIO;
|
---|
300 |
|
---|
301 | *rpart = (vbd_part_id_t)IPC_GET_ARG1(answer);
|
---|
302 | return EOK;
|
---|
303 |
|
---|
304 | }
|
---|
305 |
|
---|
306 | int vbd_part_delete(vbd_t *vbd, vbd_part_id_t part)
|
---|
307 | {
|
---|
308 | async_exch_t *exch;
|
---|
309 | int retval;
|
---|
310 |
|
---|
311 | exch = async_exchange_begin(vbd->sess);
|
---|
312 | retval = async_req_1_0(exch, VBD_PART_DELETE, part);
|
---|
313 | async_exchange_end(exch);
|
---|
314 |
|
---|
315 | if (retval != EOK)
|
---|
316 | return EIO;
|
---|
317 |
|
---|
318 | return EOK;
|
---|
319 | }
|
---|
320 |
|
---|
321 | void vbd_pspec_init(vbd_part_spec_t *pspec)
|
---|
322 | {
|
---|
323 | memset(pspec, 0, sizeof(vbd_part_spec_t));
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** Suggest partition type based on partition content.
|
---|
327 | *
|
---|
328 | * @param vbd Virtual Block Device
|
---|
329 | * @param disk Disk on which the partition will be created
|
---|
330 | * @param pcnt Partition content
|
---|
331 | * @param ptype Place to store suggested partition type
|
---|
332 | *
|
---|
333 | * @return EOK on success or negative error code
|
---|
334 | */
|
---|
335 | int vbd_suggest_ptype(vbd_t *vbd, service_id_t disk, label_pcnt_t pcnt,
|
---|
336 | label_ptype_t *ptype)
|
---|
337 | {
|
---|
338 | async_exch_t *exch;
|
---|
339 | sysarg_t retval;
|
---|
340 | ipc_call_t answer;
|
---|
341 |
|
---|
342 | exch = async_exchange_begin(vbd->sess);
|
---|
343 | aid_t req = async_send_2(exch, VBD_SUGGEST_PTYPE, disk, pcnt, &answer);
|
---|
344 | int rc = async_data_read_start(exch, ptype, sizeof(label_ptype_t));
|
---|
345 | async_exchange_end(exch);
|
---|
346 |
|
---|
347 | if (rc != EOK) {
|
---|
348 | async_forget(req);
|
---|
349 | return EIO;
|
---|
350 | }
|
---|
351 |
|
---|
352 | async_wait_for(req, &retval);
|
---|
353 | if (retval != EOK)
|
---|
354 | return EIO;
|
---|
355 |
|
---|
356 | return EOK;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | /** @}
|
---|
361 | */
|
---|