blob: 0e0ae4c36b37485ddf8a706914026281703aa6c2 [file] [log] [blame]
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001/*
2 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2006-2008 Red Hat GmbH
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-exception-store.h"
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00009
10#include <linux/mm.h>
11#include <linux/pagemap.h>
12#include <linux/vmalloc.h>
Paul Gortmakerdaaa5f72011-05-27 15:50:58 -040013#include <linux/export.h>
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000014#include <linux/slab.h>
15#include <linux/dm-io.h>
16
17#define DM_MSG_PREFIX "transient snapshot"
18
19/*-----------------------------------------------------------------
20 * Implementation of the store for non-persistent snapshots.
21 *---------------------------------------------------------------*/
22struct transient_c {
23 sector_t next_free;
24};
25
Jonathan Brassow493df712009-04-02 19:55:31 +010026static void transient_dtr(struct dm_exception_store *store)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000027{
28 kfree(store->context);
29}
30
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000031static 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)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000035{
36 return 0;
37}
38
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000039static int transient_prepare_exception(struct dm_exception_store *store,
Jon Brassow1d4989c2009-12-10 23:52:10 +000040 struct dm_exception *e)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000041{
Jonathan Brassowb2a11462009-04-02 19:55:30 +010042 struct transient_c *tc = store->context;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000043 sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000044
Jonathan Brassowd0216842009-04-02 19:55:32 +010045 if (size < (tc->next_free + store->chunk_size))
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000046 return -1;
47
Jonathan Brassow71fab002009-04-02 19:55:33 +010048 e->new_chunk = sector_to_chunk(store, tc->next_free);
Jonathan Brassowd0216842009-04-02 19:55:32 +010049 tc->next_free += store->chunk_size;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000050
51 return 0;
52}
53
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000054static void transient_commit_exception(struct dm_exception_store *store,
Mikulas Patocka385277b2016-01-08 19:07:55 -050055 struct dm_exception *e, int valid,
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000056 void (*callback) (void *, int success),
57 void *callback_context)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000058{
59 /* Just succeed */
Mikulas Patocka385277b2016-01-08 19:07:55 -050060 callback(callback_context, valid);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000061}
62
Mike Snitzer985903b2009-12-10 23:52:11 +000063static void transient_usage(struct dm_exception_store *store,
64 sector_t *total_sectors,
65 sector_t *sectors_allocated,
66 sector_t *metadata_sectors)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000067{
Mike Snitzer985903b2009-12-10 23:52:11 +000068 *sectors_allocated = ((struct transient_c *) store->context)->next_free;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000069 *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
Mike Snitzer985903b2009-12-10 23:52:11 +000070 *metadata_sectors = 0;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000071}
72
Mike Snitzerb0d3cc02015-10-08 18:05:41 -040073static int transient_ctr(struct dm_exception_store *store, char *options)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000074{
75 struct transient_c *tc;
76
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000077 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
78 if (!tc)
79 return -ENOMEM;
80
81 tc->next_free = 0;
82 store->context = tc;
83
84 return 0;
85}
86
Jonathan Brassow1e302a92009-04-02 19:55:35 +010087static unsigned transient_status(struct dm_exception_store *store,
88 status_type_t status, char *result,
89 unsigned maxlen)
Jonathan Brassow493df712009-04-02 19:55:31 +010090{
Jonathan Brassow1e302a92009-04-02 19:55:35 +010091 unsigned sz = 0;
92
93 switch (status) {
94 case STATUSTYPE_INFO:
95 break;
96 case STATUSTYPE_TABLE:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000097 DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
Tushar Sugandhi8ec45662021-07-12 17:49:03 -070098 break;
99 case STATUSTYPE_IMA:
100 *result = '\0';
101 break;
Jonathan Brassow1e302a92009-04-02 19:55:35 +0100102 }
Jonathan Brassow493df712009-04-02 19:55:31 +0100103
104 return sz;
105}
106
107static struct dm_exception_store_type _transient_type = {
108 .name = "transient",
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,
Mike Snitzer985903b2009-12-10 23:52:11 +0000115 .usage = transient_usage,
Jonathan Brassow493df712009-04-02 19:55:31 +0100116 .status = transient_status,
117};
118
119static struct dm_exception_store_type _transient_compat_type = {
120 .name = "N",
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,
Mike Snitzer985903b2009-12-10 23:52:11 +0000127 .usage = transient_usage,
Jonathan Brassow493df712009-04-02 19:55:31 +0100128 .status = transient_status,
129};
130
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000131int dm_transient_snapshot_init(void)
132{
Jonathan Brassow493df712009-04-02 19:55:31 +0100133 int r;
134
135 r = dm_exception_store_type_register(&_transient_type);
136 if (r) {
137 DMWARN("Unable to register transient exception store type");
138 return r;
139 }
140
141 r = dm_exception_store_type_register(&_transient_compat_type);
142 if (r) {
143 DMWARN("Unable to register old-style transient "
144 "exception store type");
145 dm_exception_store_type_unregister(&_transient_type);
146 return r;
147 }
148
149 return r;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000150}
151
152void dm_transient_snapshot_exit(void)
153{
Jonathan Brassow493df712009-04-02 19:55:31 +0100154 dm_exception_store_type_unregister(&_transient_type);
155 dm_exception_store_type_unregister(&_transient_compat_type);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000156}