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>
16 #define DM_MSG_PREFIX "dirty region log"
18 static LIST_HEAD(_log_types);
19 static DEFINE_SPINLOCK(_lock);
21 int dm_register_dirty_log_type(struct dirty_log_type *type)
25 list_add(&type->list, &_log_types);
31 int dm_unregister_dirty_log_type(struct dirty_log_type *type)
36 DMWARN("Attempt to unregister a log type that is still in use");
38 list_del(&type->list);
45 static struct dirty_log_type *_get_type(const char *type_name)
47 struct dirty_log_type *type;
50 list_for_each_entry (type, &_log_types, list)
51 if (!strcmp(type_name, type->name)) {
52 if (!type->use_count && !try_module_get(type->module)){
69 * Attempt to retrieve the dirty_log_type by name. If not already
70 * available, attempt to load the appropriate module.
72 * Log modules are named "dm-log-" followed by the 'type_name'.
73 * Modules may contain multiple types.
74 * This function will first try the module "dm-log-<type_name>",
75 * then truncate 'type_name' on the last '-' and try again.
77 * For example, if type_name was "clustered-disk", it would search
78 * 'dm-log-clustered-disk' then 'dm-log-clustered'.
80 * Returns: dirty_log_type* on success, NULL on failure
82 static struct dirty_log_type *get_type(const char *type_name)
84 char *p, *type_name_dup;
85 struct dirty_log_type *type;
87 type = _get_type(type_name);
91 type_name_dup = kstrdup(type_name, GFP_KERNEL);
93 DMWARN("No memory left to attempt log module load for \"%s\"",
98 while (request_module("dm-log-%s", type_name_dup) ||
99 !(type = _get_type(type_name))) {
100 p = strrchr(type_name_dup, '-');
107 DMWARN("Module for logging type \"%s\" not found.", type_name);
109 kfree(type_name_dup);
114 static void put_type(struct dirty_log_type *type)
117 if (!--type->use_count)
118 module_put(type->module);
122 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
123 unsigned int argc, char **argv)
125 struct dirty_log_type *type;
126 struct dirty_log *log;
128 log = kmalloc(sizeof(*log), GFP_KERNEL);
132 type = get_type(type_name);
139 if (type->ctr(log, ti, argc, argv)) {
148 void dm_destroy_dirty_log(struct dirty_log *log)
155 /*-----------------------------------------------------------------
156 * Persistent and core logs share a lot of their implementation.
157 * FIXME: need a reload method to be called from a resume
158 *---------------------------------------------------------------*/
160 * Magic for persistent mirrors: "MiRr"
162 #define MIRROR_MAGIC 0x4D695272
165 * The on-disk version of the metadata.
167 #define MIRROR_DISK_VERSION 2
174 * Simple, incrementing version. no backward
182 struct dm_target *ti;
184 uint32_t region_size;
185 unsigned int region_count;
188 unsigned bitset_uint32_count;
189 uint32_t *clean_bits;
191 uint32_t *recovering_bits; /* FIXME: this seems excessive */
197 DEFAULTSYNC, /* Synchronize if necessary */
198 NOSYNC, /* Devices known to be already in sync */
199 FORCESYNC, /* Force a sync to happen */
202 struct dm_io_request io_req;
208 struct dm_dev *log_dev;
209 struct log_header header;
211 struct dm_io_region header_location;
212 struct log_header *disk_header;
216 * The touched member needs to be updated every time we access
217 * one of the bitsets.
219 static inline int log_test_bit(uint32_t *bs, unsigned bit)
221 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
224 static inline void log_set_bit(struct log_c *l,
225 uint32_t *bs, unsigned bit)
227 ext2_set_bit(bit, (unsigned long *) bs);
231 static inline void log_clear_bit(struct log_c *l,
232 uint32_t *bs, unsigned bit)
234 ext2_clear_bit(bit, (unsigned long *) bs);
238 /*----------------------------------------------------------------
240 *--------------------------------------------------------------*/
241 static void header_to_disk(struct log_header *core, struct log_header *disk)
243 disk->magic = cpu_to_le32(core->magic);
244 disk->version = cpu_to_le32(core->version);
245 disk->nr_regions = cpu_to_le64(core->nr_regions);
248 static void header_from_disk(struct log_header *core, struct log_header *disk)
250 core->magic = le32_to_cpu(disk->magic);
251 core->version = le32_to_cpu(disk->version);
252 core->nr_regions = le64_to_cpu(disk->nr_regions);
255 static int rw_header(struct log_c *lc, int rw)
257 lc->io_req.bi_rw = rw;
258 lc->io_req.mem.ptr.vma = lc->disk_header;
259 lc->io_req.notify.fn = NULL;
261 return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
264 static int read_header(struct log_c *log)
268 r = rw_header(log, READ);
272 header_from_disk(&log->header, log->disk_header);
274 /* New log required? */
275 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
276 log->header.magic = MIRROR_MAGIC;
277 log->header.version = MIRROR_DISK_VERSION;
278 log->header.nr_regions = 0;
281 #ifdef __LITTLE_ENDIAN
282 if (log->header.version == 1)
283 log->header.version = 2;
286 if (log->header.version != MIRROR_DISK_VERSION) {
287 DMWARN("incompatible disk log version");
294 static inline int write_header(struct log_c *log)
296 header_to_disk(&log->header, log->disk_header);
297 return rw_header(log, WRITE);
300 /*----------------------------------------------------------------
301 * core log constructor/destructor
303 * argv contains region_size followed optionally by [no]sync
304 *--------------------------------------------------------------*/
306 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
307 unsigned int argc, char **argv,
310 enum sync sync = DEFAULTSYNC;
313 uint32_t region_size;
314 unsigned int region_count;
315 size_t bitset_size, buf_size;
318 if (argc < 1 || argc > 2) {
319 DMWARN("wrong number of arguments to dirty region log");
324 if (!strcmp(argv[1], "sync"))
326 else if (!strcmp(argv[1], "nosync"))
329 DMWARN("unrecognised sync argument to "
330 "dirty region log: %s", argv[1]);
335 if (sscanf(argv[0], "%u", ®ion_size) != 1) {
336 DMWARN("invalid region size string");
340 region_count = dm_sector_div_up(ti->len, region_size);
342 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
344 DMWARN("couldn't allocate core log");
350 lc->region_size = region_size;
351 lc->region_count = region_count;
355 * Work out how many "unsigned long"s we need to hold the bitset.
357 bitset_size = dm_round_up(region_count,
358 sizeof(*lc->clean_bits) << BYTE_SHIFT);
359 bitset_size >>= BYTE_SHIFT;
361 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
367 lc->clean_bits = vmalloc(bitset_size);
368 if (!lc->clean_bits) {
369 DMWARN("couldn't allocate clean bitset");
373 lc->disk_header = NULL;
376 lc->log_dev_failed = 0;
377 lc->header_location.bdev = lc->log_dev->bdev;
378 lc->header_location.sector = 0;
381 * Buffer holds both header and bitset.
383 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
384 bitset_size, ti->limits.hardsect_size);
385 lc->header_location.count = buf_size >> SECTOR_SHIFT;
386 lc->io_req.mem.type = DM_IO_VMA;
387 lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
389 if (IS_ERR(lc->io_req.client)) {
390 r = PTR_ERR(lc->io_req.client);
391 DMWARN("couldn't allocate disk io client");
396 lc->disk_header = vmalloc(buf_size);
397 if (!lc->disk_header) {
398 DMWARN("couldn't allocate disk log buffer");
403 lc->clean_bits = (void *)lc->disk_header +
404 (LOG_OFFSET << SECTOR_SHIFT);
407 memset(lc->clean_bits, -1, bitset_size);
409 lc->sync_bits = vmalloc(bitset_size);
410 if (!lc->sync_bits) {
411 DMWARN("couldn't allocate sync bitset");
413 vfree(lc->clean_bits);
414 vfree(lc->disk_header);
418 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
419 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
421 lc->recovering_bits = vmalloc(bitset_size);
422 if (!lc->recovering_bits) {
423 DMWARN("couldn't allocate sync bitset");
424 vfree(lc->sync_bits);
426 vfree(lc->clean_bits);
427 vfree(lc->disk_header);
431 memset(lc->recovering_bits, 0, bitset_size);
438 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
439 unsigned int argc, char **argv)
441 return create_log_context(log, ti, argc, argv, NULL);
444 static void destroy_log_context(struct log_c *lc)
446 vfree(lc->sync_bits);
447 vfree(lc->recovering_bits);
451 static void core_dtr(struct dirty_log *log)
453 struct log_c *lc = (struct log_c *) log->context;
455 vfree(lc->clean_bits);
456 destroy_log_context(lc);
459 /*----------------------------------------------------------------
460 * disk log constructor/destructor
462 * argv contains log_device region_size followed optionally by [no]sync
463 *--------------------------------------------------------------*/
464 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
465 unsigned int argc, char **argv)
470 if (argc < 2 || argc > 3) {
471 DMWARN("wrong number of arguments to disk dirty region log");
475 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
476 FMODE_READ | FMODE_WRITE, &dev);
480 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
482 dm_put_device(ti, dev);
489 static void disk_dtr(struct dirty_log *log)
491 struct log_c *lc = (struct log_c *) log->context;
493 dm_put_device(lc->ti, lc->log_dev);
494 vfree(lc->disk_header);
495 dm_io_client_destroy(lc->io_req.client);
496 destroy_log_context(lc);
499 static int count_bits32(uint32_t *addr, unsigned size)
503 for (i = 0; i < size; i++) {
504 count += hweight32(*(addr+i));
509 static void fail_log_device(struct log_c *lc)
511 if (lc->log_dev_failed)
514 lc->log_dev_failed = 1;
515 dm_table_event(lc->ti->table);
518 static int disk_resume(struct dirty_log *log)
522 struct log_c *lc = (struct log_c *) log->context;
523 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
525 /* read the disk header */
528 DMWARN("%s: Failed to read header on dirty region log device",
532 * If the log device cannot be read, we must assume
533 * all regions are out-of-sync. If we simply return
534 * here, the state will be uninitialized and could
535 * lead us to return 'in-sync' status for regions
536 * that are actually 'out-of-sync'.
538 lc->header.nr_regions = 0;
541 /* set or clear any new bits -- device has grown */
542 if (lc->sync == NOSYNC)
543 for (i = lc->header.nr_regions; i < lc->region_count; i++)
544 /* FIXME: amazingly inefficient */
545 log_set_bit(lc, lc->clean_bits, i);
547 for (i = lc->header.nr_regions; i < lc->region_count; i++)
548 /* FIXME: amazingly inefficient */
549 log_clear_bit(lc, lc->clean_bits, i);
551 /* clear any old bits -- device has shrunk */
552 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
553 log_clear_bit(lc, lc->clean_bits, i);
555 /* copy clean across to sync */
556 memcpy(lc->sync_bits, lc->clean_bits, size);
557 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
560 /* set the correct number of regions in the header */
561 lc->header.nr_regions = lc->region_count;
563 /* write the new header */
564 r = write_header(lc);
566 DMWARN("%s: Failed to write header on dirty region log device",
574 static uint32_t core_get_region_size(struct dirty_log *log)
576 struct log_c *lc = (struct log_c *) log->context;
577 return lc->region_size;
580 static int core_resume(struct dirty_log *log)
582 struct log_c *lc = (struct log_c *) log->context;
587 static int core_is_clean(struct dirty_log *log, region_t region)
589 struct log_c *lc = (struct log_c *) log->context;
590 return log_test_bit(lc->clean_bits, region);
593 static int core_in_sync(struct dirty_log *log, region_t region, int block)
595 struct log_c *lc = (struct log_c *) log->context;
596 return log_test_bit(lc->sync_bits, region);
599 static int core_flush(struct dirty_log *log)
605 static int disk_flush(struct dirty_log *log)
608 struct log_c *lc = (struct log_c *) log->context;
610 /* only write if the log has changed */
614 r = write_header(lc);
623 static void core_mark_region(struct dirty_log *log, region_t region)
625 struct log_c *lc = (struct log_c *) log->context;
626 log_clear_bit(lc, lc->clean_bits, region);
629 static void core_clear_region(struct dirty_log *log, region_t region)
631 struct log_c *lc = (struct log_c *) log->context;
632 log_set_bit(lc, lc->clean_bits, region);
635 static int core_get_resync_work(struct dirty_log *log, region_t *region)
637 struct log_c *lc = (struct log_c *) log->context;
639 if (lc->sync_search >= lc->region_count)
643 *region = ext2_find_next_zero_bit(
644 (unsigned long *) lc->sync_bits,
647 lc->sync_search = *region + 1;
649 if (*region >= lc->region_count)
652 } while (log_test_bit(lc->recovering_bits, *region));
654 log_set_bit(lc, lc->recovering_bits, *region);
658 static void core_set_region_sync(struct dirty_log *log, region_t region,
661 struct log_c *lc = (struct log_c *) log->context;
663 log_clear_bit(lc, lc->recovering_bits, region);
665 log_set_bit(lc, lc->sync_bits, region);
667 } else if (log_test_bit(lc->sync_bits, region)) {
669 log_clear_bit(lc, lc->sync_bits, region);
673 static region_t core_get_sync_count(struct dirty_log *log)
675 struct log_c *lc = (struct log_c *) log->context;
677 return lc->sync_count;
680 #define DMEMIT_SYNC \
681 if (lc->sync != DEFAULTSYNC) \
682 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
684 static int core_status(struct dirty_log *log, status_type_t status,
685 char *result, unsigned int maxlen)
688 struct log_c *lc = log->context;
691 case STATUSTYPE_INFO:
692 DMEMIT("1 %s", log->type->name);
695 case STATUSTYPE_TABLE:
696 DMEMIT("%s %u %u ", log->type->name,
697 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
704 static int disk_status(struct dirty_log *log, status_type_t status,
705 char *result, unsigned int maxlen)
708 struct log_c *lc = log->context;
711 case STATUSTYPE_INFO:
712 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
713 lc->log_dev_failed ? 'D' : 'A');
716 case STATUSTYPE_TABLE:
717 DMEMIT("%s %u %s %u ", log->type->name,
718 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
726 static struct dirty_log_type _core_type = {
728 .module = THIS_MODULE,
731 .resume = core_resume,
732 .get_region_size = core_get_region_size,
733 .is_clean = core_is_clean,
734 .in_sync = core_in_sync,
736 .mark_region = core_mark_region,
737 .clear_region = core_clear_region,
738 .get_resync_work = core_get_resync_work,
739 .set_region_sync = core_set_region_sync,
740 .get_sync_count = core_get_sync_count,
741 .status = core_status,
744 static struct dirty_log_type _disk_type = {
746 .module = THIS_MODULE,
749 .postsuspend = disk_flush,
750 .resume = disk_resume,
751 .get_region_size = core_get_region_size,
752 .is_clean = core_is_clean,
753 .in_sync = core_in_sync,
755 .mark_region = core_mark_region,
756 .clear_region = core_clear_region,
757 .get_resync_work = core_get_resync_work,
758 .set_region_sync = core_set_region_sync,
759 .get_sync_count = core_get_sync_count,
760 .status = disk_status,
763 int __init dm_dirty_log_init(void)
767 r = dm_register_dirty_log_type(&_core_type);
769 DMWARN("couldn't register core log");
771 r = dm_register_dirty_log_type(&_disk_type);
773 DMWARN("couldn't register disk type");
774 dm_unregister_dirty_log_type(&_core_type);
780 void __exit dm_dirty_log_exit(void)
782 dm_unregister_dirty_log_type(&_disk_type);
783 dm_unregister_dirty_log_type(&_core_type);
786 EXPORT_SYMBOL(dm_register_dirty_log_type);
787 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
788 EXPORT_SYMBOL(dm_create_dirty_log);
789 EXPORT_SYMBOL(dm_destroy_dirty_log);
791 module_init(dm_dirty_log_init);
792 module_exit(dm_dirty_log_exit);
794 MODULE_DESCRIPTION(DM_NAME " dirty region log");
795 MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
796 MODULE_LICENSE("GPL");