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,
551d4989c8SJon Brassow 				       struct dm_exception *e,
564db6bfe0SAlasdair G Kergon 				       void (*callback) (void *, int success),
574db6bfe0SAlasdair G Kergon 				       void *callback_context)
584db6bfe0SAlasdair G Kergon {
594db6bfe0SAlasdair G Kergon 	/* Just succeed */
604db6bfe0SAlasdair G Kergon 	callback(callback_context, 1);
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 
73493df71cSJonathan Brassow static int transient_ctr(struct dm_exception_store *store,
74493df71cSJonathan Brassow 			 unsigned argc, char **argv)
754db6bfe0SAlasdair G Kergon {
764db6bfe0SAlasdair G Kergon 	struct transient_c *tc;
774db6bfe0SAlasdair G Kergon 
784db6bfe0SAlasdair G Kergon 	tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
794db6bfe0SAlasdair G Kergon 	if (!tc)
804db6bfe0SAlasdair G Kergon 		return -ENOMEM;
814db6bfe0SAlasdair G Kergon 
824db6bfe0SAlasdair G Kergon 	tc->next_free = 0;
834db6bfe0SAlasdair G Kergon 	store->context = tc;
844db6bfe0SAlasdair G Kergon 
854db6bfe0SAlasdair G Kergon 	return 0;
864db6bfe0SAlasdair G Kergon }
874db6bfe0SAlasdair G Kergon 
881e302a92SJonathan Brassow static unsigned transient_status(struct dm_exception_store *store,
89493df71cSJonathan Brassow 				 status_type_t status, char *result,
90493df71cSJonathan Brassow 				 unsigned maxlen)
91493df71cSJonathan Brassow {
921e302a92SJonathan Brassow 	unsigned sz = 0;
931e302a92SJonathan Brassow 
941e302a92SJonathan Brassow 	switch (status) {
951e302a92SJonathan Brassow 	case STATUSTYPE_INFO:
961e302a92SJonathan Brassow 		break;
971e302a92SJonathan Brassow 	case STATUSTYPE_TABLE:
98fc56f6fbSMike Snitzer 		DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
991e302a92SJonathan Brassow 	}
100493df71cSJonathan Brassow 
101493df71cSJonathan Brassow 	return sz;
102493df71cSJonathan Brassow }
103493df71cSJonathan Brassow 
104493df71cSJonathan Brassow static struct dm_exception_store_type _transient_type = {
105493df71cSJonathan Brassow 	.name = "transient",
106493df71cSJonathan Brassow 	.module = THIS_MODULE,
107493df71cSJonathan Brassow 	.ctr = transient_ctr,
108493df71cSJonathan Brassow 	.dtr = transient_dtr,
109493df71cSJonathan Brassow 	.read_metadata = transient_read_metadata,
110493df71cSJonathan Brassow 	.prepare_exception = transient_prepare_exception,
111493df71cSJonathan Brassow 	.commit_exception = transient_commit_exception,
112985903bbSMike Snitzer 	.usage = transient_usage,
113493df71cSJonathan Brassow 	.status = transient_status,
114493df71cSJonathan Brassow };
115493df71cSJonathan Brassow 
116493df71cSJonathan Brassow static struct dm_exception_store_type _transient_compat_type = {
117493df71cSJonathan Brassow 	.name = "N",
118493df71cSJonathan Brassow 	.module = THIS_MODULE,
119493df71cSJonathan Brassow 	.ctr = transient_ctr,
120493df71cSJonathan Brassow 	.dtr = transient_dtr,
121493df71cSJonathan Brassow 	.read_metadata = transient_read_metadata,
122493df71cSJonathan Brassow 	.prepare_exception = transient_prepare_exception,
123493df71cSJonathan Brassow 	.commit_exception = transient_commit_exception,
124985903bbSMike Snitzer 	.usage = transient_usage,
125493df71cSJonathan Brassow 	.status = transient_status,
126493df71cSJonathan Brassow };
127493df71cSJonathan Brassow 
1284db6bfe0SAlasdair G Kergon int dm_transient_snapshot_init(void)
1294db6bfe0SAlasdair G Kergon {
130493df71cSJonathan Brassow 	int r;
131493df71cSJonathan Brassow 
132493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_transient_type);
133493df71cSJonathan Brassow 	if (r) {
134493df71cSJonathan Brassow 		DMWARN("Unable to register transient exception store type");
135493df71cSJonathan Brassow 		return r;
136493df71cSJonathan Brassow 	}
137493df71cSJonathan Brassow 
138493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_transient_compat_type);
139493df71cSJonathan Brassow 	if (r) {
140493df71cSJonathan Brassow 		DMWARN("Unable to register old-style transient "
141493df71cSJonathan Brassow 		       "exception store type");
142493df71cSJonathan Brassow 		dm_exception_store_type_unregister(&_transient_type);
143493df71cSJonathan Brassow 		return r;
144493df71cSJonathan Brassow 	}
145493df71cSJonathan Brassow 
146493df71cSJonathan Brassow 	return r;
1474db6bfe0SAlasdair G Kergon }
1484db6bfe0SAlasdair G Kergon 
1494db6bfe0SAlasdair G Kergon void dm_transient_snapshot_exit(void)
1504db6bfe0SAlasdair G Kergon {
151493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_transient_type);
152493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_transient_compat_type);
1534db6bfe0SAlasdair G Kergon }
154