]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/md/dm-raid1.c
dm: eliminate some holes data structures
[net-next-2.6.git] / drivers / md / dm-raid1.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2003 Sistina Software Limited.
1f965b19 3 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
1da177e4
LT
4 *
5 * This file is released under the GPL.
6 */
7
06386bbf 8#include "dm-bio-record.h"
1da177e4 9
1da177e4
LT
10#include <linux/init.h>
11#include <linux/mempool.h>
12#include <linux/module.h>
13#include <linux/pagemap.h>
14#include <linux/slab.h>
1da177e4 15#include <linux/workqueue.h>
1f965b19 16#include <linux/device-mapper.h>
a765e20e
AK
17#include <linux/dm-io.h>
18#include <linux/dm-dirty-log.h>
19#include <linux/dm-kcopyd.h>
1f965b19 20#include <linux/dm-region-hash.h>
1da177e4 21
72d94861 22#define DM_MSG_PREFIX "raid1"
1f965b19
HM
23
24#define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
88be163a 25#define DM_IO_PAGES 64
1f965b19 26#define DM_KCOPYD_PAGES 64
72d94861 27
a8e6afa2 28#define DM_RAID1_HANDLE_ERRORS 0x01
f44db678 29#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
a8e6afa2 30
33184048 31static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
1da177e4 32
e4c8b3ba
NB
33/*-----------------------------------------------------------------
34 * Mirror set structures.
35 *---------------------------------------------------------------*/
72f4b314
JB
36enum dm_raid1_error {
37 DM_RAID1_WRITE_ERROR,
64b30c46 38 DM_RAID1_FLUSH_ERROR,
72f4b314
JB
39 DM_RAID1_SYNC_ERROR,
40 DM_RAID1_READ_ERROR
41};
42
e4c8b3ba 43struct mirror {
aa5617c5 44 struct mirror_set *ms;
e4c8b3ba 45 atomic_t error_count;
39ed7adb 46 unsigned long error_type;
e4c8b3ba
NB
47 struct dm_dev *dev;
48 sector_t offset;
49};
50
51struct mirror_set {
52 struct dm_target *ti;
53 struct list_head list;
1f965b19 54
a8e6afa2 55 uint64_t features;
e4c8b3ba 56
72f4b314 57 spinlock_t lock; /* protects the lists */
e4c8b3ba
NB
58 struct bio_list reads;
59 struct bio_list writes;
72f4b314 60 struct bio_list failures;
04788507 61 struct bio_list holds; /* bios are waiting until suspend */
e4c8b3ba 62
1f965b19
HM
63 struct dm_region_hash *rh;
64 struct dm_kcopyd_client *kcopyd_client;
88be163a 65 struct dm_io_client *io_client;
06386bbf 66 mempool_t *read_record_pool;
88be163a 67
e4c8b3ba
NB
68 /* recovery */
69 region_t nr_regions;
70 int in_sync;
fc1ff958 71 int log_failure;
929be8fc 72 int leg_failure;
b80aa7a0 73 atomic_t suspend;
e4c8b3ba 74
72f4b314 75 atomic_t default_mirror; /* Default mirror */
e4c8b3ba 76
6ad36fe2
HS
77 struct workqueue_struct *kmirrord_wq;
78 struct work_struct kmirrord_work;
a2aebe03
MP
79 struct timer_list timer;
80 unsigned long timer_pending;
81
72f4b314 82 struct work_struct trigger_event;
6ad36fe2 83
1f965b19 84 unsigned nr_mirrors;
e4c8b3ba
NB
85 struct mirror mirror[0];
86};
87
1f965b19 88static void wakeup_mirrord(void *context)
1da177e4 89{
1f965b19 90 struct mirror_set *ms = context;
1da177e4 91
6ad36fe2
HS
92 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
93}
94
a2aebe03
MP
95static void delayed_wake_fn(unsigned long data)
96{
97 struct mirror_set *ms = (struct mirror_set *) data;
98
99 clear_bit(0, &ms->timer_pending);
1f965b19 100 wakeup_mirrord(ms);
a2aebe03
MP
101}
102
103static void delayed_wake(struct mirror_set *ms)
104{
105 if (test_and_set_bit(0, &ms->timer_pending))
106 return;
107
108 ms->timer.expires = jiffies + HZ / 5;
109 ms->timer.data = (unsigned long) ms;
110 ms->timer.function = delayed_wake_fn;
111 add_timer(&ms->timer);
112}
113
1f965b19 114static void wakeup_all_recovery_waiters(void *context)
1da177e4 115{
1f965b19 116 wake_up_all(&_kmirrord_recovery_stopped);
1da177e4
LT
117}
118
1f965b19 119static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1da177e4
LT
120{
121 unsigned long flags;
1da177e4 122 int should_wake = 0;
1f965b19 123 struct bio_list *bl;
1da177e4 124
1f965b19
HM
125 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
126 spin_lock_irqsave(&ms->lock, flags);
127 should_wake = !(bl->head);
128 bio_list_add(bl, bio);
129 spin_unlock_irqrestore(&ms->lock, flags);
1da177e4
LT
130
131 if (should_wake)
1f965b19 132 wakeup_mirrord(ms);
1da177e4
LT
133}
134
1f965b19 135static void dispatch_bios(void *context, struct bio_list *bio_list)
1da177e4 136{
1f965b19
HM
137 struct mirror_set *ms = context;
138 struct bio *bio;
1da177e4 139
1f965b19
HM
140 while ((bio = bio_list_pop(bio_list)))
141 queue_bio(ms, bio, WRITE);
1da177e4
LT
142}
143
06386bbf
JB
144#define MIN_READ_RECORDS 20
145struct dm_raid1_read_record {
146 struct mirror *m;
147 struct dm_bio_details details;
148};
149
95f8fac8
MP
150static struct kmem_cache *_dm_raid1_read_record_cache;
151
1da177e4
LT
152/*
153 * Every mirror should look like this one.
154 */
155#define DEFAULT_MIRROR 0
156
157/*
06386bbf
JB
158 * This is yucky. We squirrel the mirror struct away inside
159 * bi_next for read/write buffers. This is safe since the bh
1da177e4
LT
160 * doesn't get submitted to the lower levels of block layer.
161 */
06386bbf 162static struct mirror *bio_get_m(struct bio *bio)
1da177e4 163{
06386bbf 164 return (struct mirror *) bio->bi_next;
1da177e4
LT
165}
166
06386bbf 167static void bio_set_m(struct bio *bio, struct mirror *m)
1da177e4 168{
06386bbf 169 bio->bi_next = (struct bio *) m;
1da177e4
LT
170}
171
72f4b314
JB
172static struct mirror *get_default_mirror(struct mirror_set *ms)
173{
174 return &ms->mirror[atomic_read(&ms->default_mirror)];
175}
176
177static void set_default_mirror(struct mirror *m)
178{
179 struct mirror_set *ms = m->ms;
180 struct mirror *m0 = &(ms->mirror[0]);
181
182 atomic_set(&ms->default_mirror, m - m0);
183}
184
87968ddd
MP
185static struct mirror *get_valid_mirror(struct mirror_set *ms)
186{
187 struct mirror *m;
188
189 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
190 if (!atomic_read(&m->error_count))
191 return m;
192
193 return NULL;
194}
195
72f4b314
JB
196/* fail_mirror
197 * @m: mirror device to fail
198 * @error_type: one of the enum's, DM_RAID1_*_ERROR
199 *
200 * If errors are being handled, record the type of
201 * error encountered for this device. If this type
202 * of error has already been recorded, we can return;
203 * otherwise, we must signal userspace by triggering
204 * an event. Additionally, if the device is the
205 * primary device, we must choose a new primary, but
206 * only if the mirror is in-sync.
207 *
208 * This function must not block.
209 */
210static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
211{
212 struct mirror_set *ms = m->ms;
213 struct mirror *new;
214
929be8fc
MP
215 ms->leg_failure = 1;
216
72f4b314
JB
217 /*
218 * error_count is used for nothing more than a
219 * simple way to tell if a device has encountered
220 * errors.
221 */
222 atomic_inc(&m->error_count);
223
224 if (test_and_set_bit(error_type, &m->error_type))
225 return;
226
d460c65a
JB
227 if (!errors_handled(ms))
228 return;
229
72f4b314
JB
230 if (m != get_default_mirror(ms))
231 goto out;
232
233 if (!ms->in_sync) {
234 /*
235 * Better to issue requests to same failing device
236 * than to risk returning corrupt data.
237 */
238 DMERR("Primary mirror (%s) failed while out-of-sync: "
239 "Reads may fail.", m->dev->name);
240 goto out;
241 }
242
87968ddd
MP
243 new = get_valid_mirror(ms);
244 if (new)
245 set_default_mirror(new);
246 else
72f4b314
JB
247 DMWARN("All sides of mirror have failed.");
248
249out:
250 schedule_work(&ms->trigger_event);
251}
252
c0da3748
MP
253static int mirror_flush(struct dm_target *ti)
254{
255 struct mirror_set *ms = ti->private;
256 unsigned long error_bits;
257
258 unsigned int i;
259 struct dm_io_region io[ms->nr_mirrors];
260 struct mirror *m;
261 struct dm_io_request io_req = {
262 .bi_rw = WRITE_BARRIER,
263 .mem.type = DM_IO_KMEM,
264 .mem.ptr.bvec = NULL,
265 .client = ms->io_client,
266 };
267
268 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
269 io[i].bdev = m->dev->bdev;
270 io[i].sector = 0;
271 io[i].count = 0;
272 }
273
274 error_bits = -1;
275 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
276 if (unlikely(error_bits != 0)) {
277 for (i = 0; i < ms->nr_mirrors; i++)
278 if (test_bit(i, &error_bits))
279 fail_mirror(ms->mirror + i,
64b30c46 280 DM_RAID1_FLUSH_ERROR);
c0da3748
MP
281 return -EIO;
282 }
283
284 return 0;
285}
286
1da177e4
LT
287/*-----------------------------------------------------------------
288 * Recovery.
289 *
290 * When a mirror is first activated we may find that some regions
291 * are in the no-sync state. We have to recover these by
292 * recopying from the default mirror to all the others.
293 *---------------------------------------------------------------*/
4cdc1d1f 294static void recovery_complete(int read_err, unsigned long write_err,
1da177e4
LT
295 void *context)
296{
1f965b19
HM
297 struct dm_region *reg = context;
298 struct mirror_set *ms = dm_rh_region_context(reg);
8f0205b7 299 int m, bit = 0;
1da177e4 300
8f0205b7 301 if (read_err) {
f44db678
JB
302 /* Read error means the failure of default mirror. */
303 DMERR_LIMIT("Unable to read primary mirror during recovery");
8f0205b7
JB
304 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
305 }
f44db678 306
8f0205b7 307 if (write_err) {
4cdc1d1f 308 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
f44db678 309 write_err);
8f0205b7
JB
310 /*
311 * Bits correspond to devices (excluding default mirror).
312 * The default mirror cannot change during recovery.
313 */
314 for (m = 0; m < ms->nr_mirrors; m++) {
315 if (&ms->mirror[m] == get_default_mirror(ms))
316 continue;
317 if (test_bit(bit, &write_err))
318 fail_mirror(ms->mirror + m,
319 DM_RAID1_SYNC_ERROR);
320 bit++;
321 }
322 }
f44db678 323
1f965b19 324 dm_rh_recovery_end(reg, !(read_err || write_err));
1da177e4
LT
325}
326
1f965b19 327static int recover(struct mirror_set *ms, struct dm_region *reg)
1da177e4
LT
328{
329 int r;
1f965b19 330 unsigned i;
eb69aca5 331 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
1da177e4
LT
332 struct mirror *m;
333 unsigned long flags = 0;
1f965b19
HM
334 region_t key = dm_rh_get_region_key(reg);
335 sector_t region_size = dm_rh_get_region_size(ms->rh);
1da177e4
LT
336
337 /* fill in the source */
72f4b314 338 m = get_default_mirror(ms);
1da177e4 339 from.bdev = m->dev->bdev;
1f965b19
HM
340 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
341 if (key == (ms->nr_regions - 1)) {
1da177e4
LT
342 /*
343 * The final region may be smaller than
344 * region_size.
345 */
1f965b19 346 from.count = ms->ti->len & (region_size - 1);
1da177e4 347 if (!from.count)
1f965b19 348 from.count = region_size;
1da177e4 349 } else
1f965b19 350 from.count = region_size;
1da177e4
LT
351
352 /* fill in the destinations */
353 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
72f4b314 354 if (&ms->mirror[i] == get_default_mirror(ms))
1da177e4
LT
355 continue;
356
357 m = ms->mirror + i;
358 dest->bdev = m->dev->bdev;
1f965b19 359 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
1da177e4
LT
360 dest->count = from.count;
361 dest++;
362 }
363
364 /* hand to kcopyd */
f7c83e2e
JB
365 if (!errors_handled(ms))
366 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
367
eb69aca5
HM
368 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
369 flags, recovery_complete, reg);
1da177e4
LT
370
371 return r;
372}
373
374static void do_recovery(struct mirror_set *ms)
375{
1f965b19
HM
376 struct dm_region *reg;
377 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1da177e4 378 int r;
1da177e4
LT
379
380 /*
381 * Start quiescing some regions.
382 */
1f965b19 383 dm_rh_recovery_prepare(ms->rh);
1da177e4
LT
384
385 /*
386 * Copy any already quiesced regions.
387 */
1f965b19 388 while ((reg = dm_rh_recovery_start(ms->rh))) {
1da177e4
LT
389 r = recover(ms, reg);
390 if (r)
1f965b19 391 dm_rh_recovery_end(reg, 0);
1da177e4
LT
392 }
393
394 /*
395 * Update the in sync flag.
396 */
397 if (!ms->in_sync &&
398 (log->type->get_sync_count(log) == ms->nr_regions)) {
399 /* the sync is complete */
400 dm_table_event(ms->ti->table);
401 ms->in_sync = 1;
402 }
403}
404
405/*-----------------------------------------------------------------
406 * Reads
407 *---------------------------------------------------------------*/
408static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
409{
06386bbf
JB
410 struct mirror *m = get_default_mirror(ms);
411
412 do {
413 if (likely(!atomic_read(&m->error_count)))
414 return m;
415
416 if (m-- == ms->mirror)
417 m += ms->nr_mirrors;
418 } while (m != get_default_mirror(ms));
419
420 return NULL;
421}
422
423static int default_ok(struct mirror *m)
424{
425 struct mirror *default_mirror = get_default_mirror(m->ms);
426
427 return !atomic_read(&default_mirror->error_count);
428}
429
430static int mirror_available(struct mirror_set *ms, struct bio *bio)
431{
1f965b19
HM
432 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
433 region_t region = dm_rh_bio_to_region(ms->rh, bio);
06386bbf 434
1f965b19 435 if (log->type->in_sync(log, region, 0))
06386bbf
JB
436 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
437
438 return 0;
1da177e4
LT
439}
440
441/*
442 * remap a buffer to a particular mirror.
443 */
06386bbf
JB
444static sector_t map_sector(struct mirror *m, struct bio *bio)
445{
4184153f
MP
446 if (unlikely(!bio->bi_size))
447 return 0;
06386bbf
JB
448 return m->offset + (bio->bi_sector - m->ms->ti->begin);
449}
450
451static void map_bio(struct mirror *m, struct bio *bio)
1da177e4
LT
452{
453 bio->bi_bdev = m->dev->bdev;
06386bbf
JB
454 bio->bi_sector = map_sector(m, bio);
455}
456
22a1ceb1 457static void map_region(struct dm_io_region *io, struct mirror *m,
06386bbf
JB
458 struct bio *bio)
459{
460 io->bdev = m->dev->bdev;
461 io->sector = map_sector(m, bio);
462 io->count = bio->bi_size >> 9;
463}
464
04788507
MP
465static void hold_bio(struct mirror_set *ms, struct bio *bio)
466{
467 /*
468 * If device is suspended, complete the bio.
469 */
470 if (atomic_read(&ms->suspend)) {
471 if (dm_noflush_suspending(ms->ti))
472 bio_endio(bio, DM_ENDIO_REQUEUE);
473 else
474 bio_endio(bio, -EIO);
475 return;
476 }
477
478 /*
479 * Hold bio until the suspend is complete.
480 */
481 spin_lock_irq(&ms->lock);
482 bio_list_add(&ms->holds, bio);
483 spin_unlock_irq(&ms->lock);
484}
485
06386bbf
JB
486/*-----------------------------------------------------------------
487 * Reads
488 *---------------------------------------------------------------*/
489static void read_callback(unsigned long error, void *context)
490{
491 struct bio *bio = context;
492 struct mirror *m;
493
494 m = bio_get_m(bio);
495 bio_set_m(bio, NULL);
496
497 if (likely(!error)) {
498 bio_endio(bio, 0);
499 return;
500 }
501
502 fail_mirror(m, DM_RAID1_READ_ERROR);
503
504 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
505 DMWARN_LIMIT("Read failure on mirror device %s. "
506 "Trying alternative device.",
507 m->dev->name);
508 queue_bio(m->ms, bio, bio_rw(bio));
509 return;
510 }
511
512 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
513 m->dev->name);
514 bio_endio(bio, -EIO);
515}
516
517/* Asynchronous read. */
518static void read_async_bio(struct mirror *m, struct bio *bio)
519{
22a1ceb1 520 struct dm_io_region io;
06386bbf
JB
521 struct dm_io_request io_req = {
522 .bi_rw = READ,
523 .mem.type = DM_IO_BVEC,
524 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
525 .notify.fn = read_callback,
526 .notify.context = bio,
527 .client = m->ms->io_client,
528 };
529
530 map_region(&io, m, bio);
531 bio_set_m(bio, m);
1f965b19
HM
532 BUG_ON(dm_io(&io_req, 1, &io, NULL));
533}
534
535static inline int region_in_sync(struct mirror_set *ms, region_t region,
536 int may_block)
537{
538 int state = dm_rh_get_state(ms->rh, region, may_block);
539 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
1da177e4
LT
540}
541
542static void do_reads(struct mirror_set *ms, struct bio_list *reads)
543{
544 region_t region;
545 struct bio *bio;
546 struct mirror *m;
547
548 while ((bio = bio_list_pop(reads))) {
1f965b19 549 region = dm_rh_bio_to_region(ms->rh, bio);
06386bbf 550 m = get_default_mirror(ms);
1da177e4
LT
551
552 /*
553 * We can only read balance if the region is in sync.
554 */
1f965b19 555 if (likely(region_in_sync(ms, region, 1)))
1da177e4 556 m = choose_mirror(ms, bio->bi_sector);
06386bbf
JB
557 else if (m && atomic_read(&m->error_count))
558 m = NULL;
1da177e4 559
06386bbf
JB
560 if (likely(m))
561 read_async_bio(m, bio);
562 else
563 bio_endio(bio, -EIO);
1da177e4
LT
564 }
565}
566
567/*-----------------------------------------------------------------
568 * Writes.
569 *
570 * We do different things with the write io depending on the
571 * state of the region that it's in:
572 *
573 * SYNC: increment pending, use kcopyd to write to *all* mirrors
574 * RECOVERING: delay the io until recovery completes
575 * NOSYNC: increment pending, just write to the default mirror
576 *---------------------------------------------------------------*/
72f4b314 577
72f4b314 578
1da177e4
LT
579static void write_callback(unsigned long error, void *context)
580{
72f4b314 581 unsigned i, ret = 0;
1da177e4
LT
582 struct bio *bio = (struct bio *) context;
583 struct mirror_set *ms;
72f4b314
JB
584 int should_wake = 0;
585 unsigned long flags;
1da177e4 586
06386bbf
JB
587 ms = bio_get_m(bio)->ms;
588 bio_set_m(bio, NULL);
1da177e4
LT
589
590 /*
591 * NOTE: We don't decrement the pending count here,
592 * instead it is done by the targets endio function.
593 * This way we handle both writes to SYNC and NOSYNC
594 * regions with the same code.
595 */
60f355ea
MP
596 if (likely(!error)) {
597 bio_endio(bio, ret);
598 return;
599 }
1da177e4 600
72f4b314
JB
601 for (i = 0; i < ms->nr_mirrors; i++)
602 if (test_bit(i, &error))
603 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
72f4b314 604
60f355ea
MP
605 /*
606 * Need to raise event. Since raising
607 * events can block, we need to do it in
608 * the main thread.
609 */
610 spin_lock_irqsave(&ms->lock, flags);
611 if (!ms->failures.head)
612 should_wake = 1;
613 bio_list_add(&ms->failures, bio);
614 spin_unlock_irqrestore(&ms->lock, flags);
615 if (should_wake)
616 wakeup_mirrord(ms);
1da177e4
LT
617}
618
619static void do_write(struct mirror_set *ms, struct bio *bio)
620{
621 unsigned int i;
22a1ceb1 622 struct dm_io_region io[ms->nr_mirrors], *dest = io;
1da177e4 623 struct mirror *m;
88be163a 624 struct dm_io_request io_req = {
4184153f 625 .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
88be163a
MB
626 .mem.type = DM_IO_BVEC,
627 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
628 .notify.fn = write_callback,
629 .notify.context = bio,
630 .client = ms->io_client,
631 };
1da177e4 632
06386bbf
JB
633 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
634 map_region(dest++, m, bio);
1da177e4 635
06386bbf
JB
636 /*
637 * Use default mirror because we only need it to retrieve the reference
638 * to the mirror set in write_callback().
639 */
640 bio_set_m(bio, get_default_mirror(ms));
88be163a 641
1f965b19 642 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
1da177e4
LT
643}
644
645static void do_writes(struct mirror_set *ms, struct bio_list *writes)
646{
647 int state;
648 struct bio *bio;
649 struct bio_list sync, nosync, recover, *this_list = NULL;
7513c2a7
JB
650 struct bio_list requeue;
651 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
652 region_t region;
1da177e4
LT
653
654 if (!writes->head)
655 return;
656
657 /*
658 * Classify each write.
659 */
660 bio_list_init(&sync);
661 bio_list_init(&nosync);
662 bio_list_init(&recover);
7513c2a7 663 bio_list_init(&requeue);
1da177e4
LT
664
665 while ((bio = bio_list_pop(writes))) {
4184153f
MP
666 if (unlikely(bio_empty_barrier(bio))) {
667 bio_list_add(&sync, bio);
668 continue;
669 }
670
7513c2a7
JB
671 region = dm_rh_bio_to_region(ms->rh, bio);
672
673 if (log->type->is_remote_recovering &&
674 log->type->is_remote_recovering(log, region)) {
675 bio_list_add(&requeue, bio);
676 continue;
677 }
678
679 state = dm_rh_get_state(ms->rh, region, 1);
1da177e4 680 switch (state) {
1f965b19
HM
681 case DM_RH_CLEAN:
682 case DM_RH_DIRTY:
1da177e4
LT
683 this_list = &sync;
684 break;
685
1f965b19 686 case DM_RH_NOSYNC:
1da177e4
LT
687 this_list = &nosync;
688 break;
689
1f965b19 690 case DM_RH_RECOVERING:
1da177e4
LT
691 this_list = &recover;
692 break;
693 }
694
695 bio_list_add(this_list, bio);
696 }
697
7513c2a7
JB
698 /*
699 * Add bios that are delayed due to remote recovery
700 * back on to the write queue
701 */
702 if (unlikely(requeue.head)) {
703 spin_lock_irq(&ms->lock);
704 bio_list_merge(&ms->writes, &requeue);
705 spin_unlock_irq(&ms->lock);
69885683 706 delayed_wake(ms);
7513c2a7
JB
707 }
708
1da177e4
LT
709 /*
710 * Increment the pending counts for any regions that will
711 * be written to (writes to recover regions are going to
712 * be delayed).
713 */
1f965b19
HM
714 dm_rh_inc_pending(ms->rh, &sync);
715 dm_rh_inc_pending(ms->rh, &nosync);
d2b69864
JB
716
717 /*
718 * If the flush fails on a previous call and succeeds here,
719 * we must not reset the log_failure variable. We need
720 * userspace interaction to do that.
721 */
722 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
1da177e4
LT
723
724 /*
725 * Dispatch io.
726 */
5528d17d 727 if (unlikely(ms->log_failure) && errors_handled(ms)) {
b80aa7a0
JB
728 spin_lock_irq(&ms->lock);
729 bio_list_merge(&ms->failures, &sync);
730 spin_unlock_irq(&ms->lock);
1f965b19 731 wakeup_mirrord(ms);
b80aa7a0 732 } else
fc1ff958 733 while ((bio = bio_list_pop(&sync)))
b80aa7a0 734 do_write(ms, bio);
1da177e4
LT
735
736 while ((bio = bio_list_pop(&recover)))
1f965b19 737 dm_rh_delay(ms->rh, bio);
1da177e4
LT
738
739 while ((bio = bio_list_pop(&nosync))) {
ede5ea0b
MP
740 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
741 spin_lock_irq(&ms->lock);
742 bio_list_add(&ms->failures, bio);
743 spin_unlock_irq(&ms->lock);
744 wakeup_mirrord(ms);
745 } else {
929be8fc
MP
746 map_bio(get_default_mirror(ms), bio);
747 generic_make_request(bio);
748 }
1da177e4
LT
749 }
750}
751
72f4b314
JB
752static void do_failures(struct mirror_set *ms, struct bio_list *failures)
753{
754 struct bio *bio;
755
0f398a84 756 if (likely(!failures->head))
72f4b314
JB
757 return;
758
b80aa7a0
JB
759 /*
760 * If the log has failed, unattempted writes are being
0f398a84 761 * put on the holds list. We can't issue those writes
b80aa7a0
JB
762 * until a log has been marked, so we must store them.
763 *
764 * If a 'noflush' suspend is in progress, we can requeue
765 * the I/O's to the core. This give userspace a chance
766 * to reconfigure the mirror, at which point the core
767 * will reissue the writes. If the 'noflush' flag is
768 * not set, we have no choice but to return errors.
769 *
770 * Some writes on the failures list may have been
771 * submitted before the log failure and represent a
772 * failure to write to one of the devices. It is ok
773 * for us to treat them the same and requeue them
774 * as well.
775 */
0f398a84 776 while ((bio = bio_list_pop(failures))) {
60f355ea 777 if (!ms->log_failure) {
0f398a84 778 ms->in_sync = 0;
c58098be 779 dm_rh_mark_nosync(ms->rh, bio);
0f398a84 780 }
60f355ea
MP
781
782 /*
783 * If all the legs are dead, fail the I/O.
784 * If we have been told to handle errors, hold the bio
785 * and wait for userspace to deal with the problem.
786 * Otherwise pretend that the I/O succeeded. (This would
787 * be wrong if the failed leg returned after reboot and
788 * got replicated back to the good legs.)
789 */
790 if (!get_valid_mirror(ms))
791 bio_endio(bio, -EIO);
792 else if (errors_handled(ms))
793 hold_bio(ms, bio);
794 else
795 bio_endio(bio, 0);
b80aa7a0 796 }
72f4b314
JB
797}
798
799static void trigger_event(struct work_struct *work)
800{
801 struct mirror_set *ms =
802 container_of(work, struct mirror_set, trigger_event);
803
804 dm_table_event(ms->ti->table);
805}
806
1da177e4
LT
807/*-----------------------------------------------------------------
808 * kmirrord
809 *---------------------------------------------------------------*/
a2aebe03 810static void do_mirror(struct work_struct *work)
1da177e4 811{
1f965b19
HM
812 struct mirror_set *ms = container_of(work, struct mirror_set,
813 kmirrord_work);
72f4b314
JB
814 struct bio_list reads, writes, failures;
815 unsigned long flags;
1da177e4 816
72f4b314 817 spin_lock_irqsave(&ms->lock, flags);
1da177e4
LT
818 reads = ms->reads;
819 writes = ms->writes;
72f4b314 820 failures = ms->failures;
1da177e4
LT
821 bio_list_init(&ms->reads);
822 bio_list_init(&ms->writes);
72f4b314
JB
823 bio_list_init(&ms->failures);
824 spin_unlock_irqrestore(&ms->lock, flags);
1da177e4 825
1f965b19 826 dm_rh_update_states(ms->rh, errors_handled(ms));
1da177e4
LT
827 do_recovery(ms);
828 do_reads(ms, &reads);
829 do_writes(ms, &writes);
72f4b314 830 do_failures(ms, &failures);
7ff14a36
MP
831
832 dm_table_unplug_all(ms->ti->table);
1da177e4
LT
833}
834
1da177e4
LT
835/*-----------------------------------------------------------------
836 * Target functions
837 *---------------------------------------------------------------*/
838static struct mirror_set *alloc_context(unsigned int nr_mirrors,
839 uint32_t region_size,
840 struct dm_target *ti,
416cd17b 841 struct dm_dirty_log *dl)
1da177e4
LT
842{
843 size_t len;
844 struct mirror_set *ms = NULL;
845
1da177e4
LT
846 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
847
dd00cc48 848 ms = kzalloc(len, GFP_KERNEL);
1da177e4 849 if (!ms) {
72d94861 850 ti->error = "Cannot allocate mirror context";
1da177e4
LT
851 return NULL;
852 }
853
1da177e4 854 spin_lock_init(&ms->lock);
5339fc2d
MP
855 bio_list_init(&ms->reads);
856 bio_list_init(&ms->writes);
857 bio_list_init(&ms->failures);
858 bio_list_init(&ms->holds);
1da177e4
LT
859
860 ms->ti = ti;
861 ms->nr_mirrors = nr_mirrors;
862 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
863 ms->in_sync = 0;
b80aa7a0 864 ms->log_failure = 0;
929be8fc 865 ms->leg_failure = 0;
b80aa7a0 866 atomic_set(&ms->suspend, 0);
72f4b314 867 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
1da177e4 868
95f8fac8
MP
869 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
870 _dm_raid1_read_record_cache);
871
06386bbf
JB
872 if (!ms->read_record_pool) {
873 ti->error = "Error creating mirror read_record_pool";
874 kfree(ms);
875 return NULL;
876 }
877
88be163a
MB
878 ms->io_client = dm_io_client_create(DM_IO_PAGES);
879 if (IS_ERR(ms->io_client)) {
880 ti->error = "Error creating dm_io client";
06386bbf 881 mempool_destroy(ms->read_record_pool);
88be163a
MB
882 kfree(ms);
883 return NULL;
884 }
885
1f965b19
HM
886 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
887 wakeup_all_recovery_waiters,
888 ms->ti->begin, MAX_RECOVERY,
889 dl, region_size, ms->nr_regions);
890 if (IS_ERR(ms->rh)) {
72d94861 891 ti->error = "Error creating dirty region hash";
a72cf737 892 dm_io_client_destroy(ms->io_client);
06386bbf 893 mempool_destroy(ms->read_record_pool);
1da177e4
LT
894 kfree(ms);
895 return NULL;
896 }
897
898 return ms;
899}
900
901static void free_context(struct mirror_set *ms, struct dm_target *ti,
902 unsigned int m)
903{
904 while (m--)
905 dm_put_device(ti, ms->mirror[m].dev);
906
88be163a 907 dm_io_client_destroy(ms->io_client);
1f965b19 908 dm_region_hash_destroy(ms->rh);
06386bbf 909 mempool_destroy(ms->read_record_pool);
1da177e4
LT
910 kfree(ms);
911}
912
1da177e4
LT
913static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
914 unsigned int mirror, char **argv)
915{
4ee218cd 916 unsigned long long offset;
1da177e4 917
4ee218cd 918 if (sscanf(argv[1], "%llu", &offset) != 1) {
72d94861 919 ti->error = "Invalid offset";
1da177e4
LT
920 return -EINVAL;
921 }
922
8215d6ec 923 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
1da177e4 924 &ms->mirror[mirror].dev)) {
72d94861 925 ti->error = "Device lookup failure";
1da177e4
LT
926 return -ENXIO;
927 }
928
aa5617c5 929 ms->mirror[mirror].ms = ms;
72f4b314
JB
930 atomic_set(&(ms->mirror[mirror].error_count), 0);
931 ms->mirror[mirror].error_type = 0;
1da177e4
LT
932 ms->mirror[mirror].offset = offset;
933
934 return 0;
935}
936
1da177e4
LT
937/*
938 * Create dirty log: log_type #log_params <log_params>
939 */
416cd17b 940static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
1f965b19
HM
941 unsigned argc, char **argv,
942 unsigned *args_used)
1da177e4 943{
1f965b19 944 unsigned param_count;
416cd17b 945 struct dm_dirty_log *dl;
1da177e4
LT
946
947 if (argc < 2) {
72d94861 948 ti->error = "Insufficient mirror log arguments";
1da177e4
LT
949 return NULL;
950 }
951
952 if (sscanf(argv[1], "%u", &param_count) != 1) {
72d94861 953 ti->error = "Invalid mirror log argument count";
1da177e4
LT
954 return NULL;
955 }
956
957 *args_used = 2 + param_count;
958
959 if (argc < *args_used) {
72d94861 960 ti->error = "Insufficient mirror log arguments";
1da177e4
LT
961 return NULL;
962 }
963
c0da3748
MP
964 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
965 argv + 2);
1da177e4 966 if (!dl) {
72d94861 967 ti->error = "Error creating mirror dirty log";
1da177e4
LT
968 return NULL;
969 }
970
1da177e4
LT
971 return dl;
972}
973
a8e6afa2
JB
974static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
975 unsigned *args_used)
976{
977 unsigned num_features;
978 struct dm_target *ti = ms->ti;
979
980 *args_used = 0;
981
982 if (!argc)
983 return 0;
984
985 if (sscanf(argv[0], "%u", &num_features) != 1) {
986 ti->error = "Invalid number of features";
987 return -EINVAL;
988 }
989
990 argc--;
991 argv++;
992 (*args_used)++;
993
994 if (num_features > argc) {
995 ti->error = "Not enough arguments to support feature count";
996 return -EINVAL;
997 }
998
999 if (!strcmp("handle_errors", argv[0]))
1000 ms->features |= DM_RAID1_HANDLE_ERRORS;
1001 else {
1002 ti->error = "Unrecognised feature requested";
1003 return -EINVAL;
1004 }
1005
1006 (*args_used)++;
1007
1008 return 0;
1009}
1010
1da177e4
LT
1011/*
1012 * Construct a mirror mapping:
1013 *
1014 * log_type #log_params <log_params>
1015 * #mirrors [mirror_path offset]{2,}
a8e6afa2 1016 * [#features <features>]
1da177e4
LT
1017 *
1018 * log_type is "core" or "disk"
1019 * #log_params is between 1 and 3
a8e6afa2
JB
1020 *
1021 * If present, features must be "handle_errors".
1da177e4 1022 */
1da177e4
LT
1023static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1024{
1025 int r;
1026 unsigned int nr_mirrors, m, args_used;
1027 struct mirror_set *ms;
416cd17b 1028 struct dm_dirty_log *dl;
1da177e4
LT
1029
1030 dl = create_dirty_log(ti, argc, argv, &args_used);
1031 if (!dl)
1032 return -EINVAL;
1033
1034 argv += args_used;
1035 argc -= args_used;
1036
1037 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
eb69aca5 1038 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
72d94861 1039 ti->error = "Invalid number of mirrors";
416cd17b 1040 dm_dirty_log_destroy(dl);
1da177e4
LT
1041 return -EINVAL;
1042 }
1043
1044 argv++, argc--;
1045
a8e6afa2
JB
1046 if (argc < nr_mirrors * 2) {
1047 ti->error = "Too few mirror arguments";
416cd17b 1048 dm_dirty_log_destroy(dl);
1da177e4
LT
1049 return -EINVAL;
1050 }
1051
1052 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1053 if (!ms) {
416cd17b 1054 dm_dirty_log_destroy(dl);
1da177e4
LT
1055 return -ENOMEM;
1056 }
1057
1058 /* Get the mirror parameter sets */
1059 for (m = 0; m < nr_mirrors; m++) {
1060 r = get_mirror(ms, ti, m, argv);
1061 if (r) {
1062 free_context(ms, ti, m);
1063 return r;
1064 }
1065 argv += 2;
1066 argc -= 2;
1067 }
1068
1069 ti->private = ms;
1f965b19 1070 ti->split_io = dm_rh_get_region_size(ms->rh);
4184153f 1071 ti->num_flush_requests = 1;
1da177e4 1072
6ad36fe2
HS
1073 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1074 if (!ms->kmirrord_wq) {
1075 DMERR("couldn't start kmirrord");
a72cf737
DM
1076 r = -ENOMEM;
1077 goto err_free_context;
6ad36fe2
HS
1078 }
1079 INIT_WORK(&ms->kmirrord_work, do_mirror);
a2aebe03
MP
1080 init_timer(&ms->timer);
1081 ms->timer_pending = 0;
72f4b314 1082 INIT_WORK(&ms->trigger_event, trigger_event);
6ad36fe2 1083
a8e6afa2 1084 r = parse_features(ms, argc, argv, &args_used);
a72cf737
DM
1085 if (r)
1086 goto err_destroy_wq;
a8e6afa2
JB
1087
1088 argv += args_used;
1089 argc -= args_used;
1090
f44db678
JB
1091 /*
1092 * Any read-balancing addition depends on the
1093 * DM_RAID1_HANDLE_ERRORS flag being present.
1094 * This is because the decision to balance depends
1095 * on the sync state of a region. If the above
1096 * flag is not present, we ignore errors; and
1097 * the sync state may be inaccurate.
1098 */
1099
a8e6afa2
JB
1100 if (argc) {
1101 ti->error = "Too many mirror arguments";
a72cf737
DM
1102 r = -EINVAL;
1103 goto err_destroy_wq;
a8e6afa2
JB
1104 }
1105
1f965b19 1106 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
a72cf737
DM
1107 if (r)
1108 goto err_destroy_wq;
1da177e4 1109
1f965b19 1110 wakeup_mirrord(ms);
1da177e4 1111 return 0;
a72cf737
DM
1112
1113err_destroy_wq:
1114 destroy_workqueue(ms->kmirrord_wq);
1115err_free_context:
1116 free_context(ms, ti, ms->nr_mirrors);
1117 return r;
1da177e4
LT
1118}
1119
1120static void mirror_dtr(struct dm_target *ti)
1121{
1122 struct mirror_set *ms = (struct mirror_set *) ti->private;
1123
a2aebe03 1124 del_timer_sync(&ms->timer);
6ad36fe2 1125 flush_workqueue(ms->kmirrord_wq);
18776c73 1126 flush_scheduled_work();
eb69aca5 1127 dm_kcopyd_client_destroy(ms->kcopyd_client);
6ad36fe2 1128 destroy_workqueue(ms->kmirrord_wq);
1da177e4
LT
1129 free_context(ms, ti, ms->nr_mirrors);
1130}
1131
1da177e4
LT
1132/*
1133 * Mirror mapping function
1134 */
1135static int mirror_map(struct dm_target *ti, struct bio *bio,
1136 union map_info *map_context)
1137{
1138 int r, rw = bio_rw(bio);
1139 struct mirror *m;
1140 struct mirror_set *ms = ti->private;
06386bbf 1141 struct dm_raid1_read_record *read_record = NULL;
1f965b19 1142 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1da177e4
LT
1143
1144 if (rw == WRITE) {
06386bbf 1145 /* Save region for mirror_end_io() handler */
1f965b19 1146 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
1da177e4 1147 queue_bio(ms, bio, rw);
d2a7ad29 1148 return DM_MAPIO_SUBMITTED;
1da177e4
LT
1149 }
1150
1f965b19 1151 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1da177e4
LT
1152 if (r < 0 && r != -EWOULDBLOCK)
1153 return r;
1154
1da177e4 1155 /*
06386bbf 1156 * If region is not in-sync queue the bio.
1da177e4 1157 */
06386bbf
JB
1158 if (!r || (r == -EWOULDBLOCK)) {
1159 if (rw == READA)
1160 return -EWOULDBLOCK;
1da177e4 1161
1da177e4 1162 queue_bio(ms, bio, rw);
d2a7ad29 1163 return DM_MAPIO_SUBMITTED;
1da177e4
LT
1164 }
1165
06386bbf
JB
1166 /*
1167 * The region is in-sync and we can perform reads directly.
1168 * Store enough information so we can retry if it fails.
1169 */
1da177e4 1170 m = choose_mirror(ms, bio->bi_sector);
06386bbf 1171 if (unlikely(!m))
1da177e4
LT
1172 return -EIO;
1173
06386bbf
JB
1174 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1175 if (likely(read_record)) {
1176 dm_bio_record(&read_record->details, bio);
1177 map_context->ptr = read_record;
1178 read_record->m = m;
1179 }
1180
1181 map_bio(m, bio);
1182
d2a7ad29 1183 return DM_MAPIO_REMAPPED;
1da177e4
LT
1184}
1185
1186static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1187 int error, union map_info *map_context)
1188{
1189 int rw = bio_rw(bio);
1190 struct mirror_set *ms = (struct mirror_set *) ti->private;
06386bbf
JB
1191 struct mirror *m = NULL;
1192 struct dm_bio_details *bd = NULL;
1193 struct dm_raid1_read_record *read_record = map_context->ptr;
1da177e4
LT
1194
1195 /*
1196 * We need to dec pending if this was a write.
1197 */
06386bbf 1198 if (rw == WRITE) {
4184153f
MP
1199 if (likely(!bio_empty_barrier(bio)))
1200 dm_rh_dec(ms->rh, map_context->ll);
06386bbf
JB
1201 return error;
1202 }
1da177e4 1203
06386bbf
JB
1204 if (error == -EOPNOTSUPP)
1205 goto out;
1206
1f98a13f 1207 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
06386bbf
JB
1208 goto out;
1209
1210 if (unlikely(error)) {
1211 if (!read_record) {
1212 /*
1213 * There wasn't enough memory to record necessary
1214 * information for a retry or there was no other
1215 * mirror in-sync.
1216 */
e03f1a84 1217 DMERR_LIMIT("Mirror read failed.");
06386bbf
JB
1218 return -EIO;
1219 }
e03f1a84
AB
1220
1221 m = read_record->m;
1222
06386bbf
JB
1223 DMERR("Mirror read failed from %s. Trying alternative device.",
1224 m->dev->name);
1225
06386bbf
JB
1226 fail_mirror(m, DM_RAID1_READ_ERROR);
1227
1228 /*
1229 * A failed read is requeued for another attempt using an intact
1230 * mirror.
1231 */
1232 if (default_ok(m) || mirror_available(ms, bio)) {
1233 bd = &read_record->details;
1234
1235 dm_bio_restore(bd, bio);
1236 mempool_free(read_record, ms->read_record_pool);
1237 map_context->ptr = NULL;
1238 queue_bio(ms, bio, rw);
1239 return 1;
1240 }
1241 DMERR("All replicated volumes dead, failing I/O");
1242 }
1243
1244out:
1245 if (read_record) {
1246 mempool_free(read_record, ms->read_record_pool);
1247 map_context->ptr = NULL;
1248 }
1249
1250 return error;
1da177e4
LT
1251}
1252
b80aa7a0 1253static void mirror_presuspend(struct dm_target *ti)
1da177e4
LT
1254{
1255 struct mirror_set *ms = (struct mirror_set *) ti->private;
1f965b19 1256 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1da177e4 1257
04788507
MP
1258 struct bio_list holds;
1259 struct bio *bio;
1260
b80aa7a0
JB
1261 atomic_set(&ms->suspend, 1);
1262
1263 /*
1264 * We must finish up all the work that we've
1265 * generated (i.e. recovery work).
1266 */
1f965b19 1267 dm_rh_stop_recovery(ms->rh);
33184048 1268
33184048 1269 wait_event(_kmirrord_recovery_stopped,
1f965b19 1270 !dm_rh_recovery_in_flight(ms->rh));
33184048 1271
b80aa7a0
JB
1272 if (log->type->presuspend && log->type->presuspend(log))
1273 /* FIXME: need better error handling */
1274 DMWARN("log presuspend failed");
1275
1276 /*
1277 * Now that recovery is complete/stopped and the
1278 * delayed bios are queued, we need to wait for
1279 * the worker thread to complete. This way,
1280 * we know that all of our I/O has been pushed.
1281 */
1282 flush_workqueue(ms->kmirrord_wq);
04788507
MP
1283
1284 /*
1285 * Now set ms->suspend is set and the workqueue flushed, no more
1286 * entries can be added to ms->hold list, so process it.
1287 *
1288 * Bios can still arrive concurrently with or after this
1289 * presuspend function, but they cannot join the hold list
1290 * because ms->suspend is set.
1291 */
1292 spin_lock_irq(&ms->lock);
1293 holds = ms->holds;
1294 bio_list_init(&ms->holds);
1295 spin_unlock_irq(&ms->lock);
1296
1297 while ((bio = bio_list_pop(&holds)))
1298 hold_bio(ms, bio);
b80aa7a0
JB
1299}
1300
1301static void mirror_postsuspend(struct dm_target *ti)
1302{
1303 struct mirror_set *ms = ti->private;
1f965b19 1304 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
b80aa7a0 1305
6b3df0d7 1306 if (log->type->postsuspend && log->type->postsuspend(log))
1da177e4 1307 /* FIXME: need better error handling */
b80aa7a0 1308 DMWARN("log postsuspend failed");
1da177e4
LT
1309}
1310
1311static void mirror_resume(struct dm_target *ti)
1312{
b80aa7a0 1313 struct mirror_set *ms = ti->private;
1f965b19 1314 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
b80aa7a0
JB
1315
1316 atomic_set(&ms->suspend, 0);
1da177e4
LT
1317 if (log->type->resume && log->type->resume(log))
1318 /* FIXME: need better error handling */
1319 DMWARN("log resume failed");
1f965b19 1320 dm_rh_start_recovery(ms->rh);
1da177e4
LT
1321}
1322
af195ac8
JB
1323/*
1324 * device_status_char
1325 * @m: mirror device/leg we want the status of
1326 *
1327 * We return one character representing the most severe error
1328 * we have encountered.
1329 * A => Alive - No failures
1330 * D => Dead - A write failure occurred leaving mirror out-of-sync
1331 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1332 * R => Read - A read failure occurred, mirror data unaffected
1333 *
1334 * Returns: <char>
1335 */
1336static char device_status_char(struct mirror *m)
1337{
1338 if (!atomic_read(&(m->error_count)))
1339 return 'A';
1340
64b30c46
MP
1341 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1342 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
af195ac8
JB
1343 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1344 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1345}
1346
1347
1da177e4
LT
1348static int mirror_status(struct dm_target *ti, status_type_t type,
1349 char *result, unsigned int maxlen)
1350{
315dcc22 1351 unsigned int m, sz = 0;
1da177e4 1352 struct mirror_set *ms = (struct mirror_set *) ti->private;
1f965b19 1353 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
af195ac8 1354 char buffer[ms->nr_mirrors + 1];
1da177e4 1355
1da177e4
LT
1356 switch (type) {
1357 case STATUSTYPE_INFO:
1358 DMEMIT("%d ", ms->nr_mirrors);
af195ac8 1359 for (m = 0; m < ms->nr_mirrors; m++) {
1da177e4 1360 DMEMIT("%s ", ms->mirror[m].dev->name);
af195ac8
JB
1361 buffer[m] = device_status_char(&(ms->mirror[m]));
1362 }
1363 buffer[m] = '\0';
1da177e4 1364
af195ac8 1365 DMEMIT("%llu/%llu 1 %s ",
1f965b19 1366 (unsigned long long)log->type->get_sync_count(log),
af195ac8 1367 (unsigned long long)ms->nr_regions, buffer);
315dcc22 1368
1f965b19 1369 sz += log->type->status(log, type, result+sz, maxlen-sz);
315dcc22 1370
1da177e4
LT
1371 break;
1372
1373 case STATUSTYPE_TABLE:
1f965b19 1374 sz = log->type->status(log, type, result, maxlen);
315dcc22 1375
e52b8f6d 1376 DMEMIT("%d", ms->nr_mirrors);
1da177e4 1377 for (m = 0; m < ms->nr_mirrors; m++)
e52b8f6d 1378 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
b80aa7a0 1379 (unsigned long long)ms->mirror[m].offset);
a8e6afa2
JB
1380
1381 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1382 DMEMIT(" 1 handle_errors");
1da177e4
LT
1383 }
1384
1385 return 0;
1386}
1387
af4874e0
MS
1388static int mirror_iterate_devices(struct dm_target *ti,
1389 iterate_devices_callout_fn fn, void *data)
1390{
1391 struct mirror_set *ms = ti->private;
1392 int ret = 0;
1393 unsigned i;
1394
1395 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1396 ret = fn(ti, ms->mirror[i].dev,
5dea271b 1397 ms->mirror[i].offset, ti->len, data);
af4874e0
MS
1398
1399 return ret;
1400}
1401
1da177e4
LT
1402static struct target_type mirror_target = {
1403 .name = "mirror",
af4874e0 1404 .version = {1, 12, 0},
1da177e4
LT
1405 .module = THIS_MODULE,
1406 .ctr = mirror_ctr,
1407 .dtr = mirror_dtr,
1408 .map = mirror_map,
1409 .end_io = mirror_end_io,
b80aa7a0 1410 .presuspend = mirror_presuspend,
1da177e4
LT
1411 .postsuspend = mirror_postsuspend,
1412 .resume = mirror_resume,
1413 .status = mirror_status,
af4874e0 1414 .iterate_devices = mirror_iterate_devices,
1da177e4
LT
1415};
1416
1417static int __init dm_mirror_init(void)
1418{
1419 int r;
1420
95f8fac8
MP
1421 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1422 if (!_dm_raid1_read_record_cache) {
1423 DMERR("Can't allocate dm_raid1_read_record cache");
1424 r = -ENOMEM;
1425 goto bad_cache;
1426 }
1427
1da177e4 1428 r = dm_register_target(&mirror_target);
95f8fac8 1429 if (r < 0) {
0cd33124 1430 DMERR("Failed to register mirror target");
95f8fac8
MP
1431 goto bad_target;
1432 }
1433
1434 return 0;
1da177e4 1435
95f8fac8
MP
1436bad_target:
1437 kmem_cache_destroy(_dm_raid1_read_record_cache);
1438bad_cache:
1da177e4
LT
1439 return r;
1440}
1441
1442static void __exit dm_mirror_exit(void)
1443{
10d3bd09 1444 dm_unregister_target(&mirror_target);
95f8fac8 1445 kmem_cache_destroy(_dm_raid1_read_record_cache);
1da177e4
LT
1446}
1447
1448/* Module hooks */
1449module_init(dm_mirror_init);
1450module_exit(dm_mirror_exit);
1451
1452MODULE_DESCRIPTION(DM_NAME " mirror target");
1453MODULE_AUTHOR("Joe Thornber");
1454MODULE_LICENSE("GPL");