dm-log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. /* set the correct number of regions in the header */
  390. lc->header.nr_regions = lc->region_count;
  391. /* write the new header */
  392. return write_header(lc);
  393. }
  394. static uint32_t core_get_region_size(struct dirty_log *log)
  395. {
  396. struct log_c *lc = (struct log_c *) log->context;
  397. return lc->region_size;
  398. }
  399. static int core_is_clean(struct dirty_log *log, region_t region)
  400. {
  401. struct log_c *lc = (struct log_c *) log->context;
  402. return log_test_bit(lc->clean_bits, region);
  403. }
  404. static int core_in_sync(struct dirty_log *log, region_t region, int block)
  405. {
  406. struct log_c *lc = (struct log_c *) log->context;
  407. return log_test_bit(lc->sync_bits, region);
  408. }
  409. static int core_flush(struct dirty_log *log)
  410. {
  411. /* no op */
  412. return 0;
  413. }
  414. static int disk_flush(struct dirty_log *log)
  415. {
  416. int r;
  417. struct log_c *lc = (struct log_c *) log->context;
  418. /* only write if the log has changed */
  419. if (!lc->touched)
  420. return 0;
  421. r = write_header(lc);
  422. if (!r)
  423. lc->touched = 0;
  424. return r;
  425. }
  426. static void core_mark_region(struct dirty_log *log, region_t region)
  427. {
  428. struct log_c *lc = (struct log_c *) log->context;
  429. log_clear_bit(lc, lc->clean_bits, region);
  430. }
  431. static void core_clear_region(struct dirty_log *log, region_t region)
  432. {
  433. struct log_c *lc = (struct log_c *) log->context;
  434. log_set_bit(lc, lc->clean_bits, region);
  435. }
  436. static int core_get_resync_work(struct dirty_log *log, region_t *region)
  437. {
  438. struct log_c *lc = (struct log_c *) log->context;
  439. if (lc->sync_search >= lc->region_count)
  440. return 0;
  441. do {
  442. *region = ext2_find_next_zero_bit(
  443. (unsigned long *) lc->sync_bits,
  444. lc->region_count,
  445. lc->sync_search);
  446. lc->sync_search = *region + 1;
  447. if (*region >= lc->region_count)
  448. return 0;
  449. } while (log_test_bit(lc->recovering_bits, *region));
  450. log_set_bit(lc, lc->recovering_bits, *region);
  451. return 1;
  452. }
  453. static void core_complete_resync_work(struct dirty_log *log, region_t region,
  454. int success)
  455. {
  456. struct log_c *lc = (struct log_c *) log->context;
  457. log_clear_bit(lc, lc->recovering_bits, region);
  458. if (success) {
  459. log_set_bit(lc, lc->sync_bits, region);
  460. lc->sync_count++;
  461. }
  462. }
  463. static region_t core_get_sync_count(struct dirty_log *log)
  464. {
  465. struct log_c *lc = (struct log_c *) log->context;
  466. return lc->sync_count;
  467. }
  468. #define DMEMIT_SYNC \
  469. if (lc->sync != DEFAULTSYNC) \
  470. DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
  471. static int core_status(struct dirty_log *log, status_type_t status,
  472. char *result, unsigned int maxlen)
  473. {
  474. int sz = 0;
  475. struct log_c *lc = log->context;
  476. switch(status) {
  477. case STATUSTYPE_INFO:
  478. break;
  479. case STATUSTYPE_TABLE:
  480. DMEMIT("%s %u %u ", log->type->name,
  481. lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
  482. DMEMIT_SYNC;
  483. }
  484. return sz;
  485. }
  486. static int disk_status(struct dirty_log *log, status_type_t status,
  487. char *result, unsigned int maxlen)
  488. {
  489. int sz = 0;
  490. char buffer[16];
  491. struct log_c *lc = log->context;
  492. switch(status) {
  493. case STATUSTYPE_INFO:
  494. break;
  495. case STATUSTYPE_TABLE:
  496. format_dev_t(buffer, lc->log_dev->bdev->bd_dev);
  497. DMEMIT("%s %u %s %u ", log->type->name,
  498. lc->sync == DEFAULTSYNC ? 2 : 3, buffer,
  499. lc->region_size);
  500. DMEMIT_SYNC;
  501. }
  502. return sz;
  503. }
  504. static struct dirty_log_type _core_type = {
  505. .name = "core",
  506. .module = THIS_MODULE,
  507. .ctr = core_ctr,
  508. .dtr = core_dtr,
  509. .get_region_size = core_get_region_size,
  510. .is_clean = core_is_clean,
  511. .in_sync = core_in_sync,
  512. .flush = core_flush,
  513. .mark_region = core_mark_region,
  514. .clear_region = core_clear_region,
  515. .get_resync_work = core_get_resync_work,
  516. .complete_resync_work = core_complete_resync_work,
  517. .get_sync_count = core_get_sync_count,
  518. .status = core_status,
  519. };
  520. static struct dirty_log_type _disk_type = {
  521. .name = "disk",
  522. .module = THIS_MODULE,
  523. .ctr = disk_ctr,
  524. .dtr = disk_dtr,
  525. .suspend = disk_flush,
  526. .resume = disk_resume,
  527. .get_region_size = core_get_region_size,
  528. .is_clean = core_is_clean,
  529. .in_sync = core_in_sync,
  530. .flush = disk_flush,
  531. .mark_region = core_mark_region,
  532. .clear_region = core_clear_region,
  533. .get_resync_work = core_get_resync_work,
  534. .complete_resync_work = core_complete_resync_work,
  535. .get_sync_count = core_get_sync_count,
  536. .status = disk_status,
  537. };
  538. int __init dm_dirty_log_init(void)
  539. {
  540. int r;
  541. r = dm_register_dirty_log_type(&_core_type);
  542. if (r)
  543. DMWARN("couldn't register core log");
  544. r = dm_register_dirty_log_type(&_disk_type);
  545. if (r) {
  546. DMWARN("couldn't register disk type");
  547. dm_unregister_dirty_log_type(&_core_type);
  548. }
  549. return r;
  550. }
  551. void dm_dirty_log_exit(void)
  552. {
  553. dm_unregister_dirty_log_type(&_disk_type);
  554. dm_unregister_dirty_log_type(&_core_type);
  555. }
  556. EXPORT_SYMBOL(dm_register_dirty_log_type);
  557. EXPORT_SYMBOL(dm_unregister_dirty_log_type);
  558. EXPORT_SYMBOL(dm_create_dirty_log);
  559. EXPORT_SYMBOL(dm_destroy_dirty_log);