[PATCH 12/26] block: define helpers for non-512-byte sector support
Ahmad Fatoum <[email protected]> Fri, 26 Jun 2026 10:42:23 +0200
| Newsgroups | org.infradead.lists.barebox |
|---|---|
| Message-ID | <[email protected]> |
Instead of opencoding the checks at the various direct and indirect (via cdev API) block device users that are going to be switched away from hardcoding the 512 byte sector assumption, define some helpers. Signed-off-by: Ahmad Fatoum <[email protected]> --- common/block.c | 11 +++++++++++ include/block.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/common/block.c b/common/block.c index 9d3865626555..5f509bfb9df3 100644 --- a/common/block.c +++ b/common/block.c @@ -14,6 +14,7 @@ #include <range.h> #include <bootargs.h> #include <file-list.h> +#include <linux/log2.h> LIST_HEAD(block_device_list); EXPORT_SYMBOL(block_device_list); @@ -53,6 +54,16 @@ static void blk_stats_record_write(struct block_device *blk, blkcnt_t count) { } static void blk_stats_record_erase(struct block_device *blk, blkcnt_t count) { } #endif +int block_size_bits(struct device *dev, unsigned block_size) +{ + if (block_size < MIN_SECTOR_SIZE || !is_power_of_2(block_size)) { + dev_err(dev, "unsupported block size %u\n", block_size); + return -ENOTSUPP; + } + + return ffs(block_size) - 1; +} + static int chunk_flush(struct block_device *blk, struct chunk *chunk) { blkcnt_t len; diff --git a/include/block.h b/include/block.h index fa916f8d4ee7..9a0102db1565 100644 --- a/include/block.h +++ b/include/block.h @@ -3,6 +3,7 @@ #define __BLOCK_H #include <driver.h> +#include <disks.h> #include <linux/list.h> #include <linux/types.h> @@ -68,6 +69,28 @@ struct block_device { #define BLOCKSIZE(blk) (1u << (blk)->blockbits) +int block_size_bits(struct device *dev, unsigned block_size); + +static inline u64 blockdevice_size(const struct block_device *blk) +{ + return blk->num_blocks << blk->blockbits; +} + +static inline blkcnt_t +blockdevice_round_nblocks(const struct block_device *blk, u64 nbytes) +{ + if (nbytes == 0) + return 0; + + return (((u64)nbytes - 1) >> blk->blockbits) + 1; +} + +static inline u64 +blockdevice_round_block_nbytes(const struct block_device *blk, u64 nbytes) +{ + return blockdevice_round_nblocks(blk, nbytes) << blk->blockbits; +} + extern struct list_head block_device_list; #define for_each_block_device(bdev) list_for_each_entry(bdev, &block_device_list, list) @@ -126,4 +149,15 @@ static inline struct block_device *cdev_get_block_device(const struct cdev *cdev return cdev_is_block_device(cdev) ? cdev->priv : NULL; } +static inline unsigned cdev_blockbits(const struct cdev *cdev) +{ + struct block_device *bdev = cdev_get_block_device(cdev); + return bdev ? bdev->blockbits : SECTOR_SHIFT; +} + +static inline unsigned cdev_blocksize(const struct cdev *cdev) +{ + return 1u << cdev_blockbits(cdev); +} + #endif /* __BLOCK_H */ -- 2.47.3