blob: 089d62751f7ff2a3aedf7e441cb88bec0d06b8a7 [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
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000085DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
86 "A percentage of time allocated for raid resynchronization");
87
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010088static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010090 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070092 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
93}
94
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010095static void delayed_wake_fn(unsigned long data)
96{
97 struct mirror_set *ms = (struct mirror_set *) data;
98
99 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100100 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100101}
102
103static void delayed_wake(struct mirror_set *ms)
104{
105 if (test_and_set_bit(0, &ms->timer_pending))
106 return;
107
108 ms->timer.expires = jiffies + HZ / 5;
109 ms->timer.data = (unsigned long) ms;
110 ms->timer.function = delayed_wake_fn;
111 add_timer(&ms->timer);
112}
113
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100114static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100116 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100119static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100123 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100125 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
126 spin_lock_irqsave(&ms->lock, flags);
127 should_wake = !(bl->head);
128 bio_list_add(bl, bio);
129 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100132 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100135static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100137 struct mirror_set *ms = context;
138 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100140 while ((bio = bio_list_pop(bio_list)))
141 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Mikulas Patocka89c7cd82012-12-21 20:23:39 +0000144struct dm_raid1_bio_record {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000145 struct mirror *m;
Mikulas Patocka0045d612012-12-21 20:23:40 +0000146 /* if details->bi_bdev == NULL, details were not saved */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000147 struct dm_bio_details details;
Mikulas Patocka0045d612012-12-21 20:23:40 +0000148 region_t write_region;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000149};
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * Every mirror should look like this one.
153 */
154#define DEFAULT_MIRROR 0
155
156/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000157 * This is yucky. We squirrel the mirror struct away inside
158 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * doesn't get submitted to the lower levels of block layer.
160 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000161static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000163 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000166static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000168 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000171static struct mirror *get_default_mirror(struct mirror_set *ms)
172{
173 return &ms->mirror[atomic_read(&ms->default_mirror)];
174}
175
176static void set_default_mirror(struct mirror *m)
177{
178 struct mirror_set *ms = m->ms;
179 struct mirror *m0 = &(ms->mirror[0]);
180
181 atomic_set(&ms->default_mirror, m - m0);
182}
183
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000184static struct mirror *get_valid_mirror(struct mirror_set *ms)
185{
186 struct mirror *m;
187
188 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
189 if (!atomic_read(&m->error_count))
190 return m;
191
192 return NULL;
193}
194
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000195/* fail_mirror
196 * @m: mirror device to fail
197 * @error_type: one of the enum's, DM_RAID1_*_ERROR
198 *
199 * If errors are being handled, record the type of
200 * error encountered for this device. If this type
201 * of error has already been recorded, we can return;
202 * otherwise, we must signal userspace by triggering
203 * an event. Additionally, if the device is the
204 * primary device, we must choose a new primary, but
205 * only if the mirror is in-sync.
206 *
207 * This function must not block.
208 */
209static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
210{
211 struct mirror_set *ms = m->ms;
212 struct mirror *new;
213
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000214 ms->leg_failure = 1;
215
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000216 /*
217 * error_count is used for nothing more than a
218 * simple way to tell if a device has encountered
219 * errors.
220 */
221 atomic_inc(&m->error_count);
222
223 if (test_and_set_bit(error_type, &m->error_type))
224 return;
225
Jonathan Brassowd460c652009-01-06 03:04:57 +0000226 if (!errors_handled(ms))
227 return;
228
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000229 if (m != get_default_mirror(ms))
230 goto out;
231
232 if (!ms->in_sync) {
233 /*
234 * Better to issue requests to same failing device
235 * than to risk returning corrupt data.
236 */
237 DMERR("Primary mirror (%s) failed while out-of-sync: "
238 "Reads may fail.", m->dev->name);
239 goto out;
240 }
241
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000242 new = get_valid_mirror(ms);
243 if (new)
244 set_default_mirror(new);
245 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000246 DMWARN("All sides of mirror have failed.");
247
248out:
249 schedule_work(&ms->trigger_event);
250}
251
Mikulas Patockac0da3742009-12-10 23:52:02 +0000252static int mirror_flush(struct dm_target *ti)
253{
254 struct mirror_set *ms = ti->private;
255 unsigned long error_bits;
256
257 unsigned int i;
258 struct dm_io_region io[ms->nr_mirrors];
259 struct mirror *m;
260 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200261 .bi_rw = WRITE_FLUSH,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000262 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000263 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000264 .client = ms->io_client,
265 };
266
267 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
268 io[i].bdev = m->dev->bdev;
269 io[i].sector = 0;
270 io[i].count = 0;
271 }
272
273 error_bits = -1;
274 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
275 if (unlikely(error_bits != 0)) {
276 for (i = 0; i < ms->nr_mirrors; i++)
277 if (test_bit(i, &error_bits))
278 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000279 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000280 return -EIO;
281 }
282
283 return 0;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*-----------------------------------------------------------------
287 * Recovery.
288 *
289 * When a mirror is first activated we may find that some regions
290 * are in the no-sync state. We have to recover these by
291 * recopying from the default mirror to all the others.
292 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700293static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 void *context)
295{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100296 struct dm_region *reg = context;
297 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000298 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000300 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100301 /* Read error means the failure of default mirror. */
302 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000303 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
304 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100305
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000306 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700307 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100308 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000309 /*
310 * Bits correspond to devices (excluding default mirror).
311 * The default mirror cannot change during recovery.
312 */
313 for (m = 0; m < ms->nr_mirrors; m++) {
314 if (&ms->mirror[m] == get_default_mirror(ms))
315 continue;
316 if (test_bit(bit, &write_err))
317 fail_mirror(ms->mirror + m,
318 DM_RAID1_SYNC_ERROR);
319 bit++;
320 }
321 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100322
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100323 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100326static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100329 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100330 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct mirror *m;
332 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100333 region_t key = dm_rh_get_region_key(reg);
334 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000337 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100339 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
340 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /*
342 * The final region may be smaller than
343 * region_size.
344 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100345 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100347 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100349 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* fill in the destinations */
352 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000353 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 continue;
355
356 m = ms->mirror + i;
357 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100358 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dest->count = from.count;
360 dest++;
361 }
362
363 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100364 if (!errors_handled(ms))
365 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
366
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100367 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
368 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 return r;
371}
372
373static void do_recovery(struct mirror_set *ms)
374{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100375 struct dm_region *reg;
376 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /*
380 * Start quiescing some regions.
381 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100382 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /*
385 * Copy any already quiesced regions.
386 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100387 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 r = recover(ms, reg);
389 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100390 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 /*
394 * Update the in sync flag.
395 */
396 if (!ms->in_sync &&
397 (log->type->get_sync_count(log) == ms->nr_regions)) {
398 /* the sync is complete */
399 dm_table_event(ms->ti->table);
400 ms->in_sync = 1;
401 }
402}
403
404/*-----------------------------------------------------------------
405 * Reads
406 *---------------------------------------------------------------*/
407static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
408{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000409 struct mirror *m = get_default_mirror(ms);
410
411 do {
412 if (likely(!atomic_read(&m->error_count)))
413 return m;
414
415 if (m-- == ms->mirror)
416 m += ms->nr_mirrors;
417 } while (m != get_default_mirror(ms));
418
419 return NULL;
420}
421
422static int default_ok(struct mirror *m)
423{
424 struct mirror *default_mirror = get_default_mirror(m->ms);
425
426 return !atomic_read(&default_mirror->error_count);
427}
428
429static int mirror_available(struct mirror_set *ms, struct bio *bio)
430{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100431 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
432 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000433
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100434 if (log->type->in_sync(log, region, 0))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700435 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000436
437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/*
441 * remap a buffer to a particular mirror.
442 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000443static sector_t map_sector(struct mirror *m, struct bio *bio)
444{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700445 if (unlikely(!bio->bi_iter.bi_size))
Mikulas Patocka41841532009-12-10 23:51:59 +0000446 return 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700447 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000448}
449
450static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 bio->bi_bdev = m->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700453 bio->bi_iter.bi_sector = map_sector(m, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000454}
455
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100456static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000457 struct bio *bio)
458{
459 io->bdev = m->dev->bdev;
460 io->sector = map_sector(m, bio);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800461 io->count = bio_sectors(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000462}
463
Mikulas Patocka04788502009-12-10 23:52:03 +0000464static void hold_bio(struct mirror_set *ms, struct bio *bio)
465{
466 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000467 * Lock is required to avoid race condition during suspend
468 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000469 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000470 spin_lock_irq(&ms->lock);
471
Mikulas Patocka04788502009-12-10 23:52:03 +0000472 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000473 spin_unlock_irq(&ms->lock);
474
475 /*
476 * If device is suspended, complete the bio.
477 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000478 if (dm_noflush_suspending(ms->ti))
479 bio_endio(bio, DM_ENDIO_REQUEUE);
480 else
481 bio_endio(bio, -EIO);
482 return;
483 }
484
485 /*
486 * Hold bio until the suspend is complete.
487 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000488 bio_list_add(&ms->holds, bio);
489 spin_unlock_irq(&ms->lock);
490}
491
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000492/*-----------------------------------------------------------------
493 * Reads
494 *---------------------------------------------------------------*/
495static void read_callback(unsigned long error, void *context)
496{
497 struct bio *bio = context;
498 struct mirror *m;
499
500 m = bio_get_m(bio);
501 bio_set_m(bio, NULL);
502
503 if (likely(!error)) {
504 bio_endio(bio, 0);
505 return;
506 }
507
508 fail_mirror(m, DM_RAID1_READ_ERROR);
509
510 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
511 DMWARN_LIMIT("Read failure on mirror device %s. "
512 "Trying alternative device.",
513 m->dev->name);
514 queue_bio(m->ms, bio, bio_rw(bio));
515 return;
516 }
517
518 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
519 m->dev->name);
520 bio_endio(bio, -EIO);
521}
522
523/* Asynchronous read. */
524static void read_async_bio(struct mirror *m, struct bio *bio)
525{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100526 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000527 struct dm_io_request io_req = {
528 .bi_rw = READ,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700529 .mem.type = DM_IO_BIO,
530 .mem.ptr.bio = bio,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000531 .notify.fn = read_callback,
532 .notify.context = bio,
533 .client = m->ms->io_client,
534 };
535
536 map_region(&io, m, bio);
537 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100538 BUG_ON(dm_io(&io_req, 1, &io, NULL));
539}
540
541static inline int region_in_sync(struct mirror_set *ms, region_t region,
542 int may_block)
543{
544 int state = dm_rh_get_state(ms->rh, region, may_block);
545 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548static void do_reads(struct mirror_set *ms, struct bio_list *reads)
549{
550 region_t region;
551 struct bio *bio;
552 struct mirror *m;
553
554 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100555 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000556 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /*
559 * We can only read balance if the region is in sync.
560 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100561 if (likely(region_in_sync(ms, region, 1)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700562 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000563 else if (m && atomic_read(&m->error_count))
564 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000566 if (likely(m))
567 read_async_bio(m, bio);
568 else
569 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571}
572
573/*-----------------------------------------------------------------
574 * Writes.
575 *
576 * We do different things with the write io depending on the
577 * state of the region that it's in:
578 *
579 * SYNC: increment pending, use kcopyd to write to *all* mirrors
580 * RECOVERING: delay the io until recovery completes
581 * NOSYNC: increment pending, just write to the default mirror
582 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000583
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static void write_callback(unsigned long error, void *context)
586{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000587 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct bio *bio = (struct bio *) context;
589 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000590 int should_wake = 0;
591 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000593 ms = bio_get_m(bio)->ms;
594 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /*
597 * NOTE: We don't decrement the pending count here,
598 * instead it is done by the targets endio function.
599 * This way we handle both writes to SYNC and NOSYNC
600 * regions with the same code.
601 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000602 if (likely(!error)) {
603 bio_endio(bio, ret);
604 return;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Mikulas Patockaf2ed51a2015-02-12 10:09:20 -0500607 /*
608 * If the bio is discard, return an error, but do not
609 * degrade the array.
610 */
611 if (bio->bi_rw & REQ_DISCARD) {
612 bio_endio(bio, -EOPNOTSUPP);
613 return;
614 }
615
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000616 for (i = 0; i < ms->nr_mirrors; i++)
617 if (test_bit(i, &error))
618 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000619
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000620 /*
621 * Need to raise event. Since raising
622 * events can block, we need to do it in
623 * the main thread.
624 */
625 spin_lock_irqsave(&ms->lock, flags);
626 if (!ms->failures.head)
627 should_wake = 1;
628 bio_list_add(&ms->failures, bio);
629 spin_unlock_irqrestore(&ms->lock, flags);
630 if (should_wake)
631 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634static void do_write(struct mirror_set *ms, struct bio *bio)
635{
636 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100637 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700639 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200640 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
Kent Overstreet003b5c52013-10-11 15:45:43 -0700641 .mem.type = DM_IO_BIO,
642 .mem.ptr.bio = bio,
Milan Broz88be1632007-05-09 02:33:04 -0700643 .notify.fn = write_callback,
644 .notify.context = bio,
645 .client = ms->io_client,
646 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000648 if (bio->bi_rw & REQ_DISCARD) {
649 io_req.bi_rw |= REQ_DISCARD;
650 io_req.mem.type = DM_IO_KMEM;
651 io_req.mem.ptr.addr = NULL;
652 }
653
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000654 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
655 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000657 /*
658 * Use default mirror because we only need it to retrieve the reference
659 * to the mirror set in write_callback().
660 */
661 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700662
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100663 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666static void do_writes(struct mirror_set *ms, struct bio_list *writes)
667{
668 int state;
669 struct bio *bio;
670 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100671 struct bio_list requeue;
672 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
673 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (!writes->head)
676 return;
677
678 /*
679 * Classify each write.
680 */
681 bio_list_init(&sync);
682 bio_list_init(&nosync);
683 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100684 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 while ((bio = bio_list_pop(writes))) {
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000687 if ((bio->bi_rw & REQ_FLUSH) ||
688 (bio->bi_rw & REQ_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000689 bio_list_add(&sync, bio);
690 continue;
691 }
692
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100693 region = dm_rh_bio_to_region(ms->rh, bio);
694
695 if (log->type->is_remote_recovering &&
696 log->type->is_remote_recovering(log, region)) {
697 bio_list_add(&requeue, bio);
698 continue;
699 }
700
701 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100703 case DM_RH_CLEAN:
704 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 this_list = &sync;
706 break;
707
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100708 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 this_list = &nosync;
710 break;
711
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100712 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 this_list = &recover;
714 break;
715 }
716
717 bio_list_add(this_list, bio);
718 }
719
720 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100721 * Add bios that are delayed due to remote recovery
722 * back on to the write queue
723 */
724 if (unlikely(requeue.head)) {
725 spin_lock_irq(&ms->lock);
726 bio_list_merge(&ms->writes, &requeue);
727 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100728 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100729 }
730
731 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * Increment the pending counts for any regions that will
733 * be written to (writes to recover regions are going to
734 * be delayed).
735 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100736 dm_rh_inc_pending(ms->rh, &sync);
737 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100738
739 /*
740 * If the flush fails on a previous call and succeeds here,
741 * we must not reset the log_failure variable. We need
742 * userspace interaction to do that.
743 */
744 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /*
747 * Dispatch io.
748 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000749 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000750 spin_lock_irq(&ms->lock);
751 bio_list_merge(&ms->failures, &sync);
752 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100753 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000754 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100755 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000756 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100759 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 while ((bio = bio_list_pop(&nosync))) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000762 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
763 spin_lock_irq(&ms->lock);
764 bio_list_add(&ms->failures, bio);
765 spin_unlock_irq(&ms->lock);
766 wakeup_mirrord(ms);
767 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000768 map_bio(get_default_mirror(ms), bio);
769 generic_make_request(bio);
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772}
773
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000774static void do_failures(struct mirror_set *ms, struct bio_list *failures)
775{
776 struct bio *bio;
777
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000778 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000779 return;
780
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000781 /*
782 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000783 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000784 * until a log has been marked, so we must store them.
785 *
786 * If a 'noflush' suspend is in progress, we can requeue
787 * the I/O's to the core. This give userspace a chance
788 * to reconfigure the mirror, at which point the core
789 * will reissue the writes. If the 'noflush' flag is
790 * not set, we have no choice but to return errors.
791 *
792 * Some writes on the failures list may have been
793 * submitted before the log failure and represent a
794 * failure to write to one of the devices. It is ok
795 * for us to treat them the same and requeue them
796 * as well.
797 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000798 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000799 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000800 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000801 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000802 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000803
804 /*
805 * If all the legs are dead, fail the I/O.
806 * If we have been told to handle errors, hold the bio
807 * and wait for userspace to deal with the problem.
808 * Otherwise pretend that the I/O succeeded. (This would
809 * be wrong if the failed leg returned after reboot and
810 * got replicated back to the good legs.)
811 */
812 if (!get_valid_mirror(ms))
813 bio_endio(bio, -EIO);
814 else if (errors_handled(ms))
815 hold_bio(ms, bio);
816 else
817 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000818 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000819}
820
821static void trigger_event(struct work_struct *work)
822{
823 struct mirror_set *ms =
824 container_of(work, struct mirror_set, trigger_event);
825
826 dm_table_event(ms->ti->table);
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/*-----------------------------------------------------------------
830 * kmirrord
831 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100832static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100834 struct mirror_set *ms = container_of(work, struct mirror_set,
835 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000836 struct bio_list reads, writes, failures;
837 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000839 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 reads = ms->reads;
841 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000842 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 bio_list_init(&ms->reads);
844 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000845 bio_list_init(&ms->failures);
846 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100848 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 do_recovery(ms);
850 do_reads(ms, &reads);
851 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000852 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/*-----------------------------------------------------------------
856 * Target functions
857 *---------------------------------------------------------------*/
858static struct mirror_set *alloc_context(unsigned int nr_mirrors,
859 uint32_t region_size,
860 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100861 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 size_t len;
864 struct mirror_set *ms = NULL;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
867
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700868 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700870 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return NULL;
872 }
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000875 bio_list_init(&ms->reads);
876 bio_list_init(&ms->writes);
877 bio_list_init(&ms->failures);
878 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 ms->ti = ti;
881 ms->nr_mirrors = nr_mirrors;
882 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
883 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000884 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000885 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000886 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000887 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100889 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700890 if (IS_ERR(ms->io_client)) {
891 ti->error = "Error creating dm_io client";
892 kfree(ms);
893 return NULL;
894 }
895
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100896 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
897 wakeup_all_recovery_waiters,
898 ms->ti->begin, MAX_RECOVERY,
899 dl, region_size, ms->nr_regions);
900 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700901 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100902 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 kfree(ms);
904 return NULL;
905 }
906
907 return ms;
908}
909
910static void free_context(struct mirror_set *ms, struct dm_target *ti,
911 unsigned int m)
912{
913 while (m--)
914 dm_put_device(ti, ms->mirror[m].dev);
915
Milan Broz88be1632007-05-09 02:33:04 -0700916 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100917 dm_region_hash_destroy(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 kfree(ms);
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
922 unsigned int mirror, char **argv)
923{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800924 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100925 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100927 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700928 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -EINVAL;
930 }
931
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000932 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700934 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -ENXIO;
936 }
937
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100938 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000939 atomic_set(&(ms->mirror[mirror].error_count), 0);
940 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ms->mirror[mirror].offset = offset;
942
943 return 0;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * Create dirty log: log_type #log_params <log_params>
948 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100949static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100950 unsigned argc, char **argv,
951 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100953 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100954 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100955 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (argc < 2) {
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 Patocka31998ef2012-03-28 18:41:26 +0100962 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700963 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return NULL;
965 }
966
967 *args_used = 2 + param_count;
968
969 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700970 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return NULL;
972 }
973
Mikulas Patockac0da3742009-12-10 23:52:02 +0000974 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
975 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700977 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return NULL;
979 }
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return dl;
982}
983
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700984static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
985 unsigned *args_used)
986{
987 unsigned num_features;
988 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100989 char dummy;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700990
991 *args_used = 0;
992
993 if (!argc)
994 return 0;
995
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100996 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700997 ti->error = "Invalid number of features";
998 return -EINVAL;
999 }
1000
1001 argc--;
1002 argv++;
1003 (*args_used)++;
1004
1005 if (num_features > argc) {
1006 ti->error = "Not enough arguments to support feature count";
1007 return -EINVAL;
1008 }
1009
1010 if (!strcmp("handle_errors", argv[0]))
1011 ms->features |= DM_RAID1_HANDLE_ERRORS;
1012 else {
1013 ti->error = "Unrecognised feature requested";
1014 return -EINVAL;
1015 }
1016
1017 (*args_used)++;
1018
1019 return 0;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * Construct a mirror mapping:
1024 *
1025 * log_type #log_params <log_params>
1026 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001027 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 *
1029 * log_type is "core" or "disk"
1030 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001031 *
1032 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1035{
1036 int r;
1037 unsigned int nr_mirrors, m, args_used;
1038 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001039 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001040 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 dl = create_dirty_log(ti, argc, argv, &args_used);
1043 if (!dl)
1044 return -EINVAL;
1045
1046 argv += args_used;
1047 argc -= args_used;
1048
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001049 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001050 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001051 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001052 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -EINVAL;
1054 }
1055
1056 argv++, argc--;
1057
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001058 if (argc < nr_mirrors * 2) {
1059 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001060 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -EINVAL;
1062 }
1063
1064 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1065 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001066 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -ENOMEM;
1068 }
1069
1070 /* Get the mirror parameter sets */
1071 for (m = 0; m < nr_mirrors; m++) {
1072 r = get_mirror(ms, ti, m, argv);
1073 if (r) {
1074 free_context(ms, ti, m);
1075 return r;
1076 }
1077 argv += 2;
1078 argc -= 2;
1079 }
1080
1081 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001082
1083 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1084 if (r)
1085 goto err_free_context;
1086
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001087 ti->num_flush_bios = 1;
1088 ti->num_discard_bios = 1;
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001089 ti->per_bio_data_size = sizeof(struct dm_raid1_bio_record);
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001090 ti->discard_zeroes_data_unsupported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Tejun Heo670368a2013-07-30 08:40:21 -04001092 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001093 if (!ms->kmirrord_wq) {
1094 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001095 r = -ENOMEM;
1096 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001097 }
1098 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001099 init_timer(&ms->timer);
1100 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001101 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001102
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001103 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001104 if (r)
1105 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001106
1107 argv += args_used;
1108 argc -= args_used;
1109
Jonathan Brassowf44db672007-07-12 17:29:04 +01001110 /*
1111 * Any read-balancing addition depends on the
1112 * DM_RAID1_HANDLE_ERRORS flag being present.
1113 * This is because the decision to balance depends
1114 * on the sync state of a region. If the above
1115 * flag is not present, we ignore errors; and
1116 * the sync state may be inaccurate.
1117 */
1118
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001119 if (argc) {
1120 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001121 r = -EINVAL;
1122 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001123 }
1124
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001125 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001126 if (IS_ERR(ms->kcopyd_client)) {
1127 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001128 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001131 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001133
1134err_destroy_wq:
1135 destroy_workqueue(ms->kmirrord_wq);
1136err_free_context:
1137 free_context(ms, ti, ms->nr_mirrors);
1138 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static void mirror_dtr(struct dm_target *ti)
1142{
1143 struct mirror_set *ms = (struct mirror_set *) ti->private;
1144
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001145 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001146 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001147 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001148 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001149 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 free_context(ms, ti, ms->nr_mirrors);
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
1154 * Mirror mapping function
1155 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001156static int mirror_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 int r, rw = bio_rw(bio);
1159 struct mirror *m;
1160 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001161 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001162 struct dm_raid1_bio_record *bio_record =
1163 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1164
1165 bio_record->details.bi_bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001168 /* Save region for mirror_end_io() handler */
Mikulas Patocka0045d612012-12-21 20:23:40 +00001169 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001171 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001174 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (r < 0 && r != -EWOULDBLOCK)
1176 return r;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001179 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001181 if (!r || (r == -EWOULDBLOCK)) {
1182 if (rw == READA)
1183 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001186 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001189 /*
1190 * The region is in-sync and we can perform reads directly.
1191 * Store enough information so we can retry if it fails.
1192 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001193 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001194 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return -EIO;
1196
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001197 dm_bio_record(&bio_record->details, bio);
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001198 bio_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001199
1200 map_bio(m, bio);
1201
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001202 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001205static int mirror_end_io(struct dm_target *ti, struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int rw = bio_rw(bio);
1208 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001209 struct mirror *m = NULL;
1210 struct dm_bio_details *bd = NULL;
Mikulas Patocka0045d612012-12-21 20:23:40 +00001211 struct dm_raid1_bio_record *bio_record =
1212 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /*
1215 * We need to dec pending if this was a write.
1216 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001217 if (rw == WRITE) {
Mikulas Patocka751f1882012-07-20 14:25:03 +01001218 if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
Mikulas Patocka0045d612012-12-21 20:23:40 +00001219 dm_rh_dec(ms->rh, bio_record->write_region);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001220 return error;
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001223 if (error == -EOPNOTSUPP)
1224 goto out;
1225
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001226 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001227 goto out;
1228
1229 if (unlikely(error)) {
Mikulas Patocka0045d612012-12-21 20:23:40 +00001230 if (!bio_record->details.bi_bdev) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001231 /*
1232 * There wasn't enough memory to record necessary
1233 * information for a retry or there was no other
1234 * mirror in-sync.
1235 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001236 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001237 return -EIO;
1238 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001239
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001240 m = bio_record->m;
Adrian Bunke03f1a82008-02-19 19:44:19 +00001241
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001242 DMERR("Mirror read failed from %s. Trying alternative device.",
1243 m->dev->name);
1244
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001245 fail_mirror(m, DM_RAID1_READ_ERROR);
1246
1247 /*
1248 * A failed read is requeued for another attempt using an intact
1249 * mirror.
1250 */
1251 if (default_ok(m) || mirror_available(ms, bio)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001252 bd = &bio_record->details;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001253
1254 dm_bio_restore(bd, bio);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001255 bio_record->details.bi_bdev = NULL;
Mikulas Patockaf3a44fe2014-02-18 09:57:22 -05001256
1257 atomic_inc(&bio->bi_remaining);
1258
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001259 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001260 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001261 }
1262 DMERR("All replicated volumes dead, failing I/O");
1263 }
1264
1265out:
Mikulas Patocka0045d612012-12-21 20:23:40 +00001266 bio_record->details.bi_bdev = NULL;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001267
1268 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001271static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001274 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Mikulas Patocka04788502009-12-10 23:52:03 +00001276 struct bio_list holds;
1277 struct bio *bio;
1278
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001279 atomic_set(&ms->suspend, 1);
1280
1281 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001282 * Process bios in the hold list to start recovery waiting
1283 * for bios in the hold list. After the process, no bio has
1284 * a chance to be added in the hold list because ms->suspend
1285 * is set.
1286 */
1287 spin_lock_irq(&ms->lock);
1288 holds = ms->holds;
1289 bio_list_init(&ms->holds);
1290 spin_unlock_irq(&ms->lock);
1291
1292 while ((bio = bio_list_pop(&holds)))
1293 hold_bio(ms, bio);
1294
1295 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001296 * We must finish up all the work that we've
1297 * generated (i.e. recovery work).
1298 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001299 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001300
Jonathan E Brassow33184042006-11-08 17:44:44 -08001301 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001302 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001303
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001304 if (log->type->presuspend && log->type->presuspend(log))
1305 /* FIXME: need better error handling */
1306 DMWARN("log presuspend failed");
1307
1308 /*
1309 * Now that recovery is complete/stopped and the
1310 * delayed bios are queued, we need to wait for
1311 * the worker thread to complete. This way,
1312 * we know that all of our I/O has been pushed.
1313 */
1314 flush_workqueue(ms->kmirrord_wq);
1315}
1316
1317static void mirror_postsuspend(struct dm_target *ti)
1318{
1319 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001320 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001321
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001322 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001324 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327static void mirror_resume(struct dm_target *ti)
1328{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001329 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001330 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001331
1332 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (log->type->resume && log->type->resume(log))
1334 /* FIXME: need better error handling */
1335 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001336 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001339/*
1340 * device_status_char
1341 * @m: mirror device/leg we want the status of
1342 *
1343 * We return one character representing the most severe error
1344 * we have encountered.
1345 * A => Alive - No failures
1346 * D => Dead - A write failure occurred leaving mirror out-of-sync
1347 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1348 * R => Read - A read failure occurred, mirror data unaffected
1349 *
1350 * Returns: <char>
1351 */
1352static char device_status_char(struct mirror *m)
1353{
1354 if (!atomic_read(&(m->error_count)))
1355 return 'A';
1356
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001357 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1358 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001359 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1360 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1361}
1362
1363
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001364static void mirror_status(struct dm_target *ti, status_type_t type,
1365 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001367 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001369 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001370 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 switch (type) {
1373 case STATUSTYPE_INFO:
1374 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001375 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001377 buffer[m] = device_status_char(&(ms->mirror[m]));
1378 }
1379 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001381 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001382 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001383 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001384
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001385 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
1388
1389 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001390 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001391
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001392 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001394 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001395 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001396
1397 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1398 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001402static int mirror_iterate_devices(struct dm_target *ti,
1403 iterate_devices_callout_fn fn, void *data)
1404{
1405 struct mirror_set *ms = ti->private;
1406 int ret = 0;
1407 unsigned i;
1408
1409 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1410 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001411 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001412
1413 return ret;
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static struct target_type mirror_target = {
1417 .name = "mirror",
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001418 .version = {1, 13, 2},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 .module = THIS_MODULE,
1420 .ctr = mirror_ctr,
1421 .dtr = mirror_dtr,
1422 .map = mirror_map,
1423 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001424 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 .postsuspend = mirror_postsuspend,
1426 .resume = mirror_resume,
1427 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001428 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429};
1430
1431static int __init dm_mirror_init(void)
1432{
1433 int r;
1434
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001435 r = dm_register_target(&mirror_target);
1436 if (r < 0) {
1437 DMERR("Failed to register mirror target");
1438 goto bad_target;
1439 }
1440
1441 return 0;
1442
1443bad_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return r;
1445}
1446
1447static void __exit dm_mirror_exit(void)
1448{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001449 dm_unregister_target(&mirror_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
1452/* Module hooks */
1453module_init(dm_mirror_init);
1454module_exit(dm_mirror_exit);
1455
1456MODULE_DESCRIPTION(DM_NAME " mirror target");
1457MODULE_AUTHOR("Joe Thornber");
1458MODULE_LICENSE("GPL");