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