1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * This driver reads a GPT partition table from a bdev and exposes a virtual block device for 36 * each partition. 37 */ 38 39 #include "gpt.h" 40 41 #include "spdk/conf.h" 42 #include "spdk/endian.h" 43 #include "spdk/env.h" 44 #include "spdk/thread.h" 45 #include "spdk/rpc.h" 46 #include "spdk/string.h" 47 #include "spdk/util.h" 48 49 #include "spdk/bdev_module.h" 50 #include "spdk_internal/log.h" 51 52 static int vbdev_gpt_init(void); 53 static void vbdev_gpt_examine(struct spdk_bdev *bdev); 54 static int vbdev_gpt_get_ctx_size(void); 55 56 static struct spdk_bdev_module gpt_if = { 57 .name = "gpt", 58 .module_init = vbdev_gpt_init, 59 .get_ctx_size = vbdev_gpt_get_ctx_size, 60 .examine_disk = vbdev_gpt_examine, 61 62 }; 63 SPDK_BDEV_MODULE_REGISTER(gpt, &gpt_if) 64 65 /* Base block device gpt context */ 66 struct gpt_base { 67 struct spdk_gpt gpt; 68 struct spdk_bdev_part_base *part_base; 69 SPDK_BDEV_PART_TAILQ parts; 70 71 /* This channel is only used for reading the partition table. */ 72 struct spdk_io_channel *ch; 73 }; 74 75 /* Context for each gpt virtual bdev */ 76 struct gpt_disk { 77 struct spdk_bdev_part part; 78 uint32_t partition_index; 79 }; 80 81 struct gpt_channel { 82 struct spdk_bdev_part_channel part_ch; 83 }; 84 85 struct gpt_io { 86 struct spdk_io_channel *ch; 87 struct spdk_bdev_io *bdev_io; 88 89 /* for bdev_io_wait */ 90 struct spdk_bdev_io_wait_entry bdev_io_wait; 91 }; 92 93 static bool g_gpt_disabled; 94 95 static void 96 spdk_gpt_base_free(void *ctx) 97 { 98 struct gpt_base *gpt_base = ctx; 99 100 spdk_free(gpt_base->gpt.buf); 101 free(gpt_base); 102 } 103 104 static void 105 spdk_gpt_base_bdev_hotremove_cb(void *_part_base) 106 { 107 struct spdk_bdev_part_base *part_base = _part_base; 108 struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(part_base); 109 110 spdk_bdev_part_base_hotremove(part_base, &gpt_base->parts); 111 } 112 113 static int vbdev_gpt_destruct(void *ctx); 114 static void vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io); 115 static int vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w); 116 117 static struct spdk_bdev_fn_table vbdev_gpt_fn_table = { 118 .destruct = vbdev_gpt_destruct, 119 .submit_request = vbdev_gpt_submit_request, 120 .dump_info_json = vbdev_gpt_dump_info_json, 121 }; 122 123 static struct gpt_base * 124 spdk_gpt_base_bdev_init(struct spdk_bdev *bdev) 125 { 126 struct gpt_base *gpt_base; 127 struct spdk_gpt *gpt; 128 129 gpt_base = calloc(1, sizeof(*gpt_base)); 130 if (!gpt_base) { 131 SPDK_ERRLOG("Cannot alloc memory for gpt_base pointer\n"); 132 return NULL; 133 } 134 135 TAILQ_INIT(&gpt_base->parts); 136 gpt_base->part_base = spdk_bdev_part_base_construct(bdev, 137 spdk_gpt_base_bdev_hotremove_cb, 138 &gpt_if, &vbdev_gpt_fn_table, 139 &gpt_base->parts, spdk_gpt_base_free, gpt_base, 140 sizeof(struct gpt_channel), NULL, NULL); 141 if (!gpt_base->part_base) { 142 free(gpt_base); 143 SPDK_ERRLOG("cannot construct gpt_base"); 144 return NULL; 145 } 146 147 gpt = &gpt_base->gpt; 148 gpt->parse_phase = SPDK_GPT_PARSE_PHASE_PRIMARY; 149 gpt->buf_size = spdk_max(SPDK_GPT_BUFFER_SIZE, bdev->blocklen); 150 gpt->buf = spdk_zmalloc(gpt->buf_size, spdk_bdev_get_buf_align(bdev), NULL, 151 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 152 if (!gpt->buf) { 153 SPDK_ERRLOG("Cannot alloc buf\n"); 154 spdk_bdev_part_base_free(gpt_base->part_base); 155 return NULL; 156 } 157 158 gpt->sector_size = bdev->blocklen; 159 gpt->total_sectors = bdev->blockcnt; 160 gpt->lba_start = 0; 161 gpt->lba_end = gpt->total_sectors - 1; 162 163 return gpt_base; 164 } 165 166 static int 167 vbdev_gpt_destruct(void *ctx) 168 { 169 struct gpt_disk *gpt_disk = ctx; 170 171 return spdk_bdev_part_free(&gpt_disk->part); 172 } 173 174 static void 175 _vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io); 176 177 static void 178 vbdev_gpt_resubmit_request(void *arg) 179 { 180 struct gpt_io *io = (struct gpt_io *)arg; 181 182 _vbdev_gpt_submit_request(io->ch, io->bdev_io); 183 } 184 185 static void 186 vbdev_gpt_queue_io(struct gpt_io *io) 187 { 188 struct gpt_channel *ch = spdk_io_channel_get_ctx(io->ch); 189 int rc; 190 191 io->bdev_io_wait.bdev = io->bdev_io->bdev; 192 io->bdev_io_wait.cb_fn = vbdev_gpt_resubmit_request; 193 io->bdev_io_wait.cb_arg = io; 194 195 rc = spdk_bdev_queue_io_wait(io->bdev_io->bdev, 196 ch->part_ch.base_ch, &io->bdev_io_wait); 197 if (rc != 0) { 198 SPDK_ERRLOG("Queue io failed in vbdev_gpt_queue_io, rc=%d.\n", rc); 199 spdk_bdev_io_complete(io->bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 200 } 201 } 202 203 static void 204 vbdev_gpt_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success) 205 { 206 if (!success) { 207 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 208 return; 209 } 210 211 _vbdev_gpt_submit_request(ch, bdev_io); 212 } 213 214 static void 215 _vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 216 { 217 struct gpt_channel *ch = spdk_io_channel_get_ctx(_ch); 218 struct gpt_io *io = (struct gpt_io *)bdev_io->driver_ctx; 219 int rc; 220 221 rc = spdk_bdev_part_submit_request(&ch->part_ch, bdev_io); 222 if (rc) { 223 if (rc == -ENOMEM) { 224 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "gpt: no memory, queue io\n"); 225 io->ch = _ch; 226 io->bdev_io = bdev_io; 227 vbdev_gpt_queue_io(io); 228 } else { 229 SPDK_ERRLOG("gpt: error on bdev_io submission, rc=%d.\n", rc); 230 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 231 } 232 } 233 } 234 235 static void 236 vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 237 { 238 switch (bdev_io->type) { 239 case SPDK_BDEV_IO_TYPE_READ: 240 spdk_bdev_io_get_buf(bdev_io, vbdev_gpt_get_buf_cb, 241 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 242 break; 243 default: 244 _vbdev_gpt_submit_request(_ch, bdev_io); 245 break; 246 } 247 } 248 249 static void 250 write_guid(struct spdk_json_write_ctx *w, const struct spdk_gpt_guid *guid) 251 { 252 spdk_json_write_string_fmt(w, "%08x-%04x-%04x-%04x-%04x%08x", 253 from_le32(&guid->raw[0]), 254 from_le16(&guid->raw[4]), 255 from_le16(&guid->raw[6]), 256 from_be16(&guid->raw[8]), 257 from_be16(&guid->raw[10]), 258 from_be32(&guid->raw[12])); 259 } 260 261 static void 262 write_string_utf16le(struct spdk_json_write_ctx *w, const uint16_t *str, size_t max_len) 263 { 264 size_t len; 265 const uint16_t *p; 266 267 for (len = 0, p = str; len < max_len && *p; p++) { 268 len++; 269 } 270 271 spdk_json_write_string_utf16le_raw(w, str, len); 272 } 273 274 static int 275 vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 276 { 277 struct gpt_disk *gpt_disk = SPDK_CONTAINEROF(ctx, struct gpt_disk, part); 278 struct spdk_bdev_part_base *base_bdev = spdk_bdev_part_get_base(&gpt_disk->part); 279 struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(base_bdev); 280 struct spdk_bdev *part_base_bdev = spdk_bdev_part_base_get_bdev(base_bdev); 281 struct spdk_gpt *gpt = &gpt_base->gpt; 282 struct spdk_gpt_partition_entry *gpt_entry = &gpt->partitions[gpt_disk->partition_index]; 283 uint64_t offset_blocks = spdk_bdev_part_get_offset_blocks(&gpt_disk->part); 284 285 spdk_json_write_named_object_begin(w, "gpt"); 286 287 spdk_json_write_named_string(w, "base_bdev", spdk_bdev_get_name(part_base_bdev)); 288 289 spdk_json_write_named_uint64(w, "offset_blocks", offset_blocks); 290 291 spdk_json_write_name(w, "partition_type_guid"); 292 write_guid(w, &gpt_entry->part_type_guid); 293 294 spdk_json_write_name(w, "unique_partition_guid"); 295 write_guid(w, &gpt_entry->unique_partition_guid); 296 297 spdk_json_write_name(w, "partition_name"); 298 write_string_utf16le(w, gpt_entry->partition_name, SPDK_COUNTOF(gpt_entry->partition_name)); 299 300 spdk_json_write_object_end(w); 301 302 return 0; 303 } 304 305 static int 306 vbdev_gpt_create_bdevs(struct gpt_base *gpt_base) 307 { 308 uint32_t num_partition_entries; 309 uint64_t i, head_lba_start, head_lba_end; 310 uint32_t num_partitions; 311 struct spdk_gpt_partition_entry *p; 312 struct gpt_disk *d; 313 struct spdk_gpt *gpt; 314 char *name; 315 struct spdk_bdev *base_bdev; 316 int rc; 317 318 gpt = &gpt_base->gpt; 319 num_partition_entries = from_le32(&gpt->header->num_partition_entries); 320 head_lba_start = from_le64(&gpt->header->first_usable_lba); 321 head_lba_end = from_le64(&gpt->header->last_usable_lba); 322 num_partitions = 0; 323 324 for (i = 0; i < num_partition_entries; i++) { 325 p = &gpt->partitions[i]; 326 uint64_t lba_start = from_le64(&p->starting_lba); 327 uint64_t lba_end = from_le64(&p->ending_lba); 328 329 if (!SPDK_GPT_GUID_EQUAL(&gpt->partitions[i].part_type_guid, 330 &SPDK_GPT_PART_TYPE_GUID) || 331 lba_start == 0) { 332 continue; 333 } 334 if (lba_start < head_lba_start || lba_end > head_lba_end) { 335 continue; 336 } 337 338 d = calloc(1, sizeof(*d)); 339 if (!d) { 340 SPDK_ERRLOG("Memory allocation failure\n"); 341 return -1; 342 } 343 344 /* index start at 1 instead of 0 to match the existing style */ 345 base_bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base); 346 name = spdk_sprintf_alloc("%sp%" PRIu64, spdk_bdev_get_name(base_bdev), i + 1); 347 if (!name) { 348 SPDK_ERRLOG("name allocation failure\n"); 349 free(d); 350 return -1; 351 } 352 353 rc = spdk_bdev_part_construct(&d->part, gpt_base->part_base, name, 354 lba_start, lba_end - lba_start, "GPT Disk"); 355 free(name); 356 if (rc) { 357 SPDK_ERRLOG("could not construct bdev part\n"); 358 /* spdk_bdev_part_construct will free name on failure */ 359 free(d); 360 return -1; 361 } 362 num_partitions++; 363 d->partition_index = i; 364 } 365 366 return num_partitions; 367 } 368 369 static void 370 spdk_gpt_read_secondary_table_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg) 371 { 372 struct gpt_base *gpt_base = (struct gpt_base *)arg; 373 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base); 374 int rc, num_partitions = 0; 375 376 spdk_bdev_free_io(bdev_io); 377 spdk_put_io_channel(gpt_base->ch); 378 gpt_base->ch = NULL; 379 380 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 381 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n", 382 spdk_bdev_get_name(bdev), status); 383 goto end; 384 } 385 386 rc = spdk_gpt_parse_partition_table(&gpt_base->gpt); 387 if (rc) { 388 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse secondary partition table\n"); 389 goto end; 390 } 391 392 SPDK_WARNLOG("Gpt: bdev=%s primary partition table broken, use the secondary\n", 393 spdk_bdev_get_name(bdev)); 394 395 num_partitions = vbdev_gpt_create_bdevs(gpt_base); 396 if (num_partitions < 0) { 397 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to split dev=%s by gpt table\n", 398 spdk_bdev_get_name(bdev)); 399 } 400 401 end: 402 spdk_bdev_module_examine_done(&gpt_if); 403 if (num_partitions <= 0) { 404 /* If no gpt_disk instances were created, free the base context */ 405 spdk_bdev_part_base_free(gpt_base->part_base); 406 } 407 } 408 409 static int 410 vbdev_gpt_read_secondary_table(struct gpt_base *gpt_base) 411 { 412 struct spdk_gpt *gpt; 413 struct spdk_bdev_desc *part_base_desc; 414 uint64_t secondary_offset; 415 416 gpt = &gpt_base->gpt; 417 gpt->parse_phase = SPDK_GPT_PARSE_PHASE_SECONDARY; 418 gpt->header = NULL; 419 gpt->partitions = NULL; 420 421 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base); 422 423 secondary_offset = gpt->total_sectors * gpt->sector_size - gpt->buf_size; 424 return spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, secondary_offset, 425 gpt_base->gpt.buf_size, spdk_gpt_read_secondary_table_complete, 426 gpt_base); 427 } 428 429 static void 430 spdk_gpt_bdev_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg) 431 { 432 struct gpt_base *gpt_base = (struct gpt_base *)arg; 433 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base); 434 int rc, num_partitions = 0; 435 436 spdk_bdev_free_io(bdev_io); 437 438 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 439 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n", 440 spdk_bdev_get_name(bdev), status); 441 goto end; 442 } 443 444 rc = spdk_gpt_parse_mbr(&gpt_base->gpt); 445 if (rc) { 446 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse mbr\n"); 447 goto end; 448 } 449 450 rc = spdk_gpt_parse_partition_table(&gpt_base->gpt); 451 if (rc) { 452 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse primary partition table\n"); 453 rc = vbdev_gpt_read_secondary_table(gpt_base); 454 if (rc) { 455 SPDK_ERRLOG("Failed to read secondary table\n"); 456 goto end; 457 } 458 return; 459 } 460 461 num_partitions = vbdev_gpt_create_bdevs(gpt_base); 462 if (num_partitions < 0) { 463 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to split dev=%s by gpt table\n", 464 spdk_bdev_get_name(bdev)); 465 } 466 467 end: 468 spdk_put_io_channel(gpt_base->ch); 469 gpt_base->ch = NULL; 470 /* 471 * Notify the generic bdev layer that the actions related to the original examine 472 * callback are now completed. 473 */ 474 spdk_bdev_module_examine_done(&gpt_if); 475 476 /* 477 * vbdev_gpt_create_bdevs returns the number of bdevs created upon success. 478 * We can branch on this value. 479 */ 480 if (num_partitions <= 0) { 481 /* If no gpt_disk instances were created, free the base context */ 482 spdk_bdev_part_base_free(gpt_base->part_base); 483 } 484 } 485 486 static int 487 vbdev_gpt_read_gpt(struct spdk_bdev *bdev) 488 { 489 struct gpt_base *gpt_base; 490 struct spdk_bdev_desc *part_base_desc; 491 int rc; 492 493 gpt_base = spdk_gpt_base_bdev_init(bdev); 494 if (!gpt_base) { 495 SPDK_ERRLOG("Cannot allocated gpt_base\n"); 496 return -1; 497 } 498 499 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base); 500 gpt_base->ch = spdk_bdev_get_io_channel(part_base_desc); 501 if (gpt_base->ch == NULL) { 502 SPDK_ERRLOG("Failed to get an io_channel.\n"); 503 spdk_bdev_part_base_free(gpt_base->part_base); 504 return -1; 505 } 506 507 rc = spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, 0, 508 gpt_base->gpt.buf_size, spdk_gpt_bdev_complete, gpt_base); 509 if (rc < 0) { 510 spdk_put_io_channel(gpt_base->ch); 511 spdk_bdev_part_base_free(gpt_base->part_base); 512 SPDK_ERRLOG("Failed to send bdev_io command\n"); 513 return -1; 514 } 515 516 return 0; 517 } 518 519 static int 520 vbdev_gpt_init(void) 521 { 522 struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Gpt"); 523 524 if (sp && spdk_conf_section_get_boolval(sp, "Disable", false)) { 525 /* Disable Gpt probe */ 526 g_gpt_disabled = true; 527 } 528 529 return 0; 530 } 531 532 static int 533 vbdev_gpt_get_ctx_size(void) 534 { 535 return sizeof(struct gpt_io); 536 } 537 538 static void 539 vbdev_gpt_examine(struct spdk_bdev *bdev) 540 { 541 int rc; 542 543 /* A bdev with fewer than 2 blocks cannot have a GPT. Block 0 has 544 * the MBR and block 1 has the GPT header. 545 */ 546 if (g_gpt_disabled || spdk_bdev_get_num_blocks(bdev) < 2) { 547 spdk_bdev_module_examine_done(&gpt_if); 548 return; 549 } 550 551 if (spdk_bdev_get_block_size(bdev) % 512 != 0) { 552 SPDK_ERRLOG("GPT module does not support block size %" PRIu32 " for bdev %s\n", 553 spdk_bdev_get_block_size(bdev), spdk_bdev_get_name(bdev)); 554 spdk_bdev_module_examine_done(&gpt_if); 555 return; 556 } 557 558 rc = vbdev_gpt_read_gpt(bdev); 559 if (rc) { 560 spdk_bdev_module_examine_done(&gpt_if); 561 SPDK_ERRLOG("Failed to read info from bdev %s\n", spdk_bdev_get_name(bdev)); 562 } 563 } 564 565 SPDK_LOG_REGISTER_COMPONENT("vbdev_gpt", SPDK_LOG_VBDEV_GPT) 566