Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 22 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 23 | #define DM_MSG_PREFIX "raid1" |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | static struct workqueue_struct *_kmirrord_wq; |
| 26 | static struct work_struct _kmirrord_work; |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 27 | static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | static inline void wake(void) |
| 30 | { |
| 31 | queue_work(_kmirrord_wq, &_kmirrord_work); |
| 32 | } |
| 33 | |
| 34 | /*----------------------------------------------------------------- |
| 35 | * Region hash |
| 36 | * |
| 37 | * The mirror splits itself up into discrete regions. Each |
| 38 | * region can be in one of three states: clean, dirty, |
| 39 | * nosync. There is no need to put clean regions in the hash. |
| 40 | * |
| 41 | * In addition to being present in the hash table a region _may_ |
| 42 | * be present on one of three lists. |
| 43 | * |
| 44 | * clean_regions: Regions on this list have no io pending to |
| 45 | * them, they are in sync, we are no longer interested in them, |
| 46 | * they are dull. rh_update_states() will remove them from the |
| 47 | * hash table. |
| 48 | * |
| 49 | * quiesced_regions: These regions have been spun down, ready |
| 50 | * for recovery. rh_recovery_start() will remove regions from |
| 51 | * this list and hand them to kmirrord, which will schedule the |
| 52 | * recovery io with kcopyd. |
| 53 | * |
| 54 | * recovered_regions: Regions that kcopyd has successfully |
| 55 | * recovered. rh_update_states() will now schedule any delayed |
| 56 | * io, up the recovery_count, and remove the region from the |
| 57 | * hash. |
| 58 | * |
| 59 | * There are 2 locks: |
| 60 | * A rw spin lock 'hash_lock' protects just the hash table, |
| 61 | * this is never held in write mode from interrupt context, |
| 62 | * which I believe means that we only have to disable irqs when |
| 63 | * doing a write lock. |
| 64 | * |
| 65 | * An ordinary spin lock 'region_lock' that protects the three |
| 66 | * lists in the region_hash, with the 'state', 'list' and |
| 67 | * 'bhs_delayed' fields of the regions. This is used from irq |
| 68 | * context, so all other uses will have to suspend local irqs. |
| 69 | *---------------------------------------------------------------*/ |
| 70 | struct mirror_set; |
| 71 | struct region_hash { |
| 72 | struct mirror_set *ms; |
| 73 | uint32_t region_size; |
| 74 | unsigned region_shift; |
| 75 | |
| 76 | /* holds persistent region state */ |
| 77 | struct dirty_log *log; |
| 78 | |
| 79 | /* hash table */ |
| 80 | rwlock_t hash_lock; |
| 81 | mempool_t *region_pool; |
| 82 | unsigned int mask; |
| 83 | unsigned int nr_buckets; |
| 84 | struct list_head *buckets; |
| 85 | |
| 86 | spinlock_t region_lock; |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 87 | atomic_t recovery_in_flight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | struct semaphore recovery_count; |
| 89 | struct list_head clean_regions; |
| 90 | struct list_head quiesced_regions; |
| 91 | struct list_head recovered_regions; |
| 92 | }; |
| 93 | |
| 94 | enum { |
| 95 | RH_CLEAN, |
| 96 | RH_DIRTY, |
| 97 | RH_NOSYNC, |
| 98 | RH_RECOVERING |
| 99 | }; |
| 100 | |
| 101 | struct 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 Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 113 | |
| 114 | /*----------------------------------------------------------------- |
| 115 | * Mirror set structures. |
| 116 | *---------------------------------------------------------------*/ |
| 117 | struct mirror { |
| 118 | atomic_t error_count; |
| 119 | struct dm_dev *dev; |
| 120 | sector_t offset; |
| 121 | }; |
| 122 | |
| 123 | struct mirror_set { |
| 124 | struct dm_target *ti; |
| 125 | struct list_head list; |
| 126 | struct region_hash rh; |
| 127 | struct kcopyd_client *kcopyd_client; |
| 128 | |
| 129 | spinlock_t lock; /* protects the next two lists */ |
| 130 | struct bio_list reads; |
| 131 | struct bio_list writes; |
| 132 | |
| 133 | /* recovery */ |
| 134 | region_t nr_regions; |
| 135 | int in_sync; |
| 136 | |
| 137 | struct mirror *default_mirror; /* Default mirror */ |
| 138 | |
| 139 | unsigned int nr_mirrors; |
| 140 | struct mirror mirror[0]; |
| 141 | }; |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | /* |
| 144 | * Conversion fns |
| 145 | */ |
| 146 | static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio) |
| 147 | { |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 148 | return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline sector_t region_to_sector(struct region_hash *rh, region_t region) |
| 152 | { |
| 153 | return region << rh->region_shift; |
| 154 | } |
| 155 | |
| 156 | /* FIXME move this */ |
| 157 | static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw); |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | #define MIN_REGIONS 64 |
| 160 | #define MAX_RECOVERY 1 |
| 161 | static int rh_init(struct region_hash *rh, struct mirror_set *ms, |
| 162 | struct dirty_log *log, uint32_t region_size, |
| 163 | region_t nr_regions) |
| 164 | { |
| 165 | unsigned int nr_buckets, max_buckets; |
| 166 | size_t i; |
| 167 | |
| 168 | /* |
| 169 | * Calculate a suitable number of buckets for our hash |
| 170 | * table. |
| 171 | */ |
| 172 | max_buckets = nr_regions >> 6; |
| 173 | for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1) |
| 174 | ; |
| 175 | nr_buckets >>= 1; |
| 176 | |
| 177 | rh->ms = ms; |
| 178 | rh->log = log; |
| 179 | rh->region_size = region_size; |
| 180 | rh->region_shift = ffs(region_size) - 1; |
| 181 | rwlock_init(&rh->hash_lock); |
| 182 | rh->mask = nr_buckets - 1; |
| 183 | rh->nr_buckets = nr_buckets; |
| 184 | |
| 185 | rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets)); |
| 186 | if (!rh->buckets) { |
| 187 | DMERR("unable to allocate region hash memory"); |
| 188 | return -ENOMEM; |
| 189 | } |
| 190 | |
| 191 | for (i = 0; i < nr_buckets; i++) |
| 192 | INIT_LIST_HEAD(rh->buckets + i); |
| 193 | |
| 194 | spin_lock_init(&rh->region_lock); |
| 195 | sema_init(&rh->recovery_count, 0); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 196 | atomic_set(&rh->recovery_in_flight, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | INIT_LIST_HEAD(&rh->clean_regions); |
| 198 | INIT_LIST_HEAD(&rh->quiesced_regions); |
| 199 | INIT_LIST_HEAD(&rh->recovered_regions); |
| 200 | |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 201 | rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS, |
| 202 | sizeof(struct region)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (!rh->region_pool) { |
| 204 | vfree(rh->buckets); |
| 205 | rh->buckets = NULL; |
| 206 | return -ENOMEM; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static void rh_exit(struct region_hash *rh) |
| 213 | { |
| 214 | unsigned int h; |
| 215 | struct region *reg, *nreg; |
| 216 | |
| 217 | BUG_ON(!list_empty(&rh->quiesced_regions)); |
| 218 | for (h = 0; h < rh->nr_buckets; h++) { |
| 219 | list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) { |
| 220 | BUG_ON(atomic_read(®->pending)); |
| 221 | mempool_free(reg, rh->region_pool); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (rh->log) |
| 226 | dm_destroy_dirty_log(rh->log); |
| 227 | if (rh->region_pool) |
| 228 | mempool_destroy(rh->region_pool); |
| 229 | vfree(rh->buckets); |
| 230 | } |
| 231 | |
| 232 | #define RH_HASH_MULT 2654435387U |
| 233 | |
| 234 | static inline unsigned int rh_hash(struct region_hash *rh, region_t region) |
| 235 | { |
| 236 | return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask; |
| 237 | } |
| 238 | |
| 239 | static struct region *__rh_lookup(struct region_hash *rh, region_t region) |
| 240 | { |
| 241 | struct region *reg; |
| 242 | |
| 243 | list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list) |
| 244 | if (reg->key == region) |
| 245 | return reg; |
| 246 | |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | static void __rh_insert(struct region_hash *rh, struct region *reg) |
| 251 | { |
| 252 | unsigned int h = rh_hash(rh, reg->key); |
| 253 | list_add(®->hash_list, rh->buckets + h); |
| 254 | } |
| 255 | |
| 256 | static struct region *__rh_alloc(struct region_hash *rh, region_t region) |
| 257 | { |
| 258 | struct region *reg, *nreg; |
| 259 | |
| 260 | read_unlock(&rh->hash_lock); |
Daniel Kobras | c06aad8 | 2006-08-27 01:23:24 -0700 | [diff] [blame] | 261 | nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC); |
| 262 | if (unlikely(!nreg)) |
| 263 | nreg = kmalloc(sizeof(struct region), GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | nreg->state = rh->log->type->in_sync(rh->log, region, 1) ? |
| 265 | RH_CLEAN : RH_NOSYNC; |
| 266 | nreg->rh = rh; |
| 267 | nreg->key = region; |
| 268 | |
| 269 | INIT_LIST_HEAD(&nreg->list); |
| 270 | |
| 271 | atomic_set(&nreg->pending, 0); |
| 272 | bio_list_init(&nreg->delayed_bios); |
| 273 | write_lock_irq(&rh->hash_lock); |
| 274 | |
| 275 | reg = __rh_lookup(rh, region); |
| 276 | if (reg) |
| 277 | /* we lost the race */ |
| 278 | mempool_free(nreg, rh->region_pool); |
| 279 | |
| 280 | else { |
| 281 | __rh_insert(rh, nreg); |
| 282 | if (nreg->state == RH_CLEAN) { |
| 283 | spin_lock(&rh->region_lock); |
| 284 | list_add(&nreg->list, &rh->clean_regions); |
| 285 | spin_unlock(&rh->region_lock); |
| 286 | } |
| 287 | reg = nreg; |
| 288 | } |
| 289 | write_unlock_irq(&rh->hash_lock); |
| 290 | read_lock(&rh->hash_lock); |
| 291 | |
| 292 | return reg; |
| 293 | } |
| 294 | |
| 295 | static inline struct region *__rh_find(struct region_hash *rh, region_t region) |
| 296 | { |
| 297 | struct region *reg; |
| 298 | |
| 299 | reg = __rh_lookup(rh, region); |
| 300 | if (!reg) |
| 301 | reg = __rh_alloc(rh, region); |
| 302 | |
| 303 | return reg; |
| 304 | } |
| 305 | |
| 306 | static int rh_state(struct region_hash *rh, region_t region, int may_block) |
| 307 | { |
| 308 | int r; |
| 309 | struct region *reg; |
| 310 | |
| 311 | read_lock(&rh->hash_lock); |
| 312 | reg = __rh_lookup(rh, region); |
| 313 | read_unlock(&rh->hash_lock); |
| 314 | |
| 315 | if (reg) |
| 316 | return reg->state; |
| 317 | |
| 318 | /* |
| 319 | * The region wasn't in the hash, so we fall back to the |
| 320 | * dirty log. |
| 321 | */ |
| 322 | r = rh->log->type->in_sync(rh->log, region, may_block); |
| 323 | |
| 324 | /* |
| 325 | * Any error from the dirty log (eg. -EWOULDBLOCK) gets |
| 326 | * taken as a RH_NOSYNC |
| 327 | */ |
| 328 | return r == 1 ? RH_CLEAN : RH_NOSYNC; |
| 329 | } |
| 330 | |
| 331 | static inline int rh_in_sync(struct region_hash *rh, |
| 332 | region_t region, int may_block) |
| 333 | { |
| 334 | int state = rh_state(rh, region, may_block); |
| 335 | return state == RH_CLEAN || state == RH_DIRTY; |
| 336 | } |
| 337 | |
| 338 | static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list) |
| 339 | { |
| 340 | struct bio *bio; |
| 341 | |
| 342 | while ((bio = bio_list_pop(bio_list))) { |
| 343 | queue_bio(ms, bio, WRITE); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static void rh_update_states(struct region_hash *rh) |
| 348 | { |
| 349 | struct region *reg, *next; |
| 350 | |
| 351 | LIST_HEAD(clean); |
| 352 | LIST_HEAD(recovered); |
| 353 | |
| 354 | /* |
| 355 | * Quickly grab the lists. |
| 356 | */ |
| 357 | write_lock_irq(&rh->hash_lock); |
| 358 | spin_lock(&rh->region_lock); |
| 359 | if (!list_empty(&rh->clean_regions)) { |
| 360 | list_splice(&rh->clean_regions, &clean); |
| 361 | INIT_LIST_HEAD(&rh->clean_regions); |
| 362 | |
| 363 | list_for_each_entry (reg, &clean, list) { |
| 364 | rh->log->type->clear_region(rh->log, reg->key); |
| 365 | list_del(®->hash_list); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (!list_empty(&rh->recovered_regions)) { |
| 370 | list_splice(&rh->recovered_regions, &recovered); |
| 371 | INIT_LIST_HEAD(&rh->recovered_regions); |
| 372 | |
| 373 | list_for_each_entry (reg, &recovered, list) |
| 374 | list_del(®->hash_list); |
| 375 | } |
| 376 | spin_unlock(&rh->region_lock); |
| 377 | write_unlock_irq(&rh->hash_lock); |
| 378 | |
| 379 | /* |
| 380 | * All the regions on the recovered and clean lists have |
| 381 | * now been pulled out of the system, so no need to do |
| 382 | * any more locking. |
| 383 | */ |
| 384 | list_for_each_entry_safe (reg, next, &recovered, list) { |
| 385 | rh->log->type->clear_region(rh->log, reg->key); |
| 386 | rh->log->type->complete_resync_work(rh->log, reg->key, 1); |
| 387 | dispatch_bios(rh->ms, ®->delayed_bios); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 388 | if (atomic_dec_and_test(&rh->recovery_in_flight)) |
| 389 | wake_up_all(&_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | up(&rh->recovery_count); |
| 391 | mempool_free(reg, rh->region_pool); |
| 392 | } |
| 393 | |
| 394 | if (!list_empty(&recovered)) |
| 395 | rh->log->type->flush(rh->log); |
| 396 | |
| 397 | list_for_each_entry_safe (reg, next, &clean, list) |
| 398 | mempool_free(reg, rh->region_pool); |
| 399 | } |
| 400 | |
| 401 | static void rh_inc(struct region_hash *rh, region_t region) |
| 402 | { |
| 403 | struct region *reg; |
| 404 | |
| 405 | read_lock(&rh->hash_lock); |
| 406 | reg = __rh_find(rh, region); |
Jun'ichi Nomura | 844e8d9 | 2005-09-09 16:23:42 -0700 | [diff] [blame] | 407 | |
Jonathan E Brassow | 7692c5d | 2005-11-21 21:32:37 -0800 | [diff] [blame] | 408 | spin_lock_irq(&rh->region_lock); |
Jun'ichi Nomura | 844e8d9 | 2005-09-09 16:23:42 -0700 | [diff] [blame] | 409 | atomic_inc(®->pending); |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (reg->state == RH_CLEAN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | reg->state = RH_DIRTY; |
| 413 | list_del_init(®->list); /* take off the clean list */ |
Jonathan E Brassow | 7692c5d | 2005-11-21 21:32:37 -0800 | [diff] [blame] | 414 | spin_unlock_irq(&rh->region_lock); |
| 415 | |
| 416 | rh->log->type->mark_region(rh->log, reg->key); |
| 417 | } else |
| 418 | spin_unlock_irq(&rh->region_lock); |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | read_unlock(&rh->hash_lock); |
| 422 | } |
| 423 | |
| 424 | static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios) |
| 425 | { |
| 426 | struct bio *bio; |
| 427 | |
| 428 | for (bio = bios->head; bio; bio = bio->bi_next) |
| 429 | rh_inc(rh, bio_to_region(rh, bio)); |
| 430 | } |
| 431 | |
| 432 | static void rh_dec(struct region_hash *rh, region_t region) |
| 433 | { |
| 434 | unsigned long flags; |
| 435 | struct region *reg; |
| 436 | int should_wake = 0; |
| 437 | |
| 438 | read_lock(&rh->hash_lock); |
| 439 | reg = __rh_lookup(rh, region); |
| 440 | read_unlock(&rh->hash_lock); |
| 441 | |
Jonathan E Brassow | 7692c5d | 2005-11-21 21:32:37 -0800 | [diff] [blame] | 442 | spin_lock_irqsave(&rh->region_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (atomic_dec_and_test(®->pending)) { |
Jun'ichi Nomura | 930d332 | 2006-03-27 01:17:47 -0800 | [diff] [blame] | 444 | /* |
| 445 | * There is no pending I/O for this region. |
| 446 | * We can move the region to corresponding list for next action. |
| 447 | * At this point, the region is not yet connected to any list. |
| 448 | * |
| 449 | * If the state is RH_NOSYNC, the region should be kept off |
| 450 | * from clean list. |
| 451 | * The hash entry for RH_NOSYNC will remain in memory |
| 452 | * until the region is recovered or the map is reloaded. |
| 453 | */ |
| 454 | |
| 455 | /* do nothing for RH_NOSYNC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (reg->state == RH_RECOVERING) { |
| 457 | list_add_tail(®->list, &rh->quiesced_regions); |
Jun'ichi Nomura | 930d332 | 2006-03-27 01:17:47 -0800 | [diff] [blame] | 458 | } else if (reg->state == RH_DIRTY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | reg->state = RH_CLEAN; |
| 460 | list_add(®->list, &rh->clean_regions); |
| 461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | should_wake = 1; |
| 463 | } |
Jonathan E Brassow | 7692c5d | 2005-11-21 21:32:37 -0800 | [diff] [blame] | 464 | spin_unlock_irqrestore(&rh->region_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | if (should_wake) |
| 467 | wake(); |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Starts quiescing a region in preparation for recovery. |
| 472 | */ |
| 473 | static int __rh_recovery_prepare(struct region_hash *rh) |
| 474 | { |
| 475 | int r; |
| 476 | struct region *reg; |
| 477 | region_t region; |
| 478 | |
| 479 | /* |
| 480 | * Ask the dirty log what's next. |
| 481 | */ |
| 482 | r = rh->log->type->get_resync_work(rh->log, ®ion); |
| 483 | if (r <= 0) |
| 484 | return r; |
| 485 | |
| 486 | /* |
| 487 | * Get this region, and start it quiescing by setting the |
| 488 | * recovering flag. |
| 489 | */ |
| 490 | read_lock(&rh->hash_lock); |
| 491 | reg = __rh_find(rh, region); |
| 492 | read_unlock(&rh->hash_lock); |
| 493 | |
| 494 | spin_lock_irq(&rh->region_lock); |
| 495 | reg->state = RH_RECOVERING; |
| 496 | |
| 497 | /* Already quiesced ? */ |
| 498 | if (atomic_read(®->pending)) |
| 499 | list_del_init(®->list); |
Akinobu Mita | 179e091 | 2006-06-26 00:24:41 -0700 | [diff] [blame] | 500 | else |
| 501 | list_move(®->list, &rh->quiesced_regions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | spin_unlock_irq(&rh->region_lock); |
| 504 | |
| 505 | return 1; |
| 506 | } |
| 507 | |
| 508 | static void rh_recovery_prepare(struct region_hash *rh) |
| 509 | { |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 510 | /* Extra reference to avoid race with rh_stop_recovery */ |
| 511 | atomic_inc(&rh->recovery_in_flight); |
| 512 | |
| 513 | while (!down_trylock(&rh->recovery_count)) { |
| 514 | atomic_inc(&rh->recovery_in_flight); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (__rh_recovery_prepare(rh) <= 0) { |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 516 | atomic_dec(&rh->recovery_in_flight); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | up(&rh->recovery_count); |
| 518 | break; |
| 519 | } |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 520 | } |
| 521 | |
| 522 | /* Drop the extra reference */ |
| 523 | if (atomic_dec_and_test(&rh->recovery_in_flight)) |
| 524 | wake_up_all(&_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Returns any quiesced regions. |
| 529 | */ |
| 530 | static struct region *rh_recovery_start(struct region_hash *rh) |
| 531 | { |
| 532 | struct region *reg = NULL; |
| 533 | |
| 534 | spin_lock_irq(&rh->region_lock); |
| 535 | if (!list_empty(&rh->quiesced_regions)) { |
| 536 | reg = list_entry(rh->quiesced_regions.next, |
| 537 | struct region, list); |
| 538 | list_del_init(®->list); /* remove from the quiesced list */ |
| 539 | } |
| 540 | spin_unlock_irq(&rh->region_lock); |
| 541 | |
| 542 | return reg; |
| 543 | } |
| 544 | |
| 545 | /* FIXME: success ignored for now */ |
| 546 | static void rh_recovery_end(struct region *reg, int success) |
| 547 | { |
| 548 | struct region_hash *rh = reg->rh; |
| 549 | |
| 550 | spin_lock_irq(&rh->region_lock); |
| 551 | list_add(®->list, ®->rh->recovered_regions); |
| 552 | spin_unlock_irq(&rh->region_lock); |
| 553 | |
| 554 | wake(); |
| 555 | } |
| 556 | |
| 557 | static void rh_flush(struct region_hash *rh) |
| 558 | { |
| 559 | rh->log->type->flush(rh->log); |
| 560 | } |
| 561 | |
| 562 | static void rh_delay(struct region_hash *rh, struct bio *bio) |
| 563 | { |
| 564 | struct region *reg; |
| 565 | |
| 566 | read_lock(&rh->hash_lock); |
| 567 | reg = __rh_find(rh, bio_to_region(rh, bio)); |
| 568 | bio_list_add(®->delayed_bios, bio); |
| 569 | read_unlock(&rh->hash_lock); |
| 570 | } |
| 571 | |
| 572 | static void rh_stop_recovery(struct region_hash *rh) |
| 573 | { |
| 574 | int i; |
| 575 | |
| 576 | /* wait for any recovering regions */ |
| 577 | for (i = 0; i < MAX_RECOVERY; i++) |
| 578 | down(&rh->recovery_count); |
| 579 | } |
| 580 | |
| 581 | static void rh_start_recovery(struct region_hash *rh) |
| 582 | { |
| 583 | int i; |
| 584 | |
| 585 | for (i = 0; i < MAX_RECOVERY; i++) |
| 586 | up(&rh->recovery_count); |
| 587 | |
| 588 | wake(); |
| 589 | } |
| 590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | /* |
| 592 | * Every mirror should look like this one. |
| 593 | */ |
| 594 | #define DEFAULT_MIRROR 0 |
| 595 | |
| 596 | /* |
| 597 | * This is yucky. We squirrel the mirror_set struct away inside |
| 598 | * bi_next for write buffers. This is safe since the bh |
| 599 | * doesn't get submitted to the lower levels of block layer. |
| 600 | */ |
| 601 | static struct mirror_set *bio_get_ms(struct bio *bio) |
| 602 | { |
| 603 | return (struct mirror_set *) bio->bi_next; |
| 604 | } |
| 605 | |
| 606 | static void bio_set_ms(struct bio *bio, struct mirror_set *ms) |
| 607 | { |
| 608 | bio->bi_next = (struct bio *) ms; |
| 609 | } |
| 610 | |
| 611 | /*----------------------------------------------------------------- |
| 612 | * Recovery. |
| 613 | * |
| 614 | * When a mirror is first activated we may find that some regions |
| 615 | * are in the no-sync state. We have to recover these by |
| 616 | * recopying from the default mirror to all the others. |
| 617 | *---------------------------------------------------------------*/ |
| 618 | static void recovery_complete(int read_err, unsigned int write_err, |
| 619 | void *context) |
| 620 | { |
| 621 | struct region *reg = (struct region *) context; |
| 622 | |
| 623 | /* FIXME: better error handling */ |
Jonathan Brassow | ce503f5 | 2006-06-26 00:27:30 -0700 | [diff] [blame] | 624 | rh_recovery_end(reg, !(read_err || write_err)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int recover(struct mirror_set *ms, struct region *reg) |
| 628 | { |
| 629 | int r; |
| 630 | unsigned int i; |
| 631 | struct io_region from, to[KCOPYD_MAX_REGIONS], *dest; |
| 632 | struct mirror *m; |
| 633 | unsigned long flags = 0; |
| 634 | |
| 635 | /* fill in the source */ |
Jonathan E Brassow | a1a1908 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 636 | m = ms->default_mirror; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | from.bdev = m->dev->bdev; |
| 638 | from.sector = m->offset + region_to_sector(reg->rh, reg->key); |
| 639 | if (reg->key == (ms->nr_regions - 1)) { |
| 640 | /* |
| 641 | * The final region may be smaller than |
| 642 | * region_size. |
| 643 | */ |
| 644 | from.count = ms->ti->len & (reg->rh->region_size - 1); |
| 645 | if (!from.count) |
| 646 | from.count = reg->rh->region_size; |
| 647 | } else |
| 648 | from.count = reg->rh->region_size; |
| 649 | |
| 650 | /* fill in the destinations */ |
| 651 | for (i = 0, dest = to; i < ms->nr_mirrors; i++) { |
Jonathan E Brassow | a1a1908 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 652 | if (&ms->mirror[i] == ms->default_mirror) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | continue; |
| 654 | |
| 655 | m = ms->mirror + i; |
| 656 | dest->bdev = m->dev->bdev; |
| 657 | dest->sector = m->offset + region_to_sector(reg->rh, reg->key); |
| 658 | dest->count = from.count; |
| 659 | dest++; |
| 660 | } |
| 661 | |
| 662 | /* hand to kcopyd */ |
| 663 | set_bit(KCOPYD_IGNORE_ERROR, &flags); |
| 664 | r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags, |
| 665 | recovery_complete, reg); |
| 666 | |
| 667 | return r; |
| 668 | } |
| 669 | |
| 670 | static void do_recovery(struct mirror_set *ms) |
| 671 | { |
| 672 | int r; |
| 673 | struct region *reg; |
| 674 | struct dirty_log *log = ms->rh.log; |
| 675 | |
| 676 | /* |
| 677 | * Start quiescing some regions. |
| 678 | */ |
| 679 | rh_recovery_prepare(&ms->rh); |
| 680 | |
| 681 | /* |
| 682 | * Copy any already quiesced regions. |
| 683 | */ |
| 684 | while ((reg = rh_recovery_start(&ms->rh))) { |
| 685 | r = recover(ms, reg); |
| 686 | if (r) |
| 687 | rh_recovery_end(reg, 0); |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Update the in sync flag. |
| 692 | */ |
| 693 | if (!ms->in_sync && |
| 694 | (log->type->get_sync_count(log) == ms->nr_regions)) { |
| 695 | /* the sync is complete */ |
| 696 | dm_table_event(ms->ti->table); |
| 697 | ms->in_sync = 1; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /*----------------------------------------------------------------- |
| 702 | * Reads |
| 703 | *---------------------------------------------------------------*/ |
| 704 | static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector) |
| 705 | { |
| 706 | /* FIXME: add read balancing */ |
Jonathan E Brassow | a1a1908 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 707 | return ms->default_mirror; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
| 711 | * remap a buffer to a particular mirror. |
| 712 | */ |
| 713 | static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio) |
| 714 | { |
| 715 | bio->bi_bdev = m->dev->bdev; |
| 716 | bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin); |
| 717 | } |
| 718 | |
| 719 | static void do_reads(struct mirror_set *ms, struct bio_list *reads) |
| 720 | { |
| 721 | region_t region; |
| 722 | struct bio *bio; |
| 723 | struct mirror *m; |
| 724 | |
| 725 | while ((bio = bio_list_pop(reads))) { |
| 726 | region = bio_to_region(&ms->rh, bio); |
| 727 | |
| 728 | /* |
| 729 | * We can only read balance if the region is in sync. |
| 730 | */ |
| 731 | if (rh_in_sync(&ms->rh, region, 0)) |
| 732 | m = choose_mirror(ms, bio->bi_sector); |
| 733 | else |
Jonathan E Brassow | a1a1908 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 734 | m = ms->default_mirror; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 736 | map_bio(ms, m, bio); |
| 737 | generic_make_request(bio); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /*----------------------------------------------------------------- |
| 742 | * Writes. |
| 743 | * |
| 744 | * We do different things with the write io depending on the |
| 745 | * state of the region that it's in: |
| 746 | * |
| 747 | * SYNC: increment pending, use kcopyd to write to *all* mirrors |
| 748 | * RECOVERING: delay the io until recovery completes |
| 749 | * NOSYNC: increment pending, just write to the default mirror |
| 750 | *---------------------------------------------------------------*/ |
| 751 | static void write_callback(unsigned long error, void *context) |
| 752 | { |
| 753 | unsigned int i; |
| 754 | int uptodate = 1; |
| 755 | struct bio *bio = (struct bio *) context; |
| 756 | struct mirror_set *ms; |
| 757 | |
| 758 | ms = bio_get_ms(bio); |
| 759 | bio_set_ms(bio, NULL); |
| 760 | |
| 761 | /* |
| 762 | * NOTE: We don't decrement the pending count here, |
| 763 | * instead it is done by the targets endio function. |
| 764 | * This way we handle both writes to SYNC and NOSYNC |
| 765 | * regions with the same code. |
| 766 | */ |
| 767 | |
| 768 | if (error) { |
| 769 | /* |
| 770 | * only error the io if all mirrors failed. |
| 771 | * FIXME: bogus |
| 772 | */ |
| 773 | uptodate = 0; |
| 774 | for (i = 0; i < ms->nr_mirrors; i++) |
| 775 | if (!test_bit(i, &error)) { |
| 776 | uptodate = 1; |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | bio_endio(bio, bio->bi_size, 0); |
| 781 | } |
| 782 | |
| 783 | static void do_write(struct mirror_set *ms, struct bio *bio) |
| 784 | { |
| 785 | unsigned int i; |
| 786 | struct io_region io[KCOPYD_MAX_REGIONS+1]; |
| 787 | struct mirror *m; |
| 788 | |
| 789 | for (i = 0; i < ms->nr_mirrors; i++) { |
| 790 | m = ms->mirror + i; |
| 791 | |
| 792 | io[i].bdev = m->dev->bdev; |
| 793 | io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin); |
| 794 | io[i].count = bio->bi_size >> 9; |
| 795 | } |
| 796 | |
| 797 | bio_set_ms(bio, ms); |
| 798 | dm_io_async_bvec(ms->nr_mirrors, io, WRITE, |
| 799 | bio->bi_io_vec + bio->bi_idx, |
| 800 | write_callback, bio); |
| 801 | } |
| 802 | |
| 803 | static void do_writes(struct mirror_set *ms, struct bio_list *writes) |
| 804 | { |
| 805 | int state; |
| 806 | struct bio *bio; |
| 807 | struct bio_list sync, nosync, recover, *this_list = NULL; |
| 808 | |
| 809 | if (!writes->head) |
| 810 | return; |
| 811 | |
| 812 | /* |
| 813 | * Classify each write. |
| 814 | */ |
| 815 | bio_list_init(&sync); |
| 816 | bio_list_init(&nosync); |
| 817 | bio_list_init(&recover); |
| 818 | |
| 819 | while ((bio = bio_list_pop(writes))) { |
| 820 | state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1); |
| 821 | switch (state) { |
| 822 | case RH_CLEAN: |
| 823 | case RH_DIRTY: |
| 824 | this_list = &sync; |
| 825 | break; |
| 826 | |
| 827 | case RH_NOSYNC: |
| 828 | this_list = &nosync; |
| 829 | break; |
| 830 | |
| 831 | case RH_RECOVERING: |
| 832 | this_list = &recover; |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | bio_list_add(this_list, bio); |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Increment the pending counts for any regions that will |
| 841 | * be written to (writes to recover regions are going to |
| 842 | * be delayed). |
| 843 | */ |
| 844 | rh_inc_pending(&ms->rh, &sync); |
| 845 | rh_inc_pending(&ms->rh, &nosync); |
| 846 | rh_flush(&ms->rh); |
| 847 | |
| 848 | /* |
| 849 | * Dispatch io. |
| 850 | */ |
| 851 | while ((bio = bio_list_pop(&sync))) |
| 852 | do_write(ms, bio); |
| 853 | |
| 854 | while ((bio = bio_list_pop(&recover))) |
| 855 | rh_delay(&ms->rh, bio); |
| 856 | |
| 857 | while ((bio = bio_list_pop(&nosync))) { |
Jonathan E Brassow | a1a1908 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 858 | map_bio(ms, ms->default_mirror, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | generic_make_request(bio); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /*----------------------------------------------------------------- |
| 864 | * kmirrord |
| 865 | *---------------------------------------------------------------*/ |
| 866 | static LIST_HEAD(_mirror_sets); |
| 867 | static DECLARE_RWSEM(_mirror_sets_lock); |
| 868 | |
| 869 | static void do_mirror(struct mirror_set *ms) |
| 870 | { |
| 871 | struct bio_list reads, writes; |
| 872 | |
| 873 | spin_lock(&ms->lock); |
| 874 | reads = ms->reads; |
| 875 | writes = ms->writes; |
| 876 | bio_list_init(&ms->reads); |
| 877 | bio_list_init(&ms->writes); |
| 878 | spin_unlock(&ms->lock); |
| 879 | |
| 880 | rh_update_states(&ms->rh); |
| 881 | do_recovery(ms); |
| 882 | do_reads(ms, &reads); |
| 883 | do_writes(ms, &writes); |
| 884 | } |
| 885 | |
| 886 | static void do_work(void *ignored) |
| 887 | { |
| 888 | struct mirror_set *ms; |
| 889 | |
| 890 | down_read(&_mirror_sets_lock); |
| 891 | list_for_each_entry (ms, &_mirror_sets, list) |
| 892 | do_mirror(ms); |
| 893 | up_read(&_mirror_sets_lock); |
| 894 | } |
| 895 | |
| 896 | /*----------------------------------------------------------------- |
| 897 | * Target functions |
| 898 | *---------------------------------------------------------------*/ |
| 899 | static struct mirror_set *alloc_context(unsigned int nr_mirrors, |
| 900 | uint32_t region_size, |
| 901 | struct dm_target *ti, |
| 902 | struct dirty_log *dl) |
| 903 | { |
| 904 | size_t len; |
| 905 | struct mirror_set *ms = NULL; |
| 906 | |
| 907 | if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors)) |
| 908 | return NULL; |
| 909 | |
| 910 | len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors); |
| 911 | |
| 912 | ms = kmalloc(len, GFP_KERNEL); |
| 913 | if (!ms) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 914 | ti->error = "Cannot allocate mirror context"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return NULL; |
| 916 | } |
| 917 | |
| 918 | memset(ms, 0, len); |
| 919 | spin_lock_init(&ms->lock); |
| 920 | |
| 921 | ms->ti = ti; |
| 922 | ms->nr_mirrors = nr_mirrors; |
| 923 | ms->nr_regions = dm_sector_div_up(ti->len, region_size); |
| 924 | ms->in_sync = 0; |
Jonathan E Brassow | a1a1908 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 925 | ms->default_mirror = &ms->mirror[DEFAULT_MIRROR]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
| 927 | if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 928 | ti->error = "Error creating dirty region hash"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | kfree(ms); |
| 930 | return NULL; |
| 931 | } |
| 932 | |
| 933 | return ms; |
| 934 | } |
| 935 | |
| 936 | static void free_context(struct mirror_set *ms, struct dm_target *ti, |
| 937 | unsigned int m) |
| 938 | { |
| 939 | while (m--) |
| 940 | dm_put_device(ti, ms->mirror[m].dev); |
| 941 | |
| 942 | rh_exit(&ms->rh); |
| 943 | kfree(ms); |
| 944 | } |
| 945 | |
| 946 | static inline int _check_region_size(struct dm_target *ti, uint32_t size) |
| 947 | { |
| 948 | return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) || |
| 949 | size > ti->len); |
| 950 | } |
| 951 | |
| 952 | static int get_mirror(struct mirror_set *ms, struct dm_target *ti, |
| 953 | unsigned int mirror, char **argv) |
| 954 | { |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 955 | unsigned long long offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 957 | if (sscanf(argv[1], "%llu", &offset) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 958 | ti->error = "Invalid offset"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | return -EINVAL; |
| 960 | } |
| 961 | |
| 962 | if (dm_get_device(ti, argv[0], offset, ti->len, |
| 963 | dm_table_get_mode(ti->table), |
| 964 | &ms->mirror[mirror].dev)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 965 | ti->error = "Device lookup failure"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return -ENXIO; |
| 967 | } |
| 968 | |
| 969 | ms->mirror[mirror].offset = offset; |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static int add_mirror_set(struct mirror_set *ms) |
| 975 | { |
| 976 | down_write(&_mirror_sets_lock); |
| 977 | list_add_tail(&ms->list, &_mirror_sets); |
| 978 | up_write(&_mirror_sets_lock); |
| 979 | wake(); |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static void del_mirror_set(struct mirror_set *ms) |
| 985 | { |
| 986 | down_write(&_mirror_sets_lock); |
| 987 | list_del(&ms->list); |
| 988 | up_write(&_mirror_sets_lock); |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Create dirty log: log_type #log_params <log_params> |
| 993 | */ |
| 994 | static struct dirty_log *create_dirty_log(struct dm_target *ti, |
| 995 | unsigned int argc, char **argv, |
| 996 | unsigned int *args_used) |
| 997 | { |
| 998 | unsigned int param_count; |
| 999 | struct dirty_log *dl; |
| 1000 | |
| 1001 | if (argc < 2) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1002 | ti->error = "Insufficient mirror log arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | return NULL; |
| 1004 | } |
| 1005 | |
| 1006 | if (sscanf(argv[1], "%u", ¶m_count) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1007 | ti->error = "Invalid mirror log argument count"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return NULL; |
| 1009 | } |
| 1010 | |
| 1011 | *args_used = 2 + param_count; |
| 1012 | |
| 1013 | if (argc < *args_used) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1014 | ti->error = "Insufficient mirror log arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | return NULL; |
| 1016 | } |
| 1017 | |
| 1018 | dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2); |
| 1019 | if (!dl) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1020 | ti->error = "Error creating mirror dirty log"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | if (!_check_region_size(ti, dl->type->get_region_size(dl))) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1025 | ti->error = "Invalid region size"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | dm_destroy_dirty_log(dl); |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | |
| 1030 | return dl; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * Construct a mirror mapping: |
| 1035 | * |
| 1036 | * log_type #log_params <log_params> |
| 1037 | * #mirrors [mirror_path offset]{2,} |
| 1038 | * |
| 1039 | * log_type is "core" or "disk" |
| 1040 | * #log_params is between 1 and 3 |
| 1041 | */ |
| 1042 | #define DM_IO_PAGES 64 |
| 1043 | static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1044 | { |
| 1045 | int r; |
| 1046 | unsigned int nr_mirrors, m, args_used; |
| 1047 | struct mirror_set *ms; |
| 1048 | struct dirty_log *dl; |
| 1049 | |
| 1050 | dl = create_dirty_log(ti, argc, argv, &args_used); |
| 1051 | if (!dl) |
| 1052 | return -EINVAL; |
| 1053 | |
| 1054 | argv += args_used; |
| 1055 | argc -= args_used; |
| 1056 | |
| 1057 | if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 || |
| 1058 | nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1059 | ti->error = "Invalid number of mirrors"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | dm_destroy_dirty_log(dl); |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | |
| 1064 | argv++, argc--; |
| 1065 | |
| 1066 | if (argc != nr_mirrors * 2) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1067 | ti->error = "Wrong number of mirror arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | dm_destroy_dirty_log(dl); |
| 1069 | return -EINVAL; |
| 1070 | } |
| 1071 | |
| 1072 | ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl); |
| 1073 | if (!ms) { |
| 1074 | dm_destroy_dirty_log(dl); |
| 1075 | return -ENOMEM; |
| 1076 | } |
| 1077 | |
| 1078 | /* Get the mirror parameter sets */ |
| 1079 | for (m = 0; m < nr_mirrors; m++) { |
| 1080 | r = get_mirror(ms, ti, m, argv); |
| 1081 | if (r) { |
| 1082 | free_context(ms, ti, m); |
| 1083 | return r; |
| 1084 | } |
| 1085 | argv += 2; |
| 1086 | argc -= 2; |
| 1087 | } |
| 1088 | |
| 1089 | ti->private = ms; |
Alasdair G Kergon | d88854f | 2005-07-07 17:59:34 -0700 | [diff] [blame] | 1090 | ti->split_io = ms->rh.region_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
| 1092 | r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client); |
| 1093 | if (r) { |
| 1094 | free_context(ms, ti, ms->nr_mirrors); |
| 1095 | return r; |
| 1096 | } |
| 1097 | |
| 1098 | add_mirror_set(ms); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static void mirror_dtr(struct dm_target *ti) |
| 1103 | { |
| 1104 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1105 | |
| 1106 | del_mirror_set(ms); |
| 1107 | kcopyd_client_destroy(ms->kcopyd_client); |
| 1108 | free_context(ms, ti, ms->nr_mirrors); |
| 1109 | } |
| 1110 | |
| 1111 | static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw) |
| 1112 | { |
| 1113 | int should_wake = 0; |
| 1114 | struct bio_list *bl; |
| 1115 | |
| 1116 | bl = (rw == WRITE) ? &ms->writes : &ms->reads; |
| 1117 | spin_lock(&ms->lock); |
| 1118 | should_wake = !(bl->head); |
| 1119 | bio_list_add(bl, bio); |
| 1120 | spin_unlock(&ms->lock); |
| 1121 | |
| 1122 | if (should_wake) |
| 1123 | wake(); |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * Mirror mapping function |
| 1128 | */ |
| 1129 | static int mirror_map(struct dm_target *ti, struct bio *bio, |
| 1130 | union map_info *map_context) |
| 1131 | { |
| 1132 | int r, rw = bio_rw(bio); |
| 1133 | struct mirror *m; |
| 1134 | struct mirror_set *ms = ti->private; |
| 1135 | |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 1136 | map_context->ll = bio_to_region(&ms->rh, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (rw == WRITE) { |
| 1139 | queue_bio(ms, bio, rw); |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | r = ms->rh.log->type->in_sync(ms->rh.log, |
| 1144 | bio_to_region(&ms->rh, bio), 0); |
| 1145 | if (r < 0 && r != -EWOULDBLOCK) |
| 1146 | return r; |
| 1147 | |
| 1148 | if (r == -EWOULDBLOCK) /* FIXME: ugly */ |
| 1149 | r = 0; |
| 1150 | |
| 1151 | /* |
| 1152 | * We don't want to fast track a recovery just for a read |
| 1153 | * ahead. So we just let it silently fail. |
| 1154 | * FIXME: get rid of this. |
| 1155 | */ |
| 1156 | if (!r && rw == READA) |
| 1157 | return -EIO; |
| 1158 | |
| 1159 | if (!r) { |
| 1160 | /* Pass this io over to the daemon */ |
| 1161 | queue_bio(ms, bio, rw); |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | m = choose_mirror(ms, bio->bi_sector); |
| 1166 | if (!m) |
| 1167 | return -EIO; |
| 1168 | |
| 1169 | map_bio(ms, m, bio); |
| 1170 | return 1; |
| 1171 | } |
| 1172 | |
| 1173 | static int mirror_end_io(struct dm_target *ti, struct bio *bio, |
| 1174 | int error, union map_info *map_context) |
| 1175 | { |
| 1176 | int rw = bio_rw(bio); |
| 1177 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1178 | region_t region = map_context->ll; |
| 1179 | |
| 1180 | /* |
| 1181 | * We need to dec pending if this was a write. |
| 1182 | */ |
| 1183 | if (rw == WRITE) |
| 1184 | rh_dec(&ms->rh, region); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static void mirror_postsuspend(struct dm_target *ti) |
| 1190 | { |
| 1191 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1192 | struct dirty_log *log = ms->rh.log; |
| 1193 | |
| 1194 | rh_stop_recovery(&ms->rh); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame^] | 1195 | |
| 1196 | /* Wait for all I/O we generated to complete */ |
| 1197 | wait_event(_kmirrord_recovery_stopped, |
| 1198 | !atomic_read(&ms->rh.recovery_in_flight)); |
| 1199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if (log->type->suspend && log->type->suspend(log)) |
| 1201 | /* FIXME: need better error handling */ |
| 1202 | DMWARN("log suspend failed"); |
| 1203 | } |
| 1204 | |
| 1205 | static void mirror_resume(struct dm_target *ti) |
| 1206 | { |
| 1207 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1208 | struct dirty_log *log = ms->rh.log; |
| 1209 | if (log->type->resume && log->type->resume(log)) |
| 1210 | /* FIXME: need better error handling */ |
| 1211 | DMWARN("log resume failed"); |
| 1212 | rh_start_recovery(&ms->rh); |
| 1213 | } |
| 1214 | |
| 1215 | static int mirror_status(struct dm_target *ti, status_type_t type, |
| 1216 | char *result, unsigned int maxlen) |
| 1217 | { |
| 1218 | unsigned int m, sz; |
| 1219 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1220 | |
| 1221 | sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen); |
| 1222 | |
| 1223 | switch (type) { |
| 1224 | case STATUSTYPE_INFO: |
| 1225 | DMEMIT("%d ", ms->nr_mirrors); |
| 1226 | for (m = 0; m < ms->nr_mirrors; m++) |
| 1227 | DMEMIT("%s ", ms->mirror[m].dev->name); |
| 1228 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1229 | DMEMIT("%llu/%llu", |
| 1230 | (unsigned long long)ms->rh.log->type-> |
| 1231 | get_sync_count(ms->rh.log), |
| 1232 | (unsigned long long)ms->nr_regions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | break; |
| 1234 | |
| 1235 | case STATUSTYPE_TABLE: |
Jonathan Brassow | e52b8f6 | 2006-10-03 01:15:32 -0700 | [diff] [blame] | 1236 | DMEMIT("%d", ms->nr_mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | for (m = 0; m < ms->nr_mirrors; m++) |
Jonathan Brassow | e52b8f6 | 2006-10-03 01:15:32 -0700 | [diff] [blame] | 1238 | DMEMIT(" %s %llu", ms->mirror[m].dev->name, |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1239 | (unsigned long long)ms->mirror[m].offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static struct target_type mirror_target = { |
| 1246 | .name = "mirror", |
Kevin Corry | 702ca6f | 2006-06-26 00:27:28 -0700 | [diff] [blame] | 1247 | .version = {1, 0, 2}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | .module = THIS_MODULE, |
| 1249 | .ctr = mirror_ctr, |
| 1250 | .dtr = mirror_dtr, |
| 1251 | .map = mirror_map, |
| 1252 | .end_io = mirror_end_io, |
| 1253 | .postsuspend = mirror_postsuspend, |
| 1254 | .resume = mirror_resume, |
| 1255 | .status = mirror_status, |
| 1256 | }; |
| 1257 | |
| 1258 | static int __init dm_mirror_init(void) |
| 1259 | { |
| 1260 | int r; |
| 1261 | |
| 1262 | r = dm_dirty_log_init(); |
| 1263 | if (r) |
| 1264 | return r; |
| 1265 | |
Alasdair G Kergon | 48f1f53 | 2005-08-04 12:53:37 -0700 | [diff] [blame] | 1266 | _kmirrord_wq = create_singlethread_workqueue("kmirrord"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | if (!_kmirrord_wq) { |
| 1268 | DMERR("couldn't start kmirrord"); |
| 1269 | dm_dirty_log_exit(); |
| 1270 | return r; |
| 1271 | } |
| 1272 | INIT_WORK(&_kmirrord_work, do_work, NULL); |
| 1273 | |
| 1274 | r = dm_register_target(&mirror_target); |
| 1275 | if (r < 0) { |
| 1276 | DMERR("%s: Failed to register mirror target", |
| 1277 | mirror_target.name); |
| 1278 | dm_dirty_log_exit(); |
| 1279 | destroy_workqueue(_kmirrord_wq); |
| 1280 | } |
| 1281 | |
| 1282 | return r; |
| 1283 | } |
| 1284 | |
| 1285 | static void __exit dm_mirror_exit(void) |
| 1286 | { |
| 1287 | int r; |
| 1288 | |
| 1289 | r = dm_unregister_target(&mirror_target); |
| 1290 | if (r < 0) |
| 1291 | DMERR("%s: unregister failed %d", mirror_target.name, r); |
| 1292 | |
| 1293 | destroy_workqueue(_kmirrord_wq); |
| 1294 | dm_dirty_log_exit(); |
| 1295 | } |
| 1296 | |
| 1297 | /* Module hooks */ |
| 1298 | module_init(dm_mirror_init); |
| 1299 | module_exit(dm_mirror_exit); |
| 1300 | |
| 1301 | MODULE_DESCRIPTION(DM_NAME " mirror target"); |
| 1302 | MODULE_AUTHOR("Joe Thornber"); |
| 1303 | MODULE_LICENSE("GPL"); |