blob: 752a29e1855b327f13ee24fa9fcfb4478fe32b29 [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{
Mikulas Patocka41841532009-12-10 23:51:59 +0000399 if (unlikely(!bio->bi_size))
400 return 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000401 return m->offset + (bio->bi_sector - m->ms->ti->begin);
402}
403
404static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000407 bio->bi_sector = map_sector(m, bio);
408}
409
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100410static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000411 struct bio *bio)
412{
413 io->bdev = m->dev->bdev;
414 io->sector = map_sector(m, bio);
415 io->count = bio->bi_size >> 9;
416}
417
418/*-----------------------------------------------------------------
419 * Reads
420 *---------------------------------------------------------------*/
421static void read_callback(unsigned long error, void *context)
422{
423 struct bio *bio = context;
424 struct mirror *m;
425
426 m = bio_get_m(bio);
427 bio_set_m(bio, NULL);
428
429 if (likely(!error)) {
430 bio_endio(bio, 0);
431 return;
432 }
433
434 fail_mirror(m, DM_RAID1_READ_ERROR);
435
436 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
437 DMWARN_LIMIT("Read failure on mirror device %s. "
438 "Trying alternative device.",
439 m->dev->name);
440 queue_bio(m->ms, bio, bio_rw(bio));
441 return;
442 }
443
444 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
445 m->dev->name);
446 bio_endio(bio, -EIO);
447}
448
449/* Asynchronous read. */
450static void read_async_bio(struct mirror *m, struct bio *bio)
451{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100452 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000453 struct dm_io_request io_req = {
454 .bi_rw = READ,
455 .mem.type = DM_IO_BVEC,
456 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
457 .notify.fn = read_callback,
458 .notify.context = bio,
459 .client = m->ms->io_client,
460 };
461
462 map_region(&io, m, bio);
463 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100464 BUG_ON(dm_io(&io_req, 1, &io, NULL));
465}
466
467static inline int region_in_sync(struct mirror_set *ms, region_t region,
468 int may_block)
469{
470 int state = dm_rh_get_state(ms->rh, region, may_block);
471 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474static void do_reads(struct mirror_set *ms, struct bio_list *reads)
475{
476 region_t region;
477 struct bio *bio;
478 struct mirror *m;
479
480 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100481 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000482 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 /*
485 * We can only read balance if the region is in sync.
486 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100487 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000489 else if (m && atomic_read(&m->error_count))
490 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000492 if (likely(m))
493 read_async_bio(m, bio);
494 else
495 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497}
498
499/*-----------------------------------------------------------------
500 * Writes.
501 *
502 * We do different things with the write io depending on the
503 * state of the region that it's in:
504 *
505 * SYNC: increment pending, use kcopyd to write to *all* mirrors
506 * RECOVERING: delay the io until recovery completes
507 * NOSYNC: increment pending, just write to the default mirror
508 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000509
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static void write_callback(unsigned long error, void *context)
512{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000513 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct bio *bio = (struct bio *) context;
515 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000516 int uptodate = 0;
517 int should_wake = 0;
518 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000520 ms = bio_get_m(bio)->ms;
521 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * NOTE: We don't decrement the pending count here,
525 * instead it is done by the targets endio function.
526 * This way we handle both writes to SYNC and NOSYNC
527 * regions with the same code.
528 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000529 if (likely(!error))
530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000532 for (i = 0; i < ms->nr_mirrors; i++)
533 if (test_bit(i, &error))
534 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
535 else
536 uptodate = 1;
537
538 if (unlikely(!uptodate)) {
539 DMERR("All replicated volumes dead, failing I/O");
540 /* None of the writes succeeded, fail the I/O. */
541 ret = -EIO;
542 } else if (errors_handled(ms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000544 * Need to raise event. Since raising
545 * events can block, we need to do it in
546 * the main thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000548 spin_lock_irqsave(&ms->lock, flags);
549 if (!ms->failures.head)
550 should_wake = 1;
551 bio_list_add(&ms->failures, bio);
552 spin_unlock_irqrestore(&ms->lock, flags);
553 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100554 wakeup_mirrord(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000557out:
558 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561static void do_write(struct mirror_set *ms, struct bio *bio)
562{
563 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100564 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700566 struct dm_io_request io_req = {
Mikulas Patocka41841532009-12-10 23:51:59 +0000567 .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
Milan Broz88be1632007-05-09 02:33:04 -0700568 .mem.type = DM_IO_BVEC,
569 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
570 .notify.fn = write_callback,
571 .notify.context = bio,
572 .client = ms->io_client,
573 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000575 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
576 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000578 /*
579 * Use default mirror because we only need it to retrieve the reference
580 * to the mirror set in write_callback().
581 */
582 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700583
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100584 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587static void do_writes(struct mirror_set *ms, struct bio_list *writes)
588{
589 int state;
590 struct bio *bio;
591 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100592 struct bio_list requeue;
593 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
594 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (!writes->head)
597 return;
598
599 /*
600 * Classify each write.
601 */
602 bio_list_init(&sync);
603 bio_list_init(&nosync);
604 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100605 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 while ((bio = bio_list_pop(writes))) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000608 if (unlikely(bio_empty_barrier(bio))) {
609 bio_list_add(&sync, bio);
610 continue;
611 }
612
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100613 region = dm_rh_bio_to_region(ms->rh, bio);
614
615 if (log->type->is_remote_recovering &&
616 log->type->is_remote_recovering(log, region)) {
617 bio_list_add(&requeue, bio);
618 continue;
619 }
620
621 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100623 case DM_RH_CLEAN:
624 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 this_list = &sync;
626 break;
627
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100628 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 this_list = &nosync;
630 break;
631
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100632 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 this_list = &recover;
634 break;
635 }
636
637 bio_list_add(this_list, bio);
638 }
639
640 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100641 * Add bios that are delayed due to remote recovery
642 * back on to the write queue
643 */
644 if (unlikely(requeue.head)) {
645 spin_lock_irq(&ms->lock);
646 bio_list_merge(&ms->writes, &requeue);
647 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100648 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100649 }
650
651 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * Increment the pending counts for any regions that will
653 * be written to (writes to recover regions are going to
654 * be delayed).
655 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100656 dm_rh_inc_pending(ms->rh, &sync);
657 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100658
659 /*
660 * If the flush fails on a previous call and succeeds here,
661 * we must not reset the log_failure variable. We need
662 * userspace interaction to do that.
663 */
664 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /*
667 * Dispatch io.
668 */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000669 if (unlikely(ms->log_failure)) {
670 spin_lock_irq(&ms->lock);
671 bio_list_merge(&ms->failures, &sync);
672 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100673 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000674 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100675 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000676 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100679 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 while ((bio = bio_list_pop(&nosync))) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000682 map_bio(get_default_mirror(ms), bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 generic_make_request(bio);
684 }
685}
686
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000687static void do_failures(struct mirror_set *ms, struct bio_list *failures)
688{
689 struct bio *bio;
690
691 if (!failures->head)
692 return;
693
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000694 if (!ms->log_failure) {
Ilpo Jarvinenb34578a2008-10-30 13:33:07 +0000695 while ((bio = bio_list_pop(failures))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100696 ms->in_sync = 0;
697 dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
Ilpo Jarvinenb34578a2008-10-30 13:33:07 +0000698 }
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000699 return;
700 }
701
702 /*
703 * If the log has failed, unattempted writes are being
704 * put on the failures list. We can't issue those writes
705 * until a log has been marked, so we must store them.
706 *
707 * If a 'noflush' suspend is in progress, we can requeue
708 * the I/O's to the core. This give userspace a chance
709 * to reconfigure the mirror, at which point the core
710 * will reissue the writes. If the 'noflush' flag is
711 * not set, we have no choice but to return errors.
712 *
713 * Some writes on the failures list may have been
714 * submitted before the log failure and represent a
715 * failure to write to one of the devices. It is ok
716 * for us to treat them the same and requeue them
717 * as well.
718 */
719 if (dm_noflush_suspending(ms->ti)) {
720 while ((bio = bio_list_pop(failures)))
721 bio_endio(bio, DM_ENDIO_REQUEUE);
722 return;
723 }
724
725 if (atomic_read(&ms->suspend)) {
726 while ((bio = bio_list_pop(failures)))
727 bio_endio(bio, -EIO);
728 return;
729 }
730
731 spin_lock_irq(&ms->lock);
732 bio_list_merge(&ms->failures, failures);
733 spin_unlock_irq(&ms->lock);
734
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100735 delayed_wake(ms);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000736}
737
738static void trigger_event(struct work_struct *work)
739{
740 struct mirror_set *ms =
741 container_of(work, struct mirror_set, trigger_event);
742
743 dm_table_event(ms->ti->table);
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746/*-----------------------------------------------------------------
747 * kmirrord
748 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100749static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100751 struct mirror_set *ms = container_of(work, struct mirror_set,
752 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000753 struct bio_list reads, writes, failures;
754 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000756 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 reads = ms->reads;
758 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000759 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 bio_list_init(&ms->reads);
761 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000762 bio_list_init(&ms->failures);
763 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100765 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 do_recovery(ms);
767 do_reads(ms, &reads);
768 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000769 do_failures(ms, &failures);
Mikulas Patocka7ff14a32008-04-24 22:10:47 +0100770
771 dm_table_unplug_all(ms->ti->table);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*-----------------------------------------------------------------
775 * Target functions
776 *---------------------------------------------------------------*/
777static struct mirror_set *alloc_context(unsigned int nr_mirrors,
778 uint32_t region_size,
779 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100780 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 size_t len;
783 struct mirror_set *ms = NULL;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
786
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700787 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700789 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return NULL;
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 spin_lock_init(&ms->lock);
794
795 ms->ti = ti;
796 ms->nr_mirrors = nr_mirrors;
797 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
798 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000799 ms->log_failure = 0;
800 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000801 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100803 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
804 _dm_raid1_read_record_cache);
805
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000806 if (!ms->read_record_pool) {
807 ti->error = "Error creating mirror read_record_pool";
808 kfree(ms);
809 return NULL;
810 }
811
Milan Broz88be1632007-05-09 02:33:04 -0700812 ms->io_client = dm_io_client_create(DM_IO_PAGES);
813 if (IS_ERR(ms->io_client)) {
814 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000815 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700816 kfree(ms);
817 return NULL;
818 }
819
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100820 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
821 wakeup_all_recovery_waiters,
822 ms->ti->begin, MAX_RECOVERY,
823 dl, region_size, ms->nr_regions);
824 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700825 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100826 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000827 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 kfree(ms);
829 return NULL;
830 }
831
832 return ms;
833}
834
835static void free_context(struct mirror_set *ms, struct dm_target *ti,
836 unsigned int m)
837{
838 while (m--)
839 dm_put_device(ti, ms->mirror[m].dev);
840
Milan Broz88be1632007-05-09 02:33:04 -0700841 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100842 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000843 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree(ms);
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
848 unsigned int mirror, char **argv)
849{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800850 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Andrew Morton4ee218c2006-03-27 01:17:48 -0800852 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700853 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -EINVAL;
855 }
856
857 if (dm_get_device(ti, argv[0], offset, ti->len,
858 dm_table_get_mode(ti->table),
859 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700860 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENXIO;
862 }
863
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100864 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000865 atomic_set(&(ms->mirror[mirror].error_count), 0);
866 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 ms->mirror[mirror].offset = offset;
868
869 return 0;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
873 * Create dirty log: log_type #log_params <log_params>
874 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100875static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100876 unsigned argc, char **argv,
877 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100879 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100880 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700883 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return NULL;
885 }
886
887 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700888 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return NULL;
890 }
891
892 *args_used = 2 + param_count;
893
894 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700895 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return NULL;
897 }
898
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100899 dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700901 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return NULL;
903 }
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return dl;
906}
907
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700908static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
909 unsigned *args_used)
910{
911 unsigned num_features;
912 struct dm_target *ti = ms->ti;
913
914 *args_used = 0;
915
916 if (!argc)
917 return 0;
918
919 if (sscanf(argv[0], "%u", &num_features) != 1) {
920 ti->error = "Invalid number of features";
921 return -EINVAL;
922 }
923
924 argc--;
925 argv++;
926 (*args_used)++;
927
928 if (num_features > argc) {
929 ti->error = "Not enough arguments to support feature count";
930 return -EINVAL;
931 }
932
933 if (!strcmp("handle_errors", argv[0]))
934 ms->features |= DM_RAID1_HANDLE_ERRORS;
935 else {
936 ti->error = "Unrecognised feature requested";
937 return -EINVAL;
938 }
939
940 (*args_used)++;
941
942 return 0;
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/*
946 * Construct a mirror mapping:
947 *
948 * log_type #log_params <log_params>
949 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700950 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
952 * log_type is "core" or "disk"
953 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700954 *
955 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
958{
959 int r;
960 unsigned int nr_mirrors, m, args_used;
961 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100962 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 dl = create_dirty_log(ti, argc, argv, &args_used);
965 if (!dl)
966 return -EINVAL;
967
968 argv += args_used;
969 argc -= args_used;
970
971 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100972 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700973 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100974 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EINVAL;
976 }
977
978 argv++, argc--;
979
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700980 if (argc < nr_mirrors * 2) {
981 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100982 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EINVAL;
984 }
985
986 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
987 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100988 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return -ENOMEM;
990 }
991
992 /* Get the mirror parameter sets */
993 for (m = 0; m < nr_mirrors; m++) {
994 r = get_mirror(ms, ti, m, argv);
995 if (r) {
996 free_context(ms, ti, m);
997 return r;
998 }
999 argv += 2;
1000 argc -= 2;
1001 }
1002
1003 ti->private = ms;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001004 ti->split_io = dm_rh_get_region_size(ms->rh);
Mikulas Patocka41841532009-12-10 23:51:59 +00001005 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001007 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1008 if (!ms->kmirrord_wq) {
1009 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001010 r = -ENOMEM;
1011 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001012 }
1013 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001014 init_timer(&ms->timer);
1015 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001016 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001017
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001018 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001019 if (r)
1020 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001021
1022 argv += args_used;
1023 argc -= args_used;
1024
Jonathan Brassowf44db672007-07-12 17:29:04 +01001025 /*
1026 * Any read-balancing addition depends on the
1027 * DM_RAID1_HANDLE_ERRORS flag being present.
1028 * This is because the decision to balance depends
1029 * on the sync state of a region. If the above
1030 * flag is not present, we ignore errors; and
1031 * the sync state may be inaccurate.
1032 */
1033
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001034 if (argc) {
1035 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001036 r = -EINVAL;
1037 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001038 }
1039
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001040 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001041 if (r)
1042 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001044 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001046
1047err_destroy_wq:
1048 destroy_workqueue(ms->kmirrord_wq);
1049err_free_context:
1050 free_context(ms, ti, ms->nr_mirrors);
1051 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054static void mirror_dtr(struct dm_target *ti)
1055{
1056 struct mirror_set *ms = (struct mirror_set *) ti->private;
1057
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001058 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001059 flush_workqueue(ms->kmirrord_wq);
Mikulas Patocka18776c72008-11-13 23:38:52 +00001060 flush_scheduled_work();
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001061 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001062 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 free_context(ms, ti, ms->nr_mirrors);
1064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
1067 * Mirror mapping function
1068 */
1069static int mirror_map(struct dm_target *ti, struct bio *bio,
1070 union map_info *map_context)
1071{
1072 int r, rw = bio_rw(bio);
1073 struct mirror *m;
1074 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001075 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001076 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001079 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001080 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
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
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001085 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (r < 0 && r != -EWOULDBLOCK)
1087 return r;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001090 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001092 if (!r || (r == -EWOULDBLOCK)) {
1093 if (rw == READA)
1094 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001097 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001100 /*
1101 * The region is in-sync and we can perform reads directly.
1102 * Store enough information so we can retry if it fails.
1103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001105 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -EIO;
1107
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001108 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1109 if (likely(read_record)) {
1110 dm_bio_record(&read_record->details, bio);
1111 map_context->ptr = read_record;
1112 read_record->m = m;
1113 }
1114
1115 map_bio(m, bio);
1116
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001117 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1121 int error, union map_info *map_context)
1122{
1123 int rw = bio_rw(bio);
1124 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001125 struct mirror *m = NULL;
1126 struct dm_bio_details *bd = NULL;
1127 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 /*
1130 * We need to dec pending if this was a write.
1131 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001132 if (rw == WRITE) {
Mikulas Patocka41841532009-12-10 23:51:59 +00001133 if (likely(!bio_empty_barrier(bio)))
1134 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001135 return error;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001138 if (error == -EOPNOTSUPP)
1139 goto out;
1140
Jens Axboe1f98a132009-09-11 14:32:04 +02001141 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001142 goto out;
1143
1144 if (unlikely(error)) {
1145 if (!read_record) {
1146 /*
1147 * There wasn't enough memory to record necessary
1148 * information for a retry or there was no other
1149 * mirror in-sync.
1150 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001151 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001152 return -EIO;
1153 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001154
1155 m = read_record->m;
1156
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001157 DMERR("Mirror read failed from %s. Trying alternative device.",
1158 m->dev->name);
1159
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001160 fail_mirror(m, DM_RAID1_READ_ERROR);
1161
1162 /*
1163 * A failed read is requeued for another attempt using an intact
1164 * mirror.
1165 */
1166 if (default_ok(m) || mirror_available(ms, bio)) {
1167 bd = &read_record->details;
1168
1169 dm_bio_restore(bd, bio);
1170 mempool_free(read_record, ms->read_record_pool);
1171 map_context->ptr = NULL;
1172 queue_bio(ms, bio, rw);
1173 return 1;
1174 }
1175 DMERR("All replicated volumes dead, failing I/O");
1176 }
1177
1178out:
1179 if (read_record) {
1180 mempool_free(read_record, ms->read_record_pool);
1181 map_context->ptr = NULL;
1182 }
1183
1184 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001187static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001190 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001192 atomic_set(&ms->suspend, 1);
1193
1194 /*
1195 * We must finish up all the work that we've
1196 * generated (i.e. recovery work).
1197 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001198 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001199
Jonathan E Brassow33184042006-11-08 17:44:44 -08001200 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001201 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001202
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001203 if (log->type->presuspend && log->type->presuspend(log))
1204 /* FIXME: need better error handling */
1205 DMWARN("log presuspend failed");
1206
1207 /*
1208 * Now that recovery is complete/stopped and the
1209 * delayed bios are queued, we need to wait for
1210 * the worker thread to complete. This way,
1211 * we know that all of our I/O has been pushed.
1212 */
1213 flush_workqueue(ms->kmirrord_wq);
1214}
1215
1216static void mirror_postsuspend(struct dm_target *ti)
1217{
1218 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001219 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001220
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001221 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001223 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static void mirror_resume(struct dm_target *ti)
1227{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001228 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001229 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001230
1231 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (log->type->resume && log->type->resume(log))
1233 /* FIXME: need better error handling */
1234 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001235 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001238/*
1239 * device_status_char
1240 * @m: mirror device/leg we want the status of
1241 *
1242 * We return one character representing the most severe error
1243 * we have encountered.
1244 * A => Alive - No failures
1245 * D => Dead - A write failure occurred leaving mirror out-of-sync
1246 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1247 * R => Read - A read failure occurred, mirror data unaffected
1248 *
1249 * Returns: <char>
1250 */
1251static char device_status_char(struct mirror *m)
1252{
1253 if (!atomic_read(&(m->error_count)))
1254 return 'A';
1255
1256 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1257 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1258 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1259}
1260
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262static int mirror_status(struct dm_target *ti, status_type_t type,
1263 char *result, unsigned int maxlen)
1264{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001265 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001267 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001268 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 switch (type) {
1271 case STATUSTYPE_INFO:
1272 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001273 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001275 buffer[m] = device_status_char(&(ms->mirror[m]));
1276 }
1277 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001279 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001280 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001281 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001282
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001283 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 break;
1286
1287 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001288 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001289
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001290 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001292 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001293 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001294
1295 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1296 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 return 0;
1300}
1301
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001302static int mirror_iterate_devices(struct dm_target *ti,
1303 iterate_devices_callout_fn fn, void *data)
1304{
1305 struct mirror_set *ms = ti->private;
1306 int ret = 0;
1307 unsigned i;
1308
1309 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1310 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001311 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001312
1313 return ret;
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316static struct target_type mirror_target = {
1317 .name = "mirror",
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001318 .version = {1, 12, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 .module = THIS_MODULE,
1320 .ctr = mirror_ctr,
1321 .dtr = mirror_dtr,
1322 .map = mirror_map,
1323 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001324 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 .postsuspend = mirror_postsuspend,
1326 .resume = mirror_resume,
1327 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001328 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329};
1330
1331static int __init dm_mirror_init(void)
1332{
1333 int r;
1334
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001335 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1336 if (!_dm_raid1_read_record_cache) {
1337 DMERR("Can't allocate dm_raid1_read_record cache");
1338 r = -ENOMEM;
1339 goto bad_cache;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001342 r = dm_register_target(&mirror_target);
1343 if (r < 0) {
1344 DMERR("Failed to register mirror target");
1345 goto bad_target;
1346 }
1347
1348 return 0;
1349
1350bad_target:
1351 kmem_cache_destroy(_dm_raid1_read_record_cache);
1352bad_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return r;
1354}
1355
1356static void __exit dm_mirror_exit(void)
1357{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001358 dm_unregister_target(&mirror_target);
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001359 kmem_cache_destroy(_dm_raid1_read_record_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362/* Module hooks */
1363module_init(dm_mirror_init);
1364module_exit(dm_mirror_exit);
1365
1366MODULE_DESCRIPTION(DM_NAME " mirror target");
1367MODULE_AUTHOR("Joe Thornber");
1368MODULE_LICENSE("GPL");