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 static LIST_HEAD(_log_types);
16 static DEFINE_SPINLOCK(_lock);
18 int dm_register_dirty_log_type(struct dirty_log_type *type)
22 list_add(&type->list, &_log_types);
28 int dm_unregister_dirty_log_type(struct dirty_log_type *type)
33 DMWARN("Attempt to unregister a log type that is still in use");
35 list_del(&type->list);
42 static struct dirty_log_type *get_type(const char *type_name)
44 struct dirty_log_type *type;
47 list_for_each_entry (type, &_log_types, list)
48 if (!strcmp(type_name, type->name)) {
49 if (!type->use_count && !try_module_get(type->module)){
62 static void put_type(struct dirty_log_type *type)
65 if (!--type->use_count)
66 module_put(type->module);
70 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
71 unsigned int argc, char **argv)
73 struct dirty_log_type *type;
74 struct dirty_log *log;
76 log = kmalloc(sizeof(*log), GFP_KERNEL);
80 type = get_type(type_name);
87 if (type->ctr(log, ti, argc, argv)) {
96 void dm_destroy_dirty_log(struct dirty_log *log)
103 /*-----------------------------------------------------------------
104 * Persistent and core logs share a lot of their implementation.
105 * FIXME: need a reload method to be called from a resume
106 *---------------------------------------------------------------*/
108 * Magic for persistent mirrors: "MiRr"
110 #define MIRROR_MAGIC 0x4D695272
113 * The on-disk version of the metadata.
115 #define MIRROR_DISK_VERSION 2
122 * Simple, incrementing version. no backward
130 struct dm_target *ti;
132 uint32_t region_size;
133 unsigned int region_count;
136 unsigned bitset_uint32_count;
137 uint32_t *clean_bits;
139 uint32_t *recovering_bits; /* FIXME: this seems excessive */
145 DEFAULTSYNC, /* Synchronize if necessary */
146 NOSYNC, /* Devices known to be already in sync */
147 FORCESYNC, /* Force a sync to happen */
153 struct dm_dev *log_dev;
154 struct log_header header;
156 struct io_region header_location;
157 struct log_header *disk_header;
161 * The touched member needs to be updated every time we access
162 * one of the bitsets.
164 static inline int log_test_bit(uint32_t *bs, unsigned bit)
166 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
169 static inline void log_set_bit(struct log_c *l,
170 uint32_t *bs, unsigned bit)
172 ext2_set_bit(bit, (unsigned long *) bs);
176 static inline void log_clear_bit(struct log_c *l,
177 uint32_t *bs, unsigned bit)
179 ext2_clear_bit(bit, (unsigned long *) bs);
183 /*----------------------------------------------------------------
185 *--------------------------------------------------------------*/
186 static void header_to_disk(struct log_header *core, struct log_header *disk)
188 disk->magic = cpu_to_le32(core->magic);
189 disk->version = cpu_to_le32(core->version);
190 disk->nr_regions = cpu_to_le64(core->nr_regions);
193 static void header_from_disk(struct log_header *core, struct log_header *disk)
195 core->magic = le32_to_cpu(disk->magic);
196 core->version = le32_to_cpu(disk->version);
197 core->nr_regions = le64_to_cpu(disk->nr_regions);
200 static int read_header(struct log_c *log)
205 r = dm_io_sync_vm(1, &log->header_location, READ,
206 log->disk_header, &ebits);
210 header_from_disk(&log->header, log->disk_header);
212 /* New log required? */
213 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
214 log->header.magic = MIRROR_MAGIC;
215 log->header.version = MIRROR_DISK_VERSION;
216 log->header.nr_regions = 0;
219 #ifdef __LITTLE_ENDIAN
220 if (log->header.version == 1)
221 log->header.version = 2;
224 if (log->header.version != MIRROR_DISK_VERSION) {
225 DMWARN("incompatible disk log version");
232 static inline int write_header(struct log_c *log)
236 header_to_disk(&log->header, log->disk_header);
237 return dm_io_sync_vm(1, &log->header_location, WRITE,
238 log->disk_header, &ebits);
241 /*----------------------------------------------------------------
242 * core log constructor/destructor
244 * argv contains region_size followed optionally by [no]sync
245 *--------------------------------------------------------------*/
247 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
248 unsigned int argc, char **argv,
251 enum sync sync = DEFAULTSYNC;
254 uint32_t region_size;
255 unsigned int region_count;
256 size_t bitset_size, buf_size;
258 if (argc < 1 || argc > 2) {
259 DMWARN("wrong number of arguments to mirror log");
264 if (!strcmp(argv[1], "sync"))
266 else if (!strcmp(argv[1], "nosync"))
269 DMWARN("unrecognised sync argument to mirror log: %s",
275 if (sscanf(argv[0], "%u", ®ion_size) != 1) {
276 DMWARN("invalid region size string");
280 region_count = dm_sector_div_up(ti->len, region_size);
282 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
284 DMWARN("couldn't allocate core log");
290 lc->region_size = region_size;
291 lc->region_count = region_count;
295 * Work out how many "unsigned long"s we need to hold the bitset.
297 bitset_size = dm_round_up(region_count,
298 sizeof(*lc->clean_bits) << BYTE_SHIFT);
299 bitset_size >>= BYTE_SHIFT;
301 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
307 lc->clean_bits = vmalloc(bitset_size);
308 if (!lc->clean_bits) {
309 DMWARN("couldn't allocate clean bitset");
313 lc->disk_header = NULL;
316 lc->header_location.bdev = lc->log_dev->bdev;
317 lc->header_location.sector = 0;
320 * Buffer holds both header and bitset.
322 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
323 bitset_size, ti->limits.hardsect_size);
324 lc->header_location.count = buf_size >> SECTOR_SHIFT;
326 lc->disk_header = vmalloc(buf_size);
327 if (!lc->disk_header) {
328 DMWARN("couldn't allocate disk log buffer");
333 lc->clean_bits = (void *)lc->disk_header +
334 (LOG_OFFSET << SECTOR_SHIFT);
337 memset(lc->clean_bits, -1, bitset_size);
339 lc->sync_bits = vmalloc(bitset_size);
340 if (!lc->sync_bits) {
341 DMWARN("couldn't allocate sync bitset");
343 vfree(lc->clean_bits);
344 vfree(lc->disk_header);
348 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
349 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
351 lc->recovering_bits = vmalloc(bitset_size);
352 if (!lc->recovering_bits) {
353 DMWARN("couldn't allocate sync bitset");
354 vfree(lc->sync_bits);
356 vfree(lc->clean_bits);
357 vfree(lc->disk_header);
361 memset(lc->recovering_bits, 0, bitset_size);
368 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
369 unsigned int argc, char **argv)
371 return create_log_context(log, ti, argc, argv, NULL);
374 static void destroy_log_context(struct log_c *lc)
376 vfree(lc->sync_bits);
377 vfree(lc->recovering_bits);
381 static void core_dtr(struct dirty_log *log)
383 struct log_c *lc = (struct log_c *) log->context;
385 vfree(lc->clean_bits);
386 destroy_log_context(lc);
389 /*----------------------------------------------------------------
390 * disk log constructor/destructor
392 * argv contains log_device region_size followed optionally by [no]sync
393 *--------------------------------------------------------------*/
394 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
395 unsigned int argc, char **argv)
400 if (argc < 2 || argc > 3) {
401 DMWARN("wrong number of arguments to disk mirror log");
405 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
406 FMODE_READ | FMODE_WRITE, &dev);
410 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
412 dm_put_device(ti, dev);
419 static void disk_dtr(struct dirty_log *log)
421 struct log_c *lc = (struct log_c *) log->context;
423 dm_put_device(lc->ti, lc->log_dev);
424 vfree(lc->disk_header);
425 destroy_log_context(lc);
428 static int count_bits32(uint32_t *addr, unsigned size)
432 for (i = 0; i < size; i++) {
433 count += hweight32(*(addr+i));
438 static int disk_resume(struct dirty_log *log)
442 struct log_c *lc = (struct log_c *) log->context;
443 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
445 /* read the disk header */
450 /* set or clear any new bits -- device has grown */
451 if (lc->sync == NOSYNC)
452 for (i = lc->header.nr_regions; i < lc->region_count; i++)
453 /* FIXME: amazingly inefficient */
454 log_set_bit(lc, lc->clean_bits, i);
456 for (i = lc->header.nr_regions; i < lc->region_count; i++)
457 /* FIXME: amazingly inefficient */
458 log_clear_bit(lc, lc->clean_bits, i);
460 /* clear any old bits -- device has shrunk */
461 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
462 log_clear_bit(lc, lc->clean_bits, i);
464 /* copy clean across to sync */
465 memcpy(lc->sync_bits, lc->clean_bits, size);
466 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
468 /* set the correct number of regions in the header */
469 lc->header.nr_regions = lc->region_count;
471 /* write the new header */
472 return write_header(lc);
475 static uint32_t core_get_region_size(struct dirty_log *log)
477 struct log_c *lc = (struct log_c *) log->context;
478 return lc->region_size;
481 static int core_is_clean(struct dirty_log *log, region_t region)
483 struct log_c *lc = (struct log_c *) log->context;
484 return log_test_bit(lc->clean_bits, region);
487 static int core_in_sync(struct dirty_log *log, region_t region, int block)
489 struct log_c *lc = (struct log_c *) log->context;
490 return log_test_bit(lc->sync_bits, region);
493 static int core_flush(struct dirty_log *log)
499 static int disk_flush(struct dirty_log *log)
502 struct log_c *lc = (struct log_c *) log->context;
504 /* only write if the log has changed */
508 r = write_header(lc);
515 static void core_mark_region(struct dirty_log *log, region_t region)
517 struct log_c *lc = (struct log_c *) log->context;
518 log_clear_bit(lc, lc->clean_bits, region);
521 static void core_clear_region(struct dirty_log *log, region_t region)
523 struct log_c *lc = (struct log_c *) log->context;
524 log_set_bit(lc, lc->clean_bits, region);
527 static int core_get_resync_work(struct dirty_log *log, region_t *region)
529 struct log_c *lc = (struct log_c *) log->context;
531 if (lc->sync_search >= lc->region_count)
535 *region = ext2_find_next_zero_bit(
536 (unsigned long *) lc->sync_bits,
539 lc->sync_search = *region + 1;
541 if (*region >= lc->region_count)
544 } while (log_test_bit(lc->recovering_bits, *region));
546 log_set_bit(lc, lc->recovering_bits, *region);
550 static void core_complete_resync_work(struct dirty_log *log, region_t region,
553 struct log_c *lc = (struct log_c *) log->context;
555 log_clear_bit(lc, lc->recovering_bits, region);
557 log_set_bit(lc, lc->sync_bits, region);
562 static region_t core_get_sync_count(struct dirty_log *log)
564 struct log_c *lc = (struct log_c *) log->context;
566 return lc->sync_count;
569 #define DMEMIT_SYNC \
570 if (lc->sync != DEFAULTSYNC) \
571 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
573 static int core_status(struct dirty_log *log, status_type_t status,
574 char *result, unsigned int maxlen)
577 struct log_c *lc = log->context;
580 case STATUSTYPE_INFO:
583 case STATUSTYPE_TABLE:
584 DMEMIT("%s %u %u ", log->type->name,
585 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
592 static int disk_status(struct dirty_log *log, status_type_t status,
593 char *result, unsigned int maxlen)
597 struct log_c *lc = log->context;
600 case STATUSTYPE_INFO:
603 case STATUSTYPE_TABLE:
604 format_dev_t(buffer, lc->log_dev->bdev->bd_dev);
605 DMEMIT("%s %u %s %u ", log->type->name,
606 lc->sync == DEFAULTSYNC ? 2 : 3, buffer,
614 static struct dirty_log_type _core_type = {
616 .module = THIS_MODULE,
619 .get_region_size = core_get_region_size,
620 .is_clean = core_is_clean,
621 .in_sync = core_in_sync,
623 .mark_region = core_mark_region,
624 .clear_region = core_clear_region,
625 .get_resync_work = core_get_resync_work,
626 .complete_resync_work = core_complete_resync_work,
627 .get_sync_count = core_get_sync_count,
628 .status = core_status,
631 static struct dirty_log_type _disk_type = {
633 .module = THIS_MODULE,
636 .suspend = disk_flush,
637 .resume = disk_resume,
638 .get_region_size = core_get_region_size,
639 .is_clean = core_is_clean,
640 .in_sync = core_in_sync,
642 .mark_region = core_mark_region,
643 .clear_region = core_clear_region,
644 .get_resync_work = core_get_resync_work,
645 .complete_resync_work = core_complete_resync_work,
646 .get_sync_count = core_get_sync_count,
647 .status = disk_status,
650 int __init dm_dirty_log_init(void)
654 r = dm_register_dirty_log_type(&_core_type);
656 DMWARN("couldn't register core log");
658 r = dm_register_dirty_log_type(&_disk_type);
660 DMWARN("couldn't register disk type");
661 dm_unregister_dirty_log_type(&_core_type);
667 void dm_dirty_log_exit(void)
669 dm_unregister_dirty_log_type(&_disk_type);
670 dm_unregister_dirty_log_type(&_core_type);
673 EXPORT_SYMBOL(dm_register_dirty_log_type);
674 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
675 EXPORT_SYMBOL(dm_create_dirty_log);
676 EXPORT_SYMBOL(dm_destroy_dirty_log);