Skip to content

Commit

Permalink
ubi: block: Warn if volume size is not multiple of 512
Browse files Browse the repository at this point in the history
If volume size is not a multiple of 512, ubi block cuts
off the last bytes of an volume since the block layer works
on 512 byte sectors.
This can happen especially on NOR flash with minimal io
size of 1.

To avoid unpleasant surprises, print a warning.

Signed-off-by: Richard Weinberger <[email protected]>
  • Loading branch information
richardweinberger committed Sep 15, 2019
1 parent 9163e01 commit e46131b
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions drivers/mtd/ubi/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,36 @@ static const struct blk_mq_ops ubiblock_mq_ops = {
.init_request = ubiblock_init_request,
};

static int calc_disk_capacity(struct ubi_volume_info *vi, u64 *disk_capacity)
{
u64 size = vi->used_bytes >> 9;

if (vi->used_bytes % 512) {
pr_warn("UBI: block: volume size is not a multiple of 512, "
"last %llu bytes are ignored!\n",
vi->used_bytes - (size << 9));
}

if ((sector_t)size != size)
return -EFBIG;

*disk_capacity = size;

return 0;
}

int ubiblock_create(struct ubi_volume_info *vi)
{
struct ubiblock *dev;
struct gendisk *gd;
u64 disk_capacity = vi->used_bytes >> 9;
u64 disk_capacity;
int ret;

if ((sector_t)disk_capacity != disk_capacity)
return -EFBIG;
ret = calc_disk_capacity(vi, &disk_capacity);
if (ret) {
return ret;
}

/* Check that the volume isn't already handled */
mutex_lock(&devices_mutex);
if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
Expand Down Expand Up @@ -507,7 +528,8 @@ int ubiblock_remove(struct ubi_volume_info *vi)
static int ubiblock_resize(struct ubi_volume_info *vi)
{
struct ubiblock *dev;
u64 disk_capacity = vi->used_bytes >> 9;
u64 disk_capacity;
int ret;

/*
* Need to lock the device list until we stop using the device,
Expand All @@ -520,11 +542,16 @@ static int ubiblock_resize(struct ubi_volume_info *vi)
mutex_unlock(&devices_mutex);
return -ENODEV;
}
if ((sector_t)disk_capacity != disk_capacity) {

ret = calc_disk_capacity(vi, &disk_capacity);
if (ret) {
mutex_unlock(&devices_mutex);
dev_warn(disk_to_dev(dev->gd), "the volume is too big (%d LEBs), cannot resize",
vi->size);
return -EFBIG;
if (ret == -EFBIG) {
dev_warn(disk_to_dev(dev->gd),
"the volume is too big (%d LEBs), cannot resize",
vi->size);
}
return ret;
}

mutex_lock(&dev->dev_mutex);
Expand Down

0 comments on commit e46131b

Please sign in to comment.