blob: 33d80721667cdb37b371f7e030e6548b2f03ec27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software
3 *
4 * This file is released under the LGPL.
5 */
6
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/module.h>
10#include <linux/vmalloc.h>
11
12#include "dm-log.h"
13#include "dm-io.h"
14
15static LIST_HEAD(_log_types);
16static DEFINE_SPINLOCK(_lock);
17
18int dm_register_dirty_log_type(struct dirty_log_type *type)
19{
20 spin_lock(&_lock);
21 type->use_count = 0;
22 list_add(&type->list, &_log_types);
23 spin_unlock(&_lock);
24
25 return 0;
26}
27
28int dm_unregister_dirty_log_type(struct dirty_log_type *type)
29{
30 spin_lock(&_lock);
31
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);
36
37 spin_unlock(&_lock);
38
39 return 0;
40}
41
42static struct dirty_log_type *get_type(const char *type_name)
43{
44 struct dirty_log_type *type;
45
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;
52 }
53 type->use_count++;
54 spin_unlock(&_lock);
55 return type;
56 }
57
58 spin_unlock(&_lock);
59 return NULL;
60}
61
62static void put_type(struct dirty_log_type *type)
63{
64 spin_lock(&_lock);
65 if (!--type->use_count)
66 module_put(type->module);
67 spin_unlock(&_lock);
68}
69
70struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
71 unsigned int argc, char **argv)
72{
73 struct dirty_log_type *type;
74 struct dirty_log *log;
75
76 log = kmalloc(sizeof(*log), GFP_KERNEL);
77 if (!log)
78 return NULL;
79
80 type = get_type(type_name);
81 if (!type) {
82 kfree(log);
83 return NULL;
84 }
85
86 log->type = type;
87 if (type->ctr(log, ti, argc, argv)) {
88 kfree(log);
89 put_type(type);
90 return NULL;
91 }
92
93 return log;
94}
95
96void dm_destroy_dirty_log(struct dirty_log *log)
97{
98 log->type->dtr(log);
99 put_type(log->type);
100 kfree(log);
101}
102
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 *---------------------------------------------------------------*/
107/*
108 * Magic for persistent mirrors: "MiRr"
109 */
110#define MIRROR_MAGIC 0x4D695272
111
112/*
113 * The on-disk version of the metadata.
114 */
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800115#define MIRROR_DISK_VERSION 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define LOG_OFFSET 2
117
118struct log_header {
119 uint32_t magic;
120
121 /*
122 * Simple, incrementing version. no backward
123 * compatibility.
124 */
125 uint32_t version;
126 sector_t nr_regions;
127};
128
129struct 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;
135
136 unsigned bitset_uint32_count;
137 uint32_t *clean_bits;
138 uint32_t *sync_bits;
139 uint32_t *recovering_bits; /* FIXME: this seems excessive */
140
141 int sync_search;
142
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;
149
150 /*
151 * Disk log fields
152 */
153 struct dm_dev *log_dev;
154 struct log_header header;
155
156 struct io_region header_location;
157 struct log_header *disk_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160/*
161 * The touched member needs to be updated every time we access
162 * one of the bitsets.
163 */
164static inline int log_test_bit(uint32_t *bs, unsigned bit)
165{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800166 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169static inline void log_set_bit(struct log_c *l,
170 uint32_t *bs, unsigned bit)
171{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800172 ext2_set_bit(bit, (unsigned long *) bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 l->touched = 1;
174}
175
176static inline void log_clear_bit(struct log_c *l,
177 uint32_t *bs, unsigned bit)
178{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800179 ext2_clear_bit(bit, (unsigned long *) bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 l->touched = 1;
181}
182
183/*----------------------------------------------------------------
184 * Header IO
185 *--------------------------------------------------------------*/
186static void header_to_disk(struct log_header *core, struct log_header *disk)
187{
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);
191}
192
193static void header_from_disk(struct log_header *core, struct log_header *disk)
194{
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);
198}
199
200static int read_header(struct log_c *log)
201{
202 int r;
203 unsigned long ebits;
204
205 r = dm_io_sync_vm(1, &log->header_location, READ,
206 log->disk_header, &ebits);
207 if (r)
208 return r;
209
210 header_from_disk(&log->header, log->disk_header);
211
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;
217 }
218
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800219#ifdef __LITTLE_ENDIAN
220 if (log->header.version == 1)
221 log->header.version = 2;
222#endif
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (log->header.version != MIRROR_DISK_VERSION) {
225 DMWARN("incompatible disk log version");
226 return -EINVAL;
227 }
228
229 return 0;
230}
231
232static inline int write_header(struct log_c *log)
233{
234 unsigned long ebits;
235
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);
239}
240
241/*----------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * core log constructor/destructor
243 *
244 * argv contains region_size followed optionally by [no]sync
245 *--------------------------------------------------------------*/
246#define BYTE_SHIFT 3
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700247static int create_log_context(struct dirty_log *log, struct dm_target *ti,
248 unsigned int argc, char **argv,
249 struct dm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 enum sync sync = DEFAULTSYNC;
252
253 struct log_c *lc;
254 uint32_t region_size;
255 unsigned int region_count;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700256 size_t bitset_size, buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (argc < 1 || argc > 2) {
259 DMWARN("wrong number of arguments to mirror log");
260 return -EINVAL;
261 }
262
263 if (argc > 1) {
264 if (!strcmp(argv[1], "sync"))
265 sync = FORCESYNC;
266 else if (!strcmp(argv[1], "nosync"))
267 sync = NOSYNC;
268 else {
269 DMWARN("unrecognised sync argument to mirror log: %s",
270 argv[1]);
271 return -EINVAL;
272 }
273 }
274
275 if (sscanf(argv[0], "%u", &region_size) != 1) {
276 DMWARN("invalid region size string");
277 return -EINVAL;
278 }
279
280 region_count = dm_sector_div_up(ti->len, region_size);
281
282 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
283 if (!lc) {
284 DMWARN("couldn't allocate core log");
285 return -ENOMEM;
286 }
287
288 lc->ti = ti;
289 lc->touched = 0;
290 lc->region_size = region_size;
291 lc->region_count = region_count;
292 lc->sync = sync;
293
294 /*
Alasdair G Kergon0e568222005-11-21 21:32:34 -0800295 * Work out how many "unsigned long"s we need to hold the bitset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
297 bitset_size = dm_round_up(region_count,
Alasdair G Kergon29121bd2006-06-26 00:27:29 -0700298 sizeof(*lc->clean_bits) << BYTE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 bitset_size >>= BYTE_SHIFT;
300
Alasdair G Kergon29121bd2006-06-26 00:27:29 -0700301 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700302
303 /*
304 * Disk log?
305 */
306 if (!dev) {
307 lc->clean_bits = vmalloc(bitset_size);
308 if (!lc->clean_bits) {
309 DMWARN("couldn't allocate clean bitset");
310 kfree(lc);
311 return -ENOMEM;
312 }
313 lc->disk_header = NULL;
314 } else {
315 lc->log_dev = dev;
316 lc->header_location.bdev = lc->log_dev->bdev;
317 lc->header_location.sector = 0;
318
319 /*
320 * Buffer holds both header and bitset.
321 */
322 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
323 bitset_size, ti->limits.hardsect_size);
324 lc->header_location.count = buf_size >> SECTOR_SHIFT;
325
326 lc->disk_header = vmalloc(buf_size);
327 if (!lc->disk_header) {
328 DMWARN("couldn't allocate disk log buffer");
329 kfree(lc);
330 return -ENOMEM;
331 }
332
333 lc->clean_bits = (void *)lc->disk_header +
334 (LOG_OFFSET << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 memset(lc->clean_bits, -1, bitset_size);
338
339 lc->sync_bits = vmalloc(bitset_size);
340 if (!lc->sync_bits) {
341 DMWARN("couldn't allocate sync bitset");
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700342 if (!dev)
343 vfree(lc->clean_bits);
344 vfree(lc->disk_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 kfree(lc);
346 return -ENOMEM;
347 }
348 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
349 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
350
351 lc->recovering_bits = vmalloc(bitset_size);
352 if (!lc->recovering_bits) {
353 DMWARN("couldn't allocate sync bitset");
354 vfree(lc->sync_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700355 if (!dev)
356 vfree(lc->clean_bits);
357 vfree(lc->disk_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 kfree(lc);
359 return -ENOMEM;
360 }
361 memset(lc->recovering_bits, 0, bitset_size);
362 lc->sync_search = 0;
363 log->context = lc;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700368static int core_ctr(struct dirty_log *log, struct dm_target *ti,
369 unsigned int argc, char **argv)
370{
371 return create_log_context(log, ti, argc, argv, NULL);
372}
373
374static void destroy_log_context(struct log_c *lc)
375{
376 vfree(lc->sync_bits);
377 vfree(lc->recovering_bits);
378 kfree(lc);
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static void core_dtr(struct dirty_log *log)
382{
383 struct log_c *lc = (struct log_c *) log->context;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 vfree(lc->clean_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700386 destroy_log_context(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*----------------------------------------------------------------
390 * disk log constructor/destructor
391 *
392 * argv contains log_device region_size followed optionally by [no]sync
393 *--------------------------------------------------------------*/
394static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
395 unsigned int argc, char **argv)
396{
397 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct dm_dev *dev;
399
400 if (argc < 2 || argc > 3) {
401 DMWARN("wrong number of arguments to disk mirror log");
402 return -EINVAL;
403 }
404
405 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
406 FMODE_READ | FMODE_WRITE, &dev);
407 if (r)
408 return r;
409
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700410 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (r) {
412 dm_put_device(ti, dev);
413 return r;
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static void disk_dtr(struct dirty_log *log)
420{
421 struct log_c *lc = (struct log_c *) log->context;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dm_put_device(lc->ti, lc->log_dev);
424 vfree(lc->disk_header);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700425 destroy_log_context(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static int count_bits32(uint32_t *addr, unsigned size)
429{
430 int count = 0, i;
431
432 for (i = 0; i < size; i++) {
433 count += hweight32(*(addr+i));
434 }
435 return count;
436}
437
438static int disk_resume(struct dirty_log *log)
439{
440 int r;
441 unsigned i;
442 struct log_c *lc = (struct log_c *) log->context;
443 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
444
445 /* read the disk header */
446 r = read_header(lc);
447 if (r)
448 return r;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* set or clear any new bits */
451 if (lc->sync == NOSYNC)
452 for (i = lc->header.nr_regions; i < lc->region_count; i++)
453 /* FIXME: amazingly inefficient */
454 log_set_bit(lc, lc->clean_bits, i);
455 else
456 for (i = lc->header.nr_regions; i < lc->region_count; i++)
457 /* FIXME: amazingly inefficient */
458 log_clear_bit(lc, lc->clean_bits, i);
459
460 /* copy clean across to sync */
461 memcpy(lc->sync_bits, lc->clean_bits, size);
462 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* set the correct number of regions in the header */
465 lc->header.nr_regions = lc->region_count;
466
467 /* write the new header */
468 return write_header(lc);
469}
470
471static uint32_t core_get_region_size(struct dirty_log *log)
472{
473 struct log_c *lc = (struct log_c *) log->context;
474 return lc->region_size;
475}
476
477static int core_is_clean(struct dirty_log *log, region_t region)
478{
479 struct log_c *lc = (struct log_c *) log->context;
480 return log_test_bit(lc->clean_bits, region);
481}
482
483static int core_in_sync(struct dirty_log *log, region_t region, int block)
484{
485 struct log_c *lc = (struct log_c *) log->context;
486 return log_test_bit(lc->sync_bits, region);
487}
488
489static int core_flush(struct dirty_log *log)
490{
491 /* no op */
492 return 0;
493}
494
495static int disk_flush(struct dirty_log *log)
496{
497 int r;
498 struct log_c *lc = (struct log_c *) log->context;
499
500 /* only write if the log has changed */
501 if (!lc->touched)
502 return 0;
503
Kevin Corry702ca6f2006-06-26 00:27:28 -0700504 r = write_header(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!r)
506 lc->touched = 0;
507
508 return r;
509}
510
511static void core_mark_region(struct dirty_log *log, region_t region)
512{
513 struct log_c *lc = (struct log_c *) log->context;
514 log_clear_bit(lc, lc->clean_bits, region);
515}
516
517static void core_clear_region(struct dirty_log *log, region_t region)
518{
519 struct log_c *lc = (struct log_c *) log->context;
520 log_set_bit(lc, lc->clean_bits, region);
521}
522
523static int core_get_resync_work(struct dirty_log *log, region_t *region)
524{
525 struct log_c *lc = (struct log_c *) log->context;
526
527 if (lc->sync_search >= lc->region_count)
528 return 0;
529
530 do {
Stefan Bader1113a7e2006-02-02 14:28:07 -0800531 *region = ext2_find_next_zero_bit(
532 (unsigned long *) lc->sync_bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 lc->region_count,
534 lc->sync_search);
535 lc->sync_search = *region + 1;
536
Darrick J. Wongac81b2e2006-01-06 00:20:11 -0800537 if (*region >= lc->region_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539
540 } while (log_test_bit(lc->recovering_bits, *region));
541
542 log_set_bit(lc, lc->recovering_bits, *region);
543 return 1;
544}
545
546static void core_complete_resync_work(struct dirty_log *log, region_t region,
547 int success)
548{
549 struct log_c *lc = (struct log_c *) log->context;
550
551 log_clear_bit(lc, lc->recovering_bits, region);
552 if (success) {
553 log_set_bit(lc, lc->sync_bits, region);
554 lc->sync_count++;
555 }
556}
557
558static region_t core_get_sync_count(struct dirty_log *log)
559{
560 struct log_c *lc = (struct log_c *) log->context;
561
562 return lc->sync_count;
563}
564
565#define DMEMIT_SYNC \
566 if (lc->sync != DEFAULTSYNC) \
567 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
568
569static int core_status(struct dirty_log *log, status_type_t status,
570 char *result, unsigned int maxlen)
571{
572 int sz = 0;
573 struct log_c *lc = log->context;
574
575 switch(status) {
576 case STATUSTYPE_INFO:
577 break;
578
579 case STATUSTYPE_TABLE:
580 DMEMIT("%s %u %u ", log->type->name,
581 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
582 DMEMIT_SYNC;
583 }
584
585 return sz;
586}
587
588static int disk_status(struct dirty_log *log, status_type_t status,
589 char *result, unsigned int maxlen)
590{
591 int sz = 0;
592 char buffer[16];
593 struct log_c *lc = log->context;
594
595 switch(status) {
596 case STATUSTYPE_INFO:
597 break;
598
599 case STATUSTYPE_TABLE:
600 format_dev_t(buffer, lc->log_dev->bdev->bd_dev);
601 DMEMIT("%s %u %s %u ", log->type->name,
602 lc->sync == DEFAULTSYNC ? 2 : 3, buffer,
603 lc->region_size);
604 DMEMIT_SYNC;
605 }
606
607 return sz;
608}
609
610static struct dirty_log_type _core_type = {
611 .name = "core",
612 .module = THIS_MODULE,
613 .ctr = core_ctr,
614 .dtr = core_dtr,
615 .get_region_size = core_get_region_size,
616 .is_clean = core_is_clean,
617 .in_sync = core_in_sync,
618 .flush = core_flush,
619 .mark_region = core_mark_region,
620 .clear_region = core_clear_region,
621 .get_resync_work = core_get_resync_work,
622 .complete_resync_work = core_complete_resync_work,
623 .get_sync_count = core_get_sync_count,
624 .status = core_status,
625};
626
627static struct dirty_log_type _disk_type = {
628 .name = "disk",
629 .module = THIS_MODULE,
630 .ctr = disk_ctr,
631 .dtr = disk_dtr,
632 .suspend = disk_flush,
633 .resume = disk_resume,
634 .get_region_size = core_get_region_size,
635 .is_clean = core_is_clean,
636 .in_sync = core_in_sync,
637 .flush = disk_flush,
638 .mark_region = core_mark_region,
639 .clear_region = core_clear_region,
640 .get_resync_work = core_get_resync_work,
641 .complete_resync_work = core_complete_resync_work,
642 .get_sync_count = core_get_sync_count,
643 .status = disk_status,
644};
645
646int __init dm_dirty_log_init(void)
647{
648 int r;
649
650 r = dm_register_dirty_log_type(&_core_type);
651 if (r)
652 DMWARN("couldn't register core log");
653
654 r = dm_register_dirty_log_type(&_disk_type);
655 if (r) {
656 DMWARN("couldn't register disk type");
657 dm_unregister_dirty_log_type(&_core_type);
658 }
659
660 return r;
661}
662
663void dm_dirty_log_exit(void)
664{
665 dm_unregister_dirty_log_type(&_disk_type);
666 dm_unregister_dirty_log_type(&_core_type);
667}
668
669EXPORT_SYMBOL(dm_register_dirty_log_type);
670EXPORT_SYMBOL(dm_unregister_dirty_log_type);
671EXPORT_SYMBOL(dm_create_dirty_log);
672EXPORT_SYMBOL(dm_destroy_dirty_log);