Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Sistina Software |
| 3 | * |
| 4 | * This file is released under the LGPL. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/vmalloc.h> |
| 11 | |
| 12 | #include "dm-log.h" |
| 13 | #include "dm-io.h" |
| 14 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 15 | #define DM_MSG_PREFIX "mirror log" |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | static LIST_HEAD(_log_types); |
| 18 | static DEFINE_SPINLOCK(_lock); |
| 19 | |
| 20 | int dm_register_dirty_log_type(struct dirty_log_type *type) |
| 21 | { |
| 22 | spin_lock(&_lock); |
| 23 | type->use_count = 0; |
| 24 | list_add(&type->list, &_log_types); |
| 25 | spin_unlock(&_lock); |
| 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | int dm_unregister_dirty_log_type(struct dirty_log_type *type) |
| 31 | { |
| 32 | spin_lock(&_lock); |
| 33 | |
| 34 | if (type->use_count) |
| 35 | DMWARN("Attempt to unregister a log type that is still in use"); |
| 36 | else |
| 37 | list_del(&type->list); |
| 38 | |
| 39 | spin_unlock(&_lock); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static struct dirty_log_type *get_type(const char *type_name) |
| 45 | { |
| 46 | struct dirty_log_type *type; |
| 47 | |
| 48 | spin_lock(&_lock); |
| 49 | list_for_each_entry (type, &_log_types, list) |
| 50 | if (!strcmp(type_name, type->name)) { |
| 51 | if (!type->use_count && !try_module_get(type->module)){ |
| 52 | spin_unlock(&_lock); |
| 53 | return NULL; |
| 54 | } |
| 55 | type->use_count++; |
| 56 | spin_unlock(&_lock); |
| 57 | return type; |
| 58 | } |
| 59 | |
| 60 | spin_unlock(&_lock); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static void put_type(struct dirty_log_type *type) |
| 65 | { |
| 66 | spin_lock(&_lock); |
| 67 | if (!--type->use_count) |
| 68 | module_put(type->module); |
| 69 | spin_unlock(&_lock); |
| 70 | } |
| 71 | |
| 72 | struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti, |
| 73 | unsigned int argc, char **argv) |
| 74 | { |
| 75 | struct dirty_log_type *type; |
| 76 | struct dirty_log *log; |
| 77 | |
| 78 | log = kmalloc(sizeof(*log), GFP_KERNEL); |
| 79 | if (!log) |
| 80 | return NULL; |
| 81 | |
| 82 | type = get_type(type_name); |
| 83 | if (!type) { |
| 84 | kfree(log); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | log->type = type; |
| 89 | if (type->ctr(log, ti, argc, argv)) { |
| 90 | kfree(log); |
| 91 | put_type(type); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | return log; |
| 96 | } |
| 97 | |
| 98 | void dm_destroy_dirty_log(struct dirty_log *log) |
| 99 | { |
| 100 | log->type->dtr(log); |
| 101 | put_type(log->type); |
| 102 | kfree(log); |
| 103 | } |
| 104 | |
| 105 | /*----------------------------------------------------------------- |
| 106 | * Persistent and core logs share a lot of their implementation. |
| 107 | * FIXME: need a reload method to be called from a resume |
| 108 | *---------------------------------------------------------------*/ |
| 109 | /* |
| 110 | * Magic for persistent mirrors: "MiRr" |
| 111 | */ |
| 112 | #define MIRROR_MAGIC 0x4D695272 |
| 113 | |
| 114 | /* |
| 115 | * The on-disk version of the metadata. |
| 116 | */ |
Patrick Caulfield | a4fc471 | 2006-02-01 03:04:51 -0800 | [diff] [blame] | 117 | #define MIRROR_DISK_VERSION 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | #define LOG_OFFSET 2 |
| 119 | |
| 120 | struct log_header { |
| 121 | uint32_t magic; |
| 122 | |
| 123 | /* |
| 124 | * Simple, incrementing version. no backward |
| 125 | * compatibility. |
| 126 | */ |
| 127 | uint32_t version; |
| 128 | sector_t nr_regions; |
| 129 | }; |
| 130 | |
| 131 | struct log_c { |
| 132 | struct dm_target *ti; |
| 133 | int touched; |
| 134 | uint32_t region_size; |
| 135 | unsigned int region_count; |
| 136 | region_t sync_count; |
| 137 | |
| 138 | unsigned bitset_uint32_count; |
| 139 | uint32_t *clean_bits; |
| 140 | uint32_t *sync_bits; |
| 141 | uint32_t *recovering_bits; /* FIXME: this seems excessive */ |
| 142 | |
| 143 | int sync_search; |
| 144 | |
| 145 | /* Resync flag */ |
| 146 | enum sync { |
| 147 | DEFAULTSYNC, /* Synchronize if necessary */ |
| 148 | NOSYNC, /* Devices known to be already in sync */ |
| 149 | FORCESYNC, /* Force a sync to happen */ |
| 150 | } sync; |
| 151 | |
| 152 | /* |
| 153 | * Disk log fields |
| 154 | */ |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 155 | int log_dev_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | struct dm_dev *log_dev; |
| 157 | struct log_header header; |
| 158 | |
| 159 | struct io_region header_location; |
| 160 | struct log_header *disk_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /* |
| 164 | * The touched member needs to be updated every time we access |
| 165 | * one of the bitsets. |
| 166 | */ |
| 167 | static inline int log_test_bit(uint32_t *bs, unsigned bit) |
| 168 | { |
Patrick Caulfield | a4fc471 | 2006-02-01 03:04:51 -0800 | [diff] [blame] | 169 | return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static inline void log_set_bit(struct log_c *l, |
| 173 | uint32_t *bs, unsigned bit) |
| 174 | { |
Patrick Caulfield | a4fc471 | 2006-02-01 03:04:51 -0800 | [diff] [blame] | 175 | ext2_set_bit(bit, (unsigned long *) bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | l->touched = 1; |
| 177 | } |
| 178 | |
| 179 | static inline void log_clear_bit(struct log_c *l, |
| 180 | uint32_t *bs, unsigned bit) |
| 181 | { |
Patrick Caulfield | a4fc471 | 2006-02-01 03:04:51 -0800 | [diff] [blame] | 182 | ext2_clear_bit(bit, (unsigned long *) bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | l->touched = 1; |
| 184 | } |
| 185 | |
| 186 | /*---------------------------------------------------------------- |
| 187 | * Header IO |
| 188 | *--------------------------------------------------------------*/ |
| 189 | static void header_to_disk(struct log_header *core, struct log_header *disk) |
| 190 | { |
| 191 | disk->magic = cpu_to_le32(core->magic); |
| 192 | disk->version = cpu_to_le32(core->version); |
| 193 | disk->nr_regions = cpu_to_le64(core->nr_regions); |
| 194 | } |
| 195 | |
| 196 | static void header_from_disk(struct log_header *core, struct log_header *disk) |
| 197 | { |
| 198 | core->magic = le32_to_cpu(disk->magic); |
| 199 | core->version = le32_to_cpu(disk->version); |
| 200 | core->nr_regions = le64_to_cpu(disk->nr_regions); |
| 201 | } |
| 202 | |
| 203 | static int read_header(struct log_c *log) |
| 204 | { |
| 205 | int r; |
| 206 | unsigned long ebits; |
| 207 | |
| 208 | r = dm_io_sync_vm(1, &log->header_location, READ, |
| 209 | log->disk_header, &ebits); |
| 210 | if (r) |
| 211 | return r; |
| 212 | |
| 213 | header_from_disk(&log->header, log->disk_header); |
| 214 | |
| 215 | /* New log required? */ |
| 216 | if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) { |
| 217 | log->header.magic = MIRROR_MAGIC; |
| 218 | log->header.version = MIRROR_DISK_VERSION; |
| 219 | log->header.nr_regions = 0; |
| 220 | } |
| 221 | |
Patrick Caulfield | a4fc471 | 2006-02-01 03:04:51 -0800 | [diff] [blame] | 222 | #ifdef __LITTLE_ENDIAN |
| 223 | if (log->header.version == 1) |
| 224 | log->header.version = 2; |
| 225 | #endif |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (log->header.version != MIRROR_DISK_VERSION) { |
| 228 | DMWARN("incompatible disk log version"); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static inline int write_header(struct log_c *log) |
| 236 | { |
| 237 | unsigned long ebits; |
| 238 | |
| 239 | header_to_disk(&log->header, log->disk_header); |
| 240 | return dm_io_sync_vm(1, &log->header_location, WRITE, |
| 241 | log->disk_header, &ebits); |
| 242 | } |
| 243 | |
| 244 | /*---------------------------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | * core log constructor/destructor |
| 246 | * |
| 247 | * argv contains region_size followed optionally by [no]sync |
| 248 | *--------------------------------------------------------------*/ |
| 249 | #define BYTE_SHIFT 3 |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 250 | static int create_log_context(struct dirty_log *log, struct dm_target *ti, |
| 251 | unsigned int argc, char **argv, |
| 252 | struct dm_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | enum sync sync = DEFAULTSYNC; |
| 255 | |
| 256 | struct log_c *lc; |
| 257 | uint32_t region_size; |
| 258 | unsigned int region_count; |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 259 | size_t bitset_size, buf_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | if (argc < 1 || argc > 2) { |
| 262 | DMWARN("wrong number of arguments to mirror log"); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | |
| 266 | if (argc > 1) { |
| 267 | if (!strcmp(argv[1], "sync")) |
| 268 | sync = FORCESYNC; |
| 269 | else if (!strcmp(argv[1], "nosync")) |
| 270 | sync = NOSYNC; |
| 271 | else { |
| 272 | DMWARN("unrecognised sync argument to mirror log: %s", |
| 273 | argv[1]); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (sscanf(argv[0], "%u", ®ion_size) != 1) { |
| 279 | DMWARN("invalid region size string"); |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | region_count = dm_sector_div_up(ti->len, region_size); |
| 284 | |
| 285 | lc = kmalloc(sizeof(*lc), GFP_KERNEL); |
| 286 | if (!lc) { |
| 287 | DMWARN("couldn't allocate core log"); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | |
| 291 | lc->ti = ti; |
| 292 | lc->touched = 0; |
| 293 | lc->region_size = region_size; |
| 294 | lc->region_count = region_count; |
| 295 | lc->sync = sync; |
| 296 | |
| 297 | /* |
Alasdair G Kergon | 0e56822 | 2005-11-21 21:32:34 -0800 | [diff] [blame] | 298 | * Work out how many "unsigned long"s we need to hold the bitset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | */ |
| 300 | bitset_size = dm_round_up(region_count, |
Alasdair G Kergon | 29121bd | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 301 | sizeof(*lc->clean_bits) << BYTE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | bitset_size >>= BYTE_SHIFT; |
| 303 | |
Alasdair G Kergon | 29121bd | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 304 | lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits); |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Disk log? |
| 308 | */ |
| 309 | if (!dev) { |
| 310 | lc->clean_bits = vmalloc(bitset_size); |
| 311 | if (!lc->clean_bits) { |
| 312 | DMWARN("couldn't allocate clean bitset"); |
| 313 | kfree(lc); |
| 314 | return -ENOMEM; |
| 315 | } |
| 316 | lc->disk_header = NULL; |
| 317 | } else { |
| 318 | lc->log_dev = dev; |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 319 | lc->log_dev_failed = 0; |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 320 | lc->header_location.bdev = lc->log_dev->bdev; |
| 321 | lc->header_location.sector = 0; |
| 322 | |
| 323 | /* |
| 324 | * Buffer holds both header and bitset. |
| 325 | */ |
| 326 | buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) + |
| 327 | bitset_size, ti->limits.hardsect_size); |
| 328 | lc->header_location.count = buf_size >> SECTOR_SHIFT; |
| 329 | |
| 330 | lc->disk_header = vmalloc(buf_size); |
| 331 | if (!lc->disk_header) { |
| 332 | DMWARN("couldn't allocate disk log buffer"); |
| 333 | kfree(lc); |
| 334 | return -ENOMEM; |
| 335 | } |
| 336 | |
| 337 | lc->clean_bits = (void *)lc->disk_header + |
| 338 | (LOG_OFFSET << SECTOR_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | memset(lc->clean_bits, -1, bitset_size); |
| 342 | |
| 343 | lc->sync_bits = vmalloc(bitset_size); |
| 344 | if (!lc->sync_bits) { |
| 345 | DMWARN("couldn't allocate sync bitset"); |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 346 | if (!dev) |
| 347 | vfree(lc->clean_bits); |
| 348 | vfree(lc->disk_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | kfree(lc); |
| 350 | return -ENOMEM; |
| 351 | } |
| 352 | memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size); |
| 353 | lc->sync_count = (sync == NOSYNC) ? region_count : 0; |
| 354 | |
| 355 | lc->recovering_bits = vmalloc(bitset_size); |
| 356 | if (!lc->recovering_bits) { |
| 357 | DMWARN("couldn't allocate sync bitset"); |
| 358 | vfree(lc->sync_bits); |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 359 | if (!dev) |
| 360 | vfree(lc->clean_bits); |
| 361 | vfree(lc->disk_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | kfree(lc); |
| 363 | return -ENOMEM; |
| 364 | } |
| 365 | memset(lc->recovering_bits, 0, bitset_size); |
| 366 | lc->sync_search = 0; |
| 367 | log->context = lc; |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 372 | static int core_ctr(struct dirty_log *log, struct dm_target *ti, |
| 373 | unsigned int argc, char **argv) |
| 374 | { |
| 375 | return create_log_context(log, ti, argc, argv, NULL); |
| 376 | } |
| 377 | |
| 378 | static void destroy_log_context(struct log_c *lc) |
| 379 | { |
| 380 | vfree(lc->sync_bits); |
| 381 | vfree(lc->recovering_bits); |
| 382 | kfree(lc); |
| 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | static void core_dtr(struct dirty_log *log) |
| 386 | { |
| 387 | struct log_c *lc = (struct log_c *) log->context; |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | vfree(lc->clean_bits); |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 390 | destroy_log_context(lc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /*---------------------------------------------------------------- |
| 394 | * disk log constructor/destructor |
| 395 | * |
| 396 | * argv contains log_device region_size followed optionally by [no]sync |
| 397 | *--------------------------------------------------------------*/ |
| 398 | static int disk_ctr(struct dirty_log *log, struct dm_target *ti, |
| 399 | unsigned int argc, char **argv) |
| 400 | { |
| 401 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | struct dm_dev *dev; |
| 403 | |
| 404 | if (argc < 2 || argc > 3) { |
| 405 | DMWARN("wrong number of arguments to disk mirror log"); |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */, |
| 410 | FMODE_READ | FMODE_WRITE, &dev); |
| 411 | if (r) |
| 412 | return r; |
| 413 | |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 414 | r = create_log_context(log, ti, argc - 1, argv + 1, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (r) { |
| 416 | dm_put_device(ti, dev); |
| 417 | return r; |
| 418 | } |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void disk_dtr(struct dirty_log *log) |
| 424 | { |
| 425 | struct log_c *lc = (struct log_c *) log->context; |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | dm_put_device(lc->ti, lc->log_dev); |
| 428 | vfree(lc->disk_header); |
Alasdair G Kergon | b7cca19 | 2006-06-26 00:27:29 -0700 | [diff] [blame] | 429 | destroy_log_context(lc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int count_bits32(uint32_t *addr, unsigned size) |
| 433 | { |
| 434 | int count = 0, i; |
| 435 | |
| 436 | for (i = 0; i < size; i++) { |
| 437 | count += hweight32(*(addr+i)); |
| 438 | } |
| 439 | return count; |
| 440 | } |
| 441 | |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 442 | static void fail_log_device(struct log_c *lc) |
| 443 | { |
| 444 | if (lc->log_dev_failed) |
| 445 | return; |
| 446 | |
| 447 | lc->log_dev_failed = 1; |
| 448 | dm_table_event(lc->ti->table); |
| 449 | } |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | static int disk_resume(struct dirty_log *log) |
| 452 | { |
| 453 | int r; |
| 454 | unsigned i; |
| 455 | struct log_c *lc = (struct log_c *) log->context; |
| 456 | size_t size = lc->bitset_uint32_count * sizeof(uint32_t); |
| 457 | |
| 458 | /* read the disk header */ |
| 459 | r = read_header(lc); |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 460 | if (r) { |
| 461 | DMWARN("%s: Failed to read header on mirror log device", |
| 462 | lc->log_dev->name); |
| 463 | fail_log_device(lc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return r; |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Alasdair G Kergon | 8a835f1 | 2006-06-26 00:27:30 -0700 | [diff] [blame] | 467 | /* set or clear any new bits -- device has grown */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (lc->sync == NOSYNC) |
| 469 | for (i = lc->header.nr_regions; i < lc->region_count; i++) |
| 470 | /* FIXME: amazingly inefficient */ |
| 471 | log_set_bit(lc, lc->clean_bits, i); |
| 472 | else |
| 473 | for (i = lc->header.nr_regions; i < lc->region_count; i++) |
| 474 | /* FIXME: amazingly inefficient */ |
| 475 | log_clear_bit(lc, lc->clean_bits, i); |
| 476 | |
Alasdair G Kergon | 8a835f1 | 2006-06-26 00:27:30 -0700 | [diff] [blame] | 477 | /* clear any old bits -- device has shrunk */ |
| 478 | for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++) |
| 479 | log_clear_bit(lc, lc->clean_bits, i); |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | /* copy clean across to sync */ |
| 482 | memcpy(lc->sync_bits, lc->clean_bits, size); |
| 483 | lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count); |
Jonathan E Brassow | 88b20a1 | 2006-12-08 02:41:12 -0800 | [diff] [blame] | 484 | lc->sync_search = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | /* set the correct number of regions in the header */ |
| 487 | lc->header.nr_regions = lc->region_count; |
| 488 | |
| 489 | /* write the new header */ |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 490 | r = write_header(lc); |
| 491 | if (r) { |
| 492 | DMWARN("%s: Failed to write header on mirror log device", |
| 493 | lc->log_dev->name); |
| 494 | fail_log_device(lc); |
| 495 | } |
| 496 | |
| 497 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static uint32_t core_get_region_size(struct dirty_log *log) |
| 501 | { |
| 502 | struct log_c *lc = (struct log_c *) log->context; |
| 503 | return lc->region_size; |
| 504 | } |
| 505 | |
Jonathan E Brassow | 88b20a1 | 2006-12-08 02:41:12 -0800 | [diff] [blame] | 506 | static int core_resume(struct dirty_log *log) |
| 507 | { |
| 508 | struct log_c *lc = (struct log_c *) log->context; |
| 509 | lc->sync_search = 0; |
| 510 | return 0; |
| 511 | } |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | static int core_is_clean(struct dirty_log *log, region_t region) |
| 514 | { |
| 515 | struct log_c *lc = (struct log_c *) log->context; |
| 516 | return log_test_bit(lc->clean_bits, region); |
| 517 | } |
| 518 | |
| 519 | static int core_in_sync(struct dirty_log *log, region_t region, int block) |
| 520 | { |
| 521 | struct log_c *lc = (struct log_c *) log->context; |
| 522 | return log_test_bit(lc->sync_bits, region); |
| 523 | } |
| 524 | |
| 525 | static int core_flush(struct dirty_log *log) |
| 526 | { |
| 527 | /* no op */ |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int disk_flush(struct dirty_log *log) |
| 532 | { |
| 533 | int r; |
| 534 | struct log_c *lc = (struct log_c *) log->context; |
| 535 | |
| 536 | /* only write if the log has changed */ |
| 537 | if (!lc->touched) |
| 538 | return 0; |
| 539 | |
Kevin Corry | 702ca6f | 2006-06-26 00:27:28 -0700 | [diff] [blame] | 540 | r = write_header(lc); |
Jonathan E Brassow | 01d03a6 | 2007-05-09 02:32:57 -0700 | [diff] [blame^] | 541 | if (r) |
| 542 | fail_log_device(lc); |
| 543 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | lc->touched = 0; |
| 545 | |
| 546 | return r; |
| 547 | } |
| 548 | |
| 549 | static void core_mark_region(struct dirty_log *log, region_t region) |
| 550 | { |
| 551 | struct log_c *lc = (struct log_c *) log->context; |
| 552 | log_clear_bit(lc, lc->clean_bits, region); |
| 553 | } |
| 554 | |
| 555 | static void core_clear_region(struct dirty_log *log, region_t region) |
| 556 | { |
| 557 | struct log_c *lc = (struct log_c *) log->context; |
| 558 | log_set_bit(lc, lc->clean_bits, region); |
| 559 | } |
| 560 | |
| 561 | static int core_get_resync_work(struct dirty_log *log, region_t *region) |
| 562 | { |
| 563 | struct log_c *lc = (struct log_c *) log->context; |
| 564 | |
| 565 | if (lc->sync_search >= lc->region_count) |
| 566 | return 0; |
| 567 | |
| 568 | do { |
Stefan Bader | 1113a7e | 2006-02-02 14:28:07 -0800 | [diff] [blame] | 569 | *region = ext2_find_next_zero_bit( |
| 570 | (unsigned long *) lc->sync_bits, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | lc->region_count, |
| 572 | lc->sync_search); |
| 573 | lc->sync_search = *region + 1; |
| 574 | |
Darrick J. Wong | ac81b2e | 2006-01-06 00:20:11 -0800 | [diff] [blame] | 575 | if (*region >= lc->region_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | |
| 578 | } while (log_test_bit(lc->recovering_bits, *region)); |
| 579 | |
| 580 | log_set_bit(lc, lc->recovering_bits, *region); |
| 581 | return 1; |
| 582 | } |
| 583 | |
Jonathan E Brassow | f3ee6b2 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 584 | static void core_set_region_sync(struct dirty_log *log, region_t region, |
| 585 | int in_sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
| 587 | struct log_c *lc = (struct log_c *) log->context; |
| 588 | |
| 589 | log_clear_bit(lc, lc->recovering_bits, region); |
Jonathan E Brassow | f3ee6b2 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 590 | if (in_sync) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | log_set_bit(lc, lc->sync_bits, region); |
| 592 | lc->sync_count++; |
Jonathan E Brassow | f3ee6b2 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 593 | } else if (log_test_bit(lc->sync_bits, region)) { |
| 594 | lc->sync_count--; |
| 595 | log_clear_bit(lc, lc->sync_bits, region); |
| 596 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static region_t core_get_sync_count(struct dirty_log *log) |
| 600 | { |
| 601 | struct log_c *lc = (struct log_c *) log->context; |
| 602 | |
| 603 | return lc->sync_count; |
| 604 | } |
| 605 | |
| 606 | #define DMEMIT_SYNC \ |
| 607 | if (lc->sync != DEFAULTSYNC) \ |
| 608 | DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "") |
| 609 | |
| 610 | static int core_status(struct dirty_log *log, status_type_t status, |
| 611 | char *result, unsigned int maxlen) |
| 612 | { |
| 613 | int sz = 0; |
| 614 | struct log_c *lc = log->context; |
| 615 | |
| 616 | switch(status) { |
| 617 | case STATUSTYPE_INFO: |
| 618 | break; |
| 619 | |
| 620 | case STATUSTYPE_TABLE: |
| 621 | DMEMIT("%s %u %u ", log->type->name, |
| 622 | lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size); |
| 623 | DMEMIT_SYNC; |
| 624 | } |
| 625 | |
| 626 | return sz; |
| 627 | } |
| 628 | |
| 629 | static int disk_status(struct dirty_log *log, status_type_t status, |
| 630 | char *result, unsigned int maxlen) |
| 631 | { |
| 632 | int sz = 0; |
| 633 | char buffer[16]; |
| 634 | struct log_c *lc = log->context; |
| 635 | |
| 636 | switch(status) { |
| 637 | case STATUSTYPE_INFO: |
| 638 | break; |
| 639 | |
| 640 | case STATUSTYPE_TABLE: |
| 641 | format_dev_t(buffer, lc->log_dev->bdev->bd_dev); |
| 642 | DMEMIT("%s %u %s %u ", log->type->name, |
| 643 | lc->sync == DEFAULTSYNC ? 2 : 3, buffer, |
| 644 | lc->region_size); |
| 645 | DMEMIT_SYNC; |
| 646 | } |
| 647 | |
| 648 | return sz; |
| 649 | } |
| 650 | |
| 651 | static struct dirty_log_type _core_type = { |
| 652 | .name = "core", |
| 653 | .module = THIS_MODULE, |
| 654 | .ctr = core_ctr, |
| 655 | .dtr = core_dtr, |
Jonathan E Brassow | 88b20a1 | 2006-12-08 02:41:12 -0800 | [diff] [blame] | 656 | .resume = core_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | .get_region_size = core_get_region_size, |
| 658 | .is_clean = core_is_clean, |
| 659 | .in_sync = core_in_sync, |
| 660 | .flush = core_flush, |
| 661 | .mark_region = core_mark_region, |
| 662 | .clear_region = core_clear_region, |
| 663 | .get_resync_work = core_get_resync_work, |
Jonathan E Brassow | f3ee6b2 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 664 | .set_region_sync = core_set_region_sync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | .get_sync_count = core_get_sync_count, |
| 666 | .status = core_status, |
| 667 | }; |
| 668 | |
| 669 | static struct dirty_log_type _disk_type = { |
| 670 | .name = "disk", |
| 671 | .module = THIS_MODULE, |
| 672 | .ctr = disk_ctr, |
| 673 | .dtr = disk_dtr, |
| 674 | .suspend = disk_flush, |
| 675 | .resume = disk_resume, |
| 676 | .get_region_size = core_get_region_size, |
| 677 | .is_clean = core_is_clean, |
| 678 | .in_sync = core_in_sync, |
| 679 | .flush = disk_flush, |
| 680 | .mark_region = core_mark_region, |
| 681 | .clear_region = core_clear_region, |
| 682 | .get_resync_work = core_get_resync_work, |
Jonathan E Brassow | f3ee6b2 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 683 | .set_region_sync = core_set_region_sync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | .get_sync_count = core_get_sync_count, |
| 685 | .status = disk_status, |
| 686 | }; |
| 687 | |
| 688 | int __init dm_dirty_log_init(void) |
| 689 | { |
| 690 | int r; |
| 691 | |
| 692 | r = dm_register_dirty_log_type(&_core_type); |
| 693 | if (r) |
| 694 | DMWARN("couldn't register core log"); |
| 695 | |
| 696 | r = dm_register_dirty_log_type(&_disk_type); |
| 697 | if (r) { |
| 698 | DMWARN("couldn't register disk type"); |
| 699 | dm_unregister_dirty_log_type(&_core_type); |
| 700 | } |
| 701 | |
| 702 | return r; |
| 703 | } |
| 704 | |
| 705 | void dm_dirty_log_exit(void) |
| 706 | { |
| 707 | dm_unregister_dirty_log_type(&_disk_type); |
| 708 | dm_unregister_dirty_log_type(&_core_type); |
| 709 | } |
| 710 | |
| 711 | EXPORT_SYMBOL(dm_register_dirty_log_type); |
| 712 | EXPORT_SYMBOL(dm_unregister_dirty_log_type); |
| 713 | EXPORT_SYMBOL(dm_create_dirty_log); |
| 714 | EXPORT_SYMBOL(dm_destroy_dirty_log); |