1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
/*
* dm-device.c: create or remove device via device mapper API.
*
* Copyright (c) 2010 EMC Corporation, Haiying Tang <Tang_Haiying@emc.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/kdev_t.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <fcntl.h>
#include <errno.h>
#include <libdevmapper.h>
#include "device-discovery.h"
#define DM_DEV_NAME_LEN 256
#ifndef DM_MAX_TYPE_NAME
#define DM_MAX_TYPE_NAME 16
#endif
#define DM_PARAMS_LEN 512 /* XXX: is this enough for target? */
#define TYPE_HAS_DEV(type) ((type == BLOCK_VOLUME_SIMPLE) || \
(type == BLOCK_VOLUME_PSEUDO))
struct bl_dm_table {
uint64_t offset;
uint64_t size;
char target_type[DM_MAX_TYPE_NAME];
char params[DM_PARAMS_LEN];
struct bl_dm_table *next;
};
struct bl_dm_tree {
uint64_t dev;
struct dm_tree *tree;
struct bl_dm_tree *next;
};
static const char dm_name[] = "pnfs_vol_%u";
static unsigned int dev_count;
static inline struct bl_dm_table *bl_dm_table_alloc(void)
{
return (struct bl_dm_table *)calloc(1, sizeof(struct bl_dm_table));
}
static void bl_dm_table_free(struct bl_dm_table *bl_table_head)
{
struct bl_dm_table *p;
while (bl_table_head) {
p = bl_table_head->next;
free(bl_table_head);
bl_table_head = p;
}
}
static void add_to_bl_dm_table(struct bl_dm_table **bl_table_head,
struct bl_dm_table *table)
{
struct bl_dm_table *p;
if (!*bl_table_head) {
*bl_table_head = table;
return;
}
p = *bl_table_head;
while (p->next)
p = p->next;
p->next = table;
}
struct bl_dm_tree *bl_tree_head;
static struct bl_dm_tree *find_bl_dm_tree(uint64_t dev)
{
struct bl_dm_tree *p;
for (p = bl_tree_head; p; p = p->next) {
if (p->dev == dev)
break;
}
return p;
}
static void del_from_bl_dm_tree(uint64_t dev)
{
struct bl_dm_tree *p, *pre = bl_tree_head;
for (p = pre; p; p = p->next) {
if (p->dev == dev) {
pre->next = p->next;
if (p == bl_tree_head)
bl_tree_head = bl_tree_head->next;
free(p);
break;
}
pre = p;
}
}
static void add_to_bl_dm_tree(struct bl_dm_tree *tree)
{
struct bl_dm_tree *p;
if (!bl_tree_head) {
bl_tree_head = tree;
return;
}
p = bl_tree_head;
while (p->next)
p = p->next;
p->next = tree;
return;
}
/*
* Create device via device mapper
* return 0 when creation failed
* return dev no for created device
*/
static uint64_t
dm_device_create_mapped(const char *dev_name, struct bl_dm_table *p)
{
struct dm_task *dmt;
struct dm_info dminfo;
int ret = 0;
dmt = dm_task_create(DM_DEVICE_CREATE);
if (!dmt) {
BL_LOG_ERR("Create dm_task for %s failed\n", dev_name);
return 0;
}
ret = dm_task_set_name(dmt, dev_name);
if (!ret)
goto err_out;
while (p) {
ret =
dm_task_add_target(dmt, p->offset, p->size, p->target_type,
p->params);
if (!ret)
goto err_out;
p = p->next;
}
ret = dm_task_run(dmt) && dm_task_get_info(dmt, &dminfo)
&& dminfo.exists;
if (!ret)
goto err_out;
dm_task_update_nodes();
err_out:
dm_task_destroy(dmt);
if (!ret) {
BL_LOG_ERR("Create device %s failed\n", dev_name);
return 0;
}
return MKDEV(dminfo.major, dminfo.minor);
}
static int dm_device_remove_byname(const char *dev_name)
{
struct dm_task *dmt;
int ret = 0;
BL_LOG_INFO("%s: %s\n", __func__, dev_name);
dmt = dm_task_create(DM_DEVICE_REMOVE);
if (!dmt)
return 0;
ret = dm_task_set_name(dmt, dev_name) && dm_task_run(dmt);
dm_task_update_nodes();
dm_task_destroy(dmt);
return ret;
}
static int dm_device_remove(uint64_t dev)
{
struct dm_task *dmt;
struct dm_names *dmnames;
char *name = NULL;
int ret = 0;
/* Look for dev_name via dev, if dev_name could be transferred here,
we could jump to DM_DEVICE_REMOVE directly */
dmt = dm_task_create(DM_DEVICE_LIST);
if (!dmt) {
BL_LOG_ERR("dm_task creation failed\n");
goto out;
}
ret = dm_task_run(dmt);
if (!ret) {
BL_LOG_ERR("dm_task_run failed\n");
goto out;
}
dmnames = dm_task_get_names(dmt);
if (!dmnames || !dmnames->dev) {
BL_LOG_ERR("dm_task_get_names failed\n");
goto out;
}
while (dmnames) {
if (dmnames->dev == dev) {
name = strdup(dmnames->name);
break;
}
dmnames = (void *)dmnames + dmnames->next;
}
if (!name) {
BL_LOG_ERR("Could not find device\n");
goto out;
}
dm_task_update_nodes();
out:
if (dmt)
dm_task_destroy(dmt);
/* Start to remove device */
if (name) {
ret = dm_device_remove_byname(name);
free(name);
}
return ret;
}
static void dm_devicelist_remove(unsigned int start, unsigned int end)
{
char dev_name[DM_DEV_NAME_LEN];
unsigned int count;
if (start >= dev_count || end <= 1 || start >= end - 1)
return;
for (count = end - 1; count > start; count--) {
snprintf(dev_name, sizeof dev_name, dm_name, count - 1);
dm_device_remove_byname(dev_name);
}
return;
}
static void bl_dm_remove_tree(uint64_t dev)
{
struct bl_dm_tree *p;
p = find_bl_dm_tree(dev);
if (!p)
return;
dm_tree_free(p->tree);
del_from_bl_dm_tree(dev);
}
static int bl_dm_create_tree(uint64_t dev)
{
struct dm_tree *tree;
struct bl_dm_tree *bl_tree;
bl_tree = find_bl_dm_tree(dev);
if (bl_tree)
return 1;
tree = dm_tree_create();
if (!tree)
return 0;
if (!dm_tree_add_dev(tree, MAJOR(dev), MINOR(dev))) {
dm_tree_free(tree);
return 0;
}
bl_tree = malloc(sizeof(struct bl_dm_tree));
if (!bl_tree) {
dm_tree_free(tree);
return 0;
}
bl_tree->dev = dev;
bl_tree->tree = tree;
bl_tree->next = NULL;
add_to_bl_dm_tree(bl_tree);
return 1;
}
int dm_device_remove_all(uint64_t *dev)
{
struct bl_dm_tree *p;
struct dm_tree_node *node;
const char *uuid;
int ret = 0;
uint32_t major, minor;
uint64_t bl_dev;
memcpy(&major, dev, sizeof(uint32_t));
memcpy(&minor, (void *)dev + sizeof(uint32_t), sizeof(uint32_t));
bl_dev = MKDEV(major, minor);
p = find_bl_dm_tree(bl_dev);
if (!p)
return ret;
node = dm_tree_find_node(p->tree, MAJOR(bl_dev), MINOR(bl_dev));
if (!node)
return ret;
uuid = dm_tree_node_get_uuid(node);
if (!uuid)
return ret;
dm_device_remove(bl_dev);
ret = dm_tree_deactivate_children(node, uuid, strlen(uuid));
dm_task_update_nodes();
bl_dm_remove_tree(bl_dev);
return ret;
}
static int dm_device_exists(char *dev_name)
{
char fullname[DM_DEV_NAME_LEN];
snprintf(fullname, sizeof fullname, "/dev/mapper/%s", dev_name);
return (access(fullname, F_OK) >= 0);
}
/* TODO: check the value for DM_DEV_NAME_LEN, DM_TYPE_LEN, DM_PARAMS_LEN */
uint64_t dm_device_create(struct bl_volume *vols, int num_vols)
{
uint64_t size, stripe_unit, dev = 0;
unsigned int count = dev_count;
int volnum, i, pos;
struct bl_volume *node;
char *tmp;
struct bl_dm_table *table = NULL;
struct bl_dm_table *bl_table_head = NULL;
unsigned int len;
char *dev_name = NULL;
/* Create pseudo device here */
for (volnum = 0; volnum < num_vols; volnum++) {
node = &vols[volnum];
switch (node->bv_type) {
case BLOCK_VOLUME_SIMPLE:
/* Do not need to create device here */
dev = node->param.bv_dev;
goto continued;
case BLOCK_VOLUME_SLICE:
table = bl_dm_table_alloc();
if (!table)
goto out;
table->offset = 0;
table->size = node->bv_size;
strcpy(table->target_type, "linear");
if (!TYPE_HAS_DEV(node->bv_vols[0]->bv_type)) {
free(table);
goto out;
}
dev = node->bv_vols[0]->param.bv_dev;
tmp = table->params;
BL_LOG_INFO("%s: major %llu minor %llu", __func__,
(long long unsigned)MAJOR(dev),
(long long unsigned)MINOR(dev));
if (!dm_format_dev(tmp, DM_PARAMS_LEN,
MAJOR(dev), MINOR(dev))) {
free(table);
goto out;
}
tmp += strlen(tmp);
sprintf(tmp, " %llu",
(long long unsigned)node->param.bv_offset);
add_to_bl_dm_table(&bl_table_head, table);
break;
case BLOCK_VOLUME_STRIPE:
table = bl_dm_table_alloc();
if (!table)
goto out;
table->offset = 0;
/* Truncate size to a stripe unit boundary */
stripe_unit = node->param.bv_stripe_unit;
table->size =
node->bv_size - (node->bv_size % stripe_unit);
strcpy(table->target_type, "striped");
sprintf(table->params, "%d %llu %n", node->bv_vol_n,
(long long unsigned) stripe_unit, &pos);
/* Copy subdev major:minor to params */
tmp = table->params + pos;
len = DM_PARAMS_LEN - pos;
for (i = 0; i < node->bv_vol_n; i++) {
if (!TYPE_HAS_DEV(node->bv_vols[i]->bv_type)) {
free(table);
goto out;
}
dev = node->bv_vols[i]->param.bv_dev;
if (!dm_format_dev(tmp, len, MAJOR(dev),
MINOR(dev))) {
free(table);
goto out;
}
pos = strlen(tmp);
tmp += pos;
len -= pos;
sprintf(tmp, " %d ", 0);
tmp += 3;
len -= 3;
}
add_to_bl_dm_table(&bl_table_head, table);
break;
case BLOCK_VOLUME_CONCAT:
size = 0;
for (i = 0; i < node->bv_vol_n; i++) {
table = bl_dm_table_alloc();
if (!table)
goto out;
table->offset = size;
table->size = node->bv_vols[i]->bv_size;
if (!TYPE_HAS_DEV(node->bv_vols[i]->bv_type)) {
free(table);
goto out;
}
strcpy(table->target_type, "linear");
tmp = table->params;
dev = node->bv_vols[i]->param.bv_dev;
BL_LOG_INFO("%s: major %lu minor %lu", __func__,
(long unsigned int)MAJOR(dev),
(long unsigned int)MINOR(dev));
if (!dm_format_dev(tmp, DM_PARAMS_LEN,
MAJOR(dev), MINOR(dev))) {
free(table);
goto out;
}
tmp += strlen(tmp);
sprintf(tmp, " %d", 0);
size += table->size;
add_to_bl_dm_table(&bl_table_head, table);
}
break;
default:
/* Delete previous temporary devices */
dm_devicelist_remove(count, dev_count);
goto out;
} /* end of swtich */
/* Create dev_name here. Name of device is pnfs_vol_XXX */
if (dev_name)
free(dev_name);
dev_name = (char *)calloc(DM_DEV_NAME_LEN, sizeof(char));
if (!dev_name) {
BL_LOG_ERR("%s: Out of memory\n", __func__);
goto out;
}
do {
snprintf(dev_name, DM_DEV_NAME_LEN, dm_name,
dev_count++);
} while (dm_device_exists(dev_name));
dev = dm_device_create_mapped(dev_name, bl_table_head);
BL_LOG_INFO("%s: %d %s %d:%d\n", __func__, volnum, dev_name,
(int) MAJOR(dev), (int) MINOR(dev));
if (!dev) {
/* Delete previous temporary devices */
dm_devicelist_remove(count, dev_count);
goto out;
}
node->param.bv_dev = dev;
/* TODO: extend use with PSEUDO later */
node->bv_type = BLOCK_VOLUME_PSEUDO;
continued:
if (bl_table_head)
bl_dm_table_free(bl_table_head);
bl_table_head = NULL;
}
out:
if (bl_table_head) {
bl_dm_table_free(bl_table_head);
bl_table_head = NULL;
}
if (dev)
bl_dm_create_tree(dev);
if (dev_name)
free(dev_name);
return dev;
}
|