blob: a0898a66a2f848f581ae770a93de74011a9249c8 [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,
Jon Brassow1d4989c2009-12-10 23:52:10 +000039 struct dm_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;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000042 sector_t size = get_dev_size(dm_snap_cow(store->snap)->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,
Jon Brassow1d4989c2009-12-10 23:52:10 +000054 struct dm_exception *e,
Jonathan Brassowa159c1a2009-01-06 03:05:19 +000055 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
Mike Snitzer985903b2009-12-10 23:52:11 +000062static void transient_usage(struct dm_exception_store *store,
63 sector_t *total_sectors,
64 sector_t *sectors_allocated,
65 sector_t *metadata_sectors)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000066{
Mike Snitzer985903b2009-12-10 23:52:11 +000067 *sectors_allocated = ((struct transient_c *) store->context)->next_free;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000068 *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
Mike Snitzer985903b2009-12-10 23:52:11 +000069 *metadata_sectors = 0;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000070}
71
Jonathan Brassow493df712009-04-02 19:55:31 +010072static int transient_ctr(struct dm_exception_store *store,
73 unsigned argc, char **argv)
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);
Jonathan Brassow1e302a92009-04-02 19:55:35 +010098 }
Jonathan Brassow493df712009-04-02 19:55:31 +010099
100 return sz;
101}
102
103static struct dm_exception_store_type _transient_type = {
104 .name = "transient",
105 .module = THIS_MODULE,
106 .ctr = transient_ctr,
107 .dtr = transient_dtr,
108 .read_metadata = transient_read_metadata,
109 .prepare_exception = transient_prepare_exception,
110 .commit_exception = transient_commit_exception,
Mike Snitzer985903b2009-12-10 23:52:11 +0000111 .usage = transient_usage,
Jonathan Brassow493df712009-04-02 19:55:31 +0100112 .status = transient_status,
113};
114
115static struct dm_exception_store_type _transient_compat_type = {
116 .name = "N",
117 .module = THIS_MODULE,
118 .ctr = transient_ctr,
119 .dtr = transient_dtr,
120 .read_metadata = transient_read_metadata,
121 .prepare_exception = transient_prepare_exception,
122 .commit_exception = transient_commit_exception,
Mike Snitzer985903b2009-12-10 23:52:11 +0000123 .usage = transient_usage,
Jonathan Brassow493df712009-04-02 19:55:31 +0100124 .status = transient_status,
125};
126
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000127int dm_transient_snapshot_init(void)
128{
Jonathan Brassow493df712009-04-02 19:55:31 +0100129 int r;
130
131 r = dm_exception_store_type_register(&_transient_type);
132 if (r) {
133 DMWARN("Unable to register transient exception store type");
134 return r;
135 }
136
137 r = dm_exception_store_type_register(&_transient_compat_type);
138 if (r) {
139 DMWARN("Unable to register old-style transient "
140 "exception store type");
141 dm_exception_store_type_unregister(&_transient_type);
142 return r;
143 }
144
145 return r;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000146}
147
148void dm_transient_snapshot_exit(void)
149{
Jonathan Brassow493df712009-04-02 19:55:31 +0100150 dm_exception_store_type_unregister(&_transient_type);
151 dm_exception_store_type_unregister(&_transient_compat_type);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000152}