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)){
64 static void put_type(struct dirty_log_type *type)
67 if (!--type->use_count)
68 module_put(type->module);
72 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
73 unsigned int argc, char **argv)
75 struct dirty_log_type *type;
76 struct dirty_log *log;
78 log = kmalloc(sizeof(*log), GFP_KERNEL);
82 type = get_type(type_name);
89 if (type->ctr(log, ti, argc, argv)) {
98 void dm_destroy_dirty_log(struct dirty_log *log)
105 /*-----------------------------------------------------------------
106 * Persistent and core logs share a lot of their implementation.
107 * FIXME: need a reload method to be called from a resume
108 *---------------------------------------------------------------*/
110 * Magic for persistent mirrors: "MiRr"
112 #define MIRROR_MAGIC 0x4D695272
115 * The on-disk version of the metadata.
117 #define MIRROR_DISK_VERSION 2
124 * Simple, incrementing version. no backward
132 struct dm_target *ti;
134 uint32_t region_size;
135 unsigned int region_count;
138 unsigned bitset_uint32_count;
139 uint32_t *clean_bits;
141 uint32_t *recovering_bits; /* FIXME: this seems excessive */
147 DEFAULTSYNC, /* Synchronize if necessary */
148 NOSYNC, /* Devices known to be already in sync */
149 FORCESYNC, /* Force a sync to happen */
156 struct dm_dev *log_dev;
157 struct log_header header;
159 struct io_region header_location;
160 struct log_header *disk_header;
164 * The touched member needs to be updated every time we access
165 * one of the bitsets.
167 static inline int log_test_bit(uint32_t *bs, unsigned bit)
169 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
172 static inline void log_set_bit(struct log_c *l,
173 uint32_t *bs, unsigned bit)
175 ext2_set_bit(bit, (unsigned long *) bs);
179 static inline void log_clear_bit(struct log_c *l,
180 uint32_t *bs, unsigned bit)
182 ext2_clear_bit(bit, (unsigned long *) bs);
186 /*----------------------------------------------------------------
188 *--------------------------------------------------------------*/
189 static void header_to_disk(struct log_header *core, struct log_header *disk)
191 disk->magic = cpu_to_le32(core->magic);
192 disk->version = cpu_to_le32(core->version);
193 disk->nr_regions = cpu_to_le64(core->nr_regions);
196 static void header_from_disk(struct log_header *core, struct log_header *disk)
198 core->magic = le32_to_cpu(disk->magic);
199 core->version = le32_to_cpu(disk->version);
200 core->nr_regions = le64_to_cpu(disk->nr_regions);
203 static int read_header(struct log_c *log)
208 r = dm_io_sync_vm(1, &log->header_location, READ,
209 log->disk_header, &ebits);
213 header_from_disk(&log->header, log->disk_header);
215 /* New log required? */
216 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
217 log->header.magic = MIRROR_MAGIC;
218 log->header.version = MIRROR_DISK_VERSION;
219 log->header.nr_regions = 0;
222 #ifdef __LITTLE_ENDIAN
223 if (log->header.version == 1)
224 log->header.version = 2;
227 if (log->header.version != MIRROR_DISK_VERSION) {
228 DMWARN("incompatible disk log version");
235 static inline int write_header(struct log_c *log)
239 header_to_disk(&log->header, log->disk_header);
240 return dm_io_sync_vm(1, &log->header_location, WRITE,
241 log->disk_header, &ebits);
244 /*----------------------------------------------------------------
245 * core log constructor/destructor
247 * argv contains region_size followed optionally by [no]sync
248 *--------------------------------------------------------------*/
250 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
251 unsigned int argc, char **argv,
254 enum sync sync = DEFAULTSYNC;
257 uint32_t region_size;
258 unsigned int region_count;
259 size_t bitset_size, buf_size;
261 if (argc < 1 || argc > 2) {
262 DMWARN("wrong number of arguments to mirror log");
267 if (!strcmp(argv[1], "sync"))
269 else if (!strcmp(argv[1], "nosync"))
272 DMWARN("unrecognised sync argument to mirror log: %s",
278 if (sscanf(argv[0], "%u", ®ion_size) != 1) {
279 DMWARN("invalid region size string");
283 region_count = dm_sector_div_up(ti->len, region_size);
285 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
287 DMWARN("couldn't allocate core log");
293 lc->region_size = region_size;
294 lc->region_count = region_count;
298 * Work out how many "unsigned long"s we need to hold the bitset.
300 bitset_size = dm_round_up(region_count,
301 sizeof(*lc->clean_bits) << BYTE_SHIFT);
302 bitset_size >>= BYTE_SHIFT;
304 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
310 lc->clean_bits = vmalloc(bitset_size);
311 if (!lc->clean_bits) {
312 DMWARN("couldn't allocate clean bitset");
316 lc->disk_header = NULL;
319 lc->log_dev_failed = 0;
320 lc->header_location.bdev = lc->log_dev->bdev;
321 lc->header_location.sector = 0;
324 * Buffer holds both header and bitset.
326 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
327 bitset_size, ti->limits.hardsect_size);
328 lc->header_location.count = buf_size >> SECTOR_SHIFT;
330 lc->disk_header = vmalloc(buf_size);
331 if (!lc->disk_header) {
332 DMWARN("couldn't allocate disk log buffer");
337 lc->clean_bits = (void *)lc->disk_header +
338 (LOG_OFFSET << SECTOR_SHIFT);
341 memset(lc->clean_bits, -1, bitset_size);
343 lc->sync_bits = vmalloc(bitset_size);
344 if (!lc->sync_bits) {
345 DMWARN("couldn't allocate sync bitset");
347 vfree(lc->clean_bits);
348 vfree(lc->disk_header);
352 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
353 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
355 lc->recovering_bits = vmalloc(bitset_size);
356 if (!lc->recovering_bits) {
357 DMWARN("couldn't allocate sync bitset");
358 vfree(lc->sync_bits);
360 vfree(lc->clean_bits);
361 vfree(lc->disk_header);
365 memset(lc->recovering_bits, 0, bitset_size);
372 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
373 unsigned int argc, char **argv)
375 return create_log_context(log, ti, argc, argv, NULL);
378 static void destroy_log_context(struct log_c *lc)
380 vfree(lc->sync_bits);
381 vfree(lc->recovering_bits);
385 static void core_dtr(struct dirty_log *log)
387 struct log_c *lc = (struct log_c *) log->context;
389 vfree(lc->clean_bits);
390 destroy_log_context(lc);
393 /*----------------------------------------------------------------
394 * disk log constructor/destructor
396 * argv contains log_device region_size followed optionally by [no]sync
397 *--------------------------------------------------------------*/
398 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
399 unsigned int argc, char **argv)
404 if (argc < 2 || argc > 3) {
405 DMWARN("wrong number of arguments to disk mirror log");
409 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
410 FMODE_READ | FMODE_WRITE, &dev);
414 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
416 dm_put_device(ti, dev);
423 static void disk_dtr(struct dirty_log *log)
425 struct log_c *lc = (struct log_c *) log->context;
427 dm_put_device(lc->ti, lc->log_dev);
428 vfree(lc->disk_header);
429 destroy_log_context(lc);
432 static int count_bits32(uint32_t *addr, unsigned size)
436 for (i = 0; i < size; i++) {
437 count += hweight32(*(addr+i));
442 static void fail_log_device(struct log_c *lc)
444 if (lc->log_dev_failed)
447 lc->log_dev_failed = 1;
448 dm_table_event(lc->ti->table);
451 static int disk_resume(struct dirty_log *log)
455 struct log_c *lc = (struct log_c *) log->context;
456 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
458 /* read the disk header */
461 DMWARN("%s: Failed to read header on mirror log device",
467 /* set or clear any new bits -- device has grown */
468 if (lc->sync == NOSYNC)
469 for (i = lc->header.nr_regions; i < lc->region_count; i++)
470 /* FIXME: amazingly inefficient */
471 log_set_bit(lc, lc->clean_bits, i);
473 for (i = lc->header.nr_regions; i < lc->region_count; i++)
474 /* FIXME: amazingly inefficient */
475 log_clear_bit(lc, lc->clean_bits, i);
477 /* clear any old bits -- device has shrunk */
478 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
479 log_clear_bit(lc, lc->clean_bits, i);
481 /* copy clean across to sync */
482 memcpy(lc->sync_bits, lc->clean_bits, size);
483 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
486 /* set the correct number of regions in the header */
487 lc->header.nr_regions = lc->region_count;
489 /* write the new header */
490 r = write_header(lc);
492 DMWARN("%s: Failed to write header on mirror log device",
500 static uint32_t core_get_region_size(struct dirty_log *log)
502 struct log_c *lc = (struct log_c *) log->context;
503 return lc->region_size;
506 static int core_resume(struct dirty_log *log)
508 struct log_c *lc = (struct log_c *) log->context;
513 static int core_is_clean(struct dirty_log *log, region_t region)
515 struct log_c *lc = (struct log_c *) log->context;
516 return log_test_bit(lc->clean_bits, region);
519 static int core_in_sync(struct dirty_log *log, region_t region, int block)
521 struct log_c *lc = (struct log_c *) log->context;
522 return log_test_bit(lc->sync_bits, region);
525 static int core_flush(struct dirty_log *log)
531 static int disk_flush(struct dirty_log *log)
534 struct log_c *lc = (struct log_c *) log->context;
536 /* only write if the log has changed */
540 r = write_header(lc);
549 static void core_mark_region(struct dirty_log *log, region_t region)
551 struct log_c *lc = (struct log_c *) log->context;
552 log_clear_bit(lc, lc->clean_bits, region);
555 static void core_clear_region(struct dirty_log *log, region_t region)
557 struct log_c *lc = (struct log_c *) log->context;
558 log_set_bit(lc, lc->clean_bits, region);
561 static int core_get_resync_work(struct dirty_log *log, region_t *region)
563 struct log_c *lc = (struct log_c *) log->context;
565 if (lc->sync_search >= lc->region_count)
569 *region = ext2_find_next_zero_bit(
570 (unsigned long *) lc->sync_bits,
573 lc->sync_search = *region + 1;
575 if (*region >= lc->region_count)
578 } while (log_test_bit(lc->recovering_bits, *region));
580 log_set_bit(lc, lc->recovering_bits, *region);
584 static void core_set_region_sync(struct dirty_log *log, region_t region,
587 struct log_c *lc = (struct log_c *) log->context;
589 log_clear_bit(lc, lc->recovering_bits, region);
591 log_set_bit(lc, lc->sync_bits, region);
593 } else if (log_test_bit(lc->sync_bits, region)) {
595 log_clear_bit(lc, lc->sync_bits, region);
599 static region_t core_get_sync_count(struct dirty_log *log)
601 struct log_c *lc = (struct log_c *) log->context;
603 return lc->sync_count;
606 #define DMEMIT_SYNC \
607 if (lc->sync != DEFAULTSYNC) \
608 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
610 static int core_status(struct dirty_log *log, status_type_t status,
611 char *result, unsigned int maxlen)
614 struct log_c *lc = log->context;
617 case STATUSTYPE_INFO:
618 DMEMIT("1 %s", log->type->name);
621 case STATUSTYPE_TABLE:
622 DMEMIT("%s %u %u ", log->type->name,
623 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
630 static int disk_status(struct dirty_log *log, status_type_t status,
631 char *result, unsigned int maxlen)
634 struct log_c *lc = log->context;
637 case STATUSTYPE_INFO:
638 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
639 lc->log_dev_failed ? 'D' : 'A');
642 case STATUSTYPE_TABLE:
643 DMEMIT("%s %u %s %u ", log->type->name,
644 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
652 static struct dirty_log_type _core_type = {
654 .module = THIS_MODULE,
657 .resume = core_resume,
658 .get_region_size = core_get_region_size,
659 .is_clean = core_is_clean,
660 .in_sync = core_in_sync,
662 .mark_region = core_mark_region,
663 .clear_region = core_clear_region,
664 .get_resync_work = core_get_resync_work,
665 .set_region_sync = core_set_region_sync,
666 .get_sync_count = core_get_sync_count,
667 .status = core_status,
670 static struct dirty_log_type _disk_type = {
672 .module = THIS_MODULE,
675 .suspend = disk_flush,
676 .resume = disk_resume,
677 .get_region_size = core_get_region_size,
678 .is_clean = core_is_clean,
679 .in_sync = core_in_sync,
681 .mark_region = core_mark_region,
682 .clear_region = core_clear_region,
683 .get_resync_work = core_get_resync_work,
684 .set_region_sync = core_set_region_sync,
685 .get_sync_count = core_get_sync_count,
686 .status = disk_status,
689 int __init dm_dirty_log_init(void)
693 r = dm_register_dirty_log_type(&_core_type);
695 DMWARN("couldn't register core log");
697 r = dm_register_dirty_log_type(&_disk_type);
699 DMWARN("couldn't register disk type");
700 dm_unregister_dirty_log_type(&_core_type);
706 void dm_dirty_log_exit(void)
708 dm_unregister_dirty_log_type(&_disk_type);
709 dm_unregister_dirty_log_type(&_core_type);
712 EXPORT_SYMBOL(dm_register_dirty_log_type);
713 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
714 EXPORT_SYMBOL(dm_create_dirty_log);
715 EXPORT_SYMBOL(dm_destroy_dirty_log);