dm-log-writes.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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/slab.h>
  12. #include <linux/kthread.h>
  13. #include <linux/freezer.h>
  14. #define DM_MSG_PREFIX "log-writes"
  15. /*
  16. * This target will sequentially log all writes to the target device onto the
  17. * log device. This is helpful for replaying writes to check for fs consistency
  18. * at all times. This target provides a mechanism to mark specific events to
  19. * check data at a later time. So for example you would:
  20. *
  21. * write data
  22. * fsync
  23. * dmsetup message /dev/whatever mark mymark
  24. * unmount /mnt/test
  25. *
  26. * Then replay the log up to mymark and check the contents of the replay to
  27. * verify it matches what was written.
  28. *
  29. * We log writes only after they have been flushed, this makes the log describe
  30. * close to the order in which the data hits the actual disk, not its cache. So
  31. * for example the following sequence (W means write, C means complete)
  32. *
  33. * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd
  34. *
  35. * Would result in the log looking like this:
  36. *
  37. * c,a,flush,fuad,b,<other writes>,<next flush>
  38. *
  39. * This is meant to help expose problems where file systems do not properly wait
  40. * on data being written before invoking a FLUSH. FUA bypasses cache so once it
  41. * completes it is added to the log as it should be on disk.
  42. *
  43. * We treat DISCARDs as if they don't bypass cache so that they are logged in
  44. * order of completion along with the normal writes. If we didn't do it this
  45. * way we would process all the discards first and then write all the data, when
  46. * in fact we want to do the data and the discard in the order that they
  47. * completed.
  48. */
  49. #define LOG_FLUSH_FLAG (1 << 0)
  50. #define LOG_FUA_FLAG (1 << 1)
  51. #define LOG_DISCARD_FLAG (1 << 2)
  52. #define LOG_MARK_FLAG (1 << 3)
  53. #define WRITE_LOG_VERSION 1ULL
  54. #define WRITE_LOG_MAGIC 0x6a736677736872ULL
  55. /*
  56. * The disk format for this is braindead simple.
  57. *
  58. * At byte 0 we have our super, followed by the following sequence for
  59. * nr_entries:
  60. *
  61. * [ 1 sector ][ entry->nr_sectors ]
  62. * [log_write_entry][ data written ]
  63. *
  64. * The log_write_entry takes up a full sector so we can have arbitrary length
  65. * marks and it leaves us room for extra content in the future.
  66. */
  67. /*
  68. * Basic info about the log for userspace.
  69. */
  70. struct log_write_super {
  71. __le64 magic;
  72. __le64 version;
  73. __le64 nr_entries;
  74. __le32 sectorsize;
  75. };
  76. /*
  77. * sector - the sector we wrote.
  78. * nr_sectors - the number of sectors we wrote.
  79. * flags - flags for this log entry.
  80. * data_len - the size of the data in this log entry, this is for private log
  81. * entry stuff, the MARK data provided by userspace for example.
  82. */
  83. struct log_write_entry {
  84. __le64 sector;
  85. __le64 nr_sectors;
  86. __le64 flags;
  87. __le64 data_len;
  88. };
  89. struct log_writes_c {
  90. struct dm_dev *dev;
  91. struct dm_dev *logdev;
  92. u64 logged_entries;
  93. u32 sectorsize;
  94. atomic_t io_blocks;
  95. atomic_t pending_blocks;
  96. sector_t next_sector;
  97. sector_t end_sector;
  98. bool logging_enabled;
  99. bool device_supports_discard;
  100. spinlock_t blocks_lock;
  101. struct list_head unflushed_blocks;
  102. struct list_head logging_blocks;
  103. wait_queue_head_t wait;
  104. struct task_struct *log_kthread;
  105. };
  106. struct pending_block {
  107. int vec_cnt;
  108. u64 flags;
  109. sector_t sector;
  110. sector_t nr_sectors;
  111. char *data;
  112. u32 datalen;
  113. struct list_head list;
  114. struct bio_vec vecs[0];
  115. };
  116. struct per_bio_data {
  117. struct pending_block *block;
  118. };
  119. static void put_pending_block(struct log_writes_c *lc)
  120. {
  121. if (atomic_dec_and_test(&lc->pending_blocks)) {
  122. smp_mb__after_atomic();
  123. if (waitqueue_active(&lc->wait))
  124. wake_up(&lc->wait);
  125. }
  126. }
  127. static void put_io_block(struct log_writes_c *lc)
  128. {
  129. if (atomic_dec_and_test(&lc->io_blocks)) {
  130. smp_mb__after_atomic();
  131. if (waitqueue_active(&lc->wait))
  132. wake_up(&lc->wait);
  133. }
  134. }
  135. static void log_end_io(struct bio *bio, int err)
  136. {
  137. struct log_writes_c *lc = bio->bi_private;
  138. struct bio_vec *bvec;
  139. int i;
  140. if (err) {
  141. unsigned long flags;
  142. DMERR("Error writing log block, error=%d", err);
  143. spin_lock_irqsave(&lc->blocks_lock, flags);
  144. lc->logging_enabled = false;
  145. spin_unlock_irqrestore(&lc->blocks_lock, flags);
  146. }
  147. bio_for_each_segment_all(bvec, bio, i)
  148. __free_page(bvec->bv_page);
  149. put_io_block(lc);
  150. bio_put(bio);
  151. }
  152. /*
  153. * Meant to be called if there is an error, it will free all the pages
  154. * associated with the block.
  155. */
  156. static void free_pending_block(struct log_writes_c *lc,
  157. struct pending_block *block)
  158. {
  159. int i;
  160. for (i = 0; i < block->vec_cnt; i++) {
  161. if (block->vecs[i].bv_page)
  162. __free_page(block->vecs[i].bv_page);
  163. }
  164. kfree(block->data);
  165. kfree(block);
  166. put_pending_block(lc);
  167. }
  168. static int write_metadata(struct log_writes_c *lc, void *entry,
  169. size_t entrylen, void *data, size_t datalen,
  170. sector_t sector)
  171. {
  172. struct bio *bio;
  173. struct page *page;
  174. void *ptr;
  175. size_t ret;
  176. bio = bio_alloc(GFP_KERNEL, 1);
  177. if (!bio) {
  178. DMERR("Couldn't alloc log bio");
  179. goto error;
  180. }
  181. bio->bi_iter.bi_size = 0;
  182. bio->bi_iter.bi_sector = sector;
  183. bio->bi_bdev = lc->logdev->bdev;
  184. bio->bi_end_io = log_end_io;
  185. bio->bi_private = lc;
  186. set_bit(BIO_UPTODATE, &bio->bi_flags);
  187. page = alloc_page(GFP_KERNEL);
  188. if (!page) {
  189. DMERR("Couldn't alloc log page");
  190. bio_put(bio);
  191. goto error;
  192. }
  193. ptr = kmap_atomic(page);
  194. memcpy(ptr, entry, entrylen);
  195. if (datalen)
  196. memcpy(ptr + entrylen, data, datalen);
  197. memset(ptr + entrylen + datalen, 0,
  198. lc->sectorsize - entrylen - datalen);
  199. kunmap_atomic(ptr);
  200. ret = bio_add_page(bio, page, lc->sectorsize, 0);
  201. if (ret != lc->sectorsize) {
  202. DMERR("Couldn't add page to the log block");
  203. goto error_bio;
  204. }
  205. submit_bio(WRITE, bio);
  206. return 0;
  207. error_bio:
  208. bio_put(bio);
  209. __free_page(page);
  210. error:
  211. put_io_block(lc);
  212. return -1;
  213. }
  214. static int log_one_block(struct log_writes_c *lc,
  215. struct pending_block *block, sector_t sector)
  216. {
  217. struct bio *bio;
  218. struct log_write_entry entry;
  219. size_t ret;
  220. int i;
  221. entry.sector = cpu_to_le64(block->sector);
  222. entry.nr_sectors = cpu_to_le64(block->nr_sectors);
  223. entry.flags = cpu_to_le64(block->flags);
  224. entry.data_len = cpu_to_le64(block->datalen);
  225. if (write_metadata(lc, &entry, sizeof(entry), block->data,
  226. block->datalen, sector)) {
  227. free_pending_block(lc, block);
  228. return -1;
  229. }
  230. if (!block->vec_cnt)
  231. goto out;
  232. sector++;
  233. bio = bio_alloc(GFP_KERNEL, block->vec_cnt);
  234. if (!bio) {
  235. DMERR("Couldn't alloc log bio");
  236. goto error;
  237. }
  238. atomic_inc(&lc->io_blocks);
  239. bio->bi_iter.bi_size = 0;
  240. bio->bi_iter.bi_sector = sector;
  241. bio->bi_bdev = lc->logdev->bdev;
  242. bio->bi_end_io = log_end_io;
  243. bio->bi_private = lc;
  244. set_bit(BIO_UPTODATE, &bio->bi_flags);
  245. for (i = 0; i < block->vec_cnt; i++) {
  246. /*
  247. * The page offset is always 0 because we allocate a new page
  248. * for every bvec in the original bio for simplicity sake.
  249. */
  250. ret = bio_add_page(bio, block->vecs[i].bv_page,
  251. block->vecs[i].bv_len, 0);
  252. if (ret != block->vecs[i].bv_len) {
  253. atomic_inc(&lc->io_blocks);
  254. submit_bio(WRITE, bio);
  255. bio = bio_alloc(GFP_KERNEL, block->vec_cnt - i);
  256. if (!bio) {
  257. DMERR("Couldn't alloc log bio");
  258. goto error;
  259. }
  260. bio->bi_iter.bi_size = 0;
  261. bio->bi_iter.bi_sector = sector;
  262. bio->bi_bdev = lc->logdev->bdev;
  263. bio->bi_end_io = log_end_io;
  264. bio->bi_private = lc;
  265. set_bit(BIO_UPTODATE, &bio->bi_flags);
  266. ret = bio_add_page(bio, block->vecs[i].bv_page,
  267. block->vecs[i].bv_len, 0);
  268. if (ret != block->vecs[i].bv_len) {
  269. DMERR("Couldn't add page on new bio?");
  270. bio_put(bio);
  271. goto error;
  272. }
  273. }
  274. sector += block->vecs[i].bv_len >> SECTOR_SHIFT;
  275. }
  276. submit_bio(WRITE, bio);
  277. out:
  278. kfree(block->data);
  279. kfree(block);
  280. put_pending_block(lc);
  281. return 0;
  282. error:
  283. free_pending_block(lc, block);
  284. put_io_block(lc);
  285. return -1;
  286. }
  287. static int log_super(struct log_writes_c *lc)
  288. {
  289. struct log_write_super super;
  290. super.magic = cpu_to_le64(WRITE_LOG_MAGIC);
  291. super.version = cpu_to_le64(WRITE_LOG_VERSION);
  292. super.nr_entries = cpu_to_le64(lc->logged_entries);
  293. super.sectorsize = cpu_to_le32(lc->sectorsize);
  294. if (write_metadata(lc, &super, sizeof(super), NULL, 0, 0)) {
  295. DMERR("Couldn't write super");
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. static inline sector_t logdev_last_sector(struct log_writes_c *lc)
  301. {
  302. return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT;
  303. }
  304. static int log_writes_kthread(void *arg)
  305. {
  306. struct log_writes_c *lc = (struct log_writes_c *)arg;
  307. sector_t sector = 0;
  308. while (!kthread_should_stop()) {
  309. bool super = false;
  310. bool logging_enabled;
  311. struct pending_block *block = NULL;
  312. int ret;
  313. spin_lock_irq(&lc->blocks_lock);
  314. if (!list_empty(&lc->logging_blocks)) {
  315. block = list_first_entry(&lc->logging_blocks,
  316. struct pending_block, list);
  317. list_del_init(&block->list);
  318. if (!lc->logging_enabled)
  319. goto next;
  320. sector = lc->next_sector;
  321. if (block->flags & LOG_DISCARD_FLAG)
  322. lc->next_sector++;
  323. else
  324. lc->next_sector += block->nr_sectors + 1;
  325. /*
  326. * Apparently the size of the device may not be known
  327. * right away, so handle this properly.
  328. */
  329. if (!lc->end_sector)
  330. lc->end_sector = logdev_last_sector(lc);
  331. if (lc->end_sector &&
  332. lc->next_sector >= lc->end_sector) {
  333. DMERR("Ran out of space on the logdev");
  334. lc->logging_enabled = false;
  335. goto next;
  336. }
  337. lc->logged_entries++;
  338. atomic_inc(&lc->io_blocks);
  339. super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG));
  340. if (super)
  341. atomic_inc(&lc->io_blocks);
  342. }
  343. next:
  344. logging_enabled = lc->logging_enabled;
  345. spin_unlock_irq(&lc->blocks_lock);
  346. if (block) {
  347. if (logging_enabled) {
  348. ret = log_one_block(lc, block, sector);
  349. if (!ret && super)
  350. ret = log_super(lc);
  351. if (ret) {
  352. spin_lock_irq(&lc->blocks_lock);
  353. lc->logging_enabled = false;
  354. spin_unlock_irq(&lc->blocks_lock);
  355. }
  356. } else
  357. free_pending_block(lc, block);
  358. continue;
  359. }
  360. if (!try_to_freeze()) {
  361. set_current_state(TASK_INTERRUPTIBLE);
  362. if (!kthread_should_stop() &&
  363. !atomic_read(&lc->pending_blocks))
  364. schedule();
  365. __set_current_state(TASK_RUNNING);
  366. }
  367. }
  368. return 0;
  369. }
  370. /*
  371. * Construct a log-writes mapping:
  372. * log-writes <dev_path> <log_dev_path>
  373. */
  374. static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  375. {
  376. struct log_writes_c *lc;
  377. struct dm_arg_set as;
  378. const char *devname, *logdevname;
  379. as.argc = argc;
  380. as.argv = argv;
  381. if (argc < 2) {
  382. ti->error = "Invalid argument count";
  383. return -EINVAL;
  384. }
  385. lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL);
  386. if (!lc) {
  387. ti->error = "Cannot allocate context";
  388. return -ENOMEM;
  389. }
  390. spin_lock_init(&lc->blocks_lock);
  391. INIT_LIST_HEAD(&lc->unflushed_blocks);
  392. INIT_LIST_HEAD(&lc->logging_blocks);
  393. init_waitqueue_head(&lc->wait);
  394. lc->sectorsize = 1 << SECTOR_SHIFT;
  395. atomic_set(&lc->io_blocks, 0);
  396. atomic_set(&lc->pending_blocks, 0);
  397. devname = dm_shift_arg(&as);
  398. if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev)) {
  399. ti->error = "Device lookup failed";
  400. goto bad;
  401. }
  402. logdevname = dm_shift_arg(&as);
  403. if (dm_get_device(ti, logdevname, dm_table_get_mode(ti->table), &lc->logdev)) {
  404. ti->error = "Log device lookup failed";
  405. dm_put_device(ti, lc->dev);
  406. goto bad;
  407. }
  408. lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write");
  409. if (!lc->log_kthread) {
  410. ti->error = "Couldn't alloc kthread";
  411. dm_put_device(ti, lc->dev);
  412. dm_put_device(ti, lc->logdev);
  413. goto bad;
  414. }
  415. /* We put the super at sector 0, start logging at sector 1 */
  416. lc->next_sector = 1;
  417. lc->logging_enabled = true;
  418. lc->end_sector = logdev_last_sector(lc);
  419. lc->device_supports_discard = true;
  420. ti->num_flush_bios = 1;
  421. ti->flush_supported = true;
  422. ti->num_discard_bios = 1;
  423. ti->discards_supported = true;
  424. ti->per_bio_data_size = sizeof(struct per_bio_data);
  425. ti->private = lc;
  426. return 0;
  427. bad:
  428. kfree(lc);
  429. return -EINVAL;
  430. }
  431. static int log_mark(struct log_writes_c *lc, char *data)
  432. {
  433. struct pending_block *block;
  434. size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry);
  435. block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
  436. if (!block) {
  437. DMERR("Error allocating pending block");
  438. return -ENOMEM;
  439. }
  440. block->data = kstrndup(data, maxsize, GFP_KERNEL);
  441. if (!block->data) {
  442. DMERR("Error copying mark data");
  443. kfree(block);
  444. return -ENOMEM;
  445. }
  446. atomic_inc(&lc->pending_blocks);
  447. block->datalen = strlen(block->data);
  448. block->flags |= LOG_MARK_FLAG;
  449. spin_lock_irq(&lc->blocks_lock);
  450. list_add_tail(&block->list, &lc->logging_blocks);
  451. spin_unlock_irq(&lc->blocks_lock);
  452. wake_up_process(lc->log_kthread);
  453. return 0;
  454. }
  455. static void log_writes_dtr(struct dm_target *ti)
  456. {
  457. struct log_writes_c *lc = ti->private;
  458. spin_lock_irq(&lc->blocks_lock);
  459. list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks);
  460. spin_unlock_irq(&lc->blocks_lock);
  461. /*
  462. * This is just nice to have since it'll update the super to include the
  463. * unflushed blocks, if it fails we don't really care.
  464. */
  465. log_mark(lc, "dm-log-writes-end");
  466. wake_up_process(lc->log_kthread);
  467. wait_event(lc->wait, !atomic_read(&lc->io_blocks) &&
  468. !atomic_read(&lc->pending_blocks));
  469. kthread_stop(lc->log_kthread);
  470. WARN_ON(!list_empty(&lc->logging_blocks));
  471. WARN_ON(!list_empty(&lc->unflushed_blocks));
  472. dm_put_device(ti, lc->dev);
  473. dm_put_device(ti, lc->logdev);
  474. kfree(lc);
  475. }
  476. static void normal_map_bio(struct dm_target *ti, struct bio *bio)
  477. {
  478. struct log_writes_c *lc = ti->private;
  479. bio->bi_bdev = lc->dev->bdev;
  480. }
  481. static int log_writes_map(struct dm_target *ti, struct bio *bio)
  482. {
  483. struct log_writes_c *lc = ti->private;
  484. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  485. struct pending_block *block;
  486. struct bvec_iter iter;
  487. struct bio_vec bv;
  488. size_t alloc_size;
  489. int i = 0;
  490. bool flush_bio = (bio->bi_rw & REQ_FLUSH);
  491. bool fua_bio = (bio->bi_rw & REQ_FUA);
  492. bool discard_bio = (bio->bi_rw & REQ_DISCARD);
  493. pb->block = NULL;
  494. /* Don't bother doing anything if logging has been disabled */
  495. if (!lc->logging_enabled)
  496. goto map_bio;
  497. /*
  498. * Map reads as normal.
  499. */
  500. if (bio_data_dir(bio) == READ)
  501. goto map_bio;
  502. /* No sectors and not a flush? Don't care */
  503. if (!bio_sectors(bio) && !flush_bio)
  504. goto map_bio;
  505. /*
  506. * Discards will have bi_size set but there's no actual data, so just
  507. * allocate the size of the pending block.
  508. */
  509. if (discard_bio)
  510. alloc_size = sizeof(struct pending_block);
  511. else
  512. alloc_size = sizeof(struct pending_block) + sizeof(struct bio_vec) * bio_segments(bio);
  513. block = kzalloc(alloc_size, GFP_NOIO);
  514. if (!block) {
  515. DMERR("Error allocating pending block");
  516. spin_lock_irq(&lc->blocks_lock);
  517. lc->logging_enabled = false;
  518. spin_unlock_irq(&lc->blocks_lock);
  519. return -ENOMEM;
  520. }
  521. INIT_LIST_HEAD(&block->list);
  522. pb->block = block;
  523. atomic_inc(&lc->pending_blocks);
  524. if (flush_bio)
  525. block->flags |= LOG_FLUSH_FLAG;
  526. if (fua_bio)
  527. block->flags |= LOG_FUA_FLAG;
  528. if (discard_bio)
  529. block->flags |= LOG_DISCARD_FLAG;
  530. block->sector = bio->bi_iter.bi_sector;
  531. block->nr_sectors = bio_sectors(bio);
  532. /* We don't need the data, just submit */
  533. if (discard_bio) {
  534. WARN_ON(flush_bio || fua_bio);
  535. if (lc->device_supports_discard)
  536. goto map_bio;
  537. bio_endio(bio, 0);
  538. return DM_MAPIO_SUBMITTED;
  539. }
  540. /* Flush bio, splice the unflushed blocks onto this list and submit */
  541. if (flush_bio && !bio_sectors(bio)) {
  542. spin_lock_irq(&lc->blocks_lock);
  543. list_splice_init(&lc->unflushed_blocks, &block->list);
  544. spin_unlock_irq(&lc->blocks_lock);
  545. goto map_bio;
  546. }
  547. /*
  548. * We will write this bio somewhere else way later so we need to copy
  549. * the actual contents into new pages so we know the data will always be
  550. * there.
  551. *
  552. * We do this because this could be a bio from O_DIRECT in which case we
  553. * can't just hold onto the page until some later point, we have to
  554. * manually copy the contents.
  555. */
  556. bio_for_each_segment(bv, bio, iter) {
  557. struct page *page;
  558. void *src, *dst;
  559. page = alloc_page(GFP_NOIO);
  560. if (!page) {
  561. DMERR("Error allocing page");
  562. free_pending_block(lc, block);
  563. spin_lock_irq(&lc->blocks_lock);
  564. lc->logging_enabled = false;
  565. spin_unlock_irq(&lc->blocks_lock);
  566. return -ENOMEM;
  567. }
  568. src = kmap_atomic(bv.bv_page);
  569. dst = kmap_atomic(page);
  570. memcpy(dst, src + bv.bv_offset, bv.bv_len);
  571. kunmap_atomic(dst);
  572. kunmap_atomic(src);
  573. block->vecs[i].bv_page = page;
  574. block->vecs[i].bv_len = bv.bv_len;
  575. block->vec_cnt++;
  576. i++;
  577. }
  578. /* Had a flush with data in it, weird */
  579. if (flush_bio) {
  580. spin_lock_irq(&lc->blocks_lock);
  581. list_splice_init(&lc->unflushed_blocks, &block->list);
  582. spin_unlock_irq(&lc->blocks_lock);
  583. }
  584. map_bio:
  585. normal_map_bio(ti, bio);
  586. return DM_MAPIO_REMAPPED;
  587. }
  588. static int normal_end_io(struct dm_target *ti, struct bio *bio, int error)
  589. {
  590. struct log_writes_c *lc = ti->private;
  591. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  592. if (bio_data_dir(bio) == WRITE && pb->block) {
  593. struct pending_block *block = pb->block;
  594. unsigned long flags;
  595. spin_lock_irqsave(&lc->blocks_lock, flags);
  596. if (block->flags & LOG_FLUSH_FLAG) {
  597. list_splice_tail_init(&block->list, &lc->logging_blocks);
  598. list_add_tail(&block->list, &lc->logging_blocks);
  599. wake_up_process(lc->log_kthread);
  600. } else if (block->flags & LOG_FUA_FLAG) {
  601. list_add_tail(&block->list, &lc->logging_blocks);
  602. wake_up_process(lc->log_kthread);
  603. } else
  604. list_add_tail(&block->list, &lc->unflushed_blocks);
  605. spin_unlock_irqrestore(&lc->blocks_lock, flags);
  606. }
  607. return error;
  608. }
  609. /*
  610. * INFO format: <logged entries> <highest allocated sector>
  611. */
  612. static void log_writes_status(struct dm_target *ti, status_type_t type,
  613. unsigned status_flags, char *result,
  614. unsigned maxlen)
  615. {
  616. unsigned sz = 0;
  617. struct log_writes_c *lc = ti->private;
  618. switch (type) {
  619. case STATUSTYPE_INFO:
  620. DMEMIT("%llu %llu", lc->logged_entries,
  621. (unsigned long long)lc->next_sector - 1);
  622. if (!lc->logging_enabled)
  623. DMEMIT(" logging_disabled");
  624. break;
  625. case STATUSTYPE_TABLE:
  626. DMEMIT("%s %s", lc->dev->name, lc->logdev->name);
  627. break;
  628. }
  629. }
  630. static int log_writes_ioctl(struct dm_target *ti, unsigned int cmd,
  631. unsigned long arg)
  632. {
  633. struct log_writes_c *lc = ti->private;
  634. struct dm_dev *dev = lc->dev;
  635. int r = 0;
  636. /*
  637. * Only pass ioctls through if the device sizes match exactly.
  638. */
  639. if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  640. r = scsi_verify_blk_ioctl(NULL, cmd);
  641. return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg);
  642. }
  643. static int log_writes_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  644. struct bio_vec *biovec, int max_size)
  645. {
  646. struct log_writes_c *lc = ti->private;
  647. struct request_queue *q = bdev_get_queue(lc->dev->bdev);
  648. if (!q->merge_bvec_fn)
  649. return max_size;
  650. bvm->bi_bdev = lc->dev->bdev;
  651. bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
  652. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  653. }
  654. static int log_writes_iterate_devices(struct dm_target *ti,
  655. iterate_devices_callout_fn fn,
  656. void *data)
  657. {
  658. struct log_writes_c *lc = ti->private;
  659. return fn(ti, lc->dev, 0, ti->len, data);
  660. }
  661. /*
  662. * Messages supported:
  663. * mark <mark data> - specify the marked data.
  664. */
  665. static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv)
  666. {
  667. int r = -EINVAL;
  668. struct log_writes_c *lc = ti->private;
  669. if (argc != 2) {
  670. DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc);
  671. return r;
  672. }
  673. if (!strcasecmp(argv[0], "mark"))
  674. r = log_mark(lc, argv[1]);
  675. else
  676. DMWARN("Unrecognised log writes target message received: %s", argv[0]);
  677. return r;
  678. }
  679. static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits)
  680. {
  681. struct log_writes_c *lc = ti->private;
  682. struct request_queue *q = bdev_get_queue(lc->dev->bdev);
  683. if (!q || !blk_queue_discard(q)) {
  684. lc->device_supports_discard = false;
  685. limits->discard_granularity = 1 << SECTOR_SHIFT;
  686. limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT);
  687. }
  688. }
  689. static struct target_type log_writes_target = {
  690. .name = "log-writes",
  691. .version = {1, 0, 0},
  692. .module = THIS_MODULE,
  693. .ctr = log_writes_ctr,
  694. .dtr = log_writes_dtr,
  695. .map = log_writes_map,
  696. .end_io = normal_end_io,
  697. .status = log_writes_status,
  698. .ioctl = log_writes_ioctl,
  699. .merge = log_writes_merge,
  700. .message = log_writes_message,
  701. .iterate_devices = log_writes_iterate_devices,
  702. .io_hints = log_writes_io_hints,
  703. };
  704. static int __init dm_log_writes_init(void)
  705. {
  706. int r = dm_register_target(&log_writes_target);
  707. if (r < 0)
  708. DMERR("register failed %d", r);
  709. return r;
  710. }
  711. static void __exit dm_log_writes_exit(void)
  712. {
  713. dm_unregister_target(&log_writes_target);
  714. }
  715. module_init(dm_log_writes_init);
  716. module_exit(dm_log_writes_exit);
  717. MODULE_DESCRIPTION(DM_NAME " log writes target");
  718. MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
  719. MODULE_LICENSE("GPL");