[372df8f] | 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>
|
---|
[c1694b6b] | 39 | #include <str_error.h>
|
---|
[372df8f] | 40 | #include <fibril_synch.h>
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 | #include <loc.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <str.h>
|
---|
[d2c8533] | 45 | #include <vfs/vfs.h>
|
---|
[372df8f] | 46 |
|
---|
[4b6635a7] | 47 | #include "empty.h"
|
---|
[44fe800] | 48 | #include "mkfs.h"
|
---|
[372df8f] | 49 | #include "part.h"
|
---|
| 50 | #include "types/part.h"
|
---|
| 51 |
|
---|
[b7fd2a0] | 52 | static errno_t vol_part_add_locked(service_id_t);
|
---|
[1a9174e] | 53 | static void vol_part_remove_locked(vol_part_t *);
|
---|
| 54 | static errno_t vol_part_find_by_id_ref_locked(service_id_t, vol_part_t **);
|
---|
| 55 |
|
---|
[372df8f] | 56 | static LIST_INITIALIZE(vol_parts); /* of vol_part_t */
|
---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(vol_parts_lock);
|
---|
| 58 |
|
---|
[395df52] | 59 | struct fsname_type {
|
---|
| 60 | const char *name;
|
---|
| 61 | vol_fstype_t fstype;
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | static struct fsname_type fstab[] = {
|
---|
| 65 | { "ext4fs", fs_ext4 },
|
---|
| 66 | { "cdfs", fs_cdfs },
|
---|
| 67 | { "exfat", fs_exfat },
|
---|
| 68 | { "fat", fs_fat },
|
---|
| 69 | { "mfs", fs_minix },
|
---|
| 70 | { NULL, 0 }
|
---|
| 71 | };
|
---|
| 72 |
|
---|
[5f36841] | 73 | static const char *fstype_str(vol_fstype_t fstype)
|
---|
| 74 | {
|
---|
| 75 | struct fsname_type *fst;
|
---|
| 76 |
|
---|
| 77 | fst = &fstab[0];
|
---|
| 78 | while (fst->name != NULL) {
|
---|
| 79 | if (fst->fstype == fstype)
|
---|
| 80 | return fst->name;
|
---|
| 81 | ++fst;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | assert(false);
|
---|
| 85 | return NULL;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1a9174e] | 88 | /** Check for new and removed partitions */
|
---|
[b7fd2a0] | 89 | static errno_t vol_part_check_new(void)
|
---|
[372df8f] | 90 | {
|
---|
| 91 | bool already_known;
|
---|
[1a9174e] | 92 | bool still_exists;
|
---|
[372df8f] | 93 | category_id_t part_cat;
|
---|
| 94 | service_id_t *svcs;
|
---|
| 95 | size_t count, i;
|
---|
[1a9174e] | 96 | link_t *cur, *next;
|
---|
| 97 | vol_part_t *part;
|
---|
[b7fd2a0] | 98 | errno_t rc;
|
---|
[372df8f] | 99 |
|
---|
| 100 | fibril_mutex_lock(&vol_parts_lock);
|
---|
| 101 |
|
---|
| 102 | rc = loc_category_get_id("partition", &part_cat, IPC_FLAG_BLOCKING);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'partition'.");
|
---|
| 105 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 106 | return ENOENT;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | rc = loc_category_get_svcs(part_cat, &svcs, &count);
|
---|
| 110 | if (rc != EOK) {
|
---|
| 111 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of partition "
|
---|
| 112 | "devices.");
|
---|
| 113 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 114 | return EIO;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[1a9174e] | 117 | /* Check for new partitions */
|
---|
[372df8f] | 118 | for (i = 0; i < count; i++) {
|
---|
| 119 | already_known = false;
|
---|
| 120 |
|
---|
[1a9174e] | 121 | // XXX Make this faster
|
---|
[372df8f] | 122 | list_foreach(vol_parts, lparts, vol_part_t, part) {
|
---|
| 123 | if (part->svc_id == svcs[i]) {
|
---|
| 124 | already_known = true;
|
---|
| 125 | break;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | if (!already_known) {
|
---|
| 130 | log_msg(LOG_DEFAULT, LVL_NOTE, "Found partition '%lu'",
|
---|
| 131 | (unsigned long) svcs[i]);
|
---|
[edebb4a1] | 132 | rc = vol_part_add_locked(svcs[i]);
|
---|
[372df8f] | 133 | if (rc != EOK) {
|
---|
| 134 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not add "
|
---|
| 135 | "partition.");
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1a9174e] | 140 | /* Check for removed partitions */
|
---|
| 141 | cur = list_first(&vol_parts);
|
---|
| 142 | while (cur != NULL) {
|
---|
| 143 | next = list_next(cur, &vol_parts);
|
---|
| 144 | part = list_get_instance(cur, vol_part_t, lparts);
|
---|
| 145 |
|
---|
| 146 | still_exists = false;
|
---|
| 147 | // XXX Make this faster
|
---|
| 148 | for (i = 0; i < count; i++) {
|
---|
| 149 | if (part->svc_id == svcs[i]) {
|
---|
| 150 | still_exists = true;
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | if (!still_exists) {
|
---|
| 156 | log_msg(LOG_DEFAULT, LVL_NOTE, "Partition '%zu' is gone",
|
---|
| 157 | part->svc_id);
|
---|
| 158 | vol_part_remove_locked(part);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | cur = next;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | free(svcs);
|
---|
| 165 |
|
---|
[372df8f] | 166 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 167 | return EOK;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | static vol_part_t *vol_part_new(void)
|
---|
| 171 | {
|
---|
| 172 | vol_part_t *part = calloc(1, sizeof(vol_part_t));
|
---|
| 173 |
|
---|
| 174 | if (part == NULL) {
|
---|
| 175 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating partition "
|
---|
| 176 | "structure. Out of memory.");
|
---|
| 177 | return NULL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[1a9174e] | 180 | atomic_set(&part->refcnt, 1);
|
---|
[372df8f] | 181 | link_initialize(&part->lparts);
|
---|
[0ecfc62] | 182 | part->pcnt = vpc_empty;
|
---|
[372df8f] | 183 |
|
---|
| 184 | return part;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | static void vol_part_delete(vol_part_t *part)
|
---|
| 188 | {
|
---|
[1a9174e] | 189 | log_msg(LOG_DEFAULT, LVL_ERROR, "Freeing partition %p", part);
|
---|
[372df8f] | 190 | if (part == NULL)
|
---|
| 191 | return;
|
---|
| 192 |
|
---|
[db9c889] | 193 | free(part->cur_mp);
|
---|
[372df8f] | 194 | free(part->svc_name);
|
---|
| 195 | free(part);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[b7fd2a0] | 198 | static errno_t vol_part_probe(vol_part_t *part)
|
---|
[372df8f] | 199 | {
|
---|
[4b6635a7] | 200 | bool empty;
|
---|
[d2c8533] | 201 | vfs_fs_probe_info_t info;
|
---|
[395df52] | 202 | struct fsname_type *fst;
|
---|
[9c2c7d2] | 203 | char *label;
|
---|
[b7fd2a0] | 204 | errno_t rc;
|
---|
[372df8f] | 205 |
|
---|
[d2c8533] | 206 | log_msg(LOG_DEFAULT, LVL_NOTE, "Probe partition %s", part->svc_name);
|
---|
[395df52] | 207 |
|
---|
[9c2c7d2] | 208 | assert(fibril_mutex_is_locked(&vol_parts_lock));
|
---|
| 209 |
|
---|
[395df52] | 210 | fst = &fstab[0];
|
---|
| 211 | while (fst->name != NULL) {
|
---|
[9c2c7d2] | 212 | rc = vfs_fsprobe(fst->name, part->svc_id, &info);
|
---|
[395df52] | 213 | if (rc == EOK)
|
---|
| 214 | break;
|
---|
| 215 | ++fst;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | if (fst->name != NULL) {
|
---|
[9c96634] | 219 | log_msg(LOG_DEFAULT, LVL_NOTE, "Found %s, label '%s'",
|
---|
| 220 | fst->name, info.label);
|
---|
[9c2c7d2] | 221 | label = str_dup(info.label);
|
---|
| 222 | if (label == NULL) {
|
---|
| 223 | rc = ENOMEM;
|
---|
| 224 | goto error;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[d2c8533] | 227 | part->pcnt = vpc_fs;
|
---|
[395df52] | 228 | part->fstype = fst->fstype;
|
---|
[9c2c7d2] | 229 | part->label = label;
|
---|
[d2c8533] | 230 | } else {
|
---|
| 231 | log_msg(LOG_DEFAULT, LVL_NOTE, "Partition does not contain "
|
---|
| 232 | "a recognized file system.");
|
---|
| 233 |
|
---|
[9c2c7d2] | 234 | rc = volsrv_part_is_empty(part->svc_id, &empty);
|
---|
[d2c8533] | 235 | if (rc != EOK) {
|
---|
| 236 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determining if "
|
---|
| 237 | "partition is empty.");
|
---|
[9c2c7d2] | 238 | rc = EIO;
|
---|
[d2c8533] | 239 | goto error;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[9c2c7d2] | 242 | label = str_dup("");
|
---|
| 243 | if (label == NULL) {
|
---|
| 244 | rc = ENOMEM;
|
---|
[d858a660] | 245 | goto error;
|
---|
[9c2c7d2] | 246 | }
|
---|
| 247 |
|
---|
| 248 | part->pcnt = empty ? vpc_empty : vpc_unknown;
|
---|
| 249 | part->label = label;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | return EOK;
|
---|
| 253 |
|
---|
| 254 | error:
|
---|
| 255 | return rc;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[db9c889] | 258 | static errno_t vol_part_mount(vol_part_t *part)
|
---|
[5f36841] | 259 | {
|
---|
| 260 | char *mp;
|
---|
[db9c889] | 261 | int err;
|
---|
| 262 | errno_t rc;
|
---|
[5f36841] | 263 |
|
---|
| 264 | if (str_size(part->label) < 1) {
|
---|
| 265 | /* Don't mount nameless volumes */
|
---|
| 266 | log_msg(LOG_DEFAULT, LVL_NOTE, "Not mounting nameless partition.");
|
---|
| 267 | return EOK;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | log_msg(LOG_DEFAULT, LVL_NOTE, "Determine MP label='%s'", part->label);
|
---|
[db9c889] | 271 | err = asprintf(&mp, "/vol/%s", part->label);
|
---|
| 272 | if (err < 0) {
|
---|
| 273 | log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory");
|
---|
[5f36841] | 274 | return ENOMEM;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | log_msg(LOG_DEFAULT, LVL_NOTE, "Create mount point '%s'", mp);
|
---|
| 278 | rc = vfs_link_path(mp, KIND_DIRECTORY, NULL);
|
---|
| 279 | if (rc != EOK) {
|
---|
| 280 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating mount point '%s'",
|
---|
| 281 | mp);
|
---|
| 282 | free(mp);
|
---|
| 283 | return EIO;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | log_msg(LOG_DEFAULT, LVL_NOTE, "Call vfs_mount_path mp='%s' fstype='%s' svc_name='%s'",
|
---|
| 287 | mp, fstype_str(part->fstype), part->svc_name);
|
---|
| 288 | rc = vfs_mount_path(mp, fstype_str(part->fstype),
|
---|
| 289 | part->svc_name, "", 0, 0);
|
---|
| 290 | if (rc != EOK) {
|
---|
| 291 | log_msg(LOG_DEFAULT, LVL_NOTE, "Failed mounting to %s", mp);
|
---|
| 292 | }
|
---|
| 293 | log_msg(LOG_DEFAULT, LVL_NOTE, "Mount to %s -> %d\n", mp, rc);
|
---|
| 294 |
|
---|
[db9c889] | 295 | part->cur_mp = mp;
|
---|
| 296 | part->cur_mp_auto = true;
|
---|
| 297 |
|
---|
[5f36841] | 298 | return rc;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[b7fd2a0] | 301 | static errno_t vol_part_add_locked(service_id_t sid)
|
---|
[9c2c7d2] | 302 | {
|
---|
| 303 | vol_part_t *part;
|
---|
[b7fd2a0] | 304 | errno_t rc;
|
---|
[9c2c7d2] | 305 |
|
---|
| 306 | assert(fibril_mutex_is_locked(&vol_parts_lock));
|
---|
[5f36841] | 307 | log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_add_locked(%zu)", sid);
|
---|
[9c2c7d2] | 308 |
|
---|
| 309 | /* Check for duplicates */
|
---|
[1a9174e] | 310 | rc = vol_part_find_by_id_ref_locked(sid, &part);
|
---|
| 311 | if (rc == EOK) {
|
---|
| 312 | vol_part_del_ref(part);
|
---|
[9c2c7d2] | 313 | return EEXIST;
|
---|
[1a9174e] | 314 | }
|
---|
[9c2c7d2] | 315 |
|
---|
[5f36841] | 316 | log_msg(LOG_DEFAULT, LVL_NOTE, "partition %zu is new", sid);
|
---|
| 317 |
|
---|
[9c2c7d2] | 318 | part = vol_part_new();
|
---|
| 319 | if (part == NULL)
|
---|
| 320 | return ENOMEM;
|
---|
| 321 |
|
---|
| 322 | part->svc_id = sid;
|
---|
| 323 |
|
---|
| 324 | rc = loc_service_get_name(sid, &part->svc_name);
|
---|
| 325 | if (rc != EOK) {
|
---|
| 326 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
---|
| 327 | goto error;
|
---|
[4b6635a7] | 328 | }
|
---|
[372df8f] | 329 |
|
---|
[9c2c7d2] | 330 | rc = vol_part_probe(part);
|
---|
| 331 | if (rc != EOK)
|
---|
| 332 | goto error;
|
---|
| 333 |
|
---|
[5f36841] | 334 | rc = vol_part_mount(part);
|
---|
| 335 | if (rc != EOK)
|
---|
| 336 | goto error;
|
---|
| 337 |
|
---|
[372df8f] | 338 | list_append(&part->lparts, &vol_parts);
|
---|
| 339 |
|
---|
[edebb4a1] | 340 | log_msg(LOG_DEFAULT, LVL_NOTE, "Added partition %zu", part->svc_id);
|
---|
| 341 |
|
---|
[372df8f] | 342 | return EOK;
|
---|
| 343 |
|
---|
| 344 | error:
|
---|
| 345 | vol_part_delete(part);
|
---|
| 346 | return rc;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[1a9174e] | 349 | static void vol_part_remove_locked(vol_part_t *part)
|
---|
| 350 | {
|
---|
| 351 | assert(fibril_mutex_is_locked(&vol_parts_lock));
|
---|
| 352 | log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_remove_locked(%zu)", part->svc_id);
|
---|
| 353 |
|
---|
| 354 | list_remove(&part->lparts);
|
---|
| 355 |
|
---|
| 356 | log_msg(LOG_DEFAULT, LVL_NOTE, "Removed partition.");
|
---|
| 357 | vol_part_del_ref(part);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[b7fd2a0] | 360 | errno_t vol_part_add(service_id_t sid)
|
---|
[edebb4a1] | 361 | {
|
---|
[b7fd2a0] | 362 | errno_t rc;
|
---|
[edebb4a1] | 363 |
|
---|
| 364 | fibril_mutex_lock(&vol_parts_lock);
|
---|
| 365 | rc = vol_part_add_locked(sid);
|
---|
| 366 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 367 |
|
---|
| 368 | return rc;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[372df8f] | 371 | static void vol_part_cat_change_cb(void)
|
---|
| 372 | {
|
---|
| 373 | (void) vol_part_check_new();
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[b7fd2a0] | 376 | errno_t vol_part_init(void)
|
---|
[372df8f] | 377 | {
|
---|
| 378 | return EOK;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[b7fd2a0] | 381 | errno_t vol_part_discovery_start(void)
|
---|
[372df8f] | 382 | {
|
---|
[b7fd2a0] | 383 | errno_t rc;
|
---|
[372df8f] | 384 |
|
---|
| 385 | rc = loc_register_cat_change_cb(vol_part_cat_change_cb);
|
---|
| 386 | if (rc != EOK) {
|
---|
| 387 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback "
|
---|
[c1694b6b] | 388 | "for partition discovery: %s.", str_error(rc));
|
---|
[372df8f] | 389 | return rc;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | return vol_part_check_new();
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /** Get list of partitions as array of service IDs. */
|
---|
[b7fd2a0] | 396 | errno_t vol_part_get_ids(service_id_t *id_buf, size_t buf_size, size_t *act_size)
|
---|
[372df8f] | 397 | {
|
---|
| 398 | size_t act_cnt;
|
---|
| 399 | size_t buf_cnt;
|
---|
| 400 |
|
---|
| 401 | fibril_mutex_lock(&vol_parts_lock);
|
---|
| 402 |
|
---|
| 403 | buf_cnt = buf_size / sizeof(service_id_t);
|
---|
| 404 |
|
---|
| 405 | act_cnt = list_count(&vol_parts);
|
---|
| 406 | *act_size = act_cnt * sizeof(service_id_t);
|
---|
| 407 |
|
---|
| 408 | if (buf_size % sizeof(service_id_t) != 0) {
|
---|
| 409 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 410 | return EINVAL;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | size_t pos = 0;
|
---|
| 414 | list_foreach(vol_parts, lparts, vol_part_t, part) {
|
---|
| 415 | if (pos < buf_cnt)
|
---|
| 416 | id_buf[pos] = part->svc_id;
|
---|
| 417 | pos++;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 421 | return EOK;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[1a9174e] | 424 | static errno_t vol_part_find_by_id_ref_locked(service_id_t sid,
|
---|
| 425 | vol_part_t **rpart)
|
---|
[372df8f] | 426 | {
|
---|
[1a9174e] | 427 | assert(fibril_mutex_is_locked(&vol_parts_lock));
|
---|
| 428 |
|
---|
[372df8f] | 429 | list_foreach(vol_parts, lparts, vol_part_t, part) {
|
---|
| 430 | if (part->svc_id == sid) {
|
---|
[1a9174e] | 431 | /* Add reference */
|
---|
| 432 | atomic_inc(&part->refcnt);
|
---|
[372df8f] | 433 | *rpart = part;
|
---|
| 434 | return EOK;
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | return ENOENT;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
[1a9174e] | 441 | errno_t vol_part_find_by_id_ref(service_id_t sid, vol_part_t **rpart)
|
---|
| 442 | {
|
---|
| 443 | errno_t rc;
|
---|
| 444 |
|
---|
| 445 | fibril_mutex_lock(&vol_parts_lock);
|
---|
| 446 | rc = vol_part_find_by_id_ref_locked(sid, rpart);
|
---|
| 447 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 448 |
|
---|
| 449 | return rc;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | void vol_part_del_ref(vol_part_t *part)
|
---|
| 453 | {
|
---|
| 454 | if (atomic_predec(&part->refcnt) == 0)
|
---|
| 455 | vol_part_delete(part);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
[db9c889] | 458 | errno_t vol_part_eject_part(vol_part_t *part)
|
---|
| 459 | {
|
---|
| 460 | int rc;
|
---|
| 461 |
|
---|
| 462 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_eject_part()");
|
---|
| 463 |
|
---|
| 464 | if (part->cur_mp == NULL) {
|
---|
| 465 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Attempt to mount unmounted "
|
---|
| 466 | "partition.");
|
---|
| 467 | return EINVAL;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | rc = vfs_unmount_path(part->cur_mp);
|
---|
| 471 | if (rc != EOK) {
|
---|
| 472 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unmounting partition "
|
---|
| 473 | "from %s", part->cur_mp);
|
---|
| 474 | return rc;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | if (part->cur_mp_auto) {
|
---|
| 478 | rc = vfs_unlink_path(part->cur_mp);
|
---|
| 479 | if (rc != EOK) {
|
---|
| 480 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed deleting "
|
---|
| 481 | "mount directory %s.", part->cur_mp);
|
---|
| 482 | }
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | free(part->cur_mp);
|
---|
| 486 | part->cur_mp = NULL;
|
---|
| 487 | part->cur_mp_auto = false;
|
---|
| 488 |
|
---|
| 489 | return EOK;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[b7fd2a0] | 492 | errno_t vol_part_empty_part(vol_part_t *part)
|
---|
[372df8f] | 493 | {
|
---|
[b7fd2a0] | 494 | errno_t rc;
|
---|
[ea0ff6b] | 495 |
|
---|
[55f8c6e7] | 496 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_part()");
|
---|
[ea0ff6b] | 497 |
|
---|
[44fe800] | 498 | rc = volsrv_part_empty(part->svc_id);
|
---|
[ea0ff6b] | 499 | if (rc != EOK) {
|
---|
[dd8ab1c] | 500 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_part() - failed %s",
|
---|
| 501 | str_error(rc));
|
---|
[ea0ff6b] | 502 | return rc;
|
---|
| 503 | }
|
---|
[372df8f] | 504 |
|
---|
[ea0ff6b] | 505 | part->pcnt = vpc_empty;
|
---|
[372df8f] | 506 | return EOK;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[b7fd2a0] | 509 | errno_t vol_part_mkfs_part(vol_part_t *part, vol_fstype_t fstype,
|
---|
[9c2c7d2] | 510 | const char *label)
|
---|
[44fe800] | 511 | {
|
---|
[b7fd2a0] | 512 | errno_t rc;
|
---|
[44fe800] | 513 |
|
---|
[55f8c6e7] | 514 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_part()");
|
---|
[44fe800] | 515 |
|
---|
[9c2c7d2] | 516 | fibril_mutex_lock(&vol_parts_lock);
|
---|
| 517 |
|
---|
| 518 | rc = volsrv_part_mkfs(part->svc_id, fstype, label);
|
---|
[44fe800] | 519 | if (rc != EOK) {
|
---|
[dd8ab1c] | 520 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_part() - failed %s",
|
---|
| 521 | str_error(rc));
|
---|
[9c2c7d2] | 522 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
[44fe800] | 523 | return rc;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[9c2c7d2] | 526 | /*
|
---|
| 527 | * Re-probe the partition to update information. This is needed since
|
---|
| 528 | * the FS can make conversions of the volume label (e.g. make it
|
---|
| 529 | * uppercase).
|
---|
| 530 | */
|
---|
| 531 | rc = vol_part_probe(part);
|
---|
| 532 | if (rc != EOK) {
|
---|
| 533 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 534 | return rc;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[5f36841] | 537 | rc = vol_part_mount(part);
|
---|
| 538 | if (rc != EOK) {
|
---|
| 539 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
| 540 | return rc;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[9c2c7d2] | 543 | fibril_mutex_unlock(&vol_parts_lock);
|
---|
[44fe800] | 544 | return EOK;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
[b7fd2a0] | 547 | errno_t vol_part_get_info(vol_part_t *part, vol_part_info_t *pinfo)
|
---|
[372df8f] | 548 | {
|
---|
[72c72d4] | 549 | memset(pinfo, 0, sizeof(*pinfo));
|
---|
| 550 |
|
---|
[0ecfc62] | 551 | pinfo->pcnt = part->pcnt;
|
---|
| 552 | pinfo->fstype = part->fstype;
|
---|
[d858a660] | 553 | str_cpy(pinfo->label, sizeof(pinfo->label), part->label);
|
---|
[72c72d4] | 554 | if (part->cur_mp != NULL)
|
---|
| 555 | str_cpy(pinfo->cur_mp, sizeof(pinfo->cur_mp), part->cur_mp);
|
---|
[db9c889] | 556 | pinfo->cur_mp_auto = part->cur_mp_auto;
|
---|
[372df8f] | 557 | return EOK;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | /** @}
|
---|
| 561 | */
|
---|