[PATCH] dm mirror log: sector size fix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-log.c
blobae41628b75c013c08f18c7a327bf5f71d41e99f3
1 /*
2 * Copyright (C) 2003 Sistina Software
4 * This file is released under the LGPL.
5 */
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/vmalloc.h>
12 #include "dm-log.h"
13 #include "dm-io.h"
15 static LIST_HEAD(_log_types);
16 static DEFINE_SPINLOCK(_lock);
18 int dm_register_dirty_log_type(struct dirty_log_type *type)
20 spin_lock(&_lock);
21 type->use_count = 0;
22 list_add(&type->list, &_log_types);
23 spin_unlock(&_lock);
25 return 0;
28 int dm_unregister_dirty_log_type(struct dirty_log_type *type)
30 spin_lock(&_lock);
32 if (type->use_count)
33 DMWARN("Attempt to unregister a log type that is still in use");
34 else
35 list_del(&type->list);
37 spin_unlock(&_lock);
39 return 0;
42 static struct dirty_log_type *get_type(const char *type_name)
44 struct dirty_log_type *type;
46 spin_lock(&_lock);
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)){
50 spin_unlock(&_lock);
51 return NULL;
53 type->use_count++;
54 spin_unlock(&_lock);
55 return type;
58 spin_unlock(&_lock);
59 return NULL;
62 static void put_type(struct dirty_log_type *type)
64 spin_lock(&_lock);
65 if (!--type->use_count)
66 module_put(type->module);
67 spin_unlock(&_lock);
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);
77 if (!log)
78 return NULL;
80 type = get_type(type_name);
81 if (!type) {
82 kfree(log);
83 return NULL;
86 log->type = type;
87 if (type->ctr(log, ti, argc, argv)) {
88 kfree(log);
89 put_type(type);
90 return NULL;
93 return log;
96 void dm_destroy_dirty_log(struct dirty_log *log)
98 log->type->dtr(log);
99 put_type(log->type);
100 kfree(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
116 #define LOG_OFFSET 2
118 struct log_header {
119 uint32_t magic;
122 * Simple, incrementing version. no backward
123 * compatibility.
125 uint32_t version;
126 sector_t nr_regions;
129 struct log_c {
130 struct dm_target *ti;
131 int touched;
132 uint32_t region_size;
133 unsigned int region_count;
134 region_t sync_count;
136 unsigned bitset_uint32_count;
137 uint32_t *clean_bits;
138 uint32_t *sync_bits;
139 uint32_t *recovering_bits; /* FIXME: this seems excessive */
141 int sync_search;
143 /* Resync flag */
144 enum sync {
145 DEFAULTSYNC, /* Synchronize if necessary */
146 NOSYNC, /* Devices known to be already in sync */
147 FORCESYNC, /* Force a sync to happen */
148 } sync;
151 * Disk log fields
153 struct dm_dev *log_dev;
154 struct log_header header;
156 struct io_region header_location;
157 struct log_header *disk_header;
161 * The touched member needs to be updated every time we access
162 * one of the bitsets.
164 static inline int log_test_bit(uint32_t *bs, unsigned bit)
166 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
169 static inline void log_set_bit(struct log_c *l,
170 uint32_t *bs, unsigned bit)
172 ext2_set_bit(bit, (unsigned long *) bs);
173 l->touched = 1;
176 static inline void log_clear_bit(struct log_c *l,
177 uint32_t *bs, unsigned bit)
179 ext2_clear_bit(bit, (unsigned long *) bs);
180 l->touched = 1;
183 /*----------------------------------------------------------------
184 * Header IO
185 *--------------------------------------------------------------*/
186 static void header_to_disk(struct log_header *core, struct log_header *disk)
188 disk->magic = cpu_to_le32(core->magic);
189 disk->version = cpu_to_le32(core->version);
190 disk->nr_regions = cpu_to_le64(core->nr_regions);
193 static void header_from_disk(struct log_header *core, struct log_header *disk)
195 core->magic = le32_to_cpu(disk->magic);
196 core->version = le32_to_cpu(disk->version);
197 core->nr_regions = le64_to_cpu(disk->nr_regions);
200 static int read_header(struct log_c *log)
202 int r;
203 unsigned long ebits;
205 r = dm_io_sync_vm(1, &log->header_location, READ,
206 log->disk_header, &ebits);
207 if (r)
208 return r;
210 header_from_disk(&log->header, log->disk_header);
212 /* New log required? */
213 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
214 log->header.magic = MIRROR_MAGIC;
215 log->header.version = MIRROR_DISK_VERSION;
216 log->header.nr_regions = 0;
219 #ifdef __LITTLE_ENDIAN
220 if (log->header.version == 1)
221 log->header.version = 2;
222 #endif
224 if (log->header.version != MIRROR_DISK_VERSION) {
225 DMWARN("incompatible disk log version");
226 return -EINVAL;
229 return 0;
232 static inline int write_header(struct log_c *log)
234 unsigned long ebits;
236 header_to_disk(&log->header, log->disk_header);
237 return dm_io_sync_vm(1, &log->header_location, WRITE,
238 log->disk_header, &ebits);
241 /*----------------------------------------------------------------
242 * core log constructor/destructor
244 * argv contains region_size followed optionally by [no]sync
245 *--------------------------------------------------------------*/
246 #define BYTE_SHIFT 3
247 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
248 unsigned int argc, char **argv)
250 enum sync sync = DEFAULTSYNC;
252 struct log_c *lc;
253 uint32_t region_size;
254 unsigned int region_count;
255 size_t bitset_size;
257 if (argc < 1 || argc > 2) {
258 DMWARN("wrong number of arguments to mirror log");
259 return -EINVAL;
262 if (argc > 1) {
263 if (!strcmp(argv[1], "sync"))
264 sync = FORCESYNC;
265 else if (!strcmp(argv[1], "nosync"))
266 sync = NOSYNC;
267 else {
268 DMWARN("unrecognised sync argument to mirror log: %s",
269 argv[1]);
270 return -EINVAL;
274 if (sscanf(argv[0], "%u", &region_size) != 1) {
275 DMWARN("invalid region size string");
276 return -EINVAL;
279 region_count = dm_sector_div_up(ti->len, region_size);
281 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
282 if (!lc) {
283 DMWARN("couldn't allocate core log");
284 return -ENOMEM;
287 lc->ti = ti;
288 lc->touched = 0;
289 lc->region_size = region_size;
290 lc->region_count = region_count;
291 lc->sync = sync;
294 * Work out how many "unsigned long"s we need to hold the bitset.
296 bitset_size = dm_round_up(region_count,
297 sizeof(unsigned long) << BYTE_SHIFT);
298 bitset_size >>= BYTE_SHIFT;
300 lc->bitset_uint32_count = bitset_size / 4;
301 lc->clean_bits = vmalloc(bitset_size);
302 if (!lc->clean_bits) {
303 DMWARN("couldn't allocate clean bitset");
304 kfree(lc);
305 return -ENOMEM;
307 memset(lc->clean_bits, -1, bitset_size);
309 lc->sync_bits = vmalloc(bitset_size);
310 if (!lc->sync_bits) {
311 DMWARN("couldn't allocate sync bitset");
312 vfree(lc->clean_bits);
313 kfree(lc);
314 return -ENOMEM;
316 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
317 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
319 lc->recovering_bits = vmalloc(bitset_size);
320 if (!lc->recovering_bits) {
321 DMWARN("couldn't allocate sync bitset");
322 vfree(lc->sync_bits);
323 vfree(lc->clean_bits);
324 kfree(lc);
325 return -ENOMEM;
327 memset(lc->recovering_bits, 0, bitset_size);
328 lc->sync_search = 0;
329 log->context = lc;
330 return 0;
333 static void core_dtr(struct dirty_log *log)
335 struct log_c *lc = (struct log_c *) log->context;
336 vfree(lc->clean_bits);
337 vfree(lc->sync_bits);
338 vfree(lc->recovering_bits);
339 kfree(lc);
342 /*----------------------------------------------------------------
343 * disk log constructor/destructor
345 * argv contains log_device region_size followed optionally by [no]sync
346 *--------------------------------------------------------------*/
347 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
348 unsigned int argc, char **argv)
350 int r;
351 size_t size, bitset_size;
352 struct log_c *lc;
353 struct dm_dev *dev;
354 uint32_t *clean_bits;
356 if (argc < 2 || argc > 3) {
357 DMWARN("wrong number of arguments to disk mirror log");
358 return -EINVAL;
361 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
362 FMODE_READ | FMODE_WRITE, &dev);
363 if (r)
364 return r;
366 r = core_ctr(log, ti, argc - 1, argv + 1);
367 if (r) {
368 dm_put_device(ti, dev);
369 return r;
372 lc = (struct log_c *) log->context;
373 lc->log_dev = dev;
375 /* setup the disk header fields */
376 lc->header_location.bdev = lc->log_dev->bdev;
377 lc->header_location.sector = 0;
379 /* Include both the header and the bitset in one buffer. */
380 bitset_size = lc->bitset_uint32_count * sizeof(uint32_t);
381 size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) + bitset_size,
382 ti->limits.hardsect_size);
383 lc->header_location.count = size >> SECTOR_SHIFT;
385 lc->disk_header = vmalloc(size);
386 if (!lc->disk_header)
387 goto bad;
390 * Deallocate the clean_bits buffer that was allocated in core_ctr()
391 * and point it at the appropriate place in the disk_header buffer.
393 clean_bits = lc->clean_bits;
394 lc->clean_bits = (void *)lc->disk_header + (LOG_OFFSET << SECTOR_SHIFT);
395 memcpy(lc->clean_bits, clean_bits, bitset_size);
396 vfree(clean_bits);
398 return 0;
400 bad:
401 dm_put_device(ti, lc->log_dev);
402 core_dtr(log);
403 return -ENOMEM;
406 static void disk_dtr(struct dirty_log *log)
408 struct log_c *lc = (struct log_c *) log->context;
409 dm_put_device(lc->ti, lc->log_dev);
410 vfree(lc->disk_header);
411 lc->clean_bits = NULL;
412 core_dtr(log);
415 static int count_bits32(uint32_t *addr, unsigned size)
417 int count = 0, i;
419 for (i = 0; i < size; i++) {
420 count += hweight32(*(addr+i));
422 return count;
425 static int disk_resume(struct dirty_log *log)
427 int r;
428 unsigned i;
429 struct log_c *lc = (struct log_c *) log->context;
430 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
432 /* read the disk header */
433 r = read_header(lc);
434 if (r)
435 return r;
437 /* set or clear any new bits */
438 if (lc->sync == NOSYNC)
439 for (i = lc->header.nr_regions; i < lc->region_count; i++)
440 /* FIXME: amazingly inefficient */
441 log_set_bit(lc, lc->clean_bits, i);
442 else
443 for (i = lc->header.nr_regions; i < lc->region_count; i++)
444 /* FIXME: amazingly inefficient */
445 log_clear_bit(lc, lc->clean_bits, i);
447 /* copy clean across to sync */
448 memcpy(lc->sync_bits, lc->clean_bits, size);
449 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
451 /* set the correct number of regions in the header */
452 lc->header.nr_regions = lc->region_count;
454 /* write the new header */
455 return write_header(lc);
458 static uint32_t core_get_region_size(struct dirty_log *log)
460 struct log_c *lc = (struct log_c *) log->context;
461 return lc->region_size;
464 static int core_is_clean(struct dirty_log *log, region_t region)
466 struct log_c *lc = (struct log_c *) log->context;
467 return log_test_bit(lc->clean_bits, region);
470 static int core_in_sync(struct dirty_log *log, region_t region, int block)
472 struct log_c *lc = (struct log_c *) log->context;
473 return log_test_bit(lc->sync_bits, region);
476 static int core_flush(struct dirty_log *log)
478 /* no op */
479 return 0;
482 static int disk_flush(struct dirty_log *log)
484 int r;
485 struct log_c *lc = (struct log_c *) log->context;
487 /* only write if the log has changed */
488 if (!lc->touched)
489 return 0;
491 r = write_header(lc);
492 if (!r)
493 lc->touched = 0;
495 return r;
498 static void core_mark_region(struct dirty_log *log, region_t region)
500 struct log_c *lc = (struct log_c *) log->context;
501 log_clear_bit(lc, lc->clean_bits, region);
504 static void core_clear_region(struct dirty_log *log, region_t region)
506 struct log_c *lc = (struct log_c *) log->context;
507 log_set_bit(lc, lc->clean_bits, region);
510 static int core_get_resync_work(struct dirty_log *log, region_t *region)
512 struct log_c *lc = (struct log_c *) log->context;
514 if (lc->sync_search >= lc->region_count)
515 return 0;
517 do {
518 *region = ext2_find_next_zero_bit(
519 (unsigned long *) lc->sync_bits,
520 lc->region_count,
521 lc->sync_search);
522 lc->sync_search = *region + 1;
524 if (*region >= lc->region_count)
525 return 0;
527 } while (log_test_bit(lc->recovering_bits, *region));
529 log_set_bit(lc, lc->recovering_bits, *region);
530 return 1;
533 static void core_complete_resync_work(struct dirty_log *log, region_t region,
534 int success)
536 struct log_c *lc = (struct log_c *) log->context;
538 log_clear_bit(lc, lc->recovering_bits, region);
539 if (success) {
540 log_set_bit(lc, lc->sync_bits, region);
541 lc->sync_count++;
545 static region_t core_get_sync_count(struct dirty_log *log)
547 struct log_c *lc = (struct log_c *) log->context;
549 return lc->sync_count;
552 #define DMEMIT_SYNC \
553 if (lc->sync != DEFAULTSYNC) \
554 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
556 static int core_status(struct dirty_log *log, status_type_t status,
557 char *result, unsigned int maxlen)
559 int sz = 0;
560 struct log_c *lc = log->context;
562 switch(status) {
563 case STATUSTYPE_INFO:
564 break;
566 case STATUSTYPE_TABLE:
567 DMEMIT("%s %u %u ", log->type->name,
568 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
569 DMEMIT_SYNC;
572 return sz;
575 static int disk_status(struct dirty_log *log, status_type_t status,
576 char *result, unsigned int maxlen)
578 int sz = 0;
579 char buffer[16];
580 struct log_c *lc = log->context;
582 switch(status) {
583 case STATUSTYPE_INFO:
584 break;
586 case STATUSTYPE_TABLE:
587 format_dev_t(buffer, lc->log_dev->bdev->bd_dev);
588 DMEMIT("%s %u %s %u ", log->type->name,
589 lc->sync == DEFAULTSYNC ? 2 : 3, buffer,
590 lc->region_size);
591 DMEMIT_SYNC;
594 return sz;
597 static struct dirty_log_type _core_type = {
598 .name = "core",
599 .module = THIS_MODULE,
600 .ctr = core_ctr,
601 .dtr = core_dtr,
602 .get_region_size = core_get_region_size,
603 .is_clean = core_is_clean,
604 .in_sync = core_in_sync,
605 .flush = core_flush,
606 .mark_region = core_mark_region,
607 .clear_region = core_clear_region,
608 .get_resync_work = core_get_resync_work,
609 .complete_resync_work = core_complete_resync_work,
610 .get_sync_count = core_get_sync_count,
611 .status = core_status,
614 static struct dirty_log_type _disk_type = {
615 .name = "disk",
616 .module = THIS_MODULE,
617 .ctr = disk_ctr,
618 .dtr = disk_dtr,
619 .suspend = disk_flush,
620 .resume = disk_resume,
621 .get_region_size = core_get_region_size,
622 .is_clean = core_is_clean,
623 .in_sync = core_in_sync,
624 .flush = disk_flush,
625 .mark_region = core_mark_region,
626 .clear_region = core_clear_region,
627 .get_resync_work = core_get_resync_work,
628 .complete_resync_work = core_complete_resync_work,
629 .get_sync_count = core_get_sync_count,
630 .status = disk_status,
633 int __init dm_dirty_log_init(void)
635 int r;
637 r = dm_register_dirty_log_type(&_core_type);
638 if (r)
639 DMWARN("couldn't register core log");
641 r = dm_register_dirty_log_type(&_disk_type);
642 if (r) {
643 DMWARN("couldn't register disk type");
644 dm_unregister_dirty_log_type(&_core_type);
647 return r;
650 void dm_dirty_log_exit(void)
652 dm_unregister_dirty_log_type(&_disk_type);
653 dm_unregister_dirty_log_type(&_core_type);
656 EXPORT_SYMBOL(dm_register_dirty_log_type);
657 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
658 EXPORT_SYMBOL(dm_create_dirty_log);
659 EXPORT_SYMBOL(dm_destroy_dirty_log);