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