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