dm-log.c 19.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (C) 2003 Sistina Software
3
 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11
 *
 * This file is released under the LGPL.
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
A
Alasdair G Kergon 已提交
12 13
#include <linux/dm-io.h>
#include <linux/dm-dirty-log.h>
L
Linus Torvalds 已提交
14

15
#include <linux/device-mapper.h>
L
Linus Torvalds 已提交
16

17
#define DM_MSG_PREFIX "dirty region log"
18

19 20 21 22 23 24 25
struct dm_dirty_log_internal {
	struct dm_dirty_log_type *type;

	struct list_head list;
	long use;
};

L
Linus Torvalds 已提交
26 27 28
static LIST_HEAD(_log_types);
static DEFINE_SPINLOCK(_lock);

29
static struct dm_dirty_log_internal *__find_dirty_log_type(const char *name)
L
Linus Torvalds 已提交
30
{
31 32 33 34 35 36 37 38 39 40 41 42
	struct dm_dirty_log_internal *log_type;

	list_for_each_entry(log_type, &_log_types, list)
		if (!strcmp(name, log_type->type->name))
			return log_type;

	return NULL;
}

static struct dm_dirty_log_internal *_get_dirty_log_type(const char *name)
{
	struct dm_dirty_log_internal *log_type;
L
Linus Torvalds 已提交
43 44

	spin_lock(&_lock);
45 46 47 48 49 50 51 52

	log_type = __find_dirty_log_type(name);
	if (log_type) {
		if (!log_type->use && !try_module_get(log_type->type->module))
			log_type = NULL;
		else
			log_type->use++;
	}
L
Linus Torvalds 已提交
53 54

	spin_unlock(&_lock);
55 56

	return log_type;
L
Linus Torvalds 已提交
57 58
}

J
Jonathan Brassow 已提交
59 60 61 62
/*
 * get_type
 * @type_name
 *
63
 * Attempt to retrieve the dm_dirty_log_type by name.  If not already
J
Jonathan Brassow 已提交
64 65 66 67 68 69 70 71 72 73 74 75
 * available, attempt to load the appropriate module.
 *
 * Log modules are named "dm-log-" followed by the 'type_name'.
 * Modules may contain multiple types.
 * This function will first try the module "dm-log-<type_name>",
 * then truncate 'type_name' on the last '-' and try again.
 *
 * For example, if type_name was "clustered-disk", it would search
 * 'dm-log-clustered-disk' then 'dm-log-clustered'.
 *
 * Returns: dirty_log_type* on success, NULL on failure
 */
H
Heinz Mauelshagen 已提交
76
static struct dm_dirty_log_type *get_type(const char *type_name)
J
Jonathan Brassow 已提交
77 78
{
	char *p, *type_name_dup;
79
	struct dm_dirty_log_internal *log_type;
J
Jonathan Brassow 已提交
80

81 82 83 84 85 86
	if (!type_name)
		return NULL;

	log_type = _get_dirty_log_type(type_name);
	if (log_type)
		return log_type->type;
J
Jonathan Brassow 已提交
87 88 89 90 91 92 93 94 95

	type_name_dup = kstrdup(type_name, GFP_KERNEL);
	if (!type_name_dup) {
		DMWARN("No memory left to attempt log module load for \"%s\"",
		       type_name);
		return NULL;
	}

	while (request_module("dm-log-%s", type_name_dup) ||
96
	       !(log_type = _get_dirty_log_type(type_name))) {
J
Jonathan Brassow 已提交
97 98 99 100 101 102
		p = strrchr(type_name_dup, '-');
		if (!p)
			break;
		p[0] = '\0';
	}

103
	if (!log_type)
J
Jonathan Brassow 已提交
104 105 106 107
		DMWARN("Module for logging type \"%s\" not found.", type_name);

	kfree(type_name_dup);

108
	return log_type ? log_type->type : NULL;
J
Jonathan Brassow 已提交
109 110
}

H
Heinz Mauelshagen 已提交
111
static void put_type(struct dm_dirty_log_type *type)
L
Linus Torvalds 已提交
112
{
113 114 115 116 117
	struct dm_dirty_log_internal *log_type;

	if (!type)
		return;

L
Linus Torvalds 已提交
118
	spin_lock(&_lock);
119 120 121 122 123
	log_type = __find_dirty_log_type(type->name);
	if (!log_type)
		goto out;

	if (!--log_type->use)
L
Linus Torvalds 已提交
124
		module_put(type->module);
125 126 127 128

	BUG_ON(log_type->use < 0);

out:
L
Linus Torvalds 已提交
129 130 131
	spin_unlock(&_lock);
}

132 133 134 135 136 137 138 139 140 141 142
static struct dm_dirty_log_internal *_alloc_dirty_log_type(struct dm_dirty_log_type *type)
{
	struct dm_dirty_log_internal *log_type = kzalloc(sizeof(*log_type),
							 GFP_KERNEL);

	if (log_type)
		log_type->type = type;

	return log_type;
}

143 144
int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
{
145 146 147 148 149 150
	struct dm_dirty_log_internal *log_type = _alloc_dirty_log_type(type);
	int r = 0;

	if (!log_type)
		return -ENOMEM;

151
	spin_lock(&_lock);
152 153 154 155 156 157
	if (!__find_dirty_log_type(type->name))
		list_add(&log_type->list, &_log_types);
	else {
		kfree(log_type);
		r = -EEXIST;
	}
158 159
	spin_unlock(&_lock);

160
	return r;
161 162 163 164 165
}
EXPORT_SYMBOL(dm_dirty_log_type_register);

int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
{
166 167
	struct dm_dirty_log_internal *log_type;

168 169
	spin_lock(&_lock);

170 171 172 173 174 175 176 177 178 179 180 181
	log_type = __find_dirty_log_type(type->name);
	if (!log_type) {
		spin_unlock(&_lock);
		return -EINVAL;
	}

	if (log_type->use) {
		spin_unlock(&_lock);
		return -ETXTBSY;
	}

	list_del(&log_type->list);
182 183

	spin_unlock(&_lock);
184
	kfree(log_type);
185 186 187 188 189

	return 0;
}
EXPORT_SYMBOL(dm_dirty_log_type_unregister);

H
Heinz Mauelshagen 已提交
190 191 192
struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
					 struct dm_target *ti,
					 unsigned int argc, char **argv)
L
Linus Torvalds 已提交
193
{
H
Heinz Mauelshagen 已提交
194 195
	struct dm_dirty_log_type *type;
	struct dm_dirty_log *log;
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	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, ti, argc, argv)) {
		kfree(log);
		put_type(type);
		return NULL;
	}

	return log;
}
H
Heinz Mauelshagen 已提交
216
EXPORT_SYMBOL(dm_dirty_log_create);
L
Linus Torvalds 已提交
217

H
Heinz Mauelshagen 已提交
218
void dm_dirty_log_destroy(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
219 220 221 222 223
{
	log->type->dtr(log);
	put_type(log->type);
	kfree(log);
}
H
Heinz Mauelshagen 已提交
224
EXPORT_SYMBOL(dm_dirty_log_destroy);
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237

/*-----------------------------------------------------------------
 * Persistent and core logs share a lot of their implementation.
 * FIXME: need a reload method to be called from a resume
 *---------------------------------------------------------------*/
/*
 * Magic for persistent mirrors: "MiRr"
 */
#define MIRROR_MAGIC 0x4D695272

/*
 * The on-disk version of the metadata.
 */
238
#define MIRROR_DISK_VERSION 2
L
Linus Torvalds 已提交
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
#define LOG_OFFSET 2

struct log_header {
	uint32_t magic;

	/*
	 * Simple, incrementing version. no backward
	 * compatibility.
	 */
	uint32_t version;
	sector_t nr_regions;
};

struct log_c {
	struct dm_target *ti;
	int touched;
	uint32_t region_size;
	unsigned int region_count;
	region_t sync_count;

	unsigned bitset_uint32_count;
	uint32_t *clean_bits;
	uint32_t *sync_bits;
	uint32_t *recovering_bits;	/* FIXME: this seems excessive */

	int sync_search;

	/* Resync flag */
	enum sync {
		DEFAULTSYNC,	/* Synchronize if necessary */
		NOSYNC,		/* Devices known to be already in sync */
		FORCESYNC,	/* Force a sync to happen */
	} sync;

273 274
	struct dm_io_request io_req;

L
Linus Torvalds 已提交
275 276 277
	/*
	 * Disk log fields
	 */
J
Jonathan E Brassow 已提交
278
	int log_dev_failed;
L
Linus Torvalds 已提交
279 280 281
	struct dm_dev *log_dev;
	struct log_header header;

H
Heinz Mauelshagen 已提交
282
	struct dm_io_region header_location;
L
Linus Torvalds 已提交
283 284 285 286 287 288 289
	struct log_header *disk_header;
};

/*
 * The touched member needs to be updated every time we access
 * one of the bitsets.
 */
H
Heinz Mauelshagen 已提交
290
static inline int log_test_bit(uint32_t *bs, unsigned bit)
L
Linus Torvalds 已提交
291
{
292
	return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
L
Linus Torvalds 已提交
293 294 295 296 297
}

static inline void log_set_bit(struct log_c *l,
			       uint32_t *bs, unsigned bit)
{
298
	ext2_set_bit(bit, (unsigned long *) bs);
L
Linus Torvalds 已提交
299 300 301 302 303 304
	l->touched = 1;
}

static inline void log_clear_bit(struct log_c *l,
				 uint32_t *bs, unsigned bit)
{
305
	ext2_clear_bit(bit, (unsigned long *) bs);
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	l->touched = 1;
}

/*----------------------------------------------------------------
 * Header IO
 *--------------------------------------------------------------*/
static void header_to_disk(struct log_header *core, struct log_header *disk)
{
	disk->magic = cpu_to_le32(core->magic);
	disk->version = cpu_to_le32(core->version);
	disk->nr_regions = cpu_to_le64(core->nr_regions);
}

static void header_from_disk(struct log_header *core, struct log_header *disk)
{
	core->magic = le32_to_cpu(disk->magic);
	core->version = le32_to_cpu(disk->version);
	core->nr_regions = le64_to_cpu(disk->nr_regions);
}

326 327 328 329 330 331 332 333 334
static int rw_header(struct log_c *lc, int rw)
{
	lc->io_req.bi_rw = rw;
	lc->io_req.mem.ptr.vma = lc->disk_header;
	lc->io_req.notify.fn = NULL;

	return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
}

L
Linus Torvalds 已提交
335 336 337 338
static int read_header(struct log_c *log)
{
	int r;

339
	r = rw_header(log, READ);
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348 349 350 351
	if (r)
		return r;

	header_from_disk(&log->header, log->disk_header);

	/* New log required? */
	if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
		log->header.magic = MIRROR_MAGIC;
		log->header.version = MIRROR_DISK_VERSION;
		log->header.nr_regions = 0;
	}

352 353 354 355 356
#ifdef __LITTLE_ENDIAN
	if (log->header.version == 1)
		log->header.version = 2;
#endif

L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367
	if (log->header.version != MIRROR_DISK_VERSION) {
		DMWARN("incompatible disk log version");
		return -EINVAL;
	}

	return 0;
}

static inline int write_header(struct log_c *log)
{
	header_to_disk(&log->header, log->disk_header);
368
	return rw_header(log, WRITE);
L
Linus Torvalds 已提交
369 370 371 372 373 374 375 376
}

/*----------------------------------------------------------------
 * core log constructor/destructor
 *
 * argv contains region_size followed optionally by [no]sync
 *--------------------------------------------------------------*/
#define BYTE_SHIFT 3
H
Heinz Mauelshagen 已提交
377
static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
378 379
			      unsigned int argc, char **argv,
			      struct dm_dev *dev)
L
Linus Torvalds 已提交
380 381 382 383 384 385
{
	enum sync sync = DEFAULTSYNC;

	struct log_c *lc;
	uint32_t region_size;
	unsigned int region_count;
386
	size_t bitset_size, buf_size;
387
	int r;
L
Linus Torvalds 已提交
388 389

	if (argc < 1 || argc > 2) {
390
		DMWARN("wrong number of arguments to dirty region log");
L
Linus Torvalds 已提交
391 392 393 394 395 396 397 398 399
		return -EINVAL;
	}

	if (argc > 1) {
		if (!strcmp(argv[1], "sync"))
			sync = FORCESYNC;
		else if (!strcmp(argv[1], "nosync"))
			sync = NOSYNC;
		else {
400 401
			DMWARN("unrecognised sync argument to "
			       "dirty region log: %s", argv[1]);
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
			return -EINVAL;
		}
	}

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

	region_count = dm_sector_div_up(ti->len, region_size);

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

	lc->ti = ti;
	lc->touched = 0;
	lc->region_size = region_size;
	lc->region_count = region_count;
	lc->sync = sync;

	/*
426
	 * Work out how many "unsigned long"s we need to hold the bitset.
L
Linus Torvalds 已提交
427 428
	 */
	bitset_size = dm_round_up(region_count,
429
				  sizeof(*lc->clean_bits) << BYTE_SHIFT);
L
Linus Torvalds 已提交
430 431
	bitset_size >>= BYTE_SHIFT;

432
	lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
433 434 435 436 437 438 439 440 441 442 443 444 445 446

	/*
	 * Disk log?
	 */
	if (!dev) {
		lc->clean_bits = vmalloc(bitset_size);
		if (!lc->clean_bits) {
			DMWARN("couldn't allocate clean bitset");
			kfree(lc);
			return -ENOMEM;
		}
		lc->disk_header = NULL;
	} else {
		lc->log_dev = dev;
J
Jonathan E Brassow 已提交
447
		lc->log_dev_failed = 0;
448 449 450 451 452 453 454 455 456
		lc->header_location.bdev = lc->log_dev->bdev;
		lc->header_location.sector = 0;

		/*
		 * Buffer holds both header and bitset.
		 */
		buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
				       bitset_size, ti->limits.hardsect_size);
		lc->header_location.count = buf_size >> SECTOR_SHIFT;
457 458 459 460 461 462 463 464 465
		lc->io_req.mem.type = DM_IO_VMA;
		lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
								   PAGE_SIZE));
		if (IS_ERR(lc->io_req.client)) {
			r = PTR_ERR(lc->io_req.client);
			DMWARN("couldn't allocate disk io client");
			kfree(lc);
			return -ENOMEM;
		}
466 467 468 469

		lc->disk_header = vmalloc(buf_size);
		if (!lc->disk_header) {
			DMWARN("couldn't allocate disk log buffer");
470
			dm_io_client_destroy(lc->io_req.client);
471 472 473 474 475 476
			kfree(lc);
			return -ENOMEM;
		}

		lc->clean_bits = (void *)lc->disk_header +
				 (LOG_OFFSET << SECTOR_SHIFT);
L
Linus Torvalds 已提交
477
	}
478

L
Linus Torvalds 已提交
479 480 481 482 483
	memset(lc->clean_bits, -1, bitset_size);

	lc->sync_bits = vmalloc(bitset_size);
	if (!lc->sync_bits) {
		DMWARN("couldn't allocate sync bitset");
484 485
		if (!dev)
			vfree(lc->clean_bits);
486 487
		else
			dm_io_client_destroy(lc->io_req.client);
488
		vfree(lc->disk_header);
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497 498
		kfree(lc);
		return -ENOMEM;
	}
	memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
	lc->sync_count = (sync == NOSYNC) ? region_count : 0;

	lc->recovering_bits = vmalloc(bitset_size);
	if (!lc->recovering_bits) {
		DMWARN("couldn't allocate sync bitset");
		vfree(lc->sync_bits);
499 500
		if (!dev)
			vfree(lc->clean_bits);
501 502
		else
			dm_io_client_destroy(lc->io_req.client);
503
		vfree(lc->disk_header);
L
Linus Torvalds 已提交
504 505 506 507 508 509
		kfree(lc);
		return -ENOMEM;
	}
	memset(lc->recovering_bits, 0, bitset_size);
	lc->sync_search = 0;
	log->context = lc;
510

L
Linus Torvalds 已提交
511 512 513
	return 0;
}

H
Heinz Mauelshagen 已提交
514
static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
515 516 517 518 519 520
		    unsigned int argc, char **argv)
{
	return create_log_context(log, ti, argc, argv, NULL);
}

static void destroy_log_context(struct log_c *lc)
L
Linus Torvalds 已提交
521 522 523 524 525 526
{
	vfree(lc->sync_bits);
	vfree(lc->recovering_bits);
	kfree(lc);
}

H
Heinz Mauelshagen 已提交
527
static void core_dtr(struct dm_dirty_log *log)
528 529 530 531 532 533 534
{
	struct log_c *lc = (struct log_c *) log->context;

	vfree(lc->clean_bits);
	destroy_log_context(lc);
}

L
Linus Torvalds 已提交
535 536 537 538 539
/*----------------------------------------------------------------
 * disk log constructor/destructor
 *
 * argv contains log_device region_size followed optionally by [no]sync
 *--------------------------------------------------------------*/
H
Heinz Mauelshagen 已提交
540
static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
L
Linus Torvalds 已提交
541 542 543 544 545 546
		    unsigned int argc, char **argv)
{
	int r;
	struct dm_dev *dev;

	if (argc < 2 || argc > 3) {
547
		DMWARN("wrong number of arguments to disk dirty region log");
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555
		return -EINVAL;
	}

	r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
			  FMODE_READ | FMODE_WRITE, &dev);
	if (r)
		return r;

556
	r = create_log_context(log, ti, argc - 1, argv + 1, dev);
L
Linus Torvalds 已提交
557 558 559 560 561 562 563 564
	if (r) {
		dm_put_device(ti, dev);
		return r;
	}

	return 0;
}

H
Heinz Mauelshagen 已提交
565
static void disk_dtr(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
566 567
{
	struct log_c *lc = (struct log_c *) log->context;
568

L
Linus Torvalds 已提交
569 570
	dm_put_device(lc->ti, lc->log_dev);
	vfree(lc->disk_header);
571
	dm_io_client_destroy(lc->io_req.client);
572
	destroy_log_context(lc);
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581 582 583 584
}

static int count_bits32(uint32_t *addr, unsigned size)
{
	int count = 0, i;

	for (i = 0; i < size; i++) {
		count += hweight32(*(addr+i));
	}
	return count;
}

J
Jonathan E Brassow 已提交
585 586 587 588 589 590 591 592 593
static void fail_log_device(struct log_c *lc)
{
	if (lc->log_dev_failed)
		return;

	lc->log_dev_failed = 1;
	dm_table_event(lc->ti->table);
}

H
Heinz Mauelshagen 已提交
594
static int disk_resume(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602
{
	int r;
	unsigned i;
	struct log_c *lc = (struct log_c *) log->context;
	size_t size = lc->bitset_uint32_count * sizeof(uint32_t);

	/* read the disk header */
	r = read_header(lc);
J
Jonathan E Brassow 已提交
603
	if (r) {
604
		DMWARN("%s: Failed to read header on dirty region log device",
J
Jonathan E Brassow 已提交
605 606
		       lc->log_dev->name);
		fail_log_device(lc);
607 608 609 610 611 612 613 614
		/*
		 * If the log device cannot be read, we must assume
		 * all regions are out-of-sync.  If we simply return
		 * here, the state will be uninitialized and could
		 * lead us to return 'in-sync' status for regions
		 * that are actually 'out-of-sync'.
		 */
		lc->header.nr_regions = 0;
J
Jonathan E Brassow 已提交
615
	}
L
Linus Torvalds 已提交
616

617
	/* set or clear any new bits -- device has grown */
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625 626
	if (lc->sync == NOSYNC)
		for (i = lc->header.nr_regions; i < lc->region_count; i++)
			/* FIXME: amazingly inefficient */
			log_set_bit(lc, lc->clean_bits, i);
	else
		for (i = lc->header.nr_regions; i < lc->region_count; i++)
			/* FIXME: amazingly inefficient */
			log_clear_bit(lc, lc->clean_bits, i);

627 628 629 630
	/* clear any old bits -- device has shrunk */
	for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
		log_clear_bit(lc, lc->clean_bits, i);

L
Linus Torvalds 已提交
631 632 633
	/* copy clean across to sync */
	memcpy(lc->sync_bits, lc->clean_bits, size);
	lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
634
	lc->sync_search = 0;
L
Linus Torvalds 已提交
635 636 637 638 639

	/* set the correct number of regions in the header */
	lc->header.nr_regions = lc->region_count;

	/* write the new header */
J
Jonathan E Brassow 已提交
640 641
	r = write_header(lc);
	if (r) {
642
		DMWARN("%s: Failed to write header on dirty region log device",
J
Jonathan E Brassow 已提交
643 644 645 646 647
		       lc->log_dev->name);
		fail_log_device(lc);
	}

	return r;
L
Linus Torvalds 已提交
648 649
}

H
Heinz Mauelshagen 已提交
650
static uint32_t core_get_region_size(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
651 652 653 654 655
{
	struct log_c *lc = (struct log_c *) log->context;
	return lc->region_size;
}

H
Heinz Mauelshagen 已提交
656
static int core_resume(struct dm_dirty_log *log)
657 658 659 660 661 662
{
	struct log_c *lc = (struct log_c *) log->context;
	lc->sync_search = 0;
	return 0;
}

H
Heinz Mauelshagen 已提交
663
static int core_is_clean(struct dm_dirty_log *log, region_t region)
L
Linus Torvalds 已提交
664 665 666 667 668
{
	struct log_c *lc = (struct log_c *) log->context;
	return log_test_bit(lc->clean_bits, region);
}

H
Heinz Mauelshagen 已提交
669
static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
L
Linus Torvalds 已提交
670 671 672 673 674
{
	struct log_c *lc = (struct log_c *) log->context;
	return log_test_bit(lc->sync_bits, region);
}

H
Heinz Mauelshagen 已提交
675
static int core_flush(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
676 677 678 679 680
{
	/* no op */
	return 0;
}

H
Heinz Mauelshagen 已提交
681
static int disk_flush(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689
{
	int r;
	struct log_c *lc = (struct log_c *) log->context;

	/* only write if the log has changed */
	if (!lc->touched)
		return 0;

690
	r = write_header(lc);
J
Jonathan E Brassow 已提交
691 692 693
	if (r)
		fail_log_device(lc);
	else
L
Linus Torvalds 已提交
694 695 696 697 698
		lc->touched = 0;

	return r;
}

H
Heinz Mauelshagen 已提交
699
static void core_mark_region(struct dm_dirty_log *log, region_t region)
L
Linus Torvalds 已提交
700 701 702 703 704
{
	struct log_c *lc = (struct log_c *) log->context;
	log_clear_bit(lc, lc->clean_bits, region);
}

H
Heinz Mauelshagen 已提交
705
static void core_clear_region(struct dm_dirty_log *log, region_t region)
L
Linus Torvalds 已提交
706 707 708 709 710
{
	struct log_c *lc = (struct log_c *) log->context;
	log_set_bit(lc, lc->clean_bits, region);
}

H
Heinz Mauelshagen 已提交
711
static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
L
Linus Torvalds 已提交
712 713 714 715 716 717 718
{
	struct log_c *lc = (struct log_c *) log->context;

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

	do {
719 720
		*region = ext2_find_next_zero_bit(
					     (unsigned long *) lc->sync_bits,
L
Linus Torvalds 已提交
721 722 723 724
					     lc->region_count,
					     lc->sync_search);
		lc->sync_search = *region + 1;

725
		if (*region >= lc->region_count)
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733
			return 0;

	} while (log_test_bit(lc->recovering_bits, *region));

	log_set_bit(lc, lc->recovering_bits, *region);
	return 1;
}

H
Heinz Mauelshagen 已提交
734
static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
735
				 int in_sync)
L
Linus Torvalds 已提交
736 737 738 739
{
	struct log_c *lc = (struct log_c *) log->context;

	log_clear_bit(lc, lc->recovering_bits, region);
740
	if (in_sync) {
L
Linus Torvalds 已提交
741 742
		log_set_bit(lc, lc->sync_bits, region);
                lc->sync_count++;
743 744 745 746
        } else if (log_test_bit(lc->sync_bits, region)) {
		lc->sync_count--;
		log_clear_bit(lc, lc->sync_bits, region);
	}
L
Linus Torvalds 已提交
747 748
}

H
Heinz Mauelshagen 已提交
749
static region_t core_get_sync_count(struct dm_dirty_log *log)
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759
{
        struct log_c *lc = (struct log_c *) log->context;

        return lc->sync_count;
}

#define	DMEMIT_SYNC \
	if (lc->sync != DEFAULTSYNC) \
		DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")

H
Heinz Mauelshagen 已提交
760
static int core_status(struct dm_dirty_log *log, status_type_t status,
L
Linus Torvalds 已提交
761 762 763 764 765 766 767
		       char *result, unsigned int maxlen)
{
	int sz = 0;
	struct log_c *lc = log->context;

	switch(status) {
	case STATUSTYPE_INFO:
J
Jonathan E Brassow 已提交
768
		DMEMIT("1 %s", log->type->name);
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779
		break;

	case STATUSTYPE_TABLE:
		DMEMIT("%s %u %u ", log->type->name,
		       lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
		DMEMIT_SYNC;
	}

	return sz;
}

H
Heinz Mauelshagen 已提交
780
static int disk_status(struct dm_dirty_log *log, status_type_t status,
L
Linus Torvalds 已提交
781 782 783 784 785 786 787
		       char *result, unsigned int maxlen)
{
	int sz = 0;
	struct log_c *lc = log->context;

	switch(status) {
	case STATUSTYPE_INFO:
J
Jonathan E Brassow 已提交
788 789
		DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
		       lc->log_dev_failed ? 'D' : 'A');
L
Linus Torvalds 已提交
790 791 792 793
		break;

	case STATUSTYPE_TABLE:
		DMEMIT("%s %u %s %u ", log->type->name,
J
Jonathan E Brassow 已提交
794
		       lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
L
Linus Torvalds 已提交
795 796 797 798 799 800 801
		       lc->region_size);
		DMEMIT_SYNC;
	}

	return sz;
}

H
Heinz Mauelshagen 已提交
802
static struct dm_dirty_log_type _core_type = {
L
Linus Torvalds 已提交
803 804 805 806
	.name = "core",
	.module = THIS_MODULE,
	.ctr = core_ctr,
	.dtr = core_dtr,
807
	.resume = core_resume,
L
Linus Torvalds 已提交
808 809 810 811 812 813 814
	.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,
815
	.set_region_sync = core_set_region_sync,
L
Linus Torvalds 已提交
816 817 818 819
	.get_sync_count = core_get_sync_count,
	.status = core_status,
};

H
Heinz Mauelshagen 已提交
820
static struct dm_dirty_log_type _disk_type = {
L
Linus Torvalds 已提交
821 822 823 824
	.name = "disk",
	.module = THIS_MODULE,
	.ctr = disk_ctr,
	.dtr = disk_dtr,
J
Jonathan Brassow 已提交
825
	.postsuspend = disk_flush,
L
Linus Torvalds 已提交
826 827 828 829 830 831 832 833
	.resume = disk_resume,
	.get_region_size = core_get_region_size,
	.is_clean = core_is_clean,
	.in_sync = core_in_sync,
	.flush = disk_flush,
	.mark_region = core_mark_region,
	.clear_region = core_clear_region,
	.get_resync_work = core_get_resync_work,
834
	.set_region_sync = core_set_region_sync,
L
Linus Torvalds 已提交
835 836 837 838
	.get_sync_count = core_get_sync_count,
	.status = disk_status,
};

839
static int __init dm_dirty_log_init(void)
L
Linus Torvalds 已提交
840 841 842
{
	int r;

H
Heinz Mauelshagen 已提交
843
	r = dm_dirty_log_type_register(&_core_type);
L
Linus Torvalds 已提交
844 845 846
	if (r)
		DMWARN("couldn't register core log");

H
Heinz Mauelshagen 已提交
847
	r = dm_dirty_log_type_register(&_disk_type);
L
Linus Torvalds 已提交
848 849
	if (r) {
		DMWARN("couldn't register disk type");
H
Heinz Mauelshagen 已提交
850
		dm_dirty_log_type_unregister(&_core_type);
L
Linus Torvalds 已提交
851 852 853 854 855
	}

	return r;
}

856
static void __exit dm_dirty_log_exit(void)
L
Linus Torvalds 已提交
857
{
H
Heinz Mauelshagen 已提交
858 859
	dm_dirty_log_type_unregister(&_disk_type);
	dm_dirty_log_type_unregister(&_core_type);
L
Linus Torvalds 已提交
860 861
}

862 863 864 865 866 867
module_init(dm_dirty_log_init);
module_exit(dm_dirty_log_exit);

MODULE_DESCRIPTION(DM_NAME " dirty region log");
MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
MODULE_LICENSE("GPL");