dm-log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Copyright (C) 2003 Sistina Software
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the LGPL.
  6. */
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/dm-io.h>
  12. #include <linux/dm-dirty-log.h>
  13. #include "dm.h"
  14. #define DM_MSG_PREFIX "dirty region log"
  15. struct dm_dirty_log_internal {
  16. struct dm_dirty_log_type *type;
  17. struct list_head list;
  18. long use;
  19. };
  20. static LIST_HEAD(_log_types);
  21. static DEFINE_SPINLOCK(_lock);
  22. static struct dm_dirty_log_internal *__find_dirty_log_type(const char *name)
  23. {
  24. struct dm_dirty_log_internal *log_type;
  25. list_for_each_entry(log_type, &_log_types, list)
  26. if (!strcmp(name, log_type->type->name))
  27. return log_type;
  28. return NULL;
  29. }
  30. static struct dm_dirty_log_internal *_get_dirty_log_type(const char *name)
  31. {
  32. struct dm_dirty_log_internal *log_type;
  33. spin_lock(&_lock);
  34. log_type = __find_dirty_log_type(name);
  35. if (log_type) {
  36. if (!log_type->use && !try_module_get(log_type->type->module))
  37. log_type = NULL;
  38. else
  39. log_type->use++;
  40. }
  41. spin_unlock(&_lock);
  42. return log_type;
  43. }
  44. /*
  45. * get_type
  46. * @type_name
  47. *
  48. * Attempt to retrieve the dm_dirty_log_type by name. If not already
  49. * available, attempt to load the appropriate module.
  50. *
  51. * Log modules are named "dm-log-" followed by the 'type_name'.
  52. * Modules may contain multiple types.
  53. * This function will first try the module "dm-log-<type_name>",
  54. * then truncate 'type_name' on the last '-' and try again.
  55. *
  56. * For example, if type_name was "clustered-disk", it would search
  57. * 'dm-log-clustered-disk' then 'dm-log-clustered'.
  58. *
  59. * Returns: dirty_log_type* on success, NULL on failure
  60. */
  61. static struct dm_dirty_log_type *get_type(const char *type_name)
  62. {
  63. char *p, *type_name_dup;
  64. struct dm_dirty_log_internal *log_type;
  65. if (!type_name)
  66. return NULL;
  67. log_type = _get_dirty_log_type(type_name);
  68. if (log_type)
  69. return log_type->type;
  70. type_name_dup = kstrdup(type_name, GFP_KERNEL);
  71. if (!type_name_dup) {
  72. DMWARN("No memory left to attempt log module load for \"%s\"",
  73. type_name);
  74. return NULL;
  75. }
  76. while (request_module("dm-log-%s", type_name_dup) ||
  77. !(log_type = _get_dirty_log_type(type_name))) {
  78. p = strrchr(type_name_dup, '-');
  79. if (!p)
  80. break;
  81. p[0] = '\0';
  82. }
  83. if (!log_type)
  84. DMWARN("Module for logging type \"%s\" not found.", type_name);
  85. kfree(type_name_dup);
  86. return log_type ? log_type->type : NULL;
  87. }
  88. static void put_type(struct dm_dirty_log_type *type)
  89. {
  90. struct dm_dirty_log_internal *log_type;
  91. if (!type)
  92. return;
  93. spin_lock(&_lock);
  94. log_type = __find_dirty_log_type(type->name);
  95. if (!log_type)
  96. goto out;
  97. if (!--log_type->use)
  98. module_put(type->module);
  99. BUG_ON(log_type->use < 0);
  100. out:
  101. spin_unlock(&_lock);
  102. }
  103. static struct dm_dirty_log_internal *_alloc_dirty_log_type(struct dm_dirty_log_type *type)
  104. {
  105. struct dm_dirty_log_internal *log_type = kzalloc(sizeof(*log_type),
  106. GFP_KERNEL);
  107. if (log_type)
  108. log_type->type = type;
  109. return log_type;
  110. }
  111. int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
  112. {
  113. struct dm_dirty_log_internal *log_type = _alloc_dirty_log_type(type);
  114. int r = 0;
  115. if (!log_type)
  116. return -ENOMEM;
  117. spin_lock(&_lock);
  118. if (!__find_dirty_log_type(type->name))
  119. list_add(&log_type->list, &_log_types);
  120. else {
  121. kfree(log_type);
  122. r = -EEXIST;
  123. }
  124. spin_unlock(&_lock);
  125. return r;
  126. }
  127. EXPORT_SYMBOL(dm_dirty_log_type_register);
  128. int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
  129. {
  130. struct dm_dirty_log_internal *log_type;
  131. spin_lock(&_lock);
  132. log_type = __find_dirty_log_type(type->name);
  133. if (!log_type) {
  134. spin_unlock(&_lock);
  135. return -EINVAL;
  136. }
  137. if (log_type->use) {
  138. spin_unlock(&_lock);
  139. return -ETXTBSY;
  140. }
  141. list_del(&log_type->list);
  142. spin_unlock(&_lock);
  143. kfree(log_type);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(dm_dirty_log_type_unregister);
  147. struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
  148. struct dm_target *ti,
  149. unsigned int argc, char **argv)
  150. {
  151. struct dm_dirty_log_type *type;
  152. struct dm_dirty_log *log;
  153. log = kmalloc(sizeof(*log), GFP_KERNEL);
  154. if (!log)
  155. return NULL;
  156. type = get_type(type_name);
  157. if (!type) {
  158. kfree(log);
  159. return NULL;
  160. }
  161. log->type = type;
  162. if (type->ctr(log, ti, argc, argv)) {
  163. kfree(log);
  164. put_type(type);
  165. return NULL;
  166. }
  167. return log;
  168. }
  169. EXPORT_SYMBOL(dm_dirty_log_create);
  170. void dm_dirty_log_destroy(struct dm_dirty_log *log)
  171. {
  172. log->type->dtr(log);
  173. put_type(log->type);
  174. kfree(log);
  175. }
  176. EXPORT_SYMBOL(dm_dirty_log_destroy);
  177. /*-----------------------------------------------------------------
  178. * Persistent and core logs share a lot of their implementation.
  179. * FIXME: need a reload method to be called from a resume
  180. *---------------------------------------------------------------*/
  181. /*
  182. * Magic for persistent mirrors: "MiRr"
  183. */
  184. #define MIRROR_MAGIC 0x4D695272
  185. /*
  186. * The on-disk version of the metadata.
  187. */
  188. #define MIRROR_DISK_VERSION 2
  189. #define LOG_OFFSET 2
  190. struct log_header {
  191. uint32_t magic;
  192. /*
  193. * Simple, incrementing version. no backward
  194. * compatibility.
  195. */
  196. uint32_t version;
  197. sector_t nr_regions;
  198. };
  199. struct log_c {
  200. struct dm_target *ti;
  201. int touched;
  202. uint32_t region_size;
  203. unsigned int region_count;
  204. region_t sync_count;
  205. unsigned bitset_uint32_count;
  206. uint32_t *clean_bits;
  207. uint32_t *sync_bits;
  208. uint32_t *recovering_bits; /* FIXME: this seems excessive */
  209. int sync_search;
  210. /* Resync flag */
  211. enum sync {
  212. DEFAULTSYNC, /* Synchronize if necessary */
  213. NOSYNC, /* Devices known to be already in sync */
  214. FORCESYNC, /* Force a sync to happen */
  215. } sync;
  216. struct dm_io_request io_req;
  217. /*
  218. * Disk log fields
  219. */
  220. int log_dev_failed;
  221. struct dm_dev *log_dev;
  222. struct log_header header;
  223. struct dm_io_region header_location;
  224. struct log_header *disk_header;
  225. };
  226. /*
  227. * The touched member needs to be updated every time we access
  228. * one of the bitsets.
  229. */
  230. static inline int log_test_bit(uint32_t *bs, unsigned bit)
  231. {
  232. return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
  233. }
  234. static inline void log_set_bit(struct log_c *l,
  235. uint32_t *bs, unsigned bit)
  236. {
  237. ext2_set_bit(bit, (unsigned long *) bs);
  238. l->touched = 1;
  239. }
  240. static inline void log_clear_bit(struct log_c *l,
  241. uint32_t *bs, unsigned bit)
  242. {
  243. ext2_clear_bit(bit, (unsigned long *) bs);
  244. l->touched = 1;
  245. }
  246. /*----------------------------------------------------------------
  247. * Header IO
  248. *--------------------------------------------------------------*/
  249. static void header_to_disk(struct log_header *core, struct log_header *disk)
  250. {
  251. disk->magic = cpu_to_le32(core->magic);
  252. disk->version = cpu_to_le32(core->version);
  253. disk->nr_regions = cpu_to_le64(core->nr_regions);
  254. }
  255. static void header_from_disk(struct log_header *core, struct log_header *disk)
  256. {
  257. core->magic = le32_to_cpu(disk->magic);
  258. core->version = le32_to_cpu(disk->version);
  259. core->nr_regions = le64_to_cpu(disk->nr_regions);
  260. }
  261. static int rw_header(struct log_c *lc, int rw)
  262. {
  263. lc->io_req.bi_rw = rw;
  264. lc->io_req.mem.ptr.vma = lc->disk_header;
  265. lc->io_req.notify.fn = NULL;
  266. return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
  267. }
  268. static int read_header(struct log_c *log)
  269. {
  270. int r;
  271. r = rw_header(log, READ);
  272. if (r)
  273. return r;
  274. header_from_disk(&log->header, log->disk_header);
  275. /* New log required? */
  276. if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
  277. log->header.magic = MIRROR_MAGIC;
  278. log->header.version = MIRROR_DISK_VERSION;
  279. log->header.nr_regions = 0;
  280. }
  281. #ifdef __LITTLE_ENDIAN
  282. if (log->header.version == 1)
  283. log->header.version = 2;
  284. #endif
  285. if (log->header.version != MIRROR_DISK_VERSION) {
  286. DMWARN("incompatible disk log version");
  287. return -EINVAL;
  288. }
  289. return 0;
  290. }
  291. static inline int write_header(struct log_c *log)
  292. {
  293. header_to_disk(&log->header, log->disk_header);
  294. return rw_header(log, WRITE);
  295. }
  296. /*----------------------------------------------------------------
  297. * core log constructor/destructor
  298. *
  299. * argv contains region_size followed optionally by [no]sync
  300. *--------------------------------------------------------------*/
  301. #define BYTE_SHIFT 3
  302. static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
  303. unsigned int argc, char **argv,
  304. struct dm_dev *dev)
  305. {
  306. enum sync sync = DEFAULTSYNC;
  307. struct log_c *lc;
  308. uint32_t region_size;
  309. unsigned int region_count;
  310. size_t bitset_size, buf_size;
  311. int r;
  312. if (argc < 1 || argc > 2) {
  313. DMWARN("wrong number of arguments to dirty region log");
  314. return -EINVAL;
  315. }
  316. if (argc > 1) {
  317. if (!strcmp(argv[1], "sync"))
  318. sync = FORCESYNC;
  319. else if (!strcmp(argv[1], "nosync"))
  320. sync = NOSYNC;
  321. else {
  322. DMWARN("unrecognised sync argument to "
  323. "dirty region log: %s", argv[1]);
  324. return -EINVAL;
  325. }
  326. }
  327. if (sscanf(argv[0], "%u", &region_size) != 1) {
  328. DMWARN("invalid region size string");
  329. return -EINVAL;
  330. }
  331. region_count = dm_sector_div_up(ti->len, region_size);
  332. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  333. if (!lc) {
  334. DMWARN("couldn't allocate core log");
  335. return -ENOMEM;
  336. }
  337. lc->ti = ti;
  338. lc->touched = 0;
  339. lc->region_size = region_size;
  340. lc->region_count = region_count;
  341. lc->sync = sync;
  342. /*
  343. * Work out how many "unsigned long"s we need to hold the bitset.
  344. */
  345. bitset_size = dm_round_up(region_count,
  346. sizeof(*lc->clean_bits) << BYTE_SHIFT);
  347. bitset_size >>= BYTE_SHIFT;
  348. lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
  349. /*
  350. * Disk log?
  351. */
  352. if (!dev) {
  353. lc->clean_bits = vmalloc(bitset_size);
  354. if (!lc->clean_bits) {
  355. DMWARN("couldn't allocate clean bitset");
  356. kfree(lc);
  357. return -ENOMEM;
  358. }
  359. lc->disk_header = NULL;
  360. } else {
  361. lc->log_dev = dev;
  362. lc->log_dev_failed = 0;
  363. lc->header_location.bdev = lc->log_dev->bdev;
  364. lc->header_location.sector = 0;
  365. /*
  366. * Buffer holds both header and bitset.
  367. */
  368. buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
  369. bitset_size, ti->limits.hardsect_size);
  370. lc->header_location.count = buf_size >> SECTOR_SHIFT;
  371. lc->io_req.mem.type = DM_IO_VMA;
  372. lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
  373. PAGE_SIZE));
  374. if (IS_ERR(lc->io_req.client)) {
  375. r = PTR_ERR(lc->io_req.client);
  376. DMWARN("couldn't allocate disk io client");
  377. kfree(lc);
  378. return -ENOMEM;
  379. }
  380. lc->disk_header = vmalloc(buf_size);
  381. if (!lc->disk_header) {
  382. DMWARN("couldn't allocate disk log buffer");
  383. kfree(lc);
  384. return -ENOMEM;
  385. }
  386. lc->clean_bits = (void *)lc->disk_header +
  387. (LOG_OFFSET << SECTOR_SHIFT);
  388. }
  389. memset(lc->clean_bits, -1, bitset_size);
  390. lc->sync_bits = vmalloc(bitset_size);
  391. if (!lc->sync_bits) {
  392. DMWARN("couldn't allocate sync bitset");
  393. if (!dev)
  394. vfree(lc->clean_bits);
  395. vfree(lc->disk_header);
  396. kfree(lc);
  397. return -ENOMEM;
  398. }
  399. memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
  400. lc->sync_count = (sync == NOSYNC) ? region_count : 0;
  401. lc->recovering_bits = vmalloc(bitset_size);
  402. if (!lc->recovering_bits) {
  403. DMWARN("couldn't allocate sync bitset");
  404. vfree(lc->sync_bits);
  405. if (!dev)
  406. vfree(lc->clean_bits);
  407. vfree(lc->disk_header);
  408. kfree(lc);
  409. return -ENOMEM;
  410. }
  411. memset(lc->recovering_bits, 0, bitset_size);
  412. lc->sync_search = 0;
  413. log->context = lc;
  414. return 0;
  415. }
  416. static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
  417. unsigned int argc, char **argv)
  418. {
  419. return create_log_context(log, ti, argc, argv, NULL);
  420. }
  421. static void destroy_log_context(struct log_c *lc)
  422. {
  423. vfree(lc->sync_bits);
  424. vfree(lc->recovering_bits);
  425. kfree(lc);
  426. }
  427. static void core_dtr(struct dm_dirty_log *log)
  428. {
  429. struct log_c *lc = (struct log_c *) log->context;
  430. vfree(lc->clean_bits);
  431. destroy_log_context(lc);
  432. }
  433. /*----------------------------------------------------------------
  434. * disk log constructor/destructor
  435. *
  436. * argv contains log_device region_size followed optionally by [no]sync
  437. *--------------------------------------------------------------*/
  438. static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
  439. unsigned int argc, char **argv)
  440. {
  441. int r;
  442. struct dm_dev *dev;
  443. if (argc < 2 || argc > 3) {
  444. DMWARN("wrong number of arguments to disk dirty region log");
  445. return -EINVAL;
  446. }
  447. r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
  448. FMODE_READ | FMODE_WRITE, &dev);
  449. if (r)
  450. return r;
  451. r = create_log_context(log, ti, argc - 1, argv + 1, dev);
  452. if (r) {
  453. dm_put_device(ti, dev);
  454. return r;
  455. }
  456. return 0;
  457. }
  458. static void disk_dtr(struct dm_dirty_log *log)
  459. {
  460. struct log_c *lc = (struct log_c *) log->context;
  461. dm_put_device(lc->ti, lc->log_dev);
  462. vfree(lc->disk_header);
  463. dm_io_client_destroy(lc->io_req.client);
  464. destroy_log_context(lc);
  465. }
  466. static int count_bits32(uint32_t *addr, unsigned size)
  467. {
  468. int count = 0, i;
  469. for (i = 0; i < size; i++) {
  470. count += hweight32(*(addr+i));
  471. }
  472. return count;
  473. }
  474. static void fail_log_device(struct log_c *lc)
  475. {
  476. if (lc->log_dev_failed)
  477. return;
  478. lc->log_dev_failed = 1;
  479. dm_table_event(lc->ti->table);
  480. }
  481. static int disk_resume(struct dm_dirty_log *log)
  482. {
  483. int r;
  484. unsigned i;
  485. struct log_c *lc = (struct log_c *) log->context;
  486. size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
  487. /* read the disk header */
  488. r = read_header(lc);
  489. if (r) {
  490. DMWARN("%s: Failed to read header on dirty region log device",
  491. lc->log_dev->name);
  492. fail_log_device(lc);
  493. /*
  494. * If the log device cannot be read, we must assume
  495. * all regions are out-of-sync. If we simply return
  496. * here, the state will be uninitialized and could
  497. * lead us to return 'in-sync' status for regions
  498. * that are actually 'out-of-sync'.
  499. */
  500. lc->header.nr_regions = 0;
  501. }
  502. /* set or clear any new bits -- device has grown */
  503. if (lc->sync == NOSYNC)
  504. for (i = lc->header.nr_regions; i < lc->region_count; i++)
  505. /* FIXME: amazingly inefficient */
  506. log_set_bit(lc, lc->clean_bits, i);
  507. else
  508. for (i = lc->header.nr_regions; i < lc->region_count; i++)
  509. /* FIXME: amazingly inefficient */
  510. log_clear_bit(lc, lc->clean_bits, i);
  511. /* clear any old bits -- device has shrunk */
  512. for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
  513. log_clear_bit(lc, lc->clean_bits, i);
  514. /* copy clean across to sync */
  515. memcpy(lc->sync_bits, lc->clean_bits, size);
  516. lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
  517. lc->sync_search = 0;
  518. /* set the correct number of regions in the header */
  519. lc->header.nr_regions = lc->region_count;
  520. /* write the new header */
  521. r = write_header(lc);
  522. if (r) {
  523. DMWARN("%s: Failed to write header on dirty region log device",
  524. lc->log_dev->name);
  525. fail_log_device(lc);
  526. }
  527. return r;
  528. }
  529. static uint32_t core_get_region_size(struct dm_dirty_log *log)
  530. {
  531. struct log_c *lc = (struct log_c *) log->context;
  532. return lc->region_size;
  533. }
  534. static int core_resume(struct dm_dirty_log *log)
  535. {
  536. struct log_c *lc = (struct log_c *) log->context;
  537. lc->sync_search = 0;
  538. return 0;
  539. }
  540. static int core_is_clean(struct dm_dirty_log *log, region_t region)
  541. {
  542. struct log_c *lc = (struct log_c *) log->context;
  543. return log_test_bit(lc->clean_bits, region);
  544. }
  545. static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
  546. {
  547. struct log_c *lc = (struct log_c *) log->context;
  548. return log_test_bit(lc->sync_bits, region);
  549. }
  550. static int core_flush(struct dm_dirty_log *log)
  551. {
  552. /* no op */
  553. return 0;
  554. }
  555. static int disk_flush(struct dm_dirty_log *log)
  556. {
  557. int r;
  558. struct log_c *lc = (struct log_c *) log->context;
  559. /* only write if the log has changed */
  560. if (!lc->touched)
  561. return 0;
  562. r = write_header(lc);
  563. if (r)
  564. fail_log_device(lc);
  565. else
  566. lc->touched = 0;
  567. return r;
  568. }
  569. static void core_mark_region(struct dm_dirty_log *log, region_t region)
  570. {
  571. struct log_c *lc = (struct log_c *) log->context;
  572. log_clear_bit(lc, lc->clean_bits, region);
  573. }
  574. static void core_clear_region(struct dm_dirty_log *log, region_t region)
  575. {
  576. struct log_c *lc = (struct log_c *) log->context;
  577. log_set_bit(lc, lc->clean_bits, region);
  578. }
  579. static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
  580. {
  581. struct log_c *lc = (struct log_c *) log->context;
  582. if (lc->sync_search >= lc->region_count)
  583. return 0;
  584. do {
  585. *region = ext2_find_next_zero_bit(
  586. (unsigned long *) lc->sync_bits,
  587. lc->region_count,
  588. lc->sync_search);
  589. lc->sync_search = *region + 1;
  590. if (*region >= lc->region_count)
  591. return 0;
  592. } while (log_test_bit(lc->recovering_bits, *region));
  593. log_set_bit(lc, lc->recovering_bits, *region);
  594. return 1;
  595. }
  596. static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
  597. int in_sync)
  598. {
  599. struct log_c *lc = (struct log_c *) log->context;
  600. log_clear_bit(lc, lc->recovering_bits, region);
  601. if (in_sync) {
  602. log_set_bit(lc, lc->sync_bits, region);
  603. lc->sync_count++;
  604. } else if (log_test_bit(lc->sync_bits, region)) {
  605. lc->sync_count--;
  606. log_clear_bit(lc, lc->sync_bits, region);
  607. }
  608. }
  609. static region_t core_get_sync_count(struct dm_dirty_log *log)
  610. {
  611. struct log_c *lc = (struct log_c *) log->context;
  612. return lc->sync_count;
  613. }
  614. #define DMEMIT_SYNC \
  615. if (lc->sync != DEFAULTSYNC) \
  616. DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
  617. static int core_status(struct dm_dirty_log *log, status_type_t status,
  618. char *result, unsigned int maxlen)
  619. {
  620. int sz = 0;
  621. struct log_c *lc = log->context;
  622. switch(status) {
  623. case STATUSTYPE_INFO:
  624. DMEMIT("1 %s", log->type->name);
  625. break;
  626. case STATUSTYPE_TABLE:
  627. DMEMIT("%s %u %u ", log->type->name,
  628. lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
  629. DMEMIT_SYNC;
  630. }
  631. return sz;
  632. }
  633. static int disk_status(struct dm_dirty_log *log, status_type_t status,
  634. char *result, unsigned int maxlen)
  635. {
  636. int sz = 0;
  637. struct log_c *lc = log->context;
  638. switch(status) {
  639. case STATUSTYPE_INFO:
  640. DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
  641. lc->log_dev_failed ? 'D' : 'A');
  642. break;
  643. case STATUSTYPE_TABLE:
  644. DMEMIT("%s %u %s %u ", log->type->name,
  645. lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
  646. lc->region_size);
  647. DMEMIT_SYNC;
  648. }
  649. return sz;
  650. }
  651. static struct dm_dirty_log_type _core_type = {
  652. .name = "core",
  653. .module = THIS_MODULE,
  654. .ctr = core_ctr,
  655. .dtr = core_dtr,
  656. .resume = core_resume,
  657. .get_region_size = core_get_region_size,
  658. .is_clean = core_is_clean,
  659. .in_sync = core_in_sync,
  660. .flush = core_flush,
  661. .mark_region = core_mark_region,
  662. .clear_region = core_clear_region,
  663. .get_resync_work = core_get_resync_work,
  664. .set_region_sync = core_set_region_sync,
  665. .get_sync_count = core_get_sync_count,
  666. .status = core_status,
  667. };
  668. static struct dm_dirty_log_type _disk_type = {
  669. .name = "disk",
  670. .module = THIS_MODULE,
  671. .ctr = disk_ctr,
  672. .dtr = disk_dtr,
  673. .postsuspend = disk_flush,
  674. .resume = disk_resume,
  675. .get_region_size = core_get_region_size,
  676. .is_clean = core_is_clean,
  677. .in_sync = core_in_sync,
  678. .flush = disk_flush,
  679. .mark_region = core_mark_region,
  680. .clear_region = core_clear_region,
  681. .get_resync_work = core_get_resync_work,
  682. .set_region_sync = core_set_region_sync,
  683. .get_sync_count = core_get_sync_count,
  684. .status = disk_status,
  685. };
  686. static int __init dm_dirty_log_init(void)
  687. {
  688. int r;
  689. r = dm_dirty_log_type_register(&_core_type);
  690. if (r)
  691. DMWARN("couldn't register core log");
  692. r = dm_dirty_log_type_register(&_disk_type);
  693. if (r) {
  694. DMWARN("couldn't register disk type");
  695. dm_dirty_log_type_unregister(&_core_type);
  696. }
  697. return r;
  698. }
  699. static void __exit dm_dirty_log_exit(void)
  700. {
  701. dm_dirty_log_type_unregister(&_disk_type);
  702. dm_dirty_log_type_unregister(&_core_type);
  703. }
  704. module_init(dm_dirty_log_init);
  705. module_exit(dm_dirty_log_exit);
  706. MODULE_DESCRIPTION(DM_NAME " dirty region log");
  707. MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
  708. MODULE_LICENSE("GPL");