Commit | Line | Data |
---|---|---|
3bd94003 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
4db6bfe0 AK |
2 | /* |
3 | * Copyright (C) 2001-2002 Sistina Software (UK) Limited. | |
4 | * Copyright (C) 2006-2008 Red Hat GmbH | |
5 | * | |
6 | * This file is released under the GPL. | |
7 | */ | |
8 | ||
9 | #include "dm-exception-store.h" | |
4db6bfe0 AK |
10 | |
11 | #include <linux/mm.h> | |
12 | #include <linux/pagemap.h> | |
13 | #include <linux/vmalloc.h> | |
daaa5f7c | 14 | #include <linux/export.h> |
4db6bfe0 AK |
15 | #include <linux/slab.h> |
16 | #include <linux/dm-io.h> | |
17 | ||
18 | #define DM_MSG_PREFIX "transient snapshot" | |
19 | ||
20 | /*----------------------------------------------------------------- | |
21 | * Implementation of the store for non-persistent snapshots. | |
22 | *---------------------------------------------------------------*/ | |
23 | struct transient_c { | |
24 | sector_t next_free; | |
25 | }; | |
26 | ||
493df71c | 27 | static void transient_dtr(struct dm_exception_store *store) |
4db6bfe0 AK |
28 | { |
29 | kfree(store->context); | |
30 | } | |
31 | ||
a159c1ac JB |
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) | |
4db6bfe0 AK |
36 | { |
37 | return 0; | |
38 | } | |
39 | ||
a159c1ac | 40 | static int transient_prepare_exception(struct dm_exception_store *store, |
1d4989c8 | 41 | struct dm_exception *e) |
4db6bfe0 | 42 | { |
b2a11465 | 43 | struct transient_c *tc = store->context; |
fc56f6fb | 44 | sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev); |
4db6bfe0 | 45 | |
d0216849 | 46 | if (size < (tc->next_free + store->chunk_size)) |
4db6bfe0 AK |
47 | return -1; |
48 | ||
71fab00a | 49 | e->new_chunk = sector_to_chunk(store, tc->next_free); |
d0216849 | 50 | tc->next_free += store->chunk_size; |
4db6bfe0 AK |
51 | |
52 | return 0; | |
53 | } | |
54 | ||
a159c1ac | 55 | static void transient_commit_exception(struct dm_exception_store *store, |
385277bf | 56 | struct dm_exception *e, int valid, |
a159c1ac JB |
57 | void (*callback) (void *, int success), |
58 | void *callback_context) | |
4db6bfe0 AK |
59 | { |
60 | /* Just succeed */ | |
385277bf | 61 | callback(callback_context, valid); |
4db6bfe0 AK |
62 | } |
63 | ||
985903bb MS |
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) | |
4db6bfe0 | 68 | { |
985903bb | 69 | *sectors_allocated = ((struct transient_c *) store->context)->next_free; |
fc56f6fb | 70 | *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev); |
985903bb | 71 | *metadata_sectors = 0; |
4db6bfe0 AK |
72 | } |
73 | ||
b0d3cc01 | 74 | static int transient_ctr(struct dm_exception_store *store, char *options) |
4db6bfe0 AK |
75 | { |
76 | struct transient_c *tc; | |
77 | ||
4db6bfe0 AK |
78 | tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); |
79 | if (!tc) | |
80 | return -ENOMEM; | |
81 | ||
82 | tc->next_free = 0; | |
83 | store->context = tc; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
86a3238c | 88 | static unsigned int transient_status(struct dm_exception_store *store, |
1e302a92 | 89 | status_type_t status, char *result, |
86a3238c | 90 | unsigned int maxlen) |
493df71c | 91 | { |
86a3238c | 92 | unsigned int sz = 0; |
1e302a92 JB |
93 | |
94 | switch (status) { | |
95 | case STATUSTYPE_INFO: | |
96 | break; | |
97 | case STATUSTYPE_TABLE: | |
fc56f6fb | 98 | DMEMIT(" N %llu", (unsigned long long)store->chunk_size); |
8ec45662 TS |
99 | break; |
100 | case STATUSTYPE_IMA: | |
101 | *result = '\0'; | |
102 | break; | |
1e302a92 | 103 | } |
493df71c JB |
104 | |
105 | return sz; | |
106 | } | |
107 | ||
108 | static struct dm_exception_store_type _transient_type = { | |
109 | .name = "transient", | |
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, | |
985903bb | 116 | .usage = transient_usage, |
493df71c JB |
117 | .status = transient_status, |
118 | }; | |
119 | ||
120 | static struct dm_exception_store_type _transient_compat_type = { | |
121 | .name = "N", | |
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, | |
985903bb | 128 | .usage = transient_usage, |
493df71c JB |
129 | .status = transient_status, |
130 | }; | |
131 | ||
4db6bfe0 AK |
132 | int dm_transient_snapshot_init(void) |
133 | { | |
493df71c JB |
134 | int r; |
135 | ||
136 | r = dm_exception_store_type_register(&_transient_type); | |
137 | if (r) { | |
138 | DMWARN("Unable to register transient exception store type"); | |
139 | return r; | |
140 | } | |
141 | ||
142 | r = dm_exception_store_type_register(&_transient_compat_type); | |
143 | if (r) { | |
144 | DMWARN("Unable to register old-style transient " | |
145 | "exception store type"); | |
146 | dm_exception_store_type_unregister(&_transient_type); | |
147 | return r; | |
148 | } | |
149 | ||
150 | return r; | |
4db6bfe0 AK |
151 | } |
152 | ||
153 | void dm_transient_snapshot_exit(void) | |
154 | { | |
493df71c JB |
155 | dm_exception_store_type_unregister(&_transient_type); |
156 | dm_exception_store_type_unregister(&_transient_compat_type); | |
4db6bfe0 | 157 | } |