blob: cec5f9b3a27817c7d5845b7ce02a69579b6c8a68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01003 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Jonathan Brassow06386bb2008-02-08 02:11:37 +00008#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
11#include <linux/mempool.h>
12#include <linux/module.h>
13#include <linux/pagemap.h>
14#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/workqueue.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010016#include <linux/device-mapper.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010017#include <linux/dm-io.h>
18#include <linux/dm-dirty-log.h>
19#include <linux/dm-kcopyd.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010020#include <linux/dm-region-hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022#define DM_MSG_PREFIX "raid1"
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010023
24#define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070026#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010027#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070028
Jonathan E Brassow33184042006-11-08 17:44:44 -080029static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070032 * Mirror set structures.
33 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000034enum dm_raid1_error {
35 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000036 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000037 DM_RAID1_SYNC_ERROR,
38 DM_RAID1_READ_ERROR
39};
40
Neil Browne4c8b3b2006-06-26 00:27:26 -070041struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010042 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070043 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000044 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070045 struct dm_dev *dev;
46 sector_t offset;
47};
48
49struct mirror_set {
50 struct dm_target *ti;
51 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010052
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070053 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070054
Jonathan Brassow72f4b312008-02-08 02:11:29 +000055 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070056 struct bio_list reads;
57 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000058 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000059 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070060
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010061 struct dm_region_hash *rh;
62 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070063 struct dm_io_client *io_client;
Jonathan Brassow06386bb2008-02-08 02:11:37 +000064 mempool_t *read_record_pool;
Milan Broz88be1632007-05-09 02:33:04 -070065
Neil Browne4c8b3b2006-06-26 00:27:26 -070066 /* recovery */
67 region_t nr_regions;
68 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010069 int log_failure;
Mikulas Patocka929be8f2009-12-10 23:52:06 +000070 int leg_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000071 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070072
Jonathan Brassow72f4b312008-02-08 02:11:29 +000073 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070074
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070075 struct workqueue_struct *kmirrord_wq;
76 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010077 struct timer_list timer;
78 unsigned long timer_pending;
79
Jonathan Brassow72f4b312008-02-08 02:11:29 +000080 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070081
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010082 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070083 struct mirror mirror[0];
84};
85
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010086static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010088 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070090 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
91}
92
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010093static void delayed_wake_fn(unsigned long data)
94{
95 struct mirror_set *ms = (struct mirror_set *) data;
96
97 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010098 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010099}
100
101static void delayed_wake(struct mirror_set *ms)
102{
103 if (test_and_set_bit(0, &ms->timer_pending))
104 return;
105
106 ms->timer.expires = jiffies + HZ / 5;
107 ms->timer.data = (unsigned long) ms;
108 ms->timer.function = delayed_wake_fn;
109 add_timer(&ms->timer);
110}
111
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100112static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100114 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100117static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100121 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100123 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
124 spin_lock_irqsave(&ms->lock, flags);
125 should_wake = !(bl->head);
126 bio_list_add(bl, bio);
127 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100130 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100133static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100135 struct mirror_set *ms = context;
136 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100138 while ((bio = bio_list_pop(bio_list)))
139 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000142#define MIN_READ_RECORDS 20
143struct dm_raid1_read_record {
144 struct mirror *m;
145 struct dm_bio_details details;
146};
147
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100148static struct kmem_cache *_dm_raid1_read_record_cache;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
151 * Every mirror should look like this one.
152 */
153#define DEFAULT_MIRROR 0
154
155/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000156 * This is yucky. We squirrel the mirror struct away inside
157 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * doesn't get submitted to the lower levels of block layer.
159 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000160static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000162 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000165static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000167 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000170static struct mirror *get_default_mirror(struct mirror_set *ms)
171{
172 return &ms->mirror[atomic_read(&ms->default_mirror)];
173}
174
175static void set_default_mirror(struct mirror *m)
176{
177 struct mirror_set *ms = m->ms;
178 struct mirror *m0 = &(ms->mirror[0]);
179
180 atomic_set(&ms->default_mirror, m - m0);
181}
182
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000183static struct mirror *get_valid_mirror(struct mirror_set *ms)
184{
185 struct mirror *m;
186
187 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
188 if (!atomic_read(&m->error_count))
189 return m;
190
191 return NULL;
192}
193
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000194/* fail_mirror
195 * @m: mirror device to fail
196 * @error_type: one of the enum's, DM_RAID1_*_ERROR
197 *
198 * If errors are being handled, record the type of
199 * error encountered for this device. If this type
200 * of error has already been recorded, we can return;
201 * otherwise, we must signal userspace by triggering
202 * an event. Additionally, if the device is the
203 * primary device, we must choose a new primary, but
204 * only if the mirror is in-sync.
205 *
206 * This function must not block.
207 */
208static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
209{
210 struct mirror_set *ms = m->ms;
211 struct mirror *new;
212
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000213 ms->leg_failure = 1;
214
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000215 /*
216 * error_count is used for nothing more than a
217 * simple way to tell if a device has encountered
218 * errors.
219 */
220 atomic_inc(&m->error_count);
221
222 if (test_and_set_bit(error_type, &m->error_type))
223 return;
224
Jonathan Brassowd460c652009-01-06 03:04:57 +0000225 if (!errors_handled(ms))
226 return;
227
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000228 if (m != get_default_mirror(ms))
229 goto out;
230
231 if (!ms->in_sync) {
232 /*
233 * Better to issue requests to same failing device
234 * than to risk returning corrupt data.
235 */
236 DMERR("Primary mirror (%s) failed while out-of-sync: "
237 "Reads may fail.", m->dev->name);
238 goto out;
239 }
240
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000241 new = get_valid_mirror(ms);
242 if (new)
243 set_default_mirror(new);
244 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000245 DMWARN("All sides of mirror have failed.");
246
247out:
248 schedule_work(&ms->trigger_event);
249}
250
Mikulas Patockac0da3742009-12-10 23:52:02 +0000251static int mirror_flush(struct dm_target *ti)
252{
253 struct mirror_set *ms = ti->private;
254 unsigned long error_bits;
255
256 unsigned int i;
257 struct dm_io_region io[ms->nr_mirrors];
258 struct mirror *m;
259 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200260 .bi_rw = WRITE_FLUSH,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000261 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000262 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000263 .client = ms->io_client,
264 };
265
266 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
267 io[i].bdev = m->dev->bdev;
268 io[i].sector = 0;
269 io[i].count = 0;
270 }
271
272 error_bits = -1;
273 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
274 if (unlikely(error_bits != 0)) {
275 for (i = 0; i < ms->nr_mirrors; i++)
276 if (test_bit(i, &error_bits))
277 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000278 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000279 return -EIO;
280 }
281
282 return 0;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*-----------------------------------------------------------------
286 * Recovery.
287 *
288 * When a mirror is first activated we may find that some regions
289 * are in the no-sync state. We have to recover these by
290 * recopying from the default mirror to all the others.
291 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700292static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 void *context)
294{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100295 struct dm_region *reg = context;
296 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000297 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000299 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100300 /* Read error means the failure of default mirror. */
301 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000302 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
303 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100304
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000305 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700306 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100307 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000308 /*
309 * Bits correspond to devices (excluding default mirror).
310 * The default mirror cannot change during recovery.
311 */
312 for (m = 0; m < ms->nr_mirrors; m++) {
313 if (&ms->mirror[m] == get_default_mirror(ms))
314 continue;
315 if (test_bit(bit, &write_err))
316 fail_mirror(ms->mirror + m,
317 DM_RAID1_SYNC_ERROR);
318 bit++;
319 }
320 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100321
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100322 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100325static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100328 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100329 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct mirror *m;
331 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100332 region_t key = dm_rh_get_region_key(reg);
333 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000336 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100338 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
339 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /*
341 * The final region may be smaller than
342 * region_size.
343 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100344 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100346 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100348 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* fill in the destinations */
351 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000352 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 continue;
354
355 m = ms->mirror + i;
356 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100357 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dest->count = from.count;
359 dest++;
360 }
361
362 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100363 if (!errors_handled(ms))
364 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
365
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100366 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
367 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 return r;
370}
371
372static void do_recovery(struct mirror_set *ms)
373{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100374 struct dm_region *reg;
375 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /*
379 * Start quiescing some regions.
380 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100381 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Copy any already quiesced regions.
385 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100386 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 r = recover(ms, reg);
388 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100389 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 /*
393 * Update the in sync flag.
394 */
395 if (!ms->in_sync &&
396 (log->type->get_sync_count(log) == ms->nr_regions)) {
397 /* the sync is complete */
398 dm_table_event(ms->ti->table);
399 ms->in_sync = 1;
400 }
401}
402
403/*-----------------------------------------------------------------
404 * Reads
405 *---------------------------------------------------------------*/
406static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
407{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000408 struct mirror *m = get_default_mirror(ms);
409
410 do {
411 if (likely(!atomic_read(&m->error_count)))
412 return m;
413
414 if (m-- == ms->mirror)
415 m += ms->nr_mirrors;
416 } while (m != get_default_mirror(ms));
417
418 return NULL;
419}
420
421static int default_ok(struct mirror *m)
422{
423 struct mirror *default_mirror = get_default_mirror(m->ms);
424
425 return !atomic_read(&default_mirror->error_count);
426}
427
428static int mirror_available(struct mirror_set *ms, struct bio *bio)
429{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100430 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
431 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000432
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100433 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000434 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
435
436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/*
440 * remap a buffer to a particular mirror.
441 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000442static sector_t map_sector(struct mirror *m, struct bio *bio)
443{
Mikulas Patocka41841532009-12-10 23:51:59 +0000444 if (unlikely(!bio->bi_size))
445 return 0;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +0100446 return m->offset + dm_target_offset(m->ms->ti, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000447}
448
449static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000452 bio->bi_sector = map_sector(m, bio);
453}
454
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100455static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000456 struct bio *bio)
457{
458 io->bdev = m->dev->bdev;
459 io->sector = map_sector(m, bio);
460 io->count = bio->bi_size >> 9;
461}
462
Mikulas Patocka04788502009-12-10 23:52:03 +0000463static void hold_bio(struct mirror_set *ms, struct bio *bio)
464{
465 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000466 * Lock is required to avoid race condition during suspend
467 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000468 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000469 spin_lock_irq(&ms->lock);
470
Mikulas Patocka04788502009-12-10 23:52:03 +0000471 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000472 spin_unlock_irq(&ms->lock);
473
474 /*
475 * If device is suspended, complete the bio.
476 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000477 if (dm_noflush_suspending(ms->ti))
478 bio_endio(bio, DM_ENDIO_REQUEUE);
479 else
480 bio_endio(bio, -EIO);
481 return;
482 }
483
484 /*
485 * Hold bio until the suspend is complete.
486 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000487 bio_list_add(&ms->holds, bio);
488 spin_unlock_irq(&ms->lock);
489}
490
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000491/*-----------------------------------------------------------------
492 * Reads
493 *---------------------------------------------------------------*/
494static void read_callback(unsigned long error, void *context)
495{
496 struct bio *bio = context;
497 struct mirror *m;
498
499 m = bio_get_m(bio);
500 bio_set_m(bio, NULL);
501
502 if (likely(!error)) {
503 bio_endio(bio, 0);
504 return;
505 }
506
507 fail_mirror(m, DM_RAID1_READ_ERROR);
508
509 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
510 DMWARN_LIMIT("Read failure on mirror device %s. "
511 "Trying alternative device.",
512 m->dev->name);
513 queue_bio(m->ms, bio, bio_rw(bio));
514 return;
515 }
516
517 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
518 m->dev->name);
519 bio_endio(bio, -EIO);
520}
521
522/* Asynchronous read. */
523static void read_async_bio(struct mirror *m, struct bio *bio)
524{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100525 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000526 struct dm_io_request io_req = {
527 .bi_rw = READ,
528 .mem.type = DM_IO_BVEC,
529 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
530 .notify.fn = read_callback,
531 .notify.context = bio,
532 .client = m->ms->io_client,
533 };
534
535 map_region(&io, m, bio);
536 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100537 BUG_ON(dm_io(&io_req, 1, &io, NULL));
538}
539
540static inline int region_in_sync(struct mirror_set *ms, region_t region,
541 int may_block)
542{
543 int state = dm_rh_get_state(ms->rh, region, may_block);
544 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static void do_reads(struct mirror_set *ms, struct bio_list *reads)
548{
549 region_t region;
550 struct bio *bio;
551 struct mirror *m;
552
553 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100554 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000555 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /*
558 * We can only read balance if the region is in sync.
559 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100560 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000562 else if (m && atomic_read(&m->error_count))
563 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000565 if (likely(m))
566 read_async_bio(m, bio);
567 else
568 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570}
571
572/*-----------------------------------------------------------------
573 * Writes.
574 *
575 * We do different things with the write io depending on the
576 * state of the region that it's in:
577 *
578 * SYNC: increment pending, use kcopyd to write to *all* mirrors
579 * RECOVERING: delay the io until recovery completes
580 * NOSYNC: increment pending, just write to the default mirror
581 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000582
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static void write_callback(unsigned long error, void *context)
585{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000586 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct bio *bio = (struct bio *) context;
588 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000589 int should_wake = 0;
590 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000592 ms = bio_get_m(bio)->ms;
593 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /*
596 * NOTE: We don't decrement the pending count here,
597 * instead it is done by the targets endio function.
598 * This way we handle both writes to SYNC and NOSYNC
599 * regions with the same code.
600 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000601 if (likely(!error)) {
602 bio_endio(bio, ret);
603 return;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000606 for (i = 0; i < ms->nr_mirrors; i++)
607 if (test_bit(i, &error))
608 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000609
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000610 /*
611 * Need to raise event. Since raising
612 * events can block, we need to do it in
613 * the main thread.
614 */
615 spin_lock_irqsave(&ms->lock, flags);
616 if (!ms->failures.head)
617 should_wake = 1;
618 bio_list_add(&ms->failures, bio);
619 spin_unlock_irqrestore(&ms->lock, flags);
620 if (should_wake)
621 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624static void do_write(struct mirror_set *ms, struct bio *bio)
625{
626 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100627 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700629 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200630 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
Milan Broz88be1632007-05-09 02:33:04 -0700631 .mem.type = DM_IO_BVEC,
632 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
633 .notify.fn = write_callback,
634 .notify.context = bio,
635 .client = ms->io_client,
636 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000638 if (bio->bi_rw & REQ_DISCARD) {
639 io_req.bi_rw |= REQ_DISCARD;
640 io_req.mem.type = DM_IO_KMEM;
641 io_req.mem.ptr.addr = NULL;
642 }
643
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000644 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
645 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000647 /*
648 * Use default mirror because we only need it to retrieve the reference
649 * to the mirror set in write_callback().
650 */
651 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700652
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100653 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656static void do_writes(struct mirror_set *ms, struct bio_list *writes)
657{
658 int state;
659 struct bio *bio;
660 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100661 struct bio_list requeue;
662 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
663 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (!writes->head)
666 return;
667
668 /*
669 * Classify each write.
670 */
671 bio_list_init(&sync);
672 bio_list_init(&nosync);
673 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100674 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 while ((bio = bio_list_pop(writes))) {
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000677 if ((bio->bi_rw & REQ_FLUSH) ||
678 (bio->bi_rw & REQ_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000679 bio_list_add(&sync, bio);
680 continue;
681 }
682
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100683 region = dm_rh_bio_to_region(ms->rh, bio);
684
685 if (log->type->is_remote_recovering &&
686 log->type->is_remote_recovering(log, region)) {
687 bio_list_add(&requeue, bio);
688 continue;
689 }
690
691 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100693 case DM_RH_CLEAN:
694 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 this_list = &sync;
696 break;
697
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100698 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 this_list = &nosync;
700 break;
701
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100702 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 this_list = &recover;
704 break;
705 }
706
707 bio_list_add(this_list, bio);
708 }
709
710 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100711 * Add bios that are delayed due to remote recovery
712 * back on to the write queue
713 */
714 if (unlikely(requeue.head)) {
715 spin_lock_irq(&ms->lock);
716 bio_list_merge(&ms->writes, &requeue);
717 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100718 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100719 }
720
721 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * Increment the pending counts for any regions that will
723 * be written to (writes to recover regions are going to
724 * be delayed).
725 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100726 dm_rh_inc_pending(ms->rh, &sync);
727 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100728
729 /*
730 * If the flush fails on a previous call and succeeds here,
731 * we must not reset the log_failure variable. We need
732 * userspace interaction to do that.
733 */
734 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Dispatch io.
738 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000739 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000740 spin_lock_irq(&ms->lock);
741 bio_list_merge(&ms->failures, &sync);
742 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100743 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000744 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100745 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000746 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100749 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 while ((bio = bio_list_pop(&nosync))) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000752 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
753 spin_lock_irq(&ms->lock);
754 bio_list_add(&ms->failures, bio);
755 spin_unlock_irq(&ms->lock);
756 wakeup_mirrord(ms);
757 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000758 map_bio(get_default_mirror(ms), bio);
759 generic_make_request(bio);
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762}
763
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000764static void do_failures(struct mirror_set *ms, struct bio_list *failures)
765{
766 struct bio *bio;
767
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000768 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000769 return;
770
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000771 /*
772 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000773 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000774 * until a log has been marked, so we must store them.
775 *
776 * If a 'noflush' suspend is in progress, we can requeue
777 * the I/O's to the core. This give userspace a chance
778 * to reconfigure the mirror, at which point the core
779 * will reissue the writes. If the 'noflush' flag is
780 * not set, we have no choice but to return errors.
781 *
782 * Some writes on the failures list may have been
783 * submitted before the log failure and represent a
784 * failure to write to one of the devices. It is ok
785 * for us to treat them the same and requeue them
786 * as well.
787 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000788 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000789 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000790 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000791 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000792 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000793
794 /*
795 * If all the legs are dead, fail the I/O.
796 * If we have been told to handle errors, hold the bio
797 * and wait for userspace to deal with the problem.
798 * Otherwise pretend that the I/O succeeded. (This would
799 * be wrong if the failed leg returned after reboot and
800 * got replicated back to the good legs.)
801 */
802 if (!get_valid_mirror(ms))
803 bio_endio(bio, -EIO);
804 else if (errors_handled(ms))
805 hold_bio(ms, bio);
806 else
807 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000808 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000809}
810
811static void trigger_event(struct work_struct *work)
812{
813 struct mirror_set *ms =
814 container_of(work, struct mirror_set, trigger_event);
815
816 dm_table_event(ms->ti->table);
817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/*-----------------------------------------------------------------
820 * kmirrord
821 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100822static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100824 struct mirror_set *ms = container_of(work, struct mirror_set,
825 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000826 struct bio_list reads, writes, failures;
827 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000829 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 reads = ms->reads;
831 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000832 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 bio_list_init(&ms->reads);
834 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000835 bio_list_init(&ms->failures);
836 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100838 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 do_recovery(ms);
840 do_reads(ms, &reads);
841 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000842 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/*-----------------------------------------------------------------
846 * Target functions
847 *---------------------------------------------------------------*/
848static struct mirror_set *alloc_context(unsigned int nr_mirrors,
849 uint32_t region_size,
850 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100851 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 size_t len;
854 struct mirror_set *ms = NULL;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
857
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700858 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700860 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return NULL;
862 }
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000865 bio_list_init(&ms->reads);
866 bio_list_init(&ms->writes);
867 bio_list_init(&ms->failures);
868 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 ms->ti = ti;
871 ms->nr_mirrors = nr_mirrors;
872 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
873 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000874 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000875 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000876 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000877 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100879 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
880 _dm_raid1_read_record_cache);
881
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000882 if (!ms->read_record_pool) {
883 ti->error = "Error creating mirror read_record_pool";
884 kfree(ms);
885 return NULL;
886 }
887
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100888 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700889 if (IS_ERR(ms->io_client)) {
890 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000891 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700892 kfree(ms);
893 return NULL;
894 }
895
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100896 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
897 wakeup_all_recovery_waiters,
898 ms->ti->begin, MAX_RECOVERY,
899 dl, region_size, ms->nr_regions);
900 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700901 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100902 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000903 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 kfree(ms);
905 return NULL;
906 }
907
908 return ms;
909}
910
911static void free_context(struct mirror_set *ms, struct dm_target *ti,
912 unsigned int m)
913{
914 while (m--)
915 dm_put_device(ti, ms->mirror[m].dev);
916
Milan Broz88be1632007-05-09 02:33:04 -0700917 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100918 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000919 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 kfree(ms);
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
924 unsigned int mirror, char **argv)
925{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800926 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100927 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100929 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700930 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -EINVAL;
932 }
933
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000934 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700936 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -ENXIO;
938 }
939
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100940 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000941 atomic_set(&(ms->mirror[mirror].error_count), 0);
942 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 ms->mirror[mirror].offset = offset;
944
945 return 0;
946}
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
949 * Create dirty log: log_type #log_params <log_params>
950 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100951static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100952 unsigned argc, char **argv,
953 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100955 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100956 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100957 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700960 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return NULL;
962 }
963
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100964 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700965 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return NULL;
967 }
968
969 *args_used = 2 + param_count;
970
971 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700972 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return NULL;
974 }
975
Mikulas Patockac0da3742009-12-10 23:52:02 +0000976 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
977 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700979 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return NULL;
981 }
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return dl;
984}
985
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700986static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
987 unsigned *args_used)
988{
989 unsigned num_features;
990 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100991 char dummy;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700992
993 *args_used = 0;
994
995 if (!argc)
996 return 0;
997
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100998 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700999 ti->error = "Invalid number of features";
1000 return -EINVAL;
1001 }
1002
1003 argc--;
1004 argv++;
1005 (*args_used)++;
1006
1007 if (num_features > argc) {
1008 ti->error = "Not enough arguments to support feature count";
1009 return -EINVAL;
1010 }
1011
1012 if (!strcmp("handle_errors", argv[0]))
1013 ms->features |= DM_RAID1_HANDLE_ERRORS;
1014 else {
1015 ti->error = "Unrecognised feature requested";
1016 return -EINVAL;
1017 }
1018
1019 (*args_used)++;
1020
1021 return 0;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/*
1025 * Construct a mirror mapping:
1026 *
1027 * log_type #log_params <log_params>
1028 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001029 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
1031 * log_type is "core" or "disk"
1032 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001033 *
1034 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1037{
1038 int r;
1039 unsigned int nr_mirrors, m, args_used;
1040 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001041 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001042 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 dl = create_dirty_log(ti, argc, argv, &args_used);
1045 if (!dl)
1046 return -EINVAL;
1047
1048 argv += args_used;
1049 argc -= args_used;
1050
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001051 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001052 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001053 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001054 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -EINVAL;
1056 }
1057
1058 argv++, argc--;
1059
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001060 if (argc < nr_mirrors * 2) {
1061 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001062 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return -EINVAL;
1064 }
1065
1066 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1067 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001068 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -ENOMEM;
1070 }
1071
1072 /* Get the mirror parameter sets */
1073 for (m = 0; m < nr_mirrors; m++) {
1074 r = get_mirror(ms, ti, m, argv);
1075 if (r) {
1076 free_context(ms, ti, m);
1077 return r;
1078 }
1079 argv += 2;
1080 argc -= 2;
1081 }
1082
1083 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001084
1085 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1086 if (r)
1087 goto err_free_context;
1088
Mikulas Patocka41841532009-12-10 23:51:59 +00001089 ti->num_flush_requests = 1;
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +00001090 ti->num_discard_requests = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001091 ti->discard_zeroes_data_unsupported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Tejun Heo9c4376d2011-01-13 19:59:58 +00001093 ms->kmirrord_wq = alloc_workqueue("kmirrord",
1094 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001095 if (!ms->kmirrord_wq) {
1096 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001097 r = -ENOMEM;
1098 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001099 }
1100 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001101 init_timer(&ms->timer);
1102 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001103 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001104
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001105 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001106 if (r)
1107 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001108
1109 argv += args_used;
1110 argc -= args_used;
1111
Jonathan Brassowf44db672007-07-12 17:29:04 +01001112 /*
1113 * Any read-balancing addition depends on the
1114 * DM_RAID1_HANDLE_ERRORS flag being present.
1115 * This is because the decision to balance depends
1116 * on the sync state of a region. If the above
1117 * flag is not present, we ignore errors; and
1118 * the sync state may be inaccurate.
1119 */
1120
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001121 if (argc) {
1122 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001123 r = -EINVAL;
1124 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001125 }
1126
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001127 ms->kcopyd_client = dm_kcopyd_client_create();
1128 if (IS_ERR(ms->kcopyd_client)) {
1129 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001130 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001133 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001135
1136err_destroy_wq:
1137 destroy_workqueue(ms->kmirrord_wq);
1138err_free_context:
1139 free_context(ms, ti, ms->nr_mirrors);
1140 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143static void mirror_dtr(struct dm_target *ti)
1144{
1145 struct mirror_set *ms = (struct mirror_set *) ti->private;
1146
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001147 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001148 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001149 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001150 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001151 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 free_context(ms, ti, ms->nr_mirrors);
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/*
1156 * Mirror mapping function
1157 */
1158static int mirror_map(struct dm_target *ti, struct bio *bio,
1159 union map_info *map_context)
1160{
1161 int r, rw = bio_rw(bio);
1162 struct mirror *m;
1163 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001164 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001165 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001168 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001169 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001171 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001174 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (r < 0 && r != -EWOULDBLOCK)
1176 return r;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001179 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001181 if (!r || (r == -EWOULDBLOCK)) {
1182 if (rw == READA)
1183 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001186 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001189 /*
1190 * The region is in-sync and we can perform reads directly.
1191 * Store enough information so we can retry if it fails.
1192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001194 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return -EIO;
1196
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001197 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
Mikulas Patocka7c272132012-12-21 20:23:32 +00001198 dm_bio_record(&read_record->details, bio);
1199 map_context->ptr = read_record;
1200 read_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001201
1202 map_bio(m, bio);
1203
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001204 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1208 int error, union map_info *map_context)
1209{
1210 int rw = bio_rw(bio);
1211 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001212 struct mirror *m = NULL;
1213 struct dm_bio_details *bd = NULL;
1214 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /*
1217 * We need to dec pending if this was a write.
1218 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001219 if (rw == WRITE) {
Mikulas Patocka751f1882012-07-20 14:25:03 +01001220 if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
Mikulas Patocka41841532009-12-10 23:51:59 +00001221 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001222 return error;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001225 if (error == -EOPNOTSUPP)
1226 goto out;
1227
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001228 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001229 goto out;
1230
1231 if (unlikely(error)) {
1232 if (!read_record) {
1233 /*
1234 * There wasn't enough memory to record necessary
1235 * information for a retry or there was no other
1236 * mirror in-sync.
1237 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001238 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001239 return -EIO;
1240 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001241
1242 m = read_record->m;
1243
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001244 DMERR("Mirror read failed from %s. Trying alternative device.",
1245 m->dev->name);
1246
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001247 fail_mirror(m, DM_RAID1_READ_ERROR);
1248
1249 /*
1250 * A failed read is requeued for another attempt using an intact
1251 * mirror.
1252 */
1253 if (default_ok(m) || mirror_available(ms, bio)) {
1254 bd = &read_record->details;
1255
1256 dm_bio_restore(bd, bio);
1257 mempool_free(read_record, ms->read_record_pool);
1258 map_context->ptr = NULL;
1259 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001260 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001261 }
1262 DMERR("All replicated volumes dead, failing I/O");
1263 }
1264
1265out:
1266 if (read_record) {
1267 mempool_free(read_record, ms->read_record_pool);
1268 map_context->ptr = NULL;
1269 }
1270
1271 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001274static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001277 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Mikulas Patocka04788502009-12-10 23:52:03 +00001279 struct bio_list holds;
1280 struct bio *bio;
1281
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001282 atomic_set(&ms->suspend, 1);
1283
1284 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001285 * Process bios in the hold list to start recovery waiting
1286 * for bios in the hold list. After the process, no bio has
1287 * a chance to be added in the hold list because ms->suspend
1288 * is set.
1289 */
1290 spin_lock_irq(&ms->lock);
1291 holds = ms->holds;
1292 bio_list_init(&ms->holds);
1293 spin_unlock_irq(&ms->lock);
1294
1295 while ((bio = bio_list_pop(&holds)))
1296 hold_bio(ms, bio);
1297
1298 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001299 * We must finish up all the work that we've
1300 * generated (i.e. recovery work).
1301 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001302 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001303
Jonathan E Brassow33184042006-11-08 17:44:44 -08001304 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001305 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001306
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001307 if (log->type->presuspend && log->type->presuspend(log))
1308 /* FIXME: need better error handling */
1309 DMWARN("log presuspend failed");
1310
1311 /*
1312 * Now that recovery is complete/stopped and the
1313 * delayed bios are queued, we need to wait for
1314 * the worker thread to complete. This way,
1315 * we know that all of our I/O has been pushed.
1316 */
1317 flush_workqueue(ms->kmirrord_wq);
1318}
1319
1320static void mirror_postsuspend(struct dm_target *ti)
1321{
1322 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001323 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001324
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001325 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001327 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330static void mirror_resume(struct dm_target *ti)
1331{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001332 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001333 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001334
1335 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (log->type->resume && log->type->resume(log))
1337 /* FIXME: need better error handling */
1338 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001339 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001342/*
1343 * device_status_char
1344 * @m: mirror device/leg we want the status of
1345 *
1346 * We return one character representing the most severe error
1347 * we have encountered.
1348 * A => Alive - No failures
1349 * D => Dead - A write failure occurred leaving mirror out-of-sync
1350 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1351 * R => Read - A read failure occurred, mirror data unaffected
1352 *
1353 * Returns: <char>
1354 */
1355static char device_status_char(struct mirror *m)
1356{
1357 if (!atomic_read(&(m->error_count)))
1358 return 'A';
1359
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001360 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1361 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001362 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1363 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1364}
1365
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367static int mirror_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01001368 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001370 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001372 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001373 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 switch (type) {
1376 case STATUSTYPE_INFO:
1377 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001378 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001380 buffer[m] = device_status_char(&(ms->mirror[m]));
1381 }
1382 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001384 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001385 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001386 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001387
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001388 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391
1392 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001393 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001394
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001395 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001397 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001398 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001399
1400 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1401 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
1404 return 0;
1405}
1406
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001407static int mirror_iterate_devices(struct dm_target *ti,
1408 iterate_devices_callout_fn fn, void *data)
1409{
1410 struct mirror_set *ms = ti->private;
1411 int ret = 0;
1412 unsigned i;
1413
1414 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1415 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001416 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001417
1418 return ret;
1419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421static struct target_type mirror_target = {
1422 .name = "mirror",
Tejun Heo9c4376d2011-01-13 19:59:58 +00001423 .version = {1, 12, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 .module = THIS_MODULE,
1425 .ctr = mirror_ctr,
1426 .dtr = mirror_dtr,
1427 .map = mirror_map,
1428 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001429 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 .postsuspend = mirror_postsuspend,
1431 .resume = mirror_resume,
1432 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001433 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434};
1435
1436static int __init dm_mirror_init(void)
1437{
1438 int r;
1439
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001440 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1441 if (!_dm_raid1_read_record_cache) {
1442 DMERR("Can't allocate dm_raid1_read_record cache");
1443 r = -ENOMEM;
1444 goto bad_cache;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001447 r = dm_register_target(&mirror_target);
1448 if (r < 0) {
1449 DMERR("Failed to register mirror target");
1450 goto bad_target;
1451 }
1452
1453 return 0;
1454
1455bad_target:
1456 kmem_cache_destroy(_dm_raid1_read_record_cache);
1457bad_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return r;
1459}
1460
1461static void __exit dm_mirror_exit(void)
1462{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001463 dm_unregister_target(&mirror_target);
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001464 kmem_cache_destroy(_dm_raid1_read_record_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467/* Module hooks */
1468module_init(dm_mirror_init);
1469module_exit(dm_mirror_exit);
1470
1471MODULE_DESCRIPTION(DM_NAME " mirror target");
1472MODULE_AUTHOR("Joe Thornber");
1473MODULE_LICENSE("GPL");