dm-log-writes.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * Copyright (C) 2014 Facebook. All rights reserved.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include <linux/device-mapper.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/bio.h>
  11. #include <linux/dax.h>
  12. #include <linux/slab.h>
  13. #include <linux/kthread.h>
  14. #include <linux/freezer.h>
  15. #include <linux/uio.h>
  16. #define DM_MSG_PREFIX "log-writes"
  17. /*
  18. * This target will sequentially log all writes to the target device onto the
  19. * log device. This is helpful for replaying writes to check for fs consistency
  20. * at all times. This target provides a mechanism to mark specific events to
  21. * check data at a later time. So for example you would:
  22. *
  23. * write data
  24. * fsync
  25. * dmsetup message /dev/whatever mark mymark
  26. * unmount /mnt/test
  27. *
  28. * Then replay the log up to mymark and check the contents of the replay to
  29. * verify it matches what was written.
  30. *
  31. * We log writes only after they have been flushed, this makes the log describe
  32. * close to the order in which the data hits the actual disk, not its cache. So
  33. * for example the following sequence (W means write, C means complete)
  34. *
  35. * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd
  36. *
  37. * Would result in the log looking like this:
  38. *
  39. * c,a,b,flush,fuad,<other writes>,<next flush>
  40. *
  41. * This is meant to help expose problems where file systems do not properly wait
  42. * on data being written before invoking a FLUSH. FUA bypasses cache so once it
  43. * completes it is added to the log as it should be on disk.
  44. *
  45. * We treat DISCARDs as if they don't bypass cache so that they are logged in
  46. * order of completion along with the normal writes. If we didn't do it this
  47. * way we would process all the discards first and then write all the data, when
  48. * in fact we want to do the data and the discard in the order that they
  49. * completed.
  50. */
  51. #define LOG_FLUSH_FLAG (1 << 0)
  52. #define LOG_FUA_FLAG (1 << 1)
  53. #define LOG_DISCARD_FLAG (1 << 2)
  54. #define LOG_MARK_FLAG (1 << 3)
  55. #define LOG_METADATA_FLAG (1 << 4)
  56. #define WRITE_LOG_VERSION 1ULL
  57. #define WRITE_LOG_MAGIC 0x6a736677736872ULL
  58. #define WRITE_LOG_SUPER_SECTOR 0
  59. /*
  60. * The disk format for this is braindead simple.
  61. *
  62. * At byte 0 we have our super, followed by the following sequence for
  63. * nr_entries:
  64. *
  65. * [ 1 sector ][ entry->nr_sectors ]
  66. * [log_write_entry][ data written ]
  67. *
  68. * The log_write_entry takes up a full sector so we can have arbitrary length
  69. * marks and it leaves us room for extra content in the future.
  70. */
  71. /*
  72. * Basic info about the log for userspace.
  73. */
  74. struct log_write_super {
  75. __le64 magic;
  76. __le64 version;
  77. __le64 nr_entries;
  78. __le32 sectorsize;
  79. };
  80. /*
  81. * sector - the sector we wrote.
  82. * nr_sectors - the number of sectors we wrote.
  83. * flags - flags for this log entry.
  84. * data_len - the size of the data in this log entry, this is for private log
  85. * entry stuff, the MARK data provided by userspace for example.
  86. */
  87. struct log_write_entry {
  88. __le64 sector;
  89. __le64 nr_sectors;
  90. __le64 flags;
  91. __le64 data_len;
  92. };
  93. struct log_writes_c {
  94. struct dm_dev *dev;
  95. struct dm_dev *logdev;
  96. u64 logged_entries;
  97. u32 sectorsize;
  98. u32 sectorshift;
  99. atomic_t io_blocks;
  100. atomic_t pending_blocks;
  101. sector_t next_sector;
  102. sector_t end_sector;
  103. bool logging_enabled;
  104. bool device_supports_discard;
  105. spinlock_t blocks_lock;
  106. struct list_head unflushed_blocks;
  107. struct list_head logging_blocks;
  108. wait_queue_head_t wait;
  109. struct task_struct *log_kthread;
  110. struct completion super_done;
  111. };
  112. struct pending_block {
  113. int vec_cnt;
  114. u64 flags;
  115. sector_t sector;
  116. sector_t nr_sectors;
  117. char *data;
  118. u32 datalen;
  119. struct list_head list;
  120. struct bio_vec vecs[0];
  121. };
  122. struct per_bio_data {
  123. struct pending_block *block;
  124. };
  125. static inline sector_t bio_to_dev_sectors(struct log_writes_c *lc,
  126. sector_t sectors)
  127. {
  128. return sectors >> (lc->sectorshift - SECTOR_SHIFT);
  129. }
  130. static inline sector_t dev_to_bio_sectors(struct log_writes_c *lc,
  131. sector_t sectors)
  132. {
  133. return sectors << (lc->sectorshift - SECTOR_SHIFT);
  134. }
  135. static void put_pending_block(struct log_writes_c *lc)
  136. {
  137. if (atomic_dec_and_test(&lc->pending_blocks)) {
  138. smp_mb__after_atomic();
  139. if (waitqueue_active(&lc->wait))
  140. wake_up(&lc->wait);
  141. }
  142. }
  143. static void put_io_block(struct log_writes_c *lc)
  144. {
  145. if (atomic_dec_and_test(&lc->io_blocks)) {
  146. smp_mb__after_atomic();
  147. if (waitqueue_active(&lc->wait))
  148. wake_up(&lc->wait);
  149. }
  150. }
  151. static void log_end_io(struct bio *bio)
  152. {
  153. struct log_writes_c *lc = bio->bi_private;
  154. if (bio->bi_status) {
  155. unsigned long flags;
  156. DMERR("Error writing log block, error=%d", bio->bi_status);
  157. spin_lock_irqsave(&lc->blocks_lock, flags);
  158. lc->logging_enabled = false;
  159. spin_unlock_irqrestore(&lc->blocks_lock, flags);
  160. }
  161. bio_free_pages(bio);
  162. put_io_block(lc);
  163. bio_put(bio);
  164. }
  165. static void log_end_super(struct bio *bio)
  166. {
  167. struct log_writes_c *lc = bio->bi_private;
  168. complete(&lc->super_done);
  169. log_end_io(bio);
  170. }
  171. /*
  172. * Meant to be called if there is an error, it will free all the pages
  173. * associated with the block.
  174. */
  175. static void free_pending_block(struct log_writes_c *lc,
  176. struct pending_block *block)
  177. {
  178. int i;
  179. for (i = 0; i < block->vec_cnt; i++) {
  180. if (block->vecs[i].bv_page)
  181. __free_page(block->vecs[i].bv_page);
  182. }
  183. kfree(block->data);
  184. kfree(block);
  185. put_pending_block(lc);
  186. }
  187. static int write_metadata(struct log_writes_c *lc, void *entry,
  188. size_t entrylen, void *data, size_t datalen,
  189. sector_t sector)
  190. {
  191. struct bio *bio;
  192. struct page *page;
  193. void *ptr;
  194. size_t ret;
  195. bio = bio_alloc(GFP_KERNEL, 1);
  196. if (!bio) {
  197. DMERR("Couldn't alloc log bio");
  198. goto error;
  199. }
  200. bio->bi_iter.bi_size = 0;
  201. bio->bi_iter.bi_sector = sector;
  202. bio_set_dev(bio, lc->logdev->bdev);
  203. bio->bi_end_io = (sector == WRITE_LOG_SUPER_SECTOR) ?
  204. log_end_super : log_end_io;
  205. bio->bi_private = lc;
  206. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  207. page = alloc_page(GFP_KERNEL);
  208. if (!page) {
  209. DMERR("Couldn't alloc log page");
  210. bio_put(bio);
  211. goto error;
  212. }
  213. ptr = kmap_atomic(page);
  214. memcpy(ptr, entry, entrylen);
  215. if (datalen)
  216. memcpy(ptr + entrylen, data, datalen);
  217. memset(ptr + entrylen + datalen, 0,
  218. lc->sectorsize - entrylen - datalen);
  219. kunmap_atomic(ptr);
  220. ret = bio_add_page(bio, page, lc->sectorsize, 0);
  221. if (ret != lc->sectorsize) {
  222. DMERR("Couldn't add page to the log block");
  223. goto error_bio;
  224. }
  225. submit_bio(bio);
  226. return 0;
  227. error_bio:
  228. bio_put(bio);
  229. __free_page(page);
  230. error:
  231. put_io_block(lc);
  232. return -1;
  233. }
  234. static int write_inline_data(struct log_writes_c *lc, void *entry,
  235. size_t entrylen, void *data, size_t datalen,
  236. sector_t sector)
  237. {
  238. int num_pages, bio_pages, pg_datalen, pg_sectorlen, i;
  239. struct page *page;
  240. struct bio *bio;
  241. size_t ret;
  242. void *ptr;
  243. while (datalen) {
  244. num_pages = ALIGN(datalen, PAGE_SIZE) >> PAGE_SHIFT;
  245. bio_pages = min(num_pages, BIO_MAX_PAGES);
  246. atomic_inc(&lc->io_blocks);
  247. bio = bio_alloc(GFP_KERNEL, bio_pages);
  248. if (!bio) {
  249. DMERR("Couldn't alloc inline data bio");
  250. goto error;
  251. }
  252. bio->bi_iter.bi_size = 0;
  253. bio->bi_iter.bi_sector = sector;
  254. bio_set_dev(bio, lc->logdev->bdev);
  255. bio->bi_end_io = log_end_io;
  256. bio->bi_private = lc;
  257. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  258. for (i = 0; i < bio_pages; i++) {
  259. pg_datalen = min_t(int, datalen, PAGE_SIZE);
  260. pg_sectorlen = ALIGN(pg_datalen, lc->sectorsize);
  261. page = alloc_page(GFP_KERNEL);
  262. if (!page) {
  263. DMERR("Couldn't alloc inline data page");
  264. goto error_bio;
  265. }
  266. ptr = kmap_atomic(page);
  267. memcpy(ptr, data, pg_datalen);
  268. if (pg_sectorlen > pg_datalen)
  269. memset(ptr + pg_datalen, 0, pg_sectorlen - pg_datalen);
  270. kunmap_atomic(ptr);
  271. ret = bio_add_page(bio, page, pg_sectorlen, 0);
  272. if (ret != pg_sectorlen) {
  273. DMERR("Couldn't add page of inline data");
  274. __free_page(page);
  275. goto error_bio;
  276. }
  277. datalen -= pg_datalen;
  278. data += pg_datalen;
  279. }
  280. submit_bio(bio);
  281. sector += bio_pages * PAGE_SECTORS;
  282. }
  283. return 0;
  284. error_bio:
  285. bio_free_pages(bio);
  286. bio_put(bio);
  287. error:
  288. put_io_block(lc);
  289. return -1;
  290. }
  291. static int log_one_block(struct log_writes_c *lc,
  292. struct pending_block *block, sector_t sector)
  293. {
  294. struct bio *bio;
  295. struct log_write_entry entry;
  296. size_t metadatalen, ret;
  297. int i;
  298. entry.sector = cpu_to_le64(block->sector);
  299. entry.nr_sectors = cpu_to_le64(block->nr_sectors);
  300. entry.flags = cpu_to_le64(block->flags);
  301. entry.data_len = cpu_to_le64(block->datalen);
  302. metadatalen = (block->flags & LOG_MARK_FLAG) ? block->datalen : 0;
  303. if (write_metadata(lc, &entry, sizeof(entry), block->data,
  304. metadatalen, sector)) {
  305. free_pending_block(lc, block);
  306. return -1;
  307. }
  308. sector += dev_to_bio_sectors(lc, 1);
  309. if (block->datalen && metadatalen == 0) {
  310. if (write_inline_data(lc, &entry, sizeof(entry), block->data,
  311. block->datalen, sector)) {
  312. free_pending_block(lc, block);
  313. return -1;
  314. }
  315. /* we don't support both inline data & bio data */
  316. goto out;
  317. }
  318. if (!block->vec_cnt)
  319. goto out;
  320. atomic_inc(&lc->io_blocks);
  321. bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES));
  322. if (!bio) {
  323. DMERR("Couldn't alloc log bio");
  324. goto error;
  325. }
  326. bio->bi_iter.bi_size = 0;
  327. bio->bi_iter.bi_sector = sector;
  328. bio_set_dev(bio, lc->logdev->bdev);
  329. bio->bi_end_io = log_end_io;
  330. bio->bi_private = lc;
  331. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  332. for (i = 0; i < block->vec_cnt; i++) {
  333. /*
  334. * The page offset is always 0 because we allocate a new page
  335. * for every bvec in the original bio for simplicity sake.
  336. */
  337. ret = bio_add_page(bio, block->vecs[i].bv_page,
  338. block->vecs[i].bv_len, 0);
  339. if (ret != block->vecs[i].bv_len) {
  340. atomic_inc(&lc->io_blocks);
  341. submit_bio(bio);
  342. bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt - i, BIO_MAX_PAGES));
  343. if (!bio) {
  344. DMERR("Couldn't alloc log bio");
  345. goto error;
  346. }
  347. bio->bi_iter.bi_size = 0;
  348. bio->bi_iter.bi_sector = sector;
  349. bio_set_dev(bio, lc->logdev->bdev);
  350. bio->bi_end_io = log_end_io;
  351. bio->bi_private = lc;
  352. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  353. ret = bio_add_page(bio, block->vecs[i].bv_page,
  354. block->vecs[i].bv_len, 0);
  355. if (ret != block->vecs[i].bv_len) {
  356. DMERR("Couldn't add page on new bio?");
  357. bio_put(bio);
  358. goto error;
  359. }
  360. }
  361. sector += block->vecs[i].bv_len >> SECTOR_SHIFT;
  362. }
  363. submit_bio(bio);
  364. out:
  365. kfree(block->data);
  366. kfree(block);
  367. put_pending_block(lc);
  368. return 0;
  369. error:
  370. free_pending_block(lc, block);
  371. put_io_block(lc);
  372. return -1;
  373. }
  374. static int log_super(struct log_writes_c *lc)
  375. {
  376. struct log_write_super super;
  377. super.magic = cpu_to_le64(WRITE_LOG_MAGIC);
  378. super.version = cpu_to_le64(WRITE_LOG_VERSION);
  379. super.nr_entries = cpu_to_le64(lc->logged_entries);
  380. super.sectorsize = cpu_to_le32(lc->sectorsize);
  381. if (write_metadata(lc, &super, sizeof(super), NULL, 0,
  382. WRITE_LOG_SUPER_SECTOR)) {
  383. DMERR("Couldn't write super");
  384. return -1;
  385. }
  386. /*
  387. * Super sector should be writen in-order, otherwise the
  388. * nr_entries could be rewritten incorrectly by an old bio.
  389. */
  390. wait_for_completion_io(&lc->super_done);
  391. return 0;
  392. }
  393. static inline sector_t logdev_last_sector(struct log_writes_c *lc)
  394. {
  395. return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT;
  396. }
  397. static int log_writes_kthread(void *arg)
  398. {
  399. struct log_writes_c *lc = (struct log_writes_c *)arg;
  400. sector_t sector = 0;
  401. while (!kthread_should_stop()) {
  402. bool super = false;
  403. bool logging_enabled;
  404. struct pending_block *block = NULL;
  405. int ret;
  406. spin_lock_irq(&lc->blocks_lock);
  407. if (!list_empty(&lc->logging_blocks)) {
  408. block = list_first_entry(&lc->logging_blocks,
  409. struct pending_block, list);
  410. list_del_init(&block->list);
  411. if (!lc->logging_enabled)
  412. goto next;
  413. sector = lc->next_sector;
  414. if (!(block->flags & LOG_DISCARD_FLAG))
  415. lc->next_sector += dev_to_bio_sectors(lc, block->nr_sectors);
  416. lc->next_sector += dev_to_bio_sectors(lc, 1);
  417. /*
  418. * Apparently the size of the device may not be known
  419. * right away, so handle this properly.
  420. */
  421. if (!lc->end_sector)
  422. lc->end_sector = logdev_last_sector(lc);
  423. if (lc->end_sector &&
  424. lc->next_sector >= lc->end_sector) {
  425. DMERR("Ran out of space on the logdev");
  426. lc->logging_enabled = false;
  427. goto next;
  428. }
  429. lc->logged_entries++;
  430. atomic_inc(&lc->io_blocks);
  431. super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG));
  432. if (super)
  433. atomic_inc(&lc->io_blocks);
  434. }
  435. next:
  436. logging_enabled = lc->logging_enabled;
  437. spin_unlock_irq(&lc->blocks_lock);
  438. if (block) {
  439. if (logging_enabled) {
  440. ret = log_one_block(lc, block, sector);
  441. if (!ret && super)
  442. ret = log_super(lc);
  443. if (ret) {
  444. spin_lock_irq(&lc->blocks_lock);
  445. lc->logging_enabled = false;
  446. spin_unlock_irq(&lc->blocks_lock);
  447. }
  448. } else
  449. free_pending_block(lc, block);
  450. continue;
  451. }
  452. if (!try_to_freeze()) {
  453. set_current_state(TASK_INTERRUPTIBLE);
  454. if (!kthread_should_stop() &&
  455. list_empty(&lc->logging_blocks))
  456. schedule();
  457. __set_current_state(TASK_RUNNING);
  458. }
  459. }
  460. return 0;
  461. }
  462. /*
  463. * Construct a log-writes mapping:
  464. * log-writes <dev_path> <log_dev_path>
  465. */
  466. static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  467. {
  468. struct log_writes_c *lc;
  469. struct dm_arg_set as;
  470. const char *devname, *logdevname;
  471. int ret;
  472. as.argc = argc;
  473. as.argv = argv;
  474. if (argc < 2) {
  475. ti->error = "Invalid argument count";
  476. return -EINVAL;
  477. }
  478. lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL);
  479. if (!lc) {
  480. ti->error = "Cannot allocate context";
  481. return -ENOMEM;
  482. }
  483. spin_lock_init(&lc->blocks_lock);
  484. INIT_LIST_HEAD(&lc->unflushed_blocks);
  485. INIT_LIST_HEAD(&lc->logging_blocks);
  486. init_waitqueue_head(&lc->wait);
  487. init_completion(&lc->super_done);
  488. atomic_set(&lc->io_blocks, 0);
  489. atomic_set(&lc->pending_blocks, 0);
  490. devname = dm_shift_arg(&as);
  491. ret = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev);
  492. if (ret) {
  493. ti->error = "Device lookup failed";
  494. goto bad;
  495. }
  496. logdevname = dm_shift_arg(&as);
  497. ret = dm_get_device(ti, logdevname, dm_table_get_mode(ti->table),
  498. &lc->logdev);
  499. if (ret) {
  500. ti->error = "Log device lookup failed";
  501. dm_put_device(ti, lc->dev);
  502. goto bad;
  503. }
  504. lc->sectorsize = bdev_logical_block_size(lc->dev->bdev);
  505. lc->sectorshift = ilog2(lc->sectorsize);
  506. lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write");
  507. if (IS_ERR(lc->log_kthread)) {
  508. ret = PTR_ERR(lc->log_kthread);
  509. ti->error = "Couldn't alloc kthread";
  510. dm_put_device(ti, lc->dev);
  511. dm_put_device(ti, lc->logdev);
  512. goto bad;
  513. }
  514. /*
  515. * next_sector is in 512b sectors to correspond to what bi_sector expects.
  516. * The super starts at sector 0, and the next_sector is the next logical
  517. * one based on the sectorsize of the device.
  518. */
  519. lc->next_sector = lc->sectorsize >> SECTOR_SHIFT;
  520. lc->logging_enabled = true;
  521. lc->end_sector = logdev_last_sector(lc);
  522. lc->device_supports_discard = true;
  523. ti->num_flush_bios = 1;
  524. ti->flush_supported = true;
  525. ti->num_discard_bios = 1;
  526. ti->discards_supported = true;
  527. ti->per_io_data_size = sizeof(struct per_bio_data);
  528. ti->private = lc;
  529. return 0;
  530. bad:
  531. kfree(lc);
  532. return ret;
  533. }
  534. static int log_mark(struct log_writes_c *lc, char *data)
  535. {
  536. struct pending_block *block;
  537. size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry);
  538. block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
  539. if (!block) {
  540. DMERR("Error allocating pending block");
  541. return -ENOMEM;
  542. }
  543. block->data = kstrndup(data, maxsize - 1, GFP_KERNEL);
  544. if (!block->data) {
  545. DMERR("Error copying mark data");
  546. kfree(block);
  547. return -ENOMEM;
  548. }
  549. atomic_inc(&lc->pending_blocks);
  550. block->datalen = strlen(block->data);
  551. block->flags |= LOG_MARK_FLAG;
  552. spin_lock_irq(&lc->blocks_lock);
  553. list_add_tail(&block->list, &lc->logging_blocks);
  554. spin_unlock_irq(&lc->blocks_lock);
  555. wake_up_process(lc->log_kthread);
  556. return 0;
  557. }
  558. static void log_writes_dtr(struct dm_target *ti)
  559. {
  560. struct log_writes_c *lc = ti->private;
  561. spin_lock_irq(&lc->blocks_lock);
  562. list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks);
  563. spin_unlock_irq(&lc->blocks_lock);
  564. /*
  565. * This is just nice to have since it'll update the super to include the
  566. * unflushed blocks, if it fails we don't really care.
  567. */
  568. log_mark(lc, "dm-log-writes-end");
  569. wake_up_process(lc->log_kthread);
  570. wait_event(lc->wait, !atomic_read(&lc->io_blocks) &&
  571. !atomic_read(&lc->pending_blocks));
  572. kthread_stop(lc->log_kthread);
  573. WARN_ON(!list_empty(&lc->logging_blocks));
  574. WARN_ON(!list_empty(&lc->unflushed_blocks));
  575. dm_put_device(ti, lc->dev);
  576. dm_put_device(ti, lc->logdev);
  577. kfree(lc);
  578. }
  579. static void normal_map_bio(struct dm_target *ti, struct bio *bio)
  580. {
  581. struct log_writes_c *lc = ti->private;
  582. bio_set_dev(bio, lc->dev->bdev);
  583. }
  584. static int log_writes_map(struct dm_target *ti, struct bio *bio)
  585. {
  586. struct log_writes_c *lc = ti->private;
  587. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  588. struct pending_block *block;
  589. struct bvec_iter iter;
  590. struct bio_vec bv;
  591. size_t alloc_size;
  592. int i = 0;
  593. bool flush_bio = (bio->bi_opf & REQ_PREFLUSH);
  594. bool fua_bio = (bio->bi_opf & REQ_FUA);
  595. bool discard_bio = (bio_op(bio) == REQ_OP_DISCARD);
  596. bool meta_bio = (bio->bi_opf & REQ_META);
  597. pb->block = NULL;
  598. /* Don't bother doing anything if logging has been disabled */
  599. if (!lc->logging_enabled)
  600. goto map_bio;
  601. /*
  602. * Map reads as normal.
  603. */
  604. if (bio_data_dir(bio) == READ)
  605. goto map_bio;
  606. /* No sectors and not a flush? Don't care */
  607. if (!bio_sectors(bio) && !flush_bio)
  608. goto map_bio;
  609. /*
  610. * Discards will have bi_size set but there's no actual data, so just
  611. * allocate the size of the pending block.
  612. */
  613. if (discard_bio)
  614. alloc_size = sizeof(struct pending_block);
  615. else
  616. alloc_size = struct_size(block, vecs, bio_segments(bio));
  617. block = kzalloc(alloc_size, GFP_NOIO);
  618. if (!block) {
  619. DMERR("Error allocating pending block");
  620. spin_lock_irq(&lc->blocks_lock);
  621. lc->logging_enabled = false;
  622. spin_unlock_irq(&lc->blocks_lock);
  623. return DM_MAPIO_KILL;
  624. }
  625. INIT_LIST_HEAD(&block->list);
  626. pb->block = block;
  627. atomic_inc(&lc->pending_blocks);
  628. if (flush_bio)
  629. block->flags |= LOG_FLUSH_FLAG;
  630. if (fua_bio)
  631. block->flags |= LOG_FUA_FLAG;
  632. if (discard_bio)
  633. block->flags |= LOG_DISCARD_FLAG;
  634. if (meta_bio)
  635. block->flags |= LOG_METADATA_FLAG;
  636. block->sector = bio_to_dev_sectors(lc, bio->bi_iter.bi_sector);
  637. block->nr_sectors = bio_to_dev_sectors(lc, bio_sectors(bio));
  638. /* We don't need the data, just submit */
  639. if (discard_bio) {
  640. WARN_ON(flush_bio || fua_bio);
  641. if (lc->device_supports_discard)
  642. goto map_bio;
  643. bio_endio(bio);
  644. return DM_MAPIO_SUBMITTED;
  645. }
  646. /* Flush bio, splice the unflushed blocks onto this list and submit */
  647. if (flush_bio && !bio_sectors(bio)) {
  648. spin_lock_irq(&lc->blocks_lock);
  649. list_splice_init(&lc->unflushed_blocks, &block->list);
  650. spin_unlock_irq(&lc->blocks_lock);
  651. goto map_bio;
  652. }
  653. /*
  654. * We will write this bio somewhere else way later so we need to copy
  655. * the actual contents into new pages so we know the data will always be
  656. * there.
  657. *
  658. * We do this because this could be a bio from O_DIRECT in which case we
  659. * can't just hold onto the page until some later point, we have to
  660. * manually copy the contents.
  661. */
  662. bio_for_each_segment(bv, bio, iter) {
  663. struct page *page;
  664. void *src, *dst;
  665. page = alloc_page(GFP_NOIO);
  666. if (!page) {
  667. DMERR("Error allocing page");
  668. free_pending_block(lc, block);
  669. spin_lock_irq(&lc->blocks_lock);
  670. lc->logging_enabled = false;
  671. spin_unlock_irq(&lc->blocks_lock);
  672. return DM_MAPIO_KILL;
  673. }
  674. src = kmap_atomic(bv.bv_page);
  675. dst = kmap_atomic(page);
  676. memcpy(dst, src + bv.bv_offset, bv.bv_len);
  677. kunmap_atomic(dst);
  678. kunmap_atomic(src);
  679. block->vecs[i].bv_page = page;
  680. block->vecs[i].bv_len = bv.bv_len;
  681. block->vec_cnt++;
  682. i++;
  683. }
  684. /* Had a flush with data in it, weird */
  685. if (flush_bio) {
  686. spin_lock_irq(&lc->blocks_lock);
  687. list_splice_init(&lc->unflushed_blocks, &block->list);
  688. spin_unlock_irq(&lc->blocks_lock);
  689. }
  690. map_bio:
  691. normal_map_bio(ti, bio);
  692. return DM_MAPIO_REMAPPED;
  693. }
  694. static int normal_end_io(struct dm_target *ti, struct bio *bio,
  695. blk_status_t *error)
  696. {
  697. struct log_writes_c *lc = ti->private;
  698. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  699. if (bio_data_dir(bio) == WRITE && pb->block) {
  700. struct pending_block *block = pb->block;
  701. unsigned long flags;
  702. spin_lock_irqsave(&lc->blocks_lock, flags);
  703. if (block->flags & LOG_FLUSH_FLAG) {
  704. list_splice_tail_init(&block->list, &lc->logging_blocks);
  705. list_add_tail(&block->list, &lc->logging_blocks);
  706. wake_up_process(lc->log_kthread);
  707. } else if (block->flags & LOG_FUA_FLAG) {
  708. list_add_tail(&block->list, &lc->logging_blocks);
  709. wake_up_process(lc->log_kthread);
  710. } else
  711. list_add_tail(&block->list, &lc->unflushed_blocks);
  712. spin_unlock_irqrestore(&lc->blocks_lock, flags);
  713. }
  714. return DM_ENDIO_DONE;
  715. }
  716. /*
  717. * INFO format: <logged entries> <highest allocated sector>
  718. */
  719. static void log_writes_status(struct dm_target *ti, status_type_t type,
  720. unsigned status_flags, char *result,
  721. unsigned maxlen)
  722. {
  723. unsigned sz = 0;
  724. struct log_writes_c *lc = ti->private;
  725. switch (type) {
  726. case STATUSTYPE_INFO:
  727. DMEMIT("%llu %llu", lc->logged_entries,
  728. (unsigned long long)lc->next_sector - 1);
  729. if (!lc->logging_enabled)
  730. DMEMIT(" logging_disabled");
  731. break;
  732. case STATUSTYPE_TABLE:
  733. DMEMIT("%s %s", lc->dev->name, lc->logdev->name);
  734. break;
  735. }
  736. }
  737. static int log_writes_prepare_ioctl(struct dm_target *ti,
  738. struct block_device **bdev)
  739. {
  740. struct log_writes_c *lc = ti->private;
  741. struct dm_dev *dev = lc->dev;
  742. *bdev = dev->bdev;
  743. /*
  744. * Only pass ioctls through if the device sizes match exactly.
  745. */
  746. if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  747. return 1;
  748. return 0;
  749. }
  750. static int log_writes_iterate_devices(struct dm_target *ti,
  751. iterate_devices_callout_fn fn,
  752. void *data)
  753. {
  754. struct log_writes_c *lc = ti->private;
  755. return fn(ti, lc->dev, 0, ti->len, data);
  756. }
  757. /*
  758. * Messages supported:
  759. * mark <mark data> - specify the marked data.
  760. */
  761. static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv,
  762. char *result, unsigned maxlen)
  763. {
  764. int r = -EINVAL;
  765. struct log_writes_c *lc = ti->private;
  766. if (argc != 2) {
  767. DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc);
  768. return r;
  769. }
  770. if (!strcasecmp(argv[0], "mark"))
  771. r = log_mark(lc, argv[1]);
  772. else
  773. DMWARN("Unrecognised log writes target message received: %s", argv[0]);
  774. return r;
  775. }
  776. static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits)
  777. {
  778. struct log_writes_c *lc = ti->private;
  779. struct request_queue *q = bdev_get_queue(lc->dev->bdev);
  780. if (!q || !blk_queue_discard(q)) {
  781. lc->device_supports_discard = false;
  782. limits->discard_granularity = lc->sectorsize;
  783. limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT);
  784. }
  785. limits->logical_block_size = bdev_logical_block_size(lc->dev->bdev);
  786. limits->physical_block_size = bdev_physical_block_size(lc->dev->bdev);
  787. limits->io_min = limits->physical_block_size;
  788. }
  789. #if IS_ENABLED(CONFIG_DAX_DRIVER)
  790. static int log_dax(struct log_writes_c *lc, sector_t sector, size_t bytes,
  791. struct iov_iter *i)
  792. {
  793. struct pending_block *block;
  794. if (!bytes)
  795. return 0;
  796. block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
  797. if (!block) {
  798. DMERR("Error allocating dax pending block");
  799. return -ENOMEM;
  800. }
  801. block->data = kzalloc(bytes, GFP_KERNEL);
  802. if (!block->data) {
  803. DMERR("Error allocating dax data space");
  804. kfree(block);
  805. return -ENOMEM;
  806. }
  807. /* write data provided via the iterator */
  808. if (!copy_from_iter(block->data, bytes, i)) {
  809. DMERR("Error copying dax data");
  810. kfree(block->data);
  811. kfree(block);
  812. return -EIO;
  813. }
  814. /* rewind the iterator so that the block driver can use it */
  815. iov_iter_revert(i, bytes);
  816. block->datalen = bytes;
  817. block->sector = bio_to_dev_sectors(lc, sector);
  818. block->nr_sectors = ALIGN(bytes, lc->sectorsize) >> lc->sectorshift;
  819. atomic_inc(&lc->pending_blocks);
  820. spin_lock_irq(&lc->blocks_lock);
  821. list_add_tail(&block->list, &lc->unflushed_blocks);
  822. spin_unlock_irq(&lc->blocks_lock);
  823. wake_up_process(lc->log_kthread);
  824. return 0;
  825. }
  826. static long log_writes_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
  827. long nr_pages, void **kaddr, pfn_t *pfn)
  828. {
  829. struct log_writes_c *lc = ti->private;
  830. sector_t sector = pgoff * PAGE_SECTORS;
  831. int ret;
  832. ret = bdev_dax_pgoff(lc->dev->bdev, sector, nr_pages * PAGE_SIZE, &pgoff);
  833. if (ret)
  834. return ret;
  835. return dax_direct_access(lc->dev->dax_dev, pgoff, nr_pages, kaddr, pfn);
  836. }
  837. static size_t log_writes_dax_copy_from_iter(struct dm_target *ti,
  838. pgoff_t pgoff, void *addr, size_t bytes,
  839. struct iov_iter *i)
  840. {
  841. struct log_writes_c *lc = ti->private;
  842. sector_t sector = pgoff * PAGE_SECTORS;
  843. int err;
  844. if (bdev_dax_pgoff(lc->dev->bdev, sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  845. return 0;
  846. /* Don't bother doing anything if logging has been disabled */
  847. if (!lc->logging_enabled)
  848. goto dax_copy;
  849. err = log_dax(lc, sector, bytes, i);
  850. if (err) {
  851. DMWARN("Error %d logging DAX write", err);
  852. return 0;
  853. }
  854. dax_copy:
  855. return dax_copy_from_iter(lc->dev->dax_dev, pgoff, addr, bytes, i);
  856. }
  857. static size_t log_writes_dax_copy_to_iter(struct dm_target *ti,
  858. pgoff_t pgoff, void *addr, size_t bytes,
  859. struct iov_iter *i)
  860. {
  861. struct log_writes_c *lc = ti->private;
  862. sector_t sector = pgoff * PAGE_SECTORS;
  863. if (bdev_dax_pgoff(lc->dev->bdev, sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  864. return 0;
  865. return dax_copy_to_iter(lc->dev->dax_dev, pgoff, addr, bytes, i);
  866. }
  867. #else
  868. #define log_writes_dax_direct_access NULL
  869. #define log_writes_dax_copy_from_iter NULL
  870. #define log_writes_dax_copy_to_iter NULL
  871. #endif
  872. static struct target_type log_writes_target = {
  873. .name = "log-writes",
  874. .version = {1, 1, 0},
  875. .module = THIS_MODULE,
  876. .ctr = log_writes_ctr,
  877. .dtr = log_writes_dtr,
  878. .map = log_writes_map,
  879. .end_io = normal_end_io,
  880. .status = log_writes_status,
  881. .prepare_ioctl = log_writes_prepare_ioctl,
  882. .message = log_writes_message,
  883. .iterate_devices = log_writes_iterate_devices,
  884. .io_hints = log_writes_io_hints,
  885. .direct_access = log_writes_dax_direct_access,
  886. .dax_copy_from_iter = log_writes_dax_copy_from_iter,
  887. .dax_copy_to_iter = log_writes_dax_copy_to_iter,
  888. };
  889. static int __init dm_log_writes_init(void)
  890. {
  891. int r = dm_register_target(&log_writes_target);
  892. if (r < 0)
  893. DMERR("register failed %d", r);
  894. return r;
  895. }
  896. static void __exit dm_log_writes_exit(void)
  897. {
  898. dm_unregister_target(&log_writes_target);
  899. }
  900. module_init(dm_log_writes_init);
  901. module_exit(dm_log_writes_exit);
  902. MODULE_DESCRIPTION(DM_NAME " log writes target");
  903. MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
  904. MODULE_LICENSE("GPL");