]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/md/dm-log.c
dm log: generalise name in messages
[linux-2.6.git] / drivers / md / dm-log.c
1 /*
2  * Copyright (C) 2003 Sistina Software
3  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the LGPL.
6  */
7
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12
13 #include "dm-log.h"
14 #include "dm-io.h"
15
16 #define DM_MSG_PREFIX "dirty region log"
17
18 static LIST_HEAD(_log_types);
19 static DEFINE_SPINLOCK(_lock);
20
21 int dm_register_dirty_log_type(struct dirty_log_type *type)
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
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 /*
66  * get_type
67  * @type_name
68  *
69  * Attempt to retrieve the dirty_log_type by name.  If not already
70  * available, attempt to load the appropriate module.
71  *
72  * Log modules are named "dm-log-" followed by the 'type_name'.
73  * Modules may contain multiple types.
74  * This function will first try the module "dm-log-<type_name>",
75  * then truncate 'type_name' on the last '-' and try again.
76  *
77  * For example, if type_name was "clustered-disk", it would search
78  * 'dm-log-clustered-disk' then 'dm-log-clustered'.
79  *
80  * Returns: dirty_log_type* on success, NULL on failure
81  */
82 static struct dirty_log_type *get_type(const char *type_name)
83 {
84         char *p, *type_name_dup;
85         struct dirty_log_type *type;
86
87         type = _get_type(type_name);
88         if (type)
89                 return type;
90
91         type_name_dup = kstrdup(type_name, GFP_KERNEL);
92         if (!type_name_dup) {
93                 DMWARN("No memory left to attempt log module load for \"%s\"",
94                        type_name);
95                 return NULL;
96         }
97
98         while (request_module("dm-log-%s", type_name_dup) ||
99                !(type = _get_type(type_name))) {
100                 p = strrchr(type_name_dup, '-');
101                 if (!p)
102                         break;
103                 p[0] = '\0';
104         }
105
106         if (!type)
107                 DMWARN("Module for logging type \"%s\" not found.", type_name);
108
109         kfree(type_name_dup);
110
111         return type;
112 }
113
114 static void put_type(struct dirty_log_type *type)
115 {
116         spin_lock(&_lock);
117         if (!--type->use_count)
118                 module_put(type->module);
119         spin_unlock(&_lock);
120 }
121
122 struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
123                                       unsigned int argc, char **argv)
124 {
125         struct dirty_log_type *type;
126         struct dirty_log *log;
127
128         log = kmalloc(sizeof(*log), GFP_KERNEL);
129         if (!log)
130                 return NULL;
131
132         type = get_type(type_name);
133         if (!type) {
134                 kfree(log);
135                 return NULL;
136         }
137
138         log->type = type;
139         if (type->ctr(log, ti, argc, argv)) {
140                 kfree(log);
141                 put_type(type);
142                 return NULL;
143         }
144
145         return log;
146 }
147
148 void dm_destroy_dirty_log(struct dirty_log *log)
149 {
150         log->type->dtr(log);
151         put_type(log->type);
152         kfree(log);
153 }
154
155 /*-----------------------------------------------------------------
156  * Persistent and core logs share a lot of their implementation.
157  * FIXME: need a reload method to be called from a resume
158  *---------------------------------------------------------------*/
159 /*
160  * Magic for persistent mirrors: "MiRr"
161  */
162 #define MIRROR_MAGIC 0x4D695272
163
164 /*
165  * The on-disk version of the metadata.
166  */
167 #define MIRROR_DISK_VERSION 2
168 #define LOG_OFFSET 2
169
170 struct log_header {
171         uint32_t magic;
172
173         /*
174          * Simple, incrementing version. no backward
175          * compatibility.
176          */
177         uint32_t version;
178         sector_t nr_regions;
179 };
180
181 struct log_c {
182         struct dm_target *ti;
183         int touched;
184         uint32_t region_size;
185         unsigned int region_count;
186         region_t sync_count;
187
188         unsigned bitset_uint32_count;
189         uint32_t *clean_bits;
190         uint32_t *sync_bits;
191         uint32_t *recovering_bits;      /* FIXME: this seems excessive */
192
193         int sync_search;
194
195         /* Resync flag */
196         enum sync {
197                 DEFAULTSYNC,    /* Synchronize if necessary */
198                 NOSYNC,         /* Devices known to be already in sync */
199                 FORCESYNC,      /* Force a sync to happen */
200         } sync;
201
202         struct dm_io_request io_req;
203
204         /*
205          * Disk log fields
206          */
207         int log_dev_failed;
208         struct dm_dev *log_dev;
209         struct log_header header;
210
211         struct io_region header_location;
212         struct log_header *disk_header;
213 };
214
215 /*
216  * The touched member needs to be updated every time we access
217  * one of the bitsets.
218  */
219 static  inline int log_test_bit(uint32_t *bs, unsigned bit)
220 {
221         return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
222 }
223
224 static inline void log_set_bit(struct log_c *l,
225                                uint32_t *bs, unsigned bit)
226 {
227         ext2_set_bit(bit, (unsigned long *) bs);
228         l->touched = 1;
229 }
230
231 static inline void log_clear_bit(struct log_c *l,
232                                  uint32_t *bs, unsigned bit)
233 {
234         ext2_clear_bit(bit, (unsigned long *) bs);
235         l->touched = 1;
236 }
237
238 /*----------------------------------------------------------------
239  * Header IO
240  *--------------------------------------------------------------*/
241 static void header_to_disk(struct log_header *core, struct log_header *disk)
242 {
243         disk->magic = cpu_to_le32(core->magic);
244         disk->version = cpu_to_le32(core->version);
245         disk->nr_regions = cpu_to_le64(core->nr_regions);
246 }
247
248 static void header_from_disk(struct log_header *core, struct log_header *disk)
249 {
250         core->magic = le32_to_cpu(disk->magic);
251         core->version = le32_to_cpu(disk->version);
252         core->nr_regions = le64_to_cpu(disk->nr_regions);
253 }
254
255 static int rw_header(struct log_c *lc, int rw)
256 {
257         lc->io_req.bi_rw = rw;
258         lc->io_req.mem.ptr.vma = lc->disk_header;
259         lc->io_req.notify.fn = NULL;
260
261         return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
262 }
263
264 static int read_header(struct log_c *log)
265 {
266         int r;
267
268         r = rw_header(log, READ);
269         if (r)
270                 return r;
271
272         header_from_disk(&log->header, log->disk_header);
273
274         /* New log required? */
275         if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
276                 log->header.magic = MIRROR_MAGIC;
277                 log->header.version = MIRROR_DISK_VERSION;
278                 log->header.nr_regions = 0;
279         }
280
281 #ifdef __LITTLE_ENDIAN
282         if (log->header.version == 1)
283                 log->header.version = 2;
284 #endif
285
286         if (log->header.version != MIRROR_DISK_VERSION) {
287                 DMWARN("incompatible disk log version");
288                 return -EINVAL;
289         }
290
291         return 0;
292 }
293
294 static inline int write_header(struct log_c *log)
295 {
296         header_to_disk(&log->header, log->disk_header);
297         return rw_header(log, WRITE);
298 }
299
300 /*----------------------------------------------------------------
301  * core log constructor/destructor
302  *
303  * argv contains region_size followed optionally by [no]sync
304  *--------------------------------------------------------------*/
305 #define BYTE_SHIFT 3
306 static int create_log_context(struct dirty_log *log, struct dm_target *ti,
307                               unsigned int argc, char **argv,
308                               struct dm_dev *dev)
309 {
310         enum sync sync = DEFAULTSYNC;
311
312         struct log_c *lc;
313         uint32_t region_size;
314         unsigned int region_count;
315         size_t bitset_size, buf_size;
316         int r;
317
318         if (argc < 1 || argc > 2) {
319                 DMWARN("wrong number of arguments to dirty region log");
320                 return -EINVAL;
321         }
322
323         if (argc > 1) {
324                 if (!strcmp(argv[1], "sync"))
325                         sync = FORCESYNC;
326                 else if (!strcmp(argv[1], "nosync"))
327                         sync = NOSYNC;
328                 else {
329                         DMWARN("unrecognised sync argument to "
330                                "dirty region log: %s", argv[1]);
331                         return -EINVAL;
332                 }
333         }
334
335         if (sscanf(argv[0], "%u", &region_size) != 1) {
336                 DMWARN("invalid region size string");
337                 return -EINVAL;
338         }
339
340         region_count = dm_sector_div_up(ti->len, region_size);
341
342         lc = kmalloc(sizeof(*lc), GFP_KERNEL);
343         if (!lc) {
344                 DMWARN("couldn't allocate core log");
345                 return -ENOMEM;
346         }
347
348         lc->ti = ti;
349         lc->touched = 0;
350         lc->region_size = region_size;
351         lc->region_count = region_count;
352         lc->sync = sync;
353
354         /*
355          * Work out how many "unsigned long"s we need to hold the bitset.
356          */
357         bitset_size = dm_round_up(region_count,
358                                   sizeof(*lc->clean_bits) << BYTE_SHIFT);
359         bitset_size >>= BYTE_SHIFT;
360
361         lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
362
363         /*
364          * Disk log?
365          */
366         if (!dev) {
367                 lc->clean_bits = vmalloc(bitset_size);
368                 if (!lc->clean_bits) {
369                         DMWARN("couldn't allocate clean bitset");
370                         kfree(lc);
371                         return -ENOMEM;
372                 }
373                 lc->disk_header = NULL;
374         } else {
375                 lc->log_dev = dev;
376                 lc->log_dev_failed = 0;
377                 lc->header_location.bdev = lc->log_dev->bdev;
378                 lc->header_location.sector = 0;
379
380                 /*
381                  * Buffer holds both header and bitset.
382                  */
383                 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
384                                        bitset_size, ti->limits.hardsect_size);
385                 lc->header_location.count = buf_size >> SECTOR_SHIFT;
386                 lc->io_req.mem.type = DM_IO_VMA;
387                 lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
388                                                                    PAGE_SIZE));
389                 if (IS_ERR(lc->io_req.client)) {
390                         r = PTR_ERR(lc->io_req.client);
391                         DMWARN("couldn't allocate disk io client");
392                         kfree(lc);
393                         return -ENOMEM;
394                 }
395
396                 lc->disk_header = vmalloc(buf_size);
397                 if (!lc->disk_header) {
398                         DMWARN("couldn't allocate disk log buffer");
399                         kfree(lc);
400                         return -ENOMEM;
401                 }
402
403                 lc->clean_bits = (void *)lc->disk_header +
404                                  (LOG_OFFSET << SECTOR_SHIFT);
405         }
406
407         memset(lc->clean_bits, -1, bitset_size);
408
409         lc->sync_bits = vmalloc(bitset_size);
410         if (!lc->sync_bits) {
411                 DMWARN("couldn't allocate sync bitset");
412                 if (!dev)
413                         vfree(lc->clean_bits);
414                 vfree(lc->disk_header);
415                 kfree(lc);
416                 return -ENOMEM;
417         }
418         memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
419         lc->sync_count = (sync == NOSYNC) ? region_count : 0;
420
421         lc->recovering_bits = vmalloc(bitset_size);
422         if (!lc->recovering_bits) {
423                 DMWARN("couldn't allocate sync bitset");
424                 vfree(lc->sync_bits);
425                 if (!dev)
426                         vfree(lc->clean_bits);
427                 vfree(lc->disk_header);
428                 kfree(lc);
429                 return -ENOMEM;
430         }
431         memset(lc->recovering_bits, 0, bitset_size);
432         lc->sync_search = 0;
433         log->context = lc;
434
435         return 0;
436 }
437
438 static int core_ctr(struct dirty_log *log, struct dm_target *ti,
439                     unsigned int argc, char **argv)
440 {
441         return create_log_context(log, ti, argc, argv, NULL);
442 }
443
444 static void destroy_log_context(struct log_c *lc)
445 {
446         vfree(lc->sync_bits);
447         vfree(lc->recovering_bits);
448         kfree(lc);
449 }
450
451 static void core_dtr(struct dirty_log *log)
452 {
453         struct log_c *lc = (struct log_c *) log->context;
454
455         vfree(lc->clean_bits);
456         destroy_log_context(lc);
457 }
458
459 /*----------------------------------------------------------------
460  * disk log constructor/destructor
461  *
462  * argv contains log_device region_size followed optionally by [no]sync
463  *--------------------------------------------------------------*/
464 static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
465                     unsigned int argc, char **argv)
466 {
467         int r;
468         struct dm_dev *dev;
469
470         if (argc < 2 || argc > 3) {
471                 DMWARN("wrong number of arguments to disk dirty region log");
472                 return -EINVAL;
473         }
474
475         r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
476                           FMODE_READ | FMODE_WRITE, &dev);
477         if (r)
478                 return r;
479
480         r = create_log_context(log, ti, argc - 1, argv + 1, dev);
481         if (r) {
482                 dm_put_device(ti, dev);
483                 return r;
484         }
485
486         return 0;
487 }
488
489 static void disk_dtr(struct dirty_log *log)
490 {
491         struct log_c *lc = (struct log_c *) log->context;
492
493         dm_put_device(lc->ti, lc->log_dev);
494         vfree(lc->disk_header);
495         dm_io_client_destroy(lc->io_req.client);
496         destroy_log_context(lc);
497 }
498
499 static int count_bits32(uint32_t *addr, unsigned size)
500 {
501         int count = 0, i;
502
503         for (i = 0; i < size; i++) {
504                 count += hweight32(*(addr+i));
505         }
506         return count;
507 }
508
509 static void fail_log_device(struct log_c *lc)
510 {
511         if (lc->log_dev_failed)
512                 return;
513
514         lc->log_dev_failed = 1;
515         dm_table_event(lc->ti->table);
516 }
517
518 static int disk_resume(struct dirty_log *log)
519 {
520         int r;
521         unsigned i;
522         struct log_c *lc = (struct log_c *) log->context;
523         size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
524
525         /* read the disk header */
526         r = read_header(lc);
527         if (r) {
528                 DMWARN("%s: Failed to read header on dirty region log device",
529                        lc->log_dev->name);
530                 fail_log_device(lc);
531                 /*
532                  * If the log device cannot be read, we must assume
533                  * all regions are out-of-sync.  If we simply return
534                  * here, the state will be uninitialized and could
535                  * lead us to return 'in-sync' status for regions
536                  * that are actually 'out-of-sync'.
537                  */
538                 lc->header.nr_regions = 0;
539         }
540
541         /* set or clear any new bits -- device has grown */
542         if (lc->sync == NOSYNC)
543                 for (i = lc->header.nr_regions; i < lc->region_count; i++)
544                         /* FIXME: amazingly inefficient */
545                         log_set_bit(lc, lc->clean_bits, i);
546         else
547                 for (i = lc->header.nr_regions; i < lc->region_count; i++)
548                         /* FIXME: amazingly inefficient */
549                         log_clear_bit(lc, lc->clean_bits, i);
550
551         /* clear any old bits -- device has shrunk */
552         for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
553                 log_clear_bit(lc, lc->clean_bits, i);
554
555         /* copy clean across to sync */
556         memcpy(lc->sync_bits, lc->clean_bits, size);
557         lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
558         lc->sync_search = 0;
559
560         /* set the correct number of regions in the header */
561         lc->header.nr_regions = lc->region_count;
562
563         /* write the new header */
564         r = write_header(lc);
565         if (r) {
566                 DMWARN("%s: Failed to write header on dirty region log device",
567                        lc->log_dev->name);
568                 fail_log_device(lc);
569         }
570
571         return r;
572 }
573
574 static uint32_t core_get_region_size(struct dirty_log *log)
575 {
576         struct log_c *lc = (struct log_c *) log->context;
577         return lc->region_size;
578 }
579
580 static int core_resume(struct dirty_log *log)
581 {
582         struct log_c *lc = (struct log_c *) log->context;
583         lc->sync_search = 0;
584         return 0;
585 }
586
587 static int core_is_clean(struct dirty_log *log, region_t region)
588 {
589         struct log_c *lc = (struct log_c *) log->context;
590         return log_test_bit(lc->clean_bits, region);
591 }
592
593 static int core_in_sync(struct dirty_log *log, region_t region, int block)
594 {
595         struct log_c *lc = (struct log_c *) log->context;
596         return log_test_bit(lc->sync_bits, region);
597 }
598
599 static int core_flush(struct dirty_log *log)
600 {
601         /* no op */
602         return 0;
603 }
604
605 static int disk_flush(struct dirty_log *log)
606 {
607         int r;
608         struct log_c *lc = (struct log_c *) log->context;
609
610         /* only write if the log has changed */
611         if (!lc->touched)
612                 return 0;
613
614         r = write_header(lc);
615         if (r)
616                 fail_log_device(lc);
617         else
618                 lc->touched = 0;
619
620         return r;
621 }
622
623 static void core_mark_region(struct dirty_log *log, region_t region)
624 {
625         struct log_c *lc = (struct log_c *) log->context;
626         log_clear_bit(lc, lc->clean_bits, region);
627 }
628
629 static void core_clear_region(struct dirty_log *log, region_t region)
630 {
631         struct log_c *lc = (struct log_c *) log->context;
632         log_set_bit(lc, lc->clean_bits, region);
633 }
634
635 static int core_get_resync_work(struct dirty_log *log, region_t *region)
636 {
637         struct log_c *lc = (struct log_c *) log->context;
638
639         if (lc->sync_search >= lc->region_count)
640                 return 0;
641
642         do {
643                 *region = ext2_find_next_zero_bit(
644                                              (unsigned long *) lc->sync_bits,
645                                              lc->region_count,
646                                              lc->sync_search);
647                 lc->sync_search = *region + 1;
648
649                 if (*region >= lc->region_count)
650                         return 0;
651
652         } while (log_test_bit(lc->recovering_bits, *region));
653
654         log_set_bit(lc, lc->recovering_bits, *region);
655         return 1;
656 }
657
658 static void core_set_region_sync(struct dirty_log *log, region_t region,
659                                  int in_sync)
660 {
661         struct log_c *lc = (struct log_c *) log->context;
662
663         log_clear_bit(lc, lc->recovering_bits, region);
664         if (in_sync) {
665                 log_set_bit(lc, lc->sync_bits, region);
666                 lc->sync_count++;
667         } else if (log_test_bit(lc->sync_bits, region)) {
668                 lc->sync_count--;
669                 log_clear_bit(lc, lc->sync_bits, region);
670         }
671 }
672
673 static region_t core_get_sync_count(struct dirty_log *log)
674 {
675         struct log_c *lc = (struct log_c *) log->context;
676
677         return lc->sync_count;
678 }
679
680 #define DMEMIT_SYNC \
681         if (lc->sync != DEFAULTSYNC) \
682                 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
683
684 static int core_status(struct dirty_log *log, status_type_t status,
685                        char *result, unsigned int maxlen)
686 {
687         int sz = 0;
688         struct log_c *lc = log->context;
689
690         switch(status) {
691         case STATUSTYPE_INFO:
692                 DMEMIT("1 %s", log->type->name);
693                 break;
694
695         case STATUSTYPE_TABLE:
696                 DMEMIT("%s %u %u ", log->type->name,
697                        lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
698                 DMEMIT_SYNC;
699         }
700
701         return sz;
702 }
703
704 static int disk_status(struct dirty_log *log, status_type_t status,
705                        char *result, unsigned int maxlen)
706 {
707         int sz = 0;
708         struct log_c *lc = log->context;
709
710         switch(status) {
711         case STATUSTYPE_INFO:
712                 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
713                        lc->log_dev_failed ? 'D' : 'A');
714                 break;
715
716         case STATUSTYPE_TABLE:
717                 DMEMIT("%s %u %s %u ", log->type->name,
718                        lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
719                        lc->region_size);
720                 DMEMIT_SYNC;
721         }
722
723         return sz;
724 }
725
726 static struct dirty_log_type _core_type = {
727         .name = "core",
728         .module = THIS_MODULE,
729         .ctr = core_ctr,
730         .dtr = core_dtr,
731         .resume = core_resume,
732         .get_region_size = core_get_region_size,
733         .is_clean = core_is_clean,
734         .in_sync = core_in_sync,
735         .flush = core_flush,
736         .mark_region = core_mark_region,
737         .clear_region = core_clear_region,
738         .get_resync_work = core_get_resync_work,
739         .set_region_sync = core_set_region_sync,
740         .get_sync_count = core_get_sync_count,
741         .status = core_status,
742 };
743
744 static struct dirty_log_type _disk_type = {
745         .name = "disk",
746         .module = THIS_MODULE,
747         .ctr = disk_ctr,
748         .dtr = disk_dtr,
749         .postsuspend = disk_flush,
750         .resume = disk_resume,
751         .get_region_size = core_get_region_size,
752         .is_clean = core_is_clean,
753         .in_sync = core_in_sync,
754         .flush = disk_flush,
755         .mark_region = core_mark_region,
756         .clear_region = core_clear_region,
757         .get_resync_work = core_get_resync_work,
758         .set_region_sync = core_set_region_sync,
759         .get_sync_count = core_get_sync_count,
760         .status = disk_status,
761 };
762
763 int __init dm_dirty_log_init(void)
764 {
765         int r;
766
767         r = dm_register_dirty_log_type(&_core_type);
768         if (r)
769                 DMWARN("couldn't register core log");
770
771         r = dm_register_dirty_log_type(&_disk_type);
772         if (r) {
773                 DMWARN("couldn't register disk type");
774                 dm_unregister_dirty_log_type(&_core_type);
775         }
776
777         return r;
778 }
779
780 void dm_dirty_log_exit(void)
781 {
782         dm_unregister_dirty_log_type(&_disk_type);
783         dm_unregister_dirty_log_type(&_core_type);
784 }
785
786 EXPORT_SYMBOL(dm_register_dirty_log_type);
787 EXPORT_SYMBOL(dm_unregister_dirty_log_type);
788 EXPORT_SYMBOL(dm_create_dirty_log);
789 EXPORT_SYMBOL(dm_destroy_dirty_log);