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> 130e9cebe7SJosef Bacik #include <linux/slab.h> 140e9cebe7SJosef Bacik #include <linux/kthread.h> 150e9cebe7SJosef Bacik #include <linux/freezer.h> 160e9cebe7SJosef Bacik 170e9cebe7SJosef Bacik #define DM_MSG_PREFIX "log-writes" 180e9cebe7SJosef Bacik 190e9cebe7SJosef Bacik /* 200e9cebe7SJosef Bacik * This target will sequentially log all writes to the target device onto the 210e9cebe7SJosef Bacik * log device. This is helpful for replaying writes to check for fs consistency 220e9cebe7SJosef Bacik * at all times. This target provides a mechanism to mark specific events to 230e9cebe7SJosef Bacik * check data at a later time. So for example you would: 240e9cebe7SJosef Bacik * 250e9cebe7SJosef Bacik * write data 260e9cebe7SJosef Bacik * fsync 270e9cebe7SJosef Bacik * dmsetup message /dev/whatever mark mymark 280e9cebe7SJosef Bacik * unmount /mnt/test 290e9cebe7SJosef Bacik * 300e9cebe7SJosef Bacik * Then replay the log up to mymark and check the contents of the replay to 310e9cebe7SJosef Bacik * verify it matches what was written. 320e9cebe7SJosef Bacik * 330e9cebe7SJosef Bacik * We log writes only after they have been flushed, this makes the log describe 340e9cebe7SJosef Bacik * close to the order in which the data hits the actual disk, not its cache. So 350e9cebe7SJosef Bacik * for example the following sequence (W means write, C means complete) 360e9cebe7SJosef Bacik * 370e9cebe7SJosef Bacik * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd 380e9cebe7SJosef Bacik * 390e9cebe7SJosef Bacik * Would result in the log looking like this: 400e9cebe7SJosef Bacik * 410e9cebe7SJosef Bacik * c,a,flush,fuad,b,<other writes>,<next flush> 420e9cebe7SJosef Bacik * 430e9cebe7SJosef Bacik * This is meant to help expose problems where file systems do not properly wait 440e9cebe7SJosef Bacik * on data being written before invoking a FLUSH. FUA bypasses cache so once it 450e9cebe7SJosef Bacik * completes it is added to the log as it should be on disk. 460e9cebe7SJosef Bacik * 470e9cebe7SJosef Bacik * We treat DISCARDs as if they don't bypass cache so that they are logged in 480e9cebe7SJosef Bacik * order of completion along with the normal writes. If we didn't do it this 490e9cebe7SJosef Bacik * way we would process all the discards first and then write all the data, when 500e9cebe7SJosef Bacik * in fact we want to do the data and the discard in the order that they 510e9cebe7SJosef Bacik * completed. 520e9cebe7SJosef Bacik */ 530e9cebe7SJosef Bacik #define LOG_FLUSH_FLAG (1 << 0) 540e9cebe7SJosef Bacik #define LOG_FUA_FLAG (1 << 1) 550e9cebe7SJosef Bacik #define LOG_DISCARD_FLAG (1 << 2) 560e9cebe7SJosef Bacik #define LOG_MARK_FLAG (1 << 3) 570e9cebe7SJosef Bacik 58*f4ad317aSGeert Uytterhoeven #define WRITE_LOG_VERSION 1ULL 59*f4ad317aSGeert Uytterhoeven #define WRITE_LOG_MAGIC 0x6a736677736872ULL 600e9cebe7SJosef Bacik 610e9cebe7SJosef Bacik /* 620e9cebe7SJosef Bacik * The disk format for this is braindead simple. 630e9cebe7SJosef Bacik * 640e9cebe7SJosef Bacik * At byte 0 we have our super, followed by the following sequence for 650e9cebe7SJosef Bacik * nr_entries: 660e9cebe7SJosef Bacik * 670e9cebe7SJosef Bacik * [ 1 sector ][ entry->nr_sectors ] 680e9cebe7SJosef Bacik * [log_write_entry][ data written ] 690e9cebe7SJosef Bacik * 700e9cebe7SJosef Bacik * The log_write_entry takes up a full sector so we can have arbitrary length 710e9cebe7SJosef Bacik * marks and it leaves us room for extra content in the future. 720e9cebe7SJosef Bacik */ 730e9cebe7SJosef Bacik 740e9cebe7SJosef Bacik /* 750e9cebe7SJosef Bacik * Basic info about the log for userspace. 760e9cebe7SJosef Bacik */ 770e9cebe7SJosef Bacik struct log_write_super { 780e9cebe7SJosef Bacik __le64 magic; 790e9cebe7SJosef Bacik __le64 version; 800e9cebe7SJosef Bacik __le64 nr_entries; 810e9cebe7SJosef Bacik __le32 sectorsize; 820e9cebe7SJosef Bacik }; 830e9cebe7SJosef Bacik 840e9cebe7SJosef Bacik /* 850e9cebe7SJosef Bacik * sector - the sector we wrote. 860e9cebe7SJosef Bacik * nr_sectors - the number of sectors we wrote. 870e9cebe7SJosef Bacik * flags - flags for this log entry. 880e9cebe7SJosef Bacik * data_len - the size of the data in this log entry, this is for private log 890e9cebe7SJosef Bacik * entry stuff, the MARK data provided by userspace for example. 900e9cebe7SJosef Bacik */ 910e9cebe7SJosef Bacik struct log_write_entry { 920e9cebe7SJosef Bacik __le64 sector; 930e9cebe7SJosef Bacik __le64 nr_sectors; 940e9cebe7SJosef Bacik __le64 flags; 950e9cebe7SJosef Bacik __le64 data_len; 960e9cebe7SJosef Bacik }; 970e9cebe7SJosef Bacik 980e9cebe7SJosef Bacik struct log_writes_c { 990e9cebe7SJosef Bacik struct dm_dev *dev; 1000e9cebe7SJosef Bacik struct dm_dev *logdev; 1010e9cebe7SJosef Bacik u64 logged_entries; 1020e9cebe7SJosef Bacik u32 sectorsize; 1030e9cebe7SJosef Bacik atomic_t io_blocks; 1040e9cebe7SJosef Bacik atomic_t pending_blocks; 1050e9cebe7SJosef Bacik sector_t next_sector; 1060e9cebe7SJosef Bacik sector_t end_sector; 1070e9cebe7SJosef Bacik bool logging_enabled; 1080e9cebe7SJosef Bacik bool device_supports_discard; 1090e9cebe7SJosef Bacik spinlock_t blocks_lock; 1100e9cebe7SJosef Bacik struct list_head unflushed_blocks; 1110e9cebe7SJosef Bacik struct list_head logging_blocks; 1120e9cebe7SJosef Bacik wait_queue_head_t wait; 1130e9cebe7SJosef Bacik struct task_struct *log_kthread; 1140e9cebe7SJosef Bacik }; 1150e9cebe7SJosef Bacik 1160e9cebe7SJosef Bacik struct pending_block { 1170e9cebe7SJosef Bacik int vec_cnt; 1180e9cebe7SJosef Bacik u64 flags; 1190e9cebe7SJosef Bacik sector_t sector; 1200e9cebe7SJosef Bacik sector_t nr_sectors; 1210e9cebe7SJosef Bacik char *data; 1220e9cebe7SJosef Bacik u32 datalen; 1230e9cebe7SJosef Bacik struct list_head list; 1240e9cebe7SJosef Bacik struct bio_vec vecs[0]; 1250e9cebe7SJosef Bacik }; 1260e9cebe7SJosef Bacik 1270e9cebe7SJosef Bacik struct per_bio_data { 1280e9cebe7SJosef Bacik struct pending_block *block; 1290e9cebe7SJosef Bacik }; 1300e9cebe7SJosef Bacik 1310e9cebe7SJosef Bacik static void put_pending_block(struct log_writes_c *lc) 1320e9cebe7SJosef Bacik { 1330e9cebe7SJosef Bacik if (atomic_dec_and_test(&lc->pending_blocks)) { 1340e9cebe7SJosef Bacik smp_mb__after_atomic(); 1350e9cebe7SJosef Bacik if (waitqueue_active(&lc->wait)) 1360e9cebe7SJosef Bacik wake_up(&lc->wait); 1370e9cebe7SJosef Bacik } 1380e9cebe7SJosef Bacik } 1390e9cebe7SJosef Bacik 1400e9cebe7SJosef Bacik static void put_io_block(struct log_writes_c *lc) 1410e9cebe7SJosef Bacik { 1420e9cebe7SJosef Bacik if (atomic_dec_and_test(&lc->io_blocks)) { 1430e9cebe7SJosef Bacik smp_mb__after_atomic(); 1440e9cebe7SJosef Bacik if (waitqueue_active(&lc->wait)) 1450e9cebe7SJosef Bacik wake_up(&lc->wait); 1460e9cebe7SJosef Bacik } 1470e9cebe7SJosef Bacik } 1480e9cebe7SJosef Bacik 1490e9cebe7SJosef Bacik static void log_end_io(struct bio *bio, int err) 1500e9cebe7SJosef Bacik { 1510e9cebe7SJosef Bacik struct log_writes_c *lc = bio->bi_private; 1520e9cebe7SJosef Bacik struct bio_vec *bvec; 1530e9cebe7SJosef Bacik int i; 1540e9cebe7SJosef Bacik 1550e9cebe7SJosef Bacik if (err) { 1560e9cebe7SJosef Bacik unsigned long flags; 1570e9cebe7SJosef Bacik 1580e9cebe7SJosef Bacik DMERR("Error writing log block, error=%d", err); 1590e9cebe7SJosef Bacik spin_lock_irqsave(&lc->blocks_lock, flags); 1600e9cebe7SJosef Bacik lc->logging_enabled = false; 1610e9cebe7SJosef Bacik spin_unlock_irqrestore(&lc->blocks_lock, flags); 1620e9cebe7SJosef Bacik } 1630e9cebe7SJosef Bacik 1640e9cebe7SJosef Bacik bio_for_each_segment_all(bvec, bio, i) 1650e9cebe7SJosef Bacik __free_page(bvec->bv_page); 1660e9cebe7SJosef Bacik 1670e9cebe7SJosef Bacik put_io_block(lc); 1680e9cebe7SJosef Bacik bio_put(bio); 1690e9cebe7SJosef Bacik } 1700e9cebe7SJosef Bacik 1710e9cebe7SJosef Bacik /* 1720e9cebe7SJosef Bacik * Meant to be called if there is an error, it will free all the pages 1730e9cebe7SJosef Bacik * associated with the block. 1740e9cebe7SJosef Bacik */ 1750e9cebe7SJosef Bacik static void free_pending_block(struct log_writes_c *lc, 1760e9cebe7SJosef Bacik struct pending_block *block) 1770e9cebe7SJosef Bacik { 1780e9cebe7SJosef Bacik int i; 1790e9cebe7SJosef Bacik 1800e9cebe7SJosef Bacik for (i = 0; i < block->vec_cnt; i++) { 1810e9cebe7SJosef Bacik if (block->vecs[i].bv_page) 1820e9cebe7SJosef Bacik __free_page(block->vecs[i].bv_page); 1830e9cebe7SJosef Bacik } 1840e9cebe7SJosef Bacik kfree(block->data); 1850e9cebe7SJosef Bacik kfree(block); 1860e9cebe7SJosef Bacik put_pending_block(lc); 1870e9cebe7SJosef Bacik } 1880e9cebe7SJosef Bacik 1890e9cebe7SJosef Bacik static int write_metadata(struct log_writes_c *lc, void *entry, 1900e9cebe7SJosef Bacik size_t entrylen, void *data, size_t datalen, 1910e9cebe7SJosef Bacik sector_t sector) 1920e9cebe7SJosef Bacik { 1930e9cebe7SJosef Bacik struct bio *bio; 1940e9cebe7SJosef Bacik struct page *page; 1950e9cebe7SJosef Bacik void *ptr; 1960e9cebe7SJosef Bacik size_t ret; 1970e9cebe7SJosef Bacik 1980e9cebe7SJosef Bacik bio = bio_alloc(GFP_KERNEL, 1); 1990e9cebe7SJosef Bacik if (!bio) { 2000e9cebe7SJosef Bacik DMERR("Couldn't alloc log bio"); 2010e9cebe7SJosef Bacik goto error; 2020e9cebe7SJosef Bacik } 2030e9cebe7SJosef Bacik bio->bi_iter.bi_size = 0; 2040e9cebe7SJosef Bacik bio->bi_iter.bi_sector = sector; 2050e9cebe7SJosef Bacik bio->bi_bdev = lc->logdev->bdev; 2060e9cebe7SJosef Bacik bio->bi_end_io = log_end_io; 2070e9cebe7SJosef Bacik bio->bi_private = lc; 2080e9cebe7SJosef Bacik set_bit(BIO_UPTODATE, &bio->bi_flags); 2090e9cebe7SJosef Bacik 2100e9cebe7SJosef Bacik page = alloc_page(GFP_KERNEL); 2110e9cebe7SJosef Bacik if (!page) { 2120e9cebe7SJosef Bacik DMERR("Couldn't alloc log page"); 2130e9cebe7SJosef Bacik bio_put(bio); 2140e9cebe7SJosef Bacik goto error; 2150e9cebe7SJosef Bacik } 2160e9cebe7SJosef Bacik 2170e9cebe7SJosef Bacik ptr = kmap_atomic(page); 2180e9cebe7SJosef Bacik memcpy(ptr, entry, entrylen); 2190e9cebe7SJosef Bacik if (datalen) 2200e9cebe7SJosef Bacik memcpy(ptr + entrylen, data, datalen); 2210e9cebe7SJosef Bacik memset(ptr + entrylen + datalen, 0, 2220e9cebe7SJosef Bacik lc->sectorsize - entrylen - datalen); 2230e9cebe7SJosef Bacik kunmap_atomic(ptr); 2240e9cebe7SJosef Bacik 2250e9cebe7SJosef Bacik ret = bio_add_page(bio, page, lc->sectorsize, 0); 2260e9cebe7SJosef Bacik if (ret != lc->sectorsize) { 2270e9cebe7SJosef Bacik DMERR("Couldn't add page to the log block"); 2280e9cebe7SJosef Bacik goto error_bio; 2290e9cebe7SJosef Bacik } 2300e9cebe7SJosef Bacik submit_bio(WRITE, bio); 2310e9cebe7SJosef Bacik return 0; 2320e9cebe7SJosef Bacik error_bio: 2330e9cebe7SJosef Bacik bio_put(bio); 2340e9cebe7SJosef Bacik __free_page(page); 2350e9cebe7SJosef Bacik error: 2360e9cebe7SJosef Bacik put_io_block(lc); 2370e9cebe7SJosef Bacik return -1; 2380e9cebe7SJosef Bacik } 2390e9cebe7SJosef Bacik 2400e9cebe7SJosef Bacik static int log_one_block(struct log_writes_c *lc, 2410e9cebe7SJosef Bacik struct pending_block *block, sector_t sector) 2420e9cebe7SJosef Bacik { 2430e9cebe7SJosef Bacik struct bio *bio; 2440e9cebe7SJosef Bacik struct log_write_entry entry; 2450e9cebe7SJosef Bacik size_t ret; 2460e9cebe7SJosef Bacik int i; 2470e9cebe7SJosef Bacik 2480e9cebe7SJosef Bacik entry.sector = cpu_to_le64(block->sector); 2490e9cebe7SJosef Bacik entry.nr_sectors = cpu_to_le64(block->nr_sectors); 2500e9cebe7SJosef Bacik entry.flags = cpu_to_le64(block->flags); 2510e9cebe7SJosef Bacik entry.data_len = cpu_to_le64(block->datalen); 2520e9cebe7SJosef Bacik if (write_metadata(lc, &entry, sizeof(entry), block->data, 2530e9cebe7SJosef Bacik block->datalen, sector)) { 2540e9cebe7SJosef Bacik free_pending_block(lc, block); 2550e9cebe7SJosef Bacik return -1; 2560e9cebe7SJosef Bacik } 2570e9cebe7SJosef Bacik 2580e9cebe7SJosef Bacik if (!block->vec_cnt) 2590e9cebe7SJosef Bacik goto out; 2600e9cebe7SJosef Bacik sector++; 2610e9cebe7SJosef Bacik 2620e9cebe7SJosef Bacik bio = bio_alloc(GFP_KERNEL, block->vec_cnt); 2630e9cebe7SJosef Bacik if (!bio) { 2640e9cebe7SJosef Bacik DMERR("Couldn't alloc log bio"); 2650e9cebe7SJosef Bacik goto error; 2660e9cebe7SJosef Bacik } 2670e9cebe7SJosef Bacik atomic_inc(&lc->io_blocks); 2680e9cebe7SJosef Bacik bio->bi_iter.bi_size = 0; 2690e9cebe7SJosef Bacik bio->bi_iter.bi_sector = sector; 2700e9cebe7SJosef Bacik bio->bi_bdev = lc->logdev->bdev; 2710e9cebe7SJosef Bacik bio->bi_end_io = log_end_io; 2720e9cebe7SJosef Bacik bio->bi_private = lc; 2730e9cebe7SJosef Bacik set_bit(BIO_UPTODATE, &bio->bi_flags); 2740e9cebe7SJosef Bacik 2750e9cebe7SJosef Bacik for (i = 0; i < block->vec_cnt; i++) { 2760e9cebe7SJosef Bacik /* 2770e9cebe7SJosef Bacik * The page offset is always 0 because we allocate a new page 2780e9cebe7SJosef Bacik * for every bvec in the original bio for simplicity sake. 2790e9cebe7SJosef Bacik */ 2800e9cebe7SJosef Bacik ret = bio_add_page(bio, block->vecs[i].bv_page, 2810e9cebe7SJosef Bacik block->vecs[i].bv_len, 0); 2820e9cebe7SJosef Bacik if (ret != block->vecs[i].bv_len) { 2830e9cebe7SJosef Bacik atomic_inc(&lc->io_blocks); 2840e9cebe7SJosef Bacik submit_bio(WRITE, bio); 2850e9cebe7SJosef Bacik bio = bio_alloc(GFP_KERNEL, block->vec_cnt - i); 2860e9cebe7SJosef Bacik if (!bio) { 2870e9cebe7SJosef Bacik DMERR("Couldn't alloc log bio"); 2880e9cebe7SJosef Bacik goto error; 2890e9cebe7SJosef Bacik } 2900e9cebe7SJosef Bacik bio->bi_iter.bi_size = 0; 2910e9cebe7SJosef Bacik bio->bi_iter.bi_sector = sector; 2920e9cebe7SJosef Bacik bio->bi_bdev = lc->logdev->bdev; 2930e9cebe7SJosef Bacik bio->bi_end_io = log_end_io; 2940e9cebe7SJosef Bacik bio->bi_private = lc; 2950e9cebe7SJosef Bacik set_bit(BIO_UPTODATE, &bio->bi_flags); 2960e9cebe7SJosef Bacik 2970e9cebe7SJosef Bacik ret = bio_add_page(bio, block->vecs[i].bv_page, 2980e9cebe7SJosef Bacik block->vecs[i].bv_len, 0); 2990e9cebe7SJosef Bacik if (ret != block->vecs[i].bv_len) { 3000e9cebe7SJosef Bacik DMERR("Couldn't add page on new bio?"); 3010e9cebe7SJosef Bacik bio_put(bio); 3020e9cebe7SJosef Bacik goto error; 3030e9cebe7SJosef Bacik } 3040e9cebe7SJosef Bacik } 3050e9cebe7SJosef Bacik sector += block->vecs[i].bv_len >> SECTOR_SHIFT; 3060e9cebe7SJosef Bacik } 3070e9cebe7SJosef Bacik submit_bio(WRITE, bio); 3080e9cebe7SJosef Bacik out: 3090e9cebe7SJosef Bacik kfree(block->data); 3100e9cebe7SJosef Bacik kfree(block); 3110e9cebe7SJosef Bacik put_pending_block(lc); 3120e9cebe7SJosef Bacik return 0; 3130e9cebe7SJosef Bacik error: 3140e9cebe7SJosef Bacik free_pending_block(lc, block); 3150e9cebe7SJosef Bacik put_io_block(lc); 3160e9cebe7SJosef Bacik return -1; 3170e9cebe7SJosef Bacik } 3180e9cebe7SJosef Bacik 3190e9cebe7SJosef Bacik static int log_super(struct log_writes_c *lc) 3200e9cebe7SJosef Bacik { 3210e9cebe7SJosef Bacik struct log_write_super super; 3220e9cebe7SJosef Bacik 3230e9cebe7SJosef Bacik super.magic = cpu_to_le64(WRITE_LOG_MAGIC); 3240e9cebe7SJosef Bacik super.version = cpu_to_le64(WRITE_LOG_VERSION); 3250e9cebe7SJosef Bacik super.nr_entries = cpu_to_le64(lc->logged_entries); 3260e9cebe7SJosef Bacik super.sectorsize = cpu_to_le32(lc->sectorsize); 3270e9cebe7SJosef Bacik 3280e9cebe7SJosef Bacik if (write_metadata(lc, &super, sizeof(super), NULL, 0, 0)) { 3290e9cebe7SJosef Bacik DMERR("Couldn't write super"); 3300e9cebe7SJosef Bacik return -1; 3310e9cebe7SJosef Bacik } 3320e9cebe7SJosef Bacik 3330e9cebe7SJosef Bacik return 0; 3340e9cebe7SJosef Bacik } 3350e9cebe7SJosef Bacik 3360e9cebe7SJosef Bacik static inline sector_t logdev_last_sector(struct log_writes_c *lc) 3370e9cebe7SJosef Bacik { 3380e9cebe7SJosef Bacik return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT; 3390e9cebe7SJosef Bacik } 3400e9cebe7SJosef Bacik 3410e9cebe7SJosef Bacik static int log_writes_kthread(void *arg) 3420e9cebe7SJosef Bacik { 3430e9cebe7SJosef Bacik struct log_writes_c *lc = (struct log_writes_c *)arg; 3440e9cebe7SJosef Bacik sector_t sector = 0; 3450e9cebe7SJosef Bacik 3460e9cebe7SJosef Bacik while (!kthread_should_stop()) { 3470e9cebe7SJosef Bacik bool super = false; 3480e9cebe7SJosef Bacik bool logging_enabled; 3490e9cebe7SJosef Bacik struct pending_block *block = NULL; 3500e9cebe7SJosef Bacik int ret; 3510e9cebe7SJosef Bacik 3520e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 3530e9cebe7SJosef Bacik if (!list_empty(&lc->logging_blocks)) { 3540e9cebe7SJosef Bacik block = list_first_entry(&lc->logging_blocks, 3550e9cebe7SJosef Bacik struct pending_block, list); 3560e9cebe7SJosef Bacik list_del_init(&block->list); 3570e9cebe7SJosef Bacik if (!lc->logging_enabled) 3580e9cebe7SJosef Bacik goto next; 3590e9cebe7SJosef Bacik 3600e9cebe7SJosef Bacik sector = lc->next_sector; 3610e9cebe7SJosef Bacik if (block->flags & LOG_DISCARD_FLAG) 3620e9cebe7SJosef Bacik lc->next_sector++; 3630e9cebe7SJosef Bacik else 3640e9cebe7SJosef Bacik lc->next_sector += block->nr_sectors + 1; 3650e9cebe7SJosef Bacik 3660e9cebe7SJosef Bacik /* 3670e9cebe7SJosef Bacik * Apparently the size of the device may not be known 3680e9cebe7SJosef Bacik * right away, so handle this properly. 3690e9cebe7SJosef Bacik */ 3700e9cebe7SJosef Bacik if (!lc->end_sector) 3710e9cebe7SJosef Bacik lc->end_sector = logdev_last_sector(lc); 3720e9cebe7SJosef Bacik if (lc->end_sector && 3730e9cebe7SJosef Bacik lc->next_sector >= lc->end_sector) { 3740e9cebe7SJosef Bacik DMERR("Ran out of space on the logdev"); 3750e9cebe7SJosef Bacik lc->logging_enabled = false; 3760e9cebe7SJosef Bacik goto next; 3770e9cebe7SJosef Bacik } 3780e9cebe7SJosef Bacik lc->logged_entries++; 3790e9cebe7SJosef Bacik atomic_inc(&lc->io_blocks); 3800e9cebe7SJosef Bacik 3810e9cebe7SJosef Bacik super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG)); 3820e9cebe7SJosef Bacik if (super) 3830e9cebe7SJosef Bacik atomic_inc(&lc->io_blocks); 3840e9cebe7SJosef Bacik } 3850e9cebe7SJosef Bacik next: 3860e9cebe7SJosef Bacik logging_enabled = lc->logging_enabled; 3870e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 3880e9cebe7SJosef Bacik if (block) { 3890e9cebe7SJosef Bacik if (logging_enabled) { 3900e9cebe7SJosef Bacik ret = log_one_block(lc, block, sector); 3910e9cebe7SJosef Bacik if (!ret && super) 3920e9cebe7SJosef Bacik ret = log_super(lc); 3930e9cebe7SJosef Bacik if (ret) { 3940e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 3950e9cebe7SJosef Bacik lc->logging_enabled = false; 3960e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 3970e9cebe7SJosef Bacik } 3980e9cebe7SJosef Bacik } else 3990e9cebe7SJosef Bacik free_pending_block(lc, block); 4000e9cebe7SJosef Bacik continue; 4010e9cebe7SJosef Bacik } 4020e9cebe7SJosef Bacik 4030e9cebe7SJosef Bacik if (!try_to_freeze()) { 4040e9cebe7SJosef Bacik set_current_state(TASK_INTERRUPTIBLE); 4050e9cebe7SJosef Bacik if (!kthread_should_stop() && 4060e9cebe7SJosef Bacik !atomic_read(&lc->pending_blocks)) 4070e9cebe7SJosef Bacik schedule(); 4080e9cebe7SJosef Bacik __set_current_state(TASK_RUNNING); 4090e9cebe7SJosef Bacik } 4100e9cebe7SJosef Bacik } 4110e9cebe7SJosef Bacik return 0; 4120e9cebe7SJosef Bacik } 4130e9cebe7SJosef Bacik 4140e9cebe7SJosef Bacik /* 4150e9cebe7SJosef Bacik * Construct a log-writes mapping: 4160e9cebe7SJosef Bacik * log-writes <dev_path> <log_dev_path> 4170e9cebe7SJosef Bacik */ 4180e9cebe7SJosef Bacik static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv) 4190e9cebe7SJosef Bacik { 4200e9cebe7SJosef Bacik struct log_writes_c *lc; 4210e9cebe7SJosef Bacik struct dm_arg_set as; 4220e9cebe7SJosef Bacik const char *devname, *logdevname; 4230e9cebe7SJosef Bacik 4240e9cebe7SJosef Bacik as.argc = argc; 4250e9cebe7SJosef Bacik as.argv = argv; 4260e9cebe7SJosef Bacik 4270e9cebe7SJosef Bacik if (argc < 2) { 4280e9cebe7SJosef Bacik ti->error = "Invalid argument count"; 4290e9cebe7SJosef Bacik return -EINVAL; 4300e9cebe7SJosef Bacik } 4310e9cebe7SJosef Bacik 4320e9cebe7SJosef Bacik lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL); 4330e9cebe7SJosef Bacik if (!lc) { 4340e9cebe7SJosef Bacik ti->error = "Cannot allocate context"; 4350e9cebe7SJosef Bacik return -ENOMEM; 4360e9cebe7SJosef Bacik } 4370e9cebe7SJosef Bacik spin_lock_init(&lc->blocks_lock); 4380e9cebe7SJosef Bacik INIT_LIST_HEAD(&lc->unflushed_blocks); 4390e9cebe7SJosef Bacik INIT_LIST_HEAD(&lc->logging_blocks); 4400e9cebe7SJosef Bacik init_waitqueue_head(&lc->wait); 4410e9cebe7SJosef Bacik lc->sectorsize = 1 << SECTOR_SHIFT; 4420e9cebe7SJosef Bacik atomic_set(&lc->io_blocks, 0); 4430e9cebe7SJosef Bacik atomic_set(&lc->pending_blocks, 0); 4440e9cebe7SJosef Bacik 4450e9cebe7SJosef Bacik devname = dm_shift_arg(&as); 4460e9cebe7SJosef Bacik if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev)) { 4470e9cebe7SJosef Bacik ti->error = "Device lookup failed"; 4480e9cebe7SJosef Bacik goto bad; 4490e9cebe7SJosef Bacik } 4500e9cebe7SJosef Bacik 4510e9cebe7SJosef Bacik logdevname = dm_shift_arg(&as); 4520e9cebe7SJosef Bacik if (dm_get_device(ti, logdevname, dm_table_get_mode(ti->table), &lc->logdev)) { 4530e9cebe7SJosef Bacik ti->error = "Log device lookup failed"; 4540e9cebe7SJosef Bacik dm_put_device(ti, lc->dev); 4550e9cebe7SJosef Bacik goto bad; 4560e9cebe7SJosef Bacik } 4570e9cebe7SJosef Bacik 4580e9cebe7SJosef Bacik lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write"); 4590e9cebe7SJosef Bacik if (!lc->log_kthread) { 4600e9cebe7SJosef Bacik ti->error = "Couldn't alloc kthread"; 4610e9cebe7SJosef Bacik dm_put_device(ti, lc->dev); 4620e9cebe7SJosef Bacik dm_put_device(ti, lc->logdev); 4630e9cebe7SJosef Bacik goto bad; 4640e9cebe7SJosef Bacik } 4650e9cebe7SJosef Bacik 4660e9cebe7SJosef Bacik /* We put the super at sector 0, start logging at sector 1 */ 4670e9cebe7SJosef Bacik lc->next_sector = 1; 4680e9cebe7SJosef Bacik lc->logging_enabled = true; 4690e9cebe7SJosef Bacik lc->end_sector = logdev_last_sector(lc); 4700e9cebe7SJosef Bacik lc->device_supports_discard = true; 4710e9cebe7SJosef Bacik 4720e9cebe7SJosef Bacik ti->num_flush_bios = 1; 4730e9cebe7SJosef Bacik ti->flush_supported = true; 4740e9cebe7SJosef Bacik ti->num_discard_bios = 1; 4750e9cebe7SJosef Bacik ti->discards_supported = true; 4760e9cebe7SJosef Bacik ti->per_bio_data_size = sizeof(struct per_bio_data); 4770e9cebe7SJosef Bacik ti->private = lc; 4780e9cebe7SJosef Bacik return 0; 4790e9cebe7SJosef Bacik 4800e9cebe7SJosef Bacik bad: 4810e9cebe7SJosef Bacik kfree(lc); 4820e9cebe7SJosef Bacik return -EINVAL; 4830e9cebe7SJosef Bacik } 4840e9cebe7SJosef Bacik 4850e9cebe7SJosef Bacik static int log_mark(struct log_writes_c *lc, char *data) 4860e9cebe7SJosef Bacik { 4870e9cebe7SJosef Bacik struct pending_block *block; 4880e9cebe7SJosef Bacik size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry); 4890e9cebe7SJosef Bacik 4900e9cebe7SJosef Bacik block = kzalloc(sizeof(struct pending_block), GFP_KERNEL); 4910e9cebe7SJosef Bacik if (!block) { 4920e9cebe7SJosef Bacik DMERR("Error allocating pending block"); 4930e9cebe7SJosef Bacik return -ENOMEM; 4940e9cebe7SJosef Bacik } 4950e9cebe7SJosef Bacik 4960e9cebe7SJosef Bacik block->data = kstrndup(data, maxsize, GFP_KERNEL); 4970e9cebe7SJosef Bacik if (!block->data) { 4980e9cebe7SJosef Bacik DMERR("Error copying mark data"); 4990e9cebe7SJosef Bacik kfree(block); 5000e9cebe7SJosef Bacik return -ENOMEM; 5010e9cebe7SJosef Bacik } 5020e9cebe7SJosef Bacik atomic_inc(&lc->pending_blocks); 5030e9cebe7SJosef Bacik block->datalen = strlen(block->data); 5040e9cebe7SJosef Bacik block->flags |= LOG_MARK_FLAG; 5050e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 5060e9cebe7SJosef Bacik list_add_tail(&block->list, &lc->logging_blocks); 5070e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 5080e9cebe7SJosef Bacik wake_up_process(lc->log_kthread); 5090e9cebe7SJosef Bacik return 0; 5100e9cebe7SJosef Bacik } 5110e9cebe7SJosef Bacik 5120e9cebe7SJosef Bacik static void log_writes_dtr(struct dm_target *ti) 5130e9cebe7SJosef Bacik { 5140e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 5150e9cebe7SJosef Bacik 5160e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 5170e9cebe7SJosef Bacik list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks); 5180e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 5190e9cebe7SJosef Bacik 5200e9cebe7SJosef Bacik /* 5210e9cebe7SJosef Bacik * This is just nice to have since it'll update the super to include the 5220e9cebe7SJosef Bacik * unflushed blocks, if it fails we don't really care. 5230e9cebe7SJosef Bacik */ 5240e9cebe7SJosef Bacik log_mark(lc, "dm-log-writes-end"); 5250e9cebe7SJosef Bacik wake_up_process(lc->log_kthread); 5260e9cebe7SJosef Bacik wait_event(lc->wait, !atomic_read(&lc->io_blocks) && 5270e9cebe7SJosef Bacik !atomic_read(&lc->pending_blocks)); 5280e9cebe7SJosef Bacik kthread_stop(lc->log_kthread); 5290e9cebe7SJosef Bacik 5300e9cebe7SJosef Bacik WARN_ON(!list_empty(&lc->logging_blocks)); 5310e9cebe7SJosef Bacik WARN_ON(!list_empty(&lc->unflushed_blocks)); 5320e9cebe7SJosef Bacik dm_put_device(ti, lc->dev); 5330e9cebe7SJosef Bacik dm_put_device(ti, lc->logdev); 5340e9cebe7SJosef Bacik kfree(lc); 5350e9cebe7SJosef Bacik } 5360e9cebe7SJosef Bacik 5370e9cebe7SJosef Bacik static void normal_map_bio(struct dm_target *ti, struct bio *bio) 5380e9cebe7SJosef Bacik { 5390e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 5400e9cebe7SJosef Bacik 5410e9cebe7SJosef Bacik bio->bi_bdev = lc->dev->bdev; 5420e9cebe7SJosef Bacik } 5430e9cebe7SJosef Bacik 5440e9cebe7SJosef Bacik static int log_writes_map(struct dm_target *ti, struct bio *bio) 5450e9cebe7SJosef Bacik { 5460e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 5470e9cebe7SJosef Bacik struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data)); 5480e9cebe7SJosef Bacik struct pending_block *block; 5490e9cebe7SJosef Bacik struct bvec_iter iter; 5500e9cebe7SJosef Bacik struct bio_vec bv; 5510e9cebe7SJosef Bacik size_t alloc_size; 5520e9cebe7SJosef Bacik int i = 0; 5530e9cebe7SJosef Bacik bool flush_bio = (bio->bi_rw & REQ_FLUSH); 5540e9cebe7SJosef Bacik bool fua_bio = (bio->bi_rw & REQ_FUA); 5550e9cebe7SJosef Bacik bool discard_bio = (bio->bi_rw & REQ_DISCARD); 5560e9cebe7SJosef Bacik 5570e9cebe7SJosef Bacik pb->block = NULL; 5580e9cebe7SJosef Bacik 5590e9cebe7SJosef Bacik /* Don't bother doing anything if logging has been disabled */ 5600e9cebe7SJosef Bacik if (!lc->logging_enabled) 5610e9cebe7SJosef Bacik goto map_bio; 5620e9cebe7SJosef Bacik 5630e9cebe7SJosef Bacik /* 5640e9cebe7SJosef Bacik * Map reads as normal. 5650e9cebe7SJosef Bacik */ 5660e9cebe7SJosef Bacik if (bio_data_dir(bio) == READ) 5670e9cebe7SJosef Bacik goto map_bio; 5680e9cebe7SJosef Bacik 5690e9cebe7SJosef Bacik /* No sectors and not a flush? Don't care */ 5700e9cebe7SJosef Bacik if (!bio_sectors(bio) && !flush_bio) 5710e9cebe7SJosef Bacik goto map_bio; 5720e9cebe7SJosef Bacik 5730e9cebe7SJosef Bacik /* 5740e9cebe7SJosef Bacik * Discards will have bi_size set but there's no actual data, so just 5750e9cebe7SJosef Bacik * allocate the size of the pending block. 5760e9cebe7SJosef Bacik */ 5770e9cebe7SJosef Bacik if (discard_bio) 5780e9cebe7SJosef Bacik alloc_size = sizeof(struct pending_block); 5790e9cebe7SJosef Bacik else 5800e9cebe7SJosef Bacik alloc_size = sizeof(struct pending_block) + sizeof(struct bio_vec) * bio_segments(bio); 5810e9cebe7SJosef Bacik 5820e9cebe7SJosef Bacik block = kzalloc(alloc_size, GFP_NOIO); 5830e9cebe7SJosef Bacik if (!block) { 5840e9cebe7SJosef Bacik DMERR("Error allocating pending block"); 5850e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 5860e9cebe7SJosef Bacik lc->logging_enabled = false; 5870e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 5880e9cebe7SJosef Bacik return -ENOMEM; 5890e9cebe7SJosef Bacik } 5900e9cebe7SJosef Bacik INIT_LIST_HEAD(&block->list); 5910e9cebe7SJosef Bacik pb->block = block; 5920e9cebe7SJosef Bacik atomic_inc(&lc->pending_blocks); 5930e9cebe7SJosef Bacik 5940e9cebe7SJosef Bacik if (flush_bio) 5950e9cebe7SJosef Bacik block->flags |= LOG_FLUSH_FLAG; 5960e9cebe7SJosef Bacik if (fua_bio) 5970e9cebe7SJosef Bacik block->flags |= LOG_FUA_FLAG; 5980e9cebe7SJosef Bacik if (discard_bio) 5990e9cebe7SJosef Bacik block->flags |= LOG_DISCARD_FLAG; 6000e9cebe7SJosef Bacik 6010e9cebe7SJosef Bacik block->sector = bio->bi_iter.bi_sector; 6020e9cebe7SJosef Bacik block->nr_sectors = bio_sectors(bio); 6030e9cebe7SJosef Bacik 6040e9cebe7SJosef Bacik /* We don't need the data, just submit */ 6050e9cebe7SJosef Bacik if (discard_bio) { 6060e9cebe7SJosef Bacik WARN_ON(flush_bio || fua_bio); 6070e9cebe7SJosef Bacik if (lc->device_supports_discard) 6080e9cebe7SJosef Bacik goto map_bio; 6090e9cebe7SJosef Bacik bio_endio(bio, 0); 6100e9cebe7SJosef Bacik return DM_MAPIO_SUBMITTED; 6110e9cebe7SJosef Bacik } 6120e9cebe7SJosef Bacik 6130e9cebe7SJosef Bacik /* Flush bio, splice the unflushed blocks onto this list and submit */ 6140e9cebe7SJosef Bacik if (flush_bio && !bio_sectors(bio)) { 6150e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 6160e9cebe7SJosef Bacik list_splice_init(&lc->unflushed_blocks, &block->list); 6170e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 6180e9cebe7SJosef Bacik goto map_bio; 6190e9cebe7SJosef Bacik } 6200e9cebe7SJosef Bacik 6210e9cebe7SJosef Bacik /* 6220e9cebe7SJosef Bacik * We will write this bio somewhere else way later so we need to copy 6230e9cebe7SJosef Bacik * the actual contents into new pages so we know the data will always be 6240e9cebe7SJosef Bacik * there. 6250e9cebe7SJosef Bacik * 6260e9cebe7SJosef Bacik * We do this because this could be a bio from O_DIRECT in which case we 6270e9cebe7SJosef Bacik * can't just hold onto the page until some later point, we have to 6280e9cebe7SJosef Bacik * manually copy the contents. 6290e9cebe7SJosef Bacik */ 6300e9cebe7SJosef Bacik bio_for_each_segment(bv, bio, iter) { 6310e9cebe7SJosef Bacik struct page *page; 6320e9cebe7SJosef Bacik void *src, *dst; 6330e9cebe7SJosef Bacik 6340e9cebe7SJosef Bacik page = alloc_page(GFP_NOIO); 6350e9cebe7SJosef Bacik if (!page) { 6360e9cebe7SJosef Bacik DMERR("Error allocing page"); 6370e9cebe7SJosef Bacik free_pending_block(lc, block); 6380e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 6390e9cebe7SJosef Bacik lc->logging_enabled = false; 6400e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 6410e9cebe7SJosef Bacik return -ENOMEM; 6420e9cebe7SJosef Bacik } 6430e9cebe7SJosef Bacik 6440e9cebe7SJosef Bacik src = kmap_atomic(bv.bv_page); 6450e9cebe7SJosef Bacik dst = kmap_atomic(page); 6460e9cebe7SJosef Bacik memcpy(dst, src + bv.bv_offset, bv.bv_len); 6470e9cebe7SJosef Bacik kunmap_atomic(dst); 6480e9cebe7SJosef Bacik kunmap_atomic(src); 6490e9cebe7SJosef Bacik block->vecs[i].bv_page = page; 6500e9cebe7SJosef Bacik block->vecs[i].bv_len = bv.bv_len; 6510e9cebe7SJosef Bacik block->vec_cnt++; 6520e9cebe7SJosef Bacik i++; 6530e9cebe7SJosef Bacik } 6540e9cebe7SJosef Bacik 6550e9cebe7SJosef Bacik /* Had a flush with data in it, weird */ 6560e9cebe7SJosef Bacik if (flush_bio) { 6570e9cebe7SJosef Bacik spin_lock_irq(&lc->blocks_lock); 6580e9cebe7SJosef Bacik list_splice_init(&lc->unflushed_blocks, &block->list); 6590e9cebe7SJosef Bacik spin_unlock_irq(&lc->blocks_lock); 6600e9cebe7SJosef Bacik } 6610e9cebe7SJosef Bacik map_bio: 6620e9cebe7SJosef Bacik normal_map_bio(ti, bio); 6630e9cebe7SJosef Bacik return DM_MAPIO_REMAPPED; 6640e9cebe7SJosef Bacik } 6650e9cebe7SJosef Bacik 6660e9cebe7SJosef Bacik static int normal_end_io(struct dm_target *ti, struct bio *bio, int error) 6670e9cebe7SJosef Bacik { 6680e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 6690e9cebe7SJosef Bacik struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data)); 6700e9cebe7SJosef Bacik 6710e9cebe7SJosef Bacik if (bio_data_dir(bio) == WRITE && pb->block) { 6720e9cebe7SJosef Bacik struct pending_block *block = pb->block; 6730e9cebe7SJosef Bacik unsigned long flags; 6740e9cebe7SJosef Bacik 6750e9cebe7SJosef Bacik spin_lock_irqsave(&lc->blocks_lock, flags); 6760e9cebe7SJosef Bacik if (block->flags & LOG_FLUSH_FLAG) { 6770e9cebe7SJosef Bacik list_splice_tail_init(&block->list, &lc->logging_blocks); 6780e9cebe7SJosef Bacik list_add_tail(&block->list, &lc->logging_blocks); 6790e9cebe7SJosef Bacik wake_up_process(lc->log_kthread); 6800e9cebe7SJosef Bacik } else if (block->flags & LOG_FUA_FLAG) { 6810e9cebe7SJosef Bacik list_add_tail(&block->list, &lc->logging_blocks); 6820e9cebe7SJosef Bacik wake_up_process(lc->log_kthread); 6830e9cebe7SJosef Bacik } else 6840e9cebe7SJosef Bacik list_add_tail(&block->list, &lc->unflushed_blocks); 6850e9cebe7SJosef Bacik spin_unlock_irqrestore(&lc->blocks_lock, flags); 6860e9cebe7SJosef Bacik } 6870e9cebe7SJosef Bacik 6880e9cebe7SJosef Bacik return error; 6890e9cebe7SJosef Bacik } 6900e9cebe7SJosef Bacik 6910e9cebe7SJosef Bacik /* 6920e9cebe7SJosef Bacik * INFO format: <logged entries> <highest allocated sector> 6930e9cebe7SJosef Bacik */ 6940e9cebe7SJosef Bacik static void log_writes_status(struct dm_target *ti, status_type_t type, 6950e9cebe7SJosef Bacik unsigned status_flags, char *result, 6960e9cebe7SJosef Bacik unsigned maxlen) 6970e9cebe7SJosef Bacik { 6980e9cebe7SJosef Bacik unsigned sz = 0; 6990e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 7000e9cebe7SJosef Bacik 7010e9cebe7SJosef Bacik switch (type) { 7020e9cebe7SJosef Bacik case STATUSTYPE_INFO: 7030e9cebe7SJosef Bacik DMEMIT("%llu %llu", lc->logged_entries, 7040e9cebe7SJosef Bacik (unsigned long long)lc->next_sector - 1); 7050e9cebe7SJosef Bacik if (!lc->logging_enabled) 7060e9cebe7SJosef Bacik DMEMIT(" logging_disabled"); 7070e9cebe7SJosef Bacik break; 7080e9cebe7SJosef Bacik 7090e9cebe7SJosef Bacik case STATUSTYPE_TABLE: 7100e9cebe7SJosef Bacik DMEMIT("%s %s", lc->dev->name, lc->logdev->name); 7110e9cebe7SJosef Bacik break; 7120e9cebe7SJosef Bacik } 7130e9cebe7SJosef Bacik } 7140e9cebe7SJosef Bacik 7150e9cebe7SJosef Bacik static int log_writes_ioctl(struct dm_target *ti, unsigned int cmd, 7160e9cebe7SJosef Bacik unsigned long arg) 7170e9cebe7SJosef Bacik { 7180e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 7190e9cebe7SJosef Bacik struct dm_dev *dev = lc->dev; 7200e9cebe7SJosef Bacik int r = 0; 7210e9cebe7SJosef Bacik 7220e9cebe7SJosef Bacik /* 7230e9cebe7SJosef Bacik * Only pass ioctls through if the device sizes match exactly. 7240e9cebe7SJosef Bacik */ 7250e9cebe7SJosef Bacik if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT) 7260e9cebe7SJosef Bacik r = scsi_verify_blk_ioctl(NULL, cmd); 7270e9cebe7SJosef Bacik 7280e9cebe7SJosef Bacik return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg); 7290e9cebe7SJosef Bacik } 7300e9cebe7SJosef Bacik 7310e9cebe7SJosef Bacik static int log_writes_merge(struct dm_target *ti, struct bvec_merge_data *bvm, 7320e9cebe7SJosef Bacik struct bio_vec *biovec, int max_size) 7330e9cebe7SJosef Bacik { 7340e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 7350e9cebe7SJosef Bacik struct request_queue *q = bdev_get_queue(lc->dev->bdev); 7360e9cebe7SJosef Bacik 7370e9cebe7SJosef Bacik if (!q->merge_bvec_fn) 7380e9cebe7SJosef Bacik return max_size; 7390e9cebe7SJosef Bacik 7400e9cebe7SJosef Bacik bvm->bi_bdev = lc->dev->bdev; 7410e9cebe7SJosef Bacik bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector); 7420e9cebe7SJosef Bacik 7430e9cebe7SJosef Bacik return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); 7440e9cebe7SJosef Bacik } 7450e9cebe7SJosef Bacik 7460e9cebe7SJosef Bacik static int log_writes_iterate_devices(struct dm_target *ti, 7470e9cebe7SJosef Bacik iterate_devices_callout_fn fn, 7480e9cebe7SJosef Bacik void *data) 7490e9cebe7SJosef Bacik { 7500e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 7510e9cebe7SJosef Bacik 7520e9cebe7SJosef Bacik return fn(ti, lc->dev, 0, ti->len, data); 7530e9cebe7SJosef Bacik } 7540e9cebe7SJosef Bacik 7550e9cebe7SJosef Bacik /* 7560e9cebe7SJosef Bacik * Messages supported: 7570e9cebe7SJosef Bacik * mark <mark data> - specify the marked data. 7580e9cebe7SJosef Bacik */ 7590e9cebe7SJosef Bacik static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv) 7600e9cebe7SJosef Bacik { 7610e9cebe7SJosef Bacik int r = -EINVAL; 7620e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 7630e9cebe7SJosef Bacik 7640e9cebe7SJosef Bacik if (argc != 2) { 7650e9cebe7SJosef Bacik DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc); 7660e9cebe7SJosef Bacik return r; 7670e9cebe7SJosef Bacik } 7680e9cebe7SJosef Bacik 7690e9cebe7SJosef Bacik if (!strcasecmp(argv[0], "mark")) 7700e9cebe7SJosef Bacik r = log_mark(lc, argv[1]); 7710e9cebe7SJosef Bacik else 7720e9cebe7SJosef Bacik DMWARN("Unrecognised log writes target message received: %s", argv[0]); 7730e9cebe7SJosef Bacik 7740e9cebe7SJosef Bacik return r; 7750e9cebe7SJosef Bacik } 7760e9cebe7SJosef Bacik 7770e9cebe7SJosef Bacik static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits) 7780e9cebe7SJosef Bacik { 7790e9cebe7SJosef Bacik struct log_writes_c *lc = ti->private; 7800e9cebe7SJosef Bacik struct request_queue *q = bdev_get_queue(lc->dev->bdev); 7810e9cebe7SJosef Bacik 7820e9cebe7SJosef Bacik if (!q || !blk_queue_discard(q)) { 7830e9cebe7SJosef Bacik lc->device_supports_discard = false; 7840e9cebe7SJosef Bacik limits->discard_granularity = 1 << SECTOR_SHIFT; 7850e9cebe7SJosef Bacik limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT); 7860e9cebe7SJosef Bacik } 7870e9cebe7SJosef Bacik } 7880e9cebe7SJosef Bacik 7890e9cebe7SJosef Bacik static struct target_type log_writes_target = { 7900e9cebe7SJosef Bacik .name = "log-writes", 7910e9cebe7SJosef Bacik .version = {1, 0, 0}, 7920e9cebe7SJosef Bacik .module = THIS_MODULE, 7930e9cebe7SJosef Bacik .ctr = log_writes_ctr, 7940e9cebe7SJosef Bacik .dtr = log_writes_dtr, 7950e9cebe7SJosef Bacik .map = log_writes_map, 7960e9cebe7SJosef Bacik .end_io = normal_end_io, 7970e9cebe7SJosef Bacik .status = log_writes_status, 7980e9cebe7SJosef Bacik .ioctl = log_writes_ioctl, 7990e9cebe7SJosef Bacik .merge = log_writes_merge, 8000e9cebe7SJosef Bacik .message = log_writes_message, 8010e9cebe7SJosef Bacik .iterate_devices = log_writes_iterate_devices, 8020e9cebe7SJosef Bacik .io_hints = log_writes_io_hints, 8030e9cebe7SJosef Bacik }; 8040e9cebe7SJosef Bacik 8050e9cebe7SJosef Bacik static int __init dm_log_writes_init(void) 8060e9cebe7SJosef Bacik { 8070e9cebe7SJosef Bacik int r = dm_register_target(&log_writes_target); 8080e9cebe7SJosef Bacik 8090e9cebe7SJosef Bacik if (r < 0) 8100e9cebe7SJosef Bacik DMERR("register failed %d", r); 8110e9cebe7SJosef Bacik 8120e9cebe7SJosef Bacik return r; 8130e9cebe7SJosef Bacik } 8140e9cebe7SJosef Bacik 8150e9cebe7SJosef Bacik static void __exit dm_log_writes_exit(void) 8160e9cebe7SJosef Bacik { 8170e9cebe7SJosef Bacik dm_unregister_target(&log_writes_target); 8180e9cebe7SJosef Bacik } 8190e9cebe7SJosef Bacik 8200e9cebe7SJosef Bacik module_init(dm_log_writes_init); 8210e9cebe7SJosef Bacik module_exit(dm_log_writes_exit); 8220e9cebe7SJosef Bacik 8230e9cebe7SJosef Bacik MODULE_DESCRIPTION(DM_NAME " log writes target"); 8240e9cebe7SJosef Bacik MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>"); 8250e9cebe7SJosef Bacik MODULE_LICENSE("GPL"); 826