1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
4 * Copyright (C) 2006-2008 Red Hat GmbH
6 * This file is released under the GPL.
9 #include "dm-exception-store.h"
12 #include <linux/pagemap.h>
13 #include <linux/vmalloc.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/dm-io.h>
18 #define DM_MSG_PREFIX "transient snapshot"
20 /*-----------------------------------------------------------------
21 * Implementation of the store for non-persistent snapshots.
22 *---------------------------------------------------------------*/
27 static void transient_dtr(struct dm_exception_store *store)
29 kfree(store->context);
32 static int transient_read_metadata(struct dm_exception_store *store,
33 int (*callback)(void *callback_context,
34 chunk_t old, chunk_t new),
35 void *callback_context)
40 static int transient_prepare_exception(struct dm_exception_store *store,
41 struct dm_exception *e)
43 struct transient_c *tc = store->context;
44 sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
46 if (size < (tc->next_free + store->chunk_size))
49 e->new_chunk = sector_to_chunk(store, tc->next_free);
50 tc->next_free += store->chunk_size;
55 static void transient_commit_exception(struct dm_exception_store *store,
56 struct dm_exception *e, int valid,
57 void (*callback) (void *, int success),
58 void *callback_context)
61 callback(callback_context, valid);
64 static void transient_usage(struct dm_exception_store *store,
65 sector_t *total_sectors,
66 sector_t *sectors_allocated,
67 sector_t *metadata_sectors)
69 *sectors_allocated = ((struct transient_c *) store->context)->next_free;
70 *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
71 *metadata_sectors = 0;
74 static int transient_ctr(struct dm_exception_store *store, char *options)
76 struct transient_c *tc;
78 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
88 static unsigned int transient_status(struct dm_exception_store *store,
89 status_type_t status, char *result,
97 case STATUSTYPE_TABLE:
98 DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
108 static struct dm_exception_store_type _transient_type = {
110 .module = THIS_MODULE,
111 .ctr = transient_ctr,
112 .dtr = transient_dtr,
113 .read_metadata = transient_read_metadata,
114 .prepare_exception = transient_prepare_exception,
115 .commit_exception = transient_commit_exception,
116 .usage = transient_usage,
117 .status = transient_status,
120 static struct dm_exception_store_type _transient_compat_type = {
122 .module = THIS_MODULE,
123 .ctr = transient_ctr,
124 .dtr = transient_dtr,
125 .read_metadata = transient_read_metadata,
126 .prepare_exception = transient_prepare_exception,
127 .commit_exception = transient_commit_exception,
128 .usage = transient_usage,
129 .status = transient_status,
132 int dm_transient_snapshot_init(void)
136 r = dm_exception_store_type_register(&_transient_type);
138 DMWARN("Unable to register transient exception store type");
142 r = dm_exception_store_type_register(&_transient_compat_type);
144 DMWARN("Unable to register old-style transient "
145 "exception store type");
146 dm_exception_store_type_unregister(&_transient_type);
153 void dm_transient_snapshot_exit(void)
155 dm_exception_store_type_unregister(&_transient_type);
156 dm_exception_store_type_unregister(&_transient_compat_type);