Merge tag 'platform-drivers-x86-v6.8-4' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / md / dm-snap-transient.c
CommitLineData
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
a4a82ce3
HM
20/*
21 *---------------------------------------------------------------
4db6bfe0 22 * Implementation of the store for non-persistent snapshots.
a4a82ce3
HM
23 *---------------------------------------------------------------
24 */
4db6bfe0
AK
25struct transient_c {
26 sector_t next_free;
27};
28
493df71c 29static void transient_dtr(struct dm_exception_store *store)
4db6bfe0
AK
30{
31 kfree(store->context);
32}
33
a159c1ac
JB
34static int transient_read_metadata(struct dm_exception_store *store,
35 int (*callback)(void *callback_context,
36 chunk_t old, chunk_t new),
37 void *callback_context)
4db6bfe0
AK
38{
39 return 0;
40}
41
a159c1ac 42static int transient_prepare_exception(struct dm_exception_store *store,
1d4989c8 43 struct dm_exception *e)
4db6bfe0 44{
b2a11465 45 struct transient_c *tc = store->context;
fc56f6fb 46 sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
4db6bfe0 47
d0216849 48 if (size < (tc->next_free + store->chunk_size))
4db6bfe0
AK
49 return -1;
50
71fab00a 51 e->new_chunk = sector_to_chunk(store, tc->next_free);
d0216849 52 tc->next_free += store->chunk_size;
4db6bfe0
AK
53
54 return 0;
55}
56
a159c1ac 57static void transient_commit_exception(struct dm_exception_store *store,
385277bf 58 struct dm_exception *e, int valid,
8ca817c4 59 void (*callback)(void *, int success),
a159c1ac 60 void *callback_context)
4db6bfe0
AK
61{
62 /* Just succeed */
385277bf 63 callback(callback_context, valid);
4db6bfe0
AK
64}
65
985903bb
MS
66static void transient_usage(struct dm_exception_store *store,
67 sector_t *total_sectors,
68 sector_t *sectors_allocated,
69 sector_t *metadata_sectors)
4db6bfe0 70{
985903bb 71 *sectors_allocated = ((struct transient_c *) store->context)->next_free;
fc56f6fb 72 *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
985903bb 73 *metadata_sectors = 0;
4db6bfe0
AK
74}
75
b0d3cc01 76static int transient_ctr(struct dm_exception_store *store, char *options)
4db6bfe0
AK
77{
78 struct transient_c *tc;
79
4db6bfe0
AK
80 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
81 if (!tc)
82 return -ENOMEM;
83
84 tc->next_free = 0;
85 store->context = tc;
86
87 return 0;
88}
89
86a3238c 90static unsigned int transient_status(struct dm_exception_store *store,
1e302a92 91 status_type_t status, char *result,
86a3238c 92 unsigned int maxlen)
493df71c 93{
86a3238c 94 unsigned int sz = 0;
1e302a92
JB
95
96 switch (status) {
97 case STATUSTYPE_INFO:
98 break;
99 case STATUSTYPE_TABLE:
fc56f6fb 100 DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
8ec45662
TS
101 break;
102 case STATUSTYPE_IMA:
103 *result = '\0';
104 break;
1e302a92 105 }
493df71c
JB
106
107 return sz;
108}
109
110static struct dm_exception_store_type _transient_type = {
111 .name = "transient",
112 .module = THIS_MODULE,
113 .ctr = transient_ctr,
114 .dtr = transient_dtr,
115 .read_metadata = transient_read_metadata,
116 .prepare_exception = transient_prepare_exception,
117 .commit_exception = transient_commit_exception,
985903bb 118 .usage = transient_usage,
493df71c
JB
119 .status = transient_status,
120};
121
122static struct dm_exception_store_type _transient_compat_type = {
123 .name = "N",
124 .module = THIS_MODULE,
125 .ctr = transient_ctr,
126 .dtr = transient_dtr,
127 .read_metadata = transient_read_metadata,
128 .prepare_exception = transient_prepare_exception,
129 .commit_exception = transient_commit_exception,
985903bb 130 .usage = transient_usage,
493df71c
JB
131 .status = transient_status,
132};
133
4db6bfe0
AK
134int dm_transient_snapshot_init(void)
135{
493df71c
JB
136 int r;
137
138 r = dm_exception_store_type_register(&_transient_type);
139 if (r) {
140 DMWARN("Unable to register transient exception store type");
141 return r;
142 }
143
144 r = dm_exception_store_type_register(&_transient_compat_type);
145 if (r) {
2e84fecf 146 DMWARN("Unable to register old-style transient exception store type");
493df71c
JB
147 dm_exception_store_type_unregister(&_transient_type);
148 return r;
149 }
150
151 return r;
4db6bfe0
AK
152}
153
154void dm_transient_snapshot_exit(void)
155{
493df71c
JB
156 dm_exception_store_type_unregister(&_transient_type);
157 dm_exception_store_type_unregister(&_transient_compat_type);
4db6bfe0 158}