blob: e363335e8d8147d6ffb16fbc2c9f32f3e87b36d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01003 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Jonathan Brassow06386bb2008-02-08 02:11:37 +00008#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
11#include <linux/mempool.h>
12#include <linux/module.h>
13#include <linux/pagemap.h>
14#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/workqueue.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010016#include <linux/device-mapper.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010017#include <linux/dm-io.h>
18#include <linux/dm-dirty-log.h>
19#include <linux/dm-kcopyd.h>
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010020#include <linux/dm-region-hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022#define DM_MSG_PREFIX "raid1"
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010023
24#define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
Milan Broz88be1632007-05-09 02:33:04 -070025#define DM_IO_PAGES 64
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010026#define DM_KCOPYD_PAGES 64
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070028#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010029#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070030
Jonathan E Brassow33184042006-11-08 17:44:44 -080031static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*-----------------------------------------------------------------
Neil Browne4c8b3b2006-06-26 00:27:26 -070034 * Mirror set structures.
35 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +000036enum dm_raid1_error {
37 DM_RAID1_WRITE_ERROR,
Mikulas Patocka64b30c42009-12-10 23:52:02 +000038 DM_RAID1_FLUSH_ERROR,
Jonathan Brassow72f4b312008-02-08 02:11:29 +000039 DM_RAID1_SYNC_ERROR,
40 DM_RAID1_READ_ERROR
41};
42
Neil Browne4c8b3b2006-06-26 00:27:26 -070043struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +010044 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -070045 atomic_t error_count;
Al Viro39ed7ad2008-02-13 03:53:00 +000046 unsigned long error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -070047 struct dm_dev *dev;
48 sector_t offset;
49};
50
51struct mirror_set {
52 struct dm_target *ti;
53 struct list_head list;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010054
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070055 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -070056
Jonathan Brassow72f4b312008-02-08 02:11:29 +000057 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -070058 struct bio_list reads;
59 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +000060 struct bio_list failures;
Mikulas Patocka04788502009-12-10 23:52:03 +000061 struct bio_list holds; /* bios are waiting until suspend */
Neil Browne4c8b3b2006-06-26 00:27:26 -070062
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010063 struct dm_region_hash *rh;
64 struct dm_kcopyd_client *kcopyd_client;
Milan Broz88be1632007-05-09 02:33:04 -070065 struct dm_io_client *io_client;
Jonathan Brassow06386bb2008-02-08 02:11:37 +000066 mempool_t *read_record_pool;
Milan Broz88be1632007-05-09 02:33:04 -070067
Neil Browne4c8b3b2006-06-26 00:27:26 -070068 /* recovery */
69 region_t nr_regions;
70 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +010071 int log_failure;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +000072 atomic_t suspend;
Neil Browne4c8b3b2006-06-26 00:27:26 -070073
Jonathan Brassow72f4b312008-02-08 02:11:29 +000074 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -070075
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070076 struct workqueue_struct *kmirrord_wq;
77 struct work_struct kmirrord_work;
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010078 struct timer_list timer;
79 unsigned long timer_pending;
80
Jonathan Brassow72f4b312008-02-08 02:11:29 +000081 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070082
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010083 unsigned nr_mirrors;
Neil Browne4c8b3b2006-06-26 00:27:26 -070084 struct mirror mirror[0];
85};
86
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010087static void wakeup_mirrord(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010089 struct mirror_set *ms = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Holger Smolinski6ad36fe2007-05-09 02:32:50 -070091 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
92}
93
Mikulas Patockaa2aebe02008-04-24 22:10:42 +010094static void delayed_wake_fn(unsigned long data)
95{
96 struct mirror_set *ms = (struct mirror_set *) data;
97
98 clear_bit(0, &ms->timer_pending);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +010099 wakeup_mirrord(ms);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100100}
101
102static void delayed_wake(struct mirror_set *ms)
103{
104 if (test_and_set_bit(0, &ms->timer_pending))
105 return;
106
107 ms->timer.expires = jiffies + HZ / 5;
108 ms->timer.data = (unsigned long) ms;
109 ms->timer.function = delayed_wake_fn;
110 add_timer(&ms->timer);
111}
112
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100113static void wakeup_all_recovery_waiters(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100115 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100118static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int should_wake = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100122 struct bio_list *bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100124 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
125 spin_lock_irqsave(&ms->lock, flags);
126 should_wake = !(bl->head);
127 bio_list_add(bl, bio);
128 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if (should_wake)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100131 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100134static void dispatch_bios(void *context, struct bio_list *bio_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100136 struct mirror_set *ms = context;
137 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100139 while ((bio = bio_list_pop(bio_list)))
140 queue_bio(ms, bio, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000143#define MIN_READ_RECORDS 20
144struct dm_raid1_read_record {
145 struct mirror *m;
146 struct dm_bio_details details;
147};
148
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100149static struct kmem_cache *_dm_raid1_read_record_cache;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * Every mirror should look like this one.
153 */
154#define DEFAULT_MIRROR 0
155
156/*
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000157 * This is yucky. We squirrel the mirror struct away inside
158 * bi_next for read/write buffers. This is safe since the bh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * doesn't get submitted to the lower levels of block layer.
160 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000161static struct mirror *bio_get_m(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000163 return (struct mirror *) bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000166static void bio_set_m(struct bio *bio, struct mirror *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000168 bio->bi_next = (struct bio *) m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000171static struct mirror *get_default_mirror(struct mirror_set *ms)
172{
173 return &ms->mirror[atomic_read(&ms->default_mirror)];
174}
175
176static void set_default_mirror(struct mirror *m)
177{
178 struct mirror_set *ms = m->ms;
179 struct mirror *m0 = &(ms->mirror[0]);
180
181 atomic_set(&ms->default_mirror, m - m0);
182}
183
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000184static struct mirror *get_valid_mirror(struct mirror_set *ms)
185{
186 struct mirror *m;
187
188 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
189 if (!atomic_read(&m->error_count))
190 return m;
191
192 return NULL;
193}
194
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000195/* fail_mirror
196 * @m: mirror device to fail
197 * @error_type: one of the enum's, DM_RAID1_*_ERROR
198 *
199 * If errors are being handled, record the type of
200 * error encountered for this device. If this type
201 * of error has already been recorded, we can return;
202 * otherwise, we must signal userspace by triggering
203 * an event. Additionally, if the device is the
204 * primary device, we must choose a new primary, but
205 * only if the mirror is in-sync.
206 *
207 * This function must not block.
208 */
209static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
210{
211 struct mirror_set *ms = m->ms;
212 struct mirror *new;
213
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000214 /*
215 * error_count is used for nothing more than a
216 * simple way to tell if a device has encountered
217 * errors.
218 */
219 atomic_inc(&m->error_count);
220
221 if (test_and_set_bit(error_type, &m->error_type))
222 return;
223
Jonathan Brassowd460c652009-01-06 03:04:57 +0000224 if (!errors_handled(ms))
225 return;
226
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000227 if (m != get_default_mirror(ms))
228 goto out;
229
230 if (!ms->in_sync) {
231 /*
232 * Better to issue requests to same failing device
233 * than to risk returning corrupt data.
234 */
235 DMERR("Primary mirror (%s) failed while out-of-sync: "
236 "Reads may fail.", m->dev->name);
237 goto out;
238 }
239
Mikulas Patocka87968dd2009-12-10 23:52:04 +0000240 new = get_valid_mirror(ms);
241 if (new)
242 set_default_mirror(new);
243 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000244 DMWARN("All sides of mirror have failed.");
245
246out:
247 schedule_work(&ms->trigger_event);
248}
249
Mikulas Patockac0da3742009-12-10 23:52:02 +0000250static int mirror_flush(struct dm_target *ti)
251{
252 struct mirror_set *ms = ti->private;
253 unsigned long error_bits;
254
255 unsigned int i;
256 struct dm_io_region io[ms->nr_mirrors];
257 struct mirror *m;
258 struct dm_io_request io_req = {
259 .bi_rw = WRITE_BARRIER,
260 .mem.type = DM_IO_KMEM,
261 .mem.ptr.bvec = NULL,
262 .client = ms->io_client,
263 };
264
265 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
266 io[i].bdev = m->dev->bdev;
267 io[i].sector = 0;
268 io[i].count = 0;
269 }
270
271 error_bits = -1;
272 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
273 if (unlikely(error_bits != 0)) {
274 for (i = 0; i < ms->nr_mirrors; i++)
275 if (test_bit(i, &error_bits))
276 fail_mirror(ms->mirror + i,
Mikulas Patocka64b30c42009-12-10 23:52:02 +0000277 DM_RAID1_FLUSH_ERROR);
Mikulas Patockac0da3742009-12-10 23:52:02 +0000278 return -EIO;
279 }
280
281 return 0;
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*-----------------------------------------------------------------
285 * Recovery.
286 *
287 * When a mirror is first activated we may find that some regions
288 * are in the no-sync state. We have to recover these by
289 * recopying from the default mirror to all the others.
290 *---------------------------------------------------------------*/
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700291static void recovery_complete(int read_err, unsigned long write_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 void *context)
293{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100294 struct dm_region *reg = context;
295 struct mirror_set *ms = dm_rh_region_context(reg);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000296 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000298 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100299 /* Read error means the failure of default mirror. */
300 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000301 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
302 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100303
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000304 if (write_err) {
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700305 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
Jonathan Brassowf44db672007-07-12 17:29:04 +0100306 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000307 /*
308 * Bits correspond to devices (excluding default mirror).
309 * The default mirror cannot change during recovery.
310 */
311 for (m = 0; m < ms->nr_mirrors; m++) {
312 if (&ms->mirror[m] == get_default_mirror(ms))
313 continue;
314 if (test_bit(bit, &write_err))
315 fail_mirror(ms->mirror + m,
316 DM_RAID1_SYNC_ERROR);
317 bit++;
318 }
319 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100320
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100321 dm_rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100324static int recover(struct mirror_set *ms, struct dm_region *reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int r;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100327 unsigned i;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100328 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct mirror *m;
330 unsigned long flags = 0;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100331 region_t key = dm_rh_get_region_key(reg);
332 sector_t region_size = dm_rh_get_region_size(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000335 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 from.bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100337 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
338 if (key == (ms->nr_regions - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /*
340 * The final region may be smaller than
341 * region_size.
342 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100343 from.count = ms->ti->len & (region_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!from.count)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100345 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } else
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100347 from.count = region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* fill in the destinations */
350 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000351 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 continue;
353
354 m = ms->mirror + i;
355 dest->bdev = m->dev->bdev;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100356 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dest->count = from.count;
358 dest++;
359 }
360
361 /* hand to kcopyd */
Jonathan Brassowf7c83e22008-10-10 13:36:59 +0100362 if (!errors_handled(ms))
363 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
364
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100365 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
366 flags, recovery_complete, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 return r;
369}
370
371static void do_recovery(struct mirror_set *ms)
372{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100373 struct dm_region *reg;
374 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /*
378 * Start quiescing some regions.
379 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100380 dm_rh_recovery_prepare(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
383 * Copy any already quiesced regions.
384 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100385 while ((reg = dm_rh_recovery_start(ms->rh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 r = recover(ms, reg);
387 if (r)
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100388 dm_rh_recovery_end(reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 /*
392 * Update the in sync flag.
393 */
394 if (!ms->in_sync &&
395 (log->type->get_sync_count(log) == ms->nr_regions)) {
396 /* the sync is complete */
397 dm_table_event(ms->ti->table);
398 ms->in_sync = 1;
399 }
400}
401
402/*-----------------------------------------------------------------
403 * Reads
404 *---------------------------------------------------------------*/
405static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
406{
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000407 struct mirror *m = get_default_mirror(ms);
408
409 do {
410 if (likely(!atomic_read(&m->error_count)))
411 return m;
412
413 if (m-- == ms->mirror)
414 m += ms->nr_mirrors;
415 } while (m != get_default_mirror(ms));
416
417 return NULL;
418}
419
420static int default_ok(struct mirror *m)
421{
422 struct mirror *default_mirror = get_default_mirror(m->ms);
423
424 return !atomic_read(&default_mirror->error_count);
425}
426
427static int mirror_available(struct mirror_set *ms, struct bio *bio)
428{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100429 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
430 region_t region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000431
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100432 if (log->type->in_sync(log, region, 0))
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000433 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
434
435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438/*
439 * remap a buffer to a particular mirror.
440 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000441static sector_t map_sector(struct mirror *m, struct bio *bio)
442{
Mikulas Patocka41841532009-12-10 23:51:59 +0000443 if (unlikely(!bio->bi_size))
444 return 0;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000445 return m->offset + (bio->bi_sector - m->ms->ti->begin);
446}
447
448static void map_bio(struct mirror *m, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 bio->bi_bdev = m->dev->bdev;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000451 bio->bi_sector = map_sector(m, bio);
452}
453
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100454static void map_region(struct dm_io_region *io, struct mirror *m,
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000455 struct bio *bio)
456{
457 io->bdev = m->dev->bdev;
458 io->sector = map_sector(m, bio);
459 io->count = bio->bi_size >> 9;
460}
461
Mikulas Patocka04788502009-12-10 23:52:03 +0000462static void hold_bio(struct mirror_set *ms, struct bio *bio)
463{
464 /*
465 * If device is suspended, complete the bio.
466 */
467 if (atomic_read(&ms->suspend)) {
468 if (dm_noflush_suspending(ms->ti))
469 bio_endio(bio, DM_ENDIO_REQUEUE);
470 else
471 bio_endio(bio, -EIO);
472 return;
473 }
474
475 /*
476 * Hold bio until the suspend is complete.
477 */
478 spin_lock_irq(&ms->lock);
479 bio_list_add(&ms->holds, bio);
480 spin_unlock_irq(&ms->lock);
481}
482
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000483/*-----------------------------------------------------------------
484 * Reads
485 *---------------------------------------------------------------*/
486static void read_callback(unsigned long error, void *context)
487{
488 struct bio *bio = context;
489 struct mirror *m;
490
491 m = bio_get_m(bio);
492 bio_set_m(bio, NULL);
493
494 if (likely(!error)) {
495 bio_endio(bio, 0);
496 return;
497 }
498
499 fail_mirror(m, DM_RAID1_READ_ERROR);
500
501 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
502 DMWARN_LIMIT("Read failure on mirror device %s. "
503 "Trying alternative device.",
504 m->dev->name);
505 queue_bio(m->ms, bio, bio_rw(bio));
506 return;
507 }
508
509 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
510 m->dev->name);
511 bio_endio(bio, -EIO);
512}
513
514/* Asynchronous read. */
515static void read_async_bio(struct mirror *m, struct bio *bio)
516{
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100517 struct dm_io_region io;
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000518 struct dm_io_request io_req = {
519 .bi_rw = READ,
520 .mem.type = DM_IO_BVEC,
521 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
522 .notify.fn = read_callback,
523 .notify.context = bio,
524 .client = m->ms->io_client,
525 };
526
527 map_region(&io, m, bio);
528 bio_set_m(bio, m);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100529 BUG_ON(dm_io(&io_req, 1, &io, NULL));
530}
531
532static inline int region_in_sync(struct mirror_set *ms, region_t region,
533 int may_block)
534{
535 int state = dm_rh_get_state(ms->rh, region, may_block);
536 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static void do_reads(struct mirror_set *ms, struct bio_list *reads)
540{
541 region_t region;
542 struct bio *bio;
543 struct mirror *m;
544
545 while ((bio = bio_list_pop(reads))) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100546 region = dm_rh_bio_to_region(ms->rh, bio);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000547 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /*
550 * We can only read balance if the region is in sync.
551 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100552 if (likely(region_in_sync(ms, region, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000554 else if (m && atomic_read(&m->error_count))
555 m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000557 if (likely(m))
558 read_async_bio(m, bio);
559 else
560 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562}
563
564/*-----------------------------------------------------------------
565 * Writes.
566 *
567 * We do different things with the write io depending on the
568 * state of the region that it's in:
569 *
570 * SYNC: increment pending, use kcopyd to write to *all* mirrors
571 * RECOVERING: delay the io until recovery completes
572 * NOSYNC: increment pending, just write to the default mirror
573 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000574
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static void write_callback(unsigned long error, void *context)
577{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000578 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct bio *bio = (struct bio *) context;
580 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000581 int should_wake = 0;
582 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000584 ms = bio_get_m(bio)->ms;
585 bio_set_m(bio, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /*
588 * NOTE: We don't decrement the pending count here,
589 * instead it is done by the targets endio function.
590 * This way we handle both writes to SYNC and NOSYNC
591 * regions with the same code.
592 */
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000593 if (likely(!error)) {
594 bio_endio(bio, ret);
595 return;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000598 for (i = 0; i < ms->nr_mirrors; i++)
599 if (test_bit(i, &error))
600 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000601
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000602 /*
603 * Need to raise event. Since raising
604 * events can block, we need to do it in
605 * the main thread.
606 */
607 spin_lock_irqsave(&ms->lock, flags);
608 if (!ms->failures.head)
609 should_wake = 1;
610 bio_list_add(&ms->failures, bio);
611 spin_unlock_irqrestore(&ms->lock, flags);
612 if (should_wake)
613 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616static void do_write(struct mirror_set *ms, struct bio *bio)
617{
618 unsigned int i;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100619 struct dm_io_region io[ms->nr_mirrors], *dest = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -0700621 struct dm_io_request io_req = {
Mikulas Patocka41841532009-12-10 23:51:59 +0000622 .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
Milan Broz88be1632007-05-09 02:33:04 -0700623 .mem.type = DM_IO_BVEC,
624 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
625 .notify.fn = write_callback,
626 .notify.context = bio,
627 .client = ms->io_client,
628 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000630 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
631 map_region(dest++, m, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000633 /*
634 * Use default mirror because we only need it to retrieve the reference
635 * to the mirror set in write_callback().
636 */
637 bio_set_m(bio, get_default_mirror(ms));
Milan Broz88be1632007-05-09 02:33:04 -0700638
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100639 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void do_writes(struct mirror_set *ms, struct bio_list *writes)
643{
644 int state;
645 struct bio *bio;
646 struct bio_list sync, nosync, recover, *this_list = NULL;
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100647 struct bio_list requeue;
648 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
649 region_t region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (!writes->head)
652 return;
653
654 /*
655 * Classify each write.
656 */
657 bio_list_init(&sync);
658 bio_list_init(&nosync);
659 bio_list_init(&recover);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100660 bio_list_init(&requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 while ((bio = bio_list_pop(writes))) {
Mikulas Patocka41841532009-12-10 23:51:59 +0000663 if (unlikely(bio_empty_barrier(bio))) {
664 bio_list_add(&sync, bio);
665 continue;
666 }
667
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100668 region = dm_rh_bio_to_region(ms->rh, bio);
669
670 if (log->type->is_remote_recovering &&
671 log->type->is_remote_recovering(log, region)) {
672 bio_list_add(&requeue, bio);
673 continue;
674 }
675
676 state = dm_rh_get_state(ms->rh, region, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 switch (state) {
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100678 case DM_RH_CLEAN:
679 case DM_RH_DIRTY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 this_list = &sync;
681 break;
682
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100683 case DM_RH_NOSYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 this_list = &nosync;
685 break;
686
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100687 case DM_RH_RECOVERING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 this_list = &recover;
689 break;
690 }
691
692 bio_list_add(this_list, bio);
693 }
694
695 /*
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100696 * Add bios that are delayed due to remote recovery
697 * back on to the write queue
698 */
699 if (unlikely(requeue.head)) {
700 spin_lock_irq(&ms->lock);
701 bio_list_merge(&ms->writes, &requeue);
702 spin_unlock_irq(&ms->lock);
Mikulas Patocka69885682009-07-23 20:30:37 +0100703 delayed_wake(ms);
Jonathan Brassow7513c2a2009-04-02 19:55:30 +0100704 }
705
706 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Increment the pending counts for any regions that will
708 * be written to (writes to recover regions are going to
709 * be delayed).
710 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100711 dm_rh_inc_pending(ms->rh, &sync);
712 dm_rh_inc_pending(ms->rh, &nosync);
Jonathan Brassowd2b69862009-09-04 20:40:32 +0100713
714 /*
715 * If the flush fails on a previous call and succeeds here,
716 * we must not reset the log_failure variable. We need
717 * userspace interaction to do that.
718 */
719 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /*
722 * Dispatch io.
723 */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000724 if (unlikely(ms->log_failure)) {
725 spin_lock_irq(&ms->lock);
726 bio_list_merge(&ms->failures, &sync);
727 spin_unlock_irq(&ms->lock);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100728 wakeup_mirrord(ms);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000729 } else
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100730 while ((bio = bio_list_pop(&sync)))
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000731 do_write(ms, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 while ((bio = bio_list_pop(&recover)))
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100734 dm_rh_delay(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 while ((bio = bio_list_pop(&nosync))) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000737 map_bio(get_default_mirror(ms), bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 generic_make_request(bio);
739 }
740}
741
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000742static void do_failures(struct mirror_set *ms, struct bio_list *failures)
743{
744 struct bio *bio;
745
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000746 if (likely(!failures->head))
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000747 return;
748
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000749 /*
750 * If the log has failed, unattempted writes are being
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000751 * put on the holds list. We can't issue those writes
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000752 * until a log has been marked, so we must store them.
753 *
754 * If a 'noflush' suspend is in progress, we can requeue
755 * the I/O's to the core. This give userspace a chance
756 * to reconfigure the mirror, at which point the core
757 * will reissue the writes. If the 'noflush' flag is
758 * not set, we have no choice but to return errors.
759 *
760 * Some writes on the failures list may have been
761 * submitted before the log failure and represent a
762 * failure to write to one of the devices. It is ok
763 * for us to treat them the same and requeue them
764 * as well.
765 */
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000766 while ((bio = bio_list_pop(failures))) {
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000767 if (!ms->log_failure) {
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000768 ms->in_sync = 0;
Mikulas Patockac58098b2009-12-10 23:52:05 +0000769 dm_rh_mark_nosync(ms->rh, bio);
Mikulas Patocka0f398a82009-12-10 23:52:04 +0000770 }
Mikulas Patocka60f355e2009-12-10 23:52:05 +0000771
772 /*
773 * If all the legs are dead, fail the I/O.
774 * If we have been told to handle errors, hold the bio
775 * and wait for userspace to deal with the problem.
776 * Otherwise pretend that the I/O succeeded. (This would
777 * be wrong if the failed leg returned after reboot and
778 * got replicated back to the good legs.)
779 */
780 if (!get_valid_mirror(ms))
781 bio_endio(bio, -EIO);
782 else if (errors_handled(ms))
783 hold_bio(ms, bio);
784 else
785 bio_endio(bio, 0);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000786 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000787}
788
789static void trigger_event(struct work_struct *work)
790{
791 struct mirror_set *ms =
792 container_of(work, struct mirror_set, trigger_event);
793
794 dm_table_event(ms->ti->table);
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/*-----------------------------------------------------------------
798 * kmirrord
799 *---------------------------------------------------------------*/
Mikulas Patockaa2aebe02008-04-24 22:10:42 +0100800static void do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100802 struct mirror_set *ms = container_of(work, struct mirror_set,
803 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000804 struct bio_list reads, writes, failures;
805 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000807 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 reads = ms->reads;
809 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000810 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 bio_list_init(&ms->reads);
812 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000813 bio_list_init(&ms->failures);
814 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100816 dm_rh_update_states(ms->rh, errors_handled(ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 do_recovery(ms);
818 do_reads(ms, &reads);
819 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000820 do_failures(ms, &failures);
Mikulas Patocka7ff14a32008-04-24 22:10:47 +0100821
822 dm_table_unplug_all(ms->ti->table);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*-----------------------------------------------------------------
826 * Target functions
827 *---------------------------------------------------------------*/
828static struct mirror_set *alloc_context(unsigned int nr_mirrors,
829 uint32_t region_size,
830 struct dm_target *ti,
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100831 struct dm_dirty_log *dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 size_t len;
834 struct mirror_set *ms = NULL;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
837
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700838 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700840 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return NULL;
842 }
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 spin_lock_init(&ms->lock);
845
846 ms->ti = ti;
847 ms->nr_mirrors = nr_mirrors;
848 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
849 ms->in_sync = 0;
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +0000850 ms->log_failure = 0;
851 atomic_set(&ms->suspend, 0);
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000852 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Mikulas Patocka95f8fac2009-04-02 19:55:24 +0100854 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
855 _dm_raid1_read_record_cache);
856
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000857 if (!ms->read_record_pool) {
858 ti->error = "Error creating mirror read_record_pool";
859 kfree(ms);
860 return NULL;
861 }
862
Milan Broz88be1632007-05-09 02:33:04 -0700863 ms->io_client = dm_io_client_create(DM_IO_PAGES);
864 if (IS_ERR(ms->io_client)) {
865 ti->error = "Error creating dm_io client";
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000866 mempool_destroy(ms->read_record_pool);
Milan Broz88be1632007-05-09 02:33:04 -0700867 kfree(ms);
868 return NULL;
869 }
870
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100871 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
872 wakeup_all_recovery_waiters,
873 ms->ti->begin, MAX_RECOVERY,
874 dl, region_size, ms->nr_regions);
875 if (IS_ERR(ms->rh)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700876 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +0100877 dm_io_client_destroy(ms->io_client);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000878 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 kfree(ms);
880 return NULL;
881 }
882
883 return ms;
884}
885
886static void free_context(struct mirror_set *ms, struct dm_target *ti,
887 unsigned int m)
888{
889 while (m--)
890 dm_put_device(ti, ms->mirror[m].dev);
891
Milan Broz88be1632007-05-09 02:33:04 -0700892 dm_io_client_destroy(ms->io_client);
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100893 dm_region_hash_destroy(ms->rh);
Jonathan Brassow06386bb2008-02-08 02:11:37 +0000894 mempool_destroy(ms->read_record_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 kfree(ms);
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
899 unsigned int mirror, char **argv)
900{
Andrew Morton4ee218c2006-03-27 01:17:48 -0800901 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Andrew Morton4ee218c2006-03-27 01:17:48 -0800903 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700904 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906 }
907
908 if (dm_get_device(ti, argv[0], offset, ti->len,
909 dm_table_get_mode(ti->table),
910 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700911 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENXIO;
913 }
914
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100915 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000916 atomic_set(&(ms->mirror[mirror].error_count), 0);
917 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 ms->mirror[mirror].offset = offset;
919
920 return 0;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923/*
924 * Create dirty log: log_type #log_params <log_params>
925 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100926static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100927 unsigned argc, char **argv,
928 unsigned *args_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +0100930 unsigned param_count;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100931 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700934 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return NULL;
936 }
937
938 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700939 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return NULL;
941 }
942
943 *args_used = 2 + param_count;
944
945 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700946 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return NULL;
948 }
949
Mikulas Patockac0da3742009-12-10 23:52:02 +0000950 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
951 argv + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700953 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return NULL;
955 }
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return dl;
958}
959
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700960static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
961 unsigned *args_used)
962{
963 unsigned num_features;
964 struct dm_target *ti = ms->ti;
965
966 *args_used = 0;
967
968 if (!argc)
969 return 0;
970
971 if (sscanf(argv[0], "%u", &num_features) != 1) {
972 ti->error = "Invalid number of features";
973 return -EINVAL;
974 }
975
976 argc--;
977 argv++;
978 (*args_used)++;
979
980 if (num_features > argc) {
981 ti->error = "Not enough arguments to support feature count";
982 return -EINVAL;
983 }
984
985 if (!strcmp("handle_errors", argv[0]))
986 ms->features |= DM_RAID1_HANDLE_ERRORS;
987 else {
988 ti->error = "Unrecognised feature requested";
989 return -EINVAL;
990 }
991
992 (*args_used)++;
993
994 return 0;
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/*
998 * Construct a mirror mapping:
999 *
1000 * log_type #log_params <log_params>
1001 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001002 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
1004 * log_type is "core" or "disk"
1005 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001006 *
1007 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1010{
1011 int r;
1012 unsigned int nr_mirrors, m, args_used;
1013 struct mirror_set *ms;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001014 struct dm_dirty_log *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 dl = create_dirty_log(ti, argc, argv, &args_used);
1017 if (!dl)
1018 return -EINVAL;
1019
1020 argv += args_used;
1021 argc -= args_used;
1022
1023 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001024 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001025 ti->error = "Invalid number of mirrors";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001026 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EINVAL;
1028 }
1029
1030 argv++, argc--;
1031
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001032 if (argc < nr_mirrors * 2) {
1033 ti->error = "Too few mirror arguments";
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001034 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return -EINVAL;
1036 }
1037
1038 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1039 if (!ms) {
Heinz Mauelshagen416cd172008-04-24 21:43:35 +01001040 dm_dirty_log_destroy(dl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -ENOMEM;
1042 }
1043
1044 /* Get the mirror parameter sets */
1045 for (m = 0; m < nr_mirrors; m++) {
1046 r = get_mirror(ms, ti, m, argv);
1047 if (r) {
1048 free_context(ms, ti, m);
1049 return r;
1050 }
1051 argv += 2;
1052 argc -= 2;
1053 }
1054
1055 ti->private = ms;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001056 ti->split_io = dm_rh_get_region_size(ms->rh);
Mikulas Patocka41841532009-12-10 23:51:59 +00001057 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001059 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1060 if (!ms->kmirrord_wq) {
1061 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001062 r = -ENOMEM;
1063 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001064 }
1065 INIT_WORK(&ms->kmirrord_work, do_mirror);
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001066 init_timer(&ms->timer);
1067 ms->timer_pending = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001068 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001069
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001070 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001071 if (r)
1072 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001073
1074 argv += args_used;
1075 argc -= args_used;
1076
Jonathan Brassowf44db672007-07-12 17:29:04 +01001077 /*
1078 * Any read-balancing addition depends on the
1079 * DM_RAID1_HANDLE_ERRORS flag being present.
1080 * This is because the decision to balance depends
1081 * on the sync state of a region. If the above
1082 * flag is not present, we ignore errors; and
1083 * the sync state may be inaccurate.
1084 */
1085
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001086 if (argc) {
1087 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001088 r = -EINVAL;
1089 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001090 }
1091
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001092 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001093 if (r)
1094 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001096 wakeup_mirrord(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001098
1099err_destroy_wq:
1100 destroy_workqueue(ms->kmirrord_wq);
1101err_free_context:
1102 free_context(ms, ti, ms->nr_mirrors);
1103 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static void mirror_dtr(struct dm_target *ti)
1107{
1108 struct mirror_set *ms = (struct mirror_set *) ti->private;
1109
Mikulas Patockaa2aebe02008-04-24 22:10:42 +01001110 del_timer_sync(&ms->timer);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001111 flush_workqueue(ms->kmirrord_wq);
Mikulas Patocka18776c72008-11-13 23:38:52 +00001112 flush_scheduled_work();
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001113 dm_kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001114 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 free_context(ms, ti, ms->nr_mirrors);
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * Mirror mapping function
1120 */
1121static int mirror_map(struct dm_target *ti, struct bio *bio,
1122 union map_info *map_context)
1123{
1124 int r, rw = bio_rw(bio);
1125 struct mirror *m;
1126 struct mirror_set *ms = ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001127 struct dm_raid1_read_record *read_record = NULL;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001128 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (rw == WRITE) {
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001131 /* Save region for mirror_end_io() handler */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001132 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001134 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001137 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (r < 0 && r != -EWOULDBLOCK)
1139 return r;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /*
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001142 * If region is not in-sync queue the bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001144 if (!r || (r == -EWOULDBLOCK)) {
1145 if (rw == READA)
1146 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001149 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001152 /*
1153 * The region is in-sync and we can perform reads directly.
1154 * Store enough information so we can retry if it fails.
1155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 m = choose_mirror(ms, bio->bi_sector);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001157 if (unlikely(!m))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -EIO;
1159
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001160 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1161 if (likely(read_record)) {
1162 dm_bio_record(&read_record->details, bio);
1163 map_context->ptr = read_record;
1164 read_record->m = m;
1165 }
1166
1167 map_bio(m, bio);
1168
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001169 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1173 int error, union map_info *map_context)
1174{
1175 int rw = bio_rw(bio);
1176 struct mirror_set *ms = (struct mirror_set *) ti->private;
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001177 struct mirror *m = NULL;
1178 struct dm_bio_details *bd = NULL;
1179 struct dm_raid1_read_record *read_record = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
1182 * We need to dec pending if this was a write.
1183 */
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001184 if (rw == WRITE) {
Mikulas Patocka41841532009-12-10 23:51:59 +00001185 if (likely(!bio_empty_barrier(bio)))
1186 dm_rh_dec(ms->rh, map_context->ll);
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001187 return error;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001190 if (error == -EOPNOTSUPP)
1191 goto out;
1192
Jens Axboe1f98a132009-09-11 14:32:04 +02001193 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001194 goto out;
1195
1196 if (unlikely(error)) {
1197 if (!read_record) {
1198 /*
1199 * There wasn't enough memory to record necessary
1200 * information for a retry or there was no other
1201 * mirror in-sync.
1202 */
Adrian Bunke03f1a82008-02-19 19:44:19 +00001203 DMERR_LIMIT("Mirror read failed.");
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001204 return -EIO;
1205 }
Adrian Bunke03f1a82008-02-19 19:44:19 +00001206
1207 m = read_record->m;
1208
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001209 DMERR("Mirror read failed from %s. Trying alternative device.",
1210 m->dev->name);
1211
Jonathan Brassow06386bb2008-02-08 02:11:37 +00001212 fail_mirror(m, DM_RAID1_READ_ERROR);
1213
1214 /*
1215 * A failed read is requeued for another attempt using an intact
1216 * mirror.
1217 */
1218 if (default_ok(m) || mirror_available(ms, bio)) {
1219 bd = &read_record->details;
1220
1221 dm_bio_restore(bd, bio);
1222 mempool_free(read_record, ms->read_record_pool);
1223 map_context->ptr = NULL;
1224 queue_bio(ms, bio, rw);
1225 return 1;
1226 }
1227 DMERR("All replicated volumes dead, failing I/O");
1228 }
1229
1230out:
1231 if (read_record) {
1232 mempool_free(read_record, ms->read_record_pool);
1233 map_context->ptr = NULL;
1234 }
1235
1236 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001239static void mirror_presuspend(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001242 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Mikulas Patocka04788502009-12-10 23:52:03 +00001244 struct bio_list holds;
1245 struct bio *bio;
1246
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001247 atomic_set(&ms->suspend, 1);
1248
1249 /*
1250 * We must finish up all the work that we've
1251 * generated (i.e. recovery work).
1252 */
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001253 dm_rh_stop_recovery(ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001254
Jonathan E Brassow33184042006-11-08 17:44:44 -08001255 wait_event(_kmirrord_recovery_stopped,
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001256 !dm_rh_recovery_in_flight(ms->rh));
Jonathan E Brassow33184042006-11-08 17:44:44 -08001257
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001258 if (log->type->presuspend && log->type->presuspend(log))
1259 /* FIXME: need better error handling */
1260 DMWARN("log presuspend failed");
1261
1262 /*
1263 * Now that recovery is complete/stopped and the
1264 * delayed bios are queued, we need to wait for
1265 * the worker thread to complete. This way,
1266 * we know that all of our I/O has been pushed.
1267 */
1268 flush_workqueue(ms->kmirrord_wq);
Mikulas Patocka04788502009-12-10 23:52:03 +00001269
1270 /*
1271 * Now set ms->suspend is set and the workqueue flushed, no more
1272 * entries can be added to ms->hold list, so process it.
1273 *
1274 * Bios can still arrive concurrently with or after this
1275 * presuspend function, but they cannot join the hold list
1276 * because ms->suspend is set.
1277 */
1278 spin_lock_irq(&ms->lock);
1279 holds = ms->holds;
1280 bio_list_init(&ms->holds);
1281 spin_unlock_irq(&ms->lock);
1282
1283 while ((bio = bio_list_pop(&holds)))
1284 hold_bio(ms, bio);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001285}
1286
1287static void mirror_postsuspend(struct dm_target *ti)
1288{
1289 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001290 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001291
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001292 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* FIXME: need better error handling */
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001294 DMWARN("log postsuspend failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297static void mirror_resume(struct dm_target *ti)
1298{
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001299 struct mirror_set *ms = ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001300 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001301
1302 atomic_set(&ms->suspend, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (log->type->resume && log->type->resume(log))
1304 /* FIXME: need better error handling */
1305 DMWARN("log resume failed");
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001306 dm_rh_start_recovery(ms->rh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001309/*
1310 * device_status_char
1311 * @m: mirror device/leg we want the status of
1312 *
1313 * We return one character representing the most severe error
1314 * we have encountered.
1315 * A => Alive - No failures
1316 * D => Dead - A write failure occurred leaving mirror out-of-sync
1317 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1318 * R => Read - A read failure occurred, mirror data unaffected
1319 *
1320 * Returns: <char>
1321 */
1322static char device_status_char(struct mirror *m)
1323{
1324 if (!atomic_read(&(m->error_count)))
1325 return 'A';
1326
Mikulas Patocka64b30c42009-12-10 23:52:02 +00001327 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1328 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001329 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1330 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1331}
1332
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static int mirror_status(struct dm_target *ti, status_type_t type,
1335 char *result, unsigned int maxlen)
1336{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001337 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct mirror_set *ms = (struct mirror_set *) ti->private;
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001339 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001340 char buffer[ms->nr_mirrors + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 switch (type) {
1343 case STATUSTYPE_INFO:
1344 DMEMIT("%d ", ms->nr_mirrors);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001345 for (m = 0; m < ms->nr_mirrors; m++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 DMEMIT("%s ", ms->mirror[m].dev->name);
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001347 buffer[m] = device_status_char(&(ms->mirror[m]));
1348 }
1349 buffer[m] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001351 DMEMIT("%llu/%llu 1 %s ",
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001352 (unsigned long long)log->type->get_sync_count(log),
Jonathan Brassowaf195ac2008-02-08 02:11:39 +00001353 (unsigned long long)ms->nr_regions, buffer);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001354
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001355 sz += log->type->status(log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 break;
1358
1359 case STATUSTYPE_TABLE:
Heinz Mauelshagen1f965b12008-10-21 17:45:06 +01001360 sz = log->type->status(log, type, result, maxlen);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001361
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001362 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001364 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001365 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001366
1367 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1368 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 return 0;
1372}
1373
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001374static int mirror_iterate_devices(struct dm_target *ti,
1375 iterate_devices_callout_fn fn, void *data)
1376{
1377 struct mirror_set *ms = ti->private;
1378 int ret = 0;
1379 unsigned i;
1380
1381 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1382 ret = fn(ti, ms->mirror[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +01001383 ms->mirror[i].offset, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001384
1385 return ret;
1386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388static struct target_type mirror_target = {
1389 .name = "mirror",
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001390 .version = {1, 12, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 .module = THIS_MODULE,
1392 .ctr = mirror_ctr,
1393 .dtr = mirror_dtr,
1394 .map = mirror_map,
1395 .end_io = mirror_end_io,
Jonathan Brassowb80aa7a2008-02-08 02:11:35 +00001396 .presuspend = mirror_presuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 .postsuspend = mirror_postsuspend,
1398 .resume = mirror_resume,
1399 .status = mirror_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001400 .iterate_devices = mirror_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401};
1402
1403static int __init dm_mirror_init(void)
1404{
1405 int r;
1406
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001407 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1408 if (!_dm_raid1_read_record_cache) {
1409 DMERR("Can't allocate dm_raid1_read_record cache");
1410 r = -ENOMEM;
1411 goto bad_cache;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001414 r = dm_register_target(&mirror_target);
1415 if (r < 0) {
1416 DMERR("Failed to register mirror target");
1417 goto bad_target;
1418 }
1419
1420 return 0;
1421
1422bad_target:
1423 kmem_cache_destroy(_dm_raid1_read_record_cache);
1424bad_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return r;
1426}
1427
1428static void __exit dm_mirror_exit(void)
1429{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001430 dm_unregister_target(&mirror_target);
Mikulas Patocka95f8fac2009-04-02 19:55:24 +01001431 kmem_cache_destroy(_dm_raid1_read_record_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
1434/* Module hooks */
1435module_init(dm_mirror_init);
1436module_exit(dm_mirror_exit);
1437
1438MODULE_DESCRIPTION(DM_NAME " mirror target");
1439MODULE_AUTHOR("Joe Thornber");
1440MODULE_LICENSE("GPL");