dm-log.c 15 KB

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