|
34 | 34 | #include <linux/lz4.h> |
35 | 35 | #include <linux/mutex.h> |
36 | 36 | #include <linux/spinlock.h> |
| 37 | +#include <linux/string.h> |
37 | 38 | #include <linux/types.h> |
| 39 | +#include <linux/uuid.h> |
38 | 40 |
|
39 | 41 | #include "logger.h" |
40 | 42 | #include "memory-alloc.h" |
|
55 | 57 | #include "slab-depot.h" |
56 | 58 | #include "statistics.h" |
57 | 59 | #include "status-codes.h" |
| 60 | +#include "time-utils.h" |
58 | 61 | #include "vio.h" |
59 | 62 |
|
60 | 63 | #define PARANOID_THREAD_CONSISTENCY_CHECKS 0 |
@@ -286,30 +289,6 @@ static int initialize_super_block(struct vdo *vdo, struct vdo_super_block *super |
286 | 289 | &vdo->super_block.vio); |
287 | 290 | } |
288 | 291 |
|
289 | | -/** |
290 | | - * read_geometry_block() - Synchronously read the geometry block from a vdo's underlying block |
291 | | - * device. |
292 | | - * @vdo: The vdo whose geometry is to be read. |
293 | | - * |
294 | | - * Return: VDO_SUCCESS or an error code. |
295 | | - */ |
296 | | -static int __must_check read_geometry_block(struct vdo *vdo) |
297 | | -{ |
298 | | - int result; |
299 | | - |
300 | | - /* |
301 | | - * This is only safe because, having not already loaded the geometry, the vdo's geometry's |
302 | | - * bio_offset field is 0, so the fact that vio_reset_bio() will subtract that offset from |
303 | | - * the supplied pbn is not a problem. |
304 | | - */ |
305 | | - result = vdo_submit_metadata_vio_wait(&vdo->geometry_block.vio, |
306 | | - VDO_GEOMETRY_BLOCK_LOCATION, REQ_OP_READ); |
307 | | - if (result != VDO_SUCCESS) |
308 | | - return result; |
309 | | - |
310 | | - return vdo_parse_geometry_block(vdo->geometry_block.buffer, &vdo->geometry); |
311 | | -} |
312 | | - |
313 | 292 | static bool get_zone_thread_name(const thread_id_t thread_ids[], zone_count_t count, |
314 | 293 | thread_id_t id, const char *prefix, |
315 | 294 | char *buffer, size_t buffer_length) |
@@ -454,6 +433,67 @@ static int register_vdo(struct vdo *vdo) |
454 | 433 | return result; |
455 | 434 | } |
456 | 435 |
|
| 436 | +/** |
| 437 | + * vdo_format() - Format a block device to function as a new VDO. |
| 438 | + * @vdo: The vdo to format. |
| 439 | + * @error_ptr: The reason for any failure during this call. |
| 440 | + * |
| 441 | + * This function must be called on a device before a VDO can be loaded for the first time. |
| 442 | + * Once a device has been formatted, the VDO can be loaded and shut down repeatedly. |
| 443 | + * If a new VDO is desired, this function should be called again. |
| 444 | + * |
| 445 | + * Return: VDO_SUCCESS or an error |
| 446 | + **/ |
| 447 | +static int __must_check vdo_format(struct vdo *vdo, char **error_ptr) |
| 448 | +{ |
| 449 | + int result; |
| 450 | + uuid_t uuid; |
| 451 | + nonce_t nonce = current_time_us(); |
| 452 | + struct device_config *config = vdo->device_config; |
| 453 | + |
| 454 | + struct index_config index_config = { |
| 455 | + .mem = config->index_memory, |
| 456 | + .sparse = config->index_sparse, |
| 457 | + }; |
| 458 | + |
| 459 | + struct vdo_config vdo_config = { |
| 460 | + .logical_blocks = config->logical_blocks, |
| 461 | + .physical_blocks = config->physical_blocks, |
| 462 | + .slab_size = config->slab_blocks, |
| 463 | + .slab_journal_blocks = DEFAULT_VDO_SLAB_JOURNAL_SIZE, |
| 464 | + .recovery_journal_size = DEFAULT_VDO_RECOVERY_JOURNAL_SIZE, |
| 465 | + }; |
| 466 | + |
| 467 | + uuid_gen(&uuid); |
| 468 | + result = vdo_initialize_volume_geometry(nonce, &uuid, &index_config, &vdo->geometry); |
| 469 | + if (result != VDO_SUCCESS) { |
| 470 | + *error_ptr = "Could not initialize volume geometry during format"; |
| 471 | + return result; |
| 472 | + } |
| 473 | + |
| 474 | + result = vdo_initialize_component_states(&vdo_config, &vdo->geometry, nonce, &vdo->states); |
| 475 | + if (result == VDO_NO_SPACE) { |
| 476 | + block_count_t slab_blocks = config->slab_blocks; |
| 477 | + /* 1 is counting geometry block */ |
| 478 | + block_count_t fixed_layout_size = 1 + |
| 479 | + vdo->geometry.regions[VDO_DATA_REGION].start_block + |
| 480 | + DEFAULT_VDO_BLOCK_MAP_TREE_ROOT_COUNT + |
| 481 | + DEFAULT_VDO_RECOVERY_JOURNAL_SIZE + VDO_SLAB_SUMMARY_BLOCKS; |
| 482 | + block_count_t necessary_size = fixed_layout_size + slab_blocks; |
| 483 | + |
| 484 | + vdo_log_error("Minimum required size for VDO volume: %llu bytes", |
| 485 | + (unsigned long long) necessary_size * VDO_BLOCK_SIZE); |
| 486 | + *error_ptr = "Could not allocate enough space for VDO during format"; |
| 487 | + return result; |
| 488 | + } |
| 489 | + if (result != VDO_SUCCESS) { |
| 490 | + *error_ptr = "Could not initialize data layout during format"; |
| 491 | + return result; |
| 492 | + } |
| 493 | + |
| 494 | + return VDO_SUCCESS; |
| 495 | +} |
| 496 | + |
457 | 497 | /** |
458 | 498 | * initialize_vdo() - Do the portion of initializing a vdo which will clean up after itself on |
459 | 499 | * error. |
@@ -490,12 +530,26 @@ static int initialize_vdo(struct vdo *vdo, struct device_config *config, |
490 | 530 | return result; |
491 | 531 | } |
492 | 532 |
|
493 | | - result = read_geometry_block(vdo); |
| 533 | + result = vdo_submit_metadata_vio_wait(&vdo->geometry_block.vio, |
| 534 | + VDO_GEOMETRY_BLOCK_LOCATION, REQ_OP_READ); |
494 | 535 | if (result != VDO_SUCCESS) { |
495 | 536 | *reason = "Could not load geometry block"; |
496 | 537 | return result; |
497 | 538 | } |
498 | 539 |
|
| 540 | + if (mem_is_zero(vdo->geometry_block.vio.data, VDO_BLOCK_SIZE)) { |
| 541 | + result = vdo_format(vdo, reason); |
| 542 | + if (result != VDO_SUCCESS) |
| 543 | + return result; |
| 544 | + } else { |
| 545 | + result = vdo_parse_geometry_block(vdo->geometry_block.buffer, |
| 546 | + &vdo->geometry); |
| 547 | + if (result != VDO_SUCCESS) { |
| 548 | + *reason = "Could not parse geometry block"; |
| 549 | + return result; |
| 550 | + } |
| 551 | + } |
| 552 | + |
499 | 553 | result = initialize_thread_config(config->thread_counts, &vdo->thread_config); |
500 | 554 | if (result != VDO_SUCCESS) { |
501 | 555 | *reason = "Cannot create thread configuration"; |
|
0 commit comments