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("Attempt to unregister a log type that is still in use");
37 list_del(&type->list);
44 static struct dirty_log_type *_get_type(const char *type_name)
46 struct dirty_log_type *type;
49 list_for_each_entry (type, &_log_types, list)
50 if (!strcmp(type_name, type->name)) {
51 if (!type->use_count && !try_module_get(type->module)){
68 * Attempt to retrieve the dirty_log_type by name. If not already
69 * available, attempt to load the appropriate module.
71 * Log modules are named "dm-log-" followed by the 'type_name'.
72 * Modules may contain multiple types.
73 * This function will first try the module "dm-log-<type_name>",
74 * then truncate 'type_name' on the last '-' and try again.
76 * For example, if type_name was "clustered-disk", it would search
77 * 'dm-log-clustered-disk' then 'dm-log-clustered'.
79 * Returns: dirty_log_type* on success, NULL on failure
81 static struct dirty_log_type *get_type(const char *type_name)
83 char *p, *type_name_dup;
84 struct dirty_log_type *type;
86 type = _get_type(type_name);
90 type_name_dup = kstrdup(type_name, GFP_KERNEL);
92 DMWARN("No memory left to attempt log module load for \"%s\"",
97 while (request_module("dm-log-%s", type_name_dup) ||
98 !(type = _get_type(type_name))) {
99 p = strrchr(type_name_dup, '-');
106 DMWARN("Module for logging type \"%s\" not found.", type_name);
108 kfree(type_name_dup);
113 static void put_type(struct dirty_log_type *type)
116 if (!--type->use_count)
117 module_put(type->module);
121 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
122 unsigned int argc, char **argv)
124 struct dirty_log_type *type;
125 struct dirty_log *log;
127 log = kmalloc(sizeof(*log), GFP_KERNEL);
131 type = get_type(type_name);
138 if (type->ctr(log, ti, argc, argv)) {
147 void dm_destroy_dirty_log(struct dirty_log *log)
154 /*-----------------------------------------------------------------
155 * Persistent and core logs share a lot of their implementation.
156 * FIXME: need a reload method to be called from a resume
157 *---------------------------------------------------------------*/
159 * Magic for persistent mirrors: "MiRr"
161 #define MIRROR_MAGIC 0x4D695272
164 * The on-disk version of the metadata.
166 #define MIRROR_DISK_VERSION 2
173 * Simple, incrementing version. no backward
181 struct dm_target *ti;
183 uint32_t region_size;
184 unsigned int region_count;
187 unsigned bitset_uint32_count;
188 uint32_t *clean_bits;
190 uint32_t *recovering_bits; /* FIXME: this seems excessive */
196 DEFAULTSYNC, /* Synchronize if necessary */
197 NOSYNC, /* Devices known to be already in sync */
198 FORCESYNC, /* Force a sync to happen */
201 struct dm_io_request io_req;
207 struct dm_dev *log_dev;
208 struct log_header header;
210 struct io_region header_location;
211 struct log_header *disk_header;
215 * The touched member needs to be updated every time we access
216 * one of the bitsets.
218 static inline int log_test_bit(uint32_t *bs, unsigned bit)
220 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
223 static inline void log_set_bit(struct log_c *l,
224 uint32_t *bs, unsigned bit)
226 ext2_set_bit(bit, (unsigned long *) bs);
230 static inline void log_clear_bit(struct log_c *l,
231 uint32_t *bs, unsigned bit)
233 ext2_clear_bit(bit, (unsigned long *) bs);
237 /*----------------------------------------------------------------
239 *--------------------------------------------------------------*/
240 static void header_to_disk(struct log_header *core, struct log_header *disk)
242 disk->magic = cpu_to_le32(core->magic);
243 disk->version = cpu_to_le32(core->version);
244 disk->nr_regions = cpu_to_le64(core->nr_regions);
247 static void header_from_disk(struct log_header *core, struct log_header *disk)
249 core->magic = le32_to_cpu(disk->magic);
250 core->version = le32_to_cpu(disk->version);
251 core->nr_regions = le64_to_cpu(disk->nr_regions);
254 static int rw_header(struct log_c *lc, int rw)
256 lc->io_req.bi_rw = rw;
257 lc->io_req.mem.ptr.vma = lc->disk_header;
258 lc->io_req.notify.fn = NULL;
260 return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
263 static int read_header(struct log_c *log)
267 r = rw_header(log, READ);
271 header_from_disk(&log->header, log->disk_header);
273 /* New log required? */
274 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
275 log->header.magic = MIRROR_MAGIC;
276 log->header.version = MIRROR_DISK_VERSION;
277 log->header.nr_regions = 0;
280 #ifdef __LITTLE_ENDIAN
281 if (log->header.version == 1)
282 log->header.version = 2;
285 if (log->header.version != MIRROR_DISK_VERSION) {
286 DMWARN("incompatible disk log version");
293 static inline int write_header(struct log_c *log)
295 header_to_disk(&log->header, log->disk_header);
296 return rw_header(log, WRITE);
299 /*----------------------------------------------------------------
300 * core log constructor/destructor
302 * argv contains region_size followed optionally by [no]sync
303 *--------------------------------------------------------------*/
305 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
306 unsigned int argc, char **argv,
309 enum sync sync = DEFAULTSYNC;
312 uint32_t region_size;
313 unsigned int region_count;
314 size_t bitset_size, buf_size;
317 if (argc < 1 || argc > 2) {
318 DMWARN("wrong number of arguments to mirror log");
323 if (!strcmp(argv[1], "sync"))
325 else if (!strcmp(argv[1], "nosync"))
328 DMWARN("unrecognised sync argument to mirror log: %s",
334 if (sscanf(argv[0], "%u", ®ion_size) != 1) {
335 DMWARN("invalid region size string");
339 region_count = dm_sector_div_up(ti->len, region_size);
341 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
343 DMWARN("couldn't allocate core log");
349 lc->region_size = region_size;
350 lc->region_count = region_count;
354 * Work out how many "unsigned long"s we need to hold the bitset.
356 bitset_size = dm_round_up(region_count,
357 sizeof(*lc->clean_bits) << BYTE_SHIFT);
358 bitset_size >>= BYTE_SHIFT;
360 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
366 lc->clean_bits = vmalloc(bitset_size);
367 if (!lc->clean_bits) {
368 DMWARN("couldn't allocate clean bitset");
372 lc->disk_header = NULL;
375 lc->log_dev_failed = 0;
376 lc->header_location.bdev = lc->log_dev->bdev;
377 lc->header_location.sector = 0;
380 * Buffer holds both header and bitset.
382 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
383 bitset_size, ti->limits.hardsect_size);
384 lc->header_location.count = buf_size >> SECTOR_SHIFT;
385 lc->io_req.mem.type = DM_IO_VMA;
386 lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
388 if (IS_ERR(lc->io_req.client)) {
389 r = PTR_ERR(lc->io_req.client);
390 DMWARN("couldn't allocate disk io client");
395 lc->disk_header = vmalloc(buf_size);
396 if (!lc->disk_header) {
397 DMWARN("couldn't allocate disk log buffer");
402 lc->clean_bits = (void *)lc->disk_header +
403 (LOG_OFFSET << SECTOR_SHIFT);
406 memset(lc->clean_bits, -1, bitset_size);
408 lc->sync_bits = vmalloc(bitset_size);
409 if (!lc->sync_bits) {
410 DMWARN("couldn't allocate sync bitset");
412 vfree(lc->clean_bits);
413 vfree(lc->disk_header);
417 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
418 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
420 lc->recovering_bits = vmalloc(bitset_size);
421 if (!lc->recovering_bits) {
422 DMWARN("couldn't allocate sync bitset");
423 vfree(lc->sync_bits);
425 vfree(lc->clean_bits);
426 vfree(lc->disk_header);
430 memset(lc->recovering_bits, 0, bitset_size);
437 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
438 unsigned int argc, char **argv)
440 return create_log_context(log, ti, argc, argv, NULL);
443 static void destroy_log_context(struct log_c *lc)
445 vfree(lc->sync_bits);
446 vfree(lc->recovering_bits);
450 static void core_dtr(struct dirty_log *log)
452 struct log_c *lc = (struct log_c *) log->context;
454 vfree(lc->clean_bits);
455 destroy_log_context(lc);
458 /*----------------------------------------------------------------
459 * disk log constructor/destructor
461 * argv contains log_device region_size followed optionally by [no]sync
462 *--------------------------------------------------------------*/
463 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
464 unsigned int argc, char **argv)
469 if (argc < 2 || argc > 3) {
470 DMWARN("wrong number of arguments to disk mirror log");
474 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
475 FMODE_READ | FMODE_WRITE, &dev);
479 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
481 dm_put_device(ti, dev);
488 static void disk_dtr(struct dirty_log *log)
490 struct log_c *lc = (struct log_c *) log->context;
492 dm_put_device(lc->ti, lc->log_dev);
493 vfree(lc->disk_header);
494 dm_io_client_destroy(lc->io_req.client);
495 destroy_log_context(lc);
498 static int count_bits32(uint32_t *addr, unsigned size)
502 for (i = 0; i < size; i++) {
503 count += hweight32(*(addr+i));
508 static void fail_log_device(struct log_c *lc)
510 if (lc->log_dev_failed)
513 lc->log_dev_failed = 1;
514 dm_table_event(lc->ti->table);
517 static int disk_resume(struct dirty_log *log)
521 struct log_c *lc = (struct log_c *) log->context;
522 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
524 /* read the disk header */
527 DMWARN("%s: Failed to read header on mirror log device",
531 * If the log device cannot be read, we must assume
532 * all regions are out-of-sync. If we simply return
533 * here, the state will be uninitialized and could
534 * lead us to return 'in-sync' status for regions
535 * that are actually 'out-of-sync'.
537 lc->header.nr_regions = 0;
540 /* set or clear any new bits -- device has grown */
541 if (lc->sync == NOSYNC)
542 for (i = lc->header.nr_regions; i < lc->region_count; i++)
543 /* FIXME: amazingly inefficient */
544 log_set_bit(lc, lc->clean_bits, i);
546 for (i = lc->header.nr_regions; i < lc->region_count; i++)
547 /* FIXME: amazingly inefficient */
548 log_clear_bit(lc, lc->clean_bits, i);
550 /* clear any old bits -- device has shrunk */
551 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
552 log_clear_bit(lc, lc->clean_bits, i);
554 /* copy clean across to sync */
555 memcpy(lc->sync_bits, lc->clean_bits, size);
556 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
559 /* set the correct number of regions in the header */
560 lc->header.nr_regions = lc->region_count;
562 /* write the new header */
563 r = write_header(lc);
565 DMWARN("%s: Failed to write header on mirror log device",
573 static uint32_t core_get_region_size(struct dirty_log *log)
575 struct log_c *lc = (struct log_c *) log->context;
576 return lc->region_size;
579 static int core_resume(struct dirty_log *log)
581 struct log_c *lc = (struct log_c *) log->context;
586 static int core_is_clean(struct dirty_log *log, region_t region)
588 struct log_c *lc = (struct log_c *) log->context;
589 return log_test_bit(lc->clean_bits, region);
592 static int core_in_sync(struct dirty_log *log, region_t region, int block)
594 struct log_c *lc = (struct log_c *) log->context;
595 return log_test_bit(lc->sync_bits, region);
598 static int core_flush(struct dirty_log *log)
604 static int disk_flush(struct dirty_log *log)
607 struct log_c *lc = (struct log_c *) log->context;
609 /* only write if the log has changed */
613 r = write_header(lc);
622 static void core_mark_region(struct dirty_log *log, region_t region)
624 struct log_c *lc = (struct log_c *) log->context;
625 log_clear_bit(lc, lc->clean_bits, region);
628 static void core_clear_region(struct dirty_log *log, region_t region)
630 struct log_c *lc = (struct log_c *) log->context;
631 log_set_bit(lc, lc->clean_bits, region);
634 static int core_get_resync_work(struct dirty_log *log, region_t *region)
636 struct log_c *lc = (struct log_c *) log->context;
638 if (lc->sync_search >= lc->region_count)
642 *region = ext2_find_next_zero_bit(
643 (unsigned long *) lc->sync_bits,
646 lc->sync_search = *region + 1;
648 if (*region >= lc->region_count)
651 } while (log_test_bit(lc->recovering_bits, *region));
653 log_set_bit(lc, lc->recovering_bits, *region);
657 static void core_set_region_sync(struct dirty_log *log, region_t region,
660 struct log_c *lc = (struct log_c *) log->context;
662 log_clear_bit(lc, lc->recovering_bits, region);
664 log_set_bit(lc, lc->sync_bits, region);
666 } else if (log_test_bit(lc->sync_bits, region)) {
668 log_clear_bit(lc, lc->sync_bits, region);
672 static region_t core_get_sync_count(struct dirty_log *log)
674 struct log_c *lc = (struct log_c *) log->context;
676 return lc->sync_count;
679 #define DMEMIT_SYNC \
680 if (lc->sync != DEFAULTSYNC) \
681 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
683 static int core_status(struct dirty_log *log, status_type_t status,
684 char *result, unsigned int maxlen)
687 struct log_c *lc = log->context;
690 case STATUSTYPE_INFO:
691 DMEMIT("1 %s", log->type->name);
694 case STATUSTYPE_TABLE:
695 DMEMIT("%s %u %u ", log->type->name,
696 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
703 static int disk_status(struct dirty_log *log, status_type_t status,
704 char *result, unsigned int maxlen)
707 struct log_c *lc = log->context;
710 case STATUSTYPE_INFO:
711 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
712 lc->log_dev_failed ? 'D' : 'A');
715 case STATUSTYPE_TABLE:
716 DMEMIT("%s %u %s %u ", log->type->name,
717 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
725 static struct dirty_log_type _core_type = {
727 .module = THIS_MODULE,
730 .resume = core_resume,
731 .get_region_size = core_get_region_size,
732 .is_clean = core_is_clean,
733 .in_sync = core_in_sync,
735 .mark_region = core_mark_region,
736 .clear_region = core_clear_region,
737 .get_resync_work = core_get_resync_work,
738 .set_region_sync = core_set_region_sync,
739 .get_sync_count = core_get_sync_count,
740 .status = core_status,
743 static struct dirty_log_type _disk_type = {
745 .module = THIS_MODULE,
748 .postsuspend = disk_flush,
749 .resume = disk_resume,
750 .get_region_size = core_get_region_size,
751 .is_clean = core_is_clean,
752 .in_sync = core_in_sync,
754 .mark_region = core_mark_region,
755 .clear_region = core_clear_region,
756 .get_resync_work = core_get_resync_work,
757 .set_region_sync = core_set_region_sync,
758 .get_sync_count = core_get_sync_count,
759 .status = disk_status,
762 int __init dm_dirty_log_init(void)
766 r = dm_register_dirty_log_type(&_core_type);
768 DMWARN("couldn't register core log");
770 r = dm_register_dirty_log_type(&_disk_type);
772 DMWARN("couldn't register disk type");
773 dm_unregister_dirty_log_type(&_core_type);
779 void dm_dirty_log_exit(void)
781 dm_unregister_dirty_log_type(&_disk_type);
782 dm_unregister_dirty_log_type(&_core_type);
785 EXPORT_SYMBOL(dm_register_dirty_log_type);
786 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
787 EXPORT_SYMBOL(dm_create_dirty_log);
788 EXPORT_SYMBOL(dm_destroy_dirty_log);