blob: 751660b0c5741547dae116d81a167a73bbb4fd4c [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
Mikulas Patockac0da3742009-12-10 23:52:02 +0000240static int mirror_flush(struct dm_target *ti)
241{
242 struct mirror_set *ms = ti->private;
243 unsigned long error_bits;
244
245 unsigned int i;
246 struct dm_io_region io[ms->nr_mirrors];
247 struct mirror *m;
248 struct dm_io_request io_req = {
249 .bi_rw = WRITE_BARRIER,
250 .mem.type = DM_IO_KMEM,
251 .mem.ptr.bvec = NULL,
252 .client = ms->io_client,
253 };
254
255 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
256 io[i].bdev = m->dev->bdev;
257 io[i].sector = 0;
258 io[i].count = 0;
259 }
260
261 error_bits = -1;
262 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
263 if (unlikely(error_bits != 0)) {
264 for (i = 0; i < ms->nr_mirrors; i++)
265 if (test_bit(i, &error_bits))
266 fail_mirror(ms->mirror + i,
267 DM_RAID1_WRITE_ERROR);
268 return -EIO;
269 }
270
271 return 0;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*-----------------------------------------------------------------
275 * Recovery.
276 *
277 * When a mirror is first activated we may find that some regions
278 * are in the no-sync state. We have to recover these by
279 * recopying from the default mirror to all the others.
280 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700281static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 void *context)
283{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100284 struct dm_region *reg = context;
285 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000286 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000288 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100289 /* Read error means the failure of default mirror. */
290 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000291 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
292 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100293
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000294 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700295 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100296 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000297 /*
298 * Bits correspond to devices (excluding default mirror).
299 * The default mirror cannot change during recovery.
300 */
301 for (m = 0; m < ms->nr_mirrors; m++) {
302 if (&ms->mirror[m] == get_default_mirror(ms))
303 continue;
304 if (test_bit(bit, &write_err))
305 fail_mirror(ms->mirror + m,
306 DM_RAID1_SYNC_ERROR);
307 bit++;
308 }
309 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100310
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100311 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100314static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100317 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100318 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct mirror *m;
320 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100321 region_t key = dm_rh_get_region_key(reg);
322 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000325 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100327 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
328 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
330 * The final region may be smaller than
331 * region_size.
332 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100333 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100335 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100337 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* fill in the destinations */
340 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000341 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 continue;
343
344 m = ms->mirror + i;
345 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100346 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dest->count = from.count;
348 dest++;
349 }
350
351 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100352 if (!errors_handled(ms))
353 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
354
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100355 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
356 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 return r;
359}
360
361static void do_recovery(struct mirror_set *ms)
362{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100363 struct dm_region *reg;
364 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /*
368 * Start quiescing some regions.
369 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100370 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
373 * Copy any already quiesced regions.
374 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100375 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 r = recover(ms, reg);
377 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100378 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 /*
382 * Update the in sync flag.
383 */
384 if (!ms->in_sync &&
385 (log->type->get_sync_count(log) == ms->nr_regions)) {
386 /* the sync is complete */
387 dm_table_event(ms->ti->table);
388 ms->in_sync = 1;
389 }
390}
391
392/*-----------------------------------------------------------------
393 * Reads
394 *---------------------------------------------------------------*/
395static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
396{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000397 struct mirror *m = get_default_mirror(ms);
398
399 do {
400 if (likely(!atomic_read(&m->error_count)))
401 return m;
402
403 if (m-- == ms->mirror)
404 m += ms->nr_mirrors;
405 } while (m != get_default_mirror(ms));
406
407 return NULL;
408}
409
410static int default_ok(struct mirror *m)
411{
412 struct mirror *default_mirror = get_default_mirror(m->ms);
413
414 return !atomic_read(&default_mirror->error_count);
415}
416
417static int mirror_available(struct mirror_set *ms, struct bio *bio)
418{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100419 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
420 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000421
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100422 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000423 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
424
425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/*
429 * remap a buffer to a particular mirror.
430 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000431static sector_t map_sector(struct mirror *m, struct bio *bio)
432{
Mikulas Patocka41841532009-12-10 23:51:59 +0000433 if (unlikely(!bio->bi_size))
434 return 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000435 return m->offset + (bio->bi_sector - m->ms->ti->begin);
436}
437
438static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000441 bio->bi_sector = map_sector(m, bio);
442}
443
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100444static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000445 struct bio *bio)
446{
447 io->bdev = m->dev->bdev;
448 io->sector = map_sector(m, bio);
449 io->count = bio->bi_size >> 9;
450}
451
452/*-----------------------------------------------------------------
453 * Reads
454 *---------------------------------------------------------------*/
455static void read_callback(unsigned long error, void *context)
456{
457 struct bio *bio = context;
458 struct mirror *m;
459
460 m = bio_get_m(bio);
461 bio_set_m(bio, NULL);
462
463 if (likely(!error)) {
464 bio_endio(bio, 0);
465 return;
466 }
467
468 fail_mirror(m, DM_RAID1_READ_ERROR);
469
470 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
471 DMWARN_LIMIT("Read failure on mirror device %s. "
472 "Trying alternative device.",
473 m->dev->name);
474 queue_bio(m->ms, bio, bio_rw(bio));
475 return;
476 }
477
478 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
479 m->dev->name);
480 bio_endio(bio, -EIO);
481}
482
483/* Asynchronous read. */
484static void read_async_bio(struct mirror *m, struct bio *bio)
485{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100486 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000487 struct dm_io_request io_req = {
488 .bi_rw = READ,
489 .mem.type = DM_IO_BVEC,
490 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
491 .notify.fn = read_callback,
492 .notify.context = bio,
493 .client = m->ms->io_client,
494 };
495
496 map_region(&io, m, bio);
497 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100498 BUG_ON(dm_io(&io_req, 1, &io, NULL));
499}
500
501static inline int region_in_sync(struct mirror_set *ms, region_t region,
502 int may_block)
503{
504 int state = dm_rh_get_state(ms->rh, region, may_block);
505 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508static void do_reads(struct mirror_set *ms, struct bio_list *reads)
509{
510 region_t region;
511 struct bio *bio;
512 struct mirror *m;
513
514 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100515 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000516 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * We can only read balance if the region is in sync.
520 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100521 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000523 else if (m && atomic_read(&m->error_count))
524 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000526 if (likely(m))
527 read_async_bio(m, bio);
528 else
529 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531}
532
533/*-----------------------------------------------------------------
534 * Writes.
535 *
536 * We do different things with the write io depending on the
537 * state of the region that it's in:
538 *
539 * SYNC: increment pending, use kcopyd to write to *all* mirrors
540 * RECOVERING: delay the io until recovery completes
541 * NOSYNC: increment pending, just write to the default mirror
542 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000543
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static void write_callback(unsigned long error, void *context)
546{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000547 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct bio *bio = (struct bio *) context;
549 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000550 int uptodate = 0;
551 int should_wake = 0;
552 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000554 ms = bio_get_m(bio)->ms;
555 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /*
558 * NOTE: We don't decrement the pending count here,
559 * instead it is done by the targets endio function.
560 * This way we handle both writes to SYNC and NOSYNC
561 * regions with the same code.
562 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000563 if (likely(!error))
564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000566 for (i = 0; i < ms->nr_mirrors; i++)
567 if (test_bit(i, &error))
568 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
569 else
570 uptodate = 1;
571
572 if (unlikely(!uptodate)) {
573 DMERR("All replicated volumes dead, failing I/O");
574 /* None of the writes succeeded, fail the I/O. */
575 ret = -EIO;
576 } else if (errors_handled(ms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000578 * Need to raise event. Since raising
579 * events can block, we need to do it in
580 * the main thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000582 spin_lock_irqsave(&ms->lock, flags);
583 if (!ms->failures.head)
584 should_wake = 1;
585 bio_list_add(&ms->failures, bio);
586 spin_unlock_irqrestore(&ms->lock, flags);
587 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100588 wakeup_mirrord(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000591out:
592 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static void do_write(struct mirror_set *ms, struct bio *bio)
596{
597 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100598 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700600 struct dm_io_request io_req = {
Mikulas Patocka41841532009-12-10 23:51:59 +0000601 .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
Milan Broz88be1632007-05-09 02:33:04 -0700602 .mem.type = DM_IO_BVEC,
603 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
604 .notify.fn = write_callback,
605 .notify.context = bio,
606 .client = ms->io_client,
607 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000609 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
610 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000612 /*
613 * Use default mirror because we only need it to retrieve the reference
614 * to the mirror set in write_callback().
615 */
616 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700617
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100618 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static void do_writes(struct mirror_set *ms, struct bio_list *writes)
622{
623 int state;
624 struct bio *bio;
625 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100626 struct bio_list requeue;
627 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
628 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (!writes->head)
631 return;
632
633 /*
634 * Classify each write.
635 */
636 bio_list_init(&sync);
637 bio_list_init(&nosync);
638 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100639 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 while ((bio = bio_list_pop(writes))) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000642 if (unlikely(bio_empty_barrier(bio))) {
643 bio_list_add(&sync, bio);
644 continue;
645 }
646
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100647 region = dm_rh_bio_to_region(ms->rh, bio);
648
649 if (log->type->is_remote_recovering &&
650 log->type->is_remote_recovering(log, region)) {
651 bio_list_add(&requeue, bio);
652 continue;
653 }
654
655 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100657 case DM_RH_CLEAN:
658 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 this_list = &sync;
660 break;
661
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100662 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 this_list = &nosync;
664 break;
665
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100666 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 this_list = &recover;
668 break;
669 }
670
671 bio_list_add(this_list, bio);
672 }
673
674 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100675 * Add bios that are delayed due to remote recovery
676 * back on to the write queue
677 */
678 if (unlikely(requeue.head)) {
679 spin_lock_irq(&ms->lock);
680 bio_list_merge(&ms->writes, &requeue);
681 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100682 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100683 }
684
685 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 * Increment the pending counts for any regions that will
687 * be written to (writes to recover regions are going to
688 * be delayed).
689 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100690 dm_rh_inc_pending(ms->rh, &sync);
691 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100692
693 /*
694 * If the flush fails on a previous call and succeeds here,
695 * we must not reset the log_failure variable. We need
696 * userspace interaction to do that.
697 */
698 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /*
701 * Dispatch io.
702 */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000703 if (unlikely(ms->log_failure)) {
704 spin_lock_irq(&ms->lock);
705 bio_list_merge(&ms->failures, &sync);
706 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100707 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000708 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100709 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000710 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100713 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 while ((bio = bio_list_pop(&nosync))) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000716 map_bio(get_default_mirror(ms), bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 generic_make_request(bio);
718 }
719}
720
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000721static void do_failures(struct mirror_set *ms, struct bio_list *failures)
722{
723 struct bio *bio;
724
725 if (!failures->head)
726 return;
727
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000728 if (!ms->log_failure) {
Ilpo Jarvinenb34578a2008-10-30 13:33:07 +0000729 while ((bio = bio_list_pop(failures))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100730 ms->in_sync = 0;
731 dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
Ilpo Jarvinenb34578a2008-10-30 13:33:07 +0000732 }
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000733 return;
734 }
735
736 /*
737 * If the log has failed, unattempted writes are being
738 * put on the failures list. We can't issue those writes
739 * until a log has been marked, so we must store them.
740 *
741 * If a 'noflush' suspend is in progress, we can requeue
742 * the I/O's to the core. This give userspace a chance
743 * to reconfigure the mirror, at which point the core
744 * will reissue the writes. If the 'noflush' flag is
745 * not set, we have no choice but to return errors.
746 *
747 * Some writes on the failures list may have been
748 * submitted before the log failure and represent a
749 * failure to write to one of the devices. It is ok
750 * for us to treat them the same and requeue them
751 * as well.
752 */
753 if (dm_noflush_suspending(ms->ti)) {
754 while ((bio = bio_list_pop(failures)))
755 bio_endio(bio, DM_ENDIO_REQUEUE);
756 return;
757 }
758
759 if (atomic_read(&ms->suspend)) {
760 while ((bio = bio_list_pop(failures)))
761 bio_endio(bio, -EIO);
762 return;
763 }
764
765 spin_lock_irq(&ms->lock);
766 bio_list_merge(&ms->failures, failures);
767 spin_unlock_irq(&ms->lock);
768
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100769 delayed_wake(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000770}
771
772static void trigger_event(struct work_struct *work)
773{
774 struct mirror_set *ms =
775 container_of(work, struct mirror_set, trigger_event);
776
777 dm_table_event(ms->ti->table);
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*-----------------------------------------------------------------
781 * kmirrord
782 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100783static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100785 struct mirror_set *ms = container_of(work, struct mirror_set,
786 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000787 struct bio_list reads, writes, failures;
788 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000790 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 reads = ms->reads;
792 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000793 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 bio_list_init(&ms->reads);
795 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000796 bio_list_init(&ms->failures);
797 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100799 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 do_recovery(ms);
801 do_reads(ms, &reads);
802 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000803 do_failures(ms, &failures);
Mikulas Patocka7ff14a32008-04-24 22:10:47 +0100804
805 dm_table_unplug_all(ms->ti->table);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*-----------------------------------------------------------------
809 * Target functions
810 *---------------------------------------------------------------*/
811static struct mirror_set *alloc_context(unsigned int nr_mirrors,
812 uint32_t region_size,
813 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100814 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 size_t len;
817 struct mirror_set *ms = NULL;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
820
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700821 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700823 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return NULL;
825 }
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 spin_lock_init(&ms->lock);
828
829 ms->ti = ti;
830 ms->nr_mirrors = nr_mirrors;
831 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
832 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000833 ms->log_failure = 0;
834 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000835 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100837 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
838 _dm_raid1_read_record_cache);
839
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000840 if (!ms->read_record_pool) {
841 ti->error = "Error creating mirror read_record_pool";
842 kfree(ms);
843 return NULL;
844 }
845
Milan Broz88be1632007-05-09 02:33:04 -0700846 ms->io_client = dm_io_client_create(DM_IO_PAGES);
847 if (IS_ERR(ms->io_client)) {
848 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000849 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700850 kfree(ms);
851 return NULL;
852 }
853
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100854 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
855 wakeup_all_recovery_waiters,
856 ms->ti->begin, MAX_RECOVERY,
857 dl, region_size, ms->nr_regions);
858 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700859 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100860 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000861 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 kfree(ms);
863 return NULL;
864 }
865
866 return ms;
867}
868
869static void free_context(struct mirror_set *ms, struct dm_target *ti,
870 unsigned int m)
871{
872 while (m--)
873 dm_put_device(ti, ms->mirror[m].dev);
874
Milan Broz88be1632007-05-09 02:33:04 -0700875 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100876 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000877 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 kfree(ms);
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
882 unsigned int mirror, char **argv)
883{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800884 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Andrew Morton4ee218c2006-03-27 01:17:48 -0800886 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700887 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EINVAL;
889 }
890
891 if (dm_get_device(ti, argv[0], offset, ti->len,
892 dm_table_get_mode(ti->table),
893 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700894 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -ENXIO;
896 }
897
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100898 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000899 atomic_set(&(ms->mirror[mirror].error_count), 0);
900 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ms->mirror[mirror].offset = offset;
902
903 return 0;
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/*
907 * Create dirty log: log_type #log_params <log_params>
908 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100909static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100910 unsigned argc, char **argv,
911 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100913 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100914 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700917 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return NULL;
919 }
920
921 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700922 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return NULL;
924 }
925
926 *args_used = 2 + param_count;
927
928 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700929 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return NULL;
931 }
932
Mikulas Patockac0da3742009-12-10 23:52:02 +0000933 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
934 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700936 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return NULL;
938 }
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return dl;
941}
942
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700943static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
944 unsigned *args_used)
945{
946 unsigned num_features;
947 struct dm_target *ti = ms->ti;
948
949 *args_used = 0;
950
951 if (!argc)
952 return 0;
953
954 if (sscanf(argv[0], "%u", &num_features) != 1) {
955 ti->error = "Invalid number of features";
956 return -EINVAL;
957 }
958
959 argc--;
960 argv++;
961 (*args_used)++;
962
963 if (num_features > argc) {
964 ti->error = "Not enough arguments to support feature count";
965 return -EINVAL;
966 }
967
968 if (!strcmp("handle_errors", argv[0]))
969 ms->features |= DM_RAID1_HANDLE_ERRORS;
970 else {
971 ti->error = "Unrecognised feature requested";
972 return -EINVAL;
973 }
974
975 (*args_used)++;
976
977 return 0;
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
981 * Construct a mirror mapping:
982 *
983 * log_type #log_params <log_params>
984 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700985 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
987 * log_type is "core" or "disk"
988 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700989 *
990 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
993{
994 int r;
995 unsigned int nr_mirrors, m, args_used;
996 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100997 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 dl = create_dirty_log(ti, argc, argv, &args_used);
1000 if (!dl)
1001 return -EINVAL;
1002
1003 argv += args_used;
1004 argc -= args_used;
1005
1006 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001007 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001008 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001009 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EINVAL;
1011 }
1012
1013 argv++, argc--;
1014
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001015 if (argc < nr_mirrors * 2) {
1016 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001017 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EINVAL;
1019 }
1020
1021 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1022 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001023 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -ENOMEM;
1025 }
1026
1027 /* Get the mirror parameter sets */
1028 for (m = 0; m < nr_mirrors; m++) {
1029 r = get_mirror(ms, ti, m, argv);
1030 if (r) {
1031 free_context(ms, ti, m);
1032 return r;
1033 }
1034 argv += 2;
1035 argc -= 2;
1036 }
1037
1038 ti->private = ms;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001039 ti->split_io = dm_rh_get_region_size(ms->rh);
Mikulas Patocka41841532009-12-10 23:51:59 +00001040 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001042 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1043 if (!ms->kmirrord_wq) {
1044 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001045 r = -ENOMEM;
1046 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001047 }
1048 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001049 init_timer(&ms->timer);
1050 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001051 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001052
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001053 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001054 if (r)
1055 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001056
1057 argv += args_used;
1058 argc -= args_used;
1059
Jonathan Brassowf44db672007-07-12 17:29:04 +01001060 /*
1061 * Any read-balancing addition depends on the
1062 * DM_RAID1_HANDLE_ERRORS flag being present.
1063 * This is because the decision to balance depends
1064 * on the sync state of a region. If the above
1065 * flag is not present, we ignore errors; and
1066 * the sync state may be inaccurate.
1067 */
1068
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001069 if (argc) {
1070 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001071 r = -EINVAL;
1072 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001073 }
1074
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001075 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001076 if (r)
1077 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001079 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001081
1082err_destroy_wq:
1083 destroy_workqueue(ms->kmirrord_wq);
1084err_free_context:
1085 free_context(ms, ti, ms->nr_mirrors);
1086 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static void mirror_dtr(struct dm_target *ti)
1090{
1091 struct mirror_set *ms = (struct mirror_set *) ti->private;
1092
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001093 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001094 flush_workqueue(ms->kmirrord_wq);
Mikulas Patocka18776c72008-11-13 23:38:52 +00001095 flush_scheduled_work();
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001096 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001097 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 free_context(ms, ti, ms->nr_mirrors);
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/*
1102 * Mirror mapping function
1103 */
1104static int mirror_map(struct dm_target *ti, struct bio *bio,
1105 union map_info *map_context)
1106{
1107 int r, rw = bio_rw(bio);
1108 struct mirror *m;
1109 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001110 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001111 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001114 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001115 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001117 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001120 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (r < 0 && r != -EWOULDBLOCK)
1122 return r;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001125 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001127 if (!r || (r == -EWOULDBLOCK)) {
1128 if (rw == READA)
1129 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001132 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001135 /*
1136 * The region is in-sync and we can perform reads directly.
1137 * Store enough information so we can retry if it fails.
1138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001140 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return -EIO;
1142
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001143 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1144 if (likely(read_record)) {
1145 dm_bio_record(&read_record->details, bio);
1146 map_context->ptr = read_record;
1147 read_record->m = m;
1148 }
1149
1150 map_bio(m, bio);
1151
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001152 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1156 int error, union map_info *map_context)
1157{
1158 int rw = bio_rw(bio);
1159 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001160 struct mirror *m = NULL;
1161 struct dm_bio_details *bd = NULL;
1162 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /*
1165 * We need to dec pending if this was a write.
1166 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001167 if (rw == WRITE) {
Mikulas Patocka41841532009-12-10 23:51:59 +00001168 if (likely(!bio_empty_barrier(bio)))
1169 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001170 return error;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001173 if (error == -EOPNOTSUPP)
1174 goto out;
1175
Jens Axboe1f98a132009-09-11 14:32:04 +02001176 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001177 goto out;
1178
1179 if (unlikely(error)) {
1180 if (!read_record) {
1181 /*
1182 * There wasn't enough memory to record necessary
1183 * information for a retry or there was no other
1184 * mirror in-sync.
1185 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001186 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001187 return -EIO;
1188 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001189
1190 m = read_record->m;
1191
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001192 DMERR("Mirror read failed from %s. Trying alternative device.",
1193 m->dev->name);
1194
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001195 fail_mirror(m, DM_RAID1_READ_ERROR);
1196
1197 /*
1198 * A failed read is requeued for another attempt using an intact
1199 * mirror.
1200 */
1201 if (default_ok(m) || mirror_available(ms, bio)) {
1202 bd = &read_record->details;
1203
1204 dm_bio_restore(bd, bio);
1205 mempool_free(read_record, ms->read_record_pool);
1206 map_context->ptr = NULL;
1207 queue_bio(ms, bio, rw);
1208 return 1;
1209 }
1210 DMERR("All replicated volumes dead, failing I/O");
1211 }
1212
1213out:
1214 if (read_record) {
1215 mempool_free(read_record, ms->read_record_pool);
1216 map_context->ptr = NULL;
1217 }
1218
1219 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001222static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001225 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001227 atomic_set(&ms->suspend, 1);
1228
1229 /*
1230 * We must finish up all the work that we've
1231 * generated (i.e. recovery work).
1232 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001233 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001234
Jonathan E Brassow33184042006-11-08 17:44:44 -08001235 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001236 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001237
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001238 if (log->type->presuspend && log->type->presuspend(log))
1239 /* FIXME: need better error handling */
1240 DMWARN("log presuspend failed");
1241
1242 /*
1243 * Now that recovery is complete/stopped and the
1244 * delayed bios are queued, we need to wait for
1245 * the worker thread to complete. This way,
1246 * we know that all of our I/O has been pushed.
1247 */
1248 flush_workqueue(ms->kmirrord_wq);
1249}
1250
1251static void mirror_postsuspend(struct dm_target *ti)
1252{
1253 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001254 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001255
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001256 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001258 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
1261static void mirror_resume(struct dm_target *ti)
1262{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001263 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001264 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001265
1266 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (log->type->resume && log->type->resume(log))
1268 /* FIXME: need better error handling */
1269 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001270 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001273/*
1274 * device_status_char
1275 * @m: mirror device/leg we want the status of
1276 *
1277 * We return one character representing the most severe error
1278 * we have encountered.
1279 * A => Alive - No failures
1280 * D => Dead - A write failure occurred leaving mirror out-of-sync
1281 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1282 * R => Read - A read failure occurred, mirror data unaffected
1283 *
1284 * Returns: <char>
1285 */
1286static char device_status_char(struct mirror *m)
1287{
1288 if (!atomic_read(&(m->error_count)))
1289 return 'A';
1290
1291 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1292 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1293 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1294}
1295
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297static int mirror_status(struct dm_target *ti, status_type_t type,
1298 char *result, unsigned int maxlen)
1299{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001300 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001302 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001303 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 switch (type) {
1306 case STATUSTYPE_INFO:
1307 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001308 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001310 buffer[m] = device_status_char(&(ms->mirror[m]));
1311 }
1312 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001314 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001315 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001316 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001317
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001318 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321
1322 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001323 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001324
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001325 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001327 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001328 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001329
1330 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1331 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
1334 return 0;
1335}
1336
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001337static int mirror_iterate_devices(struct dm_target *ti,
1338 iterate_devices_callout_fn fn, void *data)
1339{
1340 struct mirror_set *ms = ti->private;
1341 int ret = 0;
1342 unsigned i;
1343
1344 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1345 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001346 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001347
1348 return ret;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static struct target_type mirror_target = {
1352 .name = "mirror",
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001353 .version = {1, 12, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 .module = THIS_MODULE,
1355 .ctr = mirror_ctr,
1356 .dtr = mirror_dtr,
1357 .map = mirror_map,
1358 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001359 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 .postsuspend = mirror_postsuspend,
1361 .resume = mirror_resume,
1362 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001363 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364};
1365
1366static int __init dm_mirror_init(void)
1367{
1368 int r;
1369
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001370 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1371 if (!_dm_raid1_read_record_cache) {
1372 DMERR("Can't allocate dm_raid1_read_record cache");
1373 r = -ENOMEM;
1374 goto bad_cache;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001377 r = dm_register_target(&mirror_target);
1378 if (r < 0) {
1379 DMERR("Failed to register mirror target");
1380 goto bad_target;
1381 }
1382
1383 return 0;
1384
1385bad_target:
1386 kmem_cache_destroy(_dm_raid1_read_record_cache);
1387bad_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return r;
1389}
1390
1391static void __exit dm_mirror_exit(void)
1392{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001393 dm_unregister_target(&mirror_target);
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001394 kmem_cache_destroy(_dm_raid1_read_record_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/* Module hooks */
1398module_init(dm_mirror_init);
1399module_exit(dm_mirror_exit);
1400
1401MODULE_DESCRIPTION(DM_NAME " mirror target");
1402MODULE_AUTHOR("Joe Thornber");
1403MODULE_LICENSE("GPL");