dm-log.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 1
  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. struct io_region bits_location;
  130. uint32_t *disk_bits;
  131. };
  132. /*
  133. * The touched member needs to be updated every time we access
  134. * one of the bitsets.
  135. */
  136. static inline int log_test_bit(uint32_t *bs, unsigned bit)
  137. {
  138. return test_bit(bit, (unsigned long *) bs) ? 1 : 0;
  139. }
  140. static inline void log_set_bit(struct log_c *l,
  141. uint32_t *bs, unsigned bit)
  142. {
  143. set_bit(bit, (unsigned long *) bs);
  144. l->touched = 1;
  145. }
  146. static inline void log_clear_bit(struct log_c *l,
  147. uint32_t *bs, unsigned bit)
  148. {
  149. clear_bit(bit, (unsigned long *) bs);
  150. l->touched = 1;
  151. }
  152. /*----------------------------------------------------------------
  153. * Header IO
  154. *--------------------------------------------------------------*/
  155. static void header_to_disk(struct log_header *core, struct log_header *disk)
  156. {
  157. disk->magic = cpu_to_le32(core->magic);
  158. disk->version = cpu_to_le32(core->version);
  159. disk->nr_regions = cpu_to_le64(core->nr_regions);
  160. }
  161. static void header_from_disk(struct log_header *core, struct log_header *disk)
  162. {
  163. core->magic = le32_to_cpu(disk->magic);
  164. core->version = le32_to_cpu(disk->version);
  165. core->nr_regions = le64_to_cpu(disk->nr_regions);
  166. }
  167. static int read_header(struct log_c *log)
  168. {
  169. int r;
  170. unsigned long ebits;
  171. r = dm_io_sync_vm(1, &log->header_location, READ,
  172. log->disk_header, &ebits);
  173. if (r)
  174. return r;
  175. header_from_disk(&log->header, log->disk_header);
  176. /* New log required? */
  177. if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
  178. log->header.magic = MIRROR_MAGIC;
  179. log->header.version = MIRROR_DISK_VERSION;
  180. log->header.nr_regions = 0;
  181. }
  182. if (log->header.version != MIRROR_DISK_VERSION) {
  183. DMWARN("incompatible disk log version");
  184. return -EINVAL;
  185. }
  186. return 0;
  187. }
  188. static inline int write_header(struct log_c *log)
  189. {
  190. unsigned long ebits;
  191. header_to_disk(&log->header, log->disk_header);
  192. return dm_io_sync_vm(1, &log->header_location, WRITE,
  193. log->disk_header, &ebits);
  194. }
  195. /*----------------------------------------------------------------
  196. * Bits IO
  197. *--------------------------------------------------------------*/
  198. static inline void bits_to_core(uint32_t *core, uint32_t *disk, unsigned count)
  199. {
  200. unsigned i;
  201. for (i = 0; i < count; i++)
  202. core[i] = le32_to_cpu(disk[i]);
  203. }
  204. static inline void bits_to_disk(uint32_t *core, uint32_t *disk, unsigned count)
  205. {
  206. unsigned i;
  207. /* copy across the clean/dirty bitset */
  208. for (i = 0; i < count; i++)
  209. disk[i] = cpu_to_le32(core[i]);
  210. }
  211. static int read_bits(struct log_c *log)
  212. {
  213. int r;
  214. unsigned long ebits;
  215. r = dm_io_sync_vm(1, &log->bits_location, READ,
  216. log->disk_bits, &ebits);
  217. if (r)
  218. return r;
  219. bits_to_core(log->clean_bits, log->disk_bits,
  220. log->bitset_uint32_count);
  221. return 0;
  222. }
  223. static int write_bits(struct log_c *log)
  224. {
  225. unsigned long ebits;
  226. bits_to_disk(log->clean_bits, log->disk_bits,
  227. log->bitset_uint32_count);
  228. return dm_io_sync_vm(1, &log->bits_location, WRITE,
  229. log->disk_bits, &ebits);
  230. }
  231. /*----------------------------------------------------------------
  232. * core log constructor/destructor
  233. *
  234. * argv contains region_size followed optionally by [no]sync
  235. *--------------------------------------------------------------*/
  236. #define BYTE_SHIFT 3
  237. static int core_ctr(struct dirty_log *log, struct dm_target *ti,
  238. unsigned int argc, char **argv)
  239. {
  240. enum sync sync = DEFAULTSYNC;
  241. struct log_c *lc;
  242. uint32_t region_size;
  243. unsigned int region_count;
  244. size_t bitset_size;
  245. if (argc < 1 || argc > 2) {
  246. DMWARN("wrong number of arguments to mirror log");
  247. return -EINVAL;
  248. }
  249. if (argc > 1) {
  250. if (!strcmp(argv[1], "sync"))
  251. sync = FORCESYNC;
  252. else if (!strcmp(argv[1], "nosync"))
  253. sync = NOSYNC;
  254. else {
  255. DMWARN("unrecognised sync argument to mirror log: %s",
  256. argv[1]);
  257. return -EINVAL;
  258. }
  259. }
  260. if (sscanf(argv[0], "%u", &region_size) != 1) {
  261. DMWARN("invalid region size string");
  262. return -EINVAL;
  263. }
  264. region_count = dm_sector_div_up(ti->len, region_size);
  265. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  266. if (!lc) {
  267. DMWARN("couldn't allocate core log");
  268. return -ENOMEM;
  269. }
  270. lc->ti = ti;
  271. lc->touched = 0;
  272. lc->region_size = region_size;
  273. lc->region_count = region_count;
  274. lc->sync = sync;
  275. /*
  276. * Work out how many words we need to hold the bitset.
  277. */
  278. bitset_size = dm_round_up(region_count,
  279. sizeof(*lc->clean_bits) << BYTE_SHIFT);
  280. bitset_size >>= BYTE_SHIFT;
  281. lc->bitset_uint32_count = bitset_size / 4;
  282. lc->clean_bits = vmalloc(bitset_size);
  283. if (!lc->clean_bits) {
  284. DMWARN("couldn't allocate clean bitset");
  285. kfree(lc);
  286. return -ENOMEM;
  287. }
  288. memset(lc->clean_bits, -1, bitset_size);
  289. lc->sync_bits = vmalloc(bitset_size);
  290. if (!lc->sync_bits) {
  291. DMWARN("couldn't allocate sync bitset");
  292. vfree(lc->clean_bits);
  293. kfree(lc);
  294. return -ENOMEM;
  295. }
  296. memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
  297. lc->sync_count = (sync == NOSYNC) ? region_count : 0;
  298. lc->recovering_bits = vmalloc(bitset_size);
  299. if (!lc->recovering_bits) {
  300. DMWARN("couldn't allocate sync bitset");
  301. vfree(lc->sync_bits);
  302. vfree(lc->clean_bits);
  303. kfree(lc);
  304. return -ENOMEM;
  305. }
  306. memset(lc->recovering_bits, 0, bitset_size);
  307. lc->sync_search = 0;
  308. log->context = lc;
  309. return 0;
  310. }
  311. static void core_dtr(struct dirty_log *log)
  312. {
  313. struct log_c *lc = (struct log_c *) log->context;
  314. vfree(lc->clean_bits);
  315. vfree(lc->sync_bits);
  316. vfree(lc->recovering_bits);
  317. kfree(lc);
  318. }
  319. /*----------------------------------------------------------------
  320. * disk log constructor/destructor
  321. *
  322. * argv contains log_device region_size followed optionally by [no]sync
  323. *--------------------------------------------------------------*/
  324. static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
  325. unsigned int argc, char **argv)
  326. {
  327. int r;
  328. size_t size;
  329. struct log_c *lc;
  330. struct dm_dev *dev;
  331. if (argc < 2 || argc > 3) {
  332. DMWARN("wrong number of arguments to disk mirror log");
  333. return -EINVAL;
  334. }
  335. r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
  336. FMODE_READ | FMODE_WRITE, &dev);
  337. if (r)
  338. return r;
  339. r = core_ctr(log, ti, argc - 1, argv + 1);
  340. if (r) {
  341. dm_put_device(ti, dev);
  342. return r;
  343. }
  344. lc = (struct log_c *) log->context;
  345. lc->log_dev = dev;
  346. /* setup the disk header fields */
  347. lc->header_location.bdev = lc->log_dev->bdev;
  348. lc->header_location.sector = 0;
  349. lc->header_location.count = 1;
  350. /*
  351. * We can't read less than this amount, even though we'll
  352. * not be using most of this space.
  353. */
  354. lc->disk_header = vmalloc(1 << SECTOR_SHIFT);
  355. if (!lc->disk_header)
  356. goto bad;
  357. /* setup the disk bitset fields */
  358. lc->bits_location.bdev = lc->log_dev->bdev;
  359. lc->bits_location.sector = LOG_OFFSET;
  360. size = dm_round_up(lc->bitset_uint32_count * sizeof(uint32_t),
  361. 1 << SECTOR_SHIFT);
  362. lc->bits_location.count = size >> SECTOR_SHIFT;
  363. lc->disk_bits = vmalloc(size);
  364. if (!lc->disk_bits) {
  365. vfree(lc->disk_header);
  366. goto bad;
  367. }
  368. return 0;
  369. bad:
  370. dm_put_device(ti, lc->log_dev);
  371. core_dtr(log);
  372. return -ENOMEM;
  373. }
  374. static void disk_dtr(struct dirty_log *log)
  375. {
  376. struct log_c *lc = (struct log_c *) log->context;
  377. dm_put_device(lc->ti, lc->log_dev);
  378. vfree(lc->disk_header);
  379. vfree(lc->disk_bits);
  380. core_dtr(log);
  381. }
  382. static int count_bits32(uint32_t *addr, unsigned size)
  383. {
  384. int count = 0, i;
  385. for (i = 0; i < size; i++) {
  386. count += hweight32(*(addr+i));
  387. }
  388. return count;
  389. }
  390. static int disk_resume(struct dirty_log *log)
  391. {
  392. int r;
  393. unsigned i;
  394. struct log_c *lc = (struct log_c *) log->context;
  395. size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
  396. /* read the disk header */
  397. r = read_header(lc);
  398. if (r)
  399. return r;
  400. /* read the bits */
  401. r = read_bits(lc);
  402. if (r)
  403. return r;
  404. /* set or clear any new bits */
  405. if (lc->sync == NOSYNC)
  406. for (i = lc->header.nr_regions; i < lc->region_count; i++)
  407. /* FIXME: amazingly inefficient */
  408. log_set_bit(lc, lc->clean_bits, i);
  409. else
  410. for (i = lc->header.nr_regions; i < lc->region_count; i++)
  411. /* FIXME: amazingly inefficient */
  412. log_clear_bit(lc, lc->clean_bits, i);
  413. /* copy clean across to sync */
  414. memcpy(lc->sync_bits, lc->clean_bits, size);
  415. lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
  416. /* write the bits */
  417. r = write_bits(lc);
  418. if (r)
  419. return r;
  420. /* set the correct number of regions in the header */
  421. lc->header.nr_regions = lc->region_count;
  422. /* write the new header */
  423. return write_header(lc);
  424. }
  425. static uint32_t core_get_region_size(struct dirty_log *log)
  426. {
  427. struct log_c *lc = (struct log_c *) log->context;
  428. return lc->region_size;
  429. }
  430. static int core_is_clean(struct dirty_log *log, region_t region)
  431. {
  432. struct log_c *lc = (struct log_c *) log->context;
  433. return log_test_bit(lc->clean_bits, region);
  434. }
  435. static int core_in_sync(struct dirty_log *log, region_t region, int block)
  436. {
  437. struct log_c *lc = (struct log_c *) log->context;
  438. return log_test_bit(lc->sync_bits, region);
  439. }
  440. static int core_flush(struct dirty_log *log)
  441. {
  442. /* no op */
  443. return 0;
  444. }
  445. static int disk_flush(struct dirty_log *log)
  446. {
  447. int r;
  448. struct log_c *lc = (struct log_c *) log->context;
  449. /* only write if the log has changed */
  450. if (!lc->touched)
  451. return 0;
  452. r = write_bits(lc);
  453. if (!r)
  454. lc->touched = 0;
  455. return r;
  456. }
  457. static void core_mark_region(struct dirty_log *log, region_t region)
  458. {
  459. struct log_c *lc = (struct log_c *) log->context;
  460. log_clear_bit(lc, lc->clean_bits, region);
  461. }
  462. static void core_clear_region(struct dirty_log *log, region_t region)
  463. {
  464. struct log_c *lc = (struct log_c *) log->context;
  465. log_set_bit(lc, lc->clean_bits, region);
  466. }
  467. static int core_get_resync_work(struct dirty_log *log, region_t *region)
  468. {
  469. struct log_c *lc = (struct log_c *) log->context;
  470. if (lc->sync_search >= lc->region_count)
  471. return 0;
  472. do {
  473. *region = find_next_zero_bit((unsigned long *) lc->sync_bits,
  474. lc->region_count,
  475. lc->sync_search);
  476. lc->sync_search = *region + 1;
  477. if (*region == lc->region_count)
  478. return 0;
  479. } while (log_test_bit(lc->recovering_bits, *region));
  480. log_set_bit(lc, lc->recovering_bits, *region);
  481. return 1;
  482. }
  483. static void core_complete_resync_work(struct dirty_log *log, region_t region,
  484. int success)
  485. {
  486. struct log_c *lc = (struct log_c *) log->context;
  487. log_clear_bit(lc, lc->recovering_bits, region);
  488. if (success) {
  489. log_set_bit(lc, lc->sync_bits, region);
  490. lc->sync_count++;
  491. }
  492. }
  493. static region_t core_get_sync_count(struct dirty_log *log)
  494. {
  495. struct log_c *lc = (struct log_c *) log->context;
  496. return lc->sync_count;
  497. }
  498. #define DMEMIT_SYNC \
  499. if (lc->sync != DEFAULTSYNC) \
  500. DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
  501. static int core_status(struct dirty_log *log, status_type_t status,
  502. char *result, unsigned int maxlen)
  503. {
  504. int sz = 0;
  505. struct log_c *lc = log->context;
  506. switch(status) {
  507. case STATUSTYPE_INFO:
  508. break;
  509. case STATUSTYPE_TABLE:
  510. DMEMIT("%s %u %u ", log->type->name,
  511. lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
  512. DMEMIT_SYNC;
  513. }
  514. return sz;
  515. }
  516. static int disk_status(struct dirty_log *log, status_type_t status,
  517. char *result, unsigned int maxlen)
  518. {
  519. int sz = 0;
  520. char buffer[16];
  521. struct log_c *lc = log->context;
  522. switch(status) {
  523. case STATUSTYPE_INFO:
  524. break;
  525. case STATUSTYPE_TABLE:
  526. format_dev_t(buffer, lc->log_dev->bdev->bd_dev);
  527. DMEMIT("%s %u %s %u ", log->type->name,
  528. lc->sync == DEFAULTSYNC ? 2 : 3, buffer,
  529. lc->region_size);
  530. DMEMIT_SYNC;
  531. }
  532. return sz;
  533. }
  534. static struct dirty_log_type _core_type = {
  535. .name = "core",
  536. .module = THIS_MODULE,
  537. .ctr = core_ctr,
  538. .dtr = core_dtr,
  539. .get_region_size = core_get_region_size,
  540. .is_clean = core_is_clean,
  541. .in_sync = core_in_sync,
  542. .flush = core_flush,
  543. .mark_region = core_mark_region,
  544. .clear_region = core_clear_region,
  545. .get_resync_work = core_get_resync_work,
  546. .complete_resync_work = core_complete_resync_work,
  547. .get_sync_count = core_get_sync_count,
  548. .status = core_status,
  549. };
  550. static struct dirty_log_type _disk_type = {
  551. .name = "disk",
  552. .module = THIS_MODULE,
  553. .ctr = disk_ctr,
  554. .dtr = disk_dtr,
  555. .suspend = disk_flush,
  556. .resume = disk_resume,
  557. .get_region_size = core_get_region_size,
  558. .is_clean = core_is_clean,
  559. .in_sync = core_in_sync,
  560. .flush = disk_flush,
  561. .mark_region = core_mark_region,
  562. .clear_region = core_clear_region,
  563. .get_resync_work = core_get_resync_work,
  564. .complete_resync_work = core_complete_resync_work,
  565. .get_sync_count = core_get_sync_count,
  566. .status = disk_status,
  567. };
  568. int __init dm_dirty_log_init(void)
  569. {
  570. int r;
  571. r = dm_register_dirty_log_type(&_core_type);
  572. if (r)
  573. DMWARN("couldn't register core log");
  574. r = dm_register_dirty_log_type(&_disk_type);
  575. if (r) {
  576. DMWARN("couldn't register disk type");
  577. dm_unregister_dirty_log_type(&_core_type);
  578. }
  579. return r;
  580. }
  581. void dm_dirty_log_exit(void)
  582. {
  583. dm_unregister_dirty_log_type(&_disk_type);
  584. dm_unregister_dirty_log_type(&_core_type);
  585. }
  586. EXPORT_SYMBOL(dm_register_dirty_log_type);
  587. EXPORT_SYMBOL(dm_unregister_dirty_log_type);
  588. EXPORT_SYMBOL(dm_create_dirty_log);
  589. EXPORT_SYMBOL(dm_destroy_dirty_log);