xref: /linux/drivers/md/dm-log-writes.c (revision 27db2717085198862a5b96dc8f00e527bf45c950)
10e9cebe7SJosef Bacik /*
20e9cebe7SJosef Bacik  * Copyright (C) 2014 Facebook. All rights reserved.
30e9cebe7SJosef Bacik  *
40e9cebe7SJosef Bacik  * This file is released under the GPL.
50e9cebe7SJosef Bacik  */
60e9cebe7SJosef Bacik 
70e9cebe7SJosef Bacik #include <linux/device-mapper.h>
80e9cebe7SJosef Bacik 
90e9cebe7SJosef Bacik #include <linux/module.h>
100e9cebe7SJosef Bacik #include <linux/init.h>
110e9cebe7SJosef Bacik #include <linux/blkdev.h>
120e9cebe7SJosef Bacik #include <linux/bio.h>
1398d82f48SRoss Zwisler #include <linux/dax.h>
140e9cebe7SJosef Bacik #include <linux/slab.h>
150e9cebe7SJosef Bacik #include <linux/kthread.h>
160e9cebe7SJosef Bacik #include <linux/freezer.h>
1798d82f48SRoss Zwisler #include <linux/uio.h>
180e9cebe7SJosef Bacik 
190e9cebe7SJosef Bacik #define DM_MSG_PREFIX "log-writes"
200e9cebe7SJosef Bacik 
210e9cebe7SJosef Bacik /*
220e9cebe7SJosef Bacik  * This target will sequentially log all writes to the target device onto the
230e9cebe7SJosef Bacik  * log device.  This is helpful for replaying writes to check for fs consistency
240e9cebe7SJosef Bacik  * at all times.  This target provides a mechanism to mark specific events to
250e9cebe7SJosef Bacik  * check data at a later time.  So for example you would:
260e9cebe7SJosef Bacik  *
270e9cebe7SJosef Bacik  * write data
280e9cebe7SJosef Bacik  * fsync
290e9cebe7SJosef Bacik  * dmsetup message /dev/whatever mark mymark
300e9cebe7SJosef Bacik  * unmount /mnt/test
310e9cebe7SJosef Bacik  *
320e9cebe7SJosef Bacik  * Then replay the log up to mymark and check the contents of the replay to
330e9cebe7SJosef Bacik  * verify it matches what was written.
340e9cebe7SJosef Bacik  *
350e9cebe7SJosef Bacik  * We log writes only after they have been flushed, this makes the log describe
360e9cebe7SJosef Bacik  * close to the order in which the data hits the actual disk, not its cache.  So
370e9cebe7SJosef Bacik  * for example the following sequence (W means write, C means complete)
380e9cebe7SJosef Bacik  *
390e9cebe7SJosef Bacik  * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd
400e9cebe7SJosef Bacik  *
410e9cebe7SJosef Bacik  * Would result in the log looking like this:
420e9cebe7SJosef Bacik  *
437537dad7SQu Wenruo  * c,a,b,flush,fuad,<other writes>,<next flush>
440e9cebe7SJosef Bacik  *
450e9cebe7SJosef Bacik  * This is meant to help expose problems where file systems do not properly wait
460e9cebe7SJosef Bacik  * on data being written before invoking a FLUSH.  FUA bypasses cache so once it
470e9cebe7SJosef Bacik  * completes it is added to the log as it should be on disk.
480e9cebe7SJosef Bacik  *
490e9cebe7SJosef Bacik  * We treat DISCARDs as if they don't bypass cache so that they are logged in
500e9cebe7SJosef Bacik  * order of completion along with the normal writes.  If we didn't do it this
510e9cebe7SJosef Bacik  * way we would process all the discards first and then write all the data, when
520e9cebe7SJosef Bacik  * in fact we want to do the data and the discard in the order that they
530e9cebe7SJosef Bacik  * completed.
540e9cebe7SJosef Bacik  */
550e9cebe7SJosef Bacik #define LOG_FLUSH_FLAG		(1 << 0)
560e9cebe7SJosef Bacik #define LOG_FUA_FLAG		(1 << 1)
570e9cebe7SJosef Bacik #define LOG_DISCARD_FLAG	(1 << 2)
580e9cebe7SJosef Bacik #define LOG_MARK_FLAG		(1 << 3)
59e5c4cb9bSQu Wenruo #define LOG_METADATA_FLAG	(1 << 4)
600e9cebe7SJosef Bacik 
61f4ad317aSGeert Uytterhoeven #define WRITE_LOG_VERSION 1ULL
62f4ad317aSGeert Uytterhoeven #define WRITE_LOG_MAGIC 0x6a736677736872ULL
63211ad4b7Szhangyi (F) #define WRITE_LOG_SUPER_SECTOR 0
640e9cebe7SJosef Bacik 
650e9cebe7SJosef Bacik /*
660e9cebe7SJosef Bacik  * The disk format for this is braindead simple.
670e9cebe7SJosef Bacik  *
680e9cebe7SJosef Bacik  * At byte 0 we have our super, followed by the following sequence for
690e9cebe7SJosef Bacik  * nr_entries:
700e9cebe7SJosef Bacik  *
710e9cebe7SJosef Bacik  * [   1 sector    ][  entry->nr_sectors ]
720e9cebe7SJosef Bacik  * [log_write_entry][    data written    ]
730e9cebe7SJosef Bacik  *
740e9cebe7SJosef Bacik  * The log_write_entry takes up a full sector so we can have arbitrary length
750e9cebe7SJosef Bacik  * marks and it leaves us room for extra content in the future.
760e9cebe7SJosef Bacik  */
770e9cebe7SJosef Bacik 
780e9cebe7SJosef Bacik /*
790e9cebe7SJosef Bacik  * Basic info about the log for userspace.
800e9cebe7SJosef Bacik  */
810e9cebe7SJosef Bacik struct log_write_super {
820e9cebe7SJosef Bacik 	__le64 magic;
830e9cebe7SJosef Bacik 	__le64 version;
840e9cebe7SJosef Bacik 	__le64 nr_entries;
850e9cebe7SJosef Bacik 	__le32 sectorsize;
860e9cebe7SJosef Bacik };
870e9cebe7SJosef Bacik 
880e9cebe7SJosef Bacik /*
890e9cebe7SJosef Bacik  * sector - the sector we wrote.
900e9cebe7SJosef Bacik  * nr_sectors - the number of sectors we wrote.
910e9cebe7SJosef Bacik  * flags - flags for this log entry.
920e9cebe7SJosef Bacik  * data_len - the size of the data in this log entry, this is for private log
930e9cebe7SJosef Bacik  * entry stuff, the MARK data provided by userspace for example.
940e9cebe7SJosef Bacik  */
950e9cebe7SJosef Bacik struct log_write_entry {
960e9cebe7SJosef Bacik 	__le64 sector;
970e9cebe7SJosef Bacik 	__le64 nr_sectors;
980e9cebe7SJosef Bacik 	__le64 flags;
990e9cebe7SJosef Bacik 	__le64 data_len;
1000e9cebe7SJosef Bacik };
1010e9cebe7SJosef Bacik 
1020e9cebe7SJosef Bacik struct log_writes_c {
1030e9cebe7SJosef Bacik 	struct dm_dev *dev;
1040e9cebe7SJosef Bacik 	struct dm_dev *logdev;
1050e9cebe7SJosef Bacik 	u64 logged_entries;
1060e9cebe7SJosef Bacik 	u32 sectorsize;
107228bb5b2SJosef Bacik 	u32 sectorshift;
1080e9cebe7SJosef Bacik 	atomic_t io_blocks;
1090e9cebe7SJosef Bacik 	atomic_t pending_blocks;
1100e9cebe7SJosef Bacik 	sector_t next_sector;
1110e9cebe7SJosef Bacik 	sector_t end_sector;
1120e9cebe7SJosef Bacik 	bool logging_enabled;
1130e9cebe7SJosef Bacik 	bool device_supports_discard;
1140e9cebe7SJosef Bacik 	spinlock_t blocks_lock;
1150e9cebe7SJosef Bacik 	struct list_head unflushed_blocks;
1160e9cebe7SJosef Bacik 	struct list_head logging_blocks;
1170e9cebe7SJosef Bacik 	wait_queue_head_t wait;
1180e9cebe7SJosef Bacik 	struct task_struct *log_kthread;
119211ad4b7Szhangyi (F) 	struct completion super_done;
1200e9cebe7SJosef Bacik };
1210e9cebe7SJosef Bacik 
1220e9cebe7SJosef Bacik struct pending_block {
1230e9cebe7SJosef Bacik 	int vec_cnt;
1240e9cebe7SJosef Bacik 	u64 flags;
1250e9cebe7SJosef Bacik 	sector_t sector;
1260e9cebe7SJosef Bacik 	sector_t nr_sectors;
1270e9cebe7SJosef Bacik 	char *data;
1280e9cebe7SJosef Bacik 	u32 datalen;
1290e9cebe7SJosef Bacik 	struct list_head list;
130b18ae8ddSGustavo A. R. Silva 	struct bio_vec vecs[];
1310e9cebe7SJosef Bacik };
1320e9cebe7SJosef Bacik 
1330e9cebe7SJosef Bacik struct per_bio_data {
1340e9cebe7SJosef Bacik 	struct pending_block *block;
1350e9cebe7SJosef Bacik };
1360e9cebe7SJosef Bacik 
137228bb5b2SJosef Bacik static inline sector_t bio_to_dev_sectors(struct log_writes_c *lc,
138228bb5b2SJosef Bacik 					  sector_t sectors)
139228bb5b2SJosef Bacik {
140228bb5b2SJosef Bacik 	return sectors >> (lc->sectorshift - SECTOR_SHIFT);
141228bb5b2SJosef Bacik }
142228bb5b2SJosef Bacik 
143228bb5b2SJosef Bacik static inline sector_t dev_to_bio_sectors(struct log_writes_c *lc,
144228bb5b2SJosef Bacik 					  sector_t sectors)
145228bb5b2SJosef Bacik {
146228bb5b2SJosef Bacik 	return sectors << (lc->sectorshift - SECTOR_SHIFT);
147228bb5b2SJosef Bacik }
148228bb5b2SJosef Bacik 
1490e9cebe7SJosef Bacik static void put_pending_block(struct log_writes_c *lc)
1500e9cebe7SJosef Bacik {
1510e9cebe7SJosef Bacik 	if (atomic_dec_and_test(&lc->pending_blocks)) {
1520e9cebe7SJosef Bacik 		smp_mb__after_atomic();
1530e9cebe7SJosef Bacik 		if (waitqueue_active(&lc->wait))
1540e9cebe7SJosef Bacik 			wake_up(&lc->wait);
1550e9cebe7SJosef Bacik 	}
1560e9cebe7SJosef Bacik }
1570e9cebe7SJosef Bacik 
1580e9cebe7SJosef Bacik static void put_io_block(struct log_writes_c *lc)
1590e9cebe7SJosef Bacik {
1600e9cebe7SJosef Bacik 	if (atomic_dec_and_test(&lc->io_blocks)) {
1610e9cebe7SJosef Bacik 		smp_mb__after_atomic();
1620e9cebe7SJosef Bacik 		if (waitqueue_active(&lc->wait))
1630e9cebe7SJosef Bacik 			wake_up(&lc->wait);
1640e9cebe7SJosef Bacik 	}
1650e9cebe7SJosef Bacik }
1660e9cebe7SJosef Bacik 
1674246a0b6SChristoph Hellwig static void log_end_io(struct bio *bio)
1680e9cebe7SJosef Bacik {
1690e9cebe7SJosef Bacik 	struct log_writes_c *lc = bio->bi_private;
1700e9cebe7SJosef Bacik 
1714e4cbee9SChristoph Hellwig 	if (bio->bi_status) {
1720e9cebe7SJosef Bacik 		unsigned long flags;
1730e9cebe7SJosef Bacik 
1744e4cbee9SChristoph Hellwig 		DMERR("Error writing log block, error=%d", bio->bi_status);
1750e9cebe7SJosef Bacik 		spin_lock_irqsave(&lc->blocks_lock, flags);
1760e9cebe7SJosef Bacik 		lc->logging_enabled = false;
1770e9cebe7SJosef Bacik 		spin_unlock_irqrestore(&lc->blocks_lock, flags);
1780e9cebe7SJosef Bacik 	}
1790e9cebe7SJosef Bacik 
180491221f8SGuoqing Jiang 	bio_free_pages(bio);
1810e9cebe7SJosef Bacik 	put_io_block(lc);
1820e9cebe7SJosef Bacik 	bio_put(bio);
1830e9cebe7SJosef Bacik }
1840e9cebe7SJosef Bacik 
185211ad4b7Szhangyi (F) static void log_end_super(struct bio *bio)
186211ad4b7Szhangyi (F) {
187211ad4b7Szhangyi (F) 	struct log_writes_c *lc = bio->bi_private;
188211ad4b7Szhangyi (F) 
189211ad4b7Szhangyi (F) 	complete(&lc->super_done);
190211ad4b7Szhangyi (F) 	log_end_io(bio);
191211ad4b7Szhangyi (F) }
192211ad4b7Szhangyi (F) 
1930e9cebe7SJosef Bacik /*
1940e9cebe7SJosef Bacik  * Meant to be called if there is an error, it will free all the pages
1950e9cebe7SJosef Bacik  * associated with the block.
1960e9cebe7SJosef Bacik  */
1970e9cebe7SJosef Bacik static void free_pending_block(struct log_writes_c *lc,
1980e9cebe7SJosef Bacik 			       struct pending_block *block)
1990e9cebe7SJosef Bacik {
2000e9cebe7SJosef Bacik 	int i;
2010e9cebe7SJosef Bacik 
2020e9cebe7SJosef Bacik 	for (i = 0; i < block->vec_cnt; i++) {
2030e9cebe7SJosef Bacik 		if (block->vecs[i].bv_page)
2040e9cebe7SJosef Bacik 			__free_page(block->vecs[i].bv_page);
2050e9cebe7SJosef Bacik 	}
2060e9cebe7SJosef Bacik 	kfree(block->data);
2070e9cebe7SJosef Bacik 	kfree(block);
2080e9cebe7SJosef Bacik 	put_pending_block(lc);
2090e9cebe7SJosef Bacik }
2100e9cebe7SJosef Bacik 
2110e9cebe7SJosef Bacik static int write_metadata(struct log_writes_c *lc, void *entry,
2120e9cebe7SJosef Bacik 			  size_t entrylen, void *data, size_t datalen,
2130e9cebe7SJosef Bacik 			  sector_t sector)
2140e9cebe7SJosef Bacik {
2150e9cebe7SJosef Bacik 	struct bio *bio;
2160e9cebe7SJosef Bacik 	struct page *page;
2170e9cebe7SJosef Bacik 	void *ptr;
2180e9cebe7SJosef Bacik 	size_t ret;
2190e9cebe7SJosef Bacik 
2200e9cebe7SJosef Bacik 	bio = bio_alloc(GFP_KERNEL, 1);
2210e9cebe7SJosef Bacik 	if (!bio) {
2220e9cebe7SJosef Bacik 		DMERR("Couldn't alloc log bio");
2230e9cebe7SJosef Bacik 		goto error;
2240e9cebe7SJosef Bacik 	}
2250e9cebe7SJosef Bacik 	bio->bi_iter.bi_size = 0;
2260e9cebe7SJosef Bacik 	bio->bi_iter.bi_sector = sector;
22774d46992SChristoph Hellwig 	bio_set_dev(bio, lc->logdev->bdev);
228211ad4b7Szhangyi (F) 	bio->bi_end_io = (sector == WRITE_LOG_SUPER_SECTOR) ?
229211ad4b7Szhangyi (F) 			  log_end_super : log_end_io;
2300e9cebe7SJosef Bacik 	bio->bi_private = lc;
231e6047149SMike Christie 	bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2320e9cebe7SJosef Bacik 
2330e9cebe7SJosef Bacik 	page = alloc_page(GFP_KERNEL);
2340e9cebe7SJosef Bacik 	if (!page) {
2350e9cebe7SJosef Bacik 		DMERR("Couldn't alloc log page");
2360e9cebe7SJosef Bacik 		bio_put(bio);
2370e9cebe7SJosef Bacik 		goto error;
2380e9cebe7SJosef Bacik 	}
2390e9cebe7SJosef Bacik 
2400e9cebe7SJosef Bacik 	ptr = kmap_atomic(page);
2410e9cebe7SJosef Bacik 	memcpy(ptr, entry, entrylen);
2420e9cebe7SJosef Bacik 	if (datalen)
2430e9cebe7SJosef Bacik 		memcpy(ptr + entrylen, data, datalen);
2440e9cebe7SJosef Bacik 	memset(ptr + entrylen + datalen, 0,
2450e9cebe7SJosef Bacik 	       lc->sectorsize - entrylen - datalen);
2460e9cebe7SJosef Bacik 	kunmap_atomic(ptr);
2470e9cebe7SJosef Bacik 
2480e9cebe7SJosef Bacik 	ret = bio_add_page(bio, page, lc->sectorsize, 0);
2490e9cebe7SJosef Bacik 	if (ret != lc->sectorsize) {
2500e9cebe7SJosef Bacik 		DMERR("Couldn't add page to the log block");
2510e9cebe7SJosef Bacik 		goto error_bio;
2520e9cebe7SJosef Bacik 	}
2534e49ea4aSMike Christie 	submit_bio(bio);
2540e9cebe7SJosef Bacik 	return 0;
2550e9cebe7SJosef Bacik error_bio:
2560e9cebe7SJosef Bacik 	bio_put(bio);
2570e9cebe7SJosef Bacik 	__free_page(page);
2580e9cebe7SJosef Bacik error:
2590e9cebe7SJosef Bacik 	put_io_block(lc);
2600e9cebe7SJosef Bacik 	return -1;
2610e9cebe7SJosef Bacik }
2620e9cebe7SJosef Bacik 
263e5a20660SRoss Zwisler static int write_inline_data(struct log_writes_c *lc, void *entry,
264e5a20660SRoss Zwisler 			     size_t entrylen, void *data, size_t datalen,
265e5a20660SRoss Zwisler 			     sector_t sector)
266e5a20660SRoss Zwisler {
2675f7136dbSMatthew Wilcox (Oracle) 	int bio_pages, pg_datalen, pg_sectorlen, i;
268e5a20660SRoss Zwisler 	struct page *page;
269e5a20660SRoss Zwisler 	struct bio *bio;
270e5a20660SRoss Zwisler 	size_t ret;
271e5a20660SRoss Zwisler 	void *ptr;
272e5a20660SRoss Zwisler 
273e5a20660SRoss Zwisler 	while (datalen) {
2745f7136dbSMatthew Wilcox (Oracle) 		bio_pages = bio_max_segs(DIV_ROUND_UP(datalen, PAGE_SIZE));
275e5a20660SRoss Zwisler 
276e5a20660SRoss Zwisler 		atomic_inc(&lc->io_blocks);
277e5a20660SRoss Zwisler 
278e5a20660SRoss Zwisler 		bio = bio_alloc(GFP_KERNEL, bio_pages);
279e5a20660SRoss Zwisler 		if (!bio) {
280e5a20660SRoss Zwisler 			DMERR("Couldn't alloc inline data bio");
281e5a20660SRoss Zwisler 			goto error;
282e5a20660SRoss Zwisler 		}
283e5a20660SRoss Zwisler 
284e5a20660SRoss Zwisler 		bio->bi_iter.bi_size = 0;
285e5a20660SRoss Zwisler 		bio->bi_iter.bi_sector = sector;
286e5a20660SRoss Zwisler 		bio_set_dev(bio, lc->logdev->bdev);
287e5a20660SRoss Zwisler 		bio->bi_end_io = log_end_io;
288e5a20660SRoss Zwisler 		bio->bi_private = lc;
289e5a20660SRoss Zwisler 		bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
290e5a20660SRoss Zwisler 
291e5a20660SRoss Zwisler 		for (i = 0; i < bio_pages; i++) {
292e5a20660SRoss Zwisler 			pg_datalen = min_t(int, datalen, PAGE_SIZE);
293e5a20660SRoss Zwisler 			pg_sectorlen = ALIGN(pg_datalen, lc->sectorsize);
294e5a20660SRoss Zwisler 
295e5a20660SRoss Zwisler 			page = alloc_page(GFP_KERNEL);
296e5a20660SRoss Zwisler 			if (!page) {
297e5a20660SRoss Zwisler 				DMERR("Couldn't alloc inline data page");
298e5a20660SRoss Zwisler 				goto error_bio;
299e5a20660SRoss Zwisler 			}
300e5a20660SRoss Zwisler 
301e5a20660SRoss Zwisler 			ptr = kmap_atomic(page);
302e5a20660SRoss Zwisler 			memcpy(ptr, data, pg_datalen);
303e5a20660SRoss Zwisler 			if (pg_sectorlen > pg_datalen)
304e5a20660SRoss Zwisler 				memset(ptr + pg_datalen, 0, pg_sectorlen - pg_datalen);
305e5a20660SRoss Zwisler 			kunmap_atomic(ptr);
306e5a20660SRoss Zwisler 
307e5a20660SRoss Zwisler 			ret = bio_add_page(bio, page, pg_sectorlen, 0);
308e5a20660SRoss Zwisler 			if (ret != pg_sectorlen) {
309e5a20660SRoss Zwisler 				DMERR("Couldn't add page of inline data");
310e5a20660SRoss Zwisler 				__free_page(page);
311e5a20660SRoss Zwisler 				goto error_bio;
312e5a20660SRoss Zwisler 			}
313e5a20660SRoss Zwisler 
314e5a20660SRoss Zwisler 			datalen -= pg_datalen;
315e5a20660SRoss Zwisler 			data	+= pg_datalen;
316e5a20660SRoss Zwisler 		}
317e5a20660SRoss Zwisler 		submit_bio(bio);
318e5a20660SRoss Zwisler 
319e5a20660SRoss Zwisler 		sector += bio_pages * PAGE_SECTORS;
320e5a20660SRoss Zwisler 	}
321e5a20660SRoss Zwisler 	return 0;
322e5a20660SRoss Zwisler error_bio:
323e5a20660SRoss Zwisler 	bio_free_pages(bio);
324e5a20660SRoss Zwisler 	bio_put(bio);
325e5a20660SRoss Zwisler error:
326e5a20660SRoss Zwisler 	put_io_block(lc);
327e5a20660SRoss Zwisler 	return -1;
328e5a20660SRoss Zwisler }
329e5a20660SRoss Zwisler 
3300e9cebe7SJosef Bacik static int log_one_block(struct log_writes_c *lc,
3310e9cebe7SJosef Bacik 			 struct pending_block *block, sector_t sector)
3320e9cebe7SJosef Bacik {
3330e9cebe7SJosef Bacik 	struct bio *bio;
3340e9cebe7SJosef Bacik 	struct log_write_entry entry;
335e5a20660SRoss Zwisler 	size_t metadatalen, ret;
3360e9cebe7SJosef Bacik 	int i;
3370e9cebe7SJosef Bacik 
3380e9cebe7SJosef Bacik 	entry.sector = cpu_to_le64(block->sector);
3390e9cebe7SJosef Bacik 	entry.nr_sectors = cpu_to_le64(block->nr_sectors);
3400e9cebe7SJosef Bacik 	entry.flags = cpu_to_le64(block->flags);
3410e9cebe7SJosef Bacik 	entry.data_len = cpu_to_le64(block->datalen);
342e5a20660SRoss Zwisler 
343e5a20660SRoss Zwisler 	metadatalen = (block->flags & LOG_MARK_FLAG) ? block->datalen : 0;
3440e9cebe7SJosef Bacik 	if (write_metadata(lc, &entry, sizeof(entry), block->data,
345e5a20660SRoss Zwisler 			   metadatalen, sector)) {
3460e9cebe7SJosef Bacik 		free_pending_block(lc, block);
3470e9cebe7SJosef Bacik 		return -1;
3480e9cebe7SJosef Bacik 	}
3490e9cebe7SJosef Bacik 
350e5a20660SRoss Zwisler 	sector += dev_to_bio_sectors(lc, 1);
351e5a20660SRoss Zwisler 
352e5a20660SRoss Zwisler 	if (block->datalen && metadatalen == 0) {
353e5a20660SRoss Zwisler 		if (write_inline_data(lc, &entry, sizeof(entry), block->data,
354e5a20660SRoss Zwisler 				      block->datalen, sector)) {
355e5a20660SRoss Zwisler 			free_pending_block(lc, block);
356e5a20660SRoss Zwisler 			return -1;
357e5a20660SRoss Zwisler 		}
358e5a20660SRoss Zwisler 		/* we don't support both inline data & bio data */
359e5a20660SRoss Zwisler 		goto out;
360e5a20660SRoss Zwisler 	}
361e5a20660SRoss Zwisler 
3620e9cebe7SJosef Bacik 	if (!block->vec_cnt)
3630e9cebe7SJosef Bacik 		goto out;
3640e9cebe7SJosef Bacik 
365a5d60783SMikulas Patocka 	atomic_inc(&lc->io_blocks);
3665f7136dbSMatthew Wilcox (Oracle) 	bio = bio_alloc(GFP_KERNEL, bio_max_segs(block->vec_cnt));
3670e9cebe7SJosef Bacik 	if (!bio) {
3680e9cebe7SJosef Bacik 		DMERR("Couldn't alloc log bio");
3690e9cebe7SJosef Bacik 		goto error;
3700e9cebe7SJosef Bacik 	}
3710e9cebe7SJosef Bacik 	bio->bi_iter.bi_size = 0;
3720e9cebe7SJosef Bacik 	bio->bi_iter.bi_sector = sector;
37374d46992SChristoph Hellwig 	bio_set_dev(bio, lc->logdev->bdev);
3740e9cebe7SJosef Bacik 	bio->bi_end_io = log_end_io;
3750e9cebe7SJosef Bacik 	bio->bi_private = lc;
376e6047149SMike Christie 	bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3770e9cebe7SJosef Bacik 
3780e9cebe7SJosef Bacik 	for (i = 0; i < block->vec_cnt; i++) {
3790e9cebe7SJosef Bacik 		/*
3800e9cebe7SJosef Bacik 		 * The page offset is always 0 because we allocate a new page
3810e9cebe7SJosef Bacik 		 * for every bvec in the original bio for simplicity sake.
3820e9cebe7SJosef Bacik 		 */
3830e9cebe7SJosef Bacik 		ret = bio_add_page(bio, block->vecs[i].bv_page,
3840e9cebe7SJosef Bacik 				   block->vecs[i].bv_len, 0);
3850e9cebe7SJosef Bacik 		if (ret != block->vecs[i].bv_len) {
3860e9cebe7SJosef Bacik 			atomic_inc(&lc->io_blocks);
3874e49ea4aSMike Christie 			submit_bio(bio);
3885f7136dbSMatthew Wilcox (Oracle) 			bio = bio_alloc(GFP_KERNEL,
3895f7136dbSMatthew Wilcox (Oracle) 					bio_max_segs(block->vec_cnt - i));
3900e9cebe7SJosef Bacik 			if (!bio) {
3910e9cebe7SJosef Bacik 				DMERR("Couldn't alloc log bio");
3920e9cebe7SJosef Bacik 				goto error;
3930e9cebe7SJosef Bacik 			}
3940e9cebe7SJosef Bacik 			bio->bi_iter.bi_size = 0;
3950e9cebe7SJosef Bacik 			bio->bi_iter.bi_sector = sector;
39674d46992SChristoph Hellwig 			bio_set_dev(bio, lc->logdev->bdev);
3970e9cebe7SJosef Bacik 			bio->bi_end_io = log_end_io;
3980e9cebe7SJosef Bacik 			bio->bi_private = lc;
399e6047149SMike Christie 			bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
4000e9cebe7SJosef Bacik 
4010e9cebe7SJosef Bacik 			ret = bio_add_page(bio, block->vecs[i].bv_page,
4020e9cebe7SJosef Bacik 					   block->vecs[i].bv_len, 0);
4030e9cebe7SJosef Bacik 			if (ret != block->vecs[i].bv_len) {
4040e9cebe7SJosef Bacik 				DMERR("Couldn't add page on new bio?");
4050e9cebe7SJosef Bacik 				bio_put(bio);
4060e9cebe7SJosef Bacik 				goto error;
4070e9cebe7SJosef Bacik 			}
4080e9cebe7SJosef Bacik 		}
4090e9cebe7SJosef Bacik 		sector += block->vecs[i].bv_len >> SECTOR_SHIFT;
4100e9cebe7SJosef Bacik 	}
4114e49ea4aSMike Christie 	submit_bio(bio);
4120e9cebe7SJosef Bacik out:
4130e9cebe7SJosef Bacik 	kfree(block->data);
4140e9cebe7SJosef Bacik 	kfree(block);
4150e9cebe7SJosef Bacik 	put_pending_block(lc);
4160e9cebe7SJosef Bacik 	return 0;
4170e9cebe7SJosef Bacik error:
4180e9cebe7SJosef Bacik 	free_pending_block(lc, block);
4190e9cebe7SJosef Bacik 	put_io_block(lc);
4200e9cebe7SJosef Bacik 	return -1;
4210e9cebe7SJosef Bacik }
4220e9cebe7SJosef Bacik 
4230e9cebe7SJosef Bacik static int log_super(struct log_writes_c *lc)
4240e9cebe7SJosef Bacik {
4250e9cebe7SJosef Bacik 	struct log_write_super super;
4260e9cebe7SJosef Bacik 
4270e9cebe7SJosef Bacik 	super.magic = cpu_to_le64(WRITE_LOG_MAGIC);
4280e9cebe7SJosef Bacik 	super.version = cpu_to_le64(WRITE_LOG_VERSION);
4290e9cebe7SJosef Bacik 	super.nr_entries = cpu_to_le64(lc->logged_entries);
4300e9cebe7SJosef Bacik 	super.sectorsize = cpu_to_le32(lc->sectorsize);
4310e9cebe7SJosef Bacik 
432211ad4b7Szhangyi (F) 	if (write_metadata(lc, &super, sizeof(super), NULL, 0,
433211ad4b7Szhangyi (F) 			   WRITE_LOG_SUPER_SECTOR)) {
4340e9cebe7SJosef Bacik 		DMERR("Couldn't write super");
4350e9cebe7SJosef Bacik 		return -1;
4360e9cebe7SJosef Bacik 	}
4370e9cebe7SJosef Bacik 
438211ad4b7Szhangyi (F) 	/*
439211ad4b7Szhangyi (F) 	 * Super sector should be writen in-order, otherwise the
440211ad4b7Szhangyi (F) 	 * nr_entries could be rewritten incorrectly by an old bio.
441211ad4b7Szhangyi (F) 	 */
442211ad4b7Szhangyi (F) 	wait_for_completion_io(&lc->super_done);
443211ad4b7Szhangyi (F) 
4440e9cebe7SJosef Bacik 	return 0;
4450e9cebe7SJosef Bacik }
4460e9cebe7SJosef Bacik 
4470e9cebe7SJosef Bacik static inline sector_t logdev_last_sector(struct log_writes_c *lc)
4480e9cebe7SJosef Bacik {
4490e9cebe7SJosef Bacik 	return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT;
4500e9cebe7SJosef Bacik }
4510e9cebe7SJosef Bacik 
4520e9cebe7SJosef Bacik static int log_writes_kthread(void *arg)
4530e9cebe7SJosef Bacik {
4540e9cebe7SJosef Bacik 	struct log_writes_c *lc = (struct log_writes_c *)arg;
4550e9cebe7SJosef Bacik 	sector_t sector = 0;
4560e9cebe7SJosef Bacik 
4570e9cebe7SJosef Bacik 	while (!kthread_should_stop()) {
4580e9cebe7SJosef Bacik 		bool super = false;
4590e9cebe7SJosef Bacik 		bool logging_enabled;
4600e9cebe7SJosef Bacik 		struct pending_block *block = NULL;
4610e9cebe7SJosef Bacik 		int ret;
4620e9cebe7SJosef Bacik 
4630e9cebe7SJosef Bacik 		spin_lock_irq(&lc->blocks_lock);
4640e9cebe7SJosef Bacik 		if (!list_empty(&lc->logging_blocks)) {
4650e9cebe7SJosef Bacik 			block = list_first_entry(&lc->logging_blocks,
4660e9cebe7SJosef Bacik 						 struct pending_block, list);
4670e9cebe7SJosef Bacik 			list_del_init(&block->list);
4680e9cebe7SJosef Bacik 			if (!lc->logging_enabled)
4690e9cebe7SJosef Bacik 				goto next;
4700e9cebe7SJosef Bacik 
4710e9cebe7SJosef Bacik 			sector = lc->next_sector;
472228bb5b2SJosef Bacik 			if (!(block->flags & LOG_DISCARD_FLAG))
473228bb5b2SJosef Bacik 				lc->next_sector += dev_to_bio_sectors(lc, block->nr_sectors);
474228bb5b2SJosef Bacik 			lc->next_sector += dev_to_bio_sectors(lc, 1);
4750e9cebe7SJosef Bacik 
4760e9cebe7SJosef Bacik 			/*
4770e9cebe7SJosef Bacik 			 * Apparently the size of the device may not be known
4780e9cebe7SJosef Bacik 			 * right away, so handle this properly.
4790e9cebe7SJosef Bacik 			 */
4800e9cebe7SJosef Bacik 			if (!lc->end_sector)
4810e9cebe7SJosef Bacik 				lc->end_sector = logdev_last_sector(lc);
4820e9cebe7SJosef Bacik 			if (lc->end_sector &&
4830e9cebe7SJosef Bacik 			    lc->next_sector >= lc->end_sector) {
4840e9cebe7SJosef Bacik 				DMERR("Ran out of space on the logdev");
4850e9cebe7SJosef Bacik 				lc->logging_enabled = false;
4860e9cebe7SJosef Bacik 				goto next;
4870e9cebe7SJosef Bacik 			}
4880e9cebe7SJosef Bacik 			lc->logged_entries++;
4890e9cebe7SJosef Bacik 			atomic_inc(&lc->io_blocks);
4900e9cebe7SJosef Bacik 
4910e9cebe7SJosef Bacik 			super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG));
4920e9cebe7SJosef Bacik 			if (super)
4930e9cebe7SJosef Bacik 				atomic_inc(&lc->io_blocks);
4940e9cebe7SJosef Bacik 		}
4950e9cebe7SJosef Bacik next:
4960e9cebe7SJosef Bacik 		logging_enabled = lc->logging_enabled;
4970e9cebe7SJosef Bacik 		spin_unlock_irq(&lc->blocks_lock);
4980e9cebe7SJosef Bacik 		if (block) {
4990e9cebe7SJosef Bacik 			if (logging_enabled) {
5000e9cebe7SJosef Bacik 				ret = log_one_block(lc, block, sector);
5010e9cebe7SJosef Bacik 				if (!ret && super)
5020e9cebe7SJosef Bacik 					ret = log_super(lc);
5030e9cebe7SJosef Bacik 				if (ret) {
5040e9cebe7SJosef Bacik 					spin_lock_irq(&lc->blocks_lock);
5050e9cebe7SJosef Bacik 					lc->logging_enabled = false;
5060e9cebe7SJosef Bacik 					spin_unlock_irq(&lc->blocks_lock);
5070e9cebe7SJosef Bacik 				}
5080e9cebe7SJosef Bacik 			} else
5090e9cebe7SJosef Bacik 				free_pending_block(lc, block);
5100e9cebe7SJosef Bacik 			continue;
5110e9cebe7SJosef Bacik 		}
5120e9cebe7SJosef Bacik 
5130e9cebe7SJosef Bacik 		if (!try_to_freeze()) {
5140e9cebe7SJosef Bacik 			set_current_state(TASK_INTERRUPTIBLE);
5150e9cebe7SJosef Bacik 			if (!kthread_should_stop() &&
5160c79c620SJosef Bacik 			    list_empty(&lc->logging_blocks))
5170e9cebe7SJosef Bacik 				schedule();
5180e9cebe7SJosef Bacik 			__set_current_state(TASK_RUNNING);
5190e9cebe7SJosef Bacik 		}
5200e9cebe7SJosef Bacik 	}
5210e9cebe7SJosef Bacik 	return 0;
5220e9cebe7SJosef Bacik }
5230e9cebe7SJosef Bacik 
5240e9cebe7SJosef Bacik /*
5250e9cebe7SJosef Bacik  * Construct a log-writes mapping:
5260e9cebe7SJosef Bacik  * log-writes <dev_path> <log_dev_path>
5270e9cebe7SJosef Bacik  */
5280e9cebe7SJosef Bacik static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
5290e9cebe7SJosef Bacik {
5300e9cebe7SJosef Bacik 	struct log_writes_c *lc;
5310e9cebe7SJosef Bacik 	struct dm_arg_set as;
5320e9cebe7SJosef Bacik 	const char *devname, *logdevname;
533e80d1c80SVivek Goyal 	int ret;
5340e9cebe7SJosef Bacik 
5350e9cebe7SJosef Bacik 	as.argc = argc;
5360e9cebe7SJosef Bacik 	as.argv = argv;
5370e9cebe7SJosef Bacik 
5380e9cebe7SJosef Bacik 	if (argc < 2) {
5390e9cebe7SJosef Bacik 		ti->error = "Invalid argument count";
5400e9cebe7SJosef Bacik 		return -EINVAL;
5410e9cebe7SJosef Bacik 	}
5420e9cebe7SJosef Bacik 
5430e9cebe7SJosef Bacik 	lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL);
5440e9cebe7SJosef Bacik 	if (!lc) {
5450e9cebe7SJosef Bacik 		ti->error = "Cannot allocate context";
5460e9cebe7SJosef Bacik 		return -ENOMEM;
5470e9cebe7SJosef Bacik 	}
5480e9cebe7SJosef Bacik 	spin_lock_init(&lc->blocks_lock);
5490e9cebe7SJosef Bacik 	INIT_LIST_HEAD(&lc->unflushed_blocks);
5500e9cebe7SJosef Bacik 	INIT_LIST_HEAD(&lc->logging_blocks);
5510e9cebe7SJosef Bacik 	init_waitqueue_head(&lc->wait);
552211ad4b7Szhangyi (F) 	init_completion(&lc->super_done);
5530e9cebe7SJosef Bacik 	atomic_set(&lc->io_blocks, 0);
5540e9cebe7SJosef Bacik 	atomic_set(&lc->pending_blocks, 0);
5550e9cebe7SJosef Bacik 
5560e9cebe7SJosef Bacik 	devname = dm_shift_arg(&as);
557e80d1c80SVivek Goyal 	ret = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev);
558e80d1c80SVivek Goyal 	if (ret) {
5590e9cebe7SJosef Bacik 		ti->error = "Device lookup failed";
5600e9cebe7SJosef Bacik 		goto bad;
5610e9cebe7SJosef Bacik 	}
5620e9cebe7SJosef Bacik 
5630e9cebe7SJosef Bacik 	logdevname = dm_shift_arg(&as);
564e80d1c80SVivek Goyal 	ret = dm_get_device(ti, logdevname, dm_table_get_mode(ti->table),
565e80d1c80SVivek Goyal 			    &lc->logdev);
566e80d1c80SVivek Goyal 	if (ret) {
5670e9cebe7SJosef Bacik 		ti->error = "Log device lookup failed";
5680e9cebe7SJosef Bacik 		dm_put_device(ti, lc->dev);
5690e9cebe7SJosef Bacik 		goto bad;
5700e9cebe7SJosef Bacik 	}
5710e9cebe7SJosef Bacik 
572228bb5b2SJosef Bacik 	lc->sectorsize = bdev_logical_block_size(lc->dev->bdev);
573228bb5b2SJosef Bacik 	lc->sectorshift = ilog2(lc->sectorsize);
5740e9cebe7SJosef Bacik 	lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write");
57591e630d9SVladimir Zapolskiy 	if (IS_ERR(lc->log_kthread)) {
57691e630d9SVladimir Zapolskiy 		ret = PTR_ERR(lc->log_kthread);
5770e9cebe7SJosef Bacik 		ti->error = "Couldn't alloc kthread";
5780e9cebe7SJosef Bacik 		dm_put_device(ti, lc->dev);
5790e9cebe7SJosef Bacik 		dm_put_device(ti, lc->logdev);
5800e9cebe7SJosef Bacik 		goto bad;
5810e9cebe7SJosef Bacik 	}
5820e9cebe7SJosef Bacik 
583228bb5b2SJosef Bacik 	/*
584228bb5b2SJosef Bacik 	 * next_sector is in 512b sectors to correspond to what bi_sector expects.
585228bb5b2SJosef Bacik 	 * The super starts at sector 0, and the next_sector is the next logical
586228bb5b2SJosef Bacik 	 * one based on the sectorsize of the device.
587228bb5b2SJosef Bacik 	 */
588228bb5b2SJosef Bacik 	lc->next_sector = lc->sectorsize >> SECTOR_SHIFT;
5890e9cebe7SJosef Bacik 	lc->logging_enabled = true;
5900e9cebe7SJosef Bacik 	lc->end_sector = logdev_last_sector(lc);
5910e9cebe7SJosef Bacik 	lc->device_supports_discard = true;
5920e9cebe7SJosef Bacik 
5930e9cebe7SJosef Bacik 	ti->num_flush_bios = 1;
5940e9cebe7SJosef Bacik 	ti->flush_supported = true;
5950e9cebe7SJosef Bacik 	ti->num_discard_bios = 1;
5960e9cebe7SJosef Bacik 	ti->discards_supported = true;
59730187e1dSMike Snitzer 	ti->per_io_data_size = sizeof(struct per_bio_data);
5980e9cebe7SJosef Bacik 	ti->private = lc;
5990e9cebe7SJosef Bacik 	return 0;
6000e9cebe7SJosef Bacik 
6010e9cebe7SJosef Bacik bad:
6020e9cebe7SJosef Bacik 	kfree(lc);
603e80d1c80SVivek Goyal 	return ret;
6040e9cebe7SJosef Bacik }
6050e9cebe7SJosef Bacik 
6060e9cebe7SJosef Bacik static int log_mark(struct log_writes_c *lc, char *data)
6070e9cebe7SJosef Bacik {
6080e9cebe7SJosef Bacik 	struct pending_block *block;
6090e9cebe7SJosef Bacik 	size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry);
6100e9cebe7SJosef Bacik 
6110e9cebe7SJosef Bacik 	block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
6120e9cebe7SJosef Bacik 	if (!block) {
6130e9cebe7SJosef Bacik 		DMERR("Error allocating pending block");
6140e9cebe7SJosef Bacik 		return -ENOMEM;
6150e9cebe7SJosef Bacik 	}
6160e9cebe7SJosef Bacik 
6174b259fc4SMa Shimiao 	block->data = kstrndup(data, maxsize - 1, GFP_KERNEL);
6180e9cebe7SJosef Bacik 	if (!block->data) {
6190e9cebe7SJosef Bacik 		DMERR("Error copying mark data");
6200e9cebe7SJosef Bacik 		kfree(block);
6210e9cebe7SJosef Bacik 		return -ENOMEM;
6220e9cebe7SJosef Bacik 	}
6230e9cebe7SJosef Bacik 	atomic_inc(&lc->pending_blocks);
6240e9cebe7SJosef Bacik 	block->datalen = strlen(block->data);
6250e9cebe7SJosef Bacik 	block->flags |= LOG_MARK_FLAG;
6260e9cebe7SJosef Bacik 	spin_lock_irq(&lc->blocks_lock);
6270e9cebe7SJosef Bacik 	list_add_tail(&block->list, &lc->logging_blocks);
6280e9cebe7SJosef Bacik 	spin_unlock_irq(&lc->blocks_lock);
6290e9cebe7SJosef Bacik 	wake_up_process(lc->log_kthread);
6300e9cebe7SJosef Bacik 	return 0;
6310e9cebe7SJosef Bacik }
6320e9cebe7SJosef Bacik 
6330e9cebe7SJosef Bacik static void log_writes_dtr(struct dm_target *ti)
6340e9cebe7SJosef Bacik {
6350e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
6360e9cebe7SJosef Bacik 
6370e9cebe7SJosef Bacik 	spin_lock_irq(&lc->blocks_lock);
6380e9cebe7SJosef Bacik 	list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks);
6390e9cebe7SJosef Bacik 	spin_unlock_irq(&lc->blocks_lock);
6400e9cebe7SJosef Bacik 
6410e9cebe7SJosef Bacik 	/*
6420e9cebe7SJosef Bacik 	 * This is just nice to have since it'll update the super to include the
6430e9cebe7SJosef Bacik 	 * unflushed blocks, if it fails we don't really care.
6440e9cebe7SJosef Bacik 	 */
6450e9cebe7SJosef Bacik 	log_mark(lc, "dm-log-writes-end");
6460e9cebe7SJosef Bacik 	wake_up_process(lc->log_kthread);
6470e9cebe7SJosef Bacik 	wait_event(lc->wait, !atomic_read(&lc->io_blocks) &&
6480e9cebe7SJosef Bacik 		   !atomic_read(&lc->pending_blocks));
6490e9cebe7SJosef Bacik 	kthread_stop(lc->log_kthread);
6500e9cebe7SJosef Bacik 
6510e9cebe7SJosef Bacik 	WARN_ON(!list_empty(&lc->logging_blocks));
6520e9cebe7SJosef Bacik 	WARN_ON(!list_empty(&lc->unflushed_blocks));
6530e9cebe7SJosef Bacik 	dm_put_device(ti, lc->dev);
6540e9cebe7SJosef Bacik 	dm_put_device(ti, lc->logdev);
6550e9cebe7SJosef Bacik 	kfree(lc);
6560e9cebe7SJosef Bacik }
6570e9cebe7SJosef Bacik 
6580e9cebe7SJosef Bacik static void normal_map_bio(struct dm_target *ti, struct bio *bio)
6590e9cebe7SJosef Bacik {
6600e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
6610e9cebe7SJosef Bacik 
66274d46992SChristoph Hellwig 	bio_set_dev(bio, lc->dev->bdev);
6630e9cebe7SJosef Bacik }
6640e9cebe7SJosef Bacik 
6650e9cebe7SJosef Bacik static int log_writes_map(struct dm_target *ti, struct bio *bio)
6660e9cebe7SJosef Bacik {
6670e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
6680e9cebe7SJosef Bacik 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
6690e9cebe7SJosef Bacik 	struct pending_block *block;
6700e9cebe7SJosef Bacik 	struct bvec_iter iter;
6710e9cebe7SJosef Bacik 	struct bio_vec bv;
6720e9cebe7SJosef Bacik 	size_t alloc_size;
6730e9cebe7SJosef Bacik 	int i = 0;
6741eff9d32SJens Axboe 	bool flush_bio = (bio->bi_opf & REQ_PREFLUSH);
6751eff9d32SJens Axboe 	bool fua_bio = (bio->bi_opf & REQ_FUA);
676e6047149SMike Christie 	bool discard_bio = (bio_op(bio) == REQ_OP_DISCARD);
677e5c4cb9bSQu Wenruo 	bool meta_bio = (bio->bi_opf & REQ_META);
6780e9cebe7SJosef Bacik 
6790e9cebe7SJosef Bacik 	pb->block = NULL;
6800e9cebe7SJosef Bacik 
6810e9cebe7SJosef Bacik 	/* Don't bother doing anything if logging has been disabled */
6820e9cebe7SJosef Bacik 	if (!lc->logging_enabled)
6830e9cebe7SJosef Bacik 		goto map_bio;
6840e9cebe7SJosef Bacik 
6850e9cebe7SJosef Bacik 	/*
6860e9cebe7SJosef Bacik 	 * Map reads as normal.
6870e9cebe7SJosef Bacik 	 */
6880e9cebe7SJosef Bacik 	if (bio_data_dir(bio) == READ)
6890e9cebe7SJosef Bacik 		goto map_bio;
6900e9cebe7SJosef Bacik 
6910e9cebe7SJosef Bacik 	/* No sectors and not a flush?  Don't care */
6920e9cebe7SJosef Bacik 	if (!bio_sectors(bio) && !flush_bio)
6930e9cebe7SJosef Bacik 		goto map_bio;
6940e9cebe7SJosef Bacik 
6950e9cebe7SJosef Bacik 	/*
6960e9cebe7SJosef Bacik 	 * Discards will have bi_size set but there's no actual data, so just
6970e9cebe7SJosef Bacik 	 * allocate the size of the pending block.
6980e9cebe7SJosef Bacik 	 */
6990e9cebe7SJosef Bacik 	if (discard_bio)
7000e9cebe7SJosef Bacik 		alloc_size = sizeof(struct pending_block);
7010e9cebe7SJosef Bacik 	else
702d4e6e836SZhengyuan Liu 		alloc_size = struct_size(block, vecs, bio_segments(bio));
7030e9cebe7SJosef Bacik 
7040e9cebe7SJosef Bacik 	block = kzalloc(alloc_size, GFP_NOIO);
7050e9cebe7SJosef Bacik 	if (!block) {
7060e9cebe7SJosef Bacik 		DMERR("Error allocating pending block");
7070e9cebe7SJosef Bacik 		spin_lock_irq(&lc->blocks_lock);
7080e9cebe7SJosef Bacik 		lc->logging_enabled = false;
7090e9cebe7SJosef Bacik 		spin_unlock_irq(&lc->blocks_lock);
710846785e6SChristoph Hellwig 		return DM_MAPIO_KILL;
7110e9cebe7SJosef Bacik 	}
7120e9cebe7SJosef Bacik 	INIT_LIST_HEAD(&block->list);
7130e9cebe7SJosef Bacik 	pb->block = block;
7140e9cebe7SJosef Bacik 	atomic_inc(&lc->pending_blocks);
7150e9cebe7SJosef Bacik 
7160e9cebe7SJosef Bacik 	if (flush_bio)
7170e9cebe7SJosef Bacik 		block->flags |= LOG_FLUSH_FLAG;
7180e9cebe7SJosef Bacik 	if (fua_bio)
7190e9cebe7SJosef Bacik 		block->flags |= LOG_FUA_FLAG;
7200e9cebe7SJosef Bacik 	if (discard_bio)
7210e9cebe7SJosef Bacik 		block->flags |= LOG_DISCARD_FLAG;
722e5c4cb9bSQu Wenruo 	if (meta_bio)
723e5c4cb9bSQu Wenruo 		block->flags |= LOG_METADATA_FLAG;
7240e9cebe7SJosef Bacik 
725228bb5b2SJosef Bacik 	block->sector = bio_to_dev_sectors(lc, bio->bi_iter.bi_sector);
726228bb5b2SJosef Bacik 	block->nr_sectors = bio_to_dev_sectors(lc, bio_sectors(bio));
7270e9cebe7SJosef Bacik 
7280e9cebe7SJosef Bacik 	/* We don't need the data, just submit */
7290e9cebe7SJosef Bacik 	if (discard_bio) {
7300e9cebe7SJosef Bacik 		WARN_ON(flush_bio || fua_bio);
7310e9cebe7SJosef Bacik 		if (lc->device_supports_discard)
7320e9cebe7SJosef Bacik 			goto map_bio;
7334246a0b6SChristoph Hellwig 		bio_endio(bio);
7340e9cebe7SJosef Bacik 		return DM_MAPIO_SUBMITTED;
7350e9cebe7SJosef Bacik 	}
7360e9cebe7SJosef Bacik 
7370e9cebe7SJosef Bacik 	/* Flush bio, splice the unflushed blocks onto this list and submit */
7380e9cebe7SJosef Bacik 	if (flush_bio && !bio_sectors(bio)) {
7390e9cebe7SJosef Bacik 		spin_lock_irq(&lc->blocks_lock);
7400e9cebe7SJosef Bacik 		list_splice_init(&lc->unflushed_blocks, &block->list);
7410e9cebe7SJosef Bacik 		spin_unlock_irq(&lc->blocks_lock);
7420e9cebe7SJosef Bacik 		goto map_bio;
7430e9cebe7SJosef Bacik 	}
7440e9cebe7SJosef Bacik 
7450e9cebe7SJosef Bacik 	/*
7460e9cebe7SJosef Bacik 	 * We will write this bio somewhere else way later so we need to copy
7470e9cebe7SJosef Bacik 	 * the actual contents into new pages so we know the data will always be
7480e9cebe7SJosef Bacik 	 * there.
7490e9cebe7SJosef Bacik 	 *
7500e9cebe7SJosef Bacik 	 * We do this because this could be a bio from O_DIRECT in which case we
7510e9cebe7SJosef Bacik 	 * can't just hold onto the page until some later point, we have to
7520e9cebe7SJosef Bacik 	 * manually copy the contents.
7530e9cebe7SJosef Bacik 	 */
7540e9cebe7SJosef Bacik 	bio_for_each_segment(bv, bio, iter) {
7550e9cebe7SJosef Bacik 		struct page *page;
756*27db2717SChristoph Hellwig 		void *dst;
7570e9cebe7SJosef Bacik 
7580e9cebe7SJosef Bacik 		page = alloc_page(GFP_NOIO);
7590e9cebe7SJosef Bacik 		if (!page) {
7600e9cebe7SJosef Bacik 			DMERR("Error allocing page");
7610e9cebe7SJosef Bacik 			free_pending_block(lc, block);
7620e9cebe7SJosef Bacik 			spin_lock_irq(&lc->blocks_lock);
7630e9cebe7SJosef Bacik 			lc->logging_enabled = false;
7640e9cebe7SJosef Bacik 			spin_unlock_irq(&lc->blocks_lock);
765846785e6SChristoph Hellwig 			return DM_MAPIO_KILL;
7660e9cebe7SJosef Bacik 		}
7670e9cebe7SJosef Bacik 
7680e9cebe7SJosef Bacik 		dst = kmap_atomic(page);
769*27db2717SChristoph Hellwig 		memcpy_from_bvec(dst, &bv);
7700e9cebe7SJosef Bacik 		kunmap_atomic(dst);
7710e9cebe7SJosef Bacik 		block->vecs[i].bv_page = page;
7720e9cebe7SJosef Bacik 		block->vecs[i].bv_len = bv.bv_len;
7730e9cebe7SJosef Bacik 		block->vec_cnt++;
7740e9cebe7SJosef Bacik 		i++;
7750e9cebe7SJosef Bacik 	}
7760e9cebe7SJosef Bacik 
7770e9cebe7SJosef Bacik 	/* Had a flush with data in it, weird */
7780e9cebe7SJosef Bacik 	if (flush_bio) {
7790e9cebe7SJosef Bacik 		spin_lock_irq(&lc->blocks_lock);
7800e9cebe7SJosef Bacik 		list_splice_init(&lc->unflushed_blocks, &block->list);
7810e9cebe7SJosef Bacik 		spin_unlock_irq(&lc->blocks_lock);
7820e9cebe7SJosef Bacik 	}
7830e9cebe7SJosef Bacik map_bio:
7840e9cebe7SJosef Bacik 	normal_map_bio(ti, bio);
7850e9cebe7SJosef Bacik 	return DM_MAPIO_REMAPPED;
7860e9cebe7SJosef Bacik }
7870e9cebe7SJosef Bacik 
7884e4cbee9SChristoph Hellwig static int normal_end_io(struct dm_target *ti, struct bio *bio,
7894e4cbee9SChristoph Hellwig 		blk_status_t *error)
7900e9cebe7SJosef Bacik {
7910e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
7920e9cebe7SJosef Bacik 	struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
7930e9cebe7SJosef Bacik 
7940e9cebe7SJosef Bacik 	if (bio_data_dir(bio) == WRITE && pb->block) {
7950e9cebe7SJosef Bacik 		struct pending_block *block = pb->block;
7960e9cebe7SJosef Bacik 		unsigned long flags;
7970e9cebe7SJosef Bacik 
7980e9cebe7SJosef Bacik 		spin_lock_irqsave(&lc->blocks_lock, flags);
7990e9cebe7SJosef Bacik 		if (block->flags & LOG_FLUSH_FLAG) {
8000e9cebe7SJosef Bacik 			list_splice_tail_init(&block->list, &lc->logging_blocks);
8010e9cebe7SJosef Bacik 			list_add_tail(&block->list, &lc->logging_blocks);
8020e9cebe7SJosef Bacik 			wake_up_process(lc->log_kthread);
8030e9cebe7SJosef Bacik 		} else if (block->flags & LOG_FUA_FLAG) {
8040e9cebe7SJosef Bacik 			list_add_tail(&block->list, &lc->logging_blocks);
8050e9cebe7SJosef Bacik 			wake_up_process(lc->log_kthread);
8060e9cebe7SJosef Bacik 		} else
8070e9cebe7SJosef Bacik 			list_add_tail(&block->list, &lc->unflushed_blocks);
8080e9cebe7SJosef Bacik 		spin_unlock_irqrestore(&lc->blocks_lock, flags);
8090e9cebe7SJosef Bacik 	}
8100e9cebe7SJosef Bacik 
8111be56909SChristoph Hellwig 	return DM_ENDIO_DONE;
8120e9cebe7SJosef Bacik }
8130e9cebe7SJosef Bacik 
8140e9cebe7SJosef Bacik /*
8150e9cebe7SJosef Bacik  * INFO format: <logged entries> <highest allocated sector>
8160e9cebe7SJosef Bacik  */
8170e9cebe7SJosef Bacik static void log_writes_status(struct dm_target *ti, status_type_t type,
8180e9cebe7SJosef Bacik 			      unsigned status_flags, char *result,
8190e9cebe7SJosef Bacik 			      unsigned maxlen)
8200e9cebe7SJosef Bacik {
8210e9cebe7SJosef Bacik 	unsigned sz = 0;
8220e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
8230e9cebe7SJosef Bacik 
8240e9cebe7SJosef Bacik 	switch (type) {
8250e9cebe7SJosef Bacik 	case STATUSTYPE_INFO:
8260e9cebe7SJosef Bacik 		DMEMIT("%llu %llu", lc->logged_entries,
8270e9cebe7SJosef Bacik 		       (unsigned long long)lc->next_sector - 1);
8280e9cebe7SJosef Bacik 		if (!lc->logging_enabled)
8290e9cebe7SJosef Bacik 			DMEMIT(" logging_disabled");
8300e9cebe7SJosef Bacik 		break;
8310e9cebe7SJosef Bacik 
8320e9cebe7SJosef Bacik 	case STATUSTYPE_TABLE:
8330e9cebe7SJosef Bacik 		DMEMIT("%s %s", lc->dev->name, lc->logdev->name);
8340e9cebe7SJosef Bacik 		break;
8358ec45662STushar Sugandhi 
8368ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
8378ec45662STushar Sugandhi 		*result = '\0';
8388ec45662STushar Sugandhi 		break;
8390e9cebe7SJosef Bacik 	}
8400e9cebe7SJosef Bacik }
8410e9cebe7SJosef Bacik 
842e56f81e0SChristoph Hellwig static int log_writes_prepare_ioctl(struct dm_target *ti,
8435bd5e8d8SMike Snitzer 				    struct block_device **bdev)
8440e9cebe7SJosef Bacik {
8450e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
8460e9cebe7SJosef Bacik 	struct dm_dev *dev = lc->dev;
8470e9cebe7SJosef Bacik 
848e56f81e0SChristoph Hellwig 	*bdev = dev->bdev;
8490e9cebe7SJosef Bacik 	/*
8500e9cebe7SJosef Bacik 	 * Only pass ioctls through if the device sizes match exactly.
8510e9cebe7SJosef Bacik 	 */
8520e9cebe7SJosef Bacik 	if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
853e56f81e0SChristoph Hellwig 		return 1;
854e56f81e0SChristoph Hellwig 	return 0;
8550e9cebe7SJosef Bacik }
8560e9cebe7SJosef Bacik 
8570e9cebe7SJosef Bacik static int log_writes_iterate_devices(struct dm_target *ti,
8580e9cebe7SJosef Bacik 				      iterate_devices_callout_fn fn,
8590e9cebe7SJosef Bacik 				      void *data)
8600e9cebe7SJosef Bacik {
8610e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
8620e9cebe7SJosef Bacik 
8630e9cebe7SJosef Bacik 	return fn(ti, lc->dev, 0, ti->len, data);
8640e9cebe7SJosef Bacik }
8650e9cebe7SJosef Bacik 
8660e9cebe7SJosef Bacik /*
8670e9cebe7SJosef Bacik  * Messages supported:
8680e9cebe7SJosef Bacik  *   mark <mark data> - specify the marked data.
8690e9cebe7SJosef Bacik  */
8701eb5fa84SMike Snitzer static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv,
8711eb5fa84SMike Snitzer 			      char *result, unsigned maxlen)
8720e9cebe7SJosef Bacik {
8730e9cebe7SJosef Bacik 	int r = -EINVAL;
8740e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
8750e9cebe7SJosef Bacik 
8760e9cebe7SJosef Bacik 	if (argc != 2) {
8770e9cebe7SJosef Bacik 		DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc);
8780e9cebe7SJosef Bacik 		return r;
8790e9cebe7SJosef Bacik 	}
8800e9cebe7SJosef Bacik 
8810e9cebe7SJosef Bacik 	if (!strcasecmp(argv[0], "mark"))
8820e9cebe7SJosef Bacik 		r = log_mark(lc, argv[1]);
8830e9cebe7SJosef Bacik 	else
8840e9cebe7SJosef Bacik 		DMWARN("Unrecognised log writes target message received: %s", argv[0]);
8850e9cebe7SJosef Bacik 
8860e9cebe7SJosef Bacik 	return r;
8870e9cebe7SJosef Bacik }
8880e9cebe7SJosef Bacik 
8890e9cebe7SJosef Bacik static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits)
8900e9cebe7SJosef Bacik {
8910e9cebe7SJosef Bacik 	struct log_writes_c *lc = ti->private;
8920e9cebe7SJosef Bacik 	struct request_queue *q = bdev_get_queue(lc->dev->bdev);
8930e9cebe7SJosef Bacik 
8940e9cebe7SJosef Bacik 	if (!q || !blk_queue_discard(q)) {
8950e9cebe7SJosef Bacik 		lc->device_supports_discard = false;
896228bb5b2SJosef Bacik 		limits->discard_granularity = lc->sectorsize;
8970e9cebe7SJosef Bacik 		limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT);
8980e9cebe7SJosef Bacik 	}
899228bb5b2SJosef Bacik 	limits->logical_block_size = bdev_logical_block_size(lc->dev->bdev);
900228bb5b2SJosef Bacik 	limits->physical_block_size = bdev_physical_block_size(lc->dev->bdev);
901228bb5b2SJosef Bacik 	limits->io_min = limits->physical_block_size;
9020e9cebe7SJosef Bacik }
9030e9cebe7SJosef Bacik 
904976431b0SDan Williams #if IS_ENABLED(CONFIG_DAX_DRIVER)
905976431b0SDan Williams static int log_dax(struct log_writes_c *lc, sector_t sector, size_t bytes,
906976431b0SDan Williams 		   struct iov_iter *i)
907976431b0SDan Williams {
908976431b0SDan Williams 	struct pending_block *block;
909976431b0SDan Williams 
910976431b0SDan Williams 	if (!bytes)
911976431b0SDan Williams 		return 0;
912976431b0SDan Williams 
913976431b0SDan Williams 	block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
914976431b0SDan Williams 	if (!block) {
915976431b0SDan Williams 		DMERR("Error allocating dax pending block");
916976431b0SDan Williams 		return -ENOMEM;
917976431b0SDan Williams 	}
918976431b0SDan Williams 
919976431b0SDan Williams 	block->data = kzalloc(bytes, GFP_KERNEL);
920976431b0SDan Williams 	if (!block->data) {
921976431b0SDan Williams 		DMERR("Error allocating dax data space");
922976431b0SDan Williams 		kfree(block);
923976431b0SDan Williams 		return -ENOMEM;
924976431b0SDan Williams 	}
925976431b0SDan Williams 
926976431b0SDan Williams 	/* write data provided via the iterator */
927976431b0SDan Williams 	if (!copy_from_iter(block->data, bytes, i)) {
928976431b0SDan Williams 		DMERR("Error copying dax data");
929976431b0SDan Williams 		kfree(block->data);
930976431b0SDan Williams 		kfree(block);
931976431b0SDan Williams 		return -EIO;
932976431b0SDan Williams 	}
933976431b0SDan Williams 
934976431b0SDan Williams 	/* rewind the iterator so that the block driver can use it */
935976431b0SDan Williams 	iov_iter_revert(i, bytes);
936976431b0SDan Williams 
937976431b0SDan Williams 	block->datalen = bytes;
938976431b0SDan Williams 	block->sector = bio_to_dev_sectors(lc, sector);
939976431b0SDan Williams 	block->nr_sectors = ALIGN(bytes, lc->sectorsize) >> lc->sectorshift;
940976431b0SDan Williams 
941976431b0SDan Williams 	atomic_inc(&lc->pending_blocks);
942976431b0SDan Williams 	spin_lock_irq(&lc->blocks_lock);
943976431b0SDan Williams 	list_add_tail(&block->list, &lc->unflushed_blocks);
944976431b0SDan Williams 	spin_unlock_irq(&lc->blocks_lock);
945976431b0SDan Williams 	wake_up_process(lc->log_kthread);
946976431b0SDan Williams 
947976431b0SDan Williams 	return 0;
948976431b0SDan Williams }
949976431b0SDan Williams 
95098d82f48SRoss Zwisler static long log_writes_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
95198d82f48SRoss Zwisler 					 long nr_pages, void **kaddr, pfn_t *pfn)
95298d82f48SRoss Zwisler {
95398d82f48SRoss Zwisler 	struct log_writes_c *lc = ti->private;
95498d82f48SRoss Zwisler 	sector_t sector = pgoff * PAGE_SECTORS;
95598d82f48SRoss Zwisler 	int ret;
95698d82f48SRoss Zwisler 
95798d82f48SRoss Zwisler 	ret = bdev_dax_pgoff(lc->dev->bdev, sector, nr_pages * PAGE_SIZE, &pgoff);
95898d82f48SRoss Zwisler 	if (ret)
95998d82f48SRoss Zwisler 		return ret;
96098d82f48SRoss Zwisler 	return dax_direct_access(lc->dev->dax_dev, pgoff, nr_pages, kaddr, pfn);
96198d82f48SRoss Zwisler }
96298d82f48SRoss Zwisler 
96398d82f48SRoss Zwisler static size_t log_writes_dax_copy_from_iter(struct dm_target *ti,
96498d82f48SRoss Zwisler 					    pgoff_t pgoff, void *addr, size_t bytes,
96598d82f48SRoss Zwisler 					    struct iov_iter *i)
96698d82f48SRoss Zwisler {
96798d82f48SRoss Zwisler 	struct log_writes_c *lc = ti->private;
96898d82f48SRoss Zwisler 	sector_t sector = pgoff * PAGE_SECTORS;
96998d82f48SRoss Zwisler 	int err;
97098d82f48SRoss Zwisler 
97198d82f48SRoss Zwisler 	if (bdev_dax_pgoff(lc->dev->bdev, sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
97298d82f48SRoss Zwisler 		return 0;
97398d82f48SRoss Zwisler 
97498d82f48SRoss Zwisler 	/* Don't bother doing anything if logging has been disabled */
97598d82f48SRoss Zwisler 	if (!lc->logging_enabled)
97698d82f48SRoss Zwisler 		goto dax_copy;
97798d82f48SRoss Zwisler 
97898d82f48SRoss Zwisler 	err = log_dax(lc, sector, bytes, i);
97998d82f48SRoss Zwisler 	if (err) {
98098d82f48SRoss Zwisler 		DMWARN("Error %d logging DAX write", err);
98198d82f48SRoss Zwisler 		return 0;
98298d82f48SRoss Zwisler 	}
98398d82f48SRoss Zwisler dax_copy:
98498d82f48SRoss Zwisler 	return dax_copy_from_iter(lc->dev->dax_dev, pgoff, addr, bytes, i);
98598d82f48SRoss Zwisler }
986b3a9a0c3SDan Williams 
987b3a9a0c3SDan Williams static size_t log_writes_dax_copy_to_iter(struct dm_target *ti,
988b3a9a0c3SDan Williams 					  pgoff_t pgoff, void *addr, size_t bytes,
989b3a9a0c3SDan Williams 					  struct iov_iter *i)
990b3a9a0c3SDan Williams {
991b3a9a0c3SDan Williams 	struct log_writes_c *lc = ti->private;
992b3a9a0c3SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
993b3a9a0c3SDan Williams 
994b3a9a0c3SDan Williams 	if (bdev_dax_pgoff(lc->dev->bdev, sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
995b3a9a0c3SDan Williams 		return 0;
996b3a9a0c3SDan Williams 	return dax_copy_to_iter(lc->dev->dax_dev, pgoff, addr, bytes, i);
997b3a9a0c3SDan Williams }
998b3a9a0c3SDan Williams 
999cdf6cdcdSVivek Goyal static int log_writes_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff,
1000cdf6cdcdSVivek Goyal 					  size_t nr_pages)
1001cdf6cdcdSVivek Goyal {
1002cdf6cdcdSVivek Goyal 	int ret;
1003cdf6cdcdSVivek Goyal 	struct log_writes_c *lc = ti->private;
1004cdf6cdcdSVivek Goyal 	sector_t sector = pgoff * PAGE_SECTORS;
1005cdf6cdcdSVivek Goyal 
1006cdf6cdcdSVivek Goyal 	ret = bdev_dax_pgoff(lc->dev->bdev, sector, nr_pages << PAGE_SHIFT,
1007cdf6cdcdSVivek Goyal 			     &pgoff);
1008cdf6cdcdSVivek Goyal 	if (ret)
1009cdf6cdcdSVivek Goyal 		return ret;
1010cdf6cdcdSVivek Goyal 	return dax_zero_page_range(lc->dev->dax_dev, pgoff,
1011cdf6cdcdSVivek Goyal 				   nr_pages << PAGE_SHIFT);
1012cdf6cdcdSVivek Goyal }
1013cdf6cdcdSVivek Goyal 
1014976431b0SDan Williams #else
1015976431b0SDan Williams #define log_writes_dax_direct_access NULL
1016976431b0SDan Williams #define log_writes_dax_copy_from_iter NULL
1017b3a9a0c3SDan Williams #define log_writes_dax_copy_to_iter NULL
1018cdf6cdcdSVivek Goyal #define log_writes_dax_zero_page_range NULL
1019976431b0SDan Williams #endif
102098d82f48SRoss Zwisler 
10210e9cebe7SJosef Bacik static struct target_type log_writes_target = {
10220e9cebe7SJosef Bacik 	.name   = "log-writes",
102398d82f48SRoss Zwisler 	.version = {1, 1, 0},
10240e9cebe7SJosef Bacik 	.module = THIS_MODULE,
10250e9cebe7SJosef Bacik 	.ctr    = log_writes_ctr,
10260e9cebe7SJosef Bacik 	.dtr    = log_writes_dtr,
10270e9cebe7SJosef Bacik 	.map    = log_writes_map,
10280e9cebe7SJosef Bacik 	.end_io = normal_end_io,
10290e9cebe7SJosef Bacik 	.status = log_writes_status,
1030e56f81e0SChristoph Hellwig 	.prepare_ioctl = log_writes_prepare_ioctl,
10310e9cebe7SJosef Bacik 	.message = log_writes_message,
10320e9cebe7SJosef Bacik 	.iterate_devices = log_writes_iterate_devices,
10330e9cebe7SJosef Bacik 	.io_hints = log_writes_io_hints,
103498d82f48SRoss Zwisler 	.direct_access = log_writes_dax_direct_access,
103598d82f48SRoss Zwisler 	.dax_copy_from_iter = log_writes_dax_copy_from_iter,
1036b3a9a0c3SDan Williams 	.dax_copy_to_iter = log_writes_dax_copy_to_iter,
1037cdf6cdcdSVivek Goyal 	.dax_zero_page_range = log_writes_dax_zero_page_range,
10380e9cebe7SJosef Bacik };
10390e9cebe7SJosef Bacik 
10400e9cebe7SJosef Bacik static int __init dm_log_writes_init(void)
10410e9cebe7SJosef Bacik {
10420e9cebe7SJosef Bacik 	int r = dm_register_target(&log_writes_target);
10430e9cebe7SJosef Bacik 
10440e9cebe7SJosef Bacik 	if (r < 0)
10450e9cebe7SJosef Bacik 		DMERR("register failed %d", r);
10460e9cebe7SJosef Bacik 
10470e9cebe7SJosef Bacik 	return r;
10480e9cebe7SJosef Bacik }
10490e9cebe7SJosef Bacik 
10500e9cebe7SJosef Bacik static void __exit dm_log_writes_exit(void)
10510e9cebe7SJosef Bacik {
10520e9cebe7SJosef Bacik 	dm_unregister_target(&log_writes_target);
10530e9cebe7SJosef Bacik }
10540e9cebe7SJosef Bacik 
10550e9cebe7SJosef Bacik module_init(dm_log_writes_init);
10560e9cebe7SJosef Bacik module_exit(dm_log_writes_exit);
10570e9cebe7SJosef Bacik 
10580e9cebe7SJosef Bacik MODULE_DESCRIPTION(DM_NAME " log writes target");
10590e9cebe7SJosef Bacik MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
10600e9cebe7SJosef Bacik MODULE_LICENSE("GPL");
1061