dm-log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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;
  169. uint32_t region_size;
  170. unsigned int region_count;
  171. region_t sync_count;
  172. unsigned bitset_uint32_count;
  173. uint32_t *clean_bits;
  174. uint32_t *sync_bits;
  175. uint32_t *recovering_bits; /* FIXME: this seems excessive */
  176. int sync_search;
  177. /* Resync flag */
  178. enum sync {
  179. DEFAULTSYNC, /* Synchronize if necessary */
  180. NOSYNC, /* Devices known to be already in sync */
  181. FORCESYNC, /* Force a sync to happen */
  182. } sync;
  183. struct dm_io_request io_req;
  184. /*
  185. * Disk log fields
  186. */
  187. int log_dev_failed;
  188. struct dm_dev *log_dev;
  189. struct log_header header;
  190. struct dm_io_region header_location;
  191. struct log_header *disk_header;
  192. };
  193. /*
  194. * The touched member needs to be updated every time we access
  195. * one of the bitsets.
  196. */
  197. static inline int log_test_bit(uint32_t *bs, unsigned bit)
  198. {
  199. return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
  200. }
  201. static inline void log_set_bit(struct log_c *l,
  202. uint32_t *bs, unsigned bit)
  203. {
  204. ext2_set_bit(bit, (unsigned long *) bs);
  205. l->touched = 1;
  206. }
  207. static inline void log_clear_bit(struct log_c *l,
  208. uint32_t *bs, unsigned bit)
  209. {
  210. ext2_clear_bit(bit, (unsigned long *) bs);
  211. l->touched = 1;
  212. }
  213. /*----------------------------------------------------------------
  214. * Header IO
  215. *--------------------------------------------------------------*/
  216. static void header_to_disk(struct log_header *core, struct log_header *disk)
  217. {
  218. disk->magic = cpu_to_le32(core->magic);
  219. disk->version = cpu_to_le32(core->version);
  220. disk->nr_regions = cpu_to_le64(core->nr_regions);
  221. }
  222. static void header_from_disk(struct log_header *core, struct log_header *disk)
  223. {
  224. core->magic = le32_to_cpu(disk->magic);
  225. core->version = le32_to_cpu(disk->version);
  226. core->nr_regions = le64_to_cpu(disk->nr_regions);
  227. }
  228. static int rw_header(struct log_c *lc, int rw)
  229. {
  230. lc->io_req.bi_rw = rw;
  231. return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
  232. }
  233. static int read_header(struct log_c *log)
  234. {
  235. int r;
  236. r = rw_header(log, READ);
  237. if (r)
  238. return r;
  239. header_from_disk(&log->header, log->disk_header);
  240. /* New log required? */
  241. if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
  242. log->header.magic = MIRROR_MAGIC;
  243. log->header.version = MIRROR_DISK_VERSION;
  244. log->header.nr_regions = 0;
  245. }
  246. #ifdef __LITTLE_ENDIAN
  247. if (log->header.version == 1)
  248. log->header.version = 2;
  249. #endif
  250. if (log->header.version != MIRROR_DISK_VERSION) {
  251. DMWARN("incompatible disk log version");
  252. return -EINVAL;
  253. }
  254. return 0;
  255. }
  256. static int _check_region_size(struct dm_target *ti, uint32_t region_size)
  257. {
  258. if (region_size < 2 || region_size > ti->len)
  259. return 0;
  260. if (!is_power_of_2(region_size))
  261. return 0;
  262. return 1;
  263. }
  264. /*----------------------------------------------------------------
  265. * core log constructor/destructor
  266. *
  267. * argv contains region_size followed optionally by [no]sync
  268. *--------------------------------------------------------------*/
  269. #define BYTE_SHIFT 3
  270. static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
  271. unsigned int argc, char **argv,
  272. struct dm_dev *dev)
  273. {
  274. enum sync sync = DEFAULTSYNC;
  275. struct log_c *lc;
  276. uint32_t region_size;
  277. unsigned int region_count;
  278. size_t bitset_size, buf_size;
  279. int r;
  280. if (argc < 1 || argc > 2) {
  281. DMWARN("wrong number of arguments to dirty region log");
  282. return -EINVAL;
  283. }
  284. if (argc > 1) {
  285. if (!strcmp(argv[1], "sync"))
  286. sync = FORCESYNC;
  287. else if (!strcmp(argv[1], "nosync"))
  288. sync = NOSYNC;
  289. else {
  290. DMWARN("unrecognised sync argument to "
  291. "dirty region log: %s", argv[1]);
  292. return -EINVAL;
  293. }
  294. }
  295. if (sscanf(argv[0], "%u", &region_size) != 1 ||
  296. !_check_region_size(ti, region_size)) {
  297. DMWARN("invalid region size %s", argv[0]);
  298. return -EINVAL;
  299. }
  300. region_count = dm_sector_div_up(ti->len, region_size);
  301. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  302. if (!lc) {
  303. DMWARN("couldn't allocate core log");
  304. return -ENOMEM;
  305. }
  306. lc->ti = ti;
  307. lc->touched = 0;
  308. lc->region_size = region_size;
  309. lc->region_count = region_count;
  310. lc->sync = sync;
  311. /*
  312. * Work out how many "unsigned long"s we need to hold the bitset.
  313. */
  314. bitset_size = dm_round_up(region_count,
  315. sizeof(*lc->clean_bits) << BYTE_SHIFT);
  316. bitset_size >>= BYTE_SHIFT;
  317. lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
  318. /*
  319. * Disk log?
  320. */
  321. if (!dev) {
  322. lc->clean_bits = vmalloc(bitset_size);
  323. if (!lc->clean_bits) {
  324. DMWARN("couldn't allocate clean bitset");
  325. kfree(lc);
  326. return -ENOMEM;
  327. }
  328. lc->disk_header = NULL;
  329. } else {
  330. lc->log_dev = dev;
  331. lc->log_dev_failed = 0;
  332. lc->header_location.bdev = lc->log_dev->bdev;
  333. lc->header_location.sector = 0;
  334. /*
  335. * Buffer holds both header and bitset.
  336. */
  337. buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
  338. bitset_size, ti->limits.hardsect_size);
  339. if (buf_size > dev->bdev->bd_inode->i_size) {
  340. DMWARN("log device %s too small: need %llu bytes",
  341. dev->name, (unsigned long long)buf_size);
  342. kfree(lc);
  343. return -EINVAL;
  344. }
  345. lc->header_location.count = buf_size >> SECTOR_SHIFT;
  346. lc->io_req.mem.type = DM_IO_VMA;
  347. lc->io_req.notify.fn = NULL;
  348. lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
  349. PAGE_SIZE));
  350. if (IS_ERR(lc->io_req.client)) {
  351. r = PTR_ERR(lc->io_req.client);
  352. DMWARN("couldn't allocate disk io client");
  353. kfree(lc);
  354. return -ENOMEM;
  355. }
  356. lc->disk_header = vmalloc(buf_size);
  357. if (!lc->disk_header) {
  358. DMWARN("couldn't allocate disk log buffer");
  359. dm_io_client_destroy(lc->io_req.client);
  360. kfree(lc);
  361. return -ENOMEM;
  362. }
  363. lc->io_req.mem.ptr.vma = lc->disk_header;
  364. lc->clean_bits = (void *)lc->disk_header +
  365. (LOG_OFFSET << SECTOR_SHIFT);
  366. }
  367. memset(lc->clean_bits, -1, bitset_size);
  368. lc->sync_bits = vmalloc(bitset_size);
  369. if (!lc->sync_bits) {
  370. DMWARN("couldn't allocate sync bitset");
  371. if (!dev)
  372. vfree(lc->clean_bits);
  373. else
  374. dm_io_client_destroy(lc->io_req.client);
  375. vfree(lc->disk_header);
  376. kfree(lc);
  377. return -ENOMEM;
  378. }
  379. memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
  380. lc->sync_count = (sync == NOSYNC) ? region_count : 0;
  381. lc->recovering_bits = vmalloc(bitset_size);
  382. if (!lc->recovering_bits) {
  383. DMWARN("couldn't allocate sync bitset");
  384. vfree(lc->sync_bits);
  385. if (!dev)
  386. vfree(lc->clean_bits);
  387. else
  388. dm_io_client_destroy(lc->io_req.client);
  389. vfree(lc->disk_header);
  390. kfree(lc);
  391. return -ENOMEM;
  392. }
  393. memset(lc->recovering_bits, 0, bitset_size);
  394. lc->sync_search = 0;
  395. log->context = lc;
  396. return 0;
  397. }
  398. static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
  399. unsigned int argc, char **argv)
  400. {
  401. return create_log_context(log, ti, argc, argv, NULL);
  402. }
  403. static void destroy_log_context(struct log_c *lc)
  404. {
  405. vfree(lc->sync_bits);
  406. vfree(lc->recovering_bits);
  407. kfree(lc);
  408. }
  409. static void core_dtr(struct dm_dirty_log *log)
  410. {
  411. struct log_c *lc = (struct log_c *) log->context;
  412. vfree(lc->clean_bits);
  413. destroy_log_context(lc);
  414. }
  415. /*----------------------------------------------------------------
  416. * disk log constructor/destructor
  417. *
  418. * argv contains log_device region_size followed optionally by [no]sync
  419. *--------------------------------------------------------------*/
  420. static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
  421. unsigned int argc, char **argv)
  422. {
  423. int r;
  424. struct dm_dev *dev;
  425. if (argc < 2 || argc > 3) {
  426. DMWARN("wrong number of arguments to disk dirty region log");
  427. return -EINVAL;
  428. }
  429. r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
  430. FMODE_READ | FMODE_WRITE, &dev);
  431. if (r)
  432. return r;
  433. r = create_log_context(log, ti, argc - 1, argv + 1, dev);
  434. if (r) {
  435. dm_put_device(ti, dev);
  436. return r;
  437. }
  438. return 0;
  439. }
  440. static void disk_dtr(struct dm_dirty_log *log)
  441. {
  442. struct log_c *lc = (struct log_c *) log->context;
  443. dm_put_device(lc->ti, lc->log_dev);
  444. vfree(lc->disk_header);
  445. dm_io_client_destroy(lc->io_req.client);
  446. destroy_log_context(lc);
  447. }
  448. static int count_bits32(uint32_t *addr, unsigned size)
  449. {
  450. int count = 0, i;
  451. for (i = 0; i < size; i++) {
  452. count += hweight32(*(addr+i));
  453. }
  454. return count;
  455. }
  456. static void fail_log_device(struct log_c *lc)
  457. {
  458. if (lc->log_dev_failed)
  459. return;
  460. lc->log_dev_failed = 1;
  461. dm_table_event(lc->ti->table);
  462. }
  463. static int disk_resume(struct dm_dirty_log *log)
  464. {
  465. int r;
  466. unsigned i;
  467. struct log_c *lc = (struct log_c *) log->context;
  468. size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
  469. /* read the disk header */
  470. r = read_header(lc);
  471. if (r) {
  472. DMWARN("%s: Failed to read header on dirty region log device",
  473. lc->log_dev->name);
  474. fail_log_device(lc);
  475. /*
  476. * If the log device cannot be read, we must assume
  477. * all regions are out-of-sync. If we simply return
  478. * here, the state will be uninitialized and could
  479. * lead us to return 'in-sync' status for regions
  480. * that are actually 'out-of-sync'.
  481. */
  482. lc->header.nr_regions = 0;
  483. }
  484. /* set or clear any new bits -- device has grown */
  485. if (lc->sync == NOSYNC)
  486. for (i = lc->header.nr_regions; i < lc->region_count; i++)
  487. /* FIXME: amazingly inefficient */
  488. log_set_bit(lc, lc->clean_bits, i);
  489. else
  490. for (i = lc->header.nr_regions; i < lc->region_count; i++)
  491. /* FIXME: amazingly inefficient */
  492. log_clear_bit(lc, lc->clean_bits, i);
  493. /* clear any old bits -- device has shrunk */
  494. for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
  495. log_clear_bit(lc, lc->clean_bits, i);
  496. /* copy clean across to sync */
  497. memcpy(lc->sync_bits, lc->clean_bits, size);
  498. lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
  499. lc->sync_search = 0;
  500. /* set the correct number of regions in the header */
  501. lc->header.nr_regions = lc->region_count;
  502. header_to_disk(&lc->header, lc->disk_header);
  503. /* write the new header */
  504. r = rw_header(lc, WRITE);
  505. if (r) {
  506. DMWARN("%s: Failed to write header on dirty region log device",
  507. lc->log_dev->name);
  508. fail_log_device(lc);
  509. }
  510. return r;
  511. }
  512. static uint32_t core_get_region_size(struct dm_dirty_log *log)
  513. {
  514. struct log_c *lc = (struct log_c *) log->context;
  515. return lc->region_size;
  516. }
  517. static int core_resume(struct dm_dirty_log *log)
  518. {
  519. struct log_c *lc = (struct log_c *) log->context;
  520. lc->sync_search = 0;
  521. return 0;
  522. }
  523. static int core_is_clean(struct dm_dirty_log *log, region_t region)
  524. {
  525. struct log_c *lc = (struct log_c *) log->context;
  526. return log_test_bit(lc->clean_bits, region);
  527. }
  528. static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
  529. {
  530. struct log_c *lc = (struct log_c *) log->context;
  531. return log_test_bit(lc->sync_bits, region);
  532. }
  533. static int core_flush(struct dm_dirty_log *log)
  534. {
  535. /* no op */
  536. return 0;
  537. }
  538. static int disk_flush(struct dm_dirty_log *log)
  539. {
  540. int r;
  541. struct log_c *lc = (struct log_c *) log->context;
  542. /* only write if the log has changed */
  543. if (!lc->touched)
  544. return 0;
  545. r = rw_header(lc, WRITE);
  546. if (r)
  547. fail_log_device(lc);
  548. else
  549. lc->touched = 0;
  550. return r;
  551. }
  552. static void core_mark_region(struct dm_dirty_log *log, region_t region)
  553. {
  554. struct log_c *lc = (struct log_c *) log->context;
  555. log_clear_bit(lc, lc->clean_bits, region);
  556. }
  557. static void core_clear_region(struct dm_dirty_log *log, region_t region)
  558. {
  559. struct log_c *lc = (struct log_c *) log->context;
  560. log_set_bit(lc, lc->clean_bits, region);
  561. }
  562. static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
  563. {
  564. struct log_c *lc = (struct log_c *) log->context;
  565. if (lc->sync_search >= lc->region_count)
  566. return 0;
  567. do {
  568. *region = ext2_find_next_zero_bit(
  569. (unsigned long *) lc->sync_bits,
  570. lc->region_count,
  571. lc->sync_search);
  572. lc->sync_search = *region + 1;
  573. if (*region >= lc->region_count)
  574. return 0;
  575. } while (log_test_bit(lc->recovering_bits, *region));
  576. log_set_bit(lc, lc->recovering_bits, *region);
  577. return 1;
  578. }
  579. static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
  580. int in_sync)
  581. {
  582. struct log_c *lc = (struct log_c *) log->context;
  583. log_clear_bit(lc, lc->recovering_bits, region);
  584. if (in_sync) {
  585. log_set_bit(lc, lc->sync_bits, region);
  586. lc->sync_count++;
  587. } else if (log_test_bit(lc->sync_bits, region)) {
  588. lc->sync_count--;
  589. log_clear_bit(lc, lc->sync_bits, region);
  590. }
  591. }
  592. static region_t core_get_sync_count(struct dm_dirty_log *log)
  593. {
  594. struct log_c *lc = (struct log_c *) log->context;
  595. return lc->sync_count;
  596. }
  597. #define DMEMIT_SYNC \
  598. if (lc->sync != DEFAULTSYNC) \
  599. DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
  600. static int core_status(struct dm_dirty_log *log, status_type_t status,
  601. char *result, unsigned int maxlen)
  602. {
  603. int sz = 0;
  604. struct log_c *lc = log->context;
  605. switch(status) {
  606. case STATUSTYPE_INFO:
  607. DMEMIT("1 %s", log->type->name);
  608. break;
  609. case STATUSTYPE_TABLE:
  610. DMEMIT("%s %u %u ", log->type->name,
  611. lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
  612. DMEMIT_SYNC;
  613. }
  614. return sz;
  615. }
  616. static int disk_status(struct dm_dirty_log *log, status_type_t status,
  617. char *result, unsigned int maxlen)
  618. {
  619. int sz = 0;
  620. struct log_c *lc = log->context;
  621. switch(status) {
  622. case STATUSTYPE_INFO:
  623. DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
  624. lc->log_dev_failed ? 'D' : 'A');
  625. break;
  626. case STATUSTYPE_TABLE:
  627. DMEMIT("%s %u %s %u ", log->type->name,
  628. lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
  629. lc->region_size);
  630. DMEMIT_SYNC;
  631. }
  632. return sz;
  633. }
  634. static struct dm_dirty_log_type _core_type = {
  635. .name = "core",
  636. .module = THIS_MODULE,
  637. .ctr = core_ctr,
  638. .dtr = core_dtr,
  639. .resume = core_resume,
  640. .get_region_size = core_get_region_size,
  641. .is_clean = core_is_clean,
  642. .in_sync = core_in_sync,
  643. .flush = core_flush,
  644. .mark_region = core_mark_region,
  645. .clear_region = core_clear_region,
  646. .get_resync_work = core_get_resync_work,
  647. .set_region_sync = core_set_region_sync,
  648. .get_sync_count = core_get_sync_count,
  649. .status = core_status,
  650. };
  651. static struct dm_dirty_log_type _disk_type = {
  652. .name = "disk",
  653. .module = THIS_MODULE,
  654. .ctr = disk_ctr,
  655. .dtr = disk_dtr,
  656. .postsuspend = disk_flush,
  657. .resume = disk_resume,
  658. .get_region_size = core_get_region_size,
  659. .is_clean = core_is_clean,
  660. .in_sync = core_in_sync,
  661. .flush = disk_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 = disk_status,
  668. };
  669. static int __init dm_dirty_log_init(void)
  670. {
  671. int r;
  672. r = dm_dirty_log_type_register(&_core_type);
  673. if (r)
  674. DMWARN("couldn't register core log");
  675. r = dm_dirty_log_type_register(&_disk_type);
  676. if (r) {
  677. DMWARN("couldn't register disk type");
  678. dm_dirty_log_type_unregister(&_core_type);
  679. }
  680. return r;
  681. }
  682. static void __exit dm_dirty_log_exit(void)
  683. {
  684. dm_dirty_log_type_unregister(&_disk_type);
  685. dm_dirty_log_type_unregister(&_core_type);
  686. }
  687. module_init(dm_dirty_log_init);
  688. module_exit(dm_dirty_log_exit);
  689. MODULE_DESCRIPTION(DM_NAME " dirty region log");
  690. MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
  691. MODULE_LICENSE("GPL");