blob: 92dcc06832a462a3210fbd521c323abce9b43cd0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "dm-bio-list.h"
Jonathan Brassow06386bb2008-02-08 02:11:37 +00009#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/mempool.h>
13#include <linux/module.h>
14#include <linux/pagemap.h>
15#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/workqueue.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010017#include <linux/device-mapper.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010018#include <linux/dm-io.h>
19#include <linux/dm-dirty-log.h>
20#include <linux/dm-kcopyd.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010021#include <linux/dm-region-hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Alasdair G Kergon72d94862006-06-26 00:27:35 -070023#define DM_MSG_PREFIX "raid1"
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010024
25#define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
Milan Broz88be1632007-05-09 02:33:04 -070026#define DM_IO_PAGES 64
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010027#define DM_KCOPYD_PAGES 64
Alasdair G Kergon72d94862006-06-26 00:27:35 -070028
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070029#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010030#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070031
Jonathan E Brassow33184042006-11-08 17:44:44 -080032static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070035 * Mirror set structures.
36 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000037enum dm_raid1_error {
38 DM_RAID1_WRITE_ERROR,
39 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;
Neil Browne4c8b3b2006-06-26 00:27:26 -070061
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010062 struct dm_region_hash *rh;
63 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070064 struct dm_io_client *io_client;
Jonathan Brassow06386bb2008-02-08 02:11:37 +000065 mempool_t *read_record_pool;
Milan Broz88be1632007-05-09 02:33:04 -070066
Neil Browne4c8b3b2006-06-26 00:27:26 -070067 /* recovery */
68 region_t nr_regions;
69 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010070 int log_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000071 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070072
Jonathan Brassow72f4b312008-02-08 02:11:29 +000073 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070074
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070075 struct workqueue_struct *kmirrord_wq;
76 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010077 struct timer_list timer;
78 unsigned long timer_pending;
79
Jonathan Brassow72f4b312008-02-08 02:11:29 +000080 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070081
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010082 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070083 struct mirror mirror[0];
84};
85
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010086static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010088 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070090 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
91}
92
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010093static void delayed_wake_fn(unsigned long data)
94{
95 struct mirror_set *ms = (struct mirror_set *) data;
96
97 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010098 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010099}
100
101static void delayed_wake(struct mirror_set *ms)
102{
103 if (test_and_set_bit(0, &ms->timer_pending))
104 return;
105
106 ms->timer.expires = jiffies + HZ / 5;
107 ms->timer.data = (unsigned long) ms;
108 ms->timer.function = delayed_wake_fn;
109 add_timer(&ms->timer);
110}
111
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100112static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100114 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100117static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100121 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100123 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
124 spin_lock_irqsave(&ms->lock, flags);
125 should_wake = !(bl->head);
126 bio_list_add(bl, bio);
127 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100130 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100133static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100135 struct mirror_set *ms = context;
136 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100138 while ((bio = bio_list_pop(bio_list)))
139 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000142#define MIN_READ_RECORDS 20
143struct dm_raid1_read_record {
144 struct mirror *m;
145 struct dm_bio_details details;
146};
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * Every mirror should look like this one.
150 */
151#define DEFAULT_MIRROR 0
152
153/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000154 * This is yucky. We squirrel the mirror struct away inside
155 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * doesn't get submitted to the lower levels of block layer.
157 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000158static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000160 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000163static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000165 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000168static struct mirror *get_default_mirror(struct mirror_set *ms)
169{
170 return &ms->mirror[atomic_read(&ms->default_mirror)];
171}
172
173static void set_default_mirror(struct mirror *m)
174{
175 struct mirror_set *ms = m->ms;
176 struct mirror *m0 = &(ms->mirror[0]);
177
178 atomic_set(&ms->default_mirror, m - m0);
179}
180
181/* fail_mirror
182 * @m: mirror device to fail
183 * @error_type: one of the enum's, DM_RAID1_*_ERROR
184 *
185 * If errors are being handled, record the type of
186 * error encountered for this device. If this type
187 * of error has already been recorded, we can return;
188 * otherwise, we must signal userspace by triggering
189 * an event. Additionally, if the device is the
190 * primary device, we must choose a new primary, but
191 * only if the mirror is in-sync.
192 *
193 * This function must not block.
194 */
195static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
196{
197 struct mirror_set *ms = m->ms;
198 struct mirror *new;
199
200 if (!errors_handled(ms))
201 return;
202
203 /*
204 * error_count is used for nothing more than a
205 * simple way to tell if a device has encountered
206 * errors.
207 */
208 atomic_inc(&m->error_count);
209
210 if (test_and_set_bit(error_type, &m->error_type))
211 return;
212
213 if (m != get_default_mirror(ms))
214 goto out;
215
216 if (!ms->in_sync) {
217 /*
218 * Better to issue requests to same failing device
219 * than to risk returning corrupt data.
220 */
221 DMERR("Primary mirror (%s) failed while out-of-sync: "
222 "Reads may fail.", m->dev->name);
223 goto out;
224 }
225
226 for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
227 if (!atomic_read(&new->error_count)) {
228 set_default_mirror(new);
229 break;
230 }
231
232 if (unlikely(new == ms->mirror + ms->nr_mirrors))
233 DMWARN("All sides of mirror have failed.");
234
235out:
236 schedule_work(&ms->trigger_event);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*-----------------------------------------------------------------
240 * Recovery.
241 *
242 * When a mirror is first activated we may find that some regions
243 * are in the no-sync state. We have to recover these by
244 * recopying from the default mirror to all the others.
245 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700246static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 void *context)
248{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100249 struct dm_region *reg = context;
250 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000251 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000253 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100254 /* Read error means the failure of default mirror. */
255 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000256 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
257 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100258
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000259 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700260 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100261 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000262 /*
263 * Bits correspond to devices (excluding default mirror).
264 * The default mirror cannot change during recovery.
265 */
266 for (m = 0; m < ms->nr_mirrors; m++) {
267 if (&ms->mirror[m] == get_default_mirror(ms))
268 continue;
269 if (test_bit(bit, &write_err))
270 fail_mirror(ms->mirror + m,
271 DM_RAID1_SYNC_ERROR);
272 bit++;
273 }
274 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100275
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100276 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100279static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100282 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100283 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct mirror *m;
285 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100286 region_t key = dm_rh_get_region_key(reg);
287 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000290 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100292 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
293 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * The final region may be smaller than
296 * region_size.
297 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100298 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100300 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100302 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* fill in the destinations */
305 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000306 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 continue;
308
309 m = ms->mirror + i;
310 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100311 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 dest->count = from.count;
313 dest++;
314 }
315
316 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100317 if (!errors_handled(ms))
318 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
319
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100320 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
321 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 return r;
324}
325
326static void do_recovery(struct mirror_set *ms)
327{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100328 struct dm_region *reg;
329 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /*
333 * Start quiescing some regions.
334 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100335 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /*
338 * Copy any already quiesced regions.
339 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100340 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 r = recover(ms, reg);
342 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100343 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 /*
347 * Update the in sync flag.
348 */
349 if (!ms->in_sync &&
350 (log->type->get_sync_count(log) == ms->nr_regions)) {
351 /* the sync is complete */
352 dm_table_event(ms->ti->table);
353 ms->in_sync = 1;
354 }
355}
356
357/*-----------------------------------------------------------------
358 * Reads
359 *---------------------------------------------------------------*/
360static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
361{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000362 struct mirror *m = get_default_mirror(ms);
363
364 do {
365 if (likely(!atomic_read(&m->error_count)))
366 return m;
367
368 if (m-- == ms->mirror)
369 m += ms->nr_mirrors;
370 } while (m != get_default_mirror(ms));
371
372 return NULL;
373}
374
375static int default_ok(struct mirror *m)
376{
377 struct mirror *default_mirror = get_default_mirror(m->ms);
378
379 return !atomic_read(&default_mirror->error_count);
380}
381
382static int mirror_available(struct mirror_set *ms, struct bio *bio)
383{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100384 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
385 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000386
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100387 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000388 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
389
390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
394 * remap a buffer to a particular mirror.
395 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000396static sector_t map_sector(struct mirror *m, struct bio *bio)
397{
398 return m->offset + (bio->bi_sector - m->ms->ti->begin);
399}
400
401static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000404 bio->bi_sector = map_sector(m, bio);
405}
406
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100407static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000408 struct bio *bio)
409{
410 io->bdev = m->dev->bdev;
411 io->sector = map_sector(m, bio);
412 io->count = bio->bi_size >> 9;
413}
414
415/*-----------------------------------------------------------------
416 * Reads
417 *---------------------------------------------------------------*/
418static void read_callback(unsigned long error, void *context)
419{
420 struct bio *bio = context;
421 struct mirror *m;
422
423 m = bio_get_m(bio);
424 bio_set_m(bio, NULL);
425
426 if (likely(!error)) {
427 bio_endio(bio, 0);
428 return;
429 }
430
431 fail_mirror(m, DM_RAID1_READ_ERROR);
432
433 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
434 DMWARN_LIMIT("Read failure on mirror device %s. "
435 "Trying alternative device.",
436 m->dev->name);
437 queue_bio(m->ms, bio, bio_rw(bio));
438 return;
439 }
440
441 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
442 m->dev->name);
443 bio_endio(bio, -EIO);
444}
445
446/* Asynchronous read. */
447static void read_async_bio(struct mirror *m, struct bio *bio)
448{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100449 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000450 struct dm_io_request io_req = {
451 .bi_rw = READ,
452 .mem.type = DM_IO_BVEC,
453 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
454 .notify.fn = read_callback,
455 .notify.context = bio,
456 .client = m->ms->io_client,
457 };
458
459 map_region(&io, m, bio);
460 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100461 BUG_ON(dm_io(&io_req, 1, &io, NULL));
462}
463
464static inline int region_in_sync(struct mirror_set *ms, region_t region,
465 int may_block)
466{
467 int state = dm_rh_get_state(ms->rh, region, may_block);
468 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static void do_reads(struct mirror_set *ms, struct bio_list *reads)
472{
473 region_t region;
474 struct bio *bio;
475 struct mirror *m;
476
477 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100478 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000479 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /*
482 * We can only read balance if the region is in sync.
483 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100484 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000486 else if (m && atomic_read(&m->error_count))
487 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000489 if (likely(m))
490 read_async_bio(m, bio);
491 else
492 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494}
495
496/*-----------------------------------------------------------------
497 * Writes.
498 *
499 * We do different things with the write io depending on the
500 * state of the region that it's in:
501 *
502 * SYNC: increment pending, use kcopyd to write to *all* mirrors
503 * RECOVERING: delay the io until recovery completes
504 * NOSYNC: increment pending, just write to the default mirror
505 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000506
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508static void write_callback(unsigned long error, void *context)
509{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000510 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct bio *bio = (struct bio *) context;
512 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000513 int uptodate = 0;
514 int should_wake = 0;
515 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000517 ms = bio_get_m(bio)->ms;
518 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
521 * NOTE: We don't decrement the pending count here,
522 * instead it is done by the targets endio function.
523 * This way we handle both writes to SYNC and NOSYNC
524 * regions with the same code.
525 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000526 if (likely(!error))
527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000529 for (i = 0; i < ms->nr_mirrors; i++)
530 if (test_bit(i, &error))
531 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
532 else
533 uptodate = 1;
534
535 if (unlikely(!uptodate)) {
536 DMERR("All replicated volumes dead, failing I/O");
537 /* None of the writes succeeded, fail the I/O. */
538 ret = -EIO;
539 } else if (errors_handled(ms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000541 * Need to raise event. Since raising
542 * events can block, we need to do it in
543 * the main thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000545 spin_lock_irqsave(&ms->lock, flags);
546 if (!ms->failures.head)
547 should_wake = 1;
548 bio_list_add(&ms->failures, bio);
549 spin_unlock_irqrestore(&ms->lock, flags);
550 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100551 wakeup_mirrord(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000554out:
555 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static void do_write(struct mirror_set *ms, struct bio *bio)
559{
560 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100561 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700563 struct dm_io_request io_req = {
564 .bi_rw = WRITE,
565 .mem.type = DM_IO_BVEC,
566 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
567 .notify.fn = write_callback,
568 .notify.context = bio,
569 .client = ms->io_client,
570 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000572 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
573 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000575 /*
576 * Use default mirror because we only need it to retrieve the reference
577 * to the mirror set in write_callback().
578 */
579 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700580
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100581 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static void do_writes(struct mirror_set *ms, struct bio_list *writes)
585{
586 int state;
587 struct bio *bio;
588 struct bio_list sync, nosync, recover, *this_list = NULL;
589
590 if (!writes->head)
591 return;
592
593 /*
594 * Classify each write.
595 */
596 bio_list_init(&sync);
597 bio_list_init(&nosync);
598 bio_list_init(&recover);
599
600 while ((bio = bio_list_pop(writes))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100601 state = dm_rh_get_state(ms->rh,
602 dm_rh_bio_to_region(ms->rh, bio), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100604 case DM_RH_CLEAN:
605 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 this_list = &sync;
607 break;
608
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100609 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 this_list = &nosync;
611 break;
612
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100613 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 this_list = &recover;
615 break;
616 }
617
618 bio_list_add(this_list, bio);
619 }
620
621 /*
622 * Increment the pending counts for any regions that will
623 * be written to (writes to recover regions are going to
624 * be delayed).
625 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100626 dm_rh_inc_pending(ms->rh, &sync);
627 dm_rh_inc_pending(ms->rh, &nosync);
628 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /*
631 * Dispatch io.
632 */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000633 if (unlikely(ms->log_failure)) {
634 spin_lock_irq(&ms->lock);
635 bio_list_merge(&ms->failures, &sync);
636 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100637 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000638 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100639 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000640 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100643 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 while ((bio = bio_list_pop(&nosync))) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000646 map_bio(get_default_mirror(ms), bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 generic_make_request(bio);
648 }
649}
650
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000651static void do_failures(struct mirror_set *ms, struct bio_list *failures)
652{
653 struct bio *bio;
654
655 if (!failures->head)
656 return;
657
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000658 if (!ms->log_failure) {
659 while ((bio = bio_list_pop(failures)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100660 ms->in_sync = 0;
661 dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000662 return;
663 }
664
665 /*
666 * If the log has failed, unattempted writes are being
667 * put on the failures list. We can't issue those writes
668 * until a log has been marked, so we must store them.
669 *
670 * If a 'noflush' suspend is in progress, we can requeue
671 * the I/O's to the core. This give userspace a chance
672 * to reconfigure the mirror, at which point the core
673 * will reissue the writes. If the 'noflush' flag is
674 * not set, we have no choice but to return errors.
675 *
676 * Some writes on the failures list may have been
677 * submitted before the log failure and represent a
678 * failure to write to one of the devices. It is ok
679 * for us to treat them the same and requeue them
680 * as well.
681 */
682 if (dm_noflush_suspending(ms->ti)) {
683 while ((bio = bio_list_pop(failures)))
684 bio_endio(bio, DM_ENDIO_REQUEUE);
685 return;
686 }
687
688 if (atomic_read(&ms->suspend)) {
689 while ((bio = bio_list_pop(failures)))
690 bio_endio(bio, -EIO);
691 return;
692 }
693
694 spin_lock_irq(&ms->lock);
695 bio_list_merge(&ms->failures, failures);
696 spin_unlock_irq(&ms->lock);
697
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100698 delayed_wake(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000699}
700
701static void trigger_event(struct work_struct *work)
702{
703 struct mirror_set *ms =
704 container_of(work, struct mirror_set, trigger_event);
705
706 dm_table_event(ms->ti->table);
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*-----------------------------------------------------------------
710 * kmirrord
711 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100712static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100714 struct mirror_set *ms = container_of(work, struct mirror_set,
715 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000716 struct bio_list reads, writes, failures;
717 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000719 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 reads = ms->reads;
721 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000722 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 bio_list_init(&ms->reads);
724 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000725 bio_list_init(&ms->failures);
726 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100728 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 do_recovery(ms);
730 do_reads(ms, &reads);
731 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000732 do_failures(ms, &failures);
Mikulas Patocka7ff14a32008-04-24 22:10:47 +0100733
734 dm_table_unplug_all(ms->ti->table);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*-----------------------------------------------------------------
738 * Target functions
739 *---------------------------------------------------------------*/
740static struct mirror_set *alloc_context(unsigned int nr_mirrors,
741 uint32_t region_size,
742 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100743 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 size_t len;
746 struct mirror_set *ms = NULL;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
749
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700750 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700752 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return NULL;
754 }
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_lock_init(&ms->lock);
757
758 ms->ti = ti;
759 ms->nr_mirrors = nr_mirrors;
760 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
761 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000762 ms->log_failure = 0;
763 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000764 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000766 len = sizeof(struct dm_raid1_read_record);
767 ms->read_record_pool = mempool_create_kmalloc_pool(MIN_READ_RECORDS,
768 len);
769 if (!ms->read_record_pool) {
770 ti->error = "Error creating mirror read_record_pool";
771 kfree(ms);
772 return NULL;
773 }
774
Milan Broz88be1632007-05-09 02:33:04 -0700775 ms->io_client = dm_io_client_create(DM_IO_PAGES);
776 if (IS_ERR(ms->io_client)) {
777 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000778 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700779 kfree(ms);
780 return NULL;
781 }
782
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100783 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
784 wakeup_all_recovery_waiters,
785 ms->ti->begin, MAX_RECOVERY,
786 dl, region_size, ms->nr_regions);
787 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700788 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100789 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000790 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 kfree(ms);
792 return NULL;
793 }
794
795 return ms;
796}
797
798static void free_context(struct mirror_set *ms, struct dm_target *ti,
799 unsigned int m)
800{
801 while (m--)
802 dm_put_device(ti, ms->mirror[m].dev);
803
Milan Broz88be1632007-05-09 02:33:04 -0700804 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100805 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000806 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 kfree(ms);
808}
809
810static inline int _check_region_size(struct dm_target *ti, uint32_t size)
811{
vignesh babu6f3c3f02007-10-19 22:38:44 +0100812 return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 size > ti->len);
814}
815
816static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
817 unsigned int mirror, char **argv)
818{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800819 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Andrew Morton4ee218c2006-03-27 01:17:48 -0800821 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700822 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -EINVAL;
824 }
825
826 if (dm_get_device(ti, argv[0], offset, ti->len,
827 dm_table_get_mode(ti->table),
828 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700829 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENXIO;
831 }
832
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100833 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000834 atomic_set(&(ms->mirror[mirror].error_count), 0);
835 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ms->mirror[mirror].offset = offset;
837
838 return 0;
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*
842 * Create dirty log: log_type #log_params <log_params>
843 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100844static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100845 unsigned argc, char **argv,
846 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100848 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100849 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700852 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return NULL;
854 }
855
856 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700857 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return NULL;
859 }
860
861 *args_used = 2 + param_count;
862
863 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700864 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return NULL;
866 }
867
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100868 dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700870 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return NULL;
872 }
873
874 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700875 ti->error = "Invalid region size";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100876 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return NULL;
878 }
879
880 return dl;
881}
882
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700883static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
884 unsigned *args_used)
885{
886 unsigned num_features;
887 struct dm_target *ti = ms->ti;
888
889 *args_used = 0;
890
891 if (!argc)
892 return 0;
893
894 if (sscanf(argv[0], "%u", &num_features) != 1) {
895 ti->error = "Invalid number of features";
896 return -EINVAL;
897 }
898
899 argc--;
900 argv++;
901 (*args_used)++;
902
903 if (num_features > argc) {
904 ti->error = "Not enough arguments to support feature count";
905 return -EINVAL;
906 }
907
908 if (!strcmp("handle_errors", argv[0]))
909 ms->features |= DM_RAID1_HANDLE_ERRORS;
910 else {
911 ti->error = "Unrecognised feature requested";
912 return -EINVAL;
913 }
914
915 (*args_used)++;
916
917 return 0;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * Construct a mirror mapping:
922 *
923 * log_type #log_params <log_params>
924 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700925 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * log_type is "core" or "disk"
928 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700929 *
930 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
933{
934 int r;
935 unsigned int nr_mirrors, m, args_used;
936 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100937 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 dl = create_dirty_log(ti, argc, argv, &args_used);
940 if (!dl)
941 return -EINVAL;
942
943 argv += args_used;
944 argc -= args_used;
945
946 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100947 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700948 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100949 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -EINVAL;
951 }
952
953 argv++, argc--;
954
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700955 if (argc < nr_mirrors * 2) {
956 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100957 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -EINVAL;
959 }
960
961 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
962 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100963 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -ENOMEM;
965 }
966
967 /* Get the mirror parameter sets */
968 for (m = 0; m < nr_mirrors; m++) {
969 r = get_mirror(ms, ti, m, argv);
970 if (r) {
971 free_context(ms, ti, m);
972 return r;
973 }
974 argv += 2;
975 argc -= 2;
976 }
977
978 ti->private = ms;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100979 ti->split_io = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700981 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
982 if (!ms->kmirrord_wq) {
983 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100984 r = -ENOMEM;
985 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700986 }
987 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100988 init_timer(&ms->timer);
989 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000990 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700991
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700992 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100993 if (r)
994 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700995
996 argv += args_used;
997 argc -= args_used;
998
Jonathan Brassowf44db672007-07-12 17:29:04 +0100999 /*
1000 * Any read-balancing addition depends on the
1001 * DM_RAID1_HANDLE_ERRORS flag being present.
1002 * This is because the decision to balance depends
1003 * on the sync state of a region. If the above
1004 * flag is not present, we ignore errors; and
1005 * the sync state may be inaccurate.
1006 */
1007
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001008 if (argc) {
1009 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001010 r = -EINVAL;
1011 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001012 }
1013
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001014 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001015 if (r)
1016 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001018 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001020
1021err_destroy_wq:
1022 destroy_workqueue(ms->kmirrord_wq);
1023err_free_context:
1024 free_context(ms, ti, ms->nr_mirrors);
1025 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028static void mirror_dtr(struct dm_target *ti)
1029{
1030 struct mirror_set *ms = (struct mirror_set *) ti->private;
1031
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001032 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001033 flush_workqueue(ms->kmirrord_wq);
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001034 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001035 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 free_context(ms, ti, ms->nr_mirrors);
1037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/*
1040 * Mirror mapping function
1041 */
1042static int mirror_map(struct dm_target *ti, struct bio *bio,
1043 union map_info *map_context)
1044{
1045 int r, rw = bio_rw(bio);
1046 struct mirror *m;
1047 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001048 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001049 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001052 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001053 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001055 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001058 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (r < 0 && r != -EWOULDBLOCK)
1060 return r;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001063 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001065 if (!r || (r == -EWOULDBLOCK)) {
1066 if (rw == READA)
1067 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001070 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001073 /*
1074 * The region is in-sync and we can perform reads directly.
1075 * Store enough information so we can retry if it fails.
1076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001078 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -EIO;
1080
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001081 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1082 if (likely(read_record)) {
1083 dm_bio_record(&read_record->details, bio);
1084 map_context->ptr = read_record;
1085 read_record->m = m;
1086 }
1087
1088 map_bio(m, bio);
1089
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001090 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1094 int error, union map_info *map_context)
1095{
1096 int rw = bio_rw(bio);
1097 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001098 struct mirror *m = NULL;
1099 struct dm_bio_details *bd = NULL;
1100 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
1103 * We need to dec pending if this was a write.
1104 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001105 if (rw == WRITE) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001106 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001107 return error;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001110 if (error == -EOPNOTSUPP)
1111 goto out;
1112
1113 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1114 goto out;
1115
1116 if (unlikely(error)) {
1117 if (!read_record) {
1118 /*
1119 * There wasn't enough memory to record necessary
1120 * information for a retry or there was no other
1121 * mirror in-sync.
1122 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001123 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001124 return -EIO;
1125 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001126
1127 m = read_record->m;
1128
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001129 DMERR("Mirror read failed from %s. Trying alternative device.",
1130 m->dev->name);
1131
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001132 fail_mirror(m, DM_RAID1_READ_ERROR);
1133
1134 /*
1135 * A failed read is requeued for another attempt using an intact
1136 * mirror.
1137 */
1138 if (default_ok(m) || mirror_available(ms, bio)) {
1139 bd = &read_record->details;
1140
1141 dm_bio_restore(bd, bio);
1142 mempool_free(read_record, ms->read_record_pool);
1143 map_context->ptr = NULL;
1144 queue_bio(ms, bio, rw);
1145 return 1;
1146 }
1147 DMERR("All replicated volumes dead, failing I/O");
1148 }
1149
1150out:
1151 if (read_record) {
1152 mempool_free(read_record, ms->read_record_pool);
1153 map_context->ptr = NULL;
1154 }
1155
1156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001159static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001162 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001164 atomic_set(&ms->suspend, 1);
1165
1166 /*
1167 * We must finish up all the work that we've
1168 * generated (i.e. recovery work).
1169 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001170 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001171
Jonathan E Brassow33184042006-11-08 17:44:44 -08001172 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001173 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001174
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001175 if (log->type->presuspend && log->type->presuspend(log))
1176 /* FIXME: need better error handling */
1177 DMWARN("log presuspend failed");
1178
1179 /*
1180 * Now that recovery is complete/stopped and the
1181 * delayed bios are queued, we need to wait for
1182 * the worker thread to complete. This way,
1183 * we know that all of our I/O has been pushed.
1184 */
1185 flush_workqueue(ms->kmirrord_wq);
1186}
1187
1188static void mirror_postsuspend(struct dm_target *ti)
1189{
1190 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001191 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001192
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001193 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001195 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198static void mirror_resume(struct dm_target *ti)
1199{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001200 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001201 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001202
1203 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (log->type->resume && log->type->resume(log))
1205 /* FIXME: need better error handling */
1206 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001207 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001210/*
1211 * device_status_char
1212 * @m: mirror device/leg we want the status of
1213 *
1214 * We return one character representing the most severe error
1215 * we have encountered.
1216 * A => Alive - No failures
1217 * D => Dead - A write failure occurred leaving mirror out-of-sync
1218 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1219 * R => Read - A read failure occurred, mirror data unaffected
1220 *
1221 * Returns: <char>
1222 */
1223static char device_status_char(struct mirror *m)
1224{
1225 if (!atomic_read(&(m->error_count)))
1226 return 'A';
1227
1228 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1229 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1230 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1231}
1232
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234static int mirror_status(struct dm_target *ti, status_type_t type,
1235 char *result, unsigned int maxlen)
1236{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001237 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001239 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001240 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 switch (type) {
1243 case STATUSTYPE_INFO:
1244 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001245 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001247 buffer[m] = device_status_char(&(ms->mirror[m]));
1248 }
1249 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001251 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001252 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001253 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001254
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001255 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258
1259 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001260 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001261
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001262 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001264 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001265 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001266
1267 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1268 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
1271 return 0;
1272}
1273
1274static struct target_type mirror_target = {
1275 .name = "mirror",
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001276 .version = {1, 0, 20},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 .module = THIS_MODULE,
1278 .ctr = mirror_ctr,
1279 .dtr = mirror_dtr,
1280 .map = mirror_map,
1281 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001282 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 .postsuspend = mirror_postsuspend,
1284 .resume = mirror_resume,
1285 .status = mirror_status,
1286};
1287
1288static int __init dm_mirror_init(void)
1289{
1290 int r;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 r = dm_register_target(&mirror_target);
Heinz Mauelshagen769aef32008-04-24 21:43:09 +01001293 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001294 DMERR("Failed to register mirror target");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 return r;
1297}
1298
1299static void __exit dm_mirror_exit(void)
1300{
1301 int r;
1302
1303 r = dm_unregister_target(&mirror_target);
1304 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001305 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/* Module hooks */
1309module_init(dm_mirror_init);
1310module_exit(dm_mirror_exit);
1311
1312MODULE_DESCRIPTION(DM_NAME " mirror target");
1313MODULE_AUTHOR("Joe Thornber");
1314MODULE_LICENSE("GPL");