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 #define DM_MSG_PREFIX "mirror log"
17 static LIST_HEAD(_log_types
);
18 static DEFINE_SPINLOCK(_lock
);
20 int dm_register_dirty_log_type(struct dirty_log_type
*type
)
24 list_add(&type
->list
, &_log_types
);
30 int dm_unregister_dirty_log_type(struct dirty_log_type
*type
)
35 DMWARN("Attempt to unregister a log type that is still in use");
37 list_del(&type
->list
);
44 static struct dirty_log_type
*get_type(const char *type_name
)
46 struct dirty_log_type
*type
;
49 list_for_each_entry (type
, &_log_types
, list
)
50 if (!strcmp(type_name
, type
->name
)) {
51 if (!type
->use_count
&& !try_module_get(type
->module
)){
64 static void put_type(struct dirty_log_type
*type
)
67 if (!--type
->use_count
)
68 module_put(type
->module
);
72 struct dirty_log
*dm_create_dirty_log(const char *type_name
, struct dm_target
*ti
,
73 unsigned int argc
, char **argv
)
75 struct dirty_log_type
*type
;
76 struct dirty_log
*log
;
78 log
= kmalloc(sizeof(*log
), GFP_KERNEL
);
82 type
= get_type(type_name
);
89 if (type
->ctr(log
, ti
, argc
, argv
)) {
98 void dm_destroy_dirty_log(struct dirty_log
*log
)
105 /*-----------------------------------------------------------------
106 * Persistent and core logs share a lot of their implementation.
107 * FIXME: need a reload method to be called from a resume
108 *---------------------------------------------------------------*/
110 * Magic for persistent mirrors: "MiRr"
112 #define MIRROR_MAGIC 0x4D695272
115 * The on-disk version of the metadata.
117 #define MIRROR_DISK_VERSION 2
124 * Simple, incrementing version. no backward
132 struct dm_target
*ti
;
134 uint32_t region_size
;
135 unsigned int region_count
;
138 unsigned bitset_uint32_count
;
139 uint32_t *clean_bits
;
141 uint32_t *recovering_bits
; /* FIXME: this seems excessive */
147 DEFAULTSYNC
, /* Synchronize if necessary */
148 NOSYNC
, /* Devices known to be already in sync */
149 FORCESYNC
, /* Force a sync to happen */
152 struct dm_io_request io_req
;
158 struct dm_dev
*log_dev
;
159 struct log_header header
;
161 struct io_region header_location
;
162 struct log_header
*disk_header
;
166 * The touched member needs to be updated every time we access
167 * one of the bitsets.
169 static inline int log_test_bit(uint32_t *bs
, unsigned bit
)
171 return ext2_test_bit(bit
, (unsigned long *) bs
) ? 1 : 0;
174 static inline void log_set_bit(struct log_c
*l
,
175 uint32_t *bs
, unsigned bit
)
177 ext2_set_bit(bit
, (unsigned long *) bs
);
181 static inline void log_clear_bit(struct log_c
*l
,
182 uint32_t *bs
, unsigned bit
)
184 ext2_clear_bit(bit
, (unsigned long *) bs
);
188 /*----------------------------------------------------------------
190 *--------------------------------------------------------------*/
191 static void header_to_disk(struct log_header
*core
, struct log_header
*disk
)
193 disk
->magic
= cpu_to_le32(core
->magic
);
194 disk
->version
= cpu_to_le32(core
->version
);
195 disk
->nr_regions
= cpu_to_le64(core
->nr_regions
);
198 static void header_from_disk(struct log_header
*core
, struct log_header
*disk
)
200 core
->magic
= le32_to_cpu(disk
->magic
);
201 core
->version
= le32_to_cpu(disk
->version
);
202 core
->nr_regions
= le64_to_cpu(disk
->nr_regions
);
205 static int rw_header(struct log_c
*lc
, int rw
)
207 lc
->io_req
.bi_rw
= rw
;
208 lc
->io_req
.mem
.ptr
.vma
= lc
->disk_header
;
209 lc
->io_req
.notify
.fn
= NULL
;
211 return dm_io(&lc
->io_req
, 1, &lc
->header_location
, NULL
);
214 static int read_header(struct log_c
*log
)
218 r
= rw_header(log
, READ
);
222 header_from_disk(&log
->header
, log
->disk_header
);
224 /* New log required? */
225 if (log
->sync
!= DEFAULTSYNC
|| log
->header
.magic
!= MIRROR_MAGIC
) {
226 log
->header
.magic
= MIRROR_MAGIC
;
227 log
->header
.version
= MIRROR_DISK_VERSION
;
228 log
->header
.nr_regions
= 0;
231 #ifdef __LITTLE_ENDIAN
232 if (log
->header
.version
== 1)
233 log
->header
.version
= 2;
236 if (log
->header
.version
!= MIRROR_DISK_VERSION
) {
237 DMWARN("incompatible disk log version");
244 static inline int write_header(struct log_c
*log
)
246 header_to_disk(&log
->header
, log
->disk_header
);
247 return rw_header(log
, WRITE
);
250 /*----------------------------------------------------------------
251 * core log constructor/destructor
253 * argv contains region_size followed optionally by [no]sync
254 *--------------------------------------------------------------*/
256 static int create_log_context(struct dirty_log
*log
, struct dm_target
*ti
,
257 unsigned int argc
, char **argv
,
260 enum sync sync
= DEFAULTSYNC
;
263 uint32_t region_size
;
264 unsigned int region_count
;
265 size_t bitset_size
, buf_size
;
268 if (argc
< 1 || argc
> 2) {
269 DMWARN("wrong number of arguments to mirror log");
274 if (!strcmp(argv
[1], "sync"))
276 else if (!strcmp(argv
[1], "nosync"))
279 DMWARN("unrecognised sync argument to mirror log: %s",
285 if (sscanf(argv
[0], "%u", ®ion_size
) != 1) {
286 DMWARN("invalid region size string");
290 region_count
= dm_sector_div_up(ti
->len
, region_size
);
292 lc
= kmalloc(sizeof(*lc
), GFP_KERNEL
);
294 DMWARN("couldn't allocate core log");
300 lc
->region_size
= region_size
;
301 lc
->region_count
= region_count
;
305 * Work out how many "unsigned long"s we need to hold the bitset.
307 bitset_size
= dm_round_up(region_count
,
308 sizeof(*lc
->clean_bits
) << BYTE_SHIFT
);
309 bitset_size
>>= BYTE_SHIFT
;
311 lc
->bitset_uint32_count
= bitset_size
/ sizeof(*lc
->clean_bits
);
317 lc
->clean_bits
= vmalloc(bitset_size
);
318 if (!lc
->clean_bits
) {
319 DMWARN("couldn't allocate clean bitset");
323 lc
->disk_header
= NULL
;
326 lc
->log_dev_failed
= 0;
327 lc
->header_location
.bdev
= lc
->log_dev
->bdev
;
328 lc
->header_location
.sector
= 0;
331 * Buffer holds both header and bitset.
333 buf_size
= dm_round_up((LOG_OFFSET
<< SECTOR_SHIFT
) +
334 bitset_size
, ti
->limits
.hardsect_size
);
335 lc
->header_location
.count
= buf_size
>> SECTOR_SHIFT
;
336 lc
->io_req
.mem
.type
= DM_IO_VMA
;
337 lc
->io_req
.client
= dm_io_client_create(dm_div_up(buf_size
,
339 if (IS_ERR(lc
->io_req
.client
)) {
340 r
= PTR_ERR(lc
->io_req
.client
);
341 DMWARN("couldn't allocate disk io client");
346 lc
->disk_header
= vmalloc(buf_size
);
347 if (!lc
->disk_header
) {
348 DMWARN("couldn't allocate disk log buffer");
353 lc
->clean_bits
= (void *)lc
->disk_header
+
354 (LOG_OFFSET
<< SECTOR_SHIFT
);
357 memset(lc
->clean_bits
, -1, bitset_size
);
359 lc
->sync_bits
= vmalloc(bitset_size
);
360 if (!lc
->sync_bits
) {
361 DMWARN("couldn't allocate sync bitset");
363 vfree(lc
->clean_bits
);
364 vfree(lc
->disk_header
);
368 memset(lc
->sync_bits
, (sync
== NOSYNC
) ? -1 : 0, bitset_size
);
369 lc
->sync_count
= (sync
== NOSYNC
) ? region_count
: 0;
371 lc
->recovering_bits
= vmalloc(bitset_size
);
372 if (!lc
->recovering_bits
) {
373 DMWARN("couldn't allocate sync bitset");
374 vfree(lc
->sync_bits
);
376 vfree(lc
->clean_bits
);
377 vfree(lc
->disk_header
);
381 memset(lc
->recovering_bits
, 0, bitset_size
);
388 static int core_ctr(struct dirty_log
*log
, struct dm_target
*ti
,
389 unsigned int argc
, char **argv
)
391 return create_log_context(log
, ti
, argc
, argv
, NULL
);
394 static void destroy_log_context(struct log_c
*lc
)
396 vfree(lc
->sync_bits
);
397 vfree(lc
->recovering_bits
);
401 static void core_dtr(struct dirty_log
*log
)
403 struct log_c
*lc
= (struct log_c
*) log
->context
;
405 vfree(lc
->clean_bits
);
406 destroy_log_context(lc
);
409 /*----------------------------------------------------------------
410 * disk log constructor/destructor
412 * argv contains log_device region_size followed optionally by [no]sync
413 *--------------------------------------------------------------*/
414 static int disk_ctr(struct dirty_log
*log
, struct dm_target
*ti
,
415 unsigned int argc
, char **argv
)
420 if (argc
< 2 || argc
> 3) {
421 DMWARN("wrong number of arguments to disk mirror log");
425 r
= dm_get_device(ti
, argv
[0], 0, 0 /* FIXME */,
426 FMODE_READ
| FMODE_WRITE
, &dev
);
430 r
= create_log_context(log
, ti
, argc
- 1, argv
+ 1, dev
);
432 dm_put_device(ti
, dev
);
439 static void disk_dtr(struct dirty_log
*log
)
441 struct log_c
*lc
= (struct log_c
*) log
->context
;
443 dm_put_device(lc
->ti
, lc
->log_dev
);
444 vfree(lc
->disk_header
);
445 dm_io_client_destroy(lc
->io_req
.client
);
446 destroy_log_context(lc
);
449 static int count_bits32(uint32_t *addr
, unsigned size
)
453 for (i
= 0; i
< size
; i
++) {
454 count
+= hweight32(*(addr
+i
));
459 static void fail_log_device(struct log_c
*lc
)
461 if (lc
->log_dev_failed
)
464 lc
->log_dev_failed
= 1;
465 dm_table_event(lc
->ti
->table
);
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 */
478 DMWARN("%s: Failed to read header on mirror log device",
484 /* set or clear any new bits -- device has grown */
485 if (lc
->sync
== NOSYNC
)
486 for (i
= lc
->header
.nr_regions
; i
< lc
->region_count
; i
++)
487 /* FIXME: amazingly inefficient */
488 log_set_bit(lc
, lc
->clean_bits
, i
);
490 for (i
= lc
->header
.nr_regions
; i
< lc
->region_count
; i
++)
491 /* FIXME: amazingly inefficient */
492 log_clear_bit(lc
, lc
->clean_bits
, i
);
494 /* clear any old bits -- device has shrunk */
495 for (i
= lc
->region_count
; i
% (sizeof(*lc
->clean_bits
) << BYTE_SHIFT
); i
++)
496 log_clear_bit(lc
, lc
->clean_bits
, i
);
498 /* copy clean across to sync */
499 memcpy(lc
->sync_bits
, lc
->clean_bits
, size
);
500 lc
->sync_count
= count_bits32(lc
->clean_bits
, lc
->bitset_uint32_count
);
503 /* set the correct number of regions in the header */
504 lc
->header
.nr_regions
= lc
->region_count
;
506 /* write the new header */
507 r
= write_header(lc
);
509 DMWARN("%s: Failed to write header on mirror log device",
517 static uint32_t core_get_region_size(struct dirty_log
*log
)
519 struct log_c
*lc
= (struct log_c
*) log
->context
;
520 return lc
->region_size
;
523 static int core_resume(struct dirty_log
*log
)
525 struct log_c
*lc
= (struct log_c
*) log
->context
;
530 static int core_is_clean(struct dirty_log
*log
, region_t region
)
532 struct log_c
*lc
= (struct log_c
*) log
->context
;
533 return log_test_bit(lc
->clean_bits
, region
);
536 static int core_in_sync(struct dirty_log
*log
, region_t region
, int block
)
538 struct log_c
*lc
= (struct log_c
*) log
->context
;
539 return log_test_bit(lc
->sync_bits
, region
);
542 static int core_flush(struct dirty_log
*log
)
548 static int disk_flush(struct dirty_log
*log
)
551 struct log_c
*lc
= (struct log_c
*) log
->context
;
553 /* only write if the log has changed */
557 r
= write_header(lc
);
566 static void core_mark_region(struct dirty_log
*log
, region_t region
)
568 struct log_c
*lc
= (struct log_c
*) log
->context
;
569 log_clear_bit(lc
, lc
->clean_bits
, region
);
572 static void core_clear_region(struct dirty_log
*log
, region_t region
)
574 struct log_c
*lc
= (struct log_c
*) log
->context
;
575 log_set_bit(lc
, lc
->clean_bits
, region
);
578 static int core_get_resync_work(struct dirty_log
*log
, region_t
*region
)
580 struct log_c
*lc
= (struct log_c
*) log
->context
;
582 if (lc
->sync_search
>= lc
->region_count
)
586 *region
= ext2_find_next_zero_bit(
587 (unsigned long *) lc
->sync_bits
,
590 lc
->sync_search
= *region
+ 1;
592 if (*region
>= lc
->region_count
)
595 } while (log_test_bit(lc
->recovering_bits
, *region
));
597 log_set_bit(lc
, lc
->recovering_bits
, *region
);
601 static void core_set_region_sync(struct dirty_log
*log
, region_t region
,
604 struct log_c
*lc
= (struct log_c
*) log
->context
;
606 log_clear_bit(lc
, lc
->recovering_bits
, region
);
608 log_set_bit(lc
, lc
->sync_bits
, region
);
610 } else if (log_test_bit(lc
->sync_bits
, region
)) {
612 log_clear_bit(lc
, lc
->sync_bits
, region
);
616 static region_t
core_get_sync_count(struct dirty_log
*log
)
618 struct log_c
*lc
= (struct log_c
*) log
->context
;
620 return lc
->sync_count
;
623 #define DMEMIT_SYNC \
624 if (lc->sync != DEFAULTSYNC) \
625 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
627 static int core_status(struct dirty_log
*log
, status_type_t status
,
628 char *result
, unsigned int maxlen
)
631 struct log_c
*lc
= log
->context
;
634 case STATUSTYPE_INFO
:
635 DMEMIT("1 %s", log
->type
->name
);
638 case STATUSTYPE_TABLE
:
639 DMEMIT("%s %u %u ", log
->type
->name
,
640 lc
->sync
== DEFAULTSYNC
? 1 : 2, lc
->region_size
);
647 static int disk_status(struct dirty_log
*log
, status_type_t status
,
648 char *result
, unsigned int maxlen
)
651 struct log_c
*lc
= log
->context
;
654 case STATUSTYPE_INFO
:
655 DMEMIT("3 %s %s %c", log
->type
->name
, lc
->log_dev
->name
,
656 lc
->log_dev_failed
? 'D' : 'A');
659 case STATUSTYPE_TABLE
:
660 DMEMIT("%s %u %s %u ", log
->type
->name
,
661 lc
->sync
== DEFAULTSYNC
? 2 : 3, lc
->log_dev
->name
,
669 static struct dirty_log_type _core_type
= {
671 .module
= THIS_MODULE
,
674 .resume
= core_resume
,
675 .get_region_size
= core_get_region_size
,
676 .is_clean
= core_is_clean
,
677 .in_sync
= core_in_sync
,
679 .mark_region
= core_mark_region
,
680 .clear_region
= core_clear_region
,
681 .get_resync_work
= core_get_resync_work
,
682 .set_region_sync
= core_set_region_sync
,
683 .get_sync_count
= core_get_sync_count
,
684 .status
= core_status
,
687 static struct dirty_log_type _disk_type
= {
689 .module
= THIS_MODULE
,
692 .suspend
= disk_flush
,
693 .resume
= disk_resume
,
694 .get_region_size
= core_get_region_size
,
695 .is_clean
= core_is_clean
,
696 .in_sync
= core_in_sync
,
698 .mark_region
= core_mark_region
,
699 .clear_region
= core_clear_region
,
700 .get_resync_work
= core_get_resync_work
,
701 .set_region_sync
= core_set_region_sync
,
702 .get_sync_count
= core_get_sync_count
,
703 .status
= disk_status
,
706 int __init
dm_dirty_log_init(void)
710 r
= dm_register_dirty_log_type(&_core_type
);
712 DMWARN("couldn't register core log");
714 r
= dm_register_dirty_log_type(&_disk_type
);
716 DMWARN("couldn't register disk type");
717 dm_unregister_dirty_log_type(&_core_type
);
723 void dm_dirty_log_exit(void)
725 dm_unregister_dirty_log_type(&_disk_type
);
726 dm_unregister_dirty_log_type(&_core_type
);
729 EXPORT_SYMBOL(dm_register_dirty_log_type
);
730 EXPORT_SYMBOL(dm_unregister_dirty_log_type
);
731 EXPORT_SYMBOL(dm_create_dirty_log
);
732 EXPORT_SYMBOL(dm_destroy_dirty_log
);