blob: dee326775c6064b045c8cf523a25a528d9882caf [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. */
Milan Broz88be1632007-05-09 02:33:04 -070025#define DM_IO_PAGES 64
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010026#define DM_KCOPYD_PAGES 64
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070028#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010029#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070030
Jonathan E Brassow33184042006-11-08 17:44:44 -080031static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070034 * Mirror set structures.
35 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000036enum dm_raid1_error {
37 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000038 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000039 DM_RAID1_SYNC_ERROR,
40 DM_RAID1_READ_ERROR
41};
42
Neil Browne4c8b3b2006-06-26 00:27:26 -070043struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010044 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070045 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000046 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070047 struct dm_dev *dev;
48 sector_t offset;
49};
50
51struct mirror_set {
52 struct dm_target *ti;
53 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010054
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070055 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070056
Jonathan Brassow72f4b312008-02-08 02:11:29 +000057 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070058 struct bio_list reads;
59 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000060 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000061 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070062
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010063 struct dm_region_hash *rh;
64 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070065 struct dm_io_client *io_client;
Jonathan Brassow06386bb2008-02-08 02:11:37 +000066 mempool_t *read_record_pool;
Milan Broz88be1632007-05-09 02:33:04 -070067
Neil Browne4c8b3b2006-06-26 00:27:26 -070068 /* recovery */
69 region_t nr_regions;
70 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010071 int log_failure;
Mikulas Patocka929be8f2009-12-10 23:52:06 +000072 int leg_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000073 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070074
Jonathan Brassow72f4b312008-02-08 02:11:29 +000075 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070076
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070077 struct workqueue_struct *kmirrord_wq;
78 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010079 struct timer_list timer;
80 unsigned long timer_pending;
81
Jonathan Brassow72f4b312008-02-08 02:11:29 +000082 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070083
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010084 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070085 struct mirror mirror[0];
86};
87
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010088static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010090 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070092 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
93}
94
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010095static void delayed_wake_fn(unsigned long data)
96{
97 struct mirror_set *ms = (struct mirror_set *) data;
98
99 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100100 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100101}
102
103static void delayed_wake(struct mirror_set *ms)
104{
105 if (test_and_set_bit(0, &ms->timer_pending))
106 return;
107
108 ms->timer.expires = jiffies + HZ / 5;
109 ms->timer.data = (unsigned long) ms;
110 ms->timer.function = delayed_wake_fn;
111 add_timer(&ms->timer);
112}
113
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100114static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100116 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100119static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100123 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100125 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
126 spin_lock_irqsave(&ms->lock, flags);
127 should_wake = !(bl->head);
128 bio_list_add(bl, bio);
129 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100132 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100135static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100137 struct mirror_set *ms = context;
138 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100140 while ((bio = bio_list_pop(bio_list)))
141 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000144#define MIN_READ_RECORDS 20
145struct dm_raid1_read_record {
146 struct mirror *m;
147 struct dm_bio_details details;
148};
149
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100150static struct kmem_cache *_dm_raid1_read_record_cache;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * Every mirror should look like this one.
154 */
155#define DEFAULT_MIRROR 0
156
157/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000158 * This is yucky. We squirrel the mirror struct away inside
159 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * doesn't get submitted to the lower levels of block layer.
161 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000162static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000164 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000167static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000169 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000172static struct mirror *get_default_mirror(struct mirror_set *ms)
173{
174 return &ms->mirror[atomic_read(&ms->default_mirror)];
175}
176
177static void set_default_mirror(struct mirror *m)
178{
179 struct mirror_set *ms = m->ms;
180 struct mirror *m0 = &(ms->mirror[0]);
181
182 atomic_set(&ms->default_mirror, m - m0);
183}
184
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000185static struct mirror *get_valid_mirror(struct mirror_set *ms)
186{
187 struct mirror *m;
188
189 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
190 if (!atomic_read(&m->error_count))
191 return m;
192
193 return NULL;
194}
195
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000196/* fail_mirror
197 * @m: mirror device to fail
198 * @error_type: one of the enum's, DM_RAID1_*_ERROR
199 *
200 * If errors are being handled, record the type of
201 * error encountered for this device. If this type
202 * of error has already been recorded, we can return;
203 * otherwise, we must signal userspace by triggering
204 * an event. Additionally, if the device is the
205 * primary device, we must choose a new primary, but
206 * only if the mirror is in-sync.
207 *
208 * This function must not block.
209 */
210static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
211{
212 struct mirror_set *ms = m->ms;
213 struct mirror *new;
214
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000215 ms->leg_failure = 1;
216
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000217 /*
218 * error_count is used for nothing more than a
219 * simple way to tell if a device has encountered
220 * errors.
221 */
222 atomic_inc(&m->error_count);
223
224 if (test_and_set_bit(error_type, &m->error_type))
225 return;
226
Jonathan Brassowd460c652009-01-06 03:04:57 +0000227 if (!errors_handled(ms))
228 return;
229
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000230 if (m != get_default_mirror(ms))
231 goto out;
232
233 if (!ms->in_sync) {
234 /*
235 * Better to issue requests to same failing device
236 * than to risk returning corrupt data.
237 */
238 DMERR("Primary mirror (%s) failed while out-of-sync: "
239 "Reads may fail.", m->dev->name);
240 goto out;
241 }
242
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000243 new = get_valid_mirror(ms);
244 if (new)
245 set_default_mirror(new);
246 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000247 DMWARN("All sides of mirror have failed.");
248
249out:
250 schedule_work(&ms->trigger_event);
251}
252
Mikulas Patockac0da3742009-12-10 23:52:02 +0000253static int mirror_flush(struct dm_target *ti)
254{
255 struct mirror_set *ms = ti->private;
256 unsigned long error_bits;
257
258 unsigned int i;
259 struct dm_io_region io[ms->nr_mirrors];
260 struct mirror *m;
261 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200262 .bi_rw = WRITE_FLUSH,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000263 .mem.type = DM_IO_KMEM,
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000264 .mem.ptr.addr = NULL,
Mikulas Patockac0da3742009-12-10 23:52:02 +0000265 .client = ms->io_client,
266 };
267
268 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
269 io[i].bdev = m->dev->bdev;
270 io[i].sector = 0;
271 io[i].count = 0;
272 }
273
274 error_bits = -1;
275 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
276 if (unlikely(error_bits != 0)) {
277 for (i = 0; i < ms->nr_mirrors; i++)
278 if (test_bit(i, &error_bits))
279 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000280 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000281 return -EIO;
282 }
283
284 return 0;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*-----------------------------------------------------------------
288 * Recovery.
289 *
290 * When a mirror is first activated we may find that some regions
291 * are in the no-sync state. We have to recover these by
292 * recopying from the default mirror to all the others.
293 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700294static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 void *context)
296{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100297 struct dm_region *reg = context;
298 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000299 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000301 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100302 /* Read error means the failure of default mirror. */
303 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000304 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
305 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100306
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000307 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700308 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100309 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000310 /*
311 * Bits correspond to devices (excluding default mirror).
312 * The default mirror cannot change during recovery.
313 */
314 for (m = 0; m < ms->nr_mirrors; m++) {
315 if (&ms->mirror[m] == get_default_mirror(ms))
316 continue;
317 if (test_bit(bit, &write_err))
318 fail_mirror(ms->mirror + m,
319 DM_RAID1_SYNC_ERROR);
320 bit++;
321 }
322 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100323
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100324 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100327static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100330 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100331 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct mirror *m;
333 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100334 region_t key = dm_rh_get_region_key(reg);
335 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000338 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100340 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
341 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /*
343 * The final region may be smaller than
344 * region_size.
345 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100346 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100348 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100350 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* fill in the destinations */
353 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000354 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 continue;
356
357 m = ms->mirror + i;
358 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100359 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dest->count = from.count;
361 dest++;
362 }
363
364 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100365 if (!errors_handled(ms))
366 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
367
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100368 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
369 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return r;
372}
373
374static void do_recovery(struct mirror_set *ms)
375{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100376 struct dm_region *reg;
377 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * Start quiescing some regions.
382 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100383 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /*
386 * Copy any already quiesced regions.
387 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100388 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 r = recover(ms, reg);
390 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100391 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 /*
395 * Update the in sync flag.
396 */
397 if (!ms->in_sync &&
398 (log->type->get_sync_count(log) == ms->nr_regions)) {
399 /* the sync is complete */
400 dm_table_event(ms->ti->table);
401 ms->in_sync = 1;
402 }
403}
404
405/*-----------------------------------------------------------------
406 * Reads
407 *---------------------------------------------------------------*/
408static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
409{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000410 struct mirror *m = get_default_mirror(ms);
411
412 do {
413 if (likely(!atomic_read(&m->error_count)))
414 return m;
415
416 if (m-- == ms->mirror)
417 m += ms->nr_mirrors;
418 } while (m != get_default_mirror(ms));
419
420 return NULL;
421}
422
423static int default_ok(struct mirror *m)
424{
425 struct mirror *default_mirror = get_default_mirror(m->ms);
426
427 return !atomic_read(&default_mirror->error_count);
428}
429
430static int mirror_available(struct mirror_set *ms, struct bio *bio)
431{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100432 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
433 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000434
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100435 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000436 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
437
438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/*
442 * remap a buffer to a particular mirror.
443 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000444static sector_t map_sector(struct mirror *m, struct bio *bio)
445{
Mikulas Patocka41841532009-12-10 23:51:59 +0000446 if (unlikely(!bio->bi_size))
447 return 0;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +0100448 return m->offset + dm_target_offset(m->ms->ti, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000449}
450
451static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000454 bio->bi_sector = map_sector(m, bio);
455}
456
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100457static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000458 struct bio *bio)
459{
460 io->bdev = m->dev->bdev;
461 io->sector = map_sector(m, bio);
462 io->count = bio->bi_size >> 9;
463}
464
Mikulas Patocka04788502009-12-10 23:52:03 +0000465static void hold_bio(struct mirror_set *ms, struct bio *bio)
466{
467 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +0000468 * Lock is required to avoid race condition during suspend
469 * process.
Mikulas Patocka04788502009-12-10 23:52:03 +0000470 */
Takahiro Yasuif0703042010-03-06 02:32:35 +0000471 spin_lock_irq(&ms->lock);
472
Mikulas Patocka04788502009-12-10 23:52:03 +0000473 if (atomic_read(&ms->suspend)) {
Takahiro Yasuif0703042010-03-06 02:32:35 +0000474 spin_unlock_irq(&ms->lock);
475
476 /*
477 * If device is suspended, complete the bio.
478 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000479 if (dm_noflush_suspending(ms->ti))
480 bio_endio(bio, DM_ENDIO_REQUEUE);
481 else
482 bio_endio(bio, -EIO);
483 return;
484 }
485
486 /*
487 * Hold bio until the suspend is complete.
488 */
Mikulas Patocka04788502009-12-10 23:52:03 +0000489 bio_list_add(&ms->holds, bio);
490 spin_unlock_irq(&ms->lock);
491}
492
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000493/*-----------------------------------------------------------------
494 * Reads
495 *---------------------------------------------------------------*/
496static void read_callback(unsigned long error, void *context)
497{
498 struct bio *bio = context;
499 struct mirror *m;
500
501 m = bio_get_m(bio);
502 bio_set_m(bio, NULL);
503
504 if (likely(!error)) {
505 bio_endio(bio, 0);
506 return;
507 }
508
509 fail_mirror(m, DM_RAID1_READ_ERROR);
510
511 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
512 DMWARN_LIMIT("Read failure on mirror device %s. "
513 "Trying alternative device.",
514 m->dev->name);
515 queue_bio(m->ms, bio, bio_rw(bio));
516 return;
517 }
518
519 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
520 m->dev->name);
521 bio_endio(bio, -EIO);
522}
523
524/* Asynchronous read. */
525static void read_async_bio(struct mirror *m, struct bio *bio)
526{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100527 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000528 struct dm_io_request io_req = {
529 .bi_rw = READ,
530 .mem.type = DM_IO_BVEC,
531 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
532 .notify.fn = read_callback,
533 .notify.context = bio,
534 .client = m->ms->io_client,
535 };
536
537 map_region(&io, m, bio);
538 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100539 BUG_ON(dm_io(&io_req, 1, &io, NULL));
540}
541
542static inline int region_in_sync(struct mirror_set *ms, region_t region,
543 int may_block)
544{
545 int state = dm_rh_get_state(ms->rh, region, may_block);
546 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549static void do_reads(struct mirror_set *ms, struct bio_list *reads)
550{
551 region_t region;
552 struct bio *bio;
553 struct mirror *m;
554
555 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100556 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000557 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /*
560 * We can only read balance if the region is in sync.
561 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100562 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000564 else if (m && atomic_read(&m->error_count))
565 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000567 if (likely(m))
568 read_async_bio(m, bio);
569 else
570 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572}
573
574/*-----------------------------------------------------------------
575 * Writes.
576 *
577 * We do different things with the write io depending on the
578 * state of the region that it's in:
579 *
580 * SYNC: increment pending, use kcopyd to write to *all* mirrors
581 * RECOVERING: delay the io until recovery completes
582 * NOSYNC: increment pending, just write to the default mirror
583 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000584
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static void write_callback(unsigned long error, void *context)
587{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000588 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct bio *bio = (struct bio *) context;
590 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000591 int should_wake = 0;
592 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000594 ms = bio_get_m(bio)->ms;
595 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /*
598 * NOTE: We don't decrement the pending count here,
599 * instead it is done by the targets endio function.
600 * This way we handle both writes to SYNC and NOSYNC
601 * regions with the same code.
602 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000603 if (likely(!error)) {
604 bio_endio(bio, ret);
605 return;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000608 for (i = 0; i < ms->nr_mirrors; i++)
609 if (test_bit(i, &error))
610 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000611
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000612 /*
613 * Need to raise event. Since raising
614 * events can block, we need to do it in
615 * the main thread.
616 */
617 spin_lock_irqsave(&ms->lock, flags);
618 if (!ms->failures.head)
619 should_wake = 1;
620 bio_list_add(&ms->failures, bio);
621 spin_unlock_irqrestore(&ms->lock, flags);
622 if (should_wake)
623 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626static void do_write(struct mirror_set *ms, struct bio *bio)
627{
628 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100629 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700631 struct dm_io_request io_req = {
Tejun Heod87f4c12010-09-03 11:56:19 +0200632 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
Milan Broz88be1632007-05-09 02:33:04 -0700633 .mem.type = DM_IO_BVEC,
634 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
635 .notify.fn = write_callback,
636 .notify.context = bio,
637 .client = ms->io_client,
638 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000640 if (bio->bi_rw & REQ_DISCARD) {
641 io_req.bi_rw |= REQ_DISCARD;
642 io_req.mem.type = DM_IO_KMEM;
643 io_req.mem.ptr.addr = NULL;
644 }
645
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000646 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
647 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000649 /*
650 * Use default mirror because we only need it to retrieve the reference
651 * to the mirror set in write_callback().
652 */
653 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700654
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100655 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static void do_writes(struct mirror_set *ms, struct bio_list *writes)
659{
660 int state;
661 struct bio *bio;
662 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100663 struct bio_list requeue;
664 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
665 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!writes->head)
668 return;
669
670 /*
671 * Classify each write.
672 */
673 bio_list_init(&sync);
674 bio_list_init(&nosync);
675 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100676 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 while ((bio = bio_list_pop(writes))) {
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +0000679 if ((bio->bi_rw & REQ_FLUSH) ||
680 (bio->bi_rw & REQ_DISCARD)) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000681 bio_list_add(&sync, bio);
682 continue;
683 }
684
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100685 region = dm_rh_bio_to_region(ms->rh, bio);
686
687 if (log->type->is_remote_recovering &&
688 log->type->is_remote_recovering(log, region)) {
689 bio_list_add(&requeue, bio);
690 continue;
691 }
692
693 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100695 case DM_RH_CLEAN:
696 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 this_list = &sync;
698 break;
699
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100700 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 this_list = &nosync;
702 break;
703
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100704 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 this_list = &recover;
706 break;
707 }
708
709 bio_list_add(this_list, bio);
710 }
711
712 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100713 * Add bios that are delayed due to remote recovery
714 * back on to the write queue
715 */
716 if (unlikely(requeue.head)) {
717 spin_lock_irq(&ms->lock);
718 bio_list_merge(&ms->writes, &requeue);
719 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100720 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100721 }
722
723 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * Increment the pending counts for any regions that will
725 * be written to (writes to recover regions are going to
726 * be delayed).
727 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100728 dm_rh_inc_pending(ms->rh, &sync);
729 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100730
731 /*
732 * If the flush fails on a previous call and succeeds here,
733 * we must not reset the log_failure variable. We need
734 * userspace interaction to do that.
735 */
736 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
739 * Dispatch io.
740 */
Mikulas Patocka5528d172010-02-16 18:42:55 +0000741 if (unlikely(ms->log_failure) && errors_handled(ms)) {
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000742 spin_lock_irq(&ms->lock);
743 bio_list_merge(&ms->failures, &sync);
744 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100745 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000746 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100747 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000748 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100751 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 while ((bio = bio_list_pop(&nosync))) {
Mikulas Patockaede5ea02010-03-06 02:32:22 +0000754 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
755 spin_lock_irq(&ms->lock);
756 bio_list_add(&ms->failures, bio);
757 spin_unlock_irq(&ms->lock);
758 wakeup_mirrord(ms);
759 } else {
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000760 map_bio(get_default_mirror(ms), bio);
761 generic_make_request(bio);
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764}
765
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000766static void do_failures(struct mirror_set *ms, struct bio_list *failures)
767{
768 struct bio *bio;
769
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000770 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000771 return;
772
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000773 /*
774 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000775 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000776 * until a log has been marked, so we must store them.
777 *
778 * If a 'noflush' suspend is in progress, we can requeue
779 * the I/O's to the core. This give userspace a chance
780 * to reconfigure the mirror, at which point the core
781 * will reissue the writes. If the 'noflush' flag is
782 * not set, we have no choice but to return errors.
783 *
784 * Some writes on the failures list may have been
785 * submitted before the log failure and represent a
786 * failure to write to one of the devices. It is ok
787 * for us to treat them the same and requeue them
788 * as well.
789 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000790 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000791 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000792 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000793 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000794 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000795
796 /*
797 * If all the legs are dead, fail the I/O.
798 * If we have been told to handle errors, hold the bio
799 * and wait for userspace to deal with the problem.
800 * Otherwise pretend that the I/O succeeded. (This would
801 * be wrong if the failed leg returned after reboot and
802 * got replicated back to the good legs.)
803 */
804 if (!get_valid_mirror(ms))
805 bio_endio(bio, -EIO);
806 else if (errors_handled(ms))
807 hold_bio(ms, bio);
808 else
809 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000810 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000811}
812
813static void trigger_event(struct work_struct *work)
814{
815 struct mirror_set *ms =
816 container_of(work, struct mirror_set, trigger_event);
817
818 dm_table_event(ms->ti->table);
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/*-----------------------------------------------------------------
822 * kmirrord
823 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100824static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100826 struct mirror_set *ms = container_of(work, struct mirror_set,
827 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000828 struct bio_list reads, writes, failures;
829 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000831 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 reads = ms->reads;
833 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000834 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 bio_list_init(&ms->reads);
836 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000837 bio_list_init(&ms->failures);
838 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100840 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 do_recovery(ms);
842 do_reads(ms, &reads);
843 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000844 do_failures(ms, &failures);
Mikulas Patocka7ff14a32008-04-24 22:10:47 +0100845
846 dm_table_unplug_all(ms->ti->table);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/*-----------------------------------------------------------------
850 * Target functions
851 *---------------------------------------------------------------*/
852static struct mirror_set *alloc_context(unsigned int nr_mirrors,
853 uint32_t region_size,
854 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100855 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 size_t len;
858 struct mirror_set *ms = NULL;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
861
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700862 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700864 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return NULL;
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 spin_lock_init(&ms->lock);
Mikulas Patocka5339fc22009-12-10 23:52:06 +0000869 bio_list_init(&ms->reads);
870 bio_list_init(&ms->writes);
871 bio_list_init(&ms->failures);
872 bio_list_init(&ms->holds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 ms->ti = ti;
875 ms->nr_mirrors = nr_mirrors;
876 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
877 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000878 ms->log_failure = 0;
Mikulas Patocka929be8f2009-12-10 23:52:06 +0000879 ms->leg_failure = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000880 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000881 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100883 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
884 _dm_raid1_read_record_cache);
885
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000886 if (!ms->read_record_pool) {
887 ti->error = "Error creating mirror read_record_pool";
888 kfree(ms);
889 return NULL;
890 }
891
Milan Broz88be1632007-05-09 02:33:04 -0700892 ms->io_client = dm_io_client_create(DM_IO_PAGES);
893 if (IS_ERR(ms->io_client)) {
894 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000895 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700896 kfree(ms);
897 return NULL;
898 }
899
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100900 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
901 wakeup_all_recovery_waiters,
902 ms->ti->begin, MAX_RECOVERY,
903 dl, region_size, ms->nr_regions);
904 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700905 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100906 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000907 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 kfree(ms);
909 return NULL;
910 }
911
912 return ms;
913}
914
915static void free_context(struct mirror_set *ms, struct dm_target *ti,
916 unsigned int m)
917{
918 while (m--)
919 dm_put_device(ti, ms->mirror[m].dev);
920
Milan Broz88be1632007-05-09 02:33:04 -0700921 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100922 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000923 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 kfree(ms);
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
928 unsigned int mirror, char **argv)
929{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800930 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Andrew Morton4ee218c2006-03-27 01:17:48 -0800932 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700933 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -EINVAL;
935 }
936
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000937 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700939 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -ENXIO;
941 }
942
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100943 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000944 atomic_set(&(ms->mirror[mirror].error_count), 0);
945 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 ms->mirror[mirror].offset = offset;
947
948 return 0;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * Create dirty log: log_type #log_params <log_params>
953 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100954static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100955 unsigned argc, char **argv,
956 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100958 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100959 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700962 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return NULL;
964 }
965
966 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700967 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return NULL;
969 }
970
971 *args_used = 2 + param_count;
972
973 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700974 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return NULL;
976 }
977
Mikulas Patockac0da3742009-12-10 23:52:02 +0000978 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
979 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700981 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return NULL;
983 }
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return dl;
986}
987
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700988static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
989 unsigned *args_used)
990{
991 unsigned num_features;
992 struct dm_target *ti = ms->ti;
993
994 *args_used = 0;
995
996 if (!argc)
997 return 0;
998
999 if (sscanf(argv[0], "%u", &num_features) != 1) {
1000 ti->error = "Invalid number of features";
1001 return -EINVAL;
1002 }
1003
1004 argc--;
1005 argv++;
1006 (*args_used)++;
1007
1008 if (num_features > argc) {
1009 ti->error = "Not enough arguments to support feature count";
1010 return -EINVAL;
1011 }
1012
1013 if (!strcmp("handle_errors", argv[0]))
1014 ms->features |= DM_RAID1_HANDLE_ERRORS;
1015 else {
1016 ti->error = "Unrecognised feature requested";
1017 return -EINVAL;
1018 }
1019
1020 (*args_used)++;
1021
1022 return 0;
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
1026 * Construct a mirror mapping:
1027 *
1028 * log_type #log_params <log_params>
1029 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001030 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 *
1032 * log_type is "core" or "disk"
1033 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001034 *
1035 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1038{
1039 int r;
1040 unsigned int nr_mirrors, m, args_used;
1041 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001042 struct dm_dirty_log *dl;
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
1051 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 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;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001084 ti->split_io = dm_rh_get_region_size(ms->rh);
Mikulas Patocka41841532009-12-10 23:51:59 +00001085 ti->num_flush_requests = 1;
Mike Snitzer5fc2ffe2011-01-13 19:59:48 +00001086 ti->num_discard_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Tejun Heo9c4376d2011-01-13 19:59:58 +00001088 ms->kmirrord_wq = alloc_workqueue("kmirrord",
1089 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001090 if (!ms->kmirrord_wq) {
1091 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001092 r = -ENOMEM;
1093 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001094 }
1095 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001096 init_timer(&ms->timer);
1097 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001098 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001099
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001100 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001101 if (r)
1102 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001103
1104 argv += args_used;
1105 argc -= args_used;
1106
Jonathan Brassowf44db672007-07-12 17:29:04 +01001107 /*
1108 * Any read-balancing addition depends on the
1109 * DM_RAID1_HANDLE_ERRORS flag being present.
1110 * This is because the decision to balance depends
1111 * on the sync state of a region. If the above
1112 * flag is not present, we ignore errors; and
1113 * the sync state may be inaccurate.
1114 */
1115
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001116 if (argc) {
1117 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001118 r = -EINVAL;
1119 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001120 }
1121
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001122 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001123 if (r)
1124 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001126 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001128
1129err_destroy_wq:
1130 destroy_workqueue(ms->kmirrord_wq);
1131err_free_context:
1132 free_context(ms, ti, ms->nr_mirrors);
1133 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136static void mirror_dtr(struct dm_target *ti)
1137{
1138 struct mirror_set *ms = (struct mirror_set *) ti->private;
1139
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001140 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001141 flush_workqueue(ms->kmirrord_wq);
Tejun Heod5ffa382011-01-13 19:59:56 +00001142 flush_work_sync(&ms->trigger_event);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001143 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001144 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 free_context(ms, ti, ms->nr_mirrors);
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/*
1149 * Mirror mapping function
1150 */
1151static int mirror_map(struct dm_target *ti, struct bio *bio,
1152 union map_info *map_context)
1153{
1154 int r, rw = bio_rw(bio);
1155 struct mirror *m;
1156 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001157 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001158 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001161 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001162 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001164 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001167 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (r < 0 && r != -EWOULDBLOCK)
1169 return r;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001172 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001174 if (!r || (r == -EWOULDBLOCK)) {
1175 if (rw == READA)
1176 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001179 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001182 /*
1183 * The region is in-sync and we can perform reads directly.
1184 * Store enough information so we can retry if it fails.
1185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001187 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -EIO;
1189
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001190 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1191 if (likely(read_record)) {
1192 dm_bio_record(&read_record->details, bio);
1193 map_context->ptr = read_record;
1194 read_record->m = m;
1195 }
1196
1197 map_bio(m, bio);
1198
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001199 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1203 int error, union map_info *map_context)
1204{
1205 int rw = bio_rw(bio);
1206 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001207 struct mirror *m = NULL;
1208 struct dm_bio_details *bd = NULL;
1209 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /*
1212 * We need to dec pending if this was a write.
1213 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001214 if (rw == WRITE) {
Tejun Heod87f4c12010-09-03 11:56:19 +02001215 if (!(bio->bi_rw & REQ_FLUSH))
Mikulas Patocka41841532009-12-10 23:51:59 +00001216 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001217 return error;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001220 if (error == -EOPNOTSUPP)
1221 goto out;
1222
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001223 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001224 goto out;
1225
1226 if (unlikely(error)) {
1227 if (!read_record) {
1228 /*
1229 * There wasn't enough memory to record necessary
1230 * information for a retry or there was no other
1231 * mirror in-sync.
1232 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001233 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001234 return -EIO;
1235 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001236
1237 m = read_record->m;
1238
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001239 DMERR("Mirror read failed from %s. Trying alternative device.",
1240 m->dev->name);
1241
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001242 fail_mirror(m, DM_RAID1_READ_ERROR);
1243
1244 /*
1245 * A failed read is requeued for another attempt using an intact
1246 * mirror.
1247 */
1248 if (default_ok(m) || mirror_available(ms, bio)) {
1249 bd = &read_record->details;
1250
1251 dm_bio_restore(bd, bio);
1252 mempool_free(read_record, ms->read_record_pool);
1253 map_context->ptr = NULL;
1254 queue_bio(ms, bio, rw);
1255 return 1;
1256 }
1257 DMERR("All replicated volumes dead, failing I/O");
1258 }
1259
1260out:
1261 if (read_record) {
1262 mempool_free(read_record, ms->read_record_pool);
1263 map_context->ptr = NULL;
1264 }
1265
1266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001269static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001272 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Mikulas Patocka04788502009-12-10 23:52:03 +00001274 struct bio_list holds;
1275 struct bio *bio;
1276
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001277 atomic_set(&ms->suspend, 1);
1278
1279 /*
Takahiro Yasuif0703042010-03-06 02:32:35 +00001280 * Process bios in the hold list to start recovery waiting
1281 * for bios in the hold list. After the process, no bio has
1282 * a chance to be added in the hold list because ms->suspend
1283 * is set.
1284 */
1285 spin_lock_irq(&ms->lock);
1286 holds = ms->holds;
1287 bio_list_init(&ms->holds);
1288 spin_unlock_irq(&ms->lock);
1289
1290 while ((bio = bio_list_pop(&holds)))
1291 hold_bio(ms, bio);
1292
1293 /*
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001294 * We must finish up all the work that we've
1295 * generated (i.e. recovery work).
1296 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001297 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001298
Jonathan E Brassow33184042006-11-08 17:44:44 -08001299 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001300 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001301
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001302 if (log->type->presuspend && log->type->presuspend(log))
1303 /* FIXME: need better error handling */
1304 DMWARN("log presuspend failed");
1305
1306 /*
1307 * Now that recovery is complete/stopped and the
1308 * delayed bios are queued, we need to wait for
1309 * the worker thread to complete. This way,
1310 * we know that all of our I/O has been pushed.
1311 */
1312 flush_workqueue(ms->kmirrord_wq);
1313}
1314
1315static void mirror_postsuspend(struct dm_target *ti)
1316{
1317 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001318 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001319
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001320 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001322 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325static void mirror_resume(struct dm_target *ti)
1326{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001327 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001328 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001329
1330 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (log->type->resume && log->type->resume(log))
1332 /* FIXME: need better error handling */
1333 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001334 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001337/*
1338 * device_status_char
1339 * @m: mirror device/leg we want the status of
1340 *
1341 * We return one character representing the most severe error
1342 * we have encountered.
1343 * A => Alive - No failures
1344 * D => Dead - A write failure occurred leaving mirror out-of-sync
1345 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1346 * R => Read - A read failure occurred, mirror data unaffected
1347 *
1348 * Returns: <char>
1349 */
1350static char device_status_char(struct mirror *m)
1351{
1352 if (!atomic_read(&(m->error_count)))
1353 return 'A';
1354
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001355 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1356 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001357 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1358 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1359}
1360
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362static int mirror_status(struct dm_target *ti, status_type_t type,
1363 char *result, unsigned int maxlen)
1364{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001365 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001367 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001368 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 switch (type) {
1371 case STATUSTYPE_INFO:
1372 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001373 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001375 buffer[m] = device_status_char(&(ms->mirror[m]));
1376 }
1377 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001379 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001380 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001381 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001382
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001383 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386
1387 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001388 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001389
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001390 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001392 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001393 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001394
1395 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1396 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
1399 return 0;
1400}
1401
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001402static int mirror_iterate_devices(struct dm_target *ti,
1403 iterate_devices_callout_fn fn, void *data)
1404{
1405 struct mirror_set *ms = ti->private;
1406 int ret = 0;
1407 unsigned i;
1408
1409 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1410 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001411 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001412
1413 return ret;
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static struct target_type mirror_target = {
1417 .name = "mirror",
Tejun Heo9c4376d2011-01-13 19:59:58 +00001418 .version = {1, 12, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 .module = THIS_MODULE,
1420 .ctr = mirror_ctr,
1421 .dtr = mirror_dtr,
1422 .map = mirror_map,
1423 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001424 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 .postsuspend = mirror_postsuspend,
1426 .resume = mirror_resume,
1427 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001428 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429};
1430
1431static int __init dm_mirror_init(void)
1432{
1433 int r;
1434
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001435 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1436 if (!_dm_raid1_read_record_cache) {
1437 DMERR("Can't allocate dm_raid1_read_record cache");
1438 r = -ENOMEM;
1439 goto bad_cache;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001442 r = dm_register_target(&mirror_target);
1443 if (r < 0) {
1444 DMERR("Failed to register mirror target");
1445 goto bad_target;
1446 }
1447
1448 return 0;
1449
1450bad_target:
1451 kmem_cache_destroy(_dm_raid1_read_record_cache);
1452bad_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return r;
1454}
1455
1456static void __exit dm_mirror_exit(void)
1457{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001458 dm_unregister_target(&mirror_target);
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001459 kmem_cache_destroy(_dm_raid1_read_record_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462/* Module hooks */
1463module_init(dm_mirror_init);
1464module_exit(dm_mirror_exit);
1465
1466MODULE_DESCRIPTION(DM_NAME " mirror target");
1467MODULE_AUTHOR("Joe Thornber");
1468MODULE_LICENSE("GPL");