1/* 2 * Copyright (C) 2011 Red Hat, Inc. 3 * 4 * This file is released under the GPL. 5 */ 6 7#include "dm-space-map-common.h" 8#include "dm-space-map-disk.h" 9#include "dm-space-map.h" 10#include "dm-transaction-manager.h" 11 12#include <linux/list.h> 13#include <linux/slab.h> 14#include <linux/export.h> 15#include <linux/device-mapper.h> 16 17#define DM_MSG_PREFIX "space map disk" 18 19/*----------------------------------------------------------------*/ 20 21/* 22 * Space map interface. 23 */ 24struct sm_disk { 25 struct dm_space_map sm; 26 27 struct ll_disk ll; 28 struct ll_disk old_ll; 29 30 dm_block_t begin; 31 dm_block_t nr_allocated_this_transaction; 32}; 33 34static void sm_disk_destroy(struct dm_space_map *sm) 35{ 36 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 37 38 kfree(smd); 39} 40 41static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks) 42{ 43 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 44 45 return sm_ll_extend(&smd->ll, extra_blocks); 46} 47 48static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count) 49{ 50 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 51 *count = smd->old_ll.nr_blocks; 52 53 return 0; 54} 55 56static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count) 57{ 58 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 59 *count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction; 60 61 return 0; 62} 63 64static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b, 65 uint32_t *result) 66{ 67 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 68 return sm_ll_lookup(&smd->ll, b, result); 69} 70 71static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b, 72 int *result) 73{ 74 int r; 75 uint32_t count; 76 77 r = sm_disk_get_count(sm, b, &count); 78 if (r) 79 return r; 80 81 *result = count > 1; 82 83 return 0; 84} 85 86static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b, 87 uint32_t count) 88{ 89 int r; 90 uint32_t old_count; 91 enum allocation_event ev; 92 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 93 94 r = sm_ll_insert(&smd->ll, b, count, &ev); 95 if (!r) { 96 switch (ev) { 97 case SM_NONE: 98 break; 99 100 case SM_ALLOC: 101 /* 102 * This _must_ be free in the prior transaction 103 * otherwise we've lost atomicity. 104 */ 105 smd->nr_allocated_this_transaction++; 106 break; 107 108 case SM_FREE: 109 /* 110 * It's only free if it's also free in the last 111 * transaction. 112 */ 113 r = sm_ll_lookup(&smd->old_ll, b, &old_count); 114 if (r) 115 return r; 116 117 if (!old_count) 118 smd->nr_allocated_this_transaction--; 119 break; 120 } 121 } 122 123 return r; 124} 125 126static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b) 127{ 128 int r; 129 enum allocation_event ev; 130 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 131 132 r = sm_ll_inc(&smd->ll, b, &ev); 133 if (!r && (ev == SM_ALLOC)) 134 /* 135 * This _must_ be free in the prior transaction 136 * otherwise we've lost atomicity. 137 */ 138 smd->nr_allocated_this_transaction++; 139 140 return r; 141} 142 143static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b) 144{ 145 int r; 146 uint32_t old_count; 147 enum allocation_event ev; 148 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 149 150 r = sm_ll_dec(&smd->ll, b, &ev); 151 if (!r && (ev == SM_FREE)) { 152 /* 153 * It's only free if it's also free in the last 154 * transaction. 155 */ 156 r = sm_ll_lookup(&smd->old_ll, b, &old_count); 157 if (!r && !old_count) 158 smd->nr_allocated_this_transaction--; 159 } 160 161 return r; 162} 163 164static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b) 165{ 166 int r; 167 enum allocation_event ev; 168 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 169 170 /* 171 * Any block we allocate has to be free in both the old and current ll. 172 */ 173 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b); 174 if (r == -ENOSPC) { 175 /* 176 * There's no free block between smd->begin and the end of the metadata device. 177 * We search before smd->begin in case something has been freed. 178 */ 179 r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b); 180 } 181 182 if (r) 183 return r; 184 185 smd->begin = *b + 1; 186 r = sm_ll_inc(&smd->ll, *b, &ev); 187 if (!r) { 188 BUG_ON(ev != SM_ALLOC); 189 smd->nr_allocated_this_transaction++; 190 } 191 192 return r; 193} 194 195static int sm_disk_commit(struct dm_space_map *sm) 196{ 197 int r; 198 dm_block_t nr_free; 199 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 200 201 r = sm_disk_get_nr_free(sm, &nr_free); 202 if (r) 203 return r; 204 205 r = sm_ll_commit(&smd->ll); 206 if (r) 207 return r; 208 209 memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll)); 210 smd->nr_allocated_this_transaction = 0; 211 212 r = sm_disk_get_nr_free(sm, &nr_free); 213 if (r) 214 return r; 215 216 return 0; 217} 218 219static int sm_disk_root_size(struct dm_space_map *sm, size_t *result) 220{ 221 *result = sizeof(struct disk_sm_root); 222 223 return 0; 224} 225 226static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max) 227{ 228 struct sm_disk *smd = container_of(sm, struct sm_disk, sm); 229 struct disk_sm_root root_le; 230 231 root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks); 232 root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated); 233 root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root); 234 root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root); 235 236 if (max < sizeof(root_le)) 237 return -ENOSPC; 238 239 memcpy(where_le, &root_le, sizeof(root_le)); 240 241 return 0; 242} 243 244/*----------------------------------------------------------------*/ 245 246static struct dm_space_map ops = { 247 .destroy = sm_disk_destroy, 248 .extend = sm_disk_extend, 249 .get_nr_blocks = sm_disk_get_nr_blocks, 250 .get_nr_free = sm_disk_get_nr_free, 251 .get_count = sm_disk_get_count, 252 .count_is_more_than_one = sm_disk_count_is_more_than_one, 253 .set_count = sm_disk_set_count, 254 .inc_block = sm_disk_inc_block, 255 .dec_block = sm_disk_dec_block, 256 .new_block = sm_disk_new_block, 257 .commit = sm_disk_commit, 258 .root_size = sm_disk_root_size, 259 .copy_root = sm_disk_copy_root, 260 .register_threshold_callback = NULL 261}; 262 263struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm, 264 dm_block_t nr_blocks) 265{ 266 int r; 267 struct sm_disk *smd; 268 269 smd = kmalloc(sizeof(*smd), GFP_KERNEL); 270 if (!smd) 271 return ERR_PTR(-ENOMEM); 272 273 smd->begin = 0; 274 smd->nr_allocated_this_transaction = 0; 275 memcpy(&smd->sm, &ops, sizeof(smd->sm)); 276 277 r = sm_ll_new_disk(&smd->ll, tm); 278 if (r) 279 goto bad; 280 281 r = sm_ll_extend(&smd->ll, nr_blocks); 282 if (r) 283 goto bad; 284 285 r = sm_disk_commit(&smd->sm); 286 if (r) 287 goto bad; 288 289 return &smd->sm; 290 291bad: 292 kfree(smd); 293 return ERR_PTR(r); 294} 295EXPORT_SYMBOL_GPL(dm_sm_disk_create); 296 297struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm, 298 void *root_le, size_t len) 299{ 300 int r; 301 struct sm_disk *smd; 302 303 smd = kmalloc(sizeof(*smd), GFP_KERNEL); 304 if (!smd) 305 return ERR_PTR(-ENOMEM); 306 307 smd->begin = 0; 308 smd->nr_allocated_this_transaction = 0; 309 memcpy(&smd->sm, &ops, sizeof(smd->sm)); 310 311 r = sm_ll_open_disk(&smd->ll, tm, root_le, len); 312 if (r) 313 goto bad; 314 315 r = sm_disk_commit(&smd->sm); 316 if (r) 317 goto bad; 318 319 return &smd->sm; 320 321bad: 322 kfree(smd); 323 return ERR_PTR(r); 324} 325EXPORT_SYMBOL_GPL(dm_sm_disk_open); 326 327/*----------------------------------------------------------------*/ 328