2 * Copyright (C) 2003 Sistina Software
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This file is released under the LGPL.
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
17 #define DM_MSG_PREFIX "dirty region log"
19 static LIST_HEAD(_log_types);
20 static DEFINE_SPINLOCK(_lock);
22 int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
26 list_add(&type->list, &_log_types);
31 EXPORT_SYMBOL(dm_dirty_log_type_register);
33 int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
38 DMWARN("Attempt to unregister a log type that is still in use");
40 list_del(&type->list);
46 EXPORT_SYMBOL(dm_dirty_log_type_unregister);
48 static struct dm_dirty_log_type *_get_type(const char *type_name)
50 struct dm_dirty_log_type *type;
53 list_for_each_entry (type, &_log_types, list)
54 if (!strcmp(type_name, type->name)) {
55 if (!type->use_count && !try_module_get(type->module)){
72 * Attempt to retrieve the dirty_log_type by name. If not already
73 * available, attempt to load the appropriate module.
75 * Log modules are named "dm-log-" followed by the 'type_name'.
76 * Modules may contain multiple types.
77 * This function will first try the module "dm-log-<type_name>",
78 * then truncate 'type_name' on the last '-' and try again.
80 * For example, if type_name was "clustered-disk", it would search
81 * 'dm-log-clustered-disk' then 'dm-log-clustered'.
83 * Returns: dirty_log_type* on success, NULL on failure
85 static struct dm_dirty_log_type *get_type(const char *type_name)
87 char *p, *type_name_dup;
88 struct dm_dirty_log_type *type;
90 type = _get_type(type_name);
94 type_name_dup = kstrdup(type_name, GFP_KERNEL);
96 DMWARN("No memory left to attempt log module load for \"%s\"",
101 while (request_module("dm-log-%s", type_name_dup) ||
102 !(type = _get_type(type_name))) {
103 p = strrchr(type_name_dup, '-');
110 DMWARN("Module for logging type \"%s\" not found.", type_name);
112 kfree(type_name_dup);
117 static void put_type(struct dm_dirty_log_type *type)
120 if (!--type->use_count)
121 module_put(type->module);
125 struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
126 struct dm_target *ti,
127 unsigned int argc, char **argv)
129 struct dm_dirty_log_type *type;
130 struct dm_dirty_log *log;
132 log = kmalloc(sizeof(*log), GFP_KERNEL);
136 type = get_type(type_name);
143 if (type->ctr(log, ti, argc, argv)) {
151 EXPORT_SYMBOL(dm_dirty_log_create);
153 void dm_dirty_log_destroy(struct dm_dirty_log *log)
159 EXPORT_SYMBOL(dm_dirty_log_destroy);
161 /*-----------------------------------------------------------------
162 * Persistent and core logs share a lot of their implementation.
163 * FIXME: need a reload method to be called from a resume
164 *---------------------------------------------------------------*/
166 * Magic for persistent mirrors: "MiRr"
168 #define MIRROR_MAGIC 0x4D695272
171 * The on-disk version of the metadata.
173 #define MIRROR_DISK_VERSION 2
180 * Simple, incrementing version. no backward
188 struct dm_target *ti;
190 uint32_t region_size;
191 unsigned int region_count;
194 unsigned bitset_uint32_count;
195 uint32_t *clean_bits;
197 uint32_t *recovering_bits; /* FIXME: this seems excessive */
203 DEFAULTSYNC, /* Synchronize if necessary */
204 NOSYNC, /* Devices known to be already in sync */
205 FORCESYNC, /* Force a sync to happen */
208 struct dm_io_request io_req;
214 struct dm_dev *log_dev;
215 struct log_header header;
217 struct dm_io_region header_location;
218 struct log_header *disk_header;
222 * The touched member needs to be updated every time we access
223 * one of the bitsets.
225 static inline int log_test_bit(uint32_t *bs, unsigned bit)
227 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
230 static inline void log_set_bit(struct log_c *l,
231 uint32_t *bs, unsigned bit)
233 ext2_set_bit(bit, (unsigned long *) bs);
237 static inline void log_clear_bit(struct log_c *l,
238 uint32_t *bs, unsigned bit)
240 ext2_clear_bit(bit, (unsigned long *) bs);
244 /*----------------------------------------------------------------
246 *--------------------------------------------------------------*/
247 static void header_to_disk(struct log_header *core, struct log_header *disk)
249 disk->magic = cpu_to_le32(core->magic);
250 disk->version = cpu_to_le32(core->version);
251 disk->nr_regions = cpu_to_le64(core->nr_regions);
254 static void header_from_disk(struct log_header *core, struct log_header *disk)
256 core->magic = le32_to_cpu(disk->magic);
257 core->version = le32_to_cpu(disk->version);
258 core->nr_regions = le64_to_cpu(disk->nr_regions);
261 static int rw_header(struct log_c *lc, int rw)
263 lc->io_req.bi_rw = rw;
264 lc->io_req.mem.ptr.vma = lc->disk_header;
265 lc->io_req.notify.fn = NULL;
267 return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
270 static int read_header(struct log_c *log)
274 r = rw_header(log, READ);
278 header_from_disk(&log->header, log->disk_header);
280 /* New log required? */
281 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
282 log->header.magic = MIRROR_MAGIC;
283 log->header.version = MIRROR_DISK_VERSION;
284 log->header.nr_regions = 0;
287 #ifdef __LITTLE_ENDIAN
288 if (log->header.version == 1)
289 log->header.version = 2;
292 if (log->header.version != MIRROR_DISK_VERSION) {
293 DMWARN("incompatible disk log version");
300 static inline int write_header(struct log_c *log)
302 header_to_disk(&log->header, log->disk_header);
303 return rw_header(log, WRITE);
306 /*----------------------------------------------------------------
307 * core log constructor/destructor
309 * argv contains region_size followed optionally by [no]sync
310 *--------------------------------------------------------------*/
312 static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
313 unsigned int argc, char **argv,
316 enum sync sync = DEFAULTSYNC;
319 uint32_t region_size;
320 unsigned int region_count;
321 size_t bitset_size, buf_size;
324 if (argc < 1 || argc > 2) {
325 DMWARN("wrong number of arguments to dirty region log");
330 if (!strcmp(argv[1], "sync"))
332 else if (!strcmp(argv[1], "nosync"))
335 DMWARN("unrecognised sync argument to "
336 "dirty region log: %s", argv[1]);
341 if (sscanf(argv[0], "%u", ®ion_size) != 1) {
342 DMWARN("invalid region size string");
346 region_count = dm_sector_div_up(ti->len, region_size);
348 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
350 DMWARN("couldn't allocate core log");
356 lc->region_size = region_size;
357 lc->region_count = region_count;
361 * Work out how many "unsigned long"s we need to hold the bitset.
363 bitset_size = dm_round_up(region_count,
364 sizeof(*lc->clean_bits) << BYTE_SHIFT);
365 bitset_size >>= BYTE_SHIFT;
367 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
373 lc->clean_bits = vmalloc(bitset_size);
374 if (!lc->clean_bits) {
375 DMWARN("couldn't allocate clean bitset");
379 lc->disk_header = NULL;
382 lc->log_dev_failed = 0;
383 lc->header_location.bdev = lc->log_dev->bdev;
384 lc->header_location.sector = 0;
387 * Buffer holds both header and bitset.
389 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
390 bitset_size, ti->limits.hardsect_size);
391 lc->header_location.count = buf_size >> SECTOR_SHIFT;
392 lc->io_req.mem.type = DM_IO_VMA;
393 lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
395 if (IS_ERR(lc->io_req.client)) {
396 r = PTR_ERR(lc->io_req.client);
397 DMWARN("couldn't allocate disk io client");
402 lc->disk_header = vmalloc(buf_size);
403 if (!lc->disk_header) {
404 DMWARN("couldn't allocate disk log buffer");
409 lc->clean_bits = (void *)lc->disk_header +
410 (LOG_OFFSET << SECTOR_SHIFT);
413 memset(lc->clean_bits, -1, bitset_size);
415 lc->sync_bits = vmalloc(bitset_size);
416 if (!lc->sync_bits) {
417 DMWARN("couldn't allocate sync bitset");
419 vfree(lc->clean_bits);
420 vfree(lc->disk_header);
424 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
425 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
427 lc->recovering_bits = vmalloc(bitset_size);
428 if (!lc->recovering_bits) {
429 DMWARN("couldn't allocate sync bitset");
430 vfree(lc->sync_bits);
432 vfree(lc->clean_bits);
433 vfree(lc->disk_header);
437 memset(lc->recovering_bits, 0, bitset_size);
444 static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
445 unsigned int argc, char **argv)
447 return create_log_context(log, ti, argc, argv, NULL);
450 static void destroy_log_context(struct log_c *lc)
452 vfree(lc->sync_bits);
453 vfree(lc->recovering_bits);
457 static void core_dtr(struct dm_dirty_log *log)
459 struct log_c *lc = (struct log_c *) log->context;
461 vfree(lc->clean_bits);
462 destroy_log_context(lc);
465 /*----------------------------------------------------------------
466 * disk log constructor/destructor
468 * argv contains log_device region_size followed optionally by [no]sync
469 *--------------------------------------------------------------*/
470 static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
471 unsigned int argc, char **argv)
476 if (argc < 2 || argc > 3) {
477 DMWARN("wrong number of arguments to disk dirty region log");
481 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
482 FMODE_READ | FMODE_WRITE, &dev);
486 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
488 dm_put_device(ti, dev);
495 static void disk_dtr(struct dm_dirty_log *log)
497 struct log_c *lc = (struct log_c *) log->context;
499 dm_put_device(lc->ti, lc->log_dev);
500 vfree(lc->disk_header);
501 dm_io_client_destroy(lc->io_req.client);
502 destroy_log_context(lc);
505 static int count_bits32(uint32_t *addr, unsigned size)
509 for (i = 0; i < size; i++) {
510 count += hweight32(*(addr+i));
515 static void fail_log_device(struct log_c *lc)
517 if (lc->log_dev_failed)
520 lc->log_dev_failed = 1;
521 dm_table_event(lc->ti->table);
524 static int disk_resume(struct dm_dirty_log *log)
528 struct log_c *lc = (struct log_c *) log->context;
529 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
531 /* read the disk header */
534 DMWARN("%s: Failed to read header on dirty region log device",
538 * If the log device cannot be read, we must assume
539 * all regions are out-of-sync. If we simply return
540 * here, the state will be uninitialized and could
541 * lead us to return 'in-sync' status for regions
542 * that are actually 'out-of-sync'.
544 lc->header.nr_regions = 0;
547 /* set or clear any new bits -- device has grown */
548 if (lc->sync == NOSYNC)
549 for (i = lc->header.nr_regions; i < lc->region_count; i++)
550 /* FIXME: amazingly inefficient */
551 log_set_bit(lc, lc->clean_bits, i);
553 for (i = lc->header.nr_regions; i < lc->region_count; i++)
554 /* FIXME: amazingly inefficient */
555 log_clear_bit(lc, lc->clean_bits, i);
557 /* clear any old bits -- device has shrunk */
558 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
559 log_clear_bit(lc, lc->clean_bits, i);
561 /* copy clean across to sync */
562 memcpy(lc->sync_bits, lc->clean_bits, size);
563 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
566 /* set the correct number of regions in the header */
567 lc->header.nr_regions = lc->region_count;
569 /* write the new header */
570 r = write_header(lc);
572 DMWARN("%s: Failed to write header on dirty region log device",
580 static uint32_t core_get_region_size(struct dm_dirty_log *log)
582 struct log_c *lc = (struct log_c *) log->context;
583 return lc->region_size;
586 static int core_resume(struct dm_dirty_log *log)
588 struct log_c *lc = (struct log_c *) log->context;
593 static int core_is_clean(struct dm_dirty_log *log, region_t region)
595 struct log_c *lc = (struct log_c *) log->context;
596 return log_test_bit(lc->clean_bits, region);
599 static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
601 struct log_c *lc = (struct log_c *) log->context;
602 return log_test_bit(lc->sync_bits, region);
605 static int core_flush(struct dm_dirty_log *log)
611 static int disk_flush(struct dm_dirty_log *log)
614 struct log_c *lc = (struct log_c *) log->context;
616 /* only write if the log has changed */
620 r = write_header(lc);
629 static void core_mark_region(struct dm_dirty_log *log, region_t region)
631 struct log_c *lc = (struct log_c *) log->context;
632 log_clear_bit(lc, lc->clean_bits, region);
635 static void core_clear_region(struct dm_dirty_log *log, region_t region)
637 struct log_c *lc = (struct log_c *) log->context;
638 log_set_bit(lc, lc->clean_bits, region);
641 static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
643 struct log_c *lc = (struct log_c *) log->context;
645 if (lc->sync_search >= lc->region_count)
649 *region = ext2_find_next_zero_bit(
650 (unsigned long *) lc->sync_bits,
653 lc->sync_search = *region + 1;
655 if (*region >= lc->region_count)
658 } while (log_test_bit(lc->recovering_bits, *region));
660 log_set_bit(lc, lc->recovering_bits, *region);
664 static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
667 struct log_c *lc = (struct log_c *) log->context;
669 log_clear_bit(lc, lc->recovering_bits, region);
671 log_set_bit(lc, lc->sync_bits, region);
673 } else if (log_test_bit(lc->sync_bits, region)) {
675 log_clear_bit(lc, lc->sync_bits, region);
679 static region_t core_get_sync_count(struct dm_dirty_log *log)
681 struct log_c *lc = (struct log_c *) log->context;
683 return lc->sync_count;
686 #define DMEMIT_SYNC \
687 if (lc->sync != DEFAULTSYNC) \
688 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
690 static int core_status(struct dm_dirty_log *log, status_type_t status,
691 char *result, unsigned int maxlen)
694 struct log_c *lc = log->context;
697 case STATUSTYPE_INFO:
698 DMEMIT("1 %s", log->type->name);
701 case STATUSTYPE_TABLE:
702 DMEMIT("%s %u %u ", log->type->name,
703 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
710 static int disk_status(struct dm_dirty_log *log, status_type_t status,
711 char *result, unsigned int maxlen)
714 struct log_c *lc = log->context;
717 case STATUSTYPE_INFO:
718 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
719 lc->log_dev_failed ? 'D' : 'A');
722 case STATUSTYPE_TABLE:
723 DMEMIT("%s %u %s %u ", log->type->name,
724 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
732 static struct dm_dirty_log_type _core_type = {
734 .module = THIS_MODULE,
737 .resume = core_resume,
738 .get_region_size = core_get_region_size,
739 .is_clean = core_is_clean,
740 .in_sync = core_in_sync,
742 .mark_region = core_mark_region,
743 .clear_region = core_clear_region,
744 .get_resync_work = core_get_resync_work,
745 .set_region_sync = core_set_region_sync,
746 .get_sync_count = core_get_sync_count,
747 .status = core_status,
750 static struct dm_dirty_log_type _disk_type = {
752 .module = THIS_MODULE,
755 .postsuspend = disk_flush,
756 .resume = disk_resume,
757 .get_region_size = core_get_region_size,
758 .is_clean = core_is_clean,
759 .in_sync = core_in_sync,
761 .mark_region = core_mark_region,
762 .clear_region = core_clear_region,
763 .get_resync_work = core_get_resync_work,
764 .set_region_sync = core_set_region_sync,
765 .get_sync_count = core_get_sync_count,
766 .status = disk_status,
769 int __init dm_dirty_log_init(void)
773 r = dm_dirty_log_type_register(&_core_type);
775 DMWARN("couldn't register core log");
777 r = dm_dirty_log_type_register(&_disk_type);
779 DMWARN("couldn't register disk type");
780 dm_dirty_log_type_unregister(&_core_type);
786 void __exit dm_dirty_log_exit(void)
788 dm_dirty_log_type_unregister(&_disk_type);
789 dm_dirty_log_type_unregister(&_core_type);
792 module_init(dm_dirty_log_init);
793 module_exit(dm_dirty_log_exit);
795 MODULE_DESCRIPTION(DM_NAME " dirty region log");
796 MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
797 MODULE_LICENSE("GPL");