2 * Copyright (C) 2003 Sistina Software
4 * This file is released under the LGPL.
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/vmalloc.h>
15 #define DM_MSG_PREFIX "mirror log"
17 static LIST_HEAD(_log_types);
18 static DEFINE_SPINLOCK(_lock);
20 int dm_register_dirty_log_type(struct dirty_log_type *type)
24 list_add(&type->list, &_log_types);
30 int dm_unregister_dirty_log_type(struct dirty_log_type *type)
35 DMWARN("Unregister failed: log type '%s' 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)){
65 static void put_type(struct dirty_log_type *type)
68 if (!--type->use_count)
69 module_put(type->module);
73 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
74 unsigned int argc, char **argv)
76 struct dirty_log_type *type;
77 struct dirty_log *log;
79 log = kmalloc(sizeof(*log), GFP_KERNEL);
83 type = get_type(type_name);
90 if (type->ctr(log, ti, argc, argv)) {
99 void dm_destroy_dirty_log(struct dirty_log *log)
106 /*-----------------------------------------------------------------
107 * Persistent and core logs share a lot of their implementation.
108 * FIXME: need a reload method to be called from a resume
109 *---------------------------------------------------------------*/
111 * Magic for persistent mirrors: "MiRr"
113 #define MIRROR_MAGIC 0x4D695272
116 * The on-disk version of the metadata.
118 #define MIRROR_DISK_VERSION 2
125 * Simple, incrementing version. no backward
133 struct dm_target *ti;
135 uint32_t region_size;
136 unsigned int region_count;
139 unsigned bitset_uint32_count;
140 uint32_t *clean_bits;
142 uint32_t *recovering_bits;
148 DEFAULTSYNC, /* Synchronize if necessary */
149 NOSYNC, /* Devices known to be already in sync */
150 FORCESYNC, /* Force a sync to happen */
153 int failure_response;
159 struct dm_dev *log_dev;
160 struct log_header header;
162 struct io_region header_location;
163 struct log_header *disk_header;
167 * The touched member needs to be updated every time we access
168 * one of the bitsets.
170 static inline int log_test_bit(uint32_t *bs, unsigned bit)
172 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
175 static inline void log_set_bit(struct log_c *l,
176 uint32_t *bs, unsigned bit)
178 ext2_set_bit(bit, (unsigned long *) bs);
182 static inline void log_clear_bit(struct log_c *l,
183 uint32_t *bs, unsigned bit)
185 ext2_clear_bit(bit, (unsigned long *) bs);
189 /*----------------------------------------------------------------
191 *--------------------------------------------------------------*/
192 static void header_to_disk(struct log_header *core, struct log_header *disk)
194 disk->magic = cpu_to_le32(core->magic);
195 disk->version = cpu_to_le32(core->version);
196 disk->nr_regions = cpu_to_le64(core->nr_regions);
199 static void header_from_disk(struct log_header *core, struct log_header *disk)
201 core->magic = le32_to_cpu(disk->magic);
202 core->version = le32_to_cpu(disk->version);
203 core->nr_regions = le64_to_cpu(disk->nr_regions);
206 static int read_header(struct log_c *log)
211 r = dm_io_sync_vm(1, &log->header_location, READ,
212 log->disk_header, &ebits);
216 header_from_disk(&log->header, log->disk_header);
218 /* New log required? */
219 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
220 log->header.magic = MIRROR_MAGIC;
221 log->header.version = MIRROR_DISK_VERSION;
222 log->header.nr_regions = 0;
225 #ifdef __LITTLE_ENDIAN
226 if (log->header.version == 1)
227 log->header.version = 2;
230 if (log->header.version != MIRROR_DISK_VERSION) {
231 DMWARN("incompatible disk log version");
238 static inline int write_header(struct log_c *log)
242 header_to_disk(&log->header, log->disk_header);
243 return dm_io_sync_vm(1, &log->header_location, WRITE,
244 log->disk_header, &ebits);
247 /*----------------------------------------------------------------
248 * core log constructor/destructor
250 * argv contains 1 - 3 arguments:
251 * <region_size> [[no]sync] [block_on_error]
252 *--------------------------------------------------------------*/
254 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
255 unsigned int argc, char **argv,
259 enum sync sync = DEFAULTSYNC;
260 int failure_response = DMLOG_IOERR_IGNORE;
262 uint32_t region_size;
263 unsigned int region_count;
264 size_t bitset_size, buf_size;
266 if (argc < 1 || argc > 3) {
267 DMWARN("wrong number of arguments to mirror log");
271 for (i = 1; i < argc; i++) {
272 if (!strcmp(argv[i], "sync"))
274 else if (!strcmp(argv[i], "nosync"))
276 else if (!strcmp(argv[i], "block_on_error"))
277 failure_response = DMLOG_IOERR_BLOCK;
279 DMWARN("unrecognised sync argument to mirror log: %s",
285 if (sscanf(argv[0], "%u", ®ion_size) != 1) {
286 DMWARN("invalid region size string");
290 region_count = dm_sector_div_up(ti->len, region_size);
292 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
294 DMWARN("couldn't allocate core log");
300 lc->region_size = region_size;
301 lc->region_count = region_count;
303 lc->failure_response = failure_response;
306 * Work out how many "unsigned long"s we need to hold the bitset.
308 bitset_size = dm_round_up(region_count,
309 sizeof(*lc->clean_bits) << BYTE_SHIFT);
310 bitset_size >>= BYTE_SHIFT;
312 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
318 lc->clean_bits = vmalloc(bitset_size);
319 if (!lc->clean_bits) {
320 DMWARN("couldn't allocate clean bitset");
324 lc->disk_header = NULL;
327 lc->log_dev_failed = 0;
328 lc->header_location.bdev = lc->log_dev->bdev;
329 lc->header_location.sector = 0;
332 * Buffer holds both header and bitset.
334 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
335 bitset_size, ti->limits.hardsect_size);
336 lc->header_location.count = buf_size >> SECTOR_SHIFT;
338 lc->disk_header = vmalloc(buf_size);
339 if (!lc->disk_header) {
340 DMWARN("couldn't allocate disk log buffer");
345 lc->clean_bits = (void *)lc->disk_header +
346 (LOG_OFFSET << SECTOR_SHIFT);
349 memset(lc->clean_bits, -1, bitset_size);
351 lc->sync_bits = vmalloc(bitset_size);
352 if (!lc->sync_bits) {
353 DMWARN("couldn't allocate sync bitset");
355 vfree(lc->clean_bits);
356 vfree(lc->disk_header);
360 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
361 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
363 lc->recovering_bits = vmalloc(bitset_size);
364 if (!lc->recovering_bits) {
365 DMWARN("couldn't allocate recovering bitset");
366 vfree(lc->sync_bits);
368 vfree(lc->clean_bits);
369 vfree(lc->disk_header);
373 memset(lc->recovering_bits, 0, bitset_size);
380 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
381 unsigned int argc, char **argv)
383 return create_log_context(log, ti, argc, argv, NULL);
386 static void destroy_log_context(struct log_c *lc)
388 vfree(lc->sync_bits);
389 vfree(lc->recovering_bits);
393 static void core_dtr(struct dirty_log *log)
395 struct log_c *lc = (struct log_c *) log->context;
397 vfree(lc->clean_bits);
398 destroy_log_context(lc);
401 /*----------------------------------------------------------------
402 * disk log constructor/destructor
404 * argv contains 2 - 4 arguments:
405 * <log_device> <region_size> [[no]sync] [block_on_error]
406 *--------------------------------------------------------------*/
407 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
408 unsigned int argc, char **argv)
413 if (argc < 2 || argc > 4) {
414 DMWARN("wrong number of arguments to disk mirror log");
418 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
419 FMODE_READ | FMODE_WRITE, &dev);
423 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
425 dm_put_device(ti, dev);
432 static void disk_dtr(struct dirty_log *log)
434 struct log_c *lc = (struct log_c *) log->context;
436 dm_put_device(lc->ti, lc->log_dev);
437 vfree(lc->disk_header);
438 destroy_log_context(lc);
441 static int count_bits32(uint32_t *addr, unsigned size)
445 for (i = 0; i < size; i++) {
446 count += hweight32(*(addr+i));
451 static void fail_log_device(struct log_c *lc)
453 if (lc->log_dev_failed)
456 lc->log_dev_failed = 1;
457 if (lc->failure_response == DMLOG_IOERR_BLOCK)
458 dm_table_event(lc->ti->table);
461 static void restore_log_device(struct log_c *lc)
463 lc->log_dev_failed = 0;
466 static int disk_resume(struct dirty_log *log)
470 struct log_c *lc = (struct log_c *) log->context;
471 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
474 * Read the disk header, but only if we know it is good.
475 * Assume the worst in the event of failure.
477 if (!lc->log_dev_failed && read_header(lc)) {
478 DMWARN("Failed to read header on mirror log device, %s",
481 lc->header.nr_regions = 0;
484 /* set or clear any new bits -- device has grown */
485 if (lc->sync == NOSYNC)
486 for (i = lc->header.nr_regions; i < lc->region_count; i++)
487 /* FIXME: amazingly inefficient */
488 log_set_bit(lc, lc->clean_bits, i);
490 for (i = lc->header.nr_regions; i < lc->region_count; i++)
491 /* FIXME: amazingly inefficient */
492 log_clear_bit(lc, lc->clean_bits, i);
494 /* clear any old bits -- device has shrunk */
495 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
496 log_clear_bit(lc, lc->clean_bits, i);
498 /* copy clean across to sync */
499 memcpy(lc->sync_bits, lc->clean_bits, size);
500 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
503 /* set the correct number of regions in the header */
504 lc->header.nr_regions = lc->region_count;
506 /* write the new header */
507 r = write_header(lc);
509 DMWARN("Failed to write header on mirror log device, %s",
513 restore_log_device(lc);
518 static uint32_t core_get_region_size(struct dirty_log *log)
520 struct log_c *lc = (struct log_c *) log->context;
521 return lc->region_size;
524 static int core_resume(struct dirty_log *log)
526 struct log_c *lc = (struct log_c *) log->context;
531 static int core_is_clean(struct dirty_log *log, region_t region)
533 struct log_c *lc = (struct log_c *) log->context;
534 return log_test_bit(lc->clean_bits, region);
537 static int core_in_sync(struct dirty_log *log, region_t region, int block)
539 struct log_c *lc = (struct log_c *) log->context;
540 return log_test_bit(lc->sync_bits, region);
543 static int core_flush(struct dirty_log *log)
549 static int disk_presuspend(struct dirty_log *log)
554 static int disk_flush(struct dirty_log *log)
557 struct log_c *lc = (struct log_c *) log->context;
559 /* only write if the log has changed */
563 r = write_header(lc);
568 restore_log_device(lc);
573 static void core_mark_region(struct dirty_log *log, region_t region)
575 struct log_c *lc = (struct log_c *) log->context;
576 log_clear_bit(lc, lc->clean_bits, region);
579 static void core_clear_region(struct dirty_log *log, region_t region)
581 struct log_c *lc = (struct log_c *) log->context;
582 log_set_bit(lc, lc->clean_bits, region);
585 static int core_get_resync_work(struct dirty_log *log, region_t *region)
587 struct log_c *lc = (struct log_c *) log->context;
589 if (lc->sync_search >= lc->region_count)
593 *region = ext2_find_next_zero_bit(
594 (unsigned long *) lc->sync_bits,
597 lc->sync_search = *region + 1;
599 if (*region >= lc->region_count)
602 } while (log_test_bit(lc->recovering_bits, *region));
604 log_set_bit(lc, lc->recovering_bits, *region);
608 static void core_set_region_sync(struct dirty_log *log, region_t region,
611 struct log_c *lc = (struct log_c *) log->context;
613 log_clear_bit(lc, lc->recovering_bits, region);
615 log_set_bit(lc, lc->sync_bits, region);
617 } else if (log_test_bit(lc->sync_bits, region)) {
619 log_clear_bit(lc, lc->sync_bits, region);
623 static region_t core_get_sync_count(struct dirty_log *log)
625 struct log_c *lc = (struct log_c *) log->context;
627 return lc->sync_count;
630 #define DMEMIT_SYNC \
631 if (lc->sync != DEFAULTSYNC) \
632 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
634 static int core_status(struct dirty_log *log, status_type_t status,
635 char *result, unsigned int maxlen)
639 struct log_c *lc = log->context;
642 case STATUSTYPE_INFO:
646 case STATUSTYPE_TABLE:
647 params = (lc->sync == DEFAULTSYNC) ? 1 : 2;
648 params += (lc->failure_response == DMLOG_IOERR_BLOCK) ? 1 : 0;
650 DMEMIT("%s %d %u ", log->type->name, params, lc->region_size);
652 if (lc->failure_response == DMLOG_IOERR_BLOCK)
653 DMEMIT("block_on_error ");
659 static int disk_status(struct dirty_log *log, status_type_t status,
660 char *result, unsigned int maxlen)
664 struct log_c *lc = log->context;
667 case STATUSTYPE_INFO:
668 DMEMIT("3 disk %s %c", lc->log_dev->name,
669 lc->log_dev_failed ? 'D' : 'A');
672 case STATUSTYPE_TABLE:
673 params = (lc->sync == DEFAULTSYNC) ? 2 : 3;
674 params += (lc->failure_response == DMLOG_IOERR_BLOCK) ? 1 : 0;
676 DMEMIT("%s %d %s %u ", log->type->name,
681 if (lc->failure_response == DMLOG_IOERR_BLOCK)
682 DMEMIT("block_on_error ");
688 static int core_get_failure_response(struct dirty_log *log)
690 struct log_c *lc = log->context;
692 return lc->failure_response;
695 static struct dirty_log_type _core_type = {
697 .module = THIS_MODULE,
700 .resume = core_resume,
701 .get_region_size = core_get_region_size,
702 .is_clean = core_is_clean,
703 .in_sync = core_in_sync,
705 .mark_region = core_mark_region,
706 .clear_region = core_clear_region,
707 .get_resync_work = core_get_resync_work,
708 .set_region_sync = core_set_region_sync,
709 .get_sync_count = core_get_sync_count,
710 .status = core_status,
711 .get_failure_response = core_get_failure_response,
714 static struct dirty_log_type _disk_type = {
716 .module = THIS_MODULE,
719 .presuspend = disk_presuspend,
720 .postsuspend = disk_flush,
721 .resume = disk_resume,
722 .get_region_size = core_get_region_size,
723 .is_clean = core_is_clean,
724 .in_sync = core_in_sync,
726 .mark_region = core_mark_region,
727 .clear_region = core_clear_region,
728 .get_resync_work = core_get_resync_work,
729 .set_region_sync = core_set_region_sync,
730 .get_sync_count = core_get_sync_count,
731 .status = disk_status,
732 .get_failure_response = core_get_failure_response,
735 int __init dm_dirty_log_init(void)
739 r = dm_register_dirty_log_type(&_core_type);
741 DMWARN("couldn't register core log");
743 r = dm_register_dirty_log_type(&_disk_type);
745 DMWARN("couldn't register disk type");
746 dm_unregister_dirty_log_type(&_core_type);
752 void dm_dirty_log_exit(void)
754 dm_unregister_dirty_log_type(&_disk_type);
755 dm_unregister_dirty_log_type(&_core_type);
758 EXPORT_SYMBOL(dm_register_dirty_log_type);
759 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
760 EXPORT_SYMBOL(dm_create_dirty_log);
761 EXPORT_SYMBOL(dm_destroy_dirty_log);