14db6bfe0SAlasdair G Kergon /*
24db6bfe0SAlasdair G Kergon  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
34db6bfe0SAlasdair G Kergon  * Copyright (C) 2006-2008 Red Hat GmbH
44db6bfe0SAlasdair G Kergon  *
54db6bfe0SAlasdair G Kergon  * This file is released under the GPL.
64db6bfe0SAlasdair G Kergon  */
74db6bfe0SAlasdair G Kergon 
84db6bfe0SAlasdair G Kergon #include "dm-exception-store.h"
94db6bfe0SAlasdair G Kergon 
104db6bfe0SAlasdair G Kergon #include <linux/mm.h>
114db6bfe0SAlasdair G Kergon #include <linux/pagemap.h>
124db6bfe0SAlasdair G Kergon #include <linux/vmalloc.h>
13daaa5f7cSPaul Gortmaker #include <linux/export.h>
144db6bfe0SAlasdair G Kergon #include <linux/slab.h>
154db6bfe0SAlasdair G Kergon #include <linux/dm-io.h>
164db6bfe0SAlasdair G Kergon 
174db6bfe0SAlasdair G Kergon #define DM_MSG_PREFIX "transient snapshot"
184db6bfe0SAlasdair G Kergon 
194db6bfe0SAlasdair G Kergon /*-----------------------------------------------------------------
204db6bfe0SAlasdair G Kergon  * Implementation of the store for non-persistent snapshots.
214db6bfe0SAlasdair G Kergon  *---------------------------------------------------------------*/
224db6bfe0SAlasdair G Kergon struct transient_c {
234db6bfe0SAlasdair G Kergon 	sector_t next_free;
244db6bfe0SAlasdair G Kergon };
254db6bfe0SAlasdair G Kergon 
26493df71cSJonathan Brassow static void transient_dtr(struct dm_exception_store *store)
274db6bfe0SAlasdair G Kergon {
284db6bfe0SAlasdair G Kergon 	kfree(store->context);
294db6bfe0SAlasdair G Kergon }
304db6bfe0SAlasdair G Kergon 
31a159c1acSJonathan Brassow static int transient_read_metadata(struct dm_exception_store *store,
32a159c1acSJonathan Brassow 				   int (*callback)(void *callback_context,
33a159c1acSJonathan Brassow 						   chunk_t old, chunk_t new),
34a159c1acSJonathan Brassow 				   void *callback_context)
354db6bfe0SAlasdair G Kergon {
364db6bfe0SAlasdair G Kergon 	return 0;
374db6bfe0SAlasdair G Kergon }
384db6bfe0SAlasdair G Kergon 
39a159c1acSJonathan Brassow static int transient_prepare_exception(struct dm_exception_store *store,
401d4989c8SJon Brassow 				       struct dm_exception *e)
414db6bfe0SAlasdair G Kergon {
42b2a11465SJonathan Brassow 	struct transient_c *tc = store->context;
43fc56f6fbSMike Snitzer 	sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
444db6bfe0SAlasdair G Kergon 
45d0216849SJonathan Brassow 	if (size < (tc->next_free + store->chunk_size))
464db6bfe0SAlasdair G Kergon 		return -1;
474db6bfe0SAlasdair G Kergon 
4871fab00aSJonathan Brassow 	e->new_chunk = sector_to_chunk(store, tc->next_free);
49d0216849SJonathan Brassow 	tc->next_free += store->chunk_size;
504db6bfe0SAlasdair G Kergon 
514db6bfe0SAlasdair G Kergon 	return 0;
524db6bfe0SAlasdair G Kergon }
534db6bfe0SAlasdair G Kergon 
54a159c1acSJonathan Brassow static void transient_commit_exception(struct dm_exception_store *store,
55385277bfSMikulas Patocka 				       struct dm_exception *e, int valid,
564db6bfe0SAlasdair G Kergon 				       void (*callback) (void *, int success),
574db6bfe0SAlasdair G Kergon 				       void *callback_context)
584db6bfe0SAlasdair G Kergon {
594db6bfe0SAlasdair G Kergon 	/* Just succeed */
60385277bfSMikulas Patocka 	callback(callback_context, valid);
614db6bfe0SAlasdair G Kergon }
624db6bfe0SAlasdair G Kergon 
63985903bbSMike Snitzer static void transient_usage(struct dm_exception_store *store,
64985903bbSMike Snitzer 			    sector_t *total_sectors,
65985903bbSMike Snitzer 			    sector_t *sectors_allocated,
66985903bbSMike Snitzer 			    sector_t *metadata_sectors)
674db6bfe0SAlasdair G Kergon {
68985903bbSMike Snitzer 	*sectors_allocated = ((struct transient_c *) store->context)->next_free;
69fc56f6fbSMike Snitzer 	*total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
70985903bbSMike Snitzer 	*metadata_sectors = 0;
714db6bfe0SAlasdair G Kergon }
724db6bfe0SAlasdair G Kergon 
73b0d3cc01SMike Snitzer static int transient_ctr(struct dm_exception_store *store, char *options)
744db6bfe0SAlasdair G Kergon {
754db6bfe0SAlasdair G Kergon 	struct transient_c *tc;
764db6bfe0SAlasdair G Kergon 
774db6bfe0SAlasdair G Kergon 	tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
784db6bfe0SAlasdair G Kergon 	if (!tc)
794db6bfe0SAlasdair G Kergon 		return -ENOMEM;
804db6bfe0SAlasdair G Kergon 
814db6bfe0SAlasdair G Kergon 	tc->next_free = 0;
824db6bfe0SAlasdair G Kergon 	store->context = tc;
834db6bfe0SAlasdair G Kergon 
844db6bfe0SAlasdair G Kergon 	return 0;
854db6bfe0SAlasdair G Kergon }
864db6bfe0SAlasdair G Kergon 
871e302a92SJonathan Brassow static unsigned transient_status(struct dm_exception_store *store,
88493df71cSJonathan Brassow 				 status_type_t status, char *result,
89493df71cSJonathan Brassow 				 unsigned maxlen)
90493df71cSJonathan Brassow {
911e302a92SJonathan Brassow 	unsigned sz = 0;
921e302a92SJonathan Brassow 
931e302a92SJonathan Brassow 	switch (status) {
941e302a92SJonathan Brassow 	case STATUSTYPE_INFO:
951e302a92SJonathan Brassow 		break;
961e302a92SJonathan Brassow 	case STATUSTYPE_TABLE:
97fc56f6fbSMike Snitzer 		DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
981e302a92SJonathan Brassow 	}
99493df71cSJonathan Brassow 
100493df71cSJonathan Brassow 	return sz;
101493df71cSJonathan Brassow }
102493df71cSJonathan Brassow 
103493df71cSJonathan Brassow static struct dm_exception_store_type _transient_type = {
104493df71cSJonathan Brassow 	.name = "transient",
105493df71cSJonathan Brassow 	.module = THIS_MODULE,
106493df71cSJonathan Brassow 	.ctr = transient_ctr,
107493df71cSJonathan Brassow 	.dtr = transient_dtr,
108493df71cSJonathan Brassow 	.read_metadata = transient_read_metadata,
109493df71cSJonathan Brassow 	.prepare_exception = transient_prepare_exception,
110493df71cSJonathan Brassow 	.commit_exception = transient_commit_exception,
111985903bbSMike Snitzer 	.usage = transient_usage,
112493df71cSJonathan Brassow 	.status = transient_status,
113493df71cSJonathan Brassow };
114493df71cSJonathan Brassow 
115493df71cSJonathan Brassow static struct dm_exception_store_type _transient_compat_type = {
116493df71cSJonathan Brassow 	.name = "N",
117493df71cSJonathan Brassow 	.module = THIS_MODULE,
118493df71cSJonathan Brassow 	.ctr = transient_ctr,
119493df71cSJonathan Brassow 	.dtr = transient_dtr,
120493df71cSJonathan Brassow 	.read_metadata = transient_read_metadata,
121493df71cSJonathan Brassow 	.prepare_exception = transient_prepare_exception,
122493df71cSJonathan Brassow 	.commit_exception = transient_commit_exception,
123985903bbSMike Snitzer 	.usage = transient_usage,
124493df71cSJonathan Brassow 	.status = transient_status,
125493df71cSJonathan Brassow };
126493df71cSJonathan Brassow 
1274db6bfe0SAlasdair G Kergon int dm_transient_snapshot_init(void)
1284db6bfe0SAlasdair G Kergon {
129493df71cSJonathan Brassow 	int r;
130493df71cSJonathan Brassow 
131493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_transient_type);
132493df71cSJonathan Brassow 	if (r) {
133493df71cSJonathan Brassow 		DMWARN("Unable to register transient exception store type");
134493df71cSJonathan Brassow 		return r;
135493df71cSJonathan Brassow 	}
136493df71cSJonathan Brassow 
137493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_transient_compat_type);
138493df71cSJonathan Brassow 	if (r) {
139493df71cSJonathan Brassow 		DMWARN("Unable to register old-style transient "
140493df71cSJonathan Brassow 		       "exception store type");
141493df71cSJonathan Brassow 		dm_exception_store_type_unregister(&_transient_type);
142493df71cSJonathan Brassow 		return r;
143493df71cSJonathan Brassow 	}
144493df71cSJonathan Brassow 
145493df71cSJonathan Brassow 	return r;
1464db6bfe0SAlasdair G Kergon }
1474db6bfe0SAlasdair G Kergon 
1484db6bfe0SAlasdair G Kergon void dm_transient_snapshot_exit(void)
1494db6bfe0SAlasdair G Kergon {
150493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_transient_type);
151493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_transient_compat_type);
1524db6bfe0SAlasdair G Kergon }
153