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