dm-log.c 16 KB

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