dm-snap-transient.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
 * Copyright (C) 2006-2008 Red Hat GmbH
 *
 * This file is released under the GPL.
 */

#include "dm-exception-store.h"

#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/vmalloc.h>
13
#include <linux/export.h>
14 15 16 17 18 19 20 21 22 23 24 25
#include <linux/slab.h>
#include <linux/dm-io.h>

#define DM_MSG_PREFIX "transient snapshot"

/*-----------------------------------------------------------------
 * Implementation of the store for non-persistent snapshots.
 *---------------------------------------------------------------*/
struct transient_c {
	sector_t next_free;
};

26
static void transient_dtr(struct dm_exception_store *store)
27 28 29 30
{
	kfree(store->context);
}

31 32 33 34
static int transient_read_metadata(struct dm_exception_store *store,
				   int (*callback)(void *callback_context,
						   chunk_t old, chunk_t new),
				   void *callback_context)
35 36 37 38
{
	return 0;
}

39
static int transient_prepare_exception(struct dm_exception_store *store,
40
				       struct dm_exception *e)
41
{
42
	struct transient_c *tc = store->context;
43
	sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
44

45
	if (size < (tc->next_free + store->chunk_size))
46 47
		return -1;

48
	e->new_chunk = sector_to_chunk(store, tc->next_free);
49
	tc->next_free += store->chunk_size;
50 51 52 53

	return 0;
}

54
static void transient_commit_exception(struct dm_exception_store *store,
55
				       struct dm_exception *e, int valid,
56 57
				       void (*callback) (void *, int success),
				       void *callback_context)
58 59
{
	/* Just succeed */
60
	callback(callback_context, valid);
61 62
}

63 64 65 66
static void transient_usage(struct dm_exception_store *store,
			    sector_t *total_sectors,
			    sector_t *sectors_allocated,
			    sector_t *metadata_sectors)
67
{
68
	*sectors_allocated = ((struct transient_c *) store->context)->next_free;
69
	*total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
70
	*metadata_sectors = 0;
71 72
}

73
static int transient_ctr(struct dm_exception_store *store, char *options)
74 75 76 77 78 79 80 81 82 83 84 85 86
{
	struct transient_c *tc;

	tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
	if (!tc)
		return -ENOMEM;

	tc->next_free = 0;
	store->context = tc;

	return 0;
}

87 88 89
static unsigned transient_status(struct dm_exception_store *store,
				 status_type_t status, char *result,
				 unsigned maxlen)
90
{
91 92 93 94 95 96
	unsigned sz = 0;

	switch (status) {
	case STATUSTYPE_INFO:
		break;
	case STATUSTYPE_TABLE:
97
		DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
98
	}
99 100 101 102 103 104 105 106 107 108 109 110

	return sz;
}

static struct dm_exception_store_type _transient_type = {
	.name = "transient",
	.module = THIS_MODULE,
	.ctr = transient_ctr,
	.dtr = transient_dtr,
	.read_metadata = transient_read_metadata,
	.prepare_exception = transient_prepare_exception,
	.commit_exception = transient_commit_exception,
111
	.usage = transient_usage,
112 113 114 115 116 117 118 119 120 121 122
	.status = transient_status,
};

static struct dm_exception_store_type _transient_compat_type = {
	.name = "N",
	.module = THIS_MODULE,
	.ctr = transient_ctr,
	.dtr = transient_dtr,
	.read_metadata = transient_read_metadata,
	.prepare_exception = transient_prepare_exception,
	.commit_exception = transient_commit_exception,
123
	.usage = transient_usage,
124 125 126
	.status = transient_status,
};

127 128
int dm_transient_snapshot_init(void)
{
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	int r;

	r = dm_exception_store_type_register(&_transient_type);
	if (r) {
		DMWARN("Unable to register transient exception store type");
		return r;
	}

	r = dm_exception_store_type_register(&_transient_compat_type);
	if (r) {
		DMWARN("Unable to register old-style transient "
		       "exception store type");
		dm_exception_store_type_unregister(&_transient_type);
		return r;
	}

	return r;
146 147 148 149
}

void dm_transient_snapshot_exit(void)
{
150 151
	dm_exception_store_type_unregister(&_transient_type);
	dm_exception_store_type_unregister(&_transient_compat_type);
152
}