2 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2006-2008 Red Hat GmbH
5 * This file is released under the GPL.
8 #include "dm-exception-store.h"
12 #include <linux/pagemap.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
15 #include <linux/dm-io.h>
17 #define DM_MSG_PREFIX "persistent snapshot"
18 #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
20 /*-----------------------------------------------------------------
21 * Persistent snapshots, by persistent we mean that the snapshot
22 * will survive a reboot.
23 *---------------------------------------------------------------*/
26 * We need to store a record of which parts of the origin have
27 * been copied to the snapshot device. The snapshot code
28 * requires that we copy exception chunks to chunk aligned areas
29 * of the COW store. It makes sense therefore, to store the
30 * metadata in chunk size blocks.
32 * There is no backward or forward compatibility implemented,
33 * snapshots with different disk versions than the kernel will
34 * not be usable. It is expected that "lvcreate" will blank out
35 * the start of a fresh COW device before calling the snapshot
38 * The first chunk of the COW device just contains the header.
39 * After this there is a chunk filled with exception metadata,
40 * followed by as many exception chunks as can fit in the
43 * All on disk structures are in little-endian format. The end
44 * of the exceptions info is indicated by an exception with a
45 * new_chunk of 0, which is invalid since it would point to the
50 * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
52 #define SNAP_MAGIC 0x70416e53
55 * The on-disk version of the metadata.
57 #define SNAPSHOT_DISK_VERSION 1
63 * Is this snapshot valid. There is no way of recovering
64 * an invalid snapshot.
69 * Simple, incrementing version. no backward
78 struct disk_exception {
83 struct commit_callback {
84 void (*callback)(void *, int success);
89 * The top level structure for a persistent exception store.
92 struct dm_snapshot *snap; /* up pointer to my snapshot */
95 uint32_t exceptions_per_area;
98 * Now that we have an asynchronous kcopyd there is no
99 * need for large chunk sizes, so it wont hurt to have a
100 * whole chunks worth of metadata in memory at once.
105 * An area of zeros used to clear the next area.
110 * Used to keep track of which metadata area the data in
113 chunk_t current_area;
116 * The next free chunk for an exception.
121 * The index of next free exception in the current
124 uint32_t current_committed;
126 atomic_t pending_count;
127 uint32_t callback_count;
128 struct commit_callback *callbacks;
129 struct dm_io_client *io_client;
131 struct workqueue_struct *metadata_wq;
134 static unsigned sectors_to_pages(unsigned sectors)
136 return DIV_ROUND_UP(sectors, PAGE_SIZE >> 9);
139 static int alloc_area(struct pstore *ps)
144 len = ps->snap->store->chunk_size << SECTOR_SHIFT;
147 * Allocate the chunk_size block of memory that will hold
148 * a single metadata area.
150 ps->area = vmalloc(len);
154 ps->zero_area = vmalloc(len);
155 if (!ps->zero_area) {
159 memset(ps->zero_area, 0, len);
164 static void free_area(struct pstore *ps)
168 vfree(ps->zero_area);
169 ps->zero_area = NULL;
173 struct dm_io_region *where;
174 struct dm_io_request *io_req;
175 struct work_struct work;
179 static void do_metadata(struct work_struct *work)
181 struct mdata_req *req = container_of(work, struct mdata_req, work);
183 req->result = dm_io(req->io_req, 1, req->where, NULL);
187 * Read or write a chunk aligned and sized block of data from a device.
189 static int chunk_io(struct pstore *ps, chunk_t chunk, int rw, int metadata)
191 struct dm_io_region where = {
192 .bdev = ps->snap->store->cow->bdev,
193 .sector = ps->snap->store->chunk_size * chunk,
194 .count = ps->snap->store->chunk_size,
196 struct dm_io_request io_req = {
198 .mem.type = DM_IO_VMA,
199 .mem.ptr.vma = ps->area,
200 .client = ps->io_client,
203 struct mdata_req req;
206 return dm_io(&io_req, 1, &where, NULL);
209 req.io_req = &io_req;
212 * Issue the synchronous I/O from a different thread
213 * to avoid generic_make_request recursion.
215 INIT_WORK(&req.work, do_metadata);
216 queue_work(ps->metadata_wq, &req.work);
217 flush_workqueue(ps->metadata_wq);
223 * Convert a metadata area index to a chunk index.
225 static chunk_t area_location(struct pstore *ps, chunk_t area)
227 return 1 + ((ps->exceptions_per_area + 1) * area);
231 * Read or write a metadata area. Remembering to skip the first
232 * chunk which holds the header.
234 static int area_io(struct pstore *ps, int rw)
239 chunk = area_location(ps, ps->current_area);
241 r = chunk_io(ps, chunk, rw, 0);
248 static void zero_memory_area(struct pstore *ps)
250 memset(ps->area, 0, ps->snap->store->chunk_size << SECTOR_SHIFT);
253 static int zero_disk_area(struct pstore *ps, chunk_t area)
255 struct dm_io_region where = {
256 .bdev = ps->snap->store->cow->bdev,
257 .sector = ps->snap->store->chunk_size * area_location(ps, area),
258 .count = ps->snap->store->chunk_size,
260 struct dm_io_request io_req = {
262 .mem.type = DM_IO_VMA,
263 .mem.ptr.vma = ps->zero_area,
264 .client = ps->io_client,
268 return dm_io(&io_req, 1, &where, NULL);
271 static int read_header(struct pstore *ps, int *new_snapshot)
274 struct disk_header *dh;
276 int chunk_size_supplied = 1;
279 * Use default chunk size (or hardsect_size, if larger) if none supplied
281 if (!ps->snap->store->chunk_size) {
282 ps->snap->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
283 bdev_hardsect_size(ps->snap->store->cow->bdev) >> 9);
284 ps->snap->store->chunk_mask = ps->snap->store->chunk_size - 1;
285 ps->snap->store->chunk_shift = ffs(ps->snap->store->chunk_size)
287 chunk_size_supplied = 0;
290 ps->io_client = dm_io_client_create(sectors_to_pages(ps->snap->
292 if (IS_ERR(ps->io_client))
293 return PTR_ERR(ps->io_client);
299 r = chunk_io(ps, 0, READ, 1);
303 dh = (struct disk_header *) ps->area;
305 if (le32_to_cpu(dh->magic) == 0) {
310 if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
311 DMWARN("Invalid or corrupt snapshot");
317 ps->valid = le32_to_cpu(dh->valid);
318 ps->version = le32_to_cpu(dh->version);
319 chunk_size = le32_to_cpu(dh->chunk_size);
321 if (!chunk_size_supplied || ps->snap->store->chunk_size == chunk_size)
324 DMWARN("chunk size %llu in device metadata overrides "
325 "table chunk size of %llu.",
326 (unsigned long long)chunk_size,
327 (unsigned long long)ps->snap->store->chunk_size);
329 /* We had a bogus chunk_size. Fix stuff up. */
332 ps->snap->store->chunk_size = chunk_size;
333 ps->snap->store->chunk_mask = chunk_size - 1;
334 ps->snap->store->chunk_shift = ffs(chunk_size) - 1;
336 r = dm_io_client_resize(sectors_to_pages(ps->snap->store->chunk_size),
349 static int write_header(struct pstore *ps)
351 struct disk_header *dh;
353 memset(ps->area, 0, ps->snap->store->chunk_size << SECTOR_SHIFT);
355 dh = (struct disk_header *) ps->area;
356 dh->magic = cpu_to_le32(SNAP_MAGIC);
357 dh->valid = cpu_to_le32(ps->valid);
358 dh->version = cpu_to_le32(ps->version);
359 dh->chunk_size = cpu_to_le32(ps->snap->store->chunk_size);
361 return chunk_io(ps, 0, WRITE, 1);
365 * Access functions for the disk exceptions, these do the endian conversions.
367 static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
369 BUG_ON(index >= ps->exceptions_per_area);
371 return ((struct disk_exception *) ps->area) + index;
374 static void read_exception(struct pstore *ps,
375 uint32_t index, struct disk_exception *result)
377 struct disk_exception *e = get_exception(ps, index);
380 result->old_chunk = le64_to_cpu(e->old_chunk);
381 result->new_chunk = le64_to_cpu(e->new_chunk);
384 static void write_exception(struct pstore *ps,
385 uint32_t index, struct disk_exception *de)
387 struct disk_exception *e = get_exception(ps, index);
390 e->old_chunk = cpu_to_le64(de->old_chunk);
391 e->new_chunk = cpu_to_le64(de->new_chunk);
395 * Registers the exceptions that are present in the current area.
396 * 'full' is filled in to indicate if the area has been
399 static int insert_exceptions(struct pstore *ps,
400 int (*callback)(void *callback_context,
401 chunk_t old, chunk_t new),
402 void *callback_context,
407 struct disk_exception de;
409 /* presume the area is full */
412 for (i = 0; i < ps->exceptions_per_area; i++) {
413 read_exception(ps, i, &de);
416 * If the new_chunk is pointing at the start of
417 * the COW device, where the first metadata area
418 * is we know that we've hit the end of the
419 * exceptions. Therefore the area is not full.
421 if (de.new_chunk == 0LL) {
422 ps->current_committed = i;
428 * Keep track of the start of the free chunks.
430 if (ps->next_free <= de.new_chunk)
431 ps->next_free = de.new_chunk + 1;
434 * Otherwise we add the exception to the snapshot.
436 r = callback(callback_context, de.old_chunk, de.new_chunk);
444 static int read_exceptions(struct pstore *ps,
445 int (*callback)(void *callback_context, chunk_t old,
447 void *callback_context)
452 * Keeping reading chunks and inserting exceptions until
453 * we find a partially full area.
455 for (ps->current_area = 0; full; ps->current_area++) {
456 r = area_io(ps, READ);
460 r = insert_exceptions(ps, callback, callback_context, &full);
470 static struct pstore *get_info(struct dm_exception_store *store)
472 return (struct pstore *) store->context;
475 static void persistent_fraction_full(struct dm_exception_store *store,
476 sector_t *numerator, sector_t *denominator)
478 *numerator = get_info(store)->next_free * store->chunk_size;
479 *denominator = get_dev_size(store->cow->bdev);
482 static void persistent_dtr(struct dm_exception_store *store)
484 struct pstore *ps = get_info(store);
486 destroy_workqueue(ps->metadata_wq);
487 dm_io_client_destroy(ps->io_client);
488 vfree(ps->callbacks);
493 static int persistent_read_metadata(struct dm_exception_store *store,
494 int (*callback)(void *callback_context,
495 chunk_t old, chunk_t new),
496 void *callback_context)
498 int r, uninitialized_var(new_snapshot);
499 struct pstore *ps = get_info(store);
502 * Read the snapshot header.
504 r = read_header(ps, &new_snapshot);
509 * Now we know correct chunk_size, complete the initialisation.
511 ps->exceptions_per_area = (ps->snap->store->chunk_size << SECTOR_SHIFT)
512 / sizeof(struct disk_exception);
513 ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
514 sizeof(*ps->callbacks));
519 * Do we need to setup a new snapshot ?
522 r = write_header(ps);
524 DMWARN("write_header failed");
528 ps->current_area = 0;
529 zero_memory_area(ps);
530 r = zero_disk_area(ps, 0);
532 DMWARN("zero_disk_area(0) failed");
539 if (ps->version != SNAPSHOT_DISK_VERSION) {
540 DMWARN("unable to handle snapshot disk version %d",
546 * Metadata are valid, but snapshot is invalidated
554 r = read_exceptions(ps, callback, callback_context);
562 static int persistent_prepare_exception(struct dm_exception_store *store,
563 struct dm_snap_exception *e)
565 struct pstore *ps = get_info(store);
568 sector_t size = get_dev_size(store->cow->bdev);
570 /* Is there enough room ? */
571 if (size < ((ps->next_free + 1) * store->chunk_size))
574 e->new_chunk = ps->next_free;
577 * Move onto the next free pending, making sure to take
578 * into account the location of the metadata chunks.
580 stride = (ps->exceptions_per_area + 1);
581 next_free = ++ps->next_free;
582 if (sector_div(next_free, stride) == 1)
585 atomic_inc(&ps->pending_count);
589 static void persistent_commit_exception(struct dm_exception_store *store,
590 struct dm_snap_exception *e,
591 void (*callback) (void *, int success),
592 void *callback_context)
595 struct pstore *ps = get_info(store);
596 struct disk_exception de;
597 struct commit_callback *cb;
599 de.old_chunk = e->old_chunk;
600 de.new_chunk = e->new_chunk;
601 write_exception(ps, ps->current_committed++, &de);
604 * Add the callback to the back of the array. This code
605 * is the only place where the callback array is
606 * manipulated, and we know that it will never be called
607 * multiple times concurrently.
609 cb = ps->callbacks + ps->callback_count++;
610 cb->callback = callback;
611 cb->context = callback_context;
614 * If there are exceptions in flight and we have not yet
615 * filled this metadata area there's nothing more to do.
617 if (!atomic_dec_and_test(&ps->pending_count) &&
618 (ps->current_committed != ps->exceptions_per_area))
622 * If we completely filled the current area, then wipe the next one.
624 if ((ps->current_committed == ps->exceptions_per_area) &&
625 zero_disk_area(ps, ps->current_area + 1))
629 * Commit exceptions to disk.
631 if (ps->valid && area_io(ps, WRITE))
635 * Advance to the next area if this one is full.
637 if (ps->current_committed == ps->exceptions_per_area) {
638 ps->current_committed = 0;
640 zero_memory_area(ps);
643 for (i = 0; i < ps->callback_count; i++) {
644 cb = ps->callbacks + i;
645 cb->callback(cb->context, ps->valid);
648 ps->callback_count = 0;
651 static void persistent_drop_snapshot(struct dm_exception_store *store)
653 struct pstore *ps = get_info(store);
656 if (write_header(ps))
657 DMWARN("write header failed");
660 static int persistent_ctr(struct dm_exception_store *store,
661 unsigned argc, char **argv)
665 /* allocate the pstore */
666 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
670 ps->snap = store->snap;
672 ps->version = SNAPSHOT_DISK_VERSION;
674 ps->next_free = 2; /* skipping the header and first area */
675 ps->current_committed = 0;
677 ps->callback_count = 0;
678 atomic_set(&ps->pending_count, 0);
679 ps->callbacks = NULL;
681 ps->metadata_wq = create_singlethread_workqueue("ksnaphd");
682 if (!ps->metadata_wq) {
684 DMERR("couldn't start header metadata update thread");
693 static int persistent_status(struct dm_exception_store *store,
694 status_type_t status, char *result,
702 static struct dm_exception_store_type _persistent_type = {
703 .name = "persistent",
704 .module = THIS_MODULE,
705 .ctr = persistent_ctr,
706 .dtr = persistent_dtr,
707 .read_metadata = persistent_read_metadata,
708 .prepare_exception = persistent_prepare_exception,
709 .commit_exception = persistent_commit_exception,
710 .drop_snapshot = persistent_drop_snapshot,
711 .fraction_full = persistent_fraction_full,
712 .status = persistent_status,
715 static struct dm_exception_store_type _persistent_compat_type = {
717 .module = THIS_MODULE,
718 .ctr = persistent_ctr,
719 .dtr = persistent_dtr,
720 .read_metadata = persistent_read_metadata,
721 .prepare_exception = persistent_prepare_exception,
722 .commit_exception = persistent_commit_exception,
723 .drop_snapshot = persistent_drop_snapshot,
724 .fraction_full = persistent_fraction_full,
725 .status = persistent_status,
728 int dm_persistent_snapshot_init(void)
732 r = dm_exception_store_type_register(&_persistent_type);
734 DMERR("Unable to register persistent exception store type");
738 r = dm_exception_store_type_register(&_persistent_compat_type);
740 DMERR("Unable to register old-style persistent exception "
742 dm_exception_store_type_unregister(&_persistent_type);
749 void dm_persistent_snapshot_exit(void)
751 dm_exception_store_type_unregister(&_persistent_type);
752 dm_exception_store_type_unregister(&_persistent_compat_type);