]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/lib/bdev/gpt/vbdev_gpt.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / lib / bdev / gpt / vbdev_gpt.c
CommitLineData
11fdf7f2
TL
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
52static int vbdev_gpt_init(void);
53static void vbdev_gpt_examine(struct spdk_bdev *bdev);
54static int vbdev_gpt_get_ctx_size(void);
55
56static 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};
9f95a23c 63SPDK_BDEV_MODULE_REGISTER(gpt, &gpt_if)
11fdf7f2
TL
64
65/* Base block device gpt context */
66struct gpt_base {
67 struct spdk_gpt gpt;
68 struct spdk_bdev_part_base *part_base;
9f95a23c 69 SPDK_BDEV_PART_TAILQ parts;
11fdf7f2
TL
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 */
76struct gpt_disk {
77 struct spdk_bdev_part part;
78 uint32_t partition_index;
79};
80
81struct gpt_channel {
82 struct spdk_bdev_part_channel part_ch;
83};
84
85struct 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
11fdf7f2
TL
93static bool g_gpt_disabled;
94
95static void
96spdk_gpt_base_free(void *ctx)
97{
98 struct gpt_base *gpt_base = ctx;
99
9f95a23c 100 spdk_free(gpt_base->gpt.buf);
11fdf7f2
TL
101 free(gpt_base);
102}
103
104static void
9f95a23c 105spdk_gpt_base_bdev_hotremove_cb(void *_part_base)
11fdf7f2 106{
9f95a23c
TL
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);
11fdf7f2
TL
111}
112
113static int vbdev_gpt_destruct(void *ctx);
114static void vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io);
115static int vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w);
116
117static 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
123static struct gpt_base *
124spdk_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
9f95a23c 135 TAILQ_INIT(&gpt_base->parts);
11fdf7f2
TL
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,
9f95a23c 139 &gpt_base->parts, spdk_gpt_base_free, gpt_base,
11fdf7f2
TL
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;
9f95a23c 148 gpt->parse_phase = SPDK_GPT_PARSE_PHASE_PRIMARY;
11fdf7f2 149 gpt->buf_size = spdk_max(SPDK_GPT_BUFFER_SIZE, bdev->blocklen);
9f95a23c
TL
150 gpt->buf = spdk_zmalloc(gpt->buf_size, spdk_bdev_get_buf_align(bdev), NULL,
151 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
11fdf7f2
TL
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
166static int
167vbdev_gpt_destruct(void *ctx)
168{
169 struct gpt_disk *gpt_disk = ctx;
170
171 return spdk_bdev_part_free(&gpt_disk->part);
172}
173
174static void
175vbdev_gpt_resubmit_request(void *arg)
176{
177 struct gpt_io *io = (struct gpt_io *)arg;
178
179 vbdev_gpt_submit_request(io->ch, io->bdev_io);
180}
181
182static void
183vbdev_gpt_queue_io(struct gpt_io *io)
184{
185 int rc;
186
187 io->bdev_io_wait.bdev = io->bdev_io->bdev;
188 io->bdev_io_wait.cb_fn = vbdev_gpt_resubmit_request;
189 io->bdev_io_wait.cb_arg = io;
190
191 rc = spdk_bdev_queue_io_wait(io->bdev_io->bdev,
192 io->ch, &io->bdev_io_wait);
193 if (rc != 0) {
194 SPDK_ERRLOG("Queue io failed in vbdev_gpt_queue_io, rc=%d.\n", rc);
195 spdk_bdev_io_complete(io->bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
196 }
197}
198
199static void
200vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
201{
202 struct gpt_channel *ch = spdk_io_channel_get_ctx(_ch);
203 struct gpt_io *io = (struct gpt_io *)bdev_io->driver_ctx;
204 int rc;
205
206 rc = spdk_bdev_part_submit_request(&ch->part_ch, bdev_io);
207 if (rc) {
208 if (rc == -ENOMEM) {
209 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "gpt: no memory, queue io\n");
210 io->ch = _ch;
211 io->bdev_io = bdev_io;
212 vbdev_gpt_queue_io(io);
213 } else {
214 SPDK_ERRLOG("gpt: error on bdev_io submission, rc=%d.\n", rc);
215 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
216 }
217 }
218}
219
220static void
221write_guid(struct spdk_json_write_ctx *w, const struct spdk_gpt_guid *guid)
222{
223 spdk_json_write_string_fmt(w, "%08x-%04x-%04x-%04x-%04x%08x",
224 from_le32(&guid->raw[0]),
225 from_le16(&guid->raw[4]),
226 from_le16(&guid->raw[6]),
227 from_be16(&guid->raw[8]),
228 from_be16(&guid->raw[10]),
229 from_be32(&guid->raw[12]));
230}
231
232static void
233write_string_utf16le(struct spdk_json_write_ctx *w, const uint16_t *str, size_t max_len)
234{
235 size_t len;
236 const uint16_t *p;
237
238 for (len = 0, p = str; len < max_len && *p; p++) {
239 len++;
240 }
241
242 spdk_json_write_string_utf16le_raw(w, str, len);
243}
244
245static int
246vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
247{
248 struct gpt_disk *gpt_disk = SPDK_CONTAINEROF(ctx, struct gpt_disk, part);
249 struct spdk_bdev_part_base *base_bdev = spdk_bdev_part_get_base(&gpt_disk->part);
250 struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(base_bdev);
251 struct spdk_bdev *part_base_bdev = spdk_bdev_part_base_get_bdev(base_bdev);
252 struct spdk_gpt *gpt = &gpt_base->gpt;
253 struct spdk_gpt_partition_entry *gpt_entry = &gpt->partitions[gpt_disk->partition_index];
254 uint64_t offset_blocks = spdk_bdev_part_get_offset_blocks(&gpt_disk->part);
255
9f95a23c 256 spdk_json_write_named_object_begin(w, "gpt");
11fdf7f2 257
9f95a23c 258 spdk_json_write_named_string(w, "base_bdev", spdk_bdev_get_name(part_base_bdev));
11fdf7f2 259
9f95a23c 260 spdk_json_write_named_uint64(w, "offset_blocks", offset_blocks);
11fdf7f2
TL
261
262 spdk_json_write_name(w, "partition_type_guid");
263 write_guid(w, &gpt_entry->part_type_guid);
264
265 spdk_json_write_name(w, "unique_partition_guid");
266 write_guid(w, &gpt_entry->unique_partition_guid);
267
268 spdk_json_write_name(w, "partition_name");
269 write_string_utf16le(w, gpt_entry->partition_name, SPDK_COUNTOF(gpt_entry->partition_name));
270
271 spdk_json_write_object_end(w);
272
273 return 0;
274}
275
276static int
277vbdev_gpt_create_bdevs(struct gpt_base *gpt_base)
278{
279 uint32_t num_partition_entries;
280 uint64_t i, head_lba_start, head_lba_end;
281 uint32_t num_partitions;
282 struct spdk_gpt_partition_entry *p;
283 struct gpt_disk *d;
284 struct spdk_gpt *gpt;
285 char *name;
286 struct spdk_bdev *base_bdev;
287 int rc;
288
289 gpt = &gpt_base->gpt;
290 num_partition_entries = from_le32(&gpt->header->num_partition_entries);
291 head_lba_start = from_le64(&gpt->header->first_usable_lba);
292 head_lba_end = from_le64(&gpt->header->last_usable_lba);
293 num_partitions = 0;
294
295 for (i = 0; i < num_partition_entries; i++) {
296 p = &gpt->partitions[i];
297 uint64_t lba_start = from_le64(&p->starting_lba);
298 uint64_t lba_end = from_le64(&p->ending_lba);
299
300 if (!SPDK_GPT_GUID_EQUAL(&gpt->partitions[i].part_type_guid,
301 &SPDK_GPT_PART_TYPE_GUID) ||
302 lba_start == 0) {
303 continue;
304 }
305 if (lba_start < head_lba_start || lba_end > head_lba_end) {
306 continue;
307 }
308
309 d = calloc(1, sizeof(*d));
310 if (!d) {
311 SPDK_ERRLOG("Memory allocation failure\n");
312 return -1;
313 }
314
315 /* index start at 1 instead of 0 to match the existing style */
316 base_bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
317 name = spdk_sprintf_alloc("%sp%" PRIu64, spdk_bdev_get_name(base_bdev), i + 1);
318 if (!name) {
319 SPDK_ERRLOG("name allocation failure\n");
320 free(d);
321 return -1;
322 }
323
324 rc = spdk_bdev_part_construct(&d->part, gpt_base->part_base, name,
325 lba_start, lba_end - lba_start, "GPT Disk");
326 free(name);
327 if (rc) {
328 SPDK_ERRLOG("could not construct bdev part\n");
329 /* spdk_bdev_part_construct will free name on failure */
330 free(d);
331 return -1;
332 }
333 num_partitions++;
334 d->partition_index = i;
335 }
336
337 return num_partitions;
338}
339
340static void
9f95a23c 341spdk_gpt_read_secondary_table_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg)
11fdf7f2
TL
342{
343 struct gpt_base *gpt_base = (struct gpt_base *)arg;
344 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
345 int rc, num_partitions = 0;
346
347 spdk_bdev_free_io(bdev_io);
348 spdk_put_io_channel(gpt_base->ch);
349 gpt_base->ch = NULL;
350
351 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) {
352 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n",
353 spdk_bdev_get_name(bdev), status);
354 goto end;
355 }
356
9f95a23c 357 rc = spdk_gpt_parse_partition_table(&gpt_base->gpt);
11fdf7f2 358 if (rc) {
9f95a23c 359 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse secondary partition table\n");
11fdf7f2
TL
360 goto end;
361 }
362
9f95a23c
TL
363 SPDK_WARNLOG("Gpt: bdev=%s primary partition table broken, use the secondary\n",
364 spdk_bdev_get_name(bdev));
365
11fdf7f2
TL
366 num_partitions = vbdev_gpt_create_bdevs(gpt_base);
367 if (num_partitions < 0) {
368 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to split dev=%s by gpt table\n",
369 spdk_bdev_get_name(bdev));
370 }
371
372end:
9f95a23c
TL
373 spdk_bdev_module_examine_done(&gpt_if);
374 if (num_partitions <= 0) {
375 /* If no gpt_disk instances were created, free the base context */
376 spdk_bdev_part_base_free(gpt_base->part_base);
377 }
378}
379
380static int
381vbdev_gpt_read_secondary_table(struct gpt_base *gpt_base)
382{
383 struct spdk_gpt *gpt;
384 struct spdk_bdev_desc *part_base_desc;
385 uint64_t secondary_offset;
386
387 gpt = &gpt_base->gpt;
388 gpt->parse_phase = SPDK_GPT_PARSE_PHASE_SECONDARY;
389 gpt->header = NULL;
390 gpt->partitions = NULL;
391
392 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base);
393
394 secondary_offset = gpt->total_sectors * gpt->sector_size - gpt->buf_size;
395 return spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, secondary_offset,
396 gpt_base->gpt.buf_size, spdk_gpt_read_secondary_table_complete,
397 gpt_base);
398}
399
400static void
401spdk_gpt_bdev_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg)
402{
403 struct gpt_base *gpt_base = (struct gpt_base *)arg;
404 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
405 int rc, num_partitions = 0;
406
407 spdk_bdev_free_io(bdev_io);
408
409 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) {
410 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n",
411 spdk_bdev_get_name(bdev), status);
412 goto end;
413 }
414
415 rc = spdk_gpt_parse_mbr(&gpt_base->gpt);
416 if (rc) {
417 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse mbr\n");
418 goto end;
419 }
420
421 rc = spdk_gpt_parse_partition_table(&gpt_base->gpt);
422 if (rc) {
423 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse primary partition table\n");
424 rc = vbdev_gpt_read_secondary_table(gpt_base);
425 if (rc) {
426 SPDK_ERRLOG("Failed to read secondary table\n");
427 goto end;
428 }
429 return;
430 }
431
432 num_partitions = vbdev_gpt_create_bdevs(gpt_base);
433 if (num_partitions < 0) {
434 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to split dev=%s by gpt table\n",
435 spdk_bdev_get_name(bdev));
436 }
437
438end:
439 spdk_put_io_channel(gpt_base->ch);
440 gpt_base->ch = NULL;
11fdf7f2
TL
441 /*
442 * Notify the generic bdev layer that the actions related to the original examine
443 * callback are now completed.
444 */
445 spdk_bdev_module_examine_done(&gpt_if);
446
447 /*
448 * vbdev_gpt_create_bdevs returns the number of bdevs created upon success.
449 * We can branch on this value.
450 */
451 if (num_partitions <= 0) {
452 /* If no gpt_disk instances were created, free the base context */
453 spdk_bdev_part_base_free(gpt_base->part_base);
454 }
455}
456
457static int
458vbdev_gpt_read_gpt(struct spdk_bdev *bdev)
459{
460 struct gpt_base *gpt_base;
461 struct spdk_bdev_desc *part_base_desc;
462 int rc;
463
464 gpt_base = spdk_gpt_base_bdev_init(bdev);
465 if (!gpt_base) {
466 SPDK_ERRLOG("Cannot allocated gpt_base\n");
467 return -1;
468 }
469
470 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base);
471 gpt_base->ch = spdk_bdev_get_io_channel(part_base_desc);
472 if (gpt_base->ch == NULL) {
473 SPDK_ERRLOG("Failed to get an io_channel.\n");
474 spdk_bdev_part_base_free(gpt_base->part_base);
475 return -1;
476 }
477
478 rc = spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, 0,
479 gpt_base->gpt.buf_size, spdk_gpt_bdev_complete, gpt_base);
480 if (rc < 0) {
481 spdk_put_io_channel(gpt_base->ch);
482 spdk_bdev_part_base_free(gpt_base->part_base);
483 SPDK_ERRLOG("Failed to send bdev_io command\n");
484 return -1;
485 }
486
487 return 0;
488}
489
490static int
491vbdev_gpt_init(void)
492{
493 struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Gpt");
494
495 if (sp && spdk_conf_section_get_boolval(sp, "Disable", false)) {
496 /* Disable Gpt probe */
497 g_gpt_disabled = true;
498 }
499
500 return 0;
501}
502
503static int
504vbdev_gpt_get_ctx_size(void)
505{
506 return sizeof(struct gpt_io);
507}
508
509static void
510vbdev_gpt_examine(struct spdk_bdev *bdev)
511{
512 int rc;
513
514 /* A bdev with fewer than 2 blocks cannot have a GPT. Block 0 has
515 * the MBR and block 1 has the GPT header.
516 */
517 if (g_gpt_disabled || spdk_bdev_get_num_blocks(bdev) < 2) {
518 spdk_bdev_module_examine_done(&gpt_if);
519 return;
520 }
521
522 if (spdk_bdev_get_block_size(bdev) % 512 != 0) {
523 SPDK_ERRLOG("GPT module does not support block size %" PRIu32 " for bdev %s\n",
524 spdk_bdev_get_block_size(bdev), spdk_bdev_get_name(bdev));
525 spdk_bdev_module_examine_done(&gpt_if);
526 return;
527 }
528
529 rc = vbdev_gpt_read_gpt(bdev);
530 if (rc) {
531 spdk_bdev_module_examine_done(&gpt_if);
532 SPDK_ERRLOG("Failed to read info from bdev %s\n", spdk_bdev_get_name(bdev));
533 }
534}
535
536SPDK_LOG_REGISTER_COMPONENT("vbdev_gpt", SPDK_LOG_VBDEV_GPT)