]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/bdev/gpt/vbdev_gpt.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / lib / bdev / gpt / vbdev_gpt.c
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_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
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 SPDK_BDEV_PART_TAILQ g_gpt_disks = TAILQ_HEAD_INITIALIZER(g_gpt_disks);
93
94 static bool g_gpt_disabled;
95
96 static void
97 spdk_gpt_base_free(void *ctx)
98 {
99 struct gpt_base *gpt_base = ctx;
100
101 spdk_dma_free(gpt_base->gpt.buf);
102 free(gpt_base);
103 }
104
105 static void
106 spdk_gpt_base_bdev_hotremove_cb(void *_base_bdev)
107 {
108 spdk_bdev_part_base_hotremove(_base_bdev, &g_gpt_disks);
109 }
110
111 static int vbdev_gpt_destruct(void *ctx);
112 static void vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io);
113 static int vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w);
114
115 static struct spdk_bdev_fn_table vbdev_gpt_fn_table = {
116 .destruct = vbdev_gpt_destruct,
117 .submit_request = vbdev_gpt_submit_request,
118 .dump_info_json = vbdev_gpt_dump_info_json,
119 };
120
121 static struct gpt_base *
122 spdk_gpt_base_bdev_init(struct spdk_bdev *bdev)
123 {
124 struct gpt_base *gpt_base;
125 struct spdk_gpt *gpt;
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 gpt_base->part_base = spdk_bdev_part_base_construct(bdev,
134 spdk_gpt_base_bdev_hotremove_cb,
135 &gpt_if, &vbdev_gpt_fn_table,
136 &g_gpt_disks, spdk_gpt_base_free, gpt_base,
137 sizeof(struct gpt_channel), NULL, NULL);
138 if (!gpt_base->part_base) {
139 free(gpt_base);
140 SPDK_ERRLOG("cannot construct gpt_base");
141 return NULL;
142 }
143
144 gpt = &gpt_base->gpt;
145 gpt->buf_size = spdk_max(SPDK_GPT_BUFFER_SIZE, bdev->blocklen);
146 gpt->buf = spdk_dma_zmalloc(gpt->buf_size, spdk_bdev_get_buf_align(bdev), NULL);
147 if (!gpt->buf) {
148 SPDK_ERRLOG("Cannot alloc buf\n");
149 spdk_bdev_part_base_free(gpt_base->part_base);
150 return NULL;
151 }
152
153 gpt->sector_size = bdev->blocklen;
154 gpt->total_sectors = bdev->blockcnt;
155 gpt->lba_start = 0;
156 gpt->lba_end = gpt->total_sectors - 1;
157
158 return gpt_base;
159 }
160
161 static int
162 vbdev_gpt_destruct(void *ctx)
163 {
164 struct gpt_disk *gpt_disk = ctx;
165
166 return spdk_bdev_part_free(&gpt_disk->part);
167 }
168
169 static void
170 vbdev_gpt_resubmit_request(void *arg)
171 {
172 struct gpt_io *io = (struct gpt_io *)arg;
173
174 vbdev_gpt_submit_request(io->ch, io->bdev_io);
175 }
176
177 static void
178 vbdev_gpt_queue_io(struct gpt_io *io)
179 {
180 int rc;
181
182 io->bdev_io_wait.bdev = io->bdev_io->bdev;
183 io->bdev_io_wait.cb_fn = vbdev_gpt_resubmit_request;
184 io->bdev_io_wait.cb_arg = io;
185
186 rc = spdk_bdev_queue_io_wait(io->bdev_io->bdev,
187 io->ch, &io->bdev_io_wait);
188 if (rc != 0) {
189 SPDK_ERRLOG("Queue io failed in vbdev_gpt_queue_io, rc=%d.\n", rc);
190 spdk_bdev_io_complete(io->bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
191 }
192 }
193
194 static void
195 vbdev_gpt_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
196 {
197 struct gpt_channel *ch = spdk_io_channel_get_ctx(_ch);
198 struct gpt_io *io = (struct gpt_io *)bdev_io->driver_ctx;
199 int rc;
200
201 rc = spdk_bdev_part_submit_request(&ch->part_ch, bdev_io);
202 if (rc) {
203 if (rc == -ENOMEM) {
204 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "gpt: no memory, queue io\n");
205 io->ch = _ch;
206 io->bdev_io = bdev_io;
207 vbdev_gpt_queue_io(io);
208 } else {
209 SPDK_ERRLOG("gpt: error on bdev_io submission, rc=%d.\n", rc);
210 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
211 }
212 }
213 }
214
215 static void
216 write_guid(struct spdk_json_write_ctx *w, const struct spdk_gpt_guid *guid)
217 {
218 spdk_json_write_string_fmt(w, "%08x-%04x-%04x-%04x-%04x%08x",
219 from_le32(&guid->raw[0]),
220 from_le16(&guid->raw[4]),
221 from_le16(&guid->raw[6]),
222 from_be16(&guid->raw[8]),
223 from_be16(&guid->raw[10]),
224 from_be32(&guid->raw[12]));
225 }
226
227 static void
228 write_string_utf16le(struct spdk_json_write_ctx *w, const uint16_t *str, size_t max_len)
229 {
230 size_t len;
231 const uint16_t *p;
232
233 for (len = 0, p = str; len < max_len && *p; p++) {
234 len++;
235 }
236
237 spdk_json_write_string_utf16le_raw(w, str, len);
238 }
239
240 static int
241 vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
242 {
243 struct gpt_disk *gpt_disk = SPDK_CONTAINEROF(ctx, struct gpt_disk, part);
244 struct spdk_bdev_part_base *base_bdev = spdk_bdev_part_get_base(&gpt_disk->part);
245 struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(base_bdev);
246 struct spdk_bdev *part_base_bdev = spdk_bdev_part_base_get_bdev(base_bdev);
247 struct spdk_gpt *gpt = &gpt_base->gpt;
248 struct spdk_gpt_partition_entry *gpt_entry = &gpt->partitions[gpt_disk->partition_index];
249 uint64_t offset_blocks = spdk_bdev_part_get_offset_blocks(&gpt_disk->part);
250
251 spdk_json_write_name(w, "gpt");
252 spdk_json_write_object_begin(w);
253
254 spdk_json_write_name(w, "base_bdev");
255 spdk_json_write_string(w, spdk_bdev_get_name(part_base_bdev));
256
257 spdk_json_write_name(w, "offset_blocks");
258 spdk_json_write_uint64(w, offset_blocks);
259
260 spdk_json_write_name(w, "partition_type_guid");
261 write_guid(w, &gpt_entry->part_type_guid);
262
263 spdk_json_write_name(w, "unique_partition_guid");
264 write_guid(w, &gpt_entry->unique_partition_guid);
265
266 spdk_json_write_name(w, "partition_name");
267 write_string_utf16le(w, gpt_entry->partition_name, SPDK_COUNTOF(gpt_entry->partition_name));
268
269 spdk_json_write_object_end(w);
270
271 return 0;
272 }
273
274 static int
275 vbdev_gpt_create_bdevs(struct gpt_base *gpt_base)
276 {
277 uint32_t num_partition_entries;
278 uint64_t i, head_lba_start, head_lba_end;
279 uint32_t num_partitions;
280 struct spdk_gpt_partition_entry *p;
281 struct gpt_disk *d;
282 struct spdk_gpt *gpt;
283 char *name;
284 struct spdk_bdev *base_bdev;
285 int rc;
286
287 gpt = &gpt_base->gpt;
288 num_partition_entries = from_le32(&gpt->header->num_partition_entries);
289 head_lba_start = from_le64(&gpt->header->first_usable_lba);
290 head_lba_end = from_le64(&gpt->header->last_usable_lba);
291 num_partitions = 0;
292
293 for (i = 0; i < num_partition_entries; i++) {
294 p = &gpt->partitions[i];
295 uint64_t lba_start = from_le64(&p->starting_lba);
296 uint64_t lba_end = from_le64(&p->ending_lba);
297
298 if (!SPDK_GPT_GUID_EQUAL(&gpt->partitions[i].part_type_guid,
299 &SPDK_GPT_PART_TYPE_GUID) ||
300 lba_start == 0) {
301 continue;
302 }
303 if (lba_start < head_lba_start || lba_end > head_lba_end) {
304 continue;
305 }
306
307 d = calloc(1, sizeof(*d));
308 if (!d) {
309 SPDK_ERRLOG("Memory allocation failure\n");
310 return -1;
311 }
312
313 /* index start at 1 instead of 0 to match the existing style */
314 base_bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
315 name = spdk_sprintf_alloc("%sp%" PRIu64, spdk_bdev_get_name(base_bdev), i + 1);
316 if (!name) {
317 SPDK_ERRLOG("name allocation failure\n");
318 free(d);
319 return -1;
320 }
321
322 rc = spdk_bdev_part_construct(&d->part, gpt_base->part_base, name,
323 lba_start, lba_end - lba_start, "GPT Disk");
324 free(name);
325 if (rc) {
326 SPDK_ERRLOG("could not construct bdev part\n");
327 /* spdk_bdev_part_construct will free name on failure */
328 free(d);
329 return -1;
330 }
331 num_partitions++;
332 d->partition_index = i;
333 }
334
335 return num_partitions;
336 }
337
338 static void
339 spdk_gpt_bdev_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg)
340 {
341 struct gpt_base *gpt_base = (struct gpt_base *)arg;
342 struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
343 int rc, num_partitions = 0;
344
345 spdk_bdev_free_io(bdev_io);
346 spdk_put_io_channel(gpt_base->ch);
347 gpt_base->ch = NULL;
348
349 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) {
350 SPDK_ERRLOG("Gpt: bdev=%s io error status=%d\n",
351 spdk_bdev_get_name(bdev), status);
352 goto end;
353 }
354
355 rc = spdk_gpt_parse(&gpt_base->gpt);
356 if (rc) {
357 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to parse gpt\n");
358 goto end;
359 }
360
361 num_partitions = vbdev_gpt_create_bdevs(gpt_base);
362 if (num_partitions < 0) {
363 SPDK_DEBUGLOG(SPDK_LOG_VBDEV_GPT, "Failed to split dev=%s by gpt table\n",
364 spdk_bdev_get_name(bdev));
365 }
366
367 end:
368 /*
369 * Notify the generic bdev layer that the actions related to the original examine
370 * callback are now completed.
371 */
372 spdk_bdev_module_examine_done(&gpt_if);
373
374 /*
375 * vbdev_gpt_create_bdevs returns the number of bdevs created upon success.
376 * We can branch on this value.
377 */
378 if (num_partitions <= 0) {
379 /* If no gpt_disk instances were created, free the base context */
380 spdk_bdev_part_base_free(gpt_base->part_base);
381 }
382 }
383
384 static int
385 vbdev_gpt_read_gpt(struct spdk_bdev *bdev)
386 {
387 struct gpt_base *gpt_base;
388 struct spdk_bdev_desc *part_base_desc;
389 int rc;
390
391 gpt_base = spdk_gpt_base_bdev_init(bdev);
392 if (!gpt_base) {
393 SPDK_ERRLOG("Cannot allocated gpt_base\n");
394 return -1;
395 }
396
397 part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base);
398 gpt_base->ch = spdk_bdev_get_io_channel(part_base_desc);
399 if (gpt_base->ch == NULL) {
400 SPDK_ERRLOG("Failed to get an io_channel.\n");
401 spdk_bdev_part_base_free(gpt_base->part_base);
402 return -1;
403 }
404
405 rc = spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, 0,
406 gpt_base->gpt.buf_size, spdk_gpt_bdev_complete, gpt_base);
407 if (rc < 0) {
408 spdk_put_io_channel(gpt_base->ch);
409 spdk_bdev_part_base_free(gpt_base->part_base);
410 SPDK_ERRLOG("Failed to send bdev_io command\n");
411 return -1;
412 }
413
414 return 0;
415 }
416
417 static int
418 vbdev_gpt_init(void)
419 {
420 struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Gpt");
421
422 if (sp && spdk_conf_section_get_boolval(sp, "Disable", false)) {
423 /* Disable Gpt probe */
424 g_gpt_disabled = true;
425 }
426
427 return 0;
428 }
429
430 static int
431 vbdev_gpt_get_ctx_size(void)
432 {
433 return sizeof(struct gpt_io);
434 }
435
436 static void
437 vbdev_gpt_examine(struct spdk_bdev *bdev)
438 {
439 int rc;
440
441 /* A bdev with fewer than 2 blocks cannot have a GPT. Block 0 has
442 * the MBR and block 1 has the GPT header.
443 */
444 if (g_gpt_disabled || spdk_bdev_get_num_blocks(bdev) < 2) {
445 spdk_bdev_module_examine_done(&gpt_if);
446 return;
447 }
448
449 if (spdk_bdev_get_block_size(bdev) % 512 != 0) {
450 SPDK_ERRLOG("GPT module does not support block size %" PRIu32 " for bdev %s\n",
451 spdk_bdev_get_block_size(bdev), spdk_bdev_get_name(bdev));
452 spdk_bdev_module_examine_done(&gpt_if);
453 return;
454 }
455
456 rc = vbdev_gpt_read_gpt(bdev);
457 if (rc) {
458 spdk_bdev_module_examine_done(&gpt_if);
459 SPDK_ERRLOG("Failed to read info from bdev %s\n", spdk_bdev_get_name(bdev));
460 }
461 }
462
463 SPDK_LOG_REGISTER_COMPONENT("vbdev_gpt", SPDK_LOG_VBDEV_GPT)