block: Add a helper to validate the block size
authorXie Yongji <xieyongji@bytedance.com>
Tue, 26 Oct 2021 14:40:12 +0000 (22:40 +0800)
committerJens Axboe <axboe@kernel.dk>
Wed, 27 Oct 2021 20:15:51 +0000 (14:15 -0600)
There are some duplicated codes to validate the block
size in block drivers. This limitation actually comes
from block layer, so this patch tries to add a new block
layer helper for that.

Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
Link: https://lore.kernel.org/r/20211026144015.188-2-xieyongji@bytedance.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/blkdev.h

index 6d95a4b36cfa7b0da4f1499dab3c6708ed654402..d2d627e2c782e7ec636c94726d6711c1206a2853 100644 (file)
@@ -44,6 +44,14 @@ struct blk_crypto_profile;
  */
 #define BLKCG_MAX_POLS         6
 
+static inline int blk_validate_block_size(unsigned int bsize)
+{
+       if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
+               return -EINVAL;
+
+       return 0;
+}
+
 static inline bool blk_op_is_passthrough(unsigned int op)
 {
        op &= REQ_OP_MASK;