blob: a883d6e658beeca358bc65c34a246ce3efb1bf15 [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>
13#include <linux/slab.h>
14#include <linux/dm-io.h>
15
16#define DM_MSG_PREFIX "transient snapshot"
17
18/*-----------------------------------------------------------------
19 * Implementation of the store for non-persistent snapshots.
20 *---------------------------------------------------------------*/
21struct transient_c {
22 sector_t next_free;
23};
24
Jonathan Brassow493df712009-04-02 19:55:31 +010025static void transient_dtr(struct dm_exception_store *store)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000026{
27 kfree(store->context);
28}
29
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000030static int transient_read_metadata(struct dm_exception_store *store,
31 int (*callback)(void *callback_context,
32 chunk_t old, chunk_t new),
33 void *callback_context)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000034{
35 return 0;
36}
37
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000038static int transient_prepare_exception(struct dm_exception_store *store,
39 struct dm_snap_exception *e)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000040{
Jonathan Brassowb2a11462009-04-02 19:55:30 +010041 struct transient_c *tc = store->context;
Jonathan Brassow49beb2b2009-04-02 19:55:33 +010042 sector_t size = get_dev_size(store->cow->bdev);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000043
Jonathan Brassowd0216842009-04-02 19:55:32 +010044 if (size < (tc->next_free + store->chunk_size))
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000045 return -1;
46
Jonathan Brassow71fab002009-04-02 19:55:33 +010047 e->new_chunk = sector_to_chunk(store, tc->next_free);
Jonathan Brassowd0216842009-04-02 19:55:32 +010048 tc->next_free += store->chunk_size;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000049
50 return 0;
51}
52
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000053static void transient_commit_exception(struct dm_exception_store *store,
54 struct dm_snap_exception *e,
55 void (*callback) (void *, int success),
56 void *callback_context)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000057{
58 /* Just succeed */
59 callback(callback_context, 1);
60}
61
62static void transient_fraction_full(struct dm_exception_store *store,
63 sector_t *numerator, sector_t *denominator)
64{
65 *numerator = ((struct transient_c *) store->context)->next_free;
Jonathan Brassow49beb2b2009-04-02 19:55:33 +010066 *denominator = get_dev_size(store->cow->bdev);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000067}
68
Jonathan Brassow493df712009-04-02 19:55:31 +010069static int transient_ctr(struct dm_exception_store *store,
70 unsigned argc, char **argv)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000071{
72 struct transient_c *tc;
73
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000074 tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
75 if (!tc)
76 return -ENOMEM;
77
78 tc->next_free = 0;
79 store->context = tc;
80
81 return 0;
82}
83
Jonathan Brassow493df712009-04-02 19:55:31 +010084static int transient_status(struct dm_exception_store *store,
85 status_type_t status, char *result,
86 unsigned maxlen)
87{
88 int sz = 0;
89
90 return sz;
91}
92
93static struct dm_exception_store_type _transient_type = {
94 .name = "transient",
95 .module = THIS_MODULE,
96 .ctr = transient_ctr,
97 .dtr = transient_dtr,
98 .read_metadata = transient_read_metadata,
99 .prepare_exception = transient_prepare_exception,
100 .commit_exception = transient_commit_exception,
101 .fraction_full = transient_fraction_full,
102 .status = transient_status,
103};
104
105static struct dm_exception_store_type _transient_compat_type = {
106 .name = "N",
107 .module = THIS_MODULE,
108 .ctr = transient_ctr,
109 .dtr = transient_dtr,
110 .read_metadata = transient_read_metadata,
111 .prepare_exception = transient_prepare_exception,
112 .commit_exception = transient_commit_exception,
113 .fraction_full = transient_fraction_full,
114 .status = transient_status,
115};
116
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000117int dm_transient_snapshot_init(void)
118{
Jonathan Brassow493df712009-04-02 19:55:31 +0100119 int r;
120
121 r = dm_exception_store_type_register(&_transient_type);
122 if (r) {
123 DMWARN("Unable to register transient exception store type");
124 return r;
125 }
126
127 r = dm_exception_store_type_register(&_transient_compat_type);
128 if (r) {
129 DMWARN("Unable to register old-style transient "
130 "exception store type");
131 dm_exception_store_type_unregister(&_transient_type);
132 return r;
133 }
134
135 return r;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000136}
137
138void dm_transient_snapshot_exit(void)
139{
Jonathan Brassow493df712009-04-02 19:55:31 +0100140 dm_exception_store_type_unregister(&_transient_type);
141 dm_exception_store_type_unregister(&_transient_compat_type);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000142}