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)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 20 Apr 2022 07:08:08 +0000 (09:08 +0200)
commit 570b1cac477643cbf01a45fa5d018430a1fddbce upstream.

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>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/blkdev.h

index 1c3d774d3c8391340db972532ab735357c42fe14..afbe2fcc476ac8ec48def343245f3187eccbcbd6 100644 (file)
@@ -56,6 +56,14 @@ struct blk_stat_callback;
  */
 #define BLKCG_MAX_POLS         3
 
+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;
+}
+
 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
 
 #define BLK_RL_SYNCFULL                (1U << 0)