dm-log.c 20 KB

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