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