dm-log.c 15 KB

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