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 volsrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Partition handling
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <stdbool.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <vfs/vfs.h>
|
---|
45 |
|
---|
46 | #include "empty.h"
|
---|
47 | #include "mkfs.h"
|
---|
48 | #include "part.h"
|
---|
49 | #include "types/part.h"
|
---|
50 |
|
---|
51 | static int vol_part_add_locked(service_id_t);
|
---|
52 | static LIST_INITIALIZE(vol_parts); /* of vol_part_t */
|
---|
53 | static FIBRIL_MUTEX_INITIALIZE(vol_parts_lock);
|
---|
54 |
|
---|
55 | struct fsname_type {
|
---|
56 | const char *name;
|
---|
57 | vol_fstype_t fstype;
|
---|
58 | };
|
---|
59 |
|
---|
60 | static struct fsname_type fstab[] = {
|
---|
61 | { "ext4fs", fs_ext4 },
|
---|
62 | { "cdfs", fs_cdfs },
|
---|
63 | { "exfat", fs_exfat },
|
---|
64 | { "fat", fs_fat },
|
---|
65 | { "mfs", fs_minix },
|
---|
66 | { NULL, 0 }
|
---|
67 | };
|
---|
68 |
|
---|
69 | /** Check for new partitions */
|
---|
70 | static int vol_part_check_new(void)
|
---|
71 | {
|
---|
72 | bool already_known;
|
---|
73 | category_id_t part_cat;
|
---|
74 | service_id_t *svcs;
|
---|
75 | size_t count, i;
|
---|
76 | int rc;
|
---|
77 |
|
---|
78 | fibril_mutex_lock(&vol_parts_lock);
|
---|
79 |
|
---|
80 | rc = loc_category_get_id("partition", &part_cat, IPC_FLAG_BLOCKING);
|
---|
81 | if (rc != EOK) {
|
---|
82 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'partition'.");
|
---|
83 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
84 | return ENOENT;
|
---|
85 | }
|
---|
86 |
|
---|
87 | rc = loc_category_get_svcs(part_cat, &svcs, &count);
|
---|
88 | if (rc != EOK) {
|
---|
89 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of partition "
|
---|
90 | "devices.");
|
---|
91 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
92 | return EIO;
|
---|
93 | }
|
---|
94 |
|
---|
95 | for (i = 0; i < count; i++) {
|
---|
96 | already_known = false;
|
---|
97 |
|
---|
98 | list_foreach(vol_parts, lparts, vol_part_t, part) {
|
---|
99 | if (part->svc_id == svcs[i]) {
|
---|
100 | already_known = true;
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (!already_known) {
|
---|
106 | log_msg(LOG_DEFAULT, LVL_NOTE, "Found partition '%lu'",
|
---|
107 | (unsigned long) svcs[i]);
|
---|
108 | rc = vol_part_add_locked(svcs[i]);
|
---|
109 | if (rc != EOK) {
|
---|
110 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not add "
|
---|
111 | "partition.");
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
117 | return EOK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static vol_part_t *vol_part_new(void)
|
---|
121 | {
|
---|
122 | vol_part_t *part = calloc(1, sizeof(vol_part_t));
|
---|
123 |
|
---|
124 | if (part == NULL) {
|
---|
125 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating partition "
|
---|
126 | "structure. Out of memory.");
|
---|
127 | return NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | link_initialize(&part->lparts);
|
---|
131 | part->pcnt = vpc_empty;
|
---|
132 |
|
---|
133 | return part;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static void vol_part_delete(vol_part_t *part)
|
---|
137 | {
|
---|
138 | if (part == NULL)
|
---|
139 | return;
|
---|
140 |
|
---|
141 | free(part->svc_name);
|
---|
142 | free(part);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static int vol_part_add_locked(service_id_t sid)
|
---|
146 | {
|
---|
147 | vol_part_t *part;
|
---|
148 | bool empty;
|
---|
149 | vfs_fs_probe_info_t info;
|
---|
150 | struct fsname_type *fst;
|
---|
151 | int rc;
|
---|
152 |
|
---|
153 | assert(fibril_mutex_is_locked(&vol_parts_lock));
|
---|
154 |
|
---|
155 | /* Check for duplicates */
|
---|
156 | rc = vol_part_find_by_id(sid, &part);
|
---|
157 | if (rc == EOK)
|
---|
158 | return EEXIST;
|
---|
159 |
|
---|
160 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_add_locked()");
|
---|
161 | part = vol_part_new();
|
---|
162 | if (part == NULL)
|
---|
163 | return ENOMEM;
|
---|
164 |
|
---|
165 | part->svc_id = sid;
|
---|
166 |
|
---|
167 | rc = loc_service_get_name(sid, &part->svc_name);
|
---|
168 | if (rc != EOK) {
|
---|
169 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
---|
170 | goto error;
|
---|
171 | }
|
---|
172 |
|
---|
173 | log_msg(LOG_DEFAULT, LVL_NOTE, "Probe partition %s", part->svc_name);
|
---|
174 |
|
---|
175 | fst = &fstab[0];
|
---|
176 | while (fst->name != NULL) {
|
---|
177 | rc = vfs_fsprobe(fst->name, sid, &info);
|
---|
178 | if (rc == EOK)
|
---|
179 | break;
|
---|
180 | ++fst;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (fst->name != NULL) {
|
---|
184 | log_msg(LOG_DEFAULT, LVL_NOTE, "Found %s, label '%s'",
|
---|
185 | fst->name, info.label);
|
---|
186 | part->pcnt = vpc_fs;
|
---|
187 | part->fstype = fst->fstype;
|
---|
188 | } else {
|
---|
189 | log_msg(LOG_DEFAULT, LVL_NOTE, "Partition does not contain "
|
---|
190 | "a recognized file system.");
|
---|
191 |
|
---|
192 | rc = volsrv_part_is_empty(sid, &empty);
|
---|
193 | if (rc != EOK) {
|
---|
194 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determining if "
|
---|
195 | "partition is empty.");
|
---|
196 | goto error;
|
---|
197 | }
|
---|
198 |
|
---|
199 | part->pcnt = empty ? vpc_empty : vpc_unknown;
|
---|
200 | }
|
---|
201 |
|
---|
202 | list_append(&part->lparts, &vol_parts);
|
---|
203 |
|
---|
204 | log_msg(LOG_DEFAULT, LVL_NOTE, "Added partition %zu", part->svc_id);
|
---|
205 |
|
---|
206 | return EOK;
|
---|
207 |
|
---|
208 | error:
|
---|
209 | vol_part_delete(part);
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 | int vol_part_add(service_id_t sid)
|
---|
214 | {
|
---|
215 | int rc;
|
---|
216 |
|
---|
217 | fibril_mutex_lock(&vol_parts_lock);
|
---|
218 | rc = vol_part_add_locked(sid);
|
---|
219 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
220 |
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static void vol_part_cat_change_cb(void)
|
---|
225 | {
|
---|
226 | (void) vol_part_check_new();
|
---|
227 | }
|
---|
228 |
|
---|
229 | int vol_part_init(void)
|
---|
230 | {
|
---|
231 | return EOK;
|
---|
232 | }
|
---|
233 |
|
---|
234 | int vol_part_discovery_start(void)
|
---|
235 | {
|
---|
236 | int rc;
|
---|
237 |
|
---|
238 | rc = loc_register_cat_change_cb(vol_part_cat_change_cb);
|
---|
239 | if (rc != EOK) {
|
---|
240 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback "
|
---|
241 | "for partition discovery (%d).", rc);
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | return vol_part_check_new();
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** Get list of partitions as array of service IDs. */
|
---|
249 | int vol_part_get_ids(service_id_t *id_buf, size_t buf_size, size_t *act_size)
|
---|
250 | {
|
---|
251 | size_t act_cnt;
|
---|
252 | size_t buf_cnt;
|
---|
253 |
|
---|
254 | fibril_mutex_lock(&vol_parts_lock);
|
---|
255 |
|
---|
256 | buf_cnt = buf_size / sizeof(service_id_t);
|
---|
257 |
|
---|
258 | act_cnt = list_count(&vol_parts);
|
---|
259 | *act_size = act_cnt * sizeof(service_id_t);
|
---|
260 |
|
---|
261 | if (buf_size % sizeof(service_id_t) != 0) {
|
---|
262 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
263 | return EINVAL;
|
---|
264 | }
|
---|
265 |
|
---|
266 | size_t pos = 0;
|
---|
267 | list_foreach(vol_parts, lparts, vol_part_t, part) {
|
---|
268 | if (pos < buf_cnt)
|
---|
269 | id_buf[pos] = part->svc_id;
|
---|
270 | pos++;
|
---|
271 | }
|
---|
272 |
|
---|
273 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
274 | return EOK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | int vol_part_find_by_id(service_id_t sid, vol_part_t **rpart)
|
---|
278 | {
|
---|
279 | list_foreach(vol_parts, lparts, vol_part_t, part) {
|
---|
280 | if (part->svc_id == sid) {
|
---|
281 | *rpart = part;
|
---|
282 | /* XXX Add reference */
|
---|
283 | return EOK;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | return ENOENT;
|
---|
288 | }
|
---|
289 |
|
---|
290 | int vol_part_empty_part(vol_part_t *part)
|
---|
291 | {
|
---|
292 | int rc;
|
---|
293 |
|
---|
294 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_part()");
|
---|
295 |
|
---|
296 | rc = volsrv_part_empty(part->svc_id);
|
---|
297 | if (rc != EOK) {
|
---|
298 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_part() - failed %d",
|
---|
299 | rc);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 | part->pcnt = vpc_empty;
|
---|
304 | return EOK;
|
---|
305 | }
|
---|
306 |
|
---|
307 | int vol_part_mkfs_part(vol_part_t *part, vol_fstype_t fstype)
|
---|
308 | {
|
---|
309 | int rc;
|
---|
310 |
|
---|
311 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_part()");
|
---|
312 |
|
---|
313 | rc = volsrv_part_mkfs(part->svc_id, fstype);
|
---|
314 | if (rc != EOK) {
|
---|
315 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_part() - failed %d",
|
---|
316 | rc);
|
---|
317 | return rc;
|
---|
318 | }
|
---|
319 |
|
---|
320 | part->pcnt = vpc_fs;
|
---|
321 | part->fstype = fstype;
|
---|
322 | return EOK;
|
---|
323 | }
|
---|
324 |
|
---|
325 | int vol_part_get_info(vol_part_t *part, vol_part_info_t *pinfo)
|
---|
326 | {
|
---|
327 | pinfo->pcnt = part->pcnt;
|
---|
328 | pinfo->fstype = part->fstype;
|
---|
329 | return EOK;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /** @}
|
---|
333 | */
|
---|