Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ubi: block: Warn if volume size is not multiple of 512

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 <richard@nod.at>

+35 -8
+35 -8
drivers/mtd/ubi/block.c
··· 345 345 .init_request = ubiblock_init_request, 346 346 }; 347 347 348 + static int calc_disk_capacity(struct ubi_volume_info *vi, u64 *disk_capacity) 349 + { 350 + u64 size = vi->used_bytes >> 9; 351 + 352 + if (vi->used_bytes % 512) { 353 + pr_warn("UBI: block: volume size is not a multiple of 512, " 354 + "last %llu bytes are ignored!\n", 355 + vi->used_bytes - (size << 9)); 356 + } 357 + 358 + if ((sector_t)size != size) 359 + return -EFBIG; 360 + 361 + *disk_capacity = size; 362 + 363 + return 0; 364 + } 365 + 348 366 int ubiblock_create(struct ubi_volume_info *vi) 349 367 { 350 368 struct ubiblock *dev; 351 369 struct gendisk *gd; 352 - u64 disk_capacity = vi->used_bytes >> 9; 370 + u64 disk_capacity; 353 371 int ret; 354 372 355 - if ((sector_t)disk_capacity != disk_capacity) 356 - return -EFBIG; 373 + ret = calc_disk_capacity(vi, &disk_capacity); 374 + if (ret) { 375 + return ret; 376 + } 377 + 357 378 /* Check that the volume isn't already handled */ 358 379 mutex_lock(&devices_mutex); 359 380 if (find_dev_nolock(vi->ubi_num, vi->vol_id)) { ··· 528 507 static int ubiblock_resize(struct ubi_volume_info *vi) 529 508 { 530 509 struct ubiblock *dev; 531 - u64 disk_capacity = vi->used_bytes >> 9; 510 + u64 disk_capacity; 511 + int ret; 532 512 533 513 /* 534 514 * Need to lock the device list until we stop using the device, ··· 542 520 mutex_unlock(&devices_mutex); 543 521 return -ENODEV; 544 522 } 545 - if ((sector_t)disk_capacity != disk_capacity) { 523 + 524 + ret = calc_disk_capacity(vi, &disk_capacity); 525 + if (ret) { 546 526 mutex_unlock(&devices_mutex); 547 - dev_warn(disk_to_dev(dev->gd), "the volume is too big (%d LEBs), cannot resize", 548 - vi->size); 549 - return -EFBIG; 527 + if (ret == -EFBIG) { 528 + dev_warn(disk_to_dev(dev->gd), 529 + "the volume is too big (%d LEBs), cannot resize", 530 + vi->size); 531 + } 532 + return ret; 550 533 } 551 534 552 535 mutex_lock(&dev->dev_mutex);