Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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 #define DM_MSG_PREFIX "mirror log"
16
17 static LIST_HEAD(_log_types);
18 static DEFINE_SPINLOCK(_lock);
19
20 int dm_register_dirty_log_type(struct dirty_log_type *type)
21 {
22         spin_lock(&_lock);
23         type->use_count = 0;
24         list_add(&type->list, &_log_types);
25         spin_unlock(&_lock);
26
27         return 0;
28 }
29
30 int dm_unregister_dirty_log_type(struct dirty_log_type *type)
31 {
32         spin_lock(&_lock);
33
34         if (type->use_count)
35                 DMWARN("Unregister failed: log type '%s' still in use",
36                        type->name);
37         else
38                 list_del(&type->list);
39
40         spin_unlock(&_lock);
41
42         return 0;
43 }
44
45 static struct dirty_log_type *get_type(const char *type_name)
46 {
47         struct dirty_log_type *type;
48
49         spin_lock(&_lock);
50         list_for_each_entry (type, &_log_types, list)
51                 if (!strcmp(type_name, type->name)) {
52                         if (!type->use_count && !try_module_get(type->module)){
53                                 spin_unlock(&_lock);
54                                 return NULL;
55                         }
56                         type->use_count++;
57                         spin_unlock(&_lock);
58                         return type;
59                 }
60
61         spin_unlock(&_lock);
62         return NULL;
63 }
64
65 static void put_type(struct dirty_log_type *type)
66 {
67         spin_lock(&_lock);
68         if (!--type->use_count)
69                 module_put(type->module);
70         spin_unlock(&_lock);
71 }
72
73 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
74                                       unsigned int argc, char **argv)
75 {
76         struct dirty_log_type *type;
77         struct dirty_log *log;
78
79         log = kmalloc(sizeof(*log), GFP_KERNEL);
80         if (!log)
81                 return NULL;
82
83         type = get_type(type_name);
84         if (!type) {
85                 kfree(log);
86                 return NULL;
87         }
88
89         log->type = type;
90         if (type->ctr(log, ti, argc, argv)) {
91                 kfree(log);
92                 put_type(type);
93                 return NULL;
94         }
95
96         return log;
97 }
98
99 void dm_destroy_dirty_log(struct dirty_log *log)
100 {
101         log->type->dtr(log);
102         put_type(log->type);
103         kfree(log);
104 }
105
106 /*-----------------------------------------------------------------
107  * Persistent and core logs share a lot of their implementation.
108  * FIXME: need a reload method to be called from a resume
109  *---------------------------------------------------------------*/
110 /*
111  * Magic for persistent mirrors: "MiRr"
112  */
113 #define MIRROR_MAGIC 0x4D695272
114
115 /*
116  * The on-disk version of the metadata.
117  */
118 #define MIRROR_DISK_VERSION 2
119 #define LOG_OFFSET 2
120
121 struct log_header {
122         uint32_t magic;
123
124         /*
125          * Simple, incrementing version. no backward
126          * compatibility.
127          */
128         uint32_t version;
129         sector_t nr_regions;
130 };
131
132 struct log_c {
133         struct dm_target *ti;
134         int touched;
135         uint32_t region_size;
136         unsigned int region_count;
137         region_t sync_count;
138
139         unsigned bitset_uint32_count;
140         uint32_t *clean_bits;
141         uint32_t *sync_bits;
142         uint32_t *recovering_bits;
143
144         int sync_search;
145
146         /* Resync flag */
147         enum sync {
148                 DEFAULTSYNC,    /* Synchronize if necessary */
149                 NOSYNC,         /* Devices known to be already in sync */
150                 FORCESYNC,      /* Force a sync to happen */
151         } sync;
152
153         int failure_response;
154
155         /*
156          * Disk log fields
157          */
158         int log_dev_failed;
159         struct dm_dev *log_dev;
160         struct log_header header;
161
162         struct io_region header_location;
163         struct log_header *disk_header;
164 };
165
166 /*
167  * The touched member needs to be updated every time we access
168  * one of the bitsets.
169  */
170 static  inline int log_test_bit(uint32_t *bs, unsigned bit)
171 {
172         return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
173 }
174
175 static inline void log_set_bit(struct log_c *l,
176                                uint32_t *bs, unsigned bit)
177 {
178         ext2_set_bit(bit, (unsigned long *) bs);
179         l->touched = 1;
180 }
181
182 static inline void log_clear_bit(struct log_c *l,
183                                  uint32_t *bs, unsigned bit)
184 {
185         ext2_clear_bit(bit, (unsigned long *) bs);
186         l->touched = 1;
187 }
188
189 /*----------------------------------------------------------------
190  * Header IO
191  *--------------------------------------------------------------*/
192 static void header_to_disk(struct log_header *core, struct log_header *disk)
193 {
194         disk->magic = cpu_to_le32(core->magic);
195         disk->version = cpu_to_le32(core->version);
196         disk->nr_regions = cpu_to_le64(core->nr_regions);
197 }
198
199 static void header_from_disk(struct log_header *core, struct log_header *disk)
200 {
201         core->magic = le32_to_cpu(disk->magic);
202         core->version = le32_to_cpu(disk->version);
203         core->nr_regions = le64_to_cpu(disk->nr_regions);
204 }
205
206 static int read_header(struct log_c *log)
207 {
208         int r;
209         unsigned long ebits;
210
211         r = dm_io_sync_vm(1, &log->header_location, READ,
212                           log->disk_header, &ebits);
213         if (r)
214                 return r;
215
216         header_from_disk(&log->header, log->disk_header);
217
218         /* New log required? */
219         if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
220                 log->header.magic = MIRROR_MAGIC;
221                 log->header.version = MIRROR_DISK_VERSION;
222                 log->header.nr_regions = 0;
223         }
224
225 #ifdef __LITTLE_ENDIAN
226         if (log->header.version == 1)
227                 log->header.version = 2;
228 #endif
229
230         if (log->header.version != MIRROR_DISK_VERSION) {
231                 DMWARN("incompatible disk log version");
232                 return -EINVAL;
233         }
234
235         return 0;
236 }
237
238 static inline int write_header(struct log_c *log)
239 {
240         unsigned long ebits;
241
242         header_to_disk(&log->header, log->disk_header);
243         return dm_io_sync_vm(1, &log->header_location, WRITE,
244                              log->disk_header, &ebits);
245 }
246
247 /*----------------------------------------------------------------
248  * core log constructor/destructor
249  *
250  * argv contains 1 - 3 arguments:
251  *   <region_size> [[no]sync] [block_on_error]
252  *--------------------------------------------------------------*/
253 #define BYTE_SHIFT 3
254 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
255                               unsigned int argc, char **argv,
256                               struct dm_dev *dev)
257 {
258         int i;
259         enum sync sync = DEFAULTSYNC;
260         int failure_response = DMLOG_IOERR_IGNORE;
261         struct log_c *lc;
262         uint32_t region_size;
263         unsigned int region_count;
264         size_t bitset_size, buf_size;
265
266         if (argc < 1 || argc > 3) {
267                 DMWARN("wrong number of arguments to mirror log");
268                 return -EINVAL;
269         }
270
271         for (i = 1; i < argc; i++) {
272                 if (!strcmp(argv[i], "sync"))
273                         sync = FORCESYNC;
274                 else if (!strcmp(argv[i], "nosync"))
275                         sync = NOSYNC;
276                 else if (!strcmp(argv[i], "block_on_error"))
277                         failure_response = DMLOG_IOERR_BLOCK;
278                 else {
279                         DMWARN("unrecognised sync argument to mirror log: %s",
280                                argv[i]);
281                         return -EINVAL;
282                 }
283         }
284
285         if (sscanf(argv[0], "%u", &region_size) != 1) {
286                 DMWARN("invalid region size string");
287                 return -EINVAL;
288         }
289
290         region_count = dm_sector_div_up(ti->len, region_size);
291
292         lc = kmalloc(sizeof(*lc), GFP_KERNEL);
293         if (!lc) {
294                 DMWARN("couldn't allocate core log");
295                 return -ENOMEM;
296         }
297
298         lc->ti = ti;
299         lc->touched = 0;
300         lc->region_size = region_size;
301         lc->region_count = region_count;
302         lc->sync = sync;
303         lc->failure_response = failure_response;
304
305         /*
306          * Work out how many "unsigned long"s we need to hold the bitset.
307          */
308         bitset_size = dm_round_up(region_count,
309                                   sizeof(*lc->clean_bits) << BYTE_SHIFT);
310         bitset_size >>= BYTE_SHIFT;
311
312         lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
313
314         /*
315          * Disk log?
316          */
317         if (!dev) {
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                 lc->disk_header = NULL;
325         } else {
326                 lc->log_dev = dev;
327                 lc->log_dev_failed = 0;
328                 lc->header_location.bdev = lc->log_dev->bdev;
329                 lc->header_location.sector = 0;
330
331                 /*
332                  * Buffer holds both header and bitset.
333                  */
334                 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
335                                        bitset_size, ti->limits.hardsect_size);
336                 lc->header_location.count = buf_size >> SECTOR_SHIFT;
337
338                 lc->disk_header = vmalloc(buf_size);
339                 if (!lc->disk_header) {
340                         DMWARN("couldn't allocate disk log buffer");
341                         kfree(lc);
342                         return -ENOMEM;
343                 }
344
345                 lc->clean_bits = (void *)lc->disk_header +
346                                  (LOG_OFFSET << SECTOR_SHIFT);
347         }
348
349         memset(lc->clean_bits, -1, bitset_size);
350
351         lc->sync_bits = vmalloc(bitset_size);
352         if (!lc->sync_bits) {
353                 DMWARN("couldn't allocate sync bitset");
354                 if (!dev)
355                         vfree(lc->clean_bits);
356                 vfree(lc->disk_header);
357                 kfree(lc);
358                 return -ENOMEM;
359         }
360         memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
361         lc->sync_count = (sync == NOSYNC) ? region_count : 0;
362
363         lc->recovering_bits = vmalloc(bitset_size);
364         if (!lc->recovering_bits) {
365                 DMWARN("couldn't allocate recovering bitset");
366                 vfree(lc->sync_bits);
367                 if (!dev)
368                         vfree(lc->clean_bits);
369                 vfree(lc->disk_header);
370                 kfree(lc);
371                 return -ENOMEM;
372         }
373         memset(lc->recovering_bits, 0, bitset_size);
374         lc->sync_search = 0;
375         log->context = lc;
376
377         return 0;
378 }
379
380 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
381                     unsigned int argc, char **argv)
382 {
383         return create_log_context(log, ti, argc, argv, NULL);
384 }
385
386 static void destroy_log_context(struct log_c *lc)
387 {
388         vfree(lc->sync_bits);
389         vfree(lc->recovering_bits);
390         kfree(lc);
391 }
392
393 static void core_dtr(struct dirty_log *log)
394 {
395         struct log_c *lc = (struct log_c *) log->context;
396
397         vfree(lc->clean_bits);
398         destroy_log_context(lc);
399 }
400
401 /*----------------------------------------------------------------
402  * disk log constructor/destructor
403  *
404  * argv contains 2 - 4 arguments:
405  *      <log_device> <region_size> [[no]sync] [block_on_error]
406  *--------------------------------------------------------------*/
407 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
408                     unsigned int argc, char **argv)
409 {
410         int r;
411         struct dm_dev *dev;
412
413         if (argc < 2 || argc > 4) {
414                 DMWARN("wrong number of arguments to disk mirror log");
415                 return -EINVAL;
416         }
417
418         r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
419                           FMODE_READ | FMODE_WRITE, &dev);
420         if (r)
421                 return r;
422
423         r = create_log_context(log, ti, argc - 1, argv + 1, dev);
424         if (r) {
425                 dm_put_device(ti, dev);
426                 return r;
427         }
428
429         return 0;
430 }
431
432 static void disk_dtr(struct dirty_log *log)
433 {
434         struct log_c *lc = (struct log_c *) log->context;
435
436         dm_put_device(lc->ti, lc->log_dev);
437         vfree(lc->disk_header);
438         destroy_log_context(lc);
439 }
440
441 static int count_bits32(uint32_t *addr, unsigned size)
442 {
443         int count = 0, i;
444
445         for (i = 0; i < size; i++) {
446                 count += hweight32(*(addr+i));
447         }
448         return count;
449 }
450
451 static void fail_log_device(struct log_c *lc)
452 {
453         if (lc->log_dev_failed)
454                 return;
455
456         lc->log_dev_failed = 1;
457         if (lc->failure_response == DMLOG_IOERR_BLOCK)
458                 dm_table_event(lc->ti->table);
459 }
460
461 static void restore_log_device(struct log_c *lc)
462 {
463         lc->log_dev_failed = 0;
464 }
465
466 static int disk_resume(struct dirty_log *log)
467 {
468         int r;
469         unsigned i;
470         struct log_c *lc = (struct log_c *) log->context;
471         size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
472
473         /*
474          * Read the disk header, but only if we know it is good.
475          * Assume the worst in the event of failure.
476          */
477         if (!lc->log_dev_failed && read_header(lc)) {
478                 DMWARN("Failed to read header on mirror log device, %s",
479                        lc->log_dev->name);
480                 fail_log_device(lc);
481                 lc->header.nr_regions = 0;
482         }
483
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);
489         else
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);
493
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);
497
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);
501         lc->sync_search = 0;
502
503         /* set the correct number of regions in the header */
504         lc->header.nr_regions = lc->region_count;
505
506         /* write the new header */
507         r = write_header(lc);
508         if (r) {
509                 DMWARN("Failed to write header on mirror log device, %s",
510                        lc->log_dev->name);
511                 fail_log_device(lc);
512         } else
513                 restore_log_device(lc);
514
515         return r;
516 }
517
518 static uint32_t core_get_region_size(struct dirty_log *log)
519 {
520         struct log_c *lc = (struct log_c *) log->context;
521         return lc->region_size;
522 }
523
524 static int core_resume(struct dirty_log *log)
525 {
526         struct log_c *lc = (struct log_c *) log->context;
527         lc->sync_search = 0;
528         return 0;
529 }
530
531 static int core_is_clean(struct dirty_log *log, region_t region)
532 {
533         struct log_c *lc = (struct log_c *) log->context;
534         return log_test_bit(lc->clean_bits, region);
535 }
536
537 static int core_in_sync(struct dirty_log *log, region_t region, int block)
538 {
539         struct log_c *lc = (struct log_c *) log->context;
540         return log_test_bit(lc->sync_bits, region);
541 }
542
543 static int core_flush(struct dirty_log *log)
544 {
545         /* no op */
546         return 0;
547 }
548
549 static int disk_presuspend(struct dirty_log *log)
550 {
551         return 0;
552 }
553
554 static int disk_flush(struct dirty_log *log)
555 {
556         int r;
557         struct log_c *lc = (struct log_c *) log->context;
558
559         /* only write if the log has changed */
560         if (!lc->touched)
561                 return 0;
562
563         r = write_header(lc);
564         if (r)
565                 fail_log_device(lc);
566         else {
567                 lc->touched = 0;
568                 restore_log_device(lc);
569         }
570         return r;
571 }
572
573 static void core_mark_region(struct dirty_log *log, region_t region)
574 {
575         struct log_c *lc = (struct log_c *) log->context;
576         log_clear_bit(lc, lc->clean_bits, region);
577 }
578
579 static void core_clear_region(struct dirty_log *log, region_t region)
580 {
581         struct log_c *lc = (struct log_c *) log->context;
582         log_set_bit(lc, lc->clean_bits, region);
583 }
584
585 static int core_get_resync_work(struct dirty_log *log, region_t *region)
586 {
587         struct log_c *lc = (struct log_c *) log->context;
588
589         if (lc->sync_search >= lc->region_count)
590                 return 0;
591
592         do {
593                 *region = ext2_find_next_zero_bit(
594                                              (unsigned long *) lc->sync_bits,
595                                              lc->region_count,
596                                              lc->sync_search);
597                 lc->sync_search = *region + 1;
598
599                 if (*region >= lc->region_count)
600                         return 0;
601
602         } while (log_test_bit(lc->recovering_bits, *region));
603
604         log_set_bit(lc, lc->recovering_bits, *region);
605         return 1;
606 }
607
608 static void core_set_region_sync(struct dirty_log *log, region_t region,
609                                  int in_sync)
610 {
611         struct log_c *lc = (struct log_c *) log->context;
612
613         log_clear_bit(lc, lc->recovering_bits, region);
614         if (in_sync) {
615                 log_set_bit(lc, lc->sync_bits, region);
616                 lc->sync_count++;
617         } else if (log_test_bit(lc->sync_bits, region)) {
618                 lc->sync_count--;
619                 log_clear_bit(lc, lc->sync_bits, region);
620         }
621 }
622
623 static region_t core_get_sync_count(struct dirty_log *log)
624 {
625         struct log_c *lc = (struct log_c *) log->context;
626
627         return lc->sync_count;
628 }
629
630 #define DMEMIT_SYNC \
631         if (lc->sync != DEFAULTSYNC) \
632                 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
633
634 static int core_status(struct dirty_log *log, status_type_t status,
635                        char *result, unsigned int maxlen)
636 {
637         int sz = 0;
638         int params;
639         struct log_c *lc = log->context;
640
641         switch(status) {
642         case STATUSTYPE_INFO:
643                 DMEMIT("1 core");
644                 break;
645
646         case STATUSTYPE_TABLE:
647                 params = (lc->sync == DEFAULTSYNC) ? 1 : 2;
648                 params += (lc->failure_response == DMLOG_IOERR_BLOCK) ? 1 : 0;
649
650                 DMEMIT("%s %d %u ", log->type->name, params, lc->region_size);
651                 DMEMIT_SYNC;
652                 if (lc->failure_response == DMLOG_IOERR_BLOCK)
653                         DMEMIT("block_on_error ");
654         }
655
656         return sz;
657 }
658
659 static int disk_status(struct dirty_log *log, status_type_t status,
660                        char *result, unsigned int maxlen)
661 {
662         int sz = 0;
663         int params;
664         struct log_c *lc = log->context;
665
666         switch(status) {
667         case STATUSTYPE_INFO:
668                 DMEMIT("3 disk %s %c", lc->log_dev->name,
669                        lc->log_dev_failed ? 'D' : 'A');
670                 break;
671
672         case STATUSTYPE_TABLE:
673                 params = (lc->sync == DEFAULTSYNC) ? 2 : 3;
674                 params += (lc->failure_response == DMLOG_IOERR_BLOCK) ? 1 : 0;
675
676                 DMEMIT("%s %d %s %u ", log->type->name,
677                        params,
678                        lc->log_dev->name,
679                        lc->region_size);
680                 DMEMIT_SYNC;
681                 if (lc->failure_response == DMLOG_IOERR_BLOCK)
682                         DMEMIT("block_on_error ");
683         }
684
685         return sz;
686 }
687
688 static int core_get_failure_response(struct dirty_log *log)
689 {
690         struct log_c *lc = log->context;
691
692         return lc->failure_response;
693 }
694
695 static struct dirty_log_type _core_type = {
696         .name = "core",
697         .module = THIS_MODULE,
698         .ctr = core_ctr,
699         .dtr = core_dtr,
700         .resume = core_resume,
701         .get_region_size = core_get_region_size,
702         .is_clean = core_is_clean,
703         .in_sync = core_in_sync,
704         .flush = core_flush,
705         .mark_region = core_mark_region,
706         .clear_region = core_clear_region,
707         .get_resync_work = core_get_resync_work,
708         .set_region_sync = core_set_region_sync,
709         .get_sync_count = core_get_sync_count,
710         .status = core_status,
711         .get_failure_response = core_get_failure_response,
712 };
713
714 static struct dirty_log_type _disk_type = {
715         .name = "disk",
716         .module = THIS_MODULE,
717         .ctr = disk_ctr,
718         .dtr = disk_dtr,
719         .presuspend = disk_presuspend,
720         .postsuspend = disk_flush,
721         .resume = disk_resume,
722         .get_region_size = core_get_region_size,
723         .is_clean = core_is_clean,
724         .in_sync = core_in_sync,
725         .flush = disk_flush,
726         .mark_region = core_mark_region,
727         .clear_region = core_clear_region,
728         .get_resync_work = core_get_resync_work,
729         .set_region_sync = core_set_region_sync,
730         .get_sync_count = core_get_sync_count,
731         .status = disk_status,
732         .get_failure_response = core_get_failure_response,
733 };
734
735 int __init dm_dirty_log_init(void)
736 {
737         int r;
738
739         r = dm_register_dirty_log_type(&_core_type);
740         if (r)
741                 DMWARN("couldn't register core log");
742
743         r = dm_register_dirty_log_type(&_disk_type);
744         if (r) {
745                 DMWARN("couldn't register disk type");
746                 dm_unregister_dirty_log_type(&_core_type);
747         }
748
749         return r;
750 }
751
752 void dm_dirty_log_exit(void)
753 {
754         dm_unregister_dirty_log_type(&_disk_type);
755         dm_unregister_dirty_log_type(&_core_type);
756 }
757
758 EXPORT_SYMBOL(dm_register_dirty_log_type);
759 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
760 EXPORT_SYMBOL(dm_create_dirty_log);
761 EXPORT_SYMBOL(dm_destroy_dirty_log);