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 1
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
;
159 struct io_region bits_location
;
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 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 set_bit(bit
, (unsigned long *) bs
);
179 static inline void log_clear_bit(struct log_c
*l
,
180 uint32_t *bs
, unsigned bit
)
182 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 if (log
->header
.version
!= MIRROR_DISK_VERSION
) {
223 DMWARN("incompatible disk log version");
230 static inline int write_header(struct log_c
*log
)
234 header_to_disk(&log
->header
, log
->disk_header
);
235 return dm_io_sync_vm(1, &log
->header_location
, WRITE
,
236 log
->disk_header
, &ebits
);
239 /*----------------------------------------------------------------
241 *--------------------------------------------------------------*/
242 static inline void bits_to_core(uint32_t *core
, uint32_t *disk
, unsigned count
)
246 for (i
= 0; i
< count
; i
++)
247 core
[i
] = le32_to_cpu(disk
[i
]);
250 static inline void bits_to_disk(uint32_t *core
, uint32_t *disk
, unsigned count
)
254 /* copy across the clean/dirty bitset */
255 for (i
= 0; i
< count
; i
++)
256 disk
[i
] = cpu_to_le32(core
[i
]);
259 static int read_bits(struct log_c
*log
)
264 r
= dm_io_sync_vm(1, &log
->bits_location
, READ
,
265 log
->disk_bits
, &ebits
);
269 bits_to_core(log
->clean_bits
, log
->disk_bits
,
270 log
->bitset_uint32_count
);
274 static int write_bits(struct log_c
*log
)
277 bits_to_disk(log
->clean_bits
, log
->disk_bits
,
278 log
->bitset_uint32_count
);
279 return dm_io_sync_vm(1, &log
->bits_location
, WRITE
,
280 log
->disk_bits
, &ebits
);
283 /*----------------------------------------------------------------
284 * core log constructor/destructor
286 * argv contains region_size followed optionally by [no]sync
287 *--------------------------------------------------------------*/
289 static int core_ctr(struct dirty_log
*log
, struct dm_target
*ti
,
290 unsigned int argc
, char **argv
)
292 enum sync sync
= DEFAULTSYNC
;
295 uint32_t region_size
;
296 unsigned int region_count
;
299 if (argc
< 1 || argc
> 2) {
300 DMWARN("wrong number of arguments to mirror log");
305 if (!strcmp(argv
[1], "sync"))
307 else if (!strcmp(argv
[1], "nosync"))
310 DMWARN("unrecognised sync argument to mirror log: %s",
316 if (sscanf(argv
[0], "%u", ®ion_size
) != 1) {
317 DMWARN("invalid region size string");
321 region_count
= dm_sector_div_up(ti
->len
, region_size
);
323 lc
= kmalloc(sizeof(*lc
), GFP_KERNEL
);
325 DMWARN("couldn't allocate core log");
331 lc
->region_size
= region_size
;
332 lc
->region_count
= region_count
;
336 * Work out how many words we need to hold the bitset.
338 bitset_size
= dm_round_up(region_count
,
339 sizeof(*lc
->clean_bits
) << BYTE_SHIFT
);
340 bitset_size
>>= BYTE_SHIFT
;
342 lc
->bitset_uint32_count
= bitset_size
/ 4;
343 lc
->clean_bits
= vmalloc(bitset_size
);
344 if (!lc
->clean_bits
) {
345 DMWARN("couldn't allocate clean bitset");
349 memset(lc
->clean_bits
, -1, bitset_size
);
351 lc
->sync_bits
= vmalloc(bitset_size
);
352 if (!lc
->sync_bits
) {
353 DMWARN("couldn't allocate sync bitset");
354 vfree(lc
->clean_bits
);
358 memset(lc
->sync_bits
, (sync
== NOSYNC
) ? -1 : 0, bitset_size
);
359 lc
->sync_count
= (sync
== NOSYNC
) ? region_count
: 0;
361 lc
->recovering_bits
= vmalloc(bitset_size
);
362 if (!lc
->recovering_bits
) {
363 DMWARN("couldn't allocate sync bitset");
364 vfree(lc
->sync_bits
);
365 vfree(lc
->clean_bits
);
369 memset(lc
->recovering_bits
, 0, bitset_size
);
375 static void core_dtr(struct dirty_log
*log
)
377 struct log_c
*lc
= (struct log_c
*) log
->context
;
378 vfree(lc
->clean_bits
);
379 vfree(lc
->sync_bits
);
380 vfree(lc
->recovering_bits
);
384 /*----------------------------------------------------------------
385 * disk log constructor/destructor
387 * argv contains log_device region_size followed optionally by [no]sync
388 *--------------------------------------------------------------*/
389 static int disk_ctr(struct dirty_log
*log
, struct dm_target
*ti
,
390 unsigned int argc
, char **argv
)
397 if (argc
< 2 || argc
> 3) {
398 DMWARN("wrong number of arguments to disk mirror log");
402 r
= dm_get_device(ti
, argv
[0], 0, 0 /* FIXME */,
403 FMODE_READ
| FMODE_WRITE
, &dev
);
407 r
= core_ctr(log
, ti
, argc
- 1, argv
+ 1);
409 dm_put_device(ti
, dev
);
413 lc
= (struct log_c
*) log
->context
;
416 /* setup the disk header fields */
417 lc
->header_location
.bdev
= lc
->log_dev
->bdev
;
418 lc
->header_location
.sector
= 0;
419 lc
->header_location
.count
= 1;
422 * We can't read less than this amount, even though we'll
423 * not be using most of this space.
425 lc
->disk_header
= vmalloc(1 << SECTOR_SHIFT
);
426 if (!lc
->disk_header
)
429 /* setup the disk bitset fields */
430 lc
->bits_location
.bdev
= lc
->log_dev
->bdev
;
431 lc
->bits_location
.sector
= LOG_OFFSET
;
433 size
= dm_round_up(lc
->bitset_uint32_count
* sizeof(uint32_t),
435 lc
->bits_location
.count
= size
>> SECTOR_SHIFT
;
436 lc
->disk_bits
= vmalloc(size
);
437 if (!lc
->disk_bits
) {
438 vfree(lc
->disk_header
);
444 dm_put_device(ti
, lc
->log_dev
);
449 static void disk_dtr(struct dirty_log
*log
)
451 struct log_c
*lc
= (struct log_c
*) log
->context
;
452 dm_put_device(lc
->ti
, lc
->log_dev
);
453 vfree(lc
->disk_header
);
454 vfree(lc
->disk_bits
);
458 static int count_bits32(uint32_t *addr
, unsigned size
)
462 for (i
= 0; i
< size
; i
++) {
463 count
+= hweight32(*(addr
+i
));
468 static int disk_resume(struct dirty_log
*log
)
472 struct log_c
*lc
= (struct log_c
*) log
->context
;
473 size_t size
= lc
->bitset_uint32_count
* sizeof(uint32_t);
475 /* read the disk header */
485 /* set or clear any new bits */
486 if (lc
->sync
== NOSYNC
)
487 for (i
= lc
->header
.nr_regions
; i
< lc
->region_count
; i
++)
488 /* FIXME: amazingly inefficient */
489 log_set_bit(lc
, lc
->clean_bits
, i
);
491 for (i
= lc
->header
.nr_regions
; i
< lc
->region_count
; i
++)
492 /* FIXME: amazingly inefficient */
493 log_clear_bit(lc
, lc
->clean_bits
, i
);
495 /* copy clean across to sync */
496 memcpy(lc
->sync_bits
, lc
->clean_bits
, size
);
497 lc
->sync_count
= count_bits32(lc
->clean_bits
, lc
->bitset_uint32_count
);
504 /* set the correct number of regions in the header */
505 lc
->header
.nr_regions
= lc
->region_count
;
507 /* write the new header */
508 return write_header(lc
);
511 static uint32_t core_get_region_size(struct dirty_log
*log
)
513 struct log_c
*lc
= (struct log_c
*) log
->context
;
514 return lc
->region_size
;
517 static int core_is_clean(struct dirty_log
*log
, region_t region
)
519 struct log_c
*lc
= (struct log_c
*) log
->context
;
520 return log_test_bit(lc
->clean_bits
, region
);
523 static int core_in_sync(struct dirty_log
*log
, region_t region
, int block
)
525 struct log_c
*lc
= (struct log_c
*) log
->context
;
526 return log_test_bit(lc
->sync_bits
, region
);
529 static int core_flush(struct dirty_log
*log
)
535 static int disk_flush(struct dirty_log
*log
)
538 struct log_c
*lc
= (struct log_c
*) log
->context
;
540 /* only write if the log has changed */
551 static void core_mark_region(struct dirty_log
*log
, region_t region
)
553 struct log_c
*lc
= (struct log_c
*) log
->context
;
554 log_clear_bit(lc
, lc
->clean_bits
, region
);
557 static void core_clear_region(struct dirty_log
*log
, region_t region
)
559 struct log_c
*lc
= (struct log_c
*) log
->context
;
560 log_set_bit(lc
, lc
->clean_bits
, region
);
563 static int core_get_resync_work(struct dirty_log
*log
, region_t
*region
)
565 struct log_c
*lc
= (struct log_c
*) log
->context
;
567 if (lc
->sync_search
>= lc
->region_count
)
571 *region
= find_next_zero_bit((unsigned long *) lc
->sync_bits
,
574 lc
->sync_search
= *region
+ 1;
576 if (*region
== lc
->region_count
)
579 } while (log_test_bit(lc
->recovering_bits
, *region
));
581 log_set_bit(lc
, lc
->recovering_bits
, *region
);
585 static void core_complete_resync_work(struct dirty_log
*log
, region_t region
,
588 struct log_c
*lc
= (struct log_c
*) log
->context
;
590 log_clear_bit(lc
, lc
->recovering_bits
, region
);
592 log_set_bit(lc
, lc
->sync_bits
, region
);
597 static region_t
core_get_sync_count(struct dirty_log
*log
)
599 struct log_c
*lc
= (struct log_c
*) log
->context
;
601 return lc
->sync_count
;
604 #define DMEMIT_SYNC \
605 if (lc->sync != DEFAULTSYNC) \
606 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
608 static int core_status(struct dirty_log
*log
, status_type_t status
,
609 char *result
, unsigned int maxlen
)
612 struct log_c
*lc
= log
->context
;
615 case STATUSTYPE_INFO
:
618 case STATUSTYPE_TABLE
:
619 DMEMIT("%s %u %u ", log
->type
->name
,
620 lc
->sync
== DEFAULTSYNC
? 1 : 2, lc
->region_size
);
627 static int disk_status(struct dirty_log
*log
, status_type_t status
,
628 char *result
, unsigned int maxlen
)
632 struct log_c
*lc
= log
->context
;
635 case STATUSTYPE_INFO
:
638 case STATUSTYPE_TABLE
:
639 format_dev_t(buffer
, lc
->log_dev
->bdev
->bd_dev
);
640 DMEMIT("%s %u %s %u ", log
->type
->name
,
641 lc
->sync
== DEFAULTSYNC
? 2 : 3, buffer
,
649 static struct dirty_log_type _core_type
= {
651 .module
= THIS_MODULE
,
654 .get_region_size
= core_get_region_size
,
655 .is_clean
= core_is_clean
,
656 .in_sync
= core_in_sync
,
658 .mark_region
= core_mark_region
,
659 .clear_region
= core_clear_region
,
660 .get_resync_work
= core_get_resync_work
,
661 .complete_resync_work
= core_complete_resync_work
,
662 .get_sync_count
= core_get_sync_count
,
663 .status
= core_status
,
666 static struct dirty_log_type _disk_type
= {
668 .module
= THIS_MODULE
,
671 .suspend
= disk_flush
,
672 .resume
= disk_resume
,
673 .get_region_size
= core_get_region_size
,
674 .is_clean
= core_is_clean
,
675 .in_sync
= core_in_sync
,
677 .mark_region
= core_mark_region
,
678 .clear_region
= core_clear_region
,
679 .get_resync_work
= core_get_resync_work
,
680 .complete_resync_work
= core_complete_resync_work
,
681 .get_sync_count
= core_get_sync_count
,
682 .status
= disk_status
,
685 int __init
dm_dirty_log_init(void)
689 r
= dm_register_dirty_log_type(&_core_type
);
691 DMWARN("couldn't register core log");
693 r
= dm_register_dirty_log_type(&_disk_type
);
695 DMWARN("couldn't register disk type");
696 dm_unregister_dirty_log_type(&_core_type
);
702 void dm_dirty_log_exit(void)
704 dm_unregister_dirty_log_type(&_disk_type
);
705 dm_unregister_dirty_log_type(&_core_type
);
708 EXPORT_SYMBOL(dm_register_dirty_log_type
);
709 EXPORT_SYMBOL(dm_unregister_dirty_log_type
);
710 EXPORT_SYMBOL(dm_create_dirty_log
);
711 EXPORT_SYMBOL(dm_destroy_dirty_log
);