1 | /*
|
---|
2 | * Copyright (c) 2018 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 sysinst
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file RAM disk image manipulation
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <fibril.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <task.h>
|
---|
41 | #include <vfs/vfs.h>
|
---|
42 | #include <vol.h>
|
---|
43 |
|
---|
44 | #include "rdimg.h"
|
---|
45 |
|
---|
46 | #define FILE_BD "/srv/bd/file_bd"
|
---|
47 | #define RD_SVC "bd/iird"
|
---|
48 | #define RD_LABEL "HelenOS-rd"
|
---|
49 |
|
---|
50 | /** Find volume by volume label. */
|
---|
51 | static errno_t rd_img_part_by_label(vol_t *vol, const char *label,
|
---|
52 | service_id_t *rid)
|
---|
53 | {
|
---|
54 | vol_part_info_t vinfo;
|
---|
55 | service_id_t *part_ids = NULL;
|
---|
56 | size_t nparts;
|
---|
57 | size_t i;
|
---|
58 | errno_t rc;
|
---|
59 |
|
---|
60 | rc = vol_get_parts(vol, &part_ids, &nparts);
|
---|
61 | if (rc != EOK) {
|
---|
62 | printf("Error getting list of volumes.\n");
|
---|
63 | goto out;
|
---|
64 | }
|
---|
65 |
|
---|
66 | for (i = 0; i < nparts; i++) {
|
---|
67 | rc = vol_part_info(vol, part_ids[i], &vinfo);
|
---|
68 | if (rc != EOK) {
|
---|
69 | printf("Error getting volume information.\n");
|
---|
70 | rc = EIO;
|
---|
71 | goto out;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (str_cmp(vinfo.label, label) == 0) {
|
---|
75 | *rid = part_ids[i];
|
---|
76 | rc = EOK;
|
---|
77 | goto out;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | rc = ENOENT;
|
---|
82 | out:
|
---|
83 | free(part_ids);
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Open RAM disk image.
|
---|
88 | *
|
---|
89 | * @param imgpath Image path
|
---|
90 | * @param rpath Place to store pointer to newly allocated string, the path to
|
---|
91 | * the mounted RAM disk.
|
---|
92 | * @param rimg Place to store pointer to newly allocated object.
|
---|
93 | *
|
---|
94 | * @return EOK on success or an error code
|
---|
95 | */
|
---|
96 | errno_t rd_img_open(const char *imgpath, char **rpath, rd_img_t **rimg)
|
---|
97 | {
|
---|
98 | rd_img_t *img = NULL;
|
---|
99 | char *rdpath = NULL;
|
---|
100 | errno_t rc;
|
---|
101 | task_id_t id;
|
---|
102 | task_wait_t wait;
|
---|
103 | task_exit_t texit;
|
---|
104 | vfs_stat_t stat;
|
---|
105 | int retval;
|
---|
106 | int cnt;
|
---|
107 |
|
---|
108 | printf("rd_img_open: begin\n");
|
---|
109 | rdpath = str_dup("/vol/" RD_LABEL);
|
---|
110 | if (rdpath == NULL) {
|
---|
111 | rc = ENOMEM;
|
---|
112 | goto error;
|
---|
113 | }
|
---|
114 |
|
---|
115 | img = calloc(1, sizeof(rd_img_t));
|
---|
116 | if (img == NULL) {
|
---|
117 | rc = ENOMEM;
|
---|
118 | goto error;
|
---|
119 | }
|
---|
120 |
|
---|
121 | printf("rd_img_open: spawn file_bd\n");
|
---|
122 | rc = task_spawnl(&id, &wait, FILE_BD, FILE_BD, imgpath, RD_SVC, NULL);
|
---|
123 | if (rc != EOK) {
|
---|
124 | rc = EIO;
|
---|
125 | goto error;
|
---|
126 | }
|
---|
127 |
|
---|
128 | printf("rd_img_open: wait for file_bd\n");
|
---|
129 | rc = task_wait(&wait, &texit, &retval);
|
---|
130 | if (rc != EOK || texit != TASK_EXIT_NORMAL) {
|
---|
131 | rc = EIO;
|
---|
132 | goto error;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* Wait for the RAM disk to become available */
|
---|
136 | printf("rd_img_open: wait for RAM disk to be mounted\n");
|
---|
137 | cnt = 10;
|
---|
138 | while (cnt > 0) {
|
---|
139 | rc = vfs_stat_path(rdpath, &stat);
|
---|
140 | if (rc == EOK)
|
---|
141 | break;
|
---|
142 |
|
---|
143 | fibril_sleep(1);
|
---|
144 | --cnt;
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (cnt == 0) {
|
---|
148 | printf("rd_img_open: ran out of time\n");
|
---|
149 | rc = EIO;
|
---|
150 | goto error;
|
---|
151 | }
|
---|
152 |
|
---|
153 | img->filebd_tid = id;
|
---|
154 | *rimg = img;
|
---|
155 | *rpath = rdpath;
|
---|
156 | printf("rd_img_open: success\n");
|
---|
157 | return EOK;
|
---|
158 | error:
|
---|
159 | if (rdpath != NULL)
|
---|
160 | free(rdpath);
|
---|
161 | if (img != NULL)
|
---|
162 | free(img);
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Close RAM disk image.
|
---|
167 | *
|
---|
168 | */
|
---|
169 | errno_t rd_img_close(rd_img_t *img)
|
---|
170 | {
|
---|
171 | errno_t rc;
|
---|
172 | service_id_t rd_svcid;
|
---|
173 | vol_t *vol = NULL;
|
---|
174 |
|
---|
175 | printf("rd_img_close: begin\n");
|
---|
176 |
|
---|
177 | rc = vol_create(&vol);
|
---|
178 | if (rc != EOK) {
|
---|
179 | printf("Error opening volume management service.\n");
|
---|
180 | rc = EIO;
|
---|
181 | goto error;
|
---|
182 | }
|
---|
183 |
|
---|
184 | printf("rd_img_close: Find RAM disk volume.\n");
|
---|
185 | rc = rd_img_part_by_label(vol, RD_LABEL, &rd_svcid);
|
---|
186 | if (rc != EOK) {
|
---|
187 | printf("Error getting RAM disk service ID.\n");
|
---|
188 | rc = EIO;
|
---|
189 | goto error;
|
---|
190 | }
|
---|
191 |
|
---|
192 | printf("rd_img_close: eject RAM disk volume\n");
|
---|
193 | rc = vol_part_eject(vol, rd_svcid);
|
---|
194 | if (rc != EOK) {
|
---|
195 | printf("Error ejecting RAM disk volume.\n");
|
---|
196 | rc = EIO;
|
---|
197 | goto error;
|
---|
198 | }
|
---|
199 |
|
---|
200 | vol_destroy(vol);
|
---|
201 |
|
---|
202 | rc = task_kill(img->filebd_tid);
|
---|
203 | if (rc != EOK) {
|
---|
204 | printf("Error killing file_bd.\n");
|
---|
205 | rc = EIO;
|
---|
206 | goto error;
|
---|
207 | }
|
---|
208 |
|
---|
209 | free(img);
|
---|
210 | printf("rd_img_close: success\n");
|
---|
211 | return EOK;
|
---|
212 | error:
|
---|
213 | free(img);
|
---|
214 | if (vol != NULL)
|
---|
215 | vol_destroy(vol);
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** @}
|
---|
220 | */
|
---|