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/endian.h" 42 #include "spdk/env.h" 43 #include "spdk/thread.h" 44 #include "spdk/rpc.h" 45 #include "spdk/string.h" 46 #include "spdk/util.h" 47 48 #include "spdk/bdev_module.h" 49 #include "spdk/log.h" 50 51 static int vbdev_gpt_init(void); 52 static void vbdev_gpt_examine(struct spdk_bdev *bdev); 53 static int vbdev_gpt_get_ctx_size(void); 54 55 static struct spdk_bdev_module gpt_if = { 56 .name = "gpt", 57 .module_init = vbdev_gpt_init, 58 .get_ctx_size = vbdev_gpt_get_ctx_size, 59 .examine_disk = vbdev_gpt_examine, 60 61 }; 62 SPDK_BDEV_MODULE_REGISTER(gpt, &gpt_if) 63 64 /* Base block device gpt context */ 65 struct gpt_base { 66 struct spdk_gpt gpt; 67 struct spdk_bdev_part_base *part_base; 68 SPDK_BDEV_PART_TAILQ parts; 69 70 /* This channel is only used for reading the partition table. */ 71 struct spdk_io_channel *ch; 72 }; 73 74 /* Context for each gpt virtual bdev */ 75 struct gpt_disk { 76 struct spdk_bdev_part part; 77 uint32_t partition_index; 78 }; 79 80 struct gpt_channel { 81 struct spdk_bdev_part_channel part_ch; 82 }; 83 84 struct gpt_io { 85 struct spdk_io_channel *ch; 86 struct spdk_bdev_io *bdev_io; 87 88 /* for bdev_io_wait */ 89 struct spdk_bdev_io_wait_entry bdev_io_wait; 90 }; 91 92 static void 93 gpt_base_free(void *ctx) 94 { 95 struct gpt_base *gpt_base = ctx; 96 97 spdk_free(gpt_base->gpt.buf); 98 free(gpt_base); 99 } 100 101 static void 102 gpt_base_bdev_hotremove_cb(void *_part_base) 103 { 104 struct spdk_bdev_part_base *part_base = _part_base; 105 struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(part_base); 106 107 spdk_bdev_part_base_hotremove(part_base, &gpt_base->parts); 108 } 109 110 static int vbdev_gpt_destruct(void *ctx); 111 static void vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io); 112 static int vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w); 113 static int vbdev_gpt_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, 114 int array_size); 115 116 static struct spdk_bdev_fn_table vbdev_gpt_fn_table = { 117 .destruct = vbdev_gpt_destruct, 118 .submit_request = vbdev_gpt_submit_request, 119 .dump_info_json = vbdev_gpt_dump_info_json, 120 .get_memory_domains = vbdev_gpt_get_memory_domains, 121 }; 122 123 static struct gpt_base * 124 gpt_base_bdev_init(struct spdk_bdev *bdev) 125 { 126 struct gpt_base *gpt_base; 127 struct spdk_gpt *gpt; 128 int rc; 129 130 gpt_base = calloc(1, sizeof(*gpt_base)); 131 if (!gpt_base) { 132 SPDK_ERRLOG("Cannot alloc memory for gpt_base pointer\n"); 133 return NULL; 134 } 135 136 TAILQ_INIT(&gpt_base->parts); 137 rc = spdk_bdev_part_base_construct_ext(spdk_bdev_get_name(bdev), 138 gpt_base_bdev_hotremove_cb, 139 &gpt_if, &vbdev_gpt_fn_table, 140 &gpt_base->parts, gpt_base_free, gpt_base, 141 sizeof(struct gpt_channel), NULL, NULL, &gpt_base->part_base); 142 if (rc != 0) { 143 free(gpt_base); 144 SPDK_ERRLOG("cannot construct gpt_base"); 145 return NULL; 146 } 147 148 gpt = &gpt_base->gpt; 149 gpt->parse_phase = SPDK_GPT_PARSE_PHASE_PRIMARY; 150 gpt->buf_size = spdk_max(SPDK_GPT_BUFFER_SIZE, bdev->blocklen); 151 gpt->buf = spdk_zmalloc(gpt->buf_size, spdk_bdev_get_buf_align(bdev), NULL, 152 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 153 if (!gpt->buf) { 154 SPDK_ERRLOG("Cannot alloc buf\n"); 155 spdk_bdev_part_base_free(gpt_base->part_base); 156 return NULL; 157 } 158 159 gpt->sector_size = bdev->blocklen; 160 gpt->total_sectors = bdev->blockcnt; 161 gpt->lba_start = 0; 162 gpt->lba_end = gpt->total_sectors - 1; 163 164 return gpt_base; 165 } 166 167 static int 168 vbdev_gpt_destruct(void *ctx) 169 { 170 struct gpt_disk *gpt_disk = ctx; 171 172 return spdk_bdev_part_free(&gpt_disk->part); 173 } 174 175 static void 176 _vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io); 177 178 static void 179 vbdev_gpt_resubmit_request(void *arg) 180 { 181 struct gpt_io *io = (struct gpt_io *)arg; 182 183 _vbdev_gpt_submit_request(io->ch, io->bdev_io); 184 } 185 186 static void 187 vbdev_gpt_queue_io(struct gpt_io *io) 188 { 189 struct gpt_channel *ch = spdk_io_channel_get_ctx(io->ch); 190 int rc; 191 192 io->bdev_io_wait.bdev = io->bdev_io->bdev; 193 io->bdev_io_wait.cb_fn = vbdev_gpt_resubmit_request; 194 io->bdev_io_wait.cb_arg = io; 195 196 rc = spdk_bdev_queue_io_wait(io->bdev_io->bdev, 197 ch->part_ch.base_ch, &io->bdev_io_wait); 198 if (rc != 0) { 199 SPDK_ERRLOG("Queue io failed in vbdev_gpt_queue_io, rc=%d.\n", rc); 200 spdk_bdev_io_complete(io->bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 201 } 202 } 203 204 static void 205 vbdev_gpt_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success) 206 { 207 if (!success) { 208 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 209 return; 210 } 211 212 _vbdev_gpt_submit_request(ch, bdev_io); 213 } 214 215 static void 216 _vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 217 { 218 struct gpt_channel *ch = spdk_io_channel_get_ctx(_ch); 219 struct gpt_io *io = (struct gpt_io *)bdev_io->driver_ctx; 220 int rc; 221 222 rc = spdk_bdev_part_submit_request(&ch->part_ch, bdev_io); 223 if (rc) { 224 if (rc == -ENOMEM) { 225 SPDK_DEBUGLOG(vbdev_gpt, "gpt: no memory, queue io\n"); 226 io->ch = _ch; 227 io->bdev_io = bdev_io; 228 vbdev_gpt_queue_io(io); 229 } else { 230 SPDK_ERRLOG("gpt: error on bdev_io submission, rc=%d.\n", rc); 231 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 232 } 233 } 234 } 235 236 static void 237 vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 238 { 239 switch (bdev_io->type) { 240 case SPDK_BDEV_IO_TYPE_READ: 241 spdk_bdev_io_get_buf(bdev_io, vbdev_gpt_get_buf_cb, 242 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 243 break; 244 default: 245 _vbdev_gpt_submit_request(_ch, bdev_io); 246 break; 247 } 248 } 249 250 static void 251 write_guid(struct spdk_json_write_ctx *w, const struct spdk_gpt_guid *guid) 252 { 253 spdk_json_write_string_fmt(w, "%08x-%04x-%04x-%04x-%04x%08x", 254 from_le32(&guid->raw[0]), 255 from_le16(&guid->raw[4]), 256 from_le16(&guid->raw[6]), 257 from_be16(&guid->raw[8]), 258 from_be16(&guid->raw[10]), 259 from_be32(&guid->raw[12])); 260 } 261 262 static void 263 write_string_utf16le(struct spdk_json_write_ctx *w, const uint16_t *str, size_t max_len) 264 { 265 size_t len; 266 const uint16_t *p; 267 268 for (len = 0, p = str; len < max_len && *p; p++) { 269 len++; 270 } 271 272 spdk_json_write_string_utf16le_raw(w, str, len); 273 } 274 275 static int 276 vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 277 { 278 struct gpt_disk *gpt_disk = SPDK_CONTAINEROF(ctx, struct gpt_disk, part); 279 struct spdk_bdev_part_base *base_bdev = spdk_bdev_part_get_base(&gpt_disk->part); 280 struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(base_bdev); 281 struct spdk_bdev *part_base_bdev = spdk_bdev_part_base_get_bdev(base_bdev); 282 struct spdk_gpt *gpt = &gpt_base->gpt; 283 struct spdk_gpt_partition_entry *gpt_entry = &gpt->partitions[gpt_disk->partition_index]; 284 uint64_t offset_blocks = spdk_bdev_part_get_offset_blocks(&gpt_disk->part); 285 286 spdk_json_write_named_object_begin(w, "gpt"); 287 288 spdk_json_write_named_string(w, "base_bdev", spdk_bdev_get_name(part_base_bdev)); 289 290 spdk_json_write_named_uint64(w, "offset_blocks", offset_blocks); 291 292 spdk_json_write_name(w, "partition_type_guid"); 293 write_guid(w, &gpt_entry->part_type_guid); 294 295 spdk_json_write_name(w, "unique_partition_guid"); 296 write_guid(w, &gpt_entry->unique_partition_guid); 297 298 spdk_json_write_name(w, "partition_name"); 299 write_string_utf16le(w, gpt_entry->partition_name, SPDK_COUNTOF(gpt_entry->partition_name)); 300 301 spdk_json_write_object_end(w); 302 303 return 0; 304 } 305 306 static int 307 vbdev_gpt_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size) 308 { 309 struct gpt_disk *gpt_disk = SPDK_CONTAINEROF(ctx, struct gpt_disk, part); 310 struct spdk_bdev_part_base *part_base = spdk_bdev_part_get_base(&gpt_disk->part); 311 struct spdk_bdev *part_base_bdev = spdk_bdev_part_base_get_bdev(part_base); 312 313 if (part_base_bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) { 314 /* bdev_part remaps reftag and touches metadata buffer, that means it can't support memory domains 315 * if dif is enabled */ 316 return 0; 317 } 318 319 return spdk_bdev_get_memory_domains(part_base_bdev, domains, array_size); 320 } 321 322 static int 323 vbdev_gpt_create_bdevs(struct gpt_base *gpt_base) 324 { 325 uint32_t num_partition_entries; 326 uint64_t i, head_lba_start, head_lba_end; 327 uint32_t num_partitions; 328 struct spdk_gpt_partition_entry *p; 329 struct gpt_disk *d; 330 struct spdk_gpt *gpt; 331 char *name; 332 struct spdk_bdev *base_bdev; 333 int rc; 334 335 gpt = &gpt_base->gpt; 336 num_partition_entries = from_le32(&gpt->header->num_partition_entries); 337 head_lba_start = from_le64(&gpt->header->first_usable_lba); 338 head_lba_end = from_le64(&gpt->header->last_usable_lba); 339 num_partitions = 0; 340 341 for (i = 0; i < num_partition_entries; i++) { 342 p = &gpt->partitions[i]; 343 uint64_t lba_start = from_le64(&p->starting_lba); 344 uint64_t lba_end = from_le64(&p->ending_lba); 345 346 if (!SPDK_GPT_GUID_EQUAL(&gpt->partitions[i].part_type_guid, 347 &SPDK_GPT_PART_TYPE_GUID) || 348 lba_start == 0) { 349 continue; 350 } 351 if (lba_start < head_lba_start || lba_end > head_lba_end) { 352 continue; 353 } 354 355 d = calloc(1, sizeof(*d)); 356 if (!d) { 357 SPDK_ERRLOG("Memory allocation failure\n"); 358 return -1; 359 } 360 361 /* index start at 1 instead of 0 to match the existing style */ 362 base_bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base); 363 name = spdk_sprintf_alloc("%sp%" PRIu64, spdk_bdev_get_name(base_bdev), i + 1); 364 if (!name) { 365 SPDK_ERRLOG("name allocation failure\n"); 366 free(d); 367 return -1; 368 } 369 370 rc = spdk_bdev_part_construct(&d->part, gpt_base->part_base, name, 371 lba_start, lba_end - lba_start, "GPT Disk"); 372 free(name); 373 if (rc) { 374 SPDK_ERRLOG("could not construct bdev part\n"); 375 /* spdk_bdev_part_construct will free name on failure */ 376 free(d); 377 return -1; 378 } 379 num_partitions++; 380 d->partition_index = i; 381 } 382 383 return num_partitions; 384 } 385 386 static void 387 gpt_read_secondary_table_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg) 388 { 389 struct gpt_base *gpt_base = (struct gpt_base *)arg; 390 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base); 391 int rc, num_partitions = 0; 392 393 spdk_bdev_free_io(bdev_io); 394 spdk_put_io_channel(gpt_base->ch); 395 gpt_base->ch = NULL; 396 397 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 398 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n", 399 spdk_bdev_get_name(bdev), status); 400 goto end; 401 } 402 403 rc = gpt_parse_partition_table(&gpt_base->gpt); 404 if (rc) { 405 SPDK_DEBUGLOG(vbdev_gpt, "Failed to parse secondary partition table\n"); 406 goto end; 407 } 408 409 SPDK_WARNLOG("Gpt: bdev=%s primary partition table broken, use the secondary\n", 410 spdk_bdev_get_name(bdev)); 411 412 num_partitions = vbdev_gpt_create_bdevs(gpt_base); 413 if (num_partitions < 0) { 414 SPDK_DEBUGLOG(vbdev_gpt, "Failed to split dev=%s by gpt table\n", 415 spdk_bdev_get_name(bdev)); 416 } 417 418 end: 419 spdk_bdev_module_examine_done(&gpt_if); 420 if (num_partitions <= 0) { 421 /* If no gpt_disk instances were created, free the base context */ 422 spdk_bdev_part_base_free(gpt_base->part_base); 423 } 424 } 425 426 static int 427 vbdev_gpt_read_secondary_table(struct gpt_base *gpt_base) 428 { 429 struct spdk_gpt *gpt; 430 struct spdk_bdev_desc *part_base_desc; 431 uint64_t secondary_offset; 432 433 gpt = &gpt_base->gpt; 434 gpt->parse_phase = SPDK_GPT_PARSE_PHASE_SECONDARY; 435 gpt->header = NULL; 436 gpt->partitions = NULL; 437 438 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base); 439 440 secondary_offset = gpt->total_sectors * gpt->sector_size - gpt->buf_size; 441 return spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, secondary_offset, 442 gpt_base->gpt.buf_size, gpt_read_secondary_table_complete, 443 gpt_base); 444 } 445 446 static void 447 gpt_bdev_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg) 448 { 449 struct gpt_base *gpt_base = (struct gpt_base *)arg; 450 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base); 451 int rc, num_partitions = 0; 452 453 spdk_bdev_free_io(bdev_io); 454 455 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 456 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n", 457 spdk_bdev_get_name(bdev), status); 458 goto end; 459 } 460 461 rc = gpt_parse_mbr(&gpt_base->gpt); 462 if (rc) { 463 SPDK_DEBUGLOG(vbdev_gpt, "Failed to parse mbr\n"); 464 goto end; 465 } 466 467 rc = gpt_parse_partition_table(&gpt_base->gpt); 468 if (rc) { 469 SPDK_DEBUGLOG(vbdev_gpt, "Failed to parse primary partition table\n"); 470 rc = vbdev_gpt_read_secondary_table(gpt_base); 471 if (rc) { 472 SPDK_ERRLOG("Failed to read secondary table\n"); 473 goto end; 474 } 475 return; 476 } 477 478 num_partitions = vbdev_gpt_create_bdevs(gpt_base); 479 if (num_partitions < 0) { 480 SPDK_DEBUGLOG(vbdev_gpt, "Failed to split dev=%s by gpt table\n", 481 spdk_bdev_get_name(bdev)); 482 } 483 484 end: 485 spdk_put_io_channel(gpt_base->ch); 486 gpt_base->ch = NULL; 487 /* 488 * Notify the generic bdev layer that the actions related to the original examine 489 * callback are now completed. 490 */ 491 spdk_bdev_module_examine_done(&gpt_if); 492 493 /* 494 * vbdev_gpt_create_bdevs returns the number of bdevs created upon success. 495 * We can branch on this value. 496 */ 497 if (num_partitions <= 0) { 498 /* If no gpt_disk instances were created, free the base context */ 499 spdk_bdev_part_base_free(gpt_base->part_base); 500 } 501 } 502 503 static int 504 vbdev_gpt_read_gpt(struct spdk_bdev *bdev) 505 { 506 struct gpt_base *gpt_base; 507 struct spdk_bdev_desc *part_base_desc; 508 int rc; 509 510 gpt_base = gpt_base_bdev_init(bdev); 511 if (!gpt_base) { 512 SPDK_ERRLOG("Cannot allocated gpt_base\n"); 513 return -1; 514 } 515 516 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base); 517 gpt_base->ch = spdk_bdev_get_io_channel(part_base_desc); 518 if (gpt_base->ch == NULL) { 519 SPDK_ERRLOG("Failed to get an io_channel.\n"); 520 spdk_bdev_part_base_free(gpt_base->part_base); 521 return -1; 522 } 523 524 rc = spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, 0, 525 gpt_base->gpt.buf_size, gpt_bdev_complete, gpt_base); 526 if (rc < 0) { 527 spdk_put_io_channel(gpt_base->ch); 528 spdk_bdev_part_base_free(gpt_base->part_base); 529 SPDK_ERRLOG("Failed to send bdev_io command\n"); 530 return -1; 531 } 532 533 return 0; 534 } 535 536 static int 537 vbdev_gpt_init(void) 538 { 539 return 0; 540 } 541 542 static int 543 vbdev_gpt_get_ctx_size(void) 544 { 545 return sizeof(struct gpt_io); 546 } 547 548 static void 549 vbdev_gpt_examine(struct spdk_bdev *bdev) 550 { 551 int rc; 552 553 /* A bdev with fewer than 2 blocks cannot have a GPT. Block 0 has 554 * the MBR and block 1 has the GPT header. 555 */ 556 if (spdk_bdev_get_num_blocks(bdev) < 2) { 557 spdk_bdev_module_examine_done(&gpt_if); 558 return; 559 } 560 561 if (spdk_bdev_get_block_size(bdev) % 512 != 0) { 562 SPDK_DEBUGLOG(vbdev_gpt, 563 "GPT module does not support block size %" PRIu32 " for bdev %s\n", 564 spdk_bdev_get_block_size(bdev), spdk_bdev_get_name(bdev)); 565 spdk_bdev_module_examine_done(&gpt_if); 566 return; 567 } 568 569 rc = vbdev_gpt_read_gpt(bdev); 570 if (rc) { 571 spdk_bdev_module_examine_done(&gpt_if); 572 SPDK_ERRLOG("Failed to read info from bdev %s\n", spdk_bdev_get_name(bdev)); 573 } 574 } 575 576 SPDK_LOG_REGISTER_COMPONENT(vbdev_gpt) 577