block: reorganize rounding of max_discard_sectors

Mostly a preparation for the next patch.

In principle this fixes an infinite loop if max_discard_sectors < granularity,
but that really shouldn't happen.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Acked-by: Vivek Goyal <vgoyal@redhat.com>
Tested-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block: split discard into aligned requests

When a disk has large discard_granularity and small max_discard_sectors,
discards are not split with optimal alignment.  In the limit case of
discard_granularity == max_discard_sectors, no request could be aligned
correctly, so in fact you might end up with no discarded logical blocks
at all.

Another example that helps showing the condition in the patch is with
discard_granularity == 64, max_discard_sectors == 128.  A request that is
submitted for 256 sectors 2..257 will be split in two: 2..129, 130..257.
However, only 2 aligned blocks out of 3 are included in the request;
128..191 may be left intact and not discarded.  With this patch, the
first request will be truncated to ensure good alignment of what's left,
and the split will be 2..127, 128..255, 256..257.  The patch will also
take into account the discard_alignment.

At most one extra request will be introduced, because the first request
will be reduced by at most granularity-1 sectors, and granularity
must be less than max_discard_sectors.  Subsequent requests will run
on round_down(max_discard_sectors, granularity) sectors, as in the
current code.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Acked-by: Vivek Goyal <vgoyal@redhat.com>
Tested-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
dm thin: allow discards even if block size is not a power of 2

The block layer assumes discard_granularity is a power of 2, so changes
are needed to allow discards when a pool is using a block size that is
not a power of 2.  This patch depends on commit c6e666345e1b79c62b
("block: split discard into aligned requests").

If discard passdown is enabled: use the data device's
discard_granularity but make sure block size is a multiple of it.  Also
make sure the data device's discard_max_sectors is not smaller than the
pool's block size.

If discard passdown is disabled: use a discard_granularity of
DATA_DEV_BLOCK_SIZE_MIN_SECTORS if block size isn't a power of 2.  The
pool's block size is a multiple of DATA_DEV_BLOCK_SIZE_MIN_SECTORS
(which is a power of 2).

Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Merge remote branch 'ejt/thin-dev' into thin-dev
dm thin: fix set_discard_limits for discard_passdown case

discard_granularity was alwyas getting set as of discard_passdown was
disabled.
dm thin: use proper pool_features structure when establishing discard limits

pool_io_hints() is executed _before_ the pool has been resumed; so pt->pf
must be used instead of pool->pf (long standing bug).  BUT when
disabling discard_passdown we cannot use pt->pf to do so because that
would result in a thin-pool table line conflict (which lvm2 would balk
at); so introduce a 'discard_passdown_override' member in the
pool_features structure (use it to reestablish the
pool->pf.discard_passdown after pt->pf is handed to pool->pf).

Not seeing how we avoid this nuanced complexity...
Merge branch 'thin-dev' of https://github.com/snitm/linux into mike
[dm-thin] io limits WIP
more WIP
more WIP
more WIP
[dm-cache] knock out a redundant parameter for disable_passdown_if_not_supported
use & rather than %
[dm-cache] maintain an immutable version of the pool_features for
reporting the table line.
dm thin: various discard cleanups (whitespace, formatting, etc)
[dm-cache] discard fluff
[dm-fake-discard] initial code drop
[dm-thin] more tidying of the discard logic
block: disable discard request merge temporarily

The SCSI discard request merge never worked, and looks no solution
for in future, let's disable it temporarily.

Signed-off-by: Shaohua Li <shli@fusionio.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Merge remote branch 'ejt/thin-dev' into thin-discard2