dm-log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 <linux/device-mapper.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. return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
  265. }
  266. static int read_header(struct log_c *log)
  267. {
  268. int r;
  269. r = rw_header(log, READ);
  270. if (r)
  271. return r;
  272. header_from_disk(&log->header, log->disk_header);
  273. /* New log required? */
  274. if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
  275. log->header.magic = MIRROR_MAGIC;
  276. log->header.version = MIRROR_DISK_VERSION;
  277. log->header.nr_regions = 0;
  278. }
  279. #ifdef __LITTLE_ENDIAN
  280. if (log->header.version == 1)
  281. log->header.version = 2;
  282. #endif
  283. if (log->header.version != MIRROR_DISK_VERSION) {
  284. DMWARN("incompatible disk log version");
  285. return -EINVAL;
  286. }
  287. return 0;
  288. }
  289. /*----------------------------------------------------------------
  290. * core log constructor/destructor
  291. *
  292. * argv contains region_size followed optionally by [no]sync
  293. *--------------------------------------------------------------*/
  294. #define BYTE_SHIFT 3
  295. static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
  296. unsigned int argc, char **argv,
  297. struct dm_dev *dev)
  298. {
  299. enum sync sync = DEFAULTSYNC;
  300. struct log_c *lc;
  301. uint32_t region_size;
  302. unsigned int region_count;
  303. size_t bitset_size, buf_size;
  304. int r;
  305. if (argc < 1 || argc > 2) {
  306. DMWARN("wrong number of arguments to dirty region log");
  307. return -EINVAL;
  308. }
  309. if (argc > 1) {
  310. if (!strcmp(argv[1], "sync"))
  311. sync = FORCESYNC;
  312. else if (!strcmp(argv[1], "nosync"))
  313. sync = NOSYNC;
  314. else {
  315. DMWARN("unrecognised sync argument to "
  316. "dirty region log: %s", argv[1]);
  317. return -EINVAL;
  318. }
  319. }
  320. if (sscanf(argv[0], "%u", &region_size) != 1) {
  321. DMWARN("invalid region size string");
  322. return -EINVAL;
  323. }
  324. region_count = dm_sector_div_up(ti->len, region_size);
  325. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  326. if (!lc) {
  327. DMWARN("couldn't allocate core log");
  328. return -ENOMEM;
  329. }
  330. lc->ti = ti;
  331. lc->touched = 0;
  332. lc->region_size = region_size;
  333. lc->region_count = region_count;
  334. lc->sync = sync;
  335. /*
  336. * Work out how many "unsigned long"s we need to hold the bitset.
  337. */
  338. bitset_size = dm_round_up(region_count,
  339. sizeof(*lc->clean_bits) << BYTE_SHIFT);
  340. bitset_size >>= BYTE_SHIFT;
  341. lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
  342. /*
  343. * Disk log?
  344. */
  345. if (!dev) {
  346. lc->clean_bits = vmalloc(bitset_size);
  347. if (!lc->clean_bits) {
  348. DMWARN("couldn't allocate clean bitset");
  349. kfree(lc);
  350. return -ENOMEM;
  351. }
  352. lc->disk_header = NULL;
  353. } else {
  354. lc->log_dev = dev;
  355. lc->log_dev_failed = 0;
  356. lc->header_location.bdev = lc->log_dev->bdev;
  357. lc->header_location.sector = 0;
  358. /*
  359. * Buffer holds both header and bitset.
  360. */
  361. buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
  362. bitset_size, ti->limits.hardsect_size);
  363. lc->header_location.count = buf_size >> SECTOR_SHIFT;
  364. lc->io_req.mem.type = DM_IO_VMA;
  365. lc->io_req.notify.fn = NULL;
  366. lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
  367. PAGE_SIZE));
  368. if (IS_ERR(lc->io_req.client)) {
  369. r = PTR_ERR(lc->io_req.client);
  370. DMWARN("couldn't allocate disk io client");
  371. kfree(lc);
  372. return -ENOMEM;
  373. }
  374. lc->disk_header = vmalloc(buf_size);
  375. if (!lc->disk_header) {
  376. DMWARN("couldn't allocate disk log buffer");
  377. dm_io_client_destroy(lc->io_req.client);
  378. kfree(lc);
  379. return -ENOMEM;
  380. }
  381. lc->io_req.mem.ptr.vma = lc->disk_header;
  382. lc->clean_bits = (void *)lc->disk_header +
  383. (LOG_OFFSET << SECTOR_SHIFT);
  384. }
  385. memset(lc->clean_bits, -1, bitset_size);
  386. lc->sync_bits = vmalloc(bitset_size);
  387. if (!lc->sync_bits) {
  388. DMWARN("couldn't allocate sync bitset");
  389. if (!dev)
  390. vfree(lc->clean_bits);
  391. else
  392. dm_io_client_destroy(lc->io_req.client);
  393. vfree(lc->disk_header);
  394. kfree(lc);
  395. return -ENOMEM;
  396. }
  397. memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
  398. lc->sync_count = (sync == NOSYNC) ? region_count : 0;
  399. lc->recovering_bits = vmalloc(bitset_size);
  400. if (!lc->recovering_bits) {
  401. DMWARN("couldn't allocate sync bitset");
  402. vfree(lc->sync_bits);
  403. if (!dev)
  404. vfree(lc->clean_bits);
  405. else
  406. dm_io_client_destroy(lc->io_req.client);
  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. header_to_disk(&lc->header, lc->disk_header);
  521. /* write the new header */
  522. r = rw_header(lc, WRITE);
  523. if (r) {
  524. DMWARN("%s: Failed to write header on dirty region log device",
  525. lc->log_dev->name);
  526. fail_log_device(lc);
  527. }
  528. return r;
  529. }
  530. static uint32_t core_get_region_size(struct dm_dirty_log *log)
  531. {
  532. struct log_c *lc = (struct log_c *) log->context;
  533. return lc->region_size;
  534. }
  535. static int core_resume(struct dm_dirty_log *log)
  536. {
  537. struct log_c *lc = (struct log_c *) log->context;
  538. lc->sync_search = 0;
  539. return 0;
  540. }
  541. static int core_is_clean(struct dm_dirty_log *log, region_t region)
  542. {
  543. struct log_c *lc = (struct log_c *) log->context;
  544. return log_test_bit(lc->clean_bits, region);
  545. }
  546. static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
  547. {
  548. struct log_c *lc = (struct log_c *) log->context;
  549. return log_test_bit(lc->sync_bits, region);
  550. }
  551. static int core_flush(struct dm_dirty_log *log)
  552. {
  553. /* no op */
  554. return 0;
  555. }
  556. static int disk_flush(struct dm_dirty_log *log)
  557. {
  558. int r;
  559. struct log_c *lc = (struct log_c *) log->context;
  560. /* only write if the log has changed */
  561. if (!lc->touched)
  562. return 0;
  563. r = rw_header(lc, WRITE);
  564. if (r)
  565. fail_log_device(lc);
  566. else
  567. lc->touched = 0;
  568. return r;
  569. }
  570. static void core_mark_region(struct dm_dirty_log *log, region_t region)
  571. {
  572. struct log_c *lc = (struct log_c *) log->context;
  573. log_clear_bit(lc, lc->clean_bits, region);
  574. }
  575. static void core_clear_region(struct dm_dirty_log *log, region_t region)
  576. {
  577. struct log_c *lc = (struct log_c *) log->context;
  578. log_set_bit(lc, lc->clean_bits, region);
  579. }
  580. static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
  581. {
  582. struct log_c *lc = (struct log_c *) log->context;
  583. if (lc->sync_search >= lc->region_count)
  584. return 0;
  585. do {
  586. *region = ext2_find_next_zero_bit(
  587. (unsigned long *) lc->sync_bits,
  588. lc->region_count,
  589. lc->sync_search);
  590. lc->sync_search = *region + 1;
  591. if (*region >= lc->region_count)
  592. return 0;
  593. } while (log_test_bit(lc->recovering_bits, *region));
  594. log_set_bit(lc, lc->recovering_bits, *region);
  595. return 1;
  596. }
  597. static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
  598. int in_sync)
  599. {
  600. struct log_c *lc = (struct log_c *) log->context;
  601. log_clear_bit(lc, lc->recovering_bits, region);
  602. if (in_sync) {
  603. log_set_bit(lc, lc->sync_bits, region);
  604. lc->sync_count++;
  605. } else if (log_test_bit(lc->sync_bits, region)) {
  606. lc->sync_count--;
  607. log_clear_bit(lc, lc->sync_bits, region);
  608. }
  609. }
  610. static region_t core_get_sync_count(struct dm_dirty_log *log)
  611. {
  612. struct log_c *lc = (struct log_c *) log->context;
  613. return lc->sync_count;
  614. }
  615. #define DMEMIT_SYNC \
  616. if (lc->sync != DEFAULTSYNC) \
  617. DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
  618. static int core_status(struct dm_dirty_log *log, status_type_t status,
  619. char *result, unsigned int maxlen)
  620. {
  621. int sz = 0;
  622. struct log_c *lc = log->context;
  623. switch(status) {
  624. case STATUSTYPE_INFO:
  625. DMEMIT("1 %s", log->type->name);
  626. break;
  627. case STATUSTYPE_TABLE:
  628. DMEMIT("%s %u %u ", log->type->name,
  629. lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
  630. DMEMIT_SYNC;
  631. }
  632. return sz;
  633. }
  634. static int disk_status(struct dm_dirty_log *log, status_type_t status,
  635. char *result, unsigned int maxlen)
  636. {
  637. int sz = 0;
  638. struct log_c *lc = log->context;
  639. switch(status) {
  640. case STATUSTYPE_INFO:
  641. DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
  642. lc->log_dev_failed ? 'D' : 'A');
  643. break;
  644. case STATUSTYPE_TABLE:
  645. DMEMIT("%s %u %s %u ", log->type->name,
  646. lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
  647. lc->region_size);
  648. DMEMIT_SYNC;
  649. }
  650. return sz;
  651. }
  652. static struct dm_dirty_log_type _core_type = {
  653. .name = "core",
  654. .module = THIS_MODULE,
  655. .ctr = core_ctr,
  656. .dtr = core_dtr,
  657. .resume = core_resume,
  658. .get_region_size = core_get_region_size,
  659. .is_clean = core_is_clean,
  660. .in_sync = core_in_sync,
  661. .flush = core_flush,
  662. .mark_region = core_mark_region,
  663. .clear_region = core_clear_region,
  664. .get_resync_work = core_get_resync_work,
  665. .set_region_sync = core_set_region_sync,
  666. .get_sync_count = core_get_sync_count,
  667. .status = core_status,
  668. };
  669. static struct dm_dirty_log_type _disk_type = {
  670. .name = "disk",
  671. .module = THIS_MODULE,
  672. .ctr = disk_ctr,
  673. .dtr = disk_dtr,
  674. .postsuspend = disk_flush,
  675. .resume = disk_resume,
  676. .get_region_size = core_get_region_size,
  677. .is_clean = core_is_clean,
  678. .in_sync = core_in_sync,
  679. .flush = disk_flush,
  680. .mark_region = core_mark_region,
  681. .clear_region = core_clear_region,
  682. .get_resync_work = core_get_resync_work,
  683. .set_region_sync = core_set_region_sync,
  684. .get_sync_count = core_get_sync_count,
  685. .status = disk_status,
  686. };
  687. static int __init dm_dirty_log_init(void)
  688. {
  689. int r;
  690. r = dm_dirty_log_type_register(&_core_type);
  691. if (r)
  692. DMWARN("couldn't register core log");
  693. r = dm_dirty_log_type_register(&_disk_type);
  694. if (r) {
  695. DMWARN("couldn't register disk type");
  696. dm_dirty_log_type_unregister(&_core_type);
  697. }
  698. return r;
  699. }
  700. static void __exit dm_dirty_log_exit(void)
  701. {
  702. dm_dirty_log_type_unregister(&_disk_type);
  703. dm_dirty_log_type_unregister(&_core_type);
  704. }
  705. module_init(dm_dirty_log_init);
  706. module_exit(dm_dirty_log_exit);
  707. MODULE_DESCRIPTION(DM_NAME " dirty region log");
  708. MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
  709. MODULE_LICENSE("GPL");