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 spinlock_t _lock = SPIN_LOCK_UNLOCKED;
18 int dm_register_dirty_log_type(struct dirty_log_type *type)
20 if (!try_module_get(type->module))
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);
39 module_put(type->module);
47 static struct dirty_log_type *get_type(const char *type_name)
49 struct dirty_log_type *type;
52 list_for_each_entry (type, &_log_types, list)
53 if (!strcmp(type_name, type->name)) {
63 static void put_type(struct dirty_log_type *type)
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 1
122 * Simple, incrementing version. no backward
130 struct dm_target *ti;
132 sector_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 */
146 struct dm_dev *log_dev;
147 struct log_header header;
149 struct io_region header_location;
150 struct log_header *disk_header;
152 struct io_region bits_location;
157 * The touched member needs to be updated every time we access
158 * one of the bitsets.
160 static inline int log_test_bit(uint32_t *bs, unsigned bit)
162 return test_bit(bit, (unsigned long *) bs) ? 1 : 0;
165 static inline void log_set_bit(struct log_c *l,
166 uint32_t *bs, unsigned bit)
168 set_bit(bit, (unsigned long *) bs);
172 static inline void log_clear_bit(struct log_c *l,
173 uint32_t *bs, unsigned bit)
175 clear_bit(bit, (unsigned long *) bs);
179 /*----------------------------------------------------------------
181 *--------------------------------------------------------------*/
182 static void header_to_disk(struct log_header *core, struct log_header *disk)
184 disk->magic = cpu_to_le32(core->magic);
185 disk->version = cpu_to_le32(core->version);
186 disk->nr_regions = cpu_to_le64(core->nr_regions);
189 static void header_from_disk(struct log_header *core, struct log_header *disk)
191 core->magic = le32_to_cpu(disk->magic);
192 core->version = le32_to_cpu(disk->version);
193 core->nr_regions = le64_to_cpu(disk->nr_regions);
196 static int read_header(struct log_c *log)
201 r = dm_io_sync_vm(1, &log->header_location, READ,
202 log->disk_header, &ebits);
206 header_from_disk(&log->header, log->disk_header);
208 if (log->header.magic != MIRROR_MAGIC) {
209 log->header.magic = MIRROR_MAGIC;
210 log->header.version = MIRROR_DISK_VERSION;
211 log->header.nr_regions = 0;
214 if (log->header.version != MIRROR_DISK_VERSION) {
215 DMWARN("incompatible disk log version");
222 static inline int write_header(struct log_c *log)
226 header_to_disk(&log->header, log->disk_header);
227 return dm_io_sync_vm(1, &log->header_location, WRITE,
228 log->disk_header, &ebits);
231 /*----------------------------------------------------------------
233 *--------------------------------------------------------------*/
234 static inline void bits_to_core(uint32_t *core, uint32_t *disk, unsigned count)
238 for (i = 0; i < count; i++)
239 core[i] = le32_to_cpu(disk[i]);
242 static inline void bits_to_disk(uint32_t *core, uint32_t *disk, unsigned count)
246 /* copy across the clean/dirty bitset */
247 for (i = 0; i < count; i++)
248 disk[i] = cpu_to_le32(core[i]);
251 static int read_bits(struct log_c *log)
256 r = dm_io_sync_vm(1, &log->bits_location, READ,
257 log->disk_bits, &ebits);
261 bits_to_core(log->clean_bits, log->disk_bits,
262 log->bitset_uint32_count);
266 static int write_bits(struct log_c *log)
269 bits_to_disk(log->clean_bits, log->disk_bits,
270 log->bitset_uint32_count);
271 return dm_io_sync_vm(1, &log->bits_location, WRITE,
272 log->disk_bits, &ebits);
275 /*----------------------------------------------------------------
276 * constructor/destructor
277 *--------------------------------------------------------------*/
279 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
280 unsigned int argc, char **argv)
283 sector_t region_size;
284 unsigned int region_count;
288 DMWARN("wrong number of arguments to log_c");
292 if (sscanf(argv[0], SECTOR_FORMAT, ®ion_size) != 1) {
293 DMWARN("invalid region size string");
297 region_count = dm_div_up(ti->len, region_size);
299 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
301 DMWARN("couldn't allocate core log");
307 lc->region_size = region_size;
308 lc->region_count = region_count;
311 * Work out how many words we need to hold the bitset.
313 bitset_size = dm_round_up(region_count,
314 sizeof(*lc->clean_bits) << BYTE_SHIFT);
315 bitset_size >>= BYTE_SHIFT;
317 lc->bitset_uint32_count = bitset_size / 4;
318 lc->clean_bits = vmalloc(bitset_size);
319 if (!lc->clean_bits) {
320 DMWARN("couldn't allocate clean bitset");
324 memset(lc->clean_bits, -1, bitset_size);
326 lc->sync_bits = vmalloc(bitset_size);
327 if (!lc->sync_bits) {
328 DMWARN("couldn't allocate sync bitset");
329 vfree(lc->clean_bits);
333 memset(lc->sync_bits, 0, bitset_size);
336 lc->recovering_bits = vmalloc(bitset_size);
337 if (!lc->recovering_bits) {
338 DMWARN("couldn't allocate sync bitset");
339 vfree(lc->sync_bits);
340 vfree(lc->clean_bits);
344 memset(lc->recovering_bits, 0, bitset_size);
350 static void core_dtr(struct dirty_log *log)
352 struct log_c *lc = (struct log_c *) log->context;
353 vfree(lc->clean_bits);
354 vfree(lc->sync_bits);
355 vfree(lc->recovering_bits);
359 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
360 unsigned int argc, char **argv)
368 DMWARN("wrong number of arguments to log_d");
372 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
373 FMODE_READ | FMODE_WRITE, &dev);
377 r = core_ctr(log, ti, argc - 1, argv + 1);
379 dm_put_device(ti, dev);
383 lc = (struct log_c *) log->context;
386 /* setup the disk header fields */
387 lc->header_location.bdev = lc->log_dev->bdev;
388 lc->header_location.sector = 0;
389 lc->header_location.count = 1;
392 * We can't read less than this amount, even though we'll
393 * not be using most of this space.
395 lc->disk_header = vmalloc(1 << SECTOR_SHIFT);
396 if (!lc->disk_header)
399 /* setup the disk bitset fields */
400 lc->bits_location.bdev = lc->log_dev->bdev;
401 lc->bits_location.sector = LOG_OFFSET;
403 size = dm_round_up(lc->bitset_uint32_count * sizeof(uint32_t),
405 lc->bits_location.count = size >> SECTOR_SHIFT;
406 lc->disk_bits = vmalloc(size);
407 if (!lc->disk_bits) {
408 vfree(lc->disk_header);
414 dm_put_device(ti, lc->log_dev);
419 static void disk_dtr(struct dirty_log *log)
421 struct log_c *lc = (struct log_c *) log->context;
422 dm_put_device(lc->ti, lc->log_dev);
423 vfree(lc->disk_header);
424 vfree(lc->disk_bits);
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 */
455 /* zero any new bits if the mirror has grown */
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 /* copy clean across to sync */
461 memcpy(lc->sync_bits, lc->clean_bits, size);
462 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
469 /* set the correct number of regions in the header */
470 lc->header.nr_regions = lc->region_count;
472 /* write the new header */
473 return write_header(lc);
476 static sector_t core_get_region_size(struct dirty_log *log)
478 struct log_c *lc = (struct log_c *) log->context;
479 return lc->region_size;
482 static int core_is_clean(struct dirty_log *log, region_t region)
484 struct log_c *lc = (struct log_c *) log->context;
485 return log_test_bit(lc->clean_bits, region);
488 static int core_in_sync(struct dirty_log *log, region_t region, int block)
490 struct log_c *lc = (struct log_c *) log->context;
491 return log_test_bit(lc->sync_bits, region);
494 static int core_flush(struct dirty_log *log)
500 static int disk_flush(struct dirty_log *log)
503 struct log_c *lc = (struct log_c *) log->context;
505 /* only write if the log has changed */
516 static void core_mark_region(struct dirty_log *log, region_t region)
518 struct log_c *lc = (struct log_c *) log->context;
519 log_clear_bit(lc, lc->clean_bits, region);
522 static void core_clear_region(struct dirty_log *log, region_t region)
524 struct log_c *lc = (struct log_c *) log->context;
525 log_set_bit(lc, lc->clean_bits, region);
528 static int core_get_resync_work(struct dirty_log *log, region_t *region)
530 struct log_c *lc = (struct log_c *) log->context;
532 if (lc->sync_search >= lc->region_count)
536 *region = find_next_zero_bit((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 static struct dirty_log_type _core_type = {
571 .module = THIS_MODULE,
574 .get_region_size = core_get_region_size,
575 .is_clean = core_is_clean,
576 .in_sync = core_in_sync,
578 .mark_region = core_mark_region,
579 .clear_region = core_clear_region,
580 .get_resync_work = core_get_resync_work,
581 .complete_resync_work = core_complete_resync_work,
582 .get_sync_count = core_get_sync_count
585 static struct dirty_log_type _disk_type = {
587 .module = THIS_MODULE,
590 .suspend = disk_flush,
591 .resume = disk_resume,
592 .get_region_size = core_get_region_size,
593 .is_clean = core_is_clean,
594 .in_sync = core_in_sync,
596 .mark_region = core_mark_region,
597 .clear_region = core_clear_region,
598 .get_resync_work = core_get_resync_work,
599 .complete_resync_work = core_complete_resync_work,
600 .get_sync_count = core_get_sync_count
603 int __init dm_dirty_log_init(void)
607 r = dm_register_dirty_log_type(&_core_type);
609 DMWARN("couldn't register core log");
611 r = dm_register_dirty_log_type(&_disk_type);
613 DMWARN("couldn't register disk type");
614 dm_unregister_dirty_log_type(&_core_type);
620 void dm_dirty_log_exit(void)
622 dm_unregister_dirty_log_type(&_disk_type);
623 dm_unregister_dirty_log_type(&_core_type);
626 EXPORT_SYMBOL(dm_register_dirty_log_type);
627 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
628 EXPORT_SYMBOL(dm_create_dirty_log);
629 EXPORT_SYMBOL(dm_destroy_dirty_log);