Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dm-snapshot.c |
| 3 | * |
| 4 | * Copyright (C) 2001-2002 Sistina Software (UK) Limited. |
| 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
| 9 | #include "dm.h" |
| 10 | #include "dm-snap.h" |
| 11 | #include "dm-io.h" |
| 12 | #include "kcopyd.h" |
| 13 | |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/slab.h> |
| 18 | |
| 19 | /*----------------------------------------------------------------- |
| 20 | * Persistent snapshots, by persistent we mean that the snapshot |
| 21 | * will survive a reboot. |
| 22 | *---------------------------------------------------------------*/ |
| 23 | |
| 24 | /* |
| 25 | * We need to store a record of which parts of the origin have |
| 26 | * been copied to the snapshot device. The snapshot code |
| 27 | * requires that we copy exception chunks to chunk aligned areas |
| 28 | * of the COW store. It makes sense therefore, to store the |
| 29 | * metadata in chunk size blocks. |
| 30 | * |
| 31 | * There is no backward or forward compatibility implemented, |
| 32 | * snapshots with different disk versions than the kernel will |
| 33 | * not be usable. It is expected that "lvcreate" will blank out |
| 34 | * the start of a fresh COW device before calling the snapshot |
| 35 | * constructor. |
| 36 | * |
| 37 | * The first chunk of the COW device just contains the header. |
| 38 | * After this there is a chunk filled with exception metadata, |
| 39 | * followed by as many exception chunks as can fit in the |
| 40 | * metadata areas. |
| 41 | * |
| 42 | * All on disk structures are in little-endian format. The end |
| 43 | * of the exceptions info is indicated by an exception with a |
| 44 | * new_chunk of 0, which is invalid since it would point to the |
| 45 | * header chunk. |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * Magic for persistent snapshots: "SnAp" - Feeble isn't it. |
| 50 | */ |
| 51 | #define SNAP_MAGIC 0x70416e53 |
| 52 | |
| 53 | /* |
| 54 | * The on-disk version of the metadata. |
| 55 | */ |
| 56 | #define SNAPSHOT_DISK_VERSION 1 |
| 57 | |
| 58 | struct disk_header { |
| 59 | uint32_t magic; |
| 60 | |
| 61 | /* |
| 62 | * Is this snapshot valid. There is no way of recovering |
| 63 | * an invalid snapshot. |
| 64 | */ |
| 65 | uint32_t valid; |
| 66 | |
| 67 | /* |
| 68 | * Simple, incrementing version. no backward |
| 69 | * compatibility. |
| 70 | */ |
| 71 | uint32_t version; |
| 72 | |
| 73 | /* In sectors */ |
| 74 | uint32_t chunk_size; |
| 75 | }; |
| 76 | |
| 77 | struct disk_exception { |
| 78 | uint64_t old_chunk; |
| 79 | uint64_t new_chunk; |
| 80 | }; |
| 81 | |
| 82 | struct commit_callback { |
| 83 | void (*callback)(void *, int success); |
| 84 | void *context; |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * The top level structure for a persistent exception store. |
| 89 | */ |
| 90 | struct pstore { |
| 91 | struct dm_snapshot *snap; /* up pointer to my snapshot */ |
| 92 | int version; |
| 93 | int valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | uint32_t exceptions_per_area; |
| 95 | |
| 96 | /* |
| 97 | * Now that we have an asynchronous kcopyd there is no |
| 98 | * need for large chunk sizes, so it wont hurt to have a |
| 99 | * whole chunks worth of metadata in memory at once. |
| 100 | */ |
| 101 | void *area; |
| 102 | |
| 103 | /* |
| 104 | * Used to keep track of which metadata area the data in |
| 105 | * 'chunk' refers to. |
| 106 | */ |
| 107 | uint32_t current_area; |
| 108 | |
| 109 | /* |
| 110 | * The next free chunk for an exception. |
| 111 | */ |
| 112 | uint32_t next_free; |
| 113 | |
| 114 | /* |
| 115 | * The index of next free exception in the current |
| 116 | * metadata area. |
| 117 | */ |
| 118 | uint32_t current_committed; |
| 119 | |
| 120 | atomic_t pending_count; |
| 121 | uint32_t callback_count; |
| 122 | struct commit_callback *callbacks; |
| 123 | }; |
| 124 | |
| 125 | static inline unsigned int sectors_to_pages(unsigned int sectors) |
| 126 | { |
| 127 | return sectors / (PAGE_SIZE >> 9); |
| 128 | } |
| 129 | |
| 130 | static int alloc_area(struct pstore *ps) |
| 131 | { |
| 132 | int r = -ENOMEM; |
| 133 | size_t len; |
| 134 | |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 135 | len = ps->snap->chunk_size << SECTOR_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * Allocate the chunk_size block of memory that will hold |
| 139 | * a single metadata area. |
| 140 | */ |
| 141 | ps->area = vmalloc(len); |
| 142 | if (!ps->area) |
| 143 | return r; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static void free_area(struct pstore *ps) |
| 149 | { |
| 150 | vfree(ps->area); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Read or write a chunk aligned and sized block of data from a device. |
| 155 | */ |
| 156 | static int chunk_io(struct pstore *ps, uint32_t chunk, int rw) |
| 157 | { |
| 158 | struct io_region where; |
| 159 | unsigned long bits; |
| 160 | |
| 161 | where.bdev = ps->snap->cow->bdev; |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 162 | where.sector = ps->snap->chunk_size * chunk; |
| 163 | where.count = ps->snap->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | return dm_io_sync_vm(1, &where, rw, ps->area, &bits); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Read or write a metadata area. Remembering to skip the first |
| 170 | * chunk which holds the header. |
| 171 | */ |
| 172 | static int area_io(struct pstore *ps, uint32_t area, int rw) |
| 173 | { |
| 174 | int r; |
| 175 | uint32_t chunk; |
| 176 | |
| 177 | /* convert a metadata area index to a chunk index */ |
| 178 | chunk = 1 + ((ps->exceptions_per_area + 1) * area); |
| 179 | |
| 180 | r = chunk_io(ps, chunk, rw); |
| 181 | if (r) |
| 182 | return r; |
| 183 | |
| 184 | ps->current_area = area; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int zero_area(struct pstore *ps, uint32_t area) |
| 189 | { |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 190 | memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return area_io(ps, area, WRITE); |
| 192 | } |
| 193 | |
| 194 | static int read_header(struct pstore *ps, int *new_snapshot) |
| 195 | { |
| 196 | int r; |
| 197 | struct disk_header *dh; |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 198 | chunk_t chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | r = chunk_io(ps, 0, READ); |
| 201 | if (r) |
| 202 | return r; |
| 203 | |
| 204 | dh = (struct disk_header *) ps->area; |
| 205 | |
| 206 | if (le32_to_cpu(dh->magic) == 0) { |
| 207 | *new_snapshot = 1; |
| 208 | |
| 209 | } else if (le32_to_cpu(dh->magic) == SNAP_MAGIC) { |
| 210 | *new_snapshot = 0; |
| 211 | ps->valid = le32_to_cpu(dh->valid); |
| 212 | ps->version = le32_to_cpu(dh->version); |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 213 | chunk_size = le32_to_cpu(dh->chunk_size); |
| 214 | if (ps->snap->chunk_size != chunk_size) { |
| 215 | DMWARN("chunk size %llu in device metadata overrides " |
| 216 | "table chunk size of %llu.", |
| 217 | (unsigned long long)chunk_size, |
| 218 | (unsigned long long)ps->snap->chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 220 | /* We had a bogus chunk_size. Fix stuff up. */ |
| 221 | dm_io_put(sectors_to_pages(ps->snap->chunk_size)); |
| 222 | free_area(ps); |
| 223 | |
| 224 | ps->snap->chunk_size = chunk_size; |
| 225 | ps->snap->chunk_mask = chunk_size - 1; |
| 226 | ps->snap->chunk_shift = ffs(chunk_size) - 1; |
| 227 | |
| 228 | r = alloc_area(ps); |
| 229 | if (r) |
| 230 | return r; |
| 231 | |
| 232 | r = dm_io_get(sectors_to_pages(chunk_size)); |
| 233 | if (r) |
| 234 | return r; |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } else { |
| 237 | DMWARN("Invalid/corrupt snapshot"); |
| 238 | r = -ENXIO; |
| 239 | } |
| 240 | |
| 241 | return r; |
| 242 | } |
| 243 | |
| 244 | static int write_header(struct pstore *ps) |
| 245 | { |
| 246 | struct disk_header *dh; |
| 247 | |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 248 | memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | dh = (struct disk_header *) ps->area; |
| 251 | dh->magic = cpu_to_le32(SNAP_MAGIC); |
| 252 | dh->valid = cpu_to_le32(ps->valid); |
| 253 | dh->version = cpu_to_le32(ps->version); |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 254 | dh->chunk_size = cpu_to_le32(ps->snap->chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | return chunk_io(ps, 0, WRITE); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Access functions for the disk exceptions, these do the endian conversions. |
| 261 | */ |
| 262 | static struct disk_exception *get_exception(struct pstore *ps, uint32_t index) |
| 263 | { |
| 264 | if (index >= ps->exceptions_per_area) |
| 265 | return NULL; |
| 266 | |
| 267 | return ((struct disk_exception *) ps->area) + index; |
| 268 | } |
| 269 | |
| 270 | static int read_exception(struct pstore *ps, |
| 271 | uint32_t index, struct disk_exception *result) |
| 272 | { |
| 273 | struct disk_exception *e; |
| 274 | |
| 275 | e = get_exception(ps, index); |
| 276 | if (!e) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | /* copy it */ |
| 280 | result->old_chunk = le64_to_cpu(e->old_chunk); |
| 281 | result->new_chunk = le64_to_cpu(e->new_chunk); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int write_exception(struct pstore *ps, |
| 287 | uint32_t index, struct disk_exception *de) |
| 288 | { |
| 289 | struct disk_exception *e; |
| 290 | |
| 291 | e = get_exception(ps, index); |
| 292 | if (!e) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | /* copy it */ |
| 296 | e->old_chunk = cpu_to_le64(de->old_chunk); |
| 297 | e->new_chunk = cpu_to_le64(de->new_chunk); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Registers the exceptions that are present in the current area. |
| 304 | * 'full' is filled in to indicate if the area has been |
| 305 | * filled. |
| 306 | */ |
| 307 | static int insert_exceptions(struct pstore *ps, int *full) |
| 308 | { |
| 309 | int r; |
| 310 | unsigned int i; |
| 311 | struct disk_exception de; |
| 312 | |
| 313 | /* presume the area is full */ |
| 314 | *full = 1; |
| 315 | |
| 316 | for (i = 0; i < ps->exceptions_per_area; i++) { |
| 317 | r = read_exception(ps, i, &de); |
| 318 | |
| 319 | if (r) |
| 320 | return r; |
| 321 | |
| 322 | /* |
| 323 | * If the new_chunk is pointing at the start of |
| 324 | * the COW device, where the first metadata area |
| 325 | * is we know that we've hit the end of the |
| 326 | * exceptions. Therefore the area is not full. |
| 327 | */ |
| 328 | if (de.new_chunk == 0LL) { |
| 329 | ps->current_committed = i; |
| 330 | *full = 0; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Keep track of the start of the free chunks. |
| 336 | */ |
| 337 | if (ps->next_free <= de.new_chunk) |
| 338 | ps->next_free = de.new_chunk + 1; |
| 339 | |
| 340 | /* |
| 341 | * Otherwise we add the exception to the snapshot. |
| 342 | */ |
| 343 | r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk); |
| 344 | if (r) |
| 345 | return r; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int read_exceptions(struct pstore *ps) |
| 352 | { |
| 353 | uint32_t area; |
| 354 | int r, full = 1; |
| 355 | |
| 356 | /* |
| 357 | * Keeping reading chunks and inserting exceptions until |
| 358 | * we find a partially full area. |
| 359 | */ |
| 360 | for (area = 0; full; area++) { |
| 361 | r = area_io(ps, area, READ); |
| 362 | if (r) |
| 363 | return r; |
| 364 | |
| 365 | r = insert_exceptions(ps, &full); |
| 366 | if (r) |
| 367 | return r; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static inline struct pstore *get_info(struct exception_store *store) |
| 374 | { |
| 375 | return (struct pstore *) store->context; |
| 376 | } |
| 377 | |
| 378 | static void persistent_fraction_full(struct exception_store *store, |
| 379 | sector_t *numerator, sector_t *denominator) |
| 380 | { |
| 381 | *numerator = get_info(store)->next_free * store->snap->chunk_size; |
| 382 | *denominator = get_dev_size(store->snap->cow->bdev); |
| 383 | } |
| 384 | |
| 385 | static void persistent_destroy(struct exception_store *store) |
| 386 | { |
| 387 | struct pstore *ps = get_info(store); |
| 388 | |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 389 | dm_io_put(sectors_to_pages(ps->snap->chunk_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | vfree(ps->callbacks); |
| 391 | free_area(ps); |
| 392 | kfree(ps); |
| 393 | } |
| 394 | |
| 395 | static int persistent_read_metadata(struct exception_store *store) |
| 396 | { |
| 397 | int r, new_snapshot; |
| 398 | struct pstore *ps = get_info(store); |
| 399 | |
| 400 | /* |
| 401 | * Read the snapshot header. |
| 402 | */ |
| 403 | r = read_header(ps, &new_snapshot); |
| 404 | if (r) |
| 405 | return r; |
| 406 | |
| 407 | /* |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 408 | * Now we know correct chunk_size, complete the initialisation. |
| 409 | */ |
| 410 | ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) / |
| 411 | sizeof(struct disk_exception); |
| 412 | ps->callbacks = dm_vcalloc(ps->exceptions_per_area, |
| 413 | sizeof(*ps->callbacks)); |
| 414 | if (!ps->callbacks) |
| 415 | return -ENOMEM; |
| 416 | |
| 417 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | * Do we need to setup a new snapshot ? |
| 419 | */ |
| 420 | if (new_snapshot) { |
| 421 | r = write_header(ps); |
| 422 | if (r) { |
| 423 | DMWARN("write_header failed"); |
| 424 | return r; |
| 425 | } |
| 426 | |
| 427 | r = zero_area(ps, 0); |
| 428 | if (r) { |
| 429 | DMWARN("zero_area(0) failed"); |
| 430 | return r; |
| 431 | } |
| 432 | |
| 433 | } else { |
| 434 | /* |
| 435 | * Sanity checks. |
| 436 | */ |
| 437 | if (!ps->valid) { |
| 438 | DMWARN("snapshot is marked invalid"); |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
| 442 | if (ps->version != SNAPSHOT_DISK_VERSION) { |
| 443 | DMWARN("unable to handle snapshot disk version %d", |
| 444 | ps->version); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Read the metadata. |
| 450 | */ |
| 451 | r = read_exceptions(ps); |
| 452 | if (r) |
| 453 | return r; |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int persistent_prepare(struct exception_store *store, |
| 460 | struct exception *e) |
| 461 | { |
| 462 | struct pstore *ps = get_info(store); |
| 463 | uint32_t stride; |
| 464 | sector_t size = get_dev_size(store->snap->cow->bdev); |
| 465 | |
| 466 | /* Is there enough room ? */ |
| 467 | if (size < ((ps->next_free + 1) * store->snap->chunk_size)) |
| 468 | return -ENOSPC; |
| 469 | |
| 470 | e->new_chunk = ps->next_free; |
| 471 | |
| 472 | /* |
| 473 | * Move onto the next free pending, making sure to take |
| 474 | * into account the location of the metadata chunks. |
| 475 | */ |
| 476 | stride = (ps->exceptions_per_area + 1); |
| 477 | if ((++ps->next_free % stride) == 1) |
| 478 | ps->next_free++; |
| 479 | |
| 480 | atomic_inc(&ps->pending_count); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static void persistent_commit(struct exception_store *store, |
| 485 | struct exception *e, |
| 486 | void (*callback) (void *, int success), |
| 487 | void *callback_context) |
| 488 | { |
| 489 | int r; |
| 490 | unsigned int i; |
| 491 | struct pstore *ps = get_info(store); |
| 492 | struct disk_exception de; |
| 493 | struct commit_callback *cb; |
| 494 | |
| 495 | de.old_chunk = e->old_chunk; |
| 496 | de.new_chunk = e->new_chunk; |
| 497 | write_exception(ps, ps->current_committed++, &de); |
| 498 | |
| 499 | /* |
| 500 | * Add the callback to the back of the array. This code |
| 501 | * is the only place where the callback array is |
| 502 | * manipulated, and we know that it will never be called |
| 503 | * multiple times concurrently. |
| 504 | */ |
| 505 | cb = ps->callbacks + ps->callback_count++; |
| 506 | cb->callback = callback; |
| 507 | cb->context = callback_context; |
| 508 | |
| 509 | /* |
| 510 | * If there are no more exceptions in flight, or we have |
| 511 | * filled this metadata area we commit the exceptions to |
| 512 | * disk. |
| 513 | */ |
| 514 | if (atomic_dec_and_test(&ps->pending_count) || |
| 515 | (ps->current_committed == ps->exceptions_per_area)) { |
| 516 | r = area_io(ps, ps->current_area, WRITE); |
| 517 | if (r) |
| 518 | ps->valid = 0; |
| 519 | |
| 520 | for (i = 0; i < ps->callback_count; i++) { |
| 521 | cb = ps->callbacks + i; |
| 522 | cb->callback(cb->context, r == 0 ? 1 : 0); |
| 523 | } |
| 524 | |
| 525 | ps->callback_count = 0; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Have we completely filled the current area ? |
| 530 | */ |
| 531 | if (ps->current_committed == ps->exceptions_per_area) { |
| 532 | ps->current_committed = 0; |
| 533 | r = zero_area(ps, ps->current_area + 1); |
| 534 | if (r) |
| 535 | ps->valid = 0; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | static void persistent_drop(struct exception_store *store) |
| 540 | { |
| 541 | struct pstore *ps = get_info(store); |
| 542 | |
| 543 | ps->valid = 0; |
| 544 | if (write_header(ps)) |
| 545 | DMWARN("write header failed"); |
| 546 | } |
| 547 | |
| 548 | int dm_create_persistent(struct exception_store *store, uint32_t chunk_size) |
| 549 | { |
| 550 | int r; |
| 551 | struct pstore *ps; |
| 552 | |
| 553 | r = dm_io_get(sectors_to_pages(chunk_size)); |
| 554 | if (r) |
| 555 | return r; |
| 556 | |
| 557 | /* allocate the pstore */ |
| 558 | ps = kmalloc(sizeof(*ps), GFP_KERNEL); |
| 559 | if (!ps) { |
| 560 | r = -ENOMEM; |
| 561 | goto bad; |
| 562 | } |
| 563 | |
| 564 | ps->snap = store->snap; |
| 565 | ps->valid = 1; |
| 566 | ps->version = SNAPSHOT_DISK_VERSION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | ps->next_free = 2; /* skipping the header and first area */ |
| 568 | ps->current_committed = 0; |
| 569 | |
| 570 | r = alloc_area(ps); |
| 571 | if (r) |
| 572 | goto bad; |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | ps->callback_count = 0; |
| 575 | atomic_set(&ps->pending_count, 0); |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame^] | 576 | ps->callbacks = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | store->destroy = persistent_destroy; |
| 579 | store->read_metadata = persistent_read_metadata; |
| 580 | store->prepare_exception = persistent_prepare; |
| 581 | store->commit_exception = persistent_commit; |
| 582 | store->drop_snapshot = persistent_drop; |
| 583 | store->fraction_full = persistent_fraction_full; |
| 584 | store->context = ps; |
| 585 | |
| 586 | return 0; |
| 587 | |
| 588 | bad: |
| 589 | dm_io_put(sectors_to_pages(chunk_size)); |
Jesper Juhl | f9101210 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 590 | if (ps && ps->area) |
| 591 | free_area(ps); |
| 592 | kfree(ps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | return r; |
| 594 | } |
| 595 | |
| 596 | /*----------------------------------------------------------------- |
| 597 | * Implementation of the store for non-persistent snapshots. |
| 598 | *---------------------------------------------------------------*/ |
| 599 | struct transient_c { |
| 600 | sector_t next_free; |
| 601 | }; |
| 602 | |
| 603 | static void transient_destroy(struct exception_store *store) |
| 604 | { |
| 605 | kfree(store->context); |
| 606 | } |
| 607 | |
| 608 | static int transient_read_metadata(struct exception_store *store) |
| 609 | { |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int transient_prepare(struct exception_store *store, struct exception *e) |
| 614 | { |
| 615 | struct transient_c *tc = (struct transient_c *) store->context; |
| 616 | sector_t size = get_dev_size(store->snap->cow->bdev); |
| 617 | |
| 618 | if (size < (tc->next_free + store->snap->chunk_size)) |
| 619 | return -1; |
| 620 | |
| 621 | e->new_chunk = sector_to_chunk(store->snap, tc->next_free); |
| 622 | tc->next_free += store->snap->chunk_size; |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static void transient_commit(struct exception_store *store, |
| 628 | struct exception *e, |
| 629 | void (*callback) (void *, int success), |
| 630 | void *callback_context) |
| 631 | { |
| 632 | /* Just succeed */ |
| 633 | callback(callback_context, 1); |
| 634 | } |
| 635 | |
| 636 | static void transient_fraction_full(struct exception_store *store, |
| 637 | sector_t *numerator, sector_t *denominator) |
| 638 | { |
| 639 | *numerator = ((struct transient_c *) store->context)->next_free; |
| 640 | *denominator = get_dev_size(store->snap->cow->bdev); |
| 641 | } |
| 642 | |
| 643 | int dm_create_transient(struct exception_store *store, |
| 644 | struct dm_snapshot *s, int blocksize) |
| 645 | { |
| 646 | struct transient_c *tc; |
| 647 | |
| 648 | memset(store, 0, sizeof(*store)); |
| 649 | store->destroy = transient_destroy; |
| 650 | store->read_metadata = transient_read_metadata; |
| 651 | store->prepare_exception = transient_prepare; |
| 652 | store->commit_exception = transient_commit; |
| 653 | store->fraction_full = transient_fraction_full; |
| 654 | store->snap = s; |
| 655 | |
| 656 | tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); |
| 657 | if (!tc) |
| 658 | return -ENOMEM; |
| 659 | |
| 660 | tc->next_free = 0; |
| 661 | store->context = tc; |
| 662 | |
| 663 | return 0; |
| 664 | } |