blob: 580c49cc8079f7b7e82b915a552a0b2ef7030947 [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
Kees Cook8376d3c2017-10-16 17:01:48 -070097static void delayed_wake_fn(struct timer_list *t)
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010098{
Kees Cook8376d3c2017-10-16 17:01:48 -070099 struct mirror_set *ms = from_timer(ms, t, timer);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100100
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;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100111 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;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200146 /* if details->bi_disk == 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
Lidong Zhonged632872015-05-13 14:04:10 +0800232 if (!ms->in_sync && !keep_log(ms)) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000233 /*
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 = {
Mike Christiee6047142016-06-05 14:32:04 -0500261 .bi_op = REQ_OP_WRITE,
Jan Karaff0361b2017-05-31 09:44:32 +0200262 .bi_op_flags = REQ_PREFLUSH | REQ_SYNC,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000263 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000264 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000265 .client = ms->io_client,
266 };
267
268 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
269 io[i].bdev = m->dev->bdev;
270 io[i].sector = 0;
271 io[i].count = 0;
272 }
273
274 error_bits = -1;
275 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
276 if (unlikely(error_bits != 0)) {
277 for (i = 0; i < ms->nr_mirrors; i++)
278 if (test_bit(i, &error_bits))
279 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000280 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000281 return -EIO;
282 }
283
284 return 0;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*-----------------------------------------------------------------
288 * Recovery.
289 *
290 * When a mirror is first activated we may find that some regions
291 * are in the no-sync state. We have to recover these by
292 * recopying from the default mirror to all the others.
293 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700294static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 void *context)
296{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100297 struct dm_region *reg = context;
298 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000299 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000301 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100302 /* Read error means the failure of default mirror. */
303 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000304 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
305 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100306
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000307 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700308 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100309 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000310 /*
311 * Bits correspond to devices (excluding default mirror).
312 * The default mirror cannot change during recovery.
313 */
314 for (m = 0; m < ms->nr_mirrors; m++) {
315 if (&ms->mirror[m] == get_default_mirror(ms))
316 continue;
317 if (test_bit(bit, &write_err))
318 fail_mirror(ms->mirror + m,
319 DM_RAID1_SYNC_ERROR);
320 bit++;
321 }
322 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100323
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100324 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100327static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100330 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100331 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct mirror *m;
333 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100334 region_t key = dm_rh_get_region_key(reg);
335 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000338 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100340 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
341 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /*
343 * The final region may be smaller than
344 * region_size.
345 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100346 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100348 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100350 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* fill in the destinations */
353 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000354 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 continue;
356
357 m = ms->mirror + i;
358 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100359 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dest->count = from.count;
361 dest++;
362 }
363
364 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100365 if (!errors_handled(ms))
366 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
367
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100368 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
369 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return r;
372}
373
Lidong Zhonged632872015-05-13 14:04:10 +0800374static void reset_ms_flags(struct mirror_set *ms)
375{
376 unsigned int m;
377
378 ms->leg_failure = 0;
379 for (m = 0; m < ms->nr_mirrors; m++) {
380 atomic_set(&(ms->mirror[m].error_count), 0);
381 ms->mirror[m].error_type = 0;
382 }
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static void do_recovery(struct mirror_set *ms)
386{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100387 struct dm_region *reg;
388 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /*
392 * Start quiescing some regions.
393 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100394 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * Copy any already quiesced regions.
398 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100399 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 r = recover(ms, reg);
401 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100402 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 /*
406 * Update the in sync flag.
407 */
408 if (!ms->in_sync &&
409 (log->type->get_sync_count(log) == ms->nr_regions)) {
410 /* the sync is complete */
411 dm_table_event(ms->ti->table);
412 ms->in_sync = 1;
Lidong Zhonged632872015-05-13 14:04:10 +0800413 reset_ms_flags(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415}
416
417/*-----------------------------------------------------------------
418 * Reads
419 *---------------------------------------------------------------*/
420static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
421{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000422 struct mirror *m = get_default_mirror(ms);
423
424 do {
425 if (likely(!atomic_read(&m->error_count)))
426 return m;
427
428 if (m-- == ms->mirror)
429 m += ms->nr_mirrors;
430 } while (m != get_default_mirror(ms));
431
432 return NULL;
433}
434
435static int default_ok(struct mirror *m)
436{
437 struct mirror *default_mirror = get_default_mirror(m->ms);
438
439 return !atomic_read(&default_mirror->error_count);
440}
441
442static int mirror_available(struct mirror_set *ms, struct bio *bio)
443{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100444 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
445 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000446
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100447 if (log->type->in_sync(log, region, 0))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700448 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000449
450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/*
454 * remap a buffer to a particular mirror.
455 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000456static sector_t map_sector(struct mirror *m, struct bio *bio)
457{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700458 if (unlikely(!bio->bi_iter.bi_size))
Mikulas Patocka41841532009-12-10 23:51:59 +0000459 return 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700460 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000461}
462
463static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200465 bio_set_dev(bio, m->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700466 bio->bi_iter.bi_sector = map_sector(m, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000467}
468
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100469static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000470 struct bio *bio)
471{
472 io->bdev = m->dev->bdev;
473 io->sector = map_sector(m, bio);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800474 io->count = bio_sectors(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000475}
476
Mikulas Patocka04788502009-12-10 23:52:03 +0000477static void hold_bio(struct mirror_set *ms, struct bio *bio)
478{
479 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000480 * Lock is required to avoid race condition during suspend
481 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000482 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000483 spin_lock_irq(&ms->lock);
484
Mikulas Patocka04788502009-12-10 23:52:03 +0000485 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000486 spin_unlock_irq(&ms->lock);
487
488 /*
489 * If device is suspended, complete the bio.
490 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000491 if (dm_noflush_suspending(ms->ti))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200492 bio->bi_status = BLK_STS_DM_REQUEUE;
Mikulas Patocka04788502009-12-10 23:52:03 +0000493 else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200494 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200495
496 bio_endio(bio);
Mikulas Patocka04788502009-12-10 23:52:03 +0000497 return;
498 }
499
500 /*
501 * Hold bio until the suspend is complete.
502 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000503 bio_list_add(&ms->holds, bio);
504 spin_unlock_irq(&ms->lock);
505}
506
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000507/*-----------------------------------------------------------------
508 * Reads
509 *---------------------------------------------------------------*/
510static void read_callback(unsigned long error, void *context)
511{
512 struct bio *bio = context;
513 struct mirror *m;
514
515 m = bio_get_m(bio);
516 bio_set_m(bio, NULL);
517
518 if (likely(!error)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200519 bio_endio(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000520 return;
521 }
522
523 fail_mirror(m, DM_RAID1_READ_ERROR);
524
525 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
526 DMWARN_LIMIT("Read failure on mirror device %s. "
527 "Trying alternative device.",
528 m->dev->name);
Christoph Hellwig70246282016-07-19 11:28:41 +0200529 queue_bio(m->ms, bio, bio_data_dir(bio));
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000530 return;
531 }
532
533 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
534 m->dev->name);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200535 bio_io_error(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000536}
537
538/* Asynchronous read. */
539static void read_async_bio(struct mirror *m, struct bio *bio)
540{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100541 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000542 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500543 .bi_op = REQ_OP_READ,
544 .bi_op_flags = 0,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700545 .mem.type = DM_IO_BIO,
546 .mem.ptr.bio = bio,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000547 .notify.fn = read_callback,
548 .notify.context = bio,
549 .client = m->ms->io_client,
550 };
551
552 map_region(&io, m, bio);
553 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100554 BUG_ON(dm_io(&io_req, 1, &io, NULL));
555}
556
557static inline int region_in_sync(struct mirror_set *ms, region_t region,
558 int may_block)
559{
560 int state = dm_rh_get_state(ms->rh, region, may_block);
561 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static void do_reads(struct mirror_set *ms, struct bio_list *reads)
565{
566 region_t region;
567 struct bio *bio;
568 struct mirror *m;
569
570 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100571 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000572 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /*
575 * We can only read balance if the region is in sync.
576 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100577 if (likely(region_in_sync(ms, region, 1)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700578 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000579 else if (m && atomic_read(&m->error_count))
580 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000582 if (likely(m))
583 read_async_bio(m, bio);
584 else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200585 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587}
588
589/*-----------------------------------------------------------------
590 * Writes.
591 *
592 * We do different things with the write io depending on the
593 * state of the region that it's in:
594 *
595 * SYNC: increment pending, use kcopyd to write to *all* mirrors
596 * RECOVERING: delay the io until recovery completes
597 * NOSYNC: increment pending, just write to the default mirror
598 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000599
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static void write_callback(unsigned long error, void *context)
602{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200603 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct bio *bio = (struct bio *) context;
605 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000606 int should_wake = 0;
607 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000609 ms = bio_get_m(bio)->ms;
610 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /*
613 * NOTE: We don't decrement the pending count here,
614 * instead it is done by the targets endio function.
615 * This way we handle both writes to SYNC and NOSYNC
616 * regions with the same code.
617 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000618 if (likely(!error)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200619 bio_endio(bio);
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000620 return;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Mikulas Patockaf2ed51a2015-02-12 10:09:20 -0500623 /*
624 * If the bio is discard, return an error, but do not
625 * degrade the array.
626 */
Mike Christiee6047142016-06-05 14:32:04 -0500627 if (bio_op(bio) == REQ_OP_DISCARD) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200628 bio->bi_status = BLK_STS_NOTSUPP;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200629 bio_endio(bio);
Mikulas Patockaf2ed51a2015-02-12 10:09:20 -0500630 return;
631 }
632
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000633 for (i = 0; i < ms->nr_mirrors; i++)
634 if (test_bit(i, &error))
635 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000636
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000637 /*
638 * Need to raise event. Since raising
639 * events can block, we need to do it in
640 * the main thread.
641 */
642 spin_lock_irqsave(&ms->lock, flags);
643 if (!ms->failures.head)
644 should_wake = 1;
645 bio_list_add(&ms->failures, bio);
646 spin_unlock_irqrestore(&ms->lock, flags);
647 if (should_wake)
648 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static void do_write(struct mirror_set *ms, struct bio *bio)
652{
653 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100654 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700656 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500657 .bi_op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600658 .bi_op_flags = bio->bi_opf & (REQ_FUA | REQ_PREFLUSH),
Kent Overstreet003b5c52013-10-11 15:45:43 -0700659 .mem.type = DM_IO_BIO,
660 .mem.ptr.bio = bio,
Milan Broz88be1632007-05-09 02:33:04 -0700661 .notify.fn = write_callback,
662 .notify.context = bio,
663 .client = ms->io_client,
664 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Mike Christiee6047142016-06-05 14:32:04 -0500666 if (bio_op(bio) == REQ_OP_DISCARD) {
667 io_req.bi_op = REQ_OP_DISCARD;
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000668 io_req.mem.type = DM_IO_KMEM;
669 io_req.mem.ptr.addr = NULL;
670 }
671
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000672 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
673 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000675 /*
676 * Use default mirror because we only need it to retrieve the reference
677 * to the mirror set in write_callback().
678 */
679 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700680
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100681 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static void do_writes(struct mirror_set *ms, struct bio_list *writes)
685{
686 int state;
687 struct bio *bio;
688 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100689 struct bio_list requeue;
690 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
691 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (!writes->head)
694 return;
695
696 /*
697 * Classify each write.
698 */
699 bio_list_init(&sync);
700 bio_list_init(&nosync);
701 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100702 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 while ((bio = bio_list_pop(writes))) {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600705 if ((bio->bi_opf & REQ_PREFLUSH) ||
Mike Christiee6047142016-06-05 14:32:04 -0500706 (bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000707 bio_list_add(&sync, bio);
708 continue;
709 }
710
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100711 region = dm_rh_bio_to_region(ms->rh, bio);
712
713 if (log->type->is_remote_recovering &&
714 log->type->is_remote_recovering(log, region)) {
715 bio_list_add(&requeue, bio);
716 continue;
717 }
718
719 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100721 case DM_RH_CLEAN:
722 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 this_list = &sync;
724 break;
725
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100726 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 this_list = &nosync;
728 break;
729
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100730 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 this_list = &recover;
732 break;
733 }
734
735 bio_list_add(this_list, bio);
736 }
737
738 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100739 * Add bios that are delayed due to remote recovery
740 * back on to the write queue
741 */
742 if (unlikely(requeue.head)) {
743 spin_lock_irq(&ms->lock);
744 bio_list_merge(&ms->writes, &requeue);
745 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100746 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100747 }
748
749 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * Increment the pending counts for any regions that will
751 * be written to (writes to recover regions are going to
752 * be delayed).
753 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100754 dm_rh_inc_pending(ms->rh, &sync);
755 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100756
757 /*
758 * If the flush fails on a previous call and succeeds here,
759 * we must not reset the log_failure variable. We need
760 * userspace interaction to do that.
761 */
762 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /*
765 * Dispatch io.
766 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000767 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000768 spin_lock_irq(&ms->lock);
769 bio_list_merge(&ms->failures, &sync);
770 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100771 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000772 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100773 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000774 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100777 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 while ((bio = bio_list_pop(&nosync))) {
Lidong Zhonged632872015-05-13 14:04:10 +0800780 if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000781 spin_lock_irq(&ms->lock);
782 bio_list_add(&ms->failures, bio);
783 spin_unlock_irq(&ms->lock);
784 wakeup_mirrord(ms);
785 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000786 map_bio(get_default_mirror(ms), bio);
787 generic_make_request(bio);
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790}
791
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000792static void do_failures(struct mirror_set *ms, struct bio_list *failures)
793{
794 struct bio *bio;
795
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000796 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000797 return;
798
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000799 /*
800 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000801 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000802 * until a log has been marked, so we must store them.
803 *
804 * If a 'noflush' suspend is in progress, we can requeue
805 * the I/O's to the core. This give userspace a chance
806 * to reconfigure the mirror, at which point the core
807 * will reissue the writes. If the 'noflush' flag is
808 * not set, we have no choice but to return errors.
809 *
810 * Some writes on the failures list may have been
811 * submitted before the log failure and represent a
812 * failure to write to one of the devices. It is ok
813 * for us to treat them the same and requeue them
814 * as well.
815 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000816 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000817 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000818 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000819 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000820 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000821
822 /*
823 * If all the legs are dead, fail the I/O.
Lidong Zhonged632872015-05-13 14:04:10 +0800824 * If the device has failed and keep_log is enabled,
825 * fail the I/O.
826 *
827 * If we have been told to handle errors, and keep_log
828 * isn't enabled, hold the bio and wait for userspace to
829 * deal with the problem.
830 *
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000831 * Otherwise pretend that the I/O succeeded. (This would
832 * be wrong if the failed leg returned after reboot and
833 * got replicated back to the good legs.)
834 */
Lidong Zhonged632872015-05-13 14:04:10 +0800835 if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200836 bio_io_error(bio);
Lidong Zhonged632872015-05-13 14:04:10 +0800837 else if (errors_handled(ms) && !keep_log(ms))
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000838 hold_bio(ms, bio);
839 else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200840 bio_endio(bio);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000841 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000842}
843
844static void trigger_event(struct work_struct *work)
845{
846 struct mirror_set *ms =
847 container_of(work, struct mirror_set, trigger_event);
848
849 dm_table_event(ms->ti->table);
850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*-----------------------------------------------------------------
853 * kmirrord
854 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100855static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100857 struct mirror_set *ms = container_of(work, struct mirror_set,
858 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000859 struct bio_list reads, writes, failures;
860 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000862 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 reads = ms->reads;
864 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000865 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 bio_list_init(&ms->reads);
867 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000868 bio_list_init(&ms->failures);
869 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100871 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 do_recovery(ms);
873 do_reads(ms, &reads);
874 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000875 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/*-----------------------------------------------------------------
879 * Target functions
880 *---------------------------------------------------------------*/
881static struct mirror_set *alloc_context(unsigned int nr_mirrors,
882 uint32_t region_size,
883 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100884 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 size_t len;
887 struct mirror_set *ms = NULL;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
890
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700891 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700893 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return NULL;
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000898 bio_list_init(&ms->reads);
899 bio_list_init(&ms->writes);
900 bio_list_init(&ms->failures);
901 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 ms->ti = ti;
904 ms->nr_mirrors = nr_mirrors;
905 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
906 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000907 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000908 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000909 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000910 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100912 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700913 if (IS_ERR(ms->io_client)) {
914 ti->error = "Error creating dm_io client";
915 kfree(ms);
916 return NULL;
917 }
918
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100919 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
920 wakeup_all_recovery_waiters,
921 ms->ti->begin, MAX_RECOVERY,
922 dl, region_size, ms->nr_regions);
923 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700924 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100925 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 kfree(ms);
927 return NULL;
928 }
929
930 return ms;
931}
932
933static void free_context(struct mirror_set *ms, struct dm_target *ti,
934 unsigned int m)
935{
936 while (m--)
937 dm_put_device(ti, ms->mirror[m].dev);
938
Milan Broz88be1632007-05-09 02:33:04 -0700939 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100940 dm_region_hash_destroy(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 kfree(ms);
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
945 unsigned int mirror, char **argv)
946{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800947 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100948 char dummy;
Vivek Goyale80d1c82015-07-31 09:20:36 -0400949 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100951 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700952 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EINVAL;
954 }
955
Vivek Goyale80d1c82015-07-31 09:20:36 -0400956 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
957 &ms->mirror[mirror].dev);
958 if (ret) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700959 ti->error = "Device lookup failure";
Vivek Goyale80d1c82015-07-31 09:20:36 -0400960 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100963 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000964 atomic_set(&(ms->mirror[mirror].error_count), 0);
965 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 ms->mirror[mirror].offset = offset;
967
968 return 0;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 * Create dirty log: log_type #log_params <log_params>
973 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100974static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100975 unsigned argc, char **argv,
976 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100978 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100979 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100980 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700983 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return NULL;
985 }
986
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100987 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700988 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return NULL;
990 }
991
992 *args_used = 2 + param_count;
993
994 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700995 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return NULL;
997 }
998
Mikulas Patockac0da3742009-12-10 23:52:02 +0000999 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
1000 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001002 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return NULL;
1004 }
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return dl;
1007}
1008
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001009static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1010 unsigned *args_used)
1011{
1012 unsigned num_features;
1013 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001014 char dummy;
Lidong Zhonged632872015-05-13 14:04:10 +08001015 int i;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001016
1017 *args_used = 0;
1018
1019 if (!argc)
1020 return 0;
1021
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001022 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001023 ti->error = "Invalid number of features";
1024 return -EINVAL;
1025 }
1026
1027 argc--;
1028 argv++;
1029 (*args_used)++;
1030
1031 if (num_features > argc) {
1032 ti->error = "Not enough arguments to support feature count";
1033 return -EINVAL;
1034 }
1035
Lidong Zhonged632872015-05-13 14:04:10 +08001036 for (i = 0; i < num_features; i++) {
1037 if (!strcmp("handle_errors", argv[0]))
1038 ms->features |= DM_RAID1_HANDLE_ERRORS;
1039 else if (!strcmp("keep_log", argv[0]))
1040 ms->features |= DM_RAID1_KEEP_LOG;
1041 else {
1042 ti->error = "Unrecognised feature requested";
1043 return -EINVAL;
1044 }
1045
1046 argc--;
1047 argv++;
1048 (*args_used)++;
1049 }
1050 if (!errors_handled(ms) && keep_log(ms)) {
1051 ti->error = "keep_log feature requires the handle_errors feature";
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001052 return -EINVAL;
1053 }
1054
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001055 return 0;
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/*
1059 * Construct a mirror mapping:
1060 *
1061 * log_type #log_params <log_params>
1062 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001063 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 *
1065 * log_type is "core" or "disk"
1066 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001067 *
Lidong Zhonged632872015-05-13 14:04:10 +08001068 * If present, supported features are "handle_errors" and "keep_log".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1071{
1072 int r;
1073 unsigned int nr_mirrors, m, args_used;
1074 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001075 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001076 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 dl = create_dirty_log(ti, argc, argv, &args_used);
1079 if (!dl)
1080 return -EINVAL;
1081
1082 argv += args_used;
1083 argc -= args_used;
1084
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001085 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001086 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001087 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001088 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return -EINVAL;
1090 }
1091
1092 argv++, argc--;
1093
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001094 if (argc < nr_mirrors * 2) {
1095 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001096 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EINVAL;
1098 }
1099
1100 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1101 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001102 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOMEM;
1104 }
1105
1106 /* Get the mirror parameter sets */
1107 for (m = 0; m < nr_mirrors; m++) {
1108 r = get_mirror(ms, ti, m, argv);
1109 if (r) {
1110 free_context(ms, ti, m);
1111 return r;
1112 }
1113 argv += 2;
1114 argc -= 2;
1115 }
1116
1117 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001118
1119 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1120 if (r)
1121 goto err_free_context;
1122
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001123 ti->num_flush_bios = 1;
1124 ti->num_discard_bios = 1;
Mike Snitzer30187e12016-01-31 13:28:26 -05001125 ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Tejun Heo670368a2013-07-30 08:40:21 -04001127 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001128 if (!ms->kmirrord_wq) {
1129 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001130 r = -ENOMEM;
1131 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001132 }
1133 INIT_WORK(&ms->kmirrord_work, do_mirror);
Kees Cook8376d3c2017-10-16 17:01:48 -07001134 timer_setup(&ms->timer, delayed_wake_fn, 0);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001135 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001136 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001137
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001138 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001139 if (r)
1140 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001141
1142 argv += args_used;
1143 argc -= args_used;
1144
Jonathan Brassowf44db672007-07-12 17:29:04 +01001145 /*
1146 * Any read-balancing addition depends on the
1147 * DM_RAID1_HANDLE_ERRORS flag being present.
1148 * This is because the decision to balance depends
1149 * on the sync state of a region. If the above
1150 * flag is not present, we ignore errors; and
1151 * the sync state may be inaccurate.
1152 */
1153
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001154 if (argc) {
1155 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001156 r = -EINVAL;
1157 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001158 }
1159
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001160 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001161 if (IS_ERR(ms->kcopyd_client)) {
1162 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001163 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001166 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001168
1169err_destroy_wq:
1170 destroy_workqueue(ms->kmirrord_wq);
1171err_free_context:
1172 free_context(ms, ti, ms->nr_mirrors);
1173 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176static void mirror_dtr(struct dm_target *ti)
1177{
1178 struct mirror_set *ms = (struct mirror_set *) ti->private;
1179
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001180 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001181 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001182 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001183 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001184 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 free_context(ms, ti, ms->nr_mirrors);
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/*
1189 * Mirror mapping function
1190 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001191static int mirror_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Christoph Hellwig70246282016-07-19 11:28:41 +02001193 int r, rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct mirror *m;
1195 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001196 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001197 struct dm_raid1_bio_record *bio_record =
1198 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1199
Christoph Hellwig74d46992017-08-23 19:10:32 +02001200 bio_record->details.bi_disk = NULL;
Mike Snitzercd15fb62017-06-15 08:39:15 -04001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001203 /* Save region for mirror_end_io() handler */
Mikulas Patocka0045d612012-12-21 20:23:40 +00001204 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001206 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001209 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (r < 0 && r != -EWOULDBLOCK)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001211 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001214 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001216 if (!r || (r == -EWOULDBLOCK)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001217 if (bio->bi_opf & REQ_RAHEAD)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001218 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001221 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001224 /*
1225 * The region is in-sync and we can perform reads directly.
1226 * Store enough information so we can retry if it fails.
1227 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001228 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001229 if (unlikely(!m))
Christoph Hellwig846785e2017-06-03 09:38:02 +02001230 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001232 dm_bio_record(&bio_record->details, bio);
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001233 bio_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001234
1235 map_bio(m, bio);
1236
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001237 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001240static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1241 blk_status_t *error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Christoph Hellwig70246282016-07-19 11:28:41 +02001243 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001245 struct mirror *m = NULL;
1246 struct dm_bio_details *bd = NULL;
Mikulas Patocka0045d612012-12-21 20:23:40 +00001247 struct dm_raid1_bio_record *bio_record =
1248 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /*
1251 * We need to dec pending if this was a write.
1252 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001253 if (rw == WRITE) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001254 if (!(bio->bi_opf & REQ_PREFLUSH) &&
Mike Christie28a8f0d2016-06-05 14:32:25 -05001255 bio_op(bio) != REQ_OP_DISCARD)
Mikulas Patocka0045d612012-12-21 20:23:40 +00001256 dm_rh_dec(ms->rh, bio_record->write_region);
Christoph Hellwig1be56902017-06-03 09:38:03 +02001257 return DM_ENDIO_DONE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001260 if (*error == BLK_STS_NOTSUPP)
Mike Snitzercd15fb62017-06-15 08:39:15 -04001261 goto out;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001262
Christoph Hellwig9966afa2017-06-03 09:37:57 +02001263 if (bio->bi_opf & REQ_RAHEAD)
Mike Snitzercd15fb62017-06-15 08:39:15 -04001264 goto out;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001265
Christoph Hellwig1be56902017-06-03 09:38:03 +02001266 if (unlikely(*error)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001267 if (!bio_record->details.bi_disk) {
Mike Snitzercd15fb62017-06-15 08:39:15 -04001268 /*
1269 * There wasn't enough memory to record necessary
1270 * information for a retry or there was no other
1271 * mirror in-sync.
1272 */
1273 DMERR_LIMIT("Mirror read failed.");
Linus Torvaldsc6b1e362017-07-03 10:34:51 -07001274 return DM_ENDIO_DONE;
Mike Snitzercd15fb62017-06-15 08:39:15 -04001275 }
1276
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001277 m = bio_record->m;
Adrian Bunke03f1a82008-02-19 19:44:19 +00001278
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001279 DMERR("Mirror read failed from %s. Trying alternative device.",
1280 m->dev->name);
1281
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001282 fail_mirror(m, DM_RAID1_READ_ERROR);
1283
1284 /*
1285 * A failed read is requeued for another attempt using an intact
1286 * mirror.
1287 */
1288 if (default_ok(m) || mirror_available(ms, bio)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001289 bd = &bio_record->details;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001290
1291 dm_bio_restore(bd, bio);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001292 bio_record->details.bi_disk = NULL;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001293 bio->bi_status = 0;
Mikulas Patockaf3a44fe2014-02-18 09:57:22 -05001294
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001295 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001296 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001297 }
1298 DMERR("All replicated volumes dead, failing I/O");
1299 }
1300
Mike Snitzercd15fb62017-06-15 08:39:15 -04001301out:
Christoph Hellwig74d46992017-08-23 19:10:32 +02001302 bio_record->details.bi_disk = NULL;
Mike Snitzercd15fb62017-06-15 08:39:15 -04001303
Christoph Hellwig1be56902017-06-03 09:38:03 +02001304 return DM_ENDIO_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001307static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001310 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Mikulas Patocka04788502009-12-10 23:52:03 +00001312 struct bio_list holds;
1313 struct bio *bio;
1314
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001315 atomic_set(&ms->suspend, 1);
1316
1317 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001318 * Process bios in the hold list to start recovery waiting
1319 * for bios in the hold list. After the process, no bio has
1320 * a chance to be added in the hold list because ms->suspend
1321 * is set.
1322 */
1323 spin_lock_irq(&ms->lock);
1324 holds = ms->holds;
1325 bio_list_init(&ms->holds);
1326 spin_unlock_irq(&ms->lock);
1327
1328 while ((bio = bio_list_pop(&holds)))
1329 hold_bio(ms, bio);
1330
1331 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001332 * We must finish up all the work that we've
1333 * generated (i.e. recovery work).
1334 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001335 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001336
Jonathan E Brassow33184042006-11-08 17:44:44 -08001337 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001338 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001339
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001340 if (log->type->presuspend && log->type->presuspend(log))
1341 /* FIXME: need better error handling */
1342 DMWARN("log presuspend failed");
1343
1344 /*
1345 * Now that recovery is complete/stopped and the
1346 * delayed bios are queued, we need to wait for
1347 * the worker thread to complete. This way,
1348 * we know that all of our I/O has been pushed.
1349 */
1350 flush_workqueue(ms->kmirrord_wq);
1351}
1352
1353static void mirror_postsuspend(struct dm_target *ti)
1354{
1355 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001356 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001357
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001358 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001360 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363static void mirror_resume(struct dm_target *ti)
1364{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001365 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001366 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001367
1368 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (log->type->resume && log->type->resume(log))
1370 /* FIXME: need better error handling */
1371 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001372 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001375/*
1376 * device_status_char
1377 * @m: mirror device/leg we want the status of
1378 *
1379 * We return one character representing the most severe error
1380 * we have encountered.
1381 * A => Alive - No failures
1382 * D => Dead - A write failure occurred leaving mirror out-of-sync
1383 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1384 * R => Read - A read failure occurred, mirror data unaffected
1385 *
1386 * Returns: <char>
1387 */
1388static char device_status_char(struct mirror *m)
1389{
1390 if (!atomic_read(&(m->error_count)))
1391 return 'A';
1392
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001393 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1394 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001395 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1396 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1397}
1398
1399
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001400static void mirror_status(struct dm_target *ti, status_type_t type,
1401 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001403 unsigned int m, sz = 0;
Lidong Zhonged632872015-05-13 14:04:10 +08001404 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001406 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001407 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 switch (type) {
1410 case STATUSTYPE_INFO:
1411 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001412 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001414 buffer[m] = device_status_char(&(ms->mirror[m]));
1415 }
1416 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001418 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001419 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001420 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001421
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001422 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 break;
1425
1426 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001427 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001428
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001429 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001431 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001432 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001433
Lidong Zhonged632872015-05-13 14:04:10 +08001434 num_feature_args += !!errors_handled(ms);
1435 num_feature_args += !!keep_log(ms);
1436 if (num_feature_args) {
1437 DMEMIT(" %d", num_feature_args);
1438 if (errors_handled(ms))
1439 DMEMIT(" handle_errors");
1440 if (keep_log(ms))
1441 DMEMIT(" keep_log");
1442 }
1443
1444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001448static int mirror_iterate_devices(struct dm_target *ti,
1449 iterate_devices_callout_fn fn, void *data)
1450{
1451 struct mirror_set *ms = ti->private;
1452 int ret = 0;
1453 unsigned i;
1454
1455 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1456 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001457 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001458
1459 return ret;
1460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static struct target_type mirror_target = {
1463 .name = "mirror",
Lidong Zhonged632872015-05-13 14:04:10 +08001464 .version = {1, 14, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 .module = THIS_MODULE,
1466 .ctr = mirror_ctr,
1467 .dtr = mirror_dtr,
1468 .map = mirror_map,
1469 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001470 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 .postsuspend = mirror_postsuspend,
1472 .resume = mirror_resume,
1473 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001474 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475};
1476
1477static int __init dm_mirror_init(void)
1478{
1479 int r;
1480
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001481 r = dm_register_target(&mirror_target);
1482 if (r < 0) {
1483 DMERR("Failed to register mirror target");
1484 goto bad_target;
1485 }
1486
1487 return 0;
1488
1489bad_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return r;
1491}
1492
1493static void __exit dm_mirror_exit(void)
1494{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001495 dm_unregister_target(&mirror_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498/* Module hooks */
1499module_init(dm_mirror_init);
1500module_exit(dm_mirror_exit);
1501
1502MODULE_DESCRIPTION(DM_NAME " mirror target");
1503MODULE_AUTHOR("Joe Thornber");
1504MODULE_LICENSE("GPL");