1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2023 Red Hat
8 #include <linux/log2.h>
11 #include "memory-alloc.h"
12 #include "permassert.h"
14 #include "constants.h"
15 #include "status-codes.h"
18 /** The maximum logical space is 4 petabytes, which is 1 terablock. */
19 static const block_count_t MAXIMUM_VDO_LOGICAL_BLOCKS
= 1024ULL * 1024 * 1024 * 1024;
21 /** The maximum physical space is 256 terabytes, which is 64 gigablocks. */
22 static const block_count_t MAXIMUM_VDO_PHYSICAL_BLOCKS
= 1024ULL * 1024 * 1024 * 64;
24 struct geometry_block
{
25 char magic_number
[VDO_GEOMETRY_MAGIC_NUMBER_SIZE
];
26 struct packed_header header
;
30 static const struct header GEOMETRY_BLOCK_HEADER_5_0
= {
31 .id
= VDO_GEOMETRY_BLOCK
,
37 * Note: this size isn't just the payload size following the header, like it is everywhere
40 .size
= sizeof(struct geometry_block
) + sizeof(struct volume_geometry
),
43 static const struct header GEOMETRY_BLOCK_HEADER_4_0
= {
44 .id
= VDO_GEOMETRY_BLOCK
,
50 * Note: this size isn't just the payload size following the header, like it is everywhere
53 .size
= sizeof(struct geometry_block
) + sizeof(struct volume_geometry_4_0
),
56 const u8 VDO_GEOMETRY_MAGIC_NUMBER
[VDO_GEOMETRY_MAGIC_NUMBER_SIZE
+ 1] = "dmvdo001";
58 #define PAGE_HEADER_4_1_SIZE (8 + 8 + 8 + 1 + 1 + 1 + 1)
60 static const struct version_number BLOCK_MAP_4_1
= {
65 const struct header VDO_BLOCK_MAP_HEADER_2_0
= {
71 .size
= sizeof(struct block_map_state_2_0
),
74 const struct header VDO_RECOVERY_JOURNAL_HEADER_7_0
= {
75 .id
= VDO_RECOVERY_JOURNAL
,
80 .size
= sizeof(struct recovery_journal_state_7_0
),
83 const struct header VDO_SLAB_DEPOT_HEADER_2_0
= {
89 .size
= sizeof(struct slab_depot_state_2_0
),
92 static const struct header VDO_LAYOUT_HEADER_3_0
= {
98 .size
= sizeof(struct layout_3_0
) + (sizeof(struct partition_3_0
) * VDO_PARTITION_COUNT
),
101 static const enum partition_id REQUIRED_PARTITIONS
[] = {
102 VDO_BLOCK_MAP_PARTITION
,
103 VDO_SLAB_DEPOT_PARTITION
,
104 VDO_RECOVERY_JOURNAL_PARTITION
,
105 VDO_SLAB_SUMMARY_PARTITION
,
109 * The current version for the data encoded in the super block. This must be changed any time there
110 * is a change to encoding of the component data of any VDO component.
112 static const struct version_number VDO_COMPONENT_DATA_41_0
= {
117 const struct version_number VDO_VOLUME_VERSION_67_0
= {
122 static const struct header SUPER_BLOCK_HEADER_12_0
= {
123 .id
= VDO_SUPER_BLOCK
,
129 /* This is the minimum size, if the super block contains no components. */
130 .size
= VDO_SUPER_BLOCK_FIXED_SIZE
- VDO_ENCODED_HEADER_SIZE
,
134 * validate_version() - Check whether a version matches an expected version.
135 * @expected_version: The expected version.
136 * @actual_version: The version being validated.
137 * @component_name: The name of the component or the calling function (for error logging).
139 * Logs an error describing a mismatch.
141 * Return: VDO_SUCCESS if the versions are the same,
142 * VDO_UNSUPPORTED_VERSION if the versions don't match.
144 static int __must_check
validate_version(struct version_number expected_version
,
145 struct version_number actual_version
,
146 const char *component_name
)
148 if (!vdo_are_same_version(expected_version
, actual_version
)) {
149 return vdo_log_error_strerror(VDO_UNSUPPORTED_VERSION
,
150 "%s version mismatch, expected %d.%d, got %d.%d",
152 expected_version
.major_version
,
153 expected_version
.minor_version
,
154 actual_version
.major_version
,
155 actual_version
.minor_version
);
162 * vdo_validate_header() - Check whether a header matches expectations.
163 * @expected_header: The expected header.
164 * @actual_header: The header being validated.
165 * @exact_size: If true, the size fields of the two headers must be the same, otherwise it is
166 * required that actual_header.size >= expected_header.size.
167 * @name: The name of the component or the calling function (for error logging).
169 * Logs an error describing the first mismatch found.
171 * Return: VDO_SUCCESS if the header meets expectations,
172 * VDO_INCORRECT_COMPONENT if the component ids don't match,
173 * VDO_UNSUPPORTED_VERSION if the versions or sizes don't match.
175 int vdo_validate_header(const struct header
*expected_header
,
176 const struct header
*actual_header
, bool exact_size
,
181 if (expected_header
->id
!= actual_header
->id
) {
182 return vdo_log_error_strerror(VDO_INCORRECT_COMPONENT
,
183 "%s ID mismatch, expected %d, got %d",
184 name
, expected_header
->id
,
188 result
= validate_version(expected_header
->version
, actual_header
->version
,
190 if (result
!= VDO_SUCCESS
)
193 if ((expected_header
->size
> actual_header
->size
) ||
194 (exact_size
&& (expected_header
->size
< actual_header
->size
))) {
195 return vdo_log_error_strerror(VDO_UNSUPPORTED_VERSION
,
196 "%s size mismatch, expected %zu, got %zu",
197 name
, expected_header
->size
,
198 actual_header
->size
);
204 static void encode_version_number(u8
*buffer
, size_t *offset
,
205 struct version_number version
)
207 struct packed_version_number packed
= vdo_pack_version_number(version
);
209 memcpy(buffer
+ *offset
, &packed
, sizeof(packed
));
210 *offset
+= sizeof(packed
);
213 void vdo_encode_header(u8
*buffer
, size_t *offset
, const struct header
*header
)
215 struct packed_header packed
= vdo_pack_header(header
);
217 memcpy(buffer
+ *offset
, &packed
, sizeof(packed
));
218 *offset
+= sizeof(packed
);
221 static void decode_version_number(u8
*buffer
, size_t *offset
,
222 struct version_number
*version
)
224 struct packed_version_number packed
;
226 memcpy(&packed
, buffer
+ *offset
, sizeof(packed
));
227 *offset
+= sizeof(packed
);
228 *version
= vdo_unpack_version_number(packed
);
231 void vdo_decode_header(u8
*buffer
, size_t *offset
, struct header
*header
)
233 struct packed_header packed
;
235 memcpy(&packed
, buffer
+ *offset
, sizeof(packed
));
236 *offset
+= sizeof(packed
);
238 *header
= vdo_unpack_header(&packed
);
242 * decode_volume_geometry() - Decode the on-disk representation of a volume geometry from a buffer.
243 * @buffer: A buffer to decode from.
244 * @offset: The offset in the buffer at which to decode.
245 * @geometry: The structure to receive the decoded fields.
246 * @version: The geometry block version to decode.
248 static void decode_volume_geometry(u8
*buffer
, size_t *offset
,
249 struct volume_geometry
*geometry
, u32 version
)
252 enum volume_region_id id
;
254 block_count_t bio_offset
= 0;
257 /* This is for backwards compatibility. */
258 decode_u32_le(buffer
, offset
, &unused
);
259 geometry
->unused
= unused
;
261 decode_u64_le(buffer
, offset
, &nonce
);
262 geometry
->nonce
= nonce
;
264 memcpy((unsigned char *) &geometry
->uuid
, buffer
+ *offset
, sizeof(uuid_t
));
265 *offset
+= sizeof(uuid_t
);
268 decode_u64_le(buffer
, offset
, &bio_offset
);
269 geometry
->bio_offset
= bio_offset
;
271 for (id
= 0; id
< VDO_VOLUME_REGION_COUNT
; id
++) {
272 physical_block_number_t start_block
;
273 enum volume_region_id saved_id
;
275 decode_u32_le(buffer
, offset
, &saved_id
);
276 decode_u64_le(buffer
, offset
, &start_block
);
278 geometry
->regions
[id
] = (struct volume_region
) {
280 .start_block
= start_block
,
284 decode_u32_le(buffer
, offset
, &mem
);
285 *offset
+= sizeof(u32
);
286 sparse
= buffer
[(*offset
)++];
288 geometry
->index_config
= (struct index_config
) {
295 * vdo_parse_geometry_block() - Decode and validate an encoded geometry block.
296 * @block: The encoded geometry block.
297 * @geometry: The structure to receive the decoded fields.
299 int __must_check
vdo_parse_geometry_block(u8
*block
, struct volume_geometry
*geometry
)
301 u32 checksum
, saved_checksum
;
302 struct header header
;
306 if (memcmp(block
, VDO_GEOMETRY_MAGIC_NUMBER
, VDO_GEOMETRY_MAGIC_NUMBER_SIZE
) != 0)
307 return VDO_BAD_MAGIC
;
308 offset
+= VDO_GEOMETRY_MAGIC_NUMBER_SIZE
;
310 vdo_decode_header(block
, &offset
, &header
);
311 if (header
.version
.major_version
<= 4) {
312 result
= vdo_validate_header(&GEOMETRY_BLOCK_HEADER_4_0
, &header
,
315 result
= vdo_validate_header(&GEOMETRY_BLOCK_HEADER_5_0
, &header
,
318 if (result
!= VDO_SUCCESS
)
321 decode_volume_geometry(block
, &offset
, geometry
, header
.version
.major_version
);
323 result
= VDO_ASSERT(header
.size
== offset
+ sizeof(u32
),
324 "should have decoded up to the geometry checksum");
325 if (result
!= VDO_SUCCESS
)
328 /* Decode and verify the checksum. */
329 checksum
= vdo_crc32(block
, offset
);
330 decode_u32_le(block
, &offset
, &saved_checksum
);
332 return ((checksum
== saved_checksum
) ? VDO_SUCCESS
: VDO_CHECKSUM_MISMATCH
);
335 struct block_map_page
*vdo_format_block_map_page(void *buffer
, nonce_t nonce
,
336 physical_block_number_t pbn
,
339 struct block_map_page
*page
= buffer
;
341 memset(buffer
, 0, VDO_BLOCK_SIZE
);
342 page
->version
= vdo_pack_version_number(BLOCK_MAP_4_1
);
343 page
->header
.nonce
= __cpu_to_le64(nonce
);
344 page
->header
.pbn
= __cpu_to_le64(pbn
);
345 page
->header
.initialized
= initialized
;
349 enum block_map_page_validity
vdo_validate_block_map_page(struct block_map_page
*page
,
351 physical_block_number_t pbn
)
353 BUILD_BUG_ON(sizeof(struct block_map_page_header
) != PAGE_HEADER_4_1_SIZE
);
355 if (!vdo_are_same_version(BLOCK_MAP_4_1
,
356 vdo_unpack_version_number(page
->version
)) ||
357 !page
->header
.initialized
|| (nonce
!= __le64_to_cpu(page
->header
.nonce
)))
358 return VDO_BLOCK_MAP_PAGE_INVALID
;
360 if (pbn
!= vdo_get_block_map_page_pbn(page
))
361 return VDO_BLOCK_MAP_PAGE_BAD
;
363 return VDO_BLOCK_MAP_PAGE_VALID
;
366 static int decode_block_map_state_2_0(u8
*buffer
, size_t *offset
,
367 struct block_map_state_2_0
*state
)
369 size_t initial_offset
;
370 block_count_t flat_page_count
, root_count
;
371 physical_block_number_t flat_page_origin
, root_origin
;
372 struct header header
;
375 vdo_decode_header(buffer
, offset
, &header
);
376 result
= vdo_validate_header(&VDO_BLOCK_MAP_HEADER_2_0
, &header
, true, __func__
);
377 if (result
!= VDO_SUCCESS
)
380 initial_offset
= *offset
;
382 decode_u64_le(buffer
, offset
, &flat_page_origin
);
383 result
= VDO_ASSERT(flat_page_origin
== VDO_BLOCK_MAP_FLAT_PAGE_ORIGIN
,
384 "Flat page origin must be %u (recorded as %llu)",
385 VDO_BLOCK_MAP_FLAT_PAGE_ORIGIN
,
386 (unsigned long long) state
->flat_page_origin
);
387 if (result
!= VDO_SUCCESS
)
390 decode_u64_le(buffer
, offset
, &flat_page_count
);
391 result
= VDO_ASSERT(flat_page_count
== 0,
392 "Flat page count must be 0 (recorded as %llu)",
393 (unsigned long long) state
->flat_page_count
);
394 if (result
!= VDO_SUCCESS
)
397 decode_u64_le(buffer
, offset
, &root_origin
);
398 decode_u64_le(buffer
, offset
, &root_count
);
400 result
= VDO_ASSERT(VDO_BLOCK_MAP_HEADER_2_0
.size
== *offset
- initial_offset
,
401 "decoded block map component size must match header size");
402 if (result
!= VDO_SUCCESS
)
405 *state
= (struct block_map_state_2_0
) {
406 .flat_page_origin
= flat_page_origin
,
407 .flat_page_count
= flat_page_count
,
408 .root_origin
= root_origin
,
409 .root_count
= root_count
,
415 static void encode_block_map_state_2_0(u8
*buffer
, size_t *offset
,
416 struct block_map_state_2_0 state
)
418 size_t initial_offset
;
420 vdo_encode_header(buffer
, offset
, &VDO_BLOCK_MAP_HEADER_2_0
);
422 initial_offset
= *offset
;
423 encode_u64_le(buffer
, offset
, state
.flat_page_origin
);
424 encode_u64_le(buffer
, offset
, state
.flat_page_count
);
425 encode_u64_le(buffer
, offset
, state
.root_origin
);
426 encode_u64_le(buffer
, offset
, state
.root_count
);
428 VDO_ASSERT_LOG_ONLY(VDO_BLOCK_MAP_HEADER_2_0
.size
== *offset
- initial_offset
,
429 "encoded block map component size must match header size");
433 * vdo_compute_new_forest_pages() - Compute the number of pages which must be allocated at each
434 * level in order to grow the forest to a new number of entries.
435 * @entries: The new number of entries the block map must address.
437 * Return: The total number of non-leaf pages required.
439 block_count_t
vdo_compute_new_forest_pages(root_count_t root_count
,
440 struct boundary
*old_sizes
,
441 block_count_t entries
,
442 struct boundary
*new_sizes
)
444 page_count_t leaf_pages
= max(vdo_compute_block_map_page_count(entries
), 1U);
445 page_count_t level_size
= DIV_ROUND_UP(leaf_pages
, root_count
);
446 block_count_t total_pages
= 0;
449 for (height
= 0; height
< VDO_BLOCK_MAP_TREE_HEIGHT
; height
++) {
450 block_count_t new_pages
;
452 level_size
= DIV_ROUND_UP(level_size
, VDO_BLOCK_MAP_ENTRIES_PER_PAGE
);
453 new_sizes
->levels
[height
] = level_size
;
454 new_pages
= level_size
;
455 if (old_sizes
!= NULL
)
456 new_pages
-= old_sizes
->levels
[height
];
457 total_pages
+= (new_pages
* root_count
);
464 * encode_recovery_journal_state_7_0() - Encode the state of a recovery journal.
466 * Return: VDO_SUCCESS or an error code.
468 static void encode_recovery_journal_state_7_0(u8
*buffer
, size_t *offset
,
469 struct recovery_journal_state_7_0 state
)
471 size_t initial_offset
;
473 vdo_encode_header(buffer
, offset
, &VDO_RECOVERY_JOURNAL_HEADER_7_0
);
475 initial_offset
= *offset
;
476 encode_u64_le(buffer
, offset
, state
.journal_start
);
477 encode_u64_le(buffer
, offset
, state
.logical_blocks_used
);
478 encode_u64_le(buffer
, offset
, state
.block_map_data_blocks
);
480 VDO_ASSERT_LOG_ONLY(VDO_RECOVERY_JOURNAL_HEADER_7_0
.size
== *offset
- initial_offset
,
481 "encoded recovery journal component size must match header size");
485 * decode_recovery_journal_state_7_0() - Decode the state of a recovery journal saved in a buffer.
486 * @buffer: The buffer containing the saved state.
487 * @state: A pointer to a recovery journal state to hold the result of a successful decode.
489 * Return: VDO_SUCCESS or an error code.
491 static int __must_check
decode_recovery_journal_state_7_0(u8
*buffer
, size_t *offset
,
492 struct recovery_journal_state_7_0
*state
)
494 struct header header
;
496 size_t initial_offset
;
497 sequence_number_t journal_start
;
498 block_count_t logical_blocks_used
, block_map_data_blocks
;
500 vdo_decode_header(buffer
, offset
, &header
);
501 result
= vdo_validate_header(&VDO_RECOVERY_JOURNAL_HEADER_7_0
, &header
, true,
503 if (result
!= VDO_SUCCESS
)
506 initial_offset
= *offset
;
507 decode_u64_le(buffer
, offset
, &journal_start
);
508 decode_u64_le(buffer
, offset
, &logical_blocks_used
);
509 decode_u64_le(buffer
, offset
, &block_map_data_blocks
);
511 result
= VDO_ASSERT(VDO_RECOVERY_JOURNAL_HEADER_7_0
.size
== *offset
- initial_offset
,
512 "decoded recovery journal component size must match header size");
513 if (result
!= VDO_SUCCESS
)
516 *state
= (struct recovery_journal_state_7_0
) {
517 .journal_start
= journal_start
,
518 .logical_blocks_used
= logical_blocks_used
,
519 .block_map_data_blocks
= block_map_data_blocks
,
526 * vdo_get_journal_operation_name() - Get the name of a journal operation.
527 * @operation: The operation to name.
529 * Return: The name of the operation.
531 const char *vdo_get_journal_operation_name(enum journal_operation operation
)
534 case VDO_JOURNAL_DATA_REMAPPING
:
535 return "data remapping";
537 case VDO_JOURNAL_BLOCK_MAP_REMAPPING
:
538 return "block map remapping";
541 return "unknown journal operation";
546 * encode_slab_depot_state_2_0() - Encode the state of a slab depot into a buffer.
548 static void encode_slab_depot_state_2_0(u8
*buffer
, size_t *offset
,
549 struct slab_depot_state_2_0 state
)
551 size_t initial_offset
;
553 vdo_encode_header(buffer
, offset
, &VDO_SLAB_DEPOT_HEADER_2_0
);
555 initial_offset
= *offset
;
556 encode_u64_le(buffer
, offset
, state
.slab_config
.slab_blocks
);
557 encode_u64_le(buffer
, offset
, state
.slab_config
.data_blocks
);
558 encode_u64_le(buffer
, offset
, state
.slab_config
.reference_count_blocks
);
559 encode_u64_le(buffer
, offset
, state
.slab_config
.slab_journal_blocks
);
560 encode_u64_le(buffer
, offset
, state
.slab_config
.slab_journal_flushing_threshold
);
561 encode_u64_le(buffer
, offset
, state
.slab_config
.slab_journal_blocking_threshold
);
562 encode_u64_le(buffer
, offset
, state
.slab_config
.slab_journal_scrubbing_threshold
);
563 encode_u64_le(buffer
, offset
, state
.first_block
);
564 encode_u64_le(buffer
, offset
, state
.last_block
);
565 buffer
[(*offset
)++] = state
.zone_count
;
567 VDO_ASSERT_LOG_ONLY(VDO_SLAB_DEPOT_HEADER_2_0
.size
== *offset
- initial_offset
,
568 "encoded block map component size must match header size");
572 * decode_slab_depot_state_2_0() - Decode slab depot component state version 2.0 from a buffer.
574 * Return: VDO_SUCCESS or an error code.
576 static int decode_slab_depot_state_2_0(u8
*buffer
, size_t *offset
,
577 struct slab_depot_state_2_0
*state
)
579 struct header header
;
581 size_t initial_offset
;
582 struct slab_config slab_config
;
584 physical_block_number_t first_block
, last_block
;
585 zone_count_t zone_count
;
587 vdo_decode_header(buffer
, offset
, &header
);
588 result
= vdo_validate_header(&VDO_SLAB_DEPOT_HEADER_2_0
, &header
, true,
590 if (result
!= VDO_SUCCESS
)
593 initial_offset
= *offset
;
594 decode_u64_le(buffer
, offset
, &count
);
595 slab_config
.slab_blocks
= count
;
597 decode_u64_le(buffer
, offset
, &count
);
598 slab_config
.data_blocks
= count
;
600 decode_u64_le(buffer
, offset
, &count
);
601 slab_config
.reference_count_blocks
= count
;
603 decode_u64_le(buffer
, offset
, &count
);
604 slab_config
.slab_journal_blocks
= count
;
606 decode_u64_le(buffer
, offset
, &count
);
607 slab_config
.slab_journal_flushing_threshold
= count
;
609 decode_u64_le(buffer
, offset
, &count
);
610 slab_config
.slab_journal_blocking_threshold
= count
;
612 decode_u64_le(buffer
, offset
, &count
);
613 slab_config
.slab_journal_scrubbing_threshold
= count
;
615 decode_u64_le(buffer
, offset
, &first_block
);
616 decode_u64_le(buffer
, offset
, &last_block
);
617 zone_count
= buffer
[(*offset
)++];
619 result
= VDO_ASSERT(VDO_SLAB_DEPOT_HEADER_2_0
.size
== *offset
- initial_offset
,
620 "decoded slab depot component size must match header size");
621 if (result
!= VDO_SUCCESS
)
624 *state
= (struct slab_depot_state_2_0
) {
625 .slab_config
= slab_config
,
626 .first_block
= first_block
,
627 .last_block
= last_block
,
628 .zone_count
= zone_count
,
635 * vdo_configure_slab_depot() - Configure the slab depot.
636 * @partition: The slab depot partition
637 * @slab_config: The configuration of a single slab.
638 * @zone_count: The number of zones the depot will use.
639 * @state: The state structure to be configured.
641 * Configures the slab_depot for the specified storage capacity, finding the number of data blocks
642 * that will fit and still leave room for the depot metadata, then return the saved state for that
645 * Return: VDO_SUCCESS or an error code.
647 int vdo_configure_slab_depot(const struct partition
*partition
,
648 struct slab_config slab_config
, zone_count_t zone_count
,
649 struct slab_depot_state_2_0
*state
)
651 block_count_t total_slab_blocks
, total_data_blocks
;
653 physical_block_number_t last_block
;
654 block_count_t slab_size
= slab_config
.slab_blocks
;
656 vdo_log_debug("slabDepot %s(block_count=%llu, first_block=%llu, slab_size=%llu, zone_count=%u)",
657 __func__
, (unsigned long long) partition
->count
,
658 (unsigned long long) partition
->offset
,
659 (unsigned long long) slab_size
, zone_count
);
661 /* We do not allow runt slabs, so we waste up to a slab's worth. */
662 slab_count
= (partition
->count
/ slab_size
);
666 if (slab_count
> MAX_VDO_SLABS
)
667 return VDO_TOO_MANY_SLABS
;
669 total_slab_blocks
= slab_count
* slab_config
.slab_blocks
;
670 total_data_blocks
= slab_count
* slab_config
.data_blocks
;
671 last_block
= partition
->offset
+ total_slab_blocks
;
673 *state
= (struct slab_depot_state_2_0
) {
674 .slab_config
= slab_config
,
675 .first_block
= partition
->offset
,
676 .last_block
= last_block
,
677 .zone_count
= zone_count
,
680 vdo_log_debug("slab_depot last_block=%llu, total_data_blocks=%llu, slab_count=%zu, left_over=%llu",
681 (unsigned long long) last_block
,
682 (unsigned long long) total_data_blocks
, slab_count
,
683 (unsigned long long) (partition
->count
- (last_block
- partition
->offset
)));
689 * vdo_configure_slab() - Measure and initialize the configuration to use for each slab.
690 * @slab_size: The number of blocks per slab.
691 * @slab_journal_blocks: The number of blocks for the slab journal.
692 * @slab_config: The slab configuration to initialize.
694 * Return: VDO_SUCCESS or an error code.
696 int vdo_configure_slab(block_count_t slab_size
, block_count_t slab_journal_blocks
,
697 struct slab_config
*slab_config
)
699 block_count_t ref_blocks
, meta_blocks
, data_blocks
;
700 block_count_t flushing_threshold
, remaining
, blocking_threshold
;
701 block_count_t minimal_extra_space
, scrubbing_threshold
;
703 if (slab_journal_blocks
>= slab_size
)
704 return VDO_BAD_CONFIGURATION
;
707 * This calculation should technically be a recurrence, but the total number of metadata
708 * blocks is currently less than a single block of ref_counts, so we'd gain at most one
709 * data block in each slab with more iteration.
711 ref_blocks
= vdo_get_saved_reference_count_size(slab_size
- slab_journal_blocks
);
712 meta_blocks
= (ref_blocks
+ slab_journal_blocks
);
714 /* Make sure test code hasn't configured slabs to be too small. */
715 if (meta_blocks
>= slab_size
)
716 return VDO_BAD_CONFIGURATION
;
719 * If the slab size is very small, assume this must be a unit test and override the number
720 * of data blocks to be a power of two (wasting blocks in the slab). Many tests need their
721 * data_blocks fields to be the exact capacity of the configured volume, and that used to
722 * fall out since they use a power of two for the number of data blocks, the slab size was
723 * a power of two, and every block in a slab was a data block.
725 * TODO: Try to figure out some way of structuring testParameters and unit tests so this
726 * hack isn't needed without having to edit several unit tests every time the metadata size
727 * changes by one block.
729 data_blocks
= slab_size
- meta_blocks
;
730 if ((slab_size
< 1024) && !is_power_of_2(data_blocks
))
731 data_blocks
= ((block_count_t
) 1 << ilog2(data_blocks
));
734 * Configure the slab journal thresholds. The flush threshold is 168 of 224 blocks in
735 * production, or 3/4ths, so we use this ratio for all sizes.
737 flushing_threshold
= ((slab_journal_blocks
* 3) + 3) / 4;
739 * The blocking threshold should be far enough from the flushing threshold to not produce
740 * delays, but far enough from the end of the journal to allow multiple successive recovery
743 remaining
= slab_journal_blocks
- flushing_threshold
;
744 blocking_threshold
= flushing_threshold
+ ((remaining
* 5) / 7);
745 /* The scrubbing threshold should be at least 2048 entries before the end of the journal. */
746 minimal_extra_space
= 1 + (MAXIMUM_VDO_USER_VIOS
/ VDO_SLAB_JOURNAL_FULL_ENTRIES_PER_BLOCK
);
747 scrubbing_threshold
= blocking_threshold
;
748 if (slab_journal_blocks
> minimal_extra_space
)
749 scrubbing_threshold
= slab_journal_blocks
- minimal_extra_space
;
750 if (blocking_threshold
> scrubbing_threshold
)
751 blocking_threshold
= scrubbing_threshold
;
753 *slab_config
= (struct slab_config
) {
754 .slab_blocks
= slab_size
,
755 .data_blocks
= data_blocks
,
756 .reference_count_blocks
= ref_blocks
,
757 .slab_journal_blocks
= slab_journal_blocks
,
758 .slab_journal_flushing_threshold
= flushing_threshold
,
759 .slab_journal_blocking_threshold
= blocking_threshold
,
760 .slab_journal_scrubbing_threshold
= scrubbing_threshold
};
765 * vdo_decode_slab_journal_entry() - Decode a slab journal entry.
766 * @block: The journal block holding the entry.
767 * @entry_count: The number of the entry.
769 * Return: The decoded entry.
771 struct slab_journal_entry
vdo_decode_slab_journal_entry(struct packed_slab_journal_block
*block
,
772 journal_entry_count_t entry_count
)
774 struct slab_journal_entry entry
=
775 vdo_unpack_slab_journal_entry(&block
->payload
.entries
[entry_count
]);
777 if (block
->header
.has_block_map_increments
&&
778 ((block
->payload
.full_entries
.entry_types
[entry_count
/ 8] &
779 ((u8
) 1 << (entry_count
% 8))) != 0))
780 entry
.operation
= VDO_JOURNAL_BLOCK_MAP_REMAPPING
;
786 * allocate_partition() - Allocate a partition and add it to a layout.
787 * @layout: The layout containing the partition.
788 * @id: The id of the partition.
789 * @offset: The offset into the layout at which the partition begins.
790 * @size: The size of the partition in blocks.
792 * Return: VDO_SUCCESS or an error.
794 static int allocate_partition(struct layout
*layout
, u8 id
,
795 physical_block_number_t offset
, block_count_t size
)
797 struct partition
*partition
;
800 result
= vdo_allocate(1, struct partition
, __func__
, &partition
);
801 if (result
!= VDO_SUCCESS
)
805 partition
->offset
= offset
;
806 partition
->count
= size
;
807 partition
->next
= layout
->head
;
808 layout
->head
= partition
;
814 * make_partition() - Create a new partition from the beginning or end of the unused space in a
816 * @layout: The layout.
817 * @id: The id of the partition to make.
818 * @size: The number of blocks to carve out; if 0, all remaining space will be used.
819 * @beginning: True if the partition should start at the beginning of the unused space.
821 * Return: A success or error code, particularly VDO_NO_SPACE if there are fewer than size blocks
824 static int __must_check
make_partition(struct layout
*layout
, enum partition_id id
,
825 block_count_t size
, bool beginning
)
828 physical_block_number_t offset
;
829 block_count_t free_blocks
= layout
->last_free
- layout
->first_free
;
832 if (free_blocks
== 0)
835 } else if (size
> free_blocks
) {
839 result
= vdo_get_partition(layout
, id
, NULL
);
840 if (result
!= VDO_UNKNOWN_PARTITION
)
841 return VDO_PARTITION_EXISTS
;
843 offset
= beginning
? layout
->first_free
: (layout
->last_free
- size
);
845 result
= allocate_partition(layout
, id
, offset
, size
);
846 if (result
!= VDO_SUCCESS
)
849 layout
->num_partitions
++;
851 layout
->first_free
+= size
;
853 layout
->last_free
= layout
->last_free
- size
;
859 * vdo_initialize_layout() - Lay out the partitions of a vdo.
860 * @size: The entire size of the vdo.
861 * @offset: The start of the layout on the underlying storage in blocks.
862 * @block_map_blocks: The size of the block map partition.
863 * @journal_blocks: The size of the journal partition.
864 * @summary_blocks: The size of the slab summary partition.
865 * @layout: The layout to initialize.
867 * Return: VDO_SUCCESS or an error.
869 int vdo_initialize_layout(block_count_t size
, physical_block_number_t offset
,
870 block_count_t block_map_blocks
, block_count_t journal_blocks
,
871 block_count_t summary_blocks
, struct layout
*layout
)
874 block_count_t necessary_size
=
875 (offset
+ block_map_blocks
+ journal_blocks
+ summary_blocks
);
877 if (necessary_size
> size
)
878 return vdo_log_error_strerror(VDO_NO_SPACE
,
879 "Not enough space to make a VDO");
881 *layout
= (struct layout
) {
884 .first_free
= offset
,
890 result
= make_partition(layout
, VDO_BLOCK_MAP_PARTITION
, block_map_blocks
, true);
891 if (result
!= VDO_SUCCESS
) {
892 vdo_uninitialize_layout(layout
);
896 result
= make_partition(layout
, VDO_SLAB_SUMMARY_PARTITION
, summary_blocks
,
898 if (result
!= VDO_SUCCESS
) {
899 vdo_uninitialize_layout(layout
);
903 result
= make_partition(layout
, VDO_RECOVERY_JOURNAL_PARTITION
, journal_blocks
,
905 if (result
!= VDO_SUCCESS
) {
906 vdo_uninitialize_layout(layout
);
910 result
= make_partition(layout
, VDO_SLAB_DEPOT_PARTITION
, 0, true);
911 if (result
!= VDO_SUCCESS
)
912 vdo_uninitialize_layout(layout
);
918 * vdo_uninitialize_layout() - Clean up a layout.
919 * @layout: The layout to clean up.
921 * All partitions created by this layout become invalid pointers.
923 void vdo_uninitialize_layout(struct layout
*layout
)
925 while (layout
->head
!= NULL
) {
926 struct partition
*part
= layout
->head
;
928 layout
->head
= part
->next
;
932 memset(layout
, 0, sizeof(struct layout
));
936 * vdo_get_partition() - Get a partition by id.
937 * @layout: The layout from which to get a partition.
938 * @id: The id of the partition.
939 * @partition_ptr: A pointer to hold the partition.
941 * Return: VDO_SUCCESS or an error.
943 int vdo_get_partition(struct layout
*layout
, enum partition_id id
,
944 struct partition
**partition_ptr
)
946 struct partition
*partition
;
948 for (partition
= layout
->head
; partition
!= NULL
; partition
= partition
->next
) {
949 if (partition
->id
== id
) {
950 if (partition_ptr
!= NULL
)
951 *partition_ptr
= partition
;
956 return VDO_UNKNOWN_PARTITION
;
960 * vdo_get_known_partition() - Get a partition by id from a validated layout.
961 * @layout: The layout from which to get a partition.
962 * @id: The id of the partition.
964 * Return: the partition
966 struct partition
*vdo_get_known_partition(struct layout
*layout
, enum partition_id id
)
968 struct partition
*partition
;
969 int result
= vdo_get_partition(layout
, id
, &partition
);
971 VDO_ASSERT_LOG_ONLY(result
== VDO_SUCCESS
, "layout has expected partition: %u", id
);
976 static void encode_layout(u8
*buffer
, size_t *offset
, const struct layout
*layout
)
978 const struct partition
*partition
;
979 size_t initial_offset
;
980 struct header header
= VDO_LAYOUT_HEADER_3_0
;
982 BUILD_BUG_ON(sizeof(enum partition_id
) != sizeof(u8
));
983 VDO_ASSERT_LOG_ONLY(layout
->num_partitions
<= U8_MAX
,
984 "layout partition count must fit in a byte");
986 vdo_encode_header(buffer
, offset
, &header
);
988 initial_offset
= *offset
;
989 encode_u64_le(buffer
, offset
, layout
->first_free
);
990 encode_u64_le(buffer
, offset
, layout
->last_free
);
991 buffer
[(*offset
)++] = layout
->num_partitions
;
993 VDO_ASSERT_LOG_ONLY(sizeof(struct layout_3_0
) == *offset
- initial_offset
,
994 "encoded size of a layout header must match structure");
996 for (partition
= layout
->head
; partition
!= NULL
; partition
= partition
->next
) {
997 buffer
[(*offset
)++] = partition
->id
;
998 encode_u64_le(buffer
, offset
, partition
->offset
);
999 /* This field only exists for backwards compatibility */
1000 encode_u64_le(buffer
, offset
, 0);
1001 encode_u64_le(buffer
, offset
, partition
->count
);
1004 VDO_ASSERT_LOG_ONLY(header
.size
== *offset
- initial_offset
,
1005 "encoded size of a layout must match header size");
1008 static int decode_layout(u8
*buffer
, size_t *offset
, physical_block_number_t start
,
1009 block_count_t size
, struct layout
*layout
)
1011 struct header header
;
1012 struct layout_3_0 layout_header
;
1013 struct partition
*partition
;
1014 size_t initial_offset
;
1015 physical_block_number_t first_free
, last_free
;
1020 vdo_decode_header(buffer
, offset
, &header
);
1021 /* Layout is variable size, so only do a minimum size check here. */
1022 result
= vdo_validate_header(&VDO_LAYOUT_HEADER_3_0
, &header
, false, __func__
);
1023 if (result
!= VDO_SUCCESS
)
1026 initial_offset
= *offset
;
1027 decode_u64_le(buffer
, offset
, &first_free
);
1028 decode_u64_le(buffer
, offset
, &last_free
);
1029 partition_count
= buffer
[(*offset
)++];
1030 layout_header
= (struct layout_3_0
) {
1031 .first_free
= first_free
,
1032 .last_free
= last_free
,
1033 .partition_count
= partition_count
,
1036 result
= VDO_ASSERT(sizeof(struct layout_3_0
) == *offset
- initial_offset
,
1037 "decoded size of a layout header must match structure");
1038 if (result
!= VDO_SUCCESS
)
1041 layout
->start
= start
;
1042 layout
->size
= size
;
1043 layout
->first_free
= layout_header
.first_free
;
1044 layout
->last_free
= layout_header
.last_free
;
1045 layout
->num_partitions
= layout_header
.partition_count
;
1047 if (layout
->num_partitions
> VDO_PARTITION_COUNT
) {
1048 return vdo_log_error_strerror(VDO_UNKNOWN_PARTITION
,
1049 "layout has extra partitions");
1052 for (i
= 0; i
< layout
->num_partitions
; i
++) {
1054 u64 partition_offset
, count
;
1056 id
= buffer
[(*offset
)++];
1057 decode_u64_le(buffer
, offset
, &partition_offset
);
1058 *offset
+= sizeof(u64
);
1059 decode_u64_le(buffer
, offset
, &count
);
1061 result
= allocate_partition(layout
, id
, partition_offset
, count
);
1062 if (result
!= VDO_SUCCESS
) {
1063 vdo_uninitialize_layout(layout
);
1068 /* Validate that the layout has all (and only) the required partitions */
1069 for (i
= 0; i
< VDO_PARTITION_COUNT
; i
++) {
1070 result
= vdo_get_partition(layout
, REQUIRED_PARTITIONS
[i
], &partition
);
1071 if (result
!= VDO_SUCCESS
) {
1072 vdo_uninitialize_layout(layout
);
1073 return vdo_log_error_strerror(result
,
1074 "layout is missing required partition %u",
1075 REQUIRED_PARTITIONS
[i
]);
1078 start
+= partition
->count
;
1081 if (start
!= size
) {
1082 vdo_uninitialize_layout(layout
);
1083 return vdo_log_error_strerror(UDS_BAD_STATE
,
1084 "partitions do not cover the layout");
1091 * pack_vdo_config() - Convert a vdo_config to its packed on-disk representation.
1092 * @config: The vdo config to convert.
1094 * Return: The platform-independent representation of the config.
1096 static struct packed_vdo_config
pack_vdo_config(struct vdo_config config
)
1098 return (struct packed_vdo_config
) {
1099 .logical_blocks
= __cpu_to_le64(config
.logical_blocks
),
1100 .physical_blocks
= __cpu_to_le64(config
.physical_blocks
),
1101 .slab_size
= __cpu_to_le64(config
.slab_size
),
1102 .recovery_journal_size
= __cpu_to_le64(config
.recovery_journal_size
),
1103 .slab_journal_blocks
= __cpu_to_le64(config
.slab_journal_blocks
),
1108 * pack_vdo_component() - Convert a vdo_component to its packed on-disk representation.
1109 * @component: The VDO component data to convert.
1111 * Return: The platform-independent representation of the component.
1113 static struct packed_vdo_component_41_0
pack_vdo_component(const struct vdo_component component
)
1115 return (struct packed_vdo_component_41_0
) {
1116 .state
= __cpu_to_le32(component
.state
),
1117 .complete_recoveries
= __cpu_to_le64(component
.complete_recoveries
),
1118 .read_only_recoveries
= __cpu_to_le64(component
.read_only_recoveries
),
1119 .config
= pack_vdo_config(component
.config
),
1120 .nonce
= __cpu_to_le64(component
.nonce
),
1124 static void encode_vdo_component(u8
*buffer
, size_t *offset
,
1125 struct vdo_component component
)
1127 struct packed_vdo_component_41_0 packed
;
1129 encode_version_number(buffer
, offset
, VDO_COMPONENT_DATA_41_0
);
1130 packed
= pack_vdo_component(component
);
1131 memcpy(buffer
+ *offset
, &packed
, sizeof(packed
));
1132 *offset
+= sizeof(packed
);
1136 * unpack_vdo_config() - Convert a packed_vdo_config to its native in-memory representation.
1137 * @config: The packed vdo config to convert.
1139 * Return: The native in-memory representation of the vdo config.
1141 static struct vdo_config
unpack_vdo_config(struct packed_vdo_config config
)
1143 return (struct vdo_config
) {
1144 .logical_blocks
= __le64_to_cpu(config
.logical_blocks
),
1145 .physical_blocks
= __le64_to_cpu(config
.physical_blocks
),
1146 .slab_size
= __le64_to_cpu(config
.slab_size
),
1147 .recovery_journal_size
= __le64_to_cpu(config
.recovery_journal_size
),
1148 .slab_journal_blocks
= __le64_to_cpu(config
.slab_journal_blocks
),
1153 * unpack_vdo_component_41_0() - Convert a packed_vdo_component_41_0 to its native in-memory
1155 * @component: The packed vdo component data to convert.
1157 * Return: The native in-memory representation of the component.
1159 static struct vdo_component
unpack_vdo_component_41_0(struct packed_vdo_component_41_0 component
)
1161 return (struct vdo_component
) {
1162 .state
= __le32_to_cpu(component
.state
),
1163 .complete_recoveries
= __le64_to_cpu(component
.complete_recoveries
),
1164 .read_only_recoveries
= __le64_to_cpu(component
.read_only_recoveries
),
1165 .config
= unpack_vdo_config(component
.config
),
1166 .nonce
= __le64_to_cpu(component
.nonce
),
1171 * decode_vdo_component() - Decode the component data for the vdo itself out of the super block.
1173 * Return: VDO_SUCCESS or an error.
1175 static int decode_vdo_component(u8
*buffer
, size_t *offset
, struct vdo_component
*component
)
1177 struct version_number version
;
1178 struct packed_vdo_component_41_0 packed
;
1181 decode_version_number(buffer
, offset
, &version
);
1182 result
= validate_version(version
, VDO_COMPONENT_DATA_41_0
,
1183 "VDO component data");
1184 if (result
!= VDO_SUCCESS
)
1187 memcpy(&packed
, buffer
+ *offset
, sizeof(packed
));
1188 *offset
+= sizeof(packed
);
1189 *component
= unpack_vdo_component_41_0(packed
);
1194 * vdo_validate_config() - Validate constraints on a VDO config.
1195 * @config: The VDO config.
1196 * @physical_block_count: The minimum block count of the underlying storage.
1197 * @logical_block_count: The expected logical size of the VDO, or 0 if the logical size may be
1200 * Return: A success or error code.
1202 int vdo_validate_config(const struct vdo_config
*config
,
1203 block_count_t physical_block_count
,
1204 block_count_t logical_block_count
)
1206 struct slab_config slab_config
;
1209 result
= VDO_ASSERT(config
->slab_size
> 0, "slab size unspecified");
1210 if (result
!= VDO_SUCCESS
)
1213 result
= VDO_ASSERT(is_power_of_2(config
->slab_size
),
1214 "slab size must be a power of two");
1215 if (result
!= VDO_SUCCESS
)
1218 result
= VDO_ASSERT(config
->slab_size
<= (1 << MAX_VDO_SLAB_BITS
),
1219 "slab size must be less than or equal to 2^%d",
1221 if (result
!= VDO_SUCCESS
)
1224 result
= VDO_ASSERT(config
->slab_journal_blocks
>= MINIMUM_VDO_SLAB_JOURNAL_BLOCKS
,
1225 "slab journal size meets minimum size");
1226 if (result
!= VDO_SUCCESS
)
1229 result
= VDO_ASSERT(config
->slab_journal_blocks
<= config
->slab_size
,
1230 "slab journal size is within expected bound");
1231 if (result
!= VDO_SUCCESS
)
1234 result
= vdo_configure_slab(config
->slab_size
, config
->slab_journal_blocks
,
1236 if (result
!= VDO_SUCCESS
)
1239 result
= VDO_ASSERT((slab_config
.data_blocks
>= 1),
1240 "slab must be able to hold at least one block");
1241 if (result
!= VDO_SUCCESS
)
1244 result
= VDO_ASSERT(config
->physical_blocks
> 0, "physical blocks unspecified");
1245 if (result
!= VDO_SUCCESS
)
1248 result
= VDO_ASSERT(config
->physical_blocks
<= MAXIMUM_VDO_PHYSICAL_BLOCKS
,
1249 "physical block count %llu exceeds maximum %llu",
1250 (unsigned long long) config
->physical_blocks
,
1251 (unsigned long long) MAXIMUM_VDO_PHYSICAL_BLOCKS
);
1252 if (result
!= VDO_SUCCESS
)
1253 return VDO_OUT_OF_RANGE
;
1255 if (physical_block_count
!= config
->physical_blocks
) {
1256 vdo_log_error("A physical size of %llu blocks was specified, not the %llu blocks configured in the vdo super block",
1257 (unsigned long long) physical_block_count
,
1258 (unsigned long long) config
->physical_blocks
);
1259 return VDO_PARAMETER_MISMATCH
;
1262 if (logical_block_count
> 0) {
1263 result
= VDO_ASSERT((config
->logical_blocks
> 0),
1264 "logical blocks unspecified");
1265 if (result
!= VDO_SUCCESS
)
1268 if (logical_block_count
!= config
->logical_blocks
) {
1269 vdo_log_error("A logical size of %llu blocks was specified, but that differs from the %llu blocks configured in the vdo super block",
1270 (unsigned long long) logical_block_count
,
1271 (unsigned long long) config
->logical_blocks
);
1272 return VDO_PARAMETER_MISMATCH
;
1276 result
= VDO_ASSERT(config
->logical_blocks
<= MAXIMUM_VDO_LOGICAL_BLOCKS
,
1277 "logical blocks too large");
1278 if (result
!= VDO_SUCCESS
)
1281 result
= VDO_ASSERT(config
->recovery_journal_size
> 0,
1282 "recovery journal size unspecified");
1283 if (result
!= VDO_SUCCESS
)
1286 result
= VDO_ASSERT(is_power_of_2(config
->recovery_journal_size
),
1287 "recovery journal size must be a power of two");
1288 if (result
!= VDO_SUCCESS
)
1295 * vdo_destroy_component_states() - Clean up any allocations in a vdo_component_states.
1296 * @states: The component states to destroy.
1298 void vdo_destroy_component_states(struct vdo_component_states
*states
)
1303 vdo_uninitialize_layout(&states
->layout
);
1307 * decode_components() - Decode the components now that we know the component data is a version we
1309 * @buffer: The buffer being decoded.
1310 * @offset: The offset to start decoding from.
1311 * @geometry: The vdo geometry
1312 * @states: An object to hold the successfully decoded state.
1314 * Return: VDO_SUCCESS or an error.
1316 static int __must_check
decode_components(u8
*buffer
, size_t *offset
,
1317 struct volume_geometry
*geometry
,
1318 struct vdo_component_states
*states
)
1322 decode_vdo_component(buffer
, offset
, &states
->vdo
);
1324 result
= decode_layout(buffer
, offset
, vdo_get_data_region_start(*geometry
) + 1,
1325 states
->vdo
.config
.physical_blocks
, &states
->layout
);
1326 if (result
!= VDO_SUCCESS
)
1329 result
= decode_recovery_journal_state_7_0(buffer
, offset
,
1330 &states
->recovery_journal
);
1331 if (result
!= VDO_SUCCESS
)
1334 result
= decode_slab_depot_state_2_0(buffer
, offset
, &states
->slab_depot
);
1335 if (result
!= VDO_SUCCESS
)
1338 result
= decode_block_map_state_2_0(buffer
, offset
, &states
->block_map
);
1339 if (result
!= VDO_SUCCESS
)
1342 VDO_ASSERT_LOG_ONLY(*offset
== VDO_COMPONENT_DATA_OFFSET
+ VDO_COMPONENT_DATA_SIZE
,
1343 "All decoded component data was used");
1348 * vdo_decode_component_states() - Decode the payload of a super block.
1349 * @buffer: The buffer containing the encoded super block contents.
1350 * @geometry: The vdo geometry
1351 * @states: A pointer to hold the decoded states.
1353 * Return: VDO_SUCCESS or an error.
1355 int vdo_decode_component_states(u8
*buffer
, struct volume_geometry
*geometry
,
1356 struct vdo_component_states
*states
)
1359 size_t offset
= VDO_COMPONENT_DATA_OFFSET
;
1361 /* This is for backwards compatibility. */
1362 decode_u32_le(buffer
, &offset
, &states
->unused
);
1364 /* Check the VDO volume version */
1365 decode_version_number(buffer
, &offset
, &states
->volume_version
);
1366 result
= validate_version(VDO_VOLUME_VERSION_67_0
, states
->volume_version
,
1368 if (result
!= VDO_SUCCESS
)
1371 result
= decode_components(buffer
, &offset
, geometry
, states
);
1372 if (result
!= VDO_SUCCESS
)
1373 vdo_uninitialize_layout(&states
->layout
);
1379 * vdo_validate_component_states() - Validate the decoded super block configuration.
1380 * @states: The state decoded from the super block.
1381 * @geometry_nonce: The nonce from the geometry block.
1382 * @physical_size: The minimum block count of the underlying storage.
1383 * @logical_size: The expected logical size of the VDO, or 0 if the logical size may be
1386 * Return: VDO_SUCCESS or an error if the configuration is invalid.
1388 int vdo_validate_component_states(struct vdo_component_states
*states
,
1389 nonce_t geometry_nonce
, block_count_t physical_size
,
1390 block_count_t logical_size
)
1392 if (geometry_nonce
!= states
->vdo
.nonce
) {
1393 return vdo_log_error_strerror(VDO_BAD_NONCE
,
1394 "Geometry nonce %llu does not match superblock nonce %llu",
1395 (unsigned long long) geometry_nonce
,
1396 (unsigned long long) states
->vdo
.nonce
);
1399 return vdo_validate_config(&states
->vdo
.config
, physical_size
, logical_size
);
1403 * vdo_encode_component_states() - Encode the state of all vdo components in the super block.
1405 static void vdo_encode_component_states(u8
*buffer
, size_t *offset
,
1406 const struct vdo_component_states
*states
)
1408 /* This is for backwards compatibility. */
1409 encode_u32_le(buffer
, offset
, states
->unused
);
1410 encode_version_number(buffer
, offset
, states
->volume_version
);
1411 encode_vdo_component(buffer
, offset
, states
->vdo
);
1412 encode_layout(buffer
, offset
, &states
->layout
);
1413 encode_recovery_journal_state_7_0(buffer
, offset
, states
->recovery_journal
);
1414 encode_slab_depot_state_2_0(buffer
, offset
, states
->slab_depot
);
1415 encode_block_map_state_2_0(buffer
, offset
, states
->block_map
);
1417 VDO_ASSERT_LOG_ONLY(*offset
== VDO_COMPONENT_DATA_OFFSET
+ VDO_COMPONENT_DATA_SIZE
,
1418 "All super block component data was encoded");
1422 * vdo_encode_super_block() - Encode a super block into its on-disk representation.
1424 void vdo_encode_super_block(u8
*buffer
, struct vdo_component_states
*states
)
1427 struct header header
= SUPER_BLOCK_HEADER_12_0
;
1430 header
.size
+= VDO_COMPONENT_DATA_SIZE
;
1431 vdo_encode_header(buffer
, &offset
, &header
);
1432 vdo_encode_component_states(buffer
, &offset
, states
);
1434 checksum
= vdo_crc32(buffer
, offset
);
1435 encode_u32_le(buffer
, &offset
, checksum
);
1438 * Even though the buffer is a full block, to avoid the potential corruption from a torn
1439 * write, the entire encoding must fit in the first sector.
1441 VDO_ASSERT_LOG_ONLY(offset
<= VDO_SECTOR_SIZE
,
1442 "entire superblock must fit in one sector");
1446 * vdo_decode_super_block() - Decode a super block from its on-disk representation.
1448 int vdo_decode_super_block(u8
*buffer
)
1450 struct header header
;
1452 u32 checksum
, saved_checksum
;
1455 /* Decode and validate the header. */
1456 vdo_decode_header(buffer
, &offset
, &header
);
1457 result
= vdo_validate_header(&SUPER_BLOCK_HEADER_12_0
, &header
, false, __func__
);
1458 if (result
!= VDO_SUCCESS
)
1461 if (header
.size
> VDO_COMPONENT_DATA_SIZE
+ sizeof(u32
)) {
1463 * We can't check release version or checksum until we know the content size, so we
1464 * have to assume a version mismatch on unexpected values.
1466 return vdo_log_error_strerror(VDO_UNSUPPORTED_VERSION
,
1467 "super block contents too large: %zu",
1471 /* Skip past the component data for now, to verify the checksum. */
1472 offset
+= VDO_COMPONENT_DATA_SIZE
;
1474 checksum
= vdo_crc32(buffer
, offset
);
1475 decode_u32_le(buffer
, &offset
, &saved_checksum
);
1477 result
= VDO_ASSERT(offset
== VDO_SUPER_BLOCK_FIXED_SIZE
+ VDO_COMPONENT_DATA_SIZE
,
1478 "must have decoded entire superblock payload");
1479 if (result
!= VDO_SUCCESS
)
1482 return ((checksum
!= saved_checksum
) ? VDO_CHECKSUM_MISMATCH
: VDO_SUCCESS
);