blob: 076fbb4e967a4a651116c029983237d942b3f1ac [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,
38 DM_RAID1_SYNC_ERROR,
39 DM_RAID1_READ_ERROR
40};
41
Neil Browne4c8b3b2006-06-26 00:27:26 -070042struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010043 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070044 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000045 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070046 struct dm_dev *dev;
47 sector_t offset;
48};
49
50struct mirror_set {
51 struct dm_target *ti;
52 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010053
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070054 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070055
Jonathan Brassow72f4b312008-02-08 02:11:29 +000056 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070057 struct bio_list reads;
58 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000059 struct bio_list failures;
Neil Browne4c8b3b2006-06-26 00:27:26 -070060
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010061 struct dm_region_hash *rh;
62 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070063 struct dm_io_client *io_client;
Jonathan Brassow06386bb2008-02-08 02:11:37 +000064 mempool_t *read_record_pool;
Milan Broz88be1632007-05-09 02:33:04 -070065
Neil Browne4c8b3b2006-06-26 00:27:26 -070066 /* recovery */
67 region_t nr_regions;
68 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010069 int log_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000070 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070071
Jonathan Brassow72f4b312008-02-08 02:11:29 +000072 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070073
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070074 struct workqueue_struct *kmirrord_wq;
75 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010076 struct timer_list timer;
77 unsigned long timer_pending;
78
Jonathan Brassow72f4b312008-02-08 02:11:29 +000079 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070080
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010081 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070082 struct mirror mirror[0];
83};
84
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010085static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010087 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070089 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
90}
91
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010092static void delayed_wake_fn(unsigned long data)
93{
94 struct mirror_set *ms = (struct mirror_set *) data;
95
96 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010097 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010098}
99
100static void delayed_wake(struct mirror_set *ms)
101{
102 if (test_and_set_bit(0, &ms->timer_pending))
103 return;
104
105 ms->timer.expires = jiffies + HZ / 5;
106 ms->timer.data = (unsigned long) ms;
107 ms->timer.function = delayed_wake_fn;
108 add_timer(&ms->timer);
109}
110
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100111static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100113 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100116static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100120 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100122 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
123 spin_lock_irqsave(&ms->lock, flags);
124 should_wake = !(bl->head);
125 bio_list_add(bl, bio);
126 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100129 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100132static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100134 struct mirror_set *ms = context;
135 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100137 while ((bio = bio_list_pop(bio_list)))
138 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000141#define MIN_READ_RECORDS 20
142struct dm_raid1_read_record {
143 struct mirror *m;
144 struct dm_bio_details details;
145};
146
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100147static struct kmem_cache *_dm_raid1_read_record_cache;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
150 * Every mirror should look like this one.
151 */
152#define DEFAULT_MIRROR 0
153
154/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000155 * This is yucky. We squirrel the mirror struct away inside
156 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * doesn't get submitted to the lower levels of block layer.
158 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000159static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000161 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000164static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000166 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000169static struct mirror *get_default_mirror(struct mirror_set *ms)
170{
171 return &ms->mirror[atomic_read(&ms->default_mirror)];
172}
173
174static void set_default_mirror(struct mirror *m)
175{
176 struct mirror_set *ms = m->ms;
177 struct mirror *m0 = &(ms->mirror[0]);
178
179 atomic_set(&ms->default_mirror, m - m0);
180}
181
182/* fail_mirror
183 * @m: mirror device to fail
184 * @error_type: one of the enum's, DM_RAID1_*_ERROR
185 *
186 * If errors are being handled, record the type of
187 * error encountered for this device. If this type
188 * of error has already been recorded, we can return;
189 * otherwise, we must signal userspace by triggering
190 * an event. Additionally, if the device is the
191 * primary device, we must choose a new primary, but
192 * only if the mirror is in-sync.
193 *
194 * This function must not block.
195 */
196static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
197{
198 struct mirror_set *ms = m->ms;
199 struct mirror *new;
200
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000201 /*
202 * error_count is used for nothing more than a
203 * simple way to tell if a device has encountered
204 * errors.
205 */
206 atomic_inc(&m->error_count);
207
208 if (test_and_set_bit(error_type, &m->error_type))
209 return;
210
Jonathan Brassowd460c652009-01-06 03:04:57 +0000211 if (!errors_handled(ms))
212 return;
213
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000214 if (m != get_default_mirror(ms))
215 goto out;
216
217 if (!ms->in_sync) {
218 /*
219 * Better to issue requests to same failing device
220 * than to risk returning corrupt data.
221 */
222 DMERR("Primary mirror (%s) failed while out-of-sync: "
223 "Reads may fail.", m->dev->name);
224 goto out;
225 }
226
227 for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
228 if (!atomic_read(&new->error_count)) {
229 set_default_mirror(new);
230 break;
231 }
232
233 if (unlikely(new == ms->mirror + ms->nr_mirrors))
234 DMWARN("All sides of mirror have failed.");
235
236out:
237 schedule_work(&ms->trigger_event);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*-----------------------------------------------------------------
241 * Recovery.
242 *
243 * When a mirror is first activated we may find that some regions
244 * are in the no-sync state. We have to recover these by
245 * recopying from the default mirror to all the others.
246 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700247static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 void *context)
249{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100250 struct dm_region *reg = context;
251 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000252 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000254 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100255 /* Read error means the failure of default mirror. */
256 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000257 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
258 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100259
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000260 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700261 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100262 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000263 /*
264 * Bits correspond to devices (excluding default mirror).
265 * The default mirror cannot change during recovery.
266 */
267 for (m = 0; m < ms->nr_mirrors; m++) {
268 if (&ms->mirror[m] == get_default_mirror(ms))
269 continue;
270 if (test_bit(bit, &write_err))
271 fail_mirror(ms->mirror + m,
272 DM_RAID1_SYNC_ERROR);
273 bit++;
274 }
275 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100276
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100277 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100280static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100283 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100284 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct mirror *m;
286 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100287 region_t key = dm_rh_get_region_key(reg);
288 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000291 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100293 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
294 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /*
296 * The final region may be smaller than
297 * region_size.
298 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100299 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100301 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100303 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* fill in the destinations */
306 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000307 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 continue;
309
310 m = ms->mirror + i;
311 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100312 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 dest->count = from.count;
314 dest++;
315 }
316
317 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100318 if (!errors_handled(ms))
319 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
320
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100321 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
322 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 return r;
325}
326
327static void do_recovery(struct mirror_set *ms)
328{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100329 struct dm_region *reg;
330 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
334 * Start quiescing some regions.
335 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100336 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
339 * Copy any already quiesced regions.
340 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100341 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 r = recover(ms, reg);
343 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100344 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /*
348 * Update the in sync flag.
349 */
350 if (!ms->in_sync &&
351 (log->type->get_sync_count(log) == ms->nr_regions)) {
352 /* the sync is complete */
353 dm_table_event(ms->ti->table);
354 ms->in_sync = 1;
355 }
356}
357
358/*-----------------------------------------------------------------
359 * Reads
360 *---------------------------------------------------------------*/
361static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
362{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000363 struct mirror *m = get_default_mirror(ms);
364
365 do {
366 if (likely(!atomic_read(&m->error_count)))
367 return m;
368
369 if (m-- == ms->mirror)
370 m += ms->nr_mirrors;
371 } while (m != get_default_mirror(ms));
372
373 return NULL;
374}
375
376static int default_ok(struct mirror *m)
377{
378 struct mirror *default_mirror = get_default_mirror(m->ms);
379
380 return !atomic_read(&default_mirror->error_count);
381}
382
383static int mirror_available(struct mirror_set *ms, struct bio *bio)
384{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100385 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
386 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000387
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100388 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000389 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
390
391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * remap a buffer to a particular mirror.
396 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000397static sector_t map_sector(struct mirror *m, struct bio *bio)
398{
399 return m->offset + (bio->bi_sector - m->ms->ti->begin);
400}
401
402static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000405 bio->bi_sector = map_sector(m, bio);
406}
407
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100408static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000409 struct bio *bio)
410{
411 io->bdev = m->dev->bdev;
412 io->sector = map_sector(m, bio);
413 io->count = bio->bi_size >> 9;
414}
415
416/*-----------------------------------------------------------------
417 * Reads
418 *---------------------------------------------------------------*/
419static void read_callback(unsigned long error, void *context)
420{
421 struct bio *bio = context;
422 struct mirror *m;
423
424 m = bio_get_m(bio);
425 bio_set_m(bio, NULL);
426
427 if (likely(!error)) {
428 bio_endio(bio, 0);
429 return;
430 }
431
432 fail_mirror(m, DM_RAID1_READ_ERROR);
433
434 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
435 DMWARN_LIMIT("Read failure on mirror device %s. "
436 "Trying alternative device.",
437 m->dev->name);
438 queue_bio(m->ms, bio, bio_rw(bio));
439 return;
440 }
441
442 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
443 m->dev->name);
444 bio_endio(bio, -EIO);
445}
446
447/* Asynchronous read. */
448static void read_async_bio(struct mirror *m, struct bio *bio)
449{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100450 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000451 struct dm_io_request io_req = {
452 .bi_rw = READ,
453 .mem.type = DM_IO_BVEC,
454 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
455 .notify.fn = read_callback,
456 .notify.context = bio,
457 .client = m->ms->io_client,
458 };
459
460 map_region(&io, m, bio);
461 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100462 BUG_ON(dm_io(&io_req, 1, &io, NULL));
463}
464
465static inline int region_in_sync(struct mirror_set *ms, region_t region,
466 int may_block)
467{
468 int state = dm_rh_get_state(ms->rh, region, may_block);
469 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472static void do_reads(struct mirror_set *ms, struct bio_list *reads)
473{
474 region_t region;
475 struct bio *bio;
476 struct mirror *m;
477
478 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100479 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000480 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /*
483 * We can only read balance if the region is in sync.
484 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100485 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000487 else if (m && atomic_read(&m->error_count))
488 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000490 if (likely(m))
491 read_async_bio(m, bio);
492 else
493 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495}
496
497/*-----------------------------------------------------------------
498 * Writes.
499 *
500 * We do different things with the write io depending on the
501 * state of the region that it's in:
502 *
503 * SYNC: increment pending, use kcopyd to write to *all* mirrors
504 * RECOVERING: delay the io until recovery completes
505 * NOSYNC: increment pending, just write to the default mirror
506 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000507
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static void write_callback(unsigned long error, void *context)
510{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000511 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct bio *bio = (struct bio *) context;
513 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000514 int uptodate = 0;
515 int should_wake = 0;
516 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000518 ms = bio_get_m(bio)->ms;
519 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /*
522 * NOTE: We don't decrement the pending count here,
523 * instead it is done by the targets endio function.
524 * This way we handle both writes to SYNC and NOSYNC
525 * regions with the same code.
526 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000527 if (likely(!error))
528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000530 for (i = 0; i < ms->nr_mirrors; i++)
531 if (test_bit(i, &error))
532 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
533 else
534 uptodate = 1;
535
536 if (unlikely(!uptodate)) {
537 DMERR("All replicated volumes dead, failing I/O");
538 /* None of the writes succeeded, fail the I/O. */
539 ret = -EIO;
540 } else if (errors_handled(ms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000542 * Need to raise event. Since raising
543 * events can block, we need to do it in
544 * the main thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000546 spin_lock_irqsave(&ms->lock, flags);
547 if (!ms->failures.head)
548 should_wake = 1;
549 bio_list_add(&ms->failures, bio);
550 spin_unlock_irqrestore(&ms->lock, flags);
551 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100552 wakeup_mirrord(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000555out:
556 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static void do_write(struct mirror_set *ms, struct bio *bio)
560{
561 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100562 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700564 struct dm_io_request io_req = {
565 .bi_rw = WRITE,
566 .mem.type = DM_IO_BVEC,
567 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
568 .notify.fn = write_callback,
569 .notify.context = bio,
570 .client = ms->io_client,
571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000573 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
574 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000576 /*
577 * Use default mirror because we only need it to retrieve the reference
578 * to the mirror set in write_callback().
579 */
580 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700581
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100582 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static void do_writes(struct mirror_set *ms, struct bio_list *writes)
586{
587 int state;
588 struct bio *bio;
589 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100590 struct bio_list requeue;
591 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
592 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (!writes->head)
595 return;
596
597 /*
598 * Classify each write.
599 */
600 bio_list_init(&sync);
601 bio_list_init(&nosync);
602 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100603 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 while ((bio = bio_list_pop(writes))) {
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100606 region = dm_rh_bio_to_region(ms->rh, bio);
607
608 if (log->type->is_remote_recovering &&
609 log->type->is_remote_recovering(log, region)) {
610 bio_list_add(&requeue, bio);
611 continue;
612 }
613
614 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100616 case DM_RH_CLEAN:
617 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 this_list = &sync;
619 break;
620
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100621 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 this_list = &nosync;
623 break;
624
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100625 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 this_list = &recover;
627 break;
628 }
629
630 bio_list_add(this_list, bio);
631 }
632
633 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100634 * Add bios that are delayed due to remote recovery
635 * back on to the write queue
636 */
637 if (unlikely(requeue.head)) {
638 spin_lock_irq(&ms->lock);
639 bio_list_merge(&ms->writes, &requeue);
640 spin_unlock_irq(&ms->lock);
641 }
642
643 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * Increment the pending counts for any regions that will
645 * be written to (writes to recover regions are going to
646 * be delayed).
647 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100648 dm_rh_inc_pending(ms->rh, &sync);
649 dm_rh_inc_pending(ms->rh, &nosync);
650 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /*
653 * Dispatch io.
654 */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000655 if (unlikely(ms->log_failure)) {
656 spin_lock_irq(&ms->lock);
657 bio_list_merge(&ms->failures, &sync);
658 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100659 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000660 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100661 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000662 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100665 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 while ((bio = bio_list_pop(&nosync))) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000668 map_bio(get_default_mirror(ms), bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 generic_make_request(bio);
670 }
671}
672
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000673static void do_failures(struct mirror_set *ms, struct bio_list *failures)
674{
675 struct bio *bio;
676
677 if (!failures->head)
678 return;
679
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000680 if (!ms->log_failure) {
Ilpo Jarvinenb34578a2008-10-30 13:33:07 +0000681 while ((bio = bio_list_pop(failures))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100682 ms->in_sync = 0;
683 dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
Ilpo Jarvinenb34578a2008-10-30 13:33:07 +0000684 }
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000685 return;
686 }
687
688 /*
689 * If the log has failed, unattempted writes are being
690 * put on the failures list. We can't issue those writes
691 * until a log has been marked, so we must store them.
692 *
693 * If a 'noflush' suspend is in progress, we can requeue
694 * the I/O's to the core. This give userspace a chance
695 * to reconfigure the mirror, at which point the core
696 * will reissue the writes. If the 'noflush' flag is
697 * not set, we have no choice but to return errors.
698 *
699 * Some writes on the failures list may have been
700 * submitted before the log failure and represent a
701 * failure to write to one of the devices. It is ok
702 * for us to treat them the same and requeue them
703 * as well.
704 */
705 if (dm_noflush_suspending(ms->ti)) {
706 while ((bio = bio_list_pop(failures)))
707 bio_endio(bio, DM_ENDIO_REQUEUE);
708 return;
709 }
710
711 if (atomic_read(&ms->suspend)) {
712 while ((bio = bio_list_pop(failures)))
713 bio_endio(bio, -EIO);
714 return;
715 }
716
717 spin_lock_irq(&ms->lock);
718 bio_list_merge(&ms->failures, failures);
719 spin_unlock_irq(&ms->lock);
720
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100721 delayed_wake(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000722}
723
724static void trigger_event(struct work_struct *work)
725{
726 struct mirror_set *ms =
727 container_of(work, struct mirror_set, trigger_event);
728
729 dm_table_event(ms->ti->table);
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*-----------------------------------------------------------------
733 * kmirrord
734 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100735static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100737 struct mirror_set *ms = container_of(work, struct mirror_set,
738 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000739 struct bio_list reads, writes, failures;
740 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000742 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 reads = ms->reads;
744 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000745 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 bio_list_init(&ms->reads);
747 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000748 bio_list_init(&ms->failures);
749 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100751 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 do_recovery(ms);
753 do_reads(ms, &reads);
754 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000755 do_failures(ms, &failures);
Mikulas Patocka7ff14a32008-04-24 22:10:47 +0100756
757 dm_table_unplug_all(ms->ti->table);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*-----------------------------------------------------------------
761 * Target functions
762 *---------------------------------------------------------------*/
763static struct mirror_set *alloc_context(unsigned int nr_mirrors,
764 uint32_t region_size,
765 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100766 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 size_t len;
769 struct mirror_set *ms = NULL;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
772
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700773 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700775 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return NULL;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 spin_lock_init(&ms->lock);
780
781 ms->ti = ti;
782 ms->nr_mirrors = nr_mirrors;
783 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
784 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000785 ms->log_failure = 0;
786 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000787 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100789 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
790 _dm_raid1_read_record_cache);
791
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000792 if (!ms->read_record_pool) {
793 ti->error = "Error creating mirror read_record_pool";
794 kfree(ms);
795 return NULL;
796 }
797
Milan Broz88be1632007-05-09 02:33:04 -0700798 ms->io_client = dm_io_client_create(DM_IO_PAGES);
799 if (IS_ERR(ms->io_client)) {
800 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000801 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700802 kfree(ms);
803 return NULL;
804 }
805
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100806 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
807 wakeup_all_recovery_waiters,
808 ms->ti->begin, MAX_RECOVERY,
809 dl, region_size, ms->nr_regions);
810 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700811 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100812 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000813 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 kfree(ms);
815 return NULL;
816 }
817
818 return ms;
819}
820
821static void free_context(struct mirror_set *ms, struct dm_target *ti,
822 unsigned int m)
823{
824 while (m--)
825 dm_put_device(ti, ms->mirror[m].dev);
826
Milan Broz88be1632007-05-09 02:33:04 -0700827 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100828 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000829 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 kfree(ms);
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
834 unsigned int mirror, char **argv)
835{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800836 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Andrew Morton4ee218c2006-03-27 01:17:48 -0800838 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700839 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EINVAL;
841 }
842
843 if (dm_get_device(ti, argv[0], offset, ti->len,
844 dm_table_get_mode(ti->table),
845 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700846 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -ENXIO;
848 }
849
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100850 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000851 atomic_set(&(ms->mirror[mirror].error_count), 0);
852 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ms->mirror[mirror].offset = offset;
854
855 return 0;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*
859 * Create dirty log: log_type #log_params <log_params>
860 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100861static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100862 unsigned argc, char **argv,
863 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100865 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100866 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700869 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return NULL;
871 }
872
873 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700874 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return NULL;
876 }
877
878 *args_used = 2 + param_count;
879
880 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700881 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return NULL;
883 }
884
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100885 dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700887 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return NULL;
889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return dl;
892}
893
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700894static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
895 unsigned *args_used)
896{
897 unsigned num_features;
898 struct dm_target *ti = ms->ti;
899
900 *args_used = 0;
901
902 if (!argc)
903 return 0;
904
905 if (sscanf(argv[0], "%u", &num_features) != 1) {
906 ti->error = "Invalid number of features";
907 return -EINVAL;
908 }
909
910 argc--;
911 argv++;
912 (*args_used)++;
913
914 if (num_features > argc) {
915 ti->error = "Not enough arguments to support feature count";
916 return -EINVAL;
917 }
918
919 if (!strcmp("handle_errors", argv[0]))
920 ms->features |= DM_RAID1_HANDLE_ERRORS;
921 else {
922 ti->error = "Unrecognised feature requested";
923 return -EINVAL;
924 }
925
926 (*args_used)++;
927
928 return 0;
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * Construct a mirror mapping:
933 *
934 * log_type #log_params <log_params>
935 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700936 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
938 * log_type is "core" or "disk"
939 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700940 *
941 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
944{
945 int r;
946 unsigned int nr_mirrors, m, args_used;
947 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100948 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 dl = create_dirty_log(ti, argc, argv, &args_used);
951 if (!dl)
952 return -EINVAL;
953
954 argv += args_used;
955 argc -= args_used;
956
957 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100958 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700959 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100960 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EINVAL;
962 }
963
964 argv++, argc--;
965
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700966 if (argc < nr_mirrors * 2) {
967 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100968 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -EINVAL;
970 }
971
972 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
973 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100974 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -ENOMEM;
976 }
977
978 /* Get the mirror parameter sets */
979 for (m = 0; m < nr_mirrors; m++) {
980 r = get_mirror(ms, ti, m, argv);
981 if (r) {
982 free_context(ms, ti, m);
983 return r;
984 }
985 argv += 2;
986 argc -= 2;
987 }
988
989 ti->private = ms;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100990 ti->split_io = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700992 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
993 if (!ms->kmirrord_wq) {
994 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100995 r = -ENOMEM;
996 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700997 }
998 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100999 init_timer(&ms->timer);
1000 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001001 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001002
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001003 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001004 if (r)
1005 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001006
1007 argv += args_used;
1008 argc -= args_used;
1009
Jonathan Brassowf44db672007-07-12 17:29:04 +01001010 /*
1011 * Any read-balancing addition depends on the
1012 * DM_RAID1_HANDLE_ERRORS flag being present.
1013 * This is because the decision to balance depends
1014 * on the sync state of a region. If the above
1015 * flag is not present, we ignore errors; and
1016 * the sync state may be inaccurate.
1017 */
1018
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001019 if (argc) {
1020 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001021 r = -EINVAL;
1022 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001023 }
1024
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001025 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001026 if (r)
1027 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001029 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001031
1032err_destroy_wq:
1033 destroy_workqueue(ms->kmirrord_wq);
1034err_free_context:
1035 free_context(ms, ti, ms->nr_mirrors);
1036 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static void mirror_dtr(struct dm_target *ti)
1040{
1041 struct mirror_set *ms = (struct mirror_set *) ti->private;
1042
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001043 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001044 flush_workqueue(ms->kmirrord_wq);
Mikulas Patocka18776c72008-11-13 23:38:52 +00001045 flush_scheduled_work();
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001046 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001047 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 free_context(ms, ti, ms->nr_mirrors);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * Mirror mapping function
1053 */
1054static int mirror_map(struct dm_target *ti, struct bio *bio,
1055 union map_info *map_context)
1056{
1057 int r, rw = bio_rw(bio);
1058 struct mirror *m;
1059 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001060 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001061 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001064 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001065 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001067 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001070 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (r < 0 && r != -EWOULDBLOCK)
1072 return r;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001075 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001077 if (!r || (r == -EWOULDBLOCK)) {
1078 if (rw == READA)
1079 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001082 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001085 /*
1086 * The region is in-sync and we can perform reads directly.
1087 * Store enough information so we can retry if it fails.
1088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001090 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -EIO;
1092
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001093 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1094 if (likely(read_record)) {
1095 dm_bio_record(&read_record->details, bio);
1096 map_context->ptr = read_record;
1097 read_record->m = m;
1098 }
1099
1100 map_bio(m, bio);
1101
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001102 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1106 int error, union map_info *map_context)
1107{
1108 int rw = bio_rw(bio);
1109 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001110 struct mirror *m = NULL;
1111 struct dm_bio_details *bd = NULL;
1112 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /*
1115 * We need to dec pending if this was a write.
1116 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001117 if (rw == WRITE) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001118 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001119 return error;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001122 if (error == -EOPNOTSUPP)
1123 goto out;
1124
1125 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1126 goto out;
1127
1128 if (unlikely(error)) {
1129 if (!read_record) {
1130 /*
1131 * There wasn't enough memory to record necessary
1132 * information for a retry or there was no other
1133 * mirror in-sync.
1134 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001135 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001136 return -EIO;
1137 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001138
1139 m = read_record->m;
1140
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001141 DMERR("Mirror read failed from %s. Trying alternative device.",
1142 m->dev->name);
1143
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001144 fail_mirror(m, DM_RAID1_READ_ERROR);
1145
1146 /*
1147 * A failed read is requeued for another attempt using an intact
1148 * mirror.
1149 */
1150 if (default_ok(m) || mirror_available(ms, bio)) {
1151 bd = &read_record->details;
1152
1153 dm_bio_restore(bd, bio);
1154 mempool_free(read_record, ms->read_record_pool);
1155 map_context->ptr = NULL;
1156 queue_bio(ms, bio, rw);
1157 return 1;
1158 }
1159 DMERR("All replicated volumes dead, failing I/O");
1160 }
1161
1162out:
1163 if (read_record) {
1164 mempool_free(read_record, ms->read_record_pool);
1165 map_context->ptr = NULL;
1166 }
1167
1168 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001171static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001174 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001176 atomic_set(&ms->suspend, 1);
1177
1178 /*
1179 * We must finish up all the work that we've
1180 * generated (i.e. recovery work).
1181 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001182 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001183
Jonathan E Brassow33184042006-11-08 17:44:44 -08001184 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001185 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001186
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001187 if (log->type->presuspend && log->type->presuspend(log))
1188 /* FIXME: need better error handling */
1189 DMWARN("log presuspend failed");
1190
1191 /*
1192 * Now that recovery is complete/stopped and the
1193 * delayed bios are queued, we need to wait for
1194 * the worker thread to complete. This way,
1195 * we know that all of our I/O has been pushed.
1196 */
1197 flush_workqueue(ms->kmirrord_wq);
1198}
1199
1200static void mirror_postsuspend(struct dm_target *ti)
1201{
1202 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001203 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001204
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001205 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001207 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static void mirror_resume(struct dm_target *ti)
1211{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001212 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001213 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001214
1215 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (log->type->resume && log->type->resume(log))
1217 /* FIXME: need better error handling */
1218 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001219 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001222/*
1223 * device_status_char
1224 * @m: mirror device/leg we want the status of
1225 *
1226 * We return one character representing the most severe error
1227 * we have encountered.
1228 * A => Alive - No failures
1229 * D => Dead - A write failure occurred leaving mirror out-of-sync
1230 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1231 * R => Read - A read failure occurred, mirror data unaffected
1232 *
1233 * Returns: <char>
1234 */
1235static char device_status_char(struct mirror *m)
1236{
1237 if (!atomic_read(&(m->error_count)))
1238 return 'A';
1239
1240 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1241 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1242 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1243}
1244
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static int mirror_status(struct dm_target *ti, status_type_t type,
1247 char *result, unsigned int maxlen)
1248{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001249 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001251 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001252 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 switch (type) {
1255 case STATUSTYPE_INFO:
1256 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001257 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001259 buffer[m] = device_status_char(&(ms->mirror[m]));
1260 }
1261 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001263 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001264 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001265 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001266
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001267 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
1270
1271 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001272 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001273
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001274 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001276 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001277 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001278
1279 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1280 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282
1283 return 0;
1284}
1285
1286static struct target_type mirror_target = {
1287 .name = "mirror",
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001288 .version = {1, 0, 20},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 .module = THIS_MODULE,
1290 .ctr = mirror_ctr,
1291 .dtr = mirror_dtr,
1292 .map = mirror_map,
1293 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001294 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 .postsuspend = mirror_postsuspend,
1296 .resume = mirror_resume,
1297 .status = mirror_status,
1298};
1299
1300static int __init dm_mirror_init(void)
1301{
1302 int r;
1303
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001304 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1305 if (!_dm_raid1_read_record_cache) {
1306 DMERR("Can't allocate dm_raid1_read_record cache");
1307 r = -ENOMEM;
1308 goto bad_cache;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001311 r = dm_register_target(&mirror_target);
1312 if (r < 0) {
1313 DMERR("Failed to register mirror target");
1314 goto bad_target;
1315 }
1316
1317 return 0;
1318
1319bad_target:
1320 kmem_cache_destroy(_dm_raid1_read_record_cache);
1321bad_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return r;
1323}
1324
1325static void __exit dm_mirror_exit(void)
1326{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001327 dm_unregister_target(&mirror_target);
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001328 kmem_cache_destroy(_dm_raid1_read_record_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331/* Module hooks */
1332module_init(dm_mirror_init);
1333module_exit(dm_mirror_exit);
1334
1335MODULE_DESCRIPTION(DM_NAME " mirror target");
1336MODULE_AUTHOR("Joe Thornber");
1337MODULE_LICENSE("GPL");