blob: 933a1fd99b7780d3ce6e2305eeb108dc6ac6cdba [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
Lidong Zhonged632872015-05-13 14:04:10 +080026#define DM_RAID1_HANDLE_ERRORS 0x01
27#define DM_RAID1_KEEP_LOG 0x02
Jonathan Brassowf44db672007-07-12 17:29:04 +010028#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Lidong Zhonged632872015-05-13 14:04:10 +080029#define keep_log(p) ((p)->features & DM_RAID1_KEEP_LOG)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070030
Jonathan E Brassow33184042006-11-08 17:44:44 -080031static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070034 * Mirror set structures.
35 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000036enum dm_raid1_error {
37 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000038 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000039 DM_RAID1_SYNC_ERROR,
40 DM_RAID1_READ_ERROR
41};
42
Neil Browne4c8b3b2006-06-26 00:27:26 -070043struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010044 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070045 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000046 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070047 struct dm_dev *dev;
48 sector_t offset;
49};
50
51struct mirror_set {
52 struct dm_target *ti;
53 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010054
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070055 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070056
Jonathan Brassow72f4b312008-02-08 02:11:29 +000057 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070058 struct bio_list reads;
59 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000060 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000061 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070062
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010063 struct dm_region_hash *rh;
64 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070065 struct dm_io_client *io_client;
66
Neil Browne4c8b3b2006-06-26 00:27:26 -070067 /* recovery */
68 region_t nr_regions;
69 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010070 int log_failure;
Mikulas Patocka929be8f2009-12-10 23:52:06 +000071 int leg_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000072 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070073
Jonathan Brassow72f4b312008-02-08 02:11:29 +000074 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070075
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070076 struct workqueue_struct *kmirrord_wq;
77 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010078 struct timer_list timer;
79 unsigned long timer_pending;
80
Jonathan Brassow72f4b312008-02-08 02:11:29 +000081 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070082
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010083 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070084 struct mirror mirror[0];
85};
86
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000087DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
88 "A percentage of time allocated for raid resynchronization");
89
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010090static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010092 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070094 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
95}
96
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010097static void delayed_wake_fn(unsigned long data)
98{
99 struct mirror_set *ms = (struct mirror_set *) data;
100
101 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100102 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100103}
104
105static void delayed_wake(struct mirror_set *ms)
106{
107 if (test_and_set_bit(0, &ms->timer_pending))
108 return;
109
110 ms->timer.expires = jiffies + HZ / 5;
111 ms->timer.data = (unsigned long) ms;
112 ms->timer.function = delayed_wake_fn;
113 add_timer(&ms->timer);
114}
115
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100116static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100118 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100121static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100125 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100127 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
128 spin_lock_irqsave(&ms->lock, flags);
129 should_wake = !(bl->head);
130 bio_list_add(bl, bio);
131 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100134 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100137static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100139 struct mirror_set *ms = context;
140 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100142 while ((bio = bio_list_pop(bio_list)))
143 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Mikulas Patocka89c7cd82012-12-21 20:23:39 +0000146struct dm_raid1_bio_record {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000147 struct mirror *m;
Mikulas Patocka0045d612012-12-21 20:23:40 +0000148 /* if details->bi_bdev == NULL, details were not saved */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000149 struct dm_bio_details details;
Mikulas Patocka0045d612012-12-21 20:23:40 +0000150 region_t write_region;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Every mirror should look like this one.
155 */
156#define DEFAULT_MIRROR 0
157
158/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000159 * This is yucky. We squirrel the mirror struct away inside
160 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * doesn't get submitted to the lower levels of block layer.
162 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000163static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000165 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000168static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000170 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000173static struct mirror *get_default_mirror(struct mirror_set *ms)
174{
175 return &ms->mirror[atomic_read(&ms->default_mirror)];
176}
177
178static void set_default_mirror(struct mirror *m)
179{
180 struct mirror_set *ms = m->ms;
181 struct mirror *m0 = &(ms->mirror[0]);
182
183 atomic_set(&ms->default_mirror, m - m0);
184}
185
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000186static struct mirror *get_valid_mirror(struct mirror_set *ms)
187{
188 struct mirror *m;
189
190 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
191 if (!atomic_read(&m->error_count))
192 return m;
193
194 return NULL;
195}
196
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000197/* fail_mirror
198 * @m: mirror device to fail
199 * @error_type: one of the enum's, DM_RAID1_*_ERROR
200 *
201 * If errors are being handled, record the type of
202 * error encountered for this device. If this type
203 * of error has already been recorded, we can return;
204 * otherwise, we must signal userspace by triggering
205 * an event. Additionally, if the device is the
206 * primary device, we must choose a new primary, but
207 * only if the mirror is in-sync.
208 *
209 * This function must not block.
210 */
211static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
212{
213 struct mirror_set *ms = m->ms;
214 struct mirror *new;
215
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000216 ms->leg_failure = 1;
217
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000218 /*
219 * error_count is used for nothing more than a
220 * simple way to tell if a device has encountered
221 * errors.
222 */
223 atomic_inc(&m->error_count);
224
225 if (test_and_set_bit(error_type, &m->error_type))
226 return;
227
Jonathan Brassowd460c652009-01-06 03:04:57 +0000228 if (!errors_handled(ms))
229 return;
230
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000231 if (m != get_default_mirror(ms))
232 goto out;
233
Lidong Zhonged632872015-05-13 14:04:10 +0800234 if (!ms->in_sync && !keep_log(ms)) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000235 /*
236 * Better to issue requests to same failing device
237 * than to risk returning corrupt data.
238 */
239 DMERR("Primary mirror (%s) failed while out-of-sync: "
240 "Reads may fail.", m->dev->name);
241 goto out;
242 }
243
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000244 new = get_valid_mirror(ms);
245 if (new)
246 set_default_mirror(new);
247 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000248 DMWARN("All sides of mirror have failed.");
249
250out:
251 schedule_work(&ms->trigger_event);
252}
253
Mikulas Patockac0da3742009-12-10 23:52:02 +0000254static int mirror_flush(struct dm_target *ti)
255{
256 struct mirror_set *ms = ti->private;
257 unsigned long error_bits;
258
259 unsigned int i;
260 struct dm_io_region io[ms->nr_mirrors];
261 struct mirror *m;
262 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200263 .bi_rw = WRITE_FLUSH,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000264 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000265 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000266 .client = ms->io_client,
267 };
268
269 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
270 io[i].bdev = m->dev->bdev;
271 io[i].sector = 0;
272 io[i].count = 0;
273 }
274
275 error_bits = -1;
276 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
277 if (unlikely(error_bits != 0)) {
278 for (i = 0; i < ms->nr_mirrors; i++)
279 if (test_bit(i, &error_bits))
280 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000281 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000282 return -EIO;
283 }
284
285 return 0;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*-----------------------------------------------------------------
289 * Recovery.
290 *
291 * When a mirror is first activated we may find that some regions
292 * are in the no-sync state. We have to recover these by
293 * recopying from the default mirror to all the others.
294 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700295static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 void *context)
297{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100298 struct dm_region *reg = context;
299 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000300 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000302 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100303 /* Read error means the failure of default mirror. */
304 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000305 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
306 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100307
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000308 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700309 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100310 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000311 /*
312 * Bits correspond to devices (excluding default mirror).
313 * The default mirror cannot change during recovery.
314 */
315 for (m = 0; m < ms->nr_mirrors; m++) {
316 if (&ms->mirror[m] == get_default_mirror(ms))
317 continue;
318 if (test_bit(bit, &write_err))
319 fail_mirror(ms->mirror + m,
320 DM_RAID1_SYNC_ERROR);
321 bit++;
322 }
323 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100324
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100325 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100328static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100331 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100332 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct mirror *m;
334 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100335 region_t key = dm_rh_get_region_key(reg);
336 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000339 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100341 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
342 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
344 * The final region may be smaller than
345 * region_size.
346 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100347 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100349 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100351 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* fill in the destinations */
354 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000355 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 continue;
357
358 m = ms->mirror + i;
359 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100360 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dest->count = from.count;
362 dest++;
363 }
364
365 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100366 if (!errors_handled(ms))
367 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
368
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100369 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
370 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return r;
373}
374
Lidong Zhonged632872015-05-13 14:04:10 +0800375static void reset_ms_flags(struct mirror_set *ms)
376{
377 unsigned int m;
378
379 ms->leg_failure = 0;
380 for (m = 0; m < ms->nr_mirrors; m++) {
381 atomic_set(&(ms->mirror[m].error_count), 0);
382 ms->mirror[m].error_type = 0;
383 }
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static void do_recovery(struct mirror_set *ms)
387{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100388 struct dm_region *reg;
389 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /*
393 * Start quiescing some regions.
394 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100395 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * Copy any already quiesced regions.
399 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100400 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 r = recover(ms, reg);
402 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100403 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
406 /*
407 * Update the in sync flag.
408 */
409 if (!ms->in_sync &&
410 (log->type->get_sync_count(log) == ms->nr_regions)) {
411 /* the sync is complete */
412 dm_table_event(ms->ti->table);
413 ms->in_sync = 1;
Lidong Zhonged632872015-05-13 14:04:10 +0800414 reset_ms_flags(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416}
417
418/*-----------------------------------------------------------------
419 * Reads
420 *---------------------------------------------------------------*/
421static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
422{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000423 struct mirror *m = get_default_mirror(ms);
424
425 do {
426 if (likely(!atomic_read(&m->error_count)))
427 return m;
428
429 if (m-- == ms->mirror)
430 m += ms->nr_mirrors;
431 } while (m != get_default_mirror(ms));
432
433 return NULL;
434}
435
436static int default_ok(struct mirror *m)
437{
438 struct mirror *default_mirror = get_default_mirror(m->ms);
439
440 return !atomic_read(&default_mirror->error_count);
441}
442
443static int mirror_available(struct mirror_set *ms, struct bio *bio)
444{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100445 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
446 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000447
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100448 if (log->type->in_sync(log, region, 0))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700449 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000450
451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454/*
455 * remap a buffer to a particular mirror.
456 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000457static sector_t map_sector(struct mirror *m, struct bio *bio)
458{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700459 if (unlikely(!bio->bi_iter.bi_size))
Mikulas Patocka41841532009-12-10 23:51:59 +0000460 return 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700461 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000462}
463
464static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 bio->bi_bdev = m->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700467 bio->bi_iter.bi_sector = map_sector(m, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000468}
469
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100470static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000471 struct bio *bio)
472{
473 io->bdev = m->dev->bdev;
474 io->sector = map_sector(m, bio);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800475 io->count = bio_sectors(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000476}
477
Mikulas Patocka04788502009-12-10 23:52:03 +0000478static void hold_bio(struct mirror_set *ms, struct bio *bio)
479{
480 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000481 * Lock is required to avoid race condition during suspend
482 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000483 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000484 spin_lock_irq(&ms->lock);
485
Mikulas Patocka04788502009-12-10 23:52:03 +0000486 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000487 spin_unlock_irq(&ms->lock);
488
489 /*
490 * If device is suspended, complete the bio.
491 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000492 if (dm_noflush_suspending(ms->ti))
493 bio_endio(bio, DM_ENDIO_REQUEUE);
494 else
495 bio_endio(bio, -EIO);
496 return;
497 }
498
499 /*
500 * Hold bio until the suspend is complete.
501 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000502 bio_list_add(&ms->holds, bio);
503 spin_unlock_irq(&ms->lock);
504}
505
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000506/*-----------------------------------------------------------------
507 * Reads
508 *---------------------------------------------------------------*/
509static void read_callback(unsigned long error, void *context)
510{
511 struct bio *bio = context;
512 struct mirror *m;
513
514 m = bio_get_m(bio);
515 bio_set_m(bio, NULL);
516
517 if (likely(!error)) {
518 bio_endio(bio, 0);
519 return;
520 }
521
522 fail_mirror(m, DM_RAID1_READ_ERROR);
523
524 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
525 DMWARN_LIMIT("Read failure on mirror device %s. "
526 "Trying alternative device.",
527 m->dev->name);
528 queue_bio(m->ms, bio, bio_rw(bio));
529 return;
530 }
531
532 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
533 m->dev->name);
534 bio_endio(bio, -EIO);
535}
536
537/* Asynchronous read. */
538static void read_async_bio(struct mirror *m, struct bio *bio)
539{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100540 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000541 struct dm_io_request io_req = {
542 .bi_rw = READ,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700543 .mem.type = DM_IO_BIO,
544 .mem.ptr.bio = bio,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000545 .notify.fn = read_callback,
546 .notify.context = bio,
547 .client = m->ms->io_client,
548 };
549
550 map_region(&io, m, bio);
551 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100552 BUG_ON(dm_io(&io_req, 1, &io, NULL));
553}
554
555static inline int region_in_sync(struct mirror_set *ms, region_t region,
556 int may_block)
557{
558 int state = dm_rh_get_state(ms->rh, region, may_block);
559 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562static void do_reads(struct mirror_set *ms, struct bio_list *reads)
563{
564 region_t region;
565 struct bio *bio;
566 struct mirror *m;
567
568 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100569 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000570 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
573 * We can only read balance if the region is in sync.
574 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100575 if (likely(region_in_sync(ms, region, 1)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700576 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000577 else if (m && atomic_read(&m->error_count))
578 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000580 if (likely(m))
581 read_async_bio(m, bio);
582 else
583 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585}
586
587/*-----------------------------------------------------------------
588 * Writes.
589 *
590 * We do different things with the write io depending on the
591 * state of the region that it's in:
592 *
593 * SYNC: increment pending, use kcopyd to write to *all* mirrors
594 * RECOVERING: delay the io until recovery completes
595 * NOSYNC: increment pending, just write to the default mirror
596 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000597
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static void write_callback(unsigned long error, void *context)
600{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000601 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct bio *bio = (struct bio *) context;
603 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000604 int should_wake = 0;
605 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000607 ms = bio_get_m(bio)->ms;
608 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
611 * NOTE: We don't decrement the pending count here,
612 * instead it is done by the targets endio function.
613 * This way we handle both writes to SYNC and NOSYNC
614 * regions with the same code.
615 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000616 if (likely(!error)) {
617 bio_endio(bio, ret);
618 return;
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Mikulas Patockaf2ed51a2015-02-12 10:09:20 -0500621 /*
622 * If the bio is discard, return an error, but do not
623 * degrade the array.
624 */
625 if (bio->bi_rw & REQ_DISCARD) {
626 bio_endio(bio, -EOPNOTSUPP);
627 return;
628 }
629
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000630 for (i = 0; i < ms->nr_mirrors; i++)
631 if (test_bit(i, &error))
632 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000633
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000634 /*
635 * Need to raise event. Since raising
636 * events can block, we need to do it in
637 * the main thread.
638 */
639 spin_lock_irqsave(&ms->lock, flags);
640 if (!ms->failures.head)
641 should_wake = 1;
642 bio_list_add(&ms->failures, bio);
643 spin_unlock_irqrestore(&ms->lock, flags);
644 if (should_wake)
645 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648static void do_write(struct mirror_set *ms, struct bio *bio)
649{
650 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100651 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700653 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200654 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
Kent Overstreet003b5c52013-10-11 15:45:43 -0700655 .mem.type = DM_IO_BIO,
656 .mem.ptr.bio = bio,
Milan Broz88be1632007-05-09 02:33:04 -0700657 .notify.fn = write_callback,
658 .notify.context = bio,
659 .client = ms->io_client,
660 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000662 if (bio->bi_rw & REQ_DISCARD) {
663 io_req.bi_rw |= REQ_DISCARD;
664 io_req.mem.type = DM_IO_KMEM;
665 io_req.mem.ptr.addr = NULL;
666 }
667
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000668 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
669 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000671 /*
672 * Use default mirror because we only need it to retrieve the reference
673 * to the mirror set in write_callback().
674 */
675 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700676
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100677 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static void do_writes(struct mirror_set *ms, struct bio_list *writes)
681{
682 int state;
683 struct bio *bio;
684 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100685 struct bio_list requeue;
686 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
687 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!writes->head)
690 return;
691
692 /*
693 * Classify each write.
694 */
695 bio_list_init(&sync);
696 bio_list_init(&nosync);
697 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100698 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 while ((bio = bio_list_pop(writes))) {
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000701 if ((bio->bi_rw & REQ_FLUSH) ||
702 (bio->bi_rw & REQ_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000703 bio_list_add(&sync, bio);
704 continue;
705 }
706
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100707 region = dm_rh_bio_to_region(ms->rh, bio);
708
709 if (log->type->is_remote_recovering &&
710 log->type->is_remote_recovering(log, region)) {
711 bio_list_add(&requeue, bio);
712 continue;
713 }
714
715 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100717 case DM_RH_CLEAN:
718 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 this_list = &sync;
720 break;
721
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100722 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 this_list = &nosync;
724 break;
725
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100726 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 this_list = &recover;
728 break;
729 }
730
731 bio_list_add(this_list, bio);
732 }
733
734 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100735 * Add bios that are delayed due to remote recovery
736 * back on to the write queue
737 */
738 if (unlikely(requeue.head)) {
739 spin_lock_irq(&ms->lock);
740 bio_list_merge(&ms->writes, &requeue);
741 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100742 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100743 }
744
745 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * Increment the pending counts for any regions that will
747 * be written to (writes to recover regions are going to
748 * be delayed).
749 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100750 dm_rh_inc_pending(ms->rh, &sync);
751 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100752
753 /*
754 * If the flush fails on a previous call and succeeds here,
755 * we must not reset the log_failure variable. We need
756 * userspace interaction to do that.
757 */
758 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * Dispatch io.
762 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000763 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000764 spin_lock_irq(&ms->lock);
765 bio_list_merge(&ms->failures, &sync);
766 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100767 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000768 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100769 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000770 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100773 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 while ((bio = bio_list_pop(&nosync))) {
Lidong Zhonged632872015-05-13 14:04:10 +0800776 if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000777 spin_lock_irq(&ms->lock);
778 bio_list_add(&ms->failures, bio);
779 spin_unlock_irq(&ms->lock);
780 wakeup_mirrord(ms);
781 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000782 map_bio(get_default_mirror(ms), bio);
783 generic_make_request(bio);
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786}
787
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000788static void do_failures(struct mirror_set *ms, struct bio_list *failures)
789{
790 struct bio *bio;
791
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000792 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000793 return;
794
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000795 /*
796 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000797 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000798 * until a log has been marked, so we must store them.
799 *
800 * If a 'noflush' suspend is in progress, we can requeue
801 * the I/O's to the core. This give userspace a chance
802 * to reconfigure the mirror, at which point the core
803 * will reissue the writes. If the 'noflush' flag is
804 * not set, we have no choice but to return errors.
805 *
806 * Some writes on the failures list may have been
807 * submitted before the log failure and represent a
808 * failure to write to one of the devices. It is ok
809 * for us to treat them the same and requeue them
810 * as well.
811 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000812 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000813 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000814 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000815 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000816 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000817
818 /*
819 * If all the legs are dead, fail the I/O.
Lidong Zhonged632872015-05-13 14:04:10 +0800820 * If the device has failed and keep_log is enabled,
821 * fail the I/O.
822 *
823 * If we have been told to handle errors, and keep_log
824 * isn't enabled, hold the bio and wait for userspace to
825 * deal with the problem.
826 *
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000827 * Otherwise pretend that the I/O succeeded. (This would
828 * be wrong if the failed leg returned after reboot and
829 * got replicated back to the good legs.)
830 */
Lidong Zhonged632872015-05-13 14:04:10 +0800831
832 if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000833 bio_endio(bio, -EIO);
Lidong Zhonged632872015-05-13 14:04:10 +0800834 else if (errors_handled(ms) && !keep_log(ms))
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000835 hold_bio(ms, bio);
836 else
837 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000838 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000839}
840
841static void trigger_event(struct work_struct *work)
842{
843 struct mirror_set *ms =
844 container_of(work, struct mirror_set, trigger_event);
845
846 dm_table_event(ms->ti->table);
847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/*-----------------------------------------------------------------
850 * kmirrord
851 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100852static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100854 struct mirror_set *ms = container_of(work, struct mirror_set,
855 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000856 struct bio_list reads, writes, failures;
857 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000859 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 reads = ms->reads;
861 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000862 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 bio_list_init(&ms->reads);
864 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000865 bio_list_init(&ms->failures);
866 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100868 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 do_recovery(ms);
870 do_reads(ms, &reads);
871 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000872 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*-----------------------------------------------------------------
876 * Target functions
877 *---------------------------------------------------------------*/
878static struct mirror_set *alloc_context(unsigned int nr_mirrors,
879 uint32_t region_size,
880 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100881 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 size_t len;
884 struct mirror_set *ms = NULL;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
887
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700888 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700890 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return NULL;
892 }
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000895 bio_list_init(&ms->reads);
896 bio_list_init(&ms->writes);
897 bio_list_init(&ms->failures);
898 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 ms->ti = ti;
901 ms->nr_mirrors = nr_mirrors;
902 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
903 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000904 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000905 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000906 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000907 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100909 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700910 if (IS_ERR(ms->io_client)) {
911 ti->error = "Error creating dm_io client";
912 kfree(ms);
913 return NULL;
914 }
915
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100916 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
917 wakeup_all_recovery_waiters,
918 ms->ti->begin, MAX_RECOVERY,
919 dl, region_size, ms->nr_regions);
920 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700921 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100922 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 kfree(ms);
924 return NULL;
925 }
926
927 return ms;
928}
929
930static void free_context(struct mirror_set *ms, struct dm_target *ti,
931 unsigned int m)
932{
933 while (m--)
934 dm_put_device(ti, ms->mirror[m].dev);
935
Milan Broz88be1632007-05-09 02:33:04 -0700936 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100937 dm_region_hash_destroy(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 kfree(ms);
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
942 unsigned int mirror, char **argv)
943{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800944 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100945 char dummy;
Vivek Goyale80d1c82015-07-31 09:20:36 -0400946 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100948 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700949 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -EINVAL;
951 }
952
Vivek Goyale80d1c82015-07-31 09:20:36 -0400953 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
954 &ms->mirror[mirror].dev);
955 if (ret) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700956 ti->error = "Device lookup failure";
Vivek Goyale80d1c82015-07-31 09:20:36 -0400957 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100960 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000961 atomic_set(&(ms->mirror[mirror].error_count), 0);
962 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 ms->mirror[mirror].offset = offset;
964
965 return 0;
966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968/*
969 * Create dirty log: log_type #log_params <log_params>
970 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100971static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100972 unsigned argc, char **argv,
973 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100975 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100976 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100977 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700980 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return NULL;
982 }
983
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100984 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700985 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return NULL;
987 }
988
989 *args_used = 2 + param_count;
990
991 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700992 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return NULL;
994 }
995
Mikulas Patockac0da3742009-12-10 23:52:02 +0000996 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
997 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700999 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return NULL;
1001 }
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return dl;
1004}
1005
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001006static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1007 unsigned *args_used)
1008{
1009 unsigned num_features;
1010 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001011 char dummy;
Lidong Zhonged632872015-05-13 14:04:10 +08001012 int i;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001013
1014 *args_used = 0;
1015
1016 if (!argc)
1017 return 0;
1018
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001019 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001020 ti->error = "Invalid number of features";
1021 return -EINVAL;
1022 }
1023
1024 argc--;
1025 argv++;
1026 (*args_used)++;
1027
1028 if (num_features > argc) {
1029 ti->error = "Not enough arguments to support feature count";
1030 return -EINVAL;
1031 }
1032
Lidong Zhonged632872015-05-13 14:04:10 +08001033 for (i = 0; i < num_features; i++) {
1034 if (!strcmp("handle_errors", argv[0]))
1035 ms->features |= DM_RAID1_HANDLE_ERRORS;
1036 else if (!strcmp("keep_log", argv[0]))
1037 ms->features |= DM_RAID1_KEEP_LOG;
1038 else {
1039 ti->error = "Unrecognised feature requested";
1040 return -EINVAL;
1041 }
1042
1043 argc--;
1044 argv++;
1045 (*args_used)++;
1046 }
1047 if (!errors_handled(ms) && keep_log(ms)) {
1048 ti->error = "keep_log feature requires the handle_errors feature";
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001049 return -EINVAL;
1050 }
1051
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001052 return 0;
1053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055/*
1056 * Construct a mirror mapping:
1057 *
1058 * log_type #log_params <log_params>
1059 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001060 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *
1062 * log_type is "core" or "disk"
1063 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001064 *
Lidong Zhonged632872015-05-13 14:04:10 +08001065 * If present, supported features are "handle_errors" and "keep_log".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1068{
1069 int r;
1070 unsigned int nr_mirrors, m, args_used;
1071 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001072 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001073 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 dl = create_dirty_log(ti, argc, argv, &args_used);
1076 if (!dl)
1077 return -EINVAL;
1078
1079 argv += args_used;
1080 argc -= args_used;
1081
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001082 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001083 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001084 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001085 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EINVAL;
1087 }
1088
1089 argv++, argc--;
1090
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001091 if (argc < nr_mirrors * 2) {
1092 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001093 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -EINVAL;
1095 }
1096
1097 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1098 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001099 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -ENOMEM;
1101 }
1102
1103 /* Get the mirror parameter sets */
1104 for (m = 0; m < nr_mirrors; m++) {
1105 r = get_mirror(ms, ti, m, argv);
1106 if (r) {
1107 free_context(ms, ti, m);
1108 return r;
1109 }
1110 argv += 2;
1111 argc -= 2;
1112 }
1113
1114 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001115
1116 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1117 if (r)
1118 goto err_free_context;
1119
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001120 ti->num_flush_bios = 1;
1121 ti->num_discard_bios = 1;
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001122 ti->per_bio_data_size = sizeof(struct dm_raid1_bio_record);
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001123 ti->discard_zeroes_data_unsupported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Tejun Heo670368a2013-07-30 08:40:21 -04001125 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001126 if (!ms->kmirrord_wq) {
1127 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001128 r = -ENOMEM;
1129 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001130 }
1131 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001132 init_timer(&ms->timer);
1133 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001134 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001135
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001136 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001137 if (r)
1138 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001139
1140 argv += args_used;
1141 argc -= args_used;
1142
Jonathan Brassowf44db672007-07-12 17:29:04 +01001143 /*
1144 * Any read-balancing addition depends on the
1145 * DM_RAID1_HANDLE_ERRORS flag being present.
1146 * This is because the decision to balance depends
1147 * on the sync state of a region. If the above
1148 * flag is not present, we ignore errors; and
1149 * the sync state may be inaccurate.
1150 */
1151
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001152 if (argc) {
1153 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001154 r = -EINVAL;
1155 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001156 }
1157
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001158 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001159 if (IS_ERR(ms->kcopyd_client)) {
1160 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001161 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001164 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001166
1167err_destroy_wq:
1168 destroy_workqueue(ms->kmirrord_wq);
1169err_free_context:
1170 free_context(ms, ti, ms->nr_mirrors);
1171 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static void mirror_dtr(struct dm_target *ti)
1175{
1176 struct mirror_set *ms = (struct mirror_set *) ti->private;
1177
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001178 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001179 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001180 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001181 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001182 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 free_context(ms, ti, ms->nr_mirrors);
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/*
1187 * Mirror mapping function
1188 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001189static int mirror_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 int r, rw = bio_rw(bio);
1192 struct mirror *m;
1193 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001194 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001195 struct dm_raid1_bio_record *bio_record =
1196 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1197
1198 bio_record->details.bi_bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001201 /* Save region for mirror_end_io() handler */
Mikulas Patocka0045d612012-12-21 20:23:40 +00001202 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001204 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001207 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (r < 0 && r != -EWOULDBLOCK)
1209 return r;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001212 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001214 if (!r || (r == -EWOULDBLOCK)) {
1215 if (rw == READA)
1216 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001219 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001222 /*
1223 * The region is in-sync and we can perform reads directly.
1224 * Store enough information so we can retry if it fails.
1225 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001226 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001227 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EIO;
1229
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001230 dm_bio_record(&bio_record->details, bio);
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001231 bio_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001232
1233 map_bio(m, bio);
1234
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001235 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001238static int mirror_end_io(struct dm_target *ti, struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 int rw = bio_rw(bio);
1241 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001242 struct mirror *m = NULL;
1243 struct dm_bio_details *bd = NULL;
Mikulas Patocka0045d612012-12-21 20:23:40 +00001244 struct dm_raid1_bio_record *bio_record =
1245 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /*
1248 * We need to dec pending if this was a write.
1249 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001250 if (rw == WRITE) {
Mikulas Patocka751f1882012-07-20 14:25:03 +01001251 if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
Mikulas Patocka0045d612012-12-21 20:23:40 +00001252 dm_rh_dec(ms->rh, bio_record->write_region);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001253 return error;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001256 if (error == -EOPNOTSUPP)
1257 goto out;
1258
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001259 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001260 goto out;
1261
1262 if (unlikely(error)) {
Mikulas Patocka0045d612012-12-21 20:23:40 +00001263 if (!bio_record->details.bi_bdev) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001264 /*
1265 * There wasn't enough memory to record necessary
1266 * information for a retry or there was no other
1267 * mirror in-sync.
1268 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001269 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001270 return -EIO;
1271 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001272
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001273 m = bio_record->m;
Adrian Bunke03f1a82008-02-19 19:44:19 +00001274
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001275 DMERR("Mirror read failed from %s. Trying alternative device.",
1276 m->dev->name);
1277
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001278 fail_mirror(m, DM_RAID1_READ_ERROR);
1279
1280 /*
1281 * A failed read is requeued for another attempt using an intact
1282 * mirror.
1283 */
1284 if (default_ok(m) || mirror_available(ms, bio)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001285 bd = &bio_record->details;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001286
1287 dm_bio_restore(bd, bio);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001288 bio_record->details.bi_bdev = NULL;
Mikulas Patockaf3a44fe2014-02-18 09:57:22 -05001289
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001290 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001291 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001292 }
1293 DMERR("All replicated volumes dead, failing I/O");
1294 }
1295
1296out:
Mikulas Patocka0045d612012-12-21 20:23:40 +00001297 bio_record->details.bi_bdev = NULL;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001298
1299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001302static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001305 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Mikulas Patocka04788502009-12-10 23:52:03 +00001307 struct bio_list holds;
1308 struct bio *bio;
1309
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001310 atomic_set(&ms->suspend, 1);
1311
1312 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001313 * Process bios in the hold list to start recovery waiting
1314 * for bios in the hold list. After the process, no bio has
1315 * a chance to be added in the hold list because ms->suspend
1316 * is set.
1317 */
1318 spin_lock_irq(&ms->lock);
1319 holds = ms->holds;
1320 bio_list_init(&ms->holds);
1321 spin_unlock_irq(&ms->lock);
1322
1323 while ((bio = bio_list_pop(&holds)))
1324 hold_bio(ms, bio);
1325
1326 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001327 * We must finish up all the work that we've
1328 * generated (i.e. recovery work).
1329 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001330 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001331
Jonathan E Brassow33184042006-11-08 17:44:44 -08001332 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001333 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001334
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001335 if (log->type->presuspend && log->type->presuspend(log))
1336 /* FIXME: need better error handling */
1337 DMWARN("log presuspend failed");
1338
1339 /*
1340 * Now that recovery is complete/stopped and the
1341 * delayed bios are queued, we need to wait for
1342 * the worker thread to complete. This way,
1343 * we know that all of our I/O has been pushed.
1344 */
1345 flush_workqueue(ms->kmirrord_wq);
1346}
1347
1348static void mirror_postsuspend(struct dm_target *ti)
1349{
1350 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001351 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001352
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001353 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001355 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358static void mirror_resume(struct dm_target *ti)
1359{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001360 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001361 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001362
1363 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (log->type->resume && log->type->resume(log))
1365 /* FIXME: need better error handling */
1366 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001367 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001370/*
1371 * device_status_char
1372 * @m: mirror device/leg we want the status of
1373 *
1374 * We return one character representing the most severe error
1375 * we have encountered.
1376 * A => Alive - No failures
1377 * D => Dead - A write failure occurred leaving mirror out-of-sync
1378 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1379 * R => Read - A read failure occurred, mirror data unaffected
1380 *
1381 * Returns: <char>
1382 */
1383static char device_status_char(struct mirror *m)
1384{
1385 if (!atomic_read(&(m->error_count)))
1386 return 'A';
1387
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001388 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1389 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001390 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1391 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1392}
1393
1394
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001395static void mirror_status(struct dm_target *ti, status_type_t type,
1396 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001398 unsigned int m, sz = 0;
Lidong Zhonged632872015-05-13 14:04:10 +08001399 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001401 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001402 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 switch (type) {
1405 case STATUSTYPE_INFO:
1406 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001407 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001409 buffer[m] = device_status_char(&(ms->mirror[m]));
1410 }
1411 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001413 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001414 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001415 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001416
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001417 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420
1421 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001422 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001423
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001424 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001426 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001427 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001428
Lidong Zhonged632872015-05-13 14:04:10 +08001429 num_feature_args += !!errors_handled(ms);
1430 num_feature_args += !!keep_log(ms);
1431 if (num_feature_args) {
1432 DMEMIT(" %d", num_feature_args);
1433 if (errors_handled(ms))
1434 DMEMIT(" handle_errors");
1435 if (keep_log(ms))
1436 DMEMIT(" keep_log");
1437 }
1438
1439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001443static int mirror_iterate_devices(struct dm_target *ti,
1444 iterate_devices_callout_fn fn, void *data)
1445{
1446 struct mirror_set *ms = ti->private;
1447 int ret = 0;
1448 unsigned i;
1449
1450 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1451 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001452 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001453
1454 return ret;
1455}
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457static struct target_type mirror_target = {
1458 .name = "mirror",
Lidong Zhonged632872015-05-13 14:04:10 +08001459 .version = {1, 14, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 .module = THIS_MODULE,
1461 .ctr = mirror_ctr,
1462 .dtr = mirror_dtr,
1463 .map = mirror_map,
1464 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001465 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 .postsuspend = mirror_postsuspend,
1467 .resume = mirror_resume,
1468 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001469 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470};
1471
1472static int __init dm_mirror_init(void)
1473{
1474 int r;
1475
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001476 r = dm_register_target(&mirror_target);
1477 if (r < 0) {
1478 DMERR("Failed to register mirror target");
1479 goto bad_target;
1480 }
1481
1482 return 0;
1483
1484bad_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return r;
1486}
1487
1488static void __exit dm_mirror_exit(void)
1489{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001490 dm_unregister_target(&mirror_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493/* Module hooks */
1494module_init(dm_mirror_init);
1495module_exit(dm_mirror_exit);
1496
1497MODULE_DESCRIPTION(DM_NAME " mirror target");
1498MODULE_AUTHOR("Joe Thornber");
1499MODULE_LICENSE("GPL");