4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
6 * This file is released under the GPL.
15 #include <linux/pagemap.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
19 /*-----------------------------------------------------------------
20 * Persistent snapshots, by persistent we mean that the snapshot
21 * will survive a reboot.
22 *---------------------------------------------------------------*/
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.
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
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
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
49 * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
51 #define SNAP_MAGIC 0x70416e53
54 * The on-disk version of the metadata.
56 #define SNAPSHOT_DISK_VERSION 1
62 * Is this snapshot valid. There is no way of recovering
63 * an invalid snapshot.
68 * Simple, incrementing version. no backward
77 struct disk_exception {
82 struct commit_callback {
83 void (*callback)(void *, int success);
88 * The top level structure for a persistent exception store.
91 struct dm_snapshot *snap; /* up pointer to my snapshot */
94 uint32_t exceptions_per_area;
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.
104 * Used to keep track of which metadata area the data in
107 uint32_t current_area;
110 * The next free chunk for an exception.
115 * The index of next free exception in the current
118 uint32_t current_committed;
120 atomic_t pending_count;
121 uint32_t callback_count;
122 struct commit_callback *callbacks;
125 static inline unsigned int sectors_to_pages(unsigned int sectors)
127 return sectors / (PAGE_SIZE >> 9);
130 static int alloc_area(struct pstore *ps)
135 len = ps->snap->chunk_size << SECTOR_SHIFT;
138 * Allocate the chunk_size block of memory that will hold
139 * a single metadata area.
141 ps->area = vmalloc(len);
148 static void free_area(struct pstore *ps)
154 * Read or write a chunk aligned and sized block of data from a device.
156 static int chunk_io(struct pstore *ps, uint32_t chunk, int rw)
158 struct io_region where;
161 where.bdev = ps->snap->cow->bdev;
162 where.sector = ps->snap->chunk_size * chunk;
163 where.count = ps->snap->chunk_size;
165 return dm_io_sync_vm(1, &where, rw, ps->area, &bits);
169 * Read or write a metadata area. Remembering to skip the first
170 * chunk which holds the header.
172 static int area_io(struct pstore *ps, uint32_t area, int rw)
177 /* convert a metadata area index to a chunk index */
178 chunk = 1 + ((ps->exceptions_per_area + 1) * area);
180 r = chunk_io(ps, chunk, rw);
184 ps->current_area = area;
188 static int zero_area(struct pstore *ps, uint32_t area)
190 memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
191 return area_io(ps, area, WRITE);
194 static int read_header(struct pstore *ps, int *new_snapshot)
197 struct disk_header *dh;
200 r = chunk_io(ps, 0, READ);
204 dh = (struct disk_header *) ps->area;
206 if (le32_to_cpu(dh->magic) == 0) {
209 } else if (le32_to_cpu(dh->magic) == SNAP_MAGIC) {
211 ps->valid = le32_to_cpu(dh->valid);
212 ps->version = le32_to_cpu(dh->version);
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);
220 /* We had a bogus chunk_size. Fix stuff up. */
221 dm_io_put(sectors_to_pages(ps->snap->chunk_size));
224 ps->snap->chunk_size = chunk_size;
225 ps->snap->chunk_mask = chunk_size - 1;
226 ps->snap->chunk_shift = ffs(chunk_size) - 1;
232 r = dm_io_get(sectors_to_pages(chunk_size));
237 DMWARN("Invalid/corrupt snapshot");
244 static int write_header(struct pstore *ps)
246 struct disk_header *dh;
248 memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
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);
254 dh->chunk_size = cpu_to_le32(ps->snap->chunk_size);
256 return chunk_io(ps, 0, WRITE);
260 * Access functions for the disk exceptions, these do the endian conversions.
262 static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
264 if (index >= ps->exceptions_per_area)
267 return ((struct disk_exception *) ps->area) + index;
270 static int read_exception(struct pstore *ps,
271 uint32_t index, struct disk_exception *result)
273 struct disk_exception *e;
275 e = get_exception(ps, index);
280 result->old_chunk = le64_to_cpu(e->old_chunk);
281 result->new_chunk = le64_to_cpu(e->new_chunk);
286 static int write_exception(struct pstore *ps,
287 uint32_t index, struct disk_exception *de)
289 struct disk_exception *e;
291 e = get_exception(ps, index);
296 e->old_chunk = cpu_to_le64(de->old_chunk);
297 e->new_chunk = cpu_to_le64(de->new_chunk);
303 * Registers the exceptions that are present in the current area.
304 * 'full' is filled in to indicate if the area has been
307 static int insert_exceptions(struct pstore *ps, int *full)
311 struct disk_exception de;
313 /* presume the area is full */
316 for (i = 0; i < ps->exceptions_per_area; i++) {
317 r = read_exception(ps, i, &de);
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.
328 if (de.new_chunk == 0LL) {
329 ps->current_committed = i;
335 * Keep track of the start of the free chunks.
337 if (ps->next_free <= de.new_chunk)
338 ps->next_free = de.new_chunk + 1;
341 * Otherwise we add the exception to the snapshot.
343 r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk);
351 static int read_exceptions(struct pstore *ps)
357 * Keeping reading chunks and inserting exceptions until
358 * we find a partially full area.
360 for (area = 0; full; area++) {
361 r = area_io(ps, area, READ);
365 r = insert_exceptions(ps, &full);
373 static inline struct pstore *get_info(struct exception_store *store)
375 return (struct pstore *) store->context;
378 static void persistent_fraction_full(struct exception_store *store,
379 sector_t *numerator, sector_t *denominator)
381 *numerator = get_info(store)->next_free * store->snap->chunk_size;
382 *denominator = get_dev_size(store->snap->cow->bdev);
385 static void persistent_destroy(struct exception_store *store)
387 struct pstore *ps = get_info(store);
389 dm_io_put(sectors_to_pages(ps->snap->chunk_size));
390 vfree(ps->callbacks);
395 static int persistent_read_metadata(struct exception_store *store)
398 struct pstore *ps = get_info(store);
401 * Read the snapshot header.
403 r = read_header(ps, &new_snapshot);
408 * Now we know correct chunk_size, complete the initialisation.
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));
418 * Do we need to setup a new snapshot ?
421 r = write_header(ps);
423 DMWARN("write_header failed");
427 r = zero_area(ps, 0);
429 DMWARN("zero_area(0) failed");
438 DMWARN("snapshot is marked invalid");
442 if (ps->version != SNAPSHOT_DISK_VERSION) {
443 DMWARN("unable to handle snapshot disk version %d",
451 r = read_exceptions(ps);
459 static int persistent_prepare(struct exception_store *store,
462 struct pstore *ps = get_info(store);
464 sector_t size = get_dev_size(store->snap->cow->bdev);
466 /* Is there enough room ? */
467 if (size < ((ps->next_free + 1) * store->snap->chunk_size))
470 e->new_chunk = ps->next_free;
473 * Move onto the next free pending, making sure to take
474 * into account the location of the metadata chunks.
476 stride = (ps->exceptions_per_area + 1);
477 if ((++ps->next_free % stride) == 1)
480 atomic_inc(&ps->pending_count);
484 static void persistent_commit(struct exception_store *store,
486 void (*callback) (void *, int success),
487 void *callback_context)
491 struct pstore *ps = get_info(store);
492 struct disk_exception de;
493 struct commit_callback *cb;
495 de.old_chunk = e->old_chunk;
496 de.new_chunk = e->new_chunk;
497 write_exception(ps, ps->current_committed++, &de);
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.
505 cb = ps->callbacks + ps->callback_count++;
506 cb->callback = callback;
507 cb->context = callback_context;
510 * If there are no more exceptions in flight, or we have
511 * filled this metadata area we commit the exceptions to
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);
520 for (i = 0; i < ps->callback_count; i++) {
521 cb = ps->callbacks + i;
522 cb->callback(cb->context, r == 0 ? 1 : 0);
525 ps->callback_count = 0;
529 * Have we completely filled the current area ?
531 if (ps->current_committed == ps->exceptions_per_area) {
532 ps->current_committed = 0;
533 r = zero_area(ps, ps->current_area + 1);
539 static void persistent_drop(struct exception_store *store)
541 struct pstore *ps = get_info(store);
544 if (write_header(ps))
545 DMWARN("write header failed");
548 int dm_create_persistent(struct exception_store *store, uint32_t chunk_size)
553 r = dm_io_get(sectors_to_pages(chunk_size));
557 /* allocate the pstore */
558 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
564 ps->snap = store->snap;
566 ps->version = SNAPSHOT_DISK_VERSION;
567 ps->next_free = 2; /* skipping the header and first area */
568 ps->current_committed = 0;
574 ps->callback_count = 0;
575 atomic_set(&ps->pending_count, 0);
576 ps->callbacks = NULL;
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;
589 dm_io_put(sectors_to_pages(chunk_size));
596 /*-----------------------------------------------------------------
597 * Implementation of the store for non-persistent snapshots.
598 *---------------------------------------------------------------*/
603 static void transient_destroy(struct exception_store *store)
605 kfree(store->context);
608 static int transient_read_metadata(struct exception_store *store)
613 static int transient_prepare(struct exception_store *store, struct exception *e)
615 struct transient_c *tc = (struct transient_c *) store->context;
616 sector_t size = get_dev_size(store->snap->cow->bdev);
618 if (size < (tc->next_free + store->snap->chunk_size))
621 e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
622 tc->next_free += store->snap->chunk_size;
627 static void transient_commit(struct exception_store *store,
629 void (*callback) (void *, int success),
630 void *callback_context)
633 callback(callback_context, 1);
636 static void transient_fraction_full(struct exception_store *store,
637 sector_t *numerator, sector_t *denominator)
639 *numerator = ((struct transient_c *) store->context)->next_free;
640 *denominator = get_dev_size(store->snap->cow->bdev);
643 int dm_create_transient(struct exception_store *store,
644 struct dm_snapshot *s, int blocksize)
646 struct transient_c *tc;
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;
656 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);