Commit 6afc7bca authored by Matthew Sakai's avatar Matthew Sakai Committed by Mike Snitzer

dm vdo: implement the open chapter and chapter indexes

Deduplication records are stored in groups called chapters. New records are
collected in a structure called the open chapter, which is optimized for
adding, removing, and sorting records.

When a chapter fills, it is packed into a read-only structure called a
closed chapter, which is optimized for searching and reading. The closed
chapter includes a delta index, called the chapter index, which maps each
record name to the record page containing the record and allows the index
to read at most one record page when looking up a record.
Co-developed-by: default avatarJ. corwin Coburn <corwin@hurlbutnet.net>
Signed-off-by: default avatarJ. corwin Coburn <corwin@hurlbutnet.net>
Co-developed-by: default avatarMichael Sclafani <dm-devel@lists.linux.dev>
Signed-off-by: default avatarMichael Sclafani <dm-devel@lists.linux.dev>
Co-developed-by: default avatarThomas Jaskiewicz <tom@jaskiewicz.us>
Signed-off-by: default avatarThomas Jaskiewicz <tom@jaskiewicz.us>
Signed-off-by: default avatarMatthew Sakai <msakai@redhat.com>
Signed-off-by: default avatarMike Snitzer <snitzer@kernel.org>
parent a4eb7e25
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright 2023 Red Hat
*/
#include "chapter-index.h"
#include "errors.h"
#include "hash-utils.h"
#include "logger.h"
#include "memory-alloc.h"
#include "permassert.h"
#include "uds.h"
int uds_make_open_chapter_index(struct open_chapter_index **chapter_index,
const struct geometry *geometry, u64 volume_nonce)
{
int result;
size_t memory_size;
struct open_chapter_index *index;
result = uds_allocate(1, struct open_chapter_index, "open chapter index",
&index);
if (result != UDS_SUCCESS)
return result;
/*
* The delta index will rebalance delta lists when memory gets tight,
* so give the chapter index one extra page.
*/
memory_size = ((geometry->index_pages_per_chapter + 1) * geometry->bytes_per_page);
index->geometry = geometry;
index->volume_nonce = volume_nonce;
result = uds_initialize_delta_index(&index->delta_index, 1,
geometry->delta_lists_per_chapter,
geometry->chapter_mean_delta,
geometry->chapter_payload_bits,
memory_size, 'm');
if (result != UDS_SUCCESS) {
uds_free(index);
return result;
}
index->memory_size = index->delta_index.memory_size + sizeof(struct open_chapter_index);
*chapter_index = index;
return UDS_SUCCESS;
}
void uds_free_open_chapter_index(struct open_chapter_index *chapter_index)
{
if (chapter_index == NULL)
return;
uds_uninitialize_delta_index(&chapter_index->delta_index);
uds_free(chapter_index);
}
/* Re-initialize an open chapter index for a new chapter. */
void uds_empty_open_chapter_index(struct open_chapter_index *chapter_index,
u64 virtual_chapter_number)
{
uds_reset_delta_index(&chapter_index->delta_index);
chapter_index->virtual_chapter_number = virtual_chapter_number;
}
static inline bool was_entry_found(const struct delta_index_entry *entry, u32 address)
{
return (!entry->at_end) && (entry->key == address);
}
/* Associate a record name with the record page containing its metadata. */
int uds_put_open_chapter_index_record(struct open_chapter_index *chapter_index,
const struct uds_record_name *name,
u32 page_number)
{
int result;
struct delta_index_entry entry;
u32 address;
u32 list_number;
const u8 *found_name;
bool found;
const struct geometry *geometry = chapter_index->geometry;
u64 chapter_number = chapter_index->virtual_chapter_number;
u32 record_pages = geometry->record_pages_per_chapter;
result = ASSERT(page_number < record_pages,
"Page number within chapter (%u) exceeds the maximum value %u",
page_number, record_pages);
if (result != UDS_SUCCESS)
return UDS_INVALID_ARGUMENT;
address = uds_hash_to_chapter_delta_address(name, geometry);
list_number = uds_hash_to_chapter_delta_list(name, geometry);
result = uds_get_delta_index_entry(&chapter_index->delta_index, list_number,
address, name->name, &entry);
if (result != UDS_SUCCESS)
return result;
found = was_entry_found(&entry, address);
result = ASSERT(!(found && entry.is_collision),
"Chunk appears more than once in chapter %llu",
(unsigned long long) chapter_number);
if (result != UDS_SUCCESS)
return UDS_BAD_STATE;
found_name = (found ? name->name : NULL);
return uds_put_delta_index_entry(&entry, address, page_number, found_name);
}
/*
* Pack a section of an open chapter index into a chapter index page. A range of delta lists
* (starting with a specified list index) is copied from the open chapter index into a memory page.
* The number of lists copied onto the page is returned to the caller on success.
*
* @chapter_index: The open chapter index
* @memory: The memory page to use
* @first_list: The first delta list number to be copied
* @last_page: If true, this is the last page of the chapter index and all the remaining lists must
* be packed onto this page
* @lists_packed: The number of delta lists that were packed onto this page
*/
int uds_pack_open_chapter_index_page(struct open_chapter_index *chapter_index,
u8 *memory, u32 first_list, bool last_page,
u32 *lists_packed)
{
int result;
struct delta_index *delta_index = &chapter_index->delta_index;
struct delta_index_stats stats;
u64 nonce = chapter_index->volume_nonce;
u64 chapter_number = chapter_index->virtual_chapter_number;
const struct geometry *geometry = chapter_index->geometry;
u32 list_count = geometry->delta_lists_per_chapter;
unsigned int removals = 0;
struct delta_index_entry entry;
u32 next_list;
s32 list_number;
for (;;) {
result = uds_pack_delta_index_page(delta_index, nonce, memory,
geometry->bytes_per_page,
chapter_number, first_list,
lists_packed);
if (result != UDS_SUCCESS)
return result;
if ((first_list + *lists_packed) == list_count) {
/* All lists are packed. */
break;
} else if (*lists_packed == 0) {
/*
* The next delta list does not fit on a page. This delta list will be
* removed.
*/
} else if (last_page) {
/*
* This is the last page and there are lists left unpacked, but all of the
* remaining lists must fit on the page. Find a list that contains entries
* and remove the entire list. Try the first list that does not fit. If it
* is empty, we will select the last list that already fits and has any
* entries.
*/
} else {
/* This page is done. */
break;
}
if (removals == 0) {
uds_get_delta_index_stats(delta_index, &stats);
uds_log_warning("The chapter index for chapter %llu contains %llu entries with %llu collisions",
(unsigned long long) chapter_number,
(unsigned long long) stats.record_count,
(unsigned long long) stats.collision_count);
}
list_number = *lists_packed;
do {
if (list_number < 0)
return UDS_OVERFLOW;
next_list = first_list + list_number--,
result = uds_start_delta_index_search(delta_index, next_list, 0,
&entry);
if (result != UDS_SUCCESS)
return result;
result = uds_next_delta_index_entry(&entry);
if (result != UDS_SUCCESS)
return result;
} while (entry.at_end);
do {
result = uds_remove_delta_index_entry(&entry);
if (result != UDS_SUCCESS)
return result;
removals++;
} while (!entry.at_end);
}
if (removals > 0)
uds_log_warning("To avoid chapter index page overflow in chapter %llu, %u entries were removed from the chapter index",
(unsigned long long) chapter_number, removals);
return UDS_SUCCESS;
}
/* Make a new chapter index page, initializing it with the data from a given index_page buffer. */
int uds_initialize_chapter_index_page(struct delta_index_page *index_page,
const struct geometry *geometry, u8 *page_buffer,
u64 volume_nonce)
{
return uds_initialize_delta_index_page(index_page, volume_nonce,
geometry->chapter_mean_delta,
geometry->chapter_payload_bits,
page_buffer, geometry->bytes_per_page);
}
/* Validate a chapter index page read during rebuild. */
int uds_validate_chapter_index_page(const struct delta_index_page *index_page,
const struct geometry *geometry)
{
int result;
const struct delta_index *delta_index = &index_page->delta_index;
u32 first = index_page->lowest_list_number;
u32 last = index_page->highest_list_number;
u32 list_number;
/* We walk every delta list from start to finish. */
for (list_number = first; list_number <= last; list_number++) {
struct delta_index_entry entry;
result = uds_start_delta_index_search(delta_index, list_number - first,
0, &entry);
if (result != UDS_SUCCESS)
return result;
for (;;) {
result = uds_next_delta_index_entry(&entry);
if (result != UDS_SUCCESS) {
/*
* A random bit stream is highly likely to arrive here when we go
* past the end of the delta list.
*/
return result;
}
if (entry.at_end)
break;
/* Also make sure that the record page field contains a plausible value. */
if (uds_get_delta_entry_value(&entry) >=
geometry->record_pages_per_chapter)
/*
* Do not log this as an error. It happens in normal operation when
* we are doing a rebuild but haven't written the entire volume
* once.
*/
return UDS_CORRUPT_DATA;
}
}
return UDS_SUCCESS;
}
/*
* Search a chapter index page for a record name, returning the record page number that may contain
* the name.
*/
int uds_search_chapter_index_page(struct delta_index_page *index_page,
const struct geometry *geometry,
const struct uds_record_name *name,
u16 *record_page_ptr)
{
int result;
struct delta_index *delta_index = &index_page->delta_index;
u32 address = uds_hash_to_chapter_delta_address(name, geometry);
u32 delta_list_number = uds_hash_to_chapter_delta_list(name, geometry);
u32 sub_list_number = delta_list_number - index_page->lowest_list_number;
struct delta_index_entry entry;
result = uds_get_delta_index_entry(delta_index, sub_list_number, address,
name->name, &entry);
if (result != UDS_SUCCESS)
return result;
if (was_entry_found(&entry, address))
*record_page_ptr = uds_get_delta_entry_value(&entry);
else
*record_page_ptr = NO_CHAPTER_INDEX_ENTRY;
return UDS_SUCCESS;
}
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright 2023 Red Hat
*/
#ifndef UDS_CHAPTER_INDEX_H
#define UDS_CHAPTER_INDEX_H
#include <linux/limits.h>
#include "delta-index.h"
#include "geometry.h"
/*
* A chapter index for an open chapter is a mutable structure that tracks all the records that have
* been added to the chapter. A chapter index for a closed chapter is similar except that it is
* immutable because the contents of a closed chapter can never change, and the immutable structure
* is more efficient. Both types of chapter index are implemented with a delta index.
*/
enum {
/* The value returned when no entry is found in the chapter index. */
NO_CHAPTER_INDEX_ENTRY = U16_MAX,
};
struct open_chapter_index {
const struct geometry *geometry;
struct delta_index delta_index;
u64 virtual_chapter_number;
u64 volume_nonce;
size_t memory_size;
};
int __must_check uds_make_open_chapter_index(struct open_chapter_index **chapter_index,
const struct geometry *geometry,
u64 volume_nonce);
void uds_free_open_chapter_index(struct open_chapter_index *chapter_index);
void uds_empty_open_chapter_index(struct open_chapter_index *chapter_index,
u64 virtual_chapter_number);
int __must_check uds_put_open_chapter_index_record(struct open_chapter_index *chapter_index,
const struct uds_record_name *name,
u32 page_number);
int __must_check uds_pack_open_chapter_index_page(struct open_chapter_index *chapter_index,
u8 *memory, u32 first_list,
bool last_page, u32 *lists_packed);
int __must_check uds_initialize_chapter_index_page(struct delta_index_page *index_page,
const struct geometry *geometry,
u8 *page_buffer, u64 volume_nonce);
int __must_check uds_validate_chapter_index_page(const struct delta_index_page *index_page,
const struct geometry *geometry);
int __must_check uds_search_chapter_index_page(struct delta_index_page *index_page,
const struct geometry *geometry,
const struct uds_record_name *name,
u16 *record_page_ptr);
#endif /* UDS_CHAPTER_INDEX_H */
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright 2023 Red Hat
*/
#ifndef UDS_OPEN_CHAPTER_H
#define UDS_OPEN_CHAPTER_H
#include "chapter-index.h"
#include "geometry.h"
#include "index.h"
#include "volume.h"
/*
* The open chapter tracks the newest records in memory. Like the index as a whole, each open
* chapter is divided into a number of independent zones which are interleaved when the chapter is
* committed to the volume.
*/
enum {
OPEN_CHAPTER_RECORD_NUMBER_BITS = 23,
};
struct open_chapter_zone_slot {
/* If non-zero, the record number addressed by this hash slot */
unsigned int record_number : OPEN_CHAPTER_RECORD_NUMBER_BITS;
/* If true, the record at the index of this hash slot was deleted */
bool deleted : 1;
} __packed;
struct open_chapter_zone {
/* The maximum number of records that can be stored */
unsigned int capacity;
/* The number of records stored */
unsigned int size;
/* The number of deleted records */
unsigned int deletions;
/* Array of chunk records, 1-based */
struct uds_volume_record *records;
/* The number of slots in the hash table */
unsigned int slot_count;
/* The hash table slots, referencing virtual record numbers */
struct open_chapter_zone_slot slots[];
};
int __must_check uds_make_open_chapter(const struct geometry *geometry,
unsigned int zone_count,
struct open_chapter_zone **open_chapter_ptr);
void uds_reset_open_chapter(struct open_chapter_zone *open_chapter);
void uds_search_open_chapter(struct open_chapter_zone *open_chapter,
const struct uds_record_name *name,
struct uds_record_data *metadata, bool *found);
int __must_check uds_put_open_chapter(struct open_chapter_zone *open_chapter,
const struct uds_record_name *name,
const struct uds_record_data *metadata);
void uds_remove_from_open_chapter(struct open_chapter_zone *open_chapter,
const struct uds_record_name *name);
void uds_free_open_chapter(struct open_chapter_zone *open_chapter);
int __must_check uds_close_open_chapter(struct open_chapter_zone **chapter_zones,
unsigned int zone_count, struct volume *volume,
struct open_chapter_index *chapter_index,
struct uds_volume_record *collated_records,
u64 virtual_chapter_number);
int __must_check uds_save_open_chapter(struct uds_index *index,
struct buffered_writer *writer);
int __must_check uds_load_open_chapter(struct uds_index *index,
struct buffered_reader *reader);
u64 uds_compute_saved_open_chapter_size(struct geometry *geometry);
#endif /* UDS_OPEN_CHAPTER_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment