1*3bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */ 2991d9fa0SJoe Thornber /* 3991d9fa0SJoe Thornber * Copyright (C) 2010-2011 Red Hat, Inc. 4991d9fa0SJoe Thornber * 5991d9fa0SJoe Thornber * This file is released under the GPL. 6991d9fa0SJoe Thornber */ 7991d9fa0SJoe Thornber 8991d9fa0SJoe Thornber #ifndef DM_THIN_METADATA_H 9991d9fa0SJoe Thornber #define DM_THIN_METADATA_H 10991d9fa0SJoe Thornber 11991d9fa0SJoe Thornber #include "persistent-data/dm-block-manager.h" 12ac8c3f3dSJoe Thornber #include "persistent-data/dm-space-map.h" 137d48935eSMike Snitzer #include "persistent-data/dm-space-map-metadata.h" 14991d9fa0SJoe Thornber 157d48935eSMike Snitzer #define THIN_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE 16991d9fa0SJoe Thornber 17c4a69ecdSMike Snitzer /* 18c4a69ecdSMike Snitzer * The metadata device is currently limited in size. 19c4a69ecdSMike Snitzer */ 207d48935eSMike Snitzer #define THIN_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS 21c4a69ecdSMike Snitzer 22c4a69ecdSMike Snitzer /* 23c4a69ecdSMike Snitzer * A metadata device larger than 16GB triggers a warning. 24c4a69ecdSMike Snitzer */ 25c4a69ecdSMike Snitzer #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT)) 26c4a69ecdSMike Snitzer 27991d9fa0SJoe Thornber /*----------------------------------------------------------------*/ 28991d9fa0SJoe Thornber 2907f2b6e0SMike Snitzer /* 3007f2b6e0SMike Snitzer * Thin metadata superblock flags. 3107f2b6e0SMike Snitzer */ 3207f2b6e0SMike Snitzer #define THIN_METADATA_NEEDS_CHECK_FLAG (1 << 0) 3307f2b6e0SMike Snitzer 34991d9fa0SJoe Thornber struct dm_pool_metadata; 35991d9fa0SJoe Thornber struct dm_thin_device; 36991d9fa0SJoe Thornber 37991d9fa0SJoe Thornber /* 38991d9fa0SJoe Thornber * Device identifier 39991d9fa0SJoe Thornber */ 40991d9fa0SJoe Thornber typedef uint64_t dm_thin_id; 41991d9fa0SJoe Thornber 42991d9fa0SJoe Thornber /* 43991d9fa0SJoe Thornber * Reopens or creates a new, empty metadata volume. 44991d9fa0SJoe Thornber */ 45991d9fa0SJoe Thornber struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev, 4666b1edc0SJoe Thornber sector_t data_block_size, 4766b1edc0SJoe Thornber bool format_device); 48991d9fa0SJoe Thornber 49991d9fa0SJoe Thornber int dm_pool_metadata_close(struct dm_pool_metadata *pmd); 50991d9fa0SJoe Thornber 51991d9fa0SJoe Thornber /* 52991d9fa0SJoe Thornber * Compat feature flags. Any incompat flags beyond the ones 53991d9fa0SJoe Thornber * specified below will prevent use of the thin metadata. 54991d9fa0SJoe Thornber */ 55991d9fa0SJoe Thornber #define THIN_FEATURE_COMPAT_SUPP 0UL 56991d9fa0SJoe Thornber #define THIN_FEATURE_COMPAT_RO_SUPP 0UL 57991d9fa0SJoe Thornber #define THIN_FEATURE_INCOMPAT_SUPP 0UL 58991d9fa0SJoe Thornber 59991d9fa0SJoe Thornber /* 60991d9fa0SJoe Thornber * Device creation/deletion. 61991d9fa0SJoe Thornber */ 62991d9fa0SJoe Thornber int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev); 63991d9fa0SJoe Thornber 64991d9fa0SJoe Thornber /* 65991d9fa0SJoe Thornber * An internal snapshot. 66991d9fa0SJoe Thornber * 67991d9fa0SJoe Thornber * You can only snapshot a quiesced origin i.e. one that is either 68991d9fa0SJoe Thornber * suspended or not instanced at all. 69991d9fa0SJoe Thornber */ 70991d9fa0SJoe Thornber int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev, 71991d9fa0SJoe Thornber dm_thin_id origin); 72991d9fa0SJoe Thornber 73991d9fa0SJoe Thornber /* 74991d9fa0SJoe Thornber * Deletes a virtual device from the metadata. It _is_ safe to call this 75991d9fa0SJoe Thornber * when that device is open. Operations on that device will just start 76991d9fa0SJoe Thornber * failing. You still need to call close() on the device. 77991d9fa0SJoe Thornber */ 78991d9fa0SJoe Thornber int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd, 79991d9fa0SJoe Thornber dm_thin_id dev); 80991d9fa0SJoe Thornber 81991d9fa0SJoe Thornber /* 82991d9fa0SJoe Thornber * Commits _all_ metadata changes: device creation, deletion, mapping 83991d9fa0SJoe Thornber * updates. 84991d9fa0SJoe Thornber */ 85991d9fa0SJoe Thornber int dm_pool_commit_metadata(struct dm_pool_metadata *pmd); 86991d9fa0SJoe Thornber 87991d9fa0SJoe Thornber /* 88da105ed5SJoe Thornber * Discards all uncommitted changes. Rereads the superblock, rolling back 89da105ed5SJoe Thornber * to the last good transaction. Thin devices remain open. 90da105ed5SJoe Thornber * dm_thin_aborted_changes() tells you if they had uncommitted changes. 91da105ed5SJoe Thornber * 92da105ed5SJoe Thornber * If this call fails it's only useful to call dm_pool_metadata_close(). 93da105ed5SJoe Thornber * All other methods will fail with -EINVAL. 94da105ed5SJoe Thornber */ 95da105ed5SJoe Thornber int dm_pool_abort_metadata(struct dm_pool_metadata *pmd); 96da105ed5SJoe Thornber 97da105ed5SJoe Thornber /* 98991d9fa0SJoe Thornber * Set/get userspace transaction id. 99991d9fa0SJoe Thornber */ 100991d9fa0SJoe Thornber int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd, 101991d9fa0SJoe Thornber uint64_t current_id, 102991d9fa0SJoe Thornber uint64_t new_id); 103991d9fa0SJoe Thornber 104991d9fa0SJoe Thornber int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd, 105991d9fa0SJoe Thornber uint64_t *result); 106991d9fa0SJoe Thornber 107991d9fa0SJoe Thornber /* 108991d9fa0SJoe Thornber * Hold/get root for userspace transaction. 109cc8394d8SJoe Thornber * 110cc8394d8SJoe Thornber * The metadata snapshot is a copy of the current superblock (minus the 111cc8394d8SJoe Thornber * space maps). Userland can access the data structures for READ 112cc8394d8SJoe Thornber * operations only. A small performance hit is incurred by providing this 113cc8394d8SJoe Thornber * copy of the metadata to userland due to extra copy-on-write operations 114cc8394d8SJoe Thornber * on the metadata nodes. Release this as soon as you finish with it. 115991d9fa0SJoe Thornber */ 116cc8394d8SJoe Thornber int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd); 117cc8394d8SJoe Thornber int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd); 118991d9fa0SJoe Thornber 119cc8394d8SJoe Thornber int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd, 120991d9fa0SJoe Thornber dm_block_t *result); 121991d9fa0SJoe Thornber 122991d9fa0SJoe Thornber /* 123991d9fa0SJoe Thornber * Actions on a single virtual device. 124991d9fa0SJoe Thornber */ 125991d9fa0SJoe Thornber 126991d9fa0SJoe Thornber /* 127991d9fa0SJoe Thornber * Opening the same device more than once will fail with -EBUSY. 128991d9fa0SJoe Thornber */ 129991d9fa0SJoe Thornber int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev, 130991d9fa0SJoe Thornber struct dm_thin_device **td); 131991d9fa0SJoe Thornber 132991d9fa0SJoe Thornber int dm_pool_close_thin_device(struct dm_thin_device *td); 133991d9fa0SJoe Thornber 134991d9fa0SJoe Thornber dm_thin_id dm_thin_dev_id(struct dm_thin_device *td); 135991d9fa0SJoe Thornber 136991d9fa0SJoe Thornber struct dm_thin_lookup_result { 137991d9fa0SJoe Thornber dm_block_t block; 1387f214665SMike Snitzer bool shared:1; 139991d9fa0SJoe Thornber }; 140991d9fa0SJoe Thornber 141991d9fa0SJoe Thornber /* 142991d9fa0SJoe Thornber * Returns: 143e5cfc69aSJoe Thornber * -EWOULDBLOCK iff @can_issue_io is set and would issue IO 144991d9fa0SJoe Thornber * -ENODATA iff that mapping is not present. 145991d9fa0SJoe Thornber * 0 success 146991d9fa0SJoe Thornber */ 147991d9fa0SJoe Thornber int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block, 148e5cfc69aSJoe Thornber int can_issue_io, struct dm_thin_lookup_result *result); 149991d9fa0SJoe Thornber 150991d9fa0SJoe Thornber /* 151a5d895a9SJoe Thornber * Retrieve the next run of contiguously mapped blocks. Useful for working 152a5d895a9SJoe Thornber * out where to break up IO. Returns 0 on success, < 0 on error. 153a5d895a9SJoe Thornber */ 154a5d895a9SJoe Thornber int dm_thin_find_mapped_range(struct dm_thin_device *td, 155a5d895a9SJoe Thornber dm_block_t begin, dm_block_t end, 156a5d895a9SJoe Thornber dm_block_t *thin_begin, dm_block_t *thin_end, 157a5d895a9SJoe Thornber dm_block_t *pool_begin, bool *maybe_shared); 158a5d895a9SJoe Thornber 159a5d895a9SJoe Thornber /* 160991d9fa0SJoe Thornber * Obtain an unused block. 161991d9fa0SJoe Thornber */ 162991d9fa0SJoe Thornber int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result); 163991d9fa0SJoe Thornber 164991d9fa0SJoe Thornber /* 165991d9fa0SJoe Thornber * Insert or remove block. 166991d9fa0SJoe Thornber */ 167991d9fa0SJoe Thornber int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block, 168991d9fa0SJoe Thornber dm_block_t data_block); 169991d9fa0SJoe Thornber 1706550f075SJoe Thornber int dm_thin_remove_range(struct dm_thin_device *td, 1716550f075SJoe Thornber dm_block_t begin, dm_block_t end); 172991d9fa0SJoe Thornber 173991d9fa0SJoe Thornber /* 174991d9fa0SJoe Thornber * Queries. 175991d9fa0SJoe Thornber */ 17640db5a53SJoe Thornber bool dm_thin_changed_this_transaction(struct dm_thin_device *td); 17740db5a53SJoe Thornber 1784d1662a3SMike Snitzer bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd); 1794d1662a3SMike Snitzer 180da105ed5SJoe Thornber bool dm_thin_aborted_changes(struct dm_thin_device *td); 181da105ed5SJoe Thornber 182991d9fa0SJoe Thornber int dm_thin_get_highest_mapped_block(struct dm_thin_device *td, 183991d9fa0SJoe Thornber dm_block_t *highest_mapped); 184991d9fa0SJoe Thornber 185991d9fa0SJoe Thornber int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result); 186991d9fa0SJoe Thornber 187991d9fa0SJoe Thornber int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, 188991d9fa0SJoe Thornber dm_block_t *result); 189991d9fa0SJoe Thornber 190991d9fa0SJoe Thornber int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd, 191991d9fa0SJoe Thornber dm_block_t *result); 192991d9fa0SJoe Thornber 193991d9fa0SJoe Thornber int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd, 194991d9fa0SJoe Thornber dm_block_t *result); 195991d9fa0SJoe Thornber 196991d9fa0SJoe Thornber int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result); 197991d9fa0SJoe Thornber 198d445bd9cSJoe Thornber int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result); 19919fa1a67SJoe Thornber 2002a0fbffbSJoe Thornber int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e); 2012a0fbffbSJoe Thornber int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e); 2022a0fbffbSJoe Thornber 203991d9fa0SJoe Thornber /* 204991d9fa0SJoe Thornber * Returns -ENOSPC if the new size is too small and already allocated 205991d9fa0SJoe Thornber * blocks would be lost. 206991d9fa0SJoe Thornber */ 207991d9fa0SJoe Thornber int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size); 20824347e95SJoe Thornber int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size); 209991d9fa0SJoe Thornber 21012ba58afSJoe Thornber /* 21112ba58afSJoe Thornber * Flicks the underlying block manager into read only mode, so you know 21212ba58afSJoe Thornber * that nothing is changing. 21312ba58afSJoe Thornber */ 21412ba58afSJoe Thornber void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd); 2159b7aaa64SJoe Thornber void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd); 21612ba58afSJoe Thornber 217ac8c3f3dSJoe Thornber int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd, 218ac8c3f3dSJoe Thornber dm_block_t threshold, 219ac8c3f3dSJoe Thornber dm_sm_threshold_fn fn, 220ac8c3f3dSJoe Thornber void *context); 221ac8c3f3dSJoe Thornber 22207f2b6e0SMike Snitzer /* 22307f2b6e0SMike Snitzer * Updates the superblock immediately. 22407f2b6e0SMike Snitzer */ 22507f2b6e0SMike Snitzer int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd); 22607f2b6e0SMike Snitzer bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd); 22707f2b6e0SMike Snitzer 2288a01a6afSJoe Thornber /* 2298a01a6afSJoe Thornber * Issue any prefetches that may be useful. 2308a01a6afSJoe Thornber */ 2318a01a6afSJoe Thornber void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd); 2328a01a6afSJoe Thornber 233ecda7c02SNikos Tsironis /* Pre-commit callback */ 234ecda7c02SNikos Tsironis typedef int (*dm_pool_pre_commit_fn)(void *context); 235ecda7c02SNikos Tsironis 236ecda7c02SNikos Tsironis void dm_pool_register_pre_commit_callback(struct dm_pool_metadata *pmd, 237ecda7c02SNikos Tsironis dm_pool_pre_commit_fn fn, 238ecda7c02SNikos Tsironis void *context); 239ecda7c02SNikos Tsironis 240991d9fa0SJoe Thornber /*----------------------------------------------------------------*/ 241991d9fa0SJoe Thornber 242991d9fa0SJoe Thornber #endif 243