dm-log.c 20 KB

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