blob: 7f2419099b50a8b8260f98ef7bc17ac22b1ab9e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01003 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Jonathan Brassow06386bb2008-02-08 02:11:37 +00008#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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 Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/workqueue.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010016#include <linux/device-mapper.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010017#include <linux/dm-io.h>
18#include <linux/dm-dirty-log.h>
19#include <linux/dm-kcopyd.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010020#include <linux/dm-region-hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022#define DM_MSG_PREFIX "raid1"
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010023
24#define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070026#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010027#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070028
Jonathan E Brassow33184042006-11-08 17:44:44 -080029static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070032 * Mirror set structures.
33 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000034enum dm_raid1_error {
35 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000036 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000037 DM_RAID1_SYNC_ERROR,
38 DM_RAID1_READ_ERROR
39};
40
Neil Browne4c8b3b2006-06-26 00:27:26 -070041struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010042 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070043 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000044 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070045 struct dm_dev *dev;
46 sector_t offset;
47};
48
49struct mirror_set {
50 struct dm_target *ti;
51 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010052
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070053 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070054
Jonathan Brassow72f4b312008-02-08 02:11:29 +000055 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070056 struct bio_list reads;
57 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000058 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000059 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070060
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010061 struct dm_region_hash *rh;
62 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070063 struct dm_io_client *io_client;
64
Neil Browne4c8b3b2006-06-26 00:27:26 -070065 /* recovery */
66 region_t nr_regions;
67 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010068 int log_failure;
Mikulas Patocka929be8f2009-12-10 23:52:06 +000069 int leg_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000070 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070071
Jonathan Brassow72f4b312008-02-08 02:11:29 +000072 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070073
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070074 struct workqueue_struct *kmirrord_wq;
75 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010076 struct timer_list timer;
77 unsigned long timer_pending;
78
Jonathan Brassow72f4b312008-02-08 02:11:29 +000079 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070080
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010081 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070082 struct mirror mirror[0];
83};
84
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010085static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010087 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070089 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
90}
91
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010092static 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 Mauelshagen1f965b12008-10-21 17:45:06 +010097 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010098}
99
100static 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 Mauelshagen1f965b12008-10-21 17:45:06 +0100111static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100113 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100116static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100120 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100122 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 Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100129 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100132static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100134 struct mirror_set *ms = context;
135 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100137 while ((bio = bio_list_pop(bio_list)))
138 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Mikulas Patocka89c7cd82012-12-21 20:23:39 +0000141struct dm_raid1_bio_record {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000142 struct mirror *m;
Mikulas Patocka0045d612012-12-21 20:23:40 +0000143 /* if details->bi_bdev == NULL, details were not saved */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000144 struct dm_bio_details details;
Mikulas Patocka0045d612012-12-21 20:23:40 +0000145 region_t write_region;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000146};
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * Every mirror should look like this one.
150 */
151#define DEFAULT_MIRROR 0
152
153/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000154 * This is yucky. We squirrel the mirror struct away inside
155 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * doesn't get submitted to the lower levels of block layer.
157 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000158static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000160 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000163static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000165 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000168static struct mirror *get_default_mirror(struct mirror_set *ms)
169{
170 return &ms->mirror[atomic_read(&ms->default_mirror)];
171}
172
173static void set_default_mirror(struct mirror *m)
174{
175 struct mirror_set *ms = m->ms;
176 struct mirror *m0 = &(ms->mirror[0]);
177
178 atomic_set(&ms->default_mirror, m - m0);
179}
180
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000181static struct mirror *get_valid_mirror(struct mirror_set *ms)
182{
183 struct mirror *m;
184
185 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
186 if (!atomic_read(&m->error_count))
187 return m;
188
189 return NULL;
190}
191
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000192/* fail_mirror
193 * @m: mirror device to fail
194 * @error_type: one of the enum's, DM_RAID1_*_ERROR
195 *
196 * If errors are being handled, record the type of
197 * error encountered for this device. If this type
198 * of error has already been recorded, we can return;
199 * otherwise, we must signal userspace by triggering
200 * an event. Additionally, if the device is the
201 * primary device, we must choose a new primary, but
202 * only if the mirror is in-sync.
203 *
204 * This function must not block.
205 */
206static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
207{
208 struct mirror_set *ms = m->ms;
209 struct mirror *new;
210
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000211 ms->leg_failure = 1;
212
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000213 /*
214 * error_count is used for nothing more than a
215 * simple way to tell if a device has encountered
216 * errors.
217 */
218 atomic_inc(&m->error_count);
219
220 if (test_and_set_bit(error_type, &m->error_type))
221 return;
222
Jonathan Brassowd460c652009-01-06 03:04:57 +0000223 if (!errors_handled(ms))
224 return;
225
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000226 if (m != get_default_mirror(ms))
227 goto out;
228
229 if (!ms->in_sync) {
230 /*
231 * Better to issue requests to same failing device
232 * than to risk returning corrupt data.
233 */
234 DMERR("Primary mirror (%s) failed while out-of-sync: "
235 "Reads may fail.", m->dev->name);
236 goto out;
237 }
238
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000239 new = get_valid_mirror(ms);
240 if (new)
241 set_default_mirror(new);
242 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000243 DMWARN("All sides of mirror have failed.");
244
245out:
246 schedule_work(&ms->trigger_event);
247}
248
Mikulas Patockac0da3742009-12-10 23:52:02 +0000249static int mirror_flush(struct dm_target *ti)
250{
251 struct mirror_set *ms = ti->private;
252 unsigned long error_bits;
253
254 unsigned int i;
255 struct dm_io_region io[ms->nr_mirrors];
256 struct mirror *m;
257 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200258 .bi_rw = WRITE_FLUSH,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000259 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000260 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000261 .client = ms->io_client,
262 };
263
264 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
265 io[i].bdev = m->dev->bdev;
266 io[i].sector = 0;
267 io[i].count = 0;
268 }
269
270 error_bits = -1;
271 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
272 if (unlikely(error_bits != 0)) {
273 for (i = 0; i < ms->nr_mirrors; i++)
274 if (test_bit(i, &error_bits))
275 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000276 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000277 return -EIO;
278 }
279
280 return 0;
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*-----------------------------------------------------------------
284 * Recovery.
285 *
286 * When a mirror is first activated we may find that some regions
287 * are in the no-sync state. We have to recover these by
288 * recopying from the default mirror to all the others.
289 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700290static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 void *context)
292{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100293 struct dm_region *reg = context;
294 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000295 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000297 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100298 /* Read error means the failure of default mirror. */
299 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000300 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
301 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100302
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000303 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700304 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100305 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000306 /*
307 * Bits correspond to devices (excluding default mirror).
308 * The default mirror cannot change during recovery.
309 */
310 for (m = 0; m < ms->nr_mirrors; m++) {
311 if (&ms->mirror[m] == get_default_mirror(ms))
312 continue;
313 if (test_bit(bit, &write_err))
314 fail_mirror(ms->mirror + m,
315 DM_RAID1_SYNC_ERROR);
316 bit++;
317 }
318 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100319
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100320 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100323static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100326 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100327 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct mirror *m;
329 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100330 region_t key = dm_rh_get_region_key(reg);
331 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000334 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100336 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
337 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /*
339 * The final region may be smaller than
340 * region_size.
341 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100342 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100344 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100346 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* fill in the destinations */
349 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000350 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 continue;
352
353 m = ms->mirror + i;
354 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100355 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dest->count = from.count;
357 dest++;
358 }
359
360 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100361 if (!errors_handled(ms))
362 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
363
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100364 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
365 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 return r;
368}
369
370static void do_recovery(struct mirror_set *ms)
371{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100372 struct dm_region *reg;
373 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /*
377 * Start quiescing some regions.
378 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100379 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
382 * Copy any already quiesced regions.
383 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100384 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 r = recover(ms, reg);
386 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100387 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 /*
391 * Update the in sync flag.
392 */
393 if (!ms->in_sync &&
394 (log->type->get_sync_count(log) == ms->nr_regions)) {
395 /* the sync is complete */
396 dm_table_event(ms->ti->table);
397 ms->in_sync = 1;
398 }
399}
400
401/*-----------------------------------------------------------------
402 * Reads
403 *---------------------------------------------------------------*/
404static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
405{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000406 struct mirror *m = get_default_mirror(ms);
407
408 do {
409 if (likely(!atomic_read(&m->error_count)))
410 return m;
411
412 if (m-- == ms->mirror)
413 m += ms->nr_mirrors;
414 } while (m != get_default_mirror(ms));
415
416 return NULL;
417}
418
419static int default_ok(struct mirror *m)
420{
421 struct mirror *default_mirror = get_default_mirror(m->ms);
422
423 return !atomic_read(&default_mirror->error_count);
424}
425
426static int mirror_available(struct mirror_set *ms, struct bio *bio)
427{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100428 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
429 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000430
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100431 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000432 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
433
434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/*
438 * remap a buffer to a particular mirror.
439 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000440static sector_t map_sector(struct mirror *m, struct bio *bio)
441{
Mikulas Patocka41841532009-12-10 23:51:59 +0000442 if (unlikely(!bio->bi_size))
443 return 0;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +0100444 return m->offset + dm_target_offset(m->ms->ti, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000445}
446
447static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000450 bio->bi_sector = map_sector(m, bio);
451}
452
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100453static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000454 struct bio *bio)
455{
456 io->bdev = m->dev->bdev;
457 io->sector = map_sector(m, bio);
458 io->count = bio->bi_size >> 9;
459}
460
Mikulas Patocka04788502009-12-10 23:52:03 +0000461static void hold_bio(struct mirror_set *ms, struct bio *bio)
462{
463 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000464 * Lock is required to avoid race condition during suspend
465 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000466 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000467 spin_lock_irq(&ms->lock);
468
Mikulas Patocka04788502009-12-10 23:52:03 +0000469 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000470 spin_unlock_irq(&ms->lock);
471
472 /*
473 * If device is suspended, complete the bio.
474 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000475 if (dm_noflush_suspending(ms->ti))
476 bio_endio(bio, DM_ENDIO_REQUEUE);
477 else
478 bio_endio(bio, -EIO);
479 return;
480 }
481
482 /*
483 * Hold bio until the suspend is complete.
484 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000485 bio_list_add(&ms->holds, bio);
486 spin_unlock_irq(&ms->lock);
487}
488
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000489/*-----------------------------------------------------------------
490 * Reads
491 *---------------------------------------------------------------*/
492static void read_callback(unsigned long error, void *context)
493{
494 struct bio *bio = context;
495 struct mirror *m;
496
497 m = bio_get_m(bio);
498 bio_set_m(bio, NULL);
499
500 if (likely(!error)) {
501 bio_endio(bio, 0);
502 return;
503 }
504
505 fail_mirror(m, DM_RAID1_READ_ERROR);
506
507 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
508 DMWARN_LIMIT("Read failure on mirror device %s. "
509 "Trying alternative device.",
510 m->dev->name);
511 queue_bio(m->ms, bio, bio_rw(bio));
512 return;
513 }
514
515 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
516 m->dev->name);
517 bio_endio(bio, -EIO);
518}
519
520/* Asynchronous read. */
521static void read_async_bio(struct mirror *m, struct bio *bio)
522{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100523 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000524 struct dm_io_request io_req = {
525 .bi_rw = READ,
526 .mem.type = DM_IO_BVEC,
527 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
528 .notify.fn = read_callback,
529 .notify.context = bio,
530 .client = m->ms->io_client,
531 };
532
533 map_region(&io, m, bio);
534 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100535 BUG_ON(dm_io(&io_req, 1, &io, NULL));
536}
537
538static inline int region_in_sync(struct mirror_set *ms, region_t region,
539 int may_block)
540{
541 int state = dm_rh_get_state(ms->rh, region, may_block);
542 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static void do_reads(struct mirror_set *ms, struct bio_list *reads)
546{
547 region_t region;
548 struct bio *bio;
549 struct mirror *m;
550
551 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100552 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000553 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /*
556 * We can only read balance if the region is in sync.
557 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100558 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000560 else if (m && atomic_read(&m->error_count))
561 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000563 if (likely(m))
564 read_async_bio(m, bio);
565 else
566 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568}
569
570/*-----------------------------------------------------------------
571 * Writes.
572 *
573 * We do different things with the write io depending on the
574 * state of the region that it's in:
575 *
576 * SYNC: increment pending, use kcopyd to write to *all* mirrors
577 * RECOVERING: delay the io until recovery completes
578 * NOSYNC: increment pending, just write to the default mirror
579 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000580
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static void write_callback(unsigned long error, void *context)
583{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000584 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct bio *bio = (struct bio *) context;
586 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000587 int should_wake = 0;
588 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000590 ms = bio_get_m(bio)->ms;
591 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /*
594 * NOTE: We don't decrement the pending count here,
595 * instead it is done by the targets endio function.
596 * This way we handle both writes to SYNC and NOSYNC
597 * regions with the same code.
598 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000599 if (likely(!error)) {
600 bio_endio(bio, ret);
601 return;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000604 for (i = 0; i < ms->nr_mirrors; i++)
605 if (test_bit(i, &error))
606 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000607
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000608 /*
609 * Need to raise event. Since raising
610 * events can block, we need to do it in
611 * the main thread.
612 */
613 spin_lock_irqsave(&ms->lock, flags);
614 if (!ms->failures.head)
615 should_wake = 1;
616 bio_list_add(&ms->failures, bio);
617 spin_unlock_irqrestore(&ms->lock, flags);
618 if (should_wake)
619 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static void do_write(struct mirror_set *ms, struct bio *bio)
623{
624 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100625 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700627 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200628 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
Milan Broz88be1632007-05-09 02:33:04 -0700629 .mem.type = DM_IO_BVEC,
630 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
631 .notify.fn = write_callback,
632 .notify.context = bio,
633 .client = ms->io_client,
634 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000636 if (bio->bi_rw & REQ_DISCARD) {
637 io_req.bi_rw |= REQ_DISCARD;
638 io_req.mem.type = DM_IO_KMEM;
639 io_req.mem.ptr.addr = NULL;
640 }
641
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000642 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
643 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000645 /*
646 * Use default mirror because we only need it to retrieve the reference
647 * to the mirror set in write_callback().
648 */
649 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700650
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100651 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654static void do_writes(struct mirror_set *ms, struct bio_list *writes)
655{
656 int state;
657 struct bio *bio;
658 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100659 struct bio_list requeue;
660 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
661 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (!writes->head)
664 return;
665
666 /*
667 * Classify each write.
668 */
669 bio_list_init(&sync);
670 bio_list_init(&nosync);
671 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100672 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 while ((bio = bio_list_pop(writes))) {
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000675 if ((bio->bi_rw & REQ_FLUSH) ||
676 (bio->bi_rw & REQ_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000677 bio_list_add(&sync, bio);
678 continue;
679 }
680
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100681 region = dm_rh_bio_to_region(ms->rh, bio);
682
683 if (log->type->is_remote_recovering &&
684 log->type->is_remote_recovering(log, region)) {
685 bio_list_add(&requeue, bio);
686 continue;
687 }
688
689 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100691 case DM_RH_CLEAN:
692 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 this_list = &sync;
694 break;
695
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100696 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 this_list = &nosync;
698 break;
699
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100700 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 this_list = &recover;
702 break;
703 }
704
705 bio_list_add(this_list, bio);
706 }
707
708 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100709 * Add bios that are delayed due to remote recovery
710 * back on to the write queue
711 */
712 if (unlikely(requeue.head)) {
713 spin_lock_irq(&ms->lock);
714 bio_list_merge(&ms->writes, &requeue);
715 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100716 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100717 }
718
719 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * Increment the pending counts for any regions that will
721 * be written to (writes to recover regions are going to
722 * be delayed).
723 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100724 dm_rh_inc_pending(ms->rh, &sync);
725 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100726
727 /*
728 * If the flush fails on a previous call and succeeds here,
729 * we must not reset the log_failure variable. We need
730 * userspace interaction to do that.
731 */
732 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /*
735 * Dispatch io.
736 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000737 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000738 spin_lock_irq(&ms->lock);
739 bio_list_merge(&ms->failures, &sync);
740 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100741 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000742 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100743 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000744 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100747 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 while ((bio = bio_list_pop(&nosync))) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000750 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
751 spin_lock_irq(&ms->lock);
752 bio_list_add(&ms->failures, bio);
753 spin_unlock_irq(&ms->lock);
754 wakeup_mirrord(ms);
755 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000756 map_bio(get_default_mirror(ms), bio);
757 generic_make_request(bio);
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760}
761
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000762static void do_failures(struct mirror_set *ms, struct bio_list *failures)
763{
764 struct bio *bio;
765
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000766 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000767 return;
768
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000769 /*
770 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000771 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000772 * until a log has been marked, so we must store them.
773 *
774 * If a 'noflush' suspend is in progress, we can requeue
775 * the I/O's to the core. This give userspace a chance
776 * to reconfigure the mirror, at which point the core
777 * will reissue the writes. If the 'noflush' flag is
778 * not set, we have no choice but to return errors.
779 *
780 * Some writes on the failures list may have been
781 * submitted before the log failure and represent a
782 * failure to write to one of the devices. It is ok
783 * for us to treat them the same and requeue them
784 * as well.
785 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000786 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000787 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000788 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000789 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000790 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000791
792 /*
793 * If all the legs are dead, fail the I/O.
794 * If we have been told to handle errors, hold the bio
795 * and wait for userspace to deal with the problem.
796 * Otherwise pretend that the I/O succeeded. (This would
797 * be wrong if the failed leg returned after reboot and
798 * got replicated back to the good legs.)
799 */
800 if (!get_valid_mirror(ms))
801 bio_endio(bio, -EIO);
802 else if (errors_handled(ms))
803 hold_bio(ms, bio);
804 else
805 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000806 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000807}
808
809static void trigger_event(struct work_struct *work)
810{
811 struct mirror_set *ms =
812 container_of(work, struct mirror_set, trigger_event);
813
814 dm_table_event(ms->ti->table);
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*-----------------------------------------------------------------
818 * kmirrord
819 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100820static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100822 struct mirror_set *ms = container_of(work, struct mirror_set,
823 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000824 struct bio_list reads, writes, failures;
825 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000827 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 reads = ms->reads;
829 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000830 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 bio_list_init(&ms->reads);
832 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000833 bio_list_init(&ms->failures);
834 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100836 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 do_recovery(ms);
838 do_reads(ms, &reads);
839 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000840 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/*-----------------------------------------------------------------
844 * Target functions
845 *---------------------------------------------------------------*/
846static struct mirror_set *alloc_context(unsigned int nr_mirrors,
847 uint32_t region_size,
848 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100849 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 size_t len;
852 struct mirror_set *ms = NULL;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
855
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700856 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700858 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return NULL;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000863 bio_list_init(&ms->reads);
864 bio_list_init(&ms->writes);
865 bio_list_init(&ms->failures);
866 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 ms->ti = ti;
869 ms->nr_mirrors = nr_mirrors;
870 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
871 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000872 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000873 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000874 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000875 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100877 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700878 if (IS_ERR(ms->io_client)) {
879 ti->error = "Error creating dm_io client";
880 kfree(ms);
881 return NULL;
882 }
883
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100884 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
885 wakeup_all_recovery_waiters,
886 ms->ti->begin, MAX_RECOVERY,
887 dl, region_size, ms->nr_regions);
888 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700889 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100890 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 kfree(ms);
892 return NULL;
893 }
894
895 return ms;
896}
897
898static void free_context(struct mirror_set *ms, struct dm_target *ti,
899 unsigned int m)
900{
901 while (m--)
902 dm_put_device(ti, ms->mirror[m].dev);
903
Milan Broz88be1632007-05-09 02:33:04 -0700904 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100905 dm_region_hash_destroy(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 kfree(ms);
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
910 unsigned int mirror, char **argv)
911{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800912 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100913 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100915 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700916 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -EINVAL;
918 }
919
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000920 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700922 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENXIO;
924 }
925
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100926 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000927 atomic_set(&(ms->mirror[mirror].error_count), 0);
928 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ms->mirror[mirror].offset = offset;
930
931 return 0;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/*
935 * Create dirty log: log_type #log_params <log_params>
936 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100937static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100938 unsigned argc, char **argv,
939 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100941 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100942 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100943 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700946 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return NULL;
948 }
949
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100950 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700951 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return NULL;
953 }
954
955 *args_used = 2 + param_count;
956
957 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700958 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return NULL;
960 }
961
Mikulas Patockac0da3742009-12-10 23:52:02 +0000962 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
963 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700965 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return NULL;
967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return dl;
970}
971
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700972static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
973 unsigned *args_used)
974{
975 unsigned num_features;
976 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100977 char dummy;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700978
979 *args_used = 0;
980
981 if (!argc)
982 return 0;
983
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100984 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700985 ti->error = "Invalid number of features";
986 return -EINVAL;
987 }
988
989 argc--;
990 argv++;
991 (*args_used)++;
992
993 if (num_features > argc) {
994 ti->error = "Not enough arguments to support feature count";
995 return -EINVAL;
996 }
997
998 if (!strcmp("handle_errors", argv[0]))
999 ms->features |= DM_RAID1_HANDLE_ERRORS;
1000 else {
1001 ti->error = "Unrecognised feature requested";
1002 return -EINVAL;
1003 }
1004
1005 (*args_used)++;
1006
1007 return 0;
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
1011 * Construct a mirror mapping:
1012 *
1013 * log_type #log_params <log_params>
1014 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001015 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
1017 * log_type is "core" or "disk"
1018 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001019 *
1020 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1023{
1024 int r;
1025 unsigned int nr_mirrors, m, args_used;
1026 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001027 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001028 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 dl = create_dirty_log(ti, argc, argv, &args_used);
1031 if (!dl)
1032 return -EINVAL;
1033
1034 argv += args_used;
1035 argc -= args_used;
1036
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001037 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001038 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001039 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001040 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EINVAL;
1042 }
1043
1044 argv++, argc--;
1045
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001046 if (argc < nr_mirrors * 2) {
1047 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001048 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EINVAL;
1050 }
1051
1052 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1053 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001054 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -ENOMEM;
1056 }
1057
1058 /* Get the mirror parameter sets */
1059 for (m = 0; m < nr_mirrors; m++) {
1060 r = get_mirror(ms, ti, m, argv);
1061 if (r) {
1062 free_context(ms, ti, m);
1063 return r;
1064 }
1065 argv += 2;
1066 argc -= 2;
1067 }
1068
1069 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001070
1071 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1072 if (r)
1073 goto err_free_context;
1074
Mikulas Patocka41841532009-12-10 23:51:59 +00001075 ti->num_flush_requests = 1;
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +00001076 ti->num_discard_requests = 1;
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001077 ti->per_bio_data_size = sizeof(struct dm_raid1_bio_record);
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001078 ti->discard_zeroes_data_unsupported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Tejun Heo9c4376d2011-01-13 19:59:58 +00001080 ms->kmirrord_wq = alloc_workqueue("kmirrord",
1081 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001082 if (!ms->kmirrord_wq) {
1083 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001084 r = -ENOMEM;
1085 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001086 }
1087 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001088 init_timer(&ms->timer);
1089 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001090 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001091
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001092 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001093 if (r)
1094 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001095
1096 argv += args_used;
1097 argc -= args_used;
1098
Jonathan Brassowf44db672007-07-12 17:29:04 +01001099 /*
1100 * Any read-balancing addition depends on the
1101 * DM_RAID1_HANDLE_ERRORS flag being present.
1102 * This is because the decision to balance depends
1103 * on the sync state of a region. If the above
1104 * flag is not present, we ignore errors; and
1105 * the sync state may be inaccurate.
1106 */
1107
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001108 if (argc) {
1109 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001110 r = -EINVAL;
1111 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001112 }
1113
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001114 ms->kcopyd_client = dm_kcopyd_client_create();
1115 if (IS_ERR(ms->kcopyd_client)) {
1116 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001117 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001120 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001122
1123err_destroy_wq:
1124 destroy_workqueue(ms->kmirrord_wq);
1125err_free_context:
1126 free_context(ms, ti, ms->nr_mirrors);
1127 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static void mirror_dtr(struct dm_target *ti)
1131{
1132 struct mirror_set *ms = (struct mirror_set *) ti->private;
1133
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001134 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001135 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001136 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001137 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001138 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 free_context(ms, ti, ms->nr_mirrors);
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/*
1143 * Mirror mapping function
1144 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001145static int mirror_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int r, rw = bio_rw(bio);
1148 struct mirror *m;
1149 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001150 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001151 struct dm_raid1_bio_record *bio_record =
1152 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1153
1154 bio_record->details.bi_bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001157 /* Save region for mirror_end_io() handler */
Mikulas Patocka0045d612012-12-21 20:23:40 +00001158 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001160 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001163 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (r < 0 && r != -EWOULDBLOCK)
1165 return r;
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001168 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001170 if (!r || (r == -EWOULDBLOCK)) {
1171 if (rw == READA)
1172 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001175 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001178 /*
1179 * The region is in-sync and we can perform reads directly.
1180 * Store enough information so we can retry if it fails.
1181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001183 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -EIO;
1185
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001186 dm_bio_record(&bio_record->details, bio);
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001187 bio_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001188
1189 map_bio(m, bio);
1190
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001191 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001194static int mirror_end_io(struct dm_target *ti, struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int rw = bio_rw(bio);
1197 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001198 struct mirror *m = NULL;
1199 struct dm_bio_details *bd = NULL;
Mikulas Patocka0045d612012-12-21 20:23:40 +00001200 struct dm_raid1_bio_record *bio_record =
1201 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /*
1204 * We need to dec pending if this was a write.
1205 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001206 if (rw == WRITE) {
Mikulas Patocka751f1882012-07-20 14:25:03 +01001207 if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
Mikulas Patocka0045d612012-12-21 20:23:40 +00001208 dm_rh_dec(ms->rh, bio_record->write_region);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001209 return error;
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001212 if (error == -EOPNOTSUPP)
1213 goto out;
1214
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001215 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001216 goto out;
1217
1218 if (unlikely(error)) {
Mikulas Patocka0045d612012-12-21 20:23:40 +00001219 if (!bio_record->details.bi_bdev) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001220 /*
1221 * There wasn't enough memory to record necessary
1222 * information for a retry or there was no other
1223 * mirror in-sync.
1224 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001225 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001226 return -EIO;
1227 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001228
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001229 m = bio_record->m;
Adrian Bunke03f1a82008-02-19 19:44:19 +00001230
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001231 DMERR("Mirror read failed from %s. Trying alternative device.",
1232 m->dev->name);
1233
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001234 fail_mirror(m, DM_RAID1_READ_ERROR);
1235
1236 /*
1237 * A failed read is requeued for another attempt using an intact
1238 * mirror.
1239 */
1240 if (default_ok(m) || mirror_available(ms, bio)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001241 bd = &bio_record->details;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001242
1243 dm_bio_restore(bd, bio);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001244 bio_record->details.bi_bdev = NULL;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001245 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001246 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001247 }
1248 DMERR("All replicated volumes dead, failing I/O");
1249 }
1250
1251out:
Mikulas Patocka0045d612012-12-21 20:23:40 +00001252 bio_record->details.bi_bdev = NULL;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001253
1254 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001257static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001260 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Mikulas Patocka04788502009-12-10 23:52:03 +00001262 struct bio_list holds;
1263 struct bio *bio;
1264
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001265 atomic_set(&ms->suspend, 1);
1266
1267 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001268 * Process bios in the hold list to start recovery waiting
1269 * for bios in the hold list. After the process, no bio has
1270 * a chance to be added in the hold list because ms->suspend
1271 * is set.
1272 */
1273 spin_lock_irq(&ms->lock);
1274 holds = ms->holds;
1275 bio_list_init(&ms->holds);
1276 spin_unlock_irq(&ms->lock);
1277
1278 while ((bio = bio_list_pop(&holds)))
1279 hold_bio(ms, bio);
1280
1281 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001282 * We must finish up all the work that we've
1283 * generated (i.e. recovery work).
1284 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001285 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001286
Jonathan E Brassow33184042006-11-08 17:44:44 -08001287 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001288 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001289
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001290 if (log->type->presuspend && log->type->presuspend(log))
1291 /* FIXME: need better error handling */
1292 DMWARN("log presuspend failed");
1293
1294 /*
1295 * Now that recovery is complete/stopped and the
1296 * delayed bios are queued, we need to wait for
1297 * the worker thread to complete. This way,
1298 * we know that all of our I/O has been pushed.
1299 */
1300 flush_workqueue(ms->kmirrord_wq);
1301}
1302
1303static void mirror_postsuspend(struct dm_target *ti)
1304{
1305 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001306 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001307
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001308 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001310 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313static void mirror_resume(struct dm_target *ti)
1314{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001315 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001316 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001317
1318 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (log->type->resume && log->type->resume(log))
1320 /* FIXME: need better error handling */
1321 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001322 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001325/*
1326 * device_status_char
1327 * @m: mirror device/leg we want the status of
1328 *
1329 * We return one character representing the most severe error
1330 * we have encountered.
1331 * A => Alive - No failures
1332 * D => Dead - A write failure occurred leaving mirror out-of-sync
1333 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1334 * R => Read - A read failure occurred, mirror data unaffected
1335 *
1336 * Returns: <char>
1337 */
1338static char device_status_char(struct mirror *m)
1339{
1340 if (!atomic_read(&(m->error_count)))
1341 return 'A';
1342
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001343 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1344 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001345 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1346 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1347}
1348
1349
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001350static void mirror_status(struct dm_target *ti, status_type_t type,
1351 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001353 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001355 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001356 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 switch (type) {
1359 case STATUSTYPE_INFO:
1360 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001361 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001363 buffer[m] = device_status_char(&(ms->mirror[m]));
1364 }
1365 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001367 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001368 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001369 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001370
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001371 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 break;
1374
1375 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001376 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001377
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001378 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001380 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001381 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001382
1383 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1384 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001388static int mirror_iterate_devices(struct dm_target *ti,
1389 iterate_devices_callout_fn fn, void *data)
1390{
1391 struct mirror_set *ms = ti->private;
1392 int ret = 0;
1393 unsigned i;
1394
1395 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1396 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001397 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001398
1399 return ret;
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static struct target_type mirror_target = {
1403 .name = "mirror",
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001404 .version = {1, 13, 2},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 .module = THIS_MODULE,
1406 .ctr = mirror_ctr,
1407 .dtr = mirror_dtr,
1408 .map = mirror_map,
1409 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001410 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .postsuspend = mirror_postsuspend,
1412 .resume = mirror_resume,
1413 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001414 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415};
1416
1417static int __init dm_mirror_init(void)
1418{
1419 int r;
1420
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001421 r = dm_register_target(&mirror_target);
1422 if (r < 0) {
1423 DMERR("Failed to register mirror target");
1424 goto bad_target;
1425 }
1426
1427 return 0;
1428
1429bad_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return r;
1431}
1432
1433static void __exit dm_mirror_exit(void)
1434{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001435 dm_unregister_target(&mirror_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438/* Module hooks */
1439module_init(dm_mirror_init);
1440module_exit(dm_mirror_exit);
1441
1442MODULE_DESCRIPTION(DM_NAME " mirror target");
1443MODULE_AUTHOR("Joe Thornber");
1444MODULE_LICENSE("GPL");