blob: 9978b9f07fe99db1e9d53dc2605c1d915f5d2206 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm.h"
8#include "dm-bio-list.h"
9#include "dm-io.h"
10#include "dm-log.h"
11#include "kcopyd.h"
12
13#include <linux/ctype.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/pagemap.h>
18#include <linux/slab.h>
19#include <linux/time.h>
20#include <linux/vmalloc.h>
21#include <linux/workqueue.h>
vignesh babu6f3c3f02007-10-19 22:38:44 +010022#include <linux/log2.h>
Jonathan Brassow72f4b312008-02-08 02:11:29 +000023#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "raid1"
Milan Broz88be1632007-05-09 02:33:04 -070026#define DM_IO_PAGES 64
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070028#define DM_RAID1_HANDLE_ERRORS 0x01
Jonathan Brassowf44db672007-07-12 17:29:04 +010029#define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -070030
Jonathan E Brassow33184042006-11-08 17:44:44 -080031static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*-----------------------------------------------------------------
34 * Region hash
35 *
36 * The mirror splits itself up into discrete regions. Each
37 * region can be in one of three states: clean, dirty,
38 * nosync. There is no need to put clean regions in the hash.
39 *
40 * In addition to being present in the hash table a region _may_
41 * be present on one of three lists.
42 *
43 * clean_regions: Regions on this list have no io pending to
44 * them, they are in sync, we are no longer interested in them,
45 * they are dull. rh_update_states() will remove them from the
46 * hash table.
47 *
48 * quiesced_regions: These regions have been spun down, ready
49 * for recovery. rh_recovery_start() will remove regions from
50 * this list and hand them to kmirrord, which will schedule the
51 * recovery io with kcopyd.
52 *
53 * recovered_regions: Regions that kcopyd has successfully
54 * recovered. rh_update_states() will now schedule any delayed
55 * io, up the recovery_count, and remove the region from the
56 * hash.
57 *
58 * There are 2 locks:
59 * A rw spin lock 'hash_lock' protects just the hash table,
60 * this is never held in write mode from interrupt context,
61 * which I believe means that we only have to disable irqs when
62 * doing a write lock.
63 *
64 * An ordinary spin lock 'region_lock' that protects the three
65 * lists in the region_hash, with the 'state', 'list' and
66 * 'bhs_delayed' fields of the regions. This is used from irq
67 * context, so all other uses will have to suspend local irqs.
68 *---------------------------------------------------------------*/
69struct mirror_set;
70struct region_hash {
71 struct mirror_set *ms;
72 uint32_t region_size;
73 unsigned region_shift;
74
75 /* holds persistent region state */
76 struct dirty_log *log;
77
78 /* hash table */
79 rwlock_t hash_lock;
80 mempool_t *region_pool;
81 unsigned int mask;
82 unsigned int nr_buckets;
83 struct list_head *buckets;
84
85 spinlock_t region_lock;
Jonathan E Brassow33184042006-11-08 17:44:44 -080086 atomic_t recovery_in_flight;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct semaphore recovery_count;
88 struct list_head clean_regions;
89 struct list_head quiesced_regions;
90 struct list_head recovered_regions;
Jonathan Brassowf44db672007-07-12 17:29:04 +010091 struct list_head failed_recovered_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94enum {
95 RH_CLEAN,
96 RH_DIRTY,
97 RH_NOSYNC,
98 RH_RECOVERING
99};
100
101struct region {
102 struct region_hash *rh; /* FIXME: can we get rid of this ? */
103 region_t key;
104 int state;
105
106 struct list_head hash_list;
107 struct list_head list;
108
109 atomic_t pending;
110 struct bio_list delayed_bios;
111};
112
Neil Browne4c8b3b2006-06-26 00:27:26 -0700113
114/*-----------------------------------------------------------------
115 * Mirror set structures.
116 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000117enum dm_raid1_error {
118 DM_RAID1_WRITE_ERROR,
119 DM_RAID1_SYNC_ERROR,
120 DM_RAID1_READ_ERROR
121};
122
Neil Browne4c8b3b2006-06-26 00:27:26 -0700123struct mirror {
Jonathan Brassowaa5617c2007-10-19 22:47:58 +0100124 struct mirror_set *ms;
Neil Browne4c8b3b2006-06-26 00:27:26 -0700125 atomic_t error_count;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000126 uint32_t error_type;
Neil Browne4c8b3b2006-06-26 00:27:26 -0700127 struct dm_dev *dev;
128 sector_t offset;
129};
130
131struct mirror_set {
132 struct dm_target *ti;
133 struct list_head list;
134 struct region_hash rh;
135 struct kcopyd_client *kcopyd_client;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -0700136 uint64_t features;
Neil Browne4c8b3b2006-06-26 00:27:26 -0700137
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000138 spinlock_t lock; /* protects the lists */
Neil Browne4c8b3b2006-06-26 00:27:26 -0700139 struct bio_list reads;
140 struct bio_list writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000141 struct bio_list failures;
Neil Browne4c8b3b2006-06-26 00:27:26 -0700142
Milan Broz88be1632007-05-09 02:33:04 -0700143 struct dm_io_client *io_client;
144
Neil Browne4c8b3b2006-06-26 00:27:26 -0700145 /* recovery */
146 region_t nr_regions;
147 int in_sync;
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100148 int log_failure;
Neil Browne4c8b3b2006-06-26 00:27:26 -0700149
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000150 atomic_t default_mirror; /* Default mirror */
Neil Browne4c8b3b2006-06-26 00:27:26 -0700151
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700152 struct workqueue_struct *kmirrord_wq;
153 struct work_struct kmirrord_work;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000154 struct work_struct trigger_event;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700155
Neil Browne4c8b3b2006-06-26 00:27:26 -0700156 unsigned int nr_mirrors;
157 struct mirror mirror[0];
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * Conversion fns
162 */
163static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
164{
Neil Browne4c8b3b2006-06-26 00:27:26 -0700165 return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
169{
170 return region << rh->region_shift;
171}
172
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700173static void wake(struct mirror_set *ms)
174{
175 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* FIXME move this */
179static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define MIN_REGIONS 64
182#define MAX_RECOVERY 1
183static int rh_init(struct region_hash *rh, struct mirror_set *ms,
184 struct dirty_log *log, uint32_t region_size,
185 region_t nr_regions)
186{
187 unsigned int nr_buckets, max_buckets;
188 size_t i;
189
190 /*
191 * Calculate a suitable number of buckets for our hash
192 * table.
193 */
194 max_buckets = nr_regions >> 6;
195 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
196 ;
197 nr_buckets >>= 1;
198
199 rh->ms = ms;
200 rh->log = log;
201 rh->region_size = region_size;
202 rh->region_shift = ffs(region_size) - 1;
203 rwlock_init(&rh->hash_lock);
204 rh->mask = nr_buckets - 1;
205 rh->nr_buckets = nr_buckets;
206
207 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
208 if (!rh->buckets) {
209 DMERR("unable to allocate region hash memory");
210 return -ENOMEM;
211 }
212
213 for (i = 0; i < nr_buckets; i++)
214 INIT_LIST_HEAD(rh->buckets + i);
215
216 spin_lock_init(&rh->region_lock);
217 sema_init(&rh->recovery_count, 0);
Jonathan E Brassow33184042006-11-08 17:44:44 -0800218 atomic_set(&rh->recovery_in_flight, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 INIT_LIST_HEAD(&rh->clean_regions);
220 INIT_LIST_HEAD(&rh->quiesced_regions);
221 INIT_LIST_HEAD(&rh->recovered_regions);
Jonathan Brassowf44db672007-07-12 17:29:04 +0100222 INIT_LIST_HEAD(&rh->failed_recovered_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800224 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
225 sizeof(struct region));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!rh->region_pool) {
227 vfree(rh->buckets);
228 rh->buckets = NULL;
229 return -ENOMEM;
230 }
231
232 return 0;
233}
234
235static void rh_exit(struct region_hash *rh)
236{
237 unsigned int h;
238 struct region *reg, *nreg;
239
240 BUG_ON(!list_empty(&rh->quiesced_regions));
241 for (h = 0; h < rh->nr_buckets; h++) {
242 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
243 BUG_ON(atomic_read(&reg->pending));
244 mempool_free(reg, rh->region_pool);
245 }
246 }
247
248 if (rh->log)
249 dm_destroy_dirty_log(rh->log);
250 if (rh->region_pool)
251 mempool_destroy(rh->region_pool);
252 vfree(rh->buckets);
253}
254
255#define RH_HASH_MULT 2654435387U
256
257static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
258{
259 return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
260}
261
262static struct region *__rh_lookup(struct region_hash *rh, region_t region)
263{
264 struct region *reg;
265
266 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
267 if (reg->key == region)
268 return reg;
269
270 return NULL;
271}
272
273static void __rh_insert(struct region_hash *rh, struct region *reg)
274{
275 unsigned int h = rh_hash(rh, reg->key);
276 list_add(&reg->hash_list, rh->buckets + h);
277}
278
279static struct region *__rh_alloc(struct region_hash *rh, region_t region)
280{
281 struct region *reg, *nreg;
282
283 read_unlock(&rh->hash_lock);
Daniel Kobrasc06aad82006-08-27 01:23:24 -0700284 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
285 if (unlikely(!nreg))
286 nreg = kmalloc(sizeof(struct region), GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
288 RH_CLEAN : RH_NOSYNC;
289 nreg->rh = rh;
290 nreg->key = region;
291
292 INIT_LIST_HEAD(&nreg->list);
293
294 atomic_set(&nreg->pending, 0);
295 bio_list_init(&nreg->delayed_bios);
296 write_lock_irq(&rh->hash_lock);
297
298 reg = __rh_lookup(rh, region);
299 if (reg)
300 /* we lost the race */
301 mempool_free(nreg, rh->region_pool);
302
303 else {
304 __rh_insert(rh, nreg);
305 if (nreg->state == RH_CLEAN) {
306 spin_lock(&rh->region_lock);
307 list_add(&nreg->list, &rh->clean_regions);
308 spin_unlock(&rh->region_lock);
309 }
310 reg = nreg;
311 }
312 write_unlock_irq(&rh->hash_lock);
313 read_lock(&rh->hash_lock);
314
315 return reg;
316}
317
318static inline struct region *__rh_find(struct region_hash *rh, region_t region)
319{
320 struct region *reg;
321
322 reg = __rh_lookup(rh, region);
323 if (!reg)
324 reg = __rh_alloc(rh, region);
325
326 return reg;
327}
328
329static int rh_state(struct region_hash *rh, region_t region, int may_block)
330{
331 int r;
332 struct region *reg;
333
334 read_lock(&rh->hash_lock);
335 reg = __rh_lookup(rh, region);
336 read_unlock(&rh->hash_lock);
337
338 if (reg)
339 return reg->state;
340
341 /*
342 * The region wasn't in the hash, so we fall back to the
343 * dirty log.
344 */
345 r = rh->log->type->in_sync(rh->log, region, may_block);
346
347 /*
348 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
349 * taken as a RH_NOSYNC
350 */
351 return r == 1 ? RH_CLEAN : RH_NOSYNC;
352}
353
354static inline int rh_in_sync(struct region_hash *rh,
355 region_t region, int may_block)
356{
357 int state = rh_state(rh, region, may_block);
358 return state == RH_CLEAN || state == RH_DIRTY;
359}
360
361static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
362{
363 struct bio *bio;
364
365 while ((bio = bio_list_pop(bio_list))) {
366 queue_bio(ms, bio, WRITE);
367 }
368}
369
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800370static void complete_resync_work(struct region *reg, int success)
371{
372 struct region_hash *rh = reg->rh;
373
374 rh->log->type->set_region_sync(rh->log, reg->key, success);
375 dispatch_bios(rh->ms, &reg->delayed_bios);
376 if (atomic_dec_and_test(&rh->recovery_in_flight))
377 wake_up_all(&_kmirrord_recovery_stopped);
378 up(&rh->recovery_count);
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static void rh_update_states(struct region_hash *rh)
382{
383 struct region *reg, *next;
384
385 LIST_HEAD(clean);
386 LIST_HEAD(recovered);
Jonathan Brassowf44db672007-07-12 17:29:04 +0100387 LIST_HEAD(failed_recovered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /*
390 * Quickly grab the lists.
391 */
392 write_lock_irq(&rh->hash_lock);
393 spin_lock(&rh->region_lock);
394 if (!list_empty(&rh->clean_regions)) {
395 list_splice(&rh->clean_regions, &clean);
396 INIT_LIST_HEAD(&rh->clean_regions);
397
Jonathan Brassow943317e2007-07-12 17:28:25 +0100398 list_for_each_entry(reg, &clean, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 list_del(&reg->hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 if (!list_empty(&rh->recovered_regions)) {
403 list_splice(&rh->recovered_regions, &recovered);
404 INIT_LIST_HEAD(&rh->recovered_regions);
405
406 list_for_each_entry (reg, &recovered, list)
407 list_del(&reg->hash_list);
408 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100409
410 if (!list_empty(&rh->failed_recovered_regions)) {
411 list_splice(&rh->failed_recovered_regions, &failed_recovered);
412 INIT_LIST_HEAD(&rh->failed_recovered_regions);
413
414 list_for_each_entry(reg, &failed_recovered, list)
415 list_del(&reg->hash_list);
416 }
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 spin_unlock(&rh->region_lock);
419 write_unlock_irq(&rh->hash_lock);
420
421 /*
422 * All the regions on the recovered and clean lists have
423 * now been pulled out of the system, so no need to do
424 * any more locking.
425 */
426 list_for_each_entry_safe (reg, next, &recovered, list) {
427 rh->log->type->clear_region(rh->log, reg->key);
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800428 complete_resync_work(reg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 mempool_free(reg, rh->region_pool);
430 }
431
Jonathan Brassowf44db672007-07-12 17:29:04 +0100432 list_for_each_entry_safe(reg, next, &failed_recovered, list) {
433 complete_resync_work(reg, errors_handled(rh->ms) ? 0 : 1);
434 mempool_free(reg, rh->region_pool);
435 }
436
Jonathan Brassow943317e2007-07-12 17:28:25 +0100437 list_for_each_entry_safe(reg, next, &clean, list) {
438 rh->log->type->clear_region(rh->log, reg->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mempool_free(reg, rh->region_pool);
Jonathan Brassow943317e2007-07-12 17:28:25 +0100440 }
441
442 rh->log->type->flush(rh->log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static void rh_inc(struct region_hash *rh, region_t region)
446{
447 struct region *reg;
448
449 read_lock(&rh->hash_lock);
450 reg = __rh_find(rh, region);
Jun'ichi Nomura844e8d92005-09-09 16:23:42 -0700451
Jonathan E Brassow7692c5d2005-11-21 21:32:37 -0800452 spin_lock_irq(&rh->region_lock);
Jun'ichi Nomura844e8d92005-09-09 16:23:42 -0700453 atomic_inc(&reg->pending);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (reg->state == RH_CLEAN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 reg->state = RH_DIRTY;
457 list_del_init(&reg->list); /* take off the clean list */
Jonathan E Brassow7692c5d2005-11-21 21:32:37 -0800458 spin_unlock_irq(&rh->region_lock);
459
460 rh->log->type->mark_region(rh->log, reg->key);
461 } else
462 spin_unlock_irq(&rh->region_lock);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 read_unlock(&rh->hash_lock);
466}
467
468static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
469{
470 struct bio *bio;
471
472 for (bio = bios->head; bio; bio = bio->bi_next)
473 rh_inc(rh, bio_to_region(rh, bio));
474}
475
476static void rh_dec(struct region_hash *rh, region_t region)
477{
478 unsigned long flags;
479 struct region *reg;
480 int should_wake = 0;
481
482 read_lock(&rh->hash_lock);
483 reg = __rh_lookup(rh, region);
484 read_unlock(&rh->hash_lock);
485
Jonathan E Brassow7692c5d2005-11-21 21:32:37 -0800486 spin_lock_irqsave(&rh->region_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (atomic_dec_and_test(&reg->pending)) {
Jun'ichi Nomura930d3322006-03-27 01:17:47 -0800488 /*
489 * There is no pending I/O for this region.
490 * We can move the region to corresponding list for next action.
491 * At this point, the region is not yet connected to any list.
492 *
493 * If the state is RH_NOSYNC, the region should be kept off
494 * from clean list.
495 * The hash entry for RH_NOSYNC will remain in memory
496 * until the region is recovered or the map is reloaded.
497 */
498
499 /* do nothing for RH_NOSYNC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (reg->state == RH_RECOVERING) {
501 list_add_tail(&reg->list, &rh->quiesced_regions);
Jun'ichi Nomura930d3322006-03-27 01:17:47 -0800502 } else if (reg->state == RH_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 reg->state = RH_CLEAN;
504 list_add(&reg->list, &rh->clean_regions);
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 should_wake = 1;
507 }
Jonathan E Brassow7692c5d2005-11-21 21:32:37 -0800508 spin_unlock_irqrestore(&rh->region_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (should_wake)
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700511 wake(rh->ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/*
515 * Starts quiescing a region in preparation for recovery.
516 */
517static int __rh_recovery_prepare(struct region_hash *rh)
518{
519 int r;
520 struct region *reg;
521 region_t region;
522
523 /*
524 * Ask the dirty log what's next.
525 */
526 r = rh->log->type->get_resync_work(rh->log, &region);
527 if (r <= 0)
528 return r;
529
530 /*
531 * Get this region, and start it quiescing by setting the
532 * recovering flag.
533 */
534 read_lock(&rh->hash_lock);
535 reg = __rh_find(rh, region);
536 read_unlock(&rh->hash_lock);
537
538 spin_lock_irq(&rh->region_lock);
539 reg->state = RH_RECOVERING;
540
541 /* Already quiesced ? */
542 if (atomic_read(&reg->pending))
543 list_del_init(&reg->list);
Akinobu Mita179e0912006-06-26 00:24:41 -0700544 else
545 list_move(&reg->list, &rh->quiesced_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_unlock_irq(&rh->region_lock);
548
549 return 1;
550}
551
552static void rh_recovery_prepare(struct region_hash *rh)
553{
Jonathan E Brassow33184042006-11-08 17:44:44 -0800554 /* Extra reference to avoid race with rh_stop_recovery */
555 atomic_inc(&rh->recovery_in_flight);
556
557 while (!down_trylock(&rh->recovery_count)) {
558 atomic_inc(&rh->recovery_in_flight);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (__rh_recovery_prepare(rh) <= 0) {
Jonathan E Brassow33184042006-11-08 17:44:44 -0800560 atomic_dec(&rh->recovery_in_flight);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 up(&rh->recovery_count);
562 break;
563 }
Jonathan E Brassow33184042006-11-08 17:44:44 -0800564 }
565
566 /* Drop the extra reference */
567 if (atomic_dec_and_test(&rh->recovery_in_flight))
568 wake_up_all(&_kmirrord_recovery_stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
572 * Returns any quiesced regions.
573 */
574static struct region *rh_recovery_start(struct region_hash *rh)
575{
576 struct region *reg = NULL;
577
578 spin_lock_irq(&rh->region_lock);
579 if (!list_empty(&rh->quiesced_regions)) {
580 reg = list_entry(rh->quiesced_regions.next,
581 struct region, list);
582 list_del_init(&reg->list); /* remove from the quiesced list */
583 }
584 spin_unlock_irq(&rh->region_lock);
585
586 return reg;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589static void rh_recovery_end(struct region *reg, int success)
590{
591 struct region_hash *rh = reg->rh;
592
593 spin_lock_irq(&rh->region_lock);
Jonathan Brassowf44db672007-07-12 17:29:04 +0100594 if (success)
595 list_add(&reg->list, &reg->rh->recovered_regions);
596 else {
597 reg->state = RH_NOSYNC;
598 list_add(&reg->list, &reg->rh->failed_recovered_regions);
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_unlock_irq(&rh->region_lock);
601
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700602 wake(rh->ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100605static int rh_flush(struct region_hash *rh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jonathan Brassowfc1ff952007-07-12 17:29:15 +0100607 return rh->log->type->flush(rh->log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610static void rh_delay(struct region_hash *rh, struct bio *bio)
611{
612 struct region *reg;
613
614 read_lock(&rh->hash_lock);
615 reg = __rh_find(rh, bio_to_region(rh, bio));
616 bio_list_add(&reg->delayed_bios, bio);
617 read_unlock(&rh->hash_lock);
618}
619
620static void rh_stop_recovery(struct region_hash *rh)
621{
622 int i;
623
624 /* wait for any recovering regions */
625 for (i = 0; i < MAX_RECOVERY; i++)
626 down(&rh->recovery_count);
627}
628
629static void rh_start_recovery(struct region_hash *rh)
630{
631 int i;
632
633 for (i = 0; i < MAX_RECOVERY; i++)
634 up(&rh->recovery_count);
635
Holger Smolinski6ad36fe2007-05-09 02:32:50 -0700636 wake(rh->ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/*
640 * Every mirror should look like this one.
641 */
642#define DEFAULT_MIRROR 0
643
644/*
645 * This is yucky. We squirrel the mirror_set struct away inside
646 * bi_next for write buffers. This is safe since the bh
647 * doesn't get submitted to the lower levels of block layer.
648 */
649static struct mirror_set *bio_get_ms(struct bio *bio)
650{
651 return (struct mirror_set *) bio->bi_next;
652}
653
654static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
655{
656 bio->bi_next = (struct bio *) ms;
657}
658
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000659static struct mirror *get_default_mirror(struct mirror_set *ms)
660{
661 return &ms->mirror[atomic_read(&ms->default_mirror)];
662}
663
664static void set_default_mirror(struct mirror *m)
665{
666 struct mirror_set *ms = m->ms;
667 struct mirror *m0 = &(ms->mirror[0]);
668
669 atomic_set(&ms->default_mirror, m - m0);
670}
671
672/* fail_mirror
673 * @m: mirror device to fail
674 * @error_type: one of the enum's, DM_RAID1_*_ERROR
675 *
676 * If errors are being handled, record the type of
677 * error encountered for this device. If this type
678 * of error has already been recorded, we can return;
679 * otherwise, we must signal userspace by triggering
680 * an event. Additionally, if the device is the
681 * primary device, we must choose a new primary, but
682 * only if the mirror is in-sync.
683 *
684 * This function must not block.
685 */
686static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
687{
688 struct mirror_set *ms = m->ms;
689 struct mirror *new;
690
691 if (!errors_handled(ms))
692 return;
693
694 /*
695 * error_count is used for nothing more than a
696 * simple way to tell if a device has encountered
697 * errors.
698 */
699 atomic_inc(&m->error_count);
700
701 if (test_and_set_bit(error_type, &m->error_type))
702 return;
703
704 if (m != get_default_mirror(ms))
705 goto out;
706
707 if (!ms->in_sync) {
708 /*
709 * Better to issue requests to same failing device
710 * than to risk returning corrupt data.
711 */
712 DMERR("Primary mirror (%s) failed while out-of-sync: "
713 "Reads may fail.", m->dev->name);
714 goto out;
715 }
716
717 for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
718 if (!atomic_read(&new->error_count)) {
719 set_default_mirror(new);
720 break;
721 }
722
723 if (unlikely(new == ms->mirror + ms->nr_mirrors))
724 DMWARN("All sides of mirror have failed.");
725
726out:
727 schedule_work(&ms->trigger_event);
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*-----------------------------------------------------------------
731 * Recovery.
732 *
733 * When a mirror is first activated we may find that some regions
734 * are in the no-sync state. We have to recover these by
735 * recopying from the default mirror to all the others.
736 *---------------------------------------------------------------*/
737static void recovery_complete(int read_err, unsigned int write_err,
738 void *context)
739{
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000740 struct region *reg = (struct region *)context;
741 struct mirror_set *ms = reg->rh->ms;
742 int m, bit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000744 if (read_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100745 /* Read error means the failure of default mirror. */
746 DMERR_LIMIT("Unable to read primary mirror during recovery");
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000747 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
748 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100749
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000750 if (write_err) {
Jonathan Brassowf44db672007-07-12 17:29:04 +0100751 DMERR_LIMIT("Write error during recovery (error = 0x%x)",
752 write_err);
Jonathan Brassow8f0205b2008-02-08 02:11:32 +0000753 /*
754 * Bits correspond to devices (excluding default mirror).
755 * The default mirror cannot change during recovery.
756 */
757 for (m = 0; m < ms->nr_mirrors; m++) {
758 if (&ms->mirror[m] == get_default_mirror(ms))
759 continue;
760 if (test_bit(bit, &write_err))
761 fail_mirror(ms->mirror + m,
762 DM_RAID1_SYNC_ERROR);
763 bit++;
764 }
765 }
Jonathan Brassowf44db672007-07-12 17:29:04 +0100766
Jonathan Brassowce503f52006-06-26 00:27:30 -0700767 rh_recovery_end(reg, !(read_err || write_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static int recover(struct mirror_set *ms, struct region *reg)
771{
772 int r;
773 unsigned int i;
774 struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
775 struct mirror *m;
776 unsigned long flags = 0;
777
778 /* fill in the source */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000779 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 from.bdev = m->dev->bdev;
781 from.sector = m->offset + region_to_sector(reg->rh, reg->key);
782 if (reg->key == (ms->nr_regions - 1)) {
783 /*
784 * The final region may be smaller than
785 * region_size.
786 */
787 from.count = ms->ti->len & (reg->rh->region_size - 1);
788 if (!from.count)
789 from.count = reg->rh->region_size;
790 } else
791 from.count = reg->rh->region_size;
792
793 /* fill in the destinations */
794 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000795 if (&ms->mirror[i] == get_default_mirror(ms))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 continue;
797
798 m = ms->mirror + i;
799 dest->bdev = m->dev->bdev;
800 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
801 dest->count = from.count;
802 dest++;
803 }
804
805 /* hand to kcopyd */
806 set_bit(KCOPYD_IGNORE_ERROR, &flags);
807 r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
808 recovery_complete, reg);
809
810 return r;
811}
812
813static void do_recovery(struct mirror_set *ms)
814{
815 int r;
816 struct region *reg;
817 struct dirty_log *log = ms->rh.log;
818
819 /*
820 * Start quiescing some regions.
821 */
822 rh_recovery_prepare(&ms->rh);
823
824 /*
825 * Copy any already quiesced regions.
826 */
827 while ((reg = rh_recovery_start(&ms->rh))) {
828 r = recover(ms, reg);
829 if (r)
830 rh_recovery_end(reg, 0);
831 }
832
833 /*
834 * Update the in sync flag.
835 */
836 if (!ms->in_sync &&
837 (log->type->get_sync_count(log) == ms->nr_regions)) {
838 /* the sync is complete */
839 dm_table_event(ms->ti->table);
840 ms->in_sync = 1;
841 }
842}
843
844/*-----------------------------------------------------------------
845 * Reads
846 *---------------------------------------------------------------*/
847static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
848{
849 /* FIXME: add read balancing */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000850 return get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/*
854 * remap a buffer to a particular mirror.
855 */
856static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
857{
858 bio->bi_bdev = m->dev->bdev;
859 bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
860}
861
862static void do_reads(struct mirror_set *ms, struct bio_list *reads)
863{
864 region_t region;
865 struct bio *bio;
866 struct mirror *m;
867
868 while ((bio = bio_list_pop(reads))) {
869 region = bio_to_region(&ms->rh, bio);
870
871 /*
872 * We can only read balance if the region is in sync.
873 */
Jonathan Brassowb997b822007-05-09 02:33:08 -0700874 if (rh_in_sync(&ms->rh, region, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 m = choose_mirror(ms, bio->bi_sector);
876 else
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000877 m = get_default_mirror(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 map_bio(ms, m, bio);
880 generic_make_request(bio);
881 }
882}
883
884/*-----------------------------------------------------------------
885 * Writes.
886 *
887 * We do different things with the write io depending on the
888 * state of the region that it's in:
889 *
890 * SYNC: increment pending, use kcopyd to write to *all* mirrors
891 * RECOVERING: delay the io until recovery completes
892 * NOSYNC: increment pending, just write to the default mirror
893 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000894
895/* __bio_mark_nosync
896 * @ms
897 * @bio
898 * @done
899 * @error
900 *
901 * The bio was written on some mirror(s) but failed on other mirror(s).
902 * We can successfully endio the bio but should avoid the region being
903 * marked clean by setting the state RH_NOSYNC.
904 *
905 * This function is _not_ safe in interrupt context!
906 */
907static void __bio_mark_nosync(struct mirror_set *ms,
908 struct bio *bio, unsigned done, int error)
909{
910 unsigned long flags;
911 struct region_hash *rh = &ms->rh;
912 struct dirty_log *log = ms->rh.log;
913 struct region *reg;
914 region_t region = bio_to_region(rh, bio);
915 int recovering = 0;
916
917 /* We must inform the log that the sync count has changed. */
918 log->type->set_region_sync(log, region, 0);
919 ms->in_sync = 0;
920
921 read_lock(&rh->hash_lock);
922 reg = __rh_find(rh, region);
923 read_unlock(&rh->hash_lock);
924
925 /* region hash entry should exist because write was in-flight */
926 BUG_ON(!reg);
927 BUG_ON(!list_empty(&reg->list));
928
929 spin_lock_irqsave(&rh->region_lock, flags);
930 /*
931 * Possible cases:
932 * 1) RH_DIRTY
933 * 2) RH_NOSYNC: was dirty, other preceeding writes failed
934 * 3) RH_RECOVERING: flushing pending writes
935 * Either case, the region should have not been connected to list.
936 */
937 recovering = (reg->state == RH_RECOVERING);
938 reg->state = RH_NOSYNC;
939 BUG_ON(!list_empty(&reg->list));
940 spin_unlock_irqrestore(&rh->region_lock, flags);
941
942 bio_endio(bio, error);
943 if (recovering)
944 complete_resync_work(reg, 0);
945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static void write_callback(unsigned long error, void *context)
948{
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000949 unsigned i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 struct bio *bio = (struct bio *) context;
951 struct mirror_set *ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000952 int uptodate = 0;
953 int should_wake = 0;
954 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 ms = bio_get_ms(bio);
957 bio_set_ms(bio, NULL);
958
959 /*
960 * NOTE: We don't decrement the pending count here,
961 * instead it is done by the targets endio function.
962 * This way we handle both writes to SYNC and NOSYNC
963 * regions with the same code.
964 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000965 if (likely(!error))
966 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000968 for (i = 0; i < ms->nr_mirrors; i++)
969 if (test_bit(i, &error))
970 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
971 else
972 uptodate = 1;
973
974 if (unlikely(!uptodate)) {
975 DMERR("All replicated volumes dead, failing I/O");
976 /* None of the writes succeeded, fail the I/O. */
977 ret = -EIO;
978 } else if (errors_handled(ms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /*
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000980 * Need to raise event. Since raising
981 * events can block, we need to do it in
982 * the main thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000984 spin_lock_irqsave(&ms->lock, flags);
985 if (!ms->failures.head)
986 should_wake = 1;
987 bio_list_add(&ms->failures, bio);
988 spin_unlock_irqrestore(&ms->lock, flags);
989 if (should_wake)
990 wake(ms);
991 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Jonathan Brassow72f4b312008-02-08 02:11:29 +0000993out:
994 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static void do_write(struct mirror_set *ms, struct bio *bio)
998{
999 unsigned int i;
1000 struct io_region io[KCOPYD_MAX_REGIONS+1];
1001 struct mirror *m;
Milan Broz88be1632007-05-09 02:33:04 -07001002 struct dm_io_request io_req = {
1003 .bi_rw = WRITE,
1004 .mem.type = DM_IO_BVEC,
1005 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
1006 .notify.fn = write_callback,
1007 .notify.context = bio,
1008 .client = ms->io_client,
1009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 for (i = 0; i < ms->nr_mirrors; i++) {
1012 m = ms->mirror + i;
1013
1014 io[i].bdev = m->dev->bdev;
1015 io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
1016 io[i].count = bio->bi_size >> 9;
1017 }
1018
1019 bio_set_ms(bio, ms);
Milan Broz88be1632007-05-09 02:33:04 -07001020
1021 (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024static void do_writes(struct mirror_set *ms, struct bio_list *writes)
1025{
1026 int state;
1027 struct bio *bio;
1028 struct bio_list sync, nosync, recover, *this_list = NULL;
1029
1030 if (!writes->head)
1031 return;
1032
1033 /*
1034 * Classify each write.
1035 */
1036 bio_list_init(&sync);
1037 bio_list_init(&nosync);
1038 bio_list_init(&recover);
1039
1040 while ((bio = bio_list_pop(writes))) {
1041 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
1042 switch (state) {
1043 case RH_CLEAN:
1044 case RH_DIRTY:
1045 this_list = &sync;
1046 break;
1047
1048 case RH_NOSYNC:
1049 this_list = &nosync;
1050 break;
1051
1052 case RH_RECOVERING:
1053 this_list = &recover;
1054 break;
1055 }
1056
1057 bio_list_add(this_list, bio);
1058 }
1059
1060 /*
1061 * Increment the pending counts for any regions that will
1062 * be written to (writes to recover regions are going to
1063 * be delayed).
1064 */
1065 rh_inc_pending(&ms->rh, &sync);
1066 rh_inc_pending(&ms->rh, &nosync);
Jonathan Brassowfc1ff952007-07-12 17:29:15 +01001067 ms->log_failure = rh_flush(&ms->rh) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /*
1070 * Dispatch io.
1071 */
Jonathan Brassowfc1ff952007-07-12 17:29:15 +01001072 if (unlikely(ms->log_failure))
1073 while ((bio = bio_list_pop(&sync)))
NeilBrown6712ecf2007-09-27 12:47:43 +02001074 bio_endio(bio, -EIO);
Jonathan Brassowfc1ff952007-07-12 17:29:15 +01001075 else while ((bio = bio_list_pop(&sync)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 do_write(ms, bio);
1077
1078 while ((bio = bio_list_pop(&recover)))
1079 rh_delay(&ms->rh, bio);
1080
1081 while ((bio = bio_list_pop(&nosync))) {
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001082 map_bio(ms, get_default_mirror(ms), bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 generic_make_request(bio);
1084 }
1085}
1086
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001087static void do_failures(struct mirror_set *ms, struct bio_list *failures)
1088{
1089 struct bio *bio;
1090
1091 if (!failures->head)
1092 return;
1093
1094 while ((bio = bio_list_pop(failures)))
1095 __bio_mark_nosync(ms, bio, bio->bi_size, 0);
1096}
1097
1098static void trigger_event(struct work_struct *work)
1099{
1100 struct mirror_set *ms =
1101 container_of(work, struct mirror_set, trigger_event);
1102
1103 dm_table_event(ms->ti->table);
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*-----------------------------------------------------------------
1107 * kmirrord
1108 *---------------------------------------------------------------*/
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001109static int _do_mirror(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001111 struct mirror_set *ms =container_of(work, struct mirror_set,
1112 kmirrord_work);
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001113 struct bio_list reads, writes, failures;
1114 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001116 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 reads = ms->reads;
1118 writes = ms->writes;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001119 failures = ms->failures;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 bio_list_init(&ms->reads);
1121 bio_list_init(&ms->writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001122 bio_list_init(&ms->failures);
1123 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 rh_update_states(&ms->rh);
1126 do_recovery(ms);
1127 do_reads(ms, &reads);
1128 do_writes(ms, &writes);
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001129 do_failures(ms, &failures);
1130
1131 return (ms->failures.head) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001134static void do_mirror(struct work_struct *work)
1135{
1136 /*
1137 * If _do_mirror returns 1, we give it
1138 * another shot. This helps for cases like
1139 * 'suspend' where we call flush_workqueue
1140 * and expect all work to be finished. If
1141 * a failure happens during a suspend, we
1142 * couldn't issue a 'wake' because it would
1143 * not be honored. Therefore, we return '1'
1144 * from _do_mirror, and retry here.
1145 */
1146 while (_do_mirror(work))
1147 schedule();
1148}
1149
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151/*-----------------------------------------------------------------
1152 * Target functions
1153 *---------------------------------------------------------------*/
1154static struct mirror_set *alloc_context(unsigned int nr_mirrors,
1155 uint32_t region_size,
1156 struct dm_target *ti,
1157 struct dirty_log *dl)
1158{
1159 size_t len;
1160 struct mirror_set *ms = NULL;
1161
1162 if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
1163 return NULL;
1164
1165 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
1166
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001167 ms = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!ms) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001169 ti->error = "Cannot allocate mirror context";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return NULL;
1171 }
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_lock_init(&ms->lock);
1174
1175 ms->ti = ti;
1176 ms->nr_mirrors = nr_mirrors;
1177 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
1178 ms->in_sync = 0;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001179 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Milan Broz88be1632007-05-09 02:33:04 -07001181 ms->io_client = dm_io_client_create(DM_IO_PAGES);
1182 if (IS_ERR(ms->io_client)) {
1183 ti->error = "Error creating dm_io client";
1184 kfree(ms);
1185 return NULL;
1186 }
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001189 ti->error = "Error creating dirty region hash";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001190 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 kfree(ms);
1192 return NULL;
1193 }
1194
1195 return ms;
1196}
1197
1198static void free_context(struct mirror_set *ms, struct dm_target *ti,
1199 unsigned int m)
1200{
1201 while (m--)
1202 dm_put_device(ti, ms->mirror[m].dev);
1203
Milan Broz88be1632007-05-09 02:33:04 -07001204 dm_io_client_destroy(ms->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 rh_exit(&ms->rh);
1206 kfree(ms);
1207}
1208
1209static inline int _check_region_size(struct dm_target *ti, uint32_t size)
1210{
vignesh babu6f3c3f02007-10-19 22:38:44 +01001211 return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 size > ti->len);
1213}
1214
1215static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
1216 unsigned int mirror, char **argv)
1217{
Andrew Morton4ee218c2006-03-27 01:17:48 -08001218 unsigned long long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Andrew Morton4ee218c2006-03-27 01:17:48 -08001220 if (sscanf(argv[1], "%llu", &offset) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001221 ti->error = "Invalid offset";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EINVAL;
1223 }
1224
1225 if (dm_get_device(ti, argv[0], offset, ti->len,
1226 dm_table_get_mode(ti->table),
1227 &ms->mirror[mirror].dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001228 ti->error = "Device lookup failure";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -ENXIO;
1230 }
1231
Jonathan Brassowaa5617c2007-10-19 22:47:58 +01001232 ms->mirror[mirror].ms = ms;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001233 atomic_set(&(ms->mirror[mirror].error_count), 0);
1234 ms->mirror[mirror].error_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ms->mirror[mirror].offset = offset;
1236
1237 return 0;
1238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/*
1241 * Create dirty log: log_type #log_params <log_params>
1242 */
1243static struct dirty_log *create_dirty_log(struct dm_target *ti,
1244 unsigned int argc, char **argv,
1245 unsigned int *args_used)
1246{
1247 unsigned int param_count;
1248 struct dirty_log *dl;
1249
1250 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001251 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return NULL;
1253 }
1254
1255 if (sscanf(argv[1], "%u", &param_count) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001256 ti->error = "Invalid mirror log argument count";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return NULL;
1258 }
1259
1260 *args_used = 2 + param_count;
1261
1262 if (argc < *args_used) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001263 ti->error = "Insufficient mirror log arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return NULL;
1265 }
1266
1267 dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
1268 if (!dl) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001269 ti->error = "Error creating mirror dirty log";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return NULL;
1271 }
1272
1273 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001274 ti->error = "Invalid region size";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 dm_destroy_dirty_log(dl);
1276 return NULL;
1277 }
1278
1279 return dl;
1280}
1281
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001282static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1283 unsigned *args_used)
1284{
1285 unsigned num_features;
1286 struct dm_target *ti = ms->ti;
1287
1288 *args_used = 0;
1289
1290 if (!argc)
1291 return 0;
1292
1293 if (sscanf(argv[0], "%u", &num_features) != 1) {
1294 ti->error = "Invalid number of features";
1295 return -EINVAL;
1296 }
1297
1298 argc--;
1299 argv++;
1300 (*args_used)++;
1301
1302 if (num_features > argc) {
1303 ti->error = "Not enough arguments to support feature count";
1304 return -EINVAL;
1305 }
1306
1307 if (!strcmp("handle_errors", argv[0]))
1308 ms->features |= DM_RAID1_HANDLE_ERRORS;
1309 else {
1310 ti->error = "Unrecognised feature requested";
1311 return -EINVAL;
1312 }
1313
1314 (*args_used)++;
1315
1316 return 0;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/*
1320 * Construct a mirror mapping:
1321 *
1322 * log_type #log_params <log_params>
1323 * #mirrors [mirror_path offset]{2,}
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001324 * [#features <features>]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 *
1326 * log_type is "core" or "disk"
1327 * #log_params is between 1 and 3
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001328 *
1329 * If present, features must be "handle_errors".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1332{
1333 int r;
1334 unsigned int nr_mirrors, m, args_used;
1335 struct mirror_set *ms;
1336 struct dirty_log *dl;
1337
1338 dl = create_dirty_log(ti, argc, argv, &args_used);
1339 if (!dl)
1340 return -EINVAL;
1341
1342 argv += args_used;
1343 argc -= args_used;
1344
1345 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1346 nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001347 ti->error = "Invalid number of mirrors";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 dm_destroy_dirty_log(dl);
1349 return -EINVAL;
1350 }
1351
1352 argv++, argc--;
1353
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001354 if (argc < nr_mirrors * 2) {
1355 ti->error = "Too few mirror arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 dm_destroy_dirty_log(dl);
1357 return -EINVAL;
1358 }
1359
1360 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1361 if (!ms) {
1362 dm_destroy_dirty_log(dl);
1363 return -ENOMEM;
1364 }
1365
1366 /* Get the mirror parameter sets */
1367 for (m = 0; m < nr_mirrors; m++) {
1368 r = get_mirror(ms, ti, m, argv);
1369 if (r) {
1370 free_context(ms, ti, m);
1371 return r;
1372 }
1373 argv += 2;
1374 argc -= 2;
1375 }
1376
1377 ti->private = ms;
Alasdair G Kergond88854f2005-07-07 17:59:34 -07001378 ti->split_io = ms->rh.region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001380 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1381 if (!ms->kmirrord_wq) {
1382 DMERR("couldn't start kmirrord");
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001383 r = -ENOMEM;
1384 goto err_free_context;
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001385 }
1386 INIT_WORK(&ms->kmirrord_work, do_mirror);
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001387 INIT_WORK(&ms->trigger_event, trigger_event);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001388
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001389 r = parse_features(ms, argc, argv, &args_used);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001390 if (r)
1391 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001392
1393 argv += args_used;
1394 argc -= args_used;
1395
Jonathan Brassowf44db672007-07-12 17:29:04 +01001396 /*
1397 * Any read-balancing addition depends on the
1398 * DM_RAID1_HANDLE_ERRORS flag being present.
1399 * This is because the decision to balance depends
1400 * on the sync state of a region. If the above
1401 * flag is not present, we ignore errors; and
1402 * the sync state may be inaccurate.
1403 */
1404
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001405 if (argc) {
1406 ti->error = "Too many mirror arguments";
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001407 r = -EINVAL;
1408 goto err_destroy_wq;
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001409 }
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001412 if (r)
1413 goto err_destroy_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001415 wake(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return 0;
Dmitry Monakhova72cf732007-10-19 22:38:39 +01001417
1418err_destroy_wq:
1419 destroy_workqueue(ms->kmirrord_wq);
1420err_free_context:
1421 free_context(ms, ti, ms->nr_mirrors);
1422 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static void mirror_dtr(struct dm_target *ti)
1426{
1427 struct mirror_set *ms = (struct mirror_set *) ti->private;
1428
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001429 flush_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 kcopyd_client_destroy(ms->kcopyd_client);
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001431 destroy_workqueue(ms->kmirrord_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 free_context(ms, ti, ms->nr_mirrors);
1433}
1434
1435static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1436{
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001437 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int should_wake = 0;
1439 struct bio_list *bl;
1440
1441 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001442 spin_lock_irqsave(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 should_wake = !(bl->head);
1444 bio_list_add(bl, bio);
Jonathan Brassow72f4b312008-02-08 02:11:29 +00001445 spin_unlock_irqrestore(&ms->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (should_wake)
Holger Smolinski6ad36fe2007-05-09 02:32:50 -07001448 wake(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451/*
1452 * Mirror mapping function
1453 */
1454static int mirror_map(struct dm_target *ti, struct bio *bio,
1455 union map_info *map_context)
1456{
1457 int r, rw = bio_rw(bio);
1458 struct mirror *m;
1459 struct mirror_set *ms = ti->private;
1460
Neil Browne4c8b3b2006-06-26 00:27:26 -07001461 map_context->ll = bio_to_region(&ms->rh, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 if (rw == WRITE) {
1464 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001465 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 r = ms->rh.log->type->in_sync(ms->rh.log,
1469 bio_to_region(&ms->rh, bio), 0);
1470 if (r < 0 && r != -EWOULDBLOCK)
1471 return r;
1472
1473 if (r == -EWOULDBLOCK) /* FIXME: ugly */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001474 r = DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /*
1477 * We don't want to fast track a recovery just for a read
1478 * ahead. So we just let it silently fail.
1479 * FIXME: get rid of this.
1480 */
1481 if (!r && rw == READA)
1482 return -EIO;
1483
1484 if (!r) {
1485 /* Pass this io over to the daemon */
1486 queue_bio(ms, bio, rw);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001487 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
1490 m = choose_mirror(ms, bio->bi_sector);
1491 if (!m)
1492 return -EIO;
1493
1494 map_bio(ms, m, bio);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001495 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1499 int error, union map_info *map_context)
1500{
1501 int rw = bio_rw(bio);
1502 struct mirror_set *ms = (struct mirror_set *) ti->private;
1503 region_t region = map_context->ll;
1504
1505 /*
1506 * We need to dec pending if this was a write.
1507 */
1508 if (rw == WRITE)
1509 rh_dec(&ms->rh, region);
1510
1511 return 0;
1512}
1513
1514static void mirror_postsuspend(struct dm_target *ti)
1515{
1516 struct mirror_set *ms = (struct mirror_set *) ti->private;
1517 struct dirty_log *log = ms->rh.log;
1518
1519 rh_stop_recovery(&ms->rh);
Jonathan E Brassow33184042006-11-08 17:44:44 -08001520
1521 /* Wait for all I/O we generated to complete */
1522 wait_event(_kmirrord_recovery_stopped,
1523 !atomic_read(&ms->rh.recovery_in_flight));
1524
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +01001525 if (log->type->postsuspend && log->type->postsuspend(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /* FIXME: need better error handling */
1527 DMWARN("log suspend failed");
1528}
1529
1530static void mirror_resume(struct dm_target *ti)
1531{
1532 struct mirror_set *ms = (struct mirror_set *) ti->private;
1533 struct dirty_log *log = ms->rh.log;
1534 if (log->type->resume && log->type->resume(log))
1535 /* FIXME: need better error handling */
1536 DMWARN("log resume failed");
1537 rh_start_recovery(&ms->rh);
1538}
1539
1540static int mirror_status(struct dm_target *ti, status_type_t type,
1541 char *result, unsigned int maxlen)
1542{
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001543 unsigned int m, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 struct mirror_set *ms = (struct mirror_set *) ti->private;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 switch (type) {
1547 case STATUSTYPE_INFO:
1548 DMEMIT("%d ", ms->nr_mirrors);
1549 for (m = 0; m < ms->nr_mirrors; m++)
1550 DMEMIT("%s ", ms->mirror[m].dev->name);
1551
Milan Brozc95bc202007-07-12 17:27:24 +01001552 DMEMIT("%llu/%llu 0 ",
Andrew Morton4ee218c2006-03-27 01:17:48 -08001553 (unsigned long long)ms->rh.log->type->
1554 get_sync_count(ms->rh.log),
1555 (unsigned long long)ms->nr_regions);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001556
Milan Brozc95bc202007-07-12 17:27:24 +01001557 sz += ms->rh.log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 break;
1560
1561 case STATUSTYPE_TABLE:
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001562 sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1563
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001564 DMEMIT("%d", ms->nr_mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 for (m = 0; m < ms->nr_mirrors; m++)
Jonathan Brassowe52b8f62006-10-03 01:15:32 -07001566 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
Andrew Morton4ee218c2006-03-27 01:17:48 -08001567 (unsigned long long)ms->mirror[m].offset);
Jonathan E Brassowa8e6afa2007-05-09 02:32:59 -07001568
1569 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1570 DMEMIT(" 1 handle_errors");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
1573 return 0;
1574}
1575
1576static struct target_type mirror_target = {
1577 .name = "mirror",
Jonathan E Brassow315dcc22007-05-09 02:32:58 -07001578 .version = {1, 0, 3},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 .module = THIS_MODULE,
1580 .ctr = mirror_ctr,
1581 .dtr = mirror_dtr,
1582 .map = mirror_map,
1583 .end_io = mirror_end_io,
1584 .postsuspend = mirror_postsuspend,
1585 .resume = mirror_resume,
1586 .status = mirror_status,
1587};
1588
1589static int __init dm_mirror_init(void)
1590{
1591 int r;
1592
1593 r = dm_dirty_log_init();
1594 if (r)
1595 return r;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 r = dm_register_target(&mirror_target);
1598 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001599 DMERR("Failed to register mirror target");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 dm_dirty_log_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
1603 return r;
1604}
1605
1606static void __exit dm_mirror_exit(void)
1607{
1608 int r;
1609
1610 r = dm_unregister_target(&mirror_target);
1611 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001612 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 dm_dirty_log_exit();
1615}
1616
1617/* Module hooks */
1618module_init(dm_mirror_init);
1619module_exit(dm_mirror_exit);
1620
1621MODULE_DESCRIPTION(DM_NAME " mirror target");
1622MODULE_AUTHOR("Joe Thornber");
1623MODULE_LICENSE("GPL");