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
;
159 struct io_region bits_location
;
163 * The touched member needs to be updated every time we access
164 * one of the bitsets.
166 static inline int log_test_bit(uint32_t *bs
, unsigned bit
)
168 return ext2_test_bit(bit
, (unsigned long *) bs
) ? 1 : 0;
171 static inline void log_set_bit(struct log_c
*l
,
172 uint32_t *bs
, unsigned bit
)
174 ext2_set_bit(bit
, (unsigned long *) bs
);
178 static inline void log_clear_bit(struct log_c
*l
,
179 uint32_t *bs
, unsigned bit
)
181 ext2_clear_bit(bit
, (unsigned long *) bs
);
185 /*----------------------------------------------------------------
187 *--------------------------------------------------------------*/
188 static void header_to_disk(struct log_header
*core
, struct log_header
*disk
)
190 disk
->magic
= cpu_to_le32(core
->magic
);
191 disk
->version
= cpu_to_le32(core
->version
);
192 disk
->nr_regions
= cpu_to_le64(core
->nr_regions
);
195 static void header_from_disk(struct log_header
*core
, struct log_header
*disk
)
197 core
->magic
= le32_to_cpu(disk
->magic
);
198 core
->version
= le32_to_cpu(disk
->version
);
199 core
->nr_regions
= le64_to_cpu(disk
->nr_regions
);
202 static int read_header(struct log_c
*log
)
207 r
= dm_io_sync_vm(1, &log
->header_location
, READ
,
208 log
->disk_header
, &ebits
);
212 header_from_disk(&log
->header
, log
->disk_header
);
214 /* New log required? */
215 if (log
->sync
!= DEFAULTSYNC
|| log
->header
.magic
!= MIRROR_MAGIC
) {
216 log
->header
.magic
= MIRROR_MAGIC
;
217 log
->header
.version
= MIRROR_DISK_VERSION
;
218 log
->header
.nr_regions
= 0;
221 #ifdef __LITTLE_ENDIAN
222 if (log
->header
.version
== 1)
223 log
->header
.version
= 2;
226 if (log
->header
.version
!= MIRROR_DISK_VERSION
) {
227 DMWARN("incompatible disk log version");
234 static inline int write_header(struct log_c
*log
)
238 header_to_disk(&log
->header
, log
->disk_header
);
239 return dm_io_sync_vm(1, &log
->header_location
, WRITE
,
240 log
->disk_header
, &ebits
);
243 /*----------------------------------------------------------------
245 *--------------------------------------------------------------*/
246 static int read_bits(struct log_c
*log
)
251 r
= dm_io_sync_vm(1, &log
->bits_location
, READ
,
252 log
->clean_bits
, &ebits
);
259 static int write_bits(struct log_c
*log
)
262 return dm_io_sync_vm(1, &log
->bits_location
, WRITE
,
263 log
->clean_bits
, &ebits
);
266 /*----------------------------------------------------------------
267 * core log constructor/destructor
269 * argv contains region_size followed optionally by [no]sync
270 *--------------------------------------------------------------*/
272 static int core_ctr(struct dirty_log
*log
, struct dm_target
*ti
,
273 unsigned int argc
, char **argv
)
275 enum sync sync
= DEFAULTSYNC
;
278 uint32_t region_size
;
279 unsigned int region_count
;
282 if (argc
< 1 || argc
> 2) {
283 DMWARN("wrong number of arguments to mirror log");
288 if (!strcmp(argv
[1], "sync"))
290 else if (!strcmp(argv
[1], "nosync"))
293 DMWARN("unrecognised sync argument to mirror log: %s",
299 if (sscanf(argv
[0], "%u", ®ion_size
) != 1) {
300 DMWARN("invalid region size string");
304 region_count
= dm_sector_div_up(ti
->len
, region_size
);
306 lc
= kmalloc(sizeof(*lc
), GFP_KERNEL
);
308 DMWARN("couldn't allocate core log");
314 lc
->region_size
= region_size
;
315 lc
->region_count
= region_count
;
319 * Work out how many "unsigned long"s we need to hold the bitset.
321 bitset_size
= dm_round_up(region_count
,
322 sizeof(unsigned long) << BYTE_SHIFT
);
323 bitset_size
>>= BYTE_SHIFT
;
325 lc
->bitset_uint32_count
= bitset_size
/ 4;
326 lc
->clean_bits
= vmalloc(bitset_size
);
327 if (!lc
->clean_bits
) {
328 DMWARN("couldn't allocate clean bitset");
332 memset(lc
->clean_bits
, -1, bitset_size
);
334 lc
->sync_bits
= vmalloc(bitset_size
);
335 if (!lc
->sync_bits
) {
336 DMWARN("couldn't allocate sync bitset");
337 vfree(lc
->clean_bits
);
341 memset(lc
->sync_bits
, (sync
== NOSYNC
) ? -1 : 0, bitset_size
);
342 lc
->sync_count
= (sync
== NOSYNC
) ? region_count
: 0;
344 lc
->recovering_bits
= vmalloc(bitset_size
);
345 if (!lc
->recovering_bits
) {
346 DMWARN("couldn't allocate sync bitset");
347 vfree(lc
->sync_bits
);
348 vfree(lc
->clean_bits
);
352 memset(lc
->recovering_bits
, 0, bitset_size
);
358 static void core_dtr(struct dirty_log
*log
)
360 struct log_c
*lc
= (struct log_c
*) log
->context
;
361 vfree(lc
->clean_bits
);
362 vfree(lc
->sync_bits
);
363 vfree(lc
->recovering_bits
);
367 /*----------------------------------------------------------------
368 * disk log constructor/destructor
370 * argv contains log_device region_size followed optionally by [no]sync
371 *--------------------------------------------------------------*/
372 static int disk_ctr(struct dirty_log
*log
, struct dm_target
*ti
,
373 unsigned int argc
, char **argv
)
380 if (argc
< 2 || argc
> 3) {
381 DMWARN("wrong number of arguments to disk mirror log");
385 r
= dm_get_device(ti
, argv
[0], 0, 0 /* FIXME */,
386 FMODE_READ
| FMODE_WRITE
, &dev
);
390 r
= core_ctr(log
, ti
, argc
- 1, argv
+ 1);
392 dm_put_device(ti
, dev
);
396 lc
= (struct log_c
*) log
->context
;
399 /* setup the disk header fields */
400 lc
->header_location
.bdev
= lc
->log_dev
->bdev
;
401 lc
->header_location
.sector
= 0;
402 lc
->header_location
.count
= 1;
405 * We can't read less than this amount, even though we'll
406 * not be using most of this space.
408 lc
->disk_header
= vmalloc(1 << SECTOR_SHIFT
);
409 if (!lc
->disk_header
)
412 /* setup the disk bitset fields */
413 lc
->bits_location
.bdev
= lc
->log_dev
->bdev
;
414 lc
->bits_location
.sector
= LOG_OFFSET
;
416 size
= dm_round_up(lc
->bitset_uint32_count
* sizeof(uint32_t),
418 lc
->bits_location
.count
= size
>> SECTOR_SHIFT
;
422 dm_put_device(ti
, lc
->log_dev
);
427 static void disk_dtr(struct dirty_log
*log
)
429 struct log_c
*lc
= (struct log_c
*) log
->context
;
430 dm_put_device(lc
->ti
, lc
->log_dev
);
431 vfree(lc
->disk_header
);
435 static int count_bits32(uint32_t *addr
, unsigned size
)
439 for (i
= 0; i
< size
; i
++) {
440 count
+= hweight32(*(addr
+i
));
445 static int disk_resume(struct dirty_log
*log
)
449 struct log_c
*lc
= (struct log_c
*) log
->context
;
450 size_t size
= lc
->bitset_uint32_count
* sizeof(uint32_t);
452 /* read the disk header */
462 /* set or clear any new bits */
463 if (lc
->sync
== NOSYNC
)
464 for (i
= lc
->header
.nr_regions
; i
< lc
->region_count
; i
++)
465 /* FIXME: amazingly inefficient */
466 log_set_bit(lc
, lc
->clean_bits
, i
);
468 for (i
= lc
->header
.nr_regions
; i
< lc
->region_count
; i
++)
469 /* FIXME: amazingly inefficient */
470 log_clear_bit(lc
, lc
->clean_bits
, i
);
472 /* copy clean across to sync */
473 memcpy(lc
->sync_bits
, lc
->clean_bits
, size
);
474 lc
->sync_count
= count_bits32(lc
->clean_bits
, lc
->bitset_uint32_count
);
481 /* set the correct number of regions in the header */
482 lc
->header
.nr_regions
= lc
->region_count
;
484 /* write the new header */
485 return write_header(lc
);
488 static uint32_t core_get_region_size(struct dirty_log
*log
)
490 struct log_c
*lc
= (struct log_c
*) log
->context
;
491 return lc
->region_size
;
494 static int core_is_clean(struct dirty_log
*log
, region_t region
)
496 struct log_c
*lc
= (struct log_c
*) log
->context
;
497 return log_test_bit(lc
->clean_bits
, region
);
500 static int core_in_sync(struct dirty_log
*log
, region_t region
, int block
)
502 struct log_c
*lc
= (struct log_c
*) log
->context
;
503 return log_test_bit(lc
->sync_bits
, region
);
506 static int core_flush(struct dirty_log
*log
)
512 static int disk_flush(struct dirty_log
*log
)
515 struct log_c
*lc
= (struct log_c
*) log
->context
;
517 /* only write if the log has changed */
528 static void core_mark_region(struct dirty_log
*log
, region_t region
)
530 struct log_c
*lc
= (struct log_c
*) log
->context
;
531 log_clear_bit(lc
, lc
->clean_bits
, region
);
534 static void core_clear_region(struct dirty_log
*log
, region_t region
)
536 struct log_c
*lc
= (struct log_c
*) log
->context
;
537 log_set_bit(lc
, lc
->clean_bits
, region
);
540 static int core_get_resync_work(struct dirty_log
*log
, region_t
*region
)
542 struct log_c
*lc
= (struct log_c
*) log
->context
;
544 if (lc
->sync_search
>= lc
->region_count
)
548 *region
= ext2_find_next_zero_bit(
549 (unsigned long *) lc
->sync_bits
,
552 lc
->sync_search
= *region
+ 1;
554 if (*region
>= lc
->region_count
)
557 } while (log_test_bit(lc
->recovering_bits
, *region
));
559 log_set_bit(lc
, lc
->recovering_bits
, *region
);
563 static void core_complete_resync_work(struct dirty_log
*log
, region_t region
,
566 struct log_c
*lc
= (struct log_c
*) log
->context
;
568 log_clear_bit(lc
, lc
->recovering_bits
, region
);
570 log_set_bit(lc
, lc
->sync_bits
, region
);
575 static region_t
core_get_sync_count(struct dirty_log
*log
)
577 struct log_c
*lc
= (struct log_c
*) log
->context
;
579 return lc
->sync_count
;
582 #define DMEMIT_SYNC \
583 if (lc->sync != DEFAULTSYNC) \
584 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
586 static int core_status(struct dirty_log
*log
, status_type_t status
,
587 char *result
, unsigned int maxlen
)
590 struct log_c
*lc
= log
->context
;
593 case STATUSTYPE_INFO
:
596 case STATUSTYPE_TABLE
:
597 DMEMIT("%s %u %u ", log
->type
->name
,
598 lc
->sync
== DEFAULTSYNC
? 1 : 2, lc
->region_size
);
605 static int disk_status(struct dirty_log
*log
, status_type_t status
,
606 char *result
, unsigned int maxlen
)
610 struct log_c
*lc
= log
->context
;
613 case STATUSTYPE_INFO
:
616 case STATUSTYPE_TABLE
:
617 format_dev_t(buffer
, lc
->log_dev
->bdev
->bd_dev
);
618 DMEMIT("%s %u %s %u ", log
->type
->name
,
619 lc
->sync
== DEFAULTSYNC
? 2 : 3, buffer
,
627 static struct dirty_log_type _core_type
= {
629 .module
= THIS_MODULE
,
632 .get_region_size
= core_get_region_size
,
633 .is_clean
= core_is_clean
,
634 .in_sync
= core_in_sync
,
636 .mark_region
= core_mark_region
,
637 .clear_region
= core_clear_region
,
638 .get_resync_work
= core_get_resync_work
,
639 .complete_resync_work
= core_complete_resync_work
,
640 .get_sync_count
= core_get_sync_count
,
641 .status
= core_status
,
644 static struct dirty_log_type _disk_type
= {
646 .module
= THIS_MODULE
,
649 .suspend
= disk_flush
,
650 .resume
= disk_resume
,
651 .get_region_size
= core_get_region_size
,
652 .is_clean
= core_is_clean
,
653 .in_sync
= core_in_sync
,
655 .mark_region
= core_mark_region
,
656 .clear_region
= core_clear_region
,
657 .get_resync_work
= core_get_resync_work
,
658 .complete_resync_work
= core_complete_resync_work
,
659 .get_sync_count
= core_get_sync_count
,
660 .status
= disk_status
,
663 int __init
dm_dirty_log_init(void)
667 r
= dm_register_dirty_log_type(&_core_type
);
669 DMWARN("couldn't register core log");
671 r
= dm_register_dirty_log_type(&_disk_type
);
673 DMWARN("couldn't register disk type");
674 dm_unregister_dirty_log_type(&_core_type
);
680 void dm_dirty_log_exit(void)
682 dm_unregister_dirty_log_type(&_disk_type
);
683 dm_unregister_dirty_log_type(&_core_type
);
686 EXPORT_SYMBOL(dm_register_dirty_log_type
);
687 EXPORT_SYMBOL(dm_unregister_dirty_log_type
);
688 EXPORT_SYMBOL(dm_create_dirty_log
);
689 EXPORT_SYMBOL(dm_destroy_dirty_log
);