1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2003 Sistina Software
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
6 * This file is released under the LGPL.
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/vmalloc.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-dirty-log.h>
16 #include <linux/device-mapper.h>
18 #define DM_MSG_PREFIX "dirty region log"
20 static LIST_HEAD(_log_types);
21 static DEFINE_SPINLOCK(_lock);
23 static struct dm_dirty_log_type *__find_dirty_log_type(const char *name)
25 struct dm_dirty_log_type *log_type;
27 list_for_each_entry(log_type, &_log_types, list)
28 if (!strcmp(name, log_type->name))
34 static struct dm_dirty_log_type *_get_dirty_log_type(const char *name)
36 struct dm_dirty_log_type *log_type;
40 log_type = __find_dirty_log_type(name);
41 if (log_type && !try_module_get(log_type->module))
53 * Attempt to retrieve the dm_dirty_log_type by name. If not already
54 * available, attempt to load the appropriate module.
56 * Log modules are named "dm-log-" followed by the 'type_name'.
57 * Modules may contain multiple types.
58 * This function will first try the module "dm-log-<type_name>",
59 * then truncate 'type_name' on the last '-' and try again.
61 * For example, if type_name was "clustered-disk", it would search
62 * 'dm-log-clustered-disk' then 'dm-log-clustered'.
64 * Returns: dirty_log_type* on success, NULL on failure
66 static struct dm_dirty_log_type *get_type(const char *type_name)
68 char *p, *type_name_dup;
69 struct dm_dirty_log_type *log_type;
74 log_type = _get_dirty_log_type(type_name);
78 type_name_dup = kstrdup(type_name, GFP_KERNEL);
80 DMWARN("No memory left to attempt log module load for \"%s\"",
85 while (request_module("dm-log-%s", type_name_dup) ||
86 !(log_type = _get_dirty_log_type(type_name))) {
87 p = strrchr(type_name_dup, '-');
94 DMWARN("Module for logging type \"%s\" not found.", type_name);
101 static void put_type(struct dm_dirty_log_type *type)
107 if (!__find_dirty_log_type(type->name))
110 module_put(type->module);
116 int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
121 if (!__find_dirty_log_type(type->name))
122 list_add(&type->list, &_log_types);
129 EXPORT_SYMBOL(dm_dirty_log_type_register);
131 int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
135 if (!__find_dirty_log_type(type->name)) {
140 list_del(&type->list);
146 EXPORT_SYMBOL(dm_dirty_log_type_unregister);
148 struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
149 struct dm_target *ti,
150 int (*flush_callback_fn)(struct dm_target *ti),
151 unsigned int argc, char **argv)
153 struct dm_dirty_log_type *type;
154 struct dm_dirty_log *log;
156 log = kmalloc(sizeof(*log), GFP_KERNEL);
160 type = get_type(type_name);
166 log->flush_callback_fn = flush_callback_fn;
168 if (type->ctr(log, ti, argc, argv)) {
176 EXPORT_SYMBOL(dm_dirty_log_create);
178 void dm_dirty_log_destroy(struct dm_dirty_log *log)
184 EXPORT_SYMBOL(dm_dirty_log_destroy);
187 *---------------------------------------------------------------
188 * Persistent and core logs share a lot of their implementation.
189 * FIXME: need a reload method to be called from a resume
190 *---------------------------------------------------------------
193 * Magic for persistent mirrors: "MiRr"
195 #define MIRROR_MAGIC 0x4D695272
198 * The on-disk version of the metadata.
200 #define MIRROR_DISK_VERSION 2
203 struct log_header_disk {
207 * Simple, incrementing version. no backward
214 struct log_header_core {
221 struct dm_target *ti;
225 uint32_t region_size;
226 unsigned int region_count;
229 unsigned int bitset_uint32_count;
230 uint32_t *clean_bits;
232 uint32_t *recovering_bits; /* FIXME: this seems excessive */
238 DEFAULTSYNC, /* Synchronize if necessary */
239 NOSYNC, /* Devices known to be already in sync */
240 FORCESYNC, /* Force a sync to happen */
243 struct dm_io_request io_req;
249 int log_dev_flush_failed;
250 struct dm_dev *log_dev;
251 struct log_header_core header;
253 struct dm_io_region header_location;
254 struct log_header_disk *disk_header;
258 * The touched member needs to be updated every time we access
259 * one of the bitsets.
261 static inline int log_test_bit(uint32_t *bs, unsigned int bit)
263 return test_bit_le(bit, bs) ? 1 : 0;
266 static inline void log_set_bit(struct log_c *l,
267 uint32_t *bs, unsigned int bit)
269 __set_bit_le(bit, bs);
270 l->touched_cleaned = 1;
273 static inline void log_clear_bit(struct log_c *l,
274 uint32_t *bs, unsigned int bit)
276 __clear_bit_le(bit, bs);
277 l->touched_dirtied = 1;
281 *---------------------------------------------------------------
283 *--------------------------------------------------------------
285 static void header_to_disk(struct log_header_core *core, struct log_header_disk *disk)
287 disk->magic = cpu_to_le32(core->magic);
288 disk->version = cpu_to_le32(core->version);
289 disk->nr_regions = cpu_to_le64(core->nr_regions);
292 static void header_from_disk(struct log_header_core *core, struct log_header_disk *disk)
294 core->magic = le32_to_cpu(disk->magic);
295 core->version = le32_to_cpu(disk->version);
296 core->nr_regions = le64_to_cpu(disk->nr_regions);
299 static int rw_header(struct log_c *lc, enum req_op op)
301 lc->io_req.bi_opf = op;
303 return dm_io(&lc->io_req, 1, &lc->header_location, NULL, IOPRIO_DEFAULT);
306 static int flush_header(struct log_c *lc)
308 struct dm_io_region null_location = {
309 .bdev = lc->header_location.bdev,
314 lc->io_req.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
316 return dm_io(&lc->io_req, 1, &null_location, NULL, IOPRIO_DEFAULT);
319 static int read_header(struct log_c *log)
323 r = rw_header(log, REQ_OP_READ);
327 header_from_disk(&log->header, log->disk_header);
329 /* New log required? */
330 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
331 log->header.magic = MIRROR_MAGIC;
332 log->header.version = MIRROR_DISK_VERSION;
333 log->header.nr_regions = 0;
336 #ifdef __LITTLE_ENDIAN
337 if (log->header.version == 1)
338 log->header.version = 2;
341 if (log->header.version != MIRROR_DISK_VERSION) {
342 DMWARN("incompatible disk log version");
349 static int _check_region_size(struct dm_target *ti, uint32_t region_size)
351 if (region_size < 2 || region_size > ti->len)
354 if (!is_power_of_2(region_size))
361 *--------------------------------------------------------------
362 * core log constructor/destructor
364 * argv contains region_size followed optionally by [no]sync
365 *--------------------------------------------------------------
368 static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
369 unsigned int argc, char **argv,
372 enum sync sync = DEFAULTSYNC;
375 uint32_t region_size;
376 unsigned int region_count;
377 size_t bitset_size, buf_size;
381 if (argc < 1 || argc > 2) {
382 DMWARN("wrong number of arguments to dirty region log");
387 if (!strcmp(argv[1], "sync"))
389 else if (!strcmp(argv[1], "nosync"))
392 DMWARN("unrecognised sync argument to dirty region log: %s", argv[1]);
397 if (sscanf(argv[0], "%u%c", ®ion_size, &dummy) != 1 ||
398 !_check_region_size(ti, region_size)) {
399 DMWARN("invalid region size %s", argv[0]);
403 region_count = dm_sector_div_up(ti->len, region_size);
405 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
407 DMWARN("couldn't allocate core log");
412 lc->touched_dirtied = 0;
413 lc->touched_cleaned = 0;
414 lc->flush_failed = 0;
415 lc->region_size = region_size;
416 lc->region_count = region_count;
420 * Work out how many "unsigned long"s we need to hold the bitset.
422 bitset_size = dm_round_up(region_count, BITS_PER_LONG);
423 bitset_size >>= BYTE_SHIFT;
425 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
431 lc->clean_bits = vmalloc(bitset_size);
432 if (!lc->clean_bits) {
433 DMWARN("couldn't allocate clean bitset");
437 lc->disk_header = NULL;
440 lc->log_dev_failed = 0;
441 lc->log_dev_flush_failed = 0;
442 lc->header_location.bdev = lc->log_dev->bdev;
443 lc->header_location.sector = 0;
446 * Buffer holds both header and bitset.
449 dm_round_up((LOG_OFFSET << SECTOR_SHIFT) + bitset_size,
450 bdev_logical_block_size(lc->header_location.bdev));
452 if (buf_size > bdev_nr_bytes(dev->bdev)) {
453 DMWARN("log device %s too small: need %llu bytes",
454 dev->name, (unsigned long long)buf_size);
459 lc->header_location.count = buf_size >> SECTOR_SHIFT;
461 lc->io_req.mem.type = DM_IO_VMA;
462 lc->io_req.notify.fn = NULL;
463 lc->io_req.client = dm_io_client_create();
464 if (IS_ERR(lc->io_req.client)) {
465 r = PTR_ERR(lc->io_req.client);
466 DMWARN("couldn't allocate disk io client");
471 lc->disk_header = vmalloc(buf_size);
472 if (!lc->disk_header) {
473 DMWARN("couldn't allocate disk log buffer");
474 dm_io_client_destroy(lc->io_req.client);
479 lc->io_req.mem.ptr.vma = lc->disk_header;
480 lc->clean_bits = (void *)lc->disk_header +
481 (LOG_OFFSET << SECTOR_SHIFT);
484 memset(lc->clean_bits, -1, bitset_size);
486 lc->sync_bits = vmalloc(bitset_size);
487 if (!lc->sync_bits) {
488 DMWARN("couldn't allocate sync bitset");
490 vfree(lc->clean_bits);
492 dm_io_client_destroy(lc->io_req.client);
493 vfree(lc->disk_header);
497 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
498 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
500 lc->recovering_bits = vzalloc(bitset_size);
501 if (!lc->recovering_bits) {
502 DMWARN("couldn't allocate sync bitset");
503 vfree(lc->sync_bits);
505 vfree(lc->clean_bits);
507 dm_io_client_destroy(lc->io_req.client);
508 vfree(lc->disk_header);
518 static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
519 unsigned int argc, char **argv)
521 return create_log_context(log, ti, argc, argv, NULL);
524 static void destroy_log_context(struct log_c *lc)
526 vfree(lc->sync_bits);
527 vfree(lc->recovering_bits);
531 static void core_dtr(struct dm_dirty_log *log)
533 struct log_c *lc = log->context;
535 vfree(lc->clean_bits);
536 destroy_log_context(lc);
540 *---------------------------------------------------------------------
541 * disk log constructor/destructor
543 * argv contains log_device region_size followed optionally by [no]sync
544 *---------------------------------------------------------------------
546 static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
547 unsigned int argc, char **argv)
552 if (argc < 2 || argc > 3) {
553 DMWARN("wrong number of arguments to disk dirty region log");
557 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
561 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
563 dm_put_device(ti, dev);
570 static void disk_dtr(struct dm_dirty_log *log)
572 struct log_c *lc = log->context;
574 dm_put_device(lc->ti, lc->log_dev);
575 vfree(lc->disk_header);
576 dm_io_client_destroy(lc->io_req.client);
577 destroy_log_context(lc);
580 static void fail_log_device(struct log_c *lc)
582 if (lc->log_dev_failed)
585 lc->log_dev_failed = 1;
586 dm_table_event(lc->ti->table);
589 static int disk_resume(struct dm_dirty_log *log)
593 struct log_c *lc = log->context;
594 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
596 /* read the disk header */
599 DMWARN("%s: Failed to read header on dirty region log device",
603 * If the log device cannot be read, we must assume
604 * all regions are out-of-sync. If we simply return
605 * here, the state will be uninitialized and could
606 * lead us to return 'in-sync' status for regions
607 * that are actually 'out-of-sync'.
609 lc->header.nr_regions = 0;
612 /* set or clear any new bits -- device has grown */
613 if (lc->sync == NOSYNC)
614 for (i = lc->header.nr_regions; i < lc->region_count; i++)
615 /* FIXME: amazingly inefficient */
616 log_set_bit(lc, lc->clean_bits, i);
618 for (i = lc->header.nr_regions; i < lc->region_count; i++)
619 /* FIXME: amazingly inefficient */
620 log_clear_bit(lc, lc->clean_bits, i);
622 /* clear any old bits -- device has shrunk */
623 for (i = lc->region_count; i % BITS_PER_LONG; i++)
624 log_clear_bit(lc, lc->clean_bits, i);
626 /* copy clean across to sync */
627 memcpy(lc->sync_bits, lc->clean_bits, size);
628 lc->sync_count = memweight(lc->clean_bits,
629 lc->bitset_uint32_count * sizeof(uint32_t));
632 /* set the correct number of regions in the header */
633 lc->header.nr_regions = lc->region_count;
635 header_to_disk(&lc->header, lc->disk_header);
637 /* write the new header */
638 r = rw_header(lc, REQ_OP_WRITE);
640 r = flush_header(lc);
642 lc->log_dev_flush_failed = 1;
645 DMWARN("%s: Failed to write header on dirty region log device",
653 static uint32_t core_get_region_size(struct dm_dirty_log *log)
655 struct log_c *lc = log->context;
657 return lc->region_size;
660 static int core_resume(struct dm_dirty_log *log)
662 struct log_c *lc = log->context;
668 static int core_is_clean(struct dm_dirty_log *log, region_t region)
670 struct log_c *lc = log->context;
672 return log_test_bit(lc->clean_bits, region);
675 static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
677 struct log_c *lc = log->context;
679 return log_test_bit(lc->sync_bits, region);
682 static int core_flush(struct dm_dirty_log *log)
688 static int disk_flush(struct dm_dirty_log *log)
691 struct log_c *lc = log->context;
693 /* only write if the log has changed */
694 if (!lc->touched_cleaned && !lc->touched_dirtied)
697 if (lc->touched_cleaned && log->flush_callback_fn &&
698 log->flush_callback_fn(lc->ti)) {
700 * At this point it is impossible to determine which
701 * regions are clean and which are dirty (without
702 * re-reading the log off disk). So mark all of them
705 lc->flush_failed = 1;
706 for (i = 0; i < lc->region_count; i++)
707 log_clear_bit(lc, lc->clean_bits, i);
710 r = rw_header(lc, REQ_OP_WRITE);
714 if (lc->touched_dirtied) {
715 r = flush_header(lc);
717 lc->log_dev_flush_failed = 1;
720 lc->touched_dirtied = 0;
722 lc->touched_cleaned = 0;
728 static void core_mark_region(struct dm_dirty_log *log, region_t region)
730 struct log_c *lc = log->context;
732 log_clear_bit(lc, lc->clean_bits, region);
735 static void core_clear_region(struct dm_dirty_log *log, region_t region)
737 struct log_c *lc = log->context;
739 if (likely(!lc->flush_failed))
740 log_set_bit(lc, lc->clean_bits, region);
743 static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
745 struct log_c *lc = log->context;
747 if (lc->sync_search >= lc->region_count)
751 *region = find_next_zero_bit_le(lc->sync_bits,
754 lc->sync_search = *region + 1;
756 if (*region >= lc->region_count)
759 } while (log_test_bit(lc->recovering_bits, *region));
761 log_set_bit(lc, lc->recovering_bits, *region);
765 static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
768 struct log_c *lc = log->context;
770 log_clear_bit(lc, lc->recovering_bits, region);
772 log_set_bit(lc, lc->sync_bits, region);
774 } else if (log_test_bit(lc->sync_bits, region)) {
776 log_clear_bit(lc, lc->sync_bits, region);
780 static region_t core_get_sync_count(struct dm_dirty_log *log)
782 struct log_c *lc = log->context;
784 return lc->sync_count;
787 #define DMEMIT_SYNC \
789 if (lc->sync != DEFAULTSYNC) \
790 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : ""); \
793 static int core_status(struct dm_dirty_log *log, status_type_t status,
794 char *result, unsigned int maxlen)
797 struct log_c *lc = log->context;
800 case STATUSTYPE_INFO:
801 DMEMIT("1 %s", log->type->name);
804 case STATUSTYPE_TABLE:
805 DMEMIT("%s %u %u ", log->type->name,
806 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
818 static int disk_status(struct dm_dirty_log *log, status_type_t status,
819 char *result, unsigned int maxlen)
822 struct log_c *lc = log->context;
825 case STATUSTYPE_INFO:
826 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
827 lc->log_dev_flush_failed ? 'F' :
828 lc->log_dev_failed ? 'D' :
832 case STATUSTYPE_TABLE:
833 DMEMIT("%s %u %s %u ", log->type->name,
834 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
847 static struct dm_dirty_log_type _core_type = {
849 .module = THIS_MODULE,
852 .resume = core_resume,
853 .get_region_size = core_get_region_size,
854 .is_clean = core_is_clean,
855 .in_sync = core_in_sync,
857 .mark_region = core_mark_region,
858 .clear_region = core_clear_region,
859 .get_resync_work = core_get_resync_work,
860 .set_region_sync = core_set_region_sync,
861 .get_sync_count = core_get_sync_count,
862 .status = core_status,
865 static struct dm_dirty_log_type _disk_type = {
867 .module = THIS_MODULE,
870 .postsuspend = disk_flush,
871 .resume = disk_resume,
872 .get_region_size = core_get_region_size,
873 .is_clean = core_is_clean,
874 .in_sync = core_in_sync,
876 .mark_region = core_mark_region,
877 .clear_region = core_clear_region,
878 .get_resync_work = core_get_resync_work,
879 .set_region_sync = core_set_region_sync,
880 .get_sync_count = core_get_sync_count,
881 .status = disk_status,
884 static int __init dm_dirty_log_init(void)
888 r = dm_dirty_log_type_register(&_core_type);
890 DMWARN("couldn't register core log");
892 r = dm_dirty_log_type_register(&_disk_type);
894 DMWARN("couldn't register disk type");
895 dm_dirty_log_type_unregister(&_core_type);
901 static void __exit dm_dirty_log_exit(void)
903 dm_dirty_log_type_unregister(&_disk_type);
904 dm_dirty_log_type_unregister(&_core_type);
907 module_init(dm_dirty_log_init);
908 module_exit(dm_dirty_log_exit);
910 MODULE_DESCRIPTION(DM_NAME " dirty region log");
912 MODULE_LICENSE("GPL");