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