dm vdo: add the dm-vdo deduplication and compression DM target
Subsystems (4)
Matthew Sakai
maintainerPATCH v5 33/40 dm vdo: add the primary vdo structure
In reply to Matthew SakaiAdd the data and methods that manage the dm-vdo target itself. This includes the overall state of the target and its threads, the state of the logical volumes, startup, shutdown, and statistics.
--- drivers/md/dm-vdo/vdo.c | 1784 +++++++++++++++++++++++++++++++++++++++ drivers/md/dm-vdo/vdo.h | 376 +++++++++ 2 files changed, 2160 insertions(+) create mode 100644 drivers/md/dm-vdo/vdo.c create mode 100644 drivers/md/dm-vdo/vdo.h
drivers/md/dm-vdo/vdo.c new file +1784 -0
@@ -0,0 +1,1784 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +/* +* This file contains the main entry points for normal operations on a vdo as well as functions for +* constructing and destroying vdo instances (in memory). +*/ + +/** +* DOC: +* +* A read_only_notifier has a single completion which is used to perform read-only notifications, +* however, vdo_enter_read_only_mode() may be called from any thread. A pair of fields, protected +* by a spinlock, are used to control the read-only mode entry process. The first field holds the +* read-only error. The second is the state field, which may hold any of the four special values +* enumerated here. +* +* When vdo_enter_read_only_mode() is called from some vdo thread, if the read_only_error field +* already contains an error (i.e. its value is not VDO_SUCCESS), then some other error has already +* initiated the read-only process, and nothing more is done. Otherwise, the new error is stored in +* the read_only_error field, and the state field is consulted. If the state is MAY_NOTIFY, it is +* set to NOTIFYING, and the notification process begins. If the state is MAY_NOT_NOTIFY, then +* notifications are currently disallowed, generally due to the vdo being suspended. In this case, +* the nothing more will be done until the vdo is resumed, at which point the notification will be +* performed. In any other case, the vdo is already read-only, and there is nothing more to do. +*/ + +#include "vdo.h" + +#include <linux/completion.h> +#include <linux/device-mapper.h> +#include <linux/kernel.h> +#include <linux/lz4.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/spinlock.h> +#include <linux/types.h> + +#include "logger.h" +#include "memory-alloc.h" +#include "permassert.h" +#include "string-utils.h" + +#include "block-map.h" +#include "completion.h" +#include "data-vio.h" +#include "dedupe.h" +#include "encodings.h" +#include "funnel-workqueue.h" +#include "io-submitter.h" +#include "logical-zone.h" +#include "packer.h" +#include "physical-zone.h" +#include "pool-sysfs.h" +#include "recovery-journal.h" +#include "slab-depot.h" +#include "statistics.h" +#include "status-codes.h" +#include "vio.h" + +enum {PARANOID_THREAD_CONSISTENCY_CHECKS =0 }; + +struct sync_completion { +struct vdo_completion vdo_completion; +struct completion completion; +}; + +/* +* We don't expect this set to ever get really large, so a linked list is adequate. We can use a +* pointer_map if we need to later. +*/ +struct device_registry { +struct list_head links; +/* TODO: Convert to rcu per kernel recommendation. */ +rwlock_t lock; +}; + +static struct device_registry registry; + +/** +* vdo_initialize_device_registry_once() - Initialize the necessary structures for the device +* registry. +*/ +void vdo_initialize_device_registry_once (void ) +{ +INIT_LIST_HEAD (®istry.links); +rwlock_init (®istry.lock); +} + +/** vdo_is_equal() - Implements vdo_filter_fn. */ +static bool vdo_is_equal (struct vdo *vdo,const void *context) +{ +return (vdo == context); +} + +/** +* filter_vdos_locked() - Find a vdo in the registry if it exists there. +* @filter: The filter function to apply to devices. +* @context: A bit of context to provide the filter. +* +* Context: Must be called holding the lock. +* +* Return: the vdo object found, if any. +*/ +static struct vdo *__must_check filter_vdos_locked (vdo_filter_fn filter, +const void *context) +{ +struct vdo *vdo; + +list_for_each_entry (vdo, ®istry.links, registration) { +if (filter (vdo, context)) +return vdo; + } + +return NULL ; +} + +/** +* vdo_find_matching() - Find and return the first (if any) vdo matching a given filter function. +* @filter: The filter function to apply to vdos. +* @context: A bit of context to provide the filter. +*/ +struct vdo *vdo_find_matching (vdo_filter_fn filter,const void *context) +{ +struct vdo *vdo; + +read_lock (®istry.lock); + vdo =filter_vdos_locked (filter, context); +read_unlock (®istry.lock); + +return vdo; +} + +static void start_vdo_request_queue (void *ptr) +{ +struct vdo_thread *thread =vdo_get_work_queue_owner (vdo_get_current_work_queue ()); + +uds_register_allocating_thread (&thread->allocating_thread, + &thread->vdo->allocations_allowed); +} + +static void finish_vdo_request_queue (void *ptr) +{ +uds_unregister_allocating_thread (); +} + +#ifdef MODULE +#define MODULE_NAME THIS_MODULE->name +#else +#define MODULE_NAME "dm-vdo" +#endif /* MODULE */ + +static const struct vdo_work_queue_type default_queue_type = { + .start = start_vdo_request_queue, + .finish = finish_vdo_request_queue, + .max_priority =VDO_DEFAULT_Q_MAX_PRIORITY , + .default_priority =VDO_DEFAULT_Q_COMPLETION_PRIORITY , +}; + +static const struct vdo_work_queue_type bio_ack_q_type = { + .start =NULL , + .finish =NULL , + .max_priority =BIO_ACK_Q_MAX_PRIORITY , + .default_priority =BIO_ACK_Q_ACK_PRIORITY , +}; + +static const struct vdo_work_queue_type cpu_q_type = { + .start =NULL , + .finish =NULL , + .max_priority =CPU_Q_MAX_PRIORITY , + .default_priority =CPU_Q_MAX_PRIORITY , +}; + +static void uninitialize_thread_config (struct thread_config *config) +{ +uds_free (uds_forget (config->logical_threads)); +uds_free (uds_forget (config->physical_threads)); +uds_free (uds_forget (config->hash_zone_threads)); +uds_free (uds_forget (config->bio_threads)); +memset (config,0 ,sizeof (struct thread_config )); +} + +static void assign_thread_ids (struct thread_config *config, +thread_id_t thread_ids[],zone_count_t count) +{ +zone_count_t zone; + +for (zone =0 ; zone < count; zone++) + thread_ids[zone] = config->thread_count++; +} + +/** +* initialize_thread_config() - Initialize the thread mapping +* +* If the logical, physical, and hash zone counts are all 0, a single thread will be shared by all +* three plus the packer and recovery journal. Otherwise, there must be at least one of each type, +* and each will have its own thread, as will the packer and recovery journal. +* +* Return: VDO_SUCCESS or an error. +*/ +static int __must_check initialize_thread_config (struct thread_count_config counts, +struct thread_config *config) +{ +int result; +bool single = ((counts.logical_zones + counts.physical_zones + counts.hash_zones) ==0 ); + + config->bio_thread_count = counts.bio_threads; +if (single) { + config->logical_zone_count =1 ; + config->physical_zone_count =1 ; + config->hash_zone_count =1 ; + }else { + config->logical_zone_count = counts.logical_zones; + config->physical_zone_count = counts.physical_zones; + config->hash_zone_count = counts.hash_zones; + } + + result =uds_allocate (config->logical_zone_count, thread_id_t, +"logical thread array" , &config->logical_threads); +if (result !=VDO_SUCCESS ) { +uninitialize_thread_config (config); +return result; + } + + result =uds_allocate (config->physical_zone_count, thread_id_t, +"physical thread array" , &config->physical_threads); +if (result !=VDO_SUCCESS ) { +uninitialize_thread_config (config); +return result; + } + + result =uds_allocate (config->hash_zone_count, thread_id_t, +"hash thread array" , &config->hash_zone_threads); +if (result !=VDO_SUCCESS ) { +uninitialize_thread_config (config); +return result; + } + + result =uds_allocate (config->bio_thread_count, thread_id_t, +"bio thread array" , &config->bio_threads); +if (result !=VDO_SUCCESS ) { +uninitialize_thread_config (config); +return result; + } + +if (single) { + config->logical_threads[0 ] = config->thread_count; + config->physical_threads[0 ] = config->thread_count; + config->hash_zone_threads[0 ] = config->thread_count++; + }else { + config->admin_thread = config->thread_count; + config->journal_thread = config->thread_count++; + config->packer_thread = config->thread_count++; +assign_thread_ids (config, config->logical_threads, counts.logical_zones); +assign_thread_ids (config, config->physical_threads, counts.physical_zones); +assign_thread_ids (config, config->hash_zone_threads, counts.hash_zones); + } + + config->dedupe_thread = config->thread_count++; + config->bio_ack_thread = + ((counts.bio_ack_threads >0 ) ? config->thread_count++ :VDO_INVALID_THREAD_ID ); + config->cpu_thread = config->thread_count++; +assign_thread_ids (config, config->bio_threads, counts.bio_threads); +return VDO_SUCCESS ; +} + +/** +* vdo_read_geometry_block() - Synchronously read the geometry block from a vdo's underlying block +* device. +* @vdo: The vdo whose geometry is to be read. +* +* Return: VDO_SUCCESS or an error code. +*/ +static int __must_check read_geometry_block (struct vdo *vdo) +{ +struct vio *vio; +char *block; +int result; + + result =uds_allocate (VDO_BLOCK_SIZE , u8, __func__, &block); +if (result !=VDO_SUCCESS ) +return result; + + result =create_metadata_vio (vdo,VIO_TYPE_GEOMETRY ,VIO_PRIORITY_HIGH ,NULL , + block, &vio); +if (result !=VDO_SUCCESS ) { +uds_free (block); +return result; + } + +/* +* This is only safe because, having not already loaded the geometry, the vdo's geometry's +* bio_offset field is 0, so the fact that vio_reset_bio() will subtract that offset from +* the supplied pbn is not a problem. +*/ + result =vio_reset_bio (vio, block,NULL ,REQ_OP_READ , +VDO_GEOMETRY_BLOCK_LOCATION ); +if (result !=VDO_SUCCESS ) { +free_vio (uds_forget (vio)); +uds_free (block); +return result; + } + +bio_set_dev (vio->bio,vdo_get_backing_device (vdo)); +submit_bio_wait (vio->bio); + result =blk_status_to_errno (vio->bio->bi_status); +free_vio (uds_forget (vio)); +if (result !=0 ) { +uds_log_error_strerror (result,"synchronous read failed" ); +uds_free (block); +return -EIO ; + } + + result =vdo_parse_geometry_block ((u8 *) block, &vdo->geometry); +uds_free (block); +return result; +} + +static bool get_zone_thread_name (const thread_id_t thread_ids[],zone_count_t count, +thread_id_t id,const char *prefix, +char *buffer,size_t buffer_length) +{ +if (id >= thread_ids[0 ]) { +thread_id_t index = id - thread_ids[0 ]; + +if (index < count) { +snprintf (buffer, buffer_length,"%s%d" , prefix, index); +return true; + } + } + +return false; +} + +/** +* get_thread_name() - Format the name of the worker thread desired to support a given work queue. +* @thread_config: The thread configuration. +* @thread_id: The thread id. +* @buffer: Where to put the formatted name. +* @buffer_length: Size of the output buffer. +* +* The physical layer may add a prefix identifying the product; the output from this function +* should just identify the thread. +*/ +static void get_thread_name (const struct thread_config *thread_config, +thread_id_t thread_id,char *buffer,size_t buffer_length) +{ +if (thread_id == thread_config->journal_thread) { +if (thread_config->packer_thread == thread_id) { +/* +* This is the "single thread" config where one thread is used for the +* journal, packer, logical, physical, and hash zones. In that case, it is +* known as the "request queue." +*/ +snprintf (buffer, buffer_length,"reqQ" ); +return ; + } + +snprintf (buffer, buffer_length,"journalQ" ); +return ; + }else if (thread_id == thread_config->admin_thread) { +/* Theoretically this could be different from the journal thread. */ +snprintf (buffer, buffer_length,"adminQ" ); +return ; + }else if (thread_id == thread_config->packer_thread) { +snprintf (buffer, buffer_length,"packerQ" ); +return ; + }else if (thread_id == thread_config->dedupe_thread) { +snprintf (buffer, buffer_length,"dedupeQ" ); +return ; + }else if (thread_id == thread_config->bio_ack_thread) { +snprintf (buffer, buffer_length,"ackQ" ); +return ; + }else if (thread_id == thread_config->cpu_thread) { +snprintf (buffer, buffer_length,"cpuQ" ); +return ; + } + +if (get_zone_thread_name (thread_config->logical_threads, + thread_config->logical_zone_count, + thread_id,"logQ" , buffer, buffer_length)) +return ; + +if (get_zone_thread_name (thread_config->physical_threads, + thread_config->physical_zone_count, + thread_id,"physQ" , buffer, buffer_length)) +return ; + +if (get_zone_thread_name (thread_config->hash_zone_threads, + thread_config->hash_zone_count, + thread_id,"hashQ" , buffer, buffer_length)) +return ; + +if (get_zone_thread_name (thread_config->bio_threads, + thread_config->bio_thread_count, + thread_id,"bioQ" , buffer, buffer_length)) +return ; + +/* Some sort of misconfiguration? */ +snprintf (buffer, buffer_length,"reqQ%d" , thread_id); +} + +/** +* vdo_make_thread() - Construct a single vdo work_queue and its associated thread (or threads for +* round-robin queues). +* @vdo: The vdo which owns the thread. +* @thread_id: The id of the thread to create (as determined by the thread_config). +* @type: The description of the work queue for this thread. +* @queue_count: The number of actual threads/queues contained in the "thread". +* @contexts: An array of queue_count contexts, one for each individual queue; may be NULL. +* +* Each "thread" constructed by this method is represented by a unique thread id in the thread +* config, and completions can be enqueued to the queue and run on the threads comprising this +* entity. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_make_thread (struct vdo *vdo,thread_id_t thread_id, +const struct vdo_work_queue_type *type, +unsigned int queue_count,void *contexts[]) +{ +struct vdo_thread *thread = &vdo->threads[thread_id]; +char queue_name[MAX_VDO_WORK_QUEUE_NAME_LEN ]; + +if (type ==NULL ) + type = &default_queue_type; + +if (thread->queue !=NULL ) { +return ASSERT (vdo_work_queue_type_is (thread->queue, type), +"already constructed vdo thread %u is of the correct type" , + thread_id); + } + + thread->vdo = vdo; + thread->thread_id = thread_id; +get_thread_name (&vdo->thread_config, thread_id, queue_name,sizeof (queue_name)); +return vdo_make_work_queue (vdo->thread_name_prefix, queue_name, thread, + type, queue_count, contexts, &thread->queue); +} + +/** +* register_vdo() - Register a VDO; it must not already be registered. +* @vdo: The vdo to register. +* +* Return: VDO_SUCCESS or an error. +*/ +static int register_vdo (struct vdo *vdo) +{ +int result; + +write_lock (®istry.lock); + result =ASSERT (filter_vdos_locked (vdo_is_equal, vdo) ==NULL , +"VDO not already registered" ); +if (result ==VDO_SUCCESS ) { +INIT_LIST_HEAD (&vdo->registration); +list_add_tail (&vdo->registration, ®istry.links); + } +write_unlock (®istry.lock); + +return result; +} + +/** +* initialize_vdo() - Do the portion of initializing a vdo which will clean up after itself on +* error. +* @vdo: The vdo being initialized +* @config: The configuration of the vdo +* @instance: The instance number of the vdo +* @reason: The buffer to hold the failure reason on error +*/ +static int initialize_vdo (struct vdo *vdo,struct device_config *config, +unsigned int instance,char **reason) +{ +int result; +zone_count_t i; + + vdo->device_config = config; + vdo->starting_sector_offset = config->owning_target->begin; + vdo->instance = instance; + vdo->allocations_allowed = true; +vdo_set_admin_state_code (&vdo->admin.state,VDO_ADMIN_STATE_NEW ); +INIT_LIST_HEAD (&vdo->device_config_list); +vdo_initialize_completion (&vdo->admin.completion, vdo,VDO_ADMIN_COMPLETION ); +init_completion (&vdo->admin.callback_sync); +mutex_init (&vdo->stats_mutex); + result =read_geometry_block (vdo); +if (result !=VDO_SUCCESS ) { + *reason ="Could not load geometry block" ; +return result; + } + + result =initialize_thread_config (config->thread_counts, &vdo->thread_config); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot create thread configuration" ; +return result; + } + +uds_log_info ("zones: %d logical, %d physical, %d hash; total threads: %d" , + config->thread_counts.logical_zones, + config->thread_counts.physical_zones, + config->thread_counts.hash_zones, vdo->thread_config.thread_count); + +/* Compression context storage */ + result = uds_allocate(config->thread_counts.cpu_threads, char *, "LZ4 context", + &vdo->compression_context); +if (result !=VDO_SUCCESS ) { + *reason ="cannot allocate LZ4 context" ; +return result; + } + +for (i =0 ; i < config->thread_counts.cpu_threads; i++) { + result =uds_allocate (LZ4_MEM_COMPRESS , char,"LZ4 context" , + &vdo->compression_context[i]); +if (result !=VDO_SUCCESS ) { + *reason ="cannot allocate LZ4 context" ; +return result; + } + } + + result =register_vdo (vdo); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot add VDO to device registry" ; +return result; + } + +vdo_set_admin_state_code (&vdo->admin.state,VDO_ADMIN_STATE_INITIALIZED ); +return result; +} + +/** +* vdo_make() - Allocate and initialize a vdo. +* @instance: Device instantiation counter. +* @config: The device configuration. +* @reason: The reason for any failure during this call. +* @vdo_ptr: A pointer to hold the created vdo. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_make (unsigned int instance,struct device_config *config,char **reason, +struct vdo **vdo_ptr) +{ +int result; +struct vdo *vdo; + +/* VDO-3769 - Set a generic reason so we don't ever return garbage. */ + *reason ="Unspecified error" ; + + result = uds_allocate(1, struct vdo, __func__, &vdo); +if (result !=UDS_SUCCESS ) { + *reason ="Cannot allocate VDO" ; +return result; + } + + result =initialize_vdo (vdo, config, instance, reason); +if (result !=VDO_SUCCESS ) { +vdo_destroy (vdo); +return result; + } + +/* From here on, the caller will clean up if there is an error. */ + *vdo_ptr = vdo; + +snprintf (vdo->thread_name_prefix,sizeof (vdo->thread_name_prefix), +"%s%u" ,MODULE_NAME , instance); +BUG_ON (vdo->thread_name_prefix[0 ] =='\0' ); + result =uds_allocate (vdo->thread_config.thread_count, + struct vdo_thread, __func__, &vdo->threads); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot allocate thread structures" ; +return result; + } + + result =vdo_make_thread (vdo, vdo->thread_config.admin_thread, + &default_queue_type,1 ,NULL ); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot make admin thread" ; +return result; + } + + result =vdo_make_flusher (vdo); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot make flusher zones" ; +return result; + } + + result =vdo_make_packer (vdo,DEFAULT_PACKER_BINS , &vdo->packer); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot make packer zones" ; +return result; + } + +BUG_ON (vdo->device_config->logical_block_size <=0 ); +BUG_ON (vdo->device_config->owned_device ==NULL ); + result =make_data_vio_pool (vdo,MAXIMUM_VDO_USER_VIOS , +MAXIMUM_VDO_USER_VIOS *3 /4 , + &vdo->data_vio_pool); +if (result !=VDO_SUCCESS ) { + *reason ="Cannot allocate data_vio pool" ; +return result; + } + + result =vdo_make_io_submitter (config->thread_counts.bio_threads, + config->thread_counts.bio_rotation_interval, +get_data_vio_pool_request_limit (vdo->data_vio_pool), + vdo, &vdo->io_submitter); +if (result !=VDO_SUCCESS ) { + *reason ="bio submission initialization failed" ; +return result; + } + +if (vdo_uses_bio_ack_queue (vdo)) { + result =vdo_make_thread (vdo, vdo->thread_config.bio_ack_thread, + &bio_ack_q_type, + config->thread_counts.bio_ack_threads,NULL ); +if (result !=VDO_SUCCESS ) { + *reason ="bio ack queue initialization failed" ; +return result; + } + } + + result =vdo_make_thread (vdo, vdo->thread_config.cpu_thread, &cpu_q_type, + config->thread_counts.cpu_threads, + (void **) vdo->compression_context); +if (result !=VDO_SUCCESS ) { + *reason ="CPU queue initialization failed" ; +return result; + } + +return VDO_SUCCESS ; +} + +static void finish_vdo (struct vdo *vdo) +{ +int i; + +if (vdo->threads ==NULL ) +return ; + +vdo_cleanup_io_submitter (vdo->io_submitter); +vdo_finish_dedupe_index (vdo->hash_zones); + +for (i =0 ; i < vdo->thread_config.thread_count; i++) +vdo_finish_work_queue (vdo->threads[i].queue); +} + +/** +* free_listeners() - Free the list of read-only listeners associated with a thread. +* @thread_data: The thread holding the list to free. +*/ +static void free_listeners (struct vdo_thread *thread) +{ +struct read_only_listener *listener, *next; + +for (listener =uds_forget (thread->listeners); listener !=NULL ; listener = next) { + next =uds_forget (listener->next); +uds_free (listener); + } +} + +static void uninitialize_super_block (struct vdo_super_block *super_block) +{ +free_vio_components (&super_block->vio); +uds_free (super_block->buffer); +} + +/** +* unregister_vdo() - Remove a vdo from the device registry. +* @vdo: The vdo to remove. +*/ +static void unregister_vdo (struct vdo *vdo) +{ +write_lock (®istry.lock); +if (filter_vdos_locked (vdo_is_equal, vdo) == vdo) +list_del_init (&vdo->registration); + +write_unlock (®istry.lock); +} + +/** +* vdo_destroy() - Destroy a vdo instance. +* @vdo: The vdo to destroy (may be NULL). +*/ +void vdo_destroy (struct vdo *vdo) +{ +unsigned int i; + +if (vdo ==NULL ) +return ; + +/* A running VDO should never be destroyed without suspending first. */ +BUG_ON (vdo_get_admin_state (vdo)->normal); + + vdo->allocations_allowed = true; + +/* Stop services that need to gather VDO statistics from the worker threads. */ +if (vdo->sysfs_added) { +init_completion (&vdo->stats_shutdown); +kobject_put (&vdo->stats_directory); +wait_for_completion (&vdo->stats_shutdown); + } + +finish_vdo (vdo); +unregister_vdo (vdo); +free_data_vio_pool (vdo->data_vio_pool); +vdo_free_io_submitter (uds_forget (vdo->io_submitter)); +vdo_free_flusher (uds_forget (vdo->flusher)); +vdo_free_packer (uds_forget (vdo->packer)); +vdo_free_recovery_journal (uds_forget (vdo->recovery_journal)); +vdo_free_slab_depot (uds_forget (vdo->depot)); +vdo_uninitialize_layout (&vdo->layout); +vdo_uninitialize_layout (&vdo->next_layout); +if (vdo->partition_copier) +dm_kcopyd_client_destroy (uds_forget (vdo->partition_copier)); +uninitialize_super_block (&vdo->super_block); +vdo_free_block_map (uds_forget (vdo->block_map)); +vdo_free_hash_zones (uds_forget (vdo->hash_zones)); +vdo_free_physical_zones (uds_forget (vdo->physical_zones)); +vdo_free_logical_zones (uds_forget (vdo->logical_zones)); + +if (vdo->threads !=NULL ) { +for (i =0 ; i < vdo->thread_config.thread_count; i++) { +free_listeners (&vdo->threads[i]); +vdo_free_work_queue (uds_forget (vdo->threads[i].queue)); + } +uds_free (uds_forget (vdo->threads)); + } + +uninitialize_thread_config (&vdo->thread_config); + +if (vdo->compression_context !=NULL ) { +for (i =0 ; i < vdo->device_config->thread_counts.cpu_threads; i++) +uds_free (uds_forget (vdo->compression_context[i])); + +uds_free (uds_forget (vdo->compression_context)); + } + +/* +* The call to kobject_put on the kobj sysfs node will decrement its reference count; when +* the count goes to zero the VDO object will be freed as a side effect. +*/ +if (!vdo->sysfs_added) +uds_free (vdo); +else +kobject_put (&vdo->vdo_directory); +} + +static int initialize_super_block (struct vdo *vdo,struct vdo_super_block *super_block) +{ +int result; + + result =uds_allocate (VDO_BLOCK_SIZE , char,"encoded super block" , + (char **) &vdo->super_block.buffer); +if (result !=VDO_SUCCESS ) +return result; + +return allocate_vio_components (vdo,VIO_TYPE_SUPER_BLOCK , +VIO_PRIORITY_METADATA ,NULL ,1 , + (char *) super_block->buffer, + &vdo->super_block.vio); +} + +/** +* finish_reading_super_block() - Continue after loading the super block. +* @completion: The super block vio. +* +* This callback is registered in vdo_load_super_block(). +*/ +static void finish_reading_super_block (struct vdo_completion *completion) +{ +struct vdo_super_block *super_block = + container_of(as_vio(completion), struct vdo_super_block, vio); + +vdo_continue_completion (uds_forget (completion->parent), +vdo_decode_super_block (super_block->buffer)); +} + +/** +* handle_super_block_read_error() - Handle an error reading the super block. +* @completion: The super block vio. +* +* This error handler is registered in vdo_load_super_block(). +*/ +static void handle_super_block_read_error (struct vdo_completion *completion) +{ +vio_record_metadata_io_error (as_vio (completion)); +finish_reading_super_block (completion); +} + +static void read_super_block_endio (struct bio *bio) +{ +struct vio *vio = bio->bi_private; +struct vdo_completion *parent = vio->completion.parent; + +continue_vio_after_io (vio, finish_reading_super_block, + parent->callback_thread_id); +} + +/** +* vdo_load_super_block() - Allocate a super block and read its contents from storage. +* @vdo: The vdo containing the super block on disk. +* @parent: The completion to notify after loading the super block. +*/ +void vdo_load_super_block (struct vdo *vdo,struct vdo_completion *parent) +{ +int result; + + result =initialize_super_block (vdo, &vdo->super_block); +if (result !=VDO_SUCCESS ) { +vdo_continue_completion (parent, result); +return ; + } + + vdo->super_block.vio.completion.parent = parent; +submit_metadata_vio (&vdo->super_block.vio, +vdo_get_data_region_start (vdo->geometry), + read_super_block_endio, + handle_super_block_read_error, +REQ_OP_READ ); +} + +/** +* pool_stats_release() - Signal that sysfs stats have been shut down. +* @directory: The vdo stats directory. +*/ +static void pool_stats_release (struct kobject *directory) +{ + struct vdo *vdo = container_of(directory, struct vdo, stats_directory); + +complete (&vdo->stats_shutdown); +} + +ATTRIBUTE_GROUPS (vdo_pool_stats); +static const struct kobj_type stats_directory_type = { + .release = pool_stats_release, + .sysfs_ops = &vdo_pool_stats_sysfs_ops, + .default_groups = vdo_pool_stats_groups, +}; + +/** +* vdo_add_sysfs_stats_dir() - Add the stats directory to the vdo sysfs directory. +* @vdo: The vdo. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_add_sysfs_stats_dir (struct vdo *vdo) +{ +int result; + +kobject_init (&vdo->stats_directory, &stats_directory_type); + result =kobject_add (&vdo->stats_directory, &vdo->vdo_directory,"statistics" ); +if (result !=0 ) +return VDO_CANT_ADD_SYSFS_NODE ; + +return VDO_SUCCESS ; +} + +/** +* vdo_get_backing_device() - Get the block device object underlying a vdo. +* @vdo: The vdo. +* +* Return: The vdo's current block device. +*/ +struct block_device *vdo_get_backing_device (const struct vdo *vdo) +{ +return vdo->device_config->owned_device->bdev; +} + +/** +* vdo_get_device_name() - Get the device name associated with the vdo target. +* @target: The target device interface. +* +* Return: The block device name. +*/ +const char *vdo_get_device_name (const struct dm_target *target) +{ +return dm_device_name (dm_table_get_md (target->table)); +} + +/** +* vdo_synchronous_flush() - Issue a flush request and wait for it to complete. +* @vdo: The vdo. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_synchronous_flush (struct vdo *vdo) +{ +int result; +struct bio bio; + +bio_init (&bio,vdo_get_backing_device (vdo),0 ,0 ,REQ_OP_WRITE |REQ_PREFLUSH ); +submit_bio_wait (&bio); + result =blk_status_to_errno (bio.bi_status); + +atomic64_inc (&vdo->stats.flush_out); +if (result !=0 ) { +uds_log_error_strerror (result,"synchronous flush failed" ); + result = -EIO ; + } + +bio_uninit (&bio); +return result; +} + +/** +* vdo_get_state() - Get the current state of the vdo. +* @vdo: The vdo. + +* Context: This method may be called from any thread. +* +* Return: The current state of the vdo. +*/ +enum vdo_state vdo_get_state (const struct vdo *vdo) +{ +enum vdo_state state =atomic_read (&vdo->state); + +/* pairs with barriers where state field is changed */ +smp_rmb (); +return state; +} + +/** +* vdo_set_state() - Set the current state of the vdo. +* @vdo: The vdo whose state is to be set. +* @state: The new state of the vdo. +* +* Context: This method may be called from any thread. +*/ +void vdo_set_state (struct vdo *vdo,enum vdo_state state) +{ +/* pairs with barrier in vdo_get_state */ +smp_wmb (); +atomic_set (&vdo->state, state); +} + +/** +* vdo_get_admin_state() - Get the admin state of the vdo. +* @vdo: The vdo. +* +* Return: The code for the vdo's current admin state. +*/ +const struct admin_state_code *vdo_get_admin_state (const struct vdo *vdo) +{ +return vdo_get_admin_state_code (&vdo->admin.state); +} + +/** +* record_vdo() - Record the state of the VDO for encoding in the super block. +*/ +static void record_vdo (struct vdo *vdo) +{ +/* This is for backwards compatibility. */ + vdo->states.unused = vdo->geometry.unused; + vdo->states.vdo.state =vdo_get_state (vdo); + vdo->states.block_map =vdo_record_block_map (vdo->block_map); + vdo->states.recovery_journal =vdo_record_recovery_journal (vdo->recovery_journal); + vdo->states.slab_depot =vdo_record_slab_depot (vdo->depot); + vdo->states.layout = vdo->layout; +} + +/** +* continue_super_block_parent() - Continue the parent of a super block save operation. +* @completion: The super block vio. +* +* This callback is registered in vdo_save_components(). +*/ +static void continue_super_block_parent (struct vdo_completion *completion) +{ +vdo_continue_completion (uds_forget (completion->parent), completion->result); +} + +/** +* handle_save_error() - Log a super block save error. +* @completion: The super block vio. +* +* This error handler is registered in vdo_save_components(). +*/ +static void handle_save_error (struct vdo_completion *completion) +{ +struct vdo_super_block *super_block = + container_of(as_vio(completion), struct vdo_super_block, vio); + +vio_record_metadata_io_error (&super_block->vio); +uds_log_error_strerror (completion->result,"super block save failed" ); +/* +* Mark the super block as unwritable so that we won't attempt to write it again. This +* avoids the case where a growth attempt fails writing the super block with the new size, +* but the subsequent attempt to write out the read-only state succeeds. In this case, +* writes which happened just before the suspend would not be visible if the VDO is +* restarted without rebuilding, but, after a read-only rebuild, the effects of those +* writes would reappear. +*/ + super_block->unwritable = true; + completion->callback (completion); +} + +static void super_block_write_endio (struct bio *bio) +{ +struct vio *vio = bio->bi_private; +struct vdo_completion *parent = vio->completion.parent; + +continue_vio_after_io (vio, continue_super_block_parent, + parent->callback_thread_id); +} + +/** +* vdo_save_components() - Encode the vdo and save the super block asynchronously. +* @vdo: The vdo whose state is being saved. +* @parent: The completion to notify when the save is complete. +*/ +void vdo_save_components (struct vdo *vdo,struct vdo_completion *parent) +{ +struct vdo_super_block *super_block = &vdo->super_block; + +if (super_block->unwritable) { +vdo_continue_completion (parent,VDO_READ_ONLY ); +return ; + } + +if (super_block->vio.completion.parent !=NULL ) { +vdo_continue_completion (parent,VDO_COMPONENT_BUSY ); +return ; + } + +record_vdo (vdo); + +vdo_encode_super_block (super_block->buffer, &vdo->states); + super_block->vio.completion.parent = parent; + super_block->vio.completion.callback_thread_id = parent->callback_thread_id; +submit_metadata_vio (&super_block->vio, +vdo_get_data_region_start (vdo->geometry), + super_block_write_endio, handle_save_error, +REQ_OP_WRITE |REQ_PREFLUSH |REQ_FUA ); +} + +/** +* vdo_register_read_only_listener() - Register a listener to be notified when the VDO goes +* read-only. +* @vdo: The vdo to register with. +* @listener: The object to notify. +* @notification: The function to call to send the notification. +* @thread_id: The id of the thread on which to send the notification. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_register_read_only_listener (struct vdo *vdo,void *listener, +vdo_read_only_notification_fn notification, +thread_id_t thread_id) +{ +struct vdo_thread *thread = &vdo->threads[thread_id]; +struct read_only_listener *read_only_listener; +int result; + + result =ASSERT (thread_id != vdo->thread_config.dedupe_thread, +"read only listener not registered on dedupe thread" ); +if (result !=VDO_SUCCESS ) +return result; + + result = uds_allocate(1, struct read_only_listener, __func__, + &read_only_listener); +if (result !=VDO_SUCCESS ) +return result; + + *read_only_listener = (struct read_only_listener ) { + .listener = listener, + .notify = notification, + .next = thread->listeners, + }; + + thread->listeners = read_only_listener; +return VDO_SUCCESS ; +} + +/** +* notify_vdo_of_read_only_mode() - Notify a vdo that it is going read-only. +* @listener: The vdo. +* @parent: The completion to notify in order to acknowledge the notification. +* +* This will save the read-only state to the super block. +* +* Implements vdo_read_only_notification_fn. +*/ +static void notify_vdo_of_read_only_mode (void *listener,struct vdo_completion *parent) +{ +struct vdo *vdo = listener; + +if (vdo_in_read_only_mode (vdo)) +vdo_finish_completion (parent); + +vdo_set_state (vdo,VDO_READ_ONLY_MODE ); +vdo_save_components (vdo, parent); +} + +/** +* vdo_enable_read_only_entry() - Enable a vdo to enter read-only mode on errors. +* @vdo: The vdo to enable. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_enable_read_only_entry (struct vdo *vdo) +{ +thread_id_t id; +bool is_read_only =vdo_in_read_only_mode (vdo); +struct read_only_notifier *notifier = &vdo->read_only_notifier; + +if (is_read_only) { + notifier->read_only_error =VDO_READ_ONLY ; + notifier->state =NOTIFIED ; + }else { + notifier->state =MAY_NOT_NOTIFY ; + } + +spin_lock_init (¬ifier->lock); +vdo_initialize_completion (¬ifier->completion, vdo, +VDO_READ_ONLY_MODE_COMPLETION ); + +for (id =0 ; id < vdo->thread_config.thread_count; id++) + vdo->threads[id].is_read_only = is_read_only; + +return vdo_register_read_only_listener (vdo, vdo, notify_vdo_of_read_only_mode, + vdo->thread_config.admin_thread); +} + +/** +* vdo_wait_until_not_entering_read_only_mode() - Wait until no read-only notifications are in +* progress and prevent any subsequent +* notifications. +* @parent: The completion to notify when no threads are entering read-only mode. +* +* Notifications may be re-enabled by calling vdo_allow_read_only_mode_entry(). +*/ +void vdo_wait_until_not_entering_read_only_mode (struct vdo_completion *parent) +{ +struct vdo *vdo = parent->vdo; +struct read_only_notifier *notifier = &vdo->read_only_notifier; + +vdo_assert_on_admin_thread (vdo, __func__); + +if (notifier->waiter !=NULL ) { +vdo_continue_completion (parent,VDO_COMPONENT_BUSY ); +return ; + } + +spin_lock (¬ifier->lock); +if (notifier->state ==NOTIFYING ) + notifier->waiter = parent; +else if (notifier->state ==MAY_NOTIFY ) + notifier->state =MAY_NOT_NOTIFY ; +spin_unlock (¬ifier->lock); + +if (notifier->waiter ==NULL ) { +/* +* A notification was not in progress, and now they are +* disallowed. +*/ +vdo_launch_completion (parent); +return ; + } +} + +/** +* as_notifier() - Convert a generic vdo_completion to a read_only_notifier. +* @completion: The completion to convert. +* +* Return: The completion as a read_only_notifier. +*/ +static inline struct read_only_notifier *as_notifier (struct vdo_completion *completion) +{ +vdo_assert_completion_type (completion,VDO_READ_ONLY_MODE_COMPLETION ); + return container_of(completion, struct read_only_notifier, completion); +} + +/** +* finish_entering_read_only_mode() - Complete the process of entering read only mode. +* @completion: The read-only mode completion. +*/ +static void finish_entering_read_only_mode (struct vdo_completion *completion) +{ +struct read_only_notifier *notifier =as_notifier (completion); + +vdo_assert_on_admin_thread (completion->vdo, __func__); + +spin_lock (¬ifier->lock); + notifier->state =NOTIFIED ; +spin_unlock (¬ifier->lock); + +if (notifier->waiter !=NULL ) +vdo_continue_completion (uds_forget (notifier->waiter), + completion->result); +} + +/** +* make_thread_read_only() - Inform each thread that the VDO is in read-only mode. +* @completion: The read-only mode completion. +*/ +static void make_thread_read_only (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +thread_id_t thread_id = completion->callback_thread_id; +struct read_only_notifier *notifier =as_notifier (completion); +struct read_only_listener *listener = completion->parent; + +if (listener ==NULL ) { +/* This is the first call on this thread */ +struct vdo_thread *thread = &vdo->threads[thread_id]; + + thread->is_read_only = true; + listener = thread->listeners; +if (thread_id ==0 ) +uds_log_error_strerror (READ_ONCE (notifier->read_only_error), +"Unrecoverable error, entering read-only mode" ); + }else { +/* We've just finished notifying a listener */ + listener = listener->next; + } + +if (listener !=NULL ) { +/* We have a listener to notify */ +vdo_prepare_completion (completion, make_thread_read_only, + make_thread_read_only, thread_id, + listener); + listener->notify (listener->listener, completion); +return ; + } + +/* We're done with this thread */ +if (++thread_id == vdo->thread_config.dedupe_thread) { +/* +* We don't want to notify the dedupe thread since it may be +* blocked rebuilding the index. +*/ + thread_id++; + } + +if (thread_id >= vdo->thread_config.thread_count) { +/* There are no more threads */ +vdo_prepare_completion (completion, finish_entering_read_only_mode, + finish_entering_read_only_mode, + vdo->thread_config.admin_thread,NULL ); + }else { +vdo_prepare_completion (completion, make_thread_read_only, + make_thread_read_only, thread_id,NULL ); + } + +vdo_launch_completion (completion); +} + +/** +* vdo_allow_read_only_mode_entry() - Allow the notifier to put the VDO into read-only mode, +* reversing the effects of +* vdo_wait_until_not_entering_read_only_mode(). +* @parent: The object to notify once the operation is complete. +* +* If some thread tried to put the vdo into read-only mode while notifications were disallowed, it +* will be done when this method is called. If that happens, the parent will not be notified until +* the vdo has actually entered read-only mode and attempted to save the super block. +* +* Context: This method may only be called from the admin thread. +*/ +void vdo_allow_read_only_mode_entry (struct vdo_completion *parent) +{ +struct vdo *vdo = parent->vdo; +struct read_only_notifier *notifier = &vdo->read_only_notifier; + +vdo_assert_on_admin_thread (vdo, __func__); + +if (notifier->waiter !=NULL ) { +vdo_continue_completion (parent,VDO_COMPONENT_BUSY ); +return ; + } + +spin_lock (¬ifier->lock); +if (notifier->state ==MAY_NOT_NOTIFY ) { +if (notifier->read_only_error ==VDO_SUCCESS ) { + notifier->state =MAY_NOTIFY ; + }else { + notifier->state =NOTIFYING ; + notifier->waiter = parent; + } + } +spin_unlock (¬ifier->lock); + +if (notifier->waiter ==NULL ) { +/* We're done */ +vdo_launch_completion (parent); +return ; + } + +/* Do the pending notification. */ +make_thread_read_only (¬ifier->completion); +} + +/** +* vdo_enter_read_only_mode() - Put a VDO into read-only mode and save the read-only state in the +* super block. +* @vdo: The vdo. +* @error_code: The error which caused the VDO to enter read-only mode. +* +* This method is a no-op if the VDO is already read-only. +*/ +void vdo_enter_read_only_mode (struct vdo *vdo,int error_code) +{ +bool notify = false; +thread_id_t thread_id =vdo_get_callback_thread_id (); +struct read_only_notifier *notifier = &vdo->read_only_notifier; +struct vdo_thread *thread; + +if (thread_id !=VDO_INVALID_THREAD_ID ) { + thread = &vdo->threads[thread_id]; +if (thread->is_read_only) { +/* This thread has already gone read-only. */ +return ; + } + +/* Record for this thread that the VDO is read-only. */ + thread->is_read_only = true; + } + +spin_lock (¬ifier->lock); +if (notifier->read_only_error ==VDO_SUCCESS ) { +WRITE_ONCE (notifier->read_only_error, error_code); +if (notifier->state ==MAY_NOTIFY ) { + notifier->state =NOTIFYING ; + notify = true; + } + } +spin_unlock (¬ifier->lock); + +if (!notify) { +/* The notifier is already aware of a read-only error */ +return ; + } + +/* Initiate a notification starting on the lowest numbered thread. */ +vdo_launch_completion_callback (¬ifier->completion, make_thread_read_only,0 ); +} + +/** +* vdo_is_read_only() - Check whether the VDO is read-only. +* @vdo: The vdo. +* +* Return: true if the vdo is read-only. +* +* This method may be called from any thread, as opposed to examining the VDO's state field which +* is only safe to check from the admin thread. +*/ +bool vdo_is_read_only (struct vdo *vdo) +{ +return vdo->threads[vdo_get_callback_thread_id ()].is_read_only; +} + +/** +* vdo_in_read_only_mode() - Check whether a vdo is in read-only mode. +* @vdo: The vdo to query. +* +* Return: true if the vdo is in read-only mode. +*/ +bool vdo_in_read_only_mode (const struct vdo *vdo) +{ +return (vdo_get_state (vdo) ==VDO_READ_ONLY_MODE ); +} + +/** +* vdo_in_recovery_mode() - Check whether the vdo is in recovery mode. +* @vdo: The vdo to query. +* +* Return: true if the vdo is in recovery mode. +*/ +bool vdo_in_recovery_mode (const struct vdo *vdo) +{ +return (vdo_get_state (vdo) ==VDO_RECOVERING ); +} + +/** +* vdo_enter_recovery_mode() - Put the vdo into recovery mode. +* @vdo: The vdo. +*/ +void vdo_enter_recovery_mode (struct vdo *vdo) +{ +vdo_assert_on_admin_thread (vdo, __func__); + +if (vdo_in_read_only_mode (vdo)) +return ; + +uds_log_info ("Entering recovery mode" ); +vdo_set_state (vdo,VDO_RECOVERING ); +} + +/** +* complete_synchronous_action() - Signal the waiting thread that a synchronous action is complete. +* @completion: The sync completion. +*/ +static void complete_synchronous_action (struct vdo_completion *completion) +{ +vdo_assert_completion_type (completion,VDO_SYNC_COMPLETION ); + complete(&(container_of(completion, struct sync_completion, + vdo_completion)->completion)); +} + +/** +* perform_synchronous_action() - Launch an action on a VDO thread and wait for it to complete. +* @vdo: The vdo. +* @action: The callback to launch. +* @thread_id: The thread on which to run the action. +* @parent: The parent of the sync completion (may be NULL). +*/ +static int perform_synchronous_action (struct vdo *vdo,vdo_action_fn action, +thread_id_t thread_id,void *parent) +{ +struct sync_completion sync; + +vdo_initialize_completion (&sync.vdo_completion, vdo,VDO_SYNC_COMPLETION ); +init_completion (&sync.completion); + sync.vdo_completion.parent = parent; +vdo_launch_completion_callback (&sync.vdo_completion, action, thread_id); +wait_for_completion (&sync.completion); +return sync.vdo_completion.result; +} + +/** +* set_compression_callback() - Callback to turn compression on or off. +* @completion: The completion. +*/ +static void set_compression_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +bool *enable = completion->parent; +bool was_enabled =vdo_get_compressing (vdo); + +if (*enable != was_enabled) { +WRITE_ONCE (vdo->compressing, *enable); +if (was_enabled) { +/* Signal the packer to flush since compression has been disabled. */ +vdo_flush_packer (vdo->packer); + } + } + +uds_log_info ("compression is %s" , (*enable ?"enabled" :"disabled" )); + *enable = was_enabled; +complete_synchronous_action (completion); +} + +/** +* vdo_set_compressing() - Turn compression on or off. +* @vdo: The vdo. +* @enable: Whether to enable or disable compression. +* +* Return: Whether compression was previously on or off. +*/ +bool vdo_set_compressing (struct vdo *vdo,bool enable) +{ +perform_synchronous_action (vdo, set_compression_callback, + vdo->thread_config.packer_thread, + &enable); +return enable; +} + +/** +* vdo_get_compressing() - Get whether compression is enabled in a vdo. +* @vdo: The vdo. +* +* Return: State of compression. +*/ +bool vdo_get_compressing (struct vdo *vdo) +{ +return READ_ONCE (vdo->compressing); +} + +static size_t get_block_map_cache_size (const struct vdo *vdo) +{ +return ((size_t ) vdo->device_config->cache_size) *VDO_BLOCK_SIZE ; +} + +static struct error_statistics __must_check get_vdo_error_statistics (const struct vdo *vdo) +{ +/* +* The error counts can be incremented from arbitrary threads and so must be incremented +* atomically, but they are just statistics with no semantics that could rely on memory +* order, so unfenced reads are sufficient. +*/ +const struct atomic_statistics *atoms = &vdo->stats; + +return (struct error_statistics ) { + .invalid_advice_pbn_count =atomic64_read (&atoms->invalid_advice_pbn_count), + .no_space_error_count =atomic64_read (&atoms->no_space_error_count), + .read_only_error_count =atomic64_read (&atoms->read_only_error_count), + }; +} + +static void copy_bio_stat (struct bio_stats *b,const struct atomic_bio_stats *a) +{ + b->read =atomic64_read (&a->read); + b->write =atomic64_read (&a->write); + b->discard =atomic64_read (&a->discard); + b->flush =atomic64_read (&a->flush); + b->empty_flush =atomic64_read (&a->empty_flush); + b->fua =atomic64_read (&a->fua); +} + +static struct bio_stats subtract_bio_stats (struct bio_stats minuend, +struct bio_stats subtrahend) +{ +return (struct bio_stats ) { + .read = minuend.read - subtrahend.read, + .write = minuend.write - subtrahend.write, + .discard = minuend.discard - subtrahend.discard, + .flush = minuend.flush - subtrahend.flush, + .empty_flush = minuend.empty_flush - subtrahend.empty_flush, + .fua = minuend.fua - subtrahend.fua, + }; +} + +/** +* vdo_get_physical_blocks_allocated() - Get the number of physical blocks in use by user data. +* @vdo: The vdo. +* +* Return: The number of blocks allocated for user data. +*/ +static block_count_t __must_check vdo_get_physical_blocks_allocated (const struct vdo *vdo) +{ +return (vdo_get_slab_depot_allocated_blocks (vdo->depot) - +vdo_get_journal_block_map_data_blocks_used (vdo->recovery_journal)); +} + +/** +* vdo_get_physical_blocks_overhead() - Get the number of physical blocks used by vdo metadata. +* @vdo: The vdo. +* +* Return: The number of overhead blocks. +*/ +static block_count_t __must_check vdo_get_physical_blocks_overhead (const struct vdo *vdo) +{ +/* +* config.physical_blocks is mutated during resize and is in a packed structure, +* but resize runs on admin thread. +* TODO: Verify that this is always safe. +*/ +return (vdo->states.vdo.config.physical_blocks - +vdo_get_slab_depot_data_blocks (vdo->depot) + +vdo_get_journal_block_map_data_blocks_used (vdo->recovery_journal)); +} + +static const char *vdo_describe_state (enum vdo_state state) +{ +/* These strings should all fit in the 15 chars of VDOStatistics.mode. */ +switch (state) { +case VDO_RECOVERING : +return "recovering" ; + +case VDO_READ_ONLY_MODE : +return "read-only" ; + +default : +return "normal" ; + } +} + +/** +* get_vdo_statistics() - Populate a vdo_statistics structure on the admin thread. +* @vdo: The vdo. +* @stats: The statistics structure to populate. +*/ +static void get_vdo_statistics (const struct vdo *vdo,struct vdo_statistics *stats) +{ +struct recovery_journal *journal = vdo->recovery_journal; +enum vdo_state state =vdo_get_state (vdo); + +vdo_assert_on_admin_thread (vdo, __func__); + +/* start with a clean slate */ +memset (stats,0 ,sizeof (struct vdo_statistics )); + +/* +* These are immutable properties of the vdo object, so it is safe to query them from any +* thread. +*/ + stats->version =STATISTICS_VERSION ; + stats->logical_blocks = vdo->states.vdo.config.logical_blocks; +/* +* config.physical_blocks is mutated during resize and is in a packed structure, but resize +* runs on the admin thread. +* TODO: verify that this is always safe +*/ + stats->physical_blocks = vdo->states.vdo.config.physical_blocks; + stats->block_size =VDO_BLOCK_SIZE ; + stats->complete_recoveries = vdo->states.vdo.complete_recoveries; + stats->read_only_recoveries = vdo->states.vdo.read_only_recoveries; + stats->block_map_cache_size =get_block_map_cache_size (vdo); + +/* The callees are responsible for thread-safety. */ + stats->data_blocks_used =vdo_get_physical_blocks_allocated (vdo); + stats->overhead_blocks_used =vdo_get_physical_blocks_overhead (vdo); + stats->logical_blocks_used =vdo_get_recovery_journal_logical_blocks_used (journal); +vdo_get_slab_depot_statistics (vdo->depot, stats); + stats->journal =vdo_get_recovery_journal_statistics (journal); + stats->packer =vdo_get_packer_statistics (vdo->packer); + stats->block_map =vdo_get_block_map_statistics (vdo->block_map); +vdo_get_dedupe_statistics (vdo->hash_zones, stats); + stats->errors =get_vdo_error_statistics (vdo); + stats->in_recovery_mode = (state ==VDO_RECOVERING ); +snprintf (stats->mode,sizeof (stats->mode),"%s" ,vdo_describe_state (state)); + + stats->instance = vdo->instance; + stats->current_vios_in_progress =get_data_vio_pool_active_requests (vdo->data_vio_pool); + stats->max_vios =get_data_vio_pool_maximum_requests (vdo->data_vio_pool); + + stats->flush_out =atomic64_read (&vdo->stats.flush_out); + stats->logical_block_size = vdo->device_config->logical_block_size; +copy_bio_stat (&stats->bios_in, &vdo->stats.bios_in); +copy_bio_stat (&stats->bios_in_partial, &vdo->stats.bios_in_partial); +copy_bio_stat (&stats->bios_out, &vdo->stats.bios_out); +copy_bio_stat (&stats->bios_meta, &vdo->stats.bios_meta); +copy_bio_stat (&stats->bios_journal, &vdo->stats.bios_journal); +copy_bio_stat (&stats->bios_page_cache, &vdo->stats.bios_page_cache); +copy_bio_stat (&stats->bios_out_completed, &vdo->stats.bios_out_completed); +copy_bio_stat (&stats->bios_meta_completed, &vdo->stats.bios_meta_completed); +copy_bio_stat (&stats->bios_journal_completed, + &vdo->stats.bios_journal_completed); +copy_bio_stat (&stats->bios_page_cache_completed, + &vdo->stats.bios_page_cache_completed); +copy_bio_stat (&stats->bios_acknowledged, &vdo->stats.bios_acknowledged); +copy_bio_stat (&stats->bios_acknowledged_partial, &vdo->stats.bios_acknowledged_partial); + stats->bios_in_progress = +subtract_bio_stats (stats->bios_in, stats->bios_acknowledged); +uds_get_memory_stats (&stats->memory_usage.bytes_used, + &stats->memory_usage.peak_bytes_used); +} + +/** +* vdo_fetch_statistics_callback() - Action to populate a vdo_statistics +* structure on the admin thread. +* @completion: The completion. +* +* This callback is registered in vdo_fetch_statistics(). +*/ +static void vdo_fetch_statistics_callback (struct vdo_completion *completion) +{ +get_vdo_statistics (completion->vdo, completion->parent); +complete_synchronous_action (completion); +} + +/** +* vdo_fetch_statistics() - Fetch statistics on the correct thread. +* @vdo: The vdo. +* @stats: The vdo statistics are returned here. +*/ +void vdo_fetch_statistics (struct vdo *vdo,struct vdo_statistics *stats) +{ +perform_synchronous_action (vdo, vdo_fetch_statistics_callback, + vdo->thread_config.admin_thread, stats); +} + +/** +* vdo_get_callback_thread_id() - Get the id of the callback thread on which a completion is +* currently running. +* +* Return: The current thread ID, or -1 if no such thread. +*/ +thread_id_t vdo_get_callback_thread_id (void ) +{ +struct vdo_work_queue *queue =vdo_get_current_work_queue (); +struct vdo_thread *thread; +thread_id_t thread_id; + +if (queue ==NULL ) +return VDO_INVALID_THREAD_ID ; + + thread =vdo_get_work_queue_owner (queue); + thread_id = thread->thread_id; + +if (PARANOID_THREAD_CONSISTENCY_CHECKS ) { +BUG_ON (thread_id >= thread->vdo->thread_config.thread_count); +BUG_ON (thread != &thread->vdo->threads[thread_id]); + } + +return thread_id; +} + +/** +* vdo_dump_status() - Dump status information about a vdo to the log for debugging. +* @vdo: The vdo to dump. +*/ +void vdo_dump_status (const struct vdo *vdo) +{ +zone_count_t zone; + +vdo_dump_flusher (vdo->flusher); +vdo_dump_recovery_journal_statistics (vdo->recovery_journal); +vdo_dump_packer (vdo->packer); +vdo_dump_slab_depot (vdo->depot); + +for (zone =0 ; zone < vdo->thread_config.logical_zone_count; zone++) +vdo_dump_logical_zone (&vdo->logical_zones->zones[zone]); + +for (zone =0 ; zone < vdo->thread_config.physical_zone_count; zone++) +vdo_dump_physical_zone (&vdo->physical_zones->zones[zone]); + +vdo_dump_hash_zones (vdo->hash_zones); +} + +/** +* vdo_assert_on_admin_thread() - Assert that we are running on the admin thread. +* @vdo: The vdo. +* @name: The name of the function which should be running on the admin thread (for logging). +*/ +void vdo_assert_on_admin_thread (const struct vdo *vdo,const char *name) +{ +ASSERT_LOG_ONLY ((vdo_get_callback_thread_id () == vdo->thread_config.admin_thread), +"%s called on admin thread" , name); +} + +/** +* vdo_assert_on_logical_zone_thread() - Assert that this function was called on the specified +* logical zone thread. +* @vdo: The vdo. +* @logical_zone: The number of the logical zone. +* @name: The name of the calling function. +*/ +void vdo_assert_on_logical_zone_thread (const struct vdo *vdo,zone_count_t logical_zone, +const char *name) +{ +ASSERT_LOG_ONLY ((vdo_get_callback_thread_id () == + vdo->thread_config.logical_threads[logical_zone]), +"%s called on logical thread" , name); +} + +/** +* vdo_assert_on_physical_zone_thread() - Assert that this function was called on the specified +* physical zone thread. +* @vdo: The vdo. +* @physical_zone: The number of the physical zone. +* @name: The name of the calling function. +*/ +void vdo_assert_on_physical_zone_thread (const struct vdo *vdo, +zone_count_t physical_zone,const char *name) +{ +ASSERT_LOG_ONLY ((vdo_get_callback_thread_id () == + vdo->thread_config.physical_threads[physical_zone]), +"%s called on physical thread" , name); +} + +/** +* vdo_get_physical_zone() - Get the physical zone responsible for a given physical block number. +* @vdo: The vdo containing the physical zones. +* @pbn: The PBN of the data block. +* @zone_ptr: A pointer to return the physical zone. +* +* Gets the physical zone responsible for a given physical block number of a data block in this vdo +* instance, or of the zero block (for which a NULL zone is returned). For any other block number +* that is not in the range of valid data block numbers in any slab, an error will be returned. +* This function is safe to call on invalid block numbers; it will not put the vdo into read-only +* mode. +* +* Return: VDO_SUCCESS or VDO_OUT_OF_RANGE if the block number is invalid or an error code for any +* other failure. +*/ +int vdo_get_physical_zone (const struct vdo *vdo,physical_block_number_t pbn, +struct physical_zone **zone_ptr) +{ +struct vdo_slab *slab; +int result; + +if (pbn ==VDO_ZERO_BLOCK ) { + *zone_ptr =NULL ; +return VDO_SUCCESS ; + } + +/* +* Used because it does a more restrictive bounds check than vdo_get_slab(), and done first +* because it won't trigger read-only mode on an invalid PBN. +*/ +if (!vdo_is_physical_data_block (vdo->depot, pbn)) +return VDO_OUT_OF_RANGE ; + +/* With the PBN already checked, we should always succeed in finding a slab. */ + slab =vdo_get_slab (vdo->depot, pbn); + result =ASSERT (slab !=NULL ,"vdo_get_slab must succeed on all valid PBNs" ); +if (result !=VDO_SUCCESS ) +return result; + + *zone_ptr = &vdo->physical_zones->zones[slab->allocator->zone_number]; +return VDO_SUCCESS ; +}
drivers/md/dm-vdo/vdo.h new file +376 -0
376 lines not on this page · raw message
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 31/40 dm vdo: add the recovery journal
In reply to Matthew SakaiThe recovery journal is used to amortize updates across the block map and slab depot. Each write request causes an entry to be made in the journal. Entries are either "data remappings" or "block map remappings." For a data remapping, the journal records the logical address affected and its old and new physical mappings. For a block map remapping, the journal records the block map page number and the physical block allocated for it (block map pages are never reclaimed, so the old mapping is always 0). Each journal entry and the data write it represents must be stable on disk before the other metadata structures may be updated to reflect the operation.
--- drivers/md/dm-vdo/recovery-journal.c | 1761 ++++++++++++++++++++++++++ drivers/md/dm-vdo/recovery-journal.h | 312 +++++ 2 files changed, 2073 insertions(+) create mode 100644 drivers/md/dm-vdo/recovery-journal.c create mode 100644 drivers/md/dm-vdo/recovery-journal.h
drivers/md/dm-vdo/recovery-journal.c new file +1761 -0
@@ -0,0 +1,1761 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include "recovery-journal.h" + +#include <linux/atomic.h> +#include <linux/bio.h> + +#include "logger.h" +#include "memory-alloc.h" +#include "permassert.h" + +#include "block-map.h" +#include "completion.h" +#include "constants.h" +#include "data-vio.h" +#include "encodings.h" +#include "io-submitter.h" +#include "slab-depot.h" +#include "types.h" +#include "vdo.h" +#include "vio.h" +#include "wait-queue.h" + +static const u64 RECOVERY_COUNT_MASK =0xff ; + +enum { +/* +* The number of reserved blocks must be large enough to prevent a new recovery journal +* block write from overwriting a block which appears to still be a valid head block of the +* journal. Currently, that means reserving enough space for all 2048 data_vios. +*/ +RECOVERY_JOURNAL_RESERVED_BLOCKS = + (MAXIMUM_VDO_USER_VIOS /RECOVERY_JOURNAL_ENTRIES_PER_BLOCK ) +2 , +WRITE_FLAGS =REQ_OP_WRITE |REQ_PRIO |REQ_PREFLUSH |REQ_SYNC |REQ_FUA , +}; + +/** +* DOC: Lock Counters. +* +* A lock_counter is intended to keep all of the locks for the blocks in the recovery journal. The +* per-zone counters are all kept in a single array which is arranged by zone (i.e. zone 0's lock 0 +* is at index 0, zone 0's lock 1 is at index 1, and zone 1's lock 0 is at index 'locks'. This +* arrangement is intended to minimize cache-line contention for counters from different zones. +* +* The locks are implemented as a single object instead of as a lock counter per lock both to +* afford this opportunity to reduce cache line contention and also to eliminate the need to have a +* completion per lock. +* +* Lock sets are laid out with the set for recovery journal first, followed by the logical zones, +* and then the physical zones. +*/ + +enum lock_counter_state { +LOCK_COUNTER_STATE_NOT_NOTIFYING , +LOCK_COUNTER_STATE_NOTIFYING , +LOCK_COUNTER_STATE_SUSPENDED , +}; + +/** +* get_zone_count_ptr() - Get a pointer to the zone count for a given lock on a given zone. +* @journal: The recovery journal. +* @lock_number: The lock to get. +* @zone_type: The zone type whose count is desired. +* +* Return: A pointer to the zone count for the given lock and zone. +*/ +static inline atomic_t *get_zone_count_ptr (struct recovery_journal *journal, +block_count_t lock_number, +enum vdo_zone_type zone_type) +{ +return ((zone_type ==VDO_ZONE_TYPE_LOGICAL ) + ? &journal->lock_counter.logical_zone_counts[lock_number] + : &journal->lock_counter.physical_zone_counts[lock_number]); +} + +/** +* get_counter() - Get the zone counter for a given lock on a given zone. +* @journal: The recovery journal. +* @lock_number: The lock to get. +* @zone_type: The zone type whose count is desired. +* @zone_id: The zone index whose count is desired. +* +* Return: The counter for the given lock and zone. +*/ +static inline u16 *get_counter (struct recovery_journal *journal, +block_count_t lock_number,enum vdo_zone_type zone_type, +zone_count_t zone_id) +{ +struct lock_counter *counter = &journal->lock_counter; +block_count_t zone_counter = (counter->locks * zone_id) + lock_number; + +if (zone_type ==VDO_ZONE_TYPE_JOURNAL ) +return &counter->journal_counters[zone_counter]; + +if (zone_type ==VDO_ZONE_TYPE_LOGICAL ) +return &counter->logical_counters[zone_counter]; + +return &counter->physical_counters[zone_counter]; +} + +static atomic_t *get_decrement_counter (struct recovery_journal *journal, +block_count_t lock_number) +{ +return &journal->lock_counter.journal_decrement_counts[lock_number]; +} + +/** +* is_journal_zone_locked() - Check whether the journal zone is locked for a given lock. +* @journal: The recovery journal. +* @lock_number: The lock to check. +* +* Return: true if the journal zone is locked. +*/ +static bool is_journal_zone_locked (struct recovery_journal *journal, +block_count_t lock_number) +{ +u16 journal_value = *get_counter (journal, lock_number,VDO_ZONE_TYPE_JOURNAL ,0 ); +u32 decrements =atomic_read (get_decrement_counter (journal, lock_number)); + +/* Pairs with barrier in vdo_release_journal_entry_lock() */ +smp_rmb (); +ASSERT_LOG_ONLY ((decrements <= journal_value), +"journal zone lock counter must not underflow" ); +return (journal_value != decrements); +} + +/** +* vdo_release_recovery_journal_block_reference() - Release a reference to a recovery journal +* block. +* @journal: The recovery journal. +* @sequence_number: The journal sequence number of the referenced block. +* @zone_type: The type of the zone making the adjustment. +* @zone_id: The ID of the zone making the adjustment. +* +* If this is the last reference for a given zone type, an attempt will be made to reap the +* journal. +*/ +void vdo_release_recovery_journal_block_reference (struct recovery_journal *journal, +sequence_number_t sequence_number, +enum vdo_zone_type zone_type, +zone_count_t zone_id) +{ +u16 *current_value; +block_count_t lock_number; +int prior_state; + +if (sequence_number ==0 ) +return ; + + lock_number =vdo_get_recovery_journal_block_number (journal, sequence_number); + current_value =get_counter (journal, lock_number, zone_type, zone_id); + +ASSERT_LOG_ONLY ((*current_value >=1 ), +"decrement of lock counter must not underflow" ); + *current_value -=1 ; + +if (zone_type ==VDO_ZONE_TYPE_JOURNAL ) { +if (is_journal_zone_locked (journal, lock_number)) +return ; + }else { +atomic_t *zone_count; + +if (*current_value !=0 ) +return ; + + zone_count =get_zone_count_ptr (journal, lock_number, zone_type); + +if (atomic_add_return (-1 , zone_count) >0 ) +return ; + } + +/* +* Extra barriers because this was original developed using a CAS operation that implicitly +* had them. +*/ +smp_mb__before_atomic (); + prior_state =atomic_cmpxchg (&journal->lock_counter.state, +LOCK_COUNTER_STATE_NOT_NOTIFYING , +LOCK_COUNTER_STATE_NOTIFYING ); +/* same as before_atomic */ +smp_mb__after_atomic (); + +if (prior_state !=LOCK_COUNTER_STATE_NOT_NOTIFYING ) +return ; + +vdo_launch_completion (&journal->lock_counter.completion); +} + +static inline struct recovery_journal_block *__must_check get_journal_block (struct list_head *list) +{ + return list_first_entry_or_null(list, struct recovery_journal_block, list_node); +} + +/** +* pop_free_list() - Get a block from the end of the free list. +* @journal: The journal. +* +* Return: The block or NULL if the list is empty. +*/ +static struct recovery_journal_block *__must_check pop_free_list (struct recovery_journal *journal) +{ +struct recovery_journal_block *block; + +if (list_empty (&journal->free_tail_blocks)) +return NULL ; + + block =list_last_entry (&journal->free_tail_blocks, + struct recovery_journal_block, list_node); +list_del_init (&block->list_node); +return block; +} + +/** +* is_block_dirty() - Check whether a recovery block is dirty. +* @block: The block to check. +* +* Indicates it has any uncommitted entries, which includes both entries not written and entries +* written but not yet acknowledged. +* +* Return: true if the block has any uncommitted entries. +*/ +static inline bool __must_check is_block_dirty (const struct recovery_journal_block *block) +{ +return (block->uncommitted_entry_count >0 ); +} + +/** +* is_block_empty() - Check whether a journal block is empty. +* @block: The block to check. +* +* Return: true if the block has no entries. +*/ +static inline bool __must_check is_block_empty (const struct recovery_journal_block *block) +{ +return (block->entry_count ==0 ); +} + +/** +* is_block_full() - Check whether a journal block is full. +* @block: The block to check. +* +* Return: true if the block is full. +*/ +static inline bool __must_check is_block_full (const struct recovery_journal_block *block) +{ +return ((block ==NULL ) || (block->journal->entries_per_block == block->entry_count)); +} + +/** +* assert_on_journal_thread() - Assert that we are running on the journal thread. +* @journal: The journal. +* @function_name: The function doing the check (for logging). +*/ +static void assert_on_journal_thread (struct recovery_journal *journal, +const char *function_name) +{ +ASSERT_LOG_ONLY ((vdo_get_callback_thread_id () == journal->thread_id), +"%s() called on journal thread" , function_name); +} + +/** +* continue_waiter() - Release a data_vio from the journal. +* +* Invoked whenever a data_vio is to be released from the journal, either because its entry was +* committed to disk, or because there was an error. Implements waiter_callback_fn. +*/ +static void continue_waiter (struct waiter *waiter,void *context) +{ +continue_data_vio_with_error (waiter_as_data_vio (waiter), *((int *) context)); +} + +/** +* has_block_waiters() - Check whether the journal has any waiters on any blocks. +* @journal: The journal in question. +* +* Return: true if any block has a waiter. +*/ +static inline bool has_block_waiters (struct recovery_journal *journal) +{ +struct recovery_journal_block *block =get_journal_block (&journal->active_tail_blocks); + +/* +* Either the first active tail block (if it exists) has waiters, or no active tail block +* has waiters. +*/ +return ((block !=NULL ) && + (vdo_has_waiters (&block->entry_waiters) || +vdo_has_waiters (&block->commit_waiters))); +} + +static void recycle_journal_blocks (struct recovery_journal *journal); +static void recycle_journal_block (struct recovery_journal_block *block); +static void notify_commit_waiters (struct recovery_journal *journal); + +/** +* suspend_lock_counter() - Prevent the lock counter from notifying. +* @counter: The counter. +* +* Return: true if the lock counter was not notifying and hence the suspend was efficacious. +*/ +static bool suspend_lock_counter (struct lock_counter *counter) +{ +int prior_state; + +/* +* Extra barriers because this was originally developed using a CAS operation that +* implicitly had them. +*/ +smp_mb__before_atomic (); + prior_state =atomic_cmpxchg (&counter->state,LOCK_COUNTER_STATE_NOT_NOTIFYING , +LOCK_COUNTER_STATE_SUSPENDED ); +/* same as before_atomic */ +smp_mb__after_atomic (); + +return ((prior_state ==LOCK_COUNTER_STATE_SUSPENDED ) || + (prior_state ==LOCK_COUNTER_STATE_NOT_NOTIFYING )); +} + +static inline bool is_read_only (struct recovery_journal *journal) +{ +return vdo_is_read_only (journal->flush_vio->completion.vdo); +} + +/** +* check_for_drain_complete() - Check whether the journal has drained. +* @journal: The journal which may have just drained. +*/ +static void check_for_drain_complete (struct recovery_journal *journal) +{ +int result =VDO_SUCCESS ; + +if (is_read_only (journal)) { + result =VDO_READ_ONLY ; +/* +* Clean up any full active blocks which were not written due to read-only mode. +* +* FIXME: This would probably be better as a short-circuit in write_block(). +*/ +notify_commit_waiters (journal); +recycle_journal_blocks (journal); + +/* Release any data_vios waiting to be assigned entries. */ +vdo_notify_all_waiters (&journal->entry_waiters, continue_waiter, + &result); + } + +if (!vdo_is_state_draining (&journal->state) || + journal->reaping || +has_block_waiters (journal) || +vdo_has_waiters (&journal->entry_waiters) || + !suspend_lock_counter (&journal->lock_counter)) +return ; + +if (vdo_is_state_saving (&journal->state)) { +if (journal->active_block !=NULL ) { +ASSERT_LOG_ONLY (((result ==VDO_READ_ONLY ) || + !is_block_dirty (journal->active_block)), +"journal being saved has clean active block" ); +recycle_journal_block (journal->active_block); + } + +ASSERT_LOG_ONLY (list_empty (&journal->active_tail_blocks), +"all blocks in a journal being saved must be inactive" ); + } + +vdo_finish_draining_with_result (&journal->state, result); +} + +/** +* notify_recovery_journal_of_read_only_mode() - Notify a recovery journal that the VDO has gone +* read-only. +* @listener: The journal. +* @parent: The completion to notify in order to acknowledge the notification. +* +* Implements vdo_read_only_notification_fn. +*/ +static void notify_recovery_journal_of_read_only_mode (void *listener, +struct vdo_completion *parent) +{ +check_for_drain_complete (listener); +vdo_finish_completion (parent); +} + +/** +* enter_journal_read_only_mode() - Put the journal in read-only mode. +* @journal: The journal which has failed. +* @error_code: The error result triggering this call. +* +* All attempts to add entries after this function is called will fail. All VIOs waiting for +* commits will be awakened with an error. +*/ +static void enter_journal_read_only_mode (struct recovery_journal *journal, +int error_code) +{ +vdo_enter_read_only_mode (journal->flush_vio->completion.vdo, error_code); +check_for_drain_complete (journal); +} + +/** +* vdo_get_recovery_journal_current_sequence_number() - Obtain the recovery journal's current +* sequence number. +* @journal: The journal in question. +* +* Exposed only so the block map can be initialized therefrom. +* +* Return: The sequence number of the tail block. +*/ +sequence_number_t vdo_get_recovery_journal_current_sequence_number (struct recovery_journal *journal) +{ +return journal->tail; +} + +/** +* get_recovery_journal_head() - Get the head of the recovery journal. +* @journal: The journal. +* +* The head is the lowest sequence number of the block map head and the slab journal head. +* +* Return: the head of the journal. +*/ +static inline sequence_number_t get_recovery_journal_head (const struct recovery_journal *journal) +{ +return min (journal->block_map_head, journal->slab_journal_head); +} + +/** +* compute_recovery_count_byte() - Compute the recovery count byte for a given recovery count. +* @recovery_count: The recovery count. +* +* Return: The byte corresponding to the recovery count. +*/ +static inline u8 __must_check compute_recovery_count_byte (u64 recovery_count) +{ +return (u8)(recovery_count &RECOVERY_COUNT_MASK ); +} + +/** +* check_slab_journal_commit_threshold() - Check whether the journal is over the threshold, and if +* so, force the oldest slab journal tail block to commit. +* @journal: The journal. +*/ +static void check_slab_journal_commit_threshold (struct recovery_journal *journal) +{ +block_count_t current_length = journal->tail - journal->slab_journal_head; + +if (current_length > journal->slab_journal_commit_threshold) { + journal->events.slab_journal_commits_requested++; +vdo_commit_oldest_slab_journal_tail_blocks (journal->depot, + journal->slab_journal_head); + } +} + +static void reap_recovery_journal (struct recovery_journal *journal); +static void assign_entries (struct recovery_journal *journal); + +/** +* finish_reaping() - Finish reaping the journal. +* @journal: The journal being reaped. +*/ +static void finish_reaping (struct recovery_journal *journal) +{ +block_count_t blocks_reaped; +sequence_number_t old_head =get_recovery_journal_head (journal); + + journal->block_map_head = journal->block_map_reap_head; + journal->slab_journal_head = journal->slab_journal_reap_head; + blocks_reaped =get_recovery_journal_head (journal) - old_head; + journal->available_space += blocks_reaped * journal->entries_per_block; + journal->reaping = false; +check_slab_journal_commit_threshold (journal); +assign_entries (journal); +check_for_drain_complete (journal); +} + +/** +* complete_reaping() - Finish reaping the journal after flushing the lower layer. +* @completion: The journal's flush VIO. +* +* This is the callback registered in reap_recovery_journal(). +*/ +static void complete_reaping (struct vdo_completion *completion) +{ +struct recovery_journal *journal = completion->parent; + +finish_reaping (journal); + +/* Try reaping again in case more locks were released while flush was out. */ +reap_recovery_journal (journal); +} + +/** +* handle_flush_error() - Handle an error when flushing the lower layer due to reaping. +* @completion: The journal's flush VIO. +*/ +static void handle_flush_error (struct vdo_completion *completion) +{ +struct recovery_journal *journal = completion->parent; + +vio_record_metadata_io_error (as_vio (completion)); + journal->reaping = false; +enter_journal_read_only_mode (journal, completion->result); +} + +static void flush_endio (struct bio *bio) +{ +struct vio *vio = bio->bi_private; +struct recovery_journal *journal = vio->completion.parent; + +continue_vio_after_io (vio, complete_reaping, journal->thread_id); +} + +/** +* initialize_journal_state() - Set all journal fields appropriately to start journaling from the +* current active block. +* @journal: The journal to be reset based on its active block. +*/ +static void initialize_journal_state (struct recovery_journal *journal) +{ + journal->append_point.sequence_number = journal->tail; + journal->last_write_acknowledged = journal->tail; + journal->block_map_head = journal->tail; + journal->slab_journal_head = journal->tail; + journal->block_map_reap_head = journal->tail; + journal->slab_journal_reap_head = journal->tail; + journal->block_map_head_block_number = +vdo_get_recovery_journal_block_number (journal, journal->block_map_head); + journal->slab_journal_head_block_number = +vdo_get_recovery_journal_block_number (journal, + journal->slab_journal_head); + journal->available_space = + (journal->entries_per_block *vdo_get_recovery_journal_length (journal->size)); +} + +/** +* vdo_get_recovery_journal_length() - Get the number of usable recovery journal blocks. +* @journal_size: The size of the recovery journal in blocks. +* +* Return: the number of recovery journal blocks usable for entries. +*/ +block_count_t vdo_get_recovery_journal_length (block_count_t journal_size) +{ +block_count_t reserved_blocks = journal_size /4 ; + +if (reserved_blocks >RECOVERY_JOURNAL_RESERVED_BLOCKS ) + reserved_blocks =RECOVERY_JOURNAL_RESERVED_BLOCKS ; +return (journal_size - reserved_blocks); +} + +/** +* reap_recovery_journal_callback() - Attempt to reap the journal. +* @completion: The lock counter completion. +* +* Attempts to reap the journal now that all the locks on some journal block have been released. +* This is the callback registered with the lock counter. +*/ +static void reap_recovery_journal_callback (struct vdo_completion *completion) +{ +struct recovery_journal *journal = (struct recovery_journal *) completion->parent; +/* +* The acknowledgment must be done before reaping so that there is no race between +* acknowledging the notification and unlocks wishing to notify. +*/ +smp_wmb (); +atomic_set (&journal->lock_counter.state,LOCK_COUNTER_STATE_NOT_NOTIFYING ); + +if (vdo_is_state_quiescing (&journal->state)) { +/* +* Don't start reaping when the journal is trying to quiesce. Do check if this +* notification is the last thing the is waiting on. +*/ +check_for_drain_complete (journal); +return ; + } + +reap_recovery_journal (journal); +check_slab_journal_commit_threshold (journal); +} + +/** +* initialize_lock_counter() - Initialize a lock counter. +* +* @journal: The recovery journal. +* @vdo: The vdo. +* +* Return: VDO_SUCCESS or an error. +*/ +static int __must_check initialize_lock_counter (struct recovery_journal *journal, +struct vdo *vdo) +{ +int result; +struct thread_config *config = &vdo->thread_config; +struct lock_counter *counter = &journal->lock_counter; + + result =uds_allocate (journal->size, u16, __func__, &counter->journal_counters); +if (result !=VDO_SUCCESS ) +return result; + + result =uds_allocate (journal->size, atomic_t, __func__, + &counter->journal_decrement_counts); +if (result !=VDO_SUCCESS ) +return result; + + result =uds_allocate (journal->size * config->logical_zone_count, u16, __func__, + &counter->logical_counters); +if (result !=VDO_SUCCESS ) +return result; + + result =uds_allocate (journal->size, atomic_t, __func__, + &counter->logical_zone_counts); +if (result !=VDO_SUCCESS ) +return result; + + result =uds_allocate (journal->size * config->physical_zone_count, u16, __func__, + &counter->physical_counters); +if (result !=VDO_SUCCESS ) +return result; + + result =uds_allocate (journal->size, atomic_t, __func__, + &counter->physical_zone_counts); +if (result !=VDO_SUCCESS ) +return result; + +vdo_initialize_completion (&counter->completion, vdo, +VDO_LOCK_COUNTER_COMPLETION ); +vdo_prepare_completion (&counter->completion, reap_recovery_journal_callback, + reap_recovery_journal_callback, config->journal_thread, + journal); + counter->logical_zones = config->logical_zone_count; + counter->physical_zones = config->physical_zone_count; + counter->locks = journal->size; +return VDO_SUCCESS ; +} + +/** +* set_journal_tail() - Set the journal's tail sequence number. +* @journal: The journal whose tail is to be set. +* @tail: The new tail value. +*/ +static void set_journal_tail (struct recovery_journal *journal,sequence_number_t tail) +{ +/* VDO does not support sequence numbers above 1 << 48 in the slab journal. */ +if (tail >= (1ULL <<48 )) +enter_journal_read_only_mode (journal,VDO_JOURNAL_OVERFLOW ); + + journal->tail = tail; +} + +/** +* initialize_recovery_block() - Initialize a journal block. +* @vdo: The vdo from which to construct vios. +* @journal: The journal to which the block will belong. +* @block: The block to initialize. +* +* Return: VDO_SUCCESS or an error. +*/ +static int initialize_recovery_block (struct vdo *vdo,struct recovery_journal *journal, +struct recovery_journal_block *block) +{ +char *data; +int result; + +/* +* Ensure that a block is large enough to store RECOVERY_JOURNAL_ENTRIES_PER_BLOCK entries. +*/ +BUILD_BUG_ON (RECOVERY_JOURNAL_ENTRIES_PER_BLOCK > + ((VDO_BLOCK_SIZE -sizeof (struct packed_journal_header )) / +sizeof (struct packed_recovery_journal_entry ))); + +/* +* Allocate a full block for the journal block even though not all of the space is used +* since the VIO needs to write a full disk block. +*/ + result =uds_allocate (VDO_BLOCK_SIZE , char, __func__, &data); +if (result !=VDO_SUCCESS ) +return result; + + result =allocate_vio_components (vdo,VIO_TYPE_RECOVERY_JOURNAL , +VIO_PRIORITY_HIGH , block,1 , data, &block->vio); +if (result !=VDO_SUCCESS ) { +uds_free (data); +return result; + } + +list_add_tail (&block->list_node, &journal->free_tail_blocks); + block->journal = journal; +return VDO_SUCCESS ; +} + +/** +* vdo_decode_recovery_journal() - Make a recovery journal and initialize it with the state that +* was decoded from the super block. +* +* @state: The decoded state of the journal. +* @nonce: The nonce of the VDO. +* @vdo: The VDO. +* @partition: The partition for the journal. +* @recovery_count: The VDO's number of completed recoveries. +* @journal_size: The number of blocks in the journal on disk. +* @journal_ptr: The pointer to hold the new recovery journal. +* +* Return: A success or error code. +*/ +int vdo_decode_recovery_journal (struct recovery_journal_state_7_0 state,nonce_t nonce, +struct vdo *vdo,struct partition *partition, +u64 recovery_count,block_count_t journal_size, +struct recovery_journal **journal_ptr) +{ +block_count_t i; +struct recovery_journal *journal; +int result; + + result = uds_allocate_extended(struct recovery_journal, +RECOVERY_JOURNAL_RESERVED_BLOCKS , + struct recovery_journal_block, __func__, + &journal); +if (result !=VDO_SUCCESS ) +return result; + +INIT_LIST_HEAD (&journal->free_tail_blocks); +INIT_LIST_HEAD (&journal->active_tail_blocks); +vdo_initialize_wait_queue (&journal->pending_writes); + + journal->thread_id = vdo->thread_config.journal_thread; + journal->origin = partition->offset; + journal->nonce = nonce; + journal->recovery_count =compute_recovery_count_byte (recovery_count); + journal->size = journal_size; + journal->slab_journal_commit_threshold = (journal_size *2 ) /3 ; + journal->logical_blocks_used = state.logical_blocks_used; + journal->block_map_data_blocks = state.block_map_data_blocks; + journal->entries_per_block =RECOVERY_JOURNAL_ENTRIES_PER_BLOCK ; +set_journal_tail (journal, state.journal_start); +initialize_journal_state (journal); +/* TODO: this will have to change if we make initial resume of a VDO a real resume */ +vdo_set_admin_state_code (&journal->state,VDO_ADMIN_STATE_SUSPENDED ); + +for (i =0 ; i <RECOVERY_JOURNAL_RESERVED_BLOCKS ; i++) { +struct recovery_journal_block *block = &journal->blocks[i]; + + result =initialize_recovery_block (vdo, journal, block); +if (result !=VDO_SUCCESS ) { +vdo_free_recovery_journal (journal); +return result; + } + } + + result =initialize_lock_counter (journal, vdo); +if (result !=VDO_SUCCESS ) { +vdo_free_recovery_journal (journal); +return result; + } + + result =create_metadata_vio (vdo,VIO_TYPE_RECOVERY_JOURNAL ,VIO_PRIORITY_HIGH , + journal,NULL , &journal->flush_vio); +if (result !=VDO_SUCCESS ) { +vdo_free_recovery_journal (journal); +return result; + } + + result =vdo_register_read_only_listener (vdo, journal, + notify_recovery_journal_of_read_only_mode, + journal->thread_id); +if (result !=VDO_SUCCESS ) { +vdo_free_recovery_journal (journal); +return result; + } + + result =vdo_make_default_thread (vdo, journal->thread_id); +if (result !=VDO_SUCCESS ) { +vdo_free_recovery_journal (journal); +return result; + } + + journal->flush_vio->completion.callback_thread_id = journal->thread_id; + *journal_ptr = journal; +return VDO_SUCCESS ; +} + +/** +* vdo_free_recovery_journal() - Free a recovery journal. +* @journal: The recovery journal to free. +*/ +void vdo_free_recovery_journal (struct recovery_journal *journal) +{ +block_count_t i; + +if (journal ==NULL ) +return ; + +uds_free (uds_forget (journal->lock_counter.logical_zone_counts)); +uds_free (uds_forget (journal->lock_counter.physical_zone_counts)); +uds_free (uds_forget (journal->lock_counter.journal_counters)); +uds_free (uds_forget (journal->lock_counter.journal_decrement_counts)); +uds_free (uds_forget (journal->lock_counter.logical_counters)); +uds_free (uds_forget (journal->lock_counter.physical_counters)); +free_vio (uds_forget (journal->flush_vio)); + +/* +* FIXME: eventually, the journal should be constructed in a quiescent state which +* requires opening before use. +*/ +if (!vdo_is_state_quiescent (&journal->state)) { +ASSERT_LOG_ONLY (list_empty (&journal->active_tail_blocks), +"journal being freed has no active tail blocks" ); + }else if (!vdo_is_state_saved (&journal->state) && + !list_empty (&journal->active_tail_blocks)) { +uds_log_warning ("journal being freed has uncommitted entries" ); + } + +for (i =0 ; i <RECOVERY_JOURNAL_RESERVED_BLOCKS ; i++) { +struct recovery_journal_block *block = &journal->blocks[i]; + +uds_free (uds_forget (block->vio.data)); +free_vio_components (&block->vio); + } + +uds_free (journal); +} + +/** +* vdo_initialize_recovery_journal_post_repair() - Initialize the journal after a repair. +* @journal: The journal in question. +* @recovery_count: The number of completed recoveries. +* @tail: The new tail block sequence number. +* @logical_blocks_used: The new number of logical blocks used. +* @block_map_data_blocks: The new number of block map data blocks. +*/ +void vdo_initialize_recovery_journal_post_repair (struct recovery_journal *journal, +u64 recovery_count, +sequence_number_t tail, +block_count_t logical_blocks_used, +block_count_t block_map_data_blocks) +{ +set_journal_tail (journal, tail +1 ); + journal->recovery_count =compute_recovery_count_byte (recovery_count); +initialize_journal_state (journal); + journal->logical_blocks_used = logical_blocks_used; + journal->block_map_data_blocks = block_map_data_blocks; +} + +/** +* vdo_get_journal_block_map_data_blocks_used() - Get the number of block map pages, allocated from +* data blocks, currently in use. +* @journal: The journal in question. +* +* Return: The number of block map pages allocated from slabs. +*/ +block_count_t vdo_get_journal_block_map_data_blocks_used (struct recovery_journal *journal) +{ +return journal->block_map_data_blocks; +} + +/** +* vdo_get_recovery_journal_thread_id() - Get the ID of a recovery journal's thread. +* @journal: The journal to query. +* +* Return: The ID of the journal's thread. +*/ +thread_id_t vdo_get_recovery_journal_thread_id (struct recovery_journal *journal) +{ +return journal->thread_id; +} + +/** +* vdo_open_recovery_journal() - Prepare the journal for new entries. +* @journal: The journal in question. +* @depot: The slab depot for this VDO. +* @block_map: The block map for this VDO. +*/ +void vdo_open_recovery_journal (struct recovery_journal *journal, +struct slab_depot *depot,struct block_map *block_map) +{ + journal->depot = depot; + journal->block_map = block_map; +WRITE_ONCE (journal->state.current_state,VDO_ADMIN_STATE_NORMAL_OPERATION ); +} + +/** +* vdo_record_recovery_journal() - Record the state of a recovery journal for encoding in the super +* block. +* @journal: the recovery journal. +* +* Return: the state of the journal. +*/ +struct recovery_journal_state_7_0 +vdo_record_recovery_journal (const struct recovery_journal *journal) +{ +struct recovery_journal_state_7_0 state = { + .logical_blocks_used = journal->logical_blocks_used, + .block_map_data_blocks = journal->block_map_data_blocks, + }; + +if (vdo_is_state_saved (&journal->state)) { +/* +* If the journal is saved, we should start one past the active block (since the +* active block is not guaranteed to be empty). +*/ + state.journal_start = journal->tail; + }else { +/* +* When we're merely suspended or have gone read-only, we must record the first +* block that might have entries that need to be applied. +*/ + state.journal_start =get_recovery_journal_head (journal); + } + +return state; +} + +/** +* get_block_header() - Get a pointer to the packed journal block header in the block buffer. +* @block: The recovery block. +* +* Return: The block's header. +*/ +static inline struct packed_journal_header * +get_block_header (const struct recovery_journal_block *block) +{ +return (struct packed_journal_header *) block->vio.data; +} + +/** +* set_active_sector() - Set the current sector of the current block and initialize it. +* @block: The block to update. +* @sector: A pointer to the first byte of the new sector. +*/ +static void set_active_sector (struct recovery_journal_block *block,void *sector) +{ + block->sector = sector; + block->sector->check_byte =get_block_header (block)->check_byte; + block->sector->recovery_count = block->journal->recovery_count; + block->sector->entry_count =0 ; +} + +/** +* advance_tail() - Advance the tail of the journal. +* @journal: The journal whose tail should be advanced. +* +* Return: true if the tail was advanced. +*/ +static bool advance_tail (struct recovery_journal *journal) +{ +struct recovery_block_header unpacked; +struct packed_journal_header *header; +struct recovery_journal_block *block; + + block = journal->active_block =pop_free_list (journal); +if (block ==NULL ) +return false; + +list_move_tail (&block->list_node, &journal->active_tail_blocks); + + unpacked = (struct recovery_block_header ) { + .metadata_type =VDO_METADATA_RECOVERY_JOURNAL_2 , + .block_map_data_blocks = journal->block_map_data_blocks, + .logical_blocks_used = journal->logical_blocks_used, + .nonce = journal->nonce, + .recovery_count = journal->recovery_count, + .sequence_number = journal->tail, + .check_byte =vdo_compute_recovery_journal_check_byte (journal, + journal->tail), + }; + + header =get_block_header (block); +memset (block->vio.data,0x0 ,VDO_BLOCK_SIZE ); + block->sequence_number = journal->tail; + block->entry_count =0 ; + block->uncommitted_entry_count =0 ; + block->block_number =vdo_get_recovery_journal_block_number (journal, + journal->tail); + +vdo_pack_recovery_block_header (&unpacked, header); +set_active_sector (block,vdo_get_journal_block_sector (header,1 )); +set_journal_tail (journal, journal->tail +1 ); +vdo_advance_block_map_era (journal->block_map, journal->tail); +return true; +} + +/** +* initialize_lock_count() - Initialize the value of the journal zone's counter for a given lock. +* @journal: The recovery journal. +* +* Context: This must be called from the journal zone. +*/ +static void initialize_lock_count (struct recovery_journal *journal) +{ +u16 *journal_value; +block_count_t lock_number = journal->active_block->block_number; +atomic_t *decrement_counter =get_decrement_counter (journal, lock_number); + + journal_value =get_counter (journal, lock_number,VDO_ZONE_TYPE_JOURNAL ,0 ); +ASSERT_LOG_ONLY ((*journal_value ==atomic_read (decrement_counter)), +"count to be initialized not in use" ); + *journal_value = journal->entries_per_block +1 ; +atomic_set (decrement_counter,0 ); +} + +/** +* prepare_to_assign_entry() - Prepare the currently active block to receive an entry and check +* whether an entry of the given type may be assigned at this time. +* @journal: The journal receiving an entry. +* +* Return: true if there is space in the journal to store an entry of the specified type. +*/ +static bool prepare_to_assign_entry (struct recovery_journal *journal) +{ +if (journal->available_space ==0 ) +return false; + +if (is_block_full (journal->active_block) && !advance_tail (journal)) +return false; + +if (!is_block_empty (journal->active_block)) +return true; + +if ((journal->tail -get_recovery_journal_head (journal)) > journal->size) { +/* Cannot use this block since the journal is full. */ + journal->events.disk_full++; +return false; + } + +/* +* Don't allow the new block to be reaped until all of its entries have been committed to +* the block map and until the journal block has been fully committed as well. Because the +* block map update is done only after any slab journal entries have been made, the +* per-entry lock for the block map entry serves to protect those as well. +*/ +initialize_lock_count (journal); +return true; +} + +static void write_blocks (struct recovery_journal *journal); + +/** +* schedule_block_write() - Queue a block for writing. +* @journal: The journal in question. +* @block: The block which is now ready to write. +* +* The block is expected to be full. If the block is currently writing, this is a noop as the block +* will be queued for writing when the write finishes. The block must not currently be queued for +* writing. +*/ +static void schedule_block_write (struct recovery_journal *journal, +struct recovery_journal_block *block) +{ +if (!block->committing) +vdo_enqueue_waiter (&journal->pending_writes, &block->write_waiter); +/* +* At the end of adding entries, or discovering this partial block is now full and ready to +* rewrite, we will call write_blocks() and write a whole batch. +*/ +} + +/** +* release_journal_block_reference() - Release a reference to a journal block. +* @block: The journal block from which to release a reference. +*/ +static void release_journal_block_reference (struct recovery_journal_block *block) +{ +vdo_release_recovery_journal_block_reference (block->journal, + block->sequence_number, +VDO_ZONE_TYPE_JOURNAL ,0 ); +} + +static void update_usages (struct recovery_journal *journal,struct data_vio *data_vio) +{ +if (data_vio->increment_updater.operation ==VDO_JOURNAL_BLOCK_MAP_REMAPPING ) { + journal->block_map_data_blocks++; +return ; + } + +if (data_vio->new_mapped.state !=VDO_MAPPING_STATE_UNMAPPED ) + journal->logical_blocks_used++; + +if (data_vio->mapped.state !=VDO_MAPPING_STATE_UNMAPPED ) + journal->logical_blocks_used--; +} + +/** +* assign_entry() - Assign an entry waiter to the active block. +* +* Implements waiter_callback_fn. +*/ +static void assign_entry (struct waiter *waiter,void *context) +{ +struct data_vio *data_vio =waiter_as_data_vio (waiter); +struct recovery_journal_block *block = context; +struct recovery_journal *journal = block->journal; + +/* Record the point at which we will make the journal entry. */ + data_vio->recovery_journal_point = (struct journal_point ) { + .sequence_number = block->sequence_number, + .entry_count = block->entry_count, + }; + +update_usages (journal, data_vio); + journal->available_space--; + +if (!vdo_has_waiters (&block->entry_waiters)) + journal->events.blocks.started++; + +vdo_enqueue_waiter (&block->entry_waiters, &data_vio->waiter); + block->entry_count++; + block->uncommitted_entry_count++; + journal->events.entries.started++; + +if (is_block_full (block)) { +/* +* The block is full, so we can write it anytime henceforth. If it is already +* committing, we'll queue it for writing when it comes back. +*/ +schedule_block_write (journal, block); + } + +/* Force out slab journal tail blocks when threshold is reached. */ +check_slab_journal_commit_threshold (journal); +} + +static void assign_entries (struct recovery_journal *journal) +{ +if (journal->adding_entries) { +/* Protect against re-entrancy. */ +return ; + } + + journal->adding_entries = true; +while (vdo_has_waiters (&journal->entry_waiters) &&prepare_to_assign_entry (journal)) { +vdo_notify_next_waiter (&journal->entry_waiters, assign_entry, + journal->active_block); + } + +/* Now that we've finished with entries, see if we have a batch of blocks to write. */ +write_blocks (journal); + journal->adding_entries = false; +} + +/** +* recycle_journal_block() - Prepare an in-memory journal block to be reused now that it has been +* fully committed. +* @block: The block to be recycled. +*/ +static void recycle_journal_block (struct recovery_journal_block *block) +{ +struct recovery_journal *journal = block->journal; +block_count_t i; + +list_move_tail (&block->list_node, &journal->free_tail_blocks); + +/* Release any unused entry locks. */ +for (i = block->entry_count; i < journal->entries_per_block; i++) +release_journal_block_reference (block); + +/* +* Release our own lock against reaping now that the block is completely committed, or +* we're giving up because we're in read-only mode. +*/ +if (block->entry_count >0 ) +release_journal_block_reference (block); + +if (block == journal->active_block) + journal->active_block =NULL ; +} + +/** +* continue_committed_waiter() - invoked whenever a VIO is to be released from the journal because +* its entry was committed to disk. +* +* Implements waiter_callback_fn. +*/ +static void continue_committed_waiter (struct waiter *waiter,void *context) +{ +struct data_vio *data_vio =waiter_as_data_vio (waiter); +struct recovery_journal *journal = context; +int result = (is_read_only (journal) ?VDO_READ_ONLY :VDO_SUCCESS ); +bool has_decrement; + +ASSERT_LOG_ONLY (vdo_before_journal_point (&journal->commit_point, + &data_vio->recovery_journal_point), +"DataVIOs released from recovery journal in order. Recovery journal point is (%llu, %u), but commit waiter point is (%llu, %u)" , + (unsigned long long ) journal->commit_point.sequence_number, + journal->commit_point.entry_count, + (unsigned long long ) data_vio->recovery_journal_point.sequence_number, + data_vio->recovery_journal_point.entry_count); + + journal->commit_point = data_vio->recovery_journal_point; + data_vio->last_async_operation =VIO_ASYNC_OP_UPDATE_REFERENCE_COUNTS ; +if (result !=VDO_SUCCESS ) { +continue_data_vio_with_error (data_vio, result); +return ; + } + +/* +* The increment must be launched first since it must come before the +* decrement if they are in the same slab. +*/ + has_decrement = (data_vio->decrement_updater.zpbn.pbn !=VDO_ZERO_BLOCK ); +if ((data_vio->increment_updater.zpbn.pbn !=VDO_ZERO_BLOCK ) || !has_decrement) +continue_data_vio (data_vio); + +if (has_decrement) +vdo_launch_completion (&data_vio->decrement_completion); +} + +/** +* notify_commit_waiters() - Notify any VIOs whose entries have now committed. +* @journal: The recovery journal to update. +*/ +static void notify_commit_waiters (struct recovery_journal *journal) +{ +struct recovery_journal_block *block; + +list_for_each_entry (block, &journal->active_tail_blocks, list_node) { +if (block->committing) +return ; + +vdo_notify_all_waiters (&block->commit_waiters, continue_committed_waiter, + journal); +if (is_read_only (journal)) { +vdo_notify_all_waiters (&block->entry_waiters, + continue_committed_waiter, journal); + }else if (is_block_dirty (block) || !is_block_full (block)) { +/* Stop at partially-committed or partially-filled blocks. */ +return ; + } + } +} + +/** +* recycle_journal_blocks() - Recycle any journal blocks which have been fully committed. +* @journal: The recovery journal to update. +*/ +static void recycle_journal_blocks (struct recovery_journal *journal) +{ +struct recovery_journal_block *block, *tmp; + +list_for_each_entry_safe (block, tmp, &journal->active_tail_blocks, list_node) { +if (block->committing) { +/* Don't recycle committing blocks. */ +return ; + } + +if (!is_read_only (journal) && + (is_block_dirty (block) || !is_block_full (block))) { +/* +* Don't recycle partially written or partially full blocks, except in +* read-only mode. +*/ +return ; + } + +recycle_journal_block (block); + } +} + +/** +* complete_write() - Handle post-commit processing. +* @completion: The completion of the VIO writing this block. +* +* This is the callback registered by write_block(). If more entries accumulated in the block being +* committed while the commit was in progress, another commit will be initiated. +*/ +static void complete_write (struct vdo_completion *completion) +{ +struct recovery_journal_block *block = completion->parent; +struct recovery_journal *journal = block->journal; +struct recovery_journal_block *last_active_block; + +assert_on_journal_thread (journal, __func__); + + journal->pending_write_count -=1 ; + journal->events.blocks.committed +=1 ; + journal->events.entries.committed += block->entries_in_commit; + block->uncommitted_entry_count -= block->entries_in_commit; + block->entries_in_commit =0 ; + block->committing = false; + +/* If this block is the latest block to be acknowledged, record that fact. */ +if (block->sequence_number > journal->last_write_acknowledged) + journal->last_write_acknowledged = block->sequence_number; + + last_active_block =get_journal_block (&journal->active_tail_blocks); +ASSERT_LOG_ONLY ((block->sequence_number >= last_active_block->sequence_number), +"completed journal write is still active" ); + +notify_commit_waiters (journal); + +/* +* Is this block now full? Reaping, and adding entries, might have already sent it off for +* rewriting; else, queue it for rewrite. +*/ +if (is_block_dirty (block) &&is_block_full (block)) +schedule_block_write (journal, block); + +recycle_journal_blocks (journal); +write_blocks (journal); + +check_for_drain_complete (journal); +} + +static void handle_write_error (struct vdo_completion *completion) +{ +struct recovery_journal_block *block = completion->parent; +struct recovery_journal *journal = block->journal; + +vio_record_metadata_io_error (as_vio (completion)); +uds_log_error_strerror (completion->result, +"cannot write recovery journal block %llu" , + (unsigned long long ) block->sequence_number); +enter_journal_read_only_mode (journal, completion->result); +complete_write (completion); +} + +static void complete_write_endio (struct bio *bio) +{ +struct vio *vio = bio->bi_private; +struct recovery_journal_block *block = vio->completion.parent; +struct recovery_journal *journal = block->journal; + +continue_vio_after_io (vio, complete_write, journal->thread_id); +} + +/** +* add_queued_recovery_entries() - Actually add entries from the queue to the given block. +* @block: The journal block. +*/ +static void add_queued_recovery_entries (struct recovery_journal_block *block) +{ +while (vdo_has_waiters (&block->entry_waiters)) { +struct data_vio *data_vio = +waiter_as_data_vio (vdo_dequeue_next_waiter (&block->entry_waiters)); +struct tree_lock *lock = &data_vio->tree_lock; +struct packed_recovery_journal_entry *packed_entry; +struct recovery_journal_entry new_entry; + +if (block->sector->entry_count ==RECOVERY_JOURNAL_ENTRIES_PER_SECTOR ) +set_active_sector (block, + (char *) block->sector +VDO_SECTOR_SIZE ); + +/* Compose and encode the entry. */ + packed_entry = &block->sector->entries[block->sector->entry_count++]; + new_entry = (struct recovery_journal_entry ) { + .mapping = { + .pbn = data_vio->increment_updater.zpbn.pbn, + .state = data_vio->increment_updater.zpbn.state, + }, + .unmapping = { + .pbn = data_vio->decrement_updater.zpbn.pbn, + .state = data_vio->decrement_updater.zpbn.state, + }, + .operation = data_vio->increment_updater.operation, + .slot = lock->tree_slots[lock->height].block_map_slot, + }; + *packed_entry =vdo_pack_recovery_journal_entry (&new_entry); + data_vio->recovery_sequence_number = block->sequence_number; + +/* Enqueue the data_vio to wait for its entry to commit. */ +vdo_enqueue_waiter (&block->commit_waiters, &data_vio->waiter); + } +} + +/** +* write_block() - Issue a block for writing. +* +* Implements waiter_callback_fn. +*/ +static void write_block (struct waiter *waiter,void *context__always_unused ) +{ +struct recovery_journal_block *block = + container_of(waiter, struct recovery_journal_block, write_waiter); +struct recovery_journal *journal = block->journal; +struct packed_journal_header *header =get_block_header (block); + +if (block->committing || !vdo_has_waiters (&block->entry_waiters) ||is_read_only (journal)) +return ; + + block->entries_in_commit =vdo_count_waiters (&block->entry_waiters); +add_queued_recovery_entries (block); + + journal->pending_write_count +=1 ; + journal->events.blocks.written +=1 ; + journal->events.entries.written += block->entries_in_commit; + + header->block_map_head =__cpu_to_le64 (journal->block_map_head); + header->slab_journal_head =__cpu_to_le64 (journal->slab_journal_head); + header->entry_count =__cpu_to_le16 (block->entry_count); + + block->committing = true; + +/* +* We must issue a flush and a FUA for every commit. The flush is necessary to ensure that +* the data being referenced is stable. The FUA is necessary to ensure that the journal +* block itself is stable before allowing overwrites of the lbn's previous data. +*/ +submit_metadata_vio (&block->vio, journal->origin + block->block_number, + complete_write_endio, handle_write_error,WRITE_FLAGS ); +} + + +/** +* write_blocks() - Attempt to commit blocks, according to write policy. +* @journal: The recovery journal. +*/ +static void write_blocks (struct recovery_journal *journal) +{ +assert_on_journal_thread (journal, __func__); +/* +* We call this function after adding entries to the journal and after finishing a block +* write. Thus, when this function terminates we must either have no VIOs waiting in the +* journal or have some outstanding IO to provide a future wakeup. +* +* We want to only issue full blocks if there are no pending writes. However, if there are +* no outstanding writes and some unwritten entries, we must issue a block, even if it's +* the active block and it isn't full. +*/ +if (journal->pending_write_count >0 ) +return ; + +/* Write all the full blocks. */ +vdo_notify_all_waiters (&journal->pending_writes, write_block,NULL ); + +/* +* Do we need to write the active block? Only if we have no outstanding writes, even after +* issuing all of the full writes. +*/ +if ((journal->pending_write_count ==0 ) && (journal->active_block !=NULL )) +write_block (&journal->active_block->write_waiter,NULL ); +} + +/** +* vdo_add_recovery_journal_entry() - Add an entry to a recovery journal. +* @journal: The journal in which to make an entry. +* @data_vio: The data_vio for which to add the entry. The entry will be taken +* from the logical and new_mapped fields of the data_vio. The +* data_vio's recovery_sequence_number field will be set to the +* sequence number of the journal block in which the entry was +* made. +* +* This method is asynchronous. The data_vio will not be called back until the entry is committed +* to the on-disk journal. +*/ +void vdo_add_recovery_journal_entry (struct recovery_journal *journal, +struct data_vio *data_vio) +{ +assert_on_journal_thread (journal, __func__); +if (!vdo_is_state_normal (&journal->state)) { +continue_data_vio_with_error (data_vio,VDO_INVALID_ADMIN_STATE ); +return ; + } + +if (is_read_only (journal)) { +continue_data_vio_with_error (data_vio,VDO_READ_ONLY ); +return ; + } + +ASSERT_LOG_ONLY (data_vio->recovery_sequence_number ==0 , +"journal lock not held for new entry" ); + +vdo_advance_journal_point (&journal->append_point, journal->entries_per_block); +vdo_enqueue_waiter (&journal->entry_waiters, &data_vio->waiter); +assign_entries (journal); +} + +/** +* is_lock_locked() - Check whether a lock is locked for a zone type. +* @journal: The recovery journal. +* @lock_number: The lock to check. +* @zone_type: The type of the zone. +* +* If the recovery journal has a lock on the lock number, both logical and physical zones are +* considered locked. +* +* Return: true if the specified lock has references (is locked). +*/ +static bool is_lock_locked (struct recovery_journal *journal,block_count_t lock_number, +enum vdo_zone_type zone_type) +{ +atomic_t *zone_count; +bool locked; + +if (is_journal_zone_locked (journal, lock_number)) +return true; + + zone_count =get_zone_count_ptr (journal, lock_number, zone_type); + locked = (atomic_read (zone_count) !=0 ); +/* Pairs with implicit barrier in vdo_release_recovery_journal_block_reference() */ +smp_rmb (); +return locked; +} + +/** +* reap_recovery_journal() - Conduct a sweep on a recovery journal to reclaim unreferenced blocks. +* @journal: The recovery journal. +*/ +static void reap_recovery_journal (struct recovery_journal *journal) +{ +if (journal->reaping) { +/* +* We already have an outstanding reap in progress. We need to wait for it to +* finish. +*/ +return ; + } + +if (vdo_is_state_quiescent (&journal->state)) { +/* We are supposed to not do IO. Don't botch it by reaping. */ +return ; + } + +/* +* Start reclaiming blocks only when the journal head has no references. Then stop when a +* block is referenced. +*/ +while ((journal->block_map_reap_head < journal->last_write_acknowledged) && + !is_lock_locked (journal, journal->block_map_head_block_number, +VDO_ZONE_TYPE_LOGICAL )) { + journal->block_map_reap_head++; +if (++journal->block_map_head_block_number == journal->size) + journal->block_map_head_block_number =0 ; + } + +while ((journal->slab_journal_reap_head < journal->last_write_acknowledged) && + !is_lock_locked (journal, journal->slab_journal_head_block_number, +VDO_ZONE_TYPE_PHYSICAL )) { + journal->slab_journal_reap_head++; +if (++journal->slab_journal_head_block_number == journal->size) + journal->slab_journal_head_block_number =0 ; + } + +if ((journal->block_map_reap_head == journal->block_map_head) && + (journal->slab_journal_reap_head == journal->slab_journal_head)) { +/* Nothing happened. */ +return ; + } + +/* +* If the block map head will advance, we must flush any block map page modified by the +* entries we are reaping. If the slab journal head will advance, we must flush the slab +* summary update covering the slab journal that just released some lock. +*/ + journal->reaping = true; +submit_flush_vio (journal->flush_vio, flush_endio, handle_flush_error); +} + +/** +* vdo_acquire_recovery_journal_block_reference() - Acquire a reference to a recovery journal block +* from somewhere other than the journal itself. +* @journal: The recovery journal. +* @sequence_number: The journal sequence number of the referenced block. +* @zone_type: The type of the zone making the adjustment. +* @zone_id: The ID of the zone making the adjustment. +*/ +void vdo_acquire_recovery_journal_block_reference (struct recovery_journal *journal, +sequence_number_t sequence_number, +enum vdo_zone_type zone_type, +zone_count_t zone_id) +{ +block_count_t lock_number; +u16 *current_value; + +if (sequence_number ==0 ) +return ; + +ASSERT_LOG_ONLY ((zone_type !=VDO_ZONE_TYPE_JOURNAL ), +"invalid lock count increment from journal zone" ); + + lock_number =vdo_get_recovery_journal_block_number (journal, sequence_number); + current_value =get_counter (journal, lock_number, zone_type, zone_id); +ASSERT_LOG_ONLY (*current_value <U16_MAX , +"increment of lock counter must not overflow" ); + +if (*current_value ==0 ) { +/* +* This zone is acquiring this lock for the first time. Extra barriers because this +* was original developed using an atomic add operation that implicitly had them. +*/ +smp_mb__before_atomic (); +atomic_inc (get_zone_count_ptr (journal, lock_number, zone_type)); +/* same as before_atomic */ +smp_mb__after_atomic (); + } + + *current_value +=1 ; +} + +/** +* vdo_release_journal_entry_lock() - Release a single per-entry reference count for a recovery +* journal block. +* @journal: The recovery journal. +* @sequence_number: The journal sequence number of the referenced block. +*/ +void vdo_release_journal_entry_lock (struct recovery_journal *journal, +sequence_number_t sequence_number) +{ +block_count_t lock_number; + +if (sequence_number ==0 ) +return ; + + lock_number =vdo_get_recovery_journal_block_number (journal, sequence_number); +/* +* Extra barriers because this was originally developed using an atomic add operation that +* implicitly had them. +*/ +smp_mb__before_atomic (); +atomic_inc (get_decrement_counter (journal, lock_number)); +/* same as before_atomic */ +smp_mb__after_atomic (); +} + +/** +* initiate_drain() - Initiate a drain. +* +* Implements vdo_admin_initiator_fn. +*/ +static void initiate_drain (struct admin_state *state) +{ +check_for_drain_complete (container_of(state ,struct recovery_journal ,state )); +} + +/** +* vdo_drain_recovery_journal() - Drain recovery journal I/O. +* @journal: The journal to drain. +* @operation: The drain operation (suspend or save). +* @parent: The completion to notify once the journal is drained. +* +* All uncommitted entries will be written out. +*/ +void vdo_drain_recovery_journal (struct recovery_journal *journal, +const struct admin_state_code *operation, +struct vdo_completion *parent) +{ +assert_on_journal_thread (journal, __func__); +vdo_start_draining (&journal->state, operation, parent, initiate_drain); +} + +/** +* resume_lock_counter() - Re-allow notifications from a suspended lock counter. +* @counter: The counter. +* +* Return: true if the lock counter was suspended. +*/ +static bool resume_lock_counter (struct lock_counter *counter) +{ +int prior_state; + +/* +* Extra barriers because this was original developed using a CAS operation that implicitly +* had them. +*/ +smp_mb__before_atomic (); + prior_state =atomic_cmpxchg (&counter->state,LOCK_COUNTER_STATE_SUSPENDED , +LOCK_COUNTER_STATE_NOT_NOTIFYING ); +/* same as before_atomic */ +smp_mb__after_atomic (); + +return (prior_state ==LOCK_COUNTER_STATE_SUSPENDED ); +} + +/** +* vdo_resume_recovery_journal() - Resume a recovery journal which has been drained. +* @journal: The journal to resume. +* @parent: The completion to finish once the journal is resumed. +*/ +void vdo_resume_recovery_journal (struct recovery_journal *journal, +struct vdo_completion *parent) +{ +bool saved; + +assert_on_journal_thread (journal, __func__); + saved =vdo_is_state_saved (&journal->state); +vdo_set_completion_result (parent,vdo_resume_if_quiescent (&journal->state)); +if (is_read_only (journal)) { +vdo_continue_completion (parent,VDO_READ_ONLY ); +return ; + } + +if (saved) +initialize_journal_state (journal); + +if (resume_lock_counter (&journal->lock_counter)) { +/* We might have missed a notification. */ +reap_recovery_journal (journal); + } + +vdo_launch_completion (parent); +} + +/** +* vdo_get_recovery_journal_logical_blocks_used() - Get the number of logical blocks in use by the +* VDO. +* @journal: The journal. +* +* Return: The number of logical blocks in use by the VDO. +*/ +block_count_t vdo_get_recovery_journal_logical_blocks_used (const struct recovery_journal *journal) +{ +return journal->logical_blocks_used; +} + +/** +* vdo_get_recovery_journal_statistics() - Get the current statistics from the recovery journal. +* @journal: The recovery journal to query. +* +* Return: A copy of the current statistics for the journal. +*/ +struct recovery_journal_statistics +vdo_get_recovery_journal_statistics (const struct recovery_journal *journal) +{ +return journal->events; +} + +/** +* dump_recovery_block() - Dump the contents of the recovery block to the log. +* @block: The block to dump. +*/ +static void dump_recovery_block (const struct recovery_journal_block *block) +{ +uds_log_info (" sequence number %llu; entries %u; %s; %zu entry waiters; %zu commit waiters" , + (unsigned long long ) block->sequence_number, block->entry_count, + (block->committing ?"committing" :"waiting" ), +vdo_count_waiters (&block->entry_waiters), +vdo_count_waiters (&block->commit_waiters)); +} + +/** +* vdo_dump_recovery_journal_statistics() - Dump some current statistics and other debug info from +* the recovery journal. +* @journal: The recovery journal to dump. +*/ +void vdo_dump_recovery_journal_statistics (const struct recovery_journal *journal) +{ +const struct recovery_journal_block *block; +struct recovery_journal_statistics stats =vdo_get_recovery_journal_statistics (journal); + +uds_log_info ("Recovery Journal" ); +uds_log_info (" block_map_head=%llu slab_journal_head=%llu last_write_acknowledged=%llu tail=%llu block_map_reap_head=%llu slab_journal_reap_head=%llu disk_full=%llu slab_journal_commits_requested=%llu entry_waiters=%zu" , + (unsigned long long ) journal->block_map_head, + (unsigned long long ) journal->slab_journal_head, + (unsigned long long ) journal->last_write_acknowledged, + (unsigned long long ) journal->tail, + (unsigned long long ) journal->block_map_reap_head, + (unsigned long long ) journal->slab_journal_reap_head, + (unsigned long long ) stats.disk_full, + (unsigned long long ) stats.slab_journal_commits_requested, +vdo_count_waiters (&journal->entry_waiters)); +uds_log_info (" entries: started=%llu written=%llu committed=%llu" , + (unsigned long long ) stats.entries.started, + (unsigned long long ) stats.entries.written, + (unsigned long long ) stats.entries.committed); +uds_log_info (" blocks: started=%llu written=%llu committed=%llu" , + (unsigned long long ) stats.blocks.started, + (unsigned long long ) stats.blocks.written, + (unsigned long long ) stats.blocks.committed); + +uds_log_info (" active blocks:" ); +list_for_each_entry (block, &journal->active_tail_blocks, list_node) +dump_recovery_block (block); +}
drivers/md/dm-vdo/recovery-journal.h new file +312 -0
312 lines not on this page · raw message
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 34/40 dm vdo: add the on-disk formats and marshalling of vdo structures
In reply to Matthew SakaiAdd data and methods for marshalling and unmarshalling the persistent metadata.
--- drivers/md/dm-vdo/encodings.c | 1481 +++++++++++++++++++++++++++++++++ drivers/md/dm-vdo/encodings.h | 1299 +++++++++++++++++++++++++++++ 2 files changed, 2780 insertions(+) create mode 100644 drivers/md/dm-vdo/encodings.c create mode 100644 drivers/md/dm-vdo/encodings.h
drivers/md/dm-vdo/encodings.c new file +1481 -0
@@ -0,0 +1,1481 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include "encodings.h" + +#include <linux/log2.h> + +#include "logger.h" +#include "memory-alloc.h" +#include "permassert.h" + +#include "constants.h" +#include "status-codes.h" +#include "types.h" + +struct geometry_block { +char magic_number[VDO_GEOMETRY_MAGIC_NUMBER_SIZE ]; +struct packed_header header; +u32 checksum; +} __packed; + +static const struct header GEOMETRY_BLOCK_HEADER_5_0 = { + .id =VDO_GEOMETRY_BLOCK , + .version = { + .major_version =5 , + .minor_version =0 , + }, +/* +* Note: this size isn't just the payload size following the header, like it is everywhere +* else in VDO. +*/ + .size =sizeof (struct geometry_block ) +sizeof (struct volume_geometry ), +}; + +static const struct header GEOMETRY_BLOCK_HEADER_4_0 = { + .id =VDO_GEOMETRY_BLOCK , + .version = { + .major_version =4 , + .minor_version =0 , + }, +/* +* Note: this size isn't just the payload size following the header, like it is everywhere +* else in VDO. +*/ + .size =sizeof (struct geometry_block ) +sizeof (struct volume_geometry_4_0 ), +}; + +const u8 VDO_GEOMETRY_MAGIC_NUMBER [VDO_GEOMETRY_MAGIC_NUMBER_SIZE +1 ] ="dmvdo001" ; + +enum { +PAGE_HEADER_4_1_SIZE =8 +8 +8 +1 +1 +1 +1 , +}; + +static const struct version_number BLOCK_MAP_4_1 = { + .major_version =4 , + .minor_version =1 , +}; + +const struct header VDO_BLOCK_MAP_HEADER_2_0 = { + .id =VDO_BLOCK_MAP , + .version = { + .major_version =2 , + .minor_version =0 , + }, + .size =sizeof (struct block_map_state_2_0 ), +}; + +const struct header VDO_RECOVERY_JOURNAL_HEADER_7_0 = { + .id =VDO_RECOVERY_JOURNAL , + .version = { + .major_version =7 , + .minor_version =0 , + }, + .size =sizeof (struct recovery_journal_state_7_0 ), +}; + +const struct header VDO_SLAB_DEPOT_HEADER_2_0 = { + .id =VDO_SLAB_DEPOT , + .version = { + .major_version =2 , + .minor_version =0 , + }, + .size =sizeof (struct slab_depot_state_2_0 ), +}; + +const struct header VDO_LAYOUT_HEADER_3_0 = { + .id =VDO_LAYOUT , + .version = { + .major_version =3 , + .minor_version =0 , + }, + .size =sizeof (struct layout_3_0 ) + (sizeof (struct partition_3_0 ) *VDO_PARTITION_COUNT ), +}; + +static const enum partition_id REQUIRED_PARTITIONS [] = { +VDO_BLOCK_MAP_PARTITION , +VDO_SLAB_DEPOT_PARTITION , +VDO_RECOVERY_JOURNAL_PARTITION , +VDO_SLAB_SUMMARY_PARTITION , +}; + +/* +* The current version for the data encoded in the super block. This must be changed any time there +* is a change to encoding of the component data of any VDO component. +*/ +static const struct version_number VDO_COMPONENT_DATA_41_0 = { + .major_version =41 , + .minor_version =0 , +}; + +const struct version_number VDO_VOLUME_VERSION_67_0 = { + .major_version =67 , + .minor_version =0 , +}; + +static const struct header SUPER_BLOCK_HEADER_12_0 = { + .id =VDO_SUPER_BLOCK , + .version = { + .major_version =12 , + .minor_version =0 , + }, + +/* This is the minimum size, if the super block contains no components. */ + .size =VDO_SUPER_BLOCK_FIXED_SIZE -VDO_ENCODED_HEADER_SIZE , +}; + +/** +* validate_version() - Check whether a version matches an expected version. +* @expected_version: The expected version. +* @actual_version: The version being validated. +* @component_name: The name of the component or the calling function (for error logging). +* +* Logs an error describing a mismatch. +* +* Return: VDO_SUCCESS if the versions are the same, +* VDO_UNSUPPORTED_VERSION if the versions don't match. +*/ +static int __must_check validate_version (struct version_number expected_version, +struct version_number actual_version, +const char *component_name) +{ +if (!vdo_are_same_version (expected_version, actual_version)) { +return uds_log_error_strerror (VDO_UNSUPPORTED_VERSION , +"%s version mismatch, expected %d.%d, got %d.%d" , + component_name, + expected_version.major_version, + expected_version.minor_version, + actual_version.major_version, + actual_version.minor_version); + } + +return VDO_SUCCESS ; +} + +/** +* vdo_validate_header() - Check whether a header matches expectations. +* @expected_header: The expected header. +* @actual_header: The header being validated. +* @exact_size: If true, the size fields of the two headers must be the same, otherwise it is +* required that actual_header.size >= expected_header.size. +* @name: The name of the component or the calling function (for error logging). +* +* Logs an error describing the first mismatch found. +* +* Return: VDO_SUCCESS if the header meets expectations, +* VDO_INCORRECT_COMPONENT if the component ids don't match, +* VDO_UNSUPPORTED_VERSION if the versions or sizes don't match. +*/ +int vdo_validate_header (const struct header *expected_header, +const struct header *actual_header,bool exact_size, +const char *name) +{ +int result; + +if (expected_header->id != actual_header->id) { +return uds_log_error_strerror (VDO_INCORRECT_COMPONENT , +"%s ID mismatch, expected %d, got %d" , + name, expected_header->id, + actual_header->id); + } + + result =validate_version (expected_header->version, actual_header->version, + name); +if (result !=VDO_SUCCESS ) +return result; + +if ((expected_header->size > actual_header->size) || + (exact_size && (expected_header->size < actual_header->size))) { +return uds_log_error_strerror (VDO_UNSUPPORTED_VERSION , +"%s size mismatch, expected %zu, got %zu" , + name, expected_header->size, + actual_header->size); + } + +return VDO_SUCCESS ; +} + +static void encode_version_number (u8 *buffer,size_t *offset, +struct version_number version) +{ +struct packed_version_number packed =vdo_pack_version_number (version); + +memcpy (buffer + *offset, &packed,sizeof (packed)); + *offset +=sizeof (packed); +} + +void vdo_encode_header (u8 *buffer,size_t *offset,const struct header *header) +{ +struct packed_header packed =vdo_pack_header (header); + +memcpy (buffer + *offset, &packed,sizeof (packed)); + *offset +=sizeof (packed); +} + +static void decode_version_number (u8 *buffer,size_t *offset, +struct version_number *version) +{ +struct packed_version_number packed; + +memcpy (&packed, buffer + *offset,sizeof (packed)); + *offset +=sizeof (packed); + *version =vdo_unpack_version_number (packed); +} + +void vdo_decode_header (u8 *buffer,size_t *offset,struct header *header) +{ +struct packed_header packed; + +memcpy (&packed, buffer + *offset,sizeof (packed)); + *offset +=sizeof (packed); + + *header =vdo_unpack_header (&packed); +} + +/** +* decode_volume_geometry() - Decode the on-disk representation of a volume geometry from a buffer. +* @buffer: A buffer to decode from. +* @offset: The offset in the buffer at which to decode. +* @geometry: The structure to receive the decoded fields. +* @version: The geometry block version to decode. +*/ +static void decode_volume_geometry (u8 *buffer,size_t *offset, +struct volume_geometry *geometry,u32 version) +{ +u32 unused, mem; +enum volume_region_id id; +nonce_t nonce; +block_count_t bio_offset =0 ; +bool sparse; + +/* This is for backwards compatibility. */ +decode_u32_le (buffer, offset, &unused); + geometry->unused = unused; + +decode_u64_le (buffer, offset, &nonce); + geometry->nonce = nonce; + +memcpy ((unsigned char *) &geometry->uuid, buffer + *offset,sizeof (uuid_t)); + *offset +=sizeof (uuid_t); + +if (version >4 ) +decode_u64_le (buffer, offset, &bio_offset); + geometry->bio_offset = bio_offset; + +for (id =0 ; id <VDO_VOLUME_REGION_COUNT ; id++) { +physical_block_number_t start_block; +enum volume_region_id saved_id; + +decode_u32_le (buffer, offset, &saved_id); +decode_u64_le (buffer, offset, &start_block); + + geometry->regions[id] = (struct volume_region ) { + .id = saved_id, + .start_block = start_block, + }; + } + +decode_u32_le (buffer, offset, &mem); + *offset +=sizeof (u32); + sparse = buffer[(*offset)++]; + + geometry->index_config = (struct index_config ) { + .mem = mem, + .sparse = sparse, + }; +} + +/** +* vdo_parse_geometry_block() - Decode and validate an encoded geometry block. +* @block: The encoded geometry block. +* @geometry: The structure to receive the decoded fields. +*/ +int __must_check vdo_parse_geometry_block (u8 *block,struct volume_geometry *geometry) +{ +u32 checksum, saved_checksum; +struct header header; +size_t offset =0 ; +int result; + +if (memcmp (block,VDO_GEOMETRY_MAGIC_NUMBER ,VDO_GEOMETRY_MAGIC_NUMBER_SIZE ) !=0 ) +return VDO_BAD_MAGIC ; + offset +=VDO_GEOMETRY_MAGIC_NUMBER_SIZE ; + +vdo_decode_header (block, &offset, &header); +if (header.version.major_version <=4 ) { + result =vdo_validate_header (&GEOMETRY_BLOCK_HEADER_4_0 , &header, + true, __func__); + }else { + result =vdo_validate_header (&GEOMETRY_BLOCK_HEADER_5_0 , &header, + true, __func__); + } +if (result !=VDO_SUCCESS ) +return result; + +decode_volume_geometry (block, &offset, geometry, header.version.major_version); + + result =ASSERT (header.size == offset +sizeof (u32), +"should have decoded up to the geometry checksum" ); +if (result !=VDO_SUCCESS ) +return result; + +/* Decode and verify the checksum. */ + checksum =vdo_crc32 (block, offset); +decode_u32_le (block, &offset, &saved_checksum); + +return ((checksum == saved_checksum) ?VDO_SUCCESS :VDO_CHECKSUM_MISMATCH ); +} + +struct block_map_page *vdo_format_block_map_page (void *buffer,nonce_t nonce, +physical_block_number_t pbn, +bool initialized) +{ +struct block_map_page *page = buffer; + +memset (buffer,0 ,VDO_BLOCK_SIZE ); + page->version =vdo_pack_version_number (BLOCK_MAP_4_1 ); + page->header.nonce =__cpu_to_le64 (nonce); + page->header.pbn =__cpu_to_le64 (pbn); + page->header.initialized = initialized; +return page; +} + +enum block_map_page_validity vdo_validate_block_map_page (struct block_map_page *page, +nonce_t nonce, +physical_block_number_t pbn) +{ +BUILD_BUG_ON (sizeof (struct block_map_page_header ) !=PAGE_HEADER_4_1_SIZE ); + +if (!vdo_are_same_version (BLOCK_MAP_4_1 , +vdo_unpack_version_number (page->version)) || + !page->header.initialized || (nonce !=__le64_to_cpu (page->header.nonce))) +return VDO_BLOCK_MAP_PAGE_INVALID ; + +if (pbn !=vdo_get_block_map_page_pbn (page)) +return VDO_BLOCK_MAP_PAGE_BAD ; + +return VDO_BLOCK_MAP_PAGE_VALID ; +} + +static int decode_block_map_state_2_0 (u8 *buffer,size_t *offset, +struct block_map_state_2_0 *state) +{ +size_t initial_offset; +block_count_t flat_page_count, root_count; +physical_block_number_t flat_page_origin, root_origin; +struct header header; +int result; + +vdo_decode_header (buffer, offset, &header); + result =vdo_validate_header (&VDO_BLOCK_MAP_HEADER_2_0 , &header, true, __func__); +if (result !=VDO_SUCCESS ) +return result; + + initial_offset = *offset; + +decode_u64_le (buffer, offset, &flat_page_origin); + result =ASSERT (flat_page_origin ==VDO_BLOCK_MAP_FLAT_PAGE_ORIGIN , +"Flat page origin must be %u (recorded as %llu)" , +VDO_BLOCK_MAP_FLAT_PAGE_ORIGIN , + (unsigned long long ) state->flat_page_origin); +if (result !=UDS_SUCCESS ) +return result; + +decode_u64_le (buffer, offset, &flat_page_count); + result =ASSERT (flat_page_count ==0 , +"Flat page count must be 0 (recorded as %llu)" , + (unsigned long long ) state->flat_page_count); +if (result !=UDS_SUCCESS ) +return result; + +decode_u64_le (buffer, offset, &root_origin); +decode_u64_le (buffer, offset, &root_count); + + result =ASSERT (VDO_BLOCK_MAP_HEADER_2_0 .size == *offset - initial_offset, +"decoded block map component size must match header size" ); +if (result !=VDO_SUCCESS ) +return result; + + *state = (struct block_map_state_2_0 ) { + .flat_page_origin = flat_page_origin, + .flat_page_count = flat_page_count, + .root_origin = root_origin, + .root_count = root_count, + }; + +return VDO_SUCCESS ; +} + +static void encode_block_map_state_2_0 (u8 *buffer,size_t *offset, +struct block_map_state_2_0 state) +{ +size_t initial_offset; + +vdo_encode_header (buffer, offset, &VDO_BLOCK_MAP_HEADER_2_0 ); + + initial_offset = *offset; +encode_u64_le (buffer, offset, state.flat_page_origin); +encode_u64_le (buffer, offset, state.flat_page_count); +encode_u64_le (buffer, offset, state.root_origin); +encode_u64_le (buffer, offset, state.root_count); + +ASSERT_LOG_ONLY (VDO_BLOCK_MAP_HEADER_2_0 .size == *offset - initial_offset, +"encoded block map component size must match header size" ); +} + +/** +* vdo_compute_new_forest_pages() - Compute the number of pages which must be allocated at each +* level in order to grow the forest to a new number of entries. +* @entries: The new number of entries the block map must address. +* +* Return: The total number of non-leaf pages required. +*/ +block_count_t vdo_compute_new_forest_pages (root_count_t root_count, +struct boundary *old_sizes, +block_count_t entries, +struct boundary *new_sizes) +{ +page_count_t leaf_pages =max (vdo_compute_block_map_page_count (entries),1U ); +page_count_t level_size =DIV_ROUND_UP (leaf_pages, root_count); +block_count_t total_pages =0 ; +height_t height; + +for (height =0 ; height <VDO_BLOCK_MAP_TREE_HEIGHT ; height++) { +block_count_t new_pages; + + level_size =DIV_ROUND_UP (level_size,VDO_BLOCK_MAP_ENTRIES_PER_PAGE ); + new_sizes->levels[height] = level_size; + new_pages = level_size; +if (old_sizes !=NULL ) + new_pages -= old_sizes->levels[height]; + total_pages += (new_pages * root_count); + } + +return total_pages; +} + +/** +* encode_recovery_journal_state_7_0() - Encode the state of a recovery journal. +* +* Return: VDO_SUCCESS or an error code. +*/ +static void encode_recovery_journal_state_7_0 (u8 *buffer,size_t *offset, +struct recovery_journal_state_7_0 state) +{ +size_t initial_offset; + +vdo_encode_header (buffer, offset, &VDO_RECOVERY_JOURNAL_HEADER_7_0 ); + + initial_offset = *offset; +encode_u64_le (buffer, offset, state.journal_start); +encode_u64_le (buffer, offset, state.logical_blocks_used); +encode_u64_le (buffer, offset, state.block_map_data_blocks); + +ASSERT_LOG_ONLY (VDO_RECOVERY_JOURNAL_HEADER_7_0 .size == *offset - initial_offset, +"encoded recovery journal component size must match header size" ); +} + +/** +* decode_recovery_journal_state_7_0() - Decode the state of a recovery journal saved in a buffer. +* @buffer: The buffer containing the saved state. +* @state: A pointer to a recovery journal state to hold the result of a successful decode. +* +* Return: VDO_SUCCESS or an error code. +*/ +static int __must_check decode_recovery_journal_state_7_0 (u8 *buffer,size_t *offset, +struct recovery_journal_state_7_0 *state) +{ +struct header header; +int result; +size_t initial_offset; +sequence_number_t journal_start; +block_count_t logical_blocks_used, block_map_data_blocks; + +vdo_decode_header (buffer, offset, &header); + result =vdo_validate_header (&VDO_RECOVERY_JOURNAL_HEADER_7_0 , &header, true, + __func__); +if (result !=VDO_SUCCESS ) +return result; + + initial_offset = *offset; +decode_u64_le (buffer, offset, &journal_start); +decode_u64_le (buffer, offset, &logical_blocks_used); +decode_u64_le (buffer, offset, &block_map_data_blocks); + + result =ASSERT (VDO_RECOVERY_JOURNAL_HEADER_7_0 .size == *offset - initial_offset, +"decoded recovery journal component size must match header size" ); +if (result !=UDS_SUCCESS ) +return result; + + *state = (struct recovery_journal_state_7_0 ) { + .journal_start = journal_start, + .logical_blocks_used = logical_blocks_used, + .block_map_data_blocks = block_map_data_blocks, + }; + +return VDO_SUCCESS ; +} + +/** +* vdo_get_journal_operation_name() - Get the name of a journal operation. +* @operation: The operation to name. +* +* Return: The name of the operation. +*/ +const char *vdo_get_journal_operation_name (enum journal_operation operation) +{ +switch (operation) { +case VDO_JOURNAL_DATA_REMAPPING : +return "data remapping" ; + +case VDO_JOURNAL_BLOCK_MAP_REMAPPING : +return "block map remapping" ; + +default : +return "unknown journal operation" ; + } +} + +/** +* encode_slab_depot_state_2_0() - Encode the state of a slab depot into a buffer. +* +* Return: UDS_SUCCESS or an error. +*/ +static void encode_slab_depot_state_2_0 (u8 *buffer,size_t *offset, +struct slab_depot_state_2_0 state) +{ +size_t initial_offset; + +vdo_encode_header (buffer, offset, &VDO_SLAB_DEPOT_HEADER_2_0 ); + + initial_offset = *offset; +encode_u64_le (buffer, offset, state.slab_config.slab_blocks); +encode_u64_le (buffer, offset, state.slab_config.data_blocks); +encode_u64_le (buffer, offset, state.slab_config.reference_count_blocks); +encode_u64_le (buffer, offset, state.slab_config.slab_journal_blocks); +encode_u64_le (buffer, offset, state.slab_config.slab_journal_flushing_threshold); +encode_u64_le (buffer, offset, state.slab_config.slab_journal_blocking_threshold); +encode_u64_le (buffer, offset, state.slab_config.slab_journal_scrubbing_threshold); +encode_u64_le (buffer, offset, state.first_block); +encode_u64_le (buffer, offset, state.last_block); + buffer[(*offset)++] = state.zone_count; + +ASSERT_LOG_ONLY (VDO_SLAB_DEPOT_HEADER_2_0 .size == *offset - initial_offset, +"encoded block map component size must match header size" ); +} + +/** +* decode_slab_depot_state_2_0() - Decode slab depot component state version 2.0 from a buffer. +* +* Return: UDS_SUCCESS or an error code. +*/ +static int decode_slab_depot_state_2_0 (u8 *buffer,size_t *offset, +struct slab_depot_state_2_0 *state) +{ +struct header header; +int result; +size_t initial_offset; +struct slab_config slab_config; +block_count_t count; +physical_block_number_t first_block, last_block; +zone_count_t zone_count; + +vdo_decode_header (buffer, offset, &header); + result =vdo_validate_header (&VDO_SLAB_DEPOT_HEADER_2_0 , &header, true, + __func__); +if (result !=VDO_SUCCESS ) +return result; + + initial_offset = *offset; +decode_u64_le (buffer, offset, &count); + slab_config.slab_blocks = count; + +decode_u64_le (buffer, offset, &count); + slab_config.data_blocks = count; + +decode_u64_le (buffer, offset, &count); + slab_config.reference_count_blocks = count; + +decode_u64_le (buffer, offset, &count); + slab_config.slab_journal_blocks = count; + +decode_u64_le (buffer, offset, &count); + slab_config.slab_journal_flushing_threshold = count; + +decode_u64_le (buffer, offset, &count); + slab_config.slab_journal_blocking_threshold = count; + +decode_u64_le (buffer, offset, &count); + slab_config.slab_journal_scrubbing_threshold = count; + +decode_u64_le (buffer, offset, &first_block); +decode_u64_le (buffer, offset, &last_block); + zone_count = buffer[(*offset)++]; + + result =ASSERT (VDO_SLAB_DEPOT_HEADER_2_0 .size == *offset - initial_offset, +"decoded slab depot component size must match header size" ); +if (result !=UDS_SUCCESS ) +return result; + + *state = (struct slab_depot_state_2_0 ) { + .slab_config = slab_config, + .first_block = first_block, + .last_block = last_block, + .zone_count = zone_count, + }; + +return VDO_SUCCESS ; +} + +/** +* vdo_configure_slab_depot() - Configure the slab depot. +* @partition: The slab depot partition +* @slab_config: The configuration of a single slab. +* @zone_count: The number of zones the depot will use. +* @state: The state structure to be configured. +* +* Configures the slab_depot for the specified storage capacity, finding the number of data blocks +* that will fit and still leave room for the depot metadata, then return the saved state for that +* configuration. +* +* Return: VDO_SUCCESS or an error code. +*/ +int vdo_configure_slab_depot (const struct partition *partition, +struct slab_config slab_config,zone_count_t zone_count, +struct slab_depot_state_2_0 *state) +{ +block_count_t total_slab_blocks, total_data_blocks; +size_t slab_count; +physical_block_number_t last_block; +block_count_t slab_size = slab_config.slab_blocks; + +uds_log_debug ("slabDepot %s(block_count=%llu, first_block=%llu, slab_size=%llu, zone_count=%u)" , + __func__, (unsigned long long ) partition->count, + (unsigned long long ) partition->offset, + (unsigned long long ) slab_size, zone_count); + +/* We do not allow runt slabs, so we waste up to a slab's worth. */ + slab_count = (partition->count / slab_size); +if (slab_count ==0 ) +return VDO_NO_SPACE ; + +if (slab_count >MAX_VDO_SLABS ) +return VDO_TOO_MANY_SLABS ; + + total_slab_blocks = slab_count * slab_config.slab_blocks; + total_data_blocks = slab_count * slab_config.data_blocks; + last_block = partition->offset + total_slab_blocks; + + *state = (struct slab_depot_state_2_0 ) { + .slab_config = slab_config, + .first_block = partition->offset, + .last_block = last_block, + .zone_count = zone_count, + }; + +uds_log_debug ("slab_depot last_block=%llu, total_data_blocks=%llu, slab_count=%zu, left_over=%llu" , + (unsigned long long ) last_block, + (unsigned long long ) total_data_blocks, slab_count, + (unsigned long long ) (partition->count - (last_block - partition->offset))); + +return VDO_SUCCESS ; +} + +/** +* vdo_configure_slab() - Measure and initialize the configuration to use for each slab. +* @slab_size: The number of blocks per slab. +* @slab_journal_blocks: The number of blocks for the slab journal. +* @slab_config: The slab configuration to initialize. +* +* Return: VDO_SUCCESS or an error code. +*/ +int vdo_configure_slab (block_count_t slab_size,block_count_t slab_journal_blocks, +struct slab_config *slab_config) +{ +block_count_t ref_blocks, meta_blocks, data_blocks; +block_count_t flushing_threshold, remaining, blocking_threshold; +block_count_t minimal_extra_space, scrubbing_threshold; + +if (slab_journal_blocks >= slab_size) +return VDO_BAD_CONFIGURATION ; + +/* +* This calculation should technically be a recurrence, but the total number of metadata +* blocks is currently less than a single block of ref_counts, so we'd gain at most one +* data block in each slab with more iteration. +*/ + ref_blocks =vdo_get_saved_reference_count_size (slab_size - slab_journal_blocks); + meta_blocks = (ref_blocks + slab_journal_blocks); + +/* Make sure test code hasn't configured slabs to be too small. */ +if (meta_blocks >= slab_size) +return VDO_BAD_CONFIGURATION ; + +/* +* If the slab size is very small, assume this must be a unit test and override the number +* of data blocks to be a power of two (wasting blocks in the slab). Many tests need their +* data_blocks fields to be the exact capacity of the configured volume, and that used to +* fall out since they use a power of two for the number of data blocks, the slab size was +* a power of two, and every block in a slab was a data block. +* +* TODO: Try to figure out some way of structuring testParameters and unit tests so this +* hack isn't needed without having to edit several unit tests every time the metadata size +* changes by one block. +*/ + data_blocks = slab_size - meta_blocks; +if ((slab_size <1024 ) && !is_power_of_2 (data_blocks)) + data_blocks = ((block_count_t )1 <<ilog2 (data_blocks)); + +/* +* Configure the slab journal thresholds. The flush threshold is 168 of 224 blocks in +* production, or 3/4ths, so we use this ratio for all sizes. +*/ + flushing_threshold = ((slab_journal_blocks *3 ) +3 ) /4 ; +/* +* The blocking threshold should be far enough from the flushing threshold to not produce +* delays, but far enough from the end of the journal to allow multiple successive recovery +* failures. +*/ + remaining = slab_journal_blocks - flushing_threshold; + blocking_threshold = flushing_threshold + ((remaining *5 ) /7 ); +/* The scrubbing threshold should be at least 2048 entries before the end of the journal. */ + minimal_extra_space =1 + (MAXIMUM_VDO_USER_VIOS /VDO_SLAB_JOURNAL_FULL_ENTRIES_PER_BLOCK ); + scrubbing_threshold = blocking_threshold; +if (slab_journal_blocks > minimal_extra_space) + scrubbing_threshold = slab_journal_blocks - minimal_extra_space; +if (blocking_threshold > scrubbing_threshold) + blocking_threshold = scrubbing_threshold; + + *slab_config = (struct slab_config ) { + .slab_blocks = slab_size, + .data_blocks = data_blocks, + .reference_count_blocks = ref_blocks, + .slab_journal_blocks = slab_journal_blocks, + .slab_journal_flushing_threshold = flushing_threshold, + .slab_journal_blocking_threshold = blocking_threshold, + .slab_journal_scrubbing_threshold = scrubbing_threshold}; +return VDO_SUCCESS ; +} + +/** +* vdo_decode_slab_journal_entry() - Decode a slab journal entry. +* @block: The journal block holding the entry. +* @entry_count: The number of the entry. +* +* Return: The decoded entry. +*/ +struct slab_journal_entry vdo_decode_slab_journal_entry (struct packed_slab_journal_block *block, +journal_entry_count_t entry_count) +{ +struct slab_journal_entry entry = +vdo_unpack_slab_journal_entry (&block->payload.entries[entry_count]); + +if (block->header.has_block_map_increments && + ((block->payload.full_entries.entry_types[entry_count /8 ] & + ((u8 )1 << (entry_count %8 ))) !=0 )) + entry.operation =VDO_JOURNAL_BLOCK_MAP_REMAPPING ; + +return entry; +} + +/** +* allocate_partition() - Allocate a partition and add it to a layout. +* @layout: The layout containing the partition. +* @id: The id of the partition. +* @offset: The offset into the layout at which the partition begins. +* @size: The size of the partition in blocks. +* +* Return: VDO_SUCCESS or an error. +*/ +static int allocate_partition (struct layout *layout,u8 id, +physical_block_number_t offset,block_count_t size) +{ +struct partition *partition; +int result; + + result = uds_allocate(1, struct partition, __func__, &partition); +if (result !=UDS_SUCCESS ) +return result; + + partition->id = id; + partition->offset = offset; + partition->count = size; + partition->next = layout->head; + layout->head = partition; + +return VDO_SUCCESS ; +} + +/** +* make_partition() - Create a new partition from the beginning or end of the unused space in a +* layout. +* @layout: The layout. +* @id: The id of the partition to make. +* @size: The number of blocks to carve out; if 0, all remaining space will be used. +* @beginning: True if the partition should start at the beginning of the unused space. +* +* Return: A success or error code, particularly VDO_NO_SPACE if there are fewer than size blocks +* remaining. +*/ +static int __must_check make_partition (struct layout *layout,enum partition_id id, +block_count_t size,bool beginning) +{ +int result; +physical_block_number_t offset; +block_count_t free_blocks = layout->last_free - layout->first_free; + +if (size ==0 ) { +if (free_blocks ==0 ) +return VDO_NO_SPACE ; + size = free_blocks; + }else if (size > free_blocks) { +return VDO_NO_SPACE ; + } + + result =vdo_get_partition (layout, id,NULL ); +if (result !=VDO_UNKNOWN_PARTITION ) +return VDO_PARTITION_EXISTS ; + + offset = beginning ? layout->first_free : (layout->last_free - size); + + result =allocate_partition (layout, id, offset, size); +if (result !=VDO_SUCCESS ) +return result; + + layout->num_partitions++; +if (beginning) + layout->first_free += size; +else + layout->last_free = layout->last_free - size; + +return VDO_SUCCESS ; +} + +/** +* vdo_initialize_layout() - Lay out the partitions of a vdo. +* @size: The entire size of the vdo. +* @origin: The start of the layout on the underlying storage in blocks. +* @block_map_blocks: The size of the block map partition. +* @journal_blocks: The size of the journal partition. +* @summary_blocks: The size of the slab summary partition. +* @layout: The layout to initialize. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_initialize_layout (block_count_t size,physical_block_number_t offset, +block_count_t block_map_blocks,block_count_t journal_blocks, +block_count_t summary_blocks,struct layout *layout) +{ +int result; +block_count_t necessary_size = + (offset + block_map_blocks + journal_blocks + summary_blocks); + +if (necessary_size > size) +return uds_log_error_strerror (VDO_NO_SPACE , +"Not enough space to make a VDO" ); + + *layout = (struct layout ) { + .start = offset, + .size = size, + .first_free = offset, + .last_free = size, + .num_partitions =0 , + .head =NULL , + }; + + result =make_partition (layout,VDO_BLOCK_MAP_PARTITION , block_map_blocks, true); +if (result !=VDO_SUCCESS ) { +vdo_uninitialize_layout (layout); +return result; + } + + result =make_partition (layout,VDO_SLAB_SUMMARY_PARTITION , summary_blocks, + false); +if (result !=VDO_SUCCESS ) { +vdo_uninitialize_layout (layout); +return result; + } + + result =make_partition (layout,VDO_RECOVERY_JOURNAL_PARTITION , journal_blocks, + false); +if (result !=VDO_SUCCESS ) { +vdo_uninitialize_layout (layout); +return result; + } + + result =make_partition (layout,VDO_SLAB_DEPOT_PARTITION ,0 , true); +if (result !=VDO_SUCCESS ) +vdo_uninitialize_layout (layout); + +return result; +} + +/** +* vdo_uninitialize_layout() - Clean up a layout. +* @layout: The layout to clean up. +* +* All partitions created by this layout become invalid pointers. +*/ +void vdo_uninitialize_layout (struct layout *layout) +{ +while (layout->head !=NULL ) { +struct partition *part = layout->head; + + layout->head = part->next; +uds_free (part); + } + +memset (layout,0 ,sizeof (struct layout )); +} + +/** +* vdo_get_partition() - Get a partition by id. +* @layout: The layout from which to get a partition. +* @id: The id of the partition. +* @partition_ptr: A pointer to hold the partition. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_get_partition (struct layout *layout,enum partition_id id, +struct partition **partition_ptr) +{ +struct partition *partition; + +for (partition = layout->head; partition !=NULL ; partition = partition->next) { +if (partition->id == id) { +if (partition_ptr !=NULL ) + *partition_ptr = partition; +return VDO_SUCCESS ; + } + } + +return VDO_UNKNOWN_PARTITION ; +} + +/** +* vdo_get_known_partition() - Get a partition by id from a validated layout. +* @layout: The layout from which to get a partition. +* @id: The id of the partition. +* +* Return: the partition +*/ +struct partition *vdo_get_known_partition (struct layout *layout,enum partition_id id) +{ +struct partition *partition; +int result =vdo_get_partition (layout, id, &partition); + +ASSERT_LOG_ONLY (result ==VDO_SUCCESS ,"layout has expected partition: %u" , id); + +return partition; +} + +static void encode_layout (u8 *buffer,size_t *offset,const struct layout *layout) +{ +const struct partition *partition; +size_t initial_offset; +struct header header =VDO_LAYOUT_HEADER_3_0 ; + +BUILD_BUG_ON (sizeof (enum partition_id ) !=sizeof (u8)); +ASSERT_LOG_ONLY (layout->num_partitions <=U8_MAX , +"layout partition count must fit in a byte" ); + +vdo_encode_header (buffer, offset, &header); + + initial_offset = *offset; +encode_u64_le (buffer, offset, layout->first_free); +encode_u64_le (buffer, offset, layout->last_free); + buffer[(*offset)++] = layout->num_partitions; + +ASSERT_LOG_ONLY (sizeof (struct layout_3_0 ) == *offset - initial_offset, +"encoded size of a layout header must match structure" ); + +for (partition = layout->head; partition !=NULL ; partition = partition->next) { + buffer[(*offset)++] = partition->id; +encode_u64_le (buffer, offset, partition->offset); +/* This field only exists for backwards compatibility */ +encode_u64_le (buffer, offset,0 ); +encode_u64_le (buffer, offset, partition->count); + } + +ASSERT_LOG_ONLY (header.size == *offset - initial_offset, +"encoded size of a layout must match header size" ); +} + +static int decode_layout (u8 *buffer,size_t *offset,physical_block_number_t start, +block_count_t size,struct layout *layout) +{ +struct header header; +struct layout_3_0 layout_header; +struct partition *partition; +size_t initial_offset; +physical_block_number_t first_free, last_free; +u8 partition_count; +u8 i; +int result; + +vdo_decode_header (buffer, offset, &header); +/* Layout is variable size, so only do a minimum size check here. */ + result =vdo_validate_header (&VDO_LAYOUT_HEADER_3_0 , &header, false, __func__); +if (result !=VDO_SUCCESS ) +return result; + + initial_offset = *offset; +decode_u64_le (buffer, offset, &first_free); +decode_u64_le (buffer, offset, &last_free); + partition_count = buffer[(*offset)++]; + layout_header = (struct layout_3_0 ) { + .first_free = first_free, + .last_free = last_free, + .partition_count = partition_count, + }; + + result =ASSERT (sizeof (struct layout_3_0 ) == *offset - initial_offset, +"decoded size of a layout header must match structure" ); +if (result !=VDO_SUCCESS ) +return result; + + layout->start = start; + layout->size = size; + layout->first_free = layout_header.first_free; + layout->last_free = layout_header.last_free; + layout->num_partitions = layout_header.partition_count; + +if (layout->num_partitions >VDO_PARTITION_COUNT ) { +return uds_log_error_strerror (VDO_UNKNOWN_PARTITION , +"layout has extra partitions" ); + } + +for (i =0 ; i < layout->num_partitions; i++) { +u8 id; +u64 partition_offset, count; + + id = buffer[(*offset)++]; +decode_u64_le (buffer, offset, &partition_offset); + *offset +=sizeof (u64); +decode_u64_le (buffer, offset, &count); + + result =allocate_partition (layout, id, partition_offset, count); +if (result !=VDO_SUCCESS ) { +vdo_uninitialize_layout (layout); +return result; + } + } + +/* Validate that the layout has all (and only) the required partitions */ +for (i =0 ; i <VDO_PARTITION_COUNT ; i++) { + result =vdo_get_partition (layout,REQUIRED_PARTITIONS [i], &partition); +if (result !=VDO_SUCCESS ) { +vdo_uninitialize_layout (layout); +return uds_log_error_strerror (result, +"layout is missing required partition %u" , +REQUIRED_PARTITIONS [i]); + } + + start += partition->count; + } + +if (start != size) { +vdo_uninitialize_layout (layout); +return uds_log_error_strerror (UDS_BAD_STATE , +"partitions do not cover the layout" ); + } + +return VDO_SUCCESS ; +} + +/** +* pack_vdo_config() - Convert a vdo_config to its packed on-disk representation. +* @config: The vdo config to convert. +* +* Return: The platform-independent representation of the config. +*/ +static struct packed_vdo_config pack_vdo_config (struct vdo_config config) +{ +return (struct packed_vdo_config ) { + .logical_blocks =__cpu_to_le64 (config.logical_blocks), + .physical_blocks =__cpu_to_le64 (config.physical_blocks), + .slab_size =__cpu_to_le64 (config.slab_size), + .recovery_journal_size =__cpu_to_le64 (config.recovery_journal_size), + .slab_journal_blocks =__cpu_to_le64 (config.slab_journal_blocks), + }; +} + +/** +* pack_vdo_component() - Convert a vdo_component to its packed on-disk representation. +* @component: The VDO component data to convert. +* +* Return: The platform-independent representation of the component. +*/ +static struct packed_vdo_component_41_0 pack_vdo_component (const struct vdo_component component) +{ +return (struct packed_vdo_component_41_0 ) { + .state =__cpu_to_le32 (component.state), + .complete_recoveries =__cpu_to_le64 (component.complete_recoveries), + .read_only_recoveries =__cpu_to_le64 (component.read_only_recoveries), + .config =pack_vdo_config (component.config), + .nonce =__cpu_to_le64 (component.nonce), + }; +} + +static void encode_vdo_component (u8 *buffer,size_t *offset, +struct vdo_component component) +{ +struct packed_vdo_component_41_0 packed; + +encode_version_number (buffer, offset,VDO_COMPONENT_DATA_41_0 ); + packed =pack_vdo_component (component); +memcpy (buffer + *offset, &packed,sizeof (packed)); + *offset +=sizeof (packed); +} + +/** +* unpack_vdo_config() - Convert a packed_vdo_config to its native in-memory representation. +* @config: The packed vdo config to convert. +* +* Return: The native in-memory representation of the vdo config. +*/ +static struct vdo_config unpack_vdo_config (struct packed_vdo_config config) +{ +return (struct vdo_config ) { + .logical_blocks =__le64_to_cpu (config.logical_blocks), + .physical_blocks =__le64_to_cpu (config.physical_blocks), + .slab_size =__le64_to_cpu (config.slab_size), + .recovery_journal_size =__le64_to_cpu (config.recovery_journal_size), + .slab_journal_blocks =__le64_to_cpu (config.slab_journal_blocks), + }; +} + +/** +* unpack_vdo_component_41_0() - Convert a packed_vdo_component_41_0 to its native in-memory +* representation. +* @component: The packed vdo component data to convert. +* +* Return: The native in-memory representation of the component. +*/ +static struct vdo_component unpack_vdo_component_41_0 (struct packed_vdo_component_41_0 component) +{ +return (struct vdo_component ) { + .state =__le32_to_cpu (component.state), + .complete_recoveries =__le64_to_cpu (component.complete_recoveries), + .read_only_recoveries =__le64_to_cpu (component.read_only_recoveries), + .config =unpack_vdo_config (component.config), + .nonce =__le64_to_cpu (component.nonce), + }; +} + +/** +* vdo_decode_component() - Decode the component data for the vdo itself out of the super block. +* +* Return: VDO_SUCCESS or an error. +*/ +static int decode_vdo_component (u8 *buffer,size_t *offset,struct vdo_component *component) +{ +struct version_number version; +struct packed_vdo_component_41_0 packed; +int result; + +decode_version_number (buffer, offset, &version); + result =validate_version (version,VDO_COMPONENT_DATA_41_0 , +"VDO component data" ); +if (result !=VDO_SUCCESS ) +return result; + +memcpy (&packed, buffer + *offset,sizeof (packed)); + *offset +=sizeof (packed); + *component =unpack_vdo_component_41_0 (packed); +return VDO_SUCCESS ; +} + +/** +* vdo_validate_config() - Validate constraints on a VDO config. +* @config: The VDO config. +* @physical_block_count: The minimum block count of the underlying storage. +* @logical_block_count: The expected logical size of the VDO, or 0 if the logical size may be +* unspecified. +* +* Return: A success or error code. +*/ +int vdo_validate_config (const struct vdo_config *config, +block_count_t physical_block_count, +block_count_t logical_block_count) +{ +struct slab_config slab_config; +int result; + + result =ASSERT (config->slab_size >0 ,"slab size unspecified" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (is_power_of_2 (config->slab_size), +"slab size must be a power of two" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (config->slab_size <= (1 <<MAX_VDO_SLAB_BITS ), +"slab size must be less than or equal to 2^%d" , +MAX_VDO_SLAB_BITS ); +if (result !=VDO_SUCCESS ) +return result; + + result =ASSERT (config->slab_journal_blocks >=MINIMUM_VDO_SLAB_JOURNAL_BLOCKS , +"slab journal size meets minimum size" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (config->slab_journal_blocks <= config->slab_size, +"slab journal size is within expected bound" ); +if (result !=UDS_SUCCESS ) +return result; + + result =vdo_configure_slab (config->slab_size, config->slab_journal_blocks, + &slab_config); +if (result !=VDO_SUCCESS ) +return result; + + result =ASSERT ((slab_config.data_blocks >=1 ), +"slab must be able to hold at least one block" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (config->physical_blocks >0 ,"physical blocks unspecified" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (config->physical_blocks <=MAXIMUM_VDO_PHYSICAL_BLOCKS , +"physical block count %llu exceeds maximum %llu" , + (unsigned long long ) config->physical_blocks, + (unsigned long long )MAXIMUM_VDO_PHYSICAL_BLOCKS ); +if (result !=UDS_SUCCESS ) +return VDO_OUT_OF_RANGE ; + +if (physical_block_count != config->physical_blocks) { +uds_log_error ("A physical size of %llu blocks was specified, not the %llu blocks configured in the vdo super block" , + (unsigned long long ) physical_block_count, + (unsigned long long ) config->physical_blocks); +return VDO_PARAMETER_MISMATCH ; + } + +if (logical_block_count >0 ) { + result =ASSERT ((config->logical_blocks >0 ), +"logical blocks unspecified" ); +if (result !=UDS_SUCCESS ) +return result; + +if (logical_block_count != config->logical_blocks) { +uds_log_error ("A logical size of %llu blocks was specified, but that differs from the %llu blocks configured in the vdo super block" , + (unsigned long long ) logical_block_count, + (unsigned long long ) config->logical_blocks); +return VDO_PARAMETER_MISMATCH ; + } + } + + result =ASSERT (config->logical_blocks <=MAXIMUM_VDO_LOGICAL_BLOCKS , +"logical blocks too large" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (config->recovery_journal_size >0 , +"recovery journal size unspecified" ); +if (result !=UDS_SUCCESS ) +return result; + + result =ASSERT (is_power_of_2 (config->recovery_journal_size), +"recovery journal size must be a power of two" ); +if (result !=UDS_SUCCESS ) +return result; + +return result; +} + +/** +* vdo_destroy_component_states() - Clean up any allocations in a vdo_component_states. +* @states: The component states to destroy. +*/ +void vdo_destroy_component_states (struct vdo_component_states *states) +{ +if (states ==NULL ) +return ; + +vdo_uninitialize_layout (&states->layout); +} + +/** +* decode_components() - Decode the components now that we know the component data is a version we +* understand. +* @buffer: The buffer being decoded. +* @offset: The offset to start decoding from. +* @geometry: The vdo geometry +* @states: An object to hold the successfully decoded state. +* +* Return: VDO_SUCCESS or an error. +*/ +static int __must_check decode_components (u8 *buffer,size_t *offset, +struct volume_geometry *geometry, +struct vdo_component_states *states) +{ +int result; + +decode_vdo_component (buffer, offset, &states->vdo); + + result =decode_layout (buffer, offset,vdo_get_data_region_start (*geometry) +1 , + states->vdo.config.physical_blocks, &states->layout); +if (result !=VDO_SUCCESS ) +return result; + + result =decode_recovery_journal_state_7_0 (buffer, offset, + &states->recovery_journal); +if (result !=VDO_SUCCESS ) +return result; + + result =decode_slab_depot_state_2_0 (buffer, offset, &states->slab_depot); +if (result !=VDO_SUCCESS ) +return result; + + result =decode_block_map_state_2_0 (buffer, offset, &states->block_map); +if (result !=VDO_SUCCESS ) +return result; + +ASSERT_LOG_ONLY (*offset ==VDO_COMPONENT_DATA_OFFSET +VDO_COMPONENT_DATA_SIZE , +"All decoded component data was used" ); +return VDO_SUCCESS ; +} + +/** +* vdo_decode_component_states() - Decode the payload of a super block. +* @buffer: The buffer containing the encoded super block contents. +* @geometry: The vdo geometry +* @states: A pointer to hold the decoded states. +* +* Return: VDO_SUCCESS or an error. +*/ +int vdo_decode_component_states (u8 *buffer,struct volume_geometry *geometry, +struct vdo_component_states *states) +{ +int result; +size_t offset =VDO_COMPONENT_DATA_OFFSET ; + +/* This is for backwards compatibility. */ +decode_u32_le (buffer, &offset, &states->unused); + +/* Check the VDO volume version */ +decode_version_number (buffer, &offset, &states->volume_version); + result =validate_version (VDO_VOLUME_VERSION_67_0 , states->volume_version, +"volume" ); +if (result !=VDO_SUCCESS ) +return result; + + result =decode_components (buffer, &offset, geometry, states); +if (result !=VDO_SUCCESS ) +vdo_uninitialize_layout (&states->layout); + +return result; +} + +/** +* vdo_validate_component_states() - Validate the decoded super block configuration. +* @states: The state decoded from the super block. +* @geometry_nonce: The nonce from the geometry block. +* @physical_size: The minimum block count of the underlying storage. +* @logical_size: The expected logical size of the VDO, or 0 if the logical size may be +* unspecified. +* +* Return: VDO_SUCCESS or an error if the configuration is invalid. +*/ +int vdo_validate_component_states (struct vdo_component_states *states, +nonce_t geometry_nonce,block_count_t physical_size, +block_count_t logical_size) +{ +if (geometry_nonce != states->vdo.nonce) { +return uds_log_error_strerror (VDO_BAD_NONCE , +"Geometry nonce %llu does not match superblock nonce %llu" , + (unsigned long long ) geometry_nonce, + (unsigned long long ) states->vdo.nonce); + } + +return vdo_validate_config (&states->vdo.config, physical_size, logical_size); +} + +/** +* vdo_encode_component_states() - Encode the state of all vdo components in the super block. +*/ +static void vdo_encode_component_states (u8 *buffer,size_t *offset, +const struct vdo_component_states *states) +{ +/* This is for backwards compatibility. */ +encode_u32_le (buffer, offset, states->unused); +encode_version_number (buffer, offset, states->volume_version); +encode_vdo_component (buffer, offset, states->vdo); +encode_layout (buffer, offset, &states->layout); +encode_recovery_journal_state_7_0 (buffer, offset, states->recovery_journal); +encode_slab_depot_state_2_0 (buffer, offset, states->slab_depot); +encode_block_map_state_2_0 (buffer, offset, states->block_map); + +ASSERT_LOG_ONLY (*offset ==VDO_COMPONENT_DATA_OFFSET +VDO_COMPONENT_DATA_SIZE , +"All super block component data was encoded" ); +} + +/** +* vdo_encode_super_block() - Encode a super block into its on-disk representation. +*/ +void vdo_encode_super_block (u8 *buffer,struct vdo_component_states *states) +{ +u32 checksum; +struct header header =SUPER_BLOCK_HEADER_12_0 ; +size_t offset =0 ; + + header.size +=VDO_COMPONENT_DATA_SIZE ; +vdo_encode_header (buffer, &offset, &header); +vdo_encode_component_states (buffer, &offset, states); + + checksum =vdo_crc32 (buffer, offset); +encode_u32_le (buffer, &offset, checksum); + +/* +* Even though the buffer is a full block, to avoid the potential corruption from a torn +* write, the entire encoding must fit in the first sector. +*/ +ASSERT_LOG_ONLY (offset <=VDO_SECTOR_SIZE , +"entire superblock must fit in one sector" ); +} + +/** +* vdo_decode_super_block() - Decode a super block from its on-disk representation. +*/ +int vdo_decode_super_block (u8 *buffer) +{ +struct header header; +int result; +u32 checksum, saved_checksum; +size_t offset =0 ; + +/* Decode and validate the header. */ +vdo_decode_header (buffer, &offset, &header); + result =vdo_validate_header (&SUPER_BLOCK_HEADER_12_0 , &header, false, __func__); +if (result !=VDO_SUCCESS ) +return result; + +if (header.size >VDO_COMPONENT_DATA_SIZE +sizeof (u32)) { +/* +* We can't check release version or checksum until we know the content size, so we +* have to assume a version mismatch on unexpected values. +*/ +return uds_log_error_strerror (VDO_UNSUPPORTED_VERSION , +"super block contents too large: %zu" , + header.size); + } + +/* Skip past the component data for now, to verify the checksum. */ + offset +=VDO_COMPONENT_DATA_SIZE ; + + checksum =vdo_crc32 (buffer, offset); +decode_u32_le (buffer, &offset, &saved_checksum); + + result =ASSERT (offset ==VDO_SUPER_BLOCK_FIXED_SIZE +VDO_COMPONENT_DATA_SIZE , +"must have decoded entire superblock payload" ); +if (result !=VDO_SUCCESS ) +return result; + +return ((checksum != saved_checksum) ?VDO_CHECKSUM_MISMATCH :VDO_SUCCESS ); +}
drivers/md/dm-vdo/encodings.h new file +1299 -0
1,299 lines not on this page · raw message
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 35/40 dm vdo: add statistics reporting
In reply to Matthew SakaiAdd data and methods to report statisics.
--- drivers/md/dm-vdo/message-stats.c | 1215 +++++++++++++++++++++++++++++ drivers/md/dm-vdo/message-stats.h | 13 + drivers/md/dm-vdo/statistics.h | 278 +++++++ 3 files changed, 1506 insertions(+) create mode 100644 drivers/md/dm-vdo/message-stats.c create mode 100644 drivers/md/dm-vdo/message-stats.h create mode 100644 drivers/md/dm-vdo/statistics.h
drivers/md/dm-vdo/message-stats.c new file +1215 -0
@@ -0,0 +1,1215 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include "dedupe.h" +#include "logger.h" +#include "memory-alloc.h" +#include "message-stats.h" +#include "statistics.h" +#include "thread-device.h" +#include "vdo.h" + +static int write_u64 (char *prefix, +u64 value, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int count =scnprintf (*buf, *maxlen,"%s%llu%s" , + prefix ==NULL ?"" : prefix, + value, + suffix ==NULL ?"" : suffix); + *buf += count; + *maxlen -= count; +if (count >= *maxlen) +return VDO_UNEXPECTED_EOF ; +return VDO_SUCCESS ; +} + +static int write_u32 (char *prefix, +u32 value, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int count =scnprintf (*buf, *maxlen,"%s%u%s" , + prefix ==NULL ?"" : prefix, + value, + suffix ==NULL ?"" : suffix); + *buf += count; + *maxlen -= count; +if (count >= *maxlen) +return VDO_UNEXPECTED_EOF ; +return VDO_SUCCESS ; +} + +static int write_block_count_t (char *prefix, +block_count_t value, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int count =scnprintf (*buf, *maxlen,"%s%llu%s" , + prefix ==NULL ?"" : prefix, + value, + suffix ==NULL ?"" : suffix); + *buf += count; + *maxlen -= count; +if (count >= *maxlen) +return VDO_UNEXPECTED_EOF ; +return VDO_SUCCESS ; +} + +static int write_string (char *prefix, +char *value, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int count =scnprintf (*buf, *maxlen,"%s%s%s" , + prefix ==NULL ?"" : prefix, + value, + suffix ==NULL ?"" : suffix); + *buf += count; + *maxlen -= count; +if (count >= *maxlen) +return VDO_UNEXPECTED_EOF ; +return VDO_SUCCESS ; +} + +static int write_bool (char *prefix, +bool value, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int count =scnprintf (*buf, *maxlen,"%s%d%s" , + prefix ==NULL ?"" : prefix, + value, + suffix ==NULL ?"" : suffix); + *buf += count; + *maxlen -= count; +if (count >= *maxlen) +return VDO_UNEXPECTED_EOF ; +return VDO_SUCCESS ; +} + +static int write_u8 (char *prefix, +u8 value, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int count =scnprintf (*buf, *maxlen,"%s%u%s" , + prefix ==NULL ?"" : prefix, + value, + suffix ==NULL ?"" : suffix); + *buf += count; + *maxlen -= count; +if (count >= *maxlen) +return VDO_UNEXPECTED_EOF ; +return VDO_SUCCESS ; +} + +static int write_block_allocator_statistics (char *prefix, +struct block_allocator_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The total number of slabs from which blocks may be allocated */ + result =write_u64 ("slabCount : " , + stats->slab_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The total number of slabs from which blocks have ever been allocated */ + result =write_u64 ("slabsOpened : " , + stats->slabs_opened, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The number of times since loading that a slab has been re-opened */ + result =write_u64 ("slabsReopened : " , + stats->slabs_reopened, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_commit_statistics (char *prefix, +struct commit_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The total number of items on which processing has started */ + result =write_u64 ("started : " , + stats->started, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The total number of items for which a write operation has been issued */ + result =write_u64 ("written : " , + stats->written, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The total number of items for which a write operation has completed */ + result =write_u64 ("committed : " , + stats->committed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_recovery_journal_statistics (char *prefix, +struct recovery_journal_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the on-disk journal was full */ + result =write_u64 ("diskFull : " , + stats->disk_full, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the recovery journal requested slab journal commits. */ + result =write_u64 ("slabJournalCommitsRequested : " , + stats->slab_journal_commits_requested, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Write/Commit totals for individual journal entries */ + result =write_commit_statistics ("entries : " , + &stats->entries, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Write/Commit totals for journal blocks */ + result =write_commit_statistics ("blocks : " , + &stats->blocks, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_packer_statistics (char *prefix, +struct packer_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of compressed data items written since startup */ + result =write_u64 ("compressedFragmentsWritten : " , + stats->compressed_fragments_written, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of blocks containing compressed items written since startup */ + result =write_u64 ("compressedBlocksWritten : " , + stats->compressed_blocks_written, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of VIOs that are pending in the packer */ + result =write_u64 ("compressedFragmentsInPacker : " , + stats->compressed_fragments_in_packer, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_slab_journal_statistics (char *prefix, +struct slab_journal_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the on-disk journal was full */ + result =write_u64 ("diskFullCount : " , + stats->disk_full_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times an entry was added over the flush threshold */ + result =write_u64 ("flushCount : " , + stats->flush_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times an entry was added over the block threshold */ + result =write_u64 ("blockedCount : " , + stats->blocked_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times a tail block was written */ + result =write_u64 ("blocksWritten : " , + stats->blocks_written, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times we had to wait for the tail to write */ + result =write_u64 ("tailBusyCount : " , + stats->tail_busy_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_slab_summary_statistics (char *prefix, +struct slab_summary_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of blocks written */ + result =write_u64 ("blocksWritten : " , + stats->blocks_written, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_ref_counts_statistics (char *prefix, +struct ref_counts_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of reference blocks written */ + result =write_u64 ("blocksWritten : " , + stats->blocks_written, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_block_map_statistics (char *prefix, +struct block_map_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of dirty (resident) pages */ + result =write_u32 ("dirtyPages : " , + stats->dirty_pages, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of clean (resident) pages */ + result =write_u32 ("cleanPages : " , + stats->clean_pages, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of free pages */ + result =write_u32 ("freePages : " , + stats->free_pages, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of pages in failed state */ + result =write_u32 ("failedPages : " , + stats->failed_pages, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of pages incoming */ + result =write_u32 ("incomingPages : " , + stats->incoming_pages, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of pages outgoing */ + result =write_u32 ("outgoingPages : " , + stats->outgoing_pages, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* how many times free page not avail */ + result =write_u32 ("cachePressure : " , + stats->cache_pressure, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of get_vdo_page() calls for read */ + result =write_u64 ("readCount : " , + stats->read_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of get_vdo_page() calls for write */ + result =write_u64 ("writeCount : " , + stats->write_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of times pages failed to read */ + result =write_u64 ("failedReads : " , + stats->failed_reads, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of times pages failed to write */ + result =write_u64 ("failedWrites : " , + stats->failed_writes, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of gets that are reclaimed */ + result =write_u64 ("reclaimed : " , + stats->reclaimed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of gets for outgoing pages */ + result =write_u64 ("readOutgoing : " , + stats->read_outgoing, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of gets that were already there */ + result =write_u64 ("foundInCache : " , + stats->found_in_cache, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of gets requiring discard */ + result =write_u64 ("discardRequired : " , + stats->discard_required, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of gets enqueued for their page */ + result =write_u64 ("waitForPage : " , + stats->wait_for_page, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of gets that have to fetch */ + result =write_u64 ("fetchRequired : " , + stats->fetch_required, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of page fetches */ + result =write_u64 ("pagesLoaded : " , + stats->pages_loaded, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of page saves */ + result =write_u64 ("pagesSaved : " , + stats->pages_saved, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* the number of flushes issued */ + result =write_u64 ("flushCount : " , + stats->flush_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_hash_lock_statistics (char *prefix, +struct hash_lock_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the UDS advice proved correct */ + result =write_u64 ("dedupeAdviceValid : " , + stats->dedupe_advice_valid, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the UDS advice proved incorrect */ + result =write_u64 ("dedupeAdviceStale : " , + stats->dedupe_advice_stale, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of writes with the same data as another in-flight write */ + result =write_u64 ("concurrentDataMatches : " , + stats->concurrent_data_matches, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of writes whose hash collided with an in-flight write */ + result =write_u64 ("concurrentHashCollisions : " , + stats->concurrent_hash_collisions, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Current number of dedupe queries that are in flight */ + result =write_u32 ("currDedupeQueries : " , + stats->curr_dedupe_queries, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_error_statistics (char *prefix, +struct error_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of times VDO got an invalid dedupe advice PBN from UDS */ + result =write_u64 ("invalidAdvicePBNCount : " , + stats->invalid_advice_pbn_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of times a VIO completed with a VDO_NO_SPACE error */ + result =write_u64 ("noSpaceErrorCount : " , + stats->no_space_error_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of times a VIO completed with a VDO_READ_ONLY error */ + result =write_u64 ("readOnlyErrorCount : " , + stats->read_only_error_count, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_bio_stats (char *prefix, +struct bio_stats *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of REQ_OP_READ bios */ + result =write_u64 ("read : " , + stats->read, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of REQ_OP_WRITE bios with data */ + result =write_u64 ("write : " , + stats->write, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ + result =write_u64 ("emptyFlush : " , + stats->empty_flush, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of REQ_OP_DISCARD bios */ + result =write_u64 ("discard : " , + stats->discard, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of bios tagged with REQ_PREFLUSH */ + result =write_u64 ("flush : " , + stats->flush, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of bios tagged with REQ_FUA */ + result =write_u64 ("fua : " , + stats->fua, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_memory_usage (char *prefix, +struct memory_usage *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Tracked bytes currently allocated. */ + result =write_u64 ("bytesUsed : " , + stats->bytes_used, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Maximum tracked bytes allocated. */ + result =write_u64 ("peakBytesUsed : " , + stats->peak_bytes_used, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_index_statistics (char *prefix, +struct index_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of records stored in the index */ + result =write_u64 ("entriesIndexed : " , + stats->entries_indexed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of post calls that found an existing entry */ + result =write_u64 ("postsFound : " , + stats->posts_found, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of post calls that added a new entry */ + result =write_u64 ("postsNotFound : " , + stats->posts_not_found, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of query calls that found an existing entry */ + result =write_u64 ("queriesFound : " , + stats->queries_found, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of query calls that added a new entry */ + result =write_u64 ("queriesNotFound : " , + stats->queries_not_found, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of update calls that found an existing entry */ + result =write_u64 ("updatesFound : " , + stats->updates_found, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of update calls that added a new entry */ + result =write_u64 ("updatesNotFound : " , + stats->updates_not_found, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of entries discarded */ + result =write_u64 ("entriesDiscarded : " , + stats->entries_discarded, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +static int write_vdo_statistics (char *prefix, +struct vdo_statistics *stats, +char *suffix, +char **buf, +unsigned int *maxlen) +{ +int result; + + result =write_string (prefix,"{ " ,NULL , buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_u32 ("version : " , + stats->version, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of blocks used for data */ + result =write_u64 ("dataBlocksUsed : " , + stats->data_blocks_used, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of blocks used for VDO metadata */ + result =write_u64 ("overheadBlocksUsed : " , + stats->overhead_blocks_used, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of logical blocks that are currently mapped to physical blocks */ + result =write_u64 ("logicalBlocksUsed : " , + stats->logical_blocks_used, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of physical blocks */ + result =write_block_count_t ("physicalBlocks : " , + stats->physical_blocks, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* number of logical blocks */ + result =write_block_count_t ("logicalBlocks : " , + stats->logical_blocks, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Size of the block map page cache, in bytes */ + result =write_u64 ("blockMapCacheSize : " , + stats->block_map_cache_size, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The physical block size */ + result =write_u64 ("blockSize : " , + stats->block_size, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the VDO has successfully recovered */ + result =write_u64 ("completeRecoveries : " , + stats->complete_recoveries, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the VDO has recovered from read-only mode */ + result =write_u64 ("readOnlyRecoveries : " , + stats->read_only_recoveries, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* String describing the operating mode of the VDO */ + result =write_string ("mode : " , + stats->mode, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Whether the VDO is in recovery mode */ + result =write_bool ("inRecoveryMode : " , + stats->in_recovery_mode, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* What percentage of recovery mode work has been completed */ + result =write_u8 ("recoveryPercentage : " , + stats->recovery_percentage, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The statistics for the compressed block packer */ + result =write_packer_statistics ("packer : " , + &stats->packer, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Counters for events in the block allocator */ + result =write_block_allocator_statistics ("allocator : " , + &stats->allocator, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Counters for events in the recovery journal */ + result =write_recovery_journal_statistics ("journal : " , + &stats->journal, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The statistics for the slab journals */ + result =write_slab_journal_statistics ("slabJournal : " , + &stats->slab_journal, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The statistics for the slab summary */ + result =write_slab_summary_statistics ("slabSummary : " , + &stats->slab_summary, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The statistics for the reference counts */ + result =write_ref_counts_statistics ("refCounts : " , + &stats->ref_counts, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The statistics for the block map */ + result =write_block_map_statistics ("blockMap : " , + &stats->block_map, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The dedupe statistics from hash locks */ + result =write_hash_lock_statistics ("hashLock : " , + &stats->hash_lock, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Counts of error conditions */ + result =write_error_statistics ("errors : " , + &stats->errors, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The VDO instance */ + result =write_u32 ("instance : " , + stats->instance, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Current number of active VIOs */ + result =write_u32 ("currentVIOsInProgress : " , + stats->current_vios_in_progress, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Maximum number of active VIOs */ + result =write_u32 ("maxVIOs : " , + stats->max_vios, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of times the UDS index was too slow in responding */ + result =write_u64 ("dedupeAdviceTimeouts : " , + stats->dedupe_advice_timeouts, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Number of flush requests submitted to the storage device */ + result =write_u64 ("flushOut : " , + stats->flush_out, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Logical block size */ + result =write_u64 ("logicalBlockSize : " , + stats->logical_block_size, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Bios submitted into VDO from above */ + result =write_bio_stats ("biosIn : " , + &stats->bios_in, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosInPartial : " , + &stats->bios_in_partial, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Bios submitted onward for user data */ + result =write_bio_stats ("biosOut : " , + &stats->bios_out, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Bios submitted onward for metadata */ + result =write_bio_stats ("biosMeta : " , + &stats->bios_meta, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosJournal : " , + &stats->bios_journal, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosPageCache : " , + &stats->bios_page_cache, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosOutCompleted : " , + &stats->bios_out_completed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosMetaCompleted : " , + &stats->bios_meta_completed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosJournalCompleted : " , + &stats->bios_journal_completed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosPageCacheCompleted : " , + &stats->bios_page_cache_completed, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosAcknowledged : " , + &stats->bios_acknowledged, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_bio_stats ("biosAcknowledgedPartial : " , + &stats->bios_acknowledged_partial, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Current number of bios in progress */ + result =write_bio_stats ("biosInProgress : " , + &stats->bios_in_progress, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* Memory usage stats. */ + result =write_memory_usage ("memoryUsage : " , + &stats->memory_usage, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; +/* The statistics for the UDS index */ + result =write_index_statistics ("index : " , + &stats->index, +", " , + buf, + maxlen); +if (result !=VDO_SUCCESS ) +return result; + result =write_string (NULL ,"}" , suffix, buf, maxlen); +if (result !=VDO_SUCCESS ) +return result; +return VDO_SUCCESS ; +} + +int vdo_write_stats (struct vdo *vdo, +char *buf, +unsigned int maxlen) +{ +struct vdo_statistics *stats; +int result; + + result = uds_allocate(1, struct vdo_statistics, __func__, &stats); +if (result !=VDO_SUCCESS ) +return result; + +vdo_fetch_statistics (vdo, stats); + result =write_vdo_statistics (NULL , stats,NULL , &buf, &maxlen); +uds_free (stats); +return result; +}
drivers/md/dm-vdo/message-stats.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright 2023 Red Hat +*/ + +#ifndef VDO_MESSAGE_STATS_H +#define VDO_MESSAGE_STATS_H + +#include "types.h" + +int vdo_write_stats (struct vdo *vdo,char *buf,unsigned int maxlen); + +#endif /* VDO_MESSAGE_STATS_H */
drivers/md/dm-vdo/statistics.h new file +278 -0
278 lines not on this page · raw message
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 36/40 dm vdo: add sysfs support for setting parameters and fetching stats
In reply to Matthew SakaiAdd data and methods setting run time parameters via sysfs, and to make state and statistics information available through sysfs.
--- drivers/md/dm-vdo/pool-sysfs-stats.c | 2063 ++++++++++++++++++++++++++ drivers/md/dm-vdo/pool-sysfs.c | 198 +++ drivers/md/dm-vdo/pool-sysfs.h | 19 + drivers/md/dm-vdo/sysfs.c | 82 + 4 files changed, 2362 insertions(+) create mode 100644 drivers/md/dm-vdo/pool-sysfs-stats.c create mode 100644 drivers/md/dm-vdo/pool-sysfs.c create mode 100644 drivers/md/dm-vdo/pool-sysfs.h create mode 100644 drivers/md/dm-vdo/sysfs.c
drivers/md/dm-vdo/pool-sysfs-stats.c new file +2063 -0
@@ -0,0 +1,2063 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include <linux/mutex.h> + +#include "logger.h" +#include "string-utils.h" + +#include "dedupe.h" +#include "pool-sysfs.h" +#include "statistics.h" +#include "vdo.h" + +struct pool_stats_attribute { +struct attribute attr; +ssize_t (*print)(struct vdo_statistics *stats,char *buf); +}; + +static ssize_t pool_stats_attr_show (struct kobject *directory, +struct attribute *attr, +char *buf) +{ +ssize_t size; +struct pool_stats_attribute *pool_stats_attr = + container_of(attr, struct pool_stats_attribute, attr); + struct vdo *vdo = container_of(directory, struct vdo, stats_directory); + +if (pool_stats_attr->print ==NULL ) +return -EINVAL ; + +mutex_lock (&vdo->stats_mutex); +vdo_fetch_statistics (vdo, &vdo->stats_buffer); + size = pool_stats_attr->mutex_unlock (&vdo->stats_mutex); + +return size; +} + +const struct sysfs_ops vdo_pool_stats_sysfs_ops = { + .show = pool_stats_attr_show, + .store =NULL , +}; + +/* Number of blocks used for data */ +static ssize_t +pool_stats_print_data_blocks_used (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->data_blocks_used); +} + +static struct pool_stats_attribute pool_stats_attr_data_blocks_used = { + .attr = { .name ="data_blocks_used" , .mode =0444 , }, + .print = pool_stats_print_data_blocks_used, +}; + +/* Number of blocks used for VDO metadata */ +static ssize_t +pool_stats_print_overhead_blocks_used (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->overhead_blocks_used); +} + +static struct pool_stats_attribute pool_stats_attr_overhead_blocks_used = { + .attr = { .name ="overhead_blocks_used" , .mode =0444 , }, + .print = pool_stats_print_overhead_blocks_used, +}; + +/* Number of logical blocks that are currently mapped to physical blocks */ +static ssize_t +pool_stats_print_logical_blocks_used (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->logical_blocks_used); +} + +static struct pool_stats_attribute pool_stats_attr_logical_blocks_used = { + .attr = { .name ="logical_blocks_used" , .mode =0444 , }, + .print = pool_stats_print_logical_blocks_used, +}; + +/* number of physical blocks */ +static ssize_t +pool_stats_print_physical_blocks (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->physical_blocks); +} + +static struct pool_stats_attribute pool_stats_attr_physical_blocks = { + .attr = { .name ="physical_blocks" , .mode =0444 , }, + .print = pool_stats_print_physical_blocks, +}; + +/* number of logical blocks */ +static ssize_t +pool_stats_print_logical_blocks (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->logical_blocks); +} + +static struct pool_stats_attribute pool_stats_attr_logical_blocks = { + .attr = { .name ="logical_blocks" , .mode =0444 , }, + .print = pool_stats_print_logical_blocks, +}; + +/* Size of the block map page cache, in bytes */ +static ssize_t +pool_stats_print_block_map_cache_size (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map_cache_size); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_cache_size = { + .attr = { .name ="block_map_cache_size" , .mode =0444 , }, + .print = pool_stats_print_block_map_cache_size, +}; + +/* The physical block size */ +static ssize_t +pool_stats_print_block_size (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_size); +} + +static struct pool_stats_attribute pool_stats_attr_block_size = { + .attr = { .name ="block_size" , .mode =0444 , }, + .print = pool_stats_print_block_size, +}; + +/* Number of times the VDO has successfully recovered */ +static ssize_t +pool_stats_print_complete_recoveries (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->complete_recoveries); +} + +static struct pool_stats_attribute pool_stats_attr_complete_recoveries = { + .attr = { .name ="complete_recoveries" , .mode =0444 , }, + .print = pool_stats_print_complete_recoveries, +}; + +/* Number of times the VDO has recovered from read-only mode */ +static ssize_t +pool_stats_print_read_only_recoveries (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->read_only_recoveries); +} + +static struct pool_stats_attribute pool_stats_attr_read_only_recoveries = { + .attr = { .name ="read_only_recoveries" , .mode =0444 , }, + .print = pool_stats_print_read_only_recoveries, +}; + +/* String describing the operating mode of the VDO */ +static ssize_t +pool_stats_print_mode (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%s\n" , stats->mode); +} + +static struct pool_stats_attribute pool_stats_attr_mode = { + .attr = { .name ="mode" , .mode =0444 , }, + .print = pool_stats_print_mode, +}; + +/* Whether the VDO is in recovery mode */ +static ssize_t +pool_stats_print_in_recovery_mode (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%d\n" , stats->in_recovery_mode); +} + +static struct pool_stats_attribute pool_stats_attr_in_recovery_mode = { + .attr = { .name ="in_recovery_mode" , .mode =0444 , }, + .print = pool_stats_print_in_recovery_mode, +}; + +/* What percentage of recovery mode work has been completed */ +static ssize_t +pool_stats_print_recovery_percentage (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->recovery_percentage); +} + +static struct pool_stats_attribute pool_stats_attr_recovery_percentage = { + .attr = { .name ="recovery_percentage" , .mode =0444 , }, + .print = pool_stats_print_recovery_percentage, +}; + +/* Number of compressed data items written since startup */ +static ssize_t +pool_stats_print_packer_compressed_fragments_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->packer.compressed_fragments_written); +} + +static struct pool_stats_attribute pool_stats_attr_packer_compressed_fragments_written = { + .attr = { .name ="packer_compressed_fragments_written" , .mode =0444 , }, + .print = pool_stats_print_packer_compressed_fragments_written, +}; + +/* Number of blocks containing compressed items written since startup */ +static ssize_t +pool_stats_print_packer_compressed_blocks_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->packer.compressed_blocks_written); +} + +static struct pool_stats_attribute pool_stats_attr_packer_compressed_blocks_written = { + .attr = { .name ="packer_compressed_blocks_written" , .mode =0444 , }, + .print = pool_stats_print_packer_compressed_blocks_written, +}; + +/* Number of VIOs that are pending in the packer */ +static ssize_t +pool_stats_print_packer_compressed_fragments_in_packer (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->packer.compressed_fragments_in_packer); +} + +static struct pool_stats_attribute pool_stats_attr_packer_compressed_fragments_in_packer = { + .attr = { .name ="packer_compressed_fragments_in_packer" , .mode =0444 , }, + .print = pool_stats_print_packer_compressed_fragments_in_packer, +}; + +/* The total number of slabs from which blocks may be allocated */ +static ssize_t +pool_stats_print_allocator_slab_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->allocator.slab_count); +} + +static struct pool_stats_attribute pool_stats_attr_allocator_slab_count = { + .attr = { .name ="allocator_slab_count" , .mode =0444 , }, + .print = pool_stats_print_allocator_slab_count, +}; + +/* The total number of slabs from which blocks have ever been allocated */ +static ssize_t +pool_stats_print_allocator_slabs_opened (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->allocator.slabs_opened); +} + +static struct pool_stats_attribute pool_stats_attr_allocator_slabs_opened = { + .attr = { .name ="allocator_slabs_opened" , .mode =0444 , }, + .print = pool_stats_print_allocator_slabs_opened, +}; + +/* The number of times since loading that a slab has been re-opened */ +static ssize_t +pool_stats_print_allocator_slabs_reopened (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->allocator.slabs_reopened); +} + +static struct pool_stats_attribute pool_stats_attr_allocator_slabs_reopened = { + .attr = { .name ="allocator_slabs_reopened" , .mode =0444 , }, + .print = pool_stats_print_allocator_slabs_reopened, +}; + +/* Number of times the on-disk journal was full */ +static ssize_t +pool_stats_print_journal_disk_full (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.disk_full); +} + +static struct pool_stats_attribute pool_stats_attr_journal_disk_full = { + .attr = { .name ="journal_disk_full" , .mode =0444 , }, + .print = pool_stats_print_journal_disk_full, +}; + +/* Number of times the recovery journal requested slab journal commits. */ +static ssize_t +pool_stats_print_journal_slab_journal_commits_requested (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.slab_journal_commits_requested); +} + +static struct pool_stats_attribute pool_stats_attr_journal_slab_journal_commits_requested = { + .attr = { .name ="journal_slab_journal_commits_requested" , .mode =0444 , }, + .print = pool_stats_print_journal_slab_journal_commits_requested, +}; + +/* The total number of items on which processing has started */ +static ssize_t +pool_stats_print_journal_entries_started (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.entries.started); +} + +static struct pool_stats_attribute pool_stats_attr_journal_entries_started = { + .attr = { .name ="journal_entries_started" , .mode =0444 , }, + .print = pool_stats_print_journal_entries_started, +}; + +/* The total number of items for which a write operation has been issued */ +static ssize_t +pool_stats_print_journal_entries_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.entries.written); +} + +static struct pool_stats_attribute pool_stats_attr_journal_entries_written = { + .attr = { .name ="journal_entries_written" , .mode =0444 , }, + .print = pool_stats_print_journal_entries_written, +}; + +/* The total number of items for which a write operation has completed */ +static ssize_t +pool_stats_print_journal_entries_committed (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.entries.committed); +} + +static struct pool_stats_attribute pool_stats_attr_journal_entries_committed = { + .attr = { .name ="journal_entries_committed" , .mode =0444 , }, + .print = pool_stats_print_journal_entries_committed, +}; + +/* The total number of items on which processing has started */ +static ssize_t +pool_stats_print_journal_blocks_started (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.blocks.started); +} + +static struct pool_stats_attribute pool_stats_attr_journal_blocks_started = { + .attr = { .name ="journal_blocks_started" , .mode =0444 , }, + .print = pool_stats_print_journal_blocks_started, +}; + +/* The total number of items for which a write operation has been issued */ +static ssize_t +pool_stats_print_journal_blocks_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.blocks.written); +} + +static struct pool_stats_attribute pool_stats_attr_journal_blocks_written = { + .attr = { .name ="journal_blocks_written" , .mode =0444 , }, + .print = pool_stats_print_journal_blocks_written, +}; + +/* The total number of items for which a write operation has completed */ +static ssize_t +pool_stats_print_journal_blocks_committed (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->journal.blocks.committed); +} + +static struct pool_stats_attribute pool_stats_attr_journal_blocks_committed = { + .attr = { .name ="journal_blocks_committed" , .mode =0444 , }, + .print = pool_stats_print_journal_blocks_committed, +}; + +/* Number of times the on-disk journal was full */ +static ssize_t +pool_stats_print_slab_journal_disk_full_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->slab_journal.disk_full_count); +} + +static struct pool_stats_attribute pool_stats_attr_slab_journal_disk_full_count = { + .attr = { .name ="slab_journal_disk_full_count" , .mode =0444 , }, + .print = pool_stats_print_slab_journal_disk_full_count, +}; + +/* Number of times an entry was added over the flush threshold */ +static ssize_t +pool_stats_print_slab_journal_flush_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->slab_journal.flush_count); +} + +static struct pool_stats_attribute pool_stats_attr_slab_journal_flush_count = { + .attr = { .name ="slab_journal_flush_count" , .mode =0444 , }, + .print = pool_stats_print_slab_journal_flush_count, +}; + +/* Number of times an entry was added over the block threshold */ +static ssize_t +pool_stats_print_slab_journal_blocked_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->slab_journal.blocked_count); +} + +static struct pool_stats_attribute pool_stats_attr_slab_journal_blocked_count = { + .attr = { .name ="slab_journal_blocked_count" , .mode =0444 , }, + .print = pool_stats_print_slab_journal_blocked_count, +}; + +/* Number of times a tail block was written */ +static ssize_t +pool_stats_print_slab_journal_blocks_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->slab_journal.blocks_written); +} + +static struct pool_stats_attribute pool_stats_attr_slab_journal_blocks_written = { + .attr = { .name ="slab_journal_blocks_written" , .mode =0444 , }, + .print = pool_stats_print_slab_journal_blocks_written, +}; + +/* Number of times we had to wait for the tail to write */ +static ssize_t +pool_stats_print_slab_journal_tail_busy_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->slab_journal.tail_busy_count); +} + +static struct pool_stats_attribute pool_stats_attr_slab_journal_tail_busy_count = { + .attr = { .name ="slab_journal_tail_busy_count" , .mode =0444 , }, + .print = pool_stats_print_slab_journal_tail_busy_count, +}; + +/* Number of blocks written */ +static ssize_t +pool_stats_print_slab_summary_blocks_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->slab_summary.blocks_written); +} + +static struct pool_stats_attribute pool_stats_attr_slab_summary_blocks_written = { + .attr = { .name ="slab_summary_blocks_written" , .mode =0444 , }, + .print = pool_stats_print_slab_summary_blocks_written, +}; + +/* Number of reference blocks written */ +static ssize_t +pool_stats_print_ref_counts_blocks_written (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->ref_counts.blocks_written); +} + +static struct pool_stats_attribute pool_stats_attr_ref_counts_blocks_written = { + .attr = { .name ="ref_counts_blocks_written" , .mode =0444 , }, + .print = pool_stats_print_ref_counts_blocks_written, +}; + +/* number of dirty (resident) pages */ +static ssize_t +pool_stats_print_block_map_dirty_pages (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.dirty_pages); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_dirty_pages = { + .attr = { .name ="block_map_dirty_pages" , .mode =0444 , }, + .print = pool_stats_print_block_map_dirty_pages, +}; + +/* number of clean (resident) pages */ +static ssize_t +pool_stats_print_block_map_clean_pages (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.clean_pages); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_clean_pages = { + .attr = { .name ="block_map_clean_pages" , .mode =0444 , }, + .print = pool_stats_print_block_map_clean_pages, +}; + +/* number of free pages */ +static ssize_t +pool_stats_print_block_map_free_pages (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.free_pages); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_free_pages = { + .attr = { .name ="block_map_free_pages" , .mode =0444 , }, + .print = pool_stats_print_block_map_free_pages, +}; + +/* number of pages in failed state */ +static ssize_t +pool_stats_print_block_map_failed_pages (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.failed_pages); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_failed_pages = { + .attr = { .name ="block_map_failed_pages" , .mode =0444 , }, + .print = pool_stats_print_block_map_failed_pages, +}; + +/* number of pages incoming */ +static ssize_t +pool_stats_print_block_map_incoming_pages (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.incoming_pages); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_incoming_pages = { + .attr = { .name ="block_map_incoming_pages" , .mode =0444 , }, + .print = pool_stats_print_block_map_incoming_pages, +}; + +/* number of pages outgoing */ +static ssize_t +pool_stats_print_block_map_outgoing_pages (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.outgoing_pages); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_outgoing_pages = { + .attr = { .name ="block_map_outgoing_pages" , .mode =0444 , }, + .print = pool_stats_print_block_map_outgoing_pages, +}; + +/* how many times free page not avail */ +static ssize_t +pool_stats_print_block_map_cache_pressure (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->block_map.cache_pressure); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_cache_pressure = { + .attr = { .name ="block_map_cache_pressure" , .mode =0444 , }, + .print = pool_stats_print_block_map_cache_pressure, +}; + +/* number of get_vdo_page() calls for read */ +static ssize_t +pool_stats_print_block_map_read_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.read_count); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_read_count = { + .attr = { .name ="block_map_read_count" , .mode =0444 , }, + .print = pool_stats_print_block_map_read_count, +}; + +/* number of get_vdo_page() calls for write */ +static ssize_t +pool_stats_print_block_map_write_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.write_count); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_write_count = { + .attr = { .name ="block_map_write_count" , .mode =0444 , }, + .print = pool_stats_print_block_map_write_count, +}; + +/* number of times pages failed to read */ +static ssize_t +pool_stats_print_block_map_failed_reads (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.failed_reads); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_failed_reads = { + .attr = { .name ="block_map_failed_reads" , .mode =0444 , }, + .print = pool_stats_print_block_map_failed_reads, +}; + +/* number of times pages failed to write */ +static ssize_t +pool_stats_print_block_map_failed_writes (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.failed_writes); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_failed_writes = { + .attr = { .name ="block_map_failed_writes" , .mode =0444 , }, + .print = pool_stats_print_block_map_failed_writes, +}; + +/* number of gets that are reclaimed */ +static ssize_t +pool_stats_print_block_map_reclaimed (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.reclaimed); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_reclaimed = { + .attr = { .name ="block_map_reclaimed" , .mode =0444 , }, + .print = pool_stats_print_block_map_reclaimed, +}; + +/* number of gets for outgoing pages */ +static ssize_t +pool_stats_print_block_map_read_outgoing (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.read_outgoing); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_read_outgoing = { + .attr = { .name ="block_map_read_outgoing" , .mode =0444 , }, + .print = pool_stats_print_block_map_read_outgoing, +}; + +/* number of gets that were already there */ +static ssize_t +pool_stats_print_block_map_found_in_cache (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.found_in_cache); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_found_in_cache = { + .attr = { .name ="block_map_found_in_cache" , .mode =0444 , }, + .print = pool_stats_print_block_map_found_in_cache, +}; + +/* number of gets requiring discard */ +static ssize_t +pool_stats_print_block_map_discard_required (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.discard_required); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_discard_required = { + .attr = { .name ="block_map_discard_required" , .mode =0444 , }, + .print = pool_stats_print_block_map_discard_required, +}; + +/* number of gets enqueued for their page */ +static ssize_t +pool_stats_print_block_map_wait_for_page (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.wait_for_page); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_wait_for_page = { + .attr = { .name ="block_map_wait_for_page" , .mode =0444 , }, + .print = pool_stats_print_block_map_wait_for_page, +}; + +/* number of gets that have to fetch */ +static ssize_t +pool_stats_print_block_map_fetch_required (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.fetch_required); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_fetch_required = { + .attr = { .name ="block_map_fetch_required" , .mode =0444 , }, + .print = pool_stats_print_block_map_fetch_required, +}; + +/* number of page fetches */ +static ssize_t +pool_stats_print_block_map_pages_loaded (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.pages_loaded); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_pages_loaded = { + .attr = { .name ="block_map_pages_loaded" , .mode =0444 , }, + .print = pool_stats_print_block_map_pages_loaded, +}; + +/* number of page saves */ +static ssize_t +pool_stats_print_block_map_pages_saved (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.pages_saved); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_pages_saved = { + .attr = { .name ="block_map_pages_saved" , .mode =0444 , }, + .print = pool_stats_print_block_map_pages_saved, +}; + +/* the number of flushes issued */ +static ssize_t +pool_stats_print_block_map_flush_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->block_map.flush_count); +} + +static struct pool_stats_attribute pool_stats_attr_block_map_flush_count = { + .attr = { .name ="block_map_flush_count" , .mode =0444 , }, + .print = pool_stats_print_block_map_flush_count, +}; + +/* Number of times the UDS advice proved correct */ +static ssize_t +pool_stats_print_hash_lock_dedupe_advice_valid (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->hash_lock.dedupe_advice_valid); +} + +static struct pool_stats_attribute pool_stats_attr_hash_lock_dedupe_advice_valid = { + .attr = { .name ="hash_lock_dedupe_advice_valid" , .mode =0444 , }, + .print = pool_stats_print_hash_lock_dedupe_advice_valid, +}; + +/* Number of times the UDS advice proved incorrect */ +static ssize_t +pool_stats_print_hash_lock_dedupe_advice_stale (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->hash_lock.dedupe_advice_stale); +} + +static struct pool_stats_attribute pool_stats_attr_hash_lock_dedupe_advice_stale = { + .attr = { .name ="hash_lock_dedupe_advice_stale" , .mode =0444 , }, + .print = pool_stats_print_hash_lock_dedupe_advice_stale, +}; + +/* Number of writes with the same data as another in-flight write */ +static ssize_t +pool_stats_print_hash_lock_concurrent_data_matches (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->hash_lock.concurrent_data_matches); +} + +static struct pool_stats_attribute pool_stats_attr_hash_lock_concurrent_data_matches = { + .attr = { .name ="hash_lock_concurrent_data_matches" , .mode =0444 , }, + .print = pool_stats_print_hash_lock_concurrent_data_matches, +}; + +/* Number of writes whose hash collided with an in-flight write */ +static ssize_t +pool_stats_print_hash_lock_concurrent_hash_collisions (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->hash_lock.concurrent_hash_collisions); +} + +static struct pool_stats_attribute pool_stats_attr_hash_lock_concurrent_hash_collisions = { + .attr = { .name ="hash_lock_concurrent_hash_collisions" , .mode =0444 , }, + .print = pool_stats_print_hash_lock_concurrent_hash_collisions, +}; + +/* Current number of dedupe queries that are in flight */ +static ssize_t +pool_stats_print_hash_lock_curr_dedupe_queries (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->hash_lock.curr_dedupe_queries); +} + +static struct pool_stats_attribute pool_stats_attr_hash_lock_curr_dedupe_queries = { + .attr = { .name ="hash_lock_curr_dedupe_queries" , .mode =0444 , }, + .print = pool_stats_print_hash_lock_curr_dedupe_queries, +}; + +/* number of times VDO got an invalid dedupe advice PBN from UDS */ +static ssize_t +pool_stats_print_errors_invalid_advice_pbn_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->errors.invalid_advice_pbn_count); +} + +static struct pool_stats_attribute pool_stats_attr_errors_invalid_advice_pbn_count = { + .attr = { .name ="errors_invalid_advice_pbn_count" , .mode =0444 , }, + .print = pool_stats_print_errors_invalid_advice_pbn_count, +}; + +/* number of times a VIO completed with a VDO_NO_SPACE error */ +static ssize_t +pool_stats_print_errors_no_space_error_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->errors.no_space_error_count); +} + +static struct pool_stats_attribute pool_stats_attr_errors_no_space_error_count = { + .attr = { .name ="errors_no_space_error_count" , .mode =0444 , }, + .print = pool_stats_print_errors_no_space_error_count, +}; + +/* number of times a VIO completed with a VDO_READ_ONLY error */ +static ssize_t +pool_stats_print_errors_read_only_error_count (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->errors.read_only_error_count); +} + +static struct pool_stats_attribute pool_stats_attr_errors_read_only_error_count = { + .attr = { .name ="errors_read_only_error_count" , .mode =0444 , }, + .print = pool_stats_print_errors_read_only_error_count, +}; + +/* The VDO instance */ +static ssize_t +pool_stats_print_instance (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->instance); +} + +static struct pool_stats_attribute pool_stats_attr_instance = { + .attr = { .name ="instance" , .mode =0444 , }, + .print = pool_stats_print_instance, +}; + +/* Current number of active VIOs */ +static ssize_t +pool_stats_print_current_vios_in_progress (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->current_vios_in_progress); +} + +static struct pool_stats_attribute pool_stats_attr_current_vios_in_progress = { + .attr = { .name ="current_vios_in_progress" , .mode =0444 , }, + .print = pool_stats_print_current_vios_in_progress, +}; + +/* Maximum number of active VIOs */ +static ssize_t +pool_stats_print_max_vios (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%u\n" , stats->max_vios); +} + +static struct pool_stats_attribute pool_stats_attr_max_vios = { + .attr = { .name ="max_vios" , .mode =0444 , }, + .print = pool_stats_print_max_vios, +}; + +/* Number of times the UDS index was too slow in responding */ +static ssize_t +pool_stats_print_dedupe_advice_timeouts (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->dedupe_advice_timeouts); +} + +static struct pool_stats_attribute pool_stats_attr_dedupe_advice_timeouts = { + .attr = { .name ="dedupe_advice_timeouts" , .mode =0444 , }, + .print = pool_stats_print_dedupe_advice_timeouts, +}; + +/* Number of flush requests submitted to the storage device */ +static ssize_t +pool_stats_print_flush_out (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->flush_out); +} + +static struct pool_stats_attribute pool_stats_attr_flush_out = { + .attr = { .name ="flush_out" , .mode =0444 , }, + .print = pool_stats_print_flush_out, +}; + +/* Logical block size */ +static ssize_t +pool_stats_print_logical_block_size (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->logical_block_size); +} + +static struct pool_stats_attribute pool_stats_attr_logical_block_size = { + .attr = { .name ="logical_block_size" , .mode =0444 , }, + .print = pool_stats_print_logical_block_size, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_in_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_read = { + .attr = { .name ="bios_in_read" , .mode =0444 , }, + .print = pool_stats_print_bios_in_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_in_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_write = { + .attr = { .name ="bios_in_write" , .mode =0444 , }, + .print = pool_stats_print_bios_in_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_in_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_empty_flush = { + .attr = { .name ="bios_in_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_in_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_in_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_discard = { + .attr = { .name ="bios_in_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_in_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_in_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_flush = { + .attr = { .name ="bios_in_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_in_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_in_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_fua = { + .attr = { .name ="bios_in_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_in_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_in_partial_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_partial.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_partial_read = { + .attr = { .name ="bios_in_partial_read" , .mode =0444 , }, + .print = pool_stats_print_bios_in_partial_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_in_partial_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_partial.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_partial_write = { + .attr = { .name ="bios_in_partial_write" , .mode =0444 , }, + .print = pool_stats_print_bios_in_partial_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_in_partial_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_partial.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_partial_empty_flush = { + .attr = { .name ="bios_in_partial_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_in_partial_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_in_partial_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_partial.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_partial_discard = { + .attr = { .name ="bios_in_partial_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_in_partial_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_in_partial_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_partial.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_partial_flush = { + .attr = { .name ="bios_in_partial_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_in_partial_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_in_partial_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_partial.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_partial_fua = { + .attr = { .name ="bios_in_partial_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_in_partial_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_out_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_read = { + .attr = { .name ="bios_out_read" , .mode =0444 , }, + .print = pool_stats_print_bios_out_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_out_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_write = { + .attr = { .name ="bios_out_write" , .mode =0444 , }, + .print = pool_stats_print_bios_out_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_out_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_empty_flush = { + .attr = { .name ="bios_out_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_out_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_out_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_discard = { + .attr = { .name ="bios_out_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_out_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_out_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_flush = { + .attr = { .name ="bios_out_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_out_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_out_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_fua = { + .attr = { .name ="bios_out_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_out_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_meta_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_read = { + .attr = { .name ="bios_meta_read" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_meta_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_write = { + .attr = { .name ="bios_meta_write" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_meta_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_empty_flush = { + .attr = { .name ="bios_meta_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_meta_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_discard = { + .attr = { .name ="bios_meta_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_meta_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_flush = { + .attr = { .name ="bios_meta_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_meta_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_fua = { + .attr = { .name ="bios_meta_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_journal_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_read = { + .attr = { .name ="bios_journal_read" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_journal_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_write = { + .attr = { .name ="bios_journal_write" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_journal_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_empty_flush = { + .attr = { .name ="bios_journal_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_journal_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_discard = { + .attr = { .name ="bios_journal_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_journal_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_flush = { + .attr = { .name ="bios_journal_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_journal_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_fua = { + .attr = { .name ="bios_journal_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_page_cache_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_read = { + .attr = { .name ="bios_page_cache_read" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_page_cache_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_write = { + .attr = { .name ="bios_page_cache_write" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_page_cache_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_empty_flush = { + .attr = { .name ="bios_page_cache_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_page_cache_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_discard = { + .attr = { .name ="bios_page_cache_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_page_cache_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_flush = { + .attr = { .name ="bios_page_cache_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_page_cache_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_fua = { + .attr = { .name ="bios_page_cache_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_out_completed_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out_completed.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_completed_read = { + .attr = { .name ="bios_out_completed_read" , .mode =0444 , }, + .print = pool_stats_print_bios_out_completed_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_out_completed_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out_completed.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_completed_write = { + .attr = { .name ="bios_out_completed_write" , .mode =0444 , }, + .print = pool_stats_print_bios_out_completed_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_out_completed_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out_completed.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_completed_empty_flush = { + .attr = { .name ="bios_out_completed_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_out_completed_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_out_completed_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out_completed.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_completed_discard = { + .attr = { .name ="bios_out_completed_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_out_completed_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_out_completed_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out_completed.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_completed_flush = { + .attr = { .name ="bios_out_completed_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_out_completed_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_out_completed_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_out_completed.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_out_completed_fua = { + .attr = { .name ="bios_out_completed_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_out_completed_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_meta_completed_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta_completed.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_completed_read = { + .attr = { .name ="bios_meta_completed_read" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_completed_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_meta_completed_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta_completed.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_completed_write = { + .attr = { .name ="bios_meta_completed_write" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_completed_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_meta_completed_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta_completed.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_completed_empty_flush = { + .attr = { .name ="bios_meta_completed_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_completed_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_meta_completed_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta_completed.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_completed_discard = { + .attr = { .name ="bios_meta_completed_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_completed_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_meta_completed_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta_completed.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_completed_flush = { + .attr = { .name ="bios_meta_completed_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_completed_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_meta_completed_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_meta_completed.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_meta_completed_fua = { + .attr = { .name ="bios_meta_completed_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_meta_completed_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_journal_completed_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal_completed.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_completed_read = { + .attr = { .name ="bios_journal_completed_read" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_completed_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_journal_completed_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal_completed.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_completed_write = { + .attr = { .name ="bios_journal_completed_write" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_completed_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_journal_completed_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal_completed.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_completed_empty_flush = { + .attr = { .name ="bios_journal_completed_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_completed_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_journal_completed_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal_completed.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_completed_discard = { + .attr = { .name ="bios_journal_completed_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_completed_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_journal_completed_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal_completed.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_completed_flush = { + .attr = { .name ="bios_journal_completed_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_completed_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_journal_completed_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_journal_completed.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_journal_completed_fua = { + .attr = { .name ="bios_journal_completed_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_journal_completed_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_page_cache_completed_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache_completed.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_completed_read = { + .attr = { .name ="bios_page_cache_completed_read" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_completed_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_page_cache_completed_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache_completed.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_completed_write = { + .attr = { .name ="bios_page_cache_completed_write" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_completed_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_page_cache_completed_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache_completed.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_completed_empty_flush = { + .attr = { .name ="bios_page_cache_completed_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_completed_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_page_cache_completed_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache_completed.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_completed_discard = { + .attr = { .name ="bios_page_cache_completed_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_completed_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_page_cache_completed_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache_completed.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_completed_flush = { + .attr = { .name ="bios_page_cache_completed_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_completed_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_page_cache_completed_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_page_cache_completed.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_page_cache_completed_fua = { + .attr = { .name ="bios_page_cache_completed_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_page_cache_completed_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_acknowledged_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_read = { + .attr = { .name ="bios_acknowledged_read" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_acknowledged_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_write = { + .attr = { .name ="bios_acknowledged_write" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_acknowledged_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_empty_flush = { + .attr = { .name ="bios_acknowledged_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_acknowledged_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_discard = { + .attr = { .name ="bios_acknowledged_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_acknowledged_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_flush = { + .attr = { .name ="bios_acknowledged_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_acknowledged_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_fua = { + .attr = { .name ="bios_acknowledged_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_acknowledged_partial_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged_partial.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_partial_read = { + .attr = { .name ="bios_acknowledged_partial_read" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_partial_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_acknowledged_partial_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged_partial.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_partial_write = { + .attr = { .name ="bios_acknowledged_partial_write" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_partial_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_acknowledged_partial_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged_partial.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_partial_empty_flush = { + .attr = { .name ="bios_acknowledged_partial_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_partial_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_acknowledged_partial_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged_partial.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_partial_discard = { + .attr = { .name ="bios_acknowledged_partial_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_partial_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_acknowledged_partial_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged_partial.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_partial_flush = { + .attr = { .name ="bios_acknowledged_partial_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_partial_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_acknowledged_partial_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_acknowledged_partial.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_acknowledged_partial_fua = { + .attr = { .name ="bios_acknowledged_partial_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_acknowledged_partial_fua, +}; + +/* Number of REQ_OP_READ bios */ +static ssize_t +pool_stats_print_bios_in_progress_read (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_progress.read); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_progress_read = { + .attr = { .name ="bios_in_progress_read" , .mode =0444 , }, + .print = pool_stats_print_bios_in_progress_read, +}; + +/* Number of REQ_OP_WRITE bios with data */ +static ssize_t +pool_stats_print_bios_in_progress_write (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_progress.write); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_progress_write = { + .attr = { .name ="bios_in_progress_write" , .mode =0444 , }, + .print = pool_stats_print_bios_in_progress_write, +}; + +/* Number of bios tagged with REQ_PREFLUSH and containing no data */ +static ssize_t +pool_stats_print_bios_in_progress_empty_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_progress.empty_flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_progress_empty_flush = { + .attr = { .name ="bios_in_progress_empty_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_in_progress_empty_flush, +}; + +/* Number of REQ_OP_DISCARD bios */ +static ssize_t +pool_stats_print_bios_in_progress_discard (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_progress.discard); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_progress_discard = { + .attr = { .name ="bios_in_progress_discard" , .mode =0444 , }, + .print = pool_stats_print_bios_in_progress_discard, +}; + +/* Number of bios tagged with REQ_PREFLUSH */ +static ssize_t +pool_stats_print_bios_in_progress_flush (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_progress.flush); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_progress_flush = { + .attr = { .name ="bios_in_progress_flush" , .mode =0444 , }, + .print = pool_stats_print_bios_in_progress_flush, +}; + +/* Number of bios tagged with REQ_FUA */ +static ssize_t +pool_stats_print_bios_in_progress_fua (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->bios_in_progress.fua); +} + +static struct pool_stats_attribute pool_stats_attr_bios_in_progress_fua = { + .attr = { .name ="bios_in_progress_fua" , .mode =0444 , }, + .print = pool_stats_print_bios_in_progress_fua, +}; + +/* Tracked bytes currently allocated. */ +static ssize_t +pool_stats_print_memory_usage_bytes_used (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->memory_usage.bytes_used); +} + +static struct pool_stats_attribute pool_stats_attr_memory_usage_bytes_used = { + .attr = { .name ="memory_usage_bytes_used" , .mode =0444 , }, + .print = pool_stats_print_memory_usage_bytes_used, +}; + +/* Maximum tracked bytes allocated. */ +static ssize_t +pool_stats_print_memory_usage_peak_bytes_used (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->memory_usage.peak_bytes_used); +} + +static struct pool_stats_attribute pool_stats_attr_memory_usage_peak_bytes_used = { + .attr = { .name ="memory_usage_peak_bytes_used" , .mode =0444 , }, + .print = pool_stats_print_memory_usage_peak_bytes_used, +}; + +/* Number of records stored in the index */ +static ssize_t +pool_stats_print_index_entries_indexed (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.entries_indexed); +} + +static struct pool_stats_attribute pool_stats_attr_index_entries_indexed = { + .attr = { .name ="index_entries_indexed" , .mode =0444 , }, + .print = pool_stats_print_index_entries_indexed, +}; + +/* Number of post calls that found an existing entry */ +static ssize_t +pool_stats_print_index_posts_found (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.posts_found); +} + +static struct pool_stats_attribute pool_stats_attr_index_posts_found = { + .attr = { .name ="index_posts_found" , .mode =0444 , }, + .print = pool_stats_print_index_posts_found, +}; + +/* Number of post calls that added a new entry */ +static ssize_t +pool_stats_print_index_posts_not_found (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.posts_not_found); +} + +static struct pool_stats_attribute pool_stats_attr_index_posts_not_found = { + .attr = { .name ="index_posts_not_found" , .mode =0444 , }, + .print = pool_stats_print_index_posts_not_found, +}; + +/* Number of query calls that found an existing entry */ +static ssize_t +pool_stats_print_index_queries_found (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.queries_found); +} + +static struct pool_stats_attribute pool_stats_attr_index_queries_found = { + .attr = { .name ="index_queries_found" , .mode =0444 , }, + .print = pool_stats_print_index_queries_found, +}; + +/* Number of query calls that added a new entry */ +static ssize_t +pool_stats_print_index_queries_not_found (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.queries_not_found); +} + +static struct pool_stats_attribute pool_stats_attr_index_queries_not_found = { + .attr = { .name ="index_queries_not_found" , .mode =0444 , }, + .print = pool_stats_print_index_queries_not_found, +}; + +/* Number of update calls that found an existing entry */ +static ssize_t +pool_stats_print_index_updates_found (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.updates_found); +} + +static struct pool_stats_attribute pool_stats_attr_index_updates_found = { + .attr = { .name ="index_updates_found" , .mode =0444 , }, + .print = pool_stats_print_index_updates_found, +}; + +/* Number of update calls that added a new entry */ +static ssize_t +pool_stats_print_index_updates_not_found (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.updates_not_found); +} + +static struct pool_stats_attribute pool_stats_attr_index_updates_not_found = { + .attr = { .name ="index_updates_not_found" , .mode =0444 , }, + .print = pool_stats_print_index_updates_not_found, +}; + +/* Number of entries discarded */ +static ssize_t +pool_stats_print_index_entries_discarded (struct vdo_statistics *stats,char *buf) +{ +return sprintf (buf,"%llu\n" , stats->index.entries_discarded); +} + +static struct pool_stats_attribute pool_stats_attr_index_entries_discarded = { + .attr = { .name ="index_entries_discarded" , .mode =0444 , }, + .print = pool_stats_print_index_entries_discarded, +}; + +struct attribute *vdo_pool_stats_attrs[] = { + &pool_stats_attr_data_blocks_used.attr, + &pool_stats_attr_overhead_blocks_used.attr, + &pool_stats_attr_logical_blocks_used.attr, + &pool_stats_attr_physical_blocks.attr, + &pool_stats_attr_logical_blocks.attr, + &pool_stats_attr_block_map_cache_size.attr, + &pool_stats_attr_block_size.attr, + &pool_stats_attr_complete_recoveries.attr, + &pool_stats_attr_read_only_recoveries.attr, + &pool_stats_attr_mode.attr, + &pool_stats_attr_in_recovery_mode.attr, + &pool_stats_attr_recovery_percentage.attr, + &pool_stats_attr_packer_compressed_fragments_written.attr, + &pool_stats_attr_packer_compressed_blocks_written.attr, + &pool_stats_attr_packer_compressed_fragments_in_packer.attr, + &pool_stats_attr_allocator_slab_count.attr, + &pool_stats_attr_allocator_slabs_opened.attr, + &pool_stats_attr_allocator_slabs_reopened.attr, + &pool_stats_attr_journal_disk_full.attr, + &pool_stats_attr_journal_slab_journal_commits_requested.attr, + &pool_stats_attr_journal_entries_started.attr, + &pool_stats_attr_journal_entries_written.attr, + &pool_stats_attr_journal_entries_committed.attr, + &pool_stats_attr_journal_blocks_started.attr, + &pool_stats_attr_journal_blocks_written.attr, + &pool_stats_attr_journal_blocks_committed.attr, + &pool_stats_attr_slab_journal_disk_full_count.attr, + &pool_stats_attr_slab_journal_flush_count.attr, + &pool_stats_attr_slab_journal_blocked_count.attr, + &pool_stats_attr_slab_journal_blocks_written.attr, + &pool_stats_attr_slab_journal_tail_busy_count.attr, + &pool_stats_attr_slab_summary_blocks_written.attr, + &pool_stats_attr_ref_counts_blocks_written.attr, + &pool_stats_attr_block_map_dirty_pages.attr, + &pool_stats_attr_block_map_clean_pages.attr, + &pool_stats_attr_block_map_free_pages.attr, + &pool_stats_attr_block_map_failed_pages.attr, + &pool_stats_attr_block_map_incoming_pages.attr, + &pool_stats_attr_block_map_outgoing_pages.attr, + &pool_stats_attr_block_map_cache_pressure.attr, + &pool_stats_attr_block_map_read_count.attr, + &pool_stats_attr_block_map_write_count.attr, + &pool_stats_attr_block_map_failed_reads.attr, + &pool_stats_attr_block_map_failed_writes.attr, + &pool_stats_attr_block_map_reclaimed.attr, + &pool_stats_attr_block_map_read_outgoing.attr, + &pool_stats_attr_block_map_found_in_cache.attr, + &pool_stats_attr_block_map_discard_required.attr, + &pool_stats_attr_block_map_wait_for_page.attr, + &pool_stats_attr_block_map_fetch_required.attr, + &pool_stats_attr_block_map_pages_loaded.attr, + &pool_stats_attr_block_map_pages_saved.attr, + &pool_stats_attr_block_map_flush_count.attr, + &pool_stats_attr_hash_lock_dedupe_advice_valid.attr, + &pool_stats_attr_hash_lock_dedupe_advice_stale.attr, + &pool_stats_attr_hash_lock_concurrent_data_matches.attr, + &pool_stats_attr_hash_lock_concurrent_hash_collisions.attr, + &pool_stats_attr_hash_lock_curr_dedupe_queries.attr, + &pool_stats_attr_errors_invalid_advice_pbn_count.attr, + &pool_stats_attr_errors_no_space_error_count.attr, + &pool_stats_attr_errors_read_only_error_count.attr, + &pool_stats_attr_instance.attr, + &pool_stats_attr_current_vios_in_progress.attr, + &pool_stats_attr_max_vios.attr, + &pool_stats_attr_dedupe_advice_timeouts.attr, + &pool_stats_attr_flush_out.attr, + &pool_stats_attr_logical_block_size.attr, + &pool_stats_attr_bios_in_read.attr, + &pool_stats_attr_bios_in_write.attr, + &pool_stats_attr_bios_in_empty_flush.attr, + &pool_stats_attr_bios_in_discard.attr, + &pool_stats_attr_bios_in_flush.attr, + &pool_stats_attr_bios_in_fua.attr, + &pool_stats_attr_bios_in_partial_read.attr, + &pool_stats_attr_bios_in_partial_write.attr, + &pool_stats_attr_bios_in_partial_empty_flush.attr, + &pool_stats_attr_bios_in_partial_discard.attr, + &pool_stats_attr_bios_in_partial_flush.attr, + &pool_stats_attr_bios_in_partial_fua.attr, + &pool_stats_attr_bios_out_read.attr, + &pool_stats_attr_bios_out_write.attr, + &pool_stats_attr_bios_out_empty_flush.attr, + &pool_stats_attr_bios_out_discard.attr, + &pool_stats_attr_bios_out_flush.attr, + &pool_stats_attr_bios_out_fua.attr, + &pool_stats_attr_bios_meta_read.attr, + &pool_stats_attr_bios_meta_write.attr, + &pool_stats_attr_bios_meta_empty_flush.attr, + &pool_stats_attr_bios_meta_discard.attr, + &pool_stats_attr_bios_meta_flush.attr, + &pool_stats_attr_bios_meta_fua.attr, + &pool_stats_attr_bios_journal_read.attr, + &pool_stats_attr_bios_journal_write.attr, + &pool_stats_attr_bios_journal_empty_flush.attr, + &pool_stats_attr_bios_journal_discard.attr, + &pool_stats_attr_bios_journal_flush.attr, + &pool_stats_attr_bios_journal_fua.attr, + &pool_stats_attr_bios_page_cache_read.attr, + &pool_stats_attr_bios_page_cache_write.attr, + &pool_stats_attr_bios_page_cache_empty_flush.attr, + &pool_stats_attr_bios_page_cache_discard.attr, + &pool_stats_attr_bios_page_cache_flush.attr, + &pool_stats_attr_bios_page_cache_fua.attr, + &pool_stats_attr_bios_out_completed_read.attr, + &pool_stats_attr_bios_out_completed_write.attr, + &pool_stats_attr_bios_out_completed_empty_flush.attr, + &pool_stats_attr_bios_out_completed_discard.attr, + &pool_stats_attr_bios_out_completed_flush.attr, + &pool_stats_attr_bios_out_completed_fua.attr, + &pool_stats_attr_bios_meta_completed_read.attr, + &pool_stats_attr_bios_meta_completed_write.attr, + &pool_stats_attr_bios_meta_completed_empty_flush.attr, + &pool_stats_attr_bios_meta_completed_discard.attr, + &pool_stats_attr_bios_meta_completed_flush.attr, + &pool_stats_attr_bios_meta_completed_fua.attr, + &pool_stats_attr_bios_journal_completed_read.attr, + &pool_stats_attr_bios_journal_completed_write.attr, + &pool_stats_attr_bios_journal_completed_empty_flush.attr, + &pool_stats_attr_bios_journal_completed_discard.attr, + &pool_stats_attr_bios_journal_completed_flush.attr, + &pool_stats_attr_bios_journal_completed_fua.attr, + &pool_stats_attr_bios_page_cache_completed_read.attr, + &pool_stats_attr_bios_page_cache_completed_write.attr, + &pool_stats_attr_bios_page_cache_completed_empty_flush.attr, + &pool_stats_attr_bios_page_cache_completed_discard.attr, + &pool_stats_attr_bios_page_cache_completed_flush.attr, + &pool_stats_attr_bios_page_cache_completed_fua.attr, + &pool_stats_attr_bios_acknowledged_read.attr, + &pool_stats_attr_bios_acknowledged_write.attr, + &pool_stats_attr_bios_acknowledged_empty_flush.attr, + &pool_stats_attr_bios_acknowledged_discard.attr, + &pool_stats_attr_bios_acknowledged_flush.attr, + &pool_stats_attr_bios_acknowledged_fua.attr, + &pool_stats_attr_bios_acknowledged_partial_read.attr, + &pool_stats_attr_bios_acknowledged_partial_write.attr, + &pool_stats_attr_bios_acknowledged_partial_empty_flush.attr, + &pool_stats_attr_bios_acknowledged_partial_discard.attr, + &pool_stats_attr_bios_acknowledged_partial_flush.attr, + &pool_stats_attr_bios_acknowledged_partial_fua.attr, + &pool_stats_attr_bios_in_progress_read.attr, + &pool_stats_attr_bios_in_progress_write.attr, + &pool_stats_attr_bios_in_progress_empty_flush.attr, + &pool_stats_attr_bios_in_progress_discard.attr, + &pool_stats_attr_bios_in_progress_flush.attr, + &pool_stats_attr_bios_in_progress_fua.attr, + &pool_stats_attr_memory_usage_bytes_used.attr, + &pool_stats_attr_memory_usage_peak_bytes_used.attr, + &pool_stats_attr_index_entries_indexed.attr, + &pool_stats_attr_index_posts_found.attr, + &pool_stats_attr_index_posts_not_found.attr, + &pool_stats_attr_index_queries_found.attr, + &pool_stats_attr_index_queries_not_found.attr, + &pool_stats_attr_index_updates_found.attr, + &pool_stats_attr_index_updates_not_found.attr, + &pool_stats_attr_index_entries_discarded.attr, +NULL , +};
drivers/md/dm-vdo/pool-sysfs.c new file +198 -0
198 lines not on this page · raw message
drivers/md/dm-vdo/pool-sysfs.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright 2023 Red Hat +*/ + +#ifndef VDO_POOL_SYSFS_H +#define VDO_POOL_SYSFS_H + +#include <linux/kobject.h> + +/* The kobj_type used for setting up the kernel layer kobject. */ +extern const struct kobj_type vdo_directory_type; + +/* The sysfs_ops used for the "statistics" subdirectory. */ +extern const struct sysfs_ops vdo_pool_stats_sysfs_ops; +/* The attribute used for the "statistics" subdirectory. */ +extern struct attribute *vdo_pool_stats_attrs[]; + +#endif /* VDO_POOL_SYSFS_H */
drivers/md/dm-vdo/sysfs.c new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 39/40 dm vdo: enable configuration and building of dm-vdo
In reply to Matthew Sakaidm-vdo targets are not supported for 32-bit configurations. A vdo target typically requires 1 to 1.5 GB of memory at any given time, which is likely a large fraction of the addressable memory of a 32-bit system. At the same time, the amount of addressable storage attached to a 32-bit system may not be large enough for deduplication to provide much benefit. Because of these concerns, 32-bit platforms are deemed unlikely to benefit from using a vdo target, so dm-vdo is targeted only at 64-bit platforms.
--- drivers/md/Kconfig | 16 ++++++++++++++++ drivers/md/Makefile | 2 ++ 2 files changed, 18 insertions(+)
drivers/md/Kconfig +16 -0
@@ -532,6 +532,22 @@ config DM_FLAKEY help A target that intermittently fails I/O for debugging purposes. +config DM_VDO + tristate "VDO: deduplication and compression target" + depends on 64BIT + depends on BLK_DEV_DM + select DM_BUFIO + select LZ4_COMPRESS + select LZ4_DECOMPRESS + help + This device mapper target presents a block device with + deduplication, compression and thin-provisioning. + + To compile this code as a module, choose M here: the module will + be called dm-vdo. + + If unsure, say N. + config DM_VERITY tristate "Verity target support" depends on BLK_DEV_DM
drivers/md/Makefile +2 -0
@@ -25,6 +25,7 @@ dm-ebs-y += dm-ebs-target.o dm-era-y += dm-era-target.o dm-clone-y += dm-clone-target.o dm-clone-metadata.o dm-verity-y += dm-verity-target.o +dm-vdo-y += dm-vdo-target.o $(patsubst drivers/md/dm-vdo/%.c,dm-vdo/%.o,$(wildcard $(src)/dm-vdo/*.c)) dm-zoned-y += dm-zoned-target.o dm-zoned-metadata.o dm-zoned-reclaim.o md-mod-y += md.o md-bitmap.o
@@ -74,6 +75,7 @@ obj-$(CONFIG_DM_ZERO) += dm-zero.o obj-$(CONFIG_DM_RAID) += dm-raid.o obj-$(CONFIG_DM_THIN_PROVISIONING) += dm-thin-pool.o obj-$(CONFIG_DM_VERITY) += dm-verity.o +obj-$(CONFIG_DM_VDO) += dm-vdo.o obj-$(CONFIG_DM_CACHE) += dm-cache.o obj-$(CONFIG_DM_CACHE_SMQ) += dm-cache-smq.o obj-$(CONFIG_DM_EBS) += dm-ebs.o
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 38/40 dm vdo: add the top-level DM target
In reply to Matthew SakaiThis adds the dm-vdo target.
The dm-vdo target provides inline deduplication, compression, and zero-block elimination, allowing applications to consume less actual storage than a normal target. By layering it with other device mapper targets, it can add these features to any storage stack. It can also provide a common deduplication pool for groups of targets. The vdo target does not protect against data corruption, relying instead on integrity protection of the storage below it.
--- drivers/md/dm-vdo-target.c | 2952 ++++++++++++++++++++++++++++++++++++ 1 file changed, 2952 insertions(+) create mode 100644 drivers/md/dm-vdo-target.c
drivers/md/dm-vdo-target.c new file +2952 -0
@@ -0,0 +1,2952 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include <linux/atomic.h> +#include <linux/bitops.h> +#include <linux/completion.h> +#include <linux/delay.h> +#include <linux/device-mapper.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/spinlock.h> + +#include "dm-vdo/admin-state.h" +#include "dm-vdo/block-map.h" +#include "dm-vdo/completion.h" +#include "dm-vdo/constants.h" +#include "dm-vdo/data-vio.h" +#include "dm-vdo/dedupe.h" +#include "dm-vdo/dump.h" +#include "dm-vdo/encodings.h" +#include "dm-vdo/errors.h" +#include "dm-vdo/flush.h" +#include "dm-vdo/io-submitter.h" +#include "dm-vdo/logger.h" +#include "dm-vdo/memory-alloc.h" +#include "dm-vdo/message-stats.h" +#include "dm-vdo/pool-sysfs.h" +#include "dm-vdo/recovery-journal.h" +#include "dm-vdo/repair.h" +#include "dm-vdo/slab-depot.h" +#include "dm-vdo/status-codes.h" +#include "dm-vdo/string-utils.h" +#include "dm-vdo/thread-device.h" +#include "dm-vdo/thread-registry.h" +#include "dm-vdo/types.h" +#include "dm-vdo/uds-sysfs.h" +#include "dm-vdo/vdo.h" +#include "dm-vdo/vio.h" + +#define CURRENT_VERSION "8.3.0.65" + +enum { +GROW_LOGICAL_PHASE_START , +GROW_LOGICAL_PHASE_GROW_BLOCK_MAP , +GROW_LOGICAL_PHASE_END , +GROW_LOGICAL_PHASE_ERROR , +GROW_PHYSICAL_PHASE_START , +GROW_PHYSICAL_PHASE_COPY_SUMMARY , +GROW_PHYSICAL_PHASE_UPDATE_COMPONENTS , +GROW_PHYSICAL_PHASE_USE_NEW_SLABS , +GROW_PHYSICAL_PHASE_END , +GROW_PHYSICAL_PHASE_ERROR , +LOAD_PHASE_START , +LOAD_PHASE_STATS , +LOAD_PHASE_LOAD_DEPOT , +LOAD_PHASE_MAKE_DIRTY , +LOAD_PHASE_PREPARE_TO_ALLOCATE , +LOAD_PHASE_SCRUB_SLABS , +LOAD_PHASE_DATA_REDUCTION , +LOAD_PHASE_FINISHED , +LOAD_PHASE_DRAIN_JOURNAL , +LOAD_PHASE_WAIT_FOR_READ_ONLY , +PRE_LOAD_PHASE_START , +PRE_LOAD_PHASE_LOAD_COMPONENTS , +PRE_LOAD_PHASE_END , +PREPARE_GROW_PHYSICAL_PHASE_START , +RESUME_PHASE_START , +RESUME_PHASE_ALLOW_READ_ONLY_MODE , +RESUME_PHASE_DEDUPE , +RESUME_PHASE_DEPOT , +RESUME_PHASE_JOURNAL , +RESUME_PHASE_BLOCK_MAP , +RESUME_PHASE_LOGICAL_ZONES , +RESUME_PHASE_PACKER , +RESUME_PHASE_FLUSHER , +RESUME_PHASE_DATA_VIOS , +RESUME_PHASE_END , +SUSPEND_PHASE_START , +SUSPEND_PHASE_PACKER , +SUSPEND_PHASE_DATA_VIOS , +SUSPEND_PHASE_DEDUPE , +SUSPEND_PHASE_FLUSHES , +SUSPEND_PHASE_LOGICAL_ZONES , +SUSPEND_PHASE_BLOCK_MAP , +SUSPEND_PHASE_JOURNAL , +SUSPEND_PHASE_DEPOT , +SUSPEND_PHASE_READ_ONLY_WAIT , +SUSPEND_PHASE_WRITE_SUPER_BLOCK , +SUSPEND_PHASE_END , +}; + +static const char *const ADMIN_PHASE_NAMES [] = { +"GROW_LOGICAL_PHASE_START" , +"GROW_LOGICAL_PHASE_GROW_BLOCK_MAP" , +"GROW_LOGICAL_PHASE_END" , +"GROW_LOGICAL_PHASE_ERROR" , +"GROW_PHYSICAL_PHASE_START" , +"GROW_PHYSICAL_PHASE_COPY_SUMMARY" , +"GROW_PHYSICAL_PHASE_UPDATE_COMPONENTS" , +"GROW_PHYSICAL_PHASE_USE_NEW_SLABS" , +"GROW_PHYSICAL_PHASE_END" , +"GROW_PHYSICAL_PHASE_ERROR" , +"LOAD_PHASE_START" , +"LOAD_PHASE_STATS" , +"LOAD_PHASE_LOAD_DEPOT" , +"LOAD_PHASE_MAKE_DIRTY" , +"LOAD_PHASE_PREPARE_TO_ALLOCATE" , +"LOAD_PHASE_SCRUB_SLABS" , +"LOAD_PHASE_DATA_REDUCTION" , +"LOAD_PHASE_FINISHED" , +"LOAD_PHASE_DRAIN_JOURNAL" , +"LOAD_PHASE_WAIT_FOR_READ_ONLY" , +"PRE_LOAD_PHASE_START" , +"PRE_LOAD_PHASE_LOAD_COMPONENTS" , +"PRE_LOAD_PHASE_END" , +"PREPARE_GROW_PHYSICAL_PHASE_START" , +"RESUME_PHASE_START" , +"RESUME_PHASE_ALLOW_READ_ONLY_MODE" , +"RESUME_PHASE_DEDUPE" , +"RESUME_PHASE_DEPOT" , +"RESUME_PHASE_JOURNAL" , +"RESUME_PHASE_BLOCK_MAP" , +"RESUME_PHASE_LOGICAL_ZONES" , +"RESUME_PHASE_PACKER" , +"RESUME_PHASE_FLUSHER" , +"RESUME_PHASE_DATA_VIOS" , +"RESUME_PHASE_END" , +"SUSPEND_PHASE_START" , +"SUSPEND_PHASE_PACKER" , +"SUSPEND_PHASE_DATA_VIOS" , +"SUSPEND_PHASE_DEDUPE" , +"SUSPEND_PHASE_FLUSHES" , +"SUSPEND_PHASE_LOGICAL_ZONES" , +"SUSPEND_PHASE_BLOCK_MAP" , +"SUSPEND_PHASE_JOURNAL" , +"SUSPEND_PHASE_DEPOT" , +"SUSPEND_PHASE_READ_ONLY_WAIT" , +"SUSPEND_PHASE_WRITE_SUPER_BLOCK" , +"SUSPEND_PHASE_END" , +}; + +enum { +/* If we bump this, update the arrays below */ +TABLE_VERSION =4 , +}; + +/* arrays for handling different table versions */ +static const u8 REQUIRED_ARGC [] = {10 ,12 ,9 ,7 ,6 }; +/* pool name no longer used. only here for verification of older versions */ +static const u8 POOL_NAME_ARG_INDEX [] = {8 ,10 ,8 }; + +/* +* Track in-use instance numbers using a flat bit array. +* +* O(n) run time isn't ideal, but if we have 1000 VDO devices in use simultaneously we still only +* need to scan 16 words, so it's not likely to be a big deal compared to other resource usage. +*/ + +enum { +/* +* This minimum size for the bit array creates a numbering space of 0-999, which allows +* successive starts of the same volume to have different instance numbers in any +* reasonably-sized test. Changing instances on restart allows vdoMonReport to detect that +* the ephemeral stats have reset to zero. +*/ +BIT_COUNT_MINIMUM =1000 , +/** Grow the bit array by this many bits when needed */ +BIT_COUNT_INCREMENT =100 , +}; + +struct instance_tracker { +unsigned int bit_count; +unsigned long *words; +unsigned int count; +unsigned int next; +}; + +static DEFINE_MUTEX (instances_lock); +static struct instance_tracker instances; + +/** +* free_device_config() - Free a device config created by parse_device_config(). +* @config: The config to free. +*/ +static void free_device_config (struct device_config *config) +{ +if (config ==NULL ) +return ; + +if (config->owned_device !=NULL ) +dm_put_device (config->owning_target, config->owned_device); + +uds_free (config->parent_device_name); +uds_free (config->original_string); + +/* Reduce the chance a use-after-free (as in BZ 1669960) happens to work. */ +memset (config,0 ,sizeof (*config)); +uds_free (config); +} + +/** +* get_version_number() - Decide the version number from argv. +* +* @argc: The number of table values. +* @argv: The array of table values. +* @error_ptr: A pointer to return a error string in. +* @version_ptr: A pointer to return the version. +* +* Return: VDO_SUCCESS or an error code. +*/ +static int get_version_number (int argc,char **argv,char **error_ptr, +unsigned int *version_ptr) +{ +/* version, if it exists, is in a form of V<n> */ +if (sscanf (argv[0 ],"V%u" , version_ptr) ==1 ) { +if (*version_ptr <1 || *version_ptr >TABLE_VERSION ) { + *error_ptr ="Unknown version number detected" ; +return VDO_BAD_CONFIGURATION ; + } + }else { +/* V0 actually has no version number in the table string */ + *version_ptr =0 ; + } + +/* +* V0 and V1 have no optional parameters. There will always be a parameter for thread +* config, even if it's a "." to show it's an empty list. +*/ +if (*version_ptr <=1 ) { +if (argc !=REQUIRED_ARGC [*version_ptr]) { + *error_ptr ="Incorrect number of arguments for version" ; +return VDO_BAD_CONFIGURATION ; + } + }else if (argc <REQUIRED_ARGC [*version_ptr]) { + *error_ptr ="Incorrect number of arguments for version" ; +return VDO_BAD_CONFIGURATION ; + } + +if (*version_ptr !=TABLE_VERSION ) { +uds_log_warning ("Detected version mismatch between kernel module and tools kernel: %d, tool: %d" , +TABLE_VERSION , *version_ptr); +uds_log_warning ("Please consider upgrading management tools to match kernel." ); + } +return VDO_SUCCESS ; +} + +/* Free a list of non-NULL string pointers, and then the list itself. */ +static void free_string_array (char **string_array) +{ +unsigned int offset; + +for (offset =0 ; string_array[offset] !=NULL ; offset++) +uds_free (string_array[offset]); +uds_free (string_array); +} + +/* +* Split the input string into substrings, separated at occurrences of the indicated character, +* returning a null-terminated list of string pointers. +* +* The string pointers and the pointer array itself should both be freed with uds_free() when no +* longer needed. This can be done with vdo_free_string_array (below) if the pointers in the array +* are not changed. Since the array and copied strings are allocated by this function, it may only +* be used in contexts where allocation is permitted. +* +* Empty substrings are not ignored; that is, returned substrings may be empty strings if the +* separator occurs twice in a row. +*/ +static int split_string (const char *string,char separator,char ***substring_array_ptr) +{ +unsigned int current_substring =0 , substring_count =1 ; +const char *s; +char **substrings; +int result; +ptrdiff_t length; + +for (s = string; *s !=0 ; s++) { +if (*s == separator) + substring_count++; + } + + result = uds_allocate(substring_count + 1, char *, "string-splitting array", + &substrings); +if (result !=UDS_SUCCESS ) +return result; + +for (s = string; *s !=0 ; s++) { +if (*s == separator) { +ptrdiff_t length = s - string; + + result =uds_allocate (length +1 , char,"split string" , + &substrings[current_substring]); +if (result !=UDS_SUCCESS ) { +free_string_array (substrings); +return result; + } +/* +* Trailing NUL is already in place after allocation; deal with the zero or +* more non-NUL bytes in the string. +*/ +if (length >0 ) +memcpy (substrings[current_substring], string, length); + string = s +1 ; + current_substring++; +BUG_ON (current_substring >= substring_count); + } + } +/* Process final string, with no trailing separator. */ +BUG_ON (current_substring != (substring_count -1 )); + length =strlen (string); + + result =uds_allocate (length +1 , char,"split string" , + &substrings[current_substring]); +if (result !=UDS_SUCCESS ) { +free_string_array (substrings); +return result; + } +memcpy (substrings[current_substring], string, length); + current_substring++; +/* substrings[current_substring] is NULL already */ + *substring_array_ptr = substrings; +return UDS_SUCCESS ; +} + +/* +* Join the input substrings into one string, joined with the indicated character, returning a +* string. array_length is a bound on the number of valid elements in substring_array, in case it +* is not NULL-terminated. +*/ +static int join_strings (char **substring_array,size_t array_length,char separator, +char **string_ptr) +{ +size_t string_length =0 ; +size_t i; +int result; +char *output, *current_position; + +for (i =0 ; (i < array_length) && (substring_array[i] !=NULL ); i++) + string_length +=strlen (substring_array[i]) +1 ; + + result =uds_allocate (string_length, char, __func__, &output); +if (result !=VDO_SUCCESS ) +return result; + + current_position = &output[0 ]; + +for (i =0 ; (i < array_length) && (substring_array[i] !=NULL ); i++) { + current_position =uds_append_to_buffer (current_position, + output + string_length,"%s" , + substring_array[i]); + *current_position = separator; + current_position++; + } + +/* We output one too many separators; replace the last with a zero byte. */ +if (current_position != output) + *(current_position -1 ) ='\0' ; + + *string_ptr = output; +return UDS_SUCCESS ; +} + +/** +* parse_bool() - Parse a two-valued option into a bool. +* @bool_str: The string value to convert to a bool. +* @true_str: The string value which should be converted to true. +* @false_str: The string value which should be converted to false. +* @bool_ptr: A pointer to return the bool value in. +* +* Return: VDO_SUCCESS or an error if bool_str is neither true_str nor false_str. +*/ +static inline int __must_check parse_bool (const char *bool_str,const char *true_str, +const char *false_str,bool *bool_ptr) +{ +bool value = false; + +if (strcmp (bool_str, true_str) ==0 ) + value = true; +else if (strcmp (bool_str, false_str) ==0 ) + value = false; +else +return VDO_BAD_CONFIGURATION ; + + *bool_ptr = value; +return VDO_SUCCESS ; +} + +/** +* process_one_thread_config_spec() - Process one component of a thread parameter configuration +* string and update the configuration data structure. +* @thread_param_type: The type of thread specified. +* @count: The thread count requested. +* @config: The configuration data structure to update. +* +* If the thread count requested is invalid, a message is logged and -EINVAL returned. If the +* thread name is unknown, a message is logged but no error is returned. +* +* Return: VDO_SUCCESS or -EINVAL +*/ +static int process_one_thread_config_spec (const char *thread_param_type, +unsigned int count, +struct thread_count_config *config) +{ +/* Handle limited thread parameters */ +if (strcmp (thread_param_type,"bioRotationInterval" ) ==0 ) { +if (count ==0 ) { +uds_log_error ("thread config string error: 'bioRotationInterval' of at least 1 is required" ); +return -EINVAL ; + }else if (count >VDO_BIO_ROTATION_INTERVAL_LIMIT ) { +uds_log_error ("thread config string error: 'bioRotationInterval' cannot be higher than %d" , +VDO_BIO_ROTATION_INTERVAL_LIMIT ); +return -EINVAL ; + } + config->bio_rotation_interval = count; +return VDO_SUCCESS ; + } +if (strcmp (thread_param_type,"logical" ) ==0 ) { +if (count >MAX_VDO_LOGICAL_ZONES ) { +uds_log_error ("thread config string error: at most %d 'logical' threads are allowed" , +MAX_VDO_LOGICAL_ZONES ); +return -EINVAL ; + } + config->logical_zones = count; +return VDO_SUCCESS ; + } +if (strcmp (thread_param_type,"physical" ) ==0 ) { +if (count >MAX_VDO_PHYSICAL_ZONES ) { +uds_log_error ("thread config string error: at most %d 'physical' threads are allowed" , +MAX_VDO_PHYSICAL_ZONES ); +return -EINVAL ; + } + config->physical_zones = count; +return VDO_SUCCESS ; + } +/* Handle other thread count parameters */ +if (count >MAXIMUM_VDO_THREADS ) { +uds_log_error ("thread config string error: at most %d '%s' threads are allowed" , +MAXIMUM_VDO_THREADS , thread_param_type); +return -EINVAL ; + } +if (strcmp (thread_param_type,"hash" ) ==0 ) { + config->hash_zones = count; +return VDO_SUCCESS ; + } +if (strcmp (thread_param_type,"cpu" ) ==0 ) { +if (count ==0 ) { +uds_log_error ("thread config string error: at least one 'cpu' thread required" ); +return -EINVAL ; + } + config->cpu_threads = count; +return VDO_SUCCESS ; + } +if (strcmp (thread_param_type,"ack" ) ==0 ) { + config->bio_ack_threads = count; +return VDO_SUCCESS ; + } +if (strcmp (thread_param_type,"bio" ) ==0 ) { +if (count ==0 ) { +uds_log_error ("thread config string error: at least one 'bio' thread required" ); +return -EINVAL ; + } + config->bio_threads = count; +return VDO_SUCCESS ; + } + +/* +* Don't fail, just log. This will handle version mismatches between user mode tools and +* kernel. +*/ +uds_log_info ("unknown thread parameter type \"%s\"" , thread_param_type); +return VDO_SUCCESS ; +} + +/** +* parse_one_thread_config_spec() - Parse one component of a thread parameter configuration string +* and update the configuration data structure. +* @spec: The thread parameter specification string. +* @config: The configuration data to be updated. +*/ +static int parse_one_thread_config_spec (const char *spec, +struct thread_count_config *config) +{ +unsigned int count; +char **fields; +int result; + + result =split_string (spec,'=' , &fields); +if (result !=UDS_SUCCESS ) +return result; + +if ((fields[0 ] ==NULL ) || (fields[1 ] ==NULL ) || (fields[2 ] !=NULL )) { +uds_log_error ("thread config string error: expected thread parameter assignment, saw \"%s\"" , + spec); +free_string_array (fields); +return -EINVAL ; + } + + result =kstrtouint (fields[1 ],10 , &count); +if (result !=UDS_SUCCESS ) { +uds_log_error ("thread config string error: integer value needed, found \"%s\"" , + fields[1 ]); +free_string_array (fields); +return result; + } + + result =process_one_thread_config_spec (fields[0 ], count, config); +free_string_array (fields); +return result; +} + +/** +* parse_thread_config_string() - Parse the configuration string passed and update the specified +* counts and other parameters of various types of threads to be +* created. +* @string: Thread parameter configuration string. +* @config: The thread configuration data to update. +* +* The configuration string should contain one or more comma-separated specs of the form +* "typename=number"; the supported type names are "cpu", "ack", "bio", "bioRotationInterval", +* "logical", "physical", and "hash". +* +* If an error occurs during parsing of a single key/value pair, we deem it serious enough to stop +* further parsing. +* +* This function can't set the "reason" value the caller wants to pass back, because we'd want to +* format it to say which field was invalid, and we can't allocate the "reason" strings +* dynamically. So if an error occurs, we'll log the details and pass back an error. +* +* Return: VDO_SUCCESS or -EINVAL or -ENOMEM +*/ +static int parse_thread_config_string (const char *string, +struct thread_count_config *config) +{ +int result =VDO_SUCCESS ; +char **specs; + +if (strcmp ("." , string) !=0 ) { +unsigned int i; + + result =split_string (string,',' , &specs); +if (result !=UDS_SUCCESS ) +return result; + +for (i =0 ; specs[i] !=NULL ; i++) { + result =parse_one_thread_config_spec (specs[i], config); +if (result !=VDO_SUCCESS ) +break ; + } +free_string_array (specs); + } +return result; +} + +/** +* process_one_key_value_pair() - Process one component of an optional parameter string and update +* the configuration data structure. +* @key: The optional parameter key name. +* @value: The optional parameter value. +* @config: The configuration data structure to update. +* +* If the value requested is invalid, a message is logged and -EINVAL returned. If the key is +* unknown, a message is logged but no error is returned. +* +* Return: VDO_SUCCESS or -EINVAL +*/ +static int process_one_key_value_pair (const char *key,unsigned int value, +struct device_config *config) +{ +/* Non thread optional parameters */ +if (strcmp (key,"maxDiscard" ) ==0 ) { +if (value ==0 ) { +uds_log_error ("optional parameter error: at least one max discard block required" ); +return -EINVAL ; + } +/* Max discard sectors in blkdev_issue_discard is UINT_MAX >> 9 */ +if (value > (UINT_MAX /VDO_BLOCK_SIZE )) { +uds_log_error ("optional parameter error: at most %d max discard blocks are allowed" , +UINT_MAX /VDO_BLOCK_SIZE ); +return -EINVAL ; + } + config->max_discard_blocks = value; +return VDO_SUCCESS ; + } +/* Handles unknown key names */ +return process_one_thread_config_spec (key, value, &config->thread_counts); +} + +/** +* parse_one_key_value_pair() - Parse one key/value pair and update the configuration data +* structure. +* @key: The optional key name. +* @value: The optional value. +* @config: The configuration data to be updated. +* +* Return: VDO_SUCCESS or error. +*/ +static int parse_one_key_value_pair (const char *key,const char *value, +struct device_config *config) +{ +unsigned int count; +int result; + +if (strcmp (key,"deduplication" ) ==0 ) +return parse_bool (value,"on" ,"off" , &config->deduplication); + +if (strcmp (key,"compression" ) ==0 ) +return parse_bool (value,"on" ,"off" , &config->compression); + +/* The remaining arguments must have integral values. */ + result =kstrtouint (value,10 , &count); +if (result !=UDS_SUCCESS ) { +uds_log_error ("optional config string error: integer value needed, found \"%s\"" , + value); +return result; + } +return process_one_key_value_pair (key, count, config); +} + +/** +* parse_key_value_pairs() - Parse all key/value pairs from a list of arguments. +* @argc: The total number of arguments in list. +* @argv: The list of key/value pairs. +* @config: The device configuration data to update. +* +* If an error occurs during parsing of a single key/value pair, we deem it serious enough to stop +* further parsing. +* +* This function can't set the "reason" value the caller wants to pass back, because we'd want to +* format it to say which field was invalid, and we can't allocate the "reason" strings +* dynamically. So if an error occurs, we'll log the details and return the error. +* +* Return: VDO_SUCCESS or error +*/ +static int parse_key_value_pairs (int argc,char **argv,struct device_config *config) +{ +int result =VDO_SUCCESS ; + +while (argc) { + result =parse_one_key_value_pair (argv[0 ], argv[1 ], config); +if (result !=VDO_SUCCESS ) +break ; + + argc -=2 ; + argv +=2 ; + } + +return result; +} + +/** +* parse_optional_arguments() - Parse the configuration string passed in for optional arguments. +* @arg_set: The structure holding the arguments to parse. +* @error_ptr: Pointer to a buffer to hold the error string. +* @config: Pointer to device configuration data to update. +* +* For V0/V1 configurations, there will only be one optional parameter; the thread configuration. +* The configuration string should contain one or more comma-separated specs of the form +* "typename=number"; the supported type names are "cpu", "ack", "bio", "bioRotationInterval", +* "logical", "physical", and "hash". +* +* For V2 configurations and beyond, there could be any number of arguments. They should contain +* one or more key/value pairs separated by a space. +* +* Return: VDO_SUCCESS or error +*/ +static int parse_optional_arguments (struct dm_arg_set *arg_set,char **error_ptr, +struct device_config *config) +{ +int result =VDO_SUCCESS ; + +if (config->version ==0 || config->version ==1 ) { + result =parse_thread_config_string (arg_set->argv[0 ], + &config->thread_counts); +if (result !=VDO_SUCCESS ) { + *error_ptr ="Invalid thread-count configuration" ; +return VDO_BAD_CONFIGURATION ; + } + }else { +if ((arg_set->argc %2 ) !=0 ) { + *error_ptr ="Odd number of optional arguments given but they should be <key> <value> pairs" ; +return VDO_BAD_CONFIGURATION ; + } + result =parse_key_value_pairs (arg_set->argc, arg_set->argv, config); +if (result !=VDO_SUCCESS ) { + *error_ptr ="Invalid optional argument configuration" ; +return VDO_BAD_CONFIGURATION ; + } + } +return result; +} + +/** +* handle_parse_error() - Handle a parsing error. +* @config: The config to free. +* @error_ptr: A place to store a constant string about the error. +* @error_str: A constant string to store in error_ptr. +*/ +static void handle_parse_error (struct device_config *config,char **error_ptr, +char *error_str) +{ +free_device_config (config); + *error_ptr = error_str; +} + +/** +* parse_device_config() - Convert the dmsetup table into a struct device_config. +* @argc: The number of table values. +* @argv: The array of table values. +* @ti: The target structure for this table. +* @config_ptr: A pointer to return the allocated config. +* +* Return: VDO_SUCCESS or an error code. +*/ +static int parse_device_config (int argc,char **argv,struct dm_target *ti, +struct device_config **config_ptr) +{ +bool enable_512e; +size_t logical_bytes =to_bytes (ti->len); +struct dm_arg_set arg_set; +char **error_ptr = &ti->error; +struct device_config *config =NULL ; +int result; + +if ((logical_bytes %VDO_BLOCK_SIZE ) !=0 ) { +handle_parse_error (config, error_ptr, +"Logical size must be a multiple of 4096" ); +return VDO_BAD_CONFIGURATION ; + } + +if (argc ==0 ) { +handle_parse_error (config, error_ptr,"Incorrect number of arguments" ); +return VDO_BAD_CONFIGURATION ; + } + + result = uds_allocate(1, struct device_config, "device_config", &config); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr, +"Could not allocate config structure" ); +return VDO_BAD_CONFIGURATION ; + } + + config->owning_target = ti; + config->logical_blocks = logical_bytes /VDO_BLOCK_SIZE ; +INIT_LIST_HEAD (&config->config_list); + +/* Save the original string. */ + result =join_strings (argv, argc,' ' , &config->original_string); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr,"Could not populate string" ); +return VDO_BAD_CONFIGURATION ; + } + +uds_log_info ("table line: %s" , config->original_string); + + config->thread_counts = (struct thread_count_config ) { + .bio_ack_threads =1 , + .bio_threads =DEFAULT_VDO_BIO_SUBMIT_QUEUE_COUNT , + .bio_rotation_interval =DEFAULT_VDO_BIO_SUBMIT_QUEUE_ROTATE_INTERVAL , + .cpu_threads =1 , + .logical_zones =0 , + .physical_zones =0 , + .hash_zones =0 , + }; + config->max_discard_blocks =1 ; + config->deduplication = true; + config->compression = false; + + arg_set.argc = argc; + arg_set.argv = argv; + + result =get_version_number (argc, argv, error_ptr, &config->version); +if (result !=VDO_SUCCESS ) { +/* get_version_number sets error_ptr itself. */ +handle_parse_error (config, error_ptr, *error_ptr); +return result; + } +/* Move the arg pointer forward only if the argument was there. */ +if (config->version >=1 ) +dm_shift_arg (&arg_set); + + result =uds_duplicate_string (dm_shift_arg (&arg_set),"parent device name" , + &config->parent_device_name); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr, +"Could not copy parent device name" ); +return VDO_BAD_CONFIGURATION ; + } + +/* Get the physical blocks, if known. */ +if (config->version >=1 ) { + result =kstrtoull (dm_shift_arg (&arg_set),10 , &config->physical_blocks); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr, +"Invalid physical block count" ); +return VDO_BAD_CONFIGURATION ; + } + } + +/* Get the logical block size and validate */ + result =parse_bool (dm_shift_arg (&arg_set),"512" ,"4096" , &enable_512e); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr,"Invalid logical block size" ); +return VDO_BAD_CONFIGURATION ; + } + config->logical_block_size = (enable_512e ?512 :4096 ); + +/* Skip past the two no longer used read cache options. */ +if (config->version <=1 ) +dm_consume_args (&arg_set,2 ); + +/* Get the page cache size. */ + result =kstrtouint (dm_shift_arg (&arg_set),10 , &config->cache_size); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr, +"Invalid block map page cache size" ); +return VDO_BAD_CONFIGURATION ; + } + +/* Get the block map era length. */ + result =kstrtouint (dm_shift_arg (&arg_set),10 , &config->block_map_maximum_age); +if (result !=VDO_SUCCESS ) { +handle_parse_error (config, error_ptr,"Invalid block map maximum age" ); +return VDO_BAD_CONFIGURATION ; + } + +/* Skip past the no longer used MD RAID5 optimization mode */ +if (config->version <=2 ) +dm_consume_args (&arg_set,1 ); + +/* Skip past the no longer used write policy setting */ +if (config->version <=3 ) +dm_consume_args (&arg_set,1 ); + +/* Skip past the no longer used pool name for older table lines */ +if (config->version <=2 ) { +/* +* Make sure the enum to get the pool name from argv directly is still in sync with +* the parsing of the table line. +*/ +if (&arg_set.argv[0 ] != &argv[POOL_NAME_ARG_INDEX [config->version]]) { +handle_parse_error (config, error_ptr, +"Pool name not in expected location" ); +return VDO_BAD_CONFIGURATION ; + } +dm_shift_arg (&arg_set); + } + +/* Get the optional arguments and validate. */ + result =parse_optional_arguments (&arg_set, error_ptr, config); +if (result !=VDO_SUCCESS ) { +/* parse_optional_arguments sets error_ptr itself. */ +handle_parse_error (config, error_ptr, *error_ptr); +return result; + } + +/* +* Logical, physical, and hash zone counts can all be zero; then we get one thread doing +* everything, our older configuration. If any zone count is non-zero, the others must be +* as well. +*/ +if (((config->thread_counts.logical_zones ==0 ) != + (config->thread_counts.physical_zones ==0 )) || + ((config->thread_counts.physical_zones ==0 ) != + (config->thread_counts.hash_zones ==0 ))) { +handle_parse_error (config, error_ptr, +"Logical, physical, and hash zones counts must all be zero or all non-zero" ); +return VDO_BAD_CONFIGURATION ; + } + +if (config->cache_size < + (2 *MAXIMUM_VDO_USER_VIOS * config->thread_counts.logical_zones)) { +handle_parse_error (config, error_ptr, +"Insufficient block map cache for logical zones" ); +return VDO_BAD_CONFIGURATION ; + } + + result =dm_get_device (ti, config->parent_device_name, +dm_table_get_mode (ti->table), &config->owned_device); +if (result !=0 ) { +uds_log_error ("couldn't open device \"%s\": error %d" , + config->parent_device_name, result); +handle_parse_error (config, error_ptr,"Unable to open storage device" ); +return VDO_BAD_CONFIGURATION ; + } + +if (config->version ==0 ) { +u64 device_size =i_size_read (config->owned_device->bdev->bd_inode); + + config->physical_blocks = device_size /VDO_BLOCK_SIZE ; + } + + *config_ptr = config; +return result; +} + +static struct vdo *get_vdo_for_target (struct dm_target *ti) +{ +return ((struct device_config *) ti->private)->vdo; +} + + +static int vdo_map_bio (struct dm_target *ti,struct bio *bio) +{ +struct vdo *vdo =get_vdo_for_target (ti); +struct vdo_work_queue *current_work_queue; +const struct admin_state_code *code =vdo_get_admin_state_code (&vdo->admin.state); + +ASSERT_LOG_ONLY (code->normal,"vdo should not receive bios while in state %s" , + code->name); + +/* Count all incoming bios. */ +vdo_count_bios (&vdo->stats.bios_in, bio); + + +/* Handle empty bios. Empty flush bios are not associated with a vio. */ +if ((bio_op (bio) ==REQ_OP_FLUSH ) || ((bio->bi_opf &REQ_PREFLUSH ) !=0 )) { +vdo_launch_flush (vdo, bio); +return DM_MAPIO_SUBMITTED ; + } + +/* This could deadlock, */ + current_work_queue =vdo_get_current_work_queue (); +BUG_ON ((current_work_queue !=NULL ) && + (vdo ==vdo_get_work_queue_owner (current_work_queue)->vdo)); +vdo_launch_bio (vdo->data_vio_pool, bio); +return DM_MAPIO_SUBMITTED ; +} + +static void vdo_io_hints (struct dm_target *ti,struct queue_limits *limits) +{ +struct vdo *vdo =get_vdo_for_target (ti); + + limits->logical_block_size = vdo->device_config->logical_block_size; + limits->physical_block_size =VDO_BLOCK_SIZE ; + +/* The minimum io size for random io */ +blk_limits_io_min (limits,VDO_BLOCK_SIZE ); +/* The optimal io size for streamed/sequential io */ +blk_limits_io_opt (limits,VDO_BLOCK_SIZE ); + +/* +* Sets the maximum discard size that will be passed into VDO. This value comes from a +* table line value passed in during dmsetup create. +* +* The value 1024 is the largest usable value on HD systems. A 2048 sector discard on a +* busy HD system takes 31 seconds. We should use a value no higher than 1024, which takes +* 15 to 16 seconds on a busy HD system. +* +* But using large values results in 120 second blocked task warnings in /var/log/kern.log. +* In order to avoid these warnings, we choose to use the smallest reasonable value. See +* VDO-3062 and VDO-3087. +* +* The value is displayed in sysfs, and also used by dm-thin to determine whether to pass +* down discards. The block layer splits large discards on this boundary when this is set. +*/ + limits->max_discard_sectors = + (vdo->device_config->max_discard_blocks *VDO_SECTORS_PER_BLOCK ); + +/* +* Force discards to not begin or end with a partial block by stating the granularity is +* 4k. +*/ + limits->discard_granularity =VDO_BLOCK_SIZE ; +} + +static int vdo_iterate_devices (struct dm_target *ti,iterate_devices_callout_fn fn, +void *data) +{ +struct device_config *config =get_vdo_for_target (ti)->device_config; + +return fn (ti, config->owned_device,0 , + config->physical_blocks *VDO_SECTORS_PER_BLOCK , data); +} + +/* +* Status line is: +* <device> <operating mode> <in recovery> <index state> <compression state> +* <used physical blocks> <total physical blocks> +*/ + +static void vdo_status (struct dm_target *ti,status_type_t status_type, +unsigned int status_flags,char *result,unsigned int maxlen) +{ +struct vdo *vdo =get_vdo_for_target (ti); +struct vdo_statistics *stats; +struct device_config *device_config; +/* N.B.: The DMEMIT macro uses the variables named "sz", "result", "maxlen". */ +int sz =0 ; + +switch (status_type) { +case STATUSTYPE_INFO : +/* Report info for dmsetup status */ +mutex_lock (&vdo->stats_mutex); +vdo_fetch_statistics (vdo, &vdo->stats_buffer); + stats = &vdo->stats_buffer; + +DMEMIT ("/dev/%pg %s %s %s %s %llu %llu" , +vdo_get_backing_device (vdo), stats->mode, + stats->in_recovery_mode ?"recovering" :"-" , +vdo_get_dedupe_index_state_name (vdo->hash_zones), +vdo_get_compressing (vdo) ?"online" :"offline" , + stats->data_blocks_used + stats->overhead_blocks_used, + stats->physical_blocks); +mutex_unlock (&vdo->stats_mutex); +break ; + +case STATUSTYPE_TABLE : +/* Report the string actually specified in the beginning. */ + device_config = (struct device_config *) ti->private; +DMEMIT ("%s" , device_config->original_string); +break ; + +case STATUSTYPE_IMA : +/* FIXME: We ought to be more detailed here, but this is what thin does. */ + *result ='\0' ; +break ; + } +} + +static block_count_t __must_check get_underlying_device_block_count (const struct vdo *vdo) +{ +return i_size_read (vdo_get_backing_device (vdo)->bd_inode) /VDO_BLOCK_SIZE ; +} + +static int __must_check process_vdo_message_locked (struct vdo *vdo,unsigned int argc, +char **argv) +{ +if ((argc ==2 ) && (strcasecmp (argv[0 ],"compression" ) ==0 )) { +if (strcasecmp (argv[1 ],"on" ) ==0 ) { +vdo_set_compressing (vdo, true); +return 0 ; + } + +if (strcasecmp (argv[1 ],"off" ) ==0 ) { +vdo_set_compressing (vdo, false); +return 0 ; + } + +uds_log_warning ("invalid argument '%s' to dmsetup compression message" , + argv[1 ]); +return -EINVAL ; + } + +uds_log_warning ("unrecognized dmsetup message '%s' received" , argv[0 ]); +return -EINVAL ; +} + +/* +* If the message is a dump, just do it. Otherwise, check that no other message is being processed, +* and only proceed if so. +* Returns -EBUSY if another message is being processed +*/ +static int __must_check process_vdo_message (struct vdo *vdo,unsigned int argc, +char **argv) +{ +int result; + +/* +* All messages which may be processed in parallel with other messages should be handled +* here before the atomic check below. Messages which should be exclusive should be +* processed in process_vdo_message_locked(). +*/ + +/* Dump messages should always be processed */ +if (strcasecmp (argv[0 ],"dump" ) ==0 ) +return vdo_dump (vdo, argc, argv,"dmsetup message" ); + +if (argc ==1 ) { +if (strcasecmp (argv[0 ],"dump-on-shutdown" ) ==0 ) { + vdo->dump_on_shutdown = true; +return 0 ; + } + +/* Index messages should always be processed */ +if ((strcasecmp (argv[0 ],"index-close" ) ==0 ) || + (strcasecmp (argv[0 ],"index-create" ) ==0 ) || + (strcasecmp (argv[0 ],"index-disable" ) ==0 ) || + (strcasecmp (argv[0 ],"index-enable" ) ==0 )) +return vdo_message_dedupe_index (vdo->hash_zones, argv[0 ]); + } + +if (atomic_cmpxchg (&vdo->processing_message,0 ,1 ) !=0 ) +return -EBUSY ; + + result =process_vdo_message_locked (vdo, argc, argv); + +/* Pairs with the implicit barrier in cmpxchg just above */ +smp_wmb (); +atomic_set (&vdo->processing_message,0 ); +return result; +} + +static int vdo_message (struct dm_target *ti,unsigned int argc,char **argv, +char *result_buffer,unsigned int maxlen) +{ +struct registered_thread allocating_thread, instance_thread; +struct vdo *vdo; +int result; + +if (argc ==0 ) { +uds_log_warning ("unspecified dmsetup message" ); +return -EINVAL ; + } + + vdo =get_vdo_for_target (ti); +uds_register_allocating_thread (&allocating_thread,NULL ); +uds_register_thread_device_id (&instance_thread, &vdo->instance); + +/* +* Must be done here so we don't map return codes. The code in dm-ioctl expects a 1 for a +* return code to look at the buffer and see if it is full or not. +*/ +if ((argc ==1 ) && (strcasecmp (argv[0 ],"stats" ) ==0 )) { +vdo_write_stats (vdo, result_buffer, maxlen); + result =1 ; + }else { + result =vdo_map_to_system_error (process_vdo_message (vdo, argc, argv)); + } + +uds_unregister_thread_device_id (); +uds_unregister_allocating_thread (); +return result; +} + +static void configure_target_capabilities (struct dm_target *ti) +{ + ti->discards_supported =1 ; + ti->flush_supported = true; + ti->num_discard_bios =1 ; + ti->num_flush_bios =1 ; + +/* +* If this value changes, please make sure to update the value for max_discard_sectors +* accordingly. +*/ +BUG_ON (dm_set_target_max_io_len (ti,VDO_SECTORS_PER_BLOCK ) !=0 ); +} + +/* +* Implements vdo_filter_fn. +*/ +static bool vdo_uses_device (struct vdo *vdo,const void *context) +{ +const struct device_config *config = context; + +return vdo_get_backing_device (vdo)->bd_dev == config->owned_device->bdev->bd_dev; +} + +/** +* get_thread_id_for_phase() - Get the thread id for the current phase of the admin operation in +* progress. +*/ +static thread_id_t __must_check get_thread_id_for_phase (struct vdo *vdo) +{ +switch (vdo->admin.phase) { +case RESUME_PHASE_PACKER : +case RESUME_PHASE_FLUSHER : +case SUSPEND_PHASE_PACKER : +case SUSPEND_PHASE_FLUSHES : +return vdo->thread_config.packer_thread; + +case RESUME_PHASE_DATA_VIOS : +case SUSPEND_PHASE_DATA_VIOS : +return vdo->thread_config.cpu_thread; + +case LOAD_PHASE_DRAIN_JOURNAL : +case RESUME_PHASE_JOURNAL : +case SUSPEND_PHASE_JOURNAL : +return vdo->thread_config.journal_thread; + +default : +return vdo->thread_config.admin_thread; + } +} + +static struct vdo_completion *prepare_admin_completion (struct vdo *vdo, +vdo_action_fn callback, +vdo_action_fn error_handler) +{ +struct vdo_completion *completion = &vdo->admin.completion; + +/* +* We can't use vdo_prepare_completion_for_requeue() here because we don't want to reset +* any error in the completion. +*/ + completion->callback = callback; + completion->error_handler = error_handler; + completion->callback_thread_id =get_thread_id_for_phase (vdo); + completion->requeue = true; +return completion; +} + +/** +* advance_phase() - Increment the phase of the current admin operation and prepare the admin +* completion to run on the thread for the next phase. +* @vdo: The on which an admin operation is being performed +* +* Return: The current phase +*/ +static u32 advance_phase (struct vdo *vdo) +{ +u32 phase = vdo->admin.phase++; + + vdo->admin.completion.callback_thread_id =get_thread_id_for_phase (vdo); + vdo->admin.completion.requeue = true; +return phase; +} + +/* +* Perform an administrative operation (load, suspend, grow logical, or grow physical). This method +* should not be called from vdo threads. +*/ +static int perform_admin_operation (struct vdo *vdo,u32 starting_phase, +vdo_action_fn callback,vdo_action_fn error_handler, +const char *type) +{ +int result; +struct vdo_administrator *admin = &vdo->admin; + +if (atomic_cmpxchg (&admin->busy,0 ,1 ) !=0 ) { +return uds_log_error_strerror (VDO_COMPONENT_BUSY , +"Can't start %s operation, another operation is already in progress" , + type); + } + + admin->phase = starting_phase; +reinit_completion (&admin->callback_sync); +vdo_reset_completion (&admin->completion); +vdo_launch_completion (prepare_admin_completion (vdo, callback, error_handler)); + +/* +* Using the "interruptible" interface means that Linux will not log a message when we wait +* for more than 120 seconds. +*/ +while (wait_for_completion_interruptible (&admin->callback_sync) !=0 ) +/* * However, if we get a signal in a user-mode process, we could spin... */ +fsleep (1000 ); + + result = admin->completion.result; +/* pairs with implicit barrier in cmpxchg above */ +smp_wmb (); +atomic_set (&admin->busy,0 ); +return result; +} + +/* Assert that we are operating on the correct thread for the current phase. */ +static void assert_admin_phase_thread (struct vdo *vdo,const char *what) +{ +ASSERT_LOG_ONLY (vdo_get_callback_thread_id () ==get_thread_id_for_phase (vdo), +"%s on correct thread for %s" , what, +ADMIN_PHASE_NAMES [vdo->admin.phase]); +} + +/** +* finish_operation_callback() - Callback to finish an admin operation. +* @completion: The admin_completion. +*/ +static void finish_operation_callback (struct vdo_completion *completion) +{ +struct vdo_administrator *admin = &completion->vdo->admin; + +vdo_finish_operation (&admin->state, completion->result); +complete (&admin->callback_sync); +} + +/** +* decode_from_super_block() - Decode the VDO state from the super block and validate that it is +* correct. +* @vdo: The vdo being loaded. +* +* On error from this method, the component states must be destroyed explicitly. If this method +* returns successfully, the component states must not be destroyed. +* +* Return: VDO_SUCCESS or an error. +*/ +static int __must_check decode_from_super_block (struct vdo *vdo) +{ +const struct device_config *config = vdo->device_config; +int result; + + result =vdo_decode_component_states (vdo->super_block.buffer, &vdo->geometry, + &vdo->states); +if (result !=VDO_SUCCESS ) +return result; + +vdo_set_state (vdo, vdo->states.vdo.state); + vdo->load_state = vdo->states.vdo.state; + +/* +* If the device config specifies a larger logical size than was recorded in the super +* block, just accept it. +*/ +if (vdo->states.vdo.config.logical_blocks < config->logical_blocks) { +uds_log_warning ("Growing logical size: a logical size of %llu blocks was specified, but that differs from the %llu blocks configured in the vdo super block" , + (unsigned long long ) config->logical_blocks, + (unsigned long long ) vdo->states.vdo.config.logical_blocks); + vdo->states.vdo.config.logical_blocks = config->logical_blocks; + } + + result =vdo_validate_component_states (&vdo->states, vdo->geometry.nonce, + config->physical_blocks, + config->logical_blocks); +if (result !=VDO_SUCCESS ) +return result; + + vdo->layout = vdo->states.layout; +return VDO_SUCCESS ; +} + +/** +* decode_vdo() - Decode the component data portion of a super block and fill in the corresponding +* portions of the vdo being loaded. +* @vdo: The vdo being loaded. +* +* This will also allocate the recovery journal and slab depot. If this method is called with an +* asynchronous layer (i.e. a thread config which specifies at least one base thread), the block +* map and packer will be constructed as well. +* +* Return: VDO_SUCCESS or an error. +*/ +static int __must_check decode_vdo (struct vdo *vdo) +{ +block_count_t maximum_age, journal_length; +struct partition *partition; +int result; + + result =decode_from_super_block (vdo); +if (result !=VDO_SUCCESS ) { +vdo_destroy_component_states (&vdo->states); +return result; + } + + maximum_age =vdo_convert_maximum_age (vdo->device_config->block_map_maximum_age); + journal_length = +vdo_get_recovery_journal_length (vdo->states.vdo.config.recovery_journal_size); +if (maximum_age > (journal_length /2 )) { +return uds_log_error_strerror (VDO_BAD_CONFIGURATION , +"maximum age: %llu exceeds limit %llu" , + (unsigned long long ) maximum_age, + (unsigned long long ) (journal_length /2 )); + } + +if (maximum_age ==0 ) { +return uds_log_error_strerror (VDO_BAD_CONFIGURATION , +"maximum age must be greater than 0" ); + } + + result =vdo_enable_read_only_entry (vdo); +if (result !=VDO_SUCCESS ) +return result; + + partition =vdo_get_known_partition (&vdo->layout, +VDO_RECOVERY_JOURNAL_PARTITION ); + result =vdo_decode_recovery_journal (vdo->states.recovery_journal, + vdo->states.vdo.nonce, vdo, partition, + vdo->states.vdo.complete_recoveries, + vdo->states.vdo.config.recovery_journal_size, + &vdo->recovery_journal); +if (result !=VDO_SUCCESS ) +return result; + + partition =vdo_get_known_partition (&vdo->layout,VDO_SLAB_SUMMARY_PARTITION ); + result =vdo_decode_slab_depot (vdo->states.slab_depot, vdo, partition, + &vdo->depot); +if (result !=VDO_SUCCESS ) +return result; + + result =vdo_decode_block_map (vdo->states.block_map, + vdo->states.vdo.config.logical_blocks, vdo, + vdo->recovery_journal, vdo->states.vdo.nonce, + vdo->device_config->cache_size, maximum_age, + &vdo->block_map); +if (result !=VDO_SUCCESS ) +return result; + + result =vdo_make_physical_zones (vdo, &vdo->physical_zones); +if (result !=VDO_SUCCESS ) +return result; + +/* The logical zones depend on the physical zones already existing. */ + result =vdo_make_logical_zones (vdo, &vdo->logical_zones); +if (result !=VDO_SUCCESS ) +return result; + +return vdo_make_hash_zones (vdo, &vdo->hash_zones); +} + +/** +* pre_load_callback() - Callback to initiate a pre-load, registered in vdo_initialize(). +* @completion: The admin completion. +*/ +static void pre_load_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +int result; + +assert_admin_phase_thread (vdo, __func__); + +switch (advance_phase (vdo)) { +case PRE_LOAD_PHASE_START : + result =vdo_start_operation (&vdo->admin.state, +VDO_ADMIN_STATE_PRE_LOADING ); +if (result !=VDO_SUCCESS ) { +vdo_continue_completion (completion, result); +return ; + } + +vdo_load_super_block (vdo, completion); +return ; + +case PRE_LOAD_PHASE_LOAD_COMPONENTS : +vdo_continue_completion (completion,decode_vdo (vdo)); +return ; + +case PRE_LOAD_PHASE_END : +break ; + +default : +vdo_set_completion_result (completion,UDS_BAD_STATE ); + } + +finish_operation_callback (completion); +} + +static void release_instance (unsigned int instance) +{ +mutex_lock (&instances_lock); +if (instance >= instances.bit_count) { +ASSERT_LOG_ONLY (false, +"instance number %u must be less than bit count %u" , + instance, instances.bit_count); + }else if (test_bit (instance, instances.words) ==0 ) { +ASSERT_LOG_ONLY (false,"instance number %u must be allocated" , instance); + }else { +__clear_bit (instance, instances.words); + instances.count -=1 ; + } +mutex_unlock (&instances_lock); +} + +static void set_device_config (struct dm_target *ti,struct vdo *vdo, +struct device_config *config) +{ +list_del_init (&config->config_list); +list_add_tail (&config->config_list, &vdo->device_config_list); + config->vdo = vdo; + ti->private = config; +configure_target_capabilities (ti); +} + +static int vdo_initialize (struct dm_target *ti,unsigned int instance, +struct device_config *config) +{ +struct vdo *vdo; +int result; +u64 block_size =VDO_BLOCK_SIZE ; +u64 logical_size =to_bytes (ti->len); +block_count_t logical_blocks = logical_size / block_size; + +uds_log_info ("loading device '%s'" ,vdo_get_device_name (ti)); +uds_log_debug ("Logical block size = %llu" , (u64 ) config->logical_block_size); +uds_log_debug ("Logical blocks = %llu" , logical_blocks); +uds_log_debug ("Physical block size = %llu" , (u64 ) block_size); +uds_log_debug ("Physical blocks = %llu" , config->physical_blocks); +uds_log_debug ("Block map cache blocks = %u" , config->cache_size); +uds_log_debug ("Block map maximum age = %u" , config->block_map_maximum_age); +uds_log_debug ("Deduplication = %s" , (config->deduplication ?"on" :"off" )); +uds_log_debug ("Compression = %s" , (config->compression ?"on" :"off" )); + + vdo =vdo_find_matching (vdo_uses_device, config); +if (vdo !=NULL ) { +uds_log_error ("Existing vdo already uses device %s" , + vdo->device_config->parent_device_name); + ti->error ="Cannot share storage device with already-running VDO" ; +return VDO_BAD_CONFIGURATION ; + } + + result =vdo_make (instance, config, &ti->error, &vdo); +if (result !=VDO_SUCCESS ) { +uds_log_error ("Could not create VDO device. (VDO error %d, message %s)" , + result, ti->error); +vdo_destroy (vdo); +return result; + } + + result =perform_admin_operation (vdo,PRE_LOAD_PHASE_START , pre_load_callback, + finish_operation_callback,"pre-load" ); +if (result !=VDO_SUCCESS ) { + ti->error = ((result ==VDO_INVALID_ADMIN_STATE ) ? +"Pre-load is only valid immediately after initialization" : +"Cannot load metadata from device" ); +uds_log_error ("Could not start VDO device. (VDO error %d, message %s)" , + result, ti->error); +vdo_destroy (vdo); +return result; + } + +set_device_config (ti, vdo, config); + vdo->device_config = config; +return VDO_SUCCESS ; +} + +/* Implements vdo_filter_fn. */ +static bool __must_check vdo_is_named (struct vdo *vdo,const void *context) +{ +struct dm_target *ti = vdo->device_config->owning_target; +const char *device_name =vdo_get_device_name (ti); + +return strcmp (device_name, context) ==0 ; +} + +/** +* get_bit_array_size() - Return the number of bytes needed to store a bit array of the specified +* capacity in an array of unsigned longs. +* @bit_count: The number of bits the array must hold. +* +* Return: the number of bytes needed for the array representation. +*/ +static size_t get_bit_array_size (unsigned int bit_count) +{ +/* Round up to a multiple of the word size and convert to a byte count. */ +return (BITS_TO_LONGS (bit_count) *sizeof (unsigned long )); +} + +/** +* grow_bit_array() - Re-allocate the bitmap word array so there will more instance numbers that +* can be allocated. +* +* Since the array is initially NULL, this also initializes the array the first time we allocate an +* instance number. +* +* Return: UDS_SUCCESS or an error code from the allocation +*/ +static int grow_bit_array (void ) +{ +unsigned int new_count =max (instances.bit_count +BIT_COUNT_INCREMENT , + (unsigned int )BIT_COUNT_MINIMUM ); +unsigned long *new_words; +int result; + + result =uds_reallocate_memory (instances.words, +get_bit_array_size (instances.bit_count), +get_bit_array_size (new_count), +"instance number bit array" , &new_words); +if (result !=UDS_SUCCESS ) +return result; + + instances.bit_count = new_count; + instances.words = new_words; +return UDS_SUCCESS ; +} + +/** +* allocate_instance() - Allocate an instance number. +* @instance_ptr: A point to hold the instance number +* +* Return: UDS_SUCCESS or an error code +* +* This function must be called while holding the instances lock. +*/ +static int allocate_instance (unsigned int *instance_ptr) +{ +unsigned int instance; +int result; + +/* If there are no unallocated instances, grow the bit array. */ +if (instances.count >= instances.bit_count) { + result =grow_bit_array (); +if (result !=UDS_SUCCESS ) +return result; + } + +/* +* There must be a zero bit somewhere now. Find it, starting just after the last instance +* allocated. +*/ + instance =find_next_zero_bit (instances.words, instances.bit_count, + instances.next); +if (instance >= instances.bit_count) { +/* Nothing free after next, so wrap around to instance zero. */ + instance =find_first_zero_bit (instances.words, instances.bit_count); + result =ASSERT (instance < instances.bit_count, +"impossibly, no zero bit found" ); +if (result !=UDS_SUCCESS ) +return result; + } + +__set_bit (instance, instances.words); + instances.count++; + instances.next = instance +1 ; + *instance_ptr = instance; +return UDS_SUCCESS ; +} + +static int construct_new_vdo_registered (struct dm_target *ti,unsigned int argc, +char **argv,unsigned int instance) +{ +int result; +struct device_config *config; + + result =parse_device_config (argc, argv, ti, &config); +if (result !=VDO_SUCCESS ) { +uds_log_error_strerror (result,"parsing failed: %s" , ti->error); +release_instance (instance); +return -EINVAL ; + } + +/* Beyond this point, the instance number will be cleaned up for us if needed */ + result =vdo_initialize (ti, instance, config); +if (result !=VDO_SUCCESS ) { +release_instance (instance); +free_device_config (config); +return vdo_map_to_system_error (result); + } + +return VDO_SUCCESS ; +} + +static int construct_new_vdo (struct dm_target *ti,unsigned int argc,char **argv) +{ +int result; +unsigned int instance; +struct registered_thread instance_thread; + +mutex_lock (&instances_lock); + result =allocate_instance (&instance); +mutex_unlock (&instances_lock); +if (result !=VDO_SUCCESS ) +return -ENOMEM ; + +uds_register_thread_device_id (&instance_thread, &instance); + result =construct_new_vdo_registered (ti, argc, argv, instance); +uds_unregister_thread_device_id (); +return result; +} + +/** +* check_may_grow_physical() - Callback to check that we're not in recovery mode, used in +* vdo_prepare_to_grow_physical(). +* @completion: The admin completion. +*/ +static void check_may_grow_physical (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; + +assert_admin_phase_thread (vdo, __func__); + +/* These checks can only be done from a vdo thread. */ +if (vdo_is_read_only (vdo)) +vdo_set_completion_result (completion,VDO_READ_ONLY ); + +if (vdo_in_recovery_mode (vdo)) +vdo_set_completion_result (completion,VDO_RETRY_AFTER_REBUILD ); + +finish_operation_callback (completion); +} + +static block_count_t get_partition_size (struct layout *layout,enum partition_id id) +{ +return vdo_get_known_partition (layout, id)->count; +} + +/** +* grow_layout() - Make the layout for growing a vdo. +* @vdo: The vdo preparing to grow. +* @old_size: The current size of the vdo. +* @new_size: The size to which the vdo will be grown. +* +* Return: VDO_SUCCESS or an error code. +*/ +static int grow_layout (struct vdo *vdo,block_count_t old_size,block_count_t new_size) +{ +int result; +block_count_t min_new_size; + +if (vdo->next_layout.size == new_size) { +/* We are already prepared to grow to the new size, so we're done. */ +return VDO_SUCCESS ; + } + +/* Make a copy completion if there isn't one */ +if (vdo->partition_copier ==NULL ) { + vdo->partition_copier =dm_kcopyd_client_create (NULL ); +if (vdo->partition_copier ==NULL ) +return -ENOMEM ; + } + +/* Free any unused preparation. */ +vdo_uninitialize_layout (&vdo->next_layout); + +/* +* Make a new layout with the existing partition sizes for everything but the slab depot +* partition. +*/ + result =vdo_initialize_layout (new_size, vdo->layout.start, +get_partition_size (&vdo->layout, +VDO_BLOCK_MAP_PARTITION ), +get_partition_size (&vdo->layout, +VDO_RECOVERY_JOURNAL_PARTITION ), +get_partition_size (&vdo->layout, +VDO_SLAB_SUMMARY_PARTITION ), + &vdo->next_layout); +if (result !=VDO_SUCCESS ) { +dm_kcopyd_client_destroy (uds_forget (vdo->partition_copier)); +return result; + } + +/* Ensure the new journal and summary are entirely within the added blocks. */ + min_new_size = (old_size + +get_partition_size (&vdo->next_layout, +VDO_SLAB_SUMMARY_PARTITION ) + +get_partition_size (&vdo->next_layout, +VDO_RECOVERY_JOURNAL_PARTITION )); +if (min_new_size > new_size) { +/* Copying the journal and summary would destroy some old metadata. */ +vdo_uninitialize_layout (&vdo->next_layout); +dm_kcopyd_client_destroy (uds_forget (vdo->partition_copier)); +return VDO_INCREMENT_TOO_SMALL ; + } + +return VDO_SUCCESS ; +} + +static int prepare_to_grow_physical (struct vdo *vdo,block_count_t new_physical_blocks) +{ +int result; +block_count_t current_physical_blocks = vdo->states.vdo.config.physical_blocks; + +uds_log_info ("Preparing to resize physical to %llu" , + (unsigned long long ) new_physical_blocks); +ASSERT_LOG_ONLY ((new_physical_blocks > current_physical_blocks), +"New physical size is larger than current physical size" ); + result =perform_admin_operation (vdo,PREPARE_GROW_PHYSICAL_PHASE_START , + check_may_grow_physical, + finish_operation_callback, +"prepare grow-physical" ); +if (result !=VDO_SUCCESS ) +return result; + + result =grow_layout (vdo, current_physical_blocks, new_physical_blocks); +if (result !=VDO_SUCCESS ) +return result; + + result =vdo_prepare_to_grow_slab_depot (vdo->depot, +vdo_get_known_partition (&vdo->next_layout, +VDO_SLAB_DEPOT_PARTITION )); +if (result !=VDO_SUCCESS ) { +vdo_uninitialize_layout (&vdo->next_layout); +return result; + } + +uds_log_info ("Done preparing to resize physical" ); +return VDO_SUCCESS ; +} + +/** +* validate_new_device_config() - Check whether a new device config represents a valid modification +* to an existing config. +* @to_validate: The new config to validate. +* @config: The existing config. +* @may_grow: Set to true if growing the logical and physical size of the vdo is currently +* permitted. +* @error_ptr: A pointer to hold the reason for any error. +* +* Return: VDO_SUCCESS or an error. +*/ +static int validate_new_device_config (struct device_config *to_validate, +struct device_config *config,bool may_grow, +char **error_ptr) +{ +if (to_validate->owning_target->begin != config->owning_target->begin) { + *error_ptr ="Starting sector cannot change" ; +return VDO_PARAMETER_MISMATCH ; + } + +if (to_validate->logical_block_size != config->logical_block_size) { + *error_ptr ="Logical block size cannot change" ; +return VDO_PARAMETER_MISMATCH ; + } + +if (to_validate->logical_blocks < config->logical_blocks) { + *error_ptr ="Can't shrink VDO logical size" ; +return VDO_PARAMETER_MISMATCH ; + } + +if (to_validate->cache_size != config->cache_size) { + *error_ptr ="Block map cache size cannot change" ; +return VDO_PARAMETER_MISMATCH ; + } + +if (to_validate->block_map_maximum_age != config->block_map_maximum_age) { + *error_ptr ="Block map maximum age cannot change" ; +return VDO_PARAMETER_MISMATCH ; + } + +if (memcmp (&to_validate->thread_counts, &config->thread_counts, +sizeof (struct thread_count_config )) !=0 ) { + *error_ptr ="Thread configuration cannot change" ; +return VDO_PARAMETER_MISMATCH ; + } + +if (to_validate->physical_blocks < config->physical_blocks) { + *error_ptr ="Removing physical storage from a VDO is not supported" ; +return VDO_NOT_IMPLEMENTED ; + } + +if (!may_grow && (to_validate->physical_blocks > config->physical_blocks)) { + *error_ptr ="VDO physical size may not grow in current state" ; +return VDO_NOT_IMPLEMENTED ; + } + +return VDO_SUCCESS ; +} + +static int prepare_to_modify (struct dm_target *ti,struct device_config *config, +struct vdo *vdo) +{ +int result; +bool may_grow = (vdo_get_admin_state (vdo) !=VDO_ADMIN_STATE_PRE_LOADED ); + + result =validate_new_device_config (config, vdo->device_config, may_grow, + &ti->error); +if (result !=VDO_SUCCESS ) +return -EINVAL ; + +if (config->logical_blocks > vdo->device_config->logical_blocks) { +block_count_t logical_blocks = vdo->states.vdo.config.logical_blocks; + +uds_log_info ("Preparing to resize logical to %llu" , + (unsigned long long ) config->logical_blocks); +ASSERT_LOG_ONLY ((config->logical_blocks > logical_blocks), +"New logical size is larger than current size" ); + + result =vdo_prepare_to_grow_block_map (vdo->block_map, + config->logical_blocks); +if (result !=VDO_SUCCESS ) { + ti->error ="Device vdo_prepare_to_grow_logical failed" ; +return result; + } + +uds_log_info ("Done preparing to resize logical" ); + } + +if (config->physical_blocks > vdo->device_config->physical_blocks) { + result =prepare_to_grow_physical (vdo, config->physical_blocks); +if (result !=VDO_SUCCESS ) { +if (result ==VDO_PARAMETER_MISMATCH ) { +/* +* If we don't trap this case, vdo_map_to_system_error() will remap +* it to -EIO, which is misleading and ahistorical. +*/ + result = -EINVAL ; + } + +if (result ==VDO_TOO_MANY_SLABS ) + ti->error ="Device vdo_prepare_to_grow_physical failed (specified physical size too big based on formatted slab size)" ; +else + ti->error ="Device vdo_prepare_to_grow_physical failed" ; + +return result; + } + } + +if (strcmp (config->parent_device_name, vdo->device_config->parent_device_name) !=0 ) { +const char *device_name =vdo_get_device_name (config->owning_target); + +uds_log_info ("Updating backing device of %s from %s to %s" , device_name, + vdo->device_config->parent_device_name, + config->parent_device_name); + } + +return VDO_SUCCESS ; +} + +static int update_existing_vdo (const char *device_name,struct dm_target *ti, +unsigned int argc,char **argv,struct vdo *vdo) +{ +int result; +struct device_config *config; + + result =parse_device_config (argc, argv, ti, &config); +if (result !=VDO_SUCCESS ) +return -EINVAL ; + +uds_log_info ("preparing to modify device '%s'" , device_name); + result =prepare_to_modify (ti, config, vdo); +if (result !=VDO_SUCCESS ) { +free_device_config (config); +return vdo_map_to_system_error (result); + } + +set_device_config (ti, vdo, config); +return VDO_SUCCESS ; +} + +static int vdo_ctr (struct dm_target *ti,unsigned int argc,char **argv) +{ +int result; +struct registered_thread allocating_thread, instance_thread; +const char *device_name; +struct vdo *vdo; + +uds_register_allocating_thread (&allocating_thread,NULL ); + device_name =vdo_get_device_name (ti); + vdo =vdo_find_matching (vdo_is_named, device_name); +if (vdo ==NULL ) { + result =construct_new_vdo (ti, argc, argv); + }else { +uds_register_thread_device_id (&instance_thread, &vdo->instance); + result =update_existing_vdo (device_name, ti, argc, argv, vdo); +uds_unregister_thread_device_id (); + } + +uds_unregister_allocating_thread (); +return result; +} + +static void vdo_dtr (struct dm_target *ti) +{ +struct device_config *config = ti->private; +struct vdo *vdo =uds_forget (config->vdo); + +list_del_init (&config->config_list); +if (list_empty (&vdo->device_config_list)) { +const char *device_name; + +/* This was the last config referencing the VDO. Free it. */ +unsigned int instance = vdo->instance; +struct registered_thread allocating_thread, instance_thread; + +uds_register_thread_device_id (&instance_thread, &instance); +uds_register_allocating_thread (&allocating_thread,NULL ); + + device_name =vdo_get_device_name (ti); +uds_log_info ("stopping device '%s'" , device_name); +if (vdo->dump_on_shutdown) +vdo_dump_all (vdo,"device shutdown" ); + +vdo_destroy (uds_forget (vdo)); +uds_log_info ("device '%s' stopped" , device_name); +uds_unregister_thread_device_id (); +uds_unregister_allocating_thread (); +release_instance (instance); + }else if (config == vdo->device_config) { +/* +* The VDO still references this config. Give it a reference to a config that isn't +* being destroyed. +*/ + vdo->device_config =list_first_entry (&vdo->device_config_list, + struct device_config, config_list); + } + +free_device_config (config); + ti->private =NULL ; +} + +static void vdo_presuspend (struct dm_target *ti) +{ +get_vdo_for_target (ti)->suspend_type = + (dm_noflush_suspending (ti) ?VDO_ADMIN_STATE_SUSPENDING :VDO_ADMIN_STATE_SAVING ); +} + +/** +* write_super_block_for_suspend() - Update the VDO state and save the super block. +* @completion: The admin completion +*/ +static void write_super_block_for_suspend (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; + +switch (vdo_get_state (vdo)) { +case VDO_DIRTY : +case VDO_NEW : +vdo_set_state (vdo,VDO_CLEAN ); +break ; + +case VDO_CLEAN : +case VDO_READ_ONLY_MODE : +case VDO_FORCE_REBUILD : +case VDO_RECOVERING : +case VDO_REBUILD_FOR_UPGRADE : +break ; + +case VDO_REPLAYING : +default : +vdo_continue_completion (completion,UDS_BAD_STATE ); +return ; + } + +vdo_save_components (vdo, completion); +} + +/** +* suspend_callback() - Callback to initiate a suspend, registered in vdo_postsuspend(). +* @completion: The sub-task completion. +*/ +static void suspend_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +struct admin_state *state = &vdo->admin.state; +int result; + +assert_admin_phase_thread (vdo, __func__); + +switch (advance_phase (vdo)) { +case SUSPEND_PHASE_START : +if (vdo_get_admin_state_code (state)->quiescent) { +/* Already suspended */ +break ; + } + +vdo_continue_completion (completion, +vdo_start_operation (state, vdo->suspend_type)); +return ; + +case SUSPEND_PHASE_PACKER : +/* +* If the VDO was already resumed from a prior suspend while read-only, some of the +* components may not have been resumed. By setting a read-only error here, we +* guarantee that the result of this suspend will be VDO_READ_ONLY and not +* VDO_INVALID_ADMIN_STATE in that case. +*/ +if (vdo_in_read_only_mode (vdo)) +vdo_set_completion_result (completion,VDO_READ_ONLY ); + +vdo_drain_packer (vdo->packer, completion); +return ; + +case SUSPEND_PHASE_DATA_VIOS : +drain_data_vio_pool (vdo->data_vio_pool, completion); +return ; + +case SUSPEND_PHASE_DEDUPE : +vdo_drain_hash_zones (vdo->hash_zones, completion); +return ; + +case SUSPEND_PHASE_FLUSHES : +vdo_drain_flusher (vdo->flusher, completion); +return ; + +case SUSPEND_PHASE_LOGICAL_ZONES : +/* +* Attempt to flush all I/O before completing post suspend work. We believe a +* suspended device is expected to have persisted all data written before the +* suspend, even if it hasn't been flushed yet. +*/ + result =vdo_synchronous_flush (vdo); +if (result !=VDO_SUCCESS ) +vdo_enter_read_only_mode (vdo, result); + +vdo_drain_logical_zones (vdo->logical_zones, +vdo_get_admin_state_code (state), completion); +return ; + +case SUSPEND_PHASE_BLOCK_MAP : +vdo_drain_block_map (vdo->block_map,vdo_get_admin_state_code (state), + completion); +return ; + +case SUSPEND_PHASE_JOURNAL : +vdo_drain_recovery_journal (vdo->recovery_journal, +vdo_get_admin_state_code (state), completion); +return ; + +case SUSPEND_PHASE_DEPOT : +vdo_drain_slab_depot (vdo->depot,vdo_get_admin_state_code (state), + completion); +return ; + +case SUSPEND_PHASE_READ_ONLY_WAIT : +vdo_wait_until_not_entering_read_only_mode (completion); +return ; + +case SUSPEND_PHASE_WRITE_SUPER_BLOCK : +if (vdo_is_state_suspending (state) || (completion->result !=VDO_SUCCESS )) { +/* If we didn't save the VDO or there was an error, we're done. */ +break ; + } + +write_super_block_for_suspend (completion); +return ; + +case SUSPEND_PHASE_END : +break ; + +default : +vdo_set_completion_result (completion,UDS_BAD_STATE ); + } + +finish_operation_callback (completion); +} + +static void vdo_postsuspend (struct dm_target *ti) +{ +struct vdo *vdo =get_vdo_for_target (ti); +struct registered_thread instance_thread; +const char *device_name; +int result; + +uds_register_thread_device_id (&instance_thread, &vdo->instance); + device_name =vdo_get_device_name (vdo->device_config->owning_target); +uds_log_info ("suspending device '%s'" , device_name); + +/* +* It's important to note any error here does not actually stop device-mapper from +* suspending the device. All this work is done post suspend. +*/ + result =perform_admin_operation (vdo,SUSPEND_PHASE_START , suspend_callback, + suspend_callback,"suspend" ); + +if ((result ==VDO_SUCCESS ) || (result ==VDO_READ_ONLY )) { +/* +* Treat VDO_READ_ONLY as a success since a read-only suspension still leaves the +* VDO suspended. +*/ +uds_log_info ("device '%s' suspended" , device_name); + }else if (result ==VDO_INVALID_ADMIN_STATE ) { +uds_log_error ("Suspend invoked while in unexpected state: %s" , +vdo_get_admin_state (vdo)->name); + }else { +uds_log_error_strerror (result,"Suspend of device '%s' failed" , + device_name); + } + +uds_unregister_thread_device_id (); +} + +/** +* was_new() - Check whether the vdo was new when it was loaded. +* @vdo: The vdo to query. +* +* Return: true if the vdo was new. +*/ +static bool was_new (const struct vdo *vdo) +{ +return (vdo->load_state ==VDO_NEW ); +} + +/** +* requires_repair() - Check whether a vdo requires recovery or rebuild. +* @vdo: The vdo to query. +* +* Return: true if the vdo must be repaired. +*/ +static bool __must_check requires_repair (const struct vdo *vdo) +{ +switch (vdo_get_state (vdo)) { +case VDO_DIRTY : +case VDO_FORCE_REBUILD : +case VDO_REPLAYING : +case VDO_REBUILD_FOR_UPGRADE : +return true; + +default : +return false; + } +} + +/** +* get_load_type() - Determine how the slab depot was loaded. +* @vdo: The vdo. +* +* Return: How the depot was loaded. +*/ +static enum slab_depot_load_type get_load_type (struct vdo *vdo) +{ +if (vdo_state_requires_read_only_rebuild (vdo->load_state)) +return VDO_SLAB_DEPOT_REBUILD_LOAD ; + +if (vdo_state_requires_recovery (vdo->load_state)) +return VDO_SLAB_DEPOT_RECOVERY_LOAD ; + +return VDO_SLAB_DEPOT_NORMAL_LOAD ; +} + +/** +* vdo_initialize_kobjects() - Initialize the vdo sysfs directory. +* @vdo: The vdo being initialized. +* +* Return: VDO_SUCCESS or an error code. +*/ +static int vdo_initialize_kobjects (struct vdo *vdo) +{ +int result; +struct dm_target *target = vdo->device_config->owning_target; +struct mapped_device *md =dm_table_get_md (target->table); + +kobject_init (&vdo->vdo_directory, &vdo_directory_type); + vdo->sysfs_added = true; + result =kobject_add (&vdo->vdo_directory, &disk_to_dev (dm_disk (md))->kobj, +"vdo" ); +if (result !=0 ) +return VDO_CANT_ADD_SYSFS_NODE ; + + result =vdo_add_dedupe_index_sysfs (vdo->hash_zones); +if (result !=0 ) +return VDO_CANT_ADD_SYSFS_NODE ; + +return vdo_add_sysfs_stats_dir (vdo); +} + +/** +* load_callback() - Callback to do the destructive parts of loading a VDO. +* @completion: The sub-task completion. +*/ +static void load_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +int result; + +assert_admin_phase_thread (vdo, __func__); + +switch (advance_phase (vdo)) { +case LOAD_PHASE_START : + result =vdo_start_operation (&vdo->admin.state,VDO_ADMIN_STATE_LOADING ); +if (result !=VDO_SUCCESS ) { +vdo_continue_completion (completion, result); +return ; + } + +/* Prepare the recovery journal for new entries. */ +vdo_open_recovery_journal (vdo->recovery_journal, vdo->depot, + vdo->block_map); +vdo_allow_read_only_mode_entry (completion); +return ; + +case LOAD_PHASE_STATS : +vdo_continue_completion (completion,vdo_initialize_kobjects (vdo)); +return ; + +case LOAD_PHASE_LOAD_DEPOT : +if (vdo_is_read_only (vdo)) { +/* +* In read-only mode we don't use the allocator and it may not even be +* readable, so don't bother trying to load it. +*/ +vdo_set_completion_result (completion,VDO_READ_ONLY ); +break ; + } + +if (requires_repair (vdo)) { +vdo_repair (completion); +return ; + } + +vdo_load_slab_depot (vdo->depot, + (was_new (vdo) ?VDO_ADMIN_STATE_FORMATTING : +VDO_ADMIN_STATE_LOADING ), + completion,NULL ); +return ; + +case LOAD_PHASE_MAKE_DIRTY : +vdo_set_state (vdo,VDO_DIRTY ); +vdo_save_components (vdo, completion); +return ; + +case LOAD_PHASE_PREPARE_TO_ALLOCATE : +vdo_initialize_block_map_from_journal (vdo->block_map, + vdo->recovery_journal); +vdo_prepare_slab_depot_to_allocate (vdo->depot,get_load_type (vdo), + completion); +return ; + +case LOAD_PHASE_SCRUB_SLABS : +if (vdo_state_requires_recovery (vdo->load_state)) +vdo_enter_recovery_mode (vdo); + +vdo_scrub_all_unrecovered_slabs (vdo->depot, completion); +return ; + +case LOAD_PHASE_DATA_REDUCTION : +WRITE_ONCE (vdo->compressing, vdo->device_config->compression); +if (vdo->device_config->deduplication) { +/* +* Don't try to load or rebuild the index first (and log scary error +* messages) if this is known to be a newly-formatted volume. +*/ +vdo_start_dedupe_index (vdo->hash_zones,was_new (vdo)); + } + + vdo->allocations_allowed = false; + fallthrough; + +case LOAD_PHASE_FINISHED : +break ; + +case LOAD_PHASE_DRAIN_JOURNAL : +vdo_drain_recovery_journal (vdo->recovery_journal,VDO_ADMIN_STATE_SAVING , + completion); +return ; + +case LOAD_PHASE_WAIT_FOR_READ_ONLY : +/* Avoid an infinite loop */ + completion->error_handler =NULL ; + vdo->admin.phase =LOAD_PHASE_FINISHED ; +vdo_wait_until_not_entering_read_only_mode (completion); +return ; + +default : +vdo_set_completion_result (completion,UDS_BAD_STATE ); + } + +finish_operation_callback (completion); +} + +/** +* handle_load_error() - Handle an error during the load operation. +* @completion: The admin completion. +* +* If at all possible, brings the vdo online in read-only mode. This handler is registered in +* vdo_preresume_registered(). +*/ +static void handle_load_error (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; + +if (vdo_requeue_completion_if_needed (completion, + vdo->thread_config.admin_thread)) +return ; + +if (vdo_state_requires_read_only_rebuild (vdo->load_state) && + (vdo->admin.phase ==LOAD_PHASE_MAKE_DIRTY )) { +uds_log_error_strerror (completion->result,"aborting load" ); + vdo->admin.phase =LOAD_PHASE_DRAIN_JOURNAL ; +load_callback (uds_forget (completion)); +return ; + } + +uds_log_error_strerror (completion->result, +"Entering read-only mode due to load error" ); + vdo->admin.phase =LOAD_PHASE_WAIT_FOR_READ_ONLY ; +vdo_enter_read_only_mode (vdo, completion->result); + completion->result =VDO_READ_ONLY ; +load_callback (completion); +} + +/** +* write_super_block_for_resume() - Update the VDO state and save the super block. +* @completion: The admin completion +*/ +static void write_super_block_for_resume (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; + +switch (vdo_get_state (vdo)) { +case VDO_CLEAN : +case VDO_NEW : +vdo_set_state (vdo,VDO_DIRTY ); +vdo_save_components (vdo, completion); +return ; + +case VDO_DIRTY : +case VDO_READ_ONLY_MODE : +case VDO_FORCE_REBUILD : +case VDO_RECOVERING : +case VDO_REBUILD_FOR_UPGRADE : +/* No need to write the super block in these cases */ +vdo_launch_completion (completion); +return ; + +case VDO_REPLAYING : +default : +vdo_continue_completion (completion,UDS_BAD_STATE ); + } +} + +/** +* resume_callback() - Callback to resume a VDO. +* @completion: The admin completion. +*/ +static void resume_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +int result; + +assert_admin_phase_thread (vdo, __func__); + +switch (advance_phase (vdo)) { +case RESUME_PHASE_START : + result =vdo_start_operation (&vdo->admin.state, +VDO_ADMIN_STATE_RESUMING ); +if (result !=VDO_SUCCESS ) { +vdo_continue_completion (completion, result); +return ; + } + +write_super_block_for_resume (completion); +return ; + +case RESUME_PHASE_ALLOW_READ_ONLY_MODE : +vdo_allow_read_only_mode_entry (completion); +return ; + +case RESUME_PHASE_DEDUPE : +vdo_resume_hash_zones (vdo->hash_zones, completion); +return ; + +case RESUME_PHASE_DEPOT : +vdo_resume_slab_depot (vdo->depot, completion); +return ; + +case RESUME_PHASE_JOURNAL : +vdo_resume_recovery_journal (vdo->recovery_journal, completion); +return ; + +case RESUME_PHASE_BLOCK_MAP : +vdo_resume_block_map (vdo->block_map, completion); +return ; + +case RESUME_PHASE_LOGICAL_ZONES : +vdo_resume_logical_zones (vdo->logical_zones, completion); +return ; + +case RESUME_PHASE_PACKER : + { +bool was_enabled =vdo_get_compressing (vdo); +bool enable = vdo->device_config->compression; + +if (enable != was_enabled) +WRITE_ONCE (vdo->compressing, enable); +uds_log_info ("compression is %s" , (enable ?"enabled" :"disabled" )); + +vdo_resume_packer (vdo->packer, completion); +return ; + } + +case RESUME_PHASE_FLUSHER : +vdo_resume_flusher (vdo->flusher, completion); +return ; + +case RESUME_PHASE_DATA_VIOS : +resume_data_vio_pool (vdo->data_vio_pool, completion); +return ; + +case RESUME_PHASE_END : +break ; + +default : +vdo_set_completion_result (completion,UDS_BAD_STATE ); + } + +finish_operation_callback (completion); +} + +/** +* grow_logical_callback() - Callback to initiate a grow logical. +* @completion: The admin completion. +* +* Registered in perform_grow_logical(). +*/ +static void grow_logical_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +int result; + +assert_admin_phase_thread (vdo, __func__); + +switch (advance_phase (vdo)) { +case GROW_LOGICAL_PHASE_START : +if (vdo_is_read_only (vdo)) { +uds_log_error_strerror (VDO_READ_ONLY , +"Can't grow logical size of a read-only VDO" ); +vdo_set_completion_result (completion,VDO_READ_ONLY ); +break ; + } + + result =vdo_start_operation (&vdo->admin.state, +VDO_ADMIN_STATE_SUSPENDED_OPERATION ); +if (result !=VDO_SUCCESS ) { +vdo_continue_completion (completion, result); +return ; + } + + vdo->states.vdo.config.logical_blocks = vdo->block_map->next_entry_count; +vdo_save_components (vdo, completion); +return ; + +case GROW_LOGICAL_PHASE_GROW_BLOCK_MAP : +vdo_grow_block_map (vdo->block_map, completion); +return ; + +case GROW_LOGICAL_PHASE_END : +break ; + +case GROW_LOGICAL_PHASE_ERROR : +vdo_enter_read_only_mode (vdo, completion->result); +break ; + +default : +vdo_set_completion_result (completion,UDS_BAD_STATE ); + } + +finish_operation_callback (completion); +} + +/** +* handle_logical_growth_error() - Handle an error during the grow physical process. +* @completion: The admin completion. +*/ +static void handle_logical_growth_error (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; + +if (vdo->admin.phase ==GROW_LOGICAL_PHASE_GROW_BLOCK_MAP ) { +/* +* We've failed to write the new size in the super block, so set our in memory +* config back to the old size. +*/ + vdo->states.vdo.config.logical_blocks = vdo->block_map->entry_count; +vdo_abandon_block_map_growth (vdo->block_map); + } + + vdo->admin.phase =GROW_LOGICAL_PHASE_ERROR ; +grow_logical_callback (completion); +} + +/** +* perform_grow_logical() - Grow the logical size of the vdo. +* @vdo: The vdo to grow. +* @new_logical_blocks: The size to which the vdo should be grown. +* +* Context: This method may only be called when the vdo has been suspended and must not be called +* from a base thread. +* +* Return: VDO_SUCCESS or an error. +*/ +static int perform_grow_logical (struct vdo *vdo,block_count_t new_logical_blocks) +{ +int result; + +if (vdo->device_config->logical_blocks == new_logical_blocks) { +/* +* A table was loaded for which we prepared to grow, but a table without that +* growth was what we are resuming with. +*/ +vdo_abandon_block_map_growth (vdo->block_map); +return VDO_SUCCESS ; + } + +uds_log_info ("Resizing logical to %llu" , + (unsigned long long ) new_logical_blocks); +if (vdo->block_map->next_entry_count != new_logical_blocks) +return VDO_PARAMETER_MISMATCH ; + + result =perform_admin_operation (vdo,GROW_LOGICAL_PHASE_START , + grow_logical_callback, + handle_logical_growth_error,"grow logical" ); +if (result !=VDO_SUCCESS ) +return result; + +uds_log_info ("Logical blocks now %llu" , (unsigned long long ) new_logical_blocks); +return VDO_SUCCESS ; +} + +static void copy_callback (int read_err,unsigned long write_err,void *context) +{ +struct vdo_completion *completion = context; +int result = (((read_err ==0 ) && (write_err ==0 )) ?VDO_SUCCESS : -EIO ); + +vdo_continue_completion (completion, result); +} + +static void partition_to_region (struct partition *partition,struct vdo *vdo, +struct dm_io_region *region) +{ +physical_block_number_t pbn = partition->offset - vdo->geometry.bio_offset; + + *region = (struct dm_io_region ) { + .bdev =vdo_get_backing_device (vdo), + .sector = pbn *VDO_SECTORS_PER_BLOCK , + .count = partition->count *VDO_SECTORS_PER_BLOCK , + }; +} + +/** +* copy_partition() - Copy a partition from the location specified in the current layout to that in +* the next layout. +* @vdo: The vdo preparing to grow. +* @id: The ID of the partition to copy. +* @parent: The completion to notify when the copy is complete. +*/ +static void copy_partition (struct vdo *vdo,enum partition_id id, +struct vdo_completion *parent) +{ +struct dm_io_region read_region, write_regions[1 ]; +struct partition *from =vdo_get_known_partition (&vdo->layout, id); +struct partition *to =vdo_get_known_partition (&vdo->next_layout, id); + +partition_to_region (from, vdo, &read_region); +partition_to_region (to, vdo, &write_regions[0 ]); +dm_kcopyd_copy (vdo->partition_copier, &read_region,1 , write_regions,0 , + copy_callback, parent); +} + +/** +* grow_physical_callback() - Callback to initiate a grow physical. +* @completion: The admin completion. +* +* Registered in perform_grow_physical(). +*/ +static void grow_physical_callback (struct vdo_completion *completion) +{ +struct vdo *vdo = completion->vdo; +int result; + +assert_admin_phase_thread (vdo, __func__); + +switch (advance_phase (vdo)) { +case GROW_PHYSICAL_PHASE_START : +if (vdo_is_read_only (vdo)) { +uds_log_error_strerror (VDO_READ_ONLY , +"Can't grow physical size of a read-only VDO" ); +vdo_set_completion_result (completion,VDO_READ_ONLY ); +break ; + } + + result =vdo_start_operation (&vdo->admin.state, +VDO_ADMIN_STATE_SUSPENDED_OPERATION ); +if (result !=VDO_SUCCESS ) { +vdo_continue_completion (completion, result); +return ; + } + +/* Copy the journal into the new layout. */ +copy_partition (vdo,VDO_RECOVERY_JOURNAL_PARTITION , completion); +return ; + +case GROW_PHYSICAL_PHASE_COPY_SUMMARY : +copy_partition (vdo,VDO_SLAB_SUMMARY_PARTITION , completion); +return ; + +case GROW_PHYSICAL_PHASE_UPDATE_COMPONENTS : +vdo_uninitialize_layout (&vdo->layout); + vdo->layout = vdo->next_layout; +uds_forget (vdo->next_layout.head); + vdo->states.vdo.config.physical_blocks = vdo->layout.size; +vdo_update_slab_depot_size (vdo->depot); +vdo_save_components (vdo, completion); +return ; + +case GROW_PHYSICAL_PHASE_USE_NEW_SLABS : +vdo_use_new_slabs (vdo->depot, completion); +return ; + +case GROW_PHYSICAL_PHASE_END : + vdo->depot->summary_origin = +vdo_get_known_partition (&vdo->layout, +VDO_SLAB_SUMMARY_PARTITION )->offset; + vdo->recovery_journal->origin = +vdo_get_known_partition (&vdo->layout, +VDO_RECOVERY_JOURNAL_PARTITION )->offset; +break ; + +case GROW_PHYSICAL_PHASE_ERROR : +vdo_enter_read_only_mode (vdo, completion->result); +break ; + +default : +vdo_set_completion_result (completion,UDS_BAD_STATE ); + } + +vdo_uninitialize_layout (&vdo->next_layout); +finish_operation_callback (completion); +} + +/** +* handle_physical_growth_error() - Handle an error during the grow physical process. +* @completion: The sub-task completion. +*/ +static void handle_physical_growth_error (struct vdo_completion *completion) +{ + completion->vdo->admin.phase =GROW_PHYSICAL_PHASE_ERROR ; +grow_physical_callback (completion); +} + +/** +* perform_grow_physical() - Grow the physical size of the vdo. +* @vdo: The vdo to resize. +* @new_physical_blocks: The new physical size in blocks. +* +* Context: This method may only be called when the vdo has been suspended and must not be called +* from a base thread. +* +* Return: VDO_SUCCESS or an error. +*/ +static int perform_grow_physical (struct vdo *vdo,block_count_t new_physical_blocks) +{ +int result; +block_count_t new_depot_size, prepared_depot_size; +block_count_t old_physical_blocks = vdo->states.vdo.config.physical_blocks; + +/* Skip any noop grows. */ +if (old_physical_blocks == new_physical_blocks) +return VDO_SUCCESS ; + +if (new_physical_blocks != vdo->next_layout.size) { +/* +* Either the VDO isn't prepared to grow, or it was prepared to grow to a different +* size. Doing this check here relies on the fact that the call to this method is +* done under the dmsetup message lock. +*/ +vdo_uninitialize_layout (&vdo->next_layout); +vdo_abandon_new_slabs (vdo->depot); +return VDO_PARAMETER_MISMATCH ; + } + +/* Validate that we are prepared to grow appropriately. */ + new_depot_size = +vdo_get_known_partition (&vdo->next_layout,VDO_SLAB_DEPOT_PARTITION )->count; + prepared_depot_size = (vdo->depot->new_slabs ==NULL ) ?0 : vdo->depot->new_size; +if (prepared_depot_size != new_depot_size) +return VDO_PARAMETER_MISMATCH ; + + result =perform_admin_operation (vdo,GROW_PHYSICAL_PHASE_START , + grow_physical_callback, + handle_physical_growth_error,"grow physical" ); +if (result !=VDO_SUCCESS ) +return result; + +uds_log_info ("Physical block count was %llu, now %llu" , + (unsigned long long ) old_physical_blocks, + (unsigned long long ) new_physical_blocks); +return VDO_SUCCESS ; +} + +/** +* apply_new_vdo_configuration() - Attempt to make any configuration changes from the table being +* resumed. +* @vdo: The vdo being resumed. +* @config: The new device configuration derived from the table with which the vdo is being +* resumed. +* +* Return: VDO_SUCCESS or an error. +*/ +static int __must_check apply_new_vdo_configuration (struct vdo *vdo, +struct device_config *config) +{ +int result; + + result =perform_grow_logical (vdo, config->logical_blocks); +if (result !=VDO_SUCCESS ) { +uds_log_error ("grow logical operation failed, result = %d" , result); +return result; + } + + result =perform_grow_physical (vdo, config->physical_blocks); +if (result !=VDO_SUCCESS ) +uds_log_error ("resize operation failed, result = %d" , result); + +return result; +} + +static int vdo_preresume_registered (struct dm_target *ti,struct vdo *vdo) +{ +struct device_config *config = ti->private; +const char *device_name =vdo_get_device_name (ti); +block_count_t backing_blocks; +int result; + + backing_blocks =get_underlying_device_block_count (vdo); +if (backing_blocks < config->physical_blocks) { +/* FIXME: can this still happen? */ +uds_log_error ("resume of device '%s' failed: backing device has %llu blocks but VDO physical size is %llu blocks" , + device_name, (unsigned long long ) backing_blocks, + (unsigned long long ) config->physical_blocks); +return -EINVAL ; + } + +if (vdo_get_admin_state (vdo) ==VDO_ADMIN_STATE_PRE_LOADED ) { +uds_log_info ("starting device '%s'" , device_name); + result =perform_admin_operation (vdo,LOAD_PHASE_START , load_callback, + handle_load_error,"load" ); +if ((result !=VDO_SUCCESS ) && (result !=VDO_READ_ONLY )) { +/* +* Something has gone very wrong. Make sure everything has drained and +* leave the device in an unresumable state. +*/ +uds_log_error_strerror (result, +"Start failed, could not load VDO metadata" ); + vdo->suspend_type =VDO_ADMIN_STATE_STOPPING ; +perform_admin_operation (vdo,SUSPEND_PHASE_START , + suspend_callback, suspend_callback, +"suspend" ); +return result; + } + +/* Even if the VDO is read-only, it is now able to handle read requests. */ +uds_log_info ("device '%s' started" , device_name); + } + +uds_log_info ("resuming device '%s'" , device_name); + +/* If this fails, the VDO was not in a state to be resumed. This should never happen. */ + result =apply_new_vdo_configuration (vdo, config); +BUG_ON (result ==VDO_INVALID_ADMIN_STATE ); + +/* +* Now that we've tried to modify the vdo, the new config *is* the config, whether the +* modifications worked or not. +*/ + vdo->device_config = config; + +/* +* Any error here is highly unexpected and the state of the vdo is questionable, so we mark +* it read-only in memory. Because we are suspended, the read-only state will not be +* written to disk. +*/ +if (result !=VDO_SUCCESS ) { +uds_log_error_strerror (result, +"Commit of modifications to device '%s' failed" , + device_name); +vdo_enter_read_only_mode (vdo, result); +return result; + } + +if (vdo_get_admin_state (vdo)->normal) { +/* The VDO was just started, so we don't need to resume it. */ +return VDO_SUCCESS ; + } + + result =perform_admin_operation (vdo,RESUME_PHASE_START , resume_callback, + resume_callback,"resume" ); +BUG_ON (result ==VDO_INVALID_ADMIN_STATE ); +if (result ==VDO_READ_ONLY ) { +/* Even if the vdo is read-only, it has still resumed. */ + result =VDO_SUCCESS ; + } + +if (result !=VDO_SUCCESS ) +uds_log_error ("resume of device '%s' failed with error: %d" , device_name, + result); + +return result; +} + +static int vdo_preresume (struct dm_target *ti) +{ +struct registered_thread instance_thread; +struct vdo *vdo =get_vdo_for_target (ti); +int result; + +uds_register_thread_device_id (&instance_thread, &vdo->instance); + result =vdo_preresume_registered (ti, vdo); +if ((result ==VDO_PARAMETER_MISMATCH ) || (result ==VDO_INVALID_ADMIN_STATE )) + result = -EINVAL ; +uds_unregister_thread_device_id (); +return vdo_map_to_system_error (result); +} + +static void vdo_resume (struct dm_target *ti) +{ +struct registered_thread instance_thread; + +uds_register_thread_device_id (&instance_thread, + &get_vdo_for_target (ti)->instance); +uds_log_info ("device '%s' resumed" ,vdo_get_device_name (ti)); +uds_unregister_thread_device_id (); +} + +/* +* If anything changes that affects how user tools will interact with vdo, update the version +* number and make sure documentation about the change is complete so tools can properly update +* their management code. +*/ +static struct target_type vdo_target_bio = { + .features =DM_TARGET_SINGLETON , + .name ="vdo" , + .version = {8 ,2 ,0 }, + .module =THIS_MODULE , + .ctr = vdo_ctr, + .dtr = vdo_dtr, + .io_hints = vdo_io_hints, + .iterate_devices = vdo_iterate_devices, + .map = vdo_map_bio, + .message = vdo_message, + .status = vdo_status, + .presuspend = vdo_presuspend, + .postsuspend = vdo_postsuspend, + .preresume = vdo_preresume, + .resume = vdo_resume, +}; + +static bool dm_registered; + +static void vdo_module_destroy (void ) +{ +uds_log_debug ("unloading" ); + +if (dm_registered) +dm_unregister_target (&vdo_target_bio); + +ASSERT_LOG_ONLY (instances.count ==0 , +"should have no instance numbers still in use, but have %u" , + instances.count); +uds_free (instances.words); +memset (&instances,0 ,sizeof (struct instance_tracker )); + +uds_log_info ("unloaded version %s" ,CURRENT_VERSION ); +} + +static int __init vdo_init (void ) +{ +int result =0 ; + +/* +* UDS module level initialization must be done first, as VDO initialization depends on it +*/ +uds_initialize_thread_device_registry (); +uds_memory_init (); +uds_init_sysfs (); + +vdo_initialize_device_registry_once (); +uds_log_info ("loaded version %s" ,CURRENT_VERSION ); + +/* Add VDO errors to the already existing set of errors in UDS. */ + result =vdo_register_status_codes (); +if (result !=UDS_SUCCESS ) { +uds_log_error ("vdo_register_status_codes failed %d" , result); +vdo_module_destroy (); +return result; + } + + result =dm_register_target (&vdo_target_bio); +if (result <0 ) { +uds_log_error ("dm_register_target failed %d" , result); +vdo_module_destroy (); +return result; + } + dm_registered = true; + +return result; +} + +static void __exit vdo_exit(void) +{ +vdo_module_destroy (); +/* +* UDS module level exit processing must be done after all VDO module exit processing is +* complete. +*/ +uds_put_sysfs (); +uds_memory_exit (); +} + +module_init (vdo_init); +module_exit (vdo_exit); + +MODULE_DESCRIPTION (DM_NAME " target for transparent deduplication" ); +MODULE_AUTHOR ("Red Hat, Inc." ); +MODULE_LICENSE ("GPL" );
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 40/40 dm vdo: add MAINTAINERS file entry
In reply to Matthew Sakai--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -6002,6 +6002,15 @@ F: include/linux/device-mapper.h F: include/linux/dm-*.h F: include/uapi/linux/dm-*.h +DEVICE-MAPPER VDO TARGET +M: Matthew Sakai <[email protected]> +M: [email protected] +L: [email protected] +S: Maintained +F: Documentation/admin-guide/device-mapper/vdo*.rst +F: drivers/md/dm-vdo-target.c +F: drivers/md/dm-vdo/ + DEVLINK M: Jiri Pirko <[email protected]> L: [email protected]
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 37/40 dm vdo: add debugging support
In reply to Matthew SakaiAdd support for dumping detailed vdo state to the kernel log via a dmsetup message. The dump code is not thread-safe and is generally intended for use only when the vdo is hung.
--- drivers/md/dm-vdo/dump.c | 278 +++++++++++++++++++++++++++++++++++++++ drivers/md/dm-vdo/dump.h | 17 +++ 2 files changed, 295 insertions(+) create mode 100644 drivers/md/dm-vdo/dump.c create mode 100644 drivers/md/dm-vdo/dump.h
drivers/md/dm-vdo/dump.c new file +278 -0
@@ -0,0 +1,278 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include "dump.h" + +#include <linux/module.h> + +#include "memory-alloc.h" +#include "string-utils.h" + +#include "constants.h" +#include "data-vio.h" +#include "dedupe.h" +#include "funnel-workqueue.h" +#include "io-submitter.h" +#include "logger.h" +#include "types.h" +#include "vdo.h" + +enum dump_options { +/* Work queues */ +SHOW_QUEUES , +/* Memory pools */ +SHOW_VIO_POOL , +/* Others */ +SHOW_VDO_STATUS , +/* This one means an option overrides the "default" choices, instead of altering them. */ +SKIP_DEFAULT +}; + +enum dump_option_flags { +/* Work queues */ +FLAG_SHOW_QUEUES = (1 <<SHOW_QUEUES ), +/* Memory pools */ +FLAG_SHOW_VIO_POOL = (1 <<SHOW_VIO_POOL ), +/* Others */ +FLAG_SHOW_VDO_STATUS = (1 <<SHOW_VDO_STATUS ), +/* Special */ +FLAG_SKIP_DEFAULT = (1 <<SKIP_DEFAULT ) +}; + +enum { +FLAGS_ALL_POOLS = (FLAG_SHOW_VIO_POOL ), +DEFAULT_DUMP_FLAGS = (FLAG_SHOW_QUEUES |FLAG_SHOW_VDO_STATUS ) +}; + +static inline bool is_arg_string (const char *arg,const char *this_option) +{ +/* convention seems to be case-independent options */ +return strncasecmp (arg, this_option,strlen (this_option)) ==0 ; +} + +static void do_dump (struct vdo *vdo,unsigned int dump_options_requested, +const char *why) +{ +u32 active, maximum; +s64 outstanding; + +uds_log_info ("%s dump triggered via %s" ,UDS_LOGGING_MODULE_NAME , why); + active =get_data_vio_pool_active_requests (vdo->data_vio_pool); + maximum =get_data_vio_pool_maximum_requests (vdo->data_vio_pool); + outstanding = (atomic64_read (&vdo->stats.bios_submitted) - +atomic64_read (&vdo->stats.bios_completed)); +uds_log_info ("%u device requests outstanding (max %u), %lld bio requests outstanding, device '%s'" , + active, maximum, outstanding, +vdo_get_device_name (vdo->device_config->owning_target)); +if (((dump_options_requested &FLAG_SHOW_QUEUES ) !=0 ) && (vdo->threads !=NULL )) { +thread_id_t id; + +for (id =0 ; id < vdo->thread_config.thread_count; id++) +vdo_dump_work_queue (vdo->threads[id].queue); + } + +vdo_dump_hash_zones (vdo->hash_zones); +dump_data_vio_pool (vdo->data_vio_pool, + (dump_options_requested &FLAG_SHOW_VIO_POOL ) !=0 ); +if ((dump_options_requested &FLAG_SHOW_VDO_STATUS ) !=0 ) +vdo_dump_status (vdo); + +uds_report_memory_usage (); +uds_log_info ("end of %s dump" ,UDS_LOGGING_MODULE_NAME ); +} + +static int parse_dump_options (unsigned int argc,char *const *argv, +unsigned int *dump_options_requested_ptr) +{ +unsigned int dump_options_requested =0 ; + +static const struct { +const char *name; +unsigned int flags; + } option_names[] = { + {"viopool" ,FLAG_SKIP_DEFAULT |FLAG_SHOW_VIO_POOL }, + {"vdo" ,FLAG_SKIP_DEFAULT |FLAG_SHOW_VDO_STATUS }, + {"pools" ,FLAG_SKIP_DEFAULT |FLAGS_ALL_POOLS }, + {"queues" ,FLAG_SKIP_DEFAULT |FLAG_SHOW_QUEUES }, + {"threads" ,FLAG_SKIP_DEFAULT |FLAG_SHOW_QUEUES }, + {"default" ,FLAG_SKIP_DEFAULT |DEFAULT_DUMP_FLAGS }, + {"all" , ~0 }, + }; + +bool options_okay = true; +unsigned int i; + +for (i =1 ; i < argc; i++) { +unsigned int j; + +for (j =0 ; j <ARRAY_SIZE (option_names); j++) { +if (is_arg_string (argv[i], option_names[j].name)) { + dump_options_requested |= option_names[j].flags; +break ; + } + } +if (j ==ARRAY_SIZE (option_names)) { +uds_log_warning ("dump option name '%s' unknown" , argv[i]); + options_okay = false; + } + } +if (!options_okay) +return -EINVAL ; +if ((dump_options_requested &FLAG_SKIP_DEFAULT ) ==0 ) + dump_options_requested |=DEFAULT_DUMP_FLAGS ; + *dump_options_requested_ptr = dump_options_requested; +return 0 ; +} + +/* Dump as specified by zero or more string arguments. */ +int vdo_dump (struct vdo *vdo,unsigned int argc,char *const *argv,const char *why) +{ +unsigned int dump_options_requested =0 ; +int result =parse_dump_options (argc, argv, &dump_options_requested); + +if (result !=0 ) +return result; + +do_dump (vdo, dump_options_requested, why); +return 0 ; +} + +/* Dump everything we know how to dump */ +void vdo_dump_all (struct vdo *vdo,const char *why) +{ +do_dump (vdo, ~0 , why); +} + +/* +* Dump out the data_vio waiters on a wait queue. +* wait_on should be the label to print for queue (e.g. logical or physical) +*/ +static void dump_vio_waiters (struct wait_queue *queue,char *wait_on) +{ +struct waiter *waiter, *first =vdo_get_first_waiter (queue); +struct data_vio *data_vio; + +if (first ==NULL ) +return ; + + data_vio =waiter_as_data_vio (first); + +uds_log_info (" %s is locked. Waited on by: vio %px pbn %llu lbn %llu d-pbn %llu lastOp %s" , + wait_on, data_vio, data_vio->allocation.pbn, data_vio->logical.lbn, + data_vio->duplicate.pbn,get_data_vio_operation_name (data_vio)); + +for (waiter = first->next_waiter; waiter != first; waiter = waiter->next_waiter) { + data_vio =waiter_as_data_vio (waiter); +uds_log_info (" ... and : vio %px pbn %llu lbn %llu d-pbn %llu lastOp %s" , + data_vio, data_vio->allocation.pbn, data_vio->logical.lbn, + data_vio->duplicate.pbn, +get_data_vio_operation_name (data_vio)); + } +} + +/* +* Encode various attributes of a data_vio as a string of one-character flags. This encoding is for +* logging brevity: +* +* R => vio completion result not VDO_SUCCESS +* W => vio is on a wait queue +* D => vio is a duplicate +* p => vio is a partial block operation +* z => vio is a zero block +* d => vio is a discard +* +* The common case of no flags set will result in an empty, null-terminated buffer. If any flags +* are encoded, the first character in the string will be a space character. +*/ +static void encode_vio_dump_flags (struct data_vio *data_vio,char buffer[8 ]) +{ +char *p_flag = buffer; + *p_flag++ =' ' ; +if (data_vio->vio.completion.result !=VDO_SUCCESS ) + *p_flag++ ='R' ; +if (data_vio->waiter.next_waiter !=NULL ) + *p_flag++ ='W' ; +if (data_vio->is_duplicate) + *p_flag++ ='D' ; +if (data_vio->is_partial) + *p_flag++ ='p' ; +if (data_vio->is_zero) + *p_flag++ ='z' ; +if (data_vio->remaining_discard >0 ) + *p_flag++ ='d' ; +if (p_flag == &buffer[1 ]) { +/* No flags, so remove the blank space. */ + p_flag = buffer; + } + *p_flag ='\0' ; +} + +/* Implements buffer_dump_function. */ +void dump_data_vio (void *data) +{ +struct data_vio *data_vio = data; + +/* +* This just needs to be big enough to hold a queue (thread) name and a function name (plus +* a separator character and NUL). The latter is limited only by taste. +* +* In making this static, we're assuming only one "dump" will run at a time. If more than +* one does run, the log output will be garbled anyway. +*/ +static char vio_completion_dump_buffer[100 +MAX_VDO_WORK_QUEUE_NAME_LEN ]; +/* Another static buffer... log10(256) = 2.408+, round up: */ +enum {DIGITS_PER_U64 =1 +sizeof (u64) *2409 /1000 }; + +static char vio_block_number_dump_buffer[sizeof ("P L D" ) +3 *DIGITS_PER_U64 ]; +static char vio_flush_generation_buffer[sizeof (" FG" ) +DIGITS_PER_U64 ]; +static char flags_dump_buffer[8 ]; + +/* +* We're likely to be logging a couple thousand of these lines, and in some circumstances +* syslogd may have trouble keeping up, so keep it BRIEF rather than user-friendly. +*/ +vdo_dump_completion_to_buffer (&data_vio->vio.completion, + vio_completion_dump_buffer, +sizeof (vio_completion_dump_buffer)); +if (data_vio->is_duplicate) { +snprintf (vio_block_number_dump_buffer, +sizeof (vio_block_number_dump_buffer),"P%llu L%llu D%llu" , + data_vio->allocation.pbn, data_vio->logical.lbn, + data_vio->duplicate.pbn); + }else if (data_vio_has_allocation (data_vio)) { +snprintf (vio_block_number_dump_buffer, +sizeof (vio_block_number_dump_buffer),"P%llu L%llu" , + data_vio->allocation.pbn, data_vio->logical.lbn); + }else { +snprintf (vio_block_number_dump_buffer, +sizeof (vio_block_number_dump_buffer),"L%llu" , + data_vio->logical.lbn); + } + +if (data_vio->flush_generation !=0 ) { +snprintf (vio_flush_generation_buffer, +sizeof (vio_flush_generation_buffer)," FG%llu" , + data_vio->flush_generation); + }else { + vio_flush_generation_buffer[0 ] =0 ; + } + +encode_vio_dump_flags (data_vio, flags_dump_buffer); + +uds_log_info (" vio %px %s%s %s %s%s" , data_vio, + vio_block_number_dump_buffer, + vio_flush_generation_buffer, +get_data_vio_operation_name (data_vio), + vio_completion_dump_buffer, + flags_dump_buffer); +/* +* might want info on: wantUDSAnswer / operation / status +* might want info on: bio / bios_merged +*/ + +dump_vio_waiters (&data_vio->logical.waiters,"lbn" ); + +/* might want to dump more info from vio here */ +}
drivers/md/dm-vdo/dump.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright 2023 Red Hat +*/ + +#ifndef VDO_DUMP_H +#define VDO_DUMP_H + +#include "types.h" + +int vdo_dump (struct vdo *vdo,unsigned int argc,char *const *argv,const char *why); + +void vdo_dump_all (struct vdo *vdo,const char *why); + +void dump_data_vio (void *data); + +#endif /* VDO_DUMP_H */
Signature · 1 line
2.40.0
Matthew Sakai
maintainerPATCH v5 3/40 dm vdo: add memory allocation utilities
In reply to Matthew SakaiThis patch adds standardized allocation macros and memory tracking tools to track and report any allocated memory that is not freed. This makes it easier to ensure that the vdo target does not leak memory.
This patch also adds utilities for controlling whether certain threads are allowed to allocate memory, since memory allocation during certain critical code sections can cause the vdo target to deadlock.
--- drivers/md/dm-vdo/memory-alloc.c | 442 +++++++++++++++++++++++++++++++ drivers/md/dm-vdo/memory-alloc.h | 162 +++++++++++ 2 files changed, 604 insertions(+) create mode 100644 drivers/md/dm-vdo/memory-alloc.c create mode 100644 drivers/md/dm-vdo/memory-alloc.h
drivers/md/dm-vdo/memory-alloc.c new file +442 -0
@@ -0,0 +1,442 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright 2023 Red Hat +*/ + +#include <linux/delay.h> +#include <linux/mm.h> +#include <linux/sched/mm.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> + +#include "logger.h" +#include "memory-alloc.h" +#include "permassert.h" + +/* +* UDS and VDO keep track of which threads are allowed to allocate memory freely, and which threads +* must be careful to not do a memory allocation that does an I/O request. The allocating_threads +* threads_registry and its associated methods implement this tracking. +*/ +static struct thread_registry allocating_threads; + +static bool allocations_allowed (void ) +{ +const bool *pointer =uds_lookup_thread (&allocating_threads); + +return (pointer !=NULL ) ? *pointer : false; +} + +/* +* Register the current thread as an allocating thread. +* +* An optional flag location can be supplied indicating whether, at any given point in time, the +* threads associated with that flag should be allocating storage. If the flag is false, a message +* will be logged. +* +* If no flag is supplied, the thread is always allowed to allocate storage without complaint. +* +* @new_thread: registered_thread structure to use for the current thread +* @flag_ptr: Location of the allocation-allowed flag +*/ +void uds_register_allocating_thread (struct registered_thread *new_thread, +const bool *flag_ptr) +{ +if (flag_ptr ==NULL ) { +static const bool allocation_always_allowed = true; + + flag_ptr = &allocation_always_allowed; + } + +uds_register_thread (&allocating_threads, new_thread, flag_ptr); +} + +/* Unregister the current thread as an allocating thread. */ +void uds_unregister_allocating_thread (void ) +{ +uds_unregister_thread (&allocating_threads); +} + +/* +* We track how much memory has been allocated and freed. When we unload the module, we log an +* error if we have not freed all the memory that we allocated. Nearly all memory allocation and +* freeing is done using this module. +* +* We do not use kernel functions like the kvasprintf() method, which allocate memory indirectly +* using kmalloc. +* +* These data structures and methods are used to track the amount of memory used. +*/ + +/* +* We allocate very few large objects, and allocation/deallocation isn't done in a +* performance-critical stage for us, so a linked list should be fine. +*/ +struct vmalloc_block_info { +void *ptr; +size_t size; +struct vmalloc_block_info *next; +}; + +static struct { +spinlock_t lock; +size_t kmalloc_blocks; +size_t kmalloc_bytes; +size_t vmalloc_blocks; +size_t vmalloc_bytes; +size_t peak_bytes; +struct vmalloc_block_info *vmalloc_list; +} memory_stats __cacheline_aligned; + +static void update_peak_usage (void ) +{ +size_t total_bytes = memory_stats.kmalloc_bytes + memory_stats.vmalloc_bytes; + +if (total_bytes > memory_stats.peak_bytes) + memory_stats.peak_bytes = total_bytes; +} + +static void add_kmalloc_block (size_t size) +{ +unsigned long flags; + +spin_lock_irqsave (&memory_stats.lock, flags); + memory_stats.kmalloc_blocks++; + memory_stats.kmalloc_bytes += size; +update_peak_usage (); +spin_unlock_irqrestore (&memory_stats.lock, flags); +} + +static void remove_kmalloc_block (size_t size) +{ +unsigned long flags; + +spin_lock_irqsave (&memory_stats.lock, flags); + memory_stats.kmalloc_blocks--; + memory_stats.kmalloc_bytes -= size; +spin_unlock_irqrestore (&memory_stats.lock, flags); +} + +static void add_vmalloc_block (struct vmalloc_block_info *block) +{ +unsigned long flags; + +spin_lock_irqsave (&memory_stats.lock, flags); + block->next = memory_stats.vmalloc_list; + memory_stats.vmalloc_list = block; + memory_stats.vmalloc_blocks++; + memory_stats.vmalloc_bytes += block->size; +update_peak_usage (); +spin_unlock_irqrestore (&memory_stats.lock, flags); +} + +static void remove_vmalloc_block (void *ptr) +{ +struct vmalloc_block_info *block; +struct vmalloc_block_info **block_ptr; +unsigned long flags; + +spin_lock_irqsave (&memory_stats.lock, flags); +for (block_ptr = &memory_stats.vmalloc_list; + (block = *block_ptr) !=NULL ; + block_ptr = &block->next) { +if (block->ptr == ptr) { + *block_ptr = block->next; + memory_stats.vmalloc_blocks--; + memory_stats.vmalloc_bytes -= block->size; +break ; + } + } + +spin_unlock_irqrestore (&memory_stats.lock, flags); +if (block !=NULL ) +uds_free (block); +else +uds_log_info ("attempting to remove ptr %px not found in vmalloc list" , ptr); +} + +/* +* Determine whether allocating a memory block should use kmalloc or __vmalloc. +* +* vmalloc can allocate any integral number of pages. +* +* kmalloc can allocate any number of bytes up to a configured limit, which defaults to 8 megabytes +* on some systems. kmalloc is especially good when memory is being both allocated and freed, and +* it does this efficiently in a multi CPU environment. +* +* kmalloc usually rounds the size of the block up to the next power of two, so when the requested +* block is bigger than PAGE_SIZE / 2 bytes, kmalloc will never give you less space than the +* corresponding vmalloc allocation. Sometimes vmalloc will use less overhead than kmalloc. +* +* The advantages of kmalloc do not help out UDS or VDO, because we allocate all our memory up +* front and do not free and reallocate it. Sometimes we have problems using kmalloc, because the +* Linux memory page map can become so fragmented that kmalloc will not give us a 32KB chunk. We +* have used vmalloc as a backup to kmalloc in the past, and a follow-up vmalloc of 32KB will work. +* But there is no strong case to be made for using kmalloc over vmalloc for these size chunks. +* +* The kmalloc/vmalloc boundary is set at 4KB, and kmalloc gets the 4KB requests. There is no +* strong reason for favoring either kmalloc or vmalloc for 4KB requests, except that tracking +* vmalloc statistics uses a linked list implementation. Using a simple test, this choice of +* boundary results in 132 vmalloc calls. Using vmalloc for requests of exactly 4KB results in an +* additional 6374 vmalloc calls, which is much less efficient for tracking. +* +* @size: How many bytes to allocate +*/ +static inline bool use_kmalloc (size_t size) +{ +return size <=PAGE_SIZE ; +} + +/* +* Allocate storage based on memory size and alignment, logging an error if the allocation fails. +* The memory will be zeroed. +* +* @size: The size of an object +* @align: The required alignment +* @what: What is being allocated (for error logging) +* @ptr: A pointer to hold the allocated memory +* +* Return: UDS_SUCCESS or an error code +*/ +int uds_allocate_memory (size_t size,size_t align,const char *what,void *ptr) +{ +/* +* The __GFP_RETRY_MAYFAIL flag means the VM implementation will retry memory reclaim +* procedures that have previously failed if there is some indication that progress has +* been made elsewhere. It can wait for other tasks to attempt high level approaches to +* freeing memory such as compaction (which removes fragmentation) and page-out. There is +* still a definite limit to the number of retries, but it is a larger limit than with +* __GFP_NORETRY. Allocations with this flag may fail, but only when there is genuinely +* little unused memory. While these allocations do not directly trigger the OOM killer, +* their failure indicates that the system is likely to need to use the OOM killer soon. +* The caller must handle failure, but can reasonably do so by failing a higher-level +* request, or completing it only in a much less efficient manner. +*/ +const gfp_t gfp_flags =GFP_KERNEL | __GFP_ZERO | __GFP_RETRY_MAYFAIL; +unsigned int noio_flags; +bool allocations_restricted = !allocations_allowed (); +unsigned long start_time; +void *p =NULL ; + +if (ptr ==NULL ) +return UDS_INVALID_ARGUMENT ; + +if (size ==0 ) { + *((void **) ptr) =NULL ; +return UDS_SUCCESS ; + } + +if (allocations_restricted) + noio_flags =memalloc_noio_save (); + + start_time = jiffies; +if (use_kmalloc (size) && (align <PAGE_SIZE )) { + p =kmalloc (size, gfp_flags | __GFP_NOWARN); +if (p ==NULL ) { +/* +* It is possible for kmalloc to fail to allocate memory because there is +* no page available (see VDO-3688). A short sleep may allow the page +* reclaimer to free a page. +*/ +fsleep (1000 ); + p =kmalloc (size, gfp_flags); + } + +if (p !=NULL ) +add_kmalloc_block (ksize (p)); + }else { +struct vmalloc_block_info *block; + + if (uds_allocate(1, struct vmalloc_block_info, __func__, &block) == +UDS_SUCCESS ) { +/* +* It is possible for __vmalloc to fail to allocate memory because there +* are no pages available (see VDO-3661). A short sleep may allow the page +* reclaimer to free enough pages for a small allocation. +* +* For larger allocations, the page_alloc code is racing against the page +* reclaimer. If the page reclaimer can stay ahead of page_alloc, the +* __vmalloc will succeed. But if page_alloc overtakes the page reclaimer, +* the allocation fails. It is possible that more retries will succeed. +*/ +for (;;) { + p =__vmalloc (size, gfp_flags | __GFP_NOWARN); + +if (p !=NULL ) +break ; + +if (jiffies_to_msecs (jiffies - start_time) >1000 ) { +/* Try one more time, logging a failure for this call. */ + p =__vmalloc (size, gfp_flags); +break ; + } + +fsleep (1000 ); + } + +if (p ==NULL ) { +uds_free (block); + }else { + block->ptr = p; + block->size =PAGE_ALIGN (size); +add_vmalloc_block (block); + } + } + } + +if (allocations_restricted) +memalloc_noio_restore (noio_flags); + +if (unlikely (p ==NULL )) { +uds_log_error ("Could not allocate %zu bytes for %s in %u msecs" , + size, what,jiffies_to_msecs (jiffies - start_time)); +return -ENOMEM ; + } + + *((void **) ptr) = p; +return UDS_SUCCESS ; +} + +/* +* Allocate storage based on memory size, failing immediately if the required memory is not +* available. The memory will be zeroed. +* +* @size: The size of an object. +* @what: What is being allocated (for error logging) +* +* Return: pointer to the allocated memory, or NULL if the required space is not available. +*/ +void *uds_allocate_memory_nowait (size_t size,const char *what__maybe_unused ) +{ +void *p =kmalloc (size,GFP_NOWAIT | __GFP_ZERO); + +if (p !=NULL ) +add_kmalloc_block (ksize (p)); + +return p; +} + +void uds_free (void *ptr) +{ +if (ptr !=NULL ) { +if (is_vmalloc_addr (ptr)) { +remove_vmalloc_block (ptr); +vfree (ptr); + }else { +remove_kmalloc_block (ksize (ptr)); +kfree (ptr); + } + } +} + +/* +* Reallocate dynamically allocated memory. There are no alignment guarantees for the reallocated +* memory. If the new memory is larger than the old memory, the new space will be zeroed. +* +* @ptr: The memory to reallocate. +* @old_size: The old size of the memory +* @size: The new size to allocate +* @what: What is being allocated (for error logging) +* @new_ptr: A pointer to hold the reallocated pointer +* +* Return: UDS_SUCCESS or an error code +*/ +int uds_reallocate_memory (void *ptr,size_t old_size,size_t size,const char *what, +void *new_ptr) +{ +int result; + +if (size ==0 ) { +uds_free (ptr); + *(void **) new_ptr =NULL ; +return UDS_SUCCESS ; + } + + result =uds_allocate (size, char, what, new_ptr); +if (result !=UDS_SUCCESS ) +return result; + +if (ptr !=NULL ) { +if (old_size < size) + size = old_size; + +memcpy (*((void **) new_ptr), ptr, size); +uds_free (ptr); + } + +return UDS_SUCCESS ; +} + +int uds_duplicate_string (const char *string,const char *what,char **new_string) +{ +int result; +u8 *dup; + + result =uds_allocate (strlen (string) +1 , u8, what, &dup); +if (result !=UDS_SUCCESS ) +return result; + +memcpy (dup, string,strlen (string) +1 ); + *new_string = dup; +return UDS_SUCCESS ; +} + +void uds_memory_init (void ) +{ +spin_lock_init (&memory_stats.lock); +uds_initialize_thread_registry (&allocating_threads); +} + +void uds_memory_exit (void ) +{ +ASSERT_LOG_ONLY (memory_stats.kmalloc_bytes ==0 , +"kmalloc memory used (%zd bytes in %zd blocks) is returned to the kernel" , + memory_stats.kmalloc_bytes, memory_stats.kmalloc_blocks); +ASSERT_LOG_ONLY (memory_stats.vmalloc_bytes ==0 , +"vmalloc memory used (%zd bytes in %zd blocks) is returned to the kernel" , + memory_stats.vmalloc_bytes, memory_stats.vmalloc_blocks); +uds_log_debug ("peak usage %zd bytes" , memory_stats.peak_bytes); +} + +void uds_get_memory_stats (u64 *bytes_used,u64 *peak_bytes_used) +{ +unsigned long flags; + +spin_lock_irqsave (&memory_stats.lock, flags); + *bytes_used = memory_stats.kmalloc_bytes + memory_stats.vmalloc_bytes; + *peak_bytes_used = memory_stats.peak_bytes; +spin_unlock_irqrestore (&memory_stats.lock, flags); +} + +/* +* Report stats on any allocated memory that we're tracking. Not all allocation types are +* guaranteed to be tracked in bytes (e.g., bios). +*/ +void uds_report_memory_usage (void ) +{ +unsigned long flags; +u64 kmalloc_blocks; +u64 kmalloc_bytes; +u64 vmalloc_blocks; +u64 vmalloc_bytes; +u64 peak_usage; +u64 total_bytes; + +spin_lock_irqsave (&memory_stats.lock, flags); + kmalloc_blocks = memory_stats.kmalloc_blocks; + kmalloc_bytes = memory_stats.kmalloc_bytes; + vmalloc_blocks = memory_stats.vmalloc_blocks; + vmalloc_bytes = memory_stats.vmalloc_bytes; + peak_usage = memory_stats.peak_bytes; +spin_unlock_irqrestore (&memory_stats.lock, flags); + total_bytes = kmalloc_bytes + vmalloc_bytes; +uds_log_info ("current module memory tracking (actual allocation sizes, not requested):" ); +uds_log_info (" %llu bytes in %llu kmalloc blocks" , + (unsigned long long ) kmalloc_bytes, + (unsigned long long ) kmalloc_blocks); +uds_log_info (" %llu bytes in %llu vmalloc blocks" , + (unsigned long long ) vmalloc_bytes, + (unsigned long long ) vmalloc_blocks); +uds_log_info (" total %llu bytes, peak usage %llu bytes" , + (unsigned long long ) total_bytes, (unsigned long long ) peak_usage); +}
drivers/md/dm-vdo/memory-alloc.h new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.40.0
Re: [PATCH v5 01/40] dm: add documentation for dm-vdo target
In reply to Matthew SakaiHi,
Quoted 510 lines · Matthew Sakai
This adds the admin-guide documentation for dm-vdo.
vdo.rstis the guide to using dm-vdo. vdo-design is an overview of the design of dm-vdo.Co-developed-by J. corwin Coburn <[email protected]>Signed-off-by J. corwin Coburn <[email protected]>Signed-off-by Matthew Sakai <[email protected]>--- .../admin-guide/device-mapper/vdo-design.rst | 415 ++++++++++++++++++ .../admin-guide/device-mapper/vdo.rst | 388 ++++++++++++++++ 2 files changed, 803 insertions(+) create mode 100644 Documentation/admin-guide/device-mapper/vdo-design.rst create mode 100644 Documentation/admin-guide/device-mapper/vdo.rstDocumentation/admin-guide/device-mapper/vdo-design.rst new file +415
-0@@ -0,0 +1,415 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +================ +Design of dm-vdo +================ + +The dm-vdo (virtual data optimizer) target provides inline deduplication, +compression, zero-block elimination, and thin provisioning. A dm-vdo target +can be backed by up to 256TB of storage, and can present a logical size of +up to 4PB. This target was originally developed at Permabit Technology +Corp. starting in 2009. It was first released in 2013 and has been used in +production environments ever since. It was made open-source in 2017 after +Permabit was acquired by Red Hat. This document describes the design of +dm-vdo. For usage, see vdo.rst in the same directory as this file. + +Because deduplication rates fall drastically as the block size increases, a +vdo target has a maximum block size of 4K. However, it can achieve +deduplication rates of 254:1, i.e. up to 254 copies of a given 4K block can +reference a single 4K of actual storage. It can achieve compression rates +of 14:1. All zero blocks consume no storage at all. + +Theory of Operation +=================== + +The design of dm-vdo is based on the idea that deduplication is a two-part +problem. The first is to recognize duplicate data. The second is to avoid +storing multiple copies of those duplicates. Therefore, dm-vdo has two main +parts: a deduplication index (called UDS) that is used to discover +duplicate data, and a data store with a reference counted block map that +maps from logical block addresses to the actual storage location of the +data. + +Zones and Threading +------------------- + +Due to the complexity of data optimization, the number of metadata +structures involved in a single write operation to a vdo target is larger +than most other targets. Furthermore, because vdo must operate on small +block sizes in order to achieve good deduplication rates, acceptable +performance can only be achieved through parallelism. Therefore, vdo's +design attempts to be lock-free. Most of a vdo's main data structures are +designed to be easily divided into "zones" such that any given bio must +only access a single zone of any zoned structure. Safety with minimal +locking is achieved by ensuring that during normal operation, each zone is +assigned to a specific thread, and only that thread will access the portion +of that data structure in that zone. Associated with each thread is a work +queue. Each bio is associated with a request object which can be added to a +work queue when the next phase of its operation requires access to the +structures in the zone associated with that queue. Although each structure +may be divided into zones, this division is not reflected in the on-disk +representation of each data structure. Therefore, the number of zones for +each structure, and hence the number of threads, is configured each time a +vdo target is started. + +The Deduplication Index +----------------------- + +In order to identify duplicate data efficiently, vdo was designed to +leverage some common characteristics of duplicate data. From empirical +observations, we gathered two key insights. The first is that in most data +sets with significant amounts of duplicate data, the duplicates tend to +have temporal locality. When a duplicate appears, it is more likely that +other duplicates will be detected, and that those duplicates will have been +written at about the same time. This is why the index keeps records in +temporal order. The second insight is that new data is more likely to +duplicate recent data than it is to duplicate older data and in general, +there are diminishing returns to looking further back in time. Therefore, +when the index is full, it should cull its oldest records to make space for +new ones. Another important idea behind the design of the index is that the +ultimate goal of deduplication is to reduce storage costs. Since there is a +trade-off between the storage saved and the resources expended to achieve +those savings, vdo does not attempt to find every last duplicate block. It +is sufficient to find and eliminate most of the redundancy. + +Each block of data is hashed to produce a 16-byte block name. An index +record consists of this block name paired with the presumed location of +that data on the underlying storage. However, it is not possible to +guarantee that the index is accurate. Most often, this occurs because it is +too costly to update the index when a block is over-written or discarded. +Doing so would require either storing the block name along with the blocks, +which is difficult to do efficiently in block-based storage, or reading and +rehashing each block before overwriting it. Inaccuracy can also result from +a hash collision where two different blocks have the same name. In +practice, this is extremely unlikely, but because vdo does not use a +cryptographic hash, a malicious workload can be constructed. Because of +these inaccuracies, vdo treats the locations in the index as hints, and +reads each indicated block to verify that it is indeed a duplicate before +sharing the existing block with a new one. + +Records are collected into groups called chapters. New records are added to +the newest chapter, called the open chapter. This chapter is stored in a +format optimized for adding and modifying records, and the content of the +open chapter is not finalized until it runs out of space for new records. +When the open chapter fills up, it is closed and a new open chapter is +created to collect new records. + +Closing a chapter converts it to a different format which is optimized for +writing. The records are written to a series of record pages based on the +order in which they were received. This means that records with temporal +locality should be on a small number of pages, reducing the I/O required to +retrieve them. The chapter also compiles an index that indicates which +record page contains any given name. This index means that a request for a +name can determine exactly which record page may contain that record, +without having to load the entire chapter from storage. This index uses +only a subset of the block name as its key, so it cannot guarantee that an +index entry refers to the desired block name. It can only guarantee that if +there is a record for this name, it will be on the indicated page. The +contents of a closed chapter are never altered in any way; these chapters +are read-only structures. + +Once enough records have been written to fill up all the available index +space, the oldest chapter gets removed to make space for new chapters. Any +time a request finds a matching record in the index, that record is copied +to the open chapter. This ensures that useful block names remain available +in the index, while unreferenced block names are forgotten. + +In order to find records in older chapters, the index also maintains a +higher level structure called the volume index, which contains entries +mapping a block name to the chapter containing its newest record. This +mapping is updated as records for the block name are copied or updated, +ensuring that only the newer record for a given block name is findable. +Older records for a block name can no longer be found even though they have +not been deleted. Like the chapter index, the volume index uses only a +subset of the block name as its key and can not definitively say that a +record exists for a name. It can only say which chapter would contain the +record if a record exists. The volume index is stored entirely in memory +and is saved to storage only when the vdo target is shut down. + +From the viewpoint of a request for a particular block name, first it will +look up the name in the volume index which will indicate either that the +record is new, or which chapter to search. If the latter, the request looks +up its name in the chapter index to determine if the record is new, or +which record page to search. Finally, if not new, the request will look for +its record on the indicated record page. This process may require up to two +page reads per request (one for the chapter index page and one for the +request page). However, recently accessed pages are cached so that these +page reads can be amortized across many block name requests. + +The volume index and the chapter indexes are implemented using a +memory-efficient structure called a delta index. Instead of storing the +entire key (the block name) for each entry, the entries are sorted by name +and only the difference between adjacent keys (the delta) is stored. +Because we expect the hashes to be evenly distributed, the size of the +deltas follows an exponential distribution. Because of this distribution, +the deltas are expressed in a Huffman code to take up even less space. The +entire sorted list of keys is called a delta list. This structure allows +the index to use many fewer bytes per entry than a traditional hash table, +but it is slightly more expensive to look up entries, because a request +must read every entry in a delta list to add up the deltas in order to find +the record it needs. The delta index reduces this lookup cost by splitting +its key space into many sub-lists, each starting at a fixed key value, so +that each individual list is short. + +The default index size can hold 64 million records, corresponding to about +256GB. This means that the index can identify duplicate data if the +original data was written within the last 256GB of writes. This range is +called the deduplication window. If new writes duplicate data that is older +than that, the index will not be able to find it because the records of the +older data have been removed. So when writing a 200 GB file to a vdo +target, and then immediately writing it again, the two copies will +deduplicate perfectly. Doing the same with a 500 GB file will result in no +deduplication, because the beginning of the file will no longer be in the +index by the time the second write begins (assuming there is no duplication +within the file itself). + +If you anticipate a data workload that will see useful deduplication beyond +the 256GB threshold, vdo can be configured to use a larger index with a +correspondingly larger deduplication window. (This configuration can only +be set when the target is created, not altered later. It is important to +consider the expected workload for a vdo target before configuring it.) +There are two ways to do this. + +One way is to increase the memory size of the index, which also increases +the amount of backing storage required. Doubling the size of the index will +double the length of the deduplication window at the expense of doubling +the storage size and the memory requirements. + +The other way is to enable sparse indexing. Sparse indexing increases the +deduplication window by a factor of 10, at the expense of also increasing +the storage size by a factor of 10. However with sparse indexing, the +memory requirements do not increase; the trade-off is slightly more +computation per request, and a slight decrease in the amount of +deduplication detected. (For workloads with significant amounts of +duplicate data, sparse indexing will detect 97-99% of the deduplication +that a standard, or "dense", index will detect.) + +The Data Store +-------------- + +The data store is implemented by three main data structures, all of which +work in concert to reduce or amortize metadata updates across as many data +writes as possible. + +*The Slab Depot* + +Most of the vdo volume belongs to the slab depot. The depot contains a +collection of slabs. The slabs can be up to 32GB, and are divided into +three sections. Most of a slab consists of a linear sequence of 4K blocks. +These blocks are used either to store data, or to hold portions of the +block map (see below). In addition to the data blocks, each slab has a set +of reference counters, using 1 byte for each data block. Finally each slab +has a journal. Reference updates are written to the slab journal, which is +written out one block at a time as each block fills. A copy of the +reference counters are kept in memory, and are written out a block at a +time, in oldest-dirtied-order whenever there is a need to reclaim slab +journal space. The journal is used both to ensure that the main recovery +journal (see below) can regularly free up space, and also to amortize the +cost of updating individual reference blocks. + +Each slab is independent of every other. They are assigned to "physical +zones" in round-robin fashion. If there are P physical zones, then slab n +is assigned to zone n mod P. + +The slab depot maintains an additional small data structure, the "slab +summary," which is used to reduce the amount of work needed to come back +online after a crash. The slab summary maintains an entry for each slab +indicating whether or not the slab has ever been used, whether it is clean +(i.e. all of its reference count updates have been persisted to storage), +and approximately how full it is. During recovery, each physical zone will +attempt to recover at least one slab, stopping whenever it has recovered a +slab which has some free blocks. Once each zone has some space (or has +determined that none is available), the target can resume normal operation +in a degraded mode. Read and write requests can be serviced, perhaps with +degraded performance, while the remainder of the dirty slabs are recovered. + +*The Block Map* + +The block map contains the logical to physical mapping. It can be thought +of as an array with one entry per logical address. Each entry is 5 bytes, +36 bits of which contain the physical block number which holds the data for +the given logical address. The other 4 bits are used to indicate the nature +of the mapping. Of the 16 possible states, one represents a logical address +which is unmapped (i.e. it has never been written, or has been discarded), +one represents an uncompressed block, and the other 14 states are used to +indicate that the mapped data is compressed, and which of the compression +slots in the compressed block this logical address maps to (see below). + +In practice, the array of mapping entries is divided into "block map +pages," each of which fits in a single 4K block. Each block map page +consists of a header, and 812 mapping entries (812 being the number that +fit). Each mapping page is actually a leaf of a radix tree which consists +of block map pages at each level. There are 60 radix trees which are +assigned to "logical zones" in round robin fashion (if there are L logical +zones, tree n will belong to zone n mod L). At each level, the trees are +interleaved, so logical addresses 0-811 belong to tree 0, logical addresses +812-1623 belong to tree 1, and so on. The interleaving is maintained all +the way up the forest. 60 was chosen as the number of trees because it is +highly composite and hence results in an evenly distributed number of trees +per zone for a large number of possible logical zone counts. The storage +for the 60 tree roots is allocated at format time. All other block map +pages are allocated out of the slabs as needed. This flexible allocation +avoids the need to pre-allocate space for the entire set of logical +mappings and also makes growing the logical size of a vdo easy to +implement. + +In operation, the block map maintains two caches. It is prohibitive to keep +the entire leaf level of the trees in memory, so each logical zone +maintains its own cache of leaf pages. The size of this cache is +configurable at target start time. The second cache is allocated at start +time, and is large enough to hold all the non-leaf pages of the entire +block map. This cache is populated as needed. + +*The Recovery Journal* + +The recovery journal is used to amortize updates across the block map and +slab depot. Each write request causes an entry to be made in the journal. +Entries are either "data remappings" or "block map remappings." For a data +remapping, the journal records the logical address affected and its old and +new physical mappings. For a block map remapping, the journal records the +block map page number and the physical block allocated for it (block map +pages are never reclaimed, so the old mapping is always 0). Each journal +entry and the data write it represents must be stable on disk before the +other metadata structures may be updated to reflect the operation. + +*Write Path* + +A write bio is first assigned a "data_vio," the request object which will +operate on behalf of the bio. (A "vio," from Vdo I/O, is vdo's wrapper for +bios; metadata operations use a vio, whereas submitted bios require the +much larger data_vio.) There is a fixed pool of 2048 data_vios. This number +was chosen both to bound the amount of work that is required to recover +from a crash, and because measurements indicate that increasing it consumes +more resources, but does not improve performance. These measurements have +been, and should continue to be, revisited over time. + +Once a data_vio is assigned, the following steps are performed: + +1. The bio's data is checked to see if it is all zeros, and copied if not. + +2. A lock is obtained on the logical address of the bio. Because + deduplication involves sharing blocks, it is vital to prevent + simultaneous modifications of the same block. + +3. The block map tree is traversed, loading any non-leaf pages which cover + the logical address and are not already in memory. If any of these + pages, or the leaf page which covers the logical address have not been + allocated, and the block is not all zeros, they are allocated at this + time. + +4. If the block is a zero block, skip to step 9. Otherwise, an attempt is + made to allocate a free data block. + +5. If an allocation was obtained, the bio is acknowledged. + +6. The bio's data is hashed. + +7. The data_vio obtains or joins a "hash lock," which represents all of + the bios currently writing the same data. + +8. If the hash lock does not already have a data_vio acting as its agent, + the current one assumes that role. As the agent: + + a) The index is queried. + + b) If an entry is found, the indicated block is read and compared + to the data being written. + + c) If the data matches, we have identified duplicate data. As many + of the data_vios as there are references available for that + block (including the agent) are shared. If there are more + data_vios in the hash lock than there are references available, + one of them becomes the new agent and continues as if there was + no duplicate found. + + d) If no duplicate was found, and the agent in the hash lock does + not have an allocation (fron step 3), another data_vio in the + hash lock will become the agent and write the data. If no + data_vio in the hash lock has an allocation, the data_vios will + be marked out of space and go to step 13 for cleanup. + + If there is an allocation, the data being written will be + compressed. If the compressed size is sufficiently small, the + data_vio will go to the packer where it may be placed in a bin + along with other data_vios. + + e) Once a bin is full, either because it is out of space, or + because all 14 of its slots are in use, it is written out. + + f) Each data_vio from the bin just written is the agent of some + hash lock, it will now proceed to treat the just written + compressed block as if it were a duplicate and share it with as + many other data_vios in its hash lock as possible. + + g) If the agent's data is not compressed, it will attempt to write + its data to the block it has allocated. + + h) If the data was written, this new block is treated as a + duplicate and shared as much as possible with any other + data_vios in the hash lock. + + i) If the agent wrote new data (whether compressed or not), the + index is updated to reflect the new entry. + +9. The block map is queried to determine the previous mapping of the + logical address. + +10. An entry is made in the recovery journal. The data_vio will block in + the journal until a flush has completed to ensure the data it may have + written is stable. It must also wait until its journal entry is stable + on disk. (Journal writes are all issued with the FUA bit set.) + +11. Once the recovery journal entry is stable, the data_vio makes two slab + journal entries: an increment entry for the new mapping, and a + decrement entry for the old mapping, if that mapping was non-zero. For + correctness during recovery, the slab journal entries in any given slab + journal must be in the same order as the corresponding recovery journal + entries. Therefore, if the two entries are in different zones, they are + made concurrently, and if they are in the same zone, the increment is + always made before the decrement in order to avoid underflow. After + each slab journal entry is made in memory, the associated reference + count is also updated in memory. Each of these updates will get written + out as needed. (Slab journal blocks are written out either when they + are full, or when the recovery journal requests they do so in order to + allow the recovery journal to free up space; reference count blocks are + written out whenever the associated slab journal requests they do so in + order to free up slab journal space.) + +12. Once all the reference count updates are done, the block map is updated + and the write is complete. + +13. If the data_vio did not use its allocation, it releases the allocated + block, the hash lock (if it has one), and its logical lock. The + data_vio then returns to the pool. + +*Read Path* + +Reads are much simpler than writes. After a data_vio is assigned to the +bio, and the logical lock is obtained, the block map is queried. If the +block is mapped, the appropriate physical block is read, and if necessary, +decompressed. + +*Recovery* + +When a vdo is restarted after a crash, it will attempt to recover from the +recovery journal. During the pre-resume phase of the next start, the +recovery journal is read. The increment portion of valid entries are played +into the block map. Next, valid entries are played, in order as required, +into the slab journals. Finally, each physical zone attempts to replay at +least one slab journal to reconstruct the reference counts of one slab. +Once each zone has some free space (or has determined that it has none), +the vdo comes back online, while the remainder of the slab journals are +used to reconstruct the rest of the reference counts. + +*Read-only Rebuild* + +If a vdo encounters an unrecoverable error, it will enter read-only mode. +This mode indicates that some previously acknowledged data may have been +lost. The vdo may be instructed to rebuild as best it can in order to +return to a writable state. However, this is never done automatically due +to the likelihood that data has been lost. During a read-only rebuild, the +block map is recovered from the recovery journal as before. However, the +reference counts are not rebuilt from the slab journals. Rather, the +reference counts are zeroed, and then the entire block map is traversed, +and the reference counts are updated from it. While this may lose some +data, it ensures that the block map and reference counts are consistent.Documentation/admin-guide/device-mapper/vdo.rst new file +68
-0@@ -0,0 +1,388 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +dm-vdo +====== + +The dm-vdo (virtual data optimizer) device mapper target provides +block-level deduplication, compression, and thin provisioning. As a device +mapper target, it can add these features to the storage stack, compatible +with any file system. The vdo target does not protect against data +corruption, relying instead on integrity protection of the storage below +it. It is strongly recommended that lvm be used to manage vdo volumes. See +lvmvdo(7). + +Userspace component +=================== + +Formatting a vdo volume requires the use of the 'vdoformat' tool, available +at: + +https://github.com/dm-vdo/vdo/ + +In most cases, a vdo target will recover from a crash automatically the +next time it is started. In cases where it encountered an unrecoverable +error (either during normal operation or crash recovery) the target will +enter or come up in read-only mode. Because read-only mode is indicative of +data-loss, a positive action must be taken to bring vdo out of read-only +mode. The 'vdoforcerebuild' tool, available from the same repo, is used to +prepare a read-only vdo to exit read-only mode. After running this tool, +the vdo target will rebuild its metadata the next time it is +started. Although some data may be lost, the rebuilt vdo's metadata will be +internally consistent and the target will be writable again. + +The repo also contains additional userspace tools which can be used to +inspect a vdo target's on-disk metadata. Fortunately, these tools are +rarely needed except by dm-vdo developers. + +Target interface +================ + +Table line +---------- + +:: + + <offset> <logical device size> vdo V4 <storage device> + <storage device size> <minimum I/O size> <block map cache size> + <block map era length> [optional arguments] + + +Required parameters: + + offset: + The offset, in sectors, at which the vdo volume's logical + space begins. + + logical device size: + The size of the device which the vdo volume will service, + in sectors. Must match the current logical size of the vdo + volume. + + storage device: + The device holding the vdo volume's data and metadata. + + storage device size: + The size of the device holding the vdo volume, as a number + of 4096-byte blocks. Must match the current size of the vdo + volume. +
Documentation/admin-guide/device-mapper/vdo.rst line 78
Quoted 10 lines
Documentation/admin-guide/device-mapper/vdo.rst new file +10
-0+ minimum I/O size: + The minimum I/O size for this vdo volume to accept, in + bytes. Valid values are 512 or 4096. The recommended value + is 4096. + + block map cache size: + The size of the block map cache, as a number of 4096-byte + blocks. The minimum and recommended value is 32768 blocks. + If the logical thread count is non-zero, the cache size + must be at least 4096 blocks per logical thread.
If I understand correctly the minimum of 32768 blocks results in the 128 MB metadata cache mentioned in 'Tuning', which allows to access up to 100 GB of logical space.
Is there a strict reason for this minimum? I'm evaluating to use vdo on systems with a relatively small vdo volume (say 4GB) and 'only' 4-8 GB of RAM. The 128 MB of metadata cache would be a sizeable chunk of that, which could make the use of vdo infeasible.
Quoted 100 lines
Documentation/admin-guide/device-mapper/vdo.rst new file +100
-0+ + block map era length: + The speed with which the block map cache writes out + modified block map pages. A smaller era length is likely to + reduce the amount of time spent rebuilding, at the cost of + increased block map writes during normal operation. The + maximum and recommended value is 16380; the minimum value + is 1. + +Optional parameters: +-------------------- +Some or all of these parameters may be specified as <key> <value> pairs. + +Thread related parameters: + +Different categories of work are assigned to separate thread groups, and +the number of threads in each group can be configured separately. + +If <hash>, <logical>, and <physical> are all set to 0, the work handled by +all three thread types will be handled by a single thread. If any of these +values are non-zero, all of them must be non-zero. + + ack: + The number of threads used to complete bios. Since + completing a bio calls an arbitrary completion function + outside the vdo volume, threads of this type allow the vdo + volume to continue processing requests even when bio + completion is slow. The default is 1. + + bio: + The number of threads used to issue bios to the underlying + storage. Threads of this type allow the vdo volume to + continue processing requests even when bio submission is + slow. The default is 4. + + bioRotationInterval: + The number of bios to enqueue on each bio thread before + switching to the next thread. The value must be greater + than 0 and not more than 1024; the default is 64. + + cpu: + The number of threads used to do CPU-intensive work, such + as hashing and compression. The default is 1. + + hash: + The number of threads used to manage data comparisons for + deduplication based on the hash value of data blocks. The + default is 0. + + logical: + The number of threads used to manage caching and locking + based on the logical address of incoming bios. The default + is 0; the maximum is 60. + + physical: + The number of threads used to manage administration of the + underlying storage device. At format time, a slab size for + the vdo is chosen; the vdo storage device must be large + enough to have at least 1 slab per physical thread. The + default is 0; the maximum is 16. + +Miscellaneous parameters: + + maxDiscard: + The maximum size of discard bio accepted, in 4096-byte + blocks. I/O requests to a vdo volume are normally split + into 4096-byte blocks, and processed up to 2048 at a time. + However, discard requests to a vdo volume can be + automatically split to a larger size, up to <maxDiscard> + 4096-byte blocks in a single bio, and are limited to 1500 + at a time. Increasing this value may provide better overall + performance, at the cost of increased latency for the + individual discard requests. The default and minimum is 1; + the maximum is UINT_MAX / 4096. + + deduplication: + Whether deduplication is enabled. The default is 'on'; the + acceptable values are 'on' and 'off'. + + compression: + Whether compression is enabled. The default is 'off'; the + acceptable values are 'on' and 'off'. + +Device modification +------------------- + +A modified table may be loaded into a running, non-suspended vdo volume. +The modifications will take effect when the device is next resumed. The +modifiable parameters are <logical device size>, <physical device size>, +<maxDiscard>, <compression>, and <deduplication>. + +If the logical device size or physical device size are changed, upon +successful resume vdo will store the new values and require them on future +startups. These two parameters may not be decreased. The logical device +size may not exceed 4 PB. The physical device size must increase by at +least 32832 4096-byte blocks if at all, and must not exceed the size of the +underlying storage device. Additionally, when formatting the vdo device, a +slab size is chosen: the physical device size may never increase above the +size which provides 8192 slabs, and each increase must be large enough to +add at least one new slab.
Documentation/admin-guide/device-mapper/vdo.rst line 188
Quoted 10 lines
Documentation/admin-guide/device-mapper/vdo.rst new file +10
-0+ +Examples: + +Start a previously-formatted vdo volume with 1 GB logical space and 1 GB +physical space, storing to /dev/dm-1 which has more than 1 GB of space. + +:: + + dmsetup create vdo0 --table \ + "0 2097152 vdo V4 /dev/dm-1 262144 4096 32768 16380"
IIUC the backing device needs to be previously formatted. The formatting fails when the size of the backing device is < 5GB:
vdoformat /dev/loop8
Minimum required size for VDO volume: 5063921664 bytes
vdoformat: formatVDO failed on '/dev/loop8': VDO Status: Out of space
That was with 'vdoformat' from https://github.com/dm-vdo/vdo/
It would be great if somewhat smaller devices could be supported.
Quoted 203 lines
Documentation/admin-guide/device-mapper/vdo.rst new file +200
-0+ +Grow the logical size to 4 GB. + +:: + + dmsetup reload vdo0 --table \ + "0 8388608 vdo V4 /dev/dm-1 262144 4096 32768 16380" + dmsetup resume vdo0 + +Grow the physical size to 2 GB. + +:: + + dmsetup reload vdo0 --table \ + "0 8388608 vdo V4 /dev/dm-1 524288 4096 32768 16380" + dmsetup resume vdo0 + +Grow the physical size by 1 GB more and increase max discard sectors. + +:: + + dmsetup reload vdo0 --table \ + "0 10485760 vdo V4 /dev/dm-1 786432 4096 32768 16380 maxDiscard 8" + dmsetup resume vdo0 + +Stop the vdo volume. + +:: + + dmsetup remove vdo0 + +Start the vdo volume again. Note that the logical and physical device sizes +must still match, but other parameters can change. + +:: + + dmsetup create vdo1 --table \ + "0 10485760 vdo V4 /dev/dm-1 786432 512 65550 5000 hash 1 logical 3 physical 2" + +Messages +-------- +All vdo devices accept messages in the form: + +:: + dmsetup message <target-name> 0 <message-name> <message-parameters> + +The messages are: + + stats: + Outputs the current view of the vdo statistics. Mostly used + by the vdostats userspace program to interpret the output + buffer. + + dump: + Dumps many internal structures to the system log. This is + not always safe to run, so it should only be used to debug + a hung vdo. Optional parameters to specify structures to + dump are: + + viopool: The pool of I/O requests incoming bios + pools: A synonym of 'viopool' + vdo: Most of the structures managing on-disk data + queues: Basic information about each vdo thread + threads: A synonym of 'queues' + default: Equivalent to 'queues vdo' + all: All of the above. + + dump-on-shutdown: + Perform a default dump next time vdo shuts down. + + +Status +------ + +:: + + <device> <operating mode> <in recovery> <index state> + <compression state> <physical blocks used> <total physical blocks> + + device: + The name of the vdo volume. + + operating mode: + The current operating mode of the vdo volume; values may be + 'normal', 'recovering' (the volume has detected an issue + with its metadata and is attempting to repair itself), and + 'read-only' (an error has occurred that forces the vdo + volume to only support read operations and not writes). + + in recovery: + Whether the vdo volume is currently in recovery mode; + values may be 'recovering' or '-' which indicates not + recovering. + + index state: + The current state of the deduplication index in the vdo + volume; values may be 'closed', 'closing', 'error', + 'offline', 'online', 'opening', and 'unknown'. + + compression state: + The current state of compression in the vdo volume; values + may be 'offline' and 'online'. + + used physical blocks: + The number of physical blocks in use by the vdo volume. + + total physical blocks: + The total number of physical blocks the vdo volume may use; + the difference between this value and the + <used physical blocks> is the number of blocks the vdo + volume has left before being full. + +Memory Requirements +=================== + +A vdo target requires a fixed 38 MB of RAM along with the following amounts +that scale with the target: + +- 1.15 MB of RAM for each 1 MB of configured block map cache size. The + block map cache requires a minimum of 150 MB. +- 1.6 MB of RAM for each 1 TB of logical space. +- 268 MB of RAM for each 1 TB of physical storage managed by the volume. + +The deduplication index requires additional memory which scales with the +size of the deduplication window. For dense indexes, the index requires 1 +GB of RAM per 1 TB of window. For sparse indexes, the index requires 1 GB +of RAM per 10 TB of window. The index configuration is set when the target +is formatted and may not be modified. + +Run-time Usage +============== + +When using dm-vdo, it is important to be aware of the ways in which its +behavior differs from other storage targets. + +- There is no guarantee that over-writes of existing blocks will succeed. + Because the underlying storage may be multiply referenced, over-writing + an existing block generally requires a vdo to have a free block + available. + +- When blocks are no longer in use, sending a discard request for those + blocks lets the vdo release references for those blocks. If the vdo is + thinly provisioned, discarding unused blocks is essential to prevent the + target from running out of space. However, due to the sharing of + duplicate blocks, no discard request for any given logical block is + guaranteed to reclaim space. + +- Assuming the underlying storage properly implements flush requests, vdo + is resilient against crashes, however, unflushed writes may or may not + persist after a crash. + +- Each write to a vdo target entails a significant amount of processing. + However, much of the work is paralellizable. Therefore, vdo targets + achieve better throughput at higher I/O depths, and can support up 2048 + requests in parallel. + +Tuning +====== + +The vdo device has many options, and it can be difficult to make optimal +choices without perfect knowledge of the workload. Additionally, most +configuration options must be set when a vdo target is started, and cannot +be changed without shutting it down completely; the configuration cannot be +changed while the target is active. Ideally, tuning with simulated +workloads should be performed before deploying vdo in production +environments. + +The most important value to adjust is the block map cache size. In order to +service a request for any logical address, a vdo must load the portion of +the block map which holds the relevant mapping. These mappings are cached. +Performance will suffer when the working set does not fit in the cache. By +default, a vdo allocates 128 MB of metadata cache in RAM to support +efficient access to 100 GB of logical space at a time. It should be scaled +up proportionally for larger working sets. + +The logical and physical thread counts should also be adjusted. A logical +thread controls a disjoint section of the block map, so additional logical +threads increase parallelism and can increase throughput. Physical threads +control a disjoint section of the data blocks, so additional physical +threads can also increase throughput. However, excess threads can waste +resources and increase contention. + +Bio submission threads control the parallelism involved in sending I/O to +the underlying storage; fewer threads mean there is more opportunity to +reorder I/O requests for performance benefit, but also that each I/O +request has to wait longer before being submitted. + +Bio acknowledgment threads are used for finishing I/O requests. This is +done on dedicated threads since the amount of work required to execute a +bio's callback can not be controlled by the vdo itself. Usually one thread +is sufficient but additional threads may be beneficial, particularly when +bios have CPU-heavy callbacks. + +CPU threads are used for hashing and for compression; in workloads with +compression enabled, more threads may result in higher throughput. + +Hash threads are used to sort active requests by hash and determine whether +they should deduplicate; the most CPU intensive actions done by these +threads are comparison of 4096-byte data blocks. In most cases, a single +hash thread is sufficient.Signature · 1 line
2.40.0
Matthew Sakai
maintainerRe: [PATCH v5 01/40] dm: add documentation for dm-vdo target
Quoted 34 lines · Matthias Kaehlcke
Hi,
Quoted 31 lines · Matthew Sakai
This adds the admin-guide documentation for dm-vdo.
vdo.rstis the guide to using dm-vdo. vdo-design is an overview of the design of dm-vdo.Co-developed-by J. corwin Coburn <[email protected]>Signed-off-by J. corwin Coburn <[email protected]>Signed-off-by Matthew Sakai <[email protected]>--- .../admin-guide/device-mapper/vdo-design.rst | 415 ++++++++++++++++++ .../admin-guide/device-mapper/vdo.rst | 388 ++++++++++++++++ 2 files changed, 803 insertions(+)create mode 100644
Documentation/admin-guide/device-mapper/vdo-design.rstcreate mode 100644Documentation/admin-guide/device-mapper/vdo.rstDocumentation/admin-guide/device-mapper/vdo-design.rst new file +10
-0@@ -0,0 +1,415 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +================ +Design of dm-vdo +================ + +The dm-vdo (virtual data optimizer) target provides inline deduplication, +compression, zero-block elimination, and thin provisioning. A dm-vdo target +can be backed by up to 256TB of storage, and can present a logical size of +up to 4PB.
[snip]
Quoted 10 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 5 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +5
-0+ block map cache size: + The size of the block map cache, as a number of 4096-byte + blocks. The minimum and recommended value is 32768 blocks. + If the logical thread count is non-zero, the cache size + must be at least 4096 blocks per logical thread.If I understand correctly the minimum of 32768 blocks results in the 128 MB metadata cache mentioned in 'Tuning', which allows to access up to 100 GB of logical space.
Quoted 4 lines
Is there a strict reason for this minimum? I'm evaluating to use vdo on systems with a relatively small vdo volume (say 4GB) and 'only' 4-8 GB of RAM. The 128 MB of metadata cache would be a sizeable chunk of that, which could make the use of vdo infeasible.
The short answer is that VDO can often use a smaller cache than the default, but it likely won't help in the way you want it to.
Quoted 13 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 9 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +9
-0+Examples: + +Start a previously-formatted vdo volume with 1 GB logical space and 1 GB +physical space, storing to /dev/dm-1 which has more than 1 GB of space. + +:: + + dmsetup create vdo0 --table \ + "0 2097152 vdo V4 /dev/dm-1 262144 4096 32768 16380"IIUC the backing device needs to be previously formatted. The formatting fails when the size of the backing device is < 5GB:
Quoted 7 lines
vdoformat
/dev/loop8
Minimum required size for VDO volume: 5063921664 bytes
vdoformat: formatVDO failed on '/dev/loop8': VDO Status: Out of spaceThat was with 'vdoformat' from https://github.com/dm-vdo/vdo/
It would be great if somewhat smaller devices could be supported.
VDO was designed to handle the challenge of data deduplication in very large storage pools. It generally is not very useful for very small pools. The first question to ask is whether VDO can actually provide any value in the sort of environment you're using. VDO generally takes the strategy of saving storage space by using extra RAM and CPU cycles. In addition, VDO needs to track a certain amount of metadata, which reduces the amount storage available for actual user data.
For vdoformat, the biggest consideration is the deduplication index and other metadata, which are basically a fixed cost of about 3.5GB. In order for VDO to be useful, VDO would have to find enough deduplication to make up for the storage lost to VDO's metadata, so the minimum useful size of a VDO volume is in the 8-12GB range.
For the block map cache, decreasing the cache size may increase the frequency of metadata writes, which generally decreases the write throughput of the VDO device. So the tradeoff is between RAM and write speed.
Nothing about the generic structure of VDO would prevent us from producing a smaller VDO (and in fact we do for some testing purposes), but in a scenario where you can only expect to save a few gigabytes through deduplication, VDO is generally more expensive than it is worth.
If you still think this might be worth pursuing, let me know and we can try to work out a configuration which might suit your goals.
Matt Sakai
Re: [PATCH v5 01/40] dm: add documentation for dm-vdo target
Hi Matthew,
Thanks for your reply!
Quoted 97 lines · Matthew Sakai
Quoted 34 lines · Matthias Kaehlcke
Hi,
Quoted 31 lines · Matthew Sakai
This adds the admin-guide documentation for dm-vdo.
vdo.rstis the guide to using dm-vdo. vdo-design is an overview of the design of dm-vdo.Co-developed-by J. corwin Coburn <[email protected]>Signed-off-by J. corwin Coburn <[email protected]>Signed-off-by Matthew Sakai <[email protected]>--- .../admin-guide/device-mapper/vdo-design.rst | 415 ++++++++++++++++++ .../admin-guide/device-mapper/vdo.rst | 388 ++++++++++++++++ 2 files changed, 803 insertions(+)create mode 100644
Documentation/admin-guide/device-mapper/vdo-design.rstcreate mode 100644Documentation/admin-guide/device-mapper/vdo.rstDocumentation/admin-guide/device-mapper/vdo-design.rst new file +10
-0@@ -0,0 +1,415 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +================ +Design of dm-vdo +================ + +The dm-vdo (virtual data optimizer) target provides inline deduplication, +compression, zero-block elimination, and thin provisioning. A dm-vdo target +can be backed by up to 256TB of storage, and can present a logical size of +up to 4PB.[snip]
Quoted 10 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 5 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +5
-0+ block map cache size: + The size of the block map cache, as a number of 4096-byte + blocks. The minimum and recommended value is 32768 blocks. + If the logical thread count is non-zero, the cache size + must be at least 4096 blocks per logical thread.If I understand correctly the minimum of 32768 blocks results in the 128 MB metadata cache mentioned in 'Tuning', which allows to access up to 100 GB of logical space.
Quoted 4 lines
Is there a strict reason for this minimum? I'm evaluating to use vdo on systems with a relatively small vdo volume (say 4GB) and 'only' 4-8 GB of RAM. The 128 MB of metadata cache would be a sizeable chunk of that, which could make the use of vdo infeasible.
The short answer is that VDO can often use a smaller cache than the default, but it likely won't help in the way you want it to.
Quoted 13 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 9 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +9
-0+Examples: + +Start a previously-formatted vdo volume with 1 GB logical space and 1 GB +physical space, storing to /dev/dm-1 which has more than 1 GB of space. + +:: + + dmsetup create vdo0 --table \ + "0 2097152 vdo V4 /dev/dm-1 262144 4096 32768 16380"IIUC the backing device needs to be previously formatted. The formatting fails when the size of the backing device is < 5GB:
Quoted 7 lines
vdoformat
/dev/loop8
Minimum required size for VDO volume: 5063921664 bytes
vdoformat: formatVDO failed on '/dev/loop8': VDO Status: Out of spaceThat was with 'vdoformat' from https://github.com/dm-vdo/vdo/
It would be great if somewhat smaller devices could be supported.
VDO was designed to handle the challenge of data deduplication in very large storage pools. It generally is not very useful for very small pools. The first question to ask is whether VDO can actually provide any value in the sort of environment you're using. VDO generally takes the strategy of saving storage space by using extra RAM and CPU cycles. In addition, VDO needs to track a certain amount of metadata, which reduces the amount storage available for actual user data.
For vdoformat, the biggest consideration is the deduplication index and other metadata, which are basically a fixed cost of about 3.5GB. In order for VDO to be useful, VDO would have to find enough deduplication to make up for the storage lost to VDO's metadata, so the minimum useful size of a VDO volume is in the 8-12GB range.
For the block map cache, decreasing the cache size may increase the frequency of metadata writes, which generally decreases the write throughput of the VDO device. So the tradeoff is between RAM and write speed.
Quoted 7 lines
Nothing about the generic structure of VDO would prevent us from producing a smaller VDO (and in fact we do for some testing purposes), but in a scenario where you can only expect to save a few gigabytes through deduplication, VDO is generally more expensive than it is worth.
If you still think this might be worth pursuing, let me know and we can try to work out a configuration which might suit your goals.
Some more context about my use case:
I'm evaluating the use of VDO for storing a hibernate image, the goal is to reduce hibernate resume time by loading less data from potentially slow storage. That's why the volume is relatively small. The image is only written once per hibernate cycle and generally after the system was idle for a longer time, so the lower write throughput due to a smaller cache size probably wouldn't be a major concern. The systems might not have huge amounts of free disk space, an overhead of ~3.5GB for the deduplication index would probably rule out the use of VDO.
In the context of this use case the compression part of VDO seems more interesting than the deduplication. In the documentation of VDO I noticed a parameter to disable deduplication. With that I wonder if it would be feasible/reasonable to add an option to vdoformat to omit the deduplication index.
Do you think VDO might be (made) suitable for this scenario or is it just not the right tool?
Thanks
Matthias
Matthew Sakai
maintainerRe: [PATCH v5 01/40] dm: add documentation for dm-vdo target
Quoted 127 lines · Matthias Kaehlcke
Hi Matthew,
Thanks for your reply!
Quoted 97 lines · Matthew Sakai
Quoted 34 lines · Matthias Kaehlcke
Hi,
Quoted 31 lines · Matthew Sakai
This adds the admin-guide documentation for dm-vdo.
vdo.rstis the guide to using dm-vdo. vdo-design is an overview of the design of dm-vdo.Co-developed-by J. corwin Coburn <[email protected]>Signed-off-by J. corwin Coburn <[email protected]>Signed-off-by Matthew Sakai <[email protected]>--- .../admin-guide/device-mapper/vdo-design.rst | 415 ++++++++++++++++++ .../admin-guide/device-mapper/vdo.rst | 388 ++++++++++++++++ 2 files changed, 803 insertions(+)create mode 100644
Documentation/admin-guide/device-mapper/vdo-design.rstcreate mode 100644Documentation/admin-guide/device-mapper/vdo.rstDocumentation/admin-guide/device-mapper/vdo-design.rst new file +10
-0@@ -0,0 +1,415 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +================ +Design of dm-vdo +================ + +The dm-vdo (virtual data optimizer) target provides inline deduplication, +compression, zero-block elimination, and thin provisioning. A dm-vdo target +can be backed by up to 256TB of storage, and can present a logical size of +up to 4PB.[snip]
Quoted 10 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 5 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +5
-0+ block map cache size: + The size of the block map cache, as a number of 4096-byte + blocks. The minimum and recommended value is 32768 blocks. + If the logical thread count is non-zero, the cache size + must be at least 4096 blocks per logical thread.If I understand correctly the minimum of 32768 blocks results in the 128 MB metadata cache mentioned in 'Tuning', which allows to access up to 100 GB of logical space.
Quoted 4 lines
Is there a strict reason for this minimum? I'm evaluating to use vdo on systems with a relatively small vdo volume (say 4GB) and 'only' 4-8 GB of RAM. The 128 MB of metadata cache would be a sizeable chunk of that, which could make the use of vdo infeasible.
The short answer is that VDO can often use a smaller cache than the default, but it likely won't help in the way you want it to.
Quoted 13 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 9 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +9
-0+Examples: + +Start a previously-formatted vdo volume with 1 GB logical space and 1 GB +physical space, storing to /dev/dm-1 which has more than 1 GB of space. + +:: + + dmsetup create vdo0 --table \ + "0 2097152 vdo V4 /dev/dm-1 262144 4096 32768 16380"IIUC the backing device needs to be previously formatted. The formatting fails when the size of the backing device is < 5GB:
Quoted 7 lines
vdoformat
/dev/loop8
Minimum required size for VDO volume: 5063921664 bytes
vdoformat: formatVDO failed on '/dev/loop8': VDO Status: Out of spaceThat was with 'vdoformat' from https://github.com/dm-vdo/vdo/
It would be great if somewhat smaller devices could be supported.
VDO was designed to handle the challenge of data deduplication in very large storage pools. It generally is not very useful for very small pools. The first question to ask is whether VDO can actually provide any value in the sort of environment you're using. VDO generally takes the strategy of saving storage space by using extra RAM and CPU cycles. In addition, VDO needs to track a certain amount of metadata, which reduces the amount storage available for actual user data.
For vdoformat, the biggest consideration is the deduplication index and other metadata, which are basically a fixed cost of about 3.5GB. In order for VDO to be useful, VDO would have to find enough deduplication to make up for the storage lost to VDO's metadata, so the minimum useful size of a VDO volume is in the 8-12GB range.
For the block map cache, decreasing the cache size may increase the frequency of metadata writes, which generally decreases the write throughput of the VDO device. So the tradeoff is between RAM and write speed.
Quoted 7 lines
Nothing about the generic structure of VDO would prevent us from producing a smaller VDO (and in fact we do for some testing purposes), but in a scenario where you can only expect to save a few gigabytes through deduplication, VDO is generally more expensive than it is worth.
If you still think this might be worth pursuing, let me know and we can try to work out a configuration which might suit your goals.
Some more context about my use case:
I'm evaluating the use of VDO for storing a hibernate image, the goal is to reduce hibernate resume time by loading less data from potentially slow storage. That's why the volume is relatively small. The image is only written once per hibernate cycle and generally after the system was idle for a longer time, so the lower write throughput due to a smaller cache size probably wouldn't be a major concern. The systems might not have huge amounts of free disk space, an overhead of ~3.5GB for the deduplication index would probably rule out the use of VDO.
In the context of this use case the compression part of VDO seems more interesting than the deduplication. In the documentation of VDO I noticed a parameter to disable deduplication. With that I wonder if it would be feasible/reasonable to add an option to vdoformat to omit the deduplication index.
Quoted 6 lines
Do you think VDO might be (made) suitable for this scenario or is it just not the right tool?
Thanks
Matthias
The primary reason for VDO is the deduplication capability. You can disable deduplication on a VDO target, but you would still be paying the overhead costs of being able to enable it. Certainly I think VDO itself is not the right tool here.
We have considered making a compression-only target, but realistically it would be a completely separate dm target and not a version of VDO. A compression-only target could remove all the complication of the deduplication aspects of VDO, and it could potentially even get better compression by removing some of the constraints imposed by supporting deduplication. Conceptually it's not too hard, I think, but we haven't really done any work developing it so it wouldn't come into being any time soon. If you thought it would be helpful then we can consider prioritizing that work.
For the specific use case you described, it sounds like you've got a pretty good idea of what you need to write already. Have you considered trying to compress that image before writing it, just using file-level compression or something similar? I wonder if being able to load less data from storage is actually a win once you account for the extra computation you would need to decompress the image.
Matt
Re: [PATCH v5 01/40] dm: add documentation for dm-vdo target
Quoted 133 lines · Matthew Sakai
Quoted 130 lines · Matthias Kaehlcke
Hi Matthew,
Thanks for your reply!
Quoted 97 lines · Matthew Sakai
Quoted 34 lines · Matthias Kaehlcke
Hi,
Quoted 31 lines · Matthew Sakai
This adds the admin-guide documentation for dm-vdo.
vdo.rstis the guide to using dm-vdo. vdo-design is an overview of the design of dm-vdo.Co-developed-by J. corwin Coburn <[email protected]>Signed-off-by J. corwin Coburn <[email protected]>Signed-off-by Matthew Sakai <[email protected]>--- .../admin-guide/device-mapper/vdo-design.rst | 415 ++++++++++++++++++ .../admin-guide/device-mapper/vdo.rst | 388 ++++++++++++++++ 2 files changed, 803 insertions(+)create mode 100644
Documentation/admin-guide/device-mapper/vdo-design.rstcreate mode 100644Documentation/admin-guide/device-mapper/vdo.rstDocumentation/admin-guide/device-mapper/vdo-design.rst new file +10
-0@@ -0,0 +1,415 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +================ +Design of dm-vdo +================ + +The dm-vdo (virtual data optimizer) target provides inline deduplication, +compression, zero-block elimination, and thin provisioning. A dm-vdo target +can be backed by up to 256TB of storage, and can present a logical size of +up to 4PB.[snip]
Quoted 10 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 5 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +5
-0+ block map cache size: + The size of the block map cache, as a number of 4096-byte + blocks. The minimum and recommended value is 32768 blocks. + If the logical thread count is non-zero, the cache size + must be at least 4096 blocks per logical thread.If I understand correctly the minimum of 32768 blocks results in the 128 MB metadata cache mentioned in 'Tuning', which allows to access up to 100 GB of logical space.
Quoted 4 lines
Is there a strict reason for this minimum? I'm evaluating to use vdo on systems with a relatively small vdo volume (say 4GB) and 'only' 4-8 GB of RAM. The 128 MB of metadata cache would be a sizeable chunk of that, which could make the use of vdo infeasible.
The short answer is that VDO can often use a smaller cache than the default, but it likely won't help in the way you want it to.
Quoted 13 lines
Documentation/admin-guide/device-mapper/vdo-design.rst
Quoted 9 lines
Documentation/admin-guide/device-mapper/vdo-design.rst new file +9
-0+Examples: + +Start a previously-formatted vdo volume with 1 GB logical space and 1 GB +physical space, storing to /dev/dm-1 which has more than 1 GB of space. + +:: + + dmsetup create vdo0 --table \ + "0 2097152 vdo V4 /dev/dm-1 262144 4096 32768 16380"IIUC the backing device needs to be previously formatted. The formatting fails when the size of the backing device is < 5GB:
Quoted 7 lines
vdoformat
/dev/loop8
Minimum required size for VDO volume: 5063921664 bytes
vdoformat: formatVDO failed on '/dev/loop8': VDO Status: Out of spaceThat was with 'vdoformat' from https://github.com/dm-vdo/vdo/
It would be great if somewhat smaller devices could be supported.
VDO was designed to handle the challenge of data deduplication in very large storage pools. It generally is not very useful for very small pools. The first question to ask is whether VDO can actually provide any value in the sort of environment you're using. VDO generally takes the strategy of saving storage space by using extra RAM and CPU cycles. In addition, VDO needs to track a certain amount of metadata, which reduces the amount storage available for actual user data.
For vdoformat, the biggest consideration is the deduplication index and other metadata, which are basically a fixed cost of about 3.5GB. In order for VDO to be useful, VDO would have to find enough deduplication to make up for the storage lost to VDO's metadata, so the minimum useful size of a VDO volume is in the 8-12GB range.
For the block map cache, decreasing the cache size may increase the frequency of metadata writes, which generally decreases the write throughput of the VDO device. So the tradeoff is between RAM and write speed.
Quoted 7 lines
Nothing about the generic structure of VDO would prevent us from producing a smaller VDO (and in fact we do for some testing purposes), but in a scenario where you can only expect to save a few gigabytes through deduplication, VDO is generally more expensive than it is worth.
If you still think this might be worth pursuing, let me know and we can try to work out a configuration which might suit your goals.
Some more context about my use case:
I'm evaluating the use of VDO for storing a hibernate image, the goal is to reduce hibernate resume time by loading less data from potentially slow storage. That's why the volume is relatively small. The image is only written once per hibernate cycle and generally after the system was idle for a longer time, so the lower write throughput due to a smaller cache size probably wouldn't be a major concern. The systems might not have huge amounts of free disk space, an overhead of ~3.5GB for the deduplication index would probably rule out the use of VDO.
In the context of this use case the compression part of VDO seems more interesting than the deduplication. In the documentation of VDO I noticed a parameter to disable deduplication. With that I wonder if it would be feasible/reasonable to add an option to vdoformat to omit the deduplication index.
Do you think VDO might be (made) suitable for this scenario or is it just not the right tool?
Quoted 8 lines
Quoted 3 lines
Thanks
Matthias
The primary reason for VDO is the deduplication capability. You can disable deduplication on a VDO target, but you would still be paying the overhead costs of being able to enable it. Certainly I think VDO itself is not the right tool here.
Ok, that's good to know, thanks.
Quoted 9 lines
We have considered making a compression-only target, but realistically it would be a completely separate dm target and not a version of VDO. A compression-only target could remove all the complication of the deduplication aspects of VDO, and it could potentially even get better compression by removing some of the constraints imposed by supporting deduplication. Conceptually it's not too hard, I think, but we haven't really done any work developing it so it wouldn't come into being any time soon. If you thought it would be helpful then we can consider prioritizing that work.
Thanks for the offer to priorize a compression-only target, that migh be very useful! A decision about whether hibernate is a priority for Chrome OS in 2024 is still pending, there should be more clarity within a few weeks. Before that it's probably best not to ask others to do any significant development work related with that topic :)
Quoted 4 lines
For the specific use case you described, it sounds like you've got a pretty good idea of what you need to write already. Have you considered trying to compress that image before writing it, just using file-level compression or something similar?
Unfortunately that is not a (straightforward) option. We use uswsusp, but for the sake of security the kernel writes the image directly to a raw storage device (a dm-crypt target), so any compression would have to happen in the kernel.
Quoted 3 lines
I wonder if being able to load less data from storage is actually a win once you account for the extra computation you would need to decompress the image.
That's a good point, might be worth some prototyping.
m.