dm log writes: fix >512b sectorsize support

512b sectors vs device's physical sectorsize was not maintained
consistently and as such the support for >512b sector devices has bugs.
The log metadata expects native sectorsize but 512b sectors were being
stored. Also, device's sectorsize was assumed when assigning the
bi_sector for blocks that were being logged.

Fix this up by adding two helpers to convert between bio and dev
sectors, and use these in the appropriate places to fix the problem and
make it clear which units go where. Doing so allows dm-log-writes use
with 4k devices.

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>

authored by

Josef Bacik and committed by
Mike Snitzer
228bb5b2 0c79c620

+31 -11
+31 -11
drivers/md/dm-log-writes.c
··· 100 struct dm_dev *logdev; 101 u64 logged_entries; 102 u32 sectorsize; 103 atomic_t io_blocks; 104 atomic_t pending_blocks; 105 sector_t next_sector; ··· 128 struct per_bio_data { 129 struct pending_block *block; 130 }; 131 132 static void put_pending_block(struct log_writes_c *lc) 133 { ··· 266 267 if (!block->vec_cnt) 268 goto out; 269 - sector++; 270 271 atomic_inc(&lc->io_blocks); 272 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES)); ··· 367 goto next; 368 369 sector = lc->next_sector; 370 - if (block->flags & LOG_DISCARD_FLAG) 371 - lc->next_sector++; 372 - else 373 - lc->next_sector += block->nr_sectors + 1; 374 375 /* 376 * Apparently the size of the device may not be known ··· 447 INIT_LIST_HEAD(&lc->unflushed_blocks); 448 INIT_LIST_HEAD(&lc->logging_blocks); 449 init_waitqueue_head(&lc->wait); 450 - lc->sectorsize = 1 << SECTOR_SHIFT; 451 atomic_set(&lc->io_blocks, 0); 452 atomic_set(&lc->pending_blocks, 0); 453 ··· 466 goto bad; 467 } 468 469 lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write"); 470 if (IS_ERR(lc->log_kthread)) { 471 ret = PTR_ERR(lc->log_kthread); ··· 477 goto bad; 478 } 479 480 - /* We put the super at sector 0, start logging at sector 1 */ 481 - lc->next_sector = 1; 482 lc->logging_enabled = true; 483 lc->end_sector = logdev_last_sector(lc); 484 lc->device_supports_discard = true; ··· 616 if (discard_bio) 617 block->flags |= LOG_DISCARD_FLAG; 618 619 - block->sector = bio->bi_iter.bi_sector; 620 - block->nr_sectors = bio_sectors(bio); 621 622 /* We don't need the data, just submit */ 623 if (discard_bio) { ··· 784 785 if (!q || !blk_queue_discard(q)) { 786 lc->device_supports_discard = false; 787 - limits->discard_granularity = 1 << SECTOR_SHIFT; 788 limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT); 789 } 790 } 791 792 static struct target_type log_writes_target = {
··· 100 struct dm_dev *logdev; 101 u64 logged_entries; 102 u32 sectorsize; 103 + u32 sectorshift; 104 atomic_t io_blocks; 105 atomic_t pending_blocks; 106 sector_t next_sector; ··· 127 struct per_bio_data { 128 struct pending_block *block; 129 }; 130 + 131 + static inline sector_t bio_to_dev_sectors(struct log_writes_c *lc, 132 + sector_t sectors) 133 + { 134 + return sectors >> (lc->sectorshift - SECTOR_SHIFT); 135 + } 136 + 137 + static inline sector_t dev_to_bio_sectors(struct log_writes_c *lc, 138 + sector_t sectors) 139 + { 140 + return sectors << (lc->sectorshift - SECTOR_SHIFT); 141 + } 142 143 static void put_pending_block(struct log_writes_c *lc) 144 { ··· 253 254 if (!block->vec_cnt) 255 goto out; 256 + sector += dev_to_bio_sectors(lc, 1); 257 258 atomic_inc(&lc->io_blocks); 259 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES)); ··· 354 goto next; 355 356 sector = lc->next_sector; 357 + if (!(block->flags & LOG_DISCARD_FLAG)) 358 + lc->next_sector += dev_to_bio_sectors(lc, block->nr_sectors); 359 + lc->next_sector += dev_to_bio_sectors(lc, 1); 360 361 /* 362 * Apparently the size of the device may not be known ··· 435 INIT_LIST_HEAD(&lc->unflushed_blocks); 436 INIT_LIST_HEAD(&lc->logging_blocks); 437 init_waitqueue_head(&lc->wait); 438 atomic_set(&lc->io_blocks, 0); 439 atomic_set(&lc->pending_blocks, 0); 440 ··· 455 goto bad; 456 } 457 458 + lc->sectorsize = bdev_logical_block_size(lc->dev->bdev); 459 + lc->sectorshift = ilog2(lc->sectorsize); 460 lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write"); 461 if (IS_ERR(lc->log_kthread)) { 462 ret = PTR_ERR(lc->log_kthread); ··· 464 goto bad; 465 } 466 467 + /* 468 + * next_sector is in 512b sectors to correspond to what bi_sector expects. 469 + * The super starts at sector 0, and the next_sector is the next logical 470 + * one based on the sectorsize of the device. 471 + */ 472 + lc->next_sector = lc->sectorsize >> SECTOR_SHIFT; 473 lc->logging_enabled = true; 474 lc->end_sector = logdev_last_sector(lc); 475 lc->device_supports_discard = true; ··· 599 if (discard_bio) 600 block->flags |= LOG_DISCARD_FLAG; 601 602 + block->sector = bio_to_dev_sectors(lc, bio->bi_iter.bi_sector); 603 + block->nr_sectors = bio_to_dev_sectors(lc, bio_sectors(bio)); 604 605 /* We don't need the data, just submit */ 606 if (discard_bio) { ··· 767 768 if (!q || !blk_queue_discard(q)) { 769 lc->device_supports_discard = false; 770 + limits->discard_granularity = lc->sectorsize; 771 limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT); 772 } 773 + limits->logical_block_size = bdev_logical_block_size(lc->dev->bdev); 774 + limits->physical_block_size = bdev_physical_block_size(lc->dev->bdev); 775 + limits->io_min = limits->physical_block_size; 776 } 777 778 static struct target_type log_writes_target = {