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"
11 #include <linux/pagemap.h>
12 #include <linux/vmalloc.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/dm-io.h>
17 #define DM_MSG_PREFIX "transient snapshot"
19 /*-----------------------------------------------------------------
20 * Implementation of the store for non-persistent snapshots.
21 *---------------------------------------------------------------*/
26 static void transient_dtr(struct dm_exception_store *store)
28 kfree(store->context);
31 static int transient_read_metadata(struct dm_exception_store *store,
32 int (*callback)(void *callback_context,
33 chunk_t old, chunk_t new),
34 void *callback_context)
39 static int transient_prepare_exception(struct dm_exception_store *store,
40 struct dm_exception *e)
42 struct transient_c *tc = store->context;
43 sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
45 if (size < (tc->next_free + store->chunk_size))
48 e->new_chunk = sector_to_chunk(store, tc->next_free);
49 tc->next_free += store->chunk_size;
54 static void transient_commit_exception(struct dm_exception_store *store,
55 struct dm_exception *e, int valid,
56 void (*callback) (void *, int success),
57 void *callback_context)
60 callback(callback_context, valid);
63 static void transient_usage(struct dm_exception_store *store,
64 sector_t *total_sectors,
65 sector_t *sectors_allocated,
66 sector_t *metadata_sectors)
68 *sectors_allocated = ((struct transient_c *) store->context)->next_free;
69 *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
70 *metadata_sectors = 0;
73 static int transient_ctr(struct dm_exception_store *store, char *options)
75 struct transient_c *tc;
77 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
87 static unsigned transient_status(struct dm_exception_store *store,
88 status_type_t status, char *result,
96 case STATUSTYPE_TABLE:
97 DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
107 static struct dm_exception_store_type _transient_type = {
109 .module = THIS_MODULE,
110 .ctr = transient_ctr,
111 .dtr = transient_dtr,
112 .read_metadata = transient_read_metadata,
113 .prepare_exception = transient_prepare_exception,
114 .commit_exception = transient_commit_exception,
115 .usage = transient_usage,
116 .status = transient_status,
119 static struct dm_exception_store_type _transient_compat_type = {
121 .module = THIS_MODULE,
122 .ctr = transient_ctr,
123 .dtr = transient_dtr,
124 .read_metadata = transient_read_metadata,
125 .prepare_exception = transient_prepare_exception,
126 .commit_exception = transient_commit_exception,
127 .usage = transient_usage,
128 .status = transient_status,
131 int dm_transient_snapshot_init(void)
135 r = dm_exception_store_type_register(&_transient_type);
137 DMWARN("Unable to register transient exception store type");
141 r = dm_exception_store_type_register(&_transient_compat_type);
143 DMWARN("Unable to register old-style transient "
144 "exception store type");
145 dm_exception_store_type_unregister(&_transient_type);
152 void dm_transient_snapshot_exit(void)
154 dm_exception_store_type_unregister(&_transient_type);
155 dm_exception_store_type_unregister(&_transient_compat_type);