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