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 #define DM_MSG_PREFIX "snapshots"
20 #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
22 /*-----------------------------------------------------------------
23 * Persistent snapshots, by persistent we mean that the snapshot
24 * will survive a reboot.
25 *---------------------------------------------------------------*/
28 * We need to store a record of which parts of the origin have
29 * been copied to the snapshot device. The snapshot code
30 * requires that we copy exception chunks to chunk aligned areas
31 * of the COW store. It makes sense therefore, to store the
32 * metadata in chunk size blocks.
34 * There is no backward or forward compatibility implemented,
35 * snapshots with different disk versions than the kernel will
36 * not be usable. It is expected that "lvcreate" will blank out
37 * the start of a fresh COW device before calling the snapshot
40 * The first chunk of the COW device just contains the header.
41 * After this there is a chunk filled with exception metadata,
42 * followed by as many exception chunks as can fit in the
45 * All on disk structures are in little-endian format. The end
46 * of the exceptions info is indicated by an exception with a
47 * new_chunk of 0, which is invalid since it would point to the
52 * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
54 #define SNAP_MAGIC 0x70416e53
57 * The on-disk version of the metadata.
59 #define SNAPSHOT_DISK_VERSION 1
65 * Is this snapshot valid. There is no way of recovering
66 * an invalid snapshot.
71 * Simple, incrementing version. no backward
80 struct disk_exception {
85 struct commit_callback {
86 void (*callback)(void *, int success);
91 * The top level structure for a persistent exception store.
94 struct dm_snapshot *snap; /* up pointer to my snapshot */
97 uint32_t exceptions_per_area;
100 * Now that we have an asynchronous kcopyd there is no
101 * need for large chunk sizes, so it wont hurt to have a
102 * whole chunks worth of metadata in memory at once.
107 * Used to keep track of which metadata area the data in
110 uint32_t current_area;
113 * The next free chunk for an exception.
118 * The index of next free exception in the current
121 uint32_t current_committed;
123 atomic_t pending_count;
124 uint32_t callback_count;
125 struct commit_callback *callbacks;
128 static inline unsigned int sectors_to_pages(unsigned int sectors)
130 return sectors / (PAGE_SIZE >> 9);
133 static int alloc_area(struct pstore *ps)
138 len = ps->snap->chunk_size << SECTOR_SHIFT;
141 * Allocate the chunk_size block of memory that will hold
142 * a single metadata area.
144 ps->area = vmalloc(len);
151 static void free_area(struct pstore *ps)
158 * Read or write a chunk aligned and sized block of data from a device.
160 static int chunk_io(struct pstore *ps, uint32_t chunk, int rw)
162 struct io_region where;
165 where.bdev = ps->snap->cow->bdev;
166 where.sector = ps->snap->chunk_size * chunk;
167 where.count = ps->snap->chunk_size;
169 return dm_io_sync_vm(1, &where, rw, ps->area, &bits);
173 * Read or write a metadata area. Remembering to skip the first
174 * chunk which holds the header.
176 static int area_io(struct pstore *ps, uint32_t area, int rw)
181 /* convert a metadata area index to a chunk index */
182 chunk = 1 + ((ps->exceptions_per_area + 1) * area);
184 r = chunk_io(ps, chunk, rw);
188 ps->current_area = area;
192 static int zero_area(struct pstore *ps, uint32_t area)
194 memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
195 return area_io(ps, area, WRITE);
198 static int read_header(struct pstore *ps, int *new_snapshot)
201 struct disk_header *dh;
203 int chunk_size_supplied = 1;
206 * Use default chunk size (or hardsect_size, if larger) if none supplied
208 if (!ps->snap->chunk_size) {
209 ps->snap->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
210 bdev_hardsect_size(ps->snap->cow->bdev) >> 9);
211 ps->snap->chunk_mask = ps->snap->chunk_size - 1;
212 ps->snap->chunk_shift = ffs(ps->snap->chunk_size) - 1;
213 chunk_size_supplied = 0;
216 r = dm_io_get(sectors_to_pages(ps->snap->chunk_size));
224 r = chunk_io(ps, 0, READ);
228 dh = (struct disk_header *) ps->area;
230 if (le32_to_cpu(dh->magic) == 0) {
235 if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
236 DMWARN("Invalid or corrupt snapshot");
242 ps->valid = le32_to_cpu(dh->valid);
243 ps->version = le32_to_cpu(dh->version);
244 chunk_size = le32_to_cpu(dh->chunk_size);
246 if (!chunk_size_supplied || ps->snap->chunk_size == chunk_size)
249 DMWARN("chunk size %llu in device metadata overrides "
250 "table chunk size of %llu.",
251 (unsigned long long)chunk_size,
252 (unsigned long long)ps->snap->chunk_size);
254 /* We had a bogus chunk_size. Fix stuff up. */
255 dm_io_put(sectors_to_pages(ps->snap->chunk_size));
258 ps->snap->chunk_size = chunk_size;
259 ps->snap->chunk_mask = chunk_size - 1;
260 ps->snap->chunk_shift = ffs(chunk_size) - 1;
262 r = dm_io_get(sectors_to_pages(chunk_size));
275 dm_io_put(sectors_to_pages(ps->snap->chunk_size));
279 static int write_header(struct pstore *ps)
281 struct disk_header *dh;
283 memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
285 dh = (struct disk_header *) ps->area;
286 dh->magic = cpu_to_le32(SNAP_MAGIC);
287 dh->valid = cpu_to_le32(ps->valid);
288 dh->version = cpu_to_le32(ps->version);
289 dh->chunk_size = cpu_to_le32(ps->snap->chunk_size);
291 return chunk_io(ps, 0, WRITE);
295 * Access functions for the disk exceptions, these do the endian conversions.
297 static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
299 if (index >= ps->exceptions_per_area)
302 return ((struct disk_exception *) ps->area) + index;
305 static int read_exception(struct pstore *ps,
306 uint32_t index, struct disk_exception *result)
308 struct disk_exception *e;
310 e = get_exception(ps, index);
315 result->old_chunk = le64_to_cpu(e->old_chunk);
316 result->new_chunk = le64_to_cpu(e->new_chunk);
321 static int write_exception(struct pstore *ps,
322 uint32_t index, struct disk_exception *de)
324 struct disk_exception *e;
326 e = get_exception(ps, index);
331 e->old_chunk = cpu_to_le64(de->old_chunk);
332 e->new_chunk = cpu_to_le64(de->new_chunk);
338 * Registers the exceptions that are present in the current area.
339 * 'full' is filled in to indicate if the area has been
342 static int insert_exceptions(struct pstore *ps, int *full)
346 struct disk_exception de;
348 /* presume the area is full */
351 for (i = 0; i < ps->exceptions_per_area; i++) {
352 r = read_exception(ps, i, &de);
358 * If the new_chunk is pointing at the start of
359 * the COW device, where the first metadata area
360 * is we know that we've hit the end of the
361 * exceptions. Therefore the area is not full.
363 if (de.new_chunk == 0LL) {
364 ps->current_committed = i;
370 * Keep track of the start of the free chunks.
372 if (ps->next_free <= de.new_chunk)
373 ps->next_free = de.new_chunk + 1;
376 * Otherwise we add the exception to the snapshot.
378 r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk);
386 static int read_exceptions(struct pstore *ps)
392 * Keeping reading chunks and inserting exceptions until
393 * we find a partially full area.
395 for (area = 0; full; area++) {
396 r = area_io(ps, area, READ);
400 r = insert_exceptions(ps, &full);
408 static inline struct pstore *get_info(struct exception_store *store)
410 return (struct pstore *) store->context;
413 static void persistent_fraction_full(struct exception_store *store,
414 sector_t *numerator, sector_t *denominator)
416 *numerator = get_info(store)->next_free * store->snap->chunk_size;
417 *denominator = get_dev_size(store->snap->cow->bdev);
420 static void persistent_destroy(struct exception_store *store)
422 struct pstore *ps = get_info(store);
424 dm_io_put(sectors_to_pages(ps->snap->chunk_size));
425 vfree(ps->callbacks);
430 static int persistent_read_metadata(struct exception_store *store)
433 struct pstore *ps = get_info(store);
436 * Read the snapshot header.
438 r = read_header(ps, &new_snapshot);
443 * Now we know correct chunk_size, complete the initialisation.
445 ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) /
446 sizeof(struct disk_exception);
447 ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
448 sizeof(*ps->callbacks));
453 * Do we need to setup a new snapshot ?
456 r = write_header(ps);
458 DMWARN("write_header failed");
462 r = zero_area(ps, 0);
464 DMWARN("zero_area(0) failed");
473 DMWARN("snapshot is marked invalid");
477 if (ps->version != SNAPSHOT_DISK_VERSION) {
478 DMWARN("unable to handle snapshot disk version %d",
486 r = read_exceptions(ps);
494 static int persistent_prepare(struct exception_store *store,
497 struct pstore *ps = get_info(store);
499 sector_t size = get_dev_size(store->snap->cow->bdev);
501 /* Is there enough room ? */
502 if (size < ((ps->next_free + 1) * store->snap->chunk_size))
505 e->new_chunk = ps->next_free;
508 * Move onto the next free pending, making sure to take
509 * into account the location of the metadata chunks.
511 stride = (ps->exceptions_per_area + 1);
512 if ((++ps->next_free % stride) == 1)
515 atomic_inc(&ps->pending_count);
519 static void persistent_commit(struct exception_store *store,
521 void (*callback) (void *, int success),
522 void *callback_context)
526 struct pstore *ps = get_info(store);
527 struct disk_exception de;
528 struct commit_callback *cb;
530 de.old_chunk = e->old_chunk;
531 de.new_chunk = e->new_chunk;
532 write_exception(ps, ps->current_committed++, &de);
535 * Add the callback to the back of the array. This code
536 * is the only place where the callback array is
537 * manipulated, and we know that it will never be called
538 * multiple times concurrently.
540 cb = ps->callbacks + ps->callback_count++;
541 cb->callback = callback;
542 cb->context = callback_context;
545 * If there are no more exceptions in flight, or we have
546 * filled this metadata area we commit the exceptions to
549 if (atomic_dec_and_test(&ps->pending_count) ||
550 (ps->current_committed == ps->exceptions_per_area)) {
551 r = area_io(ps, ps->current_area, WRITE);
555 for (i = 0; i < ps->callback_count; i++) {
556 cb = ps->callbacks + i;
557 cb->callback(cb->context, r == 0 ? 1 : 0);
560 ps->callback_count = 0;
564 * Have we completely filled the current area ?
566 if (ps->current_committed == ps->exceptions_per_area) {
567 ps->current_committed = 0;
568 r = zero_area(ps, ps->current_area + 1);
574 static void persistent_drop(struct exception_store *store)
576 struct pstore *ps = get_info(store);
579 if (write_header(ps))
580 DMWARN("write header failed");
583 int dm_create_persistent(struct exception_store *store)
587 /* allocate the pstore */
588 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
592 ps->snap = store->snap;
594 ps->version = SNAPSHOT_DISK_VERSION;
596 ps->next_free = 2; /* skipping the header and first area */
597 ps->current_committed = 0;
599 ps->callback_count = 0;
600 atomic_set(&ps->pending_count, 0);
601 ps->callbacks = NULL;
603 store->destroy = persistent_destroy;
604 store->read_metadata = persistent_read_metadata;
605 store->prepare_exception = persistent_prepare;
606 store->commit_exception = persistent_commit;
607 store->drop_snapshot = persistent_drop;
608 store->fraction_full = persistent_fraction_full;
614 /*-----------------------------------------------------------------
615 * Implementation of the store for non-persistent snapshots.
616 *---------------------------------------------------------------*/
621 static void transient_destroy(struct exception_store *store)
623 kfree(store->context);
626 static int transient_read_metadata(struct exception_store *store)
631 static int transient_prepare(struct exception_store *store, struct exception *e)
633 struct transient_c *tc = (struct transient_c *) store->context;
634 sector_t size = get_dev_size(store->snap->cow->bdev);
636 if (size < (tc->next_free + store->snap->chunk_size))
639 e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
640 tc->next_free += store->snap->chunk_size;
645 static void transient_commit(struct exception_store *store,
647 void (*callback) (void *, int success),
648 void *callback_context)
651 callback(callback_context, 1);
654 static void transient_fraction_full(struct exception_store *store,
655 sector_t *numerator, sector_t *denominator)
657 *numerator = ((struct transient_c *) store->context)->next_free;
658 *denominator = get_dev_size(store->snap->cow->bdev);
661 int dm_create_transient(struct exception_store *store)
663 struct transient_c *tc;
665 store->destroy = transient_destroy;
666 store->read_metadata = transient_read_metadata;
667 store->prepare_exception = transient_prepare;
668 store->commit_exception = transient_commit;
669 store->drop_snapshot = NULL;
670 store->fraction_full = transient_fraction_full;
672 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);