blob: 8811d484fdd14c77cd97fb567104b3fa9d83f496 [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
Kees Cook65972a62018-04-10 21:43:15 -070026#define MAX_NR_MIRRORS (DM_KCOPYD_MAX_REGIONS + 1)
27
Lidong Zhonged632872015-05-13 14:04:10 +080028#define DM_RAID1_HANDLE_ERRORS 0x01
29#define DM_RAID1_KEEP_LOG 0x02
Jonathan Brassowf44db672007-07-12 17:29:04 +010030#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Lidong Zhonged632872015-05-13 14:04:10 +080031#define keep_log(p) ((p)->features & DM_RAID1_KEEP_LOG)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070032
Jonathan E Brassow33184042006-11-08 17:44:44 -080033static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070036 * Mirror set structures.
37 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000038enum dm_raid1_error {
39 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000040 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000041 DM_RAID1_SYNC_ERROR,
42 DM_RAID1_READ_ERROR
43};
44
Neil Browne4c8b3b2006-06-26 00:27:26 -070045struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010046 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070047 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000048 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070049 struct dm_dev *dev;
50 sector_t offset;
51};
52
53struct mirror_set {
54 struct dm_target *ti;
55 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010056
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070057 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070058
Jonathan Brassow72f4b312008-02-08 02:11:29 +000059 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070060 struct bio_list reads;
61 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000062 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000063 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070064
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010065 struct dm_region_hash *rh;
66 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070067 struct dm_io_client *io_client;
68
Neil Browne4c8b3b2006-06-26 00:27:26 -070069 /* recovery */
70 region_t nr_regions;
71 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010072 int log_failure;
Mikulas Patocka929be8f2009-12-10 23:52:06 +000073 int leg_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000074 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070075
Jonathan Brassow72f4b312008-02-08 02:11:29 +000076 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070077
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070078 struct workqueue_struct *kmirrord_wq;
79 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010080 struct timer_list timer;
81 unsigned long timer_pending;
82
Jonathan Brassow72f4b312008-02-08 02:11:29 +000083 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070084
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010085 unsigned nr_mirrors;
Gustavo A. R. Silvab18ae8d2020-05-07 13:51:58 -050086 struct mirror mirror[];
Neil Browne4c8b3b2006-06-26 00:27:26 -070087};
88
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000089DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
90 "A percentage of time allocated for raid resynchronization");
91
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010092static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010094 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070096 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
97}
98
Kees Cook8376d3c2017-10-16 17:01:48 -070099static void delayed_wake_fn(struct timer_list *t)
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100100{
Kees Cook8376d3c2017-10-16 17:01:48 -0700101 struct mirror_set *ms = from_timer(ms, t, timer);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100102
103 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100104 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100105}
106
107static void delayed_wake(struct mirror_set *ms)
108{
109 if (test_and_set_bit(0, &ms->timer_pending))
110 return;
111
112 ms->timer.expires = jiffies + HZ / 5;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100113 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;
Christoph Hellwig309dca302021-01-24 11:02:34 +0100148 /* 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;
Kees Cook65972a62018-04-10 21:43:15 -0700260 struct dm_io_region io[MAX_NR_MIRRORS];
Mikulas Patockac0da3742009-12-10 23:52:02 +0000261 struct mirror *m;
262 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500263 .bi_op = REQ_OP_WRITE,
Jan Karaff0361b2017-05-31 09:44:32 +0200264 .bi_op_flags = REQ_PREFLUSH | REQ_SYNC,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000265 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000266 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000267 .client = ms->io_client,
268 };
269
270 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
271 io[i].bdev = m->dev->bdev;
272 io[i].sector = 0;
273 io[i].count = 0;
274 }
275
276 error_bits = -1;
277 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
278 if (unlikely(error_bits != 0)) {
279 for (i = 0; i < ms->nr_mirrors; i++)
280 if (test_bit(i, &error_bits))
281 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000282 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000283 return -EIO;
284 }
285
286 return 0;
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*-----------------------------------------------------------------
290 * Recovery.
291 *
292 * When a mirror is first activated we may find that some regions
293 * are in the no-sync state. We have to recover these by
294 * recopying from the default mirror to all the others.
295 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700296static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 void *context)
298{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100299 struct dm_region *reg = context;
300 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000301 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000303 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100304 /* Read error means the failure of default mirror. */
305 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000306 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
307 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100308
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000309 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700310 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100311 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000312 /*
313 * Bits correspond to devices (excluding default mirror).
314 * The default mirror cannot change during recovery.
315 */
316 for (m = 0; m < ms->nr_mirrors; m++) {
317 if (&ms->mirror[m] == get_default_mirror(ms))
318 continue;
319 if (test_bit(bit, &write_err))
320 fail_mirror(ms->mirror + m,
321 DM_RAID1_SYNC_ERROR);
322 bit++;
323 }
324 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100325
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100326 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Mike Snitzer7209049d2018-07-31 17:27:02 -0400329static void recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
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))
Mikulas Patockadb2351e2021-05-26 10:16:01 -0400367 flags |= BIT(DM_KCOPYD_IGNORE_ERROR);
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100368
Mike Snitzer7209049d2018-07-31 17:27:02 -0400369 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
Lidong Zhonged632872015-05-13 14:04:10 +0800373static void reset_ms_flags(struct mirror_set *ms)
374{
375 unsigned int m;
376
377 ms->leg_failure = 0;
378 for (m = 0; m < ms->nr_mirrors; m++) {
379 atomic_set(&(ms->mirror[m].error_count), 0);
380 ms->mirror[m].error_type = 0;
381 }
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void do_recovery(struct mirror_set *ms)
385{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100386 struct dm_region *reg;
387 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /*
390 * Start quiescing some regions.
391 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100392 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
395 * Copy any already quiesced regions.
396 */
Mike Snitzer7209049d2018-07-31 17:27:02 -0400397 while ((reg = dm_rh_recovery_start(ms->rh)))
398 recover(ms, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Update the in sync flag.
402 */
403 if (!ms->in_sync &&
404 (log->type->get_sync_count(log) == ms->nr_regions)) {
405 /* the sync is complete */
406 dm_table_event(ms->ti->table);
407 ms->in_sync = 1;
Lidong Zhonged632872015-05-13 14:04:10 +0800408 reset_ms_flags(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410}
411
412/*-----------------------------------------------------------------
413 * Reads
414 *---------------------------------------------------------------*/
415static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
416{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000417 struct mirror *m = get_default_mirror(ms);
418
419 do {
420 if (likely(!atomic_read(&m->error_count)))
421 return m;
422
423 if (m-- == ms->mirror)
424 m += ms->nr_mirrors;
425 } while (m != get_default_mirror(ms));
426
427 return NULL;
428}
429
430static int default_ok(struct mirror *m)
431{
432 struct mirror *default_mirror = get_default_mirror(m->ms);
433
434 return !atomic_read(&default_mirror->error_count);
435}
436
437static int mirror_available(struct mirror_set *ms, struct bio *bio)
438{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100439 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
440 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000441
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100442 if (log->type->in_sync(log, region, 0))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700443 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000444
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * remap a buffer to a particular mirror.
450 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000451static sector_t map_sector(struct mirror *m, struct bio *bio)
452{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700453 if (unlikely(!bio->bi_iter.bi_size))
Mikulas Patocka41841532009-12-10 23:51:59 +0000454 return 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700455 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000456}
457
458static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200460 bio_set_dev(bio, m->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700461 bio->bi_iter.bi_sector = map_sector(m, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000462}
463
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100464static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000465 struct bio *bio)
466{
467 io->bdev = m->dev->bdev;
468 io->sector = map_sector(m, bio);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800469 io->count = bio_sectors(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000470}
471
Mikulas Patocka04788502009-12-10 23:52:03 +0000472static void hold_bio(struct mirror_set *ms, struct bio *bio)
473{
474 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000475 * Lock is required to avoid race condition during suspend
476 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000477 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000478 spin_lock_irq(&ms->lock);
479
Mikulas Patocka04788502009-12-10 23:52:03 +0000480 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000481 spin_unlock_irq(&ms->lock);
482
483 /*
484 * If device is suspended, complete the bio.
485 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000486 if (dm_noflush_suspending(ms->ti))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200487 bio->bi_status = BLK_STS_DM_REQUEUE;
Mikulas Patocka04788502009-12-10 23:52:03 +0000488 else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200489 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200490
491 bio_endio(bio);
Mikulas Patocka04788502009-12-10 23:52:03 +0000492 return;
493 }
494
495 /*
496 * Hold bio until the suspend is complete.
497 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000498 bio_list_add(&ms->holds, bio);
499 spin_unlock_irq(&ms->lock);
500}
501
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000502/*-----------------------------------------------------------------
503 * Reads
504 *---------------------------------------------------------------*/
505static void read_callback(unsigned long error, void *context)
506{
507 struct bio *bio = context;
508 struct mirror *m;
509
510 m = bio_get_m(bio);
511 bio_set_m(bio, NULL);
512
513 if (likely(!error)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200514 bio_endio(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000515 return;
516 }
517
518 fail_mirror(m, DM_RAID1_READ_ERROR);
519
520 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
521 DMWARN_LIMIT("Read failure on mirror device %s. "
522 "Trying alternative device.",
523 m->dev->name);
Christoph Hellwig70246282016-07-19 11:28:41 +0200524 queue_bio(m->ms, bio, bio_data_dir(bio));
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000525 return;
526 }
527
528 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
529 m->dev->name);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200530 bio_io_error(bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000531}
532
533/* Asynchronous read. */
534static void read_async_bio(struct mirror *m, struct bio *bio)
535{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100536 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000537 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500538 .bi_op = REQ_OP_READ,
539 .bi_op_flags = 0,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700540 .mem.type = DM_IO_BIO,
541 .mem.ptr.bio = bio,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000542 .notify.fn = read_callback,
543 .notify.context = bio,
544 .client = m->ms->io_client,
545 };
546
547 map_region(&io, m, bio);
548 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100549 BUG_ON(dm_io(&io_req, 1, &io, NULL));
550}
551
552static inline int region_in_sync(struct mirror_set *ms, region_t region,
553 int may_block)
554{
555 int state = dm_rh_get_state(ms->rh, region, may_block);
556 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static void do_reads(struct mirror_set *ms, struct bio_list *reads)
560{
561 region_t region;
562 struct bio *bio;
563 struct mirror *m;
564
565 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100566 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000567 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /*
570 * We can only read balance if the region is in sync.
571 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100572 if (likely(region_in_sync(ms, region, 1)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700573 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000574 else if (m && atomic_read(&m->error_count))
575 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000577 if (likely(m))
578 read_async_bio(m, bio);
579 else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200580 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582}
583
584/*-----------------------------------------------------------------
585 * Writes.
586 *
587 * We do different things with the write io depending on the
588 * state of the region that it's in:
589 *
590 * SYNC: increment pending, use kcopyd to write to *all* mirrors
591 * RECOVERING: delay the io until recovery completes
592 * NOSYNC: increment pending, just write to the default mirror
593 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000594
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596static void write_callback(unsigned long error, void *context)
597{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200598 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct bio *bio = (struct bio *) context;
600 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000601 int should_wake = 0;
602 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000604 ms = bio_get_m(bio)->ms;
605 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /*
608 * NOTE: We don't decrement the pending count here,
609 * instead it is done by the targets endio function.
610 * This way we handle both writes to SYNC and NOSYNC
611 * regions with the same code.
612 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000613 if (likely(!error)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200614 bio_endio(bio);
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000615 return;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Mikulas Patockaf2ed51a2015-02-12 10:09:20 -0500618 /*
619 * If the bio is discard, return an error, but do not
620 * degrade the array.
621 */
Mike Christiee6047142016-06-05 14:32:04 -0500622 if (bio_op(bio) == REQ_OP_DISCARD) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200623 bio->bi_status = BLK_STS_NOTSUPP;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200624 bio_endio(bio);
Mikulas Patockaf2ed51a2015-02-12 10:09:20 -0500625 return;
626 }
627
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000628 for (i = 0; i < ms->nr_mirrors; i++)
629 if (test_bit(i, &error))
630 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000631
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000632 /*
633 * Need to raise event. Since raising
634 * events can block, we need to do it in
635 * the main thread.
636 */
637 spin_lock_irqsave(&ms->lock, flags);
638 if (!ms->failures.head)
639 should_wake = 1;
640 bio_list_add(&ms->failures, bio);
641 spin_unlock_irqrestore(&ms->lock, flags);
642 if (should_wake)
643 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646static void do_write(struct mirror_set *ms, struct bio *bio)
647{
648 unsigned int i;
Kees Cook65972a62018-04-10 21:43:15 -0700649 struct dm_io_region io[MAX_NR_MIRRORS], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700651 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500652 .bi_op = REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600653 .bi_op_flags = bio->bi_opf & (REQ_FUA | REQ_PREFLUSH),
Kent Overstreet003b5c52013-10-11 15:45:43 -0700654 .mem.type = DM_IO_BIO,
655 .mem.ptr.bio = bio,
Milan Broz88be1632007-05-09 02:33:04 -0700656 .notify.fn = write_callback,
657 .notify.context = bio,
658 .client = ms->io_client,
659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Mike Christiee6047142016-06-05 14:32:04 -0500661 if (bio_op(bio) == REQ_OP_DISCARD) {
662 io_req.bi_op = REQ_OP_DISCARD;
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000663 io_req.mem.type = DM_IO_KMEM;
664 io_req.mem.ptr.addr = NULL;
665 }
666
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000667 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
668 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000670 /*
671 * Use default mirror because we only need it to retrieve the reference
672 * to the mirror set in write_callback().
673 */
674 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700675
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100676 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static void do_writes(struct mirror_set *ms, struct bio_list *writes)
680{
681 int state;
682 struct bio *bio;
683 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100684 struct bio_list requeue;
685 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
686 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (!writes->head)
689 return;
690
691 /*
692 * Classify each write.
693 */
694 bio_list_init(&sync);
695 bio_list_init(&nosync);
696 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100697 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 while ((bio = bio_list_pop(writes))) {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600700 if ((bio->bi_opf & REQ_PREFLUSH) ||
Mike Christiee6047142016-06-05 14:32:04 -0500701 (bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000702 bio_list_add(&sync, bio);
703 continue;
704 }
705
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100706 region = dm_rh_bio_to_region(ms->rh, bio);
707
708 if (log->type->is_remote_recovering &&
709 log->type->is_remote_recovering(log, region)) {
710 bio_list_add(&requeue, bio);
711 continue;
712 }
713
714 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100716 case DM_RH_CLEAN:
717 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 this_list = &sync;
719 break;
720
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100721 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 this_list = &nosync;
723 break;
724
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100725 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 this_list = &recover;
727 break;
728 }
729
730 bio_list_add(this_list, bio);
731 }
732
733 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100734 * Add bios that are delayed due to remote recovery
735 * back on to the write queue
736 */
737 if (unlikely(requeue.head)) {
738 spin_lock_irq(&ms->lock);
739 bio_list_merge(&ms->writes, &requeue);
740 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100741 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100742 }
743
744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * Increment the pending counts for any regions that will
746 * be written to (writes to recover regions are going to
747 * be delayed).
748 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100749 dm_rh_inc_pending(ms->rh, &sync);
750 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100751
752 /*
753 * If the flush fails on a previous call and succeeds here,
754 * we must not reset the log_failure variable. We need
755 * userspace interaction to do that.
756 */
757 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * Dispatch io.
761 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000762 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000763 spin_lock_irq(&ms->lock);
764 bio_list_merge(&ms->failures, &sync);
765 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100766 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000767 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100768 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000769 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100772 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 while ((bio = bio_list_pop(&nosync))) {
Lidong Zhonged632872015-05-13 14:04:10 +0800775 if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000776 spin_lock_irq(&ms->lock);
777 bio_list_add(&ms->failures, bio);
778 spin_unlock_irq(&ms->lock);
779 wakeup_mirrord(ms);
780 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000781 map_bio(get_default_mirror(ms), bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200782 submit_bio_noacct(bio);
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785}
786
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000787static void do_failures(struct mirror_set *ms, struct bio_list *failures)
788{
789 struct bio *bio;
790
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000791 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000792 return;
793
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000794 /*
795 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000796 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000797 * until a log has been marked, so we must store them.
798 *
799 * If a 'noflush' suspend is in progress, we can requeue
800 * the I/O's to the core. This give userspace a chance
801 * to reconfigure the mirror, at which point the core
802 * will reissue the writes. If the 'noflush' flag is
803 * not set, we have no choice but to return errors.
804 *
805 * Some writes on the failures list may have been
806 * submitted before the log failure and represent a
807 * failure to write to one of the devices. It is ok
808 * for us to treat them the same and requeue them
809 * as well.
810 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000811 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000812 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000813 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000814 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000815 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000816
817 /*
818 * If all the legs are dead, fail the I/O.
Lidong Zhonged632872015-05-13 14:04:10 +0800819 * If the device has failed and keep_log is enabled,
820 * fail the I/O.
821 *
822 * If we have been told to handle errors, and keep_log
823 * isn't enabled, hold the bio and wait for userspace to
824 * deal with the problem.
825 *
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000826 * Otherwise pretend that the I/O succeeded. (This would
827 * be wrong if the failed leg returned after reboot and
828 * got replicated back to the good legs.)
829 */
Lidong Zhonged632872015-05-13 14:04:10 +0800830 if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200831 bio_io_error(bio);
Lidong Zhonged632872015-05-13 14:04:10 +0800832 else if (errors_handled(ms) && !keep_log(ms))
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000833 hold_bio(ms, bio);
834 else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200835 bio_endio(bio);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000836 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000837}
838
839static void trigger_event(struct work_struct *work)
840{
841 struct mirror_set *ms =
842 container_of(work, struct mirror_set, trigger_event);
843
844 dm_table_event(ms->ti->table);
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*-----------------------------------------------------------------
848 * kmirrord
849 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100850static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100852 struct mirror_set *ms = container_of(work, struct mirror_set,
853 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000854 struct bio_list reads, writes, failures;
855 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000857 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 reads = ms->reads;
859 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000860 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 bio_list_init(&ms->reads);
862 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000863 bio_list_init(&ms->failures);
864 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100866 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 do_recovery(ms);
868 do_reads(ms, &reads);
869 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000870 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*-----------------------------------------------------------------
874 * Target functions
875 *---------------------------------------------------------------*/
876static struct mirror_set *alloc_context(unsigned int nr_mirrors,
877 uint32_t region_size,
878 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100879 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Gustavo A. R. Silvabcd67652019-08-05 19:18:25 -0500881 struct mirror_set *ms =
882 kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700885 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return NULL;
887 }
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000890 bio_list_init(&ms->reads);
891 bio_list_init(&ms->writes);
892 bio_list_init(&ms->failures);
893 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 ms->ti = ti;
896 ms->nr_mirrors = nr_mirrors;
897 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
898 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000899 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000900 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000901 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000902 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100904 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700905 if (IS_ERR(ms->io_client)) {
906 ti->error = "Error creating dm_io client";
907 kfree(ms);
908 return NULL;
909 }
910
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100911 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
912 wakeup_all_recovery_waiters,
913 ms->ti->begin, MAX_RECOVERY,
914 dl, region_size, ms->nr_regions);
915 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700916 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100917 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 kfree(ms);
919 return NULL;
920 }
921
922 return ms;
923}
924
925static void free_context(struct mirror_set *ms, struct dm_target *ti,
926 unsigned int m)
927{
928 while (m--)
929 dm_put_device(ti, ms->mirror[m].dev);
930
Milan Broz88be1632007-05-09 02:33:04 -0700931 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100932 dm_region_hash_destroy(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 kfree(ms);
934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
937 unsigned int mirror, char **argv)
938{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800939 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100940 char dummy;
Vivek Goyale80d1c82015-07-31 09:20:36 -0400941 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Milan Brozef87bfc2018-11-07 22:24:55 +0100943 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
944 offset != (sector_t)offset) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700945 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return -EINVAL;
947 }
948
Vivek Goyale80d1c82015-07-31 09:20:36 -0400949 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
950 &ms->mirror[mirror].dev);
951 if (ret) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700952 ti->error = "Device lookup failure";
Vivek Goyale80d1c82015-07-31 09:20:36 -0400953 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100956 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000957 atomic_set(&(ms->mirror[mirror].error_count), 0);
958 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 ms->mirror[mirror].offset = offset;
960
961 return 0;
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * Create dirty log: log_type #log_params <log_params>
966 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100967static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100968 unsigned argc, char **argv,
969 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100971 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100972 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100973 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700976 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return NULL;
978 }
979
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100980 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700981 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return NULL;
983 }
984
985 *args_used = 2 + param_count;
986
987 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700988 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return NULL;
990 }
991
Mikulas Patockac0da3742009-12-10 23:52:02 +0000992 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
993 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700995 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return NULL;
997 }
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return dl;
1000}
1001
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001002static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1003 unsigned *args_used)
1004{
1005 unsigned num_features;
1006 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001007 char dummy;
Lidong Zhonged632872015-05-13 14:04:10 +08001008 int i;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001009
1010 *args_used = 0;
1011
1012 if (!argc)
1013 return 0;
1014
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001015 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001016 ti->error = "Invalid number of features";
1017 return -EINVAL;
1018 }
1019
1020 argc--;
1021 argv++;
1022 (*args_used)++;
1023
1024 if (num_features > argc) {
1025 ti->error = "Not enough arguments to support feature count";
1026 return -EINVAL;
1027 }
1028
Lidong Zhonged632872015-05-13 14:04:10 +08001029 for (i = 0; i < num_features; i++) {
1030 if (!strcmp("handle_errors", argv[0]))
1031 ms->features |= DM_RAID1_HANDLE_ERRORS;
1032 else if (!strcmp("keep_log", argv[0]))
1033 ms->features |= DM_RAID1_KEEP_LOG;
1034 else {
1035 ti->error = "Unrecognised feature requested";
1036 return -EINVAL;
1037 }
1038
1039 argc--;
1040 argv++;
1041 (*args_used)++;
1042 }
1043 if (!errors_handled(ms) && keep_log(ms)) {
1044 ti->error = "keep_log feature requires the handle_errors feature";
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001045 return -EINVAL;
1046 }
1047
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001048 return 0;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * Construct a mirror mapping:
1053 *
1054 * log_type #log_params <log_params>
1055 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001056 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 *
1058 * log_type is "core" or "disk"
1059 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001060 *
Lidong Zhonged632872015-05-13 14:04:10 +08001061 * If present, supported features are "handle_errors" and "keep_log".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1064{
1065 int r;
1066 unsigned int nr_mirrors, m, args_used;
1067 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001068 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001069 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 dl = create_dirty_log(ti, argc, argv, &args_used);
1072 if (!dl)
1073 return -EINVAL;
1074
1075 argv += args_used;
1076 argc -= args_used;
1077
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001078 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Kees Cook65972a62018-04-10 21:43:15 -07001079 nr_mirrors < 2 || nr_mirrors > MAX_NR_MIRRORS) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001080 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001081 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EINVAL;
1083 }
1084
1085 argv++, argc--;
1086
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001087 if (argc < nr_mirrors * 2) {
1088 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001089 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -EINVAL;
1091 }
1092
1093 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1094 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001095 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return -ENOMEM;
1097 }
1098
1099 /* Get the mirror parameter sets */
1100 for (m = 0; m < nr_mirrors; m++) {
1101 r = get_mirror(ms, ti, m, argv);
1102 if (r) {
1103 free_context(ms, ti, m);
1104 return r;
1105 }
1106 argv += 2;
1107 argc -= 2;
1108 }
1109
1110 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001111
1112 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1113 if (r)
1114 goto err_free_context;
1115
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001116 ti->num_flush_bios = 1;
1117 ti->num_discard_bios = 1;
Mike Snitzer30187e12016-01-31 13:28:26 -05001118 ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Tejun Heo670368a2013-07-30 08:40:21 -04001120 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001121 if (!ms->kmirrord_wq) {
1122 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001123 r = -ENOMEM;
1124 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001125 }
1126 INIT_WORK(&ms->kmirrord_work, do_mirror);
Kees Cook8376d3c2017-10-16 17:01:48 -07001127 timer_setup(&ms->timer, delayed_wake_fn, 0);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001128 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001129 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001130
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001131 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001132 if (r)
1133 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001134
1135 argv += args_used;
1136 argc -= args_used;
1137
Jonathan Brassowf44db672007-07-12 17:29:04 +01001138 /*
1139 * Any read-balancing addition depends on the
1140 * DM_RAID1_HANDLE_ERRORS flag being present.
1141 * This is because the decision to balance depends
1142 * on the sync state of a region. If the above
1143 * flag is not present, we ignore errors; and
1144 * the sync state may be inaccurate.
1145 */
1146
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001147 if (argc) {
1148 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001149 r = -EINVAL;
1150 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001151 }
1152
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001153 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001154 if (IS_ERR(ms->kcopyd_client)) {
1155 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001156 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001159 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001161
1162err_destroy_wq:
1163 destroy_workqueue(ms->kmirrord_wq);
1164err_free_context:
1165 free_context(ms, ti, ms->nr_mirrors);
1166 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169static void mirror_dtr(struct dm_target *ti)
1170{
1171 struct mirror_set *ms = (struct mirror_set *) ti->private;
1172
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001173 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001174 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001175 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001176 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001177 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 free_context(ms, ti, ms->nr_mirrors);
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/*
1182 * Mirror mapping function
1183 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001184static int mirror_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Christoph Hellwig70246282016-07-19 11:28:41 +02001186 int r, rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct mirror *m;
1188 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001189 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Mikulas Patocka0045d612012-12-21 20:23:40 +00001190 struct dm_raid1_bio_record *bio_record =
1191 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1192
Christoph Hellwig309dca302021-01-24 11:02:34 +01001193 bio_record->details.bi_bdev = NULL;
Mike Snitzercd15fb62017-06-15 08:39:15 -04001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001196 /* Save region for mirror_end_io() handler */
Mikulas Patocka0045d612012-12-21 20:23:40 +00001197 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001199 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001202 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (r < 0 && r != -EWOULDBLOCK)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001204 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001207 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001209 if (!r || (r == -EWOULDBLOCK)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001210 if (bio->bi_opf & REQ_RAHEAD)
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 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001214 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001217 /*
1218 * The region is in-sync and we can perform reads directly.
1219 * Store enough information so we can retry if it fails.
1220 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001221 m = choose_mirror(ms, bio->bi_iter.bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001222 if (unlikely(!m))
Christoph Hellwig846785e2017-06-03 09:38:02 +02001223 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001225 dm_bio_record(&bio_record->details, bio);
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001226 bio_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001227
1228 map_bio(m, bio);
1229
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001230 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001233static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1234 blk_status_t *error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Christoph Hellwig70246282016-07-19 11:28:41 +02001236 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001238 struct mirror *m = NULL;
1239 struct dm_bio_details *bd = NULL;
Mikulas Patocka0045d612012-12-21 20:23:40 +00001240 struct dm_raid1_bio_record *bio_record =
1241 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /*
1244 * We need to dec pending if this was a write.
1245 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001246 if (rw == WRITE) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001247 if (!(bio->bi_opf & REQ_PREFLUSH) &&
Mike Christie28a8f0d2016-06-05 14:32:25 -05001248 bio_op(bio) != REQ_OP_DISCARD)
Mikulas Patocka0045d612012-12-21 20:23:40 +00001249 dm_rh_dec(ms->rh, bio_record->write_region);
Christoph Hellwig1be56902017-06-03 09:38:03 +02001250 return DM_ENDIO_DONE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001253 if (*error == BLK_STS_NOTSUPP)
Mike Snitzercd15fb62017-06-15 08:39:15 -04001254 goto out;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001255
Christoph Hellwig9966afa2017-06-03 09:37:57 +02001256 if (bio->bi_opf & REQ_RAHEAD)
Mike Snitzercd15fb62017-06-15 08:39:15 -04001257 goto out;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001258
Christoph Hellwig1be56902017-06-03 09:38:03 +02001259 if (unlikely(*error)) {
Christoph Hellwig309dca302021-01-24 11:02:34 +01001260 if (!bio_record->details.bi_bdev) {
Mike Snitzercd15fb62017-06-15 08:39:15 -04001261 /*
1262 * There wasn't enough memory to record necessary
1263 * information for a retry or there was no other
1264 * mirror in-sync.
1265 */
1266 DMERR_LIMIT("Mirror read failed.");
Linus Torvaldsc6b1e362017-07-03 10:34:51 -07001267 return DM_ENDIO_DONE;
Mike Snitzercd15fb62017-06-15 08:39:15 -04001268 }
1269
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001270 m = bio_record->m;
Adrian Bunke03f1a82008-02-19 19:44:19 +00001271
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001272 DMERR("Mirror read failed from %s. Trying alternative device.",
1273 m->dev->name);
1274
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001275 fail_mirror(m, DM_RAID1_READ_ERROR);
1276
1277 /*
1278 * A failed read is requeued for another attempt using an intact
1279 * mirror.
1280 */
1281 if (default_ok(m) || mirror_available(ms, bio)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001282 bd = &bio_record->details;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001283
1284 dm_bio_restore(bd, bio);
Christoph Hellwig309dca302021-01-24 11:02:34 +01001285 bio_record->details.bi_bdev = NULL;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001286 bio->bi_status = 0;
Mikulas Patockaf3a44fe2014-02-18 09:57:22 -05001287
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001288 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001289 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001290 }
1291 DMERR("All replicated volumes dead, failing I/O");
1292 }
1293
Mike Snitzercd15fb62017-06-15 08:39:15 -04001294out:
Christoph Hellwig309dca302021-01-24 11:02:34 +01001295 bio_record->details.bi_bdev = NULL;
Mike Snitzercd15fb62017-06-15 08:39:15 -04001296
Christoph Hellwig1be56902017-06-03 09:38:03 +02001297 return DM_ENDIO_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001300static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001303 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Mikulas Patocka04788502009-12-10 23:52:03 +00001305 struct bio_list holds;
1306 struct bio *bio;
1307
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001308 atomic_set(&ms->suspend, 1);
1309
1310 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001311 * Process bios in the hold list to start recovery waiting
1312 * for bios in the hold list. After the process, no bio has
1313 * a chance to be added in the hold list because ms->suspend
1314 * is set.
1315 */
1316 spin_lock_irq(&ms->lock);
1317 holds = ms->holds;
1318 bio_list_init(&ms->holds);
1319 spin_unlock_irq(&ms->lock);
1320
1321 while ((bio = bio_list_pop(&holds)))
1322 hold_bio(ms, bio);
1323
1324 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001325 * We must finish up all the work that we've
1326 * generated (i.e. recovery work).
1327 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001328 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001329
Jonathan E Brassow33184042006-11-08 17:44:44 -08001330 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001331 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001332
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001333 if (log->type->presuspend && log->type->presuspend(log))
1334 /* FIXME: need better error handling */
1335 DMWARN("log presuspend failed");
1336
1337 /*
1338 * Now that recovery is complete/stopped and the
1339 * delayed bios are queued, we need to wait for
1340 * the worker thread to complete. This way,
1341 * we know that all of our I/O has been pushed.
1342 */
1343 flush_workqueue(ms->kmirrord_wq);
1344}
1345
1346static void mirror_postsuspend(struct dm_target *ti)
1347{
1348 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001349 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001350
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001351 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001353 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static void mirror_resume(struct dm_target *ti)
1357{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001358 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001359 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001360
1361 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (log->type->resume && log->type->resume(log))
1363 /* FIXME: need better error handling */
1364 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001365 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001368/*
1369 * device_status_char
1370 * @m: mirror device/leg we want the status of
1371 *
1372 * We return one character representing the most severe error
1373 * we have encountered.
1374 * A => Alive - No failures
1375 * D => Dead - A write failure occurred leaving mirror out-of-sync
1376 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1377 * R => Read - A read failure occurred, mirror data unaffected
1378 *
1379 * Returns: <char>
1380 */
1381static char device_status_char(struct mirror *m)
1382{
1383 if (!atomic_read(&(m->error_count)))
1384 return 'A';
1385
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001386 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1387 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001388 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1389 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1390}
1391
1392
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001393static void mirror_status(struct dm_target *ti, status_type_t type,
1394 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001396 unsigned int m, sz = 0;
Lidong Zhonged632872015-05-13 14:04:10 +08001397 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001399 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Kees Cook65972a62018-04-10 21:43:15 -07001400 char buffer[MAX_NR_MIRRORS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 switch (type) {
1403 case STATUSTYPE_INFO:
1404 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001405 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001407 buffer[m] = device_status_char(&(ms->mirror[m]));
1408 }
1409 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001411 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001412 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001413 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001414
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001415 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418
1419 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001420 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001421
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001422 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001424 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001425 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001426
Lidong Zhonged632872015-05-13 14:04:10 +08001427 num_feature_args += !!errors_handled(ms);
1428 num_feature_args += !!keep_log(ms);
1429 if (num_feature_args) {
1430 DMEMIT(" %d", num_feature_args);
1431 if (errors_handled(ms))
1432 DMEMIT(" handle_errors");
1433 if (keep_log(ms))
1434 DMEMIT(" keep_log");
1435 }
1436
1437 break;
Tushar Sugandhi8ec45662021-07-12 17:49:03 -07001438
1439 case STATUSTYPE_IMA:
1440 DMEMIT_TARGET_NAME_VERSION(ti->type);
1441 DMEMIT(",nr_mirrors=%d", ms->nr_mirrors);
1442 for (m = 0; m < ms->nr_mirrors; m++) {
1443 DMEMIT(",mirror_device_%d=%s", m, ms->mirror[m].dev->name);
1444 DMEMIT(",mirror_device_%d_status=%c",
1445 m, device_status_char(&(ms->mirror[m])));
1446 }
1447
1448 DMEMIT(",handle_errors=%c", errors_handled(ms) ? 'y' : 'n');
1449 DMEMIT(",keep_log=%c", keep_log(ms) ? 'y' : 'n');
1450
1451 DMEMIT(",log_type_status=");
1452 sz += log->type->status(log, type, result+sz, maxlen-sz);
1453 DMEMIT(";");
1454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001458static int mirror_iterate_devices(struct dm_target *ti,
1459 iterate_devices_callout_fn fn, void *data)
1460{
1461 struct mirror_set *ms = ti->private;
1462 int ret = 0;
1463 unsigned i;
1464
1465 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1466 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001467 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001468
1469 return ret;
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static struct target_type mirror_target = {
1473 .name = "mirror",
Lidong Zhonged632872015-05-13 14:04:10 +08001474 .version = {1, 14, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 .module = THIS_MODULE,
1476 .ctr = mirror_ctr,
1477 .dtr = mirror_dtr,
1478 .map = mirror_map,
1479 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001480 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .postsuspend = mirror_postsuspend,
1482 .resume = mirror_resume,
1483 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001484 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485};
1486
1487static int __init dm_mirror_init(void)
1488{
1489 int r;
1490
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001491 r = dm_register_target(&mirror_target);
1492 if (r < 0) {
1493 DMERR("Failed to register mirror target");
1494 goto bad_target;
1495 }
1496
1497 return 0;
1498
1499bad_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return r;
1501}
1502
1503static void __exit dm_mirror_exit(void)
1504{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001505 dm_unregister_target(&mirror_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508/* Module hooks */
1509module_init(dm_mirror_init);
1510module_exit(dm_mirror_exit);
1511
1512MODULE_DESCRIPTION(DM_NAME " mirror target");
1513MODULE_AUTHOR("Joe Thornber");
1514MODULE_LICENSE("GPL");