]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/md/dm-raid1.c
dm: mark split bio as cloned
[mirror_ubuntu-bionic-kernel.git] / drivers / md / dm-raid1.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 *
4 * This file is released under the GPL.
5 */
6
586e80e6
MP
7#include <linux/device-mapper.h>
8
1da177e4 9#include "dm-bio-list.h"
06386bbf 10#include "dm-bio-record.h"
1da177e4
LT
11
12#include <linux/ctype.h>
13#include <linux/init.h>
14#include <linux/mempool.h>
15#include <linux/module.h>
16#include <linux/pagemap.h>
17#include <linux/slab.h>
18#include <linux/time.h>
19#include <linux/vmalloc.h>
20#include <linux/workqueue.h>
6f3c3f0a 21#include <linux/log2.h>
72f4b314 22#include <linux/hardirq.h>
a765e20e
AK
23#include <linux/dm-io.h>
24#include <linux/dm-dirty-log.h>
25#include <linux/dm-kcopyd.h>
1da177e4 26
72d94861 27#define DM_MSG_PREFIX "raid1"
88be163a 28#define DM_IO_PAGES 64
72d94861 29
a8e6afa2 30#define DM_RAID1_HANDLE_ERRORS 0x01
f44db678 31#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
a8e6afa2 32
33184048 33static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
1da177e4 34
1da177e4
LT
35/*-----------------------------------------------------------------
36 * Region hash
37 *
38 * The mirror splits itself up into discrete regions. Each
39 * region can be in one of three states: clean, dirty,
40 * nosync. There is no need to put clean regions in the hash.
41 *
42 * In addition to being present in the hash table a region _may_
43 * be present on one of three lists.
44 *
45 * clean_regions: Regions on this list have no io pending to
46 * them, they are in sync, we are no longer interested in them,
47 * they are dull. rh_update_states() will remove them from the
48 * hash table.
49 *
50 * quiesced_regions: These regions have been spun down, ready
51 * for recovery. rh_recovery_start() will remove regions from
52 * this list and hand them to kmirrord, which will schedule the
53 * recovery io with kcopyd.
54 *
55 * recovered_regions: Regions that kcopyd has successfully
56 * recovered. rh_update_states() will now schedule any delayed
57 * io, up the recovery_count, and remove the region from the
58 * hash.
59 *
60 * There are 2 locks:
61 * A rw spin lock 'hash_lock' protects just the hash table,
62 * this is never held in write mode from interrupt context,
63 * which I believe means that we only have to disable irqs when
64 * doing a write lock.
65 *
66 * An ordinary spin lock 'region_lock' that protects the three
67 * lists in the region_hash, with the 'state', 'list' and
68 * 'bhs_delayed' fields of the regions. This is used from irq
69 * context, so all other uses will have to suspend local irqs.
70 *---------------------------------------------------------------*/
71struct mirror_set;
72struct region_hash {
73 struct mirror_set *ms;
74 uint32_t region_size;
75 unsigned region_shift;
76
77 /* holds persistent region state */
416cd17b 78 struct dm_dirty_log *log;
1da177e4
LT
79
80 /* hash table */
81 rwlock_t hash_lock;
82 mempool_t *region_pool;
83 unsigned int mask;
84 unsigned int nr_buckets;
85 struct list_head *buckets;
86
87 spinlock_t region_lock;
33184048 88 atomic_t recovery_in_flight;
1da177e4
LT
89 struct semaphore recovery_count;
90 struct list_head clean_regions;
91 struct list_head quiesced_regions;
92 struct list_head recovered_regions;
f44db678 93 struct list_head failed_recovered_regions;
1da177e4
LT
94};
95
96enum {
97 RH_CLEAN,
98 RH_DIRTY,
99 RH_NOSYNC,
100 RH_RECOVERING
101};
102
103struct region {
104 struct region_hash *rh; /* FIXME: can we get rid of this ? */
105 region_t key;
106 int state;
107
108 struct list_head hash_list;
109 struct list_head list;
110
111 atomic_t pending;
112 struct bio_list delayed_bios;
113};
114
e4c8b3ba
NB
115
116/*-----------------------------------------------------------------
117 * Mirror set structures.
118 *---------------------------------------------------------------*/
72f4b314
JB
119enum dm_raid1_error {
120 DM_RAID1_WRITE_ERROR,
121 DM_RAID1_SYNC_ERROR,
122 DM_RAID1_READ_ERROR
123};
124
e4c8b3ba 125struct mirror {
aa5617c5 126 struct mirror_set *ms;
e4c8b3ba 127 atomic_t error_count;
39ed7adb 128 unsigned long error_type;
e4c8b3ba
NB
129 struct dm_dev *dev;
130 sector_t offset;
131};
132
133struct mirror_set {
134 struct dm_target *ti;
135 struct list_head list;
136 struct region_hash rh;
eb69aca5 137 struct dm_kcopyd_client *kcopyd_client;
a8e6afa2 138 uint64_t features;
e4c8b3ba 139
72f4b314 140 spinlock_t lock; /* protects the lists */
e4c8b3ba
NB
141 struct bio_list reads;
142 struct bio_list writes;
72f4b314 143 struct bio_list failures;
e4c8b3ba 144
88be163a 145 struct dm_io_client *io_client;
06386bbf 146 mempool_t *read_record_pool;
88be163a 147
e4c8b3ba
NB
148 /* recovery */
149 region_t nr_regions;
150 int in_sync;
fc1ff958 151 int log_failure;
b80aa7a0 152 atomic_t suspend;
e4c8b3ba 153
72f4b314 154 atomic_t default_mirror; /* Default mirror */
e4c8b3ba 155
6ad36fe2
HS
156 struct workqueue_struct *kmirrord_wq;
157 struct work_struct kmirrord_work;
a2aebe03
MP
158 struct timer_list timer;
159 unsigned long timer_pending;
160
72f4b314 161 struct work_struct trigger_event;
6ad36fe2 162
e4c8b3ba
NB
163 unsigned int nr_mirrors;
164 struct mirror mirror[0];
165};
166
1da177e4
LT
167/*
168 * Conversion fns
169 */
170static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
171{
e4c8b3ba 172 return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
1da177e4
LT
173}
174
175static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
176{
177 return region << rh->region_shift;
178}
179
6ad36fe2
HS
180static void wake(struct mirror_set *ms)
181{
182 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
183}
184
a2aebe03
MP
185static void delayed_wake_fn(unsigned long data)
186{
187 struct mirror_set *ms = (struct mirror_set *) data;
188
189 clear_bit(0, &ms->timer_pending);
190 wake(ms);
191}
192
193static void delayed_wake(struct mirror_set *ms)
194{
195 if (test_and_set_bit(0, &ms->timer_pending))
196 return;
197
198 ms->timer.expires = jiffies + HZ / 5;
199 ms->timer.data = (unsigned long) ms;
200 ms->timer.function = delayed_wake_fn;
201 add_timer(&ms->timer);
202}
203
1da177e4
LT
204/* FIXME move this */
205static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
206
1da177e4
LT
207#define MIN_REGIONS 64
208#define MAX_RECOVERY 1
209static int rh_init(struct region_hash *rh, struct mirror_set *ms,
416cd17b 210 struct dm_dirty_log *log, uint32_t region_size,
1da177e4
LT
211 region_t nr_regions)
212{
213 unsigned int nr_buckets, max_buckets;
214 size_t i;
215
216 /*
217 * Calculate a suitable number of buckets for our hash
218 * table.
219 */
220 max_buckets = nr_regions >> 6;
221 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
222 ;
223 nr_buckets >>= 1;
224
225 rh->ms = ms;
226 rh->log = log;
227 rh->region_size = region_size;
228 rh->region_shift = ffs(region_size) - 1;
229 rwlock_init(&rh->hash_lock);
230 rh->mask = nr_buckets - 1;
231 rh->nr_buckets = nr_buckets;
232
233 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
234 if (!rh->buckets) {
235 DMERR("unable to allocate region hash memory");
236 return -ENOMEM;
237 }
238
239 for (i = 0; i < nr_buckets; i++)
240 INIT_LIST_HEAD(rh->buckets + i);
241
242 spin_lock_init(&rh->region_lock);
243 sema_init(&rh->recovery_count, 0);
33184048 244 atomic_set(&rh->recovery_in_flight, 0);
1da177e4
LT
245 INIT_LIST_HEAD(&rh->clean_regions);
246 INIT_LIST_HEAD(&rh->quiesced_regions);
247 INIT_LIST_HEAD(&rh->recovered_regions);
f44db678 248 INIT_LIST_HEAD(&rh->failed_recovered_regions);
1da177e4 249
0eaae62a
MD
250 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
251 sizeof(struct region));
1da177e4
LT
252 if (!rh->region_pool) {
253 vfree(rh->buckets);
254 rh->buckets = NULL;
255 return -ENOMEM;
256 }
257
258 return 0;
259}
260
261static void rh_exit(struct region_hash *rh)
262{
263 unsigned int h;
264 struct region *reg, *nreg;
265
266 BUG_ON(!list_empty(&rh->quiesced_regions));
267 for (h = 0; h < rh->nr_buckets; h++) {
268 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
269 BUG_ON(atomic_read(&reg->pending));
270 mempool_free(reg, rh->region_pool);
271 }
272 }
273
274 if (rh->log)
416cd17b 275 dm_dirty_log_destroy(rh->log);
1da177e4
LT
276 if (rh->region_pool)
277 mempool_destroy(rh->region_pool);
278 vfree(rh->buckets);
279}
280
281#define RH_HASH_MULT 2654435387U
282
283static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
284{
285 return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
286}
287
288static struct region *__rh_lookup(struct region_hash *rh, region_t region)
289{
290 struct region *reg;
291
292 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
293 if (reg->key == region)
294 return reg;
295
296 return NULL;
297}
298
299static void __rh_insert(struct region_hash *rh, struct region *reg)
300{
301 unsigned int h = rh_hash(rh, reg->key);
302 list_add(&reg->hash_list, rh->buckets + h);
303}
304
305static struct region *__rh_alloc(struct region_hash *rh, region_t region)
306{
307 struct region *reg, *nreg;
308
309 read_unlock(&rh->hash_lock);
c06aad85
DK
310 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
311 if (unlikely(!nreg))
312 nreg = kmalloc(sizeof(struct region), GFP_NOIO);
1da177e4
LT
313 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
314 RH_CLEAN : RH_NOSYNC;
315 nreg->rh = rh;
316 nreg->key = region;
317
318 INIT_LIST_HEAD(&nreg->list);
319
320 atomic_set(&nreg->pending, 0);
321 bio_list_init(&nreg->delayed_bios);
322 write_lock_irq(&rh->hash_lock);
323
324 reg = __rh_lookup(rh, region);
325 if (reg)
326 /* we lost the race */
327 mempool_free(nreg, rh->region_pool);
328
329 else {
330 __rh_insert(rh, nreg);
331 if (nreg->state == RH_CLEAN) {
332 spin_lock(&rh->region_lock);
333 list_add(&nreg->list, &rh->clean_regions);
334 spin_unlock(&rh->region_lock);
335 }
336 reg = nreg;
337 }
338 write_unlock_irq(&rh->hash_lock);
339 read_lock(&rh->hash_lock);
340
341 return reg;
342}
343
344static inline struct region *__rh_find(struct region_hash *rh, region_t region)
345{
346 struct region *reg;
347
348 reg = __rh_lookup(rh, region);
349 if (!reg)
350 reg = __rh_alloc(rh, region);
351
352 return reg;
353}
354
355static int rh_state(struct region_hash *rh, region_t region, int may_block)
356{
357 int r;
358 struct region *reg;
359
360 read_lock(&rh->hash_lock);
361 reg = __rh_lookup(rh, region);
362 read_unlock(&rh->hash_lock);
363
364 if (reg)
365 return reg->state;
366
367 /*
368 * The region wasn't in the hash, so we fall back to the
369 * dirty log.
370 */
371 r = rh->log->type->in_sync(rh->log, region, may_block);
372
373 /*
374 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
375 * taken as a RH_NOSYNC
376 */
377 return r == 1 ? RH_CLEAN : RH_NOSYNC;
378}
379
380static inline int rh_in_sync(struct region_hash *rh,
381 region_t region, int may_block)
382{
383 int state = rh_state(rh, region, may_block);
384 return state == RH_CLEAN || state == RH_DIRTY;
385}
386
387static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
388{
389 struct bio *bio;
390
391 while ((bio = bio_list_pop(bio_list))) {
392 queue_bio(ms, bio, WRITE);
393 }
394}
395
f3ee6b2f
JB
396static void complete_resync_work(struct region *reg, int success)
397{
398 struct region_hash *rh = reg->rh;
399
400 rh->log->type->set_region_sync(rh->log, reg->key, success);
b80aa7a0
JB
401
402 /*
403 * Dispatch the bios before we call 'wake_up_all'.
404 * This is important because if we are suspending,
405 * we want to know that recovery is complete and
406 * the work queue is flushed. If we wake_up_all
407 * before we dispatch_bios (queue bios and call wake()),
408 * then we risk suspending before the work queue
409 * has been properly flushed.
410 */
f3ee6b2f
JB
411 dispatch_bios(rh->ms, &reg->delayed_bios);
412 if (atomic_dec_and_test(&rh->recovery_in_flight))
413 wake_up_all(&_kmirrord_recovery_stopped);
414 up(&rh->recovery_count);
415}
416
1da177e4
LT
417static void rh_update_states(struct region_hash *rh)
418{
419 struct region *reg, *next;
420
421 LIST_HEAD(clean);
422 LIST_HEAD(recovered);
f44db678 423 LIST_HEAD(failed_recovered);
1da177e4
LT
424
425 /*
426 * Quickly grab the lists.
427 */
428 write_lock_irq(&rh->hash_lock);
429 spin_lock(&rh->region_lock);
430 if (!list_empty(&rh->clean_regions)) {
c12bfc92 431 list_splice_init(&rh->clean_regions, &clean);
1da177e4 432
943317ef 433 list_for_each_entry(reg, &clean, list)
1da177e4 434 list_del(&reg->hash_list);
1da177e4
LT
435 }
436
437 if (!list_empty(&rh->recovered_regions)) {
c12bfc92 438 list_splice_init(&rh->recovered_regions, &recovered);
1da177e4
LT
439
440 list_for_each_entry (reg, &recovered, list)
441 list_del(&reg->hash_list);
442 }
f44db678
JB
443
444 if (!list_empty(&rh->failed_recovered_regions)) {
c12bfc92
RD
445 list_splice_init(&rh->failed_recovered_regions,
446 &failed_recovered);
f44db678
JB
447
448 list_for_each_entry(reg, &failed_recovered, list)
449 list_del(&reg->hash_list);
450 }
451
1da177e4
LT
452 spin_unlock(&rh->region_lock);
453 write_unlock_irq(&rh->hash_lock);
454
455 /*
456 * All the regions on the recovered and clean lists have
457 * now been pulled out of the system, so no need to do
458 * any more locking.
459 */
460 list_for_each_entry_safe (reg, next, &recovered, list) {
461 rh->log->type->clear_region(rh->log, reg->key);
f3ee6b2f 462 complete_resync_work(reg, 1);
1da177e4
LT
463 mempool_free(reg, rh->region_pool);
464 }
465
f44db678
JB
466 list_for_each_entry_safe(reg, next, &failed_recovered, list) {
467 complete_resync_work(reg, errors_handled(rh->ms) ? 0 : 1);
468 mempool_free(reg, rh->region_pool);
469 }
470
943317ef
JB
471 list_for_each_entry_safe(reg, next, &clean, list) {
472 rh->log->type->clear_region(rh->log, reg->key);
1da177e4 473 mempool_free(reg, rh->region_pool);
943317ef
JB
474 }
475
476 rh->log->type->flush(rh->log);
1da177e4
LT
477}
478
479static void rh_inc(struct region_hash *rh, region_t region)
480{
481 struct region *reg;
482
483 read_lock(&rh->hash_lock);
484 reg = __rh_find(rh, region);
844e8d90 485
7692c5dd 486 spin_lock_irq(&rh->region_lock);
844e8d90
JN
487 atomic_inc(&reg->pending);
488
1da177e4 489 if (reg->state == RH_CLEAN) {
1da177e4
LT
490 reg->state = RH_DIRTY;
491 list_del_init(&reg->list); /* take off the clean list */
7692c5dd
JB
492 spin_unlock_irq(&rh->region_lock);
493
494 rh->log->type->mark_region(rh->log, reg->key);
495 } else
496 spin_unlock_irq(&rh->region_lock);
497
1da177e4 498
1da177e4
LT
499 read_unlock(&rh->hash_lock);
500}
501
502static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
503{
504 struct bio *bio;
505
506 for (bio = bios->head; bio; bio = bio->bi_next)
507 rh_inc(rh, bio_to_region(rh, bio));
508}
509
510static void rh_dec(struct region_hash *rh, region_t region)
511{
512 unsigned long flags;
513 struct region *reg;
514 int should_wake = 0;
515
516 read_lock(&rh->hash_lock);
517 reg = __rh_lookup(rh, region);
518 read_unlock(&rh->hash_lock);
519
7692c5dd 520 spin_lock_irqsave(&rh->region_lock, flags);
1da177e4 521 if (atomic_dec_and_test(&reg->pending)) {
930d332a
JN
522 /*
523 * There is no pending I/O for this region.
524 * We can move the region to corresponding list for next action.
525 * At this point, the region is not yet connected to any list.
526 *
527 * If the state is RH_NOSYNC, the region should be kept off
528 * from clean list.
529 * The hash entry for RH_NOSYNC will remain in memory
530 * until the region is recovered or the map is reloaded.
531 */
532
533 /* do nothing for RH_NOSYNC */
1da177e4
LT
534 if (reg->state == RH_RECOVERING) {
535 list_add_tail(&reg->list, &rh->quiesced_regions);
930d332a 536 } else if (reg->state == RH_DIRTY) {
1da177e4
LT
537 reg->state = RH_CLEAN;
538 list_add(&reg->list, &rh->clean_regions);
539 }
1da177e4
LT
540 should_wake = 1;
541 }
7692c5dd 542 spin_unlock_irqrestore(&rh->region_lock, flags);
1da177e4
LT
543
544 if (should_wake)
6ad36fe2 545 wake(rh->ms);
1da177e4
LT
546}
547
548/*
549 * Starts quiescing a region in preparation for recovery.
550 */
551static int __rh_recovery_prepare(struct region_hash *rh)
552{
553 int r;
554 struct region *reg;
555 region_t region;
556
557 /*
558 * Ask the dirty log what's next.
559 */
560 r = rh->log->type->get_resync_work(rh->log, &region);
561 if (r <= 0)
562 return r;
563
564 /*
565 * Get this region, and start it quiescing by setting the
566 * recovering flag.
567 */
568 read_lock(&rh->hash_lock);
569 reg = __rh_find(rh, region);
570 read_unlock(&rh->hash_lock);
571
572 spin_lock_irq(&rh->region_lock);
573 reg->state = RH_RECOVERING;
574
575 /* Already quiesced ? */
576 if (atomic_read(&reg->pending))
577 list_del_init(&reg->list);
179e0917
AM
578 else
579 list_move(&reg->list, &rh->quiesced_regions);
1da177e4 580
1da177e4
LT
581 spin_unlock_irq(&rh->region_lock);
582
583 return 1;
584}
585
586static void rh_recovery_prepare(struct region_hash *rh)
587{
33184048
JB
588 /* Extra reference to avoid race with rh_stop_recovery */
589 atomic_inc(&rh->recovery_in_flight);
590
591 while (!down_trylock(&rh->recovery_count)) {
592 atomic_inc(&rh->recovery_in_flight);
1da177e4 593 if (__rh_recovery_prepare(rh) <= 0) {
33184048 594 atomic_dec(&rh->recovery_in_flight);
1da177e4
LT
595 up(&rh->recovery_count);
596 break;
597 }
33184048
JB
598 }
599
600 /* Drop the extra reference */
601 if (atomic_dec_and_test(&rh->recovery_in_flight))
602 wake_up_all(&_kmirrord_recovery_stopped);
1da177e4
LT
603}
604
605/*
606 * Returns any quiesced regions.
607 */
608static struct region *rh_recovery_start(struct region_hash *rh)
609{
610 struct region *reg = NULL;
611
612 spin_lock_irq(&rh->region_lock);
613 if (!list_empty(&rh->quiesced_regions)) {
614 reg = list_entry(rh->quiesced_regions.next,
615 struct region, list);
616 list_del_init(&reg->list); /* remove from the quiesced list */
617 }
618 spin_unlock_irq(&rh->region_lock);
619
620 return reg;
621}
622
1da177e4
LT
623static void rh_recovery_end(struct region *reg, int success)
624{
625 struct region_hash *rh = reg->rh;
626
627 spin_lock_irq(&rh->region_lock);
f44db678
JB
628 if (success)
629 list_add(&reg->list, &reg->rh->recovered_regions);
630 else {
631 reg->state = RH_NOSYNC;
632 list_add(&reg->list, &reg->rh->failed_recovered_regions);
633 }
1da177e4
LT
634 spin_unlock_irq(&rh->region_lock);
635
6ad36fe2 636 wake(rh->ms);
1da177e4
LT
637}
638
fc1ff958 639static int rh_flush(struct region_hash *rh)
1da177e4 640{
fc1ff958 641 return rh->log->type->flush(rh->log);
1da177e4
LT
642}
643
644static void rh_delay(struct region_hash *rh, struct bio *bio)
645{
646 struct region *reg;
647
648 read_lock(&rh->hash_lock);
649 reg = __rh_find(rh, bio_to_region(rh, bio));
650 bio_list_add(&reg->delayed_bios, bio);
651 read_unlock(&rh->hash_lock);
652}
653
654static void rh_stop_recovery(struct region_hash *rh)
655{
656 int i;
657
658 /* wait for any recovering regions */
659 for (i = 0; i < MAX_RECOVERY; i++)
660 down(&rh->recovery_count);
661}
662
663static void rh_start_recovery(struct region_hash *rh)
664{
665 int i;
666
667 for (i = 0; i < MAX_RECOVERY; i++)
668 up(&rh->recovery_count);
669
6ad36fe2 670 wake(rh->ms);
1da177e4
LT
671}
672
06386bbf
JB
673#define MIN_READ_RECORDS 20
674struct dm_raid1_read_record {
675 struct mirror *m;
676 struct dm_bio_details details;
677};
678
1da177e4
LT
679/*
680 * Every mirror should look like this one.
681 */
682#define DEFAULT_MIRROR 0
683
684/*
06386bbf
JB
685 * This is yucky. We squirrel the mirror struct away inside
686 * bi_next for read/write buffers. This is safe since the bh
1da177e4
LT
687 * doesn't get submitted to the lower levels of block layer.
688 */
06386bbf 689static struct mirror *bio_get_m(struct bio *bio)
1da177e4 690{
06386bbf 691 return (struct mirror *) bio->bi_next;
1da177e4
LT
692}
693
06386bbf 694static void bio_set_m(struct bio *bio, struct mirror *m)
1da177e4 695{
06386bbf 696 bio->bi_next = (struct bio *) m;
1da177e4
LT
697}
698
72f4b314
JB
699static struct mirror *get_default_mirror(struct mirror_set *ms)
700{
701 return &ms->mirror[atomic_read(&ms->default_mirror)];
702}
703
704static void set_default_mirror(struct mirror *m)
705{
706 struct mirror_set *ms = m->ms;
707 struct mirror *m0 = &(ms->mirror[0]);
708
709 atomic_set(&ms->default_mirror, m - m0);
710}
711
712/* fail_mirror
713 * @m: mirror device to fail
714 * @error_type: one of the enum's, DM_RAID1_*_ERROR
715 *
716 * If errors are being handled, record the type of
717 * error encountered for this device. If this type
718 * of error has already been recorded, we can return;
719 * otherwise, we must signal userspace by triggering
720 * an event. Additionally, if the device is the
721 * primary device, we must choose a new primary, but
722 * only if the mirror is in-sync.
723 *
724 * This function must not block.
725 */
726static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
727{
728 struct mirror_set *ms = m->ms;
729 struct mirror *new;
730
731 if (!errors_handled(ms))
732 return;
733
734 /*
735 * error_count is used for nothing more than a
736 * simple way to tell if a device has encountered
737 * errors.
738 */
739 atomic_inc(&m->error_count);
740
741 if (test_and_set_bit(error_type, &m->error_type))
742 return;
743
744 if (m != get_default_mirror(ms))
745 goto out;
746
747 if (!ms->in_sync) {
748 /*
749 * Better to issue requests to same failing device
750 * than to risk returning corrupt data.
751 */
752 DMERR("Primary mirror (%s) failed while out-of-sync: "
753 "Reads may fail.", m->dev->name);
754 goto out;
755 }
756
757 for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
758 if (!atomic_read(&new->error_count)) {
759 set_default_mirror(new);
760 break;
761 }
762
763 if (unlikely(new == ms->mirror + ms->nr_mirrors))
764 DMWARN("All sides of mirror have failed.");
765
766out:
767 schedule_work(&ms->trigger_event);
768}
769
1da177e4
LT
770/*-----------------------------------------------------------------
771 * Recovery.
772 *
773 * When a mirror is first activated we may find that some regions
774 * are in the no-sync state. We have to recover these by
775 * recopying from the default mirror to all the others.
776 *---------------------------------------------------------------*/
4cdc1d1f 777static void recovery_complete(int read_err, unsigned long write_err,
1da177e4
LT
778 void *context)
779{
8f0205b7
JB
780 struct region *reg = (struct region *)context;
781 struct mirror_set *ms = reg->rh->ms;
782 int m, bit = 0;
1da177e4 783
8f0205b7 784 if (read_err) {
f44db678
JB
785 /* Read error means the failure of default mirror. */
786 DMERR_LIMIT("Unable to read primary mirror during recovery");
8f0205b7
JB
787 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
788 }
f44db678 789
8f0205b7 790 if (write_err) {
4cdc1d1f 791 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
f44db678 792 write_err);
8f0205b7
JB
793 /*
794 * Bits correspond to devices (excluding default mirror).
795 * The default mirror cannot change during recovery.
796 */
797 for (m = 0; m < ms->nr_mirrors; m++) {
798 if (&ms->mirror[m] == get_default_mirror(ms))
799 continue;
800 if (test_bit(bit, &write_err))
801 fail_mirror(ms->mirror + m,
802 DM_RAID1_SYNC_ERROR);
803 bit++;
804 }
805 }
f44db678 806
ce503f59 807 rh_recovery_end(reg, !(read_err || write_err));
1da177e4
LT
808}
809
810static int recover(struct mirror_set *ms, struct region *reg)
811{
812 int r;
813 unsigned int i;
eb69aca5 814 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
1da177e4
LT
815 struct mirror *m;
816 unsigned long flags = 0;
817
818 /* fill in the source */
72f4b314 819 m = get_default_mirror(ms);
1da177e4
LT
820 from.bdev = m->dev->bdev;
821 from.sector = m->offset + region_to_sector(reg->rh, reg->key);
822 if (reg->key == (ms->nr_regions - 1)) {
823 /*
824 * The final region may be smaller than
825 * region_size.
826 */
827 from.count = ms->ti->len & (reg->rh->region_size - 1);
828 if (!from.count)
829 from.count = reg->rh->region_size;
830 } else
831 from.count = reg->rh->region_size;
832
833 /* fill in the destinations */
834 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
72f4b314 835 if (&ms->mirror[i] == get_default_mirror(ms))
1da177e4
LT
836 continue;
837
838 m = ms->mirror + i;
839 dest->bdev = m->dev->bdev;
840 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
841 dest->count = from.count;
842 dest++;
843 }
844
845 /* hand to kcopyd */
f7c83e2e
JB
846 if (!errors_handled(ms))
847 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
848
eb69aca5
HM
849 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
850 flags, recovery_complete, reg);
1da177e4
LT
851
852 return r;
853}
854
855static void do_recovery(struct mirror_set *ms)
856{
857 int r;
858 struct region *reg;
416cd17b 859 struct dm_dirty_log *log = ms->rh.log;
1da177e4
LT
860
861 /*
862 * Start quiescing some regions.
863 */
864 rh_recovery_prepare(&ms->rh);
865
866 /*
867 * Copy any already quiesced regions.
868 */
869 while ((reg = rh_recovery_start(&ms->rh))) {
870 r = recover(ms, reg);
871 if (r)
872 rh_recovery_end(reg, 0);
873 }
874
875 /*
876 * Update the in sync flag.
877 */
878 if (!ms->in_sync &&
879 (log->type->get_sync_count(log) == ms->nr_regions)) {
880 /* the sync is complete */
881 dm_table_event(ms->ti->table);
882 ms->in_sync = 1;
883 }
884}
885
886/*-----------------------------------------------------------------
887 * Reads
888 *---------------------------------------------------------------*/
889static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
890{
06386bbf
JB
891 struct mirror *m = get_default_mirror(ms);
892
893 do {
894 if (likely(!atomic_read(&m->error_count)))
895 return m;
896
897 if (m-- == ms->mirror)
898 m += ms->nr_mirrors;
899 } while (m != get_default_mirror(ms));
900
901 return NULL;
902}
903
904static int default_ok(struct mirror *m)
905{
906 struct mirror *default_mirror = get_default_mirror(m->ms);
907
908 return !atomic_read(&default_mirror->error_count);
909}
910
911static int mirror_available(struct mirror_set *ms, struct bio *bio)
912{
913 region_t region = bio_to_region(&ms->rh, bio);
914
915 if (ms->rh.log->type->in_sync(ms->rh.log, region, 0))
916 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
917
918 return 0;
1da177e4
LT
919}
920
921/*
922 * remap a buffer to a particular mirror.
923 */
06386bbf
JB
924static sector_t map_sector(struct mirror *m, struct bio *bio)
925{
926 return m->offset + (bio->bi_sector - m->ms->ti->begin);
927}
928
929static void map_bio(struct mirror *m, struct bio *bio)
1da177e4
LT
930{
931 bio->bi_bdev = m->dev->bdev;
06386bbf
JB
932 bio->bi_sector = map_sector(m, bio);
933}
934
22a1ceb1 935static void map_region(struct dm_io_region *io, struct mirror *m,
06386bbf
JB
936 struct bio *bio)
937{
938 io->bdev = m->dev->bdev;
939 io->sector = map_sector(m, bio);
940 io->count = bio->bi_size >> 9;
941}
942
943/*-----------------------------------------------------------------
944 * Reads
945 *---------------------------------------------------------------*/
946static void read_callback(unsigned long error, void *context)
947{
948 struct bio *bio = context;
949 struct mirror *m;
950
951 m = bio_get_m(bio);
952 bio_set_m(bio, NULL);
953
954 if (likely(!error)) {
955 bio_endio(bio, 0);
956 return;
957 }
958
959 fail_mirror(m, DM_RAID1_READ_ERROR);
960
961 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
962 DMWARN_LIMIT("Read failure on mirror device %s. "
963 "Trying alternative device.",
964 m->dev->name);
965 queue_bio(m->ms, bio, bio_rw(bio));
966 return;
967 }
968
969 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
970 m->dev->name);
971 bio_endio(bio, -EIO);
972}
973
974/* Asynchronous read. */
975static void read_async_bio(struct mirror *m, struct bio *bio)
976{
22a1ceb1 977 struct dm_io_region io;
06386bbf
JB
978 struct dm_io_request io_req = {
979 .bi_rw = READ,
980 .mem.type = DM_IO_BVEC,
981 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
982 .notify.fn = read_callback,
983 .notify.context = bio,
984 .client = m->ms->io_client,
985 };
986
987 map_region(&io, m, bio);
988 bio_set_m(bio, m);
989 (void) dm_io(&io_req, 1, &io, NULL);
1da177e4
LT
990}
991
992static void do_reads(struct mirror_set *ms, struct bio_list *reads)
993{
994 region_t region;
995 struct bio *bio;
996 struct mirror *m;
997
998 while ((bio = bio_list_pop(reads))) {
999 region = bio_to_region(&ms->rh, bio);
06386bbf 1000 m = get_default_mirror(ms);
1da177e4
LT
1001
1002 /*
1003 * We can only read balance if the region is in sync.
1004 */
06386bbf 1005 if (likely(rh_in_sync(&ms->rh, region, 1)))
1da177e4 1006 m = choose_mirror(ms, bio->bi_sector);
06386bbf
JB
1007 else if (m && atomic_read(&m->error_count))
1008 m = NULL;
1da177e4 1009
06386bbf
JB
1010 if (likely(m))
1011 read_async_bio(m, bio);
1012 else
1013 bio_endio(bio, -EIO);
1da177e4
LT
1014 }
1015}
1016
1017/*-----------------------------------------------------------------
1018 * Writes.
1019 *
1020 * We do different things with the write io depending on the
1021 * state of the region that it's in:
1022 *
1023 * SYNC: increment pending, use kcopyd to write to *all* mirrors
1024 * RECOVERING: delay the io until recovery completes
1025 * NOSYNC: increment pending, just write to the default mirror
1026 *---------------------------------------------------------------*/
72f4b314
JB
1027
1028/* __bio_mark_nosync
1029 * @ms
1030 * @bio
1031 * @done
1032 * @error
1033 *
1034 * The bio was written on some mirror(s) but failed on other mirror(s).
1035 * We can successfully endio the bio but should avoid the region being
1036 * marked clean by setting the state RH_NOSYNC.
1037 *
1038 * This function is _not_ safe in interrupt context!
1039 */
1040static void __bio_mark_nosync(struct mirror_set *ms,
1041 struct bio *bio, unsigned done, int error)
1042{
1043 unsigned long flags;
1044 struct region_hash *rh = &ms->rh;
416cd17b 1045 struct dm_dirty_log *log = ms->rh.log;
72f4b314
JB
1046 struct region *reg;
1047 region_t region = bio_to_region(rh, bio);
1048 int recovering = 0;
1049
1050 /* We must inform the log that the sync count has changed. */
1051 log->type->set_region_sync(log, region, 0);
1052 ms->in_sync = 0;
1053
1054 read_lock(&rh->hash_lock);
1055 reg = __rh_find(rh, region);
1056 read_unlock(&rh->hash_lock);
1057
1058 /* region hash entry should exist because write was in-flight */
1059 BUG_ON(!reg);
1060 BUG_ON(!list_empty(&reg->list));
1061
1062 spin_lock_irqsave(&rh->region_lock, flags);
1063 /*
1064 * Possible cases:
1065 * 1) RH_DIRTY
1066 * 2) RH_NOSYNC: was dirty, other preceeding writes failed
1067 * 3) RH_RECOVERING: flushing pending writes
1068 * Either case, the region should have not been connected to list.
1069 */
1070 recovering = (reg->state == RH_RECOVERING);
1071 reg->state = RH_NOSYNC;
1072 BUG_ON(!list_empty(&reg->list));
1073 spin_unlock_irqrestore(&rh->region_lock, flags);
1074
1075 bio_endio(bio, error);
1076 if (recovering)
1077 complete_resync_work(reg, 0);
1078}
1079
1da177e4
LT
1080static void write_callback(unsigned long error, void *context)
1081{
72f4b314 1082 unsigned i, ret = 0;
1da177e4
LT
1083 struct bio *bio = (struct bio *) context;
1084 struct mirror_set *ms;
72f4b314
JB
1085 int uptodate = 0;
1086 int should_wake = 0;
1087 unsigned long flags;
1da177e4 1088
06386bbf
JB
1089 ms = bio_get_m(bio)->ms;
1090 bio_set_m(bio, NULL);
1da177e4
LT
1091
1092 /*
1093 * NOTE: We don't decrement the pending count here,
1094 * instead it is done by the targets endio function.
1095 * This way we handle both writes to SYNC and NOSYNC
1096 * regions with the same code.
1097 */
72f4b314
JB
1098 if (likely(!error))
1099 goto out;
1da177e4 1100
72f4b314
JB
1101 for (i = 0; i < ms->nr_mirrors; i++)
1102 if (test_bit(i, &error))
1103 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
1104 else
1105 uptodate = 1;
1106
1107 if (unlikely(!uptodate)) {
1108 DMERR("All replicated volumes dead, failing I/O");
1109 /* None of the writes succeeded, fail the I/O. */
1110 ret = -EIO;
1111 } else if (errors_handled(ms)) {
1da177e4 1112 /*
72f4b314
JB
1113 * Need to raise event. Since raising
1114 * events can block, we need to do it in
1115 * the main thread.
1da177e4 1116 */
72f4b314
JB
1117 spin_lock_irqsave(&ms->lock, flags);
1118 if (!ms->failures.head)
1119 should_wake = 1;
1120 bio_list_add(&ms->failures, bio);
1121 spin_unlock_irqrestore(&ms->lock, flags);
1122 if (should_wake)
1123 wake(ms);
1124 return;
1da177e4 1125 }
72f4b314
JB
1126out:
1127 bio_endio(bio, ret);
1da177e4
LT
1128}
1129
1130static void do_write(struct mirror_set *ms, struct bio *bio)
1131{
1132 unsigned int i;
22a1ceb1 1133 struct dm_io_region io[ms->nr_mirrors], *dest = io;
1da177e4 1134 struct mirror *m;
88be163a
MB
1135 struct dm_io_request io_req = {
1136 .bi_rw = WRITE,
1137 .mem.type = DM_IO_BVEC,
1138 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
1139 .notify.fn = write_callback,
1140 .notify.context = bio,
1141 .client = ms->io_client,
1142 };
1da177e4 1143
06386bbf
JB
1144 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
1145 map_region(dest++, m, bio);
1da177e4 1146
06386bbf
JB
1147 /*
1148 * Use default mirror because we only need it to retrieve the reference
1149 * to the mirror set in write_callback().
1150 */
1151 bio_set_m(bio, get_default_mirror(ms));
88be163a
MB
1152
1153 (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
1da177e4
LT
1154}
1155
1156static void do_writes(struct mirror_set *ms, struct bio_list *writes)
1157{
1158 int state;
1159 struct bio *bio;
1160 struct bio_list sync, nosync, recover, *this_list = NULL;
1161
1162 if (!writes->head)
1163 return;
1164
1165 /*
1166 * Classify each write.
1167 */
1168 bio_list_init(&sync);
1169 bio_list_init(&nosync);
1170 bio_list_init(&recover);
1171
1172 while ((bio = bio_list_pop(writes))) {
1173 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
1174 switch (state) {
1175 case RH_CLEAN:
1176 case RH_DIRTY:
1177 this_list = &sync;
1178 break;
1179
1180 case RH_NOSYNC:
1181 this_list = &nosync;
1182 break;
1183
1184 case RH_RECOVERING:
1185 this_list = &recover;
1186 break;
1187 }
1188
1189 bio_list_add(this_list, bio);
1190 }
1191
1192 /*
1193 * Increment the pending counts for any regions that will
1194 * be written to (writes to recover regions are going to
1195 * be delayed).
1196 */
1197 rh_inc_pending(&ms->rh, &sync);
1198 rh_inc_pending(&ms->rh, &nosync);
fc1ff958 1199 ms->log_failure = rh_flush(&ms->rh) ? 1 : 0;
1da177e4
LT
1200
1201 /*
1202 * Dispatch io.
1203 */
b80aa7a0
JB
1204 if (unlikely(ms->log_failure)) {
1205 spin_lock_irq(&ms->lock);
1206 bio_list_merge(&ms->failures, &sync);
1207 spin_unlock_irq(&ms->lock);
a2aebe03 1208 wake(ms);
b80aa7a0 1209 } else
fc1ff958 1210 while ((bio = bio_list_pop(&sync)))
b80aa7a0 1211 do_write(ms, bio);
1da177e4
LT
1212
1213 while ((bio = bio_list_pop(&recover)))
1214 rh_delay(&ms->rh, bio);
1215
1216 while ((bio = bio_list_pop(&nosync))) {
06386bbf 1217 map_bio(get_default_mirror(ms), bio);
1da177e4
LT
1218 generic_make_request(bio);
1219 }
1220}
1221
72f4b314
JB
1222static void do_failures(struct mirror_set *ms, struct bio_list *failures)
1223{
1224 struct bio *bio;
1225
1226 if (!failures->head)
1227 return;
1228
b80aa7a0
JB
1229 if (!ms->log_failure) {
1230 while ((bio = bio_list_pop(failures)))
1231 __bio_mark_nosync(ms, bio, bio->bi_size, 0);
1232 return;
1233 }
1234
1235 /*
1236 * If the log has failed, unattempted writes are being
1237 * put on the failures list. We can't issue those writes
1238 * until a log has been marked, so we must store them.
1239 *
1240 * If a 'noflush' suspend is in progress, we can requeue
1241 * the I/O's to the core. This give userspace a chance
1242 * to reconfigure the mirror, at which point the core
1243 * will reissue the writes. If the 'noflush' flag is
1244 * not set, we have no choice but to return errors.
1245 *
1246 * Some writes on the failures list may have been
1247 * submitted before the log failure and represent a
1248 * failure to write to one of the devices. It is ok
1249 * for us to treat them the same and requeue them
1250 * as well.
1251 */
1252 if (dm_noflush_suspending(ms->ti)) {
1253 while ((bio = bio_list_pop(failures)))
1254 bio_endio(bio, DM_ENDIO_REQUEUE);
1255 return;
1256 }
1257
1258 if (atomic_read(&ms->suspend)) {
1259 while ((bio = bio_list_pop(failures)))
1260 bio_endio(bio, -EIO);
1261 return;
1262 }
1263
1264 spin_lock_irq(&ms->lock);
1265 bio_list_merge(&ms->failures, failures);
1266 spin_unlock_irq(&ms->lock);
1267
a2aebe03 1268 delayed_wake(ms);
72f4b314
JB
1269}
1270
1271static void trigger_event(struct work_struct *work)
1272{
1273 struct mirror_set *ms =
1274 container_of(work, struct mirror_set, trigger_event);
1275
1276 dm_table_event(ms->ti->table);
1277}
1278
1da177e4
LT
1279/*-----------------------------------------------------------------
1280 * kmirrord
1281 *---------------------------------------------------------------*/
a2aebe03 1282static void do_mirror(struct work_struct *work)
1da177e4 1283{
6ad36fe2
HS
1284 struct mirror_set *ms =container_of(work, struct mirror_set,
1285 kmirrord_work);
72f4b314
JB
1286 struct bio_list reads, writes, failures;
1287 unsigned long flags;
1da177e4 1288
72f4b314 1289 spin_lock_irqsave(&ms->lock, flags);
1da177e4
LT
1290 reads = ms->reads;
1291 writes = ms->writes;
72f4b314 1292 failures = ms->failures;
1da177e4
LT
1293 bio_list_init(&ms->reads);
1294 bio_list_init(&ms->writes);
72f4b314
JB
1295 bio_list_init(&ms->failures);
1296 spin_unlock_irqrestore(&ms->lock, flags);
1da177e4
LT
1297
1298 rh_update_states(&ms->rh);
1299 do_recovery(ms);
1300 do_reads(ms, &reads);
1301 do_writes(ms, &writes);
72f4b314 1302 do_failures(ms, &failures);
7ff14a36
MP
1303
1304 dm_table_unplug_all(ms->ti->table);
1da177e4
LT
1305}
1306
72f4b314 1307
1da177e4
LT
1308/*-----------------------------------------------------------------
1309 * Target functions
1310 *---------------------------------------------------------------*/
1311static struct mirror_set *alloc_context(unsigned int nr_mirrors,
1312 uint32_t region_size,
1313 struct dm_target *ti,
416cd17b 1314 struct dm_dirty_log *dl)
1da177e4
LT
1315{
1316 size_t len;
1317 struct mirror_set *ms = NULL;
1318
1da177e4
LT
1319 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
1320
dd00cc48 1321 ms = kzalloc(len, GFP_KERNEL);
1da177e4 1322 if (!ms) {
72d94861 1323 ti->error = "Cannot allocate mirror context";
1da177e4
LT
1324 return NULL;
1325 }
1326
1da177e4
LT
1327 spin_lock_init(&ms->lock);
1328
1329 ms->ti = ti;
1330 ms->nr_mirrors = nr_mirrors;
1331 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
1332 ms->in_sync = 0;
b80aa7a0
JB
1333 ms->log_failure = 0;
1334 atomic_set(&ms->suspend, 0);
72f4b314 1335 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
1da177e4 1336
06386bbf
JB
1337 len = sizeof(struct dm_raid1_read_record);
1338 ms->read_record_pool = mempool_create_kmalloc_pool(MIN_READ_RECORDS,
1339 len);
1340 if (!ms->read_record_pool) {
1341 ti->error = "Error creating mirror read_record_pool";
1342 kfree(ms);
1343 return NULL;
1344 }
1345
88be163a
MB
1346 ms->io_client = dm_io_client_create(DM_IO_PAGES);
1347 if (IS_ERR(ms->io_client)) {
1348 ti->error = "Error creating dm_io client";
06386bbf 1349 mempool_destroy(ms->read_record_pool);
88be163a
MB
1350 kfree(ms);
1351 return NULL;
1352 }
1353
1da177e4 1354 if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
72d94861 1355 ti->error = "Error creating dirty region hash";
a72cf737 1356 dm_io_client_destroy(ms->io_client);
06386bbf 1357 mempool_destroy(ms->read_record_pool);
1da177e4
LT
1358 kfree(ms);
1359 return NULL;
1360 }
1361
1362 return ms;
1363}
1364
1365static void free_context(struct mirror_set *ms, struct dm_target *ti,
1366 unsigned int m)
1367{
1368 while (m--)
1369 dm_put_device(ti, ms->mirror[m].dev);
1370
88be163a 1371 dm_io_client_destroy(ms->io_client);
1da177e4 1372 rh_exit(&ms->rh);
06386bbf 1373 mempool_destroy(ms->read_record_pool);
1da177e4
LT
1374 kfree(ms);
1375}
1376
1377static inline int _check_region_size(struct dm_target *ti, uint32_t size)
1378{
6f3c3f0a 1379 return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
1da177e4
LT
1380 size > ti->len);
1381}
1382
1383static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
1384 unsigned int mirror, char **argv)
1385{
4ee218cd 1386 unsigned long long offset;
1da177e4 1387
4ee218cd 1388 if (sscanf(argv[1], "%llu", &offset) != 1) {
72d94861 1389 ti->error = "Invalid offset";
1da177e4
LT
1390 return -EINVAL;
1391 }
1392
1393 if (dm_get_device(ti, argv[0], offset, ti->len,
1394 dm_table_get_mode(ti->table),
1395 &ms->mirror[mirror].dev)) {
72d94861 1396 ti->error = "Device lookup failure";
1da177e4
LT
1397 return -ENXIO;
1398 }
1399
aa5617c5 1400 ms->mirror[mirror].ms = ms;
72f4b314
JB
1401 atomic_set(&(ms->mirror[mirror].error_count), 0);
1402 ms->mirror[mirror].error_type = 0;
1da177e4
LT
1403 ms->mirror[mirror].offset = offset;
1404
1405 return 0;
1406}
1407
1da177e4
LT
1408/*
1409 * Create dirty log: log_type #log_params <log_params>
1410 */
416cd17b 1411static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
1da177e4
LT
1412 unsigned int argc, char **argv,
1413 unsigned int *args_used)
1414{
1415 unsigned int param_count;
416cd17b 1416 struct dm_dirty_log *dl;
1da177e4
LT
1417
1418 if (argc < 2) {
72d94861 1419 ti->error = "Insufficient mirror log arguments";
1da177e4
LT
1420 return NULL;
1421 }
1422
1423 if (sscanf(argv[1], "%u", &param_count) != 1) {
72d94861 1424 ti->error = "Invalid mirror log argument count";
1da177e4
LT
1425 return NULL;
1426 }
1427
1428 *args_used = 2 + param_count;
1429
1430 if (argc < *args_used) {
72d94861 1431 ti->error = "Insufficient mirror log arguments";
1da177e4
LT
1432 return NULL;
1433 }
1434
416cd17b 1435 dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
1da177e4 1436 if (!dl) {
72d94861 1437 ti->error = "Error creating mirror dirty log";
1da177e4
LT
1438 return NULL;
1439 }
1440
1441 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
72d94861 1442 ti->error = "Invalid region size";
416cd17b 1443 dm_dirty_log_destroy(dl);
1da177e4
LT
1444 return NULL;
1445 }
1446
1447 return dl;
1448}
1449
a8e6afa2
JB
1450static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1451 unsigned *args_used)
1452{
1453 unsigned num_features;
1454 struct dm_target *ti = ms->ti;
1455
1456 *args_used = 0;
1457
1458 if (!argc)
1459 return 0;
1460
1461 if (sscanf(argv[0], "%u", &num_features) != 1) {
1462 ti->error = "Invalid number of features";
1463 return -EINVAL;
1464 }
1465
1466 argc--;
1467 argv++;
1468 (*args_used)++;
1469
1470 if (num_features > argc) {
1471 ti->error = "Not enough arguments to support feature count";
1472 return -EINVAL;
1473 }
1474
1475 if (!strcmp("handle_errors", argv[0]))
1476 ms->features |= DM_RAID1_HANDLE_ERRORS;
1477 else {
1478 ti->error = "Unrecognised feature requested";
1479 return -EINVAL;
1480 }
1481
1482 (*args_used)++;
1483
1484 return 0;
1485}
1486
1da177e4
LT
1487/*
1488 * Construct a mirror mapping:
1489 *
1490 * log_type #log_params <log_params>
1491 * #mirrors [mirror_path offset]{2,}
a8e6afa2 1492 * [#features <features>]
1da177e4
LT
1493 *
1494 * log_type is "core" or "disk"
1495 * #log_params is between 1 and 3
a8e6afa2
JB
1496 *
1497 * If present, features must be "handle_errors".
1da177e4 1498 */
1da177e4
LT
1499static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1500{
1501 int r;
1502 unsigned int nr_mirrors, m, args_used;
1503 struct mirror_set *ms;
416cd17b 1504 struct dm_dirty_log *dl;
1da177e4
LT
1505
1506 dl = create_dirty_log(ti, argc, argv, &args_used);
1507 if (!dl)
1508 return -EINVAL;
1509
1510 argv += args_used;
1511 argc -= args_used;
1512
1513 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
eb69aca5 1514 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
72d94861 1515 ti->error = "Invalid number of mirrors";
416cd17b 1516 dm_dirty_log_destroy(dl);
1da177e4
LT
1517 return -EINVAL;
1518 }
1519
1520 argv++, argc--;
1521
a8e6afa2
JB
1522 if (argc < nr_mirrors * 2) {
1523 ti->error = "Too few mirror arguments";
416cd17b 1524 dm_dirty_log_destroy(dl);
1da177e4
LT
1525 return -EINVAL;
1526 }
1527
1528 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1529 if (!ms) {
416cd17b 1530 dm_dirty_log_destroy(dl);
1da177e4
LT
1531 return -ENOMEM;
1532 }
1533
1534 /* Get the mirror parameter sets */
1535 for (m = 0; m < nr_mirrors; m++) {
1536 r = get_mirror(ms, ti, m, argv);
1537 if (r) {
1538 free_context(ms, ti, m);
1539 return r;
1540 }
1541 argv += 2;
1542 argc -= 2;
1543 }
1544
1545 ti->private = ms;
d88854f0 1546 ti->split_io = ms->rh.region_size;
1da177e4 1547
6ad36fe2
HS
1548 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1549 if (!ms->kmirrord_wq) {
1550 DMERR("couldn't start kmirrord");
a72cf737
DM
1551 r = -ENOMEM;
1552 goto err_free_context;
6ad36fe2
HS
1553 }
1554 INIT_WORK(&ms->kmirrord_work, do_mirror);
a2aebe03
MP
1555 init_timer(&ms->timer);
1556 ms->timer_pending = 0;
72f4b314 1557 INIT_WORK(&ms->trigger_event, trigger_event);
6ad36fe2 1558
a8e6afa2 1559 r = parse_features(ms, argc, argv, &args_used);
a72cf737
DM
1560 if (r)
1561 goto err_destroy_wq;
a8e6afa2
JB
1562
1563 argv += args_used;
1564 argc -= args_used;
1565
f44db678
JB
1566 /*
1567 * Any read-balancing addition depends on the
1568 * DM_RAID1_HANDLE_ERRORS flag being present.
1569 * This is because the decision to balance depends
1570 * on the sync state of a region. If the above
1571 * flag is not present, we ignore errors; and
1572 * the sync state may be inaccurate.
1573 */
1574
a8e6afa2
JB
1575 if (argc) {
1576 ti->error = "Too many mirror arguments";
a72cf737
DM
1577 r = -EINVAL;
1578 goto err_destroy_wq;
a8e6afa2
JB
1579 }
1580
eb69aca5 1581 r = dm_kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
a72cf737
DM
1582 if (r)
1583 goto err_destroy_wq;
1da177e4 1584
6ad36fe2 1585 wake(ms);
1da177e4 1586 return 0;
a72cf737
DM
1587
1588err_destroy_wq:
1589 destroy_workqueue(ms->kmirrord_wq);
1590err_free_context:
1591 free_context(ms, ti, ms->nr_mirrors);
1592 return r;
1da177e4
LT
1593}
1594
1595static void mirror_dtr(struct dm_target *ti)
1596{
1597 struct mirror_set *ms = (struct mirror_set *) ti->private;
1598
a2aebe03 1599 del_timer_sync(&ms->timer);
6ad36fe2 1600 flush_workqueue(ms->kmirrord_wq);
eb69aca5 1601 dm_kcopyd_client_destroy(ms->kcopyd_client);
6ad36fe2 1602 destroy_workqueue(ms->kmirrord_wq);
1da177e4
LT
1603 free_context(ms, ti, ms->nr_mirrors);
1604}
1605
1606static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1607{
72f4b314 1608 unsigned long flags;
1da177e4
LT
1609 int should_wake = 0;
1610 struct bio_list *bl;
1611
1612 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
72f4b314 1613 spin_lock_irqsave(&ms->lock, flags);
1da177e4
LT
1614 should_wake = !(bl->head);
1615 bio_list_add(bl, bio);
72f4b314 1616 spin_unlock_irqrestore(&ms->lock, flags);
1da177e4
LT
1617
1618 if (should_wake)
6ad36fe2 1619 wake(ms);
1da177e4
LT
1620}
1621
1622/*
1623 * Mirror mapping function
1624 */
1625static int mirror_map(struct dm_target *ti, struct bio *bio,
1626 union map_info *map_context)
1627{
1628 int r, rw = bio_rw(bio);
1629 struct mirror *m;
1630 struct mirror_set *ms = ti->private;
06386bbf 1631 struct dm_raid1_read_record *read_record = NULL;
1da177e4
LT
1632
1633 if (rw == WRITE) {
06386bbf
JB
1634 /* Save region for mirror_end_io() handler */
1635 map_context->ll = bio_to_region(&ms->rh, bio);
1da177e4 1636 queue_bio(ms, bio, rw);
d2a7ad29 1637 return DM_MAPIO_SUBMITTED;
1da177e4
LT
1638 }
1639
1640 r = ms->rh.log->type->in_sync(ms->rh.log,
1641 bio_to_region(&ms->rh, bio), 0);
1642 if (r < 0 && r != -EWOULDBLOCK)
1643 return r;
1644
1da177e4 1645 /*
06386bbf 1646 * If region is not in-sync queue the bio.
1da177e4 1647 */
06386bbf
JB
1648 if (!r || (r == -EWOULDBLOCK)) {
1649 if (rw == READA)
1650 return -EWOULDBLOCK;
1da177e4 1651
1da177e4 1652 queue_bio(ms, bio, rw);
d2a7ad29 1653 return DM_MAPIO_SUBMITTED;
1da177e4
LT
1654 }
1655
06386bbf
JB
1656 /*
1657 * The region is in-sync and we can perform reads directly.
1658 * Store enough information so we can retry if it fails.
1659 */
1da177e4 1660 m = choose_mirror(ms, bio->bi_sector);
06386bbf 1661 if (unlikely(!m))
1da177e4
LT
1662 return -EIO;
1663
06386bbf
JB
1664 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1665 if (likely(read_record)) {
1666 dm_bio_record(&read_record->details, bio);
1667 map_context->ptr = read_record;
1668 read_record->m = m;
1669 }
1670
1671 map_bio(m, bio);
1672
d2a7ad29 1673 return DM_MAPIO_REMAPPED;
1da177e4
LT
1674}
1675
1676static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1677 int error, union map_info *map_context)
1678{
1679 int rw = bio_rw(bio);
1680 struct mirror_set *ms = (struct mirror_set *) ti->private;
06386bbf
JB
1681 struct mirror *m = NULL;
1682 struct dm_bio_details *bd = NULL;
1683 struct dm_raid1_read_record *read_record = map_context->ptr;
1da177e4
LT
1684
1685 /*
1686 * We need to dec pending if this was a write.
1687 */
06386bbf
JB
1688 if (rw == WRITE) {
1689 rh_dec(&ms->rh, map_context->ll);
1690 return error;
1691 }
1da177e4 1692
06386bbf
JB
1693 if (error == -EOPNOTSUPP)
1694 goto out;
1695
1696 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1697 goto out;
1698
1699 if (unlikely(error)) {
1700 if (!read_record) {
1701 /*
1702 * There wasn't enough memory to record necessary
1703 * information for a retry or there was no other
1704 * mirror in-sync.
1705 */
e03f1a84 1706 DMERR_LIMIT("Mirror read failed.");
06386bbf
JB
1707 return -EIO;
1708 }
e03f1a84
AB
1709
1710 m = read_record->m;
1711
06386bbf
JB
1712 DMERR("Mirror read failed from %s. Trying alternative device.",
1713 m->dev->name);
1714
06386bbf
JB
1715 fail_mirror(m, DM_RAID1_READ_ERROR);
1716
1717 /*
1718 * A failed read is requeued for another attempt using an intact
1719 * mirror.
1720 */
1721 if (default_ok(m) || mirror_available(ms, bio)) {
1722 bd = &read_record->details;
1723
1724 dm_bio_restore(bd, bio);
1725 mempool_free(read_record, ms->read_record_pool);
1726 map_context->ptr = NULL;
1727 queue_bio(ms, bio, rw);
1728 return 1;
1729 }
1730 DMERR("All replicated volumes dead, failing I/O");
1731 }
1732
1733out:
1734 if (read_record) {
1735 mempool_free(read_record, ms->read_record_pool);
1736 map_context->ptr = NULL;
1737 }
1738
1739 return error;
1da177e4
LT
1740}
1741
b80aa7a0 1742static void mirror_presuspend(struct dm_target *ti)
1da177e4
LT
1743{
1744 struct mirror_set *ms = (struct mirror_set *) ti->private;
416cd17b 1745 struct dm_dirty_log *log = ms->rh.log;
1da177e4 1746
b80aa7a0
JB
1747 atomic_set(&ms->suspend, 1);
1748
1749 /*
1750 * We must finish up all the work that we've
1751 * generated (i.e. recovery work).
1752 */
1da177e4 1753 rh_stop_recovery(&ms->rh);
33184048 1754
33184048
JB
1755 wait_event(_kmirrord_recovery_stopped,
1756 !atomic_read(&ms->rh.recovery_in_flight));
1757
b80aa7a0
JB
1758 if (log->type->presuspend && log->type->presuspend(log))
1759 /* FIXME: need better error handling */
1760 DMWARN("log presuspend failed");
1761
1762 /*
1763 * Now that recovery is complete/stopped and the
1764 * delayed bios are queued, we need to wait for
1765 * the worker thread to complete. This way,
1766 * we know that all of our I/O has been pushed.
1767 */
1768 flush_workqueue(ms->kmirrord_wq);
1769}
1770
1771static void mirror_postsuspend(struct dm_target *ti)
1772{
1773 struct mirror_set *ms = ti->private;
416cd17b 1774 struct dm_dirty_log *log = ms->rh.log;
b80aa7a0 1775
6b3df0d7 1776 if (log->type->postsuspend && log->type->postsuspend(log))
1da177e4 1777 /* FIXME: need better error handling */
b80aa7a0 1778 DMWARN("log postsuspend failed");
1da177e4
LT
1779}
1780
1781static void mirror_resume(struct dm_target *ti)
1782{
b80aa7a0 1783 struct mirror_set *ms = ti->private;
416cd17b 1784 struct dm_dirty_log *log = ms->rh.log;
b80aa7a0
JB
1785
1786 atomic_set(&ms->suspend, 0);
1da177e4
LT
1787 if (log->type->resume && log->type->resume(log))
1788 /* FIXME: need better error handling */
1789 DMWARN("log resume failed");
1790 rh_start_recovery(&ms->rh);
1791}
1792
af195ac8
JB
1793/*
1794 * device_status_char
1795 * @m: mirror device/leg we want the status of
1796 *
1797 * We return one character representing the most severe error
1798 * we have encountered.
1799 * A => Alive - No failures
1800 * D => Dead - A write failure occurred leaving mirror out-of-sync
1801 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1802 * R => Read - A read failure occurred, mirror data unaffected
1803 *
1804 * Returns: <char>
1805 */
1806static char device_status_char(struct mirror *m)
1807{
1808 if (!atomic_read(&(m->error_count)))
1809 return 'A';
1810
1811 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1812 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1813 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1814}
1815
1816
1da177e4
LT
1817static int mirror_status(struct dm_target *ti, status_type_t type,
1818 char *result, unsigned int maxlen)
1819{
315dcc22 1820 unsigned int m, sz = 0;
1da177e4 1821 struct mirror_set *ms = (struct mirror_set *) ti->private;
416cd17b 1822 struct dm_dirty_log *log = ms->rh.log;
af195ac8 1823 char buffer[ms->nr_mirrors + 1];
1da177e4 1824
1da177e4
LT
1825 switch (type) {
1826 case STATUSTYPE_INFO:
1827 DMEMIT("%d ", ms->nr_mirrors);
af195ac8 1828 for (m = 0; m < ms->nr_mirrors; m++) {
1da177e4 1829 DMEMIT("%s ", ms->mirror[m].dev->name);
af195ac8
JB
1830 buffer[m] = device_status_char(&(ms->mirror[m]));
1831 }
1832 buffer[m] = '\0';
1da177e4 1833
af195ac8
JB
1834 DMEMIT("%llu/%llu 1 %s ",
1835 (unsigned long long)log->type->get_sync_count(ms->rh.log),
1836 (unsigned long long)ms->nr_regions, buffer);
315dcc22 1837
af195ac8 1838 sz += log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
315dcc22 1839
1da177e4
LT
1840 break;
1841
1842 case STATUSTYPE_TABLE:
af195ac8 1843 sz = log->type->status(ms->rh.log, type, result, maxlen);
315dcc22 1844
e52b8f6d 1845 DMEMIT("%d", ms->nr_mirrors);
1da177e4 1846 for (m = 0; m < ms->nr_mirrors; m++)
e52b8f6d 1847 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
b80aa7a0 1848 (unsigned long long)ms->mirror[m].offset);
a8e6afa2
JB
1849
1850 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1851 DMEMIT(" 1 handle_errors");
1da177e4
LT
1852 }
1853
1854 return 0;
1855}
1856
1857static struct target_type mirror_target = {
1858 .name = "mirror",
af195ac8 1859 .version = {1, 0, 20},
1da177e4
LT
1860 .module = THIS_MODULE,
1861 .ctr = mirror_ctr,
1862 .dtr = mirror_dtr,
1863 .map = mirror_map,
1864 .end_io = mirror_end_io,
b80aa7a0 1865 .presuspend = mirror_presuspend,
1da177e4
LT
1866 .postsuspend = mirror_postsuspend,
1867 .resume = mirror_resume,
1868 .status = mirror_status,
1869};
1870
1871static int __init dm_mirror_init(void)
1872{
1873 int r;
1874
1da177e4 1875 r = dm_register_target(&mirror_target);
769aef30 1876 if (r < 0)
0cd33124 1877 DMERR("Failed to register mirror target");
1da177e4
LT
1878
1879 return r;
1880}
1881
1882static void __exit dm_mirror_exit(void)
1883{
1884 int r;
1885
1886 r = dm_unregister_target(&mirror_target);
1887 if (r < 0)
0cd33124 1888 DMERR("unregister failed %d", r);
1da177e4
LT
1889}
1890
1891/* Module hooks */
1892module_init(dm_mirror_init);
1893module_exit(dm_mirror_exit);
1894
1895MODULE_DESCRIPTION(DM_NAME " mirror target");
1896MODULE_AUTHOR("Joe Thornber");
1897MODULE_LICENSE("GPL");