blob: a7d0d8a4bbdf37e8151d134718385b2241e3f6bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01003 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Jonathan Brassow06386bb2008-02-08 02:11:37 +00008#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
11#include <linux/mempool.h>
12#include <linux/module.h>
13#include <linux/pagemap.h>
14#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/workqueue.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010016#include <linux/device-mapper.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010017#include <linux/dm-io.h>
18#include <linux/dm-dirty-log.h>
19#include <linux/dm-kcopyd.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010020#include <linux/dm-region-hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022#define DM_MSG_PREFIX "raid1"
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010023
24#define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070026#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010027#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070028
Jonathan E Brassow33184042006-11-08 17:44:44 -080029static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070032 * Mirror set structures.
33 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000034enum dm_raid1_error {
35 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000036 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000037 DM_RAID1_SYNC_ERROR,
38 DM_RAID1_READ_ERROR
39};
40
Neil Browne4c8b3b2006-06-26 00:27:26 -070041struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010042 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070043 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000044 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070045 struct dm_dev *dev;
46 sector_t offset;
47};
48
49struct mirror_set {
50 struct dm_target *ti;
51 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010052
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070053 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070054
Jonathan Brassow72f4b312008-02-08 02:11:29 +000055 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070056 struct bio_list reads;
57 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000058 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000059 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070060
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010061 struct dm_region_hash *rh;
62 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070063 struct dm_io_client *io_client;
64
Neil Browne4c8b3b2006-06-26 00:27:26 -070065 /* recovery */
66 region_t nr_regions;
67 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010068 int log_failure;
Mikulas Patocka929be8f2009-12-10 23:52:06 +000069 int leg_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000070 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070071
Jonathan Brassow72f4b312008-02-08 02:11:29 +000072 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070073
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070074 struct workqueue_struct *kmirrord_wq;
75 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010076 struct timer_list timer;
77 unsigned long timer_pending;
78
Jonathan Brassow72f4b312008-02-08 02:11:29 +000079 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070080
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010081 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070082 struct mirror mirror[0];
83};
84
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010085static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010087 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070089 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
90}
91
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010092static void delayed_wake_fn(unsigned long data)
93{
94 struct mirror_set *ms = (struct mirror_set *) data;
95
96 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010097 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010098}
99
100static void delayed_wake(struct mirror_set *ms)
101{
102 if (test_and_set_bit(0, &ms->timer_pending))
103 return;
104
105 ms->timer.expires = jiffies + HZ / 5;
106 ms->timer.data = (unsigned long) ms;
107 ms->timer.function = delayed_wake_fn;
108 add_timer(&ms->timer);
109}
110
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100111static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100113 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100116static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100120 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100122 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
123 spin_lock_irqsave(&ms->lock, flags);
124 should_wake = !(bl->head);
125 bio_list_add(bl, bio);
126 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100129 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100132static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100134 struct mirror_set *ms = context;
135 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100137 while ((bio = bio_list_pop(bio_list)))
138 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Mikulas Patocka89c7cd82012-12-21 20:23:39 +0000141struct dm_raid1_bio_record {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000142 struct mirror *m;
143 struct dm_bio_details details;
144};
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * Every mirror should look like this one.
148 */
149#define DEFAULT_MIRROR 0
150
151/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000152 * This is yucky. We squirrel the mirror struct away inside
153 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * doesn't get submitted to the lower levels of block layer.
155 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000156static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000158 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000161static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000163 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000166static struct mirror *get_default_mirror(struct mirror_set *ms)
167{
168 return &ms->mirror[atomic_read(&ms->default_mirror)];
169}
170
171static void set_default_mirror(struct mirror *m)
172{
173 struct mirror_set *ms = m->ms;
174 struct mirror *m0 = &(ms->mirror[0]);
175
176 atomic_set(&ms->default_mirror, m - m0);
177}
178
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000179static struct mirror *get_valid_mirror(struct mirror_set *ms)
180{
181 struct mirror *m;
182
183 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
184 if (!atomic_read(&m->error_count))
185 return m;
186
187 return NULL;
188}
189
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000190/* fail_mirror
191 * @m: mirror device to fail
192 * @error_type: one of the enum's, DM_RAID1_*_ERROR
193 *
194 * If errors are being handled, record the type of
195 * error encountered for this device. If this type
196 * of error has already been recorded, we can return;
197 * otherwise, we must signal userspace by triggering
198 * an event. Additionally, if the device is the
199 * primary device, we must choose a new primary, but
200 * only if the mirror is in-sync.
201 *
202 * This function must not block.
203 */
204static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
205{
206 struct mirror_set *ms = m->ms;
207 struct mirror *new;
208
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000209 ms->leg_failure = 1;
210
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000211 /*
212 * error_count is used for nothing more than a
213 * simple way to tell if a device has encountered
214 * errors.
215 */
216 atomic_inc(&m->error_count);
217
218 if (test_and_set_bit(error_type, &m->error_type))
219 return;
220
Jonathan Brassowd460c652009-01-06 03:04:57 +0000221 if (!errors_handled(ms))
222 return;
223
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000224 if (m != get_default_mirror(ms))
225 goto out;
226
227 if (!ms->in_sync) {
228 /*
229 * Better to issue requests to same failing device
230 * than to risk returning corrupt data.
231 */
232 DMERR("Primary mirror (%s) failed while out-of-sync: "
233 "Reads may fail.", m->dev->name);
234 goto out;
235 }
236
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000237 new = get_valid_mirror(ms);
238 if (new)
239 set_default_mirror(new);
240 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000241 DMWARN("All sides of mirror have failed.");
242
243out:
244 schedule_work(&ms->trigger_event);
245}
246
Mikulas Patockac0da3742009-12-10 23:52:02 +0000247static int mirror_flush(struct dm_target *ti)
248{
249 struct mirror_set *ms = ti->private;
250 unsigned long error_bits;
251
252 unsigned int i;
253 struct dm_io_region io[ms->nr_mirrors];
254 struct mirror *m;
255 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200256 .bi_rw = WRITE_FLUSH,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000257 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000258 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000259 .client = ms->io_client,
260 };
261
262 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
263 io[i].bdev = m->dev->bdev;
264 io[i].sector = 0;
265 io[i].count = 0;
266 }
267
268 error_bits = -1;
269 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
270 if (unlikely(error_bits != 0)) {
271 for (i = 0; i < ms->nr_mirrors; i++)
272 if (test_bit(i, &error_bits))
273 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000274 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000275 return -EIO;
276 }
277
278 return 0;
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*-----------------------------------------------------------------
282 * Recovery.
283 *
284 * When a mirror is first activated we may find that some regions
285 * are in the no-sync state. We have to recover these by
286 * recopying from the default mirror to all the others.
287 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700288static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 void *context)
290{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100291 struct dm_region *reg = context;
292 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000293 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000295 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100296 /* Read error means the failure of default mirror. */
297 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000298 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
299 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100300
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000301 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700302 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100303 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000304 /*
305 * Bits correspond to devices (excluding default mirror).
306 * The default mirror cannot change during recovery.
307 */
308 for (m = 0; m < ms->nr_mirrors; m++) {
309 if (&ms->mirror[m] == get_default_mirror(ms))
310 continue;
311 if (test_bit(bit, &write_err))
312 fail_mirror(ms->mirror + m,
313 DM_RAID1_SYNC_ERROR);
314 bit++;
315 }
316 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100317
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100318 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100321static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100324 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100325 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct mirror *m;
327 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100328 region_t key = dm_rh_get_region_key(reg);
329 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000332 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100334 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
335 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * The final region may be smaller than
338 * region_size.
339 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100340 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100342 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100344 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* fill in the destinations */
347 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000348 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 continue;
350
351 m = ms->mirror + i;
352 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100353 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dest->count = from.count;
355 dest++;
356 }
357
358 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100359 if (!errors_handled(ms))
360 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
361
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100362 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
363 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 return r;
366}
367
368static void do_recovery(struct mirror_set *ms)
369{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100370 struct dm_region *reg;
371 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /*
375 * Start quiescing some regions.
376 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100377 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /*
380 * Copy any already quiesced regions.
381 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100382 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 r = recover(ms, reg);
384 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100385 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
388 /*
389 * Update the in sync flag.
390 */
391 if (!ms->in_sync &&
392 (log->type->get_sync_count(log) == ms->nr_regions)) {
393 /* the sync is complete */
394 dm_table_event(ms->ti->table);
395 ms->in_sync = 1;
396 }
397}
398
399/*-----------------------------------------------------------------
400 * Reads
401 *---------------------------------------------------------------*/
402static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
403{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000404 struct mirror *m = get_default_mirror(ms);
405
406 do {
407 if (likely(!atomic_read(&m->error_count)))
408 return m;
409
410 if (m-- == ms->mirror)
411 m += ms->nr_mirrors;
412 } while (m != get_default_mirror(ms));
413
414 return NULL;
415}
416
417static int default_ok(struct mirror *m)
418{
419 struct mirror *default_mirror = get_default_mirror(m->ms);
420
421 return !atomic_read(&default_mirror->error_count);
422}
423
424static int mirror_available(struct mirror_set *ms, struct bio *bio)
425{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100426 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
427 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000428
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100429 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000430 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
431
432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/*
436 * remap a buffer to a particular mirror.
437 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000438static sector_t map_sector(struct mirror *m, struct bio *bio)
439{
Mikulas Patocka41841532009-12-10 23:51:59 +0000440 if (unlikely(!bio->bi_size))
441 return 0;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +0100442 return m->offset + dm_target_offset(m->ms->ti, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000443}
444
445static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000448 bio->bi_sector = map_sector(m, bio);
449}
450
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100451static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000452 struct bio *bio)
453{
454 io->bdev = m->dev->bdev;
455 io->sector = map_sector(m, bio);
456 io->count = bio->bi_size >> 9;
457}
458
Mikulas Patocka04788502009-12-10 23:52:03 +0000459static void hold_bio(struct mirror_set *ms, struct bio *bio)
460{
461 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000462 * Lock is required to avoid race condition during suspend
463 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000464 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000465 spin_lock_irq(&ms->lock);
466
Mikulas Patocka04788502009-12-10 23:52:03 +0000467 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000468 spin_unlock_irq(&ms->lock);
469
470 /*
471 * If device is suspended, complete the bio.
472 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000473 if (dm_noflush_suspending(ms->ti))
474 bio_endio(bio, DM_ENDIO_REQUEUE);
475 else
476 bio_endio(bio, -EIO);
477 return;
478 }
479
480 /*
481 * Hold bio until the suspend is complete.
482 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000483 bio_list_add(&ms->holds, bio);
484 spin_unlock_irq(&ms->lock);
485}
486
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000487/*-----------------------------------------------------------------
488 * Reads
489 *---------------------------------------------------------------*/
490static void read_callback(unsigned long error, void *context)
491{
492 struct bio *bio = context;
493 struct mirror *m;
494
495 m = bio_get_m(bio);
496 bio_set_m(bio, NULL);
497
498 if (likely(!error)) {
499 bio_endio(bio, 0);
500 return;
501 }
502
503 fail_mirror(m, DM_RAID1_READ_ERROR);
504
505 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
506 DMWARN_LIMIT("Read failure on mirror device %s. "
507 "Trying alternative device.",
508 m->dev->name);
509 queue_bio(m->ms, bio, bio_rw(bio));
510 return;
511 }
512
513 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
514 m->dev->name);
515 bio_endio(bio, -EIO);
516}
517
518/* Asynchronous read. */
519static void read_async_bio(struct mirror *m, struct bio *bio)
520{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100521 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000522 struct dm_io_request io_req = {
523 .bi_rw = READ,
524 .mem.type = DM_IO_BVEC,
525 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
526 .notify.fn = read_callback,
527 .notify.context = bio,
528 .client = m->ms->io_client,
529 };
530
531 map_region(&io, m, bio);
532 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100533 BUG_ON(dm_io(&io_req, 1, &io, NULL));
534}
535
536static inline int region_in_sync(struct mirror_set *ms, region_t region,
537 int may_block)
538{
539 int state = dm_rh_get_state(ms->rh, region, may_block);
540 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543static void do_reads(struct mirror_set *ms, struct bio_list *reads)
544{
545 region_t region;
546 struct bio *bio;
547 struct mirror *m;
548
549 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100550 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000551 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /*
554 * We can only read balance if the region is in sync.
555 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100556 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000558 else if (m && atomic_read(&m->error_count))
559 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000561 if (likely(m))
562 read_async_bio(m, bio);
563 else
564 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566}
567
568/*-----------------------------------------------------------------
569 * Writes.
570 *
571 * We do different things with the write io depending on the
572 * state of the region that it's in:
573 *
574 * SYNC: increment pending, use kcopyd to write to *all* mirrors
575 * RECOVERING: delay the io until recovery completes
576 * NOSYNC: increment pending, just write to the default mirror
577 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000578
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static void write_callback(unsigned long error, void *context)
581{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000582 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct bio *bio = (struct bio *) context;
584 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000585 int should_wake = 0;
586 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000588 ms = bio_get_m(bio)->ms;
589 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /*
592 * NOTE: We don't decrement the pending count here,
593 * instead it is done by the targets endio function.
594 * This way we handle both writes to SYNC and NOSYNC
595 * regions with the same code.
596 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000597 if (likely(!error)) {
598 bio_endio(bio, ret);
599 return;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000602 for (i = 0; i < ms->nr_mirrors; i++)
603 if (test_bit(i, &error))
604 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000605
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000606 /*
607 * Need to raise event. Since raising
608 * events can block, we need to do it in
609 * the main thread.
610 */
611 spin_lock_irqsave(&ms->lock, flags);
612 if (!ms->failures.head)
613 should_wake = 1;
614 bio_list_add(&ms->failures, bio);
615 spin_unlock_irqrestore(&ms->lock, flags);
616 if (should_wake)
617 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620static void do_write(struct mirror_set *ms, struct bio *bio)
621{
622 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100623 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700625 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200626 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
Milan Broz88be1632007-05-09 02:33:04 -0700627 .mem.type = DM_IO_BVEC,
628 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
629 .notify.fn = write_callback,
630 .notify.context = bio,
631 .client = ms->io_client,
632 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000634 if (bio->bi_rw & REQ_DISCARD) {
635 io_req.bi_rw |= REQ_DISCARD;
636 io_req.mem.type = DM_IO_KMEM;
637 io_req.mem.ptr.addr = NULL;
638 }
639
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000640 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
641 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000643 /*
644 * Use default mirror because we only need it to retrieve the reference
645 * to the mirror set in write_callback().
646 */
647 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700648
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100649 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652static void do_writes(struct mirror_set *ms, struct bio_list *writes)
653{
654 int state;
655 struct bio *bio;
656 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100657 struct bio_list requeue;
658 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
659 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (!writes->head)
662 return;
663
664 /*
665 * Classify each write.
666 */
667 bio_list_init(&sync);
668 bio_list_init(&nosync);
669 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100670 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 while ((bio = bio_list_pop(writes))) {
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000673 if ((bio->bi_rw & REQ_FLUSH) ||
674 (bio->bi_rw & REQ_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000675 bio_list_add(&sync, bio);
676 continue;
677 }
678
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100679 region = dm_rh_bio_to_region(ms->rh, bio);
680
681 if (log->type->is_remote_recovering &&
682 log->type->is_remote_recovering(log, region)) {
683 bio_list_add(&requeue, bio);
684 continue;
685 }
686
687 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100689 case DM_RH_CLEAN:
690 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 this_list = &sync;
692 break;
693
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100694 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 this_list = &nosync;
696 break;
697
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100698 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 this_list = &recover;
700 break;
701 }
702
703 bio_list_add(this_list, bio);
704 }
705
706 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100707 * Add bios that are delayed due to remote recovery
708 * back on to the write queue
709 */
710 if (unlikely(requeue.head)) {
711 spin_lock_irq(&ms->lock);
712 bio_list_merge(&ms->writes, &requeue);
713 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100714 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100715 }
716
717 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * Increment the pending counts for any regions that will
719 * be written to (writes to recover regions are going to
720 * be delayed).
721 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100722 dm_rh_inc_pending(ms->rh, &sync);
723 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100724
725 /*
726 * If the flush fails on a previous call and succeeds here,
727 * we must not reset the log_failure variable. We need
728 * userspace interaction to do that.
729 */
730 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /*
733 * Dispatch io.
734 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000735 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000736 spin_lock_irq(&ms->lock);
737 bio_list_merge(&ms->failures, &sync);
738 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100739 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000740 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100741 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000742 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100745 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 while ((bio = bio_list_pop(&nosync))) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000748 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
749 spin_lock_irq(&ms->lock);
750 bio_list_add(&ms->failures, bio);
751 spin_unlock_irq(&ms->lock);
752 wakeup_mirrord(ms);
753 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000754 map_bio(get_default_mirror(ms), bio);
755 generic_make_request(bio);
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758}
759
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000760static void do_failures(struct mirror_set *ms, struct bio_list *failures)
761{
762 struct bio *bio;
763
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000764 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000765 return;
766
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000767 /*
768 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000769 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000770 * until a log has been marked, so we must store them.
771 *
772 * If a 'noflush' suspend is in progress, we can requeue
773 * the I/O's to the core. This give userspace a chance
774 * to reconfigure the mirror, at which point the core
775 * will reissue the writes. If the 'noflush' flag is
776 * not set, we have no choice but to return errors.
777 *
778 * Some writes on the failures list may have been
779 * submitted before the log failure and represent a
780 * failure to write to one of the devices. It is ok
781 * for us to treat them the same and requeue them
782 * as well.
783 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000784 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000785 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000786 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000787 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000788 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000789
790 /*
791 * If all the legs are dead, fail the I/O.
792 * If we have been told to handle errors, hold the bio
793 * and wait for userspace to deal with the problem.
794 * Otherwise pretend that the I/O succeeded. (This would
795 * be wrong if the failed leg returned after reboot and
796 * got replicated back to the good legs.)
797 */
798 if (!get_valid_mirror(ms))
799 bio_endio(bio, -EIO);
800 else if (errors_handled(ms))
801 hold_bio(ms, bio);
802 else
803 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000804 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000805}
806
807static void trigger_event(struct work_struct *work)
808{
809 struct mirror_set *ms =
810 container_of(work, struct mirror_set, trigger_event);
811
812 dm_table_event(ms->ti->table);
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815/*-----------------------------------------------------------------
816 * kmirrord
817 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100818static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100820 struct mirror_set *ms = container_of(work, struct mirror_set,
821 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000822 struct bio_list reads, writes, failures;
823 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000825 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 reads = ms->reads;
827 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000828 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 bio_list_init(&ms->reads);
830 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000831 bio_list_init(&ms->failures);
832 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100834 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 do_recovery(ms);
836 do_reads(ms, &reads);
837 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000838 do_failures(ms, &failures);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*-----------------------------------------------------------------
842 * Target functions
843 *---------------------------------------------------------------*/
844static struct mirror_set *alloc_context(unsigned int nr_mirrors,
845 uint32_t region_size,
846 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100847 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 size_t len;
850 struct mirror_set *ms = NULL;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
853
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700854 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700856 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return NULL;
858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000861 bio_list_init(&ms->reads);
862 bio_list_init(&ms->writes);
863 bio_list_init(&ms->failures);
864 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 ms->ti = ti;
867 ms->nr_mirrors = nr_mirrors;
868 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
869 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000870 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000871 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000872 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000873 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100875 ms->io_client = dm_io_client_create();
Milan Broz88be1632007-05-09 02:33:04 -0700876 if (IS_ERR(ms->io_client)) {
877 ti->error = "Error creating dm_io client";
878 kfree(ms);
879 return NULL;
880 }
881
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100882 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
883 wakeup_all_recovery_waiters,
884 ms->ti->begin, MAX_RECOVERY,
885 dl, region_size, ms->nr_regions);
886 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700887 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100888 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 kfree(ms);
890 return NULL;
891 }
892
893 return ms;
894}
895
896static void free_context(struct mirror_set *ms, struct dm_target *ti,
897 unsigned int m)
898{
899 while (m--)
900 dm_put_device(ti, ms->mirror[m].dev);
901
Milan Broz88be1632007-05-09 02:33:04 -0700902 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100903 dm_region_hash_destroy(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 kfree(ms);
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
908 unsigned int mirror, char **argv)
909{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800910 unsigned long long offset;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100911 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100913 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700914 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EINVAL;
916 }
917
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000918 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700920 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -ENXIO;
922 }
923
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100924 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000925 atomic_set(&(ms->mirror[mirror].error_count), 0);
926 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ms->mirror[mirror].offset = offset;
928
929 return 0;
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/*
933 * Create dirty log: log_type #log_params <log_params>
934 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100935static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100936 unsigned argc, char **argv,
937 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100939 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100940 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100941 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700944 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return NULL;
946 }
947
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100948 if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700949 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return NULL;
951 }
952
953 *args_used = 2 + param_count;
954
955 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700956 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return NULL;
958 }
959
Mikulas Patockac0da3742009-12-10 23:52:02 +0000960 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
961 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700963 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return NULL;
965 }
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return dl;
968}
969
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700970static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
971 unsigned *args_used)
972{
973 unsigned num_features;
974 struct dm_target *ti = ms->ti;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100975 char dummy;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700976
977 *args_used = 0;
978
979 if (!argc)
980 return 0;
981
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100982 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700983 ti->error = "Invalid number of features";
984 return -EINVAL;
985 }
986
987 argc--;
988 argv++;
989 (*args_used)++;
990
991 if (num_features > argc) {
992 ti->error = "Not enough arguments to support feature count";
993 return -EINVAL;
994 }
995
996 if (!strcmp("handle_errors", argv[0]))
997 ms->features |= DM_RAID1_HANDLE_ERRORS;
998 else {
999 ti->error = "Unrecognised feature requested";
1000 return -EINVAL;
1001 }
1002
1003 (*args_used)++;
1004
1005 return 0;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * Construct a mirror mapping:
1010 *
1011 * log_type #log_params <log_params>
1012 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001013 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 *
1015 * log_type is "core" or "disk"
1016 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001017 *
1018 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1021{
1022 int r;
1023 unsigned int nr_mirrors, m, args_used;
1024 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001025 struct dm_dirty_log *dl;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001026 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 dl = create_dirty_log(ti, argc, argv, &args_used);
1029 if (!dl)
1030 return -EINVAL;
1031
1032 argv += args_used;
1033 argc -= args_used;
1034
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001035 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001036 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001037 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001038 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
1040 }
1041
1042 argv++, argc--;
1043
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001044 if (argc < nr_mirrors * 2) {
1045 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001046 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EINVAL;
1048 }
1049
1050 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1051 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001052 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -ENOMEM;
1054 }
1055
1056 /* Get the mirror parameter sets */
1057 for (m = 0; m < nr_mirrors; m++) {
1058 r = get_mirror(ms, ti, m, argv);
1059 if (r) {
1060 free_context(ms, ti, m);
1061 return r;
1062 }
1063 argv += 2;
1064 argc -= 2;
1065 }
1066
1067 ti->private = ms;
Mike Snitzer542f9032012-07-27 15:08:00 +01001068
1069 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1070 if (r)
1071 goto err_free_context;
1072
Mikulas Patocka41841532009-12-10 23:51:59 +00001073 ti->num_flush_requests = 1;
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +00001074 ti->num_discard_requests = 1;
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001075 ti->per_bio_data_size = sizeof(struct dm_raid1_bio_record);
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001076 ti->discard_zeroes_data_unsupported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Tejun Heo9c4376d2011-01-13 19:59:58 +00001078 ms->kmirrord_wq = alloc_workqueue("kmirrord",
1079 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001080 if (!ms->kmirrord_wq) {
1081 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001082 r = -ENOMEM;
1083 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001084 }
1085 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001086 init_timer(&ms->timer);
1087 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001088 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001089
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001090 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001091 if (r)
1092 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001093
1094 argv += args_used;
1095 argc -= args_used;
1096
Jonathan Brassowf44db672007-07-12 17:29:04 +01001097 /*
1098 * Any read-balancing addition depends on the
1099 * DM_RAID1_HANDLE_ERRORS flag being present.
1100 * This is because the decision to balance depends
1101 * on the sync state of a region. If the above
1102 * flag is not present, we ignore errors; and
1103 * the sync state may be inaccurate.
1104 */
1105
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001106 if (argc) {
1107 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001108 r = -EINVAL;
1109 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001110 }
1111
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001112 ms->kcopyd_client = dm_kcopyd_client_create();
1113 if (IS_ERR(ms->kcopyd_client)) {
1114 r = PTR_ERR(ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001115 goto err_destroy_wq;
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001118 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001120
1121err_destroy_wq:
1122 destroy_workqueue(ms->kmirrord_wq);
1123err_free_context:
1124 free_context(ms, ti, ms->nr_mirrors);
1125 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128static void mirror_dtr(struct dm_target *ti)
1129{
1130 struct mirror_set *ms = (struct mirror_set *) ti->private;
1131
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001132 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001133 flush_workqueue(ms->kmirrord_wq);
Tejun Heo43829732012-08-20 14:51:24 -07001134 flush_work(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001135 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001136 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 free_context(ms, ti, ms->nr_mirrors);
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
1141 * Mirror mapping function
1142 */
1143static int mirror_map(struct dm_target *ti, struct bio *bio,
1144 union map_info *map_context)
1145{
1146 int r, rw = bio_rw(bio);
1147 struct mirror *m;
1148 struct mirror_set *ms = ti->private;
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001149 struct dm_raid1_bio_record *bio_record;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001150 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001153 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001154 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001156 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001159 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (r < 0 && r != -EWOULDBLOCK)
1161 return r;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001164 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001166 if (!r || (r == -EWOULDBLOCK)) {
1167 if (rw == READA)
1168 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
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
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001174 /*
1175 * The region is in-sync and we can perform reads directly.
1176 * Store enough information so we can retry if it fails.
1177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001179 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return -EIO;
1181
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001182 bio_record = dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1183 dm_bio_record(&bio_record->details, bio);
1184 map_context->ptr = bio_record;
1185 bio_record->m = m;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001186
1187 map_bio(m, bio);
1188
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001189 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1193 int error, union map_info *map_context)
1194{
1195 int rw = bio_rw(bio);
1196 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001197 struct mirror *m = NULL;
1198 struct dm_bio_details *bd = NULL;
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001199 struct dm_raid1_bio_record *bio_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /*
1202 * We need to dec pending if this was a write.
1203 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001204 if (rw == WRITE) {
Mikulas Patocka751f1882012-07-20 14:25:03 +01001205 if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
Mikulas Patocka41841532009-12-10 23:51:59 +00001206 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001207 return error;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001210 if (error == -EOPNOTSUPP)
1211 goto out;
1212
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001213 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001214 goto out;
1215
1216 if (unlikely(error)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001217 if (!bio_record) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001218 /*
1219 * There wasn't enough memory to record necessary
1220 * information for a retry or there was no other
1221 * mirror in-sync.
1222 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001223 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001224 return -EIO;
1225 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001226
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001227 m = bio_record->m;
Adrian Bunke03f1a82008-02-19 19:44:19 +00001228
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001229 DMERR("Mirror read failed from %s. Trying alternative device.",
1230 m->dev->name);
1231
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001232 fail_mirror(m, DM_RAID1_READ_ERROR);
1233
1234 /*
1235 * A failed read is requeued for another attempt using an intact
1236 * mirror.
1237 */
1238 if (default_ok(m) || mirror_available(ms, bio)) {
Mikulas Patocka89c7cd82012-12-21 20:23:39 +00001239 bd = &bio_record->details;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001240
1241 dm_bio_restore(bd, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001242 map_context->ptr = NULL;
1243 queue_bio(ms, bio, rw);
Mikulas Patocka19cbbc62012-12-21 20:23:32 +00001244 return DM_ENDIO_INCOMPLETE;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001245 }
1246 DMERR("All replicated volumes dead, failing I/O");
1247 }
1248
1249out:
Mikulas Patocka39cf0ed2012-12-21 20:23:38 +00001250 map_context->ptr = NULL;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001251
1252 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001255static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001258 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Mikulas Patocka04788502009-12-10 23:52:03 +00001260 struct bio_list holds;
1261 struct bio *bio;
1262
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001263 atomic_set(&ms->suspend, 1);
1264
1265 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001266 * Process bios in the hold list to start recovery waiting
1267 * for bios in the hold list. After the process, no bio has
1268 * a chance to be added in the hold list because ms->suspend
1269 * is set.
1270 */
1271 spin_lock_irq(&ms->lock);
1272 holds = ms->holds;
1273 bio_list_init(&ms->holds);
1274 spin_unlock_irq(&ms->lock);
1275
1276 while ((bio = bio_list_pop(&holds)))
1277 hold_bio(ms, bio);
1278
1279 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001280 * We must finish up all the work that we've
1281 * generated (i.e. recovery work).
1282 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001283 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001284
Jonathan E Brassow33184042006-11-08 17:44:44 -08001285 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001286 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001287
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001288 if (log->type->presuspend && log->type->presuspend(log))
1289 /* FIXME: need better error handling */
1290 DMWARN("log presuspend failed");
1291
1292 /*
1293 * Now that recovery is complete/stopped and the
1294 * delayed bios are queued, we need to wait for
1295 * the worker thread to complete. This way,
1296 * we know that all of our I/O has been pushed.
1297 */
1298 flush_workqueue(ms->kmirrord_wq);
1299}
1300
1301static void mirror_postsuspend(struct dm_target *ti)
1302{
1303 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001304 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001305
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001306 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001308 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311static void mirror_resume(struct dm_target *ti)
1312{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001313 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001314 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001315
1316 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (log->type->resume && log->type->resume(log))
1318 /* FIXME: need better error handling */
1319 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001320 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001323/*
1324 * device_status_char
1325 * @m: mirror device/leg we want the status of
1326 *
1327 * We return one character representing the most severe error
1328 * we have encountered.
1329 * A => Alive - No failures
1330 * D => Dead - A write failure occurred leaving mirror out-of-sync
1331 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1332 * R => Read - A read failure occurred, mirror data unaffected
1333 *
1334 * Returns: <char>
1335 */
1336static char device_status_char(struct mirror *m)
1337{
1338 if (!atomic_read(&(m->error_count)))
1339 return 'A';
1340
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001341 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1342 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001343 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1344 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1345}
1346
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348static int mirror_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01001349 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001351 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001353 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001354 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 switch (type) {
1357 case STATUSTYPE_INFO:
1358 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001359 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001361 buffer[m] = device_status_char(&(ms->mirror[m]));
1362 }
1363 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001365 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001366 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001367 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001368
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001369 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372
1373 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001374 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001375
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001376 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001378 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001379 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001380
1381 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1382 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
1385 return 0;
1386}
1387
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001388static int mirror_iterate_devices(struct dm_target *ti,
1389 iterate_devices_callout_fn fn, void *data)
1390{
1391 struct mirror_set *ms = ti->private;
1392 int ret = 0;
1393 unsigned i;
1394
1395 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1396 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001397 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001398
1399 return ret;
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static struct target_type mirror_target = {
1403 .name = "mirror",
Mikulas Patocka39cf0ed2012-12-21 20:23:38 +00001404 .version = {1, 13, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 .module = THIS_MODULE,
1406 .ctr = mirror_ctr,
1407 .dtr = mirror_dtr,
1408 .map = mirror_map,
1409 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001410 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .postsuspend = mirror_postsuspend,
1412 .resume = mirror_resume,
1413 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001414 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415};
1416
1417static int __init dm_mirror_init(void)
1418{
1419 int r;
1420
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001421 r = dm_register_target(&mirror_target);
1422 if (r < 0) {
1423 DMERR("Failed to register mirror target");
1424 goto bad_target;
1425 }
1426
1427 return 0;
1428
1429bad_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return r;
1431}
1432
1433static void __exit dm_mirror_exit(void)
1434{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001435 dm_unregister_target(&mirror_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438/* Module hooks */
1439module_init(dm_mirror_init);
1440module_exit(dm_mirror_exit);
1441
1442MODULE_DESCRIPTION(DM_NAME " mirror target");
1443MODULE_AUTHOR("Joe Thornber");
1444MODULE_LICENSE("GPL");