|
12 | 12 | #include "permassert.h" |
13 | 13 |
|
14 | 14 | #include "constants.h" |
| 15 | +#include "indexer.h" |
15 | 16 | #include "status-codes.h" |
16 | 17 | #include "types.h" |
17 | 18 |
|
@@ -1486,3 +1487,71 @@ int vdo_decode_super_block(u8 *buffer) |
1486 | 1487 |
|
1487 | 1488 | return ((checksum != saved_checksum) ? VDO_CHECKSUM_MISMATCH : VDO_SUCCESS); |
1488 | 1489 | } |
| 1490 | + |
| 1491 | +/** |
| 1492 | + * vdo_compute_index_blocks() - Compute the number of blocks that the indexer will use. |
| 1493 | + * @config: The index config from which the blocks are calculated. |
| 1494 | + * @index_blocks_ptr: The number of blocks the index will use. |
| 1495 | + * |
| 1496 | + * Return: VDO_SUCCESS or an error code. |
| 1497 | + */ |
| 1498 | +static int vdo_compute_index_blocks(const struct index_config *config, |
| 1499 | + block_count_t *index_blocks_ptr) |
| 1500 | +{ |
| 1501 | + int result; |
| 1502 | + u64 index_bytes; |
| 1503 | + struct uds_parameters uds_parameters = { |
| 1504 | + .memory_size = config->mem, |
| 1505 | + .sparse = config->sparse, |
| 1506 | + }; |
| 1507 | + |
| 1508 | + result = uds_compute_index_size(&uds_parameters, &index_bytes); |
| 1509 | + if (result != UDS_SUCCESS) |
| 1510 | + return vdo_log_error_strerror(result, "error computing index size"); |
| 1511 | + |
| 1512 | + *index_blocks_ptr = index_bytes / VDO_BLOCK_SIZE; |
| 1513 | + return VDO_SUCCESS; |
| 1514 | +} |
| 1515 | + |
| 1516 | +/** |
| 1517 | + * vdo_initialize_volume_geometry() - Initialize the volume geometry so it can be written out. |
| 1518 | + * @nonce: The nonce to use to identify the vdo. |
| 1519 | + * @uuid: The uuid to use to identify the vdo. |
| 1520 | + * @index_config: The config used for structure initialization. |
| 1521 | + * @geometry: The volume geometry to initialize. |
| 1522 | + * |
| 1523 | + * Return: VDO_SUCCESS or an error code. |
| 1524 | + */ |
| 1525 | +int vdo_initialize_volume_geometry(nonce_t nonce, uuid_t *uuid, |
| 1526 | + const struct index_config *index_config, |
| 1527 | + struct volume_geometry *geometry) |
| 1528 | +{ |
| 1529 | + int result; |
| 1530 | + block_count_t index_blocks = 0; |
| 1531 | + |
| 1532 | + result = vdo_compute_index_blocks(index_config, &index_blocks); |
| 1533 | + if (result != VDO_SUCCESS) |
| 1534 | + return result; |
| 1535 | + |
| 1536 | + *geometry = (struct volume_geometry) { |
| 1537 | + /* This is for backwards compatibility. */ |
| 1538 | + .unused = 0, |
| 1539 | + .nonce = nonce, |
| 1540 | + .bio_offset = 0, |
| 1541 | + .regions = { |
| 1542 | + [VDO_INDEX_REGION] = { |
| 1543 | + .id = VDO_INDEX_REGION, |
| 1544 | + .start_block = 1, |
| 1545 | + }, |
| 1546 | + [VDO_DATA_REGION] = { |
| 1547 | + .id = VDO_DATA_REGION, |
| 1548 | + .start_block = 1 + index_blocks, |
| 1549 | + } |
| 1550 | + } |
| 1551 | + }; |
| 1552 | + |
| 1553 | + memcpy(&(geometry->uuid), uuid, sizeof(uuid_t)); |
| 1554 | + memcpy(&geometry->index_config, index_config, sizeof(struct index_config)); |
| 1555 | + |
| 1556 | + return VDO_SUCCESS; |
| 1557 | +} |
0 commit comments