This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / md / dm-log.c
1 /*
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
15 static LIST_HEAD(_log_types);
16 static spinlock_t _lock = SPIN_LOCK_UNLOCKED;
17
18 int dm_register_dirty_log_type(struct dirty_log_type *type)
19 {
20         if (!try_module_get(type->module))
21                 return -EINVAL;
22
23         spin_lock(&_lock);
24         type->use_count = 0;
25         list_add(&type->list, &_log_types);
26         spin_unlock(&_lock);
27
28         return 0;
29 }
30
31 int dm_unregister_dirty_log_type(struct dirty_log_type *type)
32 {
33         spin_lock(&_lock);
34
35         if (type->use_count)
36                 DMWARN("Attempt to unregister a log type that is still in use");
37         else {
38                 list_del(&type->list);
39                 module_put(type->module);
40         }
41
42         spin_unlock(&_lock);
43
44         return 0;
45 }
46
47 static struct dirty_log_type *get_type(const char *type_name)
48 {
49         struct dirty_log_type *type;
50
51         spin_lock(&_lock);
52         list_for_each_entry (type, &_log_types, list)
53                 if (!strcmp(type_name, type->name)) {
54                         type->use_count++;
55                         spin_unlock(&_lock);
56                         return type;
57                 }
58
59         spin_unlock(&_lock);
60         return NULL;
61 }
62
63 static void put_type(struct dirty_log_type *type)
64 {
65         spin_lock(&_lock);
66         type->use_count--;
67         spin_unlock(&_lock);
68 }
69
70 struct 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
96 void 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  */
115 #define MIRROR_DISK_VERSION 1
116 #define LOG_OFFSET 2
117
118 struct 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
129 struct log_c {
130         struct dm_target *ti;
131         int touched;
132         sector_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         /*
144          * Disk log fields
145          */
146         struct dm_dev *log_dev;
147         struct log_header header;
148
149         struct io_region header_location;
150         struct log_header *disk_header;
151
152         struct io_region bits_location;
153         uint32_t *disk_bits;
154 };
155
156 /*
157  * The touched member needs to be updated every time we access
158  * one of the bitsets.
159  */
160 static  inline int log_test_bit(uint32_t *bs, unsigned bit)
161 {
162         return test_bit(bit, (unsigned long *) bs) ? 1 : 0;
163 }
164
165 static inline void log_set_bit(struct log_c *l,
166                                uint32_t *bs, unsigned bit)
167 {
168         set_bit(bit, (unsigned long *) bs);
169         l->touched = 1;
170 }
171
172 static inline void log_clear_bit(struct log_c *l,
173                                  uint32_t *bs, unsigned bit)
174 {
175         clear_bit(bit, (unsigned long *) bs);
176         l->touched = 1;
177 }
178
179 /*----------------------------------------------------------------
180  * Header IO
181  *--------------------------------------------------------------*/
182 static void header_to_disk(struct log_header *core, struct log_header *disk)
183 {
184         disk->magic = cpu_to_le32(core->magic);
185         disk->version = cpu_to_le32(core->version);
186         disk->nr_regions = cpu_to_le64(core->nr_regions);
187 }
188
189 static void header_from_disk(struct log_header *core, struct log_header *disk)
190 {
191         core->magic = le32_to_cpu(disk->magic);
192         core->version = le32_to_cpu(disk->version);
193         core->nr_regions = le64_to_cpu(disk->nr_regions);
194 }
195
196 static int read_header(struct log_c *log)
197 {
198         int r;
199         unsigned long ebits;
200
201         r = dm_io_sync_vm(1, &log->header_location, READ,
202                           log->disk_header, &ebits);
203         if (r)
204                 return r;
205
206         header_from_disk(&log->header, log->disk_header);
207
208         if (log->header.magic != MIRROR_MAGIC) {
209                 log->header.magic = MIRROR_MAGIC;
210                 log->header.version = MIRROR_DISK_VERSION;
211                 log->header.nr_regions = 0;
212         }
213
214         if (log->header.version != MIRROR_DISK_VERSION) {
215                 DMWARN("incompatible disk log version");
216                 return -EINVAL;
217         }
218
219         return 0;
220 }
221
222 static inline int write_header(struct log_c *log)
223 {
224         unsigned long ebits;
225
226         header_to_disk(&log->header, log->disk_header);
227         return dm_io_sync_vm(1, &log->header_location, WRITE,
228                              log->disk_header, &ebits);
229 }
230
231 /*----------------------------------------------------------------
232  * Bits IO
233  *--------------------------------------------------------------*/
234 static inline void bits_to_core(uint32_t *core, uint32_t *disk, unsigned count)
235 {
236         unsigned i;
237
238         for (i = 0; i < count; i++)
239                 core[i] = le32_to_cpu(disk[i]);
240 }
241
242 static inline void bits_to_disk(uint32_t *core, uint32_t *disk, unsigned count)
243 {
244         unsigned i;
245
246         /* copy across the clean/dirty bitset */
247         for (i = 0; i < count; i++)
248                 disk[i] = cpu_to_le32(core[i]);
249 }
250
251 static int read_bits(struct log_c *log)
252 {
253         int r;
254         unsigned long ebits;
255
256         r = dm_io_sync_vm(1, &log->bits_location, READ,
257                           log->disk_bits, &ebits);
258         if (r)
259                 return r;
260
261         bits_to_core(log->clean_bits, log->disk_bits,
262                      log->bitset_uint32_count);
263         return 0;
264 }
265
266 static int write_bits(struct log_c *log)
267 {
268         unsigned long ebits;
269         bits_to_disk(log->clean_bits, log->disk_bits,
270                      log->bitset_uint32_count);
271         return dm_io_sync_vm(1, &log->bits_location, WRITE,
272                              log->disk_bits, &ebits);
273 }
274
275 /*----------------------------------------------------------------
276  * constructor/destructor
277  *--------------------------------------------------------------*/
278 #define BYTE_SHIFT 3
279 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
280                     unsigned int argc, char **argv)
281 {
282         struct log_c *lc;
283         sector_t region_size;
284         unsigned int region_count;
285         size_t bitset_size;
286
287         if (argc != 1) {
288                 DMWARN("wrong number of arguments to log_c");
289                 return -EINVAL;
290         }
291
292         if (sscanf(argv[0], SECTOR_FORMAT, &region_size) != 1) {
293                 DMWARN("invalid region size string");
294                 return -EINVAL;
295         }
296
297         region_count = dm_div_up(ti->len, region_size);
298
299         lc = kmalloc(sizeof(*lc), GFP_KERNEL);
300         if (!lc) {
301                 DMWARN("couldn't allocate core log");
302                 return -ENOMEM;
303         }
304
305         lc->ti = ti;
306         lc->touched = 0;
307         lc->region_size = region_size;
308         lc->region_count = region_count;
309
310         /*
311          * Work out how many words we need to hold the bitset.
312          */
313         bitset_size = dm_round_up(region_count,
314                                   sizeof(*lc->clean_bits) << BYTE_SHIFT);
315         bitset_size >>= BYTE_SHIFT;
316
317         lc->bitset_uint32_count = bitset_size / 4;
318         lc->clean_bits = vmalloc(bitset_size);
319         if (!lc->clean_bits) {
320                 DMWARN("couldn't allocate clean bitset");
321                 kfree(lc);
322                 return -ENOMEM;
323         }
324         memset(lc->clean_bits, -1, bitset_size);
325
326         lc->sync_bits = vmalloc(bitset_size);
327         if (!lc->sync_bits) {
328                 DMWARN("couldn't allocate sync bitset");
329                 vfree(lc->clean_bits);
330                 kfree(lc);
331                 return -ENOMEM;
332         }
333         memset(lc->sync_bits, 0, bitset_size);
334         lc->sync_count = 0;
335
336         lc->recovering_bits = vmalloc(bitset_size);
337         if (!lc->recovering_bits) {
338                 DMWARN("couldn't allocate sync bitset");
339                 vfree(lc->sync_bits);
340                 vfree(lc->clean_bits);
341                 kfree(lc);
342                 return -ENOMEM;
343         }
344         memset(lc->recovering_bits, 0, bitset_size);
345         lc->sync_search = 0;
346         log->context = lc;
347         return 0;
348 }
349
350 static void core_dtr(struct dirty_log *log)
351 {
352         struct log_c *lc = (struct log_c *) log->context;
353         vfree(lc->clean_bits);
354         vfree(lc->sync_bits);
355         vfree(lc->recovering_bits);
356         kfree(lc);
357 }
358
359 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
360                     unsigned int argc, char **argv)
361 {
362         int r;
363         size_t size;
364         struct log_c *lc;
365         struct dm_dev *dev;
366
367         if (argc != 2) {
368                 DMWARN("wrong number of arguments to log_d");
369                 return -EINVAL;
370         }
371
372         r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
373                           FMODE_READ | FMODE_WRITE, &dev);
374         if (r)
375                 return r;
376
377         r = core_ctr(log, ti, argc - 1, argv + 1);
378         if (r) {
379                 dm_put_device(ti, dev);
380                 return r;
381         }
382
383         lc = (struct log_c *) log->context;
384         lc->log_dev = dev;
385
386         /* setup the disk header fields */
387         lc->header_location.bdev = lc->log_dev->bdev;
388         lc->header_location.sector = 0;
389         lc->header_location.count = 1;
390
391         /*
392          * We can't read less than this amount, even though we'll
393          * not be using most of this space.
394          */
395         lc->disk_header = vmalloc(1 << SECTOR_SHIFT);
396         if (!lc->disk_header)
397                 goto bad;
398
399         /* setup the disk bitset fields */
400         lc->bits_location.bdev = lc->log_dev->bdev;
401         lc->bits_location.sector = LOG_OFFSET;
402
403         size = dm_round_up(lc->bitset_uint32_count * sizeof(uint32_t),
404                            1 << SECTOR_SHIFT);
405         lc->bits_location.count = size >> SECTOR_SHIFT;
406         lc->disk_bits = vmalloc(size);
407         if (!lc->disk_bits) {
408                 vfree(lc->disk_header);
409                 goto bad;
410         }
411         return 0;
412
413  bad:
414         dm_put_device(ti, lc->log_dev);
415         core_dtr(log);
416         return -ENOMEM;
417 }
418
419 static void disk_dtr(struct dirty_log *log)
420 {
421         struct log_c *lc = (struct log_c *) log->context;
422         dm_put_device(lc->ti, lc->log_dev);
423         vfree(lc->disk_header);
424         vfree(lc->disk_bits);
425         core_dtr(log);
426 }
427
428 static 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
438 static 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
450         /* read the bits */
451         r = read_bits(lc);
452         if (r)
453                 return r;
454
455         /* zero any new bits if the mirror has grown */
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
464         /* write the bits */
465         r = write_bits(lc);
466         if (r)
467                 return r;
468
469         /* set the correct number of regions in the header */
470         lc->header.nr_regions = lc->region_count;
471
472         /* write the new header */
473         return write_header(lc);
474 }
475
476 static sector_t core_get_region_size(struct dirty_log *log)
477 {
478         struct log_c *lc = (struct log_c *) log->context;
479         return lc->region_size;
480 }
481
482 static int core_is_clean(struct dirty_log *log, region_t region)
483 {
484         struct log_c *lc = (struct log_c *) log->context;
485         return log_test_bit(lc->clean_bits, region);
486 }
487
488 static int core_in_sync(struct dirty_log *log, region_t region, int block)
489 {
490         struct log_c *lc = (struct log_c *) log->context;
491         return log_test_bit(lc->sync_bits, region);
492 }
493
494 static int core_flush(struct dirty_log *log)
495 {
496         /* no op */
497         return 0;
498 }
499
500 static int disk_flush(struct dirty_log *log)
501 {
502         int r;
503         struct log_c *lc = (struct log_c *) log->context;
504
505         /* only write if the log has changed */
506         if (!lc->touched)
507                 return 0;
508
509         r = write_bits(lc);
510         if (!r)
511                 lc->touched = 0;
512
513         return r;
514 }
515
516 static void core_mark_region(struct dirty_log *log, region_t region)
517 {
518         struct log_c *lc = (struct log_c *) log->context;
519         log_clear_bit(lc, lc->clean_bits, region);
520 }
521
522 static void core_clear_region(struct dirty_log *log, region_t region)
523 {
524         struct log_c *lc = (struct log_c *) log->context;
525         log_set_bit(lc, lc->clean_bits, region);
526 }
527
528 static int core_get_resync_work(struct dirty_log *log, region_t *region)
529 {
530         struct log_c *lc = (struct log_c *) log->context;
531
532         if (lc->sync_search >= lc->region_count)
533                 return 0;
534
535         do {
536                 *region = find_next_zero_bit((unsigned long *) lc->sync_bits,
537                                              lc->region_count,
538                                              lc->sync_search);
539                 lc->sync_search = *region + 1;
540
541                 if (*region == lc->region_count)
542                         return 0;
543
544         } while (log_test_bit(lc->recovering_bits, *region));
545
546         log_set_bit(lc, lc->recovering_bits, *region);
547         return 1;
548 }
549
550 static void core_complete_resync_work(struct dirty_log *log, region_t region,
551                                       int success)
552 {
553         struct log_c *lc = (struct log_c *) log->context;
554
555         log_clear_bit(lc, lc->recovering_bits, region);
556         if (success) {
557                 log_set_bit(lc, lc->sync_bits, region);
558                 lc->sync_count++;
559         }
560 }
561
562 static region_t core_get_sync_count(struct dirty_log *log)
563 {
564         struct log_c *lc = (struct log_c *) log->context;
565
566         return lc->sync_count;
567 }
568
569 static struct dirty_log_type _core_type = {
570         .name = "core",
571         .module = THIS_MODULE,
572         .ctr = core_ctr,
573         .dtr = core_dtr,
574         .get_region_size = core_get_region_size,
575         .is_clean = core_is_clean,
576         .in_sync = core_in_sync,
577         .flush = core_flush,
578         .mark_region = core_mark_region,
579         .clear_region = core_clear_region,
580         .get_resync_work = core_get_resync_work,
581         .complete_resync_work = core_complete_resync_work,
582         .get_sync_count = core_get_sync_count
583 };
584
585 static struct dirty_log_type _disk_type = {
586         .name = "disk",
587         .module = THIS_MODULE,
588         .ctr = disk_ctr,
589         .dtr = disk_dtr,
590         .suspend = disk_flush,
591         .resume = disk_resume,
592         .get_region_size = core_get_region_size,
593         .is_clean = core_is_clean,
594         .in_sync = core_in_sync,
595         .flush = disk_flush,
596         .mark_region = core_mark_region,
597         .clear_region = core_clear_region,
598         .get_resync_work = core_get_resync_work,
599         .complete_resync_work = core_complete_resync_work,
600         .get_sync_count = core_get_sync_count
601 };
602
603 int __init dm_dirty_log_init(void)
604 {
605         int r;
606
607         r = dm_register_dirty_log_type(&_core_type);
608         if (r)
609                 DMWARN("couldn't register core log");
610
611         r = dm_register_dirty_log_type(&_disk_type);
612         if (r) {
613                 DMWARN("couldn't register disk type");
614                 dm_unregister_dirty_log_type(&_core_type);
615         }
616
617         return r;
618 }
619
620 void dm_dirty_log_exit(void)
621 {
622         dm_unregister_dirty_log_type(&_disk_type);
623         dm_unregister_dirty_log_type(&_core_type);
624 }
625
626 EXPORT_SYMBOL(dm_register_dirty_log_type);
627 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
628 EXPORT_SYMBOL(dm_create_dirty_log);
629 EXPORT_SYMBOL(dm_destroy_dirty_log);