File: dm-log.c

package info (click to toggle)
devmapper 2%3A1.01.00-4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,044 kB
  • ctags: 2,336
  • sloc: ansic: 11,777; sh: 2,695; makefile: 291; perl: 16
file content (310 lines) | stat: -rw-r--r-- 6,856 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (C) 2003 Sistina Software
 *
 * This file is released under the LGPL.
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/vmalloc.h>

#include "dm-log.h"
#include "dm-io.h"

static LIST_HEAD(_log_types);
static spinlock_t _lock = SPIN_LOCK_UNLOCKED;

int dm_register_dirty_log_type(struct dirty_log_type *type)
{
	spin_lock(&_lock);
	type->use_count = 0;
	if (type->module)
		__MOD_INC_USE_COUNT(type->module);

	list_add(&type->list, &_log_types);
	spin_unlock(&_lock);

	return 0;
}

int dm_unregister_dirty_log_type(struct dirty_log_type *type)
{
	spin_lock(&_lock);

	if (type->use_count)
		DMWARN("Attempt to unregister a log type that is still in use");
	else {
		list_del(&type->list);
		if (type->module)
			__MOD_DEC_USE_COUNT(type->module);
	}

	spin_unlock(&_lock);

	return 0;
}

static struct dirty_log_type *get_type(const char *type_name)
{
	struct dirty_log_type *type;
	struct list_head *tmp;

	spin_lock(&_lock);
	list_for_each (tmp, &_log_types) {
		type = list_entry(tmp, struct dirty_log_type, list);
		if (!strcmp(type_name, type->name)) {
			type->use_count++;
			spin_unlock(&_lock);
			return type;
		}
	}

	spin_unlock(&_lock);
	return NULL;
}

static void put_type(struct dirty_log_type *type)
{
	spin_lock(&_lock);
	type->use_count--;
	spin_unlock(&_lock);
}

struct dirty_log *dm_create_dirty_log(const char *type_name, sector_t dev_size,
				      unsigned int argc, char **argv)
{
	struct dirty_log_type *type;
	struct dirty_log *log;

	log = kmalloc(sizeof(*log), GFP_KERNEL);
	if (!log)
		return NULL;

	type = get_type(type_name);
	if (!type) {
		kfree(log);
		return NULL;
	}

	log->type = type;
	if (type->ctr(log, dev_size, argc, argv)) {
		kfree(log);
		put_type(type);
		return NULL;
	}

	return log;
}

void dm_destroy_dirty_log(struct dirty_log *log)
{
	log->type->dtr(log);
	put_type(log->type);
	kfree(log);
}


/*-----------------------------------------------------------------
 * In core log, ie. trivial, non-persistent
 *
 * For now we'll keep this simple and just have 2 bitsets, one
 * for clean/dirty, the other for sync/nosync.  The sync bitset
 * will be freed when everything is in sync.
 *
 * FIXME: problems with a 64bit sector_t
 *---------------------------------------------------------------*/
struct core_log {
	sector_t region_size;
	unsigned int region_count;
	unsigned long *clean_bits;
	unsigned long *sync_bits;
	unsigned long *recovering_bits;	/* FIXME: this seems excessive */

	int sync_search;
};

#define BYTE_SHIFT 3

static int core_ctr(struct dirty_log *log, sector_t dev_size,
		    unsigned int argc, char **argv)
{
	struct core_log *clog;
	sector_t region_size;
	unsigned int region_count;
	size_t bitset_size;

	if (argc != 1) {
		DMWARN("wrong number of arguments to core_log");
		return -EINVAL;
	}

	if (sscanf(argv[0], SECTOR_FORMAT, &region_size) != 1) {
		DMWARN("invalid region size string");
		return -EINVAL;
	}

	region_count = dm_div_up(dev_size, region_size);

	clog = kmalloc(sizeof(*clog), GFP_KERNEL);
	if (!clog) {
		DMWARN("couldn't allocate core log");
		return -ENOMEM;
	}

	clog->region_size = region_size;
	clog->region_count = region_count;

	/*
 	 * Work out how many words we need to hold the bitset.
 	 */
	bitset_size = dm_round_up(region_count,
				  sizeof(*clog->clean_bits) << BYTE_SHIFT);
	bitset_size >>= BYTE_SHIFT;

	clog->clean_bits = vmalloc(bitset_size);
	if (!clog->clean_bits) {
		DMWARN("couldn't allocate clean bitset");
		kfree(clog);
		return -ENOMEM;
	}
	memset(clog->clean_bits, -1, bitset_size);

	clog->sync_bits = vmalloc(bitset_size);
	if (!clog->sync_bits) {
		DMWARN("couldn't allocate sync bitset");
		vfree(clog->clean_bits);
		kfree(clog);
		return -ENOMEM;
	}
	memset(clog->sync_bits, 0, bitset_size);

	clog->recovering_bits = vmalloc(bitset_size);
	if (!clog->recovering_bits) {
		DMWARN("couldn't allocate sync bitset");
		vfree(clog->sync_bits);
		vfree(clog->clean_bits);
		kfree(clog);
		return -ENOMEM;
	}
	memset(clog->recovering_bits, 0, bitset_size);
	clog->sync_search = 0;
	log->context = clog;
	return 0;
}

static void core_dtr(struct dirty_log *log)
{
	struct core_log *clog = (struct core_log *) log->context;
	vfree(clog->clean_bits);
	vfree(clog->sync_bits);
	vfree(clog->recovering_bits);
	kfree(clog);
}

static sector_t core_get_region_size(struct dirty_log *log)
{
	struct core_log *clog = (struct core_log *) log->context;
	return clog->region_size;
}

static int core_is_clean(struct dirty_log *log, region_t region)
{
	struct core_log *clog = (struct core_log *) log->context;
	return test_bit(region, clog->clean_bits);
}

static int core_in_sync(struct dirty_log *log, region_t region, int block)
{
	struct core_log *clog = (struct core_log *) log->context;

	return test_bit(region, clog->sync_bits) ? 1 : 0;
}

static int core_flush(struct dirty_log *log)
{
	/* no op */
	return 0;
}

static void core_mark_region(struct dirty_log *log, region_t region)
{
	struct core_log *clog = (struct core_log *) log->context;
	clear_bit(region, clog->clean_bits);
}

static void core_clear_region(struct dirty_log *log, region_t region)
{
	struct core_log *clog = (struct core_log *) log->context;
	set_bit(region, clog->clean_bits);
}

static int core_get_resync_work(struct dirty_log *log, region_t *region)
{
	struct core_log *clog = (struct core_log *) log->context;

	if (clog->sync_search >= clog->region_count)
		return 0;

	do {
		*region = find_next_zero_bit(clog->sync_bits,
					     clog->region_count,
					     clog->sync_search);
		clog->sync_search = *region + 1;

		if (*region == clog->region_count)
			return 0;

	} while (test_bit(*region, clog->recovering_bits));

	set_bit(*region, clog->recovering_bits);
	return 1;
}

static void core_complete_resync_work(struct dirty_log *log, region_t region,
				      int success)
{
	struct core_log *clog = (struct core_log *) log->context;

	clear_bit(region, clog->recovering_bits);
	if (success)
		set_bit(region, clog->sync_bits);
}

static struct dirty_log_type _core_type = {
	.name = "core",

	.ctr = core_ctr,
	.dtr = core_dtr,
	.get_region_size = core_get_region_size,
	.is_clean = core_is_clean,
	.in_sync = core_in_sync,
	.flush = core_flush,
	.mark_region = core_mark_region,
	.clear_region = core_clear_region,
	.get_resync_work = core_get_resync_work,
	.complete_resync_work = core_complete_resync_work
};

__init int dm_dirty_log_init(void)
{
	int r;

	r = dm_register_dirty_log_type(&_core_type);
	if (r)
		DMWARN("couldn't register core log");

	return r;
}

void dm_dirty_log_exit(void)
{
	dm_unregister_dirty_log_type(&_core_type);
}

EXPORT_SYMBOL(dm_register_dirty_log_type);
EXPORT_SYMBOL(dm_unregister_dirty_log_type);
EXPORT_SYMBOL(dm_dirty_log_init);
EXPORT_SYMBOL(dm_dirty_log_exit);
EXPORT_SYMBOL(dm_create_dirty_log);
EXPORT_SYMBOL(dm_destroy_dirty_log);