Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Sistina Software Limited. |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 3 | * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 8 | #include "dm-bio-record.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/mempool.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 16 | #include <linux/device-mapper.h> |
Alasdair G Kergon | a765e20 | 2008-04-24 22:02:01 +0100 | [diff] [blame] | 17 | #include <linux/dm-io.h> |
| 18 | #include <linux/dm-dirty-log.h> |
| 19 | #include <linux/dm-kcopyd.h> |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 20 | #include <linux/dm-region-hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 22 | #define DM_MSG_PREFIX "raid1" |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 23 | |
| 24 | #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */ |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 25 | #define DM_IO_PAGES 64 |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 26 | #define DM_KCOPYD_PAGES 64 |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 27 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 28 | #define DM_RAID1_HANDLE_ERRORS 0x01 |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 29 | #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS) |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 30 | |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 31 | static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /*----------------------------------------------------------------- |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 34 | * Mirror set structures. |
| 35 | *---------------------------------------------------------------*/ |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 36 | enum dm_raid1_error { |
| 37 | DM_RAID1_WRITE_ERROR, |
Mikulas Patocka | 64b30c4 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 38 | DM_RAID1_FLUSH_ERROR, |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 39 | DM_RAID1_SYNC_ERROR, |
| 40 | DM_RAID1_READ_ERROR |
| 41 | }; |
| 42 | |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 43 | struct mirror { |
Jonathan Brassow | aa5617c | 2007-10-19 22:47:58 +0100 | [diff] [blame] | 44 | struct mirror_set *ms; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 45 | atomic_t error_count; |
Al Viro | 39ed7ad | 2008-02-13 03:53:00 +0000 | [diff] [blame] | 46 | unsigned long error_type; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 47 | struct dm_dev *dev; |
| 48 | sector_t offset; |
| 49 | }; |
| 50 | |
| 51 | struct mirror_set { |
| 52 | struct dm_target *ti; |
| 53 | struct list_head list; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 54 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 55 | uint64_t features; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 56 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 57 | spinlock_t lock; /* protects the lists */ |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 58 | struct bio_list reads; |
| 59 | struct bio_list writes; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 60 | struct bio_list failures; |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 61 | struct bio_list holds; /* bios are waiting until suspend */ |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 62 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 63 | struct dm_region_hash *rh; |
| 64 | struct dm_kcopyd_client *kcopyd_client; |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 65 | struct dm_io_client *io_client; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 66 | mempool_t *read_record_pool; |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 67 | |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 68 | /* recovery */ |
| 69 | region_t nr_regions; |
| 70 | int in_sync; |
Jonathan Brassow | fc1ff95 | 2007-07-12 17:29:15 +0100 | [diff] [blame] | 71 | int log_failure; |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 72 | int leg_failure; |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 73 | atomic_t suspend; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 74 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 75 | atomic_t default_mirror; /* Default mirror */ |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 76 | |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 77 | struct workqueue_struct *kmirrord_wq; |
| 78 | struct work_struct kmirrord_work; |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 79 | struct timer_list timer; |
| 80 | unsigned long timer_pending; |
| 81 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 82 | struct work_struct trigger_event; |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 83 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 84 | unsigned nr_mirrors; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 85 | struct mirror mirror[0]; |
| 86 | }; |
| 87 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 88 | static void wakeup_mirrord(void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 90 | struct mirror_set *ms = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 92 | queue_work(ms->kmirrord_wq, &ms->kmirrord_work); |
| 93 | } |
| 94 | |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 95 | static void delayed_wake_fn(unsigned long data) |
| 96 | { |
| 97 | struct mirror_set *ms = (struct mirror_set *) data; |
| 98 | |
| 99 | clear_bit(0, &ms->timer_pending); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 100 | wakeup_mirrord(ms); |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void delayed_wake(struct mirror_set *ms) |
| 104 | { |
| 105 | if (test_and_set_bit(0, &ms->timer_pending)) |
| 106 | return; |
| 107 | |
| 108 | ms->timer.expires = jiffies + HZ / 5; |
| 109 | ms->timer.data = (unsigned long) ms; |
| 110 | ms->timer.function = delayed_wake_fn; |
| 111 | add_timer(&ms->timer); |
| 112 | } |
| 113 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 114 | static void wakeup_all_recovery_waiters(void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 116 | wake_up_all(&_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 119 | static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | int should_wake = 0; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 123 | struct bio_list *bl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 125 | bl = (rw == WRITE) ? &ms->writes : &ms->reads; |
| 126 | spin_lock_irqsave(&ms->lock, flags); |
| 127 | should_wake = !(bl->head); |
| 128 | bio_list_add(bl, bio); |
| 129 | spin_unlock_irqrestore(&ms->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | if (should_wake) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 132 | wakeup_mirrord(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 135 | static void dispatch_bios(void *context, struct bio_list *bio_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 137 | struct mirror_set *ms = context; |
| 138 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 140 | while ((bio = bio_list_pop(bio_list))) |
| 141 | queue_bio(ms, bio, WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 144 | #define MIN_READ_RECORDS 20 |
| 145 | struct dm_raid1_read_record { |
| 146 | struct mirror *m; |
| 147 | struct dm_bio_details details; |
| 148 | }; |
| 149 | |
Mikulas Patocka | 95f8fac | 2009-04-02 19:55:24 +0100 | [diff] [blame] | 150 | static struct kmem_cache *_dm_raid1_read_record_cache; |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
| 153 | * Every mirror should look like this one. |
| 154 | */ |
| 155 | #define DEFAULT_MIRROR 0 |
| 156 | |
| 157 | /* |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 158 | * This is yucky. We squirrel the mirror struct away inside |
| 159 | * bi_next for read/write buffers. This is safe since the bh |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | * doesn't get submitted to the lower levels of block layer. |
| 161 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 162 | static struct mirror *bio_get_m(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 164 | return (struct mirror *) bio->bi_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 167 | static void bio_set_m(struct bio *bio, struct mirror *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 169 | bio->bi_next = (struct bio *) m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 172 | static struct mirror *get_default_mirror(struct mirror_set *ms) |
| 173 | { |
| 174 | return &ms->mirror[atomic_read(&ms->default_mirror)]; |
| 175 | } |
| 176 | |
| 177 | static void set_default_mirror(struct mirror *m) |
| 178 | { |
| 179 | struct mirror_set *ms = m->ms; |
| 180 | struct mirror *m0 = &(ms->mirror[0]); |
| 181 | |
| 182 | atomic_set(&ms->default_mirror, m - m0); |
| 183 | } |
| 184 | |
Mikulas Patocka | 87968dd | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 185 | static struct mirror *get_valid_mirror(struct mirror_set *ms) |
| 186 | { |
| 187 | struct mirror *m; |
| 188 | |
| 189 | for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++) |
| 190 | if (!atomic_read(&m->error_count)) |
| 191 | return m; |
| 192 | |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 196 | /* fail_mirror |
| 197 | * @m: mirror device to fail |
| 198 | * @error_type: one of the enum's, DM_RAID1_*_ERROR |
| 199 | * |
| 200 | * If errors are being handled, record the type of |
| 201 | * error encountered for this device. If this type |
| 202 | * of error has already been recorded, we can return; |
| 203 | * otherwise, we must signal userspace by triggering |
| 204 | * an event. Additionally, if the device is the |
| 205 | * primary device, we must choose a new primary, but |
| 206 | * only if the mirror is in-sync. |
| 207 | * |
| 208 | * This function must not block. |
| 209 | */ |
| 210 | static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type) |
| 211 | { |
| 212 | struct mirror_set *ms = m->ms; |
| 213 | struct mirror *new; |
| 214 | |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 215 | ms->leg_failure = 1; |
| 216 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 217 | /* |
| 218 | * error_count is used for nothing more than a |
| 219 | * simple way to tell if a device has encountered |
| 220 | * errors. |
| 221 | */ |
| 222 | atomic_inc(&m->error_count); |
| 223 | |
| 224 | if (test_and_set_bit(error_type, &m->error_type)) |
| 225 | return; |
| 226 | |
Jonathan Brassow | d460c65 | 2009-01-06 03:04:57 +0000 | [diff] [blame] | 227 | if (!errors_handled(ms)) |
| 228 | return; |
| 229 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 230 | if (m != get_default_mirror(ms)) |
| 231 | goto out; |
| 232 | |
| 233 | if (!ms->in_sync) { |
| 234 | /* |
| 235 | * Better to issue requests to same failing device |
| 236 | * than to risk returning corrupt data. |
| 237 | */ |
| 238 | DMERR("Primary mirror (%s) failed while out-of-sync: " |
| 239 | "Reads may fail.", m->dev->name); |
| 240 | goto out; |
| 241 | } |
| 242 | |
Mikulas Patocka | 87968dd | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 243 | new = get_valid_mirror(ms); |
| 244 | if (new) |
| 245 | set_default_mirror(new); |
| 246 | else |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 247 | DMWARN("All sides of mirror have failed."); |
| 248 | |
| 249 | out: |
| 250 | schedule_work(&ms->trigger_event); |
| 251 | } |
| 252 | |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 253 | static int mirror_flush(struct dm_target *ti) |
| 254 | { |
| 255 | struct mirror_set *ms = ti->private; |
| 256 | unsigned long error_bits; |
| 257 | |
| 258 | unsigned int i; |
| 259 | struct dm_io_region io[ms->nr_mirrors]; |
| 260 | struct mirror *m; |
| 261 | struct dm_io_request io_req = { |
| 262 | .bi_rw = WRITE_BARRIER, |
| 263 | .mem.type = DM_IO_KMEM, |
| 264 | .mem.ptr.bvec = NULL, |
| 265 | .client = ms->io_client, |
| 266 | }; |
| 267 | |
| 268 | for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) { |
| 269 | io[i].bdev = m->dev->bdev; |
| 270 | io[i].sector = 0; |
| 271 | io[i].count = 0; |
| 272 | } |
| 273 | |
| 274 | error_bits = -1; |
| 275 | dm_io(&io_req, ms->nr_mirrors, io, &error_bits); |
| 276 | if (unlikely(error_bits != 0)) { |
| 277 | for (i = 0; i < ms->nr_mirrors; i++) |
| 278 | if (test_bit(i, &error_bits)) |
| 279 | fail_mirror(ms->mirror + i, |
Mikulas Patocka | 64b30c4 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 280 | DM_RAID1_FLUSH_ERROR); |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 281 | return -EIO; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | /*----------------------------------------------------------------- |
| 288 | * Recovery. |
| 289 | * |
| 290 | * When a mirror is first activated we may find that some regions |
| 291 | * are in the no-sync state. We have to recover these by |
| 292 | * recopying from the default mirror to all the others. |
| 293 | *---------------------------------------------------------------*/ |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 294 | static void recovery_complete(int read_err, unsigned long write_err, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | void *context) |
| 296 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 297 | struct dm_region *reg = context; |
| 298 | struct mirror_set *ms = dm_rh_region_context(reg); |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 299 | int m, bit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 301 | if (read_err) { |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 302 | /* Read error means the failure of default mirror. */ |
| 303 | DMERR_LIMIT("Unable to read primary mirror during recovery"); |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 304 | fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR); |
| 305 | } |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 306 | |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 307 | if (write_err) { |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 308 | DMERR_LIMIT("Write error during recovery (error = 0x%lx)", |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 309 | write_err); |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 310 | /* |
| 311 | * Bits correspond to devices (excluding default mirror). |
| 312 | * The default mirror cannot change during recovery. |
| 313 | */ |
| 314 | for (m = 0; m < ms->nr_mirrors; m++) { |
| 315 | if (&ms->mirror[m] == get_default_mirror(ms)) |
| 316 | continue; |
| 317 | if (test_bit(bit, &write_err)) |
| 318 | fail_mirror(ms->mirror + m, |
| 319 | DM_RAID1_SYNC_ERROR); |
| 320 | bit++; |
| 321 | } |
| 322 | } |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 323 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 324 | dm_rh_recovery_end(reg, !(read_err || write_err)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 327 | static int recover(struct mirror_set *ms, struct dm_region *reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | int r; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 330 | unsigned i; |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 331 | struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | struct mirror *m; |
| 333 | unsigned long flags = 0; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 334 | region_t key = dm_rh_get_region_key(reg); |
| 335 | sector_t region_size = dm_rh_get_region_size(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | /* fill in the source */ |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 338 | m = get_default_mirror(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | from.bdev = m->dev->bdev; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 340 | from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key); |
| 341 | if (key == (ms->nr_regions - 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* |
| 343 | * The final region may be smaller than |
| 344 | * region_size. |
| 345 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 346 | from.count = ms->ti->len & (region_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (!from.count) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 348 | from.count = region_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } else |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 350 | from.count = region_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | /* fill in the destinations */ |
| 353 | for (i = 0, dest = to; i < ms->nr_mirrors; i++) { |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 354 | if (&ms->mirror[i] == get_default_mirror(ms)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | continue; |
| 356 | |
| 357 | m = ms->mirror + i; |
| 358 | dest->bdev = m->dev->bdev; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 359 | dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | dest->count = from.count; |
| 361 | dest++; |
| 362 | } |
| 363 | |
| 364 | /* hand to kcopyd */ |
Jonathan Brassow | f7c83e2 | 2008-10-10 13:36:59 +0100 | [diff] [blame] | 365 | if (!errors_handled(ms)) |
| 366 | set_bit(DM_KCOPYD_IGNORE_ERROR, &flags); |
| 367 | |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 368 | r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, |
| 369 | flags, recovery_complete, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | return r; |
| 372 | } |
| 373 | |
| 374 | static void do_recovery(struct mirror_set *ms) |
| 375 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 376 | struct dm_region *reg; |
| 377 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * Start quiescing some regions. |
| 382 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 383 | dm_rh_recovery_prepare(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Copy any already quiesced regions. |
| 387 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 388 | while ((reg = dm_rh_recovery_start(ms->rh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | r = recover(ms, reg); |
| 390 | if (r) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 391 | dm_rh_recovery_end(reg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Update the in sync flag. |
| 396 | */ |
| 397 | if (!ms->in_sync && |
| 398 | (log->type->get_sync_count(log) == ms->nr_regions)) { |
| 399 | /* the sync is complete */ |
| 400 | dm_table_event(ms->ti->table); |
| 401 | ms->in_sync = 1; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /*----------------------------------------------------------------- |
| 406 | * Reads |
| 407 | *---------------------------------------------------------------*/ |
| 408 | static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector) |
| 409 | { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 410 | struct mirror *m = get_default_mirror(ms); |
| 411 | |
| 412 | do { |
| 413 | if (likely(!atomic_read(&m->error_count))) |
| 414 | return m; |
| 415 | |
| 416 | if (m-- == ms->mirror) |
| 417 | m += ms->nr_mirrors; |
| 418 | } while (m != get_default_mirror(ms)); |
| 419 | |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | static int default_ok(struct mirror *m) |
| 424 | { |
| 425 | struct mirror *default_mirror = get_default_mirror(m->ms); |
| 426 | |
| 427 | return !atomic_read(&default_mirror->error_count); |
| 428 | } |
| 429 | |
| 430 | static int mirror_available(struct mirror_set *ms, struct bio *bio) |
| 431 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 432 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
| 433 | region_t region = dm_rh_bio_to_region(ms->rh, bio); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 434 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 435 | if (log->type->in_sync(log, region, 0)) |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 436 | return choose_mirror(ms, bio->bi_sector) ? 1 : 0; |
| 437 | |
| 438 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* |
| 442 | * remap a buffer to a particular mirror. |
| 443 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 444 | static sector_t map_sector(struct mirror *m, struct bio *bio) |
| 445 | { |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 446 | if (unlikely(!bio->bi_size)) |
| 447 | return 0; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 448 | return m->offset + (bio->bi_sector - m->ms->ti->begin); |
| 449 | } |
| 450 | |
| 451 | static void map_bio(struct mirror *m, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
| 453 | bio->bi_bdev = m->dev->bdev; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 454 | bio->bi_sector = map_sector(m, bio); |
| 455 | } |
| 456 | |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 457 | static void map_region(struct dm_io_region *io, struct mirror *m, |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 458 | struct bio *bio) |
| 459 | { |
| 460 | io->bdev = m->dev->bdev; |
| 461 | io->sector = map_sector(m, bio); |
| 462 | io->count = bio->bi_size >> 9; |
| 463 | } |
| 464 | |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 465 | static void hold_bio(struct mirror_set *ms, struct bio *bio) |
| 466 | { |
| 467 | /* |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 468 | * Lock is required to avoid race condition during suspend |
| 469 | * process. |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 470 | */ |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 471 | spin_lock_irq(&ms->lock); |
| 472 | |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 473 | if (atomic_read(&ms->suspend)) { |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 474 | spin_unlock_irq(&ms->lock); |
| 475 | |
| 476 | /* |
| 477 | * If device is suspended, complete the bio. |
| 478 | */ |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 479 | if (dm_noflush_suspending(ms->ti)) |
| 480 | bio_endio(bio, DM_ENDIO_REQUEUE); |
| 481 | else |
| 482 | bio_endio(bio, -EIO); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Hold bio until the suspend is complete. |
| 488 | */ |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 489 | bio_list_add(&ms->holds, bio); |
| 490 | spin_unlock_irq(&ms->lock); |
| 491 | } |
| 492 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 493 | /*----------------------------------------------------------------- |
| 494 | * Reads |
| 495 | *---------------------------------------------------------------*/ |
| 496 | static void read_callback(unsigned long error, void *context) |
| 497 | { |
| 498 | struct bio *bio = context; |
| 499 | struct mirror *m; |
| 500 | |
| 501 | m = bio_get_m(bio); |
| 502 | bio_set_m(bio, NULL); |
| 503 | |
| 504 | if (likely(!error)) { |
| 505 | bio_endio(bio, 0); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | fail_mirror(m, DM_RAID1_READ_ERROR); |
| 510 | |
| 511 | if (likely(default_ok(m)) || mirror_available(m->ms, bio)) { |
| 512 | DMWARN_LIMIT("Read failure on mirror device %s. " |
| 513 | "Trying alternative device.", |
| 514 | m->dev->name); |
| 515 | queue_bio(m->ms, bio, bio_rw(bio)); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.", |
| 520 | m->dev->name); |
| 521 | bio_endio(bio, -EIO); |
| 522 | } |
| 523 | |
| 524 | /* Asynchronous read. */ |
| 525 | static void read_async_bio(struct mirror *m, struct bio *bio) |
| 526 | { |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 527 | struct dm_io_region io; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 528 | struct dm_io_request io_req = { |
| 529 | .bi_rw = READ, |
| 530 | .mem.type = DM_IO_BVEC, |
| 531 | .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx, |
| 532 | .notify.fn = read_callback, |
| 533 | .notify.context = bio, |
| 534 | .client = m->ms->io_client, |
| 535 | }; |
| 536 | |
| 537 | map_region(&io, m, bio); |
| 538 | bio_set_m(bio, m); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 539 | BUG_ON(dm_io(&io_req, 1, &io, NULL)); |
| 540 | } |
| 541 | |
| 542 | static inline int region_in_sync(struct mirror_set *ms, region_t region, |
| 543 | int may_block) |
| 544 | { |
| 545 | int state = dm_rh_get_state(ms->rh, region, may_block); |
| 546 | return state == DM_RH_CLEAN || state == DM_RH_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static void do_reads(struct mirror_set *ms, struct bio_list *reads) |
| 550 | { |
| 551 | region_t region; |
| 552 | struct bio *bio; |
| 553 | struct mirror *m; |
| 554 | |
| 555 | while ((bio = bio_list_pop(reads))) { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 556 | region = dm_rh_bio_to_region(ms->rh, bio); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 557 | m = get_default_mirror(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | /* |
| 560 | * We can only read balance if the region is in sync. |
| 561 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 562 | if (likely(region_in_sync(ms, region, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | m = choose_mirror(ms, bio->bi_sector); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 564 | else if (m && atomic_read(&m->error_count)) |
| 565 | m = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 567 | if (likely(m)) |
| 568 | read_async_bio(m, bio); |
| 569 | else |
| 570 | bio_endio(bio, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | /*----------------------------------------------------------------- |
| 575 | * Writes. |
| 576 | * |
| 577 | * We do different things with the write io depending on the |
| 578 | * state of the region that it's in: |
| 579 | * |
| 580 | * SYNC: increment pending, use kcopyd to write to *all* mirrors |
| 581 | * RECOVERING: delay the io until recovery completes |
| 582 | * NOSYNC: increment pending, just write to the default mirror |
| 583 | *---------------------------------------------------------------*/ |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 584 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | static void write_callback(unsigned long error, void *context) |
| 587 | { |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 588 | unsigned i, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | struct bio *bio = (struct bio *) context; |
| 590 | struct mirror_set *ms; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 591 | int should_wake = 0; |
| 592 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 594 | ms = bio_get_m(bio)->ms; |
| 595 | bio_set_m(bio, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * NOTE: We don't decrement the pending count here, |
| 599 | * instead it is done by the targets endio function. |
| 600 | * This way we handle both writes to SYNC and NOSYNC |
| 601 | * regions with the same code. |
| 602 | */ |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 603 | if (likely(!error)) { |
| 604 | bio_endio(bio, ret); |
| 605 | return; |
| 606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 608 | for (i = 0; i < ms->nr_mirrors; i++) |
| 609 | if (test_bit(i, &error)) |
| 610 | fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 611 | |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 612 | /* |
| 613 | * Need to raise event. Since raising |
| 614 | * events can block, we need to do it in |
| 615 | * the main thread. |
| 616 | */ |
| 617 | spin_lock_irqsave(&ms->lock, flags); |
| 618 | if (!ms->failures.head) |
| 619 | should_wake = 1; |
| 620 | bio_list_add(&ms->failures, bio); |
| 621 | spin_unlock_irqrestore(&ms->lock, flags); |
| 622 | if (should_wake) |
| 623 | wakeup_mirrord(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static void do_write(struct mirror_set *ms, struct bio *bio) |
| 627 | { |
| 628 | unsigned int i; |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 629 | struct dm_io_region io[ms->nr_mirrors], *dest = io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | struct mirror *m; |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 631 | struct dm_io_request io_req = { |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 632 | .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER), |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 633 | .mem.type = DM_IO_BVEC, |
| 634 | .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx, |
| 635 | .notify.fn = write_callback, |
| 636 | .notify.context = bio, |
| 637 | .client = ms->io_client, |
| 638 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 640 | for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) |
| 641 | map_region(dest++, m, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 643 | /* |
| 644 | * Use default mirror because we only need it to retrieve the reference |
| 645 | * to the mirror set in write_callback(). |
| 646 | */ |
| 647 | bio_set_m(bio, get_default_mirror(ms)); |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 648 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 649 | BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static void do_writes(struct mirror_set *ms, struct bio_list *writes) |
| 653 | { |
| 654 | int state; |
| 655 | struct bio *bio; |
| 656 | struct bio_list sync, nosync, recover, *this_list = NULL; |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 657 | struct bio_list requeue; |
| 658 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
| 659 | region_t region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | if (!writes->head) |
| 662 | return; |
| 663 | |
| 664 | /* |
| 665 | * Classify each write. |
| 666 | */ |
| 667 | bio_list_init(&sync); |
| 668 | bio_list_init(&nosync); |
| 669 | bio_list_init(&recover); |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 670 | bio_list_init(&requeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | while ((bio = bio_list_pop(writes))) { |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 673 | if (unlikely(bio_empty_barrier(bio))) { |
| 674 | bio_list_add(&sync, bio); |
| 675 | continue; |
| 676 | } |
| 677 | |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 678 | region = dm_rh_bio_to_region(ms->rh, bio); |
| 679 | |
| 680 | if (log->type->is_remote_recovering && |
| 681 | log->type->is_remote_recovering(log, region)) { |
| 682 | bio_list_add(&requeue, bio); |
| 683 | continue; |
| 684 | } |
| 685 | |
| 686 | state = dm_rh_get_state(ms->rh, region, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | switch (state) { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 688 | case DM_RH_CLEAN: |
| 689 | case DM_RH_DIRTY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | this_list = &sync; |
| 691 | break; |
| 692 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 693 | case DM_RH_NOSYNC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | this_list = &nosync; |
| 695 | break; |
| 696 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 697 | case DM_RH_RECOVERING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | this_list = &recover; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | bio_list_add(this_list, bio); |
| 703 | } |
| 704 | |
| 705 | /* |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 706 | * Add bios that are delayed due to remote recovery |
| 707 | * back on to the write queue |
| 708 | */ |
| 709 | if (unlikely(requeue.head)) { |
| 710 | spin_lock_irq(&ms->lock); |
| 711 | bio_list_merge(&ms->writes, &requeue); |
| 712 | spin_unlock_irq(&ms->lock); |
Mikulas Patocka | 6988568 | 2009-07-23 20:30:37 +0100 | [diff] [blame] | 713 | delayed_wake(ms); |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | * Increment the pending counts for any regions that will |
| 718 | * be written to (writes to recover regions are going to |
| 719 | * be delayed). |
| 720 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 721 | dm_rh_inc_pending(ms->rh, &sync); |
| 722 | dm_rh_inc_pending(ms->rh, &nosync); |
Jonathan Brassow | d2b6986 | 2009-09-04 20:40:32 +0100 | [diff] [blame] | 723 | |
| 724 | /* |
| 725 | * If the flush fails on a previous call and succeeds here, |
| 726 | * we must not reset the log_failure variable. We need |
| 727 | * userspace interaction to do that. |
| 728 | */ |
| 729 | ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | /* |
| 732 | * Dispatch io. |
| 733 | */ |
Mikulas Patocka | 5528d17 | 2010-02-16 18:42:55 +0000 | [diff] [blame] | 734 | if (unlikely(ms->log_failure) && errors_handled(ms)) { |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 735 | spin_lock_irq(&ms->lock); |
| 736 | bio_list_merge(&ms->failures, &sync); |
| 737 | spin_unlock_irq(&ms->lock); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 738 | wakeup_mirrord(ms); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 739 | } else |
Jonathan Brassow | fc1ff95 | 2007-07-12 17:29:15 +0100 | [diff] [blame] | 740 | while ((bio = bio_list_pop(&sync))) |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 741 | do_write(ms, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | while ((bio = bio_list_pop(&recover))) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 744 | dm_rh_delay(ms->rh, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | while ((bio = bio_list_pop(&nosync))) { |
Mikulas Patocka | ede5ea0 | 2010-03-06 02:32:22 +0000 | [diff] [blame] | 747 | if (unlikely(ms->leg_failure) && errors_handled(ms)) { |
| 748 | spin_lock_irq(&ms->lock); |
| 749 | bio_list_add(&ms->failures, bio); |
| 750 | spin_unlock_irq(&ms->lock); |
| 751 | wakeup_mirrord(ms); |
| 752 | } else { |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 753 | map_bio(get_default_mirror(ms), bio); |
| 754 | generic_make_request(bio); |
| 755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 759 | static void do_failures(struct mirror_set *ms, struct bio_list *failures) |
| 760 | { |
| 761 | struct bio *bio; |
| 762 | |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 763 | if (likely(!failures->head)) |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 764 | return; |
| 765 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 766 | /* |
| 767 | * If the log has failed, unattempted writes are being |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 768 | * put on the holds list. We can't issue those writes |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 769 | * until a log has been marked, so we must store them. |
| 770 | * |
| 771 | * If a 'noflush' suspend is in progress, we can requeue |
| 772 | * the I/O's to the core. This give userspace a chance |
| 773 | * to reconfigure the mirror, at which point the core |
| 774 | * will reissue the writes. If the 'noflush' flag is |
| 775 | * not set, we have no choice but to return errors. |
| 776 | * |
| 777 | * Some writes on the failures list may have been |
| 778 | * submitted before the log failure and represent a |
| 779 | * failure to write to one of the devices. It is ok |
| 780 | * for us to treat them the same and requeue them |
| 781 | * as well. |
| 782 | */ |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 783 | while ((bio = bio_list_pop(failures))) { |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 784 | if (!ms->log_failure) { |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 785 | ms->in_sync = 0; |
Mikulas Patocka | c58098b | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 786 | dm_rh_mark_nosync(ms->rh, bio); |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 787 | } |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * If all the legs are dead, fail the I/O. |
| 791 | * If we have been told to handle errors, hold the bio |
| 792 | * and wait for userspace to deal with the problem. |
| 793 | * Otherwise pretend that the I/O succeeded. (This would |
| 794 | * be wrong if the failed leg returned after reboot and |
| 795 | * got replicated back to the good legs.) |
| 796 | */ |
| 797 | if (!get_valid_mirror(ms)) |
| 798 | bio_endio(bio, -EIO); |
| 799 | else if (errors_handled(ms)) |
| 800 | hold_bio(ms, bio); |
| 801 | else |
| 802 | bio_endio(bio, 0); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 803 | } |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void trigger_event(struct work_struct *work) |
| 807 | { |
| 808 | struct mirror_set *ms = |
| 809 | container_of(work, struct mirror_set, trigger_event); |
| 810 | |
| 811 | dm_table_event(ms->ti->table); |
| 812 | } |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | /*----------------------------------------------------------------- |
| 815 | * kmirrord |
| 816 | *---------------------------------------------------------------*/ |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 817 | static void do_mirror(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 819 | struct mirror_set *ms = container_of(work, struct mirror_set, |
| 820 | kmirrord_work); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 821 | struct bio_list reads, writes, failures; |
| 822 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 824 | spin_lock_irqsave(&ms->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | reads = ms->reads; |
| 826 | writes = ms->writes; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 827 | failures = ms->failures; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | bio_list_init(&ms->reads); |
| 829 | bio_list_init(&ms->writes); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 830 | bio_list_init(&ms->failures); |
| 831 | spin_unlock_irqrestore(&ms->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 833 | dm_rh_update_states(ms->rh, errors_handled(ms)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | do_recovery(ms); |
| 835 | do_reads(ms, &reads); |
| 836 | do_writes(ms, &writes); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 837 | do_failures(ms, &failures); |
Mikulas Patocka | 7ff14a3 | 2008-04-24 22:10:47 +0100 | [diff] [blame] | 838 | |
| 839 | dm_table_unplug_all(ms->ti->table); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | /*----------------------------------------------------------------- |
| 843 | * Target functions |
| 844 | *---------------------------------------------------------------*/ |
| 845 | static struct mirror_set *alloc_context(unsigned int nr_mirrors, |
| 846 | uint32_t region_size, |
| 847 | struct dm_target *ti, |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 848 | struct dm_dirty_log *dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
| 850 | size_t len; |
| 851 | struct mirror_set *ms = NULL; |
| 852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors); |
| 854 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 855 | ms = kzalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (!ms) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 857 | ti->error = "Cannot allocate mirror context"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | return NULL; |
| 859 | } |
| 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | spin_lock_init(&ms->lock); |
Mikulas Patocka | 5339fc2 | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 862 | bio_list_init(&ms->reads); |
| 863 | bio_list_init(&ms->writes); |
| 864 | bio_list_init(&ms->failures); |
| 865 | bio_list_init(&ms->holds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | ms->ti = ti; |
| 868 | ms->nr_mirrors = nr_mirrors; |
| 869 | ms->nr_regions = dm_sector_div_up(ti->len, region_size); |
| 870 | ms->in_sync = 0; |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 871 | ms->log_failure = 0; |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 872 | ms->leg_failure = 0; |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 873 | atomic_set(&ms->suspend, 0); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 874 | atomic_set(&ms->default_mirror, DEFAULT_MIRROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Mikulas Patocka | 95f8fac | 2009-04-02 19:55:24 +0100 | [diff] [blame] | 876 | ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS, |
| 877 | _dm_raid1_read_record_cache); |
| 878 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 879 | if (!ms->read_record_pool) { |
| 880 | ti->error = "Error creating mirror read_record_pool"; |
| 881 | kfree(ms); |
| 882 | return NULL; |
| 883 | } |
| 884 | |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 885 | ms->io_client = dm_io_client_create(DM_IO_PAGES); |
| 886 | if (IS_ERR(ms->io_client)) { |
| 887 | ti->error = "Error creating dm_io client"; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 888 | mempool_destroy(ms->read_record_pool); |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 889 | kfree(ms); |
| 890 | return NULL; |
| 891 | } |
| 892 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 893 | ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord, |
| 894 | wakeup_all_recovery_waiters, |
| 895 | ms->ti->begin, MAX_RECOVERY, |
| 896 | dl, region_size, ms->nr_regions); |
| 897 | if (IS_ERR(ms->rh)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 898 | ti->error = "Error creating dirty region hash"; |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 899 | dm_io_client_destroy(ms->io_client); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 900 | mempool_destroy(ms->read_record_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | kfree(ms); |
| 902 | return NULL; |
| 903 | } |
| 904 | |
| 905 | return ms; |
| 906 | } |
| 907 | |
| 908 | static void free_context(struct mirror_set *ms, struct dm_target *ti, |
| 909 | unsigned int m) |
| 910 | { |
| 911 | while (m--) |
| 912 | dm_put_device(ti, ms->mirror[m].dev); |
| 913 | |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 914 | dm_io_client_destroy(ms->io_client); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 915 | dm_region_hash_destroy(ms->rh); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 916 | mempool_destroy(ms->read_record_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | kfree(ms); |
| 918 | } |
| 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | static int get_mirror(struct mirror_set *ms, struct dm_target *ti, |
| 921 | unsigned int mirror, char **argv) |
| 922 | { |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 923 | unsigned long long offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 925 | if (sscanf(argv[1], "%llu", &offset) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 926 | ti->error = "Invalid offset"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return -EINVAL; |
| 928 | } |
| 929 | |
Nikanth Karthikesan | 8215d6e | 2010-03-06 02:32:27 +0000 | [diff] [blame] | 930 | if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | &ms->mirror[mirror].dev)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 932 | ti->error = "Device lookup failure"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return -ENXIO; |
| 934 | } |
| 935 | |
Jonathan Brassow | aa5617c | 2007-10-19 22:47:58 +0100 | [diff] [blame] | 936 | ms->mirror[mirror].ms = ms; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 937 | atomic_set(&(ms->mirror[mirror].error_count), 0); |
| 938 | ms->mirror[mirror].error_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | ms->mirror[mirror].offset = offset; |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | /* |
| 945 | * Create dirty log: log_type #log_params <log_params> |
| 946 | */ |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 947 | static struct dm_dirty_log *create_dirty_log(struct dm_target *ti, |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 948 | unsigned argc, char **argv, |
| 949 | unsigned *args_used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 951 | unsigned param_count; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 952 | struct dm_dirty_log *dl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | if (argc < 2) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 955 | ti->error = "Insufficient mirror log arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | return NULL; |
| 957 | } |
| 958 | |
| 959 | if (sscanf(argv[1], "%u", ¶m_count) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 960 | ti->error = "Invalid mirror log argument count"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return NULL; |
| 962 | } |
| 963 | |
| 964 | *args_used = 2 + param_count; |
| 965 | |
| 966 | if (argc < *args_used) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 967 | ti->error = "Insufficient mirror log arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | return NULL; |
| 969 | } |
| 970 | |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 971 | dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count, |
| 972 | argv + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | if (!dl) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 974 | ti->error = "Error creating mirror dirty log"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | return NULL; |
| 976 | } |
| 977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | return dl; |
| 979 | } |
| 980 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 981 | static int parse_features(struct mirror_set *ms, unsigned argc, char **argv, |
| 982 | unsigned *args_used) |
| 983 | { |
| 984 | unsigned num_features; |
| 985 | struct dm_target *ti = ms->ti; |
| 986 | |
| 987 | *args_used = 0; |
| 988 | |
| 989 | if (!argc) |
| 990 | return 0; |
| 991 | |
| 992 | if (sscanf(argv[0], "%u", &num_features) != 1) { |
| 993 | ti->error = "Invalid number of features"; |
| 994 | return -EINVAL; |
| 995 | } |
| 996 | |
| 997 | argc--; |
| 998 | argv++; |
| 999 | (*args_used)++; |
| 1000 | |
| 1001 | if (num_features > argc) { |
| 1002 | ti->error = "Not enough arguments to support feature count"; |
| 1003 | return -EINVAL; |
| 1004 | } |
| 1005 | |
| 1006 | if (!strcmp("handle_errors", argv[0])) |
| 1007 | ms->features |= DM_RAID1_HANDLE_ERRORS; |
| 1008 | else { |
| 1009 | ti->error = "Unrecognised feature requested"; |
| 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | (*args_used)++; |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * Construct a mirror mapping: |
| 1020 | * |
| 1021 | * log_type #log_params <log_params> |
| 1022 | * #mirrors [mirror_path offset]{2,} |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1023 | * [#features <features>] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | * |
| 1025 | * log_type is "core" or "disk" |
| 1026 | * #log_params is between 1 and 3 |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1027 | * |
| 1028 | * If present, features must be "handle_errors". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1031 | { |
| 1032 | int r; |
| 1033 | unsigned int nr_mirrors, m, args_used; |
| 1034 | struct mirror_set *ms; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1035 | struct dm_dirty_log *dl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
| 1037 | dl = create_dirty_log(ti, argc, argv, &args_used); |
| 1038 | if (!dl) |
| 1039 | return -EINVAL; |
| 1040 | |
| 1041 | argv += args_used; |
| 1042 | argc -= args_used; |
| 1043 | |
| 1044 | if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 || |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1045 | nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1046 | ti->error = "Invalid number of mirrors"; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1047 | dm_dirty_log_destroy(dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | |
| 1051 | argv++, argc--; |
| 1052 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1053 | if (argc < nr_mirrors * 2) { |
| 1054 | ti->error = "Too few mirror arguments"; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1055 | dm_dirty_log_destroy(dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | |
| 1059 | ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl); |
| 1060 | if (!ms) { |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1061 | dm_dirty_log_destroy(dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | return -ENOMEM; |
| 1063 | } |
| 1064 | |
| 1065 | /* Get the mirror parameter sets */ |
| 1066 | for (m = 0; m < nr_mirrors; m++) { |
| 1067 | r = get_mirror(ms, ti, m, argv); |
| 1068 | if (r) { |
| 1069 | free_context(ms, ti, m); |
| 1070 | return r; |
| 1071 | } |
| 1072 | argv += 2; |
| 1073 | argc -= 2; |
| 1074 | } |
| 1075 | |
| 1076 | ti->private = ms; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1077 | ti->split_io = dm_rh_get_region_size(ms->rh); |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 1078 | ti->num_flush_requests = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1080 | ms->kmirrord_wq = create_singlethread_workqueue("kmirrord"); |
| 1081 | if (!ms->kmirrord_wq) { |
| 1082 | DMERR("couldn't start kmirrord"); |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1083 | r = -ENOMEM; |
| 1084 | goto err_free_context; |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1085 | } |
| 1086 | INIT_WORK(&ms->kmirrord_work, do_mirror); |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 1087 | init_timer(&ms->timer); |
| 1088 | ms->timer_pending = 0; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 1089 | INIT_WORK(&ms->trigger_event, trigger_event); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1090 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1091 | r = parse_features(ms, argc, argv, &args_used); |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1092 | if (r) |
| 1093 | goto err_destroy_wq; |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1094 | |
| 1095 | argv += args_used; |
| 1096 | argc -= args_used; |
| 1097 | |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 1098 | /* |
| 1099 | * Any read-balancing addition depends on the |
| 1100 | * DM_RAID1_HANDLE_ERRORS flag being present. |
| 1101 | * This is because the decision to balance depends |
| 1102 | * on the sync state of a region. If the above |
| 1103 | * flag is not present, we ignore errors; and |
| 1104 | * the sync state may be inaccurate. |
| 1105 | */ |
| 1106 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1107 | if (argc) { |
| 1108 | ti->error = "Too many mirror arguments"; |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1109 | r = -EINVAL; |
| 1110 | goto err_destroy_wq; |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1113 | r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client); |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1114 | if (r) |
| 1115 | goto err_destroy_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1117 | wakeup_mirrord(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | return 0; |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1119 | |
| 1120 | err_destroy_wq: |
| 1121 | destroy_workqueue(ms->kmirrord_wq); |
| 1122 | err_free_context: |
| 1123 | free_context(ms, ti, ms->nr_mirrors); |
| 1124 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static void mirror_dtr(struct dm_target *ti) |
| 1128 | { |
| 1129 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1130 | |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 1131 | del_timer_sync(&ms->timer); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1132 | flush_workqueue(ms->kmirrord_wq); |
Mikulas Patocka | 18776c7 | 2008-11-13 23:38:52 +0000 | [diff] [blame] | 1133 | flush_scheduled_work(); |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1134 | dm_kcopyd_client_destroy(ms->kcopyd_client); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1135 | destroy_workqueue(ms->kmirrord_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | free_context(ms, ti, ms->nr_mirrors); |
| 1137 | } |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | /* |
| 1140 | * Mirror mapping function |
| 1141 | */ |
| 1142 | static int mirror_map(struct dm_target *ti, struct bio *bio, |
| 1143 | union map_info *map_context) |
| 1144 | { |
| 1145 | int r, rw = bio_rw(bio); |
| 1146 | struct mirror *m; |
| 1147 | struct mirror_set *ms = ti->private; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1148 | struct dm_raid1_read_record *read_record = NULL; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1149 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | if (rw == WRITE) { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1152 | /* Save region for mirror_end_io() handler */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1153 | map_context->ll = dm_rh_bio_to_region(ms->rh, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | queue_bio(ms, bio, rw); |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1155 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1158 | r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | if (r < 0 && r != -EWOULDBLOCK) |
| 1160 | return r; |
| 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | /* |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1163 | * If region is not in-sync queue the bio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1165 | if (!r || (r == -EWOULDBLOCK)) { |
| 1166 | if (rw == READA) |
| 1167 | return -EWOULDBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | queue_bio(ms, bio, rw); |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1170 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1173 | /* |
| 1174 | * The region is in-sync and we can perform reads directly. |
| 1175 | * Store enough information so we can retry if it fails. |
| 1176 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | m = choose_mirror(ms, bio->bi_sector); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1178 | if (unlikely(!m)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return -EIO; |
| 1180 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1181 | read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO); |
| 1182 | if (likely(read_record)) { |
| 1183 | dm_bio_record(&read_record->details, bio); |
| 1184 | map_context->ptr = read_record; |
| 1185 | read_record->m = m; |
| 1186 | } |
| 1187 | |
| 1188 | map_bio(m, bio); |
| 1189 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1190 | return DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static int mirror_end_io(struct dm_target *ti, struct bio *bio, |
| 1194 | int error, union map_info *map_context) |
| 1195 | { |
| 1196 | int rw = bio_rw(bio); |
| 1197 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1198 | struct mirror *m = NULL; |
| 1199 | struct dm_bio_details *bd = NULL; |
| 1200 | struct dm_raid1_read_record *read_record = map_context->ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * We need to dec pending if this was a write. |
| 1204 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1205 | if (rw == WRITE) { |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 1206 | if (likely(!bio_empty_barrier(bio))) |
| 1207 | dm_rh_dec(ms->rh, map_context->ll); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1208 | return error; |
| 1209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1211 | if (error == -EOPNOTSUPP) |
| 1212 | goto out; |
| 1213 | |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 1214 | if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD)) |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1215 | goto out; |
| 1216 | |
| 1217 | if (unlikely(error)) { |
| 1218 | if (!read_record) { |
| 1219 | /* |
| 1220 | * There wasn't enough memory to record necessary |
| 1221 | * information for a retry or there was no other |
| 1222 | * mirror in-sync. |
| 1223 | */ |
Adrian Bunk | e03f1a8 | 2008-02-19 19:44:19 +0000 | [diff] [blame] | 1224 | DMERR_LIMIT("Mirror read failed."); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1225 | return -EIO; |
| 1226 | } |
Adrian Bunk | e03f1a8 | 2008-02-19 19:44:19 +0000 | [diff] [blame] | 1227 | |
| 1228 | m = read_record->m; |
| 1229 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1230 | DMERR("Mirror read failed from %s. Trying alternative device.", |
| 1231 | m->dev->name); |
| 1232 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1233 | fail_mirror(m, DM_RAID1_READ_ERROR); |
| 1234 | |
| 1235 | /* |
| 1236 | * A failed read is requeued for another attempt using an intact |
| 1237 | * mirror. |
| 1238 | */ |
| 1239 | if (default_ok(m) || mirror_available(ms, bio)) { |
| 1240 | bd = &read_record->details; |
| 1241 | |
| 1242 | dm_bio_restore(bd, bio); |
| 1243 | mempool_free(read_record, ms->read_record_pool); |
| 1244 | map_context->ptr = NULL; |
| 1245 | queue_bio(ms, bio, rw); |
| 1246 | return 1; |
| 1247 | } |
| 1248 | DMERR("All replicated volumes dead, failing I/O"); |
| 1249 | } |
| 1250 | |
| 1251 | out: |
| 1252 | if (read_record) { |
| 1253 | mempool_free(read_record, ms->read_record_pool); |
| 1254 | map_context->ptr = NULL; |
| 1255 | } |
| 1256 | |
| 1257 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1260 | static void mirror_presuspend(struct dm_target *ti) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
| 1262 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1263 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 1265 | struct bio_list holds; |
| 1266 | struct bio *bio; |
| 1267 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1268 | atomic_set(&ms->suspend, 1); |
| 1269 | |
| 1270 | /* |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 1271 | * Process bios in the hold list to start recovery waiting |
| 1272 | * for bios in the hold list. After the process, no bio has |
| 1273 | * a chance to be added in the hold list because ms->suspend |
| 1274 | * is set. |
| 1275 | */ |
| 1276 | spin_lock_irq(&ms->lock); |
| 1277 | holds = ms->holds; |
| 1278 | bio_list_init(&ms->holds); |
| 1279 | spin_unlock_irq(&ms->lock); |
| 1280 | |
| 1281 | while ((bio = bio_list_pop(&holds))) |
| 1282 | hold_bio(ms, bio); |
| 1283 | |
| 1284 | /* |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1285 | * We must finish up all the work that we've |
| 1286 | * generated (i.e. recovery work). |
| 1287 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1288 | dm_rh_stop_recovery(ms->rh); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 1289 | |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 1290 | wait_event(_kmirrord_recovery_stopped, |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1291 | !dm_rh_recovery_in_flight(ms->rh)); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 1292 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1293 | if (log->type->presuspend && log->type->presuspend(log)) |
| 1294 | /* FIXME: need better error handling */ |
| 1295 | DMWARN("log presuspend failed"); |
| 1296 | |
| 1297 | /* |
| 1298 | * Now that recovery is complete/stopped and the |
| 1299 | * delayed bios are queued, we need to wait for |
| 1300 | * the worker thread to complete. This way, |
| 1301 | * we know that all of our I/O has been pushed. |
| 1302 | */ |
| 1303 | flush_workqueue(ms->kmirrord_wq); |
| 1304 | } |
| 1305 | |
| 1306 | static void mirror_postsuspend(struct dm_target *ti) |
| 1307 | { |
| 1308 | struct mirror_set *ms = ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1309 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1310 | |
Jonathan Brassow | 6b3df0d | 2007-10-19 22:47:57 +0100 | [diff] [blame] | 1311 | if (log->type->postsuspend && log->type->postsuspend(log)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | /* FIXME: need better error handling */ |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1313 | DMWARN("log postsuspend failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static void mirror_resume(struct dm_target *ti) |
| 1317 | { |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1318 | struct mirror_set *ms = ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1319 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1320 | |
| 1321 | atomic_set(&ms->suspend, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | if (log->type->resume && log->type->resume(log)) |
| 1323 | /* FIXME: need better error handling */ |
| 1324 | DMWARN("log resume failed"); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1325 | dm_rh_start_recovery(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1328 | /* |
| 1329 | * device_status_char |
| 1330 | * @m: mirror device/leg we want the status of |
| 1331 | * |
| 1332 | * We return one character representing the most severe error |
| 1333 | * we have encountered. |
| 1334 | * A => Alive - No failures |
| 1335 | * D => Dead - A write failure occurred leaving mirror out-of-sync |
| 1336 | * S => Sync - A sychronization failure occurred, mirror out-of-sync |
| 1337 | * R => Read - A read failure occurred, mirror data unaffected |
| 1338 | * |
| 1339 | * Returns: <char> |
| 1340 | */ |
| 1341 | static char device_status_char(struct mirror *m) |
| 1342 | { |
| 1343 | if (!atomic_read(&(m->error_count))) |
| 1344 | return 'A'; |
| 1345 | |
Mikulas Patocka | 64b30c4 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 1346 | return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' : |
| 1347 | (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' : |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1348 | (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' : |
| 1349 | (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U'; |
| 1350 | } |
| 1351 | |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | static int mirror_status(struct dm_target *ti, status_type_t type, |
| 1354 | char *result, unsigned int maxlen) |
| 1355 | { |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1356 | unsigned int m, sz = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1358 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1359 | char buffer[ms->nr_mirrors + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | switch (type) { |
| 1362 | case STATUSTYPE_INFO: |
| 1363 | DMEMIT("%d ", ms->nr_mirrors); |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1364 | for (m = 0; m < ms->nr_mirrors; m++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | DMEMIT("%s ", ms->mirror[m].dev->name); |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1366 | buffer[m] = device_status_char(&(ms->mirror[m])); |
| 1367 | } |
| 1368 | buffer[m] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1370 | DMEMIT("%llu/%llu 1 %s ", |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1371 | (unsigned long long)log->type->get_sync_count(log), |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1372 | (unsigned long long)ms->nr_regions, buffer); |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1373 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1374 | sz += log->type->status(log, type, result+sz, maxlen-sz); |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | break; |
| 1377 | |
| 1378 | case STATUSTYPE_TABLE: |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1379 | sz = log->type->status(log, type, result, maxlen); |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1380 | |
Jonathan Brassow | e52b8f6 | 2006-10-03 01:15:32 -0700 | [diff] [blame] | 1381 | DMEMIT("%d", ms->nr_mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | for (m = 0; m < ms->nr_mirrors; m++) |
Jonathan Brassow | e52b8f6 | 2006-10-03 01:15:32 -0700 | [diff] [blame] | 1383 | DMEMIT(" %s %llu", ms->mirror[m].dev->name, |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1384 | (unsigned long long)ms->mirror[m].offset); |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1385 | |
| 1386 | if (ms->features & DM_RAID1_HANDLE_ERRORS) |
| 1387 | DMEMIT(" 1 handle_errors"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1393 | static int mirror_iterate_devices(struct dm_target *ti, |
| 1394 | iterate_devices_callout_fn fn, void *data) |
| 1395 | { |
| 1396 | struct mirror_set *ms = ti->private; |
| 1397 | int ret = 0; |
| 1398 | unsigned i; |
| 1399 | |
| 1400 | for (i = 0; !ret && i < ms->nr_mirrors; i++) |
| 1401 | ret = fn(ti, ms->mirror[i].dev, |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 1402 | ms->mirror[i].offset, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1403 | |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | static struct target_type mirror_target = { |
| 1408 | .name = "mirror", |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1409 | .version = {1, 12, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | .module = THIS_MODULE, |
| 1411 | .ctr = mirror_ctr, |
| 1412 | .dtr = mirror_dtr, |
| 1413 | .map = mirror_map, |
| 1414 | .end_io = mirror_end_io, |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1415 | .presuspend = mirror_presuspend, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | .postsuspend = mirror_postsuspend, |
| 1417 | .resume = mirror_resume, |
| 1418 | .status = mirror_status, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1419 | .iterate_devices = mirror_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | }; |
| 1421 | |
| 1422 | static int __init dm_mirror_init(void) |
| 1423 | { |
| 1424 | int r; |
| 1425 | |
Mikulas Patocka | 95f8fac | 2009-04-02 19:55:24 +0100 | [diff] [blame] | 1426 | _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0); |
| 1427 | if (!_dm_raid1_read_record_cache) { |
| 1428 | DMERR("Can't allocate dm_raid1_read_record cache"); |
| 1429 | r = -ENOMEM; |
| 1430 | goto bad_cache; |
| 1431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Mikulas Patocka | 95f8fac | 2009-04-02 19:55:24 +0100 | [diff] [blame] | 1433 | r = dm_register_target(&mirror_target); |
| 1434 | if (r < 0) { |
| 1435 | DMERR("Failed to register mirror target"); |
| 1436 | goto bad_target; |
| 1437 | } |
| 1438 | |
| 1439 | return 0; |
| 1440 | |
| 1441 | bad_target: |
| 1442 | kmem_cache_destroy(_dm_raid1_read_record_cache); |
| 1443 | bad_cache: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | return r; |
| 1445 | } |
| 1446 | |
| 1447 | static void __exit dm_mirror_exit(void) |
| 1448 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 1449 | dm_unregister_target(&mirror_target); |
Mikulas Patocka | 95f8fac | 2009-04-02 19:55:24 +0100 | [diff] [blame] | 1450 | kmem_cache_destroy(_dm_raid1_read_record_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* Module hooks */ |
| 1454 | module_init(dm_mirror_init); |
| 1455 | module_exit(dm_mirror_exit); |
| 1456 | |
| 1457 | MODULE_DESCRIPTION(DM_NAME " mirror target"); |
| 1458 | MODULE_AUTHOR("Joe Thornber"); |
| 1459 | MODULE_LICENSE("GPL"); |