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);
98*8ec45662STushar Sugandhi 		break;
99*8ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
100*8ec45662STushar Sugandhi 		*result = '\0';
101*8ec45662STushar Sugandhi 		break;
1021e302a92SJonathan Brassow 	}
103493df71cSJonathan Brassow 
104493df71cSJonathan Brassow 	return sz;
105493df71cSJonathan Brassow }
106493df71cSJonathan Brassow 
107493df71cSJonathan Brassow static struct dm_exception_store_type _transient_type = {
108493df71cSJonathan Brassow 	.name = "transient",
109493df71cSJonathan Brassow 	.module = THIS_MODULE,
110493df71cSJonathan Brassow 	.ctr = transient_ctr,
111493df71cSJonathan Brassow 	.dtr = transient_dtr,
112493df71cSJonathan Brassow 	.read_metadata = transient_read_metadata,
113493df71cSJonathan Brassow 	.prepare_exception = transient_prepare_exception,
114493df71cSJonathan Brassow 	.commit_exception = transient_commit_exception,
115985903bbSMike Snitzer 	.usage = transient_usage,
116493df71cSJonathan Brassow 	.status = transient_status,
117493df71cSJonathan Brassow };
118493df71cSJonathan Brassow 
119493df71cSJonathan Brassow static struct dm_exception_store_type _transient_compat_type = {
120493df71cSJonathan Brassow 	.name = "N",
121493df71cSJonathan Brassow 	.module = THIS_MODULE,
122493df71cSJonathan Brassow 	.ctr = transient_ctr,
123493df71cSJonathan Brassow 	.dtr = transient_dtr,
124493df71cSJonathan Brassow 	.read_metadata = transient_read_metadata,
125493df71cSJonathan Brassow 	.prepare_exception = transient_prepare_exception,
126493df71cSJonathan Brassow 	.commit_exception = transient_commit_exception,
127985903bbSMike Snitzer 	.usage = transient_usage,
128493df71cSJonathan Brassow 	.status = transient_status,
129493df71cSJonathan Brassow };
130493df71cSJonathan Brassow 
1314db6bfe0SAlasdair G Kergon int dm_transient_snapshot_init(void)
1324db6bfe0SAlasdair G Kergon {
133493df71cSJonathan Brassow 	int r;
134493df71cSJonathan Brassow 
135493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_transient_type);
136493df71cSJonathan Brassow 	if (r) {
137493df71cSJonathan Brassow 		DMWARN("Unable to register transient exception store type");
138493df71cSJonathan Brassow 		return r;
139493df71cSJonathan Brassow 	}
140493df71cSJonathan Brassow 
141493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_transient_compat_type);
142493df71cSJonathan Brassow 	if (r) {
143493df71cSJonathan Brassow 		DMWARN("Unable to register old-style transient "
144493df71cSJonathan Brassow 		       "exception store type");
145493df71cSJonathan Brassow 		dm_exception_store_type_unregister(&_transient_type);
146493df71cSJonathan Brassow 		return r;
147493df71cSJonathan Brassow 	}
148493df71cSJonathan Brassow 
149493df71cSJonathan Brassow 	return r;
1504db6bfe0SAlasdair G Kergon }
1514db6bfe0SAlasdair G Kergon 
1524db6bfe0SAlasdair G Kergon void dm_transient_snapshot_exit(void)
1534db6bfe0SAlasdair G Kergon {
154493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_transient_type);
155493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_transient_compat_type);
1564db6bfe0SAlasdair G Kergon }
157