1991d9fa0SJoe Thornber /* 2991d9fa0SJoe Thornber * Copyright (C) 2010-2011 Red Hat, Inc. 3991d9fa0SJoe Thornber * 4991d9fa0SJoe Thornber * This file is released under the GPL. 5991d9fa0SJoe Thornber */ 6991d9fa0SJoe Thornber 7991d9fa0SJoe Thornber #ifndef DM_THIN_METADATA_H 8991d9fa0SJoe Thornber #define DM_THIN_METADATA_H 9991d9fa0SJoe Thornber 10991d9fa0SJoe Thornber #include "persistent-data/dm-block-manager.h" 11ac8c3f3dSJoe Thornber #include "persistent-data/dm-space-map.h" 12991d9fa0SJoe Thornber 13991d9fa0SJoe Thornber #define THIN_METADATA_BLOCK_SIZE 4096 14991d9fa0SJoe Thornber 15c4a69ecdSMike Snitzer /* 16c4a69ecdSMike Snitzer * The metadata device is currently limited in size. 17c4a69ecdSMike Snitzer * 18c4a69ecdSMike Snitzer * We have one block of index, which can hold 255 index entries. Each 19c4a69ecdSMike Snitzer * index entry contains allocation info about 16k metadata blocks. 20c4a69ecdSMike Snitzer */ 21c4a69ecdSMike Snitzer #define THIN_METADATA_MAX_SECTORS (255 * (1 << 14) * (THIN_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT))) 22c4a69ecdSMike Snitzer 23c4a69ecdSMike Snitzer /* 24c4a69ecdSMike Snitzer * A metadata device larger than 16GB triggers a warning. 25c4a69ecdSMike Snitzer */ 26c4a69ecdSMike Snitzer #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT)) 27c4a69ecdSMike Snitzer 28991d9fa0SJoe Thornber /*----------------------------------------------------------------*/ 29991d9fa0SJoe Thornber 30991d9fa0SJoe Thornber struct dm_pool_metadata; 31991d9fa0SJoe Thornber struct dm_thin_device; 32991d9fa0SJoe Thornber 33991d9fa0SJoe Thornber /* 34991d9fa0SJoe Thornber * Device identifier 35991d9fa0SJoe Thornber */ 36991d9fa0SJoe Thornber typedef uint64_t dm_thin_id; 37991d9fa0SJoe Thornber 38991d9fa0SJoe Thornber /* 39991d9fa0SJoe Thornber * Reopens or creates a new, empty metadata volume. 40991d9fa0SJoe Thornber */ 41991d9fa0SJoe Thornber struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev, 4266b1edc0SJoe Thornber sector_t data_block_size, 4366b1edc0SJoe Thornber bool format_device); 44991d9fa0SJoe Thornber 45991d9fa0SJoe Thornber int dm_pool_metadata_close(struct dm_pool_metadata *pmd); 46991d9fa0SJoe Thornber 47991d9fa0SJoe Thornber /* 48991d9fa0SJoe Thornber * Compat feature flags. Any incompat flags beyond the ones 49991d9fa0SJoe Thornber * specified below will prevent use of the thin metadata. 50991d9fa0SJoe Thornber */ 51991d9fa0SJoe Thornber #define THIN_FEATURE_COMPAT_SUPP 0UL 52991d9fa0SJoe Thornber #define THIN_FEATURE_COMPAT_RO_SUPP 0UL 53991d9fa0SJoe Thornber #define THIN_FEATURE_INCOMPAT_SUPP 0UL 54991d9fa0SJoe Thornber 55991d9fa0SJoe Thornber /* 56991d9fa0SJoe Thornber * Device creation/deletion. 57991d9fa0SJoe Thornber */ 58991d9fa0SJoe Thornber int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev); 59991d9fa0SJoe Thornber 60991d9fa0SJoe Thornber /* 61991d9fa0SJoe Thornber * An internal snapshot. 62991d9fa0SJoe Thornber * 63991d9fa0SJoe Thornber * You can only snapshot a quiesced origin i.e. one that is either 64991d9fa0SJoe Thornber * suspended or not instanced at all. 65991d9fa0SJoe Thornber */ 66991d9fa0SJoe Thornber int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev, 67991d9fa0SJoe Thornber dm_thin_id origin); 68991d9fa0SJoe Thornber 69991d9fa0SJoe Thornber /* 70991d9fa0SJoe Thornber * Deletes a virtual device from the metadata. It _is_ safe to call this 71991d9fa0SJoe Thornber * when that device is open. Operations on that device will just start 72991d9fa0SJoe Thornber * failing. You still need to call close() on the device. 73991d9fa0SJoe Thornber */ 74991d9fa0SJoe Thornber int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd, 75991d9fa0SJoe Thornber dm_thin_id dev); 76991d9fa0SJoe Thornber 77991d9fa0SJoe Thornber /* 78991d9fa0SJoe Thornber * Commits _all_ metadata changes: device creation, deletion, mapping 79991d9fa0SJoe Thornber * updates. 80991d9fa0SJoe Thornber */ 81991d9fa0SJoe Thornber int dm_pool_commit_metadata(struct dm_pool_metadata *pmd); 82991d9fa0SJoe Thornber 83991d9fa0SJoe Thornber /* 84da105ed5SJoe Thornber * Discards all uncommitted changes. Rereads the superblock, rolling back 85da105ed5SJoe Thornber * to the last good transaction. Thin devices remain open. 86da105ed5SJoe Thornber * dm_thin_aborted_changes() tells you if they had uncommitted changes. 87da105ed5SJoe Thornber * 88da105ed5SJoe Thornber * If this call fails it's only useful to call dm_pool_metadata_close(). 89da105ed5SJoe Thornber * All other methods will fail with -EINVAL. 90da105ed5SJoe Thornber */ 91da105ed5SJoe Thornber int dm_pool_abort_metadata(struct dm_pool_metadata *pmd); 92da105ed5SJoe Thornber 93da105ed5SJoe Thornber /* 94991d9fa0SJoe Thornber * Set/get userspace transaction id. 95991d9fa0SJoe Thornber */ 96991d9fa0SJoe Thornber int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd, 97991d9fa0SJoe Thornber uint64_t current_id, 98991d9fa0SJoe Thornber uint64_t new_id); 99991d9fa0SJoe Thornber 100991d9fa0SJoe Thornber int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd, 101991d9fa0SJoe Thornber uint64_t *result); 102991d9fa0SJoe Thornber 103991d9fa0SJoe Thornber /* 104991d9fa0SJoe Thornber * Hold/get root for userspace transaction. 105cc8394d8SJoe Thornber * 106cc8394d8SJoe Thornber * The metadata snapshot is a copy of the current superblock (minus the 107cc8394d8SJoe Thornber * space maps). Userland can access the data structures for READ 108cc8394d8SJoe Thornber * operations only. A small performance hit is incurred by providing this 109cc8394d8SJoe Thornber * copy of the metadata to userland due to extra copy-on-write operations 110cc8394d8SJoe Thornber * on the metadata nodes. Release this as soon as you finish with it. 111991d9fa0SJoe Thornber */ 112cc8394d8SJoe Thornber int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd); 113cc8394d8SJoe Thornber int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd); 114991d9fa0SJoe Thornber 115cc8394d8SJoe Thornber int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd, 116991d9fa0SJoe Thornber dm_block_t *result); 117991d9fa0SJoe Thornber 118991d9fa0SJoe Thornber /* 119991d9fa0SJoe Thornber * Actions on a single virtual device. 120991d9fa0SJoe Thornber */ 121991d9fa0SJoe Thornber 122991d9fa0SJoe Thornber /* 123991d9fa0SJoe Thornber * Opening the same device more than once will fail with -EBUSY. 124991d9fa0SJoe Thornber */ 125991d9fa0SJoe Thornber int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev, 126991d9fa0SJoe Thornber struct dm_thin_device **td); 127991d9fa0SJoe Thornber 128991d9fa0SJoe Thornber int dm_pool_close_thin_device(struct dm_thin_device *td); 129991d9fa0SJoe Thornber 130991d9fa0SJoe Thornber dm_thin_id dm_thin_dev_id(struct dm_thin_device *td); 131991d9fa0SJoe Thornber 132991d9fa0SJoe Thornber struct dm_thin_lookup_result { 133991d9fa0SJoe Thornber dm_block_t block; 13417b7d63fSMike Snitzer unsigned shared:1; 135991d9fa0SJoe Thornber }; 136991d9fa0SJoe Thornber 137991d9fa0SJoe Thornber /* 138991d9fa0SJoe Thornber * Returns: 139991d9fa0SJoe Thornber * -EWOULDBLOCK iff @can_block is set and would block. 140991d9fa0SJoe Thornber * -ENODATA iff that mapping is not present. 141991d9fa0SJoe Thornber * 0 success 142991d9fa0SJoe Thornber */ 143991d9fa0SJoe Thornber int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block, 144991d9fa0SJoe Thornber int can_block, struct dm_thin_lookup_result *result); 145991d9fa0SJoe Thornber 146991d9fa0SJoe Thornber /* 147991d9fa0SJoe Thornber * Obtain an unused block. 148991d9fa0SJoe Thornber */ 149991d9fa0SJoe Thornber int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result); 150991d9fa0SJoe Thornber 151991d9fa0SJoe Thornber /* 152991d9fa0SJoe Thornber * Insert or remove block. 153991d9fa0SJoe Thornber */ 154991d9fa0SJoe Thornber int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block, 155991d9fa0SJoe Thornber dm_block_t data_block); 156991d9fa0SJoe Thornber 157991d9fa0SJoe Thornber int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block); 158991d9fa0SJoe Thornber 159991d9fa0SJoe Thornber /* 160991d9fa0SJoe Thornber * Queries. 161991d9fa0SJoe Thornber */ 16240db5a53SJoe Thornber bool dm_thin_changed_this_transaction(struct dm_thin_device *td); 16340db5a53SJoe Thornber 164da105ed5SJoe Thornber bool dm_thin_aborted_changes(struct dm_thin_device *td); 165da105ed5SJoe Thornber 166991d9fa0SJoe Thornber int dm_thin_get_highest_mapped_block(struct dm_thin_device *td, 167991d9fa0SJoe Thornber dm_block_t *highest_mapped); 168991d9fa0SJoe Thornber 169991d9fa0SJoe Thornber int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result); 170991d9fa0SJoe Thornber 171991d9fa0SJoe Thornber int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, 172991d9fa0SJoe Thornber dm_block_t *result); 173991d9fa0SJoe Thornber 174991d9fa0SJoe Thornber int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd, 175991d9fa0SJoe Thornber dm_block_t *result); 176991d9fa0SJoe Thornber 177991d9fa0SJoe Thornber int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd, 178991d9fa0SJoe Thornber dm_block_t *result); 179991d9fa0SJoe Thornber 180991d9fa0SJoe Thornber int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result); 181991d9fa0SJoe Thornber 182991d9fa0SJoe Thornber int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result); 183991d9fa0SJoe Thornber 184991d9fa0SJoe Thornber /* 185991d9fa0SJoe Thornber * Returns -ENOSPC if the new size is too small and already allocated 186991d9fa0SJoe Thornber * blocks would be lost. 187991d9fa0SJoe Thornber */ 188991d9fa0SJoe Thornber int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size); 18924347e95SJoe Thornber int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size); 190991d9fa0SJoe Thornber 19112ba58afSJoe Thornber /* 19212ba58afSJoe Thornber * Flicks the underlying block manager into read only mode, so you know 19312ba58afSJoe Thornber * that nothing is changing. 19412ba58afSJoe Thornber */ 19512ba58afSJoe Thornber void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd); 196*9b7aaa64SJoe Thornber void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd); 19712ba58afSJoe Thornber 198ac8c3f3dSJoe Thornber int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd, 199ac8c3f3dSJoe Thornber dm_block_t threshold, 200ac8c3f3dSJoe Thornber dm_sm_threshold_fn fn, 201ac8c3f3dSJoe Thornber void *context); 202ac8c3f3dSJoe Thornber 203991d9fa0SJoe Thornber /*----------------------------------------------------------------*/ 204991d9fa0SJoe Thornber 205991d9fa0SJoe Thornber #endif 206